:root {
  --bg: #f5f3ff;
  --surface: #ffffff;
  --surface-2: #ede9fe;
  --text: #1e1b4b;
  --muted: #4c4a6b;
  --primary: #4f46e5;
  --primary-ink: #ffffff;
  --accent: #db2777;
  --good: #15803d;
  --good-bg: #dcfce7;
  --bad: #b91c1c;
  --bad-bg: #fee2e2;
  --bond: #312e81;
  --border: #c7c2f0;
  --focus: #b45309;
  --flask: radial-gradient(circle at 50% 40%, #ffffff 0%, #eef2ff 70%, #e0e7ff 100%);
  --radius: 16px;
  --shadow: 0 2px 0 rgba(30, 27, 75, .12), 0 6px 18px rgba(30, 27, 75, .08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #120f2b; --surface: #1d1940; --surface-2: #2a2560; --text: #f5f3ff; --muted: #c4c0e8;
    --primary: #818cf8; --primary-ink: #120f2b; --accent: #f472b6; --good: #4ade80; --good-bg: #123b24;
    --bad: #fca5a5; --bad-bg: #3f1515; --bond: #c7d2fe; --border: #3d3780; --focus: #fbbf24;
    --flask: radial-gradient(circle at 50% 40%, #2a2560 0%, #1d1940 70%, #17143a 100%);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #120f2b; --surface: #1d1940; --surface-2: #2a2560; --text: #f5f3ff; --muted: #c4c0e8;
  --primary: #818cf8; --primary-ink: #120f2b; --accent: #f472b6; --good: #4ade80; --good-bg: #123b24;
  --bad: #fca5a5; --bad-bg: #3f1515; --bond: #c7d2fe; --border: #3d3780; --focus: #fbbf24;
  --flask: radial-gradient(circle at 50% 40%, #2a2560 0%, #1d1940 70%, #17143a 100%);
  color-scheme: dark;
}
/* The play boards, the scale and the world scene are always dark, so focus there is always bright. */
.board, .scale-wrap, .scene-wrap, .mode-card { --focus: #fde68a; }

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", "Segoe UI", system-ui, sans-serif;
  font-size: 18px; line-height: 1.5; letter-spacing: .01em;
  min-height: 100vh;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 4px solid var(--focus); outline-offset: 3px; border-radius: 10px; }
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip-link { position: absolute; left: 8px; top: -60px; background: var(--primary); color: var(--primary-ink); padding: 8px 14px; border-radius: 10px; z-index: 10; }
.skip-link:focus { top: 8px; }
.noscript { padding: 16px; }
main { max-width: 960px; margin: 0 auto; padding: 16px; outline: none; }

/* ---------- Top bar ---------- */
.topbar { display: flex; align-items: center; gap: 8px; padding: 8px 16px; background: var(--surface); box-shadow: var(--shadow); position: sticky; top: 0; z-index: 5; }
.brand { font-size: 1.15rem; margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-right { display: flex; align-items: center; gap: 6px; }
.star-total { font-weight: 700; padding: 0 6px; white-space: nowrap; }
.icon-btn { min-width: 44px; min-height: 44px; border: 2px solid var(--border); background: var(--surface); border-radius: 12px; font-size: 1.2rem; line-height: 1; }
.icon-btn:hover { background: var(--surface-2); }

/* ---------- Buttons ---------- */
.btn { min-height: 48px; padding: 10px 18px; border-radius: 14px; border: 2px solid var(--border); background: var(--surface); font-weight: 700; box-shadow: 0 2px 0 var(--border); }
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(2px); box-shadow: none; }
.btn-primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); box-shadow: 0 3px 0 #312e81; }
.btn-primary:hover { background: var(--primary); filter: brightness(1.08); }
.btn-big { font-size: 1.15rem; padding: 14px 24px; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.row { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.center { text-align: center; }
.muted { color: var(--muted); }

/* ---------- Home ---------- */
.hero { padding: 0 0 16px; }
.scene-wrap { position: relative; border-radius: 24px; overflow: hidden; box-shadow: var(--shadow); background: #1e1858; }
.scene { display: block; width: 100%; height: auto; aspect-ratio: 2 / 1; }
.scene-part { transition: filter 1s, opacity 1s; }
.scene-part.dim { filter: grayscale(1) brightness(.75); opacity: .55; }
.scene .spin { transform-box: fill-box; transform-origin: center; animation: spin 30s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.scene .drift { animation: drift 9s ease-in-out infinite alternate; }
@keyframes drift { to { transform: translateX(24px); } }
.scene .bubble { animation: rise 2.2s ease-in infinite; }
.scene .bubble.b2 { animation-delay: 1.1s; }
@keyframes rise { 0% { transform: translateY(0); opacity: 1; } 100% { transform: translateY(-24px); opacity: 0; } }
.scene .flame { transform-box: fill-box; transform-origin: top; animation: flicker .3s ease-in-out infinite alternate; }
@keyframes flicker { to { transform: scaleY(1.25); } }
.scene .window.lit { filter: drop-shadow(0 0 4px #fde68a); }
.scene-part.dim .spin, .scene-part.dim .drift, .scene-part.dim .bubble, .scene-part.dim .flame { animation: none; }
.scene-caption { position: absolute; left: 0; right: 0; bottom: 0; padding: 14px 18px; color: #fff; background: linear-gradient(transparent, rgba(17, 13, 56, .88) 45%); }
.scene-caption h2 { margin: 0; font-size: clamp(1.25rem, 4vw, 2rem); text-shadow: 0 2px 6px rgba(0,0,0,.5); }
.scene-caption p { margin: 0 0 6px; }
.meter { height: 12px; border-radius: 6px; background: rgba(255,255,255,.25); overflow: hidden; max-width: 420px; }
.meter span { display: block; height: 100%; background: linear-gradient(90deg, #f472b6, #fde68a, #4ade80); border-radius: 6px; }
.modes { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 12px; margin-bottom: 18px; }
.mode-card { display: flex; align-items: center; gap: 14px; text-align: left; padding: 14px 18px; border-radius: 20px; border: none; color: #fff; min-height: 84px; box-shadow: var(--shadow); }
.mode-card.inventor { background: linear-gradient(135deg, #7c3aed, #db2777); }
.mode-card.museum { background: linear-gradient(135deg, #0369a1, #0f766e); }
.mode-card:hover { transform: translateY(-2px); }
.mode-card strong { display: block; font-size: 1.2rem; }
.mode-card small { font-size: .9rem; }
.mode-emoji { font-size: 2.4rem; }
.level-tile .e { font-size: 1.5rem; line-height: 1; }
.intro-cast { display: flex; justify-content: center; gap: 10px; margin: 8px 0; }
.hand-list { list-style: none; padding: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 4px 12px; }
.win-emoji { font-size: 4.5rem; text-align: center; line-height: 1.1; animation: pop .6s; }
.restored { font-weight: 700; color: var(--accent); }
.formula-big { font-size: 1.8rem; font-weight: 800; margin: 0; }
.message.good-soft { background: var(--good-bg); color: var(--good); }

/* Museum */
.museum { list-style: none; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; }
.museum-card { display: flex; flex-direction: column; gap: 4px; padding: 14px; border-radius: 18px; background: var(--surface); box-shadow: var(--shadow); border: 2px dashed var(--border); }
.museum-card.found { border: 2px solid var(--primary); }
.museum-emoji { font-size: 2.4rem; }
.museum-formula { font-size: 1.3rem; font-weight: 800; color: var(--primary); }
.museum-card small { color: var(--muted); font-size: .85rem; line-height: 1.35; }

/* Balance scale */
.scale-wrap { background: radial-gradient(ellipse at 50% 30%, #3b2f8f, #110d38); border-radius: 24px; padding: 8px; margin-bottom: 12px; box-shadow: var(--shadow); }
.scale { display: block; width: 100%; max-height: 300px; }
.scale .stand { fill: #a5b4fc; }
.scale .beam { transform-box: view-box; transform-origin: 200px 89px; transition: transform .6s cubic-bezier(.3, 1.6, .5, 1); }
.scale .beam-bar { fill: #fde68a; }
.scale .string { stroke: #c7d2fe; stroke-width: 2; }
.scale .pan { fill: #e0e7ff; stroke: #818cf8; stroke-width: 2; }
.scale .pivot { fill: #f472b6; }
.scale .pan-label { fill: #e0e7ff; font-size: 14px; font-weight: 700; font-family: inherit; }
.scale.level .beam-bar { fill: #4ade80; filter: drop-shadow(0 0 8px #4ade80); }
.dot { display: inline-block; width: 12px; height: 12px; border-radius: 50%; border: 1px solid rgba(0,0,0,.35); vertical-align: middle; }
.mini-atoms { display: flex; flex-wrap: wrap; justify-content: center; gap: 2px; max-width: 80px; }
.world { background: var(--surface); border-radius: var(--radius); padding: 16px; margin-bottom: 18px; box-shadow: var(--shadow); }
.world-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; flex-wrap: wrap; }
.world h3 { margin: 0; font-size: 1.3rem; }
.world p { margin: 2px 0 12px; }
.badge { display: inline-block; font-size: .8rem; font-weight: 700; padding: 2px 10px; border-radius: 999px; background: var(--surface-2); }
.badge-free { background: var(--good-bg); color: var(--good); }
.badge-pass { background: #fef3c7; color: #92400e; }
.level-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 10px; }
.level-tile { min-height: 96px; border-radius: 14px; border: 2px solid var(--border); background: var(--surface-2); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; padding: 8px; text-align: center; }
.level-tile .f { font-size: 1.35rem; font-weight: 800; }
.level-tile .n { font-size: .85rem; color: var(--muted); }
.level-tile .s { font-size: .85rem; letter-spacing: 1px; }
.level-tile.done { border-color: var(--good); }
.level-tile.locked { border-style: dashed; }
.level-tile.locked .e, .level-tile.locked .f { opacity: .6; }
.level-tile:hover:not(.locked) { transform: translateY(-2px); }
.unlock-cta { margin: 12px 0 0; }
.win-actions { margin-top: 12px; }

/* ---------- Play screen ---------- */
.play-head { display: flex; justify-content: space-between; gap: 12px; align-items: center; flex-wrap: wrap; margin-bottom: 10px; }
.goal { background: var(--surface); border-radius: var(--radius); padding: 10px 16px; box-shadow: var(--shadow); }
.goal .label { font-size: .85rem; color: var(--muted); }
.goal .target { font-size: 1.8rem; font-weight: 800; }
.current { font-size: 1rem; }
.goal .small { font-size: 1.05rem; }
.counter { font-weight: 700; background: var(--surface); padding: 6px 14px; border-radius: 999px; box-shadow: var(--shadow); }

/* Atomlings: atom characters */
.board, .tray, .slot, .tray-atom, .atomling { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.atomling { position: relative; width: 60px; height: 60px; border-radius: 50%; display: grid; place-items: center; box-shadow: inset -4px -6px 10px rgba(0,0,0,.25), 0 4px 10px rgba(0,0,0,.25); border: 2px solid rgba(255,255,255,.35); }
.atomling .face { position: absolute; inset: 4px 8px auto 8px; width: calc(100% - 16px); height: 60%; }
.atomling .sym { position: relative; top: 9px; font-weight: 900; font-size: 1.2rem; letter-spacing: 0; }
.atomling.light-ink .sym { text-shadow: 0 0 2px rgba(0,0,0,.75), 0 1px 2px rgba(0,0,0,.6); }
html.calm *, html.calm *::before, html.calm *::after { animation-play-state: paused !important; }
.atomling.happy { animation: bob 2.4s ease-in-out infinite; }
@keyframes bob { 50% { transform: translateY(-3px); } }

/* Puzzle board: a glowing night lab */
.board { position: relative; width: 100%; aspect-ratio: 4 / 3; max-height: 70vh; min-height: 280px; border-radius: 26px; overflow: hidden; touch-action: manipulation;
  background: radial-gradient(ellipse at 50% 35%, #3b2f8f 0%, #1e1858 55%, #110d38 100%); box-shadow: inset 0 0 40px rgba(129,140,248,.35), var(--shadow); }
.board::before { content: ""; position: absolute; inset: 0; pointer-events: none; opacity: .7;
  background-image: radial-gradient(1.5px 1.5px at 12% 18%, #fff, transparent), radial-gradient(1.5px 1.5px at 78% 12%, #fff, transparent), radial-gradient(1px 1px at 40% 80%, #fff, transparent), radial-gradient(1.5px 1.5px at 88% 70%, #fff, transparent), radial-gradient(1px 1px at 22% 62%, #fff, transparent), radial-gradient(1px 1px at 60% 30%, #fff, transparent); }
.board svg { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.board .empty { position: absolute; inset: 0; display: grid; place-items: center; color: #e0e7ff; padding: 24px; text-align: center; }
.bond { stroke: rgba(199, 210, 254, .45); stroke-width: 6; stroke-linecap: round; stroke-dasharray: 3 9; }
.bond.lit { stroke: #fde68a; stroke-dasharray: none; filter: drop-shadow(0 0 4px #fbbf24); }
.bond.ionic { stroke-dasharray: 2 10; }
.board.solved .bond.lit { animation: glow 1s ease-in-out infinite alternate; }
@keyframes glow { to { stroke: #fff; filter: drop-shadow(0 0 10px #f472b6); } }
.board.solved .atomling { animation: dance .5s ease-in-out infinite alternate; }
@keyframes dance { from { transform: translateY(0) rotate(-6deg); } to { transform: translateY(-8px) rotate(6deg); } }

.slot { position: absolute; width: 72px; height: 72px; margin: -36px 0 0 -36px; border-radius: 50%; border: 3px dashed rgba(224, 231, 255, .7); background: rgba(255,255,255,.08); display: grid; place-items: center; padding: 0; transition: left .25s, top .25s, transform .15s; }
.slot.ready { border-color: #fde68a; animation: pulse 1.2s ease-in-out infinite; }
.slot.hover { transform: scale(1.15); border-color: #fff; background: rgba(255,255,255,.2); }
@keyframes pulse { 50% { box-shadow: 0 0 0 8px rgba(253, 230, 138, .25); } }
.slot.filled { border: none; background: transparent; }
.slot[aria-pressed="true"] .atomling { outline: 5px solid #f472b6; outline-offset: 3px; }
.slot-need { color: #fde68a; font-size: 1.6rem; font-weight: 900; text-shadow: 0 0 8px rgba(251,191,36,.7); }
.hands-badge { position: absolute; bottom: -4px; right: -4px; min-width: 26px; height: 26px; border-radius: 13px; font-size: .85rem; font-weight: 800; background: #be185d; color: #fff; display: grid; place-items: center; border: 2px solid #fff; }
.shake { animation: shake .35s; }
@keyframes shake { 20%, 60% { transform: translateX(-7px); } 40%, 80% { transform: translateX(7px); } }
.pop { animation: pop .4s; }
@keyframes pop { 0% { transform: scale(.3); } 70% { transform: scale(1.15); } 100% { transform: scale(1); } }

.tray { display: flex; flex-wrap: wrap; justify-content: center; gap: 12px; margin: 12px 0 4px; padding: 12px; min-height: 104px; border-radius: 20px; background: var(--surface); box-shadow: var(--shadow); }
.tray-atom { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 6px; border-radius: 18px; border: 3px solid transparent; background: transparent; touch-action: none; }
.tray-atom:hover .atomling { transform: translateY(-3px); }
.tray-atom[aria-pressed="true"] { border-color: var(--accent); background: var(--surface-2); }
.tray-atom[aria-pressed="true"] .atomling { transform: scale(1.1); }
.hands-label { font-size: .8rem; line-height: 1; }
.drag-ghost { position: fixed; z-index: 50; pointer-events: none; transform: translate(-50%, -50%) scale(1.15); filter: drop-shadow(0 8px 10px rgba(0,0,0,.35)); }

.controls { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 12px; }
.message { margin-top: 12px; min-height: 3em; padding: 10px 14px; border-radius: 12px; background: var(--surface); box-shadow: var(--shadow); }
.message.good { background: var(--good-bg); color: var(--good); font-weight: 700; }
.message.bad { background: var(--bad-bg); color: var(--bad); }
.how { font-size: .95rem; color: var(--muted); margin: 6px 0 0; }

/* ---------- Balance ---------- */
.equation { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 10px; background: var(--surface); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.species { display: flex; flex-direction: column; align-items: center; gap: 6px; background: var(--surface-2); border-radius: 14px; padding: 8px; }
.species .formula { font-size: 1.6rem; font-weight: 800; }
.stepper { display: flex; align-items: center; gap: 4px; }
.stepper button { width: 44px; height: 44px; border-radius: 12px; border: 2px solid var(--border); background: var(--surface); font-size: 1.3rem; font-weight: 800; }
.stepper output { min-width: 2ch; text-align: center; font-size: 1.4rem; font-weight: 800; }
.op { font-size: 1.6rem; font-weight: 800; color: var(--muted); }
.tally { width: 100%; max-width: 420px; margin: 14px auto 0; border-collapse: collapse; background: var(--surface); border-radius: 12px; overflow: hidden; box-shadow: var(--shadow); }
.tally th, .tally td { padding: 8px 12px; text-align: center; border-bottom: 1px solid var(--border); }
.tally .ok { color: var(--good); font-weight: 800; }
.tally .no { color: var(--bad); font-weight: 800; }

/* ---------- Dialog ---------- */
dialog { border: none; border-radius: 22px; padding: 0; max-width: min(520px, calc(100vw - 32px)); width: 100%; background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.35); }
dialog::backdrop { background: rgba(18, 15, 43, .6); }
.dialog-body { padding: 22px; }
.dialog-body h2 { margin: 0 0 8px; font-size: 1.5rem; }
.big-stars { font-size: 2.6rem; text-align: center; letter-spacing: 6px; margin: 4px 0; }
.fact { background: var(--surface-2); border-radius: 14px; padding: 12px 14px; }
.plans { display: grid; gap: 10px; margin: 14px 0; }
.plan { display: flex; justify-content: space-between; align-items: center; text-align: left; gap: 10px; padding: 12px 14px; border-radius: 14px; border: 3px solid var(--border); background: var(--surface); min-height: 64px; }
.plan[aria-checked="true"] { border-color: var(--primary); background: var(--surface-2); }
.plan .price { font-size: 1.2rem; font-weight: 800; white-space: nowrap; }
.plan small { display: block; color: var(--muted); }
.best { font-size: .75rem; background: var(--accent); color: #fff; padding: 1px 8px; border-radius: 999px; margin-left: 6px; }
.fine { font-size: .8rem; color: var(--muted); }
.fine a { color: var(--primary); }
.license-form { margin: 12px 0; padding: 12px; border-radius: 14px; background: var(--surface-2); }
.legal a { color: var(--primary); }
.plan-terms { font-weight: 700; min-height: 1.5em; margin: 0 0 8px; }
.legal { max-width: 720px; margin: 0 auto; padding: 24px 16px 48px; }
.legal h1 { font-size: 1.8rem; }
.legal .draft { background: #fef3c7; color: #92400e; padding: 8px 12px; border-radius: 10px; }
.demo-note { background: #fef3c7; color: #92400e; border-radius: 10px; padding: 6px 10px; font-size: .85rem; }
.gate-input { width: 100%; min-height: 52px; font-size: 1.4rem; padding: 8px 12px; border-radius: 12px; border: 2px solid var(--muted); background: var(--surface); margin: 8px 0; }
.error-text { color: var(--bad); min-height: 1.5em; }
.stat-list { list-style: none; padding: 0; margin: 8px 0; }
.stat-list li { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid var(--border); }

@media (max-width: 520px) {
  body { font-size: 17px; }
  .brand { font-size: 1rem; }
  .slot { width: 58px; height: 58px; margin: -29px 0 0 -29px; }
  .atomling { width: 50px; height: 50px; }
  .atomling .sym { font-size: 1rem; top: 7px; }
  .board { aspect-ratio: 1 / 1; }
  .scene { aspect-ratio: 4 / 3; }
  .controls .btn { flex: 1 1 40%; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
