/* Available-space primitives shared by platform and game surfaces.
   The visual viewport variables are updated in client.js when browser chrome or the keyboard moves. */
:root {
  --layout-gutter: clamp(8px, 1.5vw, 20px);
  --layout-gap: clamp(6px, 1vw, 12px);
  --layout-touch: 44px;
  --layout-vv-width: 100vw;
  --layout-vv-height: 100dvh;
  --layout-vv-top: 0px;
  --layout-vv-left: 0px;
  --layout-safe-top: max(8px, env(safe-area-inset-top));
  --layout-safe-right: max(8px, env(safe-area-inset-right));
  --layout-safe-bottom: max(8px, env(safe-area-inset-bottom));
  --layout-safe-left: max(8px, env(safe-area-inset-left));
}

html, body { max-width: 100%; }
.shell, .lobby, .lobby-card, .platform-header, .platform-meta-grid,
.game-catalog, .game-entry, .waiting-room, .seat-list, .game-workspace,
.player-rail, .board-stage, .board-panel, .context-panel, .action-dock {
  min-width: 0;
  max-width: 100%;
}
.platform-header, .waiting-heading, .waiting-actions, .test-dice-actions { flex-wrap: wrap; }
.platform-meta-grid, .game-catalog, .platform-options, .seat-list {
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr));
}
.platform-stage .game-entry, .catan-stage .join-row,
.test-dice-stage .join-row, .platform-invite-card .join-row {
  min-width: 0;
}
.platform-stage .game-entry > *, .join-row > *,
.waiting-actions > *, .test-dice-actions > * { min-width: 0; }

.waiting-heading #startGame, .waiting-actions #readyRoom,
.test-dice-actions #testDiceStart, .test-dice-actions #testDiceReady {
  min-height: var(--layout-touch);
}
.playing-view #guidePrimary, .playing-view .mobile-primary-action:not([hidden]),
.playing-view .dock-actions button:not([hidden]) {
  min-height: var(--layout-touch);
}
.playing-view .game-workspace { gap: var(--layout-gap); }
.playing-view .board-stage, .playing-view .board-panel { min-width: 0; }
.playing-view .board-panel { overflow: hidden; }

/* Shared overlay bounds; content scrolls inside, while a dialog's commands stay reachable. */
dialog[open] {
  box-sizing: border-box;
  max-width: calc(var(--layout-vv-width) - var(--layout-safe-left) - var(--layout-safe-right));
  max-height: calc(var(--layout-vv-height) - var(--layout-safe-top) - var(--layout-safe-bottom));
  overscroll-behavior: contain;
}
dialog[open]:not(#tradeHubDialog):not(#tradeDialog) {
  overflow-x: hidden;
  overflow-y: auto;
}
.game-sheet[open] { display: flex; flex-direction: column; }
.game-sheet[open] > .sheet-heading { flex: 0 0 auto; }
.game-sheet[open] > :not(.sheet-heading) { min-width: 0; }
.game-sheet[open] > .trade-card { overflow-x: hidden; overflow-y: auto; }
.game-sheet .sheet-heading button, .trade-dialog-heading button,
.mobile-sheet-heading button { min-width: var(--layout-touch); min-height: var(--layout-touch); }
#tradeDialog[open] { display: flex; flex-direction: column; }
#tradeDialog .trade-dialog-heading, #tradeDialog .development-actions { flex: 0 0 auto; }
#tradeDialog #playerTradeForm { display: flex; flex-direction: column; min-height: 0; }
#tradeDialog .trade-dialog-fields { min-height: 0; overflow-x: hidden; overflow-y: auto; }
#tradeDialog .development-actions { display: flex; flex-wrap: wrap; }

/* Drawers use the same usable-height budget; only the active surface receives events. */
.game-view .top-actions.mobile-open,
.playing-view .activity-panel.mobile-open {
  max-height: calc(var(--layout-vv-height) - var(--layout-safe-top) - var(--layout-safe-bottom));
  overflow-x: hidden;
  overscroll-behavior: contain;
}
.game-view .top-actions.mobile-open .mobile-sheet-heading,
.playing-view .activity-panel.mobile-open .mobile-sheet-heading {
  position: sticky;
  top: 0;
  z-index: 1;
}
.mobile-sheet-backdrop[hidden] { display: none !important; pointer-events: none; }

@media (max-width: 760px) {
  body:not(.playing-view) .shell { width: min(100% - 2 * var(--layout-gutter), 1180px); }
  .lobby.platform-stage, .lobby.catan-stage { min-height: 0; }
  .game-view #game.is-waiting .waiting-room { min-height: 0; }
  .game-view #game.is-waiting .waiting-heading {
    top: 0;
    gap: var(--layout-gap);
  }
  .game-view #game.is-waiting .waiting-heading #startGame {
    flex: 0 1 auto;
    min-width: min(100%, 132px);
  }
  .game-view #game.is-waiting .seat-list { grid-template-columns: repeat(auto-fit, minmax(min(100%, 140px), 1fr)); }
  .game-view #game.is-waiting .waiting-actions { grid-template-columns: repeat(auto-fit, minmax(min(100%, 140px), 1fr)); }
  .playing-view .shell { max-height: var(--layout-vv-height); }
  .playing-view .game-workspace { min-height: 0; }
  .playing-view .board-stage { min-height: 0; }
  .playing-view .context-panel { min-height: 0; }
  .playing-view .action-dock { min-width: 0; }
  .playing-view .resource-hand { min-width: 0; }
  .game-sheet[open] { max-height: min(82dvh, calc(var(--layout-vv-height) - var(--layout-safe-top) - var(--layout-safe-bottom))); }
}

@media (max-height: 500px) and (max-width: 950px) {
  .playing-view .context-panel { backdrop-filter: none; }
  .playing-view .mobile-overflow-trigger { display: inline-flex; }
  .playing-view .mobile-sheet-backdrop:not([hidden]) {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 8;
  }
  .playing-view .topbar:has(.top-actions.mobile-open) { z-index: 11; }
  .playing-view .top-actions.mobile-open,
  .playing-view .activity-panel.mobile-open {
    position: fixed;
    inset: auto 0 0;
    z-index: 10;
    width: 100%;
    min-height: 0;
    max-height: calc(var(--layout-vv-height) - var(--layout-safe-top) - var(--layout-safe-bottom));
    overflow-y: auto;
    padding-bottom: var(--layout-safe-bottom);
  }
  .playing-view .top-actions.mobile-open {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 6px;
    padding-inline: var(--layout-safe-left) var(--layout-safe-right);
  }
  .playing-view .activity-panel.mobile-open { display: flex; flex-direction: column; }
  .playing-view .top-actions.mobile-open .mobile-sheet-heading,
  .playing-view .activity-panel.mobile-open .mobile-sheet-heading { display: flex; justify-content: space-between; }
  .playing-view .activity-panel.mobile-open .activity-tabs { display: flex; flex: 0 0 auto; }
  .playing-view .activity-panel.mobile-open .activity-view { min-height: 0; overflow-y: auto; }
  dialog[open]:not(#tradeHubDialog):not(#tradeDialog) {
    position: fixed;
    top: calc(var(--layout-vv-top) + var(--layout-safe-top));
    left: calc(var(--layout-vv-left) + var(--layout-vv-width) / 2);
    translate: -50% 0;
    margin: 0;
  }
  .playing-view .game-workspace {
    grid-template-columns: clamp(76px, 12vw, 118px) minmax(0, 1fr) clamp(150px, 27vw, 230px);
  }
  .playing-view .action-dock { grid-template-columns: minmax(0, 1fr) minmax(180px, 34%); }
  .playing-view .context-panel .turn-guide { overflow-y: auto; }
  .playing-view .board-feedback { grid-template-columns: minmax(0, 1fr) auto; max-height: calc(100% - 8px); }
  .playing-view .board-feedback .dice-title,
  .playing-view .board-feedback .production-result { display: none; }
  .playing-view .activity-panel:not(.mobile-open),
  .playing-view .top-actions:not(.mobile-open) { display: none; }
  .game-view #game.is-waiting .waiting-room { max-height: calc(var(--layout-vv-height) - 54px); }
  .game-view #game.is-waiting .waiting-heading { top: 0; }
}

@media (max-height: 320px) and (max-width: 760px) {
  .playing-view #game { gap: 2px; }
  .playing-view .topbar { flex-basis: 38px; min-height: 38px; }
  .playing-view .runtime-bar { min-height: 24px; }
  .playing-view .action-dock, .playing-view .action-dock .hand-bar { min-height: 44px; }
  .playing-view .game-workspace { gap: 3px; }
}
