/* ─── 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;
  position: relative;
}

.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;
  overflow: hidden;
}

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

.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--reminder {
  background: #f0f7ff;
  border-left: 3px solid #3b82f6;
}

.message__bubble p { margin: 0.5em 0; }
.message__bubble p:first-child { margin-top: 0; }
.message__bubble p:last-child { margin-bottom: 0; }

/* Bullet & ordered list indentation (#311) */
.message__bubble ul,
.message__bubble ol {
  margin: 0.6em 0 0.6em 0;
  padding-left: 1.5em;
  list-style-position: outside;
}
.message__bubble ul { list-style-type: disc; }
.message__bubble ol { list-style-type: decimal; }
.message__bubble li {
  margin: 0.3em 0;
  padding-left: 0.25em;
  line-height: 1.5;
}
.message__bubble ul ul,
.message__bubble ol ol,
.message__bubble ul ol,
.message__bubble ol ul {
  margin: 0.2em 0;
  padding-left: 1.25em;
}
/* Add a visual gap before list when preceded by a paragraph */
.message__bubble p + ul,
.message__bubble p + ol {
  margin-top: 0.4em;
}

.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;
  overflow-y: hidden;
  border: 1px solid #d1d5db;
  border-radius: var(--radius);
  padding: 0.625rem 1rem;
  font-family: inherit;
  font-size: 1rem;
  line-height: 1.5;
  min-height: 44px;
  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;
}

/* ─── TTS toggle button ─── */
#tts-btn {
  background: var(--color-bg);
  color: var(--color-primary);
  border-radius: 50%;
  padding: 0;
  width: 2.25rem;
  height: 2.25rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s, opacity 0.15s;
}

#tts-btn:hover {
  background: var(--color-primary-light);
}

#tts-btn.tts-muted {
  color: var(--color-text-muted);
  opacity: 0.55;
}

#tts-btn.tts-speaking {
  color: var(--color-primary);
  animation: tts-pulse 1.4s ease-in-out infinite;
}

@keyframes tts-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

/* ─── 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; } }
@keyframes highlight-flash { 0% { background: #fef08a; } 100% { background: transparent; } }

/* 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;
}

/* ─── Search bar ─── */
.search-bar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0 var(--spacing);
  background: var(--color-surface);
  border-bottom: 1px solid #e5e7eb;
  box-shadow: 0 1px 4px rgba(0,0,0,0.06);
  max-height: 60px;
  overflow: hidden;
  transition: max-height 0.2s ease, padding 0.2s ease;
}

.search-bar--hidden {
  max-height: 0;
  padding-top: 0;
  padding-bottom: 0;
  border-bottom: none;
}

.search-bar:not(.search-bar--hidden) {
  max-height: 60px;
  padding-top: 0.5rem;
  padding-bottom: 0.5rem;
}

#search-input {
  flex: 1;
  border: 1px solid #d1d5db;
  border-radius: var(--radius);
  padding: 0.5rem 0.875rem;
  font-family: inherit;
  font-size: 0.9375rem;
  outline: none;
  transition: border-color 0.15s;
  min-width: 0;
}
#search-input:focus { border-color: var(--color-primary); }

.search-count {
  font-size: 0.8125rem;
  color: var(--color-text-muted);
  min-width: 3rem;
  text-align: center;
  flex-shrink: 0;
}

.search-bar button {
  background: none;
  border: 1px solid #e5e7eb;
  border-radius: 0.375rem;
  padding: 0.25rem 0.5rem;
  cursor: pointer;
  font-size: 0.875rem;
  color: var(--color-text-muted);
  transition: background 0.15s, color 0.15s;
  line-height: 1;
}
.search-bar button:hover {
  background: var(--color-primary-light);
  color: var(--color-primary);
  border-color: var(--color-primary);
}

#search-close {
  border: none;
  font-size: 1rem;
}

/* ─── Floating search nav (Telegram-style, mobile only) ─── */
.search-float-nav {
  position: absolute;
  right: 0.75rem;
  bottom: calc(var(--chat-input-height, 72px) + 1rem);
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
  z-index: 50;
  transition: opacity 0.15s;
}

.search-float-nav--hidden {
  opacity: 0;
  pointer-events: none;
}

.search-float-nav button {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1.5px solid var(--color-primary);
  background: #fff;
  color: var(--color-primary);
  font-size: 1rem;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 12px rgba(0,0,0,0.28);
  transition: background 0.15s, color 0.15s, transform 0.1s;
}
.search-float-nav button:hover {
  background: var(--color-primary);
  color: #fff;
  transform: scale(1.08);
}
.search-float-nav button:disabled {
  background: #f3f4f6;
  border-color: #d1d5db;
  color: #9ca3af;
  cursor: default;
  transform: none;
}

/* ─── Desktop: results panel (floating below search bar) ─── */
.search-results {
  position: absolute;
  top: 60px;
  left: 0;
  right: 0;
  z-index: 100;
  background: var(--color-surface);
  box-shadow: 0 4px 16px rgba(0,0,0,0.12);
  max-height: 320px;
  overflow-y: auto;
  border-bottom-left-radius: var(--radius);
  border-bottom-right-radius: var(--radius);
}
.search-results--hidden { display: none; }

.search-result-card {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  padding: 0.625rem 1rem;
  border-bottom: 1px solid #f3f4f6;
  cursor: pointer;
  transition: background 0.12s;
  flex-wrap: wrap;
}
.search-result-card:last-child { border-bottom: none; }
.search-result-card:hover,
.search-result-card--active { background: var(--color-primary-light); }

.search-result-card .snippet {
  flex: 1;
  font-size: 0.875rem;
  color: var(--color-text);
  line-height: 1.4;
  min-width: 0;
  word-break: break-word;
}
.search-result-card mark {
  background: #fef08a;
  border-radius: 2px;
  padding: 0 2px;
  color: inherit;
}

.role-badge {
  display: inline-block;
  font-size: 0.6875rem;
  font-weight: 600;
  padding: 0.125rem 0.375rem;
  border-radius: 999px;
  white-space: nowrap;
  flex-shrink: 0;
}
.role-badge--user { background: #dbeafe; color: #1d4ed8; }
.role-badge--assistant { background: #ede9fe; color: #4f46e5; }

.search-result-card .date {
  font-size: 0.75rem;
  color: var(--color-text-muted);
  white-space: nowrap;
  flex-shrink: 0;
  margin-left: auto;
}
.search-empty {
  padding: 1rem;
  text-align: center;
  color: var(--color-text-muted);
  font-size: 0.875rem;
}

/* ─── Desktop: hide float nav, show results panel ─── */
@media (min-width: 769px) {
  .search-float-nav { display: none; }
}

/* ─── Mobile: hide results panel, show float nav ─── */
@media (max-width: 768px) {
  .search-results { display: none !important; }
}

/* ─── Search match highlight (mobile inline) ─── */
.message--search-match mark {
  background: #fef08a;
  border-radius: 2px;
  padding: 0 1px;
  color: inherit;
}

.message--search-current {
  outline: 2px solid var(--color-primary);
  outline-offset: 4px;
  border-radius: var(--radius-sm);
}

/* ─── Message highlight on jump ─── */
.message--highlight {
  border-radius: var(--radius-sm);
  animation: highlight-flash 2s ease-out forwards;
}

/* ─── Search toggle button (floats in input bar) ─── */
#search-toggle {
  background: var(--color-bg);
  color: var(--color-primary);
  font-size: 1.125rem;
  line-height: 1;
}
#search-toggle:hover {
  background: var(--color-primary-light);
}

/* ─── Responsive ─── */
@media (max-width: 768px) {
  .chat-container {
    max-width: 100%;
    height: calc(100dvh - var(--shell-bottom-nav-height, 60px));
  }
  .message { max-width: 90%; }
  .chat-input { padding: 0.75rem; }
  #mic-btn { display: none; }
}

/* File-only messages (no avatar) — indent to align with normal bubbles */
.message--no-avatar {
  padding-left: calc(var(--avatar-size) + var(--gap, 0.5rem));
}

/* ─── File download link ─── */
a[data-file-download] {
  display: inline-block;
  padding: 0.4rem 1rem;
  background: var(--color-primary);
  color: #ffffff;
  border-radius: 999px;
  text-decoration: none;
  font-size: 0.875rem;
  font-weight: 600;
  margin: 0.25rem 0;
  transition: opacity 0.15s;
}
a[data-file-download]:hover {
  opacity: 0.85;
  color: #ffffff;
}
