/* ---------- Design tokens (change colours here) ---------- */
:root {
  --bg: #0a1020;
  --panel: #111a2e;
  --panel-edge: #1e2a44;
  --text: #eef2fb;
  --muted: #8d9ab5;

  --D: #2f7bf5;
  --R: #e5383b;
  --I: #e3a92b;
  --U: #46516a;          /* uncalled */
  --none: #1a2338;       /* state with no Senate race */
  --land: #1a2338;
  --border: #0a1020;
  --flip: #ffffff;      /* outline for seats that changed party */

  --gap: clamp(10px, 1.4vw, 28px);
  --radius: 12px;
  --fs-title: clamp(1.15rem, 1.1rem + 1vw, 2.4rem);
  --fs-h2: clamp(1rem, 0.9rem + 0.8vw, 2rem);
  --fs-num: clamp(1.6rem, 1.2rem + 1.8vw, 3.6rem);
  --fs-small: clamp(0.72rem, 0.65rem + 0.3vw, 1.05rem);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: var(--gap) max(var(--gap), env(safe-area-inset-right)) var(--gap) max(var(--gap), env(safe-area-inset-left));
  gap: var(--gap);
}
.muted { color: var(--muted); }

/* ---------- Header ---------- */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.topbar h1 { margin: 0; font-size: var(--fs-title); font-weight: 800; letter-spacing: -0.01em; }
.meta { display: flex; align-items: center; gap: 12px; font-size: var(--fs-small); color: var(--muted); }
.live { display: inline-flex; align-items: center; gap: 8px; }
.live .dot { width: 10px; height: 10px; border-radius: 50%; background: #22c55e; box-shadow: 0 0 0 0 #22c55e88; animation: pulse 2s infinite; }
.live.error .dot { background: #f59e0b; animation: none; }
@keyframes pulse { 70% { box-shadow: 0 0 0 10px #22c55e00; } 100% { box-shadow: 0 0 0 0 #22c55e00; } }
.demo-badge { display: none; background: var(--I); color: #111; font-weight: 800; padding: 3px 8px; border-radius: 6px; letter-spacing: 0.05em; }
body.demo .demo-badge { display: inline-block; }

/* ---------- Layout: one column on phones, two side by side on wide screens ---------- */
.board { display: grid; grid-template-columns: 1fr; gap: var(--gap); flex: 1; }
@media (min-width: 960px) {
  .board { grid-template-columns: 1fr 1fr; }
}

.panel {
  background: var(--panel);
  border: 1px solid var(--panel-edge);
  border-radius: var(--radius);
  padding: var(--gap);
  display: flex;
  flex-direction: column;
  gap: calc(var(--gap) * 0.6);
  min-width: 0;
}
.panel-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.panel-head h2 { margin: 0; font-size: var(--fs-h2); font-weight: 800; }
.status { margin: 0; font-size: var(--fs-small); font-weight: 700; padding: 4px 10px; border-radius: 999px; background: var(--U); }
.status[data-party="D"] { background: var(--D); }
.status[data-party="R"] { background: var(--R); }

.map-wrap, .meter-wrap { display: flex; justify-content: center; min-height: 0; }
.map { width: 100%; height: auto; max-height: 34vh; display: block; }
.meter { width: 100%; max-width: 560px; height: auto; max-height: 21vh; display: block; overflow: visible; }

/* ---------- Maps ---------- */
.state { fill: var(--land); stroke: none; transition: fill 0.6s ease; }
.state.no-race { fill: var(--none); }
.state.has-race { fill: var(--U); cursor: pointer; }
.state.has-race[data-winner="D"] { fill: var(--D); }
.state.has-race[data-winner="R"] { fill: var(--R); }
.state.has-race[data-winner="I"] { fill: var(--I); }
.state.has-race:hover, .state:focus-visible { filter: brightness(1.2); outline: none; }
.state.no-race { cursor: pointer; }
.borders { fill: none; stroke: var(--bg); stroke-width: 1.2; stroke-linejoin: round; pointer-events: none; }
.state-label { fill: #fff; font-size: 13px; font-weight: 700; text-anchor: middle; pointer-events: none; paint-order: stroke; stroke: #0006; stroke-width: 2px; }

.house-state { fill: #172036; stroke: rgba(255,255,255,0.75); stroke-width: 0.9; stroke-linejoin: round; cursor: pointer; transition: fill 0.2s; }
.house-state:hover, .house-state:focus-visible { fill: #24304d; outline: none; }
.house-borders { stroke: rgba(255,255,255,0.75); stroke-width: 0.9; }
.callout-box { cursor: pointer; }
.seat { fill: var(--U); stroke: var(--panel); stroke-width: 0.8; cursor: pointer; transition: fill 0.4s ease; }
.seat[data-winner="D"] { fill: var(--D); }
.seat[data-winner="R"] { fill: var(--R); }
.seat[data-winner="I"] { fill: var(--I); }
.seat:hover { stroke: #fff; stroke-width: 1.5; }
.callout-box { fill: #0d1527; stroke: var(--panel-edge); }
.callout-label { fill: var(--muted); font-size: 11px; font-weight: 700; }
.leader { fill: none; stroke: var(--muted); stroke-width: 0.8; opacity: 0.6; }
.leader-dot { fill: var(--muted); }

/* ---------- Meter ---------- */
.meter-bg { fill: var(--U); }
.meter-seg.fill-D { fill: var(--D); }
.meter-seg.fill-I { fill: var(--I); }
.meter-seg.fill-I-none { fill: var(--I); opacity: 0.7; }
.meter-seg.fill-R { fill: var(--R); }
.meter-majority { stroke: #fff; stroke-width: 3; stroke-dasharray: 6 4; }
.meter-majority-label { fill: var(--muted); font-size: 15px; font-weight: 600; }
.meter-needle path { fill: #fff; }
.meter-needle circle { fill: #fff; stroke: var(--panel); stroke-width: 4; }
.meter-centre { fill: var(--text); font-size: 52px; font-weight: 800; }
.meter-centre-sub { fill: var(--muted); font-size: 17px; }
.meter-needle { filter: drop-shadow(0 2px 3px #0009); }

/* ---------- Seat totals ---------- */
.totals { display: grid; grid-template-columns: repeat(4, 1fr); gap: calc(var(--gap) * 0.5); }
.tile { border-radius: 10px; padding: 6px 10px; background: #0d1527; border-top: 5px solid var(--U); text-align: center; min-width: 0; }
.tile-D { border-top-color: var(--D); }
.tile-R { border-top-color: var(--R); }
.tile-I { border-top-color: var(--I); }
.tile-label { font-size: var(--fs-small); color: var(--muted); font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile-num { font-size: var(--fs-num); font-weight: 800; line-height: 1.05; }
.tile-D .tile-num { color: #6ea3ff; }
.tile-R .tile-num { color: #ff6b6d; }
.tile-I .tile-num { color: var(--I); }
.tile-sub { font-size: calc(var(--fs-small) * 0.9); color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- Seats flipped ---------- */
.flips {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 6px 10px;
  font-size: var(--fs-small); padding: 6px 10px; border-radius: 10px; background: #0d1527;
}
.flips:empty { display: none; }
.flips-label { display: inline-flex; align-items: center; gap: 8px; font-weight: 600; color: var(--muted); }
.flips-label b { color: var(--text); font-size: 1.25em; }
.flip-icon { width: 14px; height: 14px; border-radius: 50%; border: 2.5px solid var(--flip); display: inline-block; }
.flip-pill { font-weight: 700; padding: 2px 10px; border-radius: 999px; border: 1.5px solid; }
.flip-D { color: #8db8ff; border-color: var(--D); }
.flip-R { color: #ff9496; border-color: var(--R); }
.flip-I { color: var(--I); border-color: var(--I); }
.flips-net { font-weight: 800; }

/* Flipped seats on the maps */
.flip-outline { fill: none; stroke: var(--flip); stroke-width: 2.6; stroke-linejoin: round; pointer-events: none; }
.seat.flip { stroke: var(--flip); stroke-width: 1.8; }

/* ---------- Legend ---------- */
.legend { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px 20px; font-size: var(--fs-small); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.sw { width: 14px; height: 14px; border-radius: 3px; display: inline-block; }
.sw-D { background: var(--D); } .sw-R { background: var(--R); } .sw-I { background: var(--I); }
.sw-U { background: var(--U); }
.sw-F { background: transparent; border: 2.5px solid var(--flip); border-radius: 50%; }
.legend a { color: var(--muted); } .sw-N { background: var(--none); border: 1px solid var(--panel-edge); }

/* ---------- Tooltip ---------- */
.tooltip {
  position: fixed; z-index: 10; pointer-events: none; opacity: 0;
  background: #fff; color: #111; padding: 8px 12px; border-radius: 8px; font-size: 14px; line-height: 1.4;
  box-shadow: 0 6px 24px #0008; transition: opacity 0.12s;
}
.tooltip.show { opacity: 1; }
.tooltip .muted { color: #666; }
.t-D { color: #1d5fd1; } .t-R { color: #c81e21; } .t-I { color: #a87400; }

/* ---------- State view (zoomed state + race list) ---------- */
body.sv-open { overflow: hidden; }
.sv-backdrop {
  position: fixed; inset: 0; z-index: 20; display: grid; place-items: center;
  background: rgba(4, 8, 18, 0.72); backdrop-filter: blur(4px);
  opacity: 0; transition: opacity 0.3s ease;
}
.sv-backdrop.show { opacity: 1; }
.sv-backdrop[hidden] { display: none; }
.sv-dialog {
  width: min(1500px, 94vw); height: min(920px, 90vh); height: min(920px, 90dvh);
  background: var(--panel); border: 1px solid var(--panel-edge); border-radius: var(--radius);
  display: flex; flex-direction: column; overflow: hidden; box-shadow: 0 30px 80px #000a;
  transform: scale(0.96); transition: transform 0.3s ease;
}
.sv-backdrop.show .sv-dialog { transform: none; }
.sv-head { display: flex; align-items: center; gap: 16px; padding: 14px var(--gap); border-bottom: 1px solid var(--panel-edge); }
.sv-head h2 { margin: 0; font-size: var(--fs-h2); font-weight: 800; flex: 1; }
.sv-tabs { display: inline-flex; background: #0d1527; border-radius: 999px; padding: 4px; }
.sv-tabs button {
  font: inherit; font-weight: 700; font-size: var(--fs-small); color: var(--muted);
  background: none; border: 0; border-radius: 999px; padding: 8px 18px; cursor: pointer; min-height: 40px;
}
.sv-tabs button.on { background: var(--text); color: var(--bg); }
.sv-close {
  font-size: 30px; line-height: 1; width: 44px; height: 44px; border-radius: 50%;
  border: 1px solid var(--panel-edge); background: #0d1527; color: var(--text); cursor: pointer;
}
.sv-close:hover { background: #1c2740; }
.sv-body { flex: 1; min-height: 0; display: grid; grid-template-columns: 1.35fr 1fr; }
.sv-map { min-height: 0; display: flex; padding: var(--gap); }
.sv-map svg { width: 100%; height: 100%; display: block; }
.sv-state { fill: #141d31; stroke: none; cursor: pointer; transition: fill 0.4s; }
.sv-state.focus { fill: #2a3655; cursor: default; }
.sv-state.focus[data-winner="none"] { fill: var(--U); }
.sv-state.focus[data-winner="norace"] { fill: var(--none); }
.sv-state.focus[data-winner="D"] { fill: var(--D); }
.sv-state.focus[data-winner="R"] { fill: var(--R); }
.sv-state.focus[data-winner="I"] { fill: var(--I); }
.sv-borders { fill: none; stroke: rgba(255,255,255,0.55); stroke-width: 0.6; vector-effect: non-scaling-stroke; pointer-events: none; }
.sv-state.focus { stroke: #fff; stroke-width: 2; vector-effect: non-scaling-stroke; }
.sv-seat { cursor: pointer; outline: none; }
.sv-seat circle { fill: var(--U); stroke: #fff; transition: fill 0.4s; }
.sv-seat circle[data-winner="D"] { fill: var(--D); }
.sv-seat circle[data-winner="R"] { fill: var(--R); }
.sv-seat circle[data-winner="I"] { fill: var(--I); }
.sv-seat text { fill: #fff; font-weight: 800; text-anchor: middle; pointer-events: none; }
.sv-seat.hl circle, .sv-seat:focus-visible circle { stroke: #ffe066; stroke-width: 0.2em; }

.sv-races { overflow-y: auto; padding: var(--gap); border-left: 1px solid var(--panel-edge); -webkit-overflow-scrolling: touch; }
.sv-sec + .sv-sec { margin-top: calc(var(--gap) * 1.2); }
.sv-sec h3 { margin: 0 0 10px; font-size: calc(var(--fs-small) * 1.3); text-transform: uppercase; letter-spacing: 0.06em; }
.sv-sum { display: flex; gap: 14px; font-weight: 700; font-size: var(--fs-small); margin-bottom: 10px; }
.sv-row {
  display: grid; grid-template-columns: minmax(90px, 0.6fr) 1fr; gap: 12px; align-items: start;
  padding: 10px 12px; border-radius: 8px; background: #0d1527; margin-bottom: 6px;
  border: 1px solid transparent; transition: border-color 0.15s, background 0.15s;
}
.sv-row.hl { border-color: #ffe066; background: #141f38; }
.sv-row-big { padding: 14px; }
.sv-id { font-weight: 700; font-size: var(--fs-small); }
.small { font-size: 0.85em; font-weight: 500; }
.sv-res { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; font-size: var(--fs-small); }
.sv-name { font-weight: 700; font-size: 1.1em; }
.sv-name.muted { font-weight: 500; font-size: 0.95em; }
.sv-caucus { font-size: 0.92em; padding: 2px 8px; border-radius: 6px; background: #1a2338; border-left: 3px solid var(--muted); }
.sv-caucus.caucus-D { border-left-color: var(--D); }
.sv-caucus.caucus-R { border-left-color: var(--R); }
.chip { font-size: 0.8em; font-weight: 800; text-transform: uppercase; letter-spacing: 0.04em; padding: 2px 8px; border-radius: 4px; background: var(--U); color: #fff; }
.chip-D { background: var(--D); } .chip-R { background: var(--R); } .chip-I { background: var(--I); color: #111; }
.gain { display: inline-block; white-space: nowrap; font-weight: 800; text-transform: uppercase; font-size: 0.85em; margin-top: 4px; padding: 1px 6px; border-radius: 4px; color: #fff; }
.gain-D { background: var(--D); } .gain-R { background: var(--R); } .gain-I { background: var(--I); color: #111; }

@media (max-width: 800px) {
  .sv-dialog { width: 100vw; height: 100vh; height: 100dvh; border-radius: 0; border: 0; }
  .sv-head { flex-wrap: wrap; padding-top: max(14px, env(safe-area-inset-top)); }
  .sv-head h2 { flex-basis: calc(100% - 60px); order: 1; }
  .sv-close { order: 2; }
  .sv-tabs { order: 3; }
  .sv-body { grid-template-columns: 1fr; grid-template-rows: 42% 1fr; }
  .sv-races { border-left: 0; border-top: 1px solid var(--panel-edge); padding-bottom: max(var(--gap), env(safe-area-inset-bottom)); }
}

.sv-lead { display: flex; align-items: center; gap: 6px; }
.dotp { width: 9px; height: 9px; border-radius: 50%; background: var(--U); display: inline-block; }
.dotp-D { background: var(--D); } .dotp-R { background: var(--R); } .dotp-I { background: var(--I); }
.sv-flipped { color: var(--text); border: 1.5px solid var(--flip); border-radius: 999px; padding: 0 8px; }

/* ---------- Phones ---------- */
@media (max-width: 600px) {
  .totals { grid-template-columns: repeat(2, 1fr); }
  .map { max-height: none; }
  .meter { max-height: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
