/* ── Reset & Base ───────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:          #f5f6f8;
  --sidebar-bg:  #1e293b;
  --sidebar-fg:  #cbd5e1;
  --primary:     #3b82f6;
  --primary-dk:  #2563eb;
  --msg-user:    #dbeafe;
  --msg-assist:  #ffffff;
  --border:      #e2e8f0;
  --text:        #1e293b;
  --text-light:  #64748b;
  --radius:      8px;
  --font:        'Segoe UI', system-ui, -apple-system, sans-serif;
}

html, body { height: 100%; font-family: var(--font); color: var(--text); background: var(--bg); }

#app { display: flex; height: 100vh; }

/* ── Sidebar ───────────────────────────────────────────── */
#sidebar {
  width: 280px; min-width: 280px;
  background: var(--sidebar-bg); color: var(--sidebar-fg);
  display: flex; flex-direction: column;
  padding: 24px 20px;
  overflow-y: auto;
}
.sidebar-header h2 { font-size: 1.15rem; color: #fff; margin-bottom: 20px; }
.sidebar-info { font-size: 0.85rem; line-height: 1.6; }
.sidebar-info ul { padding-left: 18px; margin-top: 6px; }
.sidebar-stats {
  margin-top: 20px; padding: 10px 12px;
  background: rgba(255,255,255,0.06); border-radius: var(--radius);
  font-size: 0.82rem;
}
.stat-label { opacity: 0.7; }
.stat-value { font-weight: 600; color: #fff; }
.sidebar-footer { margin-top: auto; padding-top: 20px; font-size: 0.8rem; }
.tips-title { font-weight: 600; color: #fff; margin-bottom: 6px; }
.tips { padding-left: 16px; line-height: 1.7; }

/* ── Main chat area ────────────────────────────────────── */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

#chat-messages {
  flex: 1; overflow-y: auto; padding: 24px 32px;
  display: flex; flex-direction: column; gap: 16px;
}

/* ── Messages ──────────────────────────────────────────── */
.message { max-width: 780px; width: 100%; }
.message.user { align-self: flex-end; }
.message.assistant { align-self: flex-start; }

.message-content {
  padding: 14px 18px; border-radius: var(--radius);
  font-size: 0.92rem; line-height: 1.65;
  white-space: pre-wrap; word-wrap: break-word;
}
.message.user .message-content {
  background: var(--msg-user); border: 1px solid #bfdbfe;
}
.message.assistant .message-content {
  background: var(--msg-assist); border: 1px solid var(--border);
  box-shadow: 0 1px 3px rgba(0,0,0,0.04);
}

/* ── Citation badges in answer text ─────────────────────── */
.citation-badge {
  display: inline-block;
  background: var(--primary);
  color: #fff;
  font-size: 0.72rem;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: 4px;
  cursor: pointer;
  vertical-align: super;
  line-height: 1;
  margin: 0 1px;
  transition: background 0.15s;
}
.citation-badge:hover { background: var(--primary-dk); }

/* ── References section ────────────────────────────────── */
.references-section {
  margin-top: 12px;
}
.references-header {
  font-weight: 600; font-size: 0.85rem; color: var(--primary);
  margin-bottom: 8px;
  padding: 8px 12px;
  background: #f0f7ff;
  border-radius: var(--radius);
  border: 1px solid #dbeafe;
}

/* ── Reference cards ───────────────────────────────────── */
.ref-card {
  margin-top: 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
  transition: box-shadow 0.2s;
}
.ref-card.highlight {
  box-shadow: 0 0 0 2px var(--primary);
  border-color: var(--primary);
}
.ref-card-header {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px;
  background: #f8fafc;
  cursor: pointer;
  user-select: none;
}
.ref-card-header:hover { background: #f1f5f9; }
.ref-card-num {
  background: var(--primary);
  color: #fff;
  font-size: 0.72rem;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 4px;
  flex-shrink: 0;
}
.ref-card-info { flex: 1; min-width: 0; }
.ref-source {
  font-weight: 600; font-size: 0.82rem; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.ref-path {
  font-size: 0.75rem; color: var(--text-light);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.ref-card-toggle {
  font-size: 0.7rem; color: var(--text-light);
  transition: transform 0.2s;
  flex-shrink: 0;
}
.ref-card.expanded .ref-card-toggle { transform: rotate(180deg); }
.ref-card-body {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease;
}
.ref-card.expanded .ref-card-body {
  max-height: 400px;
  overflow-y: auto;
}
.ref-chunk-text {
  padding: 12px;
  font-size: 0.78rem;
  line-height: 1.6;
  color: var(--text);
  background: #fff;
  border-top: 1px solid var(--border);
  white-space: pre-wrap;
  word-wrap: break-word;
}

/* ── Loading ───────────────────────────────────────────── */
.loading-dots span {
  display: inline-block; width: 8px; height: 8px;
  background: var(--primary); border-radius: 50%;
  margin: 0 3px; animation: bounce 1.4s infinite ease-in-out both;
}
.loading-dots span:nth-child(1) { animation-delay: -0.32s; }
.loading-dots span:nth-child(2) { animation-delay: -0.16s; }
@keyframes bounce {
  0%, 80%, 100% { transform: scale(0); }
  40% { transform: scale(1); }
}

/* ── Input form ────────────────────────────────────────── */
#chat-form {
  padding: 16px 32px 20px;
  background: var(--bg);
  border-top: 1px solid var(--border);
}
.input-row { display: flex; gap: 8px; max-width: 780px; }
#question-input {
  flex: 1; padding: 12px 16px;
  border: 1px solid var(--border); border-radius: var(--radius);
  font-size: 0.92rem; font-family: var(--font);
  outline: none; transition: border-color 0.15s;
}
#question-input:focus { border-color: var(--primary); }
#send-btn {
  padding: 0 16px; background: var(--primary); color: #fff;
  border: none; border-radius: var(--radius); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.15s;
}
#send-btn:hover { background: var(--primary-dk); }
#send-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ── Responsive ────────────────────────────────────────── */
@media (max-width: 768px) {
  #sidebar { display: none; }
  #chat-messages { padding: 16px; }
  #chat-form { padding: 12px 16px; }
}
