/* 우리집 AI */
:root {
  --bg: #f6f4ef;
  --panel: #ffffff;
  --sidebar: #efece5;
  --line: #e2ddd3;
  --text: #22232a;
  --muted: #6b6a72;
  --primary: #3f6df2;
  --primary-ink: #ffffff;
  --primary-soft: #e6ecfe;
  --user-bubble: #3f6df2;
  --user-ink: #ffffff;
  --ai-bubble: #ffffff;
  --danger: #d64545;
  --ok: #2f9e5b;
  --warn: #d98a1c;
  --off: #9b9aa3;
  --shadow: 0 1px 2px rgba(30, 28, 20, .06), 0 4px 16px rgba(30, 28, 20, .06);
  --radius: 16px;
  --fs: 17px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17181c; --panel: #212329; --sidebar: #1c1d22; --line: #33353d;
    --text: #ecebf0; --muted: #a2a1ab; --primary: #6f90ff; --primary-ink: #0d1020;
    --primary-soft: #252d48; --user-bubble: #4a6fe8; --user-ink: #fff; --ai-bubble: #24262d;
    --danger: #ff6b6b; --ok: #4cc27a; --warn: #f0a93a; --off: #6e6d76;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #17181c; --panel: #212329; --sidebar: #1c1d22; --line: #33353d;
  --text: #ecebf0; --muted: #a2a1ab; --primary: #6f90ff; --primary-ink: #0d1020;
  --primary-soft: #252d48; --user-bubble: #4a6fe8; --user-ink: #fff; --ai-bubble: #24262d;
  --danger: #ff6b6b; --ok: #4cc27a; --warn: #f0a93a; --off: #6e6d76;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.25);
  color-scheme: dark;
}
:root[data-size="large"] { --fs: 19px; }
:root[data-size="xlarge"] { --fs: 22px; }

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 400 var(--fs)/1.6 "Pretendard", "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", system-ui, sans-serif;
  -webkit-text-size-adjust: 100%;
  word-break: keep-all;
  overflow-wrap: anywhere;
}
a { color: inherit; text-decoration: none; }
button, input, textarea, select { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; line-height: 1.3; }
.muted { color: var(--muted); }
:focus-visible { outline: 3px solid color-mix(in srgb, var(--primary) 60%, transparent); outline-offset: 2px; }

/* buttons ------------------------------------------------------------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4em;
  min-height: 44px; padding: 0 1em; border-radius: 12px;
  border: 1px solid var(--line); background: var(--panel); cursor: pointer;
  font-weight: 600; font-size: .94em; white-space: nowrap;
  transition: background .15s, border-color .15s, transform .05s;
}
.btn:hover { border-color: color-mix(in srgb, var(--primary) 40%, var(--line)); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--danger); }
.btn.danger.solid { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--primary-soft); }
.btn.big { min-height: 52px; font-size: 1em; width: 100%; }
.btn.small { min-height: 34px; padding: 0 .7em; font-size: .82em; border-radius: 10px; }
.icon-btn {
  width: 44px; height: 44px; border-radius: 12px; border: 0; background: transparent;
  cursor: pointer; font-size: 1.15em; display: inline-grid; place-items: center; flex: none;
}
.icon-btn:hover { background: var(--primary-soft); }

/* boot / login -------------------------------------------------------- */
.boot { position: fixed; inset: 0; display: grid; place-items: center; }
.boot img { opacity: .5; animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .15; } }

.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
  width: min(400px, 100%); background: var(--panel); border-radius: 24px; box-shadow: var(--shadow);
  padding: 36px 28px 28px; display: flex; flex-direction: column; gap: 14px; text-align: center;
}
.login-card img { align-self: center; }
.login-card h1 { font-size: 1.6em; }
.login-card .muted { margin: -6px 0 6px; }
.field { display: flex; flex-direction: column; gap: 6px; text-align: left; font-weight: 600; font-size: .92em; }
.field small { font-weight: 400; color: var(--muted); }
.field input, .field textarea, .field select {
  width: 100%; min-height: 48px; padding: 10px 14px; border-radius: 12px;
  border: 1.5px solid var(--line); background: var(--bg); font-weight: 400; font-size: 1.05em;
}
.field textarea { resize: vertical; line-height: 1.55; }
.field input:focus, .field textarea:focus { border-color: var(--primary); outline: none; }
.form-error { color: var(--danger); margin: 0; min-height: 1.4em; font-size: .9em; }

/* app layout ---------------------------------------------------------- */
.app { display: flex; height: 100dvh; }
.sidebar {
  width: 290px; flex: none; background: var(--sidebar); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; padding: 12px 10px calc(10px + env(safe-area-inset-bottom));
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 4px 6px 12px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 1.1em; }
.sidebar-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; padding: 0 4px; }
.list-label { margin: 18px 10px 6px; font-size: .78em; font-weight: 700; color: var(--muted); letter-spacing: .02em; }
.conv-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; padding: 0 2px; }
.conv-item {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 12px; min-height: 48px;
}
.conv-item:hover { background: color-mix(in srgb, var(--panel) 70%, transparent); }
.conv-item.active { background: var(--panel); box-shadow: var(--shadow); }
.conv-item .conv-text { min-width: 0; flex: 1; display: flex; flex-direction: column; }
.conv-item .conv-title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: .93em; font-weight: 600; }
.conv-item .conv-time { font-size: .75em; color: var(--muted); }
.conv-empty { padding: 12px 10px; color: var(--muted); font-size: .9em; }

.avatar {
  --c: var(--primary);
  width: 36px; height: 36px; flex: none; border-radius: 50%; display: grid; place-items: center;
  background: color-mix(in srgb, var(--c) 20%, var(--panel)); font-size: 1.15em; line-height: 1;
  border: 2px solid color-mix(in srgb, var(--c) 45%, transparent);
}
.avatar.lg { width: 64px; height: 64px; font-size: 2em; border-width: 3px; }
.avatar.xl { width: 84px; height: 84px; font-size: 2.6em; border-width: 3px; }

.me-link { display: flex; align-items: center; gap: 10px; padding: 10px; border-radius: 14px; margin-top: 8px; border-top: 1px solid var(--line); }
.me-link:hover { background: color-mix(in srgb, var(--panel) 70%, transparent); }
.me-avatar { width: 36px; height: 36px; border-radius: 50%; background: var(--primary); color: var(--primary-ink); display: grid; place-items: center; font-weight: 800; flex: none; }
.me-text { flex: 1; display: flex; flex-direction: column; line-height: 1.25; min-width: 0; }
.me-text small { color: var(--muted); font-size: .78em; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  height: 60px; flex: none; display: flex; align-items: center; gap: 8px; padding: 0 12px 0 16px;
  border-bottom: 1px solid var(--line); background: var(--bg);
}
.top-title { flex: 1; min-width: 0; font-size: 1.02em; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: flex; align-items: center; gap: 10px; }
.top-title .avatar { width: 32px; height: 32px; font-size: 1em; }
.top-actions { display: flex; gap: 4px; }
.ai-status {
  display: flex; align-items: center; gap: 7px; padding: 6px 12px; border-radius: 999px;
  background: var(--panel); border: 1px solid var(--line); font-size: .8em; font-weight: 600; white-space: nowrap;
}
.ai-status .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--off); }
.ai-status[data-state="online"] .dot { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 25%, transparent); }
.ai-status[data-state="busy"] .dot { background: var(--warn); }
.ai-status[data-state="checking"] .dot { animation: pulse 1s infinite; }

.view { flex: 1; min-height: 0; overflow-y: auto; }
.page { max-width: 860px; margin: 0 auto; padding: 28px 20px 48px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head h1 { font-size: 1.5em; }
.page-head p { margin: 4px 0 0; color: var(--muted); }
.section-title { font-size: 1em; font-weight: 700; margin: 32px 0 12px; }

/* home ---------------------------------------------------------------- */
.hello h1 { font-size: 1.7em; }
.hello p { margin: 6px 0 24px; color: var(--muted); }
.mode-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 14px; }
.mode-card {
  text-align: left; border: 0; cursor: pointer; padding: 22px; border-radius: 20px; background: var(--panel);
  box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 8px; min-height: 160px;
  border: 2px solid transparent; transition: border-color .15s, transform .1s;
}
.mode-card:hover { border-color: var(--primary); transform: translateY(-2px); }
.mode-card .emoji { font-size: 2.2em; line-height: 1; }
.mode-card strong { font-size: 1.15em; }
.mode-card span { color: var(--muted); font-size: .92em; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1px solid var(--line); background: var(--panel); border-radius: 999px; padding: 8px 14px;
  cursor: pointer; font-size: .9em;
}
.chip:hover { border-color: var(--primary); background: var(--primary-soft); }
.recent { display: flex; flex-direction: column; gap: 8px; }
.recent .conv-item { background: var(--panel); box-shadow: var(--shadow); }

/* chat ---------------------------------------------------------------- */
.chat { display: flex; flex-direction: column; height: 100%; }
.messages { flex: 1; overflow-y: auto; padding: 24px 16px 12px; }
.messages-inner { max-width: 820px; margin: 0 auto; display: flex; flex-direction: column; gap: 18px; }
.chat-intro { text-align: center; padding: 20px 10px 8px; display: flex; flex-direction: column; align-items: center; gap: 8px; }
.chat-intro h3 { font-size: 1.25em; }
.chat-intro p { margin: 0; color: var(--muted); max-width: 520px; }
.msg { display: flex; gap: 10px; align-items: flex-start; }
.msg.user { justify-content: flex-end; }
.msg .bubble-wrap { display: flex; flex-direction: column; gap: 6px; max-width: min(680px, 86%); min-width: 0; }
.msg.user .bubble-wrap { align-items: flex-end; }
.msg .who { font-size: .78em; font-weight: 700; color: var(--muted); margin-left: 4px; }
.bubble {
  padding: 12px 16px; border-radius: 18px; white-space: pre-wrap; line-height: 1.65;
  background: var(--ai-bubble); box-shadow: var(--shadow); border-top-left-radius: 6px;
}
.msg.user .bubble { background: var(--user-bubble); color: var(--user-ink); border-top-left-radius: 18px; border-top-right-radius: 6px; box-shadow: none; }
.bubble em.action { color: var(--muted); font-style: italic; }
.msg.user .bubble em.action { color: inherit; opacity: .85; }
.bubble code { font-family: ui-monospace, Consolas, monospace; font-size: .88em; background: color-mix(in srgb, var(--text) 8%, transparent); padding: 1px 5px; border-radius: 6px; }
.bubble pre { margin: 8px 0; padding: 12px; border-radius: 10px; background: color-mix(in srgb, var(--text) 7%, transparent); overflow-x: auto; white-space: pre; }
.bubble pre code { background: none; padding: 0; }
.bubble.pending::after { content: "▍"; animation: blink 1s steps(2) infinite; color: var(--primary); }
.bubble.thinking { color: var(--muted); }
@keyframes blink { 50% { opacity: 0; } }
.msg-actions { display: flex; gap: 4px; flex-wrap: wrap; }
.msg-actions .btn { color: var(--muted); }
.notice {
  max-width: 820px; margin: 0 auto; width: 100%; background: color-mix(in srgb, var(--warn) 14%, var(--panel));
  border: 1px solid color-mix(in srgb, var(--warn) 40%, transparent); border-radius: 14px; padding: 12px 16px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; font-size: .92em;
}
.composer-wrap { flex: none; padding: 8px 16px calc(14px + env(safe-area-inset-bottom)); }
.composer-wrap .notice { margin-bottom: 8px; }
.composer {
  max-width: 820px; margin: 0 auto; display: flex; align-items: flex-end; gap: 8px;
  background: var(--panel); border: 1.5px solid var(--line); border-radius: 20px; padding: 8px 8px 8px 16px; box-shadow: var(--shadow);
}
.composer:focus-within { border-color: var(--primary); }
.composer textarea {
  flex: 1; border: 0; background: transparent; resize: none; outline: none; min-height: 28px; max-height: 40vh;
  padding: 8px 0; line-height: 1.5; font-size: 1.02em;
}
.composer .btn { min-height: 46px; border-radius: 14px; padding: 0 1.1em; }
.hint { max-width: 820px; margin: 6px auto 0; text-align: center; color: var(--muted); font-size: .75em; }

/* characters ---------------------------------------------------------- */
.char-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 14px; }
.char-card {
  --c: var(--primary);
  position: relative; background: var(--panel); border-radius: 20px; box-shadow: var(--shadow); padding: 20px 16px 16px;
  display: flex; flex-direction: column; align-items: center; text-align: center; gap: 6px; border: 2px solid transparent;
  cursor: pointer; transition: border-color .15s, transform .1s;
}
.char-card:hover { border-color: var(--c); transform: translateY(-2px); }
.char-card strong { font-size: 1.08em; margin-top: 4px; }
.char-card .tagline { color: var(--muted); font-size: .86em; min-height: 2.6em; }
.char-card .badge { font-size: .72em; font-weight: 700; padding: 2px 9px; border-radius: 999px; background: var(--primary-soft); color: var(--primary); }
.char-card .card-actions { display: flex; gap: 6px; margin-top: 6px; width: 100%; }
.char-card .card-actions .btn { flex: 1; }
.char-card.new { border: 2px dashed var(--line); background: transparent; box-shadow: none; justify-content: center; min-height: 220px; color: var(--muted); }
.char-card.new:hover { border-color: var(--primary); color: var(--primary); }
.char-card.new .plus { font-size: 2.4em; line-height: 1; }
.empty { text-align: center; color: var(--muted); padding: 30px 10px; }

.form { display: flex; flex-direction: column; gap: 18px; background: var(--panel); border-radius: 20px; padding: 24px; box-shadow: var(--shadow); }
.form .row { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; }
.picker { display: flex; flex-wrap: wrap; gap: 6px; }
.picker button {
  width: 44px; height: 44px; border-radius: 12px; border: 2px solid var(--line); background: var(--bg);
  font-size: 1.35em; cursor: pointer; display: grid; place-items: center; padding: 0;
}
.picker button[aria-pressed="true"] { border-color: var(--primary); background: var(--primary-soft); }
.picker.colors button { border-radius: 50%; width: 38px; height: 38px; }
.picker.colors button[aria-pressed="true"] { box-shadow: 0 0 0 3px var(--panel), 0 0 0 5px var(--text); }
.toggle { display: flex; align-items: center; gap: 12px; cursor: pointer; font-weight: 600; }
.toggle input { width: 22px; height: 22px; accent-color: var(--primary); }
.toggle small { display: block; font-weight: 400; color: var(--muted); }
.form-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.form-actions .spacer { flex: 1; }
.tip { background: var(--primary-soft); border-radius: 14px; padding: 12px 16px; font-size: .88em; }
.tip strong { display: block; margin-bottom: 2px; }

/* settings ------------------------------------------------------------ */
.card { background: var(--panel); border-radius: 20px; padding: 22px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 14px; }
.card + .card { margin-top: 16px; }
.card h3 { font-size: 1.08em; }
.segmented { display: inline-flex; background: var(--bg); border: 1px solid var(--line); border-radius: 14px; padding: 4px; gap: 4px; flex-wrap: wrap; }
.segmented button { border: 0; background: transparent; border-radius: 10px; padding: 8px 14px; cursor: pointer; font-weight: 600; min-height: 40px; }
.segmented button[aria-pressed="true"] { background: var(--panel); box-shadow: var(--shadow); color: var(--primary); }
.user-row { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.user-row:last-child { border-bottom: 0; }
.user-row .me-avatar { width: 40px; height: 40px; }
.user-row .info { flex: 1; min-width: 120px; display: flex; flex-direction: column; line-height: 1.3; }
.user-row .info small { color: var(--muted); }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; }

/* dialog / toast ------------------------------------------------------ */
.dialog {
  border: 0; border-radius: 22px; padding: 0; width: min(420px, calc(100% - 32px)); background: var(--panel); color: var(--text);
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.dialog::backdrop { background: rgba(15, 15, 20, .45); }
.dialog form { padding: 24px; display: flex; flex-direction: column; gap: 14px; }
.dialog h3 { font-size: 1.15em; }
.dialog p { margin: 0; color: var(--muted); }
.dialog .form-actions { justify-content: flex-end; }
.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 999px; font-size: .9em; font-weight: 600;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; max-width: calc(100% - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* responsive ---------------------------------------------------------- */
.only-mobile { display: none; }
.scrim { display: none; }
@media (max-width: 820px) {
  .only-mobile { display: inline-grid; }
  .sidebar {
    position: fixed; z-index: 30; inset: 0 auto 0 0; width: min(320px, 86vw);
    transform: translateX(-100%); transition: transform .2s ease; box-shadow: var(--shadow);
  }
  .app.nav-open .sidebar { transform: none; }
  .app.nav-open .scrim { display: block; position: fixed; inset: 0; z-index: 20; background: rgba(0,0,0,.35); }
  .topbar { padding: 0 8px; height: 56px; }
  .ai-status .label { display: none; }
  .ai-status { padding: 10px; }
  .page { padding: 20px 16px 40px; }
  .messages { padding: 16px 12px 8px; }
  .composer-wrap { padding: 6px 10px calc(10px + env(safe-area-inset-bottom)); }
  .msg .bubble-wrap { max-width: 92%; }
  .msg.assistant .avatar { display: none; }
  .hint { display: none; }
  .form { padding: 18px; }
}
