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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: #f0f0f0;
}

/* ── Header ── */
#header {
  background: #1a1a2e;
  color: #fff;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 14px;
  flex-shrink: 0;
  position: relative;
  z-index: 2000;
  box-shadow: 0 2px 6px rgba(0,0,0,0.3);
}

#header h1 {
  font-size: 0.95rem;
  font-weight: 600;
  white-space: nowrap;
  letter-spacing: 0.01em;
}

/* ── Results / Turnout toggle ── */
#view-toggle {
  display: flex;
  flex-shrink: 0;
}

.view-btn {
  background: rgba(255,255,255,0.12);
  color: rgba(255,255,255,0.85);
  border: 1px solid rgba(255,255,255,0.25);
  padding: 5px 12px;
  font-size: 0.8rem;
  cursor: pointer;
  white-space: nowrap;
}

.view-btn:first-child { border-radius: 4px 0 0 4px; }
.view-btn:last-child  { border-radius: 0 4px 4px 0; border-left: none; }

.view-btn.active {
  background: #fff;
  color: #1a1a2e;
}

/* ── Tom Select combobox (race picker) ── */
#header .ts-wrapper {
  flex: 1;
  max-width: 500px;
}

#header .ts-control {
  border: none;
  border-radius: 5px;
  padding: 5px 10px;
  font-size: 0.85rem;
  background: #fff;
  box-shadow: none;
  min-height: unset;
  cursor: text;
}

#header .ts-control input {
  font-size: 0.85rem;
  color: #222;
}

#header .ts-dropdown {
  font-size: 0.85rem;
  border-radius: 5px;
  border: 1px solid #ddd;
  box-shadow: 0 4px 16px rgba(0,0,0,0.18);
  z-index: 3000;
}

#header .ts-dropdown .option {
  padding: 8px 12px;
  color: #222;
}

#header .ts-dropdown .option:hover,
#header .ts-dropdown .option.active {
  background: #1a1a2e;
  color: #fff;
}

/* ── Extra controls wrapper (view toggle + export) ── */
#extra-controls {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-shrink: 0;
}

/* ── Controls expand button (mobile only) ── */
#controls-toggle {
  display: none;
  background: rgba(255,255,255,0.12);
  color: rgba(255,255,255,0.85);
  border: 1px solid rgba(255,255,255,0.25);
  border-radius: 4px;
  padding: 4px 10px;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
  letter-spacing: 0.05em;
}

#controls-toggle.active {
  background: #fff;
  color: #1a1a2e;
  border-color: #fff;
}

/* ── Export button ── */
#export-btn {
  background: rgba(255,255,255,0.12);
  color: rgba(255,255,255,0.85);
  border: 1px solid rgba(255,255,255,0.25);
  border-radius: 4px;
  padding: 5px 12px;
  font-size: 0.8rem;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
}

#export-btn:hover:not(:disabled) {
  background: rgba(255,255,255,0.22);
}

#export-btn:disabled {
  opacity: 0.55;
  cursor: default;
}

/* ── Map container ── */
#map-container {
  flex: 1;
  position: relative;
  overflow: hidden;
}

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

/* ── Results panel ── */
#results-panel {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 270px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 14px rgba(0,0,0,0.18);
  padding: 14px 16px;
  z-index: 900;
  max-height: calc(100% - 24px);
  overflow-y: auto;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
}

#panel-toggle {
  display: none;
  background: none;
  border: none;
  cursor: pointer;
  width: calc(100% + 32px);  /* break out of panel's 16px side padding */
  margin: -14px -16px 0;
  padding: 14px 0 12px;
  border-radius: 12px 12px 0 0;
}

.grab-pill {
  display: block;
  width: 40px;
  height: 4px;
  background: #d0d0d0;
  border-radius: 2px;
  margin: 0 auto;
  transition: background 0.15s;
}

#panel-toggle:active .grab-pill {
  background: #aaa;
}

.panel-eyebrow {
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: #999;
  margin-bottom: 6px;
}

.panel-race-name {
  font-size: 0.88rem;
  font-weight: 600;
  color: #1a1a2e;
  margin-bottom: 14px;
  line-height: 1.35;
}

.cand-row {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  margin-bottom: 10px;
}

.cand-swatch {
  width: 11px;
  height: 11px;
  border-radius: 2px;
  margin-top: 2px;
  flex-shrink: 0;
}

.cand-body { flex: 1; min-width: 0; }

.cand-name {
  font-size: 0.8rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: #222;
}

.cand-bar-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 3px;
}

.cand-bar-bg {
  flex: 1;
  height: 5px;
  background: #ebebeb;
  border-radius: 3px;
  overflow: hidden;
}

.cand-bar-fill {
  height: 100%;
  border-radius: 3px;
  transition: width 0.35s ease;
}

.cand-pct {
  font-size: 0.72rem;
  color: #555;
  min-width: 36px;
  text-align: right;
}

.cand-votes {
  font-size: 0.68rem;
  color: #999;
  margin-top: 1px;
}

.panel-totals {
  font-size: 0.72rem;
  color: #777;
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid #eee;
  line-height: 1.7;
}

/* ── Legend ── */
#legend {
  position: absolute;
  bottom: 28px;
  left: 12px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 14px rgba(0,0,0,0.18);
  padding: 10px 14px;
  z-index: 900;
  min-width: 170px;
}

.legend-eyebrow {
  font-size: 0.67rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: #999;
  margin-bottom: 8px;
}

.legend-entry {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.legend-gradient {
  width: 72px;
  height: 9px;
  border-radius: 2px;
  flex-shrink: 0;
}

.legend-label {
  font-size: 0.7rem;
  color: #555;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 120px;
}

.legend-caption {
  font-size: 0.63rem;
  color: #aaa;
  margin-top: 5px;
}

/* ── Map attribution ── */
#attribution {
  position: absolute;
  bottom: 6px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.65rem;
  color: rgba(0,0,0,0.45);
  background: rgba(255,255,255,0.7);
  padding: 2px 8px;
  border-radius: 3px;
  white-space: nowrap;
  z-index: 900;
  pointer-events: auto;
}

#attribution a {
  color: inherit;
  text-decoration: underline;
}

/* ── Leaflet tooltip overrides ── */
.leaflet-tooltip {
  padding: 0 !important;
  background: transparent !important;
  border: none !important;
  box-shadow: none !important;
}

.leaflet-tooltip::before { display: none !important; }

.tip-box {
  background: #fff;
  border-radius: 7px;
  padding: 10px 13px;
  box-shadow: 0 3px 12px rgba(0,0,0,0.22);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 0.78rem;
  min-width: 190px;
  max-width: 240px;
}

.tip-precinct {
  font-weight: 700;
  color: #1a1a2e;
  margin-bottom: 2px;
}

.tip-race {
  font-size: 0.67rem;
  color: #aaa;
  margin-bottom: 8px;
  line-height: 1.3;
}

.tip-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 4px;
}

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

.tip-name { flex: 1; }

.tip-votes { color: #444; font-variant-numeric: tabular-nums; }

.tip-pct { color: #999; font-size: 0.7rem; min-width: 36px; text-align: right; }

.tip-footer {
  margin-top: 7px;
  padding-top: 6px;
  border-top: 1px solid #eee;
  color: #aaa;
  font-size: 0.68rem;
  line-height: 1.6;
}

/* ── Tooltip states ── */
.tip-nodata {
  color: #bbb;
  font-size: 0.75rem;
}

.tip-tied {
  color: #9b7fc4;
  font-size: 0.72rem;
  font-weight: 600;
  margin-bottom: 6px;
}

/* ── Loading overlay ── */
#loading {
  position: absolute;
  inset: 0;
  background: rgba(255,255,255,0.92);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  color: #444;
  font-size: 0.9rem;
  gap: 14px;
}

#loading.hidden { display: none; }

.spinner {
  width: 38px;
  height: 38px;
  border: 4px solid #e0e0e0;
  border-top-color: #1a1a2e;
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ── Error overlay ── */
.error-box {
  text-align: center;
  padding: 24px;
  max-width: 380px;
}

.error-title {
  font-size: 1.1rem;
  color: #c00;
  margin-bottom: 10px;
}

.error-msg {
  font-size: 0.85rem;
  color: #666;
  margin-bottom: 16px;
}

.error-hint {
  font-size: 0.8rem;
  color: #999;
}

.error-code {
  background: #f4f4f4;
  padding: 2px 6px;
  border-radius: 3px;
}

/* ── Mobile layout ── */
@media (max-width: 620px) {
  #header {
    flex-wrap: wrap;
    align-content: flex-start;
  }

  #header h1 {
    flex-basis: 100%;
    font-size: 0.8rem;
  }

  #header .ts-wrapper { max-width: 100%; flex: 1; }

  #controls-toggle { display: block; }

  #extra-controls {
    display: none;
    flex-basis: 100%;
    padding-bottom: 4px;
  }

  #extra-controls.open { display: flex; }

  #panel-toggle {
    display: block;
  }

  #results-panel {
    top: auto;
    right: 0;
    bottom: 0;
    left: 0;
    width: 100%;
    border-radius: 12px 12px 0 0;
    max-height: 42vh;
  }

  #results-panel {
    transition: transform 0.4s cubic-bezier(0.32,0.72,0,1);
  }

  #legend { display: none; }
  #attribution { display: none; }
}
