/* Viewport: Spielspalte so breit/hoch wie möglich (4:3), logisch weiter 800×600 */

html {
  -webkit-text-size-adjust: 100%;
}

html,
body {
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
}

:root {
  --layout-chrome: clamp(4.25rem, 11vh, 8rem);
  --play-fit-w: min(
    calc(100vw - 0.85rem - env(safe-area-inset-left) - env(safe-area-inset-right)),
    calc((100dvh - var(--layout-chrome)) * 4 / 3)
  );
}

.app {
  display: grid;
  width: 100%;
  min-height: 100dvh;
  max-height: 100dvh;
  height: 100dvh;
  padding:
    max(0.25rem, env(safe-area-inset-top))
    max(0.35rem, env(safe-area-inset-left))
    max(0.2rem, env(safe-area-inset-bottom))
    max(0.35rem, env(safe-area-inset-right));
  gap: clamp(0.25rem, 0.8vh, 0.55rem);
  grid-template-columns: minmax(0, 1fr) var(--play-fit-w) minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-areas:
    ". hud ."
    ". stage ."
    ". touch .";
  align-items: stretch;
  justify-items: stretch;
}

.hud,
.stage,
.touch-bar {
  width: 100%;
  max-width: none;
  justify-self: stretch;
}

.hud {
  grid-area: hud;
}

.stage {
  grid-area: stage;
  flex: unset;
  min-height: 0;
  min-width: 0;
  height: 100%;
  max-height: 100%;
  align-self: stretch;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  container-type: size;
  container-name: stage;
}

.touch-bar {
  grid-area: touch;
}

#game-canvas {
  touch-action: none;
  display: block;
  flex: 0 0 auto;
  margin: 0;
  aspect-ratio: var(--play-canvas-ratio);
  width: min(100cqw, calc(100cqh * 4 / 3));
  height: min(100cqh, calc(100cqw * 3 / 4));
  max-width: 100%;
  max-height: 100%;
}

@media (pointer: coarse) {
  .stage--touch-hint::after {
    content: none;
  }
}

@media (max-width: 600px) {
  .hud__brand {
    display: none;
  }

  .overlay__hint {
    font-size: 0.78rem;
    line-height: 1.45;
  }
}

/* Touch-Tablet: HUD + Leiste in --layout-chrome (nicht Phone, nicht feine Maus) */
@media (pointer: coarse) and (min-width: 901px) {
  :root {
    --layout-chrome: clamp(6.5rem, 17vh, 9.5rem);
  }
}

@media (hover: none), (max-width: 900px) {
  :root {
    --layout-chrome: clamp(5.5rem, 15vh, 9.5rem);
  }
}

/* Phone (JS: html.phone-play): Vollbild-Canvas + Mini-HUD */
html.phone-play {
  --phone-hud-chrome: calc(2rem + env(safe-area-inset-top, 0px));
  --layout-chrome: var(--phone-hud-chrome);
}

html.phone-play .stage {
  grid-area: stage;
  z-index: 1;
  align-self: stretch;
  height: 100%;
  max-height: none;
}

html.phone-play #game-canvas {
  margin-top: var(--phone-hud-chrome);
  max-height: calc(100cqh - var(--phone-hud-chrome));
}

html.phone-play .app {
  grid-template-rows: minmax(0, 1fr);
  grid-template-areas: ". stage .";
  gap: 0;
  padding:
    env(safe-area-inset-top)
    env(safe-area-inset-right)
    env(safe-area-inset-bottom)
    env(safe-area-inset-left);
}

html.phone-play .touch-bar {
  display: none !important;
}

html.phone-play .hud {
  grid-area: stage;
  align-self: start;
  z-index: 25;
  position: relative;
  pointer-events: none;
  min-height: 1.65rem;
  max-height: 1.65rem;
  padding: 0.2rem 0.45rem;
  margin: max(0.25rem, env(safe-area-inset-top))
    max(0.2rem, env(safe-area-inset-left))
    0
    max(0.2rem, env(safe-area-inset-right));
  width: auto;
  border-radius: 8px;
  opacity: 0.94;
}

html.phone-play .hud__brand {
  display: none;
}

html.phone-play .hud__stats {
  width: 100%;
  justify-content: space-between;
  gap: 0.15rem 0.35rem;
  font-size: clamp(0.58rem, 2.4vw, 0.7rem);
}

html.phone-play .hud__label {
  font-size: 0.52rem;
}

html.phone-play .hud__powerups {
  max-width: 4rem;
}

html.phone-play .stage::before {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  border-radius: inherit;
  background: linear-gradient(
    90deg,
    rgba(62, 240, 255, 0.07) 0%,
    transparent 26%,
    transparent 74%,
    rgba(62, 240, 255, 0.07) 100%
  );
  opacity: 0.85;
}

/* Tablet Touch: HUD eigene Zeile (nie über dem Canvas) */
@media (pointer: coarse) and (min-width: 901px) {
  .hud {
    grid-area: hud;
    position: static;
    z-index: auto;
    margin: 0;
    min-height: 2.1rem;
    max-height: 2.1rem;
    padding: 0.3rem 0.55rem;
  }

  .hud__brand {
    display: none;
  }

  .hud__stats {
    width: 100%;
    justify-content: space-between;
    gap: 0.25rem 0.45rem;
    font-size: clamp(0.62rem, 1.8vh, 0.78rem);
  }
}

@media (orientation: landscape) and (pointer: coarse) and (min-width: 901px) {
  :root {
    --layout-chrome: clamp(6.5rem, 17vh, 9rem);
  }

  .app {
    grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-areas:
      ". hud ."
      ". stage ."
      ". touch .";
  }
}

@media (orientation: portrait) and (max-width: 480px) {
  .touch-btn {
    min-height: 3.1rem;
  }

  .touch-btn--fire {
    min-height: 3.25rem;
  }
}
