:root {
  --bg: #f7f7f5;
  --fg: #1c1c1a;
  --muted: #6b6b66;
  --line: #e2e1dc;
  --user: #1c1c1a;
  --user-fg: #f7f7f5;
  --assistant: #ffffff;
  --error: #8a2a2a;
  --accent: #2a5d8a;
  --accent-soft: #eef2f6;
  --sans: "Geist", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
    Arial, sans-serif;
  /* Geist is now the lead face everywhere. The "display" alias is kept so the few
     places that wanted a typographic accent (wordmark, h1) can carry a touch more
     weight/tracking without reintroducing the serif. */
  --display: "Geist", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
    Arial, sans-serif;
  /* Reading column for the conversation: comfortable line length on wide screens,
     full-bleed on mobile. Tables scroll inside their own bubble, not the page. */
  --col: 860px;
  --gutter: 24px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 16px/1.55 var(--sans);
  color: var(--fg);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  display: flex;
  flex-direction: column;
  height: 100vh;
  /* dvh tracks the visible viewport as mobile browser chrome shows/hides, so the
     composer stays pinned above the keyboard/address bar instead of off-screen. */
  height: 100dvh;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  padding: 18px max(var(--gutter), (100% - var(--col)) / 2) 14px;
  border-top: 3px solid var(--accent);
  border-bottom: 1px solid var(--line);
  background: #fff;
}
header h1 {
  margin: 0;
  font-family: var(--display);
  font-size: 20px;
  font-weight: 600;
  letter-spacing: -0.2px;
}
header .sub { margin: 5px 0 0; color: var(--muted); font-size: 13px; line-height: 1.5; max-width: 64ch; }
.head-actions { display: flex; gap: 8px; flex-shrink: 0; }

button.ghost {
  padding: 7px 13px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--fg);
  font-size: 13px;
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease;
}
button.ghost:hover { background: var(--bg); border-color: #cfcec8; }

#messages {
  flex: 1;
  overflow-y: auto;
  /* Centered reading column: the horizontal padding grows on wide screens so the
     conversation never runs edge-to-edge, while the scrollbar stays at the viewport
     edge. On screens narrower than the column it collapses to a flat --gutter. */
  padding: 24px max(var(--gutter), (100% - var(--col)) / 2) 28px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* Bubbles fill the reading column; user messages cap a little tighter so a short
   question doesn't sprawl. min-width:0 lets a wide table's scroll container be bounded
   by the bubble instead of forcing the whole column wider. */
.msg { display: flex; flex-direction: column; min-width: 0; max-width: 100%; }
.msg.user { max-width: 80%; }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant, .msg.pending, .msg.error { align-self: flex-start; }

.bubble {
  min-width: 0;
  max-width: 100%;
  padding: 12px 16px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--assistant);
  box-shadow: 0 1px 2px rgba(28, 28, 26, 0.04);
}
.msg.assistant .bubble { border-top-left-radius: 4px; }
.msg.user .bubble { border-top-right-radius: 4px; }
.msg.user .bubble { background: var(--user); color: var(--user-fg); border-color: var(--user); }
.msg.pending .bubble { color: var(--muted); font-style: italic; }
.msg.error .bubble { color: var(--error); border-color: #e3c7c7; background: #fbf4f4; }

.sources {
  margin-top: 6px;
  padding-left: 2px;
  font-size: 12px;
  color: var(--accent);
  letter-spacing: 0.2px;
}

/* Rendered Markdown inside assistant bubbles */
.bubble > :first-child { margin-top: 0; }
.bubble > :last-child { margin-bottom: 0; }
.bubble p { margin: 8px 0; }
.bubble h2, .bubble h3, .bubble h4, .bubble h5, .bubble h6 {
  margin: 14px 0 5px;
  font-family: var(--display);
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.1px;
}
.bubble ul, .bubble ol { margin: 6px 0; padding-left: 20px; }
.bubble li { margin: 2px 0; }
.bubble code {
  background: var(--bg);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 12.5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.bubble table {
  display: block;
  overflow-x: auto;
  border-collapse: collapse;
  margin: 10px 0;
  font-size: 13px;
  border: 1px solid var(--line);
  border-radius: 8px;
}
.bubble th, .bubble td {
  border-bottom: 1px solid var(--line);
  padding: 7px 11px;
  text-align: left;
  vertical-align: top;
  white-space: nowrap;
}
.bubble td + td, .bubble th + th { border-left: 1px solid var(--line); }
.bubble tr:last-child td { border-bottom: none; }
.bubble th {
  background: var(--bg);
  font-weight: 600;
  border-bottom: 1.5px solid #d6d5cf;
  font-size: 12px;
  letter-spacing: 0.3px;
  text-transform: uppercase;
  color: #4a4a45;
}
.bubble tbody tr:nth-child(even) td { background: #fbfbfa; }

#composer {
  display: flex;
  gap: 10px;
  padding: 14px max(var(--gutter), (100% - var(--col)) / 2);
  border-top: 1px solid var(--line);
  background: #fff;
}
#q {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-family: var(--sans);
  font-size: 16px;
  background: var(--bg);
  transition: border-color 0.12s ease, background 0.12s ease;
}
#q:focus {
  outline: none;
  border-color: var(--accent);
  background: #fff;
  box-shadow: 0 0 0 3px var(--accent-soft);
}
#send {
  flex-shrink: 0;
  min-height: 44px;
  padding: 12px 22px;
  border: none;
  border-radius: 10px;
  background: var(--user);
  color: var(--user-fg);
  font-family: var(--sans);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.12s ease;
}
#send:hover { background: #000; }
#send:disabled { opacity: 0.45; cursor: default; }

footer.brand {
  padding: 11px max(var(--gutter), (100% - var(--col)) / 2);
  border-top: 1px solid var(--line);
  background: #fff;
  color: var(--muted);
  font-size: 12px;
  letter-spacing: 0.2px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}
footer.brand .wordmark { font-family: var(--display); font-weight: 600; color: var(--fg); }
footer.brand a { color: var(--accent); text-decoration: none; }
footer.brand a:hover { text-decoration: underline; }

/* Login page */
.login-wrap {
  /* Center the card both axes within the flex column (.centered). */
  margin: auto;
  width: 100%;
  max-width: 380px;
  padding: 32px 30px 28px;
  background: #fff;
  border: 1px solid var(--line);
  border-top: 3px solid var(--accent);
  border-radius: 14px;
  box-shadow: 0 6px 24px rgba(28, 28, 26, 0.06);
}
.login-wrap h1 {
  font-family: var(--display);
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.3px;
  margin: 0 0 5px;
}
.login-wrap p { color: var(--muted); font-size: 13px; margin: 0 0 22px; }
.login-wrap label {
  display: block;
  font-size: 12px;
  margin: 0 0 5px;
  color: var(--muted);
  letter-spacing: 0.3px;
  text-transform: uppercase;
}
.login-wrap input {
  width: 100%;
  min-height: 44px;
  padding: 11px 13px;
  margin-bottom: 16px;
  border: 1px solid var(--line);
  border-radius: 9px;
  font-family: var(--sans);
  font-size: 16px;
  background: var(--bg);
  transition: border-color 0.12s ease, background 0.12s ease;
}
.login-wrap input:focus {
  outline: none;
  border-color: var(--accent);
  background: #fff;
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.login-wrap button {
  width: 100%;
  min-height: 44px;
  padding: 12px;
  margin-top: 4px;
  border: none;
  border-radius: 9px;
  background: var(--user);
  color: var(--user-fg);
  font-family: var(--sans);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.12s ease;
}
.login-wrap button:hover { background: #000; }
.login-error { color: var(--error); font-size: 13px; min-height: 18px; margin-top: 8px; }
.centered { display: flex; flex-direction: column; min-height: 100vh; min-height: 100dvh; padding: 24px; }

/* ----------------------------------------------------------------------------
   Mobile. Designed for a 375px phone first: the header stops competing for one
   row, touch targets clear 44px, tables keep scrolling inside their bubble, and
   the column goes full-bleed since there's no room for gutters.
---------------------------------------------------------------------------- */
@media (max-width: 640px) {
  :root { --gutter: 16px; }

  /* Header: title on top, the two ghost buttons drop to their own row beneath the
     subtitle instead of crowding the title. */
  header {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
    padding: 14px var(--gutter) 12px;
  }
  header h1 { font-size: 19px; }
  header .sub { font-size: 12.5px; max-width: none; }
  .head-actions { gap: 8px; }
  .head-actions .ghost { flex: 1; }

  /* Touch targets: ghost buttons clear 44px on phones. */
  button.ghost {
    min-height: 44px;
    padding: 10px 14px;
    font-size: 14px;
  }

  /* Conversation: a touch more vertical breathing room, user bubbles can run wider
     since horizontal space is scarce. */
  #messages { padding: 16px var(--gutter) 20px; gap: 14px; }
  .msg.user { max-width: 88%; }
  .bubble { padding: 11px 13px; }

  /* The composer keeps input + send on one row; the input flexes, send stays a
     comfortable thumb target. */
  #composer { padding: 10px var(--gutter); gap: 8px; }
  #send { padding: 12px 18px; }

  /* Footer: let the two halves wrap rather than collide on a narrow screen. */
  footer.brand {
    flex-wrap: wrap;
    gap: 4px 12px;
    padding: 10px var(--gutter);
    font-size: 11.5px;
  }

  .login-wrap { padding: 28px 22px 24px; }
}

/* Respect users who ask for less motion: drop the hover/focus transitions. */
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
