/* ═══════════════════════════════════════════════════════════════
   91STUDIO — RESPONSIVE v7.0.0 (Apple HIG)
   Breakpoints : <480 phone | <640 mobile | 640-1024 tablet
                  1024-1440 laptop | 1440-2560 desktop | >2560 ultra

   STRATEGIE OVERFLOW MOBILE :
   Les wrappers modules gardent overflow-x:hidden (contient dans viewport).
   Les enfants internes (tables, tabs) ont overflow-x:auto pour le scroll.
   Les toolbars/navs sont contraints avec max-width:100% + flex-wrap.

   STRATEGIE TABS MOBILE :
   Les barres d'onglets (6-9 boutons) sont remplacées par un <select>
   natif sur mobile. Le JS (mobile-nav-dropdown.js) crée un select
   miroir et synchronise l'onglet actif. Le CSS masque la barre
   et affiche le select uniquement sous 640px.

   SIDEBAR MOBILE :
   Swipe-to-close, auto-close on nav, plus grandes touch targets.
   Bottom nav pill avec touch targets 44px+.
   ═══════════════════════════════════════════════════════════════ */

/* ───────────────────────────────────────────
   MOBILE TAB DROPDOWN — Masqué par défaut sur desktop
   ─────────────────────────────────────────── */
.mobile-tab-dropdown {
    display: none;
}

/* ───────────────────────────────────────────
   0. GLOBAL MOBILE — < 640px
   ─────────────────────────────────────────── */
@media (max-width: 639px) {

    /* ══════════════════════════════════════════
       A. CONTAINMENT — Enfants contraints dans viewport
       ══════════════════════════════════════════ */

    /* Chantiers */
    .ch-module {
        overflow-x: hidden !important;
    }
    .ch-module > *,
    .ch-projet-selector,
    .ch-projet-selector > *,
    .ch-content > * {
        max-width: 100% !important;
        box-sizing: border-box !important;
    }

    /* Facturation */
    #factContent {
        overflow-x: hidden !important;
        max-width: 100vw !important;
    }
    #factContent .fact-wrapper {
        overflow-x: hidden !important;
        overflow-y: auto !important;
        padding: 0 12px 12px !important;
    }
    .fact-wrapper > * {
        max-width: 100% !important;
        box-sizing: border-box !important;
    }

    /* Trésorerie — unlock viewport lock */
    .treso-wrapper {
        overflow-x: hidden !important;
        overflow-y: auto !important;
        height: auto !important;
        flex: none !important;
    }
    #page-content:has(.treso-wrapper) {
        overflow-y: auto !important;
        overflow-x: hidden !important;
        height: auto !important;
        min-height: auto !important;
    }
    .treso-content {
        overflow-x: hidden !important;
        overflow-y: auto !important;
        flex: none !important;
        height: auto !important;
        min-height: 0 !important;
    }
    .treso-transactions {
        overflow-x: hidden !important;
        overflow-y: auto !important;
        flex: none !important;
        height: auto !important;
    }
    .treso-wrapper > *,
    .treso-content > * {
        max-width: 100% !important;
        box-sizing: border-box !important;
    }

    /* Projets */
    #prjContent > * {
        max-width: 100% !important;
        box-sizing: border-box !important;
    }

    /* RH */
    .rh-wrapper > *,
    .rh-content > * {
        max-width: 100% !important;
        box-sizing: border-box !important;
    }

    /* ══════════════════════════════════════════
       B. MODULE HEADERS — Detail views
       Desktop : 1 ligne, height:44px, overflow:hidden.
       Mobile : 2 lignes, height:auto, overflow:visible.
       ══════════════════════════════════════════ */

    /* --- Chantiers detail header --- */
    .ch-header {
        height: auto !important;
        min-height: 44px !important;
        overflow: visible !important;
        flex-wrap: wrap !important;
        padding: 8px 12px !important;
        gap: 4px 8px !important;
        align-items: center !important;
    }
    .ch-header-left {
        order: 1;
        flex: 1 1 auto;
        min-width: 0;
    }
    .ch-project-title {
        font-size: 13px !important;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        max-width: 200px;
    }
    .ch-header-actions {
        order: 2;
        flex-shrink: 0;
        margin-left: auto !important;
    }

    /* --- Projets detail header --- */
    .prj-main-nav {
        height: auto !important;
        min-height: 44px !important;
        overflow: visible !important;
        flex-wrap: wrap !important;
        padding: 8px 12px !important;
        gap: 4px 8px !important;
    }
    .prj-breadcrumb {
        order: 1;
        font-size: 13px;
        flex-wrap: nowrap;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: calc(100% - 50px);
    }
    #prjContent .prj-main-nav {
        padding: 0 8px !important;
        overflow-x: visible !important;
        max-width: 100% !important;
    }
    #prjContent .prj-main-nav::-webkit-scrollbar {
        display: none;
    }

    /* ══════════════════════════════════════════
       C. TOOLBARS — Empilées verticalement
       ══════════════════════════════════════════ */

    /* Chantiers toolbar (liste) */
    .ch-toolbar {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 8px !important;
        padding: 8px 0 !important;
        max-width: 100% !important;
    }
    .ch-toolbar-filters {
        flex-wrap: wrap !important;
        flex-direction: column !important;
        gap: 8px !important;
        width: 100% !important;
        max-width: 100% !important;
    }
    .ch-search-wrap {
        width: 100% !important;
        min-width: 0 !important;
    }
    .ch-search-input {
        width: 100% !important;
    }
    .ch-filters-inline {
        flex-wrap: wrap !important;
        gap: 8px !important;
        max-width: 100% !important;
        width: 100% !important;
    }
    .ch-filters-inline select,
    .ch-toolbar-filters select {
        flex: 1 1 calc(50% - 4px) !important;
        min-width: 0 !important;
        max-width: calc(50% - 4px) !important;
        font-size: 12px;
    }

    /* Facturation toolbar */
    .fact-toolbar {
        flex-wrap: wrap !important;
        gap: 8px !important;
        max-width: 100% !important;
        overflow: hidden !important;
        padding: 8px 0 !important;
    }
    .fact-toolbar-actions {
        flex-wrap: wrap !important;
        gap: 4px !important;
        max-width: 100% !important;
    }
    .fact-toolbar-actions .fact-toolbar-btn {
        font-size: 11px !important;
        padding: 6px 8px !important;
    }
    .fact-toolbar-actions .fact-toolbar-btn span:not(:first-child) {
        display: none;
    }

    /* Trésorerie toolbar */
    .treso-topbar {
        flex-direction: column !important;
        align-items: stretch !important;
        padding: 8px 12px !important;
        gap: 8px !important;
        max-width: 100% !important;
    }
    .treso-toolbar {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 8px !important;
        padding: 8px 12px !important;
    }

    /* RH toolbar */
    .rh-toolbar {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 8px !important;
        padding: 8px 0 !important;
    }
    .rh-toolbar-right {
        flex-direction: column !important;
        gap: 8px !important;
    }
    .rh-select {
        min-width: 0 !important;
        width: 100% !important;
    }

    /* RH month nav — HORIZONTAL, pas vertical */
    .rh-month-nav {
        flex-direction: row !important;
        gap: 8px !important;
        align-items: center !important;
        justify-content: space-between !important;
        margin-bottom: 16px !important;
    }
    .rh-month-nav h2 {
        min-width: 0 !important;
        font-size: 17px !important;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .rh-month-nav button {
        padding: 8px 12px !important;
        font-size: 13px !important;
        flex-shrink: 0;
        min-height: 36px;
    }

    /* Projets breadcrumb */
    .prj-breadcrumb {
        font-size: 13px;
        flex-wrap: wrap;
        gap: 4px;
    }

    /* Studio generics */
    .studio-toolbar {
        flex-direction: column;
        align-items: stretch;
    }
    .studio-toolbar-actions {
        justify-content: flex-start;
    }
    .studio-form-row {
        flex-direction: column;
    }

    /* ══════════════════════════════════════════
       D. TABS → DROPDOWN NATIF sur mobile
       Les barres d'onglets sont masquées.
       Un <select> natif les remplace (injecté par mobile-nav-dropdown.js).
       ══════════════════════════════════════════ */

    /* --- Afficher le select dropdown sur mobile --- */
    .mobile-tab-dropdown {
        display: block !important;
        width: 100% !important;
        min-height: 44px;
        padding: 10px 40px 10px 16px;
        font-size: 15px;
        font-weight: 600;
        letter-spacing: -0.24px;
        color: var(--text-primary, #1A1A1A);
        background-color: var(--bg-secondary, #F2F2F7);
        border: 1px solid var(--border-color, #E5E5E5);
        border-radius: 10px;
        appearance: none;
        -webkit-appearance: none;
        background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1.5L6 6.5L11 1.5' stroke='%236B7280' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round' fill='none'/%3E%3C/svg%3E");
        background-repeat: no-repeat;
        background-position: right 14px center;
        background-size: 12px 8px;
        box-sizing: border-box;
        margin-bottom: 8px;
        cursor: pointer;
    }
    .mobile-tab-dropdown:focus {
        outline: none;
        border-color: var(--accent-blue, #0A84FF);
        box-shadow: 0 0 0 3px rgba(10, 132, 255, 0.2);
    }

    /* Dark mode dropdown */
    [data-theme="dark"] .mobile-tab-dropdown,
    [data-theme-variant="dark"] .mobile-tab-dropdown,
    [data-theme-variant="classic-dark"] .mobile-tab-dropdown,
    [data-theme-variant="magic-blue"] .mobile-tab-dropdown {
        color: var(--text-primary, #FAFAFA);
        background-color: var(--bg-tertiary, #202020);
        border-color: rgba(255, 255, 255, 0.1);
        background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1.5L6 6.5L11 1.5' stroke='%23A0A0A6' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round' fill='none'/%3E%3C/svg%3E");
    }

    /* --- Masquer TOUTES les barres d'onglets originales --- */
    /* Modules principaux */
    .fact-nav,
    .fp-main-tabs,
    .fact-project-nav,
    .treso-nav,
    .rh-tabs-inline,
    .ch-tabs-inline,
    .prj-tabs-inline,
    .prj-main-nav .prj-tabs-inline,
    .prj-nav-tabs,
    /* Dashboard */
    .dash-tabs,
    .du-tabs,
    /* Planning — zoom + filtres */
    .zoom-btn,
    .filter-pill,
    /* Modules secondaires */
    .editor-tabs,
    .df-panel-tabs-row,
    .diff-tabs,
    .diff-project-tabs,
    .mp-tabs,
    /* Sub-tabs / filtres internes */
    .treso-bank-tabs,
    .rapprochement-bank-tabs,
    .ch-filter-tabs,
    .ch-subtabs,
    .chr-category-tabs,
    .chr-filter-tabs,
    .risk-tabs,
    .ctr-tabs,
    .cr-type-tabs,
    .activity-tabs {
        display: none !important;
    }
    /* Planning boutons individuels (pas de container) + parent converti par JS */
    .mobile-tab-converted,
    .zoom-btn,
    .filter-pill,
    .fact-project-tab,
    .ch-subtab {
        display: none !important;
    }

    /* ══════════════════════════════════════════
       D2. SIDEBAR MOBILE — Touch-friendly
       ══════════════════════════════════════════ */

    /* Sidebar navigation items — touch targets 44px */
    .sidebar .sidebar-link,
    .sidebar .nav-link,
    .sidebar a[href^="#"] {
        min-height: 44px !important;
        display: flex !important;
        align-items: center !important;
        padding: 10px 16px !important;
    }

    /* Hamburger menu button — plus gros et visible */
    .main-header .btn-menu {
        min-width: 44px !important;
        min-height: 44px !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
    }

    /* Sidebar overlay — assurer la fermeture au tap */
    .sidebar-overlay {
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
    }

    /* Sidebar — transition fluide et ombre de profondeur */
    .sidebar {
        transition: transform 0.3s cubic-bezier(0.25, 0.1, 0.25, 1) !important;
    }
    .sidebar.open {
        box-shadow: 0 0 40px rgba(0, 0, 0, 0.3) !important;
    }

    /* ══════════════════════════════════════════
       D3. BOTTOM NAV — Touch targets améliorés
       ══════════════════════════════════════════ */

    .shell-bottom-nav {
        padding: 6px 12px !important;
        padding-bottom: calc(6px + env(safe-area-inset-bottom, 0px)) !important;
    }
    .shell-bottom-nav-inner {
        gap: 2px;
        padding: 3px !important;
    }
    .shell-bottom-nav-item {
        min-height: 44px !important;
        padding: 6px 4px !important;
        gap: 2px !important;
        border-radius: 12px !important;
    }
    .shell-bottom-nav-item svg {
        width: 22px !important;
        height: 22px !important;
    }
    .shell-bottom-nav-item span {
        font-size: 10px !important;
        font-weight: 600 !important;
        max-width: none !important;
    }
    /* Active state plus visible */
    .shell-bottom-nav-item.active {
        background: var(--bg-active, rgba(0, 0, 0, 0.06)) !important;
        color: var(--text-primary) !important;
    }

    /* ══════════════════════════════════════════
       E. TABLES — Scroll horizontal interne
       ══════════════════════════════════════════ */

    /* Chantiers table */
    .ch-projet-table-wrap {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        max-width: 100% !important;
    }
    .ch-projet-table {
        table-layout: auto;
        min-width: 500px;
    }
    .ch-projet-table th,
    .ch-projet-table td {
        padding: 10px 8px !important;
        font-size: 12px !important;
    }

    /* Facturation table */
    .fact-table-container {
        max-width: 100% !important;
    }
    .fact-table-scroll {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        max-width: 100% !important;
    }
    .fact-table {
        min-width: 600px;
    }
    .fact-table th,
    .fact-table td {
        padding: 8px 6px !important;
        font-size: 11px !important;
        white-space: nowrap;
    }
    .fact-table th:first-child,
    .fact-table td:first-child {
        position: sticky;
        left: 0;
        z-index: 5;
        background: var(--bg-card, #fff);
        min-width: 100px;
        border-right: 1px solid var(--border-light, #e5e5e5);
    }

    /* Trésorerie table */
    .treso-table-wrap,
    .treso-table-container {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        max-width: 100% !important;
    }

    /* RH calendar */
    .rh-temps-calendar {
        gap: 1px;
    }
    .rh-temps-day-cell {
        padding: 4px 2px !important;
        min-height: 44px;
        font-size: 11px !important;
    }

    /* Chantiers tables internes */
    .ch-table-wrap,
    .ch-table-scroll {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        max-width: 100% !important;
    }

    /* Generic studio tables */
    .studio-table [data-priority="2"],
    .studio-table [data-priority="3"] {
        display: none;
    }

    /* ══════════════════════════════════════════
       F. KPIs — Empilés verticalement
       ══════════════════════════════════════════ */

    /* Dashboard */
    .ov-ca-grid {
        grid-template-columns: 1fr !important;
        gap: 8px;
    }
    .ov-ca-grid > * {
        min-width: 0;
    }
    .ov-kpi-col {
        padding: 16px;
    }
    .ov-kpi-val {
        font-size: 28px;
    }

    /* Facturation KPIs */
    .fact-kpis {
        display: flex !important;
        flex-direction: column !important;
        gap: 8px !important;
        max-width: 100% !important;
    }
    .fact-kpi-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 8px;
    }
    .fact-kpi-value {
        font-size: 14px !important;
    }
    .fact-kpi-hero-value {
        font-size: 28px;
    }
    .fact-kpi-card {
        min-width: 0 !important;
        overflow: hidden;
    }

    /* Trésorerie KPIs */
    .treso-kpis {
        grid-template-columns: 1fr !important;
        gap: 8px;
        padding: 0 !important;
    }
    .treso-kpi-card {
        padding: 12px;
    }
    .treso-kpi-value {
        font-size: 22px;
    }
    .treso-card-chart {
        min-height: 160px;
    }
    .treso-row {
        grid-template-columns: 1fr !important;
    }
    .treso-card-chart {
        grid-column: span 1 !important;
    }

    /* Trésorerie filtres */
    .treso-filters {
        flex-direction: column !important;
        gap: 8px;
    }
    .treso-preview-grid {
        grid-template-columns: 1fr !important;
    }

    /* RH KPIs */
    .rh-kpi-grid-2,
    .rh-kpi-grid-3,
    .rh-kpi-grid-4,
    .rh-kpi-grid-5 {
        grid-template-columns: 1fr !important;
    }

    /* Chantiers KPIs */
    .ch-kpi-grid,
    .ch-unified-grid {
        grid-template-columns: 1fr !important;
        gap: 8px;
    }
    .ch-cards-grid {
        grid-template-columns: 1fr !important;
    }
    .ch-unified-card {
        padding: 16px !important;
    }

    /* Studio generics */
    .studio-kpi-grid {
        grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    }
    .studio-grid {
        grid-template-columns: 1fr;
    }
    .studio-section {
        margin-bottom: var(--spacing-xl, 24px);
    }
    .studio-kpi-value {
        font-size: 22px;
    }

    /* ══════════════════════════════════════════
       G. SPACING + PADDING — Compact mobile
       ══════════════════════════════════════════ */

    /* Chantiers */
    .ch-projet-selector {
        padding: 0 12px !important;
        max-width: 100% !important;
    }
    .ch-content {
        padding: 0 12px !important;
    }

    /* Facturation */
    .fact-large-title {
        font-size: 24px !important;
        padding: 12px !important;
    }
    .fact-wrapper {
        padding: 0 12px 12px !important;
    }

    /* Trésorerie */
    .treso-content {
        padding: 0 8px 8px !important;
    }
    .treso-large-title {
        padding: 0 12px !important;
        font-size: 24px !important;
    }

    /* RH */
    .rh-module {
        padding: 12px 12px 80px !important;
    }
    .rh-large-title {
        font-size: 24px !important;
        padding: 0 !important;
    }

    /* Projets */
    .prj-wrapper,
    .prj-content {
        padding: 0 12px !important;
    }

    /* Agent notifications */
    .agent-notification-bar {
        position: relative !important;
        max-width: 100% !important;
    }

    /* Modals — plein écran sur mobile */
    .ch-modal-xlarge,
    .fact-modal,
    .treso-modal {
        width: 100% !important;
        max-width: 100% !important;
        margin: 0 !important;
        border-radius: 0 !important;
        height: 100vh !important;
        max-height: 100vh !important;
    }
}

/* ───────────────────────────────────────────
   0b. PHONE — < 480px (iPhone SE/mini/standard)
   ─────────────────────────────────────────── */
@media (max-width: 479px) {

    /* Chantiers : masquer colonnes secondaires */
    .ch-projet-table th:nth-child(1),
    .ch-projet-table td:nth-child(1),
    .ch-projet-table th:nth-child(3),
    .ch-projet-table td:nth-child(3),
    .ch-projet-table th:nth-child(6),
    .ch-projet-table td:nth-child(6) {
        display: none;
    }

    /* Facturation : table ultra compacte */
    .fact-table th,
    .fact-table td {
        padding: 6px 4px !important;
        font-size: 10px !important;
    }

    /* RH : compact mais touch-friendly */
    .rh-temps-day-cell {
        padding: 2px 1px !important;
        min-height: 44px;
        font-size: 10px !important;
    }

    /* Chantier actions — icônes plus petites */
    .ch-header-icon-btn {
        width: 24px !important;
        height: 24px !important;
    }

    /* Bottom nav — plus compact sur petit écran */
    .shell-bottom-nav-item span {
        font-size: 9px !important;
    }
}

/* ───────────────────────────────────────────
   2. TABLET — 640px à 1023px
   ─────────────────────────────────────────── */
@media (min-width: 640px) and (max-width: 1023px) {
    .studio-table [data-priority="3"] {
        display: none;
    }
    .studio-grid {
        grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    }

    /* Chantiers header — tabs scroll aussi sur tablette */
    .ch-header {
        height: auto !important;
        min-height: 44px !important;
        overflow: visible !important;
        flex-wrap: wrap !important;
    }
    .ch-tabs-inline {
        flex-wrap: nowrap !important;
        overflow-x: auto !important;
        scrollbar-width: none;
    }
    .ch-tabs-inline::-webkit-scrollbar {
        display: none;
    }
}

/* ───────────────────────────────────────────
   3. LAPTOP — 1024px à 1439px
   ─────────────────────────────────────────── */
@media (min-width: 1024px) and (max-width: 1439px) {
    /* Layout standard */
}

/* ───────────────────────────────────────────
   4. DESKTOP — 1440px à 2559px
   ─────────────────────────────────────────── */
@media (min-width: 1440px) {
    .studio-grid {
        grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
    }
    .studio-kpi-value {
        font-size: 32px;
    }
}

/* ───────────────────────────────────────────
   5. ULTRA — > 2560px
   ─────────────────────────────────────────── */
@media (min-width: 2560px) {
    .studio-kpi-grid {
        grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
    }
    .studio-grid {
        grid-template-columns: repeat(auto-fit, minmax(380px, 1fr));
    }
}
