/* =====================================================
   LUMIX — RESPONSIVE MOBILE COMPLET v4.0
   Fixes petits écrans, Touch targets, Tables, PWA
   ===================================================== */

/* ── Anti-zoom total (pinch + double-tap) ── */
html {
  touch-action: manipulation;
  -ms-touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
  -ms-text-size-adjust: 100%;
}

/* ── BOTTOM NAV VRAIMENT FIXE (ne bouge JAMAIS) ── */
.bottom-nav {
  position: fixed !important;
  bottom: 0 !important;
  left: 0 !important;
  right: 0 !important;
  transform: translateZ(0);
  -webkit-transform: translateZ(0);
  will-change: transform;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  /* Empêcher que le nav soit poussé par le clavier virtuel */
  contain: layout style;
}

/* Utiliser dvh pour que le contenu s'adapte au viewport dynamique (clavier) */
@supports (height: 100dvh) {
  .app-layout {
    min-height: 100dvh;
  }
  .bottom-nav {
    position: fixed !important;
    bottom: 0 !important;
  }
}

/* Touch targets minimum 44px */
.btn, .nav-item, .challenge-option, .quiz-option,
.subject-card, .lesson-item, .tab-item, .quality-btn,
.country-btn, .level-btn, .subject-toggle, .checkbox-pill {
  min-height: 44px;
}

/* Empêcher overflow horizontal global */
html, body { max-width: 100vw; overflow-x: hidden; }

/* Tables responsive (leçons, cours) */
#lessonContent table, .card table, article table {
  display: block;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
  white-space: nowrap;
}
#lessonContent table td, #lessonContent table th { min-width: 80px; }

/* Formules mathématiques overflow */
#lessonContent .formula, .formula-box, .formula {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  white-space: pre-wrap;
  word-break: break-word;
}

/* Toast max-width mobile */
.toast { white-space: normal; max-width: calc(100vw - 32px); text-align: center; }

/* Badges grid: 3 colonnes max */
.badges-grid { grid-template-columns: repeat(3, 1fr); }

/* Safe area pour iPhone X+ */
.app-layout { padding-bottom: calc(var(--nav-height) + env(safe-area-inset-bottom, 0px)); }
.bottom-nav { padding-bottom: env(safe-area-inset-bottom, 0px); height: calc(var(--nav-height) + env(safe-area-inset-bottom, 0px)); }

/* Leçon : contenu lisible */
#lessonContent {
  font-size: 1rem;
  line-height: 1.8;
  word-break: break-word;
  overflow-wrap: break-word;
}
#lessonContent h2 { font-size: 1.2rem !important; margin-top: 24px !important; }
#lessonContent h3 { font-size: 1.05rem !important; }

/* Input zoom prevention iOS (font-size < 16px provoque zoom) */
input[type="text"], input[type="email"], input[type="password"],
input[type="number"], input[type="tel"], select, textarea {
  font-size: 16px;
}

/* Images dans leçons */
#lessonContent img { max-width: 100%; height: auto; border-radius: 12px; margin: 12px 0; }

/* Code/pre overflow */
#lessonContent pre, #lessonContent code {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 0.85em;
  max-width: 100%;
}

/* Quiz nav: boutons wrappent sur petits écrans */
.quiz-nav { flex-wrap: wrap; }
.quiz-nav .btn { flex: 1; min-width: 120px; }

/* Scroll horizontal pour onglets sujets */
.subject-tabs-scroll {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  padding-bottom: 4px;
  margin-bottom: 16px;
}
.subject-tabs-scroll::-webkit-scrollbar { display: none; }

.subject-tab-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 9px 16px;
  border-radius: 9999px;
  font-size: 0.875rem;
  font-weight: 800;
  white-space: nowrap;
  cursor: pointer;
  border: 2px solid #E5E0F8;
  background: white;
  color: #6D6B8A;
  transition: all 0.2s;
  -webkit-tap-highlight-color: transparent;
  flex-shrink: 0;
  min-height: 44px;
}
.subject-tab-pill.active {
  background: linear-gradient(135deg, #7C3AED, #F97316);
  border-color: transparent;
  color: white;
  box-shadow: 0 4px 14px rgba(124,58,237,0.35);
}

/* ── Breakpoint: téléphones moyens (< 400px) ─────────── */
@media (max-width: 399px) {
  :root {
    --text-3xl: 1.65rem;
    --text-2xl: 1.3rem;
    --text-xl:  1.15rem;
    --text-lg:  1.05rem;
    --text-base: 0.95rem;
    --text-sm:  0.82rem;
    --text-xs:  0.68rem;
  }

  .app-content { padding: 12px; }
  .hero-section { padding: 20px 16px; }
  .hero-title { font-size: 1.55rem; }
  .hero-stats { gap: 6px; }
  .hero-stat { padding: 6px 10px; font-size: 0.75rem; }

  .stats-grid { gap: 8px; }
  .stat-card { padding: 14px 12px; }
  .stat-card .stat-value { font-size: 1.3rem; }

  .subject-card { padding: 12px; }
  .subject-card .subject-icon { width: 42px; height: 42px; font-size: 1.6rem; }

  .quiz-body { padding: 18px 14px; }
  .quiz-question { font-size: 1rem; }
  .quiz-option { padding: 12px 13px; font-size: 0.9rem; }
  .quiz-option .option-letter { width: 26px; height: 26px; font-size: 0.8rem; }

  .flashcard-scene { height: 195px; }
  .flashcard-text { font-size: 1rem; }

  .badges-grid { grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .badge-item { padding: 10px 4px; }
  .badge-item .badge-icon { font-size: 1.6rem; }

  .auth-logo { font-size: 2.8rem; }
  .auth-card { padding: 24px 16px; }

  .challenge-card { padding: 16px; }
  .challenge-option { padding: 11px 13px; font-size: 0.88rem; }

  .btn { padding: 11px 18px; }
  .btn-lg { padding: 13px 22px; font-size: 1rem; }
  .btn-sm { padding: 7px 13px; }

  .xp-badge, .streak-badge { padding: 4px 9px; font-size: 0.75rem; }
  .onboarding-title { font-size: 1.6rem; }
}

/* ── Breakpoint: très petits écrans (< 360px — iPhone SE) ── */
@media (max-width: 360px) {
  :root {
    --text-3xl: 1.45rem;
    --text-2xl: 1.2rem;
  }

  .app-header { padding: 0 10px; }
  .app-header .logo-text { font-size: 1.15rem; }
  .streak-badge { display: none; }

  .hero-title { font-size: 1.38rem; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .badges-grid { grid-template-columns: repeat(2, 1fr); }

  .quiz-option .option-letter { width: 24px; height: 24px; font-size: 0.75rem; }
}

/* ── Breakpoint: tout petit (< 320px) ────────────────── */
@media (max-width: 320px) {
  .xp-badge { display: none; }
  .hero-stat { font-size: 0.68rem; padding: 4px 8px; }
  .subject-card .subject-name { font-size: 0.78rem; }
}

/* ── Safe area header iOS ────────────────────────────── */
@supports (padding: env(safe-area-inset-top)) {
  .app-header {
    padding-top: env(safe-area-inset-top, 0px);
    height: calc(var(--header-height) + env(safe-area-inset-top, 0px));
  }
}

/* ═══════════════════════════════════════════════════════
   PWA INSTALL — Sticky bar + Modal full-screen
   ═══════════════════════════════════════════════════════ */

/* ── Barre d'installation PWA — HAUT de l'écran, pleine largeur ── */
#pwaStickyBar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1100;
  background: linear-gradient(135deg, #7C3AED 0%, #9333EA 60%, #6D28D9 100%);
  color: white;
  padding: 10px 14px 10px calc(14px + env(safe-area-inset-left, 0px));
  padding-top: calc(10px + env(safe-area-inset-top, 0px));
  display: flex;
  align-items: center;
  gap: 10px;
  box-shadow: 0 4px 20px rgba(124,58,237,0.55);
  transform: translateY(-110%);
  transition: transform 0.45s cubic-bezier(0.34, 1.2, 0.64, 1);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
#pwaStickyBar.show { transform: translateY(0); }

/* Quand la barre est visible, pousser le contenu vers le bas */
body.pwa-bar-visible .app-header,
body.pwa-bar-visible .auth-layout {
  margin-top: 62px;
}
@supports (padding: env(safe-area-inset-top)) {
  body.pwa-bar-visible .app-header,
  body.pwa-bar-visible .auth-layout {
    margin-top: calc(62px + env(safe-area-inset-top, 0px));
  }
}

#pwaStickyBar .pwa-icon {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  flex-shrink: 0;
  object-fit: cover;
}
#pwaStickyBar .pwa-text { flex: 1; min-width: 0; }
#pwaStickyBar .pwa-title { font-size: 0.82rem; font-weight: 900; line-height: 1.2; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#pwaStickyBar .pwa-sub   { font-size: 0.68rem; opacity: 0.85; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#pwaStickyBar .pwa-cta {
  background: white;
  color: #7C3AED;
  border: none;
  border-radius: 8px;
  padding: 7px 13px;
  font-size: 0.8rem;
  font-weight: 900;
  cursor: pointer;
  flex-shrink: 0;
  transition: transform 0.15s;
  white-space: nowrap;
}
#pwaStickyBar .pwa-cta:active { transform: scale(0.94); }
#pwaStickyBar .pwa-close {
  background: rgba(255,255,255,0.18);
  border: none;
  color: white;
  border-radius: 50%;
  width: 26px;
  height: 26px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 0.9rem;
  flex-shrink: 0;
  padding: 0;
  line-height: 1;
}

/* Modal install PWA */
#pwaModal {
  position: fixed;
  inset: 0;
  z-index: 99999;
  background: rgba(15,10,30,0.88);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  display: flex;
  align-items: flex-end;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.35s ease;
}
#pwaModal.show { opacity: 1; pointer-events: all; }

#pwaModalSheet {
  width: 100%;
  background: white;
  border-radius: 28px 28px 0 0;
  padding: 6px 0 0;
  transform: translateY(100%);
  transition: transform 0.48s cubic-bezier(0.34, 1.18, 0.64, 1);
  box-shadow: 0 -24px 60px rgba(124,58,237,0.3);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  max-height: 92vh;
  overflow-y: auto;
}
#pwaModal.show #pwaModalSheet { transform: translateY(0); }

.pwa-modal-handle {
  width: 44px; height: 5px;
  background: #E5E0F8;
  border-radius: 99px;
  margin: 10px auto 0;
}

.pwa-modal-header {
  background: linear-gradient(135deg, #7C3AED 0%, #5B21B6 100%);
  margin: 16px 20px;
  border-radius: 20px;
  padding: 24px 20px;
  color: white;
  text-align: center;
  position: relative;
  overflow: hidden;
}
.pwa-modal-header::before {
  content: '';
  position: absolute;
  top: -40px; right: -40px;
  width: 140px; height: 140px;
  background: rgba(255,255,255,0.08);
  border-radius: 50%;
}

.pwa-modal-icon {
  width: 72px; height: 72px;
  border-radius: 18px;
  background: rgba(255,255,255,0.2);
  border: 2px solid rgba(255,255,255,0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.4rem;
  margin: 0 auto 14px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.2);
}

.pwa-modal-title {
  font-size: 1.35rem;
  font-weight: 900;
  margin-bottom: 6px;
}

.pwa-modal-sub {
  font-size: 0.85rem;
  opacity: 0.85;
  font-weight: 600;
  line-height: 1.5;
}

.pwa-modal-body { padding: 0 20px 20px; }

.pwa-feature-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin: 16px 0 20px;
}

.pwa-feature {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 14px;
  background: #F8F6FF;
  border-radius: 14px;
  border: 1.5px solid #EDE9FE;
}

.pwa-feature-icon { font-size: 1.6rem; flex-shrink: 0; }
.pwa-feature-text { flex: 1; }
.pwa-feature-title { font-size: 0.88rem; font-weight: 900; color: #1E1B4B; }
.pwa-feature-desc { font-size: 0.75rem; color: #6D6B8A; font-weight: 600; margin-top: 2px; }

.pwa-modal-btn {
  width: 100%;
  padding: 16px;
  background: linear-gradient(135deg, #7C3AED, #F97316);
  color: white;
  border: none;
  border-radius: 16px;
  font-size: 1.05rem;
  font-weight: 900;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  box-shadow: 0 6px 22px rgba(124,58,237,0.4);
  transition: transform 0.2s, box-shadow 0.2s;
  -webkit-tap-highlight-color: transparent;
  min-height: 54px;
  letter-spacing: -0.01em;
}
.pwa-modal-btn:active { transform: scale(0.97); box-shadow: none; }

.pwa-modal-skip {
  width: 100%;
  background: none;
  border: none;
  color: #9E9BB8;
  font-size: 0.82rem;
  font-weight: 700;
  cursor: pointer;
  padding: 14px;
  margin-top: 4px;
}

/* iOS: instructions étape par étape */
.pwa-ios-steps {
  display: none;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 20px;
}
.pwa-ios-steps.show { display: flex; }

.pwa-ios-step {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px;
  background: #F8F6FF;
  border-radius: 14px;
  border: 1.5px solid #EDE9FE;
}
.pwa-ios-num {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: linear-gradient(135deg, #7C3AED, #5B21B6);
  color: white;
  font-weight: 900;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.pwa-ios-text { font-size: 0.85rem; font-weight: 700; color: #1E1B4B; line-height: 1.4; }

/* Bouton install dans profil */
.btn-install-pwa {
  width: 100%;
  padding: 15px 20px;
  background: linear-gradient(135deg, #7C3AED, #F97316);
  color: white;
  border: none;
  border-radius: 16px;
  font-size: 1rem;
  font-weight: 900;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  box-shadow: 0 4px 20px rgba(124,58,237,0.4);
  transition: transform 0.2s;
  -webkit-tap-highlight-color: transparent;
  min-height: 52px;
}
.btn-install-pwa:active { transform: scale(0.97); }

/* Indicateur "app installée" */
.pwa-installed-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: #D1FAE5;
  color: #065f46;
  border-radius: 99px;
  padding: 6px 14px;
  font-size: 0.8rem;
  font-weight: 800;
  border: 1.5px solid #A7F3D0;
}
