:root {
  --navy: #063f86;
  --ink: #10294a;
  --muted: #637287;
  --line: #d8e2ec;
  --page: #f6f8fb;
  --blue: #0b67b2;
  --blue-soft: #eaf5ff;
  --teal: #08aaa5;
  --teal-soft: #e8f8f5;
  --orange: #f58220;
  --orange-soft: #fff2e7;
  --violet: #7b42b4;
  --violet-soft: #f5edff;
}

* { box-sizing: border-box; }
html { min-height: 100%; overflow-x: hidden; -webkit-text-size-adjust: 100%; }
body {
  min-height: 100vh;
  margin: 0;
  overflow-x: hidden;
  color: var(--ink);
  background: var(--page);
  font-family: "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
}
button, input { font: inherit; }
[hidden] { display: none !important; }
.skip-link { position: fixed; left: 12px; top: -80px; z-index: 20; padding: 10px 14px; border-radius: 8px; color: #fff; background: var(--navy); }
.skip-link:focus { top: 12px; }
.page-shell {
  --stage-min-height: clamp(300px, calc(100svh - 455px), 520px);
  width: min(1480px, calc(100% - 28px));
  min-height: 100vh;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
}
.page-shell > main { min-height: 0; flex: 1; display: flex; flex-direction: column; }

.site-header { min-height: 96px; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 22px; padding: 12px 4px 8px; }
.brand-nav { justify-self: start; display: grid; justify-items: start; gap: 3px; }
.brand { width: 148px; }
.brand img { display: block; width: 100%; height: auto; }
.back-link { color: var(--navy); font-size: .78rem; font-weight: 800; text-decoration: none; }
.back-link:hover { text-decoration: underline; text-underline-offset: 3px; }
.title-lockup { text-align: center; }
.title-lockup p { margin: 0 0 2px; color: var(--teal); font-size: .72rem; font-weight: 850; letter-spacing: .12em; text-transform: uppercase; }
.title-lockup h1 { margin: 0; color: var(--navy); font-family: Georgia, "Times New Roman", serif; font-size: clamp(1.55rem, 2.6vw, 2.45rem); line-height: 1.05; }
.header-actions { justify-self: end; display: flex; align-items: center; gap: 8px; }

.button, .icon-button {
  border: 0;
  border-radius: 12px;
  font-weight: 780;
  cursor: pointer;
  text-decoration: none;
  transition: transform .15s ease, box-shadow .15s ease, opacity .15s ease;
}
.button { min-height: 44px; display: inline-flex; align-items: center; justify-content: center; padding: 9px 16px; }
.button:hover, .icon-button:hover { transform: translateY(-1px); }
.button:disabled { opacity: .38; cursor: default; transform: none; }
.button-primary { color: #fff; background: var(--navy); box-shadow: 0 7px 16px rgba(6, 63, 134, .18); }
.button-secondary { color: var(--navy); background: #eef4fb; }
.button-light, .button-quiet { color: var(--navy); background: #fff; border: 1px solid var(--line); }
.icon-button { width: 44px; height: 44px; display: grid; place-items: center; padding: 0; color: var(--navy); background: #fff; border: 1px solid var(--line); }
.icon-button svg { width: 23px; height: 23px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.button:focus-visible, .icon-button:focus-visible, input:focus-visible, .back-link:focus-visible { outline: 3px solid color-mix(in srgb, var(--blue) 32%, transparent); outline-offset: 3px; }

.controls-card, .board-card { border: 1px solid var(--line); border-radius: 22px; background: #fff; box-shadow: 0 9px 24px rgba(19, 49, 83, .07); }
.controls-card { padding: 15px 18px; }
#subtraction-form { display: grid; grid-template-columns: minmax(180px, 1fr) auto minmax(180px, 1fr) minmax(165px, auto) auto; align-items: end; gap: 12px; }
.number-field { display: grid; gap: 6px; color: var(--muted); font-size: .73rem; font-weight: 850; letter-spacing: .08em; text-transform: uppercase; }
.number-field input { width: 100%; height: 46px; border: 2px solid var(--role); border-radius: 11px; padding: 6px 12px; color: var(--ink); background: #fff; font-size: 1.2rem; font-weight: 800; font-variant-numeric: tabular-nums; outline: none; }
.number-field input:focus { box-shadow: 0 0 0 4px color-mix(in srgb, var(--role) 18%, transparent); }
.number-field input[aria-invalid="true"] { border-color: #b83242; box-shadow: 0 0 0 3px rgba(184, 50, 66, .12); }
.role-first { --role: var(--blue); }
.role-second { --role: var(--teal); }
.subtraction-form-symbol { align-self: center; padding-top: 16px; color: var(--navy); font-size: 1.5rem; font-weight: 900; }
.rank-control { min-height: 46px; display: grid; grid-template-columns: auto 1fr; grid-template-rows: 1fr 1fr; column-gap: 9px; align-items: center; padding: 5px 10px; border: 1px solid var(--line); border-radius: 12px; color: var(--ink); background: #fff; cursor: pointer; }
.rank-control input { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
.rank-switch { grid-row: 1 / 3; width: 34px; height: 20px; position: relative; border-radius: 999px; background: #dce4ed; transition: background .18s ease; }
.rank-switch::after { content: ""; width: 14px; height: 14px; position: absolute; top: 3px; left: 3px; border-radius: 50%; background: #fff; box-shadow: 0 1px 4px rgba(16, 41, 74, .25); transition: transform .18s ease; }
.rank-control input:checked + .rank-switch { background: var(--teal); }
.rank-control input:checked + .rank-switch::after { transform: translateX(14px); }
.rank-control input:focus-visible + .rank-switch { outline: 3px solid color-mix(in srgb, var(--blue) 28%, transparent); outline-offset: 2px; }
.rank-copy { display: contents; }
.rank-copy strong { align-self: end; font-size: .76rem; line-height: 1.05; white-space: nowrap; }
.rank-copy small { align-self: start; color: var(--muted); font-size: .65rem; font-weight: 800; letter-spacing: .1em; }
.form-error { margin: 9px 0 0; color: #a22c38; font-weight: 700; }

.projection-recap { min-height: 48px; display: flex; align-items: center; justify-content: space-between; gap: 14px; margin-top: 8px; padding: 6px 9px 6px 15px; border: 1px solid var(--line); border-radius: 15px; color: var(--navy); background: #fff; box-shadow: 0 7px 18px rgba(19, 49, 83, .06); }
.projection-recap strong { overflow: hidden; font-size: 1.02rem; font-variant-numeric: tabular-nums; text-overflow: ellipsis; white-space: nowrap; }
.projection-recap .button { min-height: 44px; flex: none; padding: 5px 12px; font-size: .78rem; }

.workspace { min-height: 0; flex: 1; display: flex; margin-top: 15px; }
.board-card { min-width: 0; flex: 1; display: grid; grid-template-rows: minmax(124px, auto) minmax(var(--stage-min-height), 1fr) auto; padding: 15px; }
.instruction { height: 100%; min-height: 0; display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 13px 17px; border-radius: 15px; background: #f3f7fc; }
.instruction-copy { min-width: 0; }
.instruction p { margin: 0 0 3px; color: var(--teal); font-size: .75rem; font-weight: 900; letter-spacing: .1em; text-transform: uppercase; }
.instruction h2 { margin: 0; color: var(--ink); font-size: clamp(1rem, 1.7vw, 1.28rem); line-height: 1.28; }
.instruction .step-detail { margin: 5px 0 0; color: var(--muted); font-size: clamp(.8rem, 1.2vw, .94rem); font-weight: 650; letter-spacing: 0; line-height: 1.3; text-transform: none; }
.instruction .exchange-memo { width: max-content; max-width: 100%; margin: 10px 0 0; padding: 7px 10px; border: 1px solid #d9c4ef; border-radius: 10px; color: var(--violet); background: var(--violet-soft); font-size: clamp(.78rem, 1.1vw, .9rem); font-weight: 850; letter-spacing: 0; line-height: 1.15; text-transform: none; }
.instruction > span { flex: none; color: var(--muted); font-size: .82rem; font-weight: 800; }

.subtraction-stage { min-width: 0; min-height: var(--stage-min-height); display: grid; grid-template-rows: minmax(0, 1fr) auto; align-items: center; padding: 14px 22px 12px; overflow: hidden; }
.subtraction-scroll { min-width: 0; width: 100%; max-width: 100%; overflow-x: auto; overflow-y: hidden; padding: 10px 2px 14px; scrollbar-color: #b5c5d7 transparent; scrollbar-width: thin; }
#posed-subtraction { width: max-content; min-width: 0; margin: 0 auto; }
.posed-subtraction { display: grid; grid-template-rows: calc(var(--row-height) * .68) var(--row-height) var(--row-height) 14px var(--row-height); align-items: center; width: max-content; padding: 4px 8px 8px 0; font-family: "DejaVu Sans Mono", Consolas, monospace; font-size: var(--digit-size); font-weight: 800; font-variant-numeric: tabular-nums; }
.posed-subtraction.has-integers-only { grid-template-columns: var(--sign-width) repeat(var(--integer-columns), var(--column-width)); }
.posed-subtraction.has-decimals { grid-template-columns: var(--sign-width) repeat(var(--integer-columns), var(--column-width)) var(--separator-width) repeat(var(--decimal-columns), var(--column-width)); }
.digit-cell, .comma-cell, .subtraction-sign, .exchange-cell { min-width: 0; height: 100%; display: grid; place-items: center; line-height: 1; }
.digit-cell { position: relative; z-index: 1; border-radius: 9px; }
.term-one-cell { color: var(--blue); }
.term-two-cell { color: var(--teal); }
.result-cell, .result-comma { color: var(--orange); }
.term-one-cell.is-active { background: var(--blue-soft); }
.term-two-cell.is-active { background: var(--teal-soft); }
.result-cell.is-active { background: var(--orange-soft); }
.term-one-cell.is-exchanged { opacity: .42; text-decoration: line-through; text-decoration-color: var(--violet); text-decoration-thickness: .08em; }
.comma-cell { z-index: 1; align-items: end; padding-bottom: .12em; }
.subtraction-sign { z-index: 1; align-items: end; padding-bottom: .13em; color: var(--navy); }
.subtraction-rule { z-index: 1; width: 100%; align-self: center; border-top: 4px solid var(--navy); }
.decimal-separator-band { z-index: 0; width: 100%; height: 100%; border-radius: 8px; background: #f3f5f8; }
.exchange-cell { z-index: 2; align-self: end; height: auto; min-height: 1.55em; padding: 3px 5px; border-radius: 8px; color: var(--violet); background: var(--violet-soft); font-size: .48em; text-decoration: none; }
.exchange-cell.is-fresh { box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--violet) 38%, transparent), 0 3px 9px rgba(83, 43, 126, .12); }
.exchange-cell.is-settled { opacity: .82; }
.rank-marker { position: absolute; top: 3px; left: 50%; transform: translateX(-50%); color: #718198; font-family: "Segoe UI", system-ui, sans-serif; font-size: clamp(.63rem, .18em, .78rem); font-weight: 900; letter-spacing: 0; line-height: 1; white-space: nowrap; text-decoration: none; }
.has-rank-guides .term-one-cell { padding-top: .17em; }

.vocabulary-strip { width: min(100%, 850px); display: grid; grid-template-columns: minmax(0, 1.18fr) auto minmax(0, 1.18fr) auto minmax(0, .85fr) auto; align-items: center; gap: 8px; margin: 3px auto 0; padding: 10px 14px; border: 1px solid var(--line); border-radius: 13px; background: #f8fafc; text-align: center; }
.vocabulary-strip strong { color: var(--navy); font-size: 1.15rem; }
.vocabulary-role { min-width: 0; font-size: clamp(.72rem, 1.1vw, .88rem); font-weight: 850; line-height: 1.15; }
.first-role { color: var(--blue); }
.second-role { color: var(--teal); }
.difference-role { color: var(--orange); }
.exchange-role { padding: 5px 8px; border: 1px solid #d9c4ef; border-radius: 999px; color: var(--violet); background: var(--violet-soft); }

.step-controls { display: grid; grid-template-columns: auto minmax(80px, 1fr) auto auto; align-items: center; gap: 10px; padding-top: 12px; border-top: 1px solid #e7edf4; }
.progress { height: 6px; overflow: hidden; border-radius: 999px; background: #e9eef5; }
.progress span { display: block; width: 0; height: 100%; border-radius: inherit; background: linear-gradient(90deg, var(--teal), var(--blue)); transition: width .2s ease; }
.site-footer { margin-top: auto; display: flex; flex-wrap: wrap; justify-content: center; gap: 7px; padding: 19px 10px 24px; color: #718096; font-size: .8rem; text-align: center; }
.site-footer a, .site-footer button { padding: 0; border: 0; color: #526d8d; background: transparent; font: inherit; text-decoration: underline; text-underline-offset: 3px; cursor: pointer; }

body.is-projection { height: 100svh; overflow: hidden; }
body.is-projection .page-shell { --stage-min-height: 0px; width: min(1700px, calc(100% - 18px)); height: 100svh; min-height: 0; overflow: hidden; }
body.is-projection .site-header { min-height: 66px; padding-block: 5px 3px; }
body.is-projection .brand { width: 118px; }
body.is-projection .title-lockup p { display: none; }
body.is-projection .title-lockup h1 { font-size: clamp(1.4rem, 2.2vw, 2rem); }
body.is-projection .workspace { min-height: 0; margin-top: 8px; }
body.is-projection .board-card { min-height: 0; grid-template-rows: minmax(96px, auto) minmax(0, 1fr) auto; padding: 10px; }
body.is-projection .instruction { padding: 9px 13px; }
body.is-projection .instruction h2 { font-size: clamp(1rem, 1.5vw, 1.2rem); }
body.is-projection .instruction .step-detail { margin-top: 3px; font-size: clamp(.74rem, 1.05vw, .86rem); }
body.is-projection .instruction .exchange-memo { margin-top: 6px; padding-block: 5px; }
body.is-projection .subtraction-stage { min-height: 0; padding: 4px 16px 3px; }
body.is-projection .subtraction-scroll { padding-block: 2px 5px; }
body.is-projection .vocabulary-strip { padding-block: 7px; }
body.is-projection .step-controls { padding-top: 7px; }
body.is-projection .step-controls .button { min-height: 44px; padding-block: 6px; }
body.is-projection .site-footer { flex: none; gap: 5px; padding: 5px 8px 6px; font-size: .68rem; }

@media (min-width: 1280px) {
  .board-card { grid-template-columns: minmax(380px, .48fr) minmax(0, 1fr); grid-template-rows: minmax(var(--stage-min-height), 1fr) auto; column-gap: 18px; }
  .instruction { grid-column: 1; grid-row: 1 / 3; position: relative; align-items: flex-start; padding: 24px 58px 24px 24px; }
  .instruction p { font-size: .86rem; }
  .instruction > span { position: absolute; top: 23px; right: 20px; font-size: .95rem; }
  .instruction h2 { margin-top: 6px; font-size: clamp(1.42rem, 1.8vw, 2.08rem); line-height: 1.24; overflow-wrap: anywhere; }
  .instruction .step-detail { margin-top: 14px; font-size: clamp(1.05rem, 1.2vw, 1.24rem); line-height: 1.4; }
  .instruction .exchange-memo { margin-top: 20px; font-size: clamp(.98rem, 1.08vw, 1.12rem); }
  .subtraction-stage { grid-column: 2; grid-row: 1; }
  .step-controls { grid-column: 2; grid-row: 2; }

  body.is-projection .board-card { grid-template-columns: minmax(390px, .48fr) minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) auto; column-gap: 16px; }
  body.is-projection .instruction { padding: 23px 60px 23px 23px; }
  body.is-projection .instruction p { font-size: .9rem; }
  body.is-projection .instruction h2 { font-size: clamp(1.55rem, 1.9vw, 2.18rem); }
  body.is-projection .instruction .step-detail { margin-top: 14px; font-size: clamp(1.08rem, 1.22vw, 1.3rem); }
}

@media (max-width: 1080px) {
  #subtraction-form { grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr) minmax(165px, auto); }
  #subtraction-form > .button-primary { grid-column: 4; }
  .rank-control { grid-column: 1 / 4; grid-row: 2; justify-self: start; }
}

@media (max-width: 780px) {
  .page-shell { width: min(100% - 18px, 1480px); }
  .site-header { grid-template-columns: auto 1fr auto; gap: 10px; }
  .brand { width: 112px; }
  .back-link { font-size: .72rem; }
  .title-lockup p { display: none; }
  .title-lockup h1 { font-size: 1.35rem; }
  #subtraction-form { grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr); gap: 9px; }
  .rank-control { grid-column: 1 / 3; grid-row: auto; justify-self: stretch; }
  #subtraction-form > .button-primary { grid-column: 3; }
  .gabarit-link { display: none; }
  .board-card { grid-template-rows: minmax(162px, auto) minmax(320px, 1fr) auto; }
  .instruction { align-items: flex-start; }
  .subtraction-stage { min-height: 320px; padding-inline: 4px; }
  body.is-projection .board-card { grid-template-rows: minmax(118px, auto) minmax(0, 1fr) auto; }
  body.is-projection .subtraction-stage { min-height: 0; }
}

@media (max-width: 520px) {
  .page-shell { width: min(100% - 10px, 1480px); }
  .site-header { min-height: 88px; gap: 7px; }
  .brand { width: 101px; }
  .back-link { font-size: .68rem; }
  .title-lockup h1 { font-size: 1.12rem; }
  .controls-card { padding: 12px; }
  .number-field input { height: 44px; padding-inline: 9px; font-size: 1.02rem; }
  .subtraction-form-symbol { padding-top: 15px; font-size: 1.28rem; }
  #subtraction-form > .button-primary { grid-column: 1 / -1; }
  .rank-control { grid-column: 1 / -1; }
  .board-card { grid-template-rows: minmax(174px, auto) minmax(310px, 1fr) auto; padding-inline: 9px; }
  .instruction { padding: 12px; }
  .instruction h2 { font-size: .96rem; }
  .instruction .step-detail { font-size: .78rem; }
  .instruction .exchange-memo { margin-top: 9px; }
  .subtraction-stage { min-height: 310px; padding-inline: 0; }
  .vocabulary-strip { grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr) auto minmax(0, .85fr); gap: 4px; padding-inline: 7px; }
  .exchange-role { grid-column: 1 / -1; justify-self: center; margin-top: 3px; }
  .vocabulary-strip strong { font-size: .92rem; }
  .step-controls { grid-template-columns: 1fr 1fr; gap: 8px; }
  #previous { grid-column: 1; grid-row: 1; }
  #next { grid-column: 2; grid-row: 1; }
  #show-all { grid-column: 1 / -1; grid-row: 2; justify-self: center; }
  .progress { grid-column: 1 / -1; grid-row: 3; }
  .step-controls .button { padding-inline: 8px; font-size: .82rem; }
  body.is-projection .site-header { min-height: 62px; }
  body.is-projection .brand { width: 91px; }
  body.is-projection .back-link { display: none; }
  body.is-projection .board-card { grid-template-rows: minmax(112px, auto) minmax(0, 1fr) auto; }
  body.is-projection .site-footer { display: none; }
}

@media (max-width: 360px) {
  .site-header { grid-template-columns: 96px minmax(0, 1fr) 44px; }
  .brand { width: 94px; }
  .title-lockup h1 { font-size: .98rem; }
  .board-card { padding-inline: 7px; }
  .instruction { padding-inline: 10px; }
  .instruction > span { font-size: .74rem; }
  .vocabulary-role { font-size: .65rem; }
}

@media (min-width: 700px) and (max-height: 520px) {
  body.is-projection .site-header { min-height: 54px; padding-block: 2px; }
  body.is-projection .brand { width: 88px; }
  body.is-projection .back-link { display: none; }
  body.is-projection .title-lockup h1 { font-size: 1.25rem; }
  body.is-projection .projection-recap { min-height: 44px; margin-top: 3px; padding-block: 0; }
  body.is-projection .workspace { margin-top: 4px; }
  body.is-projection .board-card {
    grid-template-columns: minmax(250px, .56fr) minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) auto;
    column-gap: 9px;
    padding: 7px;
  }
  body.is-projection .instruction {
    grid-column: 1;
    grid-row: 1 / 3;
    position: relative;
    align-items: flex-start;
    padding: 12px 43px 10px 12px;
  }
  body.is-projection .instruction > span { position: absolute; top: 12px; right: 11px; }
  body.is-projection .instruction h2 { margin-top: 3px; font-size: 1.02rem; line-height: 1.2; }
  body.is-projection .instruction .step-detail { margin-top: 5px; font-size: .76rem; line-height: 1.22; }
  body.is-projection .instruction .exchange-memo { margin-top: 6px; padding: 4px 7px; font-size: .72rem; }
  body.is-projection .subtraction-stage { grid-column: 2; grid-row: 1; padding: 0 3px; }
  body.is-projection .step-controls { grid-column: 2; grid-row: 2; gap: 6px; padding-top: 4px; }
  body.is-projection .step-controls .button { min-height: 42px; padding: 4px 9px; font-size: .76rem; }
  body.is-projection .vocabulary-strip { gap: 4px; padding: 4px 6px; }
  body.is-projection .site-footer { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation: none !important; transition: none !important; }
}
