/* ── Reset & Base ─────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg-base:        #0d0d0f;
  --bg-surface:     #18181b;
  --bg-elevated:    #222227;
  --bg-tool:        #141417;
  --bg-input:       #1e1e22;
  --border:         #2e2e35;
  --border-subtle:  #232328;

  --text-primary:   #ececf1;
  --text-secondary: #9b9ba8;
  --text-muted:     #6b6b78;
  --text-code:      #e2e8f0;

  --accent:         #8b5cf6;
  --accent-dim:     #6d28d9;
  --accent-glow:    rgba(139, 92, 246, 0.15);
  --user-bubble:    #1a1a2e;
  --user-border:    #312e6b;

  --green:          #10b981;
  --red:            #ef4444;
  --yellow:         #f59e0b;

  --radius-sm:      6px;
  --radius-md:      12px;
  --radius-lg:      18px;

  --font-sans:      -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  --font-mono:      'JetBrains Mono', 'Fira Code', 'Cascadia Code', Consolas, monospace;

  --header-h:       58px;
  --footer-h:       100px;
}

html, body {
  height: 100%;
  background: var(--bg-base);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.6;
  overflow: hidden;
}

/* ── Auth overlay ─────────────────────────────────────────────────────────── */
#auth-overlay {
  position: fixed; inset: 0; z-index: 999;
  background: var(--bg-base);
  display: flex; align-items: center; justify-content: center;
  animation: fadeIn 0.3s ease;
}
#auth-overlay.hidden { display: none; }

.auth-box {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 48px 40px;
  width: 100%;
  max-width: 380px;
  text-align: center;
  box-shadow: 0 24px 64px rgba(0,0,0,0.6);
}

.auth-logo {
  font-size: 48px;
  color: var(--accent);
  margin-bottom: 16px;
  display: block;
  filter: drop-shadow(0 0 12px var(--accent));
}

.auth-box h2 { font-size: 24px; font-weight: 600; margin-bottom: 6px; }
.auth-box > p { color: var(--text-secondary); font-size: 14px; margin-bottom: 28px; }

#auth-form { display: flex; flex-direction: column; gap: 12px; }

#auth-input {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-size: 15px;
  padding: 12px 16px;
  outline: none;
  transition: border-color 0.2s;
}
#auth-input:focus { border-color: var(--accent); }

#auth-form button {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-md);
  padding: 12px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
}
#auth-form button:hover { background: var(--accent-dim); }
#auth-form button:active { transform: scale(0.98); }

.auth-error { color: var(--red); font-size: 13px; margin-top: 10px; min-height: 20px; }

/* ── Layout ───────────────────────────────────────────────────────────────── */
#app {
  display: flex; flex-direction: column;
  height: 100vh;
  max-width: 900px;
  margin: 0 auto;
}

/* ── Header ───────────────────────────────────────────────────────────────── */
#header {
  height: var(--header-h);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 20px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
  position: sticky; top: 0; z-index: 10;
}

.header-left { display: flex; align-items: center; gap: 12px; }

.header-logo {
  font-size: 24px;
  color: var(--accent);
  filter: drop-shadow(0 0 8px var(--accent));
}

.header-name { font-size: 16px; font-weight: 600; display: block; }
.header-model { font-size: 12px; color: var(--text-muted); display: block; }

.header-right { display: flex; align-items: center; gap: 10px; }

.status-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
  transition: background 0.3s;
}
.status-dot.connected    { background: var(--green); box-shadow: 0 0 6px var(--green); }
.status-dot.disconnected { background: var(--red); }
.status-dot.connecting   { background: var(--yellow); animation: pulse 1.2s infinite; }

.status-text { font-size: 13px; color: var(--text-secondary); }

#clear-btn {
  background: var(--bg-elevated);
  color: var(--text-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 12px;
  font-size: 13px;
  cursor: pointer;
  transition: color 0.2s, border-color 0.2s;
}
#clear-btn:hover { color: var(--text-primary); border-color: var(--accent); }

/* ── Chat area ────────────────────────────────────────────────────────────── */
#chat-area {
  flex: 1;
  overflow-y: auto;
  padding: 24px 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  scroll-behavior: smooth;
}

#chat-area::-webkit-scrollbar { width: 6px; }
#chat-area::-webkit-scrollbar-track { background: transparent; }
#chat-area::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

#messages { display: flex; flex-direction: column; gap: 20px; }

/* ── Message bubbles ─────────────────────────────────────────────────────── */
.message { display: flex; gap: 12px; animation: slideUp 0.25s ease; }
.message.user { flex-direction: row-reverse; }

.avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 15px;
  flex-shrink: 0;
  margin-top: 2px;
}
.message.user .avatar    { background: var(--user-bubble); border: 1px solid var(--user-border); }
.message.assistant .avatar { background: var(--accent-glow); border: 1px solid var(--accent); color: var(--accent); }

.bubble {
  max-width: 78%;
  padding: 12px 16px;
  border-radius: var(--radius-lg);
  line-height: 1.65;
  font-size: 14.5px;
  word-break: break-word;
}

.message.user .bubble {
  background: var(--user-bubble);
  border: 1px solid var(--user-border);
  border-radius: var(--radius-lg) var(--radius-lg) var(--radius-sm) var(--radius-lg);
  color: #c4c4f3;
}

.message.assistant .bubble {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg) var(--radius-lg) var(--radius-lg) var(--radius-sm);
  color: var(--text-primary);
}

/* Markdown inside bubbles */
.bubble p { margin: 0 0 10px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble h1, .bubble h2, .bubble h3 { margin: 14px 0 6px; font-weight: 600; }
.bubble h1 { font-size: 1.25em; }
.bubble h2 { font-size: 1.1em; }
.bubble h3 { font-size: 1em; }
.bubble ul, .bubble ol { padding-left: 20px; margin: 6px 0; }
.bubble li { margin: 3px 0; }
.bubble a { color: var(--accent); text-decoration: none; }
.bubble a:hover { text-decoration: underline; }
.bubble strong { color: #fff; }
.bubble em { color: var(--text-secondary); }
.bubble hr { border: none; border-top: 1px solid var(--border); margin: 12px 0; }
.bubble blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 12px;
  color: var(--text-secondary);
  margin: 8px 0;
}

/* Inline code */
.bubble code:not(.hljs) {
  background: var(--bg-elevated);
  color: #f472b6;
  padding: 2px 6px;
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 13px;
}

/* Code blocks */
.bubble pre {
  background: #0f0f14 !important;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  margin: 10px 0;
  overflow: hidden;
}

.code-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 14px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-muted);
  font-family: var(--font-mono);
}

.copy-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 11px;
  cursor: pointer;
  transition: color 0.2s, border-color 0.2s;
}
.copy-btn:hover { color: var(--text-primary); border-color: var(--accent); }

.bubble pre code {
  display: block;
  padding: 14px 16px;
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.6;
  overflow-x: auto;
  background: transparent !important;
}

/* ── Tool blocks ──────────────────────────────────────────────────────────── */
.tool-block {
  max-width: 88%;
  margin-left: 44px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
  background: var(--bg-tool);
  animation: slideUp 0.2s ease;
  font-size: 13.5px;
}

.tool-header {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 14px;
  background: var(--bg-elevated);
  cursor: pointer;
  user-select: none;
  border-bottom: 1px solid var(--border-subtle);
  transition: background 0.15s;
}
.tool-header:hover { background: #2a2a30; }

.tool-icon { font-size: 15px; }
.tool-name { font-weight: 600; color: var(--text-primary); flex: 1; font-family: var(--font-mono); font-size: 13px; }
.tool-chevron { color: var(--text-muted); font-size: 12px; transition: transform 0.2s; margin-left: auto; }
.tool-block.open .tool-chevron { transform: rotate(90deg); }

.tool-body { display: none; }
.tool-block.open .tool-body { display: block; }

.tool-section {
  padding: 10px 14px;
  border-top: 1px solid var(--border-subtle);
}
.tool-section:first-child { border-top: none; }

.tool-section-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 6px;
  font-weight: 600;
}

.tool-section pre {
  background: #0c0c10;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-family: var(--font-mono);
  font-size: 12.5px;
  color: var(--text-code);
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 320px;
  overflow-y: auto;
  line-height: 1.5;
}

.tool-section pre::-webkit-scrollbar { width: 5px; height: 5px; }
.tool-section pre::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

.tool-status {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 999px;
  margin-left: 8px;
}
.tool-status.running { color: var(--yellow); background: rgba(245, 158, 11, 0.1); }
.tool-status.done    { color: var(--green);  background: rgba(16, 185, 129, 0.1); }

/* ── Typing indicator ─────────────────────────────────────────────────────── */
.typing-indicator {
  display: flex; align-items: center; gap: 10px;
  margin-left: 44px;
  padding: 8px 0;
}
.typing-indicator.hidden { display: none; }

.typing-bubble {
  display: flex; gap: 5px;
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: 999px;
  padding: 8px 14px;
}

.typing-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: typingBounce 1.2s infinite;
}
.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }

.typing-label { font-size: 13px; color: var(--text-muted); }

/* ── Input bar ────────────────────────────────────────────────────────────── */
#input-bar {
  padding: 12px 16px 16px;
  background: var(--bg-base);
  border-top: 1px solid var(--border-subtle);
  flex-shrink: 0;
}

.input-wrapper {
  display: flex; align-items: flex-end; gap: 10px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 10px 14px;
  transition: border-color 0.2s, box-shadow 0.2s;
}
.input-wrapper:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

#user-input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text-primary);
  font-size: 14.5px;
  font-family: var(--font-sans);
  line-height: 1.5;
  resize: none;
  max-height: 160px;
  overflow-y: auto;
}
#user-input::placeholder { color: var(--text-muted); }
#user-input:disabled { opacity: 0.5; cursor: not-allowed; }
#user-input::-webkit-scrollbar { width: 4px; }
#user-input::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

#send-btn {
  background: var(--accent);
  border: none;
  border-radius: var(--radius-sm);
  width: 34px; height: 34px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.2s, transform 0.1s, opacity 0.2s;
}
#send-btn:hover:not(:disabled) { background: var(--accent-dim); }
#send-btn:active:not(:disabled) { transform: scale(0.93); }
#send-btn:disabled { opacity: 0.3; cursor: not-allowed; }
#send-btn svg { width: 15px; height: 15px; stroke: #fff; }

.input-hint {
  text-align: center;
  font-size: 11.5px;
  color: var(--text-muted);
  margin-top: 8px;
}

/* ── Welcome message ─────────────────────────────────────────────────────── */
.welcome {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
}
.welcome-icon { font-size: 48px; margin-bottom: 16px; }
.welcome h3 { font-size: 18px; color: var(--text-secondary); margin-bottom: 8px; }
.welcome p { font-size: 14px; line-height: 1.6; max-width: 360px; margin: 0 auto; }

/* ── Animations ───────────────────────────────────────────────────────────── */
@keyframes slideUp {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: translateY(0); }
}
@keyframes fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}
@keyframes typingBounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
  40%           { transform: translateY(-5px); opacity: 1; }
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.4; }
}

/* ── Responsive ───────────────────────────────────────────────────────────── */
@media (max-width: 600px) {
  .bubble { max-width: 90%; font-size: 14px; }
  .tool-block { max-width: 96%; margin-left: 0; }
  #header { padding: 0 12px; }
  .header-model { display: none; }
  #chat-area { padding: 16px 10px 8px; }
  #input-bar { padding: 8px 10px 12px; }
}
