:root {
    --navy-950: #0b1220;
    --navy-900: #0f1b33;
    --navy-800: #16264a;
    --navy-700: #1a365d;
    --teal-600: #2c7a7b;
    --teal-500: #319795;
    --teal-400: #38b2ac;
    --bg: #eef2f7;
    --card: #ffffff;
    --text: #1f2937;
    --muted: #6b7280;
    --border: #e5e9f0;
    --danger: #dc2626;
    --danger-bg: #fee2e2;
    --warning: #d97706;
    --warning-bg: #fef3c7;
    --success: #16a34a;
    --success-bg: #dcfce7;
    --radius: 12px;
    --shadow: 0 1px 2px rgba(15, 23, 42, 0.06), 0 4px 16px rgba(15, 23, 42, 0.06);
}

* { box-sizing: border-box; }
.hidden { display: none !important; }

body {
    margin: 0;
    font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, Roboto, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
}

a { color: var(--teal-600); text-decoration: none; }

/* ---------------------------------------------------------------- Login */
.login-page {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: radial-gradient(circle at top left, var(--navy-800), var(--navy-950));
}

.login-card {
    width: 100%;
    max-width: 380px;
    background: var(--card);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 36px 32px;
    border-top: 4px solid var(--teal-500);
}

.login-card h1 {
    font-size: 1.4rem;
    color: var(--navy-700);
    margin: 0 0 4px;
}

.login-card p.subtitle {
    color: var(--muted);
    margin: 0 0 24px;
    font-size: 0.85rem;
}

/* ---------------------------------------------------------------- Forms */
.field { margin-bottom: 16px; }
.field label { display: block; font-size: 0.8rem; font-weight: 600; margin-bottom: 6px; color: #374151; }
input, select, textarea {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 0.92rem;
    background: #fafbfc;
    color: var(--text);
    font-family: inherit;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--teal-400); border-color: transparent; }
textarea { resize: vertical; min-height: 80px; }

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 18px;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    font-size: 0.88rem;
    cursor: pointer;
    transition: filter 0.15s ease, transform 0.05s ease;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-block { width: 100%; }
.btn-primary { background: var(--navy-700); color: #fff; }
.btn-primary:hover { filter: brightness(1.1); }
.btn-teal { background: var(--teal-600); color: #fff; }
.btn-teal:hover { filter: brightness(1.1); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-outline { background: transparent; border: 1px solid var(--border); color: var(--text); }
.btn-sm { padding: 6px 12px; font-size: 0.78rem; }

.error-banner {
    background: var(--danger-bg);
    color: var(--danger);
    padding: 10px 12px;
    border-radius: 8px;
    font-size: 0.85rem;
    margin-bottom: 14px;
}
.success-banner {
    background: var(--success-bg);
    color: var(--success);
    padding: 10px 12px;
    border-radius: 8px;
    font-size: 0.85rem;
    margin-bottom: 14px;
}

/* ---------------------------------------------------------------- Shell */
.app-shell { display: flex; min-height: 100vh; }

.sidebar {
    width: 220px;
    background: linear-gradient(180deg, var(--navy-900), var(--navy-950));
    color: #cbd5e1;
    padding: 22px 16px;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
}
.sidebar h2 { color: #fff; font-size: 1.05rem; margin: 0 0 2px; }
.sidebar .role-tag { font-size: 0.72rem; color: var(--teal-400); text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 22px; }
.sidebar nav { display: flex; flex-direction: column; gap: 4px; flex: 1; }
.sidebar nav button {
    text-align: left;
    background: transparent;
    border: none;
    color: #cbd5e1;
    padding: 10px 12px;
    border-radius: 8px;
    font-size: 0.85rem;
    cursor: pointer;
}
.sidebar nav button.active, .sidebar nav button:hover { background: rgba(255,255,255,0.08); color: #fff; }
.sidebar .footer-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }

.main { flex: 1; padding: 26px 32px; overflow-y: auto; }
.topbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 22px; }
.topbar h1 { font-size: 1.3rem; margin: 0; color: var(--navy-700); }

.status-pill {
    display: inline-flex; align-items: center; gap: 6px;
    padding: 5px 12px; border-radius: 999px; font-size: 0.78rem; font-weight: 600;
}
.status-pill.working { background: var(--success-bg); color: var(--success); }
.status-pill.break { background: var(--warning-bg); color: var(--warning); }
.status-pill.dialing { background: #fef9c3; color: #a16207; }
.status-pill.in_call { background: #dbeafe; color: #1d4ed8; }
.status-pill.post_call { background: #ede9fe; color: #6d28d9; }
.status-pill.offline { background: #e5e7eb; color: #4b5563; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; }

.presence-dot {
    display: inline-block; width: 9px; height: 9px; border-radius: 50%;
    flex-shrink: 0;
}
.presence-dot.online { background: var(--success); box-shadow: 0 0 0 2px var(--success-bg); }
.presence-dot.offline { background: var(--danger); box-shadow: 0 0 0 2px var(--danger-bg); }

.call-indicator {
    display: inline-flex; align-items: center; justify-content: center;
    width: 22px; height: 22px; border-radius: 50%; font-size: 0.8rem; flex-shrink: 0;
}
.call-indicator.on-call { background: #dbeafe; color: #1d4ed8; }
.call-indicator.idle { background: #f1f5f9; color: #cbd5e1; }

/* ---------------------------------------------------------------- Cards */
.grid { display: grid; gap: 18px; }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
@media (max-width: 900px) { .grid-3, .grid-4, .grid-2 { grid-template-columns: 1fr; } }

.card {
    background: var(--card);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 20px;
}
.card h3 { margin: 0 0 12px; font-size: 0.95rem; color: var(--navy-700); }
.metric-card { text-align: center; }
.metric-card .value { font-size: 2rem; font-weight: 700; color: var(--navy-700); }
.metric-card .label { font-size: 0.78rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.03em; }

.progress-bar { height: 10px; border-radius: 999px; background: #e5e9f0; overflow: hidden; }
.progress-bar > div { height: 100%; background: linear-gradient(90deg, var(--teal-500), var(--teal-400)); transition: width 0.4s ease; }

/* --------------------------------------------------------------- Tables */
.table-scroll {
    overflow: auto;
    max-height: 66vh;
    border-radius: 10px;
    cursor: grab;
    scrollbar-width: thin;
    scrollbar-color: #cbd5e1 #f1f5f9;
}
.table-scroll.dragging { cursor: grabbing; user-select: none; }
.table-scroll::-webkit-scrollbar { height: 10px; width: 10px; }
.table-scroll::-webkit-scrollbar-track { background: #f1f5f9; border-radius: 999px; }
.table-scroll::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 999px; }
.table-scroll::-webkit-scrollbar-thumb:hover { background: #94a3b8; }

table { width: 100%; border-collapse: collapse; font-size: 0.82rem; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; vertical-align: middle; }
th {
    color: var(--muted); font-weight: 600; font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.02em;
    position: sticky; top: 0; background: var(--card); z-index: 2; box-shadow: 0 1px 0 var(--border);
}
tr:hover td { background: #f8fafc; }

/* Fila compacta de edición de contacto (tel./extensión) */
.contact-edit { display: flex; align-items: center; gap: 4px; }
.contact-edit input {
    width: 68px; padding: 5px 7px; font-size: 0.78rem; border-radius: 6px;
}
.icon-btn {
    display: inline-flex; align-items: center; justify-content: center;
    width: 26px; height: 26px; border-radius: 6px; border: none;
    background: var(--teal-600); color: #fff; cursor: pointer; font-size: 0.85rem; flex-shrink: 0;
}
.icon-btn:hover { filter: brightness(1.1); }

/* Columna combinada de actividad: presencia + en llamada */
.activity-cell { display: flex; align-items: center; gap: 8px; }

.badge { display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: 0.72rem; font-weight: 600; }
.badge-score-high { background: var(--success-bg); color: var(--success); }
.badge-score-mid { background: var(--warning-bg); color: var(--warning); }
.badge-score-low { background: #e5e7eb; color: #4b5563; }

/* ----------------------------------------------------------------- Zona Cero */
.zona-cero { max-width: 1180px; margin: 0 auto; padding-bottom: 96px; }
/* Espacio para el softphone dock (abajo-izquierda) sin tapar la ficha */
.zona-cero #lead-container { padding-bottom: 72px; }
.zona-cero-grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(320px, 400px);
    gap: 18px;
    align-items: start;
}
.lead-card { border-top: 4px solid var(--teal-500); }
.lead-empty { text-align: center; padding: 60px 20px; color: var(--muted); }
.actions-row { display: flex; gap: 10px; margin-top: 18px; flex-wrap: wrap; }
.script-box { background: #f0fdfa; border: 1px solid #99f6e4; border-radius: 10px; padding: 12px 14px; font-size: 0.85rem; color: #115e59; margin-top: 12px; }

/* Copiloto de ventas (junto a la llamada) */
.copilot-panel {
    background: #0f172a;
    color: #e2e8f0;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    min-height: 520px;
    max-height: calc(100vh - 120px);
    position: sticky;
    top: 16px;
    overflow: hidden;
}
.copilot-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 10px;
    padding: 16px 16px 10px;
    border-bottom: 1px solid rgba(148, 163, 184, 0.25);
}
.copilot-header h3 { margin: 0; font-size: 1rem; color: #f8fafc; }
.copilot-header p { margin: 4px 0 0; font-size: 0.72rem; color: #94a3b8; }
.copilot-header .btn { color: #e2e8f0; border-color: #475569; background: transparent; }
.copilot-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 10px 14px;
}
.copilot-chips .chip {
    border: 1px solid #334155;
    background: #1e293b;
    color: #cbd5e1;
    border-radius: 999px;
    padding: 5px 10px;
    font-size: 0.72rem;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}
.copilot-chips .chip:hover { background: #334155; border-color: var(--teal-500); color: #fff; }
.copilot-brief {
    padding: 0 14px 8px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    max-height: 120px;
    overflow-y: auto;
}
.copilot-brief-item {
    background: #1e293b;
    border: 1px solid #334155;
    border-radius: 8px;
    padding: 8px 10px;
    font-size: 0.72rem;
    color: #cbd5e1;
    cursor: pointer;
}
.copilot-brief-item strong { display: block; color: #5eead4; margin-bottom: 2px; font-size: 0.7rem; }
.copilot-messages {
    flex: 1;
    overflow-y: auto;
    padding: 12px 14px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    background: linear-gradient(180deg, #0f172a 0%, #111827 100%);
}
.copilot-empty { font-size: 0.8rem; color: #94a3b8; line-height: 1.45; padding: 8px 4px; }
.copilot-bubble {
    max-width: 95%;
    padding: 10px 12px;
    border-radius: 12px;
    font-size: 0.82rem;
    line-height: 1.45;
    white-space: pre-wrap;
    word-break: break-word;
}
.copilot-bubble.user { align-self: flex-end; background: var(--teal-600); color: #fff; }
.copilot-bubble.assistant { align-self: flex-start; background: #1e293b; border: 1px solid #334155; color: #e2e8f0; }
.copilot-bubble .meta { display: block; font-size: 0.68rem; color: #94a3b8; margin-bottom: 4px; }
.copilot-bubble .copy-btn {
    margin-top: 8px;
    font-size: 0.68rem;
    border: 1px solid #475569;
    background: transparent;
    color: #94a3b8;
    border-radius: 6px;
    padding: 3px 8px;
    cursor: pointer;
}
.copilot-bubble .copy-btn:hover { color: #fff; border-color: #94a3b8; }
.copilot-composer {
    display: flex;
    gap: 8px;
    padding: 12px 14px 14px;
    border-top: 1px solid rgba(148, 163, 184, 0.25);
    background: #0b1220;
}
.copilot-composer textarea {
    flex: 1;
    resize: none;
    border-radius: 10px;
    border: 1px solid #334155;
    background: #1e293b;
    color: #f1f5f9;
    padding: 10px 12px;
    font-size: 0.85rem;
    font-family: inherit;
}
.copilot-composer textarea::placeholder { color: #64748b; }

.kb-item {
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 12px 14px;
    margin-bottom: 10px;
    background: #fff;
}
.kb-item-header { display: flex; justify-content: space-between; gap: 10px; align-items: flex-start; }
.kb-item h4 { margin: 0 0 4px; font-size: 0.95rem; }
.kb-item .kb-cat { font-size: 0.72rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.kb-item p { margin: 8px 0 0; font-size: 0.85rem; color: #334155; white-space: pre-wrap; }
.kb-item .kb-actions { display: flex; gap: 6px; flex-shrink: 0; }

/* Softphone Zadarma: dock inferior IZQUIERDA (no tapa el Copiloto a la derecha).
   Nunca tocar #zdrm-webRTCRemoteView / audio: son fullscreen y rompen el layout. */
.zdrm-webphone-box.zdrm-phone,
.zdrm-phone.zdrm-webphone-box {
    position: fixed !important;
    left: 236px !important;   /* justo a la derecha del sidebar (220px) */
    bottom: 18px !important;
    right: auto !important;
    top: auto !important;
    margin: 0 !important;
    z-index: 900 !important;
    max-width: min(420px, calc(100vw - 460px));
    height: auto !important;
    background: #fff !important;
    border-radius: 40px !important;
    box-shadow: 0 8px 28px rgba(15, 23, 42, 0.22) !important;
}

/* Videos/audio de Zadarma: deben quedarse ocultos; si reciben z-index alto
   generan la caja gris enorme sobre el Copiloto. */
#zdrm-webRTCRemoteView,
#zdrm-webRTCSelfView {
    display: none !important;
    visibility: hidden !important;
    pointer-events: none !important;
    z-index: -2 !important;
    width: 1px !important;
    height: 1px !important;
    opacity: 0 !important;
    left: -9999px !important;
    top: -9999px !important;
}

@media (max-width: 980px) {
    .zona-cero-grid { grid-template-columns: 1fr; }
    .copilot-panel { position: static; max-height: none; min-height: 420px; }
    .zdrm-webphone-box.zdrm-phone,
    .zdrm-phone.zdrm-webphone-box {
        left: 12px !important;
        bottom: 12px !important;
        max-width: calc(100vw - 24px);
    }
}

/* ------------------------------------------------------------------ Modal */
.modal-overlay {
    position: fixed; inset: 0; background: rgba(15, 23, 42, 0.55);
    display: flex; align-items: center; justify-content: center; z-index: 1000;
}
.modal-box { background: #fff; border-radius: var(--radius); padding: 26px; width: 100%; max-width: 460px; box-shadow: var(--shadow); }
.modal-box.blocking { border-top: 5px solid var(--danger); }
.modal-box h3 { margin-top: 0; }

/* ------------------------------------------------------------------ Toast */
#toast-container { position: fixed; top: 18px; right: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 1100; }
.toast { background: var(--navy-900); color: #fff; padding: 12px 16px; border-radius: 8px; font-size: 0.85rem; box-shadow: var(--shadow); min-width: 220px; }
.toast.error { background: var(--danger); }
.toast.success { background: var(--success); }

/* --------------------------------------------------------------- Chat */
.chat-box { display: flex; flex-direction: column; height: 320px; }
.chat-messages { flex: 1; overflow-y: auto; padding: 8px 4px; display: flex; flex-direction: column; gap: 8px; }
.chat-bubble { max-width: 75%; padding: 8px 12px; border-radius: 12px; font-size: 0.83rem; background: #f1f5f9; align-self: flex-start; word-break: break-word; }
.chat-bubble.mine { background: var(--teal-600); color: #fff; align-self: flex-end; }
.chat-input-row { display: flex; gap: 8px; margin-top: 8px; }

/* Panel de chat "entre todos" (directorio + conversación) */
.chat-panel { display: flex; height: 65vh; padding: 0; overflow: hidden; }
.chat-sidebar {
    width: 260px; flex-shrink: 0; border-right: 1px solid var(--border);
    display: flex; flex-direction: column; background: #fafbfc;
}
.chat-sidebar input[type="search"] {
    margin: 12px; width: calc(100% - 24px); border-radius: 999px; background: #fff;
}
.chat-contacts-list { flex: 1; overflow-y: auto; padding: 0 8px 12px; }
.chat-contact-item {
    display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 8px;
    cursor: pointer; margin-bottom: 2px; position: relative;
}
.chat-contact-item:hover { background: #eef2f7; }
.chat-contact-item.active { background: var(--teal-600); }
.chat-contact-item.active .chat-contact-name, .chat-contact-item.active .chat-contact-role { color: #fff; }
.chat-contact-info { flex: 1; min-width: 0; }
.chat-contact-name { font-size: 0.85rem; font-weight: 600; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-contact-role { font-size: 0.7rem; color: var(--muted); }
.chat-unread-badge {
    background: var(--danger); color: #fff; font-size: 0.68rem; font-weight: 700;
    border-radius: 999px; min-width: 18px; height: 18px; display: flex; align-items: center; justify-content: center; padding: 0 5px;
}

.chat-main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.chat-main-header {
    padding: 14px 18px; border-bottom: 1px solid var(--border); font-weight: 600; color: var(--navy-700); font-size: 0.92rem;
}
.chat-main .chat-messages { padding: 16px 18px; }
.chat-main .chat-input-row { padding: 12px 16px; border-top: 1px solid var(--border); margin-top: 0; }

/* Notificación (punto rojo) en el ítem del menú lateral "Chat" */
.nav-btn { position: relative; }
.nav-btn.has-notification::after {
    content: ''; position: absolute; top: 8px; right: 10px; width: 8px; height: 8px;
    border-radius: 50%; background: var(--danger); box-shadow: 0 0 0 2px var(--navy-900);
}

/* ------------------------------------------------------------ Leaderboard */
.leaderboard-item { display: flex; align-items: center; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid var(--border); }
.leaderboard-item:last-child { border-bottom: none; }
.rank-badge { width: 26px; height: 26px; border-radius: 50%; background: var(--navy-700); color: #fff; display: flex; align-items: center; justify-content: center; font-size: 0.78rem; font-weight: 700; margin-right: 10px; }
.rank-badge.gold { background: #d97706; }
.rank-badge.silver { background: #94a3b8; }
.rank-badge.bronze { background: #b45309; }

.empty-state { text-align: center; padding: 40px 10px; color: var(--muted); font-size: 0.88rem; }

/* -------------------------------------------------------------- Zadarma */
.zadarma-header {
    display: flex; align-items: center; justify-content: space-between;
    background: linear-gradient(135deg, var(--navy-900), var(--navy-700));
    border-radius: var(--radius); padding: 22px 26px; margin-bottom: 18px; color: #fff;
}
.zadarma-brand { display: flex; align-items: center; gap: 14px; }
.zadarma-logo {
    width: 46px; height: 46px; border-radius: 12px; background: rgba(255,255,255,0.12);
    display: flex; align-items: center; justify-content: center; font-size: 1.4rem; flex-shrink: 0;
}
.zadarma-header h2 { margin: 0; font-size: 1.15rem; }
.zadarma-header p { margin: 2px 0 0; font-size: 0.8rem; color: #cbd5e1; }
.zadarma-balance-chip {
    background: rgba(255,255,255,0.1); border-radius: 10px; padding: 10px 18px; text-align: right;
}
.zadarma-balance-chip .label { display: block; font-size: 0.7rem; color: #cbd5e1; text-transform: uppercase; letter-spacing: 0.04em; }
.zadarma-balance-chip .value { display: block; font-size: 1.35rem; font-weight: 700; color: var(--teal-400); }

.auto-dialer-card { border-top: 4px solid var(--teal-500); }
.auto-dialer-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
.auto-dialer-subtitle { font-size: 0.82rem; color: var(--muted); margin: 0; max-width: 480px; }
.auto-dialer-state {
    display: inline-block; margin-top: 10px; font-size: 0.8rem; font-weight: 600;
    padding: 4px 12px; border-radius: 999px; background: var(--danger-bg); color: var(--danger);
}
.auto-dialer-state.active { background: var(--success-bg); color: var(--success); }

.toggle-switch {
    position: relative; width: 52px; height: 28px; border-radius: 999px; border: none;
    background: #cbd5e1; cursor: pointer; flex-shrink: 0; transition: background 0.2s ease;
}
.toggle-switch[data-enabled="true"] { background: var(--success); }
.toggle-knob {
    position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%;
    background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,0.3); transition: transform 0.2s ease;
}
.toggle-switch[data-enabled="true"] .toggle-knob { transform: translateX(24px); }

.mini-metric { background: #f8fafc; border-radius: 10px; padding: 14px; text-align: center; }
.mini-metric.secondary { background: #f0fdfa; }
.mini-metric-value { font-size: 1.5rem; font-weight: 700; color: var(--navy-700); }
.mini-metric-label { font-size: 0.72rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.02em; margin-top: 2px; }
