:root {
  /* Pastel Purple/Blue/Pink theme applied */
  /* Logic unchanged */
  /* Visual update only */
  --pink: #f4a6c1;
  --pink-dark: #d97fa3;
  --pink-soft: #fde7f0;
  --blue: #9ecbff;
  --blue-dark: #6fa8e8;
  --blue-soft: #e8f3ff;
  --purple: #c8a2ff;
  --purple-dark: #9a74d6;
  --purple-soft: #f1e8ff;

  --bg: #fcfbff;
  --surface: #ffffff;
  --border: #e8def5;
  --text: #2a2233;
  --text-soft: #6a5f78;

  --button-bg: var(--purple);
  --button-hover: var(--purple-dark);
  --header-bg: linear-gradient(135deg, #f4a6c1, #9ecbff, #c8a2ff);

  --tile-empty: #f7f2fd;
  --board: #f4edf9;

  --tile-2: #fde7f0;
  --tile-4: #e8f3ff;
  --tile-8: #f1e8ff;
  --tile-16: #f4a6c1;
  --tile-32: #9ecbff;
  --tile-64: #c8a2ff;
  --tile-128: #d97fa3;
  --tile-256: #6fa8e8;
  --tile-512: #9a74d6;
  --tile-1024: #7f8fe7;
  --tile-2048: #b27de8;

  --radius-lg: 20px;
  --radius-md: 14px;
  --shadow: 0 14px 30px rgba(154, 116, 214, 0.14);
  --shadow-soft: 0 10px 22px rgba(200, 162, 255, 0.16);
}

* { box-sizing: border-box; }

html,
body {
  margin: 0;
  min-height: 100%;
  overflow: hidden;
}

body {
  font-family: "Segoe UI", Inter, Roboto, Arial, sans-serif;
  color: var(--text);
  background: linear-gradient(160deg, #fcfbff 0%, #fdf5fb 48%, #f1f5ff 100%);
}

.app {
  max-width: 760px;
  margin: 0 auto;
  min-height: 100vh;
  padding: clamp(10px, 2vw, 20px);
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 12px;
}

.brand-header,
.game-shell {
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.brand-header {
  display: grid;
  grid-template-columns: minmax(130px, 180px) 1fr auto;
  gap: 12px;
  align-items: center;
  padding: clamp(10px, 2.2vw, 16px);
  background: var(--header-bg);
  color: #fff;
  border: 1px solid var(--border);
}

.brand-mark-wrap {
  min-height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.brand-logo {
  max-width: 160px;
  width: 100%;
  height: auto;
  object-fit: contain;
}

.logo-fallback { display: none; font-weight: 700; }
.brand-mark-wrap.logo-missing .brand-logo { display: none; }
.brand-mark-wrap.logo-missing .logo-fallback { display: block; }

.brand-kicker {
  margin: 0;
  text-transform: uppercase;
  font-size: 0.75rem;
  letter-spacing: 0.08em;
  opacity: 0.9;
}

.brand-copy h1 {
  margin: 2px 0;
  font-size: clamp(1.35rem, 2.5vw, 1.9rem);
}

.brand-subtitle { margin: 0; opacity: 0.92; }

.top-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.game-shell {
  padding: clamp(10px, 2vw, 18px);
  border: 1px solid var(--border);
  display: grid;
  grid-template-rows: auto auto 1fr;
  gap: 10px;
  position: relative;
  overflow: hidden;
}

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

.score-box {
  background: linear-gradient(180deg, var(--purple-soft), var(--blue-soft));
  border: 1px solid var(--border);
  box-shadow: var(--shadow-soft);
  border-radius: var(--radius-md);
  padding: 8px;
  text-align: center;
}

.label {
  margin: 0;
  font-weight: 700;
  font-size: 0.76rem;
  text-transform: uppercase;
  color: var(--text-soft);
  letter-spacing: 0.07em;
}

.value {
  margin: 3px 0 0;
  font-size: 1.4rem;
  font-weight: 800;
}

.hint {
  margin: 0;
  color: var(--text-soft);
  text-align: center;
  font-size: 0.93rem;
}

.primary-btn,
.ghost-btn {
  border: 0;
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.14s ease, opacity 0.2s ease;
}

.primary-btn {
  background: var(--button-bg);
  color: #fff;
}

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

.ghost-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  color: var(--text);
  background: var(--blue-soft);
  border: 1px solid var(--border);
}

.primary-btn:hover,
.ghost-btn:hover { transform: translateY(-1px); }

.ghost-btn:hover {
  background: var(--pink-soft);
}

.board-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
}

.board {
  --grid-size: min(88vw, 520px);
  --gap: clamp(8px, 1.2vw, 12px);
  --cell: calc((var(--grid-size) - 5 * var(--gap)) / 4);
  width: var(--grid-size);
  height: var(--grid-size);
  padding: var(--gap);
  border-radius: 18px;
  background: var(--board);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-soft);
  position: relative;
  touch-action: none;
}

.grid-cell {
  position: absolute;
  width: var(--cell);
  height: var(--cell);
  border-radius: 10px;
  background: var(--tile-empty);
}

.tile {
  position: absolute;
  width: var(--cell);
  height: var(--cell);
  border-radius: 10px;
  display: grid;
  place-items: center;
  font-weight: 800;
  color: var(--text);
  transition: top 140ms ease, left 140ms ease;
  will-change: top, left;
}

.tile.new { animation: pop-in 140ms ease; }
.tile.merged { animation: merge-pop 200ms ease; }

@keyframes pop-in {
  from { transform: scale(0.6); opacity: 0.4; }
  to { transform: scale(1); opacity: 1; }
}

@keyframes merge-pop {
  0% { transform: scale(0.85); }
  55% { transform: scale(1.12); }
  100% { transform: scale(1); }
}

.tile[data-val="2"] { background: var(--tile-2); color: var(--text); }
.tile[data-val="4"] { background: var(--tile-4); color: var(--text); }
.tile[data-val="8"] { background: var(--tile-8); color: var(--text); }
.tile[data-val="16"] { background: var(--tile-16); color: var(--text); }
.tile[data-val="32"] { background: var(--tile-32); color: var(--text); }
.tile[data-val="64"] { background: var(--tile-64); color: var(--text); }
.tile[data-val="128"] { background: var(--tile-128); color: #fff; }
.tile[data-val="256"] { background: var(--tile-256); color: #fff; }
.tile[data-val="512"] { background: var(--tile-512); color: #fff; }
.tile[data-val="1024"] { background: var(--tile-1024); color: #fff; }
.tile[data-val="2048"] { background: var(--tile-2048); color: #fff; }
.tile[data-val="4096"],
.tile[data-val="8192"] { background: var(--purple-dark); color: #fff; }

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(106, 95, 120, 0.45);
  z-index: 20;
}

.overlay-card {
  width: min(92%, 360px);
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-soft);
  padding: 18px;
  text-align: center;
}

.overlay-card h2 { margin: 0 0 6px; }
.overlay-card p { margin: 0 0 14px; color: var(--text-soft); }
.overlay-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

.hidden { display: none !important; }

@media (max-width: 760px) {
  .app { padding: 8px; gap: 8px; }
  .brand-header { grid-template-columns: 1fr; text-align: center; }
  .top-actions { justify-content: center; }
  .hud-row { grid-template-columns: 1fr 1fr; }
  #restartBtn { grid-column: 1 / -1; }
  .board { --grid-size: min(94vw, 500px); }
}