:root {
  color-scheme: light;
  font-family: Inter, ui-rounded, "SF Pro Rounded", "PingFang SC", "Microsoft YaHei", sans-serif;
  color: #514f6f;
  background: #bdcadc;
  --safe-top: max(18px, env(safe-area-inset-top));
  --hud-safe-top: max(72px, calc(env(safe-area-inset-top) + 28px));
  --safe-bottom: max(18px, env(safe-area-inset-bottom));
}

* { box-sizing: border-box; }
html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; }
body { background: #bdcadc; }
button, input { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }
html, body, .game-shell, canvas { -webkit-tap-highlight-color: transparent; }

.page-shell, .game-shell { position: relative; width: 100%; height: 100%; min-height: 100svh; }
.game-shell { overflow: hidden; touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; overscroll-behavior: none; }
canvas { display: block; width: 100%; height: 100%; cursor: pointer; touch-action: none; }

.game-hud {
  position: absolute;
  inset: 0 0 auto;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: calc(var(--hud-safe-top) + 46px);
  padding: var(--hud-safe-top) clamp(18px, 5vw, 28px) 0;
  pointer-events: none;
}

.score { display: flex; align-items: baseline; gap: 8px; color: rgb(255 255 255 / 88%); font-size: 10px; font-weight: 700; letter-spacing: .16em; text-shadow: 0 2px 12px rgb(45 50 76 / 28%); }
.score strong { color: #fff; font-size: clamp(24px, 7vw, 32px); font-weight: 760; font-variant-numeric: tabular-nums; letter-spacing: -.04em; }
.score[hidden] { display: none; }

.flight-ready {
  position: absolute;
  z-index: 4;
  left: 50%;
  bottom: max(18%, calc(env(safe-area-inset-bottom) + 92px));
  padding: 9px 18px;
  border: 1px solid rgb(255 255 255 / 64%);
  border-radius: 999px;
  color: #6f6688;
  background: rgb(252 249 255 / 76%);
  box-shadow: 0 10px 28px rgb(94 83 124 / 12%), inset 0 1px 0 rgb(255 255 255 / 82%);
  font-size: 13px;
  font-weight: 650;
  letter-spacing: .12em;
  transform: translateX(-50%);
  pointer-events: none;
  animation: flight-ready-breathe 1.8s ease-in-out infinite;
}
.flight-ready[hidden] { display: none; }

@keyframes flight-ready-breathe {
  0%, 100% { opacity: .68; transform: translateX(-50%) translateY(0); }
  50% { opacity: 1; transform: translateX(-50%) translateY(-3px); }
}

.game-debug {
  position: absolute;
  z-index: 20;
  left: 8px;
  bottom: calc(var(--safe-bottom) + 8px);
  max-width: calc(100% - 16px);
  margin: 0;
  padding: 8px 10px;
  overflow: hidden;
  border-radius: 8px;
  color: #eef7ff;
  background: rgba(31, 36, 56, .82);
  font: 10px/1.35 ui-monospace, SFMono-Regular, Consolas, monospace;
  white-space: pre-wrap;
  pointer-events: none;
}
.game-debug[hidden] { display: none; }

.game-controls { display: flex; align-items: center; gap: 10px; margin-left: auto; pointer-events: auto; transform: translateY(32px); }
.pause-toggle {
  position: relative;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  min-width: 44px;
  padding: 0;
  border: 1px solid rgb(255 255 255 / 68%);
  border-radius: 50%;
  color: #71658d;
  background: linear-gradient(145deg, rgb(255 253 255 / 86%), rgb(231 225 242 / 74%));
  box-shadow: inset 0 1px 0 rgb(255 255 255 / 86%), 0 7px 20px rgb(78 69 110 / 14%);
  backdrop-filter: blur(3px);
  pointer-events: auto;
  touch-action: manipulation;
}
.pause-toggle[hidden] { display: none; }
.pause-toggle__icon { width: 21px; height: 21px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.pause-toggle__icon .play-mark { display: none; fill: currentColor; stroke: none; }
.pause-toggle.is-paused .pause-mark { display: none; }
.pause-toggle.is-paused .play-mark { display: block; }
.pause-toggle:active { transform: scale(.95); }

.pause-overlay {
  position: absolute;
  z-index: 6;
  left: 50%;
  top: 50%;
  display: grid;
  justify-items: center;
  gap: 4px;
  min-width: 154px;
  padding: 15px 22px 14px;
  border: 1px solid rgb(255 255 255 / 76%);
  border-radius: 22px;
  color: #71658d;
  background: linear-gradient(145deg, rgb(255 253 255 / 88%), rgb(235 229 245 / 80%));
  box-shadow: inset 0 1px 0 rgb(255 255 255 / 90%), 0 14px 34px rgb(78 69 110 / 15%);
  backdrop-filter: blur(3px);
  transform: translate(-50%, -50%);
  touch-action: manipulation;
}
.pause-overlay strong { font-size: 17px; letter-spacing: .12em; }
.pause-overlay span { font-size: 11px; letter-spacing: .08em; opacity: .76; }
.pause-overlay[hidden] { display: none; }

.start-screen, .game-over { position: absolute; inset: 0; display: grid; place-items: center; }
.start-screen {
  z-index: 3;
  padding: calc(var(--safe-top) + 54px) 24px calc(var(--safe-bottom) + 28px);
  background: linear-gradient(180deg, rgb(255 250 255 / 3%), rgb(193 178 207 / 6%));
}
.start-screen[hidden], .game-over[hidden] { display: none; }
.start-menu {
  position: relative;
  z-index: 1;
  display: grid;
  justify-items: center;
  align-content: center;
  width: min(84vw, 370px);
  height: auto;
  padding: clamp(18px, 2.6vh, 24px) 18px clamp(20px, 3vh, 28px);
  border: 1px solid rgb(255 255 255 / 72%);
  border-radius: 28px;
  background: linear-gradient(155deg, rgb(255 255 255 / 72%), rgb(250 244 250 / 58%));
  box-shadow: inset 0 1px 0 rgb(255 255 255 / 84%), 0 20px 48px rgb(87 86 115 / 11%);
  backdrop-filter: blur(3px);
  transform: translateY(2vh);
}

.start-hero {
  position: relative;
  display: grid;
  place-items: center;
  width: clamp(190px, 56vw, 246px);
  height: clamp(178px, 29vh, 250px);
  margin-bottom: clamp(4px, 1.2vh, 12px);
  animation: hero-float 3.8s ease-in-out infinite;
}
.start-hero__halo { position: absolute; width: 86%; aspect-ratio: 1; border-radius: 50%; background: radial-gradient(circle, rgb(255 255 255 / 66%), rgb(255 255 255 / 14%) 53%, transparent 73%); filter: blur(4px); }
.start-hero img { position: relative; width: 100%; height: 100%; object-fit: contain; filter: drop-shadow(0 0 7px rgb(255 255 255 / 90%)) drop-shadow(0 14px 16px rgb(78 88 106 / 10%)); }
.start-menu h1 { margin: 0; color: #74688c; font-size: clamp(34px, 10vw, 46px); font-weight: 720; letter-spacing: .14em; line-height: 1.15; text-indent: .14em; text-shadow: 0 2px 18px rgb(255 255 255 / 72%); }
.start-menu__eyebrow { margin: 8px 0 0; color: rgb(112 99 137 / 66%); font-size: 11px; font-weight: 600; letter-spacing: .18em; }
.start-stats { display: flex; align-items: baseline; gap: 9px; margin: clamp(12px, 2vh, 18px) 0 13px; color: #766d8c; font-size: 12px; font-weight: 620; letter-spacing: .08em; text-shadow: 0 1px 10px rgb(255 255 255 / 82%); }
.start-stats strong { color: #695f84; font-size: 25px; font-weight: 780; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }

.background-picker {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 3px;
  width: min(48vw, 176px);
  margin: -3px 0 13px;
  padding: 3px;
  border: 1px solid rgb(255 255 255 / 58%);
  border-radius: 16px;
  background: rgb(226 223 236 / 31%);
  box-shadow: inset 0 1px 3px rgb(85 80 112 / 6%);
}
.background-option {
  min-height: 30px;
  padding: 0 12px;
  border: 0;
  border-radius: 13px;
  color: rgb(103 97 127 / 68%);
  background: transparent;
  font-size: 11px;
  font-weight: 650;
  letter-spacing: .08em;
  transition: color 160ms ease, background 160ms ease, box-shadow 160ms ease;
  touch-action: manipulation;
}
.background-option.is-active {
  color: #706784;
  background: rgb(255 253 255 / 72%);
  box-shadow: 0 3px 9px rgb(91 86 117 / 9%), inset 0 1px 0 rgb(255 255 255 / 76%);
}

.start-action {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 22px;
  width: min(72vw, 282px);
  min-height: 58px;
  padding: 0 25px 0 36px;
  border: 1px solid rgb(255 255 255 / 44%);
  border-radius: 22px 22px 22px 8px;
  color: #fff;
  background: linear-gradient(135deg, #b9b9d2, #9990bb 58%, #aa94bb);
  box-shadow: inset 0 1px 0 rgb(255 255 255 / 45%), 0 15px 30px rgb(102 88 130 / 14%);
  font-size: 16px;
  font-weight: 670;
  letter-spacing: .09em;
  transition: transform 150ms ease, filter 150ms ease;
  touch-action: manipulation;
}
.start-action span:last-child { font-size: 25px; font-weight: 400; line-height: 1; }
.start-action:active { transform: translateY(2px) scale(.99); filter: brightness(.98); }
.start-action:disabled { opacity: .6; }
.start-hint { margin: 13px 0 0; color: rgb(103 93 127 / 52%); font-size: 10px; letter-spacing: .08em; }

.game-over { z-index: 4; padding: calc(var(--safe-top) + 52px) 0 calc(var(--safe-bottom) + 18px); background: linear-gradient(180deg, rgb(91 91 120 / 16%), rgb(147 128 157 / 12%)); backdrop-filter: blur(2px); }
.result-screen {
  display: grid;
  justify-items: center;
  width: min(84vw, 356px);
  max-height: calc(100svh - var(--safe-top) - var(--safe-bottom) - 76px);
  padding: clamp(18px, 2.8vh, 24px) 22px 20px;
  overflow: auto;
  border: 1px solid rgb(255 255 255 / 66%);
  border-radius: 27px;
  color: #6f6685;
  background: linear-gradient(155deg, rgb(255 253 255 / 89%), rgb(241 228 241 / 78%));
  box-shadow: inset 0 1px 0 rgb(255 255 255 / 92%), 0 22px 54px rgb(94 80 111 / 14%);
  backdrop-filter: blur(6px);
  animation: result-rise 420ms cubic-bezier(.2, .8, .2, 1) both;
}
.result-screen__hero { display: grid; place-items: center; width: clamp(86px, 25vw, 108px); height: clamp(72px, 10.8vh, 96px); margin: -14px 0 13px; animation: result-fall 1.8s ease-in-out infinite; }
.result-screen__hero img { width: 100%; height: 100%; object-fit: contain; filter: drop-shadow(0 8px 10px rgb(62 65 99 / 16%)); }
.result-grid { display: grid; justify-items: center; width: 100%; margin: 0 0 15px; }
.result-grid div { display: flex; align-items: baseline; justify-content: center; }
.result-grid dt { color: #888da3; font-size: 11px; letter-spacing: .08em; }
.result-grid dd { margin: 0; font-variant-numeric: tabular-nums; }
.result-score { flex-direction: column; align-items: center !important; }
.result-score dd { color: #71658d; font-size: clamp(50px, 15vw, 64px); font-weight: 800; line-height: .92; letter-spacing: -.055em; }
.result-score dt { margin-top: 8px; }
.result-best { gap: 8px; margin-top: 13px; }
.result-best dd { color: #666b8d; font-size: 18px; font-weight: 760; }
.result-id { display: grid; gap: 6px; width: 100%; text-align: left; }
.result-id span { padding-left: 3px; color: #7c8199; font-size: 10px; font-weight: 650; letter-spacing: .12em; }
.result-id small { color: #9a9dac; font-size: 9px; font-weight: 500; letter-spacing: .04em; }
.result-id input { width: 100%; height: 42px; padding: 0 14px; border: 1px solid rgb(100 105 143 / 20%); border-radius: 12px; outline: 0; color: #575d7b; background: rgb(255 255 255 / 54%); box-shadow: inset 0 1px 3px rgb(54 59 88 / 5%); font-size: 13px; font-weight: 600; letter-spacing: .06em; }
.result-id input:focus { border-color: rgb(112 109 161 / 55%); box-shadow: 0 0 0 3px rgb(112 109 161 / 10%); }
.result-id input::placeholder { color: rgb(105 109 139 / 38%); }
.result-actions { display: grid; grid-template-columns: .94fr 1.06fr; gap: 9px; width: 100%; margin-top: 13px; }
.result-action { min-width: 0; min-height: 44px; padding: 0 10px; border-radius: 14px; font-size: 12px; font-weight: 680; letter-spacing: .04em; touch-action: manipulation; }
.result-action--primary { color: #fff; border: 1px solid rgb(255 255 255 / 38%); background: linear-gradient(135deg, #aea4c5, #9485b3); box-shadow: 0 8px 18px rgb(105 87 130 / 14%); }
.result-action--secondary { color: #746b89; border: 1px solid rgb(137 119 153 / 17%); background: rgb(255 252 255 / 67%); }
.result-action:active { transform: translateY(1px); }
.result-home { margin-top: 13px; padding: 4px 12px; color: #737890; font-size: 11px; font-weight: 620; letter-spacing: .08em; text-decoration: none; text-underline-offset: 3px; }
.result-home:active { color: #565c7d; text-decoration: underline; }
.result-page-fallback { margin-top: 9px; color: #676c8c; font-size: 10px; text-underline-offset: 3px; }
.result-page-fallback[hidden] { display: none; }
button:focus-visible, input:focus-visible { outline: 3px solid rgb(111 108 160 / 30%); outline-offset: 3px; }

@keyframes hero-float { 0%, 100% { transform: translate3d(0, 3px, 0) rotate(-1deg); } 50% { transform: translate3d(0, -10px, 0) rotate(1.4deg); } }
@keyframes result-rise { from { opacity: 0; transform: translateY(18px) scale(.97); } to { opacity: 1; transform: translateY(0) scale(1); } }
@keyframes result-fall { 0%, 100% { transform: translateY(-3px) rotate(-2deg); } 50% { transform: translateY(5px) rotate(2deg); } }

@media (max-width: 360px), (max-height: 670px) {
  .start-screen { padding-top: calc(var(--safe-top) + 44px); }
  .start-menu { padding-block: 15px 18px; }
  .start-hero { width: clamp(164px, 49vw, 196px); height: clamp(144px, 25vh, 184px); }
  .start-stats { margin-top: 20px; }
  .result-screen { width: min(84vw, 330px); padding-block: 16px; }
  .result-screen__hero { height: 68px; }
  .result-best { margin-top: 9px; }
}

@media (min-width: 600px) and (orientation: portrait) {
  .start-hero { width: 270px; height: 270px; }
  .start-action { width: 292px; }
}

@media (min-width: 700px) and (min-aspect-ratio: 4 / 3) {
  .start-screen { padding: clamp(24px, 5vh, 42px); }
  .start-menu {
    grid-template-columns: minmax(220px, .88fr) minmax(300px, 1.12fr);
    grid-template-areas:
      "hero title"
      "hero eyebrow"
      "hero stats"
      "hero picker"
      "hero action"
      "hero hint";
    column-gap: clamp(34px, 5vw, 62px);
    align-items: center;
    width: min(76vw, 760px);
    min-height: min(62vh, 354px);
    padding: 28px 44px;
    border-radius: 32px;
    transform: none;
  }
  .start-hero {
    grid-area: hero;
    justify-self: center;
    width: min(25vw, 238px);
    height: min(47vh, 236px);
    margin: 0;
  }
  .start-menu h1 { grid-area: title; align-self: end; font-size: clamp(38px, 4.4vw, 52px); }
  .start-menu__eyebrow { grid-area: eyebrow; align-self: start; margin-top: 7px; }
  .start-stats { grid-area: stats; margin: 18px 0 12px; }
  .background-picker { grid-area: picker; width: min(100%, 202px); margin: 0 0 14px; }
  .start-action { grid-area: action; width: min(100%, 320px); min-height: 54px; }
  .start-hint { grid-area: hint; margin-top: 12px; }

  .game-over { padding: clamp(24px, 5vh, 44px); }
  .result-screen {
    grid-template-columns: minmax(210px, .82fr) minmax(310px, 1.18fr);
    grid-template-areas:
      "hero form"
      "score actions"
      "score home"
      "score fallback";
    column-gap: clamp(36px, 4.5vw, 58px);
    row-gap: 0;
    align-items: center;
    width: min(76vw, 730px);
    max-height: min(78vh, 410px);
    padding: 30px 42px 28px;
    overflow: visible;
    border-radius: 30px;
  }
  .result-screen__hero {
    grid-area: hero;
    justify-self: center;
    width: 156px;
    height: 132px;
    margin: -10px 0 2px;
  }
  .result-grid { grid-area: score; align-self: start; margin: -4px 0 0; }
  .result-score dd { font-size: 66px; }
  .result-best { margin-top: 14px; }
  .result-id { grid-area: form; align-self: end; }
  .result-actions { grid-area: actions; align-self: start; margin-top: 18px; }
  .result-home { grid-area: home; margin-top: 15px; }
  .result-page-fallback { grid-area: fallback; }
}

@media (prefers-reduced-motion: reduce) {
  .start-hero, .result-screen, .result-screen__hero { animation: none; }
}
