/* ── Reset & Variables ────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:          #0d1117;
  --surface:     #161b22;
  --surface-2:   #21262d;
  --border:      #30363d;
  --text:        #c9d1d9;
  --text-muted:  #8b949e;
  --primary:     #58a6ff;
  --primary-dim: #1f6feb;
  --success:     #3fb950;
  --warning:     #d29922;
  --danger:      #f85149;
  --purple:      #bc8cff;
  --radius:      6px;
  --shadow:      0 1px 3px rgba(0,0,0,.4);
}

html { font-size: 15px; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
  line-height: 1.5;
  min-height: 100vh;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
code { font-family: 'SFMono-Regular', Consolas, monospace; font-size: .875em; background: var(--surface-2); padding: 1px 5px; border-radius: 3px; }

/* ── Layout ───────────────────────────────────────────────────────────── */
.container { max-width: 1200px; margin: 0 auto; padding: 1.5rem 1.25rem; }

/* ── Navbar ───────────────────────────────────────────────────────────── */
.navbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding: .625rem 1.25rem;
  position: sticky;
  top: 0;
  z-index: 100;
}
.navbar-brand a {
  font-weight: 600;
  font-size: 1rem;
  color: var(--text);
  letter-spacing: -.01em;
}
.navbar-brand a:hover { text-decoration: none; color: var(--primary); }
.navbar-links { display: flex; gap: .25rem; flex: 1; }
.navbar-links a {
  color: var(--text-muted);
  padding: .375rem .75rem;
  border-radius: var(--radius);
  font-size: .875rem;
}
.navbar-links a:hover { color: var(--text); background: var(--surface-2); text-decoration: none; }
.navbar-links a.active { color: var(--text); background: var(--surface-2); }
.navbar-user { display: flex; align-items: center; gap: .75rem; }
.user-name { color: var(--text-muted); font-size: .875rem; }
.btn-logout {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  padding: .25rem .625rem;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: .8125rem;
}
.btn-logout:hover { color: var(--text); border-color: var(--text-muted); }

/* ── Page Header ──────────────────────────────────────────────────────── */
.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.page-header h1 { font-size: 1.375rem; font-weight: 600; }
.header-actions { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.breadcrumb { color: var(--text-muted); font-size: .875rem; display: block; margin-bottom: .25rem; }
.breadcrumb:hover { color: var(--primary); }
.source-url { font-size: .8125rem; margin-top: .125rem; word-break: break-all; }

/* ── Cards ────────────────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  margin-bottom: 1.25rem;
}
.card h2 { font-size: 1rem; font-weight: 600; margin-bottom: 1rem; }

/* ── Source Section (dashboard) ──────────────────────────────────────── */
.source-section {
  margin-bottom: 2.5rem;
}
.source-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
  padding-bottom: .75rem;
  border-bottom: 1px solid var(--border);
}
.source-section-title {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: .5rem;
}
.source-section-title h2 { font-size: 1.125rem; font-weight: 600; }
.source-section-title h2 a { color: var(--text); }
.source-section-title h2 a:hover { color: var(--primary); text-decoration: none; }
.fetch-meta { font-size: .8125rem; }

/* 2-column aggregation block grid */
.agg-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1rem;
}
.agg-block {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: .625rem;
}
.agg-block-title {
  font-size: .75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-muted);
}

/* Stacked bar */
.agg-bar {
  display: flex;
  height: 8px;
  border-radius: 4px;
  overflow: hidden;
  background: var(--surface-2);
  gap: 1px;
}
.agg-bar-segment { height: 100%; min-width: 2px; }

/* Legend */
.agg-legend {
  display: flex;
  flex-direction: column;
  gap: .25rem;
}
.agg-legend-row {
  display: flex;
  align-items: center;
  gap: .375rem;
  font-size: .8125rem;
  line-height: 1.3;
}
.agg-dot {
  flex-shrink: 0;
  width: 8px;
  height: 8px;
  border-radius: 50%;
}
.agg-key {
  flex: 1;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.agg-count {
  font-variant-numeric: tabular-nums;
  color: var(--text);
  font-weight: 600;
  min-width: 2ch;
  text-align: right;
}
.agg-pct {
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
  min-width: 4.5ch;
  text-align: right;
}

/* ── Charts Section (source page) ────────────────────────────────────── */
.chart-card-full {
  grid-column: 1 / -1;  /* span both columns */
  margin-bottom: 1rem;
}
.chart-header-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: .75rem;
  margin-bottom: .75rem;
}
.chart-filter-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: .5rem;
}
.filter-label { font-size: .8125rem; color: var(--text-muted); }
.filter-presets { display: flex; align-items: center; flex-wrap: wrap; gap: .25rem; }
.preset-btn.active { background: var(--primary-dim); color: #fff; border-color: var(--primary-dim); }
.filter-search { display: flex; align-items: center; gap: .25rem; }
.form-control-sm { padding: .25rem .5rem; font-size: .8125rem; width: 180px; }
.active-filter {
  font-size: .8125rem;
  color: var(--text-muted);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .375rem .75rem;
  margin-bottom: .75rem;
}
.active-filter strong { color: var(--primary); }
.chart-canvas-tall { max-height: 380px; }

/* ── Collapsable (details/summary) ───────────────────────────────────── */
.collapsable { border: none; }
.collapsable-header {
  display: flex;
  align-items: center;
  gap: .75rem;
  cursor: pointer;
  list-style: none;
  padding: .5rem 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 0;
  user-select: none;
}
.collapsable-header::-webkit-details-marker { display: none; }
.collapsable-header h2 {
  font-size: .75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-muted);
  margin: 0;
  pointer-events: none;
}
.collapsable-header::before {
  content: '▶';
  font-size: .625rem;
  color: var(--text-muted);
  transition: transform .15s;
  flex-shrink: 0;
}
details[open] .collapsable-header::before { transform: rotate(90deg); }
.collapsable-hint {
  font-size: .75rem;
  margin-left: auto;
}
details[open] .collapsable-hint { display: none; }

/* Mini chart */
.mini-chart-wrap {
  margin-top: .375rem;
  padding-top: .625rem;
  border-top: 1px solid var(--border);
}
.mini-chart-canvas { max-height: 160px; }

/* ── Source Grid (old dashboard cards, kept for reference) ─────────────── */
.source-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 1rem;
}
.source-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.125rem;
  display: flex;
  flex-direction: column;
  gap: .75rem;
}
.source-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .5rem;
}
.source-card-header h2 { font-size: 1rem; font-weight: 600; }
.source-card-header h2 a { color: var(--text); }
.source-card-header h2 a:hover { color: var(--primary); text-decoration: none; }
.source-card-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: auto;
  padding-top: .5rem;
  border-top: 1px solid var(--border);
  font-size: .8125rem;
}
.no-data { color: var(--text-muted); font-size: .875rem; }

/* ── Stat Row ──────────────────────────────────────────────────────────── */
.stat-row { display: flex; gap: 1.25rem; }
.stat-item { min-width: 0; }
.stat-value { font-size: 1.375rem; font-weight: 700; line-height: 1.2; }
.stat-label { font-size: .75rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: .04em; }

/* ── Severity Bar ─────────────────────────────────────────────────────── */
.severity-bar-wrap { margin-top: .25rem; }
.severity-bar {
  display: flex;
  height: 6px;
  border-radius: 3px;
  overflow: hidden;
  background: var(--surface-2);
  gap: 1px;
}
.sev-segment { height: 100%; }
.severity-legend { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: .375rem; font-size: .75rem; }
.sev-label { }

/* ── Stats Section ────────────────────────────────────────────────────── */
.stats-section { margin-bottom: 2rem; }
.stats-section h2,
.charts-section h2,
.events-section h2,
.fetch-log-section h2 {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: .875rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .06em;
  font-size: .75rem;
}
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 1rem;
}
.stats-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
}
.stats-card-title {
  font-size: .75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-muted);
  margin-bottom: .625rem;
}
.dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 5px;
  vertical-align: middle;
}

/* ── Charts Section ───────────────────────────────────────────────────── */
.charts-section { margin-bottom: 2rem; }
.charts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(480px, 1fr));
  gap: 1rem;
}
.chart-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.125rem;
}
.chart-title {
  font-size: .75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-muted);
  margin-bottom: .75rem;
}
.chart-wrap { position: relative; }
.chart-canvas { max-height: 280px; }

/* ── Timing / Sparkline ───────────────────────────────────────────────── */
.timing-value { font-variant-numeric: tabular-nums; font-weight: 600; }
.text-warning { color: var(--warning); }
.timing-sparkline {
  display: inline-flex;
  align-items: flex-end;
  gap: 2px;
  height: 28px;
  margin-top: 4px;
  vertical-align: bottom;
}
.spark-bar {
  display: inline-block;
  width: 6px;
  border-radius: 2px 2px 0 0;
  cursor: default;
}

/* ── Event Tooltip ────────────────────────────────────────────────────── */
.event-tooltip {
  position: fixed;
  z-index: 9999;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .5rem .75rem;
  font-size: .8125rem;
  max-width: 280px;
  box-shadow: var(--shadow);
  pointer-events: none;
  line-height: 1.4;
}
.event-tooltip strong { color: var(--text); display: block; }
.event-tooltip span { color: var(--text-muted); }

/* ── Events & Fetch Log ───────────────────────────────────────────────── */
.events-section,
.fetch-log-section { margin-bottom: 2rem; }

/* ── Tables ───────────────────────────────────────────────────────────── */
.data-table { width: 100%; border-collapse: collapse; font-size: .875rem; }
.data-table th {
  text-align: left;
  padding: .5rem .75rem;
  border-bottom: 1px solid var(--border);
  color: var(--text-muted);
  font-weight: 500;
  font-size: .8125rem;
}
.data-table td { padding: .625rem .75rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
.data-table tr:last-child td { border-bottom: none; }
.data-table tr.row-inactive td { opacity: .5; }
.text-right { text-align: right; }
.stats-table .text-right { padding-right: .25rem; }
.url-cell { max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ── Forms ────────────────────────────────────────────────────────────── */
.form-group { margin-bottom: 1rem; }
.form-group label { display: block; margin-bottom: .375rem; font-size: .875rem; font-weight: 500; }
.form-group-full { grid-column: 1 / -1; }
.form-control {
  display: block;
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: .4375rem .625rem;
  font-size: .875rem;
  font-family: inherit;
  transition: border-color .15s;
}
.form-control:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(88,166,255,.15);
}
select.form-control { cursor: pointer; }
textarea.form-control { resize: vertical; min-height: 80px; }
.checkbox-label { display: flex; align-items: center; gap: .5rem; cursor: pointer; }
.checkbox-label input[type="checkbox"] { width: auto; }
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 0 1rem;
}
.form-row { display: flex; flex-wrap: wrap; gap: .75rem; align-items: flex-end; }
.form-row .form-group { margin-bottom: 0; }
.form-actions { display: flex; gap: .5rem; margin-top: .5rem; }

/* ── Buttons ──────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .375rem;
  padding: .4375rem .875rem;
  border-radius: var(--radius);
  font-size: .875rem;
  font-weight: 500;
  cursor: pointer;
  border: 1px solid transparent;
  text-decoration: none;
  white-space: nowrap;
  transition: background .15s, border-color .15s, color .15s;
}
.btn:hover { text-decoration: none; }
.btn-primary { background: var(--primary-dim); color: #fff; border-color: var(--primary-dim); }
.btn-primary:hover { background: var(--primary); border-color: var(--primary); }
.btn-secondary { background: var(--surface-2); color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: var(--border); }
.btn-ghost { background: transparent; color: var(--text-muted); border-color: var(--border); }
.btn-ghost:hover { color: var(--text); background: var(--surface-2); }
.btn-danger { background: transparent; color: var(--danger); border-color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: #fff; }
.btn-sm { padding: .25rem .5rem; font-size: .8125rem; }
.btn-block { width: 100%; }

/* ── Badges ───────────────────────────────────────────────────────────── */
.badge {
  display: inline-flex;
  align-items: center;
  padding: .125rem .5rem;
  border-radius: 20px;
  font-size: .75rem;
  font-weight: 500;
}
.badge-success { background: rgba(63,185,80,.15); color: var(--success); }
.badge-danger { background: rgba(248,81,73,.15); color: var(--danger); }
.badge-warning { background: rgba(210,153,34,.15); color: var(--warning); }
.badge-muted { background: var(--surface-2); color: var(--text-muted); }

/* ── Alerts ───────────────────────────────────────────────────────────── */
.alert {
  padding: .625rem 1rem;
  border-radius: var(--radius);
  margin-bottom: 1rem;
  font-size: .875rem;
}
.alert.inline { display: inline-block; margin: 0; padding: .25rem .75rem; }
.alert-success { background: rgba(63,185,80,.1); border: 1px solid rgba(63,185,80,.3); color: var(--success); }
.alert-danger { background: rgba(248,81,73,.1); border: 1px solid rgba(248,81,73,.3); color: var(--danger); }

/* ── Utility ──────────────────────────────────────────────────────────── */
.text-muted { color: var(--text-muted); }
.text-success { color: var(--success); }
.text-danger { color: var(--danger); }
.text-sm { font-size: .8125rem; }
.empty-state { color: var(--text-muted); padding: 2rem; text-align: center; font-size: .9375rem; }
.actions { white-space: nowrap; }
.fetch-status { font-size: .8125rem; }

/* ── Layout helpers ───────────────────────────────────────────────────── */
.layout-two-col {
  display: grid;
  grid-template-columns: 380px 1fr;
  gap: 1.25rem;
  align-items: start;
}

/* ── Modal ────────────────────────────────────────────────────────────── */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}
.modal-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  min-width: 340px;
  max-width: 480px;
  width: 90%;
}
.modal-box h3 { font-size: 1rem; margin-bottom: 1rem; }
.modal-actions { display: flex; gap: .5rem; margin-top: 1rem; }

/* ── Login Page ───────────────────────────────────────────────────────── */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: var(--bg);
}
.login-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem;
  width: 100%;
  max-width: 360px;
}
.login-logo { text-align: center; margin-bottom: 1.5rem; }
.login-logo .logo-icon { font-size: 2rem; display: block; margin-bottom: .5rem; }
.login-logo h1 { font-size: 1.25rem; margin-bottom: .25rem; }
.login-logo p { color: var(--text-muted); font-size: .875rem; }

/* ── Responsive ───────────────────────────────────────────────────────── */
@media (max-width: 768px) {
  .charts-grid { grid-template-columns: 1fr; }
  .layout-two-col { grid-template-columns: 1fr; }
  .source-grid { grid-template-columns: 1fr; }
  .agg-grid { grid-template-columns: 1fr; }
  .page-header { flex-direction: column; }
  .source-section-header { flex-direction: column; align-items: flex-start; }
  .navbar { flex-wrap: wrap; gap: .5rem; }
  .navbar-links { flex-wrap: wrap; }
}
