/* ═════════════════════════════════════════════════════════════════════════
   mobile-fallback.css — Trezo (2026-05-21)
   Rend TOUTES les pages desktop (Dashboard, Trésorerie, Marges, Caisse,
   Chantiers, Factures, Fournisseurs, Cautions, Marchés, Ledger, etc.)
   utilisables sur iPhone Safari / PWA.

   Stratégie :
     - <768px : sidebar masquée, sortie via hamburger overlay
     - main occupe toute la largeur
     - tableaux : scroll horizontal forcé (.table-wrap)
     - grids → stack vertical
     - bottom-nav universel injecté par JS (mobile-bottom-nav.js)
     - tap targets ≥44px, polices lisibles, padding bottom safe-area
   ═════════════════════════════════════════════════════════════════════════ */

@media (max-width: 768px) {

  /* ── Layout root : sidebar cachée par défaut, main full-width ────────── */
  body .sidebar,
  body .injected-sidebar,
  body aside.sidebar {
    position: fixed !important;
    top: 0;
    left: 0;
    bottom: 0;
    width: 78vw !important;
    max-width: 320px !important;
    min-height: 100vh !important;
    transform: translateX(-100%);
    transition: transform .28s cubic-bezier(.2,.8,.2,1);
    z-index: 1000;
    overflow-y: auto !important;
    overflow-x: hidden !important;
    flex-direction: column !important;
    box-shadow: 0 0 60px rgba(0,0,0,.5);
  }
  body.sb-open .sidebar,
  body.sb-open .injected-sidebar,
  body.sb-open aside.sidebar {
    transform: translateX(0);
  }

  /* Overlay derrière la sidebar ouverte */
  body.sb-open::before {
    content: '';
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.5);
    z-index: 999;
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
  }

  /* ── Main : pleine largeur, padding réduit, place pour bottom-nav ───── */
  body .app { display: block !important; }
  body .main {
    margin-left: 0 !important;
    padding: 0 !important;
    width: 100% !important;
  }
  body .content {
    padding: 1rem 1rem calc(80px + env(safe-area-inset-bottom)) !important;
    max-width: 100vw;
    overflow-x: hidden;
  }

  /* ── Topbar : compacte + bouton hamburger ─────────────────────────────── */
  body .topbar {
    padding: .6rem .9rem !important;
    gap: .5rem;
    position: sticky;
    top: 0;
    z-index: 50;
  }
  body .topbar-title { font-size: 15px !important; }
  body .topbar-sub   { font-size: 11px !important; }

  /* ── Headings : tailles raisonnables ──────────────────────────────────── */
  body h1, body .page-header h1 { font-size: 22px !important; line-height: 1.2 !important; }
  body h2 { font-size: 18px !important; }
  body h3 { font-size: 16px !important; }

  /* ── KPI grids : 1 colonne (parfois 2 si on a 4 KPIs symétriques) ────── */
  body .kpi-row,
  body .kpi-grid,
  body .stat-grid,
  body .stats-row,
  body .summary-grid,
  body [class*="grid-cols-"] {
    display: grid !important;
    grid-template-columns: 1fr 1fr !important;
    gap: .6rem !important;
  }
  body .kpi-row.single-col,
  body .kpi-grid.single-col {
    grid-template-columns: 1fr !important;
  }
  body .kpi { padding: .9rem !important; }
  body .kpi .k-value,
  body .kpi-val { font-size: 18px !important; }
  body .kpi .k-label,
  body .kpi-label { font-size: 10px !important; }

  /* ── Cards : pleine largeur, marges réduites ─────────────────────────── */
  body .card,
  body .acc-card,
  body .panel,
  body .section {
    border-radius: 12px !important;
    margin-bottom: .8rem !important;
  }

  /* ── Tables : wrapper scrollable horizontal ──────────────────────────── */
  body table {
    font-size: 12px !important;
    min-width: 100%;
  }
  body .table-wrap,
  body .table-scroll,
  body .card > table,
  body main table {
    display: block !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    width: 100%;
    max-width: 100vw;
  }
  body th, body td {
    padding: .55rem .65rem !important;
    white-space: nowrap;
  }

  /* ── Forms : inputs plein-écran, boutons larges ──────────────────────── */
  body input:not([type="checkbox"]):not([type="radio"]),
  body select,
  body textarea {
    width: 100% !important;
    font-size: 16px !important;  /* anti-zoom Safari */
    padding: .65rem .8rem !important;
    border-radius: 9px !important;
  }
  body .btn,
  body button.btn {
    padding: .65rem 1rem !important;
    font-size: 14px !important;
    min-height: 44px;
  }
  body .btn-sm { min-height: 38px !important; padding: .45rem .8rem !important; }

  /* ── Form grids : empilées ────────────────────────────────────────────── */
  body .form-grid-2,
  body .form-grid-3,
  body .form-row {
    display: block !important;
    grid-template-columns: none !important;
  }
  body .form-grid-2 > *,
  body .form-grid-3 > *,
  body .form-row > * {
    width: 100% !important;
    margin-bottom: .7rem;
  }

  /* ── Toolbars / filtres : wrap + gap réduit ──────────────────────────── */
  body .toolbar,
  body .filters,
  body .actions-bar {
    flex-wrap: wrap !important;
    gap: .4rem !important;
  }

  /* ── Tabs : scroll horizontal ────────────────────────────────────────── */
  body .tabs,
  body .tab-bar {
    display: flex !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
    gap: .4rem;
  }
  body .tab { flex-shrink: 0 !important; }

  /* ── Bouton hamburger (injecté par mobile-bottom-nav.js) ─────────────── */
  #trezo-sb-hamburger {
    position: fixed;
    top: calc(8px + env(safe-area-inset-top));
    left: 10px;
    z-index: 60;
    width: 40px;
    height: 40px;
    border-radius: 10px;
    background: var(--surface, #fff);
    border: 1px solid var(--line, rgba(0,0,0,.1));
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0,0,0,.08);
    font-size: 18px;
    color: var(--ink, #15171c);
  }
  body.sb-open #trezo-sb-hamburger { display: none; }

  /* Décale la topbar pour laisser place au hamburger */
  body .topbar { padding-left: 56px !important; }

  /* ── Bottom nav universel (injecté JS) ───────────────────────────────── */
  #trezo-mobile-bottom-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--surface, #fff);
    border-top: 1px solid var(--line, rgba(0,0,0,.1));
    padding: 6px 8px env(safe-area-inset-bottom);
    display: flex;
    justify-content: space-around;
    z-index: 50;
    box-shadow: 0 -2px 12px rgba(0,0,0,.06);
  }
  #trezo-mobile-bottom-nav a {
    flex: 1;
    text-align: center;
    color: var(--ink-muted, #6b7280);
    text-decoration: none;
    font-size: 10px;
    padding: 6px 4px;
    border-radius: 8px;
    font-weight: 600;
    line-height: 1.1;
  }
  #trezo-mobile-bottom-nav a .ic {
    display: block;
    font-size: 18px;
    margin-bottom: 2px;
  }
  #trezo-mobile-bottom-nav a.active {
    color: var(--brand, #2563eb);
    background: var(--brand-soft, rgba(37,99,235,.08));
  }

  /* ── Floating Q&A button : remonté pour pas chevaucher la nav ────────── */
  #trezo-qa-launcher {
    bottom: calc(76px + env(safe-area-inset-bottom)) !important;
  }

  /* ── Images / svg : ne pas déborder ──────────────────────────────────── */
  body img, body svg { max-width: 100%; height: auto; }

  /* ── Cacher quelques éléments purement desktop ───────────────────────── */
  body .desktop-only,
  body kbd,
  body .sb-search-hint kbd { display: none !important; }
}

/* ── Tablettes (640-1024) : juste resserrer un peu, garder sidebar ────── */
@media (min-width: 769px) and (max-width: 1024px) {
  body .main { padding: 1.4rem 1.4rem 2rem !important; }
}
