/* ═══════════════════════════════════════════════════════════════════════════
   SYNC · Panel App Analytics — dark-first, matching the Panel Geography build.

   Two palettes live here, both validated, neither hand-tweakable step by step:

   · categorical (--cat-1…8) — series identity in the trend chart and the
     source split. Validated with the dataviz validator against --surface-1
     (#111b25): lightness band PASS, chroma floor PASS, worst adjacent CVD ΔE
     35.9 deutan / 10.2 tritan, contrast PASS. The tritan figure sits in the
     8–12 floor band, which is legal ONLY alongside a second channel — every
     chart here ships a legend AND direct labels for that reason.
   · sequential (--seq-1…6) — one teal hue, light→dark, for magnitude
     (heatmaps). Monotone OKLCH lightness, ≥0.06 ΔL per step.

   Regenerate a whole ramp if it changes; never nudge one step.
   ═══════════════════════════════════════════════════════════════════════════ */

:root {
  color-scheme: dark;

  --surface-0: #0b1118;
  --surface-1: #111b25;
  --surface-2: #16222e;
  --surface-3: #1b2a38;
  --line:      #22323f;
  --line-soft: #1a2833;

  --ink:   #f2f7f9;
  --ink-2: #a9bcc7;
  --ink-3: #6d8493;

  /* categorical — fixed order, never cycled */
  --cat-1: #2ba3a4;
  --cat-2: #9085e9;
  --cat-3: #c98500;
  --cat-4: #e66767;
  --cat-5: #3987e5;
  --cat-6: #4fa83a;
  --cat-7: #d55181;
  --cat-8: #cf6a28;

  /* sequential teal — low → high */
  --seq-0: #14212c;
  --seq-1: #0e5151;
  --seq-2: #046a6b;
  --seq-3: #098385;
  --seq-4: #259d9e;
  --seq-5: #41b7b8;
  --seq-6: #73d5d6;

  --accent:   #41b7b8;
  --accent-2: #73d5d6;
  --good:     #0ca30c;
  --warning:  #fab219;
  --critical: #d03b3b;

  --r-sm: 8px;
  --r-md: 12px;
  --r-lg: 16px;

  --ease: cubic-bezier(.16, .84, .34, 1);

  --font: ui-sans-serif, -apple-system, "SF Pro Text", Inter, "Segoe UI", Roboto, system-ui, sans-serif;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--surface-0);
  color: var(--ink);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -0.015em; }
a { color: var(--accent-2); }
.muted { color: var(--ink-3); }
.nums { font-variant-numeric: tabular-nums; }

/* ── shell ───────────────────────────────────────────────────────────────── */

.app { min-height: 100%; display: flex; flex-direction: column; }

.topbar {
  position: sticky; top: 0; z-index: 40;
  display: flex; align-items: center; gap: 20px; flex-wrap: wrap;
  padding: 14px 24px;
  background: color-mix(in srgb, var(--surface-0) 88%, transparent);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: baseline; gap: 10px; }
.brand b { font-size: 15px; letter-spacing: .02em; }
.brand span { font-size: 13px; color: var(--ink-3); }

.controls { display: flex; align-items: center; gap: 10px; margin-left: auto; flex-wrap: wrap; }

label.field { display: flex; flex-direction: column; gap: 4px; font-size: 11px; color: var(--ink-3); }
/* every display rule in this file must yield to [hidden] */
[hidden] { display: none !important; }

select, input[type="text"], input[type="date"], input[type="month"], input[type="search"] {
  font: inherit; font-size: 13px;
  color: var(--ink);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  padding: 7px 10px;
  min-width: 0;
}
select:focus-visible, input:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}

button {
  font: inherit; font-size: 13px; color: var(--ink);
  background: var(--surface-2); border: 1px solid var(--line);
  border-radius: var(--r-sm); padding: 7px 12px; cursor: pointer;
  transition: background .15s var(--ease), border-color .15s var(--ease);
}
button:hover { background: var(--surface-3); border-color: #2d4152; }
button.primary { background: var(--accent); border-color: var(--accent); color: #04181a; font-weight: 600; }
button.primary:hover { background: var(--accent-2); }
button.ghost { background: transparent; }

.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--r-sm); overflow: hidden; }
.seg button { border: 0; border-radius: 0; background: transparent; padding: 7px 12px; color: var(--ink-2); }
.seg button[aria-pressed="true"] { background: var(--accent); color: #04181a; font-weight: 600; }

.freshness { display: flex; align-items: center; gap: 7px; font-size: 12px; color: var(--ink-3); }
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--good); box-shadow: 0 0 0 3px color-mix(in srgb, var(--good) 22%, transparent); }
.dot.stale { background: var(--warning); box-shadow: 0 0 0 3px color-mix(in srgb, var(--warning) 22%, transparent); }

/* ── tabs ────────────────────────────────────────────────────────────────── */

.tabs {
  position: sticky; top: 61px; z-index: 30;
  display: flex; gap: 2px; padding: 0 16px;
  background: var(--surface-0);
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
}
.tabs button {
  border: 0; background: transparent; border-radius: 0;
  padding: 11px 14px; color: var(--ink-3); font-size: 13px;
  border-bottom: 2px solid transparent; white-space: nowrap;
}
.tabs button:hover { background: transparent; color: var(--ink-2); }
.tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); font-weight: 600; }

main { padding: 20px 24px 64px; max-width: 1560px; width: 100%; margin: 0 auto; }
.panel[hidden] { display: none; }

/* ── cards ───────────────────────────────────────────────────────────────── */

.card {
  background: var(--surface-1);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  padding: 18px 20px;
}
.card + .card, .grid + .card, .card + .grid { margin-top: 18px; }
.card-head { display: flex; align-items: baseline; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.card-head h2 { font-size: 14px; }
.card-head .sub { font-size: 12px; color: var(--ink-3); }
.card-head .spacer { margin-left: auto; }

.grid { display: grid; gap: 14px; }
.kpis { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.cols-2 { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
@media (max-width: 1100px) { .cols-2 { grid-template-columns: 1fr; } }

.kpi {
  background: var(--surface-1); border: 1px solid var(--line);
  border-radius: var(--r-md); padding: 14px 16px;
}
.kpi .k { font-size: 11px; letter-spacing: .06em; text-transform: uppercase; color: var(--ink-3); }
.kpi .v { font-size: 28px; font-weight: 600; margin-top: 6px; letter-spacing: -0.02em; }
.kpi .d { font-size: 12px; color: var(--ink-3); margin-top: 4px; }

/* ── notes / callouts ────────────────────────────────────────────────────── */

.note {
  display: flex; gap: 10px; align-items: flex-start;
  border-left: 3px solid var(--warning);
  background: color-mix(in srgb, var(--warning) 7%, var(--surface-1));
  border-radius: 0 var(--r-sm) var(--r-sm) 0;
  padding: 11px 14px; font-size: 12.5px; color: var(--ink-2); line-height: 1.55;
}
.note.info { border-left-color: var(--accent); background: color-mix(in srgb, var(--accent) 7%, var(--surface-1)); }
.note b { color: var(--ink); }

/* ── tables ──────────────────────────────────────────────────────────────── */

.tablewrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
thead th {
  position: sticky; top: 0; z-index: 1;
  text-align: right; font-weight: 500; font-size: 11px; letter-spacing: .04em;
  text-transform: uppercase; color: var(--ink-3);
  padding: 8px 10px; background: var(--surface-1);
  border-bottom: 1px solid var(--line); white-space: nowrap;
}
thead th:first-child, tbody td:first-child { text-align: left; }
thead th.sortable { cursor: pointer; user-select: none; }
thead th.sortable:hover { color: var(--ink); }
thead th[aria-sort] { color: var(--ink); }
tbody td { padding: 8px 10px; text-align: right; border-bottom: 1px solid var(--line-soft); font-variant-numeric: tabular-nums; }
tbody tr:hover td { background: var(--surface-2); }
tbody tr.clickable { cursor: pointer; }
tbody td.name { font-variant-numeric: normal; }
tfoot td { padding: 8px 10px; text-align: right; color: var(--ink-3); font-size: 12px; }

/* The matrix is wider than the viewport — pin the panelist id while scrolling. */
#matrix-table th:first-child,
#matrix-table td:first-child {
  position: sticky; left: 0; z-index: 2;
  background: var(--surface-1);
  box-shadow: 1px 0 0 var(--line);
}
#matrix-table thead th:first-child { z-index: 3; }
#matrix-table tbody tr:hover td:first-child { background: var(--surface-2); }
#matrix-picker .chip { border-radius: 999px; }

.badge {
  display: inline-block; font-size: 10.5px; letter-spacing: .04em; text-transform: uppercase;
  padding: 2px 7px; border-radius: 999px; border: 1px solid var(--line); color: var(--ink-3);
  vertical-align: middle;
}
.badge.app { border-color: color-mix(in srgb, var(--cat-1) 55%, transparent); color: var(--cat-1); }
.badge.acr { border-color: color-mix(in srgb, var(--cat-2) 55%, transparent); color: var(--cat-2); }
.badge.both { border-color: color-mix(in srgb, var(--cat-3) 60%, transparent); color: var(--cat-3); }

.chips { display: flex; flex-wrap: wrap; gap: 4px; justify-content: flex-end; }
.chip {
  font-size: 11px; padding: 2px 7px; border-radius: 999px;
  background: var(--surface-3); border: 1px solid var(--line); color: var(--ink-2);
  white-space: nowrap;
}
.chip.on { background: color-mix(in srgb, var(--accent) 20%, var(--surface-3)); border-color: var(--accent); color: var(--ink); }

/* inline proportion bar inside a table cell */
.bar { position: relative; height: 6px; border-radius: 3px; background: var(--surface-3); overflow: hidden; min-width: 60px; }
.bar > i { position: absolute; inset: 0 auto 0 0; border-radius: 3px; background: var(--accent); display: block; }

.applogo { width: 20px; height: 20px; border-radius: 5px; vertical-align: -5px; margin-right: 8px; background: var(--surface-3); object-fit: cover; }

/* ── charts ──────────────────────────────────────────────────────────────── */

.chart { width: 100%; display: block; overflow: visible; }
.chart .grid-line { stroke: var(--line-soft); stroke-width: 1; shape-rendering: crispEdges; }
.chart .axis { stroke: var(--line); stroke-width: 1; }
.chart text { fill: var(--ink-3); font-size: 11px; font-family: var(--font); }
.chart text.value { fill: var(--ink-2); font-variant-numeric: tabular-nums; }
.chart text.series-label { fill: var(--ink); font-size: 11.5px; font-weight: 600; }
.chart .mark-ring { stroke: var(--surface-1); stroke-width: 2; }
.chart .hit { fill: transparent; }

.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; margin-top: 10px; font-size: 12px; color: var(--ink-2); }
.legend .item { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.legend .swatch { width: 10px; height: 10px; border-radius: 3px; }
.legend .item[aria-pressed="false"] { opacity: .38; }

#tooltip {
  position: fixed; z-index: 90; pointer-events: none; opacity: 0;
  transform: translate(-50%, calc(-100% - 12px));
  background: var(--surface-3); border: 1px solid var(--line);
  border-radius: var(--r-sm); padding: 8px 10px; font-size: 12px;
  box-shadow: 0 12px 30px rgba(0, 0, 0, .5); max-width: 300px;
  transition: opacity .12s var(--ease);
}
#tooltip .t-title { font-weight: 600; margin-bottom: 5px; }
#tooltip .t-row { display: flex; align-items: center; gap: 8px; justify-content: space-between; }
#tooltip .t-row .sw { width: 8px; height: 8px; border-radius: 2px; flex: none; }
#tooltip .t-row b { font-variant-numeric: tabular-nums; }

/* heatmap cells */
.hm-cell { stroke: var(--surface-1); stroke-width: 2; rx: 3; }
.hm-cell.hi { stroke: var(--ink); }

/* ── map ─────────────────────────────────────────────────────────────────── */

.map-layout { display: grid; grid-template-columns: minmax(0, 1fr) 330px; gap: 20px; align-items: start; }
@media (max-width: 1080px) { .map-layout { grid-template-columns: 1fr; } }

/* width/height come from the attributes so the JS height cap holds; max-width
   keeps it from overflowing a narrow column. Do NOT set width:100% here. */
#map-canvas { position: relative; }
#map-canvas svg { display: block; max-width: 100%; height: auto; margin: 0 auto; }

/* Ambient wash behind the landmass — gives the glow something to sit on. */
#map-canvas::before {
  content: ''; position: absolute; inset: 8% 12%; z-index: 0; pointer-events: none;
  background: radial-gradient(ellipse at 50% 45%,
    color-mix(in srgb, var(--accent) 20%, transparent) 0%,
    transparent 68%);
  filter: blur(46px); opacity: .75;
}
#map-canvas svg { position: relative; z-index: 1; }

/* stroke-width and font-size are user units in SVG and would balloon as the
   viewBox shrinks on drill-down — pin the strokes, scale type in JS. */
#map-canvas .geo { vector-effect: non-scaling-stroke; transition: filter .18s var(--ease); }
#map-canvas .geo.hit { cursor: pointer; }
#map-canvas .geo.hit:hover {
  filter: brightness(1.32) drop-shadow(0 0 6px color-mix(in srgb, var(--accent-2) 85%, transparent));
}
#map-canvas .bubble { pointer-events: none; }
#map-canvas .sel { stroke: var(--ink); stroke-width: 2; }

/* The halo: the landmass FILLED and blurred, sitting under the real shapes.
   Never stroke these paths — they contain every district seam (see app.js). */
#map-canvas .coast-glow {
  fill: var(--accent-2); stroke: none; opacity: .45;
  filter: url(#mapGlow); pointer-events: none;
}
#map-canvas .label-halo {
  paint-order: stroke; stroke: var(--surface-0); stroke-width: 3px;
  stroke-linejoin: round; pointer-events: none;
}
@media (prefers-reduced-motion: no-preference) {
  #map-canvas .coast-glow { animation: coastPulse 5.5s var(--ease) infinite; }
}
@keyframes coastPulse {
  0%, 100% { opacity: .34; }
  50%      { opacity: .62; }
}

#map-rail { font-size: 13px; }
#map-rail h3 { font-size: 12px; letter-spacing: .06em; text-transform: uppercase; color: var(--ink-3); margin: 0 0 10px; }
#map-rail .rail-row {
  display: grid; grid-template-columns: 1fr auto; gap: 4px 10px;
  padding: 7px 8px; border-radius: var(--r-sm); cursor: pointer; align-items: center;
}
#map-rail .rail-row:hover { background: var(--surface-2); }
#map-rail .rail-row.on { background: var(--surface-3); }
#map-rail .rail-row .nm { color: var(--ink-2); }
#map-rail .rail-row .vl { font-variant-numeric: tabular-nums; color: var(--ink); }
#map-rail .rail-row .bar { grid-column: 1 / -1; }
#map-rail .section + .section { margin-top: 22px; }
#map-rail .scroll { max-height: 320px; overflow-y: auto; margin: 0 -8px; padding: 0 8px; }

.legend-ramp { display: flex; align-items: center; gap: 8px; margin-top: 12px; font-size: 11px; color: var(--ink-3); }
.legend-ramp i { display: block; width: 26px; height: 10px; border-radius: 2px; }

/* ── panelist drawer ─────────────────────────────────────────────────────── */

.drawer-scrim {
  position: fixed; inset: 0; z-index: 60; background: rgba(4, 9, 14, .6);
  backdrop-filter: blur(2px); opacity: 0; transition: opacity .2s var(--ease);
}
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 61;
  width: min(680px, 96vw); background: var(--surface-1);
  border-left: 1px solid var(--line); box-shadow: -24px 0 60px rgba(0, 0, 0, .5);
  transform: translateX(100%); transition: transform .28s var(--ease);
  display: flex; flex-direction: column;
}
.drawer[data-open="true"] { transform: none; }
.drawer-scrim[data-open="true"] { opacity: 1; }
.drawer-head { padding: 18px 22px; border-bottom: 1px solid var(--line); display: flex; align-items: flex-start; gap: 14px; }
.drawer-head h2 { font-size: 18px; font-family: var(--mono); }
.drawer-body { padding: 18px 22px 40px; overflow-y: auto; }

/* ── states ──────────────────────────────────────────────────────────────── */

.state { padding: 44px 20px; text-align: center; color: var(--ink-3); font-size: 13px; }
.spinner {
  width: 22px; height: 22px; margin: 0 auto 12px; border-radius: 50%;
  border: 2px solid var(--line); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.state.error { color: #f4b4b4; }

.skeleton { background: linear-gradient(90deg, var(--surface-2), var(--surface-3), var(--surface-2)); background-size: 200% 100%; animation: shimmer 1.4s linear infinite; border-radius: 4px; height: 12px; }
@keyframes shimmer { to { background-position: -200% 0; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

/* ── print (month-wise report hand-off) ──────────────────────────────────── */

@media print {
  body { background: #fff; color: #000; }
  .topbar, .tabs, .no-print { display: none !important; }
  .card { border-color: #ccc; background: #fff; }
  main { max-width: none; padding: 0; }
  thead th, tbody td { color: #000; background: #fff; }
}
