/* --- theme tokens ------------------------------------------------------- */
:root {
  --bg: #12131f;
  --surface: #1c1e30;
  --cell: 70px;
  --gap: 10px;
  --text: #f2f3fa;
  --muted: #9aa0bd;
  --line: #2c2f47;
  --accent: #7c8cff;
  --tile-sat: 62%;
  --tile-light: 46%;
  --tile-text: #0d0e16;
  --radius: 14px;
  --shake: 0px;
}

:root[data-theme='paper'] {
  --bg: #f3efe6;
  --surface: #e6e0d2;
  --text: #26241e;
  --muted: #7b7365;
  --line: #d3cabb;
  --accent: #b4652f;
  --tile-sat: 48%;
  --tile-light: 62%;
  --tile-text: #221f18;
}

:root[data-theme='mono'] {
  --bg: #0d0d0d;
  --surface: #1a1a1a;
  --text: #f5f5f5;
  --muted: #8a8a8a;
  --line: #2a2a2a;
  --accent: #e5e5e5;
  --tile-sat: 0%;
  --tile-light: 72%;
  --tile-text: #0d0d0d;
}

:root[data-theme='neon'] {
  --bg: #070a14;
  --surface: #101733;
  --text: #e8f0ff;
  --muted: #7f8ec4;
  --line: #1c2a55;
  --accent: #35e0ff;
  --tile-sat: 92%;
  --tile-light: 58%;
  --tile-text: #05070f;
}

/* --- base --------------------------------------------------------------- */
* {
  box-sizing: border-box;
  margin: 0;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, 'Segoe UI', Inter, system-ui, sans-serif;
  display: grid;
  place-items: start center;
  padding: max(12px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  overscroll-behavior: none;
  transition: background 0.3s ease, color 0.3s ease;
}

.app {
  width: min(480px, 100%);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

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

.logo {
  font-size: 30px;
  font-weight: 800;
  letter-spacing: -0.03em;
}

.meta {
  display: flex;
  align-items: center;
  gap: 6px;
}

.chip {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 5px 11px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  white-space: nowrap;
}

.icon-btn {
  width: 32px;
  height: 32px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted);
  font-size: 15px;
  cursor: pointer;
}

.icon-btn:hover {
  color: var(--text);
  border-color: var(--accent);
}

/* --- stats -------------------------------------------------------------- */
.stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.stat {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 9px 12px;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.label {
  font-size: 11px;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 600;
}

.value {
  font-size: 21px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

/* --- board -------------------------------------------------------------- */
.board {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: var(--gap);
  touch-action: none;
  user-select: none;
}

.board.shaking {
  animation: shake 0.22s ease;
}

.cells {
  position: absolute;
  inset: var(--gap);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: var(--gap);
}

.cell {
  background: color-mix(in srgb, var(--bg) 55%, transparent);
  border-radius: var(--radius);
}

.tiles {
  position: absolute;
  inset: var(--gap);
}

.tile {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--cell);
  height: var(--cell);
  border-radius: var(--radius);
  display: grid;
  place-items: center;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.03em;
  color: var(--tile-text);
  background: hsl(var(--hue) var(--tile-sat) var(--tile-light));
  box-shadow: inset 0 -2px 0 hsl(var(--hue) var(--tile-sat) calc(var(--tile-light) - 9%));
  transform: translate(
    calc(var(--x) * (var(--cell) + var(--gap))),
    calc(var(--y) * (var(--cell) + var(--gap)))
  );
  transition: transform 0.11s ease-in-out;
  font-size: calc(var(--cell) * 0.42);
}

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

/* Newly spawned tiles fade in; merged tiles land with a pop once the sources
   have finished sliding underneath them. */
.tile.is-new {
  animation: appear 0.14s ease 0.09s backwards;
}

.tile.is-merged {
  animation: pop 0.16s ease 0.1s backwards;
  z-index: 2;
}

@keyframes appear {
  from {
    opacity: 0;
    scale: 0.5;
  }
}

@keyframes pop {
  0% {
    scale: 0.6;
  }
  60% {
    scale: 1.12;
  }
  100% {
    scale: 1;
  }
}

@keyframes shake {
  25% {
    transform: translateX(calc(var(--shake) * -1));
  }
  75% {
    transform: translateX(var(--shake));
  }
}

/* --- controls ----------------------------------------------------------- */
.controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.btn {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 700;
  padding: 12px;
  cursor: pointer;
  transition: transform 0.1s ease, border-color 0.15s ease, opacity 0.15s ease;
}

.btn:hover:not(:disabled) {
  border-color: var(--accent);
}

.btn:active:not(:disabled) {
  transform: scale(0.98);
}

.btn:disabled {
  opacity: 0.42;
  cursor: default;
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--tile-text);
}

.hint {
  text-align: center;
  font-size: 13px;
  color: var(--muted);
}

/* --- modals ------------------------------------------------------------- */
.modal {
  position: fixed;
  inset: 0;
  display: none;
  place-items: center;
  padding: 20px;
  background: color-mix(in srgb, var(--bg) 78%, transparent);
  backdrop-filter: blur(6px);
  z-index: 10;
}

.modal.open {
  display: grid;
}

.sheet {
  width: min(380px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 22px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  animation: rise 0.2s ease;
}

.sheet h2 {
  font-size: 22px;
  font-weight: 800;
}

.sheet p {
  font-size: 14px;
  color: var(--muted);
}

.result {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.result > div {
  background: color-mix(in srgb, var(--bg) 55%, transparent);
  border-radius: var(--radius);
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
}

.reward {
  color: var(--accent) !important;
  font-weight: 700;
}

.rules {
  margin: 0;
  padding-left: 18px;
  font-size: 14px;
  color: var(--muted);
  display: grid;
  gap: 5px;
}

.fine {
  font-size: 12px !important;
  color: var(--muted);
}

.theme-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.theme-card {
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg) 55%, transparent);
  border-radius: var(--radius);
  padding: 12px;
  display: grid;
  gap: 6px;
  justify-items: start;
  color: var(--text);
  font: inherit;
  cursor: pointer;
}

.theme-card.active {
  border-color: var(--accent);
}

.theme-card:disabled {
  opacity: 0.45;
  cursor: default;
}

.theme-swatch {
  width: 100%;
  height: 34px;
  border-radius: 9px;
  background: linear-gradient(120deg, var(--a), var(--b));
}

.theme-name {
  font-weight: 700;
  font-size: 14px;
}

.theme-cost {
  font-size: 12px;
  color: var(--muted);
}

@keyframes rise {
  from {
    opacity: 0;
    translate: 0 12px;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
