/* Palette: the diverging blue<->red pair from the validated reference palette.
   Party colour is not a free choice here -- D reads blue, R reads red -- and that
   pair happens to be the sanctioned diverging pair, validated in both modes
   (worst CVD dE 21.6 light / 19.2 dark). */
:root {
  color-scheme: light;
  --surface-0:      #f5f4f1;
  --surface-1:      #fcfcfb;
  --surface-2:      #ffffff;
  --text-primary:   #0b0b0b;
  --text-secondary: #52514e;
  --text-muted:     #7a7973;
  --border:         #e2e0da;
  --border-strong:  #cbc9c1;
  --dem:            #2a78d6;
  --rep:            #e34948;
  --dem-soft:       #cde2fb;
  --rep-soft:       #f8d7d7;
  --neutral-mid:    #f0efec;
  --accent:         #184f95;
  --warn-bg:        #fdf4e0;
  --warn-border:    #eda100;
  --shadow:         0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.05);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --surface-0:      #121211;
    --surface-1:      #1a1a19;
    --surface-2:      #232322;
    --text-primary:   #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted:     #918f85;
    --border:         #34342f;
    --border-strong:  #4a4a44;
    --dem:            #3987e5;
    --rep:            #e66767;
    --dem-soft:       #17365e;
    --rep-soft:       #5a2626;
    --neutral-mid:    #383835;
    --accent:         #86b6ef;
    --warn-bg:        #33290f;
    --warn-border:    #c98500;
    --shadow:         0 1px 2px rgba(0,0,0,.4), 0 4px 16px rgba(0,0,0,.3);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-0:      #121211;
  --surface-1:      #1a1a19;
  --surface-2:      #232322;
  --text-primary:   #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted:     #918f85;
  --border:         #34342f;
  --border-strong:  #4a4a44;
  --dem:            #3987e5;
  --rep:            #e66767;
  --dem-soft:       #17365e;
  --rep-soft:       #5a2626;
  --neutral-mid:    #383835;
  --accent:         #86b6ef;
  --warn-bg:        #33290f;
  --warn-border:    #c98500;
  --shadow:         0 1px 2px rgba(0,0,0,.4), 0 4px 16px rgba(0,0,0,.3);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--surface-0);
  color: var(--text-primary);
  font: 15px/1.55 ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-feature-settings: "tnum" 0;
}
.num { font-variant-numeric: tabular-nums; }
h1, h2, h3 { line-height: 1.2; margin: 0; letter-spacing: -.015em; }
a { color: var(--accent); }

.wrap { max-width: 1180px; margin: 0 auto; padding: 0 20px 96px; }

/* ---------- header ---------- */
header.top {
  border-bottom: 1px solid var(--border);
  background: var(--surface-1);
}
header.top .wrap { padding-top: 34px; padding-bottom: 26px; }
.eyebrow {
  font-size: 12px; font-weight: 650; letter-spacing: .09em; text-transform: uppercase;
  color: var(--text-muted); margin-bottom: 10px;
}
h1 { font-size: clamp(26px, 4.2vw, 40px); font-weight: 700; }
.lede { margin: 12px 0 0; max-width: 68ch; color: var(--text-secondary); font-size: 16px; }
.theme-toggle {
  position: absolute; top: 18px; right: 20px;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text-secondary);
  border-radius: 8px; padding: 6px 11px; font-size: 13px; cursor: pointer;
}
.theme-toggle:hover { border-color: var(--border-strong); color: var(--text-primary); }

.banner {
  display: flex; gap: 10px; align-items: flex-start;
  margin-top: 20px; padding: 11px 14px;
  background: var(--warn-bg); border: 1px solid var(--warn-border); border-left-width: 3px;
  border-radius: 8px; font-size: 13.5px; color: var(--text-secondary);
}
.banner b { color: var(--text-primary); }

/* ---------- sections ---------- */
section { margin-top: 52px; scroll-margin-top: 20px; }
.sec-head { margin-bottom: 18px; }
.sec-head h2 { font-size: 22px; font-weight: 680; }
.sec-head p { margin: 7px 0 0; color: var(--text-secondary); font-size: 14.5px; max-width: 74ch; }

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

/* ---------- stat tiles ---------- */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 14px; }
.tile { background: var(--surface-1); border: 1px solid var(--border); border-radius: 12px; padding: 16px 18px; box-shadow: var(--shadow); }
.tile .label { font-size: 12.5px; color: var(--text-muted); font-weight: 600; letter-spacing: .02em; }
.tile .value { font-size: 30px; font-weight: 700; margin-top: 6px; letter-spacing: -.02em; }
.tile .delta { font-size: 13px; margin-top: 4px; color: var(--text-secondary); }
.tile.dem { border-top: 3px solid var(--dem); }
.tile.rep { border-top: 3px solid var(--rep); }
.up { color: var(--dem); font-weight: 640; }
.down { color: var(--rep); font-weight: 640; }

/* ---------- charts ---------- */
.chart-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(330px, 1fr)); gap: 16px; margin-top: 16px; }
.chart-title { font-size: 14.5px; font-weight: 650; }
.chart-sub { font-size: 12.5px; color: var(--text-muted); margin-top: 3px; }
svg { display: block; overflow: visible; max-width: 100%; }
.grid-line { stroke: var(--border); stroke-width: 1; }
.axis-text { fill: var(--text-muted); font-size: 11px; }
.axis-text.strong { fill: var(--text-secondary); font-weight: 600; }
.bar-label { fill: var(--text-secondary); font-size: 11px; font-weight: 600; }

.legend { display: flex; gap: 16px; flex-wrap: wrap; margin-top: 12px; font-size: 12.5px; color: var(--text-secondary); }
.legend .key { display: inline-flex; align-items: center; gap: 6px; }
.swatch { width: 11px; height: 11px; border-radius: 3px; display: inline-block; }

/* ---------- controls ---------- */
.controls { display: flex; flex-wrap: wrap; gap: 14px; align-items: flex-end; }
.ctl { display: flex; flex-direction: column; gap: 5px; }
.ctl label { font-size: 12px; font-weight: 640; color: var(--text-muted); letter-spacing: .02em; }
select, input[type=range] { font: inherit; }
select {
  background: var(--surface-2); color: var(--text-primary);
  border: 1px solid var(--border-strong); border-radius: 8px; padding: 7px 10px; font-size: 13.5px;
  max-width: 340px;
}
input[type=range] { width: 220px; accent-color: var(--dem); }
.tabs { display: flex; gap: 6px; flex-wrap: wrap; }
.tab {
  background: var(--surface-1); border: 1px solid var(--border); color: var(--text-secondary);
  padding: 7px 14px; border-radius: 999px; font-size: 13.5px; font-weight: 600; cursor: pointer;
}
.tab[aria-selected="true"] { background: var(--text-primary); color: var(--surface-1); border-color: var(--text-primary); }
.btn {
  background: var(--surface-2); border: 1px solid var(--border-strong); color: var(--text-secondary);
  border-radius: 8px; padding: 7px 12px; font-size: 13px; cursor: pointer;
}
.btn:hover { color: var(--text-primary); }

/* ---------- table ---------- */
.table-scroll { overflow-x: auto; margin-top: 16px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface-1); }
table { border-collapse: collapse; width: 100%; font-size: 13.5px; min-width: 1220px; }
thead th {
  position: sticky; top: 0; z-index: 1;
  background: var(--surface-1); border-bottom: 1px solid var(--border-strong);
  text-align: right; padding: 10px 12px; font-size: 11.5px; letter-spacing: .04em;
  text-transform: uppercase; color: var(--text-muted); font-weight: 650; white-space: nowrap;
}
thead th:first-child, thead th.l { text-align: left; }
thead th.sortable { cursor: pointer; user-select: none; }
thead th.sortable:hover { color: var(--text-primary); }
tbody td { padding: 9px 12px; text-align: right; border-bottom: 1px solid var(--border); white-space: nowrap; }
tbody td.l { text-align: left; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--surface-2); }
tbody tr[aria-expanded="true"] { background: var(--surface-2); }
tbody tr.detail-row { cursor: default; background: var(--surface-2); }
tbody tr.detail-row:hover { background: var(--surface-2); }
td.dist { font-weight: 680; }
.pill {
  display: inline-block; padding: 1px 7px; border-radius: 999px;
  font-size: 11px; font-weight: 650; border: 1px solid transparent; vertical-align: 1px;
}
.pill.d { background: var(--dem-soft); color: var(--dem); border-color: var(--dem); }
.pill.r { background: var(--rep-soft); color: var(--rep); border-color: var(--rep); }
.pill.warn { background: var(--warn-bg); color: var(--text-secondary); border-color: var(--warn-border); }
.muted { color: var(--text-muted); }
.tiny { font-size: 11.5px; }

/* diverging lean bar inside a cell */
.lean { position: relative; width: 106px; height: 16px; display: inline-block; vertical-align: middle; }
.lean .mid { position: absolute; left: 50%; top: -1px; bottom: -1px; width: 1px; background: var(--border-strong); }
.lean .fill { position: absolute; top: 2px; height: 12px; }
.lean .fill.d { background: var(--dem); border-radius: 0 4px 4px 0; left: 50%; }
.lean .fill.r { background: var(--rep); border-radius: 4px 0 0 4px; right: 50%; }

/* ---------- detail panel ---------- */
.detail { padding: 18px 14px 22px; }
.detail h3 { font-size: 16px; margin-bottom: 4px; }
.detail-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 20px; margin-top: 14px; }
.sub { font-size: 12px; font-weight: 650; letter-spacing: .05em; text-transform: uppercase; color: var(--text-muted); margin-bottom: 8px; }
table.mini { min-width: 0; font-size: 12.5px; }
table.mini td, table.mini th { padding: 5px 8px; }
.candlist { list-style: none; margin: 0; padding: 0; font-size: 13px; }
.candlist li { display: flex; justify-content: space-between; gap: 12px; padding: 3px 0; border-bottom: 1px dotted var(--border); }
.candlist li:last-child { border-bottom: 0; }
.win { font-weight: 680; }

/* ---------- tooltip ---------- */
.tip {
  position: fixed; pointer-events: none; z-index: 50; opacity: 0; transition: opacity .09s;
  background: var(--surface-2); border: 1px solid var(--border-strong); border-radius: 9px;
  padding: 9px 11px; font-size: 12.5px; box-shadow: var(--shadow); max-width: 280px; color: var(--text-primary);
}
.tip.on { opacity: 1; }
.tip .t { font-weight: 680; margin-bottom: 4px; }
.tip .row { display: flex; justify-content: space-between; gap: 14px; }

/* ---------- method ---------- */
.method p { color: var(--text-secondary); font-size: 14px; max-width: 78ch; }
.method h3 { font-size: 15px; margin: 18px 0 6px; }
.method code { background: var(--surface-2); border: 1px solid var(--border); border-radius: 5px; padding: 1px 5px; font-size: 12.5px; }
footer { margin-top: 40px; padding-top: 18px; border-top: 1px solid var(--border); color: var(--text-muted); font-size: 12.5px; }

@media (max-width: 640px) {
  .tile .value { font-size: 25px; }
  header.top .wrap { padding-top: 46px; }
}
