:root {
  --bg: #080b12;
  --panel: #0f1420;
  --panel-2: #151c2c;
  --line: #222b3f;
  --text: #e7eaf2;
  --muted: #8892a8;
  --d: #2f6bff;
  --r: #ef3b3b;
  --t: #b59f6b;
  --i: #a855f7;
  --accent: #7c9cff;
  --radius: 14px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(1200px 600px at 15% -10%, #142046 0%, transparent 60%),
    radial-gradient(900px 500px at 110% 0%, #3a1220 0%, transparent 55%), var(--bg);
  color: var(--text);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}
button, select, input { font: inherit; color: inherit; }
button { cursor: pointer; }
h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }
h2 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
.muted { color: var(--muted); }
.note { color: var(--muted); font-size: 12.5px; margin: 8px 0; }
.note.err { color: #ff8a8a; }
.note a { color: var(--accent); }

.app { display: grid; grid-template-rows: auto auto minmax(0, 1fr); height: 100vh; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 18px; border-bottom: 1px solid var(--line);
  background: rgba(8, 11, 18, 0.7); backdrop-filter: blur(10px);
}
.brand { display: flex; align-items: center; gap: 10px; margin-right: auto; }
.brand b { display: block; letter-spacing: 0.12em; font-size: 15px; }
.brand small { color: var(--muted); font-size: 11.5px; }
.logo {
  width: 34px; height: 34px; display: grid; place-items: center; border-radius: 10px;
  background: linear-gradient(135deg, var(--d), var(--r)); font-size: 16px;
}
.seg-ctl { display: flex; background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 3px; }
.seg-ctl button {
  border: 0; background: none; padding: 7px 14px; border-radius: 7px; color: var(--muted);
  display: flex; align-items: center; gap: 7px; font-weight: 550; white-space: nowrap;
}
.seg-ctl button:hover { color: var(--text); }
.seg-ctl button.active { background: #24304a; color: var(--text); box-shadow: 0 1px 0 rgba(255, 255, 255, 0.06) inset; }
.top-right { display: flex; align-items: center; gap: 8px; }
.clock { font-variant-numeric: tabular-nums; color: var(--text); font-size: 24px; font-weight: 800; line-height: 1; white-space: nowrap; padding: 0 6px; }
.icon-btn {
  width: 34px; height: 34px; border-radius: 9px; border: 1px solid var(--line); background: var(--panel-2); font-size: 16px;
}
.icon-btn:hover { border-color: #34405c; }

.dot { width: 8px; height: 8px; border-radius: 50%; background: #22c55e; box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.6); animation: pulse 2s infinite; display: inline-block; flex: none; }
.dot.err { background: #f59e0b; animation: none; }
@keyframes pulse { 70% { box-shadow: 0 0 0 7px rgba(34, 197, 94, 0); } 100% { box-shadow: 0 0 0 0 rgba(34, 197, 94, 0); } }

/* ---------- balance of power ---------- */
.bop { display: grid; grid-template-columns: 150px minmax(0, 1fr) 150px; gap: 18px; align-items: center; padding: 12px 18px; }
.bop-side { display: flex; flex-direction: column; }
.bop-side.r { text-align: right; }
.bop-party { font-size: 11px; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); }
.bop-num { font-size: 40px; font-weight: 800; line-height: 1; font-variant-numeric: tabular-nums; }
.bop-side.d .bop-num { color: var(--d); }
.bop-side.r .bop-num { color: var(--r); }
.bop-sub { font-size: 12px; color: var(--muted); }
.bop-meta, .bop-foot { display: flex; justify-content: space-between; font-size: 12.5px; color: var(--muted); margin: 0 2px 6px; }
.bop-meta b { color: var(--text); }
.bop-foot { margin: 6px 2px 0; justify-content: center; }
.bop-bar { position: relative; display: flex; height: 22px; border-radius: 6px; overflow: hidden; background: #1b2232; }
.seg { display: block; height: 100%; transition: width 0.5s ease; }
.seg.d { background: var(--d); }
.seg.d-lean { background: repeating-linear-gradient(45deg, var(--d) 0 4px, #1b2c55 4px 8px); }
.seg.i { background: var(--i); }
.seg.t { background: #2a3245; }
.seg.r-lean { background: repeating-linear-gradient(45deg, var(--r) 0 4px, #4a1e24 4px 8px); }
.seg.r { background: var(--r); }
.bop-mid { position: absolute; top: -3px; bottom: -3px; width: 2px; background: #fff; transform: translateX(-1px); box-shadow: 0 0 6px rgba(255, 255, 255, 0.6); }

/* ---------- layout ---------- */
.main {
  display: grid; grid-template-columns: minmax(0, 1fr) 370px 310px; gap: 12px;
  padding: 0 12px 12px; min-height: 0;
}
.panel {
  background: linear-gradient(180deg, rgba(21, 28, 44, 0.85), rgba(15, 20, 32, 0.85));
  border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; min-height: 0; overflow: auto;
  scrollbar-width: thin; scrollbar-color: #2b3550 transparent;
}
.panel-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; margin-bottom: 8px; }
.panel-head.sub { margin: 16px 0 8px; align-items: center; }
.panel-head.sub:first-child { margin-top: 0; }
.map-panel h2 { font-size: 16px; color: var(--text); text-transform: none; letter-spacing: -0.01em; }
.map-panel .muted { margin: 3px 0 0; font-size: 12.5px; }
.map-panel { display: flex; flex-direction: column; overflow: hidden; }
.head-actions { display: flex; gap: 8px; }
.head-actions button, .state-actions button, .demo-ctl button, menu button {
  border: 1px solid var(--line); background: var(--panel-2); padding: 6px 12px; border-radius: 8px; font-size: 12.5px;
}
.head-actions button:hover, .state-actions button:hover, .demo-ctl button:hover { border-color: #3a4766; }
button.primary { background: var(--accent); color: #0b1020; border-color: transparent; font-weight: 650; }
button.link { border: 0; background: none; color: var(--accent); font-size: 12px; }
body:not([data-mode="whatif"]) .whatif-only { display: none; }

/* ---------- map ---------- */
.map { flex: 1; min-height: 260px; display: grid; place-items: center; }
.map svg { width: 100%; height: 100%; max-height: 100%; }
.state { stroke: none; transition: fill 0.35s ease, filter 0.2s; cursor: pointer; outline: none; }
.state:hover, .state:focus-visible { filter: brightness(1.25); }
.selected-outline { fill: none; stroke: #fff; stroke-width: 2.2; stroke-linejoin: round; pointer-events: none; vector-effect: non-scaling-stroke; }
.borders { fill: none; stroke: var(--bg); stroke-width: 1.1; stroke-linejoin: round; pointer-events: none; }
.labels text, .callout text {
  font-size: 11px; font-weight: 700; fill: #fff; text-anchor: middle; dominant-baseline: middle;
  pointer-events: none; paint-order: stroke; stroke: rgba(0, 0, 0, 0.45); stroke-width: 2.5px;
}
.callout text { dominant-baseline: auto; }
.callout { cursor: pointer; }
.callout rect { stroke: var(--line); transition: fill 0.35s ease; }
.callout:hover rect { filter: brightness(1.25); }
.callout rect.selected { stroke: #fff; stroke-width: 2; }

.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; justify-content: center; padding-top: 10px; }
.lg { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.lg i, .sw { width: 14px; height: 14px; border-radius: 4px; display: inline-block; flex: none; }
.lg.brush { border: 1px solid var(--line); background: var(--panel-2); border-radius: 999px; padding: 4px 10px 4px 6px; }
.lg.brush.active { border-color: var(--accent); color: var(--text); box-shadow: 0 0 0 1px var(--accent) inset; }

/* ---------- detail ---------- */
.empty { color: var(--muted); text-align: center; padding: 26px 10px; border: 1px dashed var(--line); border-radius: 12px; }
.empty.small { padding: 12px; font-size: 12.5px; margin-top: 10px; }
.race-card { background: var(--panel-2); border: 1px solid var(--line); border-radius: 12px; padding: 14px; margin-top: 10px; }
.race-card:first-child { margin-top: 0; }
.race-card h3 { font-size: 17px; }
.badges { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.badge {
  font-size: 11px; padding: 3px 8px; border-radius: 999px; border: 1px solid var(--c, var(--line));
  color: var(--text); background: color-mix(in srgb, var(--c, transparent) 18%, transparent);
}
.badge.status { --c: #64748b; }
.badge.flip, em.flip { --c: #f59e0b; font-style: normal; font-weight: 700; color: #fbbf24; }
em.flip { font-size: 10px; margin-right: 6px; letter-spacing: 0.05em; }

.rating-picker { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; margin: 12px 0 4px; }
.rating-picker button {
  border: 1px solid var(--line); border-radius: 7px; background: color-mix(in srgb, var(--c) 22%, var(--panel));
  font-size: 10.5px; padding: 7px 2px; line-height: 1.15;
}
.rating-picker button.active { background: var(--c); color: #0b1020; font-weight: 700; border-color: #fff; }
.rating-picker button:disabled { opacity: 0.35; cursor: not-allowed; }

.cands { display: grid; gap: 10px; margin-top: 12px; }
.cand-top { display: flex; align-items: center; gap: 8px; }
.cand-top .pct { margin-left: auto; font-weight: 700; font-variant-numeric: tabular-nums; }
.cand.won b { color: #fff; }
.check { color: #22c55e; font-weight: 800; }
.pchip { font-size: 10px; font-weight: 800; padding: 2px 6px; border-radius: 5px; color: #fff; letter-spacing: 0.04em; }
.cand-bar { height: 7px; background: #1e2638; border-radius: 4px; overflow: hidden; margin: 5px 0 2px; }
.cand-bar i { display: block; height: 100%; transition: width 0.5s ease; }
.cand small { color: var(--muted); font-size: 11.5px; }
.reporting { margin-top: 12px; font-size: 12px; color: var(--muted); }
.rep-bar { height: 4px; background: #1e2638; border-radius: 3px; overflow: hidden; margin-top: 5px; }
.rep-bar i { display: block; height: 100%; background: linear-gradient(90deg, #64748b, #cbd5e1); transition: width 0.5s; }

.house-state header { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.house-state h3 small { font-size: 12px; color: var(--muted); font-weight: 500; }
.tallies { display: flex; gap: 8px; font-size: 13px; }
b.d { color: var(--d); }
b.r { color: var(--r); }
.state-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.chips { display: grid; grid-template-columns: repeat(auto-fill, minmax(38px, 1fr)); gap: 5px; margin-top: 12px; }
.chip {
  border: 1px solid rgba(255, 255, 255, 0.08); border-radius: 7px; height: 32px;
  font-size: 11.5px; font-weight: 700; color: #fff; text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}
.chip:hover { filter: brightness(1.2); }
.chip.sel { outline: 2px solid #fff; outline-offset: 1px; }
.chip.locked { border-style: dashed; border-color: rgba(255, 255, 255, 0.5); }

.race-list { display: grid; gap: 4px; }
.race-row {
  display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: center; gap: 10px;
  text-align: left; background: transparent; border: 1px solid transparent; border-radius: 9px; padding: 7px 8px;
}
.race-row:hover { background: var(--panel-2); }
.race-row.sel { background: var(--panel-2); border-color: #34405c; }
.rr-main { display: flex; flex-direction: column; min-width: 0; }
.rr-main b { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 600; }
.rr-main small, .rr-status { color: var(--muted); font-size: 11.5px; }
.rr-status { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* ---------- feed ---------- */
.source { background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.src-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.src-row .muted { font-size: 12px; width: 100%; padding-left: 16px; }
.demo-ctl { display: flex; gap: 6px; align-items: center; margin-top: 10px; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
.demo-ctl select { background: var(--panel); border: 1px solid var(--line); border-radius: 7px; padding: 4px 6px; margin-left: 4px; }
.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-top: 10px; }
.stat { background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; }
.stat span { display: block; font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; }
.stat b { font-size: 22px; font-variant-numeric: tabular-nums; }
.stat small { color: var(--muted); font-size: 13px; font-weight: 500; }
.calls { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; max-height: 42vh; overflow: auto; }
.calls li { display: grid; grid-template-columns: 62px auto 1fr; gap: 8px; align-items: center; padding: 6px 4px; border-bottom: 1px solid rgba(255, 255, 255, 0.04); font-size: 12.5px; animation: slide 0.4s ease; }
.calls li.empty { display: block; }
.calls time { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 11.5px; }
.calls .sw { width: 10px; height: 10px; border-radius: 50%; }
@keyframes slide { from { opacity: 0; transform: translateY(-4px); } }
.feed-group + .feed-group { margin-top: 16px; }
.civic-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; }
.civic-list li { background: var(--panel-2); border-radius: 8px; padding: 8px 10px; display: flex; flex-direction: column; }
.civic-list small { color: var(--muted); }

/* ---------- overlays ---------- */
.tooltip {
  position: fixed; left: 0; top: 0; z-index: 50; pointer-events: none; min-width: 150px;
  background: rgba(15, 20, 32, 0.96); border: 1px solid #2f3a55; border-radius: 10px; padding: 8px 11px;
  font-size: 12.5px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
}
.tooltip b:first-child { display: block; font-size: 13.5px; margin-bottom: 2px; }
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 60;
  background: #1f2a44; border: 1px solid #34405c; border-radius: 10px; padding: 10px 16px;
}
dialog {
  background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: var(--radius);
  width: min(520px, 94vw); padding: 20px;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.6); backdrop-filter: blur(3px); }
dialog h2 { margin-bottom: 14px; color: var(--text); font-size: 15px; }
dialog form { display: grid; gap: 12px; }
dialog label { display: grid; gap: 5px; font-size: 12.5px; color: var(--muted); }
dialog input, dialog select {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px;
}
dialog fieldset { border: 1px solid var(--line); border-radius: 10px; display: grid; gap: 10px; padding: 10px 12px 12px; }
dialog legend { font-size: 12px; color: var(--text); padding: 0 6px; }
dialog .row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
menu { display: flex; justify-content: flex-end; gap: 8px; margin: 4px 0 0; padding: 0; }

/* ---------- responsive ---------- */
@media (max-width: 1400px) {
  .main { grid-template-columns: minmax(0, 1fr) 360px; grid-template-rows: minmax(0, 1fr) auto; }
  .feed-panel { grid-column: 1 / -1; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0 16px; max-height: 260px; }
  .feed-group + .feed-group { margin-top: 0; }
  .calls { max-height: 190px; }
}
@media (max-width: 960px) {
  .app { height: auto; min-height: 100vh; }
  .main { grid-template-columns: 1fr; }
  .panel { overflow: visible; }
  .map { min-height: 0; }
  .feed-panel { display: block; max-height: none; }
  .feed-group + .feed-group { margin-top: 16px; }
  .calls { max-height: 320px; }
  .bop { grid-template-columns: 90px minmax(0, 1fr) 90px; gap: 10px; padding: 10px 12px; }
  .bop-num { font-size: 30px; }
  .bop-sub { display: none; }
  .topbar { gap: 10px; padding: 10px 12px; }
  .seg-ctl { flex: 1; }
  .seg-ctl button { flex: 1; justify-content: center; padding: 7px 8px; }
}
@media (max-width: 560px) {
  .brand small, .clock { display: none; }
  .labels text { font-size: 14px; }
  .rating-picker { grid-template-columns: repeat(4, 1fr); }
  .bop-meta { flex-direction: column; align-items: center; gap: 2px; }
}
