/* ========================================================== */
/* 01. NỀN TẢNG & BIẾN CSS (BASE & VARIABLES) */
/* ========================================================== */
:root {
    --color-primary: #007bff;
    --color-secondary: #0056b3;
    --color-sidebar-bg: #2c3e50;
    --color-sidebar-text: #ecf0f1;
    --color-header-bg: #ffffff;
    --color-danger: #dc3545;
    --color-text: #333;
    --color-background: #f4f7f9;
    --color-card: #ffffff;
    --color-border: #e9ecef;
    --font-family-main: 'Roboto', sans-serif;

    /* Spacing & Sizing */
    --spacing-unit: 1rem;       /* 16px */
    --spacing-md: 1.25rem;      /* 20px - Dùng thay thế 20px cố định */
    --border-radius-base: 0.5rem; /* 8px */
    --border-radius-sm: 4px;     /* Dùng cho input/button nhỏ */
    --sidebar-width: 250px;

    /* Shadow */
    --box-shadow-light: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);
    --box-shadow-hover: 0 4px 12px rgba(0, 0, 0, 0.1);
}

/* CSS Reset cơ bản */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: var(--font-family-main);
    background-color: var(--color-background);
    color: var(--color-text);
    display: grid;
    grid-template-rows: 60px 1fr;
    min-height: 100vh;
}

/* ========================================================== */
/* 02. BỐ CỤC CHÍNH (LAYOUT - HEADER, SIDEBAR, MAIN) */
/* ========================================================== */

/* --- HEADER --- */
.app-header {
    grid-row: 1;
    height: 60px;
    background-color: var(--color-header-bg);
    border-bottom: 1px solid var(--color-border);
    padding: 0 var(--spacing-unit);
    display: flex;
    align-items: center;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
    position: sticky;
    top: 0;
    z-index: 20;
}

.logo-text {
    font-size: 1.2rem;
    font-weight: 700;
    color: var(--color-primary);
    margin-right: var(--spacing-md);
}

.header-title {
    font-size: 1.1rem;
    font-weight: 500;
    color: var(--color-text);
    margin-right: auto;
}

/* --- MAIN LAYOUT (Sidebar + Content) --- */
.app-layout {
    grid-row: 2;
    display: grid;
    grid-template-columns: var(--sidebar-width) 1fr;
    width: 100%;
}

.sidebar {
    background-color: var(--color-sidebar-bg);
    color: var(--color-sidebar-text);
    padding: var(--spacing-unit) 0;
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 60px;
    height: calc(100vh - 60px);
    overflow-y: auto;
    z-index: 10;
}

.content-area {
    background-color: var(--color-background);
    padding: var(--spacing-unit);
    overflow-y: auto;
}

.container {
    max-width: 900px;
    background-color: var(--color-card);
    padding: 2rem;
    border-radius: var(--border-radius-base);
    box-shadow: var(--box-shadow-light);
    margin: 0 auto 2rem auto;
}

/* Ẩn thanh tab trên desktop (sử dụng trên mobile) */
.hidden-on-desktop {
    display: none;
}

/* ========================================================== */
/* 03. COMPONENTS (Thành phần Tái sử dụng: Nav, Card, Form, Button) */
/* ========================================================== */

/* --- User Status & Buttons --- */
.user-status {
    display: flex;
    align-items: center;
    gap: 10px;
}
.hidden-user {
    display: none !important;
}

.login-btn { 
    background-color: var(--color-primary); 
    color: white;
    padding: 8px 15px;
    border-radius: var(--border-radius-sm);
}
.clear-btn { background-color: var(--color-danger); }
.primary-btn { background-color: var(--color-primary); }
button {
    padding: 10px 20px;
    border: none;
    border-radius: var(--border-radius-sm);
    cursor: pointer;
    font-size: 1rem;
    font-weight: 600;
    transition: background-color 0.2s;
    color: white;
}

/* --- Navigation Sidebar --- */
.tool-nav {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
}
.nav-link {
    background-color: transparent;
    border: none;
    text-align: left;
    padding: 12px var(--spacing-unit);
    color: var(--color-sidebar-text);
    cursor: pointer;
    font-size: 1rem;
    display: flex;
    align-items: center;
    text-decoration: none;
    transition: background-color 0.2s;
}
.nav-link:hover,
.nav-link.active {
    background-color: #34495e;
    color: white;
    border-left: 4px solid var(--color-primary);
    padding-left: calc(var(--spacing-unit) - 4px);
}
.nav-link .icon {
    margin-right: 10px;
    font-size: 1.2rem;
}
.sidebar-info {
    margin-top: auto;
    padding: var(--spacing-unit);
    font-size: 0.8rem;
    color: #95a5a6;
    border-top: 1px solid #34495e;
    flex-shrink: 0;
}

/* --- Tab Content (Quan trọng cho logic tab) --- */
.tab-content {
    display: none;
}
.tab-content.active {
    display: block;
}

/* --- Form & Controls --- */
.options-and-controls {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--spacing-unit);
    margin-bottom: 1.5rem;
}
textarea, input[type="text"], 
#localLoginForm input[type="text"], 
#localLoginForm input[type="password"],
input[type="number"], select {
    width: 100%;
    padding: 10px;
    border: 1px solid #ccc;
    border-radius: var(--border-radius-sm);
    font-family: var(--font-family-main);
    font-size: 14px;
    resize: vertical;
    box-sizing: border-box; 
}
.mt-10 { margin-top: 10px; }
.controls-section {
    display: flex;
    justify-content: center;
    padding: var(--spacing-unit) 0;
    border-top: 1px dashed var(--color-border);
    margin-bottom: 1rem;
}
.control-group {
    display: flex;
    gap: 1rem;
    align-items: center;
}

/* --- Card & Typography --- */
h2 {
    font-size: 1.5rem;
    color: var(--color-secondary);
    margin-bottom: var(--spacing-unit);
    font-weight: 400;
}
h3 {
    font-size: 1.1rem;
    color: var(--color-secondary);
    margin-bottom: 0.75rem;
}
.card {
    border: 1px solid var(--color-border);
    border-radius: var(--border-radius-base);
    padding: var(--spacing-unit);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}


/* ========================================================== */
/* 04. MODULES (Funnel & Dashboard) */
/* ========================================================== */
.dashboard-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: var(--spacing-md);
    margin-bottom: var(--spacing-md);
}
.stat-card {
    cursor: pointer;
    text-align: center;
    transition: transform 0.2s, box-shadow 0.2s;
    padding: 15px;
}
.stat-card:hover {
    transform: translateY(-3px);
    box-shadow: var(--box-shadow-hover);
}
.stat-value {
    font-size: 2.5em;
    font-weight: 700;
    margin: 10px 0;
    color: var(--color-primary);
}
.status-card .stat-value {
    font-size: 1.2em;
}

/* --- FUNNEL VISUALIZATION STYLES --- */
.funnel-chart {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: var(--spacing-md) 10px;
    margin-bottom: 25px;
    background-color: #f8f9fa;
    border: 1px solid var(--color-border);
}

.funnel-step {
    width: 100%;
    max-width: 450px;
    text-align: center;
    position: relative;
    margin-bottom: 10px;
    padding: 10px 0;
    font-weight: 500;
    color: white;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    transition: all 0.3s ease;
}

/* Màu sắc cho từng bước */
.step-initial { background-color: #3498db; }
.step-unique { background-color: #2ecc71; }
.step-clean { background-color: #f39c12; }
.step-final { background-color: var(--color-danger); }

/* ========================================================== */
/* 05. MODAL ĐĂNG NHẬP (FULLSCREEN OVERLAY & HIỂN THỊ TỨC THÌ) */
/* ========================================================== */

.modal {
    /* Đảm bảo display:flex để căn giữa và kích hoạt visibility/opacity */
    display: flex; 
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0,0,0,0.6); 
    z-index: 1000; 
    justify-content: center;
    align-items: center;
    
    /* ẨN BẰNG VISIBILITY VÀ OPACITY - KÍCH HOẠT TỨC THÌ BẰNG is-active */
    visibility: hidden; 
    opacity: 0;
    transition: opacity 0.2s, visibility 0.2s;
}

.modal.is-active {
    visibility: visible;
    opacity: 1;
}

.modal-content {
    background-color: var(--color-card);
    padding: 30px;
    border-radius: var(--border-radius-base);
    width: 90%;
    max-width: 400px;
    position: relative;
    box-shadow: 0 8px 16px rgba(0,0,0,0.3);
}

#loginModal {
    /* Thiết lập Modal chiếm toàn bộ màn hình */
    display: flex; 
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    /* Màu nền đen mờ */
    background-color: rgba(0, 0, 0, 0.7); 
    /* Đảm bảo Modal luôn nằm trên cùng */
    z-index: 1000; 
    /* Căn giữa nội dung Modal */
    justify-content: center;
    align-items: center;
    
    /* ẨN MẶC ĐỊNH & THÊM HIỆU ỨNG CHUYỂN ĐỔI */
    visibility: hidden; 
    opacity: 0;
    transition: opacity 0.2s, visibility 0.2s;
}

/* Class kích hoạt: Hiển thị Modal ngay lập tức */
#loginModal.is-active {
    visibility: visible;
    opacity: 1;
}

/* Nội dung bên trong Modal */
#loginModal .modal-content {
    background-color: var(--color-card, #ffffff);
    padding: 30px;
    border-radius: var(--border-radius-base, 0.5rem);
    width: 90%;
    max-width: 400px; /* Giới hạn kích thước hộp nội dung */
    position: relative;
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3);
    /* Đảm bảo nội dung không bị che khuất bởi background-color của modal */
    transform: translateY(0); 
    transition: transform 0.3s ease-out;
}

/* Xử lý cho mobile: Nội dung modal cũng full màn hình */
@media (max-width: 768px) {
    #loginModal .modal-content {
        margin: 0; 
        width: 100%;
        height: 100%;
        max-width: 100%;
        border-radius: 0;
        overflow-y: auto; 
    }
}

.separator {
    text-align: center;
    margin: 15px 0;
    font-size: 12px;
    color: #888;
    position: relative;
}

.separator::before,
.separator::after {
    content: '';
    position: absolute;
    top: 50%;
    width: 40%;
    height: 1px;
    background-color: var(--color-border);
}

.separator::before { left: 0; }
.separator::after { right: 0; }


/* ========================================================== */
/* 06. RESPONSIVE DESIGN */
/* ========================================================== */
@media (max-width: 768px) {
    /* --- Layout Mobile --- */
    .app-layout {
        grid-template-columns: 1fr;
    }

    .sidebar {
        display: none; /* Ẩn sidebar trên mobile */
        position: static;
        height: auto;
    }

    /* --- Tab Navigation (Thay thế Sidebar) --- */
    .hidden-on-desktop {
        display: flex;
        justify-content: space-around;
        border-bottom: 1px solid var(--color-border);
        margin-bottom: 1.5rem;
        background-color: var(--color-header-bg);
        position: sticky;
        top: 60px;
        z-index: 5;
    }
    
    .tab-nav {
        width: 100%;
    }
    .tab-link {
        padding: 10px 5px;
        font-size: 0.9rem;
        text-decoration: none;
        color: var(--color-text);
        font-weight: 500;
        border-bottom: 3px solid transparent;
        flex-grow: 1;
        text-align: center;
        transition: color 0.2s, border-bottom-color 0.2s;
    }
    .tab-link.active {
        color: var(--color-primary);
        border-bottom-color: var(--color-primary);
    }

    /* --- Components Mobile --- */
    .options-and-controls {
        grid-template-columns: 1fr; /* 1 cột trên mobile */
    }
    .control-group {
        flex-direction: column;
    }
    .control-group button {
        width: 100%;
    }
    .stat-card {
        padding: 10px;
    }

    /* --- Modal Mobile (Full màn hình) --- */
    .modal-content {
        margin: 0; 
        width: 100%;
        height: 100%;
        max-width: 100%;
        border-radius: 0;
        overflow-y: auto; /* Cho phép cuộn nếu nội dung dài */
    }
}