:root {
    --color-primary: #3498db;
    --color-dark-slate: #2c3e50;
    --color-success-green: #27ae60;
    --color-error-orange: #e67e22;
    --font-stack-standard: 'Segoe UI', -apple-system, BlinkMacSystemFont, Roboto, sans-serif;
}

body {
    font-family: var(--font-stack-standard);
    margin: 0;
    display: flex;
    height: 100vh;
    overflow: hidden;
    background-color: #f0f2f5;
}

/* --- LAYOUT SIDEBAR --- */
#application-sidebar {
    width: 340px;
    background: #ffffff;
    padding: 20px;
    box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    z-index: 2000;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

#application-sidebar h2 {
    color: var(--color-dark-slate);
    font-size: 1.3rem;
    margin-top: 0;
    border-bottom: 2px solid var(--color-primary);
    padding-bottom: 10px;
}

.filter-group {
    margin-bottom: 12px;
}

.filter-group label {
    display: block;
    font-size: 0.8rem;
    font-weight: 600;
    color: #555555;
    margin-bottom: 4px;
}

.filter-group select {
    width: 100%;
    padding: 12px;
    border: 1px solid #cccccc;
    border-radius: 8px;
    background-color: #ffffff;
    font-size: 16px;
    box-sizing: border-box;
}

.filter-group select:disabled {
    background-color: #f9f9f9;
    color: #aaaaaa;
}

/* --- COMPONENTI COMPLEMENTARI (NOTE E FONTI) --- */
.information-feed-box {
    margin-top: 12px;
    font-size: 0.82rem;
    line-height: 1.4;
    display: none;
    padding: 10px;
    border-radius: 4px;
    flex-shrink: 0;
}

.indicator-theme {
    color: #555555;
    font-style: italic;
    background: #f9f9f9;
    border-left: 4px solid var(--color-primary);
}

.sources-theme {
    color: var(--color-dark-slate);
    background: #fcfcfc;
    border-left: 4px solid #7f8c8d;
}

.sources-theme a {
    color: var(--color-primary);
    text-decoration: underline;
    font-weight: 600;
}

/* --- PULSANTI E STATO --- */
#btn-load-map {
    margin-top: 15px;
    padding: 15px;
    background: var(--color-success-green);
    color: #ffffff;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-weight: bold;
    font-size: 1rem;
    transition: background 0.2s ease;
    flex-shrink: 0;
}

#btn-load-map:hover {
    background: #219150;
}

#btn-load-map:disabled {
    background: #bdc3c7;
    cursor: not-allowed;
}

#sidebar-status-bar {
    margin-top: auto;
    font-size: 0.85rem;
    color: #7f8c8d;
    text-align: center;
    padding-top: 15px;
    border-top: 1px solid #eeeeee;
    flex-shrink: 0;
}

/* --- MAPPA CONTENITORE --- */
#leaflet-map-container {
    flex-grow: 1;
    z-index: 1;
    background: #e5e5e5;
    position: relative; /* Indispensabile per posizionare l'overlay del titolo */
}

/* --- TITOLO DINAMICO OVERLAY --- */
.map-title-overlay {
    position: absolute;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 999;
    background: rgba(255, 255, 255, 0.95);
    padding: 10px 22px;
    border-radius: 6px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.15);
    font-weight: 600;
    color: var(--color-dark-slate);
    font-size: 0.95rem;
    text-align: center;
    pointer-events: none;
    max-width: 60%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    border-left: 4px solid var(--color-primary);
}

/* --- INTERFACCIA MOBILE (TRIGGER & BREAKPOINTS) --- */
#mobile-sidebar-toggle {
    display: none;
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 3000;
    background: var(--color-dark-slate);
    color: #ffffff;
    border: none;
    border-radius: 50%;
    width: 60px;
    height: 60px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    font-size: 24px;
    cursor: pointer;
}

@media (max-width: 768px) {
    #application-sidebar {
        position: fixed;
        top: 0;
        left: 0;
        height: 100%;
        transform: translateX(-100%);
        width: 280px;
    }

    #application-sidebar.open {
        transform: translateX(0);
    }

    #mobile-sidebar-toggle {
        display: block;
    }

    .info.legend {
        margin-right: 15px !important;
        margin-bottom: 90px !important;
        padding: 10px !important;
    }

    .map-title-overlay {
        font-size: 0.78rem;
        top: 12px;
        max-width: 80%;
        padding: 8px 14px;
    }
}

/* --- OTTIMIZZAZIONE COMPATTEZZA LANDSCAPE MOBILE --- */
@media (max-height: 550px) and (max-width: 950px) {
    #application-sidebar {
        padding: 10px 15px;
        width: 250px;
    }
    #application-sidebar h2 {
        font-size: 1.05rem;
        margin-bottom: 6px;
        padding-bottom: 4px;
    }
    .filter-group {
        margin-bottom: 5px;
    }
    .filter-group label {
        font-size: 0.75rem;
        margin-bottom: 2px;
    }
    .filter-group select {
        padding: 6px 10px;
        font-size: 14px;
        border-radius: 6px;
    }
    #btn-load-map {
        margin-top: 8px;
        padding: 10px;
        font-size: 14px;
    }
    .information-feed-box {
        margin-top: 6px;
        padding: 6px;
        font-size: 0.7rem;
    }
    #sidebar-status-bar {
        padding-top: 8px;
        font-size: 0.75rem;
    }
    
    .info.legend {
        margin-right: 10px !important;
        margin-bottom: 10px !important;
        padding: 12px 14px !important; 
        max-height: 75vh !important; 
        overflow-y: auto !important; 
        font-size: 0.8rem !important;  
        line-height: 1.6 !important;   
        pointer-events: auto !important;
        touch-action: pan-y !important; 
    }

    .info.legend i {
        width: 14px !important;       
        height: 14px !important;
        margin-right: 8px !important;
        vertical-align: middle !important; 
    }

    #leaflet-map-container {
        min-height: 100vh !important;
    }

    #stats-histogram-labels {
        display: flex !important;
        justify-content: space-between !important;
        width: 100% !important;
        padding: 4px 4px 0 4px !important; 
        box-sizing: border-box !important;
    }

    .histogram-label-tick {
        width: 18% !important;          
        text-align: center !important;   
        font-size: 0.62rem !important;   
        letter-spacing: -0.4px !important; 
        white-space: nowrap !important;  
    }
}

/* --- ASYNC LOADERS & MODALS --- */
#global-async-loader {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 9999;
    background: rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(3px);
    flex-direction: column;
}

.loading-spinner {
    border: 4px solid #f3f3f3;
    border-top: 4px solid var(--color-primary);
    border-radius: 50%;
    width: 50px;
    height: 50px;
    animation: spinAnimation 0.8s linear infinite;
}

@keyframes spinAnimation {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

#global-error-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(44, 62, 80, 0.6);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 10000;
    backdrop-filter: blur(2px);
}

.modal-dialog-content {
    background: #ffffff;
    padding: 25px;
    border-radius: 12px;
    width: 85%;
    max-width: 350px;
    text-align: center;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
}

.modal-icon-alert {
    font-size: 50px;
    color: var(--color-error-orange);
    margin-bottom: 15px;
}

#btn-close-error-modal {
    background: var(--color-dark-slate);
    color: white;
    border: none;
    padding: 10px 30px;
    border-radius: 6px;
    font-weight: bold;
    cursor: pointer;
}

/* --- TOOLTIP & LEGENDA MAPPA --- */
.custom-tooltip {
    background: rgba(44, 62, 80, 0.9);
    border: none;
    border-radius: 4px;
    color: white;
    font-weight: bold;
    font-size: 14px;
    padding: 5px 10px;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}

.info.legend {
    background: rgba(255, 255, 255, 0.95);
    padding: 20px 15px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
    border-radius: 8px;
    margin-right: 50px !important;
    margin-bottom: 40px !important;
}

/* --- INTEGRAZIONE STRUTTURALE ISTOGRAMMA E ASSE X --- */
#stats-histogram-bars {
    display: flex; 
    align-items: flex-end; 
    justify-content: space-between; 
    height: 65px; 
    padding: 8px 6px 0 6px; 
    background: #ffffff; 
    border: 1px solid #e0e0e0; 
    border-bottom: none;
    border-radius: 4px 4px 0 0;
}

#stats-histogram-labels {
    display: flex;
    justify-content: space-between;
    padding: 2px 2px 4px 2px;
    background: #ffffff;
    border: 1px solid #e0e0e0;
    border-top: none;
    border-radius: 0 0 4px 4px;
    font-family: var(--font-stack-standard);
}

.histogram-label-tick {
    width: 18%;
    text-align: center;
    font-size: 0.65rem; 
    color: #555;
    font-weight: 600;
    white-space: nowrap;
}

/* Ottimizzazione chirurgica del titolo per Mobile Landscape */
@media (max-height: 550px) and (orientation: landscape) {
    .map-title-overlay {
        top: 1px !important;         /* Solleva la barra quasi al limite del display */
        font-size: 0.8rem !important;/* Riduce la dimensione del testo */
        padding: 4px 16px !important;/* Rende il box nero/bianco molto più sottile */
        max-width: 80% !important;   /* Evita tagli aggressivi sul testo */
    }
}