* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  background: #08060f;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Orbitron', monospace;
  overflow: hidden;
}

.back-link {
  position: fixed;
  top: 16px;
  left: 16px;
  color: rgba(160, 180, 255, 0.6);
  font-size: 0.7rem;
  text-decoration: none;
  letter-spacing: 0.05em;
  z-index: 100;
  transition: color 0.2s;
}

.back-link:hover {
  color: #818cf8;
}

#game-container {
  width: min(100vw, 400px);
  padding: 20px;
}

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

h1 {
  font-size: 1.5rem;
  font-weight: 900;
  letter-spacing: 0.08em;
  background: linear-gradient(135deg, #818cf8, #c084fc, #f472b6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

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

.score-box {
  background: rgba(30, 20, 50, 0.9);
  border: 1px solid rgba(140, 100, 220, 0.3);
  border-radius: 8px;
  padding: 6px 14px;
  text-align: center;
  min-width: 72px;
}

.score-box .label {
  display: block;
  font-size: 0.45rem;
  letter-spacing: 0.12em;
  color: rgba(180, 160, 220, 0.55);
  margin-bottom: 2px;
}

.score-box span:last-child {
  font-size: 0.95rem;
  font-weight: 700;
  color: #e9d5ff;
  text-shadow: 0 0 8px rgba(200, 120, 255, 0.5);
  display: inline-block;
}

#score.bump {
  animation: score-bump 0.35s ease;
}

@keyframes score-bump {
  0% { transform: scale(1); }
  40% { transform: scale(1.25); color: #fde68a; }
  100% { transform: scale(1); }
}

.hint {
  font-size: 0.58rem;
  color: rgba(150, 140, 200, 0.5);
  margin-bottom: 14px;
  letter-spacing: 0.04em;
}

#board {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  background: rgba(15, 10, 28, 0.95);
  border-radius: 14px;
  border: 1px solid rgba(130, 100, 200, 0.25);
  box-shadow:
    0 0 40px rgba(129, 140, 248, 0.12),
    inset 0 0 30px rgba(0, 0, 0, 0.3);
  touch-action: none;
  overflow: hidden;
}

#grid-bg {
  position: absolute;
  inset: 12px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 10px;
}

.cell-bg {
  background: rgba(40, 30, 70, 0.5);
  border-radius: 10px;
  border: 1px solid rgba(100, 80, 160, 0.15);
}

#tiles {
  position: absolute;
  inset: 12px;
  pointer-events: none;
}

#fx-layer {
  position: absolute;
  inset: 12px;
  pointer-events: none;
  z-index: 5;
  overflow: hidden;
}

.tile {
  position: absolute;
  top: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  font-weight: 700;
  border: 1px solid rgba(255, 255, 255, 0.12);
  will-change: transform;
  z-index: 2;
}

.tile.merging {
  animation: merge-pulse 0.22s ease 0.05s;
  z-index: 3;
}

.tile.merging-away {
  z-index: 1;
  opacity: 0.85;
}

@keyframes merge-pulse {
  0% { filter: brightness(1); }
  40% { filter: brightness(1.7); box-shadow: 0 0 32px rgba(255, 255, 255, 0.55); }
  100% { filter: brightness(1); }
}

.spark {
  position: absolute;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: #fde68a;
  box-shadow: 0 0 8px #fbbf24;
  animation: spark-fly 0.4s ease-out forwards;
  margin-left: -2px;
  margin-top: -2px;
}

@keyframes spark-fly {
  0% { transform: translate(0, 0) scale(1); opacity: 1; }
  100% { transform: translate(var(--dx), var(--dy)) scale(0); opacity: 0; }
}

.merge-flash {
  position: absolute;
  border-radius: 10px;
  background: radial-gradient(circle, rgba(255,255,255,0.45) 0%, transparent 70%);
  animation: flash-fade 0.35s ease-out forwards;
  pointer-events: none;
}

@keyframes flash-fade {
  0% { opacity: 1; transform: scale(0.8); }
  100% { opacity: 0; transform: scale(1.3); }
}

.tile.v2    { background: #1e1b2e; color: #c4b5fd; box-shadow: 0 0 10px rgba(129,140,248,0.2); font-size: 1.4rem; }
.tile.v4    { background: #2e1065; color: #ddd6fe; box-shadow: 0 0 12px rgba(139,92,246,0.3); font-size: 1.4rem; }
.tile.v8    { background: #4c1d95; color: #ede9fe; box-shadow: 0 0 14px rgba(124,58,237,0.4); font-size: 1.3rem; }
.tile.v16   { background: #5b21b6; color: #fff; box-shadow: 0 0 16px rgba(109,40,217,0.5); font-size: 1.2rem; }
.tile.v32   { background: #6d28d9; color: #fff; box-shadow: 0 0 16px rgba(124,58,237,0.55); font-size: 1.2rem; }
.tile.v64   { background: #7c3aed; color: #fff; box-shadow: 0 0 18px rgba(139,92,246,0.6); font-size: 1.1rem; }
.tile.v128  { background: #8b5cf6; color: #fff; box-shadow: 0 0 20px rgba(167,139,250,0.65); font-size: 1rem; }
.tile.v256  { background: #a78bfa; color: #1e1b2e; box-shadow: 0 0 22px rgba(167,139,250,0.7); font-size: 1rem; }
.tile.v512  { background: #c084fc; color: #1e1b2e; box-shadow: 0 0 24px rgba(192,132,252,0.75); font-size: 0.95rem; }
.tile.v1024 { background: #e879f9; color: #fff; box-shadow: 0 0 26px rgba(232,121,249,0.8); font-size: 0.85rem; }
.tile.v2048 { background: linear-gradient(135deg, #fbbf24, #f59e0b); color: #1e1b2e; box-shadow: 0 0 30px rgba(251,191,36,0.9); font-size: 0.85rem; }
.tile.v4096,
.tile.v8192 { background: linear-gradient(135deg, #f472b6, #ec4899); color: #fff; box-shadow: 0 0 32px rgba(244,114,182,0.9); font-size: 0.75rem; }

#overlay {
  position: absolute;
  inset: 0;
  background: rgba(8, 6, 16, 0.75);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
  transition: opacity 0.3s;
}

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

.overlay-box {
  text-align: center;
  padding: 24px;
}

#overlay h2 {
  font-size: 1.3rem;
  letter-spacing: 0.1em;
  margin-bottom: 10px;
}

#overlay h2.win {
  color: #fbbf24;
  text-shadow: 0 0 16px rgba(251, 191, 36, 0.6);
}

#overlay h2.lose {
  color: #f87171;
  text-shadow: 0 0 16px rgba(248, 113, 113, 0.5);
}

#overlay-msg {
  color: rgba(200, 190, 230, 0.75);
  font-size: 0.75rem;
  margin-bottom: 20px;
}

button {
  font-family: 'Orbitron', monospace;
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  padding: 12px 28px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  background: linear-gradient(135deg, #6366f1, #a855f7);
  color: white;
  box-shadow: 0 0 24px rgba(129, 140, 248, 0.4);
  transition: transform 0.15s;
  margin: 4px;
}

button:hover {
  transform: scale(1.04);
}

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

#new-game-btn {
  display: block;
  width: 100%;
  margin-top: 14px;
}
