/* mobile.css — responsive scaling for phones/tablets + the on-screen touch
   pad built by touch.js. Loaded after style.css/anim.css, so rules here win
   the cascade and act as overrides/additions on top of the desktop layout.
   Uses the shared CSS variables from style.css:1 — no new colors. */

/* ===================================================================
   Touch pad (built in JS as document.body > .sz-touchpad).
   Hidden by default; touch.js toggles its own ".hidden" (existing global
   rule) to show it on touch devices. The pointer/hover media query below
   is a CSS-level safety net so it can never show on a mouse-only device
   even if JS logic changes.
   =================================================================== */
:root { --sz-pad-h: 168px; }

.sz-touchpad {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 150; /* below .modal (200), above game content */
  display: none;
  justify-content: space-between;
  align-items: flex-end;
  gap: 10px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom, 0px));
  padding-left: calc(12px + env(safe-area-inset-left, 0px));
  padding-right: calc(12px + env(safe-area-inset-right, 0px));
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  pointer-events: none; /* only the buttons themselves are interactive */
}
.sz-touchpad.hidden { display: none !important; }
@media (hover: none) and (pointer: coarse) {
  .sz-touchpad:not(.hidden) { display: flex; }
}

.sz-tp-group {
  pointer-events: auto;
  display: grid;
  gap: 8px;
  width: min(46vw, 220px);
}
.sz-tp-left {
  grid-template-columns: 1fr 1fr;
  grid-template-areas: "l r" "s s";
}
.sz-tp-left-btn { grid-area: l; }
.sz-tp-right-btn { grid-area: r; }
.sz-tp-soft { grid-area: s; }

.sz-tp-right {
  grid-template-columns: 62px 1fr;
  grid-template-areas: "hold rotate" "hard rotate";
}
.sz-tp-hold { grid-area: hold; }
.sz-tp-rotate { grid-area: rotate; }
.sz-tp-hard { grid-area: hard; }

.sz-tp-btn {
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  min-height: 56px;
  font: inherit;
  font-weight: 700;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0.92;
}
.sz-tp-move { font-size: 1.7rem; }
.sz-tp-rotate {
  font-size: 2.1rem;
  height: 100%;
  background: linear-gradient(160deg, var(--panel-2), rgba(34, 211, 238, 0.16));
  border-color: var(--accent);
}
.sz-tp-hard { font-size: 0.85rem; letter-spacing: 0.02em; }
.sz-tp-hold { font-size: 0.75rem; letter-spacing: 0.04em; }
.sz-tp-soft { font-size: 0.95rem; }

.sz-tp-btn.pressed,
.sz-tp-btn:active {
  transform: scale(0.94);
  background: #232842;
  border-color: var(--accent);
  opacity: 1;
}

/* ===================================================================
   Board scaling: the canvas has a fixed pixel width/height (10*cellSize x
   20*cellSize) set in JS. Constraining one axis to a relative unit and
   leaving the other "auto" scales it using the canvas's own intrinsic
   pixel ratio — always exactly 1:2, never distorted, on every screen.
   =================================================================== */
.arena-wrap { width: 100%; }
.arena { max-width: 100%; }
.board-wrap { max-width: 100%; }
canvas.board {
  max-width: 100%;
  height: auto;
}

/* Hide the keyboard-only hint on touch devices; the pad replaces it. */
@media (hover: none) and (pointer: coarse) {
  .controls-hint { display: none; }
  #game {
    padding-bottom: calc(var(--sz-pad-h) + env(safe-area-inset-bottom, 0px));
  }
}

/* ===================================================================
   Phones (portrait). Reflow .side into a compact horizontal strip below
   the board (its actual DOM position already), keep both versus boards
   visible side-by-side by shrinking rather than stacking, and use dvh so
   mobile browser chrome doesn't clip the board.
   =================================================================== */
@media (max-width: 760px) {
  body {
    padding: 4px;
    align-items: flex-start;
    min-height: 100vh;
    min-height: 100dvh;
  }
  #app { width: 100%; }

  /* The multi-line keyboard hint doesn't fit this layout and collides with the
     reflowed .side strip. It's already hidden on touch devices (where the pad
     replaces it); hide it at narrow widths generally, so a narrow desktop
     window gets the same clean layout instead of an overlapping one. */
  .controls-hint { display: none; }

  #menu {
    width: min(420px, 96vw);
    padding: 22px 16px;
    margin: 0 auto;
  }

  /* Board hard against the top. Both things that used to sit above it are
     taken OUT of the layout flow: the burger floats over the top-left corner,
     and the player name overlays the board's top edge (those rows are empty
     until you're already losing). Nothing above the board costs height now. */
  .topbar {
    position: fixed;
    top: 4px;
    left: 4px;
    right: 4px;
    max-width: none;
    margin: 0;
    z-index: 120;            /* above the board, below .modal (200) */
    pointer-events: none;    /* only the buttons themselves are clickable */
  }
  .topbar > * { pointer-events: auto; }

  .name-tag {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    z-index: 3;
    margin: 0;
    padding: 1px 4px;
    justify-content: center;
    gap: 5px;
    font-size: 0.62rem;
    line-height: 1.4;
    pointer-events: none;
    /* Fades out downward so it never reads as a solid bar over the playfield. */
    background: linear-gradient(180deg, rgba(11, 13, 23, 0.9), rgba(11, 13, 23, 0));
  }
  .name-tag .badge { font-size: 0.55rem; padding: 0 4px; }
  .topbar button { padding: 6px 10px; font-size: 0.8rem; }
  /* Small and translucent so it reads as an overlay control, not a header.
     It sits in the name-tag band at the top of the board; at this size it
     intrudes less than half a cell into the playfield, and it brightens on
     touch/hover so it's still obviously tappable. */
  .topbar .burger {
    width: 26px;
    height: 22px;
    padding: 0 6px;
    gap: 3px;
    border-radius: 7px;
    background: rgba(18, 21, 39, 0.55);
    backdrop-filter: blur(3px);
    opacity: 0.6;
    transition: opacity 0.15s ease, background 0.15s ease;
  }
  .topbar .burger span { height: 1.5px; }
  .topbar .burger:hover,
  .topbar .burger:active,
  .topbar .burger:focus-visible { opacity: 1; background: var(--panel-2); }
  /* Every pixel here comes straight off the board. */
  .status-bar { margin-top: 4px; font-size: 0.75rem; min-height: 0; }
  #net-stats { margin-top: 4px; }
  #net-stats .chip { font-size: 0.6rem; padding: 2px 6px; }

  /* No height cap here: the board canvas is capped on its own below, and a cap
     on the wrapper only clipped the card so the Next/Hold/Special strip spilled
     out the bottom of it. */
  .arena-wrap {
    display: flex;
    justify-content: center;
  }
  .arena {
    gap: 8px;
    flex-wrap: nowrap;
    width: 100%;
    align-items: stretch;
  }

  /* Board left, a NARROW column of chrome on the right. Stacking Next/Hold/
     stats underneath the board (an earlier version of this file) ate the
     vertical space the board actually wants — the play field is the thing you
     need to see, so everything else is squeezed into a ~48px right column. */
  .player {
    flex-direction: row;
    align-items: stretch;
    padding: 4px;
    gap: 5px;
    flex: 1 1 0;
    min-width: 0;
  }
  .garbage-meter { width: 5px; height: auto; align-self: stretch; order: 0; }

  .board-wrap {
    align-self: center;
    flex: 1 1 auto;
    min-width: 0;
    /* Must stay a COLUMN: .name-tag is a normal-flow child here, so a row
       direction would park it beside the board instead of above it. */
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  .board-col { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; gap: 3px; }

  /* The board must scale UP as well as down. `max-height`/`max-width` alone are
     upper bounds, so the canvas just sat at its intrinsic 300x600 and left dead
     space around it on a roomy phone. A DEFINITE height of
     min(vertical room, 2 x horizontal room) fills whichever axis runs out
     first, and `width: auto` keeps the 1:2 ratio exact either way. */
  .arena {
    --sz-side-w: 48px;
    --sz-board-w: calc(100vw - var(--sz-side-w) - 56px);   /* solo: one board */
    --sz-board-reserve: 74px;              /* under-stats + status + net chips */
  }
  .arena.versus {
    --sz-board-w: calc((100vw - 2 * var(--sz-side-w) - 104px) / 2);
  }
  canvas.board {
    width: auto;
    max-width: 100%;
    height: min(calc(100vh - var(--sz-board-reserve)), calc(var(--sz-board-w) * 2));
    height: min(calc(100dvh - var(--sz-board-reserve)), calc(var(--sz-board-w) * 2));
  }

  /* The right column: everything here is deliberately tiny. */
  .side {
    flex: 0 0 var(--sz-side-w);
    width: var(--sz-side-w);
    min-width: 0;
    flex-direction: column;
    align-items: center;
    gap: 4px;
  }
  .side > div { width: 100%; }
  .side h3 { font-size: 0.42rem; letter-spacing: 1px; margin: 0 0 2px; text-align: center; }
  .side canvas.mini { display: block; margin: 0 auto; }
  .side canvas[data-hold] { width: auto; max-width: 100%; max-height: 24px; }
  .side canvas[data-next] { width: auto; max-width: 100%; max-height: 54px; }
  .slot { padding: 2px; display: flex; justify-content: center; }
  .side .stat { margin: 0; text-align: center; }
  .side .stat .label { font-size: 0.42rem; letter-spacing: 0.5px; }
  .side .stat .value { font-size: 0.7rem; }

  /* Versus keeps Score/Lines under the board — one thin line, not a block. */
  .under-stats { gap: 6px; }
  .under-stats .label { font-size: 0.42rem; letter-spacing: 0.5px; }
  .under-stats .value { font-size: 0.7rem; }

  /* In the reflowed strip the specials panel gets a full-width row of its own
     and goes back to a horizontal bar — here the space is wide, not tall. The
     keyboard hint is dropped since this layout is primarily touch. */
  .side > [data-spec-mount] { width: 100%; min-width: 0; }
  .side .spec-bar { flex-direction: column; align-items: stretch; gap: 3px; padding: 4px; }
  .side .spec-label { font-size: 0.4rem; text-align: center; }
  .side .spec-meter { flex: none; width: 100%; height: 5px; }
  .side .spec-slots { justify-content: center; gap: 3px; }
  .side .spec-hint { display: none; }
  /* The interactive slots are <button>s and inherit the global button padding.
     As flex items their automatic `min-width: auto` floor is min-content, which
     silently overrode the width here and pushed them outside the column — hence
     the explicit padding/min-* reset. The opponent's mirror uses plain divs and
     never hit this. */
  .side .spec-slot {
    width: 18px;
    height: 18px;
    padding: 0;
    min-width: 0;
    min-height: 0;
    flex: 0 0 auto;
  }
  .side .spec-slot .si { font-size: 0.65rem; }
  .side .spec-slot .sk { display: none; }
  .stat { margin-bottom: 0; text-align: center; }
  .stat .label { font-size: 0.58rem; }
  .stat .value { font-size: 1rem; }
  .side h3 { font-size: 0.52rem; margin-bottom: 2px; }

  .name-tag { font-size: 0.85rem; margin-bottom: 4px; }
  .status-bar { margin-top: 8px; font-size: 0.85rem; }
  .specials { margin-top: 8px; }

  .chat {
    left: 8px;
    right: 8px;
    bottom: calc(8px + env(safe-area-inset-bottom, 0px));
    width: auto;
  }
  .chat-log { height: 100px; }
}

@media (hover: none) and (pointer: coarse) and (max-width: 760px) {
  .chat { bottom: calc(var(--sz-pad-h) + 8px + env(safe-area-inset-bottom, 0px)); }
}

/* Extra-tight phones (~360-390px wide). */
@media (max-width: 420px) {
  #menu { padding: 18px 12px; }
  h1 { font-size: 2rem; }
  .arena { gap: 6px; }
  .player { padding: 6px; gap: 4px; }
  .side > div { min-width: 44px; }
  .sz-tp-group { width: min(48vw, 190px); }
  .sz-tp-btn { min-height: 56px; }
  .sz-tp-right { grid-template-columns: 54px 1fr; }
}

/* ===================================================================
   Landscape phones / small tablets: height is the scarce resource, not
   width, so drive the board by height instead and push the pad to the
   screen edges so it doesn't eat vertical space.
   =================================================================== */
@media (max-height: 500px) and (orientation: landscape) {
  body { padding: 6px; align-items: center; min-height: 100vh; min-height: 100dvh; }
  #menu { padding: 14px; max-height: 92vh; overflow-y: auto; }

  .topbar { margin-bottom: 6px; }
  .controls-hint { display: none; }
  .status-bar { margin-top: 4px; min-height: 0; }

  .arena-wrap {
    max-height: calc(100dvh - 70px);
    max-height: calc(100vh - 70px);
  }
  .arena { gap: 10px; flex-wrap: nowrap; align-items: center; }
  .player { padding: 8px; gap: 8px; }
  .side { min-width: 74px; gap: 8px; }

  .board-wrap {
    max-height: calc(100dvh - 90px);
    max-height: calc(100vh - 90px);
  }
  canvas.board {
    height: calc(100dvh - 90px);
    height: calc(100vh - 90px);
    width: auto;
    max-width: 40vw;
    max-height: 100%;
  }

  .sz-touchpad {
    align-items: center;
    padding-top: 6px;
    padding-bottom: 6px;
  }
  .sz-tp-group { width: min(30vw, 180px); }
  .sz-tp-btn { min-height: 48px; }

  #game { padding-bottom: 0 !important; }
  .chat { bottom: 6px; max-height: 40vh; }
  .chat-log { height: 70px; }
}

/* ===================================================================
   Tablets: desktop layout mostly works, just keep it from ever exceeding
   the viewport and give the pad a bit more room.
   =================================================================== */
@media (min-width: 761px) and (max-width: 1100px) {
  .arena { gap: 16px; }
  .sz-tp-group { width: min(30vw, 240px); }
}

/* The touch pad is fixed to the bottom and #game reserves --sz-pad-h for it, so
   that height isn't available to the board. Declared last so it wins over the
   base reserve above. */
@media (hover: none) and (pointer: coarse) {
  .arena { --sz-board-reserve: calc(52px + var(--sz-pad-h)); }
}

/* Three boards abreast need a narrower share of the width than two. */
@media (max-width: 760px) {
  .arena.trio { --sz-board-w: calc((100vw - 3 * var(--sz-side-w) - 92px) / 3); }
  .arena.trio .side { --sz-side-w: 38px; }
  .arena.trio .player { padding: 3px; gap: 3px; }
  .arena.trio .name-tag { font-size: 0.52rem; }
  .arena.trio .under-stats .value { font-size: 0.6rem; }
}
