/* mobile.css — Couche UX mobile native (Improved Agency v4)
   Mobile-first : safe areas, touch targets 44px+, typographie anti-zoom iOS */

:root {
  --nav-height: calc(72px + env(safe-area-inset-bottom, 0px));
  --header-height: 56px;
  --touch-min: 44px;
  --page-x: clamp(14px, 4vw, 20px);
  --sheet-radius: 24px;
  --tabbar-h: calc(72px + env(safe-area-inset-bottom, 0px));
}

/* ─── Page shell (wrapper standard des écrans) ─────────────── */
.page-shell {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-height: 100dvh;
  background: var(--bg);
  overflow: hidden;
}

.page-header {
  flex-shrink: 0;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: max(12px, env(safe-area-inset-top, 0px)) var(--page-x) 12px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border);
  z-index: 20;
}

.page-header--sticky {
  position: sticky;
  top: 0;
}

.page-header__main {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  min-width: 0;
  flex: 1;
}

.page-header__text { min-width: 0; flex: 1; }

.page-header__eyebrow {
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--brand);
  margin-bottom: 2px;
}

.page-header__title {
  font-size: clamp(24px, 6vw, 30px);
  font-family: var(--font-heading);
  font-weight: var(--weight-bold);
  letter-spacing: -0.035em;
  line-height: var(--line-heading);
  color: var(--text);
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.page-header__subtitle {
  font-size: 12px;
  color: var(--text-3);
  margin-top: 4px;
  line-height: 1.4;
}

.page-header__actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.page-back {
  width: var(--touch-min);
  height: var(--touch-min);
  min-width: var(--touch-min);
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--bg-2);
  color: var(--text-2);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: transform 0.15s ease, background 0.15s ease;
}
.page-back:active { transform: scale(0.92); background: var(--bg-3); }

.page-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  padding: 0 var(--page-x) var(--nav-height);
}

.page-body--flush { padding-left: 0; padding-right: 0; }

/* Barre d'actions collante au-dessus de la nav */
.mobile-action-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: var(--nav-height);
  z-index: 90;
  display: flex;
  gap: 10px;
  padding: 10px var(--page-x);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  border-top: 1px solid var(--border);
}
.mobile-action-bar .btn { flex: 1; min-height: var(--touch-min); }

/* ─── Touch & inputs natifs ────────────────────────────────── */
@media (max-width: 899px) {
  .form-input,
  .search-bar input,
  select.form-input,
  textarea.form-input {
    font-size: 16px !important; /* évite le zoom auto iOS */
    min-height: var(--touch-min);
    border-radius: 14px;
  }

  textarea.form-input { min-height: 96px; }

  .btn {
    min-height: var(--touch-min);
    padding-top: 12px;
    padding-bottom: 12px;
  }

  .btn-sm {
    min-height: 44px;
    padding-top: 8px;
    padding-bottom: 8px;
  }

  .icon-btn {
    width: var(--touch-min);
    height: var(--touch-min);
  }

  /* Désactive les effets hover sur écran tactile */
  @media (hover: none) {
    .card-interactive:hover,
    .pipeline-card:hover,
    .fab-menu-item:hover {
      transform: none;
      box-shadow: inherit;
    }
  }

  .card-interactive:active,
  .pipeline-card:active {
    transform: scale(0.98);
    opacity: 0.92;
  }

  /* Topbar legacy → compacte */
  .topbar {
    padding: max(10px, env(safe-area-inset-top, 0px)) var(--page-x) 10px;
    gap: 10px;
  }

  .topbar h1 {
    font-size: clamp(18px, 5vw, 22px) !important;
  }

  /* Scroll global avec espace nav */
  body:has(.bottom-nav.visible) .scroll-zone {
    padding-bottom: calc(var(--nav-height) + 12px) !important;
  }

  body:has(.bottom-nav.visible) #app {
    padding-bottom: 0;
  }

  /* Toast au-dessus de la barre */
  .toast {
    bottom: calc(var(--nav-height) + 12px) !important;
    left: var(--page-x) !important;
    right: var(--page-x) !important;
    width: auto !important;
    max-width: none !important;
    /* Étiré entre les marges : sans le translateX(-50%) du bureau, sinon il sort de l'écran à gauche. */
    transform: translateY(20px) !important;
  }
  .toast.toast-visible { transform: translateY(0) !important; }

  /* Sabi au-dessus de la nav */
  #sabi-command-bubble {
    bottom: calc(var(--nav-height) + 12px) !important;
    right: var(--page-x) !important;
  }

  #sabi-command-panel {
    bottom: calc(var(--nav-height) + 72px) !important;
    right: var(--page-x) !important;
    left: var(--page-x) !important;
    width: auto !important;
    max-width: none !important;
  }

  /* Sheets pleine largeur mobile — au-dessus de la bottom nav (z-index 1000) */
  .sheet-overlay {
    align-items: flex-end;
    z-index: 1200;
  }
  .sheet {
    max-height: min(96dvh, calc(100dvh - env(safe-area-inset-top, 0px) - 4px));
    width: 100%;
    max-width: none;
    border-radius: var(--sheet-radius) var(--sheet-radius) 0 0;
  }
  .sheet-handle {
    width: 48px;
    height: 5px;
    margin: 10px auto 6px;
  }
  .sheet-body {
    padding: 8px var(--page-x) calc(24px + env(safe-area-inset-bottom, 0px));
  }

  body.sheet-open .bottom-nav {
    pointer-events: none;
  }
  body.sheet-open .sheet-overlay,
  body.sheet-open .sheet-body {
    touch-action: pan-y;
  }

  /* Tab bar scrollable sur petit écran */
  .tab-bar {
    overflow-x: auto;
    flex-wrap: nowrap;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .tab-bar::-webkit-scrollbar { display: none; }
  .tab-btn { flex: 0 0 auto; min-width: max-content; padding: 10px 14px; }

  /* Grilles KPI une colonne si très étroit */
  .kpi-grid { grid-template-columns: 1fr 1fr; gap: 10px; }

  /* Menu mobile grid */
  .mobile-menu-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    padding: 0 var(--page-x);
  }

  .mobile-menu-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 92px;
    padding: 12px 8px;
    background: var(--bg-2);
    border: 1px solid var(--border);
    border-radius: 18px;
    cursor: pointer;
    font-family: inherit;
    transition: transform 0.15s ease;
  }
  .mobile-menu-item:active { transform: scale(0.96); }

  .mobile-menu-section {
    font-size: 10px;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-4);
    padding: 16px var(--page-x) 8px;
  }

  /* Confirm sheet : boutons empilés sur très petit écran */
  .confirm-actions {
    display: flex;
    flex-direction: column-reverse;
    gap: 10px;
  }
  .confirm-actions .btn { width: 100%; min-height: var(--touch-min); }

  /* Kanban horizontal fluide */
  .kanban-scroll {
    display: flex;
    gap: 12px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scroll-snap-type: x proximity;
    padding: 0 var(--page-x) 8px;
    margin: 0 calc(var(--page-x) * -1);
  }
  .kanban-scroll > * {
    scroll-snap-align: start;
    flex: 0 0 min(85vw, 320px);
  }

  /* Chips horizontaux */
  .chip-scroll {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding: 2px var(--page-x) 10px;
    scrollbar-width: none;
  }
  .chip-scroll::-webkit-scrollbar { display: none; }

  /* Toggle actif dans les en-têtes */
  .icon-btn--active {
    background: var(--brand) !important;
    border-color: var(--brand) !important;
    color: #fff !important;
  }

  /* Grilles listes → une colonne sur mobile */
  #clients-list,
  #projects-list {
    grid-template-columns: 1fr !important;
  }

  /* Dashboard : actions header compactes */
  .page-header__actions--compact .icon-btn {
    width: 40px;
    height: 40px;
    min-width: 40px;
  }

  /* Meeting stats : 2 colonnes au lieu de 3 */
  #meetings-stats {
    grid-template-columns: 1fr 1fr !important;
  }

  /* Gateway : pills scrollables */
  .gw-pills {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding: 8px var(--page-x) 4px;
    scrollbar-width: none;
  }
  .gw-pills::-webkit-scrollbar { display: none; }

  /* WhatsApp / CRM : layout empilé */
  .cs-layout {
    flex-direction: column !important;
    height: 100dvh;
  }
  .cs-layout .cs-sidebar {
    width: 100% !important;
    max-height: 45dvh;
    border-right: none !important;
    border-bottom: 1px solid var(--border);
  }
  .cs-layout .cs-chat {
    flex: 1 !important;
    min-height: 0;
  }
  .cs-layout.cs-chat-open .cs-sidebar { display: none; }
  .cs-layout.cs-chat-open .cs-chat { max-height: none; }

  /* KPI pipeline leads */
  .leads-kpi-scroll {
    display: flex;
    gap: 12px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding: 12px var(--page-x) 0;
    scrollbar-width: none;
  }
  .leads-kpi-scroll::-webkit-scrollbar { display: none; }
  .leads-kpi-scroll > * { flex: 0 0 min(42vw, 160px); }

  /* Chat CRM : padding mobile */
  #chat-messages { padding: 16px var(--page-x) !important; }
  #chat-input-area { padding: 12px var(--page-x) calc(12px + env(safe-area-inset-bottom, 0px)) !important; }
  #wa-quick-replies { padding: 0 var(--page-x) !important; }
  #cs-back-list { display: none !important; }
  .cs-layout.cs-chat-open #cs-back-list { display: inline-flex !important; }

  /* WhatsApp Gateway — layout mobile (complété dans gateway.css) */
  .gw-messenger {
    flex-direction: column !important;
    margin: 0 !important;
    border-radius: 0 !important;
    border-left: none !important;
    border-right: none !important;
  }
  .gw-sidebar {
    width: 100% !important;
    max-width: none !important;
    min-width: 0 !important;
    max-height: none !important;
    flex: 1;
    border-right: none !important;
  }
  .gw-messenger.gw-chat-open .gw-sidebar { display: none; }
  .gw-messenger.gw-chat-open .gw-chat-back { display: flex !important; }

  /* Footers fixes au-dessus de la bottom nav */
  body:has(.bottom-nav.visible) .form-footer {
    bottom: var(--nav-height) !important;
  }

  /* Grilles formulaire profil lead → 1 colonne */
  .form-grid { grid-template-columns: 1fr !important; }

  /* Onglets profil scrollables */
  .profile-tabs {
    display: flex;
    gap: 6px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    border-bottom: 1px solid var(--border);
    padding-bottom: 8px;
    margin-bottom: 16px;
  }
  .profile-tabs::-webkit-scrollbar { display: none; }
  .profile-tabs .tab { flex: 0 0 auto; white-space: nowrap; }

  /* Actions rapides profil */
  .profile-actions {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 4px;
    scrollbar-width: none;
  }
  .profile-actions::-webkit-scrollbar { display: none; }
  .profile-actions .btn { flex: 0 0 auto; white-space: nowrap; }

  /* Prévention anti-zoom iOS Safari sur les champs de saisie */
  input:not([type="checkbox"]):not([type="radio"]),
  select,
  textarea {
    font-size: 16px !important;
  }

  /* Tap highlight désactivé pour sensation 100% native */
  * {
    -webkit-tap-highlight-color: transparent;
  }
}

/* ── Desktop (PC) : Écran large & Expérience Premium ── */
@media (min-width: 900px) {
  :root {
    --page-x: 32px;
  }
  .page-header {
    padding: 18px 32px;
  }
  .page-body { 
    padding: 24px 32px 48px;
    max-width: 100%;
    width: 100%;
    margin-left: 0;
    margin-right: 0;
  }
  .mobile-action-bar { display: none; }
  .confirm-actions { flex-direction: row; }
  .camp-action-bar { bottom: 0 !important; }
  body:has(.bottom-nav.visible) .form-footer { bottom: 0 !important; }

  /* Barres de défilement discrètes et élégantes sur PC */
  ::-webkit-scrollbar {
    width: 7px;
    height: 7px;
  }
  ::-webkit-scrollbar-track {
    background: transparent;
  }
  ::-webkit-scrollbar-thumb {
    background: rgba(100, 116, 139, 0.2);
    border-radius: 999px;
    transition: background 0.2s ease;
  }
  [data-theme="dark"] ::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.12);
  }
  ::-webkit-scrollbar-thumb:hover {
    background: var(--brand);
  }

  /* Micro-animations au survol sur PC */
  @media (hover: hover) {
    .card, .v5-card, .dash-pulse-card {
      transition: transform 0.2s var(--ease-out), box-shadow 0.2s var(--ease-out), border-color 0.2s ease;
    }
    .card:hover, .v5-card:hover {
      border-color: color-mix(in srgb, var(--brand) 30%, var(--border));
    }
    .btn:hover {
      transform: translateY(-1px);
    }
    .btn:active {
      transform: translateY(1px);
    }
  }

  :focus-visible {
    outline-width: 3px;
  }
}


/* === OPTIMISATIONS PWA IOS, TABLETTES & PC === */
html, body {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior-y: contain;
}

/* Prise en compte du Dynamic Island, Notch et barre Home iOS */
@supports (padding-top: env(safe-area-inset-top)) {
  body {
    padding-top: env(safe-area-inset-top);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }
  #bottom-nav {
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }
  .chat-input-bar, .chat-input-row {
    padding-bottom: max(10px, env(safe-area-inset-bottom));
  }
  .app-header {
    padding-top: max(8px, env(safe-area-inset-top));
  }
}

/* Mode Standalone (PWA installée sur écran d'accueil iOS / Android / Desktop) */
@media all and (display-mode: standalone) {
  body {
    user-select: none;
    -webkit-user-select: none;
  }
  input, textarea, [contenteditable="true"] {
    user-select: text;
    -webkit-user-select: text;
  }
}

/* Optimisation pour Tablettes et iPads (768px à 1180px) */
@media (min-width: 768px) and (max-width: 1180px) {
  .chat-sidebar {
    width: 280px !important;
    min-width: 280px !important;
  }
  .chat-screen {
    grid-template-columns: 280px 1fr !important;
  }
  .leads-pipeline {
    grid-auto-columns: minmax(240px, 1fr) !important;
  }
  .dashboard-grid {
    grid-template-columns: repeat(2, 1fr) !important;
  }
  .billing-stats-grid {
    grid-template-columns: repeat(2, 1fr) !important;
  }
}

/* Défilement fluide sans saccades */
* {
  -webkit-overflow-scrolling: touch;
}


/* ─── Modales et Dialogues Mobiles Résilients (iOS / Android) ──────── */
@media (max-width: 899px) {
  .modal-bg {
    align-items: flex-end !important;
    padding: 0 !important;
    overscroll-behavior: contain !important;
    backdrop-filter: blur(8px) !important;
    -webkit-backdrop-filter: blur(8px) !important;
    z-index: 2200 !important;
  }

  .modal {
    width: 100% !important;
    max-width: 100% !important;
    border-radius: 24px 24px 0 0 !important;
    border-bottom: none !important;
    max-height: min(92dvh, calc(100dvh - env(safe-area-inset-top, 0px) - 16px)) !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    overscroll-behavior: contain !important;
    padding: 18px var(--page-x, 16px) calc(28px + env(safe-area-inset-bottom, 16px)) !important;
    margin: 0 !important;
    box-sizing: border-box !important;
    box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.45) !important;
    position: relative !important;
    animation: sheetSlideUp 0.24s cubic-bezier(0.16, 1, 0.3, 1) !important;
  }

  /* Indicateur visuel tactile en haut de modale */
  .modal::before {
    content: '';
    display: block;
    width: 44px;
    height: 5px;
    border-radius: 999px;
    background: var(--border-2, rgba(255, 255, 255, 0.2));
    margin: -6px auto 14px auto;
  }

  /* Barre d'action / boutons collante en bas de la modale */
  .modal .actions,
  .modal-actions,
  .modal-footer,
  .modal > div[style*="justify-content: flex-end"],
  .modal > div[style*="justify-content: space-between"] {
    position: sticky !important;
    bottom: 0 !important;
    background: color-mix(in srgb, var(--bg-2) 95%, transparent) !important;
    backdrop-filter: blur(12px) !important;
    -webkit-backdrop-filter: blur(12px) !important;
    padding: 12px 0 4px 0 !important;
    margin-top: 14px !important;
    border-top: 1px solid var(--border, rgba(255, 255, 255, 0.1)) !important;
    z-index: 15 !important;
    display: flex !important;
    gap: 10px !important;
  }

  .modal .actions .btn,
  .modal-actions .btn,
  .modal-footer .btn {
    flex: 1 !important;
    min-height: 46px !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
  }

  @keyframes sheetSlideUp {
    from { transform: translateY(100%); opacity: 0.5; }
    to { transform: translateY(0); opacity: 1; }
  }
}

