:root {
  --canvas: #f3f4f7; --card: #fff; --ink: #262a35; --muted: #6b707d; --line: #eceef3; --surface: #f7f8fa;
  --dark: #1f2433; --purple: #6a4cf5; --purple-dark: #5537e6; --purple-soft: #efebff;
  --accent: #08519c; --nodata: #dfe2e8; --stroke: #fff; --selected: #f59e0b;
  --gradient: linear-gradient(135deg, #5fb784 0%, #6f97a6 45%, #8a4fe5 100%);
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 6px 24px rgba(16, 24, 40, .05);
  --lift: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 14px rgba(16, 24, 40, .08);
  --radius: 24px;
  --sans: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  --display: "Fraunces", Georgia, serif;
  --badge-bg: #fbf2c0; --badge-ink: #6b5600; --green-bg: #d8f5e6; --green-ink: #0b6b3f; --purple-ink: #4a2fc9; --soft-hover: #e3dcff;
  /* Solid focus ring: at least 3:1 against every background it sits on. */
  --focus: #6a4cf5;
  color-scheme: light;
}
/* Dark mode follows the system setting. */
@media (prefers-color-scheme: dark) {
  :root {
    --canvas: #12151c; --card: #1c202b; --ink: #e6e8ee; --muted: #9aa0ad; --line: #2a2f3c; --surface: #242936;
    --dark: #323a4f; --purple-soft: #2c2650; --accent: #6baed6; --nodata: #3a4050; --stroke: #1c202b;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 24px rgba(0, 0, 0, .25);
    --lift: 0 1px 2px rgba(0, 0, 0, .35), 0 4px 14px rgba(0, 0, 0, .3);
    --badge-bg: #3d3514; --badge-ink: #f3d86b; --green-bg: #133a29; --green-ink: #7fd9a8; --purple-ink: #c2b4ff; --soft-hover: #383063; --focus: #a996ff;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html { scroll-behavior: smooth; scroll-padding-top: 24px; }
body { margin: 0; background: var(--canvas); color: var(--ink); font: 15px/1.55 var(--sans); -webkit-font-smoothing: antialiased; }
h1, h2, h3 { margin: 0; font-family: var(--display); font-weight: 500; line-height: 1.3; }
code { font-size: .86em; background: var(--surface); border: 1px solid var(--line); padding: .1em .4em; border-radius: 6px; }
.muted { color: var(--muted); margin: 0; }

/* rail */
.rail { position: fixed; left: 0; top: 0; bottom: 0; width: 84px; z-index: 20; background: var(--card); border-radius: 0 20px 20px 0; box-shadow: var(--shadow); display: flex; flex-direction: column; align-items: center; padding: 20px 0; }
.rail-logo { width: 42px; height: 42px; display: block; }
.rail-logo svg { width: 100%; height: 100%; }
.rail-logo rect { fill: var(--dark); }
.rail-logo circle { fill: none; stroke: #6baed6; stroke-width: 2.4; }
.rail-logo path { fill: none; stroke: #ffd166; stroke-width: 2.2; stroke-linecap: round; }
.rail-nav { margin: auto 0; display: flex; flex-direction: column; gap: 10px; }
.rail-nav a, .rail-gh { width: 52px; height: 52px; border-radius: 14px; display: grid; place-items: center; color: var(--ink); transition: background .15s; }
.rail-nav a:hover { background: var(--surface); }
.rail-nav a.active { background: var(--dark); color: #fff; }
.rail-nav svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.rail-gh { background: var(--purple); color: #fff; }
.rail-gh:hover { background: var(--purple-dark); }
.rail-gh svg { width: 22px; height: 22px; fill: currentColor; }

main { margin-left: 84px; padding: 40px clamp(16px, 4vw, 56px) 32px; max-width: 1340px; }
main > section + section { margin-top: 28px; }
@media (max-width: 860px) {
  .rail { position: sticky; top: 0; bottom: auto; width: 100%; height: 64px; flex-direction: row; border-radius: 0 0 18px 18px; padding: 0 12px; }
  .rail-logo { width: 36px; height: 36px; }
  .rail-nav { margin: 0 auto; flex-direction: row; gap: 4px; }
  .rail-nav a, .rail-gh { width: 42px; height: 42px; border-radius: 11px; }
  main { margin-left: 0; padding-top: 24px; }
}

/* page head + search */
.page-head { display: flex; justify-content: space-between; align-items: center; gap: 16px; flex-wrap: wrap; margin-bottom: 28px; }
h1 { font-size: clamp(1.5rem, 3vw, 1.9rem); }
.sub { margin: 2px 0 0; color: var(--muted); }
.search { position: relative; width: min(340px, 100%); }
.search svg { position: absolute; left: 14px; top: 50%; width: 18px; height: 18px; transform: translateY(-50%); fill: none; stroke: var(--muted); stroke-width: 2; stroke-linecap: round; }
.search input { width: 100%; font: inherit; color: var(--ink); background: var(--card); border: 0; border-radius: 14px; padding: 12px 16px 12px 42px; box-shadow: var(--lift); }

/* cards */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 26px 30px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.card-head h2 { font-size: 1.2rem; }
.note { font-size: .8rem; color: var(--muted); margin: 12px 0 0; }
.body { margin: 0; max-width: 820px; }
@media (max-width: 720px) { .card { padding: 22px 20px; border-radius: 20px; } }

.badge { display: inline-block; background: var(--badge-bg); color: var(--badge-ink); font-size: .72rem; font-weight: 500; letter-spacing: .05em; text-transform: uppercase; padding: 3px 9px; border-radius: 6px; }
.badge.green { background: var(--green-bg); color: var(--green-ink); }
.badge.purple { background: var(--purple-soft); color: var(--purple-ink); }

/* buttons */
button { font: inherit; cursor: pointer; }
.btn { display: inline-flex; align-items: center; font-weight: 500; font-size: .9rem; padding: 10px 16px; border-radius: 12px; border: 0; }
a.btn, .rank-list a { text-decoration: none; }
.btn.soft { background: var(--purple-soft); color: var(--purple-ink); }
.btn.soft:hover { background: var(--soft-hover); }
.btn.soft[aria-pressed="true"] { background: var(--purple); color: #fff; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn { width: 42px; height: 42px; border-radius: 12px; border: 0; background: var(--card); display: grid; place-items: center; color: var(--ink); box-shadow: var(--lift); }
.icon-btn:hover { background: var(--surface); }
.icon-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.controls { display: flex; gap: 10px; }
button:focus-visible, input:focus-visible, summary:focus-visible, main a:focus-visible, .rail a:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

/* Skip link: first thing a keyboard user reaches, hidden until focused. */
.skip { position: absolute; left: 12px; top: -60px; z-index: 40; background: var(--dark); color: #fff; padding: 10px 16px; border-radius: 12px; font-weight: 500; text-decoration: none; }
.skip:focus { top: 12px; outline: 3px solid var(--focus); outline-offset: 2px; }
main:focus { outline: none; } /* focused only as the skip link's target */

/* Data-table fallback under the map and charts, collapsed by default */
.data-table { margin-top: 12px; }
.data-table summary { cursor: pointer; font-size: .88rem; font-weight: 500; color: var(--purple-ink); width: fit-content; border-radius: 6px; }
.data-table .table-wrap { margin-top: 10px; max-height: 360px; }
.data-table caption { text-align: left; font-size: .82rem; color: var(--muted); padding: 10px 14px 0; }
.data-table th { color: var(--muted); font-size: .8rem; font-weight: 600; padding: 10px 14px; }

/* stats */
.stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 20px; margin-bottom: 28px; }
.stats .card { padding: 20px 22px; display: flex; flex-direction: column; }
.stats .k { font-size: .82rem; color: var(--muted); }
.stats .v { font: 600 1.9rem/1.2 var(--display); margin: 6px 0 2px; font-variant-numeric: tabular-nums; }
.stats .u { font-size: .85rem; color: var(--muted); }
.highlight { background: linear-gradient(100deg, rgba(18,16,40,.82) 0%, rgba(40,28,80,.45) 100%), url(img/earth.webp) 30% center / cover, var(--dark); color: #fff; }
.highlight .k, .highlight .u { color: rgba(255,255,255,.9); }
@media (max-width: 1000px) { .stats { grid-template-columns: 1fr 1fr; } }

/* map + detail */
.layout { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 28px; align-items: start; }
@media (max-width: 960px) { .layout { grid-template-columns: 1fr; } }
.map-frame { background: var(--surface); border-radius: 16px; overflow: hidden; }
#map { width: 100%; height: auto; display: block; cursor: grab; }
#map:active { cursor: grabbing; }
#map:focus-visible { outline: 3px solid var(--focus); outline-offset: -3px; }
.country { stroke: var(--stroke); stroke-width: .4; cursor: pointer; vector-effect: non-scaling-stroke; }
.country:hover { stroke: var(--ink); stroke-width: 1.2; }
.country.selected { stroke: var(--selected); stroke-width: 2.2; }
/* Amber would vanish against Cividis yellow, so outline in ink instead. */
.cvd .country.selected { stroke: var(--ink); }
/* map filters: non-matching countries fade back */
.map-filters { margin: 0 0 8px; }
.map-filters .btn { flex: none; }
/* year slider under the map filters */
.year-row { display: flex; align-items: center; gap: 14px; margin: 0 0 8px; }
.year-row label { font-weight: 500; white-space: nowrap; }
.year-row label b { color: var(--purple-ink); font-variant-numeric: tabular-nums; }
.year-row input { flex: 1; min-width: 0; accent-color: var(--purple); cursor: pointer; }
.country { transition: opacity .2s; }
.country.dim { opacity: .18; }
.legend text { fill: var(--muted); font: 11px var(--sans); }

#detail h2 { font-size: 1.2rem; margin-bottom: 6px; }
#detail .big { font: 600 2.6rem/1.1 var(--display); color: var(--accent); margin: 14px 0 4px; font-variant-numeric: tabular-nums; }
#detail .bar { height: 10px; background: var(--surface); border-radius: 5px; overflow: hidden; margin: 12px 0; }
#detail .bar i { display: block; height: 100%; background: var(--accent); border-radius: 5px; }
#detail .region { margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--line); }
#detail .region .k { display: block; font-size: .82rem; color: var(--muted); margin-bottom: 4px; }
#detail .region b { font: 600 1.4rem/1.2 var(--display); margin-right: 8px; font-variant-numeric: tabular-nums; }
#detail .region .bar i { background: var(--muted); }

/* compare */
.cmp-pick { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin: 14px 0 18px; }
.cmp-pick select { flex: 1 1 200px; font: inherit; color: var(--ink); background: var(--card); border: 0; border-radius: 14px; padding: 11px 14px; box-shadow: var(--lift); cursor: pointer; }
.cmp-pick select:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.cmp { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 600px) { .cmp { grid-template-columns: 1fr; } }
.cmp-side { background: var(--surface); border-radius: 16px; padding: 18px 20px; }
.cmp-side h3 { font-size: 1.05rem; }
.cmp-side .big { font: 600 2.2rem/1.1 var(--display); color: var(--accent); margin: 10px 0 4px; font-variant-numeric: tabular-nums; }
.cmp-side .bar { height: 10px; background: var(--card); border-radius: 5px; overflow: hidden; margin: 10px 0; }
.cmp-side .bar i { display: block; height: 100%; background: var(--accent); border-radius: 5px; }
#cmpGap { margin-top: 14px; }
#detail .btn { margin-top: 16px; }

/* score distribution */
.dist { width: 100%; height: auto; display: block; overflow: visible; }
.dist-pick { flex: 0 1 260px; }
.dist-bar { fill: var(--accent); opacity: .55; transition: opacity .15s; }
.dist-bar:hover { opacity: .8; }
.dist-bar.on { opacity: 1; fill: var(--purple); }
.dist-axis text, .dist-label { fill: var(--muted); font: 12px var(--sans); }
.dist-axis line { stroke: var(--line); }
.dist-axis .domain { stroke: var(--line); }
.dist-mark line { stroke: var(--ink); stroke-width: 1.5; stroke-dasharray: 4 3; }
.dist-mark text { fill: var(--ink); font: 600 12px var(--sans); }
/* The chart scales down on phones, so its text is drawn larger to stay readable. */
@media (max-width: 640px) { .dist-axis text, .dist-mark text { font-size: 22px; } .dist-label { display: none; } }

/* 404 page */
.nf-list { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.nf-list a { display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; padding: 14px 16px; border-radius: 14px; background: var(--surface); color: var(--ink); text-decoration: none; height: 100%; }
.nf-list a:hover { background: var(--purple-soft); }
.nf-name { font: 500 1.05rem var(--display); }
.nf-desc { grid-column: 1 / -1; font-size: .88rem; color: var(--muted); }

/* country page */
.country-pick { font: inherit; color: var(--ink); background: var(--card); border: 0; border-radius: 14px; padding: 11px 14px; box-shadow: var(--lift); cursor: pointer; width: min(340px, 100%); }
.country-pick:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.country-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 28px; }
.rank-list li.current a { background: var(--purple-soft); }
.rank-list li.current .rank { background: var(--purple); }
.rank-list.scroll { max-height: 460px; overflow: auto; }

/* regions page */
.region-bars { margin: 0; padding: 0; list-style: none; display: grid; gap: 2px; }
.region-bar { width: 100%; display: grid; grid-template-columns: minmax(150px, 230px) 1fr auto; align-items: center; gap: 14px; border: 0; background: none; color: var(--ink); text-align: left; padding: 10px; border-radius: 12px; }
.region-bar:hover { background: var(--surface); }
.region-bar[aria-pressed="true"] { background: var(--purple-soft); }
.region-name { font-weight: 500; font-size: .92rem; }
.region-track { position: relative; height: 12px; background: var(--surface); border-radius: 6px; }
.region-bar:hover .region-track, .region-bar[aria-pressed="true"] .region-track { background: var(--card); }
.region-track .fill { position: absolute; inset: 0 auto 0 0; background: var(--accent); border-radius: 6px; }
.region-bar[aria-pressed="true"] .fill { background: var(--purple); }
.region-track .tick { position: absolute; top: -3px; bottom: -3px; width: 2px; margin-left: -1px; background: var(--ink); border-radius: 1px; }
.region-bar .score { background: var(--card); }
.region-big { font: 600 2.6rem/1.1 var(--display); color: var(--accent); margin: 14px 0 4px; font-variant-numeric: tabular-nums; }
.region-list { margin-top: 14px; }
@media (max-width: 640px) { .region-bar { grid-template-columns: 1fr auto; gap: 6px 10px; } .region-track { grid-column: 1 / -1; grid-row: 2; } }

/* game page */
.game-pair { display: grid; grid-template-columns: 1fr auto 1fr; align-items: stretch; gap: 14px; margin: 6px 0 16px; }
.game-vs { align-self: center; font: 600 .9rem var(--display); color: var(--muted); text-transform: uppercase; }
.game-pick { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; border: 2px solid transparent; background: var(--surface); color: var(--ink); text-align: left; padding: 20px 22px; border-radius: 18px; transition: background .15s, border-color .15s; }
.game-pick:not(:disabled):hover { background: var(--purple-soft); }
.game-pick:disabled { cursor: default; }
.game-name { font: 500 1.2rem/1.3 var(--display); }
.game-region { font-size: .85rem; }
.game-score { font: 600 2.2rem/1.1 var(--display); color: var(--accent); margin-top: 10px; font-variant-numeric: tabular-nums; }
.game-pick.right { border-color: var(--green-ink); background: var(--green-bg); }
.game-pick.wrong { border-color: var(--selected); }
.game-result { min-height: 1.55em; margin: 0 0 14px; }
.game-result a { color: var(--purple-ink); }
.game-stats { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin: 18px 0; }
.game-stats div { background: var(--surface); border-radius: 14px; padding: 12px 14px; }
.game-stats dt { font-size: .82rem; color: var(--muted); }
.game-stats dd { margin: 2px 0 0; font: 600 1.4rem/1.2 var(--display); font-variant-numeric: tabular-nums; }
@media (max-width: 600px) { .game-pair { grid-template-columns: 1fr; } .game-vs { justify-self: center; } }
.game-pair[hidden], .game-solo[hidden] { display: none; }
.btn[hidden] { display: none; }
.seg { display: inline-flex; flex-wrap: wrap; gap: 4px; padding: 4px; border-radius: 14px; background: var(--surface); }
.seg button { font: 500 .85rem var(--sans); padding: 7px 12px; border-radius: 10px; border: 0; background: transparent; color: var(--ink); cursor: pointer; }
.seg button:hover { background: var(--card); }
.seg button.on { background: var(--dark); color: #fff; }
.game-solo { display: grid; gap: 14px; margin: 6px 0 16px; }
.game-card { display: flex; flex-direction: column; gap: 4px; background: var(--surface); border-radius: 16px; padding: 18px 20px; }
.game-guess { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px; }
.game-guess label { font-weight: 500; font-size: .92rem; }
.game-guess output { font: 600 1.1rem var(--display); font-variant-numeric: tabular-nums; margin-left: 6px; }
.game-guess input { flex: 1 1 200px; accent-color: var(--purple); }

/* full rankings table */
.filters { display: flex; gap: 12px; flex-wrap: wrap; margin: 4px 0 12px; }
.filters .search { flex: 1 1 240px; width: auto; }
.filters select { flex: 1 1 200px; min-width: 0; font: inherit; color: var(--ink); background: var(--card); border: 0; border-radius: 14px; padding: 11px 14px; box-shadow: var(--lift); cursor: pointer; }
.filters select:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.table-count { font-size: .85rem; margin-bottom: 10px; }
.table-wrap { max-height: 620px; overflow: auto; border-radius: 16px; background: var(--surface); }
.rank-table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.rank-table thead th { position: sticky; top: 0; z-index: 1; background: var(--surface); text-align: left; padding: 0; }
.rank-table th button { display: inline-flex; align-items: center; gap: 4px; border: 0; background: none; color: var(--muted); font-size: .8rem; font-weight: 600; padding: 12px 14px; border-radius: 8px; }
.rank-table th button:hover { color: var(--ink); }
.rank-table th[aria-sort] button { color: var(--ink); }
.rank-table th[aria-sort="ascending"] button::after { content: "↑"; }
.rank-table th[aria-sort="descending"] button::after { content: "↓"; }
.rank-table td { padding: 8px 14px; border-top: 1px solid var(--line); }
.rank-table td a { color: var(--ink); font-weight: 500; text-decoration: none; border-radius: 4px; }
.rank-table td a:hover { color: var(--purple-ink); text-decoration: underline; }
.rank-table .num { text-align: right; white-space: nowrap; }
.rank-table thead .num { text-align: right; }
.rank-table .score { background: var(--card); }
@media (max-width: 640px) { .rank-table { font-size: .86rem; } .rank-table .col-region { display: none; } .rank-table td, .rank-table th button { padding-left: 5px; padding-right: 5px; } .rank-table .score { padding: 2px 6px; } .rank-table .badge { padding: 3px 5px; } }

/* rankings */
.pair { display: grid; grid-template-columns: 1fr 1fr; gap: 28px; }
@media (max-width: 760px) { .pair { grid-template-columns: 1fr; } }
.rank-list { margin: 0; padding: 0; list-style: none; counter-reset: rank; }
.rank-list li + li { border-top: 1px solid var(--line); }
.rank-list a { display: grid; grid-template-columns: 34px 1fr auto; align-items: center; gap: 12px; width: 100%; border: 0; background: none; padding: 10px 8px; border-radius: 12px; text-align: left; color: var(--ink); }
.rank-list a:hover, .rank-list a:focus-visible { background: var(--surface); }
.movers small { display: block; font-size: .8rem; font-variant-numeric: tabular-nums; }
.movers-pick { align-items: center; margin: 0 0 6px; }
.movers-pick label { font-family: var(--display); font-weight: 500; font-size: 1.15rem; }
.movers-pick select { flex: 0 0 auto; }
.rank { width: 30px; height: 30px; border-radius: 9px; background: var(--dark); color: #fff; display: grid; place-items: center; font-size: .82rem; font-weight: 600; }
.score { font-variant-numeric: tabular-nums; font-weight: 600; background: var(--surface); padding: 2px 10px; border-radius: 8px; font-size: .9rem; }

footer { margin-top: 36px; padding-top: 18px; border-top: 1px solid var(--line); display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; color: var(--muted); font-size: .8rem; }

#tip { position: fixed; pointer-events: none; background: var(--dark); color: #fff; padding: 7px 11px; border-radius: 10px; font-size: 13px; opacity: 0; z-index: 30; box-shadow: var(--lift); }
@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }

/* Printing: a light page with the data and none of the controls. */
@media print {
  :root {
    --canvas: #fff; --card: #fff; --ink: #262a35; --muted: #5f6472; --line: #e2e4ea; --surface: #f7f8fa;
    --dark: #1f2433; --purple-soft: #efebff; --accent: #08519c; --nodata: #dfe2e8; --stroke: #fff;
    --shadow: none; --lift: none;
    --badge-bg: #fbf2c0; --badge-ink: #6b5600; --green-bg: #d8f5e6; --green-ink: #0b6b3f; --purple-ink: #4a2fc9;
    color-scheme: light;
  }
  @page { margin: 14mm; }
  /* Keep map colours, bars and badges, which browsers drop from print by default. */
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  body { font-size: 11pt; }

  /* Navigation and controls do nothing on paper. */
  .rail, #tip, .skip, .data-table, .btn, .icon-btn, .controls, .search, .country-pick, .country-actions, #legend-note,
  input[type=range], .game-pick { display: none !important; }
  /* Pickers print as plain text showing their current choice. */
  select { appearance: none; -webkit-appearance: none; box-shadow: none !important; border: 1px solid var(--line) !important; background: none !important; }

  main { margin: 0; padding: 0; max-width: none; }
  .page-head { margin-bottom: 16px; }
  .card { border: 1px solid var(--line); padding: 18px 20px; border-radius: 14px; }
  .stats, .layout, .pair { gap: 14px; }
  .stats { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  main > section + section { margin-top: 16px; }
  .card, .stat, .cmp-side, .rank-list li, .rank-table tr { break-inside: avoid; }
  h1, h2, h3, .card-head { break-after: avoid; }

  /* Long lists and the full table print in full, with the table header repeated on each page. */
  .table-wrap, .rank-list.scroll { max-height: none; overflow: visible; }
  .rank-table thead th { position: static; }
  .rank-table thead { display: table-header-group; }
  .map-frame { background: none; }
  footer { margin-top: 18px; }
}
