.chat {
  display: flex;
  flex-direction: column;
  gap: 12px;
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

/* --- Drag & drop overlay --- */
.chat-drop-overlay {
  display: none;
  position: absolute;
  inset: 0;
  z-index: 10;
  background: color-mix(in srgb, var(--accent) 12%, var(--bg));
  border: 2px dashed var(--accent);
  border-radius: 14px;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 15px;
  font-weight: 600;
  color: var(--accent);
  pointer-events: none;
}

.chat-drop-overlay.visible {
  display: flex;
}

.chat-drop-icon {
  font-size: 36px;
}

.chat-log {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-right: 8px;
  padding-top: 6px;
  padding-bottom: 8px;
  mask-image: linear-gradient(to bottom, transparent 0, black 16px);
  -webkit-mask-image: linear-gradient(to bottom, transparent 0, black 16px);
}

.bubble {
  padding: 10px 14px;
  border-radius: 14px;
  line-height: 1.4;
  width: fit-content;
  max-width: 85%;
  white-space: pre-wrap;
  font-size: 13px;
}

.bubble.user {
  align-self: flex-end;
  background: var(--accent);
  color: var(--accent-on-dark);
}

.bubble.assistant {
  background: var(--surface-hover);
  color: var(--ink);
  border: 1px solid var(--border);
  white-space: normal;
}

.bubble.assistant h1,
.bubble.assistant h2,
.bubble.assistant h3 {
  margin: 8px 0 4px;
  font-weight: 600;
}

.bubble.assistant h1 {
  font-size: 20px;
}

.bubble.assistant h2 {
  font-size: 18px;
}

.bubble.assistant h3 {
  font-size: 16px;
}

.bubble.assistant p {
  margin: 6px 0;
}

.bubble.assistant ul {
  margin: 6px 0 6px 18px;
  padding: 0;
}

.bubble.assistant li {
  margin: 4px 0;
}

/* --- Markdown code blocks inside bubbles --- */
.bubble.assistant pre {
  margin: 8px 0;
  padding: 12px 14px;
  background: var(--surface-deep);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow-x: auto;
  font-size: 13px;
  line-height: 1.5;
}

.bubble.assistant pre code {
  background: none;
  padding: 0;
  border-radius: 0;
  font-size: inherit;
  color: inherit;
}

.bubble.assistant code {
  background: var(--surface-deep);
  padding: 2px 6px;
  border-radius: 5px;
  font-size: 0.9em;
  color: var(--accent);
}

.bubble.assistant table {
  width: 100%;
  border-collapse: collapse;
  margin: 8px 0;
  font-size: 13px;
}

.bubble.assistant th,
.bubble.assistant td {
  padding: 6px 10px;
  border: 1px solid var(--border);
  text-align: left;
}

.bubble.assistant th {
  background: var(--surface-deep);
  font-weight: 600;
}

.bubble.assistant blockquote {
  margin: 8px 0;
  padding: 8px 14px;
  border-left: 3px solid var(--accent);
  color: var(--muted);
  background: var(--surface-deep);
  border-radius: 0 8px 8px 0;
}

.bubble.assistant a {
  color: var(--accent);
  text-decoration: underline;
}

.bubble.streaming {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.bubble.streaming .stream-text {
  display: inline;
}

.spinner {
  display: inline-block;
  width: 12px;
  height: 12px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
  vertical-align: middle;
}

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

/* --- Typing indicator (bouncing dots) --- */
.typing-indicator {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.typing-indicator span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0.4;
  animation: typing-bounce 1.2s ease-in-out infinite;
}

.typing-indicator span:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-indicator span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes typing-bounce {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.4;
  }
  30% {
    transform: translateY(-6px);
    opacity: 1;
  }
}

.meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  margin-top: 2px;
  padding-top: 4px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  white-space: normal;
}

.bubble.streaming .meta {
  border-top: none;
  padding-top: 0;
  margin-top: 0;
}

.bubble.assistant > p:last-of-type,
.bubble.assistant > ul:last-of-type,
.bubble.assistant > ol:last-of-type,
.bubble.assistant > h1:last-of-type,
.bubble.assistant > h2:last-of-type,
.bubble.assistant > h3:last-of-type {
  margin-bottom: 0;
}

.chat-form {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  flex-shrink: 0;
}

.chat-form input {
  flex: 1;
  padding: 10px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--ink);
  font-size: 13px;
}

.chat-form button[type="submit"] {
  padding: 8px 14px;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-on-dark);
  border: none;
  cursor: pointer;
  font-weight: 600;
  font-size: 12px;
}

.chat-form button[type="submit"]:hover {
  opacity: 0.9;
}

/* --- Answer depth selector --- */
.answer-depth-select {
  background: var(--surface, #1f2937);
  color: var(--text, #e2e8f0);
  border: 1px solid var(--border, rgba(148, 163, 184, 0.2));
  border-radius: 8px;
  padding: 6px 28px 6px 10px;
  font-size: 13px;
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 10 10'><path fill='none' stroke='%2394a3b8' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round' d='M2 4l3 3 3-3'/></svg>");
  background-repeat: no-repeat;
  background-position: right 8px center;
  transition: border-color 0.2s, color 0.2s;
}

.answer-depth-select:hover,
.answer-depth-select:focus {
  border-color: var(--accent);
  color: var(--accent);
  outline: none;
}

/* --- Export button --- */
.btn-export {
  background: none;
  border: none;
  border-radius: 50%;
  width: 36px;
  height: 36px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--muted);
  transition: color 0.2s, background 0.2s;
}

.btn-export:hover {
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 10%, transparent);
}

/* --- Mic / Voice input button --- */
.btn-mic {
  padding: 6px;
  border-radius: 8px;
  background: none;
  color: var(--muted);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.2s, box-shadow 0.2s;
  position: relative;
}

.btn-mic:hover {
  color: var(--accent);
}

.btn-mic.recording {
  color: #ef4444;
  box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.25);
  animation: mic-pulse 1.5s ease-in-out infinite;
}

.btn-mic.recording .mic-icon {
  stroke: #ef4444;
}

@keyframes mic-pulse {
  0%, 100% { box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.25); }
  50% { box-shadow: 0 0 0 8px rgba(239, 68, 68, 0.1); }
}

/* --- Attach image button --- */
.btn-attach {
  padding: 6px;
  border-radius: 8px;
  background: none;
  color: var(--muted);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.2s;
}

.btn-attach:hover {
  color: var(--accent);
}

/* --- Image preview chip --- */
.image-preview-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px 4px 4px;
  background: var(--surface-hover);
  border: 1px solid var(--border);
  border-radius: 20px;
  max-width: 220px;
}

.image-preview-thumb {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1;
  min-width: 0;
}

.image-preview-thumb img {
  width: 28px;
  height: 28px;
  object-fit: cover;
  border-radius: 50%;
  border: 1px solid var(--border);
}

.image-preview-name {
  font-size: 12px;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.image-preview-remove {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 14px;
  cursor: pointer;
  padding: 0 2px;
  border-radius: 50%;
  line-height: 1;
  transition: color 0.2s;
}

.image-preview-remove:hover {
  color: #ef4444;
}

/* --- Image thumbnail in user bubble --- */
.bubble-image-thumb {
  display: block;
  max-width: 200px;
  max-height: 150px;
  border-radius: 8px;
  margin-top: 6px;
  border: 1px solid rgba(255, 255, 255, 0.15);
  cursor: pointer;
  transition: opacity 0.2s;
}

.bubble-image-thumb:hover {
  opacity: 0.8;
}

/* --- Image lightbox --- */
.image-lightbox {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(0, 0, 0, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: zoom-out;
  animation: lightbox-in 0.15s ease;
}

.image-lightbox img {
  max-width: 90vw;
  max-height: 90vh;
  border-radius: 10px;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.5);
}

@keyframes lightbox-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

.hints {
  flex-shrink: 0;
  margin-top: 8px;
  color: var(--muted);
  font-size: 11px;
}

.chat.has-messages .hints {
  display: none;
}

.hints ul {
  margin: 8px 0 0;
  padding-left: 18px;
}

/* --- In-bubble tool details --- */
.bubble-tool-details {
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--muted);
}

.bubble-tool-details summary {
  cursor: pointer;
  color: var(--accent);
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.bubble-tool-details pre {
  margin: 8px 0 0;
  padding: 10px 12px;
  background: rgba(0, 0, 0, 0.2);
  border-radius: 8px;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 12px;
  color: var(--ink);
  max-height: 200px;
  overflow-y: auto;
}

/* --- Copy button --- */
.copy-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 3px 6px;
  font-size: 13px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.2s;
  color: var(--muted);
  line-height: 1;
}

.bubble.assistant {
  position: relative;
}

.bubble.assistant:hover .copy-btn {
  opacity: 1;
}

.copy-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

@media (max-width: 640px) {
  .chat-form {
    flex-direction: row;
    flex-wrap: nowrap;
    gap: 4px;
  }

  .chat-form input {
    font-size: 14px;
    padding: 10px 10px;
    min-width: 0;
  }

  .chat-form .answer-depth-select {
    /* Inside the composer the depth dropdown sits next to the Send
     * button; on narrow screens shrink the padding so the row keeps
     * fitting in one line alongside attach + mic + send. */
    padding: 6px 22px 6px 6px;
    font-size: 12px;
    background-position: right 4px center;
  }

  .chat-form button[type="submit"] {
    padding: 10px 10px;
    font-size: 0;
    min-width: 38px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .chat-form button[type="submit"]::after {
    content: "\279C";
    font-size: 16px;
  }

  .btn-mic,
  .btn-attach {
    padding: 4px;
    flex-shrink: 0;
  }

  .bubble {
    max-width: 92%;
    font-size: 14px;
    padding: 10px 12px;
  }

  .hints {
    font-size: 12px;
  }

  .hints ul {
    padding-left: 14px;
  }
}

.error-card {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 4px 2px;
}

.error-card .error-title {
  font-weight: 600;
  color: #ff8f8f;
}

.error-card .error-message {
  color: inherit;
  font-size: 14px;
  line-height: 1.4;
}

.error-card .error-hint {
  font-size: 13px;
  opacity: 0.75;
  font-style: italic;
}

/* --- Citation cards --- */
.citation-block {
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}

.citation-header {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 8px;
}

.citation-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 8px;
}

.citation-card {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px 12px;
  background: var(--surface-deep);
  border: 1px solid var(--border);
  border-radius: 10px;
  text-align: left;
  font: inherit;
  cursor: pointer;
  color: var(--ink);
  transition: border-color 0.15s, transform 0.15s;
}

.citation-card:hover:not(.disabled) {
  border-color: var(--accent);
  transform: translateY(-1px);
}

.citation-card.disabled {
  cursor: default;
  opacity: 0.7;
}

.citation-card.cited {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent) inset;
  background: color-mix(in srgb, var(--accent) 8%, var(--surface-deep));
}

/* Inline citation chip — drops into the rendered answer text in place of
 * `[p.10]` / `(p.10)` markers so the user can click straight from the
 * sentence to the rendered page. */
.citation-chip {
  display: inline;
  padding: 0 4px;
  margin: 0 1px;
  border: 1px solid color-mix(in srgb, var(--accent) 50%, var(--border));
  border-radius: 5px;
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  color: var(--accent);
  font-family: inherit;
  font-size: 0.85em;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
  line-height: 1.2;
}

.citation-chip:hover {
  background: color-mix(in srgb, var(--accent) 24%, transparent);
  border-color: var(--accent);
}

.citation-missing {
  margin-top: 8px;
  padding: 6px 10px;
  border-radius: 8px;
  background: color-mix(in srgb, #ef4444 12%, transparent);
  border: 1px solid color-mix(in srgb, #ef4444 35%, transparent);
  color: #ffb4b4;
  font-size: 11px;
  line-height: 1.4;
}

/* --- Live trace strip (per-tool latency, streamed during turn) --- */
.trace-strip {
  align-self: flex-start;
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin: 4px 0 4px;
  max-width: 85%;
}

.trace-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 10px;
  border-radius: 8px;
  background: var(--surface-deep);
  border: 1px solid var(--border);
  font-size: 11px;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.trace-row.running {
  border-color: color-mix(in srgb, var(--accent) 50%, var(--border));
}

.trace-row.ok {
  border-color: color-mix(in srgb, #22c55e 50%, var(--border));
}

.trace-row.error {
  border-color: color-mix(in srgb, #ef4444 60%, var(--border));
  color: #ffb4b4;
}

.trace-spinner {
  display: inline-flex;
  width: 12px;
  align-items: center;
  justify-content: center;
}

.trace-row.running .trace-spinner::before {
  content: "";
  width: 10px;
  height: 10px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.trace-row.ok .trace-spinner {
  color: #22c55e;
  font-weight: 700;
}

.trace-name {
  font-weight: 600;
  color: var(--accent);
}

.trace-args {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  opacity: 0.85;
}

.trace-latency {
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  font-size: 10px;
}

/* --- Why this answer? trace expander --- */
.why-answer {
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--muted);
}

.why-answer summary {
  cursor: pointer;
  color: var(--accent);
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  list-style: none;
}

.why-answer summary::-webkit-details-marker {
  display: none;
}

.why-answer summary::before {
  content: "▸";
  display: inline-block;
  width: 1em;
  transition: transform 0.15s;
}

.why-answer[open] summary::before {
  transform: rotate(90deg);
}

.why-answer-intro {
  margin: 8px 0;
  padding: 6px 10px;
  border-left: 3px solid var(--accent);
  background: var(--surface-deep);
  border-radius: 0 6px 6px 0;
  color: var(--ink);
  font-style: italic;
}

.why-answer-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 360px;
  overflow-y: auto;
}

.why-answer-passage {
  padding: 8px 10px;
  background: var(--surface-deep);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.why-answer-passage.cited {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 10%, var(--surface-deep));
}

.why-answer-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 8px;
  font-size: 11px;
  margin-bottom: 4px;
}

.why-answer-source {
  font-weight: 600;
  color: var(--ink);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 220px;
}

.why-answer-body {
  color: var(--ink);
  font-size: 12px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 120px;
  overflow-y: auto;
}

.citation-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 8px;
  font-size: 11px;
}

.citation-source {
  font-weight: 600;
  color: var(--ink);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 140px;
}

.citation-page {
  color: var(--accent);
  font-weight: 600;
}

.citation-tag {
  padding: 1px 6px;
  border-radius: 4px;
  background: color-mix(in srgb, var(--accent) 15%, transparent);
  color: var(--accent);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.citation-conf {
  margin-left: auto;
  color: var(--muted);
  font-size: 11px;
}

.citation-snippet {
  font-size: 12px;
  line-height: 1.4;
  color: var(--muted);
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.citation-loading,
.citation-error {
  color: #fff;
  background: rgba(0, 0, 0, 0.6);
  padding: 14px 22px;
  border-radius: 10px;
  font-size: 14px;
}
