:root {
  --bg: #0b0e14;
  --bg-2: #11151f;
  --panel: #161b27;
  --panel-2: #1c2230;
  --border: #262d3d;
  --text: #e6e9ef;
  --muted: #8a93a6;
  --accent: #6c8cff;
  --accent-2: #57e6c9;
  --danger: #ff6b6b;
  --ok: #57e6c9;
  --radius: 14px;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: "Inter", system-ui, -apple-system, sans-serif;
  background: radial-gradient(1200px 700px at 80% -10%, #1a2236 0%, var(--bg) 55%);
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

.mono {
  font-family: "JetBrains Mono", monospace;
}

.muted {
  color: var(--muted);
  font-size: 14px;
  line-height: 1.6;
}

/* ===== Layout ===== */
.app {
  display: grid;
  grid-template-columns: 248px 1fr;
  min-height: 100vh;
}

/* ===== Sidebar ===== */
.sidebar {
  background: linear-gradient(180deg, var(--bg-2), #0c0f16);
  border-right: 1px solid var(--border);
  padding: 22px 16px;
  display: flex;
  flex-direction: column;
  gap: 26px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 4px 6px;
}

.brand-logo {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #0b0e14;
  font-size: 18px;
  font-weight: 800;
  box-shadow: 0 6px 18px rgba(108, 140, 255, 0.4);
}

.brand-text {
  display: flex;
  flex-direction: column;
}

.brand-title {
  font-weight: 800;
  font-size: 16px;
  letter-spacing: 0.3px;
}

.brand-sub {
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 1px;
  text-transform: uppercase;
}

.nav {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 13px;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.18s ease;
  text-align: left;
  width: 100%;
}

.nav-item:hover {
  background: var(--panel);
  color: var(--text);
}

.nav-item.active {
  background: linear-gradient(135deg, rgba(108, 140, 255, 0.18), rgba(87, 230, 201, 0.12));
  color: var(--text);
  box-shadow: inset 0 0 0 1px var(--border);
}

.nav-ico {
  font-size: 16px;
}

.sidebar-foot {
  margin-top: auto;
  display: flex;
  align-items: center;
  gap: 9px;
  font-size: 12px;
  color: var(--muted);
  padding: 10px 12px;
  background: var(--panel);
  border-radius: 10px;
}

.status-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--muted);
  box-shadow: 0 0 0 0 rgba(87, 230, 201, 0.6);
}

.status-dot.online {
  background: var(--ok);
  animation: pulse 1.8s infinite;
}

.status-dot.offline {
  background: var(--danger);
}

@keyframes pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(87, 230, 201, 0.5);
  }

  70% {
    box-shadow: 0 0 0 8px rgba(87, 230, 201, 0);
  }

  100% {
    box-shadow: 0 0 0 0 rgba(87, 230, 201, 0);
  }
}

/* ===== Main ===== */
.main {
  padding: 26px 32px;
  display: flex;
  flex-direction: column;
  gap: 22px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  flex-wrap: wrap;
}

.page-title {
  font-size: 26px;
  font-weight: 800;
  letter-spacing: -0.5px;
}

.metrics {
  display: flex;
  gap: 12px;
}

.metric {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 16px;
  min-width: 92px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.metric-label {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.8px;
}

.metric-value {
  font-size: 16px;
  font-weight: 700;
}

.metric-value.mono {
  font-size: 12px;
  word-break: break-all;
}

/* ===== Views ===== */
.view {
  display: none;
}

.view.active {
  display: block;
  animation: fade 0.35s ease;
}

@keyframes fade {
  from {
    opacity: 0;
    transform: translateY(8px);
  }

  to {
    opacity: 1;
    transform: none;
  }
}

/* ===== Cards ===== */
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
}

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.card-head h2 {
  font-size: 15px;
  font-weight: 700;
}

.tag {
  font-size: 11px;
  font-weight: 600;
  color: var(--accent-2);
  background: rgba(87, 230, 201, 0.1);
  border: 1px solid rgba(87, 230, 201, 0.25);
  padding: 3px 9px;
  border-radius: 999px;
}

/* ===== Chat ===== */
.grid-chat {
  display: grid;
  grid-template-columns: 1fr 320px;
  gap: 20px;
  align-items: start;
}

.chat-card {
  display: flex;
  flex-direction: column;
  height: calc(100vh - 190px);
}

.chat-log {
  flex: 1;
  overflow-y: auto;
  padding: 6px 4px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.empty-state {
  margin: auto;
  text-align: center;
  color: var(--muted);
}

.empty-ico {
  font-size: 40px;
  margin-bottom: 10px;
}

.msg {
  display: flex;
  gap: 10px;
  max-width: 85%;
}

.msg.user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.msg-avatar {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  font-size: 15px;
}

.msg.user .msg-avatar {
  background: linear-gradient(135deg, var(--accent), #8aa2ff);
}

.msg.bot .msg-avatar {
  background: var(--panel-2);
  border: 1px solid var(--border);
}

.msg-bubble {
  padding: 11px 14px;
  border-radius: 12px;
  line-height: 1.6;
  font-size: 14px;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg.user .msg-bubble {
  background: linear-gradient(135deg, rgba(108, 140, 255, 0.22), rgba(108, 140, 255, 0.1));
  border: 1px solid rgba(108, 140, 255, 0.3);
}

.msg.bot .msg-bubble {
  background: var(--panel-2);
  border: 1px solid var(--border);
}

.msg-bubble .caret {
  display: inline-block;
  width: 8px;
  color: var(--accent-2);
  animation: blink 1s steps(2) infinite;
}

@keyframes blink {
  50% {
    opacity: 0;
  }
}

.chat-input {
  display: flex;
  gap: 10px;
  margin-top: 14px;
}

.chat-input input {
  flex: 1;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 11px;
  padding: 13px 15px;
  color: var(--text);
  font-size: 14px;
  outline: none;
  transition: border 0.18s;
}

.chat-input input:focus {
  border-color: var(--accent);
}

.chat-input button,
.btn-primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #0b0e14;
  border: none;
  border-radius: 11px;
  padding: 0 22px;
  font-weight: 700;
  font-size: 14px;
  cursor: pointer;
  transition: transform 0.12s, opacity 0.18s;
}

.chat-input button:hover,
.btn-primary:hover {
  transform: translateY(-1px);
}

.chat-input button:disabled,
.btn-primary:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

/* ===== Sources ===== */
.sources-card {
  max-height: calc(100vh - 190px);
  overflow-y: auto;
}

.sources {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.source-item {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 12px;
  font-size: 13px;
  line-height: 1.55;
  animation: fade 0.4s ease;
}

.source-item .src-idx {
  display: inline-block;
  font-weight: 700;
  color: var(--accent-2);
  font-size: 11px;
  margin-bottom: 5px;
}

/* ===== Upload ===== */
.grid-upload {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  align-items: start;
}

.dropzone {
  display: block;
  margin: 14px 0;
  padding: 34px 20px;
  border: 2px dashed var(--border);
  border-radius: 12px;
  background: var(--bg-2);
  cursor: pointer;
  text-align: center;
  transition: all 0.2s ease;
}

.dropzone:hover,
.dropzone.drag {
  border-color: var(--accent);
  background: rgba(108, 140, 255, 0.06);
}

.dz-ico {
  font-size: 30px;
  margin-bottom: 8px;
}

.dz-title {
  font-weight: 600;
  font-size: 14px;
  margin-bottom: 4px;
}

.dz-sub {
  font-size: 12px;
  color: var(--muted);
}

.btn-primary {
  width: 100%;
  padding: 13px;
  margin-top: 4px;
}

.progress-wrap {
  margin-top: 16px;
}

.progress-track {
  height: 8px;
  background: var(--bg-2);
  border-radius: 999px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.progress-bar {
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  transition: width 0.35s ease;
}

.progress-text {
  display: block;
  margin-top: 8px;
  font-size: 12px;
  color: var(--muted);
}

.toast {
  margin-top: 14px;
  padding: 12px 14px;
  border-radius: 10px;
  font-size: 13px;
  line-height: 1.5;
}

.toast.ok {
  background: rgba(87, 230, 201, 0.1);
  border: 1px solid rgba(87, 230, 201, 0.3);
  color: var(--ok);
}

.toast.err {
  background: rgba(255, 107, 107, 0.1);
  border: 1px solid rgba(255, 107, 107, 0.3);
  color: var(--danger);
}

.mini-btn {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 12px;
  padding: 5px 11px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.16s;
}

.mini-btn:hover {
  color: var(--text);
  border-color: var(--accent);
}

.doc-list {
  display: flex;
  flex-direction: column;
  gap: 9px;
  max-height: 420px;
  overflow-y: auto;
}

.doc-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 13px;
  animation: fade 0.35s ease;
}

.doc-name {
  font-size: 13px;
  font-weight: 600;
  word-break: break-all;
}

.doc-meta {
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
}

/* ===== SplitType chars ===== */
.char {
  display: inline-block;
  opacity: 0;
  transform: translateY(14px);
}

/* ===== Head actions ===== */
.head-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* ===== Message body + markdown ===== */
.msg-body {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 100%;
}

.msg-bubble p:first-child {
  margin-top: 0;
}

.msg-bubble p:last-child {
  margin-bottom: 0;
}

.msg-bubble p {
  margin: 8px 0;
}

.msg-bubble ul,
.msg-bubble ol {
  margin: 8px 0;
  padding-left: 20px;
}

.msg-bubble li {
  margin: 3px 0;
}

.msg-bubble code {
  background: rgba(255, 255, 255, 0.08);
  padding: 2px 6px;
  border-radius: 5px;
  font-family: "JetBrains Mono", monospace;
  font-size: 13px;
}

.msg-bubble pre {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  overflow-x: auto;
  margin: 8px 0;
}

.msg-bubble pre code {
  background: none;
  padding: 0;
}

.msg-bubble h1,
.msg-bubble h2,
.msg-bubble h3 {
  font-size: 15px;
  margin: 10px 0 6px;
}

.msg-bubble strong {
  color: #fff;
}

/* 인용 칩 */
.cite {
  color: var(--accent-2);
  cursor: pointer;
  font-weight: 700;
  font-size: 12px;
  padding: 0 2px;
  border-radius: 4px;
  transition: background 0.15s;
}

.cite:hover {
  background: rgba(87, 230, 201, 0.18);
}

/* ===== Suggest chips ===== */
.suggest-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-top: 16px;
}

.chip {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.16s;
}

.chip:hover {
  border-color: var(--accent);
  background: rgba(108, 140, 255, 0.12);
  transform: translateY(-1px);
}

/* ===== Learn badge (few-shot 참고 표시) ===== */
.learn-badge {
  align-self: flex-start;
  font-size: 11px;
  font-weight: 600;
  color: #ffd479;
  background: rgba(255, 212, 121, 0.1);
  border: 1px solid rgba(255, 212, 121, 0.3);
  padding: 3px 10px;
  border-radius: 999px;
}

/* ===== Feedback bar ===== */
.feedback-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding-left: 4px;
}

.fb {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 10px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.15s;
}

.fb:hover:not(:disabled) {
  border-color: var(--accent);
  transform: scale(1.08);
}

.fb:disabled {
  opacity: 0.5;
  cursor: default;
}

.fb-msg {
  font-size: 12px;
  color: var(--accent-2);
}

/* ===== Chat stop button ===== */
#chat-stop {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 11px;
  padding: 0 18px;
  font-weight: 600;
  cursor: pointer;
}

#chat-stop:hover {
  border-color: var(--danger);
  color: var(--danger);
}

/* ===== Sources (enhanced) ===== */
.source-item.flash {
  animation: flash 1.2s ease;
}

@keyframes flash {

  0%,
  100% {
    box-shadow: none;
  }

  30% {
    box-shadow: 0 0 0 2px var(--accent-2);
  }
}

.src-top {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
  flex-wrap: wrap;
}

.src-idx {
  font-weight: 700;
  color: var(--accent-2);
  font-size: 12px;
}

.src-file {
  font-size: 12px;
  color: var(--text);
  font-weight: 600;
  word-break: break-all;
}

.src-score {
  font-size: 11px;
  color: var(--muted);
  margin-left: auto;
}

.src-preview {
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--muted);
}

/* ===== Upload steps ===== */
.steps {
  display: flex;
  gap: 8px;
  margin-top: 16px;
  flex-wrap: wrap;
}

.step {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 11px;
}

.step-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--border);
}

.step.active {
  color: var(--text);
  border-color: var(--accent);
}

.step.active .step-dot {
  background: var(--accent);
  animation: pulse 1.5s infinite;
}

.step.done {
  color: var(--ok);
}

.step.done .step-dot {
  background: var(--ok);
}

/* ===== Doc item (with delete) ===== */
.doc-info {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
}

.doc-del {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 8px;
  padding: 5px 9px;
  cursor: pointer;
  font-size: 13px;
  transition: all 0.15s;
}

.doc-del:hover {
  border-color: var(--danger);
  color: var(--danger);
}

/* ===== Sessions ===== */
.mini-btn.danger {
  color: var(--danger);
}

.mini-btn.danger:hover {
  border-color: var(--danger);
}

.session-list {
  display: flex;
  flex-direction: column;
  gap: 9px;
  margin-top: 14px;
}

.session-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  transition: all 0.16s;
  animation: fade 0.3s ease;
}

.session-item:hover {
  border-color: var(--accent);
}

.session-item.current {
  border-color: var(--accent-2);
  box-shadow: inset 0 0 0 1px var(--accent-2);
}

.session-info {
  flex: 1;
  min-width: 0;
  cursor: pointer;
}

.session-title {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 3px;
  word-break: break-all;
}

.session-meta {
  font-size: 11px;
  color: var(--muted);
}

.session-del {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 8px;
  padding: 5px 9px;
  cursor: pointer;
  font-size: 13px;
  transition: all 0.15s;
}

.session-del:hover {
  border-color: var(--danger);
  color: var(--danger);
}

/* 세션 토스트 */
.session-toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 11px 20px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s, transform 0.25s;
  z-index: 999;
}

.session-toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ===== 원문 보기 버튼 ===== */
.src-view {
  margin-top: 8px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--accent-2);
  font-size: 11px;
  padding: 4px 10px;
  border-radius: 7px;
  cursor: pointer;
  transition: all 0.15s;
}

.src-view:hover {
  border-color: var(--accent-2);
  background: rgba(87, 230, 201, 0.1);
}

/* 캐시 배지 */
.learn-badge.cache {
  color: #8aa2ff;
  background: rgba(108, 140, 255, 0.1);
  border-color: rgba(108, 140, 255, 0.3);
}

/* 질문 재작성 배지 */
.learn-badge.rewrite {
  color: #c9a8ff;
  background: rgba(180, 140, 255, 0.1);
  border-color: rgba(180, 140, 255, 0.3);
}

/* ===== 원문 모달 (기능 3) ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  animation: fade 0.2s ease;
}

/* hidden 속성이 display:flex 에 덮이지 않도록 명시적으로 숨김 처리 */
.modal-overlay[hidden] {
  display: none;
}

.modal {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: min(820px, 100%);
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow);
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.modal-head h3 {
  font-size: 15px;
  font-weight: 700;
  word-break: break-all;
}

.modal-close {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 18px;
  cursor: pointer;
  padding: 2px 8px;
  border-radius: 6px;
}

.modal-close:hover {
  color: var(--text);
  background: var(--panel-2);
}

.modal-body {
  overflow-y: auto;
  padding: 18px 20px;
}

.modal-body pre {
  white-space: pre-wrap;
  word-break: break-word;
  font-family: "JetBrains Mono", monospace;
  font-size: 13px;
  line-height: 1.7;
  color: var(--text);
  margin: 0;
}

/* ===== Select & Switch ===== */
.select-input {
  width: 100%;
  background: var(--bg-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 14px;
  outline: none;
  cursor: pointer;
}

.select-input:focus {
  border-color: var(--accent);
}

.toggle-row {
  margin-top: 18px;
}

/* 관리자 토큰 입력 */
.admin-token-row {
  margin: 14px 0;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.admin-token-hint {
  font-size: 11px;
}

.switch {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  font-size: 14px;
}

.switch input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
  cursor: pointer;
}

/* ===== Settings ===== */
.settings-card {
  max-width: 640px;
}

.setting-row {
  margin: 22px 0;
}

.setting-label {
  display: flex;
  flex-direction: column;
  gap: 3px;
  margin-bottom: 10px;
}

.setting-label span:first-child {
  font-size: 14px;
  font-weight: 600;
}

.setting-label b {
  color: var(--accent-2);
}

.setting-row input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.settings-hint {
  margin-top: 18px;
  padding: 12px 14px;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 13px;
  color: var(--muted);
}

.settings-hint b {
  color: var(--text);
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar {
  width: 9px;
  height: 9px;
}

::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 999px;
}

::-webkit-scrollbar-thumb:hover {
  background: #333c50;
}

/* ===== Responsive ===== */
@media (max-width: 980px) {
  .app {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: relative;
    height: auto;
    flex-direction: row;
    align-items: center;
    flex-wrap: wrap;
  }

  .sidebar-foot {
    margin: 0;
  }

  .nav {
    flex-direction: row;
  }

  .grid-chat,
  .grid-upload {
    grid-template-columns: 1fr;
  }

  .chat-card {
    height: 62vh;
  }
}