/* Abassign. No continuous decorative animation: everything that moves responds to a gesture or to the program. */

:root {
  --bg-top: #e9efff;
  --bg-bottom: #fff4ea;
  --ink: #26224f;
  --ink-2: #4b4878;
  --ink-3: #8e8bb4;
  --line: #e2e4f4;
  --lip: #d6d9ef;
  --panel: #f5f6fd;
  --ok: #1fb466;
  --ok-ink: #0e9152;
  --bad-ink: #d42c43;
  --danger: #ff3b4e;
  --focus: #5867ff;
  --check: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M4.5 12.8l4.8 4.7L19.5 7' fill='none' stroke='%23000' stroke-width='3.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  --arrow: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 2.6l8.6 9.6h-5.3v9.2H8.7v-9.2H3.4z' stroke='%23000' stroke-width='1.6' stroke-linejoin='round'/%3E%3C/svg%3E");
  --font-display: "Baloo 2", "Nunito", "Trebuchet MS", system-ui, sans-serif;
  --font-body: "Nunito", "Segoe UI", system-ui, sans-serif;
  --font-code: "Recursive", ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  color-scheme: light;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  color: var(--ink);
  font-family: var(--font-body);
  background:
    radial-gradient(38vmax 32vmax at 4% 6%, rgba(255, 196, 20, .30), transparent 70%),
    radial-gradient(34vmax 30vmax at 98% 4%, rgba(163, 91, 242, .22), transparent 70%),
    radial-gradient(40vmax 34vmax at 100% 100%, rgba(58, 141, 255, .22), transparent 70%),
    radial-gradient(38vmax 32vmax at 0% 100%, rgba(41, 196, 106, .22), transparent 70%),
    radial-gradient(30vmax 20vmax at 52% 106%, rgba(255, 79, 79, .14), transparent 70%),
    linear-gradient(180deg, var(--bg-top), var(--bg-bottom));
  background-attachment: fixed;
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
/* Dot-grid paper */
.backdrop {
  position: fixed; inset: 0; z-index: -1;
  background-image: radial-gradient(circle, rgba(38, 34, 79, .11) 1.25px, transparent 1.7px);
  background-size: 26px 26px;
  background-position: 13px 13px;
  pointer-events: none;
}
button { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 3px; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

.app {
  height: 100%;
  height: 100dvh;
  min-height: 540px;
  max-width: 1240px;
  margin: 0 auto;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 12px;
  padding: max(14px, env(safe-area-inset-top)) max(20px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(20px, env(safe-area-inset-left));
}

/* nsilab footer: under the game on desktop; on phones, below the screen, reached by scrolling. */
.footer {
  text-align: center;
  font: 600 15px/1.4 var(--font-body);
  color: var(--ink-2);
}
.footer a { color: inherit; font-weight: 800; text-decoration: underline; text-underline-offset: 3px; }
.footer img { height: 2.5em; vertical-align: middle; margin-right: .6em; }
.footer-in { margin: 0; }
/* Same columns as the play area: centered under the abacus. */
.app > .footer { display: grid; grid-template-columns: var(--play-cols); justify-content: center; column-gap: var(--play-gap); }
.app > .footer .footer-in { grid-column: 1; }
.footer-below { display: none; }
html.lock, html.lock body { overflow: hidden; }   /* no page scroll during a game */

/* ---------- Top bar ---------- */
.bar { display: grid; grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr); align-items: center; gap: 16px; }
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand .logo { display: flex; flex: none; border-radius: 6px; }
.mark { width: 40px; height: 40px; flex: none; filter: drop-shadow(0 3px 0 rgba(28, 25, 70, .22)); }
.wordmark { font: 800 31px/1 var(--font-display); letter-spacing: -.015em; color: var(--ink); padding-top: .1em; text-shadow: 0 3px 0 #d6d9ef; }
.wordmark .i { position: relative; }
.wordmark .i::after {
  content: "";
  position: absolute;
  left: 50%; top: .29em;
  width: .25em; height: .25em; margin-left: -.12em;   /* centered on the ı stem */
  border-radius: 50%;
  background:
    radial-gradient(circle at 34% 30%, rgba(255, 255, 255, .95) 0 12%, rgba(255, 255, 255, 0) 26%),
    radial-gradient(circle at 42% 38%, #fff4c4 0%, #ffc414 48%, #e48a00 100%);
  box-shadow: inset 0 0 0 .025em rgba(166, 92, 0, .5);
}

.levels { justify-self: center; display: grid; justify-items: center; gap: 7px; }
/* Wide screens: levels centered on the abacus, tools on the program's left edge (set in JS). */
.bar.aligned { display: block; position: relative; }
.bar.aligned .brand { height: 100%; position: absolute; left: 0; top: 0; }
.bar.aligned .levels, .bar.aligned .tools { position: absolute; top: 50%; translate: 0 -50%; }
.bar.aligned .levels { justify-items: center; }
.wire {
  /* two rows of 8 (one row on phones in landscape) */
  display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 12px 8px;
  max-width: calc(8 * 30px + 7 * 8px + 24px);
  padding: 7px 12px 12px;
  border-radius: 22px;
  background: rgba(255, 255, 255, .8);
  box-shadow: inset 0 0 0 2px var(--line), 0 4px 0 var(--lip);
}
.lvl {
  --sh: none;
  position: relative;
  width: 30px; height: 30px; padding: 2px 0 0;
  display: grid; place-items: center;
  border: 0; border-radius: 50%;
  font: 800 14px/1 var(--font-display);
  color: var(--ink);
  cursor: pointer;
  box-shadow: var(--sh);
  transition: transform .22s cubic-bezier(.3, 1.5, .5, 1), box-shadow .2s;
}
.lvl[data-state="open"] { --sh: inset 0 0 0 3px var(--c); background: #fff; }
.lvl[data-state="done"] {
  --sh: inset 0 0 0 1.5px var(--edge-a), 0 2px 0 var(--edge-a);
  background:
    radial-gradient(circle at 33% 27%, rgba(255, 255, 255, .9) 0 9%, rgba(255, 255, 255, 0) 24%),
    radial-gradient(circle at 42% 38%, var(--hi) 0%, var(--c) 50%, var(--lo) 100%);
}
/* Locked: very pale color and dotted outline, as in Print.py */
.lvl[data-state="locked"] {
  border: 2px dashed color-mix(in srgb, var(--c) 55%, #fff);
  background: color-mix(in srgb, var(--c) 9%, #fff);
  color: color-mix(in srgb, var(--lo) 50%, #fff);
  cursor: not-allowed;
}
.lvl:not([data-state="locked"]):hover { transform: scale(1.14); }
/* Current level: underlined like an active tab; the ball keeps its state */
.lvl.current::after {
  content: "";
  position: absolute; left: 50%; bottom: -7px;
  width: 16px; height: 3.5px; margin-left: -8px;
  border-radius: 2px;
  background: var(--ink);
}
.caption { margin: 0; font: 700 17px/1.2 var(--font-display); color: var(--ink-2); padding-top: 2px; }
.caption .code { font: 800 .95em var(--font-code); }

.tools { justify-self: end; display: flex; align-items: center; gap: 6px; }
.tool {
  display: inline-flex; align-items: center; gap: 7px;
  height: 42px; padding: 0 10px 0 9px;
  border: 0; border-radius: 14px;
  background: #fff;
  font: 800 15px var(--font-display);
  color: var(--ink);
  cursor: pointer;
  box-shadow: inset 0 0 0 2px var(--line), 0 4px 0 var(--lip);
  transition: transform .1s, box-shadow .1s, opacity .2s;
}
.tool-label { padding-top: 2px; }
.tool:hover { background: #fbfbff; }
.tool:active { transform: translateY(3px); box-shadow: inset 0 0 0 2px var(--line), 0 1px 0 var(--lip); }
.tool .ico { width: 21px; height: 21px; flex: none; }
.tool kbd, .btn kbd {
  font: 800 11px/1 var(--font-code);
  padding: 4px 6px 3px;
  border-radius: 7px;
}
.tool kbd { color: var(--ink-3); background: var(--panel); box-shadow: inset 0 0 0 1.5px var(--line); }
/* Mode switch: shows only the current mode */
#btn-py:not(.on) .ico-py, #btn-py:not(.on) .lab-py, #btn-py.on .ico-tile, #btn-py.on .lab-tile { display: none; }
#btn-sound .ico-off { display: none; }
#btn-sound.muted { color: var(--ink-3); }
#btn-sound.muted .ico-on { display: none; }
#btn-sound.muted .ico-off { display: block; }
body:not([data-mode="play"]):not([data-mode="pause"]) #btn-pause { opacity: .45; pointer-events: none; }

/* ---------- Board ---------- */
:root { --play-cols: minmax(0, 600px) clamp(300px, 30vw, 380px); --play-gap: clamp(20px, 3vw, 44px); }
.play {
  display: grid;
  grid-template-columns: var(--play-cols);
  justify-content: center;
  gap: var(--play-gap);
  min-height: 0;
}
.stage {
  position: relative;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  justify-items: center;
  gap: 6px;
  min-width: 0; min-height: 0;
}
.stage::before {
  content: "";
  position: absolute;
  inset: 2% 6% 26%;
  background: radial-gradient(closest-side, rgba(255, 255, 255, .85), rgba(255, 255, 255, 0));
  pointer-events: none;
  z-index: -1;
}
#abacus {
  display: block;
  touch-action: none;               /* swipes drive the game, not the page */
  -webkit-user-select: none; user-select: none;
  width: 100%; height: 100%;
  min-height: 0;
  overflow: visible;
  animation: rise-in .6s cubic-bezier(.2, .9, .3, 1) both;
}
@keyframes rise-in { from { opacity: 0; transform: translateY(12px); } }

/* Abacus SVG */
.val-num {
  font: 800 42px var(--font-display);
  text-anchor: middle;
  dominant-baseline: central;
  paint-order: stroke;
  stroke-linejoin: round;
}
.val-glow { opacity: 0; transition: opacity .2s; }
.val.on .val-glow { opacity: 1; }
.lane-on { opacity: 0; transition: opacity .18s; }
.lane-on.on { opacity: 1; }
.ruler { font: 700 14px var(--font-display); text-anchor: middle; dominant-baseline: central; fill: #a3a1c6; }
.ruler.five { fill: var(--ink-2); font-weight: 800; }
.ruler.zero { fill: #fff; font-weight: 800; font-size: 15px; }
.cursor-move { transition: transform .2s cubic-bezier(.3, 1.35, .5, 1); }

.status {
  margin: 0;
  width: 100%;
  min-height: 62px;
  display: grid; place-items: center;
}
.status .msg {
  display: inline-block;
  max-width: min(100%, 56ch);
  padding: 8px 16px 6px;
  border-radius: 16px;
  font: 700 16px/1.35 var(--font-display);
  color: var(--ink-2);
  text-align: center;
  text-wrap: balance;
  background: #fff;
  box-shadow: inset 0 0 0 2px var(--line), 0 4px 0 var(--lip);
}
.status .msg.ok { color: var(--ok-ink); background: #effcf5; box-shadow: inset 0 0 0 2px #bfeed2, 0 4px 0 #c9ecd8; }
.status .msg.bad { color: var(--bad-ink); background: #fff2f3; box-shadow: inset 0 0 0 2px #ffcbd1, 0 4px 0 #f6d0d5; }
.status .hint { margin-top: 8px; font: 700 13px/1.2 var(--font-display); color: var(--ink-3); }
.status .code { font: 800 .95em var(--font-code); color: var(--ink); white-space: nowrap; }

/* ---------- Program window ---------- */
.prog {
  --d: 0;
  align-self: stretch;              /* abacus height (margins set in JS) */
  position: relative;
  min-width: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  background: #fff;
  border-radius: 26px;
  overflow: hidden;
  box-shadow: 0 0 0 2px var(--line), 0 7px 0 var(--lip), 0 28px 44px -18px rgba(38, 34, 79, .3);
}
.prog-head {
  position: relative;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 12px 16px 10px 14px;
  border-bottom: 2px solid var(--line);
}
.prog-title {
  display: flex; align-items: center; gap: 8px;
  font: 800 15px/1 var(--font-display); letter-spacing: .07em; text-transform: uppercase;
  color: var(--ink);
  padding-top: 2px;
}
/* Python mode: the window is titled programme.py */
.name-py { display: none; font: 800 1.05em/1 var(--font-code); letter-spacing: 0; text-transform: none; }
.show-py .name-plain { display: none; }
.show-py .name-py { display: inline; }
.prog-ico { width: 22px; height: 22px; color: var(--ink); margin-top: -2px; }
.pips { display: flex; gap: 5px; }
.pip { width: 9px; height: 9px; border-radius: 50%; background: #e3e5f3; transition: background .25s, transform .25s; }
.pip.done { background: var(--ok); }
.pip.cur { background: var(--ink); transform: scale(1.3); }
.head-flash { position: absolute; inset: 0; opacity: 0; pointer-events: none; }
.head-flash.ok { background: rgba(41, 196, 106, .2); }
.head-flash.bad { background: rgba(255, 59, 78, .22); }

.tape {
  --slot: 60px;
  container-type: inline-size;      /* Python text size follows the window width */
  position: relative;
  min-height: 0;                    /* height comes from the column; always 6 slots */
  overflow: hidden;
  background-color: var(--panel);
  background-image: radial-gradient(circle, rgba(38, 34, 79, .09) 1.1px, transparent 1.5px);
  background-size: 20px 20px;
  background-position: 10px 10px;
}
.tape::before {
  content: "";
  position: absolute; inset: auto 0 0; height: calc(var(--slot) * .5);
  background: linear-gradient(to top, var(--panel), rgba(245, 246, 253, 0));
  z-index: 4; pointer-events: none;
}
.lines { position: absolute; inset: 0; z-index: 2; }
.line, .endmark {
  position: absolute;
  left: 12px; right: 12px; top: 0;
  height: calc(var(--slot) * .8);
  margin-top: calc(var(--slot) * .1);
  will-change: transform;
}
.line {
  display: grid;
  grid-template-columns: 12px 1.5em minmax(0, 1fr) auto;
  align-items: center;
  column-gap: 8px;
  padding: 0 10px 0 9px;
  border-radius: 14px;
  background: #fff;
  color: var(--ink);
  box-shadow: inset 0 0 0 2px var(--line), 0 3px 0 var(--lip);
  font: 800 clamp(14px, calc(var(--slot) * .3), 26px)/1 var(--font-code);
  transition: background .25s, box-shadow .25s;
}
.line .arrow { width: 0; height: 0; border-left: 10px solid transparent; border-top: 7px solid transparent; border-bottom: 7px solid transparent; border-radius: 2px; }
.line .no { font: 800 .62em/1 var(--font-display); color: var(--ink-3); text-align: right; font-variant-numeric: tabular-nums; padding-top: .1em; }
.line .code { white-space: nowrap; overflow: hidden; padding: .1em 0; }
.line .tile { width: .98em; height: .98em; vertical-align: -.17em; }
.line.cur {
  box-shadow: inset 0 0 0 3px var(--ink), 0 4px 0 var(--ink);
}
.line.cur .arrow { border-left-color: color-mix(in srgb, var(--danger) calc(var(--d) * 100%), var(--ink)); }
.line.done { background: #eefbf4; box-shadow: inset 0 0 0 2px #c3ecd5, 0 3px 0 #cdeadb; }
/* Python mode: each variable spelled out in its color, outlined like the abacus digits */
.pn { display: none; color: var(--c); -webkit-text-stroke: .045em var(--lo); paint-order: stroke fill; }
.show-py .pn { display: inline; }
.show-py .code .tile, .show-py .ex-code .tile { display: none; }
.show-py .line { font-size: clamp(13px, min(calc(var(--slot) * .3), calc((100cqw - 112px) / var(--py-em, 13))), 26px); }
.line.done .no, .line.done .code { opacity: .5; }
.line .stamp {
  position: relative;
  width: clamp(20px, calc(var(--slot) * .42), 30px);
  height: clamp(20px, calc(var(--slot) * .42), 30px);
  display: grid; place-items: center;
  border-radius: 50%;
  background: var(--ok);
  color: #fff;
  font: 800 clamp(12px, calc(var(--slot) * .24), 17px)/1 var(--font-display);
  box-shadow: 0 2px 0 var(--ok-ink);
  opacity: 0;
  transform: scale(0);
  font-size: 0;
}
.line .stamp::before {
  content: "";
  position: absolute; inset: 21%;
  background: #fff;
  -webkit-mask: var(--check) center / contain no-repeat;
  mask: var(--check) center / contain no-repeat;
}
.line.done .stamp { animation: stamp-in .45s cubic-bezier(.2, 1.7, .4, 1) forwards; }
@keyframes stamp-in {
  from { opacity: 0; transform: scale(0) rotate(-40deg); }
  to { opacity: 1; transform: scale(1) rotate(0deg); }
}
.endmark {
  display: flex; align-items: center; gap: 10px;
  font: 800 12px/1 var(--font-display); letter-spacing: .12em; text-transform: uppercase;
  color: var(--ink-3);
}
.endmark span { padding: 6px 12px 4px; border-radius: 99px; background: #fff; box-shadow: inset 0 0 0 2px var(--line); }
.endmark::before, .endmark::after { content: ""; flex: 1; border-top: 2.5px dotted #c9cbe3; }

.danger {
  position: absolute; inset: 0 0 auto 0; height: 58%;
  background: linear-gradient(to bottom, rgba(255, 59, 78, .3), rgba(255, 59, 78, 0));
  opacity: var(--d);
  z-index: 3; pointer-events: none;
}
.redline {
  position: absolute; left: 0; right: 0; top: 0; height: 5px;
  background: var(--danger);
  box-shadow: 0 0 calc(4px + var(--d) * 22px) calc(var(--d) * 6px) rgba(255, 59, 78, calc(.35 + var(--d) * .55));
  z-index: 5;
}
/* Review: a thin scrollbar over the whole program */
.scroller { position: absolute; right: 3px; top: 4px; bottom: 4px; width: 6px; border-radius: 3px; background: rgba(38, 34, 79, .07); opacity: 0; transition: opacity .2s; z-index: 6; pointer-events: none; }
.scroller span { position: absolute; left: 0; right: 0; top: 0; border-radius: 3px; background: rgba(38, 34, 79, .32); }
body[data-mode="review"] .scroller.on { opacity: 1; }
body[data-mode="review"] .tape { cursor: grab; touch-action: none; }
.prog.alarm .redline { animation: alarm .16s steps(2, jump-none) 6; }
@keyframes alarm { 50% { opacity: .15; } }

/* Screen reddens as a line nears the red line */
.screen-alarm {
  position: fixed; inset: 0; z-index: 15;
  pointer-events: none;
  opacity: 0;
  background: radial-gradient(ellipse at center, rgba(255, 59, 78, 0) 45%, rgba(255, 59, 78, .2) 100%);
  box-shadow: inset 0 0 110px rgba(255, 59, 78, .4);
}

/* ---------- Balls (units) and tiles (variables) ---------- */
.d0 { --c: #ffc414; --hi: #fff4c4; --lo: #e48a00; --edge-a: rgba(166, 92, 0, .55); }
.d1 { --c: #ff4f4f; --hi: #ffd2cd; --lo: #d1264a; --edge-a: rgba(154, 21, 53, .5); }
.d2 { --c: #29c46a; --hi: #c8f5d9; --lo: #0f915d; --edge-a: rgba(8, 104, 63, .5); }
.d3 { --c: #3a8dff; --hi: #d3e5ff; --lo: #1f57cf; --edge-a: rgba(22, 61, 152, .5); }
.d4 { --c: #a35bf2; --hi: #ecdaff; --lo: #6d2dce; --edge-a: rgba(77, 29, 152, .5); }
.dot, .tile {
  display: inline-block;
  width: .92em; height: .92em;
  border-radius: 50%;
  vertical-align: -.14em;
  background:
    radial-gradient(circle at 33% 28%, rgba(255, 255, 255, .95) 0 10%, rgba(255, 255, 255, 0) 24%),
    radial-gradient(circle at 42% 38%, var(--hi) 0%, var(--c) 48%, var(--lo) 100%);
  box-shadow: inset 0 0 0 .055em var(--edge-a);
}
.tile {
  border-radius: 24%;
  background:
    radial-gradient(circle at 30% 26%, rgba(255, 255, 255, .9) 0 9%, rgba(255, 255, 255, 0) 22%),
    radial-gradient(circle at 42% 38%, var(--hi) 0%, var(--c) 52%, var(--lo) 100%);
}

/* ---------- Arrows in texts, drawn as keys ---------- */
.keycap {
  display: inline-block;
  position: relative;
  width: 1.3em; height: 1.3em;
  margin: 0 .06em;
  vertical-align: -.32em;
  border-radius: .34em;
  background: var(--ink);
  box-shadow: 0 .1em 0 #15123a;
}
.keycap::before {
  content: "";
  position: absolute; inset: .23em;
  background: #fff;
  -webkit-mask: var(--arrow) center / contain no-repeat;
  mask: var(--arrow) center / contain no-repeat;
}
.keycap.down::before { transform: rotate(180deg); }
.keycap.left::before { transform: rotate(-90deg); }
.keycap.right::before { transform: rotate(90deg); }

/* ---------- Cards ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 20;
  display: flex;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  overflow: auto;
  background: rgba(233, 237, 255, .55);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  animation: fade-in .25s ease-out;
}
@keyframes fade-in { from { opacity: 0; } }
.card {
  position: relative;
  margin: auto;
  width: min(600px, 100%);
  display: grid; gap: 16px;
  grid-template-columns: minmax(0, 1fr);
  padding: 28px 30px 26px;
  border-radius: 30px;
  color: var(--ink);
  background: #fff;
  box-shadow: 0 0 0 2px var(--line), 0 10px 0 var(--lip), 0 44px 80px -24px rgba(38, 34, 79, .45);
  animation: card-in .5s cubic-bezier(.2, 1.4, .4, 1);
}
@keyframes card-in { from { opacity: 0; transform: translateY(26px) scale(.94); } }
/* Close (Esc): on the top edge, at the left like the key on the keyboard */
.card-close {
  position: absolute; top: -15px; left: 22px; z-index: 1;
  display: inline-flex; align-items: center; gap: 7px;
  height: 30px; min-width: 30px; padding: 0 10px 0 9px;
  border: 0; border-radius: 10px;
  background: #fff; color: var(--ink-2);
  cursor: pointer;
  box-shadow: inset 0 0 0 2px var(--line), 0 3px 0 var(--lip);
  transition: transform .1s, box-shadow .1s;
}
.card-close svg { width: 11px; height: 11px; flex: none; }
.card-close kbd { font: 800 11px/1 var(--font-code); color: var(--ink-3); padding-top: 1px; }
.card-close:hover { color: var(--ink); }
.card-close:active { transform: translateY(2px); box-shadow: inset 0 0 0 2px var(--line), 0 1px 0 var(--lip); }
.card-top { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.card.win .card-top, .card.lose .card-top { padding-right: 130px; }
.card-bead {
  width: 44px; height: 44px; flex: none;
  display: grid; place-items: center;
  padding-top: 3px;
  font: 800 21px/1 var(--font-display);
  color: var(--ink);
  vertical-align: 0;
}
.card-level { font: 800 13px/1 var(--font-display); letter-spacing: .12em; text-transform: uppercase; color: var(--ink-3); padding-top: 2px; }
.card-title { margin: 0; font: 800 36px/1.05 var(--font-display); letter-spacing: -.01em; text-wrap: balance; padding-top: 4px; }
.card-pattern { margin: -10px 0 0; font: 900 24px/1.2 var(--font-code); }
.card .card-pattern .code { background: none; padding: 0; font-size: 1em; box-shadow: none; }
/* Mixed level: its line forms as small plates */
.card-patterns { display: flex; flex-wrap: wrap; gap: 8px; font-size: 17px; margin-top: -6px; }
.card .card-patterns .code { background: var(--panel); padding: .2em .5em; box-shadow: inset 0 0 0 1.5px var(--line); }
.card .code {
  font: 800 .92em var(--font-code);
  white-space: nowrap;
  background: var(--panel);
  padding: .1em .4em;
  border-radius: 8px;
  box-shadow: inset 0 0 0 1.5px var(--line);
}
.card-text p, .card-lead, .card-note, .card-next { margin: 0; font: 600 17px/1.55 var(--font-body); color: #36325f; max-width: 62ch; }
.card-text { display: grid; gap: 10px; }
.card-lead { font-size: 18px; font-weight: 700; color: var(--ink); }
.card-note, .card-next { color: var(--ink-2); }
.card-rules {
  margin: 0;
  padding: 10px 14px 9px 40px;
  position: relative;
  font: 700 14px/1.45 var(--font-body);
  color: var(--ink-2);
  background: #fff6f7;
  border-radius: 14px;
  box-shadow: inset 0 0 0 1.5px #ffd9de;
}
.card-rules::before {
  content: "";
  position: absolute; left: 14px; top: 50%;
  width: 16px; height: 5px; margin-top: -2.5px;
  border-radius: 3px;
  background: var(--danger);
}

.example {
  margin: 2px 0 0;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 14px;
  padding: 16px 18px 14px;
  border-radius: 20px;
  background: var(--panel);
  box-shadow: inset 0 0 0 2px var(--line);
}
.ex-side { display: grid; justify-items: center; gap: 7px; }
.ex-label { font: 800 12px/1 var(--font-display); letter-spacing: .14em; text-transform: uppercase; color: var(--ink-3); }
.mini { display: block; height: 136px; width: auto; }
.ex-caption { margin: 0; font: 800 14px/1.2 var(--font-body); color: var(--ink-2); white-space: nowrap; }
.ex-mid { display: grid; justify-items: center; gap: 8px; min-width: 0; }
.ex-code { font: 900 22px/1 var(--font-code); white-space: nowrap; }
.ex-arrow { width: 70px; height: 21px; color: var(--ink); }
/* Code line above, arrow between the two abaci: fits even with three rods */
.example { grid-template-columns: auto auto auto; justify-content: center; grid-template-areas: "code code code" "before arrow after"; row-gap: 10px; }
.example .ex-mid { display: contents; }
.example .ex-code { grid-area: code; justify-self: center; }
.example .ex-arrow { grid-area: arrow; align-self: center; }
.example .ex-side:first-child { grid-area: before; }
.example .ex-side:last-child { grid-area: after; }
.example .ex-caption { white-space: normal; text-align: center; text-wrap: balance; max-width: 15em; }

.sticker {
  position: absolute; top: 24px; right: 26px;
  padding: 9px 16px 5px;
  border-radius: 14px;
  font: 800 23px/1 var(--font-display); letter-spacing: .06em; text-transform: uppercase;
  color: #fff;
  transform: rotate(-7deg);
  animation: slap .55s cubic-bezier(.2, 1.7, .35, 1) .12s both;
}
.sticker.bad { background: #f0435a; box-shadow: 0 4px 0 #b8213a, 0 10px 18px -8px rgba(184, 33, 58, .6); }
.sticker.ok { background: var(--ok); box-shadow: 0 4px 0 #0c8048, 0 10px 18px -8px rgba(12, 128, 72, .6); }
.sticker.gold { background: #ffc414; color: var(--ink); box-shadow: 0 4px 0 #c47b00, 0 10px 18px -8px rgba(196, 123, 0, .6); }
@keyframes slap { from { opacity: 0; transform: rotate(-7deg) scale(2.2); } }

.hop { display: flex; gap: 8px; margin: 2px 0 -4px; }
.hop .dot { width: 30px; height: 30px; animation: hop .7s cubic-bezier(.3, .8, .4, 1) both; }
@keyframes hop {
  0% { transform: translateY(0) scale(1, 1); }
  30% { transform: translateY(-16px) scale(.92, 1.1); }
  60% { transform: translateY(0) scale(1.14, .86); }
  80% { transform: translateY(-3px) scale(.98, 1.02); }
  100% { transform: translateY(0) scale(1, 1); }
}

.card-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 12px; margin-top: 4px; }
.btn {
  display: inline-flex; align-items: center; gap: 10px;
  height: 50px; padding: 2px 22px 0;
  border: 0; border-radius: 16px;
  background: #fff;
  color: var(--ink);
  font: 800 17px var(--font-display);
  cursor: pointer;
  box-shadow: inset 0 0 0 2px var(--line), 0 5px 0 var(--lip);
  transition: transform .1s, box-shadow .1s, background .1s;
}
.btn:hover { background: #fbfbff; }
.btn:active { transform: translateY(4px); box-shadow: inset 0 0 0 2px var(--line), 0 1px 0 var(--lip); }
.btn kbd { color: inherit; opacity: .7; box-shadow: inset 0 0 0 1.5px currentColor; margin-top: -2px; }
.btn.primary {
  color: #fff;
  background: var(--ink);
  box-shadow: 0 5px 0 #15123a, 0 14px 24px -10px rgba(38, 34, 79, .6);
}
.btn.primary:hover { background: #312c66; }
.btn.primary:active { box-shadow: 0 1px 0 #15123a, 0 6px 12px -8px rgba(38, 34, 79, .5); }
/* Focus ring around the key and its relief, with an even gap */
.btn, .tool { position: relative; --lip-h: 5px; --r: 16px; }
.tool { --lip-h: 4px; --r: 14px; }
.btn:active { --lip-h: 1px; }
.tool:active { --lip-h: 1px; }
.btn:focus-visible, .tool:focus-visible { outline: none; }
.btn:focus-visible::after, .tool:focus-visible::after {
  content: "";
  position: absolute;
  inset: -7px -7px calc(-7px - var(--lip-h)) -7px;
  border: 3px solid var(--focus);
  border-radius: calc(var(--r) + 7px);
  pointer-events: none;
}

.confetti { position: fixed; inset: 0; z-index: 30; pointer-events: none; overflow: hidden; }
.confetti.below { z-index: 19; }
.confetto { position: absolute; }
.confetto.strip { border-radius: 3px; background: var(--c); box-shadow: inset 0 -2px 0 var(--lo); }

/* Touch screens: no keyboard hints */
@media (hover: none) and (pointer: coarse) {
  kbd { display: none !important; }
}
/* …nor in the phone layout: the close button stands alone */
@media (max-width: 760px) and (orientation: portrait), (orientation: landscape) and (max-height: 540px) {
  kbd { display: none !important; }
  .app { grid-template-rows: auto minmax(0, 1fr); }
  .app > .footer { display: none; }
  .footer-below { display: block; padding: 24px 16px max(28px, env(safe-area-inset-bottom)); font-size: 14px; }
}

/* ---------- Medium screens ---------- */
@media (max-width: 1100px) and (min-width: 761px) {
  .wordmark { font-size: 27px; }
  .tool-label { display: none; }
}

/* ---------- Phone, portrait ---------- */
@media (max-width: 760px) and (orientation: portrait) {
  .app { gap: 10px; padding: max(10px, env(safe-area-inset-top)) 12px max(10px, env(safe-area-inset-bottom)); }
  .bar { grid-template-columns: 1fr auto; row-gap: 10px; }
  .levels { grid-column: 1 / -1; grid-row: 2; }
  .wordmark { font-size: clamp(20px, 6.4vw, 26px); }
  .mark { width: clamp(30px, 8.6vw, 34px); height: clamp(30px, 8.6vw, 34px); }
  .tools { gap: 6px; }
  /* Level buttons scale with the screen width; two rows of 8, big enough for a finger */
  .lvl { --s: min(30px, calc((100vw - 64px - 7 * 8px) / 8)); width: var(--s); height: var(--s); font-size: min(14px, calc(var(--s) * .5)); }
  .wire { flex-wrap: wrap; justify-content: center; gap: 12px 8px; padding: 7px 12px 12px; border-radius: 22px; max-width: calc(8 * var(--s, 30px) + 7 * 8px + 24px); }
  .wire .lvl { --s: min(30px, calc((100vw - 64px - 7 * 8px) / 8)); }
  .caption { display: none; }
  .tool { height: 40px; padding: 0 10px; }
  .tool-label, .tool kbd { display: none; }
  /* Abacus on top, program below: lines rise toward the abacus */
  .play { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) minmax(150px, 33%); gap: 10px; }
  .prog { align-self: stretch; grid-template-rows: auto minmax(0, 1fr); border-radius: 22px; }
  .prog-head { padding: 9px 14px 7px 12px; }
  .tape { height: auto; min-height: 0; }
  /* Status line under the abacus bar, in the faded part; the program covers the rest */
  .stage { grid-template-rows: minmax(0, 1fr); }
  .status { position: absolute; left: 0; right: 0; top: var(--status-top, auto); bottom: auto; min-height: 54px; align-content: start; z-index: 3; pointer-events: none; }
  .status .msg { font-size: 15px; padding: 7px 13px 5px; }
  .card { padding: 22px 20px 20px; gap: 14px; border-radius: 26px; }
  .card.win .card-top, .card.lose .card-top { padding-right: 100px; }
  .card-bead { width: 38px; height: 38px; font-size: 18px; }
  .card-title { font-size: 29px; }
  .card-text p, .card-lead, .card-note, .card-next { font-size: 16px; }
  .example { gap: 10px; padding: 12px; row-gap: 8px; }
  .mini { height: 110px; }
  .example .mini { height: 96px; }
  .example .ex-arrow { width: 38px; }
  .ex-code { font-size: 17px; }
  .ex-arrow { width: 46px; }
  .sticker { font-size: 19px; top: 20px; right: 18px; }
  .btn { height: 48px; padding: 2px 17px 0; }
}

/* Very small phones: tighter top buttons and program columns */
@media (max-width: 380px) and (orientation: portrait) {
  .tool { padding: 0 8px; }
  .tools { gap: 5px; }
}

/* ---------- Short screens (phone landscape) ---------- */
@media (orientation: landscape) and (max-height: 540px) {
  .app { min-height: 0; gap: 8px; padding-top: max(8px, env(safe-area-inset-top)); padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  .caption { display: none; }
  .wordmark { font-size: 24px; }
  .mark { width: 30px; height: 30px; }
  .lvl { width: 24px; height: 24px; font-size: 12px; }
  .wire { flex-wrap: nowrap; max-width: none; gap: 6px; padding: 4px 8px 9px; border-radius: 99px; }
  .lvl.current::after { bottom: -6px; width: 12px; height: 3px; margin-left: -6px; }
  .tool { height: 36px; }
  .tool-label { display: none; }
  .play { grid-template-columns: minmax(0, 1fr) clamp(240px, 34vw, 330px); gap: 14px; }
  .show-py .play { grid-template-columns: minmax(0, 1fr) clamp(260px, 41vw, 380px); }   /* longer Python lines */
  .prog { align-self: stretch; grid-template-rows: auto minmax(0, 1fr); border-radius: 20px; }
  .prog-head { padding: 8px 12px 6px; }
  .prog-title { font-size: 13px; letter-spacing: .05em; }
  .prog-ico { width: 18px; height: 18px; }
  .pips { gap: 3px; }
  .pip { width: 7px; height: 7px; }
  .stage { grid-template-rows: auto minmax(0, 1fr); }
  .status { grid-row: 1; min-height: 0; }
  .status .msg { font-size: 14px; padding: 5px 12px 3px; }
  #abacus { grid-row: 2; }
  .tape { height: auto; min-height: 0; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
