:root {
  --bg: #0b0d12;
  --panel: #171a21;
  --panel-2: #12151b;
  --border: #2a2f3a;
  --text: #e8e9ec;
  --muted: #9a9fa8;
  --cyan: #00b7c3;
  --magenta: #e6007e;
  --yellow: #ffd400;
  --key: #f2f2f2;
  --accent: #4f8cff;
  --good: #35c470;
  --bad: #ff5470;
  /* Museum frame around the mosaic. A gradient, not a flat colour, so the bevel
     reads as gilded wood rather than a yellow outline. */
  --frame: linear-gradient(155deg, #e8cd82 0%, #b8912f 22%, #6d551c 52%, #c7a343 78%, #8a6a25 100%);
  /* Stage rails. Left is a fixed strip of brand + counters; right holds the target
     and the shuffled layers, so it needs room for a couple of tile columns. */
  --rail-left-w: clamp(116px, 13vw, 176px);
  --rail-right-w: clamp(150px, 19vw, 264px);
  /* What the rails widen to once the mosaic's real width is known. Bounded: handing
     a height-bound board's leftover width entirely to the right rail blew the target
     preview up to nearly the size of the board itself. */
  --rail-left-max: clamp(130px, 16vw, 280px);
  --rail-right-max: clamp(170px, 30vw, 520px);
  /* Aspect ratio of one mosaic cell, set per level by startLevel(). Layer tiles use
     it so a plate thumbnail is shown whole instead of centre-cropped to a square. */
  --cell-ar: 1;
}

* { box-sizing: border-box; }

html {
  /* Belt-and-suspenders alongside user-scalable=no in the viewport meta tag:
     stops double-tap-to-zoom everywhere while still allowing normal one-finger
     scrolling, so a drag gesture during gameplay can never zoom the page. */
  touch-action: manipulation;
  height: 100%;
  background: var(--bg);
}

/* The page itself never scrolls. Y8 runs the game in a fixed frame — and on a phone it
   rotates that frame rather than the handset — where the browser's own scrollbar is easy
   to miss and swipe-to-refresh fires on a downward drag mid-play. The document is locked
   to exactly one viewport, and anything taller than that — the level map, long modals —
   scrolls inside its own element instead. */
body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  display: flex;
  flex-direction: column;
  /* Nothing in a game of drag-and-drop is meant to be selected: without this, a drag
     across the board paints the toolbar and slot labels blue, and a long press pops
     the callout menu on mobile. */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

/* Ambient CMYK glow behind everything. Purely a background wash — it sits under
   .home-background (z-index -1) and never over gameplay. */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  z-index: -2;
  pointer-events: none;
  background:
    radial-gradient(58% 44% at 6% 4%, rgba(0, 183, 195, .20), transparent 70%),
    radial-gradient(52% 48% at 96% 26%, rgba(230, 0, 126, .17), transparent 70%),
    radial-gradient(66% 52% at 48% 108%, rgba(255, 212, 0, .07), transparent 70%);
}

/* Grabbing a layer must move the tile, not start the browser's own image drag. */
img,
canvas {
  -webkit-user-drag: none;
  user-drag: none;
}

header {
  flex: 0 0 auto;
  padding: 14px 22px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
}

header h1 {
  font-size: 22px;
  margin: 0;
  letter-spacing: .12em;
  background: linear-gradient(90deg, var(--cyan), var(--magenta), var(--yellow));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

/* During a level the global header is gone and the game screen carries its own
   chrome inside the rails, so the mosaic gets the full viewport height. */
body.playing header { display: none; }

body.playing main {
  padding: 0;
  gap: 0;
  align-items: stretch;
  overflow: hidden;
}

select, button {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 11px;
  font-size: 14px;
}

button {
  cursor: pointer;
  background: var(--accent);
  border-color: var(--accent);
  color: white;
  font-weight: 600;
}

button:hover { filter: brightness(1.1); }

main {
  /* Scroll container for the level map (see the body rule above): min-height:0 lets
     this flex child shrink below its content height, so the overflow lands here and
     not on the document. During a level nothing overflows — fitGameToViewport() in
     js/game.js sizes the mosaic to what is left — so no scrollbar appears in play. */
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 24px;
  align-items: center;
}

/* ---------------------------------------------------------------------------------
   Game stage
   ---------------------------------------------------------------------------------
   Phone / portrait: everything stacks. Wide screen: a three-column grid (rules at
   the bottom of this file) — brand and counters left, framed mosaic in the middle at
   full height, target and shuffled layers right. The wide layout exists for one
   concrete reason: a centred board in an otherwise empty window puts gameplay at
   roughly 45% of a 16:9 frame, and Y8's game frame is exactly that shape. Filling the
   rails is what closes the gap. */

#gameView {
  width: 100%;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px;
}

.rail {
  min-width: 0;
  min-height: 0;
  display: flex;
  gap: 10px;
}

/* Stacked layout: the left rail collapses to a thin strip of live counters. The
   brand block would only eat height the mosaic needs, so it is hidden here. */
.rail-left {
  flex: 0 0 auto;
  align-items: center;
  justify-content: space-between;
}

/* Stacked, the left rail is one narrow strip: the brand block and the mode label
   would only push the mosaic down, and at 375px the counters plus four swatches
   otherwise run past the right edge. */
.rail-left .brand,
.rail-left .stat-sub { display: none; }

.rail-left .stat-panel {
  flex: 0 1 auto;
  min-width: 0;
}

/* Stacked layout: the right rail is a strip under the board, so its blocks sit side
   by side — a narrow target thumbnail on the left and the layers filling the rest.
   Capped and internally scrollable so a long tray can never push the mosaic off the
   viewport (the page itself must not scroll). */
.rail-right {
  flex: 0 0 auto;
  max-height: 26vh;
  overflow-y: auto;
  overscroll-behavior: contain;
  align-items: stretch;
}

.rail-right > .rail-block {
  flex: 0 0 96px;
}

.rail-right > #trayContainer,
.rail-right > .rail-controls {
  flex: 1 1 auto;
  min-width: 0;
}

.brand {
  text-align: center;
  padding: 2px 0;
}

.brand-letters {
  display: flex;
  justify-content: center;
  gap: 6px;
  font-size: 26px;
  font-weight: 900;
  line-height: 1;
}

.brand-letters span { color: var(--swatch-color); }

.brand-word {
  margin-top: 6px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .42em;
  text-indent: .42em;
  color: var(--muted);
}

.stat-panel {
  background: linear-gradient(180deg, var(--panel), var(--panel-2));
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .45), 0 1px 0 rgba(255, 255, 255, .05) inset;
  padding: 8px 14px;
  display: flex;
  align-items: baseline;
  gap: 8px;
  white-space: nowrap;
}

.stat-label {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .16em;
  text-transform: uppercase;
  color: var(--muted);
}

.stat-value {
  font-size: 22px;
  font-weight: 800;
  line-height: 1;
}

.stat-sub {
  font-size: 11px;
  color: var(--muted);
}

/* One glossy swatch per ink plate, showing how many of that plate are still in the
   tray. Live board state — see renderChannelLegend() in js/game.js. */
.channel-legend {
  display: flex;
  gap: 8px;
}

.legend-chip {
  width: 32px;
  height: 32px;
  flex: 0 0 auto;
  border-radius: 50%;
  background:
    radial-gradient(circle at 34% 26%, rgba(255, 255, 255, .8), transparent 42%),
    radial-gradient(circle at 50% 55%, var(--swatch-color), rgba(0, 0, 0, .45));
  box-shadow:
    0 0 0 2px rgba(255, 255, 255, .09),
    0 0 16px -3px var(--swatch-color),
    0 5px 12px rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 800;
  color: rgba(0, 0, 0, .62);
  transition: filter .25s, box-shadow .25s;
}

.legend-chip.done {
  filter: grayscale(.9) brightness(.5);
  box-shadow: 0 0 0 2px rgba(255, 255, 255, .05);
  color: rgba(255, 255, 255, .35);
}

/* in-game toolbar */

.stage-top {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.top-bar {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 12px;
  background: linear-gradient(180deg, var(--panel), var(--panel-2));
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .45), 0 1px 0 rgba(255, 255, 255, .05) inset;
}

/* Auto side margins centre the pill between the back button and the action icons
   without stretching it across the whole bar. */
.level-pill {
  flex: 0 1 auto;
  margin: 0 auto;
  min-width: 0;
  text-align: center;
  background: rgba(255, 255, 255, .06);
  border: 1px solid rgba(255, 255, 255, .09);
  border-radius: 12px;
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: .07em;
  text-transform: uppercase;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  flex: 0 0 auto;
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 12px;
  background: rgba(255, 255, 255, .06);
  border: 1px solid rgba(255, 255, 255, .09);
  color: var(--text);
  font-size: 17px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.icon-btn:disabled { opacity: .5; cursor: default; }

.hint-btn {
  color: #2a1d00;
  background: linear-gradient(180deg, #ffdc4a, #e0a800);
  border-color: rgba(255, 255, 255, .18);
  box-shadow: 0 0 18px -4px rgba(255, 212, 0, .65);
}

.gear-btn {
  flex: 0 0 auto;
  width: 46px;
  height: 46px;
  padding: 0;
  border: none;
  border-radius: 14px;
  background: linear-gradient(180deg, #f5c518, #d99b09);
  color: #2a1d00;
  font-size: 20px;
  line-height: 1;
  box-shadow: 0 0 22px -5px rgba(245, 197, 24, .6), 0 6px 16px rgba(0, 0, 0, .45);
}

.settings-popover {
  position: fixed;
  top: 66px;
  right: 14px;
  z-index: 30;
  min-width: 190px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 14px;
  background: linear-gradient(180deg, var(--panel), var(--panel-2));
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 18px 44px rgba(0, 0, 0, .6);
}

.settings-title {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .16em;
  text-transform: uppercase;
  color: var(--muted);
}

/* board */

.board-column {
  /* Sized by the stage, never by its contents: min-height/min-width 0 plus overflow
     hidden means this box is a fixed target that fitGameToViewport() can scan
     .board-fit down into, without the two chasing each other. */
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.board-fit {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.board-frame {
  width: max-content;
  padding: 8px;
  border-radius: 12px;
  background: var(--frame);
  box-shadow:
    0 0 0 1px rgba(0, 0, 0, .55),
    0 0 0 1px rgba(255, 255, 255, .1) inset,
    0 20px 55px rgba(0, 0, 0, .6);
}

.puzzle-grid {
  display: grid;
  gap: 2px;
  background: #000;
  width: 320px;
  border-radius: 4px;
  overflow: hidden;
  box-shadow: 0 0 0 2px rgba(0, 0, 0, .55) inset;
}

.cell-canvas {
  width: 100%;
  min-width: 0;
  height: auto;
  display: block;
  outline: 2px solid transparent;
  outline-offset: -2px;
  transition: outline-color .2s;
  background: repeating-conic-gradient(#2a2d33 0% 25%, #24262b 0% 50%) 50% / 16px 16px;
}

.cell-canvas.solved {
  outline-color: var(--good);
}

.controls-row {
  display: grid;
  gap: 8px;
  width: 320px;
}

.cell-controls {
  min-width: 0;
  background: linear-gradient(180deg, var(--panel), var(--panel-2));
  border: 1px solid var(--border);
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .4);
  transition: border-color .2s;
}

.cell-controls.solved {
  border-color: var(--good);
}

.slots {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  padding: 8px;
}

.slot {
  aspect-ratio: 1;
  min-width: 0;
  border: 2px dashed var(--border);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 13px;
  color: var(--muted);
  cursor: pointer;
  overflow: hidden;
  position: relative;
  touch-action: none;
}

.slot[data-channel="C"] { color: var(--cyan); }
.slot[data-channel="M"] { color: var(--magenta); }
.slot[data-channel="Y"] { color: var(--yellow); }
.slot[data-channel="K"] { color: var(--key); }

.slot.filled {
  border-style: solid;
  border-color: var(--good);
  cursor: default;
}

.slot.provisional {
  border-style: solid;
  border-color: var(--accent);
  cursor: pointer;
}

.slot.provisional:hover {
  border-color: var(--bad);
}

.slot.provisional::after {
  content: '\2715';
  position: absolute;
  top: 2px;
  right: 2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: rgba(20, 22, 26, 0.75);
  color: var(--text);
  font-size: 9px;
  line-height: 14px;
  text-align: center;
}

.slot canvas {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.slot.wrong {
  border-color: var(--bad);
  animation: flash .4s;
}

.slot.shake {
  animation: shake .3s;
}

@keyframes flash {
  0%, 100% { background: transparent; }
  50% { background: rgba(255, 84, 112, 0.25); }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

/* trays of shuffled layers */

/* A cell's own tray never holds more than its four plates, so it is laid out as four
   columns rather than a wrapping flex row. Wrapping made it two to four tile rows
   tall depending on how wide the panel happened to be, and on easy levels that panel
   sits under the mosaic — it was costing the picture roughly half its width. */
.cell-tray {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, var(--tile-size, 72px)));
  justify-content: center;
  align-content: start;
  gap: 6px;
  border-top: 1px dashed var(--border);
  padding: 8px;
  min-height: 44px;
}

.cell-tray .tile { width: 100%; }

/* The global tray is a grid so its thumbnails fill the right rail edge to edge
   instead of wrapping as loose squares. Its column width is fixed rather than tied
   to --tile-size: the rail scrolls on its own, so the tray never competes with the
   mosaic for height and has no reason to follow the fit scan. */
.tray {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
  gap: 8px;
}

/* Size comes from --tile-size, which fitGameToViewport() in js/game.js lowers on
   short screens so the per-cell trays never push the mosaic off the viewport. */
.tile {
  width: var(--tile-size, 72px);
  aspect-ratio: var(--cell-ar);
  min-width: 0;
  border: 2px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  cursor: grab;
  transition: transform .1s, border-color .1s;
  touch-action: none;
}

.tray .tile { width: 100%; }

.tile:hover { transform: scale(1.05); }

.tile.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent);
}

.tile canvas {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.dragging-source {
  opacity: .35;
}

.drag-ghost {
  position: fixed;
  top: 0;
  left: 0;
  pointer-events: none;
  z-index: 50;
  border: 2px solid var(--accent);
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .4);
  opacity: .9;
}

.drag-ghost canvas {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

/* right rail blocks */

.rail-block {
  flex: 0 0 auto;
  min-width: 0;
  padding: 10px;
  background: linear-gradient(180deg, var(--panel), var(--panel-2));
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .4);
}

.rail-label,
.tray-label {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .16em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 8px;
}

/* Reference, not a second board: uncapped it grew to almost the size of the mosaic
   on a wide screen. The panel around it still spans the rail, so centring the image
   inside costs no layout. */
.target-thumbnail {
  display: block;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: min(32vh, 320px);
  margin: 0 auto;
  border-radius: 8px;
  border: 2px solid rgba(255, 255, 255, .12);
  cursor: pointer;
}

.target-thumbnail:hover {
  border-color: var(--accent);
}

#trayContainer {
  flex: 0 1 auto;
  min-width: 0;
  min-height: 0;
  background: linear-gradient(180deg, var(--panel), var(--panel-2));
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 10px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .4);
  /* Last resort on a very short screen: the layers scroll here, inside the game,
     rather than on the page. */
  overflow-y: auto;
  overscroll-behavior: contain;
}

.rail-controls {
  min-width: 0;
  display: none;
}

.rail-controls:not(:empty) { display: block; }

/* Chrome's default scrollbar is a bright slab on these dark panels, and every one of
   them scrolls inside the game (the page itself never does),
   so it shows up right next to the artwork. */
main,
.rail-right,
#trayContainer,
.cell-tray,
.modal-content {
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, .18) transparent;
}

main::-webkit-scrollbar,
.rail-right::-webkit-scrollbar,
#trayContainer::-webkit-scrollbar,
.cell-tray::-webkit-scrollbar,
.modal-content::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

main::-webkit-scrollbar-track,
.rail-right::-webkit-scrollbar-track,
#trayContainer::-webkit-scrollbar-track,
.cell-tray::-webkit-scrollbar-track,
.modal-content::-webkit-scrollbar-track {
  background: transparent;
}

main::-webkit-scrollbar-thumb,
.rail-right::-webkit-scrollbar-thumb,
#trayContainer::-webkit-scrollbar-thumb,
.cell-tray::-webkit-scrollbar-thumb,
.modal-content::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, .16);
  border-radius: 4px;
}

.hidden { display: none !important; }

/* modals */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 40;
}

.modal-content {
  background: linear-gradient(180deg, var(--panel), var(--panel-2));
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 24px;
  max-width: 480px;
  width: 90%;
  max-height: 90vh;
  overflow-y: auto;
  text-align: center;
  box-shadow: 0 24px 60px rgba(0, 0, 0, .65);
}

.modal-content img {
  max-width: 100%;
  border-radius: 8px;
  margin: 12px 0;
}

#artworkInfo {
  text-align: left;
  font-size: 14px;
  color: var(--muted);
  line-height: 1.5;
}

#artworkInfo h3 {
  color: var(--text);
  margin-bottom: 4px;
}

#artworkInfo .credit {
  font-style: italic;
  font-size: 12px;
}

#artworkInfo .campaign-done {
  color: var(--good);
  font-weight: 700;
  text-align: center;
  font-size: 15px;
}

#artworkInfo a {
  color: var(--accent);
}

.modal-actions {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin-top: 8px;
}

/* The win / artwork modal, laid out so "Next level" is never below the fold.
   Stacked — title, painting, caption, buttons — a portrait canvas eats the whole
   viewport on its own and pushes the buttons out of sight behind a scrollbar, which is
   what Y8 review flagged. Y8's frame is wide and short, so the room actually going
   spare is horizontal: the painting takes the left column at whatever height the
   viewport allows, and its caption and the buttons sit beside it instead of underneath.
   Height then comes out as title + picture, which fits. The narrow-screen fallback
   below goes back to stacking, with the picture capped low enough to leave the buttons
   on screen. */
#winModal .modal-content {
  max-width: min(940px, 94vw);
  display: grid;
  grid-template-columns: auto minmax(240px, 1fr);
  grid-template-areas:
    "title title"
    "image info"
    "image actions";
  column-gap: 20px;
  align-content: start;
  justify-items: center;
}

#winModal h2 {
  grid-area: title;
  margin: 0 0 12px;
}

#winImage {
  grid-area: image;
  margin: 0;
  /* Cap the height, not the width: a tall portrait is what overflows, and letting it
     size from its own aspect ratio keeps the column exactly as wide as it needs. */
  max-height: 70vh;
  max-width: 100%;
  width: auto;
  align-self: center;
  object-fit: contain;
}

#winModal #artworkInfo {
  grid-area: info;
  justify-self: stretch;
  /* Long credit lines scroll inside the caption rather than growing the modal — the
     picture is what sets the height here, and nothing else is allowed to. */
  max-height: 70vh;
  overflow-y: auto;
}

#winModal .modal-actions {
  grid-area: actions;
  justify-self: stretch;
  /* The row this sits in is whatever height the painting leaves over, and a stretched
     flex child would take all of it — buttons four times their normal height. Pin them
     to their own size, at the foot of the column so they line up with the picture. */
  align-self: end;
  align-items: center;
  margin-top: 16px;
}

/* Too narrow to stand a second column: stack, and buy the buttons their room back by
   giving the picture less of the height. */
@media (max-width: 760px) {
  #winModal .modal-content {
    grid-template-columns: 1fr;
    grid-template-areas:
      "title"
      "image"
      "info"
      "actions";
  }

  #winImage {
    max-height: 42vh;
    margin: 0 0 12px;
  }

  #winModal #artworkInfo {
    max-height: 26vh;
  }

  #winModal .modal-actions {
    margin-top: 12px;
  }
}

.muted { color: var(--muted); }

/* language switcher */

.lang-switch {
  display: flex;
  gap: 4px;
}

.lang-btn {
  flex: 1 1 auto;
  padding: 6px 8px;
  font-size: 12px;
  background: rgba(255, 255, 255, .06);
  border-color: rgba(255, 255, 255, .09);
  color: var(--muted);
}

.lang-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

/* level map — fills the whole viewport below the header; the page scrolls
   normally once the level grid grows past one screen. */

#levelMapView {
  width: 100%;
  max-width: none;
  padding: 8px 4px 40px;
}

#levelMapView h2 {
  font-size: 16px;
  color: var(--muted);
  font-weight: 600;
  margin-top: 0;
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 14px;
}

/* Fixed to the viewport (not the scrolling level list) so it stays put as a
   backdrop no matter how tall the level grid grows. */
.home-background {
  position: fixed;
  inset: 0;
  overflow: hidden;
  z-index: -1;
  pointer-events: none;
}

.floating-art {
  position: absolute;
  top: 100%;
  left: var(--x);
  width: var(--size);
  height: auto;
  border-radius: 8px;
  opacity: .14;
  filter: saturate(.6) blur(.3px);
  transform: rotate(var(--rotate));
  box-shadow: 0 10px 30px rgba(0, 0, 0, .5);
  animation: floatUp var(--duration) linear var(--delay) infinite;
}

@keyframes floatUp {
  from { transform: translateY(0) rotate(var(--rotate)); }
  to { transform: translateY(-140vh) rotate(var(--rotate)); }
}

.level-band-heading {
  grid-column: 1 / -1;
  font-size: 14px;
  color: var(--muted);
  margin: 16px 0 0;
}

.level-band-heading:first-child {
  margin-top: 0;
}

.level-tile {
  position: relative;
  width: 100%;
  aspect-ratio: 3 / 4;
  border-radius: 12px;
  overflow: hidden;
  background: var(--panel) center / cover no-repeat;
  border: 2px solid var(--border);
  color: white;
  display: flex;
  align-items: flex-end;
  justify-content: flex-start;
  padding: 8px;
  cursor: pointer;
  transition: transform .15s, border-color .15s;
}

.level-tile:hover:not(.locked) {
  transform: translateY(-4px);
  border-color: var(--accent);
}

.level-tile .tile-number {
  background: rgba(10, 10, 14, .7);
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 15px;
  font-weight: 800;
  line-height: 1.4;
}

.level-tile.completed {
  border-color: var(--good);
}

.level-tile.completed .tile-number {
  background: var(--good);
  color: #06301a;
}

.level-tile.locked {
  cursor: not-allowed;
  filter: grayscale(1) brightness(.45);
}

.level-tile .lock-icon {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 30px;
}

.level-tile-info {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: rgba(10, 10, 14, .65);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  cursor: pointer;
  transition: transform .1s, background .1s;
}

.level-tile-info:hover {
  background: var(--accent);
  transform: scale(1.1);
}

/* toast */

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 14px;
  z-index: 45;
}

/* No leaderboard styles in this build: the table is Y8's own hosted modal, drawn
   outside this page's markup. See js/y8.js. */

/* how to play */

/* "How to play" is seven sections long and opens by itself on a first visit, so on a
   phone it is the first thing a new player meets. Left to scroll as one block — which is
   what .modal-content does by default — the Close button sat past the end of that text,
   off screen, with nothing on screen hinting there was anything below: the modal read as
   a dead end and the game looked like it would not start. That is the bug that made the
   game unlaunchable on a phone.
   So the modal is a column with a pinned head and foot, and only the text between them
   scrolls. The button is in view from the moment it opens, at every screen size. */
.howto-content {
  max-width: 560px;
  text-align: left;
  display: flex;
  flex-direction: column;
  /* Overrides .modal-content's own scrolling — the inner .howto-body scrolls instead,
     and two nested scrollers would fight each other for the same drag. */
  overflow: hidden;
}

.howto-content h2 {
  text-align: center;
}

.howto-body {
  flex: 1 1 auto;
  /* Without min-height:0 a flex child refuses to shrink below its content, so the column
     would grow past the modal and take the button off screen again. */
  min-height: 0;
  overflow-y: auto;
  /* Keep a flick that reaches the end of the text inside the modal, rather than handing
     it to the page behind. */
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  /* A rule between the text and the pinned button. Without it the text simply stops
     mid-sentence right above the button and reads as broken rather than as scrollable —
     the scrollbar itself is invisible on a phone. */
  border-bottom: 1px solid var(--border);
  padding-bottom: 8px;
}

#closeHowToBtn {
  flex: 0 0 auto;
  align-self: center;
  margin-top: 16px;
}

.howto-swatches {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  margin: 4px 0 20px;
}

/* The C/M/Y/K swatches are followed by the word "Puzzle" so the row spells out the
   game's full name. It matches the header, the page title and the Y8 listing exactly —
   "CMYK Puzzle". Never localize it. */
.howto-swatch-word {
  font-weight: 800;
  font-size: 22px;
  line-height: 1;
  color: var(--text);
  margin-left: 2px;
}

.howto-swatch {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 15px;
  color: #14161a;
  background: var(--swatch-color);
}

.howto-section {
  margin-bottom: 16px;
}

/* Phone in landscape — which on Y8 includes a phone held upright, since the player
   rotates the game rather than the handset. The heading and the C/M/Y/K swatch row are
   fixed overhead above a text panel with barely 90px to work with, so shrink the
   overhead and give the text the difference. */
@media (max-height: 460px) {
  .howto-content h2 {
    font-size: 18px;
    margin: 0 0 4px;
  }

  .howto-swatches {
    margin: 0 0 10px;
    gap: 6px;
  }

  .howto-swatch {
    width: 28px;
    height: 28px;
    font-size: 12px;
  }

  .howto-swatch-word {
    font-size: 16px;
  }
}

.howto-section h3 {
  margin: 0 0 4px;
  font-size: 14px;
  color: var(--accent);
}

.howto-section p {
  margin: 0;
  font-size: 14px;
  color: var(--text);
  line-height: 1.5;
}

#howToModal #closeHowToBtn {
  display: block;
  margin: 8px auto 0;
}

/* ---------------------------------------------------------------------------------
   Wide screen: the three-column stage. The aspect-ratio guard keeps a tall window
   (phone, tablet portrait) on the stacked layout, where side rails would leave the
   picture nothing to grow into.
   --------------------------------------------------------------------------------- */
/* The second condition is Y8's rotated player: on a phone it turns the game rather than
   the handset, handing it a viewport around 430x250. That is far below the 760px this
   layout was written for, but it has the same problem in a sharper form — height has run
   out and width is going spare. Stacked there the board alone is taller than the screen
   and the layers end up in a 65px strip that the drag gesture makes unscrollable; as
   columns they sit beside the board and stay reachable. The rails are narrowed to suit
   in the max-height block further down. */
@media (min-width: 760px) and (min-aspect-ratio: 5/4),
       (max-height: 460px) and (min-width: 380px) and (min-aspect-ratio: 5/4) {
  /* Two templates, and fitGameToViewport() switches between them. The plain one gives
     the middle column every spare pixel, which is what the mosaic is scanned against.
     Once the scan settles the mosaic is usually height-bound and narrower than that
     column, so .board-measured pins the middle to the width actually used, widens both
     rails to their caps, and lets space-between push what's still left into the two
     gaps — the board ends up centred between rails that sit on the screen edges,
     instead of floating in a band of empty backdrop. */
  #gameView {
    display: grid;
    grid-template-columns: var(--rail-left-w) minmax(0, 1fr) var(--rail-right-w);
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-areas:
      "left top   top"
      "left board right";
    gap: 12px;
    padding: 12px;
  }

  /* The rails are minmax, not fixed at their caps: the scan that produced
     --board-col-w ran against the *narrow* rails, so on a width-bound level the board
     can be wide enough that both caps no longer fit beside it. As minmax tracks they
     simply grow into whatever room is left and stop, instead of pushing the right
     rail off the screen. */
  #gameView.board-measured {
    grid-template-columns:
      minmax(var(--rail-left-w), var(--rail-left-max))
      var(--board-col-w, minmax(0, 1fr))
      minmax(var(--rail-right-w), var(--rail-right-max));
    justify-content: space-between;
  }

  .rail-left {
    grid-area: left;
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-start;
    gap: 14px;
  }

  .rail-left .brand,
  .rail-left .stat-sub { display: block; }

  .stat-panel {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
    padding: 12px 16px;
  }

  .stat-value { font-size: 30px; }

  .channel-legend {
    flex-direction: column;
    align-items: center;
    gap: 12px;
  }

  .legend-chip {
    width: 52px;
    height: 52px;
    font-size: 16px;
  }

  .stage-top { grid-area: top; }
  .board-column { grid-area: board; }

  .rail-right {
    grid-area: right;
    flex-direction: column;
    max-height: none;
    align-items: stretch;
  }

  .rail-right > .rail-block,
  .rail-right > .rail-controls {
    flex: 0 0 auto;
  }

  /* Square drop targets stretched to a wide cell's full width are as tall as they
     are wide, which is most of the height. Cap and centre them under their cell. */
  .slots {
    grid-template-columns: repeat(4, minmax(0, 56px));
    justify-content: center;
  }

  /* Easy levels keep each cell's layers in its own panel: side by side with the
     slots that panel is half as tall, and the height goes to the picture. */
  .cell-controls {
    flex-direction: row;
    align-items: flex-start;
  }

  .cell-tray {
    border-top: none;
    border-left: 1px dashed var(--border);
    flex: 1 1 auto;
    min-width: 0;
  }

  /* A one-cell level has no column of cells to line its slots up with, so its panel
     is rendered in the right rail instead (see renderGrid()). There it stacks
     internally again. */
  .rail-controls .cell-controls {
    flex-direction: column;
  }

  .rail-controls .cell-tray {
    border-top: 1px dashed var(--border);
    border-left: none;
  }
}

/* Very short window (phone in landscape): the square drop targets are the tallest
   fixed thing left, so cap them harder than the wide-screen rule above does. */
@media (max-height: 460px) {
  /* Rails at desktop width would leave a 430px-wide viewport barely 110px for the board.
     The left rail is only a counter and the channel legend here (its brand block is
     hidden below), and the right one holds the target thumbnail and the layers, so both
     can be much tighter than the defaults in :root. */
  :root {
    --rail-left-w: 74px;
    --rail-left-max: 92px;
    --rail-right-w: 132px;
    --rail-right-max: 168px;
  }

  /* The target thumbnail is the one thing in the right rail that is nice to have rather
     than needed: with ~180px of rail to divide between it, the slots and the layers, it
     was pushing the layers off the bottom into a strip that cannot be scrolled (tiles
     carry touch-action:none so they can be dragged). The same picture is one tap away on
     the ℹ️ button in the top bar, so on a screen this short the rail keeps only what the
     player has to reach. */
  .rail-right .rail-block {
    display: none;
  }

  /* .tray normally ignores --tile-size and lets auto-fill stretch each layer to the
     rail's full width — fine on a desktop rail that is tall and scrolls on its own. Here
     it meant one 110px layer per row and sixteen rows running off the bottom of a strip
     that cannot be scrolled by dragging. Pack them by the fitted tile size instead, so
     they tile several across and the column stays short enough to see. */
  .tray {
    grid-template-columns: repeat(auto-fill, minmax(var(--tile-size, 32px), 1fr));
    gap: 6px;
  }

  .slots {
    grid-template-columns: repeat(4, minmax(0, 40px));
    justify-content: center;
  }

  /* Easy levels: a cell's own layer strip is narrow here, so its tiles stack up and
     the panel grows taller than the picture. Bound it and let it scroll in place. */
  .cell-tray {
    max-height: 64px;
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .legend-chip { width: 30px; height: 30px; font-size: 11px; }

  /* Every pixel of chrome here comes straight out of the picture: the board column
     is only ~280px tall on a phone in landscape. */
  #gameView { gap: 8px; padding: 8px; }
  .board-fit { gap: 6px; }
  .top-bar { padding: 5px 8px; border-radius: 14px; gap: 6px; }
  .level-pill { padding: 5px 10px; font-size: 12px; }
  .icon-btn { width: 34px; height: 34px; border-radius: 10px; font-size: 15px; }
  .gear-btn { width: 38px; height: 38px; border-radius: 11px; font-size: 17px; }
  .rail-left .brand { display: none; }
}

/* Narrow screen: trim the map header, every pixel it takes comes out of the map. */
@media (max-width: 600px) {
  header {
    padding: 8px 12px;
    gap: 10px;
  }

  header h1 { font-size: 18px; }

  main {
    padding: 12px;
    gap: 12px;
  }

  .settings-popover { top: 58px; right: 10px; }
}
