/*
 * View isolation guard.
 *
 * Some portrait layout rules intentionally use display: ... !important to build
 * dedicated phone layouts. The active-view system still owns visibility: if a
 * game section has .hidden, it must never participate in layout.
 */
#gameShell > section.hidden {
  display: none !important;
}

/* Full-screen modes should not leak normal game chrome through them. */
body.event-active #battleView,
body.event-active #rewardView,
body.event-active #routeView,
body.event-active #restView,
body.event-active #shopView,
body.event-active #relicView,
body.event-active #resultView,
body.shop-active #battleView,
body.shop-active #rewardView,
body.shop-active #routeView,
body.shop-active #restView,
body.shop-active #relicView,
body.shop-active #eventView,
body.shop-active #resultView,
body.rest-active #battleView,
body.rest-active #rewardView,
body.rest-active #routeView,
body.rest-active #shopView,
body.rest-active #relicView,
body.rest-active #eventView,
body.rest-active #resultView {
  display: none !important;
}

@media (orientation: portrait) and (max-width: 700px) {
  /* One screen = one viewport. Prevent a visible view from pushing another
     screen below it if a future layout rule changes display behavior. */
  #battleView:not(.hidden),
  #rewardView:not(.hidden),
  #routeView:not(.hidden),
  #relicView:not(.hidden) {
    width: 100% !important;
    max-width: 100% !important;
  }

  /* Event/shop/rest already own the entire viewport in their component CSS. */
  body.event-active,
  body.shop-active,
  body.rest-active {
    overflow: hidden !important;
  }

  /* Portrait battle: cards own the full width; controls sit underneath. */
  #battleView:not(.hidden) .player-zone {
    min-height: 0;
    overflow: visible;
  }

  #battleView:not(.hidden) .hand-controls {
    flex: 1 1 auto;
    min-height: 0;
    display: flex !important;
    flex-direction: column !important;
    gap: 7px !important;
    align-items: stretch !important;
    margin: 0 !important;
    overflow: visible !important;
  }

  #battleView:not(.hidden) .hand,
  #battleView:not(.hidden) .hand.is-overflowing {
    order: 1;
    flex: 1 1 auto;
    width: 100% !important;
    min-width: 0 !important;
    min-height: 0 !important;
    display: flex !important;
    align-items: flex-end !important;
    justify-content: flex-start !important;
    gap: 14px !important;
    overflow-x: auto !important;
    overflow-y: visible !important;
    padding: 27px 14px 5px !important;
    scroll-padding-inline: 14px;
    scroll-snap-type: x proximity;
    overscroll-behavior-x: contain;
    touch-action: none !important;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }

  #battleView:not(.hidden) .hand.is-drag-scrolling {
    scroll-snap-type: none !important;
  }

  #battleView:not(.hidden) .hand::-webkit-scrollbar,
  #battleView:not(.hidden) .hand.is-overflowing::-webkit-scrollbar {
    display: none;
  }

  #battleView:not(.hidden) .hand .card,
  #battleView:not(.hidden) .hand.is-overflowing .card {
    flex: 0 0 136px !important;
    width: 136px !important;
    min-width: 136px !important;
    min-height: 0 !important;
    height: auto !important;
    max-height: 204px !important;
    aspect-ratio: 2 / 3;
    scroll-snap-align: center;
    touch-action: none !important;
  }

  #battleView:not(.hidden) .side-actions {
    order: 2;
    flex: 0 0 auto;
    width: 100%;
    min-width: 0;
    min-height: 0;
    display: flex !important;
    flex-direction: row !important;
    align-items: stretch !important;
    gap: 7px !important;
    margin: 0 !important;
    padding: 0 7px 4px !important;
  }

  #battleView:not(.hidden) .energy-panel {
    flex: 0 0 78px;
    min-width: 78px;
    min-height: 46px;
    display: grid;
    align-content: center;
    padding: 5px 6px !important;
    border-radius: 11px !important;
  }

  #battleView:not(.hidden) .energy-panel span {
    font-size: 7px !important;
  }

  #battleView:not(.hidden) .energy-panel strong {
    margin: 1px 0 0 !important;
    font-size: 16px !important;
  }

  #battleView:not(.hidden) .controls {
    flex: 1 1 auto;
    min-width: 0;
    min-height: 46px;
    display: grid !important;
    grid-template-columns: minmax(0, 1.35fr) minmax(0, 1fr) !important;
    grid-template-rows: 46px !important;
    gap: 7px !important;
  }

  #battleView:not(.hidden) .controls .primary-button,
  #battleView:not(.hidden) .controls .secondary-button {
    width: 100% !important;
    min-width: 0 !important;
    min-height: 46px !important;
    height: 46px !important;
    padding: 0 8px !important;
    border-radius: 11px !important;
    font-size: 11px !important;
    line-height: 1.1;
  }

  /* Keep the single enemy comfortably inside the right half of the screen. */
  #battleView:not(.hidden) .enemies-container:not([data-enemy-count="2"]):not([data-enemy-count="3"]) {
    left: auto !important;
    right: 5% !important;
    bottom: 0 !important;
    width: 42% !important;
    height: 58% !important;
    display: flex !important;
    align-items: flex-end !important;
    justify-content: center !important;
    overflow: visible !important;
    padding: 0 !important;
    transform: none !important;
  }

  #battleView:not(.hidden) .enemies-container:not([data-enemy-count="2"]):not([data-enemy-count="3"]) .fighter-enemy {
    position: relative !important;
    inset: auto !important;
    left: auto !important;
    right: auto !important;
    bottom: 0 !important;
    width: 72% !important;
    max-width: 180px !important;
    height: auto !important;
    margin: 0 !important;
    transform: none !important;
  }

  /* Multi-enemy formations also stay away from the extreme screen edge. */
  #battleView:not(.hidden) .enemies-container[data-enemy-count="2"],
  #battleView:not(.hidden) .enemies-container[data-enemy-count="3"] {
    left: 0 !important;
    right: 0 !important;
    width: 100% !important;
    overflow: visible !important;
  }

  #battleView:not(.hidden) .enemies-container[data-enemy-count="2"] .enemy-slot-1,
  #battleView:not(.hidden) .enemies-container[data-enemy-count="3"] .enemy-slot-1 {
    left: auto !important;
    right: 7% !important;
  }

  #battleView:not(.hidden) .enemies-container[data-enemy-count="2"] .enemy-slot-2,
  #battleView:not(.hidden) .enemies-container[data-enemy-count="3"] .enemy-slot-2 {
    left: 53% !important;
    right: auto !important;
  }

  #battleView:not(.hidden) .enemies-container[data-enemy-count="3"] .enemy-slot-3 {
    left: 29% !important;
    right: auto !important;
  }

  /* Shorter player HP HUD/bar for the narrow portrait battlefield. */
  #battleView:not(.hidden) .fighter-player .fighter-hud {
    left: 50% !important;
    width: 140px !important;
    min-width: 0 !important;
    max-width: 140px !important;
  }

  #battleView:not(.hidden) .fighter-player .fighter-health-row .fighter-hp-track {
    flex: 0 1 92px !important;
    width: 92px !important;
    max-width: 92px !important;
  }
}

@media (orientation: portrait) and (max-width: 390px) {
  #battleView:not(.hidden) .hand,
  #battleView:not(.hidden) .hand.is-overflowing {
    gap: 12px !important;
    padding-left: 12px !important;
    padding-right: 12px !important;
    scroll-padding-inline: 12px;
  }

  #battleView:not(.hidden) .hand .card,
  #battleView:not(.hidden) .hand.is-overflowing .card {
    flex-basis: 128px !important;
    width: 128px !important;
    min-width: 128px !important;
  }

  #battleView:not(.hidden) .energy-panel {
    flex-basis: 70px;
    min-width: 70px;
  }

  #battleView:not(.hidden) .fighter-player .fighter-hud {
    width: 132px !important;
    max-width: 132px !important;
  }

  #battleView:not(.hidden) .fighter-player .fighter-health-row .fighter-hp-track {
    flex-basis: 84px !important;
    width: 84px !important;
    max-width: 84px !important;
  }
}
