:root {
  --bg: #0f0f13;
  --surface: #1a1a23;
  --surface2: #22222e;
  --surface3: #2a2a38;
  --border: #2e2e40;
  --text: #f0f0f5;
  --text-muted: #6b6b85;
  --accent: #e8b84b;
  --accent-glow: #e8b84b33;
  --accent-dim: #e8b84b18;
  --user-bg: #1e1e3a;
  --user-border: #3a3a6a;
  --user-text: #c8c8f0;
  --error: #ff5c5c;
  --tool-bg: #0e1e12;
  --tool-text: #5dba6e;
  --tool-border: #1e3a22;
  --radius: 18px;
  --radius-sm: 10px;
}

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

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

#app { height: 100vh; display: flex; flex-direction: column; }

.screen { flex: 1; display: flex; flex-direction: column; }
.hidden { display: none !important; }

/* ── Auth ── */
#auth-screen {
  justify-content: center;
  align-items: center;
  padding: 32px 24px;
  background: radial-gradient(ellipse at 50% 30%, #1a1530 0%, var(--bg) 70%);
}

.auth-box {
  width: 100%;
  max-width: 300px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}

.auth-logo {
  width: 72px; height: 72px;
  background: var(--accent-dim);
  border: 1px solid var(--accent);
  border-radius: 20px;
  display: flex; align-items: center; justify-content: center;
  font-size: 36px;
  box-shadow: 0 0 32px var(--accent-glow);
}

.auth-box h1 {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.5px;
  color: var(--text);
}

.auth-hint {
  color: var(--text-muted);
  font-size: 14px;
  margin-top: -8px;
}

#code-input {
  width: 100%;
  padding: 14px 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 16px;
  outline: none;
  text-align: center;
  letter-spacing: 3px;
  transition: border-color 0.2s, box-shadow 0.2s;
}

#code-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

#auth-btn {
  width: 100%;
  padding: 14px;
  background: var(--accent);
  color: #0f0f13;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  letter-spacing: 0.3px;
  transition: opacity 0.15s, box-shadow 0.2s;
  box-shadow: 0 4px 20px var(--accent-glow);
}

#auth-btn:active { opacity: 0.8; }

.error { color: var(--error); font-size: 13px; }

/* ── Chat ── */
#chat-screen { height: 100vh; }

#header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 13px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.header-logo { font-size: 18px; color: var(--accent); }

.header-title {
  font-weight: 700;
  font-size: 15px;
  color: var(--text);
  letter-spacing: -0.2px;
}

#clear-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 15px;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 8px;
  transition: all 0.15s;
}

#clear-btn:hover {
  color: var(--text);
  border-color: var(--text-muted);
  background: var(--surface2);
}

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-behavior: smooth;
}

/* ── Bubbles ── */
.bubble {
  max-width: 86%;
  padding: 10px 14px;
  border-radius: var(--radius);
  line-height: 1.55;
  word-break: break-word;
  font-size: 15px;
  color: var(--text);
}

.bubble.user {
  align-self: flex-end;
  background: var(--user-bg);
  color: var(--user-text);
  border: 1px solid var(--user-border);
  border-bottom-right-radius: 5px;
}

.bubble.assistant {
  align-self: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 5px;
}

/* ── Tool badge ── */
.tool-badge {
  align-self: flex-start;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--tool-bg);
  color: var(--tool-text);
  border: 1px solid var(--tool-border);
  font-size: 12px;
  padding: 5px 11px;
  border-radius: 20px;
  font-family: 'SF Mono', 'Fira Code', monospace;
}

.tool-badge.running::after {
  content: '';
  width: 5px; height: 5px;
  background: var(--tool-text);
  border-radius: 50%;
  animation: pulse 1s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.3; transform: scale(0.8); }
}

/* ── Typing dots ── */
.typing-dots {
  align-self: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 12px 16px;
  border-radius: var(--radius);
  border-bottom-left-radius: 5px;
  display: flex;
  gap: 5px;
  align-items: center;
}

.typing-dots span {
  width: 6px; height: 6px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: bounce 1.3s infinite ease-in-out;
}

.typing-dots span:nth-child(2) { animation-delay: 0.18s; }
.typing-dots span:nth-child(3) { animation-delay: 0.36s; }

@keyframes bounce {
  0%, 70%, 100% { transform: translateY(0); }
  35% { transform: translateY(-7px); }
}

/* ── Markdown ── */
.bubble.assistant p { margin-bottom: 8px; }
.bubble.assistant p:last-child { margin-bottom: 0; }

.bubble.assistant code {
  background: var(--surface3);
  border: 1px solid var(--border);
  padding: 2px 6px;
  border-radius: 5px;
  font-size: 13px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  color: #c9e0ff;
}

.bubble.assistant pre {
  background: #090d11;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  overflow-x: auto;
  margin: 8px 0;
}

.bubble.assistant pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: 13px;
  color: inherit;
}

.bubble.assistant ul, .bubble.assistant ol {
  padding-left: 20px;
  margin: 6px 0;
}

.bubble.assistant li { margin-bottom: 3px; }

.bubble.assistant h1, .bubble.assistant h2, .bubble.assistant h3 {
  margin: 14px 0 6px;
  font-weight: 700;
  color: var(--text);
}

.bubble.assistant h1 { font-size: 18px; }
.bubble.assistant h2 { font-size: 16px; }
.bubble.assistant h3 { font-size: 15px; color: var(--accent); }

.bubble.assistant blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 12px;
  color: var(--text-muted);
  margin: 8px 0;
  font-style: italic;
}

.bubble.assistant hr { border-color: var(--border); margin: 12px 0; }
.bubble.assistant a { color: var(--accent); text-decoration: none; }
.bubble.assistant a:hover { text-decoration: underline; }

strong { color: var(--text); font-weight: 600; }

/* ── Input bar ── */
#input-bar {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

#msg-input {
  flex: 1;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 10px 16px;
  color: var(--text);
  font-size: 15px;
  resize: none;
  outline: none;
  max-height: 120px;
  overflow-y: auto;
  line-height: 1.45;
  font-family: inherit;
  transition: border-color 0.2s;
}

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

#send-btn {
  width: 40px; height: 40px;
  background: var(--accent);
  color: #0f0f13;
  border: none;
  border-radius: 50%;
  font-size: 18px;
  font-weight: 800;
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 12px var(--accent-glow);
  transition: opacity 0.15s, transform 0.1s;
}

#send-btn:not(:disabled):active {
  opacity: 0.85;
  transform: scale(0.93);
}

#send-btn:disabled {
  opacity: 0.35;
  cursor: default;
  box-shadow: none;
}

/* ── Permission card ── */
.permission-card {
  align-self: flex-start;
  max-width: 90%;
  background: var(--surface);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  border-bottom-left-radius: 5px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.perm-header {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--accent);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.perm-tool {
  font-size: 13px;
  color: var(--text);
  font-weight: 600;
}

.perm-input {
  font-size: 12px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  color: var(--text-muted);
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  word-break: break-all;
  white-space: pre-wrap;
}

.perm-buttons {
  display: flex;
  gap: 8px;
  margin-top: 4px;
}

.perm-btn {
  flex: 1;
  padding: 8px;
  border-radius: 8px;
  border: none;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s;
}

.perm-approve { background: #1e3a22; color: #5dba6e; border: 1px solid #2a5230; }
.perm-deny    { background: #3a1e1e; color: #ff6b6b; border: 1px solid #5a2a2a; }
.perm-btn:active { opacity: 0.75; }

.perm-result { font-size: 13px; font-weight: 600; }
.perm-result-yes { color: #5dba6e; }
.perm-result-no  { color: #ff6b6b; }

/* ── Scrollbar ── */
#messages::-webkit-scrollbar { width: 3px; }
#messages::-webkit-scrollbar-track { background: transparent; }
#messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
