/* Studio's Chat pane (static/chat.js).

   Sizes are Studio's own: the type roles and control sizes styles.css declares from
   `app/DesignSystem.json` in blueprint-ios-studio — body 13/18, small 12/16, caption
   11/14, badge 11/14, eyebrow 10/14, 4/8/12 spacing, a 28-point icon control, a 7-point
   field radius. Every colour is a role from styles.css (--ink, --panel, --chip, --ok-bg, …),
   which carries Studio's exact light and dark values, so the pane follows the page's
   theme without a rule of its own for dark.

   Nothing here is set from script except classes and the text area's `rows`
   attribute: the shell serves `style-src 'self'`, which refuses style attributes.

   The script adds `.chat-host` to the element it is given. That element must have a
   definite height — a stretched flex or grid item with `min-height: 0` — because the
   transcript scrolls inside the pane rather than growing it. Studio's chat pane is
   300px wide at minimum and 340px ideally. Whether the host is shown at all is the
   workspace's (the stage panes menu), so this sets no `display` of its own: one here
   would come after the workspace's rule at the same specificity and show Chat even when
   the menu has it off. */

.chat-host { flex-direction: column; min-width: 0; min-height: 0; }

.chat-shell {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  color: var(--ink);
  background: var(--panel);
  /* Studio draws the pane's own trailing hairline. */
  border-right: 1px solid var(--line);
  font: var(--type-body);
}
.chat-shell, .chat-shell * { box-sizing: border-box; }
/* styles.css already says this with !important; repeated so the pane cannot be
   shown half-built by a stylesheet loaded without it. */
.chat-shell [hidden] { display: none !important; }

/* ------------------------------------------------------------------ header */

/* 48px is Studio's preview-controls height, which the three pane headers share.
   A workspace that settles on another shared height sets --chat-header-height on
   the host. CHAT on the leading edge and the model menu on the trailing one; the
   look is context for a turn and is not titled here. */
.chat-header {
  position: relative;
  z-index: 2;
  flex: 0 0 auto;
  height: var(--chat-header-height, 48px);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  background: var(--panel-muted);
  border-bottom: 1px solid var(--line);
}
.chat-eyebrow { flex: 0 0 auto; color: var(--muted); font: var(--type-eyebrow); letter-spacing: var(--eyebrow-tracking); }
/* Not positioned: the model menu hangs from the header, so it is never wider than the pane. */
.chat-provider { min-width: 0; margin-left: auto; display: flex; align-items: center; }

/* ------------------------------------------------------------------ badges */

/* `StudioStatusBadgeView`: badge type on a 5-point capsule, in five families. "AI
   Provider" is a neutral one, and so is every turn's outcome. A queued turn is work in
   progress, so its badge is the activity orange. */
.chat-badge {
  flex: 0 0 auto;
  max-width: 100%;
  display: inline-block;
  padding: 4px 8px;
  overflow: hidden;
  color: var(--ink-soft);
  background: var(--chip);
  border-radius: var(--radius-badge);
  font: var(--type-caption-strong);
  white-space: nowrap;
  text-overflow: ellipsis;
}
.chat-badge[data-kind="success"] { color: var(--ok-ink); background: var(--ok-bg); }
.chat-badge[data-kind="warning"] { color: var(--warn-ink); background: var(--warn-bg); }
.chat-badge[data-kind="danger"] { color: var(--bad-ink); background: var(--bad-bg); }
.chat-badge[data-kind="activity"] { color: var(--activity-ink); background: var(--activity-wash); }

/* ------------------------------------------------------------------- menus */

/* `StudioCompactMenuLabelView`: caption-strong beside a chevron in a 28-point control,
   on the plain family's raised fill and line — the model menu, and a turn's Retry. A key
   that needs attention turns the model menu to the warning family. */
.chat-menu-button {
  min-width: 0;
  max-width: 100%;
  min-height: var(--control-height);
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin: 0;
  padding: 0 12px;
  color: var(--ink);
  background-color: var(--selected-fill);
  border: 1px solid var(--selected-line);
  border-radius: var(--radius-button);
  font: var(--type-caption-strong);
  cursor: pointer;
}
.chat-menu-button:hover:not(:disabled) { background-image: linear-gradient(var(--hover), var(--hover)); }
.chat-menu-button[aria-expanded="true"] { background-image: linear-gradient(var(--pressed), var(--pressed)); }
.chat-menu-button:disabled { color: var(--muted); background: var(--chip); border-color: transparent; cursor: default; }
.chat-menu-button:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.chat-menu-button[data-kind="warning"] { color: var(--warn-ink); background-color: var(--warn-bg); border-color: var(--warn-line); }
.chat-menu-button-title { min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.chat-menu-button .chat-icon { width: 11px; height: 11px; }

/* The popup: a card on the panel surface, under its control (the model menu) or over
   it (Retry, which belongs to the newest turn at the foot of the transcript). */
.chat-menu {
  position: absolute;
  z-index: 30;
  top: calc(100% + 4px);
  right: 0;
  width: max-content;
  min-width: 180px;
  max-width: min(320px, calc(100vw - 32px));
  max-height: 360px;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 6px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-card);
  box-shadow: 0 12px 32px var(--shadow-menu);
  animation: chat-menu-in .13s ease;
}
@keyframes chat-menu-in { from { opacity: 0; transform: translateY(-4px); } }
.chat-provider-menu { top: calc(100% - 4px); right: 12px; max-width: calc(100% - 24px); }
.chat-turn-retry-menu { top: auto; bottom: calc(100% + 4px); }
@media (prefers-reduced-motion: reduce) {
  .chat-menu { animation: none; }
}
.chat-menu-section + .chat-menu-section { margin-top: 4px; padding-top: 4px; border-top: 1px solid var(--line); }
.chat-menu-heading { padding: 6px 8px 2px; color: var(--ink-soft); font: var(--type-caption-strong); }
.chat-menu-model { padding: 2px 8px 6px; }
.chat-menu-model-title { color: var(--ink); font: var(--type-small); }
.chat-menu-efforts { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.chat-menu-item {
  min-height: var(--control-height);
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 0;
  padding: 0 8px;
  color: var(--ink);
  background: transparent;
  border: 0;
  border-radius: var(--radius-icon);
  font: var(--type-body);
  text-align: left;
  white-space: nowrap;
  cursor: pointer;
}
.chat-menu > .chat-menu-item, .chat-menu-section > .chat-menu-item { width: 100%; }
.chat-menu-item:hover, .chat-menu-item:focus-visible { background-color: var(--hover); }
.chat-menu-item:focus-visible { outline: 2px solid var(--focus); outline-offset: -2px; }
.chat-menu-item .chat-icon { width: 12px; height: 12px; visibility: hidden; }
.chat-menu-item[aria-checked="true"] .chat-icon { visibility: visible; }
/* An effort is a chip in its model's row; the chosen one is a selection — a raised
   neutral fill, a neutral edge and primary ink. */
.chat-menu-efforts .chat-menu-item { min-height: 24px; padding: 0 8px 0 6px; background: var(--chip); font: var(--type-small); }
.chat-menu-efforts .chat-menu-item:hover { background-image: linear-gradient(var(--hover), var(--hover)); }
.chat-menu-item[aria-checked="true"] { color: var(--selected-ink); background: var(--selected-fill); box-shadow: inset 0 0 0 1px var(--selected-line-strong); }

/* -------------------------------------------------------------- transcript */

.chat-transcript { flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
.chat-transcript-body { display: flex; flex-direction: column; gap: 12px; padding: 12px; }
/* The turn list is a keyed container for the script, not a box: its turns space
   themselves in the transcript's own column, and an empty list adds no gap. */
.chat-turns { display: contents; }

.chat-turn { display: flex; flex-direction: column; gap: 8px; }
/* Studio's arrivals: a new turn fades in rising a little, and a summary, caption,
   reason or failure fades in rising less as it appears in a turn already shown. */
.chat-arrive { animation: chat-arrive var(--motion-chat-text) ease-out; }
.chat-arrive-text { animation: chat-arrive-text var(--motion-chat-text) ease-out; }
@keyframes chat-arrive { from { opacity: 0; transform: translateY(4px); } }
@keyframes chat-arrive-text { from { opacity: 0; transform: translateY(2px); } }
@keyframes chat-fade { from { opacity: 0; } }
/* With reduced motion the text still fades in, and does not move. */
@media (prefers-reduced-motion: reduce) {
  .chat-shell .chat-arrive,
  .chat-shell .chat-arrive-text { animation: chat-fade var(--motion-chat-text) ease-out !important; }
}
.chat-message {
  margin: 0;
  padding: 12px;
  color: var(--ink);
  background: var(--chip);
  border-radius: var(--radius-button);
  font: var(--type-body);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
/* A turn's pinned look group, under its references. */
.chat-scope { margin: 0; color: var(--muted); font: var(--type-caption); overflow-wrap: anywhere; }
.chat-result { min-width: 0; display: flex; flex-direction: column; align-items: stretch; gap: 4px; }
/* The status row: a badge (none while generating), the spinner and elapsed time of a
   turn in flight, and the newest turn's Retry menu on the trailing edge. */
.chat-result-head { min-width: 0; display: flex; align-items: center; gap: 8px; }
.chat-result-head:empty { display: none; }
.chat-elapsed { color: var(--muted); font: var(--type-caption); font-variant-numeric: tabular-nums; }
.chat-turn-retry { position: relative; flex: 0 0 auto; margin-left: auto; }
.chat-summary, .chat-reason, .chat-failure, .chat-load-message, .chat-diagnostic-remedy {
  margin: 0;
  font: var(--type-small);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.chat-summary { color: var(--ink-soft); }
.chat-reason { color: var(--ink); }
.chat-failure { color: var(--bad-ink); }
/* "This turn changed the behavior spec": a reference can steer the spec, so the turn
   says so before anyone approves it. */
.chat-spec-caption { margin: 0; color: var(--warn-ink); font: var(--type-caption); }
/* A failed generation's look, element, next action and support identifier. */
.chat-diagnostic { display: flex; flex-direction: column; gap: 4px; }
.chat-diagnostic-look, .chat-diagnostic-element, .chat-diagnostic-support { margin: 0; font: var(--type-caption); overflow-wrap: anywhere; }
.chat-diagnostic-look, .chat-diagnostic-element { color: var(--ink-soft); }
.chat-diagnostic-remedy { color: var(--ink); }
.chat-diagnostic-support { color: var(--muted); user-select: text; }

/* Studio's mini progress indicator, in the neutral loading colour: a turn in flight
   and an upload. With reduced motion it holds still; the words beside it say what runs. */
.chat-spinner {
  width: 12px;
  height: 12px;
  flex: 0 0 12px;
  border: 2px solid var(--line);
  border-top-color: var(--loading);
  border-radius: 50%;
  animation: chat-spin .8s linear infinite;
}
@keyframes chat-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  .chat-spinner { animation: none; }
}

/* "Choose an AI key", shown only while the transcript is empty and no usable key is
   selected. A connected, empty transcript shows nothing at all. */
.chat-connect { display: flex; flex-direction: column; align-items: flex-start; gap: 8px; padding: 12px; background: var(--panel-muted); border-radius: var(--radius-row); }
.chat-connect-title { margin: 0; color: var(--ink); font: var(--type-h2); }
.chat-connect-copy { margin: 0; color: var(--ink-soft); font: var(--type-small); }

/* A failed read: the backend's sentence and a Retry that reads again. */
.chat-load-failure { display: flex; flex-direction: column; align-items: flex-start; gap: 12px; }
.chat-load-message { color: var(--warn-ink); }
.chat-button { gap: 6px; }
.chat-icon { display: block; width: 14px; height: 14px; flex: 0 0 auto; }
.chat-button .chat-icon { width: 12px; height: 12px; }
.chat-icon-knockout { stroke: var(--chip); }

/* ------------------------------------------------------------- attachments */

/* `StudioChatReferenceChipView` and `StudioChatArtworkChipView`: an icon, the name in
   caption type, and — in the composer — a control that removes it, on the chip colour.
   A row of them scrolls sideways rather than wrapping. */
.chat-chips {
  display: flex;
  gap: 8px;
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: none;
}
.chat-chips::-webkit-scrollbar { display: none; }
.chat-chip {
  flex: 0 0 auto;
  max-width: 240px;
  min-height: var(--control-height);
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px;
  color: var(--ink);
  background: var(--chip);
  border-radius: var(--radius-button);
  font: var(--type-caption);
}
.chat-chip:has(.chat-chip-remove) { padding-right: 0; }
.chat-chip > .chat-icon { color: var(--ink-soft); }
.chat-chip-label { min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.chat-chip .chat-icon-button:disabled { color: var(--muted); cursor: default; }
.chat-chip .chat-icon-button:disabled:hover { background: transparent; }

.chat-attachments { display: flex; flex-direction: column; gap: 8px; }
.chat-upload { display: flex; align-items: center; gap: 8px; }
.chat-upload-text { flex: 1 1 auto; min-width: 0; margin: 0; color: var(--muted); font: var(--type-caption); overflow-wrap: anywhere; }
.chat-upload-failed .chat-upload-text { color: var(--bad-ink); }

/* ------------------------------------------------------------ action error */

/* Between the transcript and the composer, and outside the composer's height
   budget. Three lines at most; the whole sentence is on its tooltip. */
.chat-error {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 8px 12px 0;
  padding: 8px;
  background: var(--bad-bg);
  border-radius: var(--radius-button);
}
.chat-error-message {
  flex: 1 1 auto;
  min-width: 0;
  margin: 0;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  line-clamp: 3;
  overflow: hidden;
  color: var(--bad-ink);
  font: var(--type-small);
  overflow-wrap: anywhere;
}
.chat-icon-button {
  width: var(--icon-button);
  height: var(--icon-button);
  flex: 0 0 var(--icon-button);
  display: grid;
  place-items: center;
  margin: 0;
  padding: 0;
  color: var(--ink-soft);
  background: transparent;
  border: 0;
  border-radius: var(--radius-icon);
  cursor: pointer;
}
.chat-icon-button:hover:not(:disabled) { color: var(--ink); background: var(--hover); }
.chat-icon-button:disabled { color: var(--muted); cursor: default; }
.chat-icon-button:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* ---------------------------------------------------------------- composer */

/* Studio's composer: the original retry caption, the attachment chips with any upload
   progress or failure, a draft's save or restore error, then the input. The script
   keeps the whole composer under 29% of the pane by choosing the text area's rows.
   The max-height repeats that bound for the one case rows cannot reach — a pane too
   short for a single line — where it clips instead. */
.chat-composer {
  flex: 0 0 auto;
  max-height: 29%;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow: hidden;
  padding: 12px;
  border-top: 1px solid var(--line);
}
.chat-retry-caption { display: flex; align-items: center; gap: 8px; }
.chat-caption { flex: 1 1 auto; min-width: 0; margin: 0; color: var(--muted); font: var(--type-caption); overflow-wrap: anywhere; }
.chat-draft-status { margin: 0; color: var(--bad-ink); font: var(--type-caption); overflow-wrap: anywhere; }

/* The rounded input holds the text and, along its bottom edge, the paperclip and Send. */
.chat-input {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px;
  background: var(--white);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-button);
}
.chat-input:has(.chat-textarea:focus-visible) { border-color: var(--focus); box-shadow: 0 0 0 1px var(--focus); }
/* Files dragged over the composer, Studio's drop target: the input they will join takes
   the focus edge. Over the rest of the pane a drag changes nothing. */
.chat-dropping .chat-input { border-color: var(--focus); box-shadow: 0 0 0 1px var(--focus); }
/* One row is 18px of text; each further row adds 18px. `overflow-x: hidden` keeps
   Firefox from reserving a horizontal scrollbar inside the rows. */
.chat-textarea {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 18px;
  margin: 0;
  padding: 0;
  color: var(--ink);
  background: transparent;
  border: 0;
  border-radius: 0;
  outline: none;
  resize: none;
  overflow-x: hidden;
  overflow-y: auto;
  font: var(--type-body);
}
.chat-textarea::placeholder { color: var(--muted); opacity: 1; }
.chat-textarea:disabled { color: var(--muted); cursor: not-allowed; }
.chat-textarea.chat-measuring { overflow-y: hidden; }
.chat-input-actions { display: flex; align-items: center; gap: 8px; }
.chat-input-spacer { flex: 1 1 auto; }

/* `StudioSendButtonView`: a 28-point circle in the primary orange holding an arrow, or a
   square while a turn generates. Disabled, it sinks to the chip colour. */
.chat-send {
  width: var(--icon-button);
  height: var(--icon-button);
  flex: 0 0 var(--icon-button);
  display: grid;
  place-items: center;
  margin: 0;
  padding: 0;
  color: var(--on-primary);
  background: var(--primary);
  border: 0;
  border-radius: 50%;
  cursor: pointer;
}
.chat-send:hover:not(:disabled) { background: var(--primary-hover); }
.chat-send:disabled { color: var(--muted); background: var(--chip); cursor: default; }
.chat-send:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* Studio's `paddingScale`, as a step: a pane too short for one line with the
   ordinary padding gives the padding up first. */
.chat-compact .chat-composer { padding-top: 2px; padding-bottom: 2px; gap: 4px; }
.chat-compact .chat-input { padding-top: 0; padding-bottom: 0; gap: 0; }

.chat-visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}
