/* ═══════════════════════════════════════════════════════════════════════════
   Maliba — Frontend Refonte v6.0 (Fix Superpositions & Responsive)
   ─────────────────────────────────────────────────────────────────────────────
   Objectif : corriger DÉFINITIVEMENT les problèmes de superposition d'écrans
   et de responsive qui persistent malgré les versions précédentes.

   PROBLÈMES CORRIGÉS :
   1. Splash screen qui reste visible par-dessus le contenu (z-200 mal géré)
   2. Modal-overlay qui s'affiche en permanence (display: flex !important dans
      style-v5.css override le `hidden` du HTML)
   3. Toast-container qui se superpose au modal
   4. Sidebar overlay qui reste visible après fermeture
   5. Install prompt + update banner qui se chevauchent en bas d'écran
   6. Éléments fixed qui sortent du viewport sur mobile
   7. Breakpoints incohérents entre style.css, style-v4.css, style-v5.css

   STRATÉGIE : Cette couche surcharge TOUTES les précédentes avec une
   hiérarchie z-index STRICTE et des règles !important ciblées.
   ═══════════════════════════════════════════════════════════════════════════ */

/* ═══ 1. HIÉRARCHIE Z-STRICTE (du plus bas au plus haut) ═════════════════════
   - z-1   : contenu principal (#app, .maliba-shell)
   - z-10  : sidebar desktop, nav items
   - z-15  : chat-header sticky
   - z-20  : topbar
   - z-30  : sidebar mobile (drawer)
   - z-40  : sidebar overlay (mobile)
   - z-50  : install prompt, update banner (notifications non-bloquantes)
   - z-60  : toast-container (notifications temporaires)
   - z-70  : modal-overlay (bloque l'interaction)
   - z-100 : splash screen (par-dessus tout au démarrage)
   ══════════════════════════════════════════════════════════════════════════ */

/* ─── 2. SPLASH SCREEN — par-dessus tout, MAIS caché proprement ─────────── */
#splash-screen {
  z-index: 9999 !important;
  position: fixed !important;
  inset: 0 !important;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  background: #050810 !important;
  transition: opacity 0.4s ease-out, visibility 0.4s ease-out !important;
}

#splash-screen.splash-hidden {
  opacity: 0 !important;
  visibility: hidden !important;
  pointer-events: none !important;
  display: none !important; /* ← FORCE le retrait complet après transition */
}

/* ─── 3. MODAL OVERLAY — caché par défaut, visible seulement avec .open ── */
/* ⚠️ CRITIQUE : style-v5.css force `display: none` mais le HTML a `hidden`.
   On s'assure que le modal ne s'affiche JAMAIS sauf avec .open. */
#modal-overlay {
  position: fixed !important;
  inset: 0 !important;
  background: rgba(0, 0, 0, 0.75) !important;
  backdrop-filter: blur(8px) !important;
  -webkit-backdrop-filter: blur(8px) !important;
  z-index: 70 !important;
  display: none !important; /* ← TOUJOURS none par défaut */
  align-items: center !important;
  justify-content: center !important;
  padding: 1rem !important;
}

#modal-overlay.open {
  display: flex !important;
}

#modal-overlay:not(.open) {
  display: none !important;
}

#modal-content {
  position: relative !important;
  z-index: 71 !important;
  background: #0A0E1A !important;
  border: 1px solid rgba(255, 255, 255, 0.09) !important;
  border-radius: 20px !important;
  box-shadow: 0 32px 64px -16px rgba(0, 0, 0, 0.8) !important;
  max-width: 32rem !important;
  width: 100% !important;
  max-height: 90vh !important;
  overflow-y: auto !important;
  margin: 0 !important;
}

/* ─── 4. TOAST CONTAINER — notifications temporaires (z-60) ─────────────── */
#toast-container {
  position: fixed !important;
  top: max(1rem, env(safe-area-inset-top)) !important;
  right: 1rem !important;
  z-index: 60 !important;
  display: flex !important;
  flex-direction: column !important;
  gap: 0.5rem !important;
  pointer-events: none !important;
  max-width: calc(100vw - 2rem) !important;
  max-height: calc(100vh - 2rem) !important;
  overflow-y: auto !important;
}

#toast-container > * {
  pointer-events: auto !important;
}

/* Sur mobile, le toast prend toute la largeur en bas */
@media (max-width: 640px) {
  #toast-container {
    left: 0.5rem !important;
    right: 0.5rem !important;
    top: auto !important;
    bottom: max(1rem, env(safe-area-inset-bottom)) !important;
    max-width: none !important;
    flex-direction: column-reverse !important;
  }
}

/* ─── 5. SIDEBAR OVERLAY — caché par défaut (z-40) ──────────────────────── */
#sidebar-overlay,
.maliba-sidebar-overlay {
  position: fixed !important;
  inset: 0 !important;
  background: rgba(0, 0, 0, 0.6) !important;
  backdrop-filter: blur(4px) !important;
  -webkit-backdrop-filter: blur(4px) !important;
  z-index: 40 !important;
  opacity: 0 !important;
  visibility: hidden !important;
  pointer-events: none !important;
  transition: opacity 0.25s ease-out, visibility 0.25s ease-out !important;
}

#sidebar-overlay.open,
#sidebar-overlay:not(.hidden),
.maliba-sidebar-overlay.open {
  opacity: 1 !important;
  visibility: visible !important;
  pointer-events: auto !important;
}

/* ⚠️ Le sidebar-overlay doit être caché sauf si la sidebar est ouverte */
#sidebar-overlay:not(.open) {
  display: none !important;
}

/* Desktop : pas d'overlay */
@media (min-width: 1024px) {
  #sidebar-overlay,
  .maliba-sidebar-overlay {
    display: none !important;
  }
}

/* ─── 6. SIDEBAR — drawer mobile (z-30) ─────────────────────────────────── */
.maliba-sidebar {
  z-index: 45 !important;
}

@media (max-width: 1023px) {
  .maliba-sidebar {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    bottom: 0 !important;
    width: 280px !important;
    max-width: 85vw !important;
    transform: translateX(-100%) !important;
    transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1) !important;
    z-index: 45 !important;
    box-shadow: 0 32px 64px -16px rgba(0, 0, 0, 0.7) !important;
  }

  .maliba-sidebar.open {
    transform: translateX(0) !important;
  }
}

/* ─── 7. INSTALL PROMPT & UPDATE BANNER — ne pas se chevaucher (z-50) ──── */
.maliba-install-prompt,
.maliba-update-banner {
  z-index: 50 !important;
}

/* Si les deux sont visibles, l'update banner passe au-dessus */
.maliba-update-banner {
  bottom: calc(1rem + env(safe-area-inset-bottom)) !important;
}

.maliba-install-prompt {
  bottom: calc(1rem + env(safe-area-inset-bottom)) !important;
}

/* Si update-banner visible, décaler install-prompt au-dessus */
.maliba-update-banner-visible ~ .maliba-install-prompt,
.maliba-update-banner-visible + .maliba-install-prompt {
  bottom: calc(5rem + env(safe-area-inset-bottom)) !important;
}

/* ─── 8. CHAT HEADER — sticky mais ne superpose pas le contenu ──────────── */
.maliba-chat-header {
  position: sticky !important;
  top: 0 !important;
  z-index: 15 !important;
  background: rgba(15, 20, 36, 0.95) !important;
  backdrop-filter: blur(20px) saturate(180%) !important;
  -webkit-backdrop-filter: blur(20px) saturate(180%) !important;
}

/* ─── 9. SHELL — pas d'overflow horizontal ──────────────────────────────── */
.maliba-shell {
  overflow: hidden !important;
  position: relative !important;
}

.maliba-main {
  overflow: hidden !important;
  min-width: 0 !important;
  position: relative !important;
}

.maliba-page {
  overflow-x: hidden !important;
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch !important;
  word-wrap: break-word !important;
  overflow-wrap: break-word !important;
}

/* ─── 10. RESPONSIVE — breakpoints cohérents ───────────────────────────── */
/* Mobile-first : par défaut tout est mobile, on ajoute des règles pour ↑ */

/* < 380px : très petits téléphones */
@media (max-width: 379px) {
  .maliba-page {
    padding: 0.625rem !important;
  }
  .maliba-chat-messages {
    padding: 0.625rem 0.5rem !important;
  }
  .maliba-msg-bubble {
    max-width: 92% !important;
    padding: 0.5rem 0.75rem !important;
    font-size: 0.825rem !important;
  }
  .maliba-chat-header {
    padding: 0.5rem 0.625rem !important;
  }
  .maliba-chat-header-title {
    font-size: 0.8rem !important;
  }
  .maliba-auth-card {
    padding: 1.25rem 1rem !important;
    margin: 0 !important;
    border-radius: 16px !important;
  }
  .maliba-auth-row {
    grid-template-columns: 1fr !important;
    gap: 0.5rem !important;
  }
}

/* 380-639px : petits téléphones */
@media (min-width: 380px) and (max-width: 639px) {
  .maliba-page {
    padding: 0.75rem !important;
  }
  .maliba-chat-messages {
    padding: 0.75rem 0.625rem !important;
  }
  .maliba-msg-bubble {
    max-width: 90% !important;
  }
}

/* 640-767px : grands téléphones */
@media (min-width: 640px) and (max-width: 767px) {
  .maliba-page {
    padding: 1rem !important;
  }
  .maliba-chat-messages {
    padding: 1rem 0.75rem !important;
  }
}

/* 768-1023px : tablette */
@media (min-width: 768px) and (max-width: 1023px) {
  .maliba-page {
    padding: 1.25rem !important;
  }
  .maliba-chat-messages {
    padding: 1.25rem 1rem !important;
  }
  .maliba-msg-bubble {
    max-width: 80% !important;
  }
}

/* 1024-1279px : desktop petit */
@media (min-width: 1024px) and (max-width: 1279px) {
  .maliba-sidebar {
    width: 260px !important;
  }
  .maliba-page {
    padding: 1.5rem !important;
  }
}

/* ≥ 1280px : desktop large */
@media (min-width: 1280px) {
  .maliba-sidebar {
    width: 280px !important;
  }
  .maliba-page {
    padding: 2rem !important;
  }
  .maliba-chat-messages {
    padding: 2rem 1.5rem !important;
  }
}

/* ≥ 1536px : desktop XL */
@media (min-width: 1536px) {
  .maliba-page {
    max-width: 1400px !important;
    margin: 0 auto !important;
  }
}

/* ─── 11. ANTI-SUPERPOSITION : tous les fixed doivent avoir un z-index ─── */
/* On s'assure qu'aucun élément fixed n'a un z-index ambigu */
.maliba-fab-menu { z-index: 35 !important; }
.maliba-chat-header-action { z-index: auto !important; }
.maliba-scroll-btn { z-index: 16 !important; }
.maliba-composer-wrap { z-index: 17 !important; }

/* ─── 12. ANTI-OVERFLOW : empêcher le scroll horizontal ─────────────────── */
html, body {
  overflow-x: hidden !important;
  max-width: 100vw !important;
}

/* ─── 13. ANTI-FOUC : cacher le contenu pendant le chargement ───────────── */
html.js-loading #app > *:not(#splash-screen) {
  visibility: hidden !important;
}

html.js-ready #app > *:not(#splash-screen) {
  visibility: visible !important;
}

/* ─── 14. MODAL CONTENT — responsive ────────────────────────────────────── */
@media (max-width: 640px) {
  #modal-content {
    max-width: calc(100vw - 1rem) !important;
    max-height: 90vh !important;
    border-radius: 16px !important;
  }
}

/* ─── 15. TOAST — responsive et non-bloquant ────────────────────────────── */
.maliba-toast {
  max-width: 360px !important;
  min-width: 240px !important;
  box-shadow: 0 8px 24px -6px rgba(0, 0, 0, 0.6) !important;
}

@media (max-width: 640px) {
  .maliba-toast {
    max-width: 100% !important;
    min-width: auto !important;
  }
}

/* ─── 16. INSTALL PROMPT — responsive ───────────────────────────────────── */
@media (max-width: 640px) {
  .maliba-install-prompt {
    left: 0.5rem !important;
    right: 0.5rem !important;
    max-width: none !important;
    flex-direction: column !important;
    gap: 0.5rem !important;
    padding: 0.75rem !important;
  }
  .maliba-install-prompt-actions {
    width: 100% !important;
    justify-content: stretch !important;
  }
  .maliba-install-prompt-btn {
    flex: 1 !important;
  }
}

/* ─── 17. UPDATE BANNER — responsive ────────────────────────────────────── */
@media (max-width: 640px) {
  .maliba-update-banner {
    left: 0.5rem !important;
    right: 0.5rem !important;
    max-width: none !important;
    flex-direction: column !important;
    gap: 0.5rem !important;
    padding: 0.75rem !important;
    text-align: center !important;
  }
  .maliba-update-banner-actions {
    width: 100% !important;
    justify-content: center !important;
  }
}

/* ─── 18. CLASSES UTILITAIRES — display par défaut ──────────────────────── */
/* S'assurer que les classes Tailwind critiques sont définies */
.hidden { display: none !important; }
.flex { display: flex !important; }
.inline-flex { display: inline-flex !important; }
.block { display: block !important; }
.fixed { position: fixed !important; }
.absolute { position: absolute !important; }
.relative { position: relative !important; }
.inset-0 { top: 0; right: 0; bottom: 0; left: 0; }
.w-full { width: 100% !important; }

/* ─── 19. NETTOYAGE des conflits display ────────────────────────────────── */
/* ⚠️ Le HTML a `class="... hidden"` mais le CSS peut forcer display.
   On s'assure que `hidden` gagne TOUJOURS. */
.hidden { display: none !important; }

/* Mais si on ajoute .open ou .visible, on override */
#modal-overlay.open { display: flex !important; }
#sidebar-overlay.open { display: block !important; opacity: 1 !important; visibility: visible !important; }

/* ─── 20. PRINT ─────────────────────────────────────────────────────────── */
@media print {
  #splash-screen,
  #modal-overlay,
  #toast-container,
  #sidebar-overlay,
  .maliba-sidebar,
  .maliba-topbar,
  .maliba-fab-menu,
  .maliba-install-prompt,
  .maliba-update-banner {
    display: none !important;
  }
}

/* ═══════════════════════════════════════════════════════════════════════════
   END — Maliba Frontend Refonte v6.0
   ═══════════════════════════════════════════════════════════════════════════ */
