:root {
  --bg: #faf9f6;
  --surface: #ffffff;
  --border: #e7e3db;
  --text: #2a2822;
  --muted: #83796a;
  --accent: #c2661d;
  --accent-soft: #f3e3d3;
  --user-bubble: #f0ede5;
  --assistant-avatar: #c2661d;
  --code-bg: #f1ede3;
  --error-bg: #fbe9e7;
  --error-border: #f0bcb2;
  --error-text: #9a3412;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1b18;
    --surface: #242320;
    --border: #38352d;
    --text: #ece8e0;
    --muted: #a49a89;
    --accent: #e08a4a;
    --accent-soft: #3a2c1e;
    --user-bubble: #2d2b26;
    --assistant-avatar: #e08a4a;
    --code-bg: #2d2b26;
    --error-bg: #3a2420;
    --error-border: #5c3129;
    --error-text: #f3a58f;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

/* The page itself scrolls (full-width background); only the message
   column is capped in width and centered. This keeps the scrollbar at
   the true browser edge instead of hugging a narrow inner column. */
.app {
  width: 100%;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  width: 100%;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.topbar-inner {
  max-width: 760px;
  margin: 0 auto;
  padding: 16px 20px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 0.95rem;
}

.brand-mark {
  color: var(--accent);
  font-size: 1rem;
}

.chat {
  flex: 1;
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
  padding: 24px 20px 12px;
  display: flex;
  flex-direction: column;
  gap: 22px;
}

.empty-state {
  margin: auto;
  text-align: center;
  max-width: 420px;
  color: var(--muted);
}

.empty-mark {
  display: inline-block;
  color: var(--accent);
  font-size: 1.8rem;
  margin-bottom: 10px;
}

.empty-state h1 {
  font-size: 1.3rem;
  color: var(--text);
  margin: 0 0 8px;
}

.empty-state p {
  margin: 0;
  font-size: 0.92rem;
  line-height: 1.5;
}

.empty-state code {
  background: var(--code-bg);
  border-radius: 4px;
  padding: 1px 5px;
}

.chat.has-messages .empty-state {
  display: none;
}

/* --- messages --- */

.msg-row {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}

.msg-row.user {
  justify-content: flex-end;
}

.msg-row.user .bubble {
  background: var(--user-bubble);
  border-radius: 16px 16px 4px 16px;
  padding: 10px 15px;
  max-width: 78%;
  line-height: 1.5;
  font-size: 0.95rem;
  white-space: pre-wrap;
}

.avatar {
  flex: 0 0 auto;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--assistant-avatar);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  margin-top: 2px;
}

.content {
  flex: 1;
  min-width: 0;
}

.markdown {
  font-size: 0.95rem;
  line-height: 1.6;
}

.markdown p {
  margin: 0 0 10px;
}

.markdown p:last-child {
  margin-bottom: 0;
}

.markdown ul {
  margin: 0 0 10px;
  padding-left: 22px;
}

.markdown li {
  margin-bottom: 4px;
}

.markdown code {
  background: var(--code-bg);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 0.85em;
}

.markdown .citation {
  color: var(--accent);
  font-weight: 600;
  font-size: 0.85em;
}

.msg-row.error .content {
  background: var(--error-bg);
  border: 1px solid var(--error-border);
  color: var(--error-text);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 0.9rem;
}

.thinking-text {
  display: inline-block;
  font-size: 0.95rem;
  padding: 4px 0;
  background: linear-gradient(
    90deg,
    var(--muted) 0%,
    var(--muted) 35%,
    var(--text) 50%,
    var(--muted) 65%,
    var(--muted) 100%
  );
  background-size: 250% 100%;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  animation: shimmer 1.6s linear infinite;
}

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -50% 0; }
}

/* --- sources (collapsible) --- */

.sources {
  margin-top: 10px;
}

.sources-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: none;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 0.8rem;
  color: var(--muted);
  cursor: pointer;
  font-family: inherit;
}

.sources-toggle:hover {
  background: var(--surface);
  color: var(--text);
}

.sources-toggle .chevron {
  transition: transform 0.18s ease;
  flex: 0 0 auto;
}

.sources-toggle[aria-expanded="true"] .chevron {
  transform: rotate(90deg);
}

.sources-panel {
  overflow: hidden;
  max-height: 0;
  transition: max-height 0.22s ease;
}

.sources-panel.open {
  max-height: 800px;
}

.sources-list {
  margin: 10px 0 0;
  padding: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.source-item {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 12px;
  background: var(--surface);
}

.source-item .source-head {
  display: flex;
  gap: 8px;
  align-items: baseline;
  font-size: 0.8rem;
  font-weight: 600;
  margin-bottom: 3px;
}

.source-item .source-num {
  color: var(--accent);
}

.source-item .source-name {
  color: var(--text);
}

.source-item .source-meta {
  color: var(--muted);
  font-weight: 400;
}

.source-item .source-excerpt {
  margin: 0;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.5;
}

/* --- input --- */

.chat-form {
  position: sticky;
  bottom: 0;
  z-index: 10;
  width: 100%;
  background: var(--bg);
}

.form-inner {
  max-width: 760px;
  margin: 0 auto;
  padding: 12px 20px 20px;
}

.input-shell {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 20px;
  padding: 8px 8px 8px 16px;
}

.input-shell:focus-within {
  border-color: var(--accent);
}

#question {
  flex: 1;
  border: none;
  background: none;
  color: var(--text);
  font-size: 0.95rem;
  font-family: inherit;
  resize: none;
  max-height: 160px;
  padding: 6px 0;
  outline: none;
}

#send-btn {
  flex: 0 0 auto;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

#send-btn:disabled {
  opacity: 0.5;
  cursor: default;
}
