/* === DESIGN TOKENS === */
:root {
  --text-xs:   clamp(0.75rem,  0.7rem  + 0.25vw, 0.875rem);
  --text-sm:   clamp(0.875rem, 0.8rem  + 0.35vw, 1rem);
  --text-base: clamp(1rem,     0.95rem + 0.25vw, 1.125rem);
  --text-lg:   clamp(1.125rem, 1rem    + 0.75vw, 1.5rem);
  --text-xl:   clamp(1.5rem,   1.2rem  + 1.25vw, 2.25rem);

  --space-1:  0.25rem;
  --space-2:  0.5rem;
  --space-3:  0.75rem;
  --space-4:  1rem;
  --space-5:  1.25rem;
  --space-6:  1.5rem;
  --space-8:  2rem;
  --space-10: 2.5rem;
  --space-12: 3rem;
  --space-16: 4rem;

  --font-display: 'Cabinet Grotesk', 'Helvetica Neue', sans-serif;
  --font-body: 'General Sans', 'Helvetica Neue', sans-serif;

  --radius-sm: 0.375rem;
  --radius-md: 0.5rem;
  --radius-lg: 0.75rem;
  --radius-xl: 1rem;
  --radius-full: 9999px;

  --transition-interactive: 180ms cubic-bezier(0.16, 1, 0.3, 1);

  /* Political palette */
  --color-rep-bright: #ef4444;
  --color-rep-mid: #dc2626;
  --color-rep-dark: #991b1b;
  --color-dem-bright: #3b82f6;
  --color-dem-mid: #2563eb;
  --color-dem-dark: #1e3a5f;
  --color-ind: #a855f7;
  --color-unopposed: #888;
}

/* LIGHT MODE */
:root, [data-theme="light"] {
  --color-bg: #f0f0f3;
  --color-surface: #ffffff;
  --color-surface-2: #f5f5f7;
  --color-border: #d4d4d8;
  --color-divider: #e4e4e7;
  --color-text: #18181b;
  --color-text-muted: #52525b;
  --color-text-faint: #a1a1aa;
  --color-text-inverse: #fafafa;
  --color-primary: #2563eb;
  --color-primary-hover: #1d4ed8;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.08);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.1);
  --shadow-lg: 0 12px 32px rgba(0,0,0,0.15);
  --map-stroke: #b0b0b8;
  --map-stroke-hover: #18181b;
  --map-bg: #ffffff;
}

/* DARK MODE */
[data-theme="dark"] {
  --color-bg: #0a0a0b;
  --color-surface: #141416;
  --color-surface-2: #1c1c1f;
  --color-border: #27272a;
  --color-divider: #27272a;
  --color-text: #e4e4e7;
  --color-text-muted: #a1a1aa;
  --color-text-faint: #52525b;
  --color-text-inverse: #18181b;
  --color-primary: #3b82f6;
  --color-primary-hover: #60a5fa;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.4);
  --shadow-lg: 0 12px 32px rgba(0,0,0,0.5);
  --map-stroke: #3f3f46;
  --map-stroke-hover: #e4e4e7;
  --map-bg: #141416;
}

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

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

body {
  min-height: 100dvh;
  line-height: 1.6;
  font-family: var(--font-body);
  font-size: var(--text-base);
  color: var(--color-text);
  background-color: var(--color-bg);
}

h1, h2, h3 { font-family: var(--font-display); line-height: 1.15; text-wrap: balance; }
button { cursor: pointer; background: none; border: none; font: inherit; color: inherit; }

a, button, [role="button"], input {
  transition: color var(--transition-interactive),
              background var(--transition-interactive),
              border-color var(--transition-interactive),
              box-shadow var(--transition-interactive);
}

::selection {
  background: rgba(59, 130, 246, 0.25);
  color: var(--color-text);
}

:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 3px;
  border-radius: var(--radius-sm);
}

/* === HEADER === */
header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

.header-inner {
  max-width: 1400px;
  margin: 0 auto;
  padding: var(--space-3) var(--space-6);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo-area {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.logo-icon { color: var(--color-text); }

header h1 {
  font-size: var(--text-lg);
  font-weight: 700;
  letter-spacing: -0.02em;
}

.header-controls button {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-md);
  color: var(--color-text-muted);
}
.header-controls button:hover { color: var(--color-text); background: var(--color-surface-2); }

/* === MAIN === */
main {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 var(--space-6);
}

/* === CONTROLS BAR === */
.controls-bar {
  padding: var(--space-6) 0 var(--space-4);
}

.controls-inner {
  display: flex;
  align-items: center;
  gap: var(--space-8);
  flex-wrap: wrap;
}

.chamber-toggle {
  display: flex;
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  flex-shrink: 0;
}

.chamber-btn {
  padding: var(--space-2) var(--space-6);
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--color-text-muted);
  border-radius: var(--radius-md);
  margin: 3px;
}

.chamber-btn.active {
  background: var(--color-primary);
  color: #fff;
}

.chamber-btn:hover:not(.active) {
  color: var(--color-text);
}

.year-selector {
  display: flex;
  align-items: center;
  gap: var(--space-4);
  flex: 1;
  min-width: 280px;
}

.year-label {
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--color-text-muted);
  white-space: nowrap;
}

.slider-container {
  flex: 1;
  position: relative;
}

#year-slider {
  width: 100%;
  height: 6px;
  -webkit-appearance: none;
  appearance: none;
  background: var(--color-border);
  border-radius: var(--radius-full);
  outline: none;
}

#year-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--color-primary);
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}

#year-slider::-moz-range-thumb {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--color-primary);
  cursor: pointer;
  border: none;
  box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}

.year-ticks {
  display: flex;
  justify-content: space-between;
  padding-top: var(--space-1);
  font-size: 10px;
  color: var(--color-text-faint);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.year-display {
  font-family: var(--font-display);
  font-size: var(--text-xl);
  font-weight: 800;
  min-width: 72px;
  max-width: 72px;
  text-align: center;
  color: var(--color-text);
  font-variant-numeric: tabular-nums;
  flex-shrink: 0;
}

.seat-counts {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  flex-shrink: 0;
  min-width: 110px;
}

.seat-count {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
}

.rep-dot { background: var(--color-rep-bright); }
.dem-dot { background: var(--color-dem-bright); }

.count-label {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  white-space: nowrap;
}

.count-value {
  font-weight: 700;
  font-size: var(--text-lg);
  font-variant-numeric: tabular-nums;
}

/* === MAP === */
.map-section {
  position: relative;
  margin-bottom: var(--space-8);
}

.map-era-label {
  font-family: var(--font-body);
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  text-align: center;
  padding: var(--space-2) var(--space-4);
  letter-spacing: 0.02em;
  display: none;
}

.map-container {
  position: relative;
  background: var(--map-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-xl);
  overflow: hidden;
  box-shadow: var(--shadow-md);
}

#map-svg {
  width: 100%;
  display: block;
  cursor: grab;
}

#map-svg:active {
  cursor: grabbing;
}

/* === LAYER CONTROLS === */
.layer-controls {
  position: absolute;
  top: var(--space-3);
  left: var(--space-3);
  display: flex;
  gap: var(--space-1);
  z-index: 10;
}

.layer-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 5px 10px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  color: var(--color-text-muted);
  font-size: 11px;
  font-weight: 500;
  font-family: var(--font-body);
  box-shadow: var(--shadow-sm);
  cursor: pointer;
  transition: all 0.15s ease;
}

.layer-btn:hover {
  color: var(--color-text);
  background: var(--color-surface-2);
}

.layer-btn.active {
  background: var(--color-primary);
  color: #fff;
  border-color: var(--color-primary);
}

.layer-btn svg {
  flex-shrink: 0;
}

/* === ZOOM CONTROLS === */
.zoom-controls {
  position: absolute;
  top: var(--space-3);
  right: var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  z-index: 10;
}

.zoom-btn {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  color: var(--color-text-muted);
  box-shadow: var(--shadow-sm);
  cursor: pointer;
}

.zoom-btn:hover {
  color: var(--color-text);
  background: var(--color-surface-2);
}

.zoom-reset {
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

.zoom-reset.visible {
  opacity: 1;
  pointer-events: auto;
}

#map-svg .district {
  stroke: var(--map-stroke);
  stroke-width: 0.5;
  cursor: pointer;
  transition: stroke 0.12s ease, opacity 0.12s ease;
}

#map-svg .district:hover {
  stroke: var(--map-stroke-hover);
  stroke-width: 1.5;
  filter: brightness(1.15);
  vector-effect: non-scaling-stroke;
}

#map-svg .district-label {
  font-family: var(--font-body);
  font-size: 9px;
  font-weight: 700;
  fill: #fff;
  text-anchor: middle;
  pointer-events: none;
  paint-order: stroke;
  stroke: rgba(0,0,0,0.85);
  stroke-width: 3px;
  stroke-linejoin: round;
}

/* === TOOLTIP === */
.tooltip {
  position: fixed;
  pointer-events: none;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-3) var(--space-4);
  box-shadow: var(--shadow-lg);
  font-size: var(--text-sm);
  max-width: 280px;
  opacity: 0;
  z-index: 200;
  transition: opacity 0.15s ease;
}

.tooltip.visible { opacity: 1; }

.tooltip-title {
  font-weight: 700;
  margin-bottom: var(--space-1);
  font-size: var(--text-sm);
}

.tooltip-party {
  display: inline-block;
  padding: 2px 8px;
  border-radius: var(--radius-full);
  font-size: 12px;
  font-weight: 600;
  color: #fff;
  margin-bottom: var(--space-2);
}

.tooltip-party.R { background: var(--color-rep-bright); }
.tooltip-party.D { background: var(--color-dem-bright); }
.tooltip-party.I { background: var(--color-ind); }

.tooltip-detail {
  color: var(--color-text-muted);
  font-size: 13px;
  line-height: 1.5;
}

/* === TREND CHART === */
.trend-section {
  margin-bottom: var(--space-8);
}

.trend-section h2 {
  font-size: var(--text-lg);
  font-weight: 700;
  margin-bottom: var(--space-4);
}

.trend-chart-container {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-xl);
  padding: var(--space-6);
  box-shadow: var(--shadow-sm);
}

#trend-svg {
  width: 100%;
  display: block;
}

/* === TABLE === */
.table-section {
  margin-bottom: var(--space-12);
}

.table-section h2 {
  font-size: var(--text-lg);
  font-weight: 700;
  margin-bottom: var(--space-4);
}

.table-controls {
  display: flex;
  gap: var(--space-4);
  margin-bottom: var(--space-4);
  flex-wrap: wrap;
}

#district-search {
  padding: var(--space-2) var(--space-4);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface);
  color: var(--color-text);
  font-size: var(--text-sm);
  width: 260px;
}

#district-search::placeholder { color: var(--color-text-faint); }
#district-search:focus { border-color: var(--color-primary); }

.filter-btns {
  display: flex;
  gap: var(--space-1);
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 3px;
}

.filter-btn {
  padding: var(--space-1) var(--space-3);
  font-size: var(--text-xs);
  font-weight: 500;
  border-radius: var(--radius-md);
  color: var(--color-text-muted);
}

.filter-btn.active {
  background: var(--color-surface);
  color: var(--color-text);
  box-shadow: var(--shadow-sm);
}

.filter-btn:hover:not(.active) { color: var(--color-text); }

.table-wrapper {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-xl);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  max-height: 600px;
  overflow-y: auto;
}

table {
  border-collapse: collapse;
  width: 100%;
}

thead {
  position: sticky;
  top: 0;
  z-index: 10;
}

th {
  background: var(--color-surface-2);
  padding: var(--space-3) var(--space-4);
  text-align: left;
  font-size: var(--text-xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-text-muted);
  border-bottom: 1px solid var(--color-border);
}

td {
  padding: var(--space-2) var(--space-4);
  font-size: var(--text-sm);
  border-bottom: 1px solid var(--color-divider);
  font-variant-numeric: tabular-nums;
}

tbody tr:hover { background: var(--color-surface-2); }

.party-badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: var(--radius-full);
  font-size: 12px;
  font-weight: 600;
  color: #fff;
}

.party-badge.R { background: var(--color-rep-bright); }
.party-badge.D { background: var(--color-dem-bright); }
.party-badge.I { background: var(--color-ind); }

.flip-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: var(--radius-full);
  font-size: 11px;
  font-weight: 600;
}

.flip-badge.flip-r { background: rgba(239,68,68,0.15); color: var(--color-rep-bright); }
.flip-badge.flip-d { background: rgba(59,130,246,0.15); color: var(--color-dem-bright); }
.flip-badge.hold { background: var(--color-surface-2); color: var(--color-text-faint); }
.flip-badge.not-up { background: var(--color-surface-2); color: var(--color-text-faint); font-style: italic; }

/* === OFF-CYCLE TABLE ROWS === */
.off-cycle-row { opacity: 0.45; }
.off-cycle-row:hover { opacity: 0.7; }

/* === OFF-CYCLE MAP LEGEND === */
.map-offcycle-legend {
  display: none;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  font-size: var(--text-xs);
  color: var(--color-text-faint);
  padding: var(--space-2) var(--space-4);
  margin-top: var(--space-1);
}

.offcycle-icon {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 3px;
  background: repeating-linear-gradient(
    45deg,
    var(--color-rep-bright),
    var(--color-rep-bright) 2px,
    rgba(0,0,0,0.3) 2px,
    rgba(0,0,0,0.3) 4px
  );
  flex-shrink: 0;
}

/* === TOOLTIP OFF-CYCLE === */
.tooltip-offcycle {
  font-size: 11px;
  font-weight: 600;
  color: var(--color-text-faint);
  font-style: italic;
  margin-bottom: var(--space-2);
  padding-bottom: var(--space-1);
  border-bottom: 1px solid var(--color-divider);
}

/* === FOOTER === */
footer {
  padding: var(--space-6) var(--space-6) var(--space-8);
  text-align: center;
  font-size: var(--text-xs);
  color: var(--color-text-faint);
  border-top: 1px solid var(--color-divider);
}

footer a {
  color: var(--color-text-muted);
  text-decoration: none;
}

footer a:hover { color: var(--color-primary); }

.footer-sep { margin: 0 var(--space-2); }

/* === RESPONSIVE === */
@media (max-width: 768px) {
  .controls-inner {
    flex-direction: column;
    align-items: stretch;
    gap: var(--space-4);
  }

  .year-selector {
    flex-direction: column;
    align-items: stretch;
    gap: var(--space-2);
  }

  .year-display {
    text-align: left;
  }

  .seat-counts {
    justify-content: center;
  }

  .table-controls {
    flex-direction: column;
  }

  #district-search { width: 100%; }

  header h1 { font-size: var(--text-base); }

  /* ── Mobile table: hide Margin, truncate long winner names ── */
  .col-margin { display: none; }

  .col-winner {
    max-width: 120px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* Tighten cell padding on mobile so columns breathe */
  td, th {
    padding-left: var(--space-2);
    padding-right: var(--space-2);
  }
}

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

.map-container { animation: fadeIn 0.4s ease; }

/* Scrollbar */
.table-wrapper::-webkit-scrollbar { width: 6px; }
.table-wrapper::-webkit-scrollbar-track { background: transparent; }
.table-wrapper::-webkit-scrollbar-thumb {
  background: var(--color-border);
  border-radius: var(--radius-full);
}
.table-wrapper::-webkit-scrollbar-thumb:hover { background: var(--color-text-faint); }

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