/*
 * Mobile-first, and literally so: every rule below is written for a phone held one-handed, with the
 * only media query widening the layout on a larger screen rather than the reverse.
 *
 * Two constraints beyond taste:
 *   - Tap targets are at least 44px, because the audience is often a tired teenager on a cracked
 *     screen at 9pm.
 *   - Nothing is conveyed by colour alone. The AI-assistance badge and every notice carry text and
 *     an icon glyph, so they survive greyscale, colour blindness and a screen reader.
 */

:root {
  --ink: #16202e;
  --ink-soft: #4a5568;
  --paper: #ffffff;
  --canvas: #f4f6fb;
  --line: #d9dfea;
  --brand: #2f4b8f;
  --brand-ink: #ffffff;
  --success: #1b6b3f;
  --success-bg: #e6f4ec;
  --warning: #8a5300;
  --warning-bg: #fdf1de;
  --refusal: #8c2f2f;
  --refusal-bg: #fbeaea;
  --limit-bg: #eef1f7;
  --radius: 14px;
  --gap: 1rem;
}

@media (prefers-color-scheme: dark) {
  :root {
    --ink: #e8ecf4;
    --ink-soft: #a8b3c7;
    --paper: #1a2230;
    --canvas: #111826;
    --line: #2f3b4f;
    --brand: #7ea2ee;
    --brand-ink: #10182a;
    --success: #7ddba4;
    --success-bg: #14301f;
    --warning: #f0c07a;
    --warning-bg: #33260f;
    --refusal: #f0a0a0;
    --refusal-bg: #331616;
    --limit-bg: #1e2637;
  }
}

* {
  box-sizing: border-box;
}

/*
 * The app hides the answer field and the photo preview with the `hidden` attribute. That works
 * through a UA rule of `[hidden] { display: none }`, which any author rule setting `display` beats
 * -- `.field { display: block }` alone was enough to leave the answer box on screen in Practice
 * mode. Restating it here, with the weight to win, keeps `hidden` meaning hidden.
 */
[hidden] {
  display: none !important;
}

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

body {
  margin: 0;
  padding: 0 max(1rem, env(safe-area-inset-left)) calc(2rem + env(safe-area-inset-bottom));
  background: var(--canvas);
  color: var(--ink);
  font: 16px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", "Noto Naskh Arabic", sans-serif;
}

main {
  max-width: 40rem;
  margin: 0 auto;
}

/* ---- masthead ---------------------------------------------------------------------------- */

.masthead {
  max-width: 40rem;
  margin: 0 auto;
  padding: 1.25rem 0 0.5rem;
}

.masthead-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--gap);
}

h1 {
  margin: 0;
  font-size: 1.4rem;
  letter-spacing: -0.01em;
}

.tagline {
  margin: 0.25rem 0 0.75rem;
  color: var(--ink-soft);
  font-size: 0.95rem;
}

.language-picker select {
  min-height: 44px;
  padding: 0 0.6rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--paper);
  color: var(--ink);
  font-size: 0.95rem;
}

/*
 * The assistance banner is permanent chrome, not a dismissible toast. A learner must not be able to
 * screenshot the output without the label being part of the page.
 */
.assistance-banner {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.5rem;
  margin: 0 0 1rem;
  padding: 0.6rem 0.75rem;
  background: var(--limit-bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  font-size: 0.85rem;
  color: var(--ink-soft);
}

.badge {
  flex: none;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  background: var(--brand);
  color: var(--brand-ink);
  font-size: 0.75rem;
  font-weight: 650;
  letter-spacing: 0.01em;
}

/* ---- cards and fields -------------------------------------------------------------------- */

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

.card h2 {
  margin: 0 0 0.75rem;
  font-size: 1rem;
}

.field {
  display: block;
  margin-top: 0.85rem;
}

.field > span {
  display: block;
  margin-bottom: 0.3rem;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--ink-soft);
}

textarea,
input[type="text"] {
  width: 100%;
  min-height: 48px;
  padding: 0.6rem 0.7rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--canvas);
  color: var(--ink);
  /* 1.05rem keeps iOS from zooming the viewport on focus, which on a phone feels like a bug. */
  font: 1.05rem/1.4 ui-monospace, "SF Mono", "Cascadia Mono", Menlo, monospace;
  resize: vertical;
}

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

.extra {
  margin-top: 0.85rem;
  font-size: 0.9rem;
  color: var(--ink-soft);
}

.extra summary {
  min-height: 44px;
  display: flex;
  align-items: center;
  cursor: pointer;
}

.extra input {
  margin-top: 0.4rem;
  font-family: inherit;
  font-size: 1rem;
}

/* ---- intake ------------------------------------------------------------------------------ */

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

.preview {
  margin: 0.85rem 0 0;
}

.preview img {
  display: block;
  width: 100%;
  max-height: 45vh;
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--canvas);
}

.preview figcaption {
  margin-top: 0.5rem;
}

/* ---- modes ------------------------------------------------------------------------------- */

.modes {
  display: grid;
  gap: 0.5rem;
}

.mode {
  display: block;
  position: relative;
}

.mode input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.mode-body {
  display: block;
  min-height: 44px;
  padding: 0.6rem 0.75rem;
  border: 1.5px solid var(--line);
  border-radius: 12px;
  background: var(--canvas);
  cursor: pointer;
}

.mode input:checked + .mode-body {
  border-color: var(--brand);
  background: var(--paper);
  /* Not colour alone: the selected mode is also marked by weight and a leading tick. */
  box-shadow: inset 0 0 0 1.5px var(--brand);
}

.mode input:checked + .mode-body .mode-label::before {
  content: "✓ ";
}

.mode input:focus-visible + .mode-body {
  outline: 3px solid var(--brand);
  outline-offset: 2px;
}

.mode-label {
  display: block;
  font-weight: 650;
}

.mode-blurb {
  display: block;
  font-size: 0.85rem;
  color: var(--ink-soft);
}

/* ---- buttons ----------------------------------------------------------------------------- */

.actions {
  display: flex;
  gap: 0.6rem;
  align-items: center;
  margin-bottom: 1.25rem;
}

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 48px;
  padding: 0 1.1rem;
  border: 1.5px solid var(--line);
  border-radius: 12px;
  background: var(--paper);
  color: var(--ink);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}

.button-primary {
  flex: 1;
  border-color: var(--brand);
  background: var(--brand);
  color: var(--brand-ink);
}

.button-quiet {
  background: transparent;
  border-color: transparent;
  color: var(--ink-soft);
  text-decoration: underline;
}

.button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

/* ---- response ---------------------------------------------------------------------------- */

.response:empty {
  display: none;
}

.response {
  margin-bottom: var(--gap);
}

.steps {
  margin: 0;
  padding-inline-start: 1.5rem;
  display: grid;
  gap: 0.7rem;
}

.steps li {
  padding-inline-start: 0.2rem;
}

.step-expression {
  display: block;
  margin-top: 0.3rem;
  padding: 0.4rem 0.6rem;
  background: var(--canvas);
  border-radius: 8px;
  font-family: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, monospace;
  /*
   * `plaintext`, not a forced direction. Each formula takes its direction from its own first strong
   * character, which is the only rule that is right for both scripts: forcing `ltr` rendered the
   * Arabic working `3س = 15` as `315 = س`, and forcing `rtl` would mangle the Latin working the same
   * way. Verified in Chromium against both.
   */
  unicode-bidi: plaintext;
  text-align: start;
}

.notice {
  margin: 0 0 0.75rem;
  padding: 0.65rem 0.8rem;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--limit-bg);
}

.notice::before {
  font-weight: 700;
  margin-inline-end: 0.4rem;
}

.notice-success {
  background: var(--success-bg);
  border-color: var(--success);
  color: var(--success);
}
.notice-success::before { content: "✓"; }

.notice-warning {
  background: var(--warning-bg);
  border-color: var(--warning);
  color: var(--warning);
}
.notice-warning::before { content: "!"; }

.notice-refusal {
  background: var(--refusal-bg);
  border-color: var(--refusal);
  color: var(--refusal);
}
.notice-refusal::before { content: "✕"; }

.notice-prompt::before { content: "→"; }
.notice-limitation::before { content: "i"; }

.practice-list {
  margin: 0;
  padding: 0;
  list-style: none;
  display: grid;
  gap: 0.6rem;
}

.practice-item {
  padding: 0.7rem 0.8rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--canvas);
}

.practice-label {
  display: block;
  font-size: 0.8rem;
  font-weight: 650;
  color: var(--ink-soft);
}

.practice-text {
  display: block;
  margin: 0.2rem 0 0.5rem;
  font-family: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, monospace;
  font-size: 1.05rem;
  /* Same reasoning as `.step-expression`. */
  unicode-bidi: plaintext;
  text-align: start;
}

.practice-item summary {
  min-height: 44px;
  display: flex;
  align-items: center;
  font-size: 0.9rem;
  color: var(--brand);
  cursor: pointer;
}

/*
 * The disclosure block is rendered with every answer and repeated at the end of the response, so a
 * learner copying the working out of the page has to scroll past it.
 */
.disclosure {
  margin-top: var(--gap);
  padding: 0.75rem 0.85rem;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  background: var(--paper);
  font-size: 0.85rem;
  color: var(--ink-soft);
}

.disclosure strong {
  color: var(--ink);
}

.citation {
  margin: 0.4rem 0 0;
  padding: 0.5rem 0.6rem;
  background: var(--canvas);
  border-radius: 8px;
  font-style: italic;
}

.hint-actions {
  margin-top: 0.75rem;
}

footer {
  max-width: 40rem;
  margin: 1.5rem auto 0;
  color: var(--ink-soft);
  font-size: 0.8rem;
  text-align: center;
}

footer p {
  margin: 0.3rem 0;
}

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

/* The only widening query: below this everything above already applies. */
@media (min-width: 34rem) {
  .modes {
    grid-template-columns: 1fr 1fr;
  }
  h1 {
    font-size: 1.6rem;
  }
}

@media (prefers-reduced-motion: no-preference) {
  .response > * {
    animation: rise 160ms ease-out both;
  }
  @keyframes rise {
    from { opacity: 0; transform: translateY(4px); }
    to { opacity: 1; transform: none; }
  }
}
