:root {
  --sea: #0a5f93;
  --ink: #06204a;
  --foam: #f2fbff;
  --foam-dim: rgba(226, 244, 255, 0.72);
  --gold: #ffd166;
  --gold-deep: #f2a900;
  --panel: rgba(6, 30, 66, 0.78);
  --line: rgba(190, 232, 255, 0.24);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --serif: "Fraunces", Georgia, "Times New Roman", serif;
  --sans: "DM Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  width: 100%; height: 100%;
  overflow: hidden;
  background: var(--sea);
  color: var(--foam);
  font-family: var(--sans);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  user-select: none; -webkit-user-select: none;
}

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

button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; touch-action: manipulation; }
button:focus-visible { outline: 3px solid var(--gold); outline-offset: 3px; }
[hidden] { display: none !important; }

/* ------------------------------------------------------------ HUD */

#hud {
  position: fixed; left: 50%; top: 0;
  transform: translateX(-50%);
  z-index: 3;
  padding: 10px 12px 12px;
  border-radius: 20px;
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: 0 10px 30px rgba(2, 12, 36, 0.35);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
.hud-top { display: flex; align-items: center; gap: 10px; }
.chip {
  flex: none; width: 40px; height: 40px; border-radius: 50%;
  display: grid; place-items: center;
  background: rgba(190, 232, 255, 0.12); border: 1px solid var(--line);
}
.chip:active { transform: scale(0.94); }
.label { font-size: 10px; letter-spacing: 0.14em; text-transform: uppercase; color: var(--foam-dim); }
.dive, .moves { flex: none; display: flex; flex-direction: column; align-items: center; line-height: 1; min-width: 44px; }
.dive b { font-family: var(--serif); font-size: 22px; font-weight: 700; margin-top: 3px; }
.moves { flex-direction: column-reverse; }
.moves b { font-family: var(--serif); font-size: 30px; font-weight: 700; color: var(--gold); margin-bottom: 2px; font-variant-numeric: tabular-nums; }
.moves.low b { color: #ff8a80; animation: pulse 0.9s ease-in-out infinite; }
@keyframes pulse { 50% { transform: scale(1.14); } }

.goals { flex: 1; min-width: 0; display: flex; justify-content: center; align-items: center; gap: 8px; flex-wrap: wrap; }
.goal {
  display: flex; align-items: center; gap: 5px;
  padding: 4px 10px 4px 5px; border-radius: 999px;
  background: rgba(190, 232, 255, 0.1); border: 1px solid var(--line);
  font-weight: 700; font-size: 15px; font-variant-numeric: tabular-nums;
}
.goal img { width: 26px; height: 26px; display: block; }
.goal.text { padding: 6px 12px; font-size: 13px; font-weight: 500; }
.goal.done { background: rgba(120, 230, 160, 0.2); border-color: rgba(120, 230, 160, 0.6); color: #b9ffd3; }
.goal.bump { animation: bump 0.25s ease-out; }
@keyframes bump { 40% { transform: scale(1.16); } }

.scorebar { position: relative; margin-top: 10px; height: 18px; }
.track { position: absolute; left: 0; right: 64px; top: 5px; height: 8px; border-radius: 99px; background: rgba(190, 232, 255, 0.16); overflow: hidden; }
/* scaled, not resized: the score counts up every frame and must not relayout the HUD */
#scoreFill { height: 100%; width: 100%; transform: scaleX(0); transform-origin: left center; background: linear-gradient(90deg, #7be0ff, var(--gold)); }
.mark {
  position: absolute; top: 0; width: 18px; height: 18px; margin-left: -9px;
  background: rgba(190, 232, 255, 0.4);
  clip-path: polygon(50% 4%, 63% 36%, 97% 39%, 71% 61%, 79% 95%, 50% 77%, 21% 95%, 29% 61%, 3% 39%, 37% 36%);
  transition: background 0.2s, transform 0.2s;
}
.mark.on { background: var(--gold); transform: scale(1.2); }
#scoreNum { position: absolute; right: 0; top: 0; width: 60px; text-align: right; font-weight: 700; font-size: 14px; line-height: 18px; font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------ banner + toast */

#banner {
  position: fixed; left: 50%; top: 44%; transform: translate(-50%, -50%);
  z-index: 4; pointer-events: none; text-align: center;
  padding: 16px 26px; border-radius: 22px; width: max-content; max-width: min(88vw, 420px);
  background: var(--panel); border: 1px solid var(--line);
  box-shadow: 0 16px 44px rgba(2, 12, 36, 0.45);
  animation: banner 2.1s ease both;
}
.banner-zone { font-size: 11px; letter-spacing: 0.16em; text-transform: uppercase; color: var(--gold); }
.banner-goal { font-family: var(--serif); font-weight: 600; font-size: 22px; line-height: 1.2; margin-top: 6px; }
@keyframes banner {
  0% { opacity: 0; transform: translate(-50%, -30%) scale(0.94); }
  12%, 80% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -64%) scale(0.98); }
}

#toast {
  position: fixed; left: 50%; bottom: calc(18px + var(--safe-b)); transform: translateX(-50%);
  z-index: 4; pointer-events: none; text-align: center;
  padding: 10px 16px; border-radius: 16px; width: max-content; max-width: min(92vw, 440px);
  background: var(--panel); border: 1px solid var(--line);
  font-size: 14px; line-height: 1.35;
  animation: toast 0.3s ease both;
}
@keyframes toast { from { opacity: 0; transform: translate(-50%, 10px); } }

/* ------------------------------------------------------------ overlays */

.overlay {
  position: fixed; inset: 0; z-index: 6;
  display: grid; place-items: center;
  padding: calc(16px + var(--safe-t)) 16px calc(16px + var(--safe-b));
  background: radial-gradient(120% 90% at 50% 40%, rgba(4, 22, 54, 0.3), rgba(3, 14, 40, 0.74));
  animation: fade 0.22s ease both;
}
@keyframes fade { from { opacity: 0; } }
.card {
  width: min(100%, 340px);
  padding: 26px 22px 22px;
  border-radius: 28px;
  background: linear-gradient(180deg, rgba(14, 52, 104, 0.94), rgba(6, 26, 62, 0.96));
  border: 1px solid var(--line);
  box-shadow: 0 24px 60px rgba(2, 10, 30, 0.55), inset 0 1px 0 rgba(255, 255, 255, 0.12);
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  text-align: center;
  animation: rise 0.32s cubic-bezier(0.2, 0.9, 0.3, 1.2) both;
}
.card.wide { width: min(100%, 420px); max-height: 100%; }
@keyframes rise { from { opacity: 0; transform: translateY(18px) scale(0.96); } }

.crest { width: 96px; height: 96px; margin-bottom: -4px; }
.crest img { width: 100%; height: 100%; display: block; animation: bob 3.2s ease-in-out infinite; }
@keyframes bob { 50% { transform: translateY(-6px) rotate(4deg); } }
.title { font-family: var(--serif); font-weight: 700; font-size: 58px; line-height: 1; letter-spacing: -0.01em; }
.title.small { font-size: 32px; }
.sub { color: var(--foam-dim); font-size: 15px; line-height: 1.4; }
.meta { font-size: 13px; color: var(--foam-dim); }
.hints { font-size: 12px; line-height: 1.5; color: rgba(226, 244, 255, 0.55); margin-top: 4px; }
.row { display: flex; gap: 8px; width: 100%; }
.row .ghost { flex: 1; }

.primary {
  width: 100%; min-height: 56px; margin-top: 8px;
  border-radius: 999px;
  font-family: var(--serif); font-weight: 700; font-size: 22px; color: #3b2500;
  background: linear-gradient(180deg, #ffe08a, var(--gold-deep));
  box-shadow: 0 6px 0 #a86f00, 0 14px 26px rgba(0, 0, 0, 0.3), inset 0 1px 0 rgba(255, 255, 255, 0.6);
  transition: transform 0.08s, box-shadow 0.08s;
}
.primary:active { transform: translateY(4px); box-shadow: 0 2px 0 #a86f00, 0 6px 14px rgba(0, 0, 0, 0.3); }
.ghost {
  width: 100%; min-height: 46px;
  border-radius: 999px;
  font-weight: 500; font-size: 15px;
  background: rgba(190, 232, 255, 0.1); border: 1px solid var(--line);
}
.ghost:active { background: rgba(190, 232, 255, 0.2); }

.over-label { font-size: 12px; letter-spacing: 0.16em; text-transform: uppercase; color: var(--gold); }
.big { font-family: var(--serif); font-weight: 700; font-size: 46px; line-height: 1; font-variant-numeric: tabular-nums; }
.stars { display: flex; gap: 6px; align-items: flex-end; margin: 4px 0; }
.star { width: 56px; height: 56px; fill: rgba(190, 232, 255, 0.16); stroke: rgba(190, 232, 255, 0.3); stroke-width: 1.5; stroke-linejoin: round; }
.star:nth-child(2) { width: 70px; height: 70px; }
.star.on { fill: var(--gold); stroke: #a86f00; animation: starpop 0.45s cubic-bezier(0.2, 0.9, 0.3, 1.6) both; filter: drop-shadow(0 4px 10px rgba(255, 200, 80, 0.5)); }
@keyframes starpop { from { transform: scale(0.2) rotate(-40deg); opacity: 0; } }

.grid {
  width: 100%; display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px;
  overflow-y: auto; max-height: min(56vh, 430px); padding: 4px 2px;
  -webkit-overflow-scrolling: touch;
}
.lvl {
  aspect-ratio: 1; border-radius: 16px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  background: rgba(190, 232, 255, 0.12); border: 1px solid var(--line);
  font-family: var(--serif); font-weight: 700; font-size: 19px;
}
.lvl small { font-family: var(--sans); font-size: 10px; letter-spacing: 1px; color: var(--gold); min-height: 12px; }
.lvl.current { background: linear-gradient(180deg, #ffe08a, var(--gold-deep)); color: #3b2500; border-color: transparent; }
.lvl.locked { opacity: 0.38; }

/* ------------------------------------------------------------ loader */

#loader {
  position: fixed; inset: 0; z-index: 9; display: grid; place-items: center;
  background: linear-gradient(180deg, #27c3d6, #0a5f93);
  transition: opacity 0.4s ease;
}
#loader.done { opacity: 0; pointer-events: none; }
.loader-inner { display: flex; flex-direction: column; align-items: center; gap: 10px; }
.loader-pearl {
  width: 54px; height: 54px; border-radius: 50%;
  background: radial-gradient(circle at 36% 32%, #fff, #f6e9dc 45%, #c8ab9f);
  box-shadow: 0 8px 24px rgba(2, 20, 50, 0.35);
  animation: bob 1.4s ease-in-out infinite;
}
.loader-title { font-family: var(--serif); font-weight: 700; font-size: 34px; }
.loader-sub { font-size: 14px; color: var(--foam-dim); }

@media (max-height: 620px) {
  .title { font-size: 44px; }
  .crest { width: 68px; height: 68px; }
  .hints { display: none; }
  .card { padding: 18px 18px 16px; gap: 8px; }
  .primary { min-height: 48px; }
}

@media (prefers-reduced-motion: reduce) {
  .crest img, .loader-pearl, .moves.low b { animation: none; }
  .card, .overlay, #toast { animation-duration: 0.01s; }
}
