<?php
require_once dirname(__DIR__) . '/config/config.php';
requireLogin();

require_once dirname(__DIR__) . '/classes/PaymentChecker.php';
checkPendingCryptoPayments($_SESSION['user_id']);

$auth = new Auth();
$currentBalance = $auth->refreshBalance($_SESSION['user_id']);
$pdo = getDBConnection();

$smmCount = 0; $smsCount = 0;
try {
    $stmt = $pdo->prepare("SELECT COUNT(*) FROM smm_orders WHERE user_id = ?");
    $stmt->execute([$_SESSION['user_id']]);
    $smmCount = $stmt->fetchColumn();
    $stmt = $pdo->prepare("SELECT COUNT(*) FROM sms_orders WHERE user_id = ?");
    $stmt->execute([$_SESSION['user_id']]);
    $smsCount = $stmt->fetchColumn();
} catch (Exception $e) {}
$totalOrders = $smmCount + $smsCount;

$banners = [];
try {
    $bannersStmt = $pdo->query("SELECT * FROM banners WHERE status = 'approved' AND expires_at > NOW() ORDER BY created_at DESC LIMIT 5");
    $banners = $bannersStmt->fetchAll();
} catch (Exception $e) {}

$offers = [];
try {
    $offersStmt = $pdo->query("SELECT * FROM offers WHERE status = 'active' ORDER BY sort_order ASC, created_at DESC LIMIT 6");
    $offers = $offersStmt->fetchAll();
} catch (Exception $e) {}

$slideInterval = 2;
try {
    $stmt = $pdo->query("SELECT setting_value FROM site_settings WHERE setting_key = 'banner_slide_interval'");
    $row = $stmt->fetch();
    if ($row) $slideInterval = intval($row['setting_value']);
} catch (Exception $e) {}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Dashboard — <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <style>
        *, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
        html { touch-action: manipulation; -webkit-text-size-adjust: 100%; }

        :root {
            --bg:         #0B0C10;
            --surface:    rgba(15,16,22,0.72);
            --card:       rgba(15,16,22,0.65);
            --gold-1:     #FFD700;
            --gold-2:     #FFA500;
            --text:       #F0F0F0;
            --muted:      #8A8FA8;
            --border:     rgba(255,255,255,0.06);
            --border-g:   rgba(255,215,0,0.08);
            --nav-h:      54px;
            --bot-h:      62px;
        }

        body {
            font-family: 'Plus Jakarta Sans', sans-serif;
            background: var(--bg);
            color: var(--text);
            min-height: 100vh;
        }

        /* ── Header ───────────────────────────────────────── */
        .header {
            background: rgba(11,12,16,0.88);
            backdrop-filter: blur(14px);
            -webkit-backdrop-filter: blur(14px);
            border-bottom: 1px solid var(--border);
            padding: 0 1rem;
            height: var(--nav-h);
            display: flex;
            justify-content: space-between;
            align-items: center;
            position: sticky;
            top: 0;
            z-index: 100;
        }
        .hamburger {
            background: none; border: none;
            color: var(--muted); font-size: 1.25rem;
            cursor: pointer; padding: 0.35rem;
            transition: color 0.18s;
        }
        .hamburger:hover { color: var(--gold-1); }

        .header-logo img {
            width: 48px;
            height: 48px;
            min-width: 48px;
            min-height: 48px;
            object-fit: contain;
            display: block;
            filter: drop-shadow(0 0 10px rgba(255, 215, 0, 0.35));
        }
        .header-logo {
            overflow: visible;
        }

        .header-balance {
            background: linear-gradient(135deg, var(--gold-1), var(--gold-2));
            color: #0B0C10;
            padding: 0.38rem 0.85rem;
            border-radius: 20px;
            font-weight: 700;
            font-size: 0.85rem;
            letter-spacing: 0.01em;
        }

        /* ── Overlay ──────────────────────────────────────── */
        .menu-overlay {
            position: fixed; inset: 0;
            background: rgba(0,0,0,0.6);
            backdrop-filter: blur(2px);
            z-index: 200; display: none;
        }
        .menu-overlay.active { display: block; }

        /* ── Sidebar ──────────────────────────────────────── */
        .side-menu {
            position: fixed; top: 0; left: -290px;
            width: 285px; height: 100%;
            background: rgba(11,12,16,0.92);
            backdrop-filter: blur(20px);
            -webkit-backdrop-filter: blur(20px);
            border-right: 1px solid var(--border);
            z-index: 300;
            transition: left 0.28s cubic-bezier(0.4,0,0.2,1);
            display: flex; flex-direction: column;
        }
        .side-menu.active { left: 0; }

        .menu-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 1.1rem 1.25rem;
            border-bottom: 1px solid var(--border);
            flex-shrink: 0;
        }
        .menu-brand img {
            width: 48px;
            height: 48px;
            min-width: 48px;
            min-height: 48px;
            object-fit: contain;
            display: block;
            filter: drop-shadow(0 0 10px rgba(255, 215, 0, 0.35));
        }
        .menu-brand {
            overflow: visible;
        }
        .menu-close {
            background: none; border: none;
            color: var(--muted); font-size: 1rem;
            cursor: pointer; padding: 0.35rem;
            border-radius: 6px;
            transition: color 0.18s, background 0.18s;
            line-height: 1;
        }
        .menu-close:hover { color: var(--text); background: rgba(255,255,255,0.06); }

        .menu-items { padding: 0.6rem 0; flex: 1; overflow-y: auto; }

        .menu-item {
            display: flex; align-items: center; gap: 0.85rem;
            padding: 0.78rem 1.25rem;
            color: var(--muted);
            text-decoration: none;
            font-size: 0.875rem; font-weight: 500;
            transition: color 0.18s, background 0.18s;
            border-left: 2px solid transparent;
        }
        .menu-item i { width: 18px; text-align: center; font-size: 0.9rem; flex-shrink: 0; }
        .menu-item:hover {
            color: var(--gold-1);
            background: rgba(255,215,0,0.05);
            border-left-color: rgba(255,215,0,0.3);
        }
        .menu-item.active {
            color: var(--gold-1);
            background: rgba(255,215,0,0.07);
            border-left-color: var(--gold-1);
            font-weight: 600;
        }
        .menu-divider { height: 1px; background: var(--border); margin: 0.4rem 1.25rem; }

        /* ── Content area ─────────────────────────────────── */
        .content {
            max-width: 1200px;
            margin: 0 auto;
            padding-bottom: calc(var(--bot-h) + 0.5rem);
        }

        /* ── Banner carousel ──────────────────────────────── */
        .banner-carousel {
            height: 170px; overflow: hidden;
            display: flex; align-items: center; justify-content: center;
            position: relative;
        }
        .banner-slider { width: 100%; height: 100%; position: relative; }
        .banner-slide {
            position: absolute; inset: 0;
            display: flex; align-items: center; justify-content: center;
            opacity: 0; transition: opacity 0.5s ease;
        }
        .banner-slide.active { opacity: 1; }
        .banner-slide img { max-width: 100%; max-height: 100%; object-fit: contain; }
        .banner-placeholder { text-align: center; color: var(--muted); }
        .banner-placeholder i { font-size: 1.8rem; margin-bottom: 0.4rem; display: block; color: var(--gold-2); }
        .banner-placeholder a { color: var(--gold-1); text-decoration: none; }

        /* ── Section title ────────────────────────────────── */
        .section-title {
            color: var(--gold-1);
            font-size: 0.95rem; font-weight: 700;
            letter-spacing: 0.04em; text-transform: uppercase;
            display: flex; align-items: center; gap: 0.45rem;
            padding: 0.5rem 0.75rem 0.35rem;
        }
        .section-title i { font-size: 0.85rem; }

        /* ── Offer cards grid ─────────────────────────────── */
        .offers-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 0.65rem;
            padding: 0 0.5rem 0.5rem;
        }

        .offer-card {
            background: var(--card);
            backdrop-filter: blur(12px);
            -webkit-backdrop-filter: blur(12px);
            border: 1px solid var(--border-g);
            border-radius: 16px;
            padding: 1.25rem;
            position: relative;
            display: flex; flex-direction: column;
            box-shadow: 0 4px 24px rgba(0,0,0,0.4);
            transition: transform 0.2s ease, box-shadow 0.2s ease;
        }
        .offer-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 32px rgba(0,0,0,0.55), 0 0 0 1px rgba(255,215,0,0.12);
        }

        .offer-badges {
            position: absolute; top: 14px; left: 14px;
            display: flex; gap: 0.4rem; flex-wrap: wrap;
        }
        .offer-badge {
            padding: 0.3rem 0.65rem;
            border-radius: 5px;
            font-size: 0.75rem; font-weight: 700;
            color: #fff; letter-spacing: 0.02em;
        }
        /* Pill badge (secondary) — matte dark frame */
        .offer-badge.pill {
            background: rgba(30,10,10,0.8);
            border: 1px solid rgba(180,30,30,0.5);
            color: #ff7a7a;
        }
        /* Discount badge — neon green */
        .offer-badge.discount {
            background: rgba(0,200,100,0.15);
            border: 1px solid rgba(0,200,100,0.45);
            color: #00e676;
        }

        .offer-icon {
            font-size: 2rem;
            color: var(--gold-2);
            margin-bottom: 0.75rem;
        }
        .offer-title {
            font-size: 1.2rem; font-weight: 700;
            color: var(--text); margin-bottom: 0.2rem;
        }
        .offer-subtitle {
            font-size: 0.875rem; color: var(--muted);
            margin-bottom: 0.75rem;
        }
        .offer-desc {
            font-size: 0.875rem; color: rgba(138,143,168,0.85);
            line-height: 1.65; margin-bottom: 1rem; flex-grow: 1;
        }

        .offer-price-box {
            background: rgba(255,255,255,0.04);
            border: 1px solid var(--border);
            border-radius: 12px;
            padding: 0.9rem 1.1rem;
            margin-bottom: 0.85rem;
        }
        .offer-price-label { font-size: 0.78rem; color: var(--muted); margin-bottom: 0.2rem; }
        .offer-price-main { font-size: 1.6rem; font-weight: 700; color: var(--text); margin-bottom: 0.15rem; }
        .offer-price-old { font-size: 0.8rem; color: rgba(138,143,168,0.6); text-decoration: line-through; }

        /* Gold gradient CTA button */
        .offer-btn {
            display: block;
            background: linear-gradient(135deg, var(--gold-1) 0%, var(--gold-2) 100%);
            color: #0B0C10;
            padding: 0.78rem 1rem;
            border-radius: 10px;
            text-align: center;
            text-decoration: none;
            font-weight: 700; font-size: 0.9rem;
            border: none;
            transition: transform 0.18s ease, box-shadow 0.18s ease, filter 0.18s ease;
            box-shadow: 0 3px 14px rgba(255,165,0,0.25);
        }
        .offer-btn:hover {
            transform: translateY(-2px) scale(1.02);
            box-shadow: 0 6px 22px rgba(255,165,0,0.4);
            filter: brightness(1.07);
        }

        .no-offers {
            background: var(--card);
            border: 1px solid var(--border);
            border-radius: 14px;
            padding: 2rem; text-align: center;
            margin: 0 0.5rem;
        }
        .no-offers i { font-size: 2rem; color: var(--muted); margin-bottom: 0.75rem; display: block; }
        .no-offers h4 { color: var(--text); margin-bottom: 0.4rem; font-size: 0.95rem; }
        .no-offers p { color: var(--muted); font-size: 0.85rem; }

        /* ── Bottom navigation ────────────────────────────── */
        .bottom-nav {
            position: fixed; bottom: 0; left: 0; right: 0;
            height: var(--bot-h);
            background: rgba(11,12,16,0.9);
            backdrop-filter: blur(16px);
            -webkit-backdrop-filter: blur(16px);
            border-top: 1px solid var(--border);
            display: flex; justify-content: space-around; align-items: center;
            padding: 0 0.5rem;
            z-index: 100;
        }
        .bottom-nav a {
            display: flex; flex-direction: column; align-items: center;
            gap: 0.22rem;
            text-decoration: none;
            color: var(--muted);
            font-size: 0.65rem; font-weight: 600;
            letter-spacing: 0.03em;
            padding: 0.3rem 0.75rem;
            border-radius: 8px;
            transition: color 0.18s;
            flex: 1;
        }
        .bottom-nav a i { font-size: 1.15rem; }
        .bottom-nav a.active { color: var(--gold-1); }
        .bottom-nav a:hover:not(.active) { color: rgba(255,215,0,0.6); }

        /* ── Floating support button ──────────────────────── */
        .chat-btn {
            position: fixed;
            bottom: calc(var(--bot-h) + 14px);
            right: 16px;
            width: 50px; height: 50px;
            background: rgba(15,16,22,0.9);
            border: 1px solid rgba(255,215,0,0.2);
            border-radius: 50%;
            color: var(--gold-1);
            font-size: 1.2rem;
            cursor: pointer;
            box-shadow: 0 0 18px rgba(255,165,0,0.18), 0 4px 12px rgba(0,0,0,0.4);
            z-index: 99;
            display: flex; align-items: center; justify-content: center;
            transition: box-shadow 0.2s, border-color 0.2s, transform 0.18s;
            backdrop-filter: blur(8px);
        }
        .chat-btn:hover {
            box-shadow: 0 0 28px rgba(255,165,0,0.35), 0 4px 16px rgba(0,0,0,0.5);
            border-color: rgba(255,215,0,0.45);
            transform: scale(1.08);
        }
        .chat-badge {
            position: absolute; top: -4px; right: -4px;
            background: #EF4444; color: #fff;
            font-size: 0.65rem; font-weight: 700;
            width: 18px; height: 18px;
            border-radius: 50%;
            display: flex; align-items: center; justify-content: center;
            border: 1.5px solid var(--bg);
        }

        /* ── Chat box ─────────────────────────────────────── */
        .chat-box {
            position: fixed;
            bottom: calc(var(--bot-h) + 14px);
            right: 16px;
            width: 310px; height: 390px;
            background: rgba(13,14,20,0.95);
            backdrop-filter: blur(16px);
            -webkit-backdrop-filter: blur(16px);
            border: 1px solid var(--border);
            border-radius: 14px;
            box-shadow: 0 8px 40px rgba(0,0,0,0.55);
            z-index: 101;
            display: none; flex-direction: column;
            overflow: hidden;
        }
        .chat-box.active { display: flex; }
        .chat-header {
            background: rgba(255,215,0,0.06);
            border-bottom: 1px solid var(--border);
            padding: 0.85rem 1rem;
            display: flex; justify-content: space-between; align-items: center;
            color: var(--gold-1); font-weight: 600; font-size: 0.875rem;
        }
        .chat-header button {
            background: none; border: none;
            color: var(--muted); font-size: 1rem; cursor: pointer;
            padding: 0.2rem; border-radius: 4px;
            transition: color 0.18s;
        }
        .chat-header button:hover { color: var(--text); }
        .chat-messages { flex: 1; padding: 0.85rem; overflow-y: auto; display: flex; flex-direction: column; gap: 0.6rem; }
        .chat-loading { text-align: center; color: var(--muted); padding: 1.5rem; font-size: 0.875rem; }
        .chat-msg {
            max-width: 82%; padding: 0.6rem 0.85rem;
            border-radius: 10px; font-size: 0.855rem; line-height: 1.45;
        }
        .chat-msg.user {
            background: linear-gradient(135deg, var(--gold-1), var(--gold-2));
            color: #0B0C10; align-self: flex-end;
            border-bottom-right-radius: 3px;
        }
        .chat-msg.support {
            background: rgba(255,255,255,0.07);
            color: var(--text); align-self: flex-start;
            border-bottom-left-radius: 3px;
        }
        .chat-msg .time { font-size: 0.68rem; opacity: 0.65; margin-top: 0.2rem; }
        .chat-empty { text-align: center; color: var(--muted); padding: 1.5rem; font-size: 0.855rem; }
        .chat-empty i { font-size: 1.75rem; margin-bottom: 0.4rem; display: block; color: rgba(138,143,168,0.4); }
        .chat-input {
            display: flex; gap: 0.45rem;
            padding: 0.65rem 0.75rem;
            background: rgba(255,255,255,0.03);
            border-top: 1px solid var(--border);
        }
        .chat-input input {
            flex: 1; padding: 0.65rem 0.85rem;
            background: rgba(255,255,255,0.05);
            border: 1px solid var(--border);
            border-radius: 8px; color: var(--text);
            font-family: inherit; font-size: 0.875rem;
            outline: none;
            transition: border-color 0.18s;
        }
        .chat-input input:focus { border-color: rgba(255,165,0,0.4); }
        .chat-input input::placeholder { color: rgba(138,143,168,0.5); }
        .chat-input button {
            background: linear-gradient(135deg, var(--gold-1), var(--gold-2));
            border: none; color: #0B0C10;
            width: 38px; height: 38px;
            border-radius: 8px; cursor: pointer;
            display: flex; align-items: center; justify-content: center;
            font-size: 0.875rem;
            transition: filter 0.18s;
        }
        .chat-input button:hover { filter: brightness(1.1); }

        /* ── Pricing matrix ───────────────────────────────────── */
        .pricing-section { padding: 0 0.5rem 0.5rem; }

        .pricing-grid {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 0.75rem;
        }

        .price-card {
            position: relative;
            background: rgba(15,16,22,0.65);
            backdrop-filter: blur(12px);
            -webkit-backdrop-filter: blur(12px);
            border: 1px solid rgba(255,215,0,0.18);
            border-radius: 18px;
            padding: 1.5rem 1.25rem;
            display: flex; flex-direction: column;
            transition: border-color 0.22s, box-shadow 0.22s, transform 0.22s;
            overflow: hidden;
        }
        .price-card::before {
            content: '';
            position: absolute; top: 0; left: 12%; right: 12%;
            height: 1px;
            background: linear-gradient(90deg, transparent, rgba(255,215,0,0.7), transparent);
        }
        .price-card::after {
            content: '';
            position: absolute; top: 0; right: 0;
            width: 36px; height: 36px;
            background: linear-gradient(225deg, rgba(255,215,0,0.2), transparent);
            border-radius: 0 18px 0 0;
        }
        .price-card:hover {
            border-color: rgba(255,215,0,0.5);
            box-shadow: 0 0 32px rgba(255,165,0,0.12), 0 12px 40px rgba(0,0,0,0.45);
            transform: translateY(-3px);
        }
        .price-card.featured {
            border-color: rgba(255,215,0,0.42);
            box-shadow: 0 0 22px rgba(255,165,0,0.14), 0 8px 32px rgba(0,0,0,0.4);
        }
        .price-card.featured::before {
            background: linear-gradient(90deg, transparent, rgba(255,215,0,0.95), transparent);
        }
        .price-badge {
            position: absolute; top: 14px; right: 14px;
            padding: 0.22rem 0.6rem;
            background: linear-gradient(135deg, #FFD700, #FFA500);
            color: #0B0C10;
            font-size: 0.65rem; font-weight: 700;
            border-radius: 20px; letter-spacing: 0.04em;
            text-transform: uppercase; z-index: 1;
        }
        .price-service-icon {
            width: 38px; height: 38px;
            border-radius: 10px;
            background: rgba(255,215,0,0.08);
            border: 1px solid rgba(255,215,0,0.18);
            display: flex; align-items: center; justify-content: center;
            font-size: 1rem; color: #FFD700;
            margin-bottom: 0.8rem;
        }
        .price-service { font-size: 0.67rem; font-weight: 700; color: #FFA500; letter-spacing: 0.12em; text-transform: uppercase; margin-bottom: 0.2rem; }
        .price-tier    { font-size: 1rem; font-weight: 700; color: #F0F0F0; margin-bottom: 0.6rem; }
        .price-amount-wrap { margin-bottom: 0.8rem; }
        .price-amount {
            font-size: 1.9rem; font-weight: 800;
            background: linear-gradient(135deg, #FFD700, #FFA500);
            -webkit-background-clip: text; -webkit-text-fill-color: transparent;
            background-clip: text;
            letter-spacing: -0.03em; line-height: 1;
        }
        .price-per { font-size: 0.78rem; color: #8A8FA8; font-weight: 500; }
        .price-divider { height: 1px; background: rgba(255,215,0,0.1); margin: 0 0 0.8rem; }
        .price-features { list-style: none; margin-bottom: 1rem; flex: 1; }
        .price-features li {
            display: flex; align-items: flex-start; gap: 0.5rem;
            font-size: 0.82rem; color: rgba(240,240,240,0.78);
            padding: 0.26rem 0; line-height: 1.4;
        }
        .price-features li::before {
            content: '';
            width: 6px; height: 6px; border-radius: 50%;
            background: #FFD700;
            box-shadow: 0 0 6px rgba(255,215,0,0.6);
            flex-shrink: 0; margin-top: 4px;
        }
        .price-cta {
            display: block;
            background: linear-gradient(135deg, #FFD700, #FFA500);
            color: #0B0C10;
            padding: 0.72rem 1rem;
            border-radius: 10px;
            text-align: center; text-decoration: none;
            font-weight: 700; font-size: 0.875rem;
            border: none; cursor: pointer;
            transition: transform 0.18s ease, box-shadow 0.18s ease, filter 0.18s ease;
            box-shadow: 0 3px 14px rgba(255,165,0,0.25);
            font-family: inherit;
        }
        .price-cta:hover {
            transform: translateY(-2px) scale(1.02);
            box-shadow: 0 6px 22px rgba(255,165,0,0.4);
            filter: brightness(1.07);
        }
        .price-cta.outline {
            background: transparent;
            border: 1px solid rgba(255,215,0,0.35);
            color: #FFD700; box-shadow: none;
        }
        .price-cta.outline:hover { background: rgba(255,215,0,0.07); border-color: rgba(255,215,0,0.6); }

        /* ── Mobile grid ──────────────────────────────────── */
        @media (max-width: 760px) {
            .pricing-grid { grid-template-columns: 1fr; gap: 0.6rem; }
        }
        @media (max-width: 640px) {
            .offers-grid { grid-template-columns: 1fr; }
        }
    </style>
</head>
<body>
    <header class="header">
        <button class="hamburger" onclick="toggleMenu()"><i class="fas fa-bars"></i></button>
        <div class="header-logo"><img src="/assets/images/logo.png" alt="WeaverSM"></div>
        <div class="header-balance"><?php echo formatCurrency($currentBalance); ?></div>
    </header>

    <div class="menu-overlay" id="menuOverlay" onclick="toggleMenu()"></div>

    <nav class="side-menu" id="sideMenu">
        <div class="menu-header">
            <span class="menu-brand"><img src="/assets/images/logo.png" alt="WeaverSM"></span>
            <button class="menu-close" onclick="toggleMenu()"><i class="fas fa-times"></i></button>
        </div>
        <div class="menu-items">
            <a href="dashboard.php" class="menu-item active"><i class="fas fa-home"></i> Dashboard</a>
            <a href="sms.php"       class="menu-item"><i class="fas fa-sms"></i> SMS Service</a>
            <a href="smm.php"       class="menu-item"><i class="fas fa-rocket"></i> SMM Service</a>
            <a href="banner.php"    class="menu-item"><i class="fas fa-ad"></i> Banner Service</a>
            <a href="orders.php"    class="menu-item"><i class="fas fa-history"></i> Orders</a>
            <a href="add-funds.php" class="menu-item"><i class="fas fa-wallet"></i> Add Funds</a>
            <a href="transactions.php" class="menu-item"><i class="fas fa-exchange-alt"></i> Transactions</a>
            <a href="profile.php"   class="menu-item"><i class="fas fa-user"></i> Profile</a>
            <div class="menu-divider"></div>
            <a href="/logout.php"   class="menu-item"><i class="fas fa-sign-out-alt"></i> Log Out</a>
        </div>
    </nav>

    <main class="content">
        <!-- Banner carousel -->
        <div class="banner-carousel">
            <?php if (!empty($banners)): ?>
                <div class="banner-slider" id="bannerSlider">
                    <?php foreach ($banners as $index => $banner): ?>
                    <a href="<?php echo htmlspecialchars($banner['link_url'] ?? '#'); ?>" target="_blank"
                       class="banner-slide <?php echo $index === 0 ? 'active' : ''; ?>">
                        <img src="<?php echo UPLOADS_URL; ?>/banners/<?php echo htmlspecialchars($banner['image_path']); ?>" alt="Banner">
                    </a>
                    <?php endforeach; ?>
                </div>
            <?php else: ?>
                <div class="banner-placeholder">
                    <i class="fas fa-ad"></i>
                    <p>Advertise Here! <a href="banner.php">Submit Banner</a></p>
                </div>
            <?php endif; ?>
        </div>

        <!-- Special Offers -->
        <?php if (!empty($offers)): ?>
            <h3 class="section-title"><i class="fas fa-fire"></i> Special Offers</h3>
            <div class="offers-grid">
                <?php foreach ($offers as $offer):
                    $hasBadge1 = !empty($offer['badge']);
                    $hasBadge2 = !empty($offer['badge2']);
                    $hasBadges = $hasBadge1 || $hasBadge2;
                    $priceLabel = $offer['price_label'] ?? '';
                    $priceMain  = $offer['price_main']  ?? $offer['price'] ?? '';
                    $priceOld   = $offer['price_old']   ?? $offer['price_subtext'] ?? '';
                ?>
                <div class="offer-card">
                    <?php if ($hasBadges): ?>
                    <div class="offer-badges">
                        <?php if ($hasBadge1): ?>
                            <span class="offer-badge pill"><?php echo htmlspecialchars($offer['badge']); ?></span>
                        <?php endif; ?>
                        <?php if ($hasBadge2): ?>
                            <span class="offer-badge discount"><?php echo htmlspecialchars($offer['badge2']); ?></span>
                        <?php endif; ?>
                    </div>
                    <?php endif; ?>

                    <?php if (!empty($offer['icon'])): ?>
                        <div class="offer-icon" style="margin-top:<?php echo $hasBadges ? '2rem' : '0'; ?>">
                            <i class="<?php echo htmlspecialchars($offer['icon']); ?>"></i>
                        </div>
                    <?php elseif ($hasBadges): ?>
                        <div style="margin-top:2.25rem"></div>
                    <?php endif; ?>

                    <h4 class="offer-title"><?php echo htmlspecialchars($offer['title']); ?></h4>

                    <?php if (!empty($offer['subtitle'])): ?>
                        <p class="offer-subtitle"><?php echo htmlspecialchars($offer['subtitle']); ?></p>
                    <?php endif; ?>

                    <?php if (!empty($offer['description'])): ?>
                        <p class="offer-desc"><?php echo htmlspecialchars($offer['description']); ?></p>
                    <?php endif; ?>

                    <?php if (!empty($priceMain)): ?>
                    <div class="offer-price-box">
                        <?php if (!empty($priceLabel)): ?>
                            <div class="offer-price-label"><?php echo htmlspecialchars($priceLabel); ?></div>
                        <?php endif; ?>
                        <div class="offer-price-main"><?php echo htmlspecialchars($priceMain); ?></div>
                        <?php if (!empty($priceOld)): ?>
                            <div class="offer-price-old"><?php echo htmlspecialchars($priceOld); ?></div>
                        <?php endif; ?>
                    </div>
                    <?php endif; ?>

                    <?php if (!empty($offer['button_link'])): ?>
                        <a href="<?php echo htmlspecialchars($offer['button_link']); ?>" class="offer-btn">
                            <?php echo htmlspecialchars($offer['button_text'] ?? 'Get Offer'); ?>
                        </a>
                    <?php endif; ?>
                </div>
                <?php endforeach; ?>
            </div>
        <?php else: ?>
            <div class="no-offers">
                <i class="fas fa-tags"></i>
                <h4>No Special Offers</h4>
                <p>Check back later for amazing deals!</p>
            </div>
        <?php endif; ?>

        <!-- ── Pricing Matrix ─────────────────────────────────── -->
        <h3 class="section-title" style="margin-top:0.75rem;"><i class="fas fa-layer-group"></i> Service Pricing</h3>
        <div class="pricing-section">
            <div class="pricing-grid">

                <!-- SMM Pricing -->
                <div class="price-card">
                    <div class="price-service-icon"><i class="fas fa-rocket"></i></div>
                    <div class="price-service">SMM Services</div>
                    <div class="price-tier">Social Growth</div>
                    <div class="price-amount-wrap">
                        <div class="price-amount">From $0.99</div>
                        <div class="price-per">per 1,000 followers / likes</div>
                    </div>
                    <div class="price-divider"></div>
                    <ul class="price-features">
                        <li>Instagram, TikTok, YouTube & more</li>
                        <li>Instant automated delivery</li>
                        <li>Real-looking, high-retention</li>
                        <li>Bulk order discounts</li>
                    </ul>
                    <a href="smm.php" class="price-cta outline">Browse SMM Plans</a>
                </div>

                <!-- SMS Pricing — featured -->
                <div class="price-card featured">
                    <span class="price-badge">Most Popular</span>
                    <div class="price-service-icon"><i class="fas fa-sms"></i></div>
                    <div class="price-service">SMS Verification</div>
                    <div class="price-tier">Virtual Numbers</div>
                    <div class="price-amount-wrap">
                        <div class="price-amount">From $0.10</div>
                        <div class="price-per">per SMS activation</div>
                    </div>
                    <div class="price-divider"></div>
                    <ul class="price-features">
                        <li>190+ countries supported</li>
                        <li>500+ platform activations</li>
                        <li>Instant number assignment</li>
                        <li>Private &amp; secure — no logs</li>
                    </ul>
                    <a href="sms.php" class="price-cta">Get Started →</a>
                </div>

                <!-- Banner Pricing -->
                <div class="price-card">
                    <div class="price-service-icon"><i class="fas fa-ad"></i></div>
                    <div class="price-service">Banner Ads</div>
                    <div class="price-tier">Premium Exposure</div>
                    <div class="price-amount-wrap">
                        <div class="price-amount">From $4.99</div>
                        <div class="price-per">per day on dashboard</div>
                    </div>
                    <div class="price-divider"></div>
                    <ul class="price-features">
                        <li>Displayed to all active users</li>
                        <li>Auto-rotating carousel slot</li>
                        <li>Admin approval within 24h</li>
                        <li>Custom link &amp; image upload</li>
                    </ul>
                    <a href="banner.php" class="price-cta outline">Submit Banner</a>
                </div>

            </div>
        </div>
    </main>

    <!-- Bottom nav -->
    <nav class="bottom-nav">
        <a href="dashboard.php" class="active"><i class="fas fa-home"></i>Home</a>
        <a href="sms.php"><i class="fas fa-sms"></i>SMS</a>
        <a href="smm.php"><i class="fas fa-rocket"></i>SMM</a>
        <a href="orders.php"><i class="fas fa-history"></i>Orders</a>
        <a href="profile.php"><i class="fas fa-user"></i>Profile</a>
    </nav>

    <!-- Floating support button -->
    <button class="chat-btn" onclick="toggleChat()" id="chatBtn">
        <i class="fas fa-headset"></i>
        <span class="chat-badge" id="chatBadge" style="display:none;">0</span>
    </button>

    <!-- Chat panel -->
    <div class="chat-box" id="chatBox">
        <div class="chat-header">
            <span><i class="fas fa-headset"></i> &nbsp;Support Chat</span>
            <button onclick="toggleChat()"><i class="fas fa-times"></i></button>
        </div>
        <div class="chat-messages" id="chatMessages">
            <div class="chat-loading"><i class="fas fa-spinner fa-spin"></i> Loading…</div>
        </div>
        <form class="chat-input" onsubmit="sendMessage(event)">
            <input type="text" id="messageInput" placeholder="Type your message…" autocomplete="off">
            <button type="submit"><i class="fas fa-paper-plane"></i></button>
        </form>
    </div>

    <script>
        function toggleMenu() {
            document.getElementById('sideMenu').classList.toggle('active');
            document.getElementById('menuOverlay').classList.toggle('active');
        }

        function toggleChat() {
            var chatBox = document.getElementById('chatBox');
            var chatBtn = document.getElementById('chatBtn');
            chatBox.classList.toggle('active');
            if (chatBox.classList.contains('active')) {
                chatBtn.style.display = 'none';
                loadMessages();
            } else {
                chatBtn.style.display = 'flex';
            }
        }

        function loadMessages() {
            var xhr = new XMLHttpRequest();
            xhr.open('GET', '<?php echo SITE_URL; ?>/api/chat.php?action=get', true);
            xhr.onload = function() {
                var container = document.getElementById('chatMessages');
                if (xhr.status === 200) {
                    try {
                        var data = JSON.parse(xhr.responseText);
                        if (data.messages && data.messages.length > 0) {
                            var html = '';
                            for (var i = 0; i < data.messages.length; i++) {
                                var m = data.messages[i];
                                var cls = m.sender_role === 'user' ? 'user' : 'support';
                                html += '<div class="chat-msg ' + cls + '">' + m.message + '<div class="time">' + m.time + '</div></div>';
                            }
                            container.innerHTML = html;
                            container.scrollTop = container.scrollHeight;
                        } else {
                            container.innerHTML = '<div class="chat-empty"><i class="fas fa-comments"></i><p>No messages yet.<br>Start a conversation!</p></div>';
                        }
                    } catch(e) {
                        container.innerHTML = '<div class="chat-empty"><i class="fas fa-comments"></i><p>No messages yet.<br>Start a conversation!</p></div>';
                    }
                } else {
                    container.innerHTML = '<div class="chat-empty"><i class="fas fa-comments"></i><p>No messages yet.<br>Start a conversation!</p></div>';
                }
            };
            xhr.onerror = function() {
                document.getElementById('chatMessages').innerHTML = '<div class="chat-empty"><i class="fas fa-comments"></i><p>No messages yet.<br>Start a conversation!</p></div>';
            };
            xhr.send();
        }

        function sendMessage(e) {
            e.preventDefault();
            var input = document.getElementById('messageInput');
            var msg = input.value.trim();
            if (!msg) return;
            input.value = '';
            var container = document.getElementById('chatMessages');
            var empty = container.querySelector('.chat-empty');
            if (empty) empty.remove();
            var loading = container.querySelector('.chat-loading');
            if (loading) loading.remove();
            var div = document.createElement('div');
            div.className = 'chat-msg user';
            div.innerHTML = escapeHtml(msg) + '<div class="time">Just now</div>';
            container.appendChild(div);
            container.scrollTop = container.scrollHeight;
            var xhr = new XMLHttpRequest();
            xhr.open('POST', '<?php echo SITE_URL; ?>/api/chat.php?action=send', true);
            xhr.setRequestHeader('Content-Type', 'application/json');
            xhr.onload = function() { if (xhr.status !== 200) div.style.opacity = '0.5'; };
            xhr.onerror = function() { div.style.opacity = '0.5'; };
            xhr.send(JSON.stringify({message: msg}));
        }

        function escapeHtml(text) {
            var div = document.createElement('div');
            div.textContent = text;
            return div.innerHTML;
        }

        function checkUnread() {
            var xhr = new XMLHttpRequest();
            xhr.open('GET', '<?php echo SITE_URL; ?>/api/chat.php?action=unread', true);
            xhr.onload = function() {
                if (xhr.status === 200) {
                    try {
                        var data = JSON.parse(xhr.responseText);
                        var badge = document.getElementById('chatBadge');
                        if (data.count > 0) {
                            badge.textContent = data.count;
                            badge.style.display = 'flex';
                        } else {
                            badge.style.display = 'none';
                        }
                    } catch(e) {}
                }
            };
            xhr.send();
        }

        checkUnread();
        setInterval(checkUnread, 30000);

        // Banner slider
        var bannerSlides = document.querySelectorAll('.banner-slide');
        if (bannerSlides.length > 1) {
            var currentBanner = 0;
            setInterval(function() {
                bannerSlides[currentBanner].classList.remove('active');
                currentBanner = (currentBanner + 1) % bannerSlides.length;
                bannerSlides[currentBanner].classList.add('active');
            }, <?php echo $slideInterval * 1000; ?>);
        }
    </script>
</body>
</html>
