:root {
  --bg: #f7f8fa;
  --surface: #ffffff;
  --border: #dfe3e8;
  --text: #1c2530;
  --text-muted: #5b6772;
  --accent: #0094a1;
  --accent-hover: #245939;
  --accent-contrast: #ffffff;
  --error-bg: #fdecec;
  --error-border: #f3b4b4;
  --error-text: #8a2a2a;
  --code-bg: #f0f2f4;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 32, 0.06), 0 1px 8px rgba(16, 24, 32, 0.04);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12161b;
    --surface: #1a2028;
    --border: #2b333d;
    --text: #e7ebef;
    --text-muted: #98a3ae;
    --accent: #0094a1;
    --accent-hover: #245939;
    --accent-contrast: #0c1410;
    --error-bg: #2a1616;
    --error-border: #5c2a2a;
    --error-text: #f3a9a9;
    --code-bg: #10151a;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 1px 8px rgba(0, 0, 0, 0.3);
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

/* App-shell layout: header and footer stay put, only .response-area
   scrolls, so the ask box is always visible while browsing older answers. */
body {
  margin: 0;
  height: 100vh;
  height: 100dvh; /* accounts for mobile browser chrome; falls back above */
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  line-height: 1.5;
}

a { color: var(--accent); }

.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.7rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.brand { display: flex; align-items: center; text-decoration: none; }
.logo { display: block; height: 32px; width: auto; }

@media (prefers-color-scheme: dark) {
  /* The logo artwork assumes a light backdrop - give it one so it stays
     legible against the dark surface color instead of blending in. */
  .logo { background: #f5f7f9; border-radius: 6px; padding: 3px 8px; }
}

.topbar-links { display: flex; align-items: center; gap: 1.25rem; font-size: 0.9rem; }
.topbar-links a { color: var(--text-muted); text-decoration: none; }
.topbar-links a:hover { color: var(--accent); }

.link-button {
  font: inherit;
  font-size: 0.9rem;
  color: var(--text-muted);
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
}
.link-button:hover { color: var(--accent); }
.link-button.small { font-size: 0.78rem; text-decoration: underline; }

.page {
  flex: 1 1 auto;
  min-height: 0; /* let .response-area's overflow apply within this column */
  display: flex;
  flex-direction: column;
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
  padding: 1.5rem 1.5rem 0;
  overflow: hidden;
}

.intro { flex: 0 0 auto; text-align: center; margin-bottom: 1.5rem; }
.intro h1 { font-size: 2rem; margin: 0 0 0.5rem; letter-spacing: -0.01em; }
.subtitle { color: var(--text-muted); font-size: 1.05rem; margin: 0; }

.composer {
  flex: 0 0 auto; /* stays visible above the scrolling response list */
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow);
  margin-bottom: 1.25rem;
}

#ask-form { display: flex; }

/* One unified pill-shaped control - textarea and send button share a single
   border/background instead of sitting in two separate boxes. */
.input-group {
  flex: 1;
  display: flex;
  align-items: flex-end;
  gap: 0.4rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 0.35rem 0.35rem 0.35rem 1rem;
}
.input-group:focus-within { outline: 2px solid var(--accent); outline-offset: 1px; }

#question {
  flex: 1;
  resize: none;
  overflow-y: auto;
  max-height: 9rem;
  min-height: 1.6rem;
  padding: 0.55rem 0;
  border: none;
  background: transparent;
  color: var(--text);
  font: inherit;
}
#question:focus { outline: none; }

#ask-button {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.55rem 1.1rem;
  border: none;
  border-radius: 18px;
  background: var(--accent);
  color: var(--accent-contrast);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
#ask-button:hover { background: var(--accent-hover); }
#ask-button:disabled { opacity: 0.6; cursor: default; }
.send-icon { flex: 0 0 auto; }

.examples {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.9rem;
}

.composer-footer {
  display: flex;
  align-items: center;
  /* flex-end (not space-between): when model-select is hidden (see
     modelSelect.hidden in app.js - a single configured provider, or none),
     "Clear chat" must stay flush right rather than jumping to the left edge
     with nothing to balance against. model-select's own margin-right: auto
     below is what actually pins it to the left when visible. */
  justify-content: flex-end;
  gap: 0.9rem;
  margin-top: 0.6rem;
}

/* Same text size as "Clear chat" (.link-button.small) so the two read as one
   small utility row, not two different UI weights. */
.model-select {
  font: inherit;
  font-size: 0.78rem;
  color: var(--text-muted);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.25rem 1.6rem 0.25rem 0.7rem;
  cursor: pointer;
  margin-right: auto; /* pins it to the left; "Clear chat" stays right (see .composer-footer) */
}
.model-select:hover { color: var(--accent); border-color: var(--accent); }
.model-select:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.example-chip {
  font: inherit;
  font-size: 0.70rem;
  padding: 0.4rem 0.75rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-muted);
  cursor: pointer;
}
.example-chip:hover { border-color: var(--accent); color: var(--accent); }

.response-area {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding-bottom: 1.5rem;
}
.placeholder { color: var(--text-muted); text-align: center; }

.turn {
  display: flex;
  flex-direction: column;
  margin-bottom: 1.25rem;
}

/* Your question - a compact, right-aligned chat bubble, like a sent message. */
.question-bubble {
  align-self: flex-end;
  max-width: 80%;
  background: var(--accent);
  color: var(--accent-contrast);
  border-radius: 16px 16px 4px 16px;
  padding: 0.55rem 0.9rem;
  margin-bottom: 0.5rem;
  font-weight: 500;
  white-space: pre-wrap;
  word-break: break-word;
}

/* The assistant's answer - a full-width card, left-aligned underneath. */
.answer-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1.25rem;
  box-shadow: var(--shadow);
}

.answer-text p { margin: 0 0 0.65em; }
.answer-text p:last-child { margin-bottom: 0; }
.answer-text ul,
.answer-text ol { margin: 0 0 0.65em; padding-left: 1.3em; }
.answer-text li { margin-bottom: 0.2em; }
.answer-text h1, .answer-text h2, .answer-text h3,
.answer-text h4, .answer-text h5, .answer-text h6 {
  margin: 0 0 0.4em;
  font-size: 1.05em;
}
.answer-text code {
  background: var(--code-bg);
  border-radius: 4px;
  padding: 0.1em 0.35em;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.9em;
}
.answer-text pre {
  background: var(--code-bg);
  border-radius: 8px;
  padding: 0.7em 0.9em;
  overflow-x: auto;
  margin: 0 0 0.65em;
}
.answer-text pre code { background: none; padding: 0; }
.answer-text a { color: var(--accent); }
.answer-text del { opacity: 0.65; }
.answer-text blockquote {
  margin: 0 0 0.65em;
  padding: 0.1em 1em;
  border-left: 3px solid var(--border);
  color: var(--text-muted);
}
.answer-text blockquote p:last-child { margin-bottom: 0; }
.answer-text hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 0.9em 0;
}
.answer-text > *:last-child { margin-bottom: 0; }

.answer-text .table-wrap {
  overflow-x: auto;
  margin: 0 0 0.65em;
  border: 1px solid var(--border);
  border-radius: 8px;
}
.answer-text table {
  border-collapse: collapse;
  width: 100%;
  font-size: 0.92em;
  white-space: nowrap;
}
.answer-text th, .answer-text td {
  padding: 0.4em 0.7em;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
.answer-text thead th {
  background: var(--code-bg);
  font-weight: 600;
  border-bottom: 1px solid var(--border);
}
.answer-text tbody tr:last-child td { border-bottom: none; }

.loading {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  color: var(--text-muted);
  padding: 0.5rem 0.1rem;
}

.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.error-box {
  background: var(--error-bg);
  border: 1px solid var(--error-border);
  color: var(--error-text);
  border-radius: var(--radius);
  padding: 0.9rem 1.1rem;
  margin-bottom: 1rem;
}

.cards { display: grid; gap: 0.7rem; margin-top: 0.9rem; }

.card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem 0.9rem;
  background: var(--bg);
}

.card-title { font-weight: 600; margin-bottom: 0.3rem; }
.card-title .matrix-id { color: var(--text-muted); font-weight: 400; font-size: 0.9em; }

.card-fields {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 0.2rem 0.8rem;
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
}
.card-fields b { color: var(--text); font-weight: 500; }

.card-actions { display: flex; gap: 0.6rem; flex-wrap: wrap; }
.card-actions a {
  font-size: 0.82rem;
  text-decoration: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.25rem 0.6rem;
  color: var(--text);
}
.card-actions a:hover { border-color: var(--accent); color: var(--accent); }

.more-results { font-size: 0.85rem; color: var(--text-muted); margin-top: 0.3rem; }

.page-footer {
  flex: 0 0 auto; /* pinned to the bottom of the viewport - never scrolls away */
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.page-footer p {
  width: 100%;
  margin: 0;
  padding: 0.55rem 1.5rem;
  color: var(--text-muted);
  font-size: 0.8rem;
  text-align: center;
}
.page-footer p + p { padding-top: 0; }
.page-footer a { color: var(--text-muted); text-decoration: underline; }
.page-footer a:hover { color: var(--accent); }

@media (max-width: 520px) {
  /* Collapse the send button to icon-only so the pill doesn't outgrow narrow screens. */
  #ask-button span { display: none; }
  #ask-button { padding: 0.6rem; border-radius: 50%; }
}
