/* shadcn/ui neutral tokens (v4), vanilla CSS. Same base as chronomap. */
:root {
  --radius: 0.625rem;
  --background: oklch(1 0 0);
  --foreground: oklch(0.145 0 0);
  --card: oklch(1 0 0);
  --popover: oklch(1 0 0);
  --primary: oklch(0.205 0 0);
  --primary-foreground: oklch(0.985 0 0);
  --secondary: oklch(0.97 0 0);
  --muted: oklch(0.97 0 0);
  --muted-foreground: oklch(0.556 0 0);
  --accent: oklch(0.97 0 0);
  --border: oklch(0.922 0 0);
  --input: oklch(0.922 0 0);
  --ring: oklch(0.708 0 0);
  --highlight: oklch(0.646 0.222 41.116);
  --pos: oklch(0.55 0.13 160);
  --neg: oklch(0.58 0.2 25);
  --mark: #FFD400;
  --shadow-sm: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
  --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
  --shadow-xs: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  --font: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, monospace;
  color-scheme: light;
}
.dark {
  --background: oklch(0.145 0 0);
  --foreground: oklch(0.985 0 0);
  --card: oklch(0.205 0 0);
  --popover: oklch(0.205 0 0);
  --primary: oklch(0.922 0 0);
  --primary-foreground: oklch(0.205 0 0);
  --secondary: oklch(0.269 0 0);
  --muted: oklch(0.269 0 0);
  --muted-foreground: oklch(0.708 0 0);
  --accent: oklch(0.269 0 0);
  --border: oklch(1 0 0 / 10%);
  --input: oklch(1 0 0 / 15%);
  --ring: oklch(0.556 0 0);
  --highlight: oklch(0.769 0.188 70.08);
  --pos: oklch(0.72 0.14 160);
  --neg: oklch(0.7 0.19 25);
  --mark: #E6BF00;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font: 14px/1.43 var(--font);
  color: var(--foreground);
  background: var(--background);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; }
svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
[hidden] { display: none !important; }
:focus-visible { outline: none; box-shadow: 0 0 0 3px color-mix(in oklch, var(--ring) 50%, transparent); }

#map { position: fixed; inset: 0; background: var(--muted); }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) + 4px);
  box-shadow: var(--shadow-sm);
}

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 36px; padding: 0 16px; border-radius: calc(var(--radius) - 2px);
  font-weight: 500; white-space: nowrap; transition: background-color .15s, color .15s, opacity .15s;
}
.btn-icon { width: 36px; padding: 0; }
.btn-primary { background: var(--primary); color: var(--primary-foreground); box-shadow: var(--shadow-xs); }
.btn-primary:hover { background: color-mix(in oklch, var(--primary) 90%, transparent); }
.btn-ghost:hover { background: var(--accent); }
.btn-outline { border: 1px solid var(--border); background: var(--background); box-shadow: var(--shadow-xs); }
.btn-outline:hover { background: var(--accent); }
.btn-sm { height: 32px; padding: 0 12px; gap: 6px; font-size: 13px; }

/* Inputs */
.input {
  height: 36px; width: 100%; min-width: 0; padding: 0 12px;
  border: 1px solid var(--input); border-radius: calc(var(--radius) - 2px);
  background: transparent; box-shadow: var(--shadow-xs);
  transition: border-color .15s, box-shadow .15s;
}
.dark .input { background: color-mix(in oklch, var(--input) 30%, transparent); }
.input::placeholder { color: var(--muted-foreground); }
.input:focus-visible { border-color: var(--ring); box-shadow: 0 0 0 3px color-mix(in oklch, var(--ring) 50%, transparent); }
input[type="search"]::-webkit-search-cancel-button { display: none; }

/* Segmented control */
.segmented {
  display: inline-flex; height: 36px; padding: 3px; gap: 2px;
  background: var(--muted); border-radius: var(--radius); flex: none;
}
.segmented button {
  padding: 0 8px; border-radius: calc(var(--radius) - 2px); border: 1px solid transparent;
  font-size: 13px; font-weight: 500; color: var(--muted-foreground); transition: color .15s, background-color .15s;
}
.segmented button[aria-checked="true"] { background: var(--background); color: var(--foreground); box-shadow: var(--shadow-sm); }
.dark .segmented button[aria-checked="true"] { background: color-mix(in oklch, var(--input) 30%, transparent); border-color: var(--input); }

.badge {
  display: inline-flex; align-items: center; height: 22px; padding: 0 8px; gap: 4px;
  border: 1px solid var(--border); border-radius: calc(var(--radius) - 2px);
  font-size: 12px; font-weight: 500; white-space: nowrap;
}
.badge-secondary { background: var(--secondary); border-color: transparent; }
.skeleton { background: var(--accent); border-radius: calc(var(--radius) - 2px); animation: pulse 2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .5; } }

/* Panel */
.panel {
  position: fixed; top: 12px; left: 12px; width: 340px; max-height: calc(100dvh - 120px);
  display: flex; flex-direction: column; gap: 12px; padding: 16px; z-index: 2;
}
.panel-head { display: flex; align-items: center; justify-content: space-between; margin: -6px -6px -4px 0; }
.brand { font-size: 16px; font-weight: 600; letter-spacing: -0.01em; margin: 0; }
.field { position: relative; }
.field-icon { position: absolute; left: 11px; top: 10px; color: var(--muted-foreground); pointer-events: none; }
.search .input { padding-left: 34px; }
.results { position: absolute; top: 42px; left: 0; right: 0; padding: 4px; z-index: 5; box-shadow: var(--shadow-md); border-radius: calc(var(--radius) - 2px); max-height: 300px; overflow-y: auto; }
.results button, .menu-item {
  display: flex; flex-direction: column; align-items: flex-start; width: 100%; text-align: left;
  padding: 6px 8px; border-radius: calc(var(--radius) - 4px);
}
.results button:hover, .results button[aria-selected="true"], .menu-item:hover { background: var(--accent); }
.results small, .muted { color: var(--muted-foreground); font-size: 12px; }

.metric { display: flex; flex-direction: column; gap: 8px; }
.metric .segmented { display: flex; }
.metric .segmented button { flex: 1; padding: 0 4px; font-size: 12px; }
.legend { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 8px; font-size: 11px; color: var(--muted-foreground); font-variant-numeric: tabular-nums; }
.legend .bar { height: 8px; border-radius: 999px; }

/* Menu */
.menu-wrap { position: relative; }
.menu { position: absolute; right: 0; top: 40px; width: 236px; padding: 4px; z-index: 6; box-shadow: var(--shadow-md); border-radius: calc(var(--radius) - 2px); }
.menu-label { padding: 6px 8px 4px; font-size: 12px; color: var(--muted-foreground); font-weight: 500; }
.metric .menu-label { padding: 0; }
.menu .segmented { display: flex; margin: 0 4px 4px; }
.menu .segmented button { flex: 1; }
.menu-item { flex-direction: row; gap: 4px; }
.sep { height: 1px; background: var(--border); margin: 4px -4px; }
code { font-family: var(--mono); font-size: 12px; }

/* Time bar */
.timebar {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%);
  width: min(880px, calc(100vw - 24px)); padding: 10px 12px; z-index: 2;
  display: flex; align-items: center; gap: 8px;
}
/* With a country open on a wide screen, the bar sits between the panel and the sheet. */
@media (min-width: 1100px) {
  .sheet-open .timebar { left: 364px; right: 504px; transform: none; width: auto; }
  .sheet-open .ticks span:nth-child(even) { display: none; }
}
.timebar .i-pause { display: none; }
.timebar.playing .i-play { display: none; }
.timebar.playing .i-pause { display: block; }
.slider-wrap { flex: 1; position: relative; padding: 6px 8px 14px; min-width: 0; }
.year {
  min-width: 72px; text-align: right; font-size: 22px; font-weight: 600; letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums; padding-right: 4px;
}
.ticks { position: absolute; left: 16px; right: 16px; bottom: -2px; height: 14px; }
.ticks span { position: absolute; transform: translateX(-50%); font-size: 11px; color: var(--muted-foreground); font-variant-numeric: tabular-nums; white-space: nowrap; }

input[type="range"] {
  -webkit-appearance: none; appearance: none; width: 100%; height: 16px; margin: 0; background: transparent; display: block;
  --pct: 50%;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 6px; border-radius: 999px;
  background: linear-gradient(to right, var(--primary) var(--pct), var(--muted) var(--pct));
}
input[type="range"]::-moz-range-track { height: 6px; border-radius: 999px; background: var(--muted); }
input[type="range"]::-moz-range-progress { height: 6px; border-radius: 999px; background: var(--primary); }
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 16px; height: 16px; margin-top: -5px; border-radius: 999px;
  background: var(--background); border: 1px solid var(--primary); box-shadow: var(--shadow-sm); transition: box-shadow .15s;
}
input[type="range"]::-moz-range-thumb {
  width: 14px; height: 14px; border-radius: 999px; background: var(--background); border: 1px solid var(--primary); box-shadow: var(--shadow-sm);
}
input[type="range"]:hover::-webkit-slider-thumb, input[type="range"]:focus-visible::-webkit-slider-thumb { box-shadow: 0 0 0 4px color-mix(in oklch, var(--ring) 50%, transparent); }
input[type="range"]:focus-visible { box-shadow: none; }

/* Sheet: the country card */
.sheet {
  position: fixed; top: 12px; right: 12px; bottom: 12px; width: 480px; z-index: 3;
  overflow-y: auto; padding: 16px 20px; box-shadow: var(--shadow-md);
  animation: sheet-in .2s ease-out;
}
@keyframes sheet-in { from { transform: translateX(16px); opacity: 0; } }
.sheet-close { position: absolute; top: 10px; right: 10px; opacity: .7; }
.sheet-close:hover { opacity: 1; }
.sheet h2 { font-size: 20px; font-weight: 600; letter-spacing: -0.01em; line-height: 1.3; margin: 0 32px 2px 0; display: flex; align-items: center; gap: 8px; }
.sheet h2 .flag { font-size: 22px; line-height: 1; }
.sheet .meta { color: var(--muted-foreground); font-size: 13px; font-variant-numeric: tabular-nums; }
.sheet p { margin: 12px 0; line-height: 1.6; }
.sheet a { color: inherit; }
.sheet .src { color: var(--muted-foreground); font-size: 12px; margin-top: 16px; padding-top: 12px; border-top: 1px solid var(--border); line-height: 1.5; }
.sheet .src a { text-decoration: underline; text-underline-offset: 3px; }
.sheet .credits p { margin: 8px 0; font-size: 13px; }
.sheet .credits a { text-decoration: underline; text-underline-offset: 3px; }

.block { margin-top: 12px; padding-top: 10px; border-top: 1px solid var(--border); }
.block-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.block-head h3 { margin: 0; font-size: 12px; font-weight: 500; letter-spacing: .04em; text-transform: uppercase; color: var(--muted-foreground); }
.block-head .y { font-size: 12px; color: var(--muted-foreground); font-variant-numeric: tabular-nums; }
.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 6px 0 2px; }
.tile .v { font-size: 18px; font-weight: 600; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; line-height: 1.15; }
.tile .l { font-size: 12px; color: var(--muted-foreground); }
.tile .v.pos { color: var(--pos); }
.tile .v.neg { color: var(--neg); }
.tile .v .u { font-size: 13px; font-weight: 500; color: var(--muted-foreground); margin-left: 2px; }
.chart { display: block; width: 100%; height: auto; overflow: visible; margin-top: 4px; }
.chart text { font-family: var(--font); font-size: 10px; fill: var(--muted-foreground); stroke: none; font-variant-numeric: tabular-nums; }
.chart .axis line, .chart .zero { stroke: var(--border); stroke-width: 1; }
.chart .line { fill: none; stroke: var(--foreground); stroke-width: 1.5; }
.chart .area { fill: color-mix(in oklch, var(--foreground) 8%, transparent); stroke: none; }
.chart .bar.pos { fill: var(--pos); stroke: none; }
.chart .bar.neg { fill: var(--neg); stroke: none; }
.chart .bar { opacity: .55; }
.chart .bar.cur { opacity: 1; }
.chart .marker { stroke: var(--highlight); stroke-width: 1.5; stroke-dasharray: 3 3; }
.chart .dot { fill: var(--highlight); stroke: var(--card); stroke-width: 1.5; }
.chart .mlabel { fill: var(--foreground); font-weight: 600; font-size: 11px; }

/* Treemap, in the Ruth Richardson manner */
.tm-title { font-size: 17px; font-weight: 700; letter-spacing: -0.015em; line-height: 1.25; margin: 6px 0 4px; }
.tm-title mark { background: var(--mark); color: #111; padding: 0 4px; border-radius: 2px; }
.tm-sub { font-size: 11.5px; color: var(--muted-foreground); line-height: 1.4; margin: 0 0 6px; }
.treemap { display: block; width: 100%; height: auto; }
.treemap rect { stroke: var(--card); stroke-width: 2; }
.treemap text { font-family: var(--font); stroke: none; pointer-events: none; }
.treemap .n { font-size: 11px; font-weight: 500; }
.treemap .p { font-size: 15px; font-weight: 700; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.treemap .n.big { font-size: 15px; }
.treemap .p.big { font-size: 26px; }
.tm-note { font-size: 11px; color: var(--muted-foreground); margin: 8px 0 0; line-height: 1.45; }
.tm-empty { padding: 24px 0; color: var(--muted-foreground); font-size: 13px; }

/* Sankey */
.sankey { display: block; width: 100%; height: auto; }
.sankey text { font-family: var(--font); fill: var(--foreground); font-size: 10.5px; paint-order: stroke; stroke: var(--card); stroke-width: 3px; stroke-linejoin: round; }
.sankey tspan.nv { fill: var(--muted-foreground); font-variant-numeric: tabular-nums; }
.sankey path { mix-blend-mode: normal; }

/* Tooltip and toast */
.tip {
  position: fixed; z-index: 4; pointer-events: none; padding: 6px 10px; max-width: 280px;
  background: var(--primary); color: var(--primary-foreground); border-radius: calc(var(--radius) - 2px);
  font-size: 12px; line-height: 1.35; transform: translate(-50%, calc(-100% - 12px));
}
.tip small { opacity: .7; display: block; }
.toast {
  position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%); z-index: 7;
  padding: 10px 14px; background: var(--popover); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow-md); font-size: 13px;
}

/* MapLibre controls in shadcn clothes */
.maplibregl-ctrl-group { background: var(--card) !important; border: 1px solid var(--border); border-radius: calc(var(--radius) - 2px) !important; box-shadow: var(--shadow-sm) !important; overflow: hidden; }
.maplibregl-ctrl-group button + button { border-top: 1px solid var(--border) !important; }
.dark .maplibregl-ctrl-group button .maplibregl-ctrl-icon { filter: invert(1); }
.maplibregl-ctrl-attrib { background: color-mix(in oklch, var(--card) 80%, transparent) !important; color: var(--muted-foreground); font-size: 11px; }
.maplibregl-ctrl-attrib a { color: inherit !important; }
.maplibregl-ctrl-attrib-button { filter: none; }
.dark .maplibregl-ctrl-attrib-button { filter: invert(1); }
.maplibregl-ctrl-bottom-right { bottom: 96px; }
@media (min-width: 1100px) { .sheet-open .maplibregl-ctrl-bottom-right { right: 500px; bottom: 12px; } }
.maplibregl-ctrl-top-right { top: 4px; }

/* Phone */
@media (max-width: 720px) {
  .panel { top: 8px; left: 8px; right: 8px; width: auto; padding: 12px; gap: 10px; max-height: none; }
  .metric .segmented button { padding: 0 6px; }
  .timebar { bottom: max(8px, env(safe-area-inset-bottom)); flex-wrap: wrap; padding: 8px; gap: 4px 6px; }
  .timebar .slider-wrap { order: 5; flex-basis: 100%; }
  .timebar .year { margin-left: auto; font-size: 18px; }
  .sheet { top: auto; left: 8px; right: 8px; bottom: 100px; width: auto; max-height: 62dvh; padding: 14px 16px; animation-name: sheet-up; }
  .legend, #metric-note { display: none; }
  .sheet-open .metric { display: none; }
  @keyframes sheet-up { from { transform: translateY(16px); opacity: 0; } }
  .maplibregl-ctrl-bottom-right { bottom: 132px; }
  .toast { bottom: 140px; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
