:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --text: #1a1a2e;
  --muted: #6b7280;
  --accent: #2563eb;
  --success: #16a34a;
  --danger: #dc2626;
  --warning: #d97706;
  --border: #e5e7eb;
  --live: #dc2626;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
}

header {
  background: var(--text);
  color: #fff;
  padding: 0.75rem 1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.income-legend {
  background: rgba(255, 255, 255, 0.95);
  padding: 0.75rem;
  border-radius: 6px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
  font-size: 0.75rem;
  line-height: 1.4;
  color: var(--text);
  min-width: 160px;
}
.income-legend h4 {
  margin: 0 0 0.5rem;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text);
}
.legend-gradient {
  height: 14px;
  border-radius: 3px;
  border: 1px solid rgba(0,0,0,0.15);
  margin-bottom: 0.25rem;
}
.legend-labels {
  display: flex;
  justify-content: space-between;
  font-size: 0.65rem;
  color: var(--muted);
  margin-bottom: 0.25rem;
}
.legend-note {
  font-size: 0.65rem;
  color: var(--muted);
  font-style: italic;
}
.brand { display: flex; align-items: center; gap: 0.75rem; }
.brand h1 { margin: 0; font-size: 1.25rem; }
.badge {
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 0.2rem 0.5rem;
  border-radius: 999px;
  background: var(--muted);
}
.badge.live { background: var(--live); color: #fff; }
.meta { display: flex; gap: 1rem; font-size: 0.8rem; color: #cbd5e1; }

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 360px 320px;
  grid-template-rows: 1fr;
  gap: 1rem;
  padding: 1rem;
  min-height: 0;
}
@media (max-width: 1200px) {
  .layout { grid-template-columns: 1fr 340px; }
  .analysis-panel { grid-column: 1 / -1; display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
}
@media (max-width: 768px) {
  .layout { grid-template-columns: 1fr; }
  .analysis-panel { grid-template-columns: 1fr; }
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.map-panel { min-height: 300px; }
#map { width: 100%; height: 100%; min-height: 300px; }

.results-panel .tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
}
.tab {
  flex: 1;
  padding: 0.6rem;
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
}
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }

.tab-content { display: none; padding: 1rem; overflow-y: auto; }
.tab-content.active { display: block; }

.totals-bar {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.5rem;
  margin-bottom: 1rem;
}
.pill {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 0.4rem;
  padding: 0.5rem;
  text-align: center;
}
.pill .label { display: block; font-size: 0.65rem; text-transform: uppercase; color: var(--muted); }
.pill .value { display: block; font-size: 1rem; font-weight: 700; }
.pill .pct { display: block; font-size: 0.75rem; color: var(--muted); }
.pill .pct.baseline { color: var(--accent); }

.tcp-widget, .tpp-widget, .candidates-list { margin-bottom: 1.25rem; }
h3 { margin: 0 0 0.5rem; font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.03em; color: var(--muted); }

.split-bar {
  height: 28px;
  border-radius: 0.35rem;
  overflow: hidden;
  display: flex;
  background: var(--bg);
  border: 1px solid var(--border);
}
.split-segment {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  font-weight: 700;
  color: #fff;
  white-space: nowrap;
  padding: 0 0.5rem;
}
.tcp-legend, .tpp-legend {
  display: flex;
  justify-content: space-between;
  margin-top: 0.35rem;
  font-size: 0.75rem;
}

.candidate-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0;
  border-bottom: 1px solid var(--border);
}
.candidate-row:last-child { border-bottom: none; }
.candidate-color {
  width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0;
}
.candidate-name { flex: 1; font-size: 0.85rem; }
.candidate-party { font-size: 0.7rem; color: var(--muted); }
.candidate-votes { font-weight: 700; font-size: 0.85rem; }
.candidate-pct { font-size: 0.75rem; color: var(--muted); width: 3rem; text-align: right; }
.candidate-swing { font-size: 0.7rem; width: 3.5rem; text-align: right; }
.swing-up { color: var(--success); }
.swing-down { color: var(--danger); }

.booth-controls { display: flex; align-items: center; gap: 0.5rem; margin-bottom: 0.75rem; }
#boothSelect { flex: 1; padding: 0.4rem; border-radius: 0.35rem; border: 1px solid var(--border); }
.counted-badge { font-size: 0.7rem; }
.booth-detail.hidden { display: none; }
.booth-detail .address { font-size: 0.8rem; color: var(--muted); margin: 0 0 0.5rem; }
.booth-totals { display: flex; gap: 0.5rem; margin-bottom: 0.75rem; }

.analysis-panel { gap: 1rem; }
.analysis-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  padding: 1rem;
  display: flex;
  flex-direction: column;
}
.analysis-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.5rem;
}
.analysis-header h3 { margin: 0; }
.ai-label {
  font-size: 0.65rem;
  background: #1e293b;
  color: #fff;
  padding: 0.2rem 0.4rem;
  border-radius: 0.25rem;
}
.analysis-body {
  font-size: 0.85rem;
  line-height: 1.5;
  color: var(--text);
}

.history-entry {
  padding: 0.6rem 0;
  border-bottom: 1px solid var(--border);
}
.history-entry:last-child { border-bottom: none; }
.history-time { font-size: 0.7rem; color: var(--muted); }
.history-type {
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  padding: 0.1rem 0.35rem;
  border-radius: 0.2rem;
  margin-left: 0.4rem;
}
.history-type.trend { background: #fff7ed; color: var(--warning); }
.history-type.conclusion { background: #f0fdf4; color: var(--success); }
.history-text { font-size: 0.8rem; margin-top: 0.25rem; }

footer {
  background: var(--panel);
  border-top: 1px solid var(--border);
  padding: 0.5rem 1rem;
  font-size: 0.75rem;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
}
