/* ============================================================
   MOBILE.CSS — Responsive universel pour ServerMas / IM2S
   Couvre : layout, nav, tableaux, formulaires, modals, cartes
   ============================================================ */

/* ── 1. META VIEWPORT (déjà dans _Layout mais rappel CSS) ── */
*, *::before, *::after { box-sizing: border-box; }

/* ── 2. LAYOUT PRINCIPAL ───────────────────────────────────── */
.layout {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0; /* évite le débordement flex */
    overflow: hidden;
}

.content {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
}

/* ── 3. SIDEBAR MOBILE ─────────────────────────────────────── */
@media (max-width: 768px) {

    .layout {
        height: 100dvh; /* dynamic viewport height pour iOS */
    }

    /* Sidebar glisse depuis la gauche */
    nav.sidebar-expanded {
        position: fixed !important;
        top: 0;
        left: -260px;
        width: 260px !important;
        height: 100dvh;
        z-index: 200;
        overflow-y: auto;
        transition: left 0.28s ease;
        padding: 16px 12px !important;
    }

    nav.sidebar-expanded.show {
        left: 0 !important;
        box-shadow: 4px 0 20px rgba(0,0,0,0.35);
    }

    .menu-overlay {
        display: none;
        position: fixed;
        inset: 0;
        background: rgba(0,0,0,0.5);
        z-index: 190;
        opacity: 0;
        transition: opacity 0.28s ease;
    }
    .menu-overlay.active {
        display: block;
        opacity: 1;
    }

    /* Top bar */
    .top-row {
        padding: 8px 12px !important;
        position: sticky;
        top: 0;
        z-index: 100;
    }

    .top-row .font-weight-bold { display: none; } /* cache le titre sur petit écran */

    /* Searchbox réduite */
    .search-box {
        width: 140px !important;
        font-size: 13px;
    }
    .search-box:focus { width: 170px !important; }

    /* Contenu */
    .content {
        margin: 6px !important;
        padding: 12px !important;
        border-radius: 6px !important;
    }

    /* Logo sidebar plus compact */
    .logo { font-size: 18px !important; margin-bottom: 18px !important; }
}

/* ── 4. TABLEAUX RESPONSIVES ───────────────────────────────── */
@media (max-width: 992px) {

    /* Wrapping horizontal sur toutes les tables */
    .table-responsive,
    .table-container-dark,
    [class*="table-responsive"] {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        width: 100%;
    }

    /* Tables avec scroll horizontal natif */
    table {
        min-width: 500px; /* force scroll plutôt que compression */
    }

    /* Exception : petites tables de stats qui doivent tenir */
    .table-stats-small table { min-width: unset; }
}

@media (max-width: 576px) {

    /* Réduction de la typographie dans les cellules */
    table th, table td {
        font-size: 0.72rem !important;
        padding: 6px 8px !important;
        white-space: nowrap;
    }

    /* Boutons d'action dans les tables : icône seule */
    .action-btns .btn-label { display: none; }
}

/* ── 5. GRILLES ET CARTES STATS ────────────────────────────── */
@media (max-width: 1200px) {
    .stats-grid         { grid-template-columns: repeat(4, 1fr) !important; }
    .nav-cards-grid.six-cols { grid-template-columns: repeat(3, 1fr) !important; }
}

@media (max-width: 768px) {
    /* Stats grids */
    .stats-grid,
    .egm-stats-grid,
    .stats-row          { grid-template-columns: repeat(2, 1fr) !important; }

    .nav-cards-grid,
    .nav-cards-grid.six-cols,
    .roles-grid,
    .filters-grid       { grid-template-columns: 1fr !important; }

    .tables-grid,
    .form-grid          { grid-template-columns: 1fr !important; }

    /* Cards stats padding réduit */
    .stat-card, .stat-box {
        padding: 12px !important;
        gap: 10px !important;
    }
    .stat-value, .egm-value { font-size: 1.3rem !important; }
    .stat-label { font-size: 0.75rem !important; }
}

@media (max-width: 480px) {
    .stats-grid,
    .egm-stats-grid     { grid-template-columns: 1fr !important; }
}

/* ── 6. FORMULAIRES ────────────────────────────────────────── */
@media (max-width: 768px) {

    /* Inputs pleine largeur */
    .form-control, .form-select,
    .form-control-dark, .form-select-dark,
    .pbj-input, .pbj-select,
    input[type="text"], input[type="number"],
    input[type="date"], input[type="time"],
    select, textarea {
        width: 100% !important;
        font-size: 16px !important; /* évite le zoom auto iOS */
    }

    /* Grid de formulaires → 1 colonne */
    .pbj-form-grid,
    .form-grid,
    .filters-grid,
    .row.g-3 { grid-template-columns: 1fr !important; }

    /* Boutons pleine largeur sur mobile */
    .pbj-actions {
        flex-direction: column !important;
        gap: 8px;
    }
    .pbj-actions .pbj-btn-gold,
    .pbj-actions .pbj-btn-outline {
        width: 100% !important;
        justify-content: center;
    }

    /* Action bars */
    .action-bar {
        flex-wrap: wrap !important;
        gap: 8px !important;
    }
    .action-bar .btn-gold,
    .action-bar .btn-dark-outline {
        flex: 1 1 calc(50% - 8px);
        min-width: 120px;
        justify-content: center;
        text-align: center;
    }
}

/* ── 7. MODALES ────────────────────────────────────────────── */
@media (max-width: 768px) {

    /* Modales pleine largeur sur mobile */
    .modal-dark,
    .modal-dark-pbj,
    .modal-dialog {
        width: 96vw !important;
        max-width: 96vw !important;
        max-height: 92dvh !important;
        margin: 4dvh auto 0 !important;
        overflow-y: auto;
    }

    .modal-overlay,
    .modal-overlay-pbj {
        align-items: flex-start !important;
        padding-top: 4dvh;
    }

    /* Header modal plus compact */
    .modal-header-dark,
    .modal-dark-pbj .modal-header-d {
        padding: 14px 16px !important;
    }

    .modal-body-dark,
    .modal-dark-pbj .modal-body-d {
        padding: 14px 16px !important;
    }

    /* Grid formulaire dans modal → 1 colonne */
    .modal-dark .form-grid,
    .modal-dark-pbj .form-grid {
        grid-template-columns: 1fr !important;
    }
}

/* ── 8. DASHBOARD MAS ──────────────────────────────────────── */
@media (max-width: 768px) {

    .dashboard-header {
        flex-direction: column !important;
        gap: 12px !important;
        padding: 16px !important;
        text-align: center;
    }

    .header-left { flex-direction: column; align-items: center; }
    .header-right { flex-wrap: wrap; justify-content: center; gap: 8px !important; }

    .header-text h1 { font-size: 18px !important; }
    .header-text p  { font-size: 12px !important; }

    .section-card { padding: 14px !important; }

    .section-header {
        flex-direction: column !important;
        gap: 8px !important;
        align-items: flex-start !important;
    }

    .stats-section-body {
        grid-template-columns: 1fr !important;
    }
    .stats-chart-area {
        padding-right: 0 !important;
        border-right: none !important;
        border-bottom: 1px solid #e2e8f0;
        padding-bottom: 16px;
    }
    .stats-detail-area {
        padding-left: 0 !important;
        padding-top: 16px;
    }
}

/* ── 9. PARC MACHINES ──────────────────────────────────────── */
@media (max-width: 768px) {

    .parc-container { font-size: 0.8rem; }

    .stat-cards {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 8px !important;
    }

    .table-container-dark {
        max-height: calc(100dvh - 320px) !important;
    }

    .pagination-bar {
        flex-direction: column !important;
        gap: 10px !important;
        align-items: center;
    }
}

/* ── 10. EGM / REMONTÉE AUTO ───────────────────────────────── */
@media (max-width: 992px) {
    .table-compteurs th,
    .table-compteurs td {
        font-size: 0.68rem !important;
        padding: 4px 5px !important;
    }
}

@media (max-width: 768px) {
    /* Masquer les colonnes N-1 moins essentielles sur mobile */
    .col-n1 { display: none; }

    .countdown { font-size: 1rem !important; }

    .btn-group {
        flex-direction: column !important;
        width: 100%;
    }
    .btn-group .btn { width: 100% !important; border-radius: 8px !important; margin: 2px 0; }
}

/* ── 11. SAISIE PBJ ────────────────────────────────────────── */
@media (max-width: 768px) {

    .pbj-container { max-width: 100% !important; }

    .cpt-table th, .cpt-table td {
        font-size: 0.72rem !important;
        padding: 5px 6px !important;
    }

    .result-grid { grid-template-columns: 1fr 1fr !important; }

    .result-card { padding: 8px !important; }
    .result-card label { font-size: 0.65rem !important; }
    .result-card .result-value { font-size: 0.85rem !important; }
}

/* ── 12. ADMIN / ROLES / PERMISSIONS ──────────────────────── */
@media (max-width: 768px) {

    .roles-page { padding: 12px !important; }

    .page-header-roles {
        flex-direction: column !important;
        gap: 10px;
    }

    .header-right {
        flex-wrap: wrap;
        gap: 6px;
    }

    .stats-row { flex-direction: column !important; }

    .role-card { padding: 14px !important; }

    .role-stats { flex-wrap: wrap; gap: 10px !important; }

    .role-actions {
        flex-wrap: wrap;
        gap: 6px;
    }
    .role-actions .btn {
        flex: 1 1 calc(50% - 6px);
        min-width: 80px;
        text-align: center;
        justify-content: center;
    }
}

/* ── 13. FILTRES HORIZONTAUX → VERTICAUX ──────────────────── */
@media (max-width: 768px) {

    .filters-panel { padding: 12px !important; }

    /* Bootstrap rows → stack */
    .row.g-3 > [class*="col-"] {
        flex: 0 0 100% !important;
        max-width: 100% !important;
    }

    /* Boutons de filtre */
    .d-flex.gap-1 { flex-wrap: wrap; }
    .d-flex.gap-1 .btn { flex: 1 1 calc(50% - 4px); }
}

/* ── 14. TYPOGRAPHIE GLOBALE ───────────────────────────────── */
@media (max-width: 576px) {

    h1 { font-size: 1.35rem !important; }
    h2 { font-size: 1.2rem !important; }
    h3 { font-size: 1.05rem !important; }
    h4 { font-size: 0.95rem !important; }
    h5 { font-size: 0.88rem !important; }

    .card-header h5 { font-size: 0.85rem !important; }
}

/* ── 15. BOUTONS GÉNÉRAUX ──────────────────────────────────── */
@media (max-width: 480px) {

    /* Boutons avec texte + icône : réduire le texte */
    .btn span.btn-text { display: none; }

    /* Assurer que les boutons icône ont une taille minimale tactile */
    .btn-icon, .btn-sm {
        min-width: 36px;
        min-height: 36px;
    }
}

/* ── 16. TOUCH TARGET MINIMUM (44px × 44px WCAG) ──────────── */
@media (max-width: 768px) {

    button, a.btn, .btn, .nav-card,
    .menu-link, nav ul li a {
        min-height: 44px;
        display: flex;
        align-items: center;
    }

    .menu-toggle-btn {
        min-width: 44px;
        min-height: 44px;
    }
}

/* ── 17. SCROLLBARS DISCRETES SUR MOBILE ──────────────────── */
@media (max-width: 768px) {
    ::-webkit-scrollbar { width: 4px; height: 4px; }
    ::-webkit-scrollbar-thumb { background: rgba(128,128,128,0.4); border-radius: 4px; }
    ::-webkit-scrollbar-track { background: transparent; }
}

/* ── 18. OVERFLOW PREVENTION ───────────────────────────────── */
body {
    overflow-x: hidden;
    max-width: 100vw;
}

img, video, iframe, embed, object {
    max-width: 100%;
    height: auto;
}

/* Empêche tout élément de dépasser le viewport */
.container-fluid, .card, .card-body,
.section-card, .stats-section-card,
.table-panel, .filters-panel {
    max-width: 100%;
    overflow-x: hidden;
}

/* ── 19. PAGINATION MOBILE ─────────────────────────────────── */
@media (max-width: 576px) {
    .pagination-controls { gap: 4px !important; }
    .btn-page { padding: 6px 10px !important; font-size: 0.75rem !important; }
    .page-indicator { font-size: 0.75rem !important; }
}

/* ── 20. USER DROPDOWN MOBILE ──────────────────────────────── */
@media (max-width: 480px) {
    .user-dropdown-content {
        right: -10px !important;
        width: 220px !important;
    }
}
