:root {
  --bg-primary: #0f172a;
  --bg-surface: rgba(15, 23, 42, 0.94);
  --bg-surface-hover: #1e293b;
  --border-color: rgba(255, 255, 255, 0.12);
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --text-dim: #64748b;
  --accent: #10b981;
  --accent-hover: #059669;
  --accent-light: rgba(16, 185, 129, 0.15);
  
  /* Crime scale colors */
  --crime-very-low: #10b981;
  --crime-low: #84cc16;
  --crime-average: #f59e0b;
  --crime-high: #f97316;
  --crime-very-high: #ef4444;
  --crime-unknown: #64748b;

  --shadow-lg: 0 10px 25px -5px rgba(0, 0, 0, 0.4), 0 8px 10px -6px rgba(0, 0, 0, 0.3);
  --radius-lg: 16px;
  --radius-md: 10px;
  --radius-sm: 6px;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

body, html {
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background-color: var(--bg-primary);
  color: var(--text-main);
}

#app-container {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  position: relative;
}

/* Header */
.app-header {
  position: absolute;
  top: 12px;
  left: 12px;
  right: 12px;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.header-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--bg-surface);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border-color);
  padding: 10px 16px;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  pointer-events: auto;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-icon {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: linear-gradient(135deg, #10b981, #047857);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
}

.brand-title {
  font-size: 15px;
  font-weight: 700;
  letter-spacing: -0.2px;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.count-badge {
  background: var(--accent-light);
  color: #34d399;
  border: 1px solid rgba(16, 185, 129, 0.3);
  font-size: 12px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 20px;
  white-space: nowrap;
}

.btn-toggle-filters {
  background: #1e293b;
  color: var(--text-main);
  border: 1px solid var(--border-color);
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
}

.btn-toggle-filters:active {
  background: #334155;
}

/* Filter Panel */
.filter-panel {
  background: var(--bg-surface);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 14px 16px;
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  gap: 14px;
  pointer-events: auto;
  max-width: 420px;
  transition: all 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

.filter-panel.collapsed {
  display: none;
}

/* Affordability Mode Switcher */
.mode-switcher {
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: #020617;
  padding: 3px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-color);
  gap: 2px;
}

.mode-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  padding: 8px 10px;
  font-size: 12px;
  font-weight: 600;
  border-radius: 7px;
  cursor: pointer;
  transition: all 0.15s ease;
  text-align: center;
  line-height: 1.2;
}

.mode-btn.active {
  background: #1e293b;
  color: #fff;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.4);
  border: 1px solid rgba(255, 255, 255, 0.15);
}

.mode-btn .subtext {
  display: block;
  font-size: 10px;
  font-weight: 400;
  color: var(--text-dim);
  margin-top: 2px;
}

.mode-btn.active .subtext {
  color: #a7f3d0;
}

/* Control Groups */
.filter-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.filter-label-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
}

.filter-val-highlight {
  color: #38bdf8;
  font-weight: 700;
  font-size: 13px;
}

/* Slider */
.slider-container {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.slider-input {
  width: 100%;
  height: 6px;
  -webkit-appearance: none;
  appearance: none;
  background: #334155;
  border-radius: 3px;
  outline: none;
  cursor: pointer;
}

.slider-input::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #38bdf8;
  border: 3px solid #0f172a;
  box-shadow: 0 0 6px rgba(0, 0, 0, 0.4);
  cursor: pointer;
}

.slider-input::-moz-range-thumb {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #38bdf8;
  border: 3px solid #0f172a;
  box-shadow: 0 0 6px rgba(0, 0, 0, 0.4);
  cursor: pointer;
}

.slider-ticks {
  display: flex;
  justify-content: space-between;
  font-size: 10px;
  color: var(--text-dim);
  padding: 0 2px;
}

/* Bedroom Pills */
.pill-group {
  display: flex;
  gap: 6px;
}

.pill-btn {
  flex: 1;
  min-height: 38px;
  background: #1e293b;
  border: 1px solid var(--border-color);
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s ease;
}

.pill-btn:active {
  transform: scale(0.96);
}

.pill-btn.active {
  background: #0284c7;
  color: #ffffff;
  border-color: #38bdf8;
  box-shadow: 0 2px 6px rgba(2, 132, 199, 0.3);
}

/* Crime Legend */
.crime-legend {
  padding-top: 8px;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.legend-title {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.legend-bar {
  display: flex;
  height: 8px;
  border-radius: 4px;
  overflow: hidden;
}

.legend-seg {
  flex: 1;
}

.legend-labels {
  display: flex;
  justify-content: space-between;
  font-size: 10px;
  color: var(--text-dim);
}

/* Map Container */
#map {
  width: 100%;
  height: 100%;
  z-index: 1;
  background-color: #e2e8f0;
}

/* Floating GPS Button */
.btn-locate {
  position: absolute;
  bottom: 24px;
  right: 16px;
  z-index: 1000;
  width: 46px;
  height: 46px;
  border-radius: 50%;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  color: var(--text-main);
  box-shadow: var(--shadow-lg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  cursor: pointer;
}

.btn-locate:active {
  background: #1e293b;
  transform: scale(0.95);
}

/* Custom Marker Pin */
.custom-pin-marker {
  display: flex;
  align-items: center;
  justify-content: center;
}

.pin-bubble {
  width: 28px;
  height: 28px;
  border-radius: 50% 50% 50% 0;
  transform: rotate(-45deg);
  border: 2px solid #ffffff;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.pin-bubble:hover {
  transform: rotate(-45deg) scale(1.18);
  box-shadow: 0 6px 14px rgba(0, 0, 0, 0.5);
  z-index: 9999 !important;
}

.pin-dot {
  width: 8px;
  height: 8px;
  background: #ffffff;
  border-radius: 50%;
}

/* Leaflet Popup Overrides */
.leaflet-popup-content-wrapper {
  background: #0f172a !important;
  color: #f8fafc !important;
  border-radius: 14px !important;
  border: 1px solid rgba(255, 255, 255, 0.15);
  box-shadow: var(--shadow-lg) !important;
  padding: 0 !important;
  overflow: hidden;
}

.leaflet-popup-content {
  margin: 0 !important;
  line-height: 1.4 !important;
}

.leaflet-popup-tip {
  background: #0f172a !important;
  border: 1px solid rgba(255, 255, 255, 0.15);
}

.leaflet-container a.leaflet-popup-close-button {
  color: #94a3b8 !important;
  top: 10px !important;
  right: 12px !important;
  font-size: 20px !important;
  padding: 4px !important;
}

/* Popup Card Content */
.popup-card {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 250px;
  max-width: 290px;
}

.popup-header {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.popup-price {
  font-size: 22px;
  font-weight: 800;
  color: #ffffff;
  letter-spacing: -0.5px;
}

.popup-address {
  font-size: 13px;
  color: #cbd5e1;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.popup-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.badge {
  font-size: 11px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 6px;
  background: #1e293b;
  color: #94a3b8;
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.badge-crime {
  color: #ffffff;
}

.popup-stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  background: #1e293b;
  border-radius: var(--radius-sm);
  padding: 10px;
  border: 1px solid rgba(255, 255, 255, 0.06);
}

.stat-box {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.stat-label {
  font-size: 10px;
  font-weight: 600;
  color: #94a3b8;
  text-transform: uppercase;
}

.stat-val {
  font-size: 14px;
  font-weight: 700;
  color: #38bdf8;
}

.stat-val.highlight-green {
  color: #34d399;
}

.stat-sub {
  font-size: 9px;
  color: #64748b;
}

.btn-view-listing {
  display: block;
  text-align: center;
  background: linear-gradient(135deg, #10b981, #059669);
  color: #ffffff !important;
  text-decoration: none !important;
  font-size: 13px;
  font-weight: 700;
  padding: 10px;
  border-radius: var(--radius-sm);
  box-shadow: 0 3px 8px rgba(16, 185, 129, 0.3);
  transition: all 0.15s ease;
}

.btn-view-listing:hover {
  background: linear-gradient(135deg, #059669, #047857);
  box-shadow: 0 4px 12px rgba(16, 185, 129, 0.4);
}

/* Mobile Responsiveness */
@media (max-width: 600px) {
  .app-header {
    top: 8px;
    left: 8px;
    right: 8px;
  }

  .header-bar {
    padding: 8px 12px;
  }

  .brand-title {
    font-size: 14px;
  }

  .filter-panel {
    max-width: 100%;
    padding: 12px 14px;
    gap: 12px;
    max-height: calc(100vh - 120px);
    overflow-y: auto;
  }

  .pill-btn {
    min-height: 42px; /* Touch friendly on mobile */
  }

  .popup-card {
    max-width: 260px;
  }
}

/* ==========================================================================
   Clustering, search, saved shortlist and the extra filters
   ========================================================================== */

/* Keyless OSM tiles are far busier than Positron was; desaturating them
   restores the pale canvas the coloured pins need to stand out against.
   Only the tile pane is filtered, so markers keep their full saturation. */
#map.muted-basemap .leaflet-tile-pane {
  filter: grayscale(0.82) brightness(1.07) contrast(0.92);
}

/* Marker Clusters - coloured by the mean crime decile of their children */
.custom-cluster-marker {
  background: transparent;
  border: none;
}

.cluster-bubble {
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  color: #ffffff;
  font-weight: 800;
  font-size: 13px;
  border: 3px solid rgba(255, 255, 255, 0.9);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35);
  transition: transform 0.15s ease;
}

.cluster-bubble span {
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.45);
}

.custom-cluster-marker:hover .cluster-bubble {
  transform: scale(1.08);
}

/* Spiderfied legs sit under the pins */
.leaflet-cluster-spider-leg {
  opacity: 0.6;
}

/* Saved pins carry a star badge so a shortlist reads at a glance */
.pin-bubble.is-fav {
  border-color: #fbbf24;
  box-shadow: 0 0 0 2px rgba(251, 191, 36, 0.45), 0 4px 10px rgba(0, 0, 0, 0.4);
}

.pin-star {
  position: absolute;
  top: -8px;
  right: -6px;
  font-size: 13px;
  color: #fbbf24;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.6);
  pointer-events: none;
}

/* Header: active filter counter */
.filter-caret {
  font-size: 10px;
  line-height: 1;
}

.active-filter-dot {
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: #0284c7;
  color: #ffffff;
  font-size: 11px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.count-badge.is-empty {
  background: rgba(239, 68, 68, 0.15);
  color: #fca5a5;
  border-color: rgba(239, 68, 68, 0.35);
}

/* Search */
.search-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.search-icon {
  position: absolute;
  left: 10px;
  font-size: 13px;
  opacity: 0.7;
  pointer-events: none;
}

.search-input {
  width: 100%;
  min-height: 40px;
  padding: 8px 34px 8px 32px;
  background: #020617;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  color: var(--text-main);
  font-size: 13px;
  font-family: inherit;
  outline: none;
}

.search-input::placeholder {
  color: var(--text-dim);
}

.search-input:focus {
  border-color: #38bdf8;
  box-shadow: 0 0 0 2px rgba(56, 189, 248, 0.2);
}

.search-input::-webkit-search-cancel-button {
  -webkit-appearance: none;
  appearance: none;
}

.btn-clear-search {
  position: absolute;
  right: 6px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: none;
  background: #334155;
  color: var(--text-muted);
  font-size: 11px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-clear-search:active {
  background: #475569;
}

/* Green variant of the range slider for the leftover-cash filter */
.slider-green::-webkit-slider-thumb {
  background: #34d399;
}

.slider-green::-moz-range-thumb {
  background: #34d399;
}

.filter-val-highlight.highlight-green {
  color: #34d399;
}

.filter-hint {
  font-size: 10px;
  font-weight: 500;
  color: var(--text-dim);
  text-transform: none;
}

/* Property type chips (multi-select, wraps freely) */
.chip-group {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip-btn {
  min-height: 34px;
  padding: 6px 10px;
  background: #1e293b;
  border: 1px solid var(--border-color);
  color: var(--text-muted);
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  transition: all 0.15s ease;
}

.chip-btn:active {
  transform: scale(0.96);
}

.chip-btn.active {
  background: #0284c7;
  color: #ffffff;
  border-color: #38bdf8;
}

.chip-count {
  font-size: 10px;
  font-weight: 700;
  opacity: 0.7;
}

/* Saved-only toggle and reset */
.filter-actions {
  display: flex;
  gap: 8px;
}

.btn-fav-only,
.btn-reset {
  flex: 1;
  min-height: 40px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-color);
  background: #1e293b;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all 0.15s ease;
}

.btn-fav-only:active,
.btn-reset:active {
  transform: scale(0.97);
}

.btn-fav-only.active {
  background: rgba(251, 191, 36, 0.18);
  border-color: #fbbf24;
  color: #fcd34d;
}

.btn-fav-only:disabled {
  opacity: 0.45;
  cursor: default;
}

.fav-count {
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: rgba(255, 255, 255, 0.1);
  font-size: 11px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* Popup additions */
.popup-price-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
}

.btn-fav {
  flex-shrink: 0;
  width: 34px;
  height: 34px;
  margin-right: 22px; /* clear of Leaflet's close button */
  border-radius: 50%;
  border: 1px solid var(--border-color);
  background: #1e293b;
  color: #94a3b8;
  font-size: 17px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s ease;
}

.btn-fav:active {
  transform: scale(0.92);
}

.btn-fav.is-fav {
  background: rgba(251, 191, 36, 0.18);
  border-color: #fbbf24;
  color: #fbbf24;
}

.popup-precision {
  font-size: 10px;
  color: var(--text-dim);
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: var(--radius-sm);
  padding: 6px 8px;
  line-height: 1.35;
}

/* Toast */
.toast {
  position: absolute;
  left: 50%;
  bottom: 84px;
  transform: translateX(-50%) translateY(8px);
  z-index: 1200;
  max-width: calc(100% - 32px);
  background: var(--bg-surface);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border-color);
  border-radius: 22px;
  box-shadow: var(--shadow-lg);
  padding: 10px 16px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-main);
  text-align: center;
  opacity: 0;
  transition: opacity 0.22s ease, transform 0.22s ease;
  pointer-events: none;
}

.toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* The panel grew a lot of controls - keep it scrollable on every viewport */
.filter-panel {
  max-height: calc(100vh - 140px);
  overflow-y: auto;
  overscroll-behavior: contain;
}

@media (max-width: 600px) {
  .chip-btn {
    min-height: 38px;
  }

  .btn-fav-only,
  .btn-reset {
    min-height: 44px;
  }

  .toast {
    bottom: 92px;
  }
}
