/* ==========================================================================
   Alzheimer Chat — Clean, modern chat interface
   ========================================================================== */

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: #f5f5f5;
  height: 100vh;
  display: flex;
  overflow: hidden;
}

/* ==========================================================================
   Sidebar
   ========================================================================== */

.sidebar {
  width: 280px;
  background: #1a1a2e;
  color: #e0e0e0;
  display: flex;
  flex-direction: column;
  transition: transform 0.3s ease;
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  z-index: 200;
  transform: translateX(-100%);
}

.sidebar.open { transform: translateX(0); }
.sidebar.hidden { display: none; }

.sidebar-header {
  padding: 16px;
  border-bottom: 1px solid #2a2a4a;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.sidebar-header h3 {
  font-size: 15px;
  font-weight: 600;
}

#newChatBtn {
  background: #10b981;
  color: white;
  border: none;
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 13px;
  cursor: pointer;
  font-weight: 500;
}

#newChatBtn:hover { background: #059669; }

.conv-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.conv-item {
  display: flex;
  align-items: center;
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  margin-bottom: 2px;
  transition: background 0.2s;
}

.conv-item:hover { background: #2a2a4a; }
.conv-item.active { background: #16213e; }

.conv-title {
  flex: 1;
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-delete {
  background: none;
  border: none;
  color: #888;
  font-size: 18px;
  cursor: pointer;
  padding: 0 4px;
  opacity: 0;
  transition: opacity 0.2s;
}

.conv-item:hover .conv-delete { opacity: 1; }
.conv-delete:hover { color: #ef4444; }

/* Sidebar overlay */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 199;
}

.sidebar.open ~ .sidebar-overlay { display: block; }

/* ==========================================================================
   Main layout
   ========================================================================== */

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100%;
}

/* ==========================================================================
   Header
   ========================================================================== */

.header {
  background: white;
  padding: 10px 16px;
  border-bottom: 1px solid #e0e0e0;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 52px;
}

.header-btn {
  background: none;
  border: none;
  padding: 6px;
  cursor: pointer;
  color: #666;
  font-size: 18px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  transition: background 0.2s;
}

.header-btn:hover { background: #f0f0f0; }

.header-title {
  font-size: 16px;
  font-weight: 600;
  color: #1a1a1a;
  flex: 1;
  text-align: center;
}

#langSelect {
  padding: 4px 8px;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  font-size: 13px;
  background: white;
  color: #333;
  cursor: pointer;
}

#authBtn {
  background: #10b981;
  color: white;
  border: none;
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 13px;
  cursor: pointer;
  font-weight: 500;
  white-space: nowrap;
}

#authBtn:hover { background: #059669; }

/* ==========================================================================
   Chat area
   ========================================================================== */

.chat-area {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  padding-bottom: 90px;
  background: #f5f5f5;
  -webkit-overflow-scrolling: touch;
}

/* Empty state */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  text-align: center;
  padding: 20px;
}

.empty-icon { font-size: 64px; margin-bottom: 16px; }
.empty-title { font-size: 24px; font-weight: 600; color: #1a1a1a; margin-bottom: 8px; }
.empty-text { font-size: 16px; color: #666; max-width: 400px; line-height: 1.5; }

/* ==========================================================================
   Messages
   ========================================================================== */

.message {
  margin-bottom: 16px;
  display: flex;
  animation: slideIn 0.3s ease-out;
}

@keyframes slideIn {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: translateY(0); }
}

.message.user { justify-content: flex-end; }
.message.bot  { justify-content: flex-start; }

.message-bubble {
  padding: 12px 16px;
  border-radius: 18px;
  word-wrap: break-word;
  line-height: 1.5;
  font-size: 15px;
  position: relative;
}

/* Keep pre-wrap for streaming (raw text) and user messages */
.message.user .message-bubble { white-space: pre-wrap; }

.message.user .message-bubble {
  max-width: 80%;
  background: #0b93f6;
  color: white;
  border-bottom-right-radius: 4px;
}

.message.bot .message-bubble {
  width: 100%;
  max-width: 800px;
  background: white;
  color: #1a1a1a;
  border-radius: 12px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.1);
}

/* Markdown inside bot bubbles */
.message.bot .message-bubble p {
  margin: 0 0 10px 0;
}

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

.message.bot .message-bubble strong { font-weight: 600; }

.message.bot .message-bubble .md-h2 {
  font-size: 16px;
  font-weight: 700;
  margin: 14px 0 6px;
}

.message.bot .message-bubble .md-h3 {
  font-size: 15px;
  font-weight: 600;
  margin: 12px 0 4px;
}

.message.bot .message-bubble .md-list-item {
  padding: 2px 0 2px 8px;
}

.message.bot .message-bubble .md-list-num {
  font-weight: 600;
  color: #0b93f6;
}

.message.bot .message-bubble .md-list-bullet {
  color: #0b93f6;
}

/* ==========================================================================
   Sources
   ========================================================================== */

.sources {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid #e0e0e0;
  font-size: 13px;
}

.sources-toggle {
  background: none;
  border: none;
  color: #0b93f6;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  padding: 4px 0;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.sources-toggle-icon {
  font-size: 12px;
  transition: transform 0.2s;
  display: inline-block;
}

.sources-toggle.expanded .sources-toggle-icon { transform: rotate(90deg); }

.sources-list {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease-out;
}

.sources-list.expanded {
  max-height: 500px;
  margin-top: 8px;
}

.source-item {
  color: #888;
  margin: 4px 0;
  font-size: 12px;
}

/* ==========================================================================
   TTS button
   ========================================================================== */

.tts-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  background: none;
  border: none;
  font-size: 16px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.2s;
  padding: 2px;
}

.message-bubble:hover .tts-btn { opacity: 0.7; }
.tts-btn:hover { opacity: 1 !important; }

/* ==========================================================================
   Typing indicator
   ========================================================================== */

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

.typing-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #999;
  animation: typing 1.4s infinite;
}

.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.7; }
  30%           { transform: translateY(-10px); opacity: 1; }
}

/* ==========================================================================
   Input area
   ========================================================================== */

.input-area {
  background: white;
  border-top: 1px solid #e0e0e0;
  padding: 12px 16px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
  display: flex;
  align-items: center;
  gap: 10px;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 100;
}

.input-wrapper {
  flex: 1;
  display: flex;
  align-items: center;
  background: #f5f5f5;
  border-radius: 24px;
  padding: 6px 16px;
}

#userInput {
  flex: 1;
  border: none;
  background: none;
  font-size: 15px;
  outline: none;
  padding: 6px 0;
  color: #1a1a1a;
  font-family: inherit;
}

#userInput::placeholder { color: #999; }

#micBtn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: none;
  border: 1px solid #e0e0e0;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  flex-shrink: 0;
}

#micBtn:hover { background: #f0f0f0; }

#micBtn.active {
  background: #ef4444;
  border-color: #ef4444;
  color: white;
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.4); }
  50%      { box-shadow: 0 0 0 8px rgba(239, 68, 68, 0); }
}

#sendBtn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: #10b981;
  color: white;
  border: none;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all 0.2s;
}

#sendBtn:hover { background: #059669; }
#sendBtn:active { transform: scale(0.95); }
#sendBtn:disabled { background: #d1d5db; cursor: not-allowed; }

/* ==========================================================================
   Modals
   ========================================================================== */

.modal {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 1000;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal.show { display: flex; }

.modal-content {
  background: white;
  border-radius: 12px;
  padding: 24px;
  max-width: 400px;
  width: 100%;
}

.modal-title { font-size: 18px; font-weight: 600; color: #1a1a1a; margin-bottom: 8px; }
.modal-text  { font-size: 14px; color: #666; margin-bottom: 20px; }

.modal-buttons {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
}

.modal-btn {
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  border: none;
  transition: all 0.2s;
}

.modal-btn-cancel  { background: #f0f0f0; color: #666; }
.modal-btn-confirm { background: #ef4444; color: white; }
.modal-btn:hover   { filter: brightness(0.95); }

/* Auth modal */
.auth-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.auth-form input {
  padding: 10px 14px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  font-size: 14px;
  outline: none;
  font-family: inherit;
}

.auth-form input:focus { border-color: #10b981; }

.auth-submit {
  background: #10b981;
  color: white;
  border: none;
  border-radius: 8px;
  padding: 10px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.auth-submit:hover { background: #059669; }

.auth-switch {
  text-align: center;
  font-size: 13px;
  color: #666;
  margin-top: 4px;
}

.auth-switch a {
  color: #0b93f6;
  cursor: pointer;
  text-decoration: none;
  font-weight: 500;
}

.auth-error {
  color: #ef4444;
  font-size: 13px;
  min-height: 18px;
}

/* ==========================================================================
   Responsive
   ========================================================================== */

@media (min-width: 768px) {
  .sidebar {
    position: relative;
    transform: translateX(0);
  }
  .sidebar.hidden { display: none; }
  .sidebar:not(.hidden) { display: flex; }
  .sidebar-overlay { display: none !important; }
}

@media (max-width: 767px) {
  .header-title { font-size: 14px; }
}
