/**
 * ===================================================================
 * Foglio di stile personalizzato per Leaflet
 * ===================================================================
 *
 * Contiene:
 * - Override generali di Leaflet
 * - Stili per i contenitori della mappa
 * - Stili per Popup
 * - Stili per i Controlli (Geolocalizzazione, Reset, Layers, etc.)
 * - Stili per le notifiche "Toast"
 * - Hack e fix specifici
 * - Media Queries per responsività
 *
 */

:root {
    /* Palette Colori Principale */
    --leaflet-primary-color: #03095e;
    --leaflet-secondary-color: #0078A8;
    --leaflet-accent-color: #ffa600;
    --leaflet-active-color-rgb: 0, 123, 255;
    --leaflet-active-color: rgb(var(--leaflet-active-color-rgb)); /* #007bff */
    --leaflet-active-border-color: #0069d9;

    /* Colori di Stato */
    --leaflet-color-warning: #ffc107;
    --leaflet-color-error: #dc3545;
    --leaflet-color-locate-circle: #136AEC;

    /* Colori UI */
    --leaflet-background-color: #fff;
    --leaflet-text-color: #333;
    --leaflet-border-color: #ccc;
    --leaflet-disabled-bg-color: #f4f4f4;
    --leaflet-disabled-text-color: #bbb;
    --leaflet-zoom-box-border-color: #38f;
    --leaflet-zoom-box-bg-color: rgba(255, 255, 255, 0.5);
    --leaflet-box-shadow-color: rgba(0, 0, 0, 0.15);
    --leaflet-black: #000;

    /* Tipografia */
    --leaflet-font-size-base: 0.75rem;
    --leaflet-line-height-base: 1.5;

    /* Controlli */
    --leaflet-control-size: 26px;
    --leaflet-control-size-touch: 40px;
    --leaflet-control-margin: 5px;
    --leaflet-control-border: 1px solid var(--leaflet-border-color);
    --leaflet-layers-selector-size: 10px;

    /* Popup */
    --leaflet-popup-content-width: 200px;
    --leaflet-popup-margin: 20px 5px 5px 5px;

    /* Notifiche Toast */
    --leaflet-toast-padding: 12px 20px;
    --leaflet-toast-border-radius: 8px;
    --leaflet-toast-transition-duration: 0.4s;

    /* Animazioni */
    --leaflet-pulse-duration: 1.5s;
    --leaflet-spin-duration: 1s;

    /* Marker Cluster */
    --leaflet-cluster-small-color-rgb: 0, 120, 168;   /* Blu secondario */
    --leaflet-cluster-medium-color-rgb: 3, 9, 94;      /* Blu primario */
    --leaflet-cluster-large-color-rgb: 255, 166, 0;   /* Arancione accento */
}

/************ REGOLAZIONI GENERALI LEAFLET ************/

/* Tipografia generale */
.leaflet-container {
	/* font-family: "Helvetica Neue", Arial, Helvetica, sans-serif;*/
	/* font-size: 12px;*/
	font-size: 0.75rem; /* Fallback */
	font-size: var(--leaflet-font-size-base);
	line-height: 1.5; /* Fallback */
	line-height: var(--leaflet-line-height-base);
	background: #fff; /* Fallback */
	background: var(--leaflet-background-color);
	outline-offset: 1px;
}

.leaflet-container a {
	color: #0078A8; /* Fallback */
	color: var(--leaflet-secondary-color);
}

/* Stile per box di zoom */
.leaflet-zoom-box {
	border: 2px dotted #38f; /* Fallback */
	border: 2px dotted var(--leaflet-zoom-box-border-color);
	background: rgba(255,255,255,0.5); /* Fallback */
	background: var(--leaflet-zoom-box-bg-color);
}

/************ CONTROLLO LEGENDA PERSONALIZZATO ************/

.leaflet-control-legend {
    background: var(--leaflet-background-color, #fff);
    padding: 10px 15px;
    border-radius: 5px;
    box-shadow: 0 1px 5px var(--leaflet-box-shadow-color, rgba(0,0,0,0.4));
    line-height: 1.2;
    color: var(--leaflet-text-color, #333);
    font-size: 0.8rem;
}

.leaflet-control-legend h4 {
    margin-top: 0;
    margin-bottom: 8px;
    font-size: 0.9rem;
    font-weight: bold;
    color: var(--leaflet-primary-color, #03095e);
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: margin-bottom 0.3s;
}

.leaflet-control-legend h4::after {
    content: '';
    width: 0; 
    height: 0; 
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-top: 5px solid currentColor;
    transition: transform 0.3s;
}

.leaflet-control-legend ul {
    list-style: none;
    padding: 0;
    margin: 0;
    overflow: hidden;
    transition: max-height 0.3s ease-out, opacity 0.3s ease-out;
    max-height: 500px; /* Altezza massima sufficiente per l'animazione */
    opacity: 1;
}

.leaflet-control-legend li {
    display: flex;
    align-items: center;
    margin-bottom: 5px;
}

.leaflet-control-legend .legend-swatch {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    margin-right: 8px;
    border: 1px solid var(--leaflet-border-color, #ccc);
}

/* Stato compresso della legenda */
.leaflet-control-legend.collapsed ul {
    max-height: 0;
    opacity: 0;
}

.leaflet-control-legend.collapsed h4 {
    margin-bottom: 0;
}

.leaflet-control-legend.collapsed h4::after {
    transform: rotate(-90deg);
}
/* Margini per i controlli */
.leaflet-top .leaflet-control {
	margin-top: 5px; /* Fallback */
	margin-top: var(--leaflet-control-margin);
}
.leaflet-bottom .leaflet-control {
	margin-bottom: 5px; /* Fallback */
	margin-bottom: var(--leaflet-control-margin);
}
.leaflet-left .leaflet-control {
	margin-left: 5px; /* Fallback */
	margin-left: var(--leaflet-control-margin);
}
.leaflet-right .leaflet-control {
	margin-right: 5px; /* Fallback */
	margin-right: var(--leaflet-control-margin);
}

/* Override per Bootstrap */
.breadcrumb {
    width: max-content;
}

/************ CONTENITORI MAPPA ************/

#map, /* pagina archivio */
#map-display { /* singola pagina */
    height: var(--map-height);
    outline: none !important;
    box-shadow: 0 0 0 0 #000 !important; /* Fallback */
    box-shadow: 0 0 0 0 var(--leaflet-black) !important;
}

/************ POPUP ************/

.leaflet-popup-content img {
    width: 100%;
    height: auto;
    margin-bottom: 20px;
    aspect-ratio: 1 / 1; /* Unificato da 3/2 e 2/2 (che è 1/1) */
    object-fit: cover;
    object-position: center;
}

.leaflet-oldie .leaflet-popup-content-wrapper {
	-ms-zoom: 1;
	zoom: 1;
}
.leaflet-popup-content {
    width: 200px; /* Fallback */
    width: var(--leaflet-popup-content-width);
	margin: 20px 5px 5px 5px; /* Fallback */
	margin: var(--leaflet-popup-margin);
	line-height: 1.3;
	font-size: 1em; /* Considerare se usare rem o px per coerenza */
	min-height: 12px;
}

/************ CONTROLLI ************/

/* --- Stile generale per i pulsanti della barra --- */
.leaflet-bar a {
    background-color: #fff; /* Fallback */
    background-color: var(--leaflet-background-color); /* Era #ffffff00, se serve trasparenza usare 'transparent' */
    border-bottom: 1px solid #ccc; /* Fallback */
    border-bottom: var(--leaflet-control-border);
    width: 26px; /* Fallback */
    width: var(--leaflet-control-size);
    height: 26px; /* Fallback */
    height: var(--leaflet-control-size);
    line-height: 26px; /* Fallback */
    line-height: var(--leaflet-control-size);
    display: flex !important;
    align-items: center;
    justify-content: center;
    text-align: center;
    text-decoration: none;
    color: #03095e; /* Fallback */
    color: var(--leaflet-primary-color);
}

.leaflet-touch .leaflet-bar a {
    width: 40px; /* Fallback */
    width: var(--leaflet-control-size-touch);
    height: 40px; /* Fallback */
    height: var(--leaflet-control-size-touch);
    line-height: 40px; /* Fallback */
    line-height: var(--leaflet-control-size-touch);
}

/* Stile per i controlli disabilitati */
.leaflet-control a.leaflet-disabled {
    cursor: default;
    background-color: #f4f4f4; /* Fallback */
    background-color: var(--leaflet-disabled-bg-color); /* Colore di sfondo leggermente diverso */
    color: #bbb; /* Fallback */
    color: var(--leaflet-disabled-text-color); /* Colore dell'icona sbiadito */
    pointer-events: none; /* Impedisce i click */
}

/* --- Pulsante Geolocalizzazione --- */
.leaflet-control-geolocate a {
    display: flex;
    justify-content: center;
    align-items: center;
}

/* Stato attivo/localizzazione */
.leaflet-control-geolocate.active a,
.leaflet-control-locate a.locating {
  background-color: #007bff; /* Fallback */
  background-color: var(--leaflet-active-color);
  color: #ffa600 !important; /* Fallback */
  color: var(--leaflet-accent-color) !important; /* Unificato colore durante localizzazione */
  border-color: #0069d9; /* Fallback */
  border-color: var(--leaflet-active-border-color);
  animation: pulse-locate 1.5s infinite; /* Fallback */
  animation: pulse-locate var(--leaflet-pulse-duration) infinite;
}

/* Spinner di caricamento */
.leaflet-control-geolocate.loading a svg {
  animation: leaflet-control-spin 1s linear infinite; /* Fallback */
  animation: leaflet-control-spin var(--leaflet-spin-duration) linear infinite;
}

@keyframes leaflet-control-spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

@keyframes pulse-locate {
  0% { box-shadow: 0 0 0 0 rgba(var(--leaflet-active-color-rgb), 0.7); }
  70% { box-shadow: 0 0 0 10px rgba(var(--leaflet-active-color-rgb), 0); }
  100% { box-shadow: 0 0 0 0 rgba(var(--leaflet-active-color-rgb), 0); }
}

/* Cerchio di localizzazione */
.leaflet-control-locate-circle {
    stroke: #136AEC; /* Fallback */
    stroke: var(--color-locate, var(--leaflet-color-locate-circle)); /* Usa variabile con fallback */
    stroke-opacity: 1;
    stroke-width: 0;
    stroke-linecap: round;
    stroke-linejoin: round;
    fill: #136AEC; /* Fallback */
    fill: var(--color-locate, var(--leaflet-color-locate-circle)); /* Usa variabile con fallback */
    fill-opacity: 0.15;
    fill-rule: evenodd;
    pointer-events: none;
}

.leaflet-control-locate-circle:hover {
    fill-opacity: 0.3;
}

/* --- Pulsante Reset --- */
.leaflet-control-reset a {
  display: flex;
  justify-content: center;
  align-items: center;
}

.leaflet-control-reset-hidden,
.leaflet-control-reset.leaflet-control-reset-hidden {
  display: none !important;
}

/* --- Pulsante Reset Vista --- */
.leaflet-control-reset-view a {
    display: flex;
    justify-content: center;
    align-items: center;
}

/* Stile per l'icona SVG all'interno del pulsante */
.leaflet-control-reset-view a svg {
    width: 16px;
    height: 16px;
}
/* --- Controllo Layers --- */
.leaflet-control-layers-selector {
    width: 10px; /* Fallback */
    width: var(--leaflet-layers-selector-size);
    height: 10px; /* Fallback */
    height: var(--leaflet-layers-selector-size);
    border: 1px solid #000; /* Fallback */
    border: 1px solid var(--leaflet-black);
}

/************ NOTIFICHE TOAST ************/
.leaflet-toast-notification {
    position: absolute;
    top: 10px;
    left: 50%;
    transform: translateX(-50%) translateY(-150%); /* Inizia fuori schermo */
    background-color: #fff; /* Fallback */
    background-color: var(--leaflet-background-color);
    color: #333; /* Fallback */
    color: var(--leaflet-text-color);
    padding: 12px 20px; /* Fallback */
    padding: var(--leaflet-toast-padding);
    border-radius: 8px; /* Fallback */
    border-radius: var(--leaflet-toast-border-radius);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); /* Fallback */
    box-shadow: 0 4px 12px var(--leaflet-box-shadow-color);
    z-index: 1010;
    opacity: 0;
    transition: transform 0.4s ease-in-out, opacity 0.4s ease-in-out; /* Fallback */
    transition: transform var(--leaflet-toast-transition-duration) ease-in-out, opacity var(--leaflet-toast-transition-duration) ease-in-out;
    max-width: 90%;
    text-align: center;
    font-size: 0.9rem;
    pointer-events: none;
    border-left: 5px solid #ffc107; /* Fallback */
    border-left: 5px solid var(--leaflet-color-warning); /* Giallo per warning di default */
}

.leaflet-toast-notification.show {
    transform: translateX(-50%) translateY(0); /* Scende in posizione */
    opacity: 1;
}

/* Variazioni per tipo di messaggio */
.leaflet-toast-notification.error, /* Supporto per vecchia classe */
.leaflet-toast-notification[data-type="error"] {
    border-left-color: #dc3545; /* Fallback */
    border-left-color: var(--leaflet-color-error); /* Rosso per errore */
}

.leaflet-toast-notification.warning {
    border-left-color: #ffc107; /* Fallback */
    border-left-color: var(--leaflet-color-warning); /* Giallo per avviso */
}

/************ MARKER CLUSTER ************/

/* Stili base per l'animazione dei cluster */
.leaflet-cluster-anim .leaflet-marker-icon,
.leaflet-cluster-anim .leaflet-marker-shadow {
    -webkit-transition: -webkit-transform 0.3s ease-out, opacity 0.3s ease-in;
    transition: transform 0.3s ease-out, opacity 0.3s ease-in;
}

/* Stile per le "zampe di ragno" quando si espande un cluster */
.leaflet-cluster-spider-leg {
    -webkit-transition: -webkit-stroke-dashoffset 0.3s ease-out, -webkit-stroke-opacity 0.3s ease-in;
    transition: stroke-dashoffset 0.3s ease-out, stroke-opacity 0.3s ease-in;
}

/* Stile base per l'icona del cluster */
.marker-cluster {
    background-clip: padding-box;
    border-radius: 20px;
}

.marker-cluster div {
    width: 30px;
    height: 30px;
    margin-left: 5px;
    margin-top: 5px;
    text-align: center;
    border-radius: 15px;
    font: bold 12px "Helvetica Neue", Arial, Helvetica, sans-serif;
    color: #fff; /* Fallback */
    color: var(--leaflet-background-color);
    text-shadow: 1px 1px 2px rgba(0,0,0,0.5);
}

.marker-cluster span {
    line-height: 30px;
}

/* Cluster Piccoli */
.marker-cluster-small {
    background-color: rgba(0, 120, 168, 0.5); /* Fallback */
    background-color: rgba(var(--leaflet-cluster-small-color-rgb), 0.5);
}
.marker-cluster-small div {
    background-color: rgba(0, 120, 168, 0.8); /* Fallback */
    background-color: rgba(var(--leaflet-cluster-small-color-rgb), 0.8);
}

/* Cluster Medi */
.marker-cluster-medium {
    background-color: rgba(3, 9, 94, 0.5); /* Fallback */
    background-color: rgba(var(--leaflet-cluster-medium-color-rgb), 0.5);
}
.marker-cluster-medium div {
    background-color: rgba(3, 9, 94, 0.8); /* Fallback */
    background-color: rgba(var(--leaflet-cluster-medium-color-rgb), 0.8);
}

/* Cluster Grandi */
.marker-cluster-large {
    background-color: rgba(255, 166, 0, 0.5); /* Fallback */
    background-color: rgba(var(--leaflet-cluster-large-color-rgb), 0.5);
}
.marker-cluster-large div {
    background-color: rgba(255, 166, 0, 0.8); /* Fallback */
    background-color: rgba(var(--leaflet-cluster-large-color-rgb), 0.8);
}

/************ HACKS E FIX ************/

/* Hack per Safari che previene "stiramento" dei layer durante caricamento tile */
.leaflet-safari .leaflet-tile-container {
	width: 1600px;
	height: 1600px;
	-webkit-transform-origin: 0 0;
	transform-origin: 0 0;
}

/* Permette il "click-through" su alcuni layer interattivi */
.leaflet-marker-icon.leaflet-interactive,
.leaflet-image-layer.leaflet-interactive,
.leaflet-pane > svg path.leaflet-interactive,
svg.leaflet-image-layer.leaflet-interactive path {
	pointer-events: auto; /* Standard rispetto a 'visiblePainted' */
}

/************ MEDIA QUERIES ************/

/* Aggiustamento mappa per cellulare */
@media only screen and (max-width: 600px) {
    /* Ridefinizione variabili per mobile (Browser Moderni) */
    :root {
        --leaflet-popup-content-width: 260px; /* Popup più larghi per leggibilità */
        --leaflet-control-margin: 10px; /* Margini aumentati per facilitare il tocco */
    }

	#mapdettaglio,
    #map,
    #map-display {
	  height: 75vh; /* Altezza bilanciata per mobile */
      min-height: 300px; /* Evita mappe troppo basse */
	}

    /* Ottimizzazione Popup: larghezza dinamica ma contenuta */
    .leaflet-popup-content {
        max-width: 80vw; /* Evita overflow orizzontale */
    }

    /* Notifiche più larghe e leggibili */
    .leaflet-toast-notification {
        width: 90%;
        font-size: 0.85rem;
    }
}
