/* ============================================================
   NZ Election Results — Design System
   Dark premium theme, glassmorphism, MapLibre overrides
   ============================================================ */

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');

/* ── Tokens ──────────────────────────────────────────────── */
:root {
  --bg-0:   #0d1117;
  --bg-1:   #161b22;
  --bg-2:   #21262d;
  --bg-3:   #30363d;
  --border: rgba(48, 54, 61, 0.8);
  --text-0: #e6edf3;
  --text-1: #8b949e;
  --text-2: #6e7681;
  --blue:   #3b82f6;
  --teal:   #14b8a6;
  --red:    #f87171;
  --glass:        rgba(13, 17, 23, 0.82);
  --glass-border: rgba(48, 54, 61, 0.55);
  --navbar-h: 60px;
  --sidebar-w: 390px;
  --radius: 10px;
  --easing: cubic-bezier(0.4, 0, 0.2, 1);
}

/* ── Reset ───────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }
body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 14px;
  background: var(--bg-0);
  color: var(--text-0);
  -webkit-font-smoothing: antialiased;
}
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); }

/* ── Navbar ──────────────────────────────────────────────── */
#navbar {
  position: fixed;
  inset: 0 0 auto 0;
  height: var(--navbar-h);
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 18px;
  background: var(--glass);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-bottom: 1px solid var(--glass-border);
  z-index: 200;
}

/* Logo */
.logo {
  display: flex;
  align-items: center;
  gap: 9px;
  flex-shrink: 0;
  text-decoration: none;
}
.logo-icon { width: 30px; height: 30px; flex-shrink: 0; }
.logo-text {
  font-size: 15px;
  font-weight: 700;
  color: var(--text-0);
  white-space: nowrap;
  letter-spacing: -0.01em;
}
.logo-accent { color: var(--teal); }

/* Separator */
.nav-sep {
  width: 1px;
  height: 24px;
  background: var(--border);
  flex-shrink: 0;
}

/* Year pills */
.year-pills { display: flex; gap: 5px; flex-shrink: 0; }
.year-pill {
  padding: 5px 13px;
  border-radius: 100px;
  border: 1px solid var(--border);
  background: var(--bg-2);
  color: var(--text-1);
  font-size: 12.5px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.18s var(--easing);
  white-space: nowrap;
  line-height: 1;
}
.year-pill:hover {
  border-color: var(--blue);
  color: var(--blue);
  background: rgba(59, 130, 246, 0.08);
}
.year-pill.active {
  background: var(--blue);
  border-color: var(--blue);
  color: #fff;
  font-weight: 600;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.25), 0 2px 8px rgba(59, 130, 246, 0.4);
}

/* Electorate, Party & By-election select */
.electorate-wrap, .party-leaning-wrap, .byelection-wrap { flex: 1; min-width: 0; max-width: 220px; }
#electorate-select, #party-leaning-select, #byelection-select {
  width: 100%;
  padding: 6px 32px 6px 11px;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-0);
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  outline: none;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%238b949e' stroke-width='2.5' stroke-linecap='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 9px center;
  transition: border-color 0.15s;
}
#electorate-select:focus, #party-leaning-select:focus, #byelection-select:focus { border-color: var(--blue); }
#electorate-select option, #party-leaning-select option, #byelection-select option, #byelection-select optgroup { background: var(--bg-1); }

/* Stats badge */
.stats-badge {
  margin-left: auto;
  padding: 4px 12px;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 100px;
  font-size: 12px;
  color: var(--text-1);
  white-space: nowrap;
  flex-shrink: 0;
}
.stats-badge strong { color: var(--teal); font-weight: 600; }

/* ── Top Leanings Floating Panel ─────────────────────────── */
.top-leanings-panel {
  position: fixed;
  top: calc(var(--navbar-h) + 12px);
  left: 14px;
  width: 320px;
  max-height: calc(100vh - var(--navbar-h) - 80px);
  background: var(--glass);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius);
  z-index: 180;
  display: flex;
  flex-direction: column;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
  transition: opacity 0.25s var(--easing), transform 0.25s var(--easing);
  overflow: hidden;
}
.top-leanings-panel.hidden {
  opacity: 0;
  pointer-events: none;
  transform: translateY(-10px);
}

.top-leanings-header {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.top-leanings-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-0);
}

.top-leanings-order {
  background: var(--bg-2);
  border: 1px solid var(--border);
  color: var(--teal);
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  padding: 3px 8px;
  border-radius: 100px;
  transition: all 0.15s;
}
.top-leanings-order:hover {
  background: var(--bg-3);
  border-color: var(--teal);
}

.top-leanings-close {
  background: transparent;
  border: none;
  color: var(--text-1);
  font-size: 14px;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
}
.top-leanings-close:hover { background: var(--bg-2); color: var(--text-0); }

.top-leanings-list {
  overflow-y: auto;
  padding: 8px;
}

.top-leaning-item {
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 3px;
  transition: background 0.15s;
  border: 1px solid transparent;
}
.top-leaning-item:hover {
  background: var(--bg-2);
  border-color: var(--border);
}

.top-leaning-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.top-leaning-name {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-0);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.top-leaning-pct {
  font-size: 12px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  flex-shrink: 0;
}

.top-leaning-sub {
  font-size: 10.5px;
  color: var(--text-1);
  display: flex;
  align-items: center;
  gap: 6px;
}

/* ── Map ─────────────────────────────────────────────────── */
#map {
  position: fixed;
  top: var(--navbar-h);
  inset-inline: 0;
  bottom: 0;
}

/* ── Map loading overlay ─────────────────────────────────── */
.map-loading {
  position: fixed;
  top: var(--navbar-h);
  inset-inline: 0;
  bottom: 0;
  background: rgba(13, 17, 23, 0.65);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  z-index: 150;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  font-size: 13px;
  color: var(--text-1);
  transition: opacity 0.3s var(--easing);
}
.map-loading.hidden { opacity: 0; pointer-events: none; }

.spinner-ring {
  width: 34px;
  height: 34px;
  border: 3px solid var(--bg-3);
  border-top-color: var(--blue);
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Sidebar ─────────────────────────────────────────────── */
#sidebar {
  position: fixed;
  top: var(--navbar-h);
  right: 0;
  width: var(--sidebar-w);
  height: calc(100dvh - var(--navbar-h));
  background: var(--glass);
  backdrop-filter: blur(28px);
  -webkit-backdrop-filter: blur(28px);
  border-left: 1px solid var(--glass-border);
  z-index: 100;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.32s var(--easing), box-shadow 0.32s var(--easing);
  overflow: hidden;
}
#sidebar.open {
  transform: translateX(0);
  box-shadow: -12px 0 48px rgba(0, 0, 0, 0.5);
}

/* Sidebar header */
.sidebar-header {
  padding: 18px 18px 14px;
  border-bottom: 1px solid var(--border);
  position: relative;
  flex-shrink: 0;
}
.sidebar-close {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 7px;
  color: var(--text-1);
  cursor: pointer;
  transition: all 0.15s;
}
.sidebar-close svg { width: 13px; height: 13px; }
.sidebar-close:hover { background: var(--bg-3); color: var(--text-0); }

.place-name {
  font-size: 15px;
  font-weight: 700;
  line-height: 1.3;
  color: var(--text-0);
  padding-right: 40px;
  margin-bottom: 2px;
  letter-spacing: -0.01em;
}

.place-subtitle {
  font-size: 12px;
  color: var(--text-1);
  padding-right: 40px;
  margin-bottom: 8px;
  line-height: 1.3;
}

.place-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.place-electorates {
  font-size: 12px;
  color: var(--text-1);
}
.place-year-badge {
  padding: 2px 9px;
  border-radius: 100px;
  font-size: 11px;
  font-weight: 600;
  background: rgba(59, 130, 246, 0.12);
  color: var(--blue);
  border: 1px solid rgba(59, 130, 246, 0.2);
}

/* Sidebar scrollable body */
.sidebar-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  scrollbar-width: thin;
  scrollbar-color: var(--bg-3) transparent;
}
.sidebar-body::-webkit-scrollbar { width: 4px; }
.sidebar-body::-webkit-scrollbar-thumb { background: var(--bg-3); border-radius: 4px; }

/* ── Result sections ─────────────────────────────────────── */
.results-section { margin-bottom: 24px; }

.section-title {
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--text-2);
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.section-title::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

/* Individual result row */
.result-row { margin-bottom: 12px; }
.result-top {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 5px;
}

.result-name-col {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.result-title {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-0);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 1.25;
}

.result-subtitle {
  font-size: 10.5px;
  color: var(--text-1);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 1.25;
  margin-top: 1px;
}

.result-chip {
  flex-shrink: 0;
  font-size: 9.5px;
  font-weight: 700;
  letter-spacing: 0.03em;
  padding: 1.5px 5px;
  border-radius: 4px;
  white-space: nowrap;
  margin-top: 1px;
}

.pct-change {
  flex-shrink: 0;
  font-weight: 600;
  font-size: 10px;
  padding: 1.5px 5px;
  border-radius: 4px;
  font-variant-numeric: tabular-nums;
  margin-top: 1px;
  line-height: 1.2;
}
.pct-change.pos {
  color: #34d399;
  background: rgba(52, 211, 153, 0.14);
}
.pct-change.neg {
  color: #f87171;
  background: rgba(248, 113, 113, 0.14);
}
.pct-change.neutral {
  color: var(--text-2);
  background: rgba(156, 163, 175, 0.1);
}

.result-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  flex-shrink: 0;
  gap: 1px;
}

.result-votes {
  font-size: 11.5px;
  color: var(--text-0);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  text-align: right;
  white-space: nowrap;
}

.result-meta-line {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 10.5px;
  color: var(--text-1);
  font-variant-numeric: tabular-nums;
}

.pct-change {
  display: inline-block;
  font-weight: 600;
  font-size: 9.5px;
  padding: 0.5px 4px;
  border-radius: 3px;
  font-variant-numeric: tabular-nums;
}
.pct-change.pos {
  color: #34d399;
  background: rgba(52, 211, 153, 0.14);
}
.pct-change.neg {
  color: #f87171;
  background: rgba(248, 113, 113, 0.14);
}
.pct-change.neutral {
  color: var(--text-2);
  background: rgba(156, 163, 175, 0.1);
}

.result-bar-track {
  height: 5px;
  background: var(--bg-2);
  border-radius: 3px;
  overflow: hidden;
  margin-top: 3px;
}
.result-bar-fill {
  height: 100%;
  border-radius: 3px;
  width: 0;
  transition: width 0.5s cubic-bezier(0.4, 0, 0.2, 1);
}

/* ── Sidebar empty/loading states ────────────────────────── */
.sidebar-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 48px 24px;
  color: var(--text-1);
  font-size: 13px;
  text-align: center;
}
.sidebar-state .spinner-ring { width: 26px; height: 26px; border-width: 2px; }
.sidebar-error { color: var(--red); }

/* ── Electorate sub-header (for multi-electorate places) ─── */
.electorate-group-header {
  font-size: 11px;
  font-weight: 600;
  color: var(--teal);
  letter-spacing: 0.04em;
  margin: 14px 0 6px;
  padding-bottom: 4px;
  border-bottom: 1px solid rgba(20, 184, 166, 0.2);
}

/* ── Legend ──────────────────────────────────────────────── */
#legend {
  position: fixed;
  bottom: 24px;
  left: 54px; /* to the right of MapLibre navigation controls */
  background: var(--glass);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius);
  padding: 10px 14px;
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 5px;
  pointer-events: none;
}
.legend-title {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-2);
  margin-bottom: 3px;
}
.legend-row {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 11.5px;
  color: var(--text-1);
}
.legend-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
  border: 1.5px solid rgba(255,255,255,0.3);
}

/* ── MapLibre GL overrides ───────────────────────────────── */
.maplibregl-ctrl-bottom-left { bottom: 28px; left: 10px; }
.maplibregl-ctrl-top-left { top: 76px; left: 10px; } /* below navbar */
.maplibregl-ctrl-attrib {
  background: rgba(13, 17, 23, 0.75) !important;
  color: rgba(110, 118, 129, 0.9) !important;
  font-size: 10px !important;
  border-radius: 5px !important;
}
.maplibregl-ctrl-attrib a { color: rgba(59, 130, 246, 0.8) !important; }
.maplibregl-ctrl-group {
  background: var(--bg-1) !important;
  border: 1px solid var(--border) !important;
  border-radius: 8px !important;
  overflow: hidden;
}
.maplibregl-ctrl-group button {
  background: transparent !important;
  border-color: var(--border) !important;
}
.maplibregl-ctrl-group button:hover { background: var(--bg-2) !important; }
.maplibregl-ctrl-icon { filter: invert(0.6); }

/* ── Mobile ──────────────────────────────────────────────── */
@media (max-width: 768px) {
  :root { --sidebar-w: 100vw; }

  #navbar {
    height: auto;
    min-height: var(--navbar-h);
    flex-wrap: wrap;
    gap: 10px;
    padding: 10px 14px;
  }

  .year-pills { overflow-x: auto; padding-bottom: 2px; }
  .electorate-wrap { max-width: none; width: 100%; }
  .stats-badge { display: none; }

  #map { top: auto; position: fixed; inset: 0; }

  #sidebar {
    top: auto;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 60vh;
    transform: translateY(100%);
    border-left: none;
    border-top: 1px solid var(--glass-border);
    border-radius: 16px 16px 0 0;
  }
  #sidebar.open {
    transform: translateY(0);
    box-shadow: 0 -12px 48px rgba(0, 0, 0, 0.5);
  }

  #legend { display: none; }
}

/* ── Non-geographic results floating button & panel ── */
.nonmap-btn {
  position: fixed;
  bottom: 24px;
  right: 24px;
  padding: 8px 16px;
  height: 38px;
  border-radius: 100px;
  background: var(--glass);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--glass-border);
  color: var(--text-1);
  cursor: pointer;
  z-index: 170;
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  transition: all 0.25s var(--easing);
}
.nonmap-btn:hover {
  border-color: var(--teal);
  color: var(--text-0);
  background: var(--bg-2);
  transform: translateY(-1px);
  box-shadow: 0 6px 20px rgba(20, 184, 166, 0.15), 0 10px 24px rgba(0, 0, 0, 0.4);
}
.nonmap-btn:active {
  transform: scale(0.97);
}
.nonmap-btn svg {
  width: 16px;
  height: 16px;
}
.nonmap-btn-text {
  font-size: 12px;
  font-weight: 500;
}
.nonmap-btn.hidden {
  opacity: 0;
  pointer-events: none;
  transform: translateY(10px);
}

.nonmap-panel {
  position: fixed;
  bottom: 96px;
  right: 28px;
  width: 440px;
  max-height: calc(100vh - 160px);
  background: var(--glass);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius);
  z-index: 180;
  display: flex;
  flex-direction: column;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
  transition: all 0.25s var(--easing);
  overflow: hidden;
}
.nonmap-panel.hidden {
  opacity: 0;
  pointer-events: none;
  transform: translateY(15px) scale(0.98);
}

.nonmap-panel-header {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.nonmap-panel-title-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
}
.nonmap-panel-title {
  font-size: 15px;
  font-weight: 700;
  color: var(--text-0);
  letter-spacing: -0.01em;
}
.nonmap-year-badge {
  padding: 2px 8px;
  background: rgba(20, 184, 166, 0.15);
  border: 1px solid rgba(20, 184, 166, 0.3);
  color: var(--teal);
  font-size: 11px;
  font-weight: 600;
  border-radius: 100px;
}
.nonmap-panel-close {
  background: transparent;
  border: none;
  color: var(--text-1);
  font-size: 16px;
  cursor: pointer;
  padding: 4px;
  transition: color 0.15s;
}
.nonmap-panel-close:hover {
  color: var(--text-0);
}

.nonmap-panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* ── Group Cards ── */
.nonmap-card {
  background: rgba(22, 27, 34, 0.6);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.nonmap-card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
}
.nonmap-card-name-wrap {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.nonmap-card-name {
  font-size: 13.5px;
  font-weight: 600;
  color: var(--text-0);
}
.nonmap-card-electorate {
  font-size: 11px;
  color: var(--text-1);
}

/* Type badges */
.nonmap-type-badge {
  padding: 2px 8px;
  font-size: 10.5px;
  font-weight: 600;
  border-radius: 100px;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  white-space: nowrap;
}
.nonmap-type-badge.prison { background: rgba(239, 68, 68, 0.15); border: 1px solid rgba(239, 68, 68, 0.3); color: #f87171; }
.nonmap-type-badge.hospital { background: rgba(168, 85, 247, 0.15); border: 1px solid rgba(168, 85, 247, 0.3); color: #c084fc; }
.nonmap-type-badge.marae { background: rgba(249, 115, 22, 0.15); border: 1px solid rgba(249, 115, 22, 0.3); color: #fb923c; }
.nonmap-type-badge.aged-care { background: rgba(34, 197, 94, 0.15); border: 1px solid rgba(34, 197, 94, 0.3); color: #4ade80; }
.nonmap-type-badge.mobile { background: rgba(59, 130, 246, 0.15); border: 1px solid rgba(59, 130, 246, 0.3); color: #60a5fa; }
.nonmap-type-badge.special { background: rgba(107, 114, 128, 0.15); border: 1px solid rgba(107, 114, 128, 0.3); color: #9ca3af; }

/* Tabs inside the card */
.nonmap-tabs {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid rgba(48, 54, 61, 0.5);
  padding-bottom: 4px;
}
.nonmap-tab {
  background: transparent;
  border: none;
  color: var(--text-2);
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  transition: all 0.15s;
}
.nonmap-tab:hover {
  color: var(--text-1);
}
.nonmap-tab.active {
  color: var(--teal);
  background: rgba(20, 184, 166, 0.08);
}

.nonmap-results-area {
  min-height: 40px;
}

@media (max-width: 768px) {
  .nonmap-panel {
    bottom: 0;
    right: 0;
    width: 100vw;
    height: 70vh;
    max-height: 70vh;
    border-radius: 16px 16px 0 0;
    border-left: none;
    border-right: none;
    border-bottom: none;
    transform: translateY(100%);
  }
  .nonmap-panel.open {
    transform: translateY(0);
  }
}

