/*
  One stylesheet, custom properties for theming, no framework.

  Light or dark follows the visitor's system setting (`prefers-color-scheme`); there is no toggle.

  Layout is a three-row grid on the viewport: masthead, scrolling thread, composer pinned to the
  bottom. `svh` rather than `vh` so the mobile URL bar collapsing does not hide the composer, which
  is the single most common way a chat UI breaks on a phone. Masthead, thread and composer share one
  column (`--measure`) so every left edge lines up.

  The system: spacing on a 4px base (4 8 12 16 24 32 48); type at 12 / 14 / 16 / 18 and one display
  size; one accent, used for the mark, links and the send button, and nothing else.
*/

:root {
  color-scheme: light dark;

  --bg: #f7f6f2;
  --surface: #ffffff;
  --surface-2: #efede7;
  --border: #e2dfd7;
  --text: #1b1d21;
  --text-dim: #5d6168;
  --accent: #2446c7;
  --accent-contrast: #ffffff;
  --question-bg: #e6e9f6;
  --question-text: #1b1d21;
  --ok: #16803c;
  --warn: #b45309;
  --danger: #b42318;
  --shadow: 0 1px 2px rgb(27 29 33 / 0.04), 0 6px 20px rgb(27 29 33 / 0.06);

  --radius: 14px;
  --radius-sm: 8px;
  --gutter: 16px;
  --measure: 44rem;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, Helvetica, Arial, sans-serif;
  --serif: ui-serif, "New York", "Iowan Old Style", Charter, "Source Serif Pro", Georgia, serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121416;
    --surface: #1a1d20;
    --surface-2: #23272b;
    --border: #2d3136;
    --text: #ecedee;
    --text-dim: #9ea4ab;
    --accent: #95a9ff;
    --accent-contrast: #121416;
    --question-bg: #242b47;
    --question-text: #eceffd;
    --ok: #4ade80;
    --warn: #fbbf24;
    --danger: #f87171;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 8px 24px rgb(0 0 0 / 0.35);
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  display: grid;
  /* A column that can be narrower than its content. The default one grew to fit the whole product
     name, so on a phone the page came out wider than the screen (test/layout.test.ts). */
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto 1fr auto;
  min-height: 100svh;
  max-height: 100svh;
  overflow: hidden;
}

.sr-only,
.msg--user .msg__role {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.skip {
  position: absolute;
  left: -9999px;
  top: 0;
  z-index: 10;
  background: var(--accent);
  color: var(--accent-contrast);
  padding: 8px 16px;
  border-radius: 0 0 var(--radius-sm) 0;
}
.skip:focus {
  left: 0;
}

/* ---------- masthead ---------- */

.masthead {
  border-bottom: 1px solid var(--border);
}

.masthead__inner {
  max-width: var(--measure);
  margin-inline: auto;
  padding: 12px var(--gutter);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

/* The mark is the wedge at the crown of an arch: the stone that holds the rest in place. */
.brand__mark {
  width: 18px;
  height: 18px;
  color: var(--accent);
  flex: none;
}

.brand__name {
  font-family: var(--serif);
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.01em;
  /* The full name is long for a phone's header: it is cut short rather than pushing the status off. */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0;
  font-size: 12px;
  color: var(--text-dim);
  white-space: nowrap;
}

.status__dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-dim);
  flex: none;
}
.status__dot[data-state="connecting"] {
  background: var(--warn);
  animation: pulse 1.4s ease-in-out infinite;
}
.status__dot[data-state="ready"] {
  background: var(--ok);
}
.status__dot[data-state="error"] {
  background: var(--danger);
}

@keyframes pulse {
  50% {
    opacity: 0.3;
  }
}

/* ---------- page + thread ---------- */

.page {
  overflow-y: auto;
  overscroll-behavior: contain;
  scroll-behavior: smooth;
}

.thread {
  max-width: var(--measure);
  margin-inline: auto;
  padding: 24px var(--gutter) 32px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

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

.empty {
  padding: clamp(24px, 9vh, 88px) 0 8px;
}

.empty__title {
  margin: 0 0 12px;
  font-family: var(--serif);
  font-weight: 500;
  font-size: clamp(34px, 7vw, 48px);
  line-height: 1.08;
  letter-spacing: -0.025em;
  text-wrap: balance;
}

.empty__body {
  margin: 0 0 32px;
  max-width: 34rem;
  color: var(--text-dim);
  font-size: 16px;
}

.suggestions {
  list-style: none;
  margin: 0;
  padding: 0;
  border-top: 1px solid var(--border);
}

.suggestions li {
  border-bottom: 1px solid var(--border);
}

.chip {
  font: inherit;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 0;
  text-align: left;
  color: var(--text);
  background: none;
  border: 0;
  cursor: pointer;
}

.chip::after {
  content: "→";
  color: var(--text-dim);
  transition: transform 0.15s, color 0.15s;
}

.chip:hover {
  color: var(--accent);
}

.chip:hover::after {
  color: var(--accent);
  transform: translateX(3px);
}

/* ---------- messages ---------- */

.msg {
  display: flex;
  flex-direction: column;
  max-width: 100%;
  animation: rise 0.18s ease-out;
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
}

@media (prefers-reduced-motion: reduce) {
  .msg {
    animation: none;
  }
  .page {
    scroll-behavior: auto;
  }
  .status__dot[data-state="connecting"] {
    animation: none;
  }
  .chip::after {
    transition: none;
  }
}

.msg--user {
  align-items: flex-end;
}

.msg--user .msg__body {
  background: var(--question-bg);
  color: var(--question-text);
  padding: 8px 16px;
  border-radius: var(--radius) var(--radius) 4px var(--radius);
  max-width: min(100%, 32rem);
}

/* Answers sit on the page like a document rather than in a box: they are long, and a border around
   forty lines of text is noise. */
.msg--agent + .msg--user {
  margin-top: 8px;
}

.msg__role {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dim);
  margin: 0 0 8px;
}

.msg__body {
  min-width: 0;
  overflow-wrap: anywhere;
}

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

.msg__body p {
  margin: 0 0 12px;
}

.msg__body ul,
.msg__body ol {
  margin: 0 0 12px;
  padding-left: 1.25em;
}

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

.msg__body li::marker {
  color: var(--text-dim);
}

.msg__body a {
  color: var(--accent);
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
}

.msg__body code {
  font-family: var(--mono);
  font-size: 0.88em;
  background: var(--surface-2);
  border-radius: 4px;
  padding: 1px 4px;
}

.msg__body h3 {
  font-size: 18px;
  font-weight: 600;
  line-height: 1.3;
  margin: 24px 0 8px;
  letter-spacing: -0.01em;
}

.msg__body h3:first-child {
  margin-top: 0;
}

/* ---------- citations ---------- */

.sources {
  margin-top: 24px;
}

.sources__title {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dim);
  margin: 0 0 4px;
}

.msg__body .sources__list {
  list-style: none;
  margin: 0;
  padding: 0;
  border-top: 1px solid var(--border);
}

.sources__list li {
  margin: 0;
  border-bottom: 1px solid var(--border);
}

.source {
  display: grid;
  grid-template-columns: 1fr auto;
  column-gap: 16px;
  padding: 8px 0;
  text-decoration: none;
}

.source__title {
  grid-column: 1;
  grid-row: 1;
  font-size: 14px;
  color: var(--text);
  line-height: 1.4;
}

.source__host {
  grid-column: 1;
  grid-row: 2;
  font-size: 12px;
  color: var(--text-dim);
}

.source::after {
  content: "↗";
  grid-column: 2;
  grid-row: 1 / span 2;
  align-self: center;
  color: var(--text-dim);
}

.source:hover .source__title,
.source:hover::after {
  color: var(--accent);
}

/* ---------- typing ---------- */

.typing {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  padding: 4px 0;
}

.typing span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-dim);
  animation: blink 1.2s infinite;
}
.typing span:nth-child(2) {
  animation-delay: 0.16s;
}
.typing span:nth-child(3) {
  animation-delay: 0.32s;
}

@keyframes blink {
  0%,
  60%,
  100% {
    opacity: 0.25;
  }
  30% {
    opacity: 1;
  }
}

.notice {
  align-self: flex-start;
  font-size: 14px;
  color: var(--danger);
  background: color-mix(in srgb, var(--danger) 8%, transparent);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  max-width: 100%;
}

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

.composer {
  padding: 8px var(--gutter) max(12px, env(safe-area-inset-bottom));
  background: var(--bg);
}

.composer__inner {
  max-width: var(--measure);
  margin-inline: auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 8px 8px 8px 16px;
  transition: border-color 0.15s;
}

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

.composer textarea {
  flex: 1;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  resize: none;
  outline: none;
  max-height: 9.5rem;
  padding: 6px 0;
  line-height: 1.5;
}

.composer textarea::placeholder {
  color: var(--text-dim);
}

.composer button {
  flex: none;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  border: 0;
  background: var(--accent);
  color: var(--accent-contrast);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: opacity 0.15s;
}

.composer button:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.composer__note {
  max-width: var(--measure);
  margin: 8px auto 0;
  padding-inline: 17px; /* the composer's border plus its inner padding, so this lines up with the text */
  font-size: 12px;
  color: var(--text-dim);
}

:where(a, button, textarea):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

@media (max-width: 30rem) {
  .composer__note {
    display: none;
  }
}

/* ---------- tables ----------
   Answers to decision questions often come as comparison tables, and a table is the one block that
   cannot simply wrap on a narrow screen. It scrolls inside its own box instead, so a wide table
   never widens the page — horizontal scroll on the whole document is the usual way a chat UI
   breaks on a phone. */

.table-scroll {
  overflow-x: auto;
  margin: 0 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  -webkit-overflow-scrolling: touch;
}

.msg__body table {
  border-collapse: collapse;
  width: 100%;
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}

.msg__body th,
.msg__body td {
  text-align: left;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.msg__body thead th {
  background: var(--surface-2);
  font-weight: 600;
  white-space: nowrap;
  position: sticky;
  top: 0;
}

.msg__body tbody tr:last-child td {
  border-bottom: 0;
}
