/* ============================================================
   MATH LESSON LAYER — css/lesson.css
   ------------------------------------------------------------
   Loaded AFTER design-system/styles.css and css/interactive-tool.css.
   Everything here is the part a maths lesson needs that a general
   interactive tool does not: a pre-lesson diagnostic, formulas,
   numbered working, an applications grid, a graded end quiz and
   the end-of-lesson survey.

   Every colour resolves to a design-system token — see
   docs/COLOR_THEME.md. Nothing in this file may declare a literal
   opaque surface or text colour: the light/dark switcher flips
   data-theme, and a frozen #111 here would freeze it on every
   lesson at once. Accent tints and low-alpha washes are fine, and
   are all built with color-mix over a token.
   ============================================================ */

/* ---- Meaning colours, one value per theme ----------------------------
   True/false, right/wrong and "these rows differ" carry meaning, so they
   cannot come from the neutral ramp. They also cannot come straight from
   --success-500 / --danger-500 / --warning-500: those are tuned for the
   near-black surfaces and the light theme does not currently retune them,
   so as TEXT on white they land around 2.3:1 — unreadable, and the exact
   failure docs/COLOR_THEME.md rule 6 describes.

   Declared as a pair here rather than fixed globally on purpose: changing
   the shared status tokens would repaint 150-odd other pages, which is a
   separate change with a separate blast radius. These are scoped to the
   lesson layer and only ever used for text and glyphs — the tinted borders
   and low-alpha washes below still build on the shared tokens, where the
   dark-tuned hue is fine because it is being mixed, not read.

   Light values verified against #ffffff: green 5.4:1, amber 5.9:1,
   red 5.9:1 — all clear of the 4.5:1 text minimum. */

:root {
    --lesson-true: var(--success-bright);
    --lesson-wrong: var(--danger-bright);
    --lesson-differ: var(--warning-500);
}

[data-theme="light"] {
    --lesson-true: #0f7a45;
    --lesson-wrong: #c02626;
    --lesson-differ: #8a5a00;
}

/* ---- Lesson stage indicator ------------------------------------------
   A lesson is a sequence, and the learner should be able to see where they
   are in it without scrolling. Marks are text + position, never colour
   alone. */

.lesson-stages {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin: 0 0 28px;
    padding: 0;
    list-style: none;
    font: var(--fw-medium) 12px/1 var(--font-mono);
    letter-spacing: var(--tracking-mono);
}

.lesson-stages li {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 7px 11px;
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    color: var(--text-tertiary);
    background: var(--surface-1);
}

.lesson-stages li[aria-current="step"] {
    border-color: var(--accent-line);
    background: var(--accent-subtle);
    color: var(--text-accent);
}

.lesson-stages li[data-done="true"] {
    color: var(--text-secondary);
    border-color: var(--border-default);
}

.lesson-stages li[data-done="true"]::before {
    content: "✓";
    font-weight: var(--fw-semibold);
    color: var(--lesson-true);
}

/* ---- Scenario -------------------------------------------------------
   The concrete situation, before any notation appears. Set apart from the
   body copy so it reads as "here is the world", not "here is the theory". */

.scenario {
    position: relative;
    margin: 0 0 8px;
    padding: 22px 24px 22px 28px;
    border: 1px solid var(--border-default);
    border-left: 3px solid var(--accent);
    border-radius: var(--radius-md);
    background: var(--surface-1);
}

.scenario p {
    margin: 0 0 12px;
    line-height: 1.7;
    color: var(--text-primary);
}

.scenario p:last-child { margin-bottom: 0; }

.scenario code,
.lesson-prose code {
    padding: 2px 6px;
    border-radius: var(--radius-sm);
    background: var(--surface-3);
    font: 0.92em/1.4 var(--font-mono);
    color: var(--text-primary);
}

/* ---- Prose ----------------------------------------------------------- */

.lesson-prose {
    max-width: 68ch;
    line-height: 1.75;
    color: var(--text-secondary);
}

.lesson-prose > p { margin: 0 0 16px; }
.lesson-prose strong { color: var(--text-primary); font-weight: var(--fw-semibold); }

.lesson-prose h3 {
    margin: 32px 0 12px;
    font: var(--fw-semibold) 17px/1.35 var(--font-sans);
    color: var(--text-primary);
}

/* ---- Maths -----------------------------------------------------------
   Formulas are MathML. It is native in every current browser, needs no
   library, is read correctly by screen readers, and — the reason it wins
   here over an image or hand-built spans — it inherits currentColor, so it
   survives the theme switcher with no JavaScript and no second palette.

   Sizes are set in em so a formula tracks the text it sits in. */

math {
    font-size: 1.08em;
    color: var(--text-primary);
}

.formula {
    display: block;
    margin: 20px 0;
    padding: 18px 20px;
    overflow-x: auto;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
    background: var(--surface-inset);
    text-align: center;
}

.formula math { font-size: 1.2em; }

/* A named formula carries its name in the margin rather than a caption
   underneath, so the eye reaches the maths first. */
.formula[data-name]::before {
    content: attr(data-name);
    display: block;
    margin-bottom: 10px;
    font: var(--fw-medium) 11px/1 var(--font-mono);
    letter-spacing: var(--tracking-mono);
    text-transform: uppercase;
    color: var(--text-tertiary);
}

/* ---- Worked steps ----------------------------------------------------
   Every line of the derivation, numbered, each with the reason it is
   allowed. The reason column is the part textbooks omit and the part that
   makes the working reproducible. */

.working {
    counter-reset: step;
    margin: 20px 0;
    padding: 0;
    list-style: none;
}

.working > li {
    counter-increment: step;
    position: relative;
    display: grid;
    grid-template-columns: 28px minmax(0, 1fr);
    gap: 4px 14px;
    padding: 16px 0;
    border-top: 1px solid var(--border-subtle);
}

.working > li:last-child { border-bottom: 1px solid var(--border-subtle); }

.working > li::before {
    content: counter(step);
    grid-row: 1 / span 2;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 1px solid var(--border-default);
    background: var(--surface-2);
    font: var(--fw-semibold) 12px/1 var(--font-mono);
    color: var(--text-secondary);
}

.working .step-math {
    align-self: center;
    overflow-x: auto;
    color: var(--text-primary);
}

.working .step-why {
    grid-column: 2;
    font-size: 14px;
    line-height: 1.6;
    color: var(--text-secondary);
}

.working .step-why b {
    color: var(--text-primary);
    font-weight: var(--fw-semibold);
}

/* ---- Truth tables and small data tables ------------------------------ */

.lesson-table-wrap { overflow-x: auto; margin: 20px 0; }

.lesson-table {
    width: 100%;
    border-collapse: collapse;
    font: 14px/1.5 var(--font-mono);
}

.lesson-table caption {
    margin-bottom: 10px;
    text-align: left;
    font: var(--fw-medium) 12px/1.4 var(--font-sans);
    letter-spacing: var(--tracking-mono);
    text-transform: uppercase;
    color: var(--text-tertiary);
}

.lesson-table th,
.lesson-table td {
    padding: 9px 14px;
    text-align: center;
    border: 1px solid var(--border-subtle);
}

.lesson-table thead th {
    background: var(--surface-2);
    color: var(--text-primary);
    font-weight: var(--fw-semibold);
    white-space: nowrap;
}

.lesson-table tbody td { color: var(--text-secondary); }

/* Truth values are marked by glyph and text, not by colour alone. */
.lesson-table .is-true { color: var(--lesson-true); font-weight: var(--fw-semibold); }
.lesson-table .is-false { color: var(--text-tertiary); }

.lesson-table tbody tr.is-highlight td {
    background: var(--accent-subtle);
    color: var(--text-primary);
}

/* ---- Applications ----------------------------------------------------- */

.applications {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 14px;
    margin: 20px 0 0;
    padding: 0;
    list-style: none;
}

.application {
    padding: 18px 20px;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
    background: var(--surface-1);
}

.application h3 {
    margin: 0 0 8px;
    font: var(--fw-semibold) 15px/1.35 var(--font-sans);
    color: var(--text-primary);
}

.application p {
    margin: 0;
    font-size: 14px;
    line-height: 1.65;
    color: var(--text-secondary);
}

.application .application-domain {
    display: block;
    margin-bottom: 6px;
    font: var(--fw-medium) 11px/1 var(--font-mono);
    letter-spacing: var(--tracking-mono);
    text-transform: uppercase;
    color: var(--text-tertiary);
}

/* ---- Quiz (pre and post share these) ---------------------------------- */

.quiz-question {
    margin: 0 0 24px;
    padding: 0;
    border: 0;
}

.quiz-question:last-of-type { margin-bottom: 0; }

.quiz-question legend {
    display: block;
    margin: 0 0 12px;
    padding: 0;
    font: var(--fw-semibold) 15px/1.5 var(--font-sans);
    color: var(--text-primary);
}

.quiz-question legend .quiz-number {
    margin-right: 8px;
    font: var(--fw-semibold) 12px/1 var(--font-mono);
    color: var(--text-tertiary);
}

.quiz-options {
    display: grid;
    gap: 8px;
}

.quiz-option {
    display: flex;
    align-items: flex-start;
    gap: 11px;
    padding: 12px 14px;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
    background: var(--surface-2);
    cursor: pointer;
    line-height: 1.55;
    color: var(--text-secondary);
    transition: border-color var(--dur-fast) var(--ease-out),
                background var(--dur-fast) var(--ease-out);
}

.quiz-option:hover { border-color: var(--border-strong); }

.quiz-option:has(input:checked) {
    border-color: var(--border-accent);
    background: var(--accent-subtle);
    color: var(--text-primary);
}

.quiz-option:has(input:focus-visible) {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.quiz-option input { margin: 4px 0 0; flex: none; }

.quiz-option:has(input:disabled) { cursor: default; }

/* Graded states. Each also gets a text marker in the label via JS, because a
   learner who cannot distinguish the two hues still has to be able to read
   the result. */
.quiz-option.is-correct {
    border-color: color-mix(in srgb, var(--success-500) 60%, transparent);
    background: color-mix(in srgb, var(--success-500) 12%, var(--surface-2));
    color: var(--text-primary);
}

.quiz-option.is-wrong {
    border-color: color-mix(in srgb, var(--danger-500) 55%, transparent);
    background: color-mix(in srgb, var(--danger-500) 10%, var(--surface-2));
    color: var(--text-primary);
}

.quiz-mark {
    margin-left: auto;
    padding-left: 12px;
    font: var(--fw-semibold) 12px/1.5 var(--font-mono);
    white-space: nowrap;
}

.is-correct .quiz-mark { color: var(--lesson-true); }
.is-wrong .quiz-mark { color: var(--lesson-wrong); }

/* ---- Result / feedback banners ---------------------------------------- */

.lesson-note {
    margin: 20px 0 0;
    padding: 16px 18px;
    border: 1px solid var(--accent-line);
    border-radius: var(--radius-md);
    background: var(--accent-subtle);
    line-height: 1.65;
    color: var(--text-primary);
}

.lesson-note p { margin: 0 0 10px; }
.lesson-note p:last-child { margin-bottom: 0; }

.lesson-note.is-pass {
    border-color: color-mix(in srgb, var(--success-500) 50%, transparent);
    background: color-mix(in srgb, var(--success-500) 10%, var(--surface-1));
}

.lesson-note.is-fail {
    border-color: color-mix(in srgb, var(--warning-500) 50%, transparent);
    background: color-mix(in srgb, var(--warning-500) 10%, var(--surface-1));
}

.lesson-note.is-quiet {
    border-color: var(--border-default);
    background: var(--surface-2);
    color: var(--text-secondary);
}

/* The pre → post comparison, shown only once both numbers exist. */
.gain-readout {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 10px;
    margin-top: 14px;
    padding-top: 14px;
    border-top: 1px solid var(--border-default);
    font: var(--fw-semibold) 15px/1.4 var(--font-mono);
    color: var(--text-primary);
}

.gain-readout .gain-delta { color: var(--lesson-true); }
.gain-readout .gain-label {
    font: var(--fw-medium) 12px/1.4 var(--font-sans);
    letter-spacing: var(--tracking-mono);
    text-transform: uppercase;
    color: var(--text-tertiary);
}

/* ---- Survey ----------------------------------------------------------- */

.survey-scale {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin: 0 0 6px;
    padding: 0;
    border: 0;
}

.survey-scale legend {
    margin: 0 0 10px;
    padding: 0;
    font: var(--fw-semibold) 15px/1.4 var(--font-sans);
    color: var(--text-primary);
}

.survey-scale .scale-row {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    width: 100%;
}

.scale-option {
    position: relative;
    flex: 1 1 auto;
    min-width: 64px;
    padding: 11px 8px;
    text-align: center;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
    background: var(--surface-2);
    color: var(--text-secondary);
    font: var(--fw-medium) 14px/1.3 var(--font-mono);
    cursor: pointer;
    transition: border-color var(--dur-fast) var(--ease-out),
                background var(--dur-fast) var(--ease-out);
}

.scale-option:hover { border-color: var(--border-strong); }

.scale-option:has(input:checked) {
    border-color: var(--border-accent);
    background: var(--accent-subtle);
    color: var(--text-accent);
}

.scale-option:has(input:focus-visible) {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* The radio itself is visually replaced by the tile, but stays in the layout
   and keeps its focus ring — never display:none, which removes it from the
   accessibility tree in some engines. */
.scale-option input {
    position: absolute;
    opacity: 0;
    width: 1px;
    height: 1px;
    margin: 0;
}

.scale-ends {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    margin: 0 0 22px;
    font-size: 12px;
    color: var(--text-tertiary);
}

.survey-field { margin: 0 0 18px; }

.survey-field label {
    display: block;
    margin-bottom: 7px;
    font: var(--fw-semibold) 14px/1.4 var(--font-sans);
    color: var(--text-primary);
}

.survey-field .survey-hint {
    display: block;
    margin-bottom: 8px;
    font: 13px/1.5 var(--font-sans);
    font-weight: var(--fw-regular);
    color: var(--text-tertiary);
}

.survey-field textarea {
    width: 100%;
    min-height: 76px;
    padding: 11px 13px;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
    background: var(--surface-2);
    color: var(--text-primary);
    font: 14px/1.6 var(--font-sans);
    resize: vertical;
}

.survey-field textarea:focus {
    outline: none;
    border-color: var(--border-accent);
    box-shadow: 0 0 0 3px var(--accent-subtle);
}

/* ---- Signed-out / unavailable state ----------------------------------- */

.lesson-gate {
    padding: 18px 20px;
    border: 1px dashed var(--border-strong);
    border-radius: var(--radius-md);
    background: var(--surface-1);
    line-height: 1.65;
    color: var(--text-secondary);
}

.lesson-gate a { color: var(--text-accent); }

.is-hidden { display: none !important; }

/* ---- Motion ----------------------------------------------------------- */

@media (prefers-reduced-motion: reduce) {
    .quiz-option,
    .scale-option,
    .lesson-stages li {
        transition: none;
    }
}

@media (max-width: 640px) {
    .working > li { grid-template-columns: 24px minmax(0, 1fr); }
    .formula { padding: 14px 12px; }
}
