/* ==========================================================================
   ZeroChat Sidebar - Historial de Conversaciones estilo ChatGPT & Claude
   ========================================================================== */

.chat-sidebar {
  width: 280px;
  min-width: 280px;
  max-width: 320px;
  height: 100%;
  height: 100dvh;
  height: var(--app-height, 100dvh);
  max-height: 100%;
  background-color: var(--bg-surface);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  z-index: var(--z-sidebar, 110);
  box-shadow: var(--shadow-sm);
  user-select: none;
  /* Transición bidireccional CSS — controlada por .sidebar-hidden */
  transition: translate 0.22s cubic-bezier(0.16, 1, 0.3, 1),
              opacity 0.2s ease,
              visibility 0.22s ease;
  translate: 0 0;
  opacity: 1;
  visibility: visible;
}

/* Estado cerrado: translate fuera de pantalla sin display:none para mantener transición de salida */
.chat-sidebar.sidebar-hidden {
  translate: -100% 0;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

/* Vistas del Sidebar (Chat e Historial / Configuración) */
.sidebar-view {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
  flex: 1 1 auto;
}

.sidebar-view[hidden] {
  display: none !important;
}

/* Cabecera en modo Configuración */
.sidebar-settings-header .sidebar-settings-header-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
}

.btn-sidebar-back {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-main);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  padding: 0.35rem 0.6rem;
  border-radius: var(--radius-sm, 6px);
  min-height: 38px;
  user-select: none;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.btn-sidebar-back:hover {
  background-color: var(--bg-hover);
  color: var(--primary);
}

/* Navegador de Configuración en el Sidebar */
.sidebar-settings-nav {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  padding: 0.75rem 0.6rem;
  overflow-y: auto;
  flex: 1 1 auto;
  min-height: 0;
}

.sidebar-settings-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  width: 100%;
  min-height: 44px;
  padding: 0.6rem 0.85rem;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-md, 8px);
  color: var(--text-main);
  font-size: 0.875rem;
  font-weight: 500;
  text-align: left;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
  box-sizing: border-box;
  transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease;
  text-decoration: none;
}

.sidebar-settings-item:hover {
  background-color: var(--bg-hover);
  color: var(--primary);
}

.sidebar-settings-item:active {
  background-color: var(--bg-active, rgba(0, 0, 0, 0.08));
  transform: scale(0.99);
}

.sidebar-settings-item .settings-item-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  color: var(--text-muted);
  flex-shrink: 0;
}

.sidebar-settings-item .settings-item-icon .ui-icon,
.sidebar-settings-choice .settings-item-icon .ui-icon {
  width: 20px;
  height: 20px;
}

.sidebar-settings-item:hover .settings-item-icon {
  color: var(--primary);
}

.sidebar-settings-item .settings-item-label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-settings-item .settings-item-chevron {
  display: inline-flex;
  align-items: center;
  color: var(--text-muted);
  opacity: 0.7;
  flex-shrink: 0;
  margin-left: auto;
}

.sidebar-settings-choice {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  width: 100%;
  min-height: 44px;
  padding: 0.5rem 0.85rem;
  background: transparent;
  border-radius: var(--radius-md, 8px);
  color: var(--text-main);
  font-size: 0.875rem;
  font-weight: 500;
  box-sizing: border-box;
}

.sidebar-settings-choice .settings-item-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  color: var(--text-muted);
  flex-shrink: 0;
}

.sidebar-settings-choice .settings-item-label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar-choice-toggle-group {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  background: var(--bg-surface-soft, rgba(0, 0, 0, 0.05));
  border: 1px solid var(--border-color, rgba(0, 0, 0, 0.1));
  border-radius: var(--radius-md, 8px);
  padding: 2px;
  flex-shrink: 0;
}

.sidebar-choice-toggle-group .btn-lang-toggle,
.sidebar-choice-toggle-group .btn-theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 32px;
  height: 28px;
  padding: 0 0.45rem;
  border-radius: 6px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
  transition: background-color 0.15s ease, color 0.15s ease, box-shadow 0.15s ease;
}

.sidebar-choice-toggle-group .btn-lang-toggle:hover,
.sidebar-choice-toggle-group .btn-theme-toggle:hover {
  color: var(--text-main);
}

.sidebar-choice-toggle-group .btn-lang-toggle.active,
.sidebar-choice-toggle-group .btn-theme-toggle.active {
  background: var(--bg-surface, #ffffff);
  color: var(--primary, #2563eb);
  box-shadow: var(--shadow-sm, 0 1px 2px rgba(0, 0, 0, 0.08));
}

[data-theme="dark"] .sidebar-choice-toggle-group {
  background: rgba(255, 255, 255, 0.06);
  border-color: rgba(255, 255, 255, 0.12);
}

[data-theme="dark"] .sidebar-choice-toggle-group .btn-lang-toggle.active,
[data-theme="dark"] .sidebar-choice-toggle-group .btn-theme-toggle.active {
  background: var(--bg-surface-elevated, #1e293b);
  color: var(--primary-light, #60a5fa);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}


/* Cabecera de Barra Lateral */
.sidebar-header {
  padding: 0.65rem 0.85rem;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-height: 48px;
  box-sizing: border-box;
}

.sidebar-header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.4rem;
  width: 100%;
}

.sidebar-header-actions {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  width: 100%;
}

.sidebar-header-actions .btn-sidebar-icon {
  flex: 1;
  width: auto;
  min-width: 0;
}

/* Botón Destacado "+ Nueva conversación" */
.btn-sidebar-new-chat {
  width: 100%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  padding: 0.55rem 0.85rem;
  background-color: var(--primary);
  color: #ffffff;
  border: 1px solid transparent;
  border-radius: var(--radius-full);
  font-size: 0.825rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
  box-shadow: var(--shadow-xs);
  user-select: none;
}

.btn-sidebar-new-chat:hover {
  background-color: var(--primary-hover);
  transform: scale(1.02);
  box-shadow: var(--shadow-sm);
}

.btn-sidebar-new-chat:active {
  transform: scale(0.98);
}

/* Botones de acción en cabecera de barra lateral (sin enmarcado) */
.btn-sidebar-icon,
.btn-sidebar-close {
  background: transparent;
  border: none;
  color: var(--text-muted);
  width: 44px;
  height: 44px;
  min-width: 44px;
  border-radius: var(--radius-md);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 0.85rem;
  text-decoration: none;
  transition: all 0.15s ease;
}

.btn-sidebar-icon .ui-icon,
.btn-sidebar-close .ui-icon,
.btn-sidebar-footer .ui-icon {
  width: 20px;
  height: 20px;
}

.btn-sidebar-icon:hover,
.btn-sidebar-close:hover {
  background-color: var(--bg-hover);
  color: var(--text-main);
}

/* Buscador de conversaciones */
.sidebar-search-box {
  padding: 0.5rem 0.85rem;
  border-bottom: 1px solid var(--border-subtle);
  position: relative;
  display: flex;
  align-items: center;
}

.sidebar-search-icon {
  position: absolute;
  left: 1.45rem;
  font-size: 0.8rem;
  color: var(--text-muted);
  pointer-events: none;
}

.sidebar-search-input {
  width: 100%;
  padding: 0.45rem 0.65rem 0.45rem 2rem;
  border-radius: var(--radius-full);
  border: 1px solid var(--input-border);
  background-color: var(--input-bg-subtle);
  color: var(--text-main);
  font-family: inherit;
  font-size: 0.8rem;
  outline: none;
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast), background-color var(--transition-fast);
}

.sidebar-search-input:hover {
  border-color: var(--input-border-hover);
}

.sidebar-search-input:focus,
.sidebar-search-input:focus-visible {
  border-color: var(--input-border-focus);
  background-color: var(--input-bg);
  box-shadow: var(--input-focus-ring);
  outline: none;
}

/* Encabezados de Agrupación Cronológica (Hoy, Ayer, Últimos 7 días, etc.) */
.sidebar-group-header {
  padding: 0.65rem 0.75rem 0.25rem 0.75rem;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  user-select: none;
}

/* Lista de conversaciones */
.sidebar-chats-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.sidebar-chat-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.55rem 0.75rem;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all 0.15s;
  border: 1px solid transparent;
  color: var(--text-main);
  position: relative;
}

.sidebar-chat-item:hover {
  background-color: var(--bg-hover);
}

.sidebar-chat-item.active {
  background-color: var(--primary-light);
  border-color: rgba(var(--primary-rgb), 0.25);
  color: var(--primary);
  font-weight: 600;
}

.sidebar-chat-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.sidebar-chat-title {
  font-size: 0.835rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.35;
}

.sidebar-chat-time {
  font-size: 0.685rem;
  color: var(--text-muted);
}

.sidebar-chat-actions {
  display: none;
  align-items: center;
  gap: 0.2rem;
  opacity: 0.8;
}

.sidebar-chat-item:hover .sidebar-chat-actions,
.sidebar-chat-item:focus-within .sidebar-chat-actions {
  display: flex;
}

.btn-chat-action {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0.2rem 0.35rem;
  font-size: 0.75rem;
  border-radius: var(--radius-sm);
  transition: all 0.15s ease;
}

.btn-chat-action:hover {
  color: var(--text-main);
  background-color: var(--bg-hover);
}

.btn-chat-action.btn-delete:hover {
  color: var(--danger);
  background-color: #fee2e2;
}

[data-theme="dark"] .btn-chat-action.btn-delete:hover {
  background-color: rgba(239, 68, 68, 0.2);
}

/* Pie de Barra Lateral (sin enmarcado) */
.sidebar-footer {
  padding: 0.65rem 0.85rem;
  border-top: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  gap: 0.4rem;
  background-color: var(--bg-surface);
}

.btn-sidebar-footer {
  flex: 0 0 44px;
  width: 44px;
  height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  border-radius: var(--radius-md);
  border: none;
  background-color: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s ease;
}

.btn-sidebar-footer:hover {
  background-color: var(--bg-hover);
  color: var(--text-main);
}

.btn-sidebar-danger {
  margin-left: auto;
  color: var(--danger);
}

.btn-sidebar-danger:hover {
  background-color: #fee2e2;
  color: var(--danger);
}

[data-theme="dark"] .btn-sidebar-danger:hover {
  background-color: rgba(239, 68, 68, 0.2);
}

/* Pie de Barra Lateral en Modo Configuración */
.sidebar-settings-footer {
  padding: 0.65rem 0.85rem;
  border-top: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  background-color: var(--bg-surface);
  flex-shrink: 0;
}

.btn-sidebar-settings-clear {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  min-height: 38px;
  padding: 0.45rem 0.75rem;
  border-radius: var(--radius-md, 8px);
  border: 1px solid rgba(239, 68, 68, 0.35);
  background-color: transparent;
  color: #ef4444;
  font-size: 0.8125rem;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
  user-select: none;
  box-sizing: border-box;
}

.btn-sidebar-settings-clear:hover {
  background-color: #fee2e2;
  border-color: #ef4444;
  color: #dc2626;
}

[data-theme="dark"] .btn-sidebar-settings-clear {
  color: #f87171;
  border-color: rgba(239, 68, 68, 0.35);
  background-color: rgba(239, 68, 68, 0.08);
}

[data-theme="dark"] .btn-sidebar-settings-clear:hover {
  background-color: rgba(239, 68, 68, 0.2);
  border-color: #ef4444;
  color: #fca5a5;
}

/* Adaptación Responsive para Móviles (Drawer Drawer Mode) */
@media (max-width: 768px) {
  .chat-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: var(--z-sidebar, 110);
    box-shadow: var(--shadow-lg);
    width: 82vw;
    max-width: 320px;
    padding-top: env(safe-area-inset-top, 0px);
  }

  /* El drawer móvil se mantiene cerrado hasta que el usuario lo solicite. */
  .chat-sidebar:not(.sidebar-visible) {
    translate: -100% 0;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
  }
}

/* Backdrop oscuro detrás del sidebar en móvil */
.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: calc(var(--z-sidebar, 110) - 1);
  background-color: rgba(15, 23, 42, 0.45);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  transition: opacity 0.22s ease;
}

@media (max-width: 768px) {
  .sidebar-backdrop.visible {
    display: block;
    animation: fadeInBackdrop 0.22s ease both;
  }
}

@keyframes fadeInBackdrop {
  from { opacity: 0; }
  to   { opacity: 1; }
}
