: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%; }

/* Screen reader only */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Focus visible for keyboard navigation */
:focus-visible {
  outline: 3px solid #2563eb;
  outline-offset: 2px;
}

/* Prevent focus outline on Leaflet map paths */
.leaflet-interactive:focus {
  outline: none !important;
}

/* Income popup styling */
.income-popup .leaflet-popup-content-wrapper {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 6px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
  border: none;
  padding: 8px 12px;
}
.income-popup .leaflet-popup-content {
  margin: 0;
  font-size: 0.8rem;
  line-height: 1.4;
  color: var(--text);
}
.income-popup .leaflet-popup-tip {
  background: rgba(255, 255, 255, 0.95);
  box-shadow: none;
}

.tab:focus-visible {
  outline-offset: -2px;
}

#boothSelect:focus-visible {
  outline-offset: 0;
  border-color: #2563eb;
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

body {
  margin: 0;
  font-family: 'Electrolize', -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.5rem 1rem;
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: nowrap;
  min-height: 72px;
}

.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; margin-left: auto; }

.header-2pp {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex: 1 1 auto;
  min-width: 120px;
  max-width: none;
}
.header-2pp.hidden { display: none; }
@media (max-width: 900px) {
  .header-2pp { display: none !important; }
}
.h2-side { font-size: 0.9rem; font-weight: 600; white-space: nowrap; }
.h2-track {
  flex: 1;
  height: 6px;
  border-radius: 3px;
  overflow: hidden;
  display: flex;
  background: var(--border);
}
.h2-seg { height: 100%; transition: width 0.6s ease; }

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr minmax(260px, 360px) minmax(260px, 320px);
  grid-template-rows: 1fr auto;
  gap: 1rem;
  padding: 1rem;
  min-height: 0;
}
.map-panel { grid-column: 1; grid-row: 1; }
.results-panel { grid-column: 2; grid-row: 1; }
.analysis-panel { grid-column: 3; grid-row: 1 / 3; }
#district-footer { grid-column: 1 / -1; grid-row: 2; }
@media (max-width: 1200px) {
  .layout { grid-template-columns: 1fr minmax(260px, 340px); grid-template-rows: auto 1fr auto; }
  .analysis-panel { grid-column: 1 / -1; grid-row: 1; }
  .map-panel { grid-column: 1; grid-row: 2; }
  .results-panel { grid-column: 2; grid-row: 2; }
  #district-footer { grid-column: 1 / -1; grid-row: 3; }
}
@media (max-width: 768px) {
  html, body { height: auto; min-height: 100vh; }
  body { display: block; }
  .layout {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    padding: 0.75rem;
  }
  .panel { overflow: visible; min-height: auto; }
  .map-panel { min-height: 250px; order: 1; }
  .results-panel { order: 2; }
  .analysis-panel { order: 3; }
  #district-footer { order: 4; }
  #district-footer {
    flex-wrap: wrap;
    overflow: visible;
    justify-content: center;
    min-height: auto;
    padding: 0.5rem;
  }
  .df-party {
    flex: 1 1 100px;
    min-width: 80px;
    max-width: 120px;
  }
}

.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.75rem 0.6rem;
  background: transparent;
  border: none;
  border-bottom: 3px solid transparent;
  cursor: pointer;
  font-family: 'Krona One', sans-serif;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  min-height: 44px;
}
.tab:hover { color: #1a1a2e; background: rgba(37, 99, 235, 0.05); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); background: rgba(37, 99, 235, 0.08); }

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

.booth-empty {
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: center;
  text-align: center;
}
.booth-empty p {
  color: var(--muted);
  font-size: 1.25rem;
  font-weight: 700;
  margin: 0;
}
.booth-detail.hidden ~ .booth-empty { display: flex; }
.booth-detail:not(.hidden) ~ .booth-empty { display: none; }

.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-family: 'Krona One', sans-serif; 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.9rem;
  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-ballot {
  width: 1.5rem; height: 1.5rem; border-radius: 50%;
  background: var(--border);
  display: flex; align-items: center; justify-content: center;
  font-size: 0.7rem; font-weight: 700; color: var(--muted);
  margin-right: 0.5rem; 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; min-width: 0; max-width: 100%; padding: 0.6rem; border-radius: 0.35rem; border: 2px solid #3b82f6; font-size: 1rem; min-height: 44px; background: #fff; }
.counted-badge { font-size: 0.7rem; }
.booth-detail {
  display: flex;
  flex-direction: column;
  flex: 1;
  gap: 0.5rem;
}
.booth-detail.hidden { display: none; }
#boothName { margin: 0; font-size: 1rem; font-weight: 700; }
#boothAddress { font-size: 0.8rem; color: var(--muted); margin: 0 0 0.5rem; }
.booth-bio {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 0.35rem;
  padding: 0.75rem;
  margin: 0.25rem 0;
}
.booth-bio h3 { margin: 0 0 0.4rem; }
.booth-bio p { margin: 0; font-size: 0.85rem; line-height: 1.5; color: var(--text); }
.booth-tcp { margin-top: 0.5rem; }
.booth-candidates { margin-top: 1.5rem; }
.booth-totals { display: flex; gap: 0.5rem; margin-bottom: 0.75rem; }

.analysis-panel { gap: 0; }
.analysis-panel .tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
}
.analysis-panel .tab {
  flex: 1;
  padding: 0.75rem 0.6rem;
  background: transparent;
  border: none;
  border-bottom: 3px solid transparent;
  cursor: pointer;
  font-family: 'Krona One', sans-serif;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  min-height: 44px;
}
.analysis-panel .tab:hover { color: #1a1a2e; background: rgba(37, 99, 235, 0.05); }
.analysis-panel .tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
  background: rgba(37, 99, 235, 0.08);
}
.analysis-main-header {
  padding: 0.75rem 1rem 0;
}
.analysis-main-header h3 {
  margin: 0 0 0.5rem;
  font-family: 'Krona One', sans-serif;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text);
}
.analysis-section + .analysis-section {
  margin-top: 0.75rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
}
.analysis-subheader {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.5rem;
  font-family: 'Krona One', sans-serif;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
}
.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; }

#district-footer {
  flex-shrink: 0;
  background: #f1f5f9;
  border-top: 1px solid #cbd5e1;
  padding: 12px 16px;
  display: flex;
  align-items: stretch;
  justify-content: center;
  gap: 8px;
  overflow-x: auto;
  overflow-y: hidden;
  min-height: 140px;
  position: relative;
}
.electorate-results-title {
  position: absolute;
  top: 4px;
  left: 16px;
  font-family: 'Krona One', sans-serif;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
  margin: 0;
}
#district-footer.hidden { display: none; }
.df-party {
  flex: 0 1 140px;
  min-width: 100px;
  max-width: 180px;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  padding: 10px 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  background: #ffffff;
  cursor: pointer;
  transition: all 0.2s ease;
  font-family: inherit;
  font-size: inherit;
  color: inherit;
  text-align: inherit;
}
.df-party:hover, .df-party.df-active, .df-party:focus-visible {
  padding: 12px 16px;
  gap: 6px;
  background: #ffffff;
  border-color: var(--accent);
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
  z-index: 1;
  outline: 3px solid #2563eb;
  outline-offset: 2px;
}
.df-party.df-leader {
  border-color: var(--accent);
  background: #eff6ff;
}
.df-party.df-leader:hover, .df-party.df-leader.df-active {
  background: #dbeafe;
}
#df-waiting { font-size: 11px; color: #64748b; padding: 0 4px; align-self: center; }
.df-party-name {
  font-size: 14px;
  font-weight: 700;
  color: #1e293b;
  text-align: center;
  line-height: 1.1;
}
.df-pct {
  font-size: 22px;
  font-weight: 700;
  line-height: 1;
}
.df-party:hover .df-pct, .df-party.df-active .df-pct {
  font-size: 26px;
}
.df-votes {
  font-size: 0px;
  color: #64748b;
  opacity: 0;
  transition: opacity 0.2s ease, font-size 0.2s ease;
}
.df-party:hover .df-votes, .df-party.df-active .df-votes {
  font-size: 14px;
  opacity: 1;
}
.df-bar-wrap {
  width: 100%;
  height: 2px;
  border-radius: 2px;
  overflow: hidden;
  background: #e2e8f0;
  transition: height 0.2s ease;
}
.df-party:hover .df-bar-wrap, .df-party.df-active .df-bar-wrap {
  height: 4px;
}
.df-bar-seg { height: 100%; transition: width 0.6s ease; }

/* Seat Projection */
.projection-section {
  background: #f8fafc;
  border-radius: 0.5rem;
  padding: 0.75rem;
}
.projection-body {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.projection-bar {
  display: flex;
  height: 28px;
  border-radius: 0.25rem;
  overflow: hidden;
  font-family: 'Krona One', sans-serif;
  font-size: 0.75rem;
  color: #fff;
}
.projection-bar .split-segment {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 40px;
}
.projection-legend {
  display: flex;
  gap: 1rem;
  font-size: 0.8rem;
  color: var(--text);
}
.projection-margin {
  font-size: 0.85rem;
  color: var(--text);
  padding-top: 0.25rem;
}
