/* PuzzleFit styling: a black page, the board on a lighter grey rounded panel,
   the tray as a black card with a white outline, one light pill button, and
   that app's die colours — green, sky, amber, orange-red, purple. */
:root {
  --bg: #0a0a0a;
  --board-panel: #242424;      /* the tray the dice sit on */
  --card: #000000;             /* outlined cards below the board */
  --outline: rgba(255, 255, 255, .92);
  --chip-given: #2f2f2f;
  --chip-empty: #6d6d6d;
  --chip-empty-text: #0f0f0f;
  --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;
}

/* 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);
  --chip-given: #c9c9cf;
  --chip-empty: #b4b4bd;
  --chip-empty-text: #23232a;
  --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: header and tray take what they need, the board gets the rest. */
.app {
  max-width: 460px;
  height: 100vh;
  height: 100dvh;
  transition: max-width .2s ease;
  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;
}

/* Big mazes get more room on a desktop; phones are full width either way. */
.app.wide { max-width: 620px; }

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

/* The board is laid out at a fixed square size; the viewport transform below
   is what scales it to fit, so type never has to be recomputed. */
.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);
  touch-action: none;
  overscroll-behavior: contain;
  cursor: grab;
}

.viewport.panning { cursor: grabbing; }

.stage {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  will-change: transform;
}

.stage.animating { transition: transform .22s ease; }

.board {
  display: grid;
  grid-template-columns: repeat(var(--cols, 5), var(--cell));
  gap: var(--cell-gap);
  --cell: 46px;
  --cell-gap: 7px;
  padding: 14px;
}

.cell {
  width: var(--cell);
  height: var(--cell);
  border-radius: 22%;
  display: grid;
  place-items: center;
  font-size: 21px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  user-select: none;
  transition: background .15s ease, box-shadow .15s ease;
}

.cell.len2 { font-size: 17px; }
.cell.len3 { font-size: 13px; }
.cell.len4 { font-size: 10px; letter-spacing: -0.02em; }

.cell.gap { background: none; }

.cell.given {
  background: var(--chip-given);
  color: var(--text);
}

/* The dice in PuzzleFit sit on the panel with a hair of separation. */
.cell.given, .cell.op, .cell.slot { box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .22); }

.cell.op {
  background: var(--op);
  color: #10100e;
  font-size: 24px;
}

.cell.slot {
  background: var(--chip-empty);
  color: var(--chip-empty-text);
  cursor: pointer;
}

.cell.slot.filled {
  background: var(--player);
  color: #052430;
}

.cell.slot.pending { background: #cfcfcf; color: #2a2a2a; }

.cell.slot.selected {
  box-shadow: 0 0 0 3px var(--bg), 0 0 0 6px var(--accent);
  transform: scale(1.04);
}

.cell.bad { box-shadow: inset 0 0 0 3px var(--bad); }

/* Answers to the Check buttons. These are the one place green means "correct"
   rather than "a piece to place", and they fade out again after a moment — the
   board itself never says which squares are right. */
.cell.right { box-shadow: inset 0 0 0 3px var(--tile); }
.cell.wrong { box-shadow: inset 0 0 0 3px var(--bad); }

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

.tray {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  align-content: flex-start;
  flex: 0 0 auto;
  min-height: 56px;
  /* Capped so a 40-tile XXL tray scrolls instead of pushing the board away. */
  max-height: min(30vh, 168px);
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--card);
  border: 2px solid var(--outline);
  border-radius: 18px;
  padding: 14px 12px 12px;    /* room for the count badges */
  scrollbar-width: thin;
  scrollbar-color: var(--outline) transparent;
}

.tray::-webkit-scrollbar { width: 6px; }
.tray::-webkit-scrollbar-thumb { background: var(--outline); border-radius: 3px; }

.tile {
  position: relative;
  min-width: 46px;
  height: 46px;
  padding: 0 10px;
  border-radius: 12px;
  border: none;
  background: var(--tile);
  color: #06280f;
  font-size: 20px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  transition: transform .12s ease, opacity .12s ease;
}

.tile:active { transform: scale(.94); }
.tile.used { opacity: .18; pointer-events: none; }

/* How many of this value are still waiting to be placed. */
.badge {
  position: absolute;
  top: -7px;
  right: -7px;
  min-width: 20px;
  height: 20px;
  padding: 0 5px;
  border-radius: 10px;
  border: 2px solid var(--bg);
  background: var(--accent);
  color: #17082b;
  font-size: 12px;
  font-weight: 700;
  line-height: 16px;
}

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