/* ===== BASE RESET & TOKENS ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root, [data-theme="light"] {
  --color-bg: #f5f4f0;
  --color-surface: #ffffff;
  --color-surface-2: #f9f8f5;
  --color-border: #ddd9d3;
  --color-text: #1a1815;
  --color-text-muted: #6b6862;
  --color-text-faint: #a8a6a1;
  --color-primary: #01696f;
  --color-primary-hover: #0c4e54;
  --color-rep: #b2182b;
  --color-dem: #2166ac;
  --color-third: #6a994e;
  --color-rep-light: #fddede;
  --color-dem-light: #d6e4f0;
  --color-map-bg: #eae8e3;
  --color-state-stroke: #ffffff;
  --color-state-hover: rgba(255,255,255,0.4);
  --color-tooltip-bg: #ffffff;
  --color-tooltip-border: #ddd9d3;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.08);
  --shadow-lg: 0 8px 30px rgba(0,0,0,0.12);
  --font-body: 'DM Sans', -apple-system, sans-serif;
  --font-mono: 'DM Mono', 'Menlo', monospace;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --transition: 180ms cubic-bezier(0.16, 1, 0.3, 1);
}

[data-theme="dark"] {
  --color-bg: #111110;
  --color-surface: #1a1918;
  --color-surface-2: #222120;
  --color-border: #333230;
  --color-text: #e8e6e1;
  --color-text-muted: #908e89;
  --color-text-faint: #5a5856;
  --color-primary: #4f98a3;
  --color-primary-hover: #227f8b;
  --color-rep: #e05a5a;
  --color-dem: #5b9bd5;
  --color-third: #7cb950;
  --color-rep-light: #3a1a1a;
  --color-dem-light: #1a2a3a;
  --color-map-bg: #1a1918;
  --color-state-stroke: #2a2928;
  --color-state-hover: rgba(255,255,255,0.12);
  --color-tooltip-bg: #222120;
  --color-tooltip-border: #3a3938;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.2);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.3);
  --shadow-lg: 0 8px 30px rgba(0,0,0,0.4);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --color-bg: #111110;
    --color-surface: #1a1918;
    --color-surface-2: #222120;
    --color-border: #333230;
    --color-text: #e8e6e1;
    --color-text-muted: #908e89;
    --color-text-faint: #5a5856;
    --color-primary: #4f98a3;
    --color-primary-hover: #227f8b;
    --color-rep: #e05a5a;
    --color-dem: #5b9bd5;
    --color-third: #7cb950;
    --color-rep-light: #3a1a1a;
    --color-dem-light: #1a2a3a;
    --color-map-bg: #1a1918;
    --color-state-stroke: #2a2928;
    --color-state-hover: rgba(255,255,255,0.12);
    --color-tooltip-bg: #222120;
    --color-tooltip-border: #3a3938;
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.2);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.3);
    --shadow-lg: 0 8px 30px rgba(0,0,0,0.4);
  }
}

html {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  scroll-behavior: smooth;
}

body {
  font-family: var(--font-body);
  font-size: 14px;
  line-height: 1.5;
  color: var(--color-text);
  background: var(--color-bg);
  min-height: 100dvh;
  overflow-x: hidden;
}

a { color: var(--color-primary); text-decoration: none; }
a:hover { color: var(--color-primary-hover); text-decoration: underline; }
button { cursor: pointer; font: inherit; border: none; background: none; color: inherit; }
img { display: block; max-width: 100%; }

/* ===== APP LAYOUT ===== */
#app {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  z-index: 100;
  gap: 16px;
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.logo { color: var(--color-text); flex-shrink: 0; }

.app-title {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.01em;
  white-space: nowrap;
}

.app-subtitle {
  font-size: 13px;
  color: var(--color-text-faint);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.data-attribution {
  font-size: 11px;
  color: var(--color-text-faint);
  white-space: nowrap;
}

.data-attribution a { color: var(--color-text-muted); }
.data-attribution a:hover { color: var(--color-primary); }

.theme-toggle {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  color: var(--color-text-muted);
  transition: all var(--transition);
}
.theme-toggle:hover { background: var(--color-surface-2); color: var(--color-text); }

/* ===== MAIN LAYOUT ===== */
.main-layout {
  display: flex;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

/* ===== SIDEBAR ===== */
.sidebar {
  width: 280px;
  min-width: 280px;
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  overflow-y: auto;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.sidebar-section {
  padding-bottom: 16px;
  border-bottom: 1px solid var(--color-border);
}
.sidebar-section:last-child { border-bottom: none; padding-bottom: 0; }

.section-title {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-text-faint);
  margin-bottom: 10px;
}

/* Winner Section */
.winner-section { text-align: center; }

.winner-portrait {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  margin: 0 auto 10px;
  background: var(--color-surface-2);
  border: 2px solid var(--color-border);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: visible;
}

.winner-party-badge {
  position: absolute;
  bottom: -2px;
  right: -2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid var(--color-surface);
}

.winner-name {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.2;
  margin-bottom: 2px;
}

.winner-party {
  font-size: 13px;
  font-weight: 500;
  margin-bottom: 2px;
}

.winner-label {
  font-size: 11px;
  color: var(--color-text-faint);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

/* Stats Section */
.stat-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 0;
}
.stat-label { font-size: 13px; color: var(--color-text-muted); }
.stat-value {
  font-size: 13px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* Electoral College Bar */
.ec-bar-container { margin-bottom: 12px; }

.ec-bar {
  height: 28px;
  border-radius: var(--radius-sm);
  overflow: hidden;
  display: flex;
  background: var(--color-surface-2);
  position: relative;
}

.ec-bar-segment {
  height: 100%;
  transition: width 600ms cubic-bezier(0.16, 1, 0.3, 1);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 600;
  color: #fff;
  min-width: 0;
  overflow: hidden;
}

.ec-bar-labels {
  display: flex;
  justify-content: space-between;
  margin-top: 4px;
  font-size: 11px;
  color: var(--color-text-muted);
  font-variant-numeric: tabular-nums;
}
.ec-label-center {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}

.ec-candidates {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.ec-candidate {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
}

.ec-candidate-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.ec-candidate-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ec-candidate-votes {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  flex-shrink: 0;
}

/* Third Parties */
.third-party-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
  font-size: 12px;
}

.third-party-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.third-party-info { flex: 1; min-width: 0; }

.third-party-name {
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.third-party-detail {
  font-size: 11px;
  color: var(--color-text-faint);
}

/* Notes */
.election-note {
  font-size: 12px;
  color: var(--color-text-muted);
  line-height: 1.5;
  font-style: italic;
}

/* ===== MAP AREA ===== */
.map-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  position: relative;
  background: var(--color-map-bg);
}

/* Controls Bar */
.controls-bar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 20px;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
  z-index: 10;
}

.year-control {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.year-nav {
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  color: var(--color-text-muted);
  transition: all var(--transition);
}
.year-nav:hover { background: var(--color-surface-2); color: var(--color-text); }

.current-year {
  font-size: 22px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  min-width: 56px;
  text-align: center;
  letter-spacing: -0.02em;
}

.slider-container {
  flex: 1;
  position: relative;
  padding: 0 4px;
}

.year-slider {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 4px;
  border-radius: 2px;
  background: var(--color-border);
  outline: none;
  cursor: pointer;
}

.year-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--color-primary);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: transform var(--transition);
}
.year-slider::-webkit-slider-thumb:hover { transform: scale(1.2); }

.year-slider::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--color-primary);
  cursor: pointer;
  border: none;
  box-shadow: var(--shadow-sm);
}

.control-buttons {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.control-btn {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  font-size: 12px;
  font-weight: 500;
  color: var(--color-text-muted);
  transition: all var(--transition);
  white-space: nowrap;
}
.control-btn:hover { background: var(--color-surface-2); color: var(--color-text); }
.control-btn.active {
  background: var(--color-primary);
  color: #fff;
}

.btn-text { pointer-events: none; }

/* Map Container */
.map-container {
  flex: 1;
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

#map-svg {
  width: 100%;
  height: 100%;
}

#map-svg .state-path {
  stroke: var(--color-state-stroke);
  stroke-width: 0.75;
  cursor: pointer;
  transition: opacity 200ms ease;
}

#map-svg .state-path:hover {
  stroke: #fff;
  stroke-width: 1.5;
  filter: brightness(1.1);
}

#map-svg .state-path.dimmed {
  opacity: 0.25;
}

#map-svg .state-path.swing-highlight {
  stroke: #ffd700;
  stroke-width: 2.5;
  filter: drop-shadow(0 0 4px rgba(255,215,0,0.5));
}

#map-svg .county-path {
  stroke: var(--color-state-stroke);
  stroke-width: 0.2;
  cursor: pointer;
}

#map-svg .county-path:hover {
  stroke: #fff;
  stroke-width: 0.8;
  filter: brightness(1.1);
}

#map-svg .state-border {
  fill: none;
  stroke: var(--color-text-faint);
  stroke-width: 1;
  pointer-events: none;
}

/* Tooltip */
.map-tooltip {
  position: absolute;
  background: var(--color-tooltip-bg);
  border: 1px solid var(--color-tooltip-border);
  border-radius: var(--radius-md);
  padding: 10px 14px;
  font-size: 13px;
  pointer-events: none;
  z-index: 50;
  box-shadow: var(--shadow-md);
  max-width: 220px;
  transition: opacity 120ms ease;
}

.tooltip-state {
  font-weight: 700;
  font-size: 14px;
  margin-bottom: 4px;
}

.tooltip-winner { font-size: 12px; margin-bottom: 2px; }
.tooltip-ev { font-size: 12px; color: var(--color-text-muted); }
.tooltip-margin { font-size: 11px; color: var(--color-text-faint); margin-top: 2px; }

/* Legend */
.map-legend {
  position: absolute;
  bottom: 16px;
  left: 16px;
  background: var(--color-tooltip-bg);
  border: 1px solid var(--color-tooltip-border);
  border-radius: var(--radius-md);
  padding: 8px 12px;
  box-shadow: var(--shadow-sm);
  z-index: 10;
}

#legend-items {
  display: flex;
  gap: 12px;
  font-size: 11px;
  flex-wrap: wrap;
}

.legend-entry {
  display: flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
}

.legend-swatch {
  width: 12px;
  height: 12px;
  border-radius: 2px;
  flex-shrink: 0;
}

/* Back button */
.back-to-states {
  position: absolute;
  top: 60px;
  left: 20px;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  font-size: 12px;
  font-weight: 500;
  box-shadow: var(--shadow-sm);
  z-index: 20;
  transition: all var(--transition);
}
.back-to-states:hover {
  background: var(--color-surface-2);
  box-shadow: var(--shadow-md);
}

/* ===== FOOTER ===== */
.app-footer {
  padding: 12px 20px;
  background: var(--color-surface);
  border-top: 1px solid var(--color-border);
  font-size: 11px;
  color: var(--color-text-faint);
  text-align: center;
  line-height: 1.6;
  flex-shrink: 0;
}
.app-footer a { color: var(--color-text-muted); }
.app-footer p + p { margin-top: 4px; }

/* ===== ANIMATION ===== */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.map-container svg { animation: fadeIn 400ms ease; }

/* Playing indicator */
.control-btn.playing {
  background: var(--color-rep);
  color: #fff;
}

/* ===== RESPONSIVE ===== */
@media (max-width: 900px) {
  .main-layout { flex-direction: column; }
  
  .sidebar {
    width: 100%;
    min-width: 100%;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 12px;
    padding: 12px 16px;
    border-right: none;
    border-bottom: 1px solid var(--color-border);
    overflow-y: visible;
    max-height: none;
  }
  
  .sidebar-section { padding-bottom: 0; border-bottom: none; }
  .winner-section { text-align: left; display: flex; align-items: center; gap: 12px; }
  .winner-portrait { margin: 0; width: 40px; height: 40px; }
  .winner-party-badge { width: 14px; height: 14px; }
  .winner-name { font-size: 15px; }
  .winner-label { display: none; }
  
  .stats-section { display: flex; gap: 16px; align-items: center; }
  .stat-row { flex-direction: column; align-items: flex-start; padding: 0; gap: 0; }
  .stat-label { font-size: 10px; }
  .stat-value { font-size: 12px; }
  
  .ec-section { flex: 1; min-width: 200px; }
  .third-section, .note-section { display: none !important; }
  
  .controls-bar { flex-wrap: wrap; padding: 8px 12px; gap: 8px; }
  .slider-container { order: 3; flex-basis: 100%; }
  .btn-text { display: none; }
  .control-btn { padding: 6px 8px; }
  
  .data-attribution { display: none; }
  .app-subtitle { display: none; }
}

@media (max-width: 600px) {
  .stats-section { display: none; }
  .ec-section { display: none; }
  .current-year { font-size: 18px; }
}

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