@import url('https://fonts.googleapis.com/css2?family=DM+Sans:wght@300;400;500;600&family=DM+Serif+Display&family=DM+Mono:wght@400&display=swap');

:root {
  --bg:      #f6f4ef;
  --surface: #ffffff;
  --border:  #e3dfd7;
  --text:    #1a1a1a;
  --muted:   #757067;
  --accent:  #b03a2e;          /* warm red, since CDI is car-themed */
  --accent-soft: rgba(176, 58, 46, .08);
  --pt:      #1f4e8c;          /* deep blue for PT-favoured */
  --balanced:#b8b3a8;          /* warm grey */
  --car:     #b03a2e;          /* warm red for car-dependent */
  --pt-soft: rgba(31, 78, 140, .12);
  --car-soft:rgba(176, 58, 46, .12);
  --font-head: 'DM Serif Display', Georgia, serif;
  --font-body: 'DM Sans', system-ui, sans-serif;
  --font-mono: 'DM Mono', monospace;
  --sidebar-w: 260px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { height: 100%; }
body {
  height: 100%; margin: 0; background: var(--bg); color: var(--text); font-family: var(--font-body);
  overflow: hidden; display: flex; flex-direction: column;
  position: fixed; width: 100%; touch-action: manipulation;
}

/* ═══════════════════════════════════════ HEADER */
header {
  display: flex; align-items: center; gap: 12px;
  padding: 0 20px; height: 52px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  box-shadow: 0 1px 3px rgba(0,0,0,.05); position: sticky; top: 0; z-index: 20;
  flex-shrink: 0;
}
header h1 { font-family: var(--font-head); font-size: 1.05rem; letter-spacing: .01em; }
header h1 span { color: var(--accent); }
#title-link { color: var(--text); text-decoration: none; display: inline-flex; align-items: center; gap: 6px; }
#title-link img { height: 22px; width: 22px; border-radius: 4px; }
#title-link:hover { opacity: .75; }
#header-sub { font-size: .78rem; color: var(--muted); border-left: 1px solid var(--border); padding-left: 12px; }
#back-btn {
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px;
  color: var(--text); font-family: var(--font-body); font-size: .8rem;
  padding: 4px 12px; cursor: pointer; display: none;
}
#back-btn:hover { background: var(--bg); }
.header-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.header-link {
  font-size: .78rem; color: var(--accent); text-decoration: none;
  background: none; border: none; cursor: pointer;
  font-family: var(--font-body); padding: 4px 8px; border-radius: 4px; transition: background .12s;
}
.header-link:hover { background: var(--accent-soft); }
.lang-toggle {
  background: var(--surface); border: 1.5px solid var(--accent); border-radius: 5px;
  color: var(--accent); font-family: var(--font-body); font-size: .72rem; font-weight: 600;
  padding: 3px 8px; cursor: pointer; letter-spacing: .04em; transition: background .12s, color .12s; line-height: 1.3;
}
.lang-toggle:hover { background: var(--accent); color: #fff; }

/* ═══════════════════════════════════════ APP SHELL */
#app { flex: 1; min-height: 0; position: relative; overflow: hidden; }

/* ═══════════════════════════════════════ EDGE TOGGLE BUTTONS */
.edge-toggle {
  position: absolute; z-index: 1100;
  top: 50%; transform: translateY(-50%);
  width: 20px; height: 48px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 0 6px 6px 0;
  color: var(--muted); font-size: .9rem;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  box-shadow: 2px 0 6px rgba(0,0,0,.06); transition: background .12s, color .12s;
  padding: 0; line-height: 1;
}
.edge-toggle:hover { background: var(--bg); color: var(--text); }

.collapse-btn {
  position: absolute; top: 10px; right: 10px; z-index: 10;
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px;
  color: var(--muted); font-size: .85rem; width: 30px; height: 30px;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: background .12s; box-shadow: 0 1px 4px rgba(0,0,0,.08);
}
.collapse-btn:hover { background: var(--bg); color: var(--text); }
.edge-toggle.collapsed { border-radius: 0 6px 6px 0; }

#landing-side-toggle { left: 300px; }
#view-landing.side-closed #landing-side { width: 0; overflow: hidden; padding: 0; border: none; }
#view-landing.side-closed { grid-template-columns: 0px 1fr; }
#view-landing.side-closed #landing-side-toggle { left: 0; }

#city-side-toggle { left: var(--sidebar-w); }
#view-city.side-closed #city-sidebar { width: 0; overflow: hidden; padding: 0; border: none; min-width: 0; }
#view-city.side-closed #city-side-toggle { left: 0; }
#view-city.side-closed { --sidebar-w: 0px; }

/* ═══════════════════════════════════════ LANDING VIEW */
#view-landing { display: grid; grid-template-columns: 300px 1fr; height: 100%; position: relative; }
#landing-side {
  background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 14px 12px; gap: 10px; overflow: hidden; position: relative;
}
.panel-title { font-size: .62rem; letter-spacing: .12em; text-transform: uppercase; color: var(--muted); margin-bottom: 4px; }
.search-row { display: flex; gap: 6px; }
.search-row input {
  flex: 1; padding: 6px 10px; border: 1px solid var(--border); border-radius: 6px;
  font-family: var(--font-body); font-size: .82rem; color: var(--text); background: var(--bg); outline: none;
}
.search-row input:focus { border-color: var(--accent); }
#city-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 5px; }
.city-card {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 10px; background: var(--bg); border: 1px solid var(--border); border-radius: 7px;
  cursor: pointer; transition: background .12s, border-color .12s; flex-shrink: 0;
}
.city-card:hover { background: #fbeae6; border-color: #f0c4bd; }
.city-card.disabled { opacity: .5; cursor: not-allowed; }
.city-card.disabled:hover { background: var(--bg); border-color: var(--border); }
.city-card strong { font-size: .85rem; }
.city-card .arrow { color: var(--muted); font-size: .9rem; }
.city-card .badge { font-size: .55rem; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.landing-hint {
  font-size: .72rem; color: var(--muted); line-height: 1.5;
  border-top: 1px solid var(--border); padding-top: 10px; margin-top: 2px;
}
.landing-hint strong { color: var(--text); }
.landing-hint a { color: var(--accent); text-decoration: none; }
.landing-hint a:hover { text-decoration: underline; }
#landing-map { width: 100%; height: 100%; position: relative; }
.info-box {
  position: absolute; top: 14px; left: 14px; z-index: 1000;
  max-width: 300px; padding: 10px 12px;
  background: rgba(255,255,255,.93); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 4px 14px rgba(0,0,0,.08); font-size: .82rem; line-height: 1.45; backdrop-filter: blur(4px);
}
.info-box b { display: block; font-size: .88rem; margin-bottom: 4px; color: var(--text); }
.info-box-cite { margin-top: 8px; padding-top: 6px; border-top: 1px solid var(--border); font-size: .7rem; color: var(--muted); }
.info-box-cite a { color: var(--accent); text-decoration: none; }
.info-box-cite a:hover { text-decoration: underline; }
.info-box-close {
  position: absolute; top: 6px; right: 6px; background: none; border: none; color: var(--muted);
  font-size: .85rem; cursor: pointer; width: 22px; height: 22px;
  display: flex; align-items: center; justify-content: center; border-radius: 4px;
}
.info-box-close:hover { background: rgba(0,0,0,.06); color: var(--text); }

.mobile-search-btn {
  position: absolute; top: 14px; right: 14px; z-index: 1200;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  color: var(--text); font-family: var(--font-body); font-size: .78rem; font-weight: 500;
  padding: 8px 14px; cursor: pointer; display: none; align-items: center; gap: 6px;
  box-shadow: 0 2px 10px rgba(0,0,0,.12); transition: background .12s;
}
.mobile-search-btn:hover { background: var(--bg); }
.mobile-search-btn svg { flex-shrink: 0; }
.mobile-search-btn.hidden-btn { display: none !important; }

.expand-btn {
  position: absolute; top: 14px; left: 14px; z-index: 1001;
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px;
  color: var(--muted); font-size: .85rem; font-weight: 600; width: 34px; height: 34px;
  cursor: pointer; display: none; align-items: center; justify-content: center;
  box-shadow: 0 2px 8px rgba(0,0,0,.1); transition: background .12s;
}
.expand-btn:hover { background: var(--bg); color: var(--text); }
@media (max-width: 1024px) {
  #expand-info { top: 60px !important; }
}

/* ═══════════════════════════════════════ CITY VIEW */
#view-city { display: none; height: 100%; position: relative; }

@media (min-width: 1025px) {
  #view-city.active {
    display: grid;
    grid-template-columns: var(--sidebar-w) var(--map-fr, 1fr) auto var(--scatter-fr, 1fr);
    grid-template-rows: 1fr;
  }
  #mobile-tabs { display: none; }
}

#city-sidebar {
  background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 14px 12px; gap: 16px; overflow-y: auto;
}

/* CDI legend (continuous gradient) */
.cdi-legend {
  display: grid; grid-template-columns: 18px 30px 1fr; gap: 8px; align-items: stretch;
  height: 130px; margin-top: 4px;
}
.cdi-legend-bar {
  width: 100%; border-radius: 4px; border: 1px solid var(--border);
  background: linear-gradient(
    to bottom,
    #67000d 0%,    /* +1 deep red (car) */
    #cb181d 15%,
    #ef6548 30%,
    #fdd8c8 45%,
    #f7f7f7 50%,   /* 0 white */
    #d4d4f0 55%,
    #6794d3 70%,
    #2867a8 85%,
    #08306b 100%   /* -1 deep blue (PT) */
  );
}
.cdi-legend-labels {
  display: flex; flex-direction: column; justify-content: space-between;
  font-family: var(--font-mono); font-size: .7rem; color: var(--muted); align-items: center;
}
.cdi-legend-labels span:nth-child(2) { transform: translateY(-1px); }
.cdi-legend-anno {
  display: flex; flex-direction: column; justify-content: space-between;
  font-size: .68rem; color: var(--muted); line-height: 1.2;
}
.cdi-legend-anno-top { color: var(--car); font-weight: 500; }
.cdi-legend-anno-mid { color: var(--muted); font-style: italic; transform: translateY(-1px); }
.cdi-legend-anno-bot { color: var(--pt); font-weight: 500; }

.legend-explain { font-size: .68rem; color: var(--muted); line-height: 1.4; margin-top: 6px; }
.legend-explain a { color: var(--accent); text-decoration: none; }
.legend-explain a:hover { text-decoration: underline; }

/* CDI range filter */
.range-control { display: flex; flex-direction: column; gap: 6px; }
.range-slider-wrap {
  position: relative; height: 26px;
}
.range-track {
  position: absolute; top: 11px; left: 0; right: 0; height: 4px;
  background: linear-gradient(to right, #08306b, #f7f7f7 50%, #67000d);
  border-radius: 2px; pointer-events: none;
  opacity: .55;
}
.range-fill {
  position: absolute; top: 0; height: 4px; border-radius: 2px;
  background: rgba(0, 0, 0, 0); pointer-events: none;
  box-shadow: inset 0 0 0 2px rgba(0, 0, 0, .25);
}
.range-slider-wrap input[type=range] {
  position: absolute; top: 0; left: 0; width: 100%; height: 26px; margin: 0;
  -webkit-appearance: none; appearance: none; background: none; pointer-events: none; outline: none;
}
.range-slider-wrap input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; pointer-events: auto; cursor: grab;
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--surface); border: 2px solid var(--accent);
  box-shadow: 0 1px 4px rgba(0,0,0,.18); margin-top: 0;
}
.range-slider-wrap input[type=range]::-moz-range-thumb {
  pointer-events: auto; cursor: grab;
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--surface); border: 2px solid var(--accent);
  box-shadow: 0 1px 4px rgba(0,0,0,.18);
}
.range-slider-wrap input[type=range]:active::-webkit-slider-thumb { cursor: grabbing; }
.range-readout {
  display: flex; align-items: center; justify-content: space-between;
  font-family: var(--font-mono); font-size: .72rem; color: var(--text);
}
.range-readout-sep { color: var(--muted); }
.range-reset {
  font-size: .68rem; color: var(--accent); background: none; border: none; cursor: pointer;
  text-align: left; padding: 0; font-family: var(--font-body);
}
.range-reset:hover { text-decoration: underline; }

/* Stat rows and clickable hints */
.stat-row { display: flex; justify-content: space-between; align-items: baseline; font-size: .75rem; margin-bottom: 4px; }
.stat-label { color: var(--muted); }
.stat-value { font-family: var(--font-mono); font-size: .78rem; }
.stat-hint-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 15px; height: 15px; border-radius: 50%; border: 1px solid var(--border);
  background: var(--surface); color: var(--accent); font-size: .6rem; font-weight: 500;
  cursor: pointer; margin-left: 3px; vertical-align: middle; transition: background .12s;
  padding: 0; line-height: 1;
}
.stat-hint-btn:hover { background: var(--accent-soft); border-color: var(--accent); }

.stat-popover {
  position: fixed; z-index: 3000; max-width: 220px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px;
  padding: 8px 10px; font-size: .72rem; line-height: 1.45; color: #555;
  box-shadow: 0 4px 12px rgba(0,0,0,.12); display: none;
}
.stat-popover.open { display: block; }

#info-box {
  background: rgba(176, 58, 46, .04); border: 1px solid rgba(176, 58, 46, .15);
  border-radius: 8px; padding: 10px 12px; font-size: .75rem; line-height: 1.5; min-height: 100px;
}
#info-box .info-title { font-family: var(--font-mono); font-size: .7rem; color: var(--accent); margin-bottom: 5px; }
#info-box .info-row { display: flex; justify-content: space-between; margin-bottom: 2px; }
#info-box .info-key { color: var(--muted); }
#info-box .info-val { font-family: var(--font-mono); }
#info-box .no-sel { color: var(--muted); font-style: italic; }
#info-box .info-desc { font-size: .68rem; color: #666; line-height: 1.4; margin: 2px 0 6px; padding-bottom: 5px; border-bottom: 1px solid rgba(0,0,0,.06); }
#info-box .info-row-muted { opacity: .55; font-size: .68rem; }
#info-box .info-cdi-bar {
  display: block; height: 6px; border-radius: 3px; background: var(--bg);
  margin: 6px 0 8px; position: relative; overflow: hidden;
  background: linear-gradient(to right, #08306b, #f7f7f7 50%, #67000d);
}
#info-box .info-cdi-marker {
  position: absolute; top: -3px; width: 2px; height: 12px; background: #111;
  border-radius: 1px;
}
.sidebar-hint { font-size: .72rem; color: var(--muted); line-height: 1.5; }

/* Map panel */
#map-panel { position: relative; overflow: hidden; border-right: 1px solid var(--border); min-width: 100px; }
#city-map { position: absolute; inset: 0; z-index: 0; }
#map-canvas { position: absolute; inset: 0; z-index: 1; width: 100%; height: 100%; pointer-events: none; }

#resize-handle {
  width: 8px; cursor: col-resize; background: var(--surface);
  border-left: 1px solid var(--border); border-right: 1px solid var(--border);
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
  transition: background .12s; z-index: 5;
}
#resize-handle:hover, #resize-handle.dragging { background: var(--accent-soft); }
.resize-grip { width: 3px; height: 24px; border-radius: 2px; background: var(--border); transition: background .12s; }
#resize-handle:hover .resize-grip { background: var(--accent); }

#scatter-panel { position: relative; overflow: hidden; background: var(--bg); min-width: 100px; }
#scatter-svg { width: 100%; height: 100%; display: block; }

.panel-label {
  position: absolute; top: 10px; left: 12px; z-index: 10;
  font-size: .6rem; letter-spacing: .1em; text-transform: uppercase; color: #666;
  background: rgba(255,255,255,.85); padding: 2px 7px; border-radius: 4px;
  pointer-events: none; display: flex; align-items: center; gap: 6px;
}
.panel-label .help-btn { pointer-events: auto; }
.help-btn {
  width: 18px; height: 18px; border-radius: 50%;
  border: 1px solid var(--border); background: var(--surface);
  color: var(--accent); font-size: .65rem; font-weight: 500;
  cursor: pointer; display: inline-flex; align-items: center; justify-content: center;
  transition: background .12s, border-color .12s; line-height: 1;
}
.help-btn:hover { background: var(--accent-soft); border-color: var(--accent); }

/* ═══════════════════════════════════════ SHARED */
.leaflet-control-attribution { font-size: 9px !important; opacity: .6; }
#tooltip {
  position: fixed; background: var(--surface); border: 1px solid var(--border);
  border-radius: 6px; padding: 7px 11px; font-size: .72rem;
  pointer-events: none; opacity: 0; transition: opacity .12s;
  z-index: 2000; line-height: 1.55; max-width: 240px; box-shadow: 0 4px 12px rgba(0,0,0,.1);
}
#tooltip.visible { opacity: 1; }
#tooltip strong { display: block; font-family: var(--font-mono); margin-bottom: 1px; }
#tooltip .tt-cdi-strong { color: var(--accent); }
#tooltip .tt-desc { display: block; font-size: .64rem; color: #888; margin-bottom: 4px; padding-bottom: 3px; border-bottom: 1px solid var(--border); font-family: var(--font-body); line-height: 1.35; }
#loading {
  position: fixed; inset: 0; background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  font-family: var(--font-mono); font-size: .85rem; color: var(--muted); z-index: 9999; transition: opacity .4s;
}
#loading.fade { opacity: 0; pointer-events: none; }

/* ═══════════════════════════════════════ MODALS */
.modal-overlay {
  position: fixed; inset: 0; z-index: 5000;
  background: rgba(0,0,0,.35); backdrop-filter: blur(3px);
  display: none; align-items: center; justify-content: center; padding: 24px;
}
.modal-overlay.open { display: flex; }
.modal {
  background: var(--surface); border-radius: 12px; box-shadow: 0 20px 60px rgba(0,0,0,.18);
  max-width: 620px; width: 100%; max-height: calc(100vh - 48px); overflow-y: auto;
  padding: 32px 36px; position: relative; animation: modalIn .2s ease-out;
}
.modal-sm { max-width: 480px; }
@keyframes modalIn { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: translateY(0); } }
.modal-close {
  position: absolute; top: 14px; right: 16px; background: none; border: none;
  font-size: 1.5rem; color: var(--muted); cursor: pointer; width: 32px; height: 32px;
  display: flex; align-items: center; justify-content: center; border-radius: 6px; transition: background .12s;
}
.modal-close:hover { background: rgba(0,0,0,.06); }
.modal h2 { font-family: var(--font-head); font-size: 1.35rem; margin-bottom: 2px; color: var(--text); }
.modal-subtitle { font-size: .82rem; color: var(--accent); margin-bottom: 16px; font-weight: 500; letter-spacing: .02em; }
.modal h3 { font-family: var(--font-head); font-size: 1rem; margin-top: 20px; margin-bottom: 8px; color: var(--text); }
.modal p { font-size: .84rem; line-height: 1.6; color: #444; margin-bottom: 8px; }
.modal ul { font-size: .84rem; line-height: 1.6; color: #444; margin: 0 0 8px 20px; }
.modal a { color: var(--accent); text-decoration: none; }
.modal a:hover { text-decoration: underline; }
.formula-box {
  font-family: var(--font-mono); font-size: .9rem;
  background: var(--bg); border: 1px solid var(--border); border-radius: 6px;
  padding: 10px 14px; margin: 8px 0; text-align: center; color: var(--text);
}
.dim-card { padding: 12px 14px; border-radius: 8px; font-size: .82rem; line-height: 1.55; margin-bottom: 8px; border-left: 4px solid; }
.dim-pt        { background: var(--pt-soft);  border-color: var(--pt);  color: #14365e; }
.dim-balanced  { background: rgba(0,0,0,.04); border-color: #999;       color: #555; }
.dim-car       { background: var(--car-soft); border-color: var(--car); color: #722520; }
.modal-footer { margin-top: 20px; padding-top: 14px; border-top: 1px solid var(--border); }
.modal-footer p { font-size: .76rem; color: var(--muted); line-height: 1.55; }

/* CDI-scale figurine inside the map-help modal */
.cdi-scale-explain { margin: 10px 0 12px; }
.cdi-scale-bar {
  width: 100%; height: 14px; border-radius: 4px; border: 1px solid var(--border);
  background: linear-gradient(to right, #08306b, #6794d3, #f7f7f7 50%, #ef6548, #67000d);
}
.cdi-scale-labels {
  display: flex; justify-content: space-between; margin-top: 4px;
  font-size: .7rem; color: var(--muted);
}

/* ═══════════════════════════════════════ MOBILE TABS */
#mobile-tabs { display: none; flex-shrink: 0; }
.mobile-tab {
  flex: 1; padding: 8px 4px; background: var(--surface); border: none;
  border-bottom: 2px solid transparent; font-family: var(--font-body); font-size: .78rem;
  color: var(--muted); cursor: pointer; transition: color .12s, border-color .12s; text-align: center;
}
.mobile-tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 500; }
.mobile-tab:hover { color: var(--text); }

/* ═══════════════════════════════════════ RESPONSIVE ≤1024 */
@media (max-width: 1024px) {
  header { padding: 0 10px; gap: 8px; height: 46px; }
  header h1 { font-size: .9rem; }
  #header-sub { display: none; }
  #back-btn { display: none !important; }
  .header-right { gap: 4px; }
  .header-link { font-size: .7rem; padding: 3px 5px; }
  .lang-toggle { font-size: .65rem; padding: 2px 6px; }
  #view-landing { grid-template-columns: 1fr !important; grid-template-rows: 1fr; }
  #landing-side { display: none !important; }
  #landing-side-toggle { display: none !important; }
  #view-landing.mobile-side-open #landing-side {
    display: flex !important; position: absolute; inset: 0; z-index: 1100;
    width: 100%; height: 100%; background: var(--surface);
  }
  .mobile-search-btn { display: flex !important; }
  #mobile-landing-close { display: flex !important; }
  #expand-info { display: none; }
  .info-box { max-width: 220px; font-size: .75rem; padding: 8px 10px; }
  #view-city.active { display: flex; flex-direction: column; height: 100%; }
  #mobile-tabs { display: flex; border-bottom: 1px solid var(--border); background: var(--surface); flex-shrink: 0; }
  #city-sidebar { border-right: none; flex: 1; min-height: 0; overflow-y: auto; }
  #city-side-toggle { display: none; }
  #resize-handle { display: none; }
  #map-panel, #scatter-panel { flex: 1; min-height: 0; }
  #scatter-panel { min-height: 300px; }
  .modal { padding: 20px 18px; max-width: 100%; }
  .modal h2 { font-size: 1.15rem; }
}
@media (max-width: 480px) {
  header h1 { font-size: .82rem; }
  .header-link { font-size: .65rem; }
  #repo-link, #paper-link { display: none; }
  .info-box { max-width: 180px; font-size: .7rem; }
  .mobile-tab { font-size: .72rem; padding: 7px 2px; }
}

/* ═══════════════════════════════════════ STATS VIEW */
#view-stats {
  height: 100%; overflow-y: auto; background: var(--bg);
  flex: 1; min-height: 0;
}
.stats-container {
  max-width: 980px; margin: 0 auto; padding: 24px 20px 48px;
}
.stats-header { margin-bottom: 24px; }
.stats-header h2 { font-family: var(--font-head); font-size: 1.3rem; color: var(--text); margin-bottom: 2px; }
.stats-subtitle { font-size: .82rem; color: var(--muted); margin-bottom: 14px; }
.stats-controls {
  display: flex; align-items: center; gap: 8px; font-size: .8rem; color: var(--muted);
}
.stats-controls select {
  padding: 5px 10px; border: 1px solid var(--border); border-radius: 6px;
  font-family: var(--font-body); font-size: .8rem; color: var(--text);
  background: var(--surface); cursor: pointer;
}
.stats-loading {
  text-align: center; padding: 60px 20px;
  font-family: var(--font-mono); font-size: .85rem; color: var(--muted);
}
.stats-chart-section { margin-bottom: 32px; }
.stats-chart-section h3 {
  font-family: var(--font-head); font-size: 1rem; color: var(--text);
  margin-bottom: 6px;
}
.stats-section-hint { font-size: .74rem; color: var(--muted); line-height: 1.5; margin-bottom: 14px; }

/* CDI bar chart (diverging) */
.cdi-bar-row {
  display: flex; align-items: center; margin-bottom: 5px; cursor: pointer;
  border-radius: 4px; padding: 2px 0; transition: background .1s;
}
.cdi-bar-row:hover { background: rgba(0,0,0,.03); }
.cdi-bar-label {
  width: 110px; flex-shrink: 0; font-size: .74rem; color: var(--text);
  text-align: right; padding-right: 10px; font-weight: 500;
}
.cdi-bar-track {
  flex: 1; position: relative; height: 18px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 3px; overflow: visible;
}
.cdi-bar-zero {
  position: absolute; left: 50%; top: -2px; bottom: -2px; width: 1px;
  background: rgba(0,0,0,.25); pointer-events: none;
}
.cdi-bar-fill {
  position: absolute; top: 0; bottom: 0;
  border-radius: 2px;
}
.cdi-bar-pct {
  width: 56px; flex-shrink: 0; font-size: .68rem; color: var(--muted);
  text-align: right; padding-left: 8px; font-family: var(--font-mono);
}

.alpha-slider {
  width: 100%; height: 4px; -webkit-appearance: none; appearance: none;
  background: var(--border); border-radius: 2px; outline: none; cursor: pointer;
}
.alpha-slider::-webkit-slider-thumb {
  -webkit-appearance: none; width: 14px; height: 14px;
  background: var(--accent); border-radius: 50%; border: none; cursor: pointer;
}
.alpha-slider::-moz-range-thumb {
  width: 14px; height: 14px;
  background: var(--accent); border-radius: 50%; border: none; cursor: pointer;
}
.map-alpha-control {
  position: absolute; bottom: 14px; left: 14px; z-index: 10;
  background: rgba(255,255,255,.9); border: 1px solid var(--border); border-radius: 6px;
  padding: 6px 10px; display: flex; align-items: center; gap: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,.08); backdrop-filter: blur(4px);
  font-size: .68rem; color: var(--muted);
}
.map-alpha-control .alpha-slider { width: 80px; }

/* View toggle button: mirror alpha-control's pill aesthetic, top-right of map */
.view-toggle-btn {
  position: absolute; top: 10px; right: 12px; z-index: 10;
  background: rgba(255,255,255,.92); border: 1px solid var(--border);
  border-radius: 999px; padding: 6px 12px 6px 10px;
  display: inline-flex; align-items: center; gap: 6px;
  font-family: inherit; font-size: .72rem; font-weight: 500;
  color: var(--text); cursor: pointer;
  box-shadow: 0 2px 8px rgba(0,0,0,.08); backdrop-filter: blur(4px);
  transition: background .12s, border-color .12s, color .12s;
}
.view-toggle-btn:hover:not(:disabled) {
  background: var(--accent); color: #fff; border-color: var(--accent);
}
.view-toggle-btn:disabled {
  opacity: .45; cursor: not-allowed;
}
.view-toggle-btn .view-toggle-icon {
  font-size: .85rem; line-height: 1; transform: translateY(-.5px);
}
.view-toggle-btn[data-mode="map"]       .when-cartogram { display: none; }
.view-toggle-btn[data-mode="cartogram"]  .when-map      { display: none; }
#map-loading {
  position: absolute; inset: 0; z-index: 20;
  display: flex; flex-direction: column; gap: 12px;
  align-items: center; justify-content: center;
  background: rgba(255, 255, 255, .55);
  backdrop-filter: blur(2px);
  pointer-events: none;
  font-size: .8rem; color: var(--muted); letter-spacing: .04em;
}
.map-loading-spinner {
  width: 32px; height: 32px;
  border: 3px solid rgba(0, 0, 0, .12);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: map-spin .8s linear infinite;
}
@keyframes map-spin {
  to { transform: rotate(360deg); }
}
.view-mode-badge {
  font-size: .58rem; letter-spacing: .08em; text-transform: uppercase;
  background: var(--accent-soft); color: var(--accent);
  padding: 1px 6px; border-radius: 3px; font-weight: 600;
}

/* City scatter & CDF */
#stats-city-scatter, #stats-cdf {
  width: 100%; height: 340px; display: block;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
}
#stats-cdf { height: 280px; }

/* Table */
.stats-table-wrap {
  overflow-x: auto; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface);
}
#stats-table { width: 100%; border-collapse: collapse; font-size: .75rem; }
#stats-table th {
  background: var(--bg); font-size: .65rem; text-transform: uppercase;
  letter-spacing: .06em; color: var(--muted); font-weight: 500;
  padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--border);
  position: sticky; top: 0;
}
#stats-table td {
  padding: 7px 10px; border-bottom: 1px solid rgba(0,0,0,.04);
  font-family: var(--font-mono); font-size: .73rem;
}
#stats-table tr:hover td { background: var(--accent-soft); }
#stats-table .city-name-cell {
  font-family: var(--font-body); font-weight: 500; color: var(--accent);
  cursor: pointer;
}
#stats-table .city-name-cell:hover { text-decoration: underline; }

@media (max-width: 1024px) {
  .stats-container { padding: 16px 12px 36px; }
  .stats-header h2 { font-size: 1.1rem; }
  .cdi-bar-label { width: 80px; font-size: .65rem; }
  #stats-city-scatter { height: 260px; }
  #stats-cdf { height: 240px; }

  .view-toggle-btn {
    top: 8px; right: 8px;
    padding: 5px 10px 5px 8px; font-size: .65rem; gap: 4px;
  }
  .view-mode-badge { font-size: .54rem; padding: 1px 4px; }
}
