/* ═══════════════════════════════════════════════════════════════════════
   ZUKO — mode « application » sur téléphone (≤ 768 px) et installée.
   Chargée après les styles des pages. Rien ne s'applique au-delà de
   768 px : le bureau garde son rendu (barre du haut + menu latéral).
   Contenu : en-tête compact, barre d'onglets, feuille « Nouveau »,
   menu « Plus », zones sûres iOS/Android, champs sans zoom, cibles
   tactiles de 44 px, modales en feuilles du bas, invitation à installer.
   ═══════════════════════════════════════════════════════════════════════ */
:root {
    --m-top: 56px;
    --m-tab: 62px;
    --m-sat: env(safe-area-inset-top, 0px);
    --m-sab: env(safe-area-inset-bottom, 0px);
    --m-sal: env(safe-area-inset-left, 0px);
    --m-sar: env(safe-area-inset-right, 0px);
    --m-ease: cubic-bezier(.2, .8, .2, 1);
}

html { -webkit-tap-highlight-color: transparent; -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

/* Éléments propres au téléphone : invisibles sur ordinateur. */
.m-tabbar, .m-sheet, .m-sheet-backdrop, .m-install, .m-drawer-head, .m-drawer-extra, .zs-cancel, .m-progress { display: none; }

@media (max-width: 768px) {
    html { scroll-padding-top: calc(var(--m-top) + var(--m-sat) + 12px); scroll-padding-bottom: calc(var(--m-tab) + var(--m-sab) + 12px); }
    body.m-lock { overflow: hidden; }

    /* ── En-tête compact ─────────────────────────────────────────────── */
    .mobile-topbar {
        height: calc(var(--m-top) + var(--m-sat));
        padding: var(--m-sat) calc(6px + var(--m-sar)) 0 calc(6px + var(--m-sal));
        gap: 2px;
        background: #003366;
        border-bottom: 0;
        box-shadow: inset 0 -2px 0 var(--nj-accent, #FFD700);
        -webkit-user-select: none; user-select: none;
    }
    body.dark-mode .mobile-topbar { background: #141824; box-shadow: inset 0 -1px 0 #262b3a; }
    .m-top-btn {
        width: 44px; height: 44px; flex-shrink: 0; border: 0; border-radius: 50%;
        background: transparent; color: #fff; display: grid; place-items: center;
        font-size: 1.05rem; text-decoration: none;
    }
    .m-top-btn:active { background: rgba(255, 255, 255, .14); }
    .m-back { font-size: 1.2rem; }
    .m-brand-mark {
        width: 34px; height: 34px; margin: 0 5px 0 6px; flex-shrink: 0; border-radius: 9px;
        background: var(--nj-accent, #FFD700); color: #003366; font-weight: 900; font-size: 1rem;
        display: grid; place-items: center; text-decoration: none; overflow: hidden;
    }
    .m-brand-mark img { width: 100%; height: 100%; object-fit: contain; background: #fff; padding: 2px; }
    .m-title {
        flex: 1; min-width: 0; margin: 0 4px; color: #fff; font-weight: 800; font-size: 1.06rem; letter-spacing: -.01em;
        white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    }
    .mobile-topbar .mob-avatar { width: 34px; height: 34px; margin: 0 5px; flex-shrink: 0; overflow: hidden; }

    /* ── Contenu : place pour l'en-tête et la barre d'onglets ───────── */
    .main-content {
        padding: calc(var(--m-top) + var(--m-sat) + 16px) calc(14px + var(--m-sar)) 24px calc(14px + var(--m-sal));
    }
    body.has-tabbar .main-content { padding-bottom: calc(var(--m-tab) + var(--m-sab) + 28px); }
    #nj-toast-container { top: calc(var(--m-top) + var(--m-sat) + 8px); left: calc(12px + var(--m-sal)); right: calc(12px + var(--m-sar)); width: auto; }
    body.has-tabbar .ai-assistant { bottom: calc(var(--m-tab) + var(--m-sab) + 14px); right: calc(14px + var(--m-sar)); width: 48px; height: 48px; }
    body.has-tabbar .ai-chat { bottom: calc(var(--m-tab) + var(--m-sab) + 70px); max-height: calc(100dvh - var(--m-top) - var(--m-sat) - var(--m-tab) - var(--m-sab) - 90px); }

    /* ── Barre d'onglets ─────────────────────────────────────────────── */
    .m-tabbar {
        display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 1040;
        height: calc(var(--m-tab) + var(--m-sab));
        padding: 0 var(--m-sar) var(--m-sab) var(--m-sal);
        background: rgba(255, 255, 255, .94);
        -webkit-backdrop-filter: saturate(180%) blur(18px); backdrop-filter: saturate(180%) blur(18px);
        border-top: 1px solid var(--ph-border, #e3e6ed);
        -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
    }
    .m-tab {
        flex: 1 1 0; min-width: 0; min-height: 44px; position: relative;
        display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
        border: 0; background: none; padding: 6px 2px 4px; text-decoration: none;
        color: var(--ph-muted, #6e7891); font-size: .66rem; font-weight: 700; letter-spacing: .01em; line-height: 1;
        -webkit-touch-callout: none;
    }
    .m-tab i { font-size: 1.2rem; transition: transform .15s var(--m-ease); }
    .m-tab span { max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .m-tab:active i { transform: scale(.86); }
    .m-tab.active { color: var(--ph-primary, #003366); }
    .m-tab.active::before {
        content: ""; position: absolute; top: 0; left: 50%; width: 26px; height: 3px; margin-left: -13px;
        border-radius: 0 0 3px 3px; background: var(--nj-accent, #FFD700);
    }
    .m-tab:focus-visible { outline-offset: -4px; }
    .m-fab {
        width: 50px; height: 50px; margin-top: -24px; border-radius: 16px;
        background: #003366; color: var(--nj-accent, #FFD700);
        display: grid; place-items: center; font-size: 1.25rem;
        box-shadow: 0 6px 16px rgba(0, 51, 102, .35), 0 0 0 4px var(--ph-bg, #f5f7fa);
        transition: transform .15s var(--m-ease);
    }
    .m-tab-new:active .m-fab { transform: scale(.92); }
    .m-tab-new[aria-expanded="true"] .m-fab i { transform: rotate(45deg); }
    .m-tab-new .m-fab i { font-size: 1.25rem; transition: transform .2s var(--m-ease); }
    body.dark-mode .m-tabbar { background: rgba(20, 24, 36, .94); border-top-color: #262b3a; }
    body.dark-mode .m-tab { color: #8a94ad; }
    body.dark-mode .m-tab.active { color: var(--nj-accent, #FFD700); }
    body.dark-mode .m-fab { background: var(--nj-accent, #FFD700); color: #0f111a; box-shadow: 0 6px 16px rgba(0, 0, 0, .45), 0 0 0 4px #0f111a; }

    /* ── Feuille « Nouveau » ─────────────────────────────────────────── */
    .m-sheet-backdrop {
        display: block; position: fixed; inset: 0; z-index: 1063;
        background: rgba(8, 18, 32, .5); opacity: 0; transition: opacity .25s var(--m-ease);
    }
    .m-sheet-backdrop.open { opacity: 1; }
    .m-sheet {
        display: block; position: fixed; left: 0; right: 0; bottom: 0; z-index: 1064;
        max-width: 640px; margin: 0 auto;
        background: var(--ph-card, #fff); color: var(--ph-text, #31374a);
        border-radius: 20px 20px 0 0; box-shadow: 0 -12px 40px rgba(8, 18, 32, .25);
        padding: 6px calc(16px + var(--m-sar)) calc(14px + var(--m-sab)) calc(16px + var(--m-sal));
        transform: translateY(105%); transition: transform .3s var(--m-ease);
        overscroll-behavior: contain; -webkit-user-select: none; user-select: none;
    }
    .m-sheet.open { transform: none; }
    .m-sheet:focus { outline: none; }
    .m-sheet-grip { width: 40px; height: 5px; border-radius: 3px; background: var(--ph-border-strong, #cbd0dd); margin: 4px auto 12px; }
    .m-sheet-title { font-weight: 800; font-size: 1.05rem; color: var(--ph-heading, #141824); margin: 0 4px 12px; }
    .m-sheet-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
    .m-sheet-grid a {
        display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
        min-height: 92px; padding: 12px 6px; border-radius: 14px; text-align: center;
        background: var(--ph-bg, #f5f7fa); border: 1px solid var(--ph-border, #e3e6ed);
        color: var(--ph-heading, #141824); font-weight: 700; font-size: .84rem; text-decoration: none;
        transition: transform .12s var(--m-ease), background .12s;
    }
    .m-sheet-grid a:active { transform: scale(.96); background: var(--ph-primary-soft, #e6eef7); }
    .m-ic { width: 42px; height: 42px; border-radius: 12px; display: grid; place-items: center; font-size: 1.1rem; }
    .m-ic.is-blue { background: #e6eef7; color: #003366; }
    .m-ic.is-green { background: #d9fbd0; color: #1c6c09; }
    .m-ic.is-orange { background: #fff6e0; color: #bc3803; }
    .m-ic.is-purple { background: #f1ecfb; color: #5b3a9e; }
    .m-ic.is-sky { background: #c7ebff; color: #005585; }
    .m-ic.is-yellow { background: #fff3b0; color: #6b5500; }
    .m-sheet-cancel {
        display: block; width: 100%; min-height: 48px; margin-top: 12px; border: 0; border-radius: 14px;
        background: var(--ph-bg, #f5f7fa); color: var(--ph-heading, #141824); font-weight: 800; font-size: .95rem;
    }
    body.dark-mode .m-sheet { background: #141824; color: #9fa6bc; }
    body.dark-mode .m-sheet-grid a, body.dark-mode .m-sheet-cancel { background: #0f111a; border-color: #262b3a; color: #eff2f6; }
    body.dark-mode .m-sheet-title { color: #eff2f6; }
    body.dark-mode .m-ic { background: rgba(255, 255, 255, .06) !important; color: var(--nj-accent, #FFD700) !important; }

    /* ── Menu « Plus » : le menu latéral en panneau plein écran ───────── */
    .sidebar {
        top: 0 !important; bottom: 0; height: auto !important; min-height: 0 !important; max-height: none !important;
        width: min(86vw, 330px) !important;
        padding: calc(var(--m-sat) + 6px) 12px calc(var(--m-sab) + 28px) calc(12px + var(--m-sal)) !important;
        transition: transform .28s var(--m-ease) !important;
        overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
        z-index: 1062 !important;
    }
    .sidebar.active { z-index: 1062 !important; }
    body .sidebar:not(.active) { box-shadow: none !important; }
    .card-custom { padding: 16px; }
    .sidebar-overlay { z-index: 1061 !important; background: rgba(8, 18, 32, .5) !important; }
    .sidebar .sidebar-brand, .sidebar .zs-trigger, .sidebar .sidebar-mini-brand { display: none !important; }
    .m-drawer-head {
        display: flex; align-items: center; gap: 6px; min-height: 52px;
        margin: 0 -4px 6px; padding: 0 0 8px 4px; border-bottom: 1px solid var(--ph-border, #e3e6ed);
    }
    .m-drawer-title { flex: 1; min-width: 0; font-weight: 800; font-size: 1.1rem; color: var(--ph-heading, #141824); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .m-drawer-head .m-top-btn { color: var(--ph-muted, #6e7891); }
    .m-drawer-head .m-top-btn:active { background: var(--ph-bg, #f5f7fa); }
    .m-drawer-extra { display: block; }
    .sidebar .nav-link-custom { min-height: 46px; font-size: .95rem; padding: 10px 12px; -webkit-touch-callout: none; }
    .sidebar .nav-link-custom i { width: 20px; font-size: 1rem !important; }
    .sidebar .nav-section-title { margin-top: 16px; }
    body.dark-mode .m-drawer-head { border-bottom-color: #262b3a; }
    body.dark-mode .m-drawer-title { color: #eff2f6; }
    body.dark-mode .sidebar { background: #141824; }

    /* ── Recherche globale en plein écran ────────────────────────────── */
    .zs-backdrop { padding: 0 !important; }
    .zs-panel { max-width: none; max-height: none; height: 100%; border-top: 0; padding-top: var(--m-sat); padding-bottom: var(--m-sab); }
    .zs-input-wrap { min-height: 60px; padding: 8px calc(8px + var(--m-sar)) 8px calc(16px + var(--m-sal)); }
    .zs-input-wrap .zs-kbd, .zs-foot { display: none !important; }
    .zs-results { flex: 1; overscroll-behavior: contain; }
    .zs-item { min-height: 52px; }
    .zs-input:focus-visible { outline: none; box-shadow: none; }
    .zs-cancel { display: block; border: 0; background: none; color: var(--ph-primary, #003366); font-weight: 800; font-size: .95rem; min-height: 44px; padding: 0 8px; }
    body.dark-mode .zs-cancel { color: var(--nj-accent, #FFD700); }

    /* ── Champs : 16 px minimum, sinon iOS zoome à la saisie ─────────── */
    input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]):not([type="file"]),
    select, textarea, .form-control, .form-control-sm, .form-control-lg, .form-select, .form-select-sm, .input-group-text {
        font-size: 16px !important;
    }
    .form-control, .form-select { min-height: 44px; }
    textarea.form-control { min-height: 88px; }
    .form-control-color { min-height: 44px; }
    .form-check-input { width: 1.3em; height: 1.3em; margin-top: .1em; }
    .form-switch .form-check-input { width: 2.4em; }
    .form-check { min-height: 30px; }

    /* ── Cibles tactiles ≥ 44 px ─────────────────────────────────────── */
    .btn { min-height: 44px; }
    .btn-sm, .btn-group-sm > .btn { min-height: 44px; padding-left: .8rem; padding-right: .8rem; }
    .btn:has(> i:only-child) { min-width: 44px; }
    .btn-close { width: 44px; height: 44px; padding: 0; background-size: 14px; margin: -10px -10px -10px auto !important; }
    .dropdown-item { padding-top: .72rem; padding-bottom: .72rem; }
    .page-link { min-width: 44px; min-height: 44px; display: grid; place-items: center; }
    .nav-tabs .nav-link, .nav-pills .nav-link, .doc-tab { min-height: 44px; display: inline-flex; align-items: center; }
    .icon-act { width: 44px !important; height: 44px !important; border-radius: 10px !important; }
    .btn:active, .icon-act:active { transform: scale(.97); }

    /* ── Tableaux : défilement horizontal maîtrisé ───────────────────── */
    .table-responsive, .main-content table:not(.table-cards) { overscroll-behavior-x: contain; -webkit-overflow-scrolling: touch; }

    /* Listes en cartes (factures et devis, contacts, dépenses) : ligne
       principale en tête, pied de tableau (totaux) sur une ligne. */
    table.table-cards tr { display: flex !important; flex-direction: column; border-radius: 12px; }
    table.table-cards td.td-primary { order: -1; }
    table.table-cards td.m-hide { display: none !important; }
    table.table-cards tfoot { display: block !important; }
    table.table-cards tfoot tr { flex-direction: row; flex-wrap: wrap; justify-content: space-between; align-items: center; font-weight: 800; }
    table.table-cards tfoot td { width: auto !important; }
    table.table-cards tfoot td:empty { display: none !important; }
    table.table-cards td.td-actions { gap: 6px; }
    table.table-cards td > a { min-width: 0; overflow-wrap: anywhere; }
    table.table-cards td.td-actions form { display: inline-flex; margin: 0; }

    /* ── Modales Bootstrap : feuilles du bas (hors petites confirmations) */
    .modal:not(.z-dialog) .modal-dialog:not([class*="modal-fullscreen"]) {
        width: 100%; max-width: 640px; margin: 0 auto; min-height: 100%; height: auto;
        display: flex; align-items: flex-end;
    }
    .modal.fade:not(.z-dialog) .modal-dialog { transform: translateY(100%); transition: transform .32s var(--m-ease); }
    .modal.show:not(.z-dialog) .modal-dialog { transform: none; }
    .modal:not(.z-dialog) .modal-dialog-scrollable { height: 100%; }
    .modal:not(.z-dialog) .modal-content {
        width: 100%; border: 0 !important; border-radius: 20px 20px 0 0 !important;
        max-height: calc(100dvh - var(--m-sat) - 12px); padding-bottom: var(--m-sab);
    }
    .modal:not(.z-dialog) .modal-content::before {
        content: ""; flex-shrink: 0; display: block; width: 40px; height: 5px; border-radius: 3px;
        background: var(--ph-border-strong, #cbd0dd); margin: 8px auto -2px;
    }
    .modal-content > form { display: flex; flex-direction: column; min-height: 0; flex: 1 1 auto; }
    .modal .modal-body { min-height: 0; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; padding: 18px calc(16px + var(--m-sar)) 18px calc(16px + var(--m-sal)); }
    .modal .modal-header { padding: 10px 16px 12px 18px; touch-action: none; }
    .modal .modal-footer { padding: 12px 16px; flex-wrap: wrap; }
    .modal:not(.z-dialog) .modal-footer > .btn { flex: 1 1 auto; }
    .modal:not(.z-dialog) .modal-footer { background: var(--ph-card, #fff) !important; }
    body.dark-mode .modal:not(.z-dialog) .modal-footer { background: #141824 !important; }
    .z-dialog .modal-dialog { margin-left: calc(16px + var(--m-sal)); margin-right: calc(16px + var(--m-sar)); }
    .dropdown-menu { max-width: calc(100vw - 24px); }

    /* ── Invitation à installer ──────────────────────────────────────── */
    .m-install {
        display: flex; align-items: center; gap: 12px;
        position: fixed; z-index: 1039; left: calc(10px + var(--m-sal)); right: calc(10px + var(--m-sar));
        bottom: calc(var(--m-tab) + var(--m-sab) + 10px);
        padding: 12px 8px 12px 12px; border-radius: 16px;
        background: #003366; color: #fff; box-shadow: 0 12px 32px rgba(8, 18, 32, .3);
        transform: translateY(20px); opacity: 0; transition: transform .3s var(--m-ease), opacity .3s;
    }
    body:not(.has-tabbar) .m-install { bottom: calc(10px + var(--m-sab)); }
    .m-install.open { transform: none; opacity: 1; }
    .m-install img { width: 42px; height: 42px; border-radius: 10px; flex-shrink: 0; }
    .m-install-text { flex: 1; min-width: 0; font-size: .82rem; line-height: 1.35; }
    .m-install-text strong { display: block; font-size: .92rem; color: var(--nj-accent, #FFD700); }
    .m-install-text i { color: var(--nj-accent, #FFD700); }
    .m-install .btn { background: var(--nj-accent, #FFD700); color: #003366; border: 0; flex-shrink: 0; }
    .m-install[data-kind="ios"] .m-install-android, .m-install[data-kind="ios"] .m-install-go,
    .m-install:not([data-kind="ios"]) .m-install-ios { display: none; }
    .m-install-close {width: 40px; height: 44px; border: 0; background: none; color: rgba(255, 255, 255, .7); flex-shrink: 0; font-size: 1.05rem; }
    body.dark-mode .m-install { background: #1c2130; border: 1px solid #373e53; }

    /* ── Barre de chargement entre deux pages (pas d'indicateur du
          navigateur en mode application) ─────────────────────────────── */
    .m-progress {
        display: block; position: fixed; z-index: 2001; left: 0; top: var(--m-sat); height: 3px; width: 100%;
        background: var(--nj-accent, #FFD700); transform-origin: left; transform: scaleX(0); opacity: 0; pointer-events: none;
    }
    html.is-navigating .m-progress { opacity: 1; animation: mProgress 6s cubic-bezier(.1, .7, .2, 1) forwards; }
}

@keyframes mProgress { 0% { transform: scaleX(0); } 20% { transform: scaleX(.45); } 100% { transform: scaleX(.92); } }

/* ── Application installée (écran d'accueil) ─────────────────────────── */
@media (display-mode: standalone), (display-mode: minimal-ui) {
    html, body { overscroll-behavior-y: none; }
    .m-install { display: none !important; }
}
html.is-standalone, html.is-standalone body { overscroll-behavior-y: none; }
html.is-standalone .m-install, html.is-standalone .m-only-browser { display: none !important; }
@media (max-width: 768px) {
    /* L'interface (en-têtes, onglets, menus) ne se sélectionne pas comme du texte. */
    html.is-standalone .nav-link-custom, html.is-standalone .btn, html.is-standalone .page-head h3 { -webkit-user-select: none; user-select: none; }
}

/* ── Transitions douces entre pages (Chrome 126+, Safari 18.2+) ──────── */
@media (max-width: 768px) and (prefers-reduced-motion: no-preference) {
    @view-transition { navigation: auto; }
    .mobile-topbar { view-transition-name: m-header; }
    .m-tabbar { view-transition-name: m-tabbar; }
    ::view-transition-old(root), ::view-transition-new(root) { animation-duration: .18s; }
}

@media (prefers-reduced-motion: reduce) {
    .m-sheet, .m-sheet-backdrop, .m-install, .sidebar, .modal .modal-dialog, .m-tab i, .m-fab { transition: none !important; }
    html.is-navigating .m-progress { animation: none; transform: scaleX(.6); }
}
