:root {
  --bg: #FAF6F1;
  --surface: #FFFFFF;
  --surface-alt: #FAECE7;
  --map-bg: #FAEEDA;
  --border: #E8DDD3;
  --text: #3A2418;
  --text-secondary: #712B13;
  --text-muted: #8A7A6D;
  --accent: #D85A30;
  --accent-dark: #993C1D;
  --chip-bg: #F5C4B3;
  --chip-text: #4A1B0C;
}

[data-theme="dark"] {
  --bg: #211712;
  --surface: #2C1F18;
  --surface-alt: #3A2A20;
  --map-bg: #33261D;
  --border: #4A392E;
  --text: #F2E8DF;
  --text-secondary: #E6B199;
  --text-muted: #B49A87;
  --accent: #E56F45;
  --accent-dark: #F0916B;
  --chip-bg: #5A3A2A;
  --chip-text: #F2C9B3;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Work Sans", -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  color: var(--text);
}

body.map-page {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

h1, .fab-label, .sagra-popup h3, .sheet h2 {
  font-family: "Fraunces", Georgia, serif;
}

.top-panel {
  position: relative;
  background: var(--surface-alt);
  border-radius: 0 0 24px 24px;
  box-shadow: 0 8px 20px rgba(74, 27, 12, 0.12);
  overflow: hidden;
  z-index: 10;
}

header.app-header {
  padding: 16px 18px;
  background: var(--surface-alt);
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
}

header.app-header h1 {
  margin: 0;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--text-secondary);
}

header.app-header p {
  margin: 2px 0 0;
  font-size: 12.5px;
  color: var(--text-secondary);
  opacity: 0.75;
}

#theme-toggle {
  background: var(--surface);
  border: 1px solid var(--border);
  width: 38px;
  height: 38px;
  border-radius: 50%;
  font-size: 17px;
  cursor: pointer;
  color: var(--text-secondary);
}

.search-bar {
  margin: 12px 12px 0;
  display: flex;
  align-items: center;
  gap: 8px;
  height: 44px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 0 14px;
  box-shadow: 0 2px 8px rgba(74, 27, 12, 0.06);
}

.search-bar input {
  border: none;
  outline: none;
  background: transparent;
  flex: 1;
  font-size: 16px;
  color: var(--text);
}

#locate-btn {
  background: none;
  border: none;
  font-size: 18px;
  cursor: pointer;
  color: var(--text-secondary);
  padding: 4px;
}

.filter-row {
  display: flex;
  gap: 6px;
  padding: 10px 12px;
  overflow-x: auto;
}

.chip {
  font-size: 12px;
  padding: 6px 14px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  white-space: nowrap;
  cursor: pointer;
}

.chip.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 2500;
  padding: 20px;
}
.modal-overlay.open { display: flex; }
.modal-card {
  background: var(--surface);
  border-radius: 16px;
  padding: 22px 20px 20px;
  max-width: 320px;
  width: 100%;
  position: relative;
  box-shadow: 0 10px 28px rgba(0,0,0,0.3);
}
.modal-close {
  position: absolute;
  top: 12px;
  right: 12px;
  background: none;
  border: none;
  font-size: 16px;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px;
}
.modal-card h2 {
  font-family: "Fraunces", serif;
  font-size: 17px;
  margin: 0 10px 10px 0;
  color: var(--text-secondary);
}
.modal-card p {
  font-size: 14px;
  color: var(--text);
  margin: 0;
  line-height: 1.55;
}

.map-frame {
  flex: 1;
  min-height: 0;
  padding: 12px 12px 14px;
  position: relative;
}

.search-bar-wrap { position: relative; margin: 12px 12px 0; }
.search-bar-wrap .search-bar { margin: 0; }

.search-suggestions {
  position: absolute;
  left: 0; right: 0;
  top: calc(100% + 4px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 6px 16px rgba(74,27,12,0.12);
  overflow: hidden;
  z-index: 20;
  display: none;
}
.search-suggestions.open { display: block; }
.search-suggestions .suggestion {
  padding: 10px 14px;
  font-size: 14px;
  color: var(--text);
  cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.search-suggestions .suggestion:last-child { border-bottom: none; }
.search-suggestions .suggestion:hover { background: var(--surface-alt); }

.map-status {
  position: absolute;
  top: 24px; left: 50%;
  transform: translateX(-50%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 8px 16px;
  font-size: 13px;
  color: var(--text-secondary);
  box-shadow: 0 4px 12px rgba(74,27,12,0.12);
  z-index: 15;
  display: none;
}
.map-status.visible { display: block; }

.reset-view-btn {
  position: absolute;
  bottom: 34px;
  left: 26px;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 17px;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0,0,0,0.18);
  z-index: 900;
}

#map {
  height: 100%;
  width: 100%;
  border-radius: 18px;
  border: 6px solid var(--surface);
  outline: 1px solid var(--border);
  box-shadow: 0 4px 14px rgba(74, 27, 12, 0.10);
  background: var(--map-bg);
}

.leaflet-tile-pane {
  filter: sepia(25%) saturate(85%) hue-rotate(-6deg) brightness(1.03);
}
[data-theme="dark"] .leaflet-tile-pane {
  filter: sepia(25%) saturate(70%) hue-rotate(-6deg) brightness(0.85) contrast(1.05);
}

.leaflet-control-attribution {
  font-size: 9px;
  background: rgba(250, 246, 241, 0.75);
  color: var(--text-muted);
  padding: 1px 4px;
}

/* Popup coerente col tema chiaro/scuro, non più bianco fisso di default */
.leaflet-popup-content-wrapper {
  background: var(--surface);
  color: var(--text);
  border-radius: 12px;
  box-shadow: 0 6px 18px rgba(0,0,0,0.25);
}
.leaflet-popup-tip {
  background: var(--surface);
}
.leaflet-popup-close-button {
  color: var(--text-muted) !important;
}
.leaflet-popup-close-button:hover {
  color: var(--text) !important;
}

/* Pin personalizzati (sostituiscono l'icona blu di default di Leaflet) */
.sagra-pin {
  width: 30px;
  height: 38px;
  position: relative;
}
.sagra-pin svg { width: 100%; height: 100%; filter: drop-shadow(0 2px 3px rgba(0,0,0,0.3)); }

/* Cluster ricolorati nella palette calda invece del verde/giallo di default */
.marker-cluster-small, .marker-cluster-medium, .marker-cluster-large {
  background-color: rgba(216, 90, 48, 0.25);
}
.marker-cluster-small div, .marker-cluster-medium div, .marker-cluster-large div {
  background-color: var(--accent);
  color: #fff;
  font-family: "Work Sans", sans-serif;
  font-weight: 600;
}

.fab {
  position: fixed;
  bottom: 34px;
  right: 26px;
  display: flex;
  align-items: center;
  gap: 8px;
  height: 48px;
  padding: 0 18px 0 16px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  border: none;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
  cursor: pointer;
  z-index: 1000;
}

.fab-icon {
  font-size: 20px;
  line-height: 1;
}

.fab-label {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
}

@media (max-width: 360px) {
  .fab-label { display: none; }
  .fab { width: 48px; height: 48px; padding: 0; justify-content: center; border-radius: 50%; }
}

/* Popup e pannelli */
.sheet-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  display: none;
  align-items: flex-end;
  justify-content: center;
  z-index: 2000;
}

.sheet-overlay.open { display: flex; }

.sheet {
  background: var(--surface);
  width: 100%;
  max-width: 420px;
  border-radius: 16px 16px 0 0;
  padding: 16px;
  max-height: 85vh;
  overflow-y: auto;
}

.sheet h2 {
  margin: 0 0 12px;
  font-size: 16px;
  color: var(--text);
}

.field { margin-bottom: 14px; }
.field label {
  display: block;
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 4px;
}
.field input, .field select, .field textarea {
  width: 100%;
  padding: 9px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
}

.btn-primary {
  width: 100%;
  height: 42px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  border: none;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.btn-secondary {
  width: 100%;
  height: 42px;
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  font-size: 14px;
  cursor: pointer;
  margin-top: 8px;
}

.tag-option {
  display: inline-block;
  font-size: 12px;
  padding: 5px 12px;
  margin: 3px 4px 3px 0;
  border-radius: 999px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  cursor: pointer;
}
.tag-option.selected {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

/* Card dettaglio sagra (popup Leaflet) */
.sagra-popup h3 { margin: 0 0 4px; font-size: 15px; }
.sagra-popup img {
  width: 100%;
  height: 150px;
  object-fit: contain;
  background: var(--surface-alt);
  border-radius: 8px;
  margin-bottom: 8px;
  display: block;
  cursor: zoom-in;
}

.lightbox-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.85);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 3000;
  padding: 20px;
}
.lightbox-overlay.open { display: flex; }
.lightbox-overlay img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 8px;
}
.sagra-popup p { margin: 2px 0; font-size: 12px; color: var(--text-muted); }
.sagra-popup .tags { margin: 6px 0; }
.sagra-popup .tags span {
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--chip-bg);
  color: var(--chip-text);
  margin-right: 4px;
}
.sagra-popup .actions { display: flex; gap: 6px; margin-top: 8px; }
.sagra-popup .actions a, .sagra-popup .actions button {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-size: 12px;
  padding: 9px 6px;
  border-radius: 6px;
  text-decoration: none;
  border: none;
  cursor: pointer;
  line-height: 1.2;
}
.sagra-popup .actions a { background: var(--accent); color: #fff; }
.sagra-popup .actions button { background: var(--bg); color: var(--text); border: 1px solid var(--border); }
