:root {
  --page: #eef1f5;
  --well: #ffffff;
  --cell-bg: #edf0f4;
  --ink: #1d2738;
  --muted: #687488;
  --line: #d5dce6;
  --accent: #d9694f;

  --font: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;

  --board: min(400px, calc(100vw - 32px));
  --gap: calc(var(--board) * 0.03);
  --cell: calc((var(--board) - 5 * var(--gap)) / 4);
  --radius: calc(var(--cell) * 0.1);
  --column-gap: 72px;
  --content: calc(2 * var(--board) + var(--column-gap));
  --slide: 100ms;
}

[hidden] {
  display: none !important;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font: 400 16px/1.5 var(--font);
  font-feature-settings: "cv11", "ss01", "ss03";
  -webkit-font-smoothing: antialiased;
}

h1,
h2 {
  margin: 0;
}

button,
select {
  font: inherit;
  color: inherit;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* ---- Masthead ---------------------------------------------------------- */

.masthead {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 24px;
  max-width: calc(var(--content) + 32px);
  margin: 0 auto;
  padding: 48px 16px 32px;
}

.wordmark {
  font-size: 56px;
  font-weight: 600;
  line-height: 1;
  letter-spacing: -0.05em;
}

.tagline {
  max-width: 44ch;
  margin: 12px 0 0;
  color: var(--muted);
}

.links {
  display: flex;
  gap: 20px;
  padding-top: 8px;
}

.link {
  padding: 0;
  border: 0;
  background: none;
  color: var(--muted);
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
}

.link:hover {
  color: var(--ink);
}

/* ---- Two players side by side ------------------------------------------- */

.arena {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 48px var(--column-gap);
  max-width: calc(var(--content) + 32px);
  margin: 0 auto;
  padding: 0 16px 64px;
}

.player {
  width: var(--board);
}

.player-head,
.player-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.player-head {
  margin-bottom: 16px;
}

.player-head h2 {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.player-foot {
  flex-wrap: wrap;
  margin-top: 16px;
}

.scores {
  display: flex;
  gap: 8px;
}

.score {
  min-width: 76px;
  padding: 6px 14px;
  border-radius: 8px;
  background: var(--well);
  text-align: center;
}

.score span {
  display: block;
  font-size: 12px;
  color: var(--muted);
}

/* Tabular figures in Inter also widen hyphens, so only numbers that change get them. */
.score strong,
.tile-face,
.bar-value {
  font-variant-numeric: tabular-nums;
}

.score strong {
  font-size: 19px;
  font-weight: 700;
}

.caption {
  margin: 0;
  font-size: 14px;
  color: var(--muted);
}

.button {
  padding: 9px 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--well);
  font-weight: 700;
  cursor: pointer;
}

.button:hover {
  border-color: #b9c3d1;
}

.button-primary {
  border-color: var(--ink);
  background: var(--ink);
  color: #fff;
}

.button-primary:hover {
  border-color: #33415a;
  background: #33415a;
}

/* ---- Board ----------------------------------------------------------------- */

.board {
  position: relative;
  width: var(--board);
  height: var(--board);
  border-radius: calc(var(--radius) * 1.6);
  background: var(--well);
  box-shadow: 0 1px 2px rgb(29 39 56 / 6%), 0 8px 24px rgb(29 39 56 / 6%);
  touch-action: none;
  user-select: none;
}

.board-grid,
.board-tiles,
.board-heat {
  position: absolute;
  inset: 0;
}

.board-grid {
  display: grid;
  grid-template-columns: repeat(4, var(--cell));
  gap: var(--gap);
  padding: var(--gap);
}

.board-cell {
  border-radius: var(--radius);
  background: var(--cell-bg);
}

/* Tiles are positioned by --x/--y so a move is just a transform transition.
   The inner face carries the colour and the pop animations, which keeps
   scaling separate from sliding. */
.tile,
.board-heat span {
  position: absolute;
  top: var(--gap);
  left: var(--gap);
  width: var(--cell);
  height: var(--cell);
  transform: translate(
    calc(var(--x) * (var(--cell) + var(--gap))),
    calc(var(--y) * (var(--cell) + var(--gap)))
  );
}

.tile {
  z-index: 1;
  transition: transform var(--slide) ease-in-out;
}

.tile-face {
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  border-radius: var(--radius);
  background: var(--tile-bg);
  color: var(--tile-fg);
  font-size: calc(var(--cell) * 0.4);
  font-weight: 700;
  letter-spacing: -0.03em;
}

.tile-face[data-digits="3"] { font-size: calc(var(--cell) * 0.33); }
.tile-face[data-digits="4"] { font-size: calc(var(--cell) * 0.26); }
.tile-face[data-digits="5"] { font-size: calc(var(--cell) * 0.21); }

/* Small tiles are cool slate and blue, the middle of the game warms into
   coral, and 2048 onward turns to ink. */
.tile-face[data-exp="1"] { --tile-bg: #d9e0eb; --tile-fg: #33415a; }
.tile-face[data-exp="2"] { --tile-bg: #c3d0e2; --tile-fg: #33415a; }
.tile-face[data-exp="3"] { --tile-bg: #a6bddc; --tile-fg: #233049; }
.tile-face[data-exp="4"] { --tile-bg: #8eaad3; --tile-fg: #fff; }
.tile-face[data-exp="5"] { --tile-bg: #6c8fc6; --tile-fg: #fff; }
.tile-face[data-exp="6"] { --tile-bg: #4b73b5; --tile-fg: #fff; }
.tile-face[data-exp="7"] { --tile-bg: #f0b9a6; --tile-fg: #5a2618; }
.tile-face[data-exp="8"] { --tile-bg: #ea9c82; --tile-fg: #fff; }
.tile-face[data-exp="9"] { --tile-bg: #e2805f; --tile-fg: #fff; }
.tile-face[data-exp="10"] { --tile-bg: #d9694f; --tile-fg: #fff; }
.tile-face[data-exp="11"] { --tile-bg: #1d2738; --tile-fg: #fff; }
.tile-face[data-exp="12"] { --tile-bg: #0f1520; --tile-fg: #f0b9a6; }

.tile-face.appear {
  animation: appear 180ms ease-out backwards;
}

.tile-face.merge {
  animation: merge 180ms ease-out;
}

@keyframes appear {
  from {
    transform: scale(0.2);
    opacity: 0;
  }
}

@keyframes merge {
  50% {
    transform: scale(1.12);
  }
}

/* ---- Overlay message ------------------------------------------------------- */

.board-message {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 16px;
  border-radius: inherit;
  background: rgb(238 241 245 / 82%);
  opacity: 0;
  visibility: hidden;
  transition: opacity 240ms ease, visibility 0s 240ms;
}

.board-message.is-visible {
  opacity: 1;
  visibility: visible;
  transition: opacity 240ms ease 120ms;
}

.board-message-title {
  margin: 0;
  font-size: 32px;
  font-weight: 700;
  letter-spacing: -0.03em;
}

.board-message-actions {
  display: flex;
  gap: 8px;
}

/* ---- AI controls --------------------------------------------------------- */

.model {
  flex: 1 1 0;
  min-width: 0;
}

.model select {
  width: 100%;
  padding: 8px 28px 8px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--well) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M1 1l4 4 4-4' fill='none' stroke='%23687488' stroke-width='1.5'/%3E%3C/svg%3E") no-repeat right 12px center;
  font-weight: 600;
  appearance: none;
  cursor: pointer;
}

.speed {
  display: inline-flex;
  padding: 3px;
  border-radius: 8px;
  background: #e1e6ee;
}

.speed button {
  padding: 5px 10px;
  border: 0;
  border-radius: 6px;
  background: none;
  color: var(--muted);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.speed button[aria-checked="true"] {
  background: var(--well);
  color: var(--ink);
  box-shadow: 0 1px 2px rgb(29 39 56 / 10%);
}

#ai-toggle {
  min-width: 92px;
}

/* ---- Thinking panel -------------------------------------------------------- */

.thinking {
  margin-top: 20px;
  border-top: 1px solid var(--line);
  padding-top: 14px;
}

.thinking summary {
  width: fit-content;
  color: var(--muted);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.thinking summary:hover {
  color: var(--ink);
}

.thinking-label {
  margin: 12px 0;
  color: var(--muted);
  font-size: 14px;
}

.bars {
  display: grid;
  gap: 8px;
}

.bar {
  display: grid;
  grid-template-columns: 64px 1fr 72px;
  align-items: center;
  gap: 12px;
  font-size: 14px;
}

.bar-track {
  height: 8px;
  border-radius: 4px;
  background: var(--cell-bg);
  overflow: hidden;
}

.bar-fill {
  display: block;
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: #a6bddc;
  transition: width 120ms ease-out;
}

.bar-value {
  color: var(--muted);
  text-align: right;
}

.bar.is-chosen .bar-name,
.bar.is-chosen .bar-value {
  color: var(--ink);
  font-weight: 700;
}

.bar.is-chosen .bar-fill {
  background: var(--accent);
}

.bar.is-blocked {
  color: #a3adbd;
}

/* The heat glow sits above the tiles as an inset ring so tile colours stay
   readable while the model's attention is still visible. */
.board-heat {
  z-index: 2;
  pointer-events: none;
}

.board-heat span {
  border-radius: var(--radius);
  box-shadow:
    inset 0 0 0 calc(var(--h, 0) * 2px) rgb(217 105 79 / calc(var(--h, 0) * 70%)),
    0 0 calc(var(--h, 0) * 14px) rgb(217 105 79 / calc(var(--h, 0) * 25%));
  transition: box-shadow 160ms ease-out;
}

/* ---- About dialog ---------------------------------------------------------- */

.about {
  width: min(560px, calc(100vw - 32px));
  max-height: calc(100vh - 64px);
  padding: 32px;
  border: 0;
  border-radius: 14px;
  background: var(--well);
  color: var(--ink);
  box-shadow: 0 24px 64px rgb(29 39 56 / 20%);
}

.about::backdrop {
  background: rgb(29 39 56 / 35%);
}

.about h2 {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.about h3 {
  margin: 24px 0 8px;
  font-size: 16px;
  font-weight: 700;
}

.about p,
.about dd {
  color: #45516a;
  line-height: 1.6;
}

.about a {
  color: var(--ink);
}

.models {
  margin: 0;
}

.models dt {
  margin-top: 14px;
  font-weight: 700;
}

.models dd {
  margin: 2px 0 0;
}

.about-foot {
  margin-top: 24px;
  font-size: 14px;
}

.close {
  float: right;
  width: 32px;
  height: 32px;
  margin: -12px -12px 0 0;
  border: 0;
  border-radius: 50%;
  background: none;
  color: var(--muted);
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
}

.close:hover {
  background: var(--cell-bg);
  color: var(--ink);
}

/* ---- Small screens and touch ------------------------------------------- */

.for-touch {
  display: none;
}

@media (pointer: coarse) {
  .for-keys {
    display: none;
  }

  .for-touch {
    display: inline;
  }
}

@media (max-width: 520px) {
  .masthead {
    flex-direction: column;
    gap: 16px;
    padding-top: 32px;
  }

  .wordmark {
    font-size: 44px;
  }

  .links {
    padding-top: 0;
  }

  .arena {
    row-gap: 56px;
  }

  #ai-toggle {
    flex-basis: 100%;
  }
}
