/* ─────────────────────────────────────────────
   messages.css — Conversation display

   Message blocks, user vs LLM differentiation,
   tool calls, thinking indicator, action buttons,
   timestamps, code/artifact details, images.
   ───────────────────────────────────────────── */

/* ── Messages Container ── */

#messages {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  padding: var(--gutter);
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior-y: contain;
  scroll-behavior: smooth;
  flex: 1 1 0;
  min-height: 0;
}

#messages-scroll-anchor {
  min-height: 1px;
  flex-shrink: 0;
}


/* ── Base Message ── */

.msg {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: auto auto auto;
  gap: var(--space-1);
  max-width: 720px;
  width: 100%;
  padding: 0;
  position: relative;
  margin: 0 auto;
}

.msg-bubble {
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-lg);
  line-height: 1.55;
  letter-spacing: 0.01em;
  word-wrap: break-word;
  overflow-wrap: break-word;
  hyphens: auto;
  user-select: text;
  -webkit-user-select: text;
}

.msg-meta {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: 0 var(--space-2);
  font-family: var(--font-ui);
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  line-height: 1;
}

.msg-actions {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  padding: 0 var(--space-1);
  opacity: 0;
  transition: opacity var(--transition-fast);
}

.msg:hover .msg-actions,
.msg:focus-within .msg-actions,
.msg-actions:focus-within {
  opacity: 1;
}

/* On touch devices, always show actions */
@media (hover: none) {
  .msg-actions {
    opacity: 1;
  }
}


/* ── User Messages ── */

.msg--user {
  justify-self: end;
  align-items: flex-end;
}

.msg--user .msg-bubble {
  font-family: var(--font-user);
  font-size: var(--text-base);
  font-weight: 420;
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  border-bottom-right-radius: var(--radius-sm);
  color: var(--color-text);
}

.msg--user .msg-meta {
  justify-content: flex-end;
}

.msg--user .msg-actions {
  justify-content: flex-end;
}


/* ── LLM Messages ── */

.msg--llm {
  justify-self: start;
  align-items: flex-start;
}

.msg--llm .msg-bubble {
  font-family: var(--font-llm);
  font-size: var(--text-sm);
  font-weight: 400;
  background: var(--color-surface-1);
  border: 1px solid transparent;
  border-bottom-left-radius: var(--radius-sm);
  color: var(--color-text-secondary);
}

.msg--llm .msg-meta {
  justify-content: flex-start;
}

.msg--llm .msg-actions {
  justify-content: flex-start;
}


/* ── System / Info Messages ── */

.msg--system {
  justify-self: center;
  text-align: center;
  max-width: 480px;
}

.msg--system .msg-bubble {
  font-family: var(--font-ui);
  font-size: var(--text-xs);
  background: transparent;
  color: var(--color-text-muted);
  padding: var(--space-2) var(--space-3);
  border: 1px dashed var(--color-border);
  border-radius: var(--radius-md);
}


/* ── Meta: Timestamp + Duration ── */

.msg-time {
  white-space: nowrap;
}

.msg-duration {
  white-space: nowrap;
  color: var(--color-text-muted);
}

.msg-duration::before {
  content: "·";
  margin-right: var(--space-1);
}

.msg-model {
  white-space: nowrap;
  color: var(--color-text-muted);
  opacity: 0.6;
}


/* ── Action Buttons ── */

.msg-action-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  padding: 0;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  cursor: pointer;
  transition:
    color var(--transition-fast),
    background var(--transition-fast);
  flex-shrink: 0;
}

.msg-action-btn:hover {
  color: var(--color-text);
  background: var(--color-surface-2);
}

.msg-action-btn:active {
  background: var(--color-surface-3);
}

/* Feedback state after copy */
.msg-action-btn[data-copied="true"] {
  color: var(--color-accent-green);
}


/* ── Tool Call Blocks ── */

.msg-tool-call {
  margin: var(--space-2) 0;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  overflow: hidden;
  font-family: var(--font-ui);
  font-size: var(--text-xs);
}

.msg-tool-call summary {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  background: var(--color-surface-2);
  cursor: pointer;
  list-style: none;
  color: var(--color-text-secondary);
  transition: background var(--transition-fast);
}

.msg-tool-call summary::-webkit-details-marker {
  display: none;
}

.msg-tool-call summary::before {
  content: "⚡";
  font-size: var(--text-sm);
  flex-shrink: 0;
}

.msg-tool-call summary:hover {
  background: var(--color-surface-3);
}

.msg-tool-call[open] summary {
  border-bottom: 1px solid var(--color-border);
}

.msg-tool-call-name {
  font-weight: 600;
  color: var(--color-text);
  letter-spacing: 0.02em;
}

.msg-tool-call-duration {
  margin-left: auto;
  color: var(--color-text-muted);
  font-variant-numeric: tabular-nums;
}

.msg-tool-call-chevron {
  display: inline-block;
  transition: transform var(--transition-fast);
  font-size: 0.6em;
  color: var(--color-text-muted);
}

.msg-tool-call[open] .msg-tool-call-chevron {
  transform: rotate(90deg);
}

.msg-tool-call-body {
  padding: var(--space-3);
  background: var(--color-bg);
  max-height: 300px;
  overflow-y: auto;
  overscroll-behavior-y: contain;
}

.msg-tool-call-body pre {
  margin: 0;
  font-family: var(--font-llm);
  font-size: var(--text-xs);
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-all;
  color: var(--color-text-muted);
  user-select: text;
  -webkit-user-select: text;
}


/* ── Thinking / Extended Thinking Block ── */

.msg-thinking {
  margin: var(--space-2) 0;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  overflow: hidden;
  font-family: var(--font-ui);
  font-size: var(--text-xs);
}

.msg-thinking summary {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  background: var(--color-surface-1);
  cursor: pointer;
  list-style: none;
  color: var(--color-text-muted);
}

.msg-thinking summary::-webkit-details-marker {
  display: none;
}

.msg-thinking summary::before {
  content: "💭";
  font-size: var(--text-sm);
}

.msg-thinking-body {
  padding: var(--space-3);
  background: var(--color-bg);
  max-height: 300px;
  overflow-y: auto;
  font-family: var(--font-llm);
  font-size: var(--text-xs);
  line-height: 1.55;
  color: var(--color-text-muted);
  white-space: pre-wrap;
  user-select: text;
  -webkit-user-select: text;
}


/* ── Thinking Indicator (streaming) ── */

#thinking-indicator {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: var(--gutter);
  align-self: flex-start;
}

#thinking-indicator[hidden] {
  display: none;
}

.thinking-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--color-text-muted);
  animation: thinking-pulse 1.4s ease-in-out infinite;
}

.thinking-dot:nth-child(2) {
  animation-delay: 0.2s;
}

.thinking-dot:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes thinking-pulse {
  0%, 80%, 100% {
    opacity: 0.2;
    transform: scale(0.8);
  }
  40% {
    opacity: 1;
    transform: scale(1);
  }
}


/* ── Code / Artifact Blocks (inside LLM messages) ── */

.msg-artifact {
  margin: var(--space-3) 0;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  overflow: hidden;
}

.msg-artifact summary {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  background: var(--color-surface-2);
  cursor: pointer;
  list-style: none;
  font-family: var(--font-ui);
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--color-text-secondary);
  letter-spacing: 0.02em;
}

.msg-artifact summary::-webkit-details-marker {
  display: none;
}

.msg-artifact summary:hover {
  background: var(--color-surface-3);
}

.msg-artifact[open] summary {
  border-bottom: 1px solid var(--color-border);
}

.msg-artifact-body {
  padding: var(--space-3);
  background: var(--color-bg);
  max-height: 60vh;
  overflow-y: auto;
  overscroll-behavior-y: contain;
}

.msg-artifact-body pre {
  margin: 0;
  font-family: var(--font-llm);
  font-size: var(--text-xs);
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-all;
  color: var(--color-text-secondary);
  user-select: text;
  -webkit-user-select: text;
}


/* ── Inline Code (in message markdown) ── */

.msg-bubble code {
  font-family: var(--font-llm);
  font-size: 0.9em;
  padding: 0.15em 0.4em;
  background: var(--color-surface-2);
  border-radius: var(--radius-sm);
  color: var(--color-text);
}

.msg-bubble pre code {
  display: block;
  padding: var(--space-3);
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  overflow-x: auto;
  line-height: 1.5;
  font-size: var(--text-xs);
}


/* ── Images in Messages ── */

.msg-image-wrap {
  position: relative;
  display: inline-block;
  margin: var(--space-2) 0;
  max-width: 100%;
  border-radius: var(--radius-md);
  overflow: hidden;
}

.msg-image-wrap img {
  display: block;
  max-width: 100%;
  max-height: 400px;
  object-fit: contain;
  border-radius: var(--radius-md);
}

.msg-image-download {
  position: absolute;
  bottom: var(--space-2);
  right: var(--space-2);
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--color-bg-translucent);
  color: var(--color-text);
  font-size: var(--text-sm);
  cursor: pointer;
  opacity: 0;
  transition: opacity var(--transition-fast);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.msg-image-wrap:hover .msg-image-download {
  opacity: 1;
}

@media (hover: none) {
  .msg-image-download {
    opacity: 1;
  }
}


/* ── Generated File Downloads (from code execution) ── */

.msg-generated-file {
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  margin: var(--space-2) 0;
  background: var(--color-surface-1);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  font-family: var(--font-ui);
  font-size: var(--text-sm);
  color: var(--color-text-2);
  text-decoration: none;
  transition: background 0.15s ease, border-color 0.15s ease;
  cursor: pointer;
}

.msg-generated-file:hover {
  background: var(--color-surface-2);
  border-color: var(--color-text-4);
}

.msg-generated-file .gen-file-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  background: var(--color-surface-2);
  color: var(--color-text-3);
  font-size: 1rem;
}

.msg-generated-file .gen-file-info {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}

.msg-generated-file .gen-file-name {
  font-weight: 500;
  color: var(--color-text-1);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.msg-generated-file .gen-file-size {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

.msg-generated-file .gen-file-ext {
  padding: 2px 6px;
  border-radius: var(--radius-sm);
  background: var(--color-surface-2);
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--color-text-3);
  letter-spacing: 0.04em;
}

.msg-generated-file .ri-download-2-line {
  color: var(--color-text-muted);
  font-size: 1rem;
  transition: color 0.15s ease;
}

.msg-generated-file:hover .ri-download-2-line {
  color: var(--color-text-1);
}


/* ── Attachments in Messages (lazy-loaded placeholders) ── */

.msg-attachment-placeholder {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  margin: var(--space-2) 0;
  background: var(--color-surface-1);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  font-family: var(--font-ui);
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  min-height: 48px;
}

.msg-attachment-placeholder.is-loading::after {
  content: "";
  width: 14px;
  height: 14px;
  border: 2px solid var(--color-border);
  border-top-color: var(--color-text-muted);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-left: auto;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}


/* ── Reply / Fork Indicator in Message ── */

.msg-reply-ref {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  padding: var(--space-1) var(--space-2);
  margin-bottom: var(--space-1);
  font-family: var(--font-ui);
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  border-left: 2px solid var(--color-border);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  background: var(--color-surface-1);
  cursor: pointer;
  transition: background var(--transition-fast);
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.msg-reply-ref:hover {
  background: var(--color-surface-2);
}


/* ── Markdown Content Inside Messages ── */

.msg-bubble h1,
.msg-bubble h2,
.msg-bubble h3,
.msg-bubble h4,
.msg-bubble h5,
.msg-bubble h6 {
  font-family: var(--font-ui);
  font-weight: 700;
  color: var(--color-text);
  margin: var(--space-3) 0 var(--space-1);
  line-height: 1.3;
}

.msg-bubble h1 { font-size: var(--text-lg); }
.msg-bubble h2 { font-size: var(--text-base); }
.msg-bubble h3 { font-size: var(--text-sm); }
.msg-bubble h4,
.msg-bubble h5,
.msg-bubble h6 { font-size: var(--text-xs); text-transform: uppercase; letter-spacing: 0.05em; }

.msg-bubble h1:first-child,
.msg-bubble h2:first-child,
.msg-bubble h3:first-child {
  margin-top: 0;
}

.msg-bubble p {
  margin: 0 0 var(--space-2);
}

.msg-bubble p:last-child {
  margin-bottom: 0;
}

.msg-bubble ul,
.msg-bubble ol {
  margin: var(--space-2) 0;
  padding-left: var(--space-4);
}

.msg-bubble li {
  margin-bottom: var(--space-1);
}

.msg-bubble blockquote {
  margin: var(--space-2) 0;
  padding: var(--space-2) var(--space-3);
  border-left: 3px solid var(--color-border);
  color: var(--color-text-muted);
  font-style: italic;
}

.msg-bubble hr {
  border: none;
  height: 1px;
  background: var(--color-border);
  margin: var(--space-3) 0;
}

.msg-bubble a {
  color: var(--color-text);
  text-decoration: underline;
  text-underline-offset: 2px;
  text-decoration-color: var(--color-text-muted);
  transition: text-decoration-color var(--transition-fast);
}

.msg-bubble a:hover {
  text-decoration-color: var(--color-text);
}

.msg-bubble table {
  width: 100%;
  border-collapse: collapse;
  margin: var(--space-2) 0;
  font-size: var(--text-xs);
}

.msg-bubble th,
.msg-bubble td {
  padding: var(--space-1) var(--space-2);
  border: 1px solid var(--color-border);
  text-align: left;
}

.msg-bubble th {
  font-weight: 600;
  background: var(--color-surface-2);
  color: var(--color-text);
}

.msg-bubble strong {
  font-weight: 700;
  color: var(--color-text);
}

.msg-bubble em {
  font-style: italic;
}


/* ── Streaming cursor (appended to last text node while streaming) ── */

.msg-bubble .streaming-cursor::after {
  content: "";
  display: inline-block;
  width: 2px;
  height: 1em;
  background: var(--color-text);
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: cursor-blink 0.8s steps(2) infinite;
}

@keyframes cursor-blink {
  0% { opacity: 1; }
  100% { opacity: 0; }
}


/* ── Empty State ── */

#messages:not(:has(.msg))::after {
  content: "Start a conversation.";
  display: block;
  text-align: center;
  font-family: var(--font-ui);
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  padding: var(--space-8) var(--space-4);
  opacity: 0.5;
}


/* ── Responsive ── */

/* Wider messages on tablet+ */
@media (min-width: 768px) {
  .msg {
    max-width: 680px;
  }

  .msg-image-wrap img {
    max-height: 500px;
  }
}

/* Larger screens: more room */
@media (min-width: 1280px) {
  .msg {
    max-width: 760px;
  }

  .msg-bubble {
    font-size: var(--text-base);
  }

  .msg--llm .msg-bubble {
    font-size: var(--text-sm);
  }

  .msg-artifact-body {
    max-height: 70vh;
  }
}
