/* Lead research chat (/leads_agent/). One column, ChatGPT-style: centered start screen, then a thread with the message
   box docked at the bottom. Loads after workspace.css and agent-chat.css and uses their --ws-* tokens. */
.chat-page { height: 100dvh; overflow: hidden; }
.visually-hidden, .composer-label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip-link { position: fixed; top: 12px; left: 12px; z-index: 100; padding: 12px; border-radius: 8px; color: var(--ws-on-accent); background: var(--ws-accent); transform: translateY(-200%); }
.skip-link:focus { transform: none; }

.chat-main { position: relative; display: flex; flex-direction: column; height: 100dvh; min-height: 0; }

/* Header: brand, title, help. Nothing else. */
.chat-header { display: flex; align-items: center; gap: 12px; min-height: 60px; padding: 8px clamp(14px, 3vw, 24px); }
.chat-header .ws-brand { min-height: 44px; }
.chat-title { flex: 1; margin: 0; color: var(--ws-muted); font-family: var(--ws-display); font-size: .9375rem; font-weight: 600; text-align: center; }
.chat-header .agent-help { position: relative; }
.chat-header .agent-help summary { display: grid; place-items: center; width: 44px; height: 44px; list-style: none; cursor: pointer; }
.chat-header .agent-help summary::-webkit-details-marker { display: none; }
.chat-header .agent-help summary span { display: grid; place-items: center; border: 1px solid var(--ws-line-strong); }
.chat-header .agent-help-panel { position: absolute; z-index: 30; top: calc(100% + 8px); right: 0; width: min(420px, calc(100vw - 28px)); max-height: 70dvh; overflow-y: auto; box-shadow: 0 20px 50px rgba(0, 0, 0, .45); }
.agent-help-panel h2 { margin: 0 0 8px; font-size: 1.0625rem; }
.agent-help-panel ol { margin: 0 0 12px; padding-left: 20px; color: var(--ws-muted); }

/* Thread */
main.chat-thread { flex: 1; width: auto; min-height: 0; margin: 0; overflow-y: auto; scroll-behavior: smooth; }
.chat-thread:focus { outline: none; }
.thread-inner { width: min(100% - 32px, 760px); margin: 0 auto; padding: 16px 0 28px; }
.setup-notice { margin-bottom: 20px; padding: 18px 20px; border: 1px solid #f4cf7466; border-radius: 14px; background: #f4cf7414; }
.setup-notice h2 { margin: 0 0 6px; font-size: 1rem; }
.setup-notice p { margin: 4px 0 0; color: var(--ws-muted); }

/* Start screen: the question and the message box sit in the middle of the page. */
.hero { text-align: center; }
.hero h1 { margin: 0 0 10px; font-family: var(--ws-display); font-size: clamp(1.625rem, 4vw, 2.125rem); font-weight: 600; letter-spacing: -.03em; line-height: 1.2; }
.hero p { max-width: 52ch; margin: 0 auto; color: var(--ws-muted); font-size: .9375rem; line-height: 1.6; }
/* On the start screen the whole column scrolls when it's taller than the screen, so nothing shrinks or overlaps. */
.is-empty .chat-main { overflow-y: auto; }
.is-empty main.chat-thread { flex: 0 0 auto; margin-top: auto; overflow: visible; }
.is-empty .thread-inner { padding-bottom: 20px; }
.is-empty .composer { margin-bottom: auto; }
body:not(.is-empty) .hero, body:not(.is-empty) .starters { display: none; }
.starters { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin-top: 14px; }
.starter { min-height: 40px; padding: 8px 14px; border: 1px solid var(--ws-line); border-radius: 999px; color: var(--ws-muted); background: transparent; font: inherit; font-size: .8125rem; text-align: left; cursor: pointer; }
.starter:hover { border-color: var(--ws-accent); color: var(--ws-text); background: var(--ws-accent-soft); }

/* Messages */
.messages { display: grid; gap: 22px; }
.msg-user { justify-self: end; max-width: 80%; padding: 10px 16px; border-radius: 18px; color: var(--ws-text); background: var(--ws-raised); white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-agent { display: flex; gap: 12px; align-items: flex-start; min-width: 0; }
.msg-agent .agent-avatar { width: 32px; height: 32px; margin-top: 2px; border-radius: 50%; font-size: .625rem; }
.msg-agent-body { flex: 1; min-width: 0; }

.run { margin: 2px 0 10px; }
.run[hidden] { display: none; }
.run-summary { display: inline-flex; align-items: center; gap: 8px; min-height: 34px; padding: 4px 12px 4px 10px; border: 1px solid var(--ws-line); border-radius: 999px; color: var(--ws-muted); background: transparent; font: inherit; font-size: .8125rem; cursor: pointer; }
.run-summary[hidden] { display: none; }
.run-summary:hover { border-color: var(--ws-line-strong); color: var(--ws-text); }
.run-summary .chevron { transition: transform .15s; }
.run-summary[aria-expanded="true"] .chevron { transform: rotate(90deg); }
.run.is-running .run-summary { color: var(--ws-accent); border-color: color-mix(in srgb, var(--ws-accent) 40%, transparent); }
.run-steps { display: grid; gap: 2px; margin: 8px 0 0; padding: 0 0 0 4px; list-style: none; }
.run-steps[hidden] { display: none; }
.step { display: flex; align-items: center; gap: 10px; min-height: 30px; color: var(--ws-muted); font-size: .875rem; }
.step-icon { display: grid; flex-shrink: 0; width: 22px; height: 22px; place-items: center; border: 1px solid var(--ws-line); border-radius: 7px; font-size: .75rem; }
.step-text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.step-text b { color: var(--ws-text); font-weight: 500; }
.step-state { flex-shrink: 0; margin-left: auto; font-size: .75rem; }
.step.is-running .step-state { position: relative; width: 7px; height: 7px; border-radius: 50%; background: #5fe08a; }
.step.is-running .step-state::after { content: ""; position: absolute; inset: -3px; border: 1px solid #5fe08a; border-radius: 50%; animation: agent-pulse 1.8s ease-out infinite; }
.step.is-done .step-state { color: var(--ws-accent); }
.step.is-error .step-state, .step.is-stopped .step-state { color: #ffb4a8; }
.run-thinking { margin: 6px 0 0 4px; color: var(--ws-muted); font-size: .8125rem; }
.run-thinking[hidden] { display: none; }
.run-thinking summary { display: flex; gap: 6px; min-height: 30px; align-items: center; cursor: pointer; list-style: none; }
.run-thinking summary::-webkit-details-marker { display: none; }
.run-thinking summary .latest { min-width: 0; overflow: hidden; font-style: italic; text-overflow: ellipsis; white-space: nowrap; opacity: .85; }
.run-thinking[open] summary .latest { display: none; }
.run-thinking .thoughts { margin: 4px 0 0; padding: 8px 12px; border-left: 2px solid var(--ws-line-strong); white-space: pre-wrap; }
.run-thinking .thoughts p { margin: 0 0 8px; }

.reply { color: var(--ws-text); font-size: .9375rem; line-height: 1.65; overflow-wrap: anywhere; }
.reply > :first-child { margin-top: 0; }
.reply p, .reply ul, .reply ol, .reply pre, .reply blockquote, .reply .table-wrap { margin: 0 0 12px; }
.reply h1, .reply h2, .reply h3, .reply h4 { margin: 20px 0 8px; font-family: var(--ws-display); font-weight: 600; line-height: 1.3; }
.reply h1 { font-size: 1.25rem; } .reply h2 { font-size: 1.125rem; } .reply h3, .reply h4 { font-size: 1rem; }
.reply ul, .reply ol { padding-left: 22px; }
.reply li { margin: 3px 0; }
.reply a { color: var(--ws-accent); }
.reply code { padding: 1px 5px; border-radius: 5px; background: var(--ws-raised); font-size: .875em; }
.reply pre { max-height: 360px; padding: 12px 14px; overflow: auto; border: 1px solid var(--ws-line); border-radius: 10px; background: var(--ws-input); }
.reply pre code { padding: 0; background: none; white-space: pre; }
.reply blockquote { padding-left: 12px; border-left: 3px solid var(--ws-line-strong); color: var(--ws-muted); }
.reply hr { margin: 18px 0; border: 0; border-top: 1px solid var(--ws-line); }
.table-wrap { max-height: min(70vh, 640px); overflow: auto; border: 1px solid var(--ws-line); border-radius: 12px; background: var(--ws-bg); overscroll-behavior: contain; }
/* Tables size to their content and scroll sideways; words never break mid-word. */
.reply table { width: max-content; min-width: 100%; border-collapse: separate; border-spacing: 0; font-size: .8125rem; line-height: 1.45; overflow-wrap: normal; word-break: normal; }
.reply th, .reply td { max-width: 30ch; padding: 9px 12px; border-bottom: 1px solid var(--ws-line); text-align: left; vertical-align: top; }
.reply th { position: sticky; top: 0; z-index: 1; color: var(--ws-muted); background: var(--ws-surface); font-weight: 600; white-space: nowrap; }
.reply tbody tr:hover td { background: var(--ws-accent-soft); }
.reply tbody tr:last-child td { border-bottom: 0; }
.reply td:first-child { color: var(--ws-muted); font-variant-numeric: tabular-nums; }
/* On wide screens a table can use more than the text column. */
@media (min-width: 1100px) {
  .msg-agent-body .table-wrap { position: relative; left: 50%; width: min(calc(100vw - 96px), 1180px); max-width: none; transform: translateX(-50%); }
}

/* leads.csv as a file card instead of raw text. */
.file { margin: 4px 0 14px; }
.file-card { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; padding: 12px 14px; border: 1px solid var(--ws-line-strong); border-radius: 14px; background: var(--ws-surface); }
.file-icon { display: grid; flex-shrink: 0; width: 40px; height: 40px; place-items: center; border-radius: 10px; color: var(--ws-on-accent); background: var(--ws-accent); font-size: .6875rem; font-weight: 800; letter-spacing: .04em; }
.file-text { display: grid; flex: 1; min-width: 140px; line-height: 1.35; }
.file-text b { font-weight: 600; }
.file-text span { color: var(--ws-muted); font-size: .8125rem; }
.file-card.is-writing .file-icon { background: var(--ws-raised); color: var(--ws-accent); }
.file-card .reply-actions { margin: 0; }
.file-card .copy-fallback { flex-basis: 100%; }
.file-preview { margin-top: 6px; font-size: .8125rem; }
.file-preview summary { display: inline-flex; min-height: 32px; align-items: center; color: var(--ws-muted); cursor: pointer; }
.file-preview summary:hover { color: var(--ws-text); }
.reply .file-preview pre { margin: 6px 0 0; }
.reply.is-streaming > :last-child::after { content: "▍"; margin-left: 2px; color: var(--ws-accent); animation: caret 1s steps(2) infinite; }
@keyframes caret { 50% { opacity: 0; } }

.notice { margin: 8px 0 0; padding: 10px 14px; border: 1px solid #f4cf7455; border-radius: 10px; color: var(--ws-text); background: #f4cf7412; font-size: .875rem; }
.notice.is-error { border-color: #ffb4a855; background: #ffb4a812; }
.reply-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.reply-actions button { min-height: 40px; padding: 0 14px; border: 1px solid var(--ws-line-strong); border-radius: 10px; color: var(--ws-text); background: var(--ws-raised); font: inherit; font-size: .875rem; font-weight: 600; cursor: pointer; }
.reply-actions button:hover { border-color: var(--ws-accent); }
.reply-actions .is-copied { color: var(--ws-accent); }
.copy-fallback { width: 100%; height: 160px; margin-top: 8px; padding: 10px; border: 1px solid var(--ws-line-strong); border-radius: 10px; color: var(--ws-text); background: var(--ws-input); font: .8125rem/1.5 ui-monospace, monospace; }

.jump-latest { position: absolute; left: 50%; bottom: calc(100% + 10px); z-index: 10; min-height: 40px; padding: 0 16px; border: 1px solid var(--ws-line-strong); border-radius: 999px; color: var(--ws-text); background: var(--ws-raised); font: inherit; font-size: .875rem; box-shadow: 0 10px 30px rgba(0, 0, 0, .4); transform: translateX(-50%); cursor: pointer; }

/* Message box */
.composer { position: relative; width: min(100% - 32px, 760px); margin: 0 auto; padding: 6px 0 4px; }
.composer-box { display: flex; align-items: flex-end; gap: 8px; padding: 6px 6px 6px 18px; border: 1px solid var(--ws-line-strong); border-radius: 26px; background: var(--ws-surface); box-shadow: 0 10px 30px rgba(0, 0, 0, .25); }
.composer-box:focus-within { border-color: color-mix(in srgb, var(--ws-accent) 70%, transparent); }
.composer-box:has(textarea:focus-visible) { outline: 2px solid color-mix(in srgb, var(--ws-accent) 45%, transparent); outline-offset: 3px; }
.composer textarea { box-sizing: border-box; flex: 1; height: 44px; min-height: 44px; max-height: 200px; padding: 11px 0; border: 0; resize: none; color: var(--ws-text); background: transparent; font: inherit; font-size: 1rem; line-height: 1.4; }
.composer textarea:focus-visible { outline: none; }
.composer textarea::placeholder { color: var(--ws-muted); opacity: .8; }
.send-button { display: grid; flex-shrink: 0; width: 40px; height: 40px; margin: 2px; place-items: center; border: 0; border-radius: 50%; color: var(--ws-on-accent); background: var(--ws-accent); cursor: pointer; }
.send-button svg { width: 18px; height: 18px; }
.send-button:disabled { opacity: .35; cursor: default; }
.send-button .icon-stop, .send-button.is-stop .icon-send { display: none; }
.send-button.is-stop .icon-stop { display: block; }
.send-button.is-stop { color: var(--ws-on-accent); background: var(--ws-text); }
/* Voice input: shown only where the browser supports speech recognition; red and pulsing while listening. */
.mic-button { display: grid; flex-shrink: 0; width: 40px; height: 40px; margin: 2px 0; place-items: center; border: 0; border-radius: 50%; color: var(--ws-muted); background: transparent; cursor: pointer; }
.mic-button[hidden] { display: none; }
.mic-button svg { width: 20px; height: 20px; }
.mic-button:hover:not(:disabled) { color: var(--ws-text); background: var(--ws-raised); }
.mic-button:disabled { opacity: .35; cursor: default; }
.mic-button.is-listening { color: var(--ws-on-accent); background: #e5484d; animation: mic-pulse 1.4s ease-in-out infinite; }
@keyframes mic-pulse { 0%, 100% { box-shadow: 0 0 0 0 #e5484d66; } 50% { box-shadow: 0 0 0 6px #e5484d00; } }
.composer-meta { display: flex; justify-content: center; gap: 12px; margin: 8px 4px 0; color: var(--ws-muted); font-size: .75rem; line-height: 1.5; text-align: center; }
.composer-count { flex-shrink: 0; }
.composer-count:empty { display: none; }
.composer-count.is-near { color: var(--ws-focus); }

.chat-footer { display: flex; flex-wrap: wrap; justify-content: center; gap: 4px 10px; padding: 6px 16px 10px; color: var(--ws-muted); font-size: .75rem; opacity: .8; }
.chat-footer a { display: inline-flex; min-height: 24px; align-items: center; color: var(--ws-muted); }
.chat-footer a:hover { color: var(--ws-accent); }
.is-empty .chat-footer { margin-top: 0; }

@media (max-width: 600px) {
  .chat-header .brand-text { display: none; }
  .msg-agent .agent-avatar { display: none; }
  .msg-user { max-width: 90%; }
  .starter { flex: 1 1 100%; }
}

@media (prefers-reduced-motion: reduce) {
  main.chat-thread { scroll-behavior: auto; }
  .step.is-running .step-state::after, .agent-pulse::after, .reply.is-streaming > :last-child::after, .mic-button.is-listening { animation: none; }
  .run-summary .chevron { transition: none; }
}
@media (forced-colors: active) {
  .send-button, .mic-button, .starter, .run-summary, .composer-box, .reply-actions button { border: 1px solid ButtonText; }
}
