/* ================================================================
   UNIT CONVERSIONS — STYLESHEET
   WCAG 2.1 AA compliant design system
   Sections:
     1.  Custom properties (design tokens)
     2.  Reset & base
     3.  Typography
     4.  Layout utilities
     5.  Skip link
     6.  Site header & navigation
     7.  Hero
     8.  Level selector & cards
     9.  Workspace
    10.  Fraction chain
    11.  Factor bank
    12.  Answer entry & feedback
    13.  Hint panel
    14.  Problem actions
    15.  Session summary
    16.  Site footer
    17.  Buttons
    18.  Tags & chips
    19.  Progress bar
    20.  Responsive — tablet (≥ 640 px)
    21.  Responsive — desktop (≥ 1024 px)
    22.  prefers-color-scheme: dark
    23.  prefers-contrast: more  (high-contrast override)
    24.  prefers-reduced-motion
================================================================ */


/* ================================================================
   1. CUSTOM PROPERTIES (DESIGN TOKENS)
================================================================ */
:root {
    /* Brand */
    --color-brand: #1a56a0;
    /* primary blue */
    --color-brand-dark: #0f3d78;
    --color-brand-light: #d0e4f7;
    --color-accent: #c8460a;
    /* warm orange — used sparingly */

    /* Semantic */
    --color-correct: #1a6e3b;
    --color-correct-bg: #d4edda;
    --color-incorrect: #8b1a1a;
    --color-incorrect-bg: #fde8e8;
    --color-hint: #5a4000;
    --color-hint-bg: #fff8e1;

    /* Neutral scale */
    --color-neutral-0: #ffffff;
    --color-neutral-50: #f7f8fa;
    --color-neutral-100: #eef0f3;
    --color-neutral-200: #d8dce3;
    --color-neutral-400: #9aa0ab;
    --color-neutral-600: #4f5769;
    --color-neutral-800: #1e2331;
    --color-neutral-900: #0d1017;

    /* Department accents */
    --color-physics: #1a56a0;
    --color-physics-bg: #d0e4f7;
    --color-chemistry: #276b42;
    --color-chemistry-bg: #d4edda;

    /* Typography */
    --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --font-mono: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;

    --text-xs: 0.75rem;
    /* 12 px */
    --text-sm: 0.875rem;
    /* 14 px */
    --text-base: 1rem;
    /* 16 px */
    --text-lg: 1.125rem;
    /* 18 px */
    --text-xl: 1.25rem;
    /* 20 px */
    --text-2xl: 1.5rem;
    /* 24 px */
    --text-3xl: 1.875rem;
    /* 30 px */
    --text-4xl: 2.25rem;
    /* 36 px */

    --font-weight-normal: 400;
    --font-weight-medium: 500;
    --font-weight-bold: 700;

    --line-height-tight: 1.25;
    --line-height-base: 1.6;
    --line-height-loose: 1.8;

    /* Spacing scale (0.25 rem steps) */
    --space-1: 0.25rem;
    --space-2: 0.5rem;
    --space-3: 0.75rem;
    --space-4: 1rem;
    --space-5: 1.25rem;
    --space-6: 1.5rem;
    --space-8: 2rem;
    --space-10: 2.5rem;
    --space-12: 3rem;
    --space-16: 4rem;
    --space-20: 5rem;

    /* Borders */
    --radius-sm: 4px;
    --radius-md: 8px;
    --radius-lg: 12px;
    --radius-xl: 16px;
    --radius-full: 9999px;

    --border-width: 2px;
    --border-color: var(--color-neutral-200);

    /* Shadows */
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, .08), 0 1px 2px rgba(0, 0, 0, .06);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, .10), 0 2px 4px rgba(0, 0, 0, .06);
    --shadow-lg: 0 10px 24px rgba(0, 0, 0, .12), 0 4px 8px rgba(0, 0, 0, .06);

    /* Focus ring — consistent, highly visible */
    --focus-ring: 0 0 0 3px var(--color-brand), 0 0 0 5px rgba(26, 86, 160, .25);

    /* Transitions */
    --transition-fast: 150ms ease;
    --transition-base: 250ms ease;
    --transition-slow: 400ms ease;

    /* Layout */
    --max-width: 1100px;
    --nav-height: 64px;
    --section-gap: var(--space-16);
}


/* ================================================================
   2. RESET & BASE
================================================================ */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    font-size: 100%;
    /* honour user browser font-size preference */
    scroll-behavior: smooth;
    -webkit-text-size-adjust: 100%;
}

body {
    font-family: var(--font-sans);
    font-size: var(--text-base);
    line-height: var(--line-height-base);
    color: var(--color-neutral-800);
    background-color: var(--color-neutral-50);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

main {
    flex: 1;
}

img,
svg {
    display: block;
    max-width: 100%;
}

ul,
ol {
    list-style: none;
}

/* Remove default button / input styles */
button,
input {
    font: inherit;
}

/* Focus-visible ring — applied globally so every interactive element
   gets a visible focus indicator (WCAG 2.4.7) */
:focus-visible {
    outline: 3px solid var(--color-brand);
    outline-offset: 3px;
    border-radius: var(--radius-sm);
}

/* Remove focus ring when using mouse (keeps it for keyboard) */
:focus:not(:focus-visible) {
    outline: none;
}

/* [hidden] attribute — ensure it always hides regardless of display overrides */
[hidden] {
    display: none !important;
}


/* ================================================================
   3. TYPOGRAPHY
================================================================ */
h1,
h2,
h3,
h4 {
    line-height: var(--line-height-tight);
    font-weight: var(--font-weight-bold);
    color: var(--color-neutral-800);
}

h1 {
    font-size: var(--text-3xl);
}

h2 {
    font-size: var(--text-2xl);
}

h3 {
    font-size: var(--text-xl);
}

h4 {
    font-size: var(--text-lg);
}

p {
    max-width: 65ch;
    /* prevent overly long lines (WCAG 1.4.8) */
}

a {
    color: var(--color-brand);
    text-decoration: underline;
    text-underline-offset: 2px;
}

a:hover {
    color: var(--color-brand-dark);
}

.section-heading {
    font-size: var(--text-2xl);
    margin-bottom: var(--space-6);
    text-align: center;
}

abbr[title] {
    text-decoration: underline dotted;
    cursor: help;
}


/* ================================================================
   4. LAYOUT UTILITIES
================================================================ */
.container {
    width: 100%;
    max-width: var(--max-width);
    margin-inline: auto;
    padding-inline: var(--space-4);
}

.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;
}


/* ================================================================
   5. SKIP LINK
================================================================ */
.skip-link {
    position: absolute;
    top: var(--space-2);
    left: var(--space-2);
    z-index: 9999;
    padding: var(--space-2) var(--space-4);
    background: var(--color-brand);
    color: var(--color-neutral-0);
    font-weight: var(--font-weight-bold);
    border-radius: var(--radius-md);
    text-decoration: none;
    /* Hidden until focused */
    transform: translateY(-150%);
    transition: transform var(--transition-fast);
}

.skip-link:focus-visible {
    transform: translateY(0);
    outline: 3px solid var(--color-neutral-0);
    outline-offset: 2px;
}


/* ================================================================
   6. SITE HEADER & NAVIGATION
================================================================ */
.site-header {
    position: sticky;
    top: 0;
    z-index: 100;
    height: var(--nav-height);
    background: var(--color-neutral-0);
    border-bottom: var(--border-width) solid var(--border-color);
    box-shadow: var(--shadow-sm);
}

.site-header__inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 100%;
    max-width: var(--max-width);
    margin-inline: auto;
    padding-inline: var(--space-4);
}

.site-header__brand {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    text-decoration: none;
    color: inherit;
}

.site-header__logo {
    flex-shrink: 0;
}

.site-header__title {
    font-size: var(--text-lg);
    font-weight: var(--font-weight-bold);
    color: var(--color-brand);
    white-space: nowrap;
}

/* ── Primary nav ──────────────────────────────────────────────── */
.primary-nav__list {
    display: flex;
    align-items: center;
    gap: var(--space-1);
}

.primary-nav__link {
    display: inline-flex;
    align-items: center;
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-md);
    font-size: var(--text-sm);
    font-weight: var(--font-weight-medium);
    color: var(--color-neutral-600);
    text-decoration: none;
    transition: background var(--transition-fast), color var(--transition-fast);
}

.primary-nav__link:hover,
.primary-nav__link[aria-current="page"] {
    background: var(--color-brand-light);
    color: var(--color-brand-dark);
}

.primary-nav__link[aria-current="page"] {
    font-weight: var(--font-weight-bold);
}

/* Hamburger (mobile) — hidden by default on wide screens */
.primary-nav__toggle {
    display: none;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    border: var(--border-width) solid var(--border-color);
    border-radius: var(--radius-md);
    background: transparent;
    cursor: pointer;
}

.hamburger,
.hamburger::before,
.hamburger::after {
    display: block;
    width: 20px;
    height: 2px;
    background: var(--color-neutral-600);
    border-radius: var(--radius-full);
    transition: transform var(--transition-base), opacity var(--transition-fast);
}

.hamburger {
    position: relative;
}

.hamburger::before,
.hamburger::after {
    content: "";
    position: absolute;
    left: 0;
}

.hamburger::before {
    top: -6px;
}

.hamburger::after {
    top: 6px;
}


/* ================================================================
   7. HERO
================================================================ */
.hero {
    background: linear-gradient(135deg, var(--color-brand) 0%, var(--color-brand-dark) 100%);
    color: var(--color-neutral-0);
    padding-block: var(--space-16) var(--space-20);
    text-align: center;
}

.hero__inner {
    max-width: 700px;
    margin-inline: auto;
    padding-inline: var(--space-4);
}

.hero__heading {
    font-size: var(--text-4xl);
    color: var(--color-neutral-0);
    margin-bottom: var(--space-4);
}

.hero__sub {
    font-size: var(--text-lg);
    line-height: var(--line-height-loose);
    color: rgba(255, 255, 255, .88);
    max-width: 55ch;
    margin-inline: auto;
    margin-bottom: var(--space-8);
}


/* ================================================================
   8. LEVEL SELECTOR & CARDS
================================================================ */
.level-selector {
    padding-block: var(--section-gap);
    background: var(--color-neutral-0);
}

/* Department filter chips */
.filter-group {
    border: none;
    margin-bottom: var(--space-8);
    text-align: center;
}

.filter-group__legend {
    font-size: var(--text-sm);
    font-weight: var(--font-weight-medium);
    color: var(--color-neutral-600);
    text-transform: uppercase;
    letter-spacing: .05em;
    margin-bottom: var(--space-3);
    display: block;
    width: 100%;
}

.filter-group__options {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    gap: var(--space-2);
}

.chip-label {
    cursor: pointer;
}

.chip-input {
    /* Visually hidden but still keyboard/AT accessible */
    position: absolute;
    opacity: 0;
    width: 1px;
    height: 1px;
}

.chip {
    display: inline-flex;
    align-items: center;
    padding: var(--space-2) var(--space-4);
    border: var(--border-width) solid var(--border-color);
    border-radius: var(--radius-full);
    font-size: var(--text-sm);
    font-weight: var(--font-weight-medium);
    color: var(--color-neutral-600);
    background: var(--color-neutral-0);
    transition: border-color var(--transition-fast),
        background var(--transition-fast),
        color var(--transition-fast);
}

.chip-input:checked+.chip {
    border-color: var(--color-brand);
    background: var(--color-brand);
    color: var(--color-neutral-0);
}

.chip-input:focus-visible+.chip {
    outline: 3px solid var(--color-brand);
    outline-offset: 3px;
    border-radius: var(--radius-full);
}

.chip-label:hover .chip {
    border-color: var(--color-brand);
    color: var(--color-brand);
}

/* Level cards grid */
.level-cards {
    display: grid;
    grid-template-columns: 1fr;
    gap: var(--space-6);
}

.level-card {
    position: relative;
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
    padding: var(--space-6);
    background: var(--color-neutral-0);
    border: var(--border-width) solid var(--border-color);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-sm);
    transition: box-shadow var(--transition-base), border-color var(--transition-base);
}

.level-card:hover {
    box-shadow: var(--shadow-md);
    border-color: var(--color-brand-light);
}

.level-card__badge {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--color-brand-light);
    color: var(--color-brand-dark);
    font-size: var(--text-xl);
    font-weight: var(--font-weight-bold);
    border-radius: var(--radius-md);
    flex-shrink: 0;
}

.level-card__heading {
    font-size: var(--text-xl);
    color: var(--color-brand-dark);
}

.level-card__desc {
    font-size: var(--text-sm);
    color: var(--color-neutral-600);
    line-height: var(--line-height-loose);
    flex: 1;
    max-width: none;
}

.level-card__tags {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    margin-top: auto;
}

.level-card__cta {
    align-self: flex-start;
    margin-top: var(--space-2);
}


/* ================================================================
   9. WORKSPACE
================================================================ */
.workspace {
    padding-block: var(--space-10);
    background: var(--color-neutral-50);
}

.workspace__header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: var(--space-4);
    margin-bottom: var(--space-4);
}

.workspace__meta {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    flex-wrap: wrap;
}

.workspace__level-label {
    font-size: var(--text-xl);
    color: var(--color-neutral-800);
}

.workspace__dept-badge {
    padding: var(--space-1) var(--space-3);
    border-radius: var(--radius-full);
    font-size: var(--text-xs);
    font-weight: var(--font-weight-bold);
    text-transform: uppercase;
    letter-spacing: .06em;
}

.workspace__dept-badge[data-dept="physics"] {
    background: var(--color-physics-bg);
    color: var(--color-physics);
}

.workspace__dept-badge[data-dept="chemistry"] {
    background: var(--color-chemistry-bg);
    color: var(--color-chemistry);
}

.workspace__controls {
    display: flex;
    align-items: center;
    gap: var(--space-4);
}

.workspace__progress {
    font-size: var(--text-sm);
    color: var(--color-neutral-600);
    font-weight: var(--font-weight-medium);
}

/* Problem statement */
.problem-statement {
    background: var(--color-neutral-0);
    border: var(--border-width) solid var(--border-color);
    border-left: 5px solid var(--color-brand);
    border-radius: var(--radius-lg);
    padding: var(--space-6);
    margin-bottom: var(--space-6);
}

.problem-statement__context {
    font-size: var(--text-sm);
    color: var(--color-neutral-600);
    margin-bottom: var(--space-2);
    max-width: none;
}

.problem-statement__question {
    font-size: var(--text-xl);
    font-weight: var(--font-weight-bold);
    color: var(--color-neutral-800);
    margin-bottom: var(--space-4);
    max-width: none;
}

.given-value {
    display: inline-flex;
    align-items: baseline;
    gap: var(--space-2);
    padding: var(--space-3) var(--space-5);
    background: var(--color-brand-light);
    border-radius: var(--radius-md);
    font-family: var(--font-mono);
}

.given-value__number {
    font-size: var(--text-2xl);
    font-weight: var(--font-weight-bold);
    color: var(--color-brand-dark);
}

.given-value__unit {
    font-size: var(--text-lg);
    color: var(--color-brand);
}


/* ================================================================
   10. FRACTION CHAIN
================================================================ */
.chain-builder {
    background: var(--color-neutral-0);
    border: var(--border-width) solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: var(--space-6);
    margin-bottom: var(--space-6);
    overflow-x: auto;
    /* scrollable on small screens */
}

.chain-builder__label {
    font-size: var(--text-base);
    font-weight: var(--font-weight-medium);
    color: var(--color-neutral-600);
    margin-bottom: var(--space-4);
}

.chain {
    display: flex;
    align-items: center;
    flex-wrap: nowrap;
    gap: var(--space-3);
    min-height: 80px;
    padding-block: var(--space-2);
}

/* A single fraction in the chain */
.fraction {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    gap: 0;
    font-family: var(--font-mono);
    font-size: var(--text-base);
    flex-shrink: 0;
}

.fraction__num,
.fraction__den {
    padding: var(--space-1) var(--space-2);
    white-space: nowrap;
    min-width: 3rem;
    text-align: center;
    transition: color var(--transition-fast),
        text-decoration var(--transition-fast);
}

/* The horizontal bar between numerator and denominator */
.fraction__bar {
    display: block;
    width: 100%;
    height: 2px;
    background: var(--color-neutral-800);
    border-radius: 1px;
    min-width: 3rem;
}

/* Cancelled unit — strikethrough + muted colour */
.fraction__num.cancelled,
.fraction__den.cancelled {
    color: var(--color-neutral-400);
    text-decoration: line-through;
    text-decoration-color: var(--color-accent);
    text-decoration-thickness: 2px;
}

/* The given value (first item, locked) */
.chain__given .fraction__num {
    font-size: var(--text-lg);
    font-weight: var(--font-weight-bold);
    color: var(--color-brand-dark);
}

/* Multiplication / equals operators */
.chain__op {
    font-size: var(--text-xl);
    color: var(--color-neutral-600);
    padding-inline: var(--space-1);
    flex-shrink: 0;
}

/* Result fraction */
.chain__result .fraction__num {
    font-size: var(--text-lg);
    font-weight: var(--font-weight-bold);
    color: var(--color-neutral-800);
    transition: color var(--transition-base);
}

.chain__result.is-valid .fraction__num {
    color: var(--color-correct);
}

/* Each added step fraction */
.chain__step {
    position: relative;
    border: var(--border-width) solid var(--color-brand-light);
    border-radius: var(--radius-md);
    padding: var(--space-1);
    background: var(--color-brand-light);
    cursor: pointer;
    transition: border-color var(--transition-fast),
        background var(--transition-fast),
        transform var(--transition-fast);
}

.chain__step:hover {
    border-color: var(--color-brand);
    background: var(--color-brand-light);
    transform: translateY(-2px);
}

.chain__step:focus-visible {
    outline: 3px solid var(--color-brand);
    outline-offset: 3px;
}

/* Remove-step button inside each chain step */
.chain__step-remove {
    position: absolute;
    top: -8px;
    right: -8px;
    width: 20px;
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--color-accent);
    /* Hardcoded #ffffff — neutral-0 flips to near-black in dark mode
       giving only 3.43:1 on orange. #fff on #c8460a = 4.86:1 ✓ in both modes */
    color: #ffffff;
    border: none;
    border-radius: var(--radius-full);
    font-size: 11px;
    line-height: 1;
    cursor: pointer;
    opacity: 0;
    transition: opacity var(--transition-fast);
}

.chain__step:hover .chain__step-remove,
.chain__step:focus-within .chain__step-remove {
    opacity: 1;
}

/* Unit-status banner below the chain */
.chain-status {
    margin-top: var(--space-4);
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-md);
    font-size: var(--text-sm);
    font-weight: var(--font-weight-medium);
    display: flex;
    align-items: center;
    gap: var(--space-2);
}

.chain-status--incomplete {
    background: var(--color-hint-bg);
    color: var(--color-hint);
    border: 1px solid #ffe082;
}

.chain-status--valid {
    background: var(--color-correct-bg);
    color: var(--color-correct);
    border: 1px solid #81c784;
}

.chain-status--invalid {
    background: var(--color-incorrect-bg);
    color: var(--color-incorrect);
    border: 1px solid #e57373;
}


/* ================================================================
   11. FACTOR BANK
================================================================ */
.factor-bank {
    background: var(--color-neutral-0);
    border: var(--border-width) solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: var(--space-6);
    margin-bottom: var(--space-6);
}

.factor-bank__label {
    font-size: var(--text-base);
    font-weight: var(--font-weight-medium);
    color: var(--color-neutral-600);
    margin-bottom: var(--space-4);
    display: flex;
    align-items: baseline;
    flex-wrap: wrap;
    gap: var(--space-2);
}

.factor-bank__hint {
    font-size: var(--text-sm);
    font-weight: var(--font-weight-normal);
    /* neutral-400 was 2.59:1 on white — fails AA. neutral-600 = 7.1:1 ✓ */
    color: var(--color-neutral-600);
}

.factor-bank__grid {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-3);
}

/* Factor tile — a button representing one conversion factor */
.factor-tile {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    gap: 0;
    padding: var(--space-3) var(--space-4);
    background: var(--color-neutral-0);
    border: var(--border-width) solid var(--border-color);
    border-radius: var(--radius-md);
    font-family: var(--font-mono);
    font-size: var(--text-sm);
    /* Explicit colour — never rely on inheritance alone so contrast holds
       in both light and dark mode (--color-neutral-800 swaps to light in dark mode) */
    color: var(--color-neutral-800);
    cursor: pointer;
    transition: border-color var(--transition-fast),
        background var(--transition-fast),
        color var(--transition-fast),
        box-shadow var(--transition-fast),
        transform var(--transition-fast);
    min-width: 80px;
}

.factor-tile:hover {
    border-color: var(--color-brand);
    background: var(--color-brand-light);
    /* --color-brand-dark is dark in light mode, light in dark mode —
       always high-contrast against --color-brand-light */
    color: var(--color-brand-dark);
    transform: translateY(-2px);
    box-shadow: var(--shadow-sm);
}

.factor-tile[aria-pressed="true"] {
    border-color: var(--color-brand);
    background: var(--color-brand);
    /* Hardcoded white — light mode: white on #1a56a0 = 7.4:1 ✓
       Dark mode override below uses dark text on light blue instead */
    color: #ffffff;
    box-shadow: var(--shadow-sm);
}

.factor-tile[aria-pressed="true"] .factor-tile__bar {
    background: rgba(255, 255, 255, .85);
}

.factor-tile__num,
.factor-tile__den {
    padding: var(--space-1) var(--space-2);
    text-align: center;
    white-space: nowrap;
}

.factor-tile__bar {
    display: block;
    width: 100%;
    height: 2px;
    background: var(--color-neutral-800);
    border-radius: 1px;
    min-width: 60px;
}

/* Flipped variant (denominator on top, numerator on bottom) */
.factor-tile--flipped .factor-tile__num {
    order: 2;
}

.factor-tile--flipped .factor-tile__den {
    order: 0;
}

.factor-tile--flipped .factor-tile__bar {
    order: 1;
}

/* Flip toggle button */
.factor-tile__flip {
    font-size: 10px;
    /* neutral-600 gives ≥ 4.5:1 against the tile background in both modes */
    color: var(--color-neutral-600);
    background: none;
    border: none;
    cursor: pointer;
    padding: 0;
    margin-top: var(--space-1);
    text-decoration: underline;
    transition: color var(--transition-fast);
}

.factor-tile__flip:hover {
    color: var(--color-brand-dark);
}

/* On a pressed (selected) tile the flip button sits on brand blue;
   white ensures it remains readable */
.factor-tile[aria-pressed="true"] .factor-tile__flip {
    color: rgba(255, 255, 255, .85);
}

.factor-tile[aria-pressed="true"] .factor-tile__flip:hover {
    color: #ffffff;
}


/* ================================================================
   12. ANSWER ENTRY & FEEDBACK
================================================================ */
.answer-entry {
    background: var(--color-neutral-0);
    border: var(--border-width) solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: var(--space-6);
    margin-bottom: var(--space-6);
}

.answer-entry__label {
    font-size: var(--text-base);
    font-weight: var(--font-weight-medium);
    color: var(--color-neutral-600);
    margin-bottom: var(--space-4);
}

.answer-entry__row {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: var(--space-4);
}

.answer-entry__field-wrap {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
}

.answer-entry__field-label {
    font-size: var(--text-sm);
    font-weight: var(--font-weight-medium);
    color: var(--color-neutral-800);
}

.answer-entry__field-hint {
    display: block;
    font-size: var(--text-xs);
    font-weight: var(--font-weight-normal);
    /* neutral-400 was 2.59:1 on white — fails AA. neutral-600 = 7.1:1 ✓ */
    color: var(--color-neutral-600);
    margin-top: 2px;
}

.answer-entry__input {
    height: 44px;
    /* minimum 44×44 touch target (WCAG 2.5.5) */
    padding: var(--space-2) var(--space-3);
    border: var(--border-width) solid var(--border-color);
    border-radius: var(--radius-md);
    font-size: var(--text-base);
    font-family: var(--font-mono);
    background: var(--color-neutral-0);
    color: var(--color-neutral-800);
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
    min-width: 180px;
}

.answer-entry__input--unit {
    min-width: 100px;
    font-family: var(--font-mono);
}

.answer-entry__input:focus-visible {
    border-color: var(--color-brand);
    box-shadow: 0 0 0 3px rgba(26, 86, 160, .2);
    outline: none;
}

.answer-entry__input.is-correct {
    border-color: var(--color-correct);
    background: var(--color-correct-bg);
}

.answer-entry__input.is-incorrect {
    border-color: var(--color-incorrect);
    background: var(--color-incorrect-bg);
}

/* Feedback message */
.answer-feedback {
    margin-top: var(--space-4);
    min-height: 2rem;
    /* reserve space so layout doesn't jump */
}

.answer-feedback__correct,
.answer-feedback__incorrect {
    display: flex;
    align-items: flex-start;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-md);
    font-size: var(--text-base);
    font-weight: var(--font-weight-medium);
}

.answer-feedback__correct {
    background: var(--color-correct-bg);
    color: var(--color-correct);
    border: 1px solid #81c784;
}

.answer-feedback__incorrect {
    background: var(--color-incorrect-bg);
    color: var(--color-incorrect);
    border: 1px solid #e57373;
}

.answer-feedback__icon {
    font-size: var(--text-xl);
    line-height: 1.2;
    flex-shrink: 0;
}


/* ================================================================
   13. HINT PANEL
================================================================ */
.hint-panel {
    margin-bottom: var(--space-6);
}

.hint-panel__toggle {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    font-size: var(--text-sm);
}

.hint-panel__count {
    font-size: var(--text-xs);
    /* neutral-400 was ~2.4:1 on light backgrounds — fails AA. neutral-600 = 6.7:1 ✓ */
    color: var(--color-neutral-600);
}

.hint-panel__content {
    margin-top: var(--space-3);
    padding: var(--space-4) var(--space-5);
    background: var(--color-hint-bg);
    border: 1px solid #ffe082;
    border-radius: var(--radius-md);
}

.hint-panel__text {
    font-size: var(--text-base);
    color: var(--color-hint);
    max-width: none;
}

.hint-panel__next {
    margin-top: var(--space-3);
}


/* ================================================================
   14. PROBLEM ACTIONS
================================================================ */
.problem-actions {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-top: var(--space-4);
    border-top: 1px solid var(--border-color);
    gap: var(--space-4);
}


/* ================================================================
   15. SESSION SUMMARY
================================================================ */
.summary {
    padding-block: var(--section-gap);
    background: var(--color-neutral-0);
}

.summary__scorecard {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    gap: var(--space-8);
    margin-bottom: var(--space-10);
    padding: var(--space-8);
    background: var(--color-neutral-50);
    border-radius: var(--radius-xl);
    border: var(--border-width) solid var(--border-color);
}

.summary__stat {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-2);
}

.summary__stat-value {
    font-size: var(--text-4xl);
    font-weight: var(--font-weight-bold);
    color: var(--color-brand);
    font-variant-numeric: tabular-nums;
}

.summary__stat-label {
    font-size: var(--text-sm);
    color: var(--color-neutral-600);
    text-transform: uppercase;
    letter-spacing: .06em;
}

.summary__table-wrap {
    overflow-x: auto;
    border-radius: var(--radius-lg);
    border: var(--border-width) solid var(--border-color);
    margin-bottom: var(--space-8);
}

/* Give the scrollable table region a visible focus ring */
.summary__table-wrap:focus-visible {
    outline: 3px solid var(--color-brand);
    outline-offset: 2px;
}

.summary__table {
    width: 100%;
    border-collapse: collapse;
    font-size: var(--text-sm);
    min-width: 520px;
}

.summary__table-caption {
    font-size: var(--text-xs);
    /* neutral-400 was 2.44:1 on neutral-50 — fails AA. neutral-600 = 6.7:1 ✓ */
    color: var(--color-neutral-600);
    text-align: left;
    padding: var(--space-3) var(--space-4);
    font-weight: var(--font-weight-medium);
    text-transform: uppercase;
    letter-spacing: .06em;
    background: var(--color-neutral-50);
    border-bottom: 1px solid var(--border-color);
}

.summary__table thead th {
    padding: var(--space-3) var(--space-4);
    text-align: left;
    font-weight: var(--font-weight-bold);
    color: var(--color-neutral-600);
    background: var(--color-neutral-50);
    border-bottom: var(--border-width) solid var(--border-color);
    white-space: nowrap;
}

.summary__table tbody tr {
    border-bottom: 1px solid var(--color-neutral-100);
    transition: background var(--transition-fast);
}

.summary__table tbody tr:last-child {
    border-bottom: none;
}

.summary__table tbody tr:hover {
    background: var(--color-neutral-50);
}

.summary__table tbody td {
    padding: var(--space-3) var(--space-4);
    vertical-align: top;
    color: var(--color-neutral-800);
}

/* Result cell — ✓ correct / ✗ incorrect */
.summary__result--correct {
    color: var(--color-correct);
    font-weight: var(--font-weight-bold);
}

.summary__result--incorrect {
    color: var(--color-incorrect);
    font-weight: var(--font-weight-bold);
}

.summary__actions {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-4);
    justify-content: center;
}


/* ================================================================
   16. SITE FOOTER
================================================================ */
.site-footer {
    background: var(--color-neutral-800);
    color: var(--color-neutral-400);
    padding-block: var(--space-8);
    margin-top: auto;
}

.site-footer__text {
    font-size: var(--text-sm);
    text-align: center;
    max-width: none;
}


/* ================================================================
   17. BUTTONS
================================================================ */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    padding: var(--space-3) var(--space-5);
    height: 44px;
    /* minimum touch target */
    border: var(--border-width) solid transparent;
    border-radius: var(--radius-md);
    font-size: var(--text-base);
    font-weight: var(--font-weight-medium);
    line-height: 1;
    cursor: pointer;
    text-decoration: none;
    white-space: nowrap;
    transition: background var(--transition-fast),
        border-color var(--transition-fast),
        color var(--transition-fast),
        box-shadow var(--transition-fast),
        transform var(--transition-fast);
    -webkit-user-select: none;
    user-select: none;
}

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

.btn:disabled,
.btn[aria-disabled="true"] {
    opacity: .45;
    cursor: not-allowed;
    pointer-events: none;
}

/* Primary */
.btn--primary {
    background: var(--color-brand);
    color: var(--color-neutral-0);
    border-color: var(--color-brand);
}

.btn--primary:hover {
    background: var(--color-brand-dark);
    border-color: var(--color-brand-dark);
}

/* Ghost */
.btn--ghost {
    background: transparent;
    color: var(--color-brand);
    border-color: var(--color-brand);
}

.btn--ghost:hover {
    background: var(--color-brand-light);
}

/* Danger */
.btn--danger {
    background: var(--color-accent);
    color: var(--color-neutral-0);
    border-color: var(--color-accent);
}

/* Sizes */
.btn--lg {
    height: 52px;
    padding: var(--space-4) var(--space-8);
    font-size: var(--text-lg);
    border-radius: var(--radius-lg);
}

.btn--sm {
    height: 36px;
    padding: var(--space-2) var(--space-3);
    font-size: var(--text-sm);
}


/* ================================================================
   18. TAGS & CHIPS
================================================================ */
.tag {
    display: inline-flex;
    align-items: center;
    padding: 2px var(--space-2);
    border-radius: var(--radius-full);
    font-size: var(--text-xs);
    font-weight: var(--font-weight-bold);
    text-transform: uppercase;
    letter-spacing: .06em;
}

.tag--physics {
    background: var(--color-physics-bg);
    color: var(--color-physics);
}

.tag--chemistry {
    background: var(--color-chemistry-bg);
    color: var(--color-chemistry);
}


/* ================================================================
   19. PROGRESS BAR
================================================================ */
.progress-bar-wrap {
    height: 6px;
    background: var(--color-neutral-200);
    border-radius: var(--radius-full);
    overflow: hidden;
    margin-bottom: var(--space-6);
}

.progress-bar {
    height: 100%;
    background: var(--color-brand);
    border-radius: var(--radius-full);
    transition: width var(--transition-slow);
}


/* ================================================================
   20. RESPONSIVE — TABLET (≥ 640 px)
================================================================ */
@media (min-width: 640px) {

    .level-cards {
        grid-template-columns: repeat(2, 1fr);
    }

    .answer-entry__input {
        min-width: 220px;
    }

    .summary__table-wrap {
        overflow-x: visible;
    }

    .summary__table {
        min-width: 0;
    }

}


/* ================================================================
   21. RESPONSIVE — DESKTOP (≥ 1024 px)
================================================================ */
@media (min-width: 1024px) {

    .level-cards {
        grid-template-columns: repeat(4, 1fr);
    }

    .container {
        padding-inline: var(--space-8);
    }

    h1 {
        font-size: var(--text-4xl);
    }

    .hero__heading {
        font-size: clamp(var(--text-3xl), 5vw, 3.5rem);
    }

    .workspace {
        padding-block: var(--space-12);
    }

    /* Wider chain scrolls naturally; cap at a comfortable width */
    .chain-builder {
        overflow-x: auto;
    }

}


/* ================================================================
   22. DARK MODE  (prefers-color-scheme: dark)
================================================================ */
@media (prefers-color-scheme: dark) {

    /* Footer — in dark mode --color-neutral-800 flips to #e8eaf2 (light gray),
       making the footer background light while its text (neutral-400 = #6b7591)
       is also light: only 3.83:1. Keep the footer visually dark by pinning it
       to neutral-50 (dark mode = #13161f) with neutral-600 text = 9.8:1 ✓ */
    .site-footer {
        background: var(--color-neutral-50);
        /* #13161f in dark mode — stays dark */
        color: var(--color-neutral-600);
        /* #b0bac9 on #13161f = 9.8:1 ✓ */
    }

    /* Pressed factor tile — in dark mode --color-brand flips to #5a9de8 (light blue).
       White text on that background is only 2.7:1 (fails).
       Switch to dark text: #1a1e2a on #5a9de8 = 6.3:1 ✓ */
    .factor-tile[aria-pressed="true"] {
        color: var(--color-neutral-0);
        /* #1a1e2a — near-black on light blue */
    }

    .factor-tile[aria-pressed="true"] .factor-tile__bar {
        background: rgba(0, 0, 0, .45);
    }

    .factor-tile[aria-pressed="true"] .factor-tile__flip {
        color: rgba(0, 0, 0, .7);
    }

    .factor-tile[aria-pressed="true"] .factor-tile__flip:hover {
        color: var(--color-neutral-0);
    }

    :root {
        --color-brand: #5a9de8;
        --color-brand-dark: #92bff0;
        --color-brand-light: #1a3456;

        --color-correct: #6fcf97;
        --color-correct-bg: #0d2b1a;
        --color-incorrect: #eb5757;
        --color-incorrect-bg: #2b0d0d;
        --color-hint: #f2c94c;
        --color-hint-bg: #2b2200;

        --color-neutral-0: #1a1e2a;
        --color-neutral-50: #13161f;
        --color-neutral-100: #1e2231;
        --color-neutral-200: #2e3447;
        --color-neutral-400: #6b7591;
        --color-neutral-600: #b0bac9;
        --color-neutral-800: #e8eaf2;
        --color-neutral-900: #f3f4f8;

        --color-physics-bg: #1a3456;
        --color-physics: #5a9de8;
        --color-chemistry-bg: #0d2b1a;
        --color-chemistry: #6fcf97;

        --border-color: var(--color-neutral-200);
        --shadow-sm: 0 1px 3px rgba(0, 0, 0, .4);
        --shadow-md: 0 4px 12px rgba(0, 0, 0, .5);
    }
}


/* ================================================================
   23. HIGH CONTRAST  (prefers-contrast: more)
   Forces stronger borders and removes subtle background tints so
   the UI meets WCAG 1.4.3 (4.5:1 minimum contrast) even in
   forced-colours / high-contrast OS modes.
================================================================ */
@media (prefers-contrast: more) {
    :root {
        --color-brand: #003e8a;
        --color-brand-dark: #00275a;
        --border-color: #000000;
        --border-width: 2px;
    }

    .level-card,
    .factor-bank,
    .chain-builder,
    .answer-entry,
    .hint-panel__content,
    .problem-statement {
        border-width: 2px;
        border-color: #000000;
    }

    .factor-tile {
        border-width: 2px;
        border-color: #000000;
    }

    .btn--primary {
        background: #003e8a;
        border-color: #000000;
    }

    .btn--ghost {
        border-color: #000000;
        color: #003e8a;
    }

    /* Ensure cancelled units remain readable */
    .fraction__num.cancelled,
    .fraction__den.cancelled {
        color: #555;
    }

    /* Focus ring — extra thick */
    :focus-visible {
        outline: 4px solid #000000;
        outline-offset: 4px;
    }
}

/* Windows forced-colours (high-contrast) mode */
@media (forced-colors: active) {

    .btn,
    .factor-tile,
    .level-card {
        forced-color-adjust: auto;
    }

    .fraction__bar,
    .factor-tile__bar {
        background: ButtonText;
    }

    .progress-bar {
        background: Highlight;
    }
}


/* ================================================================
   24. REDUCED MOTION
   Disables all transitions and animations for users who have
   requested reduced motion (WCAG 2.3.3 / 2.5.6).
================================================================ */
@media (prefers-reduced-motion: reduce) {

    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }

    .chain__step:hover,
    .factor-tile:hover,
    .btn:active {
        transform: none !important;
    }
}


/* ================================================================
   25. RESUME BANNER (injected by app.js when a saved session exists)
================================================================ */
.resume-banner {
    position: fixed;
    bottom: var(--space-4);
    left: 50%;
    transform: translateX(-50%);
    z-index: 200;
    width: min(480px, calc(100vw - var(--space-8)));
    background: var(--color-neutral-0);
    border: var(--border-width) solid var(--color-brand);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-lg);
    padding: var(--space-5) var(--space-6);
}

.resume-banner p {
    font-size: var(--text-base);
    color: var(--color-neutral-800);
    margin-bottom: var(--space-4);
    max-width: none;
}

.resume-banner__actions {
    display: flex;
    gap: var(--space-3);
    flex-wrap: wrap;
}


/* ================================================================
   26. MOBILE NAV OPEN STATE
================================================================ */
@media (max-width: 639px) {
    .primary-nav__toggle {
        display: flex;
    }

    .primary-nav__list {
        display: none;
        position: absolute;
        top: var(--nav-height);
        left: 0;
        right: 0;
        flex-direction: column;
        background: var(--color-neutral-0);
        border-bottom: var(--border-width) solid var(--border-color);
        box-shadow: var(--shadow-md);
        padding: var(--space-3) var(--space-4);
        gap: var(--space-1);
        z-index: 99;
    }

    .primary-nav__list.is-open {
        display: flex;
    }

    .primary-nav__link {
        padding: var(--space-3) var(--space-4);
        width: 100%;
        border-radius: var(--radius-md);
    }
}