:root {
  color-scheme: light dark;
  --bg: #f7f7f5;
  --panel-bg: #ffffff;
  --border: #d8d8d4;
  --text: #1f1f1f;
  --text-muted: #6b6b68;
  --accent: #2b6cb0;
  --accent-solid: #2b6cb0;
  --no-data: #c9c9c5;
  --accent-tint: rgba(43, 108, 176, 0.08);
  --map-stroke: #ffffff;
  --map-highlight-stroke: #1f1f1f;
  --state-fill: #dfe6ee;
  --state-fill-hover: #c9d8e8;
}

/* Follows the OS/browser color-scheme preference automatically - no manual
   toggle. `color-scheme: light dark` above additionally lets the browser
   pick correct native widget/scrollbar colors before this query even
   applies. */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17181a;
    --panel-bg: #212226;
    --border: #3a3b3f;
    --text: #e8e8e6;
    --text-muted: #9c9c99;
    --accent: #6fa8dc;
    --accent-solid: #3a7cbf;
    --no-data: #3a3b3e;
    --accent-tint: rgba(111, 168, 220, 0.12);
    --map-stroke: #17181a;
    --map-highlight-stroke: #f2f2f0;
    --state-fill: #2a3540;
    --state-fill-hover: #35455a;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

body {
  display: flex;
  flex-direction: column;
}

#app-header {
  display: flex;
  align-items: baseline;
  gap: 1rem;
  padding: 0.75rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--panel-bg);
}

#app-header h1 {
  font-size: 1.1rem;
  margin: 0;
  white-space: nowrap;
}

#nation-hint {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text);
}

#breadcrumb {
  font-size: 0.9rem;
  color: var(--text-muted);
}

#breadcrumb a {
  color: var(--accent);
  text-decoration: none;
  cursor: pointer;
}

#breadcrumb a:hover { text-decoration: underline; }

#app-main {
  flex: 1;
  display: flex;
  min-height: 0;
}

#county-list-panel {
  width: 240px;
  flex: 0 0 auto;
  border-right: 1px solid var(--border);
  background: var(--panel-bg);
  padding: 0.75rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

#county-list-panel h2 {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin: 0 0 0.5rem;
}

#county-search {
  width: 100%;
  padding: 0.35rem 0.5rem;
  margin-bottom: 0.5rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 0.85rem;
}

#county-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
}

#county-list li {
  padding: 0.3rem 0.4rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.85rem;
}

#county-list li:hover { background: var(--bg); }
#county-list li.selected { background: var(--accent-solid); color: white; }
#county-list li.no-data { color: var(--text-muted); }

#map-panel {
  flex: 1;
  position: relative;
  min-width: 0;
}

#map-svg {
  width: 100%;
  height: 100%;
  display: block;
}

.county-shape {
  stroke: var(--map-stroke);
  stroke-width: 0.4px;
  cursor: pointer;
}

.county-shape.no-data {
  fill: var(--no-data);
  cursor: default;
}

.county-shape:hover { stroke: var(--map-highlight-stroke); stroke-width: 1px; }

.county-shape.selected {
  stroke: var(--map-highlight-stroke);
  stroke-width: 2.5px;
  vector-effect: non-scaling-stroke;
}

.state-border {
  fill: none;
  stroke: var(--map-stroke);
  stroke-width: 1px;
  pointer-events: none;
}

.state-shape {
  fill: var(--state-fill);
  stroke: var(--map-stroke);
  stroke-width: 0.6px;
  cursor: pointer;
}

.state-shape:hover { fill: var(--state-fill-hover); }

#detail-panel {
  width: 380px;
  flex: 0 0 auto;
  border-left: 1px solid var(--border);
  background: var(--panel-bg);
  padding: 0.75rem;
  overflow-y: auto;
}

#detail-panel h2 {
  font-size: 1rem;
  margin: 0 0 0.5rem;
}

#detail-charts {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.detail-chart-title {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 0.1rem;
}

.detail-chart-svg {
  width: 100%;
  height: 120px;
  display: block;
}

/* Overrides the inline stroke="#999" set in drawMiniChart() - a CSS rule
   beats a presentation attribute regardless of specificity, so this wins
   without touching the JS. */
.detail-marker {
  stroke: var(--text-muted);
}

.legend {
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.5rem 0.65rem;
  margin: 0.75rem 0;
  font-size: 0.75rem;
}

.collapsible {
  position: relative;
  padding-right: 1.75rem;
}

.legend-collapse-toggle {
  position: absolute;
  top: 0.3rem;
  right: 0.3rem;
  width: 1.1rem;
  height: 1.1rem;
  line-height: 1;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--panel-bg);
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.85rem;
}

.legend-collapse-toggle:hover {
  color: var(--text);
  border-color: var(--accent);
}

.collapsible.collapsed {
  min-width: 0;
  width: auto;
  padding: 0.4rem 1.6rem 0.4rem 0.6rem;
}

.collapsible.collapsed > *:not(.legend-collapse-toggle):not(.legend-title) {
  /* !important: #legend-swatch (an ID selector) sets its own display:block
     and would otherwise win over this class-based rule regardless of
     specificity math - same class of bug as the SVG stroke/fill issue
     elsewhere in this file. */
  display: none !important;
}

.collapsible.collapsed .legend-title {
  margin: 0;
  white-space: nowrap;
}

#overview-stats .stat-row {
  flex-wrap: wrap;
  row-gap: 0.1rem;
}

#overview-stats .stat-county {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#overview-stats .stat-value {
  margin-left: 0.5rem;
}

.sources-legend {
  font-size: 0.68rem;
  line-height: 1.4;
}

.sources-legend p {
  margin: 0;
}

.sources-legend a {
  color: var(--accent);
  text-decoration: none;
}

.sources-legend a:hover {
  text-decoration: underline;
}

.legend-title {
  font-weight: 600;
  margin-bottom: 0.3rem;
}

#legend-swatch {
  width: 100%;
  display: block;
}

.legend-labels {
  display: flex;
  justify-content: space-between;
  color: var(--text-muted);
  margin-top: 0.15rem;
}

.legend-note {
  margin-top: 0.35rem;
  color: var(--text-muted);
}

#map-legend.clickable {
  cursor: pointer;
  border-width: 2px;
  border-color: var(--accent);
}

#map-legend.clickable:hover {
  background: var(--accent-tint);
}

#legend-toggle-hint {
  font-style: italic;
}

.chart-legend {
  position: static;
  margin-top: 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.chart-legend .swatch-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.chart-legend .swatch {
  width: 14px;
  height: 3px;
  display: inline-block;
}

.hint {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin: 0 0 0.75rem;
}

#overview-section {
  margin-bottom: 0.6rem;
}

/* One card per stat (coverage, home value, income, ratio) instead of one
   box holding all of them - each is its own flex item here so they can
   wrap freely, rather than being nested inside a single wrapping box. */
#overview-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
}

.stat-card {
  flex: 1 1 200px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel-bg);
  padding: 0.5rem 0.65rem;
}

.stat-card-title {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin: 0 0 0.35rem;
}

.stat-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 0.85rem;
  padding: 0.15rem 0;
  cursor: pointer;
  border-radius: 4px;
}

.stat-row:hover { background: var(--bg); }

.stat-row .stat-label {
  color: var(--text-muted);
  margin-right: 0.5rem;
}

.stat-row .stat-county {
  flex: 1;
}

.stat-row .stat-value {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

#timeline-panel {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  padding: 0.6rem 1.25rem;
  border-top: 1px solid var(--border);
  background: var(--panel-bg);
  max-height: 45vh;
  overflow-y: auto;
}

#timeline-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

/* The ratio legend and data sources - previously floating over the map
   in every view, now living here instead (alongside #overview-section
   above) so the map (wider than it is tall, especially at the national
   level) gets the full width of #app-main to itself in both views.
   Ratio legend is the smaller of the two (a color bar + two numbers)
   so it gets 1/3 of the row, data sources (a list of links) gets 2/3 -
   flex-basis 0 with those grow ratios splits any leftover space that
   way once both fit on one line; below that they wrap to full width,
   1/3-first, in source order. */
#info-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  align-items: flex-start;
}

#info-row #map-legend {
  flex: 1 1 240px;
}

#info-row #sources-legend {
  flex: 2 1 320px;
}

#info-row .legend {
  margin: 0;
}

#play-toggle {
  border: 1px solid var(--border);
  background: var(--panel-bg);
  border-radius: 4px;
  width: 2rem;
  height: 2rem;
  cursor: pointer;
}

#timeline-label {
  font-variant-numeric: tabular-nums;
  min-width: 5.5rem;
  color: var(--text-muted);
  font-size: 0.9rem;
}

#timeline-track {
  flex: 1;
  display: flex;
  flex-direction: column;
}

#timeline-slider {
  width: 100%;
}

#timeline-scale {
  width: 100%;
  height: 14px;
  display: block;
  margin-top: -0.15rem;
  overflow: visible;
}

#timeline-scale text {
  font-size: 9px;
  fill: var(--text-muted);
}

#timeline-scale line {
  stroke: var(--border);
}

[hidden] { display: none !important; }

/* Nation view is just the map now - no side panel competing with it for
   width at all, in any screen size, since the ratio legend/overview/data
   sources all moved to the footer's #info-row. State view still has
   #county-list-panel (240px, fixed) + #detail-panel (380px, fixed)
   though, which together can leave the map a sliver or nothing below
   ~920px (min-width: 0 lets its flex-basis go to zero) - stack those into
   one scrollable column instead, map first with a real height of its own.
   Scoped (via :has) to when #detail-panel is actually visible - i.e.
   state view - so nation view's map keeps using the full #app-main
   height at every width instead of leaving unused space below a
   height-capped map with nothing else there to fill it. */
@media (max-width: 920px) {
  #app-header {
    flex-wrap: wrap;
    row-gap: 0.15rem;
  }

  #app-main:has(#detail-panel:not([hidden])) {
    flex-direction: column;
    overflow-y: auto;
  }

  #app-main:has(#detail-panel:not([hidden])) #county-list-panel {
    order: 2;
    width: 100%;
    max-height: 200px;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  #app-main:has(#detail-panel:not([hidden])) #map-panel {
    order: 1;
    flex: none;
    width: 100%;
    height: 55vh;
    min-height: 260px;
  }

  #app-main:has(#detail-panel:not([hidden])) #detail-panel {
    order: 3;
    width: 100%;
    border-left: none;
    border-top: 1px solid var(--border);
  }

  /* Same specificity (2 ids) as the #info-row #map-legend/#sources-legend
     rules above so this actually overrides their flex-basis instead of
     losing to them regardless of source order. */
  #info-row #map-legend,
  #info-row #sources-legend {
    flex-basis: 100%;
  }

  /* .stat-card's flex-basis: 200px lets two fit per row around this
     width, giving an awkward 2x2 grid instead of a clean single-column
     stack - force full width instead. */
  #overview-stats .stat-card {
    flex-basis: 100%;
  }
}
