/* ═══════════════════════════════════════════
   MOBILE RESPONSIVE — FOOD HUB
   Breakpoints : 900px (tablet) / 600px (mobile) / 390px (iPhone small)
═══════════════════════════════════════════ */

/* Hamburger button */
.sidebar-toggle {
  display: none;
  position: fixed; top: 14px; left: 14px; z-index: 200;
  background: var(--obsidian); color: var(--white);
  border: none; border-radius: 10px; padding: 8px 12px;
  font-size: 1.1rem; cursor: pointer; box-shadow: var(--shadow-md);
  transition: all .18s;
}
.sidebar-toggle:hover { background: var(--ink); }

/* Overlay */
.sidebar-overlay {
  display: none;
  position: fixed; inset: 0; z-index: 99;
  background: rgba(0,0,0,.5);
  backdrop-filter: blur(2px);
}
.sidebar-overlay.open { display: block; }

/* ═══════════════════════════════════════════
   TABLET & below — 900px
═══════════════════════════════════════════ */
@media(max-width: 900px) {
  /* Empêche le scroll horizontal global (Salim 2026-05-06 : contenu coupé à
     gauche sur /admin/users/show car table SP avec col-md-* large débordait). */
  html, body { overflow-x: hidden; max-width: 100vw; }
  body { width: 100% }

  .sidebar-toggle { display: block; }

  #sidebar {
    transform: translateX(-100%);
    transition: transform .25s cubic-bezier(.4,0,.2,1);
    z-index: 150;
    width: 280px !important;
  }
  #sidebar.open { transform: translateX(0); }

  #main { margin-left: 0 !important; }

  .topbar {
    padding: 12px 16px 12px 64px !important;
    flex-wrap: wrap !important;
    gap: 8px !important;
    row-gap: 8px !important;
    min-height: 60px;
  }
  /* Salim 2026-05-11 : sur mobile la topbar peut wrap → titre sur une ligne,
     actions sur l'autre. On garde la padding-left=64px pour clear le burger
     fixed à 14px+40px width. */
  .topbar > div:last-child { flex-wrap: wrap; gap: 6px !important; }
  .content { padding: 16px !important; }

  /* Tables → scroll horizontal natif */
  .table-card,
  .cl-table-wrap,
  .sp-table-wrap,
  .us-table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .table-card table,
  .cl-table,
  .sp-table,
  .us-table { min-width: 640px; }

  /* Grids inline 4-5 colonnes → 2 colonnes */
  [style*="grid-template-columns:repeat(4,1fr)"],
  [style*="grid-template-columns: repeat(4,1fr)"],
  [style*="grid-template-columns:repeat(4, 1fr)"],
  [style*="grid-template-columns:repeat(5,1fr)"],
  [style*="grid-template-columns: repeat(5,1fr)"] {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  /* Cards → pleine largeur */
  .cc-grid { grid-template-columns: repeat(2, 1fr) !important; }
  .hero-metrics { grid-template-columns: repeat(2, 1fr) !important; }

  /* Stats Bootstrap → 2 colonnes */
  .row .col-md-2, .row .col-md-3, .row .col-md-4, .row .col-md-6 {
    flex: 0 0 100%;
    max-width: 100%;
  }

  /* Form cards → moins de padding */
  .form-card { padding: 16px !important; }

  /* Boutons lisibles */
  .btn-fh { font-size: .8rem !important; padding: 8px 14px !important; }

  /* Notifications dropdown → pleine largeur */
  #notif-dropdown { width: calc(100vw - 32px) !important; right: -60px !important; }

  /* Ranking list (top clients) → rester lisible */
  .rp-rank-row { font-size: .7rem !important; }
}

/* ═══════════════════════════════════════════
   MOBILE — 600px
═══════════════════════════════════════════ */
@media(max-width: 600px) {
  /* Grids inline 3 colonnes → 1 colonne */
  [style*="grid-template-columns:repeat(3,1fr)"],
  [style*="grid-template-columns: repeat(3,1fr)"],
  [style*="grid-template-columns:repeat(3, 1fr)"] {
    grid-template-columns: 1fr !important;
  }

  /* Grids 4-5 → 1 colonne (plus 2) */
  [style*="grid-template-columns:repeat(4,1fr)"],
  [style*="grid-template-columns: repeat(4,1fr)"],
  [style*="grid-template-columns:repeat(4, 1fr)"],
  [style*="grid-template-columns:repeat(5,1fr)"],
  [style*="grid-template-columns: repeat(5,1fr)"] {
    grid-template-columns: 1fr 1fr !important;
  }

  /* Dispatch line : grid 12 colonnes → carte empilée */
  .dispatch-line-row {
    display: block !important;
    grid-template-columns: none !important;
    background: #fff;
    border: 1px solid #e5e5e5;
    border-radius: 10px;
    padding: 12px !important;
    margin-bottom: 8px;
  }
  .dispatch-line-row > * {
    display: block;
    width: 100% !important;
    margin-bottom: 6px;
  }
  .dispatch-line-row input,
  .dispatch-line-row select {
    width: 100% !important;
    font-size: .9rem !important;
    padding: 8px !important;
  }

  /* Order item chips → wrap propre, taille lisible */
  .order-item-chip,
  [class*="chip-"] {
    font-size: .72rem !important;
    padding: 3px 8px !important;
  }

  /* KPI cards un peu plus grosses sur mobile */
  .rp-kpi-val, .kpi-val { font-size: 1.2rem !important; }
  .rp-kpi-label { font-size: .62rem !important; }
}

/* ═══════════════════════════════════════════
   SMALL MOBILE — 480px (iPhone SE, iPhone Mini)
═══════════════════════════════════════════ */
@media(max-width: 480px) {
  .cc-grid { grid-template-columns: 1fr !important; }
  .hero-metrics { grid-template-columns: 1fr 1fr !important; }
  h3, h4 { font-size: 1.2rem !important; }
  .content { padding: 12px !important; }

  /* Toutes les grids 4-5 → 1 colonne sur iPhone */
  [style*="grid-template-columns:repeat(4,1fr)"],
  [style*="grid-template-columns: repeat(4,1fr)"],
  [style*="grid-template-columns:repeat(4, 1fr)"],
  [style*="grid-template-columns:repeat(5,1fr)"],
  [style*="grid-template-columns: repeat(5,1fr)"] {
    grid-template-columns: 1fr !important;
  }

  /* Header reporting : filtres + boutons empilés */
  .rp-filter-bar { flex-direction: column !important; }
  .rp-filter-bar > div { width: 100%; }
  .rp-filter-bar select,
  .rp-filter-bar input[type=date] { width: 100% !important; }

  /* Dashboard : stats compactes */
  .form-card .kpi-label { font-size: .6rem !important; }

  /* Devis / orders cards */
  .order-card,
  .devis-card { padding: 10px !important; }

  /* Font de base plus lisible */
  body { font-size: 14px; }

  /* Breadcrumb header compact */
  .breadcrumb { font-size: .55rem !important; }
}

/* ═══════════════════════════════════════════
   ATELIER — adaptations mobile pour vues récentes
   (campaigns/new, campaigns/show, orders/show, orders/grid, devis index)
═══════════════════════════════════════════ */
@media(max-width: 720px) {
  /* Hero Atelier : titre plus contenu, chips wrap */
  .fh-hero-bold { padding: 22px 18px !important; }
  .fh-hero-bold h1 { font-size: 24px !important; line-height: 1.15 !important; }
  .fh-hero-bold p { font-size: 13px !important; }

  /* Order hero — titre split (ref + client) reste lisible */
  .order-hero { padding: 20px 18px !important; }
  .order-hero__title-ref,
  .order-hero__title-client { font-size: 19px !important; }
  .order-hero__title-sep { font-size: 17px !important; }
  .order-hero__meta {
    gap: 8px !important;
    font-size: 11px !important;
  }
  .order-hero__btn { padding: 9px 14px !important; font-size: 12.5px !important; }

  /* État de la commande — empile label + boutons */
  .ostat-card { padding: 14px 16px !important; gap: 10px !important; }
  .ostat-label { width: 100% !important; }
  .ostat-btn { width: 100% !important; justify-content: center !important; }

  /* Order grid sticky bar : padding réduit, fix width */
  .og-sticky-bar { width: calc(100% - 16px) !important; padding: 10px 14px !important; gap: 12px !important; }
  .og-sticky-bar .info .total { font-size: 17px !important; }
  .og-sticky-bar .actions { flex-wrap: wrap !important; gap: 6px !important; }
  .og-sticky-bar button,
  .og-sticky-bar .btn-ghost { padding: 9px 14px !important; font-size: 12px !important; flex: 1 1 auto; }

  /* Order grid toolbar — wrap propre */
  .og-toolbar { gap: 6px !important; padding: 10px !important; }
  .og-search { font-size: 13px !important; padding: 8px 10px !important; }

  /* Order grid table — scroll horizontal forcé, lignes denses */
  .og-table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .og-table { min-width: 760px; }
  .og-table thead th, .og-table tbody td { padding: 8px 8px !important; font-size: 12px !important; }
  .og-input { width: 64px !important; padding: 8px 6px !important; font-size: 13px !important; }
  .og-spec-input { font-size: 11px !important; padding: 6px 8px !important; }

  /* Campaigns grid (matrice clients × produits) — scroll horizontal */
  .grid-scroll-wrap { max-height: 70vh; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .fh-grid-table { font-size: 11px !important; }
  .grid-cell-input { width: 50px !important; padding: 6px 4px !important; font-size: 13px !important; min-height: 36px !important; }

  /* Campaigns grid filtres barre haut — wrap propre + selects pleine largeur */
  #grid-view-grid > div:first-child { padding: 10px 12px !important; gap: 6px !important }
  #grid-focus-client, #grid-focus-brand, #grid-focus-product {
    min-width: 0 !important; max-width: 100% !important; flex: 1 1 130px;
  }

  /* Devis index — table pure : scroll horizontal */
  .dvi-table { min-width: 720px; }
  .dvi-toolbar { padding: 12px 14px !important; }
  .dvi-tabs { padding: 3px !important; }
  .dvi-tab { padding: 7px 10px !important; font-size: 12px !important; }

  /* Order index hub clusters — passer en 1 col plutôt que 3 */
  .hub-clusters { grid-template-columns: 1fr !important }

  /* Forms admin — sticky footer plus contenu */
  .ec-sticky, .dn-sticky, .nc-sticky { padding: 12px 16px !important; gap: 8px !important }
  .ec-actions, .dn-actions { width: 100%; justify-content: stretch; }
  .ec-btn, .dn-btn { flex: 1 1 auto !important; justify-content: center !important }

  /* Breadcrumbs : taille lisible */
  .fh-breadcrumbs { font-size: 11px !important; gap: 4px !important; }

  /* Stepper campagne — passe en colonne mobile (déjà fait), s'assure padding ok */
  .camp-workflow { padding: 16px 14px !important }

  /* Boutons dans une ligne CRUD (Voir/Modifier/Supprimer campagne) */
  .form-card span[style*="border-radius:8px;overflow:hidden"] {
    flex-wrap: wrap !important;
  }
}

/* ═══════════════════════════════════════════
   FIX GLOBAL : boutons qui débordent (Salim 2026-05-06)
   Pattern récurrent : display:flex sans flex-wrap sur les containers
   d'actions (forms import, toolbars, headers cards). On force le wrap
   sur les patterns connus.
═══════════════════════════════════════════ */
@media(max-width: 720px) {
  /* Forms d'import (file input + bouton Importer) sur admin/users + dispatches */
  form[action*="import"] {
    flex-wrap: wrap !important;
  }
  form[action*="import"] input[type=file] {
    flex: 1 1 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
  }
  form[action*="import"] button {
    flex: 1 1 auto !important;
  }

  /* Toolbar pages /admin/users/clients + fournisseurs + admin/users/show
     (boutons Export/Import/etc) — forcer le wrap quand c'est inline-style */
  div[style*="display:flex"][style*="gap:8px"]:not([class]),
  div[style*="display:flex"][style*="gap:6px"]:not([class]) {
    flex-wrap: wrap !important;
  }

  /* btn-fh* responsive : ne JAMAIS dépasser le parent */
  .btn-fh, .fh-btn-v1, .fh-btn-sm-v1 {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* Cards .form-card avec headers flex (Title + Actions) — force wrap */
  .form-card > div[style*="justify-content:space-between"]:first-child,
  .form-card > div[style*="justify-content: space-between"]:first-child {
    flex-wrap: wrap !important;
    gap: 8px !important;
  }

  /* Tables avec actions cell : force nowrap sur les pills/boutons à
     l'intérieur des td pour éviter écrasement */
  td a.btn-fh,
  td a.fh-btn-v1,
  td button.btn-fh,
  td button.fh-btn-v1 {
    white-space: nowrap;
    padding: 6px 10px !important;
    font-size: 11.5px !important;
  }
}

/* ═══════════════════════════════════════════
   PAGES /admin/users (clients/fournisseurs/admins) — tables → cards mobile
   Salim 2026-05-06 : "View mobile non adaptée, impossible de voir le contenu"
═══════════════════════════════════════════ */
@media(max-width: 720px) {
  /* Wrappers : permettre le scroll horizontal natif si la table garde sa
     forme tabulaire (fallback si la trans card échoue). */
  .cl-table-wrap, .sp-table-wrap, .us-table-wrap, .table-card {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Salim 2026-05-11 fix : transformation table → card propre, tout en
     blocs empilés. Plus de grid 2-col qui causait des overlap badge/email. */
  .cl-table, .sp-table, .us-table {
    display: block;
    min-width: 0 !important;
    background: transparent !important;
  }
  .cl-table colgroup, .sp-table colgroup, .us-table colgroup,
  .cl-table thead,    .sp-table thead,    .us-table thead { display: none }
  .cl-table tbody, .sp-table tbody, .us-table tbody { display: block }
  .cl-table tbody tr, .sp-table tbody tr, .us-table tbody tr {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 14px 16px;
    margin-bottom: 10px;
    background: var(--bg-elev);
    border: 1px solid var(--border);
    border-radius: var(--r);
    box-shadow: var(--shadow-xs);
  }
  .cl-table tbody td, .sp-table tbody td, .us-table tbody td {
    display: block;
    padding: 0 !important;
    border: 0 !important;
    font-size: 13px;
    width: 100%;
    min-width: 0;
  }
  /* Cellules cachées sur table desktop redeviennent visibles en card mobile */
  .cl-table tbody td.cl-hide-md,
  .sp-table tbody td.sp-hide-md,
  .us-table tbody td.us-hide-md { display: block !important }

  /* Salim 2026-05-11 : étend la transformation table → card aux autres
     tables admin (devis, supplier_products edit, etc.). */
  .dvi-table {
    display: block;
    min-width: 0 !important;
    background: transparent !important;
  }
  .dvi-table thead { display: none }
  .dvi-table tbody { display: block }
  .dvi-table tbody tr {
    display: flex !important;
    flex-direction: column;
    gap: 6px;
    padding: 14px 16px;
    margin-bottom: 10px;
    background: var(--bg-elev);
    border: 1px solid var(--border);
    border-radius: var(--r);
    box-shadow: var(--shadow-xs);
  }
  .dvi-table tbody td {
    display: block;
    padding: 0 !important;
    border: 0 !important;
    font-size: 13px;
    width: 100%;
  }
  .dvi-table tbody td:last-child {
    padding-top: 8px !important;
    border-top: 1px solid var(--border) !important;
    margin-top: 4px;
  }

  /* Salim 2026-05-11 : générique pour les tables Bootstrap dans .table-card
     (brands, supplier_products, purchase_orders, pricing, etc.). Sur petit
     écran on transforme en cards stackées, sinon on garde scroll-x natif. */
  .table-card > .table,
  .table-card .table {
    display: block !important;
    min-width: 0 !important;
    width: 100%;
    background: transparent !important;
  }
  .table-card .table thead { display: none }
  .table-card .table tbody { display: block }
  .table-card .table tbody tr {
    display: flex !important;
    flex-direction: column;
    gap: 6px;
    padding: 14px 16px !important;
    margin-bottom: 10px;
    background: var(--bg-elev, #fff);
    border: 1px solid var(--border, #e5e5e5);
    border-radius: var(--r, 10px);
    box-shadow: var(--shadow-xs, 0 1px 3px rgba(0,0,0,.04));
  }
  .table-card .table tbody td {
    display: block;
    padding: 0 !important;
    border: 0 !important;
    font-size: 13px;
    width: 100%;
  }
  /* Première td (titre/produit) : sépare le header */
  .table-card .table tbody td:first-child {
    padding-bottom: 6px !important;
    border-bottom: 1px solid var(--border, #f0f0f0) !important;
    font-weight: 700;
  }
  /* Dernière td (actions) : sépare le footer */
  .table-card .table tbody td:last-child {
    padding-top: 8px !important;
    border-top: 1px solid var(--border, #f0f0f0) !important;
    margin-top: 4px;
  }
  /* Cellule actions : flex avec wrap */
  .table-card .table tbody td:last-child > div,
  .table-card .table tbody td .d-flex {
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 8px !important;
    justify-content: flex-start;
  }
  /* Boutons d'action min 36px tactile */
  .table-card .table tbody td button,
  .table-card .table tbody td a.btn-fh,
  .table-card .table tbody td a.fh-btn-v1 {
    min-height: 36px;
    padding: 7px 12px !important;
    font-size: 12px !important;
  }

  /* /admin/imports + /orders (ofo-table) — même transformation */
  .imp2-table,
  .ofo-table {
    display: block !important;
    width: 100% !important;
  }
  .imp2-table thead,
  .ofo-table thead { display: none }
  .imp2-table tbody,
  .ofo-table tbody { display: block }
  .imp2-table tbody tr,
  .ofo-table tbody tr {
    display: flex !important;
    flex-direction: column;
    gap: 6px;
    padding: 14px 16px !important;
    margin-bottom: 10px;
    background: #fff;
    border: 1px solid var(--border, #e5e5e5);
    border-radius: 10px;
  }
  .imp2-table tbody td,
  .ofo-table tbody td {
    display: block;
    padding: 0 !important;
    border: 0 !important;
    font-size: 13px;
    width: 100%;
    text-align: left !important;
  }
  .ofo-table tbody td.num { text-align: left !important }

  /* Salim 2026-05-11 : tous les inputs Bootstrap .form-control-sm sont trop
     petits pour mobile (≈32px). On force 40px min sur mobile partout. */
  .form-control-sm,
  input.form-control,
  select.form-control,
  textarea.form-control {
    min-height: 40px !important;
    font-size: 14px !important;
    padding: 8px 12px !important;
  }
  textarea.form-control { min-height: 80px !important }
  /* Selects natifs : éviter le zoom iOS sous 16px sur les selects pleins
     écran. On laisse les selects compacts (us-role-form, asg-filter) tels
     quels — c'est OK car ils sont petits chips. */
  select.form-control,
  select.form-select,
  .invf-field select,
  form select:not(.us-role-form select):not(.asg-aff-chip):not([class*="chip"]) {
    font-size: 16px !important;
  }
  /* Inputs type number/text dans tables avec onchange auto-submit */
  table input[type="number"],
  table input[type="text"]:not(.fh-input):not(.og-input) {
    font-size: 14px !important;
    min-height: 36px !important;
  }

  /* Header card (nom + avatar) : padding-bottom + séparateur */
  .cl-table tbody td:nth-child(1),
  .sp-table tbody td:nth-child(1),
  .us-table tbody td:nth-child(1) {
    padding-bottom: 8px !important;
    border-bottom: 1px solid var(--border) !important;
  }
  /* Footer card (actions) : padding-top + séparateur */
  .cl-table tbody td:last-child,
  .sp-table tbody td:last-child,
  .us-table tbody td:last-child {
    padding-top: 8px !important;
    border-top: 1px solid var(--border) !important;
    margin-top: 2px;
  }

  /* Actions row : flex wrap propre, espacement large */
  .cl-table .actions-cell, .sp-table .actions-cell, .us-table .actions-cell,
  .cl-table tbody td:last-child > div,
  .sp-table tbody td:last-child > div,
  .us-table tbody td:last-child > div,
  .us-actions {
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 8px !important;
    justify-content: flex-start !important;
    align-items: center !important;
    white-space: normal !important;
  }
  /* Boutons icônes : taille tactile minimum 36px */
  .us-ico-btn {
    width: 36px !important;
    height: 36px !important;
    font-size: 14px !important;
  }
  /* Form rôle : limite la largeur pour ne pas pousser les autres icons */
  .us-role-form {
    flex: 0 1 auto !important;
    max-width: 180px;
    min-width: 0;
  }
  .us-role-form select {
    max-width: 140px !important;
    height: 36px !important;
    font-size: 12px !important;
    padding: 4px 26px 4px 10px !important;
  }
  /* Autres boutons dans la cellule actions */
  .cl-table tbody td:last-child a:not(.us-ico-btn),
  .cl-table tbody td:last-child button:not(.us-ico-btn),
  .sp-table tbody td:last-child a:not(.us-ico-btn),
  .sp-table tbody td:last-child button:not(.us-ico-btn),
  .us-table tbody td:last-child a:not(.us-ico-btn),
  .us-table tbody td:last-child button:not(.us-ico-btn) {
    font-size: 12px !important;
    padding: 8px 12px !important;
    white-space: nowrap;
  }
  /* Cellule contenant le pill Statut : la pill en flex inline pour ne pas
     overlap avec la cellule email/société grâce à display:block sur le td */
  .us-table tbody .us-pill {
    display: inline-block;
    margin: 0;
  }

  /* Hero compact aussi sur ces pages */
  .fh-hero-bold { padding: 18px 18px 16px !important }
  .fh-hero-bold h1 { font-size: 22px !important; line-height: 1.15 !important }
}

/* iPhone SE / Mini — encore plus serré */
@media(max-width: 414px) {
  .fh-hero-bold h1 { font-size: 21px !important; }
  .fh-hero-bold p { font-size: 12.5px !important; }
  .order-hero__title-ref,
  .order-hero__title-client { font-size: 17px !important; }
  .order-hero__btn { padding: 8px 12px !important; font-size: 12px !important; }

  /* Sticky bar order grid : info + boutons en column */
  .og-sticky-bar { flex-direction: column !important; align-items: stretch !important; }
  .og-sticky-bar .actions { justify-content: space-between !important }
}

/* ═══════════════════════════════════════════
   TOUCH TARGETS — Tap area minimale 44x44px
═══════════════════════════════════════════ */
@media(pointer: coarse) {
  .btn-fh, .btn-fh-sm, button, a.btn, input[type=submit] {
    min-height: 40px;
  }
  select, input[type=text], input[type=email], input[type=number], input[type=date] {
    min-height: 40px;
    font-size: 16px !important; /* iOS n'agrandit pas le zoom si ≥16px */
  }
}
