:root {
  --bg: #1a1238;
  --panel: #2b2065;
  --panel2: #3a2d85;
  --ink: #ffffff;
  --muted: #cfc8ff;
  --yellow: #ffd23f;
  --yellow-dk: #c99a00;
  --red: #ff4f6d;
  --green: #5ad15a;
  --orange: #ff9f1c;
  --edge: #0d0920;
  --pixel: 'Press Start 2P', ui-monospace, monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  min-height: 100vh;
  color: var(--ink);
  font-family: 'Nunito', system-ui, sans-serif;
  font-weight: 800;
  background-color: var(--bg);
  background-image:
    radial-gradient(2px 2px at 20px 30px, #fff8, transparent),
    radial-gradient(2px 2px at 140px 90px, #fff6, transparent),
    radial-gradient(2px 2px at 260px 160px, #fff8, transparent),
    radial-gradient(1px 1px at 80px 200px, #fff9, transparent),
    radial-gradient(1px 1px at 200px 250px, #fff7, transparent);
  background-size: 300px 300px;
  -webkit-tap-highlight-color: transparent;
}
body.night { --bg: #0b0b24; --panel: #1b1d4a; --panel2: #2a2d6b; }

#app { max-width: 640px; margin: 0 auto; padding: 16px 16px 56px; }
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: default; }
.hidden { display: none !important; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-block;
  font-family: var(--pixel);
  font-size: 14px;
  line-height: 1.4;
  padding: 16px 22px;
  color: var(--edge);
  background: var(--yellow);
  border: 0;
  text-decoration: none;
  box-shadow: inset -4px -4px 0 var(--yellow-dk), 0 0 0 4px var(--edge), 0 8px 0 4px var(--edge);
  margin: 8px 4px 16px;
  transition: transform .06s;
}
.btn:active { transform: translateY(4px); box-shadow: inset -4px -4px 0 var(--yellow-dk), 0 0 0 4px var(--edge), 0 4px 0 4px var(--edge); }
.btn.ghost { background: var(--panel2); color: #fff; box-shadow: inset -4px -4px 0 #0004, 0 0 0 4px var(--edge), 0 8px 0 4px var(--edge); }
.btn.small { font-size: 11px; padding: 12px 14px; }
.btn.green { background: var(--green); box-shadow: inset -4px -4px 0 #2f8a3a, 0 0 0 4px var(--edge), 0 8px 0 4px var(--edge); }

/* ---------- Top bar ---------- */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 16px; }
.tb-title { font-family: var(--pixel); font-size: 11px; color: var(--muted); text-align: center; flex: 1; }
.iconbtn {
  width: 44px; height: 44px; border: 0; background: var(--panel); font-size: 18px;
  box-shadow: 0 0 0 3px var(--edge); display: grid; place-items: center;
}

/* ---------- Title ---------- */
.title { text-align: center; padding-top: 24px; }
.logo {
  font-family: var(--pixel);
  font-size: clamp(24px, 8vw, 40px);
  line-height: 1.35;
  color: var(--yellow);
  text-shadow: 4px 4px 0 var(--red), 8px 8px 0 var(--edge);
  margin: 20px 0 12px;
}
.logo.small { font-size: clamp(18px, 6vw, 28px); }
.bday { font-family: var(--pixel); font-size: 11px; line-height: 1.8; color: var(--muted); margin: 8px 0 28px; }
.start { font-size: 16px; padding: 20px 28px; }
.blink { animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: .55; } }
.bob { animation: bob 1.2s ease-in-out infinite; display: inline-block; }
@keyframes bob { 50% { transform: translateY(-8px); } }
.gobbo { display: block; image-rendering: pixelated; }

/* ---------- Gobbo talking ---------- */
.talk { display: flex; align-items: flex-start; gap: 12px; margin: 8px 0 20px; }
.talk .who { flex: none; }
.bubble {
  position: relative; flex: 1;
  background: #fff; color: #1a1238;
  padding: 14px 16px; font-size: 19px; line-height: 1.4;
  box-shadow: 0 0 0 4px var(--edge);
}
.bubble::before {
  content: ''; position: absolute; left: -14px; top: 18px;
  border: 7px solid transparent; border-right-color: #fff;
}
.speak { flex: none; width: 48px; height: 48px; border: 0; background: var(--orange); font-size: 22px; box-shadow: 0 0 0 4px var(--edge); }

/* ---------- Map ---------- */
.world {
  background: var(--panel); padding: 18px 16px 8px; margin: 0 0 28px;
  box-shadow: 0 0 0 4px var(--edge), 0 8px 0 4px var(--edge);
}
.world.w2 { background: linear-gradient(#1b1d4a, #2a1650); }
.world.locked { opacity: .75; text-align: center; padding-bottom: 18px; }
.wsub { font-family: var(--pixel); font-size: 10px; color: var(--orange); }
.wname { font-family: var(--pixel); font-size: 17px; line-height: 1.5; color: var(--yellow); margin: 8px 0 6px; }
.wtag { margin: 0 0 14px; color: var(--muted); font-size: 17px; }
.toy-head { font-family: var(--pixel); font-size: 10px; color: var(--yellow); text-align: center; margin: 6px 0 10px; }
.toy-row { display: flex; gap: 8px; justify-content: center; margin: 0 0 16px; flex-wrap: wrap; }
.toy-row .slot {
  position: relative; width: 48px; height: 48px; display: grid; place-items: center; font-size: 26px;
  background: #0004; box-shadow: inset 0 0 0 3px #0006;
}
/* Not found yet: a dark shadow of the toy. */
.toy-row .slot .toy { filter: grayscale(1) brightness(0.25); opacity: 0.55; }
/* Found: full color, gold frame, check mark. */
.toy-row .slot.got { background: #ffd23f40; box-shadow: inset 0 0 0 3px var(--yellow), 0 0 12px #ffd23f66; }
.toy-row .slot.got .toy { filter: none; opacity: 1; animation: pop .4s; }
.toy-row .slot.got::after {
  content: '✓'; position: absolute; right: -6px; bottom: -6px; width: 20px; height: 20px;
  display: grid; place-items: center; font: 900 13px/1 'Nunito', sans-serif;
  background: var(--green); color: var(--edge); box-shadow: 0 0 0 2px var(--edge);
}
.path { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-bottom: 12px; }
.node {
  aspect-ratio: 1; border: 0; background: var(--panel2);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
  box-shadow: 0 0 0 4px var(--edge), 0 6px 0 4px var(--edge);
}
.node:not(:disabled):active { transform: translateY(3px); }
.node:disabled { opacity: .45; }
.node.done { background: #2f7a3a; }
.node.boss { background: #7a2040; }
.node.boss.done { background: #2f7a3a; }
.node.prize.ready { background: var(--yellow); color: var(--edge); animation: glow 1s ease-in-out infinite; }
.node .nid { font-family: var(--pixel); font-size: 10px; }
.node .nicon { font-size: 30px; }
@keyframes glow { 50% { box-shadow: 0 0 0 4px var(--edge), 0 6px 0 4px var(--edge), 0 0 24px 8px #ffd23f88; } }

/* ---------- Level ---------- */
.lvhead { text-align: center; }
.lvid { font-family: var(--pixel); font-size: 11px; color: var(--orange); }
.lvhead h2 { font-family: var(--pixel); font-size: 17px; line-height: 1.5; color: var(--yellow); margin: 10px 0 4px; }
.puzzle {
  background: var(--panel); padding: 18px 14px; text-align: center;
  box-shadow: 0 0 0 4px var(--edge), 0 8px 0 4px var(--edge); margin-bottom: 24px;
}
.prompt { font-size: 21px; margin: 0 0 16px; }
.shake { animation: shake .4s; }
@keyframes shake { 20%, 60% { transform: translateX(-8px); } 40%, 80% { transform: translateX(8px); } }
@keyframes pop { 0% { transform: scale(.4); } 70% { transform: scale(1.2); } 100% { transform: scale(1); } }
.hint {
  background: #ffd23f22; border-left: 6px solid var(--yellow);
  padding: 12px 14px; font-size: 18px; line-height: 1.4; margin-bottom: 12px; animation: pop .3s;
}
.center { text-align: center; }

.visual { margin: 0 0 16px; font-size: 18px; line-height: 1.5; }
.visual .big { font-size: 40px; letter-spacing: 6px; }
.visual .note { display: inline-block; background: #f4e3b5; color: #3a2400; padding: 10px 18px; box-shadow: 0 0 0 4px var(--edge); margin-bottom: 14px; }
.visual .note .big { white-space: nowrap; font-size: 32px; letter-spacing: 2px; }
.legend-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.legend-grid span { background: #0003; padding: 6px; font-size: 20px; }
.decoder { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; font-family: var(--pixel); font-size: 10px; }
.decoder span { background: #0003; padding: 6px 0; line-height: 1.8; }
.decoder b { color: var(--yellow); }
.clues { text-align: left; background: #0003; padding: 10px 14px; }
.clues p { margin: 6px 0; }

/* sequence + options */
.seq { display: flex; justify-content: center; flex-wrap: wrap; gap: 8px; margin-bottom: 18px; }
.seqi { min-width: 52px; height: 56px; padding: 0 6px; display: grid; place-items: center; font-size: 30px; background: #0003; font-family: var(--pixel); }
.seqi.num { font-size: 18px; }
.seqi.q { background: var(--yellow); color: var(--edge); animation: blink 1s steps(2) infinite; font-size: 24px; }
.opts { display: flex; justify-content: center; flex-wrap: wrap; gap: 14px; }
.opt {
  min-width: 84px; min-height: 72px; padding: 8px 14px; border: 0; font-size: 32px;
  background: var(--panel2); box-shadow: 0 0 0 4px var(--edge), 0 6px 0 4px var(--edge);
}
.opt.word { font-family: var(--pixel); font-size: 13px; }
.opt.num { font-family: var(--pixel); font-size: 18px; }
.opt.nope { opacity: .3; text-decoration: line-through; }

/* number pad */
.numdisp {
  font-family: var(--pixel); font-size: 32px; background: #000a; color: var(--green);
  width: 160px; margin: 0 auto 16px; padding: 14px 0; box-shadow: 0 0 0 4px var(--edge);
}
.keypad { display: grid; grid-template-columns: repeat(3, 72px); gap: 12px; justify-content: center; }
.key {
  height: 60px; border: 0; background: var(--panel2); font-family: var(--pixel); font-size: 18px;
  box-shadow: 0 0 0 3px var(--edge), 0 5px 0 3px var(--edge);
}
.key:active { transform: translateY(3px); }
.key.ok { background: var(--green); color: var(--edge); font-size: 14px; }

/* memory */
.mem { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; max-width: 400px; margin: 0 auto; }
.card { position: relative; aspect-ratio: 3/4; border: 0; padding: 0; background: none; perspective: 600px; }
.card span {
  position: absolute; inset: 0; display: grid; place-items: center; font-size: 34px;
  backface-visibility: hidden; transition: transform .3s; box-shadow: 0 0 0 3px var(--edge);
}
.card .back { background: var(--red); font-family: var(--pixel); font-size: 20px; color: #fff; }
.card .front { background: #fff; transform: rotateY(180deg); }
.card.flip .back { transform: rotateY(180deg); }
.card.flip .front { transform: rotateY(0); }
.card.matched .front { background: #c8f7c5; }

/* letter tiles */
.slots { display: flex; justify-content: center; gap: 8px; margin-bottom: 22px; flex-wrap: wrap; }
.slots .slot {
  width: 52px; height: 60px; border: 0; background: #0005; font-family: var(--pixel); font-size: 22px;
  box-shadow: inset 0 -4px 0 var(--muted);
}
.slots .slot.filled { background: #fff; color: var(--edge); box-shadow: 0 0 0 3px var(--edge); }
.pool { display: flex; justify-content: center; flex-wrap: wrap; gap: 12px; }
.tile {
  width: 56px; height: 60px; border: 0; background: var(--yellow); color: var(--edge);
  font-family: var(--pixel); font-size: 22px; box-shadow: inset -3px -3px 0 var(--yellow-dk), 0 0 0 3px var(--edge), 0 5px 0 3px var(--edge);
}
.tile:disabled { visibility: hidden; }
.undo { margin-top: 22px; margin-bottom: 4px; }
.undo:disabled { opacity: .35; }

/* sudoku */
.sudoku { display: grid; grid-template-columns: repeat(2, auto); gap: 12px; justify-content: center; margin: 0 auto 16px; }
.sbox {
  display: grid; grid-template-columns: repeat(2, 64px); gap: 5px; padding: 7px;
  background: var(--yellow); box-shadow: 0 0 0 4px var(--edge);
}
.sbox.alt { background: #6fd3ff; }
.cell { width: 64px; height: 64px; border: 0; background: #fff; font-size: 32px; box-shadow: 0 0 0 2px var(--edge); }
.cell.given { background: #e6e1ff; cursor: default; }
.legend { font-size: 18px; margin-bottom: 14px; }

/* ---------- Overlays ---------- */
.overlay { position: fixed; inset: 0; background: #0d0920d9; display: grid; place-items: center; padding: 16px; z-index: 10; }
.overlay .panel {
  background: var(--panel); text-align: center; padding: 26px 20px 14px; max-width: 420px; width: 100%;
  box-shadow: 0 0 0 4px var(--edge), 0 0 0 8px var(--yellow); animation: pop .35s;
}
.overlay h2 { font-family: var(--pixel); color: var(--yellow); font-size: 20px; line-height: 1.5; margin: 0 0 12px; }
.overlay .reward { font-size: 72px; animation: pop .6s; }
.overlay p { font-size: 20px; }

/* ---------- Treasure ---------- */
.treasure { text-align: center; padding-top: 8px; }
.treasure h2 { font-family: var(--pixel); font-size: 14px; color: var(--yellow); line-height: 1.6; }
.prizebox {
  background: #fff; color: var(--edge); padding: 20px 14px; margin: 8px 0 24px;
  box-shadow: 0 0 0 4px var(--edge), 0 0 0 10px var(--yellow), 0 0 0 14px var(--edge);
}
.plabel { font-family: var(--pixel); font-size: 10px; color: #7a5a00; margin-bottom: 14px; }
.code {
  font-family: ui-monospace, Consolas, monospace; font-size: clamp(20px, 6vw, 30px); font-weight: 700;
  letter-spacing: 2px; word-break: break-all; background: #fff7d6; padding: 14px 8px; border: 3px dashed #c99a00; user-select: all;
}
.letter { background: #f4e3b5; color: #3a2400; padding: 16px 18px; text-align: left; font-size: 19px; line-height: 1.5; box-shadow: 0 0 0 4px var(--edge); margin-bottom: 24px; white-space: pre-wrap; }
.letter .sig { text-align: right; margin-bottom: 0; }
.teaser { background: #0b0b24; padding: 18px 14px 6px; box-shadow: 0 0 0 4px var(--edge), 0 0 0 8px #6a5cff; }
.teaser h3 { font-family: var(--pixel); font-size: 12px; line-height: 1.7; color: #b9b0ff; margin: 0 0 6px; }

/* ---------- Extended mode: time trials ---------- */
.world.trials { background: linear-gradient(#2b2065, #46206b); padding-bottom: 16px; }
.trial {
  display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap;
  width: 100%; margin: 0 0 14px; padding: 16px 14px; border: 0; text-align: left;
  background: var(--orange); color: var(--edge);
  box-shadow: inset -4px -4px 0 #b86a00, 0 0 0 4px var(--edge), 0 6px 0 4px var(--edge);
}
.trial.night { background: #8a7bff; box-shadow: inset -4px -4px 0 #5a4bd1, 0 0 0 4px var(--edge), 0 6px 0 4px var(--edge); }
.trial:disabled { opacity: .45; }
.trial:not(:disabled):active { transform: translateY(3px); }
.trial .tname { font-family: var(--pixel); font-size: 13px; }
.trial .tbest { font-family: var(--pixel); font-size: 10px; }
.trial .tbests { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; width: 100%; min-width: 0; }
.trial .tbests span { min-width: 0; background: #0002; padding: 6px 2px; font-family: var(--pixel); font-size: 7px; line-height: 1.8; text-align: center; overflow-wrap: anywhere; }
.trial .tbests b { display: block; font-size: 11px; }
.trial .tbests .ti { display: block; font-style: normal; font-size: 18px; line-height: 1.3; }
.pickhead { font-family: var(--pixel); font-size: 11px; color: var(--orange); text-align: center; margin: 0 0 12px; }
.lenpick { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-bottom: 24px; }
.len {
  display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 12px 4px; border: 0;
  background: var(--panel2); box-shadow: 0 0 0 4px var(--edge), 0 6px 0 4px var(--edge);
}
.len.on { background: var(--yellow); color: var(--edge); box-shadow: inset -4px -4px 0 var(--yellow-dk), 0 0 0 4px var(--edge), 0 6px 0 4px var(--edge); }
.len .licon { font-size: 28px; }
.len .lname { font-family: var(--pixel); font-size: 10px; }
.len .ln { font-size: 14px; }
.timer {
  font-family: var(--pixel); font-size: 13px; color: var(--green);
  background: #000a; padding: 10px 8px; min-width: 84px; text-align: center; box-shadow: 0 0 0 3px var(--edge);
}
.countdown {
  font-family: var(--pixel); font-size: 96px; color: var(--yellow); margin-top: 30vh;
  text-shadow: 6px 6px 0 var(--red); animation: pop .5s;
}
.bigtime {
  font-family: var(--pixel); font-size: clamp(34px, 12vw, 56px); color: var(--green);
  background: #000a; display: inline-block; padding: 16px 20px; margin: 4px 0 8px; box-shadow: 0 0 0 4px var(--edge);
}
.questtime { font-family: var(--pixel); font-size: 11px; color: var(--green); margin: -12px 0 22px; }
.records { background: var(--panel); padding: 16px; margin: 0 0 20px; text-align: left; box-shadow: 0 0 0 4px var(--edge); }
.records h3 { font-family: var(--pixel); font-size: 12px; color: var(--yellow); margin: 0 0 12px; text-align: center; }
.records ol { list-style: none; margin: 0; padding: 0; }
.records li { display: flex; align-items: center; gap: 12px; padding: 8px 10px; font-size: 18px; }
.records li:nth-child(odd) { background: #0002; }
.records li.new { background: #ffd23f33; box-shadow: inset 0 0 0 3px var(--yellow); animation: pop .5s; }
.records .medal { width: 30px; text-align: center; font-family: var(--pixel); font-size: 14px; }
.records b { font-family: var(--pixel); font-size: 15px; flex: 1; }
.records small { color: var(--muted); }
.records p { text-align: center; color: var(--muted); margin: 0; }
.trial-teaser { margin-top: 24px; box-shadow: 0 0 0 4px var(--edge), 0 0 0 8px var(--orange); }
.toast {
  position: fixed; left: 50%; top: 40%; transform: translateX(-50%); z-index: 15; pointer-events: none;
  font-family: var(--pixel); font-size: 20px; color: var(--edge); background: var(--green);
  padding: 16px 20px; box-shadow: 0 0 0 4px var(--edge); animation: pop .3s;
}

#confetti { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 20; }

@media (max-width: 380px) {
  .keypad { grid-template-columns: repeat(3, 64px); }
  .sudoku { gap: 10px; }
  .sbox { grid-template-columns: repeat(2, 56px); gap: 4px; padding: 6px; }
  .cell { width: 56px; height: 56px; font-size: 28px; }
  .slots .slot, .tile { width: 46px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
