/* ============================================================
   Kaito — Field-notebook design system
   A warm cream paper with dark ink, hairline rules,
   and a single quiet accent (moss) with rust for evidence.
   ============================================================ */

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

:root {
  /* Surfaces */
  --paper:    #F4F1EA;   /* cream paper */
  --paper-2:  #ECE7DA;   /* slightly deeper cream for cards/inputs */
  --paper-3:  #E2DCCB;   /* hover, active fill */

  /* Ink */
  --ink:      #0E0E0C;   /* near-black, warm */
  --ink-2:    #3A3833;   /* secondary text */
  --ink-3:    #6E6A60;   /* tertiary, hints */
  --ink-4:    #9C968A;   /* placeholder, disabled */

  /* Rules */
  --rule:     #1C1B18;   /* hairline */
  --rule-2:   #C9C2AF;   /* faint paper rule */

  /* Accents */
  --moss:     #5B6B4A;   /* primary accent — active, focus, headings */
  --moss-2:   #46553A;   /* moss on hover */
  --moss-soft:rgba(91,107,74,0.10);
  --rust:     #B5523A;   /* evidence, sources, citations */
  --rust-soft:rgba(181,82,58,0.10);

  /* Geometry */
  --r-sm: 4px;
  --r-md: 8px;
  --r-lg: 14px;

  /* Layout */
  --sidebar-w: 248px;
  --rail-w: 248px;
  --content-max: 720px;

  --ease: cubic-bezier(.2,.7,.2,1);
  --t: 0.18s var(--ease);
}

html, body { height: 100%; overflow: hidden; }

body {
  font-family: 'Inter', sans-serif;
  background: var(--paper);
  background-image:
    /* faint horizontal ruled lines, like a notebook */
    repeating-linear-gradient(
      to bottom,
      transparent 0,
      transparent 31px,
      rgba(28,27,24,0.045) 31px,
      rgba(28,27,24,0.045) 32px
    ),
    /* paper grain noise */
    url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='160' height='160'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0.07 0 0 0 0 0.07 0 0 0 0 0.06 0 0 0 0.06 0'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>");
  color: var(--ink);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(28,27,24,0.18); border-radius: 99px; }
::-webkit-scrollbar-thumb:hover { background: rgba(28,27,24,0.32); }

/* ── Utilities ── */
.hidden { display: none !important; }

/* ── Typography — display ── */
.wordmark {
  font-family: 'Fraunces', serif;
  font-weight: 500;
  font-size: 28px;
  letter-spacing: -0.02em;
  font-variation-settings: "opsz" 144, "SOFT" 30, "WONK" 0;
  line-height: 1;
  color: var(--ink);
}
.wordmark-k { font-weight: 700; color: var(--moss); }
.wordmark-sm { font-size: 20px; }

/* ── Focus ── */
:where(button, input, textarea, [tabindex]):focus-visible {
  outline: 2px solid var(--moss);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ============================================================
   MODAL / AUTH
   ============================================================ */
.modal-overlay {
  position: fixed; inset: 0; z-index: 1000;
  background: rgba(14,14,12,0.55);
  backdrop-filter: blur(8px);
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
  animation: fadeIn 0.3s var(--ease);
}
.modal-overlay.hidden { display: none !important; }

.modal-card {
  position: relative;
  background: var(--paper);
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='160' height='160'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0.07 0 0 0 0 0.07 0 0 0 0 0.06 0 0 0 0.05 0'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>");
  border: 1px solid var(--rule);
  border-radius: var(--r-md);
  padding: 44px 40px 36px;
  width: 100%; max-width: 460px;
  box-shadow:
    0 30px 80px rgba(14,14,12,0.35),
    0 0 0 6px var(--paper),
    0 0 0 7px var(--rule);
  animation: slideUp 0.35s var(--ease);
}
.modal-header { text-align: center; margin-bottom: 28px; }
.modal-eyebrow {
  margin-top: 10px;
  font-family: 'Fraunces', serif;
  font-style: italic;
  font-size: 14px;
  color: var(--ink-3);
  letter-spacing: 0.01em;
}

/* Auth Tabs */
.auth-tabs {
  display: flex; gap: 0; margin-bottom: 22px;
  border-bottom: 1px solid var(--rule);
}
.auth-tab {
  flex: 1; padding: 10px 4px;
  background: none; border: none; cursor: pointer;
  color: var(--ink-3); font-size: 14px; font-weight: 500;
  font-family: 'Inter', sans-serif;
  transition: color var(--t);
  position: relative;
}
.auth-tab:hover { color: var(--ink-2); }
.auth-tab.active { color: var(--ink); font-weight: 600; }
.auth-tab.active::after {
  content: ''; position: absolute; bottom: -1px; left: 0; right: 0;
  height: 2px; background: var(--moss);
}

/* Form */
.modal-form { display: flex; flex-direction: column; gap: 16px; }
.form-group { display: flex; flex-direction: column; gap: 6px; }
.form-group label {
  font-size: 12px; font-weight: 600; letter-spacing: 0.04em;
  text-transform: uppercase; color: var(--ink-3);
}
.required { color: var(--rust); }

.input-wrapper { position: relative; display: flex; align-items: center; }
.input-wrapper input {
  width: 100%; padding: 11px 44px 11px 14px;
  background: transparent;
  border: none;
  border-bottom: 1px solid var(--rule);
  border-radius: 0;
  color: var(--ink);
  font-family: 'Inter', sans-serif;
  font-size: 15px;
  outline: none;
  transition: border-color var(--t);
}
.input-wrapper input:focus {
  border-bottom-color: var(--moss);
  box-shadow: none;
}
.input-wrapper input::placeholder { color: var(--ink-4); font-style: italic; }

.toggle-visibility {
  position: absolute; right: 6px;
  background: none; border: none; cursor: pointer;
  color: var(--ink-3); padding: 4px;
  display: flex; align-items: center;
  transition: color var(--t);
}
.toggle-visibility:hover { color: var(--ink); }
.toggle-visibility svg { width: 17px; height: 17px; }

.error-banner {
  background: var(--rust-soft);
  border-left: 2px solid var(--rust);
  color: var(--rust);
  border-radius: 0;
  padding: 8px 12px;
  font-size: 13px;
}

/* Buttons */
.btn-primary {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  padding: 13px 24px;
  background: var(--ink);
  color: var(--paper);
  font-weight: 600; font-size: 15px;
  font-family: 'Inter', sans-serif;
  border: none; border-radius: var(--r-sm);
  cursor: pointer; transition: background var(--t), transform var(--t);
}
.btn-primary:hover { background: var(--moss-2); transform: translateY(-1px); }
.btn-primary:active { transform: translateY(0); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
.btn-full { width: 100%; }

.btn-spinner {
  width: 14px; height: 14px;
  border: 1.5px solid rgba(244,241,234,0.4);
  border-top-color: var(--paper);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

/* ============================================================
   APP LAYOUT
   ============================================================ */
.app {
  display: flex; height: 100vh; overflow: hidden;
}
.app.hidden { display: none !important; }

/* ============================================================
   SIDEBAR
   ============================================================ */
.sidebar {
  width: var(--sidebar-w); flex-shrink: 0;
  background: var(--paper-2);
  border-right: 1px solid var(--rule);
  display: flex; flex-direction: column;
  overflow: hidden;
  transition: width var(--t), transform var(--t);
}
.sidebar.collapsed { width: 0; }

.sidebar-header {
  display: flex; align-items: center; gap: 10px;
  padding: 20px 20px 18px;
  border-bottom: 1px solid var(--rule);
  flex-shrink: 0;
}
.sidebar-header .wordmark { flex: 1; }

.icon-btn {
  display: flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; border-radius: var(--r-sm);
  background: none; border: none; cursor: pointer;
  color: var(--ink-3); transition: background var(--t), color var(--t);
}
.icon-btn:hover { background: var(--paper-3); color: var(--ink); }
.icon-btn svg { width: 18px; height: 18px; }

.sidebar-section { padding: 18px 16px 0; flex-shrink: 0; }
.sidebar-section-grow { flex: 1; min-height: 0; display: flex; flex-direction: column; }

.section-label {
  font-family: 'Fraunces', serif;
  font-style: italic;
  font-size: 12px;
  color: var(--ink-3);
  letter-spacing: 0.01em;
  padding: 0 4px 10px;
  border-bottom: 1px solid var(--rule-2);
  margin-bottom: 8px;
}

.btn-new-chat {
  display: flex; align-items: center; gap: 10px;
  width: 100%; padding: 10px 14px;
  background: transparent;
  border: 1px solid var(--rule);
  border-radius: var(--r-sm);
  color: var(--ink);
  font-family: 'Inter', sans-serif;
  font-size: 13.5px; font-weight: 500;
  cursor: pointer; transition: background var(--t), border-color var(--t);
}
.btn-new-chat:hover { background: var(--paper-3); border-color: var(--ink); }
.btn-new-chat .plus-mark {
  font-family: 'Fraunces', serif;
  font-size: 18px; font-weight: 400;
  color: var(--moss);
  line-height: 1;
}

/* Thread list */
.thread-list {
  display: flex; flex-direction: column; gap: 1px;
  overflow-y: auto; flex: 1;
  padding-bottom: 4px;
}
.thread-item {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px; border-radius: var(--r-sm);
  cursor: pointer; transition: background var(--t);
  font-size: 13px; color: var(--ink-2);
  position: relative;
}
.thread-item:hover { background: var(--paper-3); color: var(--ink); }
.thread-item.active {
  background: var(--paper);
  color: var(--ink);
  font-weight: 500;
}
.thread-item.active::before {
  content: ''; position: absolute; left: -16px; top: 8px; bottom: 8px;
  width: 2px; background: var(--moss);
}
.thread-preview {
  flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-family: 'Fraunces', serif;
  font-size: 13.5px;
}
.thread-delete {
  display: none; background: none; border: none;
  color: var(--ink-3); cursor: pointer; padding: 2px 6px;
  border-radius: 4px; font-size: 16px; line-height: 1;
}
.thread-item:hover .thread-delete { display: block; }
.thread-delete:hover { color: var(--rust); }

/* Doc list */
.doc-list {
  display: flex; flex-direction: column; gap: 2px;
  max-height: 130px; overflow-y: auto;
  margin-top: 4px;
}
.doc-item {
  display: flex; align-items: center; gap: 8px;
  font-size: 12.5px; color: var(--ink-2);
  padding: 6px 8px;
  border-radius: var(--r-sm);
  transition: background var(--t);
}
.doc-item:hover { background: var(--paper-3); }
.doc-item svg { width: 13px; height: 13px; flex-shrink: 0; color: var(--rust); }
.doc-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; font-family: 'JetBrains Mono', monospace; font-size: 12px; }
.doc-delete {
  display: none; background: none; border: none;
  color: var(--ink-3); cursor: pointer; padding: 2px 4px;
  border-radius: 4px; font-size: 14px; line-height: 1; flex-shrink: 0;
}
.doc-item:hover .doc-delete { display: block; }
.doc-delete:hover { color: var(--rust); }
.empty-hint { font-size: 12px; color: var(--ink-4); padding: 6px 4px; font-style: italic; font-family: 'Fraunces', serif; }

.btn-ghost {
  display: flex; align-items: center; gap: 7px;
  background: none; border: 1px solid var(--rule-2);
  border-radius: var(--r-sm); color: var(--ink-3);
  cursor: pointer; transition: all var(--t);
  font-size: 12.5px;
  font-family: 'Inter', sans-serif;
}
.btn-ghost:hover { background: var(--paper-3); color: var(--ink); border-color: var(--rule); }
.btn-sm { padding: 7px 11px; }
.btn-ghost svg { width: 13px; height: 13px; }

/* User profile in footer */
.user-profile {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px;
  border-radius: var(--r-sm);
  border: 1px solid var(--rule-2);
  background: var(--paper);
}
.user-avatar {
  width: 30px; height: 30px; border-radius: 50%;
  background: var(--moss);
  color: var(--paper);
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 600;
  font-family: 'Fraunces', serif;
  flex-shrink: 0;
}
.user-info { flex: 1; overflow: hidden; }
.user-name {
  font-size: 13px; font-weight: 600; color: var(--ink);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.user-email {
  font-size: 11px; color: var(--ink-3);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-family: 'JetBrains Mono', monospace;
}

.sidebar-footer {
  margin-top: auto; padding: 12px;
  border-top: 1px solid var(--rule);
  display: flex; flex-direction: column; gap: 8px; flex-shrink: 0;
}

/* ============================================================
   CHAT AREA — page grid with conversation + sources rail
   ============================================================ */
.chat-area {
  flex: 1; display: flex; flex-direction: column;
  overflow: hidden; background: transparent;
  position: relative;
  min-width: 0;
}

.chat-topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 16px 24px;
  background: var(--paper);
  border-bottom: 1px solid var(--rule);
  flex-shrink: 0;
}
.topbar-title {
  flex: 1; font-family: 'Fraunces', serif;
  font-size: 17px; font-weight: 500;
  color: var(--ink);
  letter-spacing: -0.01em;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.topbar-mode {
  display: inline-flex; align-items: center; gap: 8px;
  font-size: 12px; font-weight: 500;
  letter-spacing: 0.06em; text-transform: uppercase;
  color: var(--ink-3);
  padding: 5px 12px;
  border: 1px solid var(--rule);
  border-radius: 99px;
  background: var(--paper);
}
.topbar-mode-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--moss);
  box-shadow: 0 0 0 3px var(--moss-soft);
}
.topbar-mode[data-mode="rag"] .topbar-mode-dot { background: var(--rust); box-shadow: 0 0 0 3px var(--rust-soft); }

.topbar-docs {
  font-size: 11.5px; font-weight: 600; letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 5px 12px;
  border-radius: 99px;
  border: 1px solid var(--rule);
  background: var(--rust-soft);
  color: var(--rust);
}

.mob-only { display: none; }

/* Page grid: conversation + sources rail */
.page-grid {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--rail-w);
  gap: 0;
  overflow: hidden;
  position: relative;
}

/* ============================================================
   MESSAGES
   ============================================================ */
.messages {
  overflow-y: auto;
  padding: 32px 24px 24px;
  display: flex; flex-direction: column;
  border-right: 1px solid var(--rule-2);
  position: relative;
  z-index: 1;
}

/* Welcome */
.welcome {
  display: flex; flex-direction: column;
  align-items: flex-start;
  flex: 1; padding: 32px 8px; max-width: 720px;
  margin: 0 auto;
  width: 100%;
  animation: fadeIn 0.5s var(--ease);
}
.welcome-eyebrow {
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px; letter-spacing: 0.12em; text-transform: uppercase;
  color: var(--ink-3);
  margin-bottom: 28px;
}
.welcome-title {
  font-family: 'Fraunces', serif;
  font-size: clamp(36px, 5vw, 52px);
  font-weight: 400;
  line-height: 1.05;
  letter-spacing: -0.02em;
  color: var(--ink);
  margin-bottom: 20px;
  font-variation-settings: "opsz" 144, "SOFT" 50, "WONK" 1;
}
.welcome-title em {
  font-style: italic;
  color: var(--moss);
  font-weight: 400;
}
.welcome-sub {
  font-size: 16px; color: var(--ink-2);
  max-width: 560px;
  margin-bottom: 32px;
  line-height: 1.55;
}
.welcome-starters {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1px;
  background: var(--rule-2);
  border: 1px solid var(--rule-2);
  border-radius: var(--r-sm);
  width: 100%;
  max-width: 640px;
  margin-bottom: 32px;
  overflow: hidden;
}
.starter {
  display: flex; flex-direction: column; gap: 6px;
  padding: 16px 18px;
  background: var(--paper);
  text-align: left;
  border: none; cursor: pointer;
  font-family: 'Inter', sans-serif;
  transition: background var(--t);
}
.starter:hover { background: var(--paper-3); }
.starter-kicker {
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px; letter-spacing: 0.1em; text-transform: uppercase;
  color: var(--moss);
}
.starter-text {
  font-family: 'Fraunces', serif;
  font-size: 14.5px;
  color: var(--ink);
  line-height: 1.4;
}
.welcome-foot {
  font-size: 13px;
  color: var(--ink-3);
  font-style: italic;
  font-family: 'Fraunces', serif;
  max-width: 560px;
}
.welcome-foot-mark {
  font-style: normal;
  font-family: 'JetBrains Mono', monospace;
  color: var(--rust);
  font-size: 11.5px;
  letter-spacing: 0.04em;
}
.welcome-foot-marks { display: inline-flex; gap: 2px; vertical-align: baseline; position: relative; top: -2px; }

/* Message bubbles — notebook entry style */
.msg-wrapper {
  display: flex; gap: 14px;
  padding: 4px 0;
  max-width: var(--content-max);
  width: 100%; margin: 0 auto;
  animation: fadeInUp 0.3s var(--ease);
  position: relative;
}
.msg-wrapper.user { justify-content: flex-end; }
.msg-wrapper.assistant { justify-content: flex-start; }

.avatar {
  width: 32px; height: 32px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700; flex-shrink: 0;
  font-family: 'Fraunces', serif;
  margin-top: 4px;
}
.avatar.ai {
  background: var(--moss);
  color: var(--paper);
}

.bubble {
  max-width: 78%;
  padding: 14px 18px;
  border-radius: var(--r-sm);
  font-size: 15px; line-height: 1.65;
  position: relative;
}
.bubble.user {
  background: var(--paper-2);
  color: var(--ink);
  border-left: 2px solid var(--ink-2);
  border-top-left-radius: 0;
}
.bubble.assistant {
  background: transparent;
  color: var(--ink);
  border: none;
  padding-left: 0;
  padding-right: 0;
  max-width: 88%;
}
.bubble p { margin-bottom: 0.7em; }
.bubble p:last-child { margin-bottom: 0; }
.bubble pre {
  background: var(--paper-2);
  border: 1px solid var(--rule-2);
  border-radius: var(--r-sm);
  padding: 12px 14px; margin: 12px 0; overflow-x: auto;
  font-family: 'JetBrains Mono', monospace; font-size: 13px;
  color: var(--ink);
}
.bubble code { font-family: 'JetBrains Mono', monospace; font-size: 0.9em; }
.bubble a { color: var(--rust); text-decoration: underline; text-decoration-color: var(--rust-soft); text-underline-offset: 3px; }
.bubble a:hover { text-decoration-color: var(--rust); }

/* Margin-citation inline marker */
.cite {
  display: inline-flex;
  align-items: center; justify-content: center;
  min-width: 18px; height: 18px;
  padding: 0 5px;
  margin: 0 1px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 10.5px;
  font-weight: 700;
  line-height: 1;
  color: var(--rust);
  background: var(--rust-soft);
  border: 1px solid transparent;
  border-radius: 3px;
  vertical-align: baseline;
  position: relative; top: -3px;
  cursor: pointer;
  transition: background var(--t), color var(--t), border-color var(--t);
  user-select: none;
}
.cite:hover, .cite.is-active {
  background: var(--rust);
  color: var(--paper);
  border-color: var(--rust);
}

/* Thinking dots */
.typing-bubble { display: flex; align-items: center; gap: 5px; padding: 4px 0; }
.dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--moss);
  animation: bounce 1.2s ease infinite;
  opacity: 0.6;
}
.dot:nth-child(2) { animation-delay: 0.2s; }
.dot:nth-child(3) { animation-delay: 0.4s; }

/* ============================================================
   SOURCES RAIL (signature)
   ============================================================ */
.sources-rail {
  background: var(--paper-2);
  border-left: 1px solid var(--rule);
  display: flex; flex-direction: column;
  overflow: hidden;
  position: relative;
  z-index: 2;
}
.rail-eyebrow {
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px; letter-spacing: 0.14em; text-transform: uppercase;
  color: var(--ink-3);
  padding: 20px 20px 12px;
  border-bottom: 1px solid var(--rule-2);
  flex-shrink: 0;
}
.sources-list {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
  display: flex; flex-direction: column; gap: 14px;
}
.rail-empty {
  font-family: 'Fraunces', serif;
  font-style: italic;
  font-size: 13px;
  color: var(--ink-3);
  padding: 4px 0;
  line-height: 1.5;
}

.source-card {
  display: flex; flex-direction: column; gap: 4px;
  padding: 10px 12px;
  background: var(--paper);
  border: 1px solid var(--rule-2);
  border-radius: var(--r-sm);
  position: relative;
  transition: border-color var(--t), background var(--t);
}
.source-card.is-active {
  border-color: var(--rust);
  background: var(--rust-soft);
}
.source-card-head {
  display: flex; align-items: center; gap: 8px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  color: var(--rust);
  font-weight: 600;
  letter-spacing: 0.02em;
}
.source-card-num {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 18px; height: 18px;
  padding: 0 5px;
  background: var(--rust);
  color: var(--paper);
  border-radius: 3px;
  font-size: 10.5px;
  font-weight: 700;
}
.source-card-file {
  flex: 1;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  color: var(--ink-2);
  font-weight: 500;
}
.source-card-page {
  color: var(--ink-3);
  font-weight: 400;
}
.source-card-snippet {
  font-family: 'Fraunces', serif;
  font-style: italic;
  font-size: 12.5px;
  color: var(--ink-2);
  line-height: 1.45;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.source-card.is-empty .source-card-snippet { color: var(--ink-4); }

.rail-foot {
  flex-shrink: 0;
  border-top: 1px solid var(--rule-2);
  padding: 14px 20px 18px;
  display: flex; align-items: center; gap: 10px;
  color: var(--ink-3);
}
.rail-foot-line {
  flex: 1; height: 1px;
  background: repeating-linear-gradient(to right, var(--rule-2) 0 4px, transparent 4px 8px);
}
.rail-foot-label {
  font-family: 'Fraunces', serif;
  font-style: italic;
  font-size: 11.5px;
  letter-spacing: 0.02em;
}

/* ============================================================
   CONNECTOR LAYER (SVG dotted lines from citation -> card)
   ============================================================ */
.connector-layer {
  position: fixed; inset: 0;
  pointer-events: none;
  z-index: 5;
}
.connector-layer line {
  stroke: var(--rust);
  stroke-width: 1;
  stroke-dasharray: 2 3;
  opacity: 0.45;
  fill: none;
}

/* ============================================================
   TOAST
   ============================================================ */
.toast {
  position: absolute; bottom: 110px; left: 50%; transform: translateX(-50%);
  background: var(--paper);
  border: 1px solid var(--rule);
  padding: 10px 18px; border-radius: var(--r-sm);
  font-size: 13.5px; color: var(--ink);
  box-shadow: 0 12px 30px rgba(14,14,12,0.12);
  animation: fadeInUp 0.25s var(--ease);
  white-space: nowrap; z-index: 100;
  font-family: 'Fraunces', serif;
}
.toast.success { border-left: 3px solid var(--moss); }
.toast.error   { border-left: 3px solid var(--rust); color: var(--rust); }

/* ============================================================
   INPUT AREA
   ============================================================ */
.input-area {
  padding: 16px 24px 22px;
  background: var(--paper);
  border-top: 1px solid var(--rule);
  flex-shrink: 0;
}
.upload-indicator {
  display: flex; align-items: center; gap: 8px;
  background: var(--rust-soft);
  border-left: 2px solid var(--rust);
  border-radius: 0;
  padding: 8px 12px;
  font-size: 13px; color: var(--rust); margin-bottom: 12px;
  font-family: 'JetBrains Mono', monospace;
}
.upload-indicator svg { width: 14px; height: 14px; flex-shrink: 0; }
.cancel-upload {
  background: none; border: none; cursor: pointer;
  color: var(--rust); font-size: 18px; line-height: 1;
  padding: 0 2px; margin-left: auto;
  opacity: 0.6;
}
.cancel-upload:hover { opacity: 1; }

.input-row {
  display: flex; align-items: flex-end; gap: 12px;
  background: var(--paper-2);
  border: 1px solid var(--rule);
  border-radius: var(--r-sm);
  padding: 10px 12px 10px 14px;
  transition: border-color var(--t);
}
.input-row:focus-within {
  border-color: var(--ink);
}

.upload-btn {
  flex-shrink: 0; cursor: pointer; color: var(--ink-3);
  transition: color var(--t);
}
.upload-btn:hover { color: var(--moss); }

.textarea-wrapper { flex: 1; }
textarea#chat-input {
  width: 100%; background: none; border: none; outline: none;
  resize: none; font-family: 'Inter', sans-serif;
  font-size: 15px; color: var(--ink);
  max-height: 180px; min-height: 24px;
  line-height: 1.6; padding: 5px 0;
}
textarea#chat-input::placeholder { color: var(--ink-4); font-style: italic; }

.send-btn {
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--ink);
  color: var(--paper);
  border: none; cursor: pointer; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  transition: background var(--t), transform var(--t);
}
.send-btn:disabled { opacity: 0.3; cursor: not-allowed; }
.send-btn:not(:disabled):hover { background: var(--moss-2); transform: scale(1.05); }
.send-btn svg { width: 16px; height: 16px; }

.stop-btn { background: var(--rust); }
.stop-btn:not(:disabled):hover { background: var(--rust); opacity: 0.9; }
.stop-btn svg { width: 14px; height: 14px; }

.input-hint {
  font-size: 11.5px; color: var(--ink-3); text-align: center; margin-top: 10px;
  font-family: 'Fraunces', serif;
  font-style: italic;
}
.input-hint kbd {
  font-family: 'JetBrains Mono', monospace; font-size: 10px;
  font-style: normal;
  background: var(--paper-2);
  border: 1px solid var(--rule-2);
  border-radius: 3px; padding: 1px 5px;
  color: var(--ink-2);
}

/* ============================================================
   UPLOAD OVERLAY
   ============================================================ */
.upload-modal {
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: var(--r-md);
  padding: 36px 44px;
  display: flex; flex-direction: column; align-items: center; gap: 16px;
  text-align: center;
  box-shadow: 0 30px 80px rgba(14,14,12,0.35);
}
.upload-modal p { font-family: 'Fraunces', serif; font-size: 15px; color: var(--ink-2); }
.upload-spinner {
  width: 36px; height: 36px; border-radius: 50%;
  border: 2px solid var(--rule-2);
  border-top-color: var(--rust);
  animation: spin 0.8s linear infinite;
}
.dots::after {
  content: ''; animation: ellipsis 1.4s infinite;
}

/* ============================================================
   ANIMATIONS
   ============================================================ */
@keyframes spin    { to { transform: rotate(360deg); } }
@keyframes fadeIn  { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } }
@keyframes fadeInUp{ from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }
@keyframes bounce  { 0%,80%,100% { transform: translateY(0); opacity: 0.3; } 40% { transform: translateY(-5px); opacity: 1; } }
@keyframes ellipsis{ 0% { content: ''; } 33% { content: '.'; } 66% { content: '..'; } 100% { content: '...'; } }

/* ============================================================
   MARKDOWN + CODE BLOCKS
   ============================================================ */
.bubble .hljs { background: transparent !important; padding: 0 !important; font-size: 13px; color: var(--ink); }
.bubble code:not(.hljs) {
  background: var(--paper-2);
  color: var(--ink);
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 0.88em;
  border: 1px solid var(--rule-2);
}
.bubble h1, .bubble h2, .bubble h3, .bubble h4 {
  font-family: 'Fraunces', serif;
  font-weight: 500;
  letter-spacing: -0.01em;
  margin: 1em 0 0.5em;
  color: var(--ink);
}
.bubble h1 { font-size: 1.4em; }
.bubble h2 { font-size: 1.2em; }
.bubble h3 { font-size: 1.05em; }
.bubble ul, .bubble ol { padding-left: 22px; margin: 8px 0; }
.bubble li { margin: 4px 0; }
.bubble li::marker { color: var(--moss); }
.bubble table { border-collapse: collapse; width: 100%; margin: 12px 0; font-size: 13.5px; }
.bubble th, .bubble td { border: 1px solid var(--rule-2); padding: 7px 12px; text-align: left; }
.bubble th { background: var(--paper-2); font-weight: 600; }
.bubble blockquote {
  border-left: 2px solid var(--moss);
  padding: 4px 0 4px 14px;
  color: var(--ink-2);
  margin: 10px 0;
  font-family: 'Fraunces', serif;
  font-style: italic;
}
.bubble hr { border: none; border-top: 1px solid var(--rule-2); margin: 14px 0; }

/* Code block — copy button */
.bubble pre { position: relative; }
.copy-btn {
  position: absolute; top: 8px; right: 8px;
  background: var(--paper);
  border: 1px solid var(--rule-2);
  color: var(--ink-3);
  border-radius: 4px;
  font-size: 10.5px;
  font-family: 'JetBrains Mono', monospace;
  padding: 3px 8px;
  cursor: pointer;
  opacity: 0;
  transition: opacity var(--t), color var(--t), border-color var(--t);
}
.bubble pre:hover .copy-btn { opacity: 1; }
.copy-btn:hover { color: var(--ink); border-color: var(--ink); }
.copy-btn.copied { color: var(--moss); border-color: var(--moss); }

/* ============================================================
   EDIT MESSAGE
   ============================================================ */
.bubble { position: relative; }

.btn-edit {
  position: absolute; top: 4px; right: 4px;
  background: var(--paper);
  border: 1px solid var(--rule-2);
  color: var(--ink-3);
  border-radius: 4px;
  font-size: 11px;
  padding: 3px;
  cursor: pointer;
  opacity: 0;
  transition: opacity var(--t), color var(--t);
  display: flex; align-items: center; justify-content: center;
}
.bubble:hover .btn-edit { opacity: 1; }
.btn-edit:hover { color: var(--ink); }
.btn-edit svg { width: 13px; height: 13px; }

.edit-wrapper {
  display: flex; flex-direction: column; gap: 8px;
  width: 100%; min-width: 300px;
}
.edit-wrapper textarea {
  width: 100%;
  background: var(--paper-2);
  border: 1px solid var(--rule);
  border-radius: var(--r-sm);
  outline: none;
  resize: vertical;
  font-family: 'Inter', sans-serif;
  font-size: 14.5px;
  color: var(--ink);
  padding: 10px 12px;
  min-height: 80px;
}
.edit-wrapper textarea:focus { border-color: var(--moss); }
.edit-actions { display: flex; gap: 8px; justify-content: flex-end; }
.edit-actions button {
  padding: 6px 14px;
  border-radius: var(--r-sm);
  font-size: 12.5px;
  cursor: pointer;
  transition: all var(--t);
  font-weight: 500;
  font-family: 'Inter', sans-serif;
}
.btn-edit-cancel {
  background: transparent; border: 1px solid var(--rule-2); color: var(--ink-3);
}
.btn-edit-cancel:hover { background: var(--paper-2); color: var(--ink); }
.btn-edit-save {
  background: var(--ink); color: var(--paper); border: none;
}
.btn-edit-save:hover { background: var(--moss-2); }

/* ============================================================
   STREAMING CURSOR
   ============================================================ */
.streaming-cursor::after {
  content: '▌';
  display: inline-block;
  color: var(--moss);
  animation: cursorBlink 0.9s steps(2) infinite;
  margin-left: 1px;
  font-size: 0.9em;
}
@keyframes cursorBlink { 0%,100% { opacity: 1; } 50% { opacity: 0; } }

/* ============================================================
   RESPONSIVE
   ============================================================ */
@media (max-width: 1100px) {
  .page-grid { grid-template-columns: minmax(0, 1fr) 220px; }
  :root { --rail-w: 220px; }
}

@media (max-width: 860px) {
  :root { --rail-w: 0px; }
  .sources-rail { display: none; }
  .page-grid { grid-template-columns: minmax(0, 1fr); }
  .messages { border-right: none; }
  .sidebar {
    position: fixed; z-index: 50; height: 100%;
    transform: translateX(-100%);
    transition: transform 0.25s var(--ease);
    box-shadow: 6px 0 30px rgba(14,14,12,0.12);
  }
  .sidebar.mobile-open { transform: translateX(0); }
  .mob-only { display: flex !important; }
  #sidebar-toggle { display: none; }
  .bubble { max-width: 90%; }
  .welcome-starters { grid-template-columns: 1fr; }
  .modal-card { padding: 32px 24px 28px; }
}

/* ============================================================
   REDUCED MOTION
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  .welcome { animation: none; }
  .connector-layer line { opacity: 0.3; }
}

/* ============================================================
   DARK MODE
   ============================================================ */
[data-theme="dark"] {
  /* Surfaces — dark warm paper */
  --paper:    #181816;
  --paper-2:  #21201D;
  --paper-3:  #2B2A26;

  /* Ink — warm light */
  --ink:      #E3E0D8;
  --ink-2:    #A09B90;
  --ink-3:    #716C62;
  --ink-4:    #4A4740;

  /* Rules */
  --rule:     #E3E0D8;
  --rule-2:   #2E2D29;

  /* Accents — slightly brighter against dark bg */
  --moss:     #8BAA72;
  --moss-2:   #7A9663;
  --moss-soft:rgba(139,170,114,0.13);
  --rust:     #D4654B;
  --rust-soft:rgba(212,101,75,0.13);
}

[data-theme="dark"] body {
  background: var(--paper);
  background-image:
    repeating-linear-gradient(
      to bottom,
      transparent 0,
      transparent 31px,
      rgba(227,224,216,0.045) 31px,
      rgba(227,224,216,0.045) 32px
    ),
    url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='160' height='160'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0.05 0 0 0 0 0.05 0 0 0 0 0.04 0 0 0 0.05 0'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>");
}

[data-theme="dark"] .modal-card {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='160' height='160'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0.05 0 0 0 0 0.05 0 0 0 0 0.04 0 0 0 0.05 0'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>");
}

[data-theme="dark"] .modal-overlay {
  background: rgba(0,0,0,0.7);
}

[data-theme="dark"] .modal-card {
  box-shadow:
    0 30px 80px rgba(0,0,0,0.6),
    0 0 0 6px var(--paper),
    0 0 0 7px var(--rule);
}

[data-theme="dark"] .sidebar {
  border-right-color: var(--rule);
}

[data-theme="dark"] .chat-topbar {
  border-bottom-color: var(--rule);
}

[data-theme="dark"] .topbar-mode {
  border-color: var(--rule-2);
}

[data-theme="dark"] .input-area {
  border-top-color: var(--rule);
}

[data-theme="dark"] .input-row {
  border-color: var(--rule-2);
}
[data-theme="dark"] .input-row:focus-within {
  border-color: var(--ink-3);
}

[data-theme="dark"] .text-wrapper {
  border-color: var(--rule-2);
}

[data-theme="dark"] .messages {
  border-right-color: var(--rule-2);
}

[data-theme="dark"] .sources-rail {
  border-left-color: var(--rule);
}

[data-theme="dark"] ::-webkit-scrollbar-thumb {
  background: rgba(227,224,216,0.16);
}
[data-theme="dark"] ::-webkit-scrollbar-thumb:hover {
  background: rgba(227,224,216,0.3);
}

/* ── Theme toggle button ── */
.theme-toggle {
  display: flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; border-radius: 50%;
  background: none; border: 1px solid var(--rule-2); cursor: pointer;
  color: var(--ink-3); transition: all var(--t);
  flex-shrink: 0;
}
.theme-toggle:hover {
  background: var(--paper-3); color: var(--ink);
  border-color: var(--rule);
}
.theme-toggle svg {
  width: 16px; height: 16px;
}
.theme-toggle .sun-icon { display: block; }
.theme-toggle .moon-icon { display: none; }

[data-theme="dark"] .theme-toggle .sun-icon { display: none; }
[data-theme="dark"] .theme-toggle .moon-icon { display: block; }

/* ── Highlight.js code block adjustments for dark mode ── */
[data-theme="dark"] .bubble .hljs {
  color: var(--ink-2) !important;
}
[data-theme="dark"] .bubble .hljs-comment,
[data-theme="dark"] .bubble .hljs-quote {
  color: #6C6C6C !important;
}
[data-theme="dark"] .bubble .hljs-keyword,
[data-theme="dark"] .bubble .hljs-selector-tag,
[data-theme="dark"] .bubble .hljs-subst {
  color: #C9A76A !important;
}
[data-theme="dark"] .bubble .hljs-number,
[data-theme="dark"] .bubble .hljs-literal,
[data-theme="dark"] .bubble .hljs-variable,
[data-theme="dark"] .bubble .hljs-template-variable,
[data-theme="dark"] .bubble .hljs-tag .hljs-attr {
  color: #D19A66 !important;
}
[data-theme="dark"] .bubble .hljs-string,
[data-theme="dark"] .bubble .hljs-doctag {
  color: #98C379 !important;
}
[data-theme="dark"] .bubble .hljs-title,
[data-theme="dark"] .bubble .hljs-section,
[data-theme="dark"] .bubble .hljs-selector-id {
  color: #61AFEF !important;
}
[data-theme="dark"] .bubble .hljs-type,
[data-theme="dark"] .bubble .hljs-built_in {
  color: #56B6C2 !important;
}
[data-theme="dark"] .bubble .hljs-attr,
[data-theme="dark"] .bubble .hljs-attribute {
  color: #D19A66 !important;
}

[data-theme="dark"] .bubble pre {
  border-color: var(--rule-2);
}

/* ── Dark mode responsiveness ── */
@media (max-width: 860px) {
  [data-theme="dark"] .sidebar {
    box-shadow: 6px 0 30px rgba(0,0,0,0.4);
  }
}
