/* ==========================================================================
   1. PALETA: CARBÓN CÁLIDO (OSCURO POR DEFECTO) Y PAPEL CÁLIDO (CLARO)
   ========================================================================== */
:root,
[data-theme="dark"] {
  --bg-base: #141312;
  --bg-elevated: #1C1A18;
  --bg-surface: #1F1D1A;
  --bg-user-msg: #26231F;
  --bg-bot-msg: #1B1917;
  --bg-citation: #221F1B;

  --text-primary: #EFEBE4;
  --text-secondary: #C5BEB4;
  --text-muted: #938B81;

  --accent: #DF8649;
  --accent-hover: #EB9A62;
  --accent-soft: rgba(223, 134, 73, 0.14);
  --accent-border: rgba(223, 134, 73, 0.34);
  --status-ok: #52B788;

  --border-subtle: #2C2925;
  --border-strong: #3E3933;
  --shadow-soft: 0 2px 14px rgba(0, 0, 0, 0.25);
  --shadow-card: 0 14px 40px rgba(0, 0, 0, 0.5);
  --backdrop: rgba(8, 7, 6, 0.78);
}

[data-theme="light"] {
  --bg-base: #FAF9F6;
  --bg-elevated: #F3EFE8;
  --bg-surface: #FFFFFF;
  --bg-user-msg: #EFEAE1;
  --bg-bot-msg: #FFFFFF;
  --bg-citation: #FAF7F2;

  --text-primary: #1F2421;
  --text-secondary: #48433D;
  --text-muted: #726B62;

  --accent: #B85224;
  --accent-hover: #9C4219;
  --accent-soft: rgba(184, 82, 36, 0.1);
  --accent-border: rgba(184, 82, 36, 0.3);
  --status-ok: #2E7D5B;

  --border-subtle: #E7E2D8;
  --border-strong: #D5CEC2;
  --shadow-soft: 0 2px 12px rgba(31, 36, 33, 0.04);
  --shadow-card: 0 10px 32px rgba(31, 36, 33, 0.09);
  --backdrop: rgba(24, 21, 18, 0.48);
}

:root {
  --font-serif: 'Lora', Georgia, 'Times New Roman', serif;
  --font-sans: 'Source Sans 3', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --radius-sm: 9px;
  --radius-md: 13px;
  --radius-lg: 18px;
  --max-width: 1020px; /* Mayor aprovechamiento en monitores de escritorio */
  --transition: 0.2s ease;
}

/* ==========================================================================
   2. RESET Y LAYOUT BASE (ESCALA DE MONITOR)
   ========================================================================== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  background-color: var(--bg-base);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 17.5px; /* Base tipográfica ampliada para lectura cómoda en monitor */
  line-height: 1.68;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  overscroll-behavior: none;
  transition: background-color 0.25s ease, color 0.25s ease;
}

.app-shell {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 0 32px;
}

/* ==========================================================================
   3. HEADER EDITORIAL AMPLIADO + BOTONES CON TEXTO
   ========================================================================== */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 20px 0 16px;
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.brand-logo {
  width: 46px;
  height: 46px;
  border-radius: 12px;
  background: var(--accent-soft);
  border: 1px solid var(--accent-border);
  display: grid;
  place-items: center;
  overflow: hidden;
  flex-shrink: 0;
}

.brand-logo img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.brand-logo svg {
  width: 23px;
  height: 23px;
  color: var(--accent);
}

.brand-text {
  min-width: 0;
}

.brand-title {
  font-family: var(--font-serif);
  font-size: 1.55rem;
  font-weight: 600;
  letter-spacing: -0.015em;
  color: var(--text-primary);
  line-height: 1.18;
}

.brand-subtitle {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-top: 3px;
  line-height: 1.35;
  display: -webkit-box;
  line-clamp: 2; /* Estándar oficial para el futuro */
  -webkit-line-clamp: 2; /* Soporte actual (WebKit/Blink) */
  -webkit-box-orient: vertical;
  overflow: hidden;
  white-space: normal;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

/* Botones de navegación con icono + texto (Sobre, Mejoras, Apoyo) */
.nav-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 38px;
  padding: 0 13px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-subtle);
  background: var(--bg-elevated);
  color: var(--text-secondary);
  font-family: var(--font-sans);
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  transition: var(--transition);
}

.nav-btn svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

.nav-btn:hover {
  color: var(--accent);
  border-color: var(--accent-border);
  background: var(--accent-soft);
}

.nav-btn.accent {
  border-color: var(--accent-border);
  color: var(--text-primary);
}

.nav-btn.accent svg {
  color: var(--accent);
}

.nav-btn.accent:hover {
  background: var(--accent-soft);
  color: var(--accent);
}

/* Botones cuadrados de utilidad (Limpiar y Tema) */
.icon-btn {
  background: transparent;
  border: 1px solid var(--border-subtle);
  color: var(--text-secondary);
  width: 38px;
  height: 38px;
  border-radius: var(--radius-sm);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: var(--transition);
}

.icon-btn:hover {
  color: var(--accent);
  border-color: var(--accent-border);
  background: var(--accent-soft);
}

.icon-btn svg {
  width: 17px;
  height: 17px;
}

.lang-toggle {
  display: inline-flex;
  height: 38px;
  align-items: center;
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  padding: 3px;
  user-select: none;
}

.lang-option {
  font-family: var(--font-sans);
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  padding: 5px 9px;
  border-radius: 6px;
  color: var(--text-muted);
  transition: var(--transition);
  border: none;
  background: transparent;
  cursor: pointer;
}

.lang-option.active {
  background: var(--bg-surface);
  color: var(--accent);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.14);
}

/* ==========================================================================
   4. PANEL DE INFORMACIÓN
   ========================================================================== */
.info-panel {
  display: flex;
  align-items: center;
  gap: 11px;
  margin-top: 12px;
  padding: 10px 16px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  flex-shrink: 0;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: var(--accent);
  flex-shrink: 0;
}

.info-text {
  font-size: 0.82rem; /* Reducido de 0.92rem a 0.82rem */
  color: var(--text-secondary);
  line-height: 1.45;
}

/* ==========================================================================
   5. ÁREA PRINCIPAL DE LECTURA Y CHAT
   ========================================================================== */
.chat-container {
  flex: 1;
  overflow-y: auto;
  padding: 22px 4px 16px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

.chat-container::-webkit-scrollbar {
  width: 6px;
}

.chat-container::-webkit-scrollbar-track {
  background: transparent;
}

.chat-container::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 10px;
}

/* Pantalla inicial: Realiza tu consulta + Imagen aleatoria */
.welcome-stage {
  margin: auto 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 18px;
  padding: 8px 4px;
  animation: fadeIn 0.45s ease;
}

.welcome-heading {
  font-family: var(--font-serif);
  font-size: 1.75rem;
  font-weight: 500;
  letter-spacing: -0.015em;
  color: var(--text-primary);
}

.random-image-card {
  position: relative;
  width: auto;
  max-width: 540px;
  border-radius: var(--radius-lg);
  overflow: hidden;
  border: 1px solid var(--border-subtle);
  background: var(--bg-elevated);
  box-shadow: var(--shadow-card);
}

.random-image-card img {
  width: 100%;
  height: auto;
  max-height: 42dvh;
  object-fit: contain;
  display: block;
  transition: transform 0.6s ease;
}

.random-image-card:hover img {
  transform: scale(1.015);
}

.random-image-badge {
  position: absolute;
  bottom: 10px;
  right: 10px;
  font-size: 0.74rem;
  font-weight: 500;
  color: #FAF9F6;
  background: rgba(20, 19, 18, 0.72);
  border: 1px solid rgba(255, 255, 255, 0.16);
  backdrop-filter: blur(6px);
  padding: 2px 10px;
  border-radius: 999px;
}

.suggestions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  max-width: 780px;
}

.suggestion-chip {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  color: var(--text-secondary);
  font-family: var(--font-sans);
  font-size: 0.9rem;
  padding: 8px 16px;
  border-radius: 999px;
  cursor: pointer;
  transition: var(--transition);
}

.suggestion-chip:hover {
  border-color: var(--accent-border);
  color: var(--accent);
  background: var(--accent-soft);
}

/* Mensajes estilo Cuaderno Editorial */
.message-row {
  display: flex;
  flex-direction: column;
  animation: slideUp 0.28s cubic-bezier(0.2, 0.8, 0.2, 1);
}

.message-row.user {
  align-self: flex-end;
  align-items: flex-end;
  max-width: 78%;
}

.message-row.assistant {
  align-self: stretch;
  align-items: stretch;
  max-width: 100%;
}

.message-meta {
  font-size: 0.82rem;
  color: var(--text-muted);
  margin-bottom: 6px;
  padding: 0 2px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.message-row.assistant .message-meta strong {
  font-family: var(--font-serif);
  font-weight: 600;
  color: var(--accent);
  font-size: 0.88rem;
}

.message-bubble {
  white-space: pre-wrap;
  overflow-wrap: break-word;
}

/* Burbujas de chat: Relleno amplio original para ocupar espacio, pero texto más pequeño */
.message-row.user .message-bubble {
  background-color: var(--bg-user-msg);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  border-bottom-right-radius: 4px;
  padding: 11px 16px; /* Padding ligeramente reducido */
  font-size: 0.86rem; /* Aún más pequeño */
  line-height: 1.55;
  color: var(--text-primary);
}

.message-row.assistant .message-bubble {
  background-color: var(--bg-bot-msg);
  border: 1px solid var(--border-subtle);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius-md);
  padding: 16px 20px; /* Padding proporcionado a la nueva letra */
  font-size: 0.88rem; /* Aún más pequeño */
  line-height: 1.70;
  color: var(--text-primary);
  box-shadow: var(--shadow-soft);
}

/* ==========================================================================
   6. TARJETA COMPACTA DE FUENTES (SPOILER DE TEXTO)
   ========================================================================== */
details.citations-box {
  margin-top: 10px;
  font-size: 0.86rem;
  color: var(--text-secondary);
}

.citations-summary {
  display: inline-block;
  cursor: pointer;
  user-select: none;
  list-style: none; /* Oculta la flecha nativa */
}

/* Oculta la flecha nativa en navegadores basados en WebKit */
.citations-summary::-webkit-details-marker {
  display: none;
}

.citations-label {
  font-weight: 600;
  color: var(--accent);
  text-transform: uppercase;
  font-size: 0.76rem;
  letter-spacing: 0.05em;
  transition: var(--transition);
}

/* Pequeña flecha de texto al lado de FUENTES */
.citations-label::after {
  content: ' ▼';
  font-size: 0.65rem;
  display: inline-block;
  margin-left: 4px;
  transition: transform 0.2s ease;
}

details[open] .citations-label::after {
  transform: rotate(180deg);
}

.citations-summary:hover .citations-label {
  color: var(--accent-hover);
}

.citations-content {
  margin-top: 8px;
  padding: 10px 14px;
  background: var(--bg-citation);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  line-height: 1.55;
  white-space: normal;
  animation: fadeIn 0.25s ease;
}

.citation-entry {
  display: inline;
}

.citation-entry em {
  font-family: var(--font-serif);
  color: var(--text-primary);
  font-style: italic;
}

.citation-sep {
  color: var(--accent);
  margin: 0 7px;
  opacity: 0.75;
}

/* ==========================================================================
   7. INPUT AREA + DISCLAIMER
   ========================================================================== */
.composer-wrapper {
  padding-top: 10px;
  flex-shrink: 0;
}

.input-box {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  padding: 10px 12px 10px 18px;
  box-shadow: var(--shadow-soft);
  transition: var(--transition);
}

.input-box:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

#chat-input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 1.04rem;
  line-height: 1.52;
  resize: none;
  max-height: 140px;
  padding: 6px 0;
}

#chat-input::placeholder {
  color: var(--text-muted);
}

.send-btn {
  width: 42px;
  height: 42px;
  border-radius: 10px;
  border: none;
  background: var(--accent);
  color: #FFFFFF;
  display: grid;
  place-items: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: var(--transition);
}

.send-btn:hover:not(:disabled) {
  background: var(--accent-hover);
  transform: translateY(-1px);
}

.send-btn:disabled {
  background: var(--bg-elevated);
  color: var(--text-muted);
  cursor: not-allowed;
}

.send-btn svg {
  width: 19px;
  height: 19px;
}

.disclaimer {
  font-size: 0.88rem;
  color: var(--text-secondary);
  text-align: center;
  line-height: 1.48;
  margin-top: 10px;
  padding: 0 8px;
}

/* ==========================================================================
   8. FOOTER
   ========================================================================== */
.footer {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 8px 18px;
  padding: 12px 0 max(14px, env(safe-area-inset-bottom));
  margin-top: 10px;
  border-top: 1px solid var(--border-subtle);
  flex-shrink: 0;
}

.footer-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.92rem;
  font-weight: 500;
  font-family: var(--font-sans);
  color: var(--text-secondary);
  text-decoration: none;
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 3px 2px;
  transition: var(--transition);
}

.footer-link:hover {
  color: var(--accent);
}

.footer-link.highlight {
  color: var(--text-primary);
}

.footer-link.highlight svg {
  color: var(--accent);
}

.footer-link svg {
  width: 15px;
  height: 15px;
}

.footer-divider {
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: var(--border-strong);
}

/* ==========================================================================
   9. MODALES (SOBRE, MEJORAS Y APOYO)
   ========================================================================== */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: var(--backdrop);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.22s ease, visibility 0.22s ease;
}

.modal-backdrop.open {
  opacity: 1;
  visibility: visible;
}

.modal-card {
  width: 100%;
  max-width: 740px;
  max-height: 88dvh;
  background: var(--bg-base);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transform: translateY(14px) scale(0.98);
  transition: transform 0.25s cubic-bezier(0.2, 0.8, 0.2, 1);
}

.modal-backdrop.open .modal-card {
  transform: translateY(0) scale(1);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 22px 26px 16px;
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}

.modal-title {
  font-family: var(--font-serif);
  font-size: 1.45rem;
  font-weight: 600;
  color: var(--text-primary);
}

.modal-close {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: 1px solid var(--border-subtle);
  background: transparent;
  color: var(--text-secondary);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: var(--transition);
}

.modal-close:hover {
  background: var(--bg-elevated);
  color: var(--text-primary);
}

.modal-body {
  padding: 22px 26px 26px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
  -webkit-overflow-scrolling: touch;
}

.modal-section {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 20px 22px;
}

.modal-section.support-card {
  background: var(--bg-citation);
  border-color: var(--accent-border);
}

.section-tag {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.82rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 12px;
}

.section-tag.now { color: var(--status-ok); }
.section-tag.next { color: var(--accent); }
.section-tag.support { color: var(--accent); }

.modal-paragraph {
  font-size: 0.92rem; /* Reducido de 1.04rem */
  color: var(--text-secondary);
  line-height: 1.65;
}

.modal-paragraph + .modal-paragraph {
  margin-top: 12px;
}

.feature-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 11px;
}

.feature-list li {
  font-size: 0.92rem; /* Reducido de 1.02rem */
  color: var(--text-secondary);
  display: flex;
  align-items: flex-start;
  gap: 10px;
  line-height: 1.55;
}

.feature-bullet {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  margin-top: 10px;
  flex-shrink: 0;
}

.feature-bullet.now { background: var(--status-ok); }
.feature-bullet.next { background: var(--accent); opacity: 0.8; }

.donate-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 18px;
}

.donate-btn {
  flex: 1;
  min-width: 180px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 18px;
  border-radius: 9px;
  font-size: 0.96rem;
  font-weight: 600;
  font-family: var(--font-sans);
  text-decoration: none;
  cursor: pointer;
  transition: var(--transition);
}

.donate-btn.primary {
  background: var(--accent);
  color: #FFFFFF;
  border: 1px solid transparent;
}

.donate-btn.primary:hover {
  background: var(--accent-hover);
}

.donate-btn.secondary {
  background: var(--bg-surface);
  color: var(--text-primary);
  border: 1px solid var(--border-strong);
}

.donate-btn.secondary:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.donate-note {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-top: 12px;
}

/* ==========================================================================
   10. ANIMACIONES Y RESPONSIVE (TABLET Y MÓVILES)
   ========================================================================== */
@keyframes slideUp {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes pulse {
  0%, 80%, 100% { transform: scale(0.8); opacity: 0.35; }
  40% { transform: scale(1.1); opacity: 1; }
}

@media (max-width: 840px) {
  .nav-btn .btn-label {
    display: none;
  }
  .nav-btn {
    padding: 0;
    width: 36px;
    justify-content: center;
  }
}

@media (max-width: 640px) {
  html, body { font-size: 16px; }
  .app-shell { padding: 0 10px; }
  
  /* Cabecera en 2 filas limpias: Arriba título, abajo botones */
  .header { flex-wrap: wrap; padding: 10px 0 8px; gap: 10px; }
  .brand { gap: 6px; flex: 1 1 100%; justify-content: center; text-align: center; }
  .brand-logo { width: 30px; height: 30px; }
  .brand-title { font-size: 1.1rem; }
  .brand-subtitle { display: block; font-size: 0.72rem; line-height: 1.25; margin-top: 2px; }
  
  /* Botones superiores agrupados y con texto forzado */
  .header-actions { gap: 5px; margin: 0 auto; justify-content: center; width: 100%; }
  .nav-btn { width: auto !important; padding: 0 6px !important; height: 30px; }
  .nav-btn .btn-label { 
    display: inline-block !important; 
    font-size: 0.65rem; 
    margin-left: 3px;
  }
  .icon-btn { width: 30px; height: 30px; }
  .nav-btn svg, .icon-btn svg { width: 13px; height: 13px; }
  .lang-toggle { height: 30px; }
  .lang-option { padding: 2px 5px; font-size: 0.65rem; }
  
  .info-panel { display: none; }
  
  .welcome-heading { font-size: 1.15rem; }
  
  /* IMAGEN MUCHO MÁS GRANDE EN MÓVIL (45% de la pantalla) */
  .random-image-card img { max-height: 45dvh; } 
  
  .suggestion-chip { font-size: 0.78rem; padding: 6px 12px; }
  
  .message-row.user { max-width: 90%; }
  .message-row.assistant .message-bubble { padding: 12px 14px; font-size: 0.88rem; line-height: 1.65; }
  .citations-box { padding: 8px 10px; font-size: 0.78rem; }
  
  .input-box { padding: 6px 8px 6px 12px; }
  #chat-input { font-size: 0.95rem; padding: 2px 0; }
  .send-btn { width: 36px; height: 36px; }
  
  .disclaimer { display: block; font-size: 0.62rem; line-height: 1.15; margin-top: 5px; padding: 0 2px; opacity: 0.85; }
  
  /* Ocultar botones secundarios del pie */
  #footer-about-btn, #footer-about-btn + .footer-divider,
  #footer-roadmap-btn, #footer-roadmap-btn + .footer-divider,
  #footer-support-btn, #footer-support-btn + .footer-divider { display: none; }
  
  .footer { padding: 8px 0 max(10px, env(safe-area-inset-bottom)); gap: 4px 8px; }
  .footer-link { font-size: 0.75rem; }
  .footer-link svg { width: 13px; height: 13px; }
  
  .modal-backdrop { align-items: flex-end; padding: 0; }
  .modal-card { max-width: 100%; max-height: 90dvh; border-radius: 18px 18px 0 0; border-bottom: none; transform: translateY(30px); }
  .modal-header { padding: 16px 18px 12px; }
  .modal-body { padding: 16px 18px max(22px, env(safe-area-inset-bottom)); }
  .modal-paragraph, .feature-list li { font-size: 0.88rem; }
}