/**
 * Responsive layout — Cosmic Zorbies
 * Viewport tokens, scroll shell, height/width breakpoints
 */

:root {
  --app-vh: 100dvh;
  --app-vh-fallback: 100vh;
  --pad-inline: clamp(0.5rem, 2.5vw, 1.25rem);
  --pad-block: clamp(0.35rem, 1.2vh, 0.85rem);
  --stage-h: clamp(168px, 30dvh, 320px);
  --stage-pad-y: clamp(24px, 6vw, 52px);
  --hero-gap: clamp(0.35rem, 1.5vh, 1.25rem);
  --h1-play: clamp(1.35rem, 4.5vw, 2.25rem);
  --cards-gap: clamp(0.4rem, 1.2vw, 0.85rem);
  --ui-max-w: min(94vw, 720px);
}

@supports not (height: 100dvh) {
  :root { --app-vh: 100vh; }
}

/* ── App shell ── */
html {
  height: 100%;
  height: var(--app-vh-fallback);
  height: var(--app-vh);
}

body {
  height: 100%;
  height: var(--app-vh-fallback);
  height: var(--app-vh);
  overflow: hidden;
}

.cartoon-world {
  height: 100%;
  height: var(--app-vh-fallback);
  height: var(--app-vh);
  max-height: var(--app-vh);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.scene {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  max-height: var(--app-vh);
  min-height: unset;
  height: auto;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding:
    max(var(--pad-block), env(safe-area-inset-top))
    max(var(--pad-inline), env(safe-area-inset-right))
    max(var(--pad-block), env(safe-area-inset-bottom))
    max(var(--pad-inline), env(safe-area-inset-left));
}

.scene-scroll {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  max-width: 900px;
  margin-inline: auto;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: clamp(0.25rem, 0.8vh, 0.65rem);
  padding-bottom: 0.25rem;
  scrollbar-width: none;
}

.scene-scroll::-webkit-scrollbar {
  display: none;
}

@media (min-height: 740px) {
  .scene-scroll {
    justify-content: center;
  }
}

/* ── Hero (compact in play mode) ── */
.hero-text {
  margin-bottom: var(--hero-gap);
  flex-shrink: 0;
  width: 100%;
  max-width: 520px;
}

body.is-playing .hero-text {
  margin-bottom: clamp(0.15rem, 0.6vh, 0.4rem);
}

body.is-playing .hero-text .badge {
  display: none;
}

body.is-playing .hero-text h1 {
  font-size: var(--h1-play);
  line-height: 1.08;
}

body.layout-compact.is-playing .hero-text {
  display: none;
}

/* ── Stage — fluid height ── */
.aliens-stage {
  width: var(--ui-max-w);
  min-height: unset;
  height: var(--stage-h);
  max-height: 36dvh;
  padding: var(--stage-pad-y) clamp(12px, 3vw, 36px) clamp(12px, 2vw, 24px);
  flex-shrink: 0;
  overflow: visible;
}

body.layout-compact .aliens-stage {
  --stage-h: clamp(140px, 26dvh, 220px);
  padding-top: clamp(16px, 4vw, 28px);
}

/* ── Cards — responsive grid ── */
.cards {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: var(--cards-gap);
  margin-top: clamp(0.35rem, 1vh, 1rem);
  width: var(--ui-max-w);
  flex-shrink: 0;
}

.glass-card {
  min-width: 0;
  padding: clamp(0.55rem, 1.5vw, 1rem) clamp(0.45rem, 1.2vw, 0.9rem);
}

.card-icon {
  font-size: clamp(1.1rem, 3vw, 1.65rem);
  margin-bottom: 0.2rem;
}

.card-label {
  font-size: clamp(0.55rem, 1.8vw, 0.68rem);
}

.card-value {
  font-size: clamp(0.95rem, 2.8vw, 1.35rem);
}

.card-value-sm {
  font-size: clamp(0.78rem, 2.2vw, 1.05rem);
}

/* ── HUD & mission ── */
.game-hud,
.worlds-orbit,
.progress-track,
.mission-panel {
  width: var(--ui-max-w);
  flex-shrink: 0;
}

.progress-pill span:last-child,
#progress-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: min(46vw, 14rem);
  display: inline-block;
  vertical-align: bottom;
}

.mission-reward-hint {
  display: none;
}

@media (min-width: 480px) {
  .mission-reward-hint {
    display: block;
  }
}

.footer {
  flex-shrink: 0;
  width: var(--ui-max-w);
  margin-top: clamp(0.2rem, 0.6vh, 0.5rem);
}

body.is-playing .footer {
  display: none;
}

@media (min-height: 780px) {
  body.is-playing .footer {
    display: block;
  }
}

/* ── Menu overlay scroll ── */
.game-overlay {
  align-items: flex-start;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding:
    max(0.75rem, env(safe-area-inset-top))
    max(0.75rem, env(safe-area-inset-right))
    max(0.75rem, env(safe-area-inset-bottom))
    max(0.75rem, env(safe-area-inset-left));
}

.game-overlay .overlay-panel {
  margin: auto;
  width: min(92vw, 420px);
}

/* ── Width breakpoints ── */
@media (max-width: 560px) {
  .cards {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .hud-pill .label {
    display: none;
  }

  .hud-pill {
    padding: 0.35rem 0.65rem;
  }
}

@media (max-width: 380px) {
  .cards {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .subtitle {
    font-size: 0.85rem;
    line-height: 1.35;
  }
}

/* ── Height breakpoints (landscape phones, small laptops) ── */
@media (max-height: 700px) {
  :root {
    --stage-h: clamp(150px, 28dvh, 240px);
    --hero-gap: 0.3rem;
  }

  .hero-text .subtitle {
    display: none;
  }

  h1 {
    font-size: clamp(1.5rem, 5vw, 2.25rem);
  }

  .mission-panel {
    padding: 0.5rem 0.75rem;
  }

  .mission-icon {
    font-size: 1.2rem;
  }
}

@media (max-height: 580px) {
  :root {
    --stage-h: clamp(120px, 34dvh, 180px);
  }

  body.is-playing .worlds-orbit {
    display: none;
  }

  body.is-playing .progress-track {
    margin-top: 0.25rem;
  }

  .overlay-panel p {
    font-size: 0.85rem;
    margin-bottom: 0.85rem;
  }

  .leaderboard ol {
    max-height: 80px;
  }
}

@media (max-height: 480px) {
  body.is-playing .mission-foot {
    display: none;
  }

  .game-hud {
    gap: 0.35rem;
  }
}

/* Landscape mobile */
@media (max-height: 480px) and (orientation: landscape) {
  .scene-scroll {
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    align-content: flex-start;
    gap: 0.35rem;
  }

  body.is-playing .hero-text {
    display: none;
  }

  .aliens-stage {
    width: min(52vw, 400px);
    --stage-h: clamp(110px, 72dvh, 200px);
  }

  .cards {
    width: min(42vw, 280px);
    grid-template-columns: repeat(2, 1fr);
    margin-top: 0;
  }

  .game-hud,
  .mission-panel,
  .worlds-orbit,
  .progress-track {
    width: min(92vw, 520px);
  }
}

/* Large monitors — cap width, add breathing room */
@media (min-width: 1400px) and (min-height: 900px) {
  :root {
    --stage-h: 340px;
    --ui-max-w: 760px;
  }

  .scene-scroll {
    max-width: 960px;
  }
}
