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

:root {
  --ink: #14201c;
  --muted: #4d635a;
  --paper: #eef5f1;
  --teal: #1f6f5b;
  --coral: #e07a5f;
  --sky: #3d8ea5;
  --line: rgba(20, 32, 28, 0.12);
  --board-bg: #bbada0;
  --cell-empty: rgba(238, 228, 218, 0.35);
  --cell-2: #eee4da;
  --cell-4: #ede0c8;
  --cell-8: #f2b179;
  --cell-16: #f59563;
  --cell-32: #f67c5f;
  --cell-64: #f65e3b;
  --cell-128: #edcf72;
  --cell-256: #edcc61;
  --cell-512: #edc850;
  --cell-1024: #edc53f;
  --cell-2048: #edc22e;
  --cell-super: #3c3a32;
}

html,
body {
  height: 100%;
}

body {
  font-family: "IBM Plex Sans", "Microsoft YaHei", "PingFang SC", sans-serif;
  color: var(--ink);
  background:
    radial-gradient(900px 480px at 0% 0%, rgba(224, 122, 95, 0.25), transparent 60%),
    radial-gradient(800px 420px at 100% 10%, rgba(61, 142, 165, 0.22), transparent 55%),
    var(--paper);
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding:
    max(0px, env(safe-area-inset-top))
    max(0px, env(safe-area-inset-right))
    max(0px, env(safe-area-inset-bottom))
    max(0px, env(safe-area-inset-left));
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

#box {
  position: relative;
  width: min(480px, 100vw);
  max-height: 100dvh;
  padding: clamp(0.7rem, 2.8vw, 1.2rem);
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.back-home {
  align-self: flex-start;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--teal);
  padding: 0.35rem 0.6rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.5);
}

.title-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title-row h1 {
  font-family: "Syne", sans-serif;
  font-weight: 800;
  font-size: clamp(2.4rem, 10vw, 3.4rem);
  color: var(--coral);
  letter-spacing: -0.03em;
}

.score-box {
  display: flex;
  gap: 0.5rem;
}

.score-cell {
  background: var(--board-bg);
  color: #fff;
  border-radius: 10px;
  padding: 0.4rem 0.7rem;
  min-width: 72px;
  text-align: center;
}

.score-label {
  font-size: 0.72rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  opacity: 0.85;
}

.score-val {
  font-weight: 800;
  font-size: 1.1rem;
}

.sub-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.8rem;
}

.sub-row .tip {
  color: var(--muted);
  font-size: 0.9rem;
}

.btn-restart {
  font: inherit;
  font-weight: 700;
  padding: 0.45rem 0.9rem;
  border: none;
  border-radius: 10px;
  background: var(--coral);
  color: #fff;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(224, 122, 95, 0.35);
}

.btn-restart:active {
  background: #cc6a51;
}

.board {
  position: relative;
  width: min(100%, calc(100dvh - 250px));
  aspect-ratio: 1;
  margin: 0 auto;
  background: var(--board-bg);
  border-radius: 14px;
  padding: 3%;
  touch-action: none;
  box-shadow: 0 8px 30px rgba(20, 32, 28, 0.18);
}

.grid-bg {
  position: absolute;
  inset: 3%;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 2.8%;
  pointer-events: none;
}

.grid-bg::before,
.grid-bg::after,
.grid-bg {
  /* each cell filled via extra spans inserted by JS */
}

.grid-bg .cell {
  background: var(--cell-empty);
  border-radius: 8px;
}

.tiles {
  position: absolute;
  inset: 3%;
}

.tile {
  position: absolute;
  width: calc(25% - 2.1%);
  height: calc(25% - 2.1%);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: clamp(1.2rem, 5.5vw, 2rem);
  background: var(--cell-2);
  color: #776e65;
  transition: transform 0.12s ease;
}

.tile.dark { color: #f9f6f2; }
.tile.n2 { background: var(--cell-2); }
.tile.n4 { background: var(--cell-4); }
.tile.n8 { background: var(--cell-8); }
.tile.n16 { background: var(--cell-16); }
.tile.n32 { background: var(--cell-32); }
.tile.n64 { background: var(--cell-64); }
.tile.n128 { background: var(--cell-128); }
.tile.n256 { background: var(--cell-256); }
.tile.n512 { background: var(--cell-512); }
.tile.n1024 { background: var(--cell-1024); font-size: clamp(1rem, 4.6vw, 1.7rem); }
.tile.n2048 { background: var(--cell-2048); font-size: clamp(1rem, 4.6vw, 1.7rem); }
.tile.nsuper { background: var(--cell-super); font-size: clamp(0.9rem, 4vw, 1.5rem); }

.tile.n8, .tile.n16, .tile.n32, .tile.n64, .tile.n128, .tile.n256, .tile.n512, .tile.n1024, .tile.n2048, .tile.nsuper { color: #f9f6f2; }

.tile.pop {
  animation: pop 0.16s ease;
}

@keyframes pop {
  0% { transform: scale(0.9); }
  60% { transform: scale(1.06); }
  100% { transform: scale(1); }
}

.overlay {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(238, 228, 218, 0.7);
  backdrop-filter: blur(3px);
  border-radius: 14px;
  padding: 16px;
}

.overlay.show {
  display: flex;
}

.overlay-card {
  text-align: center;
  background: #fff;
  border-radius: 14px;
  padding: 1.4rem 1.6rem;
  max-width: 82%;
  box-shadow: 0 10px 30px rgba(20, 32, 28, 0.18);
}

.overlay-card h2 {
  font-size: 1.6rem;
  color: var(--coral);
  margin-bottom: 0.5rem;
}

.overlay-card p {
  color: var(--muted);
  margin-bottom: 1rem;
  line-height: 1.4;
}

.dpad {
  display: grid;
  grid-template-columns: repeat(3, 56px);
  grid-template-rows: repeat(3, 56px);
  gap: 0.4rem;
  justify-content: center;
  margin: 0 auto;
}

.dpad-btn {
  font-size: 1.4rem;
  line-height: 1;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.6);
  color: var(--coral);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.dpad-btn:active {
  background: var(--coral);
  color: #fff;
}

.dpad .up { grid-column: 2; grid-row: 1; }
.dpad .left { grid-column: 1; grid-row: 2; }
.dpad .down { grid-column: 2; grid-row: 3; }
.dpad .right { grid-column: 3; grid-row: 2; }

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

@media (max-height: 640px) {
  .board {
    width: min(100%, calc(100dvh - 180px));
  }

  .dpad {
    grid-template-columns: repeat(3, 44px);
    grid-template-rows: repeat(3, 44px);
  }

  .hint { display: none; }
}
