:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --text: #1c2333;
  --muted: #6b7489;
  --primary: #2f6fed;
  --primary-text: #ffffff;
  --user-bubble: #2f6fed;
  --ai-bubble: #ffffff;
  --border: #dde2ee;
  --danger: #e5484d;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151d;
    --surface: #1b212c;
    --text: #e7ebf3;
    --muted: #8d97ab;
    --primary: #5b8ef5;
    --user-bubble: #3b6fe0;
    --ai-bubble: #222a37;
    --border: #2c3544;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Noto Sans KR", "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  max-width: 720px;
  margin: 0 auto;
}
button { font: inherit; cursor: pointer; }

.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 18px; margin: 0; flex: 1; }
.levels { display: flex; background: var(--bg); border-radius: 10px; padding: 3px; }
.levels button {
  border: 0; background: transparent; color: var(--muted);
  padding: 6px 12px; border-radius: 8px; font-size: 14px;
}
.levels button.active { background: var(--primary); color: var(--primary-text); }
.icon-btn {
  border: 0; background: transparent; color: var(--text);
  font-size: 20px; width: 40px; height: 40px; border-radius: 10px;
}

.settings {
  display: grid; gap: 12px; padding: 14px 16px;
  background: var(--surface); border-bottom: 1px solid var(--border); font-size: 14px;
}
.settings label { display: grid; gap: 6px; }
.settings label.check { display: flex; align-items: center; gap: 8px; }
.settings select {
  font: inherit; padding: 8px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
}

.main { flex: 1; overflow-y: auto; display: flex; flex-direction: column; position: relative; }

/* 캐릭터 고르기 */
.picker { margin: 0 auto; padding: 20px 16px 28px; text-align: center; width: 100%; }
.picker .big { font-size: 20px; font-weight: 600; margin: 8px 0 14px; }

/* 성별 / 유형 필터 탭 */
.char-filter-tabs {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  margin-bottom: 16px;
  padding: 0;
  background: transparent;
  border: 0;
}
.filter-tab {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  transition: all .15s ease;
}
.filter-tab.active {
  border-color: var(--primary);
  background: var(--primary);
  color: var(--primary-text);
  box-shadow: 0 2px 6px rgba(47, 111, 237, 0.25);
}

/* 그리드 */
.character-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(136px, 1fr));
  gap: 12px;
  justify-content: center;
}
.character-card {
  position: relative;
  border: 2px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 18px;
  padding: 12px 10px 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  transition: transform .15s ease, border-color .15s ease, box-shadow .15s ease;
  text-align: center;
}
.character-card:hover {
  transform: translateY(-2px);
  border-color: var(--primary);
  box-shadow: 0 4px 12px rgba(0, 0, 0, .06);
}
.character-card:active {
  transform: scale(0.98);
}
.character-card.selected {
  border-color: var(--primary);
  box-shadow: 0 0 0 1px var(--primary);
}
.character-card svg {
  width: 88px;
  height: 88px;
  margin: 2px 0;
}
.character-card b {
  font-size: 15px;
  line-height: 1.2;
}
.character-card small {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
  word-break: keep-all;
}

/* 음성/성별 뱃지 */
.char-tag {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  letter-spacing: .2px;
}
.char-tag.tag-female {
  background: rgba(236, 72, 153, 0.12);
  color: #db2777;
}
.char-tag.tag-male {
  background: rgba(59, 130, 246, 0.12);
  color: #2563eb;
}
@media (prefers-color-scheme: dark) {
  .char-tag.tag-female {
    background: rgba(244, 114, 182, 0.18);
    color: #f472b6;
  }
  .char-tag.tag-male {
    background: rgba(96, 165, 250, 0.18);
    color: #93c5fd;
  }
}

/* 무대 */
.stage {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 10px; padding: 16px;
}
.speech {
  position: relative; max-width: 100%; min-width: 60%;
  background: var(--ai-bubble); border: 1px solid var(--border); border-radius: 20px;
  padding: 14px 16px 10px; font-size: 18px; line-height: 1.5; text-align: center;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .06);
}
.speech::after {
  content: ""; position: absolute; left: 50%; bottom: -10px; width: 18px; height: 18px;
  background: var(--ai-bubble); border-right: 1px solid var(--border); border-bottom: 1px solid var(--border);
  transform: translateX(-50%) rotate(45deg);
}
.speech-text { white-space: pre-wrap; word-break: break-word; min-height: 1.5em; }
.speech.hidden-text .speech-text { filter: blur(6px); user-select: none; cursor: pointer; }
.speech .tools { justify-content: center; margin-top: 8px; }
.thinking-dots { display: none; gap: 6px; justify-content: center; padding: 6px 0; }
.thinking-dots span {
  width: 9px; height: 9px; border-radius: 50%; background: var(--muted);
  animation: dot 1s infinite ease-in-out;
}
.thinking-dots span:nth-child(2) { animation-delay: .15s; }
.thinking-dots span:nth-child(3) { animation-delay: .3s; }
@keyframes dot { 0%, 80%, 100% { transform: scale(.5); opacity: .4; } 40% { transform: scale(1); opacity: 1; } }
.stage.thinking .thinking-dots { display: flex; }
.stage.thinking .speech-text, .stage.thinking .speech .tools, .stage.thinking .translation { display: none; }

.avatar { width: min(52vw, 220px); aspect-ratio: 1; margin-top: 8px; }
.avatar svg { width: 100%; height: 100%; overflow: visible; }
.avatar svg .eye, .avatar svg .mouth, .avatar svg .ear { transform-box: fill-box; transform-origin: center; }
.avatar svg .eye { animation: blink 4s infinite; }
@keyframes blink { 0%, 94%, 100% { transform: scaleY(1); } 97% { transform: scaleY(.1); } }
.avatar.idle svg { animation: breathe 3.5s infinite ease-in-out; }
@keyframes breathe { 50% { transform: translateY(3px); } }
.avatar.talking svg .mouth { animation: talk .22s infinite alternate ease-in-out; }
@keyframes talk { from { transform: scaleY(.35); } to { transform: scaleY(1.5); } }
.avatar.talking svg { animation: bob .5s infinite alternate ease-in-out; }
@keyframes bob { to { transform: translateY(-4px) rotate(-1deg); } }
.avatar.listening svg { transform: rotate(-6deg); transition: transform .3s; }
.avatar.listening svg .ear { animation: perk .6s infinite alternate ease-in-out; }
@keyframes perk { to { transform: scale(1.1) translateY(-4px); } }
.avatar.thinking svg { animation: ponder 1.6s infinite ease-in-out; }
@keyframes ponder { 50% { transform: rotate(4deg); } }
.char-name { font-weight: 700; font-size: 17px; }
.user-line { color: var(--muted); font-size: 15px; text-align: center; min-height: 1.4em; }
.user-line:not(:empty)::before { content: "나: "; font-weight: 600; }

/* 대화 기록 */
.log-panel {
  position: fixed; inset: 0; z-index: 10; background: var(--bg);
  display: flex; flex-direction: column; max-width: 720px; margin: 0 auto;
}
.log-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.log { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 85%; padding: 10px 14px; border-radius: 16px; line-height: 1.5; white-space: pre-wrap; word-break: break-word; }
.msg.user { align-self: flex-end; background: var(--user-bubble); color: #fff; border-bottom-right-radius: 4px; }
.msg.assistant { align-self: flex-start; background: var(--ai-bubble); border: 1px solid var(--border); border-bottom-left-radius: 4px; }

.translation { font-size: 14px; color: var(--muted); margin-top: 6px; }
.tools { display: flex; gap: 4px; }
.tools button {
  border: 1px solid var(--border); background: var(--surface); color: var(--muted);
  font-size: 12px; padding: 3px 8px; border-radius: 999px;
}
.error { align-self: center; color: var(--danger); font-size: 14px; }

.composer {
  display: grid; grid-template-columns: 1fr auto; grid-template-rows: auto auto; gap: 8px 12px;
  align-items: center;
  padding: 8px 16px calc(12px + env(safe-area-inset-bottom));
  background: var(--surface); border-top: 1px solid var(--border);
}
.status { grid-column: 1 / -1; font-size: 13px; color: var(--muted); min-height: 18px; text-align: center; }
#form { display: flex; gap: 6px; }
#textInput {
  flex: 1; min-width: 0; font: inherit; font-size: 16px; padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
}
.mic {
  width: 64px; height: 64px; border-radius: 50%; border: 0;
  background: var(--primary); color: #fff; font-size: 28px;
  box-shadow: 0 4px 14px rgba(47, 111, 237, .35);
}
.mic.listening { background: var(--danger); animation: pulse 1.2s infinite; }
.mic:disabled { opacity: .5; }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(229, 72, 77, .5); }
  100% { box-shadow: 0 0 0 18px rgba(229, 72, 77, 0); }
}

.primary, .secondary {
  border: 0; border-radius: 12px; padding: 12px 22px; font-size: 16px;
}
.primary { background: var(--primary); color: var(--primary-text); }
.secondary { background: var(--bg); color: var(--text); border: 1px solid var(--border); }

/* AI 음성 */
.muted { color: var(--muted); font-size: 13px; margin: 0; }
.ai-voice { display: grid; gap: 10px; padding: 12px; border: 1px solid var(--border); border-radius: 12px; }
.ai-voice > div { display: grid; gap: 10px; }
.ai-voice-head { display: flex !important; justify-content: space-between; align-items: baseline; }
.row { display: flex; gap: 8px; }
.row select { flex: 1; }
.small { padding: 6px 12px; font-size: 14px; }
.link { border: 0; background: none; color: var(--danger); font-size: 13px; justify-self: start; padding: 0; }
progress { width: 100%; height: 8px; }
.model-banner {
  margin-top: 16px; padding: 12px; border-radius: 14px; font-size: 14px; line-height: 1.6;
  background: var(--surface); border: 1px dashed var(--primary);
}
.model-banner progress { margin-top: 8px; }

/* 도우미: 답변 힌트, 우리말로 물어보기 */
.helpers { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.helper-btn {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font-size: 15px; font-weight: 600; padding: 9px 14px; border-radius: 999px;
}
.helper-btn.listening { background: var(--danger); border-color: var(--danger); color: #fff; animation: pulse 1.2s infinite; }
.stage.thinking .helpers { opacity: .5; }
.help-card {
  width: 100%; max-width: 480px; display: grid; gap: 8px;
  padding: 12px 14px; border-radius: 16px; text-align: center;
  background: var(--surface); border: 1px dashed var(--primary);
}
.help-card p { margin: 0; }
.help-title { font-weight: 700; font-size: 15px; }
.help-ko { font-size: 15px; color: var(--muted); word-break: keep-all; }
.phrase {
  display: grid; gap: 2px; text-align: left; width: 100%;
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
  padding: 10px 12px; border-radius: 12px;
}
.phrase:active { transform: scale(.98); }
.phrase-en { font-size: 17px; font-weight: 600; line-height: 1.4; }
.phrase-ko { font-size: 13px; color: var(--muted); word-break: keep-all; }
.words { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; }
.word {
  font-size: 13px; padding: 3px 10px; border-radius: 999px;
  background: rgba(47, 111, 237, .1); color: var(--primary);
}
