:root {
  --bg: #fff7ec;
  --bg-2: #ffeccf;
  --surface: #ffffff;
  --surface-2: #fff3e0;
  --text: #3b2a1f;
  --text-2: #7a5f4c;
  --line: #f1dcc0;
  --accent: #ff8a3d;
  --accent-2: #ffb547;
  --accent-ink: #ffffff;
  --kid: #ffe08a;
  --kid-ink: #3b2a1f;
  --friend: #ffffff;
  --en: #2f7de1;
  --en-bg: #e8f1ff;
  --listen: #2bb673;
  --think: #8a6cf0;
  --speak: #ff8a3d;
  --danger: #d9534f;
  --shadow: 0 6px 20px rgba(150, 90, 30, .14);
  --radius: 22px;
  --font: "Pretendard", "Apple SD Gothic Neo", "Noto Sans KR", "Malgun Gothic", system-ui, -apple-system, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1f1915;
    --bg-2: #2a211c;
    --surface: #2e2520;
    --surface-2: #382d26;
    --text: #fbeee0;
    --text-2: #c9b3a1;
    --line: #463830;
    --accent: #ff9a52;
    --accent-2: #ffbf5e;
    --accent-ink: #2a1a0e;
    --kid: #6b5320;
    --kid-ink: #fff3d6;
    --friend: #342a24;
    --en: #8cc0ff;
    --en-bg: #1f3048;
    --listen: #45d08e;
    --think: #a993ff;
    --speak: #ff9a52;
    --shadow: 0 6px 20px rgba(0, 0, 0, .35);
  }
}
:root[data-theme="dark"] {
  --bg: #1f1915; --bg-2: #2a211c; --surface: #2e2520; --surface-2: #382d26;
  --text: #fbeee0; --text-2: #c9b3a1; --line: #463830; --accent: #ff9a52; --accent-2: #ffbf5e;
  --accent-ink: #2a1a0e; --kid: #6b5320; --kid-ink: #fff3d6; --friend: #342a24; --en: #8cc0ff;
  --en-bg: #1f3048; --listen: #45d08e; --think: #a993ff; --speak: #ff9a52; --shadow: 0 6px 20px rgba(0,0,0,.35);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  background-image: radial-gradient(120% 60% at 50% 0%, var(--bg-2) 0%, var(--bg) 60%);
  color: var(--text);
  font-family: var(--font);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
  padding: env(safe-area-inset-top) 16px env(safe-area-inset-bottom);
}

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 0 8px;
}
.brand {
  flex: 1;
  margin: 0;
  font-size: 1.2rem;
  font-weight: 800;
  letter-spacing: -.02em;
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  white-space: nowrap;
}
.brand-dot {
  width: 22px; height: 22px; border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, var(--accent-2), var(--accent));
  box-shadow: 0 2px 6px rgba(255, 138, 61, .45);
  flex: none;
}
.mode-toggle {
  display: flex;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px;
}
.mode-btn {
  border: 0;
  background: transparent;
  padding: 7px 14px;
  border-radius: 999px;
  font-weight: 700;
  font-size: .92rem;
  color: var(--text-2);
}
.mode-btn.is-active { background: var(--accent); color: var(--accent-ink); }
.icon-btn {
  position: relative;
  border: 0;
  background: transparent;
  width: 44px; height: 44px;
  border-radius: 50%;
  display: grid; place-items: center;
  color: var(--text-2);
  font-size: 1.1rem;
}
.icon-btn:focus-visible, .mic:focus-visible, .mode-btn:focus-visible, button:focus-visible, input:focus-visible, select:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}
.hold-ring {
  position: absolute; inset: 2px; border-radius: 50%;
  border: 3px solid var(--accent);
  opacity: 0; transform: scale(.6);
}
.icon-btn.is-holding .hold-ring { animation: hold .7s linear forwards; }
@keyframes hold { from { opacity: .3; transform: scale(.6); } to { opacity: 1; transform: scale(1); } }

/* ---------- notice ---------- */
.notice {
  display: flex; align-items: flex-start; gap: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-left: 5px solid var(--accent);
  border-radius: 14px;
  padding: 10px 12px;
  margin-bottom: 8px;
  box-shadow: var(--shadow);
  font-size: .92rem;
  line-height: 1.45;
}
.notice[hidden] { display: none; }
.notice p { margin: 0; flex: 1; white-space: pre-line; }
.notice-close { border: 0; background: transparent; color: var(--text-2); padding: 2px 6px; font-size: 1rem; }

/* ---------- chat ---------- */
.chat {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 10px 10px 18px;
  margin: 0 -10px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scroll-behavior: smooth;
}
.welcome { margin: auto 0; text-align: center; color: var(--text-2); padding: 24px 8px; }
.welcome-title { font-size: 1.5rem; font-weight: 800; color: var(--text); margin: 0 0 8px; }
.welcome-sub { margin: 0; line-height: 1.6; }

.msg { display: flex; flex-direction: column; max-width: 86%; animation: pop .22s ease-out; }
@keyframes pop { from { opacity: 0; transform: translateY(6px) scale(.98); } to { opacity: 1; transform: none; } }
.msg.kid { align-self: flex-end; align-items: flex-end; }
.msg.friend { align-self: flex-start; align-items: flex-start; }
.msg.log { align-self: stretch; max-width: 100%; }
.bubble {
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 1.25rem;
  line-height: 1.45;
  word-break: keep-all;
  overflow-wrap: anywhere;
  box-shadow: var(--shadow);
}
.kid .bubble { background: var(--kid); color: var(--kid-ink); border-bottom-right-radius: 6px; }
.friend .bubble { background: var(--friend); border: 1px solid var(--line); border-bottom-left-radius: 6px; }
.log .bubble {
  background: transparent; box-shadow: none; border: 1px dashed var(--line);
  font-size: 1rem; color: var(--text-2); border-radius: 12px; padding: 8px 12px;
}
.log .bubble::before { content: "기록 · "; font-weight: 700; }
.meta { font-size: .75rem; color: var(--text-2); margin: 4px 8px 0; }

.en-card {
  margin-top: 8px;
  display: flex; align-items: center; gap: 10px;
  background: var(--en-bg);
  color: var(--en);
  border-radius: 18px;
  padding: 10px 12px 10px 18px;
  box-shadow: var(--shadow);
  max-width: 100%;
}
.en-word {
  font-size: 2.6rem;
  font-weight: 800;
  letter-spacing: -.01em;
  line-height: 1.1;
  font-family: "Nunito", "Avenir Next", "Segoe UI", system-ui, sans-serif;
  overflow-wrap: anywhere;
}
.replay {
  flex: none;
  width: 48px; height: 48px;
  border-radius: 50%;
  border: 0;
  background: var(--surface);
  color: var(--en);
  font-size: 1.3rem;
  display: grid; place-items: center;
}

/* ---------- controls ---------- */
.controls {
  display: flex; flex-direction: column; align-items: center;
  gap: 8px;
  padding: 8px 0 14px;
}
.status {
  margin: 0;
  display: inline-flex; align-items: center; gap: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 14px;
  font-weight: 700;
  font-size: .98rem;
  color: var(--text-2);
}
.status-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--line); }
.interim { margin: 0; min-height: 1.4em; font-size: 1rem; color: var(--text-2); text-align: center; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.mic {
  position: relative;
  width: 188px; height: 188px;
  border-radius: 50%;
  border: 0;
  background: radial-gradient(circle at 35% 30%, var(--accent-2), var(--accent));
  color: var(--accent-ink);
  box-shadow: 0 12px 30px rgba(255, 138, 61, .45), inset 0 -6px 0 rgba(0, 0, 0, .08);
  display: grid; place-items: center;
  transition: transform .12s ease, filter .2s ease;
  touch-action: manipulation;
  -webkit-user-select: none; user-select: none;
}
.mic:active { transform: scale(.96); }
.mic svg { width: 84px; height: 84px; grid-area: 1 / 1; }
.mic .stop-icon { display: none; width: 64px; height: 64px; }
.mic-ring { position: absolute; inset: -10px; border-radius: 50%; border: 6px solid transparent; pointer-events: none; }

.text-toggle {
  border: 0; background: transparent; color: var(--text-2);
  font-size: .92rem; padding: 6px 10px; border-radius: 10px;
}
.text-form { width: 100%; display: flex; flex-wrap: wrap; gap: 8px; }
.text-form[hidden] { display: none; }
.text-form input {
  flex: 1; min-width: 0;
  padding: 12px 14px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--surface);
  font-size: 1.05rem;
}
.send-btn, .primary-btn {
  border: 0; border-radius: 14px; padding: 12px 18px;
  background: var(--accent); color: var(--accent-ink); font-weight: 800;
}
.text-hint { width: 100%; margin: 0; font-size: .82rem; color: var(--text-2); }

/* ---------- state styling ---------- */
.app[data-state="listening"] .status-dot { background: var(--listen); animation: blink 1s infinite; }
.app[data-state="listening"] .status { color: var(--listen); }
.app[data-state="listening"] .mic { background: radial-gradient(circle at 35% 30%, #6fe0a8, var(--listen)); box-shadow: 0 12px 30px rgba(43, 182, 115, .45); }
.app[data-state="listening"] .mic-ring { border-color: var(--listen); animation: ring 1.4s ease-out infinite; }
.app[data-state="thinking"] .status-dot { background: var(--think); animation: blink .6s infinite; }
.app[data-state="thinking"] .status { color: var(--think); }
.app[data-state="thinking"] .mic { filter: saturate(.7); }
.app[data-state="speaking"] .status-dot { background: var(--speak); animation: blink .4s infinite; }
.app[data-state="speaking"] .status { color: var(--speak); }
.app.mic-on .mic .mic-icon { display: none; }
.app.mic-on .mic .stop-icon { display: block; }
.app[data-mode="parent"] .brand-dot { background: radial-gradient(circle at 35% 35%, #b9a6ff, var(--think)); }
@keyframes blink { 50% { opacity: .35; } }
@keyframes ring { from { transform: scale(.95); opacity: .9; } to { transform: scale(1.12); opacity: 0; } }

/* ---------- settings ---------- */
.settings {
  width: min(560px, 100vw);
  max-width: 100vw;
  max-height: 100dvh;
  margin: auto auto 0;
  border: 0;
  border-radius: 24px 24px 0 0;
  padding: 0;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 -10px 40px rgba(0, 0, 0, .25);
}
.settings::backdrop { background: rgba(30, 18, 8, .45); }
.settings-form { padding: 8px 18px calc(18px + env(safe-area-inset-bottom)); }
.settings-head { display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; background: var(--surface); padding: 8px 0; z-index: 1; }
.settings-head h2 { margin: 0; font-size: 1.2rem; }
.field { margin: 14px 0; display: flex; flex-direction: column; gap: 6px; }
.field label { font-weight: 700; display: flex; justify-content: space-between; gap: 8px; }
.field output { color: var(--accent); font-weight: 800; }
.field input[type="text"], .field input[type="password"], .field select {
  width: 100%;
  padding: 11px 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface-2);
}
.field input[type="range"] { width: 100%; accent-color: var(--accent); height: 32px; }
.key-row { display: flex; gap: 8px; }
.key-row input { flex: 1; min-width: 0; }
.ghost-btn { border: 1px solid var(--line); background: transparent; border-radius: 12px; padding: 10px 14px; font-weight: 700; }
.opt { color: var(--text-2); font-weight: 500; }
.help { margin: 0; font-size: .84rem; color: var(--text-2); line-height: 1.5; }
.help.small { font-size: .78rem; }
.actions { flex-direction: row; flex-wrap: wrap; }

@media (min-width: 600px) {
  .settings { margin: auto; border-radius: 24px; }
}
@media (max-height: 640px) {
  .mic { width: 180px; height: 180px; }
  .bubble { font-size: 1.15rem; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition: none !important; }
}
