/*
 * App chrome. The board's own styling lives in @ternary/ui (board.css); this is everything
 * around it.
 *
 * Mobile-first: the board is the page on a phone, with controls below it in thumb reach.
 * Nothing here assumes a hover-capable pointer, since the primary target is touch.
 */

:root {
  --bg: #061722;
  --panel: #0c2434;
  --ink: #cfe8f2;
  --ink-dim: #7fa6b8;
  --accent: #00b8d4;
  /* NOTE: --t-spritesheet / --t-spritesheet-noborder are declared inline in index.html, not
     here. A relative url() inside a custom property resolves against the stylesheet that USES
     the var (board.css, which ships from the ui package), so declaring them in this file
     pointed the board at a path that does not exist. */
  --p1: #f2eddE;
  --p2: #12283c;
  color-scheme: dark;
}

* { box-sizing: border-box; }

/*
 * Full-height column: bar, board, dock.
 *
 * The board is limited by WIDTH on a phone, so the vertical space the top bar gave back cannot
 * make it any bigger — left alone it became a gap under the dock, which reads as the page
 * having run out rather than as breathing room. Instead the dock is pushed to the bottom and
 * the board centres in what is left, so the freed space becomes margin around the board.
 *
 * `100dvh`, not `100vh`: mobile browsers report `vh` against the viewport with the URL bar
 * hidden, which would put the dock under that bar until the user scrolled.
 */
body {
  margin: 0;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 system-ui, -apple-system, Segoe UI, sans-serif;
  /* Respect notches on phones. */
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.topbar {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
  padding: 10px 14px;
  background: var(--panel);
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.brand { font-weight: 650; letter-spacing: 0.2px; }
.spacer { flex: 1; }
.meta { color: var(--ink-dim); font-size: 13px; }

.scores { display: flex; gap: 8px; }
.chip {
  padding: 3px 10px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.06);
  font-size: 13px;
}
.chip b { font-size: 15px; margin-left: 4px; }
.chip-p1 { box-shadow: inset 0 0 0 1px var(--p1); }
.chip-p2 { box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.35); }

/*
 * A swatch of the actual piece colour in each score chip.
 *
 * The starting message says which side you are, but it fades — and "whose octopi are these?"
 * is a question that recurs all game, especially on a phone where both players' pieces are
 * small. This keeps the answer on screen permanently. The dark player's swatch needs its own
 * ring, since #12283c on a dark bar is nearly invisible.
 */
.chip::before {
  content: '';
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-right: 6px;
  vertical-align: -1px;
}
.chip-p1::before { background: var(--p1); }
.chip-p2::before { background: var(--p2); box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.5); }

.field { font-size: 13px; color: var(--ink-dim); display: flex; gap: 6px; align-items: center; }
select {
  background: rgba(255, 255, 255, 0.08);
  color: var(--ink);
  border: 0;
  border-radius: 6px;
  padding: 4px 8px;
  font: inherit;
}

/* --- phase queue ---
 * The closed colour decides which roads can be travelled and changes every time someone
 * scores, yet nothing on the board states it — so it gets its own strip directly above the
 * board rather than a corner of the top bar, where it competed with the score and the
 * difficulty select and read as decoration.
 */
.phasebar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 10px 12px 12px;
}

.phase-label {
  font-size: 13px;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  color: var(--ink-dim);
}

.phase-then {
  font-size: 12px;
  color: var(--ink-dim);
  opacity: 0.75;
  margin-left: 4px;
}

.phase-queue { display: flex; gap: 6px; align-items: center; }

.phase-token {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: inline-block;
  box-shadow: inset 0 -2px 4px rgba(0, 0, 0, 0.35);
}

/* The active token is the CLOSED colour — ringed and much larger, so it never reads as just
   another item in the queue. */
.phase-token.phase-active {
  width: 38px;
  height: 38px;
  box-shadow:
    0 0 0 3px rgba(255, 255, 255, 0.9),
    0 0 16px rgba(255, 255, 255, 0.28),
    inset 0 -3px 6px rgba(0, 0, 0, 0.42);
}

/* Upcoming tokens fade with distance: the next one matters most. */
.phase-queue .phase-token:nth-child(n + 3) { opacity: 0.55; }
.phase-queue .phase-token:nth-child(n + 6) { opacity: 0.3; }

@media (max-width: 520px) {
  .phasebar { gap: 7px; padding: 8px 10px 10px; }
  .phase-token { width: 17px; height: 17px; }
  .phase-token.phase-active { width: 30px; height: 30px; }
}

/*
 * --- the top bar on a phone ---
 *
 * It used to WRAP into three ragged rows and take ~280px of a 780px viewport — more than a
 * third of the screen consumed before the board began, on the one device where board size
 * matters most. The fix is triage rather than shrinking: the bar keeps only what changes
 * during play (the scores, and the two counters that say how near the end is), the title and
 * the win/loss record step aside, and "How to play" becomes a 44px icon so it keeps its place
 * without keeping its width. Difficulty moved into the dock menu (see index.html).
 *
 * `flex-wrap: nowrap` is the assertion: if this row ever stops fitting, it must overflow
 * visibly rather than silently reflowing into the old three-row block.
 */
@media (max-width: 520px) {
  .topbar {
    flex-wrap: nowrap;
    gap: 8px;
    padding: 6px 10px;
    min-height: 52px;
  }
  .brand { display: none; }
  #record { display: none; }
  .chip { padding: 3px 8px; font-size: 12px; }
  .chip b { font-size: 15px; }
  .counters { gap: 8px; font-size: 12px; }
  /* Words out, glyph in: "39 tiles 37 pearls" becomes "39▲ 37○" at a third of the width. */
  .counters .unit { display: none; }
  .count::after { margin-left: 3px; opacity: 0.75; font-size: 11px; }
  .count-tiles::after { content: '▲'; }
  .count-pearls::after { content: '●'; }
  #tutorial-open { min-width: 44px; padding: 0 12px; font-size: 18px; font-weight: 650; }
  #tutorial-open .btn-text { display: none; }
  #tutorial-open .btn-icon { display: inline; }
}

/* The difficulty picker now sits in the dock menu, so it lays out as a row, not a chip. */
.menu-field {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 4px 6px;
  white-space: nowrap;
}
.menu-field select { min-height: 32px; }

.menu-note {
  font-size: 12px;
  color: var(--ink-dim);
  padding: 2px 6px 6px;
  max-width: 230px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  margin-bottom: 4px;
}
.menu-note.warn { color: #ffcf70; }

main {
  max-width: 1100px;
  margin: 0 auto;
  width: 100%;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.board-host { width: 100%; overflow: hidden; }

/* Positioning context for the banner, so it can centre on the BOARD rather than the page. */
.board-wrap { position: relative; }

/*
 * A single, loud announcement laid over the board.
 *
 * Distinct from `.notice`, which is a small grey pill under the board for in-game
 * acknowledgements. This one has a job the pill kept failing at: telling a first-time player
 * that a guided demo exists. It is bigger, it is in the accent colour, and it sits in the
 * middle of where the player is already looking.
 */
.banner {
  position: absolute;
  left: 50%;
  top: 46%;
  transform: translate(-50%, -50%);
  z-index: 15;
  max-width: min(340px, calc(100% - 32px));
  text-align: center;
  padding: 14px 20px;
  border-radius: 16px;
  font-size: 18px;
  font-weight: 600;
  line-height: 1.35;
  color: #062430;
  background: linear-gradient(180deg, #6fe3f6, var(--accent));
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.55), 0 0 0 3px rgba(255, 255, 255, 0.22) inset;
  pointer-events: none;            /* never blocks a tap on the board beneath it */
  animation: bannerIn 260ms cubic-bezier(0.2, 1.3, 0.4, 1);
}
.banner[hidden] { display: none; }
.banner.fade { opacity: 0; transition: opacity 400ms ease; }

@keyframes bannerIn {
  from { opacity: 0; transform: translate(-50%, -38%) scale(0.94); }
  to   { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}

@media (prefers-reduced-motion: reduce) {
  .banner { animation: none; }
  .banner.fade { transition: none; }
}

/*
 * The dock GROWS into the slack and anchors its contents to the bottom edge; the board stays
 * put directly under the phase bar.
 *
 * Growing rather than taking a `margin-top: auto` matters only for one reason: a margin is
 * transparent, so the leftover strip showed the page background and read as a void. Letting
 * the element itself span it means its gradient fills the space.
 *
 * DO NOT centre the board with auto margins or flex-grow. `BoardRenderer.resize()` picks its
 * scale from the host's own `getBoundingClientRect().top`, so anything that makes the host's
 * POSITION depend on its SIZE closes a layout feedback loop — pushing the board down shrinks
 * the height it thinks it has, which shrinks the board, which frees more space to push it down
 * again. Measured: it settled at 283px instead of 390px. The dock's margin is safe because it
 * comes after the board in flow and never moves it.
 */
main > .controls {
  flex: 1 0 auto;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}

.controls {
  position: sticky;
  bottom: 0;
  padding: 12px 14px calc(12px + env(safe-area-inset-bottom));
  /*
   * Transparent at the top so the page background shows through unchanged. Tinting it toward
   * the board's water tone was tried and looked worse: it put a lighter band across the middle
   * of the page and made a seam at the notice. The board's outer edge (rgb 7,33,58) and the
   * page background (rgb 6,23,34) are close enough that a flat field reads as continuous.
   */
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}

.status { margin: 0 0 4px; font-weight: 550; }

/*
 * The gesture hint. Separate from the status line because it says something different in kind:
 * the status says what the game is waiting for, the hint says how to do it with a finger. It
 * appears only in the state it applies to, so it never becomes wallpaper the player tunes out.
 */
.hint {
  margin: 0 0 10px;
  font-size: 13px;
  color: var(--ink-dim);
}
.hint[hidden] { display: none; }

.buttons { display: flex; gap: 8px; flex-wrap: wrap; align-items: stretch; }

button {
  font: inherit;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.10);
  border: 0;
  border-radius: 8px;
  /* 44px minimum touch target. */
  min-height: 44px;
  padding: 0 14px;
  cursor: pointer;
}
button:disabled { cursor: default; }
button.primary { background: var(--accent); color: #04202a; font-weight: 650; }
button.ghost { background: rgba(255, 255, 255, 0.05); }

/*
 * A disabled PRIMARY needs more than the shared 0.4 opacity. Confirm is the brightest thing in
 * the dock, and at 40% the accent fill still read as a live button — so the most prominent
 * control on screen was also the one most likely to be tapped for no effect. Drain the fill
 * instead of dimming it, so "not yet" is legible at a glance.
 */
button:disabled { opacity: 0.45; }
button.primary:disabled {
  opacity: 1;
  background: rgba(255, 255, 255, 0.06);
  color: var(--ink-dim);
  font-weight: 550;
}

/* Press feedback. Touch has no hover, so this is the only confirmation a tap registered. */
button:not(:disabled):active { transform: translateY(1px); filter: brightness(1.1); }

kbd {
  font: 11px/1 ui-monospace, monospace;
  opacity: 0.6;
  margin-left: 6px;
  border: 1px solid currentColor;
  border-radius: 4px;
  padding: 2px 4px;
}
/* Keyboard hints are noise on touch devices. */
@media (pointer: coarse) { kbd { display: none; } }

/* --- tutorial --- */
.tutorial {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(3, 12, 18, 0.8);
  z-index: 50;
  overflow: auto;
}
.tutorial.open { display: block; }
.tutorial-panel {
  max-width: 640px;
  margin: 5vh auto;
  background: var(--panel);
  border-radius: 14px;
  padding: 18px 20px 24px;
}
.tutorial-panel header { display: flex; align-items: center; gap: 12px; }
.tutorial-panel h2 { margin: 0; flex: 1; font-size: 18px; }
.tutorial-panel h3 { margin: 18px 0 4px; font-size: 15px; color: var(--accent); }
.tutorial-panel p { margin: 0; color: var(--ink); }

/* Ownership, where someone looking for it would look. Quiet enough not to compete with the
   rules above it. */
.legal {
  margin-top: 22px;
  padding-top: 12px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}
.legal p { font-size: 12px; color: var(--ink-dim); margin: 0 0 3px; }

/* The board's own styles live in @ternary/ui/src/board.css; the shell imports them. */

/* --- action dock ---
 * The hand tile sits WITH the controls because it is the thing they act on. It was previously
 * not shown anywhere: the only way to see your own tile was to hover a triangle and read the
 * ghost, which made the first half of every turn guesswork.
 */
.dock { display: flex; gap: 14px; align-items: flex-start; }
.dock-main { flex: 1; min-width: 0; }

.hand { display: grid; justify-items: center; gap: 4px; flex: 0 0 auto; }

.hand-tile {
  width: 76px;
  height: 76px;
  background-image: var(--t-spritesheet);
  /* background-size and background-position are set together in refreshHand(), derived from
     this element's rendered width — see the note there. Do not set background-size here. */
  background-repeat: no-repeat;
  border-radius: 10px;
}

.hand-tile.empty { background-image: none; background: rgba(255, 255, 255, 0.06); }

.hand-label { font-size: 11px; color: var(--ink-dim); text-transform: uppercase; letter-spacing: 0.06em; }

.counters { display: flex; gap: 10px; }
.counters b { color: var(--ink); }
.count { white-space: nowrap; }

/* The icon form of a button label; the text form is the default. Swapped on narrow screens. */
.btn-icon { display: none; }

/* Contextual controls: hidden rather than disabled, so the dock only ever offers what applies.
   Eight permanently-visible buttons is the single strongest "debug harness" signal there is. */
.buttons button[hidden], .more[hidden] { display: none; }

.more { position: relative; }
.more summary {
  list-style: none;
  cursor: pointer;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  padding: 0 14px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.05);
}
.more summary::-webkit-details-marker { display: none; }
.more[open] summary { background: rgba(255, 255, 255, 0.12); }
.more-menu {
  position: absolute;
  bottom: calc(100% + 6px);
  right: 0;
  display: grid;
  gap: 6px;
  padding: 8px;
  background: var(--panel);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  z-index: 20;
  min-width: 160px;
}
.more-menu button { width: 100%; justify-content: flex-start; }

/*
 * --- the dock on a phone ---
 *
 * Confirm spans the row and leads, because it is the only button that ADVANCES the game; the
 * rest adjust or undo. A wrapped flex row put Confirm in the middle of a ragged block and
 * stranded More on a line of its own.
 */
@media (max-width: 520px) {
  .dock { gap: 10px; }
  .status { font-size: 16px; }
  .hand-tile { width: 64px; height: 64px; }
  .buttons { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); }
  #confirm { grid-column: 1 / -1; }
  .buttons > button, .more { min-width: 0; }
  .more summary { width: 100%; justify-content: center; padding: 0 8px; }
  .buttons > button { padding: 0 8px; justify-content: center; display: inline-flex; align-items: center; }
  /* Anchor the menu to the dock rather than to its button, so a right-hand
     button cannot push it off-screen. */
  .more { position: static; }
  .more-menu { left: 14px; right: 14px; bottom: calc(100% + 10px); }
}

.chip b.bump { animation: scorePop 420ms ease; display: inline-block; }

/* While a move plays out, nothing on the board is actionable — so nothing should look it.
   Suppressing the hover affordances avoids inviting a click that will be ignored. */
.t-busy .t-face,
.t-busy .t-dest,
.t-busy .t-worker { cursor: default; }
.t-busy .t-face-placeable:hover::after { width: 14px; height: 14px; }

/* --- ephemeral notices ---
 * Acknowledges a selection, and — more importantly — explains a TAP THAT DID NOTHING. Choosing
 * a worker with no legal destination used to be silently ignored, which is indistinguishable
 * from a broken interface. `aria-live` announces it to screen readers too.
 */
.notice {
  min-height: 26px;
  margin: 4px auto 0;
  text-align: center;
  font-size: 14px;
  pointer-events: none;
}

.notice span {
  display: inline-block;
  padding: 5px 13px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.11);
  color: var(--ink);
  animation: noticeIn 160ms ease-out;
}

/* A refusal has to be distinguishable from a confirmation at a glance, without relying on
   reading the sentence. */
.notice span.warn {
  background: rgba(255, 170, 80, 0.18);
  color: #ffd9a8;
  box-shadow: inset 0 0 0 1px rgba(255, 190, 110, 0.55);
}

.notice span.fade { animation: noticeOut 320ms ease-in forwards; }

@keyframes noticeIn  { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; } }
@keyframes noticeOut { to { opacity: 0; transform: translateY(-4px); } }

@media (prefers-reduced-motion: reduce) {
  .notice span, .notice span.fade { animation: none; }
}


/* --- the guided demo ---------------------------------------------------------------------
 *
 * The overlay never takes pointer events. Taps reach the board as normal and the rails gate
 * decides what counts; the scrim's job is only to say where to look. Making it modal would
 * put two independent copies of "what is allowed right now" in the codebase.
 */
.tour {
  position: fixed;
  inset: 0;
  z-index: 60;
  pointer-events: none;
}
.tour[hidden] { display: none; }

/*
 * The lit area. Its own box is the hole; the box-shadow spread paints the rest of the screen
 * dark, which is far cheaper than a clip-path and needs nothing but the target's rect.
 */
.tour-hole {
  position: fixed;
  border-radius: 14px;
  box-shadow:
    0 0 0 3px rgba(120, 230, 255, 0.9),
    0 0 22px 6px rgba(0, 184, 212, 0.55),
    0 0 0 9999px rgba(3, 14, 22, 0.72);
  transition: top 220ms ease, left 220ms ease, width 220ms ease, height 220ms ease;
}
/*
 * SHOW mode — beats with nothing to point at.
 *
 * These used to dim the WHOLE viewport (a 0.72 scrim with a zero-size hole), which put the
 * heaviest shading on precisely the beats worth watching: the opponent's turns, where the
 * board changes on its own and there is no control to point at. The demonstration was hidden
 * behind the thing meant to draw attention to it.
 *
 * So SHOW dims nothing at all. The caption instead docks to the bottom edge, clear of the
 * board, where it does not have to compete with what it is describing.
 */
.tour.tour-show .tour-hole { display: none; }
.tour.tour-show .tour-bubble { top: auto; bottom: 14px; }

.tour-bubble {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  width: min(440px, calc(100vw - 28px));
  background: var(--panel);
  border-radius: 14px;
  padding: 14px 16px 12px;
  box-shadow: 0 10px 34px rgba(0, 0, 0, 0.6);
  pointer-events: auto;      /* the bubble's own buttons must work */
  transition: top 220ms ease;
}
.tour-bubble p { margin: 0 0 10px; font-size: 15px; line-height: 1.45; }

.tour-actions { display: flex; align-items: center; gap: 8px; }
.tour-actions button { min-height: 40px; }
.tour-progress { flex: 1; font-size: 12px; color: var(--ink-dim); }
/* Next is only meaningful on informational beats; the rest advance by being played. */
#tour-next[hidden] { display: none; }

@media (prefers-reduced-motion: reduce) {
  .tour-hole, .tour-bubble { transition: none; }
}


/* --- end-of-game summary ------------------------------------------------------------------
 *
 * Reuses the tutorial overlay's shell. The result gets a headline rather than a line of status
 * text, because winning a game you spent ten minutes on should feel like something happened.
 */
.summary-panel { max-width: 460px; }

.summary-headline {
  margin: 0 0 2px;
  font-size: 30px;
  line-height: 1.15;
  letter-spacing: -0.01em;
}
.summary-headline.won  { color: #7ef0c0; }
.summary-headline.lost { color: #ffb0a0; }
.summary-headline.drew { color: var(--ink); }

.summary-score { margin: 0 0 16px; font-size: 17px; color: var(--ink-dim); }

.stat-group { margin-bottom: 16px; }
.stat-group h3 {
  margin: 0 0 6px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink-dim);
}
.stat-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 5px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}
.stat-row:last-child { border-bottom: 0; }
.stat-row .k { color: var(--ink-dim); }
.stat-row .v { font-weight: 650; font-variant-numeric: tabular-nums; }

/* The one stat that is a compliment rather than a measurement. */
.stat-row.highlight .v { color: var(--accent); }

.summary-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; }

/*
 * While the watch-along is playing.
 *
 * `dispatch` already ignores everything but the driver's own events, so this is only about
 * saying so: without it the board still lights up under the cursor and the buttons still look
 * pressable, which invites taps that do nothing.
 */
body.t-watching .board-host,
body.t-watching .controls { pointer-events: none; }
body.t-watching .controls { opacity: 0.75; }
/* The escape hatch is the one thing that must stay live and obvious. */
body.t-watching #watch-start,
body.t-watching #watch-continue { pointer-events: auto; }
/* The dock's own status line would otherwise peek out above the docked caption and read as a
   second, contradictory instruction. The hand tile stays — the walkthrough points at it. */
body.t-watching #status,
body.t-watching #hint { visibility: hidden; }

/*
 * The standing question above a watch-along caption.
 *
 * `:empty` hides it entirely, which is what keeps the rails demo — where there is no question —
 * from carrying a blank line above every beat.
 */
.tour-question {
  margin: 0 0 6px;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--accent);
}
.tour-question:empty { display: none; }

/*
 * The tile bag, in "How to play".
 *
 * Sprite-sheet slices sized by --bag-cell, which must match BAG_CELL in app.ts — the background
 * size and the offset are computed there from the same number, so changing one alone silently
 * draws a slice of the wrong tile.
 */
.bag-grid {
  list-style: none;
  margin: 10px 0 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
  gap: 10px 6px;
}
.bag-item { display: flex; flex-direction: column; align-items: center; gap: 3px; }
.bag-tile {
  width: 54px;
  height: 54px;
  background-image: var(--t-spritesheet-noborder);
  background-repeat: no-repeat;
}
.bag-count { font-size: 11px; color: var(--ink-dim); letter-spacing: 0.02em; }
