/* ============================================================================
   IUTEA PWA mobile (Android + iPhone) : coque d'application, comme une app Flutter.
   Principes :
   1. La page ne défile jamais : en-tête et barre du bas fixes, seule la zone de contenu défile.
   2. Les dimensions viennent du « viewport visuel » (--app-h, mesuré par pwa-viewport.js) : clavier, barres du navigateur et
      barre d'URL sont pris en compte automatiquement.
   3. Les zones système (encoche, barre d'état, barre de gestes) sont réservées via env(safe-area-inset-*)
      (nécessite viewport-fit=cover, ajouté par partials/pwa-head.blade.php).
   4. Rien d'interactif ne passe sous la barre de navigation de l'app (espace mesuré : --pwa-nav-espace).
   Concerne les espaces connectés (admin, étudiant, enseignant, parent) à <= 968 px. L'ordinateur est inchangé.
   ============================================================================ */
:root {
  --pwa-sat: env(safe-area-inset-top, 0px);
  --pwa-sab: env(safe-area-inset-bottom, 0px);
  --pwa-sal: env(safe-area-inset-left, 0px);
  --pwa-sar: env(safe-area-inset-right, 0px);
}

@media screen and (max-width: 968px) {
  /* ---- 1. coque fixe ------------------------------------------------------ */
  /* (sélecteurs renforcés + !important : d'autres feuilles de style de l'application, chargées plus tard, ne doivent pas les défaire) */
  html:has(.admin-main), html body:has(.admin-main) {
    height: var(--app-h, 100dvh) !important; max-height: var(--app-h, 100dvh) !important; width: 100% !important; max-width: 100% !important;
    overflow: hidden !important; overscroll-behavior: none; -webkit-tap-highlight-color: transparent;
  }
  /* la coque est ancrée sur la zone réellement visible (--app-top = décalage du viewport visuel quand le clavier s'ouvre) */
  html body .admin-layout { position: fixed !important; top: var(--app-top, 0px) !important; left: 0 !important; right: 0 !important; width: 100% !important; height: var(--app-h, 100dvh) !important; min-height: 0 !important; overflow: hidden !important; }
  html body .admin-layout .admin-main {
    height: var(--app-h, 100dvh) !important; min-height: 0 !important; display: flex !important; flex-direction: column !important; overflow: hidden !important; box-sizing: border-box !important;
    padding-top: var(--pwa-sat) !important; padding-left: var(--pwa-sal) !important; padding-right: var(--pwa-sar) !important;
    padding-bottom: var(--pwa-nav-espace, calc(90px + var(--pwa-sab))) !important;
  }
  html body:not(:has(.pwa-bottomnav)) .admin-layout .admin-main { padding-bottom: var(--pwa-sab) !important; }
  html body .admin-layout .admin-main > .admin-topbar { position: relative !important; top: auto !important; flex: 0 0 auto !important; }
  html body .admin-layout .admin-main > :not(main):not(.admin-topbar) { flex: 0 0 auto; }
  /* zone de contenu : seule à défiler (inertie iOS, pas de rebond qui entraîne la page) */
  html body .admin-layout .admin-main > main.admin-content {
    flex: 1 1 auto !important; min-height: 0 !important; height: auto !important; max-height: none !important; overflow-x: hidden !important; overflow-y: auto !important;
    -webkit-overflow-scrolling: touch; overscroll-behavior-y: contain; scroll-padding-bottom: 24px; padding-bottom: .75rem !important; margin-bottom: 0 !important;
  }
  /* menu latéral (tiroir) : même hauteur que l'écran visible, jamais sous la barre de gestes */
  html body .admin-layout .admin-sidebar { top: var(--app-top, 0px) !important; height: var(--app-h, 100dvh) !important; max-height: var(--app-h, 100dvh) !important; box-sizing: border-box !important; padding-top: var(--pwa-sat); padding-bottom: calc(var(--pwa-sab) + 16px) !important; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }

  /* ---- 2. barre de navigation de l'app ------------------------------------ */
  html body .pwa-bottomnav { bottom: max(14px, calc(var(--pwa-sab) + 8px)) !important; }
  html body.cx-plein .admin-layout .admin-main { padding-bottom: 0 !important; }
  /* discussion ouverte ou clavier affiché : la barre disparaît (elle masquait la zone d'écriture) */
  html body.cx-plein .pwa-bottomnav, html.pwa-clavier body .pwa-bottomnav { display: none !important; }
  html.pwa-clavier body .admin-layout .admin-main { padding-bottom: 0 !important; }

  /* ---- 3. débordements ---------------------------------------------------- */
  .admin-content { overflow-wrap: break-word; }
  .admin-content img, .admin-content video, .admin-content canvas, .admin-content svg { max-width: 100%; }
  .admin-content img, .admin-content video { height: auto; }
  .admin-content pre { overflow-x: auto; max-width: 100%; }
  .admin-content input, .admin-content select, .admin-content textarea { max-width: 100%; box-sizing: border-box; scroll-margin-bottom: 24px; }
  /* tableau trop large : défile à l'intérieur de sa carte au lieu d'élargir la page (enveloppe ajoutée par pwa-viewport.js) */
  .pwa-table-defile { max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }

  /* ---- 4. fenêtres, feuilles et plein écran : zones de sécurité + hauteur visible ----- */
  html body .anniv-feuille, html body .ct-modal, html body .cpe-fond, html body .jus-modal, html body .pbe-modal-fond, html body .pz-fond, html body .cx-modal, html body .rg.rg-ouvert {
    top: var(--app-top, 0px) !important; bottom: auto !important; height: var(--app-h, 100dvh) !important; box-sizing: border-box !important;
    padding-top: max(1rem, var(--pwa-sat)) !important; padding-bottom: max(1rem, var(--pwa-sab)) !important;
    padding-left: max(1rem, var(--pwa-sal)) !important; padding-right: max(1rem, var(--pwa-sar)) !important;
  }
  html body .cx-story { top: var(--app-top, 0px) !important; bottom: auto !important; height: var(--app-h, 100dvh) !important; box-sizing: border-box; }
  html body .cc-cam { top: var(--app-top, 0px) !important; bottom: auto !important; height: var(--app-h, 100dvh) !important; box-sizing: border-box !important; padding: var(--pwa-sat) var(--pwa-sar) var(--pwa-sab) var(--pwa-sal) !important; }
  html body .anniv-feuille-panneau, html body .cpe-boite, html body .pbe-modal, html body .cx-modal-boite, html body .ct-modal > div, html body .jus-modal > div {
    max-height: calc(var(--app-h, 100dvh) - 2rem - var(--pwa-sat) - var(--pwa-sab)) !important; overflow-y: auto !important; -webkit-overflow-scrolling: touch; box-sizing: border-box;
  }
  html body dialog[open] { max-height: calc(var(--app-h, 100dvh) - 2rem - var(--pwa-sat) - var(--pwa-sab)) !important; overflow-y: auto !important; box-sizing: border-box; }

  /* ---- 5. éléments flottants : toujours au-dessus de la barre de navigation -------- */
  .va-bandeau { bottom: calc(var(--pwa-nav-espace, var(--pwa-sab)) + 8px); }
  .retour-flottant { bottom: calc(var(--pwa-nav-espace, var(--pwa-sab)) + 8px); }
  html.pwa-clavier .va-bandeau { bottom: 8px; }
  /* bandeau « Actualiser » (admin) : jamais sur mobile, l'app se met à jour seule */
  .sync-bandeau, #admin-sync + .sync-bandeau { display: none !important; }

  /* ---- 6. IUTEA Connect : occupe toute la zone disponible ------------------- */
  html body:has(#cx) .admin-layout .admin-main > main.admin-content { padding: 0 !important; display: flex !important; flex-direction: column; }
  html body:has(#cx) .admin-layout .admin-main > main.admin-content > .live-zone { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; }
  html body #cx.cx { flex: 1 1 auto; min-height: 0; height: auto !important; margin: 0; border-radius: 0; }
}
