:root {
  --bg: #10141f;
  --panel: #1b2233;
  --cell: #252e44;
  --text: #e8ecf5;
  --muted: #8b95ad;
  --accent: #4fd1c5;

  --board: min(92vw, 460px);
  --gap: calc(var(--board) * 0.03);
  --size: calc((var(--board) - 5 * var(--gap)) / 4);
  --anim: 120ms;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
  -webkit-tap-highlight-color: transparent;
}

.app {
  width: var(--board);
  margin: 0 auto;
  padding: 24px 0 32px;
}

.back {
  display: inline-block;
  margin-bottom: 12px;
  font-size: 14px;
  color: var(--muted);
  text-decoration: none;
}
.back:hover { color: var(--text); }

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

h1 {
  margin: 0;
  font-size: clamp(24px, 7vw, 34px);
  line-height: 1.05;
  letter-spacing: -0.02em;
  color: var(--accent);
}

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

.score {
  min-width: 78px;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--panel);
  text-align: center;
}
.score span { display: block; font-size: 12px; color: var(--muted); }
.score strong { font-size: 20px; font-variant-numeric: tabular-nums; }

.actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 16px 0;
}
.hint { flex: 1; margin: 0; font-size: 14px; color: var(--muted); }
.hint b { color: var(--text); }

.btn {
  border: 0;
  border-radius: 10px;
  padding: 10px 14px;
  font: inherit;
  font-size: 14px;
  font-weight: 700;
  color: #0d1320;
  background: var(--accent);
  cursor: pointer;
  white-space: nowrap;
}
.btn.ghost { background: var(--panel); color: var(--text); }
.btn:disabled { opacity: 0.4; cursor: default; }

.board {
  position: relative;
  width: var(--board);
  height: var(--board);
  padding: var(--gap);
  border-radius: 14px;
  background: var(--panel);
  touch-action: none;
  user-select: none;
}

.cells, .tiles {
  position: absolute;
  inset: var(--gap);
}
.cells {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--gap);
}
.cells div { border-radius: 8px; background: var(--cell); }

.tile {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--size);
  height: var(--size);
  transform: translate(
    calc(var(--c) * (var(--size) + var(--gap))),
    calc(var(--r) * (var(--size) + var(--gap)))
  );
  transition: transform var(--anim) ease-in-out;
}
.tile span {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  border-radius: 8px;
  background: hsl(var(--h) 70% var(--l));
  color: var(--fg);
  font-weight: 800;
  font-size: calc(var(--size) * 0.42);
  font-variant-numeric: tabular-nums;
}
.tile[data-len="3"] span { font-size: calc(var(--size) * 0.34); }
.tile[data-len="4"] span { font-size: calc(var(--size) * 0.27); }
.tile[data-len="5"] span,
.tile[data-len="6"] span { font-size: calc(var(--size) * 0.21); }
.tile.glow span { box-shadow: 0 0 18px hsl(var(--h) 90% 60% / 0.7); }

.tile.new span { animation: appear 160ms ease-out var(--anim) backwards; }
.tile.merged { z-index: 2; }
.tile.merged span { animation: pop 180ms ease-out var(--anim) backwards; }

@keyframes appear {
  from { opacity: 0; transform: scale(0); }
  to { opacity: 1; transform: scale(1); }
}
@keyframes pop {
  0% { opacity: 0; transform: scale(0.6); }
  60% { opacity: 1; transform: scale(1.15); }
  100% { transform: scale(1); }
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  border-radius: 14px;
  background: rgb(16 20 31 / 0.82);
  animation: fade 300ms ease-out;
}
.overlay[hidden] { display: none; }
.overlay p { margin: 0; font-size: 28px; font-weight: 800; }
.overlay-btns { display: flex; gap: 8px; }

@keyframes fade { from { opacity: 0; } }

.help {
  margin: 16px 0 0;
  text-align: center;
  font-size: 13px;
  color: var(--muted);
}

@media (prefers-reduced-motion: reduce) {
  :root { --anim: 0ms; }
  .tile span { animation: none !important; }
}
