/* ── Reset y base ─────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --c-bg:        #f4f6f8;
    --c-surface:   #ffffff;
    --c-border:    #e2e8f0;
    --c-border-l:  #edf2f7;
    --c-text-1:    #1a202c;
    --c-text-2:    #4a5568;
    --c-text-3:    #718096;
    --c-primary:   #2563eb;
    --c-primary-h: #1d4ed8;
    --c-primary-l: #eff6ff;
    --c-success:   #16a34a;
    --c-success-l: #f0fdf4;
    --c-danger:    #dc2626;
    --c-danger-l:  #fef2f2;
    --c-warning:   #d97706;
    --c-warning-l: #fffbeb;
    --c-msg-in:    #ffffff;
    --c-msg-out:   #e7f3ff;
    --sidebar-w:   220px;
    --header-h:    56px;
    --radius:      6px;
    --radius-lg:   10px;
    --shadow:      0 1px 3px rgba(0,0,0,.08);
    --shadow-md:   0 4px 12px rgba(0,0,0,.1);
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    font-size: 14px;
    color: var(--c-text-1);
}

body { background: var(--c-bg); height: 100vh; overflow: hidden; }

.hidden { display: none !important; }

/* ── Botones ──────────────────────────────────────────────────────── */
.btn-primary {
    display: inline-flex; align-items: center; gap: 6px;
    background: var(--c-primary); color: #fff;
    border: none; border-radius: var(--radius);
    padding: 7px 14px; font-size: 13px; font-weight: 500;
    cursor: pointer; transition: background .15s;
}
.btn-primary:hover { background: var(--c-primary-h); }
.btn-primary:disabled { opacity: .5; cursor: not-allowed; }

.btn-secondary {
    display: inline-flex; align-items: center; gap: 6px;
    background: var(--c-surface); color: var(--c-text-2);
    border: 1px solid var(--c-border); border-radius: var(--radius);
    padding: 6px 12px; font-size: 13px; font-weight: 400;
    cursor: pointer; transition: border-color .15s, background .15s;
}
.btn-secondary:hover { border-color: var(--c-text-3); background: var(--c-bg); }

.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-full { width: 100%; justify-content: center; padding: 10px; }

.btn-icon {
    display: inline-flex; align-items: center; justify-content: center;
    width: 32px; height: 32px; padding: 0;
    background: transparent; border: none; border-radius: var(--radius);
    cursor: pointer; color: var(--c-text-3);
    transition: background .15s, color .15s;
}
.btn-icon:hover { background: var(--c-border-l); color: var(--c-text-1); }
.btn-icon svg { width: 18px; height: 18px; fill: currentColor; }

.btn-logout {
    background: transparent; border: none; cursor: pointer;
    color: var(--c-text-3); padding: 6px;
    border-radius: var(--radius); transition: color .15s, background .15s;
    display: flex; align-items: center;
}
.btn-logout:hover { color: var(--c-danger); background: var(--c-danger-l); }
.btn-logout svg { width: 18px; height: 18px; fill: currentColor; }

/* ── Login ────────────────────────────────────────────────────────── */
.view-login {
    display: flex; align-items: center; justify-content: center;
    min-height: 100vh; background: var(--c-bg);
}

.login-card {
    background: var(--c-surface); border: 1px solid var(--c-border);
    border-radius: var(--radius-lg); padding: 36px 40px;
    width: 380px; box-shadow: var(--shadow-md);
}

.login-logo {
    display: flex; flex-direction: column; align-items: center;
    margin-bottom: 28px;
}
.login-logo-text {
    font-size: 18px; font-weight: 700; color: var(--c-text-1);
    letter-spacing: -.3px;
}
.login-logo-sub {
    font-size: 11px; font-weight: 500; color: var(--c-primary);
    letter-spacing: 2px; text-transform: uppercase; margin-top: 2px;
}

.login-error {
    background: var(--c-danger-l); color: var(--c-danger);
    border: 1px solid #fecaca; border-radius: var(--radius);
    padding: 8px 12px; font-size: 13px; margin-bottom: 14px;
}

/* ── Formularios ──────────────────────────────────────────────────── */
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 14px; }
.field label { font-size: 12px; font-weight: 500; color: var(--c-text-2); }
.field input, .field select, .field textarea {
    border: 1px solid var(--c-border); border-radius: var(--radius);
    padding: 8px 10px; font-size: 13px; font-family: inherit;
    background: var(--c-surface); color: var(--c-text-1);
    transition: border-color .15s, box-shadow .15s;
    outline: none;
}
.field input:focus, .field select:focus, .field textarea:focus {
    border-color: var(--c-primary);
    box-shadow: 0 0 0 3px rgba(37,99,235,.1);
}
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.checkbox-label { display: flex; align-items: center; gap: 8px; cursor: pointer; font-size: 13px; }

/* ── App layout ───────────────────────────────────────────────────── */
.view-app {
    display: flex; height: 100vh; overflow: hidden;
}

/* ── Sidebar ──────────────────────────────────────────────────────── */
.sidebar {
    width: var(--sidebar-w); flex-shrink: 0;
    background: #1e293b; color: #94a3b8;
    display: flex; flex-direction: column;
    border-right: 1px solid #0f172a;
}

.sidebar-header {
    height: var(--header-h); padding: 0 16px;
    display: flex; align-items: center;
    border-bottom: 1px solid rgba(255,255,255,.06);
}
.sidebar-brand {
    font-size: 14px; font-weight: 600;
    color: #f1f5f9; letter-spacing: -.3px;
}

.sidebar-nav {
    flex: 1; padding: 8px 0; overflow-y: auto;
}

.nav-item {
    display: flex; align-items: center; gap: 10px;
    padding: 9px 16px; font-size: 13px; font-weight: 400;
    color: #94a3b8; text-decoration: none;
    border-left: 3px solid transparent;
    transition: background .15s, color .15s, border-color .15s;
    position: relative;
}
.nav-item svg { width: 16px; height: 16px; fill: currentColor; flex-shrink: 0; }
.nav-item:hover { background: rgba(255,255,255,.05); color: #e2e8f0; }
.nav-item.active {
    background: rgba(37,99,235,.15);
    color: #93c5fd; border-left-color: var(--c-primary);
}

.nav-badge {
    margin-left: auto;
    background: var(--c-danger); color: #fff;
    font-size: 10px; font-weight: 600;
    padding: 1px 6px; border-radius: 10px;
    min-width: 18px; text-align: center;
}

.sidebar-footer {
    padding: 12px 16px; border-top: 1px solid rgba(255,255,255,.06);
    display: flex; align-items: center; gap: 8px;
}
.user-info { flex: 1; min-width: 0; }
.user-name { display: block; font-size: 13px; color: #e2e8f0; font-weight: 500; truncate: ellipsis; white-space: nowrap; overflow: hidden; }
.user-role { display: block; font-size: 11px; color: #64748b; text-transform: capitalize; }

/* ── Main content ─────────────────────────────────────────────────── */
.main-content {
    flex: 1; overflow: hidden; display: flex; flex-direction: column;
}

.view-content { flex: 1; display: flex; flex-direction: column; height: 100%; overflow: hidden; }

/* ── Bandeja — layout ─────────────────────────────────────────────── */
.inbox-layout {
    display: flex; height: 100%; overflow: hidden;
}

/* ── Panel de conversaciones ──────────────────────────────────────── */
.conv-panel {
    width: 300px; flex-shrink: 0;
    border-right: 1px solid var(--c-border);
    background: var(--c-surface);
    display: flex; flex-direction: column; overflow: hidden;
}

.conv-panel-header {
    height: var(--header-h); padding: 0 14px;
    display: flex; align-items: center; justify-content: space-between;
    border-bottom: 1px solid var(--c-border);
}
.conv-panel-header h2 { font-size: 14px; font-weight: 600; }

.conv-filters {
    display: flex; gap: 4px; padding: 8px 10px;
    border-bottom: 1px solid var(--c-border-l);
    flex-wrap: wrap;
}
.filter-btn {
    background: transparent; border: 1px solid var(--c-border);
    border-radius: 4px; padding: 3px 9px; font-size: 11px;
    color: var(--c-text-2); cursor: pointer; transition: all .15s;
}
.filter-btn:hover { border-color: var(--c-primary); color: var(--c-primary); }
.filter-btn.active {
    background: var(--c-primary); color: #fff; border-color: var(--c-primary);
}

.conv-search { padding: 6px 10px; border-bottom: 1px solid var(--c-border-l); }
.conv-search input {
    width: 100%; border: 1px solid var(--c-border); border-radius: var(--radius);
    padding: 6px 10px; font-size: 12px; background: var(--c-bg); outline: none;
    transition: border-color .15s;
}
.conv-search input:focus { border-color: var(--c-primary); }

.conv-list { flex: 1; overflow-y: auto; }

.conv-item {
    display: block; padding: 11px 14px;
    border-bottom: 1px solid var(--c-border-l);
    cursor: pointer; text-decoration: none; color: inherit;
    transition: background .1s;
    position: relative;
}
.conv-item:hover { background: var(--c-bg); }
.conv-item.active { background: var(--c-primary-l); border-left: 3px solid var(--c-primary); }

.conv-item-header {
    display: flex; justify-content: space-between; align-items: flex-start;
    margin-bottom: 2px;
}
.conv-item-name {
    font-size: 13px; font-weight: 500; color: var(--c-text-1);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    max-width: 160px;
}
.conv-item-time { font-size: 11px; color: var(--c-text-3); white-space: nowrap; }
.conv-item-phone { font-size: 11px; color: var(--c-text-3); margin-bottom: 3px; }
.conv-item-preview {
    font-size: 12px; color: var(--c-text-2);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    display: flex; justify-content: space-between; align-items: center;
}
.conv-unread-badge {
    background: var(--c-primary); color: #fff;
    font-size: 10px; font-weight: 600;
    padding: 1px 6px; border-radius: 10px;
    min-width: 18px; text-align: center; flex-shrink: 0;
}
.conv-window-dot {
    width: 7px; height: 7px; border-radius: 50%;
    background: var(--c-success); display: inline-block;
    margin-left: 4px; flex-shrink: 0;
}

.loading-state, .empty-state {
    display: flex; align-items: center; justify-content: center;
    color: var(--c-text-3); font-size: 13px;
    padding: 40px 20px; text-align: center;
}

/* ── Panel de chat ────────────────────────────────────────────────── */
.chat-panel {
    flex: 1; display: flex; flex-direction: column;
    overflow: hidden; background: var(--c-bg);
    position: relative;
}

.chat-empty {
    flex: 1; display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    color: var(--c-text-3);
}
.chat-empty-icon svg {
    width: 48px; height: 48px; fill: var(--c-border);
    margin-bottom: 12px;
}
.chat-empty p { font-size: 14px; }

.chat-active { flex: 1; display: flex; flex-direction: column; overflow: hidden; }

/* Header del chat */
.chat-header {
    height: var(--header-h); padding: 0 16px;
    background: var(--c-surface); border-bottom: 1px solid var(--c-border);
    display: flex; align-items: center; justify-content: space-between;
    flex-shrink: 0;
}
.chat-header-info { display: flex; align-items: center; gap: 10px; }
.chat-avatar {
    width: 36px; height: 36px; border-radius: 50%;
    background: var(--c-primary); color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-size: 14px; font-weight: 600; flex-shrink: 0;
}
.chat-contact-name { font-size: 14px; font-weight: 600; }
.chat-contact-phone { font-size: 12px; color: var(--c-text-3); }

.chat-header-actions { display: flex; align-items: center; gap: 6px; }

.window-badge {
    padding: 3px 9px; border-radius: 12px; font-size: 11px; font-weight: 500;
}
.window-open  { background: var(--c-success-l); color: var(--c-success); }
.window-closed { background: var(--c-danger-l);  color: var(--c-danger); }

/* Mensajes */
.chat-messages {
    flex: 1; overflow-y: auto;
    padding: 16px; display: flex; flex-direction: column; gap: 6px;
}

.msg-row { display: flex; }
.msg-row.in  { justify-content: flex-start; }
.msg-row.out { justify-content: flex-end; }

.msg-bubble {
    max-width: 68%; padding: 8px 12px;
    border-radius: 10px; box-shadow: var(--shadow);
    word-break: break-word;
}
.msg-row.in  .msg-bubble { background: var(--c-msg-in);  border-radius: 2px 10px 10px 10px; }
.msg-row.out .msg-bubble { background: var(--c-msg-out); border-radius: 10px 2px 10px 10px; }

.msg-sender { font-size: 11px; color: #16a34a; font-weight: 600; margin-bottom: 3px; }
.msg-text   { font-size: 13px; line-height: 1.5; white-space: pre-wrap; }
.msg-meta   { display: flex; align-items: center; justify-content: flex-end; gap: 4px; margin-top: 3px; }
.msg-time   { font-size: 10px; color: var(--c-text-3); }
.msg-status { font-size: 10px; }
.msg-status.sent      { color: #94a3b8; }
.msg-status.delivered { color: #94a3b8; }
.msg-status.read      { color: #2563eb; }
.msg-status.failed    { color: var(--c-danger); }
.msg-agent  { font-size: 10px; color: var(--c-text-3); text-align: right; margin-top: 2px; }

/* Separador de fecha */
.date-separator {
    display: flex; align-items: center; gap: 10px;
    margin: 8px 0; color: var(--c-text-3); font-size: 11px;
}
.date-separator::before, .date-separator::after {
    content: ''; flex: 1; height: 1px; background: var(--c-border);
}

/* Marcadores en timeline */
.marcador-row {
    display: flex; align-items: center; gap: 8px; margin: 4px 0;
}
.marcador-row::before, .marcador-row::after {
    content: ''; flex: 1; height: 1px;
}
.marcador-nota::before, .marcador-nota::after { background: #6366f1; opacity: .3; }
.marcador-venta::before, .marcador-venta::after { background: var(--c-success); opacity: .3; }
.marcador-seguimiento::before, .marcador-seguimiento::after { background: var(--c-warning); opacity: .3; }

.marcador-chip {
    padding: 3px 12px; border-radius: 20px;
    font-size: 11px; font-weight: 500; white-space: nowrap;
    border: 1px solid;
}
.marcador-nota { color: #6366f1; background: #eef2ff; border-color: #c7d2fe; }
.marcador-venta { color: var(--c-success); background: var(--c-success-l); border-color: #bbf7d0; }
.marcador-seguimiento { color: var(--c-warning); background: var(--c-warning-l); border-color: #fde68a; }

/* Input de respuesta */
.chat-input-area {
    padding: 10px 14px;
    background: var(--c-surface); border-top: 1px solid var(--c-border);
    flex-shrink: 0;
}

.reply-form {
    display: flex; gap: 8px; align-items: flex-end;
}
.reply-textarea {
    flex: 1; border: 1px solid var(--c-border); border-radius: var(--radius);
    padding: 8px 12px; font-size: 13px; font-family: inherit;
    resize: none; min-height: 38px; max-height: 120px;
    background: var(--c-bg); outline: none;
    transition: border-color .15s;
}
.reply-textarea:focus { border-color: var(--c-primary); background: var(--c-surface); }

.phone-selector {
    display: flex; gap: 6px; margin-bottom: 8px; align-items: center;
}
.phone-selector-label { font-size: 11px; color: var(--c-text-3); }
.phone-btn {
    padding: 3px 10px; border-radius: 4px; border: 1px solid var(--c-border);
    background: var(--c-surface); color: var(--c-text-2);
    font-size: 11px; cursor: pointer; transition: all .15s;
}
.phone-btn.active { border-color: var(--c-primary); color: var(--c-primary); background: var(--c-primary-l); }
.phone-btn.closed { opacity: .45; cursor: not-allowed; }

.window-closed-notice {
    background: var(--c-danger-l); border: 1px solid #fecaca;
    border-radius: var(--radius); padding: 8px 12px;
    font-size: 12px; color: var(--c-danger); margin-bottom: 8px;
}

/* ── Contactos grid ───────────────────────────────────────────────── */
.page-header {
    padding: 16px 20px; background: var(--c-surface);
    border-bottom: 1px solid var(--c-border);
    display: flex; align-items: center; justify-content: space-between;
    flex-shrink: 0;
}
.page-header h2 { font-size: 16px; font-weight: 600; }

.contacts-filters {
    padding: 10px 20px; background: var(--c-surface);
    border-bottom: 1px solid var(--c-border);
    display: flex; gap: 10px; flex-shrink: 0;
}
.contacts-filters input { flex: 1; }
.contacts-filters input, .contacts-filters select {
    border: 1px solid var(--c-border); border-radius: var(--radius);
    padding: 6px 10px; font-size: 13px; outline: none;
    transition: border-color .15s;
}
.contacts-filters input:focus, .contacts-filters select:focus {
    border-color: var(--c-primary);
}

.contacts-grid {
    padding: 16px 20px; overflow-y: auto; flex: 1;
    display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
    gap: 12px; align-content: start;
}

.contact-card {
    background: var(--c-surface); border: 1px solid var(--c-border);
    border-radius: var(--radius-lg); padding: 14px;
    transition: box-shadow .15s; cursor: pointer;
}
.contact-card:hover { box-shadow: var(--shadow-md); }
.contact-card-header { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.contact-initial {
    width: 40px; height: 40px; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    font-size: 16px; font-weight: 600; color: #fff; flex-shrink: 0;
}
.contact-name { font-size: 13px; font-weight: 600; color: var(--c-text-1); }
.contact-phone { font-size: 12px; color: var(--c-text-3); }
.contact-actions {
    display: flex; gap: 6px; margin-top: 10px;
    padding-top: 10px; border-top: 1px solid var(--c-border-l);
}
.contact-actions .btn-secondary { flex: 1; justify-content: center; }

/* ── Tabla de usuarios ────────────────────────────────────────────── */
#users-list {
    padding: 16px 20px; overflow-y: auto; flex: 1;
}
.users-table {
    width: 100%; border-collapse: collapse;
    background: var(--c-surface); border-radius: var(--radius-lg);
    overflow: hidden; box-shadow: var(--shadow);
}
.users-table th {
    background: var(--c-bg); padding: 10px 14px;
    text-align: left; font-size: 11px; font-weight: 600;
    text-transform: uppercase; letter-spacing: .5px; color: var(--c-text-3);
    border-bottom: 1px solid var(--c-border);
}
.users-table td {
    padding: 10px 14px; font-size: 13px;
    border-bottom: 1px solid var(--c-border-l);
}
.users-table tr:last-child td { border-bottom: none; }
.users-table tr:hover td { background: var(--c-bg); }

.role-badge {
    padding: 2px 8px; border-radius: 4px; font-size: 11px; font-weight: 500;
}
.role-admin  { background: #ede9fe; color: #6d28d9; }
.role-agente { background: var(--c-primary-l); color: var(--c-primary); }

.status-dot {
    width: 7px; height: 7px; border-radius: 50%;
    display: inline-block; margin-right: 5px;
}
.status-active   { background: var(--c-success); }
.status-inactive { background: var(--c-text-3); }

/* ── Modal ────────────────────────────────────────────────────────── */
.modal {
    position: fixed; inset: 0; z-index: 1000;
    display: flex; align-items: center; justify-content: center;
}
.modal-backdrop {
    position: absolute; inset: 0; background: rgba(0,0,0,.45);
}
.modal-box {
    position: relative; z-index: 1;
    background: var(--c-surface); border-radius: var(--radius-lg);
    width: 460px; max-width: 95vw; max-height: 90vh;
    overflow-y: auto; box-shadow: var(--shadow-md);
}
.modal-header {
    padding: 16px 20px; border-bottom: 1px solid var(--c-border);
    display: flex; align-items: center; justify-content: space-between;
}
.modal-header h3 { font-size: 15px; font-weight: 600; }
.modal-body { padding: 20px; }
.modal-footer {
    padding: 14px 20px; border-top: 1px solid var(--c-border);
    display: flex; justify-content: flex-end; gap: 8px;
}

/* ── Tipo selector (marcadores) ───────────────────────────────────── */
.tipo-selector {
    display: flex; gap: 6px; margin-bottom: 16px;
}
.tipo-btn {
    flex: 1; padding: 8px; border: 1px solid var(--c-border);
    border-radius: var(--radius); background: var(--c-surface);
    font-size: 12px; font-weight: 500; cursor: pointer;
    color: var(--c-text-2); transition: all .15s; text-align: center;
}
.tipo-btn:hover { border-color: var(--c-primary); color: var(--c-primary); }
.tipo-btn.active { border-color: var(--c-primary); color: var(--c-primary); background: var(--c-primary-l); }

/* ── Toast ────────────────────────────────────────────────────────── */
#toast-container {
    position: fixed; bottom: 20px; right: 20px;
    display: flex; flex-direction: column; gap: 8px; z-index: 9999;
}
.toast {
    background: #1e293b; color: #f1f5f9;
    padding: 10px 16px; border-radius: var(--radius);
    font-size: 13px; box-shadow: var(--shadow-md);
    animation: toastIn .2s ease;
    max-width: 320px;
}
.toast.success { border-left: 3px solid var(--c-success); }
.toast.error   { border-left: 3px solid var(--c-danger); }
.toast.info    { border-left: 3px solid var(--c-primary); }

@keyframes toastIn {
    from { opacity: 0; transform: translateY(8px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* ── Scrollbar ────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--c-border); border-radius: 10px; }
::-webkit-scrollbar-thumb:hover { background: var(--c-text-3); }
