/* ── Reset & Variables ─────────────────────────── */
:root {
    --bg: #f0f2f5;
    --card: #ffffff;
    --text: #1a1a2e;
    --text-muted: #6b7280;
    --border: #e5e7eb;
    --primary: #3B82F6;
    --primary-hover: #2563EB;
    --success: #10B981;
    --warning: #F59E0B;
    --danger: #EF4444;
    --purple: #8B5CF6;
    --sidebar-bg: #1a1a2e;
    --sidebar-text: #e5e7eb;
    --sidebar-active: #3B82F6;
    --radius: 8px;
    --shadow: 0 1px 3px rgba(0,0,0,0.1);
    --shadow-lg: 0 4px 12px rgba(0,0,0,0.1);
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
}

/* ── Sidebar ──────────────────────────────────── */
.sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: 240px;
    background: var(--sidebar-bg);
    color: var(--sidebar-text);
    display: flex;
    flex-direction: column;
    z-index: 100;
    overflow-y: auto;
}

.sidebar-header {
    padding: 20px;
    border-bottom: 1px solid rgba(255,255,255,0.1);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}

.sidebar-header-btns {
    display: flex;
    align-items: center;
    gap: 4px;
}

.sidebar-toggle {
    background: transparent;
    border: none;
    color: rgba(255,255,255,0.55);
    cursor: pointer;
    padding: 6px 8px;
    border-radius: 6px;
    font-size: 14px;
    transition: background 0.15s, color 0.15s;
    flex-shrink: 0;
}
.sidebar-toggle:hover { background: rgba(255,255,255,0.08); color: #fff; }

/* ── Collapsed sidebar ──────────────────────────── */
body.sidebar-collapsed .sidebar { width: 64px; }
body.sidebar-collapsed .sidebar .logo span,
body.sidebar-collapsed .sidebar .nav-links a span,
body.sidebar-collapsed .sidebar .nav-section,
body.sidebar-collapsed .sidebar .user-info span,
body.sidebar-collapsed .sidebar .logout-btn span,
body.sidebar-collapsed .sidebar .logout-btn:not(:hover) { /* noop to be safe */ }
body.sidebar-collapsed .sidebar .logo span,
body.sidebar-collapsed .sidebar .nav-links a span,
body.sidebar-collapsed .sidebar .nav-section,
body.sidebar-collapsed .sidebar .user-info span {
    display: none;
}
body.sidebar-collapsed .sidebar .logout-btn {
    font-size: 0;
    text-align: center;
}
body.sidebar-collapsed .sidebar .logout-btn i {
    font-size: 14px;
}
body.sidebar-collapsed .sidebar-header-btns { flex-direction: column; gap: 4px; }
body.sidebar-collapsed .sidebar-header { padding: 16px 10px; justify-content: center; flex-direction: column; gap: 10px; }
body.sidebar-collapsed .sidebar .logo { justify-content: center; gap: 0; }
body.sidebar-collapsed .sidebar-footer { padding: 12px 10px; text-align: center; }
body.sidebar-collapsed .sidebar .user-info { justify-content: center; }
body.sidebar-collapsed .sidebar .nav-links a { justify-content: center; padding: 12px 0; }
body.sidebar-collapsed .sidebar .nav-links a i { width: auto; }
body.sidebar-collapsed main.with-sidebar { margin-left: 64px; }

.logo {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 18px;
    font-weight: 700;
}

.logo i { color: var(--primary); font-size: 22px; }

.nav-links {
    list-style: none;
    padding: 10px 0;
    flex: 1;
}

.nav-section {
    padding: 15px 20px 5px;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: rgba(255,255,255,0.4);
}

.nav-links a {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 20px;
    color: var(--sidebar-text);
    text-decoration: none;
    font-size: 14px;
    transition: all 0.2s;
}

.nav-links a:hover { background: rgba(255,255,255,0.08); }
.nav-links a.active {
    background: var(--sidebar-active);
    color: white;
    border-radius: 0;
}
.nav-links a i { width: 20px; text-align: center; }

.sidebar-footer {
    padding: 15px 20px;
    border-top: 1px solid rgba(255,255,255,0.1);
}

.user-info {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    margin-bottom: 8px;
}

.role-badge {
    font-size: 10px;
    padding: 2px 8px;
    border-radius: 10px;
    text-transform: uppercase;
    font-weight: 600;
}
.role-badge.admin { background: var(--primary); color: white; }
.role-badge.viewer { background: var(--success); color: white; }

.logout-btn {
    display: block;
    color: rgba(255,255,255,0.6);
    text-decoration: none;
    font-size: 13px;
}
.logout-btn:hover { color: white; }

/* ── Main Content ─────────────────────────────── */
main.with-sidebar { margin-left: 240px; padding: 24px; }
main:not(.with-sidebar) { padding: 0; }

.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
}
.page-header h1 { font-size: 24px; font-weight: 600; }
.page-header h1 i { color: var(--primary); margin-right: 8px; }

.header-controls { display: flex; gap: 10px; align-items: center; }

/* ── KPI Cards ────────────────────────────────── */
.kpi-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 16px;
    margin-bottom: 24px;
}

.kpi-card {
    background: var(--card);
    border-radius: var(--radius);
    padding: 20px;
    display: flex;
    align-items: center;
    gap: 16px;
    box-shadow: var(--shadow);
}

a.kpi-card, a.kpi-link {
    color: inherit;
    text-decoration: none;
    transition: transform 0.15s, box-shadow 0.15s;
    cursor: pointer;
}
a.kpi-card:hover, a.kpi-link:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(0,0,0,0.08);
}
.kpi-edit-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 2px 4px;
    margin-left: 6px;
    border-radius: 4px;
    font-size: 10px;
    opacity: 0.4;
    transition: opacity 0.15s, background 0.15s;
}
.kpi-edit-btn:hover { opacity: 1; background: var(--bg); color: var(--primary); }
.kpi-card:hover .kpi-edit-btn { opacity: 1; }

.kpi-link-arrow {
    font-size: 10px;
    margin-left: 4px;
    opacity: 0;
    transition: opacity 0.15s, transform 0.15s;
    color: var(--primary);
}
a.kpi-link:hover .kpi-link-arrow {
    opacity: 1;
    transform: translateX(2px);
}

.kpi-icon {
    width: 48px;
    height: 48px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
}
.kpi-icon.blue { background: #EBF5FF; color: var(--primary); }
.kpi-icon.green { background: #ECFDF5; color: var(--success); }
.kpi-icon.orange { background: #FFFBEB; color: var(--warning); }
.kpi-icon.purple { background: #F5F3FF; color: var(--purple); }

.kpi-label { font-size: 13px; color: var(--text-muted); }
.kpi-value { font-size: 22px; font-weight: 700; }

/* ── Charts ───────────────────────────────────── */
.charts-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    margin-bottom: 24px;
}

.chart-card {
    background: var(--card);
    border-radius: var(--radius);
    padding: 20px;
    box-shadow: var(--shadow);
}
.chart-card h3 { font-size: 15px; margin-bottom: 12px; color: var(--text-muted); }

/* ── Data Cards & Tables ──────────────────────── */
.data-card {
    background: var(--card);
    border-radius: var(--radius);
    padding: 20px;
    box-shadow: var(--shadow);
    margin-bottom: 24px;
}
.data-card h3 { font-size: 16px; margin-bottom: 16px; }
.data-card h3 i { color: var(--primary); margin-right: 6px; }

.card-header-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
}

.table-responsive { overflow-x: auto; -webkit-overflow-scrolling: touch; }

.data-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}
.data-table th {
    background: var(--bg);
    padding: 10px 12px;
    text-align: left;
    font-weight: 600;
    color: var(--text-muted);
    border-bottom: 2px solid var(--border);
    white-space: nowrap;
}
.data-table td {
    padding: 8px 12px;
    border-bottom: 1px solid var(--border);
}
.data-table tr:hover { background: #f9fafb; }
.data-table .totals-row td {
    background: var(--bg);
    font-weight: 700;
    border-top: 2px solid var(--border);
}

.inactive-row { opacity: 0.5; }

/* ── Forms ────────────────────────────────────── */
.form-group { margin-bottom: 12px; flex: 1; }
.form-group label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    margin-bottom: 4px;
    color: var(--text-muted);
}

input[type="text"], input[type="password"], input[type="number"],
input[type="email"], input[type="date"], select, textarea {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 14px;
    background: white;
}
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(59,130,246,0.1);
}

.form-select {
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 14px;
    background: white;
}

.form-row { display: flex; gap: 16px; }
.form-actions { display: flex; gap: 8px; margin-top: 12px; }

.filter-bar {
    display: flex;
    gap: 12px;
    margin-bottom: 16px;
    flex-wrap: wrap;
    align-items: end;
}
.filter-form { display: flex; gap: 12px; align-items: end; flex-wrap: wrap; }

.branch-filter { display: flex; gap: 12px; margin-bottom: 16px; }

/* ── Buttons ──────────────────────────────────── */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border: none;
    border-radius: var(--radius);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    text-decoration: none;
}
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-outline { background: white; border: 1px solid var(--border); color: var(--text); }
.btn-outline:hover { background: var(--bg); }
.btn-danger { background: var(--danger); color: white; }
.btn-danger:hover { background: #dc2626; }
.btn-success { background: var(--success); color: white; }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-full { width: 100%; justify-content: center; }

/* ── Status Badges ────────────────────────────── */
.status-badge {
    font-size: 11px;
    padding: 3px 10px;
    border-radius: 10px;
    font-weight: 600;
}
.status-badge.active { background: #ECFDF5; color: #059669; }
.status-badge.inactive { background: #FEF2F2; color: #DC2626; }

/* ── Campaign Tags ────────────────────────────── */
.campaign-tag {
    display: inline-block;
    font-size: 11px;
    padding: 2px 8px;
    border-radius: 4px;
    margin: 2px;
}
.campaign-dot {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    margin-right: 4px;
    vertical-align: middle;
}

/* ── Spend Table ──────────────────────────────── */
.spend-table {
    min-width: 1400px;          /* force horizontal scroll so columns aren't crushed */
}
.spend-table th { white-space: nowrap; }

.spend-input {
    width: 100%;
    padding: 6px 8px;
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 13px;
    text-align: right;
    -moz-appearance: textfield;
}
.spend-input::-webkit-outer-spin-button,
.spend-input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.spend-input:focus {
    border-color: var(--primary);
    outline: none;
    box-shadow: 0 0 0 2px rgba(59,130,246,0.15);
}

.spend-input-sm {
    width: 100%;
    min-width: 80px;
    padding: 5px 6px;
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 13px;
    text-align: right;
    -moz-appearance: textfield;
}
.spend-input-sm::-webkit-outer-spin-button,
.spend-input-sm::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.spend-input-sm:focus {
    border-color: var(--primary);
    outline: none;
    box-shadow: 0 0 0 2px rgba(59,130,246,0.15);
}
.spend-input-sm::placeholder { color: #ccc; }

.month-col { text-align: center; font-size: 13px; }
.month-cell { padding: 4px 3px !important; }
.month-cell.has-campaign { background: rgba(59,130,246,0.04); }
.vendor-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 280px; }
.vendor-total { font-weight: 600; white-space: nowrap; text-align: right; padding-right: 8px !important; }

/* ── Split Modal ──────────────────────────────── */
.modal-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
}
.modal {
    background: white;
    border-radius: var(--radius);
    width: 480px;
    max-height: 80vh;
    overflow-y: auto;
    box-shadow: var(--shadow-lg);
}
.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
}
.modal-body { padding: 20px; }
.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    padding: 16px 20px;
    border-top: 1px solid var(--border);
}
.split-row {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 0;
}
.split-row label { flex: 1; display: flex; align-items: center; gap: 6px; }
.split-pct { width: 60px; text-align: right; }
.split-total { margin-top: 12px; font-weight: 600; text-align: right; }

/* ── Login ────────────────────────────────────── */
.login-container {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
}
.login-card {
    background: white;
    border-radius: 12px;
    padding: 40px;
    width: 400px;
    box-shadow: var(--shadow-lg);
}
.login-header {
    text-align: center;
    margin-bottom: 32px;
}
.login-header i { font-size: 40px; color: var(--primary); margin-bottom: 12px; }
.login-header h1 { font-size: 22px; margin-bottom: 4px; }
.login-header p { color: var(--text-muted); font-size: 14px; }
.login-form .form-group { margin-bottom: 16px; }
.login-form label { display: flex; align-items: center; gap: 6px; }

/* ── Flash Messages ───────────────────────────── */
.flash-messages { margin-bottom: 16px; }
.flash {
    padding: 12px 16px;
    border-radius: var(--radius);
    font-size: 14px;
    margin-bottom: 8px;
}
.flash.error { background: #FEF2F2; color: #DC2626; border: 1px solid #FECACA; }
.flash.success { background: #ECFDF5; color: #059669; border: 1px solid #A7F3D0; }

/* ── Responsive – Tablet ─────────────────────── */
@media (max-width: 1024px) {
    .charts-row { grid-template-columns: 1fr; }
}

/* ── Responsive – Mobile ─────────────────────── */
@media (max-width: 768px) {
    /* Hide desktop sidebar */
    .sidebar { display: none !important; }

    /* Main content: no sidebar offset, bottom-nav clearance */
    main.with-sidebar {
        margin-left: 0;
        padding: 16px 12px 80px;
    }

    /* ── Page header ── */
    .page-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
        margin-bottom: 16px;
    }
    .page-header h1 { font-size: 18px; }
    .header-controls {
        width: 100%;
        flex-wrap: wrap;
        gap: 8px;
    }
    .header-controls .btn,
    .header-controls .form-select,
    .header-controls select {
        font-size: 13px;
        padding: 6px 10px;
    }

    /* ── KPI grid ── */
    .kpi-grid {
        grid-template-columns: 1fr 1fr;
        gap: 10px;
        margin-bottom: 16px;
    }
    .kpi-card {
        padding: 12px;
        gap: 10px;
    }
    .kpi-icon { width: 36px; height: 36px; font-size: 16px; border-radius: 8px; }
    .kpi-value { font-size: 14px; font-weight: 600; }
    .kpi-label { font-size: 11px; }

    /* ── Charts ── */
    .charts-row { grid-template-columns: 1fr; gap: 12px; }
    .chart-card { padding: 14px; }
    .chart-card h3 { font-size: 13px; }

    /* ── Data cards & tables ── */
    .data-card { padding: 14px; margin-bottom: 16px; }
    .data-card h3 { font-size: 14px; margin-bottom: 10px; }

    .table-responsive {
        margin: 0 -14px;
        padding: 0 14px;
    }
    .data-table { font-size: 12px; }
    .data-table th { padding: 8px 6px; font-size: 11px; }
    .data-table td { padding: 6px; }

    /* ── Filter bar ── */
    .filter-bar, .filter-form {
        flex-direction: column;
        gap: 8px;
        align-items: stretch;
    }
    .filter-bar .form-group,
    .filter-form .form-group { width: 100%; }
    .filter-bar select,
    .filter-form select { width: 100%; }
    .branch-filter {
        flex-direction: column;
        gap: 8px;
    }

    /* ── Form rows ── */
    .form-row { flex-direction: column; gap: 8px; }
    .form-actions { flex-wrap: wrap; }

    /* ── Buttons ── */
    .btn { padding: 8px 14px; font-size: 13px; }
    .btn-sm { padding: 5px 10px; font-size: 11px; }

    /* ── Modals – full-width on mobile ── */
    .modal-overlay {
        align-items: flex-end;
        padding: 0;
    }
    .modal {
        width: 100%;
        max-width: 100%;
        max-height: 85vh;
        border-radius: 16px 16px 0 0;
    }
    .modal-header { padding: 14px 16px; }
    .modal-body { padding: 16px; }
    .modal-footer { padding: 12px 16px; }
    .modal-body input[type="number"],
    .modal-body input[type="text"],
    .modal-body select { font-size: 15px; }

    /* ── Login ── */
    .login-card {
        width: calc(100% - 32px);
        max-width: 400px;
        padding: 24px;
    }
    .login-header i { font-size: 32px; }
    .login-header h1 { font-size: 18px; }

    /* ── Flash messages ── */
    .flash { font-size: 13px; padding: 10px 12px; }

    /* ── Spend table specific ── */
    .spend-table { min-width: 900px; }
    .spend-input-sm { font-size: 12px; padding: 4px 5px; min-width: 60px; }
    .vendor-name { max-width: 120px; font-size: 12px; }
    .month-cell { padding: 3px 2px !important; }

    /* ── Card header row ── */
    .card-header-row {
        flex-direction: column;
        align-items: flex-start;
        gap: 8px;
    }
}

/* ── Mobile Bottom Navigation ─────────────────── */
.mobile-bottom-nav {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--sidebar-bg);
    z-index: 150;
    padding: 0;
    box-shadow: 0 -2px 12px rgba(0,0,0,0.15);
}

@media (max-width: 768px) {
    .mobile-bottom-nav {
        display: flex;
        justify-content: space-around;
        align-items: stretch;
    }
}

.mobile-bottom-nav a {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 8px 4px 10px;
    color: rgba(255,255,255,0.55);
    text-decoration: none;
    font-size: 10px;
    font-weight: 500;
    gap: 3px;
    transition: color 0.15s;
    -webkit-tap-highlight-color: transparent;
}
.mobile-bottom-nav a i { font-size: 18px; }
.mobile-bottom-nav a.active {
    color: var(--primary);
}
.mobile-bottom-nav a:active {
    color: white;
}

/* Safe area (notch) padding on modern iPhones */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
    .mobile-bottom-nav {
        padding-bottom: env(safe-area-inset-bottom);
    }
    @media (max-width: 768px) {
        main.with-sidebar {
            padding-bottom: calc(80px + env(safe-area-inset-bottom));
        }
    }
}

/* ── Mobile Admin Drawer ─────────────────────── */
.mobile-admin-drawer {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    z-index: 200;
}

.mobile-admin-overlay {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.45);
    -webkit-tap-highlight-color: transparent;
}

.mobile-admin-panel {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--card);
    border-radius: 16px 16px 0 0;
    max-height: 70vh;
    overflow-y: auto;
    padding: 0 0 20px;
    animation: slideUp 0.25s ease-out;
}

@keyframes slideUp {
    from { transform: translateY(100%); }
    to   { transform: translateY(0); }
}

.mobile-admin-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px 12px;
    border-bottom: 1px solid var(--border);
    font-size: 16px;
    font-weight: 600;
    color: var(--text);
}
.mobile-admin-header button {
    background: none;
    border: none;
    font-size: 18px;
    color: var(--text-muted);
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 6px;
}
.mobile-admin-header button:hover { background: var(--bg); }

.mobile-admin-panel > a {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 20px;
    color: var(--text);
    text-decoration: none;
    font-size: 14px;
    transition: background 0.12s;
    -webkit-tap-highlight-color: transparent;
}
.mobile-admin-panel > a:hover { background: var(--bg); }
.mobile-admin-panel > a i {
    width: 20px;
    text-align: center;
    color: var(--text-muted);
    font-size: 15px;
}

/* ── Safe area for admin panel bottom ── */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
    .mobile-admin-panel {
        padding-bottom: calc(20px + env(safe-area-inset-bottom));
    }
}
