/* Townsfolk Tussle: the stage layout (docs/UI-PLAN.md, Phone layout and Where each piece of state lives; DESIGN.md,
   Layout). One grid on [data-stage], one rule per view placing its root in its area, and the shared sizes as custom
   properties. A view lays out anything inside its own root and never sets its root's place, size or layer: that is
   here. docs/ui/layout-lead.md has each view's area at each size. app/js/layout.js measures the commit corner into
   --corner-reach and --corner-h and guards the orientation. UI-14, UI-17, UI-28, UI-120. */

:root {
  /* The safe areas: the scene runs edge to edge, the panels at the edges keep their contents inside these. */
  --safe-t: env(safe-area-inset-top, 0px); --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px); --safe-l: env(safe-area-inset-left, 0px);

  /* DESIGN.md's spacing steps. */
  --space-1: 4px; --space-2: 8px; --space-3: 16px; --space-4: 24px; --space-5: 40px;

  /* Desktop and iPad (the sketch at 1600x1000): the Beatin' Order rail on the left, the Fight Phase sideboard on the
     right, the TERRAIN strip and the acting townsfolk's tray along the foot of the board. */
  --rail-w: clamp(140px, 12vw, 200px);     /* the bar's column */
  --side-w: clamp(200px, 17vw, 280px);     /* the ruffian sideboard's column */
  --terrain-h: clamp(64px, 9dvh, 100px);    /* the TERRAIN row's strip */
  --tray-h: clamp(150px, 20dvh, 210px);     /* the tray's row; never less than the commit corner's height */
  /* Phone only (UI-PLAN, Phone layout: rail left 64, tray right 220, the Ruffian's corner at the board's top right). */
  --tray-w: 220px;
  --ruffian-corner-w: 222px; --ruffian-corner-h: 64px;

  /* The commit corner (js/kit/corner.js, fixed at the bottom right by kit.css). layout.js measures it once it is up:
     --corner-reach is how far its left edge sits from the screen's right edge, --corner-h how far its top sits from
     the foot. These defaults are the kit's sizes before it mounts. --corner-over is the part of the corner that
     reaches past the right-hand column into the board's columns; the grid gives it its own column at the foot, so
     the tray ends before it. */
  --corner-reach: 410px;
  --corner-h: calc(var(--commit, 116px) + max(10px, var(--safe-b)));
  --corner-over: max(0px, calc(var(--corner-reach) + var(--space-2) - var(--side-w) - var(--safe-r)));

  /* The prompt row: the top of the board's columns, clear of whatever sits at the board's top right. The prompt line
     centres on --prompt-mid and is at most --prompt-w wide. */
  --prompt-l: calc(var(--rail-w) + var(--safe-l));
  --prompt-r: calc(var(--side-w) + var(--safe-r));
  --prompt-mid: calc((var(--prompt-l) + 100vw - var(--prompt-r)) / 2);
  --prompt-w: calc(100vw - var(--prompt-l) - var(--prompt-r) - 2 * var(--space-3));

  /* Layers on the stage, back to front (DESIGN.md, Depth). kit.css puts the prompt line at 30 and the commit corner
     at 40 inside the prompt view; motion.css's motion layer is 50. */
  --z-board: 0; --z-strip: 5; --z-panel: 10; --z-spotlight: 20; --z-prompt: 30; --z-shell: 60; --z-handover: 65; --z-menu: 70; --z-debug: 80;
}

/* A phone on its side: 932x430 down to 667x320 with Safari's bars, 844x300 at the shortest (CO-47). The same
   breakpoint as kit.css's phone sizes, and layout.js's PHONE. Every height here comes from the visible viewport (dvh,
   container units, or a measure), never 100dvh: Safari's vh is the height with its bars hidden, a quarter more than a
   phone on its side shows while they are up. */
@media (max-height: 500px) {
  :root {
    --rail-w: 64px;
    --tray-w: 236px;
    --terrain-h: 0px; --tray-h: 0px;      /* the TERRAIN row is a sheet; the tray is the right column */
    /* The commit corner has its own strip at the foot of the tray's column (CO-47): two rows of 44 px plates beside
       the commit slot, above the home indicator. The tray ends at the strip and nothing in the corner reaches into the
       board, so no measure is needed here (layout.js leaves these to CSS on a phone). */
    --corner-rows: calc(2 * var(--kit-h, 44px) + var(--space-1));
    --corner-foot: max(var(--space-1), var(--safe-b));
    --corner-h: calc(var(--corner-rows) + var(--corner-foot) + var(--space-2));
    --corner-reach: calc(var(--tray-w) + var(--safe-r));
    --corner-over: 0px;
    /* The prompt plank takes the board's whole width at its top, so a line like "Norman Fishboy's turn" stays on one
       line; the Ruffian's corner stands under it (CO-47: squeezed beside the corner it wrapped to three lines). */
    --prompt-r: calc(var(--tray-w) + var(--safe-r));
    --prompt-mid: calc((var(--prompt-l) + 100vw - var(--prompt-r)) / 2);
    --prompt-w: calc(100vw - var(--prompt-l) - var(--prompt-r) - 2 * var(--space-2));
  }
}

/* ---- the grid
   Columns: the rail, the board, the corner's overreach, the right-hand column (the sideboard, or the tray on a
   phone). Rows: the play area, the TERRAIN strip, the foot (the tray, or on a phone the commit corner's height). */
[data-stage] {
  display: grid;
  grid-template-columns: [edge-l rail] calc(var(--rail-w) + var(--safe-l)) [board] minmax(0, 1fr) [over] var(--corner-over) [side] calc(var(--side-w) + var(--safe-r)) [edge-r];
  grid-template-rows: [top play] minmax(0, 1fr) [strip] var(--terrain-h) [foot] var(--tray-h) [bottom];
}
@media (max-height: 500px) {
  [data-stage] {
    grid-template-columns: [edge-l rail] calc(var(--rail-w) + var(--safe-l)) [board] minmax(0, 1fr) [over] var(--corner-over) [side] calc(var(--tray-w) + var(--safe-r)) [edge-r];
    grid-template-rows: [top play] minmax(0, 1fr) [strip] 0px [foot] var(--corner-h) [bottom];
  }
}

/* Every view's root is a direct child of the stage. It never grows its area, and a hidden one takes no room. */
:where([data-stage] > [data-view]) { position: relative; box-sizing: border-box; min-width: 0; min-height: 0; margin: 0; }
[data-stage] > [data-view][hidden] { display: none !important; }

/* ---- desktop and iPad */
/* The landscape: the scene canvas and its target layer fill the board's columns above the strip. */
[data-view="fight"] { grid-column: board / side; grid-row: play / strip; z-index: var(--z-board); }
/* In town the shop takes the board's place, the strip's row with it, and the sideboard's column: the Ruffian view
   hides outside a fight, so the shop runs to the screen's right edge (CO-44). The prompt line centres on that. */
[data-view="town"] { grid-column: board / edge-r; grid-row: play / foot; z-index: var(--z-board); }
@media (min-height: 501px) {
  [data-stage]:has(> [data-view="town"]:not([hidden])) {
    --prompt-r: var(--safe-r);
    --prompt-mid: calc((var(--prompt-l) + 100vw - var(--prompt-r)) / 2);
    --prompt-w: calc(100vw - var(--prompt-l) - var(--prompt-r) - 2 * var(--space-3));
  }
}
/* The Beatin' Order rail and the Town Map: the left column, top to foot. */
[data-view="bar"] { grid-column: rail / board; grid-row: top / bottom; z-index: var(--z-panel); padding-left: var(--safe-l); }
/* The Fight Phase sideboard: the right column down to the commit corner. */
[data-view="ruffian"] { grid-column: side / edge-r; grid-row: play / foot; z-index: var(--z-panel); padding-right: var(--safe-r); }
/* The TERRAIN row: a strip under the board, its seven slots in a row. */
[data-view="terrain-row"] { grid-column: board / side; grid-row: strip / foot; z-index: var(--z-strip); }
/* One townsfolk's area: the foot of the board's column, ending before the commit corner. Every seat's tray goes
   here; the one not showing is hidden. */
[data-view="tray"] { grid-column: board / over; grid-row: foot / bottom; z-index: var(--z-panel); padding-bottom: var(--safe-b); }
/* The card that is asking: centre stage over the board and the strip. Taps pass through where it shows nothing. */
[data-view="spotlight"] { grid-column: board / side; grid-row: play / foot; z-index: var(--z-spotlight); pointer-events: none; }
/* The prompt row: the top of the board's columns. Its line and the commit corner are kit pieces fixed to the screen
   (kit.css); the any-time tray's opener sits at the row's left end. Taps pass through where it shows nothing. */
[data-view="prompt"] { grid-column: board / side; grid-row: play / strip; align-self: start; z-index: var(--z-prompt); pointer-events: none; }
:is([data-view="spotlight"], [data-view="prompt"]) > * { pointer-events: auto; }

/* The shell screens cover the whole stage, their backdrops edge to edge and their contents inside the safe areas. */
:is([data-view="title"], [data-view="setup"], [data-view="end"], [data-view="handover"], [data-view="menu"]) {
  grid-column: edge-l / edge-r; grid-row: top / bottom; z-index: var(--z-shell);
  padding: var(--safe-t) var(--safe-r) var(--safe-b) var(--safe-l);
}
[data-view="handover"] { z-index: var(--z-handover); }
[data-view="menu"] { z-index: var(--z-menu); }
/* The debug export sheet (js/debug.js) over everything, the menu included. */
[data-view="debug"] { grid-column: edge-l / edge-r; grid-row: top / bottom; z-index: var(--z-debug);
  padding: var(--safe-t) var(--safe-r) var(--safe-b) var(--safe-l); }

/* ---- a phone on its side: layout (c), the rail left, the board framed by the camera in the middle, the acting
   townsfolk's tray on the right with the commit corner at its foot. */
@media (max-height: 500px) {
  /* The board fills the middle top to bottom. The commit corner covers its bottom right, --corner-over wide and
     --corner-h tall: the camera keeps the decision's subject out of that box. */
  [data-view="fight"], [data-view="town"] { grid-column: board / side; grid-row: top / bottom; }
  [data-view="tray"] { grid-column: side / edge-r; grid-row: play / foot; padding: var(--safe-t) var(--safe-r) 0 0; }
  /* The strip under the tray: the column's wood, so the corner's plates stand on the game's art, not the board. */
  [data-stage]::after { content: ""; grid-column: side / edge-r; grid-row: foot / bottom; z-index: var(--z-panel); pointer-events: none;
    background: var(--order-bar-wood) url(../assets/kit/wood.jpg) center / cover; box-shadow: inset 4px 0 0 var(--feat-red); }
  [data-stage]:has(> :is([data-view="title"], [data-view="setup"], [data-view="end"]):not([hidden]))::after { display: none; }
  /* The Ruffian's corner: a small box at the board's top right, under the prompt plank's foot (--prompt-b, measured
     by layout.js). Its sheet is the sideboard. */
  [data-view="ruffian"] { grid-column: board / side; grid-row: play / strip; justify-self: end; align-self: start;
    width: var(--ruffian-corner-w); min-height: var(--ruffian-corner-h); padding: calc(max(var(--safe-t), var(--prompt-b, 48px)) + var(--space-1)) 0 0 0; }
  /* The TERRAIN row is a sheet over the board; the root covers the board and passes taps through. */
  [data-view="terrain-row"] { grid-column: board / side; grid-row: top / bottom; z-index: var(--z-spotlight); pointer-events: none; }
  [data-view="terrain-row"] > * { pointer-events: auto; }
  [data-view="spotlight"] { grid-column: board / side; grid-row: top / bottom; }
  /* The prompt row: the board's whole width at its top. */
  [data-view="prompt"] { grid-column: board / side; padding-top: var(--safe-t); }
}
