:root {
  --bg: #f6f4ef;
  --panel: #ffffff;
  --sidebar: #efebe2;
  --text: #24211b;
  --muted: #7a7366;
  --border: #e2ddd1;
  --accent: #e8a317;
  --accent-ink: #3a2a00;
  --user-bubble: #f3e3bd;
  --code-bg: #f4f1ea;
  --danger: #c0392b;
  --warn: #b7791f;
  --radius: 14px;
  --sidebar-w: 270px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17150f;
    --panel: #1f1c15;
    --sidebar: #12100c;
    --text: #ece7dc;
    --muted: #9b9384;
    --border: #2f2b22;
    --accent: #f0b429;
    --accent-ink: #2a1e00;
    --user-bubble: #3a3020;
    --code-bg: #0f0d09;
    --danger: #ef6f62;
    --warn: #e0a84a;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", "Noto Sans KR", "Segoe UI", sans-serif;
  font-size: 16px;
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
}

button { font: inherit; color: inherit; cursor: pointer; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.error { color: var(--danger); }

/* ---------- 로그인 ---------- */

.login {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}
.login-card {
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: center;
}
.login-logo { width: 64px; height: 64px; margin: 0 auto; }
.login-card h1 { margin: 0 0 8px; font-size: 22px; }
.login-card input {
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  font-size: 16px;
}
.login-card button {
  padding: 12px;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}
.login-card button:disabled { opacity: .6; }

/* ---------- 레이아웃 ---------- */

.layout {
  display: flex;
  height: 100dvh;
  overflow: hidden;
}

.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}
.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 12px 8px 16px;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; }
.brand img { width: 24px; height: 24px; }

.conv-list { flex: 1; overflow-y: auto; padding: 4px 8px; }
.conv-item {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 10px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 14px;
}
.conv-item:hover { background: color-mix(in srgb, var(--text) 6%, transparent); }
.conv-item.active { background: color-mix(in srgb, var(--accent) 22%, transparent); }
.conv-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-actions { display: none; }
.conv-item:hover .conv-actions, .conv-item.active .conv-actions { display: flex; }
.empty { color: var(--muted); font-size: 14px; padding: 8px 10px; }

.sidebar-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 16px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--muted);
}
.quota.allowed_warning { color: var(--warn); }
.quota.rejected { color: var(--danger); }
.link-btn { background: none; border: 0; padding: 0; color: var(--muted); font-size: 12px; text-decoration: underline; }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 10px;
  background: transparent;
}
.icon-btn:hover { background: color-mix(in srgb, var(--text) 8%, transparent); }
.icon-btn.small { width: 28px; height: 28px; }
.icon-btn.small svg { width: 16px; height: 16px; }

.scrim { display: none; }

/* ---------- 채팅 ---------- */

.chat {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}
.chat-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
  border-bottom: 1px solid var(--border);
}
.chat-head h2 {
  flex: 1;
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.menu-btn { visibility: hidden; }

.messages { flex: 1; overflow-y: auto; overscroll-behavior: contain; }
.messages-inner {
  max-width: 780px;
  margin: 0 auto;
  padding: 20px 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.welcome { text-align: center; color: var(--muted); margin-top: 18vh; }
.welcome img { width: 56px; height: 56px; opacity: .9; }

.msg { display: flex; flex-direction: column; min-width: 0; }
.msg.user { align-items: flex-end; }
.msg.user .bubble {
  max-width: 85%;
  background: var(--user-bubble);
  padding: 10px 14px;
  border-radius: var(--radius) var(--radius) 4px var(--radius);
  white-space: normal;
  overflow-wrap: anywhere;
}
.msg.assistant .bubble { max-width: 100%; overflow-wrap: anywhere; }
.msg-meta { font-size: 12px; color: var(--muted); margin-top: 4px; display: flex; gap: 6px; align-items: center; min-height: 18px; }

.typing { display: inline-flex; gap: 3px; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite ease-in-out; }
.typing i:nth-child(2) { animation-delay: .2s; }
.typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* 마크다운 */
.markdown > :first-child { margin-top: 0; }
.markdown > :last-child { margin-bottom: 0; }
.markdown p { margin: 0 0 .7em; }
.markdown ul, .markdown ol { margin: 0 0 .7em; padding-left: 1.4em; }
.markdown h1, .markdown h2, .markdown h3 { font-size: 1.05em; margin: 1em 0 .5em; }
.markdown a { color: var(--accent); }
.markdown blockquote { margin: 0 0 .7em; padding-left: 12px; border-left: 3px solid var(--border); color: var(--muted); }
.markdown code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .88em; background: var(--code-bg); padding: .1em .35em; border-radius: 5px; }
.markdown pre { margin: 0 0 .8em; border-radius: 10px; overflow-x: auto; border: 1px solid var(--border); }
.markdown pre code.hljs { display: block; padding: 12px 14px; background: var(--code-bg); font-size: 13px; line-height: 1.5; }
.markdown table { border-collapse: collapse; margin: 0 0 .8em; display: block; overflow-x: auto; font-size: 14px; }
.markdown th, .markdown td { border: 1px solid var(--border); padding: 5px 9px; text-align: left; }

/* 입력창 */
.composer {
  padding: 8px 12px calc(10px + env(safe-area-inset-bottom));
}
.composer-box {
  max-width: 780px;
  margin: 0 auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 10px 10px 8px 14px;
  box-shadow: 0 1px 6px rgb(0 0 0 / .05);
}
.composer textarea {
  width: 100%;
  border: 0;
  outline: 0;
  resize: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 16px; /* iOS 자동 확대 방지 */
  max-height: 200px;
}
.composer-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.deep-toggle {
  font-size: 13px;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 10px;
  cursor: pointer;
  user-select: none;
}
.deep-toggle input { display: none; }
.deep-toggle.on { color: var(--accent-ink); background: var(--accent); border-color: var(--accent); }
.send-btn {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-ink);
}
.send-btn:disabled { opacity: .35; cursor: default; }
.send-btn.stop { background: var(--text); color: var(--bg); }
.send-btn.stop svg { fill: currentColor; stroke: none; }

/* ---------- 모바일: 사이드바를 드로어로 ---------- */

@media (max-width: 860px) {
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 20;
    width: min(var(--sidebar-w), 82vw);
    transform: translateX(-100%);
    transition: transform .2s ease;
  }
  .drawer-open .sidebar { transform: none; }
  .drawer-open .scrim { display: block; position: fixed; inset: 0; z-index: 10; background: rgb(0 0 0 / .35); }
  .menu-btn { visibility: visible; }
  .conv-actions { display: flex; }
}
