/* ============================================================
   Country Comparison Atlas — design system.
   Reference: National Geographic, CIA World Factbook, Our World in
   Data, GIS. Two-sided comparison encoded in color throughout:
   Side A = deep teal (ocean), Side B = terracotta (land/relief).
   ============================================================ */
:root {
  --paper: #f7f3ea;
  --panel: #ffffff;
  --panel-alt: #f0ebdd;
  --ink: #1a2620;
  --ink-dim: #5c6b5f;
  --line: #d8d0ba;

  --side-a: #0b6e73;
  --side-a-soft: #d8ecec;
  --side-b: #c97c4b;
  --side-b-soft: #f6e3d3;
  --accent-green: #4a7c59;
  --accent-warn: #b0413e;

  --font-display: "Archivo", "Barlow Condensed", sans-serif;
  --font-body: "Source Sans 3", -apple-system, sans-serif;
  --font-mono: "Space Mono", monospace;

  --radius: 6px;
  --shadow: 0 2px 10px rgba(26, 38, 32, 0.1);
}

:root[data-theme="dark"] {
  --paper: #0d1b1a;
  --panel: #132524;
  --panel-alt: #0f1e1d;
  --ink: #e7ede8;
  --ink-dim: #93a599;
  --line: #274441;
  --side-a: #3fb8bd;
  --side-a-soft: #143534;
  --side-b: #e0985f;
  --side-b-soft: #35251a;
  --accent-green: #7fbf8f;
  --shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #0d1b1a;
    --panel: #132524;
    --panel-alt: #0f1e1d;
    --ink: #e7ede8;
    --ink-dim: #93a599;
    --line: #274441;
    --side-a: #3fb8bd;
    --side-a-soft: #143534;
    --side-b: #e0985f;
    --side-b-soft: #35251a;
    --accent-green: #7fbf8f;
    --shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-body);
  line-height: 1.5;
}

.wrap { max-width: 1320px; margin: 0 auto; padding: 0 20px; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 60;
  background: color-mix(in srgb, var(--paper) 94%, transparent);
  backdrop-filter: blur(6px);
  border-bottom: 1px solid var(--line);
}
.topbar-inner {
  display: flex; align-items: center; gap: 18px;
  padding: 12px 20px; max-width: 1320px; margin: 0 auto;
}
.brand {
  display: flex; align-items: center; gap: 10px;
  font-family: var(--font-display); font-weight: 700;
  font-size: 1.2rem; letter-spacing: 0.01em;
  color: var(--ink); text-decoration: none; white-space: nowrap;
}
.brand .glyph { width: 26px; height: 26px; flex-shrink: 0; }
.cat-nav {
  display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none; flex: 1;
}
.cat-nav::-webkit-scrollbar { display: none; }
.cat-nav button {
  background: transparent; border: none; color: var(--ink-dim);
  font-family: var(--font-body); font-size: 0.76rem; font-weight: 600;
  letter-spacing: 0.02em; padding: 8px 10px; white-space: nowrap;
  border-bottom: 2px solid transparent; cursor: pointer;
}
.cat-nav button:hover { color: var(--ink); }
.cat-nav button.active { color: var(--ink); border-color: var(--accent-green); }
.theme-toggle {
  background: transparent; border: 1px solid var(--line); color: var(--ink);
  border-radius: var(--radius); padding: 6px 10px; font-family: var(--font-mono);
  font-size: 0.72rem; cursor: pointer; white-space: nowrap;
}

/* ---------- Hero ---------- */
.hero { padding: 36px 0 8px; text-align: center; }
.hero .eyebrow {
  font-family: var(--font-mono); font-size: 0.74rem; letter-spacing: 0.16em;
  text-transform: uppercase; color: var(--accent-green); margin-bottom: 8px;
}
.hero h1 { font-family: var(--font-display); font-weight: 700; font-size: clamp(1.8rem, 4.5vw, 2.7rem); margin: 0; }
.hero p { color: var(--ink-dim); max-width: 62ch; margin: 10px auto 0; font-size: 0.95rem; }

/* ---------- Selector panels ---------- */
.selector-row {
  display: grid; grid-template-columns: 1fr auto 1fr; gap: 16px;
  align-items: stretch; margin: 28px 0;
}
@media (max-width: 760px) {
  .selector-row { grid-template-columns: 1fr; }
  .vs-badge { display: none; }
}
.country-panel {
  background: var(--panel); border: 2px solid var(--line); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow); position: relative;
}
.country-panel.side-a { border-color: var(--side-a); }
.country-panel.side-b { border-color: var(--side-b); }
.panel-side-label {
  font-family: var(--font-mono); font-size: 0.68rem; letter-spacing: 0.12em;
  text-transform: uppercase; margin-bottom: 10px;
}
.side-a .panel-side-label { color: var(--side-a); }
.side-b .panel-side-label { color: var(--side-b); }

.search-box { position: relative; margin-bottom: 12px; }
.search-row { display: flex; gap: 6px; }
.search-box input {
  flex: 1; width: 100%; background: var(--panel-alt); border: 1px solid var(--line);
  border-radius: var(--radius); color: var(--ink); padding: 9px 12px;
  font-family: var(--font-body); font-size: 0.92rem;
}
.browse-btn {
  flex-shrink: 0; background: var(--panel-alt); border: 1px solid var(--line);
  border-radius: var(--radius); color: var(--ink-dim); padding: 9px 12px;
  cursor: pointer; font-size: 1rem; line-height: 1;
}
.browse-btn:hover { color: var(--ink); border-color: var(--accent-green); }
.search-results {
  position: absolute; top: 100%; left: 0; right: 0; z-index: 20;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  margin-top: 4px; max-height: 320px; overflow-y: auto; box-shadow: var(--shadow);
  display: none;
}
.search-results.open { display: block; }
.results-group-label {
  font-family: var(--font-mono); font-size: 0.64rem; letter-spacing: 0.1em;
  text-transform: uppercase; color: var(--accent-green); background: var(--panel-alt);
  padding: 6px 12px; position: sticky; top: 0;
}
.search-results button {
  display: flex; align-items: center; gap: 8px; width: 100%;
  background: transparent; border: none; text-align: left; padding: 8px 12px;
  font-family: var(--font-body); font-size: 0.86rem; color: var(--ink); cursor: pointer;
}
.search-results button:hover { background: var(--panel-alt); }
.search-results .flag { font-size: 1.1rem; }
.search-results .kind {
  margin-left: auto; font-family: var(--font-mono); font-size: 0.64rem;
  color: var(--ink-dim); text-transform: uppercase;
}

.country-card { display: flex; gap: 14px; align-items: flex-start; }
.country-card .flag-big { font-size: 3rem; line-height: 1; }
.country-card h2 { font-family: var(--font-display); font-weight: 700; font-size: 1.35rem; margin: 0 0 2px; }
.country-card .kind-tag {
  font-family: var(--font-mono); font-size: 0.68rem; color: var(--ink-dim);
  text-transform: uppercase; letter-spacing: 0.05em;
}
.country-card .basics { margin-top: 8px; display: grid; gap: 3px; font-size: 0.84rem; color: var(--ink-dim); }
.country-card .basics b { color: var(--ink); font-weight: 600; }

.vs-badge {
  align-self: center; font-family: var(--font-display); font-weight: 700;
  font-size: 1.1rem; color: var(--ink-dim); width: 44px; height: 44px;
  border-radius: 50%; border: 2px solid var(--line); display: flex;
  align-items: center; justify-content: center; background: var(--panel);
}

/* ---------- Relative scale strip ---------- */
.scale-strip {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px; margin: 8px 0 32px;
}
.scale-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px;
}
.scale-card .label {
  font-family: var(--font-mono); font-size: 0.66rem; letter-spacing: 0.1em;
  text-transform: uppercase; color: var(--accent-green); margin-bottom: 6px;
}
.scale-card .value { font-family: var(--font-display); font-weight: 700; font-size: 1.2rem; }
.scale-bar { display: flex; height: 10px; border-radius: 5px; overflow: hidden; margin-top: 8px; border: 1px solid var(--line); }
.scale-bar .a { background: var(--side-a); }
.scale-bar .b { background: var(--side-b); }

/* ---------- Category panels ---------- */
.category-panel { display: none; padding: 8px 0 48px; }
.category-panel.active { display: block; }
.category-head { display: flex; align-items: baseline; gap: 12px; margin-bottom: 6px; }
.category-head h2 { font-family: var(--font-display); font-weight: 700; font-size: 1.5rem; margin: 0; }
.category-sub { color: var(--ink-dim); font-size: 0.88rem; max-width: 70ch; margin: 0 0 20px; }

.stat-table { width: 100%; border-collapse: collapse; table-layout: fixed; }
.stat-row { border-bottom: 1px solid var(--line); }
.stat-row:hover { background: var(--panel-alt); }
.stat-row td { padding: 12px 10px; vertical-align: top; }
.stat-val { font-family: var(--font-mono); font-size: 0.95rem; white-space: normal; overflow-wrap: break-word; width: 37%; }
.stat-val.a { color: var(--side-a); text-align: right; }
.stat-val.b { color: var(--side-b); text-align: left; }
.stat-label {
  text-align: center; font-size: 0.8rem; color: var(--ink-dim);
  width: 26%;
}
.stat-label .name { color: var(--ink); font-weight: 600; display: block; }
.stat-label button.info-btn {
  background: none; border: none; color: var(--ink-dim); cursor: pointer;
  font-family: var(--font-mono); font-size: 0.68rem; text-decoration: underline;
  padding: 0; margin-top: 2px;
}
.stat-compare-bar { display: flex; height: 5px; margin-top: 6px; border-radius: 3px; overflow: hidden; }
.stat-compare-bar .a { background: var(--side-a); }
.stat-compare-bar .b { background: var(--side-b); }
.stat-na { color: var(--ink-dim); font-style: italic; font-family: var(--font-body); font-size: 0.82rem; }
.field-flag {
  font-family: var(--font-body); font-size: 0.74rem; font-style: italic;
  color: var(--accent-warn, #b0413e); white-space: normal; margin-top: 4px; line-height: 1.4;
}

.meta-note {
  font-size: 0.78rem; color: var(--ink-dim); background: var(--panel-alt);
  border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 14px; margin-top: 18px;
}

/* ---------- Info popover (data explorer) ---------- */
.info-popover {
  position: fixed; z-index: 80; background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px;
  max-width: 320px; font-size: 0.82rem; display: none;
}
.info-popover.open { display: block; }
.info-popover dl { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; }
.info-popover dt { font-family: var(--font-mono); font-size: 0.66rem; text-transform: uppercase; color: var(--accent-green); }
.info-popover dd { margin: 0; color: var(--ink); }
.info-popover .close-btn {
  position: absolute; top: 6px; right: 8px; background: none; border: none;
  color: var(--ink-dim); cursor: pointer; font-size: 1rem;
}

/* ---------- Charts / viz ---------- */
.viz-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 20px; margin-top: 20px; }
.viz-card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.viz-card h3 { font-family: var(--font-display); font-weight: 700; font-size: 1rem; margin: 0 0 10px; }
.viz-card svg { width: 100%; height: auto; display: block; }
.viz-caption { font-size: 0.78rem; color: var(--ink-dim); margin-top: 10px; }
.legend-row { display: flex; gap: 16px; font-size: 0.78rem; margin-top: 8px; }
.legend-row .dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 5px; }
.legend-row .a .dot { background: var(--side-a); }
.legend-row .b .dot { background: var(--side-b); }
.growth-controls { display: flex; flex-wrap: wrap; gap: 10px 20px; margin-bottom: 12px; }

/* ---------- Timeline (history category) ---------- */
.dual-timeline { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; }
@media (max-width: 700px) { .dual-timeline { grid-template-columns: 1fr; } }
.tl-col { border-left: 3px solid var(--line); padding-left: 16px; }
.tl-col.a { border-color: var(--side-a); }
.tl-col.b { border-color: var(--side-b); }
.tl-item { margin-bottom: 16px; }
.tl-item .yr { font-family: var(--font-mono); font-size: 0.78rem; font-weight: 700; }
.tl-col.a .yr { color: var(--side-a); }
.tl-col.b .yr { color: var(--side-b); }
.tl-item p { margin: 3px 0 0; font-size: 0.86rem; color: var(--ink-dim); }

/* ---------- footer ---------- */
footer { text-align: center; padding: 30px 20px 50px; color: var(--ink-dim); font-size: 0.78rem; }
footer a { color: var(--accent-green); }

@media (max-width: 640px) {
  .stat-label { font-size: 0.72rem; }
  .stat-val { font-size: 0.8rem; }
  .stat-row td { padding: 10px 6px; }
}

/* ---------- Borders & Neighbours (border graph, js/viz/borders.js) ---------- */
.borders { padding: 8px 0 40px; }
.b-stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 1px; background: var(--line); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; margin: 14px 0; }
.bs-cell { background: var(--panel); padding: 10px 14px; display: flex; flex-direction: column; min-width: 0; }
.bs-cell b { font-family: var(--font-mono); font-size: 1.35rem; color: var(--ink); }
.bs-cell span { font-size: 0.76rem; color: var(--ink-dim); }
.b-bar { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 8px 16px; margin-bottom: 10px; }
.b-bar .chip-row { margin-bottom: 0; align-items: center; }
.b-bar-k { font-family: var(--font-mono); font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--ink-dim); margin-right: 4px; }
.b-zoom { display: flex; align-items: center; gap: 6px; }
.b-zoom .chip { min-width: 44px; min-height: 36px; }
.bz-k { font-family: var(--font-mono); font-size: 0.72rem; color: var(--ink-dim); min-width: 42px; text-align: center; }
.b-main { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 16px; align-items: start; }
.b-frame { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
#borderGraph { display: block; width: 100%; height: auto; touch-action: pan-y; user-select: none; -webkit-user-select: none; }
.be { fill: none; stroke: var(--ink-dim); stroke-opacity: 0.55; stroke-linecap: round; transition: stroke-opacity .15s; }
.be-via-territory { stroke-dasharray: 5 4; }
.be-disputed { stroke-dasharray: 1.2 3.4; stroke: var(--accent-warn); stroke-opacity: 0.8; }
.be.hot { stroke: var(--ink); stroke-opacity: 1; }
.be.dim { stroke-opacity: 0.12; }
.be-route { fill: none; stroke: var(--accent-green); stroke-width: 4; stroke-linecap: round; stroke-opacity: 0.85; pointer-events: none; }
.bn { transition: opacity .15s; }
.bn.dim { opacity: 0.22; }
.bn-ring { fill: var(--panel-alt); stroke: var(--ink-dim); stroke-width: 0.8; }
.bn.is-focus .bn-ring { stroke: var(--ink); stroke-width: 2.4; }
.bn.is-a .bn-ring { stroke: var(--side-a); stroke-width: 3.4; }
.bn.is-b .bn-ring { stroke: var(--side-b); stroke-width: 3.4; }
.bn-label { font-family: var(--font-mono); font-size: 9px; text-anchor: middle; fill: var(--ink-dim); paint-order: stroke; stroke: var(--panel); stroke-width: 3px; pointer-events: none; }
.b-panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); font-size: 0.86rem; max-height: 560px; overflow: auto; }
.b-panel h4 { font-family: var(--font-display); font-size: 1.05rem; margin: 0 0 6px; }
.b-panel p { margin: 0 0 10px; color: var(--ink-dim); }
.bp-head { display: flex; align-items: center; gap: 10px; margin-bottom: 4px; }
.bp-head h4 { margin: 0; }
.bp-flag { width: 34px; height: 34px; border-radius: 50%; object-fit: cover; border: 1px solid var(--line); }
.bp-kind { font-family: var(--font-mono); font-size: 0.66rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--accent-green); margin-bottom: 4px; }
.bp-list { list-style: none; margin: 0 0 10px; padding: 0; }
.bp-list li { display: flex; flex-wrap: wrap; align-items: center; gap: 2px 8px; padding: 5px 0; border-top: 1px solid var(--line); }
.bp-km { font-family: var(--font-mono); font-size: 0.72rem; color: var(--ink-dim); margin-left: auto; }
.bp-tag { flex-basis: 100%; font-size: 0.72rem; color: var(--ink-dim); padding-left: 26px; }
.bp-n { display: inline-flex; align-items: center; gap: 6px; background: none; border: 0; padding: 4px 0; min-height: 32px; font: inherit; color: var(--ink); cursor: pointer; text-align: left; }
.bp-n:hover { text-decoration: underline; }
.bp-n img, .br-stop img { width: 18px; height: 18px; border-radius: 50%; object-fit: cover; border: 1px solid var(--line); flex-shrink: 0; }
.bp-ents { display: flex; flex-wrap: wrap; gap: 4px 12px; margin-bottom: 8px; }
.bp-src { font-size: 0.74rem; }
.bp-pick { display: flex; gap: 8px; }
.bp-side { flex: 1; min-height: 44px; border-radius: var(--radius); font: inherit; font-weight: 600; font-size: 0.8rem; cursor: pointer; background: var(--panel); }
.bp-side.a { border: 2px solid var(--side-a); color: var(--side-a); }
.bp-side.b { border: 2px solid var(--side-b); color: var(--side-b); }
.bl-legend { display: flex; flex-direction: column; gap: 4px; }
.bl-item { display: flex; align-items: center; gap: 8px; background: none; border: 1px solid transparent; border-radius: var(--radius); padding: 6px 6px; min-height: 36px; font: inherit; font-size: 0.8rem; color: var(--ink); cursor: pointer; text-align: left; }
.bl-item:not(.on) { opacity: 0.45; text-decoration: line-through; }
.bl-item svg { width: 34px; height: 10px; flex-shrink: 0; }
.bl-item .be { stroke-width: 2; }
.b-route { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; margin: 14px 0 6px; font-size: 0.88rem; min-height: 44px; }
.br-k { font-family: var(--font-mono); font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--accent-green); }
.br-t { color: var(--ink-dim); }
.br-chain { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; }
.br-stop { display: inline-flex; align-items: center; gap: 5px; background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 3px 10px 3px 4px; min-height: 32px; font: inherit; font-size: 0.82rem; color: var(--ink); cursor: pointer; }
.br-stop.a { border-color: var(--side-a); }
.br-stop.b { border-color: var(--side-b); }
.br-arrow { color: var(--ink-dim); }
.br-n { font-family: var(--font-mono); font-size: 0.72rem; color: var(--ink-dim); }
.b-h3 { font-family: var(--font-display); font-size: 1rem; margin: 22px 0 10px; }
.ba-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.ba-chip { background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 6px 12px; min-height: 36px; font: inherit; font-size: 0.8rem; color: var(--ink); cursor: pointer; }
.ba-chip:hover { border-color: var(--ink-dim); }
.ba-chip.on { background: var(--ink); color: var(--panel); border-color: var(--ink); }
.b-list { margin-top: 18px; }
.b-list summary { cursor: pointer; font-weight: 600; min-height: 44px; display: flex; align-items: center; }
.bt { width: 100%; border-collapse: collapse; font-size: 0.82rem; }
.bt th { text-align: left; font-family: var(--font-mono); font-size: 0.66rem; text-transform: uppercase; color: var(--ink-dim); padding: 6px 8px; border-bottom: 1px solid var(--line); }
.bt td { padding: 6px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
.bt-c { font-family: var(--font-mono); text-align: center; }
.bt-n { background: none; border: 0; padding: 0; font: inherit; font-weight: 600; color: var(--side-a); cursor: pointer; text-align: left; }
@media (max-width: 900px) {
  .b-main { grid-template-columns: minmax(0, 1fr); }
  .b-panel { max-height: none; }
}
@media (max-width: 640px) {
  .b-stats { grid-template-columns: 1fr 1fr; }
  .bt th:nth-child(2), .bt td:nth-child(2) { display: none; }
}
/* Shared chip buttons (also used by the Growth Over Time toggles, which had no style before). */
.chip-row { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 6px 12px; min-height: 36px; font-family: var(--font-body); font-size: 0.8rem; font-weight: 600; color: var(--ink); cursor: pointer; }
.chip:hover { border-color: var(--ink-dim); }
.chip.active { background: var(--ink); color: var(--panel); border-color: var(--ink); }
.b-how { margin: 0; font-size: 0.86rem; color: var(--ink-dim); }
.b-legend { display: flex; flex-wrap: wrap; gap: 6px 18px; margin-top: 8px; font-size: 0.78rem; color: var(--ink-dim); }
.b-legend span { display: inline-flex; align-items: center; gap: 6px; }
.b-legend svg { width: 34px; height: 10px; }
.b-legend .be { stroke-width: 2; }
.bp-head { justify-content: space-between; }
.bp-head h4 { flex: 1; }
.bp-close { background: none; border: 1px solid var(--line); border-radius: 50%; width: 36px; height: 36px; font-size: 1.1rem; line-height: 1; color: var(--ink-dim); cursor: pointer; flex-shrink: 0; }
.bp-close:hover { color: var(--ink); border-color: var(--ink-dim); }
