/* ==============================================
   CSS VARIABLES FOR THEMING
   ============================================== */
:root {
  /* Light theme (default) */
  --bg-primary: #f7f7f8;
  --bg-secondary: #ffffff;
  --bg-tertiary: #f3f4f6;
  --bg-input: #ffffff;
  --bg-code: #1f2937;
  --bg-code-inline: #f3f4f6;

  --text-primary: #1a1a1a;
  --text-secondary: #2d2d2d;
  --text-muted: #6b7280;
  --text-accent: #2563eb;
  --text-code-inline: #dc2626;

  --border-color: #e5e5e5;
  --border-light: #e5e7eb;
  --border-focus: #3b82f6;

  --user-bg: #dbeafe;
  --user-border: #bfdbfe;
  --user-text: #1e40af;

  --success: #10b981;
  --error: #ef4444;
  --warning: #f59e0b;

  --shadow-sm: 0 1px 3px rgba(0,0,0,0.05);
  --shadow-md: 0 -2px 10px rgba(0,0,0,0.05);

  --reasoning-bg: linear-gradient(180deg, #f5f3ff 0%, #eef2ff 100%);
  --reasoning-border: #ddd6fe;
  --reasoning-accent: #8b5cf6;
  --reasoning-text: #312e81;
  --reasoning-label-bg: #ede9fe;
  --reasoning-label-text: #5b21b6;

  --tool-border: #3b82f6;
  --tool-completed: #10b981;
}

[data-theme="dark"] {
  /* Dark theme */
  --bg-primary: #0f172a;
  --bg-secondary: #1e293b;
  --bg-tertiary: #334155;
  --bg-input: #1e293b;
  --bg-code: #0f172a;
  --bg-code-inline: #334155;

  --text-primary: #f1f5f9;
  --text-secondary: #e2e8f0;
  --text-muted: #94a3b8;
  --text-accent: #60a5fa;
  --text-code-inline: #fca5a5;

  --border-color: #334155;
  --border-light: #475569;
  --border-focus: #60a5fa;

  --user-bg: #1e3a5f;
  --user-border: #2563eb;
  --user-text: #93c5fd;

  --success: #34d399;
  --error: #f87171;
  --warning: #fbbf24;

  --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-md: 0 -2px 10px rgba(0,0,0,0.3);

  --reasoning-bg: linear-gradient(180deg, #1e1b4b 0%, #1e293b 100%);
  --reasoning-border: #4c1d95;
  --reasoning-accent: #a78bfa;
  --reasoning-text: #c4b5fd;
  --reasoning-label-bg: #4c1d95;
  --reasoning-label-text: #c4b5fd;

  --tool-border: #3b82f6;
  --tool-completed: #34d399;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--bg-primary);
  color: var(--text-secondary);
  height: 100vh;
  display: flex;
  flex-direction: column;
  font-size: var(--font-size, 16px);
  transition: background 0.3s ease, color 0.3s ease;
  overflow: hidden;
}

/* ==============================================
   APP CONTAINER & SIDEBAR LAYOUT
   ============================================== */
.app-container {
  display: flex;
  flex: 1;
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  width: 260px;
  min-width: 180px;
  max-width: 500px;
  flex-shrink: 0;
  background: var(--bg-secondary);
  border-right: none;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

.sidebar.resizing {
  user-select: none;
}

/* Resize handle between sidebar and content */
.sidebar-resize-handle {
  position: absolute;
  top: 0;
  right: 0;
  width: 4px;
  height: 100%;
  background: var(--border-color);
  cursor: col-resize;
  z-index: 10;
  transition: background 0.15s;
}

.sidebar-resize-handle:hover,
.sidebar-resize-handle.dragging {
  background: var(--accent);
}

.sidebar-resize-handle::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 2px;
  height: 30px;
  background: var(--text-muted);
  border-radius: 2px;
  opacity: 0;
  transition: opacity 0.15s;
}

.sidebar-resize-handle:hover::after,
.sidebar-resize-handle.dragging::after {
  opacity: 0.5;
}

.sidebar.collapsed {
  width: 0;
  border-right: none;
  overflow: hidden;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

.sidebar-header h2 {
  font-size: 1.125rem;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0;
}

.sidebar-btn {
  background: var(--bg-tertiary);
  border: 1px solid var(--border-light);
  border-radius: 0.375rem;
  padding: 0.25rem 0.5rem;
  font-size: 1rem;
  cursor: pointer;
  color: var(--text-muted);
  transition: all 0.15s;
}

.sidebar-btn:hover {
  background: var(--border-light);
  color: var(--text-primary);
}

.chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
}

.chat-item {
  padding: 0.625rem 0.75rem;
  border-radius: 0.5rem;
  cursor: pointer;
  margin-bottom: 0.25rem;
  transition: background 0.15s;
  border: 1px solid transparent;
  overflow: hidden;  /* Enable text truncation in children */
  min-width: 0;      /* Allow flex shrinking */
}

.chat-item:hover {
  background: var(--bg-tertiary);
}

.chat-item.active {
  background: var(--bg-tertiary);
  border-color: var(--border-focus);
}

.chat-item-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;  /* Allow flex container to shrink for child ellipsis */
}

.chat-item-icon {
  flex-shrink: 0;
  font-size: 1rem;
  opacity: 0.7;
}

.chat-item-title {
  font-size: 1rem;
  font-weight: 500;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  min-width: 0;  /* Allow flex item to shrink below content size for proper ellipsis */
}

.chat-item-expand {
  font-size: 0.75rem;
  color: var(--text-secondary);
  transition: transform 0.2s;
  flex-shrink: 0;
}

.chat-item.expanded .chat-item-expand {
  transform: rotate(90deg);
}

.chat-item-meta {
  font-size: 0.875rem;
  color: var(--text-secondary);
  margin-top: 0.25rem;
}

.chat-item-messages {
  display: none;
  margin-top: 0.5rem;
  padding-left: 0.5rem;
  border-left: 2px solid var(--border-light);
}

.chat-item.expanded .chat-item-messages {
  display: block;
}

.message-item {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.375rem 0.5rem;
  font-size: 0.875rem;
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: 0.25rem;
  margin-bottom: 0.125rem;
  transition: background 0.15s;
}

.message-item-icon {
  flex-shrink: 0;
  font-size: 0.75rem;
  opacity: 0.6;
}

.message-item-text {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  min-width: 0;
}

.message-item-time {
  flex-shrink: 0;
  font-size: 0.75rem;
  color: var(--text-muted);
  opacity: 0.7;
  margin-left: auto;
}

.message-item:hover {
  background: var(--bg-secondary);
  color: var(--text-primary);
}

.chat-item-actions {
  display: none;
  gap: 0.25rem;
}

.chat-item:hover .chat-item-actions {
  display: flex;
}

.chat-action-btn {
  background: transparent;
  border: none;
  padding: 0.125rem 0.25rem;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 0.75rem;
  border-radius: 0.25rem;
  transition: all 0.15s;
}

.chat-action-btn:hover {
  background: var(--bg-secondary);
  color: var(--error);
}

.sidebar-toggle {
  position: fixed;
  left: 0.5rem;
  top: 50%;
  transform: translateY(-50%);
  z-index: 100;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 0.5rem;
  padding: 0.5rem;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 1rem;
  display: none;
  transition: all 0.15s;
}

.sidebar-toggle:hover {
  background: var(--bg-tertiary);
  color: var(--text-primary);
}

.sidebar.collapsed ~ .sidebar-toggle {
  display: flex;
}

.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

/* Load more messages indicator */
.load-more-indicator {
  text-align: center;
  padding: 0.75rem 1rem;
  color: var(--text-muted);
  font-size: 0.8125rem;
  border-bottom: 1px solid var(--border-light);
  background: var(--bg-secondary);
  position: sticky;
  top: 0;
  z-index: 5;
}

.load-more-indicator span {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
}

/* Scrollbar styling for sidebar */
.chat-list::-webkit-scrollbar {
  width: 6px;
}
.chat-list::-webkit-scrollbar-track {
  background: transparent;
}
.chat-list::-webkit-scrollbar-thumb {
  background: var(--border-light);
  border-radius: 999px;
}
.chat-list::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* Mobile responsive sidebar */
@media (max-width: 720px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 99;
    transform: translateX(0);
  }

  .sidebar.collapsed {
    transform: translateX(-100%);
    width: 260px;
  }

  .sidebar-toggle {
    display: flex;
  }

  .sidebar:not(.collapsed) ~ .sidebar-toggle {
    left: 270px;
  }
}

header {
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
  padding: 0.75rem clamp(0.75rem, 2vw, 1.5rem);
  box-shadow: var(--shadow-sm);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  transition: background 0.3s ease, border-color 0.3s ease;
}

h1 {
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--text-primary);
}

/* Header controls styling */
.header-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

/* Theme toggle button */
.theme-toggle {
  background: var(--bg-tertiary);
  border: 1px solid var(--border-light);
  border-radius: 0.5rem;
  padding: 0.5rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, border-color 0.2s;
  color: var(--text-muted);
  font-size: 1.1rem;
  line-height: 1;
}

.theme-toggle:hover {
  background: var(--border-light);
  color: var(--text-primary);
}

/* Font size controls */
.font-controls {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-light);
  border-radius: 0.5rem;
  padding: 0.25rem;
}

.font-btn {
  background: transparent;
  border: none;
  border-radius: 0.375rem;
  padding: 0.375rem 0.5rem;
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text-muted);
  transition: background 0.2s, color 0.2s;
  line-height: 1;
}

.font-btn:hover {
  background: var(--bg-secondary);
  color: var(--text-primary);
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.control {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
}

.control label {
  font-size: 0.8125rem;
  color: var(--text-muted);
  white-space: nowrap;
}

.select {
  appearance: none;
  -webkit-appearance: none;
  border: 1px solid var(--border-light);
  background: var(--bg-input);
  color: var(--text-primary);
  border-radius: 0.75rem;
  padding: 0.5rem 2rem 0.5rem 0.75rem;
  font-size: 0.9375rem;
  line-height: 1.2;
  cursor: pointer;
  background-image:
    linear-gradient(45deg, transparent 50%, var(--text-muted) 50%),
    linear-gradient(135deg, var(--text-muted) 50%, transparent 50%);
  background-position:
    calc(100% - 18px) 55%,
    calc(100% - 12px) 55%;
  background-size: 6px 6px, 6px 6px;
  background-repeat: no-repeat;
  transition: background 0.2s, border-color 0.2s, color 0.2s;
}

.select:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.select-sm {
  padding: 0.375rem 2rem 0.375rem 0.625rem;
  border-radius: 0.625rem;
  font-size: 0.875rem;
}

#chat-container {
  flex: 1;
  overflow-y: auto;
  padding: clamp(0.75rem, 2vw, 1.5rem);
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.hidden {
  display: none !important;
}

/* Controls row - followup buttons + model selector */
.controls-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.25rem;
}

.model-select-wrapper {
  flex-shrink: 0;
  margin-left: auto;  /* Always align to right edge */
  margin-right: calc(36px + 0.75rem);  /* Align with chatbox right edge (36px button + gap) */
}

.model-select-wrapper .select {
  min-width: 120px;
  font-size: 0.8rem;
  padding: 0.25rem 0.5rem;
}

.followup-panel {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0;
}

.followup-panel-title {
  font-size: 0.65rem;
  font-weight: 500;
  color: var(--text-tertiary);
  text-transform: uppercase;
  letter-spacing: 0.3px;
  white-space: nowrap;
}

.followup-buttons {
  display: flex;
  gap: 0.25rem;
  flex-wrap: nowrap;
  align-items: center;
  overflow: visible;
  padding-top: 10px;  /* Room for time badges */
  margin-top: -2px;
}

.followup-btn {
  position: relative;
  background: transparent;
  color: var(--text-primary);
  border: none;
  padding: 0.25rem;
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  transition: transform 0.1s, opacity 0.15s;
  opacity: 0.8;
}

.followup-btn:hover {
  transform: scale(1.15);
  opacity: 1;
}

.followup-btn:active {
  transform: scale(0.9);
}

/* Prefetch loading state - subtle pulse animation */
.followup-btn.prefetch-loading {
  position: relative;
  animation: prefetch-pulse 1.5s ease-in-out infinite;
}

.followup-btn.prefetch-loading::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 50%;
  transform: translateX(-50%);
  width: 4px;
  height: 4px;
  background: var(--accent);
  border-radius: 50%;
  animation: prefetch-dot 0.6s ease-in-out infinite;
}

@keyframes prefetch-pulse {
  0%, 100% { opacity: 0.6; }
  50% { opacity: 0.9; }
}

@keyframes prefetch-dot {
  0%, 100% { opacity: 0.3; }
  50% { opacity: 1; }
}

/* Prefetch ready state - green dot indicator */
.followup-btn.prefetch-ready {
  position: relative;
  opacity: 1;
}

.followup-btn.prefetch-ready::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 50%;
  transform: translateX(-50%);
  width: 6px;
  height: 6px;
  background: var(--success);
  border-radius: 50%;
  box-shadow: 0 0 4px var(--success);
}

/* Elapsed time indicator on prefetch buttons */
.prefetch-time {
  position: absolute;
  top: -14px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.65rem;
  color: var(--success);
  background: var(--bg-primary);
  padding: 2px 4px;
  border-radius: 3px;
  white-space: nowrap;
  pointer-events: none;
  z-index: 10;
  border: 1px solid var(--success);
  font-weight: 500;
}

/* Disabled state for followup buttons when Stage 1 fails */
.followup-btn.followup-disabled {
  opacity: 0.3;
  cursor: not-allowed;
  pointer-events: none;
}

.followup-btn.followup-disabled:hover {
  transform: none;
}

/* Panel processing state - main request in progress (buttons disabled) */
.followup-panel.panel-processing .followup-panel-title {
  opacity: 0.5;
}

/* Panel prefetching state - background work in progress (blinking dot at end) */
.followup-panel.panel-prefetching::after {
  content: '';
  display: inline-block;
  width: 6px;
  height: 6px;
  margin-left: 6px;
  background: var(--accent);
  border-radius: 50%;
  animation: prefetch-blink 0.8s ease-in-out infinite;
  flex-shrink: 0;
}

@keyframes prefetch-blink {
  0%, 100% { opacity: 0.3; transform: scale(0.8); }
  50% { opacity: 1; transform: scale(1.1); }
}

/* MESSAGE LAYOUT - Vertical stacking, responsive width */
.message {
  max-width: calc(100% - 1rem);  /* Nearly full width, small margin */
  width: 100%;                    /* Fill available width */
  animation: slideIn 0.2s ease-out;
}

@keyframes slideIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Both user and assistant messages stack vertically on left */
.message.user {
  align-self: flex-start;  /* Changed from flex-end - now stacks on left */
}

.message.assistant {
  align-self: flex-start;
}

.message-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.875rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-bottom: none;
  border-radius: 0.5rem 0.5rem 0 0;
  cursor: pointer;
  user-select: none;
  transition: background 0.2s, border-color 0.2s;
}

.message-header:hover {
  background: var(--border-light);
}

.message-header-text {
  flex: 1;
  font-weight: 600;
  font-size: 0.875rem;
  color: var(--text-primary);
}

.message-header-right {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  font-size: 0.75rem;
  color: var(--text-muted);
  font-weight: normal;
}

.message-header-model {
  background: var(--bg-secondary);
  padding: 0.125rem 0.375rem;
  border-radius: 0.25rem;
  border: 1px solid var(--border-light);
}

.message-header-elapsed {
  white-space: nowrap;
}

.message-header-cached {
  font-size: 0.875rem;
  color: var(--success);
  margin-left: 0.25rem;
}

/* User message header - just timestamp on right */
.message-header.user-header {
  display: flex;
  justify-content: flex-end;
  padding: 0.25rem 0.5rem;
  background: transparent;
  border: none;
  cursor: default;
}

.message-header.user-header:hover {
  background: transparent;
}

/* Timestamp styling */
.message-timestamp {
  font-size: 0.75rem;
  color: var(--text-muted);
  white-space: nowrap;
  font-family: 'SF Mono', Monaco, Consolas, 'Liberation Mono', monospace;
}

.collapse-icon {
  font-size: 0.75rem;
  color: var(--text-muted);
  transition: transform 0.2s;
}

.message.collapsed .collapse-icon {
  transform: rotate(-90deg);
}

.message-content {
  padding: 0.75rem 0.875rem;  /* Slightly increased padding for better spacing */
  border-radius: 0.5rem;
  word-wrap: break-word;
  line-height: 1.5;
  transition: max-height 0.3s ease-out, opacity 0.2s;
  /* Containment: prevent child content from affecting parent layout */
  contain: layout style;
  overflow: hidden;
}

.message.last-viewed .message-content {
  box-shadow: inset 4px 0 0 var(--accent);
}

.assistant .message-content {
  border-radius: 0 0 0.5rem 0.5rem;
}

.message.collapsed .message-content {
  max-height: 0;
  opacity: 0;
  overflow: hidden;
  padding: 0 0.875rem;
}

/* USER MESSAGE STYLING */
.user .message-content {
  background: var(--user-bg);
  color: var(--user-text);
  border: 1px solid var(--user-border);
  white-space: pre-wrap;
  transition: background 0.2s, border-color 0.2s, color 0.2s;
}

/* ASSISTANT MESSAGE STYLING */
.assistant .message-content {
  background: var(--bg-secondary);
  color: var(--text-secondary);
  border: 1px solid var(--border-color);
  border-top: none;
  transition: background 0.2s, border-color 0.2s, color 0.2s;
}

/* ============================================
   MARKDOWN CONTENT STYLING
   ============================================ */

/* Base text container */
.message-text {
  line-height: 1.6;  /* Overall line spacing for all text */
  /* Note: white-space: pre-wrap only for .plain-text - markdown HTML handles its own spacing */
}

/* Separator for error recovery messages */
.message-content hr {
  border: none;
  border-top: 1px dashed var(--border-light);
  margin: 1rem 0;
  opacity: 0.6;
}

.assistant-panel {
  width: 100%;
  border-radius: 0.75rem;
  padding: 0.75rem 0.875rem;
}

.assistant-reasoning {
  margin-bottom: 0.75rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-light);
  color: var(--text-primary);
  white-space: pre-wrap;
}

.assistant-response {
  background: var(--bg-secondary);
  border: 1px solid var(--border-light);
}


.reasoning-label {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 700;
  font-size: 0.75rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--reasoning-label-text);
  background: var(--reasoning-label-bg);
  border: 1px solid var(--reasoning-border);
  padding: 0.25rem 0.5rem;
  border-radius: 999px;
  margin-bottom: 0.75rem;
}

.reasoning-message .message-content {
  background: var(--reasoning-bg);
  border: 1px solid var(--reasoning-border);
  border-top: 1px solid var(--reasoning-border);
  border-radius: 0.75rem;
  box-shadow: 0 1px 2px rgba(17, 24, 39, 0.06);
  padding: 0.875rem 1rem;
  position: relative;
}

.reasoning-message .message-content::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0.75rem;
  bottom: 0.75rem;
  width: 4px;
  border-radius: 999px;
  background: var(--reasoning-accent);
}

.reasoning-output {
  color: var(--reasoning-text);
  font-size: 0.875rem;
  line-height: 1.55;
}

.tool-message .message-content {
  background: var(--bg-tertiary);
}

/* HEADERS (h1, h2, h3, h4)
   Adjust margin-top to control space ABOVE headers
   Adjust margin-bottom to control space BELOW headers */
.message-text h1,
.message-text h2,
.message-text h3,
.message-text h4 {
  margin-top: 1.25em;
  margin-bottom: 0.5em;
  font-weight: 600;
  color: var(--text-primary);
  line-height: 1.3;
}

/* Remove top margin from first header */
.message-text h1:first-child,
.message-text h2:first-child,
.message-text h3:first-child,
.message-text h4:first-child {
  margin-top: 0;
}

/* Header sizes */
.message-text h1 { font-size: 1.5rem; }
.message-text h2 { font-size: 1.25rem; }
.message-text h3 { font-size: 1.1rem; }
.message-text h4 { font-size: 1rem; }

/* PARAGRAPHS
   Adjust margin-bottom to control spacing between paragraphs */
.message-text p {
  margin-bottom: 0.5em;    /* Space AFTER each paragraph - INCREASE for more space */
  line-height: 1.6;        /* Line spacing within paragraphs */
}

/* Remove margin from last paragraph */
.message-text p:last-child {
  margin-bottom: 0;
}

/* LISTS (ul = bullets, ol = numbered)
   Adjust margins to control space around entire list
   Adjust padding-left to control indentation */
.message-text ul,
.message-text ol {
  margin: 0.5em 0;         /* Space ABOVE and BELOW entire list - INCREASE for more space */
  padding-left: 1.5rem;    /* How far list is indented from left */
}

/* Space between list and following paragraph */
.message-text ul + p,
.message-text ol + p {
  margin-top: 0.5em;       /* Space after list before next paragraph - INCREASE for more space */
}

/* LIST ITEMS
   Adjust margin-bottom to control spacing BETWEEN bullets/numbers */
.message-text li {
  margin-bottom: 0.25em;   /* Space BETWEEN list items - DECREASE to 0 for tighter, INCREASE for looser */
  line-height: 1.6;        /* Line spacing within multi-line list items */
}

/* Remove margin from last item in list */
.message-text li:last-child {
  margin-bottom: 0;
}

/* INLINE CODE (`code`)
   Style for code snippets within text */
.message-text code {
  background: var(--bg-code-inline);
  padding: 0.125rem 0.375rem;
  border-radius: 0.25rem;
  font-family: 'Monaco', 'Courier New', monospace;
  font-size: 0.875em;
  color: var(--text-code-inline);
}

/* CODE BLOCKS (```code```) */
.message-text pre {
  background: var(--bg-code);
  color: #f9fafb;
  padding: 1rem;
  border-radius: 0.5rem;
  overflow-x: auto;
  margin: 0.75em 0;
}

.message-text pre code {
  background: none;
  padding: 0;
  color: inherit;
  font-size: 0.875rem;
}

/* BLOCKQUOTES (> text) */
.message-text blockquote {
  border-left: 3px solid var(--border-light);
  padding-left: 1rem;
  margin: 0.5em 0;
  color: var(--text-muted);
}

/* BOLD TEXT (**text**)
   Adjust font-weight and color for emphasis */
.message-text strong {
  font-weight: 700;
  color: var(--text-primary);
}

/* ITALIC TEXT (*text*) */
.message-text em {
  font-style: italic;
}

/* LINKS */
.message-text a {
  color: var(--text-accent);
  text-decoration: underline;
}

/* HORIZONTAL RULES (---) */
.message-text hr {
  border: none;
  border-top: 1px solid var(--border-light);
  margin: 1em 0;
}

/* ============================================
   TOOL STEPS & VISUALIZATIONS
   ============================================ */

.tool-step {
  font-size: 0.875rem;
  color: var(--text-muted);
  margin-top: 0.25rem;
  padding: 0.25rem 0.5rem;
  background: var(--bg-tertiary);
  border-radius: 0.25rem;
  border-left: 2px solid var(--tool-border);
  transition: background 0.2s, border-color 0.2s;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tool-step.completed {
  border-left-color: var(--tool-completed);
  color: var(--success);
}

.tool-step.error {
  border-left-color: var(--error);
  color: var(--error);
}

.tool-trace {
  font-size: 0.75rem;
  color: var(--text-muted);
  background: var(--bg-tertiary);
  border-radius: 0.5rem;
  padding: 0.5rem 0.75rem;
  margin-top: 0.5rem;
  overflow-x: auto;
  font-family: 'Monaco', 'Courier New', monospace;
}

.viz {
  margin-top: 1rem;
  border: 1px solid var(--border-color);
  border-radius: 0.75rem;
  padding: 1rem;
  background: var(--bg-secondary);
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
}

/* Content rendering: ensure proper handling of markdown, plain text, and HTML */
/* CSS Containment: Prevent LLM-generated HTML from breaking parent layout */
.message-content .html-result,
.message-content .html-inline-result {
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  contain: layout style paint;  /* Isolate layout, styles, and paint from parent */
  isolation: isolate;           /* Create new stacking context */
}

.message-content .html-result > *,
.message-content .html-inline-result > * {
  max-width: 100% !important;   /* Override any inline max-width */
  width: auto !important;       /* Override any inline width constraints */
  margin-left: 0 !important;    /* Prevent margin: auto centering */
  margin-right: 0 !important;
}

/* Plain text preservation within message text */
.message-text.plain-text {
  white-space: pre-wrap;
  font-family: inherit;
}

table {
  border-collapse: collapse;
  width: 100%;
  max-width: 100%;
  font-size: 0.875rem;
  margin-top: 1rem;
  background: var(--bg-secondary);
  border-radius: 0.5rem;
  overflow: hidden;
}

/* Responsive table container */
.table-wrapper {
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  margin-top: 1rem;
}

.echarts {
  width: 100%;
  min-height: 280px;
  max-height: min(50vh, 500px);
  height: clamp(280px, 35vw, 450px);
  margin-top: 0.75rem;
}

th, td {
  border: 1px solid var(--border-color);
  padding: 0.5rem 0.75rem;
  text-align: left;
}

th {
  background: var(--bg-tertiary);
  font-weight: 600;
  color: var(--text-primary);
}

/* ============================================
   INPUT AREA
   ============================================ */

#input-area {
  background: var(--bg-secondary);
  border-top: 1px solid var(--border-color);
  padding: clamp(1rem, 2.5vw, 2rem) clamp(0.75rem, 2vw, 1.5rem);
  box-shadow: var(--shadow-md);
  transition: background 0.2s, border-color 0.2s;
}

#input-wrapper {
  width: 100%;
  margin: 0 auto;
  padding: 0;
  display: flex;
  gap: 0.75rem;
  align-items: flex-end;
}

#input-box {
  flex: 1;
  min-height: 40px;
  max-height: 120px;
  padding: 0.875rem 1rem;
  border: 1px solid var(--border-light);
  border-radius: 0.75rem;
  font-family: inherit;
  font-size: inherit;
  resize: none;
  overflow-y: auto;
  transition: border-color 0.2s, box-shadow 0.2s, background 0.2s;
  line-height: 1.5;
  background: var(--bg-input);
  color: var(--text-secondary);
}

#input-box:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

#input-box::placeholder {
  color: var(--text-muted);
}

/* Stacked icon buttons container */
.input-buttons {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  flex-shrink: 0;
}

/* Icon button base style */
.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 0.5rem;
  border: none;
  font-size: 1rem;
  cursor: pointer;
  transition: all 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
}

#send-btn {
  background: #2563eb;
  color: white;
}

#send-btn:hover:not(:disabled) {
  background: #1d4ed8;
  transform: scale(1.05);
}

#send-btn:disabled {
  background: var(--border-light);
  color: var(--text-muted);
  cursor: not-allowed;
}

#stop-btn {
  background: var(--error);
  color: white;
  display: none;
}

#stop-btn:hover {
  background: #dc2626;
  transform: scale(1.05);
}

/* Responsive layout: keep textarea full width; buttons move below on small screens */
@media (max-width: 720px) {
  header {
    align-items: flex-start;
  }

  .header-controls {
    width: 100%;
    justify-content: flex-start;
  }

  #input-wrapper {
    flex-direction: column;
    align-items: stretch;
  }

  .input-buttons {
    flex-direction: row;
    justify-content: flex-end;
  }

  #send-btn,
  #stop-btn {
    width: 36px;
  }

  .model-select-wrapper {
    margin-right: 0;  /* Reset on mobile - stacked layout */
  }

  .followup-title {
    display: none;
  }

}

/* In-chart toolbar (ECharts) */
.echarts-frame {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  align-items: stretch;
}

.echarts-toolbar {
  position: relative;
  align-self: flex-end;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  z-index: 1;
  padding: 0.375rem 0.5rem;
  border-radius: 0.75rem;
  background: var(--bg-secondary);
  border: 1px solid var(--border-light);
  backdrop-filter: blur(6px);
}

.echarts-toolbar label {
  font-size: 0.75rem;
  color: var(--text-muted);
  white-space: nowrap;
}

.typing-indicator {
  display: inline-block;
  padding: 0.5rem 1rem;
  background: var(--bg-tertiary);
  border-radius: 1rem;
  margin-top: 0.5rem;
}

.typing-indicator span {
  display: inline-block;
  width: 8px;
  height: 8px;
  background: var(--text-muted);
  border-radius: 50%;
  margin: 0 2px;
  animation: bounce 1.4s infinite ease-in-out both;
}

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

@keyframes bounce {
  0%, 80%, 100% { transform: scale(0); }
  40% { transform: scale(1); }
}

/* Scrollbar styling for chat container */
#chat-container::-webkit-scrollbar {
  width: 8px;
}
#chat-container::-webkit-scrollbar-track {
  background: transparent;
}
#chat-container::-webkit-scrollbar-thumb {
  background: var(--border-light);
  border-radius: 999px;
}
#chat-container::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}
