/* ============================================
   MOBILE.CSS — Responsive mobile shell overrides
   Loaded after all other CSS.
   Scoped to max-width: 768px.
   ============================================ */

/* ── Mobile CSS tokens ── */
:root {
  --tab-h:  calc(56px + env(safe-area-inset-bottom));
  --top-h:  calc(56px + env(safe-area-inset-top));
}

/* ══════════════════════════════════════════
   MOBILE SHELL — body class (driven by JS isMobile() detection,
   NOT a media query — handles "request desktop site" mode and
   tablets that report >768px width).
   ══════════════════════════════════════════ */

/* ── Hide desktop chrome ── */
body.app-shell-mobile .sidebar,
body.app-shell-mobile .sidebar-overlay {
  display: none !important;
}

body.app-shell-mobile .app-header {
  display: none !important;
}

/* ── Main wrapper resets ── */
body.app-shell-mobile .main-wrapper,
body.app-shell-mobile .main-wrapper--collapsed {
  margin-left: 0 !important;
}

/* ── Main content area: fills between top bar and tab bar ── */
body.app-shell-mobile .main-content {
  position: fixed !important;
  top: var(--top-h) !important;
  bottom: var(--tab-h) !important;
  left: 0 !important;
  right: 0 !important;
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch !important;
  padding: var(--space-4) var(--space-4) var(--space-6) !important;
}

body.app-shell-mobile .main-content::-webkit-scrollbar {
  display: none;
}

/* AI page needs special handling: its own grid handles layout */
body.app-shell-mobile .ai-page-main.main-content {
  padding: 0 !important;
  overflow: hidden !important;
}

/* Prevent body scroll when mobile shell is active */
body.app-shell-mobile {
  overflow: hidden;
}

/* ══════════════════════════════════════════
   GLOBAL MOBILE FIXES (apply always, not just with body class)
   ══════════════════════════════════════════ */
@media (max-width: 900px) {

  /* Bigger tap targets */
  .btn,
  button.btn {
    min-height: 44px;
  }

  /* Prevent iOS zoom on input focus */
  input,
  textarea,
  select {
    font-size: 16px !important;
  }

  /* ── Modals become full-screen sheets ── */
  .modal,
  .pre-exam-modal {
    width: 100% !important;
    max-width: 100% !important;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0 !important;
    margin: 0 !important;
  }

  .pre-exam-modal-overlay {
    align-items: flex-end !important;
  }

  /* ── AI helper panel ── */
  .ai-helper-panel {
    top: var(--top-h, 56px) !important;
    width: 100% !important;
    border-left: none !important;
    border-top: 1px solid var(--border) !important;
  }

  /* ── Helper trigger: above tab bar ── */
  .ai-helper-trigger-wrap {
    bottom: calc(var(--tab-h) + 16px) !important;
  }
}

/* ══════════════════════════════════════════
   PAGE-SPECIFIC OVERRIDES
   ══════════════════════════════════════════ */

/* ── Dashboard: supplemental mobile shell overrides ──
   Core responsive rules live in dashboard.js injectDashboardStyles().
   Rules here handle general overflow containment. ── */
body.app-shell-mobile .main-content {
  overflow-x: hidden !important;
}

/* Keep legacy class stubs in case old markup survives a partial load */
@media (max-width: 900px) {
  .db-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .db-header__title {
    font-size: 1.4rem;
  }
}

/* ── Topics grid ── */
@media (max-width: 600px) {
  .topics-grid {
    grid-template-columns: 1fr !important;
  }
}

/* ── Practice ── */
@media (max-width: 900px) {
  .practice-wrapper {
    grid-template-columns: 1fr !important;
    padding: 0 !important;
  }

  .practice-sticky-header {
    padding: var(--space-3) var(--space-4) !important;
  }

  .question-card {
    padding: var(--space-4) !important;
  }
}

/* ── Exam ── */
@media (max-width: 900px) {
  .exam-layout,
  .exam-body {
    flex-direction: column !important;
  }

  .exam-header__inner {
    flex-direction: column !important;
    gap: var(--space-3) !important;
  }

  .exam-timer {
    order: -1;
    align-self: center;
  }

  .exam-nav-grid {
    grid-template-columns: repeat(5, 1fr) !important;
  }
}

/* ── Exam result ── */
@media (max-width: 900px) {
  .result-breakdown-grid {
    grid-template-columns: 1fr !important;
  }

  .exam-result-hero {
    flex-direction: column !important;
    text-align: center !important;
  }
}

/* ── Profile ── */
@media (max-width: 900px) {
  .profile-hero {
    flex-direction: column !important;
    align-items: center !important;
    text-align: center !important;
  }

  .profile-heatmap {
    overflow-x: auto !important;
  }

  .profile-stats-grid {
    grid-template-columns: repeat(2, 1fr) !important;
  }
}

/* ── Settings ── */
@media (max-width: 900px) {
  .settings-layout {
    grid-template-columns: 1fr !important;
    gap: var(--space-4) !important;
  }

  .settings-nav {
    position: static !important;
    display: flex !important;
    flex-wrap: wrap !important;
    gap: var(--space-2) !important;
  }

  .settings-card {
    padding: var(--space-4) !important;
  }
}

/* ── AI page (driven by body.app-shell-mobile, NOT @media — works for UA-detected mobile)── */
body.app-shell-mobile .ai-page-grid {
  grid-template-columns: 1fr !important;
  height: 100% !important;
}

/* Hide history sidebar on mobile — accessed via top-bar menu icon */
body.app-shell-mobile .ai-history-sidebar {
  display: none !important;
}

body.app-shell-mobile .ai-history-sidebar.ai-sidebar-open {
  display: flex !important;
  position: fixed !important;
  top: var(--top-h) !important;
  left: 0 !important;
  bottom: var(--tab-h) !important;
  width: 85vw !important;
  max-width: 320px !important;
  z-index: var(--z-overlay) !important;
  background: var(--surface) !important;
  box-shadow: var(--shadow-lg) !important;
  border-right: 1px solid var(--border) !important;
  animation: slideInLeft 0.25s ease !important;
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch !important;
}

@keyframes slideInLeft {
  from { transform: translateX(-100%); }
  to   { transform: translateX(0); }
}

body.app-shell-mobile .ai-chat-area {
  grid-column: 1 !important;
  height: 100% !important;
}

body.app-shell-mobile .ai-page-active .main-content {
  overflow: hidden !important;
}

/* Mobile top-bar buttons row on AI page should accommodate two buttons */
body.app-shell-mobile .mobile-top-bar__right {
  display: flex;
  gap: 4px;
  align-items: center;
}

/* ── Support ── */
@media (max-width: 900px) {
  .support-form,
  .support-wrapper {
    max-width: 100% !important;
    padding: 0 !important;
  }
}

/* ── Bookmarks ── */
@media (max-width: 900px) {
  .bookmarks-grid {
    grid-template-columns: 1fr !important;
  }
}

/* ── Mobile Top Bar (injected by mobile-shell.js) ── */
.mobile-top-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--top-h);
  padding-top: env(safe-area-inset-top);
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  padding-left: 16px;
  padding-right: 16px;
  padding-bottom: 12px;
  background: rgba(var(--surface-rgb, 255,255,255), 0.92);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border);
  /* Below overlays (300) and modals (400) so dialogs cover the bars. */
  z-index: var(--z-mobile-chrome);
  box-sizing: border-box;
}

:root[data-theme="dark"] .mobile-top-bar {
  background: rgba(10, 15, 28, 0.92);
}

.mobile-top-bar__left {
  width: 44px;
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

.mobile-top-bar__center {
  flex: 1;
  text-align: center;
  font-family: var(--font-heading);
  font-weight: 700;
  font-size: 16px;
  color: var(--text);
  letter-spacing: -0.01em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 0 8px;
}

.mobile-top-bar__right {
  width: 44px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.mobile-top-bar__btn {
  width: 36px;
  height: 36px;
  border-radius: var(--radius);
  border: none;
  background: transparent;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background var(--transition-fast), color var(--transition-fast);
  -webkit-tap-highlight-color: transparent;
}

.mobile-top-bar__btn:hover,
.mobile-top-bar__btn:active {
  background: var(--surface-2);
  color: var(--primary);
}

/* ── Mobile Tab Bar (injected by mobile-shell.js) ── */
.mobile-tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--tab-h);
  padding-bottom: env(safe-area-inset-bottom);
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  background: rgba(var(--surface-rgb, 255,255,255), 0.95);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-top: 1px solid var(--border);
  /* Same layer as top bar — below overlays and modals. */
  z-index: var(--z-mobile-chrome);
  box-sizing: border-box;
}

:root[data-theme="dark"] .mobile-tab-bar {
  background: rgba(19, 26, 43, 0.95);
}

.mobile-tab-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  padding: 8px 4px 0;
  min-height: 44px;
  -webkit-tap-highlight-color: transparent;
  transition: color var(--transition-fast);
}

.mobile-tab-btn--active {
  color: var(--primary);
}

.mobile-tab-btn__label {
  font-size: 10px;
  font-weight: 500;
  font-family: var(--font-heading);
  line-height: 1;
}

.mobile-tab-btn--active .mobile-tab-btn__label {
  font-weight: 700;
}

/* ── AI tab special: gradient text + neon glow when active ── */
.mobile-tab-btn--ai.mobile-tab-btn--active {
  filter: drop-shadow(0 0 6px rgba(var(--primary-rgb), 0.7));
}

.mobile-tab-btn--ai.mobile-tab-btn--active .mobile-tab-btn__label {
  background: linear-gradient(90deg, var(--primary), var(--primary-light));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  font-weight: 700;
}

/* ── AI sidebar overlay backdrop ── */
.ai-sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: calc(var(--z-overlay) - 1);
  /* No backdrop-filter — caused blur to the main content under iOS Safari
     even when sidebar covered only 85vw. */
}

.ai-sidebar-overlay.visible {
  display: block;
}

/* ── Keyboard-open mode (iOS Safari keyboard up).
   Hide the bottom tab bar so the AI composer floats just above the keyboard
   instead of being trapped behind a 56px-high bar.
   Scoped to .app-shell-mobile — focusin on a desktop <input> also adds the
   kb-open class globally, but on desktop there is no soft keyboard and no
   reason to hide the floating AI helper. ── */
body.kb-open.app-shell-mobile .mobile-tab-bar {
  transform: translateY(100%);
}
body.kb-open.app-shell-mobile .main-content {
  bottom: 0 !important;
}
body.kb-open.app-shell-mobile .ai-helper-trigger-wrap {
  display: none !important;
}
