:root {
  --bg: #0b0e14;
  --panel: #11151d;
  --panel-2: #161b25;
  --line: #232a37;
  --text: #e6edf3;
  --muted: #8b97a7;
  --accent: #38bdf8;
  --accent-2: #f59e0b;
  --watch: #ef4444;
  --radius: 10px;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.4);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
code { font-family: ui-monospace, "SF Mono", Menlo, monospace; color: var(--muted); }
.muted { color: var(--muted); font-weight: 400; }

/* ---------- header ---------- */
.app-header {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 19px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark { color: var(--accent); font-size: 20px; }
.brand-text h1 { font-size: 15px; margin: 0; letter-spacing: 0.2px; }
.brand-text .tagline { font-size: 11px; margin: 0; color: var(--muted); }
/* Crop selector — prominent segmented bar in the header (primary dimension) */
.crop-nav {
  display: flex; margin-left: auto; border: 1px solid var(--line);
  border-radius: 9px; overflow: hidden;
}
.crop-pill {
  background: var(--panel-2);
  border: none;
  border-left: 1px solid var(--line);
  color: var(--muted);
  padding: 9px 18px;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.12s;
}
.crop-pill:first-child { border-left: none; }
.crop-pill:hover { color: var(--text); background: var(--line); }
.crop-pill.active { background: var(--accent); color: #04222e; font-weight: 600; }

/* ---------- layout ---------- */
.layout { flex: 1; display: flex; min-height: 0; }

/* floating map-control card (replaces the old left sidebar) */
.map-controls {
  position: absolute; top: 12px; left: 12px; z-index: 3; width: 176px;
  background: rgba(17, 21, 29, 0.92); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 11px; backdrop-filter: blur(6px);
  display: flex; flex-direction: column; gap: 10px; box-shadow: var(--shadow);
}
.mc-block { display: flex; flex-direction: column; gap: 6px; }
.mc-label { font-size: 10px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--muted); }
.mc-row { display: flex; flex-direction: column; gap: 8px; }
.mc-field { flex: 1; display: flex; flex-direction: column; gap: 4px; font-size: 10px; text-transform: uppercase; letter-spacing: 0.4px; color: var(--muted); }
.mc-field select {
  appearance: none; -webkit-appearance: none;
  background-color: var(--panel-2); color: var(--text); border: 1px solid var(--line);
  background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="10" height="6"><path d="M0 0l5 6 5-6z" fill="%238b97a7"/></svg>');
  background-repeat: no-repeat; background-position: right 8px center;
  border-radius: 7px; padding: 6px 22px 6px 8px; font-size: 12px; text-transform: none; letter-spacing: 0;
  cursor: pointer; width: 100%;
}
.mc-field select:hover { border-color: var(--accent); }
.mc-desc { font-size: 11px; color: var(--muted); line-height: 1.4; border-top: 1px solid var(--line); padding-top: 8px; }

/* Vertical segmented controls (single-column control card) */
.segmented { display: flex; flex-direction: column; background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.seg-btn {
  background: transparent; border: none; border-bottom: 1px solid var(--line);
  color: var(--muted); padding: 7px 10px; font-size: 13px; cursor: pointer;
  transition: all 0.12s; text-align: center;
}
.seg-btn:last-child { border-bottom: none; }
.seg-btn:hover { color: var(--text); }
.seg-btn.active { background: var(--accent); color: #04222e; font-weight: 600; }

.state-filter { display: flex; flex-wrap: wrap; gap: 4px; max-height: 150px; overflow-y: auto; }
.state-chip {
  background: var(--panel-2); border: 1px solid var(--line); color: var(--muted);
  padding: 3px 8px; font-size: 11px; border-radius: 6px; cursor: pointer; font-family: ui-monospace, monospace;
}
.state-chip:hover { color: var(--text); }
.state-chip.active { background: var(--accent-2); border-color: var(--accent-2); color: #2a1a00; font-weight: 600; }

.advanced summary { font-size: 12px; color: var(--muted); cursor: pointer; padding: 4px 0; }
.radio-row { display: flex; align-items: flex-start; gap: 8px; padding: 6px; border-radius: 8px; font-size: 12px; cursor: pointer; color: var(--muted); }
.radio-row.active { color: var(--text); background: var(--panel-2); }
.radio-row input { margin-top: 2px; }

.vintage { border-top: 1px solid var(--line); padding-top: 14px; margin-top: auto; }
.vintage-row { display: flex; justify-content: space-between; font-size: 11px; padding: 2px 0; }

/* ---------- map ---------- */
.map-wrap { flex: 1; position: relative; min-width: 0; }
#map { position: absolute; inset: 0; }
.boot-error { padding: 40px; color: var(--watch); font-family: ui-monospace, monospace; }

.map-legend {
  position: absolute; bottom: 18px; right: 12px; z-index: 2;
  background: rgba(17, 21, 29, 0.92); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 10px 12px; font-size: 12px; min-width: 150px;
  backdrop-filter: blur(6px);
}
.legend-title { font-weight: 600; margin-bottom: 6px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--muted); }
.legend-bar { height: 10px; border-radius: 4px; }
.legend-scale { display: flex; justify-content: space-between; font-size: 10px; color: var(--muted); margin-top: 3px; }
.legend-row { display: flex; align-items: center; gap: 8px; padding: 2px 0; }
.legend-row .sw { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }

.map-caption {
  position: absolute; top: 12px; left: 12px; z-index: 2; max-width: 360px;
  background: rgba(17, 21, 29, 0.92); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 8px 12px; font-size: 12px; color: var(--muted);
  backdrop-filter: blur(6px);
}

/* ---------- timeline ---------- */
.timeline {
  position: absolute; bottom: 18px; left: 50%; transform: translateX(-50%); z-index: 3;
  display: flex; align-items: center; gap: 10px;
  background: rgba(17, 21, 29, 0.94); border: 1px solid var(--line);
  border-radius: 999px; padding: 6px 12px; backdrop-filter: blur(6px); box-shadow: var(--shadow);
}
.timeline:empty { display: none; }
.tl-presets { display: flex; gap: 0; border: 1px solid var(--line); border-radius: 7px; overflow: hidden; }
.tl-preset { background: var(--panel-2); border: none; color: var(--muted); font-size: 10px; padding: 4px 8px; cursor: pointer; white-space: nowrap; }
.tl-preset:hover { color: var(--text); }
.tl-preset.active { background: var(--accent); color: #04222e; font-weight: 600; }
.tl-btn {
  background: var(--accent); color: #04222e; border: none; width: 30px; height: 30px;
  border-radius: 50%; font-size: 13px; cursor: pointer; flex-shrink: 0;
}
.tl-rangewrap { position: relative; width: 260px; display: flex; align-items: center; }
.tl-range { width: 100%; accent-color: var(--accent); cursor: pointer; }
.tl-ticks { position: absolute; left: 8px; right: 8px; top: 50%; height: 0; pointer-events: none; }
.tl-tick { position: absolute; top: -4px; width: 1px; height: 5px; background: rgba(139, 151, 167, 0.3); }
.tl-tick.decade { top: -7px; height: 11px; background: rgba(139, 151, 167, 0.55); }
.tl-date { font-size: 12px; font-family: ui-monospace, monospace; min-width: 64px; text-align: center; }
.tl-exit {
  background: transparent; border: 1px solid var(--line); color: var(--muted);
  border-radius: 999px; font-size: 11px; padding: 3px 8px; cursor: pointer;
}
.tl-exit:hover { color: var(--text); border-color: var(--accent); }
.tl-tag { background: var(--accent-2); color: #2a1a00; font-size: 8px; padding: 1px 4px; border-radius: 3px; letter-spacing: 0.5px; vertical-align: middle; }

/* ---------- detail panel ---------- */
.detail-panel { border-bottom: 2px solid var(--accent); background: var(--panel-2); }
.dp-head { display: flex; justify-content: space-between; align-items: flex-start; padding: 12px 14px 8px; }
.dp-name { font-size: 14px; font-weight: 600; display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.dp-fips { font-size: 10px; color: var(--muted); font-family: ui-monospace, monospace; margin-top: 2px; }
.dp-flag { background: rgba(245,158,11,0.2); color: var(--accent-2); font-size: 9px; padding: 1px 5px; border-radius: 4px; }
.dp-close { background: transparent; border: none; color: var(--muted); font-size: 14px; cursor: pointer; }
.dp-close:hover { color: var(--text); }
.dp-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1px; background: var(--line); }
.dp-stat { background: var(--panel-2); padding: 7px 10px; }
.dp-stat span { display: block; font-size: 9px; text-transform: uppercase; letter-spacing: 0.4px; color: var(--muted); }
.dp-stat b { font-size: 14px; font-family: ui-monospace, monospace; }
.dp-note { font-size: 10px; color: var(--muted); padding: 6px 14px 0; }
.dp-sparks { display: flex; flex-direction: column; gap: 8px; padding: 10px 14px 14px; }
.dp-spark-label { font-size: 10px; text-transform: uppercase; letter-spacing: 0.4px; color: var(--muted); margin-bottom: 2px; }
.dp-breakdown { padding: 4px 14px 0; }
.bd-table { width: 100%; border-collapse: collapse; font-size: 11px; margin-top: 2px; }
.bd-table th { text-align: right; color: var(--muted); font-weight: 500; font-size: 9px; text-transform: uppercase; letter-spacing: 0.4px; padding: 2px 4px; border-bottom: 1px solid var(--line); }
.bd-table th:first-child { text-align: left; }
.bd-table td { text-align: right; padding: 3px 4px; font-family: ui-monospace, monospace; border-bottom: 1px solid rgba(35,42,55,0.5); }
.bd-table td:first-child { text-align: left; font-family: inherit; }

.spark { display: block; width: 100%; height: 42px; }
.spark-empty { font-size: 10px; color: var(--muted); font-style: italic; padding: 6px 0; }

/* ---------- analysis rail ---------- */
.analysis-rail {
  width: 424px;
  flex-shrink: 0;
  background: var(--panel);
  border-left: 1px solid var(--line);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}
.kpi-strip {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1px;
  background: var(--line);
  border-bottom: 1px solid var(--line);
}
.kpi { background: var(--panel); padding: 10px 12px; }
.kpi-label { font-size: 10px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--muted); }
.kpi-value { font-size: 18px; font-weight: 600; font-family: ui-monospace, monospace; margin-top: 2px; }
.kpi-delta { font-size: 11px; font-weight: 500; }
.kpi-delta.bad { color: var(--watch); }
.kpi-delta.good { color: #4ade80; }

.rail-section { padding: 14px 14px 6px; border-bottom: 1px solid var(--line); }
.rail-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.rail-title { font-size: 13px; margin: 0; }
.scatter-ctl { display: flex; gap: 0; border: 1px solid var(--line); border-radius: 7px; overflow: hidden; }
.mini-toggle { background: var(--panel-2); border: none; color: var(--muted); font-size: 10px; padding: 3px 8px; cursor: pointer; }
.mini-toggle.active { background: var(--accent); color: #04222e; font-weight: 600; }
.rail-title .muted { font-size: 11px; }
.rail-sub { font-size: 11px; color: var(--muted); margin: 4px 0 8px; }
.scatter { height: 300px; }
.empty { color: var(--muted); font-size: 12px; padding: 20px 4px; text-align: center; }

.watch-table { width: 100%; border-collapse: collapse; font-size: 11px; }
.watch-table th { text-align: left; color: var(--muted); font-weight: 500; padding: 4px 6px; border-bottom: 1px solid var(--line); text-transform: uppercase; font-size: 9px; letter-spacing: 0.4px; cursor: pointer; user-select: none; white-space: nowrap; }
.watch-table th:hover { color: var(--text); }
.watch-table th.sorted { color: var(--accent); }
.watch-table td { padding: 4px 6px; border-bottom: 1px solid rgba(35,42,55,0.5); font-family: ui-monospace, monospace; }
.watch-table td:first-child { font-family: inherit; }
.watch-table tr[data-fips] { cursor: pointer; }
.watch-table tr[data-fips]:hover { background: var(--panel-2); }
.watch-table tr.hot { background: rgba(245,158,11,0.14); }

/* ---------- tooltip ---------- */
.tooltip {
  position: absolute; z-index: 5; pointer-events: none;
  background: rgba(8, 11, 16, 0.96); border: 1px solid var(--accent);
  border-radius: 8px; padding: 8px 10px; font-size: 12px; min-width: 180px;
  box-shadow: var(--shadow);
}
.tip-head { font-weight: 600; margin-bottom: 6px; display: flex; gap: 6px; align-items: center; }
.tip-watch { background: var(--watch); color: #fff; font-size: 9px; padding: 1px 5px; border-radius: 4px; letter-spacing: 0.5px; }
.tip-row { display: flex; justify-content: space-between; gap: 16px; padding: 1px 0; color: var(--muted); }
.tip-row b { color: var(--text); }
.tip-note { font-size: 10px; color: var(--muted); font-style: italic; margin-top: 4px; }

/* ---------- footer ---------- */
.app-footer {
  display: flex; justify-content: space-between; align-items: center;
  padding: 6px 18px; background: var(--panel); border-top: 1px solid var(--line);
  font-size: 11px; color: var(--muted); font-family: ui-monospace, monospace;
}

/* ---------- about / methodology page ---------- */
.doc-body { display: block; height: auto; overflow: auto; }
.doc-back, .doc-footer a { margin-left: auto; font-size: 13px; }
.doc { max-width: 760px; margin: 0 auto; padding: 28px 20px 60px; }
.doc section { margin-bottom: 28px; }
.doc h2 { font-size: 16px; border-bottom: 1px solid var(--line); padding-bottom: 6px; margin-bottom: 12px; }
.doc p, .doc li { color: #c3ccd8; line-height: 1.6; font-size: 14px; }
.doc em { color: var(--muted); font-style: italic; }
.doc pre { background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; overflow-x: auto; }
.doc pre code { color: #c3ccd8; font-size: 12.5px; line-height: 1.5; }
.doc table { width: 100%; border-collapse: collapse; font-size: 13px; }
.doc th { text-align: left; color: var(--muted); border-bottom: 1px solid var(--line); padding: 6px 8px; font-weight: 600; }
.doc td { padding: 6px 8px; border-bottom: 1px solid rgba(35,42,55,0.6); color: #c3ccd8; }
.doc-lineage p { font-size: 13px; color: var(--muted); }
.doc-footer { text-align: center; color: var(--muted); font-size: 12px; margin-top: 40px; }
.doc-footer a { margin: 0; }

/* ---------- maplibre overrides ---------- */
.maplibregl-ctrl-attrib { font-size: 10px; }

/* ---------- responsive ---------- */
.map-controls { max-width: calc(100vw - 24px); }
.controls-toggle { display: none; }
@media (max-width: 760px) {
  .controls-toggle {
    display: block; position: absolute; top: 10px; left: 10px; z-index: 4;
    background: rgba(17, 21, 29, 0.95); border: 1px solid var(--line); color: var(--text);
    border-radius: 8px; padding: 7px 12px; font-size: 12px; cursor: pointer;
  }
  .map-controls { display: none; top: 50px; }
  .map-controls.open { display: flex; }
}
@media (max-width: 1200px) {
  .analysis-rail { width: 320px; }
}
@media (max-width: 980px) {
  .layout { flex-wrap: wrap; }
  .map-wrap { min-height: 60vh; flex: 1 1 60%; }
  .analysis-rail { width: 100%; flex: 1 1 100%; border-left: none; border-top: 1px solid var(--line); flex-direction: row; flex-wrap: wrap; }
  .analysis-rail .kpi-strip { flex: 1 1 100%; grid-template-columns: repeat(5, 1fr); }
  .analysis-rail .rail-section { flex: 1 1 48%; border-bottom: none; }
}
@media (max-width: 760px) {
  .layout { flex-direction: column; }
  .map-wrap { min-height: 56vh; }
  .analysis-rail { flex-direction: column; }
  .analysis-rail .kpi-strip { grid-template-columns: repeat(2, 1fr); }
  .crop-nav { width: 100%; margin-left: 0; overflow-x: auto; }
  .crop-pill { white-space: nowrap; }
}
