/* Learnova responsive layer - loaded last on every page.
 *
 * Only sizing and containment: colours, radii and type faces stay in the token and
 * component files, so dark mode needs nothing here. Measured with
 * scripts/mobile_audit.py at 320-1440px; each rule below fixes something that audit
 * found (horizontal page scroll, clipped controls, sub-44px touch targets, fields that
 * make iOS Safari zoom, unreadably small text).
 */

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

/* --- Nothing a student or the AI writes can widen the page -------------------------
   Long German compounds, URLs and formulas break inside their own box instead of
   pushing the layout past the screen. `anywhere` also lets grid and flex columns shrink
   below the length of their longest word, which is what stretched the lesson column. */
main, .lesson-main, .chat-messages, .message, .assistant-thread {
  overflow-wrap: anywhere;
}
main :is(h1, h2, h3) {
  hyphens: auto;
}
main :is(img, video, canvas) {
  max-width: 100%;
  height: auto;
}
main iframe {
  max-width: 100%;
}

/* Wide content scrolls inside itself, never the page. */
:is(.message, .prose, .speech-readable-text, .asst-message) table {
  display: block;
  max-width: 100%;
  overflow-x: auto;
  border-collapse: collapse;
  margin: .6em 0;
}
:is(.message, .speech-readable-text) :is(th, td) {
  /* Cells keep whole words (the table scrolls instead); "anywhere" from the container
     broke "Exponenten" into "Exponent / en". */
  overflow-wrap: normal;
  hyphens: auto;
  min-width: 5.5em;
  padding: 6px 10px;
  border: 1px solid var(--ln-line-strong);
  text-align: left;
  vertical-align: top;
}
:is(.message, .speech-readable-text) th {
  background: color-mix(in srgb, var(--ln-violet) 8%, transparent);
}
.katex-display, .ln-math-display {
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  padding-block: 2px;
}
main pre, .message pre {
  max-width: 100%;
  overflow-x: auto;
}
/* Readable stand-in when KaTeX cannot load (static/js/math.js). */
.ln-math-fallback {
  font-family: "Cambria Math", "STIX Two Math", "Times New Roman", serif;
  white-space: nowrap;
}
.ln-math-fallback.ln-math-display {
  display: block;
  margin: .5em 0;
  text-align: center;
  white-space: normal;
}
/* A formula KaTeX cannot typeset (malformed, or offline and not spellable as text) is
   shown as its source: readable monospace, wrapping inside its box, never red. */
.ln-math-source {
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: .92em;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  color: inherit;
}
.ln-math-source-display {
  display: block;
  margin: .5em 0;
}
/* Chat replies: an inline formula wider than the bubble scrolls inside itself (the same
   rule as .asst-body in assistant.css). */
:is(.tutor-message, .history-message) .ln-math:not(.ln-math-display) {
  display: inline-block;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  vertical-align: middle;
}
/* A stored tutor reply on the lesson history page, rendered as markdown
   (static/js/rich-history.js). Token colours only, so dark mode needs nothing extra. */
.history-message-body > :first-child { margin-top: 0; }
.history-message-body > :last-child { margin-bottom: 0; }
.history-message-body :is(ul, ol) { margin: 7px 0; padding-inline-start: 1.3rem; }
.history-message-body li { margin: 4px 0; line-height: 1.55; }
.history-message-body :is(h3, h4, h5, h6) { margin: 11px 0 4px; font-size: 1rem; }
.history-message-body table {
  display: block;
  width: max-content;
  max-width: 100%;
  overflow-x: auto;
  margin: .6em 0;
  border-collapse: collapse;
}
.history-message-body :is(th, td) {
  overflow-wrap: normal;
  hyphens: auto;
  padding: 6px 10px;
  border: 1px solid var(--ln-line-strong);
  text-align: start;
  vertical-align: top;
}
.history-message-body th { background: color-mix(in srgb, var(--ln-violet) 8%, transparent); }
.history-message-body code {
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: .9em;
  padding: 1px 5px;
  border-radius: 6px;
  background: color-mix(in srgb, var(--ln-violet) 13%, transparent);
}
.history-message-body pre {
  max-width: 100%;
  overflow-x: auto;
  margin: .6em 0;
  padding: .7rem .9rem;
  border-radius: 12px;
  background: color-mix(in srgb, var(--ln-ink) 7%, transparent);
}
.history-message-body pre code { padding: 0; background: none; }
.history-message-body .code-lang {
  display: block;
  font-size: .68rem;
  text-transform: uppercase;
  letter-spacing: .06em;
  opacity: .6;
}
.history-message-body blockquote {
  margin: 8px 0;
  padding: 4px 11px;
  border-inline-start: 3px solid color-mix(in srgb, var(--ln-violet) 45%, transparent);
}

/* Screen-reader-only text is absolutely positioned; without offsets it sits at its
   static position, and inside a horizontal scroller that escaped the scroller and widened
   the whole page (the About comparison table: 663px on a 390px phone). */
.visually-hidden {
  top: 0;
  left: 0;
}

/* Development-only AI mode badge: never wider than the screen. */
.ai-mode-badge {
  max-width: calc(100vw - 24px);
}

/* --- Phones and small tablets ------------------------------------------------------ */
@media (max-width: 820px) {
  /* iOS Safari zooms into any focused field whose text is under 16px, and the page
     then stays zoomed. 16px is the floor, not a design size. */
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]),
  select,
  textarea {
    font-size: max(16px, 1em);
  }

  /* Touch targets: 44px tall at least (Apple HIG / WCAG 2.5.5). Width follows content. */
  button,
  select,
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="hidden"]):not([type="file"]),
  .primary-button,
  .secondary-button,
  .app-brand {
    min-height: var(--ln-tap);
  }
  /* Formula pieces and in-text controls are not tap targets. */
  .katex button, .ln-math button {
    min-height: 0;
  }
  /* Controls whose own rules fix a smaller size (36px read-aloud and record buttons,
     the 27px chat close, the 38px password eye). */
  .listen-control :is(button, select),
  .speech-input-control button,
  .speech-record-button,
  .password-toggle,
  #chatClose {
    min-width: var(--ln-tap);
    min-height: var(--ln-tap);
  }
  textarea {
    min-height: var(--ln-tap);
  }
  input:is([type="checkbox"], [type="radio"]) {
    width: 20px;
    height: 20px;
  }
  /* Meta text that was set at 10-11.5px. */
  small, .asst-disclaimer, .card-field-label {
    font-size: max(12.5px, .8rem);
  }

  /* Small labels stay legible: uppercase eyebrows and the XP chip were 11-12.5px. */
  .eyebrow {
    font-size: max(12px, .78rem);
  }
  .xp-chip {
    font-size: 14px;
  }
}

/* --- Lesson reading on phones ------------------------------------------------------ */
@media (max-width: 760px) {
  .lesson-main {
    padding: 22px var(--ln-gutter) calc(110px + env(safe-area-inset-bottom));
  }
  .content-card {
    padding: 20px 18px;
  }
  .lesson-top h1 {
    font-size: clamp(1.75rem, 1.2rem + 3vw, 2.25rem);
    line-height: 1.12;
  }
  /* The worked-example problem is a sentence, not a page title: at 32px a three-line
     word problem filled the whole screen. */
  #exampleProblem, .content-card h2 {
    font-size: clamp(1.25rem, 1.05rem + 1.2vw, 1.5rem);
    line-height: 1.3;
  }
  .prose, #exampleSteps li, #teacherTips li, #exceptionsList li, .question-prompt {
    font-size: var(--ln-text-body);
    line-height: var(--ln-leading-read);
  }
  #exampleSteps li + li {
    margin-top: .55em;
  }
}

/* --- Tutor chat on phones: a bottom sheet that respects the home indicator ---------- */
@media (max-width: 520px) {
  /* The panel lives inside the lesson's stacking context, below the sticky header, so
     it starts under the header instead of hiding its own title and close button. */
  .chat-panel {
    top: auto;
    right: 0;
    bottom: 0;
    left: 0;
    width: 100%;
    height: calc(100dvh - var(--app-nav-height) - 10px);
    border-radius: 20px 20px 0 0;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .chat-toggle {
    right: var(--ln-gutter);
    bottom: calc(16px + env(safe-area-inset-bottom));
    min-height: 48px;
  }
  .chat-controls {
    flex-wrap: wrap;
    overflow-x: visible;
  }
  .chat-messages .message {
    font-size: var(--ln-text-body);
    line-height: 1.55;
  }
}

/* --- Comfort ------------------------------------------------------------------------ */
:where(a, button, input, select, textarea, summary, [tabindex]):focus-visible {
  outline: 3px solid var(--ln-focus);
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
    scroll-behavior: auto !important;
  }
}

/* --- Practice exam (exam_take.html) ----------------------------------------------- */
/* One column on phones, but a minmax(0, 1fr) one: a plain 1fr track cannot shrink below
   its content's minimum width, so the eight-wide question nav widened the column to
   544px on a 390px phone and the centred shell spilled off both edges. The nav keeps
   its own horizontal scroll inside the column. */
@media (max-width: 800px) {
  .exam-shell, .exam-question-list { grid-template-columns: minmax(0, 1fr); }
  .exam-sidebar, .exam-question-list, .exam-question { min-width: 0; }
  /* The header wraps instead of pushing the timer off the edge; autosave stays visible
     below it (it reports whether answers are saved, which matters most on a phone). */
  .exam-header { flex-wrap: wrap; gap: 4px 12px; min-height: 0; padding: 8px 12px; }
  .exam-header > div { min-width: 0; }
  .exam-question-meta { flex-wrap: wrap; }
}
