/* ── Main layout ─────────────────────────────────────────────────────────── */
#main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  height: var(--app-height, 100dvh);
  overflow: hidden;
  position: relative;
}

/* ── Welcome view ────────────────────────────────────────────────────────── */
.welcome-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 24px;
  gap: 32px;
  min-height: 0;
  overflow: hidden;
}

.welcome-title {
  font-size: clamp(26px, 4vw, 44px);
  font-weight: 300;
  letter-spacing: -0.03em;
  color: var(--text);
  text-align: center;
}

.welcome-input-wrap {
  width: 100%;
  max-width: 760px;
}

/* ── Chat view ───────────────────────────────────────────────────────────── */
.chat-view {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 24px 0 8px;
  display: flex;
  flex-direction: column;
}

.chat-messages {
  width: 100%;
  max-width: 780px;
  margin: 0 auto;
  padding: 0 24px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

/* ── Message group ───────────────────────────────────────────────────────── */
.msg-group {
  display: flex;
  flex-direction: column;
  gap: 0;
  position: relative;
  /* Extra bottom space so action buttons don't clip into next group */
  margin-bottom: 18px;
}

/* User bubble — aligned right */
.msg-user {
  align-self: flex-end;
  max-width: 78%;
  padding: 10px 16px;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 18px 18px 4px 18px;
  font-size: 15px;
  color: var(--text);
  word-break: break-word;
  position: relative;
}

/* User bubble editing state */
.msg-user.editing-user {
  max-width: 86%;
  width: 86%;
  align-self: flex-end;
  border-radius: var(--radius-lg);
  border-color: rgba(74,158,255,0.4);
  box-shadow: 0 0 0 2px rgba(74,158,255,0.08);
  padding: 12px 14px 10px;
}

/* Assistant bubble - transparent */
.msg-assistant {
  align-self: flex-start;
  max-width: 92%;
  padding: 4px 0;
  font-size: 15px;
  color: var(--text);
  word-break: break-word;
  position: relative;
}

/* Assistant editing state */
.msg-assistant.editing {
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--border-bright);
  border-radius: var(--radius-lg);
  padding: 14px 16px 52px;
  width: 100%;
  max-width: 100%;
  box-shadow: 0 0 0 2px rgba(74,158,255,0.1);
}

.assistant-timeline {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.assistant-timeline-segment {
  min-width: 0;
}

.assistant-timeline-text > :first-child {
  margin-top: 0;
}

.assistant-timeline-text > :last-child {
  margin-bottom: 0;
}

.assistant-flow-divider {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 2px 0;
}

.assistant-flow-line {
  flex: 1;
  min-width: 18px;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--border-bright), transparent);
}

.assistant-flow-label {
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.assistant-stream-status {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
  color: var(--text-muted);
  font-size: 12px;
}

.assistant-stream-spinner {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.assistant-stream-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
  opacity: 0.35;
  animation: thinkBounce 1.2s infinite ease-in-out;
}

.assistant-stream-dot:nth-child(2) {
  animation-delay: 0.15s;
}

.assistant-stream-dot:nth-child(3) {
  animation-delay: 0.3s;
}

.msg-assistant-error {
  width: min(100%, 680px);
  padding: 14px 16px;
  border: 1px solid rgba(248, 113, 113, 0.35);
  border-radius: var(--radius-lg);
  background: rgba(127, 29, 29, 0.18);
}

.assistant-error-copy {
  color: #fca5a5;
  line-height: 1.5;
}

.assistant-error-inline {
  display: inline-flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 12px;
  padding: 12px 14px;
  border-radius: var(--radius-lg);
  border: 1px solid rgba(248, 113, 113, 0.28);
  background: rgba(127, 29, 29, 0.15);
}

.assistant-error-inline-copy {
  color: #fca5a5;
}

.assistant-error-retry,
.assistant-error-inline-retry {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  margin-top: 12px;
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid rgba(248, 113, 113, 0.35);
  color: #fecaca;
  background: rgba(127, 29, 29, 0.22);
  transition: background var(--transition), border-color var(--transition), color var(--transition);
}

.assistant-error-retry:hover,
.assistant-error-inline-retry:hover {
  background: rgba(127, 29, 29, 0.32);
  border-color: rgba(248, 113, 113, 0.55);
  color: #fff1f2;
}

.html-sandbox-placeholder {
  margin: 10px 0;
}

.html-sandbox-frame {
  width: 100%;
  min-height: 80px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--bg-raised);
}

.html-sandbox-warning {
  padding: 12px 14px;
  border-radius: var(--radius-md);
  border: 1px solid rgba(250, 204, 21, 0.28);
  background: rgba(250, 204, 21, 0.08);
}

.html-sandbox-warning-title {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--yellow);
}

.html-sandbox-warning-copy {
  margin-top: 6px;
  font-size: 13px;
  color: var(--text-dim);
}

.html-sandbox-warning-btn {
  margin-top: 10px;
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid rgba(250, 204, 21, 0.3);
  color: var(--text);
  background: rgba(250, 204, 21, 0.12);
}

/* ── Version navigator ───────────────────────────────────────────────────── */
/*
 * Sits BELOW the user bubble, right-aligned, always visible when present.
 * (Version nav is only added to user messages.)
 */
.msg-version-nav {
  align-self: flex-end;
  display: flex;
  align-items: center;
  gap: 2px;
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 3px;
  /* Always visible — no opacity:0 trick since it's structural */
}

.msg-version-nav button {
  display: flex; align-items: center; justify-content: center;
  width: 22px; height: 22px; border-radius: 50%;
  color: var(--text-muted);
  font-size: 16px; line-height: 1;
  transition: color var(--transition), background var(--transition);
}
.msg-version-nav button:hover:not(:disabled) { color: var(--text); background: var(--bg-hover); }
.msg-version-nav button:disabled { opacity: 0.3; cursor: default; }

.msg-version-nav-left {
  align-self: flex-start;
}

/* ── Message action buttons ──────────────────────────────────────────────── */
/*
 * Action buttons live BELOW their bubble in the flex column.
 * They fade in on group hover.
 */
.msg-actions {
  display: flex;
  gap: 2px;
  opacity: 0;
  transition: opacity var(--transition);
  pointer-events: none;
  margin-top: 4px;
}

/* User actions: right-aligned */
.msg-actions-right {
  align-self: flex-end;
  flex-direction: row-reverse;
}

/* Assistant actions: left-aligned with a tiny top gap */
.msg-actions-left {
  align-self: flex-start;
  padding-top: 1px;
}

.msg-group:hover .msg-actions,
.msg-group:focus-within .msg-actions {
  opacity: 1;
  pointer-events: auto;
}

.msg-action-btn {
  display: flex; align-items: center; justify-content: center;
  width: 26px; height: 26px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  transition: color var(--transition), background var(--transition);
}
.msg-action-btn:hover { color: var(--text); background: var(--bg-hover); }

/* Edit toolbar (assistant bubble) */
.edit-actions {
  position: absolute;
  bottom: 10px;
  right: 12px;
  display: flex;
  gap: 6px;
}

/* ── Edit toolbar for user messages ─────────────────────────────────────── */
.edit-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

.edit-tool-row {
  display: flex;
  align-items: center;
  gap: 3px;
  flex-wrap: wrap;
  flex: 1;
}

.edit-tool-btn {
  /* Inherits .tool-btn-sm styles */
}

.edit-attach-btn {
  display: flex; align-items: center; justify-content: center;
  width: 26px; height: 26px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  border: 1px solid var(--border-bright);
  transition: color var(--transition), background var(--transition);
  flex-shrink: 0;
}
.edit-attach-btn:hover { color: var(--text); background: var(--bg-hover); }

.edit-btn-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.edit-send-btn {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 12px !important;
  padding: 5px 12px !important;
}

.edit-file-preview-row {
  display: none;
  flex-wrap: wrap;
  gap: 6px;
  padding: 4px 0 0;
}

/* ── Thinking animation ──────────────────────────────────────────────────── */
.msg-thinking {
  display: flex; gap: 4px; align-items: center;
  padding: 10px 0;
}
.thinking-dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--text-muted);
  animation: thinkBounce 1.4s infinite ease-in-out;
}
.thinking-dot:nth-child(2) { animation-delay: 0.2s; }
.thinking-dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes thinkBounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
  40% { transform: translateY(-6px); opacity: 1; }
}

/* ── Code blocks ─────────────────────────────────────────────────────────── */
.code-block {
  margin: 10px 0;
  border-radius: var(--radius-md);
  overflow: hidden;
  border: 1px solid var(--border);
  background: #0d0e11;
}
[data-theme="light"] .code-block { background: #1a1b20; }

.code-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 14px;
  background: rgba(255,255,255,0.04);
  border-bottom: 1px solid var(--border);
}
.code-lang {
  font-size: 12px; font-family: var(--font-mono);
  color: var(--text-muted);
}
.code-copy-btn {
  font-size: 11px; color: var(--text-muted);
  padding: 2px 8px; border-radius: 4px;
  cursor: pointer;
  transition: color var(--transition), background var(--transition);
}
.code-copy-btn:hover { color: var(--text); background: var(--bg-hover); }

.code-block pre {
  padding: 14px 16px;
  overflow-x: auto;
  font-size: 13px;
  font-family: var(--font-mono);
  line-height: 1.6;
  color: #e2e8f0;
}

.code-block pre code,
.code-block code {
  background: none !important;
  padding: 0 !important;
  border-radius: 0 !important;
  font-size: inherit !important;
  color: inherit !important;
}

/* Inline code */
.msg-assistant .inline-code,
.msg-user .inline-code,
.msg-assistant > p > code,
.msg-user > p > code {
  font-family: var(--font-mono);
  font-size: 0.88em;
  background: var(--bg-active);
  padding: 1px 5px;
  border-radius: 4px;
}

.code-color-token {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
}

.code-color-swatch {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  -webkit-user-select: none;
  pointer-events: none;
}

/* Tables */
.msg-assistant table {
  border-collapse: collapse;
  width: 100%;
  margin: 10px 0;
  font-size: 14px;
}
.msg-assistant th, .msg-assistant td {
  border: 1px solid var(--border-bright);
  padding: 7px 12px;
  text-align: left;
}
.msg-assistant th { background: var(--bg-raised); font-weight: 600; }
.msg-assistant tr:nth-child(even) { background: rgba(255,255,255,0.02); }

/* Images/video/audio in chat */
.msg-media {
  max-width: 420px;
  border-radius: var(--radius-md);
  overflow: hidden;
  margin: 8px 0;
  position: relative;
}
.msg-media img {
  display: block; width: 100%;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: opacity var(--transition);
}
.msg-media img:hover { opacity: 0.9; }
.msg-media video, .msg-media audio {
  width: 100%;
  border-radius: var(--radius-md);
}
.media-download-btn {
  position: absolute; top: 8px; right: 8px;
  padding: 4px 10px; border-radius: var(--radius-full);
  background: rgba(0,0,0,0.6); color: white; font-size: 11px;
  opacity: 0; transition: opacity var(--transition);
}
.msg-media:hover .media-download-btn { opacity: 1; }

.media-load-error {
  opacity: 0.45;
}

/* Tool call bubble */
.msg-tool-call {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px; border-radius: var(--radius-full);
  background: var(--bg-raised); border: 1px solid var(--border);
  font-size: 12px; color: var(--text-dim);
  cursor: pointer; margin: 4px 0;
  transition: border-color var(--transition), color var(--transition);
}
.msg-tool-call:hover { border-color: var(--blue-bright); color: var(--text); }
.msg-tool-call svg { color: var(--yellow); }

.msg-tool-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.msg-tool-call[data-state="pending"] {
  border-color: rgba(250, 204, 21, 0.32);
  background: rgba(250, 204, 21, 0.08);
}

.msg-tool-call[data-state="canceled"] {
  opacity: 0.72;
}

.response-edit-log {
  margin-top: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--bg-raised);
}

.response-edit-log summary {
  cursor: pointer;
  padding: 8px 10px;
  font-size: 12px;
  color: var(--text-dim);
}

.response-edit-item {
  padding: 8px 10px 10px;
  border-top: 1px solid var(--border);
}

.response-edit-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.response-edit-reason {
  margin-top: 4px;
  font-size: 13px;
}

.response-edit-compare {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-top: 8px;
}

.response-edit-pane {
  min-width: 0;
  padding: 8px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: rgba(255,255,255,0.02);
}

.response-edit-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.response-edit-pane pre {
  margin: 6px 0 0;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text-dim);
}

/* Span colors from AI */
span[data-color="green"]  { color: #4ade80; }
span[data-color="blue"]   { color: #60a5fa; }
span[data-color="red"]    { color: #f87171; }
span[data-color="orange"] { color: #fb923c; }
span[data-color="yellow"] { color: #facc15; }
span[data-color="purple"] { color: #c084fc; }
span[data-color="teal"]   { color: #2dd4bf; }
span[data-color="gold"]   { color: #e5c846; }
span[data-color="coral"]  { color: #f97316; }
span[data-color="pink"]   { color: #f472b6; }

/* Pasted content chip */
.pasted-chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px; border-radius: var(--radius-full);
  background: var(--bg-raised); border: 1px solid var(--border-bright);
  font-size: 12px; color: var(--text-dim); cursor: pointer;
  margin: 4px 0;
}
.pasted-chip .chip-remove {
  width: 14px; height: 14px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 10px; color: var(--text-muted);
  transition: background var(--transition);
}
.pasted-chip .chip-remove:hover { background: var(--bg-hover); color: var(--text); }

/* Attached image thumb */
.attach-thumb {
  display: inline-block;
  width: 48px; height: 48px;
  border-radius: var(--radius-sm); overflow: hidden;
  position: relative; cursor: pointer;
}
.attach-thumb img { width: 100%; height: 100%; object-fit: cover; }
.attach-thumb .thumb-remove {
  position: absolute; top: -4px; right: -4px;
  width: 16px; height: 16px; border-radius: 50%;
  background: var(--bg); border: 1px solid var(--border-bright);
  display: flex; align-items: center; justify-content: center;
  font-size: 9px; color: var(--text);
}

/* SVG side panel */
#svg-panel {
  position: fixed; right: 0; top: 0; bottom: 0;
  width: 320px; background: var(--bg-surface);
  border-left: 1px solid var(--border-bright);
  z-index: 200; display: flex; flex-direction: column;
  animation: slideInRight 0.2s ease;
}
#svg-panel-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
}
#svg-panel-content { flex: 1; overflow: auto; padding: 16px; }
#svg-panel img { max-width: 100%; border-radius: var(--radius-sm); }

.media-editor-panel {
  position: absolute;
  top: 12px;
  right: 12px;
  bottom: 12px;
  width: min(42vw, 540px);
  display: flex;
  flex-direction: column;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-bright);
  background: var(--bg-surface);
  box-shadow: var(--shadow-lg);
  z-index: 40;
}

.media-editor-header,
.media-editor-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}

.media-editor-footer {
  justify-content: flex-end;
  border-top: 1px solid var(--border);
  border-bottom: none;
}

.media-editor-title {
  font-size: 15px;
  font-weight: 600;
}

.media-editor-meta {
  margin-top: 3px;
  font-size: 12px;
  color: var(--text-muted);
}

.media-editor-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
}

.media-editor-tool {
  padding: 6px 9px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-bright);
  color: var(--text-dim);
  font-size: 12px;
}

.media-editor-content {
  flex: 1;
  min-height: 0;
  padding: 14px;
  overflow: auto;
}

.media-text-editor,
.media-rich-editor {
  width: 100%;
  min-height: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--bg-raised);
  color: var(--text);
  padding: 14px;
  font-size: 14px;
  line-height: 1.6;
  outline: none;
}

.media-text-editor {
  resize: none;
}

.media-rich-editor blockquote {
  margin: 0;
  padding-left: 12px;
  border-left: 2px solid var(--border-bright);
  color: var(--text-dim);
}

.media-editor-status {
  padding: 0 14px 10px;
  font-size: 12px;
  color: var(--text-muted);
}

.media-editor-status[data-kind="success"] {
  color: var(--plan-core);
}

.media-editor-status[data-kind="error"] {
  color: #f87171;
}

/* Generating animation */
.msg-generating {
  animation: generatingGlow 2s ease infinite;
}
@keyframes generatingGlow {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.75; }
}

/* ══════════════════════════════════════════════════════════════════════════
   MOBILE CHAT
   ══════════════════════════════════════════════════════════════════════════ */

@media (max-width: 768px) {

  /* Main: full height, strict no-overflow */
  #main {
    height: var(--app-height, 100dvh);
    max-height: var(--app-height, 100dvh);
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }

  /* Chat view: the only vertical scrollbar */
  .chat-view {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-y: contain;
    /* Account for fixed top bar */
    padding-top: 4px;
  }

  /* Welcome view: centered, no scroll */
  .welcome-view {
    flex: 1;
    min-height: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding-top: 0 !important;
    padding-bottom: 40px !important;
    gap: 20px !important;
  }

  .welcome-title {
    text-align: center;
    font-size: clamp(22px, 6vw, 36px) !important;
  }

  /* Messages: no extra horizontal overflow */
  .chat-messages {
    padding: 0 12px;
    max-width: 100%;
    /* Ensure nothing bleeds sideways */
    overflow-x: hidden;
  }

  /* Bubbles */
  .msg-user {
    max-width: 88%;
    font-size: 14px;
    padding: 9px 14px;
  }

  .msg-user.editing-user {
    max-width: 98%;
    width: 98%;
  }

  .msg-assistant {
    max-width: 100%;
    font-size: 14px;
  }

  /* Code blocks: horizontal scroll only */
  .code-block {
    overflow: hidden;
  }

  .code-block pre {
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    font-size: 12px;
  }

  /* Tables: horizontal scroll only */
  .msg-assistant table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
  }

  /* Edit toolbar on mobile: stack tool row and button row */
  .edit-toolbar {
    flex-direction: column;
    align-items: stretch;
  }

  .edit-tool-row {
    flex-wrap: wrap;
  }

  /* On mobile, hide tool label text in edit toolbar */
  .edit-tool-btn span {
    display: none;
  }
  .edit-tool-btn {
    padding: 3px 6px;
    min-width: 28px;
    justify-content: center;
  }

  .edit-btn-row {
    justify-content: flex-end;
  }

  /* Action buttons: always show on mobile (no hover) */
  .msg-actions {
    opacity: 1;
    pointer-events: auto;
  }

  /* Version nav: always visible, compact */
  .msg-version-nav {
    font-size: 11px;
  }

  .media-editor-panel {
    inset: 0;
    width: 100%;
    border-radius: 0;
  }

  .response-edit-compare {
    grid-template-columns: 1fr;
  }
}
