/* ── Reset & Variables ──────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:        #070e1b;
  --surface:   #0c1525;
  --surface2:  #111e30;
  --border2:   rgba(255,255,255,0.13);
  --accent:    #6c5fff;
  --text:      #e8edf8;
  --muted:     #8892aa;
  --header-h:  52px;
  --radius:    12px;
  --board-gap: 8px;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  touch-action: manipulation;
}

button { font: inherit; cursor: pointer; border: none; background: none; touch-action: manipulation; }

/* ── Screen ──────────────────────────────────────────────────────── */
.screen { display: flex; flex-direction: column; min-height: 100dvh; }

/* ── Header ──────────────────────────────────────────────────────── */
header {
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border2);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  flex-shrink: 0;
  width: 100%;
  position: sticky;
  top: 0;
  z-index: 10;
}

.logo-wrap { display: flex; align-items: center; gap: 8px; }

.logo-back {
  color: var(--muted);
  text-decoration: none;
  font-size: 1.1rem;
  padding: 6px 4px;
  line-height: 1;
  transition: color 0.15s;
}
.logo-back:hover { color: var(--text); }

.logo-title { font-weight: 800; font-size: 1rem; }
.logo-title span { color: var(--accent); }

.header-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.score-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: var(--surface2);
  border: 1px solid var(--border2);
  border-radius: 8px;
  padding: 4px 10px;
  min-width: 52px;
}

.score-lbl {
  font-size: 9px;
  font-weight: 700;
  color: var(--muted);
  letter-spacing: 0.5px;
}

.score-val {
  font-size: 1rem;
  font-weight: 800;
  line-height: 1.2;
  transition: transform 0.12s;
}

.score-val.bump {
  animation: scoreBump 0.2s ease-out;
}

@keyframes scoreBump {
  0%   { transform: scale(1); }
  50%  { transform: scale(1.25); color: #a78bfa; }
  100% { transform: scale(1); }
}

.btn-ghost {
  background: transparent;
  border: 1px solid var(--border2);
  border-radius: 8px;
  color: var(--muted);
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 600;
  transition: color 0.15s, border-color 0.15s;
  white-space: nowrap;
}
.btn-ghost:hover { color: var(--text); border-color: rgba(255,255,255,0.25); }

.btn-icon {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 1px solid var(--border2);
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.15s, border-color 0.15s;
  flex-shrink: 0;
}
.btn-icon:hover { color: var(--text); border-color: rgba(255,255,255,0.25); }

/* ── Game Body ───────────────────────────────────────────────────── */
.game-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px 12px 24px;
  gap: 16px;
}

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

/* ── Board ───────────────────────────────────────────────────────── */
#board {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: var(--board-gap);
  background: rgba(255,255,255,0.07);
  border-radius: 14px;
  padding: var(--board-gap);
  width: min(92vw, 400px);
  height: min(92vw, 400px);
  position: relative;
  flex-shrink: 0;
}

.cell-bg {
  background: rgba(255,255,255,0.04);
  border-radius: 8px;
}

/* ── Tiles ───────────────────────────────────────────────────────── */
.tile {
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: clamp(1.2rem, 6vw, 2rem);
  line-height: 1;
  z-index: 1;
  animation: tileAppear 0.1s ease-out;
}

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

.tile.tile-merge {
  animation: tileMerge 0.15s ease-out;
}

@keyframes tileMerge {
  0%   { transform: scale(1); }
  40%  { transform: scale(1.18); }
  100% { transform: scale(1); }
}

/* Tile colours — dark theme */
.tile[data-val="2"]    { background: #1a2848; color: #c8d2e8; }
.tile[data-val="4"]    { background: #1e3260; color: #c8d2e8; }
.tile[data-val="8"]    { background: #92400e; color: #fde68a; }
.tile[data-val="16"]   { background: #b45309; color: #fde68a; }
.tile[data-val="32"]   { background: #9a1c1c; color: #fecaca; }
.tile[data-val="64"]   { background: #c42020; color: #fff; }
.tile[data-val="128"]  { background: #92700a; color: #fef3c7; font-size: clamp(1rem, 5vw, 1.6rem); }
.tile[data-val="256"]  { background: #b88a0c; color: #fff;    font-size: clamp(1rem, 5vw, 1.6rem); }
.tile[data-val="512"]  { background: #166534; color: #d1fae5; font-size: clamp(1rem, 5vw, 1.6rem); }
.tile[data-val="1024"] { background: #1d4ed8; color: #eff6ff; font-size: clamp(0.8rem, 4vw, 1.25rem); }
.tile[data-val="2048"] { background: var(--accent); color: #fff; font-size: clamp(0.8rem, 4vw, 1.25rem); box-shadow: 0 0 24px rgba(108,95,255,0.55); }
.tile[data-val="4096"] { background: #7c3aed; color: #fff; font-size: clamp(0.75rem, 3.5vw, 1.1rem); }
.tile[data-val="8192"] { background: #be185d; color: #fff; font-size: clamp(0.75rem, 3.5vw, 1.1rem); }

/* ── Overlays ────────────────────────────────────────────────────── */
.overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(7,14,27,0.82);
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 24px;
  backdrop-filter: blur(4px);
}
.overlay.active { display: flex; }

.overlay-card {
  background: var(--surface);
  border: 1px solid var(--border2);
  border-radius: 20px;
  padding: 40px 28px 36px;
  text-align: center;
  max-width: 320px;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  animation: cardIn 0.35s cubic-bezier(0.34,1.56,0.64,1);
}

.overlay-card--info {
  text-align: left;
  padding: 28px 24px 28px;
  gap: 0;
  position: relative;
  max-width: 360px;
}

.overlay-card--info h2 {
  font-size: 1.4rem;
  font-weight: 800;
  margin-bottom: 20px;
  color: var(--text);
}

.info-close {
  position: absolute;
  top: 16px;
  right: 16px;
  color: var(--muted);
  font-size: 14px;
  padding: 4px 8px;
  border-radius: 6px;
  transition: color 0.15s;
}
.info-close:hover { color: var(--text); }

.info-section {
  display: flex;
  gap: 14px;
  align-items: flex-start;
  padding: 12px 0;
  border-bottom: 1px solid var(--border2);
}
.info-section:last-of-type { border-bottom: none; margin-bottom: 8px; }

.info-icon {
  font-size: 1.3rem;
  flex-shrink: 0;
  width: 28px;
  text-align: center;
  margin-top: 1px;
}

.info-section > div > strong {
  display: block;
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 3px;
}

.info-section p strong {
  display: inline;
  color: var(--text);
}

.info-section p {
  font-size: 0.85rem;
  color: var(--muted);
  line-height: 1.5;
  margin: 0;
}

.info-example {
  display: inline-block;
  background: rgba(108,95,255,0.15);
  color: var(--accent);
  font-weight: 700;
  padding: 1px 7px;
  border-radius: 5px;
  font-size: 0.8rem;
}

.overlay-card--info .btn-primary {
  margin-top: 8px;
}

@keyframes cardIn {
  from { opacity: 0; transform: scale(0.88) translateY(20px); }
  to   { opacity: 1; transform: scale(1) translateY(0); }
}

.overlay-emoji { font-size: 3rem; margin-bottom: 4px; }
.overlay-card h2 { font-size: 2rem; font-weight: 800; }
.overlay-card p  { color: var(--muted); font-size: 0.95rem; margin-bottom: 8px; }

.btn-primary {
  width: 100%;
  background: linear-gradient(135deg, #6c5fff, #a855f7);
  border-radius: var(--radius);
  color: #fff;
  font-weight: 700;
  padding: 14px 32px;
  font-size: 1rem;
  transition: opacity 0.15s, transform 0.1s;
  margin-top: 4px;
}
.btn-primary:hover  { opacity: 0.9; }
.btn-primary:active { transform: scale(0.97); }

.btn-ghost-lg {
  width: 100%;
  background: transparent;
  border: 1px solid var(--border2);
  border-radius: var(--radius);
  color: var(--muted);
  padding: 12px 32px;
  font-size: 0.95rem;
  font-weight: 600;
  transition: color 0.15s, border-color 0.15s;
  margin-top: 4px;
}
.btn-ghost-lg:hover { color: var(--text); border-color: rgba(255,255,255,0.25); }

/* ── Responsive ──────────────────────────────────────────────────── */
@media (max-height: 680px) {
  .game-body { justify-content: flex-start; padding-top: 10px; gap: 10px; }
  .hint-text  { display: none; }
}
