/* ------------------------------------------------------------------
   Tokens. Light values here, dark declared under both the OS media
   query and the [data-theme] scope so the toggle wins either way.
   Palette slots were validated with the dataviz validator:
     bristol 5-class diverging  -> CVD dE 11.9 light / 8.4 dark, normal 20.2 / 16.0
     count ordinal ramp         -> monotone L, light end clears 2:1
     treatment pair             -> all checks pass both modes
   ------------------------------------------------------------------ */
:root {
  color-scheme: light;

  --surface-1: #fcfcfb;
  --page: #f9f9f7;
  --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.04);

  /* Bristol: diverging around T4. cool = too hard, warm = too loose. */
  --b-hard: #104281;
  --b-firm: #3987e5;
  --b-ideal: #a8a69c;
  --b-soft: #e34948;
  --b-loose: #7d1414;

  /* sequential / ordinal ramp for counts (one hue, light -> dark) */
  --seq-1: #86b6ef;
  --seq-2: #5598e7;
  --seq-3: #2a78d6;
  --seq-4: #1c5cab;
  --seq-5: #0d366b;
  --seq-0: #eceae4; /* empty cell: neutral, not a low value on the ramp */

  /* treatment entities */
  --ent: #4a3aa7;
  --ust: #1baf7a;

  --good: #006300;
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme='light'])) {
    color-scheme: dark;
    --surface-1: #1a1a19;
    --page: #0d0d0d;
    --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.05);

    --b-hard: #9ec5f4;
    --b-firm: #3987e5;
    --b-ideal: #55544e;
    --b-soft: #d84b4a;
    --b-loose: #f6a3a2;

    --seq-1: #184f95;
    --seq-2: #256abf;
    --seq-3: #3987e5;
    --seq-4: #6da7ec;
    --seq-5: #b7d3f6;
    --seq-0: #26261f;

    --ent: #9085e9;
    --ust: #199e70;
    --good: #0ca30c;
  }
}

:root[data-theme='dark'] {
  color-scheme: dark;
  --surface-1: #1a1a19;
  --page: #0d0d0d;
  --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.05);

  --b-hard: #9ec5f4;
  --b-firm: #3987e5;
  --b-ideal: #55544e;
  --b-soft: #d84b4a;
  --b-loose: #f6a3a2;

  --seq-1: #184f95;
  --seq-2: #256abf;
  --seq-3: #3987e5;
  --seq-4: #6da7ec;
  --seq-5: #b7d3f6;
  --seq-0: #26261f;

  --ent: #9085e9;
  --ust: #199e70;
  --good: #0ca30c;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--page);
  color: var(--text-primary);
  font: 14px/1.5 system-ui, -apple-system, 'Segoe UI', sans-serif;
  -webkit-font-smoothing: antialiased;
}

.wrap { max-width: 1180px; margin: 0 auto; padding: 24px 20px 80px; }

/* ---------- header + the single filter row ---------- */

header.top { padding: 8px 0 4px; display: flex; align-items: flex-start; gap: 16px; }
header.top .titles { flex: 1 1 auto; min-width: 0; }
header.top .theme { flex: 0 0 auto; }
header.top h1 { font-size: 20px; font-weight: 650; margin: 0 0 2px; letter-spacing: -0.01em; }
header.top p { margin: 0; color: var(--text-secondary); font-size: 13px; }

.filters {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 20px;
  margin: 16px 0 20px;
  padding: 10px 14px;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.filters .group { display: flex; align-items: center; gap: 6px; }
.filters .glabel {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-right: 2px;
}
.filters .spacer { flex: 1 1 auto; }

.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 7px; overflow: hidden; }
.seg button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-secondary);
  font: inherit;
  font-size: 13px;
  padding: 4px 10px;
  cursor: pointer;
}
.seg button + button { border-left: 1px solid var(--border); }
.seg button:hover { background: var(--wash); }
.seg button[aria-pressed='true'] { background: var(--text-primary); color: var(--surface-1); font-weight: 550; }

.check { display: inline-flex; align-items: center; gap: 6px; color: var(--text-secondary); cursor: pointer; font-size: 13px; }

/* ---------- nav ---------- */

nav.tabs { display: flex; flex-wrap: wrap; gap: 2px; margin-bottom: 18px; border-bottom: 1px solid var(--border); }
nav.tabs button {
  appearance: none;
  border: 0;
  border-bottom: 2px solid transparent;
  background: transparent;
  color: var(--text-secondary);
  font: inherit;
  padding: 8px 14px;
  cursor: pointer;
  margin-bottom: -1px;
}
nav.tabs button:hover { color: var(--text-primary); }
nav.tabs button[aria-selected='true'] { color: var(--text-primary); font-weight: 600; border-bottom-color: var(--text-primary); }

/* ---------- cards ---------- */

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px 14px;
  margin-bottom: 18px;
}
.card > header { display: flex; align-items: flex-start; gap: 16px; margin-bottom: 4px; }
.card h2 { font-size: 15px; font-weight: 600; margin: 0; letter-spacing: -0.005em; }
.card .sub { color: var(--text-secondary); font-size: 12.5px; margin: 3px 0 0; max-width: 74ch; }
.card .tv { margin-left: auto; flex: 0 0 auto; }
.card .plot { margin-top: 12px; overflow-x: auto; }
/* Charts that size themselves to the container get max-width as a safety net.
   Charts that are intentionally wider than the screen (the calendar) opt out
   with .scroll — otherwise this rule silently shrinks them instead of
   scrolling, and the cells stop being tappable. */
.card .plot svg { display: block; max-width: 100%; height: auto; }

/* Inner horizontal scroller for charts that are wider than the screen by
   design. It wraps only the SVG — legends and notes stay outside it, or they
   scroll off with the chart. */
.hscroll {
  overflow-x: auto;
  overscroll-behavior-x: contain;
  -webkit-overflow-scrolling: touch;
}
/* Must outrank `.card .plot svg` above, hence the full path. */
.card .plot .hscroll svg { max-width: none; }

.note { color: var(--text-muted); font-size: 12px; margin: 8px 0 0; }

code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.92em;
  background: var(--wash);
  padding: 0.5px 4px;
  border-radius: 4px;
}

/* ---------- legend ---------- */

.legend { display: flex; flex-wrap: wrap; gap: 4px 16px; margin: 10px 0 0; padding: 0; list-style: none; }
.legend li { display: flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--text-secondary); }
.legend .sw { width: 11px; height: 11px; border-radius: 3px; flex: 0 0 auto; }
.legend .sw.line { height: 3px; border-radius: 2px; width: 14px; }
.legend .ramp { display: inline-flex; gap: 2px; }
.legend .ramp span { width: 15px; height: 11px; border-radius: 2px; }

/* ---------- stat tiles ---------- */

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 16px 24px; margin-top: 6px; }
.tile .label { color: var(--text-secondary); font-size: 12.5px; }
.tile .value { font-size: 26px; font-weight: 620; letter-spacing: -0.02em; line-height: 1.25; }
.tile .delta { font-size: 12px; color: var(--text-secondary); }
.tile .delta.good { color: var(--good); }
.hero { border-bottom: 1px solid var(--border); padding-bottom: 14px; margin-bottom: 14px; }
.hero .label { color: var(--text-secondary); font-size: 13px; }
.hero .value { font-size: 52px; font-weight: 620; letter-spacing: -0.03em; line-height: 1.05; }
.hero .value small { font-size: 18px; font-weight: 500; color: var(--text-secondary); letter-spacing: 0; }

/* ---------- tables (the table-view twin of every chart) ---------- */

.tablewrap { max-height: 420px; overflow: auto; margin-top: 12px; border: 1px solid var(--border); border-radius: 8px; }
table.dv { border-collapse: collapse; width: 100%; font-size: 12.5px; font-variant-numeric: tabular-nums; }
table.dv th, table.dv td { padding: 5px 10px; text-align: right; white-space: nowrap; }
table.dv th:first-child, table.dv td:first-child { text-align: left; }
table.dv thead th {
  position: sticky;
  top: 0;
  background: var(--surface-1);
  border-bottom: 1px solid var(--axis);
  color: var(--text-secondary);
  font-weight: 600;
  text-align: right;
}
table.dv thead th:first-child { text-align: left; }
table.dv tbody tr + tr td { border-top: 1px solid var(--grid); }
table.dv td.k { color: var(--text-secondary); }
table.dv .chip { display: inline-block; width: 9px; height: 9px; border-radius: 2px; margin-right: 6px; vertical-align: -1px; }

/* ---------- tooltip ---------- */

#tip {
  position: fixed;
  z-index: 100;
  pointer-events: none;
  opacity: 0;
  transition: opacity 90ms;
  background: var(--surface-1);
  border: 1px solid var(--axis);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 12.5px;
  line-height: 1.45;
  color: var(--text-primary);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.18);
  max-width: 260px;
}
#tip.on { opacity: 1; }
#tip .t { font-weight: 600; margin-bottom: 3px; }
#tip .r { color: var(--text-secondary); }
#tip .r b { color: var(--text-primary); font-weight: 550; font-variant-numeric: tabular-nums; }
#tip .sw { display: inline-block; width: 9px; height: 9px; border-radius: 2px; margin-right: 5px; vertical-align: -1px; }

/* ---------- svg chrome ---------- */

.axis path.domain { stroke: var(--axis); }
.axis line { stroke: var(--axis); }
.axis text { fill: var(--text-muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.grid line { stroke: var(--grid); shape-rendering: crispEdges; }
.grid path { display: none; }
.alab { fill: var(--text-secondary); font-size: 11.5px; }
.dlab { fill: var(--text-primary); font-size: 11.5px; font-weight: 600; }
.hit { fill: transparent; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

/* ------------------------------------------------------------------
   Phone layout. Breakpoint matches PC.MOBILE_MAX in js/core.js.
   ------------------------------------------------------------------ */
@media (max-width: 700px) {
  .wrap { padding: 14px 12px 56px; }

  header.top { gap: 10px; }
  header.top h1 { font-size: 18px; }
  header.top p { font-size: 12.5px; }
  header.top .theme .seg button { padding: 10px 12px; font-size: 13px; min-height: 44px; }

  /* Not sticky on a phone: wrapped over three rows it would eat a third of a
     915px-tall viewport and leave no room for the chart it scopes. */
  .filters {
    position: static;
    gap: 8px 14px;
    padding: 8px 10px;
    margin: 12px 0 14px;
  }
  .filters .group { flex-wrap: wrap; }
  .filters .spacer { display: none; }

  /* 44px minimum touch targets throughout */
  .seg button { padding: 10px 14px; font-size: 14px; min-height: 44px; }
  .check { min-height: 44px; font-size: 14px; }
  .check input { width: 20px; height: 20px; }

  /* One scrolling row rather than wrapping onto two lines */
  nav.tabs {
    flex-wrap: nowrap;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
    margin-bottom: 14px;
  }
  nav.tabs::-webkit-scrollbar { display: none; }
  nav.tabs button { padding: 11px 14px; min-height: 44px; white-space: nowrap; }

  .card { padding: 14px 12px 12px; border-radius: 12px; margin-bottom: 14px; }
  .card > header { flex-direction: column; gap: 10px; }
  .card h2 { font-size: 15px; }
  .card .sub { font-size: 12.5px; }
  .card .tv { margin-left: 0; display: flex; flex-wrap: wrap; gap: 8px; }
  .card .plot { -webkit-overflow-scrolling: touch; overscroll-behavior-x: contain; }

  .hero { padding-bottom: 12px; margin-bottom: 12px; }
  .hero .value { font-size: 42px; }
  .hero .value small { font-size: 15px; display: block; }

  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px 14px; }
  .tile .value { font-size: 22px; }
  .tile .label, .tile .delta { font-size: 12px; }

  .legend { gap: 4px 12px; }
  .legend li { font-size: 12px; }

  .tablewrap { max-height: 360px; }
  table.dv { font-size: 12px; }
  table.dv th, table.dv td { padding: 8px 8px; }

  /* Never wider than the screen, and keep clear of the fingertip. */
  #tip {
    max-width: min(300px, calc(100vw - 20px));
    font-size: 13px;
    padding: 9px 11px;
  }
}
