* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg1: #8ec5fc;
  --bg2: #e0c3fc;
  --card: #ffffff;
  --ink: #3d3554;
  --ink-soft: #7a7295;
  --primary: #7c5cff;
  --primary-dark: #6547e0;
  --accent: #ffb703;
  --board-bg: #b9a7e8;
  --cell-bg: rgba(255, 255, 255, 0.45);
  --radius: 18px;
}

html, body {
  height: 100%;
  overscroll-behavior: none;
}

body {
  font-family: "Segoe UI", "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
  background: linear-gradient(160deg, var(--bg1), var(--bg2));
  color: var(--ink);
  min-height: 100dvh;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

.screen {
  display: none;
  min-height: 100dvh;
  flex-direction: column;
  align-items: center;
  padding: max(12px, env(safe-area-inset-top)) 16px calc(24px + env(safe-area-inset-bottom));
  max-width: 520px;
  margin: 0 auto;
}
.screen.active { display: flex; }

/* ---------- 버튼 ---------- */
.btn {
  border: none;
  border-radius: var(--radius);
  background: var(--card);
  color: var(--ink);
  font-family: inherit;
  font-size: 1.05rem;
  font-weight: 700;
  padding: 14px 20px;
  box-shadow: 0 4px 0 rgba(61, 53, 84, 0.15);
  cursor: pointer;
  transition: transform 0.08s, box-shadow 0.08s;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(61, 53, 84, 0.15); }
.btn-primary { background: var(--primary); color: #fff; box-shadow: 0 4px 0 var(--primary-dark); }
.btn-big { width: 100%; max-width: 320px; font-size: 1.2rem; padding: 16px 20px; }
.btn-icon { padding: 10px 16px; font-size: 1.1rem; }

.pill {
  background: var(--accent);
  color: #6b4500;
  border-radius: 999px;
  padding: 2px 10px;
  font-size: 0.85rem;
}

/* ---------- 홈 ---------- */
.home-inner {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  width: 100%;
  text-align: center;
}
.home-mascot {
  font-size: 5rem;
  animation: bounce 2.2s ease-in-out infinite;
}
@keyframes bounce {
  0%, 100% { transform: translateY(0) rotate(-4deg); }
  50% { transform: translateY(-14px) rotate(4deg); }
}
.home-title { font-size: 2.6rem; letter-spacing: -1px; }
.home-title span { color: var(--primary); }
.home-sub { color: var(--ink-soft); font-weight: 600; margin-bottom: 10px; }
.home-note {
  margin-top: 18px;
  font-size: 0.8rem;
  color: var(--ink-soft);
  line-height: 1.5;
}

/* ---------- 상단바 ---------- */
.topbar {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
}
.topbar h2 { flex: 1; text-align: center; font-size: 1.25rem; }
.topbar-right { font-weight: 800; min-width: 64px; text-align: right; }

/* ---------- 레벨 선택 ---------- */
.level-grid {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 10px;
}
.level-cell {
  aspect-ratio: 1;
  border: none;
  border-radius: 14px;
  background: var(--card);
  font-family: inherit;
  font-size: 1.05rem;
  font-weight: 800;
  color: var(--ink);
  box-shadow: 0 3px 0 rgba(61, 53, 84, 0.15);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
}
.level-cell:active { transform: translateY(2px); box-shadow: none; }
.level-cell .lv-stars { font-size: 0.55rem; letter-spacing: -1px; }
.level-cell.locked {
  background: rgba(255, 255, 255, 0.35);
  color: rgba(61, 53, 84, 0.4);
  box-shadow: none;
  cursor: default;
}
.level-cell.cleared { background: #fff6d6; }

/* ---------- 스티커북 ---------- */
.sticker-hint { color: var(--ink-soft); font-weight: 600; font-size: 0.9rem; margin-bottom: 12px; }
.sticker-grid {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 10px;
}
.sticker-cell {
  aspect-ratio: 1;
  border-radius: 14px;
  background: var(--card);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: 1.7rem;
  box-shadow: 0 3px 0 rgba(61, 53, 84, 0.12);
}
.sticker-cell .st-name { font-size: 0.55rem; font-weight: 700; color: var(--ink-soft); }
.sticker-cell.empty { background: rgba(255, 255, 255, 0.3); box-shadow: none; }
.sticker-cell.empty::before { content: "?"; color: rgba(61, 53, 84, 0.35); font-weight: 800; }

/* ---------- 게임 ---------- */
.game-info {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr 1.3fr;
  gap: 10px;
  margin-bottom: 14px;
}
.info-card {
  background: var(--card);
  border-radius: 14px;
  padding: 8px 6px;
  text-align: center;
  box-shadow: 0 3px 0 rgba(61, 53, 84, 0.12);
}
.info-label { font-size: 0.7rem; font-weight: 700; color: var(--ink-soft); }
.info-value { font-size: 1.15rem; font-weight: 800; }

.board-wrap { width: 100%; display: flex; justify-content: center; }
.board {
  --gap: 8px;
  position: relative;
  width: min(92vw, 400px, 46dvh);
  background: var(--board-bg);
  border-radius: var(--radius);
  padding: var(--gap);
  box-shadow: inset 0 3px 8px rgba(61, 53, 84, 0.25);
  touch-action: none;
}
.board-cells {
  position: absolute;
  inset: var(--gap);
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: repeat(6, 1fr);
  gap: var(--gap);
}
#chain-svg {
  position: absolute;
  inset: var(--gap);
  pointer-events: none;
  overflow: visible;
  z-index: 1;
}
#chain-line {
  fill: none;
  stroke: rgba(255, 183, 3, 0.75);
  stroke-linecap: round;
  stroke-linejoin: round;
}
#chain-chip {
  position: absolute;
  top: -16px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--accent);
  color: #6b4500;
  font-weight: 800;
  font-size: 1.05rem;
  border-radius: 999px;
  padding: 4px 16px;
  box-shadow: 0 3px 0 rgba(61, 53, 84, 0.25);
  display: none;
  z-index: 3;
}
#chain-chip.show { display: block; }
.bcell { background: var(--cell-bg); border-radius: 10px; }
.bcell.blocked {
  background: #6d5f96;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
}
.bcell.blocked::before { content: "🧱"; }

.board-tiles { position: absolute; inset: var(--gap); z-index: 2; }
.tile {
  position: absolute;
  /* --u: 셀 한 칸 + 간격 이동량, --s: 셀 크기 (game.js에서 계산) */
  width: var(--s);
  height: var(--s);
  transform: translate(calc(var(--x) * var(--u)), calc(var(--y) * var(--u)));
  transition: transform 0.12s ease-in-out;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  background: #fff;
  box-shadow: 0 2px 0 rgba(61, 53, 84, 0.2);
  z-index: 1;
}
.tile .t-num { font-size: calc(var(--s) * 0.42); font-weight: 800; line-height: 1; }
.tile.d3 .t-num { font-size: calc(var(--s) * 0.34); }
.tile.d4 .t-num { font-size: calc(var(--s) * 0.27); }
.tile.d5 .t-num { font-size: calc(var(--s) * 0.22); }
.tile.sel {
  box-shadow: 0 0 0 4px var(--accent), 0 2px 0 rgba(61, 53, 84, 0.2);
  filter: brightness(1.06);
}
.tile.merging { z-index: 0; opacity: 0.75; }

.tile.v2    { background: #f6f9ff; }
.tile.v4    { background: #fff3e0; }
.tile.v8    { background: #ffe9f0; }
.tile.v16   { background: #fff0d9; }
.tile.v32   { background: #e8f6ea; }
.tile.v64   { background: #e3f2fd; }
.tile.v128  { background: #ffe3c2; }
.tile.v256  { background: #ffd9a1; }
.tile.v512  { background: #f3e2ff; }
.tile.v1024 { background: #dcefe2; }
.tile.v2048 { background: #ffe066; box-shadow: 0 0 16px #ffd000; }
.tile.v4096 { background: #ffd6ff; }
.tile.v8192 { background: #c8f7dc; }
.tile.v16384 { background: #ffd0c2; }
.tile.v32768 { background: #d9c9ff; box-shadow: 0 0 16px #b197fc; }

.tile.spawn { animation: spawn 0.16s ease-out; }
@keyframes spawn {
  from { transform: translate(calc(var(--x) * var(--u)), calc(var(--y) * var(--u))) scale(0.3); opacity: 0; }
  to   { transform: translate(calc(var(--x) * var(--u)), calc(var(--y) * var(--u))) scale(1); opacity: 1; }
}
.tile.pop { animation: pop 0.18s ease-out; }
@keyframes pop {
  0%   { transform: translate(calc(var(--x) * var(--u)), calc(var(--y) * var(--u))) scale(1); }
  55%  { transform: translate(calc(var(--x) * var(--u)), calc(var(--y) * var(--u))) scale(1.22); }
  100% { transform: translate(calc(var(--x) * var(--u)), calc(var(--y) * var(--u))) scale(1); }
}

.swipe-hint {
  margin: 14px 0 12px;
  color: var(--ink-soft);
  font-weight: 600;
  font-size: 0.85rem;
}

/* ---------- 팝업 ---------- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(61, 53, 84, 0.45);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 50;
}
.overlay.show { display: flex; }
.popup {
  background: var(--card);
  border-radius: 24px;
  padding: 24px 20px;
  width: min(92vw, 360px);
  text-align: center;
  animation: popup-in 0.25s ease-out;
}
@keyframes popup-in {
  from { transform: scale(0.7); opacity: 0; }
  to   { transform: scale(1); opacity: 1; }
}
.popup-burst { font-size: 3rem; }
.popup-title { margin: 6px 0 10px; }
.popup-text { color: var(--ink-soft); font-weight: 600; line-height: 1.5; margin-bottom: 16px; }
.popup-stars { font-size: 2rem; letter-spacing: 4px; margin-bottom: 12px; }
.popup-stars .dim { opacity: 0.25; }

.reward-box {
  background: #fff6d6;
  border-radius: 18px;
  padding: 12px;
  margin-bottom: 12px;
}
.reward-label { font-size: 0.8rem; font-weight: 800; color: #a8760a; }
.reward-sticker { font-size: 3.2rem; animation: bounce 1.6s ease-in-out infinite; }
.reward-name { font-weight: 800; }
.reward-badge {
  background: #efe6ff;
  border-radius: 18px;
  padding: 10px;
  margin-bottom: 12px;
  font-weight: 800;
  font-size: 1rem;
}
.popup-btns { display: flex; gap: 10px; justify-content: center; }
.popup-btns .btn { flex: 1; }

/* ---------- 폭죽 ---------- */
#confetti-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 60;
}
.confetti {
  position: absolute;
  top: -20px;
  width: 10px;
  height: 14px;
  border-radius: 3px;
  animation: fall linear forwards;
}
@keyframes fall {
  to { transform: translateY(110vh) rotate(720deg); }
}
