/* ========================================
   RESPONSIVE MOBILE - RESCUE_SAT
   Barra blu + barra bianca (solo 4 top buttons)
   + barra sottopulsanti separata scrollabile
   ======================================== */

/* Desktop: il pannello hazard mobile NON deve mai vedersi */
#hazard-buttons-mobile {
    display: none;
}

/* ===== MOBILE ===== */
@media (max-width: 768px) {

    /* ===== Base ===== */
    html, body {
        width: 100%;
        max-width: 100%;
        overflow-x: hidden !important;
        overflow-y: auto !important;
        height: auto !important;
        margin: 0;
        padding: 0;
        -webkit-overflow-scrolling: touch;
    }

    body {
        position: relative;
    }

    /* ===== Nascondi bottoni desktop mappa ===== */
    .map-buttons,
    #btn-osm,
    #btn-sat,
    #btn-raster {
        display: none !important;
        visibility: hidden !important;
    }

    /* ===== Barra blu fissa ===== */
    #top-bar-blue {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        height: 50px !important;
        padding: 5px 10px !important;
        display: flex !important;
        justify-content: space-between !important;
        align-items: center !important;
        z-index: 2000 !important;
        background: #0066CC !important;
        box-sizing: border-box !important;
    }

    #top-bar-blue h1 {
        font-size: 18px !important;
        margin: 0 !important;
        line-height: 1 !important;
    }

    #top-bar-blue .version {
        font-size: 10px !important;
    }

    /* ===== Barra bianca SOLO top buttons ===== */
    #top-bar-white {
        position: fixed !important;
        top: 50px !important;
        left: 0 !important;
        right: 0 !important;
        height: 57px !important;
        z-index: 2000 !important;
        background: #fff !important;
        box-shadow: 0 2px 4px rgba(0,0,0,0.12) !important;
        padding: 6px !important;
        box-sizing: border-box !important;
        display: flex !important;
        align-items: center !important;
        overflow: hidden !important;
    }

    /* Nascondi loghi su mobile */
    #logo1, #logo2 {
        display: none !important;
    }

    /* Riga 4 pulsanti principali */
    #top-buttons {
        display: flex !important;
        width: 100% !important;
        gap: 6px !important;
        justify-content: space-between !important;
        align-items: center !important;
        flex-wrap: nowrap !important;
        box-sizing: border-box !important;
    }

    #top-buttons button {
        flex: 1 1 0 !important;
        min-width: 0 !important;
        height: 40px !important;
        padding: 6px 4px !important;
        font-size: 10px !important;
        line-height: 1.1 !important;
        white-space: nowrap !important;
        text-align: center !important;
        box-sizing: border-box !important;
    }

    /* =========================================================
       SOTTO-PULSANTI STANDARD (NO HAZARD desktop)
       geodata / structure / risk
       ========================================================= */
    #geodata-buttons,
    #structure-buttons,
    #risk-buttons {
        display: none !important;
        position: fixed !important;
        top: 106px !important; /* 50 + 56 */
        left: 0 !important;
        right: 0 !important;
        width: 100vw !important;
        max-width: 100vw !important;

        /* reset desktop */
        bottom: auto !important;
        transform: none !important;
        margin: 0 !important;

        z-index: 1900 !important;
        padding: 6px !important;
        box-sizing: border-box !important;

        background: rgba(255,255,255,0.96) !important;
        box-shadow: 0 2px 4px rgba(0,0,0,0.10) !important;

        overflow-x: auto !important;
        overflow-y: hidden !important;
        -webkit-overflow-scrolling: touch !important;
        white-space: nowrap !important;
        scrollbar-width: thin !important;

        flex-direction: row !important;
        align-items: center !important;
        justify-content: flex-start !important;
        gap: 6px !important;
    }

    #geodata-buttons.is-visible,
    #structure-buttons.is-visible,
    #risk-buttons.is-visible {
        display: flex !important;
        flex-direction: row !important;
        flex-wrap: nowrap !important;
        align-items: center !important;
        justify-content: flex-start !important;
        gap: 6px !important;
    }

    /* reset margini/padding interni (solo pannelli standard) */
    #geodata-buttons *,
    #structure-buttons *,
    #risk-buttons * {
        margin-left: 0 !important;
        margin-right: 0 !important;
        box-sizing: border-box !important;
    }

    /* stile sottopulsanti standard */
    #geodata-buttons button,
    #structure-buttons button,
    #risk-buttons button,
    #structure-buttons .structure-link,
    #risk-buttons .structure-link {
        display: inline-flex !important;
        align-items: center !important;
        justify-content: center !important;
        flex: 0 0 auto !important;

        min-width: 88px !important;
        width: auto !important;
        max-width: none !important;

        height: 32px !important;
        padding: 4px 8px !important;

        font-size: 9px !important;
        line-height: 1.05 !important;
        white-space: nowrap !important;
        text-align: center !important;
        border-radius: 4px !important;
    }

    /* =========================================================
       HAZARD desktop: SEMPRE nascosto su mobile
       (anche se il JS gli mette .is-visible)
       ========================================================= */
    #hazard-buttons,
    #hazard-buttons.is-visible {
        display: none !important;
        visibility: hidden !important;
        pointer-events: none !important;

        /* reset duro di eventuali regole desktop */
        position: fixed !important;
        left: -99999px !important;
        top: -99999px !important;
        right: auto !important;
        transform: none !important;
        width: 0 !important;
        height: 0 !important;
        overflow: hidden !important;
        opacity: 0 !important;
    }

    /* =========================================================
       HAZARD MOBILE (7 pulsanti su una riga scrollabile)
       ========================================================= */
    #hazard-buttons-mobile {
        display: none !important;
        position: fixed !important;
        top: 107px !important; /* sotto barra blu+bianca */
        left: 0 !important;
        right: 0 !important;
        width: 100vw !important;
        max-width: 100vw !important;
        z-index: 1900 !important;

        padding: 6px !important;
        box-sizing: border-box !important;
        background: rgba(255,255,255,0.96) !important;
        box-shadow: 0 2px 4px rgba(0,0,0,0.10) !important;

        overflow-x: auto !important;
        overflow-y: hidden !important;
        -webkit-overflow-scrolling: touch !important;

        white-space: nowrap !important;
        flex-wrap: nowrap !important;
        align-items: center !important;
        justify-content: flex-start !important;
        gap: 6px !important;
    }

    #hazard-buttons-mobile.is-visible {
        display: flex !important;
    }
    /* ===== Pulsanti HAZARD MOBILE: grigi -> blu quando attivi ===== */
#hazard-buttons-mobile button {
    display: inline-flex !important;
    flex: 0 0 auto !important;
    align-items: center !important;
    justify-content: center !important;

    min-width: 78px !important;
    width: auto !important;
    max-width: none !important;
    height: 32px !important;
    padding: 4px 8px !important;

    font-size: 9px !important;
    line-height: 1 !important;
    white-space: nowrap !important;
    text-align: center !important;
    border-radius: 4px !important;
    box-sizing: border-box !important;

    /* stile base */
    color: #fff !important;
    background: #999 !important;
    border: none !important;
    font-weight: bold !important;
    box-shadow: 0 2px 4px rgba(0,0,0,0.18) !important;
    -webkit-appearance: none !important;
    appearance: none !important;
    background-image: none !important;
}

/* hover non attivi */
#hazard-buttons-mobile button:hover {
    background: #7f7f7f !important;
    color: #fff !important;
}

/* attivo */
#hazard-buttons-mobile button.active {
    background: #0066CC !important;
    color: #fff !important;
    box-shadow: 0 3px 6px rgba(0,0,0,0.22) !important;
}

/* hover attivo */
#hazard-buttons-mobile button.active:hover {
    background: #0052A3 !important;
    color: #fff !important;
}

/* focus */
#hazard-buttons-mobile button:focus {
    outline: none !important;
}



    /* ===== MAPPA ===== */
    #map {
        position: relative !important;
        top: 106px !important; /* sotto barra bianca */
        left: 0 !important;
        right: 0 !important;
        width: 100% !important;
        height: 58vh !important;
        margin: 0 !important;
        z-index: 100 !important;
        box-sizing: border-box !important;
    }

    /* mappa più in basso quando c'è una barra sottopulsanti visibile */
    body:has(#geodata-buttons.is-visible) #map,
    body:has(#structure-buttons.is-visible) #map,
    body:has(#risk-buttons.is-visible) #map,
    body:has(#hazard-buttons-mobile.is-visible) #map {
        top: 150px !important;
    }

    /* ===== PANNELLO ===== */
    #side-panel {
        position: relative !important;
        top: 106px !important;
        left: 0 !important;
        right: 0 !important;
        width: 100% !important;
        height: auto !important;
        min-height: 42vh !important;
        z-index: 1000 !important;
        background: #fff !important;
        border-top: 3px solid #0066CC !important;
        box-shadow: 0 -2px 10px rgba(0,0,0,0.08) !important;
        padding: 15px !important;
        margin: 0 !important;
        overflow: visible !important;
        display: block !important;
        box-sizing: border-box !important;
    }

    body:has(#geodata-buttons.is-visible) #side-panel,
    body:has(#structure-buttons.is-visible) #side-panel,
    body:has(#risk-buttons.is-visible) #side-panel,
    body:has(#hazard-buttons-mobile.is-visible) #side-panel {
        top: 150px !important;
    }

    #panel-content {
        padding-bottom: 20px !important;
        font-size: 13px !important;
        line-height: 1.4 !important;
    }

    #panel-content h2,
    #panel-content h3 {
        font-size: 16px !important;
        margin: 5px 0 10px 0 !important;
        color: #0066CC !important;
        line-height: 1.2 !important;
    }

    #panel-content table {
        width: 100% !important;
        font-size: 10px !important;
        display: block !important;
        overflow-x: auto !important;
        margin: 10px 0 !important;
        border-collapse: collapse !important;
    }

    #panel-content img {
        max-width: 100% !important;
        height: auto !important;
        margin: 10px 0 !important;
        display: block !important;
    }

    /* ===== Download panel ===== */
    #hazard-download-panel {
        position: relative !important;
        bottom: auto !important;
        left: auto !important;
        right: auto !important;
        margin: 15px 0 0 0 !important;
        padding: 12px !important;
        background: #f5f5f5 !important;
        border-top: 2px solid #ddd !important;
        box-sizing: border-box !important;
    }

    #hazard-download-panel > div {
        display: flex !important;
        flex-direction: column !important;
        gap: 8px !important;
        justify-content: flex-start !important;
    }

    #hazard-download-panel a {
        width: 100% !important;
        box-sizing: border-box !important;
        text-align: center !important;
    }

/* ===== LEGENDE MOBILE ULTRA COMPATTE NON SCROLLABILI ===== */
#legend-panel,
#legend-risk-panel,
#legend-hazard-panel {
    position: fixed !important;
    left: 6px !important;
    top: 210px !important;

    width: 78px !important;
    max-width: 78px !important;

    height: 120px !important;
    max-height: 120px !important;

    overflow: visible !important;   /* NO SCROLL */
    padding: 2px !important;
    border-radius: 3px !important;
    background: rgba(255,255,255,0.90) !important;
    box-shadow: 0 1px 3px rgba(0,0,0,0.12) !important;
    z-index: 1500 !important;

    font-size: 6px !important;
    line-height: 1 !important;
}



#legend-panel h4,
#legend-risk-panel h4 {
    margin: 0 0 2px 0 !important;
    font-size: 6px !important;
    line-height: 1 !important;
    font-weight: 600 !important;
}

#legend-panel .legend-item,
#legend-risk-panel .legend-item,
#legend-hazard-panel .legend-item {
    display: flex !important;
    align-items: center !important;
    gap: 2px !important;
    margin: 0 0 1px 0 !important;
    min-height: 6px !important;
}

#legend-panel .legend-color,
#legend-risk-panel .legend-color,
#legend-hazard-panel .legend-color {
    width: 5px !important;
    min-width: 5px !important;
    height: 5px !important;
    border: 1px solid rgba(0,0,0,0.12) !important;
    box-sizing: border-box !important;
}

#legend-panel .legend-label,
#legend-risk-panel .legend-label,
#legend-hazard-panel .legend-label {
    font-size: 6px !important;
    line-height: 1 !important;
    white-space: normal !important;
    word-break: break-word !important;
}



/* Se c'è la barra sottopulsanti visibile */
body:has(#geodata-buttons.is-visible) #legend-panel,
body:has(#structure-buttons.is-visible) #legend-panel,
body:has(#risk-buttons.is-visible) #legend-panel,
body:has(#hazard-buttons-mobile.is-visible) #legend-panel,
body:has(#geodata-buttons.is-visible) #legend-risk-panel,
body:has(#structure-buttons.is-visible) #legend-risk-panel,
body:has(#risk-buttons.is-visible) #legend-risk-panel,
body:has(#hazard-buttons-mobile.is-visible) #legend-risk-panel {
    top: 238px !important;
}

body:has(#geodata-buttons.is-visible) #legend-hazard-panel,
body:has(#structure-buttons.is-visible) #legend-hazard-panel,
body:has(#risk-buttons.is-visible) #legend-hazard-panel,
body:has(#hazard-buttons-mobile.is-visible) #legend-hazard-panel {
    top: 238px !important;
}


    /* ===== GPS Leaflet ===== */
    .leaflet-top.leaflet-right .leaflet-bar.leaflet-control {
        position: fixed !important;
        top: 155px !important;
        right: 10px !important;
        z-index: 1500 !important;
    }

    body:has(#geodata-buttons.is-visible) .leaflet-top.leaflet-right .leaflet-bar.leaflet-control,
    body:has(#structure-buttons.is-visible) .leaflet-top.leaflet-right .leaflet-bar.leaflet-control,
    body:has(#risk-buttons.is-visible) .leaflet-top.leaflet-right .leaflet-bar.leaflet-control,
    body:has(#hazard-buttons-mobile.is-visible) .leaflet-top.leaflet-right .leaflet-bar.leaflet-control {
        top: 195px !important;
    }

    /* ===== Menu contestuali ===== */
    #context-menu,
    #context-menu-sat {
        z-index: 2100 !important;
        max-width: calc(100vw - 20px) !important;
    }
}

/* ===== Landscape mobile ===== */
@media (max-width: 768px) and (orientation: landscape) {
    #map {
        height: 50vh !important;
    }

    #side-panel {
        min-height: 50vh !important;
    }
}