@font-face {
  font-family: 'Space Mono';
  src: local('Space Mono');
}

:root {
  /* Base palette (fallback if not running inside Telegram) */
  --tg-bg: #12141c;
  --tg-bg-secondary: #1a1e2a;
  --tg-text: #f4f4f6;
  --tg-hint: #8991a8;
  --tg-link: #6366f1;
  --tg-button: #6366f1;
  --tg-button-text: #ffffff;

  --board-bg: #1c2030;
  --cell-bg: rgba(255, 255, 255, 0.045);
  --accent: #6366f1;

  /* Thermal / aurora tile scale: value grows -> teal -> violet -> rose */
  --tile-2: #1f6f66;
  --tile-4: #17877d;
  --tile-8: #159c9c;
  --tile-16: #2c9bd6;
  --tile-32: #5b8ce0;
  --tile-64: #7c72e6;
  --tile-128: #9a5fe0;
  --tile-256: #b854d6;
  --tile-512: #d34fb8;
  --tile-1024: #e84f8e;
  --tile-2048: #f4436b;
  --tile-super: #ff8a3d;

  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 10px;

  --font-ui: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'Space Mono', 'JetBrains Mono', ui-monospace, 'SF Mono', Menlo, monospace;
}

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

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overscroll-behavior: none;
  background: var(--tg-bg);
  color: var(--tg-text);
  font-family: var(--font-ui);
}

.app {
  position: relative;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 14px 16px calc(20px + env(safe-area-inset-bottom));
  overflow: hidden;
}

/* ---------- Signature watermark: the doubling sequence ---------- */
.watermark {
  position: absolute;
  top: -6px;
  left: 0;
  right: 0;
  white-space: nowrap;
  font-family: var(--font-mono);
  font-size: 42px;
  font-weight: 700;
  letter-spacing: 10px;
  color: var(--accent);
  opacity: 0.06;
  pointer-events: none;
  transform: rotate(-2deg) translateX(-2%);
  user-select: none;
}

/* ---------- Top bar ---------- */
.topbar {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  z-index: 1;
  padding: 4px 2px 10px;
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 6px;
}

.brand-mark {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--tg-hint);
  opacity: 0.8;
}

.brand-name {
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: 20px;
  letter-spacing: 1px;
}

.stat-group {
  display: flex;
  gap: 8px;
}

.stat {
  background: var(--tg-bg-secondary);
  border-radius: var(--radius-sm);
  padding: 6px 12px;
  text-align: center;
  min-width: 64px;
}

.stat-label {
  display: block;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--tg-hint);
}

.stat-value {
  display: block;
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: 17px;
  line-height: 1.4;
}

/* ---------- Toolbar ---------- */
.toolbar {
  display: flex;
  gap: 8px;
  z-index: 1;
  margin-bottom: 14px;
}

.btn {
  flex: 1;
  border: none;
  border-radius: var(--radius-md);
  padding: 11px 14px;
  font-family: var(--font-ui);
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  transition: transform 0.08s ease, opacity 0.15s ease;
}

.btn:active { transform: scale(0.97); }

.btn-primary {
  background: var(--tg-button);
  color: var(--tg-button-text);
}

.btn-ghost {
  background: var(--tg-bg-secondary);
  color: var(--tg-text);
}

/* ---------- Board ---------- */
.board-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 1;
}

.board {
  position: relative;
  width: min(94vw, 420px);
  aspect-ratio: 1 / 1;
  background: var(--board-bg);
  border-radius: var(--radius-lg);
  padding: 10px;
  touch-action: none;
  box-shadow: 0 20px 50px -20px rgba(0, 0, 0, 0.6);
}

.grid-bg {
  position: absolute;
  inset: 10px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 3.5%;
}

.grid-bg .cell {
  background: var(--cell-bg);
  border-radius: var(--radius-md);
}

.tiles {
  position: absolute;
  inset: 10px;
}

.tile {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-md);
  font-family: var(--font-mono);
  font-weight: 700;
  color: #fff;
  transition: transform 0.12s cubic-bezier(0.2, 0.9, 0.3, 1), opacity 0.1s ease;
  will-change: transform;
}

.tile.new-tile { animation: tile-in 0.16s ease-out; }
.tile.merged { animation: tile-pop 0.16s ease-out; }

@keyframes tile-in {
  from { transform: scale(0.35); opacity: 0.3; }
  to   { transform: scale(1); opacity: 1; }
}

@keyframes tile-pop {
  0%   { transform: scale(1); }
  45%  { transform: scale(1.14); }
  100% { transform: scale(1); }
}

.hint {
  margin-top: 14px;
  font-size: 12px;
  color: var(--tg-hint);
  text-align: center;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%) translateY(20px);
  background: var(--accent);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  padding: 10px 18px;
  border-radius: 999px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 50;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ---------- Leaderboard sheet ---------- */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 30;
}

.sheet-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 78vh;
  background: var(--tg-bg-secondary);
  border-radius: 22px 22px 0 0;
  transform: translateY(102%);
  transition: transform 0.25s cubic-bezier(0.2, 0.9, 0.3, 1);
  z-index: 31;
  padding: 8px 16px calc(18px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
}

.sheet.open { transform: translateY(0); }

.sheet-handle {
  width: 36px;
  height: 4px;
  border-radius: 999px;
  background: var(--tg-hint);
  opacity: 0.4;
  margin: 6px auto 12px;
}

.sheet-header h2 {
  margin: 0 0 10px;
  font-size: 18px;
}

.tabs {
  display: flex;
  gap: 6px;
  background: rgba(255, 255, 255, 0.04);
  border-radius: var(--radius-sm);
  padding: 4px;
}

.tab {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--tg-hint);
  font-family: var(--font-ui);
  font-size: 12.5px;
  font-weight: 600;
  padding: 8px 6px;
  border-radius: 8px;
  cursor: pointer;
}

.tab.active {
  background: var(--accent);
  color: #fff;
}

.sheet-body {
  overflow-y: auto;
  margin-top: 12px;
}

.leaderboard-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.lb-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 6px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.lb-rank {
  width: 24px;
  font-family: var(--font-mono);
  font-weight: 700;
  color: var(--tg-hint);
  font-size: 13px;
}

.lb-rank.top1 { color: var(--tile-super); }
.lb-rank.top2 { color: var(--tile-16); }
.lb-rank.top3 { color: var(--tile-1024); }

.lb-name {
  flex: 1;
  font-size: 13.5px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lb-score {
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: 13.5px;
}

.lb-empty {
  color: var(--tg-hint);
  font-size: 13px;
  text-align: center;
  padding: 24px 0;
}

.me-row {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px dashed rgba(255, 255, 255, 0.12);
  font-size: 12.5px;
  color: var(--tg-hint);
  display: none;
}

.me-row.show { display: block; }

/* ---------- Game over overlay ---------- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 11, 16, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 40;
}

.overlay.open { opacity: 1; pointer-events: auto; }

.overlay-card {
  background: var(--tg-bg-secondary);
  border-radius: var(--radius-lg);
  padding: 26px 22px;
  width: min(84vw, 320px);
  text-align: center;
}

.overlay-card h2 { margin: 0 0 8px; }
.overlay-card p { color: var(--tg-hint); margin: 0 0 18px; font-family: var(--font-mono); }
