/* =========================================================================
   MOBILE — couche responsive « appli » (chargée en dernier, surcharge).
   Cibles : tablette (≤900px) et téléphone (≤640px), fiable jusqu'à ~320px.
   Tout en unités fluides : aucun débordement horizontal de page.
   ========================================================================= */

/* Évite tout débordement latéral global. */
html, body { max-width: 100%; overflow-x: hidden; }
*, *::before, *::after { box-sizing: border-box; }

/* Hamburger : masqué sur desktop, montré en mobile. */
.nav-toggle { display: none; }
.mobile-nav { display: none; }

/* Bouton « Retour » unifié (transformé par mobile.js) : carré à picto, sur toutes
   largeurs en web ; masqué en mobile (le hamburger assure le retour). */
.btn-back {
    display: inline-flex; align-items: center; justify-content: center;
    box-sizing: border-box; flex: 0 0 auto; vertical-align: middle;
    width: var(--control-h); min-width: var(--control-h);
    height: var(--control-h); min-height: var(--control-h);
    padding: 0;
    border: 1px solid var(--c-neutral); border-radius: var(--btn-radius);
    background: var(--c-neutral); color: #fff;
    font-size: 18px; line-height: 1.1; text-decoration: none;
}
.btn-back:hover { background: var(--c-neutral-hover); border-color: var(--c-neutral-hover); }
@media (max-width: 640px) { .btn-back { display: none !important; } }

/* ============================ TABLETTE & MOBILE (≤900px) ================== */
@media (max-width: 900px) {

    /* En-tête compact : hamburger + logo sur une ligne ; bloc user/logout
       déplacé dans le menu hamburger. On neutralise l'empilement hérité. */
    .header-row { flex-direction: row !important; align-items: center; gap: 12px; }
    .header-left { flex-direction: row !important; align-items: center; gap: 12px; min-width: 0; }
    .header-right { display: none !important; }
    .logo { height: 38px; }

    .nav-toggle {
        display: inline-flex; flex-direction: column; justify-content: center; gap: 4px;
        width: 42px; height: 42px; padding: 9px; flex: 0 0 auto;
        background: rgba(255,255,255,.12); border: 0; border-radius: var(--radius-lg); cursor: pointer;
    }
    .nav-toggle span { display: block; height: 2px; background: #fff; border-radius: var(--radius-xs); }

    /* Panneau de navigation (ouvert = attribut hidden retiré par le JS). */
    .mobile-nav {
        display: flex; flex-direction: column;
        background: var(--c-primary-hover); margin-top: 10px;
        border-radius: var(--radius-lg); padding: 6px; gap: 2px;
    }
    .mobile-nav-link {
        display: block; width: 100%; text-align: left;
        color: #fff; text-decoration: none; background: transparent; border: 0;
        padding: 13px 14px; border-radius: var(--radius-md); font-size: 15px; cursor: pointer;
    }
    .mobile-nav-link:hover, .mobile-nav-link:active { background: rgba(255,255,255,.14); }
    .mobile-nav-logout { margin: 0; }
    .mobile-nav-logout-btn { color: #ffd5d5; }

    /* Conteneur pleine largeur (fin du gaspillage du mode --wide). */
    .page-container, .page-container--wide { max-width: 100%; margin: 14px auto; padding: 0 12px; }
    .app-footer { max-width: 100%; }
    .card { padding: 16px 14px; }

    /* Grilles : toutes en une colonne. */
    .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; gap: 14px; }

    /* En-tête de section : titre au-dessus, actions en dessous. */
    .section-header, .page-title-row { flex-direction: column; align-items: stretch; gap: 10px; }
    .section-header .action_naviguation_right,
    .page-title-row .action_naviguation_right { justify-content: flex-start; flex-wrap: wrap; }

    h1 { font-size: 1.5rem; }
}

/* ============================ TÉLÉPHONE (≤640px) ========================= */
@media (max-width: 640px) {

    /* Cibles tactiles confortables. */
    .btn, .btn--primary, .btn--secondary, .btn--ghost, .btn--danger {
        min-height: 44px; display: inline-flex; align-items: center; justify-content: center;
        font-size: 14px; padding: 10px 16px;
    }
    .btn-action-column { min-width: 40px; width: auto; height: 40px; font-size: 17px; padding: 0 10px; }
    .col-actions, .activity-row-actions { display: flex; flex-wrap: wrap; gap: 8px; }

    /* Formulaires : champs pleine largeur, lisibles, sans débordement.
       (On EXCLUT les cases à cocher / boutons radio, qui ne doivent pas être étirés.) */
    input:not([type="checkbox"]):not([type="radio"]),
    select, textarea { width: 100%; max-width: 100%; min-width: 0; font-size: 16px; /* anti-zoom iOS */ }
    .field input:not([type="checkbox"]):not([type="radio"]),
    .field select, .field textarea { min-height: 44px; }
    .field textarea { min-height: 88px; }

    /* Cases à cocher / radios : taille fixe confortable, alignées avec leur libellé. */
    input[type="checkbox"], input[type="radio"] {
        width: 20px; height: 20px; min-height: 0; flex: 0 0 auto; margin: 0 8px 0 0; vertical-align: middle;
    }
    .checkbox-row, .field-check label, .communication-check, .checkbox-grid label,
    .health-attest-certify {
        display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
    }
    .datetime-split { flex-wrap: wrap; }
    .datetime-split input[type="date"] { flex: 1 1 100%; }
    .datetime-split input[type="time"] { flex: 1 1 100%; width: 100%; }
    .checkbox-grid { grid-template-columns: 1fr; }

    /* Barres d'action / toolbars : empilées. */
    .campaign-mass-toolbar, .activity-mass-actions { flex-direction: column; align-items: stretch; }
    .campaign-mass-toolbar .btn, .activity-mass-actions .btn { width: 100%; }

    /* Onglets (campagne / activité) : pleine largeur, repli. */
    .document-tab-buttons { flex-wrap: wrap; }
    .document-tab-button { flex: 1 1 auto; }

    /* ---- TABLEAUX DE LISTE → CARTES EMPILÉES (label : valeur) ----
       Les libellés sont injectés en data-label par mobile.js (depuis l'en-tête). */
    .simple-list-table, .admin-family-table, .document-campaign-table,
    .audit-table, .admin-user-table { display: block; width: 100%; }
    .simple-list-table thead, .admin-family-table thead, .document-campaign-table thead,
    .audit-table thead, .admin-user-table thead,
    .simple-list-table colgroup, .admin-family-table colgroup { display: none; }
    .simple-list-table tbody, .admin-family-table tbody, .document-campaign-table tbody,
    .audit-table tbody, .admin-user-table tbody { display: block; }

    .simple-list-table tr, .admin-family-table tr, .document-campaign-table tr,
    .audit-table tr, .admin-user-table tr {
        display: block; background: #fff;
        border: 1px solid #e2e8f0; border-radius: var(--radius-lg);
        padding: 8px 12px; margin: 0 0 12px;
        box-shadow: 0 1px 4px rgba(0,0,0,.05);
    }
    /* On masque la 1re ligne d'en-tête posée en <tr><th> (hors thead). */
    .simple-list-table tr:has(> th):not(:has(> td)),
    .admin-family-table tr:has(> th):not(:has(> td)) { display: none; }

    /* Cellule = bloc : libellé AU-DESSUS, valeur pleine largeur dessous (robuste
       même sur écrans étroits — pas de colonne valeur écrasée). */
    .simple-list-table td, .admin-family-table td, .document-campaign-table td,
    .audit-table td, .admin-user-table td {
        display: block !important; padding: 8px 0; border: none;
        /* Neutralise les largeurs/alignements par colonne du mode bureau (sinon la
           cellule reste étroite → valeur tronquée, ou centrée). */
        width: auto !important; max-width: 100% !important; text-align: left !important;
        white-space: normal; overflow-wrap: break-word; word-break: normal;
    }
    .simple-list-table td + td, .admin-family-table td + td,
    .document-campaign-table td + td, .audit-table td + td, .admin-user-table td + td {
        border-top: 1px solid var(--c-ghost-hover-bg);
    }
    .simple-list-table td::before, .admin-family-table td::before,
    .document-campaign-table td::before, .audit-table td::before, .admin-user-table td::before {
        display: block; content: attr(data-label);
        font-weight: 700; color: var(--c-primary); font-size: .76rem; text-transform: uppercase;
        letter-spacing: .02em; margin-bottom: 3px;
    }
    /* Nom principal = titre de la carte (sans libellé). */
    td.col-main { font-weight: 700; font-size: 1.05rem; }
    td.col-main::before { display: none; content: ''; }
    /* Cellule d'actions : rangée de boutons, sans libellé. */
    td.col-actions { display: flex !important; flex-wrap: wrap; gap: 8px; padding-top: 10px; }
    td.col-actions::before { display: none; content: ''; }
    td.col-actions form, td.col-actions .inline-action-form { display: inline-flex; margin: 0; }

    /* Liste des FRAIS : regrouper 2 infos par ligne (limite l'effet château de cartes).
       Libellé pleine largeur, puis Date|Montant, Catégorie|Payeur, Justificatifs|Actions. */
    .expense-list-table tbody tr { display: grid; grid-template-columns: 1fr 1fr; column-gap: 14px; }
    .expense-list-table tbody td { border-top: none !important; padding: 6px 0; }
    .expense-list-table tbody td:nth-child(2) { grid-column: 1 / -1; order: 0; }  /* Libellé */
    .expense-list-table tbody td:nth-child(1) { order: 1; }  /* Date */
    .expense-list-table tbody td:nth-child(5) { order: 2; }  /* Montant */
    .expense-list-table tbody td:nth-child(4) { order: 3; }  /* Catégorie */
    .expense-list-table tbody td:nth-child(3) { order: 4; }  /* Payeur */
    .expense-list-table tbody td.expense-receipts-cell { order: 5; }  /* Justificatifs */
    .expense-list-table tbody td.col-actions { order: 6; }  /* Actions */
    .expense-list-table .expense-edit-row td { grid-column: 1 / -1; }

    /* Lignes document/paiement (divs, pas des tables) : empilées. */
    .document-line { flex-direction: column; align-items: stretch; gap: 8px; }
    .document-line-actions { justify-content: flex-end; flex-wrap: wrap; }

    /* Tables clé/valeur : pleine largeur, libellé au-dessus si étroit. */
    .profile-table, .Th70Td30 { width: 100%; }
    .profile-table th, .Th70Td30 th { width: 45%; }

    /* Matrices larges (outils admin) : défilement horizontal tactile soigné. */
    .import-scroll, .communication-tree, .table-wrap.keep-scroll {
        overflow-x: auto; -webkit-overflow-scrolling: touch;
    }

    /* Dialogues : presque plein écran. */
    .upload-dialog { width: 96%; max-width: 96%; padding: 16px; }
}

/* ============================ FINITIONS (≤640px) ========================= */
@media (max-width: 640px) {

    /* Fil d'Ariane masqué : le menu hamburger assure la navigation. */
    .breadcrumb { display: none; }

    /* Anti-troncature : on neutralise les ellipses/nowrap hérités du mode bureau
       (table-layout fixe, colonnes étroites) — sauf matrices en défilement. */
    .simple-list-table td, .admin-family-table td, .document-campaign-table td,
    .audit-table td, .admin-user-table td,
    .simple-list-table th, .admin-family-table th {
        overflow: visible !important;
        text-overflow: clip !important;
        white-space: normal !important;
        word-break: normal;
        overflow-wrap: break-word;
    }
    .badge, .scope-badge, .activity-status, .pending-status, .payment-amount,
    .dash-bar-label, .dashboard-list-meta, .user-name, .user-email {
        white-space: normal !important;
        overflow: visible !important;
        text-overflow: clip !important;
        overflow-wrap: break-word;
    }

    /* Boutons : texte centré, jamais coupé ; actions d'en-tête en pleine largeur
       (rendu « appli », évite les boutons décentrés). */
    .btn, .btn--primary, .btn--secondary, .btn--ghost, .btn--danger, .section-header .btn {
        white-space: normal;
        text-align: center;
    }
    .section-header .action_naviguation_right > .btn,
    .section-header .action_naviguation_right > .btn--primary,
    .section-header .action_naviguation_right > .btn--secondary,
    .page-title-row .action_naviguation_right > .btn,
    .page-title-row .action_naviguation_right > .btn--primary {
        width: 100%;
    }

    /* Badges/pastilles : un peu d'air vertical quand ils reviennent à la ligne. */
    .badge, .scope-badge, .activity-status { display: inline-block; margin: 2px 0; }
}

/* ============================ TRÈS PETITS ÉCRANS (≤360px) ================ */
@media (max-width: 360px) {
    .page-container, .page-container--wide { padding: 0 8px; }
    .card { padding: 12px 10px; }
    .simple-list-table td::before, .admin-family-table td::before,
    .document-campaign-table td::before, .audit-table td::before,
    .admin-user-table td::before { flex-basis: 50%; }
    h1 { font-size: 1.3rem; }
}

/* ---- Correctifs passe mobile (≤640px) ---- */
@media (max-width: 640px) {
    /* Barre de filtres liste familles : grille 2 colonnes (560px min) → empilée pleine largeur. */
    .admin-family-toolbar { grid-template-columns: 1fr !important; }

    /* Bloc photo enfant : champ fichier + bouton à largeur fixe → empilés pleine largeur. */
    .child-photo-form { flex-direction: column; align-items: stretch; }
    .child-photo-form input { width: 100% !important; }
    .child-photo-btn { width: 100%; }
}
