/* PuzzleDuo — play.css
   Three regions on a dark board: tray (left, 26%), board (centre), rail (right,
   220). Pieces are shape-only in the tray and resolve to the photo as they are
   placed, which keeps 300-piece puzzles cheap and reads like a real jigsaw. */

.pf-play {
  display: grid;
  grid-template-columns: minmax(0, 26%) minmax(0, 1fr) 220px;
  height: calc(100vh - 48px);
  min-height: 520px;
  background: var(--stage-900);
}
@media (min-width: 1600px) {
  .pf-play { grid-template-columns: minmax(0, 26%) minmax(0, 1fr) 240px; }
}

/* =============================================================== tray ===== */
.pf-tray {
  --piece: 64px;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--paper-1);
  border-right: 1px solid var(--line-200);
}
.pf-tray__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  height: 44px;
  flex: none;
  padding: 0 12px;
  border-bottom: 1px solid var(--line-200);
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.04em;
  color: var(--ink-500);
  text-transform: uppercase;
}
.pf-tray__count { color: var(--ink-900); }
.pf-tray__body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 6px;
  padding: 10px;
  background: var(--paper-1);
}
.pf-tray__item {
  position: relative;
  /* One number for the box, the grid track and the containment placeholder.
     They used to be three (56px box, 62px pitch, 64px contain-intrinsic-size)
     and content-visibility painted the off-screen pieces at the placeholder
     size: measured at 768 as pieces 14% too large, overlapping their neighbours. */
  width: var(--piece);
  height: var(--piece);
  flex: none;
  border: 0;
  padding: 0;
  background: transparent;
  border-radius: 4px;
  content-visibility: auto;
  contain-intrinsic-size: var(--piece);
  cursor: grab;
  touch-action: none;
}
.pf-tray__item svg { width: 100%; height: 100%; display: block; overflow: visible; }
/* The photo lives under the outline, so the base is the paper that shows if the
   image cannot paint, and `.pf-shape` is stroke-only chrome on top of it. */
.pf-tray__item .pf-shape__base { fill: var(--paper-0); }
.pf-tray__item .pf-shape {
  fill: none;
  /* the tray thumbnails are drawn in board units (~166 per piece) and shown at
     64px, so a 1.2-unit stroke lands at 0.4px and disappears. non-scaling-stroke
     keeps the outline a real hairline at any zoom. */
  stroke: rgba(43, 32, 24, 0.45);
  stroke-width: 1.2;
  vector-effect: non-scaling-stroke;
  transition: fill var(--dur-fast) var(--ease), stroke var(--dur-fast) var(--ease);
}
/* A wash, not an opaque fill: the hover has to say "this one" without hiding the
   photograph that just made the piece identifiable. */
.pf-tray__item:hover .pf-shape { fill: rgba(194, 96, 60, 0.16); stroke: var(--clay-500); }
.pf-tray__item:focus-visible { outline: 2px solid var(--focus-ring); outline-offset: 1px; border-radius: 6px; }
.pf-tray__item[data-hinting="true"] .pf-shape { fill: rgba(194, 96, 60, 0.24); stroke: var(--clay-600); stroke-width: 2; }
.pf-tray__item[data-dragging="true"] { opacity: 0.35; }
.pf-tray__item[hidden] { display: none; }
.pf-tray__empty {
  color: var(--ink-500);
  font-size: var(--fs-small);
  padding: 16px 10px;
  line-height: 1.5;
}

/* ============================================================== board ===== */
.pf-boardwrap {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  padding: var(--s6);
  background: var(--stage-900);
  box-shadow: inset 0 1px 0 var(--stage-hair);
  overflow: hidden;
  touch-action: none;
}
.pf-boardwrap::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background-image:
    radial-gradient(var(--stage-grain) 1px, transparent 1.2px),
    radial-gradient(var(--stage-grain) 1px, transparent 1.2px);
  background-size: 4px 4px, 9px 9px;
  background-position: 0 0, 2px 3px;
}
.pf-board {
  position: relative;
  display: block;
  flex: none;
  border-radius: 2px;
  box-shadow: 0 18px 40px -18px rgba(0, 0, 0, 0.5), 0 2px 6px rgba(0, 0, 0, 0.22);
  background: var(--paper-0);
  overflow: visible;
}
/* The sheet the puzzle is assembled on. `.pf-board` already paints paper-0, but
   an explicit rect keeps the sheet part of the drawing itself: it survives an
   element screenshot, stays inside a viewBox that clips the ghost and the pulse,
   and does not depend on a CSS background for the board to look like paper. */
.pf-board__sheet { fill: var(--paper-0); }
/* The guide picture under the placed/loose layers, shown only in ghost mode. */
.pf-board__under { opacity: 0; transition: opacity var(--dur-slow) var(--ease); }
.pf-board__photo { opacity: 1; transition: opacity var(--dur-slow) var(--ease); }
.pf-board[data-won="true"] .pf-board__photo { opacity: 1; }

/* ---- display modes ---- */
/* "grid" is the default: paper and the grid, nothing else. The photograph used
   to sit under every mode at 26%, which meant the board already held the answer
   and the only skill left was finding the empty hole. */
.pf-board[data-board-mode="ghost"] .pf-board__under { opacity: 0.42; }
.pf-board[data-board-mode="blank"] .pf-board__outline { opacity: 0; }

/* Grid lines were rgba(253,250,244,0.16) — a near-white hairline, chosen when the
   board was going to be a dark table. The board is paper, so the grid was drawn
   white-on-white and nobody could see it.

   Two things had to change together. Ink at 26% is not a visible line: the board
   is a 1000-unit viewBox shown at ~700 px, so a 1-unit stroke covers 0.7 device
   pixels, anti-aliases to about rgb(198,198,193) and disappears. `non-scaling-
   stroke` makes it exactly one CSS pixel at any size (the tray thumbnails use the
   same trick), and 42% ink lands it at rgb(164,160,155) — a cut line you can see
   without it turning the sheet into graph paper. */
.pf-board__outline {
  fill: none;
  stroke: rgba(43, 32, 24, 0.42);
  stroke-width: 1;
  vector-effect: non-scaling-stroke;
  pointer-events: none;
}
/* This one sits on the pieces' own photo, where the local brightness is whatever
   the picture happens to be, so it keeps the light value and a heavy halo. */
.pf-board__placedline {
  fill: none;
  stroke: rgba(253, 250, 244, 0.34);
  stroke-width: 1;
  pointer-events: none;
}
.pf-board[data-won="true"] .pf-board__outline,
.pf-board[data-won="true"] .pf-board__placedline { opacity: 0; transition: opacity 600ms var(--ease); }

.pf-outline-pulse {
  stroke: var(--clay-500) !important;
  stroke-width: 3 !important;
  stroke-opacity: 1 !important;
  animation: pf-pulse 1600ms var(--ease) 2;
}
@keyframes pf-pulse {
  0%, 100% { stroke-opacity: 1; }
  50% { stroke-opacity: 0.25; }
}

.pf-ghost { pointer-events: none; }
.pf-ghost__line { fill: none; stroke: var(--clay-500); stroke-width: 2; }
.pf-ghost__halo { fill: none; stroke: rgba(0, 0, 0, 0.5); stroke-width: 5; }

/* the piece in flight while the pointer is outside the board */
.pf-dragfly {
  position: fixed;
  z-index: 300;
  width: 64px;
  height: 64px;
  pointer-events: none;
  transform: translate(-50%, -50%);
  opacity: 0.9;
}
.pf-dragfly svg { width: 100%; height: 100%; overflow: visible; }
.pf-dragfly .pf-shape__base { fill: var(--paper-0); }
.pf-dragfly .pf-shape { fill: none; stroke: var(--clay-600); stroke-width: 1.5; }

/* The one shared <image> every thumbnail references. Zero-sized and out of flow:
   it exists to be referenced, never to be seen, and it must not join the tray's
   flex/grid layout as a 0×0 item that still consumes a gap. */
.pf-photo-defs { position: absolute; left: 0; top: 0; width: 0; height: 0; overflow: hidden; pointer-events: none; }

/* empty state */
.pf-empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: var(--s6);
  z-index: 5;
}
.pf-empty[hidden] { display: none; }
.pf-empty__card {
  width: min(420px, 100%);
  background: var(--paper-0);
  border-radius: var(--r-panel);
  box-shadow: var(--e3);
  padding: var(--s8) var(--s6);
  text-align: center;
}
.pf-empty__card h1 { font-size: 1.5rem; }
.pf-empty__card p { margin: var(--s3) 0 var(--s6); color: var(--ink-700); font-size: var(--fs-small); }
.pf-empty__actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

/* =============================================================== rail ===== */
.pf-rail-r {
  display: flex;
  flex-direction: column;
  gap: var(--s4);
  padding: var(--s4);
  background: var(--stage-800);
  border-left: 1px solid rgba(0, 0, 0, 0.35);
  color: var(--paper-0);
  min-height: 0;
  overflow-y: auto;
}
.pf-rail-r__label {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--stage-40);
  margin: 0 0 6px;
}
.pf-progress { display: flex; align-items: center; gap: 12px; }
.pf-ring { flex: none; width: 48px; height: 48px; transform: rotate(-90deg); }
.pf-ring__track { fill: none; stroke: rgba(253, 250, 244, 0.14); stroke-width: 4; }
.pf-ring__fill {
  fill: none;
  stroke: var(--clay-500);
  stroke-width: 4;
  stroke-linecap: round;
  transition: stroke-dashoffset 300ms var(--ease);
}
.pf-progress__text { min-width: 0; }
/* Value + caption, not a sentence: caption 11/700 uppercase above the value with
   a 4px gap, value in JetBrains Mono 500 20/1.1 tabular. Nothing wraps at the
   180px content width. */
.pf-progress__num {
  font-family: var(--font-mono);
  font-size: 20px;
  font-weight: 500;
  color: var(--paper-0);
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}
.pf-progress__sub {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--stage-70);
  margin-bottom: 4px;
}

.pf-stat {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--stage-70);
  padding: 6px 0;
  border-bottom: 1px solid rgba(253, 250, 244, 0.08);
}
.pf-stat__cap {
  font-family: var(--font-ui);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--stage-70);
}
.pf-stat strong {
  color: var(--paper-0);
  font-weight: 500;
  font-size: 20px;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}

.pf-rail-r .pf-btn {
  width: 100%;
  height: 44px;
  font-size: var(--fs-small);
  background: var(--paper-0);
  color: var(--ink-900);
  border-color: transparent;
}
.pf-rail-r .pf-btn:hover { background: var(--clay-100); color: var(--ink-900); }
.pf-rail-r .pf-btn--ghoststage {
  background: transparent;
  color: var(--paper-0);
  border: 1px solid rgba(253, 250, 244, 0.22);
}
.pf-rail-r .pf-btn--ghoststage:hover { background: rgba(253, 250, 244, 0.1); color: var(--paper-0); }

/* ---- board display mode: a three-way segmented control ---- */
/* Native radios, so arrow keys, Home/End and screen-reader announcement of
   "2 of 3" are the platform's job rather than a reimplementation of it. The
   input stays focusable and covers the whole label; the span paints the pill. */
.pf-bmode {
  display: flex;
  gap: 2px;
  padding: 2px;
  border-radius: var(--r-btn);
  background: rgba(253, 250, 244, 0.07);
  border: 1px solid rgba(253, 250, 244, 0.14);
}
.pf-bmode__opt { position: relative; flex: 1 1 0; display: block; cursor: pointer; }
.pf-bmode__opt input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}
.pf-bmode__opt span {
  display: grid;
  place-items: center;
  height: 30px;
  padding: 0 6px;
  border-radius: calc(var(--r-btn) - 2px);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.01em;
  color: var(--stage-70);
  white-space: nowrap;
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.pf-bmode__opt:hover span { background: rgba(253, 250, 244, 0.09); color: var(--paper-0); }
.pf-bmode__opt input:checked + span { background: var(--paper-0); color: var(--ink-900); }
.pf-bmode__opt input:focus-visible + span { outline: 2px solid var(--focus-ring); outline-offset: 2px; }

.pf-ref {
  width: 100%;
  aspect-ratio: 1;
  border-radius: var(--r-xs);
  overflow: hidden;
  border: 1px solid rgba(253, 250, 244, 0.18);
  background: var(--paper-2);
  padding: 0;
  cursor: zoom-in;
}
.pf-ref img { width: 100%; height: 100%; object-fit: contain; background: var(--paper-0); }

/* ================================================================ win ===== */
.pf-win {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  z-index: 20;
  background: rgba(42, 33, 27, 0.55);
  padding: var(--s6);
  animation: pf-win-in 260ms var(--ease) both;
}
.pf-win[hidden] { display: none; }
@keyframes pf-win-in {
  from { opacity: 0; }
  to { opacity: 1; }
}
.pf-win__card {
  width: min(440px, 100%);
  background: var(--paper-0);
  border-radius: var(--r-panel);
  box-shadow: var(--e3);
  padding: var(--s8) var(--s6);
  text-align: center;
  animation: pf-win-card 320ms var(--ease) both;
}
@keyframes pf-win-card {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: none; }
}
.pf-win__title { font-family: var(--font-display); font-size: 2rem; line-height: 1.1; letter-spacing: -0.015em; }
.pf-win__body { margin: var(--s3) 0 var(--s6); color: var(--ink-700); }
.pf-win__stats {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--ink-500);
  display: flex;
  gap: 16px;
  justify-content: center;
  margin-bottom: var(--s6);
  flex-wrap: wrap;
}
.pf-win__stats strong { color: var(--ink-900); font-weight: 500; }
.pf-win__actions { display: flex; flex-direction: column; gap: 8px; }

@media (prefers-reduced-motion: reduce) {
  .pf-win, .pf-win__card { animation: none; }
}

/* ======================================================= keyboard cue ===== */
.pf-board__kbd {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.04em;
  color: var(--stage-40);
  white-space: nowrap;
  text-align: center;
  pointer-events: none;
}
.pf-board__kbd kbd {
  background: rgba(253, 250, 244, 0.1);
  border-radius: 3px;
  padding: 1px 5px;
  color: var(--stage-70);
  font-size: 10px;
}

/* ========================================================= responsive ===== */
@media (max-width: 1279px) {
  .pf-play { grid-template-columns: minmax(0, 30%) minmax(0, 1fr); grid-template-rows: 48px minmax(0, 1fr); }
  .pf-rail-r {
    grid-column: 1 / -1;
    grid-row: 1;
    flex-direction: row;
    align-items: center;
    gap: 12px;
    padding: 6px 12px;
    border-left: 0;
    border-bottom: 1px solid rgba(0, 0, 0, 0.35);
    overflow-x: auto;
    overflow-y: hidden;
  }
  .pf-rail-r .pf-rail-r__block { display: flex; align-items: center; gap: 8px; }
  .pf-rail-r__label { display: none; }
  .pf-rail-r .pf-btn { width: auto; padding: 0 12px; }
  .pf-ref { width: 44px; height: 44px; aspect-ratio: auto; flex: none; }
  .pf-stat { border-bottom: 0; padding: 0; }
  .pf-ring { width: 32px; height: 32px; }
  .pf-progress__num { font-size: 13px; }
  .pf-hide-sm { display: none; }
}

@media (max-width: 1023px) {
  /* Phone and tablet: this page is a playing surface, not a document. Measured
     before the fix at 390x844 — the document was 1699px tall against an 844px
     viewport, so scrolling to the footer carried the board and the tray off
     screen mid-game (one drag-scroll and the piece you were holding was gone).
     The surface now fills the viewport under the header and never scrolls; the
     footer stays in the DOM for crawlers but does not sit on this screen. */
  .pf-playbody { height: 100vh; height: 100dvh; overflow: hidden; }
  .pf-playbody .pf-footer { display: none; }
  .pf-play {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr) auto;
    height: calc(100vh - 48px);
    height: calc(100dvh - 48px);
    /* 520px was a desktop floor. On a 390px-tall landscape phone it made the
       surface taller than the viewport, which is the other half of the scroll. */
    min-height: 0;
  }
  .pf-tray {
    grid-row: 3;
    border-right: 0;
    border-top: 1px solid var(--line-200);
  }
  /* Two rows, not one. A single 56px row of pieces in a 169px tray showed six
     of 48 through a peephole, and left 45px of dead paper above and below it. */
  .pf-tray { --piece: 56px; }
  .pf-tray__body {
    display: grid;
    grid-auto-flow: column;
    grid-template-rows: repeat(2, var(--piece));
    grid-auto-columns: var(--piece);
    align-content: start;
    gap: 6px;
    padding: 8px;
    overflow-x: auto;
    overflow-y: hidden;
  }
  .pf-boardwrap { grid-row: 2; padding: var(--s3); }
  .pf-board__kbd { display: none; }
  .pf-rail-r { grid-row: 1; }
}

/* A phone held sideways has ~390px of height for header + rail + tray + board.
   Two rows there would leave the board a letterbox, so this width gets one row
   of smaller pieces instead — still every piece reachable, just fewer in sight. */
@media (max-width: 560px) {
  .pf-play { grid-template-rows: auto minmax(0, 1fr) auto; }
  .pf-empty__card { padding: var(--s6) var(--s4); }
  .pf-win__card { padding: var(--s6) var(--s4); }
}

/* ========================================================= acceptance fixes
   Measured with qa/overflow.mjs: the tray track inflated to 754px on a 390px
   viewport because 1fr floors at min-content and the tray body is nowrap. */
.pf-play { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 220px; }
.pf-play > * { min-width: 0; }
.pf-tray, .pf-tray__head, .pf-tray__body, .pf-rail-r, .pf-boardwrap { min-width: 0; }
.pf-tray__body { min-width: 0; overflow-x: hidden; }
.pf-rail-r .pf-btn--full { width: 100%; max-width: 100%; }

@media (max-width: 1023px) {
  .pf-play { grid-template-columns: minmax(0, 1fr); }
  .pf-tray__body { overflow-x: auto; }
}

/* ============================================================== mobile rail --
   Below 768 the rail is a single 44px bar. Measured problems this fixes:
   · `.pf-rail-r .pf-rail-r__block{display:flex}` (0,2,0) was beating
     `.pf-hide-sm{display:none}` (0,1,0), so TIME and HINTS stayed on screen at
     390 and pushed the action buttons out of the bar;
   · the ring is 32px there, and "0 / 48" at 13px cannot fit inside it;
   · the bar scrolled horizontally, which clips a button with no visible scroll
     affordance. Nothing here is clipped now: the buttons shrink instead. */
@media (max-width: 767px) {
  .pf-rail-r { overflow-x: hidden; gap: 8px; padding: 6px 10px; }
  .pf-rail-r .pf-rail-r__block.pf-hide-sm { display: none; }
  .pf-ring { display: none; }
  .pf-ref { display: none; }
  .pf-progress { gap: 6px; }
  .pf-progress__sub { margin-bottom: 0; }
  .pf-progress__num { font-size: 13px; }
  .pf-rail-r .pf-btn { flex: 1 1 auto; min-width: 0; padding: 0 8px; font-size: 12px; white-space: nowrap; }
  /* The rail is a single 44px bar on a phone and the Board control did not fit:
     measured at 390 it sat at x = 296…418 inside a 390px bar, so overflow-x:
     hidden cut it off — the mode was invisible and untappable, while its three
     radios still counted as 30px targets (audit@390: 3 tap targets < 40px).
     It gets its own row at thumb size instead of being dropped, because the
     phone is where most people actually play. */
  .pf-play { grid-template-rows: auto minmax(0, 1fr) auto; }
  .pf-rail-r { flex-wrap: wrap; row-gap: 6px; }
  .pf-rail-r__block--bmode { flex: 1 0 100%; order: 9; }
  .pf-rail-r__block--bmode .pf-bmode { flex: 1 1 auto; }
  .pf-bmode__opt span { height: 40px; }
  /* the puzzle name gives way to the actions, not the other way round */
  .pf-topbar__name, #puzzle-name { max-width: 12ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
}

/* ========================================================= landscape phones --
   A phone held sideways has ~390px of height for header + rail + tray + board.
   Measured at 844x390 before this block: the tray showed 0 complete pieces and
   the board came out 120x160. Two rows cannot fit here, so this width gets one
   row of 44px pieces, the rail back on a single line (its wrapped second row is
   61px — a third of the board), and no tray head, because the rail already
   prints "PLACED 0 / 48". Rendered last on purpose: these have to beat the
   <=767 rules above, not tie with them. `orientation: landscape` is not
   decoration — a 320x568 portrait phone is also under 600px tall and was the
   first thing this rule broke (one row of 44px pieces on the narrowest phone
   that most needs small ones). */
@media (max-width: 1023px) and (max-height: 620px) and (orientation: landscape) {
  .pf-tray { --piece: 44px; }
  .pf-tray__body { grid-template-rows: var(--piece); }
  .pf-tray__head { display: none; }
  .pf-rail-r { flex-wrap: nowrap; }
  .pf-rail-r__block--bmode { flex: 0 0 auto; order: 0; }
  .pf-bmode__opt span { height: 30px; }
}

/* ================================================= short phones, held upright --
   320x568 measured 156x208 of board with 56px pieces: the rail (109px wrapped)
   and a two-row tray (179px) left the board narrower than three pieces. The
   pieces drop to 44px and the tray head goes; the rail keeps its thumb-sized
   pills, because a 40px control matters more than 20 extra pixels of board.
   700px, not 640: at 375x667 (an iPhone 8, still everywhere) 56px pieces gave a
   230x307 board, 44px gives 281x375. */
@media (max-width: 1023px) and (max-height: 700px) and (orientation: portrait) {
  .pf-tray { --piece: 44px; }
  .pf-tray__body { grid-template-rows: repeat(2, var(--piece)); }
  .pf-tray__head { display: none; }
}
