:root {
  --ink: #f4efe6;
  --soft: #97a0b4;
  --dim: #6b7487;
  --panel: rgba(20, 27, 41, 0.72);
  --edge: rgba(255, 255, 255, 0.08);
  --accent: #ffcf4a;
  --strip-h: calc(30px + env(safe-area-inset-bottom));
  --b: clamp(64px, 18vmin, 92px);
  --font: ui-rounded, 'SF Pro Rounded', 'Nunito', 'Arial Rounded MT Bold', system-ui, -apple-system, 'Segoe UI', sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  position: fixed; inset: 0; overscroll-behavior: none; touch-action: none;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
  color: var(--ink); font-family: var(--font);
  background: radial-gradient(120% 90% at 50% 38%, #2b3754 0%, #161d2d 55%, #0b0f18 100%);
}
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

#scene { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }

/* ---------- HUD ---------- */
.hud {
  position: absolute; z-index: 3; left: 0; right: 0; top: 0;
  padding: calc(env(safe-area-inset-top) + 10px) calc(env(safe-area-inset-right) + 12px) 0 calc(env(safe-area-inset-left) + 12px);
  display: grid; grid-template-columns: 1.5fr 0.8fr 1fr 1fr auto; grid-template-rows: auto auto; column-gap: 8px; row-gap: 8px; align-items: stretch;
  pointer-events: none; transition: opacity .3s;
}
body[data-state="title"] .hud { opacity: 0; }
.stat {
  padding: 6px 10px 7px; border-radius: 14px; background: var(--panel); box-shadow: inset 0 0 0 1px var(--edge);
  display: flex; flex-direction: column; justify-content: center; min-width: 0;
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}
.stat .k { font-size: 10.5px; font-weight: 700; letter-spacing: .1em; text-transform: uppercase; color: var(--soft); }
.stat b { font-size: clamp(17px, 4.6vw, 24px); font-weight: 800; font-variant-numeric: tabular-nums; line-height: 1.15; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stat.score b { color: var(--accent); }
.lives { display: flex; gap: 5px; margin-top: 4px; }
.lives i { width: 13px; height: 13px; border-radius: 50%; border: 3.5px solid #ffcf4a; box-shadow: 0 1px 3px #0006; transition: border-color .3s, opacity .3s; }
.lives i.gone { border-color: #3a445a; box-shadow: none; }
.stat.next { grid-row: 1 / span 2; grid-column: 5; align-items: center; padding: 6px 8px; }
#nextCv { width: 64px; height: 50px; display: block; margin-top: 2px; }
.timer { grid-column: 1 / span 4; height: 8px; border-radius: 6px; background: rgba(255, 255, 255, .08); overflow: hidden; align-self: center; }
.timer i { display: block; height: 100%; border-radius: 6px; transform-origin: left; background: linear-gradient(90deg, #36c486, #7fe0a6); transition: background .3s; }
.timer i.mid { background: linear-gradient(90deg, #e0a311, #ffd25a); }
.timer i.low { background: linear-gradient(90deg, #e2433b, #ff7b6b); animation: blink .5s steps(2) infinite; }
@keyframes blink { 50% { opacity: .45; } }

/* Upright phones: the four small stats get one row, a bit tighter */
@media (max-width: 420px) {
  .hud { grid-template-columns: 1.45fr 0.75fr 1fr 0.95fr auto; column-gap: 6px; }
  .stat { padding: 5px 8px 6px; border-radius: 12px; }
  .stat .k { font-size: 9.5px; }
  .lives i { width: 11px; height: 11px; border-width: 3px; }
  #nextCv { width: 54px; height: 44px; }
}

/* Sideways: the HUD becomes a column on the left */
body.land .hud {
  right: auto; bottom: var(--strip-h); width: calc(clamp(150px, 17vw, 230px) + env(safe-area-inset-left));
  grid-template-columns: 1fr 1fr; grid-template-rows: none; grid-auto-rows: min-content; align-content: center;
  padding: calc(env(safe-area-inset-top) + 8px) 0 8px calc(env(safe-area-inset-left) + 12px);
}
body.land .stat.score { grid-column: 1 / span 2; }
body.land .lives-wrap { grid-column: 1 / span 2; }
body.land .stat.next { grid-column: 1 / span 2; grid-row: auto; }
body.land .timer { grid-column: 1 / span 2; }
body.land .stat b { font-size: clamp(17px, 2.3vw, 28px); }
body.land .stat .k { font-size: clamp(9.5px, 0.95vw, 12px); }
body.land #nextCv { width: 100%; height: clamp(40px, 9vh, 80px); }
@media (max-height: 430px) {
  body.land .hud { row-gap: 5px; }
  body.land .stat { padding: 4px 8px 5px; }
  body.land .stat b { font-size: 17px; }
  body.land #nextCv { height: 38px; }
}

/* ---------- moments ---------- */
#pops { position: absolute; inset: 0; z-index: 4; pointer-events: none; }
.pop {
  position: absolute; transform: translate(-50%, -50%); font-weight: 900; font-size: clamp(26px, 7vw, 40px); color: #fff;
  text-shadow: 0 3px 0 rgba(0, 0, 0, .25), 0 6px 18px rgba(0, 0, 0, .45); white-space: nowrap;
  animation: pop 1.3s cubic-bezier(.2, .9, .3, 1) forwards;
}
.pop.swept { font-size: clamp(15px, 4vw, 20px); color: #ffe7a6; animation-delay: .1s; opacity: 0; }
@keyframes pop {
  0% { opacity: 0; transform: translate(-50%, -30%) scale(.6); }
  15% { opacity: 1; transform: translate(-50%, -60%) scale(1.08); }
  30% { transform: translate(-50%, -70%) scale(1); }
  75% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -150%) scale(1); }
}
.banner {
  position: absolute; z-index: 6; left: 50%; top: 42%; transform: translate(-50%, -50%) scale(.9); opacity: 0; pointer-events: none;
  padding: 16px 26px 14px; border-radius: 20px; text-align: center; background: rgba(14, 19, 30, .86); box-shadow: inset 0 0 0 1px var(--edge), 0 20px 50px #0008;
  display: flex; flex-direction: column; gap: 2px; min-width: 220px;
}
.banner b { font-size: 26px; font-weight: 900; }
.banner span { color: var(--soft); font-weight: 600; }
.banner.good b { color: var(--accent); }
.banner.bad b { color: #ff7b6b; }
.banner.show { animation: banner 1.5s ease forwards; }
@keyframes banner {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(.85); }
  12%, 80% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -50%) scale(1); }
}
.toast {
  position: absolute; z-index: 6; left: 50%; bottom: calc(var(--strip-h) + 120px); transform: translateX(-50%); opacity: 0; pointer-events: none;
  padding: 8px 16px; border-radius: 99px; background: rgba(14, 19, 30, .9); font-weight: 700; font-size: 14px; white-space: nowrap;
}
.toast.show { animation: toast 1.6s ease forwards; }
@keyframes toast { 10%, 80% { opacity: 1; } 100% { opacity: 0; } }

/* ---------- touch controls ---------- */
#touch { position: absolute; left: 0; right: 0; top: 0; bottom: var(--strip-h); z-index: 5; touch-action: none; }
#stick {
  position: absolute; z-index: 7; width: 132px; height: 132px; margin: -66px 0 0 -66px; border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, .25); background: radial-gradient(circle, rgba(255, 255, 255, .04), rgba(255, 255, 255, .12));
  pointer-events: none; opacity: 0; transition: opacity .15s;
}
#stick.on { opacity: 1; transition: none; }
#knob {
  position: absolute; left: 50%; top: 50%; width: 60px; height: 60px; border-radius: 50%; transform: translate(-50%, -50%);
  background: radial-gradient(circle at 40% 35%, rgba(255, 255, 255, .6), rgba(255, 255, 255, .22));
  box-shadow: 0 4px 14px rgba(0, 0, 0, .35);
}
.btns {
  position: absolute; z-index: 8; right: calc(14px + env(safe-area-inset-right)); bottom: calc(var(--strip-h) + 12px);
  display: flex; align-items: flex-end; gap: calc(var(--b) * .2);
}
.tb {
  width: var(--b); height: var(--b); border-radius: 50%; padding: 0;
  border: 1.5px solid rgba(255, 255, 255, .35); background: rgba(255, 255, 255, .12); color: #fff;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
  backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px); box-shadow: 0 6px 18px rgba(0, 0, 0, .3);
  touch-action: none; transition: transform .08s, background .08s;
}
.tb svg { width: 40%; height: 40%; stroke: #fff; }
.tb span { font-size: calc(var(--b) * .15); font-weight: 800; letter-spacing: .02em; }
.tb.place { margin-bottom: calc(var(--b) * .5); background: rgba(255, 207, 74, .22); border-color: rgba(255, 207, 74, .65); }
.tb.down { transform: scale(.92); background: rgba(255, 255, 255, .3); }
.tb.place.down { background: rgba(255, 207, 74, .45); }
body[data-state="title"] .btns, body[data-state="over"] .btns { opacity: 0; pointer-events: none; }

/* ---------- title, pause, game over ---------- */
.screen { position: absolute; inset: 0 0 var(--strip-h) 0; z-index: 9; display: grid; place-items: center; padding: 16px; pointer-events: none; }
.card {
  pointer-events: auto; width: min(380px, 100%); padding: 26px 24px 22px; border-radius: 26px; text-align: center;
  background: rgba(16, 22, 35, .82); box-shadow: inset 0 0 0 1px var(--edge), 0 30px 80px rgba(0, 0, 0, .55);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  animation: card .4s cubic-bezier(.2, .9, .3, 1.2);
}
.card.small { width: min(320px, 100%); }
@keyframes card { from { opacity: 0; transform: translateY(14px) scale(.96); } }
.card h1 { margin: 0; font-size: clamp(40px, 11vw, 54px); font-weight: 900; letter-spacing: -.02em; line-height: 1; }
.card h2 { margin: 0 0 14px; font-size: 30px; font-weight: 900; }
.tag { margin: 8px 0 20px; color: var(--soft); font-weight: 600; font-size: 16px; }
.play {
  border: 0; border-radius: 18px; padding: 15px 52px; font-size: 21px; font-weight: 900; color: #2b1d00;
  background: linear-gradient(180deg, #ffe07a, #ffbf2e); box-shadow: 0 5px 0 #b9821a, 0 12px 26px rgba(0, 0, 0, .4);
  transition: transform .08s;
}
.play.down, .play:active { transform: translateY(3px); box-shadow: 0 2px 0 #b9821a, 0 8px 18px rgba(0, 0, 0, .4); }
.best { margin: 14px 0 0; color: var(--accent); font-weight: 700; min-height: 1.2em; }
.how { list-style: none; margin: 16px 0 0; padding: 14px 0 0; border-top: 1px solid var(--edge); display: grid; gap: 7px; font-size: 14px; color: var(--soft); text-align: left; }
.how li { padding-left: 20px; position: relative; line-height: 1.35; }
.how li::before { content: ""; position: absolute; left: 0; top: .32em; width: 8px; height: 8px; border-radius: 50%; border: 2.5px solid var(--accent); }
.touch-only { display: none; }
body.touchy .touch-only { display: revert; }
body.touchy .key-only { display: none; }
.link { display: block; margin: 16px auto 0; border: 0; background: none; color: var(--soft); text-decoration: underline; text-underline-offset: 3px; font-weight: 600; }
.final { margin: 0; font-size: 54px; font-weight: 900; color: var(--accent); font-variant-numeric: tabular-nums; line-height: 1; }
.sub { margin: 10px 0 20px; color: var(--soft); font-weight: 600; }
@media (max-height: 430px) {
  .card { padding: 16px 20px 14px; }
  .tag { margin: 4px 0 12px; }
  .how { margin-top: 10px; padding-top: 10px; gap: 4px; font-size: 13px; }
  .play { padding: 11px 44px; }
}

/* ---------- bottom strip: pause, sound, credit ---------- */
.strip {
  position: absolute; left: 0; right: 0; bottom: 0; height: var(--strip-h); z-index: 10;
  padding: 0 calc(10px + env(safe-area-inset-right)) env(safe-area-inset-bottom) calc(10px + env(safe-area-inset-left));
  display: flex; align-items: center; justify-content: center; font-size: 12px; color: var(--dim);
}
.sys { position: absolute; left: calc(8px + env(safe-area-inset-left)); top: 0; height: 30px; display: flex; align-items: center; gap: 2px; }
.sys button { width: 36px; height: 28px; border: 0; border-radius: 8px; background: transparent; display: grid; place-items: center; padding: 0; }
.sys svg { width: 18px; height: 18px; fill: var(--soft); stroke: var(--soft); }
.sys button.down { background: rgba(255, 255, 255, .1); }
#soundBtn .cross { display: none; }
#soundBtn[data-mode="1"] .wave2 { display: none; }
#soundBtn[data-mode="2"] .wave, #soundBtn[data-mode="2"] .wave2 { display: none; }
#soundBtn[data-mode="2"] .cross { display: block; }
.credit { display: flex; align-items: center; gap: 8px; }
.credit a { color: var(--soft); text-decoration: none; padding: 4px 2px; }
.credit a:hover { color: var(--accent); text-decoration: underline; }
@media (max-width: 400px) { .credit { margin-left: 64px; } }
button:focus-visible { outline: 3px solid #7fc4ff; outline-offset: 3px; }

.nogl { position: absolute; inset: 40% 16px auto; z-index: 20; text-align: center; font-weight: 700; }
