/* ── Message list ────────────────────────────────────────────────────────── */

#messages-host {
  position: relative;
  overflow: hidden;
  min-height: 0;
}

.session-messages {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  -webkit-overflow-scrolling: touch;
}

.session-messages.hidden {
  display: none;
}

/* ── Message bubbles ─────────────────────────────────────────────────────── */

.msg {
  max-width: 86%;
  padding: 0.55rem 0.85rem;
  border-radius: var(--radius);
  word-break: break-word;
  line-height: 1.55;
  font-size: 0.95rem;
}

.msg-user {
  align-self: flex-end;
  background: oklch(var(--p) / 80%);
  color: oklch(var(--pc));
  border-bottom-right-radius: 3px;
}

/* Markdown inside user bubbles — inherits --pc text color */
.msg-user p            { margin: 0 0 0.45em; }
.msg-user p:last-child { margin-bottom: 0; }
.msg-user ul, .msg-user ol { padding-left: 1.4em; margin: 0 0 0.45em; list-style-position: inside; }
.msg-user ul { list-style-type: disc; }
.msg-user ol { list-style-type: decimal; }
.msg-user li { margin-bottom: 0.15em; }
.msg-user strong { font-weight: 600; }
.msg-user em     { font-style: italic; }
.msg-user :not(pre) > code {
  background: rgba(0, 0, 0, 0.20);
  border-radius: 3px;
  padding: 0.1em 0.35em;
  font-family: "JetBrains Mono", "SF Mono", "Fira Code", "Cascadia Code", monospace;
  font-size: 0.875em;
}

.msg-assistant {
  align-self: flex-start;
  background: oklch(var(--p) / 10%);
  border-left: 3px solid oklch(var(--p) / 40%);
  border-bottom-left-radius: 3px;
}

/* Extra top gap when two assistant bubbles follow each other directly
   (multi-turn tool loop produces multiple sequential response segments) */
.msg-assistant + .msg-assistant { margin-top: 0.6em; }

/* Preserve newlines in raw text while streaming (before markdown render) */
.msg-assistant.streaming { white-space: pre-wrap; }

/* Blinking cursor while streaming */
.msg-assistant.streaming::after {
  content: '▋';
  color: oklch(var(--p));
  margin-left: 1px;
  animation: blink 0.75s step-end infinite;
}

@keyframes blink {
  50% { opacity: 0; }
}

/* ── Rendered markdown inside assistant bubbles ──────────────────────────── */

.msg-assistant p            { margin: 0 0 0.55em; }
.msg-assistant p:last-child { margin-bottom: 0; }

.msg-assistant h1, .msg-assistant h2, .msg-assistant h3,
.msg-assistant h4, .msg-assistant h5, .msg-assistant h6 {
  font-weight: 600;
  margin: 0.8em 0 0.3em;
  line-height: 1.3;
}
.msg-assistant h1 { font-size: 1.2em; }
.msg-assistant h2 { font-size: 1.1em; }
.msg-assistant h3 { font-size: 1.0em; }

.msg-assistant ul, .msg-assistant ol {
  padding-left: 1.4em;
  margin: 0 0 0.55em;
}
.msg-assistant ul { list-style-type: disc; }
.msg-assistant ol { list-style-type: decimal; }
.msg-assistant li { margin-bottom: 0.2em; }

.msg-assistant strong { font-weight: 600; }
.msg-assistant em     { font-style: italic; }

.msg-assistant a {
  color: oklch(var(--p));
  text-decoration: underline;
  text-underline-offset: 2px;
}

.msg-assistant blockquote,
.msg-user blockquote {
  margin: 0.4em 0;
  padding-left: 0.75em;
  border-left: 3px solid oklch(var(--p) / 35%);
  color: oklch(var(--bc) / 75%);
}

/* ── Fenced code block wrapper (injected by _installCopyButtons) ─────────── */

.code-block {
  margin: 0.6em 0;
  border-radius: 7px;
  overflow: hidden;
  border: 1px solid oklch(var(--p) / 18%);
}

.code-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5px 12px;
  background: oklch(var(--b2) / 45%);
  border-bottom: 1px solid oklch(var(--p) / 15%);
}

.code-lang {
  font-size: 0.72em;
  font-family: "JetBrains Mono", "SF Mono", "Fira Code", monospace;
  color: oklch(var(--bc) / 55%);
  text-transform: lowercase;
}

.code-copy-btn {
  font-size: 0.72em;
  padding: 2px 9px;
  background: oklch(var(--bc) / 6%);
  border: 1px solid oklch(var(--bc) / 18%);
  border-radius: 4px;
  color: oklch(var(--bc) / 65%);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.code-copy-btn:hover          { background: rgba(255, 255, 255, 0.12); color: oklch(var(--bc) / 90%); }
.code-copy-btn.copied         { color: oklch(var(--su)); border-color: oklch(var(--su) / 40%); }

/* ── Table block wrapper (injected by installTableCopyButtons) ──────────────── */

.table-block {
  margin: 0.6em 0;
  border-radius: 7px;
  overflow: hidden;
  border: 1px solid oklch(var(--p) / 18%);
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5px 12px;
  background: rgba(0, 0, 0, 0.45);
  border-bottom: 1px solid oklch(var(--p) / 18%);
}

.table-lang {
  font-size: 0.72em;
  font-family: "JetBrains Mono", "SF Mono", "Fira Code", monospace;
  color: oklch(var(--bc) / 55%);
  text-transform: lowercase;
}

.table-copy-btn {
  font-size: 0.72em;
  padding: 2px 9px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid oklch(var(--bc) / 18%);
  border-radius: 4px;
  color: oklch(var(--bc) / 65%);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.table-copy-btn:hover   { background: rgba(255, 255, 255, 0.12); color: oklch(var(--bc) / 90%); }
.table-copy-btn.copied  { color: oklch(var(--su)); border-color: oklch(var(--su) / 40%); }

.table-block table {
  margin: 0;
  border-radius: 0;
}

.code-block pre {
  margin: 0;
  padding: 0.85em 1.1em;
  overflow-x: auto;
  background: oklch(var(--b1) / 60%);
  font-family: "JetBrains Mono", "JetBrains Mono", "SF Mono", "Fira Code", "Cascadia Code", monospace;
  font-size: 0.875em;
}

.code-block pre code {
  background: none;
  border: none;
  padding: 0;
  font-family: inherit;
  font-size: inherit;
  color: oklch(var(--bc));
}

/* ── Nested fence blocks (expanded from inner ``` by _expandNestedFences) ─── */

/* Container that replaces the outer <pre> when inner fences are detected */
.nested-blocks {
  padding: 0.3em 0;
}

/* Plain-text segments between nested code blocks */
.nested-blocks > pre {
  margin: 0;
  padding: 0.4em 1.1em;
  background: transparent;
  overflow-x: auto;
}

.nested-blocks > pre code {
  font-family: "JetBrains Mono", "JetBrains Mono", "SF Mono", "Fira Code", "Cascadia Code", monospace;
  font-size: 0.875em;
  color: oklch(var(--bc));
}

/* Nested .code-block — inset style to visually sit inside the outer block */
.code-block.nested {
  margin: 0.4em 0.7em;
  border-color: oklch(var(--p) / 28%);
}

.code-block.nested .code-header {
  background: oklch(var(--b2) / 60%);
  border-bottom-color: oklch(var(--p) / 18%);
}

.code-block.nested pre {
  background: oklch(var(--b1) / 75%);
}

.msg-system {
  align-self: center;
  background: none;
  color: oklch(var(--bc) / 50%);
  font-size: 0.78rem;
  padding: 0.15rem 0;
  max-width: 100%;
  text-align: center;
}

/* Model narration emitted alongside tool calls — shown inline in chat history */
.msg-tool-thinking {
  align-self: flex-start;
  background: oklch(var(--b2));
  border-left: 2px solid oklch(var(--p) / 35%);
  border-radius: 0.5rem;
  color: oklch(var(--bc) / 70%);
  font-size: 0.82rem;
  font-style: italic;
  max-width: 85%;
  padding: 0.45rem 0.75rem;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg-compression {
  opacity: 0.45;
  font-size: 0.72rem;
  letter-spacing: 0.02em;
  padding: 0.15rem 0;
}

.msg-error {
  align-self: stretch;
  max-width: 100%;
  background: oklch(var(--er) / 15%);
  color: oklch(var(--er));
  font-size: 0.85rem;
  border-radius: var(--radius-sm);
}

.msg-warning {
  align-self: stretch;
  max-width: 100%;
  background: oklch(var(--wa, 0.82 0.18 85) / 15%);
  color: oklch(var(--wa, 0.82 0.18 85));
  font-size: 0.85rem;
  border-radius: var(--radius-sm);
  padding: 0.4rem 0.6rem;
  border-left: 3px solid oklch(var(--wa, 0.82 0.18 85) / 60%);
}
.msg-warning-head { font-weight: 500; }
.msg-warning-diag { margin-top: 0.35rem; opacity: 0.85; }
.msg-warning-diag summary {
  cursor: pointer;
  font-size: 0.78rem;
  user-select: none;
}
.msg-warning-diag pre {
  margin: 0.35rem 0 0 0;
  font-size: 0.75rem;
  background: oklch(var(--b1) / 40%);
  padding: 0.4rem 0.5rem;
  border-radius: var(--radius-sm);
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-word;
}
