/* HorrorMath — UI over the WebGL canvas. No backdrop-filter (too costly over WebGL on tablets). */
:root {
  --orange: #ff8a1e;
  --orange-2: #ffb347;
  --purple: #2a2046;
  --purple-2: #3d2f66;
  --ink: #f6f1ff;
  --green: #39b45a;
  --red: #e2453c;
  --panel: rgba(16, 11, 30, 0.9);
  --font: "Trebuchet MS", "Segoe UI", "Helvetica Neue", Arial, "DejaVu Sans", sans-serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --k: min(21vw, 9vh, 100px); /* number pad key size */
  --gap: calc(var(--k) * 0.14);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #120d22; color: var(--ink); font-family: var(--font); }
body { -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
#scene { position: fixed; inset: 0; width: 100%; height: 100%; display: block; }

/* ------------------------------------------------------------------ screens */
.hud, .play, .menu, .end, .loading { display: none; }
.mode-loading .loading { display: flex; }
.mode-menu .menu { display: flex; }
.mode-play .hud, .mode-play .play { display: block; }
.mode-end .hud { display: block; }
.mode-end .end { display: flex; }

.loading {
  position: fixed; inset: 0; align-items: center; justify-content: center; flex-direction: column; gap: 18px;
  background: radial-gradient(circle at 50% 40%, #2a2048, #0d0918 70%); font-size: 22px; z-index: 30;
}
.spinner { width: 56px; height: 56px; border-radius: 50%; border: 6px solid rgba(255, 138, 30, 0.25); border-top-color: var(--orange); animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

#fade { position: fixed; inset: 0; background: #07050d; opacity: 0; pointer-events: none; transition: opacity 0.22s ease; z-index: 25; }
#fade.on { opacity: 1; }

/* ------------------------------------------------------------------ panels */
.panel {
  position: fixed; z-index: 10; flex-direction: column; align-items: center; text-align: center; gap: 12px;
  background: var(--panel); border: 2px solid rgba(255, 138, 30, 0.45); border-radius: 26px;
  padding: 22px 26px 24px; box-shadow: 0 10px 40px rgba(0, 0, 0, 0.6), 0 0 30px rgba(255, 120, 20, 0.12) inset;
  width: min(460px, calc(100vw - 24px));
}
.menu { left: calc(4vw + var(--safe-l)); top: 50%; transform: translateY(-50%); max-height: calc(100vh - 20px); overflow: auto; }
.end { left: 50%; top: 50%; transform: translate(-50%, -50%); }
.logo { margin: 0; font-size: clamp(40px, 7vw, 64px); line-height: 1; letter-spacing: 1px; color: var(--ink); text-shadow: 0 3px 0 #000, 0 0 18px rgba(160, 110, 255, 0.6); }
.logo span { color: var(--orange); text-shadow: 0 3px 0 #000, 0 0 18px rgba(255, 120, 20, 0.8); }
.tag { margin: 0; font-size: 19px; color: #ddd3f5; }
.panel h2 { margin: 6px 0 0; font-size: 22px; color: var(--orange-2); }
.choices { display: flex; gap: 14px; justify-content: center; flex-wrap: wrap; }
.choice {
  display: flex; flex-direction: column; align-items: center; gap: 6px; width: 132px; padding: 12px 8px 10px;
  border-radius: 20px; border: 3px solid var(--purple-2); background: var(--purple); font-size: 20px; font-weight: 800;
}
.choice svg { width: 64px; height: 64px; }
.choice[aria-checked="true"] { border-color: var(--orange); background: #4a2a1a; box-shadow: 0 0 18px rgba(255, 138, 30, 0.55); }
.btn {
  border: 0; border-radius: 999px; font-weight: 900; letter-spacing: 1px; background: linear-gradient(#ffa23a, #e86a0c);
  color: #2a1203; box-shadow: 0 6px 0 #8a3a05, 0 10px 24px rgba(0, 0, 0, 0.5); transition: transform 0.08s;
}
.btn:active { transform: translateY(4px); box-shadow: 0 2px 0 #8a3a05; }
.btn.big { font-size: 32px; padding: 14px 54px; margin-top: 6px; text-transform: uppercase; }
.btn.small { font-size: 18px; padding: 10px 22px; background: var(--purple-2); color: var(--ink); box-shadow: 0 4px 0 #1a1230; }
.rules { list-style: none; margin: 4px 0 0; padding: 0; font-size: 17px; color: #cfc3ea; line-height: 1.5; }
.rules b { color: var(--orange-2); font-size: 20px; }
.end h2 { font-size: clamp(34px, 6vw, 48px); margin: 0; color: var(--ink); }
.end.won h2 { color: var(--orange-2); }
.end p { margin: 0; font-size: 21px; }
.end-score { display: flex; gap: 4px; flex-wrap: wrap; justify-content: center; }
.end-score svg { width: 30px; height: 30px; }
.end-score svg .p { fill: var(--orange); stroke: #7a3000; stroke-width: 1.2; }
.end-score svg .s { fill: #4f7a22; }

/* ------------------------------------------------------------------ HUD */
.hud { position: fixed; z-index: 5; top: calc(10px + var(--safe-t)); left: calc(12px + var(--safe-l)); right: calc(72px + var(--safe-r)); pointer-events: none; }
.hud > div { display: flex; align-items: center; }
.lives { gap: 6px; float: left; }
.lives svg { width: 40px; height: 40px; transition: transform 0.3s, opacity 0.3s; }
.lives svg .h { fill: #ff4d6d; stroke: #3a0010; stroke-width: 1.5; }
.lives svg.lost { opacity: 0.35; transform: scale(0.8); }
.lives svg.lost .h { fill: #555; }
.lives svg.pop { animation: pop 0.6s; }
.score { gap: 3px; justify-content: center; margin-left: 150px; }
.score svg { width: 34px; height: 34px; transition: transform 0.3s; }
.score svg .p { fill: #3b3350; stroke: #6e6290; stroke-width: 1.2; }
.score svg .s { fill: #6e6290; }
.score svg.got .p { fill: var(--orange); stroke: #7a3000; }
.score svg.got .s { fill: #4f7a22; }
.score svg.pop { animation: pop 0.6s; }
@keyframes pop { 0% { transform: scale(1); } 40% { transform: scale(1.5); } 100% { transform: scale(1); } }

.mute {
  position: fixed; z-index: 20; top: calc(10px + var(--safe-t)); right: calc(12px + var(--safe-r)); width: 52px; height: 52px;
  border-radius: 50%; border: 2px solid var(--purple-2); background: rgba(20, 14, 36, 0.85); display: grid; place-items: center; padding: 0;
}
.mute svg { width: 28px; height: 28px; fill: none; stroke: var(--ink); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.mute svg path:first-child { fill: var(--ink); }
.mute .i-off, .mute[aria-pressed="true"] .i-on { display: none; }
.mute[aria-pressed="true"] .i-off { display: block; }
.mode-loading .mute { display: none; }

/* ------------------------------------------------------------------ question + pad */
.play { position: fixed; inset: 0; z-index: 4; pointer-events: none; }
.question {
  position: absolute; left: 50%; transform: translateX(-50%); top: calc(104px + var(--safe-t));
  white-space: nowrap; font-weight: 900; font-size: min(14vw, 7.5vh, 96px); line-height: 1; letter-spacing: 0.02em;
  padding: 0.14em 0.34em; border-radius: 0.3em; background: rgba(12, 8, 24, 0.72); border: 2px solid rgba(255, 255, 255, 0.12);
  text-shadow: 0 3px 0 #000;
}
.question .op { color: var(--orange-2); margin: 0 0.12em; }
.question .box {
  display: inline-block; min-width: 1.5em; text-align: center; padding: 0 0.1em; margin-left: 0.1em; border-radius: 0.18em;
  border: 0.06em dashed var(--orange); background: rgba(255, 138, 30, 0.12); color: #fff;
}
.question .box.empty { color: var(--orange); animation: blink 1.2s infinite; }
.question .box.right { border-style: solid; border-color: var(--green); background: rgba(57, 180, 90, 0.3); }
.question .box.wrong { border-style: solid; border-color: var(--red); background: rgba(226, 69, 60, 0.3); }
.question .box s { color: #ff9a90; font-size: 0.6em; margin-right: 0.18em; vertical-align: middle; text-decoration-thickness: 0.1em; text-decoration-color: var(--red); }
@keyframes blink { 50% { opacity: 0.45; } }
.question.shake { animation: qshake 0.45s; }
@keyframes qshake { 20%, 60% { transform: translateX(calc(-50% - 10px)); } 40%, 80% { transform: translateX(calc(-50% + 10px)); } }
.feedback {
  position: absolute; left: 50%; transform: translateX(-50%); white-space: nowrap; font-size: min(7vw, 4vh, 38px); font-weight: 900;
  opacity: 0; transition: opacity 0.2s; text-shadow: 0 2px 0 #000, 0 0 12px #000;
}
.feedback.show { opacity: 1; }
.feedback.good { color: #7cf09a; }
.feedback.bad { color: #ffb0aa; }

.pad {
  position: absolute; pointer-events: auto; display: grid; grid-template-columns: repeat(3, var(--k)); gap: var(--gap);
  padding: var(--gap); border-radius: calc(var(--k) * 0.3); background: rgba(12, 8, 24, 0.72); border: 2px solid rgba(255, 255, 255, 0.1);
  left: 50%; transform: translateX(-50%); bottom: calc(12px + var(--safe-b));
}
.pad button {
  width: var(--k); height: var(--k); border-radius: 24%; border: 3px solid #5a4a8c; background: linear-gradient(#3e3168, #2a2046);
  font-size: calc(var(--k) * 0.52); font-weight: 900; line-height: 1; box-shadow: 0 calc(var(--k) * 0.06) 0 #120c22;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; padding: 0;
  transition: transform 0.06s, background 0.1s;
}
.pad button:active, .pad button.press { transform: translateY(calc(var(--k) * 0.05)); box-shadow: 0 calc(var(--k) * 0.01) 0 #120c22; background: #5b3a8a; }
.pad button svg { width: 46%; height: 46%; fill: none; stroke: currentColor; stroke-width: 2.6; stroke-linecap: round; stroke-linejoin: round; }
.pad button small { font-size: max(11px, calc(var(--k) * 0.16)); font-weight: 800; }
.pad .erase { background: linear-gradient(#6a3a3a, #4a2626); border-color: #8c5050; }
.pad .ok { background: linear-gradient(#3fc46a, #23893f); border-color: #6fe08f; color: #fff; }
.pad .ok:disabled { filter: grayscale(0.8) brightness(0.7); }
.pad.locked button { opacity: 0.55; }

/* landscape: pad on the right, the scene and the question share the rest */
@media (min-aspect-ratio: 1/1) {
  :root { --k: min(14.5vh, 9.5vw, 100px); }
  .pad { left: auto; right: calc(2.5vw + var(--safe-r)); top: 50%; bottom: auto; transform: translateY(-38%); }
  .question, .feedback { left: calc((100vw - 3 * var(--k) - 6 * var(--gap) - 2.5vw) / 2); }
  .question { top: calc(76px + var(--safe-t)); font-size: min(11vh, 7vw, 104px); }
  .score { margin-left: 160px; }
}
/* portrait: hearts + mute on the first row, pumpkins below */
@media (max-aspect-ratio: 1/1) {
  .score { margin-left: 0; clear: both; padding-top: 8px; justify-content: flex-start; }
  .score svg { width: min(8.4vw, 34px); height: min(8.4vw, 34px); }
  .hud { right: calc(12px + var(--safe-r)); }
  .menu { left: 50%; top: auto; bottom: calc(14px + var(--safe-b)); transform: translateX(-50%); gap: 9px; padding: 18px 18px 20px; }
  .menu .rules { font-size: 15px; }
  .logo { font-size: clamp(38px, 11vw, 60px); }
}
@media (max-height: 520px) and (min-aspect-ratio: 1/1) {
  .menu { gap: 6px; padding: 12px 18px; }
  .menu .tag, .menu .rules { display: none; }
  .choice { padding: 6px; width: 112px; } .choice svg { width: 44px; height: 44px; }
  .btn.big { font-size: 24px; padding: 10px 40px; }
  .lives svg { width: 30px; height: 30px; } .score svg { width: 26px; height: 26px; }
  .question { top: calc(54px + var(--safe-t)); }
}

/* ------------------------------------------------------------------ attack + victory effects */
#fx { position: fixed; inset: 0; pointer-events: none; z-index: 3; }
#vignette { position: absolute; inset: 0; background: radial-gradient(ellipse at center, transparent 55%, rgba(0, 0, 0, 0.55) 100%); transition: background 0.4s; }
.danger-1 #vignette { background: radial-gradient(ellipse at center, transparent 50%, rgba(80, 0, 10, 0.5) 100%); }
.danger-2 #vignette { background: radial-gradient(ellipse at center, transparent 40%, rgba(120, 0, 15, 0.65) 100%); animation: throb 1.6s infinite; }
@keyframes throb { 50% { opacity: 0.7; } }
#flash { position: absolute; inset: 0; background: #b0001a; opacity: 0; }
#flash.go { animation: flash 0.9s ease-out; }
@keyframes flash { 0% { opacity: 0; } 8% { opacity: 0.75; } 100% { opacity: 0.18; } }
.attacked #flash { opacity: 0.18; }
#scratch { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; }
#scratch path { fill: none; stroke-linecap: round; vector-effect: non-scaling-stroke; }
#scratch g { opacity: 0; transform-origin: 30% 0; }
#scratch path.edge { stroke: rgba(70, 0, 8, 0.75); stroke-width: 14px; }
#scratch path.core { stroke: rgba(255, 236, 236, 0.92); stroke-width: 6px; }
.attacked #scratch { opacity: 0.85; }
.attacked #scratch g { animation: claw 0.2s ease-out forwards; }
.attacked #scratch g:nth-child(2) { animation-delay: 0.04s; }
.attacked #scratch g:nth-child(3) { animation-delay: 0.08s; }
.attacked #scratch g:nth-child(4) { animation-delay: 0.12s; }
@keyframes claw { from { opacity: 0; transform: scaleY(0.2); } to { opacity: 1; transform: scaleY(1); } }
.attacked #scene { animation: shake 0.5s; }
@keyframes shake { 15% { transform: translate(-14px, 8px); } 30% { transform: translate(12px, -10px); } 45% { transform: translate(-9px, -6px); } 60% { transform: translate(7px, 9px); } 80% { transform: translate(-3px, 2px); } }

#confetti { position: fixed; inset: 0; pointer-events: none; z-index: 9; overflow: hidden; }
#confetti i { position: absolute; top: -40px; width: 14px; height: 14px; border-radius: 50%; animation: fall linear forwards; }
@keyframes fall { to { transform: translateY(115vh) rotate(540deg); } }

.noscript { position: fixed; inset: 0; display: grid; place-items: center; font-size: 22px; }
@media (prefers-reduced-motion: reduce) { .attacked #scene, .question.shake, .danger-2 #vignette { animation: none; } }
