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

   The tiles are the exception and they are deliberate. 2048's tile colours are
   the game: a player reads 128 off the shade before they read the number, and
   that association is thirty seconds old for anyone who has ever played it.
   They live in --t2 … --t2048 below and are the one set of colours here that
   are not PuzzleFit's, so the roles the brand does own stay clear — amber
   `--op` for the target, sky `--player` for the score, green `--tile` for a
   tile that just landed, orange-red `--bad` for a board with no move left.
   Green still never means "correct": here it means "new", the same way it means
   "a piece to place" in schakelsom. */
:root {
  --bg: #0a0a0a;
  --board-panel: #242424;      /* the tray the dice sit on */
  --card: #000000;             /* outlined cards */
  --outline: rgba(255, 255, 255, .92);
  --hole: rgba(255, 255, 255, .07);   /* an empty square */
  --op: #efbc4a;
  --player: #5cc8f7;
  --tile: #4ed35e;
  --accent: #ab8dfa;
  --bad: #ff5722;
  --light: #e3e3e3;            /* the primary button */
  --primary-text: #0a0a0a;     /* ...and the text on it */
  --text: #ffffff;
  --muted: #9b9b9b;

  /* The tiles. Dark ink on the small ones, light on the big ones, exactly as
     the original does — it is what makes the board readable at a glance. */
  --tile-ink: #0a0a0a;
  --tile-ink-high: #f7f4ef;
  --t2: #eee4da;
  --t4: #ede0c8;
  --t8: #f2b179;
  --t16: #f59563;
  --t32: #f67c5f;
  --t64: #f65e3b;
  --t128: #edcf72;
  --t256: #edcc61;
  --t512: #edc850;
  --t1024: #edc53f;
  --t2048: #edc22e;
  --tbig: #3c3a32;
}

/* A PGSDK host can report that it is running light; the tile colours and 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);
  --hole: rgba(0, 0, 0, .10);
  --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 takes what it needs, the board gets the rest. */
.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: 12px;
}

.bar {
  display: flex;
  align-items: center;
  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);
}

.goal, .score, .moves {
  font-size: 13px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  border-radius: 999px;
  padding: 2px 10px;
}

/* The tile to reach. Amber, because that is the colour a 2048 board runs to. */
.goal { color: var(--primary-text); background: var(--op); }

/* The 2048 score: what the merges have been worth. */
.score { color: var(--primary-text); background: var(--player); }

/* The move count, which is what the best is kept in — the clock is company. */
.moves { color: var(--text); border: 1px solid var(--outline); }

/* The board is laid out at whatever square size fits; there is no pan and no
   zoom, because every grid this game ships fits on a phone with room over. */
.viewport {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;                          /* let it shrink inside the column */
  overflow: hidden;
  display: grid;
  place-items: center;
  border-radius: 26px;
  background: var(--board-panel);
  touch-action: none;                     /* the swipe is a move, never a scroll */
  overscroll-behavior: contain;
}

.stage { position: relative; }

/* Both layers are laid out in the geometry game.js publishes as --cell and
   --gap, and cellAt()-style arithmetic in the module undoes exactly this — so
   the squares and the tiles can never disagree about where a square is. */
.board {
  display: grid;
  grid-template-columns: repeat(var(--cols, 4), var(--cell));
  gap: var(--gap);
  padding: var(--gap);
  background: rgba(0, 0, 0, .28);
  border-radius: calc(var(--gap) * 2);
}

.hole {
  width: var(--cell);
  height: var(--cell);
  border-radius: calc(var(--gap) * 1.2);
  background: var(--hole);
}

.tiles {
  position: absolute;
  top: var(--gap);
  left: var(--gap);
  pointer-events: none;                   /* the viewport handles every gesture */
}

.tile {
  position: absolute;
  width: var(--cell);
  height: var(--cell);
  display: grid;
  place-items: center;
  border-radius: calc(var(--gap) * 1.2);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--tile-ink);
  background: var(--t2);
  /* The slide. Nothing else on the tile animates position, so a tile keeps its
     identity across a move and the board reads as tiles moving rather than as a
     grid being redrawn. */
  transition: transform .11s ease-in-out;
  will-change: transform;
}

/* The type shrinks as the number gets longer, so four digits still fit. */
.tile { font-size: calc(var(--cell) * .42); }
.tile.wide3 { font-size: calc(var(--cell) * .34); }
.tile.wide4 { font-size: calc(var(--cell) * .27); }

.tile.v4 { background: var(--t4); }
.tile.v8 { background: var(--t8); }
.tile.v16 { background: var(--t16); }
.tile.v32 { background: var(--t32); }
.tile.v64 { background: var(--t64); }
.tile.v128 { background: var(--t128); }
.tile.v256 { background: var(--t256); }
.tile.v512 { background: var(--t512); }
.tile.v1024 { background: var(--t1024); }
.tile.v2048 { background: var(--t2048); }
.tile.vbig { background: var(--tbig); color: var(--tile-ink-high); }

/* A tile that has just appeared, and one that has just been made by a merge.
   Both are the same idea — say what changed — and both are one animation that
   does not touch `transform`'s transition above, since that is the slide. */
.tile.fresh { animation: appear .14s ease-out; }
.tile.merged { animation: pop .16s ease-out; z-index: 2; }

@keyframes appear {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes pop {
  0% { box-shadow: 0 0 0 0 var(--tile); }
  55% { box-shadow: 0 0 0 5px var(--tile); }
  100% { box-shadow: 0 0 0 0 var(--tile); }
}

/* The tile that wins the board, once it is on it. */
.tile.goal { box-shadow: 0 0 0 3px var(--op); }

/* A board with no move left on it. */
.stage.stuck .tile { filter: saturate(.35); }
.stage.stuck .board { box-shadow: 0 0 0 3px var(--bad); }

.stage.shake { animation: shake .3s; }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}

.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; }
