:root {
  --bg: #F6F3EC;
  --card: #FFFFFF;
  --ink: #1F2420;
  --muted: #7A817B;
  --line: #E8E3D8;
  --accent: #3E9B48;
  --accent-ink: #FFFFFF;
  --danger: #E0604F;
  --shadow: 0 1px 2px rgba(31, 36, 32, .04), 0 8px 24px rgba(31, 36, 32, .07);
  --radius: 20px;
  --font: -apple-system, BlinkMacSystemFont, "Pretendard", "Apple SD Gothic Neo", "Noto Sans KR", "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  user-select: none;
  -webkit-user-select: none;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; border: 0; cursor: pointer; }
h1, h2 { margin: 0; letter-spacing: -0.02em; }

.screen { display: none; min-height: 100%; }
.screen.active { display: block; }

/* ---------- 홈 ---------- */
#home {
  max-width: 480px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 20px) 16px calc(env(safe-area-inset-bottom) + 28px);
}
.hero { text-align: center; padding: 12px 0 20px; }
.hero-bug { width: auto; height: 128px; animation: breathe 3.2s ease-in-out infinite; transform-origin: 50% 80%; }
.hero h1 { font-size: 34px; font-weight: 800; margin-top: 6px; }
.tagline { margin: 6px 0 0; color: var(--muted); font-size: 15px; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  margin-bottom: 14px;
}
.label { font-size: 12px; color: var(--muted); font-weight: 600; letter-spacing: .02em; }
.profile-top { display: flex; align-items: center; justify-content: space-between; }
.nick { font-size: 20px; font-weight: 750; margin-top: 3px; }
.icon-btn {
  width: 44px; height: 44px; border-radius: 14px;
  background: var(--bg); font-size: 20px;
  transition: transform .15s;
}
.icon-btn:active { transform: scale(.9) rotate(-12deg); }

.profile-stats {
  display: grid; grid-template-columns: 1.3fr 1fr 1fr; gap: 10px;
  margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--line);
}
.stat .num { font-size: 20px; font-weight: 750; margin-top: 3px; font-variant-numeric: tabular-nums; }
.tier {
  display: flex; align-items: center; gap: 8px;
  background: var(--bg); border-radius: 14px; padding: 10px 12px;
  font-weight: 750; font-size: 15px;
}
.tier-dot { width: 14px; height: 14px; border-radius: 50%; background: #BDBDBD; box-shadow: inset 0 -2px 0 rgba(0,0,0,.15); flex: none; }

.section-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.section-head h2 { font-size: 17px; font-weight: 750; }
.hint { font-size: 12px; color: var(--muted); text-align: right; }

.levels { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.lv {
  aspect-ratio: 1 / 1.08;
  border-radius: 14px;
  background: var(--bg);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  transition: transform .12s, background .15s;
}
.lv b { font-size: 18px; font-weight: 800; }
.lv small { font-size: 10.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.lv.locked { opacity: .38; }
.lv.selected { background: var(--accent); color: var(--accent-ink); }
.lv.selected small { color: rgba(255,255,255,.85); }
.lv:not(.locked):active { transform: scale(.94); }

.primary, .secondary {
  border-radius: 16px; padding: 14px 18px; font-weight: 750; font-size: 16px;
  transition: transform .12s, filter .15s;
}
.primary { background: var(--accent); color: var(--accent-ink); box-shadow: 0 6px 16px rgba(62, 155, 72, .28); }
.secondary { background: var(--bg); }
.primary:active, .secondary:active { transform: scale(.97); }
.primary.big { width: 100%; padding: 18px; font-size: 18px; margin: 4px 0 14px; }

.ranking { list-style: none; margin: 0; padding: 0; }
.ranking li {
  display: grid; grid-template-columns: 28px 1fr auto; align-items: center; gap: 10px;
  padding: 10px 2px; border-bottom: 1px solid var(--line); font-size: 14px;
}
.ranking li:last-child { border-bottom: 0; }
.ranking .rank { font-weight: 800; color: var(--muted); text-align: center; }
.ranking .who { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ranking .who { min-width: 0; }
.ranking li.me .who, .ranking li.me .rank { color: var(--accent); font-weight: 750; }
.ranking .mine-rank { display: block; color: var(--muted); text-align: right; font-size: 13px; padding: 10px 2px 0; border: 0; }
.ranking .ms { font-weight: 750; font-variant-numeric: tabular-nums; }
.ranking .empty { display: block; color: var(--muted); text-align: center; padding: 14px 0; border: 0; }
.plays { list-style: none; margin: 0; padding: 0; }
.plays li {
  display: grid; grid-template-columns: 76px 1fr auto; align-items: center; gap: 10px;
  padding: 9px 2px; border-bottom: 1px solid var(--line); font-size: 14px;
}
.plays li:last-child { border-bottom: 0; }
.plays .when { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.plays li.fail .what { color: var(--danger); }
.plays .ms { font-weight: 750; font-variant-numeric: tabular-nums; }
.plays .empty { display: block; color: var(--muted); text-align: center; padding: 14px 0; border: 0; }
.foot a { color: inherit; }
.foot { text-align: center; color: var(--muted); font-size: 12px; margin: 8px 0 0; }

/* ---------- 게임 ---------- */
#game { position: fixed; inset: 0; overflow: hidden; }
.topbar {
  position: absolute; left: 0; right: 0; top: 0; z-index: 3;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 12px) 16px 0;
}
.ghost { background: rgba(255,255,255,.7); backdrop-filter: blur(8px); border-radius: 12px; padding: 8px 12px; font-weight: 650; font-size: 14px; }
.level-pill { background: var(--ink); color: #fff; border-radius: 999px; padding: 6px 12px; font-size: 13px; font-weight: 750; }
.pills { display: flex; gap: 6px; align-items: center; }
.clicks-pill { background: rgba(255,255,255,.8); color: var(--ink); border-radius: 999px; padding: 6px 12px; font-size: 13px; font-weight: 750; font-variant-numeric: tabular-nums; }
.clicks-pill.low { background: var(--danger); color: #fff; }
.instruction {
  position: absolute; left: 16px; right: 16px; z-index: 2;
  top: calc(env(safe-area-inset-top) + 72px);
  text-align: center; margin: 0; font-size: 18px; font-weight: 700; color: var(--ink);
  transition: opacity .3s;
  pointer-events: none;
}
.instruction.dim { opacity: .35; }
.arena { position: absolute; inset: 0; touch-action: none; z-index: 1; }

.bug {
  position: absolute; left: 0; top: 0;
  width: 94px; height: 128px;               /* app.js BUG_H와 맞출 것 (235×320 컷 비율) */
  margin-left: -47px; margin-top: -64px;
  will-change: transform;
  pointer-events: none;
}
.bug img {
  width: 100%; height: 100%; display: block;
  transform-origin: 50% 85%;
  filter: drop-shadow(0 8px 8px rgba(31,36,32,.16));
  -webkit-user-drag: none;
}

/* 숨쉬기(대기) — 컷 교체와 함께 아주 살짝 */
.bug.idle img { animation: breathe 2.6s ease-in-out infinite; }
@keyframes breathe { 0%,100% { transform: scale(1); } 50% { transform: scale(1.025, .985); } }

/* 페이크: 움찔 */
.bug.fake-twitch img { animation: twitch .38s ease-in-out; }
@keyframes twitch {
  20% { transform: translate(-5px, 1px) rotate(-5deg); }
  45% { transform: translate(6px, -1px) rotate(5deg); }
  70% { transform: translate(-3px, 0) rotate(-2deg); }
}

/* 달리기 — 위에서 본 컷이 거의 같아서 꿈틀(늘었다 줄었다)을 더한다 */
.bug.run img { animation: inch .14s ease-in-out infinite alternate; transform-origin: 50% 50%; }
@keyframes inch { from { transform: scale(.94, 1.06); } to { transform: scale(1.05, .93); } }

/* 잡힘 */
.bug.caught img { animation: squash .45s cubic-bezier(.2,.9,.3,1.2) forwards; }
@keyframes squash {
  0% { transform: scale(1); }
  35% { transform: scale(1.2, .7); }
  100% { transform: scale(1.08, .9); }
}

/* 탭 물결 */
.ripple {
  position: absolute; width: 56px; height: 56px; margin: -28px 0 0 -28px;
  border-radius: 50%; border: 3px solid var(--ink); opacity: .5;
  pointer-events: none; z-index: 4;
  animation: ripple .4s ease-out forwards;
}
.ripple.hit { border-color: var(--accent); opacity: .9; }
.ripple.miss { border-color: var(--danger); }
@keyframes ripple { from { transform: scale(.3); } to { transform: scale(1.6); opacity: 0; } }

/* 결과 */
.result {
  position: absolute; inset: 0; z-index: 5;
  display: flex; align-items: flex-end; justify-content: center;
  background: rgba(246, 243, 236, .55); backdrop-filter: blur(3px);
  padding: 16px 16px calc(env(safe-area-inset-bottom) + 16px);
}
.result[hidden] { display: none; }
.result-card {
  width: 100%; max-width: 440px;
  background: var(--card); border-radius: 26px; box-shadow: var(--shadow);
  padding: 24px 18px 18px; text-align: center;
  animation: rise .32s cubic-bezier(.2,.9,.3,1.1);
}
@keyframes rise { from { transform: translateY(30px); opacity: 0; } }
.result-kicker { font-size: 14px; font-weight: 700; color: var(--muted); }
.result-ms { font-size: 56px; font-weight: 850; letter-spacing: -0.03em; margin: 4px 0; font-variant-numeric: tabular-nums; }
.result-ms.fail { font-size: 32px; color: var(--danger); margin: 10px 0 6px; }
.result-sub { font-size: 15px; color: var(--muted); line-height: 1.5; min-height: 22px; }
.result-sub b { color: var(--ink); }
.result#rules { align-items: center; }
.rules-title { font-size: 24px; margin: 6px 0 14px; }
.rules-list { text-align: left; margin: 0 auto; padding: 0 0 0 22px; max-width: 300px; font-size: 16px; line-height: 1.5; color: var(--ink); }
.rules-list li { margin: 6px 0; }
.result-actions { display: flex; gap: 8px; margin-top: 20px; }
.result-actions button { flex: 1; }
.result-actions #homeBtn { flex: 0 0 auto; }
.result-actions #nextBtn { flex: 1.3; }
.result-actions [hidden] { display: none; }

@media (prefers-reduced-motion: reduce) {
  .hero-bug, .bug.idle img, .bug.run img { animation: none; }
}
