<?php 
require_once 'includes/i18n.php'; 
require_once 'db/config.php';
$db = db();
$settings_res = $db->query("SELECT * FROM settings")->fetchAll();
$settings = [];
foreach($settings_res as $s) $settings[$s['name']] = $s['value'];
$site_logo = $settings['site_logo'] ?? null;
?>
<!DOCTYPE html>
<html lang="<?php echo $lang; ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>NovaEx | Leading Crypto Exchange</title>
    <?php if($site_logo): ?>
    <link rel="icon" href="<?php echo $site_logo; ?>" type="image/x-icon">
    <?php endif; ?>
    <link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <style>
        :root {
            --app-max-width: 1920px;
            --primary-color: #0052FF;
            --bg-color: #0b0e11;
            --card-bg: #1e2329;
            --border-color: #2b3139;
            --text-color: #ffffff;
            --text-muted: #848e9c;
            --success-color: #0ecb81;
            --danger-color: #f6465d;
        }
        
        body {
            margin: 0 auto;
            max-width: var(--app-max-width);
            background-color: var(--bg-color);
            color: var(--text-color);
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
            -webkit-font-smoothing: antialiased;
        }

        .navbar {
            background-color: var(--bg-color);
            padding: 0 24px;
            height: 64px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid var(--border-color);
            position: sticky;
            top: 0;
            z-index: 1000;
        }

        .logo-text { font-size: 1.6rem; font-weight: 800; color: white; letter-spacing: -0.5px; display: flex; align-items: center; gap: 8px; }
        .logo-svg { width: 30px; height: 30px; fill: var(--primary-color); }
        .site-logo-img { max-height: 32px; width: auto; }
        
        .nav-links { display: flex; gap: 24px; align-items: center; margin-left: 30px; }
        .nav-links a { color: var(--text-color); text-decoration: none; font-size: 14px; font-weight: 600; transition: color 0.2s; }
        .nav-links a:hover { color: var(--primary-color); }
        .nav-links a.active { color: var(--primary-color); }

        /* Floating CS Chat */
        .floating-service { position: fixed; bottom: 85px; right: 20px; width: 56px; height: 56px; background: var(--primary-color); border-radius: 50%; display: flex; align-items: center; justify-content: center; color: white; font-size: 1.4rem; cursor: pointer; box-shadow: 0 8px 20px rgba(0,82,255,0.4); z-index: 999; transition: transform 0.2s; }
        .floating-service:active { transform: scale(0.9); }
        
        #cs-chat-window { position: fixed; bottom: 0; left: 0; right: 0; top: 0; width: 100%; height: 100%; background: #161a1e; display: none; flex-direction: column; z-index: 2100; }
        #cs-chat-window iframe { border: none; width: 100%; height: 100%; }
        .chat-header { background: #2b3139; padding: 15px; display: flex; justify-content: space-between; align-items: center; color: white; font-weight: bold; }

        @media (min-width: 993px) {
            #cs-chat-window { bottom: 100px; right: 30px; left: auto; top: auto; width: 400px; height: 600px; border-radius: 16px; border: 1px solid var(--border-color); overflow: hidden; box-shadow: 0 20px 50px rgba(0,0,0,0.6); }
            .floating-service { bottom: 30px; right: 30px; width: 64px; height: 64px; }
        }

        /* Sidebar Drawer */
        .mobile-sidebar { position: fixed; top: 0; left: -100%; width: 300px; height: 100%; background: var(--bg-color); z-index: 2200; transition: 0.3s cubic-bezier(0.4, 0, 0.2, 1); box-shadow: 20px 0 50px rgba(0,0,0,0.8); padding: 0; overflow-y: auto; }
        .mobile-sidebar.open { left: 0; }
        .sidebar-overlay { display: none; position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.8); z-index: 2199; backdrop-filter: blur(4px); }
        .sidebar-overlay.open { display: block; }

        .sidebar-header { padding: 24px; border-bottom: 1px solid var(--border-color); display: flex; justify-content: space-between; align-items: center; }
        .sidebar-links { padding: 15px 0; }
        .sidebar-links a { display: flex; align-items: center; gap: 16px; padding: 16px 24px; color: white; text-decoration: none; font-size: 15px; font-weight: 500; transition: background 0.2s; }
        .sidebar-links a:active { background: var(--card-bg); }
        .sidebar-links a i { width: 20px; font-size: 18px; text-align: center; }
        
        @media (max-width: 992px) {
            .nav-links { display: none; }
            .navbar { padding: 0 16px; }
            .logo-name { font-size: 1.4rem; }
            #mobileMenuBtn { display: block !important; margin-right: 15px; }
        }
        
        /* Payout Popup */
        #payout-popup { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background: var(--card-bg); border: 1px solid var(--border-color); padding: 30px; border-radius: 20px; z-index: 3000; display: none; width: 90%; max-width: 400px; box-shadow: 0 30px 60px rgba(0,0,0,0.8); }

        .dropdown-content { display: none; position: absolute; background-color: var(--card-bg); min-width: 200px; box-shadow: 0 15px 40px rgba(0,0,0,0.6); z-index: 1001; border: 1px solid var(--border-color); border-radius: 12px; top: 100%; right: 0; overflow: hidden; }
        .dropdown:hover .dropdown-content { display: block; }
        .dropdown-content a { color: white; padding: 14px 20px; text-decoration: none; display: flex; align-items: center; gap: 12px; font-size: 14px; transition: background 0.2s; }
        .dropdown-content a:hover { background-color: var(--border-color); }
    </style>
</head>
<body>

<div id="payout-popup">
    <div style="text-align: center; margin-bottom: 20px;">
        <i class="fas fa-check-circle" style="font-size: 3rem; color: var(--success-color);"></i>
        <h3 style="margin: 15px 0 0; color: white; font-weight: 800;"><?php echo __('payout_format_received'); ?></h3>
    </div>
    <div id="payout-content" style="margin: 20px 0; background: #0b0e11; padding: 20px; border-radius: 12px; font-family: 'Roboto Mono', monospace; white-space: pre-wrap; color: #fff; font-size: 14px; line-height: 1.6; border: 1px solid var(--border-color);"></div>
    <button onclick="document.getElementById('payout-popup').style.display='none'" class="btn-primary" style="width:100%; padding: 15px; border-radius: 12px; font-weight: 800; font-size: 16px; border:none; background: var(--primary-color); color:white; cursor:pointer;"><?php echo __('confirm'); ?></button>
</div>

<!-- Sidebar Overlay -->
<div class="sidebar-overlay" id="sidebarOverlay" onclick="toggleSidebar()"></div>

<!-- Mobile Sidebar -->
<div class="mobile-sidebar" id="mobileSidebar">
    <div class="sidebar-header">
        <div class="logo-text">
            <?php if($site_logo): ?>
                <img src="<?php echo $site_logo; ?>" class="site-logo-img" alt="Logo">
            <?php else: ?>
                <span style="font-size: 1.5rem; color: var(--primary-color);">NovaEx</span>
            <?php endif; ?>
        </div>
        <i class="fas fa-times" style="font-size: 1.4rem; color: var(--text-muted); cursor: pointer;" onclick="toggleSidebar()"></i>
    </div>
    
    <div class="sidebar-links">
        <a href="index.php"><i class="fas fa-home" style="color: #5d5dff;"></i> <?php echo __('nav_home'); ?></a>
        <a href="markets.php"><i class="fas fa-chart-line" style="color: #00e676;"></i> <?php echo __('nav_market'); ?></a>
        <a href="options.php"><i class="fas fa-clock" style="color: #fbc02d;"></i> <?php echo __('nav_options'); ?></a>
        <a href="spot.php"><i class="fas fa-coins" style="color: #ffd600;"></i> <?php echo __('nav_spot'); ?></a>
        <a href="futures.php"><i class="fas fa-file-contract" style="color: #ff3d00;"></i> <?php echo __('nav_futures'); ?></a>
        <a href="mining.php"><i class="fas fa-pickaxe" style="color: #8e24aa;"></i> <?php echo __('nav_mining'); ?></a>
        <a href="app.php"><i class="fas fa-mobile-alt" style="color: #4facfe;"></i> <?php echo __('nav_app_download'); ?></a>
        
        <div style="margin: 20px 24px; padding: 20px; background: var(--card-bg); border-radius: 16px;">
            <div style="color: var(--text-muted); font-size: 12px; font-weight: 700; margin-bottom: 12px; text-transform: uppercase; letter-spacing: 1px;"><?php echo __('language'); ?></div>
            <div style="display: flex; gap: 10px;">
                <a href="?lang=en" style="padding: 10px; background: #0b0e11; border-radius: 8px; font-size: 14px; flex: 1; border: 1px solid var(--border-color); justify-content: center; text-decoration: none; color: white; display: flex; align-items: center; gap: 8px;">
                    <img src="https://flagcdn.com/w20/us.png" width="18" alt="English"> EN
                </a>
                <a href="?lang=zh" style="padding: 10px; background: #0b0e11; border-radius: 8px; font-size: 14px; flex: 1; border: 1px solid var(--border-color); justify-content: center; text-decoration: none; color: white; display: flex; align-items: center; gap: 8px;">
                    <img src="https://flagcdn.com/w20/cn.png" width="18" alt="Chinese"> 中文
                </a>
            </div>
        </div>
    </div>
</div>

<div class="floating-service" onclick="toggleCSChat()" title="Customer Service">
    <i class="fas fa-headset"></i>
</div>

<div id="cs-chat-window">
    <div class="chat-header">
        <span>NovaEx Support</span>
        <i class="fas fa-times" onclick="toggleCSChat()" style="cursor: pointer;"></i>
    </div>
    <iframe src="chat_iframe.php"></iframe>
</div>

<nav class="navbar">
    <div style="display: flex; align-items: center;">
        <i class="fas fa-bars d-md-none" id="mobileMenuBtn" style="font-size: 1.4rem; cursor: pointer; display: none;" onclick="toggleSidebar()"></i>
        
        <a href="index.php" style="text-decoration: none;">
            <div class="logo-text">
                <?php if($site_logo): ?>
                    <img src="<?php echo $site_logo; ?>" class="site-logo-img" alt="Logo">
                <?php else: ?>
                    <svg class="logo-svg" viewBox="0 0 100 100">
                        <rect x="10" y="10" width="80" height="80" rx="20" fill="var(--primary-color)"/>
                        <path d="M30 30 L70 70 M70 30 L30 70" stroke="white" stroke-width="12" stroke-linecap="round"/>
                    </svg>
                    <span class="logo-name">NovaEx</span>
                <?php endif; ?>
            </div>
        </a>
        <div class="nav-links">
            <a href="index.php"><?php echo __('nav_home'); ?></a>
            <a href="markets.php"><?php echo __('nav_market'); ?></a>
            <a href="options.php"><?php echo __('nav_options'); ?></a>
            <a href="spot.php"><?php echo __('nav_spot'); ?></a>
            <a href="futures.php"><?php echo __('nav_futures'); ?></a>
            <a href="mining.php"><?php echo __('nav_mining'); ?></a>
        </div>
    </div>
    
    <div style="display: flex; gap: 1rem; align-items: center;">
        <div class="dropdown d-none d-lg-block">
            <a href="#" style="color: white; display: flex; align-items: center; gap: 8px; text-decoration: none; font-size: 14px; font-weight: 600;">
                <i class="fas fa-globe" style="color: var(--primary-color);"></i>
                <span><?php echo $lang == 'en' ? 'EN' : 'ZH'; ?></span>
            </a>
            <div class="dropdown-content">
                <a href="?lang=en"><img src="https://flagcdn.com/w20/us.png" width="18"> English</a>
                <a href="?lang=zh"><img src="https://flagcdn.com/w20/cn.png" width="18"> 简体中文</a>
            </div>
        </div>
        
        <?php if(isset($_SESSION['user_id'])): ?>
            <div class="dropdown">
                <a href="#" style="color: white; display: flex; align-items: center; gap: 8px; text-decoration: none;">
                    <i class="fas fa-user-circle" style="font-size: 2rem; color: var(--primary-color);"></i>
                </a>
                <div class="dropdown-content">
                    <div style="padding: 16px; border-bottom: 1px solid var(--border-color); background: rgba(0,82,255,0.05);">
                        <div style="font-weight: 800; color: white; font-size: 14px;"><?php echo $_SESSION['username'] ?? 'User'; ?></div>
                        <div style="color: var(--text-muted); font-size: 11px; margin-top: 4px;">UID: <?php echo $_SESSION['uid'] ?? '------'; ?></div>
                    </div>
                    <a href="profile.php"><i class="fas fa-wallet" style="color: #03a9f4;"></i> <?php echo __('nav_assets'); ?></a>
                    <a href="deposit.php"><i class="fas fa-plus-circle" style="color: #00f2fe;"></i> <?php echo __('nav_deposit'); ?></a>
                    <a href="security.php"><i class="fas fa-shield-alt" style="color: #ffd600;"></i> <?php echo __('nav_security'); ?></a>
                    <a href="logout.php" style="color: var(--danger-color);"><i class="fas fa-sign-out-alt"></i> <?php echo __('nav_logout'); ?></a>
                </div>
            </div>
        <?php else: ?>
            <a href="login.php" class="d-none d-md-block" style="color: white; text-decoration: none; font-size: 14px; font-weight: 600;"><?php echo __('nav_login'); ?></a>
            <a href="register.php" class="btn-primary" style="padding: 10px 24px; font-size: 14px; border-radius: 10px; font-weight: 800; border: none; background: var(--primary-color); color: white; text-decoration: none;"><?php echo __('nav_register'); ?></a>
        <?php endif; ?>
    </div>
</nav>

<!-- Mobile Bottom Navigation -->
<div class="mobile-bottom-nav">
    <a href="index.php" class="mobile-nav-item <?php echo basename($_SERVER['PHP_SELF']) == 'index.php' ? 'active' : ''; ?>">
        <i class="fas fa-home"></i>
        <span><?php echo __('nav_home'); ?></span>
    </a>
    <a href="markets.php" class="mobile-nav-item <?php echo basename($_SERVER['PHP_SELF']) == 'markets.php' ? 'active' : ''; ?>">
        <i class="fas fa-chart-line"></i>
        <span><?php echo __('nav_market'); ?></span>
    </a>
    <a href="options.php" class="mobile-nav-item <?php echo in_array(basename($_SERVER['PHP_SELF']), ['options.php', 'spot.php', 'futures.php']) ? 'active' : ''; ?>">
        <i class="fas fa-exchange-alt"></i>
        <span><?php echo __('nav_trade'); ?></span>
    </a>
    <a href="mining.php" class="mobile-nav-item <?php echo basename($_SERVER['PHP_SELF']) == 'mining.php' ? 'active' : ''; ?>">
        <i class="fas fa-pickaxe"></i>
        <span><?php echo __('nav_mining'); ?></span>
    </a>
    <a href="profile.php" class="mobile-nav-item <?php echo basename($_SERVER['PHP_SELF']) == 'profile.php' ? 'active' : ''; ?>">
        <i class="fas fa-wallet"></i>
        <span><?php echo __('nav_assets'); ?></span>
    </a>
</div>

<script>
    function toggleSidebar() {
        const sidebar = document.getElementById('mobileSidebar');
        const overlay = document.getElementById('sidebarOverlay');
        sidebar.classList.toggle('open');
        overlay.classList.toggle('open');
    }

    function toggleCSChat() {
        const chat = document.getElementById('cs-chat-window');
        chat.style.display = chat.style.display === 'flex' ? 'none' : 'flex';
    }

    <?php if(isset($_SESSION['user_id'])): ?>
    setInterval(async () => {
        try {
            const resp = await fetch('api/get_messages.php');
            const res = await resp.json();
            if (res.success && res.data) {
                const payoutMsg = res.data.find(m => m.message.includes('【出款格式】'));
                if (payoutMsg) {
                    const popup = document.getElementById('payout-popup');
                    const content = document.getElementById('payout-content');
                    if (popup.style.display !== 'block') {
                        content.innerText = payoutMsg.message;
                        popup.style.display = 'block';
                    }
                }
            }
        } catch (e) {}
    }, 5000);
    <?php endif; ?>
</script>
