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

html, body{
    width:100%;
    height:100%;
}

body{
    font-family: "Segoe UI", Arial, sans-serif;
    background:#0f172a;
    color:#ffffff;
}

/* ==========================================================
   VARIABLES
========================================================== */
:root{
    --bg:#0f172a;
    --sidebar:#111827;
    --panel:#1e293b; /* Color de los paneles principales */
    --panel2:#263548;
    --border:#334155;
    --blue:#2563eb;
    --blueHover:#1d4ed8;
    --green:#22c55e;
    --text:#ffffff;
    --muted:#94a3b8;
    
    /* Variables nuevas para la V2 */
    --bg-surface: #1e293b; 
    --bg-surface-hover: #263548;
    --border-light: #475569;
    --primary: #3b82f6;
    --danger: #ef4444;
    --success: #10b981;
    --warning: #f59e0b;
    --radius-md: 10px;
    --radius-lg: 16px;
    
    /* Variable especifica para el fondo solido de los menus */
    --menu-bg-solid: #1e293b; /* Igual a --panel, pero aseguramos que sea opaco */
}

/* ==========================================================
   LAYOUT
========================================================== */
.app{
    display:flex;
    min-height:100vh;
}

.content{
    flex:1;
    padding:30px;
    /* Crear un contexto de apilamiento base para todo el contenido principal */
    position: relative;
    z-index: 1;
}

/* ==========================================================
   SIDEBAR
========================================================== */
.sidebar{
    width:260px;
    background:var(--sidebar);
    border-right:1px solid var(--border);
    padding:24px;
    position: relative;
    z-index: 100; /* Sidebar por encima del contenido general si colapsara */
}

.logo{
    display:flex;
    align-items:center;
    gap:15px;
    margin-bottom:40px;
}

.logo-icon{
    width:52px;
    height:52px;
    border-radius:16px;
    background:linear-gradient(135deg, var(--blue), #4f8cff);
    display:flex;
    justify-content:center;
    align-items:center;
    font-size:24px;
    font-weight:bold;
}

.logo h2{ font-size:24px; }
.logo span{ color:var(--muted); font-size:13px; }

/* ==========================================================
   MENU
========================================================== */
.sidebar nav{
    display:flex;
    flex-direction:column;
    gap:10px;
}

.menu-btn{
    width:100%;
    border:none;
    background:transparent;
    color:white;
    text-align:left;
    padding:14px 16px;
    border-radius:12px;
    cursor:pointer;
    font-size:15px;
    transition:.25s;
}

.menu-btn:hover{ background:var(--panel); }
.menu-btn.active{ background:var(--blue); }

/* ==========================================================
   HEADER
========================================================== */
.topbar{
    display:flex;
    justify-content:space-between;
    align-items:center;
    margin-bottom:30px;
}

.topbar h1{ font-size:44px; margin-bottom:8px; }
.topbar p{ color:var(--muted); }

.profile{
    display:flex;
    align-items:center;
    gap:20px;
    background:var(--panel);
    border:1px solid var(--border);
    border-radius:16px;
    padding:18px 24px;
}

.profile button{
    background:var(--blue);
    color:white;
    border:none;
    padding:12px 22px;
    border-radius:10px;
    cursor:pointer;
}

.profile button:hover{ background:var(--blueHover); }

/* ==========================================================
   PAGINAS
========================================================== */
.page{ display:none; }
.page.active{ display:block; }
.page-title{ font-size:34px; font-weight:700; margin-bottom:6px; }
.page-subtitle{ color:var(--muted); margin-bottom:30px; }

/* ==========================================================
   TARJETAS DASHBOARD
========================================================== */
.cards{
    display:grid;
    grid-template-columns:repeat(auto-fit,minmax(220px,1fr));
    gap:20px;
    margin-bottom:30px;
}

.card-mini{
    background:var(--panel);
    border:1px solid var(--border);
    border-radius:18px;
    padding:24px;
}

.card-mini span{ font-size:30px; }
.card-mini h3{ margin-top:18px; color:var(--muted); font-size:16px; }
.card-mini strong{ display:block; font-size:42px; margin-top:10px; }

/* ==========================================================
   PANELES
========================================================== */
.panel{
    background:#1f2937;
    border:1px solid #2d3b52;
    border-radius:16px;
    padding:22px;
    margin-bottom:28px;
    box-shadow: 0 8px 25px rgba(0,0,0,.18);
    /* Permitir que los menús floten fuera de la caja */
    overflow: visible !important; 
    /* El panel en sí no debe atrapar el z-index de sus hijos muy alto */
    position: relative;
    /* z-index: 10; <- Eliminamos esto para que no limite a los hijos */
}

.panel h2{
    display:flex;
    align-items:center;
    gap:10px;
    font-size:30px;
    margin-bottom:18px;
    font-weight:700;
}

.dashboard-row{
    display:grid;
    grid-template-columns:1fr 1fr;
    gap:24px;
    margin-top:24px;
}

/* ==========================================================
   FORMULARIOS Y BOTONES
========================================================== */
input, select, textarea {
    width: 100%;
    background: var(--bg-surface);
    color: var(--text);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-md);
    padding: 12px 16px;
    font-size: 14px;
    transition: 0.25s;
    font-family: inherit;
    /* Aseguramos que los inputs estandar esten por debajo de los menus */
    position: relative;
    z-index: 1;
}

textarea {
    min-height: 180px;
    resize: vertical;
}

input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.25);
}

/* Native select fallback */
select { color-scheme: dark; cursor: pointer; }
select:hover { border-color: var(--primary); background-color: var(--bg-surface-hover); }

button, .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 18px;
    border-radius: var(--radius-md);
    font-weight: 600;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s ease;
    border: none;
    background: var(--primary);
    color: white;
}

button:hover, .btn-primary:hover { background: var(--blueHover); transform: translateY(-1px); }
.btn-outline { background: transparent; border: 1px solid var(--border-light); color: var(--text); }
.btn-outline:hover { background: var(--bg-surface-hover); border-color: var(--muted); }
.btn-danger { background: rgba(239, 68, 68, 0.1); color: var(--danger); border: 1px solid rgba(239, 68, 68, 0.2); }
.btn-danger:hover { background: var(--danger); color: white; }
/* Nuevo botón para liberar (naranja) */
.btn-warning { 
    background: rgba(245, 158, 11, 0.1); 
    color: var(--warning); 
    border: 1px solid rgba(245, 158, 11, 0.3); 
}
.btn-warning:hover { 
    background: var(--warning); 
    color: white; 
}
button.btn-small { padding: 8px 12px; font-size: 13px; background: var(--bg-surface-hover); border: 1px solid var(--border-light); }
button.btn-small:hover { background: var(--border-light); }

/* ==========================================================
   CUSTOM SELECT (Unificado y Solucionado el problema de la capa)
========================================================== */
.custom-select-wrapper {
    position: relative;
    user-select: none;
    width: 100%;
    margin-bottom: 15px;
    /* No le damos un z-index alto al wrapper general cuando esta cerrado */
}

.custom-select-trigger {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 46px; /* Altura unificada a 46px */
    padding: 0 16px; /* Dejamos que flexbox centre el texto verticalmente */
    font-size: 14px;
    color: var(--text);
    background: var(--bg-surface);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: all 0.2s;
    position: relative;
    z-index: 2; 
}

.custom-select-trigger:hover {
    background: var(--bg-surface-hover);
    border-color: var(--primary);
}

.custom-select-trigger::after {
    content: "▼";
    font-size: 10px;
    color: var(--muted);
    transition: transform 0.2s;
}

.custom-options {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    margin-top: 4px;
    
    /* AQUI ESTA LA MAGIA: Forzamos un color solido sin transparencia */
    background-color: var(--menu-bg-solid) !important;
    
    border: 1px solid var(--primary);
    border-radius: var(--radius-md);
    box-shadow: 0 10px 30px rgba(0,0,0,0.8); /* Sombra mas oscura para definir bordes */
    
    /* El Z-index MAS ALTO de toda la aplicacion */
    z-index: 999999 !important; 
    
    /* Efecto oculto por defecto */
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transform: translateY(-10px);
    transition: all 0.2s ease;
    
    max-height: 250px;
    overflow-y: auto;
}

/* Estado abierto */
/* Cuando el wrapper se abre, le damos prioridad sobre todo lo demas en la pagina */
.custom-select-wrapper.open { z-index: 999999; } 
.custom-select-wrapper.open .custom-select-trigger { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15); }
.custom-select-wrapper.open .custom-select-trigger::after { transform: rotate(180deg); }

.custom-select-wrapper.open .custom-options {
    opacity: 1;
    visibility: visible;
    pointer-events: all;
    transform: translateY(0);
}

.custom-option { padding: 10px 16px; font-size: 14px; color: var(--text); cursor: pointer; transition: background 0.1s; }
.custom-option:hover { background: var(--primary); color: white; }
.custom-option.selected { background: var(--primary); color: white; font-weight: 600; }

/* Para los select especificos que no usan la clase general */
#contenedor-revendedor .custom-options {
     background-color: var(--menu-bg-solid) !important;
     z-index: 999999 !important;
}


/* ==========================================================
   BARRA DE FILTROS Y BUSCADORES (Alineación Perfecta)
========================================================== */
.filter-toolbar {
    display: flex; 
    flex-direction: row; 
    align-items: center; 
    gap: 16px; 
    padding: 20px;
    background: var(--bg-surface); 
    border: 1px solid var(--border-light);
    border-radius: var(--radius-lg); 
    margin-bottom: 24px; 
    box-shadow: var(--shadow-sm);
    position: relative; 
}

/* 1. Contenedor principal de la búsqueda a 46px */
.search-box { 
    position: relative; 
    flex: 1 1 auto; 
    min-width: 0; 
    width: 100%; 
    height: 46px; /* Altura unificada */
    display: flex;
    align-items: center;
    background: var(--bg-panel);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-md);
    transition: all 0.2s;
    overflow: hidden;
}

.search-box:focus-within {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
}

.search-icon {
    position: absolute;
    left: 14px;
    color: var(--muted);
    font-size: 16px;
    pointer-events: none;
    z-index: 2;
}

/* 2. Textarea inteligente: Respeta la altura pero permite scroll vertical */
.search-box textarea { 
    width: 100%; 
    height: 46px !important; 
    min-height: 46px !important;
    padding: 12px 16px 12px 42px !important; 
    background: transparent !important;
    color: var(--text) !important;
    border: none !important;
    outline: none !important;
    font-size: 14px;
    margin: 0 !important;
    box-shadow: none !important;
    resize: none;
    
    /* La magia visual para listas largas de correos */
    white-space: pre-wrap !important; /* Respeta y muestra los saltos de línea */
    overflow-y: auto !important; /* Muestra barra de scroll solo si es necesario */
    overflow-x: hidden !important;
    line-height: 20px;
}

/* Estilizamos la barra de scroll interna del textarea para que sea sutil */
.search-box textarea::-webkit-scrollbar {
    width: 6px;
}
.search-box textarea::-webkit-scrollbar-thumb {
    background: var(--border-light);
    border-radius: 4px;
}

/* 3. Aseguramos que los anchos de los menús sean correctos */
.filter-toolbar select { width: auto; min-width: 180px; }
.filter-toolbar .custom-select-wrapper { width: auto !important; min-width: 220px !important; flex: 0 0 auto; margin-bottom: 0; }

/* 4. Botón Limpiar a 46px exactos */
.filter-toolbar .btn {
    height: 46px; /* Altura unificada */
    white-space: nowrap; 
    flex-shrink: 0;
    margin: 0;
    display: inline-flex;
    align-items: center;
}

@media (max-width: 900px) {
    .filter-toolbar { flex-direction: column; align-items: stretch; }
    .filter-toolbar select, .filter-toolbar .btn, .filter-toolbar .custom-select-wrapper { width: 100% !important; }
}

/* Mejora visual para la caja grande de "Asignar Cuentas" */
#correoCuenta {
    min-height: 120px !important;
    background: var(--bg-panel);
    font-family: monospace;
    padding: 16px;
    font-size: 13px;
    line-height: 1.6;
    margin-bottom: 10px;
}


/* ==========================================================
   DATAGRID (TABLA INVENTARIO)
========================================================== */
.inventory-tools { display: flex; justify-content: flex-end; gap: 12px; margin-bottom: 16px; position: relative; z-index: 1;}
.inventory-table-container { background: var(--bg-surface); border: 1px solid var(--border); border-radius: var(--radius-lg); overflow: hidden; position: relative; z-index: 1;}
.data-grid { width: 100%; border-collapse: collapse; text-align: left; }
.data-grid th { background: rgba(0, 0, 0, 0.2); padding: 16px 20px; font-weight: 600; font-size: 13px; text-transform: uppercase; color: var(--muted); border-bottom: 1px solid var(--border); }
.data-grid td { padding: 16px 20px; border-bottom: 1px solid rgba(51, 65, 85, 0.5); font-size: 14px; color: #cbd5e1; }
.grid-row:hover td { background: var(--bg-surface-hover); }
.grid-row:last-child td { border-bottom: none; }
.cell-primary { font-weight: 600; color: white; }
.flex-align { display: flex; align-items: center; gap: 10px; }

/* Badges, Tags y Checkboxes */
.badge { padding: 4px 10px; border-radius: 20px; font-size: 12px; font-weight: 600; text-transform: capitalize; }
.badge-success { background: rgba(16, 185, 129, 0.15); color: #34d399; border: 1px solid rgba(16, 185, 129, 0.3); }
.badge-warning { background: rgba(245, 158, 11, 0.15); color: #fbbf24; border: 1px solid rgba(245, 158, 11, 0.3); }
.badge-danger { background: rgba(239, 68, 68, 0.15); color: #f87171; border: 1px solid rgba(239, 68, 68, 0.3); }
.plat-tag { background: #334155; padding: 4px 10px; border-radius: 6px; font-size: 12px; font-weight: 600; }
.actions-group { display: flex; gap: 8px; }
.action-btn { background: transparent; border: none; padding: 6px; border-radius: 6px; cursor: pointer; color: var(--muted); transition: 0.2s; font-size: 16px; }
.action-btn:hover { background: var(--border); color: white; }
.action-btn.delete:hover { background: rgba(239, 68, 68, 0.2); color: var(--danger); }
input[type="checkbox"] { appearance: none; width: 18px; height: 18px; border: 2px solid var(--border-light); border-radius: 4px; background: var(--bg-surface); cursor: pointer; position: relative; z-index: 1;}
input[type="checkbox"]:checked { background: var(--primary); border-color: var(--primary); }
input[type="checkbox"]:checked::after { content: "✓"; position: absolute; color: white; font-size: 12px; font-weight: bold; top: 50%; left: 50%; transform: translate(-50%, -50%); }

/* ==========================================================
   ACORDEONES DE REVENDEDORES
========================================================== */
#listaCuentas {
    position: relative;
    z-index: 1; /* Mantenemos el acordeon por debajo de los menus desplegables superiores */
}
.revendedor-card { background: var(--bg-surface); border: 1px solid var(--border-light); border-radius: var(--radius-lg); margin-bottom: 12px; overflow: hidden; }
.revendedor-header { padding: 16px 20px; display: flex; justify-content: space-between; align-items: center; cursor: pointer; background: var(--bg-panel); }
.revendedor-header strong { font-size: 16px; }
.revendedor-header .count-badge { background: var(--primary); padding: 2px 10px; border-radius: 12px; font-size: 12px; }
.revendedor-header:hover { background: var(--bg-surface-hover); }
.revendedor-body { display: none !important; }
.revendedor-card.expanded .revendedor-body { display: grid !important; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 8px; padding: 15px; background: var(--bg-panel); border-top: 1px solid var(--border-light); }
.cuenta-fila { display: flex; justify-content: space-between; align-items: center; background: var(--bg-surface); padding: 8px 12px; border-radius: var(--radius-md); border: 1px solid var(--border-light); font-size: 13px; }

/* ==========================================================
   PAGINACIÓN, TOAST Y MODAL
========================================================== */
.pagination-container { display: flex; justify-content: center; gap: 6px; margin-top: 24px; position: relative; z-index: 1; }
.btn-page { width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; background: var(--bg-surface); border: 1px solid var(--border); border-radius: 8px; color: var(--text); font-weight: 600; cursor: pointer; transition: 0.2s; }
.btn-page:hover:not(:disabled) { background: var(--border); }
.btn-page.active { background: var(--primary); border-color: var(--primary); color: white; }
.btn-page:disabled { opacity: 0.5; cursor: not-allowed; }

.empty-state { text-align: center; padding: 60px 20px; color: var(--muted); }
.empty-icon { font-size: 48px; margin-bottom: 16px; opacity: 0.5; }

#toast { position: fixed; bottom: 30px; right: 30px; background: var(--bg-surface); color: white; padding: 16px 24px; border-left: 4px solid var(--success); border-radius: var(--radius-md); font-weight: 500; font-size: 14px; box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1); opacity: 0; transform: translateY(20px); transition: all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55); z-index: 10000; }
#toast.show { opacity: 1; transform: translateY(0); }

.modal-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.7); backdrop-filter: blur(4px); display: flex; justify-content: center; align-items: center; z-index: 9999; opacity: 0; pointer-events: none; transition: opacity 0.3s ease; }
.modal-backdrop.active { opacity: 1; pointer-events: auto; }
.modal-box { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius-lg); width: 100%; max-width: 450px; box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5); transform: translateY(20px) scale(0.95); transition: transform 0.3s ease; }
.modal-backdrop.active .modal-box { transform: translateY(0) scale(1); }
.modal-header { padding: 20px 24px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; }
.modal-header h3 { font-size: 18px; font-weight: 600; }
.modal-close { background: none; border: none; color: var(--muted); font-size: 24px; cursor: pointer; }
.modal-close:hover { color: white; }
.modal-body { padding: 24px; font-size: 15px; color: var(--muted); line-height: 1.5; }
.modal-footer { padding: 16px 24px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 12px; background: rgba(0,0,0,0.1); border-bottom-left-radius: var(--radius-lg); border-bottom-right-radius: var(--radius-lg); }



/* ==========================================================
   DASHBOARD - ACTIVIDAD Y PLATAFORMAS (V2)
========================================================== */
.activity-item { display: flex; justify-content: space-between; align-items: center; padding: 12px 0; border-bottom: 1px solid var(--border-light); }
.activity-item:last-child { border-bottom: none; }
.activity-main { display: flex; flex-direction: column; gap: 5px; }
.activity-main strong { font-size: 14px; color: white; }
.activity-main span { font-size: 12px; color: var(--muted); }
.pill { padding: 4px 10px; border-radius: 20px; font-size: 11px; font-weight: bold; }
.pill-ok { background: rgba(16, 185, 129, 0.15); color: #34d399; }
.pill-bad { background: rgba(239, 68, 68, 0.15); color: #f87171; }

.platform-row { display: flex; align-items: center; gap: 12px; margin-bottom: 15px; font-size: 13px; font-weight: 600;}
.platform-row strong { width: 90px; color: var(--muted); }
.platform-row .bar { flex: 1; height: 8px; background: var(--bg-surface-hover); border-radius: 4px; overflow: hidden; }
.platform-row .bar span { display: block; height: 100%; background: var(--primary); border-radius: 4px; }

/* ==========================================================
   USUARIOS - TARJETAS (V2)
========================================================== */
.users-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; margin-top: 20px; }
.user-card { background: var(--bg-surface); border: 1px solid var(--border-light); border-radius: var(--radius-lg); padding: 20px; display: flex; flex-direction: column; gap: 18px; }
.user-info { display: flex; flex-direction: column; gap: 6px; }
.user-info strong { font-size: 18px; color: white; }
.user-info span { font-size: 13px; color: var(--muted); }
.user-actions { display: flex; gap: 10px; margin-top: auto; }
.user-actions button { flex: 1; padding: 8px 12px; font-size: 13px; }