/* ===========================================
   Conversation (UI redesign Slice 3)

   Reading first. The page scrolls, not the conversation box, and the
   composer sticks to the bottom with the safe-area inset. User and
   assistant are told apart by side and by a label above the bubble, never by
   colour alone; every bubble is an opaque surface (wave proofs measure its
   text contrast and require full opacity). Markup: messages/_message, which
   models broadcast, so nested .message elements (a broadcast update renders
   the partial inside its own target) must still lay out.
   =========================================== */

/* Keyboard focus and scrollIntoView keep clear of the sticky composer. */
html:has(.chat-composer) {
  scroll-padding-bottom: 9rem;
}

.task-conversation {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.messages-container {
  display: block;
  min-width: 0;
}

.messages {
  max-width: 100%;
  padding: var(--spacing-sm) 0 var(--spacing-lg);
}

.messages__list {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-lg);
  min-width: 0;
}

/* ===========================================
   Message
   =========================================== */

.message {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: var(--spacing-xs);
  min-width: 0;
  max-width: 100%;
}

/* A broadcast update renders the partial inside its own target. */
.message > .message {
  align-self: stretch;
  max-width: none;
}

.message--user {
  align-self: flex-end;
  align-items: flex-end;
  max-width: min(88%, 40rem);
  margin-left: auto;
}

.message--assistant {
  align-self: flex-start;
  max-width: min(100%, var(--prose-max-width));
}

.message__role {
  padding: 0 var(--spacing-xs);
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-weight: var(--font-medium);
  letter-spacing: var(--tracking-normal);
}

.message__bubble {
  position: relative;
  min-width: 0;
  max-width: 100%;
  padding: var(--spacing-md) var(--spacing-lg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  background-color: var(--color-surface);
  color: var(--color-text);
  font-size: var(--text-base);
  line-height: var(--leading-relaxed);
  overflow-wrap: anywhere;
}

.message--user .message__bubble {
  border-color: var(--color-border-subtle);
  border-end-end-radius: var(--radius-xs);
  background-color: var(--color-surface-raised);
}

.message--assistant .message__bubble {
  border-end-start-radius: var(--radius-xs);
}

/* Markdown inside a bubble. Headings stay below the page h1 and the
   aside's h2 in size. */
.message__bubble > p,
.message__bubble > ul,
.message__bubble > ol,
.message__bubble > pre,
.message__bubble > blockquote,
.message__bubble > table,
.message__bubble > h1,
.message__bubble > h2,
.message__bubble > h3,
.message__bubble > h4,
.message__bubble > h5,
.message__bubble > h6 {
  margin: 0 0 var(--spacing-sm);
}

.message__bubble > :last-child {
  margin-bottom: 0;
}

.message__bubble > h1,
.message__bubble > h2,
.message__bubble > h3,
.message__bubble > h4,
.message__bubble > h5,
.message__bubble > h6 {
  color: var(--color-text-emphasis);
  font-weight: var(--font-semibold);
  line-height: var(--leading-snug);
}

.message__bubble > h1 { font-size: var(--text-lg); }
.message__bubble > h2 { font-size: var(--text-base); }

.message__bubble > h3,
.message__bubble > h4,
.message__bubble > h5,
.message__bubble > h6 {
  font-size: var(--text-sm);
}

.message__bubble ul,
.message__bubble ol {
  padding-left: var(--spacing-xl);
}

.message__bubble ul { list-style: disc; }
.message__bubble ol { list-style: decimal; }

.message__bubble li + li {
  margin-top: var(--spacing-xs);
}

/* Links in running text are underlined: the accent alone is too close to
   body text (design §A.5, §D.15). */
.message__bubble a {
  color: var(--color-primary);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}

.message__bubble a:hover {
  color: var(--color-primary-hover);
}

.message__bubble pre {
  max-width: 100%;
  overflow-x: auto;
  padding: var(--spacing-md);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-background-subtle);
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  line-height: var(--leading-normal);
  overflow-wrap: normal;
  tab-size: 2;
}

.message__bubble code {
  font-family: var(--font-mono);
}

.message__bubble pre code {
  padding: 0;
  background: none;
  color: inherit;
  font-size: inherit;
}

.message__bubble blockquote {
  padding-left: var(--spacing-md);
  border-left: 2px solid var(--color-border);
  color: var(--color-text-muted);
}

.message__bubble table {
  display: block;
  max-width: 100%;
  overflow-x: auto;
  border-collapse: collapse;
  font-size: var(--text-sm);
}

.message__bubble th,
.message__bubble td {
  padding: var(--spacing-xs) var(--spacing-sm);
  border-bottom: 1px solid var(--color-border-subtle);
  text-align: left;
  vertical-align: top;
}

.message__bubble hr {
  margin: var(--spacing-md) 0;
  border: 0;
  border-top: 1px solid var(--color-border-subtle);
}

/* ===========================================
   Attachments
   =========================================== */

.message__attachment {
  margin-top: var(--spacing-sm);
}

.message__attachment img,
.message__image {
  display: block;
  max-width: 100%;
  height: auto;
  border: 1px solid var(--color-border-subtle);
  border-radius: var(--radius-md);
}

.message__file {
  display: inline-flex;
  align-items: center;
  gap: var(--spacing-xs);
  min-height: var(--control-height-sm);
  color: var(--color-primary);
  font-size: var(--text-sm);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.message__file svg {
  flex-shrink: 0;
  fill: none;
  stroke: currentColor;
}

/* ===========================================
   Details line: source, tokens, cost, time and delete. Always visible;
   hover-only meta is unreachable on touch.
   =========================================== */

.message__details {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--spacing-xs) var(--spacing-md);
  margin-top: var(--spacing-sm);
  padding-top: var(--spacing-sm);
  border-top: 1px solid var(--color-border-subtle);
  color: var(--color-text-subtle);
  font-size: var(--text-xs);
  line-height: var(--leading-normal);
  font-variant-numeric: tabular-nums;
}

.message__source {
  display: inline-flex;
  align-items: center;
  padding: 0 var(--spacing-xs);
  border: 1px solid var(--color-neutral-border);
  border-radius: var(--radius-sm);
  background-color: var(--color-neutral-surface);
  color: var(--color-neutral);
}

.message__tokens,
.message__cost,
.message__time {
  color: var(--color-text-subtle);
  font-size: var(--text-xs);
  white-space: nowrap;
}

.message__details > form {
  display: flex;
  margin-left: auto;
}

.message__delete {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: var(--control-height-sm);
  min-height: var(--control-height-sm);
  padding: 0;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--color-text-muted);
  cursor: pointer;
  transition:
    background-color var(--duration-fast) var(--ease-out),
    border-color var(--duration-fast) var(--ease-out),
    color var(--duration-fast) var(--ease-out);
}

.message__delete:hover {
  border-color: var(--color-danger-border);
  background: var(--color-danger-surface);
  color: var(--color-danger);
}

.message__delete svg {
  width: 1rem;
  height: 1rem;
  fill: none;
  stroke: currentColor;
}

/* ===========================================
   Tool calls and responses: compact, full width, never translucent.
   =========================================== */

.message--tool_call,
.message--tool {
  align-self: stretch;
  max-width: 100%;
}

.message__tool {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--color-border-subtle);
  border-radius: var(--radius-md);
  background: var(--color-background-subtle);
}

.message__tool-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--spacing-xs) var(--spacing-md);
  padding: var(--spacing-sm) var(--spacing-md);
  border-bottom: 1px solid var(--color-border-subtle);
  color: var(--color-text-muted);
  font-size: var(--text-xs);
}

.message__tool-type {
  display: inline-flex;
  align-items: center;
  gap: var(--spacing-xs);
  color: var(--color-text-muted);
  font-weight: var(--font-medium);
}

.message__tool-type svg {
  flex-shrink: 0;
  fill: none;
  stroke: currentColor;
}

.message__tool-content {
  min-width: 0;
}

.message__code {
  max-height: 16rem;
  margin: 0;
  overflow: auto;
  padding: var(--spacing-sm) var(--spacing-md);
  color: var(--color-text);
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  line-height: var(--leading-normal);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.message__code code {
  padding: 0;
  background: none;
  color: inherit;
  font-size: inherit;
}

/* ===========================================
   Error messages (the details block is in error_details.css)
   =========================================== */

.message--error {
  align-self: stretch;
  max-width: min(100%, var(--prose-max-width));
}

.message__error {
  width: 100%;
  min-width: 0;
  padding: var(--spacing-md) var(--spacing-lg);
  border: 1px solid var(--color-danger-border);
  border-radius: var(--radius-lg);
  background-color: var(--color-danger-surface);
  color: var(--color-text);
  overflow-wrap: anywhere;
}

.message__error-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--spacing-xs) var(--spacing-md);
  margin-bottom: var(--spacing-sm);
}

.message__error-header .message__error-type {
  display: inline-flex;
  align-items: center;
  gap: var(--spacing-xs);
  color: var(--color-danger);
  font-size: var(--text-sm);
}

.message__error-header svg {
  flex-shrink: 0;
  fill: none;
  stroke: currentColor;
}

.message__error > .message__error-content > :first-child {
  margin-top: 0;
}

.message__error > .message__error-content > :last-child {
  margin-bottom: 0;
}

.message__error .message__details {
  border-top-color: var(--color-danger-border);
}

/* ===========================================
   Composer (messages/_form): sticks to the bottom of the viewport while the
   conversation scrolls under it, full bleed across the page gutter.
   =========================================== */

.chat-composer {
  position: sticky;
  bottom: 0;
  z-index: var(--z-sticky);
  margin: 0 calc(var(--page-gutter) * -1);
  padding: var(--spacing-md) var(--page-gutter) calc(var(--spacing-md) + env(safe-area-inset-bottom));
  border-top: 1px solid var(--color-border);
  background: var(--color-background);
}

.chat-composer__row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: end;
  gap: var(--spacing-sm);
}

.chat-composer .form__group {
  margin: 0;
}

.chat-composer .form__actions {
  margin: 0;
}

.chat-composer__input {
  display: block;
  min-height: 5.5rem;
  max-height: 40vh;
  padding: var(--spacing-sm) var(--spacing-md);
  resize: vertical;
  line-height: var(--leading-normal);
}

.chat-composer .form__actions > .chat-composer__send {
  width: auto;
}

.chat-composer__hint {
  display: none;
  margin: var(--spacing-xs) 0 0;
  color: var(--color-text-subtle);
  font-size: var(--text-xs);
}

/* ===========================================
   From 640px: hint visible; from 768px: a slimmer composer
   =========================================== */

@media (min-width: 640px) {
  .chat-composer__hint {
    display: block;
  }
}

@media (min-width: 768px) {
  .messages__list {
    gap: var(--spacing-xl);
  }

  .chat-composer__input {
    min-height: 4.5rem;
  }
}

@media (forced-colors: active) {
  .message__bubble,
  .message__tool,
  .message__error {
    border: 1px solid CanvasText;
  }
}

@media print {
  .chat-composer,
  .message__delete {
    display: none;
  }
}
