<?php
session_start();
require_once '../connection.php';

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

// Get user ID
$user_id = $_SESSION['user_id'];

// Fetch user data
$stmt = $conn->prepare("SELECT * FROM users WHERE id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    session_destroy();
    header("Location: login.php?error=User account not found");
    exit();
}

$user = $result->fetch_assoc();
$stmt->close();

// Check account status
if (isset($user['email_verified']) && $user['email_verified'] == 0) {
    header("Location: verify.php?message=Please verify your email first");
    exit();
}

if (isset($user['account_status']) && $user['account_status'] != 'active') {
    $error_message = "Your account is " . $user['account_status'];
    header("Location: login.php?error=" . urlencode($error_message));
    exit();
}

// Fetch user investments data
$active_investments = 0;
$total_invested_amount = 0.00;
$pending_investments = 0;
$completed_investments = 0;

$check_table = $conn->query("SHOW TABLES LIKE 'user_investments'");
if ($check_table && $check_table->num_rows > 0) {
    // Active investments
    $stmt = $conn->prepare("SELECT COUNT(*) as count, COALESCE(SUM(amount), 0) as total FROM user_investments WHERE user_id = ? AND status IN ('active', 'running')");
    if ($stmt) {
        $stmt->bind_param("i", $user_id);
        $stmt->execute();
        $res = $stmt->get_result();
        if ($row = $res->fetch_assoc()) {
            $active_investments = $row['count'];
            $total_invested_amount = $row['total'];
        }
        $stmt->close();
    }
    
    // Pending investments
    $stmt = $conn->prepare("SELECT COUNT(*) as count FROM user_investments WHERE user_id = ? AND status = 'pending'");
    if ($stmt) {
        $stmt->bind_param("i", $user_id);
        $stmt->execute();
        $res = $stmt->get_result();
        if ($row = $res->fetch_assoc()) {
            $pending_investments = $row['count'];
        }
        $stmt->close();
    }
    
    // Completed investments
    $stmt = $conn->prepare("SELECT COUNT(*) as count FROM user_investments WHERE user_id = ? AND status = 'completed'");
    if ($stmt) {
        $stmt->bind_param("i", $user_id);
        $stmt->execute();
        $res = $stmt->get_result();
        if ($row = $res->fetch_assoc()) {
            $completed_investments = $row['count'];
        }
        $stmt->close();
    }
}

// Fetch site settings
$site_settings = [];
$settings_stmt = $conn->prepare("SELECT setting_key, setting_value FROM site_settings WHERE setting_group = 'general'");
if ($settings_stmt) {
    $settings_stmt->execute();
    $settings_result = $settings_stmt->get_result();
    while ($row = $settings_result->fetch_assoc()) {
        $site_settings[$row['setting_key']] = $row['setting_value'];
    }
    $settings_stmt->close();
}

// Calculate user metrics
$account_balance = isset($user['account_balance']) ? (float)$user['account_balance'] : 0.00;
$total_earnings = isset($user['total_earnings']) ? (float)$user['total_earnings'] : 0.00;
$total_withdrawals = isset($user['total_withdrawals']) ? (float)$user['total_withdrawals'] : 0.00;
$total_deposits = isset($user['total_deposits']) ? (float)$user['total_deposits'] : 0.00;
$referral_earnings = isset($user['referral_earnings']) ? (float)$user['referral_earnings'] : 0.00;
$total_referrals = isset($user['total_referrals']) ? (int)$user['total_referrals'] : 0;
$investment_balance = isset($user['investment_balance']) ? (float)$user['investment_balance'] : 0.00;
$total_invested = isset($user['total_invested']) ? (float)$user['total_invested'] : 0.00;

// Calculate additional metrics
$available_balance = $account_balance;
$total_assets = $account_balance + $investment_balance;
$net_profit = $total_earnings - $total_invested;
$withdrawal_limit = $account_balance;

// Calculate ROI
$roi_percentage = 0;
if ($total_invested > 0) {
    $roi_percentage = ($net_profit / $total_invested) * 100;
}

// Format dates
$last_login_date = isset($user['last_login']) && $user['last_login'] ? date('M d, Y h:i A', strtotime($user['last_login'])) : 'Never';
$member_since = isset($user['created_at']) ? date('M d, Y', strtotime($user['created_at'])) : 'N/A';

// Account tier
$account_tier = isset($user['account_tier']) ? $user['account_tier'] : 'basic';
$tier_names = [
    'basic' => 'Basic',
    'silver' => 'Silver',
    'gold' => 'Gold',
    'platinum' => 'Platinum',
    'vip' => 'VIP'
];
$tier_name = isset($tier_names[$account_tier]) ? $tier_names[$account_tier] : 'Basic';

// Mock market data with variations
$btc_price = 92748 + rand(-500, 500);
$eth_price = 3267 + rand(-50, 50);
$btc_change = rand(-200, 200) / 100;
$eth_change = rand(-20, 20) / 10;
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard - <?php echo htmlspecialchars($site_settings['site_name'] ?? 'Trading Platform'); ?></title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    
      <style>
        :root {
            --sidebar-width: 16rem;
            --primary-gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            --success-gradient: linear-gradient(135deg, #10b981 0%, #059669 100%);
        }
        
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Inter', sans-serif;
            background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
            min-height: 100vh;
            color: #e2e8f0;
        }
        
        /* Sidebar Styles */
        .sidebar {
            background: linear-gradient(180deg, #1e293b 0%, #0f172a 100%);
            width: var(--sidebar-width);
            height: 100vh;
            position: fixed;
            left: 0;
            top: 0;
            z-index: 1000;
            transform: translateX(-100%);
            transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            overflow-y: auto;
            box-shadow: 4px 0 20px rgba(0, 0, 0, 0.3);
            border-right: 1px solid rgba(255, 255, 255, 0.05);
        }
        
        .sidebar.active {
            transform: translateX(0);
        }
        
        @media (min-width: 1024px) {
            .sidebar {
                transform: translateX(0) !important;
            }
        }
        
        .sidebar-overlay {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0, 0, 0, 0.7);
            z-index: 999;
            backdrop-filter: blur(4px);
        }
        
        .sidebar-overlay.active {
            display: block;
        }
        
        /* Main Content */
        .main-content {
            width: 100%;
            min-height: 100vh;
            transition: margin-left 0.3s ease;
        }
        
        @media (min-width: 1024px) {
            .main-content {
                margin-left: var(--sidebar-width);
                width: calc(100% - var(--sidebar-width));
            }
        }
        
        /* Mobile Menu Button */
        .mobile-menu-btn {
            position: fixed;
            top: 1.5rem;
            left: 1.5rem;
            z-index: 1100;
            width: 3rem;
            height: 3rem;
            background: rgba(30, 41, 59, 0.95);
            border-radius: 0.75rem;
            display: flex;
            align-items: center;
            justify-content: center;
            border: 1px solid rgba(255, 255, 255, 0.1);
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
            transition: all 0.3s ease;
            backdrop-filter: blur(10px);
        }
        
        @media (min-width: 1024px) {
            .mobile-menu-btn {
                display: none;
            }
        }
        
        /* Navigation Items */
        .nav-item {
            display: flex;
            align-items: center;
            padding: 0.875rem 1rem;
            border-radius: 0.75rem;
            margin: 0.25rem 0.75rem;
            color: #94a3b8;
            text-decoration: none;
            transition: all 0.2s ease;
            position: relative;
            overflow: hidden;
        }
        
        .nav-item::before {
            content: '';
            position: absolute;
            left: 0;
            top: 0;
            height: 100%;
            width: 3px;
            background: var(--primary-gradient);
            transform: translateX(-100%);
            transition: transform 0.3s ease;
        }
        
        .nav-item:hover {
            background: rgba(255, 255, 255, 0.05);
            color: #e2e8f0;
            transform: translateX(4px);
        }
        
        .nav-item:hover::before {
            transform: translateX(0);
        }
        
        .nav-item.active {
            background: rgba(102, 126, 234, 0.1);
            color: #e2e8f0;
            box-shadow: 0 4px 12px rgba(102, 126, 234, 0.15);
        }
        
        .nav-item.active::before {
            transform: translateX(0);
        }
        
        .nav-item.active i {
            color: #667eea;
            transform: scale(1.1);
        }
        
        .nav-item i {
            width: 1.5rem;
            margin-right: 0.875rem;
            transition: all 0.2s ease;
            font-size: 1.1rem;
        }
        
        /* Card Styles */
        .glass-card {
            background: rgba(30, 41, 59, 0.7);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.1);
            border-radius: 1.25rem;
            transition: all 0.3s ease;
        }
        
        .glass-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
            border-color: rgba(102, 126, 234, 0.3);
        }
        
        .gradient-card {
            background: linear-gradient(135deg, rgba(102, 126, 234, 0.15) 0%, rgba(118, 75, 162, 0.15) 100%);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(102, 126, 234, 0.2);
            border-radius: 1.25rem;
        }
        
        /* Form Styles */
        .form-input {
            background: rgba(255, 255, 255, 0.05);
            border: 1px solid rgba(255, 255, 255, 0.1);
            border-radius: 0.75rem;
            padding: 0.875rem 1rem;
            color: #e2e8f0;
            font-size: 0.95rem;
            transition: all 0.2s ease;
            width: 100%;
        }
        
        .form-input:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
            background: rgba(255, 255, 255, 0.08);
        }
        
        .form-input::placeholder {
            color: #94a3b8;
        }
        
        .form-label {
            display: block;
            color: #cbd5e0;
            font-size: 0.875rem;
            font-weight: 500;
            margin-bottom: 0.5rem;
        }
        
        .form-group {
            margin-bottom: 1.5rem;
        }
        
        .form-select {
            background: rgba(255, 255, 255, 0.05);
            border: 1px solid rgba(255, 255, 255, 0.1);
            border-radius: 0.75rem;
            padding: 0.875rem 1rem;
            color: #e2e8f0;
            font-size: 0.95rem;
            transition: all 0.2s ease;
            width: 100%;
            appearance: none;
            background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3E%3Cpath stroke='%2394a3b8' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3E%3C/svg%3E");
            background-position: right 0.75rem center;
            background-repeat: no-repeat;
            background-size: 1.5em 1.5em;
        }
        
        .form-select:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
            background-color: rgba(255, 255, 255, 0.08);
        }
        
        .btn-primary {
            background: var(--primary-gradient);
            color: white;
            border: none;
            border-radius: 0.75rem;
            padding: 0.875rem 1.75rem;
            font-weight: 600;
            font-size: 0.95rem;
            cursor: pointer;
            transition: all 0.2s ease;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 0.5rem;
        }
        
        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 25px -5px rgba(102, 126, 234, 0.4);
        }
        
        .btn-primary:active {
            transform: translateY(0);
        }
        
        .btn-success {
            background: var(--success-gradient);
            color: white;
            border: none;
            border-radius: 0.75rem;
            padding: 0.875rem 1.75rem;
            font-weight: 600;
            font-size: 0.95rem;
            cursor: pointer;
            transition: all 0.2s ease;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 0.5rem;
        }
        
        .btn-success:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 25px -5px rgba(16, 185, 129, 0.4);
        }
        
        .btn-secondary {
            background: rgba(255, 255, 255, 0.05);
            color: #cbd5e0;
            border: 1px solid rgba(255, 255, 255, 0.1);
            border-radius: 0.75rem;
            padding: 0.875rem 1.75rem;
            font-weight: 500;
            font-size: 0.95rem;
            cursor: pointer;
            transition: all 0.2s ease;
        }
        
        .btn-secondary:hover {
            background: rgba(255, 255, 255, 0.1);
            color: #e2e8f0;
        }
        
        /* Alert Messages */
        .alert-success {
            background: rgba(72, 187, 120, 0.1);
            border: 1px solid rgba(72, 187, 120, 0.2);
            color: #48bb78;
            padding: 1rem 1.5rem;
            border-radius: 0.75rem;
            margin-bottom: 1.5rem;
            display: flex;
            align-items: center;
            gap: 0.75rem;
            animation: slideIn 0.3s ease-out;
        }
        
        .alert-error {
            background: rgba(245, 101, 101, 0.1);
            border: 1px solid rgba(245, 101, 101, 0.2);
            color: #f56565;
            padding: 1rem 1.5rem;
            border-radius: 0.75rem;
            margin-bottom: 1.5rem;
            display: flex;
            align-items: center;
            gap: 0.75rem;
            animation: slideIn 0.3s ease-out;
        }
        
        .alert-info {
            background: rgba(59, 130, 246, 0.1);
            border: 1px solid rgba(59, 130, 246, 0.2);
            color: #3b82f6;
            padding: 1rem 1.5rem;
            border-radius: 0.75rem;
            margin-bottom: 1.5rem;
            display: flex;
            align-items: center;
            gap: 0.75rem;
        }
        
        @keyframes slideIn {
            from {
                opacity: 0;
                transform: translateY(-10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        /* Payment Method Cards */
        .payment-method-card {
            background: rgba(255, 255, 255, 0.03);
            border: 1px solid rgba(255, 255, 255, 0.1);
            border-radius: 1rem;
            padding: 1.5rem;
            cursor: pointer;
            transition: all 0.2s ease;
        }
        
        .payment-method-card:hover {
            background: rgba(255, 255, 255, 0.05);
            border-color: #667eea;
            transform: translateY(-2px);
        }
        
        .payment-method-card.selected {
            background: rgba(102, 126, 234, 0.1);
            border-color: #667eea;
            box-shadow: 0 4px 12px rgba(102, 126, 234, 0.15);
        }
        
        /* Crypto Network Badge */
        .network-badge {
            display: inline-flex;
            align-items: center;
            padding: 0.25rem 0.75rem;
            border-radius: 9999px;
            font-size: 0.75rem;
            font-weight: 500;
            margin-top: 0.5rem;
        }
        
        .network-btc {
            background: rgba(247, 147, 26, 0.1);
            color: #f7931a;
            border: 1px solid rgba(247, 147, 26, 0.2);
        }
        
        .network-eth {
            background: rgba(118, 92, 222, 0.1);
            color: #767cde;
            border: 1px solid rgba(118, 92, 222, 0.2);
        }
        
        .network-usdt {
            background: rgba(38, 161, 123, 0.1);
            color: #26a17b;
            border: 1px solid rgba(38, 161, 123, 0.2);
        }
        
        /* Wallet Address Display */
        .wallet-address {
            background: rgba(0, 0, 0, 0.2);
            border: 1px solid rgba(255, 255, 255, 0.1);
            border-radius: 0.75rem;
            padding: 0.75rem 1rem;
            font-family: monospace;
            font-size: 0.9rem;
            word-break: break-all;
            position: relative;
        }
        
        .copy-btn {
            position: absolute;
            right: 0.5rem;
            top: 50%;
            transform: translateY(-50%);
            background: rgba(102, 126, 234, 0.2);
            color: #667eea;
            border: none;
            border-radius: 0.5rem;
            padding: 0.5rem 0.75rem;
            cursor: pointer;
            transition: all 0.2s ease;
        }
        
        .copy-btn:hover {
            background: rgba(102, 126, 234, 0.3);
        }
        
        /* Progress Bar */
        .progress-bar {
            height: 6px;
            background: rgba(255, 255, 255, 0.1);
            border-radius: 3px;
            overflow: hidden;
            margin-top: 0.5rem;
        }
        
        .progress-fill {
            height: 100%;
            background: linear-gradient(90deg, #667eea, #764ba2);
            border-radius: 3px;
        }
        
        /* Status Badges */
        .status-badge {
            display: inline-flex;
            align-items: center;
            padding: 0.25rem 0.75rem;
            border-radius: 9999px;
            font-size: 0.75rem;
            font-weight: 500;
        }
        
        .status-pending {
            background: rgba(245, 158, 11, 0.1);
            color: #f59e0b;
            border: 1px solid rgba(245, 158, 11, 0.2);
        }
        
        .status-completed {
            background: rgba(72, 187, 120, 0.1);
            color: #48bb78;
            border: 1px solid rgba(72, 187, 120, 0.2);
        }
        
        .status-rejected {
            background: rgba(245, 101, 101, 0.1);
            color: #f56565;
            border: 1px solid rgba(245, 101, 101, 0.2);
        }
        
        /* Close button for mobile sidebar */
        .sidebar-close-btn {
            display: none;
        }
        
        @media (max-width: 1023px) {
            .sidebar-close-btn {
                display: block;
                position: absolute;
                top: 1rem;
                right: 1rem;
                z-index: 1001;
                width: 2.5rem;
                height: 2.5rem;
                background: rgba(255, 255, 255, 0.1);
                border-radius: 50%;
                display: flex;
                align-items: center;
                justify-content: center;
                border: none;
                color: white;
                cursor: pointer;
                transition: all 0.2s ease;
                backdrop-filter: blur(10px);
            }
            
            .sidebar-close-btn:hover {
                background: rgba(255, 255, 255, 0.2);
                transform: rotate(90deg);
            }
        }
        
        /* Scrollbar */
        ::-webkit-scrollbar {
            width: 6px;
            height: 6px;
        }
        
        ::-webkit-scrollbar-track {
            background: rgba(255, 255, 255, 0.05);
        }
        
        ::-webkit-scrollbar-thumb {
            background: var(--primary-gradient);
            border-radius: 3px;
        }
        
        /* Animations */
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        
        .animate-fade-in {
            animation: fadeIn 0.5s ease-out;
        }
        
        /* ========================================
   Notification System Styles
   ======================================== */

/* Notification Container */
.notification-container {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 9999;
    max-width: 350px;
    width: 100%;
    pointer-events: none;
}

/* Individual Notification */
.notification {
    background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
    border-left: 4px solid;
    border-radius: 12px;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.3), 0 5px 10px rgba(0, 0, 0, 0.2);
    margin-bottom: 15px;
    padding: 16px 20px;
    transform: translateX(120%);
    animation: slideIn 0.3s ease forwards;
    display: flex;
    align-items: flex-start;
    gap: 15px;
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    pointer-events: auto;
    position: relative;
    overflow: hidden;
}

.notification::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, rgba(102, 126, 234, 0.1), rgba(118, 75, 162, 0.1));
    pointer-events: none;
}

.notification.removing {
    animation: slideOut 0.3s ease forwards;
}

/* Notification Types */
.notification.success {
    border-left-color: #10b981;
}

.notification.success .notification-icon {
    background: rgba(16, 185, 129, 0.2);
    color: #10b981;
}

.notification.error {
    border-left-color: #ef4444;
}

.notification.error .notification-icon {
    background: rgba(239, 68, 68, 0.2);
    color: #ef4444;
}

.notification.warning {
    border-left-color: #f59e0b;
}

.notification.warning .notification-icon {
    background: rgba(245, 158, 11, 0.2);
    color: #f59e0b;
}

.notification.info {
    border-left-color: #3b82f6;
}

.notification.info .notification-icon {
    background: rgba(59, 130, 246, 0.2);
    color: #3b82f6;
}

/* Notification Icon */
.notification-icon {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    flex-shrink: 0;
    backdrop-filter: blur(5px);
}

/* Notification Content */
.notification-content {
    flex: 1;
}

.notification-title {
    color: white;
    font-weight: 600;
    font-size: 16px;
    margin-bottom: 4px;
    font-family: 'Inter', sans-serif;
}

.notification-message {
    color: #94a3b8;
    font-size: 14px;
    line-height: 1.5;
    margin: 0;
    font-family: 'Inter', sans-serif;
}

/* Close Button */
.notification-close {
    background: none;
    border: none;
    color: #64748b;
    cursor: pointer;
    font-size: 14px;
    padding: 5px;
    transition: all 0.2s ease;
    border-radius: 6px;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.notification-close:hover {
    background: rgba(255, 255, 255, 0.1);
    color: #e2e8f0;
    transform: rotate(90deg);
}

/* Progress Bar */
.notification-progress {
    position: absolute;
    bottom: 0;
    left: 0;
    height: 3px;
    background: linear-gradient(90deg, #667eea, #764ba2);
    width: 100%;
    transform-origin: left;
    animation: progress 5s linear forwards;
}

.notification.success .notification-progress {
    background: linear-gradient(90deg, #10b981, #059669);
}

.notification.error .notification-progress {
    background: linear-gradient(90deg, #ef4444, #dc2626);
}

.notification.warning .notification-progress {
    background: linear-gradient(90deg, #f59e0b, #d97706);
}

.notification.info .notification-progress {
    background: linear-gradient(90deg, #3b82f6, #2563eb);
}

/* Animations */
@keyframes slideIn {
    0% {
        transform: translateX(120%);
        opacity: 0;
    }
    100% {
        transform: translateX(0);
        opacity: 1;
    }
}

@keyframes slideOut {
    0% {
        transform: translateX(0);
        opacity: 1;
    }
    100% {
        transform: translateX(120%);
        opacity: 0;
    }
}

@keyframes progress {
    from {
        width: 100%;
    }
    to {
        width: 0%;
    }
}

/* Stacking Notifications */
.notification:nth-child(1) {
    animation-delay: 0s;
}

.notification:nth-child(2) {
    animation-delay: 0.1s;
}

.notification:nth-child(3) {
    animation-delay: 0.2s;
}

/* Hover Effect */
.notification:hover {
    transform: translateX(0) scale(1.02);
    box-shadow: 0 20px 30px rgba(0, 0, 0, 0.4);
    transition: all 0.2s ease;
}

.notification:hover .notification-progress {
    animation-play-state: paused;
}

/* Mobile Responsive */
@media (max-width: 640px) {
    .notification-container {
        top: 10px;
        right: 10px;
        left: 10px;
        max-width: none;
    }
    
    .notification {
        padding: 14px 16px;
    }
    
    .notification-icon {
        width: 36px;
        height: 36px;
        font-size: 16px;
    }
    
    .notification-title {
        font-size: 15px;
    }
    
    .notification-message {
        font-size: 13px;
    }
}

/* Dark Mode Support (already dark by default) */
@media (prefers-color-scheme: light) {
    .notification {
        background: linear-gradient(135deg, #ffffff 0%, #f8fafc 100%);
        border: 1px solid rgba(0, 0, 0, 0.1);
    }
    
    .notification-title {
        color: #1e293b;
    }
    
    .notification-message {
        color: #475569;
    }
    
    .notification-close {
        color: #94a3b8;
    }
    
    .notification-close:hover {
        background: rgba(0, 0, 0, 0.05);
        color: #1e293b;
    }
}

/* Notification Badge */
.notification-badge {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border-radius: 10px;
    background: rgba(30, 41, 59, 0.95);
    border: 1px solid rgba(255, 255, 255, 0.1);
    cursor: pointer;
    transition: all 0.2s ease;
}

.notification-badge:hover {
    background: rgba(30, 41, 59, 1);
    transform: translateY(-2px);
}

.notification-badge i {
    color: #94a3b8;
    font-size: 18px;
    transition: all 0.2s ease;
}

.notification-badge:hover i {
    color: #667eea;
}

.notification-badge .badge-count {
    position: absolute;
    top: -5px;
    right: -5px;
    background: linear-gradient(135deg, #ef4444, #dc2626);
    color: white;
    font-size: 11px;
    font-weight: 600;
    min-width: 18px;
    height: 18px;
    border-radius: 9px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 4px;
    border: 2px solid #1e293b;
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.1);
    }
    100% {
        transform: scale(1);
    }
}

/* Notification Dropdown */
.notification-dropdown {
    position: absolute;
    top: 50px;
    right: 0;
    width: 360px;
    background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
    border-radius: 16px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.4);
    overflow: hidden;
    transform-origin: top right;
    animation: dropdownFade 0.2s ease;
    z-index: 1000;
}

@keyframes dropdownFade {
    from {
        opacity: 0;
        transform: scale(0.95);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

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

.dropdown-header h3 {
    color: white;
    font-size: 16px;
    font-weight: 600;
    margin: 0;
}

.dropdown-header span {
    color: #94a3b8;
    font-size: 13px;
}

.dropdown-footer {
    padding: 12px 20px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    text-align: center;
}

.dropdown-footer a {
    color: #94a3b8;
    text-decoration: none;
    font-size: 13px;
    transition: color 0.2s ease;
}

.dropdown-footer a:hover {
    color: #667eea;
}

.notification-list {
    max-height: 300px;
    overflow-y: auto;
}

.notification-list-item {
    padding: 16px 20px;
    display: flex;
    align-items: flex-start;
    gap: 12px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    cursor: pointer;
    transition: all 0.2s ease;
    text-decoration: none;
}

.notification-list-item:hover {
    background: rgba(255, 255, 255, 0.03);
}

.notification-list-item.unread {
    background: rgba(102, 126, 234, 0.05);
}

.item-icon {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    flex-shrink: 0;
}

.item-content {
    flex: 1;
}

.item-title {
    color: white;
    font-size: 14px;
    font-weight: 500;
    margin-bottom: 4px;
}

.item-message {
    color: #94a3b8;
    font-size: 12px;
    line-height: 1.4;
    margin-bottom: 4px;
}

.item-time {
    color: #64748b;
    font-size: 11px;
    display: flex;
    align-items: center;
    gap: 4px;
}

.unread-dot {
    width: 8px;
    height: 8px;
    background: #3b82f6;
    border-radius: 50%;
    flex-shrink: 0;
}

/* Custom Scrollbar for Notification List */
.notification-list::-webkit-scrollbar {
    width: 4px;
}

.notification-list::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.05);
}

.notification-list::-webkit-scrollbar-thumb {
    background: linear-gradient(135deg, #667eea, #764ba2);
    border-radius: 4px;
}

/* Empty State */
.notification-empty {
    padding: 40px 20px;
    text-align: center;
}

.notification-empty i {
    font-size: 40px;
    color: #334155;
    margin-bottom: 15px;
}

.notification-empty p {
    color: #64748b;
    font-size: 14px;
    margin: 0;
}
    </style>
</head>
<body class="text-white">
    
     <?php include("nav.php"); ?>
  
        <!-- Main Content -->
        <div class="main-content">
    
            <!-- Top Bar -->
            <div class="border-b border-gray-800/50 p-6 glass-card mx-4 mt-4">
                <div class="flex flex-col lg:flex-row lg:justify-between lg:items-center gap-6">
                    <div>
                        <h2 class="text-2xl lg:text-3xl font-bold bg-gradient-to-r from-blue-400 to-purple-400 bg-clip-text text-transparent">
                            Welcome back, <?php echo htmlspecialchars($user['full_name'] ?? $user['username'] ?? 'Trader'); ?>!
                        </h2>
                        <p class="text-gray-400 mt-2">
                            <i class="fas fa-calendar-alt mr-2"></i> <?php echo date('l, F j, Y'); ?>
                            <span class="mx-3">•</span>
                            <i class="fas fa-clock mr-2"></i> Last login: <?php echo $last_login_date; ?>
                        </p>
                    </div>
                    <div class="flex items-center gap-4">
                       
                 

<!-- Notifications Dropdown -->
<div class="fixed top-6 right-6 z-[1100]" style="z-index: 3;">
    <div class="relative" style="z-index: 3;">
        <!-- Notification Bell -->
        <button id="notificationToggle" class="relative w-12 h-12 rounded-xl bg-gray-800/95 hover:bg-gray-800 flex items-center justify-center transition-all duration-300 border border-gray-700/50 backdrop-blur-lg group">
           <!-- <i class="fas fa-bell text-gray-400 text-lg group-hover:text-blue-400 transition-colors"></i> -->
            <?php
            // Fetch unread notifications count
            $unread_stmt = $conn->prepare("SELECT COUNT(*) as unread_count FROM notifications WHERE user_id = ? AND is_read = 0");
            $unread_stmt->bind_param("i", $user_id);
            $unread_stmt->execute();
            $unread_result = $unread_stmt->get_result();
            $unread_count = $unread_result->fetch_assoc()['unread_count'];
            $unread_stmt->close();
            
            if ($unread_count > 0):
            ?>
            <span class="absolute -top-1 -right-1 w-5 h-5 bg-gradient-to-r from-red-500 to-pink-500 rounded-full flex items-center justify-center text-xs font-bold text-white animate-pulse shadow-lg shadow-red-500/30">
                <?php echo min($unread_count, 9); if($unread_count > 9) echo '+'; ?>
            </span>
            <?php endif; ?>
        </button>
        
        <!-- Notifications Dropdown Menu -->
        <div id="notificationsDropdown" style="z-index: 3;" class="hidden absolute right-0 mt-3 w-96 bg-gradient-to-b from-gray-900 to-gray-950 rounded-2xl shadow-2xl border border-gray-800/50 backdrop-blur-xl overflow-hidden z-50 transform transition-all duration-300 origin-top-right">
            <!-- Dropdown Header -->
            <div class="p-4 border-b border-gray-800/50 bg-gray-900/50" style="z-index: 3;">
                <div class="flex items-center justify-between" style="z-index: 3;">
                    <div style="z-index: 3px !important;">
                        <h3 class="font-bold text-white">Notifications</h3>
                        <p class="text-xs text-gray-400 mt-0.5">You have <?php echo $unread_count; ?> unread messages</p>
                    </div>
                    <div class="flex items-center gap-2" style="z-index: 3;">
                        <?php if ($unread_count > 0): ?>
                        <a href="mark_all_read.php" class="text-xs px-3 py-1.5 bg-blue-500/20 hover:bg-blue-500/30 text-blue-400 rounded-lg transition-all hover:scale-105 flex items-center gap-1">
                            <i class="fas fa-check-double text-xs"></i>
                            Mark all read
                        </a>
                        <?php endif; ?>
                        <!--
                        <a href="notifications.php" class="text-xs px-3 py-1.5 bg-gray-800/50 hover:bg-gray-800 text-gray-300 rounded-lg transition-all hover:scale-105">
                            View all
                        </a>
                        -->
                    </div>
                </div>
            </div>
            
            <!-- Notifications List -->
            <div class="max-h-[32rem] overflow-y-auto custom-scrollbar" style="z-index: 3;">
                <?php
                // Fetch recent notifications
                $notifications_stmt = $conn->prepare("
                    SELECT * FROM notifications 
                    WHERE user_id = ? 
                    ORDER BY created_at DESC 
                    LIMIT 10
                ");
                $notifications_stmt->bind_param("i", $user_id);
                $notifications_stmt->execute();
                $notifications_result = $notifications_stmt->get_result();
                
                if ($notifications_result->num_rows > 0):
                    while ($notification = $notifications_result->fetch_assoc()):
                        // Set icon based on notification type
                        $icon_class = 'fa-bell';
                        $icon_bg = 'bg-blue-500/20';
                        $icon_color = 'text-blue-400';
                        
                        switch ($notification['notification_type']) {
                            case 'deposit':
                                $icon_class = 'fa-arrow-down';
                                $icon_bg = 'bg-green-500/20';
                                $icon_color = 'text-green-400';
                                break;
                            case 'withdrawal':
                                $icon_class = 'fa-arrow-up';
                                $icon_bg = 'bg-blue-500/20';
                                $icon_color = 'text-blue-400';
                                break;
                            case 'investment':
                                $icon_class = 'fa-chart-line';
                                $icon_bg = 'bg-purple-500/20';
                                $icon_color = 'text-purple-400';
                                break;
                            case 'profit':
                                $icon_class = 'fa-coins';
                                $icon_bg = 'bg-yellow-500/20';
                                $icon_color = 'text-yellow-400';
                                break;
                            case 'referral':
                                $icon_class = 'fa-users';
                                $icon_bg = 'bg-orange-500/20';
                                $icon_color = 'text-orange-400';
                                break;
                            case 'security':
                                $icon_class = 'fa-shield-alt';
                                $icon_bg = 'bg-red-500/20';
                                $icon_color = 'text-red-400';
                                break;
                        }
                        
                        // Format time
                        $notification_time = strtotime($notification['created_at']);
                        $time_diff = time() - $notification_time;
                        
                        if ($time_diff < 60) {
                            $time_text = 'Just now';
                        } elseif ($time_diff < 3600) {
                            $minutes = floor($time_diff / 60);
                            $time_text = $minutes . ' min' . ($minutes > 1 ? 's' : '') . ' ago';
                        } elseif ($time_diff < 86400) {
                            $hours = floor($time_diff / 3600);
                            $time_text = $hours . ' hour' . ($hours > 1 ? 's' : '') . ' ago';
                        } elseif ($time_diff < 604800) {
                            $days = floor($time_diff / 86400);
                            $time_text = $days . ' day' . ($days > 1 ? 's' : '') . ' ago';
                        } else {
                            $time_text = date('M d, Y', $notification_time);
                        }
                ?>
                <a href="view_notification.php?id=<?php echo $notification['id']; ?>" 
                   class="notification-item block px-4 py-4 hover:bg-gray-800/50 transition-all duration-200 border-b border-gray-800/30 <?php echo !$notification['is_read'] ? 'bg-blue-500/5' : ''; ?> group">
                    <div class="flex items-start gap-4">
                        <!-- Icon -->
                        <div class="flex-shrink-0">
                            <div class="w-10 h-10 <?php echo $icon_bg; ?> rounded-xl flex items-center justify-center group-hover:scale-110 transition-transform">
                                <i class="fas <?php echo $icon_class; ?> <?php echo $icon_color; ?> text-sm"></i>
                            </div>
                        </div>
                        
                        <!-- Content -->
                        <div class="flex-1 min-w-0">
                            <div class="flex items-start justify-between gap-2">
                                <div>
                                    <p class="font-medium text-sm text-white group-hover:text-blue-400 transition-colors">
                                        <?php echo htmlspecialchars($notification['title']); ?>
                                    </p>
                                    <p class="text-xs text-gray-400 mt-0.5 line-clamp-2">
                                        <?php echo htmlspecialchars($notification['message']); ?>
                                    </p>
                                </div>
                                <?php if (!$notification['is_read']): ?>
                                <span class="w-2 h-2 bg-blue-500 rounded-full flex-shrink-0 mt-1.5"></span>
                                <?php endif; ?>
                            </div>
                            <div class="flex items-center gap-2 mt-2">
                                <span class="text-xs text-gray-500">
                                    <i class="far fa-clock mr-1 text-[10px]"></i>
                                    <?php echo $time_text; ?>
                                </span>
                                <?php if (!$notification['is_read']): ?>
                                <span class="text-xs px-2 py-0.5 bg-blue-500/20 text-blue-400 rounded-full">
                                    New
                                </span>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </a>
                <?php 
                    endwhile;
                else:
                ?>
                <!-- Empty State -->
                <div class="px-4 py-12 text-center">
                    <div class="w-20 h-20 bg-gray-800/50 rounded-full flex items-center justify-center mx-auto mb-4">
                        <i class="fas fa-bell text-gray-600 text-3xl"></i>
                    </div>
                    <h4 class="text-white font-medium mb-1">No notifications yet</h4>
                    <p class="text-gray-500 text-sm">We'll notify you when something arrives</p>
                </div>
                <?php endif; ?>
                <?php $notifications_stmt->close(); ?>
            </div>
            
            <!-- Dropdown Footer -->
            <?php if ($notifications_result->num_rows > 0): ?>
            <div class="p-3 border-t border-gray-800/50 bg-gray-900/50 text-center">
                <a href="notifications.php" class="text-sm text-gray-400 hover:text-blue-400 transition-colors flex items-center justify-center gap-2 py-1">
                    View all notifications
                    <i class="fas fa-arrow-right text-xs"></i>
                </a>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<!-- Add this CSS for custom scrollbar -->
<style>
.custom-scrollbar::-webkit-scrollbar {
    width: 4px;
}

.custom-scrollbar::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.05);
    border-radius: 4px;
}

.custom-scrollbar::-webkit-scrollbar-thumb {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 4px;
}

.custom-scrollbar::-webkit-scrollbar-thumb:hover {
    background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
}

/* Line clamp utility */
.line-clamp-2 {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* Notification dropdown animation */
#notificationsDropdown {
    transform-origin: top right;
    animation: dropdownFadeIn 0.2s ease-out;
}

@keyframes dropdownFadeIn {
    from {
        opacity: 0;
        transform: scale(0.95);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

/* Notification item hover effect */
.notification-item {
    position: relative;
    overflow: hidden;
}

.notification-item::after {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    width: 3px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    transform: translateX(-100%);
    transition: transform 0.2s ease;
}

.notification-item:hover::after {
    transform: translateX(0);
}

.notification-item:active {
    transform: scale(0.98);
}
</style>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const notificationToggle = document.getElementById('notificationToggle');
    const notificationsDropdown = document.getElementById('notificationsDropdown');
    
    if (notificationToggle && notificationsDropdown) {
        // Toggle dropdown
        notificationToggle.addEventListener('click', function(e) {
            e.preventDefault();
            e.stopPropagation();
            notificationsDropdown.classList.toggle('hidden');
            
            // Add animation class
            if (!notificationsDropdown.classList.contains('hidden')) {
                notificationsDropdown.classList.add('animate-in');
                
                // Mark notifications as read when dropdown opens
                <?php if ($unread_count > 0): ?>
                setTimeout(function() {
                    fetch('mark_notifications_read.php', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                        },
                        body: JSON.stringify({
                            user_id: <?php echo $user_id; ?>
                        })
                    }).then(() => {
                        // Remove notification badge
                        const badge = notificationToggle.querySelector('span');
                        if (badge) {
                            badge.style.animation = 'fadeOut 0.3s ease';
                            setTimeout(() => badge.remove(), 300);
                        }
                    });
                }, 2000);
                <?php endif; ?>
            }
        });
        
        // Close dropdown when clicking outside
        document.addEventListener('click', function(e) {
            if (!notificationToggle.contains(e.target) && !notificationsDropdown.contains(e.target)) {
                notificationsDropdown.classList.add('hidden');
            }
        });
        
        // Prevent dropdown from closing when clicking inside it
        notificationsDropdown.addEventListener('click', function(e) {
            e.stopPropagation();
        });
        
        // Close on escape key
        document.addEventListener('keydown', function(e) {
            if (e.key === 'Escape' && !notificationsDropdown.classList.contains('hidden')) {
                notificationsDropdown.classList.add('hidden');
            }
        });
    }
});
</script>

<!-- Remove the old notification bell from the top bar -->
<!-- Find and remove this section:
<button class="w-12 h-12 rounded-xl bg-gray-800/50 flex items-center justify-center hover:bg-gray-800 transition-colors border border-gray-700">
    <i class="fas fa-bell text-gray-400 text-lg"></i>
    <span class="absolute top-3 right-3 w-2 h-2 bg-red-500 rounded-full animate-pulse"></span>
</button>
-->
                    </div>
                </div>
            </div>
            
            <!-- TradingView Widget BEGIN -->
<div class="tradingview-widget-container">
  <div class="tradingview-widget-container__widget"></div>
  <script type="text/javascript" src="https://s3.tradingview.com/external-embedding/embed-widget-ticker-tape.js" async>
  {
    "symbols": [
      {
        "proName": "BITSTAMP:BTCUSD",
        "title": "Bitcoin"
      },
      {
        "proName": "BITSTAMP:ETHUSD",
        "title": "Ethereum"
      },
      {
        "proName": "BINANCE:SOLUSD",
        "title": "Solana"
      },
      {
        "proName": "BINANCE:ADAUSD",
        "title": "Cardano"
      },
      {
        "proName": "BINANCE:DOTUSD",
        "title": "Polkadot"
      },
      {
        "proName": "BINANCE:AVAXUSD",
        "title": "Avalanche"
      },
      {
        "proName": "BINANCE:MATICUSD",
        "title": "Polygon"
      },
      {
        "proName": "BINANCE:LINKUSD",
        "title": "Chainlink"
      },
      {
        "proName": "COINBASE:DOGEUSD",
        "title": "Dogecoin"
      },
      {
        "proName": "BINANCE:XRPUSD",
        "title": "Ripple"
      },
      {
        "proName": "BINANCE:LTCUSD",
        "title": "Litecoin"
      },
      {
        "proName": "BINANCE:UNIUSD",
        "title": "Uniswap"
      }
    ],
    "showSymbolLogo": true,
    "colorTheme": "dark",
    "isTransparent": false,
    "displayMode": "compact",
    "locale": "en",
    "backgroundColor": "#000000"
  }
  </script>
</div>
<!-- TradingView Widget END -->

            <!-- Dashboard Content -->
            <div class="p-6">
                <!-- Balance Overview -->
                <div class="grid grid-cols-1 lg:grid-cols-3 gap-6 mb-8">
                    <!-- Total Balance Card -->
                    <div class="gradient-card p-6 animate-fade-in">
                        <div class="flex items-start justify-between mb-6">
                            <div>
                                <p class="text-gray-400 text-sm mb-1">Total Balance</p>
                                <h3 class="text-3xl font-bold">$<?php echo number_format($total_assets, 2); ?></h3>
                            </div>
                            <div class="w-14 h-14 bg-gradient-to-br from-blue-500/20 to-purple-500/20 rounded-xl flex items-center justify-center">
                                <i class="fas fa-wallet text-2xl text-blue-400"></i>
                            </div>
                        </div>
                        <div class="space-y-3">
                            <div class="flex justify-between items-center">
                                <span class="text-gray-400">Available</span>
                                <span class="font-semibold text-green-400">$<?php echo number_format($available_balance, 2); ?></span>
                            </div>
                            <div class="flex justify-between items-center">
                                <span class="text-gray-400">Invested</span>
                                <span class="font-semibold text-blue-400">$<?php echo number_format($investment_balance, 2); ?></span>
                            </div>
                            <div class="pt-3 border-t border-gray-700/50">
                                <div class="flex justify-between text-sm">
                                    <span class="text-gray-400">Withdrawal Limit</span>
                                    <span class="font-medium">$<?php echo number_format($withdrawal_limit, 2); ?></span>
                                </div>
                                <div class="progress-bar mt-2">
                                    <div class="progress-fill" style="width: <?php echo min(100, ($available_balance / ($total_assets ?: 1)) * 100); ?>%"></div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Earnings Card -->
                    <div class="metric-card green animate-fade-in" style="animation-delay: 0.1s">
                        <div class="flex items-start justify-between mb-6">
                            <div>
                                <p class="text-gray-400 text-sm mb-1">Total Earnings</p>
                                <h3 class="text-3xl font-bold">$<?php echo number_format($total_earnings, 2); ?></h3>
                            </div>
                            <div class="w-14 h-14 bg-green-500/10 rounded-xl flex items-center justify-center">
                                <i class="fas fa-chart-line text-2xl text-green-400"></i>
                            </div>
                        </div>
                        <div class="space-y-4">
                            <div class="flex justify-between items-center">
                                <div>
                                    <span class="text-gray-400 text-sm">Net Profit</span>
                                    <div class="text-lg font-bold <?php echo $net_profit >= 0 ? 'text-green-400' : 'text-red-400'; ?>">
                                        $<?php echo number_format($net_profit, 2); ?>
                                    </div>
                                </div>
                                <div class="text-right">
                                    <span class="text-gray-400 text-sm">ROI</span>
                                    <div class="text-lg font-bold <?php echo $roi_percentage >= 0 ? 'text-green-400' : 'text-red-400'; ?>">
                                        <?php echo number_format($roi_percentage, 2); ?>%
                                    </div>
                                </div>
                            </div>
                            <div class="pt-3 border-t border-gray-700/50">
                                <div class="text-sm text-gray-400 mb-2">Performance Trend</div>
                                <div class="chart-container">
                                    <!-- Simple sparkline chart using SVG -->
                                    <svg viewBox="0 0 100 40" class="w-full h-10">
                                        <path class="sparkline" d="M0,20 L20,15 L40,25 L60,10 L80,30 L100,20" />
                                        <path class="sparkline-area" d="M0,20 L20,15 L40,25 L60,10 L80,30 L100,20 L100,40 L0,40 Z" />
                                    </svg>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Quick Actions Card -->
                    <div class="glass-card p-6 animate-fade-in" style="animation-delay: 0.2s">
                        <div class="flex items-center justify-between mb-6">
                            <h4 class="text-lg font-bold">Quick Actions</h4>
                            <i class="fas fa-bolt text-yellow-400 text-xl"></i>
                        </div>
                        <div class="grid grid-cols-2 gap-3">
                            <a href="deposit.php" class="flex flex-col items-center justify-center p-4 bg-blue-500/10 hover:bg-blue-500/20 rounded-xl transition-all hover:scale-105 group">
                                <div class="w-12 h-12 bg-blue-500 rounded-xl flex items-center justify-center mb-2 group-hover:bg-blue-600">
                                    <i class="fas fa-plus text-white text-lg"></i>
                                </div>
                                <span class="font-medium text-sm">Deposit</span>
                            </a>
                            <a href="withdraw.php" class="flex flex-col items-center justify-center p-4 bg-green-500/10 hover:bg-green-500/20 rounded-xl transition-all hover:scale-105 group">
                                <div class="w-12 h-12 bg-green-500 rounded-xl flex items-center justify-center mb-2 group-hover:bg-green-600">
                                    <i class="fas fa-download text-white text-lg"></i>
                                </div>
                                <span class="font-medium text-sm">Withdraw</span>
                            </a>
                            <a href="investment.php" class="flex flex-col items-center justify-center p-4 bg-purple-500/10 hover:bg-purple-500/20 rounded-xl transition-all hover:scale-105 group">
                                <div class="w-12 h-12 bg-purple-500 rounded-xl flex items-center justify-center mb-2 group-hover:bg-purple-600">
                                    <i class="fas fa-chart-line text-white text-lg"></i>
                                </div>
                                <span class="font-medium text-sm">Invest</span>
                            </a>
                            <a href="support.php" class="flex flex-col items-center justify-center p-4 bg-orange-500/10 hover:bg-orange-500/20 rounded-xl transition-all hover:scale-105 group">
                                <div class="w-12 h-12 bg-orange-500 rounded-xl flex items-center justify-center mb-2 group-hover:bg-orange-600">
                                    <i class="fas fa-phone text-white text-lg"></i>
                                </div>
                                <span class="font-medium text-sm">Support</span>
                            </a>
                        </div>
                    </div>
                </div>
                
                <style>
  /* Force TradingView widget to be black */
  .tradingview-widget-container,
  .tradingview-widget-container *,
  .tradingview-widget-container iframe,
  .tradingview-widget-container div {
    background-color: #000000 !important;
    color: #ffffff !important;
    border: none !important;
  }
  
  /* Full width container */
  .tradingview-widget-container {
    width: 100% !important;
    max-width: 100% !important;
    margin: 0 !important;
    padding: 0 !important;
  }
  
  /* Chart container */
  #tradingview_chart {
    width: 100% !important;
    height: 500px !important;
  }
</style>

<!-- TradingView Widget BEGIN -->
<div class="tradingview-widget-container">
  <div id="tradingview_chart"></div>
  <script type="text/javascript" src="https://s3.tradingview.com/tv.js"></script>
  <script type="text/javascript">
  new TradingView.widget({
    "width": "100%",
    "height": 500,
    "symbol": "NASDAQ:AAPL",
    "interval": "D",
    "timezone": "Etc/UTC",
    "theme": "dark",
    "style": "1",
    "locale": "en",
    "toolbar_bg": "#000000",
    "enable_publishing": false,
    "allow_symbol_change": true,
    "container_id": "tradingview_chart"
  });
  </script>
</div>
<!-- TradingView Widget END -->

<br>

                <!-- Investment Summary -->
                <div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-8">
                    <!-- Investment Statistics -->
                    <div class="glass-card p-6">
                        <div class="flex items-center justify-between mb-6">
                            <h4 class="text-lg font-bold">Investment Statistics</h4>
                            <div class="flex items-center gap-2">
                                <span class="text-xs px-2 py-1 bg-blue-500/20 text-blue-400 rounded-full">Active</span>
                                <span class="text-xs px-2 py-1 bg-green-500/20 text-green-400 rounded-full"><?php echo $total_investments_count; ?> Total</span>
                            </div>
                        </div>
                        
                        <div class="space-y-6">
                            <!-- Investment Distribution -->
                            <div>
                                <div class="flex justify-between text-sm mb-2">
                                    <span class="text-gray-400">Investment Distribution</span>
                                    <span class="font-medium">$<?php echo number_format($total_invested_amount, 2); ?></span>
                                </div>
                                <div class="h-2 bg-gray-700 rounded-full overflow-hidden">
                                    <?php
                                    $total = $active_investments + $pending_investments + $completed_investments;
                                    if ($total > 0) {
                                        $active_width = ($active_investments / $total) * 100;
                                        $pending_width = ($pending_investments / $total) * 100;
                                        $completed_width = ($completed_investments / $total) * 100;
                                    } else {
                                        $active_width = $pending_width = $completed_width = 0;
                                    }
                                    ?>
                                    <div class="h-full flex">
                                        <div class="bg-blue-500 h-full" style="width: <?php echo $active_width; ?>%"></div>
                                        <div class="bg-yellow-500 h-full" style="width: <?php echo $pending_width; ?>%"></div>
                                        <div class="bg-green-500 h-full" style="width: <?php echo $completed_width; ?>%"></div>
                                    </div>
                                </div>
                                <div class="flex justify-between text-xs text-gray-400 mt-2">
                                    <div class="flex items-center">
                                        <div class="w-3 h-3 bg-blue-500 rounded mr-1"></div>
                                        <span>Active: <?php echo $active_investments; ?></span>
                                    </div>
                                    <div class="flex items-center">
                                        <div class="w-3 h-3 bg-yellow-500 rounded mr-1"></div>
                                        <span>Pending: <?php echo $pending_investments; ?></span>
                                    </div>
                                    <div class="flex items-center">
                                        <div class="w-3 h-3 bg-green-500 rounded mr-1"></div>
                                        <span>Completed: <?php echo $completed_investments; ?></span>
                                    </div>
                                </div>
                            </div>
                            
                            <!-- Stats Grid -->
                            <div class="grid grid-cols-3 gap-4">
                                <div class="text-center p-4 bg-gray-800/50 rounded-xl">
                                    <div class="text-2xl font-bold text-blue-400"><?php echo $active_investments; ?></div>
                                    <div class="text-xs text-gray-400 mt-1">Active</div>
                                </div>
                                <div class="text-center p-4 bg-gray-800/50 rounded-xl">
                                    <div class="text-2xl font-bold text-yellow-400"><?php echo $pending_investments; ?></div>
                                    <div class="text-xs text-gray-400 mt-1">Pending</div>
                                </div>
                                <div class="text-center p-4 bg-gray-800/50 rounded-xl">
                                    <div class="text-2xl font-bold text-green-400"><?php echo $completed_investments; ?></div>
                                    <div class="text-xs text-gray-400 mt-1">Completed</div>
                                </div>
                            </div>
                            
                            <!-- Performance Indicator -->
                            <div class="pt-4 border-t border-gray-700/50">
                                <div class="flex items-center justify-between">
                                    <div>
                                        <span class="text-gray-400 text-sm">Performance</span>
                                        <div class="text-lg font-bold <?php echo $roi_percentage >= 0 ? 'text-green-400' : 'text-red-400'; ?>">
                                            <?php echo $roi_percentage >= 0 ? '+' : ''; ?><?php echo number_format($roi_percentage, 2); ?>%
                                        </div>
                                    </div>
                                    <div class="text-right">
                                        <span class="text-gray-400 text-sm">Avg. Return</span>
                                        <div class="text-lg font-bold text-blue-400">15.2%</div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Transaction History -->
                    <div class="glass-card p-6">
                        
                        <div class="flex items-center justify-between mb-6">
                            <h4 class="text-lg font-bold">Recent Activity</h4>
                            <a href="statement.php" class="text-sm text-blue-400 hover:text-blue-300 flex items-center gap-1">
                                View All <i class="fas fa-arrow-right text-xs"></i>
                            </a>
                        </div>
                        
                        <div class="space-y-4">
                            <!-- Deposit -->
                            <div class="flex items-center justify-between p-4 bg-gray-800/30 rounded-xl">
                                <div class="flex items-center">
                                    <div class="w-10 h-10 bg-green-500/20 rounded-lg flex items-center justify-center mr-3">
                                        <i class="fas fa-arrow-down text-green-400"></i>
                                    </div>
                                    <div>
                                        <p class="font-medium">Deposit</p>
                                        <p class="text-xs text-gray-400">Via Bank Transfer</p>
                                    </div>
                                </div>
                                <div class="text-right">
                                    <p class="font-bold text-green-400">+$<?php echo number_format($total_deposits > 0 ? $total_deposits/2 : 500, 2); ?></p>
                                    <p class="text-xs text-gray-400">2 hours ago</p>
                                </div>
                            </div>
                            
                            <!-- Withdrawal -->
                            <div class="flex items-center justify-between p-4 bg-gray-800/30 rounded-xl">
                                <div class="flex items-center">
                                    <div class="w-10 h-10 bg-blue-500/20 rounded-lg flex items-center justify-center mr-3">
                                        <i class="fas fa-arrow-up text-blue-400"></i>
                                    </div>
                                    <div>
                                        <p class="font-medium">Withdrawal</p>
                                        <p class="text-xs text-gray-400">To Bank Account</p>
                                    </div>
                                </div>
                                <div class="text-right">
                                    <p class="font-bold text-blue-400">-$<?php echo number_format($total_withdrawals > 0 ? $total_withdrawals/3 : 200, 2); ?></p>
                                    <p class="text-xs text-gray-400">1 day ago</p>
                                </div>
                            </div>
                            
                            <!-- Investment -->
                            <div class="flex items-center justify-between p-4 bg-gray-800/30 rounded-xl">
                                <div class="flex items-center">
                                    <div class="w-10 h-10 bg-purple-500/20 rounded-lg flex items-center justify-center mr-3">
                                        <i class="fas fa-chart-line text-purple-400"></i>
                                    </div>
                                    <div>
                                        <p class="font-medium">Investment</p>
                                        <p class="text-xs text-gray-400">Gold Plan</p>
                                    </div>
                                </div>
                                <div class="text-right">
                                    <p class="font-bold text-purple-400">+$<?php echo number_format($total_invested > 0 ? $total_invested/4 : 1000, 2); ?></p>
                                    <p class="text-xs text-gray-400">3 days ago</p>
                                </div>
                            </div>
                            
                            <!-- Referral Bonus -->
                            <div class="flex items-center justify-between p-4 bg-gray-800/30 rounded-xl">
                                <div class="flex items-center">
                                    <div class="w-10 h-10 bg-orange-500/20 rounded-lg flex items-center justify-center mr-3">
                                        <i class="fas fa-users text-orange-400"></i>
                                    </div>
                                    <div>
                                        <p class="font-medium">Referral Bonus</p>
                                        <p class="text-xs text-gray-400">From @trader_john</p>
                                    </div>
                                </div>
                                <div class="text-right">
                                    <p class="font-bold text-orange-400">+$<?php echo number_format($referral_earnings > 0 ? $referral_earnings/2 : 50, 2); ?></p>
                                    <p class="text-xs text-gray-400">1 week ago</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Referral & Account Stats -->
                <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
                    <!-- Referral Program -->
                    <div class="glass-card p-6">
                        <div class="flex items-center justify-between mb-6">
                            <h4 class="text-lg font-bold">Referral Program</h4>
                            <span class="text-xs px-3 py-1 bg-purple-500/20 text-purple-400 rounded-full font-medium">
                                <i class="fas fa-star mr-1"></i> Active
                            </span>
                        </div>
                        
                        <div class="space-y-6">
                           
                          
                            
                            <!-- Share Links -->
                            <div class="pt-4 border-t border-gray-700/50">
                                <p class="text-gray-400 text-sm mb-3">Share your link</p>
                                <div class="flex gap-2">
                                    <button class="flex-1 flex items-center justify-center gap-2 bg-gray-800/50 hover:bg-gray-800 py-2 rounded-lg transition-colors">
                                        <i class="fab fa-facebook text-blue-400"></i>
                                        <span class="text-sm">Facebook</span>
                                    </button>
                                    <button class="flex-1 flex items-center justify-center gap-2 bg-gray-800/50 hover:bg-gray-800 py-2 rounded-lg transition-colors">
                                        <i class="fab fa-twitter text-blue-400"></i>
                                        <span class="text-sm">Twitter</span>
                                    </button>
                                    <button class="flex-1 flex items-center justify-center gap-2 bg-gray-800/50 hover:bg-gray-800 py-2 rounded-lg transition-colors">
                                        <i class="fab fa-whatsapp text-green-400"></i>
                                        <span class="text-sm">WhatsApp</span>
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Account Security -->
                    <div class="glass-card p-6">
                        <div class="flex items-center justify-between mb-6">
                            <h4 class="text-lg font-bold">Account Security</h4>
                            <div class="text-xs px-3 py-1 bg-green-500/20 text-green-400 rounded-full font-medium">
                                <?php
                                $security_score = 0;
                                if (isset($user['email_verified']) && $user['email_verified']) $security_score += 25;
                                if (isset($user['two_factor_enabled']) && $user['two_factor_enabled']) $security_score += 25;
                                if (isset($user['account_status']) && $user['account_status'] == 'active') $security_score += 25;
                                echo $security_score; ?>% Secure
                            </div>
                        </div>
                        
                        <div class="space-y-4">
                            <!-- Security Score -->
                            <div>
                                <div class="flex justify-between text-sm mb-2">
                                    <span class="text-gray-400">Security Score</span>
                                    <span class="font-medium"><?php echo $security_score; ?>%</span>
                                </div>
                                <div class="progress-bar">
                                    <div class="progress-fill" style="width: <?php echo $security_score; ?>%"></div>
                                </div>
                            </div>
                            
                            <!-- Security Items -->
                            <div class="space-y-3">
                                <div class="flex items-center justify-between p-3 bg-gray-800/30 rounded-lg">
                                    <div class="flex items-center">
                                        <div class="w-8 h-8 rounded-lg flex items-center justify-center mr-3 <?php echo isset($user['email_verified']) && $user['email_verified'] ? 'bg-green-500/20' : 'bg-yellow-500/20'; ?>">
                                            <i class="fas fa-envelope <?php echo isset($user['email_verified']) && $user['email_verified'] ? 'text-green-400' : 'text-yellow-400'; ?>"></i>
                                        </div>
                                        <div>
                                            <p class="font-medium text-sm">Email Verification</p>
                                            <p class="text-xs text-gray-400">Verify your email address</p>
                                        </div>
                                    </div>
                                    <span class="text-sm <?php echo isset($user['email_verified']) && $user['email_verified'] ? 'text-green-400' : 'text-yellow-400'; ?>">
                                        <?php echo isset($user['email_verified']) && $user['email_verified'] ? 'Verified' : 'Pending'; ?>
                                    </span>
                                </div>
                                
                                <div class="flex items-center justify-between p-3 bg-gray-800/30 rounded-lg">
                                    <div class="flex items-center">
                                        <div class="w-8 h-8 rounded-lg flex items-center justify-center mr-3 <?php echo isset($user['two_factor_enabled']) && $user['two_factor_enabled'] ? 'bg-green-500/20' : 'bg-red-500/20'; ?>">
                                            <i class="fas fa-shield-alt <?php echo isset($user['two_factor_enabled']) && $user['two_factor_enabled'] ? 'text-green-400' : 'text-red-400'; ?>"></i>
                                        </div>
                                        <div>
                                            <p class="font-medium text-sm">2FA Authentication</p>
                                            <p class="text-xs text-gray-400">Add extra security layer</p>
                                        </div>
                                    </div>
                                    <a href="security.php" class="text-sm text-blue-400 hover:text-blue-300">
                                        <?php echo isset($user['two_factor_enabled']) && $user['two_factor_enabled'] ? 'Enabled' : 'Enable'; ?>
                                    </a>
                                </div>
                                
                                <div class="flex items-center justify-between p-3 bg-gray-800/30 rounded-lg">
                                    <div class="flex items-center">
                                        <div class="w-8 h-8 rounded-lg flex items-center justify-center mr-3 <?php echo isset($user['kyc_status']) && $user['kyc_status'] == 'verified' ? 'bg-green-500/20' : 'bg-yellow-500/20'; ?>">
                                            <i class="fas fa-id-card <?php echo isset($user['kyc_status']) && $user['kyc_status'] == 'verified' ? 'text-green-400' : 'text-yellow-400'; ?>"></i>
                                        </div>
                                        <div>
                                            <p class="font-medium text-sm">KYC Verification</p>
                                            <p class="text-xs text-gray-400">Verify your identity</p>
                                        </div>
                                    </div>
                                    <span class="text-sm <?php echo isset($user['kyc_status']) && $user['kyc_status'] == 'verified' ? 'text-green-400' : 'text-yellow-400'; ?>">
                                        <?php echo isset($user['kyc_status']) ? ucfirst($user['kyc_status']) : 'Pending'; ?>
                                    </span>
                                </div>
                            </div>
                            
                            <!-- Security Tips -->
                            <div class="pt-4 border-t border-gray-700/50">
                                <p class="text-sm text-gray-400 mb-2">Security Tips</p>
                                <ul class="text-xs text-gray-500 space-y-1">
                                    <li class="flex items-center gap-2">
                                        <i class="fas fa-check-circle text-green-400"></i>
                                        <span>Use a strong, unique password</span>
                                    </li>
                                    <li class="flex items-center gap-2">
                                        <i class="fas fa-check-circle text-green-400"></i>
                                        <span>Enable Two-Factor Authentication</span>
                                    </li>
                                    <li class="flex items-center gap-2">
                                        <i class="fas fa-check-circle text-green-400"></i>
                                        <span>Never share your login details</span>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- Footer -->
                <div class="mt-8 text-center">
                    <p class="text-gray-500 text-sm">
                        © <?php echo date('Y'); ?> <?php echo htmlspecialchars($site_settings['site_name'] ?? 'Trading Platform'); ?>. 
                        All rights reserved. | 
                        Account ID: <span class="font-mono text-gray-400"><?php echo isset($user['id']) ? str_pad($user['id'], 8, '0', STR_PAD_LEFT) : 'N/A'; ?></span>
                    </p>
                    <p class="text-gray-600 text-xs mt-2">
                        Need assistance? <a href="support.php" class="text-blue-400 hover:text-blue-300">Contact our support team</a>
                    </p>
                </div>
            </div>
        </div>
    </div>

  <script>
document.addEventListener('DOMContentLoaded', function() {
    // Mobile menu toggle functionality
    const menuToggle = document.getElementById('menuToggle');
    const sidebar = document.getElementById('sidebar');
    const sidebarOverlay = document.getElementById('sidebarOverlay');
    const closeSidebar = document.getElementById('closeSidebar');
    
    // Debug: Check if elements exist
    console.log('menuToggle:', menuToggle);
    console.log('sidebar:', sidebar);
    console.log('sidebarOverlay:', sidebarOverlay);
    console.log('closeSidebar:', closeSidebar);
    
    // Function to toggle sidebar
    function toggleSidebar() {
        console.log('Toggling sidebar');
        sidebar.classList.toggle('active');
        sidebarOverlay.classList.toggle('active');
        
        // Prevent body scroll when sidebar is open on mobile
        if (sidebar.classList.contains('active')) {
            document.body.style.overflow = 'hidden';
            console.log('Sidebar opened');
        } else {
            document.body.style.overflow = '';
            console.log('Sidebar closed');
        }
    }
    
    // Function to close sidebar
    function closeSidebarFunc() {
        console.log('Closing sidebar');
        sidebar.classList.remove('active');
        sidebarOverlay.classList.remove('active');
        document.body.style.overflow = '';
    }
    
    // Add click event to menu toggle button
    if (menuToggle) {
        menuToggle.addEventListener('click', function(e) {
            e.preventDefault();
            e.stopPropagation();
            toggleSidebar();
        });
    } else {
        console.error('Menu toggle button not found!');
    }
    
    // Add click event to close button
    if (closeSidebar) {
        closeSidebar.addEventListener('click', function(e) {
            e.preventDefault();
            e.stopPropagation();
            closeSidebarFunc();
        });
    } else {
        console.error('Close sidebar button not found!');
    }
    
    // Add click event to overlay
    if (sidebarOverlay) {
        sidebarOverlay.addEventListener('click', function(e) {
            e.preventDefault();
            e.stopPropagation();
            closeSidebarFunc();
        });
    } else {
        console.error('Sidebar overlay not found!');
    }
    
    // Close sidebar on escape key
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Escape' && sidebar.classList.contains('active')) {
            closeSidebarFunc();
        }
    });
    
    // Handle window resize
    window.addEventListener('resize', function() {
        if (window.innerWidth >= 1024) {
            // Desktop: Always show sidebar
            sidebar.classList.add('active');
            sidebarOverlay.classList.remove('active');
            document.body.style.overflow = '';
        } else {
            // Mobile: Hide sidebar by default
            sidebar.classList.remove('active');
            sidebarOverlay.classList.remove('active');
            document.body.style.overflow = '';
        }
    });
    
    // Initialize sidebar state on load
    if (window.innerWidth >= 1024) {
        sidebar.classList.add('active');
        sidebarOverlay.classList.remove('active');
    } else {
        sidebar.classList.remove('active');
        sidebarOverlay.classList.remove('active');
    }
    
    // Set active nav item based on current page
    const currentPage = window.location.pathname.split('/').pop() || 'index.php';
    console.log('Current page:', currentPage);
    
    document.querySelectorAll('.nav-item').forEach(nav => {
        const href = nav.getAttribute('href');
        if (href && href.includes(currentPage)) {
            nav.classList.add('active');
            console.log('Active nav item set:', href);
        } else if (currentPage === 'dashboard.php' && href === 'index.php') {
            nav.classList.add('active');
            console.log('Active nav item set for dashboard:', href);
        }
    });
    
    // Close sidebar when clicking on navigation links on mobile
    document.querySelectorAll('.nav-item').forEach(item => {
        item.addEventListener('click', function(e) {
            // Don't prevent default for actual links
            if (this.getAttribute('href') === '#') {
                e.preventDefault();
            }
            
            // Update active state
            document.querySelectorAll('.nav-item').forEach(nav => {
                nav.classList.remove('active');
            });
            
            this.classList.add('active');
            
            // Close sidebar on mobile after selection (with slight delay for better UX)
            if (window.innerWidth < 1024) {
                setTimeout(() => {
                    closeSidebarFunc();
                }, 300);
            }
        });
    });
    
    // Initialize animations
    const cards = document.querySelectorAll('.animate-fade-in');
    cards.forEach((card, index) => {
        card.style.animationDelay = `${index * 0.1}s`;
    });
    
    // Notification toggle functionality (if exists)
    const notificationToggle = document.getElementById('notificationToggle');
    const notificationsDropdown = document.getElementById('notificationsDropdown');
    
    if (notificationToggle && notificationsDropdown) {
        notificationToggle.addEventListener('click', function(e) {
            e.preventDefault();
            e.stopPropagation();
            notificationsDropdown.classList.toggle('hidden');
        });
        
        document.addEventListener('click', function(e) {
            if (!notificationToggle.contains(e.target) && !notificationsDropdown.contains(e.target)) {
                notificationsDropdown.classList.add('hidden');
            }
        });
        
        notificationsDropdown.addEventListener('click', function(e) {
            e.stopPropagation();
        });
        
        document.addEventListener('keydown', function(e) {
            if (e.key === 'Escape' && !notificationsDropdown.classList.contains('hidden')) {
                notificationsDropdown.classList.add('hidden');
            }
        });
    }
});

// Copy referral code function
function copyReferralCode() {
    const codeElement = document.querySelector('.font-mono');
    if (!codeElement) return;
    
    const code = codeElement.textContent.trim();
    
    navigator.clipboard.writeText(code).then(() => {
        showNotification('Referral code copied to clipboard!', 'success');
    }).catch(() => {
        // Fallback
        const textArea = document.createElement('textarea');
        textArea.value = code;
        document.body.appendChild(textArea);
        textArea.select();
        document.execCommand('copy');
        document.body.removeChild(textArea);
        showNotification('Referral code copied!', 'success');
    });
}

// Show notification function
function showNotification(message, type = 'info') {
    const existing = document.querySelector('.notification-toast');
    if (existing) existing.remove();
    
    const notification = document.createElement('div');
    notification.className = `notification-toast fixed bottom-6 right-6 px-4 py-3 rounded-xl shadow-xl z-50 animate-fade-in ${type === 'success' ? 'bg-gradient-to-r from-green-600 to-green-500' : 'bg-gradient-to-r from-blue-600 to-blue-500'} text-white`;
    notification.innerHTML = `
        <div class="flex items-center gap-3">
            <i class="fas fa-${type === 'success' ? 'check-circle' : 'info-circle'}"></i>
            <span>${message}</span>
        </div>
    `;
    
    document.body.appendChild(notification);
    
    setTimeout(() => {
        notification.style.opacity = '0';
        notification.style.transform = 'translateX(100%)';
        setTimeout(() => notification.remove(), 300);
    }, 3000);
}

// Share functions
function shareOnFacebook() {
    const url = encodeURIComponent(window.location.origin + '/register.php?ref=<?php echo $user['referral_code'] ?? ''; ?>');
    window.open('https://www.facebook.com/sharer/sharer.php?u=' + url, '_blank');
}

function shareOnTwitter() {
    const text = encodeURIComponent('Join me on this amazing trading platform!');
    const url = encodeURIComponent(window.location.origin + '/register.php?ref=<?php echo $user['referral_code'] ?? ''; ?>');
    window.open('https://twitter.com/intent/tweet?text=' + text + '&url=' + url, '_blank');
}

function shareOnWhatsApp() {
    const text = encodeURIComponent('Join me on this amazing trading platform! ');
    const url = encodeURIComponent(window.location.origin + '/register.php?ref=<?php echo $user['referral_code'] ?? ''; ?>');
    window.open('https://wa.me/?text=' + text + url, '_blank');
}

/**
 * Notification System
 * A complete notification system with toast notifications and dropdown
 */

class NotificationSystem {
    constructor(options = {}) {
        this.options = {
            containerId: options.containerId || 'notificationContainer',
            position: options.position || 'top-right',
            maxNotifications: options.maxNotifications || 5,
            duration: options.duration || 5000,
            animation: options.animation !== false,
            ...options
        };

        this.notifications = [];
        this.container = null;
        this.init();
    }

    init() {
        // Create container if it doesn't exist
        let container = document.getElementById(this.options.containerId);
        if (!container) {
            container = document.createElement('div');
            container.id = this.options.containerId;
            container.className = 'notification-container';
            
            // Set position
            if (this.options.position === 'top-left') {
                container.style.left = '20px';
                container.style.right = 'auto';
            } else if (this.options.position === 'bottom-right') {
                container.style.top = 'auto';
                container.style.bottom = '20px';
            } else if (this.options.position === 'bottom-left') {
                container.style.top = 'auto';
                container.style.bottom = '20px';
                container.style.left = '20px';
                container.style.right = 'auto';
            }
            
            document.body.appendChild(container);
        }
        
        this.container = container;
    }

    /**
     * Show a notification
     * @param {string} message - Notification message
     * @param {string} type - success, error, warning, info
     * @param {object} options - Additional options
     */
    show(message, type = 'info', options = {}) {
        const {
            title = this.getDefaultTitle(type),
            duration = this.options.duration,
            icon = this.getIcon(type)
        } = options;

        // Create notification element
        const notification = document.createElement('div');
        notification.className = `notification ${type}`;
        notification.setAttribute('role', 'alert');
        
        notification.innerHTML = `
            <div class="notification-icon">
                <i class="fas ${icon}"></i>
            </div>
            <div class="notification-content">
                ${title ? `<div class="notification-title">${title}</div>` : ''}
                <p class="notification-message">${message}</p>
            </div>
            <button class="notification-close" aria-label="Close">
                <i class="fas fa-times"></i>
            </button>
            <div class="notification-progress"></div>
        `;

        // Add to container
        this.container.appendChild(notification);
        
        // Track notification
        const notificationObj = {
            element: notification,
            timeout: null
        };
        this.notifications.push(notificationObj);

        // Manage max notifications
        if (this.notifications.length > this.options.maxNotifications) {
            const oldest = this.notifications.shift();
            this.remove(oldest.element);
        }

        // Close button handler
        const closeBtn = notification.querySelector('.notification-close');
        closeBtn.addEventListener('click', () => {
            this.remove(notification);
        });

        // Auto remove after duration
        notificationObj.timeout = setTimeout(() => {
            this.remove(notification);
        }, duration);

        // Mouse enter/pause
        notification.addEventListener('mouseenter', () => {
            clearTimeout(notificationObj.timeout);
            const progress = notification.querySelector('.notification-progress');
            if (progress) {
                progress.style.animationPlayState = 'paused';
            }
        });

        notification.addEventListener('mouseleave', () => {
            notificationObj.timeout = setTimeout(() => {
                this.remove(notification);
            }, duration);
            
            const progress = notification.querySelector('.notification-progress');
            if (progress) {
                progress.style.animationPlayState = 'running';
            }
        });

        return notification;
    }

    /**
     * Remove a notification
     * @param {HTMLElement} notification 
     */
    remove(notification) {
        if (!notification || notification.classList.contains('removing')) return;

        notification.classList.add('removing');
        
        // Remove from array
        this.notifications = this.notifications.filter(n => n.element !== notification);
        
        // Clear timeout
        const notifObj = this.notifications.find(n => n.element === notification);
        if (notifObj && notifObj.timeout) {
            clearTimeout(notifObj.timeout);
        }

        // Remove element after animation
        setTimeout(() => {
            if (notification && notification.parentNode) {
                notification.parentNode.removeChild(notification);
            }
        }, 300);
    }

    /**
     * Clear all notifications
     */
    clearAll() {
        while (this.notifications.length) {
            const notification = this.notifications.shift();
            this.remove(notification.element);
        }
    }

    /**
     * Success notification
     * @param {string} message 
     * @param {object} options 
     */
    success(message, options = {}) {
        return this.show(message, 'success', options);
    }

    /**
     * Error notification
     * @param {string} message 
     * @param {object} options 
     */
    error(message, options = {}) {
        return this.show(message, 'error', options);
    }

    /**
     * Warning notification
     * @param {string} message 
     * @param {object} options 
     */
    warning(message, options = {}) {
        return this.show(message, 'warning', options);
    }

    /**
     * Info notification
     * @param {string} message 
     * @param {object} options 
     */
    info(message, options = {}) {
        return this.show(message, 'info', options);
    }

    /**
     * Get default title based on type
     * @param {string} type 
     */
    getDefaultTitle(type) {
        const titles = {
            success: 'Success!',
            error: 'Error!',
            warning: 'Warning!',
            info: 'Information'
        };
        return titles[type] || 'Notification';
    }

    /**
     * Get icon based on type
     * @param {string} type 
     */
    getIcon(type) {
        const icons = {
            success: 'fa-check-circle',
            error: 'fa-exclamation-circle',
            warning: 'fa-exclamation-triangle',
            info: 'fa-info-circle'
        };
        return icons[type] || 'fa-bell';
    }
}

// Initialize notification system
const notifications = new NotificationSystem();

// Export for use
if (typeof module !== 'undefined' && module.exports) {
    module.exports = NotificationSystem;
} else {
    window.NotificationSystem = NotificationSystem;
    window.notifications = notifications;
}

/**
 * Usage Examples:
 * 
 * // Basic usage
 * notifications.show('Your deposit was successful!', 'success');
 * 
 * // With custom title
 * notifications.show('Welcome back!', 'info', { title: 'Hello!' });
 * 
 * // Shortcut methods
 * notifications.success('Operation completed successfully');
 * notifications.error('Something went wrong');
 * notifications.warning('Please verify your email');
 * notifications.info('New update available');
 * 
 * // With custom duration (10 seconds)
 * notifications.show('This will stay longer', 'info', { duration: 10000 });
 * 
 * // Clear all notifications
 * notifications.clearAll();
 */

// Initialize when DOM is ready
document.addEventListener('DOMContentLoaded', function() {
    // Example: Show welcome notification
    setTimeout(() => {
        notifications.info('Welcome back!', {
            title: 'Hello!',
            duration: 3000
        });
    }, 1000);
});
</script>

<!-- Add this CSS to ensure proper mobile menu behavior -->
<style>
    /* Ensure sidebar transitions work smoothly */
    .sidebar {
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }
    
    @media (max-width: 1023px) {
        .sidebar {
            transform: translateX(-100%);
        }
        
        .sidebar.active {
            transform: translateX(0);
        }
    }
    
    /* Overlay styles */
    .sidebar-overlay {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.7);
        z-index: 39;
        backdrop-filter: blur(4px);
    }
    
    .sidebar-overlay.active {
        display: block;
    }
    
    /* Mobile menu button */
    .mobile-menu-btn {
        position: fixed;
        top: 1.5rem;
        left: 1.5rem;
        z-index: 41;
        width: 3rem;
        height: 3rem;
        background: rgba(30, 41, 59, 0.95);
        border-radius: 0.75rem;
        display: flex;
        align-items: center;
        justify-content: center;
        border: 1px solid rgba(255, 255, 255, 0.1);
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
        transition: all 0.3s ease;
        backdrop-filter: blur(10px);
        cursor: pointer;
    }
    
    .mobile-menu-btn:hover {
        background: rgba(30, 41, 59, 1);
        transform: translateY(-2px);
    }
    
    @media (min-width: 1024px) {
        .mobile-menu-btn {
            display: none;
        }
    }
    
    /* Close button for mobile sidebar */
    .sidebar-close-btn {
        display: none;
    }
    
    @media (max-width: 1023px) {
        .sidebar-close-btn {
            display: block;
            position: absolute;
            top: 1rem;
            right: 1rem;
            width: 2.5rem;
            height: 2.5rem;
            background: rgba(255, 255, 255, 0.1);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            border: none;
            color: white;
            cursor: pointer;
            transition: all 0.2s ease;
            backdrop-filter: blur(10px);
            z-index: 42;
        }
        
        .sidebar-close-btn:hover {
            background: rgba(255, 255, 255, 0.2);
            transform: rotate(90deg);
        }
    }
    
    /* Prevent body scroll when sidebar is open */
    body.sidebar-open {
        overflow: hidden;
    }
    
    /* Animation for notifications */
    @keyframes slideIn {
        from {
            opacity: 0;
            transform: translateX(100%);
        }
        to {
            opacity: 1;
            transform: translateX(0);
        }
    }
    
    .notification-toast {
        animation: slideIn 0.3s ease-out;
        transition: all 0.3s ease;
    }
</style>
</body>
</html>