:root {
  --abyss: #03140f;
  --storm: #0b3b33;
  --glass: #3fa58a;
  --mist: #c8f5e0;
  --core: #fff8e1;
  --red: #ff4d3d;
  --display: "Cinzel", "Didot", Georgia, "Times New Roman", serif;
  --ui: "Barlow Semi Condensed", "Arial Narrow", system-ui, sans-serif;
  --st: env(safe-area-inset-top, 0px);
  --sr: env(safe-area-inset-right, 0px);
  --sb: env(safe-area-inset-bottom, 0px);
  --sl: env(safe-area-inset-left, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden; background: var(--abyss); color: var(--mist);
  font-family: var(--ui); overscroll-behavior: none; user-select: none; -webkit-user-select: none;
  touch-action: none;
}
[hidden] { display: none !important; }

#scene { position: fixed; inset: 0; width: 100vw; height: 100vh; height: 100dvh; display: block; touch-action: none; }
#vig {
  position: fixed; inset: 0; pointer-events: none;
  background:
    radial-gradient(120% 90% at 50% 62%, transparent 52%, rgba(1, 10, 7, 0.55) 100%),
    linear-gradient(180deg, rgba(3, 20, 15, 0.55) 0%, rgba(3, 20, 15, 0) 16%);
}
#flash { position: fixed; inset: 0; pointer-events: none; background: #e8fff6; opacity: 0; mix-blend-mode: screen; }

/* ------------------------------------------------------------ buttons */
button { font-family: var(--ui); cursor: pointer; border: 0; }
.primary {
  display: block; min-width: 220px; height: 58px; padding: 0 36px; border-radius: 999px;
  background: linear-gradient(180deg, #fffdf3 0%, var(--core) 55%, #f1e5c4 100%);
  color: var(--abyss); font-size: 21px; font-weight: 700; letter-spacing: 0.08em;
  box-shadow: 0 0 0 1px rgba(255, 248, 225, 0.4), 0 10px 40px rgba(255, 248, 225, 0.22), 0 0 70px rgba(200, 245, 224, 0.18);
  transition: transform 140ms ease-out, box-shadow 160ms ease-out;
}
.primary:active { transform: scale(0.96); }
.primary:focus-visible, .ghost:focus-visible, .icon-btn:focus-visible { outline: 2px solid var(--mist); outline-offset: 3px; }
@media (hover: hover) { .primary:hover { box-shadow: 0 0 0 1px rgba(255, 248, 225, 0.6), 0 12px 46px rgba(255, 248, 225, 0.32), 0 0 90px rgba(200, 245, 224, 0.26); } }
.ghost {
  display: block; min-width: 180px; height: 46px; padding: 0 26px; border-radius: 999px;
  background: rgba(3, 20, 15, 0.35); color: var(--mist); font-size: 17px; font-weight: 600; letter-spacing: 0.06em;
  box-shadow: inset 0 0 0 1px rgba(200, 245, 224, 0.32);
  transition: transform 140ms ease-out, background 160ms ease-out;
}
.ghost:active { transform: scale(0.96); }
@media (hover: hover) { .ghost:hover { background: rgba(63, 165, 138, 0.18); } }
.icon-btn {
  width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center;
  background: rgba(3, 20, 15, 0.5); color: var(--mist);
  box-shadow: inset 0 0 0 1px rgba(200, 245, 224, 0.25);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  transition: transform 120ms ease-out, background 160ms;
}
.icon-btn:active { transform: scale(0.92); }
.icon-btn .slash { opacity: 0; }
.icon-btn.muted .slash { opacity: 1; }
.icon-btn.muted .wave { opacity: 0; }

/* ------------------------------------------------------------ HUD */
#hud { position: fixed; inset: 0; pointer-events: none; animation: fadeIn 500ms ease-out; }
#topbar {
  position: absolute; left: 0; right: 0; top: 0;
  padding: calc(var(--st) + 12px) calc(var(--sr) + 12px) 0 calc(var(--sl) + 16px);
  display: flex; justify-content: space-between; align-items: flex-start;
}
.score { line-height: 1; }
.score-label { font-size: 12px; font-weight: 700; letter-spacing: 0.22em; text-transform: uppercase; color: rgba(200, 245, 224, 0.75); }
.score-num {
  font-family: var(--display); font-weight: 900; font-size: 54px; color: var(--core); margin-top: 2px;
  text-shadow: 0 0 18px rgba(255, 248, 225, 0.45), 0 2px 0 rgba(3, 20, 15, 0.6);
  transform-origin: 10% 60%;
}
.score-num.pop { animation: pop 520ms cubic-bezier(.2, 1.6, .4, 1); }
.phase {
  display: inline-block; margin-top: 6px; padding: 4px 10px; border-radius: 999px;
  font-size: 12px; font-weight: 700; letter-spacing: 0.16em; text-transform: uppercase;
  color: var(--mist); background: rgba(3, 20, 15, 0.5); box-shadow: inset 0 0 0 1px rgba(63, 165, 138, 0.45);
}
.phase.flash { animation: chip 900ms ease-out; }
.right { display: flex; align-items: center; gap: 8px; pointer-events: auto; }
.wrecks { display: flex; gap: 5px; margin-right: 4px; padding: 0 2px; }
.wrecks i { width: 24px; height: 24px; display: block; transition: transform 240ms ease-out; }
.wrecks svg { width: 24px; height: 24px; display: block; overflow: visible; }
.wrecks .hull { fill: var(--mist); opacity: 0.85; filter: drop-shadow(0 0 4px rgba(200, 245, 224, 0.35)); }
.wrecks .flare { display: none; fill: var(--red); filter: drop-shadow(0 0 6px rgba(255, 77, 61, 0.8)); }
.wrecks .flare .hot { fill: #ffd2c8; }
.wrecks .flare .line { fill: none; stroke: var(--red); stroke-width: 1.5; }
.wrecks i.gone .hull { display: none; }
.wrecks i.gone .flare { display: inline; }
.wrecks i.gone { animation: flare 700ms ease-out; }

#toast {
  position: absolute; left: 50%; top: calc(var(--st) + 19vh); transform: translateX(-50%);
  font-family: var(--display); font-weight: 700; font-size: clamp(20px, 5.6vw, 30px); letter-spacing: 0.14em; text-transform: uppercase;
  color: var(--core); white-space: nowrap; text-shadow: 0 0 22px rgba(255, 248, 225, 0.5), 0 2px 0 rgba(3, 20, 15, 0.8);
  animation: toast 1900ms ease-out forwards;
}
#toast.bad { color: #ffd2c8; text-shadow: 0 0 22px rgba(255, 77, 61, 0.8), 0 2px 0 rgba(3, 20, 15, 0.8); }
#hint {
  position: absolute; left: 50%; bottom: calc(var(--sb) + 7vh); transform: translateX(-50%);
  max-width: min(92vw, 520px); padding: 10px 18px; border-radius: 20px; text-align: center; text-wrap: balance;
  font-size: 17px; font-weight: 600; letter-spacing: 0.03em; color: var(--core);
  background: rgba(3, 20, 15, 0.62); box-shadow: inset 0 0 0 1px rgba(200, 245, 224, 0.28);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  animation: hintIn 380ms ease-out;
}
#hint.urgent { box-shadow: inset 0 0 0 1px rgba(255, 77, 61, 0.7), 0 0 30px rgba(255, 77, 61, 0.25); }
#pop {
  position: absolute; left: calc(var(--sl) + 70px); top: calc(var(--st) + 30px);
  font-family: var(--display); font-weight: 900; font-size: 26px; color: var(--core);
  text-shadow: 0 0 14px rgba(255, 248, 225, 0.7);
  animation: rise 900ms ease-out forwards;
}

/* ------------------------------------------------------------ title */
#menu {
  position: fixed; inset: 0; display: flex; flex-direction: column; justify-content: space-between; align-items: center;
  padding: calc(var(--st) + 9vh) 20px calc(var(--sb) + 5vh);
  background: linear-gradient(180deg, rgba(3, 20, 15, 0.72) 0%, rgba(3, 20, 15, 0.1) 38%, rgba(3, 20, 15, 0) 55%, rgba(3, 20, 15, 0.7) 100%);
  transition: opacity 600ms ease-out;
}
#menu.leaving { opacity: 0; pointer-events: none; }
.title-block { text-align: center; }
.title-mark { opacity: 0.95; margin-bottom: 4px; filter: drop-shadow(0 0 10px rgba(255, 248, 225, 0.4)); }
.title {
  margin: 0; font-family: var(--display); font-weight: 900; text-transform: uppercase;
  font-size: clamp(40px, 12vw, 128px); letter-spacing: 0.26em; padding-left: 0.26em; line-height: 1;
  background: linear-gradient(100deg, #8fd8bd 0%, var(--mist) 30%, var(--core) 46%, #ffffff 50%, var(--core) 54%, var(--mist) 70%, #8fd8bd 100%);
  background-size: 320% 100%; background-position: 100% 0;
  -webkit-background-clip: text; background-clip: text; color: transparent;
  filter: drop-shadow(0 0 22px rgba(200, 245, 224, 0.35)) drop-shadow(0 3px 0 rgba(3, 20, 15, 0.7));
  animation: sweep 6s ease-in-out infinite;
}
.sub { margin: 14px 0 0; font-family: var(--display); font-weight: 500; font-size: clamp(15px, 4.2vw, 21px); letter-spacing: 0.12em; color: var(--mist); text-shadow: 0 2px 10px rgba(3, 20, 15, 0.9); }
.menu-foot { display: flex; flex-direction: column; align-items: center; gap: 12px; text-align: center; }
.best { font-size: 16px; font-weight: 700; letter-spacing: 0.18em; text-transform: uppercase; color: var(--core); min-height: 19px; }
.storm { font-size: 14px; font-weight: 500; letter-spacing: 0.05em; color: rgba(200, 245, 224, 0.75); }
.howto { font-size: 14px; font-weight: 500; letter-spacing: 0.03em; color: rgba(200, 245, 224, 0.6); max-width: 420px; line-height: 1.5; }

/* ------------------------------------------------------------ overlays */
.overlay {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  padding: calc(var(--st) + 20px) calc(var(--sr) + 16px) calc(var(--sb) + 20px) calc(var(--sl) + 16px);
  background: rgba(3, 20, 15, 0.42);
  animation: fadeIn 260ms ease-out;
}
.card {
  position: relative; width: min(92vw, 380px); padding: 26px 24px 22px; border-radius: 22px; text-align: center;
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  background: linear-gradient(180deg, rgba(11, 59, 51, 0.72), rgba(3, 20, 15, 0.82));
  box-shadow: inset 0 0 0 1px rgba(200, 245, 224, 0.2), 0 30px 80px rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px);
  color: var(--mist);
  animation: cardIn 420ms cubic-bezier(.2, .9, .3, 1.1);
}
.card-title { margin: 0; font-family: var(--display); font-weight: 700; font-size: 23px; letter-spacing: 0.05em; line-height: 1.25; color: var(--core); }
.card-sub { margin: 0 0 8px; font-size: 16px; color: rgba(200, 245, 224, 0.75); }
.over { align-items: flex-end; padding-top: calc(var(--st) + 140px); padding-bottom: calc(var(--sb) + 5vh); background: linear-gradient(180deg, rgba(3, 20, 15, 0) 30%, rgba(3, 20, 15, 0.55) 100%); }
.over .card { animation-delay: 0ms; }
.big-label { margin-top: 6px; font-size: 13px; font-weight: 700; letter-spacing: 0.26em; text-transform: uppercase; color: rgba(200, 245, 224, 0.8); }
.big-num {
  font-family: var(--display); font-weight: 900; font-size: 104px; line-height: 0.95; color: var(--core);
  text-shadow: 0 0 34px rgba(255, 248, 225, 0.5), 0 3px 0 rgba(3, 20, 15, 0.7);
}
.over-time { font-family: var(--display); font-size: 16px; letter-spacing: 0.06em; color: rgba(200, 245, 224, 0.85); }
.over-challenge { max-width: 300px; font-size: 14px; line-height: 1.45; color: rgba(255, 248, 225, 0.85); margin: -2px 0 6px; }
.over-best { font-size: 16px; font-weight: 600; letter-spacing: 0.12em; text-transform: uppercase; color: rgba(200, 245, 224, 0.8); margin-bottom: 8px; }
.ribbon {
  position: absolute; top: -14px; left: 50%; transform: translateX(-50%) rotate(-2deg);
  padding: 6px 16px; border-radius: 6px; background: var(--core); color: var(--abyss);
  font-size: 14px; font-weight: 700; letter-spacing: 0.2em; text-transform: uppercase;
  box-shadow: 0 0 30px rgba(255, 248, 225, 0.5);
  animation: ribbon 700ms 300ms cubic-bezier(.2, 1.6, .4, 1) both;
}

/* ------------------------------------------------------------ loader */
#loader {
  position: fixed; inset: 0; display: grid; place-items: center; background: var(--abyss);
  transition: opacity 700ms ease-out, visibility 0s 700ms;
}
#loader.done { opacity: 0; visibility: hidden; }
.loader-inner { position: relative; display: grid; place-items: center; width: 160px; height: 200px; }
.loader-ring { position: absolute; top: 0; left: 20px; width: 120px; height: 120px; transform: rotate(-90deg); }
.loader-ring .track { fill: none; stroke: rgba(63, 165, 138, 0.2); stroke-width: 2; }
.loader-ring .arc { fill: none; stroke: var(--glass); stroke-width: 2.5; stroke-linecap: round; stroke-dasharray: 327; stroke-dashoffset: 327; transition: stroke-dashoffset 300ms ease-out; filter: drop-shadow(0 0 4px rgba(63, 165, 138, 0.8)); }
.loader-glass {
  position: absolute; top: 30px; left: 50px; width: 60px; height: 60px; border-radius: 50%;
  background: radial-gradient(circle, var(--core) 0%, rgba(255, 248, 225, 0.65) 32%, rgba(200, 245, 224, 0.12) 62%, transparent 72%);
  opacity: 0.1; transition: opacity 400ms ease-out;
}
.loader-title { position: absolute; top: 136px; width: 100%; text-align: center; font-family: var(--display); font-weight: 700; font-size: 22px; letter-spacing: 0.32em; padding-left: 0.32em; color: var(--core); }
.loader-sub { position: absolute; top: 172px; width: 100%; text-align: center; font-size: 14px; color: rgba(200, 245, 224, 0.65); }

#glLost { position: fixed; inset: 0; display: grid; place-items: center; background: rgba(3, 20, 15, 0.85); z-index: 10; }

/* ------------------------------------------------------------ motion */
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes cardIn { from { opacity: 0; transform: translateY(24px) scale(0.98); } to { opacity: 1; transform: none; } }
@keyframes pop { 0% { transform: scale(1); } 35% { transform: scale(1.38); color: #fff; } 100% { transform: scale(1); } }
@keyframes rise { 0% { opacity: 0; transform: translateY(8px) scale(0.8); } 20% { opacity: 1; } 100% { opacity: 0; transform: translateY(-26px) scale(1.1); } }
@keyframes toast { 0% { opacity: 0; transform: translate(-50%, 10px); letter-spacing: 0.3em; } 15% { opacity: 1; transform: translate(-50%, 0); letter-spacing: 0.14em; } 80% { opacity: 1; } 100% { opacity: 0; transform: translate(-50%, -8px); } }
@keyframes hintIn { from { opacity: 0; transform: translate(-50%, 10px); } to { opacity: 1; transform: translate(-50%, 0); } }
@keyframes sweep { 0%, 15% { background-position: 100% 0; } 60%, 100% { background-position: 0% 0; } }
@keyframes flare { 0% { transform: scale(1.8) translateY(-4px); } 100% { transform: none; } }
@keyframes chip { 0% { box-shadow: inset 0 0 0 1px var(--core), 0 0 24px rgba(255, 248, 225, 0.6); } 100% { box-shadow: inset 0 0 0 1px rgba(63, 165, 138, 0.45); } }
@keyframes ribbon { from { opacity: 0; transform: translateX(-50%) rotate(-2deg) scale(0.4); } to { opacity: 1; transform: translateX(-50%) rotate(-2deg) scale(1); } }

@media (prefers-reduced-motion: reduce) {
  .title { animation: none; background-position: 50% 0; }
  .score-num.pop, #pop, .ribbon, .card { animation: none; }
}

@media (min-width: 900px) {
  .score-num { font-size: 62px; }
  #toast { top: calc(var(--st) + 16vh); }
}
