/* ── Bootstrap dark theme variable overrides ─────────────────────────────── */
[data-bs-theme="dark"] {
  --bs-body-bg:           #0f172a;
  --bs-body-color:        #e2e8f0;
  --bs-secondary-bg:      #1e293b;
  --bs-tertiary-bg:       #334155;
  --bs-border-color:      #334155;
  --bs-modal-bg:          #1e293b;
  --bs-modal-border-color: #334155;
  --bs-card-bg:           #1e293b;
  --bs-card-border-color: #334155;
}

/* ── Custom variables ─────────────────────────────────────────────────────── */
:root {
  --bg:          #0f172a;
  --sidebar:     #1e293b;
  --surface:     #1e293b;
  --surface2:    #334155;
  --surface3:    #475569;
  --accent:      #3b82f6;
  --accent-dark: #1d4ed8;
  --text:        #e2e8f0;
  --text-muted:  #94a3b8;
  --border:      #334155;
  --user-bg:     #2563eb;
  --ai-bg:       #1e293b;
  --error:       #ef4444;
  --error-bg:    #450a0a;
  --success:     #22c55e;
  --medico:      #0ea5e9;
  --psicologo:   #a855f7;
}

html, body { height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 15px;
  line-height: 1.5;
  overflow: hidden;
}

/* ── App layout ──────────────────────────────────────────────────────────── */
.app { display: flex; height: 100vh; overflow: hidden; }

/* ── Sidebar (offcanvas on mobile, static on md+) ────────────────────────── */
.sidebar {
  background: var(--sidebar);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border-right: 1px solid var(--border);
}

/* Desktop: static sidebar at 280px */
@media (min-width: 768px) {
  .sidebar {
    width: 280px !important;
    min-width: 280px !important;
    position: relative !important;
    transform: none !important;
    visibility: visible !important;
    background: var(--sidebar) !important;
    border-right: 1px solid var(--border) !important;
    height: 100vh;
    flex-shrink: 0;
    transition: width 0.2s, min-width 0.2s;
  }
  .sidebar.sidebar-collapsed {
    width: 0 !important;
    min-width: 0 !important;
    display: none !important;
  }
  /* Hide Bootstrap's offcanvas backdrop on desktop */
  .offcanvas-backdrop { display: none !important; }
}

/* Mobile offcanvas uses Bootstrap's default behavior */
@media (max-width: 767.98px) {
  .sidebar {
    --bs-offcanvas-width: 85vw;
    max-width: 320px;
    background: var(--sidebar) !important;
  }
}

.sidebar-header {
  padding: 1rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

/* ── Sidebar new-chat button ─────────────────────────────────────────────── */
.btn-new {
  flex: 1;
  justify-content: center;
  gap: 0.4rem;
  font-size: 0.9rem;
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 0.5rem 0.75rem;
  border-radius: 0.5rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  font-weight: 600;
  transition: background 0.15s;
}
.btn-new:hover { background: var(--accent-dark); }
.btn-new span { font-size: 1.1rem; line-height: 1; }

.session-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
}
.session-empty {
  color: var(--text-muted);
  font-size: 0.85rem;
  text-align: center;
  padding: 2rem 1rem;
}
.session-item {
  padding: 0.65rem 0.75rem;
  border-radius: 0.5rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  transition: background 0.12s;
  user-select: none;
}
.session-item:hover { background: var(--surface2); }
.session-item.active { background: var(--surface2); }
.session-icon { font-size: 1rem; flex-shrink: 0; }
.session-info { flex: 1; min-width: 0; }
.session-title {
  font-size: 0.85rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.session-meta { font-size: 0.75rem; color: var(--text-muted); }
.session-model-tag {
  font-size: 0.68rem;
  font-weight: 700;
  padding: 0.1rem 0.4rem;
  border-radius: 0.25rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  flex-shrink: 0;
}
.session-model-tag.medico    { background: rgba(14,165,233,0.15); color: var(--medico); }
.session-model-tag.psicologo { background: rgba(168,85,247,0.15); color: var(--psicologo); }

.sidebar-footer {
  padding: 0.875rem 1rem;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}
.sidebar-user { font-size: 0.85rem; color: var(--text-muted); }
.btn-logout {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.82rem;
  padding: 0.3rem 0.5rem;
  border-radius: 0.35rem;
  transition: color 0.15s, background 0.15s;
}
.btn-logout:hover { color: var(--error); background: var(--error-bg); }

/* ── Delete-all button ───────────────────────────────────────────────────── */
.btn-delete-all {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 1rem;
  padding: 0.4rem;
  border-radius: 0.4rem;
  flex-shrink: 0;
  transition: color 0.15s, background 0.15s;
}
.btn-delete-all:hover { color: var(--error); background: var(--error-bg); }

/* ── Main area ───────────────────────────────────────────────────────────── */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; overflow: hidden; }

/* ── Chat header ─────────────────────────────────────────────────────────── */
.chat-header {
  padding: 0.65rem 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--sidebar);
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}
.btn-back {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 1.1rem;
  padding: 0.25rem 0.5rem;
  border-radius: 0.35rem;
  transition: color 0.15s;
  flex-shrink: 0;
  line-height: 1;
}
.btn-back:hover { color: var(--text); }
.chat-model-badge {
  flex: 1;
  font-size: 0.85rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.badge-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.badge-dot.medico    { background: var(--medico); }
.badge-dot.psicologo { background: var(--psicologo); }
.chat-header-actions { display: flex; gap: 0.35rem; flex-shrink: 0; }

/* Header action buttons — use Bootstrap .btn .btn-sm with our ghost style */
.btn-ghost {
  background: transparent !important;
  color: var(--text-muted) !important;
  border: 1px solid var(--border) !important;
  font-size: 0.82rem !important;
  padding: 0.3rem 0.55rem !important;
  border-radius: 0.4rem !important;
}
.btn-ghost:hover {
  background: var(--surface2) !important;
  color: var(--text) !important;
}
#btn-autoplay.active {
  background: var(--accent) !important;
  color: #fff !important;
  border-color: var(--accent) !important;
}

/* ── Messages ────────────────────────────────────────────────────────────── */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  scroll-behavior: smooth;
}

.welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  flex: 1;
  gap: 0.75rem;
  padding: 2rem 1rem;
}
.welcome-icon { font-size: 3rem; }
.welcome h2 { font-size: 1.3rem; font-weight: 600; }
.welcome p  { color: var(--text-muted); }
.model-cards {
  display: flex;
  gap: 1rem;
  margin-top: 0.5rem;
  flex-wrap: wrap;
  justify-content: center;
}
.model-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  padding: 1.25rem 1.5rem;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
  transition: background 0.15s, border-color 0.15s;
  min-width: 130px;
  color: var(--text);
}
.model-card:hover { background: var(--surface2); border-color: var(--accent); }
.model-card-icon { font-size: 2rem; }
.model-card strong { font-size: 0.95rem; }
.model-card small { font-size: 0.8rem; color: var(--text-muted); }

/* Chat message bubbles */
.msg {
  display: flex;
  gap: 0.75rem;
  max-width: 82%;
  animation: fadeIn 0.15s ease;
}
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; } }

.msg.user { align-self: flex-end; flex-direction: row-reverse; }
.msg.assistant { align-self: flex-start; }

.msg-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--surface2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  flex-shrink: 0;
  margin-top: 2px;
}
.msg.user .msg-avatar { background: var(--accent); }

.msg-bubble {
  padding: 0.65rem 0.9rem;
  border-radius: 1rem;
  line-height: 1.65;
  font-size: 0.9375rem;
  max-width: 100%;
  word-break: break-word;
}
.msg.user .msg-bubble {
  background: var(--user-bg);
  border-radius: 1rem 0.25rem 1rem 1rem;
  color: #fff;
}
.msg.assistant .msg-bubble {
  background: var(--ai-bg);
  border: 1px solid var(--border);
  border-radius: 0.25rem 1rem 1rem 1rem;
}

/* Markdown-like content */
.msg-bubble p  { margin-bottom: 0.5rem; }
.msg-bubble p:last-child { margin-bottom: 0; }
.msg-bubble h2, .msg-bubble h3, .msg-bubble h4 { margin: 0.75rem 0 0.35rem; font-weight: 700; }
.msg-bubble h2 { font-size: 1.05rem; }
.msg-bubble h3 { font-size: 0.95rem; }
.msg-bubble h4 { font-size: 0.9rem; }
.msg-bubble ul, .msg-bubble ol { padding-left: 1.5rem; margin: 0.5rem 0; }
.msg-bubble li { margin-bottom: 0.25rem; }
.msg-bubble strong { font-weight: 700; }
.msg-bubble em { font-style: italic; }
.msg-bubble code {
  background: var(--surface3);
  padding: 0.15rem 0.35rem;
  border-radius: 0.25rem;
  font-family: monospace;
  font-size: 0.875em;
}
.msg-bubble pre {
  background: var(--surface3);
  padding: 0.75rem;
  border-radius: 0.5rem;
  overflow-x: auto;
  margin: 0.5rem 0;
}
.msg-bubble pre code { background: none; padding: 0; }
.msg-bubble hr { border: none; border-top: 1px solid var(--border); margin: 0.75rem 0; }

.msg-error .msg-bubble {
  background: var(--error-bg);
  border-color: var(--error);
  color: var(--error);
}

/* Typing indicator */
.typing-dots {
  display: flex;
  gap: 4px;
  padding: 0.25rem 0;
  align-items: center;
}
.typing-dots span {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: bounce 1.2s infinite;
}
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.5; }
  40% { transform: translateY(-5px); opacity: 1; }
}

/* ── Input area ──────────────────────────────────────────────────────────── */
.input-area {
  padding: 0.75rem 1rem;
  border-top: 1px solid var(--border);
  background: var(--sidebar);
  flex-shrink: 0;
}
.input-form {
  display: flex;
  gap: 0.4rem;
  align-items: flex-end;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 0.875rem;
  padding: 0.4rem;
  transition: border-color 0.15s;
}
.input-form:focus-within { border-color: var(--accent); }
.input-form textarea {
  flex: 1;
  background: none;
  border: none;
  color: var(--text);
  font-size: 0.9375rem;
  font-family: inherit;
  resize: none;
  outline: none;
  padding: 0.35rem 0.4rem;
  min-height: 36px;
  max-height: 150px;
  line-height: 1.5;
}
.input-form textarea::placeholder { color: var(--text-muted); }

.btn-send {
  background: var(--accent);
  border: none;
  color: #fff;
  width: 36px;
  height: 36px;
  border-radius: 0.6rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, opacity 0.15s;
}
.btn-send:hover:not(:disabled) { background: var(--accent-dark); }
.btn-send:disabled { opacity: 0.4; cursor: not-allowed; }

/* ── Voice mode status bar ───────────────────────────────────────────────── */
.voice-status {
  display: none;
  align-items: center;
  gap: 0.6rem;
  padding: 0.5rem 0.75rem;
  background: var(--surface2);
  border-radius: 0.6rem;
  margin-bottom: 0.5rem;
  font-size: 0.875rem;
  font-weight: 500;
}
.voice-status.active { display: flex; }
.voice-status-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
  background: var(--text-muted);
}
.voice-status-dot.listening    { background: #dc2626; animation: pulse-rec 1.2s infinite; }
.voice-status-dot.transcribing { background: #f59e0b; }
.voice-status-dot.sending      { background: var(--accent); animation: pulse-send 1s infinite; }
.voice-status-dot.speaking     { background: #22c55e; animation: pulse-rec 1.2s infinite; }
@keyframes pulse-send {
  0%, 100% { box-shadow: 0 0 0 0 rgba(59,130,246,0.5); }
  50% { box-shadow: 0 0 0 6px rgba(59,130,246,0); }
}
#voice-status-text { flex: 1; color: var(--text); }
.btn-voice-exit {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.8rem;
  padding: 0.15rem 0.4rem;
  border-radius: 0.3rem;
  transition: color 0.15s;
}
.btn-voice-exit:hover { color: var(--error); }

/* ── Voice mode button ───────────────────────────────────────────────────── */
.btn-voice-mode {
  background: var(--surface3);
  border: none;
  color: var(--text);
  width: 36px; height: 36px;
  border-radius: 0.6rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 1rem;
  transition: background 0.15s;
}
.btn-voice-mode:hover:not(:disabled) { background: var(--surface2); }
.btn-voice-mode.active { background: #dc2626; animation: pulse-rec 1.2s infinite; }
.btn-voice-mode:disabled { opacity: 0.4; cursor: not-allowed; }

/* ── Image attach button ─────────────────────────────────────────────────── */
.btn-img {
  background: var(--surface3);
  border: none;
  color: var(--text);
  width: 36px;
  height: 36px;
  border-radius: 0.6rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 1rem;
  transition: background 0.15s;
}
.btn-img:hover { background: var(--surface2); }

/* ── Image preview ───────────────────────────────────────────────────────── */
.img-preview {
  display: none;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.5rem 0;
}
.img-preview.visible { display: flex; }
.img-preview img {
  height: 56px;
  width: auto;
  max-width: 180px;
  border-radius: 0.5rem;
  border: 1px solid var(--border);
  object-fit: cover;
}
.btn-img-clear {
  background: var(--surface3);
  border: none;
  color: var(--text-muted);
  width: 22px;
  height: 22px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 0.7rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s;
}
.btn-img-clear:hover { background: var(--error-bg); color: var(--error); }

/* ── Image in message bubble ─────────────────────────────────────────────── */
.msg-bubble img.msg-img {
  display: block;
  max-width: 240px;
  max-height: 240px;
  border-radius: 0.5rem;
  margin-bottom: 0.5rem;
  object-fit: contain;
  border: 1px solid var(--border);
}

/* ── Mic button ──────────────────────────────────────────────────────────── */
.btn-mic {
  background: var(--surface3);
  border: none;
  color: var(--text);
  width: 36px;
  height: 36px;
  border-radius: 0.6rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 1rem;
  transition: background 0.15s, opacity 0.15s;
}
.btn-mic:hover:not(:disabled) { background: var(--surface2); }
.btn-mic:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-mic.recording {
  background: #dc2626;
  animation: pulse-rec 1.2s ease-in-out infinite;
}
.btn-mic.transcribing { opacity: 0.6; cursor: wait; }
@keyframes pulse-rec {
  0%, 100% { box-shadow: 0 0 0 0 rgba(220,38,38,0.5); }
  50% { box-shadow: 0 0 0 7px rgba(220,38,38,0); }
}

/* ── Speak button (per message) ──────────────────────────────────────────── */
.btn-speak {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.85rem;
  padding: 0.2rem 0.35rem;
  border-radius: 0.35rem;
  align-self: flex-end;
  flex-shrink: 0;
  opacity: 0.5;
  transition: opacity 0.15s, color 0.15s;
}
.btn-speak:hover { opacity: 1; color: var(--text); }
.btn-speak.playing { color: var(--accent); opacity: 1; }

/* ── Bootstrap modal custom overrides ───────────────────────────────────── */
.chatbot-modal {
  background: var(--sidebar) !important;
  border-color: var(--border) !important;
}
.modal-models { display: flex; flex-direction: column; gap: 0.75rem; }
.modal-model-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  padding: 1rem 1.25rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 1rem;
  text-align: left;
  color: var(--text);
  width: 100%;
  transition: background 0.15s, border-color 0.15s;
}
.modal-model-btn:hover { background: var(--surface3); border-color: var(--accent); }
.modal-model-btn > span { font-size: 2rem; line-height: 1; }
.modal-model-btn > div { display: flex; flex-direction: column; gap: 0.2rem; }
.modal-model-btn strong { font-size: 1rem; color: var(--text); }
.modal-model-btn small { color: var(--text-muted); font-size: 0.85rem; }

/* ── Auth / Login page ───────────────────────────────────────────────────── */
.auth-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background: var(--bg);
  overflow-y: auto;
}
.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--sidebar);
  border: 1px solid var(--border);
  border-radius: 1rem;
  padding: 2rem;
}
.auth-logo { text-align: center; margin-bottom: 1.5rem; }
.auth-logo-icon { font-size: 2.5rem; display: block; margin-bottom: 0.5rem; }
.auth-logo h1 { font-size: 1.4rem; font-weight: 700; margin: 0; }
.auth-logo p  { color: var(--text-muted); font-size: 0.9rem; margin: 0.25rem 0 0; }

/* ── Scrollbar ───────────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--surface3); border-radius: 3px; }

/* ── Mobile responsive ───────────────────────────────────────────────────── */
@media (max-width: 767.98px) {
  body { overflow: hidden; }

  .app { height: 100dvh; }   /* dynamic viewport for mobile browsers */

  .chat-header { padding: 0.5rem 0.65rem; gap: 0.4rem; }
  .chat-model-badge { font-size: 0.8rem; }

  .messages { padding: 0.875rem 0.65rem; }
  .msg { max-width: 90%; }
  .msg-bubble { font-size: 0.9rem; padding: 0.6rem 0.8rem; }

  .input-area { padding: 0.6rem 0.65rem; }
  .input-form { padding: 0.35rem; gap: 0.3rem; }
  .input-form textarea { font-size: 0.9rem; }

  /* Slightly smaller icon buttons on mobile to fit */
  .btn-send, .btn-mic, .btn-voice-mode, .btn-img {
    width: 34px;
    height: 34px;
    font-size: 0.95rem;
  }

  .welcome { padding: 1.25rem 0.75rem; gap: 0.6rem; }
  .welcome h2 { font-size: 1.15rem; }
  .model-cards { gap: 0.75rem; }
  .model-card {
    min-width: 0;
    flex-direction: row;
    text-align: left;
    padding: 0.875rem 1rem;
  }
  .model-card-icon { font-size: 1.5rem; }

  .msg-bubble img.msg-img { max-width: 180px; max-height: 180px; }
}
