/* ============================================================================
   KEPPLER AI v4.3 — LAYOUT EJECUTIVO COMPACTO + PESTAÑAS ANIDADAS (NIVEL 1 + 2)
   Cero menús laterales largos · Densidad ejecutiva · Cero overflow horizontal
   ============================================================================ */

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

html, body {
  width: 100%;
  max-width: 100vw;
  min-height: 100vh;
  overflow-x: hidden;
  background-color: var(--bg-app);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 12.5px;
  line-height: 1.38;
  -webkit-font-smoothing: antialiased;
}

.kp-mono {
  font-family: var(--font-mono) !important;
  font-variant-numeric: tabular-nums;
}

::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
::-webkit-scrollbar-track {
  background: var(--bg-app);
}
::-webkit-scrollbar-thumb {
  background: var(--border-active);
  border-radius: 3px;
}

.kp-app-shell {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  width: 100%;
  max-width: 100vw;
  overflow-x: hidden;
}

/* ============================================================================
   HEADER EJECUTIVO SUPERIOR (FIJO Y COMPACTO — 56px)
   ============================================================================ */
.kp-header {
  position: sticky;
  top: 0;
  z-index: 60;
  height: var(--header-h);
  min-height: var(--header-h);
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-subtle);
  padding: 0 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  max-width: 100%;
}

.kp-header__brand {
  display: flex;
  align-items: center;
  gap: 9px;
  min-width: 0;
  flex-shrink: 1;
}

.kp-logo-mark {
  width: 30px;
  height: 30px;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, var(--accent-emerald), var(--accent-blue));
  color: #ffffff;
  font-weight: 800;
  font-size: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  letter-spacing: -0.5px;
}

.kp-brand-text {
  display: flex;
  align-items: center;
  gap: 7px;
  min-width: 0;
}

.kp-brand-title {
  font-weight: 700;
  font-size: 13px;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.kp-tenant-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 8px;
  border-radius: var(--radius-pill);
  font-size: 10.5px;
  font-weight: 600;
  background: var(--accent-blue-soft);
  color: var(--accent-blue);
  border: 1px solid var(--accent-blue-border);
  white-space: nowrap;
}

.kp-header__controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
  min-width: 0;
}

.kp-select,
.kp-input,
.kp-textarea {
  background: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  padding: 5px 9px;
  font-size: 12px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.14s ease, background-color 0.14s ease;
  max-width: 100%;
}

.kp-select:focus,
.kp-input:focus,
.kp-textarea:focus {
  border-color: var(--accent-blue);
}

.kp-header-select {
  height: 32px;
  font-weight: 600;
  cursor: pointer;
}

/* Interruptor SHIELD (Retención de Borradores) */
.kp-shield-toggle {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 32px;
  padding: 0 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--accent-emerald-border);
  background: var(--accent-emerald-soft);
  color: var(--accent-emerald);
  font-size: 11.5px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
}

.kp-shield-toggle.is-off {
  border-color: var(--accent-amber-border);
  background: var(--accent-amber-soft);
  color: var(--accent-amber);
}

.kp-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: currentColor;
  display: inline-block;
  flex-shrink: 0;
}

/* Botones Ejecutivos */
.kp-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  height: 30px;
  padding: 0 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-subtle);
  background: var(--bg-card);
  color: var(--text-primary);
  font-size: 11.5px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 0.14s ease, border-color 0.14s ease, color 0.14s ease;
}

.kp-btn:hover {
  background: var(--bg-hover);
  border-color: var(--border-active);
}

.kp-btn--emerald {
  background: var(--accent-emerald);
  border-color: var(--accent-emerald);
  color: #ffffff;
}
.kp-btn--emerald:hover {
  filter: brightness(1.07);
  background: var(--accent-emerald);
  border-color: var(--accent-emerald);
}

.kp-btn--blue {
  background: var(--accent-blue);
  border-color: var(--accent-blue);
  color: #ffffff;
}
.kp-btn--blue:hover {
  filter: brightness(1.08);
  background: var(--accent-blue);
  border-color: var(--accent-blue);
}

.kp-btn--amber {
  background: var(--accent-amber-soft);
  border-color: var(--accent-amber-border);
  color: var(--accent-amber);
}

.kp-btn--coral {
  background: var(--accent-coral-soft);
  border-color: var(--accent-coral-border);
  color: var(--accent-coral);
}

.kp-btn--ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text-secondary);
  height: 26px;
  padding: 0 8px;
  font-size: 11px;
}
.kp-btn--ghost:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
  border-color: var(--border-subtle);
}

.kp-btn--sm {
  height: 25px;
  padding: 0 8px;
  font-size: 11px;
}

/* ============================================================================
   DOBLE BARRA DE PESTAÑAS HORIZONTAL (NIVEL 1 + NIVEL 2)
   ============================================================================ */
.kp-nav-l1 {
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-subtle);
  padding: 0 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: var(--nav-l1-h);
  width: 100%;
  max-width: 100%;
}

.kp-nav-l1__tabs {
  display: flex;
  align-items: center;
  gap: 4px;
  overflow-x: auto;
  scrollbar-width: none;
  max-width: 100%;
}
.kp-nav-l1__tabs::-webkit-scrollbar {
  display: none;
}

.kp-tab-l1 {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: var(--nav-l1-h);
  padding: 0 12px;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
  transition: color 0.14s ease, background-color 0.14s ease;
}

.kp-tab-l1:hover {
  color: var(--text-primary);
  background: var(--bg-hover);
}

.kp-tab-l1.is-active {
  color: var(--text-primary);
}

.kp-tab-l1.is-active::after {
  content: "";
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: 0;
  height: 2px;
  background: var(--accent-emerald);
  border-radius: 2px 2px 0 0;
}

.kp-tab-count {
  padding: 1px 6px;
  border-radius: var(--radius-pill);
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: 700;
  background: var(--bg-card);
  color: var(--text-secondary);
  border: 1px solid var(--border-subtle);
}

.kp-tab-l1.is-active .kp-tab-count {
  background: var(--accent-emerald-soft);
  color: var(--accent-emerald);
  border-color: var(--accent-emerald-border);
}

/* Barra Secundaria Nivel 2 */
.kp-nav-l2 {
  background: var(--bg-card);
  border-bottom: 1px solid var(--border-subtle);
  padding: 4px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: var(--nav-l2-h);
  width: 100%;
  max-width: 100%;
}

.kp-nav-l2__tabs {
  display: flex;
  align-items: center;
  gap: 5px;
  overflow-x: auto;
  scrollbar-width: none;
  min-width: 0;
  flex: 1;
}
.kp-nav-l2__tabs::-webkit-scrollbar {
  display: none;
}

.kp-tab-l2 {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 27px;
  padding: 0 10px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-secondary);
  font-size: 11.5px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.14s ease;
}

.kp-tab-l2:hover {
  color: var(--text-primary);
  background: var(--bg-hover);
}

.kp-tab-l2.is-active {
  background: var(--bg-surface);
  color: var(--text-primary);
  border-color: var(--border-active);
}

.kp-nav-l2__actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

/* ============================================================================
   ÁREA PRINCIPAL DE CONTENIDO Y SISTEMA DE TARJETAS COLAPSABLES
   ============================================================================ */
.kp-main {
  flex: 1;
  padding: 12px 14px 20px;
  width: 100%;
  max-width: 1600px;
  margin: 0 auto;
  min-width: 0;
}

.kp-grid-2 {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.kp-grid-3 {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.kp-grid-4 {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

/* Tarjeta Colapsable Anti-Saturación */
.kp-card {
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-card);
  min-width: 0;
  overflow: hidden;
  margin-bottom: 10px;
}

.kp-card:last-child {
  margin-bottom: 0;
}

.kp-card__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-subtle);
  cursor: pointer;
  user-select: none;
}

.kp-card.is-collapsed .kp-card__header {
  border-bottom-color: transparent;
}

.kp-card.is-collapsed .kp-card__body {
  display: none;
}

.kp-card__title-group {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.kp-card__title {
  font-size: 12.5px;
  font-weight: 700;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.kp-card__subtitle {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.kp-card__collapse-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-subtle);
  background: var(--bg-card);
  color: var(--text-secondary);
  font-size: 12px;
  cursor: pointer;
  flex-shrink: 0;
}

.kp-card__collapse-btn:hover {
  color: var(--text-primary);
  border-color: var(--border-active);
}

.kp-card__body {
  padding: 10px 12px;
  min-width: 0;
}

/* Badges Semánticos */
.kp-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 7px;
  border-radius: var(--radius-pill);
  font-size: 10.5px;
  font-weight: 700;
  border: 1px solid var(--border-subtle);
  background: var(--bg-surface);
  color: var(--text-secondary);
  white-space: nowrap;
}

.kp-badge--emerald {
  background: var(--accent-emerald-soft);
  color: var(--accent-emerald);
  border-color: var(--accent-emerald-border);
}

.kp-badge--amber {
  background: var(--accent-amber-soft);
  color: var(--accent-amber);
  border-color: var(--accent-amber-border);
}

.kp-badge--blue {
  background: var(--accent-blue-soft);
  color: var(--accent-blue);
  border-color: var(--accent-blue-border);
}

.kp-badge--coral {
  background: var(--accent-coral-soft);
  color: var(--accent-coral);
  border-color: var(--accent-coral-border);
}

/* ============================================================================
   1.1 BANDEJA ACTIVA — LAYOUT WHATSAPP 3 COLUMNAS COMPACTAS
   ============================================================================ */
.kp-inbox-layout {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr) 300px;
  gap: 10px;
  align-items: start;
  min-width: 0;
}

.kp-chat-list {
  display: flex;
  flex-direction: column;
  gap: 5px;
  max-height: 500px;
  overflow-y: auto;
}

.kp-chat-item {
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 8px 9px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-subtle);
  background: var(--bg-surface);
  cursor: pointer;
  text-align: left;
  width: 100%;
  color: inherit;
  font-family: inherit;
  transition: background-color 0.12s ease, border-color 0.12s ease;
}

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

.kp-chat-item.is-selected {
  border-color: var(--accent-emerald);
  background: var(--bg-hover);
}

.kp-chat-feed {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 290px;
  min-height: 210px;
  overflow-y: auto;
  padding: 10px;
  background: var(--bg-app);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
}

.kp-msg {
  max-width: 86%;
  padding: 7px 10px;
  border-radius: var(--radius-md);
  font-size: 12px;
  line-height: 1.38;
  word-break: break-word;
}

.kp-msg--in {
  align-self: flex-start;
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  color: var(--text-primary);
}

.kp-msg--out {
  align-self: flex-end;
  background: var(--accent-emerald-soft);
  border: 1px solid var(--accent-emerald-border);
  color: var(--text-primary);
}

.kp-msg__meta {
  display: flex;
  justify-content: flex-end;
  gap: 6px;
  margin-top: 3px;
  font-size: 10px;
  color: var(--text-muted);
  font-family: var(--font-mono);
}

/* Caja de Borrador Retenido por SHIELD */
.kp-shield-box {
  margin-top: 9px;
  padding: 9px 10px;
  border-radius: var(--radius-md);
  border: 1px solid var(--accent-emerald-border);
  background: var(--accent-emerald-soft);
}

.kp-shield-box.is-bypassed {
  border-color: var(--accent-amber-border);
  background: var(--accent-amber-soft);
}

/* Tablas Ejecutivas Compactas */
.kp-table-wrap {
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
}

.kp-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 11.8px;
  text-align: left;
}

.kp-table th {
  background: var(--bg-surface);
  color: var(--text-secondary);
  font-weight: 700;
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 7px 9px;
  border-bottom: 1px solid var(--border-subtle);
  white-space: nowrap;
}

.kp-table td {
  padding: 7px 9px;
  border-bottom: 1px solid var(--border-subtle);
  color: var(--text-primary);
  vertical-align: middle;
}

.kp-table tbody tr:last-child td {
  border-bottom: none;
}

.kp-table tbody tr:hover {
  background: var(--bg-hover);
}

/* KPIs Ejecutivos */
.kp-kpi {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 10px 12px;
  min-width: 0;
}

.kp-kpi__label {
  font-size: 11px;
  color: var(--text-secondary);
  margin-bottom: 4px;
}

.kp-kpi__value {
  font-family: var(--font-mono);
  font-size: 19px;
  font-weight: 700;
  color: var(--text-primary);
}

.kp-kpi__delta {
  font-family: var(--font-mono);
  font-size: 10.5px;
  margin-top: 3px;
}

/* Sandbox Dividido (Módulo 3.4) */
.kp-sandbox-split {
  display: grid;
  grid-template-columns: minmax(0, 1.15fr) minmax(0, 0.85fr);
  gap: 10px;
  min-width: 0;
}

.kp-trace-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 320px;
  overflow-y: auto;
}

.kp-trace-item {
  padding: 7px 9px;
  border-radius: var(--radius-sm);
  background: var(--bg-app);
  border: 1px solid var(--border-subtle);
  font-size: 11.5px;
}

/* ============================================================================
   MODAL DE ROLLBACK 1-CLIC & TOASTS
   ============================================================================ */
.kp-modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 120;
  background: var(--overlay-backdrop);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
}

.kp-modal-backdrop[hidden] {
  display: none !important;
}

.kp-modal {
  width: 100%;
  max-width: 620px;
  max-height: 88vh;
  overflow-y: auto;
  background: var(--bg-surface);
  border: 1px solid var(--border-active);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-modal);
}

.kp-modal__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border-subtle);
}

.kp-modal__body {
  padding: 12px 14px;
}

.kp-toast-stack {
  position: fixed;
  bottom: 14px;
  right: 14px;
  z-index: 150;
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-width: min(360px, calc(100vw - 28px));
}

.kp-toast {
  padding: 9px 12px;
  border-radius: var(--radius-md);
  background: var(--bg-surface);
  border: 1px solid var(--accent-emerald-border);
  color: var(--text-primary);
  box-shadow: var(--shadow-modal);
  font-size: 11.5px;
  font-weight: 600;
}

/* Utilidades Flex */
.kp-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.kp-between {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
}

.kp-stack {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* ============================================================================
   PORTÓN EJECUTIVO DE ACCESO PRIVADO (HOMÓLOGO A MARIA.KEPPLERIA.COM)
   ============================================================================ */
:root[data-auth="locked"] .kp-app-shell {
  display: none !important;
}

:root[data-auth="unlocked"] .kp-login-gate {
  display: none !important;
}

.kp-login-gate {
  min-height: 100vh;
  width: 100%;
  max-width: 100vw;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  background: radial-gradient(circle at 50% 0%, rgba(16, 185, 129, 0.08), transparent 52%), var(--bg-app);
  padding: 14px 16px;
}

.kp-login-gate__bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  max-width: 1120px;
  width: 100%;
  margin: 0 auto;
}

.kp-login-gate__center {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px 0;
  width: 100%;
}

.kp-login-card {
  width: 100%;
  max-width: 400px;
  background: var(--bg-surface);
  border: 1px solid var(--border-active);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-modal);
  padding: 24px 22px;
}

.kp-login-card__seal {
  width: 44px;
  height: 44px;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, var(--accent-emerald), var(--accent-blue));
  color: #ffffff;
  font-weight: 800;
  font-size: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 12px;
}

.kp-login-card__title {
  text-align: center;
  font-size: 18px;
  font-weight: 800;
  color: var(--text-primary);
  letter-spacing: -0.02em;
}

.kp-login-card__sub {
  text-align: center;
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 3px;
  margin-bottom: 18px;
}

.kp-login-field-wrap {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%;
}

.kp-login-field-wrap .kp-input {
  width: 100%;
  height: 38px;
  padding-right: 68px;
  font-size: 13px;
}

.kp-login-eye-btn {
  position: absolute;
  right: 5px;
  height: 28px;
  padding: 0 8px;
  font-size: 11px;
}

.kp-login-error {
  min-height: 18px;
  margin: 8px 0;
  font-size: 11.5px;
  font-weight: 600;
  color: var(--accent-coral);
}

.kp-login-guarantees {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 11px;
  color: var(--text-secondary);
}

.kp-login-gate__footer {
  text-align: center;
  font-size: 11px;
  color: var(--text-muted);
  padding-top: 8px;
}

/* ============================================================================
   ADAPTACIÓN MÓVIL ESTRICTA (<= 1080px y Android 393x873 — CERO OVERFLOW)
   ============================================================================ */
@media (max-width: 1120px) {
  .kp-inbox-layout {
    grid-template-columns: 1fr;
  }
  .kp-sandbox-split {
    grid-template-columns: 1fr;
  }
  .kp-grid-4 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .kp-grid-3 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 760px) {
  .kp-header {
    height: auto;
    min-height: var(--header-h);
    padding: 7px 10px;
    flex-wrap: wrap;
    gap: 6px;
  }
  .kp-header__brand {
    width: 100%;
    justify-content: space-between;
  }
  .kp-header__controls {
    width: 100%;
    flex-wrap: wrap;
    gap: 5px;
  }
  .kp-header-select {
    flex: 1 1 135px;
    min-width: 0;
    font-size: 11px;
    height: 29px;
    padding: 3px 6px;
  }
  .kp-shield-toggle {
    height: 29px;
    padding: 0 8px;
    font-size: 10.5px;
  }
  .kp-nav-l1,
  .kp-nav-l2 {
    padding-left: 8px;
    padding-right: 8px;
  }
  .kp-nav-l2 {
    flex-wrap: wrap;
    gap: 5px;
  }
  .kp-nav-l2__tabs {
    width: 100%;
    flex: 1 1 100%;
  }
  .kp-nav-l2__actions {
    width: 100%;
    justify-content: flex-end;
  }
  .kp-main {
    padding: 8px;
  }
  .kp-grid-4,
  .kp-grid-3,
  .kp-grid-2 {
    grid-template-columns: 1fr;
  }
  .kp-card__subtitle {
    display: none;
  }
}
