/* T01b — shared components, ported from legacy styles.css onto design tokens.
   Colors/spacing/radii/shadows come only from styles/tokens.css.
   Focus rings are handled by :focus-visible in the base layer. */

@layer components {
  /* ---- Screen typography & back navigation ---- */

  .screen-title {
    margin: 0 0 var(--sp-1);
    font-size: var(--fs-h1);
    letter-spacing: -0.01em;
  }

  .screen-hint {
    margin: 0 0 var(--sp-6);
    color: var(--muted);
  }

  .back-link {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    color: var(--muted);
    font-weight: 600;
    font-size: var(--fs-sm);
    text-decoration: none;
    transition: color 0.12s ease;
    /* A 22px text link is under the §7.1 floor. The target grows to 44px and the
       negative margins hand the extra 11px back to the layout, so the visible
       position of the label and the gap below it do not move. */
    min-height: 44px;
    margin-top: -11px;
    margin-bottom: calc(var(--sp-5) - 11px);
  }

  .back-link:hover { color: var(--accent-strong); }

  /* ---- Cards & panels ---- */

  .card, .panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--r-md);
    box-shadow: var(--shadow-1);
  }

  /* ---- Icons (size utilities; sprite/illustrations land in later tasks) ---- */

  .icon {
    display: inline-block;
    flex-shrink: 0;
    vertical-align: middle;
  }

  .icon-16 { width: 16px; height: 16px; }
  .icon-20 { width: 20px; height: 20px; }
  .icon-24 { width: 24px; height: 24px; }

  /* ---- Icon circle: the ONE affordance chrome for every navigation
     arrow/chevron (cards, lists, chips, resume strip, topbar exit) ----
     Surface fill + shadow-1. In dark the (black-alpha) shadow barely reads on a
     dark card, so the circle also gets a 1px --circle-edge ring — transparent in
     light, --border in dark; border-box keeps the size identical either way.
     Sizes pair with an icon size: lg 20, md 18, sm 16. */

  .icon-circle {
    flex: none;
    display: grid;
    place-items: center;
    border: 1px solid var(--circle-edge);
    border-radius: var(--r-full);
    background: var(--surface);
    box-shadow: var(--shadow-1);
    color: var(--ink);
  }

  .icon-circle--lg { width: 44px; height: 44px; }
  .icon-circle--md { width: 36px; height: 36px; }
  .icon-circle--sm { width: 28px; height: 28px; }

  /* ---- Charts (src/ui/charts.js) ----
     Entry motion: the ring draws over 600ms, mastery segments unfold over 480ms
     with a 40ms stagger. base.css neutralises both under reduced motion, and
     charts.js additionally ships the final frame. */

  .ring { position: relative; display: inline-grid; place-items: center; flex: none; }
  .ring-svg { display: block; }
  .ring-track { fill: none; stroke: var(--border); }
  .ring-arc {
    fill: none;
    stroke: var(--accent);
    stroke-linecap: round;
    transition: stroke-dashoffset 600ms var(--ease-out);
  }
  .ring-label {
    position: absolute;
    font-family: var(--font-display);
    font-weight: 700;
    line-height: 1;
    color: var(--ink);
  }

  .mastery { display: inline-flex; gap: 3px; flex: none; }
  .mastery-seg {
    width: 14px; height: 6px;
    border-radius: var(--r-full);
    /* Empty segments ride --border-strong: on dark, --surface-2 (30%) vs the
       card --surface (25%) is invisible. */
    background: var(--border-strong);
    opacity: 0.45;
    transform: scaleX(0.35);
    transform-origin: left center;
    transition: transform 480ms var(--ease-out), opacity 480ms var(--ease-out);
  }
  .mastery-seg.is-in { opacity: 1; transform: none; }
  .mastery-seg.is-filled { background: var(--accent); }

  .spark-wrap { min-width: 0; }
  .spark { display: block; width: 100%; height: auto; }
  .spark-line {
    fill: none;
    stroke: var(--accent);
    stroke-width: 2;
    stroke-linejoin: round;
    stroke-linecap: round;
    vector-effect: non-scaling-stroke;
  }
  .spark-dot { fill: var(--surface); stroke: var(--accent); stroke-width: 2; }
  .spark-label {
    fill: var(--muted);
    font-family: var(--font-body);
    font-size: 10px;
    font-weight: 600;
    text-anchor: middle;
  }

  /* Heatmap ladder sits on the card --surface. An empty --surface-2 cell was a
     1.17:1 ghost in dark, so the whole ramp is rebased: "ink 12% over surface"
     reads as a subtle well in light and a faint raised cell in dark, and each
     accent step lands clearly above the previous one. */
  .heatmap {
    display: grid;
    grid-auto-flow: column;
    grid-template-rows: repeat(7, 12px);
    gap: 3px;
    overflow-x: auto;
    padding-bottom: var(--sp-1);
  }
  .hm-cell { width: 12px; height: 12px; border-radius: 3px; background: color-mix(in oklab, var(--ink) 12%, var(--surface)); }
  .hm-l1 { background: color-mix(in oklab, var(--accent) 25%, color-mix(in oklab, var(--ink) 12%, var(--surface))); }
  .hm-l2 { background: color-mix(in oklab, var(--accent) 50%, color-mix(in oklab, var(--ink) 12%, var(--surface))); }
  .hm-l3 { background: color-mix(in oklab, var(--accent) 75%, color-mix(in oklab, var(--ink) 12%, var(--surface))); }
  .hm-l4 { background: var(--accent); }
  .hm-legend {
    display: flex; align-items: center; gap: 4px;
    margin-top: 6px;
    color: var(--muted); font-size: var(--fs-xs); font-weight: 600;
  }
  .hm-legend .hm-cell { width: 10px; height: 10px; }

  /* ---- Skeleton stubs (shimmer animation added later in motion.css) ---- */

  .skeleton {
    background: var(--surface-2);
    border-radius: var(--r-xs);
    color: transparent;
  }

  .skeleton-line { height: 1em; margin: var(--sp-1) 0; }

  /* ---- Badges & chips ---- */

  .badge, .pill {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 2px 10px;
    border-radius: var(--r-full);
    font-size: var(--fs-sm);
    font-weight: 600;
    background: var(--surface-2);
    color: var(--ink);
    border: 1px solid var(--border);
    white-space: nowrap;
  }

  .badge-muted { color: var(--muted); background: var(--surface); }

  /* ---- Buttons ---- */
  /* Hover/press transitions live in motion.css; focus outline in base.css. */

  /* Buttons share ONE radius (--r-md, the value .btn-ghost already used) and
     ONE minimum target height (§7.1: 44px). Padding alone got a 16px/1.6 label
     to ~45px, which is why the size held on most screens but not on .btn-hint
     (42px) or .btn-compact (40px) — an explicit floor makes it a contract. */
  .btn,
  .btn-primary,
  .btn-secondary,
  .btn-danger,
  .sa-btn,
  .btn-mark {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--sp-2);
    font: inherit;
    font-weight: 600;
    border-radius: var(--r-md);
    cursor: pointer;
    min-height: 44px;
    padding: 9px var(--sp-4);
    border: 1px solid transparent;
  }

  .btn:disabled,
  .btn-primary:disabled,
  .btn-secondary:disabled,
  .btn-danger:disabled,
  .sa-btn:disabled,
  .btn-mark:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }

  /* Solid launch/next buttons: white label on the deep AA-safe orange, never on
     the lighter decorative --accent fill. */
  .btn-primary {
    background: var(--btn-primary);
    color: var(--on-accent);
    border-color: var(--btn-primary);
  }
  @media (hover: hover) and (pointer: fine) {
    .btn-primary:hover:not(:disabled) {
      background: var(--btn-primary-hover);
      border-color: var(--btn-primary-hover);
    }

    .btn-secondary:hover:not(:disabled) {
      background: var(--accent-soft);
      border-color: var(--accent);
    }

    .btn-ghost:hover:not(:disabled) {
      background: var(--accent-soft);
      border-color: var(--accent);
      color: var(--ink);
    }

    .btn-ghost-danger:hover:not(:disabled) {
      background: var(--danger-soft);
      border-color: color-mix(in oklab, var(--danger) 40%, var(--surface));
    }

    .btn-danger:hover:not(:disabled) {
      background: var(--seal-danger-hover);
      border-color: var(--seal-danger-hover);
    }

    .sa-btn:hover:not(:disabled) {
      background: var(--accent-soft);
      border-color: var(--accent);
    }

    .btn-mark:hover {
      background: var(--surface-2);
      color: var(--accent-strong);
      border-color: var(--accent);
    }

    .btn-icon:hover:not(:disabled) {
      background: var(--surface-2);
      border-color: var(--accent);
      color: var(--accent-strong);
    }
  }

  .btn-secondary {
    background: var(--surface);
    color: var(--accent-strong);
    border-color: var(--border-strong);
  }

  /* Ghost button: a real, readable button — bordered and filled, never bare
     text. Covers "Ask AI Tutor", "Mark", "Clear Chat", "Retry", "Cancel". */
  .btn-ghost {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--sp-2);
    font: inherit;
    font-weight: 600;
    border-radius: var(--r-md);
    cursor: pointer;
    min-height: 44px;
    padding: 0 var(--sp-4);
    border: 1px solid var(--border-strong);
    background: var(--surface);
    color: var(--ink);
  }

  /* Ghost danger: a destructive action that stays quiet until hovered —
     "Finish Early" in the exam bar, "Reset progress" in the danger zone. */
  .btn-ghost-danger {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--sp-2);
    font: inherit;
    font-weight: 600;
    border-radius: var(--r-md);
    cursor: pointer;
    min-height: 44px;
    padding: 9px var(--sp-4);
    border: 1px solid transparent;
    background: transparent;
    color: var(--danger);
  }

  /* Solid danger button: --seal-danger, not raw --danger (white on dark
     --danger is 3.05:1; the seal keeps 6.3:1 in dark and 5.9:1 in light). */
  .btn-danger {
    background: var(--seal-danger);
    color: var(--on-accent);
    border-color: var(--seal-danger);
  }

  .sa-btn {
    background: var(--surface);
    color: var(--accent-strong);
    border-color: var(--border-strong);
  }

  .btn-mark {
    background: var(--surface);
    color: var(--muted);
    border-color: var(--border-strong);
  }
  .btn-mark.is-marked {
    background: var(--accent-soft);
    border-color: var(--accent);
    color: var(--accent-strong);
  }

  /* Icon-only button (the tutor sheet's close): a 36px disc failed §7.1 for a
     touch target that has no larger twin anywhere on the sheet. */
  .btn-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    padding: 0;
    border-radius: var(--r-md);
    background: var(--surface);
    color: var(--ink);
    border: 1px solid var(--border-strong);
    cursor: pointer;
  }

  /* The Read Aloud button shows its own spinner and "Loading…" label while the
     clip is fetched, so keep it at full opacity instead of the faded :disabled look. */
  .btn[aria-busy="true"],
  .btn-secondary[aria-busy="true"]:disabled,
  .ai-tts-read[aria-busy="true"] {
    opacity: 1;
    cursor: progress;
  }

  /* ---- Choices (question options) ---- */

  .choices {
    display: flex;
    flex-direction: column;
    gap: 10px;
  }

  .choice {
    position: relative;
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    min-height: 52px;
    padding: var(--sp-3) var(--sp-4);
    background: var(--surface);
    border: 2px solid var(--border);
    border-radius: var(--r-sm);
    cursor: pointer;
    transition: border-color 140ms var(--ease-out), background 140ms var(--ease-out), transform 120ms var(--ease-out);
  }

  @media (hover: hover) and (pointer: fine) {
    .choice:hover { border-color: var(--border-strong); }
  }

  @media (hover: none), (pointer: coarse) {
    .choice:active:not(.choice-static) {
      transform: scale(0.985);
      border-color: var(--border-strong);
    }
  }

  .choice:has(input:checked) {
    border-color: var(--accent);
    background: var(--accent-soft);
  }

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

  /* The native control stays in the DOM for semantics, grouping, keyboard
     selection and form behaviour, but the A/B/C/D letter badge is its only
     visible carrier — so it is visually hidden rather than removed. Same
     technique as .sr-only; display:none would drop it out of the tab order. */
  .choice input[type="radio"] {
    appearance: none;
    -webkit-appearance: none;
    position: absolute;
    top: 0;
    left: 0;
    width: 1px;
    height: 1px;
    margin: 0;
    padding: 0;
    border: 0;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }
  /* The card carries the focus ring (see :has(input:focus-visible) above). */
  .choice input[type="radio"]:focus-visible { outline: none; }

  .choice-text { line-height: 1.45; flex: 1; min-width: 0; }

  /* Option letter badge (A/B/C/D) — matches the letters used by Read Aloud. */
  .choice-letter {
    flex: none;
    width: 28px;
    height: 28px;
    display: grid;
    place-items: center;
    border: 1px solid var(--border);
    border-radius: var(--r-full);
    background: var(--surface-2);
    color: var(--muted);
    font-family: var(--font-display);
    font-size: 0.82em;
    font-weight: 700;
    line-height: 1;
    transition: background 140ms var(--ease-out), border-color 140ms var(--ease-out), color 140ms var(--ease-out), transform 140ms var(--ease-out);
  }
  .choice:has(input:checked) .choice-letter {
    border-color: var(--btn-primary);
    background: var(--btn-primary);
    color: var(--on-accent);
    transform: scale(1.06);
  }
  .choice-static .choice-letter { background: var(--surface); }
  /* Verdict seals are solid discs under --on-accent text: raw dark --success /
     --danger only reach 2.5 / 3.1:1, the --seal-* mixes reach 6.1 / 6.3. */
  .choice.is-correct .choice-letter {
    border-color: var(--seal-success);
    background: var(--seal-success);
    color: var(--on-accent);
  }
  .choice.is-wrong .choice-letter {
    border-color: var(--seal-danger);
    background: var(--seal-danger);
    color: var(--on-accent);
  }

  /* Static recap of choices shown in feedback: the ONLY carrier of the chosen/
     correct answers for mcq/truefalse (green outline + check, red + x). */
  .choice-static { cursor: default; }
  .choice-static:hover { border-color: var(--border); }
  .choice.is-correct {
    border-color: color-mix(in oklab, var(--success) 40%, var(--surface));
    background: var(--success-soft);
    color: color-mix(in oklab, var(--success) 80%, var(--ink));
  }
  .choice.is-wrong {
    border-color: color-mix(in oklab, var(--danger) 40%, var(--surface));
    background: var(--danger-soft);
    color: color-mix(in oklab, var(--danger) 80%, var(--ink));
  }
  .choice.is-correct .choice-text,
  .choice.is-wrong .choice-text { font-weight: 600; }
  .choice > .icon {
    width: 22px;
    height: 22px;
    margin-left: auto;
    flex-shrink: 0;
  }
  /* ---- Forms & inputs ---- */

  .numeric-input input[type="number"],
  .short-input textarea {
    width: 100%;
    max-width: 340px;
    padding: 10px var(--sp-3);
    font: inherit;
    border: 1px solid var(--border-strong);
    border-radius: var(--r-sm);
    background: var(--surface);
    transition: border-color 0.12s ease;
  }

  .numeric-input input[type="number"]:focus-visible,
  .short-input textarea:focus-visible {
    border-color: var(--accent);
  }

  .short-input textarea {
    max-width: 100%;
    resize: vertical;
    min-height: 120px;
  }

  /* ---- Feedback ---- */

  /* Post-answer wrapper: keeps the smooth-scroll target clear of the topbar. */
  .feedback { scroll-margin-top: 72px; }

  .feedback-banner {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    padding: 12px var(--sp-4);
    border-radius: var(--r-md);
    margin-bottom: var(--sp-4);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-1);
  }
  .feedback-banner-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    flex-shrink: 0;
  }
  .feedback-banner-icon .icon {
    width: 20px;
    height: 20px;
  }
  .feedback-banner-title {
    font-size: var(--fs-body);
    font-weight: 800;
    letter-spacing: -0.01em;
  }
  .feedback-banner.correct {
    border-left: 4px solid var(--success);
    border-color: color-mix(in oklab, var(--success) 35%, var(--border));
    background: color-mix(in oklab, var(--success-soft) 40%, var(--surface));
  }
  .feedback-banner.correct .feedback-banner-icon {
    background: var(--success);
    color: #ffffff;
  }
  .feedback-banner.correct .feedback-banner-title {
    color: color-mix(in oklab, var(--success) 85%, var(--ink));
  }
  .feedback-banner.incorrect {
    border-left: 4px solid var(--danger);
    border-color: color-mix(in oklab, var(--danger) 35%, var(--border));
    background: color-mix(in oklab, var(--danger-soft) 40%, var(--surface));
  }
  .feedback-banner.incorrect .feedback-banner-icon {
    background: var(--danger);
    color: #ffffff;
  }
  .feedback-banner.incorrect .feedback-banner-title {
    color: color-mix(in oklab, var(--danger) 85%, var(--ink));
  }

  .feedback-line { margin: var(--sp-2) 0; }

  /* Compact correct-answer hint (numeric feedback only, shown when wrong). */
  .feedback-answer {
    display: inline-block;
    margin-left: var(--sp-3);
    padding: 0 var(--sp-2);
    border-radius: var(--r-full);
    background: var(--surface-2);
    color: var(--ink);
    font-size: var(--fs-sm);
    font-weight: 700;
    white-space: nowrap;
  }

  .feedback-explanation {
    margin: 16px 0 var(--sp-3);
    padding: var(--sp-3) var(--sp-4);
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--r-md);
  }
  .feedback-explanation-header {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    margin-bottom: var(--sp-2);
  }
  .feedback-explanation-title {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    color: var(--ink);
    font-size: var(--fs-body);
    font-weight: 700;
  }
  .feedback-explanation-title .icon {
    color: var(--accent-strong);
    flex-shrink: 0;
  }

  .sa-explanation-section {
    margin: var(--sp-4) 0 var(--sp-3);
  }
  .sa-explanation-section .feedback-explanation-header {
    margin-bottom: var(--sp-2);
  }
  .sa-explanation-section .collapse {
    margin: var(--sp-2) 0;
  }
  .feedback-explanation-header .speech-control {
    margin: 0;
  }
  .feedback-explanation-header .speech-trigger,
  .feedback-explanation-header .speech-toggle,
  .feedback-explanation-header .speech-stop {
    width: 36px;
    height: 36px;
  }
  .feedback-explanation-header .speech-trigger .icon,
  .feedback-explanation-header .speech-toggle .icon {
    width: 18px;
    height: 18px;
  }
  .feedback-explanation-header .speech-stop .icon {
    width: 16px;
    height: 16px;
  }
  .feedback-explanation-text {
    margin: 0;
    color: var(--ink);
    font-size: var(--fs-body);
    line-height: 1.6;
    text-wrap: pretty;
  }

  /* Collapsible feedback sections ("Why the other options are wrong",
     "Explain More"): details/summary with a rotating chevron and an
     animated body (discrete transitions where supported). */
  .collapse {
    margin: 14px 0 var(--sp-2);
    padding: var(--sp-2) var(--sp-3);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
  }
  .collapse summary {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    cursor: pointer;
    font-weight: 600;
    list-style: none;
    /* 34px as shipped (§7.1 needs 44) and flush to the text, so the focus ring
       drew on top of the border of the <details> it sits inside. */
    min-height: 44px;
    padding: var(--sp-1) var(--sp-2);
    border-radius: var(--r-xs);
  }
  /* `summary` is focusable and toggles on Enter/Space, but UA styles give it no
     visible ring once list-style is removed — re-assert the base.css token ring. */
  .collapse summary:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
  }
  .collapse summary::-webkit-details-marker { display: none; }
  .collapse summary .icon {
    margin-left: auto;
    color: var(--muted);
    transition: transform var(--dur-med) var(--ease-out);
  }
  .collapse[open] summary .icon { transform: rotate(180deg); }
  .collapse-body {
    color: var(--ink-soft);
    opacity: 0;
    transform: translateY(-4px);
    transition:
      opacity 240ms ease-out,
      transform 240ms ease-out,
      display 240ms ease-out allow-discrete;
  }
  .collapse[open] .collapse-body { opacity: 1; transform: none; }
  @starting-style {
    .collapse[open] .collapse-body { opacity: 0; transform: translateY(-4px); }
  }
  /* Fallback for browsers without discrete transitions on details content. */
  .collapse[open] .collapse-body { animation: collapse-in 240ms ease-out; }
  @keyframes collapse-in {
    from { opacity: 0; transform: translateY(-4px); }
    to { opacity: 1; transform: none; }
  }
  .collapse + .collapse { margin-top: var(--sp-2); }
  .collapse-body ul:not(.why-not-list) {
    margin: var(--sp-2) 0 var(--sp-1);
    padding-left: var(--sp-5);
  }
  .collapse-body p:not(.explain-more) {
    margin: var(--sp-2) 0;
    line-height: 1.55;
    color: var(--ink);
  }
  .collapse-body li {
    margin-bottom: 4px;
    line-height: 1.45;
  }

  .why-not-list {
    margin: var(--sp-2) 0 var(--sp-1);
    padding-left: var(--sp-5);
  }

  /* Letter of the option each "why not" reason refers to. */
  .why-not-letter {
    display: inline-block;
    min-width: 1.6em;
    margin-right: 2px;
    font-weight: 700;
    color: var(--accent-strong);
  }

  .explain-more {
    margin: var(--sp-1) 0;
    padding: var(--sp-2) 0;
    color: var(--ink);
  }

  /* ---- Short-answer feedback ---- */

  .sa-block {
    margin: 0 0 var(--sp-4);
    padding: var(--sp-4);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--r-md);
  }

  .sa-block > strong {
    display: block;
    margin-bottom: 6px;
  }

  .sa-block ul { margin: 6px 0 0; padding-left: var(--sp-5); }
  .sa-block p { margin: var(--sp-1) 0; }

  .sa-your-answer {
    white-space: pre-wrap;
    font-style: italic;
    color: var(--muted);
  }

  /* ---- Short-answer AI grading card ---- */

  .sa-grading-card {
    margin: var(--sp-3) 0 var(--sp-4);
    padding: var(--sp-4);
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--r-md);
  }

  .sa-grading-loading {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    font-size: var(--fs-sm);
    font-weight: 600;
    color: var(--muted);
    padding: var(--sp-1) 0;
  }

  .sa-grading-header {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    margin-bottom: var(--sp-2);
  }

  .sa-grading-body {
    margin: var(--sp-2) 0 var(--sp-3);
    font-size: var(--fs-body);
    line-height: 1.55;
  }

  .sa-grading-feedback {
    margin: 0 0 var(--sp-2);
    color: var(--ink);
    font-weight: 500;
  }

  .sa-grading-covered {
    margin: 0 0 var(--sp-1);
    font-size: var(--fs-sm);
    color: color-mix(in oklab, var(--success) 85%, var(--ink));
  }

  .sa-grading-missing {
    margin: 0 0 var(--sp-1);
    font-size: var(--fs-sm);
    color: color-mix(in oklab, var(--danger) 85%, var(--ink));
  }

  .sa-grading-footer {
    margin-top: var(--sp-3);
    padding-top: var(--sp-3);
    border-top: 1px solid var(--border);
  }

  .sa-grading-label {
    display: block;
    font-size: var(--fs-xs);
    font-weight: 600;
    color: var(--muted);
    margin-bottom: var(--sp-2);
  }

  .sa-buttons {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: var(--sp-2);
  }

  .sa-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-height: 44px;
    padding: 8px 6px;
    border-radius: var(--r-md);
    background: var(--surface);
    color: var(--ink-soft);
    border: 1px solid var(--border-strong);
    font: inherit;
    font-size: var(--fs-xs);
    font-weight: 600;
    white-space: nowrap;
    cursor: pointer;
    transition: all 140ms var(--ease-out);
  }

  .sa-btn .icon {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
    transition: transform 120ms var(--ease-out);
  }

  .sa-btn-got_it .icon { color: var(--success); }
  .sa-btn-partial .icon { color: var(--warn); }
  .sa-btn-didnt_know .icon { color: var(--danger); }

  .sa-btn:hover:not(:disabled) {
    background: var(--surface-2);
    border-color: var(--border-strong);
    color: var(--ink);
  }

  .sa-btn:active:not(:disabled) {
    transform: scale(0.97);
  }

  .sa-btn.is-active {
    font-weight: 700;
  }
  .sa-btn.is-active .icon {
    transform: scale(1.1);
  }

  .sa-btn-got_it.is-active {
    background: var(--success-soft);
    border-color: var(--success);
    color: color-mix(in oklab, var(--success) 85%, var(--ink));
    box-shadow: inset 0 0 0 1px var(--success);
  }

  .sa-btn-partial.is-active {
    background: var(--warn-soft);
    border-color: var(--warn);
    color: color-mix(in oklab, var(--warn) 85%, var(--ink));
    box-shadow: inset 0 0 0 1px var(--warn);
  }

  .sa-btn-didnt_know.is-active {
    background: var(--danger-soft);
    border-color: var(--danger);
    color: color-mix(in oklab, var(--danger) 85%, var(--ink));
    box-shadow: inset 0 0 0 1px var(--danger);
  }

  @media (max-width: 360px) {
    .sa-btn {
      flex-direction: column;
      gap: 2px;
      padding: 6px 2px;
      font-size: 11px;
    }
  }

  .sa-recorded {
    margin: var(--sp-2) 0 0;
    font-size: var(--fs-sm);
    color: var(--muted);
  }
  .sa-recorded strong {
    color: var(--ink);
  }

  /* ---- Read Aloud control (mountSpeechControl) ---- */

  .speech-control {
    display: inline-flex;
    align-items: center;
    gap: var(--sp-2);
    margin: var(--sp-3) 0;
  }

  .speech-trigger,
  .speech-toggle,
  .speech-stop {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    flex: none;
    padding: 0;
    border-radius: var(--r-full);
    background: var(--surface);
    color: var(--ink);
    border: 1px solid var(--border-strong);
    cursor: pointer;
    transition: background 140ms var(--ease-out), border-color 140ms var(--ease-out), color 140ms var(--ease-out), transform 100ms var(--ease-out);
  }
  .speech-trigger:hover:not(:disabled),
  .speech-toggle:hover:not(:disabled),
  .speech-stop:hover:not(:disabled) {
    background: var(--accent-soft);
    border-color: var(--accent);
    color: var(--accent-strong);
  }
  .speech-trigger:active:not(:disabled),
  .speech-toggle:active:not(:disabled),
  .speech-stop:active:not(:disabled) {
    transform: scale(0.95);
  }
  .speech-trigger.is-active {
    background: var(--accent-soft);
    border-color: var(--accent);
    color: var(--accent-strong);
  }
  .speech-trigger .icon,
  .speech-toggle .icon {
    width: 20px;
    height: 20px;
  }
  .speech-stop .icon {
    width: 18px;
    height: 18px;
    color: var(--danger);
  }
  .speech-toggle[hidden],
  .speech-stop[hidden] {
    display: none !important;
  }
  .speech-toggle[aria-busy="true"] { opacity: 1; cursor: progress; }

  .speech-spinner {
    display: inline-block;
    width: 18px;
    height: 18px;
    border: 2px solid var(--border-strong);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: border-spin 1s linear infinite;
  }
  @keyframes border-spin {
    to { transform: rotate(360deg); }
  }

  .speech-status {
    flex-basis: 100%;
    margin: var(--sp-1) 0 0;
    color: color-mix(in oklab, var(--danger) 80%, var(--ink));
    font-size: var(--fs-sm);
    font-weight: 600;
  }

  /* The status line is now reserved for errors, so an empty one must not leave a
     gap under the controls. */
  .speech-status:empty {
    display: none;
  }

  /* ---- AI Tutor (inline chat) ---- */

  .ai-tutor { margin: var(--sp-3) 0 var(--sp-1); }

  .ai-tutor-host { margin-top: 10px; }

  .ai-tutor-panel {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    background: var(--surface);
    overflow: hidden;
  }

  /* The tutor sheet has a height of its own (see `.sheet:has(...)` below), so the
     body chain has to pass it down for the transcript to flex into it. */
  .ai-tutor-sheet-body {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
  }

  /* Chat scroll: the transcript is the flexible part of the panel, so it takes
     every pixel the composer leaves. It used to subtract a guessed 420px from
     the sheet ceiling; the real head + composer + chips is ~280px, so the answer
     lost ~140px for nothing and the sheet never reached its own height. */
  .ai-tutor-messages {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: var(--sp-3) 14px;
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
  }

  .ai-msg { display: flex; flex-direction: column; gap: 6px; }
  .ai-msg-user { align-items: flex-end; }
  .ai-msg-assistant { align-items: flex-start; }

  .ai-msg-bubble {
    max-width: 88%;
    padding: 9px var(--sp-3);
    border-radius: var(--r-sm);
    font-size: var(--fs-body);
    line-height: 1.5;
    overflow-wrap: anywhere;
  }
  .ai-msg-user .ai-msg-bubble { background: var(--btn-primary); color: var(--on-accent); }
  .ai-msg-assistant .ai-msg-bubble { background: var(--surface-2); color: var(--ink); border: 1px solid var(--border); }

  .ai-msg-line { min-height: 0; }
  .ai-msg-break { height: 6px; }

  /* The tutor is working: three dots that take turns lifting. base.css already
     freezes animations under reduced motion, and three still dots on their own
     read as "waiting for an answer". */
  .ai-msg-bubble.ai-typing {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 13px var(--sp-3);
  }
  .ai-typing-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--muted);
    animation: ai-typing-rise 1.05s var(--ease-out) infinite;
  }
  .ai-typing-dot:nth-child(2) { animation-delay: 130ms; }
  .ai-typing-dot:nth-child(3) { animation-delay: 260ms; }

  @keyframes ai-typing-rise {
    0%, 55%, 100% { transform: translateY(0); opacity: .45; }
    25% { transform: translateY(-4px); opacity: 1; }
  }

  /* The starter chips, last in the sheet under the composer. Compact and
     content-sized — deliberately not full-width buttons. */
  .ai-tutor-chips {
    display: flex;
    flex-wrap: wrap;
    gap: var(--sp-2);
    padding: var(--sp-2) 14px var(--sp-3);
  }

  /* Hint chips are topic chips wearing the same clothes: --accent-soft fill,
     --accent-strong label, --r-full, no border (the border they used to carry
     was the one visual difference from `button.chip` on the setup screens).
     They are real buttons, so they take the 44px minimum target. */
  .chip-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 1 auto;
    min-height: 44px;
    padding: var(--sp-2) var(--sp-4);
    border: 1px solid transparent;
    border-radius: var(--r-full);
    background: var(--accent-soft);
    color: var(--accent-strong);
    font: inherit;
    font-size: var(--fs-sm);
    font-weight: 600;
    line-height: 1.3;
    cursor: pointer;
  }
  .chip-button:hover:not(:disabled) {
    /* One step more saturated than the resting fill. */
    background: color-mix(in oklab, var(--accent-soft) 72%, var(--accent));
    color: var(--accent-strong);
  }
  .chip-button:disabled { opacity: 0.5; cursor: not-allowed; }

  /* Footer button row: [Send primary compact, Clear Chat ghost]. Content-sized
     and left-aligned — never stretched across the sheet. */
  .ai-tutor-actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: flex-start;
    gap: var(--sp-2);
    flex: 1 1 100%;
  }
  .ai-tutor-actions > button {
    flex: 0 1 auto;
    width: auto;
    min-width: 120px;
  }

  /* Compact size for in-sheet buttons: tighter label and padding than the
     56px action-bar buttons, but never below the 44px target floor (§7.1). */
  .btn-compact {
    padding: 7px var(--sp-3);
    font-size: var(--fs-sm);
    min-height: 44px;
  }

  .ai-tutor-footer {
    display: flex;
    flex-wrap: wrap;
    gap: var(--sp-2);
    align-items: flex-start;
    padding: 10px 14px;
    border-top: 1px solid var(--border);
  }
  /* The line only separates the chat from the composer. With no chat the
     footer is the first block in the box, and its own line would double the
     panel border it sits against. */
  .ai-tutor-messages[hidden] + .ai-tutor-footer { border-top: 0; }

  .ai-tutor-input {
    flex: 1 1 100%;
    padding: 10px var(--sp-3);
    font: inherit;
    border: 1px solid var(--border-strong);
    border-radius: var(--r-sm);
    background: var(--surface);
    resize: vertical;
    /* Same box as the short-answer field on the question card: a typed answer
       needs room to be read back before it is sent. */
    min-height: 120px;
  }

  .ai-tutor-input:focus-visible { border-color: var(--accent); }

  /* Phone: once the starters are retired the composer should not keep the height
     it needed for a first question. Half the field, and answers no longer broken
     apart by an 88% cap, hand the sheet back to the transcript. */
  @media (max-width: 479px) {
    .ai-tutor-panel.is-chatting .ai-tutor-input { height: 60px; min-height: 60px; }
    .ai-msg-bubble { max-width: 100%; }
  }

  .ai-tutor-error {
    flex-basis: 100%;
    margin: 0;
    color: color-mix(in oklab, var(--danger) 80%, var(--ink));
    font-size: var(--fs-sm);
    font-weight: 600;
  }

  .ai-tutor-link {
    color: var(--accent-strong);
    font-weight: 700;
    text-decoration: underline;
    white-space: nowrap;
  }
  .ai-tutor-link:hover { color: var(--accent-strong); }

  .ai-tts-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px;
  }

  /* Chat-message Read Aloud: the same circular buttons, kept compact. */
  .ai-tts-bar .speech-control { margin: 0; gap: 6px; }
  .ai-tts-bar .speech-trigger,
  .ai-tts-bar .speech-toggle,
  .ai-tts-bar .speech-stop { width: 36px; height: 36px; }
  .ai-tts-bar .speech-trigger .icon,
  .ai-tts-bar .speech-toggle .icon { width: 18px; height: 18px; }
  .ai-tts-bar .speech-stop .icon { width: 16px; height: 16px; }

  .ai-tts-status { display: inline-flex; align-items: center; }

  /* ---- Markdown rendering + TTS spinner ---- */

  .spinner {
    display: inline-block;
    width: 14px;
    height: 14px;
    border: 2px solid var(--border-strong);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
    vertical-align: -2px;
    margin-right: 6px;
  }

  @keyframes spin {
    to { transform: rotate(360deg); }
  }

  .tutor-md p { margin: 0 0 6px; }
  .tutor-md p:last-child { margin-bottom: 0; }
  .tutor-md ul, .tutor-md ol { margin: var(--sp-1) 0 var(--sp-2); padding-left: var(--sp-5); }
  .tutor-md li { margin: 2px 0; }
  .tutor-md h4, .tutor-md h5, .tutor-md h6 { margin: 10px 0 var(--sp-1); font-weight: 700; line-height: 1.3; }
  .tutor-md h4 { font-size: 1.18em; }
  .tutor-md h5 { font-size: 1.09em; }
  .tutor-md h6 { font-size: 1em; }
  .tutor-md code {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--r-xs);
    padding: 1px var(--sp-1);
    font-size: 0.9em;
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  }
  .tutor-md pre {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--r-xs);
    padding: var(--sp-2) 10px;
    overflow-x: auto;
    margin: 6px 0;
  }
  .tutor-md pre code { background: none; border: none; padding: 0; }
  .tutor-md strong { font-weight: 700; }
  .tutor-md em { font-style: italic; }
  .tutor-md .math {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    background: var(--surface-2);
    border-radius: var(--r-xs);
    padding: 0 3px;
    white-space: nowrap;
  }

  /* Markdown tables from the tutor. The wrapper scrolls horizontally so a wide
     table never breaks the layout on ~390px / ~320px screens. */
  .tutor-md .table-wrap {
    margin: var(--sp-2) 0;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .tutor-md table.tutor-table {
    border-collapse: collapse;
    width: 100%;
    font-size: 0.95em;
  }
  .tutor-md table.tutor-table th,
  .tutor-md table.tutor-table td {
    border: 1px solid var(--border);
    padding: 6px 10px;
    vertical-align: top;
    line-height: 1.4;
  }
  .tutor-md table.tutor-table thead th {
    background: var(--surface-2);
    font-weight: 600;
    white-space: nowrap;
  }

  /* ---- Session Review (shared component) ---- */

  .session-review { margin: 14px 0; }

  .session-review-header {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--sp-3);
    margin-bottom: var(--sp-3);
  }

  .session-review-tabs {
    display: flex;
    gap: 6px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 2px;
    max-width: 100%;
  }

  .session-review-tabs::-webkit-scrollbar {
    display: none;
  }

  .review-tab {
    flex-shrink: 0;
    white-space: nowrap;
    padding: 6px var(--sp-3);
    min-height: 40px;
    font: inherit;
    font-size: var(--fs-sm);
    font-weight: 600;
    background: var(--surface);
    color: var(--ink);
    border: 1px solid var(--border);
    border-radius: var(--r-full);
    cursor: pointer;
    transition: background 120ms ease, border-color 120ms ease, color 120ms ease;
  }

  @media (hover: hover) and (pointer: fine) {
    .review-tab:hover { border-color: var(--accent); }
  }

  .review-tab.is-active {
    background: var(--btn-primary);
    color: var(--on-accent);
    border-color: var(--btn-primary);
  }

  .session-review-retry {
    padding: 8px 16px;
    font-size: var(--fs-sm);
    font-weight: 600;
    border-radius: var(--r-full);
    white-space: nowrap;
  }

  .session-review-rows {
    display: flex;
    flex-direction: column;
    gap: var(--sp-2);
  }

  .session-review-row {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    width: 100%;
    padding: var(--sp-3) var(--sp-4);
    font: inherit;
    text-align: left;
    background: var(--surface);
    color: var(--ink);
    border: 1px solid var(--border);
    border-radius: var(--r-md);
    cursor: pointer;
    box-shadow: var(--shadow-1);
    transition: border-color 0.14s ease, box-shadow 0.14s ease;
  }

  @media (hover: hover) and (pointer: fine) {
    .session-review-row:hover {
      border-color: var(--accent);
      box-shadow: var(--shadow-2);
    }
    .session-review-row:hover .icon-circle {
      color: var(--accent);
      border-color: var(--accent);
      background: var(--accent-soft);
      transform: translateX(2px);
    }
  }

  .session-review-row .icon-circle {
    color: var(--muted);
    flex-shrink: 0;
    align-self: center;
    margin-left: auto;
    transition: color 120ms ease, transform 120ms ease, background 120ms ease, border-color 120ms ease;
  }

  .session-review-content {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 6px;
    flex: 1;
    min-width: 0;
  }

  .session-review-row-header {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    min-width: 0;
    max-width: 100%;
  }

  /* Status chip: soft token fill + strong token label + a sprite icon. */
  .status-chip {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
    padding: 2px 10px;
    border-radius: var(--r-full);
    background: var(--surface-2);
    color: var(--muted);
    font-size: var(--fs-xs);
    font-weight: 700;
    white-space: nowrap;
  }
  .status-chip.is-correct { background: var(--success-soft); color: color-mix(in oklab, var(--success) 80%, var(--ink)); }
  .status-chip.is-incorrect { background: var(--danger-soft); color: var(--danger); }
  .status-chip.is-needs-review { background: var(--warn-soft); color: color-mix(in oklab, var(--warn) 80%, var(--ink)); }
  .status-chip.is-unanswered { background: var(--surface-2); color: var(--muted); }

  .session-review-meta {
    flex: 1;
    min-width: 0;
    color: var(--muted);
    font-size: var(--fs-xs);
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .session-review-prompt {
    width: 100%;
    min-width: 0;
    font-size: var(--fs-sm);
    font-weight: 600;
    color: var(--ink);
    line-height: 1.45;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  @media (max-width: 640px) {
    .session-review-header {
      flex-direction: column;
      align-items: stretch;
      gap: var(--sp-3);
    }
    .session-review-tabs {
      width: 100%;
      padding-left: 2px;
      padding-right: 2px;
    }
    .session-review-retry {
      width: 100%;
      justify-content: center;
    }
    .session-review-row {
      padding: var(--sp-3) var(--sp-3);
      gap: var(--sp-2);
    }
  }

/* ===== App shell: topbar / rail / tabbar / action bar / sheet / toast ===== */

.shell-topbar {
  position: sticky;
  top: 0;
  z-index: var(--z-topbar);
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-4);
  background: color-mix(in oklab, var(--bg) 82%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.topbar-titles { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
/* Name and session counter share one baseline row; the title keeps the ellipsis
   budget and the counter never shrinks. */
.topbar-name { display: flex; align-items: baseline; gap: var(--sp-2); min-width: 0; }
.topbar-name > .topbar-title { flex: 0 1 auto; min-width: 0; }
.topbar-name > .topbar-counter { flex: 0 0 auto; }
.topbar-overline {
  font-size: var(--fs-overline); font-weight: 600;
  letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted);
}
.topbar-title {
  margin: 0;
  font-family: var(--font-display); font-weight: 800; font-size: var(--fs-h1);
  line-height: 1.15;
  color: var(--ink); overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
/* When the bar carries the page's h1 it carries the wave too. The wave hangs
   below the line box, and its stroke paints past the svg box, while the name
   needs overflow: hidden for the ellipsis. Padding gives the clip box that
   room, the negative margin hands it straight back, so the bar height does not
   change and the stroke stays inside the bar's own bottom padding. */
h1.topbar-title {
  padding-bottom: 0.5em;
  margin-bottom: -0.5em;
}
/* The bar's name is a heading only while the screen has no h1 of its own; the
   div variant keeps this exact typography. */
.topbar-right { display: flex; align-items: center; gap: var(--sp-2); margin-left: auto; }
/* The longest screen name ("Check your knowledge") needs 253px and the bar
   leaves 246 at 390px. The exit circle and the toggle keep their 44px targets,
   so the air between them is what gives. */
@media (max-width: 479px) {
  .shell-topbar { padding-left: var(--sp-3); padding-right: var(--sp-3); gap: var(--sp-2); }
}
/* Same face, size and line height as the name it sits beside — only the color
   stays quieter, so the pair reads as one label rather than a heading plus a
   footnote. */
.topbar-counter { font: 800 var(--fs-h1)/1.15 var(--font-display); color: var(--muted); }
.topbar-theme, .sheet-close, .rail-theme {
  display: inline-flex; align-items: center; justify-content: center;
  width: 44px; height: 44px; flex: 0 0 auto;
  border: 1px solid var(--border); border-radius: var(--r-full);
  background: var(--surface); color: var(--ink); cursor: pointer;
  text-decoration: none;
  transition: background var(--dur-fast) var(--ease-out);
}
.topbar-theme:hover, .sheet-close:hover, .rail-theme:hover {
  background: var(--surface-2);
}
/* Exit is the reference circle: it carries .icon-circle--lg, which already
   paints the surface fill, radius and shadow-1, so it must not add the group
   border on top (double chrome). */
.topbar-exit {
  padding: 0;
  font: inherit;
  cursor: pointer;
  text-decoration: none;
  transition: background var(--dur-fast) var(--ease-out), box-shadow var(--dur-fast) var(--ease-out);
}
.topbar-exit:hover { background: var(--surface-2); }

.shell-rail { display: none; }

.shell-tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: var(--z-tabbar);
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  min-height: 56px;
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in oklab, var(--surface) 92%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-top: 1px solid var(--border);
}
.tab-item {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; min-height: 56px; padding: 6px 2px;
  min-width: 0;
  border: 0; background: none; cursor: pointer;
  color: var(--muted); text-decoration: none;
  transition: color var(--dur-fast) var(--ease-out);
}
/* The active tab is 11px label text on the tab bar's own surface: --accent
   (66%) is a 3.27:1 there, --accent-strong is 5.84:1 and still reads as orange. */
.tab-item[aria-current="page"] { color: var(--accent-strong); }
.tab-label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.action-bar {
  position: fixed;
  left: 0; right: 0;
  /* The tab bar grows by the bottom safe area when the browser's own toolbar
     collapses, so this offset has to carry the same inset or the action row
     slides behind it. */
  bottom: calc(56px + env(safe-area-inset-bottom, 0px));
  z-index: var(--z-actionbar);
  display: flex; align-items: center; justify-content: flex-end; gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-4);
  background: var(--surface);
  border-top: 1px solid var(--border);
  box-shadow: var(--shadow-3);
}
.action-bar[hidden] { display: none; }
/* Action-bar buttons keep their own size and sit right-aligned — no full-width
   stretching. Views hand setActionBar one node or an array (Next / Mark /
   Start / Finish Early), all <button>s. */
.action-bar button {
  flex: 0 1 auto;
  min-width: 160px;
  max-width: 360px;
  min-height: 56px;
}
/* On narrow screens (390px with three actions) the row shares the width so
   Next/Mark/Start still fit without overflowing. */
@media (max-width: 480px) {
  .action-bar { justify-content: stretch; }
  .action-bar button { flex: 1 1 auto; min-width: 0; }
}

body.focus-mode .shell-tabbar,
body.focus-mode .shell-rail,
body.focus-mode .shell-footer { display: none !important; }
body.focus-mode .action-bar { bottom: env(safe-area-inset-bottom, 0px); }

@media (min-width: 1024px) {
  .shell-rail {
    display: flex; flex-direction: column; align-items: center;
    position: fixed; top: 0; left: 0; bottom: 0;
    z-index: var(--z-topbar);
    width: 88px;
    gap: var(--sp-3);
    padding: var(--sp-5) var(--sp-3);
    background: var(--surface);
    border-right: 1px solid var(--border);
  }
  /* Rail gutter. The rail is an opaque fixed panel painted AFTER the sticky
     topbar (same z-index), so without this offset it covers the topbar's first
     88px — the exit button plus the left of the topbar title ("Learn" rendered
     as "earn"). The gutter goes on <body>, not on `.app-main { margin-left }`,
     because @layer screens declares `.app-main { margin: 0 auto }` and outranks
     this layer, which silently cancelled the previous margin-left. */
  body:not(.focus-mode) { padding-left: 88px; }
  body:not(.focus-mode) .action-bar { left: 88px; }
  .shell-tabbar { display: none; }
  .action-bar { bottom: 0; }
  /* Desktop: the bar is no longer a strip. shell.js moves the same node to sit
     right under the screen's card, where it keeps only the row of buttons — no
     panel, border or shadow, and static positioning makes the left/bottom
     offsets above inert. */
  .action-bar--inline {
    position: static;
    z-index: auto;
    max-width: 760px;
    margin: var(--sp-5) auto 0;
    padding: 0;
    background: none;
    border-top: 0;
    box-shadow: none;
  }
  /* One row spanning the card: the primary action takes the larger share. */
  .action-bar--inline button { flex: 1 1 0; min-width: 0; max-width: none; }
  .action-bar--inline .btn-primary { flex: 2 1 0; }
}
.rail-item {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 4px; width: 68px; min-height: 56px; padding: var(--sp-2) 4px;
  border-radius: var(--r-md);
  border: 0; background: none; cursor: pointer;
  color: var(--muted); text-decoration: none;
  font-size: 11.5px; font-weight: 600;
  white-space: nowrap;
  transition: color var(--dur-fast) var(--ease-out), background var(--dur-fast) var(--ease-out);
}
.rail-item:hover { background: var(--surface-2); }
/* Active rail item: label text sits on --accent-soft, so it takes the strong
   orange (4.0:1 --accent fails AA there, 6.0:1 passes). */
.rail-item[aria-current="page"] { color: var(--accent-strong); background: var(--accent-soft); }
.rail-theme { margin-top: auto; }

/* ---- App shell footer (ultra-minimal, quiet technical text) ---- */

.shell-footer {
  max-width: 680px;
  margin: var(--sp-9) auto 0;
  padding: 0 var(--sp-4) calc(var(--sp-8) + 64px + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  background: transparent;
  border: none;
  box-shadow: none;
}

/* When a fixed bottom action-bar is active above the tab bar (e.g. Practice setup,
   Simulation setup, Weak topics on mobile), expand footer padding so footer text
   and interactive links (Sources & References, Feedback, Terms) are fully visible
   and clickable above the Start/action button. */
.shell-footer--with-action-bar,
body.has-fixed-action-bar .shell-footer,
body:has(.action-bar:not([hidden]):not(.action-bar--inline)) .shell-footer {
  padding-bottom: calc(var(--sp-8) + 144px + env(safe-area-inset-bottom, 0px));
}

@media (min-width: 1024px) {
  .shell-footer {
    padding-bottom: var(--sp-8);
  }
  .shell-footer--with-action-bar,
  body.has-fixed-action-bar .shell-footer,
  body:has(.action-bar:not([hidden]):not(.action-bar--inline)) .shell-footer {
    padding-bottom: calc(var(--sp-8) + 84px);
  }
  body:has(.action-bar--inline) .shell-footer {
    padding-bottom: var(--sp-8);
  }
}

@media (max-width: 639px) {
  .shell-footer { margin-top: var(--sp-6); }
}

.footer-text,
.footer-subtext {
  margin: 0;
  font-family: var(--font-body);
  font-size: 0.7rem;
  line-height: 1.45;
  color: var(--muted);
  opacity: 0.65;
  text-align: center;
}

.footer-text {
  margin-bottom: 2px;
}

.footer-links {
  margin: var(--sp-2) 0 0;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: var(--sp-1) var(--sp-2);
}

.footer-link {
  display: inline-flex;
  align-items: center;
  min-height: 32px;
  padding: 2px 4px;
  font-family: var(--font-body);
  font-size: 0.725rem;
  font-weight: 500;
  color: var(--accent-strong);
  text-decoration: underline;
  text-underline-offset: 3px;
  transition: color var(--dur-fast) var(--ease-out);
}

.footer-link:hover {
  color: var(--accent);
}

button.footer-link-btn {
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
}

.footer-dot {
  color: var(--muted);
  opacity: 0.6;
  font-size: 0.725rem;
}



/* Sheet: centered dialog — inset + margin auto pins the card to the middle on
   both axes; the old bottom-anchored translate pattern is gone. */
.sheet {
  position: fixed;
  inset: 0;
  margin: auto;
  width: min(560px, calc(100% - 32px));
  max-height: min(80vh, 720px);
  display: flex;
  flex-direction: column;
  border: 0;
  border-radius: var(--r-xl);
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow-3);
  padding: var(--sp-4);
  overflow: hidden;
  opacity: 0;
  transform: scale(0.96);
  transition:
    transform var(--dur-med) var(--ease-out) allow-discrete,
    opacity var(--dur-med) var(--ease-out) allow-discrete,
    overlay var(--dur-med) var(--ease-out) allow-discrete,
    display var(--dur-med) var(--ease-out) allow-discrete;
}
.sheet[open] { opacity: 1; transform: scale(1); }
/* The tutor is a chat: once there is a conversation, a steady height reads better
   than a card that grows and shrinks with every answer, and it is what lets the
   transcript flex to fill the room. `dvh` keeps the card inside the visible
   viewport while a mobile browser shows its address bar. An empty chat hides the
   transcript (src/ui/tutor.js), and that case stays content-sized: no tall card
   with a dead empty field. */
.sheet:has(.ai-tutor-messages:not([hidden])) {
  height: min(88dvh, 760px);
  max-height: 88dvh;
}
.sheet::backdrop {
  /* --scrim: an ink veil in light, a black veil in dark (ink-in-dark would
     fog the page white). */
  background: var(--scrim);
  opacity: 0;
  transition:
    opacity var(--dur-med) var(--ease-out) allow-discrete,
    overlay var(--dur-med) var(--ease-out) allow-discrete,
    display var(--dur-med) var(--ease-out) allow-discrete;
}
.sheet[open]::backdrop { opacity: 1; }
@starting-style {
  .sheet[open]::backdrop { opacity: 0; }
}
.sheet-head { display: flex; align-items: center; gap: var(--sp-3); flex: none; margin-bottom: var(--sp-3); }
.sheet-head h2, .sheet-title {
  flex: 0 1 auto; margin: 0;
  font-family: var(--font-display); font-weight: 800; font-size: var(--fs-h2);
}
/* The close button owns the far edge, so a `meta` chip stays beside the title
   instead of drifting toward it. */
.sheet-close { margin-left: auto; }
.sheet-body { flex: 1 1 auto; min-height: 0; overflow: auto; font-size: var(--fs-body); color: var(--ink-soft); }
/* Actions sit inside the card at the bottom. A centered sheet never reaches
   the screen edge, so plain padding applies — no safe-area inset. Buttons keep
   their own width (min 120px) and start from the left — no full-width row. */
.sheet-actions {
  flex: none;
  display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-start; gap: var(--sp-3);
  padding: var(--sp-4) 0 0;
}
.sheet-actions > button {
  flex: 0 1 auto;
  width: auto;
  min-width: 120px;
}

/* Toasts */
.toast-stack {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(72px + env(safe-area-inset-bottom, 0px));
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sp-2);
  width: calc(100vw - 32px);
  max-width: 440px;
  pointer-events: none;
}
.toast {
  display: inline-flex;
  align-items: flex-start;
  gap: var(--sp-2);
  width: auto;
  max-width: 100%;
  padding: 10px 16px;
  border-radius: var(--r-md);
  background: var(--ink);
  color: var(--bg);
  font-size: var(--fs-sm);
  font-weight: 500;
  line-height: 1.45;
  box-shadow: var(--shadow-3, 0 8px 24px rgba(0, 0, 0, 0.18));
  pointer-events: auto;
  text-align: left;
  word-break: break-word;
}
.toast .icon {
  flex-shrink: 0;
  margin-top: 2px;
}
.toast span {
  flex: 1;
  min-width: 0;
}
/* Toast icons sit on the inverted --ink pill, so in dark they land on a near-
   white field where raw accents only reach ~2:1. Pulling each one 38% toward
   --bg moves the accent AWAY from the toast field in both themes (4.4–5.5:1 in
   dark, 6.2–8.6:1 in light). */
.toast--info .icon { color: color-mix(in oklab, var(--accent) 62%, var(--bg)); }
.toast--success .icon { color: color-mix(in oklab, var(--success) 62%, var(--bg)); }
.toast--danger .icon { color: color-mix(in oklab, var(--danger) 62%, var(--bg)); }
.toast--warn .icon { color: color-mix(in oklab, var(--warn) 62%, var(--bg)); }

  .study-card { margin-top: var(--sp-3); }

  /* ---- Responsive (component-level) ---- */

  @media (max-width: 380px) {
    .choice { padding: 10px var(--sp-3); }
    .btn, .btn-primary, .btn-secondary, .btn-danger, .sa-btn, .btn-mark { padding: var(--sp-2) var(--sp-3); }
  }
}

/* ---- Question tools bar (Speech + Hint on the same line) ---- */

.q-tools {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--sp-2);
  margin: var(--sp-3) 0 var(--sp-4);
}

.q-tools .speech-control {
  margin: 0;
}

.q-tools .hint-control {
  display: contents;
}

.q-tools .hint-popover {
  flex-basis: 100%;
  width: 100%;
}

/* ---- Hint (💡 nudge, collapsed until asked for) ---- */

.hint-control {
  margin: 0 0 var(--sp-3);
}

.btn-hint {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-1);
  padding: 0 var(--sp-4);
  height: 44px;
  min-height: 44px;
  background: var(--surface);
  color: var(--accent-strong);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-full);
  font: inherit;
  font-size: var(--fs-sm);
  font-weight: 600;
  cursor: pointer;
  transition: background 140ms var(--ease-out), border-color 140ms var(--ease-out), color 140ms var(--ease-out), transform 100ms var(--ease-out);
}

.btn-hint:hover,
.btn-hint[aria-expanded="true"] {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.btn-hint:active {
  transform: scale(0.96);
}


.hint-popover {
  margin-top: var(--sp-2);
  padding: var(--sp-3) var(--sp-4);
  background: var(--accent-soft);
  border: 1px solid var(--accent-border);
  border-left: 3px solid var(--accent);
  border-radius: var(--r-sm);
}

.hint-popover[hidden] {
  display: none;
}

.hint-text {
  margin: 0;
  font-size: var(--fs-sm);
  line-height: 1.45;
  color: var(--ink);
}

/* ---- Authentication and Profile components ---- */

/* ---- Authentication & Profile Sheet & Components ---- */

.sheet.sheet-auth,
.sheet:has(.auth-modal),
.sheet:has(.profile-modal) {
  width: min(380px, calc(100% - 32px));
  max-width: 380px;
  padding: var(--sp-6) var(--sp-5) var(--sp-5);
  border-radius: var(--r-xl);
}

.sheet.sheet-auth .sheet-head {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  margin-bottom: var(--sp-2);
}

.sheet.sheet-auth .sheet-head h2 {
  font-family: var(--font-display);
  font-size: 1.35rem;
  font-weight: 800;
  letter-spacing: -0.02em;
  color: var(--ink);
  text-align: center;
  margin: 0;
}

.sheet.sheet-auth .sheet-close {
  position: absolute;
  top: -4px;
  right: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--r-full);
  color: var(--muted);
  transition: color var(--dur-fast) var(--ease-out), background var(--dur-fast) var(--ease-out);
}

.sheet.sheet-auth .sheet-close:hover {
  color: var(--ink);
  background: var(--surface-2);
}

.auth-modal {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sp-4);
  padding: 0;
  width: 100%;
  box-sizing: border-box;
}

.auth-modal-subtitle {
  margin: -4px 0 2px;
  font-size: var(--fs-xs);
  color: var(--muted);
  line-height: 1.45;
  text-align: center;
  max-width: 290px;
}

.auth-alert {
  width: 100%;
  max-width: 290px;
  padding: var(--sp-2) var(--sp-3);
  border-radius: var(--r-md);
  font-size: var(--fs-xs);
  line-height: 1.45;
  word-break: break-word;
  box-sizing: border-box;
  text-align: center;
}

.auth-alert--error {
  background: var(--danger-soft, #fee2e2);
  color: var(--danger, #dc2626);
  border: 1px solid var(--danger-border, #fca5a5);
}

.auth-alert--success {
  background: var(--success-soft, #ecfdf5);
  color: var(--success, #059669);
  border: 1px solid var(--success-border, #a7f3d0);
}

.auth-buttons-group {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  width: 100%;
}

.auth-provider-btn {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  max-width: 290px;
  height: 44px;
  min-height: 44px;
  padding: 0 44px;
  font-family: inherit;
  font-size: var(--fs-sm);
  font-weight: 600;
  border-radius: var(--r-md);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
  box-sizing: border-box;
  text-decoration: none;
  transition: background var(--dur-fast) var(--ease-out),
              border-color var(--dur-fast) var(--ease-out),
              transform var(--dur-fast) var(--ease-out);
}

.auth-provider-btn:hover:not(:disabled) {
  background: var(--surface-2);
  border-color: var(--accent);
}

.auth-provider-btn:active:not(:disabled) {
  transform: scale(0.98);
}

.auth-provider-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.auth-provider-icon {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  pointer-events: none;
}

.auth-provider-label {
  display: block;
  text-align: center;
  font-size: var(--fs-sm);
  font-weight: 600;
  line-height: 1;
  white-space: nowrap;
}

/* Apple branded button */
.auth-provider-btn--apple {
  background: #000;
  color: #fff;
  border-color: #000;
}

.auth-provider-btn--apple:hover:not(:disabled) {
  background: #222;
  border-color: #222;
}

/* Dark theme adaptation for Apple button */
[data-theme="dark"] .auth-provider-btn--apple {
  background: #fff;
  color: #000;
  border-color: #fff;
}

[data-theme="dark"] .auth-provider-btn--apple:hover:not(:disabled) {
  background: #e5e5e5;
  border-color: #e5e5e5;
}

.auth-divider {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  max-width: 290px;
  margin: var(--sp-1) 0;
  color: var(--muted);
  font-size: var(--fs-xs);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 600;
}

.auth-divider::before,
.auth-divider::after {
  content: "";
  flex: 1;
  border-bottom: 1px solid var(--border);
}

.auth-divider span {
  padding: 0 var(--sp-3);
  font-size: 11px;
}

.auth-email-form {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sp-3);
  width: 100%;
  max-width: 290px;
  box-sizing: border-box;
}

.auth-field {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
  width: 100%;
}

.auth-field-label {
  font-size: var(--fs-xs);
  font-weight: 600;
  color: var(--ink-soft);
  letter-spacing: -0.01em;
}

.auth-email-input {
  width: 100%;
  height: 44px;
  padding: 0 var(--sp-3);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-md);
  background: #ffffff;
  color: var(--ink);
  font-family: inherit;
  font-size: var(--fs-sm);
  outline: none;
  box-sizing: border-box;
  transition: border-color var(--dur-fast) var(--ease-out),
              box-shadow var(--dur-fast) var(--ease-out),
              background var(--dur-fast) var(--ease-out);
}

.auth-email-input::placeholder {
  color: var(--muted);
  opacity: 0.8;
}

.auth-email-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in oklab, var(--accent) 20%, transparent);
}

.auth-email-input:disabled {
  background: var(--surface-2);
  opacity: 0.6;
  cursor: not-allowed;
}

[data-theme="dark"] .auth-email-input {
  background: var(--surface);
  border-color: var(--border-strong);
  color: var(--ink);
}

[data-theme="dark"] .auth-email-input:disabled {
  background: var(--surface-2);
}

.auth-submit-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 44px;
  min-height: 44px;
  padding: 0 var(--sp-4);
  font-family: inherit;
  font-size: var(--fs-sm);
  font-weight: 600;
  border-radius: var(--r-md);
  cursor: pointer;
  box-sizing: border-box;
  transition: background var(--dur-fast) var(--ease-out),
              transform var(--dur-fast) var(--ease-out);
}

.auth-submit-btn:active:not(:disabled) {
  transform: scale(0.98);
}

.auth-email-hint {
  font-size: var(--fs-xs);
  color: var(--muted);
  line-height: 1.4;
  margin: 0;
  text-align: center;
}

/* ---- Auth Magic Link Sent State ---- */
.auth-sent-view {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  width: 100%;
  max-width: 310px;
  box-sizing: border-box;
  animation: kpi-in 260ms var(--ease-out) backwards;
  padding: var(--sp-2) 0 0;
}

.auth-sent-icon {
  width: 60px;
  height: 60px;
  border-radius: var(--r-full);
  background: var(--accent-soft);
  color: var(--accent-strong);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: var(--sp-3);
  box-shadow: 0 0 0 8px color-mix(in oklab, var(--accent-soft) 40%, transparent);
}

.auth-sent-text {
  margin: 0 0 var(--sp-1);
  font-size: var(--fs-xs);
  color: var(--muted);
  line-height: 1.4;
}

.auth-sent-email {
  display: inline-block;
  font-family: inherit;
  font-size: var(--fs-sm);
  font-weight: 700;
  color: var(--ink);
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 4px 12px;
  border-radius: var(--r-full);
  margin-bottom: var(--sp-3);
  word-break: break-all;
  max-width: 100%;
}

.auth-sent-hint {
  margin: 0 0 var(--sp-4);
  font-size: var(--fs-xs);
  color: var(--ink-soft);
  line-height: 1.45;
}

.auth-sent-inbox-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 44px;
  text-decoration: none;
  font-weight: 700;
  font-size: var(--fs-sm);
  border-radius: var(--r-md);
  margin-bottom: var(--sp-3);
  background: var(--btn-primary);
  color: var(--on-accent);
  box-sizing: border-box;
  transition: background var(--dur-fast) var(--ease-out),
              transform var(--dur-fast) var(--ease-out);
}

.auth-sent-inbox-btn:hover {
  background: var(--btn-primary-hover);
  color: var(--on-accent);
}

.auth-sent-inbox-btn:active {
  transform: scale(0.98);
}

.auth-sent-spam {
  margin: 0 0 var(--sp-4);
  padding: var(--sp-2) var(--sp-3);
  background: var(--surface-2);
  border-radius: var(--r-sm);
  font-size: var(--fs-xs);
  color: var(--muted);
  line-height: 1.4;
  text-align: center;
}

.auth-sent-footer {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  font-size: var(--fs-xs);
  flex-wrap: wrap;
}

.auth-sent-footer button {
  background: none;
  border: none;
  padding: 0;
  color: var(--accent-strong);
  font-family: inherit;
  font-size: var(--fs-xs);
  font-weight: 600;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
  transition: color var(--dur-fast) var(--ease-out);
}

.auth-sent-footer button:hover {
  color: var(--ink);
}

.auth-sent-footer button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  text-decoration: none;
}

.auth-sent-dot {
  color: var(--muted);
  user-select: none;
}

/* ---- Auth Loading / Syncing State ---- */
.auth-loading-view {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  width: 100%;
  max-width: 310px;
  min-height: 230px;
  box-sizing: border-box;
  animation: kpi-in 260ms var(--ease-out) backwards;
  padding: var(--sp-4) var(--sp-2);
}

.auth-loading-spinner-wrap {
  width: 64px;
  height: 64px;
  border-radius: var(--r-full);
  background: var(--accent-soft);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: var(--sp-4);
  box-shadow: 0 0 0 8px color-mix(in oklab, var(--accent-soft) 40%, transparent);
}

.auth-loading-spinner {
  width: 32px;
  height: 32px;
  border: 3.5px solid color-mix(in oklab, var(--accent) 30%, transparent);
  border-top-color: var(--btn-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  display: inline-block;
}

.auth-loading-title {
  margin: 0 0 var(--sp-1);
  font-family: var(--font-display);
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--ink);
  line-height: 1.3;
}

.auth-loading-subtext {
  margin: 0;
  font-size: var(--fs-xs);
  color: var(--muted);
  line-height: 1.45;
  max-width: 260px;
}

/* ---- Feedback & Support Modal ---- */

.sheet.sheet-feedback,
.sheet:has(.feedback-modal) {
  width: min(450px, calc(100% - 32px));
  max-width: 450px;
  padding: var(--sp-6) var(--sp-5) var(--sp-5);
  border-radius: var(--r-xl);
}

.sheet.sheet-feedback .sheet-head {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  margin-bottom: var(--sp-2);
}

.sheet.sheet-feedback .sheet-head h2 {
  font-family: var(--font-display);
  font-size: 1.35rem;
  font-weight: 800;
  letter-spacing: -0.02em;
  color: var(--ink);
  text-align: center;
  margin: 0;
}

.sheet.sheet-feedback .sheet-close {
  position: absolute;
  top: -4px;
  right: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--r-full);
  color: var(--muted);
  transition: color var(--dur-fast) var(--ease-out), background var(--dur-fast) var(--ease-out);
}

.sheet.sheet-feedback .sheet-close:hover {
  color: var(--ink);
  background: var(--surface-2);
}

.feedback-modal {
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
  width: 100%;
  box-sizing: border-box;
}

.feedback-modal-subtitle {
  margin: -4px 0 var(--sp-1);
  font-size: var(--fs-xs);
  color: var(--muted);
  line-height: 1.45;
  text-align: center;
}

.feedback-user-badge {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  padding: 6px 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-full);
  font-size: var(--fs-xs);
  color: var(--ink-soft);
  margin-bottom: var(--sp-1);
}

.feedback-form {
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
  width: 100%;
}

.feedback-field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  width: 100%;
}

.feedback-field--half {
  flex: 1 1 calc(50% - 6px);
}

.feedback-row {
  display: flex;
  gap: var(--sp-3);
  width: 100%;
}

@media (max-width: 480px) {
  .feedback-row {
    flex-direction: column;
    gap: var(--sp-3);
  }
}

.feedback-field-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
}

.feedback-field-label {
  font-size: var(--fs-xs);
  font-weight: 600;
  color: var(--ink-soft);
  letter-spacing: -0.01em;
}

.feedback-char-count {
  font-size: 0.7rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.feedback-char-count.is-near-limit {
  color: var(--danger, #e53e3e);
  font-weight: 600;
}

.feedback-category-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.feedback-chip {
  padding: 5px 11px;
  border-radius: var(--r-full);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--ink-soft);
  font-family: inherit;
  font-size: var(--fs-xs);
  font-weight: 500;
  cursor: pointer;
  transition: all var(--dur-fast) var(--ease-out);
  white-space: nowrap;
}

.feedback-chip:hover {
  border-color: var(--accent);
  color: var(--ink);
  background: var(--surface-2);
}

.feedback-chip.is-selected {
  background: var(--accent-soft);
  border-color: var(--accent-strong);
  color: var(--accent-strong);
  font-weight: 600;
}

.feedback-chip--danger {
  border-color: rgba(229, 62, 62, 0.35);
  color: var(--ink-soft);
}

.feedback-chip--danger:hover {
  border-color: var(--danger, #e53e3e);
  color: var(--danger, #e53e3e);
  background: rgba(229, 62, 62, 0.08);
}

.feedback-chip--danger.is-selected {
  background: rgba(229, 62, 62, 0.12);
  border-color: var(--danger, #e53e3e);
  color: var(--danger, #e53e3e);
  font-weight: 600;
}

.feedback-input,
.feedback-textarea {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--r-md);
  background: #ffffff;
  color: var(--ink);
  font-family: inherit;
  font-size: var(--fs-sm);
  outline: none;
  box-sizing: border-box;
  transition: border-color var(--dur-fast) var(--ease-out),
              box-shadow var(--dur-fast) var(--ease-out);
}

.feedback-input {
  height: 40px;
}

.feedback-textarea {
  resize: vertical;
  min-height: 85px;
  line-height: 1.45;
}

.feedback-input:focus,
.feedback-textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in oklab, var(--accent) 20%, transparent);
}

.feedback-input:disabled,
.feedback-textarea:disabled {
  background: var(--surface-2);
  opacity: 0.6;
  cursor: not-allowed;
}

[data-theme="dark"] .feedback-input,
[data-theme="dark"] .feedback-textarea {
  background: var(--surface);
  border-color: var(--border-strong);
  color: var(--ink);
}

.feedback-hp-input {
  position: absolute !important;
  left: -9999px !important;
  width: 1px !important;
  height: 1px !important;
  opacity: 0 !important;
  pointer-events: none !important;
}

.feedback-turnstile-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 66px;
  margin: var(--sp-1) 0;
  width: 100%;
}

.feedback-turnstile-loading {
  font-size: var(--fs-xs);
  color: var(--muted);
  text-align: center;
  padding: var(--sp-2) 0;
  font-style: italic;
}

.feedback-submit-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 42px;
  font-family: inherit;
  font-size: var(--fs-sm);
  font-weight: 600;
  border-radius: var(--r-md);
  cursor: pointer;
  box-sizing: border-box;
  margin-top: var(--sp-1);
}

.feedback-success-view {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  width: 100%;
  padding: var(--sp-3) 0 var(--sp-2);
  animation: kpi-in 260ms var(--ease-out) backwards;
}

.feedback-success-icon {
  width: 64px;
  height: 64px;
  border-radius: var(--r-full);
  background: var(--accent-soft);
  color: var(--accent-strong);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: var(--sp-3);
  box-shadow: 0 0 0 8px color-mix(in oklab, var(--accent-soft) 40%, transparent);
}

.feedback-success-title {
  margin: 0 0 var(--sp-2);
  font-family: var(--font-display);
  font-size: 1.25rem;
  font-weight: 800;
  color: var(--ink);
}

.feedback-success-text {
  margin: 0 0 var(--sp-5);
  font-size: var(--fs-sm);
  color: var(--muted);
  line-height: 1.5;
  max-width: 320px;
}

.feedback-success-close-btn {
  width: 100%;
  max-width: 220px;
  height: 40px;
}

/* Header auth button */
.topbar-auth.btn-auth-signin {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 38px;
  min-height: 38px;
  width: auto;
  padding: 0 14px;
  flex: 0 0 auto;
  border: 1px solid var(--border-strong);
  border-radius: var(--r-full);
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
  text-decoration: none;
  box-sizing: border-box;
  white-space: nowrap;
  font-family: inherit;
  font-size: var(--fs-sm);
  font-weight: 600;
  transition: background var(--dur-fast) var(--ease-out),
              border-color var(--dur-fast) var(--ease-out),
              color var(--dur-fast) var(--ease-out),
              transform var(--dur-fast) var(--ease-out);
}

.topbar-auth.btn-auth-signin:hover {
  background: var(--surface-2);
  border-color: var(--accent);
  color: var(--accent-strong);
}

.topbar-auth.btn-auth-signin:active {
  transform: scale(0.97);
}

.auth-signin-text {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: -0.01em;
  line-height: 1;
}

.topbar-auth.is-authenticated {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  flex: 0 0 auto;
  border: 1px solid var(--border-strong);
  border-radius: var(--r-full);
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
  padding: 0;
  text-decoration: none;
  box-sizing: border-box;
  transition: background var(--dur-fast) var(--ease-out),
              border-color var(--dur-fast) var(--ease-out),
              transform var(--dur-fast) var(--ease-out);
}

.topbar-auth.is-authenticated:hover {
  background: var(--surface-2);
  border-color: var(--accent);
}

.topbar-auth.is-authenticated:active {
  transform: scale(0.96);
}

.auth-topbar-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  object-fit: cover;
  display: block;
}

.auth-topbar-initials {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.auth-sync-dot {
  position: absolute;
  bottom: 1px;
  right: 1px;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--success, #10b981);
  box-shadow: 0 0 0 2px var(--surface);
}

.topbar-auth[data-sync-status="syncing"] .auth-sync-dot {
  background: var(--accent, #3b82f6);
  animation: sync-pulse 1s infinite alternate;
}

.topbar-auth[data-sync-status="offline"] .auth-sync-dot {
  background: var(--muted, #94a3b8);
}

.topbar-auth[data-sync-status="error"] .auth-sync-dot {
  background: var(--danger, #ef4444);
}

@keyframes sync-pulse {
  from { opacity: 0.35; transform: scale(0.9); }
  to { opacity: 1; transform: scale(1.1); }
}

/* Profile modal styling */
.profile-modal {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sp-4);
  padding: var(--sp-1) 0;
  width: 100%;
  box-sizing: border-box;
}

.profile-card {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-4);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  width: 100%;
  max-width: 290px;
  box-sizing: border-box;
}

.profile-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: var(--fs-md);
  overflow: hidden;
  flex-shrink: 0;
}

.profile-avatar-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.profile-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.profile-name {
  font-size: var(--fs-sm);
  font-weight: 700;
  color: var(--ink);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.profile-email {
  font-size: var(--fs-xs);
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.profile-sync-row {
  display: flex;
  align-items: center;
  width: 100%;
  max-width: 290px;
}

.sync-badge {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-3);
  border-radius: var(--r-md);
  font-size: var(--fs-xs);
  font-weight: 600;
  width: 100%;
  box-sizing: border-box;
}

.sync-badge--synced {
  background: var(--success-soft, #ecfdf5);
  color: var(--success, #059669);
  border: 1px solid var(--success-border, #a7f3d0);
}

.sync-badge--syncing {
  background: var(--accent-soft, #eff6ff);
  color: var(--accent, #2563eb);
  border: 1px solid var(--accent-border, #bfdbfe);
}

.sync-badge--offline {
  background: var(--surface-2);
  color: var(--muted);
  border: 1px solid var(--border);
}

.profile-desc {
  margin: 0;
  font-size: var(--fs-xs);
  color: var(--muted);
  line-height: 1.5;
  text-align: center;
  max-width: 290px;
}

.sheet.sheet-auth .sheet-actions {
  justify-content: center;
  padding-top: var(--sp-3);
}

.sheet.sheet-auth .sheet-actions > button {
  width: 100%;
  max-width: 290px;
  height: 44px;
}

/* ---- Forced colors (high contrast) ----
   Unlayered so it outranks the cascade; in this mode the OS drops our fills
   and shadows, so every affordance that relied on one gets a CanvasText edge
   back, and focus outlines are re-asserted over the component-level rings
   (base.css only covers the plain :focus-visible selector). */
@media (forced-colors: active) {
  .choice, .icon-circle, .sheet, .toast, .badge, .pill, .status-chip,
  .btn, .btn-primary, .btn-secondary, .btn-ghost, .btn-ghost-danger, .btn-danger,
  .btn-mark, .btn-icon, .chip-button, .review-tab, .session-review-row,
  .topbar-theme, .rail-theme, .sheet-close {
    border: 1px solid CanvasText;
  }
  .choice { border: 2px solid CanvasText; }
  .choice:has(input:checked) { outline: 2px solid CanvasText; outline-offset: 2px; }
  .btn-primary, .btn-danger, .review-tab.is-active, .ai-msg-user .ai-msg-bubble,
  .choice:has(input:checked) .choice-letter,
  .choice.is-correct .choice-letter, .choice.is-wrong .choice-letter {
    forced-color-adjust: none;
    color: var(--on-accent);
  }
  .icon-circle { box-shadow: none; }
  .sheet::backdrop { background: Canvas; }
  :focus-visible,
  .choice:has(input:focus-visible),
  input.search:focus-visible,
  .ai-tutor-input:focus-visible,
  .numeric-input input[type="number"]:focus-visible,
  .short-input textarea:focus-visible {
    outline: 2px solid Highlight;
    outline-offset: 2px;
  }
}
