/* 行動裝置適配：僅在窄螢幕 / 觸控裝置生效，桌面樣式完全不受影響
   依賴 index.html 的 viewport-fit=cover 與 body[data-screen] 狀態標記
   視覺沿用 shell.css 的復古街機 token，此處只處理版面、觸控尺寸與機台配色 */
:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

html { -webkit-text-size-adjust: 100%; }
html, body { overscroll-behavior: none; }
body { -webkit-tap-highlight-color: transparent; }
button, input, select, textarea { touch-action: manipulation; }

/* 桌面保持原樣，觸控元件不出現 */
.hint-touch, .touch-bar, .rotate-tip, .touch-hint, .tip-touch { display: none; }

@media (max-width: 900px) {
  /* ---------- 外殼：頂欄兩行 + 可滑動導覽 ---------- */
  .app { min-height: 100dvh; }
  .topbar {
    height: auto; min-height: 0; flex-wrap: wrap; gap: 8px 10px;
    padding: calc(10px + var(--safe-top)) calc(12px + var(--safe-right)) 8px calc(12px + var(--safe-left));
  }
  .brand-mark { width: 32px; height: 32px; }
  .brand-mark svg { width: 18px; height: 18px; }
  .brand-text b { font-size: 15px; letter-spacing: .12em; }
  .brand-text small { display: none; }
  .account { margin-left: auto; gap: 6px; }
  .account-chip { padding: 4px 10px 4px 4px; font-size: 11px; }
  .account .btn { padding: 8px 12px; font-size: 12px; min-height: 38px; }
  .nav {
    order: 3; width: 100%; margin-right: 0; gap: 6px;
    overflow-x: auto; -webkit-overflow-scrolling: touch;
    scrollbar-width: none; padding-bottom: 2px;
  }
  .nav::-webkit-scrollbar { display: none; }
  .nav-btn {
    flex: 1 0 auto; min-height: 40px; padding: 8px 14px;
    white-space: nowrap; font-size: 12px;
  }

  .main {
    padding: 18px calc(14px + var(--safe-right)) calc(24px + var(--safe-bottom)) calc(14px + var(--safe-left));
  }
  .screen { animation-duration: .18s; }
  .panel { padding: 16px; box-shadow: 4px 4px 0 var(--ink); }
  .section-head { flex-wrap: wrap; gap: 4px 10px; margin: 20px 0 12px; }
  .section-head h2 { font-size: 19px; }
  .section-head .muted { font-size: 11px; }
  .footer {
    gap: 10px;
    padding: 12px calc(14px + var(--safe-right)) calc(12px + var(--safe-bottom)) calc(14px + var(--safe-left));
    font-size: 10px; flex-wrap: wrap;
  }
  .footer .tiny { font-size: 10px; }
  .footer-top, .footer-bottom { flex-direction: column; align-items: flex-start; gap: 9px; }
  .footer-brand { font-size: 11px; }
  .footer-links {
    display: grid; grid-template-columns: repeat(3, 1fr); gap: 5px; width: 100%;
  }
  .footer-links a {
    justify-content: center; min-height: 38px; padding: 4px 3px;
    border: 1px solid rgba(243, 234, 214, .34); background: rgba(243, 234, 214, .05);
    font-size: 10px; letter-spacing: .02em; text-align: center; line-height: 1.3;
  }
  .footer-links a:first-child { border-left: 1px solid rgba(243, 234, 214, .34); }
  .footer-links a:active { background: var(--red); border-color: var(--red); }
  .footer-bottom { padding-top: 9px; }
  .footer-copy { font-size: 9px; line-height: 1.5; }

  /* 按鈕與輸入：點擊區域至少 40px，輸入 16px 避免 iOS 自動放大 */
  .btn { min-height: 44px; padding: 10px 18px; }
  .btn.mini { min-height: 36px; padding: 6px 14px; }
  .tab { min-height: 42px; }
  .form input { font-size: 16px; padding: 12px 14px; }
  .link-btn { min-height: 40px; display: inline-flex; align-items: center; }

  /* Toast 放在底部，避免壓住頂欄的帳號資訊 */
  .toast-host {
    left: calc(12px + var(--safe-left)); right: calc(12px + var(--safe-right));
    bottom: calc(48px + var(--safe-bottom)); top: auto; align-items: center;
  }
  .toast { min-width: 0; max-width: 100%; font-size: 12px; padding: 10px 14px; }

  /* ---------- 登入頁：保留標題層次，避免只剩一張表單的空白 ---------- */
  .auth-layout { min-height: auto; gap: 22px; }
  .auth-intro { display: block; }
  .auth-intro .score-strip { font-size: 9px; gap: 10px; padding: 5px 10px; margin-bottom: 12px; box-shadow: 3px 3px 0 var(--red); }
  /* 招牌貼紙在小屏等比收小，不加描邊以免繁體筆畫糊在一起 */
  .auth-intro h1 {
    font-size: 28px; line-height: 1.2; margin: 10px 0 18px; letter-spacing: .04em;
    padding: 3px 12px 5px; box-shadow: 4px 4px 0 var(--ink);
    transform: rotate(-1.2deg);
  }
  .auth-intro .lead { font-size: 12px; line-height: 1.7; }
  .auth-intro .feature-list, .auth-intro .eyebrow::after { display: none; }
  .h1-dot { width: 8px; height: 8px; margin-left: 6px; }
  .auth-card { padding: 20px; padding-top: 24px; max-width: 460px; }
  .coin-label { font-size: 9px; margin-bottom: 12px; }
  .auth-foot { margin-top: 12px; }

  /* ---------- 機庫 ---------- */
  .hangar-grid { gap: 16px; }
  .col-side { order: 2; }
  .col-main { gap: 14px; }
  .mode-grid { grid-template-columns: 1fr; gap: 14px; }
  .mode-card { padding: 16px; }
  .mode-card p { min-height: 0; }
  .mode-icon { width: 36px; height: 28px; margin-bottom: 10px; }
  .mode-start { margin-top: 12px; }
  .player-top { gap: 12px; }
  .avatar-lg { width: 48px; height: 48px; font-size: 20px; }
  .stat-grid { gap: 10px; margin-top: 14px; }
  .stat-item { padding: 10px 8px; }
  .stat-item b { font-size: 18px; }
  .run-row { padding: 10px; }
  .tip-desktop { display: none; }
  .tip-touch { display: block; }
  .tips p { font-size: 12px; line-height: 1.6; }

  /* ---------- 排行榜 ---------- */
  .panel.wide { max-width: none; }
  .lb-tabs { gap: 8px; margin-bottom: 14px; }
  .lb-tab { padding: 8px 14px; min-height: 38px; }
  .lb-row { padding: 11px 8px; gap: 6px; }

  /* ---------- 商城 ---------- */
  .shop-head { gap: 12px; margin-bottom: 14px; }
  .shop-head p { font-size: 12px; }
  .wallet { width: 100%; justify-content: space-between; gap: 8px; padding: 10px 14px; }
  .wallet b { font-size: 18px; }
  .shop-grid { grid-template-columns: 1fr; gap: 14px; }
  .item-card { flex-direction: column; gap: 12px; padding: 16px; }
  .item-icon { width: 38px; height: 38px; flex-basis: 38px; font-size: 11px; }
  .item-buy {
    flex-direction: row; align-items: center; justify-content: space-between;
    width: 100%; min-width: 0; gap: 10px;
    padding-top: 12px; border-top: 2px dashed var(--ink-2);
  }
  .item-buy .btn { min-width: 120px; }
  .bonus-bar { gap: 8px; }
  .bonus-chip { flex: 1 1 28%; min-width: 0; padding: 10px 6px; }
  .guest-inline { flex-direction: column; align-items: flex-start; gap: 8px; }
  .order-row { flex-wrap: wrap; padding: 10px 12px; }

  /* ---------- 戰機塗裝 ---------- */
  .skin-grid { grid-template-columns: 1fr; gap: 12px; }
  .skin-card { gap: 10px; padding: 12px; }
  .skin-preview-wrap { flex-basis: 80px; }
  .skin-preview { width: 80px; height: 96px; }
  .skin-top h3 { font-size: 14px; }
  .skin-info p { font-size: 12px; }
  .skin-price { font-size: 15px; }
  .hangar-skin-body { gap: 10px; }
  .hangar-skin-body canvas { width: 70px; height: 84px; flex-basis: 70px; }
  .hangar-skin-body b { font-size: 15px; }

  /* ---------- 儲值 ---------- */
  .demo-notice { flex-direction: column; gap: 6px; padding: 12px 14px; font-size: 11px; }
  .pack-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .pack-card { padding: 14px 8px; }
  .pack-icon { font-size: 10px; }
  .pack-coins { font-size: 20px; }
  .pack-price { font-size: 16px; }
  .channel-list { gap: 8px; }
  .channel { width: 100%; min-width: 0; padding: 12px 14px; min-height: 48px; }
  .checkout-actions { flex-wrap: wrap; }
  .checkout-actions .btn { flex: 1 1 130px; }

  /* ---------- 遊戲：機台艙體、畫布儘量撐滿 ---------- */
  /* 手機隱藏頂欄，改由這條警示斜紋帶出機台感；桌面由頂欄自帶斜紋 */
  body[data-screen="game"] .main { position: relative; }
  body[data-screen="game"] .main::before {
    content: ''; position: absolute; left: 0; right: 0; top: 0; height: 7px; z-index: 3;
    background: repeating-linear-gradient(45deg, var(--red) 0 12px, var(--ink) 12px 24px);
    pointer-events: none;
  }
  body[data-screen="game"] .app {
    height: 100dvh; min-height: 0; overflow: hidden;
  }
  body[data-screen="game"] .topbar,
  body[data-screen="game"] .footer { display: none; }
  body[data-screen="game"] .main {
    padding: calc(8px + var(--safe-top)) calc(8px + var(--safe-right)) calc(6px + var(--safe-bottom)) calc(8px + var(--safe-left));
    max-width: none; flex: 1 1 auto; min-height: 0; overflow: hidden;
    display: flex; flex-direction: column;
  }
  body[data-screen="game"] .screen { animation: none; flex: 1; min-height: 0; display: flex; flex-direction: column; }
  body[data-screen="game"] .game-layout {
    display: flex; flex-direction: column; align-items: stretch;
    gap: 8px; flex: 1 1 auto; min-height: 0;
  }
  body[data-screen="game"] .stage {
    flex: 1 1 auto; min-height: 0; position: relative; width: 100%;
    display: block; gap: 0;
    padding: 0; background: transparent; border: 0; box-shadow: none;
  }
  body[data-screen="game"] .stage::before { display: none; }
  body[data-screen="game"] .wrap {
    position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
    width: auto; height: auto; aspect-ratio: auto; max-width: 100%; max-height: 100%;
    border: 3px solid var(--cream); box-shadow: 4px 4px 0 var(--red);
    /* 實際尺寸由 app.js 的 fitStage() 依可用空間計算，確保 2:3 不變形 */
  }
  body[data-screen="game"] #game { border-radius: 0; }
  body[data-screen="game"] .stage-hint { display: none; }

  /* 實時統計壓成一條可橫向滑動的資料列 */
  body[data-screen="game"] .side { width: 100%; max-width: none; flex: 0 0 auto; gap: 0; }
  body[data-screen="game"] .side > .panel:not(:first-child),
  body[data-screen="game"] .side > .btn { display: none; }
  body[data-screen="game"] .side > .panel:first-child { padding: 6px 8px; box-shadow: 3px 3px 0 var(--red); }
  body[data-screen="game"] .side h3 { display: none; }
  body[data-screen="game"] .stat-grid {
    display: flex; flex-wrap: nowrap; gap: 6px; margin: 0;
    overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none;
  }
  body[data-screen="game"] .stat-grid::-webkit-scrollbar { display: none; }
  body[data-screen="game"] .stat-item {
    flex: 0 0 auto; min-width: 64px; padding: 5px 8px;
    box-shadow: 2px 2px 0 var(--ink); text-align: center;
  }
  body[data-screen="game"] .stat-item b { font-size: 15px; }
  body[data-screen="game"] .stat-item span { font-size: 9px; }

  /* 觸控列：炸彈 / 暫停 / 音效 / 機庫，做成機台實體按鍵 */
  .touch-hint {
    display: block; flex: 0 0 auto; margin: 4px 0 0; text-align: center;
    color: var(--yellow); font-family: var(--mono);
    font-size: 10px; font-weight: 700; line-height: 1.5; letter-spacing: .04em;
  }
  .touch-bar { display: flex; gap: 6px; flex: 0 0 auto; width: 100%; }
  .touch-btn {
    flex: 1 1 0; min-width: 0; min-height: 52px;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
    border: 3px solid var(--ink); background: var(--cream); color: var(--ink);
    box-shadow: 3px 3px 0 var(--red);
    -webkit-user-select: none; user-select: none;
    transition: transform .08s ease-out, box-shadow .08s ease-out, background .12s;
  }
  .touch-btn:active { transform: translate(3px, 3px); box-shadow: 0 0 0 var(--ink); background: var(--yellow); }
  .touch-btn.primary { background: var(--red); color: var(--cream); text-shadow: 1px 1px 0 var(--red-dark); }
  .touch-btn.primary:active { background: var(--red-dark); }
  .touch-btn.off { opacity: .55; }
  .touch-btn b { font-size: 13px; font-weight: 800; line-height: 1.2; letter-spacing: .08em; }
  .touch-btn small { font-family: var(--mono); font-size: 9px; font-weight: 700; opacity: .78; line-height: 1.2; }

  /* 結算 / 暫停浮層在小屏可捲動 */
  .overlay { overflow-y: auto; padding: 10px; }
  .overlay-card {
    width: 100%; max-width: 340px; max-height: 100%; overflow-y: auto;
    padding: 20px 14px 16px; margin: auto;
  }
  .overlay-card h2 { font-size: 22px; }
  .big-score { font-size: 36px; }
  .settle-grid { gap: 6px; }
  .settle-grid div { padding: 7px 2px; }
  .overlay-actions .btn { flex: 1 1 120px; min-height: 44px; }

  body[data-screen="game"] .toast-host {
    top: auto; bottom: calc(160px + var(--safe-bottom));
  }
  .hint-desktop { display: none; }
  .hint-touch { display: inline; }
}

@media (max-width: 560px) {
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  .account-chip .chip-coin, .account-chip .chip-lv { display: none; }
  .lb-row { grid-template-columns: 34px 1fr 72px; }
  .lb-row .level { display: none; }
  .pack-grid { gap: 8px; }
  .pack-card { padding: 12px 6px; }
  .skin-preview-wrap { flex-basis: 68px; }
  .skin-preview { width: 68px; height: 82px; }
  .skin-card { gap: 8px; }
  .skin-foot { flex-wrap: wrap; }
  .touch-btn b { font-size: 12px; }
  .touch-btn small { font-size: 9px; }
}

/* 橫屏提示：不阻塞操作，只提醒直式體驗較佳 */
@media (max-width: 900px) and (orientation: landscape) and (max-height: 560px) {
  .rotate-tip {
    display: block; position: fixed; z-index: 80;
    left: 50%; top: calc(6px + var(--safe-top)); transform: translateX(-50%);
    padding: 4px 12px; border: 2px solid var(--cream);
    box-shadow: 3px 3px 0 var(--red);
    white-space: nowrap; background: var(--ink);
    color: var(--yellow); font-family: var(--mono);
    font-size: 10px; font-weight: 700; letter-spacing: .06em; pointer-events: none;
  }
  body[data-screen="game"] .game-layout { padding-top: 30px; }
  body[data-screen="game"] .touch-btn { min-height: 44px; }
}

/* 尊重系統減少動態效果設定 */
@media (prefers-reduced-motion: reduce) {
  .screen, .toast { animation: none; }
  .btn, .mode-card, .touch-btn, .tab { transition: none; }
}
