:root {
  --bg: #fdf6e9;
  --ink: #2b2d42;
  --soft: #6c6f85;
  --card: #ffffff;
  --accent: #ffb703;
  --go: #2a9d8f;
  --bad: #e63946;
  --radius: 16px;
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", "Varela Round", system-ui, sans-serif;
  color: var(--ink);
}
* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: radial-gradient(circle at 20% 0%, #fff8e1, var(--bg) 60%); }
body { -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

/* ---------- buttons */
.btn {
  font: inherit; font-size: 1rem; font-weight: 700;
  border: none; border-radius: 12px; padding: 0.6em 1em;
  background: #fff; color: var(--ink); cursor: pointer;
  box-shadow: 0 3px 0 #d9cdb5, 0 4px 10px rgba(0,0,0,.06);
  transition: transform .08s;
}
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 #d9cdb5; }
.btn:disabled { opacity: .45; cursor: default; }
.btn.go { background: var(--go); color: #fff; box-shadow: 0 3px 0 #1f7a6f; }
.btn.on { background: var(--accent); }
.btn.ghost { background: transparent; box-shadow: none; }

/* ---------- home */
.hero { text-align: center; padding: 2rem 1rem 0.5rem; }
.hero h1 { font-size: clamp(2.2rem, 6vw, 3.4rem); margin: 0; }
.hero p { color: var(--soft); font-size: 1.15rem; margin: .4rem 0 0; }
#levels { max-width: 980px; margin: 0 auto; padding: 0 1rem 3rem; }
#levels h3 { margin: 1.8rem 0 .7rem; color: var(--soft); text-transform: uppercase; letter-spacing: .06em; font-size: .9rem; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 14px; }
.lcard {
  font: inherit; text-align: left; border: none; cursor: pointer;
  background: var(--card); border-radius: var(--radius); padding: 14px 16px;
  box-shadow: 0 4px 0 #e6dac2, 0 6px 16px rgba(0,0,0,.06);
  display: flex; flex-direction: column; gap: 4px; transition: transform .1s;
}
.lcard:hover { transform: translateY(-2px); }
.lcard .num { font-size: 1.6rem; font-weight: 800; color: var(--accent); }
.lcard .ltitle { font-weight: 800; font-size: 1.1rem; }
.lcard .kind { color: var(--soft); font-size: .85rem; }
.lcard.locked { opacity: .5; cursor: default; }
.lcard.locked:hover { transform: none; }
.stars { color: var(--accent); letter-spacing: 2px; font-size: 1.1rem; }

/* ---------- play */
#play { display: flex; flex-direction: column; height: 100dvh; }
.bar { display: flex; align-items: center; gap: 12px; padding: 8px 12px; }
.bar h2 { margin: 0; flex: 1; font-size: 1.3rem; }
.stage { flex: 1; min-height: 0; display: grid; grid-template-columns: 1fr minmax(280px, 340px); gap: 12px; padding: 0 12px 12px; }
.boardwrap { position: relative; min-height: 0; display: flex; flex-direction: column; }
#board { flex: 1; min-height: 0; width: 100%; touch-action: none; user-select: none; }
#readout { text-align: center; color: var(--soft); padding: 6px; font-size: 1rem; min-height: 2em; }
#readout b { color: var(--ink); }
#readout .bad { color: var(--bad); }
#readout .of { color: var(--soft); font-size: .9em; }
#toast {
  position: absolute; left: 50%; top: 12px; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: .6em 1.1em; border-radius: 999px;
  font-weight: 700; pointer-events: none; box-shadow: 0 6px 20px rgba(0,0,0,.2);
}

.panel { display: flex; flex-direction: column; gap: 12px; overflow-y: auto; }
#bubble { background: var(--card); border-radius: var(--radius); padding: 14px 16px; box-shadow: 0 4px 0 #e6dac2; font-size: 1.08rem; line-height: 1.45; }
#bubble p { margin: 0 0 .7em; }
#bubble .stepno { font-size: .8rem; color: var(--soft); font-weight: 700; text-transform: uppercase; letter-spacing: .05em; margin-bottom: .3em; }
#bubble .goal { background: #fff3cd; border-radius: 10px; padding: .5em .7em; }
#bubble .tip { color: var(--soft); font-size: .95rem; }
#bubble .fb { background: #eef6ff; border-radius: 10px; padding: .5em .7em; }
#bubble .fb.good { background: #e3f7ee; }
#bubble .fb.bad { background: #fff0e6; }
.options { display: flex; flex-direction: column; gap: 8px; margin-bottom: .7em; }
.btn.opt { text-align: left; }
.btn.opt.right { background: #b7efc5; }
.btn.opt.wrong { background: #ffc9c9; }

#layerpick { display: flex; align-items: center; gap: 8px; font-weight: 700; }
#tray { display: flex; flex-wrap: wrap; gap: 10px; background: #f3e9d6; border-radius: var(--radius); padding: 10px; }
.trayitem {
  font: inherit; border: none; background: #fff; border-radius: 12px; padding: 8px 10px; cursor: grab;
  display: flex; flex-direction: column; align-items: center; gap: 2px; touch-action: none;
  box-shadow: 0 3px 0 #d9cdb5; position: relative; min-width: 72px;
}
.trayitem:disabled { opacity: .35; cursor: default; }
.trayitem .tname { font-size: .8rem; font-weight: 700; }
.trayitem .count { position: absolute; top: 4px; right: 6px; font-weight: 800; font-size: .85rem; color: var(--soft); }
.trayitem.pulse { animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 50% { box-shadow: 0 3px 0 #d9cdb5, 0 0 0 6px rgba(255,183,3,.45); } }
.controls { display: flex; flex-wrap: wrap; gap: 8px; }

@media (max-width: 860px) {
  #play { height: auto; min-height: 100dvh; }
  .stage { grid-template-columns: 1fr; }
  #board { max-height: 62dvh; }
}

/* ---------- board svg */
.boardbg { fill: #e7c99a; stroke: #c9a46c; stroke-width: 0.06; }
.holes circle { fill: #8a6a3d; opacity: .55; }
.rock circle { fill: #7d8597; stroke: #5c677d; stroke-width: 0.05; }
.rock .shine { fill: #b8c0cc; stroke: none; }
.target { fill: rgba(255,214,102,.35); stroke: #e09f00; stroke-width: 0.06; stroke-dasharray: 0.15 0.1; }
.teeth { stroke: rgba(0,0,0,.35); stroke-width: 0.035; stroke-linejoin: round; }
.web { fill: rgba(255,255,255,.18); stroke: rgba(0,0,0,.18); stroke-width: 0.03; }
.dot { fill: #fff; stroke: rgba(0,0,0,.4); stroke-width: 0.03; }
.hub { fill: #fff; stroke: rgba(0,0,0,.35); stroke-width: 0.04; }
.tlabel { font-size: 0.3px; font-weight: 800; text-anchor: middle; fill: var(--ink); pointer-events: none; font-family: inherit; }
.speedlabel { font-size: 0.32px; font-weight: 800; text-anchor: middle; fill: #fff; paint-order: stroke; stroke: rgba(0,0,0,.55); stroke-width: 0.08px; pointer-events: none; }
.gear.movable { cursor: grab; }
.gear.jam .teeth { stroke: var(--bad); stroke-width: 0.08; }
.jamming .gear.jam .shake { animation: shake .12s linear infinite; }
@keyframes shake { 0%, 100% { transform: translate(0.05px, 0); } 50% { transform: translate(-0.05px, 0.03px); } }
.layer0.dim { opacity: .4; }

.crank { cursor: grab; }
.crank .arm { stroke: #b07d00; stroke-width: 0.2; stroke-linecap: round; }
.crank .pivot { fill: #ffd166; stroke: #b07d00; stroke-width: 0.05; }
.crank .knob { fill: #ffd166; stroke: #b07d00; stroke-width: 0.07; }
.crank .hit { fill: transparent; }
.flag { pointer-events: none; }
.flag .pole { stroke: #3d405b; stroke-width: 0.09; stroke-linecap: round; }
.flag .cloth { fill: #e63946; stroke: #9d0208; stroke-width: 0.03; }
.flag .pivot { fill: #3d405b; }

.stepghost path { fill: rgba(255,255,255,.35); stroke: #3d405b; stroke-width: 0.06; stroke-dasharray: 0.18 0.12; animation: breathe 1.4s ease-in-out infinite; }
.stepghost.answer path { fill: rgba(67,97,238,.12); stroke: #4361ee; animation: none; }
@keyframes breathe { 50% { opacity: .45; } }
.ghost { pointer-events: none; }
.ghostgear { opacity: .75; }
.ghostgear.ok .teeth { stroke: #2a9d8f; stroke-width: 0.1; }
.ghostgear.bad .teeth { stroke: var(--bad); stroke-width: 0.1; }
.ghostgear.bad { opacity: .5; }

/* ---------- modal */
#modal { position: fixed; inset: 0; background: rgba(43,45,66,.45); display: grid; place-items: center; z-index: 10; padding: 1rem; }
#modal .card { background: #fff; border-radius: 22px; padding: 1.6rem 2rem; text-align: center; max-width: 420px; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
#modal h2 { margin: 0 0 .3rem; font-size: 2rem; }
#modal .bigstars { font-size: 2.6rem; letter-spacing: 6px; }
#modal p { font-size: 1.1rem; line-height: 1.4; }
#modal .row { display: flex; gap: 10px; justify-content: center; }

.confetti { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 11; }
.confetti i { position: absolute; top: -12px; width: 10px; height: 14px; border-radius: 2px; animation: fall linear forwards; }
@keyframes fall { to { transform: translateY(105vh) rotate(720deg); } }
