/**
 * hai-client — default UI.
 *
 * Styles ONLY what the framework renders: transcript blocks, the tool
 * provenance row, the surface container, and the shell that mountChat() builds.
 *
 * It deliberately does not style the inside of your surfaces. Your components
 * emit their own class names and your own stylesheet owns them — otherwise the
 * framework starts dictating what your UI looks like. They can use the tokens,
 * though: --hai-hover and --hai-selected are there for rows and pickers.
 *
 * Light and dark, following the system. Every colour is a custom property set
 * here at zero specificity, so a plain `:root` rule in your own sheet wins.
 * Give one value per scheme with light-dark(), behind a plain dark value for
 * the browsers that fall back to dark below:
 *
 *   :root { --hai-accent: #b07cff; }
 *   @supports (color: light-dark(#000, #fff)) {
 *     :root { --hai-accent: light-dark(#6d28d9, #b07cff); }
 *   }
 *
 * <html data-hai-theme="dark"> pins the page to one scheme, as does
 * `mountChat({ theme: "dark" })`. The pin is on the root because the tokens
 * are: a minifier that lowers light-dark() resolves them there.
 */

:where(:root) {
  color-scheme: light dark;

  --hai-bg: light-dark(#f6f7f9, #0c0f13);
  --hai-panel: light-dark(#ffffff, #141920);
  --hai-panel-2: light-dark(#f9fafb, #10141a);
  --hai-line: light-dark(#e1e5ea, #252c35);
  --hai-fg: light-dark(#161b22, #e3e8ee);
  --hai-dim: light-dark(#5b6675, #8a95a3);
  --hai-accent: light-dark(#2160e0, #7aa7ff);
  --hai-on-accent: light-dark(#ffffff, #0b1220);
  --hai-good: light-dark(#127a4c, #5ec597);
  --hai-warn: light-dark(#9a5b00, #e7b65c);
  --hai-bad: light-dark(#c23338, #f07b7b);
  --hai-user-bg: light-dark(#e7eefb, #1e2835);
  --hai-code-bg: light-dark(#f3f5f8, #0a0d11);
  --hai-shadow: 0 1px 2px light-dark(rgb(15 23 42 / .06), rgb(0 0 0 / .35));
  --hai-logo-ink: light-dark(#0f1115, #f3f2ee);
  --hai-logo-paper: light-dark(#f3f2ee, #0f1115);
  --hai-logo-hot: #ff5a1f;

  /* tints, derived — override the colour and these follow */
  --hai-line-soft: color-mix(in srgb, var(--hai-line) 60%, transparent);
  --hai-hover: color-mix(in srgb, var(--hai-fg) 6%, transparent);
  --hai-selected: color-mix(in srgb, var(--hai-good) 15%, transparent);
  --hai-accent-soft: color-mix(in srgb, var(--hai-accent) 14%, transparent);
  --hai-good-soft: color-mix(in srgb, var(--hai-good) 13%, transparent);
  --hai-warn-soft: color-mix(in srgb, var(--hai-warn) 12%, transparent);
  --hai-bad-soft: color-mix(in srgb, var(--hai-bad) 12%, transparent);

  --hai-radius: 12px;
  --hai-mono: ui-monospace, "SF Mono", "Cascadia Code", Menlo, Consolas, monospace;
  --hai-sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --hai-column: 780px;
  --hai-inspector-width: 400px;
}

:where(:root[data-hai-theme="light"]) { color-scheme: light; }
:where(:root[data-hai-theme="dark"]) { color-scheme: dark; }

/* A browser without light-dark() gets the dark scheme, not no colours at all. */
@supports not (color: light-dark(#000, #fff)) {
  :where(:root) {
    color-scheme: dark;
    --hai-bg: #0c0f13;
    --hai-panel: #141920;
    --hai-panel-2: #10141a;
    --hai-line: #252c35;
    --hai-fg: #e3e8ee;
    --hai-dim: #8a95a3;
    --hai-accent: #7aa7ff;
    --hai-on-accent: #0b1220;
    --hai-good: #5ec597;
    --hai-warn: #e7b65c;
    --hai-bad: #f07b7b;
    --hai-user-bg: #1e2835;
    --hai-code-bg: #0a0d11;
    --hai-shadow: 0 1px 2px rgb(0 0 0 / .35);
    --hai-logo-ink: #f3f2ee;
    --hai-logo-paper: #0f1115;
  }
}

/* the shell is the page */
:where(body:has(.hai-shell)) { margin: 0; }

/* ── shell ──────────────────────────────────────────────────────────── */
.hai-shell {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--hai-bg);
  color: var(--hai-fg);
  font: 14px/1.5 var(--hai-sans);
  -webkit-font-smoothing: antialiased;
}

.hai-icon { width: 16px; height: 16px; flex: none; }

.hai-top {
  flex: none;
  display: flex;
  align-items: center;
  gap: 12px;
  height: 52px;
  padding: 0 10px 0 16px;
  border-bottom: 1px solid var(--hai-line);
  background: var(--hai-panel);
}
.hai-brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.hai-logo { flex: none; }
.hai-logo-ink { fill: var(--hai-logo-ink); }
.hai-logo-paper { fill: var(--hai-logo-paper); }
.hai-logo-paper-dim { fill: var(--hai-logo-paper); opacity: .5; }
.hai-logo-hot { fill: var(--hai-logo-hot); }
.hai-mark { width: 20px; height: 20px; }
.hai-title { font-size: 15px; font-weight: 650; letter-spacing: -.01em; white-space: nowrap; }
.hai-subtitle {
  color: var(--hai-dim);
  font-size: 13px;
  padding-left: 10px;
  border-left: 1px solid var(--hai-line);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.hai-actions { margin-left: auto; display: flex; align-items: center; gap: 4px; flex: none; }
.hai-model {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-right: 6px;
  padding: 3px 9px;
  border: 1px solid var(--hai-line);
  border-radius: 999px;
  font: 11.5px var(--hai-mono);
  color: var(--hai-dim);
}
.hai-model::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--hai-good); }
.hai-model[hidden] { display: none; }

.hai-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 32px;
  padding: 0 10px;
  border: none;
  border-radius: 8px;
  background: none;
  color: var(--hai-dim);
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
}
.hai-btn:hover:not(:disabled) { background: var(--hai-hover); color: var(--hai-fg); }
.hai-btn:disabled { opacity: .4; cursor: default; }
.hai-btn[aria-expanded="true"] { color: var(--hai-fg); background: var(--hai-hover); }
.hai-ctx-count { font: 11.5px var(--hai-mono); color: var(--hai-dim); }
.hai-ctx-count:empty { display: none; }

.hai-icon-btn {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border: none;
  border-radius: 8px;
  background: none;
  color: var(--hai-dim);
  cursor: pointer;
}
.hai-icon-btn:hover { background: var(--hai-hover); color: var(--hai-fg); }

/* Clips the drawer while it waits off the right edge. `clip`, not just
   `hidden`: a hidden box still scrolls, so bringing anything into view
   centred, as find in page does, slid the whole chat sideways and left it
   there. `hidden` stays for browsers without `clip`. */
.hai-main { flex: 1; display: grid; grid-template-columns: minmax(0, 1fr); min-height: 0; position: relative; overflow: hidden; overflow: clip; }

/* the conversation is a centred column; the scrollbar stays at the edge */
.hai-chat {
  --hai-gutter: max(16px, calc((100% - var(--hai-column)) / 2));
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  transition: margin-right .25s cubic-bezier(.2, .8, .2, 1), opacity .25s;
}
/* behind the drawer on a narrow screen, out of reach until it closes */
.hai-chat[inert] { opacity: .4; }
/* on a wide screen the chat makes room for the open drawer */
@media (min-width: 900px) {
  .hai-main.inspector-open > .hai-chat { margin-right: var(--hai-inspector-width); }
}
.hai-transcript {
  flex: 1;
  overflow-y: auto;
  padding: 28px var(--hai-gutter) 8px;
  scrollbar-width: thin;
  scrollbar-color: var(--hai-line) transparent;
}

/* ── empty state ────────────────────────────────────────────────────── */
.hai-empty { color: var(--hai-dim); }
.hai-empty p { margin: 0; max-width: 54ch; font-size: 15px; line-height: 1.6; }
.hai-empty-mark { width: 44px; height: 44px; margin-bottom: 20px; }
/* alone, it greets; after what init showed, it stays out of the way */
.hai-empty:first-child {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding-top: clamp(24px, 14vh, 140px);
}
.hai-empty:not(:first-child) .hai-empty-mark { display: none; }

.hai-suggestions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 20px; }
.hai-empty:first-child .hai-suggestions { justify-content: center; }
.hai-empty:not(:first-child) .hai-suggestions { margin-top: 12px; }
.hai-suggestion {
  padding: 7px 14px;
  border: 1px solid var(--hai-line);
  border-radius: 999px;
  background: var(--hai-panel);
  color: var(--hai-fg);
  font: inherit;
  font-size: 13.5px;
  cursor: pointer;
  transition: border-color .15s, background-color .15s;
}
.hai-suggestion:hover { border-color: var(--hai-accent); background: var(--hai-accent-soft); }

/* ── transcript blocks ──────────────────────────────────────────────── */
.hai-block { margin: 0 0 18px; }
.hai-user .hai-body,
.hai-assistant .hai-body {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  /* each paragraph takes its own direction: a reply can mix scripts */
  unicode-bidi: plaintext;
  font-size: 15px;
}
.hai-assistant { max-width: 72ch; line-height: 1.65; }
.hai-user { display: flex; justify-content: flex-end; }
.hai-user .hai-body {
  max-width: min(85%, 62ch);
  padding: 9px 14px;
  border-radius: 18px 18px 6px 18px;
  background: var(--hai-user-bg);
}

/* an answer given in a surface — it went back to the model as the tool result */
.hai-interaction { display: flex; align-items: flex-start; gap: 10px; max-width: 72ch; font-size: 13.5px; color: var(--hai-dim); }
.hai-arrow {
  flex: none;
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  margin-top: -1px;
  border-radius: 50%;
  background: var(--hai-good-soft);
  color: var(--hai-good);
}
.hai-arrow .hai-icon { width: 13px; height: 13px; }

.hai-error,
.hai-expired {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 11px 14px;
  border: 1px solid;
  border-radius: var(--hai-radius);
}
.hai-error .hai-icon,
.hai-expired .hai-icon { margin-top: 2px; }
.hai-error {
  border-color: color-mix(in srgb, var(--hai-bad) 35%, transparent);
  background: var(--hai-bad-soft);
  color: var(--hai-bad);
  font: 12.5px/1.55 var(--hai-mono);
}
.hai-error .hai-body { overflow-wrap: anywhere; }

/* tool provenance row — chrome for the surface directly beneath it */
.hai-tool {
  border: 1px solid var(--hai-line);
  border-radius: var(--hai-radius);
  background: var(--hai-panel-2);
  overflow: hidden;
}
.hai-tool:has(+ .hai-surface) { margin-bottom: 0; border-bottom-left-radius: 0; border-bottom-right-radius: 0; }
.hai-tool summary {
  display: flex;
  align-items: center;
  gap: 9px;
  min-height: 36px;
  padding: 7px 12px;
  box-sizing: border-box;
  cursor: pointer;
  list-style: none;
  font-size: 12.5px;
  user-select: none;
}
.hai-tool summary::-webkit-details-marker { display: none; }
.hai-tool summary:hover { background: var(--hai-hover); }
.hai-ticon { flex: none; display: grid; place-items: center; width: 16px; height: 16px; color: var(--hai-dim); }
.hai-ticon .hai-icon { width: 14px; height: 14px; }
.hai-tname { font: 500 12.5px var(--hai-mono); }
.hai-targs {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font: 12px var(--hai-mono);
  color: var(--hai-dim);
}
.hai-tstatus { color: var(--hai-dim); white-space: nowrap; font-variant-numeric: tabular-nums; }
.hai-chevron { width: 14px; height: 14px; color: var(--hai-dim); transition: transform .15s; }
.hai-tool[open] .hai-chevron { transform: rotate(90deg); }

.hai-status-ok .hai-ticon { color: var(--hai-good); }
.hai-status-running .hai-tstatus { color: var(--hai-accent); }
.hai-status-awaiting .hai-tstatus { color: var(--hai-warn); }
.hai-status-error .hai-ticon,
.hai-status-error .hai-tstatus { color: var(--hai-bad); }
/* the component is the question: the turn is parked on it */
.hai-tool.hai-status-awaiting { background: var(--hai-warn-soft); }
.hai-tool.hai-status-awaiting,
.hai-tool.hai-status-awaiting + .hai-surface { border-color: color-mix(in srgb, var(--hai-warn) 45%, var(--hai-line)); }

.hai-tool-detail { padding: 0 12px 12px; border-top: 1px solid var(--hai-line); }
.hai-label { margin: 10px 0 5px; font: 11px var(--hai-mono); color: var(--hai-dim); }
.hai-code,
.hai-digest {
  margin: 0;
  padding: 9px 11px;
  max-height: 200px;
  overflow: auto;
  border: 1px solid var(--hai-line);
  border-radius: 8px;
  background: var(--hai-code-bg);
  font: 12px/1.55 var(--hai-mono);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.hai-digest { color: var(--hai-good); }

.hai-spinner {
  width: 12px;
  height: 12px;
  box-sizing: border-box;
  border: 2px solid var(--hai-accent-soft);
  border-top-color: var(--hai-accent);
  border-radius: 50%;
  animation: hai-spin .8s linear infinite;
}
.hai-pulse {
  flex: none;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--hai-warn);
  animation: hai-pulse 1.8s ease-out infinite;
}

/* surface container — your component mounts inside this */
.hai-surface {
  margin-bottom: 18px;
  overflow: hidden;
  border: 1px solid var(--hai-line);
  border-radius: var(--hai-radius);
  background: var(--hai-panel);
  box-shadow: var(--hai-shadow);
}
.hai-tool + .hai-surface { border-top: none; border-top-left-radius: 0; border-top-right-radius: 0; }
.hai-skeleton { display: grid; gap: 9px; padding: 14px; }
.hai-skeleton-label { margin-bottom: 2px; font: 12px var(--hai-mono); color: var(--hai-dim); }
.hai-shimmer {
  height: 10px;
  border-radius: 5px;
  background: linear-gradient(90deg, var(--hai-hover), var(--hai-line), var(--hai-hover)) 0 0 / 200% 100%;
  animation: hai-shimmer 1.4s ease-in-out infinite;
}
.hai-shimmer:nth-child(3) { width: 82%; }
.hai-shimmer:nth-child(4) { width: 60%; }
.hai-surface-error {
  margin-bottom: 18px;
  padding: 11px 14px;
  border: 1px solid color-mix(in srgb, var(--hai-bad) 35%, transparent);
  border-radius: var(--hai-radius);
  background: var(--hai-bad-soft);
  color: var(--hai-bad);
  font: 12.5px var(--hai-mono);
}

/* out of date — the runtime has made the surface inert; this only shows it */
.hai-surface[data-expired] { opacity: .45; filter: grayscale(.7); }
.hai-expired {
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 12px;
  border-color: color-mix(in srgb, var(--hai-warn) 45%, transparent);
  background: var(--hai-warn-soft);
  color: var(--hai-warn);
}
.hai-expired .hai-icon { margin-top: 0; }
.hai-expired .hai-body { flex: 1 1 30ch; }
.hai-new {
  padding: 7px 14px;
  border: none;
  border-radius: 8px;
  background: var(--hai-warn);
  color: var(--hai-on-accent);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

/* ── status + composer ──────────────────────────────────────────────── */
.hai-status {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 22px;
  padding: 0 var(--hai-gutter) 6px;
  font-size: 12.5px;
  color: var(--hai-dim);
}
.hai-status[data-status="awaiting"] { color: var(--hai-warn); }
.hai-dots { display: inline-flex; gap: 3px; }
.hai-dots i { width: 5px; height: 5px; border-radius: 50%; background: currentColor; animation: hai-blink 1.2s infinite both; }
.hai-dots i:nth-child(2) { animation-delay: .2s; }
.hai-dots i:nth-child(3) { animation-delay: .4s; }

.hai-composer { flex: none; padding: 0 var(--hai-gutter) 18px; }
.hai-composer-box {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 7px 7px 7px 14px;
  border: 1px solid var(--hai-line);
  border-radius: 16px;
  background: var(--hai-panel);
  box-shadow: var(--hai-shadow);
  transition: border-color .15s, box-shadow .15s;
}
.hai-composer-box:focus-within { border-color: var(--hai-accent); box-shadow: 0 0 0 3px var(--hai-accent-soft); }
.hai-input {
  flex: 1;
  box-sizing: border-box;
  min-height: 34px;
  max-height: 200px;
  padding: 6px 0;
  overflow-y: auto;
  resize: none;
  border: none;
  outline: none;
  background: transparent;
  color: var(--hai-fg);
  font: inherit;
  font-size: 15px;
  line-height: 1.45;
}
.hai-input::placeholder { color: var(--hai-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hai-input:disabled { cursor: not-allowed; opacity: .6; }
.hai-send {
  flex: none;
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 10px;
  background: var(--hai-accent);
  color: var(--hai-on-accent);
  cursor: pointer;
  transition: opacity .15s;
}
.hai-send .hai-icon { width: 18px; height: 18px; }
.hai-send:disabled { opacity: .35; cursor: default; }

/* ── model context debug drawer ─────────────────────────────────────── */
/* Closed, it waits off the right edge, out of the tab order and the
   accessibility tree; open, it slides in, over the chat on a narrow screen. */
.hai-inspector {
  position: absolute;
  inset: 0 0 0 auto;
  z-index: 10;
  display: flex;
  flex-direction: column;
  width: min(100%, var(--hai-inspector-width));
  min-height: 0;
  border-left: 1px solid var(--hai-line);
  background: var(--hai-panel-2);
  box-shadow: -16px 0 32px -12px rgb(0 0 0 / .25);
  transform: translateX(100%);
  visibility: hidden;
  transition: transform .25s cubic-bezier(.2, .8, .2, 1), visibility 0s .25s;
}
.hai-main.inspector-open > .hai-inspector {
  transform: none;
  visibility: visible;
  transition: transform .25s cubic-bezier(.2, .8, .2, 1), visibility 0s;
}
.hai-inspector-head { display: flex; align-items: center; gap: 8px; padding: 10px 10px 10px 16px; border-bottom: 1px solid var(--hai-line); }
.hai-inspector-title { flex: 1; display: flex; flex-direction: column; font-size: 13px; font-weight: 600; }
.hai-inspector-title span { color: var(--hai-dim); font-size: 12px; font-weight: 400; }
.hai-inspector-body {
  flex: 1;
  overflow-y: auto;
  padding: 0 16px 16px;
  scrollbar-width: thin;
  scrollbar-color: var(--hai-line) transparent;
}
.hai-inspector-empty { margin: 16px 0; color: var(--hai-dim); font-size: 13px; }

/* the numbers stay in view as the context scrolls under them */
.hai-ctx {
  position: sticky;
  top: 0;
  z-index: 1;
  padding: 14px 0 12px;
  border-bottom: 1px solid var(--hai-line);
  background: var(--hai-panel-2);
}
.hai-ctx-headline { display: flex; align-items: baseline; gap: 8px; margin-bottom: 8px; font-size: 12.5px; color: var(--hai-dim); }
.hai-ctx-pct { font-size: 26px; font-weight: 650; letter-spacing: -.02em; color: var(--hai-good); font-variant-numeric: tabular-nums; }
.hai-meter { display: flex; height: 6px; margin-bottom: 10px; overflow: hidden; border-radius: 3px; background: var(--hai-good); }
.hai-meter-model { min-width: 2px; background: var(--hai-accent); }
.hai-ctx-stat { display: flex; justify-content: space-between; padding: 1px 0; font: 12px var(--hai-mono); }
.hai-ctx-label { display: inline-flex; align-items: center; gap: 7px; color: var(--hai-dim); }
.hai-ctx-tok { font-variant-numeric: tabular-nums; }
.hai-key { width: 8px; height: 8px; border-radius: 2px; }
.hai-key-model { background: var(--hai-accent); }
.hai-key-ui { background: var(--hai-good); }

.hai-msg { margin-top: 14px; padding-left: 11px; border-left: 2px solid var(--hai-line); }
.hai-role { margin-bottom: 4px; font: 600 10px var(--hai-mono); text-transform: uppercase; letter-spacing: .08em; color: var(--hai-dim); }
.hai-mbody { display: grid; gap: 6px; font: 12px/1.55 var(--hai-mono); white-space: pre-wrap; overflow-wrap: anywhere; }
.hai-tu,
.hai-tr { padding: 6px 8px; border-radius: 6px; }
.hai-tu { background: var(--hai-accent-soft); color: var(--hai-accent); }
.hai-tr { background: var(--hai-good-soft); color: var(--hai-good); }
.hai-kind { display: block; margin-bottom: 2px; font-size: 10.5px; opacity: .75; }

/* ── narrow screens ─────────────────────────────────────────────────── */
@media (max-width: 899.98px) {
  .hai-subtitle { display: none; }
  .hai-btn { padding: 0 8px; }
  .hai-btn-label {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }
}
@media (max-width: 599.98px) {
  .hai-model { display: none; }
  .hai-transcript { padding-top: 18px; }
  .hai-composer { padding-bottom: 12px; }
}

/* ── focus, motion ──────────────────────────────────────────────────── */
.hai-btn:focus-visible,
.hai-icon-btn:focus-visible,
.hai-send:focus-visible,
.hai-suggestion:focus-visible,
.hai-new:focus-visible { outline: 2px solid var(--hai-accent); outline-offset: 2px; }
.hai-tool summary:focus-visible { outline: 2px solid var(--hai-accent); outline-offset: -2px; }

@keyframes hai-spin { to { transform: rotate(360deg); } }
@keyframes hai-pulse {
  0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--hai-warn) 55%, transparent); }
  100% { box-shadow: 0 0 0 7px transparent; }
}
@keyframes hai-blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
@keyframes hai-shimmer { from { background-position: 100% 0; } to { background-position: -100% 0; } }

@media (prefers-reduced-motion: reduce) {
  .hai-spinner,
  .hai-pulse,
  .hai-dots i,
  .hai-shimmer { animation: none; }
  .hai-chevron,
  .hai-suggestion,
  .hai-composer-box,
  .hai-chat,
  .hai-inspector,
  .hai-main.inspector-open > .hai-inspector { transition: none; }
}
