:root {
  color-scheme: light;
  --page: #f8f8f8;
  --surface: #fcfcfb;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #007BFF;
  --male: #2a78d6;
  --female: #eb6834;
  --scenario: #2a78d6;
  --births: #2a78d6;
  --deaths: #eb6834;
  --un: #898781;
  --band: rgba(137, 135, 129, 0.16);
  --age1: #86b6ef;
  --age2: #2a78d6;
  --age3: #104281;
  --nodata: #d6d4cc;
  --stroke: #fcfcfb;
  --hover: rgba(11, 11, 11, 0.05);
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.06);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d; --surface: #1a1a19; --ink: #ffffff; --ink-2: #c3c2b7; --muted: #898781;
    --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10); --accent: #5aa2ff;
    --male: #3987e5; --female: #d95926; --scenario: #3987e5; --births: #3987e5; --deaths: #d95926;
    --band: rgba(195, 194, 183, 0.14); --age1: #184f95; --age2: #3987e5; --age3: #9ec5f4;
    --nodata: #2a2a28; --stroke: #1a1a19; --hover: rgba(255, 255, 255, 0.06); --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --ink: #ffffff; --ink-2: #c3c2b7; --muted: #898781;
  --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10); --accent: #5aa2ff;
  --male: #3987e5; --female: #d95926; --scenario: #3987e5; --births: #3987e5; --deaths: #d95926;
  --band: rgba(195, 194, 183, 0.14); --age1: #184f95; --age2: #3987e5; --age3: #9ec5f4;
  --nodata: #2a2a28; --stroke: #1a1a19; --hover: rgba(255, 255, 255, 0.06); --shadow: none;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { -webkit-tap-highlight-color: transparent; margin: 0; background: var(--page); color: var(--ink); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.num { font-variant-numeric: tabular-nums; }
.wrap { max-width: 1280px; margin: 0 auto; padding: 24px 16px 64px; }
header.top { display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px 16px; margin-bottom: 4px; }
header.top h1 { font-size: 1.75rem; margin: 0; letter-spacing: -0.01em; }
header.top .back { font-size: .9rem; }
.lede { color: var(--ink-2); max-width: 760px; margin: 4px 0 14px; }
.lede .stamp { color: var(--muted); font-size: .82rem; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); padding: 16px; min-width: 0; }
.card h2 { font-size: 1rem; margin: 0 0 2px; }
.card .sub { color: var(--muted); font-size: .82rem; margin: 0 0 8px; }

/* buttons & inputs */
.btn { border: 1px solid var(--border); background: transparent; color: var(--ink); font: inherit; font-size: .85rem; padding: 5px 10px; border-radius: 8px; cursor: pointer; }
.btn:hover { background: var(--hover); }
button:focus, .scrubber:focus { outline: none; }
button:focus-visible { outline: 2px solid var(--axis); outline-offset: 1px; }
.btn[aria-pressed="true"] { background: var(--hover); font-weight: 600; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg .btn { border: 0; border-radius: 0; color: var(--ink-2); }
.seg .btn + .btn { border-left: 1px solid var(--border); }
.seg .btn[aria-pressed="true"] { color: var(--ink); }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
input[type="search"] { font: inherit; font-size: .9rem; padding: 6px 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--ink); width: 220px; max-width: 100%; }
select { font: inherit; font-size: .85rem; padding: 4px 6px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--ink); }
input[type="range"] { accent-color: var(--scenario); }
.check { display: flex; gap: 8px; align-items: flex-start; font-size: .85rem; color: var(--ink-2); cursor: pointer; }
.check input { margin-top: 3px; accent-color: var(--scenario); }

/* master timeline */
.timebar { position: sticky; top: 8px; z-index: 30; display: grid; grid-template-columns: auto 132px minmax(0, 1fr) auto; gap: 8px 14px; align-items: center; padding: 10px 14px; margin-bottom: 16px; }
.timebar .play { width: 36px; height: 36px; padding: 0; display: grid; place-items: center; }
.year-readout { display: flex; flex-direction: column; width: 132px; flex: none; overflow: hidden; }
.year-readout .y { font-size: 1.5rem; font-weight: 650; font-variant-numeric: tabular-nums; line-height: 1.1; }
.year-readout .s { font-size: .75rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.year-readout .s.proj { color: var(--scenario); }
.scrubber { position: relative; height: 38px; cursor: pointer; touch-action: none; user-select: none; -webkit-user-select: none; min-width: 0; }
.scrubber .track { position: absolute; left: 9px; right: 9px; top: 10px; height: 4px; border-radius: 2px;
  background: repeating-linear-gradient(90deg, var(--grid) 0 6px, transparent 6px 9px); }
.scrubber .past { position: absolute; left: 0; top: 0; bottom: 0; background: var(--grid); border-radius: 2px; }
.scrubber .fill { position: absolute; left: 0; top: 0; bottom: 0; background: var(--scenario); border-radius: 2px; }
.scrubber .notch { position: absolute; top: -4px; width: 2px; height: 12px; margin-left: -1px; background: var(--ink-2); border-radius: 1px; }
.scrubber .thumb { position: absolute; top: 3px; width: 18px; height: 18px; margin-left: -9px; border-radius: 50%; background: var(--surface); border: 2px solid var(--scenario); box-shadow: 0 1px 3px rgba(0,0,0,.25); pointer-events: none; }
.scrubber.dragging .thumb { transform: scale(1.12); }
.scrubber:focus-visible .thumb { box-shadow: 0 0 0 3px var(--hover), 0 0 0 5px var(--axis); }
.ticks { position: absolute; left: 0; right: 0; bottom: 0; height: 14px; font-size: .7rem; color: var(--muted); font-variant-numeric: tabular-nums; pointer-events: none; }
.ticks span { position: absolute; transform: translateX(-50%); white-space: nowrap; }
.ticks span.today { color: var(--ink-2); font-weight: 600; }
@media (max-width: 760px) {
  .timebar { grid-template-columns: auto minmax(0, 1fr) auto; top: 4px; }
  .scrubber { grid-column: 1 / -1; grid-row: 2; }
  .year-readout { width: auto; min-width: 0; }
  .year-readout .s { white-space: normal; line-height: 1.2; }
}

/* map + country: one card, two columns */
.atlas { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(320px, 1fr); gap: 0 24px; }
.atlas-map { min-width: 0; }
.atlas-country { min-width: 0; display: flex; flex-direction: column; border-left: 1px solid var(--grid); padding-left: 24px; }
@media (max-width: 1000px) {
  .atlas { grid-template-columns: minmax(0, 1fr); }
  .atlas-country { border-left: 0; padding-left: 0; border-top: 1px solid var(--grid); padding-top: 16px; margin-top: 16px; }
}
.map-head { display: flex; flex-wrap: wrap; gap: 10px 16px; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.map-title { font-weight: 600; }
.viewbox { position: relative; width: 100%; aspect-ratio: 1.8 / 1; min-height: 230px; }
.view { position: absolute; inset: 0; }
#map { width: 100%; height: 100%; }
#map svg { display: block; width: 100%; height: 100%; cursor: grab; touch-action: pan-y; }
#map svg:active { cursor: grabbing; }
#map .sphere { fill: none; stroke: var(--grid); }
#map path.country { stroke: var(--stroke); stroke-width: .4; vector-effect: non-scaling-stroke; cursor: pointer; }
#map path.country:hover { stroke: var(--ink); stroke-width: 1; }
#map path.country.selected { stroke: var(--ink); stroke-width: 1.8; }
#map path.sub { stroke: var(--stroke); stroke-width: .6; vector-effect: non-scaling-stroke; cursor: default; }
#map path.sub:hover { stroke: var(--ink); stroke-width: 1.2; }
#map path.sub-outline { fill: none; stroke: var(--ink); stroke-width: 1.6; vector-effect: non-scaling-stroke; pointer-events: none; }
#map path.sub.selected { stroke: var(--ink); stroke-width: 2; }
#map .lakes path { fill: var(--surface); stroke: none; }
.zoom-ctl { position: absolute; right: 8px; bottom: 8px; display: flex; flex-direction: column; gap: 4px; }
.zoom-ctl button { width: 30px; height: 30px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--ink); font-size: 16px; cursor: pointer; display: grid; place-items: center; padding: 0; }
.zoom-ctl svg path { fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.zoom-ctl .fs-out, body.map-full .zoom-ctl .fs-in { display: none; }
body.map-full .zoom-ctl .fs-out { display: inline; }
.map-hint { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; text-align: center; padding: 16px;
  background: rgba(0, 0, 0, .5); color: #fff; font-size: .95rem; pointer-events: none; opacity: 0; transition: opacity .25s; }
.map-hint.on { opacity: 1; }

/* full screen: the map card covers the window, with the timeline on top */
body.map-full { overflow: hidden; }
body.map-full .atlas-map { position: fixed; inset: 0; z-index: 60; background: var(--surface); display: flex; flex-direction: column; gap: 8px;
  padding: max(10px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left)); }
body.map-full .atlas-map .timebar { position: static; margin: 0; box-shadow: none; padding: 6px 10px; }
body.map-full .map-head { margin-bottom: 0; }
body.map-full .viewbox { flex: 1 1 auto; aspect-ratio: auto; min-height: 0; }
body.map-full .map-foot { margin-top: 0; }
body.map-full .regions { display: none; }
body.map-full #map svg { touch-action: none; }
body.map-full .map-hint { display: none; }
.map-foot { display: flex; flex-wrap: wrap; gap: 4px 16px; align-items: center; margin-top: 8px; }
.map-foot svg { display: block; width: min(360px, 100%); height: 34px; overflow: visible; }
.map-foot svg text { font-size: 10.5px; fill: var(--ink-2); font-variant-numeric: tabular-nums; }
.map-foot .cap { font-size: .75rem; color: var(--muted); line-height: 1.3; }
.regions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.regions .btn { font-size: .8rem; padding: 3px 9px; }

/* table view: compact ranked bars */
#table-view, #about-view { display: none; overflow: auto; }
#table-view table { width: 100%; border-collapse: collapse; font-size: .84rem; table-layout: fixed; }
#table-view th, #table-view td { padding: 3px 6px; text-align: left; white-space: nowrap; }
#table-view th { position: sticky; top: 0; z-index: 1; background: var(--surface); color: var(--muted); font-weight: 500; font-size: .75rem; cursor: pointer; user-select: none; border-bottom: 1px solid var(--grid); padding-top: 4px; padding-bottom: 5px; }
#table-view th.rk, #table-view td.rk { width: 36px; color: var(--muted); font-variant-numeric: tabular-nums; }
#table-view th.pop, #table-view td.pop { width: 76px; text-align: right; color: var(--ink-2); font-variant-numeric: tabular-nums; }
#table-view th.tfr { width: 46%; }
#table-view td.nm { overflow: hidden; text-overflow: ellipsis; }
#table-view th .sm { display: none; }
@media (max-width: 560px) {
  #table-view th .lg { display: none; }
  #table-view th .sm { display: inline; }
  #table-view th.tfr { width: 34%; }
  #table-view th.rk, #table-view td.rk { width: 30px; }
  #table-view th.pop, #table-view td.pop { width: 56px; }
  .tfrv { gap: 5px; }
}
#table-view tbody tr { cursor: pointer; }
#table-view tbody tr:hover td { background: var(--hover); }
#table-view tbody tr.sel td { background: var(--hover); font-weight: 600; }
.tfrv { display: flex; align-items: center; gap: 8px; }
.tfrv .bw { flex: 1; height: 9px; }
.tfrv .bw span { display: block; height: 100%; border-radius: 2px; }
.tfrv b { width: 34px; text-align: right; font-variant-numeric: tabular-nums; }
#about-view { padding: 2px 6px 8px 0; font-size: .86rem; color: var(--ink-2); }
#about-view h3 { font-size: .95rem; color: var(--ink); margin: 12px 0 2px; }
#about-view h3:first-child { margin-top: 0; }
#about-view p { margin: 0 0 6px; line-height: 1.5; }
#about-view .muted, #about-view #data-stamp { color: var(--muted); }
#about-country { border-bottom: 1px solid var(--grid); padding-bottom: 8px; margin-bottom: 10px; }
#about-country ul { margin: 2px 0 0; padding-left: 18px; }
#about-country li { margin: 3px 0; line-height: 1.45; }

/* country column */
.atlas-country h2.name { font-size: 1.4rem; margin: 2px 0 10px; letter-spacing: -0.01em; }
.atlas-country .crumbs { font-size: .82rem; color: var(--muted); margin-bottom: -2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.figs { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; }
@media (max-width: 480px) { .figs { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.figs > div { background: var(--page); border-radius: 8px; padding: 8px 9px; min-width: 0; }
.figs .v { font-size: 1.25rem; font-weight: 650; letter-spacing: -0.01em; display: flex; align-items: center; gap: 7px; }
.figs .v .sw { width: 10px; height: 10px; border-radius: 2px; flex: none; }
.figs .l { font-size: .75rem; color: var(--ink-2); margin-top: 1px; line-height: 1.3; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: flex; align-items: center; gap: 4px; }
.info { border: 0; background: none; padding: 0 2px; color: var(--muted); cursor: pointer; font: inherit; font-size: .8rem; line-height: 1; }
.info:hover { color: var(--accent); }
.pyr-head { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 6px 12px; align-items: center; margin: 12px 0 2px; }
.pyr-head h2 { font-size: 1rem; margin: 0; }
.pyr-head .toolbar { gap: 6px; }
.pyr-head select { max-width: 132px; font-size: .8rem; }
.pyr-head .toolbar .seg .btn { padding: 4px 9px; }
#pyramid { position: relative; flex: 1 1 auto; min-height: 300px; margin-top: 4px; }
#pyramid svg { position: absolute; inset: 0; width: 100%; height: 100%; }
#pyramid .pyr-empty { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; text-align: center; color: var(--muted); font-size: .9rem; line-height: 1.5; }
@media (max-width: 1000px) { #pyramid { min-height: 420px; } }

/* future */
.future { margin-top: 20px; }
.panel-head { display: flex; flex-wrap: wrap; gap: 4px 16px; align-items: center; margin-bottom: 12px; }
.panel-head .seg { margin-left: auto; }
.panel-head h2 { font-size: 1.4rem; margin: 0; letter-spacing: -0.01em; }
.panel-head .facts { color: var(--ink-2); font-size: .9rem; }
.off-note { display: none; color: var(--ink-2); margin: 0 0 12px; }
.future.off .off-note { display: block; }
.future.off .controls, .future.off #outcomes { display: none; }
.future.off .layout { grid-template-columns: minmax(0, 1fr); }
.layout { display: grid; grid-template-columns: 300px minmax(0, 1fr); gap: 16px; align-items: start; }
@media (max-width: 900px) { .layout { grid-template-columns: minmax(0, 1fr); } }
.controls { position: sticky; top: 92px; }
@media (max-width: 900px) { .controls { position: static; } }
.ctl { margin: 16px 0; }
.ctl .seg + .hint { margin-top: 6px; }
.presets .btn[aria-pressed="true"] { background: var(--hover); }
.link { border: 0; background: none; color: var(--accent); cursor: pointer; font: inherit; font-size: .8rem; padding: 0; }
.link:hover { text-decoration: underline; }
.controls .ctl:last-of-type { margin-bottom: 4px; }
.ctl:first-child { margin-top: 6px; }
.ctl .lbl { display: flex; justify-content: space-between; align-items: baseline; font-size: .85rem; color: var(--ink-2); margin-bottom: 4px; }
.ctl .lbl output { color: var(--ink); font-weight: 650; font-size: 1rem; font-variant-numeric: tabular-nums; }
.ctl input[type="range"] { width: 100%; }
.ctl .lbl output.editable { cursor: text; border-bottom: 1px dashed var(--axis); }
.ctl .lbl input.num-edit { width: 5.5em; font: inherit; font-weight: 650; font-size: 1rem; text-align: right; padding: 0 4px; border: 1px solid var(--scenario); border-radius: 6px; background: var(--surface); color: var(--ink); }
.ctl .hint { font-size: .75rem; color: var(--muted); margin-top: 2px; }
.ctl .seg { display: flex; }
.ctl .seg .btn { flex: 1; padding: 6px 4px; font-size: .8rem; white-space: nowrap; }
.ctl.disabled { opacity: .45; }
.presets { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
.presets .btn { font-size: .78rem; padding: 3px 8px; }
.tiles { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
@media (max-width: 640px) { .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.tiles > div { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); padding: 12px 14px; min-width: 0; }
.tiles .k { font-size: .75rem; color: var(--ink-2); }
.tiles .v { font-size: 1.5rem; font-weight: 650; letter-spacing: -0.01em; margin-top: 2px; line-height: 1.15; }
.tiles .d { font-size: .78rem; color: var(--muted); margin-top: 2px; }
.charts { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.charts .wide { grid-column: 1 / -1; }
.gens { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px 20px; margin-top: 6px; }
@media (max-width: 640px) { .gens { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.gen .gv { font-size: 1.4rem; font-weight: 650; font-variant-numeric: tabular-nums; line-height: 1.1; }
.gen .gl { font-size: .78rem; color: var(--ink-2); margin-bottom: 6px; white-space: nowrap; }
.gen svg { display: block; width: 100%; }
.gen-unit { font-size: .75rem; color: var(--muted); margin-top: 6px; }
.card-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: flex-start; gap: 8px; }
.card-head .seg .btn { padding: 4px 10px; font-size: .8rem; }
@media (max-width: 760px) { .charts { grid-template-columns: minmax(0, 1fr); } }


/* charts */
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; overflow: visible; }
.legend-row { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: .78rem; color: var(--ink-2); margin: 2px 0 6px; min-height: 18px; }
.legend-row .it { display: inline-flex; align-items: center; gap: 6px; }
.legend-row .ln { width: 16px; height: 0; border-top: 2px solid; }
.legend-row .ln.dash { border-top-style: dashed; }
.legend-row .bx { width: 10px; height: 10px; border-radius: 2px; }
.legend-row .dot { width: 9px; height: 9px; border-radius: 50%; }
.axis text { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.axis path, .axis line { stroke: var(--axis); }
.grid line { stroke: var(--grid); }
.grid path { display: none; }
.lbl-direct, .age-lbl { font-size: 11px; fill: var(--ink-2); }
.annot { font-size: 11px; fill: var(--muted); }
.handle { cursor: grab; }
.handle:active { cursor: grabbing; }

.tip { position: fixed; pointer-events: none; z-index: 100; background: var(--surface); color: var(--ink); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font-size: .8rem; line-height: 1.35; box-shadow: 0 4px 18px rgba(0,0,0,.16); max-width: 280px; opacity: 0; transition: opacity .08s; }
.tip .t { font-weight: 650; margin-bottom: 2px; }
.tip .r { display: flex; justify-content: space-between; gap: 14px; font-variant-numeric: tabular-nums; }
.tip .r span:first-child { color: var(--ink-2); display: inline-flex; align-items: center; gap: 6px; }
.tip .m { color: var(--muted); font-size: .74rem; margin-top: 3px; }
.tip i { display: inline-block; width: 8px; height: 8px; border-radius: 2px; }

.loading { color: var(--muted); padding: 40px 0; text-align: center; }

/* phones */
@media (max-width: 640px) {
  .wrap { padding: 10px 10px 48px; }
  .card { padding: 12px; border-radius: 10px; }
  .timebar { gap: 0 10px; padding: 6px 10px 4px; margin-bottom: 10px; top: 4px; }
  .timebar .play { width: 34px; height: 34px; }
  .timebar .btn { padding: 4px 9px; }
  .year-readout .y { font-size: 1.25rem; }
  .year-readout .s { font-size: .7rem; }
  .scrubber { height: 34px; }
  .map-head { gap: 8px; }
  .map-head .toolbar { width: 100%; flex-wrap: nowrap; }
  .map-head input[type="search"] { flex: 1 1 0; width: 0; min-width: 0; }
  .map-head .seg { flex: none; }
  .map-head .seg .btn { padding: 5px 9px; }
  .viewbox { aspect-ratio: 1.3 / 1; }
  input[type="search"], select, .ctl .lbl input.num-edit { font-size: 16px; } /* below 16px iOS zooms the page on focus */
  .zoom-ctl button { width: 36px; height: 36px; }
  .regions { gap: 5px; }
  .atlas-country { padding-top: 12px; margin-top: 12px; }
  .atlas-country h2.name { font-size: 1.25rem; margin-bottom: 8px; }
  .figs .v { font-size: 1.1rem; }
  #pyramid { min-height: 360px; }
  .future { margin-top: 14px; }
  .panel-head { margin-bottom: 8px; }
  .panel-head h2 { font-size: 1.2rem; }
  .panel-head .seg { margin-left: 0; }
  .layout, .charts { gap: 10px; }
  .tiles { gap: 8px; margin-bottom: 10px; }
  .tiles > div { padding: 10px 12px; border-radius: 10px; }
  .tiles .v { font-size: 1.25rem; }
  .ctl { margin: 12px 0; }
}
