/* =========================================================
   手机端 / 窄屏 / 矮屏 · 触控优先
   ========================================================= */

/* 安全区 + 触控基础 */
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
body {
  min-height: 100dvh;
  min-height: 100svh;
  overscroll-behavior: none;
  touch-action: manipulation;
}
/* iOS 输入框 16px 防自动放大 */
input, select, textarea {
  font-size: 16px;
}
.btn, button, .tile {
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

/* ---------- 大厅 / 登录 ---------- */
@media (max-width: 720px) {
  #screen-auth.active {
    padding: max(12px, env(safe-area-inset-top)) 12px max(16px, env(safe-area-inset-bottom));
    align-items: flex-start;
  }
  .auth-card,
  .modal-card {
    width: 100%;
    max-width: 100%;
    padding: 18px 14px;
    border-radius: 16px;
  }
  .lobby-main {
    grid-template-columns: 1fr !important;
    padding: 12px 10px max(16px, env(safe-area-inset-bottom));
    max-width: 100%;
    gap: 12px;
  }
  .lobby-grid {
    grid-template-columns: 1fr !important;
  }
  .side {
    max-height: none;
    padding: 12px;
  }
  .field input,
  .field select,
  .settings-grid input,
  .settings-grid select {
    min-height: 44px;
    font-size: 16px;
    border-radius: 12px;
  }
  .btn {
    min-height: 44px;
    padding: 12px 16px;
    font-size: 15px;
  }
  .btn.sm {
    min-height: 40px;
    padding: 10px 14px;
    font-size: 13px;
  }
  .modal {
    padding: 8px;
    align-items: flex-end;
  }
  .modal-card {
    max-height: min(92dvh, 92vh);
    width: 100%;
    border-radius: 18px 18px 12px 12px;
    margin-bottom: env(safe-area-inset-bottom, 0);
  }
  .toast-host {
    top: max(10px, env(safe-area-inset-top));
    width: min(94vw, 400px);
  }
  .toast { font-size: 13px; padding: 10px 14px; }
}

/* ---------- 牌桌舞台：手机竖屏主战场 ---------- */
@media (max-width: 720px) {
  #screen-table {
    width: 100vw;
    height: 100dvh;
    min-height: 0;
  }
  .game-stage {
    position: relative;
    width: 100vw;
    height: 100dvh;
    max-width: 100vw;
    padding: 0;
    display: block;
    overflow: hidden;
  }

  /* HUD：可横滑，不换行占高 */
  .game-hud {
    top: max(8px, env(safe-area-inset-top));
    left: max(8px, env(safe-area-inset-left));
    max-width: calc(100vw - 72px);
    overflow: visible;
  }
  .game-hud::-webkit-scrollbar { display: none; }
  .game-hud .pill {
    flex: 0 0 auto;
    padding: 4px 10px;
    font-size: 11px;
    gap: 6px;
    white-space: nowrap;
  }
  .live-log {
    height: 26px;
    max-height: 26px;
    font-size: 10px;
    padding: 2px 8px;
    border-radius: 8px;
  }

  /* 牌桌：侧栏收窄，中行尽量高 */
  .board {
    inset:
      max(2px, env(safe-area-inset-top))
      max(2px, env(safe-area-inset-right))
      max(2px, env(safe-area-inset-bottom))
      max(2px, env(safe-area-inset-left));
    width: auto;
    height: auto;
    aspect-ratio: auto;
    border-width: 4px;
    border-radius: 6px;
    grid-template-columns: minmax(54px, 15%) minmax(0, 70%) minmax(54px, 15%);
    grid-template-rows: minmax(82px, 18%) minmax(170px, 44%) minmax(150px, 38%);
  }
  .board-cell { padding: 2px; }
  .board-cell.top { padding-top: 2px; }
  .board-cell.top .seat-card {
    align-self: flex-end;
    margin-right: 6px;
    margin-left: auto;
    transform: none;
  }
  .board-cell.bottom {
    padding: 2px 4px calc(var(--self-zone-h, 150px) + 4px);
    justify-content: flex-end;
    overflow: hidden;
  }
  .board-cell.left {
    padding: 2px 0 2px 2px;
    gap: 2px;
  }
  .board-cell.right {
    padding: 2px 2px 2px 0;
    gap: 2px;
  }

  /* 座位卡：手机只留头像+短名 */
  .seat-card {
    padding: 4px 6px;
    gap: 4px;
    margin-bottom: 2px;
    border-radius: 10px;
  }
  .board-cell.left .seat-card,
  .board-cell.right .seat-card {
    min-width: 44px;
    padding: 4px 3px;
  }
  .board-cell.left .seat-card .avatar,
  .board-cell.right .seat-card .avatar,
  .seat-card .avatar.md,
  .seat-card .avatar {
    width: 32px !important;
    height: 32px !important;
    border-radius: 10px;
    border-width: 1px;
  }
  .board-cell.left .seat-card,
  .board-cell.right .seat-card {
    min-width: 72px;
    max-width: 96px;
  }
  .board-cell.left .seat-card .sn,
  .board-cell.right .seat-card .sn {
    max-width: 100%;
    font-size: 10px;
    white-space: nowrap;
    writing-mode: horizontal-tb;
  }
  .board-cell.top .seat-card .sn { max-width: 72px; font-size: 11px; white-space: nowrap; }
  .seat-card .sc { font-size: 10px; white-space: nowrap; }
  .board-cell.left .seat-card .sc,
  .board-cell.right .seat-card .sc {
    font-size: 9px;
    white-space: nowrap;
  }
  .sq-tag,
  .hu-tag {
    font-size: 9px;
    padding: 1px 6px;
    white-space: nowrap !important;
    writing-mode: horizontal-tb !important;
  }

  /* 侧手牌竖放，叠压防裁切 */
  .board-cell.left .hand-row .tile--sm,
  .board-cell.right .hand-row .tile--sm {
    --tw: 20px;
    --th: 28px;
    width: 20px;
    height: 28px;
    margin-block: -9px;
  }
  .board-cell.top .hand-row .tile--sm {
    --tw: 22px;
    --th: 30px;
    width: 22px;
    height: 30px;
  }
  .board-cell.left .discard-grid .tile--xs,
  .board-cell.right .discard-grid .tile--xs,
  .board-cell.left .meld-row .tile--xs,
  .board-cell.right .meld-row .tile--xs {
    --tw: 18px;
    --th: 26px;
    width: 18px;
    height: 26px;
  }
  .discard-grid {
    padding: 3px;
    gap: 2px;
    grid-template-columns: repeat(6, var(--tile-xs-w));
    max-width: 100%;
  }
  .discard-grid.is-side {
    grid-template-columns: repeat(1, 20px);
    max-width: calc(20px + 6px);
    padding: 2px;
  }

  /* 中心牌墙缩小 */
  .center-table {
    width: min(280px, 96%);
    height: min(230px, 94%);
    min-height: 150px;
  }
  .wall-total-badge {
    font-size: 11px;
    padding: 4px 10px;
  }
  .turn-chip {
    font-size: 11px;
    padding: 5px 10px;
    max-width: 92%;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .last-discard-tip {
    font-size: 10px;
    padding: 2px 8px;
    max-width: 100%;
  }
  .wall-tiles .tile {
    --tw: 14px;
    --th: 20px;
    width: 14px;
    height: 20px;
  }
  .wall-tiles .tile.rot-left,
  .wall-tiles .tile.rot-right {
    width: 20px;
    height: 14px;
  }
  .wall-top .wall-tiles,
  .wall-bottom .wall-tiles { width: max-content; max-width: none; }
  .wall-left .wall-tiles,
  .wall-right .wall-tiles { height: max-content; max-height: none; }

  /* 自己操作坞：触控大、手牌横滑；操作键横排居中在底部，不挡碰/弃牌，不碰右下工具坞 */
  .self-dock {
    left: max(4px, env(safe-area-inset-left));
    right: max(70px, calc(env(safe-area-inset-right) + 66px));
    bottom: max(4px, env(safe-area-inset-bottom));
    width: auto;              /* 手机用 left/right 定宽，不受 fit-content 撑开 */
    transform: none;          /* 覆盖主定义 translateX(-50%) 居中 */
    flex-direction: column;   /* 操作键在手牌上方，手牌贴底 */
    max-height: none;
    padding: 0;
    gap: 3px;
    background: transparent;
  }
  .self-dock .action-bar {
    gap: 8px;
    padding: 2px 4px;
    flex-wrap: wrap;
    min-height: 0;
    height: auto;
    width: auto;
    min-width: 0;
    max-width: 100%;
  }
  .self-dock .action-bar .btn {
    min-width: 72px;
    width: auto;
    min-height: 44px;
    font-size: 15px;
    padding: 8px 14px;
    border-radius: 8px;
  }
  .self-dock .action-bar .btn-discard,
  .self-dock .action-bar .btn-draw {
    min-width: 88px;
    min-height: 46px;
    font-size: 16px;
    padding: 10px 16px;
  }
  .btn.hu, .btn.peng, .btn.gang {
    min-width: 0 !important;
    width: auto !important;
    min-height: 46px !important;
    font-size: 16px !important;
    padding: 10px 16px !important;
  }

  .board-cell.bottom {
    padding: 2px 4px calc(var(--self-zone-h) + 4px);
  }
  .board-cell.bottom .discard-grid {
    grid-template-columns: repeat(auto-fill, var(--tile-xs-w));
    max-width: min(98%, calc(14 * var(--tile-xs-w) + 26px));
    max-height: calc(5 * var(--tile-xs-h) + 16px);
    overflow: hidden;
  }
  .board-cell.bottom .discard-grid .tile--xs {
    width: var(--tile-xs-w);
    height: var(--tile-xs-h);
  }

  .hand-tray {
    padding: 4px 2px 2px;
    background: transparent;
    border: 0;
    box-shadow: none;
  }
  .hand-tray .my-hand {
    justify-content: flex-start;
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: clip;
    -webkit-overflow-scrolling: touch;
    gap: 2px;
    min-height: calc(var(--tile-md-h) + 20px);
    padding: 16px 4px 4px;
    scrollbar-width: none;
  }
  .hand-tray .my-hand::-webkit-scrollbar { display: none; }
  .hand-tray .my-hand .tile {
    flex: 0 0 auto;
  }
  .self-bar {
    left: 0;
    bottom: calc(100% + 56px);   /* 操作键上方间隙：弃牌河下方、操作键上方，互不遮蔽 */
    max-width: min(56vw, 210px);
  }
  /* 对局中手机底坞拥挤：自家信息只留头像，放操作键上方，不压文字/按钮 */
  body.is-playing .self-bar .me-block > div:not(.avatar) { display: none; }
  body.is-playing .self-bar .me-block { gap: 0; }
  .self-bar .me-block {
    padding: 4px 8px 4px 4px;
    gap: 6px;
  }
  .table-play-actions {
    left: 50%;
    right: auto;
    bottom: max(128px, calc(env(safe-area-inset-bottom) + 118px));
    width: min(340px, calc(100vw - 20px));
    min-height: 58px;
    gap: 6px;
    padding: 6px;
  }
  .table-play-actions .btn {
    height: 46px;
    min-height: 46px;
    font-size: 16px;
  }
  body.is-playing .table-play-actions {
    visibility: hidden;
  }

  .table-utility-dock {
    right: max(6px, env(safe-area-inset-right));
    bottom: max(6px, env(safe-area-inset-bottom));
    padding: 6px;
    flex-direction: row;
  }
  .table-utility-dock:not(.is-open) > .utility-btn:not(#btn-table-more) {
    display: none;
  }
  .table-utility-dock.is-open {
    display: flex;
    flex-direction: row;
    max-width: calc(100vw - 12px);
    overflow-x: auto;
  }
  .table-more-panel {
    right: 0;
    bottom: calc(100% + 8px);
    width: min(350px, calc(100vw - 12px));
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
  .end-reveal-bar {
    bottom: calc(clamp(138px, 25dvh, 180px) + env(safe-area-inset-bottom));
    width: min(92vw, 560px);
    max-width: 92vw;
    padding: 8px;
  }
  .end-reveal-main { text-align: center; }
  /* 可点牌抬高：绝不盖掉侧牌 rot-left/right */
  .tile:not(.rot-left):not(.rot-right).is-playable:hover .tile__inner,
  .tile:not(.rot-left):not(.rot-right).is-playable:focus-visible .tile__inner {
    transform: translateY(-6px) scale(1.03);
  }
  .tile:not(.rot-left):not(.rot-right).is-selected .tile__inner {
    transform: translateY(-10px) scale(1.05);
  }
  .tile.rot-left.is-playable:hover .tile__inner,
  .tile.rot-left.is-selected .tile__inner {
    transform: translate(-50%, -50%) rotate(-90deg) scale(1.05);
  }
  .tile.rot-right.is-playable:hover .tile__inner,
  .tile.rot-right.is-selected .tile__inner {
    transform: translate(-50%, -50%) rotate(90deg) scale(1.05);
  }

  .self-bar {
    padding: 4px 2px 2px;
    gap: 6px;
  }
  .self-bar .me-block {
    padding: 4px 8px 4px 4px;
    gap: 8px;
    border-radius: 12px;
  }
  #self-avatar {
    width: 36px !important;
    height: 36px !important;
    border-radius: 10px !important;
  }
  #self-name { font-size: 13px; }
  #self-chips { font-size: 11px; }
}

/* 更窄手机（≤430） */
@media (max-width: 430px) {
  :root {
    --tile-md-w: 36px;
    --tile-md-h: 50px;
    --tile-sm-w: 20px;
    --tile-sm-h: 28px;
    --tile-xs-w: 18px;
    --tile-xs-h: 24px;
    --tile-lg-w: 42px;
    --tile-lg-h: 58px;
    --hand-gap: 2px;
  }
  .board {
    grid-template-columns: 50px minmax(0, 1fr) 50px;
    grid-template-rows: minmax(76px, 18%) minmax(160px, 44%) minmax(144px, 38%);
    border-width: 3px;
    border-radius: 6px;
  }
  .board-cell.left .seat-card .meta .sc,
  .board-cell.right .seat-card .meta .sc {
    display: none; /* 侧家筹码藏掉，省高 */
  }
  .board-watermark {
    font-size: 36px;
    letter-spacing: .2em;
  }
  .center-table {
    width: min(220px, 94%);
    height: min(184px, 92%);
    min-height: 132px;
  }
}

/* 矮屏手机（常见 667～780 高） */
@media (max-width: 720px) and (max-height: 780px) {
  :root {
    --tile-md-w: 34px;
    --tile-md-h: 48px;
  }
  .live-log { display: none; } /* 极矮时日志让位 */
  .self-dock .action-bar .btn {
    min-height: 42px;
    font-size: 15px;
  }
  .hand-tray .my-hand {
    min-height: calc(var(--tile-md-h) + 10px);
    padding-top: 6px;
  }
}

/* 横屏手机：牌桌拉宽，坞更扁 */
@media (max-width: 960px) and (max-height: 500px) and (orientation: landscape) {
  :root {
    --tile-md-w: 32px;
    --tile-md-h: 44px;
    --tile-sm-w: 18px;
    --tile-sm-h: 24px;
    --tile-xs-w: 16px;
    --tile-xs-h: 22px;
  }
  .game-stage {
    padding-left: max(4px, env(safe-area-inset-left));
    padding-right: max(4px, env(safe-area-inset-right));
  }
  .live-log { display: none; }
  .game-hud .pill { font-size: 10px; padding: 2px 8px; }
  .board {
    grid-template-columns: minmax(70px, 0.85fr) minmax(0, 2fr) minmax(70px, 0.85fr);
    grid-template-rows: minmax(62px, 20%) minmax(120px, 46%) minmax(112px, 34%);
    border-width: 3px;
  }
  .self-dock {
    left: max(8px, env(safe-area-inset-left));
    right: max(70px, calc(env(safe-area-inset-right) + 66px));
    transform: none;
    max-height: none;
    padding-bottom: max(2px, env(safe-area-inset-bottom));
  }
  .self-dock .action-bar .btn {
    min-height: 36px;
    font-size: 13px;
    padding: 6px 12px;
  }
  /* 横屏只藏头像信息，主操作仍固定在右侧。 */
  .self-bar .me-block { display: none; }
  .self-bar {
    display: flex;
    justify-content: flex-end;
    padding: 2px 0;
  }
  .table-play-actions {
    width: min(330px, calc(100vw - 12px));
    bottom: max(96px, calc(env(safe-area-inset-bottom) + 82px));
  }
  .table-play-actions .btn {
    height: 36px;
    min-height: 36px;
    font-size: 13px;
  }
  .hand-tray .my-hand {
    min-height: calc(var(--tile-md-h) + 6px);
  }
  .table-utility-dock { flex-direction: row; }
  .end-reveal-bar { bottom: calc(124px + env(safe-area-inset-bottom)); }
}

/* ---------- 定缺弹层手机 ---------- */
@media (max-width: 720px) {
  .que-modal {
    padding: max(8px, env(safe-area-inset-top)) 8px max(8px, env(safe-area-inset-bottom));
    align-items: flex-end;
  }
  .que-card--wide {
    width: 100%;
    max-width: 100%;
    max-height: 90dvh;
    overflow: auto;
    border-radius: 18px 18px 12px 12px;
  }
  .que-title { font-size: 22px !important; }
  .que-hand {
    min-height: 64px;
    gap: 3px;
  }
  .que-hand .tile {
    --tw: 32px !important;
    --th: 44px !important;
    width: 32px !important;
    height: 44px !important;
  }
  .que-btns { gap: 10px; }
  .btn.que {
    width: 96px !important;
    height: 96px !important;
    border-radius: 18px !important;
  }
  .btn.que .que-name { font-size: 28px; }
}

/* ---------- 快捷聊 / 花活 手机位置（单一真相，覆盖 flavor.css） ---------- */
@media (max-width: 720px) {
  .chat-menu {
    bottom: max(62px, calc(env(safe-area-inset-bottom) + 54px)) !important;
    left: auto !important;
    right: max(58px, calc(env(safe-area-inset-right) + 54px)) !important;
    z-index: 140;
  }
  .fun-menu {
    bottom: max(62px, calc(env(safe-area-inset-bottom) + 54px)) !important;
    right: max(58px, calc(env(safe-area-inset-right) + 54px)) !important;
    z-index: 140;
  }
  .chat-panel,
  .fun-panel {
    max-width: min(92vw, 360px);
    max-height: min(58dvh, 420px);
  }
  .chat-dock-fast,
  .fun-toggle {
    min-width: 44px;
    min-height: 44px;
  }
  .chat-dock,
  .fun-dock { display: none !important; }
  .flavor-toast-host {
    bottom: max(130px, calc(env(safe-area-inset-bottom) + 120px));
    width: min(94vw, 400px);
  }
  .chat-pop {
    max-width: min(70vw, 240px);
    font-size: 13px;
  }
  .chat-pop.pos-bottom { bottom: 28%; }
  .chat-pop.pos-top { top: 12%; }
}

/* ---------- 设置里「惊喜扩展」手机好点 ---------- */
@media (max-width: 720px) {
  .settings-grid .btn {
    min-height: 44px;
  }
  #set-fun-key,
  input[type="password"] {
    min-height: 44px;
    font-size: 16px;
  }
}
