/* ============================================
   CAPIBridge v2 - Sidebar + Dark/Light Mode
   ============================================ */

/* ---- CSS Variables (Light Mode) ---- */
:root {
    --sidebar-width: 260px;
    --sidebar-bg: #1e2235;
    --sidebar-text: #a0aec0;
    --sidebar-text-active: #ffffff;
    --sidebar-item-hover: rgba(255,255,255,0.06);
    --sidebar-item-active: linear-gradient(135deg, #667eea, #764ba2);
    --sidebar-border: rgba(255,255,255,0.05);
    --sidebar-header-bg: #181b2e;
    --sidebar-footer-bg: #181b2e;

    --main-bg: #f5f7fb;
    --card-bg: #ffffff;
    --card-border: #e8ecf0;
    --card-shadow: 0 2px 8px rgba(0,0,0,0.05);
    --text-primary: #1a202c;
    --text-secondary: #718096;
    --text-muted: #a0aec0;
    --input-bg: #ffffff;
    --input-border: #e2e8f0;
    --table-header-bg: #f8fafc;
    --table-border: #f1f5f9;
    --primary: #667eea;
    --primary-dark: #5568d3;
    --secondary: #764ba2;
    --success: #10b981;
    --danger: #ef4444;
    --warning: #f59e0b;
    --info: #06b6d4;
    --gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

/* ---- CSS Variables (Dark Mode) ---- */
[data-theme="dark"] {
    --main-bg: #0f1117;
    --card-bg: #1a1d2e;
    --card-border: #2d3148;
    --card-shadow: 0 2px 8px rgba(0,0,0,0.3);
    --text-primary: #e2e8f0;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;
    --input-bg: #1e2235;
    --input-border: #2d3148;
    --table-header-bg: #1e2235;
    --table-border: #2d3148;
}

/* ---- Base ---- */
*, *::before, *::after { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--main-bg);
    color: var(--text-primary);
    transition: background 0.3s, color 0.3s;
    overflow-x: hidden;
}

/* ============================================
   SIDEBAR
   ============================================ */
.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: var(--sidebar-width);
    height: 100vh;
    background: var(--sidebar-bg);
    display: flex;
    flex-direction: column;
    z-index: 1000;
    overflow: hidden;
    transition: transform 0.3s ease;
}

/* Sidebar Header */
.sidebar-header {
    background: var(--sidebar-header-bg);
    padding: 20px 20px 18px;
    border-bottom: 1px solid var(--sidebar-border);
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    text-decoration: none;
    flex: 1;
}

.brand-icon {
    font-size: 22px;
    line-height: 1;
}

.brand-text {
    font-size: 18px;
    font-weight: 700;
    color: #ffffff;
    letter-spacing: -0.3px;
}

.admin-tag {
    background: #f59e0b;
    color: #000;
    font-size: 9px;
    font-weight: 800;
    padding: 2px 7px;
    border-radius: 10px;
    letter-spacing: 0.5px;
}

/* Sidebar Nav */
.sidebar-nav {
    flex: 1;
    overflow-y: auto;
    padding: 12px 0;
}

.sidebar-nav::-webkit-scrollbar { width: 0; }

.nav-group {
    padding: 0 10px;
    margin-bottom: 8px;
}

.nav-group-title {
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--sidebar-text);
    opacity: 0.5;
    padding: 10px 10px 6px;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 11px 12px;
    border-radius: 8px;
    color: var(--sidebar-text);
    text-decoration: none;
    font-size: 14px;
    font-weight: 500;
    transition: all 0.2s;
    margin-bottom: 2px;
}

.nav-item:hover {
    background: var(--sidebar-item-hover);
    color: var(--sidebar-text-active);
}

.nav-item.active {
    background: var(--sidebar-item-active);
    color: #ffffff;
    box-shadow: 0 4px 12px rgba(102,126,234,0.3);
}

.nav-item i {
    font-size: 16px;
    width: 20px;
    flex-shrink: 0;
    text-align: center;
}

/* Sidebar Footer */
.sidebar-footer {
    background: var(--sidebar-footer-bg);
    padding: 14px 16px;
    border-top: 1px solid var(--sidebar-border);
    flex-shrink: 0;
}

/* Usage mini */
.usage-mini {
    margin-bottom: 12px;
}

.text-muted-soft {
    color: var(--sidebar-text);
    font-size: 12px;
}

.usage-mini-bar {
    height: 4px;
    background: rgba(255,255,255,0.1);
    border-radius: 2px;
    overflow: hidden;
}

.usage-mini-fill {
    height: 100%;
    background: var(--gradient);
    border-radius: 2px;
    transition: width 0.3s;
}

/* Theme switch */
.theme-switch {
    display: flex;
    gap: 4px;
    background: rgba(255,255,255,0.05);
    padding: 4px;
    border-radius: 8px;
    margin-bottom: 12px;
}

.theme-btn {
    flex: 1;
    padding: 6px;
    border: none;
    border-radius: 6px;
    background: transparent;
    color: var(--sidebar-text);
    cursor: pointer;
    font-size: 13px;
    transition: all 0.2s;
}

.theme-btn.active-theme {
    background: rgba(255,255,255,0.15);
    color: #ffffff;
}

/* User panel */
.user-panel {
    display: flex;
    align-items: center;
    gap: 10px;
}

.user-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--gradient);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 14px;
    flex-shrink: 0;
}

.user-info {
    flex: 1;
    min-width: 0;
}

.user-name {
    font-size: 13px;
    font-weight: 600;
    color: #ffffff;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.user-role {
    font-size: 11px;
    color: var(--sidebar-text);
    text-transform: capitalize;
}

.user-menu-btn {
    background: none;
    border: none;
    color: var(--sidebar-text);
    cursor: pointer;
    padding: 4px 6px;
    border-radius: 6px;
    font-size: 14px;
    transition: all 0.2s;
}

.user-menu-btn:hover {
    background: rgba(255,255,255,0.1);
    color: #fff;
}

/* ============================================
   MAIN CONTENT AREA
   ============================================ */
.main-content {
    margin-left: var(--sidebar-width);
    min-height: 100vh;
    padding: 28px 32px;
    transition: margin-left 0.3s;
    background: var(--main-bg);
}

/* Flash alert */
.flash-alert {
    margin-bottom: 24px;
    border-radius: 10px;
}

/* ============================================
   MOBILE MENU TOGGLE
   ============================================ */
.mobile-menu-toggle {
    position: fixed;
    top: 16px;
    left: 16px;
    z-index: 1100;
    background: var(--sidebar-bg);
    border: none;
    color: #fff;
    width: 40px;
    height: 40px;
    border-radius: 8px;
    font-size: 18px;
    display: none;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}

@media (max-width: 991px) {
    .mobile-menu-toggle {
        display: flex;
    }
    .sidebar {
        transform: translateX(-100%);
    }
    .sidebar.open {
        transform: translateX(0);
        box-shadow: 4px 0 20px rgba(0,0,0,0.3);
    }
    .main-content {
        margin-left: 0;
        padding: 60px 16px 24px;
    }
}

/* ============================================
   CARDS
   ============================================ */
.card {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 12px;
    box-shadow: var(--card-shadow);
    transition: transform 0.2s, box-shadow 0.2s;
}

.card:hover {
    box-shadow: 0 4px 16px rgba(0,0,0,0.08);
}

/* ============================================
   STATS TILES
   ============================================ */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 16px;
    margin-bottom: 24px;
}

.stat-tile {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 12px;
    padding: 20px;
    display: flex;
    align-items: center;
    gap: 16px;
    transition: transform 0.2s;
}

.stat-tile:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.08);
}

.stat-icon-box {
    width: 52px;
    height: 52px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 22px;
    color: #fff;
    flex-shrink: 0;
}

.stat-icon-box.primary { background: linear-gradient(135deg, #667eea, #764ba2); }
.stat-icon-box.success { background: linear-gradient(135deg, #10b981, #059669); }
.stat-icon-box.warning { background: linear-gradient(135deg, #f59e0b, #d97706); }
.stat-icon-box.danger { background: linear-gradient(135deg, #ef4444, #dc2626); }
.stat-icon-box.info { background: linear-gradient(135deg, #06b6d4, #0891b2); }
.stat-icon-box.purple { background: linear-gradient(135deg, #a855f7, #7e22ce); }

.stat-tile-value {
    font-size: 28px;
    font-weight: 700;
    color: var(--text-primary);
    line-height: 1;
}

.stat-tile-label {
    color: var(--text-secondary);
    font-size: 13px;
    margin-top: 4px;
}

/* ============================================
   TABLES
   ============================================ */
.table {
    color: var(--text-primary);
}

.table thead th {
    background: var(--table-header-bg);
    color: var(--text-secondary);
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    font-weight: 600;
    border-color: var(--table-border);
    padding: 14px 16px;
}

.table td {
    border-color: var(--table-border);
    padding: 14px 16px;
    vertical-align: middle;
    color: var(--text-primary);
}

.table-hover tbody tr:hover td {
    background: var(--table-header-bg);
}

/* ============================================
   FORMS
   ============================================ */
.form-control, .form-select {
    background: var(--input-bg);
    border-color: var(--input-border);
    color: var(--text-primary);
    border-radius: 8px;
    transition: all 0.2s;
}

.form-control:focus, .form-select:focus {
    background: var(--input-bg);
    color: var(--text-primary);
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(102,126,234,0.15);
}

.form-label {
    color: var(--text-secondary);
    font-size: 13px;
    font-weight: 500;
    margin-bottom: 6px;
}

/* ============================================
   BUTTONS
   ============================================ */
.btn-primary {
    background: var(--primary);
    border-color: var(--primary);
    font-weight: 500;
}

.btn-primary:hover {
    background: var(--primary-dark);
    border-color: var(--primary-dark);
}

/* ============================================
   BADGES & STATUS
   ============================================ */
.status-badge {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 4px 10px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: 500;
}

.status-success { background: #d1fae5; color: #065f46; }
.status-failed { background: #fee2e2; color: #991b1b; }
.status-pending { background: #fef3c7; color: #92400e; }

.inv-status { display: inline-flex; padding: 4px 10px; border-radius: 12px; font-size: 11px; font-weight: 600; }
.status-paid { background: #d1fae5; color: #065f46; }
.status-sent { background: #dbeafe; color: #1e40af; }
.status-overdue { background: #fee2e2; color: #991b1b; }
.status-draft { background: #f3f4f6; color: #4b5563; }
.status-cancelled { background: #f3f4f6; color: #6b7280; }

/* ============================================
   PLATFORM BADGES
   ============================================ */
.platform-badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 6px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
}

.platform-bitrix24 { background: #dbeafe; color: #1e40af; }
.platform-hubspot { background: #fef3c7; color: #92400e; }
.platform-zoho { background: #fce7f3; color: #9f1239; }
.platform-meta { background: #dbeafe; color: #1877f2; }
.platform-google { background: #fef3c7; color: #ea8600; }
.platform-tiktok { background: #fee2e2; color: #fe2c55; }
.platform-custom { background: #e0e7ff; color: #3730a3; }

/* ============================================
   WEBHOOK URL BOX
   ============================================ */
.webhook-url {
    background: var(--table-header-bg);
    border: 1px dashed var(--input-border);
    padding: 10px 14px;
    border-radius: 8px;
    font-family: monospace;
    font-size: 12px;
    word-break: break-all;
    color: var(--text-primary);
}

/* ============================================
   EMPTY STATE
   ============================================ */
.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: var(--text-muted);
}

.empty-state .empty-icon {
    font-size: 56px;
    margin-bottom: 16px;
    opacity: 0.3;
    display: block;
}

/* ============================================
   USAGE BAR
   ============================================ */
.usage-bar {
    background: var(--input-border);
    border-radius: 10px;
    height: 8px;
    overflow: hidden;
}

.usage-bar-fill {
    height: 100%;
    background: var(--gradient);
    transition: width 0.3s;
}

/* ============================================
   PAGE HEADER
   ============================================ */
.page-header {
    margin-bottom: 28px;
}

.page-header h2 {
    font-size: 24px;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 4px;
}

.page-header p {
    color: var(--text-secondary);
    margin: 0;
}

/* ============================================
   AUTH PAGES
   ============================================ */
.auth-page {
    min-height: 100vh;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.auth-card {
    background: var(--card-bg);
    border-radius: 16px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.2);
    padding: 40px;
    max-width: 440px;
    width: 100%;
}

.auth-logo {
    font-size: 28px;
    font-weight: 700;
    color: var(--primary);
    text-align: center;
    margin-bottom: 8px;
}

/* ============================================
   DROPDOWN FIX FOR DARK MODE
   ============================================ */
[data-theme="dark"] .dropdown-menu {
    background: #1e2235;
    border-color: #2d3148;
}

[data-theme="dark"] .dropdown-item {
    color: var(--text-primary);
}

[data-theme="dark"] .dropdown-item:hover {
    background: rgba(255,255,255,0.06);
    color: #fff;
}

[data-theme="dark"] .table-light,
[data-theme="dark"] .table thead {
    background: var(--table-header-bg) !important;
    color: var(--text-secondary);
}

[data-theme="dark"] .card {
    background: var(--card-bg);
    border-color: var(--card-border);
}

[data-theme="dark"] .modal-content {
    background: var(--card-bg);
    border-color: var(--card-border);
}

[data-theme="dark"] .alert {
    background: var(--card-bg);
    border-color: var(--card-border);
    color: var(--text-primary);
}

[data-theme="dark"] pre {
    background: #0f1117;
    color: #e2e8f0;
}

/* ============================================
   MISC
   ============================================ */
.code-block {
    background: #1e2235;
    color: #e2e8f0;
    padding: 16px;
    border-radius: 8px;
    font-family: monospace;
    font-size: 13px;
    overflow-x: auto;
}

pre {
    background: var(--table-header-bg);
    color: var(--text-primary);
    padding: 12px;
    border-radius: 6px;
    font-size: 12px;
    max-height: 300px;
    overflow: auto;
}

hr {
    border-color: var(--card-border);
    opacity: 1;
}

.text-muted {
    color: var(--text-secondary) !important;
}

a {
    color: var(--primary);
}

/* Range slider */
.range-slider {
    width: 100%;
    height: 6px;
    border-radius: 3px;
    background: var(--input-border);
    outline: none;
    -webkit-appearance: none;
    cursor: pointer;
}

.range-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: var(--gradient);
    cursor: pointer;
    box-shadow: 0 2px 6px rgba(102,126,234,0.4);
}
