*,
*::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: #c8c3bf;
  font-family: "Comfortaa", 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%;
  height: 100%;
  max-width: min(560px, 100vw);
  max-height: calc(100dvh - 16px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  border-radius: clamp(0px, 2vw, 16px);
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25), 0 0 0 2px rgba(51, 51, 68, 0.08);
  touch-action: manipulation;
}

#game {
  position: absolute;
  inset: 0;
}

#game canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
  touch-action: none;
}

#score {
  position: absolute;
  top: calc(12px + env(safe-area-inset-top, 0px));
  left: 0;
  width: 100%;
  text-align: center;
  font-size: clamp(2.5rem, 10vh, 4.5rem);
  font-weight: 700;
  line-height: 1;
  transition: transform 0.5s ease;
  color: #334;
  transform: translateY(-200px) scale(1);
  pointer-events: none;
  z-index: 2;
}

.high-score {
  position: absolute;
  top: calc(12px + env(safe-area-inset-top, 0px));
  right: 16px;
  font-size: clamp(0.75rem, 2.5vw, 0.9rem);
  color: rgba(51, 51, 68, 0.75);
  pointer-events: none;
  z-index: 2;
}

.instructions {
  position: absolute;
  width: 100%;
  top: calc(14vh + env(safe-area-inset-top, 0px));
  left: 0;
  padding: 0 16px;
  text-align: center;
  font-size: clamp(0.85rem, 3vw, 1rem);
  color: #334;
  transition: opacity 0.5s ease, transform 0.5s ease;
  opacity: 0;
  pointer-events: none;
  z-index: 2;
}

.instructions.hide {
  opacity: 0 !important;
}

.game-over {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  text-align: center;
  z-index: 10;
  pointer-events: none;
}

.game-over * {
  transition: opacity 0.5s ease, transform 0.5s ease;
  opacity: 0;
  transform: translateY(-50px);
  color: #334;
}

.game-over h2 {
  margin: 0 0 8px;
  font-size: clamp(1.75rem, 6vw, 2.5rem);
}

.game-over p {
  margin: 6px 0;
  font-size: clamp(0.95rem, 3.5vw, 1.1rem);
}

.game-ready {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(208, 203, 199, 0.92);
  z-index: 11;
}

.start-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  max-width: 320px;
}

.game-title {
  margin: 0 0 12px;
  font-size: clamp(2rem, 8vw, 2.75rem);
  color: #334;
}

.start-desc {
  margin: 0 0 24px;
  font-size: clamp(0.9rem, 3.5vw, 1.05rem);
  color: rgba(51, 51, 68, 0.85);
  line-height: 1.5;
}

.btn-start {
  padding: 14px 40px;
  min-height: 48px;
  min-width: 140px;
  font-family: inherit;
  font-size: clamp(1.1rem, 4vw, 1.35rem);
  font-weight: 700;
  color: #334;
  background: transparent;
  border: 3px solid #334;
  border-radius: 999px;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.15s ease, background 0.15s ease, color 0.15s ease;
}

.btn-start:hover {
  background: #334;
  color: #d0cbc7;
}

.btn-start:active {
  transform: scale(0.97);
}

#container.playing #score,
#container.resetting #score {
  transform: translateY(0) scale(1);
}

#container.playing .instructions {
  opacity: 1;
}

#container.ready .game-ready {
  pointer-events: auto;
}

#container.playing .game-ready,
#container.ended .game-ready,
#container.resetting .game-ready {
  display: none;
}

#container.ended #score {
  transform: translateY(6vh) scale(1.5);
}

#container.ended .game-over * {
  opacity: 1;
  transform: translateY(0);
}

#container.ended .game-over p {
  transition-delay: 0.3s;
}

#container.ended .game-over {
  pointer-events: auto;
}

@media (min-width: 768px) {
  .app-shell {
    padding: 16px;
  }

  .game-wrapper {
    max-height: calc(100dvh - 32px);
    border-radius: 16px;
  }
}

@media (max-height: 500px) and (orientation: landscape) {
  .game-ready {
    flex-direction: row;
    justify-content: center;
    gap: 24px;
  }

  .start-panel {
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    max-width: 90%;
  }

  .game-title {
    width: 100%;
    margin-bottom: 4px;
  }

  .start-desc {
    margin-bottom: 12px;
  }

  .instructions {
    top: 10vh;
  }
}
