:root {
  /* 색 — 랜딩과 통일한 보라 포인트 (밝은 배경은 유지해 가독성 보존) */
  --blue: #9047ff;        /* 포인트(보라) */
  --blue-dark: #7a2fe8;   /* 진한 보라 (호버 등) */
  --blue-soft: #f1ebff;   /* 아주 연한 보라 배경 */
  --blue-line: #e2d6fb;   /* 연한 보라 테두리 */
  --ink: #1e1630;         /* 본문 글자(보라빛 먹색) */
  --muted: #7b7490;       /* 보조 글자 */
  --bg: #faf8ff;          /* 전체 배경(아주 옅은 보라빛 흰색) */
  --surface: #ffffff;
  --danger: #d64545;

  /* 간격 (4px 배수 스케일) */
  --sp-1: 4px;
  --sp-2: 8px;
  --sp-3: 12px;
  --sp-4: 16px;
  --sp-5: 20px;
  --sp-6: 24px;

  /* 모서리 반경 */
  --r-sm: 8px;
  --r-md: 12px;
  --r-lg: 18px;
  --r-pill: 999px;

  /* 그림자 */
  --shadow-sm: 0 1px 3px rgba(27, 42, 51, 0.06);
  --shadow-md: 0 2px 14px rgba(144, 71, 255, 0.08);
  --shadow-lg: 0 12px 40px rgba(144, 71, 255, 0.12);

  /* 글자 크기 */
  --fs-sm: 13px;
  --fs-base: 15px;
  --fs-lg: 18px;

  /* 전환 */
  --t-fast: 0.15s ease;
}

* { box-sizing: border-box; }

/* hidden 속성은 항상 숨김 (display 지정이 있어도) */
[hidden] { display: none !important; }

body {
  margin: 0;
  height: 100vh;
  height: 100dvh;
  font-family: system-ui, -apple-system, "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
  background: transparent;
  color: var(--ink);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  letter-spacing: -0.01em;
}
html { background: #05060f; }

/* 호수 달빛 셰이더 배경: 화면 맨 뒤에 고정 (완전한 밤 풍경이라 불투명) */
#chat-bg {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: -1;
  pointer-events: none;
}
/* 채팅 영역은 배경(별)이 비치도록 투명. .app 에는 z-index 를 주지 않아
   사이드바(z-50)와 뒤덮개(z-40)의 쌓임 순서가 정상 작동하게 한다. */
.chat { background: transparent; }
/* 호수가 어두우니 채팅 영역의 흰 말풍선 위 글자는 그대로 두되,
   AI 말풍선은 반투명 흰 배경으로 호수가 살짝 비치게 (가독성 유지) */
.msg.ai .bubble {
  background: rgba(255, 255, 255, 0.92) !important;
  backdrop-filter: blur(4px);
}

/* 부드러운 스크롤바 (웹킷) */
*::-webkit-scrollbar { width: 10px; height: 10px; }
*::-webkit-scrollbar-thumb { background: var(--blue-line); border-radius: var(--r-pill); border: 2px solid transparent; background-clip: padding-box; }
*::-webkit-scrollbar-thumb:hover { background: #c9b3f5; background-clip: padding-box; }
*::-webkit-scrollbar-track { background: transparent; }

/* 전체 틀: 왼쪽 사이드바 + 오른쪽 메인 */
.app {
  display: flex;
  height: 100%;
}

/* ===== 사이드바 ===== */
.sidebar {
  width: 270px;
  flex-shrink: 0;
  background: #f3eefb;
  border-right: 1px solid var(--blue-line);
  display: flex;
  flex-direction: column;
  padding: 12px;
  gap: 10px;
  overflow-y: auto;
  transition: transform 0.25s ease;
}
.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
  color: var(--blue);
  font-weight: 800;
  font-size: 16px;
}
.new-chat {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 11px 14px;
  border: 1px solid var(--blue-line);
  border-radius: 12px;
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}
.new-chat:hover { background: var(--blue-soft); }

/* 로그인이 필요하다는 안내 (사이드바) */
.login-required {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  text-align: left;
  padding: 14px;
  border: 1px dashed var(--blue);
  border-radius: 12px;
  background: var(--blue-soft);
  color: var(--blue);
  font: inherit;
  cursor: pointer;
  line-height: 1.4;
  transition: background 0.15s;
}
.login-required:hover { background: #ebe1fc; }
.login-required svg { flex-shrink: 0; }
.login-required span { font-size: 12.5px; color: var(--ink); }
.login-required strong { color: var(--blue); font-size: 13.5px; }

.side-menu { display: flex; flex-direction: column; gap: 2px; }
.side-menu-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border: 0;
  border-radius: 10px;
  background: none;
  color: var(--ink);
  font: inherit;
  font-size: 14px;
  text-align: left;
  cursor: pointer;
  transition: background 0.15s;
}
.side-menu-item:hover { background: var(--blue-soft); }
.side-menu-item.active { background: var(--blue-soft); color: var(--blue); font-weight: 600; }

.side-section-label {
  font-size: 12px;
  color: var(--muted);
  font-weight: 700;
  padding: 8px 12px 2px;
  letter-spacing: 0.3px;
}

.conv-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; overflow-y: auto; }
.conv-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 9px 12px;
  border-radius: 10px;
  cursor: pointer;
  color: var(--ink);
  font-size: 14px;
  transition: background 0.15s;
}
.conv-item:hover { background: var(--blue-soft); }
.conv-item.active { background: #e7dbfc; font-weight: 600; box-shadow: inset 3px 0 0 var(--blue); }
.conv-item .conv-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-item .conv-del {
  flex-shrink: 0;
  border: 0;
  background: none;
  color: var(--muted);
  cursor: pointer;
  padding: 2px;
  opacity: 0;
  display: inline-flex;
}
.conv-item:hover .conv-del { opacity: 1; }
.conv-item .conv-del:hover { color: #d64545; }
.conv-empty { color: var(--muted); font-size: 13px; padding: 8px 12px; }

/* 폰에서 사이드바 뒤를 덮는 막 */
.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 40, 60, 0.4);
  z-index: 40;
}

/* ===== 오른쪽 메인 ===== */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100%;
}

/* 아이콘 버튼(햄버거 등) */
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  background: none;
  color: var(--ink);
  cursor: pointer;
  padding: 6px;
  border-radius: 10px;
}
.icon-btn:hover { background: var(--blue-soft); color: var(--blue); }
/* 햄버거/닫기 버튼은 데스크톱에선 숨기고 폰에서만 보인다 */
#hamburger, .only-mobile { display: none; }

/* 상단 로딩 진행 바 */
.loading-bar {
  position: fixed;
  top: 0; left: 0;
  height: 3px;
  width: 100%;
  z-index: 100;
  background: linear-gradient(90deg, transparent, var(--blue), transparent);
  background-size: 40% 100%;
  background-repeat: no-repeat;
  animation: loading-slide 1.1s infinite ease-in-out;
}
@keyframes loading-slide {
  0% { background-position: -40% 0; }
  100% { background-position: 140% 0; }
}

/* 'AI가 답 쓰는 중' 타이핑 표시 (점 세 개가 순서대로 깜빡임) */
.bubble.typing {
  display: inline-flex;
  gap: 5px;
  align-items: center;
  padding: 14px 16px;
}
.bubble.typing .dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: currentColor;
  opacity: 0.4;
  animation: typing-bounce 1.2s infinite ease-in-out;
}
.bubble.typing .dot:nth-child(2) { animation-delay: 0.18s; }
.bubble.typing .dot:nth-child(3) { animation-delay: 0.36s; }
@keyframes typing-bounce {
  0%, 60%, 100% { opacity: 0.35; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-4px); }
}

/* 헤더 */
.top {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  background: #fff;
  border-bottom: 1px solid var(--blue-line);
  flex-shrink: 0;
}
.chat-title {
  flex: 1;
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  color: var(--ink);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.top-buttons { display: flex; gap: 6px; }
.top-buttons button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 10px;
  padding: 8px 10px;
  background: var(--blue-soft);
  color: var(--blue);
  font: inherit;
  cursor: pointer;
  transition: background 0.15s;
}
.top-buttons button:hover { background: #ebe1fc; }

/* 대화 영역 */
.chat {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: 100%;
  max-width: 820px;
  margin: 0 auto;
}

/* 환영 화면 — 어두운 별밤 배경 위에서 글자만 밝게 (배경판 없음) */
.welcome {
  margin: auto;
  text-align: center;
  padding: 24px;
  color: #eceaf4;
  max-width: 560px;
}
.welcome-logo { font-size: 54px; color: #d9c4ff; }
.welcome h2 { margin: 14px 0 8px; color: #ffffff; font-weight: 800; }
.welcome p { margin: 0 0 22px; font-size: 14px; line-height: 1.6; color: #c9c2dc; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.chip {
  border: 1px solid var(--blue-line);
  background: #fff;
  color: var(--blue);
  border-radius: 20px;
  padding: 9px 15px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.15s;
}
.chip:hover { background: var(--blue); color: #fff; border-color: var(--blue); }

/* 말풍선 */
.msg { display: flex; flex-direction: column; max-width: 88%; gap: 4px; animation: pop 0.22s ease; }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.ai { align-self: flex-start; align-items: flex-start; }
@keyframes pop { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.bubble {
  padding: 12px 16px;
  border-radius: 18px;
  white-space: pre-wrap;
  /* 한국어는 단어를 통째로 유지하고(어절 단위로만 줄바꿈), 아주 긴 단어/URL만 끊는다.
     → 글자 하나하나에서 끊기던 들쭉날쭉한 줄바꿈을 막아 가독성을 높인다. */
  word-break: keep-all;
  overflow-wrap: break-word;
  line-height: 1.7;
  font-size: 15px;
  /* 폰에서 글자를 꾹 누르면 '복사하기'가 뜨도록 텍스트 선택을 허용 */
  -webkit-user-select: text;
  user-select: text;
  -webkit-touch-callout: default;
}
.msg.user .bubble {
  background: var(--blue);
  color: #fff;
  border-bottom-right-radius: 5px;
}
.msg.ai .bubble {
  background: #fff;
  color: var(--ink);
  border: 1px solid var(--blue-line);
  border-bottom-left-radius: 5px;
  box-shadow: var(--shadow-sm);
}
.bubble a { color: var(--blue); word-break: break-all; }
.msg.user .bubble a { color: #fff; text-decoration: underline; }

/* 첫 응답 로딩 안내 말풍선 (은은하게) */
.msg.hint-msg .bubble { background: var(--blue-soft); border-color: var(--blue-line); color: var(--muted); font-size: 14px; }

/* ===== 마크다운 렌더링 ===== */
.md-p { margin: 0 0 8px; }
.md-p:last-child { margin-bottom: 0; }
.md-h { margin: 10px 0 6px; font-weight: 700; line-height: 1.3; }
h3.md-h { font-size: 18px; }
h4.md-h { font-size: 16px; }
h5.md-h { font-size: 15px; }
.md-list { margin: 4px 0 8px; padding-left: 22px; }
.md-list li { margin: 2px 0; }

/* 인라인 코드 `...` */
.inline-code {
  background: #f1ebfb;
  border: 1px solid var(--blue-line);
  border-radius: 6px;
  padding: 1px 6px;
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  font-size: 0.88em;
  color: #6a2fc0;
}
.msg.user .inline-code { background: rgba(255,255,255,0.2); border-color: rgba(255,255,255,0.3); color: #fff; }

/* 코드블록 ```...``` */
.code-block {
  margin: 10px 0;
  border: 1px solid #2b3a44;
  border-radius: 12px;
  overflow: hidden;
  background: #1e2a33;
}
.code-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px 6px 14px;
  background: #16222b;
  border-bottom: 1px solid #2b3a44;
}
.code-lang {
  font-size: 12px;
  color: #9db6c6;
  font-family: ui-monospace, Menlo, Consolas, monospace;
}
.code-copy {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  border: 0;
  background: none;
  color: #9db6c6;
  font: inherit;
  font-size: 12px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 7px;
  transition: background 0.15s, color 0.15s;
}
.code-copy:hover { background: #2b3a44; color: #fff; }
.code-block pre {
  margin: 0;
  padding: 14px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.code-block pre code {
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  font-size: 13px;
  line-height: 1.55;
  color: #e6edf3;
  white-space: pre;
}

.copy-btn {
  border: 0;
  background: none;
  color: var(--muted);
  padding: 2px 4px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  /* 평소엔 숨겨두고 말풍선에 마우스를 올렸을 때만 보이게 */
  opacity: 0;
  transition: opacity 0.15s, color 0.15s;
}
/* 메시지 줄에 마우스를 올리면 복사 아이콘이 나타남 */
.msg:hover .copy-btn,
.copy-btn:focus-visible { opacity: 1; }
.copy-btn:hover { color: var(--blue); }
.copy-btn.copied { color: var(--blue); opacity: 1; }

/* 터치 기기(폰)엔 hover가 없으니 아이콘을 항상 은은하게 보여준다.
   (텍스트를 꾹 누르면 브라우저 기본 '복사하기'도 함께 뜬다) */
@media (hover: none) {
  .copy-btn { opacity: 0.55; }
}

/* 패널 공통 (학교자료/라이브러리/저장소) */
.side-panel {
  width: 100%;
  max-width: 820px;
  margin: 0 auto;
  padding: 16px;
  background: #fff;
  border-bottom: 1px solid var(--blue-line);
  overflow-y: auto;
  max-height: 55vh;
}
.side-panel h2 { margin: 0 0 6px; font-size: 16px; color: var(--blue); }
.panel-help { margin: 0 0 12px; font-size: 13px; color: var(--muted); line-height: 1.5; }

#kb-form, #memory-form { display: flex; flex-direction: column; gap: 8px; }
#memory-form { flex-direction: row; }
#kb-form input, #kb-form textarea,
#memory-form input {
  padding: 10px 12px;
  border: 1px solid var(--blue-line);
  border-radius: 10px;
  font: inherit;
  resize: vertical;
}
#memory-form input { flex: 1; }
#kb-form input:focus, #kb-form textarea:focus,
#memory-form input:focus { outline: none; border-color: var(--blue); }
#kb-form button, #memory-form button {
  align-self: flex-start;
  flex-shrink: 0;
  padding: 10px 18px;
  border: 0;
  border-radius: 10px;
  background: var(--blue);
  color: #fff;
  font: inherit;
  cursor: pointer;
}
#kb-list, #memory-list { list-style: none; padding: 0; margin: 12px 0 0; }
#kb-list li, #memory-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 9px 0;
  border-top: 1px solid var(--blue-soft);
}
.kb-title-text, .mem-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.kb-empty, .mem-empty { color: var(--muted); font-size: 13px; justify-content: center !important; }
#kb-list li button, #memory-list li button {
  flex-shrink: 0;
  padding: 5px 11px;
  border: 0;
  border-radius: 8px;
  background: var(--blue-soft);
  color: var(--blue);
  cursor: pointer;
}
#kb-list li button:hover, #memory-list li button:hover { background: #f3d9d9; color: #d64545; }

/* 라이브러리 그리드 */
.library-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
  gap: 10px;
  margin-top: 4px;
}
.lib-item {
  position: relative;
  border: 1px solid var(--blue-line);
  border-radius: 10px;
  overflow: hidden;
  cursor: pointer;
  aspect-ratio: 1;
}
.lib-item img { width: 100%; height: 100%; object-fit: cover; display: block; }
.lib-item .lib-del {
  position: absolute;
  top: 4px; right: 4px;
  width: 22px; height: 22px;
  border: 0;
  border-radius: 50%;
  background: rgba(15, 40, 60, 0.6);
  color: #fff;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.lib-del:hover { background: #d64545; }
.lib-empty { color: var(--muted); font-size: 13px; grid-column: 1 / -1; text-align: center; padding: 10px; }

/* 입력 영역 */
.input-area {
  width: 100%;
  max-width: 820px;
  margin: 0 auto;
  background: none;
  padding-bottom: env(safe-area-inset-bottom);
}

/* 입력창 둥근 박스 */
.composer {
  margin: 10px 16px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 22px;
  padding: 8px 8px 8px 16px;
  box-shadow: none;
  backdrop-filter: blur(6px);
  transition: border-color 0.15s, box-shadow 0.15s;
}
.composer:focus-within {
  border-color: var(--blue);
  box-shadow: 0 2px 18px rgba(144, 71, 255, 0.22);
}

/* 첨부 이미지 칩 */
.attach-preview {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 4px 10px;
  margin-bottom: 4px;
  border-bottom: 1px solid var(--blue-soft);
}
.attach-preview img {
  width: 44px;
  height: 44px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--blue-line);
  flex-shrink: 0;
}
.attach-info { flex: 1; min-width: 0; font-size: 13px; color: var(--blue); }
.attach-info span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#attach-remove {
  flex-shrink: 0;
  border: 0;
  background: var(--blue-soft);
  color: var(--blue);
  border-radius: 50%;
  width: 24px;
  height: 24px;
  font-size: 13px;
  cursor: pointer;
}
#attach-remove:hover { background: #ebe1fc; }

/* 입력칸 */
#prompt {
  display: block;
  width: 100%;
  min-height: 24px;
  max-height: 160px;
  border: 0;
  background: none;
  padding: 6px 0;
  font: inherit;
  font-size: 15px;
  line-height: 1.5;
  resize: none;
  color: #f0eef8;
}
#prompt::placeholder { color: rgba(240, 238, 248, 0.55); }
#prompt:focus { outline: none; }

/* 버튼 줄 */
.composer-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 2px;
}
#attach-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 50%;
  background: none;
  color: rgba(240, 238, 248, 0.7);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
#attach-btn:hover { background: rgba(255,255,255,0.12); color: #fff; }
#send-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-left: auto;
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 50%;
  background: var(--blue);
  color: #fff;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
}
#send-btn:hover { background: var(--blue-dark); }
#send-btn:active { transform: scale(0.92); }
#send-btn:disabled { background: #cdbdf0; cursor: default; }

/* 반응형 */
@media (min-width: 600px) {
  .top { padding: 14px 20px; }
  .chat { padding: 24px; gap: 16px; }
  .msg { max-width: 80%; }
  .bubble { font-size: 16px; }
  .composer { margin: 12px 24px; }
}

/* 폰·좁은 화면: 사이드바를 화면 밖으로 숨기고 햄버거로 연다 */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    z-index: 50;
    transform: translateX(-100%);
    box-shadow: 2px 0 20px rgba(15, 40, 60, 0.15);
  }
  /* 열림 상태 */
  body.sidebar-open .sidebar { transform: translateX(0); }
  /* 햄버거·닫기 버튼을 폰에서만 보이게 */
  #hamburger, .only-mobile { display: inline-flex; }

  /* ===== 폰에서 대화/답장 읽기 좋게 ===== */
  .chat { padding: 16px 12px; gap: 12px; }

  /* 말풍선: 화면 폭을 더 넓게 쓰고(92%), 여백·둥글기 조정 */
  .msg { max-width: 92%; }
  .bubble {
    padding: 11px 14px;
    border-radius: 16px;
    font-size: 15.5px;      /* 폰에서 읽기 좋은 크기 */
    line-height: 1.65;
  }
  /* AI 답장은 거의 전체 폭을 쓰게 해 줄바꿈이 덜 답답하도록 */
  .msg.ai { max-width: 96%; }

  /* 마크다운 요소 폰 크기 */
  .md-p { margin: 0 0 7px; }
  h3.md-h { font-size: 17px; }
  h4.md-h { font-size: 15.5px; }
  h5.md-h { font-size: 14.5px; }
  .md-list { padding-left: 20px; }

  /* 코드블록: 폰에서 가로 스크롤 되게, 글자 작게, 말풍선 밖으로 안 넘치게 */
  .code-block { margin: 8px 0; border-radius: 10px; }
  .code-block pre { padding: 12px; }
  .code-block pre code { font-size: 12.5px; line-height: 1.5; }
  .code-bar { padding: 5px 8px 5px 12px; }
  .code-lang, .code-copy { font-size: 11.5px; }

  /* 인라인 코드가 길어도 줄바꿈되게 */
  .inline-code { word-break: break-all; }
}

/* 큰 화면에선 뒤 덮개가 필요 없음 */
@media (min-width: 769px) {
  .sidebar-backdrop { display: none !important; }
}
