/* design tokens */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #17202a;
  --text-muted: #5c6771;
  --border: #dfe3e8;
  --accent: #0f6fd6;
  --accent-soft: rgba(15, 111, 214, 0.12);
  --win: #b45309;
  --win-bg: rgba(180, 83, 9, 0.12);
  --agree: #0f8a5f;
  --disagree: #c2410c;
  --danger: #b3261e;
  --shadow: 0 1px 3px rgba(16, 24, 32, 0.08), 0 4px 16px rgba(16, 24, 32, 0.06);
  --radius: 14px;
}
html[data-theme='dark'] {
  --bg: #0e1319;
  --surface: #161d26;
  --surface-2: #1e2833;
  --text: #e7edf3;
  --text-muted: #93a1ad;
  --border: #2a3641;
  --accent: #4da3ff;
  --accent-soft: rgba(77, 163, 255, 0.16);
  --win: #f0a53f;
  --win-bg: rgba(240, 165, 63, 0.14);
  --agree: #2fbf87;
  --disagree: #f07a3f;
  --danger: #ff6b60;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.4), 0 4px 16px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', 'Microsoft JhengHei', 'PingFang TC', 'Noto Sans TC', system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; cursor: pointer; }
[hidden] { display: none !important; }
.muted { color: var(--text-muted); }

/* ---------- gate ---------- */
.gate-overlay {
  position: fixed; inset: 0; z-index: 100;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.gate-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 36px 32px;
  max-width: 380px; width: 100%;
  text-align: center;
}
.gate-mark { font-size: 44px; }
.gate-card h1 { font-size: 22px; margin: 10px 0 4px; }
.gate-sub { margin: 0; color: var(--text-muted); font-size: 13px; }
.gate-hint { margin: 18px 0 10px; font-size: 14px; }
.turnstile-box { display: flex; justify-content: center; min-height: 66px; }
.gate-error { color: var(--danger); font-size: 13px; }

/* ---------- topbar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 50;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 1080px; margin: 0 auto;
  padding: 10px 16px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; }
.brand-badge {
  background: var(--accent-soft); color: var(--accent);
  font-size: 11px; font-weight: 700; padding: 2px 7px; border-radius: 999px;
}
.topbar-actions { display: flex; align-items: center; gap: 10px; }
.updated-time { color: var(--text-muted); font-size: 12.5px; font-variant-numeric: tabular-nums; }
.icon-btn, .refresh-btn {
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 7px 10px; line-height: 1;
}
.icon-btn:hover, .refresh-btn:hover { border-color: var(--accent); }
html[data-theme='light'] .theme-icon-dark { display: none; }
html[data-theme='dark'] .theme-icon-light { display: none; }
.refresh-btn { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; }
.refresh-btn.loading .refresh-icon { display: inline-block; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.refresh-countdown { color: var(--text-muted); font-size: 12px; min-width: 2ch; font-variant-numeric: tabular-nums; }
.refresh-btn.blocked { opacity: 0.45; cursor: not-allowed; }

/* ---------- tabs ---------- */
.tabs {
  max-width: 1080px; margin: 0 auto; padding: 12px 16px 0;
  display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  background: var(--surface); color: var(--text-muted);
  border: 1px solid var(--border); border-radius: 999px;
  padding: 8px 16px; font-weight: 600; white-space: nowrap;
}
.tab.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ---------- subnav chips ---------- */
.subnav {
  max-width: 1080px; margin: 0 auto; padding: 10px 16px 0;
  display: flex; gap: 6px; flex-wrap: wrap;
}
.subnav-secondary { padding-top: 6px; }
.chip {
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 5px 12px; font-size: 13.5px; font-weight: 600;
}
.chip.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.chip:disabled { opacity: 0.5; cursor: default; }
.subnav-label { color: var(--text-muted); font-size: 13px; align-self: center; margin-right: 4px; }

/* ---------- main ---------- */
.main { max-width: 1080px; margin: 0 auto; padding: 16px 16px 40px; }
.empty-state { color: var(--text-muted); text-align: center; padding: 60px 0; }

.summary-strip {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px; margin-bottom: 14px;
}
.stat-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 12px 16px;
}
.stat-card .stat-label { color: var(--text-muted); font-size: 12.5px; }
.stat-card .stat-value { font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; }
.stat-card .stat-sub { color: var(--text-muted); font-size: 12px; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  margin-bottom: 14px; overflow: hidden;
}
.card-header {
  display: flex; align-items: baseline; justify-content: space-between; gap: 10px;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.card-title { font-weight: 700; font-size: 15.5px; }
.card-meta { color: var(--text-muted); font-size: 12.5px; font-variant-numeric: tabular-nums; }
.card-body { padding: 6px 16px 12px; }

.progress-bar {
  height: 8px; border-radius: 999px; background: var(--surface-2);
  overflow: hidden; margin-top: 6px;
}
.progress-bar > i { display: block; height: 100%; border-radius: 999px; }

.cand-row { padding: 9px 0; border-bottom: 1px dashed var(--border); }
.cand-row:last-child { border-bottom: none; }
.cand-line1 { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.cand-no { color: var(--text-muted); font-size: 12px; min-width: 2.2em; font-variant-numeric: tabular-nums; }
.cand-name { font-weight: 700; font-size: 15px; }
.cand-party { color: var(--text-muted); font-size: 12.5px; }
.cand-tag {
  font-size: 11px; font-weight: 700; border-radius: 6px; padding: 1px 6px;
  background: var(--win-bg); color: var(--win);
}
.cand-votes { margin-left: auto; text-align: right; font-variant-numeric: tabular-nums; }
.cand-votes .votes { font-weight: 800; font-size: 16px; }
.cand-votes .pct { color: var(--text-muted); font-size: 12.5px; margin-left: 6px; }
.cand-row.leader .cand-name { color: var(--accent); }

.grid-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 12px; }
.mini-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 12px 14px; cursor: pointer; text-align: left;
  display: block; width: 100%; color: inherit;
}
button.mini-card { border: 1px solid var(--border); }
.mini-card:hover { border-color: var(--accent); }
.mini-title { font-weight: 700; font-size: 14px; }
.mini-sub { color: var(--text-muted); font-size: 12.5px; margin-top: 2px; }
.mini-leader { margin-top: 8px; font-size: 13.5px; }
.mini-leader b { font-size: 14px; }
.mini-precincts { color: var(--text-muted); font-size: 12px; margin-top: 6px; font-variant-numeric: tabular-nums; }

/* referendum */
.ref-hero {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 20px 18px; margin-bottom: 14px;
}
.ref-question { font-size: 16.5px; font-weight: 700; margin: 0 0 4px; }
.ref-case { color: var(--text-muted); font-size: 13px; margin: 0 0 14px; }
.ref-split { display: flex; height: 26px; border-radius: 999px; overflow: hidden; background: var(--surface-2); }
.ref-split .agree { background: var(--agree); }
.ref-split .disagree { background: var(--disagree); }
.ref-split-legend { display: flex; justify-content: space-between; margin-top: 8px; font-weight: 700; }
.ref-split-legend .agree { color: var(--agree); }
.ref-split-legend .disagree { color: var(--disagree); }
.ref-stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 8px; margin-top: 14px; }
.ref-stat { background: var(--surface-2); border-radius: 10px; padding: 8px 12px; }
.ref-stat .k { color: var(--text-muted); font-size: 12px; }
.ref-stat .v { font-weight: 700; font-variant-numeric: tabular-nums; }
.ref-threshold { color: var(--text-muted); font-size: 12.5px; margin-top: 12px; }

/* ---------- footer / toast ---------- */
.footer { max-width: 1080px; margin: 0 auto; padding: 10px 16px 40px; color: var(--text-muted); font-size: 12.5px; }
.footer p { margin: 3px 0; }
.toast {
  position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%);
  background: var(--text); color: var(--bg);
  padding: 10px 18px; border-radius: 12px; font-weight: 600; font-size: 14px;
  box-shadow: var(--shadow); z-index: 90; max-width: 90vw;
}

@media (max-width: 640px) {
  .brand-text { font-size: 15px; }
  .updated-time { display: none; }
  .stat-card .stat-value { font-size: 18px; }
  .main { padding: 12px 12px 32px; }
  .tabs, .subnav { padding-left: 12px; padding-right: 12px; }
}
