:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1f242e;
  --border: #2a313c;
  --text: #e7eaf0;
  --muted: #9aa3b2;
  --accent: #6d8bff;
  --accent-ink: #ffffff;
  --user-bubble: #2a3550;
  --danger: #ff6b6b;
  --radius: 14px;
  --drawer-w: 280px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Apple SD Gothic Neo", sans-serif;
  -webkit-text-size-adjust: 100%;
  overflow: hidden;
}
button { font: inherit; cursor: pointer; }

/* ---------- 레이아웃 ---------- */
#main {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

#topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  padding-top: calc(10px + env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
#topbar .title { flex: 1; font-weight: 600; text-align: center; }
.icon-btn {
  background: transparent; color: var(--text); border: none;
  font-size: 20px; width: 40px; height: 40px; border-radius: 10px;
}
.icon-btn:active { background: var(--panel-2); }

/* ---------- 드로어 ---------- */
#drawer {
  position: fixed; top: 0; left: 0; z-index: 30;
  width: var(--drawer-w); height: 100dvh;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  transform: translateX(-100%);
  transition: transform .22s ease;
  padding-top: env(safe-area-inset-top);
}
#drawer.open { transform: translateX(0); }
.drawer-head { padding: 12px; }
.drawer-foot { padding: 12px; border-top: 1px solid var(--border); }
.conv-list { flex: 1; overflow-y: auto; padding: 0 8px; }
.conv-item {
  display: flex; align-items: center; gap: 6px;
  padding: 10px 10px; margin: 2px 0; border-radius: 10px;
  color: var(--text);
}
.conv-item.active { background: var(--panel-2); }
.conv-title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.conv-del {
  background: transparent; border: none; color: var(--muted);
  width: 26px; height: 26px; border-radius: 8px; font-size: 13px;
}
.conv-del:active { color: var(--danger); background: var(--panel); }

#overlay {
  position: fixed; inset: 0; z-index: 25;
  background: rgba(0,0,0,.45);
}

/* ---------- 메시지 ---------- */
.messages {
  flex: 1; overflow-y: auto;
  padding: 16px 12px 24px;
  display: flex; flex-direction: column; gap: 14px;
  scroll-behavior: smooth;
}
.msg { display: flex; }
.msg-user { justify-content: flex-end; }
.msg-assistant { justify-content: flex-start; }
.msg-body {
  max-width: 86%;
  padding: 10px 14px;
  border-radius: var(--radius);
  word-break: break-word;
  overflow-wrap: anywhere;
}
.msg-user .msg-body {
  background: var(--user-bubble);
  border-bottom-right-radius: 4px;
  white-space: pre-wrap;
}
.msg-assistant .msg-body {
  background: var(--panel);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.msg-assistant.streaming .msg-body::after {
  content: "▍"; color: var(--accent);
  animation: blink 1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

.empty {
  margin: auto; text-align: center; color: var(--muted); padding: 24px;
}
.error { color: var(--danger); font-size: 14px; }

/* 마크다운 */
.msg-body p { margin: .4em 0; }
.msg-body p:first-child { margin-top: 0; }
.msg-body p:last-child { margin-bottom: 0; }
.msg-body pre {
  background: #0c0e13; border: 1px solid var(--border);
  border-radius: 10px; padding: 12px; overflow-x: auto; font-size: 13px;
}
.msg-body code {
  background: #0c0e13; padding: .1em .35em; border-radius: 6px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .9em;
}
.msg-body pre code { background: transparent; padding: 0; }
.msg-body a { color: var(--accent); }
.msg-body ul, .msg-body ol { padding-left: 1.3em; margin: .4em 0; }
.msg-body table { border-collapse: collapse; }
.msg-body th, .msg-body td { border: 1px solid var(--border); padding: 4px 8px; }

/* ---------- 입력창 ---------- */
#composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 10px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--panel);
}
#input {
  flex: 1; resize: none; max-height: 160px;
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 12px;
  padding: 10px 12px; font: inherit; line-height: 1.4;
}
#input:focus { outline: none; border-color: var(--accent); }
.web-toggle {
  background: var(--panel-2); border: 1px solid var(--border);
  color: var(--muted); border-radius: 12px; height: 42px; width: 42px; font-size: 18px;
  flex: none; opacity: .7;
}
.web-toggle.on { color: var(--accent); border-color: var(--accent); opacity: 1; }
.send-btn {
  flex: none; height: 42px; width: 42px; border-radius: 12px;
  background: var(--accent); color: var(--accent-ink); border: none; font-size: 18px;
}
.send-btn:disabled { opacity: .5; }

/* ---------- 버튼 ---------- */
.btn-primary {
  background: var(--accent); color: var(--accent-ink);
  border: none; border-radius: 10px; padding: 10px 14px; width: 100%;
}
.btn-ghost {
  background: transparent; color: var(--text);
  border: 1px solid var(--border); border-radius: 10px; padding: 8px 14px;
}

/* ---------- 설정 모달 ---------- */
.modal {
  position: fixed; inset: 0; z-index: 40;
  display: none; align-items: center; justify-content: center;
  background: rgba(0,0,0,.5); padding: 16px;
}
.modal.open { display: flex; }
.modal-card {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 16px; padding: 20px; width: 100%; max-width: 440px;
  max-height: 88dvh; overflow-y: auto;
}
.modal-card h2 { margin: 0 0 12px; font-size: 18px; }
.modal-card label { display: block; margin: 12px 0 4px; font-size: 14px; color: var(--muted); }
.modal-card label.row { display: flex; align-items: center; gap: 8px; color: var(--text); }
.modal-card input[type=text], .modal-card input:not([type]),
.modal-card input[list], .modal-card input[type=password], .modal-card textarea {
  width: 100%; background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px; padding: 9px 11px; font: inherit;
}
.modal-card textarea { resize: vertical; }
.hint { font-size: 12px; color: var(--muted); margin: 6px 0 0; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; }

/* ---------- 데스크탑: 드로어 상시 표시 ---------- */
@media (min-width: 820px) {
  #drawer { transform: translateX(0); }
  #main { margin-left: var(--drawer-w); }
  #menu { display: none; }
  #overlay { display: none !important; }
  .msg-body { max-width: 70%; }
}
