/*
 * Mobile-first, and literally so: every rule here is the phone rule, with a single widening
 * breakpoint at the end. The target device is a mid-range Android held one-handed in a corridor
 * between lessons, so the primary controls are thumb-sized and near the bottom of the reading
 * order, and nothing depends on hover.
 *
 * System fonts only. A webfont would add a render-blocking round trip to another origin for a page
 * whose entire promise is that it works on this device without sending anything anywhere.
 */

:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --ink: #111827;
  --ink-soft: #4b5563;
  --line: #e5e7eb;
  --accent: #1d4ed8;
  --accent-ink: #ffffff;
  --answer-bg: #ecfdf5;
  --answer-line: #6ee7b7;
  --error-bg: #fef2f2;
  --error-line: #fca5a5;
  --error-ink: #991b1b;
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1020;
    --surface: #151b2e;
    --ink: #eef2ff;
    --ink-soft: #a3adc2;
    --line: #2a3352;
    --accent: #6366f1;
    --answer-bg: #06281f;
    --answer-line: #0f766e;
    --error-bg: #2a1214;
    --error-line: #7f1d1d;
    --error-ink: #fca5a5;
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0 0 3rem;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  font-size: 17px;
  line-height: 1.55;
  -webkit-text-size-adjust: 100%;
}

main,
.masthead,
footer {
  max-width: 44rem;
  margin: 0 auto;
  padding: 0 1rem;
}

.masthead {
  padding-top: max(1rem, env(safe-area-inset-top));
  padding-bottom: 0.5rem;
}

.masthead-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

h1 {
  font-size: 1.4rem;
  margin: 0.5rem 0 0.25rem;
}

h2 {
  font-size: 1.05rem;
  margin: 0 0 0.4rem;
}

#tagline {
  margin: 0 0 0.5rem;
  color: var(--ink-soft);
  font-size: 0.95rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1rem;
  margin: 0.75rem 0;
}

.hint {
  color: var(--ink-soft);
  font-size: 0.88rem;
  margin: 0 0 0.6rem;
}

/* Controls -------------------------------------------------------------------------------- */

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  /* 48px is the smallest reliable thumb target; these are the only controls that matter. */
  min-height: 48px;
  padding: 0 1rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
}

.button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

.button.wide {
  flex: 1 1 auto;
}

.button:active {
  transform: translateY(1px);
}

.button:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.capture {
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
}

.capture .button {
  flex: 1 1 10rem;
}

select {
  min-height: 44px;
  padding: 0 0.5rem;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  font: inherit;
}

textarea {
  width: 100%;
  padding: 0.7rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  color: var(--ink);
  /* Monospace so `x^2` and `1/2` line up and a stray character is visible when checking. */
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 1.05rem;
  resize: vertical;
}

.controls {
  display: flex;
  gap: 0.6rem;
  align-items: flex-end;
  flex-wrap: wrap;
  margin-top: 0.75rem;
}

.detail {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.85rem;
  color: var(--ink-soft);
}

.examples {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
  margin-top: 0.6rem;
}

.example {
  border: 1px dashed var(--line);
  border-radius: 999px;
  background: transparent;
  color: var(--ink-soft);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.85rem;
  padding: 0.35rem 0.7rem;
  min-height: 36px;
  cursor: pointer;
}

/* Image preview --------------------------------------------------------------------------- */

.preview {
  margin: 0.75rem 0 0;
}

.preview img {
  width: 100%;
  max-height: 40vh;
  object-fit: contain;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg);
}

.preview figcaption {
  color: var(--ink-soft);
  font-size: 0.85rem;
  margin-top: 0.4rem;
}

/* Result ---------------------------------------------------------------------------------- */

.answer {
  margin: 0;
  padding: 0.75rem 1rem;
  background: var(--answer-bg);
  border: 1px solid var(--answer-line);
  border-radius: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 1.3rem;
  font-weight: 700;
  overflow-wrap: anywhere;
}

/* The step number is drawn with a counter rather than a list marker.
 *
 * A native `::marker` aligns to the baseline of the item's first line box, and the atomic maths box
 * changes that baseline — so on a wrapped step the number drifted onto the second row. A flex row
 * with the counter as the first item puts the number beside the top of the step, in both writing
 * directions, and stops the layout depending on inline metrics at all. */
.steps {
  margin: 0.5rem 0 0;
  padding-inline-start: 0;
  list-style: none;
  counter-reset: step;
}

.steps li {
  display: flex;
  gap: 0.5rem;
  /* Tops, not baselines: the atomic maths box moves the first line's baseline, which is the whole
   * reason the number drifted in the first place. */
  align-items: flex-start;
  margin-bottom: 0.7rem;
  counter-increment: step;
}

.steps li::before {
  content: counter(step) ".";
  flex: 0 0 auto;
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
}

/* The step's text and its optional explanation stack inside one flex item, so the number stays a
 * single column beside them rather than becoming a third sibling in the row. */
.steps li > .body {
  flex: 1 1 auto;
  min-width: 0;
}

/* A block, so the list marker aligns to the step's first line. Left inline, the atomic maths box
 * changes the first line box's baseline and the number drifts onto the wrong row. */
.steps .text {
  display: block;
  overflow-wrap: anywhere;
}

/* Mathematics reads left-to-right even inside a right-to-left sentence.
 *
 * `unicode-bidi: isolate` alone is not enough, and the difference only shows on a narrow screen:
 * an isolated inline run that *wraps* is still broken into line-level pieces which the paragraph's
 * right-to-left algorithm then orders right-to-left, so `(-5)^2 - 4(1)(6) = 1` came out with its
 * tail on the first line. `display: inline-block` makes the fragment an atomic box — the paragraph
 * places it as a single unit, and any wrapping happens *inside* it, in its own direction. */
.steps .text bdi,
.answer bdi {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  display: inline-block;
  direction: ltr;
  unicode-bidi: isolate;
  text-align: left;
  max-width: 100%;
}

.why {
  display: block;
  margin-top: 0.25rem;
  color: var(--ink-soft);
  font-size: 0.88rem;
  border-inline-start: 3px solid var(--line);
  padding-inline-start: 0.6rem;
}

.error {
  padding: 0.75rem 1rem;
  background: var(--error-bg);
  border: 1px solid var(--error-line);
  border-radius: 12px;
  color: var(--error-ink);
}

.disclosure {
  margin: 1rem 0 0.75rem;
  padding: 0.6rem 0.8rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--ink-soft);
  font-size: 0.85rem;
}

.integrity p {
  margin: 0 0 0.5rem;
  font-size: 0.92rem;
}

footer {
  color: var(--ink-soft);
  font-size: 0.85rem;
  text-align: center;
}

.notice {
  margin: 1rem;
  padding: 1rem;
  border: 1px solid var(--error-line);
  border-radius: 12px;
  background: var(--error-bg);
  color: var(--error-ink);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

@media (min-width: 40rem) {
  body {
    padding-bottom: 4rem;
  }
  h1 {
    font-size: 1.7rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
  }
  .button:active {
    transform: none;
  }
}
