:root {
  --bg: #262624;
  --surface: #30302e;
  --surface-2: #3a3a37;
  --text: #f0efea;
  --dim: #a8a69e;
  --accent: #d97757;
  --accent-hover: #e08b6f;
  --accent-soft: rgba(217, 119, 87, 0.14);
  --blue: #8ab4ff;
  --red: #ff9d94;
  --border: #423f3c;
  --radius: 16px;
}
* { margin: 0; padding: 0; box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  font-family: "Pretendard", -apple-system, "Segoe UI", "Malgun Gothic", sans-serif;
  background: var(--bg);
  color: var(--text);
  user-select: text;
  -webkit-user-select: text;
}
.app { height: 100dvh; display: flex; }
.main-col { flex: 1; min-width: 0; display: flex; flex-direction: column; height: 100dvh; }

/* ===== 사이드바 (ChatGPT 스타일) ===== */
#sidebar {
  width: 250px; flex-shrink: 0; height: 100dvh;
  background: #1e1e1c; border-right: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 8px;
  padding: 12px 10px;
}
#new-chat-btn {
  padding: 10px 12px; border-radius: 10px; text-align: left;
  background: none; border: 1px solid var(--border);
  color: var(--text); font-size: 0.9rem; font-weight: 600;
  cursor: pointer; font-family: inherit;
}
#new-chat-btn:hover { background: var(--surface); }
.conv-label { font-size: 0.72rem; color: var(--dim); padding: 6px 8px 0; }
#conv-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.conv-item {
  display: flex; align-items: center; gap: 4px;
  padding: 8px 10px; border-radius: 8px; cursor: pointer;
  color: var(--text); font-size: 0.87rem;
}
.conv-item:hover { background: var(--surface); }
.conv-item.active { background: var(--surface-2); }
.conv-title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-del {
  visibility: hidden; flex-shrink: 0;
  background: none; border: none; cursor: pointer;
  color: var(--dim); font-size: 0.85rem; padding: 2px 5px; border-radius: 5px;
}
.conv-item:hover .conv-del { visibility: visible; }
.conv-del:hover { color: var(--red); background: var(--bg); }
.conv-empty { color: var(--dim); font-size: 0.82rem; padding: 8px 10px; }

.menu-btn {
  display: none; background: none; border: none;
  color: var(--text); font-size: 1.25rem; cursor: pointer;
  padding: 4px 10px 4px 2px;
}
#sidebar-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 190; }

@media (max-width: 900px) {
  #sidebar {
    position: fixed; left: 0; top: 0; z-index: 200;
    transform: translateX(-100%); transition: transform 0.25s ease;
    box-shadow: 4px 0 24px rgba(0,0,0,0.4);
  }
  #sidebar.open { transform: translateX(0); }
  .menu-btn { display: block; }
  /* 터치 기기는 호버가 없으므로 삭제 버튼 항상 표시 */
  .conv-del { visibility: visible; }
}

/* ===== 로그인 / 회원가입 ===== */
.auth-wrap {
  min-height: 100dvh; display: flex; align-items: center; justify-content: center;
  padding: 24px 16px;
}
.auth-card {
  width: 100%; max-width: 400px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 20px; padding: 36px 30px;
  display: flex; flex-direction: column; align-items: center;
}
.auth-logo {
  width: 56px; height: 56px; border-radius: 16px;
  background: var(--accent); display: grid; place-items: center;
  font-size: 1.8rem; margin-bottom: 14px;
}
.auth-title { font-size: 1.25rem; font-weight: 700; }
.auth-sub { font-size: 0.82rem; color: var(--dim); margin: 4px 0 24px; }
.auth-form { width: 100%; display: flex; flex-direction: column; gap: 10px; }
.auth-form input[type="text"], .auth-form input[type="password"],
.auth-form input:not([type]) {
  width: 100%; padding: 12px 14px; font-size: 0.95rem;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px; outline: none;
}
.auth-form input:focus { border-color: var(--accent); }
.auth-sec {
  font-size: 0.8rem; font-weight: 700; color: var(--dim);
  margin-top: 10px; padding-bottom: 2px;
  border-bottom: 1px dashed var(--border);
}
.remember {
  display: flex; align-items: center; gap: 8px;
  font-size: 0.87rem; color: var(--dim); cursor: pointer; user-select: none;
}
.remember input { accent-color: var(--accent); width: 15px; height: 15px; }
.auth-btn {
  padding: 12px; border: none; border-radius: 10px;
  background: var(--accent); color: #fff;
  font-size: 0.95rem; font-weight: 700; cursor: pointer;
}
.auth-btn:hover { background: var(--accent-hover); }
.auth-btn.ghost {
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); font-weight: 600;
}
.auth-btn.ghost:hover { border-color: var(--accent); background: var(--surface-2); }
.auth-hint {
  font-size: 0.78rem; color: var(--dim); text-decoration: none;
  align-self: flex-end; margin-top: -4px;
}
.auth-hint:hover { color: var(--accent); }
.auth-msg { min-height: 1.2em; font-size: 0.85rem; color: var(--red); text-align: center; }
.auth-switch { font-size: 0.85rem; color: var(--dim); text-align: center; }
.auth-switch a { color: var(--accent); text-decoration: none; font-weight: 600; }

/* 헤더 로그아웃 버튼 (kb-link 스타일 재사용) */
button.kb-link { cursor: pointer; font-family: inherit; }

/* ===== 알림 팝업 ===== */
.modal-overlay {
  position: fixed; inset: 0; z-index: 1000;
  background: rgba(0, 0, 0, 0.55);
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
}
.modal-box {
  width: 100%; max-width: 340px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 18px; padding: 30px 26px;
  display: flex; flex-direction: column; align-items: center; gap: 14px;
  text-align: center;
}
.modal-icon { font-size: 2.2rem; }
.modal-box p { line-height: 1.55; font-size: 0.95rem; }
.modal-actions { display: flex; gap: 8px; width: 100%; }
.modal-actions .auth-btn { flex: 1; }

/* ===== 스크롤바 ===== */
::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #4a4845; border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #5a5855; }

/* ===== 헤더 ===== */
header {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 20px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.logo {
  width: 40px; height: 40px; border-radius: 12px;
  background: var(--accent);
  display: grid; place-items: center; font-size: 1.25rem;
  flex-shrink: 0;
}
header h1 { font-size: 1.02rem; font-weight: 700; letter-spacing: -0.01em; }
header p { font-size: 0.76rem; color: var(--dim); margin-top: 1px; }
.header-spacer { flex: 1; }
.kb-link {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 8px 14px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--border);
  color: var(--text); text-decoration: none;
  font-size: 0.85rem; font-weight: 600; white-space: nowrap;
  transition: border-color 0.15s, background 0.15s;
}
.kb-link:hover { border-color: var(--accent); background: var(--surface-2); }
.kb-link .kb-icon { font-size: 0.95rem; }
@media (max-width: 560px) { .kb-link .kb-text { display: none; } .kb-link { padding: 8px 11px; } }

/* ===== 채팅 영역 ===== */
main {
  flex: 1; overflow-y: auto;
  padding: 18px max(16px, calc((100% - 820px) / 2));
  display: flex; flex-direction: column; gap: 12px;
}
.msg { display: flex; }
.msg.user { justify-content: flex-end; }
.bubble {
  padding: 10px 14px;
  border-radius: var(--radius); line-height: 1.55; font-size: 0.95rem;
  white-space: pre-wrap;
  word-break: keep-all; overflow-wrap: break-word;
  user-select: text; -webkit-user-select: text; cursor: text;
}
.msg.agent .bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  border-top-left-radius: 6px;
  max-width: 100%;
  position: relative;
  padding-right: 44px;
}
.msg.user .bubble {
  background: var(--surface-2);
  color: var(--text);
  border-top-right-radius: 6px;
  max-width: 82%;
}
.msg .col { display: flex; flex-direction: column; max-width: 88%; }
.msg .col .bubble { max-width: 100%; }

/* ===== 마크다운 답변 ===== */
.bubble.md { white-space: normal; }
.bubble.md p { margin: 0 0 6px; }
.bubble.md p:last-child { margin-bottom: 0; }
.bubble.md h1, .bubble.md h2, .bubble.md h3 {
  font-size: 1rem; margin: 12px 0 5px; line-height: 1.4;
  color: var(--blue); font-weight: 700;
}
.bubble.md h1:first-child, .bubble.md h2:first-child, .bubble.md h3:first-child { margin-top: 0; }
.bubble.md strong { color: var(--red); font-weight: 700; }
.bubble.md h1 strong, .bubble.md h2 strong, .bubble.md h3 strong { color: inherit; }
.bubble.md ul, .bubble.md ol { margin: 2px 0 6px 20px; }
.bubble.md li { margin: 2px 0; }
.bubble.md li > ul, .bubble.md li > ol { margin-bottom: 2px; }
.bubble.md a { color: var(--accent); word-break: break-all; }
.bubble.md a:hover { color: var(--accent-hover); }
.bubble.md code {
  background: #1e1e1c; padding: 2px 6px;
  border-radius: 5px; font-size: 0.85em; color: #e8d8b0;
}
.bubble.md pre {
  background: #1e1e1c; padding: 12px 14px; border-radius: 10px;
  overflow-x: auto; margin: 8px 0; border: 1px solid var(--border);
}
.bubble.md pre code { background: none; padding: 0; }
.bubble.md blockquote {
  border-left: 3px solid var(--accent); padding-left: 12px;
  color: var(--dim); margin: 8px 0;
}
.bubble.md table {
  border-collapse: collapse; margin: 10px 0;
  display: block; overflow-x: auto; max-width: 100%;
}
.bubble.md th, .bubble.md td {
  border: 1px solid var(--border); padding: 7px 12px; font-size: 0.88rem;
}
.bubble.md th { background: var(--surface-2); }
.bubble.md hr { border: none; border-top: 1px solid var(--border); margin: 12px 0; }

/* 검색 상태 표시 (경과 시간 초 단위 표시) */
.bubble.searching { color: var(--dim); font-style: italic; }

/* ===== 출처 표시 ===== */
.sources {
  margin-top: 8px; padding-top: 7px;
  border-top: 1px dashed var(--border); font-size: 0.85rem;
}
.sources .label { font-weight: 700; margin-bottom: 4px; }
.sources.personal .label { color: var(--blue); }
.sources.external .label { color: #6fd3c7; }
.sources a {
  display: block; color: var(--dim); text-decoration: none;
  padding: 2px 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.sources a:hover { color: var(--accent); text-decoration: underline; }

/* ===== 복사 버튼 (답변 말풍선 오른쪽 상단) ===== */
.copy-btn {
  position: absolute; top: 8px; right: 8px;
  width: 28px; height: 28px;
  display: grid; place-items: center;
  background: none; border: none; cursor: pointer;
  color: var(--dim); border-radius: 7px;
  line-height: 1;
}
.copy-btn:hover { color: var(--text); background: var(--surface-2); }

/* ===== 외부 검색 확인 버튼 ===== */
.actions { display: flex; flex-direction: column; gap: 8px; margin-top: 14px; }
.actions button {
  padding: 12px 18px; border-radius: 12px; font-size: 0.93rem;
  font-weight: 600; cursor: pointer; border: none; width: 100%;
  transition: filter 0.15s;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-secondary {
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border);
}
.btn-secondary:hover { border-color: var(--dim); }
.actions button:disabled { opacity: 0.45; cursor: default; }
@media (min-width: 600px) {
  .actions { flex-direction: row; }
  .actions button { width: auto; }
}

/* ===== 입력창 ===== */
footer {
  padding: 14px 20px calc(16px + env(safe-area-inset-bottom));
  background: var(--bg);
  position: relative;
}

/* ===== 명령 팔레트 (/ 입력 시 입력창 위 팝업) ===== */
#cmd-palette {
  position: absolute;
  bottom: calc(100% - 6px);
  left: max(20px, calc((100% - 820px) / 2));
  right: max(20px, calc((100% - 820px) / 2));
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 6px;
  box-shadow: 0 -8px 28px rgba(0, 0, 0, 0.45);
  z-index: 100;
  max-height: 40dvh;
  overflow-y: auto;
}
.cmd-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: 9px; cursor: pointer;
  font-size: 0.92rem;
}
.cmd-item.sel, .cmd-item:hover { background: var(--surface-2); }
.cmd-item .cmd-icon { flex-shrink: 0; }
.cmd-item .cmd-name { font-weight: 700; color: var(--accent); white-space: nowrap; }
.cmd-item .cmd-desc {
  color: var(--dim); overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap;
}
#chat-form {
  max-width: 820px; margin: 0 auto;
  display: flex; gap: 8px; align-items: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 6px 6px 6px 10px;
  transition: border-color 0.15s;
}
#attach-btn {
  width: 36px; height: 36px; flex-shrink: 0;
  border: none; border-radius: 50%;
  background: none; color: var(--dim);
  font-size: 1.05rem; cursor: pointer;
  display: grid; place-items: center;
  transition: background 0.15s, color 0.15s;
}
#attach-btn:hover { background: var(--border); color: var(--text); }

/* ===== 이미지 첨부 썸네일 스트립 (이미지입력 계획서 §2 — 최대 5장) ===== */
#attach-strip {
  max-width: 820px; margin: 0 auto 10px;
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
}
.attach-tile {
  position: relative; width: 56px; height: 56px;
}
.attach-tile img {
  width: 100%; height: 100%; object-fit: cover;
  border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface);
}
.attach-del {
  position: absolute; top: -7px; right: -7px;
  width: 20px; height: 20px;
  border: 1px solid var(--border); border-radius: 50%;
  background: var(--surface); color: var(--text);
  font-size: 0.8rem; line-height: 1; cursor: pointer;
  display: grid; place-items: center; padding: 0;
}
.attach-del:hover { background: #5a5855; }

/* 사용자 말풍선 속 첨부 썸네일 (여러 장 가로 나열) */
.bubble .user-imgs {
  display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 6px;
}
.bubble .user-img {
  display: block; max-width: 160px; max-height: 160px;
  border-radius: 10px;
}
.bubble .user-imgs .user-img:only-child { max-width: 240px; max-height: 200px; }
#chat-form:focus-within { border-color: #5a5855; }
#chat-input {
  flex: 1; padding: 10px 0; font-size: 1rem;
  border: none; background: none; outline: none;
  color: var(--text);
}
#chat-input::placeholder { color: var(--dim); }
#send-btn {
  width: 40px; height: 40px; flex-shrink: 0;
  border: none; border-radius: 50%;
  background: var(--accent); color: #fff;
  font-size: 1.05rem; cursor: pointer;
  display: grid; place-items: center;
  transition: background 0.15s;
}
#send-btn:hover { background: var(--accent-hover); }
#send-btn:disabled { opacity: 0.45; cursor: default; }
