:root {
  --ink: #2a2140;
  --paper: #fff8ec;
  --pink: #ff5d73;
  --yellow: #ffd23f;
  --teal: #3bceac;
  --blue: #4d9de0;
  --purple: #8a5cf6;
  --green: #2fbf71;
  --orange: #ff9f1c;
  --k: 1;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: 'Fredoka', 'Trebuchet MS', system-ui, sans-serif;
  color: var(--ink);
  background: #2a2140;
  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; }

.screen { position: fixed; inset: 0; display: none; }
.screen.active { display: block; }

/* ---------- menu ---------- */
#menu.active {
  display: flex; flex-direction: column; align-items: center;
  overflow-y: auto; padding: 24px 16px 16px;
  background: radial-gradient(circle at 20% 10%, #5b3fa8 0, transparent 50%),
              radial-gradient(circle at 90% 80%, #c2407a 0, transparent 45%), #2a2140;
  color: #fff;
}
.menu-head { text-align: center; margin-bottom: 18px; }
.menu-head h1 { font-size: clamp(2.2rem, 6vw, 3.6rem); margin: 0; letter-spacing: 0.5px; text-shadow: 0 4px 0 rgba(0,0,0,0.25); }
.menu-head h1 span { color: var(--yellow); }
.menu-head p { margin: 6px 0 0; font-size: 1.15rem; opacity: 0.9; }
.mode-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 20px; width: min(1000px, 100%); }
.mode-card {
  background: var(--paper); color: var(--ink); border-radius: 26px; padding: 20px;
  box-shadow: 0 10px 0 rgba(0,0,0,0.25); display: flex; flex-direction: column; align-items: center; text-align: center;
}
.mode-card h2 { font-size: 2rem; margin: 6px 0 4px; }
.mode-card p { margin: 0 0 14px; font-size: 1.1rem; line-height: 1.35; }
.mode-art { width: 100%; max-width: 420px; height: auto; border-radius: 16px; }
.diff-row { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }
.diff-btn, .big-btn {
  border: 0; border-radius: 18px; padding: 14px 22px; font-size: 1.35rem; font-weight: 700; color: #fff;
  box-shadow: 0 6px 0 rgba(0,0,0,0.25); transition: transform 0.08s;
}
.diff-btn:active, .big-btn:active { transform: translateY(4px); box-shadow: 0 2px 0 rgba(0,0,0,0.25); }
.diff-btn.easy { background: var(--green); }
.diff-btn.medium { background: var(--orange); }
.diff-btn.hard { background: var(--pink); }
.big-btn { background: var(--purple); }
.best { font-size: 0.95rem !important; margin-top: 12px !important; opacity: 0.75; min-height: 1.3em; }
.collection-title { margin: 18px 0 8px; font-size: 1.1rem; }
.collection { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; width: 100%; max-width: 420px; }
.collection canvas { width: 100%; height: auto; border-radius: 10px; display: block; }
.collection .slot { position: relative; }
.collection .slot.locked canvas { filter: brightness(0) opacity(0.22); }
.collection .slot.locked::after { content: '?'; position: absolute; inset: 0; display: grid; place-items: center; font-size: 1.6rem; font-weight: 700; color: #8a7fa8; }
.menu-foot { display: flex; gap: 18px; align-items: center; margin-top: 22px; padding-bottom: 8px; }
.menu-foot a { color: #fff; opacity: 0.8; }
.sound-btn { background: rgba(255,255,255,0.15); border: 0; border-radius: 12px; padding: 8px 14px; color: #fff; }

/* ---------- shared bits ---------- */
.icon-btn {
  width: 48px; height: 48px; border-radius: 14px; border: 0; background: rgba(255,255,255,0.9);
  font-size: 1.3rem; font-weight: 700; box-shadow: 0 4px 0 rgba(0,0,0,0.25); flex: none;
}
.overlay {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  background: rgba(30, 20, 60, 0.55); z-index: 20; overflow: hidden;
}
.panel {
  background: var(--paper); border-radius: 28px; padding: 26px 30px; text-align: center; max-width: min(92vw, 520px);
  box-shadow: 0 12px 0 rgba(0,0,0,0.3); animation: pop-in 0.35s cubic-bezier(.2,1.6,.5,1);
}
.panel h2 { font-size: 2.2rem; margin: 0 0 6px; }
.panel p { font-size: 1.2rem; margin: 6px 0; }
.panel .btn-row { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; margin-top: 18px; }
.panel .big-btn.alt { background: var(--blue); }
.panel img { width: min(70vw, 340px); border-radius: 18px; display: block; margin: 10px auto; box-shadow: 0 6px 0 rgba(0,0,0,0.15); }
@keyframes pop-in { from { transform: scale(0.6); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.confetti {
  position: absolute; top: -20px; width: 12px; height: 18px; border-radius: 3px; pointer-events: none;
  animation: confetti-fall linear forwards;
}
@keyframes confetti-fall {
  to { transform: translate(var(--drift), 110vh) rotate(720deg); }
}

/* ---------- speedway ---------- */
#speedCanvas { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; display: block; }
.hud { position: absolute; top: 0; left: 0; right: 0; display: flex; align-items: center; gap: 12px; padding: 10px 12px; z-index: 5; }
.health { display: flex; align-items: center; gap: 8px; background: rgba(255,255,255,0.9); border-radius: 14px; padding: 6px 12px; box-shadow: 0 4px 0 rgba(0,0,0,0.2); }
.health-label { font-weight: 700; font-size: 1rem; }
.health-bar { display: flex; gap: 4px; }
.health-bar i { display: block; width: clamp(18px, 4vw, 34px); height: 20px; border-radius: 6px; background: #ddd; transition: background 0.3s, transform 0.3s; }
.health-bar i.on { background: var(--green); }
.health-bar.mid i.on { background: var(--orange); }
.health-bar.low i.on { background: var(--pink); animation: throb 0.8s infinite; }
.health-bar i.lost { animation: lose 0.6s; }
.health-bar i.gain { animation: gain 0.6s; }
@keyframes throb { 50% { transform: scale(1.12); } }
@keyframes lose { 30% { transform: scale(1.5) rotate(-12deg); background: #222; } }
@keyframes gain { 40% { transform: scale(1.5); background: var(--yellow); } }
.score-box { margin-left: auto; background: rgba(255,255,255,0.9); border-radius: 14px; padding: 6px 14px; font-size: 1.5rem; font-weight: 700; box-shadow: 0 4px 0 rgba(0,0,0,0.2); }
.score-star { color: #f5a400; }
.speed-question {
  position: absolute; top: 70px; left: 50%; transform: translateX(-50%); z-index: 4;
  background: #fff; border-radius: 22px; padding: 6px 30px; font-size: clamp(2.4rem, 8vw, 4.2rem); font-weight: 700;
  box-shadow: 0 6px 0 rgba(0,0,0,0.2); white-space: nowrap; pointer-events: none;
}
.speed-question:empty { display: none; }
.speed-question.right { animation: q-right 0.5s; }
.speed-question.wrong { animation: q-wrong 0.5s; }
@keyframes q-right { 40% { transform: translateX(-50%) scale(1.15); background: #c9f7d9; } }
@keyframes q-wrong { 20%, 60% { transform: translateX(-54%); background: #ffd2d8; } 40%, 80% { transform: translateX(-46%); } }
.speed-hint { position: absolute; bottom: 12px; left: 50%; transform: translateX(-50%); color: #fff; font-size: 1rem; opacity: 0.85; z-index: 4; pointer-events: none; text-shadow: 0 2px 2px rgba(0,0,0,0.5); white-space: nowrap; }
.diff-pick { display: flex; gap: 8px; justify-content: center; margin-top: 10px; }
.diff-pick .diff-btn { font-size: 1.05rem; padding: 10px 16px; }

/* ---------- puzzle ---------- */
#puzzle.active {
  display: flex; flex-direction: column;
  background: linear-gradient(160deg, #ffe7c2, #ffd1dc 60%, #e3d4ff);
}
.puzzle-top { display: flex; align-items: center; gap: 12px; padding: 10px 12px; flex-wrap: wrap; }
.puzzle-title { font-size: 1.5rem; font-weight: 700; }
.puzzle-progress { font-size: 1.05rem; background: rgba(255,255,255,0.7); padding: 6px 12px; border-radius: 12px; }
.hint-toggle { margin-left: auto; font-size: 1rem; display: flex; align-items: center; gap: 6px; background: rgba(255,255,255,0.7); padding: 6px 12px; border-radius: 12px; cursor: pointer; }
.hint-toggle input { width: 20px; height: 20px; }
.puzzle-layout {
  flex: 1; min-height: 0; display: grid; gap: 12px; padding: 0 12px 12px;
  grid-template-columns: clamp(220px, 24vw, 300px) minmax(0, 1fr) clamp(170px, 22vw, 280px);
  grid-template-rows: minmax(0, 1fr);
  grid-template-areas: 'ask board tray';
}
.ask-panel { grid-area: ask; background: #fff; border-radius: 24px; padding: 16px; display: flex; flex-direction: column; align-items: stretch; gap: 10px; box-shadow: 0 6px 0 rgba(0,0,0,0.12); align-self: start; }
.ask-question { font-size: clamp(2.4rem, 5vw, 3.4rem); font-weight: 700; text-align: center; white-space: nowrap; }
.ask-display {
  height: 72px; border-radius: 16px; border: 4px solid var(--purple); background: #f6f2ff;
  font-size: 2.8rem; font-weight: 700; display: flex; align-items: center; justify-content: center; letter-spacing: 2px;
}
.ask-display.empty::after { content: '?'; color: #c4b6ee; }
.ask-display.right { border-color: var(--green); background: #dff8e8; animation: pop-in 0.3s; }
.ask-display.wrong { border-color: var(--pink); background: #ffe1e6; animation: q-wrong-small 0.4s; }
@keyframes q-wrong-small { 25%, 75% { transform: translateX(-6px); } 50% { transform: translateX(6px); } }
.ask-feedback { min-height: 2.6em; text-align: center; font-size: 1.15rem; line-height: 1.3; }
.ask-feedback.good { color: #13894b; font-weight: 700; }
.ask-feedback.bad { color: #c23a52; }
.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.keypad button {
  border: 0; border-radius: 16px; background: #efe8ff; font-size: 1.8rem; font-weight: 700; height: 62px;
  box-shadow: 0 5px 0 #c9bbf0; transition: transform 0.06s;
}
.keypad button:active { transform: translateY(4px); box-shadow: 0 1px 0 #c9bbf0; }
.keypad button.go { background: var(--green); color: #fff; box-shadow: 0 5px 0 #1b8a50; }
.keypad button.del { background: #ffe1e6; box-shadow: 0 5px 0 #f0b0bb; }
.ask-done { text-align: center; font-size: 1.3rem; padding: 12px 6px; }
.ask-done b { display: block; font-size: 2rem; margin-bottom: 6px; }

.board-wrap { grid-area: board; min-width: 0; min-height: 0; overflow: hidden; display: flex; align-items: center; justify-content: center; }
.board {
  position: relative; width: 500px; height: 500px; flex: none;
  background: rgba(255,255,255,0.55); border-radius: 6px;
  box-shadow: 0 0 0 6px #fff, 0 10px 0 6px rgba(0,0,0,0.12);
  background-image:
    linear-gradient(rgba(120,90,160,0.18) 2px, transparent 2px),
    linear-gradient(90deg, rgba(120,90,160,0.18) 2px, transparent 2px);
  background-size: 20% 20%;
}
.board .ghost { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0.2; pointer-events: none; border-radius: 6px; transition: opacity 0.3s; }
.board.no-ghost .ghost { opacity: 0; }
.piece { position: absolute; touch-action: none; cursor: grab; filter: drop-shadow(0 3px 2px rgba(0,0,0,0.3)); }
.board .piece.placed { cursor: default; filter: none; pointer-events: none; }
.board .piece.placed.just { animation: snap-glow 0.6s; }
@keyframes snap-glow { 40% { filter: drop-shadow(0 0 10px #fff) brightness(1.15); } }
.piece.dragging { position: fixed; z-index: 100; cursor: grabbing; filter: drop-shadow(0 10px 8px rgba(0,0,0,0.35)); }
.piece.near { filter: drop-shadow(0 0 8px #3bceac) drop-shadow(0 0 4px #3bceac); }

.tray-wrap { grid-area: tray; background: rgba(255,255,255,0.6); border-radius: 24px; padding: 12px; display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.tray-wrap h3 { margin: 0 0 8px; font-size: 1.2rem; text-align: center; }
.tray { flex: 1; min-height: 0; overflow-y: auto; display: flex; flex-wrap: wrap; align-content: flex-start; justify-content: center; gap: 2px; }
.tray .piece { position: relative; width: min(calc(148px * var(--k) * 0.78), calc(50% - 2px)); height: auto; aspect-ratio: 1; }
.tray .piece.new { animation: pop-in 0.45s cubic-bezier(.2,1.6,.5,1); }
.tray .piece.bounce { animation: q-wrong-small 0.4s; }
.tray-empty { text-align: center; opacity: 0.7; font-size: 1.05rem; padding: 10px; }

/* portrait and narrow screens: stack it */
@media (max-aspect-ratio: 1/1), (max-width: 820px) {
  .puzzle-layout {
    grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-areas: 'ask' 'board' 'tray'; gap: 8px; padding: 0 8px 8px;
  }
  .ask-panel { display: grid; grid-template-columns: auto 1fr; grid-template-areas: 'q d' 'f f' 'k k'; padding: 10px; gap: 6px 10px; align-self: stretch; }
  .ask-question { grid-area: q; font-size: 2.2rem; }
  .ask-display { grid-area: d; height: 56px; font-size: 2.2rem; }
  .ask-feedback { grid-area: f; min-height: 1.4em; font-size: 1rem; }
  .keypad { grid-area: k; grid-template-columns: repeat(6, 1fr); }
  .keypad button { height: 50px; font-size: 1.5rem; }
  .ask-done { grid-column: 1 / -1; }
  .tray-wrap { padding: 8px; }
  .tray-wrap h3 { font-size: 1rem; margin-bottom: 4px; }
  .tray { flex-wrap: nowrap; overflow-x: auto; overflow-y: hidden; justify-content: flex-start; height: calc(148px * var(--k) * 0.78 + 6px); flex: none; }
  .tray .piece { flex: none; width: calc(148px * var(--k) * 0.78); }
  .hint-toggle { margin-left: 0; }
}

@media (max-width: 600px) {
  .puzzle-top { gap: 6px; padding: 8px; }
  .puzzle-top .icon-btn, .hud .icon-btn { width: 40px; height: 40px; font-size: 1.1rem; }
  .puzzle-title { font-size: 1.05rem; flex: 1; min-width: 0; }
  .puzzle-progress, .hint-toggle { font-size: 0.85rem; padding: 4px 8px; }
  .hud { gap: 6px; padding: 8px; }
  .health { padding: 4px 8px; }
  .health-label { display: none; }
  .score-box { font-size: 1.2rem; padding: 4px 10px; }
  .speed-question { top: 60px; }
  .speed-hint { font-size: 0.85rem; }
}
