/* ── reset & base ────────────────────────────────────────────────────────── */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #ffffff;
  color: #0f172a;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* ── loading ─────────────────────────────────────────────────────────────── */
#loading {
  position: fixed;
  inset: 0;
  z-index: 500;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #ffffff;
  gap: 16px;
}

#loading.hidden {
  display: none;
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid #e2e8f0;
  border-top-color: #3b82f6;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

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

.loading-text {
  font-size: 14px;
  color: #64748b;
}

/* ── error banner ────────────────────────────────────────────────────────── */
#error-banner {
  position: fixed;
  inset: 0;
  z-index: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #ffffff;
  padding: 24px;
  text-align: center;
  font-size: 16px;
  font-weight: 500;
  color: #64748b;
}

#error-banner.hidden {
  display: none;
}

/* ── map ─────────────────────────────────────────────────────────────────── */
#map-container {
  position: fixed;
  inset: 0;
  z-index: 0;
  background: #f8fafc;
}

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

#counties-layer path {
  cursor: pointer;
  transition: opacity 120ms;
}

#counties-layer path:active {
  opacity: 0.8;
}

/* ── search bar ──────────────────────────────────────────────────────────── */
#search-bar {
  position: fixed;
  top: 12px;
  left: 12px;
  right: 12px;
  z-index: 100;
}

.search-wrapper {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 50px;
  padding: 0 14px;
  background: #ffffff;
  border-radius: 14px;
  box-shadow:
    0 2px 12px rgba(0, 0, 0, 0.12),
    0 0 0 1px rgba(0, 0, 0, 0.06);
}

.search-icon {
  flex-shrink: 0;
  color: #94a3b8;
}

#search-input {
  flex: 1;
  min-width: 0;
  border: none;
  outline: none;
  background: transparent;
  font-size: 15px;
  color: #0f172a;
  line-height: 1.4;
}

#search-input::placeholder {
  color: #94a3b8;
}

#search-clear {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  flex-shrink: 0;
  background: none;
  border: none;
  cursor: pointer;
  color: #94a3b8;
  border-radius: 8px;
  margin: 0 -8px 0 0;
}

#search-clear:active {
  color: #64748b;
}

#search-clear.hidden {
  display: none;
}

#search-dropdown {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  list-style: none;
  background: #ffffff;
  border-radius: 14px;
  box-shadow:
    0 4px 24px rgba(0, 0, 0, 0.14),
    0 0 0 1px rgba(0, 0, 0, 0.06);
  overflow: hidden;
  max-height: 360px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

#search-dropdown.hidden {
  display: none;
}

#search-dropdown li {
  display: flex;
  align-items: center;
  padding: 0 16px;
  min-height: 50px;
  font-size: 14px;
  color: #0f172a;
  cursor: pointer;
  border-bottom: 1px solid #f1f5f9;
  user-select: none;
  -webkit-user-select: none;
}

#search-dropdown li:last-child {
  border-bottom: none;
}

#search-dropdown li:active {
  background: #f8fafc;
}

/* ── legend ──────────────────────────────────────────────────────────────── */
#legend {
  position: fixed;
  bottom: 14px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 90;
  transition: opacity 200ms ease;
  white-space: nowrap;
}

#legend.hidden {
  opacity: 0;
  pointer-events: none;
}

.legend-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 10px 14px;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-radius: 14px;
  box-shadow:
    0 2px 12px rgba(0, 0, 0, 0.10),
    0 0 0 1px rgba(0, 0, 0, 0.06);
}

.legend-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.legend-label {
  font-size: 11px;
  font-weight: 700;
  color: #0f172a;
  letter-spacing: 0.02em;
}

.legend-swatches {
  display: flex;
  gap: 2px;
}

.swatch {
  width: 20px;
  height: 16px;
  border-radius: 4px;
}

@media (max-width: 380px) {
  .swatch {
    width: 16px;
    height: 14px;
  }
}

.legend-note {
  font-size: 10px;
  color: #64748b;
  text-align: center;
}

/* ── backdrop ────────────────────────────────────────────────────────────── */
#backdrop {
  position: fixed;
  inset: 0;
  z-index: 150;
  background: rgba(15, 23, 42, 0.28);
  backdrop-filter: blur(1px);
  -webkit-backdrop-filter: blur(1px);
}

#backdrop.hidden {
  display: none;
}

/* ── detail sheet ────────────────────────────────────────────────────────── */
#detail-sheet {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 200;
  background: #ffffff;
  border-radius: 20px 20px 0 0;
  box-shadow: 0 -2px 32px rgba(0, 0, 0, 0.14);
  max-height: 70vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  transition: transform 300ms cubic-bezier(0.16, 1, 0.3, 1);
  will-change: transform;
}

#detail-sheet.sheet-closed {
  transform: translateY(100%);
  pointer-events: none;
}

#detail-sheet.sheet-open {
  transform: translateY(0);
  pointer-events: auto;
}

.sheet-handle {
  width: 36px;
  height: 4px;
  border-radius: 2px;
  background: #e2e8f0;
  margin: 12px auto 0;
}

#sheet-close {
  position: sticky;
  top: 0;
  float: right;
  margin: -28px 16px 0 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  background: #f1f5f9;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  color: #64748b;
  z-index: 10;
  flex-shrink: 0;
}

#sheet-close:active {
  background: #e2e8f0;
}

/* ── sheet content ───────────────────────────────────────────────────────── */
#sheet-content {
  padding: 8px 20px 40px;
  clear: both;
}

.sheet-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 18px;
}

.sheet-county {
  font-size: 20px;
  font-weight: 700;
  color: #0f172a;
  line-height: 1.2;
  margin-bottom: 3px;
}

.sheet-state {
  font-size: 13px;
  color: #64748b;
}

.winner-badge {
  flex-shrink: 0;
  font-size: 12px;
  font-weight: 600;
  padding: 5px 11px;
  border-radius: 99px;
  margin-top: 2px;
}

.badge-dem {
  background: #dbeafe;
  color: #1d4ed8;
}

.badge-rep {
  background: #fee2e2;
  color: #b91c1c;
}

.badge-other {
  background: #dcfce7;
  color: #15803d;
}

.vote-table {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 16px;
  padding: 14px;
  background: #f8fafc;
  border-radius: 12px;
}

.vote-row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
}

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

.dot-dem { background: #3b82f6; }
.dot-rep { background: #ef4444; }
.dot-other { background: #16a34a; }

.party-name {
  flex: 1;
  color: #0f172a;
  font-weight: 500;
}

.vote-count {
  color: #0f172a;
  font-size: 13px;
  min-width: 72px;
  text-align: right;
}

.vote-pct {
  color: #64748b;
  font-size: 13px;
  min-width: 42px;
  text-align: right;
}

.margin-row {
  font-size: 14px;
  color: #0f172a;
  padding: 13px 0;
  border-top: 1px solid #e2e8f0;
  margin-bottom: 10px;
}

.year-note {
  font-size: 11px;
  color: #94a3b8;
  font-style: italic;
}

.no-data-msg {
  padding: 28px 0 12px;
  text-align: center;
  color: #64748b;
  font-size: 14px;
}
