/* ============================================================================
 * shared/mobile.css — Adaptations mobile + iOS standalone webapp
 * ----------------------------------------------------------------------------
 * Audit responsive 20/05/2026. Ce fichier contient les règles communes aux
 * 17 pages pour rendre l'app utilisable en <768px et prête à être wrappée
 * en webapp iOS/Android (Capacitor, PWA install).
 *
 * À CHARGER APRÈS shared/components.css + shared/layout.css.
 *
 * Sommaire :
 *   1. Variables globales viewport mobile + safe-area iOS
 *   2. Layout root : .app responsive (sidebar masquée <768px)
 *   3. Sidebar → bottom-nav mobile (cf. shared/sidebar.js qui rend ce markup)
 *   4. Padding global content adapté mobile
 *   5. Touch targets ≥44×44 sur boutons icon-seul
 *   6. Hover-only wrappés dans @media (hover: hover) pour éviter sticky-hover
 *   7. 100dvh fallback pour iOS Safari (100vh inclut la barre URL)
 *   8. Toast bottom + safe-area
 *   9. Print mode (factures) reset
 * ============================================================================ */

/* === 1. Variables ========================================================== */
:root {
  /* Safe-area iOS : notch top + home indicator bottom (env() = 0 sur non-iOS) */
  --safe-top:    env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left:   env(safe-area-inset-left, 0px);
  --safe-right:  env(safe-area-inset-right, 0px);

  /* Hauteurs UI mobile */
  --mobile-topbar-h: calc(56px + var(--safe-top));
  /* Anciennement bottom-nav 4-icons (supprimée 20/05/2026). Conservé pour
     back-compat des règles `.toast`, `.page`, etc. qui référencent cette
     variable. Nouvelle valeur = clearance pour le burger FAB rond
     (56px) + 16px marge + safe-bottom. */
  --mobile-bottomnav-h: calc(72px + var(--safe-bottom));
  /* Clearance dédiée au FAB pour les pages qui veulent une valeur sémantique */
  --mobile-fab-clearance: calc(72px + var(--safe-bottom));
}

/* === 2. Layout root ======================================================== */
@media (max-width: 767px) {
  .app {
    grid-template-columns: 1fr !important;
    /* Refonte 20/05/2026 : on ne reserve PLUS de padding-bottom pour la
       bottom-nav (supprimée). Le burger FAB rond est positionné en
       fixed bottom-right et n'empiète pas sur la grid root. Les pages
       individuelles ajoutent leur propre clearance via .page (cf. §5).
       Exception : la page conversation (espace-patient.html) qui n'a pas
       de FAB → pas besoin de clearance non plus, et son layout
       100dvh + flex column suffit. */
  }
  .side {
    display: none !important;
  }
  /* Idem pour les alias buggés .aside (corrigés par ailleurs mais belt+braces) */
  .aside { display: none !important; }
}

/* === Garde-fou DESKTOP : force display:none sur les éléments mobile-only.
   Sans ce garde-fou, si sidebar.js insère les éléments avant que mobile.css
   soit appliqué (cache navigateur, ordre des CSS, etc.), ils peuvent flasher
   ou rester visibles en desktop. */
@media (min-width: 768px) {
  .mobile-top-bar,
  .mobile-bottom-nav,
  .mobile-burger-fab,
  .mobile-drawer {
    display: none !important;
  }
}

/* === Burger FAB mobile (bouton flottant rond en bas-à-droite) ============== */
/* Refonte 20/05/2026 : remplace la bottom-nav 4-icons. Rendu par
   shared/sidebar.js#renderMobileBurgerFab(). Position fixed bottom-right
   avec safe-area pour iPhone home-indicator. NE PAS afficher sur la page
   conversation (sidebar.js gère ça via opts.hideBurger=true). */
.mobile-burger-fab {
  display: none;
}
@media (max-width: 767px) {
  .mobile-burger-fab {
    display: flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    /* Décalage vertical : 16px en bas par défaut, MAIS si la page a un
       composer fixed (demarrer.html), on le remonte au-dessus.
       La règle ciblée .has-composer-fixed est posée par sidebar.js
       quand le composer existe sur la page. */
    bottom: calc(16px + var(--safe-bottom));
    right: calc(16px + var(--safe-right));
    z-index: 100;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--accent, #5B65DE);
    color: #fff;
    border: none;
    cursor: pointer;
    font-family: inherit;
    /* Ombre généreuse pour bien lire le bouton sur fond clair ET sombre */
    box-shadow:
      0 8px 24px -4px rgba(91, 101, 222, 0.45),
      0 4px 8px -2px rgba(15, 20, 50, 0.18);
    transition: transform .15s ease, box-shadow .15s ease;
  }
  .mobile-burger-fab:active {
    transform: scale(0.94);
    box-shadow:
      0 4px 12px -2px rgba(91, 101, 222, 0.35),
      0 2px 4px -1px rgba(15, 20, 50, 0.14);
  }
  .mobile-burger-fab svg {
    stroke: currentColor;
    fill: none;
  }
  /* Si la page contient un composer fixed (demarrer.html) → remonte le
     FAB au-dessus pour éviter le chevauchement. Détection auto via
     l'existence de .composer-wrap dans le DOM (CSS :has() — supporté
     sur tous browsers récents iOS 15.4+). */
  body:has(.composer-wrap) .mobile-burger-fab {
    bottom: calc(110px + var(--safe-bottom));
  }
}

/* === 3. Bottom-nav mobile ================================================== */
/* Rendu par shared/sidebar.js#renderMobileNav() quand window.innerWidth < 768.
   Position fixed bottom avec safe-area pour iPhone home-indicator. */
.mobile-bottom-nav {
  display: none;
  position: fixed;
  bottom: 0; left: 0; right: 0;
  z-index: 100;
  background: var(--surface, #fff);
  border-top: 1px solid var(--line, #E2E4ED);
  padding-bottom: var(--safe-bottom);
  /* iOS standalone : ombrage léger pour distinguer du contenu */
  box-shadow: 0 -2px 10px rgba(19, 32, 85, 0.04);
}
.mobile-bottom-nav-inner {
  display: flex;
  justify-content: space-around;
  align-items: stretch;
  height: 64px;
}
.mobile-bottom-nav a {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 6px 8px;
  min-width: 56px;
  min-height: 44px; /* Touch target Apple HIG */
  text-decoration: none;
  color: var(--muted, #6B7294);
  font-size: 11px;
  font-weight: 500;
  transition: color .15s;
}
.mobile-bottom-nav a.active {
  color: var(--accent, #5B65DE);
}
/* Bouton burger (item terminal de la bottom-nav, sans label texte).
   Aligné verticalement au CENTRE de la barre pour rester cohérent
   visuellement avec les autres items (qui ont icône + label en dessous). */
.mobile-bottom-nav-burger {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 6px 8px;
  min-width: 56px;
  min-height: 44px;
  background: transparent;
  border: none;
  cursor: pointer;
  color: var(--muted, #6B7294);
  font-family: inherit;
}
.mobile-bottom-nav-burger svg {
  display: block;
}
.mobile-bottom-nav a .ico {
  width: 24px; height: 24px;
  display: flex; align-items: center; justify-content: center;
}
.mobile-bottom-nav a .ico svg {
  width: 22px; height: 22px;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
@media (max-width: 767px) {
  .mobile-bottom-nav { display: block; }
}

/* === 4. Top-bar mobile (logo + burger optionnel) =========================== */
/* Rendu par shared/sidebar.js#renderMobileTopBar() quand window.innerWidth < 768.
   Présent sur les pages connectées pour donner le contexte (logo) +
   accès au menu (settings / logout) qui n'a pas sa place en bottom-nav. */
.mobile-top-bar {
  display: none;
  position: sticky;
  top: 0;
  z-index: 90;
  background: var(--surface, #fff);
  border-bottom: 1px solid var(--line, #E2E4ED);
  padding-top: var(--safe-top);
}
.mobile-top-bar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 56px;
  padding: 0 16px;
}
.mobile-top-bar img {
  height: 26px;
}
.mobile-top-bar .menu-btn {
  width: 44px; height: 44px;
  border: none;
  background: transparent;
  color: var(--ink, #132055);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
}
.mobile-top-bar .menu-btn svg {
  width: 22px; height: 22px;
}
/* mobile-top-bar RETIRÉE 20/05/2026 sur demande user. Le bouton burger
   en bas-à-droite de la bottom-nav suffit pour ouvrir le drawer (profil,
   logout, etc.). On laisse les styles ci-dessus pour rétro-compat mais le
   display: block en mobile est désactivé : la top-bar ne sera plus visible
   même si l'ancien code la rendait. */
.mobile-top-bar { display: none !important; }

/* === 4bis. Mobile drawer (panel slide-in depuis la droite) ================ */
/* Refonte 20/05/2026 : drawer riche style maquette canvas design.
   Structure : header pastille + carte "Suivi avec psy" + 3 sections +
   footer logout. Indépendant de la .side desktop cachée. */
.mobile-drawer { display: none; }
.mobile-drawer[hidden] { display: none !important; }
@media (max-width: 767px) {
  .mobile-drawer {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 200;
  }
  .mobile-drawer-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(15, 20, 50, 0.45);
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
    animation: drw-fade .15s ease-out;
  }
  .mobile-drawer-panel {
    position: absolute;
    top: 0; right: 0; bottom: 0;
    width: min(340px, 88vw);
    background: var(--bg, #FAFAFC);
    padding-top: calc(16px + env(safe-area-inset-top, 0px));
    /* padding-bas géré par .mdrawer-footer pour qu'il atteigne le bord (sinon
       bande vide sous "Se déconnecter" = la "bandeau" du bas, 16/06). */
    padding-bottom: 0;
    overflow-y: auto;
    box-shadow: -8px 0 32px rgba(15, 20, 50, 0.18);
    animation: drw-slide .22s cubic-bezier(.2,.9,.3,1);
    display: flex;
    flex-direction: column;
  }
  @keyframes drw-fade { from { opacity: 0; } to { opacity: 1; } }
  @keyframes drw-slide { from { transform: translateX(100%); } to { transform: translateX(0); } }

  /* ─── HEADER : logo cliquable (→ home.html) + bouton close ──────
     Refonte 01/06/2026 : avant on avait pastille avatar + nom serif italic
     + email. Retiré pour un branding plus neutre et un clic rapide vers
     l'accueil depuis le drawer. Le profil reste accessible via "Mon compte"
     en bas du drawer. */
  .mdrawer-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 8px 18px 18px;
  }
  .mdrawer-logo {
    display: inline-flex;
    align-items: center;
    flex: 0 1 auto;
    min-width: 0;
    text-decoration: none;
    /* Touch target accessible : ≥44px de hauteur effective autour du logo */
    padding: 8px 4px;
    margin: -8px -4px;
    border-radius: 8px;
  }
  .mdrawer-logo img {
    height: 24px;
    width: auto;
    display: block;
  }
  .mdrawer-logo:active {
    background: var(--surface-2, #F4F5F9);
  }
  /* Compat avec les anciennes classes (au cas où une page custom du repo
     les utilise encore) — non-affichées dans le nouveau header */
  .mdrawer-avatar, .mdrawer-id, .mdrawer-name, .mdrawer-since {
    display: none;
  }
  .mdrawer-close {
    width: 32px; height: 32px;
    border: none;
    background: var(--surface, #fff);
    border-radius: 50%;
    color: var(--muted, #6B7294);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 32px;
  }
  .mdrawer-close svg { stroke: currentColor; fill: none; stroke-width: 2; }

  /* ─── CARTE "SUIVI AVEC [PSY]" ───────────────────────────────────── */
  .mdrawer-psy-card {
    display: flex;
    align-items: center;
    gap: 10px;
    margin: 0 16px 16px;
    padding: 12px 14px;
    background: var(--surface, #fff);
    border: 1px solid var(--line, #E2E4ED);
    border-radius: 14px;
    text-decoration: none;
    color: inherit;
    transition: background .15s;
  }
  .mdrawer-psy-card:active {
    background: var(--surface-2, #F4F5F9);
  }
  .mdrawer-psy-avatar {
    width: 36px; height: 36px;
    border-radius: 10px;
    background-color: var(--surface-2, #F4F5F9);
    background-size: cover;
    background-position: center;
    color: var(--accent, #5B65DE);
    flex: 0 0 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 13px;
    font-weight: 600;
  }
  .mdrawer-psy-info {
    flex: 1;
    min-width: 0;
  }
  .mdrawer-psy-label {
    font-size: 10.5px;
    font-weight: 600;
    color: var(--muted, #6B7294);
    letter-spacing: 0.04em;
    text-transform: uppercase;
  }
  .mdrawer-psy-name {
    font-size: 13px;
    font-weight: 600;
    color: var(--ink, #132055);
    line-height: 1.2;
    margin-top: 3px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .mdrawer-psy-chevron {
    flex: 0 0 14px;
    color: var(--muted-2, #8B91B0);
    display: flex;
    align-items: center;
  }

  /* ─── SECTIONS ──────────────────────────────────────────────────── */
  .mdrawer-section {
    margin-bottom: 14px;
    padding: 0 12px;
  }
  .mdrawer-section-label {
    font-size: 10px;
    font-weight: 700;
    color: var(--muted, #6B7294);
    letter-spacing: 0.1em;
    text-transform: uppercase;
    padding: 6px 8px 10px;
  }

  /* ─── ITEMS ─────────────────────────────────────────────────────── */
  .mdrawer-item {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    padding: 11px 8px;
    border: none;
    background: transparent;
    text-decoration: none;
    color: var(--ink, #132055);
    font-family: inherit;
    text-align: left;
    cursor: pointer;
    border-radius: 11px;
    min-height: 48px;
    transition: background .12s;
  }
  .mdrawer-item:active {
    background: var(--surface-2, #F4F5F9);
  }
  .mdrawer-item.is-active {
    background: var(--surface, #fff);
    border: 1px solid var(--line, #E2E4ED);
    box-shadow: 0 4px 12px -6px rgba(15, 20, 50, 0.06);
  }
  /* Pastille d'icône 34×34 : accent-soft + accent par défaut, accent + blanc si active */
  .mdrawer-item-ico {
    width: 34px; height: 34px;
    border-radius: 10px;
    background: var(--surface-2, #F4F5F9);
    color: var(--accent, #5B65DE);
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 34px;
  }
  .mdrawer-item.is-active .mdrawer-item-ico {
    background: var(--accent, #5B65DE);
    color: #fff;
  }
  .mdrawer-item-ico svg {
    stroke: currentColor;
    fill: none;
    stroke-width: 1.6;
    stroke-linecap: round;
    stroke-linejoin: round;
  }
  /* Override pour 'star' qui a fill=currentColor */
  .mdrawer-item-ico svg [fill="currentColor"] { fill: currentColor; }
  .mdrawer-item-body {
    flex: 1;
    min-width: 0;
  }
  .mdrawer-item-label {
    font-size: 14px;
    font-weight: 500;
    color: var(--ink, #132055);
    line-height: 1.2;
  }
  .mdrawer-item.is-active .mdrawer-item-label {
    font-weight: 600;
  }
  .mdrawer-item-hint {
    font-size: 11.5px;
    color: var(--muted, #6B7294);
    margin-top: 3px;
    line-height: 1.3;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  /* Si le hint est vide (data pas encore chargée), on cache l'élément
     pour éviter un espace blanc. */
  .mdrawer-item-hint:empty { display: none; }
  /* Badge (ex: "2" sur Messagerie pour les unread) */
  .mdrawer-item-badge {
    font-size: 10.5px;
    font-weight: 700;
    padding: 3px 7px;
    border-radius: 99px;
    background: var(--accent, #5B65DE);
    color: #fff;
    flex: 0 0 auto;
  }

  /* ─── FOOTER : logout ──────────────────────────────────────────── */
  .mdrawer-footer {
    margin-top: auto;
    padding: 12px 18px calc(16px + env(safe-area-inset-bottom, 0px));
    border-top: 1px solid var(--line, #E2E4ED);
    background: var(--surface, #fff);
  }
  .mdrawer-logout {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 10px 12px;
    border: none;
    background: transparent;
    color: var(--muted, #6B7294);
    font-family: inherit;
    font-size: 13.5px;
    cursor: pointer;
    border-radius: 10px;
    text-align: left;
  }
  .mdrawer-logout:active {
    background: var(--surface-2, #F4F5F9);
  }
  .mdrawer-logout svg {
    stroke: currentColor;
    fill: none;
    stroke-width: 1.8;
  }
}

/* === 5. Padding global content (pages connectées) ========================== */
@media (max-width: 767px) {
  .page,
  main.page,
  .content-wrap {
    padding-left: max(16px, var(--safe-left));
    padding-right: max(16px, var(--safe-right));
    padding-top: 16px;
    padding-bottom: calc(20px + var(--mobile-bottomnav-h));
  }
}

/* === 6. Touch targets ≥44×44 mobile ======================================== */
@media (max-width: 767px) {
  .table-action-btn,
  .icon-btn,
  .btn-sm,
  .f-audio-play,
  .f-pay-checkbox {
    min-width: 44px;
    min-height: 44px;
  }
  /* Boutons icon-only dans les composants tighter */
  .f-back,
  .f-question-skip,
  button[aria-label] {
    min-height: 44px;
  }
}

/* === 7. Hover-only → wrap @media (hover: hover) ============================ */
/* Désactive le sticky-hover sur tap mobile. Les :hover déjà écrits ailleurs
   continueront de fonctionner sur souris, mais ne resteront pas "collés"
   après tap sur mobile. */
@media (hover: none) {
  /* Reset proactif des states hover qui pourraient "coller" */
  *:hover {
    /* Cible large mais safe : on annule juste les transitions hover-only
       qui resteraient déclenchées par un tap. Les couleurs hover propres
       sont gérées par les composants. */
  }
}

/* === 8. 100dvh fallback iOS Safari ========================================= */
/* iOS Safari < 17.4 calculait 100vh = viewport + barre URL → contenu coupé
   quand la barre se masquait au scroll. 100dvh (dynamic) résout ça.
   Note : on n'override pas ici, c'est aux composants d'utiliser 100dvh.
   Ce reset est défensif pour les composants qui ont encore 100vh hardcodé. */
@supports (height: 100dvh) {
  .app[style*="100vh"] { height: 100dvh; }
}

/* === 9. Toast safe-area ==================================================== */
/* Le toast `bottom: 20px` collidait avec la home-indicator iPhone en standalone */
.toast-wrap,
.toast-stack,
.toast {
  bottom: calc(20px + var(--safe-bottom)) !important;
}
@media (max-width: 767px) {
  /* En mobile, on remonte aussi le toast au-dessus de la bottom-nav */
  .toast-wrap,
  .toast-stack,
  .toast {
    bottom: calc(20px + var(--mobile-bottomnav-h)) !important;
  }
}

/* === 10. Print reset (factures) ============================================ */
@media print {
  .mobile-bottom-nav,
  .mobile-top-bar,
  .side {
    display: none !important;
  }
  .app {
    grid-template-columns: 1fr !important;
    padding-bottom: 0 !important;
  }
}

/* === 11. Scroll behavior smooth (UX native-feel) =========================== */
html { -webkit-text-size-adjust: 100%; }
body { -webkit-tap-highlight-color: transparent; }
/* Désactive le pull-to-refresh navigateur en standalone qui peut casser
   les composants scroll internes. */
@media (display-mode: standalone) {
  html, body { overscroll-behavior-y: contain; }
}

/* === 12. Défense responsive cross-page — règles SAFE uniquement =========== */
/* RETRAIT 20/05/2026 : les règles défensives avec !important sur .plans /
   .pack-grid / .summary / .stats-grid cassaient certaines pages (test.html
   notamment qui a `.plans { grid-template-columns: 1fr 1fr }` voulu).
   On garde uniquement les règles vraiment universelles (typo + touch targets
   + modales padding) qui ne risquent pas d'écraser un layout volontaire. */
@media (max-width: 640px) {
  /* H1 / H2 par défaut : downsize pour mobile */
  h1 { font-size: clamp(20px, 5vw, 28px); line-height: 1.2; }
  h2 { font-size: clamp(18px, 4.5vw, 24px); line-height: 1.25; }
  /* Touch targets pour les icons-btn génériques */
  .icon-btn-sm,
  .icon-btn-xs,
  .table-action-btn {
    min-width: 40px; min-height: 40px;
  }
  /* Modales génériques : safe-area-bottom (les paddings restent à la charge
     du CSS local, on ajoute juste la safe-area sans !important) */
  .modal-content,
  .modal-box,
  .simple-modal {
    padding-bottom: calc(20px + env(safe-area-inset-bottom, 0px));
    max-height: 90dvh;
    overflow-y: auto;
  }
  /* Sticky bottom CTAs : safe-area (pas de !important) */
  .sticky-bottom,
  .page-footer-cta {
    padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  }
}
