/**
 * ============================================================================
 * AFFILIATE / VENDOR PLATFORM – OPTIMIZED MOBILE-FIRST STYLESHEET (v4)
 * ============================================================================
 * 
 * IMPROVEMENTS IN THIS VERSION:
 * ✓ Mobile-first approach (base styles for mobile, then scale up)
 * ✓ Removed all !important declarations
 * ✓ Eliminated negative margin hacks
 * ✓ Consolidated media queries for better performance
 * ✓ Touch-friendly tap targets (minimum 44px)
 * ✓ Proper safe area insets for notched devices
 * ✓ Better table handling without horizontal scroll
 * ✓ Improved form field sizing
 * ✓ Consistent spacing scale
 * ✓ Better sidebar overlay with body scroll prevention
 * ✓ Smoother animations and transitions
 */

:root {
    /* Colors */
    --primary-color: #4f46e5;
    --primary-hover: #4338ca;
    --secondary-color: #10b981;
    --danger-color: #ef4444;
    --warning-color: #f59e0b;
    --info-color: #3b82f6;
    --dark-bg: #1f2937;
    --light-bg: #f9fafb;
    --border-color: #e5e7eb;
    --text-primary: #111827;
    --text-secondary: #6b7280;
    
    /* Layout (Mobile First) */
    --sidebar-width: 260px;
    --header-height: 60px;
    --content-padding: 16px;
    
    /* Spacing Scale */
    --space-xs: 4px;
    --space-sm: 8px;
    --space-md: 16px;
    --space-lg: 24px;
    --space-xl: 32px;
    
    /* Radius */
    --radius-sm: 6px;
    --radius-md: 8px;
    --radius-lg: 12px;
    
    /* Shadows */
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.1);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.15);
    --shadow-lg: 0 8px 20px rgba(0,0,0,0.15);
    
    /* Safe Area Insets for notched devices */
    --safe-area-top: env(safe-area-inset-top, 0px);
    --safe-area-bottom: env(safe-area-inset-bottom, 0px);
    --safe-area-left: env(safe-area-inset-left, 0px);
    --safe-area-right: env(safe-area-inset-right, 0px);
}

/* =========================
   RESET & BASE STYLES
   ========================= */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--light-bg);
    color: var(--text-primary);
    line-height: 1.6;
    overflow-x: auto;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* Prevent body scroll when sidebar is open on mobile */
body.sidebar-open {
    overflow: hidden;
    position: fixed;
    width: 100%;
}

a {
    text-decoration: none;
    color: inherit;
}

img {
    max-width: 100%;
    height: auto;
    display: block;
}

button {
    font-family: inherit;
}

/* =========================
   LAYOUT STRUCTURE
   ========================= */
.dashboard-wrapper {
    display: flex;
    min-height: 100vh;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 var(--content-padding);
}

/* =========================
   MOBILE MENU TOGGLE
   ========================= */
.mobile-menu-toggle {
    position: fixed;
    top: calc(var(--safe-area-top) + 12px);
    left: calc(var(--safe-area-left) + 12px);
    z-index: 1001;
    width: 44px;
    height: 44px;
    border-radius: var(--radius-md);
    background: var(--primary-color);
    color: #fff;
    border: none;
    font-size: 20px;
    cursor: pointer;
    box-shadow: var(--shadow-md);
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    -webkit-tap-highlight-color: transparent;
}

.mobile-menu-toggle:hover {
    background: var(--primary-hover);
    transform: scale(1.05);
}

.mobile-menu-toggle:active {
    transform: scale(0.95);
}

/* =========================
   SIDEBAR NAVIGATION
   ========================= */
.sidebar {
    width: var(--sidebar-width);
    background: #fff;
    border-right: 1px solid var(--border-color);
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    overflow-y: auto;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 1000;
    transform: translateX(-100%);
    box-shadow: var(--shadow-lg);
}

.sidebar.active {
    transform: translateX(0);
}

.sidebar-logo {
    padding: var(--space-lg);
    border-bottom: 1px solid var(--border-color);
    font-size: 18px;
    font-weight: 700;
    color: var(--primary-color);
    position: sticky;
    top: 0;
    background: #fff;
    z-index: 10;
}

.sidebar-nav {
    padding: var(--space-lg) 0;
}

.nav-item {
    padding: 14px var(--space-lg);
    display: flex;
    align-items: center;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.2s ease;
    border-left: 3px solid transparent;
    min-height: 44px; /* Touch-friendly */
    -webkit-tap-highlight-color: transparent;
}

.nav-item:hover,
.nav-item.active {
    background: var(--light-bg);
    color: var(--primary-color);
    border-left-color: var(--primary-color);
}

.nav-icon {
    margin-right: 12px;
    font-size: 20px;
    width: 24px;
    text-align: center;
}

/* Sidebar overlay for mobile */
.sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 999;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.sidebar-overlay.active {
    display: block;
    opacity: 1;
}

/* =========================
   MAIN CONTENT AREA
   ========================= */
.main-content {
    flex: 1;
    min-height: 100vh;
    width: 100%;
}

/* =========================
   HEADER
   ========================= */
.header {
    background: #fff;
    height: var(--header-height);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 var(--content-padding) 0 calc(var(--safe-area-left) + 60px);
    position: sticky;
    top: 0;
    z-index: 998;
}

.greeting {
    font-size: 18px;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.user-menu {
    display: flex;
    align-items: center;
    gap: var(--space-md);
}

.user-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--primary-color);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    cursor: pointer;
    font-size: 16px;
    flex-shrink: 0;
    -webkit-tap-highlight-color: transparent;
}

/* =========================
   DASHBOARD CONTENT
   ========================= */
.dashboard-content {
    padding: var(--space-lg) var(--content-padding);
    padding-bottom: calc(var(--space-xl) + var(--safe-area-bottom));
}

/* =========================
   STATS GRID & CARDS
   ========================= */
.stats-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: var(--space-md);
    margin-bottom: var(--space-lg);
}

.stat-card {
    background: #fff;
    padding: var(--space-lg);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.stat-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}

.stat-label {
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: var(--space-sm);
    line-height: 1.3;
}

.stat-value {
    font-size: 28px;
    font-weight: 700;
    color: var(--text-primary);
    line-height: 1.2;
}

.stat-change {
    font-size: 13px;
    margin-top: var(--space-sm);
}

.stat-change.positive {
    color: var(--secondary-color);
}

.stat-change.negative {
    color: var(--danger-color);
}

/* =========================
   GENERAL CARD COMPONENT
   ========================= */
.card {
    background: #fff;
    border-radius: var(--radius-lg);
    padding: var(--space-lg);
    box-shadow: var(--shadow-sm);
    margin-bottom: var(--space-lg);
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: var(--space-lg);
    gap: var(--space-md);
    flex-wrap: wrap;
}

.card-title {
    font-size: 18px;
    font-weight: 600;
    color: var(--text-primary);
}

/* =========================
   TABLES
   ========================= */
.table-responsive {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0 calc(var(--content-padding) * -1);
    padding: 0 var(--content-padding);
}

table {
    width: 100%;
    border-collapse: collapse;
    min-width: 500px;
}

thead {
    background: var(--light-bg);
}

th, td {
    padding: 12px 10px;
    text-align: left;
    border-bottom: 1px solid var(--border-color);
}

th {
    font-weight: 600;
    color: var(--text-primary);
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

td {
    color: var(--text-secondary);
    font-size: 14px;
}

tr:hover {
    background: var(--light-bg);
}

/* =========================
   BUTTONS
   ========================= */
.btn {
    padding: 12px 20px;
    border-radius: var(--radius-md);
    border: none;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    font-size: 15px;
    min-height: 44px; /* Touch-friendly */
    -webkit-tap-highlight-color: transparent;
    white-space: nowrap;
}

.btn-primary {
    background: var(--primary-color);
    color: #fff;
}

.btn-primary:hover {
    background: var(--primary-hover);
}

.btn-primary:active {
    transform: scale(0.98);
}

.btn-secondary {
    background: var(--secondary-color);
    color: #fff;
}

.btn-secondary:hover {
    background: #059669;
}

.btn-danger {
    background: var(--danger-color);
    color: #fff;
}

.btn-danger:hover {
    background: #dc2626;
}

.btn-outline {
    background: transparent;
    border: 2px solid var(--primary-color);
    color: var(--primary-color);
}

.btn-outline:hover {
    background: var(--primary-color);
    color: #fff;
}

.btn-sm {
    padding: 8px 16px;
    font-size: 14px;
    min-height: 36px;
}

.btn-block {
    width: 100%;
    display: flex;
}

/* =========================
   FORMS
   ========================= */
.form-group {
    margin-bottom: var(--space-lg);
}

.form-label {
    display: block;
    margin-bottom: var(--space-sm);
    font-weight: 500;
    color: var(--text-primary);
    font-size: 14px;
}

.form-control {
    width: 100%;
    padding: 14px 16px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    font-size: 16px; /* Prevents zoom on iOS */
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
    background: #fff;
    min-height: 48px; /* Touch-friendly */
}

.form-control:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

select.form-control {
    cursor: pointer;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%236b7280' d='M6 9L1 4h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 40px;
}

textarea.form-control {
    resize: vertical;
    min-height: 120px;
    line-height: 1.5;
}

/* =========================
   BADGES
   ========================= */
.badge {
    padding: 6px 12px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
    display: inline-block;
    line-height: 1;
}

.badge-success,
.badge-successful {
    background: #d1fae5;
    color: #065f46;
}

.badge-danger {
    background: #fee2e2;
    color: #991b1b;
}

.badge-warning,
.badge-pending {
    background: #fef3c7;
    color: #92400e;
}

.badge-info {
    background: #dbeafe;
    color: #1e40af;
}

/* =========================
   ALERTS
   ========================= */
.alert {
    padding: var(--space-md) var(--space-lg);
    border-radius: var(--radius-md);
    margin-bottom: var(--space-lg);
    display: flex;
    align-items: flex-start;
    gap: var(--space-md);
    transition: opacity 0.3s ease;
    line-height: 1.5;
}

.alert-success {
    background: #d1fae5;
    color: #065f46;
    border-left: 4px solid var(--secondary-color);
}

.alert-error {
    background: #fee2e2;
    color: #991b1b;
    border-left: 4px solid var(--danger-color);
}

.alert-warning {
    background: #fef3c7;
    color: #92400e;
    border-left: 4px solid var(--warning-color);
}

.alert-info {
    background: #dbeafe;
    color: #1e40af;
    border-left: 4px solid var(--info-color);
}

/* =========================
   MODALS
   ========================= */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.5);
    z-index: 2000;
    align-items: center;
    justify-content: center;
    padding: var(--space-lg);
}

.modal.active {
    display: flex;
}

.modal-content {
    background: #fff;
    border-radius: var(--radius-lg);
    padding: var(--space-xl);
    max-width: 600px;
    width: 100%;
    max-height: 85vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: var(--space-lg);
}

.modal-title {
    font-size: 20px;
    font-weight: 600;
}

.modal-close {
    background: none;
    border: none;
    font-size: 24px;
    cursor: pointer;
    color: var(--text-secondary);
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm);
    transition: background 0.2s ease;
    -webkit-tap-highlight-color: transparent;
}

.modal-close:hover {
    background: var(--light-bg);
}

/* =========================
   LEADERBOARD
   ========================= */
.leaderboard-item {
    display: flex;
    align-items: center;
    padding: var(--space-md);
    border-bottom: 1px solid var(--border-color);
    gap: var(--space-md);
}

.leaderboard-rank {
    font-size: 20px;
    font-weight: 700;
    min-width: 40px;
    color: var(--primary-color);
    text-align: center;
}

.leaderboard-info {
    flex: 1;
    min-width: 0; /* Prevents flex overflow */
}

.leaderboard-name {
    font-weight: 600;
    margin-bottom: var(--space-xs);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.leaderboard-stats {
    font-size: 13px;
    color: var(--text-secondary);
}

.leaderboard-value {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
}

/* =========================
   PRODUCT GRID
   ========================= */
.product-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: var(--space-lg);
}

.product-card {
    background: #fff;
    border-radius: var(--radius-lg);
    padding: var(--space-lg);
    box-shadow: var(--shadow-sm);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.product-card:hover {
    transform: translateY(-4px);
    box-shadow: var(--shadow-lg);
}

.product-title {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: var(--space-sm);
    color: var(--text-primary);
}

.product-vendor {
    font-size: 14px;
    color: var(--text-secondary);
    margin-bottom: var(--space-md);
}

.product-price {
    font-size: 24px;
    font-weight: 700;
    color: var(--primary-color);
    margin-bottom: var(--space-sm);
}

.product-commission {
    font-size: 14px;
    color: var(--secondary-color);
    margin-bottom: var(--space-md);
}

/* =========================
   COPY BUTTON & SPINNER
   ========================= */
.copy-button {
    position: relative;
    display: inline-block;
    width: 100%;
}

.copy-input {
    padding-right: 90px;
}

.copy-btn {
    position: absolute;
    right: 4px;
    top: 50%;
    transform: translateY(-50%);
}

.spinner {
    border: 3px solid var(--light-bg);
    border-top: 3px solid var(--primary-color);
    border-radius: 50%;
    width: 40px;
    height: 40px;
    animation: spin 1s linear infinite;
    margin: var(--space-lg) auto;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* =========================
   UTILITY CLASSES
   ========================= */
.text-center { text-align: center; }
.text-right { text-align: right; }
.mt-10 { margin-top: var(--space-sm); }
.mt-20 { margin-top: var(--space-lg); }
.mb-10 { margin-bottom: var(--space-sm); }
.mb-20 { margin-bottom: var(--space-lg); }
.flex { display: flex; }
.flex-between { justify-content: space-between; }
.flex-center { align-items: center; }
.gap-10 { gap: var(--space-sm); }
.gap-20 { gap: var(--space-lg); }
.hidden { display: none; }

/* =========================
   TABLET & DESKTOP BREAKPOINTS
   ========================= */

/* Small tablets and larger phones (576px+) */
@media (min-width: 576px) {
    :root {
        --content-padding: 20px;
    }
    
    .stats-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: var(--space-lg);
    }
    
    .product-grid {
        grid-template-columns: repeat(2, 1fr);
    }
    
    .stat-value {
        font-size: 32px;
    }
    
    .greeting {
        font-size: 20px;
    }
}

/* Tablets (768px+) */
@media (min-width: 768px) {
    :root {
        --header-height: 70px;
        --content-padding: 30px;
    }
    
    .mobile-menu-toggle {
        display: none;
    }
    
    .sidebar {
        transform: translateX(0);
        box-shadow: none;
    }
    
    .sidebar-overlay {
        display: none;
    }
    
    .main-content {
        margin-left: var(--sidebar-width);
    }
    
    .header {
        padding: 0 var(--space-xl);
    }
    
    .greeting {
        font-size: 24px;
    }
    
    /* Use auto-fit like original for flexible card distribution */
    .stats-grid {
        grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
        gap: 20px;
    }
    
    .stat-card {
        padding: 25px;
    }
    
    .stat-value {
        font-size: 32px;
    }
    
    .stat-label {
        font-size: 14px;
    }
    
    .card {
        padding: 25px;
    }
    
    .card-header {
        flex-wrap: nowrap;
        align-items: center;
        margin-bottom: 20px;
    }
    
    .card-header .btn {
        width: auto;
        margin-top: 0;
    }
    
    .card-title {
        font-size: 18px;
    }
    
    .product-grid {
        grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    }
    
    .table-responsive {
        margin: 0;
        padding: 0;
    }
    
    table {
        min-width: 600px;
    }
    
    th, td {
        padding: 12px 15px;
    }
    
    th {
        font-size: 14px;
    }
    
    td {
        font-size: 15px;
    }
}

/* Large tablets and desktops (992px+) */
@media (min-width: 992px) {
    .leaderboard-rank {
        font-size: 24px;
        min-width: 50px;
    }
    
    .leaderboard-value {
        font-size: 18px;
    }
}

/* Large desktops (1200px+) */
@media (min-width: 1200px) {
    :root {
        --sidebar-width: 280px;
    }
}

/* =========================
   PRINT STYLES
   ========================= */
@media print {
    .sidebar,
    .mobile-menu-toggle,
    .sidebar-overlay,
    .btn,
    .header {
        display: none;
    }
    
    .main-content {
        margin-left: 0;
    }
    
    .card,
    .stat-card {
        box-shadow: none;
        border: 1px solid var(--border-color);
    }
}

/* Profile Photo Styles in Header */
.user-avatar {
    width: 45px;
    height: 45px;
    border-radius: 50%; /* ✅ CIRCULAR */
    background: var(--primary-color);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 18px;
    text-decoration: none;
    overflow: hidden;
    border: 2px solid #e5e7eb;
    transition: all 0.2s;
    flex-shrink: 0; /* ✅ Prevents squashing */
}

.user-avatar:hover {
    transform: scale(1.05);
    border-color: var(--primary-color);
}

.user-avatar-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.user-avatar-text {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
}