:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #1a1e24;
  --muted: #5d6876;
  --border: #dde1e6;

  --dem: #1d5fc4;          /* Democrat: race called */
  --dem-lead: #7ea8e8;     /* Democrat: leading */
  --rep: #c9302a;          /* Republican: race called */
  --rep-lead: #ef8b85;     /* Republican: leading */
  --oth: #7a3ea8;          /* Independent / other: race called */
  --oth-lead: #b993d8;     /* Independent / other: leading */
  --await: #a5abb3;        /* race on ballot, no votes yet */
  --norace: #eceef1;       /* no race this year */
  --norace-line: #d3d7dd;

  --map-stroke: #ffffff;
  --select: #111418;
  --label: #ffffff;
  --label-halo: rgba(0, 0, 0, 0.35);
  --ok: #1f9d55;
  --warn: #d48806;
  --bad: #c9302a;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1217;
    --surface: #161b22;
    --surface-2: #1d232c;
    --text: #e6eaef;
    --muted: #96a1b0;
    --border: #2a313b;

    --dem: #4a8cf0;
    --dem-lead: #2c548f;
    --rep: #ea5a52;
    --rep-lead: #8e3733;
    --oth: #a874e0;
    --oth-lead: #5f4385;
    --await: #4b535e;
    --norace: #1b2129;
    --norace-line: #2a313b;

    --map-stroke: #0e1217;
    --select: #ffffff;
    --label: #ffffff;
    --label-halo: rgba(0, 0, 0, 0.55);
    --shadow: none;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  padding: 0 16px 32px;
  max-width: 1400px;
  margin: 0 auto;
}

a { color: var(--dem); }

.muted { color: var(--muted); }

.btn {
  font: inherit;
  font-size: 14px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: 0.6; cursor: progress; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
}

/* ---------- banners ---------- */
.demo-banner {
  margin: 0 -16px;
  padding: 10px 16px;
  background: #ffcf33;
  color: #241c00;
  font-weight: 600;
  text-align: center;
}
.demo-banner a { color: #241c00; margin-left: 8px; }

.error-banner {
  margin-top: 12px;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid var(--bad);
  background: color-mix(in srgb, var(--bad) 10%, var(--surface));
}

/* ---------- header ---------- */
.site-header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px 24px;
  padding: 20px 0 12px;
}
.site-header h1 { margin: 0; font-size: 26px; letter-spacing: -0.01em; }
.subtitle { margin: 2px 0 0; color: var(--muted); }

.status {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  font-size: 14px;
}
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.dot-ok { background: var(--ok); }
.dot-loading { background: var(--warn); animation: pulse 1s ease-in-out infinite; }
.dot-error { background: var(--bad); }
@keyframes pulse { 50% { opacity: 0.35; } }

/* ---------- tabs ---------- */
.tabs {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--border);
  margin-top: 8px;
}
.tabs button {
  font: inherit;
  font-weight: 600;
  font-size: 15px;
  color: var(--muted);
  background: none;
  border: 0;
  border-bottom: 3px solid transparent;
  padding: 10px 16px;
  cursor: pointer;
}
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--text); }
.tabs button:hover { color: var(--text); }

/* ---------- balance of power ---------- */
.balance { padding: 16px 0 8px; }
.balance-head {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: end;
  gap: 12px;
}
.balance-side strong { font-size: 30px; line-height: 1; display: block; }
.balance-side small { color: var(--muted); font-size: 13px; }
.balance-side.dem strong { color: var(--dem); }
.balance-side.rep { text-align: right; }
.balance-side.rep strong { color: var(--rep); }
.balance-center { text-align: center; color: var(--muted); font-size: 13px; max-width: 280px; }
.balance-bar {
  position: relative;
  display: flex;
  height: 18px;
  margin-top: 8px;
  border-radius: 4px;
  overflow: hidden;
  background: var(--await);
}
.balance-bar span { display: block; height: 100%; transition: width 0.4s ease; }
.balance-bar .midline {
  position: absolute;
  left: 50%;
  top: -3px;
  bottom: -3px;
  width: 2px;
  margin-left: -1px;
  background: var(--text);
}
.balance-foot { margin-top: 6px; font-size: 13px; color: var(--muted); }

/* ---------- layout ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 16px;
  margin-top: 8px;
}
@media (max-width: 960px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
}

.map-card { padding: 12px; min-width: 0; }
.map-toolbar { display: flex; align-items: center; gap: 12px; min-height: 34px; }
.map-wrap { position: relative; }
#map { display: block; width: 100%; height: auto; }

/* ---------- map fills ---------- */
#map path { vector-effect: non-scaling-stroke; }
.state, .district {
  stroke: var(--map-stroke);
  stroke-width: 0.8;
  cursor: pointer;
  transition: fill 0.4s ease, opacity 0.3s ease;
}
.district { stroke-width: 1; }
.state:hover, .district:hover { filter: brightness(0.92); }
@media (prefers-color-scheme: dark) {
  .state:hover, .district:hover { filter: brightness(1.2); }
}
.state.selected, .district.selected { stroke: var(--select); stroke-width: 2.5; }
.state.dimmed { opacity: 0.3; }
.state.inset-hidden { display: none; }
.state-borders { fill: none; stroke: var(--map-stroke); stroke-width: 0.8; pointer-events: none; }

.f-D-called { fill: var(--dem); }
.f-D-lead   { fill: var(--dem-lead); }
.f-R-called { fill: var(--rep); }
.f-R-lead   { fill: var(--rep-lead); }
.f-O-called { fill: var(--oth); }
.f-O-lead   { fill: var(--oth-lead); }
.f-awaiting { fill: var(--await); }
.f-tie      { fill: url(#pat-tie); }
.f-none     { fill: url(#pat-none); }

.pat-tie-a { fill: var(--dem-lead); }
.pat-tie-b { fill: var(--rep-lead); }
.pat-none-bg { fill: var(--norace); }
.pat-none-line { stroke: var(--norace-line); stroke-width: 3; }

.state-label, .district-label {
  fill: var(--label);
  font-weight: 700;
  text-anchor: middle;
  dominant-baseline: central;
  pointer-events: none;
  paint-order: stroke;
  stroke: var(--label-halo);
  stroke-linejoin: round;
}
.state-label { font-size: 11px; stroke-width: 2px; }
.state-label.lbl-muted { fill: var(--muted); stroke: none; }
.district-label { stroke-width: 0.25px; }
#map.is-zoomed .state-label { display: none; }

/* ---------- tooltip ---------- */
.tooltip {
  position: absolute;
  pointer-events: none;
  z-index: 5;
  max-width: 260px;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.18);
  font-size: 13px;
}
.tooltip strong { display: block; font-size: 14px; }

/* ---------- legend ---------- */
.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 16px;
  padding: 10px 4px 2px;
  font-size: 13px;
  color: var(--muted);
}
.legend-item { display: inline-flex; align-items: center; gap: 6px; }
.legend-note { flex-basis: 100%; }
.sw {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  display: inline-block;
  border: 1px solid var(--border);
}
.sw.f-D-called { background: var(--dem); }
.sw.f-D-lead   { background: var(--dem-lead); }
.sw.f-R-called { background: var(--rep); }
.sw.f-R-lead   { background: var(--rep-lead); }
.sw.f-O-called { background: var(--oth); }
.sw.f-O-lead   { background: var(--oth-lead); }
.sw.f-awaiting { background: var(--await); }
.sw.f-tie      { background: repeating-linear-gradient(45deg, var(--dem-lead) 0 4px, var(--rep-lead) 4px 8px); }
.sw.f-none     { background: repeating-linear-gradient(45deg, var(--norace) 0 3px, var(--norace-line) 3px 5px); }

/* ---------- side panel ---------- */
.panel { padding: 16px; min-width: 0; max-height: 760px; overflow: auto; }
.panel h2 { margin: 0 0 4px; font-size: 19px; }
.panel h3 { margin: 18px 0 8px; font-size: 14px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.panel p { margin: 6px 0; }

.race-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  margin-top: 12px;
  background: var(--surface);
}
.race-card header { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.race-card h4 { margin: 0; font-size: 16px; }
.race-meta { font-size: 13px; color: var(--muted); margin: 2px 0 8px; }

.badge {
  font-size: 12px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
  white-space: nowrap;
  background: var(--surface-2);
  color: var(--muted);
}
.badge.b-D { background: var(--dem); color: #fff; }
.badge.b-R { background: var(--rep); color: #fff; }
.badge.b-O { background: var(--oth); color: #fff; }

.cands { list-style: none; margin: 0; padding: 0; }
.cand {
  display: grid;
  grid-template-columns: 12px minmax(0, 1fr) auto 52px;
  align-items: center;
  column-gap: 8px;
  padding: 5px 0;
  border-top: 1px solid var(--border);
}
.cand:first-child { border-top: 0; }
.chip { width: 12px; height: 12px; border-radius: 50%; }
.chip.c-D, .barfill.c-D { background: var(--dem); }
.chip.c-R, .barfill.c-R { background: var(--rep); }
.chip.c-O, .barfill.c-O { background: var(--oth); }
.cname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cname .party { color: var(--muted); font-size: 12px; margin-left: 4px; }
.cname .check { font-weight: 700; margin-left: 4px; }
.cvotes { font-variant-numeric: tabular-nums; text-align: right; }
.cpct { font-variant-numeric: tabular-nums; text-align: right; font-weight: 600; }
.cbar { grid-column: 2 / -1; height: 4px; background: var(--surface-2); border-radius: 2px; margin-top: 3px; }
.barfill { display: block; height: 100%; border-radius: 2px; }

.mini-list { list-style: none; margin: 0; padding: 0; }
.mini-list li { border-top: 1px solid var(--border); }
.mini-list li:first-child { border-top: 0; }
.mini-row {
  display: grid;
  grid-template-columns: 64px minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  width: 100%;
  font: inherit;
  font-size: 14px;
  color: inherit;
  text-align: left;
  background: none;
  border: 0;
  padding: 7px 4px;
  cursor: pointer;
  border-radius: 6px;
}
.mini-row:hover, .mini-row.active { background: var(--surface-2); }
.mini-row .who { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mini-row .num { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 13px; }

.counts-row { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 14px; }
.counts-row b.d { color: var(--dem); }
.counts-row b.r { color: var(--rep); }
.counts-row b.o { color: var(--oth); }

/* ---------- table ---------- */
.table-card { margin-top: 16px; padding: 12px 16px 16px; }
.table-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; }
.table-head h2 { margin: 0; font-size: 18px; }
#filter {
  font: inherit;
  font-size: 14px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  min-width: 220px;
}
.table-scroll { overflow-x: auto; margin-top: 8px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 7px 8px; border-top: 1px solid var(--border); white-space: nowrap; }
th { color: var(--muted); font-weight: 600; font-size: 13px; border-top: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--surface-2); }
tbody tr.active { background: var(--surface-2); }
.link-btn {
  font: inherit;
  font-weight: 600;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
  text-align: left;
}
.link-btn:focus-visible, .mini-row:focus-visible, .tabs button:focus-visible, .btn:focus-visible { outline: 2px solid var(--dem); outline-offset: 2px; }

.site-footer { margin-top: 24px; font-size: 13px; color: var(--muted); }
.site-footer p { margin: 4px 0; }

@media (max-width: 600px) {
  .site-header h1 { font-size: 22px; }
  .balance-side strong { font-size: 24px; }
  .balance-center { display: none; }
  .balance-head { grid-template-columns: 1fr 1fr; }
}
