/* =====================================================
   ATLAS - PATCH RESPONSIVE v1.0
   Va caricato PER ULTIMO in index.html (dopo mezzi.css),
   perche' deve poter correggere gli altri fogli di stile.

   Integra (non sostituisce) mobile.css: qui stanno solo le
   rifiniture che mancavano per l'uso su telefono e tablet.
   ===================================================== */

/* -----------------------------------------------------
   1. NIENTE SFORAMENTO ORIZZONTALE
   La causa n.1 della pagina "che scorre di lato" su telefono.
   ----------------------------------------------------- */
@media (max-width: 992px) {
    html, body {
        max-width: 100%;
        overflow-x: hidden;
    }
    .main-content {
        max-width: 100%;
        overflow-x: hidden;
        padding-left: 12px !important;
        padding-right: 12px !important;
    }
    /* Immagini e contenuti non sfondano mai il contenitore */
    img, canvas, video, iframe {
        max-width: 100%;
        height: auto;
    }
}

/* -----------------------------------------------------
   2. GRIGLIE E FORM A COLONNA SINGOLA
   Molte schermate usano griglie a 2-4 colonne fisse: su
   telefono i campi diventano illeggibili.
   ----------------------------------------------------- */
@media (max-width: 768px) {
    .form-row,
    .form-grid,
    .grid-2, .grid-3, .grid-4,
    .stats-grid,
    .cards-grid,
    .dashboard-grid,
    .row-2col, .row-3col {
        display: grid !important;
        grid-template-columns: 1fr !important;
        gap: 12px !important;
    }
    /* I campi affiancati passano uno sotto l'altro */
    .form-group {
        width: 100% !important;
        min-width: 0 !important;
    }
    /* Intestazioni di pagina: titolo sopra, pulsanti sotto */
    .page-header {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 10px !important;
    }
    .page-header h2 {
        font-size: 20px !important;
    }
    .header-actions,
    .header-filters {
        display: flex !important;
        flex-wrap: wrap !important;
        gap: 8px !important;
        width: 100% !important;
    }
    .header-actions .btn,
    .header-filters .form-control {
        flex: 1 1 auto !important;
        min-width: 130px !important;
    }
}

/* -----------------------------------------------------
   3. TABELLE SEMPRE SCORREVOLI
   Il modulo responsive-mobile.js aggiunge il contenitore
   alle tabelle che ne erano prive; qui lo rendiamo scorrevole
   con un'ombra che suggerisce lo scorrimento laterale.
   ----------------------------------------------------- */
@media (max-width: 992px) {
    .table-container,
    .table-responsive,
    .table-wrapper {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        max-width: 100%;
    }
    table.data-table {
        min-width: 600px; /* sotto questa soglia scorre invece di comprimersi */
    }
}

/* -----------------------------------------------------
   4. AREE TATTILI E LEGGIBILITA'
   Le linee guida indicano almeno 44x44 px per un tocco
   affidabile con il dito.
   ----------------------------------------------------- */
@media (max-width: 768px) {
    .btn,
    .menu-item,
    button,
    .modal-close,
    .close-btn {
        min-height: 44px;
    }
    .btn-sm {
        min-height: 36px;
        padding: 8px 12px !important;
    }
    /* Pulsanti nelle tabelle: restano compatti ma toccabili */
    .data-table .btn-sm {
        min-height: 32px;
        padding: 6px 10px !important;
    }
    /* IMPORTANTE: 16px impedisce a iOS di ingrandire la pagina
       automaticamente quando si tocca un campo di testo. */
    input, select, textarea, .form-control {
        font-size: 16px !important;
    }
}

/* -----------------------------------------------------
   5. MODALI A TUTTO SCHERMO SU TELEFONO
   ----------------------------------------------------- */
@media (max-width: 768px) {
    .modal {
        align-items: flex-end !important;
    }
    .modal-content {
        width: 100% !important;
        max-width: 100% !important;
        max-height: 92vh !important;
        margin: 0 !important;
        border-radius: 16px 16px 0 0 !important;
        overflow-y: auto;
    }
    .modal-footer {
        display: flex !important;
        flex-direction: column-reverse !important;
        gap: 8px !important;
        position: sticky;
        bottom: 0;
        background: #fff;
        padding-bottom: env(safe-area-inset-bottom, 10px);
    }
    .modal-footer .btn {
        width: 100% !important;
    }
}

/* -----------------------------------------------------
   6. BARRA SUPERIORE
   ----------------------------------------------------- */
@media (max-width: 768px) {
    .topbar,
    .header,
    .top-header {
        padding-left: 10px !important;
        padding-right: 10px !important;
        gap: 8px !important;
    }
    /* L'email dell'utente occupa troppo spazio: la nascondiamo */
    .user-email,
    .topbar .user-info span:not(.badge) {
        display: none !important;
    }
    .menu-toggle {
        display: inline-flex !important;
        align-items: center;
        justify-content: center;
        min-width: 44px;
        min-height: 44px;
        font-size: 20px;
    }
}

/* Su desktop l'hamburger non serve */
@media (min-width: 993px) {
    .menu-toggle {
        display: none !important;
    }
}

/* -----------------------------------------------------
   7. OVERLAY DEL MENU
   ----------------------------------------------------- */
@media (max-width: 992px) {
    .sidebar-overlay {
        position: fixed;
        top: 0; left: 0;
        width: 100%; height: 100%;
        background: rgba(0,0,0,0.45);
        z-index: 1000;
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.25s ease;
    }
    .sidebar-overlay.show,
    .sidebar-overlay.active {
        opacity: 1;
        visibility: visible;
    }
}

/* -----------------------------------------------------
   8. TABLET (769-992px): due colonne invece di quattro
   ----------------------------------------------------- */
@media (min-width: 769px) and (max-width: 992px) {
    .grid-3, .grid-4,
    .stats-grid,
    .cards-grid,
    .dashboard-grid {
        display: grid !important;
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 14px !important;
    }
}

/* -----------------------------------------------------
   9. STAMPA: niente menu nei PDF/stampe
   ----------------------------------------------------- */
@media print {
    .sidebar, .sidebar-overlay, .menu-toggle,
    .header-actions, .btn {
        display: none !important;
    }
    .main-content {
        margin-left: 0 !important;
        padding: 0 !important;
    }
}

/* =====================================================
   10. CORREZIONI DOPO PROVA SU TELEFONO (v1.1)
   ===================================================== */

/* -----------------------------------------------------
   10a. BARRA SUPERIORE: ATLAS finiva a destra
   Causa: '.header-left' ha 'margin-left: 250px' pensato per
   lasciare spazio alla sidebar del DESKTOP. Su telefono quel
   margine spingeva logo e hamburger fuori posto, e il blocco
   di destra (badge ruolo + logout) traboccava sopra i contenuti.
   ----------------------------------------------------- */
@media (max-width: 992px) {
    .header-left,
    body:has(.sidebar.collapsed) .header-left,
    body:has(.sidebar.show) .header-left {
        margin-left: 0 !important;
    }
    .header {
        display: flex !important;
        flex-wrap: nowrap !important;
        justify-content: space-between !important;
        align-items: center !important;
        height: 56px !important;
        padding: 8px 12px !important;
        gap: 8px !important;
    }
    .header-left {
        flex: 1 1 auto;
        min-width: 0;
        gap: 10px !important;
    }
    .header-right {
        flex: 0 0 auto;
        gap: 6px !important;
    }
    /* Il contenuto scorre sotto la barra fissa: gli lasciamo spazio */
    .main-content {
        padding-top: 66px !important;
    }
}

/* -----------------------------------------------------
   10b. BADGE RUOLO (Super Admin): occupava troppo spazio
   Viene creato da roles.js con stile inline, quindi serve
   '!important' per ridimensionarlo su schermi piccoli.
   ----------------------------------------------------- */
@media (max-width: 768px) {
    #userRoleBadge {
        font-size: 10px !important;
        padding: 3px 8px !important;
        margin-left: 4px !important;
        max-width: 110px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
    }
}
@media (max-width: 480px) {
    /* Su telefoni stretti resta solo l'icona dello scudo */
    #userRoleBadge {
        max-width: 32px !important;
        padding: 4px 6px !important;
        font-size: 0 !important;   /* nasconde il testo */
    }
    #userRoleBadge i {
        font-size: 13px !important;
    }
}

/* -----------------------------------------------------
   10c. PANNELLO MENU A TUTTA LARGHEZZA
   Causa: 'sidebar-always-open.css' imposta '.sidebar {width:220px}'
   sotto i 992px e, caricandosi DOPO mobile.css, vinceva sulla
   larghezza piena del pannello. Risultato: menu stretto e voci
   lunghe tagliate ("Domande di ammissione", "Registro Ruoli...").
   ----------------------------------------------------- */
@media (max-width: 768px) {
    .sidebar,
    .sidebar.collapsed,
    .sidebar.show,
    .sidebar.collapsed.show {
        width: 100% !important;
        max-width: 100% !important;
        left: 0 !important;
        right: 0 !important;
    }
    /* Le voci del menu non vengono più troncate */
    .sidebar .menu-item {
        padding: 14px 18px !important;
        white-space: normal !important;
        overflow: visible !important;
    }
    .sidebar .menu-item span {
        white-space: normal !important;
        overflow: visible !important;
        text-overflow: clip !important;
        font-size: 15px !important;
    }
    .sidebar .menu-item i {
        min-width: 26px;
        text-align: center;
    }
    /* Pannello più alto e scorrevole: prima si vedevano poche voci */
    .sidebar {
        max-height: 80vh !important;
    }
}

/* Tablet: sidebar laterale a larghezza piena leggibile */
@media (min-width: 769px) and (max-width: 992px) {
    .sidebar,
    .sidebar.collapsed,
    .sidebar.collapsed.show {
        width: 280px !important;
    }
    .sidebar .menu-item span {
        white-space: normal !important;
        font-size: 14px !important;
    }
}

/* =====================================================
   11. TABELLE COME SCHEDE SU TELEFONO (v1.2)
   Una tabella a 8 colonne su schermo stretto e' illeggibile
   anche scorrendo. Qui ogni riga diventa una scheda con
   "Etichetta: valore", grazie all'attributo data-label che
   responsive-mobile.js copia dalle intestazioni.
   ===================================================== */
@media (max-width: 768px) {
    /* Il contenitore non deve piu' scorrere di lato */
    .table-container,
    .table-responsive,
    .table-wrapper {
        overflow-x: visible !important;
    }

    table.data-table {
        min-width: 0 !important;
        width: 100% !important;
        border: none !important;
        background: transparent !important;
        display: block;
    }

    /* Le intestazioni non servono piu': l'etichetta e' in ogni cella */
    table.data-table thead {
        display: none !important;
    }

    table.data-table tbody,
    table.data-table tr,
    table.data-table td {
        display: block !important;
        width: auto !important;
    }

    /* Ogni riga = una scheda */
    table.data-table tbody tr {
        background: #fff;
        border: 1px solid #e5e7eb;
        border-left: 4px solid #3b82f6;
        border-radius: 10px;
        margin-bottom: 12px;
        padding: 6px 4px;
        box-shadow: 0 1px 3px rgba(0,0,0,0.06);
    }

    /* Ogni cella = una riga "Etichetta ....... valore" */
    table.data-table tbody td {
        display: flex !important;
        justify-content: space-between;
        align-items: center;
        gap: 12px;
        padding: 8px 12px !important;
        border: none !important;
        border-bottom: 1px solid #f3f4f6 !important;
        text-align: right !important;
        font-size: 14px;
        min-height: 0 !important;
        white-space: normal !important;
    }

    table.data-table tbody td:last-child {
        border-bottom: none !important;
    }

    /* L'etichetta presa dall'intestazione */
    table.data-table tbody td::before {
        content: attr(data-label);
        font-weight: 600;
        color: #6b7280;
        font-size: 12px;
        text-transform: uppercase;
        letter-spacing: 0.3px;
        text-align: left;
        flex: 0 0 42%;
        white-space: normal;
    }

    /* Celle senza etichetta (es. caselle di selezione) restano centrate */
    table.data-table tbody td:not([data-label]) {
        justify-content: center;
        text-align: center !important;
    }

    /* La colonna "Azioni": pulsanti in fila, ben toccabili */
    table.data-table tbody td[data-label="Azioni"],
    table.data-table tbody td[data-label="Azione"] {
        flex-wrap: wrap;
        justify-content: flex-end;
        gap: 8px;
        padding-top: 12px !important;
        background: #fafafa;
        border-radius: 0 0 8px 8px;
    }
    table.data-table tbody td[data-label="Azioni"] .btn,
    table.data-table tbody td[data-label="Azione"] .btn {
        min-height: 38px;
        min-width: 44px;
    }

    /* Righe di messaggio ("Nessun dato trovato") */
    table.data-table tbody tr.riga-messaggio {
        border-left-color: #d1d5db;
    }
    table.data-table tbody tr.riga-messaggio td {
        justify-content: center;
        text-align: center !important;
    }
    table.data-table tbody tr.riga-messaggio td::before {
        content: none;
    }

    /* Prima cella (di solito il nome/tessera): piu' evidente */
    table.data-table tbody td:first-child {
        background: #f8fafc;
        border-radius: 8px 8px 0 0;
        font-weight: 600;
        color: #1e3a8a;
    }
}

/* =====================================================
   12. LEGGIBILITA' GENERALE SU TELEFONO
   ===================================================== */
@media (max-width: 768px) {
    body {
        font-size: 15px;
        line-height: 1.5;
    }
    .card, .box, .panel {
        border-radius: 12px !important;
        margin-bottom: 12px !important;
    }
    .card-header h3,
    .card-header h2 {
        font-size: 16px !important;
    }
    .card-body {
        padding: 14px !important;
    }
    /* Riquadri statistiche: due per riga invece di uno enorme */
    .stat-card, .stats-box {
        padding: 12px !important;
    }
    .stat-card h3, .stat-value {
        font-size: 22px !important;
    }
    /* Etichette dei campi piu' leggibili */
    label, .form-label {
        font-size: 13px !important;
        font-weight: 600 !important;
        margin-bottom: 4px !important;
    }
    /* Evita testi che sfondano (email, codici lunghi) */
    td, th, .card, p, span {
        overflow-wrap: break-word;
        word-break: break-word;
    }
}
