/* Final mobile menu safety layer. Loaded last so every menu stays readable. */
@media (max-width: 0px) {
  :root {
    --mobile-menu-gap: clamp(5px, 1.2vh, 8px);
    --mobile-title-height: clamp(54px, 9vh, 64px);
  }

  html {
    font-size: clamp(11px, 1.7vh, 14px) !important;
  }

  body:has(#windowBackdrop:not(.hidden)) {
    overflow: hidden !important;
  }

  #windowBackdrop:not(.hidden) {
    width: 100vw !important;
    height: var(--app-height, 100dvh) !important;
    position: fixed !important;
    inset: 0 !important;
    z-index: 100 !important;
    padding:
      env(safe-area-inset-top)
      env(safe-area-inset-right)
      env(safe-area-inset-bottom)
      env(safe-area-inset-left) !important;
    place-items: stretch !important;
    overflow: hidden !important;
    background: #030708ee !important;
  }

  .game-window,
  .game-window.shop-window,
  .game-window.profile-window,
  .game-window.storage-window,
  .game-window.research-window,
  .game-window.power-window,
  .game-window.match-window,
  .game-window.transformer-window {
    box-sizing: border-box !important;
    width: 100% !important;
    height: 100% !important;
    min-width: 0 !important;
    min-height: 0 !important;
    max-width: none !important;
    max-height: none !important;
    aspect-ratio: auto !important;
    border-radius: 0 !important;
    overflow: hidden !important;
    box-shadow: none !important;
  }

  .game-window *,
  .game-window *::before,
  .game-window *::after {
    box-sizing: border-box;
    min-width: 0;
    max-inline-size: 100%;
  }

  .game-window .window-titlebar {
    box-sizing: border-box !important;
    width: 100% !important;
    height: var(--mobile-title-height) !important;
    min-height: var(--mobile-title-height) !important;
    padding: 7px 9px !important;
    gap: 8px !important;
  }

  .game-window .window-titlebar::before {
    width: 42px !important;
    height: 42px !important;
    margin-right: 5px !important;
    flex: 0 0 42px !important;
    font-size: 1.35rem !important;
  }

  .game-window .window-titlebar > div {
    min-width: 0 !important;
    overflow: hidden !important;
  }

  .game-window .window-titlebar h2 {
    margin: 0 !important;
    overflow: hidden !important;
    font-size: 1.12rem !important;
    line-height: 1.05 !important;
    text-overflow: ellipsis !important;
    white-space: nowrap !important;
  }

  .game-window .panel-kicker {
    overflow: hidden !important;
    font-size: 0.62rem !important;
    text-overflow: ellipsis !important;
    white-space: nowrap !important;
  }

  .game-window .close-button {
    width: 44px !important;
    height: 44px !important;
    min-width: 44px !important;
    min-height: 44px !important;
    flex: 0 0 44px !important;
    padding: 0 !important;
    font-size: 1.15rem !important;
  }

  .game-window .window-content,
  .compact-1280 .game-window .window-content {
    box-sizing: border-box !important;
    width: 100% !important;
    height: calc(100% - var(--mobile-title-height)) !important;
    min-height: 0 !important;
    max-height: none !important;
    padding: var(--mobile-menu-gap) !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    overscroll-behavior: contain !important;
    scrollbar-gutter: stable;
    -webkit-overflow-scrolling: touch;
  }

  .game-window button,
  .game-window input,
  .game-window select {
    max-width: 100% !important;
    min-height: 44px;
    font-size: max(0.78rem, 12px);
    touch-action: manipulation;
  }

  .game-window button:not(.match-tile),
  .game-window input,
  .game-window select {
    min-height: 44px !important;
  }

  .game-window img {
    max-width: 100%;
  }

  .game-window h1,
  .game-window h2,
  .game-window h3,
  .game-window h4,
  .game-window p,
  .game-window span,
  .game-window strong,
  .game-window small,
  .game-window b,
  .game-window label,
  .game-window button {
    overflow-wrap: anywhere;
  }

  .cards-grid,
  .profile-layout {
    height: auto !important;
    grid-template-columns: 1fr !important;
    gap: 9px !important;
  }

  .panel-card,
  .form-card {
    width: 100% !important;
    padding: 11px !important;
  }

  .item-row,
  .leader-entry {
    align-items: flex-start !important;
    font-size: 0.82rem !important;
  }

  /* Market */
  .shop-window .window-content {
    height: calc(100% - var(--mobile-title-height)) !important;
  }

  .market-sheet,
  .market-layout,
  .market-command,
  .market-command:not(.market-command-market),
  .market-tab-page {
    width: 100% !important;
    height: auto !important;
    min-height: 0 !important;
    overflow: visible !important;
  }

  .market-layout {
    display: flex !important;
    flex-direction: column !important;
    gap: 9px !important;
  }

  .market-navigation {
    position: sticky;
    top: -8px;
    z-index: 8;
    padding: 7px !important;
    display: flex !important;
    flex-direction: row !important;
    gap: 6px !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    overscroll-behavior-x: contain;
    scrollbar-width: thin;
  }

  .market-navigation > button {
    min-width: 142px !important;
    min-height: 44px !important;
    flex: 0 0 142px !important;
    padding: 6px 9px !important;
    font-size: 0.72rem !important;
    white-space: normal !important;
  }

  .friend-offer-compact {
    display: none !important;
  }

  .market-command,
  .market-top,
  .market-bottom {
    display: flex !important;
    flex-direction: column !important;
    gap: 9px !important;
  }

  .market-upgrades,
  .market-banner,
  .market-purchase,
  .market-selling,
  .market-daily,
  .market-prices,
  .market-tab-section,
  .market-order-card,
  .market-offer-grid,
  .market-favorite-grid {
    width: 100% !important;
    height: auto !important;
    min-height: 0 !important;
    padding: 9px !important;
    overflow: visible !important;
  }

  .market-banner,
  .friend-offer-showcase {
    grid-template-columns: minmax(110px, 0.75fr) minmax(0, 1.25fr) !important;
  }

  .machine-part-scroll {
    overflow-x: auto !important;
    overflow-y: hidden !important;
    overscroll-behavior-x: contain;
  }

  .market-products,
  .market-selling > div,
  .market-command-trade .market-selling > div,
  .market-offer-grid,
  .market-favorite-grid {
    display: grid !important;
    grid-template-columns: repeat(auto-fit, minmax(142px, 1fr)) !important;
    gap: 8px !important;
  }

  .market-product,
  .market-sale-item,
  .machine-part-card {
    min-width: 0 !important;
    width: 100% !important;
    height: auto !important;
  }

  .market-tab-page > header {
    align-items: stretch !important;
    flex-direction: column !important;
    gap: 8px !important;
  }

  /* Profile */
  .profile-sheet,
  .profile-layout-v2,
  .profile-command,
  .profile-lower {
    width: 100% !important;
    height: auto !important;
    min-height: 0 !important;
    overflow: visible !important;
  }

  .profile-layout-v2 {
    display: flex !important;
    flex-direction: column !important;
    gap: 9px !important;
  }

  .profile-navigation {
    position: sticky;
    top: -8px;
    z-index: 8;
    padding: 7px !important;
    display: flex !important;
    flex-direction: row !important;
    gap: 6px !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    overscroll-behavior-x: contain;
  }

  .profile-navigation > button,
  .profile-navigation > span {
    min-width: 132px !important;
    min-height: 44px !important;
    flex: 0 0 132px !important;
    padding: 6px 9px !important;
    font-size: 0.72rem !important;
  }

  .profile-membership,
  .profile-controls {
    min-width: 230px !important;
    flex: 0 0 230px !important;
  }

  .profile-command {
    display: flex !important;
    flex-direction: column !important;
    gap: 9px !important;
  }

  .profile-identity,
  .profile-facts,
  .profile-statistics > div,
  .profile-lower,
  .profile-collection > div {
    height: auto !important;
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 8px !important;
  }

  .profile-identity {
    grid-template-columns: minmax(90px, 0.7fr) minmax(0, 1.3fr) !important;
  }

  .profile-league {
    grid-column: 1 / -1;
  }

  .profile-collection,
  .profile-community,
  .profile-statistics,
  .profile-facts,
  .profile-identity {
    min-height: 0 !important;
    padding: 9px !important;
    overflow: visible !important;
  }

  /* Depot */
  .storage-window .window-titlebar {
    display: none !important;
  }

  .storage-window .window-content {
    height: 100% !important;
    padding: 7px !important;
  }

  .reference-depot.storehub-screen,
  .reference-depot .depot-ref-layout,
  .reference-depot .depot-ref-left,
  .reference-depot .depot-ref-side {
    width: 100% !important;
    height: auto !important;
    min-height: 0 !important;
    display: flex !important;
    flex-direction: column !important;
    gap: 8px !important;
    overflow: visible !important;
  }

  .reference-depot .depot-ref-header,
  .reference-depot .depot-terminal,
  .reference-depot .depot-delivery-body,
  .reference-depot .depot-ref-footer {
    grid-template-columns: 1fr !important;
    gap: 8px !important;
  }

  .reference-depot .depot-ref-metrics {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 7px !important;
  }

  .reference-depot .depot-ref-metrics .storehub-capacity {
    grid-column: 1 / -1;
  }

  .reference-depot .depot-truck-arrivals,
  .reference-depot .depot-terminal-actions {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }

  .reference-depot .input-cards,
  .reference-depot .product-cards {
    grid-template-columns: repeat(auto-fit, minmax(138px, 1fr)) !important;
    gap: 7px !important;
  }

  /* Research */
  .research-window .window-titlebar {
    display: none !important;
  }

  .research-window .window-content {
    height: 100% !important;
    padding: 0 !important;
  }

  .refresearch-screen {
    width: 100% !important;
    height: auto !important;
    min-height: 100% !important;
    padding: 7px !important;
    display: flex !important;
    flex-direction: column !important;
    gap: 8px !important;
    overflow: visible !important;
  }

  .refresearch-header,
  .refresearch-body,
  .refresearch-left,
  .refresearch-core,
  .refresearch-right {
    width: 100% !important;
    height: auto !important;
    min-height: 0 !important;
    display: flex !important;
    flex-direction: column !important;
    gap: 8px !important;
    overflow: visible !important;
  }

  .refresearch-header-actions {
    display: grid !important;
    grid-template-columns: repeat(4, minmax(44px, 1fr)) !important;
  }

  .refresearch-resources {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }

  .refresearch-hero {
    grid-template-columns: 1fr !important;
  }

  .refresearch-hero-art {
    min-height: 190px !important;
  }

  .refresearch-active > div {
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)) !important;
  }

  .refresearch-current > div {
    grid-template-columns: repeat(6, minmax(132px, 1fr)) !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
  }

  .refresearch-tabs {
    position: sticky !important;
    bottom: -7px;
    z-index: 10;
    width: 100% !important;
    display: flex !important;
    gap: 6px !important;
    padding: 7px 0 !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    background: #030708ee !important;
  }

  .refresearch-tabs button {
    min-width: 108px !important;
    min-height: 44px !important;
    flex: 0 0 108px !important;
  }

  /* Power */
  .power-window .window-content {
    height: 100% !important;
    padding: 0 !important;
    overflow-y: auto !important;
  }

  .refpower-screen {
    width: 100% !important;
    height: auto !important;
    min-height: 100% !important;
    padding: 7px !important;
    display: flex !important;
    flex-direction: column !important;
    gap: 8px !important;
    overflow: visible !important;
  }

  .refpower-header {
    position: sticky !important;
    top: -7px;
    z-index: 10;
    min-height: 62px !important;
    grid-template-columns: minmax(0, 1fr) 44px !important;
    background: linear-gradient(180deg, #172327, #0a1113) !important;
  }

  .refpower-rail {
    display: none !important;
  }

  .refpower-body,
  .refpower-overview,
  .refpower-mid,
  .refpower-right {
    width: 100% !important;
    height: auto !important;
    min-height: 0 !important;
    display: flex !important;
    flex-direction: column !important;
    gap: 8px !important;
    overflow: visible !important;
  }

  .refpower-frame {
    width: 100% !important;
    height: auto !important;
    min-height: 0 !important;
    overflow: visible !important;
  }

  .refpower-plant {
    min-height: 220px !important;
  }

  .refpower-generator-row {
    min-height: 112px !important;
    grid-template-columns: 34px 74px minmax(0, 1fr) !important;
  }

  .refpower-generator-row button {
    grid-column: 1 / -1 !important;
    width: 100% !important;
  }

  /* Transformer */
  .transformer-window .window-content {
    position: static !important;
    inset: auto !important;
    height: 100% !important;
    padding: 0 !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    background: #101a22 !important;
  }

  .transformer-sheet {
    width: 100% !important;
    height: auto !important;
    min-height: 100% !important;
    padding: 68px 8px 8px !important;
    background:
      radial-gradient(circle at 50% 0%, rgb(58 131 188 / 30%), transparent 30%),
      linear-gradient(#1b2c39, #0c151c) !important;
  }

  .transformer-title {
    position: absolute !important;
    left: 12px !important;
    right: 62px !important;
    top: 20px !important;
    font-size: 1.25rem !important;
  }

  .transformer-close-hit {
    width: 44px !important;
    height: 44px !important;
    position: absolute !important;
    top: 9px !important;
    right: 9px !important;
    border: 1px solid #b05b42 !important;
    border-radius: 9px !important;
    color: #ffe0d3 !important;
    background: linear-gradient(#a9472e, #5d1b12) !important;
  }

  .transformer-close-hit::before {
    content: "X";
    display: grid;
    place-items: center;
    color: #ffe0d3;
    font-weight: 900;
  }

  .transformer-columns {
    width: 100% !important;
    height: auto !important;
    position: static !important;
    display: flex !important;
    flex-direction: column !important;
    gap: 8px !important;
  }

  .transformer-side,
  .transformer-center {
    width: 100% !important;
    height: auto !important;
    min-height: 0 !important;
    padding: 9px !important;
  }

  .transformer-side h3 {
    height: auto !important;
    min-height: 38px !important;
    margin: -9px -9px 8px !important;
  }

  .input-materials {
    display: grid !important;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)) !important;
    gap: 7px !important;
  }

  .material-row,
  .output-product {
    min-height: 94px !important;
  }

  .main-machine-slot,
  .factory-slot {
    min-height: 210px !important;
  }

  .center-lower,
  .machine-info-grid {
    grid-template-columns: 1fr !important;
  }

  .machine-center-actions {
    display: grid !important;
    grid-template-columns: 1fr !important;
  }

  .machine-center-actions .upgrade-machine,
  .run-machine {
    width: 100% !important;
    min-height: 50px !important;
  }

  .outputs .output-product,
  .output-bin-slot {
    width: 100% !important;
    height: auto !important;
    min-height: 130px !important;
    margin: 0 0 8px !important;
  }

  .transformer-footer {
    width: 100% !important;
    height: auto !important;
    position: static !important;
    margin-top: 8px !important;
    padding: 9px !important;
    grid-template-columns: 1fr !important;
    gap: 8px !important;
    border-radius: 8px;
    background: #d6d8c6;
  }

  .footer-info {
    display: none !important;
  }

  .transformer-footer > p {
    padding: 0 !important;
    font-size: 0.8rem !important;
  }

  .recipe-visuals {
    height: auto !important;
    min-height: 90px !important;
    overflow-x: auto !important;
  }

  .recipe-visuals .visual-slot {
    min-width: 90px !important;
    height: 82px !important;
    flex: 0 0 90px !important;
  }

  /* Match-3 */
  .match-window .window-titlebar {
    height: var(--mobile-title-height) !important;
    min-height: var(--mobile-title-height) !important;
    padding: 7px 9px !important;
    display: flex !important;
    border-bottom: 1px solid #178edc !important;
    background: linear-gradient(180deg, #0c3977, #061d43) !important;
  }

  .match-window .window-titlebar > div {
    width: auto !important;
    height: auto !important;
    position: static !important;
    clip-path: none !important;
  }

  .match-window .window-titlebar h2 {
    color: #f4f8ff !important;
  }

  .match-window .close-button {
    width: 44px !important;
    height: 44px !important;
    position: static !important;
    border: 1px solid #d36c4d !important;
    color: #fff0e7 !important;
    background: linear-gradient(#b84f31, #6d2115) !important;
  }

  .match-window .window-content {
    height: calc(100% - var(--mobile-title-height)) !important;
    padding: 8px !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
  }

  .match-game {
    width: 100% !important;
    height: auto !important;
    min-height: 100% !important;
    overflow: visible !important;
  }

  .match-header {
    align-items: stretch !important;
    flex-direction: column !important;
    gap: 7px !important;
  }

  .match-copy p {
    font-size: 0.78rem !important;
  }

  .match-score {
    display: grid !important;
    grid-template-columns: repeat(4, minmax(0, 1fr)) !important;
    gap: 5px !important;
  }

  .match-score span {
    min-width: 0 !important;
    padding: 5px 2px !important;
    font-size: 0.62rem !important;
  }

  .match-score strong {
    font-size: 1.05rem !important;
  }

  .match-layout {
    width: 100% !important;
    display: flex !important;
    flex-direction: column !important;
    gap: 8px !important;
  }

  .match-layout > div:first-child {
    width: 100% !important;
  }

  .match-board {
    --match-cell: min(10.5vw, 52px) !important;
    width: fit-content !important;
    max-width: 100% !important;
    margin-inline: auto !important;
    gap: 2px !important;
    padding: 4px !important;
  }

  .match-rewards {
    width: 100% !important;
    height: auto !important;
    max-height: none !important;
    overflow: visible !important;
  }

  .match-tools-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }

  .active-materials {
    width: 100% !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    overscroll-behavior-x: contain;
  }

  .active-materials > strong,
  .active-materials > small,
  .active-materials > div {
    flex: 0 0 auto !important;
  }
}

@media (max-width: 0px) {
  :root {
    --mobile-title-height: 58px;
  }

  html {
    font-size: 13px !important;
  }

  .profile-identity,
  .profile-facts,
  .profile-statistics > div,
  .profile-lower,
  .profile-collection > div,
  .reference-depot .depot-ref-metrics,
  .reference-depot .depot-truck-arrivals,
  .reference-depot .depot-terminal-actions,
  .refresearch-resources,
  .refresearch-active > div,
  .match-tools-grid {
    grid-template-columns: 1fr !important;
  }

  .market-banner,
  .friend-offer-showcase {
    display: flex !important;
    flex-direction: column !important;
  }

  .market-products,
  .market-selling > div,
  .market-command-trade .market-selling > div,
  .market-offer-grid,
  .market-favorite-grid,
  .reference-depot .input-cards,
  .reference-depot .product-cards {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }

  .profile-league {
    grid-column: auto;
  }

  .refresearch-brand {
    align-items: center !important;
  }

  .refresearch-brand h2 {
    font-size: 1.45rem !important;
  }

  .refresearch-boosters > article,
  .refresearch-bonuses article {
    grid-template-columns: 52px minmax(0, 1fr) !important;
  }

  .refresearch-boosters > article > button,
  .refresearch-bonuses article > strong {
    grid-column: 1 / -1 !important;
  }

  .refpower-brand h2 {
    font-size: 1.05rem !important;
  }

  .refpower-emblem {
    width: 42px !important;
    height: 42px !important;
  }

  .refpower-mini-stats {
    grid-template-columns: 1fr !important;
  }

  .refpower-generator-row,
  .refpower-storage article,
  .refpower-fuel article {
    display: flex !important;
    flex-direction: column !important;
  }

  .refpower-generator-row img,
  .refpower-storage article img,
  .refpower-fuel article img {
    width: 100% !important;
    height: 110px !important;
  }

  .match-board {
    --match-cell: calc((100vw - 48px) / 8) !important;
  }

  .match-tools-grid {
    display: grid !important;
  }
}

@media (max-width: 0px) {
  .market-products,
  .market-selling > div,
  .market-command-trade .market-selling > div,
  .market-offer-grid,
  .market-favorite-grid,
  .reference-depot .input-cards,
  .reference-depot .product-cards {
    grid-template-columns: 1fr !important;
  }
}
