/* 레이아웃: 화면 전체가 불 꺼진 방의 생일상(3D), UI는 그 위에 떠 있는 얇은 패널 */
:root {
  --night: #0d1014;          /* 방의 어둠 */
  --panel: #151a20;          /* 패널 바탕 */
  --panel-2: #1c232b;        /* 눌리는 면 */
  --line: rgba(232, 224, 210, 0.11);
  --text: #ece5d8;           /* 따뜻한 흰색 */
  --muted: #9b968c;
  --flame: #f0a45d;          /* 촛불 */
  --flame-ink: #2a1708;
  --danger: #e07b6a;

  --f-display: 'Hahmlet', 'Noto Serif KR', 'Batang', serif;
  --f-ui: 'IBM Plex Sans KR', 'Apple SD Gothic Neo', 'Malgun Gothic', system-ui, sans-serif;

  --r-sm: 8px;
  --r-md: 12px;
  --ease: cubic-bezier(.2, .7, .2, 1);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--night);
  color: var(--text);
  font: 400 14px/1.55 var(--f-ui);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--flame); outline-offset: 2px; }
svg.lucide { width: 18px; height: 18px; stroke-width: 1.75; flex: none; }

/* ---------- 공통 버튼 ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 8px;
  height: 42px; padding: 0 16px;
  border: 1px solid var(--line); border-radius: var(--r-sm);
  background: var(--panel-2); color: var(--text);
  font-weight: 500; white-space: nowrap;
  transition: background .2s, border-color .2s, transform .15s;
}
.btn:hover { background: #242c36; border-color: rgba(232, 224, 210, .2); }
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--flame); color: var(--flame-ink); border-color: transparent; font-weight: 600; }
.btn-primary:hover { background: #f5b373; border-color: transparent; }
.btn-ghost { background: transparent; }
.icon-btn {
  display: inline-grid; place-items: center;
  width: 36px; height: 36px; padding: 0;
  border: 1px solid var(--line); border-radius: var(--r-sm);
  background: transparent; color: var(--text);
  transition: background .2s;
}
.icon-btn:hover { background: var(--panel-2); }

/* ---------- 1. 잠금 화면 ---------- */
.lock {
  position: fixed; inset: 0; z-index: 50;
  display: grid; place-items: center;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  background: var(--night);
  transition: opacity .7s var(--ease), visibility .7s;
}
.lock.leaving { opacity: 0; visibility: hidden; }
.lock-glow {
  position: absolute; left: 50%; top: 38%; width: 520px; height: 520px; translate: -50% -50%;
  background: radial-gradient(circle, rgba(240, 164, 93, .16), rgba(240, 164, 93, 0) 62%);
  animation: breathe 4.5s ease-in-out infinite;
  pointer-events: none;
}
@keyframes breathe { 50% { opacity: .65; scale: .94; } }
.lock-inner { position: relative; width: min(320px, 100%); display: grid; justify-items: center; text-align: center; }
.lock-date {
  margin: 0; display: flex; align-items: center; gap: 14px;
  font: 300 15px/1 var(--f-display); letter-spacing: .3em; color: var(--flame);
}
.lock-date i { width: 1px; height: 14px; background: currentColor; opacity: .5; }
.lock-title { margin: 22px 0 10px; font: 500 clamp(34px, 9vw, 44px)/1.1 var(--f-display); letter-spacing: -.01em; }
.lock-hint { margin: 0 0 30px; color: var(--muted); }
.pin { display: flex; gap: 18px; height: 14px; }
.pin span { width: 12px; height: 12px; border-radius: 50%; border: 1.5px solid rgba(236, 229, 216, .45); transition: background .15s, border-color .15s, transform .15s; }
.pin span.on { background: var(--text); border-color: var(--text); transform: scale(1.08); }
.pin.wrong { animation: shake .42s; }
.pin.wrong span { border-color: var(--danger); background: transparent; }
.pin.ok span { background: var(--flame); border-color: var(--flame); }
@keyframes shake { 20%, 60% { transform: translateX(-8px); } 40%, 80% { transform: translateX(8px); } }
.pin-msg { min-height: 22px; margin: 14px 0 18px; font-size: 13px; color: var(--danger); }
.keypad { display: grid; grid-template-columns: repeat(3, 72px); gap: 14px 22px; }
.keypad button {
  width: 72px; height: 72px; border-radius: 50%;
  border: 1px solid var(--line); background: transparent;
  font: 400 26px/1 var(--f-display);
  display: grid; place-items: center;
  transition: background .15s;
}
.keypad button:hover { background: rgba(236, 229, 216, .05); }
.keypad button:active { background: rgba(236, 229, 216, .12); }
.keypad button[data-k="back"] { border-color: transparent; }
.keypad button svg.lucide { width: 24px; height: 24px; }

/* ---------- 2. 생일상 화면 ---------- */
.app, .stage { position: fixed; inset: 0; }
.stage canvas { display: block; width: 100%; height: 100%; touch-action: none; }
.stage-loading {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; gap: 12px;
  color: var(--muted); pointer-events: none; transition: opacity .6s;
}
.stage-loading.done { opacity: 0; }
.spinner { width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--line); border-top-color: var(--flame); animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.topbar {
  position: fixed; top: 0; left: 0; right: 0;
  display: flex; align-items: baseline; justify-content: space-between; gap: 24px;
  padding: calc(env(safe-area-inset-top) + 16px) max(16px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left));
  pointer-events: none;
}
.brand { display: flex; align-items: baseline; gap: 14px; padding: 10px 14px; border-radius: var(--r-sm); background: color-mix(in srgb, var(--panel) 88%, transparent); border: 1px solid var(--line); }
.brand-name { font: 500 22px/1 var(--f-display); }
.brand-date { font-size: 12px; letter-spacing: .14em; color: var(--flame); font-variant-numeric: tabular-nums; }
.topbar-tip { margin: 0; padding: 8px 12px; border-radius: var(--r-sm); background: color-mix(in srgb, var(--panel) 80%, transparent); color: var(--muted); font-size: 12.5px; }

.menu {
  position: fixed; left: max(16px, env(safe-area-inset-left)); top: calc(env(safe-area-inset-top) + 76px); bottom: calc(env(safe-area-inset-bottom) + 92px);
  width: 300px;
  display: flex; flex-direction: column;
  background: color-mix(in srgb, var(--panel) 94%, transparent);
  border: 1px solid var(--line); border-radius: var(--r-md);
  overflow: hidden;
}
.tabs { display: flex; padding: 6px; gap: 2px; border-bottom: 1px solid var(--line); }
.tabs button {
  flex: 1; height: 34px; border: 0; border-radius: 6px; background: transparent;
  color: var(--muted); font-weight: 500;
  transition: color .2s, background .2s;
}
.tabs button[aria-selected="true"] { background: var(--panel-2); color: var(--text); }
.tabs .count { margin-left: 4px; font-size: 11px; color: var(--flame); font-variant-numeric: tabular-nums; }
.menu-note { margin: 0; padding: 12px 16px 4px; font-size: 12px; color: var(--muted); }
.cards { flex: 1; overflow-y: auto; padding: 8px 8px 12px; display: grid; grid-template-columns: 1fr 1fr; align-content: start; gap: 4px; scrollbar-width: thin; scrollbar-color: #2b333d transparent; }
.card {
  position: relative;
  display: grid; justify-items: center; gap: 2px;
  padding: 14px 8px 12px; border: 1px solid transparent; border-radius: 10px;
  background: transparent; text-align: center;
  transition: background .2s, border-color .2s;
}
.card:hover { background: var(--panel-2); }
.card.current { border-color: rgba(240, 164, 93, .45); background: rgba(240, 164, 93, .06); }
.card img { width: 92px; height: 92px; border-radius: 50%; box-shadow: 0 10px 18px -8px rgba(0, 0, 0, .9), 0 0 0 1px rgba(255, 255, 255, .04); margin-bottom: 8px; transition: transform .35s var(--ease); }
.card:hover img { transform: translateY(-2px) rotate(-6deg); }
.card-name { font-weight: 500; }
.card-note { font-size: 11.5px; color: var(--muted); line-height: 1.35; }
.card-badge { position: absolute; top: 8px; right: 8px; font-size: 10.5px; color: var(--flame); letter-spacing: .04em; }
.cards-empty { grid-column: 1 / -1; display: grid; justify-items: center; gap: 10px; padding: 40px 16px; text-align: center; color: var(--muted); }
.cards-empty svg.lucide { width: 28px; height: 28px; color: var(--flame); }

.dock {
  position: fixed; left: 50%; translate: -50% 0; bottom: calc(env(safe-area-inset-bottom) + 20px);
  display: flex; align-items: center; gap: 8px;
  padding: 8px; max-width: calc(100vw - 32px);
  background: color-mix(in srgb, var(--panel) 94%, transparent);
  border: 1px solid var(--line); border-radius: var(--r-md);
}
.dock-sep { width: 1px; height: 24px; background: var(--line); margin: 0 4px; }
.stepper { display: flex; align-items: center; gap: 6px; padding-left: 8px; }
.stepper-label { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); margin-right: 4px; }
.stepper-label svg.lucide { color: var(--flame); width: 16px; height: 16px; }
.stepper-val { min-width: 26px; text-align: center; font: 500 17px/1 var(--f-display); font-variant-numeric: tabular-nums; }

.item-pop {
  position: fixed; z-index: 5; translate: -50% -100%;
  display: flex; align-items: center; gap: 10px;
  padding: 6px 6px 6px 14px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 12px 30px -10px rgba(0, 0, 0, .8);
}
.item-pop-name { font-weight: 500; white-space: nowrap; }
.item-pop .icon-btn { color: var(--danger); }

/* 축하 화면으로 넘어갈 때 차리기 UI 숨김 */
body.partying .topbar, body.partying .menu, body.partying .dock, body.partying .item-pop { opacity: 0; pointer-events: none; transform: translateY(8px); }
.topbar, .menu, .dock { transition: opacity .6s var(--ease), transform .6s var(--ease); }

/* ---------- 3. 생일 축하 ---------- */
.party { position: fixed; inset: 0; pointer-events: none; }
.party > * { pointer-events: auto; }
.party-back { position: absolute; top: calc(env(safe-area-inset-top) + 16px); left: max(16px, env(safe-area-inset-left)); background: rgba(13, 16, 20, .55); }

.cam {
  position: absolute; top: calc(env(safe-area-inset-top) + 16px); right: max(16px, env(safe-area-inset-right));
  width: min(30vw, 380px); margin: 0;
  animation: rise .8s .6s var(--ease) both;
}
.cam-frame { position: relative; aspect-ratio: 4 / 3; border-radius: var(--r-md); overflow: hidden; background: #07090b; border: 1px solid var(--line); box-shadow: 0 24px 50px -20px rgba(0, 0, 0, .9); }
.cam video, .cam canvas { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; transform: scaleX(-1); }
.cam-empty { position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 8px; color: var(--muted); font-size: 13px; text-align: center; padding: 16px; }
.cam-cap { margin-top: 8px; font-size: 12px; color: var(--muted); text-align: right; }
@keyframes rise { from { opacity: 0; transform: translateY(12px); } }

.lyrics {
  position: absolute; left: 50%; translate: -50% 0; top: calc(env(safe-area-inset-top) + 7vh);
  display: grid; justify-items: center; gap: 6px; text-align: center; pointer-events: none;
}
.lyric { font: 500 clamp(24px, 3.6vw, 40px)/1.2 var(--f-display); color: rgba(236, 229, 216, .18); position: relative; white-space: nowrap; transition: opacity .5s; }
.lyric b { position: absolute; inset: 0; font-weight: inherit; color: var(--text); clip-path: inset(0 calc(100% - var(--p, 0%)) 0 0); text-shadow: 0 0 24px rgba(240, 164, 93, .45); }
.lyric.gone { opacity: 0; }
.lyric-next { font-size: clamp(15px, 1.7vw, 20px); color: rgba(236, 229, 216, .38); }

.blow {
  position: absolute; left: 50%; translate: -50% 0; bottom: calc(env(safe-area-inset-bottom) + 20px);
  width: min(440px, calc(100vw - 32px));
  padding: 16px 16px 12px;
  background: rgba(13, 16, 20, .72); border: 1px solid var(--line); border-radius: var(--r-md);
  animation: rise .8s 2.6s var(--ease) both;
}
.blow-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.blow-title { font: 500 17px/1.3 var(--f-display); }
.blow-count { font-size: 12px; color: var(--flame); font-variant-numeric: tabular-nums; white-space: nowrap; }
.meter { margin: 12px 0 8px; height: 4px; border-radius: 2px; background: rgba(236, 229, 216, .08); overflow: hidden; }
.meter span { display: block; height: 100%; width: 0; background: linear-gradient(90deg, #c9773f, var(--flame), #ffe0b5); transition: width .08s linear; }
.blow-sub { margin: 0; font-size: 12.5px; color: var(--muted); }
.blow-hold { margin-top: 10px; width: 100%; justify-content: center; user-select: none; -webkit-user-select: none; touch-action: none; }
.blow-hold.holding { background: rgba(240, 164, 93, .14); border-color: rgba(240, 164, 93, .4); }
.party-tools { display: flex; align-items: center; gap: 6px; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); }
.stepper-sm { padding-left: 0; margin-right: auto; }

.finale {
  position: absolute; left: 0; right: 0; top: calc(env(safe-area-inset-top) + 6vh);
  display: grid; justify-items: center; gap: 10px; text-align: center; pointer-events: none;
}
.finale-small { margin: 0; font-size: 13px; letter-spacing: .24em; color: var(--flame); animation: rise 1s .2s var(--ease) both; }
.finale-big { margin: 0; font: 500 clamp(34px, 6vw, 68px)/1.1 var(--f-display); text-shadow: 0 2px 30px rgba(0, 0, 0, .6); animation: rise 1.2s .45s var(--ease) both; }
.confetti { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none !important; }

/* ---------- 사진 추가 시트 ---------- */
.sheet {
  width: min(520px, calc(100vw - 24px)); max-height: calc(100dvh - 24px);
  padding: 0; border: 1px solid var(--line); border-radius: 14px;
  background: var(--panel); color: var(--text);
  overflow: auto;
}
.sheet::backdrop { background: rgba(5, 7, 9, .72); }
.sheet[open] { animation: rise .45s var(--ease); }
.sheet-inner { margin: 0; padding: 18px; display: grid; gap: 14px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.sheet-head h2 { margin: 0; font: 500 19px/1.2 var(--f-display); }
.cap-stage { position: relative; aspect-ratio: 1; border-radius: 10px; overflow: hidden; background: #07090b; }
.cap-stage video, .cap-stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.cap-stage canvas { touch-action: none; cursor: grab; }
.cap-guide { position: absolute; inset: 8%; border-radius: 50%; border: 1.5px dashed rgba(236, 229, 216, .55); box-shadow: 0 0 0 999px rgba(7, 9, 11, .45); pointer-events: none; }
.cap-stage.editing .cap-guide { border-style: solid; border-color: rgba(240, 164, 93, .7); box-shadow: 0 0 0 999px rgba(7, 9, 11, .72); }
.cap-msg { position: absolute; left: 0; right: 0; bottom: 10px; margin: 0; text-align: center; font-size: 12.5px; color: var(--text); text-shadow: 0 1px 6px #000; pointer-events: none; }
.cap-actions { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.cap-actions .btn { flex: 1; justify-content: center; padding: 0 10px; }
.shutter { width: 64px; height: 64px; flex: none; border-radius: 50%; border: 2px solid var(--text); background: transparent; padding: 4px; }
.shutter span { display: block; width: 100%; height: 100%; border-radius: 50%; background: var(--text); transition: transform .12s; }
.shutter:active span { transform: scale(.9); }
.cap-edit { display: grid; gap: 14px; }
.field { display: grid; gap: 6px; margin: 0; padding: 0; border: 0; }
.field-label { font-size: 12px; color: var(--muted); padding: 0; }
.field input[type="text"] { height: 40px; padding: 0 12px; border-radius: var(--r-sm); border: 1px solid var(--line); background: var(--night); }
.field input[type="range"] { accent-color: var(--flame); width: 100%; }
.vessel { display: flex; flex-wrap: wrap; gap: 6px; }
.vessel legend { margin-bottom: 6px; }
.vessel label { flex: 1; min-width: 92px; }
.vessel input { position: absolute; opacity: 0; }
.vessel span { display: grid; place-items: center; height: 36px; border-radius: var(--r-sm); border: 1px solid var(--line); font-size: 13px; color: var(--muted); cursor: pointer; transition: all .15s; }
.vessel input:checked + span { border-color: var(--flame); color: var(--text); background: rgba(240, 164, 93, .08); }
.vessel input:focus-visible + span { outline: 2px solid var(--flame); outline-offset: 2px; }
.cap-edit-row { display: flex; gap: 8px; }
.cap-edit-row .btn { flex: 1; justify-content: center; }

.toast {
  position: fixed; z-index: 60; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 96px);
  translate: -50% 12px; opacity: 0;
  padding: 10px 16px; border-radius: 10px;
  background: var(--text); color: var(--night); font-weight: 500;
  transition: opacity .3s, translate .3s var(--ease); pointer-events: none;
  max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; translate: -50% 0; }

/* ---------- 좁은 화면 ---------- */
@media (max-width: 820px) {
  .topbar-tip { display: none; }
  .topbar { padding-top: calc(env(safe-area-inset-top) + 14px); }
  .menu {
    left: max(8px, env(safe-area-inset-left)); right: max(8px, env(safe-area-inset-right)); width: auto;
    top: auto; bottom: calc(env(safe-area-inset-bottom) + 72px); height: 196px;
  }
  .menu-note { display: none; }
  .cards { grid-template-columns: none; grid-auto-flow: column; grid-auto-columns: 104px; overflow-x: auto; overflow-y: hidden; }
  .card { padding: 8px 4px; }
  .card img { width: 68px; height: 68px; margin-bottom: 4px; }
  .card-note { display: none; }
  .dock { bottom: calc(env(safe-area-inset-bottom) + 10px); gap: 4px; padding: 6px; }
  .dock .btn span { display: none; }
  .dock .btn { padding: 0 12px; }
  .dock .btn-primary span { display: inline; }
  .stepper-label span, .stepper-label { font-size: 0; margin-right: 0; }
  .stepper-label svg.lucide { width: 16px; height: 16px; }
  .cam { width: 38vw; top: calc(env(safe-area-inset-top) + 64px); }
  .lyrics { top: auto; bottom: 280px; }
  .finale { top: 18vh; }
  .toast { bottom: calc(env(safe-area-inset-bottom) + 280px); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-delay: 0s !important; transition-duration: .01ms !important; }
}
