/* `:host` so the variables also resolve when this sheet is linked inside the widget's shadow root,
   where `:root` matches nothing (the standalone page / iframe rely on `:root` = <html>). */
:root, :host {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --text: #1d2733;
  --muted: #6b7785;
  --border: #e2e6ea;
  --brand: #0a6cba;
  --brand-ink: #ffffff;
  --user-bg: #0a6cba;
  --user-ink: #ffffff;
  --assistant-bg: #eef1f4;
  --assistant-ink: #1d2733;
  --code-bg: rgba(16, 28, 43, 0.08);
  --shadow: 0 1px 3px rgba(16, 28, 43, 0.08), 0 8px 24px rgba(16, 28, 43, 0.06);
}

@media (prefers-color-scheme: dark) {
  :root, :host {
    --bg: #11161c;
    --panel: #171d25;
    --text: #e6ebf1;
    --muted: #9aa6b3;
    --border: #28323d;
    --brand: #3a9be0;
    /* Dark mode lightens --brand so it reads as a link/accent on a dark panel, which leaves white text
       on a brand FILL at only 3.02:1 — enough for a component boundary (3:1) but not for label text
       (4.5:1, and 15px/600 is not WCAG "large"). Flip the ink to a deep navy instead: 5.84:1 on
       #3a9be0. --brand-ink is only ever used as text on a --brand fill (send, confirm-yes,
       schedule-yes, join-now, sign-in), so this one token fixes every white-on-brand label. DAS-176. */
    --brand-ink: #0b1a26;
    --assistant-bg: #1f2731;
    --assistant-ink: #e6ebf1;
    --code-bg: rgba(255, 255, 255, 0.1);
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  display: flex;
  justify-content: center;
}

.chat {
  position: relative; /* containing block for the in-panel attachment lightbox */
  /* Size container for the composer's narrow-width rules (DAS-162). The composer has to react to the
     width of THIS panel, not the viewport: the embedded widget is ~343px inside a 375px phone, but it
     is equally narrow when a desktop user drags the panel down to its 320px minimum on a 1920px screen.
     A viewport media query gets the second case wrong in both directions, so query the container. */
  container: chat / inline-size;
  width: min(820px, 100%);
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
}

.chat__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}

.chat__brand { display: flex; align-items: center; gap: 12px; }

.chat__logo {
  width: 38px; height: 38px;
  border-radius: 9px;
  object-fit: contain;
  flex: none;
}

.chat__title { margin: 0; font-size: 16px; }
.chat__subtitle { margin: 2px 0 0; font-size: 12.5px; color: var(--muted); }

.chat__badge {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 9px;
}

.chat__log {
  flex: 1;
  /* min-height:0 lets this flex child shrink below its content so it actually scrolls,
     instead of growing and overflowing the panel (which clips it → no scrollbar). */
  min-height: 0;
  overflow-y: auto;
  padding: 20px 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.msg { display: flex; }
.msg--user { justify-content: flex-end; }
.msg--assistant { justify-content: flex-start; }

.msg__bubble {
  max-width: 78%;
  padding: 10px 14px;
  border-radius: 14px;
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.msg--user .msg__bubble {
  background: var(--user-bg);
  color: var(--user-ink);
  border-bottom-right-radius: 4px;
}

.msg--assistant .msg__bubble {
  background: var(--assistant-bg);
  color: var(--assistant-ink);
  border-bottom-left-radius: 4px;
}

/* Code inside a user message — a fenced ``` block or an auto-detected code paste (e.g. a gINT report
   expression). Monospace and horizontally scrollable so long unbroken lines stay readable rather than
   hard-wrapping. The bubble itself drops pre-wrap; the <pre> owns the whitespace. */
.msg__bubble--hascode { white-space: normal; }
.msg__linetext { white-space: pre-wrap; }
.msg__code {
  margin: 4px 0;
  padding: 8px 10px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.22);
  overflow-x: auto;
  white-space: pre;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px;
  line-height: 1.45;
}
.msg__code:first-child { margin-top: 0; }
.msg__code:last-child { margin-bottom: 0; }
.msg__code code { font: inherit; background: none; padding: 0; }

/* Rendered-Markdown bubbles (assistant / team member). The structural HTML carries the layout, so
   drop pre-wrap; keep tight, chat-appropriate spacing. */
.msg__bubble--md { white-space: normal; }
.msg__bubble--md > :first-child { margin-top: 0; }
.msg__bubble--md > :last-child { margin-bottom: 0; }
.msg__bubble--md p { margin: 0 0 8px; }
.msg__bubble--md ul, .msg__bubble--md ol { margin: 6px 0; padding-left: 22px; }
.msg__bubble--md li { margin: 2px 0; }
.msg__bubble--md li > ul, .msg__bubble--md li > ol { margin: 2px 0; }
.msg__bubble--md :is(h1, h2, h3, h4) { margin: 10px 0 4px; font-size: 1em; font-weight: 700; }
.msg__bubble--md a { color: var(--brand); }
.msg__bubble--md code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.88em;
  background: var(--code-bg);
  padding: 1px 5px;
  border-radius: 5px;
}
.msg__bubble--md pre {
  margin: 8px 0;
  padding: 10px 12px;
  background: var(--code-bg);
  border-radius: 8px;
  overflow-x: auto;
}
.msg__bubble--md pre code { background: none; padding: 0; font-size: 0.86em; }

/* Live team-member (relay) message — distinct from the AI assistant */
.msg--agent { flex-direction: column; align-items: flex-start; gap: 3px; }
.msg__name { font-size: 12px; font-weight: 600; color: var(--brand); padding-left: 2px; }
.msg--agent .msg__bubble {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--brand);
  border-bottom-left-radius: 4px;
}

/* System/relay status line */
.msg--system {
  align-self: center;
  justify-content: center;
  font-size: 12px;
  color: var(--muted);
  font-style: italic;
  text-align: center;
}

.msg__bubble--typing { display: inline-flex; gap: 5px; align-items: center; }
.dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--muted);
  opacity: 0.5;
  animation: blink 1.3s infinite ease-in-out;
}
.dot:nth-child(2) { animation-delay: 0.2s; }
.dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 0.9; } }

.chat__composer {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px 16px 6px;
  border-top: 1px solid var(--border);
}
.chat__composer--dragover { outline: 2px dashed var(--brand); outline-offset: -4px; border-radius: 8px; }

/* Row 1: the tool buttons (attach, code, read-aloud, speed, mic…). Kept on their own line so the
   text box below can be full width. */
.chat__tools { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }

/* Row 2: the wide message box with the Send button to its right. */
.chat__inputrow { display: flex; align-items: flex-end; gap: 10px; }

/* Attachment chips row (wraps onto its own line above the input). */
.chat__chips { width: 100%; display: flex; flex-wrap: wrap; gap: 6px; }
.chat__chips:empty { display: none; }

.chat__chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  max-width: 240px;
  font-size: 12px;
  color: var(--text);
  background: var(--assistant-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 4px 6px 4px 8px;
}
.chat__chip--error { border-color: #c0392b; color: #c0392b; }
.chat__chip-thumb {
  width: 22px; height: 22px; border-radius: 5px; object-fit: cover; flex: none;
  background: var(--border);
  display: grid; place-items: center; font-size: 13px;
}
.chat__chip-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat__chip-size { color: var(--muted); flex: none; }
.chat__chip-spin {
  width: 12px; height: 12px; flex: none; border-radius: 50%;
  border: 2px solid var(--border); border-top-color: var(--brand);
  animation: chip-spin 0.8s linear infinite;
}
@keyframes chip-spin { to { transform: rotate(360deg); } }
.chat__chip-remove {
  font: inherit; line-height: 1; border: none; background: transparent; color: var(--muted);
  cursor: pointer; padding: 0 2px; flex: none;
}
.chat__chip-remove:hover { color: var(--text); }

/* Attach button — sized to match the send button, and the shared base for every composer tool button
   (code, read-aloud, speed, stop, mic all reuse this class). The flex centring + min-height give them
   all one uniform 44px touch target regardless of glyph size or padding; without it the short-padded
   ones (notably the "1×" speed button) collapsed to ~14px tall, well under the 44px minimum. */
.chat__attach {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  line-height: 1;
  font-size: 18px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 9px 12px;
  cursor: pointer;
}
.chat__attach:hover { filter: brightness(0.98); }
.chat__attach:disabled { opacity: 0.5; cursor: default; }
.chat__attach-input { display: none; }

/* Voice controls (browser-native, Phase 1) — the read-aloud toggle + speak-to-type mic, both sized like
   the attach button. The toggle tints brand-blue when on; the mic pulses red while listening. */
.chat__voice--on { color: var(--brand); border-color: var(--brand); }
.chat__mic--listening {
  color: #c0392b; border-color: #c0392b;
  animation: mic-pulse 1.2s ease-in-out infinite;
}
@keyframes mic-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(192, 57, 43, 0.35); }
  50%      { box-shadow: 0 0 0 6px rgba(192, 57, 43, 0); }
}
@media (prefers-reduced-motion: reduce) { .chat__mic--listening { animation: none; } }

/* Clickable attachment thumbnail / icon in a composer chip. */
button.chat__chip-thumb { font: inherit; }
.chat__chip-thumb--clickable { cursor: pointer; padding: 0; border: 1px solid transparent; }
.chat__chip-thumb--clickable:hover { border-color: var(--brand); }
.chat__chip-thumb--clickable:focus-visible { outline: 2px solid var(--brand); outline-offset: 1px; }
/* Rendered PDF first-page thumbnail filling a composer chip's thumb box. The filled modifier switches
   the button from its grid (which centres the 📄 glyph) to a block box so the image's height:100%
   resolves against the fixed 22px square and the portrait page crops square instead of stretching it. */
.chat__chip-thumb--filled { display: block; overflow: hidden; }
.chat__chip-thumb-img { width: 100%; height: 100%; object-fit: cover; object-position: top; border-radius: 4px; display: block; }

/* Attachments shown under a sent user message. */
.msg__attachments { margin-top: 6px; display: flex; flex-wrap: wrap; gap: 5px; justify-content: flex-end; align-items: center; }
.msg__attachment {
  font-size: 11.5px;
  color: var(--user-ink);
  background: rgba(255, 255, 255, 0.18);
  border-radius: 8px;
  padding: 2px 8px;
}
/* Non-image attachment rendered as a clickable pill (opens the file in a new tab). */
.msg__attachment--btn { font: inherit; border: none; cursor: pointer; }
.msg__attachment--btn:hover { filter: brightness(1.12); }
/* Image attachment rendered as a clickable thumbnail (opens the lightbox). */
.msg__attachment-thumb {
  padding: 0; border: 1px solid var(--border); border-radius: 8px; background: none;
  cursor: pointer; line-height: 0; overflow: hidden; flex: none;
}
.msg__attachment-thumb img { width: 64px; height: 64px; object-fit: cover; display: block; }
/* PDF page thumbnails are portrait — show the top of the page and give it a paper background. */
.msg__attachment-thumb--pdf img { object-position: top; background: #fff; }
.msg__attachment-thumb:hover { border-color: var(--brand); }
.msg__attachment-thumb:focus-visible { outline: 2px solid var(--brand); outline-offset: 1px; }

/* Full-size image lightbox — scoped to the .chat panel (shadow root for the embedded widget). */
.chat__lightbox {
  position: absolute; inset: 0; z-index: 50;
  display: grid; place-items: center; padding: 24px;
  background: rgba(0, 0, 0, 0.72);
}
.chat__lightbox-img {
  max-width: 100%; max-height: 100%; object-fit: contain;
  border-radius: 6px; box-shadow: 0 8px 40px rgba(0, 0, 0, 0.5);
}
.chat__lightbox-close {
  position: absolute; top: 12px; right: 14px;
  width: 34px; height: 34px; border-radius: 50%;
  border: none; cursor: pointer; font-size: 18px; line-height: 1;
  color: #fff; background: rgba(0, 0, 0, 0.5);
}
.chat__lightbox-close:hover { background: rgba(0, 0, 0, 0.75); }

.chat__input {
  flex: 1;
  /* Grow with content (JS caps auto-grow at ~half the panel), and let the user drag it taller for a big
     paste. resize:vertical keeps the drag grip; the JS honours a manual height once set. */
  resize: vertical;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 11px 13px;
  min-height: 44px;
  max-height: 60vh;
}

/* Monospace "</>" composer button (chat-core.js builds it beside 📎). */
.chat__code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 14px; font-weight: 600; }

/* Reading-speed button ("1×"). Styled here rather than inline from chat-core.js so the narrow-width
   container rules below can actually shrink it — an inline style would outrank them (DAS-162). */
.chat__voice-speed { width: auto; min-width: 2.4em; padding: 0 8px; font-size: 12px; font-weight: 700; }
/* Keep the send button on the same 44px touch target as the tool row. */
.chat__send { min-height: 44px; }
.chat__input:focus { outline: 2px solid var(--brand); outline-offset: 0; }

.chat__send {
  font: inherit;
  font-weight: 600;
  color: var(--brand-ink);
  background: var(--brand);
  border: none;
  border-radius: 12px;
  padding: 11px 18px;
  cursor: pointer;
}
.chat__send:disabled { opacity: 0.5; cursor: default; }

/* Narrow panel (phone-width embed, or a shrunk desktop widget) — DAS-162.
   The two-row composer (DAS-129) already keeps the message box full width, so the remaining problem at
   this size is the TOOL row: six buttons at their comfortable desktop size wrap onto a second line and
   eat ~50px out of an already-short conversation area. Compact the buttons so the whole set stays on
   one line instead of hiding any of them behind an overflow menu — every control stays directly
   reachable (and keyboard/AT-reachable), which an overflow menu would cost us. Only horizontal padding
   and glyph size shrink: the 44px min-height touch target on .chat__attach is deliberately untouched. */
@container chat (max-width: 400px) {
  .chat__composer { padding-left: 12px; padding-right: 12px; }
  .chat__tools { gap: 6px; }
  .chat__attach { font-size: 16px; padding: 9px 7px; }
  .chat__code { font-size: 13px; }
  .chat__voice-speed { min-width: 1.9em; padding: 0 5px; font-size: 11px; }
  .chat__inputrow { gap: 8px; }
  .chat__input { padding: 11px 10px; }
  .chat__send { padding: 11px 12px; }
}

.chat__disclaimer {
  margin: 0;
  padding: 4px 16px 12px;
  font-size: 11.5px;
  color: var(--muted);
  text-align: center;
}

/* Build-version stamp — appended inline to the end of the disclaimer line (chat-core.js fills it from
   /api/config). Inherits the disclaimer's muted colour + size; just a quiet, non-wrapping suffix. */
.chat__version {
  margin-left: 6px;
  opacity: 0.75;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

/* Support-ticket confirmation card */
.confirm-card {
  align-self: flex-start;
  max-width: 88%;
  border: 1px solid var(--border);
  border-left: 3px solid var(--brand);
  border-radius: 12px;
  background: var(--assistant-bg);
  padding: 12px 14px;
  font-size: 14px;
}
.confirm-card__head { font-weight: 700; margin-bottom: 8px; }
.confirm-card__row { display: flex; gap: 8px; margin: 2px 0; }
.confirm-card__row > span { color: var(--muted); min-width: 64px; }
.confirm-card__desc {
  margin: 8px 0;
  padding: 8px 10px;
  background: var(--panel);
  border-radius: 8px;
  white-space: pre-wrap;
  max-height: 180px;
  overflow-y: auto;
}
.confirm-card__actions { display: flex; gap: 8px; margin-top: 6px; }
.confirm-card__yes, .confirm-card__no {
  font: inherit; font-weight: 600; border-radius: 9px; padding: 7px 14px; cursor: pointer; border: none;
}
.confirm-card__yes { background: var(--brand); color: var(--brand-ink); }
.confirm-card__no { background: transparent; color: var(--text); border: 1px solid var(--border); }
.confirm-card__yes:disabled, .confirm-card__no:disabled { opacity: 0.5; cursor: default; }
.confirm-card--done { opacity: 0.7; }

/* Background-action confirmation chip (auto CRM, raised ticket) */
.action-chip {
  align-self: center;
  font-size: 12px;
  color: var(--muted);
  background: var(--assistant-bg);
  border-radius: 999px;
  padding: 4px 12px;
  margin: 2px 0;
}
.action-chip a { color: var(--brand); }

/* Instant Teams meeting "Join now" card */
.join-now-card {
  align-self: flex-start;
  max-width: 88%;
  border: 1px solid var(--border);
  border-left: 3px solid var(--brand);
  border-radius: 12px;
  background: var(--assistant-bg);
  padding: 12px 14px;
}
.join-now-card__head { font-weight: 700; margin-bottom: 2px; }
.join-now-card__sub { font-size: 12.5px; color: var(--muted); margin-bottom: 10px; }
.join-now-card__btn {
  display: inline-block;
  font-weight: 600;
  color: var(--brand-ink);
  background: var(--brand);
  border-radius: 9px;
  padding: 8px 16px;
  text-decoration: none;
}
.join-now-card__btn:hover { filter: brightness(1.05); }

/* Schedule-a-meeting time picker */
.schedule-picker {
  align-self: flex-start;
  max-width: 88%;
  border: 1px solid var(--border);
  border-left: 3px solid var(--brand);
  border-radius: 12px;
  background: var(--assistant-bg);
  padding: 12px 14px;
  font-size: 14px;
}
.schedule-picker__head { font-weight: 700; margin-bottom: 8px; }
.schedule-picker__opt {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 8px;
  cursor: pointer;
}
.schedule-picker__opt:hover { background: var(--panel); }
.schedule-picker__actions { display: flex; gap: 8px; margin-top: 10px; }
.schedule-picker__yes, .schedule-picker__no {
  font: inherit; font-weight: 600; border-radius: 9px; padding: 7px 14px; cursor: pointer; border: none;
}
.schedule-picker__yes { background: var(--brand); color: var(--brand-ink); }
.schedule-picker__no { background: transparent; color: var(--text); border: 1px solid var(--border); }
.schedule-picker__yes:disabled, .schedule-picker__no:disabled { opacity: 0.5; cursor: default; }
.schedule-picker--done { opacity: 0.7; }

/* Header sign-in state (standalone page) */
.chat__session { display: flex; align-items: center; gap: 10px; }

.chat__user {
  font-size: 13px;
  color: var(--text);
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat__signin {
  font: inherit;
  font-weight: 600;
  font-size: 13px;
  color: var(--brand-ink);
  background: var(--brand);
  border: none;
  border-radius: 999px;
  padding: 7px 14px;
  cursor: pointer;
}
.chat__signin:hover { filter: brightness(1.05); }

.chat__link-btn {
  font: inherit;
  font-size: 12.5px;
  color: var(--muted);
  background: transparent;
  border: none;
  padding: 2px 4px;
  cursor: pointer;
  text-decoration: underline;
}
.chat__link-btn:hover { color: var(--text); }

/* OIDC callback popup page (/oauth/callback) */
.oauth {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
}
.oauth__panel { text-align: center; display: flex; flex-direction: column; align-items: center; gap: 14px; }
.oauth__logo { width: 40px; height: 40px; border-radius: 9px; object-fit: contain; }
.oauth__msg { margin: 0; color: var(--muted); font-size: 14px; }

/* ---- Chat history sidebar (DAS-78, standalone page) ----------------------- */
.layout {
  display: flex;
  width: min(1120px, 100%);
  height: 100dvh;
}

.history {
  width: 280px;
  flex: 0 0 280px;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  border-left: 1px solid var(--border);
  overflow: hidden;
}

.history__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 14px 14px 10px;
  border-bottom: 1px solid var(--border);
}

.history__heading {
  font-weight: 600;
  font-size: 13px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.history__new {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: 8px;
  padding: 5px 10px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}
.history__new:hover { border-color: var(--brand); color: var(--brand); }

.history__list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.history__empty {
  color: var(--muted);
  font-size: 13px;
  padding: 12px;
}

.history__item {
  display: flex;
  align-items: stretch;
  border-radius: 8px;
  margin-bottom: 2px;
}
.history__item:hover { background: var(--assistant-bg); }
.history__item--active { background: var(--assistant-bg); }
.history__item--active .history__title { color: var(--brand); }

.history__open {
  flex: 1;
  min-width: 0;
  text-align: left;
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px 10px;
  font: inherit;
  color: var(--text);
}

.history__title {
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history__meta {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.history__actions {
  display: none;
  align-items: center;
  gap: 2px;
  padding-right: 6px;
}
.history__item:hover .history__actions { display: flex; }

.history__act {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 13px;
  line-height: 1;
  padding: 4px;
  border-radius: 6px;
  opacity: 0.7;
}
.history__act:hover { opacity: 1; background: var(--border); }

/* Narrow screens: drop the sidebar (the chat stays full-width and usable). */
@media (max-width: 760px) {
  .history { display: none; }
}
