:root {
  --bg: #f6efe5;
  --paper: #fffaf2;
  --ink: #3f3429;
  --muted: #7b6d60;
  --line: #d9c8b6;
  --board: #b9aa9a;
  --cell: #cec0b2;
  --accent: #ed8f42;
  --accent-dark: #d9772d;
  --shadow: 0 18px 40px rgba(67, 50, 35, 0.16);
  --game-scale: 1;
  --board-size: 460px;
  --tile-size: calc((var(--board-size) - 52px) / 4);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  overscroll-behavior-y: none;
  touch-action: none;
  -webkit-overflow-scrolling: auto;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  background: radial-gradient(circle at 18% 10%, #fff6da 0 18%, transparent 36%), var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button {
  min-height: 42px;
  border: 0;
  border-radius: 8px;
  padding: 0 16px;
  color: #fff;
  background: #887564;
  font: inherit;
  font-size: 15px;
  line-height: 1.08;
  font-weight: 750;
  cursor: pointer;
  transition: transform 120ms ease, background 120ms ease, opacity 120ms ease;
}

.ad-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.ad-button img {
  width: 22px;
  height: 22px;
  flex: 0 0 22px;
  border-radius: 6px;
}

.ad-button span,
button span {
  min-width: 0;
  max-width: 100%;
}

button:hover {
  transform: translateY(-1px);
}

button:active {
  transform: translateY(1px);
}

button:disabled {
  cursor: default;
  opacity: 0.48;
  transform: none;
}

.primary-btn {
  background: var(--accent);
}

.primary-btn:hover {
  background: var(--accent-dark);
}

.app-shell {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 620px;
  height: 720px;
  padding: 18px 30px;
  display: grid;
  grid-template-rows: auto auto minmax(0, auto) auto auto;
  align-content: center;
  gap: 10px;
  transform: translate(-50%, -50%) scale(var(--game-scale));
  transform-origin: center center;
  touch-action: none;
  user-select: none;
}

.top-bar {
  width: 560px;
  margin: 0 auto;
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 14px;
}

.title-block {
  min-width: 0;
  flex: 1 1 auto;
}

h1 {
  margin: 0;
  font-size: 58px;
  line-height: 0.92;
  font-weight: 900;
  letter-spacing: 0;
}

.subtitle,
.hint {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 14px;
  font-weight: 650;
}

.subtitle {
  white-space: nowrap;
}

.score-row {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.score-box {
  min-width: 78px;
  padding: 7px 9px;
  border: 1px solid rgba(120, 99, 78, 0.18);
  border-radius: 8px;
  background: rgba(255, 250, 242, 0.76);
  box-shadow: 0 8px 20px rgba(88, 68, 48, 0.08);
  text-align: center;
}

.score-box span {
  display: block;
  color: var(--muted);
  font-size: 11px;
  font-weight: 850;
  line-height: 1.1;
  text-transform: uppercase;
}

.score-box strong {
  display: block;
  min-width: 42px;
  margin-top: 2px;
  font-size: 23px;
  line-height: 1;
}

.controls {
  width: 560px;
  margin: 0 auto;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.controls button {
  flex: 0 1 auto;
  min-width: 142px;
  max-width: min(260px, 100%);
  padding: 0 14px;
  white-space: normal;
}

.controls .ad-button {
  min-width: 220px;
}

.board-wrap {
  position: relative;
  width: var(--board-size);
  height: var(--board-size);
  max-width: 100%;
  margin: 0 auto;
}

.board {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 8px;
  padding: 10px;
  background: var(--board);
  box-shadow: var(--shadow);
  touch-action: none;
  user-select: none;
}

.cell,
.tile {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--tile-size);
  height: var(--tile-size);
  border-radius: 8px;
}

.cell {
  background: rgba(238, 228, 218, 0.36);
}

.tile {
  display: grid;
  place-items: center;
  color: #f8f6f2;
  font-weight: 900;
  font-size: clamp(22px, calc(var(--tile-size) * 0.34), 48px);
  line-height: 1;
  box-shadow: inset 0 -4px rgba(0, 0, 0, 0.08), 0 8px 12px rgba(76, 56, 38, 0.12);
  transform: translate(var(--x), var(--y)) scale(1);
  transition: transform 240ms cubic-bezier(0.22, 0.72, 0.18, 1), opacity 180ms ease;
}

.tile.no-transition {
  transition: none;
}

.tile.new {
  animation: pop 280ms cubic-bezier(0.18, 0.82, 0.22, 1);
}

.tile.merged {
  animation: merge 180ms ease-out;
}

.tile-2 { color: #776e65; background: #eee4da; }
.tile-4 { color: #776e65; background: #ede0c8; }
.tile-8 { background: #f2b179; }
.tile-16 { background: #f59563; }
.tile-32 { background: #f67c5f; }
.tile-64 { background: #f65e3b; }
.tile-128 { background: #edcf72; font-size: clamp(20px, calc(var(--tile-size) * 0.30), 42px); }
.tile-256 { background: #edcc61; font-size: clamp(20px, calc(var(--tile-size) * 0.30), 42px); }
.tile-512 { background: #edc850; font-size: clamp(20px, calc(var(--tile-size) * 0.30), 42px); }
.tile-1024 { background: #edc53f; font-size: clamp(17px, calc(var(--tile-size) * 0.24), 34px); }
.tile-2048 { background: #edc22e; font-size: clamp(17px, calc(var(--tile-size) * 0.24), 34px); }
.tile-super { background: #3c3a32; font-size: clamp(16px, calc(var(--tile-size) * 0.22), 30px); }

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: rgba(250, 246, 238, 0.78);
  backdrop-filter: blur(3px);
  z-index: 4;
}

.overlay-panel {
  width: min(86%, 340px);
  padding: 22px;
  border: 1px solid rgba(94, 73, 54, 0.18);
  border-radius: 8px;
  background: var(--paper);
  box-shadow: 0 18px 34px rgba(67, 50, 35, 0.18);
  text-align: center;
}

.overlay-panel h2 {
  margin: 0;
  font-size: 26px;
}

.overlay-panel p {
  margin: 8px 0 18px;
  color: var(--muted);
  font-weight: 650;
}

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

.overlay-actions button {
  min-width: 128px;
}

.hint {
  text-align: center;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: max(16px, env(safe-area-inset-bottom));
  z-index: 10;
  width: min(calc(100vw - 28px), 440px);
  padding: 12px 14px;
  border-radius: 8px;
  color: #fff;
  background: rgba(59, 48, 38, 0.94);
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.2);
  font-weight: 750;
  text-align: center;
  transform: translateX(-50%);
}

.hidden {
  display: none;
}

@keyframes pop {
  0% { transform: translate(var(--x), var(--y)) scale(0.35); opacity: 0; }
  100% { transform: translate(var(--x), var(--y)) scale(1); opacity: 1; }
}

@keyframes merge {
  0% { transform: translate(var(--x), var(--y)) scale(1); }
  55% { transform: translate(var(--x), var(--y)) scale(1.13); }
  100% { transform: translate(var(--x), var(--y)) scale(1); }
}
