:root {
    --primary: #6366f1;
    /* Indigo moderno */
    --primary-dark: #4f46e5;
    --secondary: #ec4899;
    /* Pink accent */
    --dark-bg: #0f172a;
    --card-bg: #1e293b;
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
    --success: #10b981;
    --warning: #f59e0b;
    --danger: #ef4444;
}

* {
    box-sizing: border-box;
    scrollbar-width: thin;
    /* Firefox */
    scrollbar-color: var(--primary) transparent;
}

/* Custom Scrollbar for Webkit */
::-webkit-scrollbar {
    width: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background-color: var(--primary);
    border-radius: 20px;
}

body {
    font-family: 'Inter', sans-serif;
    margin: 0;
    background-color: var(--dark-bg);
    color: var(--text-main);
    height: 100vh;
    overflow: hidden;
}

/* LOGIN */
#login-screen {
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background: radial-gradient(circle at top right, #1e1b4b, var(--dark-bg));
}

.login-box {
    background: rgba(30, 41, 59, 0.7);
    backdrop-filter: blur(12px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    padding: 2.5rem;
    border-radius: 16px;
    text-align: center;
    width: 320px;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
}

.login-box h1 {
    margin-bottom: 0.5rem;
    font-size: 1.8rem;
}

.login-box p {
    color: var(--text-muted);
    margin-bottom: 2rem;
}

.login-box input {
    width: 100%;
    padding: 12px;
    margin: 10px 0;
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 8px;
    color: white;
    box-sizing: border-box;
    /* Fix padding issue */
}

.login-box button {
    width: 100%;
    padding: 12px;
    background: linear-gradient(135deg, var(--primary), var(--secondary));
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 600;
    margin-top: 1rem;
    transition: transform 0.2s;
}

.login-box button:hover {
    transform: translateY(-2px);
}

/* DASHBOARD LAYOUT */
#dashboard {
    display: flex;
    height: 100vh;
}

.sidebar {
    width: 260px;
    background: rgba(15, 23, 42, 0.95);
    border-right: 1px solid rgba(255, 255, 255, 0.05);
    display: flex;
    flex-direction: column;
    padding: 1.5rem;
    overflow-y: auto;
    height: 100vh;
}

.logo {
    font-size: 1.5rem;
    font-weight: 800;
    background: linear-gradient(to right, #818cf8, #e879f9);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 3rem;
    text-align: center;
}

/* BOT STATUS INDICATOR */
.status-indicator {
    display: flex;
    align-items: center;
    gap: 10px;
    background: rgba(255, 255, 255, 0.05);
    padding: 10px;
    border-radius: 8px;
    margin-bottom: 2rem;
    font-size: 0.9rem;
}

.status-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--text-muted);
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
}

.status-dot.connected {
    background: var(--success);
    box-shadow: 0 0 10px var(--success);
}

.status-dot.disconnected {
    background: var(--danger);
    box-shadow: 0 0 10px var(--danger);
}

.status-dot.qr {
    background: var(--warning);
    box-shadow: 0 0 10px var(--warning);
}

.sidebar button {
    background: transparent;
    border: none;
    text-align: left;
    padding: 14px;
    cursor: pointer;
    font-size: 1rem;
    color: var(--text-muted);
    border-radius: 8px;
    margin-bottom: 8px;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 10px;
}

.sidebar button:hover {
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-main);
}

.sidebar button.active {
    background: rgba(99, 102, 241, 0.1);
    color: var(--primary);
    font-weight: 600;
}

.logout-btn {
    margin-top: auto;
    color: var(--danger) !important;
}

.logout-btn:hover {
    background: rgba(239, 68, 68, 0.1) !important;
}

.content {
    flex: 1;
    padding: 2.5rem;
    overflow-y: auto;
    background: linear-gradient(to bottom, var(--dark-bg), #111827);
    display: flex;
    flex-direction: column;
}

main section {
    display: none;
    flex: 1;
    flex-direction: column;
}

main section.active-section {
    display: flex !important;
}

h2 {
    font-size: 1.8rem;
    margin-bottom: 1.5rem;
    font-weight: 700;
}

/* CARDS & TABLES */
.table-container,
.card {
    background: var(--card-bg);
    border-radius: 12px;
    border: 1px solid rgba(255, 255, 255, 0.05);
    padding: 1rem;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}

table {
    width: 100%;
    border-collapse: collapse;
}

th {
    text-align: left;
    padding: 1rem;
    color: var(--text-muted);
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

td {
    padding: 1rem;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    vertical-align: middle;
}

tr:hover {
    background: rgba(255, 255, 255, 0.02);
}

.card h3 {
    margin-top: 0;
    color: var(--primary);
}

/* RESOURCES GRID */
.resource-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 20px;
}

.snippet-item {
    background: rgba(0, 0, 0, 0.2);
    padding: 15px;
    border-radius: 8px;
    margin-bottom: 10px;
    border-left: 3px solid var(--secondary);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.snippet-text {
    color: var(--text-muted);
    font-size: 0.9rem;
    margin-top: 5px;
}

/* ACTIONS */
.btn-action {
    padding: 6px 12px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.85rem;
    margin-right: 5px;
    transition: transform 0.1s;
}

.btn-action:hover {
    transform: scale(1.05);
}

.btn-whatsapp {
    background: #10b981;
    color: white;
}

.btn-edit {
    background: #3b82f6;
    color: white;
}

.btn-delete {
    background: #ef4444;
    color: white;
}

.btn-copy {
    background: #64748b;
    color: white;
}

/* MODAL */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    backdrop-filter: blur(4px);
    justify-content: center;
    align-items: center;
    z-index: 1000;
}

.modal-content {
    background: var(--card-bg);
    padding: 2rem;
    border-radius: 16px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    width: 500px;
    max-width: 95%;
    max-height: 90vh;
    /* Ensure it fits in viewport */
    overflow-y: auto;
    /* Enable scrolling */
    display: flex;
    flex-direction: column;
    gap: 15px;
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.5);
}

.modal-content h3 {
    margin-top: 0;
    color: white;
}

.modal-content input,
.modal-content select,
.modal-content textarea {
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: white;
    padding: 10px;
    border-radius: 8px;
    font-family: inherit;
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 10px;
}

.btn-cancel {
    background: transparent;
    color: var(--text-muted);
    border: 1px solid rgba(255, 255, 255, 0.1);
    padding: 10px 20px;
    border-radius: 8px;
}

.btn-save {
    background: var(--primary);
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 8px;
    font-weight: bold;
}

/* UTILS */
.badge {
    padding: 4px 8px;
    border-radius: 12px;
    font-size: 0.75rem;
    font-weight: bold;
}

.badge-new {
    background: rgba(16, 185, 129, 0.2);
    color: #34d399;
}

.header-actions {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

/* --- SMART CRM LAYOUT --- */
.leads-layout {
    display: flex;
    height: calc(100vh - 40px);
    /* Adjust for header if exists, or just 100% */
    border: 1px solid rgba(255, 255, 255, 0.05);
    border-radius: 12px;
    overflow: hidden;
    background: var(--card-bg);
}

.leads-list-panel {
    width: 350px;
    min-width: 300px;
    background: rgba(0, 0, 0, 0.2);
    border-right: 1px solid rgba(255, 255, 255, 0.05);
    display: flex;
    flex-direction: column;
}

.leads-scroll {
    flex: 1;
    overflow-y: auto;
}

.lead-item {
    padding: 15px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    cursor: pointer;
    transition: background 0.2s;
}

.lead-item:hover,
.lead-item.active {
    background: rgba(255, 255, 255, 0.05);
    border-left: 3px solid var(--primary);
}

.lead-detail-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--card-bg);
    position: relative;
    min-width: 0;
    /* Important for flex child with overflow */
}

/* SMART BADGES & TAGS */
.tag-badge {
    padding: 4px 10px;
    border-radius: 12px;
    font-size: 0.7rem;
    font-weight: bold;
    text-transform: uppercase;
    display: inline-flex;
    align-items: center;
}

.tag-demo {
    background: rgba(59, 130, 246, 0.2);
    color: #60a5fa;
    border: 1px solid rgba(59, 130, 246, 0.4);
}

.tag-reseller {
    background: rgba(245, 158, 11, 0.2);
    color: #fbbf24;
    border: 1px solid rgba(245, 158, 11, 0.4);
}

.tag-buy {
    background: rgba(16, 185, 129, 0.2);
    color: #34d399;
    border: 1px solid rgba(16, 185, 129, 0.4);
}

.tag-support {
    background: rgba(239, 68, 68, 0.2);
    color: #fca5a5;
}

/* CHAT AREA COMPONENTS */
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: rgba(0, 0, 0, 0.1);
}

.chat-history {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.chat-input-area {
    background: var(--card-bg);
    padding: 15px 20px;
    border-top: 1px solid rgba(255, 255, 255, 0.05);
    display: flex;
    gap: 10px;
    align-items: center;
    flex-shrink: 0;
}

.quick-actions {
    padding: 10px 15px;
    background: rgba(0, 0, 0, 0.1);
    display: flex;
    gap: 10px;
    overflow-x: auto;
    flex-shrink: 0;
    border-top: 1px solid rgba(255, 255, 255, 0.03);
}

.btn-chip {
    background: rgba(16, 185, 129, 0.1);
    border: 1px solid rgba(16, 185, 129, 0.3);
    color: #34d399;
    padding: 6px 12px;
    border-radius: 20px;
    cursor: pointer;
    font-size: 0.85rem;
    white-space: nowrap;
}

/* UTILS */
.btn-small {
    background: transparent;
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: var(--text-muted);
    padding: 5px 10px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.8rem;
}

.form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

/* SWITCH COMPONENT */
.switch {
    position: relative;
    display: inline-block;
    width: 34px;
    height: 18px;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #334155;
    transition: .4s;
    border-radius: 34px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 12px;
    width: 12px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: .4s;
    border-radius: 50%;
}

input:checked+.slider {
    background-color: var(--primary);
}

input:checked+.slider:before {
    transform: translateX(16px);
}

/* --- CONSOLIDATED MOBILE RULES (v4.0) --- */
@media (max-width: 768px) {

    body,
    html {
        height: 100vh;
        height: 100dvh;
        overflow-y: auto; /* Permite scroll global si es necesario */
        -webkit-overflow-scrolling: touch;
    }

    #dashboard {
        height: 100vh;
        height: 100dvh;
        overflow: hidden; /* El dashboard se mantiene fijo, sus hijos scrollean */
    }

    .mobile-menu-btn {
        display: block;
        position: fixed;
        top: 15px;
        right: 15px;
        z-index: 200;
        background: var(--primary);
        border: none;
        color: white;
        padding: 10px;
        border-radius: 8px;
        font-size: 1.5rem;
        box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);
    }

    .sidebar {
        position: fixed;
        width: 80%;
        height: 100dvh;
        z-index: 300;
        transform: translateX(-100%);
        transition: transform 0.3s ease;
        background: var(--dark-bg);
    }

    .sidebar.open {
        transform: translateX(0);
    }

    /* Target the main tag with class content explicitly */
    main.content {
        padding: 0 !important;
        height: 100% !important;
        flex: 1 !important;
        display: flex !important;
        flex-direction: column !important;
        min-height: 0 !important;
        overflow-y: auto !important; /* Habilitar scroll en el contenido principal */
        -webkit-overflow-scrolling: touch;
    }

    /* Override general rules for sections inside main */
    main.content>section {
        flex: 1 !important;
        height: auto !important; /* Permitir que crezca con el contenido */
        min-height: 100% !important;
        padding: 0 !important;
        overflow-y: visible !important; /* El scroll lo maneja el padre main.content */
        display: none;
        /* JS toggles this */
    }

    main section.active-section {
        display: flex !important;
    }

    #leads-section {
        flex-direction: column;
        /* NUCLEAR OPTION: Fixed position to ignore parent layout issues */
        position: fixed !important;
        top: 0;
        left: 0;
        width: 100% !important;
        height: 100dvh !important;
        background: var(--dark-bg);
        z-index: 100 !important;
        /* Below Sidebar, Above Content */

        display: none;
        /* Hide by default */
    }

    #leads-section.active-section {
        display: flex !important;
    }

    .leads-layout {
        height: 100% !important;
        border: none;
        border-radius: 0;
        flex: 1;
        display: flex;
        flex-direction: column;
        position: relative;
        /* Critical for absolute children */
    }

    /* --- MOBILE VIEW TOGGLING (Refactor) --- */
    /* DEFAULT STATE: List Visible, Detail Hidden */
    .leads-list-panel {
        display: flex !important;
        width: 100% !important;
        height: 100% !important;
        flex-direction: column;
        min-height: 0;
        /* Important for inner scroll */
    }

    .lead-detail-panel {
        display: none !important;
        width: 100% !important;
        height: 100% !important;
        position: absolute;
        /* Or fixed to cover */
        top: 0;
        left: 0;
        z-index: 50;
        /* Higher Z-Index */
        background: var(--dark-bg);
        /* Opaque background */
    }

    /* Toggle visibility on mobile */
    .leads-layout.mobile-chat-active .lead-detail-panel {
        display: flex !important;
    }

    .leads-layout.mobile-chat-active .leads-list-panel {
        display: none !important;
    }

    /* Chat Area Optimization */
    .chat-area {
        height: 100% !important;
        background: var(--dark-bg);
    }

    .smart-profile {
        padding: 10px !important;
        background: var(--card-bg);
        border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    }

    .profile-data-grid,
    .profile-id {
        display: none !important;
    }

    .chat-history {
        padding: 10px !important;
    }

    .chat-input-area {
        padding: 10px !important;
        background: var(--card-bg);
        gap: 5px;
    }

    .chat-input-area button {
        width: 42px !important;
        height: 42px !important;
        font-size: 1.1rem !important;
    }

    .chat-input-area input {
        border-radius: 20px !important;
        padding: 10px 15px !important;
    }

    /* Grids & Tables */
    .crm-grid-layout,
    .resource-grid,
    .form-grid {
        grid-template-columns: 1fr !important;
        padding: 10px;
    }

    .header-actions {
        flex-direction: column;
        padding: 15px;
        gap: 10px;
    }

    .desktop-only {
        display: none !important;
    }

    .desktop-only-hide {
        display: inline-block !important;
    }
}

@media (min-width: 769px) {
    .desktop-only-hide {
        display: none !important;
    }
}