:root {
  --bg-body: #0f172a;
  --bg-surface: #1e293b;
  --bg-elevated: #334155;
  --border-color: #475569;
  --text-primary: #f8fafc;
  --text-secondary: #cbd5e1;
  --text-muted: #94a3b8;
  --accent-primary: #3b82f6;
  --accent-hover: #2563eb;
  --success: #10b981;
  --warning: #f59e0b;
  --danger: #ef4444;
  --radius: 8px;
  --font-sans: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-mono: "JetBrains Mono", Consolas, monospace;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  background-color: var(--bg-body);
  color: var(--text-primary);
  font-family: var(--font-sans);
  line-height: 1.5;
  font-size: 14px;
}

/* Accesibilidad WCAG 2.1 AA: Foco visible */
:focus-visible {
  outline: 2px solid var(--accent-primary);
  outline-offset: 2px;
}

/* Header Superior */
.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 24px;
  background-color: var(--bg-surface);
  border-bottom: 1px solid var(--border-color);
  position: sticky;
  top: 0;
  z-index: 50;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-badge {
  background: var(--accent-primary);
  color: #fff;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 12px;
  letter-spacing: 0.5px;
}

.brand h1 {
  font-size: 17px;
  font-weight: 600;
}

.role-switcher {
  display: flex;
  gap: 8px;
}

/* Botones y Controles */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: var(--radius);
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
  border: 1px solid var(--border-color);
  background: var(--bg-elevated);
  color: var(--text-primary);
  transition: background 0.15s ease, border-color 0.15s ease;
}

.btn:hover {
  background: #475569;
}

.btn-primary {
  background: var(--accent-primary);
  border-color: var(--accent-primary);
  color: #ffffff;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-success {
  background: var(--success);
  border-color: var(--success);
  color: #052e16;
}

.btn-danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #ffffff;
}

.btn-sm {
  padding: 5px 10px;
  font-size: 12px;
}

/* Layout Principal */
.container {
  max-width: 1380px;
  margin: 0 auto;
  padding: 24px;
}

.auth-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  padding: 14px 18px;
  border-radius: var(--radius);
  margin-bottom: 24px;
}

.auth-bar label {
  font-weight: 600;
  color: var(--text-secondary);
}

.input,
.select,
.textarea {
  background: var(--bg-body);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 13px;
  font-family: inherit;
  width: 100%;
}

.textarea {
  min-height: 90px;
  resize: vertical;
}

.input-error {
  border-color: var(--danger) !important;
  box-shadow: 0 0 0 2px rgba(239, 68, 68, 0.25);
}

.grid-2 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(420px, 1fr));
  gap: 20px;
  margin-bottom: 24px;
}

.grid-3 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 20px;
  margin-bottom: 24px;
}

.card {
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border-color);
}

.card-header h2 {
  font-size: 15px;
  font-weight: 600;
}

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

.form-group label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 4px;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

/* Tablas de Alta Densidad */
.table-wrapper {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

th,
td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border-color);
}

th {
  color: var(--text-secondary);
  font-weight: 600;
  background: rgba(15, 23, 42, 0.5);
}

.mono {
  font-family: var(--font-mono);
  font-size: 12px;
}

/* Badges de Estado */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
}

.badge-active,
.badge-connected {
  background: rgba(16, 185, 129, 0.2);
  color: #34d399;
  border: 1px solid #059669;
}

.badge-paused,
.badge-qrcode,
.badge-connecting {
  background: rgba(245, 158, 11, 0.2);
  color: #fbbf24;
  border: 1px solid #d97706;
}

.badge-inactive,
.badge-disconnected,
.badge-blocked {
  background: rgba(239, 68, 68, 0.2);
  color: #f87171;
  border: 1px solid #dc2626;
}

/* Caja de Código QR */
.qr-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #ffffff;
  color: #0f172a;
  padding: 16px;
  border-radius: var(--radius);
  margin-top: 12px;
  min-height: 220px;
}

.qr-box img {
  width: 260px;
  max-width: 100%;
  height: auto;
  image-rendering: pixelated;
}

/* Historial de Chat */
.chat-box {
  background: var(--bg-body);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 14px;
  height: 320px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 12px;
}

.chat-bubble {
  max-width: 80%;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 13px;
  white-space: pre-wrap;
}

.chat-user {
  align-self: flex-start;
  background: var(--bg-elevated);
  border-left: 3px solid var(--accent-primary);
}

.chat-assistant {
  align-self: flex-end;
  background: rgba(59, 130, 246, 0.2);
  border-right: 3px solid var(--accent-primary);
}

.chat-human {
  align-self: flex-end;
  background: rgba(16, 185, 129, 0.18);
  border-right: 3px solid var(--success);
}

.live-feed-box {
  background: var(--bg-body);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 10px;
  max-height: 360px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.live-feed-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-radius: 6px;
  background: var(--bg-surface);
  border-left: 3px solid var(--accent-primary);
}

.live-feed-item.status-paused_human {
  border-left-color: var(--warning);
}

.live-thread-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 10px 12px;
  background: var(--bg-body);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  margin-bottom: 10px;
}

.chat-meta {
  font-size: 10px;
  color: var(--text-muted);
  margin-bottom: 3px;
}

.hidden {
  display: none !important;
}

/* Pantalla de Login Bloqueante + CAPTCHA Visual */
.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: radial-gradient(circle at top, rgba(59, 130, 246, 0.16), transparent 55%), var(--bg-body);
}

.login-card {
  width: 100%;
  max-width: 440px;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  padding: 28px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.45);
}

.login-header {
  text-align: center;
  margin-bottom: 22px;
}

.login-header h1 {
  font-size: 20px;
  margin: 10px 0 6px;
}

.login-header p {
  font-size: 13px;
  color: var(--text-secondary);
}

.captcha-box {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}

.captcha-box img {
  height: 58px;
  border-radius: 8px;
  border: 1px solid var(--border-color);
  background: #0f172a;
}

.alert-banner {
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 12.5px;
  margin-bottom: 14px;
  border: 1px solid var(--border-color);
}

.alert-error {
  background: rgba(239, 68, 68, 0.14);
  border-color: rgba(239, 68, 68, 0.45);
  color: #fca5a5;
}

.alert-warning {
  background: rgba(245, 158, 11, 0.14);
  border-color: rgba(245, 158, 11, 0.45);
  color: #fde68a;
}

.alert-success {
  background: rgba(16, 185, 129, 0.14);
  border-color: rgba(16, 185, 129, 0.45);
  color: #6ee7b7;
}

.header-user-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.user-pill {
  font-size: 12px;
  padding: 5px 10px;
  border-radius: 999px;
  background: rgba(59, 130, 246, 0.14);
  border: 1px solid rgba(59, 130, 246, 0.35);
  color: var(--text-primary);
}

/* Modales Ligeros */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(2, 6, 23, 0.78);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal-panel {
  width: 100%;
  max-width: 480px;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  padding: 22px;
  box-shadow: 0 20px 45px rgba(0, 0, 0, 0.55);
}

/* Notificaciones Toast */
#toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  background: var(--bg-surface);
  border-left: 4px solid var(--accent-primary);
  color: var(--text-primary);
  padding: 12px 16px;
  border-radius: 6px;
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.5);
  font-size: 13px;
  min-width: 280px;
}

/* Reglas de Impresión (@media print) según directivas UI/UX */
@media print {
  body {
    background: #ffffff;
    color: #000000;
  }
  .app-header,
  .auth-bar,
  .btn,
  #toast-container {
    display: none !important;
  }
  .card {
    border: 1px solid #cccccc;
    break-inside: avoid;
    background: #ffffff;
    color: #000000;
  }
}
