/* ======================================================================= */
/* === CHAT GULY - VARIABLES SINCRONIZADAS CON TEMA GLOBAL === */
/* ======================================================================= */
:root {
  --chat-primary: #28a745;
  --chat-bg: #f8f9fa;
  --chat-text: #333;
  --chat-input-bg: #ffffff;
  --chat-btn-hover: #218838;
  --chat-user-bg: #28a745;
  --chat-bot-bg: #e9ecef;
  --chat-border: #dddddd;
  --chat-modal-bg: #ffffff;
}

/* Variables para tema oscuro */
html[data-theme="dark"] {
  --chat-primary: #28a745;
  --chat-bg: #1a1a1a;
  --chat-text: #e0e0e0;
  --chat-input-bg: #2d2d2d;
  --chat-btn-hover: #1e7e34;
  --chat-user-bg: #218838;
  --chat-bot-bg: #333333;
  --chat-border: #444444;
  --chat-modal-bg: #2d2d2d;
}

/* Forzar variables en modo oscuro (backup) */
[data-theme="dark"] {
  --chat-bg: #1a1a1a;
  --chat-text: #e0e0e0;
  --chat-input-bg: #2d2d2d;
  --chat-border: #444444;
}

/* ======================================================================= */
/* === ESTILOS DEL CHAT === */
/* ======================================================================= */

/* === CHAT TOGGLE === */
#chat-widget-toggle { 
  position: fixed; 
  right: 15px; 
  bottom: 15px; 
  width: 50px; 
  height: 50px; 
  background: var(--chat-primary); 
  color: white; 
  border: none; 
  border-radius: 50%; 
  font-size: 20px; 
  cursor: pointer; 
  box-shadow: 0 3px 10px rgba(0,0,0,0.2); 
  z-index: 9999; 
  display: flex !important;
  align-items: center; 
  justify-content: center; 
  transition: all 0.3s; 
}

#chat-widget-toggle:hover { 
  background: var(--chat-btn-hover); 
  transform: scale(1.08); 
}

/* === CHAT WIDGET === */
#chat-widget { 
  position: fixed; 
  right: 15px; 
  bottom: 75px; 
  width: 340px; 
  max-width: 90vw; 
  height: 500px; 
  background: var(--chat-input-bg); 
  border-radius: 12px; 
  box-shadow: 0 8px 25px rgba(0,0,0,0.3); 
  display: flex; 
  flex-direction: column; 
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; 
  overflow: hidden; 
  z-index: 9998; 
  opacity: 0; 
  visibility: hidden; 
  transform: translateY(15px); 
  transition: all 0.3s ease; 
  border: 1px solid var(--chat-border); 
}

#chat-widget.open { 
  opacity: 1; 
  visibility: visible; 
  transform: translateY(0); 
}

/* === HEADER === */
#chat-header { 
  background: var(--chat-primary); 
  color: white; 
  padding: 10px 12px; 
  font-weight: 600; 
  text-align: center; 
  position: relative; 
  font-size: 0.9rem; 
}

.chat-icon-btn { 
  background: none; 
  border: none; 
  color: white; 
  font-size: 15px; 
  cursor: pointer; 
  padding: 6px; 
  border-radius: 50%; 
  transition: 0.2s; 
}

.chat-icon-btn:hover { 
  background: rgba(255,255,255,0.2); 
}

/* OCULTAR botón de tema del chat (usa el tema global) */
#chat-theme-toggle { 
  display: none !important;
}

#chat-clear { position: absolute; top: 8px; left: 10px; }
#chat-kb-editor { position: absolute; top: 8px; left: 38px; }
#chat-whatsapp { position: absolute; top: 8px; right: 95px; }
#chat-pdf { position: absolute; top: 8px; right: 68px; }
#chat-sound { position: absolute; top: 8px; right: 41px; }
#chat-close { position: absolute; top: 8px; right: 10px; }

/* === MESSAGES === */
#chat-messages { 
  flex: 1; 
  padding: 10px; 
  overflow-y: auto; 
  background: var(--chat-bg); 
  font-size: 0.8rem;
}

#chat-messages::-webkit-scrollbar { width: 5px; }
#chat-messages::-webkit-scrollbar-track { background: transparent; }
#chat-messages::-webkit-scrollbar-thumb { 
  background: var(--chat-border); 
  border-radius: 10px; 
}

.msg { 
  margin: 6px 0; 
  padding: 8px 11px; 
  border-radius: 14px; 
  max-width: 82%; 
  word-wrap: break-word; 
  line-height: 1.35; 
  animation: fadeIn 0.3s ease; 
  white-space: pre-line; 
  font-size: 0.8rem;
}

.msg.user { 
  background: var(--chat-user-bg); 
  color: white; 
  margin-left: auto; 
  border-bottom-right-radius: 3px; 
}

.msg.bot { 
  background: var(--chat-bot-bg); 
  color: var(--chat-text); 
  margin-right: auto; 
  border-bottom-left-radius: 3px; 
}

.msg.typing { 
  font-style: italic; 
  color: #888; 
  font-size: 0.75rem;
}

/* === SUGGESTIONS === */
.suggestions { 
  display: flex; 
  flex-wrap: wrap; 
  gap: 5px; 
  padding: 8px 10px; 
  border-top: 1px solid var(--chat-border); 
  background: var(--chat-input-bg); 
}

.sug-btn { 
  background: rgba(40, 167, 69, 0.15); 
  color: var(--chat-primary); 
  border: 1px solid var(--chat-primary); 
  padding: 5px 9px; 
  border-radius: 15px; 
  font-size: 0.7rem; 
  cursor: pointer; 
  transition: 0.2s; 
  display: flex; 
  align-items: center; 
  gap: 4px;
  font-weight: 500;
}

.sug-btn:hover { 
  background: var(--chat-primary);
  color: white;
}

/* === INPUT AREA === */
#chat-input-area { 
  display: flex; 
  padding: 8px; 
  background: var(--chat-input-bg); 
  border-top: 1px solid var(--chat-border); 
}

#chat-input { 
  flex: 1; 
  padding: 8px 12px; 
  border: 1px solid var(--chat-border); 
  border-radius: 20px; 
  background: var(--chat-bg); 
  color: var(--chat-text); 
  outline: none; 
  font-size: 0.8rem; 
}

#chat-input::placeholder {
  color: #888;
}

#chat-input:focus { 
  border-color: var(--chat-primary); 
}

#chat-send { 
  width: 36px; 
  height: 36px; 
  margin-left: 6px; 
  background: var(--chat-primary); 
  color: white; 
  border: none; 
  border-radius: 50%; 
  cursor: pointer; 
  font-size: 15px; 
  display: flex; 
  align-items: center; 
  justify-content: center;
  transition: all 0.2s;
}

#chat-send:hover { 
  background: var(--chat-btn-hover); 
  transform: scale(1.05);
}

/* === MODALS === */
#pdf-modal, #confirm-modal, #kb-editor-modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  z-index: 10001;
  justify-content: center;
  align-items: center;
}

#pdf-modal.open, #confirm-modal.open, #kb-editor-modal.open {
  display: flex;
}

#pdf-frame { 
  width: 90%; 
  height: 90%; 
  border: none; 
  border-radius: 12px; 
}

/* === CONFIRM MODAL === */
.confirm-box {
  background: var(--chat-modal-bg);
  border-radius: 12px;
  padding: 20px;
  max-width: 320px;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.3);
  text-align: center;
  animation: fadeIn 0.3s ease;
  border: 1px solid var(--chat-border);
}

.confirm-box h3 {
  margin: 0 0 10px 0;
  color: var(--chat-text);
  font-size: 1.1rem;
}

.confirm-box p {
  margin: 0 0 20px 0;
  color: var(--chat-text);
  font-size: 0.85rem;
  line-height: 1.4;
}

.confirm-buttons {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.confirm-btn {
  padding: 8px 20px;
  border: none;
  border-radius: 6px;
  font-size: 0.85rem;
  cursor: pointer;
  transition: 0.2s;
  font-weight: 500;
}

.confirm-btn.cancel {
  background: #6c757d;
  color: white;
}

.confirm-btn.cancel:hover {
  background: #5a6268;
}

.confirm-btn.delete {
  background: #dc3545;
  color: white;
}

.confirm-btn.delete:hover {
  background: #c82333;
}

/* === KB EDITOR === */
.kb-editor-box {
  background: var(--chat-modal-bg);
  border-radius: 12px;
  padding: 20px;
  max-width: 80vw;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.3);
  animation: fadeIn 0.3s ease;
  border: 1px solid var(--chat-border);
}

.kb-editor-box h3 {
  margin: 0 0 15px 0;
  color: var(--chat-text);
  font-size: 1.2rem;
}

#kb-editor-textarea {
  width: 100%;
  min-height: 400px;
  padding: 10px;
  border: 1px solid var(--chat-border);
  border-radius: 6px;
  background: var(--chat-bg);
  color: var(--chat-text);
  font-family: 'Courier New', monospace;
  font-size: 0.85rem;
  resize: vertical;
}

.kb-editor-buttons {
  display: flex;
  gap: 10px;
  justify-content: space-between;
  margin-top: 15px;
  flex-wrap: wrap;
}

.kb-btn {
  padding: 8px 16px;
  border: none;
  border-radius: 6px;
  font-size: 0.85rem;
  cursor: pointer;
  transition: 0.2s;
  font-weight: 500;
}

.kb-btn.save {
  background: var(--chat-primary);
  color: white;
}

.kb-btn.save:hover {
  background: var(--chat-btn-hover);
}

.kb-btn.export {
  background: #007bff;
  color: white;
}

.kb-btn.export:hover {
  background: #0056b3;
}

.kb-btn.import {
  background: #ffc107;
  color: #333;
  cursor: pointer;
  display: inline-block;
}

.kb-btn.import:hover {
  background: #e0a800;
}

.kb-btn.reset {
  background: #dc3545;
  color: white;
}

.kb-btn.reset:hover {
  background: #c82333;
}

.kb-btn.cancel {
  background: #6c757d;
  color: white;
}

.kb-btn.cancel:hover {
  background: #5a6268;
}

.kb-status {
  margin-top: 10px;
  padding: 8px;
  border-radius: 4px;
  font-size: 0.8rem;
  display: none;
}

.kb-status.success {
  background: #d4edda;
  color: #155724;
  display: block;
}

.kb-status.error {
  background: #f8d7da;
  color: #721c24;
  display: block;
}

/* Modo oscuro para status */
html[data-theme="dark"] .kb-status.success {
  background: #1e4620;
  color: #7cd992;
}

html[data-theme="dark"] .kb-status.error {
  background: #4a1c1c;
  color: #f8a3a3;
}

/* === ANIMATIONS === */
@keyframes fadeIn { 
  from { opacity: 0; transform: translateY(8px); } 
  to { opacity: 1; transform: translateY(0); } 
}

/* === RESPONSIVE === */
@media (max-width: 480px) { 
  #chat-widget { 
    width: 95vw; 
    height: 70vh; 
    right: 2.5vw; 
  }
  .kb-editor-box {
    max-width: 95vw;
    padding: 15px;
  }
  #chat-widget-toggle {
    width: 45px;
    height: 45px;
    font-size: 18px;
  }
}

/* ======================================================================= */
/* === EDITOR VISUAL KB - DARK MODE FORZADO COMPLETO === */
/* ======================================================================= */

/* Modal base */
#kb-visual-editor-modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.8);
  z-index: 10002;
  justify-content: center;
  align-items: center;
}

#kb-visual-editor-modal.open {
  display: flex !important;
}

/* Box principal */
#kb-visual-editor-modal .kb-visual-box {
  background: var(--chat-input-bg) !important;
  width: 95%;
  max-width: 1200px;
  max-height: 90vh;
  border-radius: 12px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border: 1px solid var(--chat-border);
}

/* Header */
#kb-visual-editor-modal .kb-visual-header {
  background: var(--chat-primary) !important;
  color: white !important;
  padding: 15px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

#kb-visual-editor-modal .kb-visual-header h3,
#kb-visual-editor-modal .kb-visual-header h3 * {
  color: white !important;
  margin: 0;
}

#kb-visual-editor-modal .kb-visual-header .btn {
  background: rgba(255, 255, 255, 0.2) !important;
  border-color: rgba(255, 255, 255, 0.3) !important;
  color: white !important;
}

/* Tabs */
#kb-visual-editor-modal .kb-tabs-container {
  background: var(--chat-bg) !important;
  border-bottom: 1px solid var(--chat-border);
  min-height: 50px;
}

#kb-visual-editor-modal .kb-tab {
  background: var(--chat-bg) !important;
  color: var(--chat-text) !important;
  border-bottom: 3px solid transparent;
  padding: 12px 20px;
}

#kb-visual-editor-modal .kb-tab:hover {
  background: var(--chat-border) !important;
}

#kb-visual-editor-modal .kb-tab.active {
  background: var(--chat-primary) !important;
  color: white !important;
}

/* Content area */
#kb-visual-editor-modal #kb-tab-content {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  background: var(--chat-bg) !important;
  color: var(--chat-text) !important;
}

/* Títulos */
#kb-visual-editor-modal .kb-section-title {
  color: var(--chat-primary) !important;
  border-bottom-color: var(--chat-border);
}

/* ========================================================================= */
/* === CATEGORÍAS Y CONTENEDORES - CRÍTICO === */
/* ========================================================================= */

#kb-visual-editor-modal .kb-category,
#kb-visual-editor-modal .kb-category > *,
#kb-visual-editor-modal .kb-scroll-container {
  background: var(--chat-input-bg) !important;
  background-color: var(--chat-input-bg) !important;
  border-color: var(--chat-border) !important;
}

#kb-visual-editor-modal .kb-category h5,
#kb-visual-editor-modal .kb-category h5 * {
  color: var(--chat-text) !important;
  border-bottom-color: var(--chat-border);
}

#kb-visual-editor-modal .kb-category h5 code {
  background: var(--chat-bg) !important;
  color: var(--chat-primary) !important;
  border-color: var(--chat-border);
}

/* ========================================================================= */
/* === TABLAS - SOBRESCRIBIR BOOTSTRAP COMPLETAMENTE === */
/* ========================================================================= */

#kb-visual-editor-modal table,
#kb-visual-editor-modal .table,
#kb-visual-editor-modal .kb-category table,
#kb-visual-editor-modal .kb-category .table,
#kb-visual-editor-modal table.table,
#kb-visual-editor-modal .kb-category table.table {
  background: var(--chat-bg) !important;
  background-color: var(--chat-bg) !important;
  color: var(--chat-text) !important;
  border-color: var(--chat-border) !important;
}

#kb-visual-editor-modal table tbody,
#kb-visual-editor-modal .table tbody,
#kb-visual-editor-modal table thead,
#kb-visual-editor-modal .table thead {
  background: var(--chat-bg) !important;
  background-color: var(--chat-bg) !important;
}

#kb-visual-editor-modal table th,
#kb-visual-editor-modal .table th,
#kb-visual-editor-modal table.table-sm th,
#kb-visual-editor-modal .table-bordered th {
  background: var(--chat-border) !important;
  background-color: var(--chat-border) !important;
  color: var(--chat-text) !important;
  border-color: var(--chat-border) !important;
}

#kb-visual-editor-modal table td,
#kb-visual-editor-modal .table td,
#kb-visual-editor-modal table.table-sm td,
#kb-visual-editor-modal .table-bordered td {
  background: var(--chat-bg) !important;
  background-color: var(--chat-bg) !important;
  color: var(--chat-text) !important;
  border-color: var(--chat-border) !important;
}

#kb-visual-editor-modal table tr,
#kb-visual-editor-modal .table tr,
#kb-visual-editor-modal table tbody tr,
#kb-visual-editor-modal .table tbody tr {
  background: var(--chat-bg) !important;
  background-color: var(--chat-bg) !important;
  color: var(--chat-text) !important;
}

#kb-visual-editor-modal .table-hover tbody tr:hover,
#kb-visual-editor-modal table.table-hover tbody tr:hover {
  background: var(--chat-border) !important;
  background-color: var(--chat-border) !important;
}

#kb-visual-editor-modal .table-light,
#kb-visual-editor-modal table.table-light {
  background: var(--chat-bg) !important;
  background-color: var(--chat-bg) !important;
}

#kb-visual-editor-modal .table-bordered,
#kb-visual-editor-modal table.table-bordered {
  border-color: var(--chat-border) !important;
}

/* ========================================================================= */
/* === INPUTS Y TEXTAREAS - SOBRESCRIBIR BOOTSTRAP === */
/* ========================================================================= */

#kb-visual-editor-modal input,
#kb-visual-editor-modal textarea,
#kb-visual-editor-modal input.form-control,
#kb-visual-editor-modal textarea.form-control,
#kb-visual-editor-modal input.form-control-sm,
#kb-visual-editor-modal textarea.form-control-sm,
#kb-visual-editor-modal .kb-category input,
#kb-visual-editor-modal .kb-category textarea,
#kb-visual-editor-modal .kb-category input.form-control,
#kb-visual-editor-modal .kb-category textarea.form-control {
  background: var(--chat-bg) !important;
  background-color: var(--chat-bg) !important;
  border: 1px solid var(--chat-border) !important;
  border-color: var(--chat-border) !important;
  color: var(--chat-text) !important;
}

#kb-visual-editor-modal input:focus,
#kb-visual-editor-modal textarea:focus,
#kb-visual-editor-modal input.form-control:focus,
#kb-visual-editor-modal textarea.form-control:focus {
  background: var(--chat-bg) !important;
  background-color: var(--chat-bg) !important;
  border-color: var(--chat-primary) !important;
  color: var(--chat-text) !important;
  box-shadow: 0 0 0 0.2rem rgba(40, 167, 69, 0.15) !important;
}

#kb-visual-editor-modal input::placeholder,
#kb-visual-editor-modal textarea::placeholder {
  color: #888 !important;
  opacity: 1;
}

/* ========================================================================= */
/* === BOTONES === */
/* ========================================================================= */

#kb-visual-editor-modal .kb-visual-footer {
  background: var(--chat-bg) !important;
  border-top-color: var(--chat-border);
}

#kb-visual-editor-modal .btn-success {
  background-color: #28a745 !important;
  border-color: #28a745 !important;
  color: white !important;
}

#kb-visual-editor-modal .btn-info {
  background-color: #17a2b8 !important;
  border-color: #17a2b8 !important;
  color: white !important;
}

#kb-visual-editor-modal .btn-warning {
  background-color: #ffc107 !important;
  border-color: #ffc107 !important;
  color: #333 !important;
}

#kb-visual-editor-modal .btn-danger {
  background-color: #dc3545 !important;
  border-color: #dc3545 !important;
  color: white !important;
}

#kb-visual-editor-modal .btn-primary {
  background-color: var(--chat-primary) !important;
  border-color: var(--chat-primary) !important;
  color: white !important;
}

/* ========================================================================= */
/* === ELEMENTOS ESPECÍFICOS === */
/* ========================================================================= */

#kb-visual-editor-modal small,
#kb-visual-editor-modal small.text-muted {
  color: #888 !important;
}

#kb-visual-editor-modal .text-center,
#kb-visual-editor-modal .text-end {
  color: var(--chat-text) !important;
}

/* Scrollbars */
#kb-visual-editor-modal #kb-tab-content::-webkit-scrollbar,
#kb-visual-editor-modal .kb-scroll-container::-webkit-scrollbar {
  width: 6px;
}

#kb-visual-editor-modal #kb-tab-content::-webkit-scrollbar-thumb,
#kb-visual-editor-modal .kb-scroll-container::-webkit-scrollbar-thumb {
  background: var(--chat-border);
  border-radius: 3px;
}

/* ========================================================================= */
/* === MODO OSCURO EXPLÍCITO === */
/* ========================================================================= */

html[data-theme="dark"] #kb-visual-editor-modal .kb-category,
html[data-theme="dark"] #kb-visual-editor-modal table,
html[data-theme="dark"] #kb-visual-editor-modal .table,
html[data-theme="dark"] #kb-visual-editor-modal table th,
html[data-theme="dark"] #kb-visual-editor-modal .table th,
html[data-theme="dark"] #kb-visual-editor-modal table td,
html[data-theme="dark"] #kb-visual-editor-modal .table td,
html[data-theme="dark"] #kb-visual-editor-modal table tr,
html[data-theme="dark"] #kb-visual-editor-modal .table tr {
  background: var(--chat-bg) !important;
  background-color: var(--chat-bg) !important;
  color: var(--chat-text) !important;
  border-color: var(--chat-border) !important;
}

html[data-theme="dark"] #kb-visual-editor-modal input,
html[data-theme="dark"] #kb-visual-editor-modal textarea {
  background: var(--chat-bg) !important;
  background-color: var(--chat-bg) !important;
  color: var(--chat-text) !important;
  border-color: var(--chat-border) !important;
}

html[data-theme="dark"] #kb-visual-editor-modal .kb-category h5 code {
  background: var(--chat-bg) !important;
  color: #5cd85c !important;
}

/* ========================================================================= */
/* === RESPONSIVE === */
/* ========================================================================= */

@media (max-width: 768px) {
  #kb-visual-editor-modal .kb-visual-box {
    width: 98%;
  }
  
  #kb-visual-editor-modal .kb-visual-header h3 {
    font-size: 1rem;
  }
  
  #kb-visual-editor-modal .kb-visual-footer {
    flex-direction: column;
  }
}