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

:root {
  --bg: #0f0f0f;
  --surface: #1a1a1a;
  --border: #2a2a2a;
  --accent: #6366f1;
  --accent-dim: #4f46e5;
  --text: #e8e8e8;
  --text-dim: #888;
  --agent-bubble: #1e1e1e;
  --radius: 12px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.screen { display: flex; flex-direction: column; height: 100dvh; }
.hidden { display: none !important; }

/* PIN screen */
#pin-screen { align-items: center; justify-content: center; }
.pin-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2.5rem 2rem;
  text-align: center;
  width: min(360px, 90vw);
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.logo { font-size: 1.8rem; font-weight: 700; color: var(--accent); letter-spacing: -0.5px; }
.pin-box p { color: var(--text-dim); font-size: 0.9rem; }
.pin-box input {
  width: 100%;
  padding: 0.75rem 1rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 1rem;
  text-align: center;
  letter-spacing: 0.3em;
  outline: none;
}
.pin-box input:focus { border-color: var(--accent); }
.pin-box button {
  padding: 0.75rem;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}
.pin-box button:hover { background: var(--accent-dim); }
.error { color: #f87171; font-size: 0.85rem; }

/* Chat screen */
header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.logo-small { font-weight: 700; color: var(--accent); font-size: 1rem; }
#agent-name-header { color: var(--text-dim); font-size: 0.85rem; }
.credits-badge {
  margin-left: auto;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--text-dim);
  background: rgba(255,255,255,0.05);
  border: 1px solid var(--border);
  border-radius: 99px;
  padding: 0.15rem 0.55rem;
  white-space: nowrap;
}
.credits-badge.credits-low   { color: #f59e0b; border-color: #f59e0b44; }
.credits-badge.credits-empty { color: #f87171; border-color: #f8717144; }
.status-dot { color: #4ade80; font-size: 0.7rem; }
.header-action-btn, #new-chat-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-dim);
  font-size: 0.78rem;
  font-weight: 600;
  line-height: 1;
  height: 2rem;
  padding: 0 0.6rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  white-space: nowrap;
  transition: border-color 0.15s, color 0.15s;
}
#new-chat-btn { font-size: 1.1rem; width: 2rem; padding: 0; }
.header-action-btn:hover, #new-chat-btn:hover { border-color: var(--accent); color: var(--accent); }

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  scroll-behavior: smooth;
}

.message {
  max-width: 75%;
  padding: 0.65rem 1rem;
  border-radius: var(--radius);
  line-height: 1.5;
  font-size: 0.95rem;
  white-space: pre-wrap;
  word-break: break-word;
}
.message.user {
  background: var(--accent);
  color: #fff;
  align-self: flex-end;
  border-bottom-right-radius: 3px;
}
.message.agent {
  background: var(--agent-bubble);
  border: 1px solid var(--border);
  align-self: flex-start;
  border-bottom-left-radius: 3px;
}
.message.thinking {
  color: var(--text-dim);
  font-style: italic;
  font-size: 0.85rem;
  background: transparent;
  border: none;
  padding-left: 0.25rem;
  align-self: flex-start;
  min-height: 1.5rem;
  display: flex;
  align-items: center;
}

/* Animated typing dots */
.typing-dots {
  display: inline-flex;
  gap: 5px;
  align-items: center;
  padding: 0.15rem 0;
}
.typing-dots span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-dim);
  animation: typingBounce 1.3s ease-in-out infinite;
}
.typing-dots span:nth-child(2) { animation-delay: 0.15s; }
.typing-dots span:nth-child(3) { animation-delay: 0.3s; }
@keyframes typingBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-6px); opacity: 1; }
}
@keyframes blink {
  0%, 100% { opacity: 0.6; }
  50% { opacity: 0; }
}

/* Markdown content inside agent bubbles */
.message.agent .md, .message.supervisor .md { line-height: 1.6; }
.message.agent .md p, .message.supervisor .md p { margin-bottom: 0.55em; }
.message.agent .md p:last-child, .message.supervisor .md p:last-child { margin-bottom: 0; }
.message.agent .md h1, .message.agent .md h2, .message.agent .md h3 {
  font-weight: 600; margin: 0.6em 0 0.3em; line-height: 1.3;
}
.message.agent .md h1 { font-size: 1.1em; }
.message.agent .md h2 { font-size: 1.0em; }
.message.agent .md h3 { font-size: 0.95em; }
.message.agent .md ul, .message.agent .md ol { padding-left: 1.35em; margin-bottom: 0.5em; }
.message.agent .md li { margin-bottom: 0.2em; }
.message.agent .md code {
  background: rgba(255,255,255,0.08);
  padding: 0.1em 0.35em;
  border-radius: 4px;
  font-family: 'Menlo', 'Monaco', monospace;
  font-size: 0.85em;
}
.message.agent .md pre {
  background: rgba(0,0,0,0.35);
  border-radius: 8px;
  padding: 0.75em 1em;
  overflow-x: auto;
  margin: 0.5em 0;
  border: 1px solid rgba(255,255,255,0.06);
}
.message.agent .md pre code { background: none; padding: 0; font-size: 0.82em; }
.message.agent .md a { color: #a5b4fc; text-decoration: underline; }
.message.agent .md strong { font-weight: 600; }
.message.agent .md em { font-style: italic; }
.message.agent .md blockquote {
  border-left: 2px solid var(--border);
  padding-left: 0.8em;
  margin: 0.4em 0;
  color: var(--text-dim);
  font-style: italic;
}
.message.agent .md hr { border: none; border-top: 1px solid var(--border); margin: 0.75em 0; }
.message.agent .md table { border-collapse: collapse; width: 100%; margin: 0.5em 0; font-size: 0.88em; }
.message.agent .md th, .message.agent .md td { border: 1px solid var(--border); padding: 0.35em 0.6em; text-align: left; }
.message.agent .md th { background: rgba(255,255,255,0.05); font-weight: 600; }

#input-area {
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
#user-input {
  flex: 1;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 0.95rem;
  padding: 0.6rem 0.9rem;
  resize: none;
  outline: none;
  max-height: 8rem;
  overflow-y: auto;
  line-height: 1.5;
  font-family: inherit;
}
#user-input:focus { border-color: var(--accent); }
#send-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 0 1rem;
  font-size: 1rem;
  cursor: pointer;
  align-self: flex-end;
  height: 2.4rem;
  transition: background 0.15s;
  flex-shrink: 0;
}
#send-btn:hover { background: var(--accent-dim); }
#send-btn:disabled { opacity: 0.5; cursor: not-allowed; }

.message.supervisor {
  background: #2d1b5e;
  border: 1px solid #7c3aed;
  color: #e0d4ff;
  align-self: flex-start;
  border-bottom-left-radius: 3px;
}
.msg-source-label {
  font-size: 0.68rem;
  color: #a78bfa;
  padding: 0 0.2rem;
  margin-bottom: 0.1rem;
  align-self: flex-start;
}

/* Attach button */
#attach-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-dim);
  font-size: 1.1rem;
  line-height: 1;
  width: 2.4rem;
  height: 2.4rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  align-self: flex-end;
  transition: border-color 0.15s, color 0.15s;
}
#attach-btn:hover { border-color: var(--accent); color: var(--accent); }
#attach-btn.has-image { border-color: var(--accent); color: var(--accent); background: rgba(99,102,241,0.1); }

/* Image preview strip above input */
#image-preview {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
#image-preview img {
  width: 4rem;
  height: 4rem;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border);
}
#image-preview-remove {
  background: rgba(0,0,0,0.6);
  border: none;
  color: #fff;
  border-radius: 50%;
  width: 1.4rem;
  height: 1.4rem;
  font-size: 0.7rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

/* Images inside chat bubbles */
.message img.chat-image {
  display: block;
  max-width: 100%;
  max-height: 14rem;
  border-radius: 8px;
  margin-bottom: 0.35rem;
  object-fit: contain;
}

@media (max-width: 480px) { .message { max-width: 90%; } }
