/* ==========================================================================
   LOYALTEL · UI REFRESH LAYER
   --------------------------------------------------------------------------
   Capa de pulido visual y de experiencia cargada DESPUÉS de los estilos
   propios de cada panel. No redefine la estructura: unifica detalles de
   interacción (foco, hover, transiciones), corrige comportamientos nativos
   y mejora la lectura de tablas, formularios, modales y estados vacíos.
   ========================================================================== */

/* ---------- Tokens compartidos ---------- */
:root {
  --ui-primary: #2563eb;
  --ui-primary-strong: #1d4ed8;
  --ui-ring: 0 0 0 3px rgba(37, 99, 235, 0.18);
  --ui-ring-danger: 0 0 0 3px rgba(239, 68, 68, 0.18);
  --ui-surface: #ffffff;
  --ui-surface-soft: #f8fafc;
  --ui-line: #e2e8f0;
  --ui-line-strong: #cbd5e1;
  --ui-text: #0f172a;
  --ui-muted: #64748b;
  --ui-radius: 12px;
  --ui-ease: cubic-bezier(0.16, 1, 0.3, 1);
  --ui-shadow-card: 0 1px 3px rgba(15, 23, 42, 0.05), 0 12px 28px -22px rgba(15, 23, 42, 0.35);
}

/* ---------- Correcciones de base ---------- */
/* El atributo hidden debe ganar siempre a los display de las hojas de estilo. */
[hidden] {
  display: none !important;
}

html {
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
}

body {
  text-rendering: optimizeLegibility;
}

::selection {
  background: rgba(37, 99, 235, 0.2);
  color: var(--ui-text);
}

img,
svg {
  -webkit-user-drag: none;
}

/* ---------- Scrollbars más legibles ---------- */
* {
  scrollbar-width: thin;
  scrollbar-color: #cbd5e1 transparent;
}

*::-webkit-scrollbar {
  width: 9px;
  height: 9px;
}

*::-webkit-scrollbar-track {
  background: transparent;
}

*::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border: 2px solid transparent;
  border-radius: 999px;
  background-clip: content-box;
}

*::-webkit-scrollbar-thumb:hover {
  background: #94a3b8;
  background-clip: content-box;
}

/* Contenedores de tablas anchas: barra siempre visible y cómoda. */
.table-responsive,
.table-wrap,
.table-scroll,
.table-container,
.users-table-container,
.usuarios-table-container {
  scrollbar-width: auto;
  scrollbar-color: #94a3b8 #eef2f7;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-x: contain;
}

.table-responsive::-webkit-scrollbar,
.table-wrap::-webkit-scrollbar,
.table-scroll::-webkit-scrollbar,
.table-container::-webkit-scrollbar,
.users-table-container::-webkit-scrollbar,
.usuarios-table-container::-webkit-scrollbar {
  height: 11px;
  width: 11px;
}

.table-responsive::-webkit-scrollbar-thumb,
.table-wrap::-webkit-scrollbar-thumb,
.table-scroll::-webkit-scrollbar-thumb,
.table-container::-webkit-scrollbar-thumb,
.users-table-container::-webkit-scrollbar-thumb,
.usuarios-table-container::-webkit-scrollbar-thumb {
  background: #94a3b8;
  border: 2px solid #eef2f7;
  border-radius: 999px;
}

/* ---------- Accesibilidad: foco visible consistente ---------- */
a:focus-visible,
button:focus-visible,
[role="button"]:focus-visible,
[role="tab"]:focus-visible,
summary:focus-visible,
.tab:focus-visible,
.nav:focus-visible,
.filter-chip:focus-visible,
.pagination button:focus-visible {
  outline: 2px solid var(--ui-primary);
  outline-offset: 2px;
}

input[type="checkbox"]:focus-visible,
input[type="radio"]:focus-visible,
input[type="range"]:focus-visible {
  outline: 2px solid var(--ui-primary);
  outline-offset: 2px;
}

/* Botones neutros: refuerzo por si el panel no define variante. */
.btn-secondary {
  background: #ffffff;
  border-color: var(--ui-line);
  color: #475569;
}

.btn-secondary:hover {
  background: #f8fafc;
  border-color: var(--ui-line-strong);
  color: var(--ui-text);
}

/* ---------- Botones: micro-interacciones ---------- */
.btn,
.btn-modern,
.modal-btn,
.refresh-btn,
.action-btn,
.btn-logout,
.btn-subtle-dark,
.filter-chip,
.tab,
.nav,
.user-menu button {
  transition:
    background-color 0.18s ease,
    border-color 0.18s ease,
    color 0.18s ease,
    box-shadow 0.18s ease,
    transform 0.12s ease;
}

.btn:active,
.btn-modern:active,
.modal-btn:active,
.action-btn:active {
  transform: translateY(1px) scale(0.995);
}

.btn:disabled,
.btn-modern:disabled,
.modal-btn:disabled,
button[aria-busy="true"] {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
  pointer-events: none;
}

/* Iconos dentro de botones siempre alineados. */
.btn svg,
.btn-modern svg,
.modal-btn svg,
.action-btn svg {
  flex-shrink: 0;
}

/* ---------- Formularios ---------- */
input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="file"]),
select,
textarea {
  transition:
    border-color 0.16s ease,
    box-shadow 0.16s ease,
    background-color 0.16s ease;
}

input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="file"]):focus,
select:focus,
textarea:focus {
  box-shadow: 0 0 0 3px var(--primary-glow, rgba(37, 99, 235, 0.18));
}

input:disabled,
select:disabled,
textarea:disabled,
button:disabled {
  opacity: 0.68;
  cursor: not-allowed;
}

textarea {
  resize: vertical;
}

input::placeholder,
textarea::placeholder {
  opacity: 0.75;
}

.form-hint {
  display: block;
  margin-top: 5px;
  font-size: 11px;
  font-weight: 500;
  line-height: 1.4;
  color: var(--ui-muted);
}

.form-group > label {
  display: block;
  margin-bottom: 5px;
  font-size: 11.5px;
  font-weight: 700;
  letter-spacing: 0.2px;
  color: #475569;
}

/* Modal de nueva conversación del Asesor: formulario autocontenido. */
#newConversationModal .modal-content {
  padding: 20px 22px 18px;
  overflow-y: auto;
}

#newConversationModal .modal-content > h3 {
  margin: 0 0 14px;
  font-size: 15px;
  font-weight: 800;
  letter-spacing: -0.2px;
  color: var(--ui-text);
}

#newConversationModal .nc-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

#newConversationModal .nc-header h3 {
  margin: 0;
  font-size: 15px;
  font-weight: 800;
  letter-spacing: -0.2px;
  color: var(--ui-text);
}

#newConversationModal .nc-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  flex-shrink: 0;
  border: 1px solid var(--ui-line);
  border-radius: 8px;
  background: #ffffff;
  color: #64748b;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

#newConversationModal .nc-close:hover {
  background: #fef2f2;
  border-color: #fecaca;
  color: #dc2626;
}

#newConversationModal .form-group {
  margin-bottom: 12px;
}

#newConversationModal label {
  display: block;
  margin-bottom: 5px;
  font-size: 11.5px;
  font-weight: 700;
  letter-spacing: 0.2px;
  color: #475569;
}

#newConversationModal input,
#newConversationModal select {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--ui-line);
  border-radius: 9px;
  background: #ffffff;
  color: var(--ui-text);
  font: inherit;
  font-size: 13px;
  outline: none;
}

#newConversationModal input:focus,
#newConversationModal select:focus {
  border-color: var(--ui-primary);
  box-shadow: 0 0 0 3px var(--primary-glow, rgba(37, 99, 235, 0.18));
}

#newConversationModal .modal-actions {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 14px;
}

#newConversationModal .btn-cancel,
#newConversationModal .btn-save {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 9px 18px;
  border-radius: 9px;
  font-family: inherit;
  font-size: 12.5px;
  font-weight: 700;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease, transform 0.12s ease;
}

#newConversationModal .btn-cancel {
  background: #ffffff;
  border: 1px solid var(--ui-line);
  color: #475569;
}

#newConversationModal .btn-cancel:hover {
  background: #f1f5f9;
  color: var(--ui-text);
}

#newConversationModal .btn-save {
  background: var(--ui-primary);
  border: 1px solid var(--ui-primary);
  color: #ffffff;
}

#newConversationModal .btn-save:hover {
  background: var(--ui-primary-strong);
  border-color: var(--ui-primary-strong);
}

/* Autofill coherente con el tema claro/oscuro de cada panel. */
input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus {
  -webkit-text-fill-color: currentColor;
  transition: background-color 5000s ease-in-out 0s;
}

/* ---------- Tarjetas y superficies ---------- */
.card,
.flow-card,
.stat-card,
.metric,
.panel,
.chat-container,
.workspace-card {
  border-radius: 16px;
}

.card,
.flow-card {
  box-shadow: var(--ui-shadow-card);
}

.stat-card:hover,
.metric:hover {
  border-color: var(--ui-line-strong);
}

/* ---------- Tablas ---------- */
table {
  border-collapse: collapse;
}

/* Matrices extensas: lectura en una sola línea + desplazamiento horizontal. */
table.table-wide th,
table.table-wide td {
  white-space: nowrap;
}

/* Filas alternas para escanear tablas largas con mayor rapidez. */
table.table-wide tbody tr:nth-child(even) td,
.users-table tbody tr:nth-child(even) td {
  background: #fbfcfe;
}

table.table-wide tbody tr:hover td,
.users-table tbody tr:hover td {
  background: #f1f5f9;
}

thead th {
  white-space: nowrap;
}

tbody tr {
  transition: background-color 0.12s ease;
}

/* Evita que celdas con mucho texto rompan el ancho de la tabla. */
tbody td {
  overflow-wrap: anywhere;
}

/* ---------- Tabs / filtros ---------- */
.tabs {
  max-width: 100%;
  overflow-x: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.tabs::-webkit-scrollbar {
  display: none;
}

.tab {
  white-space: nowrap;
  flex: 0 0 auto;
}

.filter-chip {
  flex: 0 0 auto;
}

/* ---------- Listas de conversaciones (Asesor) ---------- */
.conversation-item {
  transition:
    background-color 0.15s ease,
    box-shadow 0.15s ease,
    opacity 0.15s ease;
}

.conversation-item:not(.active):hover {
  box-shadow: inset 3px 0 0 #bfdbfe;
}

.conversation-item.active {
  box-shadow:
    inset 3px 0 0 var(--ui-primary),
    0 6px 18px -18px rgba(37, 99, 235, 0.65);
}

/* ---------- Estados vacíos ---------- */
.empty-state {
  border-radius: 14px;
}

.empty-state-icon {
  transition: transform 0.25s var(--ui-ease);
}

.empty-state:hover .empty-state-icon {
  transform: translateY(-2px);
}

/* ---------- Modales ---------- */
.modal-content,
.modern-modal {
  max-height: calc(100vh - 40px);
  overscroll-behavior: contain;
}

.modal.active .modal-content,
.modal.show .modal-content,
.modern-modal-backdrop .modern-modal {
  animation: ui-modal-in 0.24s var(--ui-ease) both;
}

.modal-backdrop:not([hidden]) .modal {
  animation: ui-modal-in 0.24s var(--ui-ease) both;
}

@keyframes ui-modal-in {
  from {
    opacity: 0;
    transform: translateY(12px) scale(0.985);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.modal-content img,
.modern-modal img {
  max-width: 100%;
  height: auto;
}

.modal-close,
.modal-x,
.close-modal {
  transition:
    background-color 0.16s ease,
    color 0.16s ease,
    transform 0.16s ease;
}

.modal-close:hover,
.modal-x:hover,
.close-modal:hover {
  transform: rotate(90deg);
}

/* ---------- Navegación lateral ---------- */
.nav,
.sidebar-nav a,
.sidebar-nav button {
  transition:
    background-color 0.16s ease,
    color 0.16s ease,
    padding-left 0.16s ease;
}

.nav:hover {
  padding-left: 20px;
}

/* ---------- Pie de página legible ---------- */
.app-footer {
  color: #64748b;
}

.app-footer .foot-online,
body > footer .foot-online {
  color: #047857;
}

/* ---------- Texto secundario con contraste AA ---------- */
/* Muchos paneles usan #94a3b8 (2.56:1 sobre blanco). Se eleva a #64748b. */
.panel-sub,
.empty {
  color: #64748b !important;
}

.empty-state,
.empty-state p {
  color: #64748b !important;
}

.flow-step-desc,
.cobranza-item-name small,
.cobranza-item-meta span,
.cobranza-amount-box span,
.cobranza-amount-box small,
.cobranza-metric small,
.cob-step-copy small,
.cob-summary-cell span,
.cob-timeline-top span,
.conversation-time,
.conversation-phone {
  color: #475569;
}

.conversation-preview {
  color: #475569;
}

.finalizada-badge {
  color: #475569;
}

.conversation-item.finalizada {
  opacity: 1;
}

.cobranza-item-meta strong {
  color: #0f766e;
}

/* ---------- Chips / badges ---------- */
.live-chip,
.badge {
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}

/* ---------- Utilidades nuevas ---------- */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.ui-scroll-hint {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  font-weight: 600;
  color: var(--ui-muted);
  margin: 0 0 8px 2px;
  user-select: none;
}

.ui-scroll-hint span {
  display: inline-block;
  animation: ui-nudge 2.4s ease-in-out infinite;
}

@keyframes ui-nudge {
  0%, 100% { transform: translateX(0); }
  50% { transform: translateX(4px); }
}

.ui-loading-dots {
  display: inline-flex;
  gap: 4px;
  align-items: center;
}

.ui-loading-dots i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
  opacity: 0.35;
  animation: ui-dot 1s ease-in-out infinite;
}

.ui-loading-dots i:nth-child(2) { animation-delay: 0.15s; }
.ui-loading-dots i:nth-child(3) { animation-delay: 0.3s; }

@keyframes ui-dot {
  0%, 100% { opacity: 0.3; transform: translateY(0); }
  50% { opacity: 1; transform: translateY(-2px); }
}

.ui-kbd {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  height: 20px;
  padding: 0 5px;
  border: 1px solid var(--ui-line);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: #f8fafc;
  color: var(--ui-muted);
  font-size: 10px;
  font-weight: 700;
  font-family: inherit;
}

/* ---------- Responsive compartido ---------- */
@media (max-width: 1180px) {
  .stats-grid,
  .cards {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }
}

@media (max-width: 760px) {
  .stats-grid,
  .cards {
    grid-template-columns: 1fr !important;
  }

  main {
    padding: 18px 16px !important;
  }

  .content {
    padding: 20px 16px 28px !important;
  }

  .flow-diagram {
    justify-content: flex-start !important;
  }

  .modal-content,
  .modern-modal {
    max-height: calc(100vh - 24px);
  }
}

/* ---------- Modales de conversación (Asesor y Back Office) ---------- */
.modal .form-group {
  margin-bottom: 14px;
}

.modal .form-group:last-child {
  margin-bottom: 0;
}

.modal .form-group > label {
  display: block;
  font-size: 12px;
  font-weight: 700;
  color: #334155;
  margin-bottom: 6px;
}

.modal .form-input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border, #e2e8f0);
  border-radius: 10px;
  font-size: 13px;
  font-family: inherit;
  background: #f8fafc;
  color: #0f172a;
  outline: none;
  transition: border-color 0.18s, box-shadow 0.18s, background 0.18s;
}

.modal .form-input:focus {
  background: #fff;
  border-color: #2563eb;
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.14);
}

.modal textarea.form-input {
  resize: vertical;
  min-height: 76px;
}

.tag-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 4px 0 16px;
}

.tag-picker-empty {
  width: 100%;
  padding: 14px;
  border: 1px dashed #cbd5e1;
  border-radius: 10px;
  text-align: center;
  font-size: 12px;
  color: #64748b;
  background: #f8fafc;
}

.tag-picker .tag-chip {
  padding: 5px 6px 5px 10px;
  font-size: 11.5px;
}

.tag-picker .tag-chip button {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.tag-picker .tag-chip button:hover {
  background: rgba(255, 255, 255, 0.28);
}

.swatch-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.swatch {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  padding: 0;
  transition: transform 0.15s, box-shadow 0.15s;
}

.swatch:hover {
  transform: translateY(-1px);
}

.swatch[aria-pressed="true"] {
  border-color: #0f172a;
  box-shadow: 0 0 0 3px #fff, 0 0 0 5px rgba(15, 23, 42, 0.18);
}

.interactive-buttons {
  display: grid;
  gap: 10px;
}

.interactive-buttons .form-group {
  margin-bottom: 0;
}

@media (min-width: 640px) {
  .interactive-buttons {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

/* ---------- Campos de formulario en modales ---------- */
.password-field {
  position: relative;
  margin-bottom: 14px;
}

.password-field input {
  margin: 0 !important;
  padding-right: 44px !important;
}

.password-toggle {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: #64748b;
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background 0.18s, color 0.18s;
}

.password-toggle:hover {
  background: #e2e8f0;
  color: #0f172a;
}

.field-hint {
  display: block;
  font-size: 11.5px;
  line-height: 1.45;
  color: #64748b;
  margin: -7px 0 12px;
}

.field-hint[hidden] {
  display: none;
}

.modal-content .is-invalid,
.modal .form-input.is-invalid {
  border-color: #dc2626 !important;
  box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.12) !important;
}

.form-grid-2 {
  display: grid;
  gap: 0 14px;
}

@media (min-width: 560px) {
  .form-grid-2 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

.switch-line {
  display: flex !important;
  align-items: center;
  gap: 9px;
  text-transform: none !important;
  letter-spacing: 0 !important;
  font-size: 12.5px !important;
  font-weight: 600 !important;
  color: #334155 !important;
  cursor: pointer;
  margin: 2px 0 14px;
}

.switch-line input[type="checkbox"] {
  width: 17px !important;
  height: 17px;
  margin: 0 !important;
  flex-shrink: 0;
  accent-color: #2563eb;
}

/* ---------- Movimiento reducido ---------- */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}
