/* PuzzleFit styling, the same palette Schakelsom runs: a black page, the board
   on a lighter grey rounded panel, one light pill button, and that app's die
   colours — green, sky, amber, orange-red, purple.

   The roles here are the game's own. Sky `--player` is water: a piece the
   inlet already reaches. Green `--in` is the inlet, amber `--out` the outlet.
   Grey `--pipe` is a piece the water has not got to yet, which is the board's
   resting state and says nothing about whether the piece is turned right. */
:root {
  --bg: #0a0a0a;
  --board-panel: #242424;      /* the tray the dice sit on */
  --card: #000000;             /* outlined cards */
  --outline: rgba(255, 255, 255, .92);
  --tile-face: #2f2f2f;        /* the square a piece is drawn on */
  --pipe: #8b8b8b;             /* a dry pipe */
  --player: #5cc8f7;           /* water */
  --in: #4ed35e;
  --out: #efbc4a;
  --accent: #ab8dfa;
  --bad: #ff5722;
  --light: #e3e3e3;            /* the primary button */
  --primary-text: #0a0a0a;     /* ...and the text on it */
  --text: #ffffff;
  --muted: #9b9b9b;
}

/* A PGSDK host can report that it is running light; the die colours are the
   brand and stay put, everything around them inverts. Set by applyDarkMode(). */
body.light {
  --bg: #f1f1f4;
  --board-panel: #dededf;
  --card: #ffffff;
  --outline: rgba(0, 0, 0, .88);
  --tile-face: #c9c9cf;
  --pipe: #4a4a52;
  --light: #14141a;
  --primary-text: #f4f4f6;
  --text: #101014;
  --muted: #5c5c66;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;                       /* the game owns the viewport */
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

/* One screenful: the header and the legend take what they need, the board gets
   the rest. Anything added to .app steals height from the board. */
.app {
  max-width: 460px;
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  padding: max(10px, env(safe-area-inset-top)) 12px calc(12px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;                        /* narrow phones drop the tools to a second line */
}

.tools {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
  margin-left: auto;
}

[hidden] { display: none; }

select {
  background: var(--card);
  color: var(--text);
  border: 2px solid var(--outline);
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 15px;
  font-weight: 700;
}

.clock {
  font-size: 20px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

.legend {
  flex: 0 0 auto;
  margin: 0;
  text-align: center;
  font-size: 13px;
  color: var(--muted);
}

/* There is no pan or zoom here: the grid is small enough that the whole board
   always fits, and fitBoard() picks the square size that makes it so. */
.viewport {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;                          /* let it shrink inside the column */
  overflow: hidden;
  border-radius: 26px;
  background: var(--board-panel);
  display: grid;
  place-items: center;
  padding: 10px;
  touch-action: manipulation;
}

.board {
  display: grid;
  gap: 2px;
  /* --size is written by fitBoard(); the fallback only matters before the first
     paint, when the viewport has no measurable size yet. */
  grid-template-columns: repeat(var(--cols, 1), var(--size, 34px));
  grid-auto-rows: var(--size, 34px);
}

/* Every square carries a piece — the pipe fills the board — so `.square` alone
   is only ever seen if the server sent a board with a hole in it. */
.square { border-radius: 8px; }

.square.piece {
  background: var(--tile-face);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .22);
  cursor: pointer;
  position: relative;
  /* Turning is the whole verb of this game, so it is the one thing that
     animates. The rotation is cumulative, never modulo 4, so a piece turned
     four times spins forward four times instead of snapping back. */
  transition: transform .16s ease-out, background-color .12s linear;
  transform: rotate(calc(var(--turn, 0) * 90deg));
}

.square.piece:active { transform: rotate(calc(var(--turn, 0) * 90deg)) scale(.94); }

/* The pipe itself. One SVG per piece, drawn in its base orientation; the square
   above is what rotates, so the drawing never has to be redone. */
.square.piece svg { display: block; width: 100%; height: 100%; }

.square.piece svg path {
  fill: none;
  stroke: var(--pipe);
  stroke-width: 22;
  stroke-linecap: round;
  stroke-linejoin: round;
  transition: stroke .12s linear;
}

/* Water: a piece the inlet reaches through connected pipe. It is live feedback,
   not a verdict — a wet piece can still be on a branch that goes nowhere. */
.square.piece.wet svg path { stroke: var(--player); }

/* The two ends. They do not turn, so they carry a flat face rather than the
   panel grey, and the disc drawn at their centre marks which is which. */
.square.piece.fixed { cursor: default; }
.square.piece.in { background: color-mix(in srgb, var(--in) 26%, var(--tile-face)); }
.square.piece.out { background: color-mix(in srgb, var(--out) 26%, var(--tile-face)); }
.square.piece.in svg path { stroke: var(--in); }
.square.piece.out svg path { stroke: var(--out); }
.square.piece.in svg circle { fill: var(--in); }
.square.piece.out svg circle { fill: var(--out); }
.square.piece svg circle { fill: var(--pipe); }
.square.piece.wet svg circle { fill: var(--player); }

/* The answer to Controleer: the pieces still open onto nothing, marked for a
   moment and then forgotten. The board carries no standing mark saying a piece
   is right — any move clears this. */
.square.piece.loose { box-shadow: inset 0 0 0 3px var(--bad); }

.square.piece.hinted { box-shadow: inset 0 0 0 3px var(--accent); }

/* Where the arrow keys are. Mouse and touch never move it anywhere the player
   cannot see, so it only really shows up when the board is played by keyboard. */
.square.piece.cursor { box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent); }

.btn {
  background: var(--card);
  color: var(--text);
  border: 2px solid var(--outline);
  border-radius: 999px;
  padding: 16px 20px;
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
}

/* The header row: small enough that three of them cost the board a few pixels. */
.btn.mini {
  padding: 5px 9px;
  font-size: 12px;
  border-width: 1px;
}

.btn:active { transform: scale(.97); }
.btn.primary { background: var(--light); border-color: var(--light); color: var(--primary-text); }
.btn.hidden { display: none; }

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .72);
  display: grid;
  place-items: center;
  padding: 20px;
}

.overlay.hidden { display: none; }

.panel {
  background: var(--card);
  border-radius: 22px;
  border: 2px solid var(--outline);
  padding: 26px 22px;
  text-align: center;
  max-width: 320px;
  width: 100%;
}

.panel h2 { margin: 0 0 8px; font-size: 28px; font-weight: 800; }
.panel p { margin: 0 0 18px; color: var(--muted); font-size: 15px; line-height: 1.5; }
.panel .btn { width: 100%; }
.panel .btn + .btn { margin-top: 10px; }
