:root {
  --paper: #f5f0e6;
  --card: #fffbf2;
  --ink: #2a241f;
  --ink2: #645b51;
  --line: #d9cfbd;
  --accent: #c8402c;
  --kraft: #eadbb8;
  --sans: 'Noto Sans', system-ui, sans-serif;
  --serif: 'Noto Serif', serif;
  --hand: 'Kalam', cursive;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--paper); color: var(--ink); font-family: var(--sans); }
body { display: flex; flex-direction: column; overflow: hidden; }
button { font: inherit; color: inherit; cursor: pointer; }

#topbar { display: flex; align-items: center; gap: 14px; padding: 8px 16px; border-bottom: 1.5px solid var(--ink); background: var(--paper); z-index: 2; }
.brand { font-family: var(--serif); font-weight: 700; font-size: 18px; white-space: nowrap; }
.brand small { font-family: var(--hand); font-weight: 400; font-size: 17px; color: var(--ink2); margin-left: 8px; }
#lessonNav { display: flex; gap: 6px; flex: 1; overflow-x: auto; scrollbar-width: none; }
#lessonNav button { border: 1.5px solid var(--ink); background: var(--card); border-radius: 999px; padding: 4px 12px; white-space: nowrap; font-size: 14px; }
#lessonNav button[aria-current="true"] { background: var(--ink); color: var(--paper); }
#lessonNav button .seal { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--accent); margin-left: 6px; vertical-align: 1px; }
.tools { display: flex; gap: 6px; }
.tool { border: 1.5px solid var(--ink); background: transparent; border-radius: 8px; padding: 4px 10px; font-size: 13px; white-space: nowrap; }
.tool:hover, #lessonNav button:hover { background: #efe6d3; }
#lessonNav button[aria-current="true"]:hover { background: var(--ink); }

main { flex: 1; display: flex; min-height: 0; }
#panel { width: 410px; flex: none; overflow-y: auto; padding: 22px 22px 48px; border-right: 1.5px solid var(--ink);
  background: var(--paper) repeating-linear-gradient(transparent 0 27px, rgba(120,150,200,.16) 27px 28px); }
#stage { flex: 1; position: relative; min-width: 0; }
#c { display: block; width: 100%; height: 100%; touch-action: none; outline: none; }

.kicker { font-size: 12px; letter-spacing: .14em; color: var(--accent); font-weight: 700; }
#panel h1 { font-family: var(--serif); font-size: 30px; margin: 4px 0 2px; }
#panel h1 small { font-family: var(--hand); font-weight: 400; font-size: 20px; color: var(--ink2); margin-left: 8px; }
.big p { margin: 8px 0; line-height: 1.6; }
.eq { margin: 14px 0 6px; padding: 10px 14px; border: 1.5px solid var(--ink); border-radius: 8px; background: var(--card);
  font-family: var(--serif); font-size: 24px; font-style: italic; text-align: center; }
.eq-words { font-size: 13px; color: var(--ink2); margin: 0 0 10px; text-align: center; }
.keywords { margin: 10px 0; }
.keywords summary { font-weight: 700; font-size: 13px; letter-spacing: .08em; color: var(--accent); cursor: pointer; }
.keywords dl { margin: 6px 0 0; display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; font-size: 14px; }
.keywords dt { font-weight: 700; }
.keywords dd { margin: 0; color: var(--ink2); }

.card { position: relative; margin: 16px 0; padding: 14px 16px 16px; border: 1.5px solid var(--ink); border-radius: 12px; background: var(--card); box-shadow: 3px 3px 0 var(--ink); }
.card h2 { margin: 0 0 8px; font-size: 16px; display: flex; align-items: center; gap: 8px; }
.step { display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: 50%; border: 1.5px solid var(--ink); font-size: 13px; }
.card p { margin: 6px 0; line-height: 1.55; font-size: 15px; }
.small { font-size: 13px !important; color: var(--ink2); }

.opts { display: grid; gap: 6px; margin-top: 8px; }
.opts label { display: flex; gap: 10px; align-items: flex-start; padding: 8px 10px; border: 1.5px dashed var(--line); border-radius: 8px; cursor: pointer; line-height: 1.4; }
.opts label:hover { border-color: var(--ink); }
.opts input { appearance: none; width: 18px; height: 18px; flex: none; margin: 1px 0 0; border: 1.5px solid var(--ink); border-radius: 3px; background: #fff; display: grid; place-items: center; }
.opts input:checked::after { content: '✓'; font-family: var(--hand); font-weight: 700; font-size: 20px; line-height: 0; color: var(--accent); transform: translateY(-1px); }
.opts label:has(input:checked) { border-style: solid; border-color: var(--ink); background: #fff; }

.btn { border: 1.5px solid var(--ink); background: #fff; border-radius: 8px; padding: 7px 12px; font-size: 14px; }
.btn:hover { background: #f1e9d8; }
.btn.primary { background: var(--ink); color: var(--paper); }
.btn.primary:hover { background: #463d35; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.ctl { display: grid; grid-template-columns: 96px 1fr 64px; align-items: center; gap: 8px; margin: 10px 0; font-size: 14px; }
.ctl .lab { font-weight: 500; }
.ctl output { font-family: var(--hand); font-size: 20px; font-weight: 700; text-align: right; }
.ctl input[type=range] { width: 100%; accent-color: var(--ink); }
.seg { display: inline-flex; border: 1.5px solid var(--ink); border-radius: 8px; overflow: hidden; grid-column: span 2; justify-self: start; }
.seg button { border: 0; background: #fff; padding: 5px 12px; font-size: 14px; }
.seg button + button { border-left: 1.5px solid var(--ink); }
.seg button.on { background: var(--ink); color: var(--paper); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip { border: 1.5px solid var(--ink); border-radius: 999px; padding: 3px 10px; background: #fff; font-size: 13px; }
.chip::before { content: ''; display: inline-block; width: 9px; height: 9px; border-radius: 50%; border: 1.5px solid var(--c); margin-right: 6px; vertical-align: -1px; }
.chip.on::before { background: var(--c); }

.flap { display: grid; perspective: 900px; margin-top: 6px; }
.flap > * { grid-area: 1 / 1; }
.flap-body { padding: 4px 2px; visibility: hidden; line-height: 1.6; font-size: 15px; }
.flap.open .flap-body { visibility: visible; }
.flap-cover { position: relative; z-index: 1; border: 1.5px solid var(--ink); border-radius: 8px; padding: 14px 16px; text-align: left; transform-origin: top center;
  background: var(--kraft) repeating-linear-gradient(45deg, transparent 0 9px, rgba(0,0,0,.05) 9px 10px);
  transition: transform .8s cubic-bezier(.3,.7,.2,1), opacity .8s; backface-visibility: hidden; }
.flap-cover b { font-family: var(--hand); font-size: 24px; display: block; }
.flap-cover small { color: var(--ink2); }
.flap.open .flap-cover { transform: rotateX(-178deg); opacity: 0; pointer-events: none; }
.flap.locked .flap-cover { cursor: not-allowed; }
.flap.shake .flap-cover { animation: shake .4s; }
@keyframes shake { 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }
.verdict { font-family: var(--hand); font-size: 21px; font-weight: 700; margin-bottom: 4px; }
.verdict.ok { color: #1f7a4c; }
.verdict.no { color: var(--accent); }

.challenge { padding-right: 112px; min-height: 120px; }
.stamp { position: absolute; right: 14px; top: 14px; width: 92px; height: 92px; border: 4px double var(--accent); border-radius: 50%; color: var(--accent);
  display: grid; place-items: center; text-align: center; font-family: var(--hand); font-weight: 700; font-size: 20px; line-height: 1;
  transform: rotate(-14deg) scale(0); opacity: 0; }
.stamp.on { animation: stamp .5s cubic-bezier(.2,1.6,.4,1) forwards; }
.stamp.static { transform: rotate(-14deg) scale(1); opacity: .9; animation: none; }
.stamp-empty { position: absolute; right: 14px; top: 14px; width: 92px; height: 92px; border: 2px dashed var(--line); border-radius: 50%; display: grid; place-items: center; color: var(--line); font-family: var(--hand); font-size: 18px; }
@keyframes stamp { from { transform: rotate(-14deg) scale(2.4); opacity: 0; } to { transform: rotate(-14deg) scale(1); opacity: .92; } }

.numbers { font-family: var(--hand); font-size: 21px; line-height: 1.45; }
.numbers .sum { border-top: 1.5px dashed var(--ink2); margin-top: 4px; padding-top: 4px; font-weight: 700; }
.numbers b { font-weight: 700; }
.numbers .dim { color: var(--ink2); font-size: 18px; }
.lesson-foot { display: flex; justify-content: space-between; margin-top: 18px; }

#hint { position: absolute; left: 14px; top: 14px; max-width: min(520px, calc(100% - 28px)); padding: 6px 12px; border: 1.5px solid var(--ink); border-radius: 8px;
  background: rgba(255, 251, 242, .92); font-family: var(--hand); font-size: 21px; line-height: 1.3; pointer-events: none; }
#hint:empty { display: none; }
#toast { position: absolute; left: 50%; top: 18%; transform: translateX(-50%); padding: 10px 22px; border: 3px double var(--accent); border-radius: 12px; background: var(--card);
  color: var(--accent); font-family: var(--hand); font-weight: 700; font-size: 30px; pointer-events: none; animation: pop .4s cubic-bezier(.2,1.6,.4,1); }
@keyframes pop { from { transform: translateX(-50%) scale(.4); opacity: 0; } }
#caption { position: absolute; left: 50%; bottom: 18px; transform: translateX(-50%); display: flex; align-items: center; gap: 12px; max-width: calc(100% - 24px);
  padding: 6px 8px 6px 6px; border: 1.5px solid var(--ink); border-radius: 999px; background: var(--card); box-shadow: 3px 3px 0 var(--ink); }
#capText { font-family: var(--hand); font-size: 22px; min-width: 0; }
#capDots { display: flex; gap: 4px; flex: none; }
#capDots i { width: 7px; height: 7px; border-radius: 50%; border: 1.5px solid var(--ink); }
#capDots i.on { background: var(--ink); }
.round { width: 34px; height: 34px; flex: none; border-radius: 50%; border: 1.5px solid var(--ink); background: #fff; font-size: 13px; }
#loading { position: absolute; inset: 0; display: grid; place-items: center; background: var(--paper); font-family: var(--hand); font-size: 28px; transition: opacity .5s; }
#loading.gone { opacity: 0; pointer-events: none; }

/* embedded in a host textbook (ALGET): one lesson, no book chrome */
body.embed #topbar, body.embed .lesson-foot { display: none; }
body.embed #panel { width: 360px; }

dialog#teacher { width: min(860px, calc(100% - 32px)); max-height: calc(100% - 48px); border: 1.5px solid var(--ink); border-radius: 14px; background: var(--paper); color: var(--ink); padding: 26px 30px; box-shadow: 5px 5px 0 var(--ink); }
dialog#teacher::backdrop { background: rgba(42, 36, 31, .35); }
dialog .close { position: absolute; right: 14px; top: 12px; border: 0; background: none; font-size: 20px; }
#teacherBody h2 { font-family: var(--serif); margin: 0 0 6px; }
#teacherBody h3 { font-family: var(--serif); margin: 22px 0 6px; font-size: 18px; }
#teacherBody table { border-collapse: collapse; width: 100%; font-size: 14px; }
#teacherBody th, #teacherBody td { border: 1px solid var(--line); padding: 7px 9px; text-align: left; vertical-align: top; line-height: 1.5; }
#teacherBody th { background: var(--card); }
#teacherBody li { margin: 4px 0; line-height: 1.5; font-size: 14px; }
#teacherBody p { line-height: 1.6; }

@media (max-width: 900px) {
  main { flex-direction: column-reverse; }
  #stage { flex: none; height: 52vh; border-bottom: 1.5px solid var(--ink); }
  #panel { width: auto; flex: 1; border-right: 0; padding: 16px 16px 40px; }
  .brand small { display: none; }
  #topbar { padding: 6px 10px; gap: 8px; }
  #hint { font-size: 18px; }
  #capText { font-size: 18px; }
}
@media (prefers-reduced-motion: reduce) {
  .flap-cover, .stamp.on, #toast { transition: none; animation: none; }
  .stamp.on { transform: rotate(-14deg) scale(1); opacity: .92; }
}
