/* Feature layer — conversation rendering (messages, Markdown, streaming, tools, sources, artifacts). */

.msg { position: relative; padding: var(--sp-3) 0; }
.msg.is-entering { animation: rise-in var(--dur-base) var(--ease-out); }
.msg + .msg { margin-top: var(--sp-2); }

/* ---- User message: compact, distinguishable, not shouting ---- */
.msg-user { display: flex; flex-direction: column; align-items: flex-end; }
.msg-user .bubble {
  max-width: min(85%, 38rem); padding: var(--sp-3) var(--sp-4);
  border-radius: var(--r-lg); border-start-end-radius: var(--r-xs);
  background: var(--surface-2); border: 1px solid var(--border-subtle);
  font-size: var(--fs-reading); line-height: var(--lh-snug); white-space: pre-wrap; overflow-wrap: anywhere;
}
:root[data-theme="light"] .msg-user .bubble { background: var(--bg-sunken); }
.msg-files { display: flex; flex-wrap: wrap; gap: var(--sp-2); margin-bottom: var(--sp-2); max-width: min(85%, 38rem); }
.msg-files img { width: 160px; max-height: 200px; object-fit: cover; border-radius: var(--r-md); border: 1px solid var(--border); cursor: zoom-in; background: var(--surface-hover); }
.file-chip {
  display: inline-flex; align-items: center; gap: var(--sp-2); max-width: 260px;
  height: 40px; padding: 0 var(--sp-3) 0 var(--sp-2); border-radius: var(--r-md);
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text); text-decoration: none; font-size: var(--fs-sm);
}
.file-chip:hover { border-color: var(--border-strong); }
.file-chip .file-ic { display: grid; place-items: center; width: 28px; height: 28px; border-radius: var(--r-xs); background: var(--accent-soft); color: var(--accent-text); font-size: 9px; font-weight: var(--fw-bold); flex: none; }
.file-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---- Assistant message: open content, no enclosing bubble ---- */
.msg-assistant { display: grid; grid-template-columns: 28px minmax(0, 1fr); gap: var(--sp-3); }
.msg-assistant .brand-mark { width: 28px; height: 28px; margin-top: 2px; }
.msg-body { min-width: 0; }
.msg-head { display: flex; align-items: center; gap: var(--sp-2); min-height: 28px; flex-wrap: wrap; }
.msg-head .who { font-size: var(--fs-sm); font-weight: var(--fw-semibold); }
.msg-head .meta { font-size: var(--fs-sm); color: var(--text-2); display: inline-flex; align-items: center; gap: 6px; flex-wrap: wrap; line-height: 1.4; }
.msg-head .meta-model { display: inline-flex; align-items: center; gap: 5px; font-weight: var(--fw-medium); color: var(--text); }
.msg-head .meta-sep { color: var(--text-3); }
.msg-head .meta-reasoning { color: var(--text-2); }
.model-glyph.is-xs { width: 18px; height: 18px; border-radius: 5px; }
.model-glyph.is-xs .icon { width: 12px; height: 12px; }
.msg.is-error .md { color: var(--danger); }

.msg-actions { display: flex; align-items: center; gap: 2px; margin-top: var(--sp-1); margin-inline-start: -6px; opacity: 0; transition: opacity var(--dur-fast); }
.msg:hover .msg-actions, .msg:focus-within .msg-actions, .msg.is-last .msg-actions, :root[data-actions="always"] .msg-actions { opacity: 1; }
@media (hover: none) { .msg-actions { opacity: 1; } }
.msg-actions .icon-btn { width: 30px; height: 30px; color: var(--text-3); }
.msg-actions .icon-btn:hover { color: var(--text); }
.msg-actions .icon { width: 17px; height: 17px; }
.msg-user .msg-actions { margin-inline-start: 0; }

.retired-note { display: flex; gap: var(--sp-3); align-items: center; margin: var(--sp-2) 0 var(--sp-4); }
.status-line { display: flex; align-items: center; gap: var(--sp-2); margin-top: var(--sp-2); font-size: var(--fs-sm); color: var(--text-3); }
.status-line .icon { width: 16px; height: 16px; }
.status-line.is-warning { color: var(--warning); }

/* ---- Streaming state indicator (real lifecycle phases only) ---- */
.thinking { display: inline-flex; align-items: center; gap: var(--sp-3); min-height: 32px; color: var(--text-2); font-size: var(--fs-sm); }
.thinking .orbit { position: relative; width: 18px; height: 18px; flex: none; }
.thinking .orbit::before, .thinking .orbit::after { content: ""; position: absolute; inset: 0; border-radius: 50%; }
.thinking .orbit::before { border: 1.5px solid var(--accent-border); }
.thinking .orbit::after { inset: 0; border: 1.5px solid transparent; border-top-color: var(--accent-text); animation: spin 1.1s linear infinite; }
.thinking .label { background: linear-gradient(90deg, var(--text-3) 0%, var(--text) 50%, var(--text-3) 100%); background-size: 200% 100%; -webkit-background-clip: text; background-clip: text; color: transparent; animation: shimmer 2.2s linear infinite; }
@media (prefers-reduced-motion: reduce) { .thinking .label { color: var(--text-2); background: none; } }
.caret { display: inline-block; width: 0.5em; height: 1.05em; vertical-align: -0.15em; margin-inline-start: 2px; border-radius: 2px; background: var(--accent-text); animation: energy-breathe 1s steps(2) infinite; }

/* ---- Markdown body ---- */
.md { font-size: var(--fs-reading); line-height: var(--lh-reading); color: var(--text); overflow-wrap: anywhere; }
.md > * + * { margin-top: 0.85em; }
.md h1, .md h2, .md h3, .md h4 { line-height: 1.4; margin-top: 1.4em; font-weight: var(--fw-bold); }
.md > :first-child { margin-top: 0; }
.md h1 { font-size: 1.35em; } .md h2 { font-size: 1.2em; } .md h3 { font-size: 1.06em; } .md h4 { font-size: 1em; color: var(--text-2); }
.md strong { font-weight: var(--fw-bold); }
.md ul, .md ol { padding-inline-start: 1.5em; }
.md li + li { margin-top: 0.3em; }
.md li > ul, .md li > ol { margin-top: 0.3em; }
.md li::marker { color: var(--text-3); }
.md blockquote { padding: 0.2em 1em; border-inline-start: 3px solid var(--accent-border); color: var(--text-2); }
.md hr { border: 0; height: 1px; background: var(--border); margin: 1.6em 0; }
.md a { color: var(--accent-text); }
.md :not(pre) > code { padding: 0.12em 0.4em; border-radius: 5px; background: var(--surface-active); font-size: 0.86em; unicode-bidi: isolate; }
.md .table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--r-md); }
.md table { width: 100%; font-size: 0.92em; line-height: var(--lh-snug); }
.md th, .md td { padding: 0.55em 0.9em; border-bottom: 1px solid var(--border-subtle); text-align: start; vertical-align: top; }
.md th { background: var(--surface-hover); font-weight: var(--fw-semibold); white-space: nowrap; }
.md tr:last-child td { border-bottom: 0; }
.md .math { font-family: "Cambria Math", "STIX Two Math", "Latin Modern Math", serif; direction: ltr; unicode-bidi: isolate; }
.md .math.is-block { display: block; overflow-x: auto; padding: var(--sp-2) var(--sp-3); border-radius: var(--r-sm); background: var(--surface-hover); text-align: center; }

/* Inline citation marker */
.cite {
  display: inline-flex; align-items: center; justify-content: center; vertical-align: 0.1em;
  min-width: 1.35em; height: 1.35em; padding: 0 0.3em; margin-inline: 0.15em;
  border-radius: var(--r-pill); background: var(--accent-soft); color: var(--accent-text);
  font-size: 0.7em; font-weight: var(--fw-bold); text-decoration: none; line-height: 1; direction: ltr;
}
.cite:hover { background: var(--accent); color: var(--text-on-accent); }

/* ---- Code block ---- */
.code-block { border-radius: var(--r-md); border: 1px solid var(--code-border); background: var(--code-bg); overflow: hidden; direction: ltr; }
.code-head { display: flex; align-items: center; gap: var(--sp-2); height: 36px; padding: 0 var(--sp-2) 0 var(--sp-3); border-bottom: 1px solid var(--code-border); font-family: var(--font-sans); }
.code-head .lang { flex: 1; font-size: var(--fs-xs); color: var(--text-3); font-family: var(--font-mono); text-transform: lowercase; }
.code-head .btn { min-height: 26px; padding: 0 var(--sp-2); font-size: var(--fs-xs); color: var(--text-2); }
.code-block pre { margin: 0; padding: var(--sp-3) var(--sp-4); overflow-x: auto; font-size: 0.82em; line-height: 1.65; color: var(--code-text); tab-size: 4; }
:root[data-code-wrap="on"] .code-block pre { white-space: pre-wrap; overflow-wrap: anywhere; }
:root[data-code-size="small"] .code-block pre { font-size: 0.76em; }
:root[data-code-size="large"] .code-block pre { font-size: 0.9em; }
.code-block code { font-family: var(--font-mono); }
.code-block .ln { display: inline-block; width: 2.4em; margin-inline-end: 1em; color: var(--syn-comment); text-align: end; user-select: none; opacity: 0.7; }
.tok-k { color: var(--syn-keyword); } .tok-s { color: var(--syn-string); } .tok-n { color: var(--syn-number); }
.tok-c { color: var(--syn-comment); font-style: italic; } .tok-f { color: var(--syn-fn); }

/* ---- Tool activity: one compact expandable row per tool ---- */
.activity { margin: var(--sp-1) 0 var(--sp-3); display: grid; gap: var(--sp-1); }
.activity details { border-radius: var(--r-md); }
.activity summary {
  display: inline-flex; align-items: center; gap: var(--sp-2); min-height: 30px; padding: 0 var(--sp-3) 0 var(--sp-2);
  border-radius: var(--r-pill); border: 1px solid var(--border-subtle); background: var(--surface-hover);
  color: var(--text-2); font-size: var(--fs-sm); cursor: pointer; list-style: none; user-select: none;
}
.activity summary::-webkit-details-marker { display: none; }
.activity summary:hover { color: var(--text); border-color: var(--border); }
.activity summary .icon { width: 15px; height: 15px; }
.activity summary .state { color: var(--text-3); }
.activity summary .chev { width: 13px; height: 13px; color: var(--text-3); transition: transform var(--dur-fast); }
.activity details[open] summary .chev { transform: rotate(180deg); }
.activity .is-running summary .state { color: var(--accent-text); }
.activity .is-failed summary, .activity .is-failed summary .state { color: var(--danger); }
.activity .is-running summary .tool-ic { animation: energy-breathe 1.4s infinite; }
.activity .detail { margin: var(--sp-2) 0 var(--sp-1); padding: var(--sp-3) var(--sp-4); border-radius: var(--r-md); background: var(--surface-hover); font-size: var(--fs-sm); color: var(--text-2); display: grid; gap: var(--sp-2); }
.activity .detail dl { display: grid; grid-template-columns: auto 1fr; gap: 4px var(--sp-4); }
.activity .detail dt { color: var(--text-3); }
.activity .detail dd { margin: 0; color: var(--text); overflow-wrap: anywhere; }

/* ---- Sources ---- */
.sources { margin-top: var(--sp-4); }
.sources-toggle { display: inline-flex; align-items: center; gap: var(--sp-2); height: 32px; padding: 0 var(--sp-3) 0 var(--sp-2); border-radius: var(--r-pill); border: 1px solid var(--border); font-size: var(--fs-sm); color: var(--text-2); }
.sources-toggle:hover { color: var(--text); background: var(--surface-hover); }
.favicons { display: inline-flex; }
.favicons span { width: 18px; height: 18px; border-radius: 50%; display: grid; place-items: center; background: var(--surface-active); color: var(--text-2); font-size: 9px; font-weight: var(--fw-bold); border: 2px solid var(--bg); margin-inline-start: -6px; text-transform: uppercase; }
.favicons span:first-child { margin-inline-start: 0; }
.sources-list { list-style: none; padding: 0; margin-top: var(--sp-2); display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: var(--sp-2); }
.source { display: grid; grid-template-columns: auto 1fr; gap: 2px var(--sp-2); padding: var(--sp-2) var(--sp-3); border-radius: var(--r-md); border: 1px solid var(--border-subtle); background: var(--surface-2); text-decoration: none; color: var(--text); font-size: var(--fs-sm); line-height: var(--lh-snug); }
.source:hover { border-color: var(--accent-border); }
.source .n { grid-row: span 2; display: grid; place-items: center; width: 22px; height: 22px; border-radius: 50%; background: var(--accent-soft); color: var(--accent-text); font-size: var(--fs-2xs); font-weight: var(--fw-bold); }
.source .t { overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.source .h { color: var(--text-3); font-size: var(--fs-xs); direction: ltr; text-align: start; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.source.is-flash { box-shadow: 0 0 0 2px var(--accent); }

/* ---- Generated artifacts in the thread ---- */
.artifacts { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 240px), 1fr)); gap: var(--sp-3); margin-top: var(--sp-3); }
.artifact-card { display: flex; flex-direction: column; border-radius: var(--r-md); border: 1px solid var(--border); background: var(--surface-2); overflow: hidden; text-align: start; transition: border-color var(--dur-fast), transform var(--dur-fast) var(--ease-out); }
.artifact-card:hover { border-color: var(--accent-border); }
.artifact-card .thumb { aspect-ratio: 1; background: var(--surface-hover); display: grid; place-items: center; overflow: hidden; }
.artifact-card .thumb img { width: 100%; height: 100%; object-fit: cover; }
.artifact-card.is-file .thumb { aspect-ratio: auto; height: 64px; }
.artifact-card .info { display: flex; align-items: center; gap: var(--sp-2); padding: var(--sp-2) var(--sp-3); min-width: 0; }
.artifact-card .info strong { flex: 1; min-width: 0; font-size: var(--fs-sm); font-weight: var(--fw-medium); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.artifact-card .info small { color: var(--text-3); font-size: var(--fs-xs); }
.file-badge { display: grid; place-items: center; min-width: 40px; height: 40px; padding: 0 6px; border-radius: var(--r-sm); background: var(--accent-soft); color: var(--accent-text); font-size: var(--fs-2xs); font-weight: var(--fw-bold); letter-spacing: 0.02em; }

/* Artifact panel content */
.preview-image { display: block; max-width: 100%; max-height: calc(100% - 8px); margin: 0 auto; border-radius: var(--r-md); background: repeating-conic-gradient(var(--surface-hover) 0 25%, transparent 0 50%) 0 0 / 18px 18px; }
.preview-text { margin: 0; padding: var(--sp-4); border-radius: var(--r-md); background: var(--code-bg); border: 1px solid var(--code-border); color: var(--code-text); font-size: var(--fs-sm); line-height: 1.6; white-space: pre-wrap; overflow-wrap: anywhere; direction: ltr; text-align: left; }
.preview-text[dir="rtl"] { direction: rtl; text-align: right; }
.file-facts { display: grid; grid-template-columns: auto 1fr; gap: var(--sp-2) var(--sp-4); font-size: var(--fs-sm); margin-top: var(--sp-4); }
.file-facts dt { color: var(--text-3); }
.file-facts dd { margin: 0; overflow-wrap: anywhere; }

@media (max-width: 767px) {
  .msg-assistant { grid-template-columns: minmax(0, 1fr); gap: var(--sp-1); }
  .msg-assistant > .brand-mark { display: none; }
  .msg-user .bubble { max-width: 92%; }
  .msg-files img { width: 128px; }
}

/* ---- V9.0 stabilization: branches, inline editing, explicit re-attachment ---- */
.branch-nav { display: inline-flex; align-items: center; gap: 2px; margin-inline-end: var(--sp-1); padding: 0 2px; border-radius: var(--r-pill); background: var(--surface-hover); font-size: var(--fs-xs); color: var(--text-2); font-variant-numeric: tabular-nums; }
.branch-nav .icon-btn { width: 26px; height: 26px; }
.branch-nav span { min-width: 2.4em; text-align: center; direction: ltr; }
.msg-user .msg-actions { justify-content: flex-end; }
.msg-actions .icon-dir { transform: none; }
.edit-box { width: min(100%, 38rem); display: grid; gap: var(--sp-2); padding: var(--sp-3); border-radius: var(--r-lg); background: var(--surface-2); border: 1px solid var(--accent-border); }
.edit-box .textarea { min-height: 84px; font-size: var(--fs-reading); }
.edit-box .row { justify-content: flex-end; }
.edit-files { display: flex; flex-wrap: wrap; gap: var(--sp-1); }
.edit-files:empty { display: none; }
.msg-img { position: relative; margin: 0; }
.reattach {
  display: inline-flex; align-items: center; gap: 4px; min-height: 28px; padding: 0 var(--sp-2); border-radius: var(--r-pill);
  font-size: var(--fs-xs); color: var(--text-2); background: var(--surface-3); border: 1px solid var(--border); box-shadow: var(--shadow-1);
  transition: opacity var(--dur-fast), color var(--dur-fast), border-color var(--dur-fast);
}
.reattach:hover { color: var(--text); border-color: var(--accent-border); }
.msg-img .reattach { position: absolute; inset-inline-end: 6px; bottom: 6px; opacity: 0; }
.msg-img:hover .reattach, .msg-img:focus-within .reattach { opacity: 1; }
.file-row { display: inline-flex; align-items: center; gap: var(--sp-1); }
.file-row .reattach { opacity: 0; }
.file-row:hover .reattach, .file-row:focus-within .reattach { opacity: 1; }
.artifact-wrap { display: grid; gap: var(--sp-1); align-content: start; }
.artifact-wrap .reattach { justify-self: start; opacity: 0.85; }
@media (hover: none) { .msg-img .reattach, .file-row .reattach { opacity: 1; } }
