/* ============================================================
   LearnBase — Global Stylesheet
   Dark operational aesthetic, amber accents, mobile-first
   ============================================================ */

@import url('https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@300;400;500;700&family=DM+Sans:wght@300;400;500;600&display=swap');

/* ── Variables ── */
:root {
  --bg:          #0d0d10;
  --surface:     #141418;
  --surface2:    #1c1c22;
  --surface3:    #252530;
  --border:      rgba(255,255,255,0.07);
  --border-hi:   rgba(255,255,255,0.13);
  --amber:       #d97706;
  --amber-hi:    #f59e0b;
  --amber-dim:   rgba(217,119,6,0.15);
  --text:        #e8e8ed;
  --text-muted:  #666678;
  --text-dim:    #3d3d50;
  --user-msg:    #1a1a28;
  --ai-msg:      #131318;
  --font-ui:     'DM Sans', system-ui, sans-serif;
  --font-mono:   'JetBrains Mono', 'Courier New', monospace;
  --nav-h:       52px;
  --sidebar-w:   260px;
}

/* ── Reset ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; -webkit-text-size-adjust: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; }
button { font-family: var(--font-ui); }

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 4px; height: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--surface3); border-radius: 2px; }

/* ── Top Nav ── */
.topnav {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--nav-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.25rem;
  z-index: 200;
}
.nav-brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.nav-logo {
  color: var(--amber);
  font-size: 1.1rem;
}
.nav-title {
  font-family: var(--font-mono);
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--amber);
  letter-spacing: 0.06em;
}
.nav-links {
  display: flex;
  align-items: center;
  gap: 0.25rem;
}
.nav-link {
  font-size: 0.8rem;
  color: var(--text-muted);
  text-decoration: none;
  padding: 0.3rem 0.65rem;
  border-radius: 4px;
  transition: color 0.15s, background 0.15s;
  font-family: var(--font-mono);
  letter-spacing: 0.03em;
}
.nav-link:hover, .nav-link.active {
  color: var(--amber);
  background: var(--amber-dim);
}
.nav-user {
  font-size: 0.75rem;
  color: var(--text-dim);
  font-family: var(--font-mono);
  padding: 0 0.4rem;
}
.nav-logout { color: var(--text-dim); }

/* ── Chat layout ── */
.chat-body { overflow: hidden; height: 100vh; }

.chat-layout {
  display: flex;
  height: 100vh;
  padding-top: var(--nav-h);
}

/* ── Sidebar ── */
.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
  transition: transform 0.25s cubic-bezier(0.4,0,0.2,1);
  z-index: 150;
}
.sidebar-header {
  padding: 0.9rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  gap: 0.5rem;
}
.btn-new-chat {
  flex: 1;
  background: var(--amber-dim);
  border: 1px solid rgba(217,119,6,0.3);
  color: var(--amber);
  border-radius: 5px;
  padding: 0.5rem 0.75rem;
  font-family: var(--font-mono);
  font-size: 0.8rem;
  font-weight: 700;
  cursor: pointer;
  letter-spacing: 0.04em;
  transition: background 0.15s;
}
.btn-new-chat:hover { background: rgba(217,119,6,0.25); }
.sidebar-close {
  display: none;
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 4px;
  padding: 0.4rem 0.6rem;
  cursor: pointer;
  font-size: 0.85rem;
}
.session-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
}
.loading-sessions {
  color: var(--text-dim);
  font-size: 0.8rem;
  font-family: var(--font-mono);
  padding: 1rem;
  text-align: center;
}
.session-item {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.55rem 0.7rem;
  border-radius: 5px;
  cursor: pointer;
  transition: background 0.15s;
  border: 1px solid transparent;
  margin-bottom: 2px;
}
.session-item:hover { background: var(--surface2); }
.session-item.active {
  background: var(--amber-dim);
  border-color: rgba(217,119,6,0.25);
}
.session-item .s-icon {
  color: var(--text-dim);
  font-size: 0.85rem;
  flex-shrink: 0;
}
.session-item.active .s-icon { color: var(--amber); }
.session-title {
  flex: 1;
  font-size: 0.82rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.session-item.active .session-title { color: var(--amber); }
.session-del {
  opacity: 0;
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 0.85rem;
  padding: 0.1rem 0.2rem;
  border-radius: 3px;
  transition: opacity 0.15s, color 0.15s;
}
.session-item:hover .session-del { opacity: 1; }
.session-del:hover { color: #f87171; }
.sidebar-footer {
  padding: 0.75rem;
  border-top: 1px solid var(--border);
}
.kb-link {
  display: block;
  text-decoration: none;
  color: var(--text-muted);
  font-size: 0.8rem;
  font-family: var(--font-mono);
  padding: 0.5rem 0.6rem;
  border-radius: 4px;
  transition: color 0.15s, background 0.15s;
}
.kb-link:hover { color: var(--amber); background: var(--amber-dim); }

/* ── Backdrop (mobile) ── */
.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  z-index: 140;
  backdrop-filter: blur(2px);
}

/* ── Chat main ── */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}
.chat-topbar {
  display: none;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.hamburger {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 4px;
  padding: 0.35rem 0.6rem;
  cursor: pointer;
  font-size: 1rem;
  transition: color 0.15s;
}
.hamburger:hover { color: var(--amber); }
.chat-title-bar {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  color: var(--text-muted);
  max-width: 60%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Messages ── */
.messages-wrap {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
}
.messages {
  max-width: 760px;
  margin: 0 auto;
  padding: 1.5rem 1rem 1rem;
}

/* Welcome state */
.welcome-state {
  text-align: center;
  padding: 4rem 1rem 2rem;
}
.welcome-icon {
  font-size: 2.5rem;
  color: var(--amber);
  margin-bottom: 1rem;
  display: block;
  animation: pulse 3s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50%       { opacity: 0.7; transform: scale(0.95); }
}
.welcome-state h2 {
  font-size: 1.2rem;
  color: var(--text);
  font-weight: 500;
  margin-bottom: 0.5rem;
}
.welcome-state p {
  color: var(--text-muted);
  font-size: 0.9rem;
  max-width: 400px;
  margin: 0 auto 1.25rem;
}
.welcome-kb-link {
  display: inline-block;
  color: var(--amber);
  font-family: var(--font-mono);
  font-size: 0.8rem;
  text-decoration: none;
  border: 1px solid rgba(217,119,6,0.35);
  padding: 0.45rem 1rem;
  border-radius: 4px;
  transition: background 0.15s;
}
.welcome-kb-link:hover { background: var(--amber-dim); }

/* Message bubbles */
.msg {
  display: flex;
  gap: 0.75rem;
  margin-bottom: 1.25rem;
  animation: fadeUp 0.25s ease;
}
@keyframes fadeUp {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}
.msg-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  font-family: var(--font-mono);
  font-weight: 700;
  margin-top: 2px;
}
.msg.user .msg-avatar  { background: var(--amber-dim); color: var(--amber); border: 1px solid rgba(217,119,6,0.3); }
.msg.ai   .msg-avatar  { background: var(--surface3); color: var(--text-muted); border: 1px solid var(--border); }
.msg-content {
  flex: 1;
  min-width: 0;
}
.msg-role {
  font-size: 0.7rem;
  font-family: var(--font-mono);
  color: var(--text-dim);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  margin-bottom: 0.3rem;
}
.msg.user .msg-role { color: var(--amber); opacity: 0.7; }
.msg-text {
  font-size: 0.92rem;
  line-height: 1.7;
  color: var(--text);
  word-break: break-word;
}
.msg.user .msg-text { color: #cccce0; }

/* Markdown-style formatting in AI responses */
.msg-text p  { margin: 0 0 0.6em; }
.msg-text p:last-child { margin-bottom: 0; }
.msg-text ul, .msg-text ol { padding-left: 1.4em; margin: 0.4em 0 0.6em; }
.msg-text li { margin-bottom: 0.25em; }
.msg-text strong { color: #fff; font-weight: 600; }
.msg-text em { color: var(--text-muted); }
.msg-text code {
  font-family: var(--font-mono);
  font-size: 0.82em;
  background: var(--surface3);
  border: 1px solid var(--border-hi);
  border-radius: 3px;
  padding: 0.1em 0.35em;
  color: var(--amber-hi);
}
.msg-text pre {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1rem;
  overflow-x: auto;
  margin: 0.6em 0;
}
.msg-text pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: 0.83em;
  color: var(--text);
}
.msg-text h1, .msg-text h2, .msg-text h3 {
  color: var(--amber-hi);
  font-weight: 600;
  margin: 0.8em 0 0.3em;
}
.msg-text h1 { font-size: 1.05em; }
.msg-text h2 { font-size: 0.98em; }
.msg-text h3 { font-size: 0.93em; }
.msg-text blockquote {
  border-left: 3px solid var(--amber);
  padding-left: 0.9rem;
  color: var(--text-muted);
  margin: 0.5em 0;
}

/* Thinking indicator */
.msg.thinking .msg-text {
  display: flex;
  align-items: center;
  gap: 4px;
}
.dot {
  width: 6px; height: 6px;
  background: var(--text-dim);
  border-radius: 50%;
  animation: bounce 1.2s infinite;
}
.dot:nth-child(2) { animation-delay: 0.2s; }
.dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30%            { transform: translateY(-5px); background: var(--amber); }
}

/* Source indicator */
.source-badge {
  display: inline-block;
  font-size: 0.7rem;
  font-family: var(--font-mono);
  color: var(--text-dim);
  margin-top: 0.4rem;
  letter-spacing: 0.06em;
}
.source-badge.has-sources { color: var(--amber); opacity: 0.6; }

/* ── Chat input ── */
.chat-input-wrap {
  padding: 0.75rem 1rem 0.9rem;
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.chat-input-inner {
  display: flex;
  align-items: flex-end;
  gap: 0.6rem;
  max-width: 760px;
  margin: 0 auto;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.55rem 0.55rem 0.55rem 0.9rem;
  transition: border-color 0.2s;
}
.chat-input-inner:focus-within { border-color: rgba(217,119,6,0.5); }
.msg-input {
  flex: 1;
  background: none;
  border: none;
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 0.93rem;
  line-height: 1.5;
  resize: none;
  outline: none;
  max-height: 180px;
  overflow-y: auto;
}
.msg-input::placeholder { color: var(--text-dim); }
.send-btn {
  background: var(--amber);
  color: #000;
  border: none;
  border-radius: 7px;
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s, transform 0.1s;
  font-size: 1rem;
  font-weight: 700;
}
.send-btn:hover { background: var(--amber-hi); }
.send-btn:active { transform: scale(0.93); }
.send-btn:disabled { background: var(--surface3); color: var(--text-dim); cursor: not-allowed; }
.input-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 760px;
  margin: 0.3rem auto 0;
  padding: 0 0.2rem;
  min-height: 16px;
}
.chunk-indicator {
  font-size: 0.7rem;
  font-family: var(--font-mono);
  color: var(--amber);
  opacity: 0.7;
}
.char-count {
  font-size: 0.7rem;
  font-family: var(--font-mono);
  color: var(--text-dim);
}

/* ── Mobile ── */
@media (max-width: 700px) {
  .sidebar {
    position: fixed;
    left: 0; top: 0; bottom: 0;
    transform: translateX(-100%);
    padding-top: var(--nav-h);
  }
  .sidebar.open {
    transform: translateX(0);
    box-shadow: 4px 0 24px rgba(0,0,0,0.5);
  }
  .sidebar-backdrop.visible { display: block; }
  .sidebar-close { display: flex; }
  .chat-topbar { display: flex; }
  .nav-user { display: none; }
  .messages { padding: 1rem 0.75rem 0.75rem; }
  .chat-input-wrap { padding: 0.6rem 0.75rem 0.75rem; }
}

/* ── Utility ── */
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); }

/* ── Subject filter bar (chat) ── */
.subject-filter-bar {
  display: flex;
  align-items: center;
  gap: .6rem;
  padding: .5rem 1rem;
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.subject-filter-bar label {
  font-size: .72rem;
  font-family: var(--font-mono);
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .1em;
  white-space: nowrap;
}
.subject-filter-bar select {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: .3rem .7rem;
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: .8rem;
  cursor: pointer;
  transition: border-color .2s;
}
.subject-filter-bar select:focus {
  outline: none;
  border-color: var(--amber);
}
