:root {
  --ink: #2a2150;
  --ink-soft: #645c98;
  --card: #fffdf6;
  --sun: #ffc531;
  --coral: #ff5d73;
  --accent: #ff5d73;
  --good: #2fb866;
  font-family: 'Fredoka', 'Arial Rounded MT Bold', ui-rounded, system-ui, sans-serif;
  color: var(--ink);
}
body[data-world="add"], [data-w="add"] { --accent: #27a85a; }
body[data-world="sub"], [data-w="sub"] { --accent: #2b93e6; }
body[data-world="mul"], [data-w="mul"] { --accent: #7b6cf6; }
body[data-world="div"], [data-w="div"] { --accent: #ee5a97; }

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; overflow: hidden; }
body { background: #69c9ff; -webkit-tap-highlight-color: transparent; user-select: none; }
button { font: inherit; color: inherit; cursor: pointer; border: 0; }

/* ---------- Sky ---------- */
.sky { position: fixed; inset: 0; opacity: 0; transition: opacity 1.1s ease; }
.sky-home { background: radial-gradient(circle at 84% 14%, #fffbe0 0 4%, rgba(255,244,190,.75) 7%, rgba(255,240,170,0) 26%), linear-gradient(#4db8ff 0%, #9fdfff 45%, #e9f8ff 70%); }
.sky-add  { background: radial-gradient(circle at 16% 16%, #fffbe0 0 4%, rgba(255,240,170,.7) 7%, rgba(255,240,170,0) 28%), linear-gradient(#3fb4f5 0%, #a8e6ff 48%, #f3fbe4 72%); }
.sky-sub  { background: linear-gradient(#ff9d6b 0%, #ffc98f 30%, #ffe9c2 55%, #d9f1ff 75%); }
.sky-mul  {
  background:
    radial-gradient(2px 2px at 12% 18%, #fff 50%, transparent 51%),
    radial-gradient(2px 2px at 28% 62%, #fff 50%, transparent 51%),
    radial-gradient(3px 3px at 44% 12%, #ffe9a8 50%, transparent 51%),
    radial-gradient(2px 2px at 58% 40%, #fff 50%, transparent 51%),
    radial-gradient(3px 3px at 71% 22%, #fff 50%, transparent 51%),
    radial-gradient(2px 2px at 83% 55%, #ffe9a8 50%, transparent 51%),
    radial-gradient(2px 2px at 92% 14%, #fff 50%, transparent 51%),
    radial-gradient(2px 2px at 6% 48%, #fff 50%, transparent 51%),
    radial-gradient(2px 2px at 36% 34%, #fff 50%, transparent 51%),
    radial-gradient(2px 2px at 65% 8%, #fff 50%, transparent 51%),
    linear-gradient(#17103f 0%, #37288a 55%, #7e52d6 100%);
}
.sky-div  { background: linear-gradient(#ff9fc6 0%, #ffd3e6 50%, #fff3d2 100%); }
body[data-world="home"] .sky-home,
body[data-world="add"] .sky-add,
body[data-world="sub"] .sky-sub,
body[data-world="mul"] .sky-mul,
body[data-world="div"] .sky-div { opacity: 1; }

.flash { position: fixed; inset: 0; z-index: 3; pointer-events: none; opacity: 0;
  background: radial-gradient(circle at 50% 55%, #fff 0 18%, rgba(255,255,255,.85) 34%, rgba(255,244,190,0) 70%); }
.flash.go { animation: flash .75s ease-out; }
@keyframes flash { 0% { opacity: 0; transform: scale(.2); } 25% { opacity: 1; } 100% { opacity: 0; transform: scale(2.4); } }

#stage { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
#ui { position: fixed; inset: 0; pointer-events: none; }

/* ---------- Top bar ---------- */
.topbar { position: absolute; top: 0; left: 0; right: 0; display: flex; gap: 10px; padding: 12px 14px; z-index: 5; }
.spacer { flex: 1; }
.chip {
  pointer-events: auto; display: inline-flex; align-items: center; gap: 6px;
  height: 42px; padding: 0 16px; border-radius: 999px;
  background: var(--card); font-weight: 600; font-size: 17px;
  box-shadow: 0 4px 0 rgba(42, 33, 80, .18);
}
button.chip:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(42, 33, 80, .18); }
.chip.stars b { min-width: 1ch; }

/* ---------- Buttons ---------- */
.btn {
  pointer-events: auto; padding: 12px 24px; border-radius: 18px;
  background: var(--accent); color: #fff; font-weight: 600; font-size: 19px;
  box-shadow: 0 5px 0 color-mix(in srgb, var(--accent) 68%, #000);
  transition: transform .08s, box-shadow .08s, filter .15s;
}
.btn:hover { filter: brightness(1.06); }
.btn:active { transform: translateY(4px); box-shadow: 0 1px 0 color-mix(in srgb, var(--accent) 68%, #000); }
.btn.ghost { background: #fff; color: var(--ink); box-shadow: 0 5px 0 #d9d3ee; }
.btn.ghost:active { box-shadow: 0 1px 0 #d9d3ee; }
.btn.big { font-size: clamp(22px, 3.2vh, 30px); padding: 16px 38px; border-radius: 26px; --accent: #ff5d73; animation: bob 1.8s ease-in-out infinite; }
.btn:disabled { opacity: .5; cursor: default; }
:focus-visible { outline: 4px solid #fff; outline-offset: 3px; }

/* ---------- Home ---------- */
.screen { position: absolute; inset: 0; pointer-events: none; animation: fade-in .35s ease both; }
.title-block { position: absolute; top: clamp(50px, 7vh, 70px); left: 0; right: 0; text-align: center; padding: 0 12px; }
.title { margin: 0; line-height: .95; font-weight: 700; letter-spacing: .5px; }
.title span { display: block; }
.title .t1 { font-size: clamp(26px, 5vh, 46px); color: #fff; -webkit-text-stroke: 8px var(--ink); paint-order: stroke fill; }
.title .t1 .js-name { display: inline; }
.title .t2 { font-size: clamp(40px, 9.5vh, 88px); color: var(--sun); -webkit-text-stroke: 12px var(--ink); paint-order: stroke fill; text-shadow: 0 7px 0 var(--ink); }
.tagline { margin: 14px 0 0; font-size: clamp(15px, 2.4vh, 21px); font-weight: 500; color: var(--ink); }
.home .btn.big { position: absolute; left: 50%; bottom: max(26px, 4vh); translate: -50% 0; white-space: nowrap; }

/* ---------- Speech bubble ---------- */
.bubble {
  position: absolute; left: 0; top: 0; z-index: 4; width: max-content; max-width: min(330px, 78vw);
  background: var(--card); border: 3px solid #fff; border-radius: 22px; padding: 12px 18px;
  font-size: clamp(16px, 1.4vw, 19px); font-weight: 500; line-height: 1.3;
  box-shadow: 0 5px 0 rgba(42, 33, 80, .14), 0 14px 30px rgba(42, 33, 80, .16);
  will-change: transform;
}
.bubble p { margin: 0; }
.bubble p.pop { animation: pop .3s cubic-bezier(.3, 1.6, .5, 1) both; }
.bubble::after { content: ''; position: absolute; width: 18px; height: 18px; background: var(--card); border-radius: 3px; rotate: 45deg; }
.bubble[data-anchor="above"]::after { left: calc(50% - 9px + var(--tail, 0px)); bottom: -9px; }
.bubble[data-anchor="right"]::after { left: -9px; top: calc(50% - 9px); }
.bubble[data-anchor="side"] {
  left: var(--buddy-w, 40vw); right: 10px; top: 64px; width: auto; max-width: none;
  transform: none !important; font-size: 15px; padding: 10px 14px;
}
.bubble[data-anchor="side"] p { max-height: calc(33vh - 92px); overflow: auto; }
.bubble[data-anchor="side"]::after { left: -9px; top: 26px; }

/* ---------- Side panel ---------- */
.screen.side { display: flex; align-items: center; padding: 70px 3vw 3vh 46vw; }
.panel {
  pointer-events: auto; width: 100%; max-width: 780px; max-height: 100%; margin-inline: auto; overflow: auto;
  background: rgba(255, 253, 246, .95); border: 4px solid #fff; border-radius: 30px;
  padding: clamp(14px, 2.4vh, 26px) clamp(14px, 2vw, 28px);
  box-shadow: 0 8px 0 rgba(42, 33, 80, .12), 0 24px 50px rgba(42, 33, 80, .2);
  display: flex; flex-direction: column; gap: clamp(8px, 1.6vh, 16px);
}
.panel h2 { margin: 0; font-size: clamp(24px, 4vh, 36px); font-weight: 700; text-align: center; }
.sub { margin: 0; text-align: center; font-size: clamp(16px, 2.3vh, 20px); color: var(--ink-soft); font-weight: 500; }

body.portrait .screen.side { padding: 34vh 10px 10px; align-items: stretch; }
body.portrait .panel { border-radius: 24px; }
.panel > * { flex-shrink: 0; }

/* World cards */
.world-grid { display: grid; grid-template-columns: 1fr 1fr; gap: clamp(10px, 1.8vh, 16px); }
.world-card {
  position: relative; overflow: hidden; text-align: left; color: #fff;
  border-radius: 24px; padding: clamp(12px, 2vh, 20px); min-height: clamp(104px, 19vh, 170px);
  background: linear-gradient(140deg, color-mix(in srgb, var(--accent) 78%, #fff), var(--accent));
  box-shadow: 0 6px 0 color-mix(in srgb, var(--accent) 66%, #000);
  display: flex; flex-direction: column; justify-content: flex-end; gap: 2px;
  transition: transform .12s, box-shadow .12s;
}
.world-card:hover { transform: translateY(-3px) rotate(-.6deg); }
.world-card:active { transform: translateY(4px); box-shadow: 0 2px 0 color-mix(in srgb, var(--accent) 66%, #000); }
.wc-symbol { position: absolute; right: 10px; top: -18px; font-size: clamp(80px, 17vh, 150px); font-weight: 700; line-height: 1; opacity: .26; }
.wc-emoji { font-size: clamp(28px, 5vh, 44px); line-height: 1; }
.wc-name { font-size: clamp(17px, 2.7vh, 24px); font-weight: 700; line-height: 1.05; }
.wc-blurb { font-size: clamp(13px, 1.9vh, 16px); font-weight: 500; opacity: .95; }
.wc-stars { position: absolute; left: 12px; top: 10px; background: rgba(255, 255, 255, .92); color: var(--ink); border-radius: 999px; padding: 2px 10px; font-size: 14px; font-weight: 600; }

/* Levels */
.world-badge { align-self: center; font-size: clamp(40px, 8vh, 64px); line-height: 1; animation: bob 2s ease-in-out infinite; }
.level-list { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.level-btn {
  background: #fff; border-radius: 22px; padding: 14px 8px; text-align: center;
  border: 3px solid color-mix(in srgb, var(--accent) 40%, #fff);
  box-shadow: 0 6px 0 color-mix(in srgb, var(--accent) 50%, #fff);
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  transition: transform .1s, box-shadow .1s;
}
.level-btn:active { transform: translateY(4px); box-shadow: 0 2px 0 color-mix(in srgb, var(--accent) 50%, #fff); }
.level-btn:disabled { opacity: .55; cursor: default; }
.lv-num { width: 54px; height: 54px; border-radius: 50%; display: grid; place-items: center; background: var(--accent); color: #fff; font-size: 28px; font-weight: 700; }
.lv-name { font-size: 20px; font-weight: 600; }
.lv-hint { font-size: 14px; color: var(--ink-soft); }
.stars-row { font-size: 22px; letter-spacing: 2px; color: #d8d2ea; }
.stars-row .on { color: var(--sun); text-shadow: 0 1px 0 #d59a00; }

/* Lesson */
.dots { display: flex; gap: 8px; justify-content: center; }
.dots span { width: 16px; height: 16px; border-radius: 50%; background: #e4def4; transition: background .3s, transform .3s; }
.dots .now { background: var(--accent); transform: scale(1.25); }
.dots .gold { background: var(--sun); }
.dots .ok { background: #9fdcb7; }
.prompt { margin: 0; text-align: center; font-size: clamp(17px, 2.7vh, 23px); font-weight: 500; line-height: 1.3; }
.visual {
  flex: 1 1 auto; min-height: clamp(90px, 20vh, 200px);
  display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 10px;
}
.grp {
  display: grid; grid-template-columns: repeat(var(--cols, 5), auto); gap: 6px; padding: 10px 12px;
  border-radius: 18px; position: relative;
  background: color-mix(in srgb, var(--accent) 10%, #fff);
  border: 2px dashed color-mix(in srgb, var(--accent) 45%, #fff);
  transition: background .25s, transform .25s;
}
.grp.lit { background: color-mix(in srgb, var(--sun) 40%, #fff); transform: scale(1.05); }
.grp .total { position: absolute; right: -8px; top: -12px; background: var(--ink); color: #fff; font-style: normal; font-weight: 600; font-size: 15px; border-radius: 999px; padding: 1px 9px; scale: 0; transition: scale .25s cubic-bezier(.3, 1.6, .5, 1); }
.grp.lit .total, .grp.done .total { scale: 1; }
.visual .op { font-size: 36px; font-weight: 700; color: var(--accent); }
.obj { position: relative; font-size: var(--obj, 40px); line-height: 1; animation: pop .35s cubic-bezier(.3, 1.6, .5, 1) both; transition: opacity .4s, transform .4s, filter .4s; }
.obj i { font-style: normal; display: block; transition: transform .2s; }
.obj b {
  position: absolute; right: -7px; top: -8px; min-width: 21px; height: 21px; padding: 0 4px; border-radius: 999px;
  background: var(--ink); color: #fff; font-size: 13px; font-weight: 600; display: grid; place-items: center;
  scale: 0; transition: scale .2s cubic-bezier(.3, 1.6, .5, 1);
}
.obj.counted b { scale: 1; }
.obj.counted i { transform: translateY(-3px) scale(1.1); }
.obj.gone { opacity: .28; transform: translateY(-14px) rotate(14deg); filter: grayscale(1); }
.v-div { flex-direction: column; }
.pile:empty { display: none; }
.plates { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }
.plate { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.friend { font-size: 30px; line-height: 1; }
.plate .grp { min-width: 62px; min-height: 46px; border-style: solid; border-radius: 50% / 40%; padding: 8px 14px; }
.v-steps { flex-direction: column; }
.trick { font-size: 40px; letter-spacing: 6px; }
.steps { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.steps li { background: color-mix(in srgb, var(--accent) 12%, #fff); border-radius: 14px; padding: 6px 16px; font-size: clamp(17px, 2.6vh, 22px); font-weight: 600; text-align: center; animation: pop .3s both; }

.equation { text-align: center; font-size: clamp(34px, 7vh, 58px); font-weight: 700; line-height: 1; }
.equation .sym { color: var(--accent); margin: 0 .12em; }
.slot { display: inline-grid; place-items: center; min-width: 1.5em; height: 1.25em; padding: 0 .15em; border-radius: 16px; background: #efeafb; border: 3px dashed #b9afe0; color: var(--ink-soft); vertical-align: -0.12em; }
.slot.filled { background: var(--good); border: 3px solid var(--good); color: #fff; animation: pop .4s cubic-bezier(.3, 1.6, .5, 1); }

.choices { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.choice {
  background: #fff; border-radius: 20px; padding: 12px 0; font-size: clamp(26px, 4.8vh, 38px); font-weight: 700;
  border: 3px solid color-mix(in srgb, var(--accent) 45%, #fff);
  box-shadow: 0 6px 0 color-mix(in srgb, var(--accent) 55%, #fff);
  transition: transform .08s, box-shadow .08s, background .2s;
}
.choice:hover:not(:disabled) { background: color-mix(in srgb, var(--accent) 10%, #fff); }
.choice:active:not(:disabled) { transform: translateY(4px); box-shadow: 0 2px 0 color-mix(in srgb, var(--accent) 55%, #fff); }
.choice.right { background: var(--good); border-color: var(--good); color: #fff; box-shadow: 0 6px 0 #1f8a4a; animation: pop .4s cubic-bezier(.3, 1.6, .5, 1); }
.choice.wrong { background: #f1eef8; border-color: #d9d3ee; color: #aaa3c8; box-shadow: none; animation: shake .4s; }
.choice.glow { animation: glow 1s ease-in-out infinite; }
.choice:disabled { cursor: default; }
.lesson.locked .choices { pointer-events: none; opacity: .6; }
.foot { display: flex; justify-content: space-between; gap: 10px; min-height: 52px; }
.foot .btn:only-child, #btn-next { margin-left: auto; }

/* Results */
.results { align-items: center; text-align: center; }
.big-stars { font-size: clamp(54px, 12vh, 96px); line-height: 1; letter-spacing: 6px; color: #dcd6ee; }
.big-stars .on { color: var(--sun); text-shadow: 0 4px 0 #d59a00; display: inline-block; animation: pop .5s cubic-bezier(.3, 1.8, .5, 1) both; }
.big-stars .on:nth-child(2) { animation-delay: .25s; }
.big-stars .on:nth-child(3) { animation-delay: .5s; }
.res-actions { display: flex; flex-wrap: wrap; gap: 12px; justify-content: center; margin-top: 6px; }

@media (max-width: 520px) {
  .choices { gap: 8px; }
  .level-list { gap: 8px; }
  .lv-num { width: 44px; height: 44px; font-size: 22px; }
  .lv-hint { display: none; }
  .btn { font-size: 16px; padding: 10px 14px; }
  .visual { gap: 6px; min-height: 0; }
  .obj { font-size: calc(var(--obj, 40px) * .72); }
  .grp { gap: 3px; padding: 7px 8px; border-radius: 14px; }
  .plate .grp { padding: 6px 10px; min-width: 50px; }
  .friend { font-size: 24px; }
  .panel { gap: 8px; }
}

@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes pop { from { opacity: 0; scale: .6; } to { opacity: 1; scale: 1; } }
@keyframes bob { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-6px); } }
@keyframes shake { 20% { transform: translateX(-6px); } 40% { transform: translateX(6px); } 60% { transform: translateX(-4px); } 80% { transform: translateX(4px); } }
@keyframes glow { 0%, 100% { box-shadow: 0 6px 0 color-mix(in srgb, var(--accent) 55%, #fff), 0 0 0 0 var(--sun); } 50% { box-shadow: 0 6px 0 color-mix(in srgb, var(--accent) 55%, #fff), 0 0 0 8px var(--sun); } }

@media (prefers-reduced-motion: reduce) {
  .btn.big, .world-badge { animation: none; }
}

/* Visible to search engines and screen readers, not on screen */
.sr-only { position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
