/* Catardom on phones. Loaded last, and every rule sits inside a phone-sized media query, so the desktop layout is
   untouched. Two layouts:
   - Phones (portrait, or any screen 700px wide or less): a one-line header, a slim turn panel, and a small resource
     bar and build tray at the bottom, so the board gets the room.
   - Phones held sideways (landscape, 500px tall or less): the board fills the left of the screen; the turn, your
     resources, the actions and the build tray sit in one column on the right. Nothing scrolls the page.
   The game itself (rules, art, animations) is the same everywhere. --hdrh is the header's height, set by ui/app.js. */

/* ---------- Every phone layout ---------- */
@media (max-width: 700px), (orientation: landscape) and (max-height: 500px) {
  body { -webkit-tap-highlight-color: transparent; }
  body:not(.athome):not(.onmenu) { padding-top: max(6px, env(safe-area-inset-top, 0px)); }
  .wrap { gap: 6px; }

  /* Header: one row. The logo, then the players (scroll sideways if they don't fit), then small buttons. */
  body:not(.athome):not(.onmenu) > .wrap > header { flex-wrap: nowrap; align-items: center; gap: 5px; padding: 4px 6px; min-height: 0; }
  body:not(.athome):not(.onmenu) > .wrap > header h1 { font-size: 0; margin: 0; flex: none; }
  body:not(.athome):not(.onmenu) > .wrap > header h1 .logomark { width: 22px; height: 25px; }
  .pstrip { flex: 1 1 auto; flex-wrap: nowrap; overflow-x: auto; gap: 4px; scrollbar-width: none; padding: 2px 1px; }
  .pstrip::-webkit-scrollbar { display: none; }
  .pchip { flex: none; gap: 3px; padding: 1px 6px 1px 2px; font-size: .7rem; }
  .pchip .crest { width: 18px; height: 18px; font-size: .62rem; border-radius: 5px; }
  .pchip .pn { max-width: 58px; }
  .pchip .pp { font-size: .82rem; }
  .pchip .pp small { font-size: .58rem; }
  .pchip .ptag { font-size: .5rem; letter-spacing: .03em; }
  .pchip.you .pn::after { content: ""; }
  .pchip.now { transform: none; }
  body:not(.athome) header .btn, body:not(.athome) header summary.btn, body:not(.athome) nav button { padding: 3px 7px; font-size: .7rem; min-height: 30px; }
  body:not(.athome) .gm-menu svg { width: 13px; height: 13px; }
  body:not(.athome):not(.onmenu) header nav { flex: none; }
  body:not(.athome):not(.onmenu) > .wrap > header #gmenu { position: relative; inset: auto; flex: none; order: 9; }
  body:not(.athome):not(.onmenu) > .wrap > header .gmenu-body { position: fixed; top: calc(var(--hdrh, 46px) + 4px); right: 8px; max-height: calc(100svh - var(--hdrh, 46px) - 16px); overflow-y: auto; }
  .online { flex: none; gap: 4px; }
  .online .netlabel { font-size: .7rem; white-space: nowrap; }
  .online .netlabel small { display: none; }

  /* The turn panel: just whose turn, the clock and the points. */
  .tphead { padding: 5px 10px; gap: 8px; }
  .tphead .crest { width: 26px; height: 26px; font-size: .78rem; }
  .tphead .who strong { font-size: 1.05rem; }
  .tphead .score b { font-size: 1.05rem; }
  .turnpanel .guide { margin: 4px 6px 6px; padding: 5px 8px; }
  .turnpanel .guide h2 { font-size: .9rem; margin: 0; }
  .turnpanel .guide p { display: none; }
  .turnpanel .guide .btnrow { margin-top: 4px; gap: 4px; }
  .turnpanel .guide .btn { padding: 4px 9px; font-size: .74rem; min-height: 30px; }
  .tphead .score .eyebrow, .turnpanel .steps, .turnpanel .rollmeaning, .turnpanel .tpfoot, .turnpanel .dicetray { display: none; } /* the game log has all of it */
  #realmfold { display: none; }

  /* The bottom bar: actions, resources and the build tray, all small. */
  .respanel.underboard { padding: 3px 6px calc(3px + env(safe-area-inset-bottom, 0px)); border-radius: 10px 10px 0 0; }
  .actstrip { margin: 0 auto 3px; padding: 2px 4px 2px 8px; gap: 4px 6px; border-radius: 8px; }
  .actstrip > b { font-size: .76rem; }
  .actstrip .btn { padding: 4px 8px; font-size: .72rem; min-height: 30px; }
  .actstrip [data-act]::before { display: none !important; }
  .dockrow { gap: 4px; }
  .dockres .reshead { display: none; }
  .respanel.underboard .resbar, .dockres .resbar { gap: 2px; }
  .respanel.underboard .resbar li { gap: 3px; padding: 0; justify-content: center; }
  .respanel.underboard .resbar li svg { width: 18px; height: 18px; }
  .respanel.underboard .resbar .rv b { font-size: 1rem; }
  .respanel.underboard .resbar .rv small { display: none; }
  .respanel.underboard .resbar .delta { left: 14px; top: -10px; font-size: .7rem; }
  .respanel.underboard .resbar li.need::after, .respanel.underboard .resbar li.short::after { bottom: -3px; height: 2px; }
  .tray { gap: 4px; padding: 2px 1px; }
  #hand .tbi { flex: 0 0 auto; min-width: 50px; max-width: none; padding: 2px 4px; gap: 0; border-radius: 8px; }
  #hand .tbi svg.px { width: 20px; height: 20px; }
  #hand .tbi b { font-size: .6rem; line-height: 1.1; }
  #hand .tcs { flex-wrap: nowrap; gap: 1px; }
  #hand .tc { font-size: .56rem; }
  #hand .tc svg { width: 10px; height: 10px; }
  #hand .tbi.ok:hover { transform: none; }

  /* Hints and bars over the board stay small. */
  .tip { max-width: 240px; font-size: .74rem; padding: 6px 8px; }
  .tip:has(.topts) > ul { display: none; } /* a bubble with buttons keeps just its title and the buttons */
  .tip .topts { gap: 4px; margin-top: 4px; padding-top: 4px; }
  .tip .topt { gap: 4px 6px; }
  .tip .topt .btn { padding: 4px 9px; font-size: .72rem; min-height: 30px; }
  .tip .tlabel { font-size: .7rem; }
  .tip .thide { font-size: .64rem; }
  .boarddice .bigdie { width: 30px; height: 30px; padding: 4px; }
  .boarddice .bdtotal { min-width: 30px; height: 30px; font-size: 1rem; }
  .boardtools button { width: 30px; height: 30px; min-width: 30px; font-size: .9rem; padding: 0; }
  .confirmbar, .garrisonbar { padding: 6px 8px 6px 10px; gap: 6px 8px; }
  .confirmbar .cftext, .garrisonbar .gbtext { font-size: .8rem; }
  .garrisonbar .gbnote { display: none; }
  .confirmbar .btn, .garrisonbar .btn { min-height: 34px; padding: 5px 12px; }
  .evactive { top: 44px; right: 6px; width: 132px; }
  .evactive .evtabtxt b { overflow-wrap: normal; word-break: normal; font-size: .72rem; }
  .evactive .evtabico { display: block; width: 26px; } .evactive .evtabico .evimg { width: 26px; }

  /* Touch: the board takes every finger (drag to move it, pinch to zoom), and the selection is easy to see. */
  svg#board { touch-action: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
  #board .sel { stroke-width: 10; filter: drop-shadow(0 0 5px rgba(233, 185, 73, .95)); } /* the selected troop or tile */
  #board .atk { stroke-width: 9; }                                                        /* what it can attack */
  #board .enterline { stroke-width: 8; }
}

/* ---------- Phone, upright ---------- */
@media (max-width: 700px) and (orientation: portrait) {
  body:not(.athome):not(.onmenu) { padding-left: max(6px, env(safe-area-inset-left, 0px)); padding-right: max(6px, env(safe-area-inset-right, 0px)); }
  /* Two short rows: the players across the top, the buttons under them. Nothing wider than the phone. */
  body:not(.athome):not(.onmenu) > .wrap > header { flex-wrap: wrap; row-gap: 4px; overflow: hidden; }
  body:not(.athome):not(.onmenu) > .wrap > header .pstrip { flex: 1 1 calc(100% - 40px); order: 1; }
  body:not(.athome):not(.onmenu) > .wrap > header h1 { order: 0; }
  body:not(.athome):not(.onmenu) > .wrap > header > :not(h1):not(.pstrip) { order: 2; }
  body:not(.athome):not(.onmenu) > .wrap > header #gmenu { order: 3; margin-left: auto; }
  body:not(.athome):not(.onmenu) > .wrap > header .online { max-width: 100%; overflow-x: auto; }
  .game { gap: 6px; }
  /* The move buttons: all of them always in view, in an even grid under "Your move". */
  .actstrip { width: 100%; flex-wrap: wrap; justify-content: center; }
  .actstrip > b { flex: 1 1 100%; text-align: center; }
  .actstrip .btnrow { flex: 1 1 100%; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 3px; }
  .actstrip .btnrow .btn { width: 100%; padding: 4px 2px; }
  .boardbox { padding: 2px; border-radius: 8px; }
  /* The board fills the space between the header and the bottom bar, leaving room for the slim turn panel. */
  svg#board { height: max(260px, calc(100svh - var(--dockh, 130px) - var(--hdrh, 60px) - 74px)); max-height: none; }
  aside { gap: 6px; }
}

/* ---------- Phone held sideways ---------- */
@media (orientation: landscape) and (max-height: 500px) {
  /* No page scrolling: the header on top, then the board on the left and one column on the right. */
  /* Only while the board is on screen: the Rules, the Cards and the menu still scroll like any page. */
  html:has(#view-board:not([hidden])), body:not(.athome):not(.onmenu):has(#view-board:not([hidden])) { height: 100%; overflow: hidden; }
  body:not(.athome):not(.onmenu):has(#view-board:not([hidden])) { padding: max(4px, env(safe-area-inset-top, 0px)) max(6px, env(safe-area-inset-right, 0px)) 0 max(6px, env(safe-area-inset-left, 0px)) !important; }
  body:not(.athome):not(.onmenu):has(#view-board:not([hidden])) > .wrap { gap: 4px; height: 100%; grid-template-rows: auto minmax(0, 1fr); max-width: none; }
  body:not(.athome):not(.onmenu) > .wrap { grid-template-columns: minmax(0, 1fr); }
  body:not(.athome):not(.onmenu) > .wrap > header { padding: 2px 4px; min-width: 0; overflow: hidden; }

  #view-board.game:not([hidden]) { --sidew: clamp(230px, 32vw, 300px); display: grid !important; grid-template-columns: minmax(0, 1fr) var(--sidew) !important;
    grid-template-rows: auto auto minmax(0, 1fr) !important; grid-template-areas: "board turn" "board dock" "board info" !important; gap: 4px 6px; min-height: 0; height: 100%; padding-bottom: max(4px, env(safe-area-inset-bottom, 0px)); }
  #view-board .leftcol { display: contents; }
  #view-board .boardbox { grid-area: board; min-height: 0; height: 100%; padding: 2px; }
  #view-board svg#board { height: 100%; max-height: none; }
  #view-board .offerbar { grid-area: board; align-self: start; z-index: 5; margin: 4px; }
  #view-board > aside { display: contents; }
  #view-board .turnpanel { grid-area: turn; }
  #view-board #command { grid-area: info; min-height: 0; overflow-y: auto; align-self: stretch; }
  #view-board #command:empty, #view-board #command[hidden] { display: none; }
  /* Your resources, the actions and the build tray: in the right-hand column, not over the board. */
  #view-board #hand.respanel.underboard { grid-area: dock; position: static; transform: none; width: auto; left: auto; bottom: auto; border-radius: 10px; padding: 4px 5px; box-shadow: none; max-height: 100%; overflow-y: auto; }
  /* The actions: a tidy 2 x 2 grid; the clock is already in the turn panel just above. */
  #view-board #hand .actstrip { width: 100%; display: grid; grid-template-columns: minmax(0, 1fr); padding: 3px; gap: 3px; }
  #view-board #hand .actstrip:has(.btnrow .btn) > b:has(.turntimer) { display: none; }
  #view-board #hand .actstrip > b { text-align: center; }
  #view-board #hand .actstrip .btnrow { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 3px; }
  #view-board #hand .actstrip .btnrow .btn { font-size: .66rem; padding: 4px 2px; }
  #view-board #hand .actstrip .btnrow .btn { width: 100%; padding: 4px 4px; }
  #view-board #hand .actstrip .btnrow .btn:only-child { grid-column: 1 / -1; }
  #view-board #hand .dockrow { flex-direction: column; align-items: stretch; flex-wrap: nowrap; }
  #view-board #hand .dockres { flex: none; }
  #view-board #hand .tray { flex: none; flex-wrap: wrap; overflow: visible; justify-content: flex-start; }
  #view-board #hand .tbi { flex: 1 0 calc(33.3% - 4px); min-width: 0; }
  body:has(#view-board:not([hidden])) { padding-bottom: 0 !important; }
  .confirmbar { left: calc((100vw - var(--sidew, 260px)) / 2); bottom: max(10px, env(safe-area-inset-bottom, 0px)); max-width: calc(100vw - var(--sidew, 260px) - 30px); }
  /* The troops-inside bar sits at the top of the board, clear of the hint bubble at the bottom. */
  .garrisonbar { top: calc(var(--hdrh, 46px) + 6px); bottom: auto; left: max(12px, env(safe-area-inset-left, 0px)); transform: none; max-width: calc(100vw - var(--sidew, 260px) - 150px); }
  .toasts { left: calc((100vw - var(--sidew, 260px)) / 2); bottom: 56px; width: min(420px, calc(100vw - var(--sidew, 260px) - 30px)); }
  .chatbtn, #chatbtn { bottom: 8px; }
  dialog#modal[data-view="trade"], dialog#modal[data-view="compose"], dialog#modal[data-view="respond"] { inset: auto auto 8px 8px; max-height: calc(100svh - 16px); max-width: calc(100vw - var(--sidew, 260px) - 30px); overflow-y: auto; }
}

/* Small phones held sideways (iPhone SE and mini): player chips show the crest and points only; names are on hover
   and in the game log. Header buttons get shorter labels' room. */
@media (orientation: landscape) and (max-height: 500px) and (max-width: 760px) {
  .pchip .pn, .pchip .ptag { display: none; }
  .pchip .pp small { display: none; }
  body:not(.athome) header .btn, body:not(.athome) header summary.btn, body:not(.athome) nav button { padding: 3px 6px; font-size: .66rem; }
  #view-board.game:not([hidden]) { --sidew: 220px; }
}
@media (max-width: 700px), (orientation: landscape) and (max-height: 500px) {
  .pstat { font-size: .62rem; padding: 0 3px 0 1px; } .pstat svg.px { width: 11px; height: 11px; } .pstat i { font-size: .5rem; }
}
