/* 2048 — современная тёмная тема */

:root {
  --text: #eef0ff;
  --muted: #9aa3c7;
  --panel: rgba(255, 255, 255, 0.07);
  --panel-border: rgba(255, 255, 255, 0.12);
  --accent1: #8b5cf6;
  --accent2: #22d3ee;

  --board-bg: linear-gradient(150deg, #191d33, #141629);
  --cell-bg: rgba(255, 255, 255, 0.055);

  --side: min(88vw, 430px);
  --gap: calc(var(--side) * 0.032);
  --cell: calc((var(--side) - var(--gap) * 5) / 4);
  --tile-radius: min(12px, calc(var(--cell) * 0.16));
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  display: flex;
  justify-content: center;
  padding: clamp(12px, 3vw, 32px) 12px;
  background: linear-gradient(160deg, #0c0f1d, #151027 45%, #0d1526);
  color: var(--text);
  font-family: 'Inter', -apple-system, 'Segoe UI', Roboto, Arial, sans-serif;
  touch-action: manipulation;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}

/* Мягкие цветные пятна на фоне */
body::before,
body::after {
  content: '';
  position: fixed;
  z-index: -1;
  width: 46vmax;
  height: 46vmax;
  border-radius: 50%;
  filter: blur(90px);
  opacity: 0.22;
  pointer-events: none;
}
body::before {
  background: var(--accent1);
  top: -14vmax;
  left: -12vmax;
}
body::after {
  background: var(--accent2);
  bottom: -16vmax;
  right: -12vmax;
}

.app {
  width: min(94vw, 470px);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ---------- шапка ---------- */

header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 12px;
}

h1 {
  margin: 0;
  font-size: clamp(44px, 12vw, 58px);
  font-weight: 800;
  letter-spacing: -0.03em;
  line-height: 0.95;
  background: linear-gradient(135deg, #ffffff 10%, #a78bfa 55%, var(--accent2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.tagline {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 13px;
  max-width: 24ch;
}

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

.score-box {
  position: relative;
  min-width: 78px;
  padding: 8px 14px 9px;
  text-align: center;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  backdrop-filter: blur(8px);
}

.score-box .label {
  display: block;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}

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

.float-score {
  position: absolute;
  left: 0;
  right: 0;
  top: 2px;
  font-size: 15px;
  font-weight: 800;
  color: #4ade80;
  pointer-events: none;
  animation: floatUp 0.8s ease-out forwards;
}

@keyframes floatUp {
  from { transform: translateY(6px); opacity: 1; }
  to { transform: translateY(-26px); opacity: 0; }
}

/* ---------- кнопки ---------- */

.controls {
  display: flex;
  gap: 10px;
}

button {
  cursor: pointer;
  font: inherit;
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  padding: 11px 18px;
  border-radius: 13px;
  border: 1px solid var(--panel-border);
  background: var(--panel);
  transition: transform 0.15s ease, filter 0.15s ease, opacity 0.2s ease;
}

button:hover {
  filter: brightness(1.2);
}

button:active {
  transform: scale(0.96);
}

button:disabled {
  opacity: 0.4;
  pointer-events: none;
}

button.primary {
  flex: 1;
  border: none;
  color: #fff;
  background: linear-gradient(135deg, var(--accent1), #6d28d9 55%, var(--accent2));
  box-shadow: 0 6px 22px rgba(139, 92, 246, 0.35);
}

#undoBtn {
  flex: 1;
}

/* ---------- доска ---------- */

.board-wrap {
  position: relative;
  align-self: center;
  touch-action: none;
}

.board-wrap.shake {
  animation: shake 0.3s ease;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  55% { transform: translateX(5px); }
  80% { transform: translateX(-3px); }
}

.board {
  position: relative;
  width: calc(var(--cell) * 4 + var(--gap) * 5);
  height: calc(var(--cell) * 4 + var(--gap) * 5);
  background: var(--board-bg);
  border: 1px solid var(--panel-border);
  border-radius: 18px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
}

.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: var(--cell-bg);
  border-radius: var(--tile-radius);
  box-shadow: inset 0 1px 1px rgba(255, 255, 255, 0.04);
}

.tiles {
  position: absolute;
  inset: 0;
}

/* Плитка: внешний слой отвечает за позицию (плавное скольжение),
   внутренний — за масштаб (появление и слияние). */
.tile {
  position: absolute;
  width: var(--cell);
  height: var(--cell);
  transform:
    translate(
      calc(var(--gap) + (var(--cell) + var(--gap)) * var(--c)),
      calc(var(--gap) + (var(--cell) + var(--gap)) * var(--r))
    );
  transition: transform 0.14s cubic-bezier(0.25, 0.8, 0.35, 1.15);
  will-change: transform;
  z-index: 2;
}

.tile-inner {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--tile-radius);
  font-weight: 800;
  font-size: calc(var(--cell) * 0.44);
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.25), 0 4px 14px rgba(0, 0, 0, 0.25);
}

.tile-inner[data-len='3'] { font-size: calc(var(--cell) * 0.36); }
.tile-inner[data-len='4'] { font-size: calc(var(--cell) * 0.28); }
.tile-inner[data-len='5'] { font-size: calc(var(--cell) * 0.23); }

/* ---------- цвета плиток ---------- */

.v2 .tile-inner   { background: linear-gradient(145deg, #fdf6e3, #ece1c8); color: #6b6157; }
.v4 .tile-inner   { background: linear-gradient(145deg, #f7ecd4, #e8d9b0); color: #6b6157; }
.v8 .tile-inner   { background: linear-gradient(145deg, #ffb35c, #f79346); color: #fff; }
.v16 .tile-inner  { background: linear-gradient(145deg, #ffa24e, #f97b2f); color: #fff; }
.v32 .tile-inner  { background: linear-gradient(145deg, #ff7e5f, #f65454); color: #fff; }
.v64 .tile-inner  { background: linear-gradient(145deg, #ff5f52, #ea3928); color: #fff; }
.v128 .tile-inner {
  background: linear-gradient(145deg, #ffd76b, #efbe3c);
  color: #fff;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.3), 0 0 18px rgba(240, 190, 60, 0.35);
}
.v256 .tile-inner {
  background: linear-gradient(145deg, #ffd35e, #eab928);
  color: #fff;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.3), 0 0 20px rgba(240, 190, 60, 0.45);
}
.v512 .tile-inner {
  background: linear-gradient(145deg, #ffc94d, #e2ad12);
  color: #fff;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.3), 0 0 22px rgba(240, 190, 60, 0.55);
}
.v1024 .tile-inner {
  background: linear-gradient(145deg, #ffc436, #dba106);
  color: #fff;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.3), 0 0 26px rgba(255, 200, 0, 0.6);
}
.v2048 .tile-inner {
  background: linear-gradient(145deg, #ffd700, #f0a500);
  color: #fff;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.35), 0 0 34px rgba(255, 200, 0, 0.75);
  animation: glowPulse 1.6s ease-in-out infinite;
}
.v-super .tile-inner {
  background: linear-gradient(145deg, #3b2f63, #1d1935);
  color: #ffe08a;
  border: 1px solid rgba(255, 224, 138, 0.5);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.15), 0 0 30px rgba(139, 92, 246, 0.6);
  animation: glowPulse 1.6s ease-in-out infinite;
}

@keyframes glowPulse {
  0%, 100% { filter: brightness(1); }
  50% { filter: brightness(1.18); }
}

/* ---------- анимации плиток ---------- */

.tile-inner.appear {
  animation: appear 0.2s cubic-bezier(0.34, 1.4, 0.64, 1) both;
}

.tile-inner.appear.delayed {
  animation-delay: 0.12s;
}

@keyframes appear {
  from { transform: scale(0); opacity: 0; }
  60% { transform: scale(1.08); opacity: 1; }
  to { transform: scale(1); opacity: 1; }
}

.tile-inner.pop {
  animation: pop 0.22s ease;
}

@keyframes pop {
  0% { transform: scale(1); }
  40% { transform: scale(1.22); }
  100% { transform: scale(1); }
}

/* ---------- оверлей ---------- */

.overlay {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 20px;
  text-align: center;
  border-radius: 18px;
  background: rgba(10, 12, 24, 0.72);
  backdrop-filter: blur(6px);
  transition: opacity 0.25s ease, visibility 0.25s ease;
}

.overlay.hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.overlay h2 {
  margin: 0;
  font-size: 30px;
  font-weight: 800;
  letter-spacing: -0.02em;
  background: linear-gradient(135deg, #fff, #a78bfa);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.overlay.win h2 {
  background: linear-gradient(135deg, #ffd700, #f59e0b, #fff);
  -webkit-background-clip: text;
  background-clip: text;
}

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

.overlay-buttons {
  display: flex;
  gap: 10px;
}

.overlay-buttons button {
  padding: 10px 16px;
  font-size: 14px;
}

.overlay .hidden {
  display: none;
}

/* ---------- подсказка ---------- */

.hint {
  margin: 0;
  text-align: center;
  color: var(--muted);
  font-size: 12.5px;
  line-height: 1.7;
}

kbd {
  padding: 1px 6px;
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  background: rgba(255, 255, 255, 0.08);
  font-family: inherit;
  font-size: 11px;
}

/* ---------- прочее ---------- */

@media (max-height: 760px) {
  body { padding-top: 14px; padding-bottom: 14px; }
  .app { gap: 10px; }
}

@media (prefers-reduced-motion: reduce) {
  .tile,
  .overlay,
  button {
    transition: none;
  }
  .tile-inner.appear,
  .tile-inner.pop,
  .v2048 .tile-inner,
  .v-super .tile-inner,
  .board-wrap.shake,
  .float-score {
    animation: none;
  }
}
