:root {
  --bg: #fcfcfb;
  --panel: #ffffff;
  --ink: #1a1a19;
  --ink-2: #55554f;
  --ink-3: #85857d;
  --rule: #e2e1dc;
  --empty: #e9e8e4;
  --neutral: #f0efec;
  --county-line: rgba(255, 255, 255, 0.35);
  --state-line: #ffffff;
  --accent: #1a1a19;
  --dem: #1c5cab;
  --rep: #b3261e;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1a1a19;
    --panel: #232321;
    --ink: #f2f1ed;
    --ink-2: #bdbcb5;
    --ink-3: #8d8c85;
    --rule: #3a3a37;
    --empty: #2c2c2a;
    --neutral: #383835;
    --county-line: rgba(0, 0, 0, 0.3);
    --state-line: #1a1a19;
    --accent: #f2f1ed;
    --dem: #6da7ec;
    --rep: #f0786c;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1a1a19; --panel: #232321; --ink: #f2f1ed; --ink-2: #bdbcb5; --ink-3: #8d8c85;
  --rule: #3a3a37; --empty: #2c2c2a; --neutral: #383835; --county-line: rgba(0,0,0,.3);
  --state-line: #1a1a19; --accent: #f2f1ed; --dem: #6da7ec; --rep: #f0786c; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
}
a { color: inherit; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.top {
  display: flex; flex-wrap: wrap; gap: 12px 24px; align-items: end; justify-content: space-between;
  padding: 20px 24px 12px; border-bottom: 1px solid var(--rule);
}
h1 { font-size: 1.5rem; margin: 0; letter-spacing: -0.01em; }
.sub { margin: 2px 0 0; color: var(--ink-2); font-size: .875rem; }
.controls { display: flex; flex-wrap: wrap; gap: 8px 16px; }

.seg { display: inline-flex; border: 1px solid var(--rule); border-radius: 8px; padding: 2px; background: var(--panel); flex-wrap: wrap; }
.seg button {
  font: inherit; font-size: .875rem; color: var(--ink-2); background: none; border: 0;
  padding: 6px 12px; border-radius: 6px; cursor: pointer; white-space: nowrap;
}
.seg button:hover { color: var(--ink); }
.seg button[aria-checked="true"] { background: var(--accent); color: var(--bg); }
.seg button:focus-visible { outline: 2px solid var(--dem); outline-offset: 1px; }

main {
  display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 24px;
  padding: 16px 24px; align-items: start;
}
@media (max-width: 960px) { main { grid-template-columns: 1fr; } }

.summary { display: flex; flex-wrap: wrap; gap: 8px 28px; margin-bottom: 10px; min-height: 48px; }
.stat .k { font-size: .75rem; color: var(--ink-3); text-transform: uppercase; letter-spacing: .04em; }
.stat .v { font-size: 1.25rem; font-weight: 600; }
.stat .v .d { color: var(--dem); }
.stat .v .r { color: var(--rep); }
.stat .note { font-size: .75rem; color: var(--ink-3); }

.map-card {
  background: var(--panel); border: 1px solid var(--rule); border-radius: 12px;
  overflow: hidden; display: flex; flex-direction: column;
}
.map-wrap { position: relative; padding: 12px; }
/* One finger scrolls the page; two fingers zoom the map (see the zoom filter in app.js). */
#map { width: 100%; height: auto; display: block; touch-action: pan-x pan-y; }
.map-foot {
  border-top: 1px solid var(--rule); padding: 10px 14px 12px;
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px 20px;
}
.map-panel, .side { min-width: 0; }
.map-foot .legend { flex: 0 1 auto; }
.legend .keys { display: flex; flex-direction: column; gap: 2px; }
.legend .key { white-space: nowrap; }
.map-foot .legend .bar { width: min(280px, 100%); }
.map-foot .explain { flex: 1 1 260px; }
#map .county { stroke: var(--county-line); stroke-width: .25; vector-effect: non-scaling-stroke; }
#map .county:hover, #map .county.hot { stroke: var(--ink); stroke-width: 1.5; }
#map .states { fill: none; stroke: var(--state-line); stroke-width: 1; vector-effect: non-scaling-stroke; pointer-events: none; }
#map .hatch-bg { fill: var(--panel); }
#map .hatch-line { stroke: var(--rule); stroke-width: 1.5; }
.legend .key i.hatch { background: repeating-linear-gradient(45deg, var(--rule) 0 1.5px, var(--panel) 1.5px 4px); }
#map .nation { fill: none; stroke: var(--ink-3); stroke-width: .75; vector-effect: non-scaling-stroke; pointer-events: none; }

.tooltip {
  position: absolute; pointer-events: none; z-index: 2; min-width: 200px; max-width: 280px;
  background: var(--panel); color: var(--ink); border: 1px solid var(--rule); border-radius: 8px;
  padding: 8px 10px; font-size: .8125rem; box-shadow: 0 4px 16px rgba(0,0,0,.12);
}
.tooltip h3 { margin: 0 0 4px; font-size: .875rem; }
.tooltip .row { display: flex; justify-content: space-between; gap: 12px; }
.tooltip .row .sw { display: inline-block; width: 8px; height: 8px; border-radius: 2px; margin-right: 6px; }
.tooltip .muted { color: var(--ink-3); }
.tooltip hr { border: 0; border-top: 1px solid var(--rule); margin: 6px 0; }

.zoom-controls { position: absolute; top: 10px; right: 10px; display: flex; gap: 4px; }
.zoom-controls button {
  font: inherit; font-size: .875rem; min-width: 30px; height: 30px; padding: 0 8px; cursor: pointer;
  background: var(--panel); color: var(--ink); border: 1px solid var(--rule); border-radius: 6px;
}
.zoom-controls button:hover { border-color: var(--ink-3); }
.zoom-hint {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); pointer-events: none;
  background: rgba(26, 26, 25, .82); color: #fff; padding: 8px 14px; border-radius: 8px; font-size: .875rem;
  opacity: 0; transition: opacity .2s;
}
.zoom-hint.show { opacity: 1; }

.legend { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; font-size: .8125rem; color: var(--ink-2); }
.legend .bar { width: min(360px, 100%); }
.legend .bar .ramp { height: 10px; border-radius: 4px; }
.legend .bar .ticks { display: flex; justify-content: space-between; gap: 8px; margin-top: 2px; white-space: nowrap; }
.legend .key { display: inline-flex; align-items: center; gap: 6px; }
.legend .key i { width: 12px; height: 12px; border-radius: 3px; display: inline-block; border: 1px solid var(--rule); }
.explain { color: var(--ink-2); font-size: .8125rem; margin: 0; }

.side h2 { font-size: 1rem; margin: 0 0 8px; }
.table-scroll { max-height: 72vh; overflow: auto; border: 1px solid var(--rule); border-radius: 8px; background: var(--panel); }
table { width: 100%; border-collapse: collapse; font-size: .8125rem; }
th, td { padding: 5px 8px; text-align: left; border-bottom: 1px solid var(--rule); }
th { position: sticky; top: 0; background: var(--panel); color: var(--ink-3); font-weight: 600; font-size: .75rem; }
td.num, th.num { text-align: right; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--empty); }
td .chip { display: inline-block; width: 8px; height: 8px; border-radius: 2px; margin-right: 6px; vertical-align: 1px; }
td .flag { color: var(--ink-3); }

footer { padding: 8px 24px 24px; color: var(--ink-3); font-size: .75rem; }
footer p { margin: 0; }

/* Big enough screens: one viewport, no page scroll. The map scales to fit its
   card and only the state table scrolls, inside its own box. */
@media (min-width: 961px) and (min-height: 600px) {
  body { height: 100vh; height: 100dvh; display: flex; flex-direction: column; overflow: hidden; }
  .top { padding-top: 14px; padding-bottom: 10px; }
  main { flex: 1; min-height: 0; padding-top: 12px; padding-bottom: 8px; align-items: stretch; grid-template-rows: minmax(0, 1fr); }
  .map-panel { display: flex; flex-direction: column; min-height: 0; }
  .summary { min-height: 0; margin-bottom: 8px; }
  .stat .v { font-size: 1.125rem; }
  .map-foot .legend { flex-wrap: nowrap; }
  .map-card { flex: 1; min-height: 0; }
  .map-wrap { flex: 1; min-height: 0; }
  #map { height: 100%; touch-action: none; }
  .side { display: flex; flex-direction: column; min-height: 0; }
  .table-scroll { flex: 1; min-height: 0; max-height: none; }
  footer { padding: 0 24px 10px; }
}

@media (max-width: 600px) {
  .top, main, footer { padding-left: 16px; padding-right: 16px; }
  .seg button { padding: 6px 9px; }
  .summary { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 16px; }
  .stat .v { font-size: 1.0625rem; }
}
