* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: #1a1a2e;
  overflow: hidden;
}

#map {
  position: fixed;
  inset: 0;
  z-index: 0;
}

/* ---- Liquid glass shared tokens ---- */
:root {
  --glass-bg: rgba(10,10,20,0.62);
  --glass-blur: blur(28px) saturate(160%);
  --glass-border: 1px solid rgba(255,255,255,0.1);
  --glass-shadow: 0 8px 32px rgba(0,0,0,0.55), inset 0 1px 0 rgba(255,255,255,0.08);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

/* ---- Top bar ---- */
#topbar {
  position: fixed;
  top: max(10px, calc(var(--safe-top) + 6px));
  left: max(10px, calc(var(--safe-left) + 4px));
  right: max(10px, calc(var(--safe-right) + 4px));
  z-index: 1000;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 10px;
  background: var(--glass-bg);
  backdrop-filter: var(--glass-blur);
  -webkit-backdrop-filter: var(--glass-blur);
  border: var(--glass-border);
  border-radius: 18px;
  box-shadow: var(--glass-shadow);
}

/* ---- Municipality picker (replaces title) ---- */
#muni-picker {
  position: relative;
  flex-shrink: 0;
}
#muni-picker-btn {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 6px 10px;
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 11px;
  color: rgba(255,255,255,0.9);
  font-size: 13px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.08);
  transition: background 0.15s;
}
#muni-picker-btn:hover { background: rgba(255,255,255,0.14); }
#muni-picker-chevron {
  font-size: 9px;
  color: rgba(255,255,255,0.4);
  transition: transform 0.2s;
}
#muni-picker-btn.open #muni-picker-chevron { transform: rotate(180deg); }

#muni-dropdown {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  min-width: 100%;
  background: var(--glass-bg);
  backdrop-filter: var(--glass-blur);
  -webkit-backdrop-filter: var(--glass-blur);
  border: var(--glass-border);
  border-radius: 14px;
  box-shadow: var(--glass-shadow);
  overflow-y: auto;
  max-height: calc(7.5 * 38px);
  z-index: 3000;
  display: none;
}
#muni-dropdown.open { display: block; }
.muni-option {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 11px 14px;
  font-size: 13px;
  color: rgba(255,255,255,0.75);
  cursor: pointer;
  border-bottom: 1px solid rgba(255,255,255,0.06);
  transition: background 0.12s;
  white-space: nowrap;
}
.muni-option:last-child { border-bottom: none; }
.muni-option:hover { background: rgba(255,255,255,0.07); }
.muni-option.selected { color: rgba(255,255,255,0.97); font-weight: 600; }
.muni-option-check { font-size: 11px; color: #4ade80; opacity: 0; }
.muni-option.selected .muni-option-check { opacity: 1; }

/* ---- Search ---- */
#search-wrap {
  position: relative;
  flex: 1;
}

#search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  width: 15px;
  height: 15px;
  color: rgba(255,255,255,0.35);
  pointer-events: none;
  z-index: 1;
}

#search-clear {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: rgba(255,255,255,0.9);
  cursor: pointer;
  padding: 0;
  line-height: 1;
  width: 18px;
  height: 18px;
  display: none;
}
#search-clear:hover { color: white; }
#search-clear.visible { display: block; }

#search-input {
  width: 100%;
  padding: 7px 28px 7px 30px;
  background: rgba(255,255,255,0.07);
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 11px;
  color: rgba(255,255,255,0.9);
  font-size: 13px;
  outline: none;
  transition: border-color 0.15s, background 0.15s, box-shadow 0.15s;
}
#search-input::placeholder { color: rgba(255,255,255,0.3); }
#search-input:focus {
  background: rgba(255,255,255,0.11);
  border-color: rgba(255,255,255,0.25);
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.06);
}

#search-results {
  position: absolute;
  top: calc(100% + 6px);
  left: 0; right: 0;
  background: var(--glass-bg);
  backdrop-filter: var(--glass-blur);
  -webkit-backdrop-filter: var(--glass-blur);
  border: var(--glass-border);
  border-radius: 14px;
  box-shadow: var(--glass-shadow);
  overflow-y: auto;
  max-height: 60vh;
  -webkit-overflow-scrolling: touch;
  z-index: 2000;
  display: none;
}
#search-results.open { display: block; }

.search-result-item {
  padding: 9px 14px;
  font-size: 12px;
  color: rgba(255,255,255,0.82);
  cursor: pointer;
  border-bottom: 1px solid rgba(255,255,255,0.06);
  line-height: 1.4;
}
.search-result-item:last-child { border-bottom: none; }
.search-result-item:hover,
.search-result-item.active { background: rgba(255,255,255,0.10); }
#search-results { transition: opacity 0.15s ease; }
#search-results.closing { opacity: 0; }
.search-result-item.no-results {
  color: rgba(255,255,255,0.35);
  cursor: default;
  font-style: italic;
}
.search-result-item.outside-area .result-warning {
  display: block;
  font-size: 10px;
  color: #ff6b6b;
  margin-top: 2px;
}

/* ---- Status bar ---- */
#statusbar {
  position: fixed;
  top: max(76px, calc(var(--safe-top) + 70px));
  left: max(10px, calc(var(--safe-left) + 4px));
  right: max(10px, calc(var(--safe-right) + 4px));
  z-index: 999;
  display: flex;
  gap: 6px;
  padding: 6px 10px;
  background: var(--glass-bg);
  backdrop-filter: var(--glass-blur);
  -webkit-backdrop-filter: var(--glass-blur);
  border: var(--glass-border);
  border-radius: 14px;
  box-shadow: var(--glass-shadow);
  overflow-x: auto;
  scrollbar-width: none;
}
#statusbar::-webkit-scrollbar { display: none; }

.status-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 12px;
  min-height: 32px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 500;
  white-space: nowrap;
  border: 1px solid rgba(255,255,255,0.1);
  backdrop-filter: blur(10px);
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.06);
}
.status-chip .dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.chip-loading { background: rgba(255,193,7,0.12); color: #ffc107; }
.chip-loading .dot {
  width: 13px; height: 13px;
  border-radius: 50%;
  border: 2px solid rgba(255,193,7,0.25);
  border-top-color: #ffc107;
  background: transparent;
  animation: spin 0.75s linear infinite;
}
.chip-ok { background: rgba(40,167,69,0.12); color: #4ade80; }
.chip-ok .dot { background: #4ade80; }
.chip-warn { background: rgba(255,193,7,0.12); color: #ffc107; }
.chip-warn .dot { background: #ffc107; }
.chip-error { background: rgba(220,53,69,0.12); color: #f87171; }
.chip-error .dot { background: #f87171; }
.chip-hidden { background: rgba(255,255,255,0.05); color: rgba(255,255,255,0.28); }
.chip-hidden .dot { background: rgba(255,255,255,0.18); }
.chip-gps { background: rgba(66,153,225,0.12); color: #60a5fa; }
.chip-gps .dot { background: #60a5fa; animation: pulse 1.5s infinite; }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

/* ---- Detail bottom sheet ---- */
#detail-panel {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  z-index: 2000;
  background: var(--glass-bg);
  backdrop-filter: var(--glass-blur);
  -webkit-backdrop-filter: var(--glass-blur);
  border-top: var(--glass-border);
  border-left: var(--glass-border);
  border-right: var(--glass-border);
  border-radius: 20px 20px 0 0;
  box-shadow: 0 -4px 40px rgba(0,0,0,0.55), inset 0 1px 0 rgba(255,255,255,0.08);
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
  max-height: 70vh;
  display: flex;
  flex-direction: column;
}

#detail-panel.open {
  transform: translateY(0);
}

#detail-handle {
  width: 36px; height: 4px;
  background: rgba(255,255,255,0.18);
  border-radius: 2px;
  margin: 10px auto 0;
  flex-shrink: 0;
}

#detail-close {
  position: absolute;
  top: 10px; right: 14px;
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 50%;
  width: 36px; height: 36px;
  color: rgba(255,255,255,0.55);
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.08);
  transition: background 0.15s;
}
#detail-close:hover { background: rgba(255,255,255,0.14); }

#detail-content {
  overflow-y: auto;
  padding: 14px 16px calc(20px + var(--safe-bottom));
  scrollbar-width: thin;
  scrollbar-color: rgba(255,255,255,0.15) transparent;
}

#detail-cat-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
  margin-bottom: 10px;
  border: 1px solid rgba(255,255,255,0.15);
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.06);
}

#detail-cat-badge .swatch {
  width: 10px; height: 10px;
  border-radius: 2px;
}

#detail-title {
  font-size: 18px;
  font-weight: 700;
  color: rgba(255,255,255,0.97);
  margin-bottom: 4px;
  line-height: 1.3;
}

#detail-sub {
  font-size: 13px;
  color: rgba(255,255,255,0.45);
  margin-bottom: 14px;
}

.detail-row {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  margin-bottom: 8px;
}

.detail-row-value {
  font-size: 13px;
  color: rgba(255,255,255,0.82);
  line-height: 1.4;
}

/* ---- Doc buttons ---- */
.doc-buttons {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 14px;
}

.doc-btn {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 11px 14px;
  background: rgba(255,255,255,0.06);
  border: 1px solid rgba(255,255,255,0.09);
  border-radius: 13px;
  text-decoration: none;
  color: rgba(255,255,255,0.9);
  transition: background 0.15s, box-shadow 0.15s;
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.06);
}
.doc-btn:hover { background: rgba(255,255,255,0.1); box-shadow: inset 0 1px 0 rgba(255,255,255,0.1); }

.doc-btn-left { display: flex; flex-direction: column; gap: 2px; }
.doc-btn-title { font-size: 13px; font-weight: 600; }
.doc-btn-page { font-size: 11px; color: rgba(255,255,255,0.38); }
.doc-btn-arrow { font-size: 16px; color: rgba(255,255,255,0.35); flex-shrink: 0; }

/* ---- GPS accuracy circle overrides ---- */
.gps-dot {
  background: #4299e1;
  border: 3px solid #fff;
  border-radius: 50%;
  box-shadow: 0 0 0 2px rgba(66,153,225,0.5);
}

/* ---- Locate button ---- */
#locate-btn {
  position: fixed;
  bottom: calc(24px + var(--safe-bottom));
  right: max(14px, calc(var(--safe-right) + 10px));
  z-index: 1000;
  width: 46px; height: 46px;
  background: var(--glass-bg);
  border: var(--glass-border);
  border-radius: 14px;
  color: #60a5fa;
  font-size: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  backdrop-filter: var(--glass-blur);
  -webkit-backdrop-filter: var(--glass-blur);
  box-shadow: var(--glass-shadow);
  transition: background 0.15s, box-shadow 0.15s, bottom 0.3s cubic-bezier(0.32, 0.72, 0, 1);
}
#locate-btn:hover {
  background: rgba(20,20,40,0.72);
  box-shadow: 0 10px 36px rgba(0,0,0,0.65), inset 0 1px 0 rgba(255,255,255,0.1);
}

/* ---- Basemap toggle button ---- */
#basemap-btn {
  position: fixed;
  bottom: calc(78px + var(--safe-bottom));
  right: max(14px, calc(var(--safe-right) + 10px));
  z-index: 1000;
  width: 46px; height: 46px;
  background: var(--glass-bg);
  border: var(--glass-border);
  border-radius: 14px;
  color: rgba(255,255,255,0.88);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  backdrop-filter: var(--glass-blur);
  -webkit-backdrop-filter: var(--glass-blur);
  box-shadow: var(--glass-shadow);
  transition: background 0.15s, bottom 0.3s cubic-bezier(0.32, 0.72, 0, 1);
}
#basemap-btn:hover { background: rgba(20,20,40,0.72); }

/* ---- Layers trigger button ---- */
#layers-btn {
  position: fixed;
  bottom: calc(24px + var(--safe-bottom));
  left: max(14px, calc(var(--safe-left) + 10px));
  z-index: 1000;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 16px;
  height: 46px;
  background: var(--glass-bg);
  border: var(--glass-border);
  border-radius: 14px;
  color: rgba(255,255,255,0.88);
  font-size: 13px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  backdrop-filter: var(--glass-blur);
  -webkit-backdrop-filter: var(--glass-blur);
  box-shadow: var(--glass-shadow);
  transition: background 0.15s, bottom 0.3s cubic-bezier(0.32, 0.72, 0, 1);
  white-space: nowrap;
  max-width: calc(50vw - 28px);
}
#layers-btn:hover { background: rgba(20,20,40,0.72); }
#layers-btn svg { flex-shrink: 0; opacity: 0.7; }
#layers-btn-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---- Layers popup ---- */
#layers-backdrop {
  position: fixed;
  inset: 0;
  z-index: 1400;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s;
}
#layers-backdrop.open { opacity: 1; pointer-events: auto; }

#layers-sheet {
  position: fixed;
  bottom: 0;
  left: max(14px, calc(var(--safe-left) + 10px));
  z-index: 1500;
  background: rgba(8,8,20,0.82);
  backdrop-filter: blur(32px) saturate(180%);
  -webkit-backdrop-filter: blur(32px) saturate(180%);
  border: var(--glass-border);
  border-radius: 18px;
  box-shadow: var(--glass-shadow);
  width: fit-content;
  min-width: 200px;
  max-width: calc(100vw - 28px);
  max-height: 72vh;
  display: flex;
  flex-direction: column;
  transform: translateY(calc(100% + 12px));
  opacity: 0;
  pointer-events: none;
  transition: transform 0.25s cubic-bezier(0.32, 0.72, 0, 1), opacity 0.2s;
}
#layers-sheet.open { transform: translateY(0); opacity: 1; pointer-events: auto; }

#layers-handle { display: none; }

#layers-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px 6px;
  flex-shrink: 0;
}
#layers-title {
  font-size: 13px;
  font-weight: 600;
  color: rgba(255,255,255,0.95);
  letter-spacing: 0.1px;
}
#layers-clear {
  background: none;
  border: none;
  font-size: 13px;
  font-family: inherit;
  color: rgba(255,255,255,0.45);
  cursor: pointer;
  padding: 0;
}
#layers-clear:hover { color: rgba(255,255,255,0.75); }


#overlay-body {
  overflow-y: auto;
  padding: 4px 8px 8px;
  scrollbar-width: none;
  flex: 1;
}
#overlay-body::-webkit-scrollbar { display: none; }

.overlay-group-title {
  font-size: 9px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: rgba(255,255,255,0.28);
  margin: 10px 4px 3px;
}
.overlay-item {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 10px 10px;
  min-height: 44px;
  border-radius: 11px;
  margin-bottom: 1px;
  cursor: pointer;
  user-select: none;
  transition: background 0.15s;
}
.overlay-item input[type=radio] { display: none; }
.overlay-item:hover { background: rgba(255,255,255,0.05); }
.overlay-item:has(input:checked) {
  background: rgba(255,255,255,0.09);
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.1);
}
.overlay-dot {
  width: 12px; height: 12px;
  border-radius: 3px;
  flex-shrink: 0;
  border: 1px solid rgba(255,255,255,0.25);
  transition: box-shadow 0.15s;
}
.overlay-item:has(input:checked) .overlay-dot {
  box-shadow: 0 0 0 2px rgba(255,255,255,0.55);
}
.overlay-name {
  font-size: 12px;
  color: rgba(255,255,255,0.72);
  line-height: 1.3;
  flex: 1;
  transition: color 0.15s;
}
.overlay-item:has(input:checked) .overlay-name {
  color: rgba(255,255,255,0.97);
  font-weight: 500;
}
.overlay-spinner {
  font-size: 13px;
  color: #ffc107;
  flex-shrink: 0;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Leaflet popup overrides */
.leaflet-popup-content-wrapper {
  display: none; /* we use our own panel */
}

/* ---- Attribution ---- */
.leaflet-control-attribution {
  background: rgba(8,8,18,0.55) !important;
  backdrop-filter: blur(12px) !important;
  -webkit-backdrop-filter: blur(12px) !important;
  color: rgba(255,255,255,0.35) !important;
  font-size: 9px !important;
  border-radius: 8px 8px 0 0 !important;
  border: 1px solid rgba(255,255,255,0.07) !important;
  border-bottom: none !important;
  padding: 3px 7px !important;
}
.leaflet-control-attribution a { color: rgba(255,255,255,0.45) !important; }

/* ---- Cache date indicator ---- */
#cache-date {
  position: fixed;
  bottom: max(6px, var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  font-size: 9px;
  color: rgba(255,255,255,0.28);
  z-index: 900;
  white-space: nowrap;
  pointer-events: none;
  letter-spacing: 0.1px;
}
