.mental-rotation-page {
  --bg: var(--paper);
  --surface: var(--card);
  --surface-2: var(--paper-soft);
  --text: var(--ink);
  --accent: var(--plum);
  --accent-contrast: #ffffff;
  --danger: var(--red);
  --danger-bg: color-mix(in srgb, var(--red) 10%, var(--card));
  --focus: var(--plum);
  --radius: 1.2rem;
  overflow-wrap: anywhere;
}

button, input, select { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .48; }
button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible, summary:focus-visible, [tabindex="-1"]:focus-visible { outline: 3px solid var(--focus); outline-offset: 3px; }
[tabindex="-1"]:focus { outline: none; }

.shell { width: min(900px, calc(100% - 2rem)); margin: 0 auto; padding-block: 116px 5rem; }
.flow > * + * { margin-top: 1.15rem; }
.eyebrow { margin: 0 0 8px; text-transform: uppercase; letter-spacing: .1em; font-size: .76rem; font-weight: 760; color: var(--muted); }
h1 { font-size: clamp(2rem, 5vw, 3.5rem); line-height: 1.03; letter-spacing: -.035em; margin: 0 0 18px; }
h2 { margin-top: 2.2rem; line-height: 1.15; }
.lede { font-size: clamp(1.08rem, 2.5vw, 1.3rem); max-width: 68ch; }
.muted { color: var(--muted); font-size: .93rem; }

.notice { padding: 18px 20px; border: 1px solid var(--line); background: var(--surface-2); border-radius: var(--radius); }
.notice p:last-child { margin-bottom: 0; }
.notice.danger { border-color: color-mix(in srgb, var(--danger), transparent 55%); background: var(--danger-bg); color: var(--text); }
.steps { padding-left: 1.3rem; }
.steps li + li { margin-top: .55rem; }

.actions { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-top: 28px; }
.actions.compact { margin-top: 14px; }
.primary, .secondary, .button-link { min-height: 46px; padding: 11px 22px; border-radius: 999px; border: 1.5px solid #51305a; font-weight: 700; letter-spacing: .02em; text-decoration: none; display: inline-flex; align-items: center; justify-content: center; box-shadow: 0 4px 14px rgba(49, 49, 49, .2); }
.primary { background: linear-gradient(135deg, #51305a, #9e7d71); color: #ffffff; border-color: #51305a; }
.secondary, .button-link { background: var(--control); color: var(--control-ink); border-color: #111; }
.text-button { min-height: 44px; padding: 10px 15px; border: 1px solid transparent; border-radius: 999px; background: transparent; color: var(--muted); font-weight: 700; box-shadow: none; }
.practice-choice-start { justify-content: center; }
.practice-stage.is-awaiting-start > * { visibility: hidden; }

.mode-grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 18px; }
.mode-card { display: flex; flex-direction: column; padding: 22px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); }
.mode-card h2 { margin: 0 0 6px; }
.mode-card ul { padding-left: 1.25rem; color: var(--muted); }
.mode-card .primary { margin-top: auto; align-self: flex-start; }

form { display: grid; gap: 20px; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; }
legend { font-weight: 750; padding: 0 6px; }
label { font-weight: 650; }
.choice { display: flex; align-items: flex-start; gap: 10px; font-weight: 500; padding: 8px 0; }
.choice input { margin-top: .25em; }
input[type="text"], input[type="number"], select { width: 100%; padding: 12px 13px; color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: 10px; }
.rating-grid { display: grid; gap: 18px; }
.rating-grid label { display: grid; gap: 8px; padding: 16px 18px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.rating-grid select { max-width: 220px; }
.scale { display: flex; justify-content: space-between; color: var(--muted); font-size: .76rem; font-weight: 500; }
.consent { background: var(--surface-2); border-radius: var(--radius); padding: 14px; }
.form-error { color: var(--danger); font-weight: 700; }

.cue-card { display: grid; gap: 8px; justify-items: center; padding: 28px; border: 1px solid var(--line); background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); text-align: center; }
.cue-card strong { font-size: 1.35rem; }
.countdown { display: grid; place-items: center; width: 68px; height: 68px; border-radius: 50%; background: var(--surface-2); font-size: 2rem; font-variant-numeric: tabular-nums; }

.task-head { display: flex; justify-content: space-between; gap: 20px; align-items: flex-start; }
.timer { text-align: right; font-variant-numeric: tabular-nums; font-weight: 800; font-size: 1.6rem; }
.timer small { display: block; font-size: .72rem; font-weight: 550; color: var(--muted); }
.progress-track { height: 7px; border-radius: 999px; background: var(--surface-2); overflow: hidden; margin: 16px 0 24px; }
.progress-track span { display: block; width: 0; height: 100%; background: var(--accent); transition: width .2s linear; }
.task-instruction { color: var(--muted); max-width: 68ch; }

.rotation-stage { margin: 28px auto 18px; display: grid; grid-template-columns: 1fr auto 1fr; gap: 24px; align-items: center; max-width: 650px; }
.rotation-stage figure { margin: 0; min-width: 0; text-align: center; transition: opacity .12s ease; }
.rotation-stage figcaption { font-weight: 700; color: var(--muted); margin-bottom: 9px; }
.rotation-stage figure.visually-withheld { visibility: hidden; }
.shape { width: min(240px, 100%); aspect-ratio: 1 / 1; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); }
.shape rect.cell, .next-piece rect.cell { fill: var(--text); rx: 5; }
.shape.target rect.cell { fill: transparent; stroke: var(--text); stroke-width: 5; rx: 5; }
.turn-arrow { font-size: 2rem; color: var(--muted); }
.trial-prompt { text-align: center; font-weight: 750; }
.answer-grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 10px; max-width: 680px; margin: 0 auto; }
.answer-grid button { min-height: 54px; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); color: var(--text); font-weight: 720; }
.answer-grid button:hover:not(:disabled) { border-color: var(--accent); }
kbd { margin-left: 7px; padding: 1px 5px; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 5px; color: var(--muted); font-size: .75rem; }
.status { text-align: center; min-height: 1.5em; }
.task-stats { display: flex; justify-content: center; gap: 22px; flex-wrap: wrap; margin: 22px 0 0; color: var(--muted); font-size: .9rem; }
.task-stats strong { color: var(--text); }

.game-layout { display: grid; grid-template-columns: minmax(260px, 360px) minmax(180px, 1fr); gap: 24px; align-items: start; max-width: 760px; margin: 26px auto 18px; }
.game-canvas { display: block; width: min(100%, 300px); height: auto; margin: 0 auto; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); }
.game-side { display: grid; gap: 10px; align-content: start; }
.next-piece { width: 150px; max-width: 100%; aspect-ratio: 1 / 1; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.game-prompt { max-width: 30ch; }
.game-controls { display: grid; grid-template-columns: repeat(4, minmax(0,1fr)); gap: 9px; max-width: 620px; margin: 14px auto 0; }
.game-controls button { min-height: 50px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); color: var(--text); font-weight: 750; }
.game-controls .primary { background: var(--accent); color: var(--accent-contrast); border-color: var(--accent); }
.control-hint { text-align: center; }
.practice-game-layout { margin-top: 18px; }
.practice-stage { margin-top: 18px; }

.pause-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.58); z-index: 20; display: grid; place-items: center; padding: 20px; }
.pause-overlay[hidden] { display: none; }
.pause-card { width: min(420px, 100%); padding: 24px; background: var(--surface); color: var(--text); border-radius: var(--radius); box-shadow: var(--shadow); text-align: center; }

.result-grid { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 12px; }
.metric { display: grid; gap: 4px; padding: 18px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.metric span { color: var(--muted); font-size: .86rem; }
.metric strong { font-size: 1.35rem; }
details { border: 1px solid var(--line); background: var(--surface); border-radius: var(--radius); padding: 14px 16px; }
summary { cursor: pointer; font-weight: 750; }
.ratings-table { width: 100%; border-collapse: collapse; margin: 18px 0 4px; }
.ratings-table th, .ratings-table td { padding: 10px; border-bottom: 1px solid var(--line); text-align: right; }
.ratings-table th:first-child { text-align: left; }
.ratings-table thead th { color: var(--muted); font-size: .82rem; }

.static-trials { display: grid; gap: 18px; counter-reset: trial; }
.static-trial { border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; background: var(--surface); counter-increment: trial; break-inside: avoid; }
.static-trial h3::before { content: "Problem " counter(trial) ": "; }
.static-shapes { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; align-items: center; }
.static-shape { width: min(180px, 100%); aspect-ratio: 1/1; border: 1px solid var(--line); border-radius: 10px; }
.static-options { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 8px; margin-top: 12px; }
.static-option { padding: 10px; border: 1px solid var(--line); border-radius: 8px; }

.diary-grid { display: grid; gap: 12px; }
.diary-day { display: grid; grid-template-columns: minmax(110px, .7fr) 1fr; gap: 12px; align-items: center; padding: 14px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }

@media (max-width: 720px) {
  .shell { padding-top: 98px; }
  .mode-grid { grid-template-columns: 1fr; }
  .task-head { align-items: end; }
  .rotation-stage { gap: 10px; }
  .turn-arrow { font-size: 1.45rem; }
  .answer-grid { grid-template-columns: 1fr; }
  .game-layout { grid-template-columns: 1fr; }
  .game-side { justify-items: center; text-align: center; }
  .game-controls { grid-template-columns: repeat(2, minmax(0,1fr)); }
  .result-grid { grid-template-columns: 1fr; }
  .static-shapes { grid-template-columns: 1fr; justify-items: center; }
  .static-options { grid-template-columns: 1fr; }
  .diary-day { grid-template-columns: 1fr; }
  .paper-diary th, .paper-diary td { padding: 8px 6px; }
}

@media (min-width: 721px) {
  #intro > .eyebrow, #intro > h1, #intro > .lede { text-align: center; margin-inline: auto; }
}

@media print {
  .site-header, .site-footer, .actions, .task-instruction, .theme-toggle { display: none !important; }
  .shell { width: 100%; padding: 0; }
  body { background: white; color: black; }
  details { border: 0; padding: 0; }
  details > * { display: block !important; }
  .static-trial { break-inside: avoid; }
}

/* Revision 2 follow-up windows */
.followup-phase { padding: 18px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.followup-phase h3 { margin: 0; font-size: 1.3rem; }
.phase-head { display: flex; gap: 16px; align-items: center; justify-content: space-between; flex-wrap: wrap; margin-bottom: 14px; }
.phase-head .eyebrow { margin-bottom: 4px; }
.date-chip { padding: 6px 10px; border: 1px solid var(--line); border-radius: 999px; color: var(--muted); font-size: .84rem; font-variant-numeric: tabular-nums; }
.diary-day.is-future { opacity: .62; background: var(--surface-2); }
.paper-diary { width: 100%; table-layout: fixed; border-collapse: collapse; margin: 16px 0 28px; }
.paper-diary th, .paper-diary td { border: 1px solid var(--line); padding: 12px; text-align: left; min-height: 42px; overflow-wrap: anywhere; }
.paper-diary thead th { background: var(--surface-2); }
@media print {
  .paper-diary th, .paper-diary td { border-color: #888; height: 44px; }
}

/* Revision 2 practice competency display */
.practice-requirements { margin: 18px auto 0; max-width: 620px; padding: 14px 16px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface-2); }
.practice-requirements ul { list-style: none; padding: 0; margin: 10px 0 0; display: grid; gap: 6px; }
.practice-requirements li { display: flex; gap: 9px; align-items: baseline; color: var(--muted); }
.practice-requirements li[data-done="true"] { color: var(--text); }
.practice-requirements li[data-done="true"] span { color: var(--accent); font-weight: 900; }

/* Revision 3: dialog, research-tool, motion and high-contrast refinements */
.dialog-actions { display: grid; gap: 10px; margin-top: 18px; }
.dialog-actions .primary, .dialog-actions .secondary { width: 100%; }
.stop-card { text-align: left; }
.stop-card .dialog-actions { text-align: center; }
.detail-tools { margin-top: 18px; }
.detail-tools .actions { margin-bottom: 2px; }

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* Script-free pages follow the operating-system theme. */
@media (prefers-color-scheme: dark) {
  html:not([data-theme]) {
    color-scheme: dark;
    --paper: #1f2a37;
    --paper-soft: #2a3645;
    --card: #253241;
    --control: #ffffff;
    --control-ink: #111111;
    --ink: #eaeaea;
    --muted: #aab5c0;
    --plum: #cbb2dc;
    --plum-dark: #eadff2;
    --clay: #c69f91;
    --line: #4a5a6a;
    --green: #86c8aa;
    --red: #efadbc;
    --shadow: 0 14px 30px rgba(0, 0, 0, 0.25);
  }
}

@media (forced-colors: active) {
  .primary, .secondary, .button-link, .text-button,
  .answer-grid button, .game-controls button,
  .notice, details, .metric, .mode-card, .game-canvas, .shape, .next-piece {
    border: 1px solid CanvasText;
  }
  .primary, .game-controls .primary { background: Highlight; color: HighlightText; }
  .danger, .notice.danger { border-color: Mark; }
  .progress-track span { background: Highlight; }
}


/* Static Rotation Choice fallback (moved out of inline CSS for CSP compatibility) */
.memory-trial > summary { list-style-position: outside; }
.memory-trial[open] > summary .source-memory { display:none; }
.source-memory, .target-memory { margin-top:12px; text-align:center; }
.answer-reveal { margin-top:12px; background:var(--surface-2); }

/* Revision 4: mobile task ergonomics and high-density Canvas rendering support */
.game-canvas {
  aspect-ratio: 5 / 9;
  max-height: min(62svh, 540px);
  object-fit: contain;
}
.practice-game-layout .game-canvas { aspect-ratio: 5 / 8; max-height: min(58svh, 480px); }
.game-controls button {
  min-height: 56px;
  padding: 10px 12px;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}
.game-controls button span { pointer-events: none; }
.answer-grid button { touch-action: manipulation; }

@media (max-width: 720px) {
  .shell { width: min(100% - 24px, 900px); }
  .rotation-stage {
    grid-template-columns: minmax(0, 1fr);
    width: min(330px, 100%);
    min-height: min(330px, calc(100vw - 48px));
  }
  .rotation-stage figure { grid-area: 1 / 1; }
  .rotation-stage .turn-arrow { display: none; }
  .shape { width: min(300px, 100%); }

  .game-layout {
    grid-template-columns: minmax(0, 1fr) minmax(88px, 112px);
    gap: 12px;
    align-items: start;
    max-width: 460px;
    margin-top: 18px;
  }
  .game-canvas {
    width: 100%;
    max-height: 56svh;
  }
  .practice-game-layout .game-canvas { max-height: 52svh; }
  .game-side { justify-items: stretch; text-align: left; gap: 8px; }
  .game-side > .muted { font-size: .78rem; }
  .next-piece { width: 100%; }
  .game-prompt { font-size: .84rem; line-height: 1.4; margin: 0; }
  .game-side .status { font-size: .82rem; text-align: left; }
  .game-controls { max-width: 460px; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 7px; }
  .game-controls button { min-height: 58px; padding: 8px 5px; line-height: 1.15; }
  .game-controls button span { display: block; font-size: .78rem; }
  .control-hint { max-width: 46ch; margin-left: auto; margin-right: auto; }
}

@media (max-width: 360px) {
  .game-layout { grid-template-columns: minmax(0, 1fr) 82px; gap: 8px; }
  .game-prompt { font-size: .78rem; }
  .game-controls { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .game-controls button span { display: inline; font-size: inherit; }
}
