/* World-class UI/UX Phase 10 — mobile-first production pass.
   This layer loads last so production viewport fixes can safely normalize
   earlier phase-specific mobile rules without changing gameplay. */

@media (max-width: 720px) {
  html,
  body {
    width: 100%;
    min-width: 0;
  }

  body {
    min-height: var(--app-height);
    overflow-y: auto;
    overscroll-behavior-y: contain;
  }

  .app-shell {
    width: 100%;
    min-height: var(--app-height);
    padding-top: max(6px, env(safe-area-inset-top));
    padding-right: max(6px, env(safe-area-inset-right));
    padding-bottom: max(6px, env(safe-area-inset-bottom));
    padding-left: max(6px, env(safe-area-inset-left));
    gap: 6px;
  }

  .topbar {
    min-height: 44px;
    gap: 8px;
    padding-inline: 2px;
  }

  .brand-block h1 {
    font-size: clamp(1.2rem, 5.4vw, 1.5rem);
  }

  .game-frame {
    min-height: 0;
    grid-template-rows: auto auto auto;
    overflow: visible;
    border-radius: 18px;
  }

  .hud {
    position: sticky;
    top: max(0px, env(safe-area-inset-top));
    z-index: 25;
    gap: 5px;
    padding: 6px;
    border-radius: 18px 18px 0 0;
    backdrop-filter: blur(12px);
  }

  .hud-item {
    min-height: 44px;
    padding: 5px 6px;
    border-radius: 9px;
  }

  .hud-label {
    font-size: 0.42rem;
    letter-spacing: 0.08em;
  }

  .hud-item strong {
    font-size: clamp(0.82rem, 4vw, 1rem);
  }

  .battlefield {
    min-height: 0;
    overflow: visible;
    align-content: start;
    gap: 8px;
    padding: 8px 7px 10px;
  }

  .combat-panel .wave-progress {
    margin-bottom: 6px;
  }

  .combat-stage {
    height: clamp(190px, 31vh, 270px);
    height: clamp(190px, 31svh, 270px);
  }

  .board-preview {
    padding: 7px;
    gap: 5px;
  }

  .board-preview__slot {
    min-height: 56px;
    aspect-ratio: 1.32;
  }

  /* Keep the draggable defender itself large enough to acquire reliably. */
  .board-unit {
    width: min(78%, 72px);
    min-width: 44px;
    min-height: 44px;
  }

  .board-recycle {
    width: 36px;
    height: 36px;
    top: 3px;
    left: 3px;
  }

  .control-dock {
    position: sticky;
    bottom: 0;
    z-index: 30;
    gap: 5px;
    padding: 7px 8px max(8px, env(safe-area-inset-bottom));
    border-radius: 0 0 18px 18px;
    background: rgba(3, 10, 16, 0.96);
    backdrop-filter: blur(14px);
    box-shadow: 0 -12px 28px rgba(0, 0, 0, 0.26);
  }

  .environment-status {
    min-height: 0;
    margin: 0;
  }

  .environment-status__item--deploy {
    padding: 2px 7px;
  }

  .economy-controls {
    grid-template-columns: minmax(0, 0.95fr) minmax(0, 1.45fr) minmax(64px, 0.6fr);
    gap: 5px;
  }

  .deploy-type-selector {
    gap: 4px;
  }

  .deploy-type {
    min-height: 42px;
    font-size: 0.56rem;
  }

  .deploy-action,
  .primary-action,
  .pause-action {
    min-height: 48px;
    padding-inline: 7px;
  }

  .deploy-action,
  .primary-action {
    font-size: 0.64rem;
  }

  .pause-action {
    font-size: 0.6rem;
    letter-spacing: 0.05em;
  }

  button:not(.board-unit) {
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
  }

  .perk-overlay,
  .pause-overlay,
  .settings-overlay,
  .victory-overlay {
    overflow-y: auto;
    overscroll-behavior: contain;
    padding-top: max(8px, env(safe-area-inset-top));
    padding-right: max(8px, env(safe-area-inset-right));
    padding-bottom: max(8px, env(safe-area-inset-bottom));
    padding-left: max(8px, env(safe-area-inset-left));
  }

  .perk-panel,
  .pause-panel,
  .settings-panel,
  .victory-panel {
    max-height: calc(var(--app-height) - 20px);
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }

  .pause-panel,
  .settings-panel,
  .victory-panel {
    width: min(100%, 430px);
  }

  .onboarding-coach {
    width: min(calc(100vw - 16px), 410px);
    max-height: min(42vh, 240px);
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .onboarding-coach__action,
  .how-to-play-action {
    min-height: 44px;
  }
}

/* 412–430px portrait phones: preserve breathing room without wasting width. */
@media (min-width: 400px) and (max-width: 430px) and (max-height: 980px) {
  .battlefield {
    padding-inline: 9px;
  }

  .combat-stage {
    height: clamp(220px, 31svh, 285px);
  }

  .board-preview__slot {
    min-height: 62px;
  }
}

/* 360–390px portrait phones: reduce chrome before reducing interaction targets. */
@media (max-width: 390px) {
  .app-shell {
    padding-left: max(5px, env(safe-area-inset-left));
    padding-right: max(5px, env(safe-area-inset-right));
  }

  .topbar {
    min-height: 42px;
  }

  .hud {
    grid-template-columns: 0.96fr 1.08fr 0.96fr;
    gap: 4px;
    padding: 5px;
  }

  .hud-item {
    min-height: 42px;
    padding-inline: 5px;
  }

  .battlefield {
    padding-inline: 5px;
  }

  .combat-stage {
    height: clamp(180px, 29svh, 235px);
  }

  .board-preview__slot {
    min-height: 54px;
    aspect-ratio: 1.38;
  }

  .control-dock {
    padding-inline: 6px;
  }

  .economy-controls {
    grid-template-columns: minmax(0, 0.9fr) minmax(0, 1.5fr) minmax(60px, 0.58fr);
    gap: 4px;
  }

  .deploy-type-selector {
    gap: 3px;
  }

  .deploy-action,
  .primary-action,
  .pause-action {
    padding-inline: 5px;
    letter-spacing: 0.04em;
  }

  .perk-panel {
    padding: 13px 10px;
  }

  .settings-panel,
  .pause-panel,
  .victory-panel {
    padding: 18px 14px;
  }
}

/* 320–359px phones: keep actions and drag surfaces usable, compress labels instead. */
@media (max-width: 359px) {
  .brand-kicker {
    display: none;
  }

  .brand-block h1 {
    font-size: 1.18rem;
  }

  .hud-label {
    font-size: 0.37rem;
    letter-spacing: 0.045em;
  }

  .hud-item strong {
    font-size: 0.78rem;
  }

  .combat-stage {
    height: clamp(172px, 28svh, 218px);
  }

  .board-preview {
    padding: 5px;
    gap: 4px;
  }

  .board-preview__slot {
    min-height: 50px;
  }

  .board-preview__slot::after,
  .board-preview__slot:nth-child(-n + 3)::before {
    font-size: 0.36rem;
  }

  .deploy-type {
    font-size: 0.5rem;
  }

  .deploy-action,
  .primary-action,
  .pause-action {
    min-height: 46px;
    font-size: 0.56rem;
  }

  .primary-action span,
  .deploy-action span {
    font-size: 0.48rem;
  }

  .environment-status {
    display: none;
  }

  .setting-row {
    grid-template-columns: minmax(0, 1fr);
    gap: 7px;
  }

  .setting-toggle,
  .setting-status,
  .how-to-play-action {
    min-width: 0;
    width: 100%;
    text-align: center;
  }
}

/* Short portrait screens including 320×568 and 360×640. Scroll is intentional;
   combat and drag targets stay usable instead of being scaled below touch comfort. */
@media (max-width: 720px) and (max-height: 680px) {
  .app-shell {
    gap: 4px;
  }

  .topbar {
    min-height: 38px;
  }

  .hud {
    padding: 4px 5px;
  }

  .hud-item {
    min-height: 40px;
    padding-block: 4px;
  }

  .battlefield {
    gap: 6px;
    padding-top: 6px;
    padding-bottom: 7px;
  }

  .combat-panel .unit-roster,
  .combat-panel .perk-summary {
    display: none;
  }

  .combat-panel .wave-progress {
    padding: 6px 7px;
    margin-bottom: 5px;
  }

  .wave-progress__meta {
    margin-bottom: 4px;
  }

  .combat-stage {
    height: clamp(158px, 26svh, 205px);
  }

  .board-preview__slot {
    min-height: 48px;
    aspect-ratio: 1.46;
  }

  .control-dock {
    gap: 4px;
    padding-top: 5px;
  }

  .environment-status {
    display: none;
  }

  .deploy-type {
    min-height: 40px;
  }

  .deploy-action,
  .primary-action,
  .pause-action {
    min-height: 46px;
  }

  .context-hint {
    padding: 6px 8px;
    font-size: 0.6rem;
    line-height: 1.3;
  }

  .onboarding-coach {
    bottom: max(102px, calc(env(safe-area-inset-bottom) + 90px));
    max-height: 180px;
    padding: 9px 10px;
  }

  .onboarding-coach p {
    line-height: 1.28;
  }

  .perk-panel,
  .pause-panel,
  .settings-panel,
  .victory-panel {
    max-height: calc(var(--app-height) - 12px);
  }

  .perk-panel > p {
    margin-bottom: 8px;
  }

  .settings-list {
    gap: 7px;
    margin-block: 12px;
  }

  .setting-row {
    min-height: 50px;
    padding: 8px 9px;
  }
}

@media (max-width: 720px) and (max-height: 600px) {
  .app-shell:not([data-onboarding-active="true"]) .context-hint {
    display: none !important;
  }

  .combat-stage {
    height: clamp(148px, 25svh, 180px);
  }

  .board-preview__slot {
    min-height: 46px;
  }

  .perk-panel__eyebrow,
  .perk-panel > p,
  .settings-panel > p {
    display: none;
  }
}

/* Graceful landscape compatibility while portrait remains the primary layout. */
@media (orientation: landscape) and (max-height: 520px) and (max-width: 932px) {
  .app-shell {
    padding-top: max(4px, env(safe-area-inset-top));
    padding-right: max(6px, env(safe-area-inset-right));
    padding-bottom: max(4px, env(safe-area-inset-bottom));
    padding-left: max(6px, env(safe-area-inset-left));
  }

  .topbar {
    min-height: 34px;
  }

  .brand-kicker {
    display: none;
  }

  .brand-block h1 {
    font-size: 1.1rem;
  }

  .game-frame {
    min-height: 0;
    grid-template-rows: auto auto auto;
  }

  .battlefield {
    grid-template-columns: minmax(0, 1.08fr) minmax(260px, 0.92fr);
    grid-template-rows: auto;
    align-items: start;
    gap: 7px;
  }

  .combat-panel,
  .board-preview {
    width: 100%;
  }

  .combat-panel .unit-roster,
  .combat-panel .perk-summary,
  .combat-panel .context-hint {
    display: none;
  }

  .combat-stage {
    height: clamp(190px, 58svh, 255px);
  }

  .board-preview__slot {
    min-height: 44px;
    aspect-ratio: 1.5;
  }

  .control-dock {
    position: sticky;
    bottom: 0;
  }
}

@media (prefers-reduced-motion: reduce) and (max-width: 720px) {
  .control-dock,
  .hud {
    backdrop-filter: none;
  }
}
