/* Division: the pictures, the boards and the check.
   ------------------------------------------------------------------------
   Everything the fifth game draws that the other four never needed — the
   eight teaching pictures (dealing, groups, arrays, number lines, bars, fact
   triangles, base-ten rods, chests), the two hands-on stages the child works
   directly, the counting bucket boards, and the "prove it" tap.

   Rules of the house, kept from css/style.css: every size is a clamp() so the
   same layout reads on a phone held sideways and on an iPad, nothing scrolls,
   and every animation is short, runs once as the thing lands, and is switched
   off wholesale under prefers-reduced-motion at the foot of the file.

   Nothing here touches css/style.css; the two are loaded side by side. */

/* ====================================================== shared animations */
@keyframes div-pop {
  from {
    opacity: 0;
    transform: scale(0.55);
  }
}
@keyframes div-drop {
  from {
    opacity: 0;
    transform: translateY(-36px) scale(0.5);
  }
}
@keyframes div-grow {
  from {
    opacity: 0;
    transform: scaleX(0.05);
  }
}
@keyframes div-arc {
  from {
    opacity: 0;
    clip-path: inset(0 100% 0 0);
  }
  to {
    opacity: 1;
    clip-path: inset(0 0 0 0);
  }
}

/* ============================================================ drag boards */

/* A sorting board has two homes; a dealing board can have six, so the
   holders shrink and wrap rather than squeezing off the side of a phone. */
.bucket-row.counting {
  flex-wrap: wrap;
  gap: clamp(5px, 1.6vw, 12px);
}

.bucket-row.counting .bucket {
  flex: 1 1 clamp(70px, 18vw, 124px);
  min-width: clamp(70px, 18vw, 124px);
  max-width: 190px;
  min-height: clamp(76px, 15vh, 150px);
  padding: 5px;
  gap: 3px;
}

.bucket-row.counting .bucket-label {
  font-size: clamp(0.72rem, 2.3vw, 1rem);
  padding: 1px 7px;
}

.bucket-row.counting .zone-items .drag-item {
  --w: clamp(22px, 6.4vw, 42px);
  border-width: 3px;
  border-radius: 10px;
}

/* Twenty things to deal out cannot be the size of four. The bank shrinks in
   two steps so it stays two or three rows and leaves the holders their room. */
.bank-items.counting-bank {
  gap: clamp(3px, 1vw, 9px);
}

.bank-items.counting-bank .drag-item {
  --w: clamp(38px, 9vw, 64px);
  border-width: 3px;
  border-radius: 13px;
}

.bank-items.crowded .drag-item {
  --w: clamp(30px, 7.2vw, 52px);
  border-width: 3px;
  border-radius: 11px;
}

.bank-items.crowded-2 .drag-item {
  --w: clamp(25px, 5.8vw, 42px);
  border-width: 2px;
  border-radius: 9px;
}

.bank-items.counting-bank .drag-item .face {
  font-size: calc(var(--w) * 0.62);
}

/* The tray is not a holder: it is where the bits that will not go round end
   up, so it reads as a different kind of place. */
.bucket.tray {
  border-style: dotted;
  border-color: var(--red);
  background: rgba(226, 87, 76, 0.1);
}

.bucket.fresh {
  animation: div-pop 0.35s ease;
}

/* A crate that is full says so before a finger tries to put a fifth in. */
.bucket[data-cap] {
  border-style: solid;
}

/* The running commentary under the board. Printed, never spoken — a child
   is looking at their own crates when they read it. */
.drag-note {
  margin: 0;
  text-align: center;
  font-size: clamp(0.78rem, 2.6vw, 1.05rem);
  color: var(--ink);
  opacity: 0.72;
}

/* Story cards are sentences, not pictures: wide, wrapped and small. */
.drag-item.story {
  --w: clamp(112px, 33vw, 210px);
  width: var(--w);
  height: auto;
  min-height: clamp(62px, 15vw, 92px);
  padding: 6px 9px;
  cursor: grab;
}

.drag-item.story .text {
  font-size: clamp(0.62rem, 2.15vw, 0.9rem);
  line-height: 1.25;
  text-align: center;
  white-space: normal;
  word-break: normal;
}

.bucket .zone-items .drag-item.story {
  --w: clamp(88px, 26vw, 160px);
  min-height: clamp(52px, 12vw, 76px);
}

.bucket .zone-items .drag-item.story .text {
  font-size: clamp(0.55rem, 1.9vw, 0.78rem);
}

/* A hands-on board is tall, and on a short screen the panel would rather
   squash the question to nothing than admit it. The question does not
   shrink: the board does. */
.drag-stage .prompt,
.drag-stage .kind-badge,
.stage-board .prompt,
.stage-board .kind-badge,
.drag-stage .drag-note {
  flex: 0 0 auto;
}

/* The same goes for a tap puzzle whose picture is a tall one. */
.stage:has(.array-show, .tens-show, .chests-show, .deal-show, .groups-show, .tri-show, .numberline, .barstage) > .prompt,
.stage:has(.array-show, .tens-show, .chests-show, .deal-show, .groups-show, .tri-show, .numberline, .barstage) > .kind-badge {
  flex: 0 0 auto;
}

/* =========================================================== stage boards */
/* A number line to hop along, a bar to cut: the child works the picture
   itself, and the answer row below holds the one button that drives it. */
.choices.stage-controls {
  justify-content: center;
  align-items: center;
}

.stage-step {
  font-size: clamp(1.05rem, 3.6vw, 1.5rem);
  padding: 0.5em 1.3em;
}

.stage-board .stimulus {
  width: 100%;
}

/* ---------------------------------------------------------- number line */
.numberline {
  width: 100%;
  max-width: 640px;
  padding: 0 clamp(14px, 5vw, 30px);
}

.nl-line {
  position: relative;
  height: clamp(58px, 15vw, 96px);
  margin-top: clamp(14px, 4vw, 26px);
  border-bottom: 4px solid var(--ink);
}

.nl-tick {
  position: absolute;
  bottom: 0;
  left: calc(var(--at) * 100%);
  width: 0;
}

.nl-tick::before {
  content: "";
  position: absolute;
  left: -1px;
  bottom: -4px;
  width: 2px;
  height: 7px;
  background: var(--ink);
  opacity: 0.5;
}

.nl-tick.major::before {
  height: 15px;
  width: 3px;
  left: -1.5px;
  opacity: 1;
}

.nl-tick b {
  position: absolute;
  left: 0;
  bottom: -1.55em;
  transform: translateX(-50%);
  font-size: clamp(0.62rem, 2.2vw, 0.95rem);
  font-weight: normal;
}

/* One hop: an arc from where the child was to where they landed, wearing
   the number of the hop so the count is recorded rather than remembered. */
.nl-hop {
  position: absolute;
  bottom: 0;
  left: calc(var(--a) * 100%);
  width: calc(var(--w) * 100%);
  height: clamp(26px, 7vw, 46px);
  border: 3px solid var(--gold-deep);
  border-bottom: none;
  border-radius: 62% 62% 0 0 / 100% 100% 0 0;
  background: rgba(255, 201, 74, 0.16);
  animation: div-arc 0.3s ease backwards;
  animation-delay: min(calc(var(--i) * 120ms), 1.3s);
}

.nl-hop b {
  position: absolute;
  left: 50%;
  top: -0.85em;
  transform: translateX(-50%);
  background: var(--gold);
  border: 2px solid var(--gold-deep);
  border-radius: 999px;
  padding: 0 0.45em;
  font-size: clamp(0.6rem, 2.1vw, 0.88rem);
}

.nl-marker {
  position: absolute;
  bottom: 1px;
  left: calc(var(--at) * 100%);
  transform: translateX(-50%);
  font-size: clamp(1.1rem, 3.8vw, 1.9rem);
  transition: left 0.28s ease;
}

.nl-read {
  margin: 0.15em 0 0;
  text-align: center;
  font-size: clamp(1.3rem, 5vw, 2.2rem);
  color: var(--gold-deep);
}

/* Going past zero is meant to be seen — that is the whole self-correction. */
.numberline.overshot .nl-line {
  border-bottom-color: var(--red);
}

.numberline.overshot .nl-marker {
  filter: saturate(0.4);
  animation: div-pop 0.3s ease;
}

.numberline.overshot .nl-read {
  color: var(--red);
}

.numberline.solved .nl-read {
  color: var(--green);
}

/* --------------------------------------------------------------- the bar */
.barstage {
  width: 100%;
  max-width: 560px;
  text-align: center;
}

.bar-total {
  margin: 0 0 0.25em;
  font-size: clamp(1.5rem, 5.4vw, 2.4rem);
  color: var(--gold-deep);
}

.bar-body {
  display: flex;
  height: clamp(40px, 11vw, 68px);
  border: 4px solid var(--ink);
  border-radius: 14px;
  overflow: hidden;
  background: var(--parchment-2);
}

.bar-part {
  flex: 1 1 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-right: 4px solid var(--ink);
  font-size: clamp(0.95rem, 3.4vw, 1.5rem);
  animation: div-grow 0.28s ease backwards;
  animation-delay: min(calc(var(--i) * 110ms), 1.2s);
}

.bar-part:last-child {
  border-right: none;
}

.bar-body.even .bar-part {
  background: linear-gradient(180deg, #eaffef, #c7f5d8);
}

.bar-read {
  margin: 0.25em 0 0;
  font-size: clamp(0.9rem, 3.2vw, 1.3rem);
  opacity: 0.75;
}

.barstage.overshot .bar-body {
  border-color: var(--red);
}

/* ======================================================= teaching pictures */

/* ------------------------------------------------- dealing (partitive) */
.deal-show {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: center;
  gap: clamp(5px, 1.8vw, 14px);
  max-width: 100%;
}

.deal-holder {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 5px 7px;
  border: 3px dashed var(--parchment-edge);
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.4);
  min-width: clamp(52px, 14vw, 96px);
}

.holder-e {
  font-size: clamp(1.2rem, 4vw, 2rem);
  line-height: 1;
}

.holder-items {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 2px;
  max-width: clamp(56px, 15vw, 104px);
}

.holder-items .item {
  font-size: clamp(0.8rem, 2.7vw, 1.4rem);
  line-height: 1.1;
}

.holder-n {
  font-size: clamp(0.6rem, 2vw, 0.8rem);
  opacity: 0.55;
}

.item.dealt {
  animation: div-drop 0.4s ease backwards;
  animation-delay: min(calc(var(--i) * 80ms), 1.5s);
}

/* ------------------------------------------------- lassos (quotative) */
.groups-show {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: clamp(5px, 1.8vw, 14px);
  max-width: 100%;
}

.lasso {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: clamp(3px, 1vw, 7px) clamp(7px, 2.2vw, 13px);
  border: 3px solid var(--gold-deep);
  border-radius: 999px;
  background: rgba(255, 201, 74, 0.2);
  animation: div-pop 0.34s ease backwards;
  animation-delay: min(calc(var(--i) * 190ms), 1.5s);
}

.lasso .item {
  font-size: clamp(0.95rem, 3.2vw, 1.7rem);
  line-height: 1.1;
}

/* ------------------------------------------------------------- the array */
/* The single best picture in division: turn it ninety degrees and the rows
   become the columns without one thing moving. The wrapper is square so the
   turn never pushes the grid outside its own box. */
.array-show {
  /* The cell is whichever is smaller: a comfortable thumb-sized square, or
     as much as forty per cent of the screen's height divided between the
     rows. A six-by-six array then shrinks instead of shoving the question
     off the parchment. */
  --cell: min(clamp(15px, 4.4vw, 34px), calc(40vh / var(--big)), calc(80vw / var(--big)));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(4px, 1.2vw, 10px);
  max-width: 100%;
}

.array-wrap {
  width: calc(var(--big) * var(--cell));
  height: calc(var(--big) * var(--cell));
  display: grid;
  place-items: center;
}

.array-grid {
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell));
  grid-auto-rows: var(--cell);
  transition: transform 0.6s cubic-bezier(0.34, 0.9, 0.3, 1);
}

.array-grid .item {
  width: var(--cell);
  height: var(--cell);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--cell) * 0.78);
  line-height: 1;
  border-radius: 5px;
  transition: transform 0.6s cubic-bezier(0.34, 0.9, 0.3, 1);
  animation: div-pop 0.34s ease backwards;
  animation-delay: min(calc(var(--i) * 26ms), 1.2s);
}

/* Banding the alternate rows (or columns) is what makes "five rows of four"
   countable at a glance instead of a fog of forty things. */
.array-show.hl-rows .item.r1 {
  background: rgba(221, 148, 32, 0.22);
}

.array-show.hl-cols .item.c1 {
  background: rgba(26, 127, 181, 0.2);
}

.array-show.flipped .array-grid {
  transform: rotate(90deg);
}

.array-show.flipped .array-grid .item {
  transform: rotate(-90deg);
}

.array-label {
  margin: 0;
  font-size: clamp(1rem, 3.8vw, 1.7rem);
  color: var(--gold-deep);
  letter-spacing: 0.03em;
}

.array-flip {
  font: inherit;
  cursor: pointer;
  font-size: clamp(0.8rem, 2.8vw, 1.1rem);
  padding: 0.25em 0.9em;
  border-radius: 999px;
  border: 3px solid var(--gold-deep);
  background: linear-gradient(180deg, #fffdf6, var(--gold));
  color: var(--ink);
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.18);
}

.array-flip:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.18);
}

/* ---------------------------------------------------- the fact triangle */
.tri-show {
  position: relative;
  /* Height-capped as well as width-capped, or a short screen squashes it
     into a doorstop. */
  width: min(clamp(150px, 44vw, 250px), 40vh);
  aspect-ratio: 1 / 0.88;
  flex: 0 0 auto;
  animation: div-pop 0.4s ease backwards;
}

.tri-body {
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, var(--gold), var(--gold-deep));
  clip-path: polygon(50% 0%, 100% 100%, 0% 100%);
}

.tri-n {
  position: absolute;
  transform: translate(-50%, -50%);
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 2.05em;
  height: 2.05em;
  padding: 0 0.25em;
  border-radius: 999px;
  border: 3px solid var(--parchment-edge);
  background: #fffdf6;
  font-size: clamp(0.95rem, 3.6vw, 1.55rem);
  color: var(--ink);
}

.tri-n.top {
  left: 50%;
  top: 20%;
}

.tri-n.left {
  left: 23%;
  top: 83%;
}

.tri-n.right {
  left: 77%;
  top: 83%;
}

.tri-n.covered {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--gold);
  animation: div-pop 0.9s ease-in-out 2;
}

/* One operator, between the two factors and under the product: the triangle
   is read "these two times each other make that one". */
.tri-op {
  position: absolute;
  left: 50%;
  top: 56%;
  transform: translate(-50%, -50%);
  font-size: clamp(1.3rem, 5vw, 2.2rem);
  color: rgba(58, 36, 16, 0.6);
}

/* ------------------------------------------------------- base-ten split */
.tens-show {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(3px, 1.2vw, 9px);
  max-width: 100%;
}

.tens-pool,
.tens-holder {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: center;
  gap: 3px;
}

.rod {
  width: clamp(8px, 2.3vw, 14px);
  height: clamp(38px, 10vw, 66px);
  border: 2px solid #2c6b45;
  border-radius: 3px;
  background: repeating-linear-gradient(180deg, #8fe0ab 0 9%, #4fb377 9% 10%);
  animation: div-pop 0.3s ease backwards;
  animation-delay: min(calc(var(--i) * 55ms), 1s);
}

.cube {
  width: clamp(8px, 2.3vw, 14px);
  height: clamp(8px, 2.3vw, 14px);
  border: 2px solid #a2701a;
  border-radius: 3px;
  background: #f5c45c;
  animation: div-pop 0.3s ease backwards;
  animation-delay: min(calc(var(--i) * 55ms), 1s);
}

.tens-op {
  margin: 0;
  font-size: clamp(0.95rem, 3.4vw, 1.4rem);
  color: var(--gold-deep);
}

.tens-holders {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: clamp(4px, 1.6vw, 11px);
  max-width: 100%;
}

.tens-holder {
  align-items: flex-end;
  min-width: clamp(34px, 9vw, 58px);
  min-height: clamp(44px, 11vw, 72px);
  padding: 4px;
  border: 3px dashed var(--parchment-edge);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.42);
}

.tens-holder.waiting {
  align-items: center;
  justify-content: center;
}

.tens-q {
  font-size: clamp(1.1rem, 4vw, 1.9rem);
  color: var(--gold-deep);
}

.tens-show.split .tens-holder {
  animation: div-pop 0.36s ease backwards;
  animation-delay: min(calc(0.5s + var(--g) * 180ms), 1.6s);
}

.tens-show.split .tens-pool {
  opacity: 0.45;
}

/* --------------------------------------------- chests, for the four rules */
.chests-show {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: center;
  gap: clamp(5px, 2vw, 16px);
  max-width: 100%;
}

.chest {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  animation: div-pop 0.34s ease backwards;
  animation-delay: min(calc(var(--i) * 110ms), 1.3s);
}

.chest-e {
  font-size: clamp(1.6rem, 5.6vw, 2.8rem);
  line-height: 1;
}

.chest-in {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 1px;
  max-width: clamp(58px, 16vw, 108px);
  min-height: 1.2em;
}

.gem {
  font-size: clamp(0.72rem, 2.5vw, 1.25rem);
  line-height: 1.1;
}

.chest.empty .nought {
  font-size: clamp(1rem, 3.4vw, 1.6rem);
  font-weight: bold;
  color: var(--red);
}

.chest-more {
  align-self: center;
  font-size: clamp(1.3rem, 4.6vw, 2.2rem);
  color: var(--gold-deep);
}

/* A picture is a fixed size, not a thing to be squeezed: the stage gives it
   its room and the parchment grows instead. */
.array-show,
.deal-show,
.groups-show,
.chests-show,
.tens-show,
.numberline,
.barstage {
  flex: 0 0 auto;
}

/* ------------------------------------------- tiles that are whole sentences */
/* "4 rows of 6 make 24." will not fit on a tile built for the word "True",
   so a problem that hands out sentences says so and the tiles go roomy. */
.choices.long-tiles .tile.word {
  width: auto;
  min-width: clamp(140px, 34vw, 280px);
  max-width: min(92vw, 620px);
  height: auto;
  min-height: clamp(40px, 8vw, 58px);
  padding: 0.35em 0.85em;
  font-size: clamp(0.82rem, 2.9vw, 1.2rem);
  line-height: 1.2;
  letter-spacing: 0;
}

/* ============================================================== prove it */
/* One tap after every right answer: check the division with its times fact.
   It reuses the ordinary number tiles, so only the sum itself needs dressing. */
.prove-eq {
  font-size: clamp(2rem, 9vw, 4rem);
  color: var(--gold-deep);
  letter-spacing: 0.05em;
  text-shadow: 0 3px 0 rgba(0, 0, 0, 0.12);
  animation: div-pop 0.35s ease backwards;
}

.prove-stage .prompt,
.stage.follow-up .prompt {
  color: var(--ink);
}

.stage.follow-up .prompt {
  animation: div-pop 0.3s ease backwards;
}

/* ============================================ phone held sideways, iPad */
/* A short, wide window is the awkward one: the pictures lose height before
   they lose width, so the tall parts shrink first. */
@media (max-height: 520px) {
  .array-show {
    --cell: min(clamp(13px, 3.4vh, 26px), calc(46vh / var(--big)));
  }
  .nl-line {
    height: clamp(40px, 11vh, 70px);
    margin-top: clamp(10px, 3vh, 20px);
  }
  .bar-body {
    height: clamp(30px, 8.5vh, 52px);
  }
  .rod {
    height: clamp(28px, 8.5vh, 52px);
  }
  .deal-holder {
    min-width: clamp(46px, 11vw, 80px);
  }
  /* A phone on its side gives the board about two hundred pixels of height.
     The badge goes, the question goes small, and everything you drag halves. */
  .drag-stage .kind-badge,
  .stage-board .kind-badge {
    display: none;
  }
  .drag-stage .prompt,
  .stage-board .prompt {
    font-size: clamp(0.82rem, 2.6vh, 1.1rem);
  }
  .bucket-row.counting .bucket {
    min-height: clamp(50px, 15vh, 104px);
    flex-basis: clamp(56px, 14vw, 110px);
    min-width: clamp(56px, 14vw, 110px);
  }
  .bucket-row.counting .zone-items .drag-item {
    --w: clamp(16px, 4.4vh, 30px);
    border-width: 2px;
    border-radius: 7px;
  }
  .bank-items.counting-bank .drag-item {
    --w: clamp(26px, 7vh, 46px);
  }
  .bank-items.crowded .drag-item {
    --w: clamp(22px, 5.6vh, 38px);
  }
  .bank-items.crowded-2 .drag-item {
    --w: clamp(19px, 4.6vh, 32px);
  }
  .drag-item.story {
    --w: clamp(96px, 23vw, 160px);
    min-height: clamp(46px, 13vh, 72px);
  }
  .drag-item.story .text {
    font-size: clamp(0.54rem, 1.7vh, 0.78rem);
  }
  .drag-note {
    font-size: clamp(0.66rem, 2vh, 0.85rem);
  }
  .prove-eq {
    font-size: clamp(1.7rem, 7vh, 3rem);
  }
  /* The little "Numbers" pill is decoration; on two hundred pixels of board
     the picture needs its row more than the label does. */
  .stage:has(.array-show, .tens-show, .chests-show, .deal-show, .groups-show, .tri-show, .numberline, .barstage) > .kind-badge {
    display: none;
  }
  .stage-step {
    font-size: clamp(0.8rem, 2.6vh, 1.15rem);
    padding: 0.35em 0.8em;
  }
  .choices.long-tiles .tile.word {
    min-width: clamp(110px, 26vw, 210px);
    min-height: clamp(30px, 7vh, 44px);
    font-size: clamp(0.7rem, 2.2vh, 0.98rem);
    padding: 0.25em 0.7em;
  }
}

@media (min-width: 820px) {
  .array-show {
    --cell: min(clamp(24px, 3.6vw, 40px), calc(44vh / var(--big)));
  }
}

/* ======================================================== reduced motion */
@media (prefers-reduced-motion: reduce) {
  .item.dealt,
  .lasso,
  .array-grid .item,
  .tri-show,
  .tri-n.covered,
  .rod,
  .cube,
  .chest,
  .bar-part,
  .nl-hop,
  .prove-eq,
  .bucket.fresh,
  .tens-show.split .tens-holder,
  .stage.follow-up .prompt,
  .numberline.overshot .nl-marker {
    animation: none;
  }
  .array-grid,
  .array-grid .item,
  .nl-marker {
    transition: none;
  }
}

/* The hop-size buttons: the one the child commits to stays lit, the rest
   grey out, and the marker walking past zero turns the line red. */
.stage-step.chosen {
  box-shadow: 0 0 0 4px rgba(46, 168, 106, 0.5);
}

.stage-step:disabled {
  opacity: 0.4;
  cursor: default;
}

.nl-marker.past {
  filter: grayscale(0.6);
}

.numberline.overshot .nl-hop {
  border-color: var(--red);
  background: rgba(226, 87, 76, 0.14);
}

.numberline.overshot .nl-hop b {
  background: #ffd9d5;
  border-color: var(--red);
}
