/* ==========================================================================
   Phones and tablets (checked by tools/mobile-test.mjs, `npm run test:mobile`)

   Upright phones and tablets (≤820px wide, portrait): the board fills the screen between the players and
   the action bar; the turn card, market, tips, side bet and portfolio are tabs in a bottom sheet that opens
   from the bar. The page itself never scrolls.
   Phones on their side (landscape, ≤500px tall): the board on the left, a column on the right with the
   header, players, house rule, the tabs and the roll button.
   Loaded last; `body[data-theme]:not(.home)` outranks each look's own `[data-theme="x"]:not(.home)` rules.
   ========================================================================== */

/* hidden on desktop */
.menu-btn, .tools-btn, .side-tabs .st-turn, .side-tabs .side-close { display: none; }

/* ---------- small screens of any shape: header menu, notices ---------- */
@media (max-width: 820px), (max-height: 500px) {
  body[data-theme]:not(.home) { --header: 52px; }
  body[data-theme]:not(.home) header.top { height: var(--header); min-height: 0; flex-wrap: nowrap; gap: 10px; padding-block: 0; justify-content: flex-start; }
  body[data-theme]:not(.home) header.top .meta { display: block; flex: 1 1 0; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 12px; line-height: 1.3; -webkit-line-clamp: unset; line-clamp: unset; }
  body[data-theme]:not(.home) header.top .logo .wordmark { font-size: 18px; }
  body[data-theme]:not(.home) header.top .menu-btn { display: inline-flex; align-items: center; justify-content: center; flex: 0 0 auto; min-height: 44px; min-width: 64px; padding: 4px 14px; font-size: 14px; }
  /* the menu: a card under the header, one button per row */
  body[data-theme]:not(.home) header.top .acts { display: none; position: absolute; top: calc(100% + 6px); right: max(8px, env(safe-area-inset-right, 0px)); z-index: 70; flex-direction: column; align-items: stretch; gap: 6px; width: min(240px, calc(100vw - 16px)); padding: 10px; background: var(--surface); color: var(--fg); border: 1px solid var(--line-2); border-radius: calc(var(--r) + 2px); box-shadow: 0 18px 40px rgba(0,0,0,.45); }
  body[data-theme]:not(.home) header.top.menu-open .acts { display: flex; }
  body[data-theme]:not(.home) header.top .acts .btn { flex: 0 0 auto; width: 100%; min-height: 44px; min-width: 0; justify-content: flex-start; padding: 8px 14px; font-size: 15px; }
  body[data-theme]:not(.home) header.top .acts #rulesBtn { display: inline-flex; }
  /* notices keep their Continue button in view; the story scrolls above it */
  body[data-theme]:not(.home) .notice { padding-bottom: 0; }
  body[data-theme]:not(.home) .stage:has(.notice:not([hidden])) .stage-banner { display: none; }
  body[data-theme]:not(.home) .notice > .n-foot { position: sticky; bottom: 0; z-index: 1; margin: 0 -14px; padding: 8px 14px 12px; background: inherit; }
}

/* touch screens: the logo (a link home) is a full 44px to tap; the board's tools fold behind one button */
@media (max-width: 820px), (max-height: 500px), (pointer: coarse) {
  header.top .logo { min-height: 44px; min-width: 44px; align-items: center; justify-content: center; }
  body[data-theme]:not(.home) .tools-btn { display: inline-grid; place-items: center; position: absolute; right: 10px; bottom: 10px; z-index: 6; width: 44px; height: 44px; min-height: 44px; padding: 0; border-radius: 50%; background: color-mix(in srgb, var(--surface) 88%, transparent); backdrop-filter: blur(6px); }
  body[data-theme]:not(.home) .stage .stage-tools { display: none; }
  body[data-theme]:not(.home) .stage.tools-open .stage-tools { display: flex; flex-direction: column; align-items: stretch; flex-wrap: nowrap; overflow: visible; left: auto; right: 10px; bottom: 62px; gap: 6px; padding: 8px; width: auto; min-width: 180px; background: var(--surface); border: 1px solid var(--line-2); border-radius: calc(var(--r) + 2px); box-shadow: 0 18px 40px rgba(0,0,0,.45); z-index: 8; }
  body[data-theme]:not(.home) .stage.tools-open .stage-tools .btn { min-height: 44px; font-size: 14px; padding: 6px 12px; justify-content: flex-start; }
  body[data-theme]:not(.home) .stage:has(.notice:not([hidden])) .tools-btn { opacity: 0; pointer-events: none; }
}

/* ---------- upright phones and tablets: the board, with a bottom sheet ---------- */
@media (max-width: 820px) and (orientation: portrait) {
  html:has(#game:not([hidden])), body:has(#game:not([hidden])) { overflow: hidden; overscroll-behavior: none; }
  body[data-theme]:not(.home) .game { display: grid; height: calc(100dvh - var(--header) - env(safe-area-inset-top, 0px)); grid-template-columns: minmax(0, 1fr); grid-template-rows: auto auto minmax(0, 1fr) auto auto; grid-template-areas: "strip" "party" "stage" "side" "dock"; }
  body[data-theme]:not(.home) .game > .strip { grid-area: strip; gap: 6px; padding-block: 6px; }
  body[data-theme]:not(.home) .game > .party-bar { grid-area: party; }
  body[data-theme]:not(.home) .game > .main { display: contents; }
  body[data-theme]:not(.home) .game > .main > .stage { grid-area: stage; height: auto; min-height: 0; }
  body[data-theme]:not(.home) .game > .tape { display: none; }
  body[data-theme]:not(.home) .game > .dock { grid-area: dock; position: static; display: flex; }
  body[data-theme]:not(.home) .game > .dock[hidden] { display: none; }
  body[data-theme]:not(.home) .dock .btn { min-height: 52px; }
  body[data-theme]:not(.home) .dock #dockTrade[aria-expanded="true"] { background: var(--fg); color: var(--bg); border-color: var(--fg); }

  /* the sheet: closed, the board has the room; open, it takes the lower half and the board shrinks above it */
  body[data-theme]:not(.home) .game > .main > .side { grid-area: side; display: none; }
  body.sheet-open[data-theme]:not(.home) .game > .main > .side { display: flex; flex-direction: column; gap: 8px; max-height: min(50dvh, 600px); overflow: hidden; padding: 8px 12px 10px; border-left: 0; border-top: 1px solid var(--line-2); box-shadow: 0 -14px 30px rgba(0,0,0,.28); }
  body[data-theme]:not(.home) #side > * { flex: 0 0 auto; min-width: 0; margin-top: 0; margin-bottom: 0; }
  body[data-theme]:not(.home) #side > .side-tabs { order: -1; }
  body[data-theme]:not(.home) #side > .pane-on { flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
  body[data-theme]:not(.home) #side .card.turn:not(.pane-on) { display: none; }
  body[data-theme]:not(.home) #side > #offerCard:not([hidden]) { max-height: 44%; overflow-y: auto; }

  /* the tabs scroll sideways if they must; the chevron closes the sheet */
  body[data-theme]:not(.home) .side-tabs { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; gap: 6px; margin: 0; padding-bottom: 2px; }
  body[data-theme]:not(.home) .side-tabs::-webkit-scrollbar { display: none; }
  body[data-theme]:not(.home) .side-tabs button { flex: 1 0 auto; min-height: 44px; padding: 6px 10px; font-size: 14px; }
  body[data-theme]:not(.home) .side-tabs .st-turn { display: inline-flex; }
  body[data-theme]:not(.home) .side-tabs .st-turn[hidden] { display: none; }
  body[data-theme]:not(.home) .side-tabs .side-close { display: inline-flex; flex: 0 0 44px; width: 44px; padding: 0; }

  /* the turn card in its tab: no step tracker (the hint says it), no roll button (the bar has it) */
  body[data-theme]:not(.home) #side .card.turn { gap: 8px; padding: 12px 14px; }
  body[data-theme]:not(.home) #side .turn .tracker { display: none; }
  body[data-theme]:not(.home) #side .turn h3 { font-size: 24px; }

  /* the stage's own layers */
  body[data-theme]:not(.home) .notice { bottom: 8px; max-height: calc(100% - 16px); }
  body[data-theme]:not(.home) .drink-calls { bottom: 8px; }
}
/* short phones: the players step aside while the sheet is open, so the board keeps some room */
@media (max-width: 820px) and (orientation: portrait) and (max-height: 900px) {
  body.sheet-open[data-theme]:not(.home) .game > .strip { display: none; }
}

/* ---------- phones both ways: compact player chips ---------- */
@media (max-width: 820px) and (orientation: portrait), (orientation: landscape) and (max-height: 500px) {
  body[data-theme]:not(.home) .strip .pchip { min-width: 150px; max-width: 210px; flex: 0 0 auto; padding-inline: 10px; padding-bottom: 6px; gap: 3px; }
  body[data-theme]:not(.home) .strip .pchip .bar, body[data-theme]:not(.home) .strip .pchip .holds { display: none; }
  body[data-theme]:not(.home) .strip .pchip .top-line { font-size: 15px; gap: 6px; }
  body[data-theme]:not(.home) .strip .pchip .cash { font-size: 19px; }
  body[data-theme]:not(.home) .strip .pchip .sub, body[data-theme]:not(.home) .strip .pchip .tally { flex-wrap: nowrap; white-space: nowrap; overflow: hidden; gap: 6px; align-items: center; }
  body[data-theme]:not(.home) .strip .pchip .tally { font-size: 12px; margin: 0; }
  body[data-theme]:not(.home) .strip .pchip .tok-img { width: 116%; height: 116%; }

}

/* ---------- phones on their side: the board left, a column right ---------- */
@media (orientation: landscape) and (max-height: 500px) {
  html:has(#game:not([hidden])), body:has(#game:not([hidden])) { overflow: hidden; overscroll-behavior: none; }
  body[data-theme]:not(.home) { --side-w: clamp(280px, 42vw, 380px); --header: 48px; }
  /* the header sits over the column, not the board */
  body[data-theme]:not(.home):has(#game:not([hidden])) header.top { position: fixed; top: 0; right: 0; left: auto; width: var(--side-w); padding-inline: 10px max(10px, env(safe-area-inset-right, 0px)); gap: 8px; border-left: 1px solid var(--line); }
  body[data-theme]:not(.home):has(#game:not([hidden])) header.top .logo .wordmark { display: none; }
  body[data-theme]:not(.home) .game { display: grid; height: 100dvh; grid-template-columns: minmax(0, 1fr) var(--side-w); grid-template-rows: var(--header) auto auto minmax(0, 1fr) auto; grid-template-areas: "stage ." "stage strip" "stage party" "stage side" "stage dock"; }
  body[data-theme]:not(.home) .game > .strip { grid-area: strip; min-width: 0; border-left: 1px solid var(--line); gap: 6px; padding: 6px 10px; }
  body[data-theme]:not(.home) .game > .party-bar { grid-area: party; border-left: 1px solid var(--line); }
  body[data-theme]:not(.home) .game > .main { display: contents; }
  body[data-theme]:not(.home) .game > .main > .stage { grid-area: stage; height: auto; min-height: 0; }
  body[data-theme]:not(.home) .game > .main > .side { grid-area: side; min-height: 0; display: flex; flex-direction: column; gap: 8px; overflow: hidden; padding: 8px 10px; border-left: 1px solid var(--line); border-top: 0; }
  body[data-theme]:not(.home) #side > * { flex: 0 0 auto; min-width: 0; margin-top: 0; margin-bottom: 0; }
  body[data-theme]:not(.home) #side > .side-tabs { order: -1; }
  body[data-theme]:not(.home) #side > .pane-on { flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
  body[data-theme]:not(.home) .game > .tape { display: none !important; }
  /* the roll button under the column; the tabs (Turn first) are always there, so no Market button */
  body[data-theme]:not(.home) .game > .dock { grid-area: dock; position: static; display: flex; border-left: 1px solid var(--line); padding: 6px 10px max(6px, env(safe-area-inset-bottom, 0px)); }
  body[data-theme]:not(.home) .game > .dock[hidden], body[data-theme]:not(.home) .dock #dockTrade { display: none; }
  body[data-theme]:not(.home) .dock .btn.big { min-height: 48px; font-size: 16px; }
  /* the turn card is the Turn tab */
  body[data-theme]:not(.home) #side .card.turn:not(.pane-on) { display: none; }
  body[data-theme]:not(.home) #side .card.turn { gap: 6px; padding: 10px 12px; }
  body[data-theme]:not(.home) #side .turn .tracker { display: none; }
  body[data-theme]:not(.home) #side .turn h3 { font-size: 20px; }
  body[data-theme]:not(.home) #side .turn .roll-row .btn.big { display: none; }
  body[data-theme]:not(.home) #side .side-tabs { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; gap: 6px; margin: 0; padding-bottom: 2px; }
  body[data-theme]:not(.home) #side .side-tabs::-webkit-scrollbar { display: none; }
  body[data-theme]:not(.home) #side .side-tabs button { flex: 1 0 auto; min-height: 44px; padding: 4px 10px; font-size: 13px; }
  body[data-theme]:not(.home) .side-tabs .st-turn { display: inline-flex; }
  body[data-theme]:not(.home) .side-tabs .st-turn[hidden] { display: none; }
  body[data-theme]:not(.home) .strip .pchip { min-width: 140px; }
  /* the stage's own layers; a news notice puts its picture beside the story */
  body[data-theme]:not(.home) .notice { bottom: 8px; max-height: calc(100% - 16px); }
  body[data-theme]:not(.home) .notice.news { width: calc(100% - 16px); grid-template-columns: minmax(110px, 32%) minmax(0, 1fr); column-gap: 14px; align-items: start; }
  body[data-theme]:not(.home) .notice.news > * { grid-column: 2; }
  body[data-theme]:not(.home) .notice.news > .n-mast, body[data-theme]:not(.home) .notice.news > .n-kick, body[data-theme]:not(.home) .notice.news > .n-foot { grid-column: 1 / -1; }
  body[data-theme]:not(.home) .notice.news > .n-art { grid-column: 1; grid-row: span 3; align-self: start; }
}

/* ---------- home on a phone: both Play buttons above the fold ---------- */
/* a shorter picture, and the buttons straight under the headline (the description follows them) */
@media (max-width: 560px) and (orientation: portrait) {
  body.home .h-shot { aspect-ratio: 16 / 8; }
  body.home .h-card > .hero-cta { order: 1; }
  body.home .h-card > .sub { order: 2; }
  body.home .h-card > #learnBtn { order: 3; }
  body.home .h-card > .facts { order: 4; }
}

/* ---------- dialogs on small and touch screens: the buttons stay in view while the sheet scrolls ---------- */
@media (max-width: 820px), (max-height: 500px), (pointer: coarse) {
  body[data-theme] .overlay .sheet:has(> .row:last-child) { padding-bottom: 0; }
  body[data-theme] .overlay .sheet > .row:last-child { position: sticky; bottom: 0; z-index: 3; margin: 0 -16px; padding: 10px 16px max(14px, env(safe-area-inset-bottom, 0px)); background: inherit; border-top: 1px solid var(--line, rgba(0,0,0,.15)); }
}

/* ---------- home on a phone on its side: the picture beside the card, the buttons under the headline ---------- */
@media (orientation: landscape) and (max-height: 500px) {
  body.home .h-hero { grid-template-columns: minmax(0, .85fr) minmax(0, 1.15fr); align-items: stretch; min-height: 0; padding: 0 max(12px, env(safe-area-inset-right, 0px)) 32px 0; }
  body.home .h-shot { grid-column: 1; grid-row: 1; aspect-ratio: auto; margin: 0; min-height: 100%; }
  body.home .h-card { grid-column: 2; grid-row: 1; margin: 8px 0 0 -24px; padding-inline: 20px; padding-bottom: 16px; gap: 10px; align-content: start; }
  /* each look's card has its own band along the top: keep clear of it */
  body.home[data-theme="classic"] .h-card { padding-top: 22px; }
  body.home[data-theme="modern"] .h-card { padding-top: 30px; }
  body.home[data-theme="party"] .h-card { padding-top: 38px; }
  body.home .h-stamp { display: none; }
  body.home[data-theme] .h-card h1 { font-size: clamp(26px, 4.2vw, 34px); }
  body.home[data-theme] .h-card { gap: 8px; }
  body.home .h-card > .hero-cta { order: 1; flex-wrap: nowrap; width: 100%; margin: 0; }
  body.home .h-card > .hero-cta .btn.big { flex: 1 1 0; width: auto; min-height: 48px; font-size: 16px; padding-inline: 12px; }
  body.home .h-card > .sub { order: 2; font-size: 15px; }
  body.home .h-card > #learnBtn { order: 3; }
  body.home .h-card > .facts { order: 4; }
}

/* ---------- touch screens of every size: 44px targets ---------- */
@media (pointer: coarse), (max-width: 820px) {
  body.home .home-nav a { display: inline-flex; align-items: center; min-height: 44px; }
  body[data-theme]:not(.home) .more summary { display: flex; align-items: center; min-height: 44px; }
  /* the seat count wraps to a second row rather than squeeze under 44px */
  body:not(.home) .room .rm-seg.rm-n { flex-wrap: wrap; }
  body:not(.home) .room .rm-seg.rm-n button { flex: 1 0 44px; min-width: 44px; }
}
/* the table code's label clears the big letters under it */
body:not(.home) .room .rm-code .label { margin-bottom: 8px; }

/* ---------- tablets on their side (desktop layout, touch): Party's house rule is a band, not on the board ---------- */
@media (pointer: coarse) and (min-width: 821px) and (min-height: 501px) {
  body[data-theme]:not(.home) .game { grid-template-rows: auto auto minmax(0, 1fr) auto; grid-template-areas: "strip side" "party side" "stage side" "tape tape"; }
  body[data-theme]:not(.home) .game > .party-bar { grid-area: party; }
}
@media (pointer: coarse) and (min-width: 821px) and (min-height: 501px), (max-width: 820px) and (orientation: portrait), (orientation: landscape) and (max-height: 500px) {
  body[data-theme="party"]:not(.home) .game > .party-bar { position: static; display: flex; flex-wrap: nowrap; align-items: center; gap: 8px; width: auto; padding: 6px 12px; margin: 0; border: 0; border-bottom: var(--pt-line, 2px) solid var(--pt-ink, currentColor); background: var(--pt-paper, var(--bg-2)); pointer-events: auto; overflow: hidden; }
  body[data-theme="party"]:not(.home) .game > .party-bar .hr-sticker { display: flex; align-items: center; gap: 8px; flex: 1 1 auto; min-width: 0; transform: none; box-shadow: none; border-width: 2px; padding: 4px 4px 4px 8px; }
  body[data-theme="party"]:not(.home) .game > .party-bar .hr-tag { flex: 0 0 auto; }
  body[data-theme="party"]:not(.home) .game > .party-bar .hr-text { flex: 1 1 auto; min-width: 0; font-size: 15px; line-height: 1.2; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  body[data-theme="party"]:not(.home) .game > .party-bar .hr-text.muted { font-size: 12px; white-space: normal; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
  body[data-theme="party"]:not(.home) .game > .party-bar .hr-sticker .btn { flex: 0 0 auto; min-height: 44px; }
  body[data-theme="party"]:not(.home) .game > .party-bar .hr-buddy { flex: 0 1 auto; min-width: 0; max-width: 40%; transform: none; box-shadow: none; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
}

/* ---------- toasts in the game sit over the board, never on the buttons ---------- */
@media (max-width: 820px) and (orientation: portrait) {
  body:has(#game:not([hidden])) .toast { bottom: calc(88px + env(safe-area-inset-bottom, 0px)); }
}
@media (orientation: landscape) and (max-height: 500px) {
  body:has(#game:not([hidden])) .toast { left: calc((100vw - var(--side-w)) / 2); bottom: 12px; max-width: calc(100vw - var(--side-w) - 24px); }
}
/* in the narrow landscape column the buddies go under the house rule */
@media (orientation: landscape) and (max-height: 500px) {
  body[data-theme="party"]:not(.home) .game > .party-bar { flex-wrap: wrap; row-gap: 6px; }
  body[data-theme="party"]:not(.home) .game > .party-bar .hr-buddy { max-width: 100%; flex-basis: 100%; }
}
