:root {
  --bg: #14110f;
  --panel: #1d1917;
  --line: #332c28;
  --text: #ece5df;
  --dim: #9c8f86;
  --accent: #c8452f;
  --accent-hi: #e05a41;
  --ok: #6fae6a;
  --warn: #d9a441;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Pretendard", "Malgun Gothic", system-ui, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

#stage { display: block; width: 100vw; height: 100dvh; touch-action: none; }

/* ---------- 로딩 ---------- */
#loading {
  position: fixed; inset: 0; z-index: 30;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 1rem;
  background: var(--bg); transition: opacity .4s ease;
}
#loading.done { opacity: 0; pointer-events: none; }
#loading p { color: var(--dim); font-size: .9rem; margin: 0; }
.spinner {
  width: 34px; height: 34px; border-radius: 50%;
  border: 3px solid var(--line); border-top-color: var(--accent);
  animation: spin 900ms linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- 상단 바 ---------- */
#topbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 12;
  display: flex; align-items: center; justify-content: space-between;
  gap: .5rem; padding: max(.75rem, env(safe-area-inset-top)) .9rem .75rem;
  pointer-events: none;
}
#topbar > * { pointer-events: auto; }

.status {
  font-size: .72rem; color: var(--dim);
  background: color-mix(in srgb, var(--panel) 85%, transparent);
  backdrop-filter: blur(8px);
  border: 1px solid var(--line); border-radius: 99px;
  padding: .25rem .65rem;
  max-width: 65vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.status.busy { color: var(--warn); border-color: var(--warn); }
.status.ok   { color: var(--ok);   border-color: var(--ok); }
.status.err  { color: var(--accent-hi); border-color: var(--accent-hi); }

button.icon {
  width: 36px; height: 36px; padding: 0; font-size: 1rem;
  display: grid; place-items: center;
  background: color-mix(in srgb, var(--panel) 85%, transparent);
  backdrop-filter: blur(8px);
  border: 1px solid var(--line); border-radius: 10px; color: var(--text);
  cursor: pointer;
}

/* ---------- 자막 ---------- */
#subtitle {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(190px + var(--safe-b)); z-index: 11;
  width: min(680px, 92vw); text-align: center; pointer-events: none;
}
#subtitle p {
  margin: 0 0 .4rem; padding: .5rem .9rem; border-radius: 10px;
  font-size: clamp(.85rem, 2.6vw, 1rem); line-height: 1.5;
  background: color-mix(in srgb, #000 55%, transparent);
  backdrop-filter: blur(6px);
}
#subtitle .me { color: var(--dim); font-size: clamp(.75rem, 2.2vw, .85rem); }
#subtitle .ai { color: #f6e3c4; }

/* ---------- 하단 푸시투토크 ---------- */
#talkbar {
  position: fixed; left: 0; right: 0; z-index: 12;
  bottom: calc(1.5rem + var(--safe-b));
  display: flex; flex-direction: column; align-items: center; gap: .6rem;
  pointer-events: none;
}
#talkbar > * { pointer-events: auto; }

#btn-talk {
  position: relative;
  width: 92px; height: 92px; border-radius: 50%;
  border: none; padding: 0; cursor: pointer;
  background: var(--accent);
  color: #fff;
  display: grid; place-items: center;
  box-shadow: 0 8px 28px rgba(0,0,0,.5), inset 0 -2px 8px rgba(0,0,0,.25);
  /* 모바일에서 길게 눌러도 스크롤/선택/컨텍스트메뉴가 안 뜨게 */
  touch-action: none;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  transition: transform .12s ease, background .12s ease;
}
#btn-talk:active { transform: scale(.94); }
#btn-talk svg {
  width: 40px; height: 40px; fill: none;
  stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
}
#btn-talk svg path:first-child { fill: currentColor; stroke: none; }

#btn-talk.recording { background: var(--accent-hi); }
#talk-ring {
  position: absolute; inset: -6px; border-radius: 50%;
  border: 3px solid var(--accent-hi); opacity: 0; pointer-events: none;
}
#btn-talk.recording #talk-ring { animation: ring 1.1s ease-out infinite; }
@keyframes ring {
  0%   { opacity: .85; transform: scale(1); }
  100% { opacity: 0;   transform: scale(1.35); }
}

#btn-talk:disabled { opacity: .5; cursor: not-allowed; }
#btn-talk:disabled + #talk-hint { opacity: .5; }

#talk-hint {
  margin: 0; font-size: .75rem; color: var(--dim);
  background: color-mix(in srgb, #000 45%, transparent);
  padding: .25rem .7rem; border-radius: 99px; backdrop-filter: blur(6px);
}

/* ---------- 설정 패널 ---------- */
#panel {
  position: fixed; z-index: 20;
  top: 0; right: 0; height: 100dvh; width: min(340px, 88vw);
  overflow-y: auto;
  background: color-mix(in srgb, var(--panel) 96%, transparent);
  backdrop-filter: blur(12px);
  border-left: 1px solid var(--line);
  padding: max(1rem, env(safe-area-inset-top)) 1rem calc(1rem + var(--safe-b));
}
#panel[hidden] { display: none; }

#panel header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 1rem;
}
#panel h1 { font-size: 1rem; margin: 0; }

#panel section { border-top: 1px solid var(--line); padding-top: .85rem; margin-top: .85rem; }
#panel section:first-of-type { border-top: 0; margin-top: 0; padding-top: 0; }

#panel h2 {
  font-size: .72rem; font-weight: 600; text-transform: uppercase;
  letter-spacing: .07em; color: var(--dim); margin: 0 0 .6rem;
}
#panel h2 em {
  font-style: normal; font-weight: 400; text-transform: none;
  letter-spacing: 0; opacity: .65; margin-left: .35rem;
}

label { display: block; font-size: .78rem; color: var(--dim); margin-bottom: .6rem; }
label.check { display: flex; align-items: center; gap: .45rem; }

input[type=text] {
  width: 100%; margin-top: .25rem;
  background: #100d0c; color: var(--text);
  border: 1px solid var(--line); border-radius: 6px;
  padding: .5rem; font-size: 16px;   /* iOS 자동 확대 방지: 16px 미만이면 확대됨 */
  font-family: ui-monospace, monospace;
}
input[type=text]:focus { outline: none; border-color: var(--accent); }
input[type=range] { width: 100%; margin-top: .35rem; accent-color: var(--accent); }

.row { display: flex; gap: .5rem; }
.row > button { flex: 1; }

button.ghost {
  font: inherit; font-size: .82rem; cursor: pointer;
  border-radius: 7px; padding: .55rem .7rem;
  border: 1px solid var(--line); background: #262120; color: var(--text);
}
button.ghost:hover:not(:disabled) { background: #302a28; border-color: #4a403b; }
button.wide { width: 100%; }

.hint { font-size: .7rem; color: var(--dim); margin: .45rem 0 0; line-height: 1.5; }
.timing { color: var(--dim); font-size: .7rem; font-family: ui-monospace, monospace; margin: .5rem 0 0; }

/* ---------- viseme 미터 ---------- */
#meters {
  position: fixed; left: .9rem; bottom: calc(1rem + var(--safe-b)); z-index: 11;
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 2px;
  width: min(210px, 55vw);
}
#meters[hidden] { display: none; }
.meter { height: 22px; background: rgba(0,0,0,.55); border-radius: 2px; position: relative; overflow: hidden; }
.meter i {
  position: absolute; bottom: 0; left: 0; right: 0; height: 0%;
  background: var(--accent);
}
.meter span {
  position: absolute; inset: 0; display: grid; place-items: center;
  font-size: .55rem; color: var(--dim); letter-spacing: .04em; font-family: ui-monospace, monospace;
}

/* ---------- 모바일 ---------- */
@media (max-width: 640px) {
  #btn-talk { width: 84px; height: 84px; }
  #subtitle { bottom: calc(175px + var(--safe-b)); }
  #meters { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

.build {
  margin: 1rem 0 0; text-align: right;
  font-size: .65rem; color: var(--dim); opacity: .7;
  font-family: ui-monospace, monospace;
}
