/* ─── Custom properties ─── */
:root {
  --color-bg: #f5f5f5;
  --color-surface: #ffffff;
  --color-primary: #4f46e5;
  --color-primary-light: #eef2ff;
  --color-text: #1f2937;
  --color-text-muted: #6b7280;
  --color-user-bubble: var(--color-primary);
  --color-user-text: #ffffff;
  --color-sherpa-bubble: var(--color-surface);
  --color-sherpa-text: var(--color-text);
  --color-error: #ef4444;
  --color-error-bg: #fef2f2;
  --radius: 1rem;
  --radius-sm: 0.5rem;
  --spacing: 1rem;
  --avatar-size: 40px;
}

/* ─── Reset ─── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

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

/* ─── Layout ─── */
.chat-container {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 800px;
  margin: 0 auto;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: var(--spacing);
  background: var(--color-surface);
  border-bottom: 1px solid #e5e7eb;
}

.chat-header__brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.chat-header__avatar {
  font-size: 1.5rem;
  width: var(--avatar-size);
  height: var(--avatar-size);
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-primary-light);
  border-radius: 50%;
}

.chat-header__title {
  font-size: 1.125rem;
  font-weight: 600;
}

/* ─── Header nav ─── */
.chat-header__nav {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.chat-nav-link {
  font-size: 0.8125rem;
  font-weight: 500;
  color: #6b7280;
  text-decoration: none;
  padding: 0.375rem 0.625rem;
  border-radius: 0.375rem;
  border: none;
  background: none;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  white-space: nowrap;
}

.chat-nav-link:hover {
  background: #f3f4f6;
  color: #111827;
}

.chat-nav-link--signout {
  color: #9ca3af;
}

.chat-nav-link--signout:hover {
  background: #fee2e2;
  color: #dc2626;
}

/* ─── Offline banner ─── */
.offline-banner {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.5rem var(--spacing);
  background: #fef3c7;
  color: #92400e;
  font-size: 0.875rem;
  font-weight: 500;
}
/* ─── Error banner ─── */
.error-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem var(--spacing);
  background: var(--color-error-bg);
  color: var(--color-error);
  font-size: 0.875rem;
}
.error-banner button {
  background: none;
  border: none;
  color: var(--color-error);
  font-size: 1.25rem;
  cursor: pointer;
}

/* ─── Message list ─── */
.message-list {
  flex: 1;
  overflow-y: auto;
  padding: var(--spacing);
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  scroll-behavior: smooth;
}

/* ─── Message bubbles ─── */
.message {
  display: flex;
  gap: 0.5rem;
  max-width: 80%;
  animation: fadeIn 0.2s ease;
}

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

.message--sherpa {
  align-self: flex-start;
}

.message__avatar {
  width: var(--avatar-size);
  height: var(--avatar-size);
  border-radius: 50%;
  background: var(--color-primary-light);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
  flex-shrink: 0;
}

.avatar--thinking {
  animation: pulse 1.5s ease-in-out infinite;
}

.message__bubble {
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  line-height: 1.5;
  word-break: break-word;
}

.message--user .message__bubble {
  background: var(--color-user-bubble);
  color: var(--color-user-text);
  border-bottom-right-radius: var(--radius-sm);
}

.message--sherpa .message__bubble {
  background: var(--color-sherpa-bubble);
  color: var(--color-sherpa-text);
  border-bottom-left-radius: var(--radius-sm);
  box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}

.message__bubble p { margin: 0.5em 0; }
.message__bubble p:first-child { margin-top: 0; }
.message__bubble p:last-child { margin-bottom: 0; }
.message__bubble code {
  background: rgba(0,0,0,0.06);
  padding: 0.15em 0.35em;
  border-radius: 3px;
  font-size: 0.9em;
}
.message__bubble pre {
  background: #1e1e1e;
  color: #d4d4d4;
  padding: 0.75rem;
  border-radius: var(--radius-sm);
  overflow-x: auto;
  margin: 0.5em 0;
}
.message__bubble pre code {
  background: none;
  padding: 0;
  color: inherit;
}

/* ─── Typing indicator ─── */
.typing-indicator span {
  display: inline-block;
  width: 8px;
  height: 8px;
  background: var(--color-text-muted);
  border-radius: 50%;
  margin: 0 2px;
  animation: bounce 1.4s ease-in-out infinite;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

/* ─── New messages indicator ─── */
.new-messages-indicator {
  text-align: center;
  padding: 0.25rem;
}
.new-messages-indicator button {
  background: var(--color-primary);
  color: white;
  border: none;
  border-radius: 2rem;
  padding: 0.375rem 1rem;
  font-size: 0.8125rem;
  cursor: pointer;
}

/* ─── Input area ─── */
.chat-input {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  padding: var(--spacing);
  background: var(--color-surface);
  border-top: 1px solid #e5e7eb;
}

.chat-input textarea {
  flex: 1;
  resize: none;
  border: 1px solid #d1d5db;
  border-radius: var(--radius);
  padding: 0.625rem 1rem;
  font-family: inherit;
  font-size: 1rem;
  line-height: 1.5;
  max-height: 120px;
  outline: none;
  transition: border-color 0.15s;
}
.chat-input textarea:focus { border-color: var(--color-primary); }

.chat-input button {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
#send-btn {
  background: var(--color-primary);
  color: white;
}
#send-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
#mic-btn {
  background: var(--color-bg);
  font-size: 1.125rem;
  opacity: 0.5;
  cursor: not-allowed;
}

/* ─── Animations ─── */
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.5; } }
@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); }
  40% { transform: translateY(-6px); }
}
@keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: 0; } }

/* Blinking cursor while tokens are streaming in */
.message__bubble.streaming::after {
  content: '▋';
  display: inline-block;
  margin-left: 1px;
  animation: blink 0.7s step-start infinite;
  color: var(--color-primary, #6366f1);
  font-size: 0.9em;
  vertical-align: baseline;
}

/* ─── Responsive ─── */
@media (max-width: 768px) {
  .chat-container { max-width: 100%; }
  .message { max-width: 90%; }
  .chat-input { padding: 0.75rem; }
}
