/* The stage layout: where every view sits, at desktop and on an upright phone. One place per view in
   docs/port/plans/ui.json, the shared sizes as custom properties, and nothing inside a view. docs/ui/layout-lead.md
   says what a view may change (anything inside its place) and what it may not (the places themselves).

   Two layouts, from docs/UI-PLAN.md's Phone layout (Layout: areas, Orientation: portrait):
   - table: landscape (desktops, tablets). The sketch's whole-table screen at its own design size, 1500 to 1800 by
     1000 to 1200 px, scaled to the screen by app/js/layout.js. View CSS writes design pixels.
   - areas: portrait (phones, an upright tablet). One area of the board at a time in the board row, a tab per area
     under it, at the screen's own pixels inside the safe areas.
   A phone on its side gets the turn-your-phone screen (app.css). */

:root {
  /* both layouts */
  --gap: 12px;                     /* between docked places, and the board's inner margin */
  --camera-ms: 350ms;              /* an area tab's camera move; UI-120 allows 600 */
  --z-board: 0; --z-bar: 3; --z-tray: 10; --z-sheet: 20; --z-over: 40; --z-shell: 50;

  /* the table: design pixels (docs/UI-PLAN.md, the sketch's desktop screen) */
  --prompt-h: 64px;                /* the prompt row */
  --hand-h: 176px;                 /* the hand row */
  --draft-w: 404px;                /* the draft column */
  --draft-board-w: calc(var(--draft-w) - var(--gap));  /* each draft board in it */
  --draft-board-h: calc(var(--draft-board-w) * 2008 / 3602);
  --decks-h: 130px;                /* the deck row under the two draft boards */
  --side-w: 420px;                 /* the Founder Board column */
  --founder-w: calc(var(--side-w) - var(--gap));
  --founder-h: calc(var(--founder-w) * 1370 / 2067);
  --gearfolk-h: 0px;               /* the Gearfolk's corner, solo only (set below) */
  --sheet-w: 640px;                /* a sheet sliding in from the right */
  --tray-w: 960px;                 /* the widest a tray over the board gets */
  --commit-w: 132px;               /* the commit spot */

  /* shares of the printed art the layout places things on, from source-art/boards */
  --notepad: 7.8% 16.9% 24.4% 78.7%;          /* people board: the Itinerary notepad, x y w h */
  --supply-box: 41% 70.8% 59% 29.2%;          /* Founder Board: the district supply, x y w h */
}

/* -------------------------------------------------------------------------------------------------------------
   The table (landscape) */

@media not (orientation: portrait) {
  [data-stage] {
    inset: auto; left: var(--stage-x, 0px); top: var(--stage-y, 0px);
    width: var(--stage-w, 100vw); height: var(--stage-h, 100vh);
    transform: scale(var(--stage-scale, 1)); transform-origin: 0 0;
    background: var(--felt);                       /* the table (UI-112) */
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto auto;
    grid-template-rows: var(--prompt-h) minmax(0, 1fr) var(--hand-h);
    grid-template-areas:
      "day    seats  bar    commit"
      "board  board  board  board"
      "hand   hand   hand   hand";
  }
  [data-stage]:has(> [data-view="gearfolk"]:not([hidden])) { --gearfolk-h: 250px; }

  /* the prompt row's band, behind the views that sit in it */
  [data-row="prompt"] { grid-area: 1 / 1 / 2 / -1; background: var(--band);
    clip-path: polygon(0 0, 100% 0, 100% 92%, 82% 100%, 61% 94%, 37% 100%, 14% 95%, 0 100%); }

  [data-tabs], [data-row="supply"] { display: none; }

  /* the board: draft boards, the district in focus, the Founder Board and what's housed beside it */
  [data-board-frame] { grid-area: board; }
  [data-board] {
    position: absolute; inset: 0; padding: var(--gap) var(--gap) 0 var(--gap);
    display: grid; gap: var(--gap);
    grid-template-columns: calc(var(--draft-w) - var(--gap)) minmax(0, 1fr) calc(var(--side-w) - var(--gap));
    grid-template-rows: var(--founder-h) minmax(0, 1fr) var(--gearfolk-h);
    grid-template-areas:
      "draft  district  founder"
      "draft  district  tableau"
      "draft  district  gearfolk";
  }

  /* the draft column: the Buildings board, the Townsfolk and Tools board, the deck row, centred */
  [data-view="draft"] { grid-area: draft; display: flex; flex-direction: column; justify-content: center;
    align-items: center; gap: var(--gap); min-height: 0; }
  [data-view="draft"] > [data-area="buildings"],
  [data-view="draft"] > [data-area="people"] { position: relative; flex: none; width: var(--draft-board-w); height: var(--draft-board-h); }
  [data-view="draft"] > [data-part="decks"] { flex: none; width: var(--draft-board-w); height: var(--decks-h); }

  /* the Itinerary card on the people board's notepad: the column's centre, moved to the notepad by the sizes above */
  [data-view="itinerary"][data-part="pad"] {
    grid-area: draft; align-self: center; justify-self: center; position: relative; z-index: 1;
    width: calc(var(--draft-board-w) * .244); height: calc(var(--draft-board-h) * .787);
    left: calc(var(--draft-board-w) * (.078 + .122) - var(--draft-board-w) / 2);
    top: calc(var(--draft-board-h) * (1 + .169 + .3935) + var(--gap) - (var(--draft-board-h) * 2 + var(--decks-h) + var(--gap) * 2) / 2);
  }

  /* the district in focus fills the middle column as a square; the others wait under it (a seat chip shows one) */
  [data-view="district"] { grid-area: district; position: relative; min-width: 0; min-height: 0; container-type: size; }
  [data-view="district"] > [data-area^="district-"] {
    position: absolute; inset: 0; margin: auto; width: min(100cqw, 100cqh); height: min(100cqw, 100cqh); visibility: hidden;
  }
  [data-board]:not([data-focus]) [data-area="district-0"],
  [data-board][data-focus="district-0"] [data-area="district-0"],
  [data-board][data-focus="district-1"] [data-area="district-1"],
  [data-board][data-focus="district-2"] [data-area="district-2"],
  [data-board][data-focus="district-3"] [data-area="district-3"],
  [data-board][data-focus="district-4"] [data-area="district-4"] { visibility: visible; }

  /* the Founder Board at the top of its column, the housed Townsfolk and Vibes under it */
  [data-view="founder"] { grid-area: founder; position: relative; }
  [data-view="tableau"] { grid-area: tableau; position: relative; min-height: 0; }

  /* the Founder Board's district supply: on the board's cut-out at its foot, over the board column */
  [data-view="founder"][data-part="supply"] {
    grid-area: board; align-self: start; justify-self: end; position: relative; z-index: var(--z-bar);
    margin-top: calc(var(--gap) + var(--founder-h) * .708); margin-right: var(--gap);
    width: calc(var(--founder-w) * .59); height: calc(var(--founder-h) * .292);
  }

  /* the Gearfolk's card, deck and used pile: the board's bottom right corner, in solo */
  [data-view="gearfolk"] { grid-area: board; align-self: end; justify-self: end; position: relative; z-index: var(--z-bar);
    width: calc(var(--side-w) - var(--gap) * 2); height: var(--gearfolk-h); margin: 0 var(--gap) var(--gap) 0; }

  /* the prompt row */
  [data-view="itinerary"] { grid-area: day; align-self: center; z-index: var(--z-bar); }
  [data-view="seats"] { grid-area: seats; align-self: center; min-width: 0; z-index: var(--z-bar); }
  [data-view="menu"][data-part="bar"] { grid-area: bar; align-self: center; z-index: var(--z-bar); }
  [data-commit-spot] { grid-area: commit; align-self: center; min-width: var(--commit-w); min-height: var(--tap);
    margin-right: var(--gap); z-index: var(--z-bar); }

  /* the hand row */
  [data-view="hand"] { grid-area: hand; position: relative; min-width: 0; z-index: var(--z-bar); }

  /* trays over the board: the asking card and its options, and cards dealt to choose among */
  [data-view="ask"], [data-view="tray"] { grid-area: board; align-self: end; justify-self: center; position: relative;
    max-width: min(var(--tray-w), 100%); max-height: 100%; margin-bottom: var(--gap); z-index: var(--z-tray); }

  /* sheets slide in from the right over the board and the hand */
  [data-view="pool"], [data-view="score"], [data-stage] > [data-sheet] {
    grid-area: 2 / 1 / 4 / -1; justify-self: end; align-self: stretch; width: min(var(--sheet-w), 100%); min-height: 0; position: relative;
    z-index: var(--z-sheet);
  }
}

/* -------------------------------------------------------------------------------------------------------------
   Areas (portrait): the prompt row, one area of the board, the tab row, the supply row, the hand row */

@media (orientation: portrait) {
  :root {
    --prompt-h: 44px;
    --tabs-h: 52px;
    --supply-h: 44px;
    /* the hand row takes what the district (a square the phone's width, plus its labels) leaves, 75 to 200 px */
    --hand-h: clamp(75px, calc(100dvh - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px) - 140px - 100vw - 40px), 200px);
    --commit-w: 104px;
    --tray-w: 100%;
  }
  [data-stage] {
    background: var(--felt);                       /* the table (UI-112) */
    padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    grid-template-rows: var(--prompt-h) minmax(0, 1fr) var(--tabs-h) var(--supply-h) var(--hand-h);
    grid-template-areas:
      "day    seats  bar"
      "board  board  board"
      "tabs   tabs   tabs"
      "supply supply commit"
      "hand   hand   hand";
  }
  [data-row="prompt"] { grid-area: 1 / 1 / 2 / -1; background: var(--band); }
  [data-row="supply"] { grid-area: supply / supply / commit / commit; background: var(--fboard); }

  /* the board row: every area side by side in one strip, the area's width each; the camera slides the strip */
  [data-board-frame] { grid-area: board; container-type: size; background: var(--frame); }
  [data-board] {
    position: absolute; left: 0; top: 0; height: 100cqh;
    display: grid; grid-auto-flow: column;
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-columns: [d0] var(--col-d0, 100cqw) [d1] var(--col-d1, 0px) [d2] var(--col-d2, 0px) [d3] var(--col-d3, 0px)
      [d4] var(--col-d4, 0px) [buildings] 100cqw [people] 100cqw [founder] 100cqw;
    transition: transform var(--camera-ms) cubic-bezier(.2, .8, .2, 1);
  }
  [data-board]:has([data-area="district-1"]) { --col-d1: 100cqw; }
  [data-board]:has([data-area="district-2"]) { --col-d2: 100cqw; }
  [data-board]:has([data-area="district-3"]) { --col-d3: 100cqw; }
  [data-board]:has([data-area="district-4"]) { --col-d4: 100cqw; }
  [data-board][data-still] { transition: none; }

  /* the district and draft views hand their areas to the strip; anything else they hold waits off the phone */
  [data-view="district"], [data-view="draft"] { display: contents; }
  [data-view="district"] > :not([data-area]), [data-view="draft"] > :not([data-area]) { display: none; }
  [data-area="district-0"] { grid-area: 1 / d0 / 3 / d1; }
  [data-area="district-1"] { grid-area: 1 / d1 / 3 / d2; }
  [data-area="district-2"] { grid-area: 1 / d2 / 3 / d3; }
  [data-area="district-3"] { grid-area: 1 / d3 / 3 / d4; }
  [data-area="district-4"] { grid-area: 1 / d4 / 3 / buildings; }
  [data-area="buildings"] { grid-area: 1 / buildings / 3 / people; }
  [data-area="people"] { grid-area: 1 / people / 3 / founder; }
  [data-area] { position: relative; min-width: 0; min-height: 0; overflow: clip; }

  /* the founder area: the Founder Board, and under it what's housed */
  [data-view="founder"] { grid-area: 1 / founder / 2 / span 1; position: relative; }
  [data-view="tableau"] { grid-area: 2 / founder / 3 / span 1; position: relative; min-height: 0; }

  /* the Itinerary pad is off the people area's frame and opens from the sun */
  [data-view="itinerary"][data-part="pad"] { display: none; }

  /* the prompt row */
  [data-view="itinerary"] { grid-area: day; align-self: center; z-index: var(--z-bar); }
  [data-view="seats"] { grid-area: seats; align-self: center; min-width: 0; z-index: var(--z-bar); }
  [data-view="menu"][data-part="bar"] { grid-area: bar; align-self: center; z-index: var(--z-bar); }

  /* the tab row */
  [data-tabs] { grid-area: tabs; display: flex; z-index: var(--z-bar); }

  /* the supply row: your supply and score, and the commit spot at its right end (thumb right) */
  [data-view="founder"][data-part="supply"] { grid-area: supply; min-width: 0; align-self: stretch; z-index: var(--z-bar); }
  [data-commit-spot] { grid-area: commit; width: var(--commit-w); min-height: var(--tap); align-self: center; z-index: var(--z-bar); }

  /* the hand row */
  [data-view="hand"] { grid-area: hand; position: relative; min-width: 0; z-index: var(--z-bar); }

  /* trays over the board area; the Gearfolk's card plays in one at its top */
  [data-view="ask"], [data-view="tray"] { grid-area: board; align-self: end; position: relative; max-width: 100%; max-height: 100%; z-index: var(--z-tray); }
  [data-view="gearfolk"] { grid-area: board; align-self: start; position: relative; max-width: 100%; max-height: 100%; z-index: var(--z-tray); }

  /* sheets slide up over everything under the prompt row */
  [data-view="pool"], [data-view="score"], [data-stage] > [data-sheet] {
    grid-area: 2 / 1 / -1 / -1; align-self: stretch; min-height: 0; position: relative; z-index: var(--z-sheet);
  }
}

/* -------------------------------------------------------------------------------------------------------------
   Both: the screens that cover the whole stage, safe areas included */

[data-view="title"], [data-view="setup"], [data-view="menu"], [data-view="score"][data-part="end"],
[data-view="seats"][data-part="handover"] {
  grid-area: auto; position: absolute; inset: 0; z-index: var(--z-shell);
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
}
[data-view="seats"][data-part="handover"] { z-index: var(--z-over); }
[data-view="menu"][data-part="bar"] { position: relative; inset: auto; padding: 0; }
@media not (orientation: portrait) {
  /* the table's stage is already inside the safe areas (layout.js fits it there) */
  [data-view="title"], [data-view="setup"], [data-view="menu"], [data-view="score"][data-part="end"],
  [data-view="seats"][data-part="handover"] { padding: 0; }
}

/* the area tabs (UI-120): one per area, in area order; a lit tab carries the count of targets waiting there */
[data-tabs] { gap: 3px; padding: 4px; background: var(--fboard); }
[data-area-tab] { position: relative; flex: 1 1 0; min-width: var(--tap); min-height: var(--tap); padding: 0; border: 0;
  border-radius: 8px; overflow: hidden; background: var(--res-tray); color: var(--paper); cursor: pointer;
  outline: 2px solid transparent; outline-offset: -2px; font: 800 11px/1 var(--body); }
[data-area-tab][aria-current="true"] { outline: 3px solid var(--orange); outline-offset: -3px; transform: translateY(-3px); box-shadow: 0 4px 8px #0008; }
[data-area-tab][aria-current="true"] span { background: var(--orange); }
[data-area-tab] img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
[data-area-tab] span { position: absolute; left: 0; right: 0; bottom: 0; padding: 2px 2px 2px; background: #2c2c2cd9;
  text-align: center; font-size: 10px; line-height: 1.05; display: -webkit-box; -webkit-line-clamp: 2;
  -webkit-box-orient: vertical; overflow: hidden; }
[data-area-tab] [data-count] { position: absolute; right: 2px; top: 2px; min-width: 20px; height: 20px; padding: 0 4px;
  border-radius: 10px; background: var(--orange); color: var(--paper); font: 700 14px/20px var(--vp); text-align: center; }
[data-area-tab]:not([data-lit]) [data-count] { display: none; }

@media (prefers-reduced-motion: reduce) {
  :root { --camera-ms: 150ms; }
}
