:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --border: #e4e7ec;
  --text: #1c2126;
  --text-muted: #6b7280;
  --text-faint: #9aa2ad;
  --accent: #0e7490;
  --accent-strong: #0b5a70;
  --accent-soft: rgba(14, 116, 144, 0.10);
  --danger: #dc2626;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06);
  --shadow-md: 0 4px 16px rgba(16, 24, 40, 0.10);
  --shadow-lg: 0 16px 40px rgba(16, 24, 40, 0.18);
  --radius: 10px;
  --radius-lg: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171a;
    --surface: #1b1f23;
    --surface-2: #22262b;
    --border: #2c3136;
    --text: #eef0f2;
    --text-muted: #9aa2ad;
    --text-faint: #6b7280;
    --accent: #22b8d8;
    --accent-strong: #6fd6ea;
    --accent-soft: rgba(34, 184, 216, 0.14);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 16px 40px rgba(0, 0, 0, 0.55);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  -webkit-font-smoothing: antialiased;
}

.app {
  display: flex;
  height: 100vh;
  width: 100%;
}

/* ---------- Sidebar ---------- */

.sidebar {
  width: 380px;
  min-width: 380px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  padding: 24px 20px;
  gap: 20px;
}

.brand {
  display: flex;
  align-items: flex-start;
  gap: 12px;
}

.brand-mark {
  width: 36px;
  height: 36px;
  min-width: 36px;
  border-radius: 10px;
  background: var(--accent);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
}
.brand-mark svg { width: 20px; height: 20px; }

.brand h1 {
  font-size: 17px;
  font-weight: 700;
  margin: 0 0 2px;
  letter-spacing: -0.01em;
}

.tagline {
  margin: 0;
  font-size: 12.5px;
  color: var(--text-muted);
  line-height: 1.4;
}

.panel {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px;
}

.mode-tabs {
  display: flex;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 3px;
  margin-bottom: 12px;
}

.mode-tab {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-size: 12.5px;
  font-weight: 600;
  padding: 8px 6px;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.mode-tab.is-active {
  background: var(--accent);
  color: white;
}

.mode-panel.is-hidden { display: none; }

.search-row {
  position: relative;
  margin-bottom: 10px;
}

.search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  width: 15px;
  height: 15px;
  color: var(--text-faint);
  pointer-events: none;
}

#area-search {
  width: 100%;
  font: inherit;
  font-size: 13px;
  padding: 8px 10px 8px 32px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}
#area-search:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.area-list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 260px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.area-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 9px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  border: 1px solid transparent;
  transition: background 0.12s ease, border-color 0.12s ease;
}

.area-item:hover {
  background: var(--surface);
}

.area-item.is-selected {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.area-item .code {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.area-item .status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #22c55e;
  flex-shrink: 0;
}

.hint {
  font-size: 12.5px;
  color: var(--text-muted);
  line-height: 1.5;
  margin: 0 0 12px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font: inherit;
  font-size: 13.5px;
  font-weight: 600;
  padding: 10px 16px;
  border-radius: 9px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: transform 0.08s ease, background 0.15s ease, box-shadow 0.15s ease, opacity 0.15s ease;
  width: 100%;
}
.btn svg { width: 15px; height: 15px; }
.btn:active { transform: scale(0.98); }

.btn-secondary {
  background: var(--surface);
  border-color: var(--border);
  color: var(--text);
}
.btn-secondary:hover { border-color: var(--accent); color: var(--accent); }

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  margin-top: 8px;
}
.btn-ghost:hover { color: var(--text); }

.btn-primary {
  background: var(--accent);
  color: white;
  box-shadow: var(--shadow-sm);
  padding: 13px 16px;
  font-size: 14px;
}
.btn-primary:hover:not(:disabled) { background: var(--accent-strong); box-shadow: var(--shadow-md); }
.btn-primary:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.selection-panel {
  padding: 12px 14px;
}
.selection-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-faint);
  font-weight: 600;
  margin-bottom: 4px;
}
.selection-value {
  font-size: 14px;
  font-weight: 600;
}

.run-row { display: flex; flex-direction: column; gap: 8px; }

.run-note {
  font-size: 11.5px;
  color: var(--text-faint);
  margin: 0;
  line-height: 1.4;
  text-align: center;
}

.results-heading {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-faint);
  font-weight: 600;
  margin-bottom: 2px;
}

.results-for {
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 10px;
}

.area-list.is-locked {
  pointer-events: none;
  opacity: 0.5;
}

.legend {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 4px;
  font-size: 13px;
  cursor: pointer;
  user-select: none;
}

.legend-item input { accent-color: var(--accent); }

.legend-swatch {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  flex-shrink: 0;
}

.legend-name { flex: 1; }
.legend-count {
  font-size: 11.5px;
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
}

.width-control {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

.width-control label {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.width-control label span {
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.width-control input[type="range"] {
  width: 100%;
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  appearance: none;
  -webkit-appearance: none;
  cursor: pointer;
}

.width-control input[type="range"]::-webkit-slider-thumb {
  appearance: none;
  -webkit-appearance: none;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: var(--shadow-sm);
  cursor: pointer;
}

.width-control input[type="range"]::-moz-range-thumb {
  width: 15px;
  height: 15px;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: var(--shadow-sm);
  cursor: pointer;
}


.is-hidden { display: none !important; }

/* ---------- Map ---------- */

.map-pane {
  position: relative;
  flex: 1;
  min-width: 0;
}

#map {
  position: absolute;
  inset: 0;
  background: var(--bg);
}

.leaflet-container { font-family: inherit; }

.status-overlay {
  position: absolute;
  inset: 0;
  background: rgba(20, 23, 26, 0.28);
  backdrop-filter: blur(1px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.status-card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: 28px 36px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  min-width: 220px;
}

.spinner {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 3px solid var(--accent-soft);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.status-text { font-size: 14px; font-weight: 600; }
.status-elapsed { font-size: 12.5px; color: var(--text-faint); font-variant-numeric: tabular-nums; }

/* Basemap toggle -- Google Maps style live preview square, bottom-left. */
.basemap-toggle {
  position: relative;
  width: 84px;
  height: 84px;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  box-shadow: var(--shadow-md);
  border: 2px solid white;
  background: var(--surface-2);
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}
.basemap-toggle:hover {
  transform: scale(1.04);
  box-shadow: var(--shadow-lg);
}

.basemap-toggle-thumb {
  width: 100%;
  height: 100%;
  pointer-events: none;
}
/* Leaflet's own control positioning wraps this in absolutely-positioned
   panes; the mini map needs an explicit size so it isn't 0x0 the instant
   it initializes. */
.basemap-toggle-thumb .leaflet-container {
  width: 84px;
  height: 84px;
  background: var(--surface-2);
  font: inherit;
}

.basemap-toggle-label {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 4px 0 5px;
  text-align: center;
  font-size: 11px;
  font-weight: 600;
  color: white;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.55), rgba(0, 0, 0, 0));
  pointer-events: none;
}

.toast {
  position: absolute;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%);
  background: var(--danger);
  color: white;
  padding: 12px 20px;
  border-radius: 9px;
  box-shadow: var(--shadow-md);
  font-size: 13px;
  font-weight: 500;
  max-width: 70%;
  z-index: 1100;
}

@media (max-width: 760px) {
  .app { flex-direction: column; }
  .sidebar { width: 100%; min-width: 0; height: 46%; border-right: none; border-bottom: 1px solid var(--border); }
  .map-pane { flex: 1; }
}
