/* Layout: one screen. A 3D box (2 or 3 numbers along each side) that always settles face-on and right side up.
   Each row/column is a ring around the box; its target is a bingo ball on every edge it crosses. Number pad below. */
:root{
  --paper:#edf2f8; --rule:rgba(38,78,150,.09); --ink:#18263f; --muted:#5b6983;
  --tile:#ffffff; --tile-edge:#c3cfe0; --given:#dfe7f2; --slot:#f8fafd; --slot-line:#9cadc9;
  --face:#f9fbfe; --face-line:#b8c6dc;
  --you:#2f5bff; --you-soft:#dde5ff; --you-edge:#1c3dc2; --on-you:#ffffff;
  --good:#12a165; --on-good:#ffffff; --bad:#e0434a; --bad-soft:#ffd9da; --gold:#f2b705;
  --r0:#ef5a4f; --r1:#f39a1e; --r2:#13a89e; --r3:#8b5cf6; --r4:#e2468f; --r5:#1f9fd6; --r6:#6aa121; --r7:#c2410c; --r8:#4f46e5;
  --ball-face:#ffffff; --ball-ink:#18263f;
  --warn:#ffd84d; --warn-edge:#d9a400; --on-warn:#3d2c00;
  --panel:rgba(255,255,255,.75); --panel-line:rgba(24,38,63,.1); --scrim:rgba(237,242,248,.72);
  --font-display:"Fredoka","Baloo 2","Trebuchet MS",system-ui,sans-serif;
  --font-body:"Nunito",system-ui,-apple-system,"Segoe UI",sans-serif;
}
@media (prefers-color-scheme: dark){ :root:not([data-theme="light"]){
  --paper:#0e1525; --rule:rgba(140,170,255,.07); --ink:#e6ecfa; --muted:#93a0bb;
  --tile:#1b2640; --tile-edge:#070b15; --given:#24304d; --slot:#121a2d; --slot-line:#4a5b82;
  --face:#151e33; --face-line:#3c4c70;
  --you:#6684ff; --you-soft:#1d2858; --you-edge:#3550d0; --on-you:#ffffff;
  --good:#38d895; --on-good:#062a1a; --bad:#ff6a70; --bad-soft:#4a1a1f; --gold:#ffd23f;
  --r0:#ff7a6e; --r1:#ffb347; --r2:#2fd0c3; --r3:#a98bff; --r4:#ff6fb1; --r5:#4cc3f5; --r6:#8fd14f; --r7:#ff8a4c; --r8:#8b8cff;
  --ball-face:#ffffff; --ball-ink:#18263f;
  --warn:#f5c518; --warn-edge:#a87f00; --on-warn:#2a1e00;
  --panel:rgba(22,31,52,.8); --panel-line:rgba(230,236,250,.1); --scrim:rgba(14,21,37,.74);
  color-scheme:dark;
}}
:root[data-theme="dark"]{
  --paper:#0e1525; --rule:rgba(140,170,255,.07); --ink:#e6ecfa; --muted:#93a0bb;
  --tile:#1b2640; --tile-edge:#070b15; --given:#24304d; --slot:#121a2d; --slot-line:#4a5b82;
  --face:#151e33; --face-line:#3c4c70;
  --you:#6684ff; --you-soft:#1d2858; --you-edge:#3550d0; --on-you:#ffffff;
  --good:#38d895; --on-good:#062a1a; --bad:#ff6a70; --bad-soft:#4a1a1f; --gold:#ffd23f;
  --r0:#ff7a6e; --r1:#ffb347; --r2:#2fd0c3; --r3:#a98bff; --r4:#ff6fb1; --r5:#4cc3f5; --r6:#8fd14f; --r7:#ff8a4c; --r8:#8b8cff;
  --ball-face:#ffffff; --ball-ink:#18263f;
  --warn:#f5c518; --warn-edge:#a87f00; --on-warn:#2a1e00;
  --panel:rgba(22,31,52,.8); --panel-line:rgba(230,236,250,.1); --scrim:rgba(14,21,37,.74);
  color-scheme:dark;
}
html,body{height:100%}
body{background-color:var(--paper); background-image:linear-gradient(var(--rule) 1px,transparent 1px),linear-gradient(90deg,var(--rule) 1px,transparent 1px); background-size:22px 22px;
  color:var(--ink); font-family:var(--font-body); margin:0; overflow:hidden; -webkit-tap-highlight-color:transparent; user-select:none; -webkit-user-select:none}
button{font:inherit; color:inherit; cursor:pointer}
[hidden]{display:none!important}
#app{height:100vh; height:100dvh; max-width:640px; margin:0 auto; display:flex; flex-direction:column; gap:6px; box-sizing:border-box;
  padding-left:max(16px, env(safe-area-inset-left, 0px)); padding-right:max(16px, env(safe-area-inset-right, 0px));
  padding-top:max(8px, env(safe-area-inset-top, 0px)); padding-bottom:max(8px, env(safe-area-inset-bottom, 0px))}

.hud{display:flex; align-items:center; gap:8px; flex-wrap:wrap}
.stat{display:flex; align-items:baseline; gap:6px; padding:5px 11px; border-radius:14px; background:var(--panel); border:1px solid var(--panel-line)}
.stat .lab{font-size:10px; font-weight:800; letter-spacing:.08em; text-transform:uppercase; color:var(--muted)}
.stat b{font-family:var(--font-display); font-weight:600; font-size:20px; font-variant-numeric:tabular-nums}
#scoreBox b{color:var(--you)}
.stat.bump b{animation:bump .35s ease}
@keyframes bump{40%{transform:scale(1.22)}}
.lvl{font-family:var(--font-display); font-weight:600; font-size:15px; padding:3px 10px; border-radius:999px; background:var(--ink); color:var(--paper); white-space:nowrap}
.combo{margin-left:auto; font-size:12px; font-weight:800; color:var(--you)}
.status{min-height:20px; font-size:14px; font-weight:700; color:var(--muted); text-align:center}
.status.pop{animation:bpop .3s cubic-bezier(.3,1.6,.5,1)}
@keyframes bpop{0%{transform:scale(.85); opacity:.3}}

/* Stage + box */
.stage{position:relative; flex:1 1 0; min-height:200px; perspective:1600px; touch-action:none; overflow:hidden; border-radius:18px; cursor:grab}
.stage.turning{cursor:grabbing}
/* The box is centred in the part of the stage above the turn buttons, so they never cover it. */
.cubewrap{position:absolute; left:50%; top:calc((100% - var(--ctl-h, 48px)) / 2); width:0; height:0; transform-style:preserve-3d}
/* Full width on phones: the stage runs to the safe-area edges (the box is width-bound there). */
.stage{margin-left:calc(env(safe-area-inset-left, 0px) - max(16px, env(safe-area-inset-left, 0px))); margin-right:calc(env(safe-area-inset-right, 0px) - max(16px, env(safe-area-inset-right, 0px))); border-radius:0}
/* Wide stage: the turn buttons stand in a column on the right and the box centres in the rest. */
.stage.side .cubewrap{left:calc((100% - var(--ctl-h, 48px)) / 2); top:50%}
.cube{position:absolute; left:0; top:0; width:0; height:0; transform-style:preserve-3d; --cell:48px; --B:44px}
.face{position:absolute; box-sizing:border-box; display:grid; gap:3px; padding:4px; border-radius:calc(var(--cell) * .3); background:var(--face); border:2px solid var(--face-line);
  backface-visibility:hidden; -webkit-backface-visibility:hidden; transition:background .5s, border-color .5s, box-shadow .5s}
.face.done{background:color-mix(in srgb, var(--good) 14%, var(--face)); border-color:var(--good)}
.face.flash{animation:faceFlash .9s ease}
@keyframes faceFlash{30%{box-shadow:0 0 0 6px var(--good), 0 0 40px color-mix(in srgb, var(--good) 60%, transparent)}}
.fname{grid-row:1; justify-self:center; align-self:start; font-size:calc(var(--cell) * .2); font-weight:800; letter-spacing:.1em; text-transform:uppercase; color:var(--muted); opacity:.7; margin-top:2px}
.cell{position:relative; display:grid; place-items:center; border:0; padding:0; min-width:0; min-height:0; border-radius:calc(var(--cell) * .22);
  font-family:var(--font-display); font-weight:600; font-size:calc(var(--cell) * .55); line-height:1; font-variant-numeric:tabular-nums; transition:background .15s, border-color .15s, transform .18s cubic-bezier(.3,1.6,.5,1), opacity .25s}
.cell.given.num{background:var(--given); color:var(--ink)}
.cell.given.op{color:var(--muted); font-size:calc(var(--cell) * .48)}
.cell.eq{color:var(--rc, var(--ink)); font-size:calc(var(--cell) * .5); font-weight:700}
.cell.eq.v .val{display:inline-block; transform:rotate(90deg)}
.cell .val{white-space:nowrap; text-align:center}
.key .kl{display:inline-block; white-space:nowrap; text-align:center}
.cell.long .val{font-size:.66em; letter-spacing:-.02em; white-space:nowrap}
.cell.word .val{font-size:.62em; font-weight:800; font-family:var(--font-body)}
.cell.base::before,.cell.base::after{position:absolute; top:50%; transform:translateY(-54%); font-weight:400; font-size:1.1em; color:var(--ink); opacity:.75}
.cell.base::before{content:'('; left:-.08em} .cell.base::after{content:')'; right:-.08em}
.cell.lean-h .val{display:inline-block; transform:translateX(18%)}
.cell.lean-v .val{display:inline-block; transform:translateY(18%)}
.cell.slot{background:var(--slot); border:2px dashed var(--slot-line)}
.cell.slot.op{border-radius:50%}
.cell.slot.picked{border-style:solid; border-color:var(--you); background:var(--you-soft); box-shadow:0 0 0 3px var(--you); animation:picked 1s ease-in-out infinite; z-index:2}
@keyframes picked{50%{box-shadow:0 0 0 7px color-mix(in srgb, var(--you) 30%, transparent)}}
/* Filled but not checked yet: neutral (no red until a finished line is actually wrong). */
.cell.slot.loose{background:var(--tile); border:2px solid var(--slot-line); color:var(--ink); transform:rotate(var(--tilt, 0deg))}
.cell.slot.loose.bad{background:var(--bad-soft); border-color:var(--bad); color:var(--bad)}
/* Yellow: the sums work, but a striped ball they use isn't the right value. */
.cell.slot.iffy{background:var(--warn); border:2px solid var(--warn-edge); color:var(--on-warn); box-shadow:0 3px 0 var(--warn-edge)}
.cell.slot.iffy.picked{box-shadow:0 0 0 3px var(--you)}
.face.iffy{background:color-mix(in srgb, var(--warn) 30%, var(--face)); border-color:var(--warn-edge)}
.ball.iffy{box-shadow:0 0 0 3px var(--ball-face), 0 0 0 7px var(--warn), 0 0 16px var(--warn)}
.opg{display:block; width:.82em; height:.82em; fill:currentColor; overflow:visible}
.key .opg{width:24px; height:24px; margin:auto}
.cell.slot.loose.picked{transform:none}
.cell.slot.stuck{background:var(--good); border:2px solid color-mix(in srgb, var(--good) 72%, #000); color:var(--on-good); box-shadow:0 3px 0 color-mix(in srgb, var(--good) 72%, #000)}
.cell.slot.stuck.picked{box-shadow:0 0 0 3px var(--you)}
.cell.slot.locked{background:var(--you); border:2px solid var(--you-edge); color:var(--on-you)}
.cell.slot:focus-visible,.key:focus-visible,.btn:focus-visible,.chip:focus-visible{outline:3px solid var(--you); outline-offset:2px}
.face.focusing .cell:not(.hl){opacity:.35}
.cell.jelly{animation:jelly .5s ease both}
@keyframes jelly{0%{transform:scale(1.3,.7)}30%{transform:scale(.82,1.18)}50%{transform:scale(1.1,.9)}70%{transform:scale(.96,1.04)}100%{transform:scale(1)}}
.cell.solidify{animation:solid .6s cubic-bezier(.3,1.7,.5,1)}
@keyframes solid{0%{transform:scale(1.35); filter:brightness(1.8)}55%{transform:scale(.9)}100%{transform:none}}
.cell.shake{animation:shake .42s ease}
@keyframes shake{15%{transform:translateX(-6px) rotate(-6deg)}35%{transform:translateX(6px) rotate(6deg)}55%{transform:translateX(-4px)}75%{transform:translateX(3px)}}
.cell.rewind{animation:rewind .5s cubic-bezier(.3,1.4,.5,1)}
@keyframes rewind{from{transform:rotate(0) scale(.6)}to{transform:rotate(-360deg) scale(1)}}
.cell.wave{animation:wave .7s ease both}
@keyframes wave{40%{transform:scale(1.18) translateY(-3px); box-shadow:0 0 0 3px var(--good)}}
.cell.intro{animation:introPop .5s cubic-bezier(.3,1.7,.5,1) both}
@keyframes introPop{0%{transform:scale(0) rotate(-35deg); opacity:0}}

/* Bingo balls: one per ring per edge, always turned to face you */
.ball{position:absolute; left:0; top:0; width:var(--B); height:var(--B); margin:calc(var(--B) / -2) 0 0 calc(var(--B) / -2); border-radius:50%; pointer-events:none;
  display:grid; place-items:center; font:700 calc(var(--B) * .4)/1 var(--font-display); color:var(--ball-ink); font-variant-numeric:tabular-nums;
  background:radial-gradient(circle at 50% 52%, var(--ball-face) 0 38%, transparent 39%), radial-gradient(circle at 34% 28%, color-mix(in srgb, var(--rc) 35%, #fff) 0, var(--rc) 45%, color-mix(in srgb, var(--rc) 60%, #000) 100%);
  box-shadow:0 5px 12px rgba(0,0,0,.28); transition:box-shadow .3s}
.ball span{display:block; transition:transform .3s; white-space:nowrap}
.ball.long{width:calc(var(--B) * 1.25); height:calc(var(--B) * 1.25); margin:calc(var(--B) * -.625) 0 0 calc(var(--B) * -.625)}
.ball.long span{font-size:.92em; letter-spacing:-.04em}
.ball.ok{--rc:var(--good); box-shadow:0 0 0 3px var(--ball-face), 0 0 18px var(--good)}
.ball.hl{box-shadow:0 0 0 4px var(--ball-face), 0 0 0 8px var(--rc), 0 6px 14px rgba(0,0,0,.3)}
.ball.pop span{animation:ballpop .6s cubic-bezier(.3,1.8,.5,1)}
@keyframes ballpop{0%{transform:scale(.4) rotate(-90deg)}100%{transform:none}}
.ball.blank{pointer-events:auto; cursor:pointer}
.ball.bad{box-shadow:0 0 0 3px var(--ball-face), 0 0 0 7px var(--bad, #e5484d), 0 0 16px var(--bad, #e5484d)}
.ball.blank.empty{color:var(--muted); background:radial-gradient(circle at 50% 52%, var(--ball-face) 0 38%, transparent 39%), repeating-conic-gradient(var(--rc) 0 22.5deg, color-mix(in srgb, var(--rc) 45%, #fff) 22.5deg 45deg)}
/* A striped ball you've typed into stays marked as yours to change: dashed ring + pencil badge. */
.ball.blank:not(.empty){outline:2px dashed var(--rc); outline-offset:3px}
.ball.blank:not(.empty)::after{content:'✎'; position:absolute; right:-6%; bottom:-6%; width:38%; height:38%; border-radius:50%; display:grid; place-items:center;
  background:var(--ball-face); color:var(--ink); font:700 calc(var(--B) * .22)/1 var(--font-body); box-shadow:0 0 0 1.5px var(--rc), 0 2px 4px rgba(0,0,0,.25)}
.ball.picked{box-shadow:0 0 0 4px var(--ball-face), 0 0 0 9px var(--you), 0 6px 14px rgba(0,0,0,.3)}

.turnbar{position:absolute; inset:auto 8px 8px auto; display:flex; gap:6px; z-index:4}
.turnbtn{width:36px; height:36px; border-radius:50%; border:1px solid var(--panel-line); background:var(--tile); box-shadow:0 3px 0 var(--tile-edge); font:700 16px/1 var(--font-display); padding:0}
.turnbtn:active{transform:translateY(2px); box-shadow:0 1px 0 var(--tile-edge)}
.viewchip{position:absolute; left:12px; bottom:10px; z-index:4}
.turnbar{right:12px}
.stage.side .turnbar{inset:50% 8px auto auto; transform:translateY(-50%); flex-direction:column}
.stage.side .turnbtn[data-dir="U"]{order:0} .stage.side .turnbtn[data-dir="L"]{order:1} .stage.side .turnbtn[data-dir="R"]{order:2} .stage.side .turnbtn[data-dir="D"]{order:3}
/* In the side column "Whole box" becomes a round icon: two stacked squares (whole box) or one (back to face). */
.stage.side .viewchip{left:auto; right:8px; bottom:auto; top:8px; width:36px; height:36px; padding:0; border-radius:50%; font-size:0; color:var(--ink)}
.stage.side .viewchip::before,.stage.side .viewchip::after{content:''; position:absolute; width:11px; height:11px; border:2px solid currentColor; border-radius:3px; box-sizing:content-box}
.stage.side .viewchip::before{left:8px; top:13px}
.stage.side .viewchip::after{left:13px; top:8px; background:var(--tile)}
.stage.side .viewchip[data-over="1"]::before{left:10px; top:10px; width:12px; height:12px}
.stage.side .viewchip[data-over="1"]::after{display:none}

/* Pad */
.pad{display:flex; flex-direction:column; gap:6px; padding:8px 10px 10px; border-radius:18px; background:var(--panel); border:1px solid var(--panel-line)}
.pad-head{display:flex; justify-content:space-between; align-items:center; gap:8px; min-height:22px}
.pad-label{font-size:11px; font-weight:800; letter-spacing:.08em; text-transform:uppercase; color:var(--muted)}
.keys{display:grid; grid-template-columns:repeat(11, 1fr); gap:6px}
.key{height:46px; border-radius:12px; border:2px solid var(--tile-edge); background:var(--tile); box-shadow:0 4px 0 var(--tile-edge); font:600 22px/1 var(--font-display); color:var(--ink); padding:0; transition:transform .1s, opacity .2s}
.key:active{transform:translateY(3px); box-shadow:0 1px 0 var(--tile-edge)}
.key[disabled]{opacity:.35; pointer-events:none}
.key.clear{font-size:13px; font-weight:800; font-family:var(--font-body)}
.key.var{background:var(--you-soft); border-color:var(--you); color:var(--you); font-style:italic}
.key.long{font-size:17px} .key.word{font-size:16px; font-family:var(--font-body); font-weight:800}
/* Mode picker on the start card */
.modes{display:grid; grid-template-columns:repeat(3, 1fr); gap:8px}
.mode{display:flex; flex-direction:column; align-items:center; gap:3px; padding:10px 4px; border-radius:14px; border:2px solid var(--panel-line); background:var(--given); box-shadow:0 3px 0 var(--tile-edge)}
.mode b{font-family:var(--font-display); font-weight:600; font-size:18px}
.mode span{font-size:12px; font-weight:700; color:var(--muted); white-space:nowrap}
.mode[aria-checked="true"]{border-color:var(--you); background:var(--you-soft); box-shadow:0 3px 0 var(--you)}
.submodes{display:flex; gap:8px; justify-content:center}
.submodes .chip{font-size:13px; padding:7px 12px}
.submodes .chip[aria-checked="true"]{background:var(--you); color:var(--on-you); border-color:transparent}
.modehint{margin:0; font-size:14px; line-height:1.45; font-weight:600; text-align:center; color:var(--ink); background:var(--given); border-radius:12px; padding:8px 10px}
.keys.ops{grid-template-columns:repeat(5, 1fr)}
@media (max-width:480px){ .keys{grid-template-columns:repeat(6, 1fr)} .key{height:40px} }
@media (max-height:640px){ .key{height:36px; font-size:19px} }
.chip{padding:6px 10px; border-radius:999px; border:1px solid var(--panel-line); background:var(--panel); font-size:12px; font-weight:800}
.chip.undo{background:var(--ink); color:var(--paper); border-color:transparent}
.chip.view{background:var(--tile); box-shadow:0 2px 0 var(--tile-edge)}

/* FX */
#fx{position:fixed; inset:0; pointer-events:none; z-index:50; overflow:hidden}
.spark{position:fixed; left:0; top:0; width:7px; height:7px; margin:-3.5px 0 0 -3.5px; border-radius:2px; background:var(--c); transform:translate(var(--x0),var(--y0)); animation:spark .75s cubic-bezier(.15,.7,.3,1) forwards}
@keyframes spark{to{transform:translate(calc(var(--x0) + var(--dx)),calc(var(--y0) + var(--dy))) rotate(220deg) scale(.2); opacity:0}}
.ringfx{position:fixed; left:var(--x); top:var(--y); width:18px; height:18px; margin:-9px 0 0 -9px; border-radius:50%; border:3px solid var(--c); animation:ringfx .55s ease-out forwards}
@keyframes ringfx{to{transform:scale(4); opacity:0}}
.floater{position:fixed; left:var(--x); top:var(--y); transform:translate(-50%,-50%); font-family:var(--font-display); font-weight:700; font-size:19px; color:var(--c); white-space:nowrap; text-shadow:0 2px 0 var(--paper),0 0 6px var(--paper); animation:floatUp 1.05s ease-out forwards}
@keyframes floatUp{0%{opacity:0; transform:translate(-50%,-30%) scale(.6)}18%{opacity:1; transform:translate(-50%,-60%) scale(1.15)}100%{opacity:0; transform:translate(-50%,-260%) scale(1)}}
.praise{position:fixed; left:50%; top:var(--y); transform:translate(-50%,-50%); font-family:var(--font-display); font-weight:700; font-size:clamp(34px,10vw,54px); color:var(--c); white-space:nowrap; text-shadow:0 4px 0 var(--paper),0 0 14px var(--paper); animation:praise 1.1s cubic-bezier(.3,1.5,.5,1) forwards}
@keyframes praise{0%{opacity:0; transform:translate(-50%,-50%) scale(.3) rotate(-8deg)}25%{opacity:1; transform:translate(-50%,-50%) scale(1.1) rotate(2deg)}70%{opacity:1; transform:translate(-50%,-60%) scale(1)}100%{opacity:0; transform:translate(-50%,-90%) scale(.95)}}
.confetti{position:fixed; inset:0; width:100%; height:100%; pointer-events:none; z-index:60}

.overlay{position:fixed; inset:0; z-index:40; display:grid; place-items:center; background:var(--scrim); backdrop-filter:blur(5px); overflow:auto;
  padding-left:max(16px, env(safe-area-inset-left, 0px)); padding-right:max(16px, env(safe-area-inset-right, 0px)); padding-block:max(16px, env(safe-area-inset-top, 0px)) max(16px, env(safe-area-inset-bottom, 0px))}
.card{width:100%; max-width:420px; box-sizing:border-box; padding:24px 22px; border-radius:22px; background:var(--tile); border:1px solid var(--panel-line); box-shadow:0 8px 0 var(--tile-edge),0 24px 50px rgba(0,0,0,.18); display:flex; flex-direction:column; gap:14px; animation:cardIn .4s cubic-bezier(.3,1.4,.5,1)}
@keyframes cardIn{0%{transform:translateY(20px) scale(.95); opacity:0}}
h1,h2{margin:0; font-family:var(--font-display); font-weight:700; line-height:1.05; text-wrap:balance}
h1{font-size:40px} h2{font-size:30px}
.lede{margin:0; font-size:15px; line-height:1.45; color:var(--muted); font-weight:600}
.rules{margin:0; padding-left:20px; display:flex; flex-direction:column; gap:7px; font-size:14px; line-height:1.4}
.k-good{color:var(--good); font-weight:800} .k-bad{color:var(--bad); font-weight:800} .k-warn{color:var(--warn-edge); font-weight:800}
.btn{border:0; border-radius:14px; padding:14px 18px; font-family:var(--font-display); font-weight:600; font-size:20px; background:var(--you); color:var(--on-you); box-shadow:0 5px 0 var(--you-edge)}
.btn:active{transform:translateY(3px); box-shadow:0 2px 0 var(--you-edge)}
.btn.ghost{background:var(--given); color:var(--ink); box-shadow:0 5px 0 var(--tile-edge); font-size:17px}
.btns{display:flex; flex-direction:column; gap:10px}
.tally{display:flex; flex-direction:column; gap:6px}
.tally div{display:flex; justify-content:space-between; align-items:baseline; font-weight:700; font-size:15px; padding:6px 0; border-bottom:1px solid var(--panel-line)}
.tally b{font-family:var(--font-display); font-size:20px; font-variant-numeric:tabular-nums; color:var(--you)}
.practice{margin:0; padding:8px 12px; border-radius:12px; background:var(--bad-soft); color:var(--ink); text-align:center; font-family:var(--font-display); font-weight:500; font-size:15px}
.balls-demo{display:flex; justify-content:center; gap:8px}
.balls-demo .ball{position:static; margin:0; --B:40px; animation:demo 2.4s ease-in-out infinite}
.balls-demo .ball:nth-child(2){animation-delay:.2s} .balls-demo .ball:nth-child(3){animation-delay:.4s} .balls-demo .ball:nth-child(4){animation-delay:.6s}
@keyframes demo{0%,100%{transform:translateY(0)}20%{transform:translateY(-8px) rotate(-10deg)}40%{transform:translateY(0)}}
@media (prefers-reduced-motion: reduce){ .cell.jelly,.cell.solidify,.cell.shake,.cell.rewind,.cell.wave,.cell.intro,.cell.picked,.card,.status.pop,.face.flash,.ball.pop span,.balls-demo .ball{animation:none!important} .cell.slot.loose{transform:none} }

/* ---------- Grade + topic picker ---------- */
.card.start{max-width:460px; gap:12px}
.grades{display:flex; flex-wrap:wrap; gap:6px; justify-content:center}
.grade{min-width:40px; padding:7px 10px; border-radius:999px; border:2px solid var(--panel-line); background:var(--given); font:600 15px/1 var(--font-display); color:var(--ink)}
.grade[aria-selected="true"]{background:var(--you); border-color:var(--you-edge); color:var(--on-you)}
.topics-head{font-size:11px; font-weight:800; letter-spacing:.08em; text-transform:uppercase; color:var(--muted); text-align:center; margin-bottom:-4px}
.topics{display:grid; grid-template-columns:repeat(auto-fill, minmax(128px, 1fr)); gap:8px}
.topic{display:flex; flex-direction:column; align-items:flex-start; gap:2px; padding:9px 11px; border-radius:14px; border:2px solid var(--panel-line); background:var(--given); box-shadow:0 3px 0 var(--tile-edge); text-align:left; min-width:0}
.topic b{font-family:var(--font-display); font-weight:600; font-size:16px; line-height:1.1}
.topic span{font-size:12.5px; font-weight:700; color:var(--muted); white-space:nowrap; overflow:hidden; text-overflow:ellipsis; max-width:100%}
.topic[aria-checked="true"]{border-color:var(--you); background:var(--you-soft); box-shadow:0 3px 0 var(--you)}
.diffs{display:flex; justify-content:center; align-items:center; gap:6px}
.diffs .lab{font-size:11px; font-weight:800; letter-spacing:.08em; text-transform:uppercase; color:var(--muted); margin-right:2px}
.diff{min-width:64px; min-height:36px; padding:6px 12px; border-radius:999px; border:2px solid var(--panel-line); background:var(--given); font:600 14px/1 var(--font-display); color:var(--ink)}
.diff[aria-checked="true"]{background:var(--you); border-color:var(--you-edge); color:var(--on-you)}
/* Rule pill: the box's banner (or a small "? Example" chip). Always holds its space while playing, so the cube never moves. */
.rulebar{align-self:center; max-width:100%; min-height:32px; box-sizing:border-box; display:flex; align-items:center; gap:7px; padding:3px 12px 3px 4px; border-radius:999px;
  background:var(--given); border:2px solid var(--panel-line); font:600 14px/1.2 var(--font-display); color:var(--ink); cursor:pointer;
  box-shadow:0 2px 0 var(--tile-edge); transition:transform .15s cubic-bezier(.3,1.6,.5,1), border-color .2s, opacity .2s; touch-action:manipulation}
.rulebar .rt{min-width:0; white-space:nowrap; overflow:hidden; text-overflow:ellipsis}
.rulebar .rq{flex:none; width:22px; height:22px; border-radius:50%; display:grid; place-items:center; background:var(--you); color:var(--on-you); font:700 14px/1 var(--font-display)}
.rulebar:hover{border-color:var(--you)}
.rulebar:active{transform:translateY(2px) scale(.97); box-shadow:0 0 0 var(--tile-edge)}
.rulebar[aria-expanded="true"]{border-color:var(--you); background:var(--you-soft)}
.rulebar:focus-visible{outline:3px solid var(--you); outline-offset:2px}
.rulebar.plain{padding-right:10px; font:800 11px/1 var(--font-body); letter-spacing:.08em; text-transform:uppercase; color:var(--muted); background:var(--panel)}
.rulebar.plain .rq{width:20px; height:20px; font-size:13px; letter-spacing:0; background:var(--given); color:var(--you); box-shadow:inset 0 0 0 2px var(--you)}
.rulebar.off{visibility:hidden; pointer-events:none}
.rulebar[hidden]{display:none}
/* Worked-example sheet: floats over the stage under the pill; tap outside, ✕ or Esc to close. */
.ex-scrim{position:fixed; inset:0; z-index:30; background:color-mix(in srgb, var(--paper) 35%, transparent)}
.ex-sheet{position:fixed; z-index:31; left:50%; width:min(420px, calc(100vw - 32px)); max-height:min(60vh, 340px); box-sizing:border-box; transform:translateX(-50%);
  padding:12px 14px 14px; border-radius:18px; background:var(--tile); border:1px solid var(--panel-line); box-shadow:0 6px 0 var(--tile-edge), 0 18px 40px rgba(0,0,0,.22);
  display:flex; flex-direction:column; gap:8px; transform-origin:50% -10px}
.ex-sheet::before{content:''; position:absolute; top:-7px; left:var(--axo, 50%); width:12px; height:12px; margin-left:-6px; background:var(--tile); border-left:1px solid var(--panel-line); border-top:1px solid var(--panel-line); transform:rotate(45deg)}
.ex-sheet.in{animation:exIn .34s cubic-bezier(.3,1.5,.5,1) both}
.ex-sheet.out{animation:exOut .16s ease-in both}
@keyframes exIn{0%{opacity:0; transform:translateX(-50%) translateY(-10px) scale(.86)}}
@keyframes exOut{to{opacity:0; transform:translateX(-50%) translateY(-6px) scale(.94)}}
.ex-head{display:grid; grid-template-columns:1fr auto; align-items:center; column-gap:8px}
.ex-kicker{grid-column:1; font-size:10px; font-weight:800; letter-spacing:.1em; text-transform:uppercase; color:var(--you)}
.ex-head h3{grid-column:1; margin:0; font:700 20px/1.1 var(--font-display)}
.ex-close{grid-column:2; grid-row:1 / span 2; width:40px; height:40px; border-radius:50%; border:0; background:var(--given); font:700 16px/1 var(--font-body); color:var(--muted); padding:0}
.ex-close:focus-visible{outline:3px solid var(--you); outline-offset:2px}
.ex-rule{margin:0; padding:6px 10px; border-radius:10px; background:var(--you-soft); color:var(--ink); font:600 14px/1.3 var(--font-display)}
.ex-lines{margin:0; padding:0; list-style:none; counter-reset:ex; display:flex; flex-direction:column; gap:6px}
.ex-lines li{counter-increment:ex; display:grid; grid-template-columns:22px 1fr; gap:8px; align-items:start; font:500 16px/1.35 var(--font-display); color:var(--ink); overflow-wrap:anywhere}
.ex-lines li::before{content:counter(ex); width:22px; height:22px; border-radius:50%; display:grid; place-items:center; background:var(--given); color:var(--muted); font:700 12px/1 var(--font-body); margin-top:1px}
.ex-sheet.in .ex-lines li{animation:exLine .4s cubic-bezier(.3,1.5,.5,1) both; animation-delay:calc(120ms + var(--i, 0) * 110ms)}
@keyframes exLine{0%{opacity:0; transform:translateX(-12px)}}

/* Picture tiles (K–2): ten-frames, base-ten blocks, coins. The number stays on top, small. */
.cell .val.pic{position:absolute; inset:7% 7% 8%; display:flex; flex-direction:column; align-items:center; justify-content:center; gap:4%; font-size:1em}
.val.pic .pnum,.key.pic .pnum{flex:none; font:700 calc(var(--cell) * .24)/1 var(--font-display); letter-spacing:0; white-space:nowrap}
.val.pic .pic,.key.pic .pic{flex:1 1 0; min-height:0; width:100%; height:100%; overflow:visible; display:block}
.key.pic{display:flex; flex-direction:column; align-items:center; justify-content:center; gap:3px; padding:5px 4px 6px; height:58px}
.key.pic .pnum{font-size:14px}
.tf-box{fill:var(--tile); stroke:var(--slot-line); stroke-width:2}
.tf-line{fill:none; stroke:var(--slot-line); stroke-width:1.4}
.tf-dot{fill:var(--r0)} .tf-dot.b{fill:var(--r5)}
.b10{fill:var(--r5); stroke:color-mix(in srgb, var(--r5) 55%, #000); stroke-width:.14}
.b10.u{fill:var(--r1); stroke:color-mix(in srgb, var(--r1) 55%, #000)}
.b10-grid{fill:none; stroke:color-mix(in srgb, var(--r5) 50%, #000); stroke-width:.07; opacity:.55}
.cell.slot.locked .b10{fill:var(--on-you); stroke:var(--you-edge)} .cell.slot.locked .b10-grid{stroke:var(--you-edge)}
.cell.slot.stuck .b10{stroke:color-mix(in srgb, var(--r5) 30%, #000)}
.coin circle{fill:#d5dbe2; stroke:#7b8693; stroke-width:1.1} .coin .rim{fill:none; stroke:rgba(40,50,64,.22); stroke-width:.8}
.coin.cu circle{fill:#d98a56; stroke:#8a4a22} .coin.cu .rim{fill:none; stroke:rgba(90,40,10,.28)}
.coin text{fill:#26303d; font-family:var(--font-display); font-weight:700; letter-spacing:-.04em}
.coin.cu text{fill:#3e1f0b}
/* Pictures switch on the start card */
.picsrow{display:flex; justify-content:center; align-items:center; gap:8px; margin-top:-4px}
.picsrow .lab{font-size:11px; font-weight:800; letter-spacing:.08em; text-transform:uppercase; color:var(--muted)}
.pictoggle{display:flex; align-items:center; gap:7px; min-height:36px; padding:4px 12px 4px 4px; border-radius:999px; border:2px solid var(--panel-line); background:var(--given); font:600 14px/1 var(--font-display); color:var(--ink)}
.pictoggle .knob{width:40px; height:24px; border-radius:999px; background:var(--slot-line); position:relative; transition:background .2s}
.pictoggle .knob::after{content:''; position:absolute; top:3px; left:3px; width:18px; height:18px; border-radius:50%; background:#fff; box-shadow:0 1px 3px rgba(0,0,0,.3); transition:transform .25s cubic-bezier(.3,1.6,.5,1)}
.pictoggle[aria-checked="true"] .knob{background:var(--good)}
.pictoggle[aria-checked="true"] .knob::after{transform:translateX(16px)}
.pictoggle:focus-visible{outline:3px solid var(--you); outline-offset:2px}
@media (prefers-reduced-motion: reduce){ .ex-sheet.in,.ex-sheet.out,.ex-sheet.in .ex-lines li{animation:none!important} }
.diff:focus-visible,.grade:focus-visible,.topic:focus-visible{outline:3px solid var(--you); outline-offset:2px}
.how summary{cursor:pointer; font-weight:800; font-size:14px; color:var(--muted)}
.how[open]{display:flex; flex-direction:column; gap:10px}
.btn[disabled]{opacity:.55; pointer-events:none}
/* Longer labels (fractions, decimals, surds) */
.cell.xlong .val{font-size:.5em; letter-spacing:-.03em; white-space:nowrap}
.key.xlong{font-size:14px; letter-spacing:-.02em}
.ball.xlong{width:calc(var(--B) * 1.45); height:calc(var(--B) * 1.45); margin:calc(var(--B) * -.725) 0 0 calc(var(--B) * -.725)}
.ball.xlong span{font-size:.78em; letter-spacing:-.05em}
.lvl{max-width:52vw; overflow:hidden; text-overflow:ellipsis}

/* Screen layout: a compact top bar, and the stage takes everything else. The pad only appears as a sheet while a
   square or blank ball is selected (bottom edge in portrait, right edge on landscape screens); the box moves out of
   its way (syncSheet in game.js). */
#app{max-width:none; gap:4px}
.topbar{flex:none; display:flex; flex-direction:column; gap:4px; min-width:0}
.hud{flex-wrap:nowrap; gap:6px; min-width:0; min-height:36px}
.lvl{flex:0 1 auto; min-width:0; max-width:none; margin-right:auto; font-size:14px; padding:4px 10px; text-overflow:ellipsis}
.stat{flex:none; padding:3px 8px; border-radius:10px; align-items:center; gap:4px}
.stat b{font-size:16px}
.stat .lab{display:none}
#scoreBox::before{content:'★'; font-size:12px; line-height:1; color:var(--you)}
.iconbtn{flex:none; width:36px; height:36px; padding:0; border-radius:50%; border:1px solid var(--panel-line); background:var(--tile); font:700 17px/1 var(--font-display); color:var(--ink); touch-action:manipulation}
.iconbtn.undo{background:var(--ink); color:var(--paper); border-color:transparent}
.iconbtn:active{transform:translateY(1px)}
.rulebar{min-height:28px; font-size:13px}
@media (max-width:520px){ .lvl .lg{display:none} .stat{padding:3px 6px} .iconbtn{width:34px; height:34px} }
@media (min-width:720px){ .stat .lab{display:inline} #scoreBox::before{display:none} }
/* The face name sits in a corner: the edge tracks are too thin to hold it now. */
.fname{position:absolute; left:calc(var(--cell) * .1); top:calc(var(--cell) * .06); margin:0; font-size:calc(var(--cell) * .14); letter-spacing:.04em; pointer-events:none}
/* Big portrait screens: one row, title, stats and buttons, then the rule pill. */
@media (min-width:900px) and (max-aspect-ratio: 124/100){
  .topbar{flex-direction:row; align-items:center; gap:10px}
  .hud{flex:0 1 auto}
  .rulewrap{flex:0 1 auto; margin-left:auto}
  .status{left:auto; width:max-content; max-width:60vw}
}
/* Landscape: height is what limits the box and width is spare, so all of this stands in a narrow rail and the stage gets the full height. */
@media (min-aspect-ratio: 5/4){
  #app{flex-direction:row; gap:8px; padding-left:max(8px, env(safe-area-inset-left, 0px))}
  .topbar{width:clamp(124px, 15vw, 200px); gap:6px; overflow:hidden}
  .hud{flex-wrap:wrap; align-content:flex-start}
  .lvl{flex:1 1 100%; margin:0; white-space:normal; border-radius:12px}
  .lvl .lg{display:inline}
  .stat{flex:1 1 100%; justify-content:space-between; box-sizing:border-box}
  .stat .lab{display:inline}
  #scoreBox::before{display:none}
  .rulewrap{display:block}
  .rulebar{width:100%; border-radius:14px; align-items:flex-start; padding:5px 8px 5px 5px; font-size:12px}
  .rulebar .rt{white-space:normal}
  .status{top:0; bottom:auto; padding:5px 8px; font-size:12px; -webkit-line-clamp:7}   /* the rail is narrow, with room below */
  .stage{flex:1 1 0; min-width:0; min-height:0; margin-left:0; overflow:visible}   /* a selected ball's ring may reach past the stage */
}
/* Messages cover the rule pill for a moment, so they never take room from the box or sit on its balls.
   Taller ones grow upward over the title, away from the box. Taps pass through to the pill. */
.topbar{position:relative; z-index:6}
.rulewrap{position:relative; flex:none; display:flex; justify-content:center; min-width:0}
.status{position:absolute; left:0; right:0; bottom:-3px; z-index:5; min-height:calc(100% + 3px); margin:0; box-sizing:border-box; padding:4px 12px; border-radius:12px; text-align:center;
  background:var(--paper); border:1px solid var(--panel-line); box-shadow:0 3px 10px rgba(0,0,0,.14); font-size:13px; color:var(--ink); pointer-events:none; opacity:0; transition:opacity .25s;
  display:-webkit-box; -webkit-line-clamp:2; -webkit-box-orient:vertical; overflow:hidden; line-height:1.3}
.status.on{opacity:1}
.combo{position:absolute; right:10px; top:6px; z-index:5; margin:0; pointer-events:none}
/* The box slides by this much while the pad is open (a separate property, so it never fights the 3D transform). */
.cubewrap{transition:translate .24s cubic-bezier(.2,.9,.3,1)}
/* The pad sheet */
.pad{position:fixed; z-index:20; left:0; right:0; bottom:0; max-width:760px; margin:0 auto; box-sizing:border-box; max-height:60dvh; overflow-y:auto; gap:5px;
  padding:6px 12px max(12px, env(safe-area-inset-bottom, 0px)); border-radius:18px 18px 0 0; border-width:1px 1px 0; background:var(--paper); box-shadow:0 -8px 24px rgba(0,0,0,.18);
  transform:translateY(105%); visibility:hidden; transition:transform .24s cubic-bezier(.2,.9,.3,1), visibility 0s .24s}
.pad.open{transform:none; visibility:visible; transition:transform .24s cubic-bezier(.2,.9,.3,1), visibility 0s}
.pad-close{flex:none; width:30px; height:30px; padding:0; border-radius:50%; border:1px solid var(--panel-line); background:var(--tile); font:700 13px/1 var(--font-body); color:var(--muted)}
@media (min-aspect-ratio: 5/4){
  .pad{left:auto; right:8px; bottom:8px; width:clamp(280px, 32vw, 400px); max-width:none; max-height:calc(100dvh - 60px); margin:0; padding:6px 10px 10px; border-radius:18px; border-width:1px; box-shadow:-6px 6px 24px rgba(0,0,0,.18); transform:translateX(110%)}
}
@media (min-aspect-ratio: 5/4) and (max-height:500px){
  .pad{padding:4px 8px 8px; gap:4px} .pad-close{width:26px; height:26px}
  .key{height:34px; font-size:18px} .key.pic{height:46px}
}
@media (prefers-reduced-motion: reduce){ .pad,.pad.open,.cubewrap{transition:none} }
