:root {
  --bg: #f8f4ef;
  --panel: #ffffff;
  --text: #2f251f;
  --muted: #6f6258;
  --line: #ddcec0;
  --brand: #c30000;
  --brand-soft: #f9dddd;
  --accent: #7a6f60;
  --success: #2f7d4f;
  --success-soft: #e3f3e8;
  --danger: #b33632;
  --blue: #1d63d6;
  --blue-soft: #e4edfb;
  --carry: #7b3fa0;
  --carry-soft: #f1e8f7;
  --hl: #fff1b8;
  --cell: 56px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Avenir Next", "Segoe UI", sans-serif;
  color: var(--text);
  background: radial-gradient(circle at top right, #fff8f3, var(--bg) 50%);
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

h1, h2, p {
  margin: 0;
}

button {
  font: inherit;
  cursor: pointer;
}

.hidden {
  display: none !important;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 20px;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, #fff, #fcf8f3);
  box-shadow: 0 4px 20px rgba(90, 52, 38, 0.08);
}

.brand-block {
  display: flex;
  align-items: center;
  gap: 14px;
}

.master-logo {
  width: 42px;
  height: 42px;
  object-fit: contain;
}

.topbar h1 {
  font-size: 1.3rem;
  color: var(--brand);
}

.topbar p {
  color: var(--muted);
  margin-top: 2px;
  font-size: 0.85rem;
}

/* ---------- Level picker ---------- */

.level-title {
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 6px;
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
}

.level-btn {
  background: #fff;
  border: 1px solid var(--line);
  color: var(--text);
  padding: 6px 4px;
  min-height: 44px;
  border-radius: 12px;
  font-weight: 700;
  font-size: 0.85rem;
  line-height: 1.15;
  text-align: center;
}

.level-btn.active {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
}

/* ---------- Layout ---------- */

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 16px;
  width: 100%;
  max-width: 1280px;
  margin: 0 auto;
  padding: 16px 20px;
}

.board-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 16px 12px;
  box-shadow: 0 6px 18px rgba(90, 52, 38, 0.06);
}

/* The board's digit size is picked in script.js (fitBoard) to fill this area. */
.board-area {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.panel-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 16px;
  box-shadow: 0 6px 18px rgba(90, 52, 38, 0.06);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* ---------- The problem board ---------- */

.board {
  display: grid;
  grid-template-columns: repeat(var(--cols, 4), var(--cell));
  justify-content: center;
  row-gap: var(--row-gap, 4px);
}

.cell {
  width: var(--cell);
  height: var(--cell);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: calc(var(--cell) * 0.62);
  line-height: 1;
  font-variant-numeric: tabular-nums;
  position: relative;
}

.cell.sign {
  color: var(--muted);
}

.cell.factor {
  border-radius: 10px;
  transition: background 0.2s ease;
}

.cell.factor.hl {
  background: var(--hl);
  box-shadow: inset 0 0 0 2px #e6c84f;
}

.cell.factor.hl-bottom {
  background: var(--blue-soft);
  box-shadow: inset 0 0 0 2px var(--blue);
}

/* answer boxes */
.ans-box {
  width: calc(var(--cell) - 6px);
  height: calc(var(--cell) - 6px);
  border: 2px dashed var(--line);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text);
  background: #fffdfa;
}

.ans-box.filled {
  border: 2px solid transparent;
  background: transparent;
}

.ans-box.placeholder-zero {
  color: var(--muted);
}

.ans-box.active {
  border: 3px solid var(--blue);
  background: var(--blue-soft);
  animation: pulse 1.4s ease-in-out infinite;
}

.ans-box.wrong {
  border-color: var(--danger) !important;
  background: #fbe9e8 !important;
  color: var(--danger);
  animation: shake 0.35s ease;
}

.ans-box.hl-col {
  background: var(--hl);
}

/* regroup (carry) boxes */
.cell.carry-cell {
  height: calc(var(--cell) * 0.72);
  align-items: flex-end;
}

.carry-box {
  width: calc(var(--cell) * 0.62);
  height: calc(var(--cell) * 0.62);
  border: 2px solid var(--carry);
  border-radius: 8px;
  background: var(--carry-soft);
  color: var(--carry);
  font-size: calc(var(--cell) * 0.42);
  display: flex;
  align-items: center;
  justify-content: center;
  animation: pop-in 0.35s ease;
}

.carry-box.active {
  border-width: 3px;
  animation: pop-in 0.35s ease, pulse 1.4s ease-in-out 0.35s infinite;
}

.carry-box.wrong {
  border-color: var(--danger);
  background: #fbe9e8;
  color: var(--danger);
  animation: shake 0.35s ease;
}

.carry-box.in-use {
  box-shadow: 0 0 0 3px var(--hl), 0 0 0 5px #e6c84f;
}

.carry-box.done {
  opacity: 0.35;
  text-decoration: line-through;
  text-decoration-thickness: 2px;
  animation: none;
}

.rule {
  grid-column: 1 / -1;
  height: 0;
  border-top: 3px solid var(--text);
  margin: 4px 0;
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(29, 99, 214, 0.35); }
  50% { box-shadow: 0 0 0 6px rgba(29, 99, 214, 0); }
}

@keyframes pop-in {
  0% { transform: scale(0.2); opacity: 0; }
  70% { transform: scale(1.15); opacity: 1; }
  100% { transform: scale(1); }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}

.think-line {
  min-height: 2.8em;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  text-align: center;
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text);
  max-width: 34ch;
  line-height: 1.4;
}

.think-line .tag {
  display: inline-block;
  padding: 1px 10px;
  border-radius: 999px;
  font-size: 0.85rem;
  margin-right: 6px;
  vertical-align: 2px;
  background: var(--blue);
  color: #fff;
}

.think-line .tag.regroup {
  background: var(--carry);
}

.think-line .tag.add {
  background: var(--success);
}

.feedback {
  min-height: 2.6rem;
  font-weight: 700;
  text-align: center;
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 1.05rem;
  line-height: 1.5;
  width: 100%;
  max-width: 440px;
}

.feedback.correct {
  color: var(--success);
  background: var(--success-soft);
  animation: feedback-pop 0.5s ease;
}

.feedback.incorrect {
  color: var(--danger);
  background: #fbe9e8;
}

@keyframes feedback-pop {
  0% { transform: scale(0.7); opacity: 0; }
  60% { transform: scale(1.08); opacity: 1; }
  100% { transform: scale(1); }
}

/* ---------- Keypad ---------- */

.keypad {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
}

.key-btn {
  min-height: 54px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: #fffdfa;
  font-size: 1.6rem;
  font-weight: 800;
  color: var(--text);
  box-shadow: 0 3px 0 var(--line);
  touch-action: manipulation;
}

.key-btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 var(--line);
}

.keypad.locked .key-btn {
  opacity: 0.4;
  pointer-events: none;
}

.practice-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: center;
}

.primary {
  border: none;
  border-radius: 999px;
  padding: 14px 24px;
  min-height: 52px;
  font-weight: 800;
  font-size: 1.1rem;
  color: #fff;
  background: var(--brand);
  width: 100%;
}

.primary.success {
  background: var(--success);
}

.text-link-btn {
  background: none;
  border: none;
  color: var(--muted);
  text-decoration: underline;
  font-size: 0.9rem;
  padding: 6px;
}

.score-line {
  text-align: center;
  color: var(--muted);
  font-size: 0.9rem;
}

.skal-tip {
  text-align: center;
  color: var(--muted);
  font-size: 0.8rem;
}

.skal-tip strong {
  color: var(--brand);
}

.site-footer {
  text-align: center;
  padding: 6px 20px 10px;
  color: var(--muted);
  font-size: 0.75rem;
}

.site-footer a {
  color: var(--brand);
}

/* Big enough screens (laptops, tablets, smart boards): everything fits on one
   screen. The board fills the left side; the panel scrolls on its own only if
   it ever runs out of room. */
@media (min-width: 760px) and (min-height: 480px) {
  body {
    height: 100vh;
    height: 100dvh;
  }
  .layout {
    flex: 1;
    min-height: 0;
    /* The panel narrows on upright tablets so the board keeps more room. */
    grid-template-columns: minmax(0, 1fr) clamp(280px, 30vw, 360px);
    grid-template-rows: minmax(0, 1fr);
  }
  .board-panel {
    min-height: 0;
  }
  .board-area {
    flex: 1;
    min-height: 0;
  }
  .side-panel {
    min-height: 0;
    overflow-y: auto;
  }
}

/* Phones (and very short screens): the board is stacked above the panel, so
   keep the number pad right under the tabs where thumbs (and eyes) are, and
   don't hold empty space for a hint that isn't showing. */
@media (max-width: 759px), (max-height: 479px) {
  #practiceKeypadSlot,
  #teachKeypadSlot {
    order: -1;
  }
  .think-line {
    font-size: 1.05rem;
    line-height: 1.35;
  }
  .feedback:not(:empty) {
    font-size: 0.95rem;
  }
}

@media (max-width: 600px) {
  .topbar {
    padding: 8px 16px;
  }
  .topbar p {
    display: none;
  }
  .layout {
    padding: 12px 16px 16px;
    gap: 12px;
  }
  .key-btn {
    min-height: 48px;
  }
}

/* ---------- Teach Me ---------- */

.mute-btn {
  flex-shrink: 0;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--panel);
  font-size: 1.3rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.mute-btn:hover {
  border-color: var(--brand);
}

.mode-tabs {
  display: flex;
  gap: 8px;
  border-bottom: 1px solid var(--line);
}

.mode-tab {
  flex: 1;
  background: none;
  border: none;
  padding: 10px 12px;
  min-height: 48px;
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--muted);
  border-bottom: 3px solid transparent;
}

.mode-tab.active {
  color: var(--brand);
  border-bottom-color: var(--brand);
}

.teach-dots {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.teach-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: none;
  background: var(--line);
  padding: 0;
}

.teach-dot.active {
  background: var(--brand);
  transform: scale(1.25);
}

.teach-dot.done {
  background: var(--success);
}

.teach-caption {
  text-align: center;
  font-size: 1.05rem;
  font-weight: 600;
  line-height: 1.5;
  color: var(--text);
}

.teach-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.speak-btn {
  flex: 1;
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 12px 16px;
  min-height: 52px;
  border-radius: 999px;
  font-weight: 600;
}

.speak-btn:hover {
  border-color: var(--brand);
  color: var(--brand);
}

.ghost {
  background: none;
  border: 1px solid var(--line);
  color: var(--text);
  padding: 12px 16px;
  min-height: 52px;
  border-radius: 999px;
  font-weight: 700;
}

.ghost:disabled {
  opacity: 0.35;
  cursor: default;
}

.demo-math {
  display: inline-block;
  padding: 4px 16px;
  border-radius: 999px;
  background: var(--hl);
  font-size: 1.15rem;
  line-height: 1.3;
  font-weight: 800;
  animation: feedback-pop 0.5s ease;
}

.strategy-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 4px;
  width: calc(100% - 40px);
  max-width: 1240px;
  margin: 12px auto 0;
  padding: 4px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 22px;
  box-shadow: 0 4px 12px rgba(90, 52, 38, 0.06);
}

.strategy-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  font-weight: 700;
  padding: 8px 16px;
  min-height: 40px;
  border-radius: 999px;
  white-space: nowrap;
}

.strategy-btn.active {
  background: var(--brand);
  color: #fff;
}

@media (max-width: 600px) {
  .strategy-row {
    width: calc(100% - 32px);
    margin-top: 8px;
    flex-wrap: nowrap;
    overflow-x: auto;
    justify-content: flex-start;
  }
  .strategy-btn {
    padding: 6px 12px;
    min-height: 36px;
    font-size: 0.9rem;
  }
}

/* The step line and a hint / "You did it!" share one fixed-height spot: the
   hint takes the step line's place, so the board never shrinks or jumps. */
.board-panel:has(.feedback:not(:empty)) .think-line {
  display: none;
}

.feedback:empty {
  display: none;
}

.think-line,
.feedback:not(:empty) {
  height: 4rem;
  min-height: 0;
  flex-shrink: 0;
}

.feedback:not(:empty) {
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1.3;
  padding: 4px 12px;
}

/* Strategy buttons sit in the header bar on laptops, tablets and smart boards;
   on phones they drop to their own row under the title. */
.topbar {
  flex-wrap: wrap;
}

.topbar .strategy-row {
  width: auto;
  max-width: none;
  margin: 0;
  flex: 0 1 auto;
}

@media (max-width: 759px), (max-height: 479px) {
  .topbar .strategy-row {
    order: 3;
    flex-basis: 100%;
  }
}

@media (max-width: 759px), (max-height: 479px) {
  .think-line,
  .feedback:not(:empty) {
    height: 3.6rem;
  }
}

/* Short screens (projectors, small laptops): trim padding so tall problems fit. */
@media (min-width: 760px) and (max-height: 720px) {
  .board-panel {
    padding: 10px 12px;
    gap: 4px;
  }
  .think-line,
  .feedback:not(:empty) {
    height: 3.4rem;
  }
  .layout {
    padding-top: 10px;
    padding-bottom: 10px;
  }
}

/* Phones: slimmer header, strategy row, and tabs so the number pad stays
   above Safari's floating address bar. */
@media (max-width: 600px) {
  .topbar {
    padding: 6px 16px;
    gap: 6px 12px;
  }
  .master-logo {
    width: 34px;
    height: 34px;
  }
  .topbar h1 {
    font-size: 1.15rem;
  }
  .mute-btn {
    width: 36px;
    height: 36px;
    font-size: 1.1rem;
  }
  .topbar .strategy-row {
    padding: 3px;
  }
  .strategy-btn {
    min-height: 32px;
    padding: 4px 12px;
  }
  .mode-tab {
    min-height: 40px;
    padding: 6px 12px;
  }
  .layout {
    gap: 10px;
    padding-top: 10px;
  }
  .side-panel {
    gap: 10px;
  }
  .panel-card {
    padding: 12px;
  }
}

/* Phones: in a lesson, the number pad (on "Now you try" steps) and the
   Back / Play / Next buttons come right under the tabs, so they're always on
   screen; the lesson text follows below. */
@media (max-width: 759px), (max-height: 479px) {
  #teachKeypadSlot {
    order: -2;
  }
  .teach-nav {
    order: -1;
  }
}

/* ---------- Area model ---------- */
@media (min-width: 760px) and (max-height: 620px) {
  .topbar {
    padding: 4px 20px;
  }
  .master-logo {
    width: 32px;
    height: 32px;
  }
  .topbar h1 {
    font-size: 1.1rem;
  }
  .topbar p {
    display: none;
  }
  .mute-btn {
    width: 34px;
    height: 34px;
    font-size: 1rem;
  }
  .strategy-btn {
    min-height: 32px;
    padding: 4px 14px;
  }
  .layout {
    padding: 8px 16px 6px;
    gap: 12px;
  }
  .side-panel {
    gap: 6px;
  }
  .mode-tab {
    min-height: 36px;
    padding: 4px 10px;
  }
  .panel-card {
    padding: 10px;
    gap: 8px;
  }
  .level-title {
    font-size: 0.75rem;
    margin-bottom: 3px;
  }
  .level-grid {
    gap: 4px;
  }
  .level-btn {
    min-height: 32px;
    padding: 2px 4px;
    font-size: 0.72rem;
    line-height: 1.1;
  }
  .keypad {
    gap: 6px;
  }
  .key-btn {
    min-height: 40px;
    font-size: 1.3rem;
  }
  .practice-actions {
    gap: 2px;
  }
  .text-link-btn {
    padding: 2px;
    font-size: 0.8rem;
  }
  .score-line,
  .skal-tip {
    font-size: 0.72rem;
  }
  .teach-dots {
    gap: 5px;
  }
  .teach-dot {
    width: 10px;
    height: 10px;
  }
  .teach-caption {
    font-size: 0.95rem;
    line-height: 1.35;
  }
  .teach-nav .ghost,
  .teach-nav .speak-btn {
    min-height: 40px;
    padding: 6px 12px;
  }
  .primary {
    min-height: 40px;
    padding: 8px 16px;
    font-size: 1rem;
  }
  .site-footer {
    padding: 2px 20px 4px;
    font-size: 0.7rem;
  }
}

/* ---------- Division board ---------- */

.rem-row {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
}

.rem-btn {
  min-height: 38px;
}

.think-line .tag.mul {
  background: var(--success);
}

.think-line .tag.sub {
  background: var(--brand);
}

.think-line .tag.bring {
  background: var(--carry);
}

.think-line .tag.rem {
  background: #b06a00;
}

.cell.divisor,
.cell.dividend {
  border-radius: 10px;
  transition: background 0.2s ease;
}

/* The division bracket: a line over the dividend, curving down on its left.
   Drawn with pseudo-elements so a highlighted digit doesn't break the line. */
.cell.bracket-top::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  top: -2px;
  border-top: 3px solid var(--text);
}

.cell.bracket-left::before {
  content: '';
  position: absolute;
  left: calc(var(--cell) * -0.2);
  top: -2px;
  bottom: calc(var(--cell) * -0.06);
  width: calc(var(--cell) * 0.2);
  border-top: 3px solid var(--text);
  border-right: 3px solid var(--text);
  border-radius: 0 40% 100% 0 / 0 20% 40% 0;
}

.cell.hl,
.ans-box.hl {
  background: var(--hl);
  box-shadow: inset 0 0 0 2px #e6c84f;
  border-radius: 10px;
}

.cell.hl-blue {
  background: var(--blue-soft);
  box-shadow: inset 0 0 0 2px var(--blue);
}

/* The subtraction line under each product. */
.cell.sub-line {
  border-bottom: 3px solid var(--text);
}

/* Bring Down: a dashed arrow straight down from the dividend digit. */
.cell.bring-line::before {
  content: '';
  position: absolute;
  left: 50%;
  top: calc(var(--cell) * -0.1);
  bottom: 0;
  border-left: 2px dashed var(--muted);
  opacity: 0.45;
}

.cell.bring-end::after {
  content: '';
  position: absolute;
  left: calc(50% - var(--cell) * 0.1 + 1px);
  bottom: calc(var(--cell) * -0.06);
  border-left: calc(var(--cell) * 0.1) solid transparent;
  border-right: calc(var(--cell) * 0.1) solid transparent;
  border-top: calc(var(--cell) * 0.14) solid var(--muted);
  opacity: 0.45;
}

.cell.bring-active::before {
  border-left: 3px solid var(--carry);
  opacity: 1;
}

.cell.bring-active.bring-end::after {
  border-top-color: var(--carry);
  opacity: 1;
}

.cell.rem-r {
  color: var(--muted);
  font-size: calc(var(--cell) * 0.5);
}

/* Fraction remainders: numerator over the divisor. */
.board.has-frac .quotient-cell {
  height: calc(var(--cell) * 1.5);
  align-items: flex-end;
}

.board.has-frac .quotient-cell.frac-cell {
  align-items: center;
}

.cell.frac-cell {
  width: auto;
  justify-content: flex-start;
  padding-left: calc(var(--cell) * 0.1);
}

.frac {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.frac-part {
  display: flex;
  justify-content: center;
  padding: 0 3px;
}

.frac-den {
  border-top: 3px solid var(--text);
}

.ans-box.frac-box,
.frac-blank {
  width: calc(var(--cell) * 0.6);
  height: calc(var(--cell) * 0.6);
  font-size: calc(var(--cell) * 0.5);
  border-radius: 8px;
}

/* The opening question ("Can we divide 1 by 4?") is answered with Yes / No. */
.think-line .tag.start {
  background: var(--text);
}

.yes-no {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.yn-btn {
  min-height: 116px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: #fffdfa;
  font-size: 1.8rem;
  font-weight: 800;
  box-shadow: 0 3px 0 var(--line);
  touch-action: manipulation;
}

.yn-btn.yes {
  color: var(--success);
}

.yn-btn.no {
  color: var(--danger);
}

.yn-btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 var(--line);
}

.yn-btn.wrong {
  border-color: var(--danger);
  background: #fbe9e8;
  animation: shake 0.35s ease;
}

@media (min-width: 760px) and (max-height: 620px) {
  .yn-btn {
    min-height: 86px;
    font-size: 1.4rem;
  }
}

@media (max-width: 600px) {
  .yn-btn {
    min-height: 104px;
  }
}

/* ---------- Partial quotients ---------- */

/* The column of chunks ("how many groups") beside the work. */
.cell.chunk-cell {
  width: auto;
  justify-content: flex-end;
  padding-right: calc(var(--cell) * 0.15);
}

.cell.chunk-sep {
  border-left: 3px solid var(--text);
}

.chunk-val {
  padding: 0 calc(var(--cell) * 0.1);
  border-radius: 10px;
}

.chunk-val.hl {
  background: var(--hl);
  box-shadow: inset 0 0 0 2px #e6c84f;
}

.ans-box.chunk-entry {
  width: auto;
  min-width: calc(var(--cell) - 6px);
  padding: 0 calc(var(--cell) * 0.12);
}

/* The line over the total, under the chunks. */
.cell.chunk-cell.sum-line {
  border-top: 3px solid var(--text);
}

.total-digits {
  display: flex;
}

/* ⌫ and ✓ Done: only on the Partial Quotients tab, and only usable while a
   chunk is being chosen. */
.keypad .chunk-key {
  display: none;
}

.keypad.with-chunk-keys .chunk-key {
  display: block;
}

.keypad .key-enter {
  grid-column: span 4;
  background: var(--success);
  border-color: var(--success);
  color: #fff;
  box-shadow: 0 3px 0 #1f5a37;
  font-size: 1.3rem;
}

.keypad.with-chunk-keys:not(.choosing) .chunk-key {
  opacity: 0.35;
  pointer-events: none;
}

/* Yes / No take the same space as the taller keypad, so nothing jumps. */
.yes-no.with-chunk-keys .yn-btn {
  min-height: 178px;
}

@media (min-width: 760px) and (max-height: 620px) {
  .keypad .chunk-key {
    min-height: 34px;
    font-size: 1.1rem;
  }
  .yes-no.with-chunk-keys .yn-btn {
    min-height: 126px;
  }
}

@media (max-width: 600px) {
  .yes-no.with-chunk-keys .yn-btn {
    min-height: 160px;
  }
}

/* ---------- Regrouping in the subtraction steps (same look as the Adding and
   Subtracting Lab) ---------- */

.cell.regroup-cell {
  height: calc(var(--cell) * 0.62);
}

.carry-box {
  min-width: calc(var(--cell) * 0.56);
  height: calc(var(--cell) * 0.56);
  padding: 0 calc(var(--cell) * 0.05);
  white-space: nowrap;
  border: 2px solid var(--carry);
  border-radius: 8px;
  background: var(--carry-soft);
  color: var(--carry);
  font-size: calc(var(--cell) * 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  z-index: 1;
}

.carry-box.active {
  border-width: 3px;
  animation: pop-in 0.35s ease, pulse 1.4s ease-in-out 0.35s infinite;
}

.carry-box.wrong {
  border-color: var(--danger);
  background: #fbe9e8;
  color: var(--danger);
  animation: shake 0.35s ease;
}

.carry-box.in-use {
  box-shadow: 0 0 0 3px var(--hl), 0 0 0 5px #e6c84f;
}

.carry-box.done {
  opacity: 0.35;
  animation: none;
}

/* Crossed out with a diagonal slash once it has been regrouped. */
.cell.crossed {
  color: var(--muted);
}

.cell.crossed .ans-box {
  color: var(--muted);
}

.cell.crossed::after,
.carry-box.done::after {
  content: '';
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  pointer-events: none;
  background: linear-gradient(
    to bottom right,
    transparent calc(50% - 1.5px),
    var(--text) calc(50% - 1.5px),
    var(--text) calc(50% + 1.5px),
    transparent calc(50% + 1.5px)
  );
}

.cell.crossed::after {
  width: calc(var(--cell) * 0.55);
  height: calc(var(--cell) * 0.7);
}

.carry-box.done::after {
  width: 80%;
  height: 90%;
}

/* When regroup boxes sit above the dividend, the bracket rises above them. */
.cell.bracket-left-top::before,
.cell.bracket-mid::before,
.cell.bracket-left-low::before {
  content: '';
  position: absolute;
  left: calc(var(--cell) * -0.2);
  width: calc(var(--cell) * 0.2);
  border-right: 3px solid var(--text);
}

.cell.bracket-left-top::before {
  top: -2px;
  bottom: 0;
  border-top: 3px solid var(--text);
  border-radius: 0 40% 0 0 / 0 30% 0 0;
}

.cell.bracket-mid::before {
  top: 0;
  bottom: 0;
}

.cell.bracket-left-low::before {
  top: 0;
  bottom: calc(var(--cell) * -0.06);
  border-radius: 0 0 100% 0 / 0 0 40% 0;
}

/* Regroup / Subtract and the "ask the neighbor" answers. */
.yn-btn.regroup {
  color: var(--carry);
}

.yn-btn.long {
  font-size: 1.15rem;
  padding: 6px 10px;
  line-height: 1.25;
}

/* Step instructions can run long (regrouping, choosing a chunk), so they get
   a wider line on laptops and smart boards — fewer lines, same height. */
@media (min-width: 760px) and (min-height: 480px) {
  .think-line {
    max-width: 48ch;
  }
  .feedback {
    max-width: 560px;
  }
}

@media (min-width: 760px) and (max-height: 620px) {
  .rem-btn {
    min-height: 30px;
  }
}

/* Short screens (smart boards at big zoom): a little tighter so the Partial
   Quotients number pad (with ⌫ and ✓ Done) fits with no scrolling. */
@media (min-width: 760px) and (max-height: 620px) {
  .level-btn {
    min-height: 28px;
  }
  .key-btn {
    min-height: 38px;
  }
  .keypad .chunk-key {
    min-height: 32px;
  }
  .yn-btn {
    min-height: 82px;
  }
  .yes-no.with-chunk-keys .yn-btn {
    min-height: 120px;
  }
}

@media (min-width: 760px) and (max-height: 620px) {
  .panel-card {
    gap: 6px;
  }
}

/* Once a problem is solved the pad is locked; the ⌫ / Done row steps aside to
   make room for the Next Problem button. */
.keypad.with-chunk-keys.locked .chunk-key {
  display: none;
}


/* Laptops (768–800 tall): slightly smaller buttons so the Partial Quotients
   number pad fits in the side panel with no scrolling. */
@media (min-width: 760px) and (min-height: 621px) and (max-height: 820px) {
  .level-btn {
    min-height: 38px;
  }
  .key-btn {
    min-height: 44px;
  }
  .keypad .chunk-key {
    min-height: 36px;
  }
  .yn-btn {
    min-height: 96px;
  }
  .yes-no.with-chunk-keys .yn-btn {
    min-height: 140px;
  }
  .panel-card {
    gap: 8px;
  }
  .side-panel {
    gap: 8px;
  }
}

/* Upright tablets: the board is narrow, so long step instructions get one
   more line of room (there's plenty of height). */
@media (min-width: 760px) and (min-height: 900px) {
  .think-line,
  .feedback:not(:empty) {
    height: 5.2rem;
  }
}

/* Phones: room for four lines of instructions or hints. */
@media (max-width: 759px), (max-height: 479px) {
  .think-line,
  .feedback:not(:empty) {
    height: 5rem;
    font-size: 0.95rem;
    line-height: 1.3;
  }
}

/* Only the box being typed pops in; finished boxes stay put when the board redraws. */
.carry-box:not(.active) {
  animation: none;
}
