:root {
  --app-viewport-height: 100dvh;
  --bg-top: #112333;
  --bg-bottom: #040c17;
  --panel: rgba(6, 17, 32, 0.78);
  --line: #7ce2f7;
  --line-soft: rgba(124, 226, 247, 0.35);
  --player: #ffd57a;
  --player-edge: #ff9f43;
  --rect: #f04d63;
  --rect-top: #ff7d81;
  --tri: #72ffd2;
  --text-main: #e7f5ff;
  --text-soft: #a5bfd0;
  --accent: #ffe35c;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
}

body {
  font-family: "Trebuchet MS", "Lucida Sans Unicode", "Verdana", sans-serif;
  color: var(--text-main);
  background:
    radial-gradient(120% 120% at 12% 8%, #24486a 0%, transparent 58%),
    radial-gradient(95% 95% at 92% 88%, #1c3452 0%, transparent 56%),
    linear-gradient(155deg, var(--bg-top), var(--bg-bottom));
  overflow: hidden;
  touch-action: none;
}

.game-shell {
  width: 100%;
  height: 100vh;
  height: var(--app-viewport-height, 100dvh);
  display: block;
  padding:
    max(4px, env(safe-area-inset-top))
    max(4px, env(safe-area-inset-right))
    max(4px, env(safe-area-inset-bottom))
    max(4px, env(safe-area-inset-left));
}

.hud-controls {
  display: inline-flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  align-items: center;
  gap: 6px;
}

.hud-control-button {
  border: 1px solid rgba(255, 255, 255, 0.26);
  border-radius: 12px;
  background: linear-gradient(180deg, rgba(95, 217, 246, 0.25), rgba(19, 74, 104, 0.35));
  color: var(--text-main);
  font-size: clamp(14px, 2vw, 20px);
  font-weight: 700;
  width: clamp(34px, 4.6vw, 44px);
  height: clamp(34px, 4.6vw, 44px);
  display: inline-grid;
  place-items: center;
  padding: 0;
  text-decoration: none;
  line-height: 1;
  cursor: pointer;
  box-shadow:
    0 8px 16px rgba(0, 0, 0, 0.2),
    inset 0 -6px 10px rgba(0, 0, 0, 0.2),
    inset 0 2px 6px rgba(255, 255, 255, 0.2);
}

.icon-button .icon {
  line-height: 1;
}

.hud-control-button:hover {
  filter: brightness(1.06);
}

.hud-control-button:active {
  transform: translateY(1px) scale(0.98);
}

.arena {
  position: relative;
  height: 100%;
  border-radius: 14px;
  overflow: hidden;
  border: 1px solid rgba(124, 226, 247, 0.35);
  box-shadow:
    inset 0 0 0 1px rgba(255, 255, 255, 0.08),
    0 18px 40px rgba(0, 0, 0, 0.38);
}

.game-top-overlay {
  position: absolute;
  top: max(8px, env(safe-area-inset-top));
  left: max(8px, env(safe-area-inset-left));
  right: max(8px, env(safe-area-inset-right));
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  z-index: 7;
  pointer-events: none;
}

.stage-overlay {
  min-width: min(54vw, 340px);
  display: grid;
  gap: 5px;
  padding: 8px 10px;
  border-radius: 10px;
  background: rgba(5, 16, 31, 0.58);
  border: 1px solid rgba(124, 226, 247, 0.35);
  box-shadow:
    0 6px 16px rgba(0, 0, 0, 0.28),
    inset 0 1px 0 rgba(255, 255, 255, 0.08);
}

.stage-chip {
  font-size: clamp(12px, 1.7vw, 16px);
  font-weight: 800;
  letter-spacing: 0.03em;
  text-shadow: 0 0 8px rgba(13, 29, 47, 0.7);
}

.stage-chip span {
  color: var(--accent);
}

.stage-progress {
  height: 10px;
  border-radius: 999px;
  overflow: hidden;
  background: rgba(6, 20, 32, 0.78);
  border: 1px solid rgba(255, 255, 255, 0.26);
}

#stageProgressFill {
  display: block;
  width: 0%;
  height: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, #ffe35c, #ffa13a);
  transition: width 160ms ease;
}

.stage-progress-meta {
  justify-self: end;
  font-size: clamp(11px, 1.35vw, 14px);
  font-weight: 700;
  color: #cfe5f5;
}

.game-top-overlay .hud-controls {
  pointer-events: auto;
}

#gameCanvas {
  width: 100%;
  height: 100%;
  display: block;
}

.jump-button {
  position: absolute;
  right: max(12px, env(safe-area-inset-right));
  bottom: max(12px, env(safe-area-inset-bottom));
  width: clamp(68px, 13vw, 122px);
  height: clamp(68px, 13vw, 122px);
  border: none;
  border-radius: 50%;
  background: radial-gradient(circle at 28% 26%, #fff3b6, #ffc64b 58%, #dd8500 100%);
  color: #2d1803;
  font-weight: 800;
  font-size: clamp(15px, 2.3vw, 24px);
  cursor: pointer;
  box-shadow:
    0 12px 25px rgba(0, 0, 0, 0.3),
    inset 0 -8px 14px rgba(0, 0, 0, 0.24),
    inset 0 4px 8px rgba(255, 255, 255, 0.58);
  transform: translateY(0);
  transition: transform 90ms ease-out, filter 120ms ease-out;
  z-index: 6;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
}

.jump-button:active {
  transform: translateY(3px) scale(0.98);
  filter: brightness(0.95);
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  place-items: center;
  gap: 12px;
  text-align: center;
  background: linear-gradient(160deg, rgba(8, 22, 42, 0.88), rgba(2, 10, 20, 0.88));
  z-index: 8;
  opacity: 0;
  pointer-events: none;
  transition: opacity 220ms ease-out;
  overflow-y: auto;
  padding:
    max(12px, env(safe-area-inset-top))
    max(10px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom))
    max(10px, env(safe-area-inset-left));
}

.overlay.visible {
  opacity: 1;
  pointer-events: auto;
}

.overlay.idle {
  background: linear-gradient(160deg, rgba(8, 22, 42, 0.9), rgba(2, 10, 20, 0.9));
}

.overlay.success {
  background:
    radial-gradient(circle at 24% 22%, rgba(175, 255, 198, 0.26), transparent 52%),
    linear-gradient(162deg, rgba(18, 87, 58, 0.92), rgba(8, 36, 26, 0.9));
}

.overlay.fail {
  background:
    radial-gradient(circle at 72% 24%, rgba(255, 176, 176, 0.24), transparent 48%),
    linear-gradient(162deg, rgba(88, 20, 35, 0.92), rgba(30, 8, 15, 0.9));
}

.overlay-badge {
  min-width: 110px;
  border-radius: 999px;
  padding: 8px 14px;
  font-size: clamp(12px, 1.8vw, 18px);
  font-weight: 900;
  letter-spacing: 0.06em;
  border: 1px solid rgba(255, 255, 255, 0.35);
  text-transform: uppercase;
  box-shadow:
    0 8px 22px rgba(0, 0, 0, 0.32),
    inset 0 -6px 10px rgba(0, 0, 0, 0.2);
}

.overlay.idle .overlay-badge {
  background: linear-gradient(180deg, #57d8ff, #1c9dc6);
  color: #032132;
}

.overlay.success .overlay-badge {
  background: linear-gradient(180deg, #92ff8d, #2bcf7c);
  color: #053d23;
  animation: badgeBounce 900ms ease-in-out infinite;
}

.overlay.fail .overlay-badge {
  background: linear-gradient(180deg, #ff9ea8, #ed5168);
  color: #4d0615;
}

.overlay h1 {
  margin: 0;
  font-size: clamp(24px, 4.2vw, 48px);
  letter-spacing: 0.03em;
}

.overlay p {
  margin: 0;
  color: var(--text-soft);
  font-size: clamp(13px, 2vw, 24px);
}

.overlay button {
  margin-top: 6px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 10px;
  padding: 10px 18px;
  font-weight: 700;
  font-size: clamp(14px, 2vw, 22px);
  background: linear-gradient(180deg, #27cae4, #1485aa);
  color: white;
  cursor: pointer;
}

.overlay-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.overlay button.secondary {
  background: linear-gradient(180deg, #657d98, #3c4f66);
}

.hidden {
  display: none !important;
}

.leaderboard-card {
  width: min(88vw, 560px);
  margin-top: 4px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  background: rgba(3, 10, 20, 0.42);
  padding: 10px 12px;
  text-align: left;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.06);
  max-height: min(56vh, 380px);
  overflow: hidden;
}

.leaderboard-inputs {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}

.leaderboard-inputs input {
  width: 100%;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.26);
  background: rgba(4, 14, 24, 0.78);
  color: var(--text-main);
  padding: 8px 10px;
  font-size: clamp(12px, 1.6vw, 16px);
}

.leaderboard-inputs button {
  margin: 0;
  padding: 8px 12px;
  font-size: clamp(12px, 1.7vw, 15px);
  border-radius: 8px;
}

.leaderboard-inputs button:disabled,
.leaderboard-inputs input:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.leaderboard-inputs.needs-name {
  animation: inputNudge 380ms ease-out;
}

.save-status-indicator {
  margin: 8px 0 8px;
  min-height: 34px;
  display: grid;
  place-items: center;
}

.save-status-icon {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: inline-grid;
  place-items: center;
  font-size: 19px;
  box-shadow:
    0 0 0 2px rgba(255, 255, 255, 0.2),
    0 8px 18px rgba(0, 0, 0, 0.3);
  background: radial-gradient(circle at 30% 28%, #fff8d6 0%, #ffe48c 54%, #d69e26 100%);
}

.save-status-indicator.state-prompt .save-status-icon {
  background: radial-gradient(circle at 30% 28%, #fff8d6 0%, #ffe48c 54%, #d69e26 100%);
  animation: statusPulse 1.1s ease-in-out infinite;
}

.save-status-indicator.state-saved .save-status-icon {
  background: radial-gradient(circle at 28% 24%, #e7ffd8 0%, #8bffa8 52%, #26b96f 100%);
  animation: statusPop 620ms ease-out;
}

.save-status-indicator.state-locked .save-status-icon {
  background: radial-gradient(circle at 32% 28%, #f2f8ff 0%, #b2d4ff 52%, #5d87d4 100%);
  animation: statusPulse 0.95s ease-out 2;
}

.save-status-indicator.state-error .save-status-icon {
  background: radial-gradient(circle at 28% 24%, #ffe2e2 0%, #ff9ea0 52%, #d74863 100%);
  animation: statusShake 460ms ease-out;
}

.leaderboard-podium {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  align-items: end;
  min-height: 120px;
  margin-bottom: 8px;
}

.podium-card {
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.28);
  text-align: center;
  padding: 8px 6px;
  display: grid;
  gap: 2px;
  box-shadow:
    0 10px 18px rgba(0, 0, 0, 0.25),
    inset 0 0 0 1px rgba(255, 255, 255, 0.08);
}

.podium-card .podium-medal {
  font-size: clamp(16px, 2.2vw, 24px);
}

.podium-card .podium-rank {
  font-size: clamp(11px, 1.3vw, 13px);
  font-weight: 900;
  letter-spacing: 0.05em;
}

.podium-card .podium-name {
  font-size: clamp(12px, 1.45vw, 15px);
  font-weight: 800;
}

.podium-card .podium-score {
  font-size: clamp(11px, 1.35vw, 14px);
  color: #17344a;
  font-weight: 800;
}

.podium-card .podium-stage {
  font-size: clamp(10px, 1.2vw, 12px);
  color: rgba(8, 24, 38, 0.78);
  font-weight: 700;
}

.podium-card.rank-1 {
  order: 2;
  min-height: 116px;
  background: linear-gradient(180deg, #ffe89c, #f4c64d);
  transform: translateY(-8px);
}

.podium-card.rank-2 {
  order: 1;
  min-height: 98px;
  background: linear-gradient(180deg, #edf1f6, #c0ccd8);
}

.podium-card.rank-3 {
  order: 3;
  min-height: 90px;
  background: linear-gradient(180deg, #f1d0a9, #cc8f59);
}

.podium-empty {
  grid-column: 1 / -1;
  text-align: center;
  color: #cfe1ee;
  font-size: clamp(12px, 1.4vw, 14px);
  align-self: center;
}

.leaderboard-list {
  margin: 0;
  padding-left: 0;
  max-height: min(148px, 26vh);
  overflow: auto;
  font-size: clamp(12px, 1.55vw, 15px);
  color: #ebf8ff;
  list-style: none;
}

.leaderboard-list li {
  margin-bottom: 5px;
}

.leaderboard-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 8px;
  background: rgba(9, 22, 36, 0.42);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.leaderboard-row .lb-rank {
  font-weight: 900;
  color: #ffe36b;
}

.leaderboard-row .lb-name {
  font-weight: 700;
}

.leaderboard-row .lb-meta {
  color: #bad2e4;
  font-size: clamp(10px, 1.2vw, 13px);
}

@keyframes badgeBounce {
  0%,
  100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-3px);
  }
}

@keyframes statusPop {
  0% {
    transform: scale(0.5);
    opacity: 0;
  }
  55% {
    transform: scale(1.14);
    opacity: 1;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes statusPulse {
  0%,
  100% {
    transform: scale(1);
    box-shadow:
      0 0 0 2px rgba(255, 255, 255, 0.2),
      0 8px 18px rgba(0, 0, 0, 0.3);
  }
  50% {
    transform: scale(1.08);
    box-shadow:
      0 0 0 2px rgba(255, 255, 255, 0.24),
      0 10px 21px rgba(0, 0, 0, 0.34),
      0 0 18px rgba(255, 232, 136, 0.38);
  }
}

@keyframes statusShake {
  0% {
    transform: translateX(0);
  }
  20% {
    transform: translateX(-4px);
  }
  40% {
    transform: translateX(4px);
  }
  60% {
    transform: translateX(-3px);
  }
  80% {
    transform: translateX(3px);
  }
  100% {
    transform: translateX(0);
  }
}

@keyframes inputNudge {
  0% {
    transform: translateX(0);
  }
  22% {
    transform: translateX(-5px);
  }
  44% {
    transform: translateX(5px);
  }
  66% {
    transform: translateX(-3px);
  }
  88% {
    transform: translateX(3px);
  }
  100% {
    transform: translateX(0);
  }
}

.portrait-hint {
  position: absolute;
  inset: 0;
  display: none;
  place-content: center;
  gap: 8px;
  text-align: center;
  background: rgba(2, 10, 20, 0.88);
  font-weight: 700;
  color: var(--text-main);
  z-index: 10;
}

.portrait-hint p {
  margin: 0;
}

@media (orientation: portrait) and (max-width: 1200px) {
  .portrait-hint {
    display: grid;
  }
}

@media (orientation: landscape) and (max-height: 460px) {
  .game-top-overlay {
    top: max(6px, env(safe-area-inset-top));
    left: max(6px, env(safe-area-inset-left));
    right: max(6px, env(safe-area-inset-right));
  }

  .stage-overlay {
    min-width: min(62vw, 280px);
    padding: 6px 8px;
    gap: 4px;
  }

  .stage-progress {
    height: 8px;
  }

  .overlay {
    place-content: start center;
    gap: 8px;
  }

  .overlay h1 {
    font-size: clamp(18px, 3.6vw, 30px);
  }

  .overlay p {
    font-size: clamp(11px, 1.9vw, 14px);
  }

  .overlay button {
    padding: 8px 14px;
    font-size: clamp(12px, 1.8vw, 15px);
  }

  .leaderboard-card {
    width: min(94vw, 500px);
    max-height: min(64vh, 250px);
    padding: 8px 10px;
  }

  .leaderboard-podium {
    min-height: 74px;
    margin-bottom: 6px;
    gap: 6px;
  }

  .podium-card.rank-1 {
    min-height: 84px;
    transform: translateY(-4px);
  }

  .podium-card.rank-2 {
    min-height: 76px;
  }

  .podium-card.rank-3 {
    min-height: 70px;
  }

  .leaderboard-list {
    max-height: min(82px, 24vh);
  }
}

@media (max-width: 760px) {
  .game-top-overlay {
    gap: 8px;
  }

  .stage-overlay {
    min-width: min(60vw, 260px);
    padding: 7px 8px;
  }

  .hud-controls {
    gap: 4px;
  }

  .hud-control-button {
    width: clamp(32px, 8.8vw, 38px);
    height: clamp(32px, 8.8vw, 38px);
    border-radius: 10px;
    font-size: clamp(13px, 3.5vw, 17px);
  }

  .leaderboard-inputs {
    grid-template-columns: 1fr;
  }
}
