/* ============================================================
   0x800 — hexadecimal 2048 on a hex grid
   ============================================================ */

/* ---------- tokens ---------- */
:root {
  --font: ui-monospace, 'SF Mono', Menlo, Consolas, 'Liberation Mono', monospace;
  --hex-size: 40px;
  --hex-clip: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
  --slide: 90ms;
  --pop: 110ms;
  --spawn: 120ms;
  --spawn-delay: 100ms;
  --nudge: 160ms;

  color-scheme: light;
  --bg: #f6f1e7;
  --bg-glow: #fffbf1;
  --fg: #2a251d;
  --muted: #8a8172;
  --line: #ddd3bf;
  --cell: #e5dbc5;
  --cell-fg: #c6ba9f;
  --panel: #2a251d;
  --panel-fg: #f6f1e7;
  --panel-muted: #a89e88;
  --accent: #c0331c;
  --gold: #c99a00;
  --focus: #3462c4;
  --btn: #ede5d3;
  --btn-fg: #2a251d;
  --btn-hover: #e2d8c0;
  --btn-border: #d4c9b0;
  --overlay: rgba(246, 241, 231, 0.82);
  --card: #fffdf8;
  --shadow: 0 24px 60px -24px rgba(60, 40, 10, 0.45);

  /* tile palette — one step per exponent, hue rotating through the wheel.
     Every fg/bg pair is checked for >= 4.5:1 contrast in test/palette.test.js */
  --tile-1: #f5f0e6;
  --tile-1-fg: #5c5446; /* 1            */
  --tile-2: #efe4cf;
  --tile-2-fg: #5c5446; /* 2            */
  --tile-4: #f4b183;
  --tile-4-fg: #201a12; /* 4            */
  --tile-8: #ef8354;
  --tile-8-fg: #2a1206; /* 8            */
  --tile-10: #c0331c;
  --tile-10-fg: #fdf9f3; /* 10   = 16    */
  --tile-20: #b8305e;
  --tile-20-fg: #fdf9f3; /* 20   = 32    */
  --tile-40: #9a34c4;
  --tile-40-fg: #fdf9f3; /* 40   = 64    */
  --tile-80: #6c4bd1;
  --tile-80-fg: #fdf9f3; /* 80   = 128   */
  --tile-100: #3462c4;
  --tile-100-fg: #fdf9f3; /* 100  = 256   */
  --tile-200: #1c6f96;
  --tile-200-fg: #fdf9f3; /* 200  = 512   */
  --tile-400: #22b39a;
  --tile-400-fg: #06201c; /* 400  = 1024  */
  --tile-800: #ffce3a;
  --tile-800-fg: #2a2000; /* 800  = 2048 — WIN */
  --tile-1000: #b8ff3a;
  --tile-1000-fg: #12200a;
  --tile-2000: #6cffd1;
  --tile-2000-fg: #04211a;
  --tile-4000: #ff6cd1;
  --tile-4000-fg: #22041a;
  --tile-8000: #101014;
  --tile-8000-fg: #ffce3a; /* the void */
}

[data-theme='dark'] {
  color-scheme: dark;
  --bg: #0f1116;
  --bg-glow: #1a1e2a;
  --fg: #e8e3d8;
  --muted: #7d8291;
  --line: #262a36;
  --cell: #1a1d26;
  --cell-fg: #343947;
  --panel: #1a1d26;
  --panel-fg: #f2eee6;
  --panel-muted: #6d7381;
  --accent: #ff6b4a;
  --gold: #ffce3a;
  --focus: #7aa2ff;
  --btn: #1e222d;
  --btn-fg: #e8e3d8;
  --btn-hover: #2a2f3d;
  --btn-border: #2e3342;
  --overlay: rgba(15, 17, 22, 0.8);
  --card: #171a22;
  --shadow: 0 24px 60px -24px rgba(0, 0, 0, 0.9);
  /* the two lowest tiles are toned down so they don't glare on a dark board */
  --tile-1: #2e3140;
  --tile-1-fg: #d8d3c6;
  --tile-2: #3b3f52;
  --tile-2-fg: #e6e1d5;
}

[data-theme='contrast'] {
  color-scheme: dark;
  --bg: #000000;
  --bg-glow: #000000;
  --fg: #ffffff;
  --muted: #d0d0d0;
  --line: #ffffff;
  --cell: #1c1c1c;
  --cell-fg: #7a7a7a;
  --panel: #ffffff;
  --panel-fg: #000000;
  --panel-muted: #333333;
  --accent: #ff5c5c;
  --gold: #ffe600;
  --focus: #ffe600;
  --btn: #000000;
  --btn-fg: #ffffff;
  --btn-hover: #222222;
  --btn-border: #ffffff;
  --overlay: rgba(0, 0, 0, 0.92);
  --card: #000000;
  --shadow: none;
  --tile-1: #ffffff;
  --tile-1-fg: #000000;
  --tile-2: #ffe600;
  --tile-2-fg: #000000;
  --tile-4: #00e5ff;
  --tile-4-fg: #000000;
  --tile-8: #ff9ecf;
  --tile-8-fg: #000000;
  --tile-10: #9dff57;
  --tile-10-fg: #000000;
  --tile-20: #ffb347;
  --tile-20-fg: #000000;
  --tile-40: #a8c8ff;
  --tile-40-fg: #000000;
  --tile-80: #ff6b6b;
  --tile-80-fg: #000000;
  --tile-100: #c9a6ff;
  --tile-100-fg: #000000;
  --tile-200: #7dffc8;
  --tile-200-fg: #000000;
  --tile-400: #ffd6a5;
  --tile-400-fg: #000000;
  --tile-800: #ffe600;
  --tile-800-fg: #000000;
  --tile-1000: #baff29;
  --tile-1000-fg: #000000;
  --tile-2000: #57d8ff;
  --tile-2000-fg: #000000;
  --tile-4000: #ffabab;
  --tile-4000-fg: #000000;
  --tile-8000: #ffffff;
  --tile-8000-fg: #000000;
}

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

html {
  height: 100%;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100%;
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.4;
  color: var(--fg);
  background: var(--bg);
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
  color: inherit;
}
kbd {
  font: inherit;
}
:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}
::selection {
  background: var(--fg);
  color: var(--bg);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.gold {
  color: var(--gold);
}

/* ---------- layout ---------- */
.app {
  display: grid;
  grid-template-areas: 'top' 'meta' 'stage' 'log' 'controls';
  grid-template-rows: auto auto minmax(200px, 1fr) auto auto;
  gap: 8px;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
  padding: max(10px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right))
    max(10px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
}

.top {
  grid-area: top;
}
.meta {
  grid-area: meta;
}
.stage {
  grid-area: stage;
}
.log {
  grid-area: log;
}
.controls {
  grid-area: controls;
}

/* wide landscape / desktop: board on the left, everything else in a side column */
@media (min-width: 720px) and (min-aspect-ratio: 5/4) {
  .app {
    max-width: 960px;
    grid-template-columns: minmax(0, 1fr) 300px;
    grid-template-rows: auto auto auto 1fr;
    grid-template-areas: 'top meta' 'stage log' 'stage controls' 'stage .';
    column-gap: 32px;
  }
  .meta {
    align-self: end;
    justify-content: flex-end;
  }
  .log {
    height: calc(1.5em * 6);
  }
  .stage {
    min-height: 460px;
  }
}

/* ---------- header ---------- */
.top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.logo {
  margin: 0;
  font-size: clamp(30px, 9vw, 42px);
  font-weight: 800;
  line-height: 1;
  letter-spacing: -0.03em;
}
.logo .prefix {
  color: var(--muted);
  font-weight: 500;
}
.logo small {
  display: block;
  margin-top: 6px;
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.12em;
  color: var(--muted);
}

.scores {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.panel {
  position: relative;
  min-width: 82px;
  padding: 6px 10px 7px;
  border: 0;
  border-radius: 8px;
  background: var(--panel);
  color: var(--panel-fg);
  text-align: left;
  cursor: default;
}
button.panel {
  cursor: pointer;
}
.panel-k {
  display: block;
  font-size: 10px;
  letter-spacing: 0.16em;
  color: var(--panel-muted);
}
.panel-v {
  display: block;
  margin-top: 1px;
  font-size: 18px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.gain {
  position: absolute;
  right: 8px;
  top: -6px;
  font-size: 13px;
  font-weight: 700;
  color: var(--gold);
  opacity: 0;
  pointer-events: none;
}
.gain.show {
  animation: rise 700ms ease-out forwards;
}
@keyframes rise {
  0% {
    opacity: 0;
    translate: 0 10px;
  }
  25% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    translate: 0 -24px;
  }
}

/* ---------- meta line ---------- */
.meta {
  display: flex;
  flex-wrap: wrap;
  gap: 2px 16px;
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.meta .seed {
  margin-left: auto;
  opacity: 0.7;
}

/* ---------- stage & board ---------- */
.stage {
  position: relative;
  min-height: 0;
  border-radius: 24px;
  background: radial-gradient(closest-side at 50% 50%, var(--bg-glow), transparent 100%);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.board {
  position: absolute;
  left: 50%;
  top: 50%;
  width: calc(var(--hex-size) * var(--bw, 8.66));
  height: calc(var(--hex-size) * var(--bh, 8));
  margin-left: calc(var(--hex-size) * var(--bw, 8.66) / -2);
  margin-top: calc(var(--hex-size) * var(--bh, 8) / -2);
}
.board.nudge {
  animation: nudge var(--nudge) ease-out;
}
@keyframes nudge {
  0%,
  100% {
    translate: 0 0;
  }
  40% {
    translate: calc(var(--nx, 0) * 7px) calc(var(--ny, 0) * 7px);
  }
}

.cells,
.tiles {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.cell,
.tile {
  position: absolute;
  left: calc(50% - var(--hex-size) * 0.8660254);
  top: calc(50% - var(--hex-size));
  width: calc(var(--hex-size) * 1.7320508);
  height: calc(var(--hex-size) * 2);
}

.cell {
  display: grid;
  place-items: center;
  transform: translate3d(calc(var(--hex-size) * var(--tx)), calc(var(--hex-size) * var(--ty)), 0)
    scale(0.93);
  clip-path: var(--hex-clip);
  background: var(--cell);
  color: var(--cell-fg);
  font-size: calc(var(--hex-size) * 0.5);
  letter-spacing: -0.12em;
}

.tile {
  transform: translate3d(calc(var(--hex-size) * var(--tx)), calc(var(--hex-size) * var(--ty)), 0);
  transition: transform var(--slide) ease-out;
  will-change: transform;
  z-index: 2;
}
.tile.absorbed {
  z-index: 1;
}

.face {
  position: absolute;
  inset: 3.5%;
  display: grid;
  place-items: center;
  clip-path: var(--hex-clip);
  font-weight: 700;
  font-size: calc(var(--hex-size) * 0.68);
  letter-spacing: -0.03em;
  line-height: 1;
  background-color: var(--tile-1);
  color: var(--tile-1-fg);
  background-image: linear-gradient(
    160deg,
    rgba(255, 255, 255, 0.22),
    rgba(255, 255, 255, 0) 45%,
    rgba(0, 0, 0, 0.1)
  );
  transition:
    background-color 120ms,
    color 120ms;
}
.tile[data-len='3'] .face {
  font-size: calc(var(--hex-size) * 0.52);
}
.tile[data-len='4'] .face {
  font-size: calc(var(--hex-size) * 0.42);
  letter-spacing: -0.05em;
}

.face.pop {
  animation: pop var(--pop) ease-out;
}
.tile.spawn .face {
  animation: spawn var(--spawn) var(--spawn-delay) cubic-bezier(0.2, 0.9, 0.3, 1.25) backwards;
}

@keyframes pop {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.18);
  }
  100% {
    transform: scale(1);
  }
}
@keyframes spawn {
  from {
    transform: scale(0.1);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

/* tile colours, one rule per exponent */
.tile[data-v='1'] .face {
  background-color: var(--tile-1);
  color: var(--tile-1-fg);
}
.tile[data-v='2'] .face {
  background-color: var(--tile-2);
  color: var(--tile-2-fg);
}
.tile[data-v='4'] .face {
  background-color: var(--tile-4);
  color: var(--tile-4-fg);
}
.tile[data-v='8'] .face {
  background-color: var(--tile-8);
  color: var(--tile-8-fg);
}
.tile[data-v='10'] .face {
  background-color: var(--tile-10);
  color: var(--tile-10-fg);
}
.tile[data-v='20'] .face {
  background-color: var(--tile-20);
  color: var(--tile-20-fg);
}
.tile[data-v='40'] .face {
  background-color: var(--tile-40);
  color: var(--tile-40-fg);
}
.tile[data-v='80'] .face {
  background-color: var(--tile-80);
  color: var(--tile-80-fg);
}
.tile[data-v='100'] .face {
  background-color: var(--tile-100);
  color: var(--tile-100-fg);
}
.tile[data-v='200'] .face {
  background-color: var(--tile-200);
  color: var(--tile-200-fg);
}
.tile[data-v='400'] .face {
  background-color: var(--tile-400);
  color: var(--tile-400-fg);
}
.tile[data-v='800'] .face {
  background-color: var(--tile-800);
  color: var(--tile-800-fg);
}
.tile[data-v='1000'] .face {
  background-color: var(--tile-1000);
  color: var(--tile-1000-fg);
}
.tile[data-v='2000'] .face {
  background-color: var(--tile-2000);
  color: var(--tile-2000-fg);
}
.tile[data-v='4000'] .face {
  background-color: var(--tile-4000);
  color: var(--tile-4000-fg);
}
.tile[data-v='8000'] .face {
  background-color: var(--tile-8000);
  color: var(--tile-8000-fg);
}

/* the trophy shimmers */
.tile[data-v='800'] .face {
  background-image:
    linear-gradient(
      120deg,
      rgba(255, 255, 255, 0) 30%,
      rgba(255, 255, 255, 0.55) 50%,
      rgba(255, 255, 255, 0) 70%
    ),
    linear-gradient(160deg, rgba(255, 255, 255, 0.22), rgba(0, 0, 0, 0.08));
  background-size:
    250% 100%,
    100% 100%;
  animation: shimmer 2.6s ease-in-out infinite;
}
@keyframes shimmer {
  0%,
  55% {
    background-position:
      120% 0,
      0 0;
  }
  100% {
    background-position:
      -120% 0,
      0 0;
  }
}

/* ---------- log ---------- */
.log {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  margin: 0;
  height: calc(1.5em * 4);
  overflow: hidden;
  font: inherit;
  font-size: 11.5px;
  line-height: 1.5;
  color: var(--muted);
  white-space: pre-wrap;
  word-break: break-all;
}
.log span {
  display: block;
  opacity: 0.55;
}
.log span:last-child {
  opacity: 1;
  color: var(--fg);
}

/* ---------- controls ---------- */
.controls {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding-bottom: 4px;
}

.pad {
  --pad-r: 58px;
  --pad-w: 48px;
  --pad-h: 55px;
  position: relative;
  width: calc(var(--pad-r) * 2 + var(--pad-w));
  height: calc(var(--pad-r) * 2 * 0.8660254 + var(--pad-h));
}

.pad button {
  position: absolute;
  left: 50%;
  top: 50%;
  width: var(--pad-w);
  height: var(--pad-h);
  margin: calc(var(--pad-h) / -2) 0 0 calc(var(--pad-w) / -2);
  translate: calc(var(--pad-r) * var(--ax, 0)) calc(var(--pad-r) * var(--ay, 0));
  display: grid;
  place-items: center;
  align-content: center;
  gap: 0;
  padding: 0;
  border: 0;
  clip-path: var(--hex-clip);
  background: var(--btn);
  background-image: linear-gradient(160deg, rgba(255, 255, 255, 0.18), rgba(0, 0, 0, 0.06));
  color: var(--btn-fg);
  cursor: pointer;
  touch-action: manipulation;
  transition:
    background-color 120ms,
    transform 120ms,
    color 120ms;
}
.pad button:hover {
  background-color: var(--btn-hover);
}
.pad button:active,
.pad button.pressed {
  background-color: var(--fg);
  color: var(--bg);
  transform: scale(0.92);
}
.pad button:focus-visible {
  outline: none;
  background-color: var(--focus);
  color: #fff;
}
.pad button:disabled {
  opacity: 0.35;
  cursor: default;
}
.pad .glyph {
  font-size: 20px;
  line-height: 1;
}
.pad .key {
  font-size: 9px;
  line-height: 1;
  letter-spacing: 0.1em;
  opacity: 0.55;
  margin-top: 3px;
}
.pad .undo .glyph {
  font-size: 18px;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 40px;
  padding: 8px 14px;
  border: 1px solid var(--btn-border);
  border-radius: 10px;
  background: var(--btn);
  color: var(--btn-fg);
  font-size: 13px;
  cursor: pointer;
  touch-action: manipulation;
  transition:
    background-color 120ms,
    transform 120ms;
}
.btn:hover {
  background: var(--btn-hover);
}
.btn:active {
  transform: translateY(1px);
}
.btn.primary {
  background: var(--fg);
  color: var(--bg);
  border-color: var(--fg);
}
.btn.primary:hover {
  opacity: 0.9;
}
.btn kbd {
  font-size: 10px;
  padding: 0 4px;
  border: 1px solid currentColor;
  border-radius: 4px;
  opacity: 0.55;
}

/* on pointer devices the pad fades until you reach for it */
@media (hover: hover) and (pointer: fine) {
  .pad {
    opacity: 0.45;
    transition: opacity 200ms;
  }
  .pad:hover,
  .pad:focus-within {
    opacity: 1;
  }
}
/* on touch devices there are no keys to hint at */
@media (hover: none) {
  .btn kbd,
  .pad .key {
    display: none;
  }
}

/* ---------- overlay ---------- */
.overlay {
  position: absolute;
  inset: -6px;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 12px;
  border-radius: 24px;
  background: var(--overlay);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  animation: fade 160ms ease-out;
}
.overlay[hidden] {
  display: none;
}
@keyframes fade {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.card {
  width: min(100%, 400px);
  max-height: 100%;
  overflow: auto;
  touch-action: pan-y;
  padding: 22px 22px 18px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--card);
  box-shadow: var(--shadow);
  animation: card-in 200ms cubic-bezier(0.2, 0.9, 0.3, 1.2);
}
@keyframes card-in {
  from {
    transform: scale(0.92);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

.ov-kicker {
  margin: 0;
  font-size: 11px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--muted);
}
.ov-title {
  margin: 6px 0 4px;
  font-size: clamp(24px, 7vw, 34px);
  line-height: 1.05;
  letter-spacing: -0.02em;
}
.overlay[data-kind='win'] .ov-title {
  color: var(--gold);
}
.overlay[data-kind='over'] .ov-title {
  color: var(--accent);
}
.ov-sub {
  margin: 0 0 10px;
  font-size: 13px;
  color: var(--muted);
}
.ov-sub:empty {
  display: none;
}
.ov-body {
  font-size: 13px;
  line-height: 1.5;
}
.ov-body:empty {
  display: none;
}
.ov-body h3 {
  margin: 12px 0 4px;
  font-size: 11px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--muted);
}
.ov-body p {
  margin: 0 0 8px;
}
.ov-body code {
  font-weight: 700;
}
.ov-body pre {
  margin: 6px 0 10px;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--cell);
  color: var(--fg);
  font: inherit;
  font-size: 12px;
  line-height: 1.45;
  overflow-x: auto;
}
.ov-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 14px;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 4px 0 8px;
}
.chip {
  min-height: 34px;
  padding: 6px 12px;
  border: 1px solid var(--btn-border);
  border-radius: 999px;
  background: var(--btn);
  color: var(--btn-fg);
  font-size: 12px;
  cursor: pointer;
  touch-action: manipulation;
}
.chip[aria-pressed='true'] {
  background: var(--fg);
  color: var(--bg);
  border-color: var(--fg);
}

/* ---------- high-contrast extras ---------- */
[data-theme='contrast'] .btn {
  border-width: 2px;
}
[data-theme='contrast'] .face {
  background-image: none;
}
[data-theme='contrast'] .pad button {
  background-image: none;
}

/* ---------- motion ---------- */
@media (prefers-reduced-motion: reduce) {
  :root {
    --slide: 0ms;
    --pop: 0ms;
    --spawn: 0ms;
    --spawn-delay: 0ms;
    --nudge: 0ms;
  }
  .board.nudge,
  .tile[data-v='800'] .face,
  .card,
  .overlay,
  .gain.show {
    animation: none;
  }
  .gain.show {
    opacity: 1;
  }
}
