/* Ultimate Stars mobile release layer.
   Loaded last so phone and touch layouts have one clear responsive authority. */

@media (max-width: 900px) {
  html,
  body {
    width: 100%;
    max-width: 100%;
    overscroll-behavior-x: none;
  }

  body {
    min-height: calc(var(--us-vh, 1vh) * 100);
    overflow-x: hidden !important;
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }

  html,
  body,
  .app,
  .screen.active {
    overflow-y: auto !important;
    touch-action: pan-y pinch-zoom;
    -webkit-overflow-scrolling: touch;
  }

  .panel,
  .menu-card,
  .home-carousel-shell,
  .home-carousel-viewport,
  .shop-pack-button,
  .starter-pack-button,
  .game-card,
  .card-grid,
  #allCardsGrid,
  #myCardsGrid,
  #packshopScreen .shop-shell,
  #cardsScreen > .panel,
  #deckScreen .panel,
  #playScreen .panel {
    touch-action: pan-y pinch-zoom !important;
  }

  input,
  select,
  textarea,
  button,
  .btn {
    touch-action: manipulation;
  }

  .app {
    width: 100% !important;
    max-width: none !important;
    padding:
      max(8px, env(safe-area-inset-top))
      8px
      max(24px, env(safe-area-inset-bottom)) !important;
  }

  .screen,
  .screen.active,
  .panel,
  .modal-shell {
    min-width: 0;
  }

  .panel,
  .modal-shell,
  .hero-card,
  .menu-card,
  .slot {
    border-radius: 8px !important;
  }

  button,
  .btn,
  .input,
  .select {
    min-height: 44px;
  }

  .input,
  .select {
    width: 100%;
    min-width: 0;
    font-size: 16px;
  }

  button:hover:not(:disabled),
  .btn:hover:not(:disabled),
  .menu-card:hover:not(:disabled) {
    transform: none !important;
  }

  .topbar,
  .page-head {
    align-items: stretch !important;
    flex-direction: column !important;
    gap: 10px !important;
    margin-bottom: 12px !important;
  }

  .brand strong {
    font-size: 32px !important;
    line-height: 1 !important;
    letter-spacing: 0 !important;
  }

  .brand span {
    line-height: 1.35;
  }

  .topbar > .nav-actions {
    width: 100%;
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px !important;
  }

  .topbar > .nav-actions .btn {
    width: 100%;
    min-width: 0 !important;
    padding: 10px 8px !important;
    justify-content: center !important;
  }

  /* Public start page: keep the oversized desktop typography inside the phone. */
  #startScreen .start-page {
    width: 100% !important;
    max-width: 100% !important;
    gap: 16px !important;
    padding-bottom: 32px !important;
  }

  #startScreen .start-hero-card {
    width: 100% !important;
    min-width: 0 !important;
    min-height: 460px !important;
    padding: 22px !important;
    overflow: hidden !important;
  }

  #startScreen .start-main-title {
    max-width: 100% !important;
    font-size: 38px !important;
    line-height: 1 !important;
    letter-spacing: 0 !important;
    overflow-wrap: anywhere;
  }

  #startScreen .start-main-title .start-byline {
    max-width: 100%;
    font-size: 13px !important;
    line-height: 1.35 !important;
    overflow-wrap: anywhere;
  }

  #startScreen .start-mature-notice {
    width: 100% !important;
    max-width: 100% !important;
    padding: 14px !important;
  }

  #startScreen .start-info-grid {
    width: 100% !important;
    grid-template-columns: 1fr !important;
    gap: 10px !important;
  }

  #startScreen .start-info-card {
    width: 100% !important;
    min-width: 0 !important;
    padding: 16px !important;
    border-radius: 8px !important;
  }

  #startScreen .start-info-card strong,
  #startScreen .start-info-card span {
    max-width: 100%;
    overflow-wrap: anywhere;
  }

  /* Compact HUD with fixed-size artwork and resilient large values. */
  #economyHud,
  .economy-hud {
    width: calc(100% - 4px) !important;
    min-height: 0 !important;
    position: sticky !important;
    top: max(4px, env(safe-area-inset-top)) !important;
    margin: 0 auto 10px !important;
    padding: 5px !important;
    border-radius: 8px !important;
  }

  #hudRightGroup,
  .hud-right-group {
    width: 100% !important;
    min-width: 0 !important;
    min-height: 0 !important;
    margin: 0 !important;
    padding: 5px !important;
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) repeat(3, 44px);
    gap: 5px !important;
    border-radius: 8px !important;
  }

  #hudRightGroup .hud-trophy-pill,
  .hud-right-group .hud-trophy-pill,
  .hud-coin-pill {
    width: 100% !important;
    min-width: 0 !important;
    max-width: none !important;
    height: 44px !important;
    min-height: 44px !important;
    padding: 0 7px !important;
    gap: 5px !important;
    justify-content: flex-start !important;
    overflow: hidden !important;
    border-radius: 8px !important;
  }

  #hudRightGroup .hud-trophy-icon,
  .hud-right-group .hud-trophy-icon,
  .hud-coin-icon {
    width: 25px !important;
    height: 25px !important;
    flex: 0 0 25px !important;
  }

  #hudRightGroup .hud-trophy-img,
  .hud-right-group .hud-trophy-img,
  .hud-coin-icon img {
    width: 78% !important;
    height: 78% !important;
    flex: none !important;
    object-fit: contain !important;
  }

  #hudRightGroup .hud-trophy-value,
  .hud-right-group .hud-trophy-value,
  .hud-coin-value {
    min-width: 0 !important;
    overflow: hidden;
    color: inherit;
    font-size: 13px !important;
    font-variant-numeric: tabular-nums;
    letter-spacing: 0 !important;
    white-space: nowrap;
  }

  #hudRightGroup .hud-icon-btn,
  .hud-right-group .hud-icon-btn {
    width: 44px !important;
    min-width: 44px !important;
    height: 44px !important;
    min-height: 44px !important;
    border-radius: 8px !important;
  }

  /* Home and common content surfaces. */
  body #homeScreen .home-menu-stage {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    padding-inline: 0 !important;
    justify-items: stretch !important;
  }

  #homeScreen .home-menu-with-tasks,
  #homeScreen .home-menu-shell {
    width: 100% !important;
    max-width: none !important;
    min-width: 0 !important;
    margin-inline: 0 !important;
    justify-self: stretch !important;
    grid-template-columns: 1fr !important;
    gap: 14px !important;
  }

  #homeScreen .home-menu-grid {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    margin-inline: 0 !important;
    grid-template-columns: 1fr !important;
    gap: 10px !important;
  }

  #homeScreen .home-menu-with-tasks .menu-card,
  #homeScreen .home-menu-with-tasks .menu-card[data-nav="deck"],
  #homeScreen .home-menu-with-tasks .menu-card[data-nav="packshop"] {
    min-height: 150px !important;
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    margin-inline: 0 !important;
    padding: 18px !important;
  }

  #homeScreen .home-menu-with-tasks .menu-card strong {
    font-size: 34px !important;
    line-height: 1 !important;
    letter-spacing: 0 !important;
  }

  #homeScreen .home-menu-with-tasks .menu-card span {
    font-size: 14px !important;
  }

  .home-carousel-shell,
  .home-carousel-viewport {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    margin-inline: 0 !important;
    min-height: 160px !important;
    border-radius: 8px !important;
  }

  .home-carousel-item {
    padding: 18px !important;
  }

  .home-carousel-label {
    font-size: 38px !important;
    line-height: 1 !important;
    letter-spacing: 0 !important;
  }

  .home-carousel-nav {
    width: 44px !important;
    height: 44px !important;
    border-radius: 8px !important;
  }

  .home-carousel-dots {
    bottom: 8px !important;
    gap: 4px !important;
    min-height: 0 !important;
    padding: 4px 5px !important;
  }

  .home-carousel-dot {
    width: 7px !important;
    min-width: 7px !important;
    height: 7px !important;
    min-height: 7px !important;
    padding: 0 !important;
  }

  .home-carousel-dot[aria-pressed="true"] {
    width: 22px !important;
  }

  /* Cards collection: two readable columns and denser mobile pagination. */
  #cardsScreen > .panel {
    padding: 10px !important;
  }

  #cardsScreen .filters,
  .collection-shell .filters {
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px !important;
  }

  #cardsScreen .filters .input,
  .collection-shell .filters .input {
    grid-column: 1 / -1;
  }

  #allCardsGrid,
  #myCardsGrid {
    width: 100%;
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    column-gap: 8px !important;
    row-gap: 10px !important;
  }

  #allCardsGrid .game-card,
  #myCardsGrid .game-card {
    width: 100% !important;
    min-width: 0 !important;
    max-width: none !important;
  }

  #cardsScreen .cards-pagination {
    gap: 10px;
    margin-top: 12px;
    padding-top: 12px;
  }

  #cardsScreen .cards-pagination-controls {
    width: 100%;
    grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
    gap: 7px;
  }

  #cardsScreen .cards-pagination-controls .btn {
    padding: 10px 7px !important;
  }

  .card-collection-detail-shell,
  .evolution-point-allocation-shell {
    width: calc(100vw - 16px) !important;
    max-height: calc(var(--us-vh, 1vh) * 100 - 16px) !important;
    overflow-y: auto !important;
  }

  .card-collection-detail-card {
    width: min(300px, 82vw) !important;
  }

  .card-collection-detail-game-card {
    min-height: 0 !important;
    aspect-ratio: 3 / 4;
  }

  /* Deck management remains compact enough to scan all six slots. */
  #deckScreen .panel {
    padding: 10px !important;
  }

  #deckScreen .deck-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 8px !important;
    margin: 10px 0 !important;
  }

  #deckScreen .slot {
    width: 100% !important;
    min-width: 0 !important;
    min-height: 0 !important;
    aspect-ratio: 3 / 4;
    padding: 4px !important;
  }

  #deckScreen .slot .game-card {
    width: 100% !important;
    max-width: none !important;
  }

  #deckScreen .slot .remove-slot {
    width: 40px !important;
    height: 40px !important;
    top: 4px !important;
    right: 4px !important;
  }

  #deckScreen .deck-actions {
    width: 100%;
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px !important;
  }

  #deckScreen .deck-actions #openMyCardsBtn {
    grid-column: 1 / -1;
  }

  #deckScreen .deck-actions .btn {
    width: 100%;
    min-width: 0 !important;
    padding: 11px 8px !important;
  }

  /* Play setup and shop surfaces. */
  #playScreen #modeSelection.mode-grid,
  #duelSetup .duel-setup-layout {
    grid-template-columns: 1fr !important;
  }

  #duelSetup.duel-setup-panel {
    padding:
      max(8px, env(safe-area-inset-top))
      8px
      max(8px, env(safe-area-inset-bottom)) !important;
  }

  #duelSetup .duel-setup-layout {
    width: 100% !important;
    max-width: none !important;
    max-height: calc(var(--us-vh, 1vh) * 100 - 16px) !important;
    padding: 10px !important;
    gap: 10px !important;
    border-radius: 8px !important;
  }

  #duelSetup .duel-setup-copy,
  #duelSetup .duel-setup-preview {
    padding: 12px !important;
    border-radius: 8px !important;
  }

  #duelSetup .duel-setup-copy h2 {
    font-size: 36px !important;
    line-height: 1 !important;
    letter-spacing: 0 !important;
  }

  #duelSetup .duel-setup-actions {
    grid-template-columns: 1fr !important;
  }

  #duelSetupDeck.small-card-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
    gap: 6px !important;
  }

  #duelSetupDeck .game-card {
    max-width: none !important;
  }

  #packshopScreen .shop-shell {
    min-height: 0;
    padding: 12px !important;
  }

  #packshopScreen .shop-header {
    align-items: stretch;
    flex-direction: column;
    margin-bottom: 14px;
  }

  #packshopScreen .shop-header h2 {
    font-size: 38px !important;
    line-height: 1 !important;
    letter-spacing: 0 !important;
  }

  #packshopScreen .shop-stock-pill {
    width: 100%;
    white-space: normal;
    text-align: center;
  }

  #packshopScreen .shop-pack-grid {
    grid-template-columns: 1fr !important;
    gap: 12px !important;
  }

  #packshopScreen .shop-product-card,
  #packshopScreen .starter-pack-card {
    min-height: 0 !important;
    padding: 16px !important;
    border-radius: 8px !important;
  }

  #packshopScreen .shop-pack-button,
  #packshopScreen .starter-pack-button {
    width: min(230px, 72vw) !important;
    border-radius: 8px !important;
  }

  .claim-reward-card,
  .claim-reward-panel-head {
    align-items: stretch;
    flex-direction: column;
  }

  /* Battle: large horizontal card rail, no clipped first or last card. */
  body.battle-active .app {
    width: 100% !important;
    padding-left: 4px !important;
    padding-right: 4px !important;
  }

  body.battle-active #battleScreen,
  body.battle-active .battle-screen {
    width: 100% !important;
    min-width: 0 !important;
    height: auto !important;
    min-height: calc(var(--us-vh, 1vh) * 100 - 12px) !important;
    overflow: visible !important;
  }

  body.battle-active .battle-screen {
    display: grid !important;
    grid-template-rows: auto auto auto !important;
    gap: 8px !important;
  }

  body.battle-active .battle-top {
    min-width: 0;
    overflow: hidden;
  }

  body.battle-active .enemy-row {
    width: 100%;
    grid-template-columns: repeat(6, minmax(36px, 1fr)) !important;
    gap: 4px !important;
  }

  body.battle-active .enemy-card-back {
    width: 100% !important;
    max-width: 58px !important;
    min-width: 0 !important;
  }

  body.battle-active .battle-arena {
    width: 100% !important;
    min-width: 0 !important;
    height: auto !important;
    min-height: 0 !important;
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    grid-template-areas:
      "score score"
      "player cpu" !important;
    align-items: start !important;
    justify-items: stretch !important;
    gap: 10px 8px !important;
    padding: 8px !important;
    border-radius: 8px !important;
    overflow: hidden !important;
  }

  body.battle-active #battleScoreCenter {
    grid-area: score !important;
  }

  body.battle-active #playerDropZone {
    grid-area: player !important;
  }

  body.battle-active #cpuDropZone {
    grid-area: cpu !important;
  }

  body.battle-active .drop-zone {
    width: 100% !important;
    min-width: 0 !important;
    max-width: none !important;
    height: 190px !important;
    min-height: 190px !important;
    max-height: 190px !important;
    padding: 6px !important;
    border-radius: 8px !important;
  }

  body.battle-active .drop-zone::before {
    top: 7px !important;
    padding: 5px 8px !important;
    font-size: 10px !important;
    line-height: 1 !important;
    letter-spacing: .08em !important;
  }

  body.battle-active .drop-zone .game-card {
    width: 100% !important;
    max-width: 118px !important;
  }

  body.battle-active .drop-empty {
    max-width: 100% !important;
    padding: 5px !important;
    font-size: 10px !important;
  }

  body.battle-active .battle-match-score {
    width: min(230px, 76vw) !important;
    min-width: 0 !important;
    max-width: 230px !important;
    height: auto !important;
    min-height: 178px !important;
    padding: 12px 14px !important;
    gap: 6px !important;
    justify-self: center !important;
    align-self: start !important;
    align-content: center !important;
    justify-items: center !important;
    border-radius: 8px !important;
    overflow: hidden !important;
  }

  body.battle-active .battle-match-round-title {
    max-width: 100% !important;
    font-size: 22px !important;
    line-height: 1 !important;
    letter-spacing: 0 !important;
    text-align: center !important;
  }

  body.battle-active .battle-match-round-sub,
  body.battle-active .battle-match-score span {
    font-size: 10px !important;
  }

  body.battle-active .battle-match-special {
    max-width: 100% !important;
    padding: 5px 8px !important;
    font-size: 10px !important;
    line-height: 1.15 !important;
  }

  body.battle-active .battle-match-score > div:last-child > strong {
    font-size: 44px !important;
    line-height: .9 !important;
  }

  body.battle-active .battle-match-score .action-timer {
    width: auto !important;
    max-width: 100% !important;
    padding: 7px 10px !important;
    font-size: 11px !important;
  }

  body.battle-active .battle-match-score .action-timer strong {
    font-size: 34px !important;
  }

  body.battle-active .battle-bottom.panel {
    width: 100% !important;
    height: auto !important;
    min-height: 244px !important;
    max-height: none !important;
    padding: 8px 0 max(10px, env(safe-area-inset-bottom)) !important;
    border-radius: 8px !important;
    overflow: hidden !important;
  }

  body.battle-active #playerBattleDeck,
  body.battle-active .player-row {
    width: 100% !important;
    height: auto !important;
    min-height: 224px;
    display: grid !important;
    grid-template-columns: none !important;
    grid-auto-flow: column;
    grid-auto-columns: minmax(150px, 42vw);
    justify-content: start !important;
    align-items: end !important;
    align-content: end !important;
    gap: 10px !important;
    padding: 4px 14px 10px !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    scroll-padding-inline: 14px;
    scroll-snap-type: x proximity;
    scrollbar-width: thin;
    scrollbar-color: rgba(103,232,249,.56) rgba(255,255,255,.08);
    touch-action: pan-x !important;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
  }

  body.battle-active #playerBattleDeck *,
  body.battle-active .player-row * {
    touch-action: pan-x !important;
  }

  body.battle-active #playerBattleDeck::-webkit-scrollbar {
    height: 6px;
  }

  body.battle-active #playerBattleDeck::-webkit-scrollbar-track {
    background: rgba(255,255,255,.08);
  }

  body.battle-active #playerBattleDeck::-webkit-scrollbar-thumb {
    border-radius: 999px;
    background: rgba(103,232,249,.56);
  }

  body.battle-active .battle-hand-card {
    width: 100% !important;
    min-width: 0 !important;
    scroll-snap-align: center;
  }

  body.battle-active .battle-hand-card .game-card,
  body.battle-active .battle-bottom .game-card {
    width: 100% !important;
    max-width: none !important;
    max-height: none !important;
    transform: none !important;
  }

  body.battle-active .battle-card-type-chip {
    width: min(100%, 150px);
    min-height: 24px;
    padding: 5px 7px !important;
    border-radius: 8px !important;
    font-size: 9px !important;
    letter-spacing: 0 !important;
  }

  body.battle-active .surrender-btn {
    min-height: 44px;
  }

  /* Every overlay stays inside the visible phone viewport. */
  .home-news-window,
  .profile-window,
  .message-popover,
  #us26QuestModal .modal-shell,
  #myCardsModal .modal-shell,
  #packOpeningView,
  #us23SurrenderOverlay > *,
  #ultimateStarsAuthModal .modal-shell,
  #ultimateStarsDisplayNameModal .modal-shell {
    max-width: calc(100vw - 16px) !important;
    max-height: calc(var(--us-vh, 1vh) * 100 - 16px) !important;
    border-radius: 8px !important;
    overflow-y: auto !important;
  }
}

@media (min-width: 701px) and (max-width: 900px) {
  #allCardsGrid,
  #myCardsGrid,
  #deckScreen .deck-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
  }

  #packshopScreen .shop-pack-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }

  body.battle-active #playerBattleDeck,
  body.battle-active .player-row {
    grid-auto-columns: 180px;
  }
}

@media (max-width: 360px) {
  #hudRightGroup,
  .hud-right-group {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) repeat(3, 40px);
    gap: 4px !important;
    padding: 4px !important;
  }

  #hudRightGroup .hud-icon-btn,
  .hud-right-group .hud-icon-btn {
    width: 40px !important;
    min-width: 40px !important;
    height: 42px !important;
    min-height: 42px !important;
  }

  #hudRightGroup .hud-trophy-pill,
  .hud-right-group .hud-trophy-pill,
  .hud-coin-pill {
    padding: 0 5px !important;
  }

  body.battle-active #playerBattleDeck,
  body.battle-active .player-row {
    grid-auto-columns: minmax(144px, 44vw);
    padding-inline: 10px !important;
    scroll-padding-inline: 10px;
  }
}

@media (orientation: landscape) and (max-height: 540px) and (pointer: coarse) {
  #economyHud,
  .economy-hud {
    position: relative !important;
    top: 0 !important;
  }

  body.battle-active .battle-screen {
    min-height: 0 !important;
  }

  body.battle-active .battle-arena {
    min-height: 170px !important;
  }

  body.battle-active .drop-zone {
    height: 158px !important;
    min-height: 158px !important;
  }

  body.battle-active .battle-bottom.panel {
    min-height: 218px !important;
  }

  body.battle-active #playerBattleDeck,
  body.battle-active .player-row {
    min-height: 198px;
    grid-auto-columns: 142px;
  }
}
