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

:root {
  --bg-primary: #212121;
  --bg-secondary: #171717;
  --bg-input: #2f2f2f;
  --bg-user-msg: #2f2f2f;
  --text-primary: #ececec;
  --text-secondary: #9b9b9b;
  --accent: #10a37f;
  --accent-hover: #0d8a6b;
  --error: #ef4444;
  --success: #10a37f;
  --border: #424242;
}

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

.screen {
  height: 100%;
}

/* ========== Marketplace ========== */

.marketplace {
  max-width: 800px;
  margin: 0 auto;
  padding: 40px 20px;
  min-height: 100%;
}

.marketplace-header {
  text-align: center;
  margin-bottom: 40px;
}

.marketplace-header h1 {
  font-size: 32px;
  font-weight: 700;
  margin-bottom: 8px;
}

.marketplace-subtitle {
  color: var(--text-secondary);
  font-size: 16px;
}

.search-wrapper {
  position: relative;
  max-width: 400px;
  margin: 24px auto 0;
}

.search-icon {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-secondary);
  pointer-events: none;
}

#search-input {
  width: 100%;
  padding: 12px 16px 12px 42px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text-primary);
  font-size: 15px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.2s;
}

#search-input:focus {
  border-color: var(--accent);
}

#search-input::placeholder {
  color: var(--text-secondary);
}

.agents-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 16px;
}

.agent-card {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 20px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 12px;
  text-decoration: none;
  color: var(--text-primary);
  transition: border-color 0.2s, background 0.2s;
  cursor: pointer;
}

.agent-card:hover {
  border-color: var(--accent);
  background: var(--bg-input);
}

.agent-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: 700;
  flex-shrink: 0;
  overflow: hidden;
}

.agent-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.agent-info h2 {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 4px;
}

.agent-info p {
  font-size: 14px;
  color: var(--text-secondary);
  line-height: 1.4;
}

.loading {
  text-align: center;
  color: var(--text-secondary);
  padding: 40px;
  grid-column: 1 / -1;
}

/* ========== Auth Screen ========== */

.auth-container {
  max-width: 360px;
  margin: 0 auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100%;
}

.auth-container h1 {
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 8px;
}

.auth-subtitle {
  color: var(--text-secondary);
  margin-bottom: 32px;
  font-size: 14px;
}

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

#auth-form input {
  width: 100%;
  padding: 12px 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text-primary);
  font-size: 15px;
  outline: none;
  transition: border-color 0.2s;
}

#auth-form input:focus {
  border-color: var(--accent);
}

#auth-form button {
  width: 100%;
  padding: 12px;
  border-radius: 8px;
  border: none;
  background: var(--accent);
  color: white;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

#auth-form button:hover {
  background: var(--accent-hover);
}

#auth-form button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

#google-btn {
  width: 100%;
  padding: 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: white;
  color: #3c4043;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  transition: background 0.2s;
}

#google-btn:hover {
  background: #f7f7f7;
}

.auth-divider {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 16px 0;
  color: var(--text-secondary);
  font-size: 13px;
}

.auth-divider::before,
.auth-divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border);
}

.auth-toggle {
  margin-top: 16px;
  font-size: 14px;
  color: var(--text-secondary);
}

.auth-toggle a {
  color: var(--accent);
  text-decoration: none;
  margin-left: 4px;
}

.auth-error {
  margin-top: 12px;
  color: var(--error);
  font-size: 14px;
  text-align: center;
}

.auth-success {
  margin-top: 12px;
  color: var(--success);
  font-size: 14px;
  text-align: center;
}

/* ========== Chat Screen ========== */

#chat-screen {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-secondary);
  flex-shrink: 0;
  gap: 12px;
}

.chat-header h1 {
  font-size: 16px;
  font-weight: 600;
  flex: 1;
}

.back-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  color: var(--text-secondary);
  text-decoration: none;
  transition: color 0.2s, background 0.2s;
  flex-shrink: 0;
}

.back-btn:hover {
  color: var(--text-primary);
  background: var(--bg-input);
}

#logout-btn {
  padding: 6px 14px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-secondary);
  font-size: 13px;
  cursor: pointer;
  transition: color 0.2s, border-color 0.2s;
  flex-shrink: 0;
}

#logout-btn:hover {
  color: var(--text-primary);
  border-color: var(--text-secondary);
}

/* Messages */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.message {
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
  display: flex;
  gap: 12px;
}

.message.user {
  flex-direction: row-reverse;
}

.message-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 600;
  flex-shrink: 0;
  overflow: hidden;
}

.message-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.message.assistant .message-avatar {
  background: var(--accent);
  color: white;
}

.message.user .message-avatar {
  background: #6b5ce7;
  color: white;
}

.message-content {
  padding: 12px 16px;
  border-radius: 12px;
  line-height: 1.6;
  font-size: 15px;
  word-break: break-word;
}

.message.user .message-content {
  white-space: pre-wrap;
  background: var(--bg-user-msg);
  border-top-right-radius: 4px;
}

.message.assistant .message-content {
  background: transparent;
  padding-left: 0;
}

.message-content p {
  margin-bottom: 8px;
}

.message-content p:last-child {
  margin-bottom: 0;
}

.message-content strong {
  font-weight: 600;
}

.message-content ul,
.message-content ol {
  margin: 8px 0;
  padding-left: 20px;
}

.message-content li {
  margin-bottom: 4px;
}

.message-content a {
  color: var(--accent);
  text-decoration: underline;
}

.message-content code {
  background: var(--bg-input);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 0.9em;
}

.message-content pre {
  background: var(--bg-secondary);
  border-radius: 8px;
  padding: 12px;
  overflow-x: auto;
  margin: 8px 0;
}

.message-content pre code {
  background: none;
  padding: 0;
}

/* Rate limit banner */
.rate-limit-banner {
  max-width: 720px;
  width: 100%;
  margin: 8px auto;
  padding: 20px;
  text-align: center;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.rate-limit-banner p {
  color: var(--text-secondary);
  font-size: 14px;
  margin-bottom: 12px;
}

.rate-limit-banner button {
  padding: 10px 24px;
  border-radius: 8px;
  border: none;
  background: var(--accent);
  color: white;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.rate-limit-banner button:hover {
  background: var(--accent-hover);
}

/* Articles/Sources cited */
.articles-cited {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  font-size: 13px;
  color: var(--text-secondary);
}

.articles-cited summary {
  cursor: pointer;
  font-weight: 600;
  margin-bottom: 8px;
}

.articles-cited ul {
  list-style: none;
  padding: 0;
}

.articles-cited li {
  padding: 4px 0;
}

.articles-cited .similarity {
  color: var(--accent);
  font-size: 12px;
  margin-left: 4px;
}

/* Typing indicator */
.typing-indicator {
  display: inline-flex;
  gap: 4px;
  padding: 4px 0;
}

.typing-indicator span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-secondary);
  animation: typing 1.4s infinite;
}

.typing-indicator span:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-indicator span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes typing {
  0%, 60%, 100% { opacity: 0.3; transform: scale(0.8); }
  30% { opacity: 1; transform: scale(1); }
}

/* Input Area */
.input-area {
  padding: 12px 20px 20px;
  background: var(--bg-primary);
  flex-shrink: 0;
}

.input-wrapper {
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg-input);
  border-radius: 12px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  transition: border-color 0.2s;
}

.input-wrapper:focus-within {
  border-color: var(--accent);
}

#question-input {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text-primary);
  font-size: 15px;
  font-family: inherit;
  resize: none;
  outline: none;
  max-height: 200px;
  line-height: 1.5;
  padding: 4px 0;
}

#question-input::placeholder {
  color: var(--text-secondary);
}

#send-btn,
#stop-btn {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: none;
  background: var(--accent);
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.2s;
}

#send-btn:hover,
#stop-btn:hover {
  background: var(--accent-hover);
}

#send-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

#stop-btn {
  background: var(--error);
}

#stop-btn:hover {
  background: #dc2626;
}

/* Welcome state */
.welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  color: var(--text-secondary);
  text-align: center;
  padding: 40px 20px;
}

.welcome h2 {
  font-size: 22px;
  color: var(--text-primary);
  margin-bottom: 8px;
}

.welcome p {
  font-size: 14px;
  max-width: 480px;
}

/* Suggestion chips */
.suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-top: 16px;
}

.suggestion-chip {
  padding: 8px 16px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-primary);
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
}

.suggestion-chip:hover {
  border-color: var(--accent);
  background: var(--bg-input);
}

/* Community suggestion card */
.community-suggestion-card {
  margin-top: 16px;
  padding: 20px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.community-suggestion-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 4px;
}

.community-suggestion-subtitle {
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 12px;
}

.community-suggestion-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.community-suggestion-chip {
  padding: 8px 16px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-primary);
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
}

.community-suggestion-chip:hover {
  border-color: var(--accent);
  background: var(--bg-input);
}

.community-suggestion-login {
  margin-top: 12px;
  font-size: 13px;
  color: var(--text-secondary);
}

.community-suggestion-login p {
  margin-bottom: 8px;
}

.community-suggestion-login-btn {
  padding: 8px 16px;
  border-radius: 8px;
  border: none;
  background: var(--accent);
  color: white;
  font-size: 13px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.2s;
}

.community-suggestion-login-btn:hover {
  background: var(--accent-hover);
}

.community-suggestion-form {
  margin-top: 12px;
}

.community-suggestion-form-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.community-suggestion-form-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--accent);
}

.community-suggestion-close {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.2s, background 0.2s;
}

.community-suggestion-close:hover {
  color: var(--text-primary);
  background: var(--bg-input);
}

.community-suggestion-textarea {
  width: 100%;
  padding: 12px 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text-primary);
  font-size: 14px;
  font-family: inherit;
  resize: vertical;
  outline: none;
  transition: border-color 0.2s;
}

.community-suggestion-textarea:focus {
  border-color: var(--accent);
}

.community-suggestion-textarea::placeholder {
  color: var(--text-secondary);
}

.community-suggestion-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 8px;
}

.community-suggestion-counter {
  font-size: 12px;
  color: var(--text-secondary);
}

.community-suggestion-submit {
  padding: 8px 20px;
  border-radius: 8px;
  border: none;
  background: var(--accent);
  color: white;
  font-size: 13px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.2s;
}

.community-suggestion-submit:hover {
  background: var(--accent-hover);
}

.community-suggestion-submit:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.community-suggestion-legal {
  margin-top: 8px;
  font-size: 12px;
  color: var(--text-secondary);
}

.community-suggestion-legal a {
  color: var(--accent);
  text-decoration: none;
}

.community-suggestion-legal a:hover {
  text-decoration: underline;
}

.community-suggestion-error {
  margin-top: 8px;
  font-size: 13px;
  color: var(--error);
}

.community-suggestion-success {
  font-size: 14px;
  color: var(--success);
  font-weight: 500;
}

/* Scrollbar */
.messages::-webkit-scrollbar {
  width: 6px;
}

.messages::-webkit-scrollbar-track {
  background: transparent;
}

.messages::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

/* Responsive */
@media (max-width: 600px) {
  .marketplace {
    padding: 24px 16px;
  }

  .marketplace-header h1 {
    font-size: 24px;
  }

  .agents-grid {
    grid-template-columns: 1fr;
  }

  .messages {
    padding: 12px;
  }

  .input-area {
    padding: 8px 12px 12px;
  }

  .message-content {
    font-size: 14px;
  }

  .community-suggestion-chips {
    flex-direction: column;
  }
}
