/* chat.css — "Ask my AI twin" widget + answer rendering.
   THEMING CONTRACT (§7): consumes ONLY design tokens from site.css :root
   (--bg, --bg-card, --bg-subtle, --border, --border-soft, --text,
   --text-muted, --accent, --accent-soft, --accent-glow, --success,
   --font-sans, --font-mono, --radius*). No new colors, no new fonts. */

/* ── Launcher pill ─────────────────────────────────────────────── */
.chat-launcher {
  position: fixed; right: 1.25rem; bottom: 1.25rem; z-index: 950;
  display: inline-flex; align-items: center; gap: 0.5rem;
  padding: 0.65rem 1.1rem; border-radius: 999px;
  background: var(--accent); color: #fff; border: 1px solid var(--accent);
  font-family: var(--font-sans); font-size: 0.9rem; font-weight: 600;
  cursor: pointer; box-shadow: 0 4px 24px var(--accent-glow);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.chat-launcher:hover { transform: translateY(-2px); box-shadow: 0 6px 28px var(--accent-glow); }
.chat-widget.is-open .chat-launcher { display: none; }

/* ── Panel ─────────────────────────────────────────────────────── */
.chat-panel {
  position: fixed; right: 1.25rem; bottom: 1.25rem; z-index: 960;
  width: min(420px, calc(100vw - 2rem));
  height: min(620px, calc(100dvh - 4rem));
  display: flex; flex-direction: column;
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: var(--radius-lg); overflow: hidden;
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.45);
  font-family: var(--font-sans); color: var(--text);
}
/* The panel's open/closed state is the [hidden] attribute, toggled in chat.js.
   Because .chat-panel sets display:flex, author CSS overrides the UA
   [hidden]{display:none} rule — so without this the panel would render
   permanently (always "open") and the close button would have no visible
   effect. This restores hide-on-[hidden]; specificity (0,2,0) wins over
   .chat-panel (0,1,0) and the mobile override below. */
.chat-panel[hidden] { display: none; }
.chat-header {
  display: flex; align-items: flex-start; justify-content: space-between;
  padding: 0.85rem 1rem; border-bottom: 1px solid var(--border-soft);
  background: var(--bg-subtle);
}
.chat-title { font-weight: 700; font-size: 0.95rem; }
.chat-disclosure { font-family: var(--font-mono); font-size: 0.68rem; color: var(--text-muted); margin-top: 0.15rem; }
.chat-close {
  background: none; border: none; color: var(--text-muted); font-size: 1.5rem;
  line-height: 1; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  /* >=44px tap target for touch; negative margin keeps it visually tight in the header */
  width: 2.75rem; height: 2.75rem; margin: -0.5rem -0.5rem -0.5rem 0;
  border-radius: var(--radius);
}
.chat-close:hover { color: var(--text); }

/* ── Messages ──────────────────────────────────────────────────── */
.chat-messages {
  flex: 1; overflow-y: auto; padding: 1rem; display: flex; flex-direction: column; gap: 0.75rem;
  overscroll-behavior: contain;          /* don't chain scroll to the page behind the sheet */
  -webkit-overflow-scrolling: touch;     /* momentum scroll on iOS */
}
.chat-msg { max-width: 88%; padding: 0.6rem 0.8rem; border-radius: var(--radius); font-size: 0.875rem; line-height: 1.5; }
.chat-msg.user { align-self: flex-end; background: var(--accent-glow); border: 1px solid var(--border-soft); }
.chat-msg.bot  { align-self: flex-start; background: var(--bg-subtle); border: 1px solid var(--border-soft); }
.chat-msg.error { align-self: flex-start; background: var(--bg-subtle); border: 1px dashed var(--border); color: var(--text-muted); }

/* tool activity status line (while a tool call runs) */
.chat-status {
  align-self: flex-start; font-family: var(--font-mono); font-size: 0.72rem;
  color: var(--text-muted); padding: 0.15rem 0.4rem;
}
.chat-status::after { content: '…'; animation: chat-ellipsis 1.2s infinite; }
@keyframes chat-ellipsis { 0% { opacity: 0.2; } 50% { opacity: 1; } 100% { opacity: 0.2; } }

/* "thinking" loading wheel — shown from message submit until the first token,
   tool activity, refusal, or error arrives (so the visitor never stares at a
   frozen UI while the guard + model spin up). */
.chat-thinking {
  align-self: flex-start; display: inline-flex; align-items: center; gap: 0.5rem;
  padding: 0.5rem 0.75rem; border-radius: var(--radius);
  background: var(--bg-subtle); border: 1px solid var(--border-soft);
  font-size: 0.8rem; color: var(--text-muted);
}
.chat-spinner {
  width: 1.05rem; height: 1.05rem; border-radius: 50%; flex-shrink: 0;
  border: 2px solid var(--border); border-top-color: var(--accent);
  animation: chat-spin 0.7s linear infinite;
}
@keyframes chat-spin { to { transform: rotate(360deg); } }

/* per-answer collapsible tool trace (§7) */
.chat-trace { align-self: flex-start; max-width: 88%; font-family: var(--font-mono); font-size: 0.7rem; color: var(--text-muted); }
.chat-trace summary { cursor: pointer; user-select: none; padding: 0.1rem 0.2rem; }
.chat-trace summary:hover { color: var(--text); }
.chat-trace-call { margin: 0.35rem 0 0 0.75rem; }
.chat-trace-call .name { color: var(--accent-soft); }
.chat-trace pre {
  margin: 0.2rem 0 0; padding: 0.4rem 0.5rem; background: var(--bg);
  border: 1px solid var(--border-soft); border-radius: var(--radius-sm);
  white-space: pre-wrap; word-break: break-word; max-height: 8rem; overflow-y: auto;
}

/* ── Starter chips ─────────────────────────────────────────────── */
.chat-chips { display: flex; flex-wrap: wrap; gap: 0.4rem; padding: 0 1rem 0.75rem; }
.chat-chip {
  font-family: var(--font-mono); font-size: 0.72rem; color: var(--text);
  background: var(--bg-subtle); border: 1px solid var(--border);
  border-radius: 999px; padding: 0.3rem 0.7rem; cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease;
}
.chat-chip:hover { border-color: var(--accent); background: var(--accent-glow); }

/* ── Collapsible "what people usually ask me" drawer ───────────── */
.chat-faq { border-top: 1px solid var(--border-soft); }
.chat-faq-toggle {
  width: 100%; display: flex; align-items: center; justify-content: space-between;
  gap: 0.5rem; min-height: 2.75rem;            /* ≥44px touch target */
  padding: 0.6rem 1rem; cursor: pointer;
  background: var(--bg-subtle); border: none; color: var(--text-muted);
  font-family: var(--font-mono); font-size: 0.72rem; text-align: left;
}
.chat-faq-toggle:hover { color: var(--text); }
.chat-faq-arrow { transition: transform 0.2s ease; }
.chat-faq:not(.is-open) .chat-faq-arrow { transform: rotate(-90deg); }

/* Slide via grid-template-rows 0fr→1fr (no magic max-height; survives wrap).
   visibility:hidden on collapse removes the chips from the tab order /
   a11y tree — a 0fr row alone leaves the buttons focusable. */
.chat-faq-body {
  display: grid; grid-template-rows: 0fr; visibility: hidden;
  transition: grid-template-rows 0.25s ease, visibility 0s linear 0.25s;
}
.chat-faq.is-open .chat-faq-body {
  grid-template-rows: 1fr; visibility: visible;
  transition: grid-template-rows 0.25s ease, visibility 0s;
}
.chat-faq-body > .chat-chips { overflow: hidden; min-height: 0; }

/* ── Input ─────────────────────────────────────────────────────── */
.chat-input {
  display: flex; gap: 0.5rem; align-items: flex-end;
  padding: 0.75rem 1rem; border-top: 1px solid var(--border-soft);
}
.chat-input textarea {
  flex: 1; resize: none; background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius);
  font-family: var(--font-sans); font-size: 0.875rem; padding: 0.5rem 0.65rem;
  min-height: 2.4rem; max-height: 9rem;
}
.chat-input textarea:focus { outline: none; border-color: var(--accent); }
.chat-send {
  background: var(--accent); color: #fff; border: none; border-radius: var(--radius);
  width: 2.4rem; height: 2.4rem; font-size: 1rem; cursor: pointer;
}
.chat-send:disabled { opacity: 0.45; cursor: default; }
/* Send button doubles as a Stop control while streaming. */
.chat-send.is-stop { background: var(--bg-subtle); color: var(--text); border: 1px solid var(--border); }

/* Per-answer action row (Copy). */
.chat-answer-actions { align-self: flex-start; display: flex; gap: 0.4rem; margin-top: -0.35rem; }
.chat-copy {
  font-family: var(--font-mono); font-size: 0.68rem; color: var(--text-muted);
  background: none; border: 1px solid var(--border-soft); border-radius: var(--radius-sm);
  padding: 0.15rem 0.45rem; cursor: pointer;
}
.chat-copy:hover { color: var(--text); border-color: var(--border); }

/* Contextual follow-up suggestion chips under an answer. */
.chat-suggestions { align-self: flex-start; display: flex; flex-wrap: wrap; gap: 0.4rem; max-width: 88%; }
.chat-suggestion {
  font-family: var(--font-mono); font-size: 0.72rem; color: var(--text);
  background: var(--bg-subtle); border: 1px solid var(--border);
  border-radius: 999px; padding: 0.3rem 0.7rem; cursor: pointer; text-align: left;
  transition: border-color 0.15s ease, background 0.15s ease;
}
.chat-suggestion:hover { border-color: var(--accent); background: var(--accent-glow); }

/* 👍/👎 feedback buttons in the answer-action row. */
.chat-feedback {
  font-size: 0.8rem; line-height: 1; color: var(--text-muted);
  background: none; border: 1px solid var(--border-soft); border-radius: var(--radius-sm);
  padding: 0.15rem 0.4rem; cursor: pointer;
}
.chat-feedback:hover { border-color: var(--border); }
.chat-feedback.is-on { border-color: var(--accent); background: var(--accent-glow); }
.chat-feedback:disabled { cursor: default; }
.chat-fit-note {
  font-family: var(--font-mono); font-size: 0.68rem; color: var(--accent-soft);
  padding: 0.25rem 1rem 0;
}
.chat-footer {
  font-family: var(--font-mono); font-size: 0.65rem; color: var(--text-muted);
  text-align: center; padding: 0.4rem 1rem 0.6rem;
}

/* ── Rendered answer content (.cr-*) ───────────────────────────── */
.chat-msg .cr-p { margin: 0 0 0.5rem; }
.chat-msg .cr-p:last-child { margin-bottom: 0; }
.chat-msg .cr-h { font-weight: 700; }
.chat-msg .cr-list { margin: 0 0 0.5rem; padding-left: 1.2rem; }
.chat-msg .cr-list li { margin-bottom: 0.2rem; }
.chat-msg a { color: var(--accent-soft); text-decoration: underline; }
.chat-msg code {
  font-family: var(--font-mono); font-size: 0.8em;
  background: var(--bg); border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm); padding: 0.05rem 0.3rem;
}
.cr-pre {
  font-family: var(--font-mono); font-size: 0.75rem; background: var(--bg);
  border: 1px solid var(--border-soft); border-radius: var(--radius-sm);
  padding: 0.5rem 0.65rem; overflow-x: auto; white-space: pre-wrap; word-break: break-word;
}
.cr-table-wrap { overflow-x: auto; margin: 0.25rem 0 0.5rem; }
.cr-table { border-collapse: collapse; font-size: 0.8rem; width: 100%; }
.cr-table th {
  font-family: var(--font-mono); font-size: 0.68rem; text-transform: uppercase;
  letter-spacing: 0.04em; color: var(--text-muted); text-align: left;
  background: var(--bg-subtle); padding: 0.4rem 0.55rem; border: 1px solid var(--border);
}
.cr-table td { padding: 0.4rem 0.55rem; border: 1px solid var(--border-soft); vertical-align: top; }
.cr-chart { margin: 0.4rem 0 0.5rem; }
.cr-chart figcaption {
  font-family: var(--font-mono); font-size: 0.72rem; color: var(--text);
  margin-bottom: 0.3rem;
}
.cr-svg { width: 100%; height: auto; display: block; }
.cr-axis-label { font-family: var(--font-mono); font-size: 11px; fill: var(--text-muted); }
.cr-legend { display: flex; flex-wrap: wrap; gap: 0.4rem 0.9rem; margin-top: 0.35rem; }
.cr-legend-item {
  display: inline-flex; align-items: center; gap: 0.3rem;
  font-family: var(--font-mono); font-size: 0.68rem; color: var(--text-muted);
}
.cr-legend-swatch { width: 0.6rem; height: 0.6rem; border-radius: 2px; display: inline-block; }
.cr-chart-pending {
  font-family: var(--font-mono); font-size: 0.72rem; color: var(--text-muted);
  font-style: italic; padding: 0.5rem 0;
}

/* ── Mobile: full-screen sheet ─────────────────────────────────── */
@media (max-width: 640px) {
  .chat-panel {
    inset: 0; width: 100%; border-radius: 0;
    height: 100vh; height: 100dvh;          /* dvh tracks browser chrome; chat.js also syncs to visualViewport so the input clears the soft keyboard */
    max-height: 100vh; max-height: 100dvh;
  }
  .chat-launcher { right: 1rem; bottom: max(1rem, env(safe-area-inset-bottom)); min-height: 44px; }

  /* iOS focus auto-zoom fix: >=16px stops Safari zooming on input focus.
     Also bring the input row + send button to the 44px touch-target minimum. */
  .chat-input textarea { font-size: 16px; min-height: 2.75rem; }
  .chat-send { width: 2.75rem; height: 2.75rem; }

  /* Pad the full-screen sheet for the notch / Dynamic Island (top) and the
     home indicator (bottom); needs viewport-fit=cover on the viewport meta. */
  .chat-header { padding-top: max(0.85rem, env(safe-area-inset-top)); }
  .chat-input  { padding-bottom: max(0.75rem, env(safe-area-inset-bottom)); }
  /* While the full-screen sheet is open, freeze the page behind it. chat.js toggles
     .chat-locked on <html>; scoped to mobile so the floating desktop panel still
     lets the page scroll underneath. */
  html.chat-locked, body.chat-locked { overflow: hidden; }
}

@media (prefers-reduced-motion: reduce) {
  .chat-launcher, .chat-chip, .chat-suggestion { transition: none; }
  .chat-faq-body, .chat-faq-arrow { transition: none; }
  .chat-status::after { animation: none; content: '…'; }
  .chat-spinner { animation: none; }
}
