:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --surface: #f0f2f5;
  --border: #dfe2e8;
  --text: #1a1d23;
  --muted: #667085;
  --accent: #3b6fe0;
  --accent-text: #ffffff;
  --good: #1f8a5a;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.disclaimer {
  background: #fdecec;
  color: #7a1f1f;
  border-bottom: 2px solid #d33;
  padding: 0.85rem 1.5rem;
  text-align: center;
  font-size: 0.92rem;
  line-height: 1.5;
}

.disclaimer strong { color: #b02a2a; }

header {
  padding: 2rem 1.5rem 1rem;
  max-width: 900px;
  margin: 0 auto;
}

h1 { margin: 0 0 0.25rem; font-size: 1.6rem; }
.subtitle { color: var(--muted); margin: 0; }

.back-link { margin: 0 0 0.75rem; }
.back-link a, .explore-link a { color: var(--accent); text-decoration: none; font-size: 0.9rem; }
.back-link a:hover, .explore-link a:hover { text-decoration: underline; }
.explore-link { text-align: center; margin-top: 0.5rem; }

main {
  max-width: 900px;
  margin: 0 auto;
  padding: 0 1.5rem 3rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.step {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.25rem 1.5rem;
  box-shadow: 0 1px 2px rgba(16, 24, 40, 0.04);
}

.step h2 { margin-top: 0; font-size: 1.1rem; }
.step p { color: var(--muted); font-size: 0.92rem; }

code {
  background: var(--surface);
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
  font-size: 0.85em;
}

textarea, input[type="text"] {
  width: 100%;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.6rem 0.75rem;
  font-family: inherit;
  font-size: 0.92rem;
  resize: vertical;
}

button {
  margin-top: 0.75rem;
  background: var(--accent);
  color: var(--accent-text);
  border: none;
  border-radius: 6px;
  padding: 0.55rem 1.1rem;
  font-size: 0.92rem;
  font-weight: 600;
  cursor: pointer;
}

button:disabled {
  background: var(--border);
  color: var(--muted);
  cursor: not-allowed;
}

button:not(:disabled):hover { filter: brightness(1.1); }

.output {
  margin-top: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.65rem 0.85rem;
  font-size: 0.88rem;
}

.card .meta {
  color: var(--muted);
  font-size: 0.8rem;
  margin-bottom: 0.3rem;
}

.card .distance { color: var(--good); font-weight: 600; }

.result-group h3 {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
  margin: 0.5rem 0 0.4rem;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 0.75rem;
}

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.6rem 0.75rem;
}

.stat .value { font-size: 1.2rem; font-weight: 700; }
.stat .label { color: var(--muted); font-size: 0.78rem; }

.vec-preview {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.78rem;
  color: var(--muted);
  word-break: break-all;
}

.error { color: #c62828; }

.threshold-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-top: 0.75rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.threshold-row input[type="number"] {
  width: 5.5rem;
  flex: none;
}

.help-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.1rem;
  height: 1.1rem;
  border-radius: 50%;
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.72rem;
  cursor: help;
  user-select: none;
}

.help-icon:hover,
.help-icon:focus {
  border-color: var(--accent);
  color: var(--accent);
  outline: none;
}

.corpus-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.corpus-list a {
  display: block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.7rem 0.9rem;
  color: var(--text);
  text-decoration: none;
  font-size: 0.92rem;
}

.corpus-list a:hover { border-color: var(--accent); }
.corpus-list .filename { color: var(--muted); font-size: 0.8rem; }

.doc-content {
  font-size: 0.92rem;
  line-height: 1.6;
}

/* Rendered Markdown (see renderMarkdown() in common.js) inside a chat
   bubble or the corpus document viewer — both were plain textContent
   before and relied on white-space: pre-wrap for line breaks; now real
   <p>/<ul>/<h*> tags carry that spacing instead, so these zero out the
   browser's default block margins in favor of ones that fit each context. */
.chat-msg p,
.doc-content p {
  margin: 0 0 0.6em;
}

.chat-msg p:last-child,
.doc-content p:last-child {
  margin-bottom: 0;
}

.chat-msg ul,
.chat-msg ol,
.doc-content ul,
.doc-content ol {
  margin: 0 0 0.6em;
  padding-left: 1.3em;
}

.chat-msg li,
.doc-content li {
  margin-bottom: 0.25em;
}

.chat-msg h2, .chat-msg h3, .chat-msg h4, .chat-msg h5, .chat-msg h6,
.doc-content h2, .doc-content h3, .doc-content h4, .doc-content h5, .doc-content h6 {
  margin: 0.8em 0 0.4em;
  line-height: 1.3;
}

.chat-msg h2:first-child, .chat-msg h3:first-child,
.doc-content h2:first-child, .doc-content h3:first-child {
  margin-top: 0;
}

.chat-msg code,
.doc-content code {
  background: var(--surface);
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
  font-size: 0.85em;
}

/* --- Chat page --- */

.chat-toolbar {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 0.75rem;
}

.btn-secondary {
  margin-top: 0;
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
}

.btn-secondary:hover:not(:disabled) {
  filter: none;
  color: var(--text);
  border-color: var(--accent);
}

.chat-log {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  min-height: 200px;
  max-height: 60vh;
  overflow-y: auto;
  margin-bottom: 1rem;
}

.chat-msg {
  max-width: 80%;
  padding: 0.65rem 0.9rem;
  border-radius: 10px;
  font-size: 0.92rem;
  line-height: 1.5;
  white-space: pre-wrap;
}

.chat-msg.user {
  align-self: flex-end;
  background: var(--accent);
  color: var(--accent-text);
}

.chat-msg.assistant {
  align-self: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
}

.chat-msg.assistant.no-context { color: var(--muted); font-style: italic; }

.chat-msg.pending { color: var(--muted); font-style: italic; }

.sources {
  align-self: flex-start;
  max-width: 80%;
  font-size: 0.76rem;
  color: var(--muted);
}

.sources-label { margin-right: 0.2rem; }

.source-tag {
  display: inline-block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.05rem 0.4rem;
  margin: 0.1rem 0;
  cursor: help;
}

.source-tag:hover,
.source-tag:focus {
  border-color: var(--accent);
  outline: none;
}

/* Follows the cursor over a .source-tag — see renderSources() in
   common.js. Fixed positioning so it's never clipped by .chat-log's
   overflow-y: auto. */
.source-tooltip {
  position: fixed;
  z-index: 1000;
  max-width: 360px;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.6rem 0.75rem;
  font-size: 0.8rem;
  line-height: 1.45;
  white-space: pre-wrap;
  box-shadow: 0 6px 20px rgba(16, 24, 40, 0.15);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.08s ease;
}

.source-tooltip.visible { opacity: 1; }

.chat-input-row {
  display: flex;
  gap: 0.6rem;
}

.chat-input-row input[type="text"] { flex: 1; }
.chat-input-row button { margin-top: 0; flex: none; }

.chat-nav {
  display: flex;
  gap: 1.25rem;
  justify-content: center;
  margin-top: 0.5rem;
}
