@import url('https://fonts.googleapis.com/css2?family=Lexend+Deca:wght@300;400;500;600;700&display=swap');

/* ==================== Variables ==================== */
:root {
    --primary: #4f6ef7;
    --primary-dark: #3b5de7;
    --primary-light: #8da4fb;
    --bg: #f5f7fa;
    --bg-secondary: #ffffff;
    --bg-tertiary: #f0f2f5;
    --text: #1a202c;
    --text-secondary: #718096;
    --border: #e2e8f0;
    --success: #48bb78;
    --danger: #fc8181;
    --sidebar-bg: #ffffff;
    --sidebar-text: #64748b;
    --sidebar-text-bright: #1e293b;
    --sidebar-border: #e2e8f0;
    --sidebar-hover: #f1f5f9;
    --shadow: 0 1px 3px rgba(0,0,0,0.08);
}

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

body {
    font-family: 'Lexend Deca', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 15px;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    height: 100vh;
    overflow: hidden;
}

/* ==================== Login ==================== */
.login-overlay {
    position: fixed;
    inset: 0;
    background: var(--bg);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
}

.login-box {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 36px;
    width: 340px;
    text-align: center;
    box-shadow: 0 4px 20px rgba(0,0,0,0.1);
}

.login-box h2 { margin-bottom: 4px; font-size: 1.4rem; }
.login-box p { color: var(--text-secondary); font-size: 0.9rem; margin-bottom: 18px; }
.login-box .input { width: 100%; margin-bottom: 14px; text-align: center; font-size: 1rem; padding: 10px; }
.login-box .btn { width: 100%; font-size: 1rem; padding: 10px; }
.login-error { display: block; color: var(--danger); font-size: 0.85rem; margin-top: 8px; font-weight: 600; }

/* ==================== App Layout ==================== */
.app-layout {
    display: flex;
    height: 100vh;
    width: 100vw;
}

.app-layout.app-hidden {
    display: none;
}

/* ==================== Sidebar ==================== */
.sidebar {
    width: 260px;
    min-width: 260px;
    background: var(--sidebar-bg);
    color: var(--sidebar-text);
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--sidebar-border);
    overflow-y: auto;
    transition: margin-left 0.2s ease;
}

.sidebar.collapsed {
    margin-left: -260px;
}

.sidebar-top {
    padding: 20px 18px 16px;
    border-bottom: 1px solid var(--sidebar-border);
}

.sidebar-brand {
    font-size: 1.4rem;
    font-weight: 700;
    color: var(--sidebar-text-bright);
    letter-spacing: -0.5px;
}

.sidebar-subtitle {
    font-size: 0.8rem;
    color: var(--sidebar-text);
    margin-top: 2px;
}

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

.sidebar-section {
    padding: 10px 18px;
}

.sidebar-section-title {
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: var(--text-secondary);
    margin-bottom: 8px;
}

.sidebar-label {
    display: block;
    font-size: 0.8rem;
    color: var(--sidebar-text);
    margin: 8px 0 4px;
    font-weight: 500;
}

.sidebar-input {
    width: 100%;
    background: var(--sidebar-hover);
    border-color: var(--sidebar-border);
    color: var(--sidebar-text-bright);
    font-size: 0.9rem;
    padding: 8px 10px;
}

.sidebar-input:focus {
    border-color: var(--primary);
    background: var(--sidebar-hover);
    box-shadow: 0 0 0 2px rgba(79,110,247,0.2);
}

.toggle-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 10px;
}

.sidebar-bottom {
    border-top: 1px solid var(--sidebar-border);
    padding: 8px 0 14px;
}

.sidebar-user {
    font-size: 0.85rem;
    color: var(--sidebar-text);
    font-weight: 500;
    padding: 4px 0 8px;
}

/* ==================== Chips Autocomplete ==================== */
.chips-wrapper {
    position: relative;
}

.chips-container {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 8px 10px;
    border: 1px solid var(--sidebar-border);
    border-radius: 8px;
    background: var(--sidebar-hover);
    min-height: 44px;
    align-items: center;
    cursor: text;
}

.chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 10px;
    background: var(--primary);
    color: white;
    border-radius: 6px;
    font-size: 0.9rem;
    font-weight: 600;
    white-space: nowrap;
    line-height: 1.2;
}

.chip-remove {
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: rgba(255,255,255,0.25);
    font-size: 1rem;
    font-weight: 700;
    line-height: 1;
    opacity: 0.9;
    transition: background 0.15s;
    flex-shrink: 0;
}
.chip-remove:hover { background: rgba(255,255,255,0.45); opacity: 1; }

.chips-input {
    border: none;
    outline: none;
    background: transparent;
    font-size: 0.95rem;
    min-width: 100px;
    flex: 1;
    padding: 6px 4px;
    font-family: 'Lexend Deca', sans-serif;
    color: var(--sidebar-text-bright);
}

.chips-dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 8px;
    margin-top: 4px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.12);
    z-index: 10;
    max-height: 260px;
    overflow-y: auto;
}

.chips-dropdown-item {
    padding: 10px 14px;
    cursor: pointer;
    font-size: 0.95rem;
    font-weight: 500;
    color: var(--text);
    border-bottom: 1px solid var(--bg-tertiary);
}
.chips-dropdown-item:last-child { border-bottom: none; }

.chips-dropdown-item:hover {
    background: var(--primary);
    color: white;
}

/* ==================== Main Content ==================== */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-width: 0;
}

/* ==================== Top Bar ==================== */
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 20px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    gap: 12px;
    flex-wrap: wrap;
    flex-shrink: 0;
}

.topbar-left, .topbar-right {
    display: flex;
    align-items: center;
    gap: 10px;
}

.last-updated {
    color: var(--text-secondary);
    font-size: 0.85rem;
}

.search-input {
    width: 220px;
    border-radius: 6px;
}

.result-count {
    color: var(--text-secondary);
    font-size: 0.85rem;
    font-weight: 500;
    min-width: 20px;
}

.sidebar-toggle-btn {
    display: none;
}

/* ==================== Buttons ==================== */
.btn {
    padding: 8px 16px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 600;
    transition: all 0.15s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    font-family: 'Lexend Deca', sans-serif;
    white-space: nowrap;
}

.btn:hover { opacity: 0.9; }
.btn:active { transform: scale(0.97); }

.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary { background: transparent; color: var(--text); border: 1px solid var(--border); }
.sidebar .btn-secondary { color: var(--sidebar-text-bright); border-color: var(--sidebar-border); }

.main-content .btn-secondary { color: var(--text); border-color: var(--border); background: var(--bg-secondary); }
.main-content .btn-secondary:hover { background: var(--bg-tertiary); }

.btn-large { padding: 10px 24px; }
.btn-sm { padding: 6px 12px; font-size: 0.85rem; }
.btn-block { width: 100%; }
.btn-icon { width: 38px; height: 38px; padding: 0; font-size: 1.2rem; }

/* ==================== Inputs ==================== */
.input {
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 0.95rem;
    background: var(--bg-tertiary);
    color: var(--text);
    transition: border-color 0.15s;
    font-family: 'Lexend Deca', sans-serif;
}

.input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(79,110,247,0.1);
}

.input-small { width: 60px; padding: 6px 8px; font-size: 0.85rem; }

/* Toggle */
.toggle { position: relative; display: inline-block; width: 40px; height: 22px; }
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle-slider {
    position: absolute; cursor: pointer; inset: 0;
    background: var(--sidebar-border); border-radius: 22px; transition: 0.25s;
}
.toggle-slider:before {
    position: absolute; content: ""; height: 16px; width: 16px;
    left: 3px; bottom: 3px; background: white; border-radius: 50%; transition: 0.25s;
}
.toggle input:checked + .toggle-slider { background: var(--primary); }
.toggle input:checked + .toggle-slider:before { transform: translateX(18px); }

.auto-refresh {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 5px 12px;
    background: var(--bg-tertiary);
    border-radius: 6px;
    border: 1px solid var(--border);
    font-size: 0.85rem;
}

/* ==================== Status Bar ==================== */
.status-bar {
    padding: 8px 20px;
    font-weight: 500;
    font-size: 0.9rem;
    background: #ebf5ff;
    color: var(--primary-dark);
    border-bottom: 1px solid var(--primary-light);
    flex-shrink: 0;
}
.status-bar.error { background: #fff5f5; color: #e53e3e; border-color: #fc8181; }
.status-bar.success { background: #f0fff4; color: #38a169; border-color: #68d391; }
.status-bar.hidden { display: none; }

/* ==================== Summary ==================== */
.summary-bar {
    display: flex;
    gap: 1px;
    background: var(--border);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.summary-item {
    flex: 1;
    background: var(--bg-secondary);
    padding: 12px 16px;
    text-align: center;
}

.summary-value {
    display: block;
    font-size: 1.3rem;
    font-weight: 700;
    color: var(--primary-dark);
}

.summary-label {
    display: block;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-top: 2px;
}

/* ==================== Table ==================== */
.table-container {
    flex: 1;
    overflow: auto;
}

.data-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
}

.data-table th {
    background: #dbeafe;
    color: var(--primary-dark);
    padding: 12px 16px;
    text-align: left;
    font-weight: 600;
    font-size: 0.85rem;
    cursor: pointer;
    user-select: none;
    white-space: nowrap;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 1;
}

.data-table th:hover { background: var(--primary-light); color: white; }

.data-table th .sort-icon {
    margin-left: 4px; display: inline-flex; flex-direction: column;
    vertical-align: middle; font-size: 0.55rem; color: var(--text-secondary); opacity: 0.3;
}
.data-table th:hover .sort-icon { opacity: 0.6; }
.data-table th.sorted .sort-icon { opacity: 1; }
.data-table th .sort-icon::before { content: "▲"; line-height: 1; }
.data-table th .sort-icon::after { content: "▼"; line-height: 1; }
.data-table th.sorted-asc .sort-icon::after, .data-table th.sorted-desc .sort-icon::before { opacity: 0.2; }
.data-table th.sorted-asc .sort-icon::before { color: var(--primary-dark); }
.data-table th.sorted-desc .sort-icon::after { color: var(--primary-dark); }

.data-table td {
    padding: 10px 16px;
    border-bottom: 1px solid var(--border);
    font-size: 0.95rem;
    color: var(--text);
}

.data-table tr:last-child td { border-bottom: none; }
.data-table tr:hover { background: #eef2ff; }

.data-table tr.empty-row td {
    text-align: center; color: var(--text-secondary);
    padding: 60px; font-size: 1rem;
}

/* Status Badge */
.status-badge {
    display: inline-block; padding: 3px 12px;
    border-radius: 20px; font-size: 0.8rem; font-weight: 600;
}
.status-badge.active { background: #f0fff4; color: #38a169; }
.status-badge.paused { background: #fffff0; color: #d69e2e; }
.status-badge.unknown { background: var(--bg-tertiary); color: var(--text-secondary); }

.roas-high { color: #38a169; font-weight: 600; }
.roas-medium { color: #d69e2e; }
.roas-low { color: #e53e3e; }
.profit-positive { color: #38a169; font-weight: 600; }
.profit-negative { color: #e53e3e; font-weight: 600; }
.profit-zero { color: var(--text-secondary); }

/* ==================== Chart ==================== */
.chart-container {
    display: none;
    padding: 16px 20px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    height: 300px;
    flex-shrink: 0;
}

/* ==================== Utility ==================== */
.hidden { display: none !important; }
.status-text { font-size: 0.8rem; font-weight: 600; color: #38a169; margin-top: 6px; display: block; }
.status-text.error { color: #e53e3e; }
.loading { opacity: 0.7; pointer-events: none; }
.loading::after { content: " Đang xử lý..."; font-weight: 600; }

/* ==================== Responsive ==================== */

/* Tablet */
@media (max-width: 900px) {
    .sidebar {
        position: fixed; left: 0; top: 0; bottom: 0; z-index: 100;
        box-shadow: 4px 0 20px rgba(0,0,0,0.15);
    }
    .sidebar.collapsed { margin-left: -260px; box-shadow: none; }
    .sidebar-toggle-btn { display: flex; }
    .main-content { width: 100vw; }
}

/* Mobile */
@media (max-width: 600px) {
    body { font-size: 14px; }

    /* Login */
    .login-box { width: 90vw; max-width: 360px; padding: 28px 20px; }
    .login-box .input { font-size: 1rem; padding: 12px; }
    .login-box .btn { padding: 12px; font-size: 1rem; }

    /* Sidebar */
    .sidebar { width: 280px; min-width: 280px; }
    .sidebar.collapsed { margin-left: -280px; }
    .sidebar-section { padding: 12px 16px; }

    /* Chips - bigger on mobile */
    .chips-container { min-height: 48px; gap: 8px; padding: 10px 12px; }
    .chip { padding: 8px 12px; font-size: 0.95rem; border-radius: 8px; }
    .chip-remove { width: 26px; height: 26px; font-size: 1.1rem; }
    .chips-input { font-size: 1rem; padding: 8px 4px; }
    .chips-dropdown-item { padding: 14px 16px; font-size: 1rem; }

    /* Topbar */
    .topbar { flex-direction: column; align-items: stretch; padding: 10px 12px; gap: 8px; }
    .topbar-left { flex-wrap: wrap; gap: 8px; }
    .topbar-right { flex-wrap: wrap; gap: 8px; }
    .search-input { width: 100%; flex: 1; }
    .btn-large { padding: 10px 16px; flex: 1; }
    .auto-refresh { font-size: 0.8rem; }
    .last-updated { font-size: 0.78rem; width: 100%; }

    /* Summary */
    .summary-bar { flex-wrap: wrap; }
    .summary-item { flex: 1 1 48%; min-width: 0; padding: 10px 8px; }
    .summary-value { font-size: 1.1rem; }
    .summary-label { font-size: 0.65rem; letter-spacing: 0.3px; }

    /* Table */
    .table-container { -webkit-overflow-scrolling: touch; }
    .data-table th { padding: 10px 12px; font-size: 0.8rem; }
    .data-table td { padding: 10px 12px; font-size: 0.88rem; white-space: nowrap; }

    /* Buttons */
    .btn { padding: 10px 14px; font-size: 0.88rem; }
    .btn-icon { width: 42px; height: 42px; font-size: 1.3rem; }
    .btn-sm { padding: 8px 12px; }
    .input { padding: 10px 12px; font-size: 0.95rem; }

    /* Chart */
    .chart-container { height: 220px; padding: 12px; }

    /* Status bar */
    .status-bar { padding: 8px 12px; font-size: 0.85rem; }
}

/* Small mobile */
@media (max-width: 400px) {
    .summary-item { flex: 1 1 100%; }
    .topbar-right { justify-content: stretch; }
    .topbar-right .btn { flex: 1; }
}

/* Scrollbar */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--text-secondary); border-radius: 6px; }
::-webkit-scrollbar-thumb:hover { background: var(--primary); }
