*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  height: 100%;
  overscroll-behavior: none;
}

body {
  margin: 0;
  min-height: 100dvh;
  height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(180deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
  font-family: "Nunito", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  overscroll-behavior: none;
  overflow: hidden;
}

.app-shell {
  width: 100%;
  height: 100%;
  max-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
}

.game-wrapper {
  position: relative;
  width: 100%;
  max-width: min(480px, 100vw);
  max-height: calc(100dvh - 16px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5), 0 0 0 4px rgba(255, 255, 255, 0.06);
  display: flex;
  flex-direction: column;
}

#gameCanvas {
  display: block;
  width: 100%;
  flex: 1 1 auto;
  min-height: 0;
  aspect-ratio: 480 / 600;
  max-height: calc(100dvh - 120px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  height: auto;
  background: linear-gradient(180deg, #87ceeb 0%, #e0f6ff 40%, #98d8aa 70%, #7bc67e 100%);
  cursor: pointer;
  touch-action: none;
}

.game-ui {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  padding: calc(12px + env(safe-area-inset-top, 0px)) 16px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  pointer-events: none;
  background: linear-gradient(to bottom, rgba(0, 0, 0, 0.35), transparent);
  z-index: 2;
}

.score-label,
.high-score-label {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: rgba(255, 255, 255, 0.85);
  margin-right: 4px;
}

.score-value,
.high-score-value {
  font-family: "Fredoka One", "Trebuchet MS", sans-serif;
  font-size: clamp(18px, 4.5vw, 22px);
  color: #fff;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.4);
}

.high-score-value {
  color: #ffd93d;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.75);
  padding: 24px;
  text-align: center;
  z-index: 10;
}

.overlay.hidden {
  display: none;
}

.overlay-start h1 {
  font-family: "Fredoka One", "Trebuchet MS", sans-serif;
  font-size: clamp(2rem, 8vw, 2.5rem);
  color: #fff;
  margin: 0 0 16px;
  text-shadow: 0 4px 0 #6bcb77, 0 6px 12px rgba(0, 0, 0, 0.4);
}

.overlay-start p {
  color: rgba(255, 255, 255, 0.9);
  margin: 8px 0;
  font-size: clamp(0.9rem, 3.5vw, 1rem);
}

.overlay-start kbd {
  display: inline-block;
  padding: 4px 8px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 6px;
  font-family: inherit;
  font-size: 0.9em;
}

.btn-start {
  margin-top: 24px;
  padding: 14px 40px;
  min-height: 48px;
  font-family: "Fredoka One", "Trebuchet MS", sans-serif;
  font-size: 1.25rem;
  color: #1a1a2e;
  background: linear-gradient(180deg, #6bcb77 0%, #4ade80 100%);
  border: none;
  border-radius: 999px;
  cursor: pointer;
  box-shadow: 0 6px 0 #2d8a3e, 0 8px 20px rgba(0, 0, 0, 0.3);
  transition: transform 0.1s, box-shadow 0.1s;
  touch-action: manipulation;
}

.btn-start:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 0 #2d8a3e, 0 10px 24px rgba(0, 0, 0, 0.35);
}

.btn-start:active {
  transform: translateY(2px);
  box-shadow: 0 4px 0 #2d8a3e, 0 6px 16px rgba(0, 0, 0, 0.3);
}

.overlay-gameover h2 {
  font-family: "Fredoka One", "Trebuchet MS", sans-serif;
  font-size: clamp(1.75rem, 6vw, 2rem);
  color: #ff6b6b;
  margin: 0 0 8px;
}

.overlay-gameover p {
  color: rgba(255, 255, 255, 0.95);
  font-size: 1.1rem;
  margin: 8px 0;
}

.overlay-gameover #finalScoreEl {
  font-family: "Fredoka One", "Trebuchet MS", sans-serif;
  color: #ffd93d;
}

.mobile-controls {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 12px 20px calc(16px + env(safe-area-inset-bottom, 0px));
  display: flex;
  justify-content: space-between;
  align-items: center;
  pointer-events: none;
  z-index: 5;
}

.mobile-controls .ctrl-btn {
  pointer-events: auto;
  width: 64px;
  height: 64px;
  min-width: 48px;
  min-height: 48px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.25);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  color: #fff;
  font-size: 28px;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
  transition: transform 0.1s, background 0.1s;
}

.mobile-controls .ctrl-btn:active,
.mobile-controls .ctrl-btn.active {
  background: rgba(107, 203, 119, 0.8);
  transform: scale(0.95);
}

@media (hover: hover) {
  .mobile-controls .ctrl-btn:hover {
    background: rgba(255, 255, 255, 0.35);
  }
}

.overlay-start .mobile-hint {
  font-size: 0.9rem;
  color: rgba(255, 255, 255, 0.75);
}

@media (min-width: 768px) {
  .app-shell {
    padding: 16px;
  }

  .game-wrapper {
    max-height: calc(100dvh - 32px);
  }
}
