/* Phone layout: keep the complete table, hand and current actions inside one viewport. */
@media (max-width: 700px) {
  html, body { width: 100%; min-width: 0; overscroll-behavior: none; }
  body:has(.card-game-shell) { overflow: hidden; }
  .shell:has(.card-game-shell) {
    --mobile-nav-height: calc(48px + env(safe-area-inset-bottom));
    display: block;
    min-height: 100dvh;
  }
  .shell:has(.card-game-shell) .rail {
    position: fixed;
    inset: auto 0 0;
    z-index: 80;
    width: 100%;
    height: var(--mobile-nav-height);
    display: flex;
    flex-direction: row;
    justify-content: center;
    gap: 1px;
    padding: 2px 4px env(safe-area-inset-bottom);
    border-top: 1px solid #d4b57455;
    background: #102f27f5;
    box-shadow: 0 -5px 16px #0b160e66;
  }
  .shell:has(.card-game-shell) .rail .brand,
  .shell:has(.card-game-shell) .rail-line,
  .shell:has(.card-game-shell) .profile-dot { display: none; }
  .shell:has(.card-game-shell) .rail-bottom { display: contents; }
  .shell:has(.card-game-shell) .nav-button {
    width: min(58px, calc((100vw - 12px) / 6));
    height: 43px !important;
    padding: 1px;
    gap: 0;
    border-radius: 7px;
    font-size: 7px;
    line-height: 1.05;
  }
  .shell:has(.card-game-shell) .nav-button .nav-icon { font-size: 15px; line-height: 17px; }
  .shell:has(.card-game-shell) .content {
    height: calc(100dvh - var(--mobile-nav-height));
    min-height: 0;
    overflow: hidden;
  }
  .shell:has(.card-game-shell) .topbar { display: none; }
  .shell:has(.card-game-shell) .connection-warning {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 90;
    padding: 6px 10px;
    font-size: 9px;
  }
  .card-game-shell,
  .card-game-shell .game-layout,
  .card-game-shell .game-board { height: 100%; min-height: 0; }
  .card-game-shell { overflow: hidden; }
  .card-game-shell .game-layout { display: block; }
  .card-game-shell .game-board {
    display: flex;
    flex-direction: column;
    padding: 2px 0 3px;
    overflow: hidden;
  }
  .card-game-shell .game-hud {
    flex: 0 0 auto;
    width: calc(100% - 10px);
    min-height: 30px;
    margin: 0 auto -4px;
    padding: 4px 8px;
    gap: 5px;
    border-radius: 6px 6px 12px 12px;
  }
  .card-game-shell .game-hud .round-label { font-size: 13px; }
  .card-game-shell .game-hud .meta-rule {
    display: inline-flex;
    align-items: center;
    min-width: 0;
    color: #f2d98c;
    font-size: 0;
    font-weight: 700;
    white-space: nowrap;
  }
  .card-game-shell .game-hud .meta-rule::after { content: attr(data-mobile); font-size: 9px; }
  .card-game-shell .game-hud .subtle-button { padding: 3px 4px; font-size: 8px; }
  .card-game-shell .game-hud .pill { padding: 3px 6px; font-size: 7px; }

  .card-table-scene,
  .table-stage.spacious-table {
    flex: 1 1 auto;
    width: 100%;
    height: auto;
    min-height: 0 !important;
    margin: 0;
    padding: 31px 4px 43px;
  }
  .card-table-scene::before { inset: 13px 2px 8px; border-radius: 22px; }
  .card-table-scene::after { inset: 20px 8px 16px; border-radius: 16px; }
  .spacious-table .table-felt.table-surface {
    width: 100%;
    height: 100%;
    min-height: 0;
    padding: 43px 35px 42px;
    overflow: hidden;
    border-radius: 13px;
    grid-template-columns: minmax(0, 1fr) minmax(124px, 1.7fr) minmax(0, 1fr);
    grid-template-areas: 'top top top' 'left center right' 'bottom bottom bottom';
    grid-template-rows: minmax(36px, auto) minmax(105px, 1fr) minmax(36px, auto);
    gap: 4px;
  }
  .spacious-table .table-felt.table-surface::before { inset: 5px; border-radius: 9px; }
  .table-surface .table-monogram { display: none; }

  .spacious-table .player { width: 126px; }
  .spacious-table .player-nameplate { min-height: 41px; padding: 4px 21px 4px 5px; gap: 4px; border-radius: 8px; }
  .spacious-table .player .avatar { width: 22px; height: 22px; font-size: 10px; }
  .spacious-table .player-name { font-size: 9px; }
  .spacious-table .player-detail,
  .spacious-table .player-opening { margin-top: 1px; font-size: 7px; }
  .spacious-table .player-score { padding: 1px 3px; font-size: 7px; }
  .player-card-count { top: -6px; right: -5px; min-width: 20px; height: 20px; padding: 0 3px; font-size: 8px; }
  .spacious-table .player-fixed-penalty { margin-top: 1px; padding: 1px 4px; font-size: 7px; }
  .spacious-table .player-meld-count { margin-top: 2px; padding: 2px 4px; font-size: 6px; }
  .spacious-table .player.seat-top { top: 8px; }
  .spacious-table .player.seat-bottom { bottom: 12px; }
  .spacious-table .player.seat-left,
  .spacious-table .player.seat-right { width: 78px; }
  .spacious-table .player.seat-left { left: 0; }
  .spacious-table .player.seat-right { right: 0; }
  .spacious-table .player.seat-left .player-nameplate,
  .spacious-table .player.seat-right .player-nameplate {
    min-height: 110px;
    padding: 5px 11px 5px 4px;
    gap: 2px;
  }
  .spacious-table .player.seat-left .avatar,
  .spacious-table .player.seat-right .avatar { width: 20px; height: 20px; }
  .spacious-table .player.seat-left .player-info,
  .spacious-table .player.seat-right .player-info { width: 100%; }
  .spacious-table .player.seat-left .player-detail,
  .spacious-table .player.seat-right .player-detail,
  .spacious-table .player.seat-left .player-opening,
  .spacious-table .player.seat-right .player-opening {
    display: block;
    margin-top: 1px;
    font-size: 7px;
    line-height: 1.15;
    white-space: normal;
  }
  .spacious-table .player.seat-left .player-meld-count,
  .spacious-table .player.seat-right .player-meld-count {
    max-width: 100%;
    margin-top: 2px;
    padding: 2px 3px;
    white-space: normal;
  }
  .opponent-hand { width: 80px; height: 34px; }
  .opponent-card-back { width: 18px; height: 27px; transform: translateX(calc(-50% + var(--card-center) * 5px)) rotate(calc(var(--card-center) * 1deg)); }
  .seat-top .opponent-hand { top: 38px; }
  .seat-left .opponent-hand { left: 73px; }
  .seat-right .opponent-hand { right: 73px; }
  .seat-left .opponent-card-back,
  .seat-right .opponent-card-back { transform: translateY(calc(-50% + var(--card-center) * 3.5px)) rotate(calc(90deg + var(--card-center) * .7deg)); }
  .player-turn-arrow { width: 19px; height: 24px; }
  .seat-top .player-turn-arrow { bottom: -50px; }
  .seat-bottom .player-turn-arrow { top: -28px; }
  .seat-left .player-turn-arrow { right: -31px; }
  .seat-right .player-turn-arrow { left: -31px; }

  .table-toolbar { min-height: 105px; width: 100%; max-width: 190px; gap: 2px 9px; padding: 0; }
  .table-toolbar .stock-pile { padding-right: 3px; }
  .stock-pile.has-bottom { padding-left: 20px; }
  .stock-pile .stock-top { width: 45px; }
  .bottom-peek { top: 8px; width: 22px; height: 66px; }
  .table-toolbar .card { width: 45px; height: 66px; }
  .table-toolbar .card-corner { top: 4px; left: 4px; font-size: 13px; }
  .table-toolbar .card-corner small { font-size: 10px; }
  .table-toolbar .card-corner.reverse { inset: auto 4px 4px auto; }
  .table-toolbar .card-center { font-size: 21px; }
  .table-toolbar .card-back::after { inset: 20px 8px; font-size: 14px; }
  .discard-stack { width: 65px; height: 82px; }
  .discard-layer { left: 10px; top: 8px; }
  .discard-stack .empty-pile { inset: 8px 10px; width: 45px; height: 66px; }
  .discard-count, .stock-count { min-width: 18px; padding: 3px; font-size: 8px; }
  .spacious-table .discard-pile .discard-source,
  .pile-label { max-width: 92px; font-size: 7px; line-height: 1.2; }
  .table-toolbar .table-turn { min-height: 25px; padding: 4px 6px; font-size: 8px; line-height: 1.35; }
  .table-turn small { display: none; }

  .meld-section { gap: 2px; padding: 1px 0; }
  .meld-heading { gap: 2px 4px; padding: 0; font-size: 7px; line-height: 1.1; }
  .meld-heading span { font-size: 6px; }
  .owner-melds { gap: 2px 3px; }
  .spacious-table .meld { padding: 1px; }
  .spacious-table .meld-cards { gap: 1px; }
  .spacious-table .meld .card.tiny { width: 24px; height: 35px; }
  .spacious-table .meld .card.tiny .card-corner { top: 2px; left: 2px; font-size: 10px; }
  .spacious-table .meld .card.tiny .card-corner small { font-size: 9px; }
  .spacious-table .meld .card.tiny:not(.joker) .card-center { font-size: 11px; }

  .card-game-shell .player-rack-panel {
    flex: 0 0 auto;
    width: calc(100% - 4px);
    margin: -2px auto 0;
    padding: 6px 6px 4px;
    border-radius: 10px 10px 3px 3px;
    overflow: hidden;
  }
  .player-rack-panel .hand-heading {
    min-height: 66px;
    margin: 0;
    gap: 4px;
    overflow: visible;
    flex-wrap: wrap;
    align-content: flex-start;
  }
  .player-rack-panel .hand-heading::after {
    content: '';
    order: 2;
    flex: 0 0 100%;
    height: 0;
  }
  .player-rack-panel .hand-heading h3 { order: 1; flex: 1 1 72px; font-size: 11px; }
  .player-rack-panel .hand-meta { display: none; }
  .player-rack-panel .hand-heading .spacer { display: none; }
  .player-rack-panel .layout-score { order: 1; padding: 5px 6px; font-size: 9px; white-space: nowrap; }
  .player-rack-panel .subtle-button {
    order: 3;
    flex: 1 1 0;
    min-width: 0;
    min-height: 40px;
    margin-left: 0;
    padding: 7px 5px;
    border: 1px solid #e5c47b61;
    border-radius: 7px;
    font-size: 10px;
    line-height: 1.15;
    white-space: normal;
    background: #3a211578;
  }
  .player-rack-panel .inspection-button { order: 3; margin-left: 0; padding: 7px 5px; }
  .player-rack-panel .hand-actions { flex-wrap: nowrap; gap: 3px; padding-top: 3px; overflow-x: auto; scrollbar-width: none; }
  .player-rack-panel .hand-actions::-webkit-scrollbar { display: none; }
  .player-rack-panel .hand-actions .btn { flex: 1 1 auto; min-width: max-content; min-height: 43px; padding: 8px 9px; font-size: 10px; }
  .player-rack-panel .hand-actions .btn:disabled { display: none; }
  .player-rack-panel .hand-actions .move-card { flex: 0 0 27px; min-width: 27px; }
  .player-rack-panel .rack {
    min-height: 75px;
    justify-content: center;
    padding: 10px 0 1px;
    overflow: hidden;
  }
  .player-rack-panel .rack .card {
    width: 46px;
    height: 69px;
    margin-left: -27px;
    transform: translateY(calc(var(--fan-distance) * .22px)) rotate(calc(var(--fan-index) * .25deg));
  }
  .player-rack-panel .rack .card:first-child { margin-left: 0; }
  .player-rack-panel .rack .card.group-end { margin-right: 0; border-right: 2px solid #d5aa53; }
  .player-rack-panel .rack .card:hover { transform: translateY(calc(var(--fan-distance) * .15px - 5px)) rotate(calc(var(--fan-index) * .18deg)); }
  .player-rack-panel .rack .card.selected { transform: translateY(calc(var(--fan-distance) * .12px - 9px)) rotate(calc(var(--fan-index) * .12deg)); }
  .player-rack-panel .rack .card-corner { top: 4px; left: 4px; font-size: 14px; }
  .player-rack-panel .rack .card-corner small { font-size: 10px; }
  .player-rack-panel .rack .card-corner.reverse { inset: auto 4px 4px auto; }
  .player-rack-panel .rack .card-center { font-size: 21px; }
  .player-rack-panel .hand-help { display: none; }
  .drawn-card { margin-bottom: 2px; padding: 5px; }
  .drawn-card .card { width: 40px; height: 59px; }
  .draw-reveal { bottom: 41px; transform: translateX(-50%) scale(.82); transform-origin: bottom center; }

  .cut-table-scene { min-height: 0; padding: 31px 4px 20px; }
  .cut-table-surface { min-height: 0 !important; padding: 44px 30px !important; }
  .cut-table-center { width: min(304px, 88vw); min-height: 210px; padding: 12px; }
  .cut-copy h2 { font-size: 19px; }
}

/* Portrait phones keep the real hand order in one readable, gently curved row. */
@media (max-width: 700px) and (orientation: portrait) {
  .player-rack-panel .rack {
    display: flex;
    align-items: flex-start;
    justify-content: center;
    min-height: 91px;
    padding: 6px 4px 1px;
    overflow: hidden;
  }
  .player-rack-panel .rack .card {
    flex: 0 0 54px;
    width: 54px;
    height: 79px;
    margin-left: -32px;
    transform: translateY(calc(var(--fan-distance) * .95px)) rotate(calc(var(--fan-index) * .55deg));
  }
  .player-rack-panel .rack .card:first-child { margin-left: 0; }
  .player-rack-panel .rack .card:hover { transform: translateY(calc(var(--fan-distance) * .55px - 5px)) rotate(calc(var(--fan-index) * .35deg)); }
  .player-rack-panel .rack .card.selected { transform: translateY(calc(var(--fan-distance) * .4px - 10px)) rotate(calc(var(--fan-index) * .28deg)); }
  .player-rack-panel .rack .card-corner { font-size: 17px; }
  .player-rack-panel .rack .card-corner small { font-size: 12px; }
  .player-rack-panel .rack .card-center { font-size: 25px; }
}

@media (max-width: 340px) {
  .player-rack-panel .rack { min-height: 84px; padding-inline: 3px; }
  .player-rack-panel .rack .card { flex-basis: 50px; width: 50px; height: 73px; margin-left: -32px; }
  .player-rack-panel .layout-score { display: none; }
  .card-game-shell .game-hud .subtle-button { font-size: 7px; }
  .card-game-shell .game-hud .meta-rule::after { font-size: 8px; }
  .spacious-table .player.seat-left,
  .spacious-table .player.seat-right { width: 70px; }
  .spacious-table .player.seat-left .player-nameplate,
  .spacious-table .player.seat-right .player-nameplate { min-height: 98px; padding-right: 9px; }
  .seat-left .opponent-hand { left: 65px; }
  .seat-right .opponent-hand { right: 65px; }
}

/* A phone turned sideways uses the table and hand as two adjacent surfaces. */
@media (max-width: 950px) and (max-height: 540px) and (orientation: landscape) {
  body:has(.card-game-shell) { overflow: hidden; }
  .shell:has(.card-game-shell) {
    --mobile-nav-height: calc(41px + env(safe-area-inset-bottom));
    display: block;
    min-height: 100dvh;
  }
  .shell:has(.card-game-shell) .topbar { display: none; }
  .shell:has(.card-game-shell) .rail {
    position: fixed;
    inset: auto 0 0;
    z-index: 80;
    width: 100%;
    height: var(--mobile-nav-height);
    display: flex;
    flex-direction: row;
    justify-content: center;
    gap: 2px;
    padding: 1px 4px env(safe-area-inset-bottom);
  }
  .shell:has(.card-game-shell) .rail .brand,
  .shell:has(.card-game-shell) .rail-line,
  .shell:has(.card-game-shell) .profile-dot { display: none; }
  .shell:has(.card-game-shell) .rail-bottom { display: contents; }
  .shell:has(.card-game-shell) .nav-button { width: min(62px, calc((100vw - 18px) / 6)); height: 37px !important; font-size: 7px; }
  .shell:has(.card-game-shell) .nav-button .nav-icon { font-size: 14px; line-height: 15px; }
  .shell:has(.card-game-shell) .content { height: calc(100dvh - var(--mobile-nav-height)); overflow: hidden; }
  .card-game-shell,
  .card-game-shell .game-layout,
  .card-game-shell .game-board { height: 100%; min-height: 0; }
  .card-game-shell .game-board {
    display: grid;
    grid-template-columns: minmax(350px, 1.18fr) minmax(285px, .82fr);
    grid-template-rows: 28px minmax(0, 1fr);
    padding: 1px 2px 2px;
    overflow: hidden;
  }
  .card-game-shell .game-hud { grid-column: 1 / -1; width: calc(100% - 6px); min-height: 28px; margin: 0 auto -3px; padding: 3px 8px; }
  .card-game-shell .game-hud .meta-rule {
    display: inline-flex;
    align-items: center;
    color: #f2d98c;
    font-size: 0;
    font-weight: 700;
    white-space: nowrap;
  }
  .card-game-shell .game-hud .meta-rule::after { content: attr(data-mobile); font-size: 9px; }
  .card-table-scene,
  .table-stage.spacious-table { grid-column: 1; grid-row: 2; height: 100%; min-height: 0 !important; padding: 27px 3px 30px; }
  .spacious-table .table-felt.table-surface { height: 100%; min-height: 0; padding: 35px 34px 34px; grid-template-rows: minmax(24px, auto) minmax(80px, 1fr) minmax(24px, auto); }
  .card-game-shell .player-rack-panel {
    grid-column: 2;
    grid-row: 2;
    align-self: stretch;
    width: calc(100% - 3px);
    height: calc(100% - 3px);
    margin: 1px 1px 2px 0;
    padding: 5px;
    border-radius: 9px;
  }
  .player-rack-panel .hand-heading { min-height: 64px; }
  .player-rack-panel .hand-actions .btn { min-height: 40px; padding: 6px 7px; font-size: 9px; }
  .player-rack-panel .hand-actions .btn:disabled { display: none; }
  .player-rack-panel .rack { display: flex; min-height: 68px; flex: 1 1 auto; padding-top: 8px; }
  .player-rack-panel .rack .card { width: 43px; height: 64px; margin-left: -26px; }
  .spacious-table .player { width: 112px; }
  .spacious-table .player.seat-top,
  .spacious-table .player.seat-bottom { width: 154px; }
  .spacious-table .player-nameplate { min-height: 34px; }
  .spacious-table .player.seat-top .player-opening,
  .spacious-table .player.seat-bottom .player-opening { white-space: nowrap; }
  .spacious-table .player.seat-left,
  .spacious-table .player.seat-right { width: 72px; }
  .spacious-table .player.seat-left .player-nameplate,
  .spacious-table .player.seat-right .player-nameplate { min-height: 86px; padding: 4px 10px 4px 3px; gap: 2px; }
  .spacious-table .player.seat-left .player-info,
  .spacious-table .player.seat-right .player-info { width: 100%; }
  .spacious-table .player.seat-left .player-detail,
  .spacious-table .player.seat-right .player-detail,
  .spacious-table .player.seat-left .player-opening,
  .spacious-table .player.seat-right .player-opening { display: block; margin-top: 1px; font-size: 7px; line-height: 1.1; white-space: normal; }
  .seat-left .opponent-hand { left: 67px; }
  .seat-right .opponent-hand { right: 67px; }
  .table-toolbar { min-height: 75px; max-width: 175px; }
  .table-toolbar .card { width: 39px; height: 57px; }
  .discard-stack { width: 57px; height: 70px; }
  .cut-table-scene { grid-column: 1 / -1; }
}
