/* ═══════════════════════════════════════════════════════════════════════════
   AstroPlanets Academy — mobile / flexible layout layer
   Load LAST on every page (after style.css and theme.css).
   Only adds responsive behaviour; desktop layout is untouched (>768px).
   ═══════════════════════════════════════════════════════════════════════════ */

/* ── Global safety nets (all sizes) ───────────────────────────────────────── */
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
img, video, canvas, svg, iframe { max-width: 100%; }
body { overflow-wrap: anywhere; }
button, a, input, select, textarea { touch-action: manipulation; }

/* ── Phones & small tablets ───────────────────────────────────────────────── */
@media (max-width: 768px) {

  /* Never let the page scroll sideways */
  html, body { max-width: 100%; overflow-x: hidden; }

  /* Use the real visible height (mobile browser chrome shrinks 100vh) */
  .sidebar { height: 100dvh; padding-bottom: env(safe-area-inset-bottom); }
  .login-page-wrap, .guest-wrap { min-height: 100dvh; }

  /* iOS zooms into any input under 16px — stop that */
  input:not([type=checkbox]):not([type=radio]):not([type=range]):not([type=file]):not([type=color]),
  select, textarea { font-size: 16px !important; }

  /* Comfortable tap targets */
  .btn, button.btn, .tab-btn, .sidebar-nav a, .sidebar-nav button.nav-btn { min-height: 40px; }
  .sidebar-toggle { width: 40px; height: 40px; }

  /* App shell */
  .app-header { padding-left: max(14px, env(safe-area-inset-left)); padding-right: max(14px, env(safe-area-inset-right)); }
  .header-right { gap: 6px; min-width: 0; }
  .app-header h1 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
  .page-content { padding: 14px; padding-bottom: calc(14px + env(safe-area-inset-bottom)); }

  /* Cards, grids, flex rows that were sized for desktop */
  .card { min-width: 0; }
  .grid-2, .grid-3, .grid-4, .courses-upcoming-grid { grid-template-columns: 1fr !important; }
  .page-content [style*="grid-template-columns:1fr 1fr"],
  .page-content [style*="grid-template-columns: 1fr 1fr"],
  .page-content [style*="grid-template-columns:repeat(2"],
  .page-content [style*="grid-template-columns:repeat(3"],
  .page-content [style*="grid-template-columns:repeat(4"] { grid-template-columns: 1fr !important; }
  .page-content [style*="display:flex"][style*="gap"] { flex-wrap: wrap; }
  .page-content [style*="min-width:180px"] { min-width: 0 !important; max-width: 100% !important; width: 100% !important; flex: 1 1 100% !important; }
  .page-content [style*="max-width:560px"],
  .page-content [style*="max-width:700px"] { max-width: 100% !important; }

  /* Tables scroll inside their own box instead of widening the page */
  .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; max-width: 100%; }
  .table-wrap table { min-width: 560px; }
  .page-content table { max-width: 100%; }
  .page-content td, .page-content th { padding-left: 8px; padding-right: 8px; }

  /* Modals fit the screen and scroll */
  .modal-overlay { padding: 12px; align-items: flex-end; }
  .modal { max-width: 100%; width: 100%; max-height: 92dvh; overflow-y: auto; padding: 20px 16px; border-radius: 16px; }
  .modal-hdr h3 { font-size: 15px; }

  /* Landing page */
  .site-header { padding: 16px 16px 0; }
  .brand { font-size: 17px; }
  .hero { padding: 28px 16px 24px; gap: 28px; }
  .hero h1 { font-size: 30px; }
  .hero-cta { width: 100%; }
  .hero-cta .btn-primary, .hero-cta .btn-outline { flex: 1 1 100%; text-align: center; }
  .hero-card { padding: 22px 18px; }
  .section { padding: 34px 16px; }
  .section-title { font-size: 23px; }
  .section-sub { margin-bottom: 26px; }
  .cta-band { padding: 34px 18px; border-radius: 18px; }
  .site-footer { padding: 24px 16px calc(32px + env(safe-area-inset-bottom)); }

  /* Decorative backgrounds: lighter on phones (battery/perf + no overflow) */
  .planet-saturn, .comet, .planet-4, .planet-5 { display: none !important; }
  .nebula { max-width: 70vw; max-height: 70vw; }

  /* Guest join / left pages */
  .guest-wrap { padding: 14px; padding-bottom: calc(14px + env(safe-area-inset-bottom)); align-items: flex-start; }
  .guest-card { padding: 26px 20px 22px; border-radius: 18px; margin: auto 0; }
  .guest-brand .icon { font-size: 36px; }
  .guest-brand .name { font-size: 19px; }
  .guest-btn { padding: 15px; min-height: 48px; }

  /* ── Live meeting room ─────────────────────────────────────────────────── */
  .meeting-wrap { height: 100dvh; padding-bottom: calc(72px + env(safe-area-inset-bottom)); }
  .meeting-topbar { padding-left: max(10px, env(safe-area-inset-left)); padding-right: max(10px, env(safe-area-inset-right)); }
  .video-area { min-height: 180px; }
  .video-thumb-row { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .video-thumb { flex-shrink: 0; }
  .meeting-panel { max-height: 46dvh; }
  .panel-tabs { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .panel-tab { flex: 1 0 auto; white-space: nowrap; min-height: 40px; }
  .chat-input-wrap { padding-bottom: calc(8px + env(safe-area-inset-bottom)); }
  .chat-msg-bubble { max-width: 100%; }

  /* Controls bar: pinned to the bottom, clear of the iPhone home indicator */
  .meeting-controls {
    padding-bottom: calc(8px + env(safe-area-inset-bottom)) !important;
    padding-left: max(6px, env(safe-area-inset-left)) !important;
    padding-right: max(6px, env(safe-area-inset-right)) !important;
    scrollbar-width: none;
  }
  .meeting-controls::-webkit-scrollbar { display: none; }
  .ctrl-btn { min-height: 44px; }

  /* Floating "on stage" panel: full width above the controls instead of a
     240px box covering the video */
  .stage-panel {
    left: 10px !important; right: 10px !important; width: auto !important;
    bottom: calc(80px + env(safe-area-inset-bottom)) !important;
    max-height: 38dvh !important;
  }

  /* Participant action menu must not run off-screen */
  .participant-menu { right: 0; max-width: 70vw; }

  /* Whiteboard: toolbar scrolls instead of overflowing; canvas fills the rest */
  #wb-overlay { height: 100dvh; }
  #wb-toolbar { flex-wrap: nowrap !important; overflow-x: auto; -webkit-overflow-scrolling: touch; padding: 8px 10px !important; scrollbar-width: none; }
  #wb-toolbar::-webkit-scrollbar { display: none; }
  #wb-toolbar > * { flex-shrink: 0; }
  #wb-overlay canvas { touch-action: none; max-width: 100%; }

  /* Login card */
  .login-box-wrapper { max-height: none; }
}

/* ── Small phones ─────────────────────────────────────────────────────────── */
@media (max-width: 480px) {
  .hero h1 { font-size: 26px; }
  .hero p.lead { font-size: 15px; }
  .features-grid, .roles-grid { grid-template-columns: 1fr; }
  .stat-val { font-size: 20px; }
  .guest-card h1 { font-size: 19px; }
  .app-header h1 { max-width: 46vw; }
  .modal-overlay { padding: 0; }
  .modal { border-bottom-left-radius: 0; border-bottom-right-radius: 0; }
  .site-nav { gap: 10px; }
  .btn-login { padding: 9px 16px; }
}

/* ── Phones held sideways: video beside the panel, thin controls ──────────── */
@media (max-width: 950px) and (orientation: landscape) and (max-height: 520px) {
  .meeting-wrap { padding-bottom: calc(52px + env(safe-area-inset-bottom)); }
  .meeting-main { flex-direction: row !important; }
  .video-area { flex: 1 1 0; min-width: 0; min-height: 0; }
  .meeting-panel {
    width: 38% !important; max-width: 320px; max-height: none !important;
    border-top: none !important; border-left: 3px solid var(--orange, #fd7e14) !important;
  }
  .meeting-topbar { height: 38px; min-height: 38px; }
  .meeting-controls { min-height: 50px !important; padding-top: 4px !important; padding-bottom: calc(4px + env(safe-area-inset-bottom)) !important; }
  .ctrl-btn { min-height: 38px; padding: 3px 8px; }
  .ctrl-btn-label { display: none; }
  .stage-panel { left: auto !important; width: 260px !important; bottom: calc(58px + env(safe-area-inset-bottom)) !important; max-height: 55dvh !important; }
  .guest-wrap { align-items: flex-start; }
  .guest-card { margin: 0 auto; }
}

/* ── Tablets (portrait) ───────────────────────────────────────────────────── */
@media (min-width: 769px) and (max-width: 1024px) {
  .meeting-panel { width: 240px; }
  .page-content { padding: 18px; }
  .grid-4 { grid-template-columns: 1fr 1fr; }
}

/* ── Respect reduced-motion (also saves battery on phones) ────────────────── */
@media (prefers-reduced-motion: reduce) {
  .planet, .comet, .nebula, .zodiac, .star, .planet-saturn { animation: none !important; }
}
