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

:root {
  --bg: #0f1115;
  --bg-card: #161920;
  --bg-input: #1a1e27;
  --bg-hover: #1e2330;
  --bg-pre: #0d1117;
  --border: #2a2f3c;
  --text: #e5e7eb;
  --text-dim: #9ca3af;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --danger: #ef4444;
  --success: #22c55e;
  --user-bubble: #2d333b;
  --user-bubble-text: #e5e7eb;
  --send-btn-bg: #2d333b;
  --send-btn-bg-hover: #3d444d;
  --send-btn-text: #e5e7eb;
  --radius: 12px;
  --font-size: 16px;
  --sidebar-font-size: calc(var(--font-size) * 0.875);
  --sidebar-font-size-sm: calc(var(--font-size) * 0.6875);
  --chat-max-width: 760px;
}

/* Light theme */
[data-theme="light"] {
  --bg: #f5f7fa;
  --bg-card: #ffffff;
  --bg-input: #f0f2f5;
  --bg-hover: #e8ebf0;
  --bg-pre: #f1f3f5;
  --border: #d1d5db;
  --text: #1f2937;
  --text-dim: #6b7280;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --danger: #ef4444;
  --success: #16a34a;
  --user-bubble: #d1d5db;
  --user-bubble-text: #1f2937;
  --send-btn-bg: #4b5563;
  --send-btn-bg-hover: #374151;
  --send-btn-text: #ffffff;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: var(--font-size);
}

.screen { width: 100%; height: 100%; }
.hidden { display: none !important; }

/* ==================== LOADING SCREEN ==================== */
#loading-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 9999;
  position: fixed;
  inset: 0;
}
.loading-spinner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}
.loading-spinner svg {
  animation: pulse 2s infinite ease-in-out;
  filter: drop-shadow(0 0 8px rgba(0, 229, 255, 0.3));
}
@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.7; transform: scale(0.95); }
}
.loading-dots {
  display: flex;
  gap: 6px;
}
.loading-dots span {
  width: 8px;
  height: 8px;
  background: var(--accent);
  border-radius: 50%;
  animation: bounce 1.4s infinite ease-in-out;
}
.loading-dots span:nth-child(1) { animation-delay: 0s; }
.loading-dots span:nth-child(2) { animation-delay: 0.2s; }
.loading-dots span:nth-child(3) { animation-delay: 0.4s; }

/* ==================== LOGIN ==================== */
#login-screen {
  display: flex; align-items: center; justify-content: center;
  background: var(--bg);
}
.login-box {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 40px 32px;
  width: 320px;
  text-align: center;
}
.login-logo {
  margin-bottom: 20px;
  display: flex;
  justify-content: center;
}
.login-logo svg {
  filter: drop-shadow(0 0 6px rgba(0, 229, 255, 0.25));
}
.login-box h1 { font-size: 24px; margin-bottom: 20px; }
.login-tabs {
  display: flex; gap: 4px; margin-bottom: 20px;
  background: var(--bg); border-radius: 8px; padding: 3px;
}
.login-tabs .tab {
  flex: 1; padding: 8px; border: none; border-radius: 6px;
  background: transparent; color: var(--text-dim);
  cursor: pointer; font-size: 14px;
  transition: background 0.15s, color 0.15s;
}
.login-tabs .tab:hover {
  color: var(--text);
  background: var(--send-btn-bg-hover);
}
.login-tabs .tab.active {
  background: var(--send-btn-bg);
  color: var(--send-btn-text);
}
.login-box input {
  width: 100%; padding: 12px 14px; margin-bottom: 10px;
  background: var(--bg); border: 1px solid var(--border);
  border-radius: 8px; color: var(--text); font-size: 15px;
  outline: none;
  transition: border-color 0.15s;
}
.login-box input:focus { border-color: var(--accent); }
.login-box button[type=submit] {
  width: 100%; padding: 12px; border: none; border-radius: 8px;
  background: var(--accent); color: #fff; font-size: 15px;
  cursor: pointer; margin-top: 4px;
  transition: background 0.15s, transform 0.1s;
}
.login-box button[type=submit]:hover {
  background: var(--accent-hover);
  transform: translateY(-1px);
}
.login-box button[type=submit]:active {
  transform: translateY(0);
}
.auth-error { color: var(--danger); font-size: 13px; margin-top: 10px; min-height: 18px; }

/* ==================== HEADER ==================== */
#header {
  position: fixed; top: 0; left: 0; right: 0; z-index: 100;
  display: flex; align-items: center; gap: 6px;
  height: 52px; padding: 0 10px;
  background: var(--bg-card); border-bottom: 1px solid var(--border);
}
.header-title {
  flex: 1; font-size: 15px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* ==================== CUSTOM MODEL SELECTOR ==================== */
.model-select-wrap {
  position: relative;
  flex-shrink: 0;
}
.model-select-btn {
  display: flex; align-items: center; gap: 6px;
  padding: 5px 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  max-width: 150px;
  transition: border-color 0.15s, background 0.15s;
}
.model-select-btn:hover {
  border-color: var(--accent);
  background: var(--bg-hover);
}
.model-select-label {
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  max-width: 110px;
}
.model-select-caret {
  flex-shrink: 0;
  color: var(--text-dim);
  transition: transform 0.2s;
}
.model-select-wrap.open .model-select-caret {
  transform: rotate(180deg);
}

.model-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  min-width: 180px;
  max-height: 320px;
  overflow-y: auto;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 12px 32px rgba(0,0,0,0.4);
  padding: 6px;
  z-index: 300;
  animation: menuIn 0.15s ease;
}
@keyframes menuIn {
  from { opacity: 0; transform: translateY(-6px); }
  to { opacity: 1; transform: translateY(0); }
}
.model-menu-item {
  display: flex; align-items: center; gap: 8px;
  width: 100%;
  padding: 9px 12px;
  border: none;
  border-radius: 7px;
  background: transparent;
  color: var(--text);
  font-size: 13px;
  text-align: left;
  cursor: pointer;
  white-space: nowrap;
}
.model-menu-item:hover {
  background: var(--bg-hover);
}
.model-menu-item.active {
  background: var(--accent);
  color: #fff;
}
.model-menu-item.offline {
  color: var(--text-dim);
  opacity: 0.55;
  cursor: not-allowed;
}
.model-menu-item .model-dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--success);
  flex-shrink: 0;
}
.model-menu-item.offline .model-dot {
  background: var(--text-dim);
}
.model-menu-item .model-check {
  margin-left: auto;
  color: var(--text-dim);
}
.model-menu-item.active .model-check {
  color: #fff;
}

.icon-btn {
  background: none; border: none; color: var(--text-dim);
  cursor: pointer; padding: 6px;
  border-radius: 6px; line-height: 1;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }
.new-chat-header-btn {
  color: var(--accent);
}
.new-chat-header-btn:hover {
  background: var(--bg-hover);
  color: var(--accent);
}

/* ==================== SIDEBAR ==================== */
#sidebar {
  position: fixed; top: 52px; left: 0;
  bottom: 0;
  z-index: 110;
  width: 280px; background: var(--bg-card);
  border-right: 1px solid var(--border);
  transform: translateX(-100%);
  transition: transform 0.2s ease;
  display: flex; flex-direction: column;
}
#sidebar.open { transform: translateX(0); }
#sidebar-overlay {
  position: fixed; inset: 0; z-index: 105;
  background: rgba(0,0,0,0.5);
  display: none;
}
#sidebar-overlay.show { display: block; }
.new-chat-btn {
  margin: 12px; padding: 10px; border: 1px dashed var(--border);
  border-radius: 8px; background: none; color: var(--text);
  font-size: var(--sidebar-font-size); cursor: pointer;
  display: flex; align-items: center; gap: 6px; justify-content: center;
  flex-shrink: 0;
}
.new-chat-btn:hover { border-color: var(--accent); color: var(--accent); }
#chat-list { flex: 1; overflow-y: auto; padding: 0 8px; }
.chat-item {
  display: flex; flex-direction: column;
  padding: 10px 12px; border-radius: 8px; cursor: pointer;
  color: var(--text-dim);
}
.chat-item:hover { background: var(--bg-hover); }
.chat-item.active { background: var(--bg-hover); color: var(--text); }
.chat-item .chat-top {
  display: flex; align-items: center; gap: 6px;
}
.chat-item .chat-name {
  flex: 1; font-size: var(--sidebar-font-size);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-item .chat-date {
  font-size: var(--sidebar-font-size-sm); color: var(--text-dim); margin-top: 2px;
}
.chat-item .chat-del {
  opacity: 0; font-size: 14px; color: var(--text-dim);
  flex-shrink: 0; cursor: pointer;
}
.chat-item:hover .chat-del { opacity: 1; }
.chat-item .chat-del:hover { color: var(--danger); }

/* Sidebar footer (export + logout) */
.sidebar-footer {
  padding: 12px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.sidebar-export {
  display: flex; align-items: center; gap: 8px;
  width: 100%; padding: 10px 12px;
  border: none; border-radius: 8px;
  background: none; color: var(--text-dim);
  font-size: var(--sidebar-font-size); cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.sidebar-export:hover {
  background: var(--bg-hover);
  color: var(--accent);
}
.sidebar-export svg { flex-shrink: 0; }

.sidebar-logout {
  display: flex; align-items: center; gap: 8px;
  width: 100%; padding: 10px 12px;
  border: none; border-radius: 8px;
  background: none; color: var(--text-dim);
  font-size: var(--sidebar-font-size); cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.sidebar-logout:hover {
  background: var(--bg-hover);
  color: var(--danger);
}
.sidebar-logout svg { flex-shrink: 0; }

/* ==================== SCROLL-TO-BOTTOM BUTTON ==================== */
.scroll-bottom-btn {
  position: fixed;
  bottom: 80px;
  right: 16px;
  z-index: 150;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text-dim);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 12px rgba(0,0,0,0.25);
  transition: opacity 0.2s, transform 0.2s, color 0.15s, border-color 0.15s;
  animation: fadeIn 0.2s ease;
}
.scroll-bottom-btn:hover {
  color: var(--accent);
  border-color: var(--accent);
  transform: translateY(-2px);
}
.scroll-bottom-btn.hidden {
  display: none !important;
}

/* ==================== MESSAGES ==================== */
#messages {
  position: fixed; top: 52px; left: 0; right: 0;
  bottom: 0;
  overflow-y: auto;
  scroll-behavior: smooth;
}

.messages-inner {
  max-width: var(--chat-max-width);
  margin: 0 auto;
  padding: 20px 16px 120px;
}

.msg {
  margin-bottom: 20px;
  animation: fadeIn 0.15s ease;
}
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; } }

.msg.user {
  display: flex; flex-direction: column; align-items: flex-end;
}
.msg.assistant {
  display: flex; flex-direction: column; align-items: flex-start;
}
.msg.tool {
  display: flex; justify-content: center;
}

/* User message */
.msg.user .msg-content {
  background: var(--user-bubble);
  color: var(--user-bubble-text);
  padding: 10px 14px;
  border-radius: var(--radius) var(--radius) 4px var(--radius);
  max-width: 85%;
  font-size: var(--font-size); line-height: 1.5;
  word-wrap: break-word; white-space: pre-wrap;
}
.msg.user .msg-content img {
  max-width: 100%; max-height: 300px;
  border-radius: 8px; margin-top: 6px; display: block;
}

/* Assistant message — без фона */
.msg.assistant .msg-content {
  max-width: 100%;
  font-size: var(--font-size); line-height: 1.7;
  color: var(--text);
  word-wrap: break-word;
}

/* ==================== INLINE TOOL CALL (внутри assistant) ==================== */
.tool-call-inline {
  margin: 10px 0;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  transition: border-color 0.3s, box-shadow 0.3s;
}

/* Pending (выполняется) — акцентная рамка + свечение */
.tool-call-inline.pending {
  border-color: var(--accent);
  box-shadow: 0 0 8px rgba(59, 130, 246, 0.15);
}

.tool-call-inline .tool-call-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  cursor: pointer;
  user-select: none;
  transition: background 0.15s;
}
.tool-call-inline .tool-call-header:hover {
  background: var(--bg-hover);
}

.tool-call-icon {
  font-size: 14px;
  flex-shrink: 0;
}

.tool-call-name {
  flex: 1;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tool-call-status {
  font-size: 12px;
  color: var(--success);
  flex-shrink: 0;
  display: flex;
  align-items: center;
}
.tool-call-status.running {
  color: var(--accent);
}

/* Анимированный спиннер внутри tool-call status */
.tool-spinner {
  display: inline-flex;
  gap: 3px;
  align-items: center;
}
.tool-spinner span {
  width: 5px;
  height: 5px;
  background: var(--accent);
  border-radius: 50%;
  animation: bounce 1.4s infinite ease-in-out;
}
.tool-spinner span:nth-child(1) { animation-delay: 0s; }
.tool-spinner span:nth-child(2) { animation-delay: 0.2s; }
.tool-spinner span:nth-child(3) { animation-delay: 0.4s; }

.tool-call-toggle {
  font-size: 10px;
  color: var(--text-dim);
  transition: transform 0.2s;
  flex-shrink: 0;
}
.tool-call-toggle.expanded {
  transform: rotate(180deg);
}

.tool-call-body {
  display: none;
  padding: 0 12px 10px;
  border-top: 1px solid var(--border);
}
.tool-call-body.expanded {
  display: block;
}

.tool-call-section {
  margin-top: 10px;
}

.tool-call-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 4px;
}

.tool-call-pre {
  background: var(--bg-pre);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 12px;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 12px;
  line-height: 1.5;
  color: var(--text);
  white-space: pre-wrap;
  word-wrap: break-word;
  max-height: 300px;
  overflow-y: auto;
}

.tool-call-result {
  color: var(--text-dim);
}

/* "Выполняется..." секция с анимированными точками */
.tool-call-running {
  padding: 4px 0;
}
.tool-running-dots {
  display: flex;
  gap: 4px;
  align-items: center;
  padding: 4px 0;
}
.tool-running-dots span {
  width: 6px;
  height: 6px;
  background: var(--accent);
  border-radius: 50%;
  animation: bounce 1.4s infinite ease-in-out;
}
.tool-running-dots span:nth-child(1) { animation-delay: 0s; }
.tool-running-dots span:nth-child(2) { animation-delay: 0.2s; }
.tool-running-dots span:nth-child(3) { animation-delay: 0.4s; }

/* Inline thinking (между tool calls внутри assistant) */
.inline-thinking {
  padding: 6px 0;
}
.inline-thinking .thinking {
  padding: 0;
}
.thinking-label {
  font-size: 12px;
  color: var(--text-dim);
  margin-left: 4px;
}

/* text-stream контейнер внутри assistant */
.msg.assistant .msg-content .text-stream {
  /* наследует все стили markdown */
}

/* ==================== WELCOME SCREEN ==================== */
.welcome-screen {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  min-height: calc(100vh - 220px);
  text-align: center;
  gap: 12px;
}
.welcome-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: auto;
  height: auto;
}
.welcome-icon svg {
  width: calc(var(--font-size) * 3.5);
  height: calc(var(--font-size) * 4);
  display: block;
}
.welcome-screen h2 {
  font-size: calc(var(--font-size) * 1.5);
  font-weight: 600;
}
.welcome-screen p {
  color: var(--text-dim);
  font-size: calc(var(--font-size) * 0.94);
}
.welcome-new-chat {
  margin-top: 8px;
  padding: 10px 20px;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: transparent;
  color: var(--accent);
  font-size: calc(var(--font-size) * 0.94);
  cursor: pointer;
  display: flex; align-items: center; gap: 8px;
}
.welcome-new-chat:hover { background: var(--accent); color: #fff; }

/* ==================== THINKING INDICATOR ==================== */
.thinking {
  display: flex; align-items: center; gap: 8px;
  color: var(--text-dim); font-size: 14px;
  padding: 4px 0;
}
.thinking-dots {
  display: flex; gap: 4px;
}
.thinking-dots span {
  width: 6px; height: 6px;
  background: var(--accent);
  border-radius: 50%;
  animation: bounce 1.4s infinite ease-in-out;
}
.thinking-dots span:nth-child(1) { animation-delay: 0s; }
.thinking-dots span:nth-child(2) { animation-delay: 0.2s; }
.thinking-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}

/* ==================== MARKDOWN (assistant) ==================== */
.msg.assistant .msg-content h1,
.msg.assistant .msg-content h2,
.msg.assistant .msg-content h3,
.msg.assistant .msg-content h4 {
  margin: 16px 0 8px;
  font-weight: 600;
}
.msg.assistant .msg-content h1 { font-size: 1.4em; }
.msg.assistant .msg-content h2 { font-size: 1.25em; }
.msg.assistant .msg-content h3 { font-size: 1.1em; }

.msg.assistant .msg-content p {
  margin: 8px 0;
}

.msg.assistant .msg-content ul,
.msg.assistant .msg-content ol {
  margin: 8px 0; padding-left: 24px;
}
.msg.assistant .msg-content li { margin: 4px 0; }

.msg.assistant .msg-content blockquote {
  border-left: 3px solid var(--accent);
  padding: 4px 12px; margin: 8px 0;
  color: var(--text-dim);
  background: var(--bg-hover);
  border-radius: 0 6px 6px 0;
}

.msg.assistant .msg-content code {
  background: var(--bg-pre);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 0.9em;
}

.msg.assistant .msg-content pre {
  background: var(--bg-pre);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px;
  margin: 12px 0;
  overflow-x: auto;
}
.msg.assistant .msg-content pre code {
  background: none; padding: 0;
  font-size: 13px; line-height: 1.5;
}

.msg.assistant .msg-content table {
  border-collapse: collapse;
  margin: 12px 0;
  width: 100%;
}
.msg.assistant .msg-content th,
.msg.assistant .msg-content td {
  border: 1px solid var(--border);
  padding: 8px 12px;
  text-align: left;
}
.msg.assistant .msg-content th {
  background: var(--bg-hover);
  font-weight: 600;
}

.msg.assistant .msg-content a {
  color: var(--accent);
  text-decoration: none;
}
.msg.assistant .msg-content a:hover { text-decoration: underline; }

.msg.assistant .msg-content img {
  max-width: 100%;
  border-radius: 8px;
  margin: 8px 0;
}

/* ==================== INPUT ==================== */
#input-area {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  z-index: 100;
  background: var(--bg-card);
  border-top: 1px solid var(--border);
}

.input-inner {
  max-width: var(--chat-max-width);
  margin: 0 auto;
  padding: 10px 12px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
}

#img-previews {
  display: flex; gap: 6px; margin-bottom: 6px;
  flex-wrap: wrap;
}
#img-previews:empty { display: none; }
.img-preview {
  position: relative; width: 56px; height: 56px;
  border-radius: 8px; overflow: hidden;
  border: 1px solid var(--border);
}
.img-preview img { width: 100%; height: 100%; object-fit: cover; }
.img-preview .remove {
  position: absolute; top: 2px; right: 2px;
  width: 18px; height: 18px; border-radius: 50%;
  background: rgba(0,0,0,0.7); color: #fff;
  border: none; font-size: 12px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}

/* Input row */
.input-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.input-btn {
  width: 40px; height: 40px;
  min-width: 40px; min-height: 40px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text-dim);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.input-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.input-row textarea {
  flex: 1;
  resize: none;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  color: var(--text);
  font-size: var(--font-size);
  line-height: 1.4;
  height: 40px;
  max-height: 120px;
  outline: none;
  font-family: inherit;
  min-width: 0;
  overflow: hidden;
}

.input-row textarea:focus { border-color: var(--accent); }

.send-btn {
  width: 40px; height: 40px;
  min-width: 40px; min-height: 40px;
  border-radius: 50%;
  background: var(--send-btn-bg);
  border: none;
  color: var(--send-btn-text);
  cursor: pointer;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
}
.send-btn:disabled { opacity: 0.4; cursor: default; }
.send-btn:hover:not(:disabled) { background: var(--send-btn-bg-hover); }
.send-btn.stop-mode { background: var(--danger); color: #fff; }
.send-btn.stop-mode:hover { background: #dc2626; }

/* ==================== MESSAGE ACTIONS ==================== */
.msg-actions {
  display: flex; gap: 2px; margin-top: 4px;
  opacity: 0; transition: opacity 0.15s;
}
.msg:hover .msg-actions { opacity: 1; }
.msg-action-btn {
  background: none; border: none;
  color: var(--text-dim); font-size: 13px;
  cursor: pointer; padding: 3px 7px;
  border-radius: 4px;
  display: flex; align-items: center; gap: 3px;
}
.msg-action-btn:hover { background: var(--bg-hover); color: var(--text); }
.msg-action-btn svg { width: 14px; height: 14px; }
.msg-action-delete:hover { color: var(--danger); }

/* ==================== EDIT MODE ==================== */
.edit-area {
  width: 100%; max-width: 85%;
  background: var(--bg-input); border: 1px solid var(--accent);
  border-radius: 10px; padding: 10px 14px;
  color: var(--text); font-size: var(--font-size); line-height: 1.4;
  font-family: inherit; resize: vertical; min-height: 60px;
  outline: none;
}
.edit-actions {
  display: flex; gap: 8px; margin-top: 6px;
  justify-content: flex-end;
}
.edit-actions button {
  padding: 6px 14px; border-radius: 6px;
  border: none; font-size: 13px; cursor: pointer;
}
.edit-actions .edit-save { background: var(--accent); color: #fff; }
.edit-actions .edit-cancel { background: var(--bg-hover); color: var(--text); }

/* ==================== SETTINGS MODAL ==================== */
.settings-overlay {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
}
.settings-modal {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 16px;
  width: 360px;
  max-width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,0.4);
}
.settings-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--bg-card);
  z-index: 1;
}
.settings-header h3 { font-size: 17px; font-weight: 600; }
.settings-body {
  padding: 20px;
  display: flex; flex-direction: column; gap: 20px;
}
.settings-row {
  display: flex; align-items: center; justify-content: space-between;
}
.settings-row label { font-size: 15px; color: var(--text); }
.font-size-control {
  display: flex; align-items: center; gap: 10px;
}
.font-size-btn {
  width: 36px; height: 36px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 14px; font-weight: 600;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.font-size-btn:hover { border-color: var(--accent); color: var(--accent); }
#font-size-value {
  font-size: 14px; color: var(--text-dim);
  min-width: 40px; text-align: center;
}
#font-size-slider {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
}
.theme-toggle {
  display: flex; gap: 4px;
  background: var(--bg); border-radius: 8px; padding: 3px;
}
.theme-btn {
  padding: 6px 12px; border: none; border-radius: 6px;
  background: transparent; color: var(--text-dim);
  cursor: pointer; font-size: 13px;
}
.theme-btn.active { background: var(--accent); color: #fff; }

/* ==================== CUSTOM COLORS ==================== */
.color-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
}
.color-row:last-child { margin-bottom: 0; }
.color-row label {
  font-size: 14px;
  color: var(--text);
  flex: 1;
}
.color-row input[type="color"] {
  width: 48px; height: 32px;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  cursor: pointer;
  flex-shrink: 0;
}
.color-row input[type="color"]:hover {
  border-color: var(--accent);
}

/* HEX input для ручного ввода цвета (полноценный пикер на мобильных) */
.hex-input {
  width: 80px;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-input);
  color: var(--text);
  font-size: 12px;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  outline: none;
  flex-shrink: 0;
  transition: border-color 0.15s;
}
.hex-input:focus {
  border-color: var(--accent);
}
.hex-input::placeholder {
  color: var(--text-dim);
  opacity: 0.6;
}

/* Note под заголовком "Моя тема" */
.settings-section-note {
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 12px;
}
.settings-section-note span {
  color: var(--accent);
  font-weight: 500;
}

/* ==================== MCP SERVERS SECTION ==================== */
.settings-section {
  border-top: 1px solid var(--border);
  padding-top: 16px;
}

.settings-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.settings-section-header h4 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

.mcp-add-btn {
  width: 28px; height: 28px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--accent);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.mcp-add-btn:hover {
  border-color: var(--accent);
  background: var(--bg-hover);
}

.mcp-subsection {
  margin-bottom: 12px;
}

.mcp-subsection-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 6px;
}

.mcp-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.mcp-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  gap: 8px;
}

.mcp-item.disabled {
  opacity: 0.6;
}

.mcp-item-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  flex: 1;
}

.mcp-item-name {
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.mcp-item-meta {
  font-size: 11px;
  color: var(--text-dim);
}

.mcp-item-badge {
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  flex-shrink: 0;
}

.mcp-item-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.mcp-item-btn {
  width: 26px; height: 26px;
  border-radius: 5px;
  border: none;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.mcp-item-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
}
.mcp-item-delete:hover {
  color: var(--danger);
}

.mcp-empty {
  font-size: 12px;
  color: var(--text-dim);
  padding: 8px 10px;
  text-align: center;
}

/* Toggle switch */
.mcp-toggle {
  position: relative;
  display: inline-block;
  width: 36px;
  height: 20px;
  flex-shrink: 0;
  cursor: pointer;
}
.mcp-toggle input {
  opacity: 0;
  width: 0;
  height: 0;
}
.mcp-toggle-slider {
  position: absolute;
  inset: 0;
  background: var(--border);
  border-radius: 20px;
  transition: background 0.2s;
}
.mcp-toggle-slider::before {
  content: '';
  position: absolute;
  width: 14px; height: 14px;
  left: 3px; top: 3px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.2s;
}
.mcp-toggle input:checked + .mcp-toggle-slider {
  background: var(--accent);
}
.mcp-toggle input:checked + .mcp-toggle-slider::before {
  transform: translateX(16px);
}

/* MCP Form */
.mcp-form {
  margin-top: 12px;
  padding: 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.mcp-form.hidden {
  display: none;
}

.mcp-form-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 4px;
}

.mcp-form input[type="text"] {
  width: 100%;
  padding: 8px 10px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 13px;
  outline: none;
}
.mcp-form input[type="text"]:focus {
  border-color: var(--accent);
}

.mcp-form-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 4px;
}

.mcp-form-cancel {
  padding: 6px 14px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-dim);
  font-size: 13px;
  cursor: pointer;
}
.mcp-form-cancel:hover {
  background: var(--bg-hover);
}

.mcp-form-save {
  padding: 6px 14px;
  border-radius: 6px;
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 13px;
  cursor: pointer;
}
.mcp-form-save:hover {
  background: var(--accent-hover);
}

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

/* ==================== MOBILE ==================== */
@media (max-width: 800px) {
  .messages-inner {
    max-width: 100%;
    padding: 16px 16px 110px;
  }
  .input-inner {
    max-width: 100%;
    padding: 8px 8px;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }
}

@media (max-width: 600px) {
  .msg.user .msg-content { max-width: 92%; }
  .model-select-btn { max-width: 110px; font-size: 12px; }
  .model-select-label { max-width: 70px; }
  .model-menu { min-width: 150px; }
  #header { gap: 4px; padding: 0 8px; }
  .input-row { gap: 6px; }
  .input-btn { width: 38px; height: 38px; min-width: 38px; min-height: 38px; }
  .send-btn { width: 38px; height: 38px; min-width: 38px; min-height: 38px; }
  .input-row textarea { height: 38px; padding: 8px 12px; }
  .tool-call-pre { max-height: 200px; font-size: 11px; }
  .settings-modal { width: 100%; max-height: 90vh; }
  .hex-input { width: 70px; font-size: 11px; }
  .color-row input[type="color"] { width: 42px; height: 30px; }
  .scroll-bottom-btn { bottom: 72px; right: 10px; width: 32px; height: 32px; }
}

/* ==================== COPY CODE BUTTON ==================== */
.msg.assistant .msg-content pre {
  position: relative;
}
.copy-code-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-card);
  color: var(--text-dim);
  font-size: 11px;
  font-family: inherit;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s, color 0.15s, border-color 0.15s, background 0.15s;
  z-index: 2;
}
.msg.assistant .msg-content pre:hover .copy-code-btn {
  opacity: 1;
}
.copy-code-btn:hover {
  color: var(--text);
  border-color: var(--accent);
  background: var(--bg-hover);
}
.copy-code-btn.copied {
  color: var(--success);
  border-color: var(--success);
  opacity: 1;
}
.copy-code-btn svg {
  width: 12px;
  height: 12px;
}

/* Контейнер для tool-calls — всегда в начале assistant-ответа */
.tool-calls-container {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 8px;
}
.tool-calls-container:empty {
  display: none;
}
.tool-calls-container .tool-call-inline {
  margin: 0; /* отступы уже даёт gap контейнера */
}


/* Состояние: Идет запись (красный пульс) */
.mic-btn.recording {
  color: var(--danger);
  border-color: var(--danger);
  animation: mic-pulse 1.2s infinite;
}

@keyframes mic-pulse {
  0% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.4); }
  70% { box-shadow: 0 0 0 15px rgba(239, 68, 68, 0); }
  100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0); }
}

/* Состояние: Идет распознавание (синий/акцентный пульс + спиннер) */
.mic-btn.transcribing {
  color: var(--accent);
  border-color: var(--accent);
  pointer-events: none; /* Запретить нажатия во время обработки */
}

/* Спиннер внутри кнопки (показывается вместо иконки микрофона, если хочешь) */
.mic-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top-color: currentColor;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

/* Визуальная подсказка в поле ввода (опционально, можно сделать плавающим элементом) */
#voice-status-hint {
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-card);
  border: 1px solid var(--border);
  padding: 4px 8px;
  border-radius: 6px;
  font-size: 12px;
  color: var(--text-dim);
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
  z-index: 10;
}

#voice-status-hint.visible {
  opacity: 1;
}
