/* Chat (PRD §12): a conventional chat layout. A centered thread and the
   message box at the bottom; the user's conversations live in the app sidebar
   (shell.css). */

/* ---------- Main column ---------- */

.chat-page {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.chat-head {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-5);
}

.chat-head__title { min-width: 0; overflow-wrap: anywhere; color: var(--color-heading); }

/* Sharing follows Spend Relay: a small panel under the header's right button. */
.chat-share { position: relative; flex: none; margin-left: auto; }
.chat-share [hidden] { display: none !important; }
.chat-share__trigger { display: inline-flex; align-items: center; gap: 6px; padding: 5px var(--space-3); border: 1px solid var(--color-border-strong); border-radius: var(--radius-md); background: var(--color-raised); color: var(--color-heading); font: inherit; font-size: var(--text-sm); cursor: pointer; }
.chat-share__trigger:hover { background: var(--color-surface-sunken); }
.chat-share__trigger.is-shared { color: var(--color-success-fg); border-color: var(--color-success-fg); }
.chat-share__panel { position: absolute; right: 0; top: calc(100% + 6px); z-index: 30; width: min(360px, calc(100vw - 104px)); box-sizing: border-box; padding: var(--space-4); border: 1px solid var(--color-border); border-radius: var(--radius-lg); background: var(--color-raised); box-shadow: var(--shadow-md); }
.chat-share__title { margin: 0 0 var(--space-1); font-weight: var(--font-semibold); color: var(--color-heading); }
.chat-share__lead { margin: 0 0 var(--space-3); font-size: var(--text-sm); line-height: 1.55; color: var(--color-text-muted); }
.chat-share__link { display: flex; gap: var(--space-2); }
.chat-share__link input { flex: 1; min-width: 0; padding: var(--space-1) var(--space-2); border: 1px solid var(--color-border); border-radius: var(--radius-md); background: var(--color-surface-sunken); color: var(--color-text); font: inherit; font-size: var(--text-xs); }
.chat-share__create { width: 100%; }
.chat-share__stop { margin-top: var(--space-3); padding: 0; border: 0; background: none; font: inherit; font-size: var(--text-sm); color: var(--color-danger-fg); cursor: pointer; }
.chat-share__stop:hover { text-decoration: underline; }
.chat-share__error { margin: var(--space-2) 0 0; font-size: var(--text-xs); color: var(--color-danger-fg); }
.chat-share button:disabled { opacity: .6; cursor: wait; }
.chat-viewer-note { margin: 0; padding: var(--space-3); border: 1px dashed var(--color-border-strong); border-radius: var(--radius-lg); font-size: var(--text-sm); color: var(--color-text-muted); text-align: center; }

.chat-thread {
  flex: 1;
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
  padding: var(--space-4) var(--space-5) var(--space-6);
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  gap: var(--space-6);
}

.chat-turn,
.chat-agent {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.chat-user { display: flex; justify-content: flex-end; }

.chat-user p {
  max-width: 75%;
  margin: 0;
  padding: var(--space-3) var(--space-4);
  border-radius: 18px;
  background: var(--color-surface-sunken);
  color: var(--color-heading);
  white-space: pre-wrap;
}

.chat-event {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  margin: 0;
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

.chat-event::before,
.chat-event::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--color-border);
}

.chat-reply,
.chat-withdrawn__text {
  color: var(--color-heading);
  font-size: var(--text-md);
  line-height: 1.7;
}

.chat-reply p,
.chat-withdrawn__text p,
.chat-reply ul,
.chat-reply ol,
.chat-reply pre { margin: 0 0 var(--space-3); }
.chat-reply > :last-child { margin-bottom: 0; }
.chat-reply.is-streaming > :last-child::after { content: "▍"; color: var(--color-primary); }
.chat-reply ul,
.chat-reply ol { padding-left: var(--space-5); }
.chat-reply li + li { margin-top: var(--space-1); }
.chat-reply .chat-heading { margin-top: var(--space-4); font-weight: var(--font-semibold); }
.chat-rule { margin: var(--space-4) 0; border: 0; border-top: 1px solid var(--color-border); }

.chat-reply code,
.chat-process code {
  padding: 1px 5px;
  border-radius: var(--radius-sm);
  background: var(--color-surface-sunken);
  font-family: var(--font-mono);
  font-size: 0.9em;
}

.chat-code {
  overflow-x: auto;
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  background: var(--color-surface-sunken);
  font-size: var(--text-sm);
  line-height: 1.5;
}

.chat-code code { padding: 0; background: none; }

/* A code block's Copy sits in its top right corner, shown on hover. */
.chat-code-block { position: relative; margin: 0 0 var(--space-3); }
.chat-code-block .chat-code { margin: 0; }
.chat-code-block > .chat-copy { position: absolute; top: var(--space-2); right: var(--space-2); opacity: 0; }
.chat-code-block:hover > .chat-copy,
.chat-code-block > .chat-copy:focus-visible,
.chat-code-block > .chat-copy.is-done { opacity: 1; }

/* Markdown tables in a reply scroll sideways rather than widen the page;
   amounts the agent aligned right stay right, in even digits. */
.chat-table { overflow-x: auto; margin: 0 0 var(--space-3); }
.chat-table table { width: auto; min-width: 50%; }
.chat-table th,
.chat-table td { padding: var(--space-2) var(--space-3); }
.chat-table thead th { text-transform: none; letter-spacing: 0; font-size: var(--text-sm); color: var(--color-heading); font-weight: var(--font-semibold); }
.chat-table .is-num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* A finished reply, with its Copy under it, shown on hover like a code
   block's (always on touch screens, which have no hover). */
.chat-answer__actions { display: flex; gap: var(--space-2); margin-top: var(--space-2); opacity: 0; transition: opacity 120ms ease; }
.chat-answer:hover .chat-answer__actions,
.chat-answer__actions:focus-within,
.chat-answer__actions:has(.is-done) { opacity: 1; }
@media (hover: none) { .chat-answer__actions { opacity: 1; } }

.chat-copy,
.chat-retry {
  min-height: 28px;
  padding: 0 var(--space-3);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-raised);
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  cursor: pointer;
}
.chat-copy:hover,
.chat-retry:hover { background: var(--color-surface-sunken); color: var(--color-heading); }
.chat-copy.is-done { color: var(--color-heading); }
.chat-retry-form { margin: 0; }
.chat-retry { color: var(--color-heading); }

/* A turn's work, folded into one quiet line: reasoning, the agent's notes
   between tool calls, and the steps, in the order they happened. */
.chat-process {
  margin: 0;
  padding: 0;
  border: 0;
  background: none;
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

.chat-process[open] summary { margin-bottom: 0; }

.chat-process summary {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  cursor: pointer;
  list-style: none;
}

.chat-process summary::-webkit-details-marker { display: none; }
.chat-process summary::after { content: "›"; transition: transform 120ms ease; }
.chat-process[open] summary::after { transform: rotate(90deg); }
.chat-process summary:hover { color: var(--color-heading); }

.chat-process__list {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  margin: var(--space-2) 0 0;
  padding: 0 0 0 var(--space-4);
  border-left: 2px solid var(--color-border);
  list-style: none;
}

.chat-process__thought p,
.chat-process__note p { margin: 0 0 var(--space-1); }
.chat-process__thought { font-style: italic; }
.chat-process__note { color: var(--color-text); }
.chat-process__outcome { margin-left: var(--space-2); }
.chat-process__step--fail { color: var(--color-danger-fg); }
.chat-process__caveat { margin: var(--space-2) 0 0 var(--space-4); font-size: var(--text-xs); }

.chat-stopped { display: flex; align-items: center; gap: var(--space-3); margin: 0; font-size: var(--text-sm); color: var(--color-text-muted); }

.chat-withdrawn {
  padding: var(--space-3) var(--space-4);
  border: 1px dashed var(--color-border-strong);
  border-radius: var(--radius-md);
}

.chat-withdrawn__text { text-decoration: line-through; color: var(--color-text-muted); font-size: var(--text-sm); }
.chat-withdrawn__why { margin: 0; font-size: var(--text-sm); color: var(--color-warning-fg); }

.chat-review {
  display: flex;
  align-items: center;
  gap: var(--space-4);
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  color: var(--color-text);
  text-decoration: none;
}

.chat-review:hover { background: var(--color-bg); text-decoration: none; }
.chat-review__body { display: flex; flex-direction: column; gap: 2px; min-width: 0; margin-right: auto; }
.chat-review__body strong { color: var(--color-heading); }
.chat-review__meta { font-size: var(--text-xs); color: var(--color-text-muted); }

.chat-working {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  margin: 0;
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

.chat-error {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  background: var(--color-danger-bg);
  color: var(--color-danger-fg);
  font-size: var(--text-sm);
}

.chat-error .chat-retry-form { align-self: flex-start; }

.chat-suggestions { display: flex; flex-wrap: wrap; gap: var(--space-2); }
.chat-suggestion { margin: 0; }

.chat-suggestion button {
  min-height: 36px;
  padding: 0 var(--space-4);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-pill);
  background: var(--color-raised);
  color: var(--color-heading);
  font: inherit;
  font-size: var(--text-sm);
  cursor: pointer;
}

.chat-suggestion button:hover { background: var(--color-bg); }

/* ---------- Message box ---------- */

.chat-dock {
  position: sticky;
  bottom: 0;
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
  /* A tall fade above the box, so text scrolling under it never butts
     against the box's edge. */
  padding: var(--space-7) var(--space-5) var(--space-4);
  box-sizing: border-box;
  background: linear-gradient(to bottom, transparent, var(--color-surface) var(--space-6));
  pointer-events: none;
}

.chat-dock > * { pointer-events: auto; }

/* Shown when a reply arrives below where the user has scrolled to. */
.chat-jump {
  position: absolute;
  top: var(--space-2);
  left: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--color-border);
  border-radius: 50%;
  background: var(--color-raised);
  color: var(--color-heading);
  box-shadow: var(--shadow-md);
  transform: translateX(-50%);
  cursor: pointer;
}

.chat-jump[hidden] { display: none; }
.chat-jump svg { flex: none; }

.chat-composer {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  padding: var(--space-3) var(--space-3) var(--space-2) var(--space-4);
  border: 1px solid var(--color-border);
  border-radius: 20px;
  background: var(--color-raised);
  box-shadow: var(--shadow-md);
  transition: border-color 120ms ease;
}

.chat-composer:focus-within { border-color: var(--color-border-strong); }

/* In a conversation: one row, the field beside send or stop; a longer
   message grows the row upward (after spend-relay). */
.chat-composer--inline {
  flex-direction: row;
  align-items: flex-end;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-2) var(--space-2) var(--space-4);
  border-radius: 14px;
}
.chat-composer--inline textarea,
.chat-composer--inline textarea:focus,
.chat-composer--inline textarea:focus-visible { flex: 1; min-width: 0; min-height: 32px; padding: 4px 0; }
.chat-composer--inline .chat-composer__bar { flex: none; }

/* The line under a conversation's box: the read-only note on the left, the
   context meter and the model on the right. */
.chat-composer__foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  min-height: 28px;
  margin-top: 2px;
  padding-inline: var(--space-2);
}
.chat-composer__hint {
  min-width: 0;
  margin: 0;
  overflow: hidden;
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chat-composer__tools { display: flex; flex: none; align-items: center; gap: var(--space-1); }
.chat-composer__tools .context-meter { width: 24px; height: 24px; }
.chat-composer__tools .context-meter svg { width: 15px; height: 15px; }

/* The box itself shows focus; the field inside draws no ring of its own. */
.chat-composer textarea,
.chat-composer textarea:focus,
.chat-composer textarea:focus-visible {
  width: 100%;
  max-width: none;
  min-height: 28px;
  padding: var(--space-1) 0;
  border: 0;
  border-radius: 0;
  outline: none;
  box-shadow: none;
  resize: none;
  background: transparent;
  color: var(--color-heading);
  font: inherit;
  font-size: var(--text-base);
  line-height: 1.5;
  box-sizing: border-box;
}

.chat-composer__bar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-2);
}

.chat-form .chat-composer__model {
  width: auto;
  field-sizing: content;
  max-width: 12rem;
  min-height: 32px;
  padding: 0 var(--space-2);
  border: 0;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--color-text-muted);
  font: inherit;
  font-size: var(--text-sm);
  cursor: pointer;
}

.chat-form .chat-composer__model:hover { background: var(--color-surface-sunken); color: var(--color-heading); }
.chat-form .chat-composer__model:focus { outline: none; border: 0; }
.chat-form .chat-composer__model:focus-visible { outline: 2px solid var(--color-focus-ring); outline-offset: 0; }
.chat-composer__foot .chat-composer__model { min-height: 24px; font-size: var(--text-xs); }

.chat-composer__send {
  display: flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 0;
  border-radius: var(--radius-md);
  background: var(--color-primary);
  color: var(--color-primary-contrast);
  cursor: pointer;
}

.chat-composer__send[hidden] { display: none; }
.chat-composer__send svg { flex: none; }
.chat-composer__send:hover:not(:disabled) { filter: brightness(1.08); }
.chat-composer__send:focus-visible { outline: 2px solid var(--color-focus-ring); outline-offset: 2px; }
/* The stop button inverts the page: ink with a light square in light, light
   with a navy square in dark. */
.chat-composer__stop { background: var(--color-heading); color: var(--color-surface); }
.chat-composer__send:disabled { background: var(--color-border-strong); cursor: not-allowed; }

/* The context meter: a ring that fills as the conversation takes more of
   the model's context window, warning past 80%. Its panel is a popover
   anchored above the ring. */
.context-meter {
  display: flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 0;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--color-primary);
  cursor: pointer;
  anchor-name: --context-meter;
}

.context-meter:hover { background: var(--color-surface-sunken); }
.context-meter:focus-visible { outline: 2px solid var(--color-focus-ring); outline-offset: 0; }
.context-meter.is-high { color: var(--color-warning-fg); }
.context-meter svg { transform: rotate(-90deg); }
.context-meter circle { fill: none; stroke-width: 2.5; }
.context-meter__track { stroke: var(--color-border); }
.context-meter__used { stroke: currentColor; stroke-linecap: round; transition: stroke-dasharray 300ms ease; }

.context-meter__panel {
  position-anchor: --context-meter;
  position-area: top span-left;
  inset: auto;
  width: 17rem;
  margin: 0 0 var(--space-2);
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  background: var(--color-raised);
  box-shadow: var(--shadow-md);
  color: var(--color-text);
  font-size: var(--text-sm);
}

.context-meter__head {
  display: flex;
  justify-content: space-between;
  color: var(--color-heading);
  font-weight: 600;
}

.context-meter__percent { font-variant-numeric: tabular-nums; }

.context-meter__bar {
  height: 6px;
  margin: var(--space-2) 0;
  border-radius: var(--radius-pill);
  background: var(--color-surface-sunken);
  overflow: hidden;
}

.context-meter__bar span { display: block; height: 100%; border-radius: inherit; background: var(--color-primary); }
.context-meter.is-high + .context-meter__panel .context-meter__bar span { background: var(--color-warning-fg); }
.context-meter__figure { margin: 0; font-variant-numeric: tabular-nums; }
.context-meter__note { margin: var(--space-2) 0 0; color: var(--color-text-muted); font-size: var(--text-xs); }

.context-meter__warning {
  margin: var(--space-2) 0 0;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  background: var(--color-warning-bg);
  color: var(--color-warning-fg);
  font-size: var(--text-xs);
}

.context-meter__warning[hidden] { display: none; }

.context-meter__compact {
  width: 100%;
  min-height: 32px;
  margin-top: var(--space-3);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--color-heading);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.context-meter__compact:hover:not(:disabled) { background: var(--color-surface-sunken); }
.context-meter__compact:focus-visible { outline: 2px solid var(--color-focus-ring); outline-offset: 2px; }
.context-meter__compact:disabled { color: var(--color-text-muted); cursor: not-allowed; }

/* A compaction: a rule across the thread with its label in the middle; the
   summary opens below it. */
.chat-compact {
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: none;
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

.chat-compact > summary,
p.chat-compact {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  list-style: none;
}

.chat-compact > summary { margin: 0; cursor: pointer; }
.chat-compact > summary::-webkit-details-marker { display: none; }
.chat-compact > summary::before,
.chat-compact > summary::after { content: ""; flex: 1; border-top: 1px solid var(--color-border); }
.chat-compact > summary:hover { color: var(--color-heading); }
.chat-compact > summary:focus-visible { outline: 2px solid var(--color-focus-ring); outline-offset: 2px; }
.chat-compact--failed { color: var(--color-danger-fg); }

.chat-compact__summary {
  margin-top: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-raised);
  color: var(--color-text);
}

.chat-compact__note { margin: var(--space-2) 0 0; font-size: var(--text-xs); color: var(--color-text-muted); }

.chat-dock__note {
  margin: var(--space-2) 0 0;
  text-align: center;
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

.chat-archived {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin: 0;
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  background: var(--color-surface-sunken);
  font-size: var(--text-sm);
}

/* ---------- A new conversation: greeting and box centered ---------- */

.chat-page--fresh .chat-thread {
  flex: 0 0 auto;
  margin-top: 10vh;  /* the agent orb above takes this space when shown */
  text-align: center;
}

.chat-page--fresh .chat-thread { min-height: 3rem; }
.chat-page--fresh .chat-reply { font-size: var(--text-2xl); color: var(--color-heading); }
.chat-welcome { margin: 0; font-size: var(--text-2xl); font-weight: var(--font-normal); color: var(--color-heading); letter-spacing: 0; }
.chat-page--fresh .chat-process { display: none; }

/* What a new conversation can do: a row of chips under the box. */
.chat-capabilities {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: var(--space-2);
  margin: var(--space-4) 0 0;
  padding: 0;
  list-style: none;
}

.chat-capability {
  min-height: 32px;
  padding: 0 var(--space-4);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-pill);
  background: var(--color-raised);
  color: var(--color-text-muted);
  font: inherit;
  font-size: var(--text-sm);
  cursor: pointer;
}

.chat-capability:hover { border-color: var(--color-border-strong); color: var(--color-heading); }

/* The link under a new conversation's box to how Spend Lens works. */
.chat-dock__about {
  display: block;
  margin-top: var(--space-3);
  text-align: center;
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  text-decoration: none;
}

.chat-dock__about:hover { color: var(--color-heading); text-decoration: underline; }

/* How Spend Lens works (/about). */
.chat-about .agent-flow { margin-top: var(--space-4); }
.chat-about__finds { margin: var(--space-5) 0 0; font-size: var(--text-sm); line-height: 1.55; color: var(--color-text-muted); }

.chat-about__tips { display: grid; gap: var(--space-2); margin: 0; padding: 0; list-style: none; font-size: var(--text-sm); line-height: 1.55; }
.chat-about__tips li { padding-left: var(--space-4); position: relative; color: var(--color-text-muted); }
.chat-about__tips li::before { content: ""; position: absolute; left: 2px; top: 0.6em; width: 5px; height: 5px; border-radius: 50%; background: var(--color-primary); }
.chat-about__tips strong { color: var(--color-heading); font-weight: var(--font-semibold); }

.chat-about__examples { margin: var(--space-4) 0 0; font-size: var(--text-sm); line-height: 1.55; color: var(--color-text-muted); }
.chat-about__fine { margin: 0 0 var(--space-6); font-size: var(--text-xs); line-height: 1.55; color: var(--color-text-muted); }
.chat-about__back { font-size: var(--text-sm); color: var(--color-text-muted); text-decoration: none; }
.chat-about__back:hover { color: var(--color-heading); text-decoration: underline; }

@media (max-width: 760px) {
  .chat-thread, .chat-dock { padding-inline: var(--space-4); }
}

/* A FeedMob system link (a click URL in Admin): the FeedMob "+" mark, then
   the ID, in the brand teal; it opens in a new tab. */
.fm-link {
  display: inline-flex;
  align-items: baseline;
  gap: 3px;
  color: var(--color-primary);
  font-weight: var(--font-medium);
  text-decoration: none;
  white-space: nowrap;
  border-bottom: 1px solid color-mix(in srgb, var(--color-primary) 35%, transparent);
}

.fm-link::before {
  content: "";
  flex: none;
  align-self: center;
  width: 0.85em;
  height: 0.85em;
  background: url("/assets/feedmob-mark-cd07341a.svg") center / contain no-repeat;
}

.fm-link:hover { color: var(--color-primary-hover); border-bottom-color: currentColor; }
.fm-link:focus-visible { outline: 2px solid var(--color-focus-ring); outline-offset: 2px; border-radius: 2px; }
