:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --ink: #16181d;
  --ink-2: #4a505c;
  --ink-3: #737a87;
  --line: #e2e5ea;
  --pending: #d9dde3;
  --head-bg: #10141c;
  --head-ink: #f3f5f8;
  --accent: #c8102e;
  --kmt: #1d4fa3;
  --dpp: #1f9a50;
  --tpp: #1b98cc;
  --oth: #b0467a;
  --tossup: #b25e00;
  --safe: #2b6a3f;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115; --surface: #191b20; --surface-2: #22252c; --ink: #eef0f3; --ink-2: #b8bdc7; --ink-3: #8c93a0;
    --line: #2d313a; --pending: #353a44; --head-bg: #0a0c10;
    --kmt: #5a6ee6; --dpp: #3aae66; --tpp: #28a9d4; --oth: #d466a0; --tossup: #f0a24a; --safe: #6fcf8e;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1115; --surface: #191b20; --surface-2: #22252c; --ink: #eef0f3; --ink-2: #b8bdc7; --ink-3: #8c93a0;
  --line: #2d313a; --pending: #353a44; --head-bg: #0a0c10;
  --kmt: #5a6ee6; --dpp: #3aae66; --tpp: #28a9d4; --oth: #d466a0; --tossup: #f0a24a; --safe: #6fcf8e;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; scroll-padding-top: 110px; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 15px/1.55 "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", system-ui, sans-serif;
  font-variant-numeric: tabular-nums;
}
a { color: inherit; }
.wrap { max-width: 1240px; margin: 0 auto; padding: 0 16px; }
.muted { color: var(--ink-3); }
.small { font-size: 13px; }

.top { background: var(--head-bg); color: var(--head-ink); position: sticky; top: 0; z-index: 10; border-bottom: 3px solid var(--accent); }
.top-inner { display: flex; justify-content: space-between; align-items: center; gap: 16px; padding-top: 12px; padding-bottom: 8px; flex-wrap: wrap; }
.top h1 { margin: 0; font-size: 22px; letter-spacing: .02em; }
.top .sub { margin: 2px 0 0; font-size: 13px; color: #b9c0cc; }
.top .sub .muted { color: #8d95a3; }
.status { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 999px; background: #2a303b; font-weight: 600; font-size: 13px; }
.pill.live::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); animation: pulse 1.4s infinite; }
.pill.done { background: #234a31; }
@keyframes pulse { 50% { opacity: .25; } }
.meta { font-size: 12px; color: #9aa3b1; }
.toc { display: flex; gap: 14px; padding-bottom: 8px; overflow-x: auto; font-size: 13px; }
.toc a { color: #c9d0db; text-decoration: none; white-space: nowrap; }
.toc a:hover { color: #fff; text-decoration: underline; }

main { padding-top: 16px; padding-bottom: 24px; display: grid; gap: 16px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 16px; min-width: 0; }
.sec-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
h2 { margin: 0; font-size: 17px; }
.grid2 { display: grid; grid-template-columns: minmax(0, 1.05fr) minmax(0, 1fr); gap: 16px; }
@media (max-width: 860px) { .grid2 { grid-template-columns: 1fr; } }

/* seat bar */
.seatbar { display: flex; gap: 2px; height: 34px; }
.seat { flex: 1; border-radius: 4px; background: var(--pending); position: relative; cursor: pointer; }
.seat.lead { opacity: .45; }
.seat:focus-visible, .seat:hover { outline: 2px solid var(--ink); outline-offset: 1px; }
.seat-mid { position: relative; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; margin-top: 10px; font-size: 13px; color: var(--ink-2); }
.legend .sw { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.legend .sw.hollow { opacity: .45; }
.compare { margin-top: 8px; font-size: 13px; color: var(--ink-2); }
.compare b { color: var(--ink); }

/* map */
.map { width: 100%; aspect-ratio: 4 / 5; max-height: 640px; }
.map svg { width: 100%; height: 100%; display: block; }
.map path.county { stroke: var(--surface); stroke-width: 1; cursor: pointer; }
.map path.county:hover, .map path.county.sel { stroke: var(--ink); stroke-width: 2; }
.map .inset-box { fill: none; stroke: var(--line); stroke-dasharray: 3 3; }
.map text.inset-label { font-size: 11px; fill: var(--ink-3); }
.map text.lbl { font-size: 10px; fill: var(--ink); paint-order: stroke; stroke: var(--surface); stroke-width: 3px; pointer-events: none; font-weight: 600; }

/* detail */
.detail h3 { margin: 0; font-size: 20px; }
.detail .d-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }
.kpis { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin: 12px 0; }
.kpi { background: var(--surface-2); border-radius: 8px; padding: 8px 10px; }
.kpi .k { font-size: 12px; color: var(--ink-3); }
.kpi .v { font-size: 18px; font-weight: 700; }
.progress { height: 6px; background: var(--pending); border-radius: 3px; overflow: hidden; margin-top: 4px; }
.progress > i { display: block; height: 100%; background: var(--ink-2); border-radius: 3px; }

.cand { display: grid; grid-template-columns: 26px minmax(0, 1fr) auto; gap: 2px 8px; align-items: center; padding: 6px 0; border-top: 1px solid var(--line); }
.cand .no { width: 24px; height: 24px; border-radius: 50%; display: grid; place-items: center; font-size: 12px; font-weight: 700; background: var(--surface-2); }
.cand .nm { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cand .pt { font-size: 12px; color: var(--ink-3); font-weight: 400; margin-left: 6px; }
.cand .vt { text-align: right; font-weight: 700; }
.cand .vt small { display: block; font-weight: 400; color: var(--ink-3); font-size: 12px; }
.cand .bar { grid-column: 2 / 4; height: 8px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
.cand .bar > i { display: block; height: 100%; border-radius: 0 4px 4px 0; }
.cand.win .nm::after { content: " ✔ 當選"; color: var(--safe); font-size: 12px; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 700; border: 1px solid currentColor; white-space: nowrap; }
.badge.pending { color: var(--ink-3); }
.badge.lead { color: var(--ink-2); }
.badge.tossup { color: var(--tossup); }
.badge.safe { color: var(--safe); }
.badge.elected { color: #fff; background: var(--safe); border-color: var(--safe); }
.badge.flip { color: var(--accent); }

.spark { width: 100%; height: 120px; margin-top: 10px; }
.spark text { font-size: 10px; fill: var(--ink-3); }
.spark .grid { stroke: var(--line); }

/* filters + cards */
.filters { display: flex; gap: 4px; flex-wrap: wrap; }
.filters button { font: inherit; font-size: 13px; padding: 4px 12px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); color: var(--ink-2); cursor: pointer; }
.filters button[aria-selected="true"] { background: var(--ink); color: var(--surface); border-color: var(--ink); }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 10px; }
.rc { border: 1px solid var(--line); border-left: 5px solid var(--pending); border-radius: 8px; padding: 10px 12px; cursor: pointer; background: var(--surface); text-align: left; font: inherit; color: inherit; width: 100%; }
.rc:hover, .rc:focus-visible { border-color: var(--ink-3); }
.rc .rh { display: flex; justify-content: space-between; align-items: center; gap: 6px; }
.rc .rn { font-weight: 700; }
.rc .ld { margin-top: 6px; font-size: 14px; }
.rc .ld b { font-size: 15px; }
.rc .mini { display: flex; gap: 2px; height: 8px; margin-top: 6px; border-radius: 4px; overflow: hidden; background: var(--surface-2); }
.rc .mini i { display: block; height: 100%; }
.rc .rf { display: flex; justify-content: space-between; font-size: 12px; color: var(--ink-3); margin-top: 6px; }
.empty { color: var(--ink-3); padding: 12px 0; }

.multiples { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 12px; }
.multiples .mp h4 { margin: 0 0 2px; font-size: 14px; }

.method ul { margin: 8px 0 0; padding-left: 20px; color: var(--ink-2); }
.method li { margin: 4px 0; }
.foot { padding-bottom: 32px; }

.tip { position: fixed; z-index: 50; pointer-events: none; background: var(--surface); color: var(--ink); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-size: 13px; box-shadow: 0 6px 20px rgba(0,0,0,.18); max-width: 260px; }
.tip .tr { display: flex; justify-content: space-between; gap: 12px; }

@media (max-width: 520px) {
  .top h1 { font-size: 18px; }
  .status { align-items: flex-start; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  html { scroll-padding-top: 150px; }
}
@media (prefers-reduced-motion: reduce) { .pill.live::before { animation: none; } html { scroll-behavior: auto; } }

/* owner login + admin */
.login-btn { font: inherit; font-size: 12px; padding: 3px 12px; border-radius: 999px; border: 1px solid #4a5363; background: transparent; color: #dfe4ec; cursor: pointer; }
.login-btn:hover { background: #2a303b; }
.login-btn.owner { border-color: #f3c13a; color: #f3c13a; }
.admin { border: 2px solid #f3c13a; }
.admin-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; }
@media (max-width: 760px) { .admin-grid { grid-template-columns: 1fr; } }
.admin-actions { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0; }
.admin-actions button, .btn-access { font: inherit; font-size: 13px; padding: 6px 12px; border-radius: 6px; border: 1px solid var(--line); background: var(--surface-2); color: var(--ink); cursor: pointer; text-decoration: none; display: inline-block; }
.admin-actions button.primary, .btn-access { background: var(--ink); color: var(--surface); border-color: var(--ink); }
.admin-actions button:disabled { opacity: .5; cursor: wait; }
.admin-kv { display: grid; grid-template-columns: auto 1fr; gap: 2px 12px; font-size: 13px; margin: 8px 0 0; }
.admin-kv dt { color: var(--ink-3); }
.admin-kv dd { margin: 0; word-break: break-all; }
.admin textarea, dialog input { width: 100%; font: 12px/1.4 ui-monospace, monospace; background: var(--surface-2); color: var(--ink); border: 1px solid var(--line); border-radius: 6px; padding: 8px; }
.admin-log { font-size: 12px; background: var(--surface-2); border-radius: 6px; padding: 8px; max-height: 160px; overflow: auto; margin: 8px 0 0; white-space: pre-wrap; }
.admin-log:empty { display: none; }
dialog { border: 1px solid var(--line); border-radius: 12px; background: var(--surface); color: var(--ink); width: min(380px, calc(100vw - 32px)); padding: 18px; }
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog h3 { margin: 0 0 4px; }
dialog input { font: inherit; padding: 8px; margin: 4px 0; }
dialog .btn-access { display: block; text-align: center; margin: 10px 0 14px; }
.err { color: var(--accent); min-height: 1em; margin: 4px 0; }

/* news */
.news { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 0 20px; }
.news li { padding: 8px 0; border-top: 1px solid var(--line); }
.news a { text-decoration: none; font-weight: 600; }
.news a:hover { text-decoration: underline; }
.news .nm { display: block; font-size: 12px; color: var(--ink-3); margin-top: 2px; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 4px; vertical-align: 0; }
.dot.ok { background: var(--safe); }
.dot.bad { background: var(--accent); }
