:root {
  --bg: #f2f4f8;
  --surface: #ffffff;
  --surface-2: #f8f9fc;
  --border: #e3e6ee;
  --border-strong: #cdd2df;
  --text: #1a1e2c;
  --text-soft: #5b6478;
  --text-faint: #8a92a5;
  --accent: #3b5bdb;
  --accent-hover: #2f4dc4;
  --accent-soft: #e8ecfb;
  --danger: #d43f3a;
  --success: #2f9e44;
  --warn-bg: #fff7e6;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.04), 0 1px 3px rgba(16, 24, 40, 0.06);
  --shadow-md: 0 4px 12px rgba(16, 24, 40, 0.08), 0 1px 3px rgba(16, 24, 40, 0.05);
  --shadow-lg: 0 12px 28px rgba(16, 24, 40, 0.12), 0 2px 6px rgba(16, 24, 40, 0.06);
  --radius: 10px;
  --radius-sm: 6px;
  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --mono: 'JetBrains Mono', ui-monospace, Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: var(--font);
  font-size: 13px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

body { display: flex; height: 100vh; overflow: hidden; }

/* ---------------- sidebar ---------------- */

#sidebar {
  width: 380px;
  min-width: 340px;
  max-width: 440px;
  padding: 18px 18px 24px;
  overflow-y: auto;
  background: var(--bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

#sidebar::-webkit-scrollbar { width: 10px; }
#sidebar::-webkit-scrollbar-track { background: transparent; }
#sidebar::-webkit-scrollbar-thumb { background: #d4d8e2; border-radius: 5px; border: 2px solid var(--bg); }

.app-header { margin-bottom: 2px; }
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  width: 36px; height: 36px;
  border-radius: 10px;
  background: linear-gradient(135deg, #4c6ef5 0%, #7950f2 100%);
  box-shadow: 0 3px 10px rgba(76, 110, 245, 0.35);
  flex-shrink: 0;
}
.brand h1 {
  font-size: 16px;
  font-weight: 700;
  margin: 0;
  letter-spacing: -0.01em;
}
.sub {
  color: var(--text-soft);
  font-size: 11.5px;
  margin: 2px 0 0;
  line-height: 1.4;
}

/* ---------------- cards ---------------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 14px 12px;
  box-shadow: var(--shadow-sm);
}

.card-title {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  color: var(--text-soft);
  letter-spacing: 0.06em;
  margin-bottom: 10px;
}
.card-title.row { display: flex; justify-content: space-between; align-items: center; }

.field-label {
  display: block;
  font-size: 11.5px;
  font-weight: 500;
  color: var(--text-soft);
  margin-bottom: 5px;
}

.hint {
  font-size: 11px;
  color: var(--text-faint);
  margin-top: 6px;
  line-height: 1.45;
}

/* ---------------- inputs ---------------- */

textarea, select, input[type="text"] {
  width: 100%;
  padding: 8px 10px;
  font-size: 13px;
  font-family: inherit;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  transition: border-color 0.15s, box-shadow 0.15s;
}
textarea {
  font-family: var(--mono);
  resize: vertical;
  min-height: 72px;
  line-height: 1.5;
}
textarea:focus, select:focus, input[type="text"]:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath fill='%235b6478' d='M0 0l5 6 5-6z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 30px;
  cursor: pointer;
}

/* ---------------- segmented control ---------------- */

.segmented {
  display: flex;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 3px;
  gap: 3px;
}
.segmented label {
  flex: 1;
  cursor: pointer;
  position: relative;
}
.segmented input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}
.segmented span {
  display: block;
  text-align: center;
  padding: 5px 4px;
  font-size: 11.5px;
  font-weight: 500;
  color: var(--text-soft);
  border-radius: 4px;
  transition: all 0.15s;
  white-space: nowrap;
}
.segmented label:hover span { color: var(--text); }
.segmented input:checked + span {
  background: var(--surface);
  color: var(--accent);
  box-shadow: var(--shadow-sm);
  font-weight: 600;
}

/* ---------------- mini buttons ---------------- */

.mini-buttons { display: flex; gap: 4px; }
.mini-buttons button {
  font-family: inherit;
  font-size: 10.5px;
  font-weight: 500;
  padding: 3px 8px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text-soft);
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.15s;
}
.mini-buttons button:hover {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--accent-soft);
}

/* ---------------- tag tree ---------------- */

#tag-tree {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  max-height: 320px;
  overflow-y: auto;
  margin-top: -2px;
}
#tag-tree::-webkit-scrollbar { width: 8px; }
#tag-tree::-webkit-scrollbar-thumb { background: #ccd2e0; border-radius: 4px; }

.category:not(:last-child) { border-bottom: 1px solid var(--border); }

.cat-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  cursor: pointer;
  user-select: none;
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
}
.cat-header:hover { background: rgba(59, 91, 219, 0.04); }
.cat-chevron {
  width: 10px;
  height: 10px;
  color: var(--text-faint);
  transition: transform 0.15s;
}
.category.collapsed .cat-chevron { transform: rotate(-90deg); }
.cat-label { flex: 1; }
.cat-count {
  font-size: 10.5px;
  font-weight: 500;
  color: var(--text-faint);
  padding: 1px 6px;
  background: var(--border);
  border-radius: 10px;
}
.cat-count.active { background: var(--accent-soft); color: var(--accent); }

.tag-list {
  padding: 2px 0 6px 28px;
}
.category.collapsed .tag-list { display: none; }

.tag-row {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 3px 10px 3px 0;
  font-size: 12px;
  cursor: pointer;
  border-radius: 3px;
  color: var(--text);
}
.tag-row:hover { background: rgba(59, 91, 219, 0.05); }
.tag-row.master { font-weight: 600; color: var(--text); }
.tag-row.master::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border);
  margin-left: 6px;
}

/* custom checkboxes */
input[type="checkbox"] {
  appearance: none;
  width: 15px;
  height: 15px;
  border: 1.5px solid var(--border-strong);
  border-radius: 3px;
  background: var(--surface);
  cursor: pointer;
  display: inline-block;
  position: relative;
  flex-shrink: 0;
  transition: all 0.1s;
  margin: 0;
}
input[type="checkbox"]:hover { border-color: var(--accent); }
input[type="checkbox"]:checked {
  background: var(--accent);
  border-color: var(--accent);
}
input[type="checkbox"]:checked::after {
  content: "";
  position: absolute;
  left: 3.5px;
  top: 0.5px;
  width: 5px;
  height: 9px;
  border: solid white;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}
input[type="checkbox"]:indeterminate {
  background: var(--accent);
  border-color: var(--accent);
}
input[type="checkbox"]:indeterminate::after {
  content: "";
  position: absolute;
  left: 2.5px;
  top: 5.5px;
  width: 8px;
  height: 2px;
  background: white;
}

/* ---------------- run button ---------------- */

.run-btn {
  width: 100%;
  padding: 11px;
  font-size: 13.5px;
  font-weight: 600;
  font-family: inherit;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.15s;
  box-shadow: 0 2px 6px rgba(59, 91, 219, 0.25);
}
.run-btn:hover { background: var(--accent-hover); box-shadow: 0 3px 10px rgba(59, 91, 219, 0.35); }
.run-btn:active { transform: translateY(1px); }
.run-btn:disabled {
  background: var(--text-faint);
  cursor: wait;
  box-shadow: none;
}

.status {
  font-size: 11.5px;
  color: var(--text-soft);
  padding: 4px 2px;
  line-height: 1.5;
  min-height: 17px;
}
.status.error { color: var(--danger); }
.status.working { color: var(--accent); }
.status.working::before {
  content: "";
  display: inline-block;
  width: 9px; height: 9px;
  border: 1.5px solid var(--accent-soft);
  border-top-color: var(--accent);
  border-radius: 50%;
  margin-right: 6px;
  vertical-align: -1px;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------------- results ---------------- */

.hidden { display: none !important; }

.summary-stats {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
  margin-bottom: 10px;
}
.stat-cell {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 7px 8px;
  text-align: center;
}
.stat-cell .stat-value {
  display: block;
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}
.stat-cell .stat-label {
  display: block;
  font-size: 10px;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-top: 3px;
}

.table-wrap {
  max-height: 260px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}
.table-wrap::-webkit-scrollbar { width: 8px; }
.table-wrap::-webkit-scrollbar-thumb { background: #ccd2e0; border-radius: 4px; }

#district-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
}
#district-table th {
  position: sticky;
  top: 0;
  background: var(--surface-2);
  padding: 6px 8px;
  text-align: left;
  font-weight: 600;
  font-size: 10.5px;
  color: var(--text-soft);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  user-select: none;
}
#district-table th:hover { color: var(--accent); }
#district-table th.num { text-align: right; }
#district-table th.sort-active { color: var(--accent); }
#district-table th.sort-active::after {
  content: " ↓";
  font-size: 10px;
}
#district-table th.sort-active.asc::after { content: " ↑"; }

#district-table td {
  padding: 5px 8px;
  border-bottom: 1px solid var(--border);
}
#district-table td.num { text-align: right; }
#district-table tbody tr { cursor: pointer; transition: background 0.1s; }
#district-table tbody tr:hover { background: var(--accent-soft); }
#district-table tbody tr.active {
  background: var(--accent-soft);
  box-shadow: inset 3px 0 0 var(--accent);
}
#district-table tbody tr.nodata td { color: var(--text-faint); }

.frac-bar {
  display: inline-block;
  width: 32px;
  height: 7px;
  background: var(--border);
  border-radius: 3px;
  margin-right: 6px;
  vertical-align: middle;
  overflow: hidden;
}
.frac-bar > span {
  display: block;
  height: 100%;
  border-radius: 3px;
}

/* ---------------- main area ---------------- */

#main {
  flex: 1;
  position: relative;
  overflow: hidden;
}
#map {
  width: 100%;
  height: 100%;
  background: #e6eaf1;
}

/* ---------------- legend ---------------- */

.legend {
  position: absolute;
  bottom: 20px;
  right: 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  font-size: 11px;
  box-shadow: var(--shadow-md);
  min-width: 220px;
  z-index: 500;
}
.legend-title {
  font-weight: 600;
  font-size: 11px;
  color: var(--text-soft);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 8px;
}
.legend-bar {
  height: 10px;
  width: 100%;
  border-radius: 3px;
  border: 1px solid var(--border);
  margin-bottom: 5px;
}
.legend-scale {
  display: flex;
  justify-content: space-between;
  font-variant-numeric: tabular-nums;
  color: var(--text-soft);
  font-size: 10.5px;
}
.legend-nodata {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--text-soft);
  font-size: 11px;
}
.legend-nodata-swatch {
  width: 14px; height: 14px;
  background: #d9dde7;
  border: 1px solid var(--border-strong);
  border-radius: 3px;
  background-image: repeating-linear-gradient(45deg, transparent 0 3px, rgba(0,0,0,0.08) 3px 6px);
}

/* ---------------- district panel ---------------- */

.district-panel {
  position: absolute;
  top: 20px;
  right: 20px;
  width: 340px;
  max-height: calc(100vh - 40px);
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  font-size: 12px;
  box-shadow: var(--shadow-lg);
  z-index: 600;
  line-height: 1.5;
}
.district-panel::-webkit-scrollbar { width: 8px; }
.district-panel::-webkit-scrollbar-thumb { background: #ccd2e0; border-radius: 4px; }
.district-panel h2 {
  margin: 0 0 2px;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.district-panel .close {
  position: absolute;
  top: 10px; right: 12px;
  cursor: pointer;
  color: var(--text-faint);
  font-size: 20px;
  line-height: 1;
  width: 22px; height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
}
.district-panel .close:hover { background: var(--surface-2); color: var(--text); }
.district-panel .headline {
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 8px 10px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  margin: 10px 0 12px;
}
.district-panel .headline-frac {
  font-size: 22px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
}
.district-panel .headline-sub {
  font-size: 12px;
  color: var(--text-soft);
  font-variant-numeric: tabular-nums;
}
.district-panel h3 {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  color: var(--text-soft);
  letter-spacing: 0.04em;
  margin: 14px 0 6px;
}
.district-panel ul, .district-panel ol {
  margin: 0;
  padding: 0;
  list-style: none;
}

/* grouped matches */
.match-group { margin-bottom: 10px; }
.match-group-head {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-soft);
  padding: 4px 0 3px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 3px;
}
.match-group-count {
  font-size: 10.5px;
  font-weight: 600;
  background: var(--accent-soft);
  color: var(--accent);
  padding: 1px 6px;
  border-radius: 10px;
  margin-left: auto;
}
.match-list { padding: 0; margin: 0; }
.match-list li {
  padding: 3px 6px;
  border-radius: 3px;
  font-size: 12px;
  line-height: 1.35;
  cursor: default;
}
.match-list li.match-item { cursor: pointer; }
.match-list li:hover { background: var(--accent-soft); }
.match-list li a {
  color: var(--accent);
  text-decoration: none;
  font-weight: 500;
}
.match-list li a:hover { text-decoration: underline; }
.match-list li.plain { color: var(--text-soft); }

/* hover tooltip */
.match-tooltip {
  position: fixed;
  z-index: 10000;
  background: #1a1e2c;
  color: #e8ecfb;
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 11.5px;
  line-height: 1.5;
  box-shadow: 0 12px 28px rgba(0,0,0,0.35), 0 2px 6px rgba(0,0,0,0.25);
  max-width: 320px;
  pointer-events: auto;
}
.match-tooltip.hidden { display: none; }
.match-tooltip .tt-name {
  font-size: 13px;
  font-weight: 600;
  color: white;
  margin-bottom: 2px;
}
.match-tooltip .tt-cat {
  font-size: 10.5px;
  color: #b5bfda;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 8px;
}
.match-tooltip .tt-row {
  display: grid;
  grid-template-columns: 78px 1fr;
  gap: 6px;
  margin-bottom: 3px;
  word-break: break-word;
}
.match-tooltip .tt-key {
  color: #8a92a5;
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding-top: 1px;
}
.match-tooltip .tt-meta {
  margin-top: 6px;
  padding-top: 6px;
  border-top: 1px solid #2a3042;
  color: #8a92a5;
  font-size: 10.5px;
}
.match-tooltip a {
  color: #8ec0ff;
  text-decoration: none;
}
.match-tooltip a:hover { text-decoration: underline; }

/* ---------------- leaflet overrides ---------------- */

.leaflet-tooltip.district-tooltip {
  background: var(--text);
  color: white;
  border: none;
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 11.5px;
  font-family: var(--font);
  box-shadow: var(--shadow-md);
  font-weight: 500;
  line-height: 1.45;
}
.leaflet-tooltip.district-tooltip::before { display: none; }
.leaflet-container { font-family: var(--font); background: #e6eaf1; }
.leaflet-control-attribution { font-size: 10px; }

/* ---------------- credit footer ---------------- */
.credit {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  font-size: 10.5px;
  color: var(--text-faint);
  text-align: center;
  line-height: 1.5;
}
.credit a {
  color: var(--accent);
  text-decoration: none;
}
.credit a:hover { text-decoration: underline; }

/* ---------------- city select meta ---------------- */
#city-meta {
  margin-top: 6px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* ---------------- loading overlay ---------------- */
.loading-overlay {
  position: absolute;
  inset: 0;
  background: rgba(242, 244, 248, 0.88);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 800;
  font-size: 13px;
  color: var(--text-soft);
  backdrop-filter: blur(2px);
}
.loading-overlay::before {
  content: "";
  width: 16px; height: 16px;
  border: 2px solid var(--accent-soft);
  border-top-color: var(--accent);
  border-radius: 50%;
  margin-right: 10px;
  animation: spin 0.8s linear infinite;
}
