/* ============================================================================
   ISIMaps - Layout smartphone e tablet
   ----------------------------------------------------------------------------
   OGNI regola è sotto html.is-phone o html.is-tablet: le classi vengono
   aggiunte in <head> (partials/phone-head.php) solo su smartphone/tablet.
   Il desktop non è toccato. Le regole html:is(.is-phone, .is-tablet) valgono
   per entrambi; quelle html.is-phone solo per i telefoni.
   ============================================================================ */

/* ---------- Base ---------- */
html:is(.is-phone, .is-tablet) body {
    -webkit-text-size-adjust: 100%;
    touch-action: manipulation;   /* niente zoom da doppio tap sui controlli */
    overflow-x: hidden;
}
/* Input a 16px: sotto questa soglia iOS zooma la pagina al focus */
html:is(.is-phone, .is-tablet) input,
html:is(.is-phone, .is-tablet) select,
html:is(.is-phone, .is-tablet) textarea,
html:is(.is-phone, .is-tablet) .form-input,
html:is(.is-phone, .is-tablet) .form-select,
html:is(.is-phone, .is-tablet) .form-textarea {
    font-size: 16px;
}

/* ---------- Shell: sidebar a scomparsa ---------- */
html:is(.is-phone, .is-tablet) .tenant-shell { display: block; }
html:is(.is-phone, .is-tablet) .tenant-sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    height: 100%;
    width: 260px;
    max-width: 82vw;
    z-index: 900;
    transform: translateX(-100%);
    transition: transform .22s ease;
    overflow-y: auto;
    box-shadow: none;
}
html:is(.is-phone, .is-tablet).nav-open .tenant-sidebar {
    transform: none;
    box-shadow: 0 0 40px rgba(0, 0, 0, .3);
}
html:is(.is-phone, .is-tablet) .mobile-nav-backdrop {
    display: block;
    position: fixed; inset: 0;
    background: rgba(0, 0, 0, .45);
    z-index: 899;
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s;
}
html:is(.is-phone, .is-tablet).nav-open .mobile-nav-backdrop {
    opacity: 1;
    pointer-events: auto;
}
html:is(.is-phone, .is-tablet).nav-open body { overflow: hidden; }
html:is(.is-phone, .is-tablet) .tenant-nav a { padding: 11px 12px; font-size: 15px; }

/* Topbar: menu a sinistra, logo, azioni a destra */
html:is(.is-phone, .is-tablet) .tenant-topbar {
    padding: 0 12px;
    gap: 8px;
}
html:is(.is-phone, .is-tablet) .mobile-nav-btn {
    display: inline-flex;
    align-items: center; justify-content: center;
    width: 40px; height: 40px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-elevated);
    color: var(--text);
    flex-shrink: 0;
}
html:is(.is-phone, .is-tablet) .mobile-nav-logo {
    display: inline-flex;
    margin-right: auto;
    font-size: 18px; font-weight: 700;
    letter-spacing: -.02em;
    text-decoration: none;
}
html:is(.is-phone, .is-tablet) .mobile-nav-logo .isi { color: var(--brand); }
html:is(.is-phone, .is-tablet) .mobile-nav-logo .maps { color: var(--text); }

html.is-phone .tenant-content { padding: 16px 12px; }
html.is-phone .tenant-banner { padding: 10px 12px; font-size: 12px; }

/* ---------- Pagine generiche ---------- */
html.is-phone .page-header {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
    margin-bottom: 16px;
}
html.is-phone .page-title { font-size: 20px; }
html.is-phone .page-actions { width: 100%; }
html.is-phone .page-actions .btn { flex: 1 1 auto; }
html.is-phone .card-body { padding: 14px; }

/* Breadcrumb: solo la pagina corrente (il tasto ← porta al livello sopra) */
html.is-phone .breadcrumb-bar { padding: 4px 40px; }
html.is-phone .breadcrumb-nav > a,
html.is-phone .breadcrumb-nav > .breadcrumb-sep,
html.is-phone .breadcrumb-nav > .breadcrumb-current:not(:last-child) {
    display: none;
}
html.is-phone .breadcrumb-nav .breadcrumb-current {
    display: block;
    max-width: calc(100vw - 110px);
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    font-size: 14px;
}

/* Griglie inline multi-colonna → una colonna */
html.is-phone .tenant-content [style*="grid-template-columns"] {
    grid-template-columns: 1fr !important;
}
html.is-phone .form-row { flex-wrap: wrap; }

/* Tabelle larghe: scroll orizzontale invece di sforare la pagina */
html.is-phone .tenant-content table:not(.help-shortcuts):not(.logs-table):not(.inventory-table) {
    display: block;
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}
html.is-phone .user-table { table-layout: auto; }
html.is-phone .user-table th,
html.is-phone .user-table td { white-space: nowrap; }

/* Log e Inventario: ogni riga diventa una scheda compatta */
html.is-phone table.logs-table,
html.is-phone table.inventory-table,
html.is-phone table.logs-table tbody,
html.is-phone table.inventory-table tbody {
    display: block;
    width: 100% !important;
}
html.is-phone table.logs-table thead,
html.is-phone table.inventory-table thead {
    display: none;
}
html.is-phone table.logs-table tr,
html.is-phone table.inventory-table tr {
    display: grid;
    align-items: center;
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
}
html.is-phone table.logs-table tr:last-child,
html.is-phone table.inventory-table tr:last-child { border-bottom: none; }
html.is-phone table.logs-table td,
html.is-phone table.inventory-table td {
    display: block;
    padding: 0 !important;
    border: none;
    width: auto !important;
    min-width: 0;
    text-align: left !important;
    overflow-wrap: anywhere;
}

/* Log: data + azione, poi utente, descrizione e IP su righe intere */
html.is-phone table.logs-table tr {
    grid-template-columns: auto 1fr;
    gap: 4px 10px;
}
html.is-phone table.logs-table td:nth-child(1) { font-size: 12px; }
html.is-phone table.logs-table td:nth-child(2) { justify-self: end; }
html.is-phone table.logs-table td:nth-child(3),
html.is-phone table.logs-table td:nth-child(4),
html.is-phone table.logs-table td:nth-child(5) { grid-column: 1 / -1; }
html.is-phone table.logs-table td:nth-child(3) { font-size: 12px; color: var(--text-muted); }
html.is-phone table.logs-table td:nth-child(5) { font-size: 11px; color: var(--text-subtle); }

/* Inventario: icona | nome + tipo + posizione | apri */
html.is-phone table.inventory-table tr {
    grid-template-columns: 40px 1fr auto;
    column-gap: 10px;
    row-gap: 2px;
}
html.is-phone table.inventory-table td:nth-child(1) { grid-column: 1; grid-row: 1 / 4; align-self: start; }
html.is-phone table.inventory-table td:nth-child(2) { grid-column: 2; grid-row: 1; }
html.is-phone table.inventory-table td:nth-child(3) { grid-column: 2; grid-row: 2; font-size: 12px; }
html.is-phone table.inventory-table td:nth-child(4) { grid-column: 2 / 4; grid-row: 3; font-size: 12px; }
html.is-phone table.inventory-table td:nth-child(5) { grid-column: 3; grid-row: 1 / 3; }
html.is-phone table.inventory-table td:nth-child(5) .btn { padding: 6px 8px; font-size: 12px; }

/* Pagina Team: intestazioni sezioni */
html.is-phone .user-section-header { padding: 12px 14px; }
html.is-phone .user-section-quota {
    margin-left: 0;
    width: 100%;
    white-space: normal;
    justify-content: space-between;
}

/* ---------- Modali: a tutto schermo ---------- */
html.is-phone .modal-backdrop {
    padding: 0 !important;
    align-items: stretch !important;
}
html.is-phone .modal-card {
    max-width: 100% !important;
    width: 100% !important;
    max-height: 100% !important;
    height: 100%;
    border-radius: 0;
    border: none;
}
html.is-phone .modal-card form { max-height: 100%; height: 100%; }
html.is-phone .modal-header { padding: 12px 14px; }
html.is-phone .modal-body { padding: 14px; -webkit-overflow-scrolling: touch; }
html.is-phone .modal-footer {
    padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
    flex-wrap: wrap;
    gap: 8px;
}
html.is-phone .field-row { grid-template-columns: 1fr; }
html.is-phone .inspector-field { grid-template-columns: minmax(80px, 36%) 1fr; }
html.is-phone .isi-dialog-backdrop { align-items: center; }

/* ============================================================================
   MAPPA
   ============================================================================ */

/* La pagina mappa occupa esattamente lo schermo (niente scroll di pagina) */
html.is-phone body.map-page .tenant-main {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
}
html.is-phone body.map-page .tenant-content {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    padding: 6px 0 0;
}
html.is-phone body.map-page .breadcrumb-bar {
    margin: 0 8px 6px;
    min-height: 32px;
}
html.is-phone .map-workspace {
    flex: 1 1 auto;
    height: auto;
    min-height: 0;
    margin: 0;
}

/* Toolbar: una riga scorrevole, solo icone */
html.is-phone .map-toolbar {
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    gap: 2px;
    padding: 4px 6px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
}
html.is-phone .map-toolbar::-webkit-scrollbar { display: none; }
html.is-phone .map-toolbar-group { flex-shrink: 0; }
html.is-phone .map-toolbar-sep { margin: 4px 2px; }
html.is-phone .map-tool-btn { padding: 9px; }
html.is-phone .map-tool-btn span { display: none; }
html.is-phone .map-tool-btn svg { width: 20px; height: 20px; }
/* Non disponibili su touch: statistiche e disegno aree (richiede click destro) */
html.is-phone .map-toolbar-stats,
html.is-phone .map-tool-btn[data-tool="link-zone"] {
    display: none !important;
}

/* Canvas sopra, pannello sotto (il pannello riduce la mappa, non la copre) */
html.is-phone .map-main {
    flex-direction: column;
    position: relative;
}
html.is-phone .map-main .map-canvas-wrap { min-height: 0; }
html:is(.is-phone, .is-tablet) .map-svg {
    touch-action: none;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
}
html.is-phone .map-topbar { top: 8px; left: 8px; right: 8px; }

/* Pannello laterale → pannello inferiore */
html.is-phone .map-side-panel-collapsible {
    width: 100% !important;
    left: 0 !important;
    right: auto !important;
    height: 50% !important;
    flex: 0 0 auto;
    border-left: none !important;
    border-top: 1px solid var(--border) !important;
    transition: none;
    padding-bottom: env(safe-area-inset-bottom);
}
/* Su smartphone il pannello è chiuso di default: aperto solo con .m-open
   (gestito da mobile.js). Da chiuso restano visibili le tab: un tocco su
   una tab riapre il pannello. */
html.is-phone .map-side-panel-collapsible:not(.m-open) {
    height: auto !important;
}
html.is-phone .map-side-panel-collapsible:not(.m-open) > *:not(.map-side-panel-header):not(.map-side-panel-toggle) {
    display: none !important;
}
html.is-phone .map-side-panel-collapsible > .map-side-panel-header {
    display: flex !important;
}
html.is-phone .map-side-panel-collapsible .map-side-panel-header {
    padding-right: 48px;
}
html.is-phone .map-side-panel-tab { padding: 13px 6px; font-size: 14px; }
html.is-phone .map-side-panel-toggle {
    left: auto;
    right: 8px;
    top: 7px;
    width: 34px;
    height: 34px;
    box-shadow: none;
}
html.is-phone .map-side-panel-collapsible.m-open .map-side-panel-toggle-icon {
    transform: rotate(90deg);
}
html.is-phone .map-side-panel-collapsible:not(.m-open) .map-side-panel-toggle-icon {
    transform: rotate(-90deg);
}
html.is-phone .map-side-panel-collapsible .map-side-panel-body {
    padding: 10px 12px 16px;
    -webkit-overflow-scrolling: touch;
}

/* Elementi lista / magazzino più facili da toccare */
html.is-phone .map-list-row { min-height: 42px; }
html.is-phone .map-list-bulk-actions .btn,
html.is-phone .map-list-selection-actions .btn { padding: 8px 10px; }
html.is-phone .warehouse-item,
html.is-phone .warehouse-template-row { min-height: 44px; }
html:is(.is-phone, .is-tablet) .warehouse-item,
html:is(.is-phone, .is-tablet) .warehouse-template-row {
    -webkit-user-drag: none;
    -webkit-touch-callout: none;
}
html.is-phone .warehouse-item-trash { min-width: 36px; min-height: 36px; }

/* Rack: disposizione su una colonna */
html.is-phone .rack-inner-split { grid-template-columns: 1fr; }

/* ---------- Elementi aggiunti da mobile.js ---------- */
/* Barra "piazzamento da magazzino" */
html:is(.is-phone, .is-tablet) .m-place-bar {
    position: absolute;
    left: 8px; right: 8px; top: 8px;
    z-index: 40;
    display: flex; align-items: center; gap: 10px;
    padding: 10px 12px;
    background: var(--bg-elevated);
    border: 1px solid var(--brand);
    border-radius: 10px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, .18);
    font-size: 14px;
}
html:is(.is-phone, .is-tablet) .m-place-bar span { flex: 1; min-width: 0; }

/* Menu azioni (bottom sheet) */
html:is(.is-phone, .is-tablet) .m-sheet-backdrop {
    position: fixed; inset: 0;
    z-index: 1100;
    background: rgba(0, 0, 0, .45);
    display: flex; align-items: flex-end;
}
html:is(.is-phone, .is-tablet) .m-sheet {
    width: 100%;
    background: var(--bg-elevated);
    border-radius: 14px 14px 0 0;
    padding: 8px 12px calc(12px + env(safe-area-inset-bottom));
    display: flex; flex-direction: column; gap: 6px;
}
html:is(.is-phone, .is-tablet) .m-sheet-title {
    padding: 8px 4px 6px;
    font-size: 13px; font-weight: 600;
    color: var(--text-muted);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
html:is(.is-phone, .is-tablet) .m-sheet .btn { width: 100%; padding: 13px; font-size: 15px; }

/* Avviso breve (es. pin bloccato) */
html:is(.is-phone, .is-tablet) .m-toast {
    position: fixed;
    left: 50%; bottom: calc(24px + env(safe-area-inset-bottom));
    transform: translateX(-50%);
    z-index: 1200;
    max-width: calc(100vw - 32px);
    padding: 10px 16px;
    border-radius: 20px;
    background: rgba(15, 23, 42, .92);
    color: #fff;
    font-size: 13px;
    pointer-events: none;
}

/* ============================================================================
   ORIENTAMENTO
   Fuori dall'editor mappa il telefono va tenuto in verticale: in orizzontale
   un avviso copre la pagina. Nell'editor mappa l'orizzontale è consentito
   e ha un layout dedicato (html.m-landscape gestita da mobile.js).
   ============================================================================ */
.m-rotate-overlay { display: none; }
html.is-phone.m-landscape body:not(.map-page) .m-rotate-overlay {
    display: flex;
    position: fixed; inset: 0;
    z-index: 5000;
    flex-direction: column;
    align-items: center; justify-content: center;
    gap: 10px;
    padding: 24px;
    text-align: center;
    background: var(--bg);
    color: var(--text);
}
html.is-phone .m-rotate-overlay svg {
    color: var(--brand);
    animation: m-rotate-hint 2.4s ease-in-out infinite;
}
@keyframes m-rotate-hint {
    0%, 30%   { transform: rotate(-90deg); }
    60%, 100% { transform: rotate(0deg); }
}
html.is-phone .m-rotate-title { font-size: 18px; font-weight: 700; }
html.is-phone .m-rotate-text { font-size: 13px; color: var(--text-muted); max-width: 320px; }
html.is-phone.m-landscape body:not(.map-page) { overflow: hidden; }

/* ---------- Editor mappa in orizzontale ---------- */
/* Niente topbar e breadcrumb: tutta l'altezza alla mappa */
html.is-phone.m-landscape body.map-page .tenant-topbar,
html.is-phone.m-landscape body.map-page .tenant-banner,
html.is-phone.m-landscape body.map-page .breadcrumb-bar {
    display: none;
}
html.is-phone.m-landscape body.map-page .tenant-content { padding: 0; }
html.is-phone.m-landscape .map-workspace {
    border-top: none;
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
}
html.is-phone.m-landscape .map-toolbar { padding: 2px 6px; }
html.is-phone.m-landscape .map-tool-btn { padding: 7px; }

/* Pannello a destra, come su desktop */
html.is-phone.m-landscape .map-main { flex-direction: row; }
html.is-phone.m-landscape .map-side-panel-collapsible {
    width: 300px !important;
    max-width: 45vw;
    height: 100% !important;
    border-top: none !important;
    border-left: 1px solid var(--border) !important;
    padding-bottom: 0;
}
/* Chiuso: striscia sottile con il solo pulsante di apertura */
html.is-phone.m-landscape .map-side-panel-collapsible:not(.m-open) {
    width: 44px !important;
    height: 100% !important;
}
html.is-phone.m-landscape .map-side-panel-collapsible:not(.m-open) > .map-side-panel-header {
    display: none !important;
}
html.is-phone.m-landscape .map-side-panel-collapsible:not(.m-open) .map-side-panel-toggle {
    right: 4px;
    top: 8px;
}
html.is-phone.m-landscape .map-side-panel-collapsible.m-open .map-side-panel-toggle-icon {
    transform: rotate(0deg);
}
html.is-phone.m-landscape .map-side-panel-collapsible:not(.m-open) .map-side-panel-toggle-icon {
    transform: rotate(180deg);
}
html.is-phone.m-landscape .map-side-panel-tab { padding: 10px 4px; font-size: 13px; }

/* Menu azioni in orizzontale: larghezza contenuta e centrato */
html.is-phone.m-landscape .m-sheet-backdrop { justify-content: center; }
html.is-phone.m-landscape .m-sheet { max-width: 480px; }

/* ============================================================================
   TABLET
   Layout desktop, ma con menu a scomparsa (vedi regole condivise in alto) e
   gesti touch sulla mappa (mobile.js). Nessun vincolo di orientamento.
   ============================================================================ */
/* Altezza reale dello schermo (100vh su Safari include la barra indirizzi) */
html.is-tablet .map-workspace {
    height: calc(100vh - 120px);
    height: calc(100dvh - 120px);
}
html.is-tablet .map-toolbar-stats { display: none !important; }
/* map.css sotto i 900px allarga il pannello a quasi tutto lo schermo:
   sul tablet resta una colonna laterale */
html.is-tablet .map-side-panel-collapsible {
    width: 320px;
    max-width: 45vw;
    left: auto !important;
    right: auto !important;
}

/* Pannello dispositivi: chiuso di default in verticale (.t-open lo apre),
   aperto di default in orizzontale (.t-closed lo chiude). Stato gestito da
   mobile.js al posto della classe .collapsed del desktop. */
html.is-tablet:not(.m-landscape) .map-side-panel-collapsible:not(.t-open),
html.is-tablet.m-landscape .map-side-panel-collapsible.t-closed {
    width: 32px;
}
html.is-tablet:not(.m-landscape) .map-side-panel-collapsible:not(.t-open) > *:not(.map-side-panel-toggle),
html.is-tablet.m-landscape .map-side-panel-collapsible.t-closed > *:not(.map-side-panel-toggle) {
    display: none !important;
}
html.is-tablet:not(.m-landscape) .map-side-panel-collapsible:not(.t-open) .map-side-panel-toggle-icon,
html.is-tablet.m-landscape .map-side-panel-collapsible.t-closed .map-side-panel-toggle-icon {
    transform: rotate(180deg);
}
html.is-tablet .map-side-panel-toggle { width: 34px; height: 34px; left: -17px; }

/* Menu azioni: centrato, non a tutta larghezza */
html.is-tablet .m-sheet-backdrop { justify-content: center; }
html.is-tablet .m-sheet { max-width: 480px; }
