:root {
  --ink: #f4ead8;
  --ink-dim: #cdbfa6;
  --bg: #1c1410;
  --panel: rgba(28, 20, 16, .72);
  --line: rgba(244, 234, 216, .22);
  --gold: #d9a441;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: "Pretendard", "Apple SD Gothic Neo", "Noto Sans KR", "Malgun Gothic", system-ui, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}
button {
  font: inherit;
  color: var(--ink);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  cursor: pointer;
  touch-action: manipulation;
}
button:disabled { opacity: .45; cursor: default; }
button:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }

#scene { position: fixed; inset: 0; touch-action: none; }
#scene canvas { display: block; width: 100%; height: 100%; }

/* 시작 화면 */
#welcome {
  position: fixed; inset: 0; z-index: 3;
  display: flex; flex-direction: column; align-items: center; justify-content: flex-start;
  gap: 14px; padding: max(24px, env(safe-area-inset-top)) max(20px, env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) max(20px, env(safe-area-inset-left)); text-align: center;
  overflow-y: auto; overscroll-behavior: contain;
  background: linear-gradient(rgba(20, 14, 10, .78), rgba(20, 14, 10, .92));
  transition: opacity .5s;
}
/* 세로 화면은 가운데 정렬하고, 가로 화면처럼 높이가 부족하면 위부터 스크롤한다. */
#welcome > * { flex-shrink: 0; }
#welcome > :first-child { margin-top: auto; }
#welcome > :last-child { margin-bottom: auto; }
#welcome.gone { opacity: 0; pointer-events: none; }
.place { margin: 0; letter-spacing: .3em; font-size: 14px; color: var(--ink-dim); }
h1 { margin: 0; font-size: clamp(34px, 9vw, 52px); font-weight: 600; letter-spacing: .08em; }
.intro { margin: 4px 0 10px; font-size: 18px; line-height: 1.7; color: var(--ink-dim); }
.name-field { display: flex; flex-direction: column; gap: 6px; width: min(300px, 86vw); text-align: left; font-size: 15px; color: var(--ink-dim); }
.name-field small { font-size: 13px; }
.name-field input {
  min-height: 50px; padding: 0 16px; font: inherit; font-size: 18px; color: var(--ink);
  background: rgba(244, 234, 216, .08); border: 1px solid var(--line); border-radius: 14px;
}
.name-error { margin: -6px 0 0; width: min(300px, 86vw); text-align: left; font-size: 15px; color: #f0a48c; }
.name-field input:focus { outline: 2px solid var(--gold); outline-offset: 1px; }

/* 캐릭터 위 이름 */
#names { position: fixed; inset: 0; z-index: 1; pointer-events: none; overflow: hidden; }
#names[hidden] { display: none; }
.name-tag {
  position: absolute; left: 0; top: 0; padding: 3px 10px; border-radius: 999px;
  background: rgba(28, 20, 16, .62); color: var(--ink); font-size: 14px; font-weight: 600; white-space: nowrap;
  transform: translate(-50%, -100%); will-change: transform;
}
.name-tag.me { color: var(--gold); }

.primary {
  min-height: 60px; padding: 0 36px; font-size: 20px; font-weight: 600;
  background: var(--gold); color: #24180c; border-color: transparent;
}
.small { margin: 4px 0 0; font-size: 14px; color: var(--ink-dim); }
#loading { width: min(280px, 80vw); }
.bar { height: 4px; border-radius: 4px; background: var(--line); overflow: hidden; }
#bar-fill { height: 100%; width: 0; background: var(--gold); transition: width .2s; }
#loading-text { margin: 8px 0 0; font-size: 14px; color: var(--ink-dim); }

/* 법당 화면 */
#hall { position: fixed; inset: 0; z-index: 2; pointer-events: none; }
#hall > * { pointer-events: auto; }
#hall header {
  position: absolute; top: 0; left: 0; right: 0;
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px;
}
#hall header { flex-wrap: nowrap; }
#hall header button { min-height: 44px; padding: 0 13px; font-size: 15px; white-space: nowrap; }
.secondary { min-height: 52px; padding: 0 26px; font-size: 17px; }
#route {
  position: absolute; left: 0; right: 0; bottom: calc(112px + env(safe-area-inset-bottom));
  display: flex; align-items: center; justify-content: center; gap: 8px;
  max-width: 520px; margin: 0 auto; padding: 0 12px;
}
#route button { min-height: 52px; padding: 0 18px; font-size: 17px; font-weight: 600; flex: 0 0 auto; }
#place {
  flex: 1 1 auto; text-align: center; padding: 13px 8px; font-size: 18px; font-weight: 600;
  background: var(--panel); border: 1px solid var(--line); border-radius: 999px; white-space: nowrap;
}
#fade {
  position: fixed; inset: 0; z-index: 1; background: #14100c; opacity: 0;
  pointer-events: none; transition: opacity .35s;
}
#fade.on { opacity: 1; }
#together {
  margin-left: auto; padding: 10px 12px; font-size: 15px; white-space: nowrap;
  background: var(--panel); border: 1px solid var(--line); border-radius: 999px;
}
#together[hidden] { display: none; }
#together:not([hidden]) + #count { margin-left: 0; }
#count {
  margin-left: auto; padding: 10px 12px; font-size: 15px; white-space: nowrap;
  background: var(--panel); border: 1px solid var(--line); border-radius: 999px;
}
#count strong { font-size: 18px; color: var(--gold); }

#hint, #status {
  position: absolute; left: 50%; transform: translateX(-50%);
  margin: 0; padding: 8px 16px; border-radius: 999px;
  background: var(--panel); font-size: 16px; white-space: nowrap;
  pointer-events: none; transition: opacity .6s;
}
#hint { top: calc(70px + env(safe-area-inset-top)); color: var(--ink-dim); text-align: center; border-radius: 18px; line-height: 1.5; }
#hint .hint-zoom { font-size: 14px; }
#hint { white-space: normal; width: max-content; max-width: calc(100vw - 32px); }
.for-touch { display: none; }
@media (pointer: coarse) { .for-touch { display: inline; } .for-mouse { display: none; } }

/* 휴대폰 조이스틱 (마우스 쓰는 PC에서는 숨김) */
#stick {
  position: absolute; left: 18px; bottom: calc(178px + env(safe-area-inset-bottom));
  width: 120px; height: 120px; border-radius: 50%;
  background: rgba(28, 20, 16, .38); border: 2px solid rgba(244, 234, 216, .35);
  touch-action: none; display: none;
}
#stick.on { background: rgba(28, 20, 16, .55); border-color: var(--gold); }
#stick-knob {
  position: absolute; left: 50%; top: 50%; width: 52px; height: 52px; margin: -26px 0 0 -26px; border-radius: 50%;
  background: rgba(244, 234, 216, .85); box-shadow: 0 2px 8px rgba(0, 0, 0, .35); pointer-events: none;
}
#stick.on #stick-knob { background: var(--gold); }
@media (pointer: coarse) { #stick { display: block; } }
#hint.gone { opacity: 0; }
#status { bottom: calc(180px + env(safe-area-inset-bottom)); font-size: 17px; white-space: normal; width: max-content; max-width: calc(100vw - 32px); text-align: center; }
#status:empty { opacity: 0; }

#actions {
  position: absolute; left: 0; right: 0; bottom: 0;
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px;
  max-width: 520px; margin: 0 auto;
  padding: 10px 12px calc(14px + env(safe-area-inset-bottom));
}
#actions button {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-height: 84px; border-radius: 18px; font-size: 18px; font-weight: 600;
}
#actions .glyph { font-size: 24px; font-weight: 400; color: var(--gold); line-height: 1.1; }
#actions button[aria-pressed="true"] { background: var(--gold); color: #24180c; border-color: transparent; }
#actions button[aria-pressed="true"] .glyph { color: #24180c; }
#actions button:active:not(:disabled) { transform: scale(.97); }

#error {
  position: fixed; z-index: 5; left: 16px; right: 16px; top: 40%;
  padding: 18px; border-radius: 14px; text-align: center; font-size: 17px; line-height: 1.6;
  background: #3a1d16; border: 1px solid #a5543f;
}

@media (prefers-reduced-motion: reduce) {
  #welcome, #hint, #status, #bar-fill { transition: none; }
}
