:root {
  color-scheme: light dark;

  --bg: #fbfaf9;
  --panel: #ffffff;
  --ink: #1a1a19;
  --ink-soft: #6b6b68;
  --line: #e6e3df;
  --accent: #2563eb;
  --accent-ink: #ffffff;
  --bubble-assistant: #f2f1ee;
  --code-bg: rgba(20, 20, 19, 0.06);
  --shadow: 0 1px 2px rgba(20, 20, 19, 0.05), 0 8px 24px rgba(20, 20, 19, 0.06);
  --radius: 1.1rem;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131313;
    --panel: #1b1b1a;
    --ink: #ececea;
    --ink-soft: #9a9a95;
    --line: #2e2e2c;
    --accent: #3b82f6;
    --bubble-assistant: #262624;
    --code-bg: rgba(255, 255, 255, 0.08);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.3);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 15.5px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

.shell {
  max-width: 780px;
  margin: 0 auto;
  padding: 2rem 1rem 1.25rem;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

/* ---------- header ---------- */

header {
  display: flex;
  align-items: center;
  gap: 0.85rem;
}

header h1 {
  margin: 0;
  font-size: 1.3rem;
  letter-spacing: -0.02em;
}

header p {
  margin: 0.15rem 0 0;
  color: var(--ink-soft);
  font-size: 0.9rem;
}

.mark {
  flex: none;
  width: 2.4rem;
  height: 2.4rem;
  display: grid;
  place-items: center;
  border-radius: 0.75rem;
  background: linear-gradient(145deg, var(--accent), #7c3aed);
  color: #fff;
  font-weight: 650;
  font-size: 1.1rem;
  box-shadow: var(--shadow);
}

/* ---------- chat ---------- */

#chat {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  overflow-y: auto;
  padding: 0.25rem;
  scroll-behavior: smooth;
}

.turn {
  display: contents;
}

.message {
  max-width: 82%;
  padding: 0.7rem 1rem;
  border-radius: var(--radius);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  animation: rise 0.28s cubic-bezier(0.2, 0.8, 0.3, 1) both;
}

/* Rendered markdown supplies its own block spacing, so pre-wrap would double
   every gap. Plain-text bubbles (user, and the optimistic copy) keep it. */
.message.markdown {
  white-space: normal;
  max-width: 88%;
}

.message.user {
  align-self: flex-end;
  background: var(--accent);
  color: var(--accent-ink);
  border-bottom-right-radius: 0.35rem;
}

.message.assistant {
  align-self: flex-start;
  background: var(--bubble-assistant);
  color: var(--ink);
  border-bottom-left-radius: 0.35rem;
  box-shadow: var(--shadow);
}

/* The in-progress assistant reply: plain text (pre-wrap, from .message)
   filled in token by token, with a blinking cursor at the write head. */
.message.assistant.streaming .status {
  color: var(--ink-soft);
  font-style: italic;
}

.cursor {
  display: inline-block;
  width: 0.5em;
  height: 1em;
  margin-left: 1px;
  vertical-align: text-bottom;
  background: currentColor;
  opacity: 0.6;
  animation: blink-cursor 1s step-start infinite;
}

@keyframes blink-cursor {
  50% { opacity: 0; }
}

.message.assistant.error {
  color: var(--ink-soft);
  font-style: italic;
}

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

/* ---------- rendered markdown ---------- */

.markdown > :first-child { margin-top: 0; }
.markdown > :last-child { margin-bottom: 0; }

.markdown p,
.markdown ul,
.markdown ol,
.markdown blockquote,
.markdown table,
.markdown pre {
  margin: 0.6rem 0;
}

.markdown h1,
.markdown h2,
.markdown h3,
.markdown h4 {
  margin: 1rem 0 0.5rem;
  font-size: 1rem;
  line-height: 1.3;
  letter-spacing: -0.01em;
}

.markdown h1 { font-size: 1.15rem; }
.markdown h2 { font-size: 1.07rem; }

.markdown ul,
.markdown ol { padding-left: 1.25rem; }
.markdown li { margin: 0.2rem 0; }
.markdown li::marker { color: var(--ink-soft); }

.markdown a {
  color: var(--accent);
  text-decoration-color: color-mix(in srgb, var(--accent) 40%, transparent);
  text-underline-offset: 2px;
}

.markdown blockquote {
  padding-left: 0.8rem;
  border-left: 2px solid var(--line);
  color: var(--ink-soft);
}

.markdown code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.87em;
  padding: 0.1rem 0.32rem;
  border-radius: 0.35rem;
  background: var(--code-bg);
}

.markdown pre {
  padding: 0.75rem 0.9rem;
  border-radius: 0.6rem;
  background: var(--code-bg);
  overflow-x: auto;
}

.markdown pre code {
  padding: 0;
  background: none;
  font-size: 0.85em;
}

.markdown hr {
  border: none;
  border-top: 1px solid var(--line);
  margin: 0.9rem 0;
}

/* Tables can exceed the bubble; scroll them rather than the page. */
.markdown table {
  display: block;
  width: max-content;
  max-width: 100%;
  overflow-x: auto;
  border-collapse: collapse;
  font-size: 0.9rem;
  font-variant-numeric: tabular-nums;
}

.markdown th,
.markdown td {
  padding: 0.4rem 0.7rem;
  border: 1px solid var(--line);
  text-align: left;
}

.markdown thead th {
  background: var(--code-bg);
  font-weight: 600;
}

.markdown .chart {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 0.6rem 0;
  border-radius: 0.6rem;
  border: 1px solid var(--line);
}

/* Citation markers ([1, 13]) — the provenance signal in a RAG answer. */
.citation {
  padding: 0.05em 0.3em;
  border-radius: 0.35rem;
  background: color-mix(in srgb, var(--accent) 14%, transparent);
  color: var(--accent);
  font-size: 0.82em;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* ---------- empty state ---------- */

#empty-state {
  margin: auto 0;
  text-align: center;
  animation: rise 0.4s ease both;
}

#empty-state .lead {
  color: var(--ink-soft);
  margin: 0 0 1rem;
}

.suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;
}

.suggestion {
  padding: 0.45rem 0.85rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink-soft);
  font: inherit;
  font-size: 0.87rem;
  cursor: pointer;
  transition: transform 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.suggestion:hover {
  transform: translateY(-1px);
  color: var(--ink);
  border-color: var(--accent);
}

/* ---------- composer ---------- */

#chat-form {
  position: sticky;
  bottom: 0;
  display: flex;
  gap: 0.5rem;
  align-items: center;
  padding: 0.4rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--panel);
  box-shadow: var(--shadow);
  transition: border-color 0.18s ease;
}

#chat-form:focus-within {
  border-color: var(--accent);
}

#chat-form input[type="text"] {
  flex: 1;
  padding: 0.55rem 0.9rem;
  border: none;
  background: none;
  color: inherit;
  font: inherit;
  outline: none;
}

#chat-form button {
  flex: none;
  width: 2.4rem;
  height: 2.4rem;
  display: grid;
  place-items: center;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-ink);
  cursor: pointer;
  transition: transform 0.15s ease, opacity 0.15s ease;
}

#chat-form button:hover:not(:disabled) { transform: scale(1.06); }
#chat-form button:active:not(:disabled) { transform: scale(0.94); }
#chat-form button:disabled { opacity: 0.45; cursor: default; }

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
  #chat { scroll-behavior: auto; }
}

/* ---------- documents page ---------- */

.nav {
  margin-left: auto;
  flex: none;
}

.nav a {
  color: var(--accent);
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 550;
}

.nav a:hover { text-decoration: underline; }

.alert {
  padding: 0.65rem 0.95rem;
  border-radius: 0.7rem;
  border: 1px solid var(--line);
  background: var(--panel);
  font-size: 0.9rem;
}

.alert code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }

.alert.ok {
  border-color: color-mix(in srgb, #22c55e 45%, transparent);
  background: color-mix(in srgb, #22c55e 10%, var(--panel));
}

.alert.err {
  border-color: color-mix(in srgb, #ef4444 45%, transparent);
  background: color-mix(in srgb, #ef4444 10%, var(--panel));
}

.empty { color: var(--ink-soft); }

.docs {
  width: 100%;
  border-collapse: collapse;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

.docs th,
.docs td {
  padding: 0.55rem 0.85rem;
  text-align: left;
  border-bottom: 1px solid var(--line);
  font-size: 0.9rem;
  font-variant-numeric: tabular-nums;
}

.docs thead th { background: var(--code-bg); font-weight: 600; }
.docs tbody tr:last-child td { border-bottom: none; }

.doc-name {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.83em;
  overflow-wrap: anywhere;
}

.upload {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  padding: 1rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
}

.upload label { font-size: 0.9rem; font-weight: 600; }

.upload input[type="file"],
.upload input[type="password"] {
  font: inherit;
  padding: 0.5rem 0.7rem;
  border: 1px solid var(--line);
  border-radius: 0.6rem;
  background: var(--bg);
  color: inherit;
}

.upload button {
  align-self: flex-start;
  padding: 0.5rem 1.1rem;
  border: none;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-ink);
  font: inherit;
  cursor: pointer;
}

.upload button:hover { filter: brightness(1.05); }
