/* ═══════════════════════════════════════════════════════════════════════
   Textbook RAG — Styles
   ═══════════════════════════════════════════════════════════════════════ */

:root {
  --bg: #0b0b12;
  --bg-surface: #13131f;
  --bg-elevated: #1c1c2e;
  --bg-input: #181825;
  --border: #252540;
  --border-active: #6969e8;
  --text: #e2e2f0;
  --text-secondary: #9494b8;
  --text-muted: #5e5e7a;
  --accent: #6969e8;
  --accent-hover: #8181ff;
  --accent-soft: rgba(105,105,232,0.15);
  --accent-glow: rgba(105,105,232,0.25);
  --green: #34d399;
  --red: #f87171;
  --orange: #fbbf24;
  --blue: #60a5fa;
  --radius: 14px;
  --radius-sm: 10px;
  --radius-xs: 6px;
  --font: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'JetBrains Mono', 'SF Mono', Menlo, Consolas, monospace;
  --transition: 0.18s ease;
}

* { margin:0; padding:0; box-sizing:border-box; }

html, body {
  height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

/* ── Layout ────────────────────────────────────────────────────────────── */
#app { display:flex; height:100vh; overflow:hidden; }

.sidebar {
  width: 260px;
  min-width: 260px;
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: var(--transition);
}

.sidebar-header {
  padding: 18px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.sidebar-header h2 {
  font-size: 15px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.sidebar-nav { flex:1; overflow-y:auto; padding: 8px; }

.sidebar-nav .nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border-radius: var(--radius-xs);
  cursor: pointer;
  color: var(--text-secondary);
  transition: var(--transition);
  font-size: 13px;
  font-weight: 500;
}

.sidebar-nav .nav-item:hover { background: var(--bg-elevated); color: var(--text); }
.sidebar-nav .nav-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.topbar {
  height: 52px;
  min-height: 52px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  background: var(--bg-surface);
}

.topbar-left { display:flex; align-items:center; gap:10px; }
.topbar-right { display:flex; align-items:center; gap:10px; }

.chat-area {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.chat-footer {
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  background: var(--bg-surface);
}

/* ── Messages ──────────────────────────────────────────────────────────── */
.message {
  display: flex;
  gap: 14px;
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
  animation: msgIn 0.25s ease;
}

@keyframes msgIn {
  from { opacity:0; transform: translateY(8px); }
  to { opacity:1; transform: translateY(0); }
}

.message.user { flex-direction: row-reverse; }

.message .avatar {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  color: #fff;
  flex-shrink: 0;
  letter-spacing: -0.02em;
}

.message.user .avatar { background: var(--green); color: #0b0b12; }

.message .body {
  flex: 1;
  min-width: 0;
}

.message .body .role-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.message .body .content {
  font-size: 14.5px;
  line-height: 1.72;
  color: var(--text);
  word-break: break-word;
}

/* ── Markdown content ──────────────────────────────────────────────────── */
.content h1 { font-size: 1.4em; margin: 20px 0 10px; font-weight: 700; line-height: 1.3; }
.content h2 { font-size: 1.2em; margin: 16px 0 8px; font-weight: 600; }
.content h3 { font-size: 1.05em; margin: 14px 0 6px; font-weight: 600; color: var(--text-secondary); }
.content p { margin: 8px 0; }
.content ul, .content ol { margin: 8px 0; padding-left: 22px; }
.content li { margin: 4px 0; line-height: 1.65; }
.content strong { color: #fff; font-weight: 600; }
.content em { color: var(--text-secondary); }
.content blockquote {
  border-left: 2px solid var(--accent);
  padding: 6px 14px;
  margin: 10px 0;
  background: var(--accent-soft);
  border-radius: 0 var(--radius-xs) var(--radius-xs) 0;
  font-style: italic;
  color: var(--text-secondary);
}
.content code {
  background: var(--bg-elevated);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 0.88em;
}
.content pre {
  background: #0c0c1a;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px 16px;
  margin: 10px 0;
  overflow-x: auto;
}
.content pre code { background: none; padding: 0; font-size: 13px; line-height: 1.6; }
.content table {
  width: 100%;
  border-collapse: collapse;
  margin: 12px 0;
  font-size: 13px;
}
.content th {
  background: var(--bg-elevated);
  padding: 8px 12px;
  text-align: left;
  font-weight: 600;
  border: 1px solid var(--border);
}
.content td {
  padding: 8px 12px;
  border: 1px solid var(--border);
}
.content a { color: var(--accent); text-decoration: none; }
.content a:hover { text-decoration: underline; }
.content hr { border: none; border-top: 1px solid var(--border); margin: 18px 0; }

/* ── Citations ─────────────────────────────────────────────────────────── */
.citation-link {
  display: inline;
  color: var(--accent);
  cursor: pointer;
  text-decoration: none;
  font-size: 0.8em;
  font-weight: 700;
  vertical-align: super;
  padding: 0 1px;
  transition: var(--transition);
}
.citation-link:hover { color: var(--accent-hover); background: var(--accent-soft); border-radius: 2px; }

/* ── Sources panel ─────────────────────────────────────────────────────── */
.sources-panel {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  max-width: 800px;
  width: 100%;
  margin: 8px auto 0 auto;
}

.sources-panel h4 {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 14px;
  font-weight: 600;
}

.source-item {
  padding: 10px 14px;
  border-radius: var(--radius-xs);
  background: var(--bg-elevated);
  margin-bottom: 8px;
  font-size: 13px;
  line-height: 1.5;
  border: 1px solid transparent;
  transition: var(--transition);
}

.source-item:hover { border-color: var(--border); }

.source-item .source-title {
  font-weight: 600;
  color: var(--text);
  font-size: 13.5px;
}

.source-item .source-meta {
  color: var(--text-muted);
  font-size: 11.5px;
  margin-top: 2px;
}

.source-item .source-pages {
  display: inline-flex;
  gap: 5px;
  margin-top: 5px;
}

.source-item .page-badge {
  background: var(--accent-soft);
  color: var(--accent);
  padding: 3px 9px;
  border-radius: 5px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

/* ── Input area ────────────────────────────────────────────────────────── */
.chat-input-wrap {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  max-width: 800px;
  margin: 0 auto;
}

.chat-input-wrap textarea {
  flex: 1;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  resize: none;
  min-height: 46px;
  max-height: 200px;
  outline: none;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.chat-input-wrap textarea::placeholder { color: var(--text-muted); }

.chat-input-wrap textarea:focus {
  border-color: var(--border-active);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.chat-input-wrap button {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 12px 18px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: var(--transition);
  white-space: nowrap;
  letter-spacing: -0.01em;
}

.chat-input-wrap button:hover { background: var(--accent-hover); transform: translateY(-1px); }
.chat-input-wrap button:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }

/* ── Model selector ────────────────────────────────────────────────────── */
.model-select { display:flex; align-items:center; gap:6px; }
.model-select select {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-xs);
  color: var(--text);
  padding: 5px 10px;
  font-size: 12px;
  font-weight: 500;
  outline: none;
  cursor: pointer;
}
.model-select select:focus { border-color: var(--border-active); }

/* ── Login page ────────────────────────────────────────────────────────── */
.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 24px;
}

.login-card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 36px;
  width: 100%;
  max-width: 420px;
}

.login-card h1 {
  font-size: 22px;
  margin-bottom: 2px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.login-card .subtitle {
  color: var(--text-secondary);
  margin-bottom: 24px;
  font-size: 13.5px;
}

.login-card .form-group { margin-bottom: 14px; }

.login-card label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 5px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.login-card input {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-xs);
  padding: 11px 14px;
  color: var(--text);
  font-size: 14px;
  outline: none;
  font-family: var(--font);
  transition: border-color var(--transition), box-shadow var(--transition);
}

.login-card input:focus {
  border-color: var(--border-active);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.login-card .btn {
  width: 100%;
  padding: 13px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-xs);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  margin-top: 6px;
  transition: var(--transition);
}

.login-card .btn:hover { background: var(--accent-hover); }
.login-card .btn:disabled { opacity: 0.4; cursor: not-allowed; }

.login-card .link {
  color: var(--accent);
  cursor: pointer;
  font-size: 12.5px;
  text-decoration: none;
  font-weight: 500;
}
.login-card .link:hover { text-decoration: underline; }
.login-card .error {
  background: rgba(248,113,113,0.12);
  color: var(--red);
  padding: 10px 14px;
  border-radius: var(--radius-xs);
  font-size: 13px;
  margin-bottom: 14px;
}
.login-card .divider {
  text-align: center;
  color: var(--text-muted);
  margin: 14px 0;
  font-size: 12px;
}

/* ── Spinner ───────────────────────────────────────────────────────────── */
.spinner {
  display: inline-block;
  width: 18px;
  height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Toast ─────────────────────────────────────────────────────────────── */
.toast-container {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.toast {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-xs);
  padding: 12px 18px;
  font-size: 13px;
  color: var(--text);
  animation: slideIn 0.3s ease;
  max-width: 380px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.3);
}
.toast.error { border-color: var(--red); }
.toast.success { border-color: var(--green); }
@keyframes slideIn { from { opacity: 0; transform: translateX(16px); } to { opacity: 1; transform: translateX(0); } }

/* ── Image generation ──────────────────────────────────────────────────── */
.image-gen-panel {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  max-width: 800px;
  margin: 0 auto;
}
.image-gen-panel .generated-image { max-width: 100%; border-radius: var(--radius-sm); margin-top: 12px; }
.image-gen-panel .form-row { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.image-gen-panel select, .image-gen-panel input {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-xs);
  color: var(--text);
  padding: 7px 10px;
  font-size: 12px;
  outline: none;
  font-family: var(--font);
}

/* ── Settings ──────────────────────────────────────────────────────────── */
.settings-section { margin-bottom: 22px; }
.settings-section h3 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 10px;
}
.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 9px 0;
  border-bottom: 1px solid var(--border);
}
.settings-row:last-child { border-bottom: none; }
.settings-row label { font-size: 13.5px; }
.settings-row .hint { font-size: 11.5px; color: var(--text-muted); margin-top: 1px; }

/* ── Streaming cursor ──────────────────────────────────────────────────── */
.streaming-cursor::after {
  content: '|';
  animation: blink 1.2s ease infinite;
  color: var(--accent);
  font-weight: 100;
}
@keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: 0; } }

/* ── Citation tooltip ──────────────────────────────────────────────────── */
.cite-tooltip {
  position: absolute;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 15px;
  max-width: 350px;
  z-index: 100;
  font-size: 12px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.5);
  line-height: 1.5;
  pointer-events: none;
}
.cite-tooltip .cite-title { font-weight: 600; margin-bottom: 3px; font-size: 13px; }
.cite-tooltip .cite-page { color: var(--accent); font-weight: 700; font-size: 13px; }
.cite-tooltip .cite-text { color: var(--text-secondary); margin-top: 4px; font-size: 12px; line-height: 1.55; }

/* ── Responsive ────────────────────────────────────────────────────────── */
@media (max-width: 768px) {
  .sidebar { display: none; }
  .sidebar.open { display: flex; position: fixed; z-index: 100; height: 100%; }
  .message, .sources-panel { margin: 0; }
  .chat-area { padding: 12px; }
}
