:root {
  --sand: #f3e2b8;
  --turquoise: #1aa79d;
  --orange: #f28a1b;
  --bg: var(--tg-theme-bg-color, #fff8e8);
  --text: var(--tg-theme-text-color, #3b2f1e);
  --hint: var(--tg-theme-hint-color, #8a7a5c);
  --card: var(--tg-theme-secondary-bg-color, #f6e9c8);
  --tile-text: #3b2f1e;
}

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

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-user-select: none;
  user-select: none;
}

#app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100dvh;
  padding: 10px 12px calc(14px + env(safe-area-inset-bottom));
}

.screen { display: flex; flex-direction: column; gap: 12px; min-height: calc(100dvh - 24px); }
.hidden { display: none !important; }

.card { background: var(--card); border-radius: 16px; padding: 10px 12px; }

/* --- buttons --- */
.btn {
  border: 0; border-radius: 14px; padding: 12px 14px;
  font-size: 15px; font-weight: 700; cursor: pointer;
  color: #fff; background: var(--turquoise);
  transition: transform .08s ease, opacity .2s ease;
}
.btn:active { transform: scale(.96); }
.btn:disabled { opacity: .45; cursor: default; transform: none; }
.btn.primary { background: var(--turquoise); }
.btn.accent { background: var(--orange); }
.btn.secondary { background: var(--card); color: var(--text); }
.btn.big { min-height: 56px; font-size: 17px; width: 100%; }
.btn.small { padding: 8px 12px; font-size: 14px; }

/* --- top panel --- */
.top { display: flex; flex-direction: column; gap: 10px; }
.order { display: flex; align-items: center; justify-content: space-between; gap: 10px; border: 2px solid var(--orange); }
.order-label { font-size: 12px; color: var(--hint); text-transform: uppercase; letter-spacing: .05em; }
.order-main { display: flex; align-items: center; gap: 10px; margin-top: 2px; }
.order-emoji { font-size: 32px; }
.reward { display: block; font-size: 13px; color: var(--orange); font-weight: 700; }

.stats { display: flex; align-items: center; gap: 8px; }
.stat { display: flex; align-items: center; gap: 6px; flex: 1; font-size: 16px; }
.stat small { color: var(--hint); font-size: 12px; margin-left: auto; }
.stat.bump b { animation: bump .4s ease; }

/* --- board --- */
.board {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
  touch-action: none;
}

.cell {
  aspect-ratio: 1;
  border-radius: 14px;
  background: var(--card);
  box-shadow: inset 0 0 0 2px rgba(0, 0, 0, .05);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: var(--tile-text);
  position: relative;
}
.cell .emoji { font-size: clamp(22px, 8vw, 34px); line-height: 1; }
.cell .name { font-size: 9px; font-weight: 700; margin-top: 2px; }

.cell.l1 { background: #efe3c6; }
.cell.l2 { background: #c4ece6; }
.cell.l3 { background: #92d6cd; }
.cell.l4 { background: #ffd8a3; }
.cell.l5 { background: linear-gradient(135deg, #f7b267, #f28a1b); box-shadow: 0 0 0 2px #d9730d inset; }
.cell.sel { outline: 3px solid var(--turquoise); outline-offset: -3px; transform: scale(1.05); z-index: 1; }

.cell.merged { animation: pop .5s ease; }
.cell.merged::after {
  content: ""; position: absolute; inset: 0; border-radius: 14px;
  box-shadow: 0 0 22px 8px rgba(255, 214, 102, .9);
  animation: glow .5s ease forwards; pointer-events: none;
}
.cell.spawn { animation: spawn .35s ease; }

@keyframes pop { 0% { transform: scale(1); } 45% { transform: scale(1.3); } 100% { transform: scale(1); } }
@keyframes glow { 0% { opacity: 1; } 100% { opacity: 0; } }
@keyframes spawn { 0% { transform: scale(.3); opacity: 0; } 100% { transform: scale(1); opacity: 1; } }
@keyframes bump { 0% { transform: scale(1); } 50% { transform: scale(1.35); color: var(--orange); } 100% { transform: scale(1); } }

/* --- footer --- */
footer { display: flex; flex-direction: column; gap: 10px; margin-top: auto; }
.row { display: flex; gap: 8px; }
.row .btn { flex: 1; }

/* --- leaderboard --- */
.lb-head { display: flex; align-items: center; gap: 12px; }
.lb-head h2 { margin: 0; font-size: 20px; }
.lb-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.lb-list li { display: flex; align-items: center; gap: 10px; background: var(--card); border-radius: 12px; padding: 10px 12px; }
.lb-list li.me { outline: 2px solid var(--turquoise); }
.lb-rank { width: 34px; font-weight: 800; }
.lb-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-coins { font-weight: 800; color: var(--orange); }
.lb-me { text-align: center; font-weight: 700; margin-top: auto; }

/* --- misc --- */
.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translate(-50%, 20px);
  background: rgba(30, 30, 30, .92); color: #fff; padding: 10px 16px; border-radius: 12px;
  font-size: 14px; opacity: 0; pointer-events: none; transition: all .25s ease; max-width: 90%; text-align: center; z-index: 10;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.overlay {
  position: fixed; inset: 0; background: var(--bg); display: flex;
  align-items: center; justify-content: center; text-align: center; padding: 24px; z-index: 20; font-size: 18px;
}
