:root {
  --panel: rgba(46, 28, 74, 0.94);
  --panel-2: #3c2862;
  --border: #7a5cb0;
  --ink: #2a1838;
  --text: #fff8ee;
  --muted: #cbb9e8;
  --amber: #ffc53d;
  --green: #6fe08a;
  --red: #ff6b8a;
  --cyan: #4fc3f7;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --font: "Fredoka", ui-rounded, "Arial Rounded MT Bold", "Nunito", "Trebuchet MS", system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #160e26;
  color: var(--text);
  font-family: var(--font);
  overscroll-behavior: none;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

#app { position: fixed; inset: 0; overflow: hidden; }

#game {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  display: block;
  touch-action: none;
}

.hidden { display: none !important; }

/* ------------------------------------------------------------------ HUD */
#hud { position: absolute; inset: 0; pointer-events: none; }

.hud-left {
  position: absolute;
  top: calc(var(--sat) + 8px);
  left: calc(var(--sal) + 8px);
  display: flex; flex-direction: column; gap: 6px; align-items: flex-start;
}
.btn-row { display: flex; gap: 6px; }
.icon-btn {
  pointer-events: auto;
  width: 44px; height: 44px;
  border-radius: 14px;
  border: 2px solid var(--border);
  background: var(--panel);
  color: var(--text);
  display: grid; place-items: center;
  padding: 0;
  touch-action: manipulation;
}
.icon-btn svg { width: 22px; height: 22px; fill: currentColor; }
.icon-btn:active { background: var(--panel-2); }
#btn-mute .snd-off { display: none; }
#btn-mute.muted .snd-on { display: none; }
#btn-mute.muted .snd-off { display: block; }

.stat {
  background: rgba(46, 28, 74, 0.78);
  border: 2px solid var(--border);
  border-radius: 12px;
  padding: 3px 10px 2px;
  font-size: 19px; font-weight: 600; line-height: 1.1;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
}
.stat .lbl { color: var(--muted); font-size: 13px; margin-right: 6px; letter-spacing: 0.04em; }

.knife-ind {
  display: flex; align-items: center; gap: 6px;
  background: rgba(46, 28, 74, 0.6);
  border: 2px dashed #5e4888;
  border-radius: 12px;
  padding: 3px 10px;
  font-size: 14px; font-weight: 600; letter-spacing: 0.03em;
  color: #8a76ad;
  transition: all .2s;
}
.knife-ind svg { width: 32px; height: 16px; opacity: .35; filter: grayscale(1); }
.knife-ind.on {
  color: var(--ink); background: var(--amber); border: 2px solid #fff0b0;
  box-shadow: 0 0 16px rgba(255, 197, 61, .7);
  animation: wiggle 1.4s ease-in-out infinite;
}
@keyframes wiggle { 0%, 100% { transform: rotate(0); } 10% { transform: rotate(-4deg); } 20% { transform: rotate(4deg); } 30% { transform: rotate(0); } }
.knife-ind.on svg { opacity: 1; filter: none; }

.hud-center {
  position: absolute;
  top: calc(var(--sat) + 4px);
  left: 50%; transform: translateX(-50%);
  text-align: center;
}
#hud-timer {
  font-size: 46px; font-weight: 700; line-height: 1;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 3px 0 var(--ink), 0 0 12px rgba(20, 8, 40, .8);
  letter-spacing: 0.02em;
}
#hud-timer.urgent { color: var(--red); animation: pulse 1s infinite; }
@keyframes pulse {
  0% { transform: scale(1.25); }
  30% { transform: scale(1); }
  100% { transform: scale(1); }
}
#hud-level {
  font-size: 15px; font-weight: 600; color: var(--amber);
  letter-spacing: 0.08em;
  text-shadow: 0 2px 0 var(--ink);
}

.hud-right {
  position: absolute;
  top: calc(var(--sat) + 8px);
  right: calc(var(--sar) + 8px);
}
#minimap {
  display: block;
  background: rgba(22, 14, 38, 0.6);
  border: 2px solid rgba(122, 92, 176, 0.9);
  border-radius: 8px;
  opacity: 0.88;
}

#toasts {
  position: absolute; left: 0; right: 0;
  top: calc(var(--sat) + min(132px, 22vh));
  display: flex; flex-direction: column; align-items: center; gap: 6px;
}
.toast {
  background: var(--panel);
  border: 2px solid var(--border);
  border-left: 6px solid var(--amber);
  padding: 7px 16px 6px;
  border-radius: 14px;
  font-size: 20px; font-weight: 600; letter-spacing: 0.02em;
  animation: toast 1.9s ease forwards;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .5);
}
.toast.good { border-left-color: var(--green); }
.toast.bad { border-left-color: var(--red); }
@keyframes toast {
  0% { opacity: 0; transform: translateY(-10px) scale(.9); }
  10% { opacity: 1; transform: none; }
  80% { opacity: 1; }
  100% { opacity: 0; transform: translateY(-6px); }
}

#banner {
  position: absolute; left: 0; right: 0; top: 19%;
  text-align: center;
  text-shadow: 0 4px 0 var(--ink), 0 0 24px rgba(20, 8, 40, .9);
}
#banner .big { font-size: 64px; font-weight: 700; letter-spacing: 0.04em; line-height: 1; }
#banner .small { font-size: 22px; color: var(--text); margin-top: 6px; letter-spacing: 0.06em; }
#banner.go .big { color: var(--green); animation: go .6s ease-out forwards; }
#banner.win .big { color: var(--green); }
@keyframes go { 0% { transform: scale(1.6); opacity: 0; } 25% { transform: scale(1); opacity: 1; } 100% { opacity: 0; transform: scale(1.05); } }
#banner.pop .big { animation: pop .35s ease-out; }
@keyframes pop { 0% { transform: scale(.6); opacity: 0; } 100% { transform: none; opacity: 1; } }

/* -------------------------------------------------------------- screens */
.screen {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  padding: calc(var(--sat) + 16px) calc(var(--sar) + 16px) calc(var(--sab) + 16px) calc(var(--sal) + 16px);
  background: radial-gradient(ellipse at center, rgba(30, 16, 52, 0.35) 0%, rgba(22, 12, 40, 0.85) 75%);
  animation: fadein .25s ease;
  touch-action: manipulation;
}
@keyframes fadein { from { opacity: 0; } to { opacity: 1; } }

.panel {
  width: min(370px, 100%);
  max-height: 100%;
  overflow-y: auto;
  background: var(--panel);
  border: 3px solid var(--border);
  border-radius: 24px;
  padding: 20px 20px 18px;
  text-align: center;
  box-shadow: 0 8px 0 rgba(20, 8, 40, .55), 0 14px 40px rgba(0, 0, 0, .5);
  -webkit-backdrop-filter: blur(3px);
  backdrop-filter: blur(3px);
}
.panel h2 { margin: 0 0 14px; font-size: 40px; font-weight: 700; letter-spacing: 0.02em; line-height: 1; text-shadow: 0 3px 0 var(--ink); }
.panel h2.good { color: var(--green); }
.panel h2.bad { color: var(--red); font-size: 52px; }

.mascot { width: 104px; margin: -4px auto 0; animation: bob 2.4s ease-in-out infinite; }
.mascot svg { display: block; width: 100%; }
.mascot .pupil { animation: look 4s ease-in-out infinite; }
@keyframes bob { 0%, 100% { transform: translateY(0) rotate(-2deg); } 50% { transform: translateY(-6px) rotate(2deg); } }
@keyframes look { 0%, 40%, 100% { transform: translate(0, 0); } 50%, 70% { transform: translate(-7px, -1px); } 80%, 90% { transform: translate(4px, 2px); } }
.title {
  margin: 0;
  font-size: clamp(40px, 13vw, 56px); font-weight: 700; line-height: 0.95; letter-spacing: 0.01em;
  color: var(--text);
  text-shadow: 0 4px 0 var(--ink);
}
.title span { color: var(--amber); }
.subtitle { margin: 2px 0 10px; font-size: 20px; font-weight: 600; color: #c8a0ff; letter-spacing: 0.06em; }
.lead { font-size: 18px; line-height: 1.3; margin: 0 0 12px; color: var(--text); }
.legend { list-style: none; padding: 0; margin: 0 0 12px; text-align: left; font-size: 15px; color: #ece2fa; line-height: 1.25; }
.legend li { display: flex; gap: 10px; align-items: center; margin-bottom: 8px; }
.legend b { color: var(--amber); font-weight: 600; }
.dot { flex: none; width: 24px; height: 24px; border-radius: 50%; display: inline-block; }
.dot.yellow { background: rgba(240, 190, 70, .18); border: 3px solid rgba(245, 200, 80, .9); }
.dot.red { border: 3px dashed var(--red); background: rgba(255, 107, 138, .12); }
.dot.knife { background: radial-gradient(circle, #fff6d8 0, #ffd23f 40%, rgba(200,140,255,.4) 60%, rgba(200,140,255,0) 72%); }
.dot.move { background: #4fc3f7; border: 3px solid #e0f6ff; box-shadow: 0 0 0 3px rgba(79,195,247,.3); }
.controls { font-size: 14px; color: var(--muted); margin: 0 0 14px; line-height: 1.3; }
.record { margin: 12px 0 0; color: var(--muted); font-size: 17px; letter-spacing: 0.02em; }
.record b { color: var(--amber); font-variant-numeric: tabular-nums; }

.btn {
  pointer-events: auto;
  display: block; width: 100%;
  margin-top: 12px;
  padding: 15px 10px 13px;
  font: 700 25px/1 var(--font);
  letter-spacing: 0.03em;
  color: var(--text);
  background: var(--panel-2);
  border: 3px solid var(--border);
  border-radius: 18px;
  box-shadow: 0 5px 0 #24163a;
  touch-action: manipulation;
  cursor: pointer;
}
.btn.primary { background: var(--amber); color: var(--ink); border-color: #fff0b0; box-shadow: 0 5px 0 #b07a10; }
.btn:active { transform: translateY(4px); box-shadow: 0 1px 0 #24163a; }
.btn.primary:active { box-shadow: 0 1px 0 #b07a10; }

.rows { margin: 0 0 8px; font-size: 20px; }
.rows > div { display: flex; justify-content: space-between; padding: 6px 4px; border-bottom: 2px dotted #5e4888; }
.rows span { color: var(--muted); }
.rows b { font-variant-numeric: tabular-nums; }
.hint { color: var(--muted); font-size: 15px; margin: 10px 0 4px; }
.hint.cheer { font-size: 17px; color: var(--text); margin: -4px 0 10px; }
.newbest { color: var(--amber); font-size: 22px; margin: 10px 0 0; letter-spacing: 0.06em; animation: pulse 1.2s infinite; }

#fps {
  position: absolute; bottom: calc(var(--sab) + 4px); left: 4px;
  font: 11px monospace; color: #8f8; background: rgba(0,0,0,.5); padding: 2px 4px;
  pointer-events: none;
}

@media (max-height: 640px) {
  .mascot { width: 72px; }
  .title { font-size: 44px; }
  .lead { font-size: 16px; }
  .legend { font-size: 13px; }
  .btn { padding: 11px 10px 9px; font-size: 21px; }
}

/* start level picker */
.level-pick { display: flex; align-items: center; justify-content: center; gap: 8px; margin: 0 0 14px; }
.level-pick label { font-size: 16px; font-weight: 600; color: var(--muted); letter-spacing: 0.03em; margin-right: 4px; }
.level-pick .step {
  width: 46px; height: 46px; border-radius: 14px; font-size: 26px; line-height: 1; font-weight: 700; font-family: var(--font);
  border: 2px solid var(--border); background: rgba(255, 255, 255, 0.06); color: var(--text);
  touch-action: manipulation; cursor: pointer;
}
.level-pick .step:active { background: rgba(255, 197, 61, 0.3); }
.level-pick input {
  width: 72px; height: 46px; border-radius: 14px; text-align: center;
  font: 700 22px var(--font); /* >= 16px so iOS does not zoom on focus */
  color: var(--amber); background: rgba(20, 8, 40, 0.45); border: 2px solid var(--border);
  -webkit-user-select: text; user-select: text; touch-action: manipulation;
  -moz-appearance: textfield; appearance: textfield;
}
.level-pick input::-webkit-outer-spin-button,
.level-pick input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.level-pick input:focus { outline: 2px solid var(--amber); outline-offset: 1px; }
