/* ==========================================================================
   GESTOR DE SESIÓN Y VISTA REAL DE CLIENTES (PC & MOBILE)
   Sin banners ni simulaciones artificiales: carga la sesión 100% real del usuario.
   ========================================================================== */

/* Limpieza estricta: en vista de clínica no debe haber elementos de administración */
body.role-clinic .admin-only,
body.role-clinic .sidebar-toggle-btn,
body.role-clinic #btnHeaderGroqAI {
    display: none !important;
}

body.role-clinic .sidebar,
body.role-clinic #appSidebar {
    display: none !important;
}

body.role-clinic .main-content,
body.role-clinic .main-layout,
body.role-clinic .app-container,
body.role-clinic .app-container.collapsed .main-layout {
    margin-left: 0 !important;
    padding-left: 0 !important;
    width: 100% !important;
    max-width: 100% !important;
    flex: 1 1 100% !important;
}

body.role-clinic #view-patients {
    display: flex !important;
    flex-direction: column !important;
    min-height: 100% !important;
    width: 100% !important;
    opacity: 1 !important;
    visibility: visible !important;
}

/* 1. MODAL SELECTOR DE CLIENTES (PARA INICIAR SESIÓN REAL) */
.csm-overlay {
    position: fixed;
    inset: 0;
    z-index: 100050;
    background: rgba(15, 23, 42, 0.85);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    opacity: 0;
    visibility: hidden;
    transition: all 0.22s cubic-bezier(0.16, 1, 0.3, 1);
}

.csm-overlay.active {
    opacity: 1;
    visibility: visible;
}

.csm-container {
    width: 100%;
    max-width: 820px;
    max-height: 90vh;
    background: #0f172a;
    border: 1px solid #334155;
    border-radius: 16px;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.8);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transform: scale(0.96) translateY(12px);
    transition: transform 0.22s cubic-bezier(0.16, 1, 0.3, 1);
}

.csm-overlay.active .csm-container {
    transform: scale(1) translateY(0);
}

/* Header del Modal */
.csm-header {
    padding: 18px 22px;
    background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
    border-bottom: 1px solid #334155;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
}

.csm-header-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.csm-header-icon {
    width: 42px;
    height: 42px;
    border-radius: 10px;
    background: linear-gradient(135deg, rgba(16, 185, 129, 0.2) 0%, rgba(5, 150, 105, 0.35) 100%);
    border: 1px solid rgba(16, 185, 129, 0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    color: #34d399;
    font-size: 1.25rem;
    flex-shrink: 0;
}

.csm-title {
    margin: 0;
    font-size: 1.15rem;
    font-weight: 700;
    color: #f8fafc;
}

.csm-subtitle {
    margin: 2px 0 0 0;
    font-size: 0.8rem;
    color: #94a3b8;
}

.csm-close-btn {
    width: 36px;
    height: 36px;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.12);
    color: #cbd5e1;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.15s ease;
    font-size: 1.1rem;
}

.csm-close-btn:hover {
    background: rgba(239, 68, 68, 0.25);
    border-color: rgba(239, 68, 68, 0.4);
    color: #fca5a5;
}

/* Toolbar de Búsqueda y Filtros */
.csm-toolbar {
    padding: 14px 22px;
    background: #131d33;
    border-bottom: 1px solid #1e293b;
    display: flex;
    gap: 12px;
    align-items: center;
    flex-wrap: wrap;
}

.csm-search-box {
    position: relative;
    flex: 1;
    min-width: 220px;
}

.csm-search-icon {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: #64748b;
    font-size: 0.9rem;
    pointer-events: none;
}

.csm-search-input {
    width: 100%;
    padding: 9px 12px 9px 36px;
    background: #0b1120;
    border: 1px solid #334155;
    border-radius: 8px;
    color: #f8fafc;
    font-size: 0.88rem;
    outline: none;
    transition: border-color 0.18s ease;
    box-sizing: border-box;
}

.csm-search-input:focus {
    border-color: #10b981;
    box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.2);
}

.csm-pills {
    display: flex;
    gap: 6px;
}

.csm-pill-btn {
    padding: 6px 12px;
    font-size: 0.78rem;
    font-weight: 600;
    border-radius: 6px;
    background: #1e293b;
    border: 1px solid #334155;
    color: #94a3b8;
    cursor: pointer;
    transition: all 0.15s ease;
}

.csm-pill-btn:hover {
    color: #ffffff;
    background: #334155;
}

.csm-pill-btn.active {
    background: #10b981;
    color: #ffffff;
    border-color: #10b981;
}

/* Lista / Rejilla de Clientes */
.csm-body {
    padding: 18px 22px;
    overflow-y: auto;
    flex: 1;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
    gap: 12px;
}

.csm-client-card {
    background: #162036;
    border: 1px solid #28354f;
    border-radius: 12px;
    padding: 14px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    gap: 12px;
    transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

.csm-client-card:hover {
    border-color: #10b981;
    transform: translateY(-2px);
    box-shadow: 0 10px 24px -6px rgba(0, 0, 0, 0.5);
}

.csm-card-top {
    display: flex;
    align-items: flex-start;
    gap: 12px;
}

.csm-client-avatar {
    width: 44px;
    height: 44px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
    flex-shrink: 0;
}

.csm-avatar-doctor {
    background: linear-gradient(135deg, rgba(56, 189, 248, 0.15), rgba(147, 197, 253, 0.3));
    border: 1px solid rgba(56, 189, 248, 0.35);
    color: #38bdf8;
}

.csm-avatar-clinic {
    background: linear-gradient(135deg, rgba(168, 85, 247, 0.15), rgba(192, 132, 252, 0.3));
    border: 1px solid rgba(168, 85, 247, 0.35);
    color: #c084fc;
}

.csm-client-meta {
    flex: 1;
    min-width: 0;
}

.csm-client-name {
    margin: 0 0 3px 0;
    font-size: 0.94rem;
    font-weight: 700;
    color: #f8fafc;
    line-height: 1.35;
    word-break: break-word;
}

.csm-client-sub {
    margin: 0;
    font-size: 0.76rem;
    color: #94a3b8;
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
}

.csm-tag-specialty {
    color: #60a5fa;
    font-weight: 600;
}

.csm-tag-user {
    background: rgba(255, 255, 255, 0.06);
    padding: 1px 6px;
    border-radius: 4px;
    font-family: monospace;
    color: #cbd5e1;
}

.csm-card-bottom {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding-top: 10px;
    border-top: 1px solid rgba(255, 255, 255, 0.06);
}

.csm-patient-count {
    font-size: 0.74rem;
    color: #94a3b8;
    display: flex;
    align-items: center;
    gap: 5px;
}

.csm-patient-count strong {
    color: #34d399;
}

.csm-btn-select {
    padding: 8px 14px;
    font-size: 0.82rem;
    font-weight: 700;
    border-radius: 6px;
    background: linear-gradient(135deg, #10b981 0%, #059669 100%);
    color: #ffffff;
    border: none;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    transition: all 0.18s ease;
}

.csm-btn-select:hover {
    background: linear-gradient(135deg, #059669 0%, #047857 100%);
    box-shadow: 0 4px 12px rgba(16, 185, 129, 0.35);
    transform: translateY(-1px);
}

/* Footer del Modal */
.csm-footer {
    padding: 12px 22px;
    background: #0b1120;
    border-top: 1px solid #1e293b;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 0.76rem;
    color: #64748b;
}

/* 2. MENÚ DE RETORNO / CAMBIO RÁPIDO PARA EL ADMINISTRADOR (BOTTOM SHEET EN MÓVIL) */
.client-switch-sheet {
    position: fixed;
    inset: 0;
    z-index: 100060;
    background: rgba(15, 23, 42, 0.85);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    opacity: 0;
    visibility: hidden;
    transition: all 0.2s ease;
}

.client-switch-sheet.active {
    opacity: 1;
    visibility: visible;
}

.css-content {
    width: 100%;
    max-width: 420px;
    background: #0f172a;
    border: 1px solid #334155;
    border-radius: 16px;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.9);
    overflow: hidden;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.css-header {
    text-align: center;
    padding-bottom: 10px;
    border-bottom: 1px solid #1e293b;
}

.css-header h3 {
    margin: 0;
    font-size: 1.05rem;
    color: #f8fafc;
}

.css-header p {
    margin: 4px 0 0 0;
    font-size: 0.8rem;
    color: #94a3b8;
}

.css-option-btn {
    width: 100%;
    padding: 12px 14px;
    border-radius: 10px;
    font-size: 0.88rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 12px;
    cursor: pointer;
    border: 1px solid #334155;
    background: #1e293b;
    color: #f8fafc;
    transition: all 0.16s ease;
    text-align: left;
}

.css-option-btn:hover {
    background: #334155;
    transform: translateY(-1px);
}

.css-option-btn.btn-return-admin {
    background: linear-gradient(135deg, rgba(16, 185, 129, 0.2), rgba(5, 150, 105, 0.35));
    border-color: #10b981;
    color: #34d399;
}

.css-option-btn.btn-return-admin:hover {
    background: #10b981;
    color: #ffffff;
}

.css-option-btn.btn-cancel {
    background: transparent;
    border-color: transparent;
    color: #94a3b8;
    justify-content: center;
}

.css-option-btn.btn-cancel:hover {
    color: #ffffff;
}

/* ADAPTABILIDAD MÓVIL (Smartphones <= 600px) */
@media (max-width: 600px) {
    .csm-container {
        max-height: 94vh;
        border-radius: 12px;
    }

    .csm-header {
        padding: 14px 16px;
    }

    .csm-toolbar {
        padding: 10px 16px;
        flex-direction: column;
        align-items: stretch;
    }

    .csm-pills {
        justify-content: space-between;
    }

    .csm-pill-btn {
        flex: 1;
        text-align: center;
    }

    .csm-body {
        grid-template-columns: 1fr;
        padding: 12px 16px;
    }
}
