:root {
  --bg-primary: #0f1117;
  --bg-secondary: #161822;
  --bg-tertiary: #1c1f2e;
  --bg-hover: #252838;
  --bg-input: #1a1d2b;
  --text-primary: #e4e6f0;
  --text-secondary: #8b8fa3;
  --text-muted: #5c6078;
  --accent: #6c5ce7;
  --accent-glow: rgba(108, 92, 231, 0.25);
  --accent-hover: #7f71ed;
  --border: #2a2d3e;
  --user-bubble: #2d2f45;
  --bot-bubble: #1e2033;
  --error-bg: #3d1f2e;
  --error-text: #f27068;
  --code-bg: #0d0f16;
  --img-bg: #0d0f16;
  --radius: 12px;
  --radius-lg: 16px;
  --font-body: 'DM Sans', system-ui, -apple-system, sans-serif;
  --font-mono: 'JetBrains Mono', 'Fira Code', monospace;
  --transition: 0.2s ease;
}

/* Light theme */
[data-theme="light"] {
  --bg-primary: #f5f6fa;
  --bg-secondary: #ffffff;
  --bg-tertiary: #ebedf3;
  --bg-hover: #e2e4ec;
  --bg-input: #ffffff;
  --text-primary: #1a1c2e;
  --text-secondary: #5c6078;
  --text-muted: #8b8fa3;
  --accent: #5b4cd0;
  --accent-glow: rgba(91, 76, 208, 0.15);
  --accent-hover: #6d5de2;
  --border: #dfe1ea;
  --user-bubble: #e8eaf3;
  --bot-bubble: #ffffff;
  --error-bg: #fde8eb;
  --error-text: #d63040;
  --code-bg: #f0f1f5;
  --img-bg: #ebedf3;
}

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

html, body {
  height: 100%;
  margin: 0;
  overscroll-behavior: none;
  font-family: var(--font-body);
  background: var(--bg-primary);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
}

#app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ---------- Chat Area ---------- */
#chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  max-width: 860px;
  margin: 0 auto;
  width: 100%;
  height: 100vh;
  height: 100dvh;
}

#chat-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-primary);
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--accent);
  font-weight: 700;
  font-size: 1.05rem;
  letter-spacing: -0.3px;
}

.logo svg { opacity: 0.85; flex-shrink: 0; }

.typing-indicator {
  font-size: 0.78rem;
  color: var(--accent);
  font-weight: 500;
  animation: pulse 1.5s ease infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

/* ---------- Messages ---------- */
#messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scroll-behavior: smooth;
  min-height: 0;
  /* Fix iOS Safari 100vh bug */
  -webkit-overflow-scrolling: touch;
}

.welcome-msg {
  text-align: center;
  margin: auto;
  padding: 40px 20px;
  color: var(--text-muted);
}

.welcome-icon { font-size: 3rem; margin-bottom: 16px; }
.welcome-msg h2 { color: var(--text-secondary); font-size: 1.2rem; margin-bottom: 8px; }
.welcome-msg p { font-size: 0.9rem; }

.msg-row {
  display: flex;
  gap: 12px;
  max-width: 85%;
  animation: fadeSlideIn 0.3s ease;
}

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

.msg-row.user { align-self: flex-end; flex-direction: row-reverse; }
.msg-row.assistant { align-self: flex-start; }

.msg-avatar {
  width: 32px; height: 32px;
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-size: 0.85rem;
  flex-shrink: 0;
  margin-top: 2px;
}

.msg-row.user .msg-avatar { background: var(--accent); color: #fff; }
.msg-row.assistant .msg-avatar { background: var(--bg-tertiary); color: var(--text-secondary); border: 1px solid var(--border); }

.msg-bubble {
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 0.9rem;
  line-height: 1.6;
  word-break: break-word;
}

.msg-row.user .msg-bubble {
  background: var(--user-bubble);
  border-bottom-right-radius: 4px;
}

.msg-row.assistant .msg-bubble {
  background: var(--bot-bubble);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.msg-bubble.error {
  background: var(--error-bg);
  color: var(--error-text);
  border-color: rgba(242, 112, 104, 0.2);
}

.msg-bubble pre {
  background: var(--code-bg, var(--bg-primary));
  padding: 12px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 8px 0;
  font-family: var(--font-mono);
  font-size: 0.82rem;
  border: 1px solid var(--border);
}

.msg-bubble code {
  font-family: var(--font-mono);
  font-size: 0.85em;
  background: var(--code-bg, var(--bg-primary));
  padding: 2px 6px;
  border-radius: 4px;
}

.msg-bubble pre code { background: none; padding: 0; }

.msg-bubble p { margin-bottom: 8px; }
.msg-bubble p:last-child { margin-bottom: 0; }

.msg-bubble .msg-image {
  max-width: 100%;
  max-height: 400px;
  border-radius: 8px;
  margin: 8px 0;
  cursor: pointer;
  display: block;
  object-fit: contain;
  background: var(--bg-primary);
}
.msg-bubble .msg-image:hover {
  opacity: 0.9;
  box-shadow: 0 0 12px var(--accent-glow);
}

.msg-bubble ul, .msg-bubble ol { padding-left: 20px; margin: 8px 0; }
.msg-bubble li { margin-bottom: 4px; }

.msg-time {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 4px;
}

.msg-row.user .msg-time { text-align: right; }

/* ---------- Input Area ---------- */
#input-area {
  padding: 12px 16px 16px;
  border-top: 1px solid var(--border);
  background: var(--bg-primary);
}

#chat-form {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 6px 10px;
  transition: border-color var(--transition);
}

#chat-form:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-glow); }

#msg-input {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 0.925rem;
  font-family: var(--font-body);
  resize: none;
  outline: none;
  max-height: 120px;
  line-height: 1.5;
  padding: 4px 2px;
}

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

#send-btn {
  background: var(--accent);
  border: none;
  color: #fff;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all var(--transition);
}

#send-btn:hover { background: var(--accent-hover); transform: scale(1.05); }
#send-btn:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }

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

/* ---------- Login Overlay ---------- */
#login-overlay {
  position: fixed; inset: 0; z-index: 9999;
  background: var(--bg-primary);
  display: flex; align-items: center; justify-content: center;
}
.login-card {
  text-align: center; padding: 40px 36px;
  background: var(--bg-secondary); border: 1px solid var(--border);
  border-radius: 20px; min-width: 320px;
}
.login-logo { font-size: 3rem; margin-bottom: 12px; }
.login-card h2 { color: var(--text-primary); margin-bottom: 24px; font-size: 1.3rem; }
#login-form input {
  width: 100%; padding: 12px 16px;
  background: var(--bg-input); border: 1px solid var(--border);
  border-radius: var(--radius); color: var(--text-primary);
  font-size: 0.95rem; outline: none;
  transition: border-color var(--transition);
}
#login-form input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-glow); }
#login-btn {
  width: 100%; margin-top: 16px; padding: 12px;
  background: var(--accent); color: #fff; border: none;
  border-radius: var(--radius); font-size: 0.95rem; cursor: pointer;
  transition: all var(--transition);
}
#login-btn:hover { background: var(--accent-hover); }
#login-error { color: var(--error-text); margin-top: 12px; font-size: 0.85rem; min-height: 1.2em; }

/* ---------- Mobile ---------- */
@media (max-width: 768px) {
  .msg-row { max-width: 92%; }
  #messages { padding: 16px; }
  #input-area { padding: 12px 16px 16px; }
}

/* ---------- Theme Toggle ---------- */
#theme-toggle {
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  width: 36px;
  height: 36px;
  border-radius: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all var(--transition);
  margin-left: auto;
}
#theme-toggle:hover { background: var(--bg-hover); color: var(--text-primary); }

/* Dark mode: show moon icon (to switch to light) */
[data-theme="dark"] .icon-sun { display: none; }
[data-theme="dark"] .icon-moon { display: block; }
/* Light mode: show sun icon (to switch to dark) */
[data-theme="light"] .icon-sun { display: block; }
[data-theme="light"] .icon-moon { display: none; }

/* ---------- Refresh Button ---------- */
#refresh-btn {
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  width: 36px;
  height: 36px;
  border-radius: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all var(--transition);
}
#refresh-btn:hover { background: var(--bg-hover); color: var(--text-primary); }
#refresh-btn.spinning svg { animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Tools Toggle Button ---------- */
.tools-btn {
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  height: 36px;
  padding: 0 14px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 0.82rem;
  font-family: var(--font-body);
  display: flex;
  align-items: center;
  flex-shrink: 0;
  transition: all var(--transition);
  gap: 5px;
}
.tools-btn:hover { background: var(--bg-hover); color: var(--text-primary); }
.tools-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

/* ---------- Tool Blocks (hidden by default) ---------- */
.tool-block {
  margin-top: 8px;
  border-top: 1px dashed var(--border);
  padding-top: 8px;
}

.tool-call {
  margin-bottom: 6px;
}
.tool-name {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--accent);
}
.tool-args {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  background: var(--code-bg, var(--bg-primary));
  padding: 8px 10px;
  border-radius: 6px;
  overflow-x: auto;
  margin-top: 4px;
  white-space: pre-wrap;
  word-break: break-all;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  max-height: 120px;
}

.tool-result {
  margin-bottom: 6px;
}
.tool-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-weight: 500;
}
.tool-result pre {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  background: var(--code-bg, var(--bg-primary));
  padding: 8px 10px;
  border-radius: 6px;
  overflow-x: auto;
  margin-top: 4px;
  white-space: pre-wrap;
  word-break: break-all;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  max-height: 120px;
}
