:root {
    color-scheme: light;
    --bg:       #f0f2f5;
    --surface:  #ffffff;
    --surface2: #f7f8fa;
    --border:   #dde1e8;
    --accent:   #2563eb;
    --text:     #111827;
    --muted:    #6b7280;
    --shadow:   0 1px 4px rgba(0,0,0,0.09);
    --lll:    #272e7a;
    --buhain: #cc0202;
    --others: #696969;
    --t1: #eab308;
    --t2: #f97316;
    --t3: #ef4444;
  }
  html.dark {
    color-scheme: dark;
    --bg:       #0f1117;
    --surface:  #1a1d27;
    --surface2: #222533;
    --border:   #2e3347;
    --accent:   #4f7cff;
    --text:     #e2e8f0;
    --muted:    #8892a4;
    --shadow:   0 1px 4px rgba(0,0,0,0.4);
  }
  #map, .map-legend, .leaflet-tile { color-scheme: light !important; }
  * { box-sizing: border-box; margin: 0; padding: 0; }
  body {
    font-family: 'Segoe UI', system-ui, sans-serif;
    background: var(--bg); color: var(--text);
    height: 100vh; display: flex; flex-direction: column; overflow: hidden;
  }

  header {
    background: var(--surface); border-bottom: 1px solid var(--border);
    padding: 0 16px; height: 46px;
    display: flex; align-items: center; gap: 14px;
    flex-shrink: 0; box-shadow: var(--shadow); z-index: 1000;
  }
  header h1 { font-size: 14px; font-weight: 700; color: var(--text); }

  .hdr-filters { display: flex; align-items: center; gap: 8px; margin-left: 16px; }
  .hdr-sep { width: 1px; height: 20px; background: var(--border); }

  select.hsel {
    background: var(--surface2); border: 1px solid var(--border); border-radius: 6px;
    padding: 4px 24px 4px 8px; font-size: 12px; color: var(--text);
    appearance: none; -webkit-appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='11' height='11' viewBox='0 0 24 24' fill='none' stroke='%236b7280' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
    background-repeat: no-repeat; background-position: right 7px center;
    cursor: pointer; outline: none; min-width: 130px;
  }
  select.hsel:focus { border-color: var(--accent); }

  .mode-toggle { display: flex; background: var(--surface2); border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
  .mode-btn {
    padding: 4px 12px; font-size: 11px; font-weight: 700; letter-spacing: 0.5px;
    border: none; background: none; cursor: pointer; color: var(--muted); transition: all 0.15s;
  }
  .mode-btn.active { background: var(--accent); color: #fff; }

  .hdr-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }
  .load-badge { font-size: 10px; color: var(--muted); }

  .body { display: flex; flex: 1; overflow: hidden; }

  .stats-panel {
    width: 380px; flex-shrink: 0;
    background: var(--surface); border-right: 1px solid var(--border);
    display: flex; flex-direction: column; overflow-y: auto;
    box-shadow: 2px 0 6px rgba(0,0,0,0.04);
    transition: transform 0.25s ease, width 0.25s ease;
  }
  .stats-panel::-webkit-scrollbar { width: 4px; }
  .stats-panel::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

  .sp-section { padding: 12px 14px; border-bottom: 1px solid var(--border); }
  #spStatsSection { overflow-y: auto; flex-shrink: 0; }
  .sp-title {
    font-size: 9px; font-weight: 800; letter-spacing: 2px; text-transform: uppercase;
    color: var(--muted); margin-bottom: 10px;
  }
  .stat-row {
    display: flex; align-items: center; justify-content: space-between;
    padding: 5px 0; border-bottom: 1px solid var(--surface2);
  }
  .stat-row:last-child { border-bottom: none; }
  .stat-row-left { display: flex; align-items: center; gap: 7px; }
  .stat-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
  .stat-label { font-size: 12px; color: var(--text); }
  .stat-val { font-size: 13px; font-weight: 700; color: var(--text); }
  .stat-pct { font-size: 10px; color: var(--muted); margin-top: 1px; text-align: right; }

  .stat-bar-wrap { height: 4px; background: var(--surface2); border-radius: 2px; margin-top: 4px; overflow: hidden; }
  .stat-bar-fill { height: 100%; border-radius: 2px; transition: width 0.4s; }

  .map-wrap { flex: 1; min-width: 0; position: relative; }
  #map { width: 100%; height: 100%; }

  .sidebar {
    width: 380px; flex-shrink: 0;
    background: var(--surface); border-left: 1px solid var(--border);
    display: flex; flex-direction: column; overflow: hidden;
    box-shadow: -2px 0 6px rgba(0,0,0,0.04);
    transition: transform 0.25s ease;
  }

  .drawer-overlay {
    display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.4);
    z-index: 1500; backdrop-filter: blur(2px);
  }
  .drawer-overlay.active { display: block; }

  .mob-fabs {
    display: none; position: fixed; bottom: 16px; right: 16px;
    flex-direction: column; gap: 10px; z-index: 1400;
  }
  .mob-fab {
    width: 46px; height: 46px; border-radius: 50%;
    background: var(--accent); color: #fff; border: none;
    box-shadow: 0 3px 10px rgba(0,0,0,0.25); cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    font-size: 18px; transition: transform 0.15s, background 0.15s;
  }
  .mob-fab:hover { transform: scale(1.08); }
  .mob-fab.stats-fab { background: #374151; }

  @media (max-width: 900px) {
    .stats-panel {
      position: fixed; left: 0; top: 0; bottom: 0; z-index: 1600;
      transform: translateX(-100%); width: 300px;
    }
    .stats-panel.open { transform: translateX(0); }

    .sidebar {
      position: fixed; right: 0; top: 0; bottom: 0; z-index: 1600;
      transform: translateX(100%); width: min(380px, 90vw);
    }
    .sidebar.open { transform: translateX(0); }

    .mob-fabs { display: flex; }

    .body { flex-direction: column; }
    .map-wrap { flex: 1; width: 100%; }

    header { height: auto; min-height: 46px; flex-wrap: wrap; padding: 6px 12px; gap: 6px; }
    header h1 { font-size: 13px; }
    .hdr-filters { margin-left: 0; width: 100%; padding-bottom: 4px; gap: 6px; }
    .hdr-sep { display: none; }
    select.hsel { min-width: 0; flex: 1; font-size: 11px; }
    .hdr-brgy-search { min-width: 0; flex: 1; font-size: 11px; }
    .hdr-right { margin-left: auto; }
    .load-badge { display: none; }

    .map-legend { bottom: 70px; left: 8px; }

    .mode-toggle { flex-wrap: wrap; }
    .mode-btn { padding: 4px 8px; font-size: 10px; }
  }

  @media (max-width: 480px) {
    .map-legend { display: none; }
    .night-btn { width: 26px; height: 26px; font-size: 12px; }
  }
  .sb-header {
    padding: 12px 14px; border-bottom: 1px solid var(--border);
    background: var(--surface2); flex-shrink: 0;
  }
  .sb-muni { font-size: 10px; color: var(--muted); letter-spacing: 1px; text-transform: uppercase; font-weight: 600; }
  .sb-brgy { font-size: 18px; font-weight: 700; color: var(--text); margin-top: 1px; }

  .sb-content { flex: 1; overflow-y: auto; padding: 10px 12px; }
  .sb-content::-webkit-scrollbar { width: 4px; }
  .sb-content::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

  .placeholder {
    display: flex; flex-direction: column; align-items: center;
    justify-content: center; height: 100%; gap: 8px;
    color: var(--muted); text-align: center; padding: 20px;
  }
  .placeholder svg { opacity: 0.18; }
  .placeholder p { font-size: 12px; line-height: 1.5; }

  .sec-label {
    font-size: 9px; font-weight: 800; letter-spacing: 2px; text-transform: uppercase;
    color: var(--muted); margin: 12px 0 6px; display: flex; align-items: center; gap: 5px;
  }
  .sec-label::after { content: ''; flex: 1; height: 1px; background: var(--border); }

  .pcard {
    background: var(--surface2);
    border: 3px solid var(--border);
    border-radius: 8px;
    padding: 10px 12px; margin-bottom: 8px;
    cursor: pointer;
    transition: border-color 0.15s, box-shadow 0.15s;
  }
  .pcard:hover { box-shadow: 0 2px 8px rgba(0,0,0,0.12); }
  .pcard-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 7px; margin-bottom: 7px; }
  .pname { font-size: 13px; font-weight: 700; color: var(--text); line-height: 1.3; }
  .sbadge {
    padding: 2px 7px; border-radius: 4px; font-size: 9px; font-weight: 700;
    text-transform: uppercase; white-space: nowrap; flex-shrink: 0;
  }
  .sbadge.INCUMBENT { background: rgba(22,163,74,0.12); color: #22c55e; border: 1px solid rgba(22,163,74,0.3); }
  .sbadge.ASPIRANT  { background: rgba(217,119,6,0.12); color: #fbbf24; border: 1px solid rgba(217,119,6,0.3); }

  .cfield { display: flex; gap: 6px; margin-bottom: 4px; align-items: flex-start; font-size: 11px; }
  .cfield-label { color: var(--muted); font-weight: 700; letter-spacing: 0.5px; white-space: nowrap; min-width: 60px; padding-top: 1px; }
  .cfield-val { color: var(--text); line-height: 1.4; }
  .cfield-pill {
    display: inline-block; padding: 1px 7px; border-radius: 10px;
    font-size: 10px; font-weight: 600; margin: 1px 2px 1px 0;
    background: rgba(79,124,255,0.1); color: var(--accent); border: 1px solid rgba(79,124,255,0.2);
  }
  .cfield-pill.supp { background: rgba(8,145,178,0.1); color: #67e8f9; border-color: rgba(8,145,178,0.25); }

  .cfield-block { margin-bottom: 4px; }
  .cfield-block .cfield-label { display: block; color: var(--muted); font-size: 10px; font-weight: 700; letter-spacing: 0.5px; margin-bottom: 4px; }
  .supp-list { display: flex; flex-direction: column; gap: 3px; }
  .supp-pill {
    font-size: 11px; color: #ffffff;
    background: #0e7490;
    border-radius: 5px; padding: 3px 9px;
    display: inline-block; width: fit-content;
  }
  html.dark .supp-pill { background: #0891b2; color: #ffffff; }

  .rem-box {
    font-size: 11px; color: var(--text);
    background: rgba(251,146,60,0.08); border: 1px solid rgba(251,146,60,0.25);
    border-radius: 5px; padding: 5px 9px; margin-top: 6px; line-height: 1.5;
  }
  .rem-box::before { content: '📝 '; font-weight: 700; }

  .raw-note {
    font-size: 10px; color: var(--muted); line-height: 1.5;
    background: var(--surface); border-radius: 4px; padding: 6px 9px;
    border-left: 2px solid var(--border); margin-top: 6px;
  }
  .pcard-expand-hint {
    font-size: 9px; color: var(--muted); text-align: right; margin-top: 4px;
    letter-spacing: 0.3px;
  }

  .map-legend {
    position: absolute; bottom: 24px; left: 12px; z-index: 900;
    background: var(--surface); border: 1px solid var(--border);
    border-radius: 8px; padding: 10px 13px; box-shadow: var(--shadow);
    min-width: 140px;
  }
  .ml-title { font-size: 9px; font-weight: 800; letter-spacing: 1.5px; text-transform: uppercase; color: var(--muted); margin-bottom: 7px; }
  .ml-item { display: flex; align-items: center; gap: 7px; font-size: 11px; color: var(--text); margin-bottom: 4px; }
  .ml-item:last-child { margin-bottom: 0; }
  .ml-swatch { width: 14px; height: 14px; border-radius: 3px; flex-shrink: 0; }
  .ml-swatch.outline { background: transparent; border: 2px solid #111; }

  .stat-row.clickable { cursor: pointer; border-radius: 5px; padding: 5px 4px; margin: 0 -4px; transition: background 0.12s; }
  .stat-row.clickable:hover { background: var(--surface2); }
  .stat-row.clickable.active { background: rgba(79,124,255,0.15); }

  .sp-brgy-filter {
    padding: 0 14px 8px;
    display: flex; flex-direction: column;
    flex: 1; min-height: 0; overflow: hidden;
  }
  .sp-brgy-search {
    width: 100%; background: var(--surface2); border: 1px solid var(--border);
    border-radius: 6px; padding: 5px 9px; font-size: 12px; color: var(--text);
    outline: none; margin-bottom: 6px; transition: border-color 0.15s;
  }
  .sp-brgy-search::placeholder { color: var(--muted); }
  .sp-brgy-search:focus { border-color: var(--accent); }
  .sp-brgy-filter-list { overflow-y: auto; flex: 1; min-height: 0; }
  .sp-brgy-filter-list::-webkit-scrollbar { width: 3px; }
  .sp-brgy-filter-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
  .sp-brgy-fi {
    display: flex; align-items: center; gap: 6px;
    padding: 4px 2px; font-size: 11px; cursor: pointer;
    border-radius: 4px; transition: background 0.1s; color: var(--text);
  }
  .sp-brgy-fi:hover { background: var(--surface2); }
  .sp-brgy-fi.active { background: rgba(79,124,255,0.15); color: var(--accent); font-weight: 600; }
  .sp-brgy-fi-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
  .sp-brgy-fi-muni { font-size: 9px; color: var(--muted); margin-left: auto; }

  .night-btn {
    width: 30px; height: 30px; border-radius: 6px; border: 1px solid var(--border);
    background: var(--surface2); cursor: pointer; display: flex; align-items: center;
    justify-content: center; font-size: 14px; transition: all 0.15s; flex-shrink: 0;
  }
  .night-btn:hover { border-color: var(--accent); }

  .hdr-brgy-search {
    background: var(--surface2); border: 1px solid var(--border); border-radius: 6px;
    padding: 4px 9px; font-size: 12px; color: var(--text); outline: none;
    min-width: 160px; transition: border-color 0.15s;
  }
  .hdr-brgy-search::placeholder { color: var(--muted); }
  .hdr-brgy-search:focus { border-color: var(--accent); }
  .brgy-autocomplete {
    position: absolute; top: 100%; left: 0; right: 0; z-index: 2000;
    background: var(--surface); border: 1px solid var(--border); border-top: none;
    border-radius: 0 0 6px 6px; max-height: 220px; overflow-y: auto;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  }
  .brgy-ac-item {
    padding: 6px 10px; font-size: 12px; cursor: pointer; color: var(--text);
    display: flex; justify-content: space-between; align-items: center;
    transition: background 0.1s;
  }
  .brgy-ac-item:hover { background: var(--surface2); }
  .brgy-ac-item .ac-muni { font-size: 10px; color: var(--muted); }
  .hdr-search-wrap { position: relative; }

  .tag-row { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 4px; }
  .tag { font-size: 10px; padding: 2px 6px; border-radius: 4px; background: rgba(79,124,255,0.1); color: var(--accent); border: 1px solid rgba(79,124,255,0.2); }
  .tag.term { background: rgba(124,58,237,0.1); color: #a78bfa; border-color: rgba(124,58,237,0.25); }
  .tag.alliance { color: #fff; border: none; }

  /* leaflet overrides */
  .leaflet-control-zoom { border: 1px solid var(--border) !important; border-radius: 6px !important; overflow: hidden; box-shadow: var(--shadow) !important; }
  .leaflet-control-zoom a { background: var(--surface) !important; color: var(--text) !important; border-color: var(--border) !important; }
  .leaflet-control-zoom a:hover { background: var(--surface2) !important; }
  .leaflet-control-attribution { font-size: 9px !important; }

/* labels */
.map-label {
  background: transparent;
  border: none;
  white-space: nowrap;
  pointer-events: none;
  user-select: none;
  transform: translate(-50%, -50%);
}
.muni-label {
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: #111;
  text-shadow: 0 0 3px #fff, 0 0 6px #fff, 0 0 2px #fff;
}
.brgy-label {
  font-size: 10px;
  font-weight: 600;
  color: #111;
  text-shadow: 0 0 3px #fff, 0 0 5px #fff;
}
html.dark .muni-label {
  color: #fff;
  text-shadow: 0 0 3px #000, 0 0 6px #000, 0 0 2px #000;
}
html.dark .brgy-label {
  color: #fff;
  text-shadow: 0 0 3px #000, 0 0 5px #000;
}

.alliance-bubble {
  display: inline-block;
  font-size: 9px; font-weight: 800; letter-spacing: 0.5px;
  color: #fff; border-radius: 4px; padding: 1px 6px;
  vertical-align: middle; margin-left: 4px;
  text-transform: uppercase;
}

.raw-toggle {
  font-size: 10px; color: var(--accent); cursor: pointer;
  margin-top: 4px; display: inline-block;
  user-select: none;
}
.raw-toggle:hover { text-decoration: underline; }