/**
 * Téléphone et tablette (chargé sur toutes les pages par layouts/_head_assets.php)
 *
 * - barre d'onglets en bas d'écran (.app-tabbar, rendue par _navbar.php) ;
 * - application installée (display-mode: standalone) : bouton retour, zones sûres ;
 * - règles tactiles communes : tableaux défilants, champs à 16 px (pas de zoom iOS),
 *   en-têtes de page qui passent à la ligne, fenêtres plein écran.
 *
 * Tout est sous media query (≤ 991,98 px ou pointeur tactile) : l'affichage
 * ordinateur ne change pas. Bootstrap 5.1.3.
 */

:root {
    --app-tabbar-h: 62px;
    --app-safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* ---------- Barre d'onglets (mobile) ---------- */
.app-tabbar {
    display: none;
}

@media (max-width: 991.98px) {
    .app-tabbar {
        position: fixed;
        right: 0;
        bottom: 0;
        left: 0;
        z-index: 1036; /* au-dessus du header (1035), sous les panneaux Bootstrap (1040+) */
        display: flex;
        align-items: stretch;
        justify-content: space-around;
        height: calc(var(--app-tabbar-h) + var(--app-safe-bottom));
        padding: 0 max(.25rem, env(safe-area-inset-right, 0px)) var(--app-safe-bottom) max(.25rem, env(safe-area-inset-left, 0px));
        border-top: 1px solid #eaecf0;
        background: rgba(255, 255, 255, .97);
        box-shadow: 0 -2px 10px rgba(16, 24, 40, .06);
        font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
        -webkit-backdrop-filter: saturate(180%) blur(12px);
        backdrop-filter: saturate(180%) blur(12px);
    }

    .app-tab {
        position: relative;
        display: flex;
        flex: 1 1 0;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 3px;
        min-width: 0;
        padding: 6px 2px 4px;
        border: 0;
        background: none;
        color: #667085;
        font-size: .68rem;
        font-weight: 600;
        line-height: 1.1;
        text-decoration: none;
        -webkit-tap-highlight-color: transparent;
    }

    .app-tab > i {
        font-size: 1.2rem;
        line-height: 1;
    }

    .app-tab > span {
        max-width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .app-tab:hover,
    .app-tab:focus {
        color: #344054;
    }

    .app-tab.active,
    .app-tab[aria-current="page"] {
        color: #0d6efd;
    }

    .app-tab.active::before {
        position: absolute;
        top: 0;
        left: 28%;
        right: 28%;
        height: 3px;
        border-radius: 0 0 3px 3px;
        background: #0d6efd;
        content: "";
    }

    /* Bouton central « Rechercher » mis en avant */
    .app-tab--search > i {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 46px;
        height: 46px;
        margin-top: -20px;
        border: 3px solid #fff;
        border-radius: 50%;
        background: linear-gradient(135deg, #3b82f6, #0d6efd 55%, #0a58ca);
        box-shadow: 0 4px 12px rgba(13, 110, 253, .35);
        color: #fff;
        font-size: 1.1rem;
    }

    .app-tab--search {
        color: #0d6efd;
    }

    .app-tab .app-tab-badge {
        position: absolute;
        top: 3px;
        left: calc(50% + 6px);
        min-width: 18px;
        height: 18px;
        padding: 0 5px;
        border: 2px solid #fff;
        border-radius: 9px;
        background: #dc3545;
        color: #fff;
        font-size: .62rem;
        font-weight: 700;
        line-height: 14px;
        text-align: center;
    }

    /* Place pour la barre : rien ne passe dessous */
    body.has-tabbar {
        padding-bottom: calc(var(--app-tabbar-h) + var(--app-safe-bottom) + .5rem);
    }

    /* Menu du header en double avec l'onglet « Menu » */
    body.has-tabbar .app-header [data-bs-target="#appNavOffcanvas"] {
        display: none;
    }

    /* Le pied de page ordinateur est remplacé par la barre */
    body.has-tabbar > footer.bg-light {
        display: none;
    }

    /* Boutons flottants des pages (ex. « Ajouter ») : au-dessus de la barre */
    body.has-tabbar .fixed-bottom,
    body.has-tabbar .position-fixed.bottom-0 {
        bottom: calc(var(--app-tabbar-h) + var(--app-safe-bottom)) !important;
    }

    /* Panneaux et notifications au-dessus de la barre */
    body.has-tabbar .toast-container.bottom-0 {
        bottom: calc(var(--app-tabbar-h) + var(--app-safe-bottom)) !important;
    }

    /* Barres d'action collées en bas (lettrage du grand livre, export de l'organigramme,
       sélection de documents, toute barre marquée .app-bottom-bar) : posées sur la barre d'onglets */
    body.has-tabbar .lettrage-sticky-bar,
    body.has-tabbar .org-exportbar,
    body.has-tabbar .doc-selection-bar,
    body.has-tabbar .app-bottom-bar {
        bottom: calc(var(--app-tabbar-h) + var(--app-safe-bottom)) !important;
    }

    /* Menus déroulants ouverts près du bas : au-dessus de la barre */
    body.has-tabbar .app-main .dropdown-menu.show {
        z-index: 1037;
    }
}

/* ---------- Bouton retour (application installée seulement) ---------- */
.app-back-btn {
    display: none !important;
}

@media (display-mode: standalone) and (max-width: 991.98px) {
    .app-back-btn.is-available {
        display: inline-flex !important;
    }

    /* Encoche / barre d'état iOS */
    .app-header {
        padding-top: env(safe-area-inset-top, 0px);
    }
}

/* ---------- Recherche plein écran sur téléphone ---------- */
/* (plein écran : classe Bootstrap .modal-fullscreen-sm-down sur la fenêtre) */
@media (max-width: 575.98px) {
    /* nav.css centre la fenêtre (marge 12vh, 640 px, corps 55vh) : annulé ici */
    #navPalette .modal-dialog {
        max-width: none;
        margin: 0;
    }

    #navPalette .modal-content {
        border: 0;
        border-radius: 0;
        box-shadow: none;
    }

    #navPalette .modal-body {
        max-height: none;
        padding-bottom: calc(1rem + env(safe-area-inset-bottom, 0px)) !important;
    }

    #navPalette .nav-palette-opt {
        min-height: 52px;
    }

    #navPalette .nav-palette-group {
        display: none;
    }

    #navPalette .modal-header {
        padding-top: calc(.75rem + env(safe-area-inset-top, 0px));
    }

    #navPalette .modal-footer {
        display: none;
    }

    #navPalette .nav-palette-close {
        display: inline-flex !important;
    }

    #navPalette .modal-header kbd {
        display: none;
    }
}

/* ---------- Règles communes des pages (téléphone / tablette) ---------- */
@media (max-width: 991.98px) {
    .app-main,
    body > .container,
    body > .container-fluid {
        overflow-wrap: break-word;
    }

    .app-main img,
    .app-main video,
    .app-main canvas {
        max-width: 100%;
    }

    .app-main img {
        height: auto;
    }

    /* Tableaux larges : défilement horizontal dans leur cadre (voir mobile.js) */
    .app-table-scroll,
    .table-responsive {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior-x: contain;
    }

    .app-table-scroll > .table {
        margin-bottom: 0;
    }

    /* Onglets Bootstrap : une ligne qui défile plutôt que trois lignes tassées */
    .app-main .nav-tabs,
    .app-main .nav-pills {
        flex-wrap: nowrap;
        overflow-x: auto;
        overflow-y: hidden;
        scrollbar-width: none;
        -webkit-overflow-scrolling: touch;
    }

    .app-main .nav-tabs::-webkit-scrollbar,
    .app-main .nav-pills::-webkit-scrollbar {
        display: none;
    }

    .app-main .nav-tabs .nav-link,
    .app-main .nav-pills .nav-link {
        white-space: nowrap;
    }

    /* Groupes de boutons et pagination : passent à la ligne */
    .app-main .btn-toolbar,
    .app-main .pagination {
        flex-wrap: wrap;
        gap: .25rem;
    }

    .app-main .btn-group:not(.btn-group-vertical) {
        flex-wrap: wrap;
    }

    /* Menus déroulants : jamais plus larges que l'écran */
    .dropdown-menu {
        max-width: calc(100vw - 1rem);
    }

    .dropdown-menu .dropdown-item {
        white-space: normal;
    }
}

@media (max-width: 767.98px) {
    .app-main.container,
    .app-main.container-fluid {
        margin-top: 1rem !important;
        padding-right: .85rem;
        padding-left: .85rem;
    }

    /* Titres de page proportionnés */
    .app-main h1,
    .app-main .h1 {
        font-size: 1.5rem;
    }

    .app-main h2,
    .app-main .h2 {
        font-size: 1.3rem;
    }

    .app-main h3,
    .app-main .h3 {
        font-size: 1.15rem;
    }

    .app-main .display-4,
    .app-main .display-5,
    .app-main .display-6 {
        font-size: 1.6rem;
    }

    /* En-têtes « titre + boutons » : les boutons passent sous le titre */
    .app-main .d-flex.justify-content-between:not(.list-group-item):not(li):not(td):not(th):not(.form-check):not(.app-nowrap) {
        flex-wrap: wrap;
        row-gap: .5rem;
    }

    /* Tableaux plus compacts */
    .app-main .table {
        font-size: .875rem;
    }

    .app-main .table:not(.table-sm) > :not(caption) > * > * {
        padding: .45rem .5rem;
    }

    /* Cartes : marges internes réduites */
    .app-main .card-body {
        padding: .9rem;
    }

    .app-main .card-header,
    .app-main .card-footer {
        padding: .6rem .9rem;
    }

    /* Fenêtres : presque plein écran */
    .modal-lg:not([class*="modal-fullscreen"]),
    .modal-xl:not([class*="modal-fullscreen"]) {
        max-width: calc(100vw - 1rem);
    }

    /* Messages flash lisibles */
    #flash-messages-container {
        padding-right: .85rem;
        padding-left: .85rem;
    }
}

@media (max-width: 575.98px) {
    /* Grandes fenêtres : plein écran sur téléphone */
    .modal-lg .modal-content,
    .modal-xl .modal-content {
        min-height: calc(100vh - 1rem);
    }

    .modal-footer {
        flex-wrap: wrap;
    }

    .modal-footer > .btn {
        flex: 1 1 auto;
    }
}

/* ---------- Composants réutilisables (voir mobile.js) ----------
 *
 * 1) Bloc repliable sur téléphone : <div data-mobile-collapse="Filtres"> … </div>
 *    → replié derrière un bouton « Filtres » (ouvert d'office si un champ est rempli).
 * 2) Tableau en cartes sur téléphone : <table class="table app-stack">
 *    → chaque ligne devient une carte ; libellés repris de <thead> (data-label) ;
 *    <td class="app-stack-title"> = titre de la carte, <th/td class="app-stack-hide"> = masqué,
 *    <td class="app-stack-actions"> = boutons en bas de carte.
 */
.app-collapse-toggle {
    display: none;
}

@media (max-width: 767.98px) {
    .app-collapse-toggle {
        display: flex;
        align-items: center;
        gap: .5rem;
        width: 100%;
        min-height: 44px;
        margin-bottom: .75rem;
        padding: .5rem .9rem;
        border: 1px solid #dee2e6;
        border-radius: .6rem;
        background: #fff;
        color: #344054;
        font-weight: 600;
        text-align: left;
    }

    .app-collapse-toggle .app-collapse-count {
        padding: .05rem .5rem;
        border-radius: 1rem;
        background: #e7f1ff;
        color: #0b5ed7;
        font-size: .78rem;
    }

    .app-collapse-toggle .fa-chevron-down {
        margin-left: auto;
        transition: transform .2s;
    }

    .app-collapse-toggle[aria-expanded="true"] .fa-chevron-down {
        transform: rotate(180deg);
    }

    [data-mobile-collapse].is-collapsed {
        display: none !important;
    }

    /* Tableau → cartes */
    table.app-stack,
    table.app-stack > tbody,
    table.app-stack > tbody > tr,
    table.app-stack > tbody > tr > td {
        display: block;
        width: 100%;
    }

    table.app-stack {
        border: 0;
        background: transparent;
    }

    table.app-stack > thead {
        display: none;
    }

    table.app-stack > tbody > tr {
        position: relative;
        margin-bottom: .75rem;
        padding: .65rem .85rem;
        border: 1px solid #eaecf0;
        border-radius: .75rem;
        background: #fff;
        box-shadow: 0 1px 2px rgba(16, 24, 40, .05);
    }

    table.app-stack > tbody > tr > td {
        display: flex;
        gap: .75rem;
        justify-content: space-between;
        align-items: baseline;
        padding: .2rem 0 !important;
        border: 0 !important;
        background: transparent !important;
        text-align: right;
        box-shadow: none !important;
    }

    table.app-stack > tbody > tr > td[data-label]::before {
        flex: 0 0 auto;
        max-width: 45%;
        color: #667085;
        font-size: .8rem;
        font-weight: 500;
        text-align: left;
        content: attr(data-label);
    }

    table.app-stack > tbody > tr > td:empty,
    table.app-stack > tbody > tr > td.app-stack-hide {
        display: none;
    }

    table.app-stack > tbody > tr > td.app-stack-title {
        display: block;
        padding-bottom: .35rem !important;
        font-size: 1rem;
        font-weight: 600;
        text-align: left;
    }

    table.app-stack > tbody > tr > td.app-stack-title::before {
        display: none;
    }

    table.app-stack > tbody > tr > td.app-stack-actions {
        justify-content: flex-end;
        flex-wrap: wrap;
        margin-top: .35rem;
        padding-top: .5rem !important;
        border-top: 1px solid #f2f4f7 !important;
    }

    table.app-stack > tbody > tr > td.app-stack-actions::before {
        display: none;
    }

    /* Ligne « aucun résultat » (colspan) : texte centré */
    table.app-stack > tbody > tr > td[colspan] {
        display: block;
        text-align: center;
    }

    table.app-stack > tbody > tr > td[colspan]::before {
        display: none;
    }
}

/* ---------- Écrans tactiles ---------- */
@media (pointer: coarse) {
    /* iOS zoome sur un champ < 16 px : on garde 16 px */
    input.form-control:not(.form-control-lg),
    select.form-select:not(.form-select-lg),
    textarea.form-control:not(.form-control-lg),
    .form-control-sm,
    .form-select-sm,
    input[type="search"]:not(.form-control-lg),
    input[type="text"]:not(.form-control-lg),
    input[type="email"]:not(.form-control-lg),
    input[type="number"]:not(.form-control-lg),
    input[type="date"]:not(.form-control-lg),
    input[type="password"]:not(.form-control-lg),
    input[type="tel"]:not(.form-control-lg),
    select,
    textarea {
        font-size: 16px;
    }

    /* Cibles tactiles d'au moins 40 px */
    .app-main .btn {
        min-height: 40px;
    }

    .app-main .btn-sm {
        min-height: 36px;
    }

    .app-main .btn.btn-link {
        min-height: 0;
    }

    .app-main .form-check:not(.form-switch) > .form-check-input {
        width: 1.25em;
        height: 1.25em;
    }

    .app-main .page-link {
        min-width: 40px;
        min-height: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
}

/* Pas d'effets de survol « collants » au doigt */
@media (hover: none) {
    .btn:hover,
    .document-item:hover {
        transform: none;
    }

    .card:hover {
        box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);
    }
}

@media print {
    .app-tabbar,
    .app-back-btn {
        display: none !important;
    }

    body.has-tabbar {
        padding-bottom: 0;
    }
}
