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

html, body {
  margin: 0; padding: 0;
  font-family: "Clear Sans", "Helvetica Neue", Arial, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background: #faf8ef;
  color: #776e65;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: contain;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  justify-content: center;
  padding: 20px 16px max(20px, env(safe-area-inset-bottom));
}

.container {
  width: 100%;
  max-width: 460px;
  display: flex;
  flex-direction: column;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 16px;
}

.title {
  margin: 0;
  font-size: 64px;
  font-weight: 800;
  color: #776e65;
  line-height: 1;
  letter-spacing: -1px;
}

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

.score-box {
  background: #bbada0;
  color: #fff;
  padding: 8px 16px;
  border-radius: 6px;
  text-align: center;
  min-width: 72px;
}

.score-label {
  font-size: 11px;
  color: #eee4da;
  text-transform: uppercase;
  letter-spacing: 1px;
  font-weight: 700;
}

.score-value {
  font-size: 22px;
  font-weight: 800;
  line-height: 1.2;
}

.intro {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
}

.intro p {
  margin: 0;
  font-size: 13px;
  line-height: 1.5;
  flex: 1;
  color: #776e65;
}

.intro p strong { color: #5b554e; }

.restart-btn {
  background: #8f7a66;
  color: #f9f6f2;
  border: none;
  padding: 10px 18px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
  transition: background 120ms ease, transform 80ms ease;
}
.restart-btn:hover { background: #9f8b77; }
.restart-btn:active { transform: scale(0.96); }

.game-container {
  position: relative;
  background: #bbada0;
  border-radius: 10px;
  padding: 12px;
  width: 100%;
  aspect-ratio: 1 / 1;
  touch-action: none;
  box-shadow: 0 4px 16px rgba(143, 122, 102, 0.18);
}

.grid-background {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 12px;
  width: 100%;
  height: 100%;
}

.grid-cell {
  background: rgba(238, 228, 218, 0.35);
  border-radius: 6px;
}

.tile-container {
  position: absolute;
  inset: 12px;
  pointer-events: none;
}

.tile {
  position: absolute;
  left: 0;
  top: 0;
  transition: transform 130ms ease-in-out;
  will-change: transform;
}

.tile-inner {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  border-radius: 6px;
  background: #eee4da;
  color: #776e65;
  font-size: clamp(20px, 7vw, 38px);
  line-height: 1;
}

.tile-2   .tile-inner { background: #eee4da; color: #776e65; }
.tile-4   .tile-inner { background: #ede0c8; color: #776e65; }
.tile-8   .tile-inner { background: #f2b179; color: #f9f6f2; }
.tile-16  .tile-inner { background: #f59563; color: #f9f6f2; }
.tile-32  .tile-inner { background: #f67c5f; color: #f9f6f2; }
.tile-64  .tile-inner { background: #f65e3b; color: #f9f6f2; }
.tile-128 .tile-inner { background: #edcf72; color: #f9f6f2; font-size: clamp(17px, 6vw, 32px); box-shadow: 0 0 28px 4px rgba(243, 215, 116, 0.24), inset 0 0 0 1px rgba(255, 255, 255, 0.14); }
.tile-256 .tile-inner { background: #edcc61; color: #f9f6f2; font-size: clamp(17px, 6vw, 32px); box-shadow: 0 0 28px 4px rgba(243, 215, 116, 0.32), inset 0 0 0 1px rgba(255, 255, 255, 0.19); }
.tile-512 .tile-inner { background: #edc850; color: #f9f6f2; font-size: clamp(17px, 6vw, 32px); box-shadow: 0 0 28px 4px rgba(243, 215, 116, 0.40), inset 0 0 0 1px rgba(255, 255, 255, 0.24); }
.tile-1024 .tile-inner { background: #edc53f; color: #f9f6f2; font-size: clamp(14px, 5vw, 26px); box-shadow: 0 0 28px 6px rgba(243, 215, 116, 0.48), inset 0 0 0 1px rgba(255, 255, 255, 0.29); }
.tile-2048 .tile-inner { background: #edc22e; color: #f9f6f2; font-size: clamp(14px, 5vw, 26px); box-shadow: 0 0 30px 8px rgba(243, 215, 116, 0.56), inset 0 0 0 1px rgba(255, 255, 255, 0.33); }
.tile-super .tile-inner { background: #3c3a32; color: #f9f6f2; font-size: clamp(12px, 4.5vw, 22px); }

@keyframes appear {
  0%   { transform: scale(0); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}
.tile-new .tile-inner { animation: appear 200ms ease 50ms backwards; }

@keyframes pop {
  0%   { transform: scale(1);   }
  50%  { transform: scale(1.18); }
  100% { transform: scale(1);   }
}
.tile-merged { z-index: 20; }
.tile-merged .tile-inner { animation: pop 220ms ease-out 110ms; }

.game-overlay {
  position: absolute;
  inset: 0;
  background: rgba(238, 228, 218, 0.78);
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  gap: 18px;
  border-radius: 10px;
  z-index: 100;
  animation: fadein 600ms ease;
}
.game-overlay[hidden] { display: none; }
.game-overlay.won {
  background: rgba(237, 194, 46, 0.55);
}
.overlay-message {
  font-size: clamp(36px, 9vw, 56px);
  font-weight: 800;
  color: #776e65;
  text-align: center;
}
.game-overlay.won .overlay-message { color: #f9f6f2; text-shadow: 0 2px 8px rgba(0,0,0,0.15); }

.overlay-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
}
.overlay-btn {
  border: none;
  padding: 12px 22px;
  border-radius: 6px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  transition: background 120ms ease, transform 80ms ease;
}
.overlay-btn.primary {
  background: #8f7a66;
  color: #f9f6f2;
}
.overlay-btn.primary:hover { background: #9f8b77; }
.overlay-btn.secondary {
  background: rgba(255, 255, 255, 0.7);
  color: #776e65;
}
.overlay-btn.secondary:hover { background: rgba(255, 255, 255, 0.9); }
.overlay-btn:active { transform: scale(0.96); }

.footer {
  margin-top: 16px;
  text-align: center;
}
.sound-btn {
  background: none;
  border: 1px solid #d6cdc4;
  color: #998a7e;
  font-size: 13px;
  padding: 6px 14px;
  border-radius: 999px;
  cursor: pointer;
  transition: background 120ms ease, color 120ms ease;
}
.sound-btn:hover { background: rgba(238, 228, 218, 0.6); color: #776e65; }

@keyframes fadein {
  from { opacity: 0; }
  to   { opacity: 1; }
}

body.no-transition .tile { transition: none !important; }

@media (max-width: 380px) {
  body { padding: 12px; }
  .header { margin-bottom: 12px; }
  .title { font-size: 48px; }
  .score-box { padding: 6px 12px; min-width: 62px; }
  .score-value { font-size: 18px; }
  .intro { flex-direction: column; align-items: stretch; gap: 8px; margin-bottom: 12px; }
  .restart-btn { align-self: flex-end; }
}
