* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --sidebar-width: 320px;
  --bg:            #0d1117;
  --surface:       #161b22;
  --border:        #30363d;
  --text:          #e6edf3;
  --text-muted:    #8b949e;
  --accent:        #2f9e44;
}

body {
  display: flex;
  height: 100vh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

/* ── Sidebar ────────────────────────────────────────────── */
#sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: 10;
}

#sidebar-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.brand {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 4px;
}

#sidebar-header h1 {
  font-size: 17px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 2px;
}

#sidebar-header p {
  font-size: 11px;
  color: var(--text-muted);
  line-height: 1.4;
}

/* ── Footer ─────────────────────────────────────────────── */
#sidebar-footer {
  flex-shrink: 0;
  padding: 10px 16px;
  font-size: 10px;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
  line-height: 1.4;
}

#sidebar-footer a { color: var(--accent); text-decoration: none; }
#sidebar-footer a:hover { text-decoration: underline; }

/* ── Map ────────────────────────────────────────────────── */
#map { flex: 1; position: relative; }

.sidebar-section-title {
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-bottom: 6px;
}

/* ── Overview row ───────────────────────────────────────── */
#sidebar-overview {
  display: flex;
  gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  align-items: stretch;
}

#minimap-wrap { flex-shrink: 0; }

#minimap {
  width: 110px;
  height: 110px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
}

#minimap .maplibregl-ctrl-bottom-left,
#minimap .maplibregl-ctrl-bottom-right,
#minimap .maplibregl-ctrl-top-left,
#minimap .maplibregl-ctrl-top-right { display: none; }

#jumpto-wrap { flex: 1; display: flex; flex-direction: column; }

#jumpto-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: repeat(3, 1fr);
  gap: 5px;
  flex: 1;
}

.jumpto-btn {
  padding: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-muted);
  font-size: 11px;
  text-align: center;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
  height: 100%;
}

.jumpto-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(47, 158, 68, 0.08);
}

/* ── Level switcher ─────────────────────────────────────── */
#sidebar-levels {
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
}

#level-list {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 5px;
}

.level-btn {
  padding: 5px 4px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-muted);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-align: center;
  cursor: pointer;
  transition: all 0.15s;
  text-transform: uppercase;
}

.level-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(47, 158, 68, 0.08);
}

.level-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}

/* ── Year slider ────────────────────────────────────────── */
#sidebar-year {
  padding: 8px 16px 10px 16px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  background: rgba(255,255,255,0.02);
}

.year-slider-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}

.year-slider-title {
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-muted);
}

#year-label {
  font-size: 11px;
  font-weight: 700;
  color: var(--accent);
}

#year-slider {
  width: 100%;
  -webkit-appearance: none;
  appearance: none;
  height: 3px;
  background: var(--border);
  border-radius: 2px;
  outline: none;
  cursor: pointer;
}

#year-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: 2px solid var(--surface);
  box-shadow: 0 0 0 1px var(--accent);
}

#year-slider::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: 2px solid var(--surface);
}

.year-range-labels {
  display: flex;
  justify-content: space-between;
  margin-top: 4px;
  font-size: 9px;
  color: var(--text-muted);
}

/* ── Variables + Year (integrated card) ─────────────────── */
#sidebar-vars-wrap {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  border-bottom: 1px solid var(--border);
}

#sidebar-vars {
  padding: 8px 16px 0 16px;
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

#var-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
  padding-bottom: 8px;
  -webkit-mask-image: linear-gradient(to bottom, black 70%, transparent 100%);
  mask-image: linear-gradient(to bottom, black 70%, transparent 100%);
}

.var-btn {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 6px 10px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  text-align: left;
  cursor: pointer;
  transition: all 0.15s;
  flex-shrink: 0;
  gap: 8px;
}

.var-btn:hover { background: var(--border); }
.var-btn.active { background: var(--accent); border-color: var(--accent); }

.var-btn-label {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.3;
  transition: color 0.15s;
  flex: 1;
}

.var-btn:hover .var-btn-label { color: var(--text); }
.var-btn.active .var-btn-label { color: #ffffff; }

.var-btn-years {
  font-size: 9px;
  color: var(--text-muted);
  background: rgba(255,255,255,0.06);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
  white-space: nowrap;
  flex-shrink: 0;
  transition: all 0.15s;
}

.var-btn.active .var-btn-years {
  color: rgba(255,255,255,0.7);
  background: rgba(255,255,255,0.15);
  border-color: rgba(255,255,255,0.2);
}

/* ── Featured ───────────────────────────────────────────── */
#sidebar-featured {
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

#featured-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2px 8px;
}

.featured-col-header {
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  padding: 0 4px 3px 4px;
  margin-bottom: 1px;
}

.featured-col-header.top { color: var(--accent); }
.featured-col-header.bot { color: #e05c5c; }

.featured-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
  padding: 2px 4px;
  border-radius: 5px;
  cursor: pointer;
  transition: background 0.12s;
}

.featured-item:hover { background: var(--border); }

.featured-rank {
  font-size: 8px;
  font-weight: 700;
  flex-shrink: 0;
  color: var(--text-muted);
}

.featured-name {
  flex: 1;
  font-size: 10px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.featured-value {
  font-size: 10px;
  font-weight: 700;
  color: var(--text-muted);
  flex-shrink: 0;
}

.featured-loading { font-size: 11px; color: var(--text-muted); padding: 4px 0; }

/* ── Tooltip ────────────────────────────────────────────── */
#tooltip {
  display: none;
  position: absolute;
  background: rgba(13, 17, 23, 0.93);
  border: 1px solid rgba(255,255,255,0.15);
  border-radius: 8px;
  padding: 10px 14px;
  pointer-events: none;
  z-index: 20;
  min-width: 160px;
  max-width: 220px;
}

.tooltip-name { font-size: 14px; font-weight: 700; color: #f1f5f9; margin-bottom: 4px; line-height: 1.3; }
.tooltip-id { font-size: 10px; font-weight: 400; color: var(--text-muted); }
.tooltip-value { font-size: 18px; font-weight: 700; color: #ffffff; line-height: 1.2; }
.tooltip-label { font-size: 10px; color: var(--text-muted); margin-top: 3px; text-transform: uppercase; letter-spacing: 0.05em; }

/* ── Legend ─────────────────────────────────────────────── */
#legend {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 380px;
  background: rgba(13, 17, 23, 0.93);
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 10px;
  padding: 10px 14px;
  z-index: 20;
  backdrop-filter: blur(6px);
  flex-direction: column;
  gap: 8px;
}

.legend-label {
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.legend-steps { display: flex; gap: 5px; }

.legend-step {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 5px 4px;
  cursor: pointer;
  transition: all 0.15s;
}

.legend-step:hover {
  border-color: rgba(255,255,255,0.2);
  background: rgba(255,255,255,0.05);
}

.legend-step.active {
  border-color: #ffffff;
  background: rgba(255,255,255,0.1);
}

.legend-swatch { width: 100%; height: 10px; border-radius: 3px; }

.legend-range {
  font-size: 9px;
  color: var(--text-muted);
  text-align: center;
  line-height: 1.3;
  white-space: nowrap;
}

.legend-step.active .legend-range { color: var(--text); }

.legend-reset {
  align-self: flex-end;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-muted);
  font-size: 9px;
  padding: 2px 8px;
  cursor: pointer;
  transition: all 0.15s;
}

.legend-reset:hover { border-color: #e05c5c; color: #e05c5c; }

/* ── Variable category headers (collapsible + sticky) ───── */
.var-category-wrap { display: flex; flex-direction: column; }

.var-category-header {
  display: flex;
  align-items: center;
  gap: 3px;
  padding: 6px 10px;
  background: var(--surface);
  border: none;
  border-radius: 6px;
  cursor: pointer;
  width: 100%;
  text-align: left;
  transition: background 0.12s;
  position: sticky;
  top: 0;
  z-index: 2;
}

.var-category-header:hover { background: var(--border); }

.var-category-icon {
  width: 11px;
  height: 11px;
  max-width: 11px;
  max-height: 11px;
  filter: brightness(0) invert(0.45);
  flex-shrink: 0;
  object-fit: contain;
  display: block;
}

.var-category-label {
  flex: 1;
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.var-category-chevron {
  font-size: 7px;
  color: var(--text-muted);
  transition: transform 0.2s;
}

.var-category-header.open .var-category-chevron { transform: rotate(90deg); }
.var-category-header.open .var-category-icon { filter: brightness(0) invert(0.65); }
.var-category-header.open .var-category-label { color: var(--text); }

.var-category-header.has-active { background: rgba(47, 158, 68, 0.12); }
.var-category-header.has-active .var-category-label { color: var(--accent); }
.var-category-header.has-active .var-category-icon { filter: brightness(0) saturate(100%) invert(48%) sepia(60%) saturate(400%) hue-rotate(100deg); }
.var-category-header.has-active .var-category-chevron { color: var(--accent); }

.var-category-body {
  flex-direction: column;
  gap: 2px;
  padding: 0 0 4px 0;
}






/* ════════════════════════════════════════════════════════════
   REPORT PANEL — right side, below legend
   ════════════════════════════════════════════════════════════ */

#detail-panel {
  position: absolute;
  top: 120px;
  right: 16px;
  width: 380px;
  height: calc(100vh - 150px);
  padding: 0;
  display: flex;
  flex-direction: column;
  background: rgba(10, 13, 18, 0.97);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 12px;
  z-index: 25;
  backdrop-filter: blur(14px);
  box-shadow: -2px 4px 24px rgba(0,0,0,0.5);
  overflow: hidden;
}

/* ── Header: single compact line ────────────────────────────── */
#detail-header {
  display: flex;
  align-items: stretch;
  gap: 0;
  flex-shrink: 0;
  border-bottom: 1px solid var(--border);
  background: rgba(16, 20, 27, 0.98);
}

#detail-shape-map {
  width: 90px;
  min-width: 90px;
  border-right: 1px solid var(--border);
  border-radius: 12px 0 0 0;
  overflow: hidden;
}

#detail-shape-map .maplibregl-ctrl-bottom-left,
#detail-shape-map .maplibregl-ctrl-bottom-right,
#detail-shape-map .maplibregl-ctrl-top-left,
#detail-shape-map .maplibregl-ctrl-top-right,
#detail-shape-map .maplibregl-ctrl-attrib { display: none !important; }

#detail-header-right {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  gap: 0;
}

#detail-header-top {
  display: flex;
  align-items: center;
  gap: 0;
  padding: 8px 10px 6px 12px;
  min-width: 0;
}

#detail-header-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 2px;
  min-width: 0;
}

#detail-title {
  font-size: 17px;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  letter-spacing: -0.01em;
  line-height: 1.2;
}

/* Remove old title-row rule — replaced by detail-meta-row */
#detail-title-row { display: contents; }

#detail-meta-row {
  display: flex;
  align-items: baseline;
  gap: 6px;
  min-width: 0;
}

#detail-subtitle {
  font-size: 10px;
  font-family: 'Space Mono', monospace;
  letter-spacing: 0.05em;
  color: var(--accent);
  white-space: nowrap;
  flex-shrink: 0;
}

#detail-parent {
  font-size: 10px;
  color: var(--accent);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#detail-close {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 5px;
  color: var(--text-muted);
  font-size: 10px;
  cursor: pointer;
  padding: 3px 7px;
  flex-shrink: 0;
  transition: all 0.15s;
  line-height: 1;
  margin-left: 6px;
}
#detail-close:hover { border-color: #e05c5c; color: #e05c5c; }

/* ── KPI strip — 6 items, 2 rows of 3 ──────────────────────── */
#detail-kpi-strip {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.kpi-item {
  padding: 5px 10px;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 1px;
}
/* Remove bottom border on last row (items 4,5,6) */
.kpi-item:nth-child(n+4) { border-bottom: none; }
/* Remove right border on every 3rd item */
.kpi-item:nth-child(3n) { border-right: none; }

.kpi-label {
  font-size: 8px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
  white-space: nowrap;
}

.kpi-value {
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.1;
  white-space: nowrap;
}

/* ── Section nav tabs ───────────────────────────────────────── */
#detail-nav {
  display: flex;
  flex-shrink: 0;
  border-bottom: 1px solid var(--border);
  background: rgba(13,17,23,0.9);
  overflow-x: auto;
  scrollbar-width: none;
}
#detail-nav::-webkit-scrollbar { display: none; }

#detail-nav {
  justify-content: center;
}

.detail-nav-tab {
  flex: 1;
  padding: 6px 4px;
  font-size: 9px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  text-align: center;
  white-space: nowrap;
  transition: color 0.15s, border-color 0.15s;
  background: none;
  border-top: none;
  border-left: none;
  border-right: none;
}
.detail-nav-tab:hover  { color: var(--text); }
.detail-nav-tab.active { color: var(--accent); border-bottom-color: var(--accent); }

/* ── Scrollable body ────────────────────────────────────────── */
#detail-body {
  flex: 1;
  display: flex;
  flex-direction: row;
  overflow-x: auto;
  overflow-y: hidden;
  scroll-snap-type: x mandatory;
  scrollbar-width: none;
  scroll-behavior: smooth;
  min-height: 0;
}
#detail-body::-webkit-scrollbar { display: none; }

/* Each section is a fixed-size horizontal slot */
.report-section {
  min-width: 100%;
  width: 100%;
  height: 100%;
  overflow: hidden;
  scroll-snap-align: start;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
}
.report-section-title { display: none; }

/* Inner scroll container — this is what actually scrolls vertically */
.section-scroll {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 14px 16px 40px 16px;
}
.section-scroll::-webkit-scrollbar { width: 3px; }
.section-scroll::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* ── Stat card ──────────────────────────────────────────────── */
.wiki-card { margin-bottom: 10px; }
.wiki-card-label {
  font-size: 8px; font-weight: 600; letter-spacing: 0.09em;
  text-transform: uppercase; color: var(--text-muted); margin-bottom: 2px;
}
.wiki-card-value {
  font-size: 18px; font-weight: 700; color: var(--text);
  line-height: 1.1; letter-spacing: -0.02em;
}

/* ── Sparkline ──────────────────────────────────────────────── */
.spark-wrap { margin-top: 3px; }

.spark-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1px;
}

.spark-years-left {
  font-size: 7px;
  color: var(--text-muted);
}

.spark-avg {
  font-size: 9px;
  color: #6b7280;
  flex: 1;
  text-align: center;
  letter-spacing: 0.02em;
}

.spark-trend {
  font-size: 10px;
  font-weight: 700;
  text-align: right;
}

.trend-up { color: #38b86e; }
.trend-dn { color: #e05c5c; }

.spark-footer {
  display: flex;
  justify-content: flex-end;
  margin-top: 1px;
}

.spark-year-end {
  font-size: 7px;
  color: var(--text-muted);
}

/* ── Mini stat grid ─────────────────────────────────────────── */
.mini-grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-bottom: 10px; }
.mini-grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 6px; margin-bottom: 10px; }
.mini-stat { background: rgba(255,255,255,0.03); border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px; }
.mini-label { font-size: 7px; font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase; color: var(--text-muted); margin-bottom: 2px; }
.mini-value { font-size: 12px; font-weight: 700; color: var(--text); }

/* ── Horizontal bar ─────────────────────────────────────────── */
.hbar-section-title { font-size: 8px; font-weight: 700; letter-spacing: 0.1em; text-transform: uppercase; color: var(--text-muted); margin-bottom: 5px; margin-top: 10px; }
.hbar-section-title:first-child { margin-top: 0; }
.hbar-row { display: flex; align-items: center; gap: 6px; margin-bottom: 4px; }
.hbar-row:last-child { margin-bottom: 0; }
.hbar-label { font-size: 10px; color: var(--text-muted); width: 72px; flex-shrink: 0; }
.hbar-track { flex: 1; height: 5px; background: var(--border); border-radius: 3px; overflow: hidden; }
.hbar-fill  { height: 100%; border-radius: 3px; }
.hbar-value { font-size: 10px; font-weight: 700; color: var(--text-muted); width: 36px; text-align: right; flex-shrink: 0; }

/* ── Education ladder ───────────────────────────────────────── */
.edu-ladder-wrap { margin-bottom: 10px; }
.edu-bar { display: flex; height: 10px; border-radius: 5px; overflow: hidden; margin-bottom: 6px; gap: 1px; }
.edu-seg { height: 100%; }
.edu-legend { display: grid; grid-template-columns: 1fr 1fr; gap: 3px 8px; }
.edu-leg-item { display: flex; align-items: center; gap: 4px; font-size: 9px; color: var(--text-muted); }
.edu-leg-dot  { width: 6px; height: 6px; border-radius: 2px; flex-shrink: 0; }

/* ── Age pyramid ────────────────────────────────────────────── */
.detail-pyramid-wrap { margin-top: 6px; }
.detail-section-label { font-size: 8px; font-weight: 700; letter-spacing: 0.12em; text-transform: uppercase; color: var(--text-muted); margin-bottom: 3px; }
.pyramid-legend { display: flex; gap: 12px; margin-bottom: 3px; font-size: 9px; }
.pyramid-legend-m { color: #6baed6; }
.pyramid-legend-f { color: #f768a1; }

/* ── Age structure bars ─────────────────────────────────────── */
.age-structure-wrap { margin-bottom: 10px; }
.age-structure-title {
  font-size: 9px; font-weight: 700; letter-spacing: 0.12em;
  text-transform: uppercase; color: var(--text-muted); margin-bottom: 8px;
}
.age-band-row { display: flex; align-items: center; gap: 7px; margin-bottom: 5px; }
.age-band-row:last-child { margin-bottom: 0; }
.age-band-label { font-size: 10px; color: var(--text-muted); width: 56px; flex-shrink: 0; }
.age-band-track { flex: 1; height: 8px; background: var(--border); border-radius: 4px; overflow: hidden; }
.age-band-fill  { height: 100%; border-radius: 4px; }
.age-band-value { font-size: 11px; font-weight: 700; color: var(--text); width: 38px; text-align: right; flex-shrink: 0; }