:root {
  --bg: #0d0a1a;
  --panel: rgba(28, 20, 52, 0.92);
  --panel2: rgba(42, 30, 76, 0.95);
  --line: rgba(181, 140, 255, 0.28);
  --gold: #ffd66b;
  --cyan: #6ff0ff;
  --pink: #ff5c8a;
  --green: #6fdc8c;
  --text: #f1ebff;
  --dim: #a79cc8;
  --st: env(safe-area-inset-top, 0px);
  --sb: env(safe-area-inset-bottom, 0px);
  --font: 'Jua', 'Apple SD Gothic Neo', 'Malgun Gothic', system-ui, sans-serif;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text);
  font-family: var(--font); user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  overscroll-behavior: none; touch-action: none;
}
canvas#game { position: fixed; inset: 0; display: block; touch-action: none; }
button { font-family: inherit; color: inherit; border: 0; background: none; cursor: pointer; touch-action: manipulation; }
[hidden] { display: none !important; }

/* ── HUD ── */
#hud { position: fixed; inset: 0; pointer-events: none; }
#xpbar {
  position: absolute; left: 0; right: 0; top: 0; height: calc(14px + var(--st)); padding-top: var(--st);
  background: rgba(10, 6, 22, 0.85); border-bottom: 1px solid var(--line);
}
#xpfill {
  height: 100%; width: 0%; background: linear-gradient(90deg, #4a8cff, var(--cyan));
  box-shadow: 0 0 10px var(--cyan); transition: width 0.12s linear;
}
#lvl {
  position: absolute; right: 8px; top: calc(var(--st) - 1px); font-size: 12px; line-height: 15px;
  text-shadow: 0 1px 2px #000;
}
#hudtop {
  position: absolute; top: calc(18px + var(--st)); left: 10px; right: 56px;
  display: flex; align-items: center; justify-content: space-between;
}
#timer {
  font-size: 24px; letter-spacing: 1px; text-shadow: 0 2px 0 #000, 0 0 12px rgba(111, 240, 255, 0.35);
  position: absolute; left: 50%; transform: translateX(calc(-50% + 23px));
}
#timer.urgent { color: var(--pink); }
.hudstat { display: flex; align-items: center; gap: 5px; font-size: 16px; text-shadow: 0 1px 2px #000; }
.hudstat b { font-weight: normal; }
.hudstat .ico { opacity: 0.8; }
.hudstat.right { margin-left: auto; }
.coin {
  width: 13px; height: 13px; border-radius: 50%; background: radial-gradient(circle at 35% 35%, #fff4c8, var(--gold) 50%, #a8742a);
  display: inline-block;
}
#pauseBtn {
  pointer-events: auto; position: absolute; right: 8px; top: calc(20px + var(--st));
  width: 40px; height: 40px; border-radius: 12px; background: rgba(28, 20, 52, 0.7); border: 1px solid var(--line);
  display: flex; align-items: center; justify-content: center; gap: 5px;
}
#pauseBtn span { width: 4px; height: 14px; border-radius: 2px; background: var(--text); }
#slots { position: absolute; top: calc(50px + var(--st)); left: 8px; display: flex; flex-direction: column; gap: 3px; }
#wslots, #pslots { display: flex; gap: 3px; }
.slot {
  width: 26px; height: 26px; border-radius: 7px; background: rgba(20, 14, 40, 0.75); border: 1px solid var(--line);
  position: relative; display: flex; align-items: center; justify-content: center;
}
.slot img { width: 22px; height: 22px; }
.slot.evo { border-color: var(--gold); box-shadow: 0 0 6px rgba(255, 214, 107, 0.6); }
.slot i {
  position: absolute; right: -2px; bottom: -3px; font-style: normal; font-size: 9px; line-height: 10px;
  background: #1a1030; border-radius: 4px; padding: 0 2px; color: var(--gold);
}
.slot.p { width: 20px; height: 20px; border-radius: 6px; }
.slot.p img { width: 16px; height: 16px; }
#bossbar {
  position: absolute; left: 50%; transform: translateX(-50%); top: calc(52px + var(--st));
  width: min(70vw, 380px); text-align: center; margin-top: 50px;
}
#bossname { font-size: 14px; color: #ffb8cc; text-shadow: 0 1px 3px #000; margin-bottom: 3px; }
#bossbar .bar { height: 9px; border-radius: 5px; background: rgba(10, 6, 22, 0.85); border: 1px solid rgba(255, 92, 138, 0.5); overflow: hidden; }
#bossfill { height: 100%; width: 100%; background: linear-gradient(90deg, #c8284a, var(--pink)); transition: width 0.15s; }
#hint {
  position: absolute; left: 0; right: 0; bottom: calc(18% + var(--sb)); text-align: center; font-size: 19px;
  text-shadow: 0 2px 6px #000; animation: hintPulse 1.6s ease-in-out infinite;
}
#hint small { font-size: 14px; color: var(--dim); }
@keyframes hintPulse { 50% { opacity: 0.55; } }

#banner {
  position: fixed; left: 0; right: 0; top: 34%; text-align: center; pointer-events: none;
  font-size: 22px; opacity: 0; text-shadow: 0 2px 8px #000; padding: 0 16px;
}
#banner.show { animation: banner 2.4s ease forwards; }
#banner.boss { color: #ff8aa8; font-size: 26px; }
#banner.warn { color: var(--gold); }
#banner.gold { color: var(--gold); }
#banner.dim { color: #c9b8ff; font-size: 18px; }
@keyframes banner {
  0% { opacity: 0; transform: scale(1.4); letter-spacing: 6px; }
  12% { opacity: 1; transform: scale(1); letter-spacing: 1px; }
  80% { opacity: 1; }
  100% { opacity: 0; transform: translateY(-14px); }
}

/* ── 공통 화면 ── */
#screen { position: fixed; inset: 0; pointer-events: none; }
#screen > * { pointer-events: auto; }
.page {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center;
  padding: calc(16px + var(--st)) 16px calc(16px + var(--sb)); overflow-y: auto; touch-action: pan-y;
  animation: fadeIn 0.25s ease;
}
.dim-bg { background: rgba(8, 5, 18, 0.72); backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px); }
@keyframes fadeIn { from { opacity: 0; } }
.btn {
  display: block; width: 100%; max-width: 320px; padding: 14px 18px; border-radius: 16px; font-size: 20px;
  background: linear-gradient(180deg, #8a6aff, #5a3ad0); box-shadow: 0 4px 0 #3a2290, 0 8px 20px rgba(90, 58, 208, 0.35);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.4); transition: transform 0.08s;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 #3a2290; }
.btn.gold { background: linear-gradient(180deg, #ffe08a, #e0a83a); color: #3a2410; box-shadow: 0 4px 0 #a8742a, 0 8px 20px rgba(255, 214, 107, 0.3); text-shadow: none; }
.btn.gold:active { box-shadow: 0 1px 0 #a8742a; }
.btn.ghost { background: rgba(42, 30, 76, 0.8); box-shadow: 0 4px 0 #1a1030; border: 1px solid var(--line); }
.btn.small { font-size: 16px; padding: 10px 14px; }
.btn:disabled { filter: grayscale(0.8) brightness(0.7); }
.row { display: flex; gap: 10px; width: 100%; max-width: 320px; }
.row .btn { flex: 1; }
.goldtag {
  display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; border-radius: 99px;
  background: rgba(20, 14, 40, 0.8); border: 1px solid rgba(255, 214, 107, 0.35); font-size: 17px; color: var(--gold);
}
.topbar { width: 100%; max-width: 460px; display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.back { font-size: 16px; padding: 8px 12px; border-radius: 12px; background: rgba(42, 30, 76, 0.8); border: 1px solid var(--line); }
h2 { margin: 4px 0 12px; font-weight: normal; font-size: 26px; }

/* 타이틀 */
#titlePage { justify-content: center; gap: 12px; }
.logo { text-align: center; margin-bottom: 26px; animation: floaty 4s ease-in-out infinite; }
.logo .moon {
  width: 86px; height: 86px; margin: 0 auto 10px; border-radius: 50%;
  background: radial-gradient(circle at 38% 38%, #fffbe8, #ffe8a8 45%, #d8a84a 100%);
  filter: drop-shadow(0 0 18px rgba(255, 230, 160, 0.7)) drop-shadow(0 0 50px rgba(255, 214, 107, 0.35));
  -webkit-mask: radial-gradient(circle at 78% 30%, transparent 36px, #000 37px);
  mask: radial-gradient(circle at 78% 30%, transparent 36px, #000 37px);
}
.logo h1 {
  margin: 0; font-weight: normal; font-size: 46px; letter-spacing: 3px;
  background: linear-gradient(180deg, #fff, #c9b8ff); -webkit-background-clip: text; background-clip: text; color: transparent;
  filter: drop-shadow(0 3px 0 #2a1a5a) drop-shadow(0 0 18px rgba(181, 140, 255, 0.5));
}
.logo p { margin: 4px 0 0; color: var(--dim); letter-spacing: 4px; font-size: 13px; }
@keyframes floaty { 50% { transform: translateY(-6px); } }
.titlegold { position: absolute; top: calc(14px + var(--st)); right: 14px; }
.toggles { display: flex; gap: 8px; margin-top: 18px; }
.tog {
  padding: 8px 12px; border-radius: 12px; font-size: 14px; background: rgba(42, 30, 76, 0.8); border: 1px solid var(--line); color: var(--dim);
}
.tog.on { color: var(--text); border-color: rgba(111, 240, 255, 0.5); }
.best { color: var(--dim); font-size: 14px; margin-top: 6px; text-align: center; line-height: 1.6; }

/* 캐릭터 선택 */
.chars { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 10px; width: 100%; max-width: 460px; }
.char {
  position: relative; border-radius: 16px; padding: 8px 6px 10px; background: var(--panel); border: 2px solid var(--line); text-align: center;
}
.char img { width: 72px; height: 72px; display: block; margin: 0 auto; }
.char .nm { font-size: 17px; }
.char .tt { font-size: 11px; color: var(--dim); }
.char.sel { border-color: var(--gold); box-shadow: 0 0 16px rgba(255, 214, 107, 0.35); background: var(--panel2); }
.char.locked img { filter: brightness(0) opacity(0.5); }
.char .cost { position: absolute; top: 6px; right: 6px; font-size: 12px; color: var(--gold); display: flex; align-items: center; gap: 3px; }
.char .cost .coin { width: 10px; height: 10px; }
.detail {
  width: 100%; max-width: 460px; margin: 12px 0; padding: 12px 14px; border-radius: 16px; background: var(--panel); border: 1px solid var(--line);
  display: flex; gap: 12px; align-items: center;
}
.detail img { width: 44px; height: 44px; flex: none; }
.detail .t { font-size: 17px; }
.detail .d { font-size: 13px; color: var(--dim); margin-top: 2px; line-height: 1.4; }
.diff {
  display: flex; align-items: center; justify-content: space-between; width: 100%; max-width: 320px; margin-bottom: 14px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 6px;
}
.diff button { width: 40px; height: 40px; border-radius: 10px; background: rgba(60, 44, 110, 0.9); font-size: 18px; }
.diff button:disabled { opacity: 0.3; }
.diff .dv { text-align: center; }
.diff .dv b { font-weight: normal; font-size: 19px; display: block; }
.diff .dv small { color: var(--dim); font-size: 12px; }

/* 상점 */
.shop { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; width: 100%; max-width: 460px; }
.sitem {
  border-radius: 14px; padding: 10px; background: var(--panel); border: 1px solid var(--line); text-align: left; display: flex; flex-direction: column; gap: 4px;
}
.sitem .nm { font-size: 17px; }
.sitem .d { font-size: 12px; color: var(--dim); min-height: 30px; line-height: 1.3; }
.pips { display: flex; gap: 3px; }
.pips i { flex: 1; height: 5px; border-radius: 3px; background: rgba(255, 255, 255, 0.12); }
.pips i.on { background: var(--gold); box-shadow: 0 0 4px var(--gold); }
.sitem .buy {
  margin-top: 4px; padding: 7px; border-radius: 10px; background: linear-gradient(180deg, #ffe08a, #e0a83a); color: #3a2410; font-size: 15px;
  display: flex; align-items: center; justify-content: center; gap: 5px;
}
.sitem .buy:disabled { background: rgba(80, 70, 110, 0.6); color: var(--dim); }
.sitem.max .buy { background: rgba(111, 220, 140, 0.25); color: var(--green); }
.refund { margin-top: 14px; font-size: 13px; color: var(--dim); text-decoration: underline; }

/* 기록 */
.codex { width: 100%; max-width: 460px; display: flex; flex-direction: column; gap: 8px; }
.cx { display: flex; gap: 10px; align-items: center; padding: 10px; border-radius: 14px; background: var(--panel); border: 1px solid var(--line); }
.cx img { width: 40px; height: 40px; flex: none; }
.cx .t { font-size: 16px; }
.cx .d { font-size: 12px; color: var(--dim); line-height: 1.4; }
.cx .evo { color: var(--gold); }
.statgrid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; width: 100%; max-width: 460px; margin-bottom: 14px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 10px; text-align: center; }
.stat b { display: block; font-weight: normal; font-size: 22px; }
.stat small { color: var(--dim); font-size: 12px; }
.sect { width: 100%; max-width: 460px; color: var(--dim); margin: 6px 0 8px; font-size: 14px; }

/* 레벨업 */
#lvPage { justify-content: center; }
.lvtitle {
  font-size: 34px; margin-bottom: 4px; color: var(--gold);
  text-shadow: 0 3px 0 #7a4a1a, 0 0 22px rgba(255, 214, 107, 0.6); animation: popIn 0.4s cubic-bezier(.2,1.6,.4,1);
}
.lvsub { color: var(--dim); font-size: 14px; margin-bottom: 14px; }
@keyframes popIn { from { transform: scale(0.3); opacity: 0; } }
.cards { display: flex; flex-direction: column; gap: 10px; width: 100%; max-width: 420px; }
.card {
  display: flex; gap: 12px; align-items: center; text-align: left; padding: 12px; border-radius: 18px;
  background: linear-gradient(180deg, var(--panel2), var(--panel)); border: 2px solid var(--line);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35); animation: cardIn 0.35s cubic-bezier(.2,1.2,.4,1) backwards;
  transition: transform 0.08s;
}
.card:active { transform: scale(0.97); }
.card .ic {
  width: 58px; height: 58px; flex: none; border-radius: 14px; background: rgba(10, 6, 22, 0.6); display: flex; align-items: center; justify-content: center;
  border: 1px solid rgba(255,255,255,0.08);
}
.card .ic img { width: 50px; height: 50px; }
.card .body { flex: 1; min-width: 0; }
.card .hd { display: flex; align-items: baseline; gap: 8px; }
.card .nm { font-size: 19px; }
.card .tg { font-size: 12px; padding: 2px 7px; border-radius: 99px; background: rgba(111, 240, 255, 0.15); color: var(--cyan); white-space: nowrap; }
.card .tg.new { background: rgba(255, 214, 107, 0.18); color: var(--gold); }
.card .ds { font-size: 13.5px; color: #d8d0f0; margin: 4px 0 6px; line-height: 1.35; }
.card .pips i { height: 4px; max-width: 26px; }
.card.evo {
  border-color: var(--gold); background: linear-gradient(135deg, #5a3a1a, #3a2458 60%, #5a3a1a);
  box-shadow: 0 0 22px rgba(255, 214, 107, 0.45); position: relative; overflow: hidden;
}
.card.evo::after {
  content: ''; position: absolute; top: 0; bottom: 0; width: 60px; left: -80px;
  background: linear-gradient(90deg, transparent, rgba(255, 240, 180, 0.35), transparent); transform: skewX(-20deg);
  animation: shine 2s ease-in-out infinite;
}
.card.evo .tg { background: var(--gold); color: #3a2410; }
.card .from { font-size: 11px; color: var(--gold); }
@keyframes shine { 60%, 100% { left: 120%; } }
@keyframes cardIn { from { transform: translateY(40px) scale(0.9); opacity: 0; } }
.lvfoot { display: flex; gap: 10px; margin-top: 14px; width: 100%; max-width: 420px; }
.lvfoot .btn { font-size: 15px; padding: 10px; }

/* 상자 */
#chestPage { justify-content: center; text-align: center; }
.chestbox { width: 150px; height: 130px; margin: 10px auto 18px; position: relative; cursor: pointer; }
.chestbox .glow {
  position: absolute; inset: -60px; border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 214, 107, 0.55), transparent 65%); animation: pulse 1.2s ease-in-out infinite;
}
.chestbox .lid, .chestbox .base { position: absolute; left: 10px; right: 10px; border-radius: 12px; }
.chestbox .base { bottom: 0; height: 72px; background: linear-gradient(180deg, #b87a3a, #7a4a1a); border: 4px solid #4a2a10; }
.chestbox .lid {
  bottom: 64px; height: 48px; background: linear-gradient(180deg, #d8944a, #a86a2a); border: 4px solid #4a2a10; border-radius: 30px 30px 8px 8px;
  transform-origin: 50% 100%; transition: transform 0.4s cubic-bezier(.3,1.6,.5,1);
}
.chestbox .band { position: absolute; left: 50%; width: 22px; margin-left: -11px; top: -4px; bottom: -4px; background: var(--gold); border: 3px solid #a8742a; }
.chestbox.shake { animation: shake 0.5s ease-in-out infinite; }
.chestbox.open .lid { transform: translateY(-30px) rotate(-18deg); }
.chestbox.open .glow { background: radial-gradient(circle, rgba(255, 244, 200, 0.9), transparent 65%); }
@keyframes shake { 20% { transform: rotate(-4deg); } 40% { transform: rotate(4deg); } 60% { transform: rotate(-2deg); } 80% { transform: rotate(2deg); } }
@keyframes pulse { 50% { opacity: 0.55; transform: scale(0.92); } }
.chestitems { display: flex; flex-direction: column; gap: 8px; width: 100%; max-width: 380px; min-height: 60px; }
.citem {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-radius: 14px; background: var(--panel2); border: 1px solid var(--line);
  animation: cardIn 0.4s cubic-bezier(.2,1.4,.4,1) backwards; text-align: left;
}
.citem img { width: 38px; height: 38px; }
.citem .nm { font-size: 16px; }
.citem .tg { font-size: 12px; color: var(--cyan); }
.citem.evo { border-color: var(--gold); box-shadow: 0 0 14px rgba(255, 214, 107, 0.45); }
.citem.evo .tg { color: var(--gold); }
.tapmsg { color: var(--dim); animation: hintPulse 1.2s infinite; margin-bottom: 8px; }

/* 일시정지 / 결과 */
#pausePage, #resultPage { justify-content: flex-start; }
.panel {
  width: 100%; max-width: 420px; padding: 14px; border-radius: 18px; background: var(--panel); border: 1px solid var(--line); margin-bottom: 12px;
}
.buildrow { display: flex; flex-wrap: wrap; gap: 6px; }
.bitem { display: flex; align-items: center; gap: 6px; padding: 5px 8px 5px 5px; border-radius: 10px; background: rgba(10, 6, 22, 0.5); font-size: 13px; }
.bitem img { width: 26px; height: 26px; }
.bitem.evo { color: var(--gold); }
.dmgrow { display: flex; align-items: center; gap: 8px; margin: 6px 0; font-size: 13px; }
.dmgrow img { width: 26px; height: 26px; }
.dmgrow .n { width: 76px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dmgrow .bar { flex: 1; height: 8px; border-radius: 4px; background: rgba(255,255,255,0.08); overflow: hidden; }
.dmgrow .bar i { display: block; height: 100%; background: linear-gradient(90deg, #7b5cff, var(--cyan)); }
.dmgrow .v { width: 58px; text-align: right; color: var(--dim); }
.stats2 { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 14px; font-size: 13px; color: var(--dim); }
.stats2 b { color: var(--text); font-weight: normal; float: right; }
.restitle { font-size: 40px; margin: 18px 0 2px; text-shadow: 0 3px 0 #000; animation: popIn 0.5s cubic-bezier(.2,1.6,.4,1); }
.restitle.win { color: var(--gold); text-shadow: 0 3px 0 #7a4a1a, 0 0 30px rgba(255, 214, 107, 0.7); }
.restitle.lose { color: #c9b8ff; }
.ressub { color: var(--dim); margin-bottom: 14px; }
.goldearn { font-size: 30px; color: var(--gold); display: flex; align-items: center; justify-content: center; gap: 8px; margin: 4px 0; }
.goldearn .coin { width: 22px; height: 22px; }
.goldnote { text-align: center; color: var(--dim); font-size: 12px; }
.unlock { text-align: center; color: var(--green); margin-top: 8px; font-size: 15px; }

@media (min-width: 700px) {
  #slots { top: calc(52px + var(--st)); }
  .slot { width: 34px; height: 34px; } .slot img { width: 28px; height: 28px; }
  .slot.p { width: 26px; height: 26px; } .slot.p img { width: 20px; height: 20px; }
  #timer { font-size: 28px; }
}
@media (max-height: 640px) {
  .card { padding: 8px 10px; } .card .ic { width: 46px; height: 46px; } .card .ic img { width: 40px; height: 40px; }
  .card .ds { margin: 2px 0 4px; }
  .lvtitle { font-size: 28px; }
}
