* { box-sizing: border-box }
body { margin: 0; color: #263a35; background: #f6f2e9; font-family: var(--math-font); }
.lesson {
  --pine: #175941; --ink: #253a38; --rule: #8ca78b;
  position: relative; isolation: isolate; container-type: inline-size;
  width: min(100%, max(900px, 150svh)); aspect-ratio: 3 / 2; margin-inline: auto;
  background: #fcf9ef url("pencil-division/desk.webp") center / cover;
}
.lesson[data-mode="place"] { background-image: url("pencil-division/classroom.webp"); }
button, a { -webkit-tap-highlight-color: transparent }
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
button:focus-visible, a:focus-visible { outline: 4px solid #165899; outline-offset: 3px; z-index: 1; }
#question:focus { outline: none; }
.mast { position: absolute; inset: 5.8% 0 auto; display: flex; align-items: center; justify-content: center; gap: 2.7cqw; color: #354537; font-size: 2.25cqw; font-weight: 750; }
.mast a { padding: .05em .45em; background: #e0ecd28c; border-radius: 56% 39% 51% 37%; color: #214c42; font-size: 1.29em; font-weight: 850; text-decoration: none; letter-spacing: -.07em; }
.mast > span { padding: .17em .72em; background: #e3ead8; border-radius: 2em; }
.mast b { color: #243d31; background: #e3edda; border-radius: .3em; padding: .12em .45em; }
h1 { position: absolute; inset: 20% 3% auto; margin: 0; text-align: center; font-size: 4.55cqw; font-weight: 850; line-height: 1.23; letter-spacing: -.055em; word-break: keep-all; text-wrap: balance; }
.lesson[data-page="place"] h1 { inset: 20.9% 0 auto 6.5%; font-size: 5.85cqw; color: #35291f; }
h1::after { content: ""; display: block; width: 7.5cqw; height: .5cqw; border-radius: 50%; background: #e8bd5b; margin: .65cqw auto 0; transform: rotate(-2deg); }
@media (min-width: 721px) and (min-aspect-ratio: 1/1) {
  .lesson[data-page="place"]::before { content: ""; position: absolute; inset: 0; background: url("pencil-division/brush.webp") center / 100% 100%; pointer-events: none; }
  .lesson[data-page="place"] h1::after { display: none; }
  .mast { inset: 6.4% 0 auto 3.5%; }
  .mast a { padding: .1em .75em; }
  .mast b { font-size: 1.29em; }
}
button, .action { border: 0; }
.action, .choice, .object-button > span:last-child {
  display: inline-flex; align-items: center; justify-content: center; min-height: 46px; border-radius: .75em;
  padding: .52em 1.1em; font-size: max(17px, 1.7cqw); font-weight: 750; text-decoration: none; line-height: 1.35;
}
.action, .object-button > span:last-child { background: var(--pine); color: white; }
.action:hover, .object-button:hover > span:last-child { background: #0d4431; }
.work img { display: block; width: 100%; height: auto; user-select: none; }
.bundle { display: inline-grid; position: relative; align-items: center; justify-items: center; }
.bundle > span { position: absolute; display: grid; place-items: center; inset: 5% 37%; border-radius: 8%; background: #f6db99; color: #563a16; font-weight: 850; font-size: max(10px, .91cqw); white-space: nowrap; }
.loose { display: grid; align-content: center; justify-items: center; row-gap: .12em; }
.loose:empty { display: none; }
.stock { position: absolute; inset: 36% 5% auto; text-align: center; }
.supply { display: flex; align-items: center; justify-content: center; gap: 1.6cqw; min-height: 9cqw; }
.supply > .bundle { width: 11cqw; }
.supply > .loose { width: 11cqw; }
.supply > .loose:only-child { width: 32cqw; grid-template-columns: repeat(2, 1fr); gap: .36cqw 1.6cqw; }
.stock p { word-break: keep-all; text-wrap: balance; margin: .4em 0 0; font-size: max(16px, 1.65cqw); font-weight: 650; }
.cases { position: absolute; inset: 59% 7% auto; display: flex; justify-content: center; gap: 3cqw; }
.pouch { display: grid; justify-items: center; align-content: start; gap: .35em; width: 22%; background: none; padding: .3em; border-radius: 15px; }
.four .pouch { width: 21%; }
.case-art { display: block; position: relative; width: 100%; }
.packed { position: absolute; inset: 55% 12% 17%; display: flex; flex-direction: column; justify-content: center; align-items: center; }
.packed > .bundle { width: 74%; max-height: 100%; }
.packed > .bundle img { max-height: 100%; object-fit: contain; }
.packed > .bundle > span { font-size: max(9px,.7cqw); }
.packed > .loose { width: 72%; max-height: 100%; grid-auto-flow: column; grid-template-rows: repeat(4,auto); grid-auto-columns: 1fr; row-gap: 0; column-gap: 3%; }
.packed:has(.bundle + .loose) { display: grid; grid-template-columns: 1fr 1fr; gap: 5%; }
.packed > .bundle:has(+ .loose) { width: 100%; }
.packed > .bundle + .loose { width: 100%; max-height: none; grid-template-rows: none; grid-auto-flow: row; row-gap: .18cqw; }
.case-label { display: flex; align-items: center; gap: .55em; font-size: max(16px, 1.5cqw); font-weight: 700; white-space: nowrap; }
.case-label b { color: var(--pine); }
button.pouch .case-label { background: #fffaf0; border: 2px solid #668d74; border-radius: 2em; padding: .25em .7em; }
button.pouch:hover .case-art { filter: drop-shadow(0 2px 3px #43574755); }
.pouch--preview {
  background: #fff5d6;
  box-shadow: 0 0 0 2px #a97526, 0 0 0 5px #f8dea0;
}
button.pouch--preview .case-label { border-color: #a97526; background: #ffe6a8; }
.work > .action { position: absolute; inset: auto auto 5% 50%; transform: translateX(-50%); }
.pack-view, .exchange, .count-view { position: absolute; inset: 38% 7% 13%; display: grid; align-content: center; justify-items: center; }
.object-button { display: grid; justify-items: center; gap: 1.7cqw; background: none; padding: .5em; border-radius: 16px; }
.pack-view .loose, .groups > div:first-child .loose { grid-template-columns: repeat(2,1fr); gap: .6cqw 2cqw; width: 47cqw; }
.exchange > div, .groups { display: flex; align-items: center; justify-content: center; gap: 6cqw; }
.exchange .bundle { width: 28cqw; }
.exchange .bundle > span, .case-contents .bundle > span {
  inset-inline: 50% auto;
  width: max-content;
  min-width: 26%;
  padding-inline: .2em;
  transform: translateX(-50%);
  font-size: max(14px, 1.65cqw);
}
.original, .groups > div { display: grid; justify-items: center; gap: 1.2cqw; font-size: max(16px, 1.65cqw); font-weight: 650; text-align: center; }
.original .loose, .groups > div:last-child .loose { width: 20cqw; gap: .5cqw; }
.choices { display: flex; justify-content: center; flex-wrap: wrap; gap: .6em; }
.count-view .choices { margin-top: 3cqw; }
.choice { background: #fffbef; color: var(--pine); border: 2px solid #779c87; }
.choice:hover, .positions button:hover { background: #f0f6e6; }
.closeup { position: absolute; inset: 0; pointer-events: none; }
.callout { position: absolute; left: 32.5%; top: 40%; padding: .26em .85em; background: #faf1c8; border: .12cqw solid #d5d4a0; border-radius: 1.5em; color: #30483d; font-size: 2.25cqw; font-weight: 740; }
.callout::after { content: ""; position: absolute; top: 98%; left: 44%; border: .9cqw solid transparent; border-top: 2.4cqw solid #f7efc5; }
.case-contents { position: absolute; left: 26.5%; top: 65.2%; width: 30%; height: 16.5%; display: flex; justify-content: center; align-items: center; flex-direction: column; }
.case-contents > .bundle { width: 97%; }
.case-contents .bundle > span { font-size: max(14px, 2.45cqw); background: #f8e4b8; }
.case-contents:has(.loose) { flex-direction: row; justify-content: space-between; gap: 6%; }
.case-contents:has(.loose) > .bundle { width: 49%; }
.case-contents:has(.loose) .bundle > span { font-size: max(14px, 1.25cqw); }
.case-contents .loose { width: 58%; gap: .23cqw; }
.positions, .answer-side { position: absolute; left: 60.7%; top: 46%; width: 37.4%; height: 40.8%; }
.positions { display: grid; grid-template-columns: repeat(2, 1fr); padding: .8cqw; border: .3cqw solid #cad2ad; border-radius: 2.8cqw; overflow: hidden; background: #fffdf2; }
.positions button { padding: 0; display: grid; grid-template-rows: 30% 1fr; justify-items: stretch; background: transparent; }
.positions button + button { border-left: .15cqw solid #d3d5ad; }
.positions button span { display: grid; place-items: center; background: #e0ebd0; font-size: 3.2cqw; color: #345b45; font-weight: 800; }
.positions button:first-child span { border-top-left-radius: 2cqw; }
.positions button:last-child span { border-top-right-radius: 2cqw; }
.positions b { align-self: center; justify-self: center; width: 77%; aspect-ratio: 1.06; border: .32cqw solid #c6cfaa; border-radius: 1.55cqw; }
.positions button:focus-visible { outline-offset: -6px; border-radius: 1em; }
.answer-side { display: flex; flex-direction: column; justify-content: center; align-items: center; gap: 1.1cqw; height: auto; min-height: 36%; background: #fffdf2ed; border: 2px solid #a3b49a; border-radius: 2.5cqw; padding: 1.3cqw; }
.written { width: 57%; display: grid; grid-template-columns: .8fr 1fr 1fr; align-items: center; text-align: center; }
.written small { color: var(--pine); font-weight: 700; font-size: max(14px, 1.5cqw); }
.written small:first-child, .written b:first-of-type { grid-column: 2; }
.written b, .written span, .written em { font-size: 3.7cqw; font-style: normal; font-weight: 700; line-height: 1.15; }
.written b { color: var(--pine); }
.written em { border-top: .22cqw solid var(--ink); }
.written em:first-of-type { border-left: .22cqw solid var(--ink); border-top-left-radius: .3em; }
.summary p, .summary strong { margin: 0; font-size: max(17px, 1.7cqw); text-align: center; }
.summary strong { font-size: max(22px, 2.3cqw); }
#feedback { position: absolute; bottom: 2%; left: 22%; right: 5%; margin: 0; text-align: center; font-size: max(16px, 1.55cqw); font-weight: 650; color: #593e18; }
#feedback:not(:empty) { padding: .45em .7em; background: #fff5d9; border-radius: 1em; }
#back, #restart { position: absolute; bottom: 2%; left: 2%; min-height: 44px; padding: .5em .7em; background: #fffaf0e6; color: #37584b; border-radius: .6em; font-size: max(14px, 1.2cqw); }
#restart { left: auto; right: 2%; }
@media (max-width: 720px), (max-aspect-ratio: 1/1) {
  body { background: #fbf8ef; }
  .lesson, .lesson[data-mode="place"] { width: min(100%, 700px); min-height: 100svh; aspect-ratio: auto; padding: 1rem clamp(12px,4vw,27px); background-image: url("pencil-division/desk.webp"); }
  .mast, h1, .lesson[data-page="place"] h1, .work > .action, .stock, .cases, .pack-view, .exchange, .count-view, .positions, .answer-side, #feedback, #back, #restart { position: static; }
  .mast { flex-wrap: wrap; gap: .35em .72em; font-size: clamp(14px,3vw,19px); }
  .mast a { font-size: 1.35em; }
  .mast > span { padding: .1em .3em; }
  h1, .lesson[data-page="place"] h1 { margin: clamp(20px,4.5vh,48px) auto 1rem; font-size: clamp(24px,5.7vw,42px); }
  h1::after, .lesson[data-page="place"] h1::after { width: 3.5em; height: .1em; margin-top: .3em; }
  .work { display: grid; justify-items: center; gap: 1rem; }
  .stock { width: 100%; }
  .supply { min-height: 72px; gap: 1.4%; flex-wrap: wrap; }
  .supply > .bundle { width: 16%; }
  .supply > .loose { width: 14%; }
  .supply > .loose:only-child { width: 74%; gap: .22em .65em; }
  .stock p { margin: .6em 0 .2em; }
  .cases { width: 100%; display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: .8rem .5rem; }
  .pouch, .four .pouch { width: 100%; gap: .12em; padding: 0 .15em; }
  .case-art { max-width: 195px; }
  .four .pouch--preview { width: min(100%, calc(195px + .3em)); justify-self: center; }
  .case-label { font-size: clamp(14px,3.8vw,18px); gap: .3em; }
  button.pouch .case-label { padding: .2em .45em; }
  .work > .action { transform: none; }
  .pack-view, .exchange, .count-view { width: 100%; margin-block: 1.5rem; }
  .pack-view .loose, .groups > div:first-child .loose { width: 80cqw; gap: .5rem 1rem; }
  .object-button { gap: 1rem; }
  .exchange > div { flex-direction: column; gap: 1.5rem; }
  .exchange .bundle { width: 55cqw; }
  .groups { flex-wrap: wrap; gap: 1.5rem; }
  .original .loose, .groups > div:last-child .loose { width: 42cqw; }
  .count-view .choices { margin-top: 1.5rem; }
  .exchange .bundle > span, .case-contents .bundle > span { font-size: max(14px, 2.5cqw); }
  .closeup { position: relative; inset: auto; width: min(90%, 400px); aspect-ratio: 1.68; margin-top: 1.5rem; background: url("pencil-division/case.webp") center / contain no-repeat; }
  .callout { left: 50%; top: -1.65rem; transform: translateX(-50%); white-space: nowrap; font-size: 16px; }
  .case-contents { left: 16%; top: 55%; width: 66%; height: 31%; transform: none; }
  .positions, .answer-side { width: min(100%, 430px); }
  .positions { height: clamp(145px, 24vh, 220px); border-width: 3px; border-radius: 18px; }
  .positions button span { font-size: clamp(20px,4.8vw,30px); }
  .positions b { height: 73%; width: auto; border-width: 2px; }
  .answer-side { padding: .95rem; border-radius: 16px; gap: .65rem; }
  .written { width: 48%; }
  .written b, .written span, .written em { font-size: clamp(26px,6.5vw,38px); }
  #feedback:not(:empty) { margin: 1rem auto .25rem; }
  #back, #restart { margin-top: 1rem; }
}
@media (prefers-reduced-motion: no-preference) {
  button.pouch:hover .case-art { transform: translateY(-2px); }
  .pouch--preview { animation: pouch-glow 1.4s ease-in-out 3; }
  @keyframes pouch-glow {
    50% { box-shadow: 0 0 0 2px #a97526, 0 0 0 5px #f8dea0, 0 0 16px 6px #edb53ba6; }
  }
}
