:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface-1: #fcfcfb;
  --surface-2: #f3f2ee;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #6f6d68;
  --axis-muted: #898781;
  --grid: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --reference: #898781;
  --up: #006300;
  --down: #b42727;
  --focus: #2a78d6;
  --seq-1: #cde2fb;
  --seq-2: #9ec5f4;
  --seq-3: #5598e7;
  --seq-4: #256abf;
  --seq-5: #104281;
  --ink-1: #0b0b0b;
  --ink-2: #0b0b0b;
  --ink-3: #0b0b0b;
  --ink-4: #ffffff;
  --ink-5: #ffffff;
  --div-1: #c93b3a;
  --div-2: #eda5a2;
  --div-3: #f0efec;
  --div-4: #86b6ef;
  --div-5: #256abf;
  --dink-edge: #ffffff;
  --dink-mid: #0b0b0b;
  --nodata: #e8e7e2;
  --shadow: 0 8px 24px rgba(11, 11, 11, 0.12);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface-1: #1a1a19;
    --surface-2: #232321;
    --text-primary: #f0efec;
    --text-secondary: #c3c2b7;
    --text-muted: #a3a29b;
    --axis-muted: #898781;
    --grid: #2c2c2a;
    --baseline: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --series-1: #3987e5;
    --series-2: #d95926;
    --reference: #a3a29b;
    --up: #0ca30c;
    --down: #e66767;
    --focus: #6da7ec;
    --seq-1: #184f95;
    --seq-2: #1c5cab;
    --seq-3: #2a78d6;
    --seq-4: #6da7ec;
    --seq-5: #b7d3f6;
    --ink-1: #ffffff;
    --ink-2: #ffffff;
    --ink-3: #ffffff;
    --ink-4: #0b0b0b;
    --ink-5: #0b0b0b;
    --div-1: #e66767;
    --div-2: #8a3b3a;
    --div-3: #383835;
    --div-4: #1c5cab;
    --div-5: #6da7ec;
    --dink-edge: #0b0b0b;
    --dink-mid: #ffffff;
    --nodata: #2c2c2a;
    --shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface-1: #1a1a19;
  --surface-2: #232321;
  --text-primary: #f0efec;
  --text-secondary: #c3c2b7;
  --text-muted: #a3a29b;
  --axis-muted: #898781;
  --grid: #2c2c2a;
  --baseline: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --series-1: #3987e5;
  --series-2: #d95926;
  --reference: #a3a29b;
  --up: #0ca30c;
  --down: #e66767;
  --focus: #6da7ec;
  --seq-1: #184f95;
  --seq-2: #1c5cab;
  --seq-3: #2a78d6;
  --seq-4: #6da7ec;
  --seq-5: #b7d3f6;
  --ink-1: #ffffff;
  --ink-2: #ffffff;
  --ink-3: #ffffff;
  --ink-4: #0b0b0b;
  --ink-5: #0b0b0b;
  --div-1: #e66767;
  --div-2: #8a3b3a;
  --div-3: #383835;
  --div-4: #1c5cab;
  --div-5: #6da7ec;
  --dink-edge: #0b0b0b;
  --dink-mid: #ffffff;
  --nodata: #2c2c2a;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--page);
  color: var(--text-primary);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--series-1); }
a:focus-visible, button:focus-visible, select:focus-visible, input:focus-visible, [tabindex]:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}
.wrap { max-width: 1240px; margin: 0 auto; padding: 0 24px; }

header.top {
  border-bottom: 1px solid var(--border);
  background: var(--surface-1);
  position: sticky;
  top: 0;
  z-index: 10;
}
header.top .wrap { display: flex; align-items: center; gap: 24px; min-height: 56px; flex-wrap: wrap; }
.brand { font-weight: 650; font-size: 16px; color: var(--text-primary); text-decoration: none; white-space: nowrap; }
.brand span { color: var(--text-muted); font-weight: 500; }
nav.sections { display: flex; gap: 4px; flex-wrap: wrap; flex: 1; }
nav.sections a {
  color: var(--text-secondary);
  text-decoration: none;
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 14px;
}
nav.sections a:hover { background: var(--surface-2); color: var(--text-primary); }
.theme-toggle {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-secondary);
  border-radius: 6px;
  padding: 5px 10px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}

.intro { padding: 32px 0 8px; }
.intro h1 { font-size: 28px; line-height: 1.2; margin: 0 0 8px; font-weight: 650; letter-spacing: -0.01em; }
.intro p { margin: 0; color: var(--text-secondary); max-width: 760px; }

section.block { padding: 28px 0 8px; scroll-margin-top: 64px; }
section.block > h2 { font-size: 20px; margin: 0 0 4px; font-weight: 650; }
section.block > .sub { color: var(--text-secondary); margin: 0 0 16px; font-size: 14px; }

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px 18px;
  min-width: 0;
}
.card h3 { font-size: 15px; margin: 0 0 2px; font-weight: 600; }
.card .note { color: var(--text-muted); font-size: 12.5px; margin: 0 0 10px; }

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; }
.tile { padding: 14px 16px 12px; display: flex; flex-direction: column; gap: 2px; }
.tile .label { font-size: 13px; color: var(--text-secondary); }
.tile .value { font-size: 26px; font-weight: 600; line-height: 1.2; }
.tile .value small { font-size: 13px; font-weight: 500; color: var(--text-muted); margin-left: 4px; }
.tile .meta { font-size: 12.5px; color: var(--text-muted); display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.delta { font-weight: 600; }
.delta.good { color: var(--up); }
.delta.bad { color: var(--down); }
.delta.flat { color: var(--text-muted); }
.spark { display: block; width: 100%; height: 28px; margin-top: 6px; }

.controls { display: flex; flex-wrap: wrap; gap: 12px; align-items: end; margin-bottom: 12px; }
.controls label { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--text-secondary); }
select, input[type="search"] {
  font: inherit;
  font-size: 14px;
  color: var(--text-primary);
  background: var(--surface-1);
  border: 1px solid var(--baseline);
  border-radius: 6px;
  padding: 6px 10px;
  min-height: 34px;
  max-width: 100%;
}
input[type="search"] { min-width: 220px; }
button.btn {
  font: inherit;
  font-size: 14px;
  border: 1px solid var(--baseline);
  background: var(--surface-1);
  color: var(--text-primary);
  border-radius: 6px;
  padding: 6px 12px;
  min-height: 34px;
  cursor: pointer;
}
button.btn:hover { background: var(--surface-2); }

.grid-2 { display: grid; grid-template-columns: minmax(0, 1.3fr) minmax(0, 1fr); gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 340px), 1fr)); gap: 16px; }
.card .controls label { flex: 1 1 140px; min-width: 0; }
.card select { width: 100%; }
@media (max-width: 900px) {
  .grid-2, .grid-3 { grid-template-columns: minmax(0, 1fr); }
}

svg text { fill: var(--text-secondary); font-size: 12px; font-family: inherit; }
svg .axis text { fill: var(--axis-muted); font-variant-numeric: tabular-nums; }
svg .grid line { stroke: var(--grid); stroke-width: 1; }
svg .baseline { stroke: var(--baseline); stroke-width: 1; }
svg .bar { fill: var(--series-1); }
svg .bar.dim { fill: var(--series-1); opacity: 0.35; }
svg .bar.s2 { fill: var(--series-2); }
svg .refline { stroke: var(--reference); stroke-width: 1.5; }
svg .line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
svg .line.s1 { stroke: var(--series-1); }
svg .line.s2 { stroke: var(--series-2); }
svg .area { fill: var(--series-1); opacity: 0.10; }
svg .dot { stroke: var(--surface-1); stroke-width: 2; }
svg .dot.s1 { fill: var(--series-1); }
svg .dot.s2 { fill: var(--series-2); }
svg .hit { fill: transparent; cursor: pointer; }
svg .value-label { fill: var(--text-primary); font-variant-numeric: tabular-nums; }
svg .row-label { fill: var(--text-secondary); }
svg .row-label.sel { fill: var(--text-primary); font-weight: 650; }
svg .crosshair { stroke: var(--baseline); stroke-width: 1; }
svg .spark-line { fill: none; stroke: var(--axis-muted); stroke-width: 1.5; }
svg .spark-dot { fill: var(--series-1); }

/* Tile map */
.tilemap { width: 100%; height: auto; display: block; }
.tilemap .cell { stroke: var(--surface-1); stroke-width: 2; cursor: pointer; }
.tilemap .cell.sel { stroke: var(--text-primary); stroke-width: 2.5; }
.tilemap .t-name { font-size: 15px; font-weight: 600; pointer-events: none; }
.tilemap .t-val { font-size: 15px; pointer-events: none; font-variant-numeric: tabular-nums; }
.tilemap .ink-1 { fill: var(--ink-1); }
.tilemap .ink-2 { fill: var(--ink-2); }
.tilemap .ink-3 { fill: var(--ink-3); }
.tilemap .ink-4 { fill: var(--ink-4); }
.tilemap .ink-5 { fill: var(--ink-5); }
.tilemap .ink-0 { fill: var(--text-secondary); }
.scale { display: flex; align-items: center; gap: 8px; margin-top: 10px; font-size: 12px; color: var(--text-muted); flex-wrap: wrap; }
.scale .steps { display: flex; gap: 2px; }
.scale .steps span { width: 34px; height: 10px; border-radius: 2px; display: block; }
.seq-1 { fill: var(--seq-1); background: var(--seq-1); }
.seq-2 { fill: var(--seq-2); background: var(--seq-2); }
.seq-3 { fill: var(--seq-3); background: var(--seq-3); }
.seq-4 { fill: var(--seq-4); background: var(--seq-4); }
.seq-5 { fill: var(--seq-5); background: var(--seq-5); }
.nodata { fill: var(--nodata); background: var(--nodata); }
/* Diverging (net migration): red = more people left, blue = more arrived, gray = balanced. */
.div-1 { fill: var(--div-1); background: var(--div-1); }
.div-2 { fill: var(--div-2); background: var(--div-2); }
.div-3 { fill: var(--div-3); background: var(--div-3); }
.div-4 { fill: var(--div-4); background: var(--div-4); }
.div-5 { fill: var(--div-5); background: var(--div-5); }
.tilemap .dink-1, .tilemap .dink-5 { fill: var(--dink-edge); }
.tilemap .dink-2, .tilemap .dink-3, .tilemap .dink-4 { fill: var(--dink-mid); }

.facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 10px 16px; margin-top: 18px; border-top: 1px solid var(--grid); padding-top: 14px; }
.facts div { display: flex; flex-direction: column; }
.facts span { font-size: 12.5px; color: var(--text-muted); }
.facts b { font-weight: 600; font-size: 15px; }
.legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: 12.5px; color: var(--text-secondary); margin: 2px 0 8px; }
.legend .key { display: inline-flex; align-items: center; gap: 6px; }
.legend .sw { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }
.legend .sw.s1 { background: var(--series-1); }
.legend .sw.s2 { background: var(--series-2); }
.legend .sw.ref { background: var(--reference); height: 2px; width: 14px; border-radius: 0; }

/* Profile */
.profile-head { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.profile-head h3 { font-size: 22px; margin: 0; font-weight: 650; }
.profile-head .zone { color: var(--text-muted); font-size: 14px; }
.ptiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(178px, 1fr)); gap: 10px; margin-bottom: 16px; }
.ptile { padding: 10px 12px; }
.ptile .label { font-size: 12.5px; color: var(--text-secondary); line-height: 1.3; }
.ptile .value { font-size: 19px; font-weight: 600; }
.ptile .rank { font-size: 12px; color: var(--text-muted); }

/* Tables */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 10px; background: var(--surface-1); max-height: 560px; overflow-y: auto; }
table { border-collapse: collapse; width: 100%; font-size: 13.5px; }
thead th {
  position: sticky; top: 0; background: var(--surface-2); text-align: left; font-weight: 600;
  padding: 8px 10px; border-bottom: 1px solid var(--baseline); white-space: nowrap; color: var(--text-secondary);
}
thead th.num, td.num { text-align: right; }
thead th button { all: unset; cursor: pointer; }
thead th button:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
thead th[aria-sort="ascending"] button::after { content: " ▲"; font-size: 10px; }
thead th[aria-sort="descending"] button::after { content: " ▼"; font-size: 10px; }
tbody td { padding: 6px 10px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
td.num { font-variant-numeric: tabular-nums; }
tbody tr:hover td { background: var(--surface-2); }
td .neg { color: var(--down); }
td .pos { color: var(--up); }
.count { font-size: 13px; color: var(--text-muted); }

.cat-list { list-style: none; padding: 0; margin: 0; }
.cat-list li { padding: 10px 14px; border-bottom: 1px solid var(--grid); }
.cat-list li:last-child { border-bottom: 0; }
.cat-list .t { font-weight: 550; }
.cat-list .m { color: var(--text-muted); font-size: 12.5px; }
.chip { display: inline-block; font-size: 11.5px; padding: 1px 7px; border-radius: 999px; background: var(--surface-2); color: var(--text-secondary); margin-right: 4px; }

.tooltip {
  position: fixed; pointer-events: none; z-index: 50;
  background: var(--surface-1); color: var(--text-primary);
  border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow);
  padding: 8px 10px; font-size: 13px; line-height: 1.4; max-width: 280px;
  opacity: 0; transition: opacity 80ms;
}
.tooltip.on { opacity: 1; }
.tooltip b { font-weight: 650; }
.tooltip .tv { font-variant-numeric: tabular-nums; }

footer { margin: 40px 0 0; padding: 24px 0 40px; border-top: 1px solid var(--border); color: var(--text-muted); font-size: 13px; }
footer p { margin: 0 0 8px; max-width: 860px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.loading { padding: 60px 0; color: var(--text-muted); }
.hidden { display: none !important; }

@media (max-width: 640px) {
  .wrap { padding: 0 16px; }
  .intro h1 { font-size: 23px; }
  header.top { position: static; }
  input[type="search"] { min-width: 0; width: 100%; }
  .controls label { flex: 1 1 140px; }
}

/* ---------------------------------------------------------------- All-tables explorer */
:root {
  --c0: #2a78d6; --c1: #eb6834; --c2: #1f9e6e; --c3: #8a5cd1;
  --c4: #c9a20c; --c5: #d1457a; --c6: #14a3b8; --c7: #6f6d68;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --c0: #4b93ea; --c1: #f07a48; --c2: #34b985; --c3: #a47ce4;
    --c4: #e2bb2a; --c5: #e56a97; --c6: #2fbfd3; --c7: #a3a29b;
  }
}
:root[data-theme="dark"] {
  --c0: #4b93ea; --c1: #f07a48; --c2: #34b985; --c3: #a47ce4;
  --c4: #e2bb2a; --c5: #e56a97; --c6: #2fbfd3; --c7: #a3a29b;
}
.c0 { --c: var(--c0); } .c1 { --c: var(--c1); } .c2 { --c: var(--c2); } .c3 { --c: var(--c3); }
.c4 { --c: var(--c4); } .c5 { --c: var(--c5); } .c6 { --c: var(--c6); } .c7 { --c: var(--c7); }
svg .line[class*=" c"] { stroke: var(--c); }
svg .dot[class*=" c"] { fill: var(--c); }
svg .bar-c { fill: var(--c); }
.legend .sw[class*=" c"], .sw-inline { background: var(--c); }
.sw-inline { display: inline-block; width: 9px; height: 9px; border-radius: 2px; margin-right: 6px; }
.legend .x-remove { all: unset; cursor: pointer; color: var(--text-muted); padding: 0 4px; border-radius: 4px; line-height: 1; }
.legend .x-remove:hover { color: var(--text-primary); background: var(--surface-2); }
.legend .x-remove:focus-visible { outline: 2px solid var(--focus); }
svg .crosshair.hidden { display: none; }
svg .rank-row { cursor: pointer; outline: none; }
svg .rank-row:focus-visible .row-label { fill: var(--focus); text-decoration: underline; }

nav.sections a[aria-current="page"] { background: var(--surface-2); color: var(--text-primary); font-weight: 600; }

.explorer { display: grid; grid-template-columns: minmax(260px, 340px) minmax(0, 1fr); gap: 20px; align-items: start; margin-top: 16px; }
.x-list { position: sticky; top: 72px; padding: 14px 0 0; display: flex; flex-direction: column; max-height: calc(100vh - 88px); }
.x-list .controls { padding: 0 14px; margin-bottom: 8px; }
.controls.stack { flex-direction: column; align-items: stretch; gap: 8px; }
.controls.stack label, .controls.stack input, .controls.stack select { width: 100%; min-width: 0; }
.card .controls.stack label, .controls.stack .row-2 label { flex: 0 0 auto; }
.row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.x-tables { list-style: none; margin: 0; padding: 0; overflow-y: auto; border-top: 1px solid var(--grid); flex: 1; }
.x-tables li { border-bottom: 1px solid var(--grid); }
.x-tables li.none { padding: 14px; color: var(--text-muted); }
.x-tables button {
  all: unset; box-sizing: border-box; display: flex; flex-direction: column; gap: 2px; width: 100%;
  padding: 9px 14px; cursor: pointer;
}
.x-tables button:hover { background: var(--surface-2); }
.x-tables button:focus-visible { outline: 2px solid var(--focus); outline-offset: -2px; }
.x-tables button[aria-selected="true"] { background: var(--surface-2); box-shadow: inset 3px 0 0 var(--series-1); }
.x-tables .t { font-size: 13.5px; font-weight: 550; line-height: 1.35; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.x-tables .m { font-size: 12px; color: var(--text-muted); }

.x-view { min-width: 0; display: flex; flex-direction: column; gap: 16px; }
.x-view > div { display: flex; flex-direction: column; gap: 16px; }
.x-view > div.hidden { display: none; }
#t-body { display: flex; flex-direction: column; gap: 16px; }
.x-empty h2 { margin: 0 0 6px; font-size: 18px; }
.x-empty p { color: var(--text-secondary); margin: 0 0 16px; }
.x-head h2 { font-size: 21px; line-height: 1.3; margin: 2px 0 4px; font-weight: 650; }
.x-crumb { margin: 0; font-size: 13px; color: var(--text-muted); }
.x-en { margin: 0 0 6px; font-size: 13px; color: var(--text-muted); }
.x-meta { margin: 0 0 12px; font-size: 13px; color: var(--text-secondary); font-variant-numeric: tabular-nums; }
.x-actions { display: flex; flex-wrap: wrap; gap: 8px; }
a.btn {
  font-size: 14px; border: 1px solid var(--baseline); background: var(--surface-1); color: var(--text-primary);
  border-radius: 6px; padding: 6px 12px; min-height: 34px; text-decoration: none; display: inline-flex; align-items: center;
}
a.btn:hover { background: var(--surface-2); }
#filters label { flex: 1 1 200px; min-width: 0; }
#filters select { width: 100%; }
.x-charts .controls label { flex: 0 1 180px; }
.x-pivot .controls { align-items: center; }
.x-pivot .controls h3 { margin: 0 8px 0 0; }
.x-pivot .table-wrap { max-height: 620px; }
#pivot tbody th { position: sticky; left: 0; background: var(--surface-1); text-align: left; font-weight: 500; padding: 6px 10px; border-bottom: 1px solid var(--grid); white-space: nowrap; max-width: 320px; overflow: hidden; text-overflow: ellipsis; }
#pivot thead th:first-child { position: sticky; left: 0; z-index: 2; }
#pivot tbody tr:hover th { background: var(--surface-2); }
#rank-more, #pivot-more { margin-top: 10px; }

@media (max-width: 900px) {
  .explorer { grid-template-columns: minmax(0, 1fr); }
  .x-list { position: static; max-height: 420px; }
}
