/*
 * 2048 — styles. Every selector starts with .g-2048 (the game root).
 * Colours come only from the site variables; tile colours are color-mix
 * ramps, so they follow the light and dark themes on their own.
 */

.g-2048 {
  --g-gap: clamp(5px, 2vw, 10px);   /* space between cells */
  --g-r: clamp(5px, 1.6vw, 9px);    /* tile corner radius */
  position: relative;
}
.g-2048 [hidden] { display: none !important; }

/* visually hidden text for screen readers */
.g-2048-sr {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden;
  clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; border: 0;
}

/* three tabs must fit a 320px screen without scrolling */
@media (max-width: 400px) {
  .g-2048-tabs .pz-tab { padding: 8px 11px; font-size: .9rem; }
}

/* ---------- level bar ---------- */
.g-2048-levelbar { display: flex; align-items: center; justify-content: center; gap: 10px; margin: 0 0 10px; }
.g-2048-levelname { font-size: 1.15rem; font-weight: 900; min-width: 6.5em; text-align: center; }
.g-2048-levelbar .pz-btn { min-width: 44px; min-height: 40px; padding: 2px 10px; font-size: 1.35rem; line-height: 1; }

/* moves nearly gone: the number itself is the signal, the colour only underlines it */
.g-2048 .g-2048-low span { color: var(--bad); }

/* ---------- board ---------- */
.g-2048-board {
  --g-n: 4;
  position: relative;
  width: min(100%, 440px, max(250px, 64vh));
  aspect-ratio: 1;
  margin: 0 auto;
  border-radius: calc(var(--g-r) + var(--g-gap));
  background: var(--bg);
  box-shadow: inset 0 0 0 1.5px var(--border);
  container-type: inline-size;      /* lets the tile text scale with the board (cqw) */
  touch-action: none;               /* swipes move tiles, never the page */
  overscroll-behavior: contain;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
  cursor: grab;
}
.g-2048-board:active { cursor: grabbing; }

.g-2048-cells {
  position: absolute; inset: 0;
  display: grid;
  grid-template-columns: repeat(var(--g-n), minmax(0, 1fr));
  grid-template-rows: repeat(var(--g-n), minmax(0, 1fr));
  gap: var(--g-gap); padding: var(--g-gap);
}
.g-2048-cell { border-radius: var(--g-r); background: var(--surface); }

/* a wall: solid, hatched, clearly not a tile and not an empty cell */
.g-2048-cell.is-wall {
  background-color: color-mix(in srgb, var(--text) 62%, var(--surface-2));
  background-image: repeating-linear-gradient(45deg,
    transparent 0 5px, color-mix(in srgb, var(--surface) 40%, transparent) 5px 8px);
  box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--text) 82%, var(--surface-2));
}

/* ---------- tiles ---------- */
.g-2048-tiles { position: absolute; inset: var(--g-gap); pointer-events: none; }

/* --x / --y are the column and row; a tile is moved by changing them */
.g-2048-tile {
  position: absolute; left: 0; top: 0;
  width: calc((100% - (var(--g-n) - 1) * var(--g-gap)) / var(--g-n));
  height: calc((100% - (var(--g-n) - 1) * var(--g-gap)) / var(--g-n));
  transform: translate(calc(var(--x) * (100% + var(--g-gap))), calc(var(--y) * (100% + var(--g-gap))));
  transition: transform 110ms ease-out;
}
.g-2048-tile.is-merged { z-index: 2; }

.g-2048-face {
  --g-bg: var(--surface-2);
  --g-ink: var(--text);
  --g-fs: .46;        /* text height as a share of the tile, by number of digits */
  --g-fb: 1.4rem;     /* used only where cqw units are missing */
  width: 100%; height: 100%;
  display: grid; place-items: center; overflow: hidden;
  border-radius: var(--g-r);
  background: var(--g-bg);
  color: var(--g-ink);
  box-shadow:
    inset 0 0 0 1.5px color-mix(in srgb, var(--g-ink) 18%, transparent),
    inset 0 -3px 0 color-mix(in srgb, var(--g-ink) 9%, transparent);
  font-weight: 900; line-height: 1; letter-spacing: -.02em; font-variant-numeric: tabular-nums;
  font-size: var(--g-fb);
}
@supports (width: 1cqw) {
  .g-2048-face { font-size: calc(var(--g-fs) * (100cqw - (var(--g-n) + 1) * var(--g-gap)) / var(--g-n)); }
}
.g-2048-face[data-len="1"] { --g-fs: .52; --g-fb: 1.6rem; }
.g-2048-face[data-len="2"] { --g-fs: .46; --g-fb: 1.4rem; }
.g-2048-face[data-len="3"] { --g-fs: .36; --g-fb: 1.1rem; }
.g-2048-face[data-len="4"] { --g-fs: .29; --g-fb: .9rem; }
.g-2048-face[data-len="5"] { --g-fs: .235; --g-fb: .75rem; }
.g-2048-face[data-len="6"] { --g-fs: .2; --g-fb: .65rem; }

/*
 * The colour ramp (data-e = power of two).
 * 2 … 64: --surface-2 tinted more and more with --accent, text in --text.
 * 128 and up: solid --accent, then towards --warn, then --bad; text in
 * --accent-ink. A little --text is mixed into the solid colours: it darkens
 * them in the light theme and lightens them in the dark one, which is the
 * direction --accent-ink needs in each (contrast stays above 4.4 : 1).
 */
.g-2048-face[data-e="1"] { --g-bg: color-mix(in srgb, var(--accent) 10%, var(--surface-2)); }
.g-2048-face[data-e="2"] { --g-bg: color-mix(in srgb, var(--accent) 18%, var(--surface-2)); }
.g-2048-face[data-e="3"] { --g-bg: color-mix(in srgb, var(--accent) 26%, var(--surface-2)); }
.g-2048-face[data-e="4"] { --g-bg: color-mix(in srgb, var(--accent) 34%, var(--surface-2)); }
.g-2048-face[data-e="5"] { --g-bg: color-mix(in srgb, var(--accent) 42%, var(--surface-2)); }
.g-2048-face[data-e="6"] { --g-bg: color-mix(in srgb, var(--accent) 50%, var(--surface-2)); }
.g-2048-face[data-e="7"] {
  --g-ink: var(--accent-ink);
  --g-bg: color-mix(in srgb, var(--accent) 84%, var(--text));
}
.g-2048-face[data-e="8"] {
  --g-ink: var(--accent-ink);
  --g-bg: color-mix(in srgb, color-mix(in srgb, var(--warn) 35%, var(--accent)) 84%, var(--text));
}
.g-2048-face[data-e="9"] {
  --g-ink: var(--accent-ink);
  --g-bg: color-mix(in srgb, color-mix(in srgb, var(--warn) 70%, var(--accent)) 84%, var(--text));
}
.g-2048-face[data-e="10"] {
  --g-ink: var(--accent-ink);
  --g-bg: color-mix(in srgb, var(--warn) 84%, var(--text));
}
.g-2048-face[data-e="11"] {
  --g-ink: var(--accent-ink);
  --g-bg: color-mix(in srgb, color-mix(in srgb, var(--bad) 50%, var(--warn)) 84%, var(--text));
}
.g-2048-face[data-e="12"] {
  --g-ink: var(--accent-ink);
  --g-bg: color-mix(in srgb, var(--bad) 84%, var(--text));
}
.g-2048-face[data-e="13"] {
  --g-ink: var(--accent-ink);
  --g-bg: color-mix(in srgb, var(--bad) 62%, var(--text));
}

/* a new tile grows in; a merged tile gives a small pop */
.g-2048-tile.is-new .g-2048-face { animation: g-2048-appear 140ms ease-out both; }
.g-2048-tile.is-merged .g-2048-face { animation: g-2048-pop 150ms ease-out both; }
@keyframes g-2048-appear {
  from { transform: scale(.4); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}
@keyframes g-2048-pop {
  0% { transform: scale(1); }
  45% { transform: scale(1.13); }
  100% { transform: scale(1); }
}

/* ---------- text under the board ---------- */
.g-2048 .g-2048-info { max-width: 46ch; margin: 12px auto 4px; }
.g-2048 .g-2048-help { font-size: .85rem; margin-top: 14px; }

/* ---------- on-screen arrows ---------- */
.g-2048-pad {
  display: grid;
  grid-template-columns: repeat(3, 56px);
  grid-template-rows: repeat(2, 46px);
  gap: 6px; justify-content: center;
  margin: 10px 0 4px;
}
.g-2048 .g-2048-arrow { padding: 0; min-height: 0; border-radius: 12px; font-size: 1.3rem; line-height: 1; }
.g-2048-up { grid-area: 1 / 2; }
.g-2048-left { grid-area: 2 / 1; }
.g-2048-down { grid-area: 2 / 2; }
.g-2048-right { grid-area: 2 / 3; }
.g-2048-pad.g-2048-off { opacity: .45; }

/* ---------- level picker ---------- */
.g-2048 .g-2048-picker { margin-top: 14px; }
.g-2048-tick { position: absolute; top: 2px; right: 5px; font-size: .62rem; font-weight: 900; color: var(--accent); }
@media (max-width: 400px) {
  .g-2048 .g-2048-picker { grid-template-columns: repeat(auto-fill, minmax(44px, 1fr)); gap: 6px; }
}

/* ---------- no motion ---------- */
@media (prefers-reduced-motion: reduce) {
  .g-2048-tile { transition: none; }
  .g-2048-tile.is-new .g-2048-face,
  .g-2048-tile.is-merged .g-2048-face { animation: none; }
}

/* Tile ramp runs teal → amber → red (the site's own palette), not the beige-and-orange of the original game. */
.g-2048 .g-2048-face { --accent: var(--logic); }
