:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f3f2ee;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --wash: rgba(11, 11, 11, 0.035);
  --focus: #2a78d6;
  /* categorical, fixed order */
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --zone-1: #1baf7a;
  --zone-2: #4a3aa7;
  --zone-1-ink: #0b0b0b;
  --zone-2-ink: #ffffff;
  /* status (severity) — always paired with an icon and a label */
  --sev-0: #0ca30c;
  --sev-1: #fab219;
  --sev-2: #ec835a;
  --sev-3: #d03b3b;
  --sev-0-wash: rgba(12, 163, 12, 0.1);
  --sev-1-wash: rgba(250, 178, 25, 0.16);
  --sev-2-wash: rgba(236, 131, 90, 0.16);
  --sev-3-wash: rgba(208, 59, 59, 0.12);
  --critical: #d03b3b;
  --critical-text: #b42f2f;
  --good-text: #006300;
  /* sequential blue */
  --seq-0: #f0efec;
  --seq-1: #cde2fb;
  --seq-2: #9ec5f4;
  --seq-3: #6da7ec;
  --seq-4: #3987e5;
  --seq-5: #256abf;
  --seq-6: #184f95;
  --seq-7: #0d366b;
  --seq-ink-lo: #0b0b0b;
  --seq-ink-hi: #ffffff;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232321;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.1);
    --wash: rgba(255, 255, 255, 0.04);
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --zone-1: #199e70;
    --zone-2: #9085e9;
    --zone-2-ink: #0b0b0b;
    --critical: #e66767;
    --critical-text: #f08a8a;
    --good-text: #0ca30c;
    --sev-0-wash: rgba(12, 163, 12, 0.16);
    --sev-1-wash: rgba(250, 178, 25, 0.16);
    --sev-2-wash: rgba(236, 131, 90, 0.18);
    --sev-3-wash: rgba(208, 59, 59, 0.22);
    --seq-0: #2a2a28;
    --seq-1: #0d366b;
    --seq-2: #104281;
    --seq-3: #184f95;
    --seq-4: #256abf;
    --seq-5: #3987e5;
    --seq-6: #6da7ec;
    --seq-7: #b7d3f6;
    --seq-ink-lo: #ffffff;
    --seq-ink-hi: #0b0b0b;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232321;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.1);
  --wash: rgba(255, 255, 255, 0.04);
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --zone-1: #199e70;
  --zone-2: #9085e9;
  --zone-2-ink: #0b0b0b;
  --critical: #e66767;
  --critical-text: #f08a8a;
  --good-text: #0ca30c;
  --sev-0-wash: rgba(12, 163, 12, 0.16);
  --sev-1-wash: rgba(250, 178, 25, 0.16);
  --sev-2-wash: rgba(236, 131, 90, 0.18);
  --sev-3-wash: rgba(208, 59, 59, 0.22);
  --seq-0: #2a2a28;
  --seq-1: #0d366b;
  --seq-2: #104281;
  --seq-3: #184f95;
  --seq-4: #256abf;
  --seq-5: #3987e5;
  --seq-6: #6da7ec;
  --seq-7: #b7d3f6;
  --seq-ink-lo: #ffffff;
  --seq-ink-hi: #0b0b0b;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--page);
  color: var(--text-primary);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
}
h1, h2, h3 { margin: 0; font-weight: 600; }
h1 { font-size: 24px; line-height: 1.25; }
h2 { font-size: 16px; }
h3 { font-size: 14px; }
p { margin: 0; }
a { color: inherit; }
button, select, input { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
[hidden] { display: none !important; }

/* ---------- chrome ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap;
  padding: 16px 24px; max-width: 1280px; margin: 0 auto;
}
.brand { display: flex; flex-direction: column; text-decoration: none; }
.brand-title { font-size: 17px; font-weight: 600; }
.brand-sub { color: var(--text-secondary); font-size: 13px; }
.topbar-controls { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.week-picker { display: inline-flex; align-items: center; gap: 8px; color: var(--text-secondary); font-size: 13px; }
select {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 6px 10px; min-height: 34px; color: var(--text-primary);
}
.ghost, .btn {
  background: transparent; border: 1px solid var(--border); border-radius: 8px;
  padding: 6px 12px; min-height: 34px; cursor: pointer; color: var(--text-primary); text-decoration: none;
  display: inline-flex; align-items: center; gap: 6px;
}
.ghost:hover, .btn:hover { background: var(--wash); }
.btn.primary { background: var(--text-primary); color: var(--page); border-color: transparent; font-weight: 600; }
.btn.primary:hover { opacity: 0.88; }
.btn[disabled] { opacity: 0.4; cursor: default; }
.live { color: var(--text-secondary); font-size: 12px; display: inline-flex; align-items: center; gap: 6px; }
.live .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--sev-0); flex: none; }
.live .dot.stale { background: var(--text-muted); }

.crumbs { max-width: 1280px; margin: 0 auto; padding: 0 24px 8px; font-size: 13px; color: var(--text-secondary); display: flex; gap: 6px; flex-wrap: wrap; }
.crumbs a { text-decoration: none; color: var(--text-secondary); }
.crumbs a:hover { color: var(--text-primary); text-decoration: underline; }
.crumbs .sep { color: var(--text-muted); }
.crumbs [aria-current] { color: var(--text-primary); font-weight: 600; }

main { max-width: 1280px; margin: 0 auto; padding: 8px 24px 64px; display: flex; flex-direction: column; gap: 20px; outline: none; }
main.loading { opacity: 0.55; transition: opacity 0.2s; }

/* ---------- building blocks ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 20px; min-width: 0; }
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.hint { color: var(--text-secondary); font-size: 13px; margin-top: 4px; max-width: 80ch; }
.muted { color: var(--text-muted); }
.two-col { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 460px), 1fr)); gap: 20px; }
.scroll-x { overflow-x: auto; }
.empty { color: var(--text-secondary); padding: 24px 0; }
.error { color: var(--critical-text); padding: 24px 0; }

.hero { display: flex; flex-direction: column; gap: 10px; padding: 8px 0 4px; }
.hero .eyebrow { color: var(--text-secondary); font-size: 13px; }
.hero .lede { font-size: 16px; color: var(--text-secondary); max-width: 90ch; }
.hero .lede strong { color: var(--text-primary); font-weight: 600; }
.title-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }

/* severity badge: icon + label, tinted background */
.sev { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px 3px 8px; border-radius: 999px; font-size: 13px; font-weight: 600; white-space: nowrap; color: var(--text-primary); }
.sev svg { flex: none; }
.sev-0 { background: var(--sev-0-wash); }
.sev-1 { background: var(--sev-1-wash); }
.sev-2 { background: var(--sev-2-wash); }
.sev-3 { background: var(--sev-3-wash); }
.sev.lg { font-size: 15px; padding: 5px 14px 5px 10px; }
.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; background: var(--surface-2); color: var(--text-secondary); border: 1px solid var(--border); vertical-align: middle; }

/* severity distribution strip */
.sev-strip { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
@media (max-width: 720px) { .sev-strip { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.sev-tile { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; display: flex; flex-direction: column; gap: 4px; border-top-width: 4px; }
.sev-tile .count { font-size: 32px; font-weight: 600; line-height: 1.1; }
.sev-tile .what { color: var(--text-secondary); font-size: 13px; }
.sev-tile.is-zero .count { color: var(--text-muted); }

/* KPI tiles */
.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
@media (max-width: 1000px) { .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 520px) { .kpis { grid-template-columns: 1fr; } }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 16px; min-width: 0; }
.kpi .label { color: var(--text-secondary); font-size: 13px; }
.kpi .value { font-size: 28px; font-weight: 600; margin-top: 4px; line-height: 1.15; }
.kpi .detail { color: var(--text-secondary); font-size: 13px; margin-top: 6px; }
.delta { font-size: 13px; font-weight: 600; margin-left: 6px; }
.delta.up { color: var(--critical-text); }
.delta.down { color: var(--good-text); }

/* ranking table */
table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--grid); vertical-align: middle; }
th { font-weight: 600; color: var(--text-secondary); font-size: 12px; white-space: nowrap; }
td.num, th.num { text-align: right; }
tbody tr:last-child td { border-bottom: 0; }
.rank tbody tr { cursor: pointer; }
.rank tbody tr:hover td { background: var(--wash); }
.rank .rank-n { color: var(--text-muted); width: 28px; }
.rank .st-name { font-weight: 600; white-space: nowrap; }
.rank .st-sub { color: var(--text-secondary); font-size: 12px; white-space: nowrap; }
.rank .big { font-size: 18px; font-weight: 600; white-space: nowrap; }
.rank .bar-cell { min-width: 150px; }
.meter { height: 6px; border-radius: 3px; background: var(--seq-1); margin-top: 6px; position: relative; overflow: hidden; }
.meter > span { position: absolute; inset: 0 auto 0 0; background: var(--series-1); border-radius: 3px; }
.small { font-size: 12px; color: var(--text-secondary); }
.nowrap { white-space: nowrap; }
.check-cell { width: 36px; }
.check-cell input { width: 16px; height: 16px; cursor: pointer; }
.toolbar { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }

/* day matrix: text color per sequential step so labels clear contrast on every fill */
.matrix a.s0, .matrix a.s1, .matrix a.s2, .matrix a.s3 { color: var(--seq-ink-lo); }
.matrix a.s4, .matrix a.s5, .matrix a.s6, .matrix a.s7 { color: var(--seq-ink-hi); }
.matrix a.s0 { color: var(--text-muted); font-weight: 400; }
.matrix a.s4 { color: #ffffff; } /* mid step: white clears contrast in both themes */
.matrix td.cell { text-align: center; padding: 0; border-bottom: 2px solid var(--surface); border-left: 2px solid var(--surface); }
.matrix td.cell a { display: block; padding: 12px 6px; text-decoration: none; font-weight: 600; font-size: 13px; }
.matrix td.cell a:hover { outline: 2px solid var(--text-primary); outline-offset: -2px; border-radius: 4px; }
.matrix th { text-align: center; }
.matrix th:first-child { text-align: left; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; margin-top: 12px; font-size: 12px; color: var(--text-secondary); align-items: center; }
.legend .key { display: inline-flex; align-items: center; gap: 6px; }
.legend .swatch { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }
.legend .line { width: 16px; height: 0; border-top: 2px solid; display: inline-block; }
.legend .ring { width: 12px; height: 12px; border-radius: 3px; border: 2px solid var(--critical); display: inline-block; }
.legend .ramp { display: inline-flex; border-radius: 3px; overflow: hidden; }
.legend .ramp span { width: 18px; height: 10px; }

/* drivers ("why this rating") */
.drivers { display: grid; }
.driver { display: grid; grid-template-columns: minmax(0, 1fr) auto auto; gap: 12px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--grid); }
.driver:last-child { border-bottom: 0; }
.driver .v { font-weight: 600; font-variant-numeric: tabular-nums; }

details.footnote { color: var(--text-secondary); font-size: 13px; }
details.footnote summary { cursor: pointer; }
details.footnote ul { margin: 8px 0 0; padding-left: 18px; }

/* segmented control */
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; flex-wrap: wrap; }
.seg button, .seg a { background: transparent; border: 0; padding: 6px 12px; cursor: pointer; min-height: 32px; color: var(--text-secondary); text-decoration: none; display: inline-flex; align-items: center; }
.seg > * + * { border-left: 1px solid var(--border) !important; }
.seg [aria-pressed="true"], .seg [aria-current="page"] { background: var(--surface-2); color: var(--text-primary); font-weight: 600; }
.seg button:hover, .seg a:hover { background: var(--wash); }

.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip { display: inline-flex; align-items: center; gap: 8px; border: 1px solid var(--border); border-radius: 999px; padding: 4px 12px 4px 8px; cursor: pointer; background: var(--surface); }
.chip input { margin: 0; }
.chip i { width: 14px; border-top: 2px solid; display: inline-block; }

/* charts */
.chart { position: relative; width: 100%; }
.chart svg { display: block; overflow: visible; }
.chart text { fill: var(--text-muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart text.ink { fill: var(--text-secondary); }
.chart text.strong { fill: var(--text-primary); font-weight: 600; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .baseline, .spark .baseline { stroke: var(--axis); stroke-width: 1; }
.chart .threshold { stroke: var(--critical); stroke-width: 1.5; }
.chart .threshold-label { fill: var(--critical-text); font-weight: 600; }
.chart .crosshair { stroke: var(--text-muted); stroke-width: 1; }
.chart .band { fill: var(--wash); }
.chart .hit { fill: transparent; cursor: crosshair; }
.chart .cell:hover, .chart .bar:hover { opacity: 0.8; }
.spark { display: block; }
.key-line { width: 14px; border-top: 2px solid; display: inline-block; vertical-align: middle; margin-right: 6px; }
.inline-bar { display: inline-block; height: 8px; border-radius: 0 4px 4px 0; background: var(--series-1); vertical-align: middle; margin-right: 8px; }
.notes { margin-top: 12px; color: var(--text-secondary); font-size: 13px; }

.tooltip {
  position: fixed; z-index: 10; pointer-events: none;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12); padding: 8px 10px; font-size: 12px; min-width: 140px;
}
.tooltip .t-title { color: var(--text-secondary); margin-bottom: 4px; }
.tooltip .t-row { display: flex; align-items: center; gap: 8px; justify-content: space-between; }
.tooltip .t-row .k { display: inline-flex; align-items: center; gap: 6px; color: var(--text-secondary); }
.tooltip .t-row .k i { width: 12px; border-top: 2px solid; display: inline-block; }
.tooltip .t-row b { font-weight: 600; color: var(--text-primary); font-variant-numeric: tabular-nums; }
.tooltip .t-flag { margin-top: 4px; color: var(--critical-text); font-weight: 600; }

/* camera frames */
.frame-viewer { display: grid; grid-template-columns: minmax(0, 2fr) minmax(220px, 1fr); gap: 20px; align-items: start; }
@media (max-width: 820px) { .frame-viewer { grid-template-columns: 1fr; } }
.frame-main { background: var(--surface-2); border-radius: 8px; overflow: hidden; aspect-ratio: 16 / 9; }
.frame-img { display: block; width: 100%; height: 100%; object-fit: contain; }
.frame-meta { display: flex; flex-direction: column; gap: 10px; align-items: flex-start; }
.frame-meta .drivers { width: 100%; }
.frame-time { font-size: 28px; font-weight: 600; line-height: 1.1; }
.filmstrip { display: flex; gap: 8px; overflow-x: auto; padding: 14px 2px 6px; margin-top: 8px; scroll-snap-type: x proximity; }
.thumb { flex: none; width: 116px; padding: 0; border: 2px solid transparent; border-radius: 8px; background: none; cursor: pointer; scroll-snap-align: center; color: var(--text-secondary); font-size: 11px; text-align: left; }
.thumb img { display: block; width: 100%; aspect-ratio: 16 / 9; object-fit: cover; border-radius: 6px; background: var(--surface-2); }
.thumb span { display: block; padding: 3px 2px 0; font-variant-numeric: tabular-nums; }
.thumb b { color: var(--critical-text); }
.thumb.over { border-color: var(--critical); }
.thumb[aria-selected="true"] { outline: 2px solid var(--text-primary); outline-offset: 2px; color: var(--text-primary); font-weight: 600; }

@media (max-width: 640px) {
  .topbar { padding: 12px 16px; }
  .crumbs { padding: 0 16px 8px; }
  main { padding: 8px 16px 40px; }
  .card { padding: 16px; }
  h1 { font-size: 20px; }
}

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(360px, 100%); display: flex; flex-direction: column; gap: 16px; }
.login-card h1 { margin: 0; }
.login-card label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--text-secondary); }
.login-card input {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 10px; min-height: 38px; color: var(--text-primary); font: inherit;
}
.login-card input:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }
.login-card .btn { justify-content: center; min-height: 40px; }
.login-error { margin: 0; padding: 8px 12px; border-radius: 8px; background: var(--sev-3-wash); color: var(--critical-text); font-size: 13px; }
