/* ===== Reset & Variables ===== */
* { margin: 0; padding: 0; box-sizing: border-box; }

.hidden { display: none !important; }

:root {
  --sidebar-w: 260px;
  --radius-lg: 16px;
  --radius-md: 12px;
  --radius-sm: 8px;
  --radius-xs: 4px;
  --transition: 0.2s ease;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans SC', sans-serif;
  --font-mono: 'SF Mono', 'Fira Code', 'JetBrains Mono', Consolas, monospace;
  --danger: #ef4444;
  --ai-avatar-url: url('icon.png');
}

/* ===== Dark Theme (default) ===== */
[data-theme="dark"] {
  --bg-sidebar: #171717;
  --bg-main: #212121;
  --bg-input: #2f2f2f;
  --bg-hover: #2a2a2a;
  --bg-modal: #1e1e1e;
  --bg-code: #1a1a1a;
  --bg-backdrop: rgba(0,0,0,0.5);
  --border: #3a3a3a;
  --text-1: #ececec;
  --text-2: #b4b4b4;
  --text-3: #8e8e8e;
  --accent: #10a37f;
  --accent-hover: #0d8c6d;
  --shadow: 0 4px 24px rgba(0,0,0,0.4);
  --scrollbar-thumb: #3a3a3a;
}

/* ===== Light Theme ===== */
[data-theme="light"] {
  --bg-sidebar: #f9f9f9;
  --bg-main: #ffffff;
  --bg-input: #f4f4f4;
  --bg-hover: #ececec;
  --bg-modal: #ffffff;
  --bg-code: #f6f6f6;
  --bg-backdrop: rgba(0,0,0,0.3);
  --border: #e5e5e5;
  --text-1: #1a1a1a;
  --text-2: #6b6b6b;
  --text-3: #999999;
  --accent: #10a37f;
  --accent-hover: #0d8c6d;
  --shadow: 0 4px 24px rgba(0,0,0,0.08);
  --scrollbar-thumb: #c5c5c5;
}

html { height: 100%; }
body { height: 100%; background: var(--bg-main); color: var(--text-1); font-family: var(--font); line-height: 1.6; overscroll-behavior: none; }

#app { height: 100vh; height: 100dvh; display: flex; }

/* Initial paint mode guard. JS removes data-boot-mode after switchMode() takes over. */
html[data-boot-mode="image"] #mode-chat-btn {
  color: var(--text-3);
}

html[data-boot-mode="image"] #mode-image-btn {
  color: #fff;
}

html[data-boot-mode] .mode-btn {
  transition: none;
}

html[data-boot-mode="image"] .mode-switch::before {
  transform: translateX(100%);
  transition: none;
}

html[data-boot-mode="image"] #messages,
html[data-boot-mode="image"] #welcome,
html[data-boot-mode="image"] #input-area,
html[data-boot-mode="image"] #conv-token-summary {
  display: none !important;
}

html[data-boot-mode="image"] #image-workspace {
  display: block !important;
}

html[data-boot-mode="image"] #image-input-area {
  display: block !important;
}

html[data-boot-sidebar="collapsed"] #sidebar {
  margin-left: calc(var(--sidebar-w) * -1);
  opacity: 0;
  pointer-events: none;
  transition: none;
}

/* ===== Sidebar ===== */
#sidebar {
  width: var(--sidebar-w);
  background: var(--bg-sidebar);
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  overflow: hidden;
  flex-shrink: 0;
  transition: margin-left var(--transition), opacity var(--transition);
}

#sidebar.collapsed {
  margin-left: calc(var(--sidebar-w) * -1);
  opacity: 0;
  pointer-events: none;
}

.sidebar-header { padding: 12px; display: flex; flex-direction: column; gap: 8px; }

.sidebar-new-btn {
  width: 100%;
  padding: 10px 16px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-1);
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: background var(--transition);
}

.sidebar-new-btn:hover { background: var(--bg-hover); }

.sidebar-search {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-input);
  color: var(--text-1);
  font-size: 13px;
}

.sidebar-search:focus {
  outline: none;
  border-color: var(--accent);
}

.sidebar-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px;
}

.conv-item {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  color: var(--text-2);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  transition: background var(--transition);
  margin-bottom: 2px;
}

.conv-item:hover { background: var(--bg-hover); color: var(--text-1); }
.conv-item.active { background: var(--bg-hover); color: var(--text-1); }

.conv-item-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conv-item-rename {
  opacity: 0;
  background: none;
  border: none;
  color: var(--text-3);
  cursor: pointer;
  padding: 4px;
  border-radius: var(--radius-xs);
  transition: opacity var(--transition), color var(--transition), background var(--transition);
  flex-shrink: 0;
  min-width: 28px;
  min-height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.conv-item:hover .conv-item-rename { opacity: 1; }
.conv-item-rename:hover { color: var(--text-1); background: rgba(255,255,255,0.06); opacity: 1; }

.conv-rename-input {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--accent);
  border-radius: var(--radius-xs);
  color: var(--text-1);
  font-size: 13px;
  padding: 2px 6px;
  outline: none;
  font-family: var(--font);
}

.conv-item-delete {
  opacity: 0;
  background: none;
  border: none;
  color: var(--text-3);
  cursor: pointer;
  padding: 4px 6px;
  border-radius: var(--radius-xs);
  transition: opacity var(--transition), color var(--transition);
  flex-shrink: 0;
  font-size: 16px;
  line-height: 1;
  min-width: 28px;
  min-height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.conv-item:hover .conv-item-delete { opacity: 1; }
.conv-item-delete:hover { color: var(--danger); }

.sidebar-empty {
  padding: 14px 12px;
  color: var(--text-3);
  font-size: 13px;
  text-align: center;
}

.sidebar-footer {
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
}

.sidebar-footer-btn {
  padding: 10px 12px;
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  color: var(--text-2);
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: background var(--transition), color var(--transition);
  white-space: nowrap;
  text-decoration: none;
}

.sidebar-footer-btn:hover { background: var(--bg-hover); color: var(--text-1); }

#github-link { margin-left: auto; }

/* Theme toggle in header */
.theme-icon-dark, .theme-icon-light { display: none; }
[data-theme="dark"] .theme-icon-light { display: block; }
[data-theme="light"] .theme-icon-dark { display: block; }

/* Sidebar backdrop */
.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: var(--bg-backdrop);
  z-index: 79;
}

/* ===== Main ===== */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  position: relative;
}

/* ===== Header ===== */
#header {
  display: flex;
  align-items: center;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  gap: 8px;
  min-height: 52px;
}

.header-btn {
  background: none;
  border: none;
  color: var(--text-2);
  cursor: pointer;
  padding: 8px;
  border-radius: var(--radius-xs);
  transition: background var(--transition), color var(--transition);
  min-width: 40px;
  min-height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.header-btn:hover { background: var(--bg-hover); color: var(--text-1); }

.mode-switch {
  position: relative;
  display: flex;
  align-items: center;
  gap: 0;
  padding: 3px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 999px;
  flex-shrink: 0;
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.04);
}

.mode-switch::before {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: calc(50% - 3px);
  height: calc(100% - 6px);
  border-radius: 999px;
  background: var(--accent);
  box-shadow: 0 6px 16px rgba(0,0,0,0.18);
  transition: transform 180ms ease, background var(--transition);
}

.mode-switch.is-image::before,
.mode-switch:has(#mode-image-btn.active)::before {
  transform: translateX(100%);
}

.mode-btn {
  position: relative;
  z-index: 1;
  border: none;
  background: transparent;
  color: var(--text-3);
  border-radius: 999px;
  padding: 7px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  min-width: 78px;
  transition: color var(--transition), transform var(--transition);
}

.mode-btn:hover {
  color: var(--text-1);
}

.mode-btn.active {
  color: #fff;
}

.mode-btn:active {
  transform: scale(0.98);
}

.header-model { flex: 1; display: flex; justify-content: center; min-width: 0; }

.model-slot {
  min-width: 0;
  display: flex;
  align-items: center;
}

/* Model Dropdown */
.model-dropdown { position: relative; }

.model-dropdown-btn {
  padding: 7px 10px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-1);
  font-size: 13px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: background var(--transition);
  max-width: 100%;
  overflow: hidden;
}

.model-dropdown-btn:hover { background: var(--bg-hover); }
#current-model { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.model-dropdown-list {
  position: absolute;
  top: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-modal);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 0;
  min-width: 260px;
  max-height: 400px;
  overflow-y: auto;
  z-index: 50;
  box-shadow: var(--shadow);
}

.toolbar-right .model-dropdown-list,
.image-params .model-dropdown-list {
  top: auto;
  right: 0;
  bottom: calc(100% + 6px);
  left: auto;
  transform: none;
  z-index: 120;
}

.model-dropdown-header {
  padding: 12px 16px 8px;
  font-size: 12px;
  color: var(--text-3);
  font-weight: 500;
  border-bottom: 1px solid var(--border);
}

.model-dropdown-scroll {
  padding: 4px;
}

.model-option {
  padding: 10px 14px;
  font-size: 14px;
  color: var(--text-2);
  cursor: pointer;
  border-radius: var(--radius-sm);
  transition: background var(--transition), color var(--transition);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: flex;
  align-items: center;
  gap: 8px;
}

.model-option:hover { background: var(--bg-hover); color: var(--text-1); }
.model-option-clear { color: var(--danger); }
.model-option.active {
  color: var(--accent);
  font-weight: 600;
  background: var(--bg-hover);
}

.model-option.active::before {
  content: '';
  display: inline-block;
  width: 6px;
  height: 6px;
  background: var(--accent);
  border-radius: 50%;
}

.header-actions { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }

/* ===== Messages ===== */
#messages {
  flex: 1;
  overflow-y: auto;
  padding: 0;
  min-height: 0;
}

#messages.has-messages { padding: 8px 0; }

.conv-token-summary {
  flex-shrink: 0;
  padding: 4px 24px 0;
  background: var(--bg-main);
}

.conv-token-summary-inner {
  max-width: 768px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  color: var(--text-3);
  font-size: 11px;
  line-height: 1.4;
  white-space: nowrap;
}

.conv-token-summary-inner::before,
.conv-token-summary-inner::after {
  content: '';
  height: 1px;
  flex: 1;
  min-width: 24px;
  background: color-mix(in srgb, var(--border) 55%, transparent);
}

.conv-token-summary-inner span {
  padding: 2px 6px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg-input);
}

.conv-token-summary-inner .conv-token-warn {
  color: #eab308;
  border-color: rgba(234, 179, 8, 0.36);
  background: rgba(234, 179, 8, 0.08);
}

.conv-token-summary-inner .conv-token-over {
  color: var(--danger);
  border-color: rgba(239, 68, 68, 0.38);
  background: rgba(239, 68, 68, 0.08);
}

.conv-token-summary-inner .conv-token-unlimited {
  color: var(--text-2);
}

.chat-msg {
  padding: 12px 24px;
  animation: fadeIn 0.2s ease;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.chat-msg-inner {
  max-width: 768px;
  margin: 0 auto;
  width: 100%;
  display: flex;
  gap: 12px;
}

.chat-msg.user .chat-msg-inner {
  flex-direction: row-reverse;
}

.chat-msg-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.chat-msg.user .chat-msg-avatar { background: var(--accent); color: #fff; }

.ai-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background-image: var(--ai-avatar-url);
  background-size: contain;
  background-position: center;
  background-repeat: no-repeat;
}

.chat-msg-body {
  max-width: 85%;
  min-width: 0;
  font-size: 15px;
  line-height: 1.75;
  word-break: break-word;
  padding: 10px 14px;
  border-radius: var(--radius-md);
}

.chat-msg.user .chat-msg-body {
  background: var(--accent);
  color: #fff;
}

.chat-msg.ai .chat-msg-body {
  background: var(--bg-input);
  border: 1px solid var(--border);
  color: var(--text-1);
}

/* Message meta row (latency + tokens + actions) */
.msg-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 6px;
  font-size: 12px;
  color: var(--text-3);
  flex-wrap: wrap;
  opacity: 0.85;
}

.chat-msg.user .msg-meta {
  justify-content: flex-end;
  color: rgba(255,255,255,0.7);
  opacity: 1;
}

.msg-meta-item {
  white-space: nowrap;
}

.msg-model-tag {
  color: var(--accent);
  font-weight: 500;
  opacity: 0.85;
}

.chat-msg.user .msg-model-tag {
  color: rgba(255,255,255,0.8);
}

/* Thinking block (collapsible reasoning section) */
.thinking-block {
  margin-bottom: 8px;
}

.thinking-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  background: var(--bg-hover);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-3);
  font-size: 13px;
  cursor: pointer;
  width: 100%;
  transition: background var(--transition), color var(--transition);
}

.thinking-toggle:hover { background: var(--bg-input); color: var(--text-2); }

.thinking-chevron { transition: transform var(--transition); flex-shrink: 0; }
.thinking-block.expanded .thinking-chevron { transform: rotate(180deg); }

.thinking-content {
  display: none;
  margin-top: 6px;
  padding: 10px 12px;
  background: var(--bg-code);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 13px;
  color: var(--text-3);
  max-height: 400px;
  overflow-y: auto;
}

.thinking-block.expanded .thinking-content { display: block; }

.msg-action-btn {
  background: transparent;
  border: none;
  color: var(--text-3);
  cursor: pointer;
  padding: 2px 4px;
  border-radius: var(--radius-xs);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition), color var(--transition);
  min-width: 24px;
  min-height: 24px;
  opacity: 0.7;
}

.msg-action-btn:hover { opacity: 1; color: var(--text-1); }

.msg-action-btn[data-tooltip],
.toolbar-btn[data-tooltip],
.send-btn[data-tooltip],
.model-dropdown-btn[data-tooltip] {
  position: relative;
}

.msg-action-btn[data-tooltip]::after,
.toolbar-btn[data-tooltip]::after,
.send-btn[data-tooltip]::after,
.model-dropdown-btn[data-tooltip]::after {
  content: attr(data-tooltip);
  position: absolute;
  right: 50%;
  bottom: calc(100% + 8px);
  transform: translateX(50%);
  display: none;
  width: max-content;
  max-width: 180px;
  padding: 5px 8px;
  background: var(--bg-modal);
  color: var(--text-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-xs);
  box-shadow: var(--shadow);
  font-size: 12px;
  line-height: 1.35;
  white-space: normal;
  text-align: center;
  pointer-events: none;
  z-index: 30;
}

.msg-action-btn[data-tooltip]:hover::after,
.toolbar-btn[data-tooltip]:hover::after,
.send-btn[data-tooltip]:hover::after,
.model-dropdown-btn[data-tooltip]:hover::after {
  display: block;
}

.chat-msg.user .msg-action-btn {
  color: rgba(255,255,255,0.7);
  opacity: 1;
}

.chat-msg.user .msg-action-btn:hover { background: rgba(255,255,255,0.2); color: #fff; }

.copy-menu {
  position: relative;
  display: inline-flex;
}

.copy-menu-popover {
  position: absolute;
  right: 0;
  bottom: calc(100% + 6px);
  display: none;
  min-width: 126px;
  padding: 4px;
  background: var(--bg-modal);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  z-index: 20;
}

.copy-menu.open .copy-menu-popover { display: grid; gap: 2px; }

.copy-menu-popover button {
  width: 100%;
  border: 0;
  background: transparent;
  color: var(--text-2);
  padding: 7px 9px;
  border-radius: var(--radius-xs);
  text-align: left;
  font: inherit;
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
}

.copy-menu-popover button:hover {
  background: var(--bg-hover);
  color: var(--text-1);
}

.chat-msg.user .copy-menu-popover {
  color: var(--text-2);
}

/* Toast */
.toast {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: var(--bg-modal);
  color: var(--text-1);
  padding: 10px 20px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  font-size: 14px;
  z-index: 300;
  animation: toastIn 0.2s ease, fadeOut 0.3s ease 1.5s forwards;
}

@keyframes fadeOut { from { opacity: 1; transform: translate(-50%, -50%) scale(1); } to { opacity: 0; transform: translate(-50%, -50%) scale(1); } }

/* Markdown content */
.msg-md h1 { font-size: 22px; font-weight: 700; margin: 20px 0 10px; }
.msg-md h2 { font-size: 18px; font-weight: 700; margin: 16px 0 8px; }
.msg-md h3 { font-size: 16px; font-weight: 600; margin: 14px 0 6px; }
.msg-md h4 { font-size: 15px; font-weight: 600; margin: 12px 0 4px; }
.msg-md h5 { font-size: 14px; font-weight: 600; margin: 10px 0 3px; }
.msg-md h6 { font-size: 13px; font-weight: 600; margin: 8px 0 2px; color: var(--text-3); }

.msg-md p { margin-bottom: 10px; line-height: 1.75; }
.msg-md p:last-child { margin-bottom: 0; }

/* Code blocks with language label */
.msg-md .code-block {
  position: relative;
  margin: 12px 0;
  border-radius: var(--radius-sm);
  background: var(--bg-code);
  border: 1px solid var(--border);
}

.code-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-hover);
}

.msg-md .code-lang {
  font-size: 11px;
  color: var(--text-3);
  font-family: var(--font);
  text-transform: lowercase;
}

.code-copy-btn {
  background: none;
  border: none;
  color: var(--text-3);
  cursor: pointer;
  padding: 2px 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color var(--transition);
  opacity: 0;
}

.msg-md .code-block:hover .code-copy-btn { opacity: 1; }
.code-copy-btn:hover { color: var(--text-1); }
.code-copy-btn.copied { color: var(--accent); opacity: 1; }

.msg-md .code-block pre {
  background: var(--bg-code);
  padding: 14px 16px;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.5;
  margin: 0;
  border: none;
  border-radius: 0 0 var(--radius-sm) var(--radius-sm);
}

/* Syntax highlighting */
.hl-keyword { color: #c678dd; font-weight: 500; }
.hl-string { color: #98c379; }
.hl-number { color: #d19a66; }
.hl-comment { color: #5c6370; font-style: italic; }
.hl-func { color: #61afef; }

[data-theme="light"] .hl-keyword { color: #a626a4; }
[data-theme="light"] .hl-string { color: #50a14f; }
[data-theme="light"] .hl-number { color: #986801; }
[data-theme="light"] .hl-comment { color: #a0a1a7; }
[data-theme="light"] .hl-func { color: #4078f2; }

/* Inline code */
.msg-md code { font-family: var(--font-mono); font-size: 13px; }

.msg-md code:not(pre code):not(.code-block code) {
  background: var(--bg-input);
  padding: 2px 6px;
  border-radius: var(--radius-xs);
  color: var(--accent);
}

/* Lists */
.msg-md ul, .msg-md ol { margin: 8px 0; padding-left: 24px; }
.msg-md li { margin-bottom: 4px; line-height: 1.65; }
.msg-md ul li::marker { color: var(--text-3); }
.msg-md ol li::marker { color: var(--text-3); }

/* Nested lists */
.msg-md ul ul, .msg-md ol ol, .msg-md ul ol, .msg-md ol ul { margin: 4px 0; }
.msg-md .list-depth-2 { padding-left: 20px; }
.msg-md .list-depth-4 { padding-left: 40px; }

/* Task lists */
.msg-md .task-item {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin: 4px 0;
  padding-left: 4px;
}
.msg-md .task-box {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border: 2px solid var(--border);
  border-radius: 4px;
  font-size: 11px;
  flex-shrink: 0;
  margin-top: 2px;
  color: var(--accent);
}
.msg-md .task-checked .task-box {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.msg-md .task-checked {
  color: var(--text-3);
  text-decoration: line-through;
}

/* Blockquotes */
.msg-md blockquote {
  margin: 10px 0;
  padding: 10px 16px;
  border-left: 3px solid var(--accent);
  background: var(--bg-code);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  color: var(--text-2);
  font-style: italic;
}

.msg-md blockquote p { margin-bottom: 4px; }

/* Tables */
.msg-md table {
  width: 100%;
  border-collapse: collapse;
  margin: 12px 0;
  font-size: 13px;
  overflow-x: auto;
  display: block;
}

.msg-md th, .msg-md td {
  border: 1px solid var(--border);
  padding: 8px 12px;
  text-align: left;
}

.msg-md th {
  background: var(--bg-hover);
  font-weight: 600;
  color: var(--text-1);
}

.msg-md td { color: var(--text-2); }

/* Horizontal rule */
.msg-md hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 16px 0;
}

/* Strong & emphasis & strikethrough */
.msg-md strong { font-weight: 600; }
.msg-md em { font-style: italic; }
.msg-md del { text-decoration: line-through; color: var(--text-3); }

/* Links */
.msg-md a { color: var(--accent); text-decoration: none; }
.msg-md a:hover { text-decoration: underline; }

/* Images */
.msg-md img {
  max-width: 100%;
  border-radius: var(--radius-sm);
  margin: 8px 0;
}

/* Typing indicator */
.typing-dots { display: inline-flex; gap: 4px; padding: 4px 0; }
.typing-dots span { width: 8px; height: 8px; background: var(--text-3); border-radius: 50%; animation: bounce 1.4s infinite; }
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }

/* ===== Welcome ===== */
.welcome { flex: 1; display: flex; align-items: center; justify-content: center; }

.welcome-inner { text-align: center; max-width: 560px; padding: 40px 24px; }
.welcome-inner h1 { font-size: 28px; font-weight: 700; margin-bottom: 8px; }
.welcome-inner p { color: var(--text-3); font-size: 15px; margin-bottom: 32px; }

.welcome-tips { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.tip-card {
  padding: 14px 16px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  font-size: 13px;
  color: var(--text-2);
  cursor: pointer;
  transition: background var(--transition), color var(--transition), border-color var(--transition);
}
.tip-card:hover { background: var(--bg-hover); color: var(--text-1); border-color: var(--accent); }

/* ===== Image Mode ===== */
.image-workspace {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 12px 24px;
}

.image-empty {
  height: 100%;
  min-height: 320px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--text-3);
}

.image-empty h1 {
  color: var(--text-1);
  font-size: 28px;
  margin-bottom: 8px;
}

.image-empty p {
  max-width: 520px;
  font-size: 15px;
}

.image-gallery {
  max-width: 768px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.image-job-card {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.image-chat-msg {
  display: flex;
  width: 100%;
}

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

.image-chat-msg.ai {
  justify-content: flex-start;
}

.image-chat-inner {
  width: 100%;
  display: flex;
  gap: 12px;
}

.image-chat-msg.user .image-chat-inner {
  flex-direction: row-reverse;
}

.image-chat-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.image-chat-msg.user .image-chat-avatar {
  color: #fff;
  background: var(--accent);
}

.image-ai-avatar {
  background-image: var(--ai-avatar-url);
  background-size: contain;
  background-position: center;
  background-repeat: no-repeat;
}

.image-chat-bubble {
  max-width: 85%;
  min-width: 0;
  border-radius: var(--radius-md);
  padding: 10px 14px;
  font-size: 14px;
  line-height: 1.65;
  word-break: break-word;
}

.image-chat-msg.user .image-chat-bubble {
  color: #fff;
  background: var(--accent);
}

.image-chat-msg.ai .image-chat-bubble {
  color: var(--text-1);
  background: var(--bg-input);
  border: 1px solid var(--border);
}

.image-chat-prompt {
  white-space: pre-wrap;
}

.image-msg-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  font-size: 12px;
  color: var(--text-3);
  flex-wrap: wrap;
  opacity: 0.85;
}

.image-chat-msg.user .image-msg-meta {
  justify-content: flex-end;
  color: rgba(255,255,255,0.72);
  opacity: 1;
}

.image-chat-msg.user .msg-action-btn {
  color: rgba(255,255,255,0.72);
}

.image-chat-msg.user .msg-action-btn:hover {
  color: #fff;
  background: rgba(255,255,255,0.2);
}

.image-msg-meta span {
  white-space: nowrap;
}

.image-job-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 10px;
  flex-shrink: 0;
}

.image-job-actions .btn-secondary {
  padding: 6px 10px;
  font-size: 13px;
  min-height: 34px;
}

.image-input-ref {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  padding: 6px 8px;
  background: rgba(0,0,0,0.12);
  border: 1px solid rgba(255,255,255,0.18);
  border-radius: var(--radius-sm);
  color: currentColor;
  font-size: 12px;
}

.image-input-ref img {
  width: 34px;
  height: 34px;
  object-fit: cover;
  border-radius: var(--radius-xs);
}

.image-input-preview {
  cursor: zoom-in;
}

.image-error {
  margin-bottom: 12px;
  padding: 10px 12px;
  border: 1px solid rgba(239, 68, 68, 0.35);
  border-radius: var(--radius-sm);
  color: var(--danger);
  background: rgba(239, 68, 68, 0.08);
  font-size: 13px;
}

.image-progress {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  width: min(520px, 100%);
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--bg-code);
  background: color-mix(in srgb, var(--bg-code) 86%, var(--accent) 14%);
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.04);
}

.image-progress-indicator {
  width: 34px;
  height: 34px;
  flex-shrink: 0;
  border-radius: 50%;
  background: var(--bg-input);
  display: flex;
  align-items: center;
  justify-content: center;
}

.image-progress-body {
  min-width: 0;
  flex: 1;
  padding-top: 1px;
}

.image-spinner {
  width: 18px;
  height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

.image-progress-title {
  font-size: 14px;
  color: var(--text-1);
  font-weight: 600;
  line-height: 1.35;
}

.image-progress-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 5px;
}

.image-progress-stats span {
  padding: 2px 7px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg-input);
  color: var(--text-2);
  font-size: 11px;
  line-height: 1.35;
  white-space: nowrap;
}

.image-progress-note {
  margin-top: 7px;
  font-size: 12px;
  color: var(--text-3);
  line-height: 1.45;
}

.image-cancel-btn {
  flex-shrink: 0;
  min-height: 32px;
  padding: 6px 12px;
  font-size: 12px;
}

.image-results {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px;
}

.image-result {
  position: relative;
  background: var(--bg-code);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  min-height: 220px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.image-result img {
  width: 100%;
  height: auto;
  max-height: 640px;
  object-fit: contain;
  display: block;
  cursor: zoom-in;
  flex: 1 1 auto;
  min-height: 0;
}

.image-result-meta {
  width: 100%;
  min-height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text-3);
  font-size: 12px;
  line-height: 1.3;
  white-space: nowrap;
}

.image-result-meta span {
  color: var(--text-3);
  opacity: 0.65;
}

.image-result-actions {
  position: absolute;
  right: 8px;
  bottom: 42px;
  display: flex;
  gap: 6px;
  background: var(--bg-modal);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 4px;
  opacity: 0;
  transition: opacity var(--transition);
  z-index: 5;
}

.image-result:hover .image-result-actions { opacity: 1; }

.image-viewer {
  position: fixed;
  inset: 0;
  z-index: 260;
  display: flex;
  align-items: center;
  justify-content: center;
}

.image-viewer-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.78);
}

.image-viewer-content {
  position: relative;
  width: 94vw;
  height: 92dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  z-index: 1;
  overflow: hidden;
}

.image-viewer-content img {
  max-width: 100%;
  max-height: calc(92dvh - 70px);
  object-fit: contain;
  border-radius: var(--radius-sm);
  background: var(--bg-code);
  box-shadow: var(--shadow);
  cursor: grab;
  transform-origin: center center;
  transition: transform 120ms ease-out;
  user-select: none;
  -webkit-user-drag: none;
  touch-action: none;
  will-change: transform;
}

.image-viewer-content img.is-zoomed,
.image-viewer.is-panning .image-viewer-content img {
  cursor: grabbing;
}

.image-viewer.is-panning .image-viewer-content img {
  transition: none;
}

.image-viewer-close {
  position: absolute;
  top: 8px;
  right: 8px;
  background: var(--bg-modal);
  border: 1px solid var(--border);
  z-index: 2;
}

.image-viewer-toolbar {
  display: flex;
  gap: 10px;
  justify-content: center;
  z-index: 2;
}

#image-input-area {
  padding: 8px 24px 8px;
  flex-shrink: 0;
  background: var(--bg-main);
}

.image-input-wrapper {
  max-width: 768px;
  margin: 0 auto;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 0;
  overflow: visible;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.image-input-wrapper:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}

#image-prompt {
  width: 100%;
  min-height: 78px;
  max-height: 180px;
  resize: vertical;
  background: transparent;
  border: none;
  color: var(--text-1);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.6;
  display: block;
  padding: 14px 16px 6px;
}

#image-prompt:focus { outline: none; }
#image-prompt::placeholder { color: var(--text-3); }

.image-params {
  padding: 4px 6px 6px;
  flex-wrap: wrap;
}

.image-ref-preview {
  padding: 4px 16px 0;
  margin-bottom: 0;
}

.image-ref-card {
  position: relative;
  display: flex;
  align-items: center;
  gap: 10px;
  width: fit-content;
  max-width: 100%;
  padding: 6px 30px 6px 6px;
  background: var(--bg-hover);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.image-ref-card img {
  width: 52px;
  height: 52px;
  object-fit: cover;
  border-radius: var(--radius-xs);
}

.image-ref-name {
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
  color: var(--text-1);
}

.image-ref-hint {
  font-size: 12px;
  color: var(--text-3);
}

.image-ref-remove {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 22px;
  height: 22px;
  border: none;
  border-radius: var(--radius-xs);
  background: transparent;
  color: var(--text-3);
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
}

.image-ref-remove:hover {
  background: var(--danger);
  color: #fff;
}

.image-ref-btn {
  flex-shrink: 0;
}

.image-toolbar-left {
  flex: 1 1 auto;
  min-width: 0;
  gap: 4px;
}

.image-toolbar-right {
  flex: 0 0 auto;
}

.image-params .model-dropdown-btn {
  max-width: 190px;
  justify-content: space-between;
}

.image-settings-panel {
  padding: 10px 12px 12px;
}

.image-settings-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.image-param-field {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 6px;
  min-height: 64px;
  padding: 9px 10px 10px;
  background: var(--bg-hover);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  transition: border-color var(--transition), background var(--transition);
}

.image-param-field span {
  color: var(--text-3);
  font-size: 12px;
  font-weight: 500;
  line-height: 1;
}

.image-param-field select {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid transparent;
  border-radius: var(--radius-xs);
  color: var(--text-1);
  font-size: 12px;
  padding: 5px 6px;
  min-height: 30px;
  cursor: pointer;
}

.image-param-field:hover {
  border-color: var(--accent);
}

.image-param-field select:focus {
  outline: none;
  border-color: var(--accent);
  background: var(--bg-main);
}

.image-generate-btn {
  flex-shrink: 0;
}

/* ===== Input Area ===== */
#input-area {
  padding: 8px 24px 8px;
  flex-shrink: 0;
  background: var(--bg-main);
}

.input-disclaimer {
  max-width: 768px;
  margin: 6px auto 0;
  text-align: center;
  font-size: 11px;
  color: var(--text-3);
  line-height: 1.4;
}

.input-wrapper {
  max-width: 768px;
  margin: 0 auto;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  transition: border-color var(--transition), box-shadow var(--transition);
  overflow: visible;
}

.input-wrapper:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}

#user-input {
  background: transparent;
  border: none;
  color: var(--text-1);
  font-size: 16px;
  line-height: 1.6;
  resize: none;
  max-height: 200px;
  font-family: var(--font);
  padding: 14px 16px 6px;
  width: 100%;
  display: block;
}

#user-input:focus { outline: none; }
#user-input::placeholder { color: var(--text-3); }

.input-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 6px 6px;
  gap: 8px;
}

.toolbar-left {
  display: flex;
  align-items: center;
  gap: 4px;
}

.toolbar-right {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.toolbar-right .model-dropdown-btn {
  max-width: 220px;
}

.toolbar-btn {
  background: none;
  border: none;
  color: var(--text-3);
  cursor: pointer;
  padding: 6px 8px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color var(--transition), background var(--transition);
  min-width: 32px;
  min-height: 32px;
}

.toolbar-btn:hover { color: var(--text-1); background: var(--bg-hover); }
.toolbar-btn.active { color: var(--accent); }
.toolbar-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

#image-optimize-btn.active svg {
  animation: toolbar-spin 1.1s linear infinite;
}

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

.send-btn {
  width: 32px;
  height: 32px;
  background: var(--accent);
  border: none;
  border-radius: 50%;
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background var(--transition), opacity var(--transition), transform var(--transition);
}
.send-btn:hover { background: var(--accent-hover); transform: scale(1.05); }
.send-btn:disabled { opacity: 0.35; cursor: not-allowed; background: var(--accent); transform: none; }
.send-btn.is-stopping svg { display: none; }
.send-btn.is-stopping::before {
  content: "";
  width: 11px;
  height: 11px;
  border-radius: 2px;
  background: currentColor;
}
.send-btn.is-stopping {
  opacity: 1;
  cursor: pointer;
}

/* File preview inside input wrapper */
.file-preview {
  display: flex;
  gap: 8px;
  padding: 4px 16px 0;
  overflow-x: auto;
}

.file-preview-item {
  position: relative;
  background: var(--bg-hover);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  min-width: 72px;
  max-width: 96px;
}

.file-thumb {
  width: 48px;
  height: 48px;
  object-fit: cover;
  border-radius: var(--radius-xs);
}

.file-icon {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-3);
}

.file-name {
  font-size: 11px;
  color: var(--text-2);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

.file-remove {
  position: absolute;
  top: 2px;
  right: 2px;
  background: var(--bg-hover);
  border: none;
  color: var(--text-3);
  font-size: 14px;
  cursor: pointer;
  width: 18px;
  height: 18px;
  border-radius: var(--radius-xs);
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.file-remove:hover { background: var(--danger); color: #fff; }

/* Conversation settings panel (inside input wrapper) */
.conv-settings-panel {
  padding: 12px 16px 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  animation: fadeIn 0.15s ease;
}

.conv-settings-row {
  display: flex;
  align-items: center;
  gap: 8px;
  position: relative;
}

.conv-settings-row label {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-2);
  min-width: 90px;
  flex-shrink: 0;
  line-height: 1.4;
}

.conv-settings-row input,
.conv-settings-row textarea {
  flex: 1;
  padding: 4px 8px;
  background: var(--bg-hover);
  border: 1px solid var(--border);
  border-radius: var(--radius-xs);
  color: var(--text-1);
  font-size: 13px;
  font-family: var(--font-mono);
}

.conv-settings-row input:focus,
.conv-settings-row textarea:focus { outline: none; border-color: var(--accent); }

.conv-settings-row input[type="text"],
.conv-settings-row textarea {
  font-family: var(--font);
}

.conv-role-row {
  align-items: flex-start;
}

.conv-settings-row textarea {
  min-height: 72px;
  max-height: 180px;
  resize: vertical;
  line-height: 1.5;
}

.param-hint {
  font-size: 11px;
  color: var(--text-3);
  flex-shrink: 0;
  white-space: nowrap;
}

/* ===== Modal ===== */
.modal { position: fixed; inset: 0; z-index: 100; display: flex; align-items: center; justify-content: center; }
.modal-backdrop { position: absolute; inset: 0; background: var(--bg-backdrop); animation: fadeIn 0.2s ease; }

.modal-content {
  position: relative;
  background: var(--bg-modal);
  border: 1px solid var(--border);
  border-radius: 14px;
  width: min(520px, 92vw);
  max-height: min(88dvh, 760px);
  box-shadow: 0 18px 60px rgba(0,0,0,0.32);
  animation: scaleIn 0.2s ease;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.modal-header { display: flex; align-items: center; gap: 10px; padding: 14px 16px 12px 18px; border-bottom: 1px solid var(--border); background: var(--bg-modal); flex-shrink: 0; }
.modal-header h2 { font-size: 16px; font-weight: 600; flex-shrink: 0; }

.modal-header .modal-close-btn { margin-left: auto; }
.modal-close-btn { background: none; border: none; color: var(--text-3); cursor: pointer; padding: 4px; border-radius: var(--radius-xs); transition: color var(--transition); min-width: 36px; min-height: 36px; display: flex; align-items: center; justify-content: center; }
.modal-close-btn:hover { color: var(--text-1); }

.modal-body { padding: 18px 20px 20px; flex: 1; overflow-y: auto; min-height: 0; background: var(--bg-main); }
.modal-footer { padding: 14px 18px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 10px; align-items: center; flex-shrink: 0; background: var(--bg-modal); }

.settings-tabs {
  display: flex;
  gap: 4px;
  padding: 12px 18px;
  background: var(--bg-modal);
  border-bottom: 1px solid var(--border);
}

.settings-tab {
  flex: 1;
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text-3);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  cursor: pointer;
  font-size: 14px;
}

.settings-tab.active {
  color: var(--text-1);
  background: var(--bg-hover);
  border-color: var(--accent);
}

/* ===== Form Elements ===== */
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 13px; font-weight: 500; color: var(--text-2); margin-bottom: 6px; }

.form-group input,
.form-group select {
  width: 100%;
  padding: 10px 14px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-1);
  font-size: 16px; /* prevents iOS auto-zoom */
  transition: border-color var(--transition);
}

.form-group input:focus,
.form-group select:focus { outline: none; border-color: var(--accent); }

.field-hint { font-size: 12px; color: var(--text-3); margin-top: 4px; }
.model-field { display: flex; gap: 8px; }
.model-field select { flex: 1; }
.model-manual-input { margin-top: 8px; }

.settings-panel {
  background: var(--bg-modal);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px;
}

.settings-panel .form-group:last-child {
  margin-bottom: 0;
}

.settings-config-tools {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 0;
  padding: 11px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-modal);
}

.settings-config-info {
  min-width: 0;
}

.settings-config-title {
  color: var(--text-2);
  font-size: 13px;
  font-weight: 600;
  line-height: 1.3;
}

.settings-config-desc {
  color: var(--text-3);
  font-size: 12px;
  line-height: 1.4;
  margin-top: 2px;
}

.tool-chip {
  min-height: 30px;
  padding: 6px 9px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-input);
  color: var(--text-2);
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
  transition: border-color var(--transition), color var(--transition), background var(--transition);
}

.tool-chip:hover {
  color: var(--text-1);
  border-color: var(--accent);
  background: var(--bg-hover);
}

.tool-chip svg {
  flex-shrink: 0;
}

.danger-chip {
  color: var(--danger);
  border-color: rgba(239, 68, 68, 0.35);
}

.image-history-tools {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px 16px;
  margin-top: 4px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-input);
}

.image-history-info {
  min-width: 0;
}

.image-history-title {
  color: var(--text-2);
  font-size: 13px;
  font-weight: 500;
  line-height: 1.3;
}

.image-history-summary {
  color: var(--text-3);
  font-size: 12px;
  line-height: 1.4;
  margin-top: 4px;
  overflow-wrap: anywhere;
}

.tool-chip-group {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 7px;
  flex-shrink: 0;
}

.config-preview {
  display: grid;
  gap: 8px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-input);
  font-size: 13px;
}

.config-preview-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: var(--text-2);
}

.config-preview-row strong {
  color: var(--text-1);
  font-weight: 600;
}

/* Message images */
.msg-images {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.msg-img {
  max-width: 200px;
  max-height: 200px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  object-fit: contain;
}

.btn-refresh {
  padding: 10px 12px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-2);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background var(--transition), color var(--transition);
  min-width: 44px;
  min-height: 44px;
}
.btn-refresh:hover { background: var(--bg-hover); color: var(--text-1); }
.btn-refresh:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-primary { padding: 10px 20px; background: var(--accent); color: #fff; border: none; border-radius: var(--radius-sm); font-size: 16px; font-weight: 500; cursor: pointer; transition: background var(--transition); min-height: 44px; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-secondary { padding: 10px 20px; background: var(--bg-input); color: var(--text-1); border: 1px solid var(--border); border-radius: var(--radius-sm); font-size: 16px; cursor: pointer; transition: background var(--transition); min-height: 44px; }
.btn-secondary:hover { background: var(--bg-hover); }

.btn-block { width: 100%; padding: 14px; font-size: 16px; }

/* ===== Setup Overlay ===== */
#setup-overlay { position: fixed; inset: 0; background: var(--bg-main); display: flex; align-items: center; justify-content: center; z-index: 200; overflow-y: auto; }

.setup-card { width: 400px; max-width: 90vw; text-align: center; padding: 24px 16px; }
.setup-logo { margin-bottom: 16px; }
.setup-card h1 { font-size: 32px; font-weight: 700; margin-bottom: 4px; }
.setup-desc { color: var(--text-3); font-size: 14px; margin-bottom: 32px; }
.setup-card .form-group { text-align: left; }
.setup-later-btn { margin-top: 10px; }

/* ===== Animations ===== */
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, -50%) scale(0.9); } to { opacity: 1; transform: translate(-50%, -50%) scale(1); } }
@keyframes scaleIn { from { opacity: 0; transform: scale(0.96); } to { opacity: 1; transform: scale(1); } }
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
@keyframes bounce { 0%, 80%, 100% { transform: translateY(0); } 40% { transform: translateY(-6px); } }
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-3); }

/* ===== Mobile Responsive ===== */
@media (max-width: 768px) {
  #sidebar {
    position: fixed;
    z-index: 80;
    height: 100vh;
    height: 100dvh;
    top: 0;
    left: 0;
    width: 280px;
    max-width: 85vw;
    transition: transform 0.3s ease, opacity 0.3s ease;
    /* shadow for overlay feel */
    box-shadow: 4px 0 16px rgba(0,0,0,0.3);
  }

  #sidebar.collapsed {
    transform: translateX(-100%);
    opacity: 0;
    pointer-events: none;
    margin-left: 0; /* override desktop margin-left approach */
  }

  html[data-boot-sidebar="collapsed"] #sidebar {
    transform: translateX(-100%);
    margin-left: 0;
  }

  .sidebar-backdrop { z-index: 79; }

  #header {
    padding: 8px 12px;
    min-height: 48px;
  }

  .header-btn {
    min-width: 36px;
    min-height: 36px;
    padding: 6px;
  }

  .toolbar-right .model-dropdown-btn {
    max-width: 160px;
    padding: 5px 8px;
    font-size: 12px;
  }

  .mode-switch {
    padding: 2px;
  }

  .mode-switch::before {
    top: 2px;
    left: 2px;
    width: calc(50% - 2px);
    height: calc(100% - 4px);
  }

  .mode-btn {
    min-width: 68px;
    padding: 6px 9px;
    font-size: 12px;
  }

  #current-model { max-width: 120px; }

  .model-dropdown-list {
    position: fixed;
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    transform: none;
    max-height: 60vh;
    min-width: unset;
    width: 100%;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
    z-index: 240;
    animation: slideUp 0.3s ease;
    border: none;
    box-shadow: 0 -4px 24px rgba(0,0,0,0.3);
  }

  .toolbar-right .model-dropdown-list,
  .image-params .model-dropdown-list {
    position: fixed;
    top: auto;
    right: 0;
    bottom: 0;
    left: 0;
    transform: none;
    max-height: 60vh;
    width: 100%;
    z-index: 240;
  }

  .model-dropdown-list::before {
    content: '';
    display: block;
    width: 36px;
    height: 4px;
    background: var(--border);
    border-radius: 2px;
    margin: 8px auto 4px;
  }

  .model-dropdown-header {
    padding: 4px 16px 8px;
    font-size: 13px;
  }

  .model-option {
    padding: 12px 16px;
    font-size: 14px;
  }

  .chat-msg { padding: 10px 12px; }
  .chat-msg-inner { gap: 8px; }
  .chat-msg-avatar { width: 28px; height: 28px; }
  .ai-avatar { width: 28px; height: 28px; }
  .chat-msg-body { font-size: 14px; max-width: 88%; padding: 8px 12px; }
  .msg-meta { font-size: 11px; gap: 6px; }
  .msg-action-btn { min-width: 28px; min-height: 28px; }
  .copy-menu-popover { min-width: 132px; }
  .image-chat-inner { gap: 8px; }
  .image-chat-avatar { width: 28px; height: 28px; }
  .image-msg-meta { font-size: 11px; gap: 6px; }

  .conv-token-summary {
    padding: 4px 12px 0;
  }

  .conv-token-summary-inner {
    justify-content: center;
    gap: 5px;
    overflow-x: auto;
    scrollbar-width: none;
    font-size: 10px;
  }

  .conv-token-summary-inner::-webkit-scrollbar {
    display: none;
  }

  .conv-token-summary-inner span {
    padding: 2px 6px;
  }

  .thinking-toggle { font-size: 12px; padding: 5px 8px; }
  .thinking-content { font-size: 12px; padding: 8px 10px; max-height: 280px; }

  .msg-md .task-box { width: 16px; height: 16px; font-size: 10px; }
  .msg-md .task-item { font-size: 13px; }

  .msg-md .code-block pre { padding: 10px 12px; font-size: 12px; }
  .msg-md .code-lang { font-size: 10px; }
  .code-copy-btn { opacity: 1; padding: 2px; }
  .code-header { padding: 4px 8px; }
  .msg-md h1 { font-size: 18px; }
  .msg-md h2 { font-size: 16px; }
  .msg-md h3 { font-size: 15px; }
  .msg-md table { font-size: 12px; }
  .msg-md th, .msg-md td { padding: 6px 8px; }
  .msg-md blockquote { padding: 8px 12px; }
  .msg-md code:not(pre code):not(.code-block code) { font-size: 12px; padding: 1px 4px; }

  .conv-item-rename,
  .conv-item-delete {
    opacity: 0.72;
  }

  #input-area {
    padding: 6px 12px;
    padding-bottom: max(env(safe-area-inset-bottom), 6px);
  }

  #user-input { font-size: 16px; max-height: 100px; padding: 12px 12px 4px; }
  .input-toolbar { padding: 2px 4px 6px; gap: 6px; }
  .toolbar-left { flex-shrink: 0; }
  .toolbar-right { gap: 4px; }
  .toolbar-btn { min-width: 28px; min-height: 28px; padding: 4px 6px; }
  .send-btn { width: 28px; height: 28px; }

  .conv-settings-row {
    align-items: flex-start;
  }
  .conv-settings-row label { min-width: 70px; font-size: 12px; padding-top: 4px; }
  .conv-settings-row input,
  .conv-settings-row textarea { font-size: 12px; }
  .param-hint { font-size: 10px; }

  .file-preview-item { min-width: 64px; max-width: 80px; padding: 4px; }
  .file-thumb { width: 40px; height: 40px; }
  .file-name { font-size: 10px; }

  .msg-img { max-width: 140px; max-height: 140px; }

  .welcome-inner { padding: 24px 16px; }
  .welcome-inner h1 { font-size: 24px; }
  .welcome-inner p { font-size: 14px; margin-bottom: 24px; }
  .welcome-tips { gap: 8px; }
  .tip-card { padding: 12px 14px; font-size: 12px; }

  .image-workspace {
    padding: 12px;
  }

  .image-empty h1 {
    font-size: 24px;
  }

  .image-chat-bubble {
    max-width: 88%;
    padding: 8px 12px;
  }

  .image-job-actions {
    width: 100%;
  }

  .image-job-actions .btn-secondary {
    flex: 1;
  }

  .image-results {
    grid-template-columns: 1fr;
  }

  .image-result {
    min-height: 180px;
  }

  .image-result-actions {
    opacity: 1;
  }

  .image-progress {
    display: grid;
    grid-template-columns: 30px minmax(0, 1fr);
    gap: 10px;
    width: 100%;
    padding: 10px;
  }

  .image-progress-indicator {
    width: 30px;
    height: 30px;
  }

  .image-spinner {
    width: 16px;
    height: 16px;
  }

  .image-progress-body {
    padding-top: 0;
  }

  .image-progress-title {
    font-size: 13px;
  }

  .image-progress-stats {
    gap: 5px;
  }

  .image-progress-stats span {
    font-size: 10px;
    padding: 2px 6px;
  }

  .image-progress-note {
    font-size: 11px;
    line-height: 1.4;
  }

  .image-cancel-btn {
    grid-column: 2;
    justify-self: end;
    min-width: 56px;
    min-height: 30px;
    padding: 5px 10px;
  }

  #image-input-area {
    padding: 6px 12px;
    padding-bottom: max(env(safe-area-inset-bottom), 6px);
  }

  .image-input-wrapper {
    max-width: 100%;
  }

  #image-prompt {
    min-height: 70px;
    font-size: 16px;
  }

  .image-params {
    align-items: center;
    gap: 8px;
    flex-direction: row;
    flex-wrap: nowrap;
  }

  .image-toolbar-left {
    width: auto;
    order: 0;
    flex: 1 1 auto;
    display: flex;
    gap: 4px;
  }

  .image-toolbar-right {
    width: auto;
    justify-content: flex-end;
    order: 0;
    flex: 0 0 auto;
    min-width: 0;
  }

  .image-settings-panel {
    padding: 8px 10px 10px;
  }

  .image-settings-grid {
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .image-param-field {
    min-width: 0;
    min-height: 60px;
    padding: 8px;
  }

  .image-param-field select {
    min-width: 0;
    max-width: none;
  }

  .image-params .model-dropdown-btn {
    max-width: min(220px, calc(100vw - 96px));
  }

  .setup-card { padding: 24px 16px; }
  .setup-card h1 { font-size: 24px; }

  .modal-content {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    top: auto;
    width: 100%;
    max-width: 100vw;
    max-height: 88dvh;
    border-radius: 16px 16px 0 0;
    border: none;
    box-shadow: 0 -10px 34px rgba(0,0,0,0.32);
    animation: slideUp 0.3s ease;
  }

  .modal-content::before {
    content: '';
    display: block;
    width: 36px;
    height: 4px;
    background: var(--border);
    border-radius: 2px;
    margin: 8px auto 0;
  }

  .modal-header {
    padding: 10px 14px 12px;
    flex-wrap: wrap;
    gap: 8px;
  }

  .modal-header h2 {
    order: 1;
  }

  .modal-header .modal-close-btn {
    order: 2;
    margin-left: auto;
  }

  .modal-body {
    padding: 14px;
    overflow-y: auto;
  }

  .modal-footer {
    padding: 10px 14px;
    padding-bottom: max(env(safe-area-inset-bottom), 12px);
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .settings-tabs {
    padding: 10px 14px;
  }

  .modal-footer .btn-primary,
  .modal-footer .btn-secondary {
    width: 100%;
  }

  .sidebar-footer-btn {
    min-height: 44px;
  }

  .sidebar-search {
    min-height: 40px;
    font-size: 16px;
  }

  .tool-chip {
    min-height: 34px;
    padding: 7px 10px;
    white-space: nowrap;
  }

  .settings-config-tools {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    padding: 10px 14px 9px;
  }

  .settings-config-info {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 10px;
  }

  .settings-config-desc {
    margin-top: 0;
    font-size: 11px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .settings-config-tools .tool-chip-group {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    width: 100%;
    gap: 6px;
  }

  .settings-config-tools .tool-chip {
    justify-content: center;
    min-width: 0;
    min-height: 36px;
    padding: 7px 8px;
  }

  .image-history-tools {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .tool-chip-group {
    width: 100%;
    justify-content: flex-start;
  }
}

@media (max-width: 380px) {
  .welcome-tips { grid-template-columns: 1fr; }
  .toolbar-right .model-dropdown-btn { max-width: 120px; }
  #current-model { max-width: 80px; }
  .image-param-field { padding: 7px; }
  .image-param-field span,
  .image-param-field select { font-size: 11px; }
  .image-params .model-dropdown-btn { max-width: min(180px, calc(100vw - 88px)); }
  .settings-config-desc { display: none; }
}
