.viz-root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface-1: #fcfcfb;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --neutral: #f0efec;
  --ink-dark: #0b0b0b;
  --ink-light: #ffffff;
  /* Sequential blue, near-zero recedes toward the surface. */
  --s0: #cde2fb; --s1: #9ec5f4; --s2: #6da7ec; --s3: #3987e5;
  --s4: #256abf; --s5: #184f95; --s6: #0d366b;
  --t0: var(--ink-dark); --t1: var(--ink-dark); --t2: var(--ink-dark); --t3: var(--ink-dark);
  --t4: var(--ink-light); --t5: var(--ink-light); --t6: var(--ink-light);
  --warning: #fab219; --serious: #ec835a; --critical: #d03b3b;
  --a0: #86b6ef; --a1: #5598e7; --a2: #3987e5; --a3: #2a78d6; --a4: #256abf; --a5: #184f95; --a6: #0d366b;
  --land: #e1e0d9; --land-edge: #fcfcfb; --ocean: #f4f3ef;
}
@media (prefers-color-scheme: dark) {
  .viz-root {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface-1: #1a1a19;
    --text-primary: #f0efec;
    --text-secondary: #c3c2b7;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --series-1: #3987e5;
    --series-2: #d95926;
    --neutral: #383835;
    /* Same ramp, reversed so near-zero recedes into the dark surface. */
    --s0: #0d366b; --s1: #104281; --s2: #184f95; --s3: #256abf;
    --s4: #3987e5; --s5: #86b6ef; --s6: #cde2fb;
    --t0: var(--ink-light); --t1: var(--ink-light); --t2: var(--ink-light); --t3: var(--ink-light);
    --t4: var(--ink-dark); --t5: var(--ink-dark); --t6: var(--ink-dark);
    --a0: #184f95; --a1: #1c5cab; --a2: #256abf; --a3: #3987e5; --a4: #6da7ec; --a5: #9ec5f4; --a6: #cde2fb;
    --land: #2c2c2a; --land-edge: #1a1a19; --ocean: #141413;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  padding: 24px clamp(12px, 3vw, 40px) 40px;
  background: var(--page);
  color: var(--text-primary);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
h1 { font-size: 22px; margin: 0 0 4px; font-weight: 650; }
h2 { font-size: 16px; margin: 0; font-weight: 600; }
.sub, .hint, footer { color: var(--text-secondary); margin: 0; }
.hint { font-size: 12px; margin-top: 8px; }
footer { font-size: 12px; margin-top: 32px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.top { display: flex; flex-wrap: wrap; gap: 16px 32px; justify-content: space-between; align-items: flex-end; margin-bottom: 20px; }
.tiles { display: flex; gap: 12px; margin: 0; flex-wrap: wrap; }
.tile { background: var(--surface-1); border: 1px solid var(--border); border-radius: 8px; padding: 8px 14px; min-width: 112px; }
.tile dt { font-size: 12px; color: var(--text-secondary); }
.tile dd { margin: 2px 0 0; font-size: 20px; font-weight: 600; font-variant-numeric: tabular-nums; }

.banner { background: var(--surface-1); border: 1px solid var(--critical); border-left-width: 4px; border-radius: 6px; padding: 8px 12px; margin-bottom: 16px; }

.controls { display: flex; flex-wrap: wrap; gap: 12px 20px; align-items: center; margin-bottom: 12px; }
.controls label { color: var(--text-secondary); display: flex; gap: 6px; align-items: center; }
select, button { font: inherit; color: var(--text-primary); }
select { background: var(--surface-1); border: 1px solid var(--axis); border-radius: 6px; padding: 4px 8px; }
.seg { display: inline-flex; border: 1px solid var(--axis); border-radius: 8px; overflow: hidden; }
.seg button { background: var(--surface-1); border: 0; padding: 6px 12px; cursor: pointer; }
.seg button + button { border-left: 1px solid var(--axis); }
.seg button[aria-checked="true"] { background: var(--text-primary); color: var(--surface-1); }
button:focus-visible, select:focus-visible, td:focus-visible { outline: 2px solid var(--series-1); outline-offset: 2px; }
.ghost { background: none; border: 1px solid var(--axis); border-radius: 6px; padding: 4px 10px; cursor: pointer; }

.legend { display: flex; align-items: center; gap: 2px; font-size: 12px; color: var(--text-secondary); }
.legend .sw { display: inline-flex; flex-direction: column; align-items: center; gap: 2px; }
.legend .chip { width: 40px; height: 12px; border-radius: 3px; border: 1px solid var(--border); }
.legend .title { margin-right: 8px; }

.matrix-wrap { overflow: auto; background: var(--surface-1); border: 1px solid var(--border); border-radius: 8px; padding: 8px; max-height: 80vh; }
.matrix { border-collapse: separate; border-spacing: 2px; font-variant-numeric: tabular-nums; font-size: 12px; }
.matrix th { font-weight: 500; color: var(--text-secondary); white-space: nowrap; background: var(--surface-1); }
.matrix thead th { position: sticky; top: 0; z-index: 2; vertical-align: bottom; height: 96px; padding: 0 0 4px; }
.matrix thead th span { writing-mode: vertical-rl; transform: rotate(180deg); display: inline-block; }
.matrix tbody th { position: sticky; left: 0; z-index: 1; text-align: right; padding: 0 8px 0 0; }
.matrix thead th.corner { left: 0; z-index: 3; text-align: right; font-size: 11px; color: var(--text-muted); padding-right: 8px; }
.matrix td { min-width: 40px; height: 24px; text-align: center; border-radius: 4px; cursor: pointer; padding: 0 2px; }
.matrix td.self { color: var(--text-muted); cursor: default; background: none; }
.matrix td.na { color: var(--text-muted); background: var(--neutral); }
.matrix .area-start { box-shadow: inset 2px 0 0 var(--axis); }
.matrix tr.area-start th, .matrix tr.area-start td { box-shadow: inset 0 2px 0 var(--axis); }
.matrix th.hl { color: var(--text-primary); font-weight: 700; }
.matrix td.hover { outline: 2px solid var(--text-primary); outline-offset: -1px; }

.b0 { background: var(--s0); color: var(--t0); } .b1 { background: var(--s1); color: var(--t1); }
.b2 { background: var(--s2); color: var(--t2); } .b3 { background: var(--s3); color: var(--t3); }
.b4 { background: var(--s4); color: var(--t4); } .b5 { background: var(--s5); color: var(--t5); }
.b6 { background: var(--s6); color: var(--t6); }
.a0 { background: var(--a0); } .a1 { background: var(--a1); } .a2 { background: var(--a2); } .a3 { background: var(--a3); }
.a4 { background: var(--a4); } .a5 { background: var(--a5); } .a6 { background: var(--a6); }
.l0 { background: var(--neutral); color: var(--text-secondary); }
.l1 { background: var(--warning); color: var(--ink-dark); }
.l2 { background: var(--serious); color: var(--ink-dark); }
.l3 { background: var(--critical); color: var(--ink-light); }

.tooltip { position: fixed; z-index: 10; pointer-events: none; background: var(--surface-1); color: var(--text-primary); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; box-shadow: 0 4px 16px rgba(0,0,0,.18); font-size: 12px; max-width: 320px; }
.tooltip .tt-title { font-weight: 600; margin-bottom: 4px; }
.tooltip table { border-collapse: collapse; }
.tooltip td { padding: 1px 0; }
.tooltip td + td { padding-left: 12px; text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.tooltip td.tt-sub { padding-left: 16px; color: var(--text-secondary); }
.tooltip .key { display: inline-block; width: 10px; height: 2px; vertical-align: middle; margin-right: 6px; border-radius: 1px; }

.detail { margin-top: 20px; background: var(--surface-1); border: 1px solid var(--border); border-radius: 8px; padding: 16px; }
.detail-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.series-legend { display: flex; gap: 16px; font-size: 12px; color: var(--text-secondary); margin-bottom: 8px; }
.series-legend .key { display: inline-block; width: 16px; height: 2px; vertical-align: middle; margin-right: 6px; border-radius: 1px; }
.detail-controls { display: flex; flex-wrap: wrap; gap: 12px 24px; align-items: center; margin-bottom: 12px; }
.detail-controls .series-legend { margin-bottom: 0; }
.detail-controls label { color: var(--text-secondary); display: flex; gap: 6px; align-items: center; }
.chart-block + .chart-block { margin-top: 16px; }
.chart-block h3 { font-size: 13px; font-weight: 600; margin: 0 0 4px; color: var(--text-secondary); }
.chart { position: relative; min-height: 40px; }
.chart svg { display: block; width: 100%; height: 200px; overflow: visible; }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .axis { stroke: var(--axis); stroke-width: 1; }
.chart text { fill: var(--text-muted); font-size: 11px; }
.chart text.end-label { fill: var(--text-secondary); font-size: 12px; }
.chart .line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .crosshair { stroke: var(--axis); stroke-width: 1; }
.chart .dot { stroke: var(--surface-1); stroke-width: 2; }
.chart .hit { fill: transparent; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--axis); margin-bottom: 14px; }
.tabs button { background: none; border: 0; border-bottom: 2px solid transparent; padding: 8px 14px; cursor: pointer; color: var(--text-secondary); font-weight: 500; margin-bottom: -1px; }
.tabs button[aria-selected="true"] { color: var(--text-primary); border-bottom-color: var(--text-primary); }
body[data-view="map"] .matrix-only, body[data-view="matrix"] .map-only { display: none; }
.controls input[type="range"] { width: 140px; }
.controls output { min-width: 56px; color: var(--text-primary); font-variant-numeric: tabular-nums; }
.matrix-note { margin: 0 0 8px; }

.map-layout { display: grid; grid-template-columns: minmax(0, 1fr) 260px; gap: 16px; align-items: start; }
@media (max-width: 960px) { .map-layout { grid-template-columns: 1fr; } }
.map-frame { position: relative; background: var(--ocean); border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.map { display: block; width: 100%; height: auto; aspect-ratio: 1000 / 438; cursor: grab; touch-action: none; user-select: none; }
.map.dragging { cursor: grabbing; }
.map .land { fill: var(--land); stroke: var(--land-edge); stroke-width: 0.6; vector-effect: non-scaling-stroke; }
.map .link { fill: none; stroke-linecap: round; vector-effect: non-scaling-stroke; cursor: pointer; }
.map .link-flow { fill: none; stroke-linecap: round; vector-effect: non-scaling-stroke; pointer-events: none;
  stroke-dasharray: 2 14; animation: flow linear infinite; }
.map .link-hit { fill: none; stroke: transparent; stroke-width: 10; vector-effect: non-scaling-stroke; cursor: pointer; }
.map .link.hover { stroke-width: 3.5 !important; }
@keyframes flow { to { stroke-dashoffset: -32; } }
@media (prefers-reduced-motion: reduce) { .map .link-flow { animation: none; display: none; } }
.map .site { fill: var(--text-primary); stroke: var(--surface-1); vector-effect: non-scaling-stroke; stroke-width: 2; cursor: pointer; }
.map .site.selected { fill: var(--series-1); }
.map .site.dim { opacity: 0.45; }
.map .site-hit { fill: transparent; cursor: pointer; }
.map .site-label { fill: var(--text-primary); font-size: 11px; paint-order: stroke; stroke: var(--ocean); stroke-width: 3px; pointer-events: none; }
.map .site-label.selected { font-weight: 700; }
.map-zoom { position: absolute; right: 10px; top: 10px; display: flex; flex-direction: column; gap: 4px; }
.map-zoom button { width: 32px; height: 32px; border-radius: 6px; border: 1px solid var(--axis); background: var(--surface-1); cursor: pointer; font-size: 16px; line-height: 1; }
.map-empty { position: absolute; left: 50%; bottom: 14px; transform: translateX(-50%); margin: 0; background: var(--surface-1); border: 1px solid var(--border); border-radius: 6px; padding: 6px 10px; color: var(--text-secondary); font-size: 12px; }

.map-side { background: var(--surface-1); border: 1px solid var(--border); border-radius: 8px; padding: 12px; max-height: 560px; overflow: auto; box-sizing: border-box; }
.map-side h2 { font-size: 14px; }
.map-side .hint { margin: 2px 0 8px; }
.rank-list { list-style: none; margin: 0; padding: 0; font-size: 12px; font-variant-numeric: tabular-nums; }
.rank-list li { display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; padding: 4px 6px; border-radius: 4px; cursor: pointer; }
.rank-list li:hover, .rank-list li:focus-visible { background: var(--neutral); outline: none; }
.rank-list .name { color: var(--text-primary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rank-list .val { color: var(--text-primary); font-weight: 600; text-align: right; }
.rank-list .bar { grid-column: 1 / -1; height: 4px; border-radius: 2px; background: var(--grid); overflow: hidden; }
.rank-list .bar span { display: block; height: 100%; border-radius: 2px; }
