/* ==========================================================================
   UNIT 4 PRACTICE STUDIO — shared design system
   Used by every Unit 4 practice studio page (standard + challenging).
   Pages keep only the rules unique to their own models; everything the pages
   share (tokens, header, trail, cards, buttons, feedback, dialogs, bonus board)
   lives here so the ten pages cannot drift apart.

   Colour carries meaning: blue = fraction, teal = decimal, orange = percent.
   Every coloured thing also carries a label or a shape, never colour alone.
   Text pairs are >= 4.5:1; large display text and UI edges are >= 3:1.
   ========================================================================== */

:root {
  --paper: #F3F5F8;
  --well: #E6EBF1;
  --surface: #FFFFFF;
  --surface-soft: #F7F9FB;

  --brand: #2C4A68;
  --brand-dark: #1B2C3F;
  --brand-tint: #E5ECF3;
  --brand-line: #BBCADA;

  --sun: #F0BD4F;
  --sun-deep: #B8841A;
  --sun-tint: #FAF2DC;
  --sun-ink: #5A4310;

  --frac: #3A6AA3;
  --frac-tint: #E6EEF7;
  --frac-line: #B7CBE2;
  --dec: #1C7169;
  --dec-tint: #E0F1EE;
  --dec-line: #A3D2CC;
  --pct: #A94C16;
  --pct-tint: #FBEBDF;
  --pct-line: #EBC3A6;

  --ok: #0A7D4B;
  --ok-tint: #E4F7EC;
  --ok-line: #9ADCB8;
  --warn: #7A4A00;
  --warn-tint: #FFF3D1;
  --warn-line: #F2CB62;
  --bad: #B42318;
  --bad-tint: #FDECEA;
  --bad-line: #F4B4AE;

  --on-brand: #FFFFFF;
  --on-sun: var(--brand-dark);
  --on-ok: #FFFFFF;
  --on-warn: #FFFFFF;
  --on-frac: #FFFFFF;
  --on-dec: #FFFFFF;
  --on-pct: #FFFFFF;
  --on-bad: #FFFFFF;
  --ok-ink: #064E33;
  --bad-ink: #8A1C13;
  --warn-ink: var(--warn);

  --ink: #1B2733;
  --ink-muted: #3F4C5C;
  --ink-light: #566475;

  --border: #D5DCE5;
  --border-strong: #7A8B9F;

  --font-display: "Avenir Next Rounded", "Arial Rounded MT Bold", "Nunito", "Varela Round", ui-rounded, "Trebuchet MS", system-ui, sans-serif;
  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

  --radius-sm: 10px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --radius-pill: 999px;

  --shadow-card: 0 1px 2px rgba(27, 44, 63, 0.06), 0 6px 18px rgba(27, 44, 63, 0.07);
  --shadow-pop: 0 18px 44px rgba(15, 26, 38, 0.30);
  --lift: 4px;
}

*, *::before, *::after { box-sizing: border-box; }
html { font-size: 17px; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-sans);
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  font-variant-numeric: tabular-nums;
}
h1, h2, h3 { font-family: var(--font-display); font-weight: 800; letter-spacing: 0; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}
:focus-visible { outline: 4px solid var(--sun-deep); outline-offset: 2px; }

/* Spanish support */
.es-text { display: none; color: var(--brand); font-size: 0.95rem; margin-top: 4px; }
body.show-es .es-text { display: block; }

/* ---- Header ------------------------------------------------------------ */
.app-header { background: var(--brand-dark); color: #FFFFFF; padding: 8px 20px; }
.header-wrap {
  max-width: 1120px; margin: 0 auto; display: flex; flex-wrap: wrap;
  align-items: center; justify-content: space-between; gap: 8px 14px;
}
.brand { display: flex; align-items: center; gap: 14px; min-width: 0; }
.brand-icon {
  overflow: hidden; white-space: nowrap; letter-spacing: -1px;
  width: 38px; height: 38px; border-radius: 12px; background: var(--sun); color: var(--brand-dark);
  font-family: var(--font-display); font-size: 1.3rem; font-weight: 800;
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
  box-shadow: 0 3px 0 var(--sun-deep);
}
.brand-title { margin: 0; font-size: 1.18rem; line-height: 1.2; }
.brand > div:last-child { min-width: 0; }
.brand-sub { font-size: 0.78rem; color: #D3DCE8; margin-top: 1px; }

.header-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-left: auto; }
.score-pill {
  background: rgba(255, 255, 255, 0.1); border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: var(--radius-pill); padding: 6px 16px; font-size: 0.9rem; font-weight: 800;
  display: flex; align-items: center; gap: 8px;
}
.top-btn {
  background: transparent; color: #FFFFFF; border: 2px solid rgba(255, 255, 255, 0.45);
  border-radius: var(--radius-md); padding: 8px 14px; min-height: 44px; font-size: 0.9rem; font-weight: 800;
  cursor: pointer; font-family: inherit;
}
.top-btn:hover { background: rgba(255, 255, 255, 0.16); border-color: #FFFFFF; }
.top-btn[aria-pressed="true"] { background: var(--sun); color: var(--brand-dark); border-color: var(--sun); }
.top-btn.stars-btn { background: var(--sun); color: var(--brand-dark); border-color: var(--sun); box-shadow: 0 4px 0 var(--sun-deep); }
.top-btn.stars-btn:hover { background: #F5CE74; }
.top-btn.bonus-btn { border-color: var(--sun); color: var(--sun); }
.top-btn.bump { animation: u4-bump 0.45s ease; }

/* ---- Trail (section navigation) ---------------------------------------- */
.tab-bar {
  background: var(--surface); border-bottom: 2px solid var(--border);
  position: sticky; top: 0; z-index: 80; box-shadow: 0 4px 12px rgba(27, 44, 63, 0.06);
}
.tab-list {
  --trail: 0;
  max-width: 1120px; margin: 0 auto; padding: 7px 16px; display: grid;
  grid-template-columns: repeat(5, 1fr); gap: 4px; position: relative;
}
.tab-list::before, .tab-list::after {
  content: ""; position: absolute; top: calc(50% - 3px); left: 10%; height: 6px; border-radius: 3px;
}
.tab-list::before { right: 10%; background: var(--well); }
.tab-list::after { width: calc(80% * var(--trail)); background: var(--ok); transition: width 0.5s ease; }
.tab-btn {
  appearance: none; position: relative; z-index: 1; background: transparent; border: none;
  padding: 0 4px; cursor: pointer; font-family: inherit; display: flex; flex-direction: row;
  align-items: center; justify-content: center; gap: 6px; color: var(--ink-muted); text-align: left; min-width: 0;
  min-height: 44px;
}
.tab-btn:hover .tab-badge { border-color: var(--brand); }
.tab-badge {
  width: 32px; height: 32px; border-radius: 50%; background: var(--surface); border: 3px solid var(--border-strong);
  color: var(--ink); font-family: var(--font-display); font-size: 0.95rem; font-weight: 800;
  display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0;
  transition: transform 0.2s ease, background 0.2s ease;
}
.tab-btn[aria-selected="true"] .tab-badge {
  background: var(--brand); border-color: var(--brand); color: var(--on-brand);
  box-shadow: 0 0 0 3px var(--sun); transform: scale(1.1);
}
.tab-btn.tab-complete .tab-badge { background: var(--ok); border-color: var(--ok); color: var(--on-ok); font-size: 0; }
.tab-btn.tab-complete .tab-badge::before { content: "\2713"; font-size: 1.15rem; }
.tab-btn.tab-complete[aria-selected="true"] .tab-badge { box-shadow: 0 0 0 4px var(--sun); }
.tab-text { display: flex; flex-direction: column; min-width: 0; max-width: 100%; background: var(--surface); padding: 0 6px 0 2px; border-radius: 6px; }
.tab-name { font-size: 0.92rem; font-weight: 800; line-height: 1.25; color: var(--ink); }
.tab-btn[aria-selected="true"] .tab-name { color: var(--brand); }
/* the section's description is said, not shown: the trail stays one row and the section heading below repeats it */
.tab-desc {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden;
  clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

.main-container { max-width: 1120px; width: 100%; margin: 0 auto; padding: 14px 20px 56px; flex: 1; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* ---- Section header ---------------------------------------------------- */
.topic-strip {
  background: var(--surface); border: 2px solid var(--border); border-radius: var(--radius-lg);
  padding: 10px 18px; margin-bottom: 12px; display: flex; flex-wrap: wrap; align-items: center;
  justify-content: space-between; gap: 8px 18px; box-shadow: var(--shadow-card);
}
.topic-strip h2 { margin: 0 0 2px 0; font-size: 1.2rem; line-height: 1.25; color: var(--brand); }
.topic-strip p { margin: 0; font-size: 0.95rem; color: var(--ink-muted); max-width: 70ch; }
.key-rule-pill {
  background: var(--sun-tint); border: 2px solid var(--sun); color: var(--sun-ink);
  padding: 10px 16px; border-radius: var(--radius-md); font-size: 0.92rem; font-weight: 700;
}

/* ---- Problem stepper --------------------------------------------------- */
.stepper-bar {
  background: var(--surface); border: 2px solid var(--border); border-radius: var(--radius-lg);
  padding: 6px 12px; margin-bottom: 12px; display: flex; flex-wrap: wrap; align-items: center;
  justify-content: space-between; gap: 8px 12px; box-shadow: var(--shadow-card);
}
.dots-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.step-dot {
  min-width: 44px; height: 44px; padding: 0 10px; border-radius: var(--radius-pill);
  border: 2px solid var(--border-strong); background: var(--surface); color: var(--ink);
  font-family: var(--font-display); font-weight: 800; font-size: 0.95rem; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; transition: transform 0.15s ease, background 0.15s ease;
}
.step-dot:hover { border-color: var(--brand); background: var(--brand-tint); }
.step-dot.active { background: var(--brand); color: var(--on-brand); border-color: var(--brand); box-shadow: 0 0 0 3px var(--sun); }
.step-dot.solved { background: var(--ok-tint); border-color: var(--ok); color: var(--ok); }
.step-dot.solved.active { background: var(--ok); color: var(--on-ok); border-color: var(--ok); }

.stage-card {
  background: var(--surface); border: 2px solid var(--border); border-radius: var(--radius-lg);
  padding: 20px 24px; box-shadow: var(--shadow-card);
}
.two-col-stage { display: grid; grid-template-columns: minmax(300px, 380px) 1fr; gap: 24px; align-items: start; }
.diagram-well { background: var(--surface-soft); border: 2px solid var(--border); border-radius: var(--radius-md); padding: 14px; }

/* ---- Buttons ----------------------------------------------------------- */
.btn {
  appearance: none; border: 2px solid var(--border-strong); border-bottom-width: var(--lift);
  background: var(--surface); color: var(--brand); font-family: inherit; font-size: 1rem; font-weight: 800;
  padding: 10px 18px; min-height: 48px; border-radius: var(--radius-md); cursor: pointer; max-width: 100%; text-align: center;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  transition: transform 0.1s ease, background 0.15s ease;
}
.btn:hover { background: var(--brand-tint); border-color: var(--brand); }
.btn:active { transform: translateY(2px); border-bottom-width: 2px; margin-bottom: 2px; }
.btn[disabled] { opacity: 0.55; cursor: not-allowed; transform: none; }
.btn-primary { background: var(--brand); color: var(--on-brand); border-color: var(--brand-dark); }
.btn-primary:hover { background: var(--brand-dark); border-color: var(--brand-dark); }
.btn-go { background: var(--sun); color: var(--brand-dark); border-color: var(--sun-deep); }
.btn-go:hover { background: #F5CE74; border-color: var(--sun-deep); }
.btn-sm { padding: 6px 14px; min-height: 44px; font-size: 0.92rem; }

.speak-btn {
  background: var(--surface-soft); border: 2px solid var(--border); color: var(--brand);
  border-radius: var(--radius-md); padding: 6px 12px; min-height: 40px; font-size: 0.88rem; font-weight: 700;
  cursor: pointer; display: inline-flex; align-items: center; gap: 6px; font-family: inherit;
}
.speak-btn:hover { background: var(--brand-tint); border-color: var(--brand); }

/* ---- Answer inputs ----------------------------------------------------- */
.num-input {
  font-family: var(--font-display); font-size: 1.4rem; font-weight: 800; text-align: center;
  padding: 8px 12px; min-height: 54px; border: 3px solid var(--border-strong); border-radius: var(--radius-md);
  background: var(--surface-soft); color: var(--ink); transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.num-input:focus { border-color: var(--brand); outline: none; box-shadow: 0 0 0 4px var(--sun); }
.num-input.ok { border-color: var(--ok); background: var(--ok-tint); color: var(--ok); }
.num-input.err { border-color: var(--bad); background: var(--bad-tint); color: var(--bad); }

/* ---- Feedback ---------------------------------------------------------- */
.feedback-box {
  margin-top: 18px; padding: 14px 18px 14px 56px; border-radius: var(--radius-md); font-size: 1rem;
  font-weight: 600; display: none; position: relative; line-height: 1.5;
}
.feedback-box::before {
  position: absolute; left: 14px; top: 12px; width: 30px; height: 30px; border-radius: 50%;
  font-family: var(--font-display); font-weight: 800; font-size: 1rem; line-height: 30px; text-align: center; color: var(--on-ok);
}
.feedback-box.show { display: block; animation: u4-pop 0.22s ease-out; }
.feedback-box.ok { background: var(--ok-tint); border: 2px solid var(--ok-line); color: var(--ok-ink); }
.feedback-box.ok::before { content: "\2713"; background: var(--ok); }
.feedback-box.warn { background: var(--warn-tint); border: 2px solid var(--warn-line); color: var(--warn-ink); }
.feedback-box.warn::before { content: "?"; background: var(--sun-deep); color: var(--on-sun); }
.feedback-box.err { background: var(--bad-tint); border: 2px solid var(--bad-line); color: var(--bad-ink); }
.feedback-box.err::before { content: "!"; background: var(--bad); color: var(--on-bad); }

/* ---- Multiple choice --------------------------------------------------- */
.choice-stack { display: flex; flex-direction: column; gap: 12px; margin: 18px 0; counter-reset: u4choice; }
.choice-btn {
  appearance: none; background: var(--surface); border: 2px solid var(--border-strong); border-bottom-width: var(--lift);
  border-radius: var(--radius-md); padding: 12px 18px; min-height: 56px; text-align: left; font-family: inherit;
  font-size: 1.05rem; color: var(--ink); cursor: pointer; display: flex; align-items: center; gap: 14px;
  transition: transform 0.1s ease, background 0.15s ease;
}
.choice-btn:not(:has(.choice-letter))::before {
  counter-increment: u4choice; content: counter(u4choice, upper-alpha);
  flex: 0 0 34px; height: 34px; border-radius: 50%; background: var(--brand-tint); color: var(--brand);
  font-family: var(--font-display); font-weight: 800; display: inline-flex; align-items: center; justify-content: center;
}
.choice-letter {
  flex: 0 0 34px; height: 34px; border-radius: 50%; background: var(--brand-tint); color: var(--brand);
  font-family: var(--font-display); font-weight: 800; display: inline-flex; align-items: center; justify-content: center;
}
.choice-btn.correct .choice-letter { background: var(--ok); color: var(--on-ok); }
.choice-btn.wrong .choice-letter { background: var(--bad); color: var(--on-bad); }
.choice-btn:hover:not([disabled]) { background: var(--brand-tint); border-color: var(--brand); }
.choice-btn:active:not([disabled]) { transform: translateY(2px); border-bottom-width: 2px; margin-bottom: 2px; }
.choice-btn.selected { background: var(--brand-tint); border-color: var(--brand); font-weight: 700; }
.choice-btn.correct { background: var(--ok-tint); border-color: var(--ok); color: var(--ok-ink); font-weight: 800; }
.choice-btn.correct:not(:has(.choice-letter))::before { content: "\2713"; background: var(--ok); color: var(--on-ok); }
.choice-btn.wrong { background: var(--bad-tint); border-color: var(--bad); color: var(--bad-ink); }
.choice-btn.wrong:not(:has(.choice-letter))::before { content: "\2715"; background: var(--bad); color: var(--on-bad); }

/* ---- Shared model pieces ----------------------------------------------- */
.benchmark-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; width: 100%; margin-top: 12px; }
.bm-tile { background: var(--surface-soft); border: 2px solid var(--border); border-radius: var(--radius-sm); padding: 8px; text-align: center; }
.bm-tile span { display: block; font-size: 0.78rem; font-weight: 800; color: var(--ink-light); }
.bm-tile strong { font-family: var(--font-display); font-size: 1.2rem; color: var(--brand); }

.trio-container { display: flex; flex-wrap: wrap; align-items: stretch; justify-content: center; gap: 10px; margin: 14px 0; }
.trio-pill {
  flex: 1 1 140px; min-width: 130px; border-radius: var(--radius-md); padding: 12px 14px; text-align: center;
  background: var(--surface-soft); border: 3px solid var(--border-strong);
}
.trio-label { font-size: 0.82rem; font-weight: 800; color: var(--ink-muted); }
.trio-val { font-family: var(--font-display); font-size: 1.6rem; font-weight: 800; color: var(--brand); }
.trio-arrow { align-self: center; font-size: 1.4rem; font-weight: 800; color: var(--ink-light); }

.complete-banner {
  background: var(--ok); color: var(--on-ok); padding: 18px 22px; border-radius: var(--radius-lg);
  font-family: var(--font-display); font-size: 1.15rem; font-weight: 800; margin-bottom: 22px; text-align: center;
  box-shadow: var(--shadow-card);
}
.complete-banner[hidden] { display: none; }

.v2-badge {
  display: inline-block; background: var(--sun-tint); color: var(--sun-ink); border: 2px solid var(--sun);
  border-radius: var(--radius-pill); padding: 2px 12px; font-size: 0.78rem; font-weight: 800;
}

.app-footer { background: var(--brand-dark); color: #FFFFFF; padding: 18px 24px; font-size: 0.88rem; margin-top: auto; }
.app-footer a, .app-footer a:visited { color: #F7DE9A; text-decoration: underline; }
.footer-wrap { max-width: 1120px; margin: 0 auto; display: flex; flex-wrap: wrap; justify-content: space-between; gap: 10px; }

/* ==========================================================================
   Engagement layer (assets/u4-studio.js)
   ========================================================================== */
#u4-fx { position: fixed; inset: 0; pointer-events: none; z-index: 400; overflow: hidden; }
.u4-spark {
  position: absolute; left: 0; top: 0; font-family: var(--font-display); font-weight: 800; font-size: 1.3rem;
  padding: 2px 8px; border-radius: var(--radius-pill); background: var(--surface); border: 2px solid currentColor;
  will-change: transform, opacity; white-space: nowrap;
}
.u4-spark.k-frac { color: var(--frac); }
.u4-spark.k-dec { color: var(--dec); }
.u4-spark.k-pct { color: var(--pct); }
.u4-spark.k-star { color: var(--sun-ink); background: var(--sun); border-color: var(--sun-deep); }
/* Night worlds lighten --sun-ink for text on dark tints; on the bright --sun pill it fell to 1.1-1.6:1.
   The chips sit on the dark --surface, where the daylight fraction/decimal/percent hues are too dim. */
/* ---- night: the whole Unit 4 palette, for a student who picks Dark or whose device is in dark mode (Challenge pages
   start here). Lessons 2-3 and 5-9 get the same treatment per world from assets/studio-worlds.css, which is more specific
   and wins there. tools/studio-contrast.test.mjs holds every text pair below to WCAG AA. ---- */
body[data-theme="dark"] {
  --paper: hsl(210, 30%, 7%); --well: hsl(210, 28%, 10%); --surface: hsl(210, 28%, 13%); --surface-soft: hsl(210, 26%, 17%);
  --brand: hsl(210, 55%, 74%); --brand-dark: hsl(210, 28%, 10%); --brand-tint: hsl(210, 24%, 21%); --brand-line: hsl(210, 20%, 36%);
  --sun: hsl(41, 85%, 62%); --sun-deep: hsl(41, 88%, 68%); --sun-tint: hsl(41, 45%, 17%); --sun-ink: hsl(41, 90%, 82%);
  --frac: #8DB4E2; --frac-tint: hsl(212, 35%, 20%); --frac-line: hsl(212, 30%, 38%);
  --dec: #6FCFC4; --dec-tint: hsl(174, 30%, 17%); --dec-line: hsl(174, 28%, 36%);
  --pct: #F2A77A; --pct-tint: hsl(22, 40%, 19%); --pct-line: hsl(22, 35%, 38%);
  --ok: hsl(152, 55%, 55%); --ok-tint: hsl(152, 38%, 15%); --ok-line: hsl(152, 40%, 34%);
  --bad: hsl(5, 80%, 66%); --bad-tint: hsl(5, 40%, 17%); --bad-line: hsl(5, 45%, 40%);
  --warn: hsl(42, 95%, 72%); --warn-tint: hsl(42, 45%, 15%); --warn-line: hsl(42, 60%, 38%);
  --ink: hsl(210, 6%, 93%); --ink-muted: hsl(210, 5%, 80%); --ink-light: hsl(210, 4%, 70%);
  --border: hsl(210, 22%, 27%); --border-strong: hsl(210, 16%, 58%);
  --on-brand: hsl(210, 28%, 10%); --on-sun: hsl(210, 28%, 10%); --on-ok: hsl(152, 60%, 8%); --on-warn: hsl(42, 80%, 8%); --on-bad: hsl(5, 60%, 8%);
  --on-frac: hsl(210, 28%, 10%); --on-dec: hsl(210, 28%, 10%); --on-pct: hsl(210, 28%, 10%);
  --ok-ink: hsl(148, 70%, 88%); --bad-ink: hsl(5, 100%, 90%); --warn-ink: hsl(42, 100%, 82%);
  --shadow-card: 0 1px 2px rgba(0, 0, 0, 0.4), 0 6px 18px rgba(0, 0, 0, 0.35);
  color-scheme: dark;
}
body[data-theme="dark"] .u4-spark.k-star { color: hsl(22, 80%, 12%); }
body[data-theme="dark"] .u4-spark.k-frac { color: #8DB4E2; }
body[data-theme="dark"] .u4-spark.k-dec { color: #6FCFC4; }
body[data-theme="dark"] .u4-spark.k-pct { color: #F2A77A; }
body.fx-off #u4-fx { display: none; }

.u4-toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 350;
  max-width: min(560px, calc(100vw - 32px)); background: var(--brand-dark); color: #FFFFFF;
  border: 3px solid var(--sun); border-radius: var(--radius-lg); padding: 14px 18px;
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap; box-shadow: var(--shadow-pop);
  animation: u4-rise 0.3s ease-out;
}
.u4-toast[hidden] { display: none; }
.u4-toast strong { font-family: var(--font-display); font-size: 1.05rem; }
.u4-toast .btn { min-height: 44px; }
.u4-toast-close { background: transparent; border: none; color: #D3DCE8; font-size: 1.4rem; line-height: 1; cursor: pointer; padding: 4px 8px; min-height: 44px; }

dialog.u4-dialog {
  border: 4px solid var(--brand-dark); border-radius: 24px; padding: 0; background: var(--surface); color: var(--ink);
  width: min(720px, calc(100vw - 24px)); max-height: calc(100vh - 24px); overflow: auto; box-shadow: var(--shadow-pop);
}
dialog.u4-dialog::backdrop { background: rgba(15, 26, 38, 0.62); }
.u4-dialog-head {
  background: var(--brand-dark); color: #FFFFFF; padding: 18px 24px; display: flex; align-items: center;
  justify-content: space-between; gap: 12px;
}
.u4-dialog-head h2 { margin: 0; font-size: 1.4rem; }
.u4-dialog-head .es-text, .u4-toast .es-text { color: #D3DCE8; }
.u4-dialog-body { padding: 22px 24px 26px; }
.u4-close { background: transparent; border: 2px solid rgba(255, 255, 255, 0.5); color: #FFFFFF; border-radius: var(--radius-md); min-height: 44px; padding: 6px 14px; font-weight: 800; font-family: inherit; cursor: pointer; }
.u4-close:hover { background: rgba(255, 255, 255, 0.16); }

/* My Stars */
.u4-rank { display: flex; align-items: center; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.u4-rank-badge {
  width: 84px; height: 84px; border-radius: 50%; background: var(--sun); color: var(--brand-dark); border: 4px solid var(--sun-deep);
  display: flex; flex-direction: column; align-items: center; justify-content: center; font-family: var(--font-display); font-weight: 800; line-height: 1.05;
}
.u4-rank-badge b { font-size: 1.9rem; }
.u4-rank-badge span { font-size: 0.78rem; }
.u4-rank-text h3 { margin: 0; font-size: 1.35rem; color: var(--brand); }
.u4-rank-text p { margin: 2px 0 0; color: var(--ink-muted); }
.u4-meter { height: 14px; background: var(--well); border-radius: var(--radius-pill); overflow: hidden; margin-top: 8px; min-width: 220px; }
.u4-meter > i { display: block; height: 100%; background: var(--sun-deep); border-radius: var(--radius-pill); transition: width 0.4s ease; }
.u4-sections { list-style: none; margin: 0 0 18px; padding: 0; display: grid; gap: 10px; }
.u4-sections li { display: grid; grid-template-columns: minmax(130px, 220px) 1fr auto; align-items: center; gap: 12px; font-weight: 700; }
.u4-sections .u4-meter { margin: 0; min-width: 0; }
.u4-sections .u4-meter > i { background: var(--ok); }
.u4-sections li.is-done .u4-meter > i { background: var(--ok); }
.u4-setting { display: flex; align-items: center; gap: 10px; padding-top: 14px; border-top: 2px solid var(--border); font-weight: 700; }
.u4-setting input { width: 24px; height: 24px; accent-color: var(--brand); }

/* Bonus board */
.u4-board-top { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 10px; align-items: center; margin-bottom: 14px; font-weight: 800; }
.u4-board-top .chip { background: var(--brand-tint); color: var(--brand); border-radius: var(--radius-pill); padding: 4px 14px; border: 2px solid var(--brand-line); }
.u4-board-help { margin: 0 0 14px; color: var(--ink-muted); }
.u4-board { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.u4-tile {
  appearance: none; font-family: var(--font-display); font-weight: 800; font-size: 1.25rem; color: var(--ink);
  background: var(--surface); border: 3px solid var(--border-strong); border-bottom-width: var(--lift);
  border-radius: var(--radius-md); min-height: 72px; padding: 8px 6px; cursor: pointer; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 2px; line-height: 1.15; text-align: center;
  transition: transform 0.1s ease, background 0.15s ease;
}
.u4-tile small { font-family: var(--font-sans); font-size: 0.72rem; font-weight: 700; color: var(--ink-muted); }
.u4-tile:hover:not([disabled]) { border-color: var(--brand); background: var(--brand-tint); }
.u4-tile[aria-pressed="true"] { background: var(--sun-tint); border-color: var(--sun-deep); box-shadow: 0 0 0 3px var(--sun); transform: translateY(-2px); }
.u4-tile.k-frac { border-left: 10px solid var(--frac); }
.u4-tile.k-dec { border-left: 10px solid var(--dec); }
.u4-tile.k-pct { border-left: 10px solid var(--pct); }
.u4-tile.k-a { border-left: 10px solid var(--frac); }
.u4-tile.k-b { border-left: 10px solid var(--dec); }
.u4-tile.k-c { border-left: 10px solid var(--pct); }
.u4-tile[disabled] { cursor: default; }
.u4-tile.is-matched { color: var(--on-frac); border-color: transparent; border-left-width: 3px; }
.u4-tile.is-matched small { color: inherit; }
.u4-tile.is-matched.set-1 { color: var(--on-dec); }
.u4-tile.is-matched.set-2 { color: var(--on-pct); }
.u4-tile.is-matched.set-0 { background: var(--frac); }
.u4-tile.is-matched.set-1 { background: var(--dec); }
.u4-tile.is-matched.set-2 { background: var(--pct); }
.u4-tile.is-matched.set-3 { background: var(--brand); }
.u4-tile.shake { animation: u4-shake 0.35s ease; }
.u4-board-note { min-height: 1.6em; margin: 14px 0 0; font-weight: 700; color: var(--ink-muted); }
.u4-board-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 14px; }
.u4-result { text-align: center; padding: 8px 0; }
.u4-result h3 { font-size: 1.5rem; color: var(--brand); margin: 6px 0; }
.u4-stars-row { font-size: 2rem; letter-spacing: 6px; color: var(--sun-deep); }

@keyframes u4-pop { from { transform: scale(0.97); opacity: 0.4; } to { transform: scale(1); opacity: 1; } }
@keyframes u4-rise { from { transform: translate(-50%, 24px); opacity: 0; } to { transform: translate(-50%, 0); opacity: 1; } }
@keyframes u4-bump { 0%, 100% { transform: scale(1); } 40% { transform: scale(1.14); } }
@keyframes u4-shake { 0%, 100% { transform: translateX(0); } 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }

/* ---- Responsive -------------------------------------------------------- */
@media (max-width: 840px) {
  html { font-size: 16px; }
  .two-col-stage { grid-template-columns: 1fr; }
  .app-header { padding: 12px 14px; }
  .brand-title { font-size: 1.2rem; }
  .tab-list { padding: 8px 6px 6px; gap: 0; }
  .tab-list::before, .tab-list::after { top: 22px; }
  .tab-btn { flex-direction: column; gap: 3px; text-align: center; }
  .tab-text { background: transparent; padding: 0; }
  .tab-badge { width: 30px; height: 30px; font-size: 0.9rem; }
  .tab-name { font-size: 0.72rem; }
  .stage-card { padding: 18px; }
  .topic-strip { padding: 16px; }
  .topic-strip h2 { font-size: 1.3rem; }
  .u4-sections li { grid-template-columns: 1fr auto; }
  .u4-sections .u4-meter { grid-column: 1 / -1; order: 3; }
}
@media (max-width: 600px) {
  .data-table, .matrix-table { display: block; overflow-x: auto; }
}
@media (max-width: 520px) {
  .u4-board { grid-template-columns: repeat(2, 1fr); }
  .header-actions { gap: 6px; }
  .header-actions .top-btn { padding: 4px 10px; min-height: 40px; font-size: 0.82rem; }
  .score-pill { padding: 4px 12px; font-size: 0.82rem; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
  #u4-fx { display: none; }
}
@media (forced-colors: active) {
  .tab-badge, .step-dot, .btn, .choice-btn, .u4-tile { border: 2px solid ButtonText; }
  .tab-btn[aria-selected="true"] .tab-badge, .step-dot.active, .u4-tile[aria-pressed="true"] { outline: 4px solid Highlight; }
}

/* ---- Quiz results -------------------------------------------------------- */
.results-card {
  margin-top: 18px; background: var(--surface); border: 3px solid var(--brand); border-radius: var(--radius-lg);
  padding: 22px; box-shadow: var(--shadow-card);
}
.results-card[hidden] { display: none; }
.results-card h3 { margin: 0 0 6px 0; font-size: 1.4rem; color: var(--brand); }
.results-card p { margin: 0 0 12px 0; }
.review-list { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0 14px; }

/* ---- Data tables (4-4 and 4-5 challenge) ------------------------------- */
.data-table, .matrix-table {
  width: 100%; border-collapse: collapse; margin: 14px 0; background: var(--surface); border-radius: var(--radius-sm);
  overflow: hidden; border: 2px solid var(--border);
}
.data-table th, .data-table td, .matrix-table th, .matrix-table td {
  padding: 10px 14px; text-align: left; border-bottom: 1px solid var(--border); font-size: 0.98rem;
}
.data-table th, .matrix-table th { background: var(--brand); color: var(--on-brand); font-weight: 800; }
.data-table tr:last-child td, .matrix-table tr:last-child td { border-bottom: none; }

/* ---- Challenge versions: darker header so students can tell at a glance - */
body.theme-challenge .app-header { background: #232B33; }
body.theme-challenge .brand-icon { background: #D9822B; box-shadow: 0 4px 0 #A8601A; color: #232B33; }

/* ---- Equivalent-ratio machine (4-1 and 4-2): part/whole, scale arrows, per-100 ------------ */
.scaling-visual-card { background: var(--surface-soft); border: 1.5px solid var(--border); border-radius: var(--radius-md); padding: 22px; margin: 18px 0; }
.ratio-machine { display: flex; align-items: center; justify-content: center; gap: 18px; flex-wrap: wrap; margin-bottom: 20px; }
.big-fraction { background: var(--surface); border: 2px solid var(--brand); border-radius: var(--radius-md); padding: 12px 20px; display: flex; flex-direction: column; align-items: center; min-width: 110px; box-shadow: var(--shadow-card); }
.big-fraction .num, .big-fraction .den { font-family: var(--font-display); font-size: 1.65rem; font-weight: 800; color: var(--brand); line-height: 1.2; }
.big-fraction .f-bar { width: 100%; height: 3px; background: var(--brand); margin: 6px 0; border-radius: 2px; }
.big-fraction .role-tag { font-size: 0.72rem; font-weight: 700; color: var(--ink-light); }
.arrow-stack { display: flex; flex-direction: column; gap: 22px; align-items: center; }
.scale-arrow-pill { background: var(--sun-tint); border: 2px solid var(--sun-deep); color: var(--warn); padding: 5px 14px; border-radius: var(--radius-pill); font-family: var(--font-display); font-weight: 800; font-size: 1rem; display: flex; align-items: center; gap: 6px; }
.ratio-setup { margin: 4px 0 14px; font-family: var(--font-display); font-size: 1.35rem; font-weight: 800; color: var(--brand); }
.ratio-setup .q { color: var(--pct); }

/* ---- Guided steps (shared by the 4-1 and 4-2 models) ------------------- */
.guided-steps-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 14px; margin: 18px 0; }
.step-box {
  background: var(--surface); border: 2px solid var(--brand-line); border-radius: var(--radius-md);
  padding: 16px; display: flex; flex-direction: column; justify-content: space-between; gap: 12px;
}
.step-box.step-2 { border-color: var(--ok-line); }
.step-box.step-3 { border-color: var(--sun); }
.optional-step {
  margin: 4px 0 0; padding: 14px 18px; background: var(--surface-soft); border: 2px dashed var(--border-strong);
  border-radius: var(--radius-md);
}
.optional-step .step-box-desc { margin: 2px 0 8px; }
.step-box.examine-step { grid-column: 1 / -1; }
.examine .choice-stack { margin: 8px 0 0; gap: 8px; }
.step-box-label { font-size: 0.92rem; font-weight: 800; color: var(--brand); }
.step-box-desc { font-size: 0.92rem; color: var(--ink-muted); margin: 4px 0 0 0; }
.step-box-eq { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; font-family: var(--font-display); font-weight: 800; font-size: 1.15rem; color: var(--brand); }

/* ---- Save / Resume widget: the site-wide launcher is a vivid #1d4ed8 that reads purple beside this
   palette. Retint it here only; the shared widget stays as it is on every other page. ---- */
body #nsr-root { --nsr-accent: var(--brand); --nsr-accent-fg: #FFFFFF; }
/* Night worlds (body[data-theme="dark"]) lift --brand to a light tint, so white text on it fell to
   1.5-3:1. Use the page's own paper as the on-accent ink: near-black in every night world, and #fff
   under high-contrast comfort, where --brand is forced back to navy. */
body[data-theme="dark"] #nsr-root { --nsr-accent-fg: var(--on-brand, var(--paper)); }

/* ---- Fixed-chrome rail: the Save/Resume and Math Workbench launchers and both toasts sit in a right rail
   that page content never enters, so nothing fixed ever covers an answer choice, a Read button or a link.
   Wide screens keep the labelled launchers; 600-899px uses icon-only launchers in a slimmer rail; phones keep
   the shared widget's own layout. --studio-rail is the single source for the rail width. ---- */
@media (min-width: 600px) {
  body { --studio-rail: 172px; }
  .main-container { padding-right: max(20px, calc(var(--studio-rail) - max(0px, (100% - 1120px) / 2))); }
  .tab-list { max-width: min(1120px, calc(100% - 2 * var(--studio-rail))); }
  .footer-wrap { padding-right: max(0px, calc(var(--studio-rail) - 24px - max(0px, (100% - 1120px) / 2))); }
  body #nsr-root { right: 8px; bottom: 12px; width: calc(var(--studio-rail) - 16px); align-items: stretch; }
  body #nsr-root #nsr-workbench, body #nsr-root #nsr-launcher { justify-content: center; text-align: center; }
  .u4-toast {
    left: auto; right: 8px; bottom: 124px; transform: none; width: calc(var(--studio-rail) - 16px); max-width: none;
    flex-direction: column; align-items: stretch; gap: 8px; padding: 12px 14px; border-radius: var(--radius-md);
    animation-name: u4-rise-rail;
  }
  .u4-toast > span { padding-right: 22px; }
  .u4-toast .u4-toast-close { position: absolute; top: 2px; right: 2px; }
  body #comfort-toast { left: auto; right: 8px; top: 40vh; bottom: auto; width: calc(var(--studio-rail) - 16px); max-width: none; transform: translateY(16px); }
  body #comfort-toast.show { transform: none; }
}
@media (min-width: 600px) and (max-width: 899px) {
  body { --studio-rail: 64px; }
  body #nsr-root #nsr-workbench, body #nsr-root #nsr-launcher { min-width: 44px; min-height: 44px; padding: 4px; border-radius: 50%; }
  body #nsr-root .nsr-launcher-label, body #nsr-root .nsr-workbench-label {
    position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
  }
  body #nsr-root .nsr-dot { display: none; }
  /* toasts are too wide for a 64px rail: they sit above the launchers and end short of the rail's column */
  .u4-toast, body #comfort-toast { width: auto; left: 12px; right: 72px; }
  .u4-toast { bottom: 12px; flex-direction: row; align-items: center; }
  body #comfort-toast { top: auto; bottom: 80px; }
}
@keyframes u4-rise-rail { from { transform: translateY(24px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---- Generated practice studios (assets/studio/*.js) -------------------- */
.item-head { display: flex; justify-content: space-between; margin-bottom: 8px; }
.item-count { font-size: 0.84rem; font-weight: 800; color: var(--brand); }
.item-q { font-size: 1.15rem; font-weight: 700; color: var(--brand); margin: 0 0 8px; line-height: 1.45; }
.item-sub { font-size: 0.92rem; color: var(--ink-muted); margin: 4px 0 10px; }
.err-steps { list-style: none; margin: 0 0 8px; padding: 0; display: grid; gap: 8px; }
.err-step { width: 100%; text-align: left; }
.table-wrap { overflow-x: auto; margin: 8px 0 12px; }
.table-wrap .num-input { width: 100%; min-width: 5.5em; }
.write-label { display: block; font-weight: 800; color: var(--brand); margin: 8px 0 4px; }
.write-box { width: 100%; padding: 12px 14px; font: inherit; line-height: 1.5; border: 2px solid var(--border-strong); border-radius: var(--radius-md); background: var(--surface-soft); color: var(--ink); resize: vertical; }
.write-box:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 4px var(--sun); }
.hint-area { margin: 10px 0 4px; }
.hint-list { margin: 0 0 8px; padding-left: 1.3em; color: var(--ink); line-height: 1.5; }
.item-actions { display: flex; gap: 8px; flex-wrap: wrap; margin: 8px 0; }
.more-in-lesson { margin: 16px 0 0; color: var(--ink-muted); }
.studio-list { margin: 8px 0 0; padding-left: 1.2em; line-height: 1.9; }

/* ---- Story cards (assets/u4-studio-story.js): facts, one question, labelled boxes, hint ladder ---- */
.story-label { display: block; font-size: 0.78rem; font-weight: 800; letter-spacing: 0.04em; text-transform: uppercase; color: var(--ink-muted); margin: 0 0 4px; }
.story-facts { margin: 0 0 14px; padding: 12px 16px; background: var(--surface-soft); border-left: 4px solid var(--brand); border-radius: var(--radius-sm); }
.story-facts.story-challenge { border-left-color: var(--sun-deep); }
.story-facts ul { margin: 0; padding-left: 1.2em; font-size: 1.08rem; font-weight: 600; color: var(--brand); line-height: 1.5; }
.story-facts li + li { margin-top: 2px; }
.story-ratio-table { margin: 14px 0; padding: 12px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); overflow-x: auto; }
.story-ratio-table table { width: 100%; min-width: 360px; border-collapse: collapse; text-align: center; }
.story-ratio-table th, .story-ratio-table td { padding: 8px 10px; border-bottom: 1px solid var(--border); }
.story-ratio-table thead { background: var(--brand); color: var(--on-brand); }
.story-ratio-table tbody th { color: var(--brand); }
.story-ratio-table tbody tr:last-child th, .story-ratio-table tbody tr:last-child td { border-bottom: 0; }
.story-fields { display: grid; gap: 12px; margin-top: 14px; }
.story-field { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; }
.story-field label { font-weight: 700; color: var(--brand); min-width: 11em; }
.story-field .num-input { width: 130px; }
.story-unit { font-weight: 800; color: var(--brand); }
.story-hints { margin-top: 8px; }
.story-hint { margin: 6px 0; padding: 8px 12px; background: var(--brand-tint); border: 1px solid var(--brand-line); border-radius: var(--radius-sm); color: var(--brand); }
.story-hint strong { margin-right: 8px; }

/* ---- Print: every section on paper, no interface chrome ---------------- */
@media print {
  .app-header, .tab-bar, .stepper-bar, .app-footer, .top-btn, .btn, .speak-btn, .u4-toast, #u4-fx, dialog.u4-dialog { display: none !important; }
  body { background: #FFFFFF; }
  .main-container { padding: 0; }
  .tab-panel { display: block !important; margin-bottom: 20px; break-inside: avoid; }
}

.print-only { display: none; }
@media print { .print-only { display: block !important; } }

/* ---- My thinking (assets/u4-studio-thinking.js) ---------------------------------------------------------------------- */
.u4-think-list { list-style: none; margin: 14px 0 0; padding: 0; display: grid; gap: 12px; }
.u4-think-list li { background: var(--surface-soft); border: 2px solid var(--border); border-radius: var(--radius-md); padding: 12px 16px; }
.u4-think-list h3 { margin: 0 0 2px; font-size: 1.05rem; color: var(--brand); }
.u4-think-list p { margin: 4px 0 0; }
.u4-think-meta { font-size: 0.88rem; color: var(--ink-muted); font-weight: 700; }
#u4-think-btn[data-count]:not([data-count=""])::after { content: " (" attr(data-count) ")"; }

/* ---- guess resistance on the hand-built pages (assets/u4-studio-guess.js): the fresh problem a guessed answer owes ------------ */
.feedback-box[data-owed] .next-btn { display: none; }
.u4-twin { margin: 14px 0 0; }
.u4-twin-q { margin: 0 0 6px; color: var(--brand); }
.u4-twin-stem { margin: 0 0 10px; font-size: 1.1rem; font-weight: 700; color: var(--ink); }
.u4-twin-row { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.u4-twin-row .num-input { width: 9em; }
.u4-twin [data-twin-fb] { margin-top: 10px; }
