:root {
  --bg: #faf8ef;
  --panel: #bbada0;
  --panel-soft: #cdc1b4;
  --text: #776e65;
  --tile-light: #f9f6f2;
  --tile-dark: #776e65;
  --gap: 10px;
  --radius: 12px;
}

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

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  min-height: 100%;
  overflow: hidden;
  touch-action: none;
  background: radial-gradient(circle at 20% 15%, #fffdf8, #f5efe2 60%, #efe7d4);
  color: var(--text);
  font-family: "Avenir Next", "Segoe UI", sans-serif;
  overscroll-behavior: none;
}

body {
  display: flex;
  justify-content: center;
  position: fixed;
  inset: 0;
  width: 100%;
  overflow: hidden;
  touch-action: none;
  padding: max(14px, env(safe-area-inset-top)) 14px max(14px, env(safe-area-inset-bottom));
}

.app {
  width: min(96vw, 460px);
  max-height: 100%;
  display: grid;
  gap: 14px;
  align-content: start;
}

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

.home-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 10px 14px;
  min-width: 44px;
  min-height: 44px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.08);
  color: inherit;
  text-decoration: none;
  font-weight: 700;
  font-size: 0.95rem;
  transition: background 150ms ease, transform 80ms ease;
  touch-action: manipulation;
}

.home-btn:active {
  transform: scale(0.96);
  background: rgba(0, 0, 0, 0.12);
}

.home-btn .icon {
  font-size: 1.1rem;
  line-height: 1;
}

h1 {
  margin: 0;
  font-size: clamp(1.7rem, 6.8vw, 2.5rem);
  letter-spacing: 0.03em;
}

.hud {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 10px;
  align-items: stretch;
}

.score-card {
  background: #bbada0;
  color: #fff;
  border-radius: 10px;
  padding: 8px 10px;
  text-align: center;
}

.score-card .label {
  display: block;
  font-size: 0.72rem;
  opacity: 0.9;
  text-transform: uppercase;
}

.score-card .value {
  display: block;
  font-size: 1.2rem;
  font-weight: 700;
}

.btn {
  border: 0;
  border-radius: 10px;
  background: #8f7a66;
  color: #fff;
  font-weight: 700;
  padding: 10px 12px;
  cursor: pointer;
  touch-action: manipulation;
}

.btn:active {
  transform: translateY(1px);
}

.game-area {
  position: relative;
}

.board {
  background: var(--panel);
  border-radius: var(--radius);
  padding: var(--gap);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--gap);
  aspect-ratio: 1 / 1;
  touch-action: none;
  user-select: none;
}

.tile {
  border-radius: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: clamp(1.05rem, 7vw, 2rem);
  line-height: 1;
  background: var(--panel-soft);
  color: transparent;
  transition: background-color 130ms ease, color 130ms ease;
}

.tile.has-value {
  color: var(--tile-dark);
}

.tile-v-2 {
  background: #eee4da;
}

.tile-v-4 {
  background: #ede0c8;
}

.tile-v-8 {
  background: #f2b179;
  color: var(--tile-light);
}

.tile-v-16 {
  background: #f59563;
  color: var(--tile-light);
}

.tile-v-32 {
  background: #f67c5f;
  color: var(--tile-light);
}

.tile-v-64 {
  background: #f65e3b;
  color: var(--tile-light);
}

.tile-v-128 {
  background: #edcf72;
  color: var(--tile-light);
  font-size: clamp(0.95rem, 6vw, 1.55rem);
}

.tile-v-256 {
  background: #edcc61;
  color: var(--tile-light);
  font-size: clamp(0.95rem, 6vw, 1.55rem);
}

.tile-v-512 {
  background: #edc850;
  color: var(--tile-light);
  font-size: clamp(0.95rem, 6vw, 1.55rem);
}

.tile-v-1024 {
  background: #edc53f;
  color: var(--tile-light);
  font-size: clamp(0.82rem, 5vw, 1.25rem);
}

.tile-v-2048 {
  background: #edc22e;
  color: var(--tile-light);
  font-size: clamp(0.82rem, 5vw, 1.25rem);
}

.tile-v-super {
  background: #3c3a32;
  color: var(--tile-light);
  font-size: clamp(0.82rem, 5vw, 1.15rem);
}

.overlay {
  position: absolute;
  inset: 0;
  border-radius: var(--radius);
  background: rgba(238, 228, 218, 0.86);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 12px;
  text-align: center;
  padding: 18px;
}

.overlay.hidden {
  display: none;
}

.overlay-text {
  margin: 0;
  font-weight: 800;
  font-size: clamp(1.15rem, 5.2vw, 1.9rem);
}

.overlay-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
}

.hint {
  margin: 0;
  text-align: center;
  color: #8f7a66;
  font-size: 0.95rem;
}

@media (max-width: 360px) {
  .hud {
    grid-template-columns: 1fr 1fr;
  }

  #restart-btn {
    grid-column: 1 / -1;
  }
}
