<?php
require_once __DIR__ . '/bootstrap.php';
require_once __DIR__ . '/includes/flash.php';
require_once __DIR__ . '/includes/auth.php';
require_once __DIR__ . '/includes/payout_email.php';
require_once __DIR__ . '/includes/referral_engine.php';

$page_title = 'Affiliate Program - FantasyXXX';

$isAff = is_affiliate_logged_in();
$isAdmin = is_admin_logged_in();

$flash = get_flash();
$signup_error = null;
$signup_success = false;

// Get database connection
$pdo = db();
$refCode = strtoupper(trim((string)($_GET['ref'] ?? $_POST['ref_code'] ?? '')));
$referralLinkCode = strtoupper(trim((string)($_GET['rl'] ?? $_POST['referral_link_code'] ?? '')));
$referrer = null;
$referralLink = null;

if ($refCode !== '') {
    $stmt = $pdo->prepare('SELECT affiliate_id, public_code, email, contact_name, status FROM affiliates WHERE public_code = ? LIMIT 1');
    $stmt->execute([$refCode]);
    $referrer = $stmt->fetch() ?: null;
    if (!$referrer || (($referrer['status'] ?? '') !== 'active')) {
        $referrer = null;
        if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
            $signup_error = 'Invalid referral code.';
        }
    }
}

if ($referralLinkCode !== '') {
    $referralLink = get_affiliate_referral_link_by_code($referralLinkCode);
    if (!$referralLink || (($referralLink['status'] ?? '') !== 'active')) {
        $referralLink = null;
        if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
            $signup_error = 'Invalid referral link.';
        }
    } elseif (!$referrer || (int)($referralLink['affiliate_id'] ?? 0) !== (int)($referrer['affiliate_id'] ?? 0)) {
        $referralLink = null;
        if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
            $signup_error = 'Referral link does not match referral code.';
        }
    }
}

if ($_SERVER['REQUEST_METHOD'] !== 'POST' && $referrer) {
    track_affiliate_referral_click(
        (int)$referrer['affiliate_id'],
        $referralLink ? (int)($referralLink['referral_link_id'] ?? 0) : null,
        (string)$referrer['public_code']
    );
}

// Handle signup form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['_csrf'])) {
    if (!csrf_validate($_POST['_csrf'] ?? null)) {
        $signup_error = 'Invalid CSRF token';
    } else {
        $email = strtolower(trim((string)($_POST['email'] ?? '')));
        $password = (string)($_POST['password'] ?? '');
        $confirm = (string)($_POST['confirm_password'] ?? '');
        $contact = trim((string)($_POST['contact_name'] ?? ''));
        $company = trim((string)($_POST['company_name'] ?? ''));
        $instantMessenger = trim((string)($_POST['preferred_instant_messenger'] ?? ''));
        $accept = (string)($_POST['accept_terms'] ?? '');

        if ($email === '' || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $signup_error = 'Please enter a valid email address.';
        } elseif (strlen($password) < 8) {
            $signup_error = 'Password must be at least 8 characters.';
        } elseif (!hash_equals($password, $confirm)) {
            $signup_error = 'Passwords do not match.';
        } elseif ($accept !== '1') {
            $signup_error = 'You must accept the terms to continue.';
        } elseif ($contact === '') {
            $signup_error = 'Contact Name is required.';
        } elseif ($company === '') {
            $signup_error = 'Company is required.';
        } else {
            $exists = $pdo->prepare('SELECT affiliate_id FROM affiliates WHERE email = ?');
            $exists->execute([$email]);
            if ($exists->fetch()) {
                $signup_error = 'An account with this email already exists.';
            } else {
                $referredBy = null;
                $referredViaLinkId = null;
                if ($refCode !== '') {
                    if (!$referrer) {
                        $signup_error = 'Invalid referral code.';
                    } elseif (strtolower((string)$referrer['email']) === $email) {
                        $signup_error = 'You cannot use your own affiliate referral code.';
                    } else {
                        $referredBy = (int)$referrer['affiliate_id'];
                    }
                }

                if ($referralLinkCode !== '') {
                    if (!$referralLink) {
                        $signup_error = 'Invalid referral link.';
                    } elseif (!$referrer || (int)($referralLink['affiliate_id'] ?? 0) !== (int)($referrer['affiliate_id'] ?? 0)) {
                        $signup_error = 'Referral link does not match referral code.';
                    } else {
                        $referredViaLinkId = (int)$referralLink['referral_link_id'];
                    }
                }

                if ($signup_error !== null) {
                    goto render_index;
                }

                // Generate unique code
                for ($i = 0; $i < 30; $i++) {
                    $raw = random_bytes(6);
                    $code = strtoupper(substr(base64_encode($raw), 0, 10));
                    $code = preg_replace('/[^A-Z0-9]/', 'A', $code);
                    $stmt = $pdo->prepare('SELECT affiliate_id FROM affiliates WHERE public_code = ?');
                    $stmt->execute([$code]);
                    if (!$stmt->fetch()) {
                        break;
                    }
                }
                
                $hash = password_hash($password, PASSWORD_DEFAULT);
                $ipBin = ip_to_binary($_SERVER['REMOTE_ADDR'] ?? '');
                $termsVersion = defined('TERMS_VERSION') ? (string)TERMS_VERSION : 'v1';

                try {
                    $stmt = $pdo->prepare('INSERT INTO affiliates (public_code, referred_by, referred_via_link_id, status, email, password_hash, created_at, terms_version, terms_accepted_at, terms_accepted_ip, contact_name, company_name, preferred_instant_messenger)
                        VALUES (?, ?, ?, \'pending\', ?, ?, UTC_TIMESTAMP(), ?, UTC_TIMESTAMP(), ?, ?, ?, ?)');
                    $stmt->execute([
                        $code,
                        $referredBy,
                        $referredViaLinkId,
                        $email,
                        $hash,
                        $termsVersion,
                        $ipBin,
                        substr($contact, 0, 120),
                        substr($company, 0, 120),
                        $instantMessenger !== '' ? substr($instantMessenger, 0, 255) : null,
                    ]);
                    
                    // Send email notification to admin
                    $ipAddress = $_SERVER['REMOTE_ADDR'] ?? 'Unknown';
                    sendAffiliateSignupNotification($code, $email, $contact, $company, $ipAddress);
                    
                    $signup_success = true;
                } catch (Throwable $e) {
                    $signup_error = 'Signup failed. Please try again.';
                }
            }
        }
    }
}

render_index:

// Load hero box content from database
$stmt = $pdo->prepare('SELECT content_html FROM hero_box_content ORDER BY id DESC LIMIT 1');
$stmt->execute();
$heroContent = $stmt->fetch();

// Get commission percentages for variable substitution
$stmt = $pdo->prepare("SELECT revshare_bps, applies_to FROM commission_plans WHERE type = 'revshare' AND active = 1");
$stmt->execute();
$plans = $stmt->fetchAll();

$membership_percentage = 50; // default
$token_percentage = 15; // default

foreach ($plans as $plan) {
    $percentage = $plan['revshare_bps'] / 100;
    if ($plan['applies_to'] === 'membership_purchase' || $plan['applies_to'] === 'all_sales') {
        $membership_percentage = $percentage;
    }
    if ($plan['applies_to'] === 'token_purchase' || $plan['applies_to'] === 'all_sales') {
        $token_percentage = $percentage;
    }
}

// Prepare hero box HTML with variable substitution
if ($heroContent) {
    $heroBoxHtml = $heroContent['content_html'];
    // Replace variables
    $heroBoxHtml = str_replace('{membership_percentage}', $membership_percentage, $heroBoxHtml);
    $heroBoxHtml = str_replace('{token_percentage}', $token_percentage, $heroBoxHtml);
} else {
    // Fallback to default content if database is empty
    $heroBoxHtml = '<div class="eyebrow">FantasyXXX.ai Affiliate Program</div>
<h1>Turn AI fantasies into recurring revenue</h1>
<p class="hero-sub" style="font-size: 17px; font-weight: bold;">
  Promote FantasyXXX.ai and earn ' . $membership_percentage . '% lifetime revshare on Memberships and ' . $token_percentage . '% on ALL Token sales with only one tracking link.
  from one of the most advanced AI fantasy platforms online.
  High-converting funnels, real-time tracking, and on-time payouts — built for serious adult affiliates.
</p>
<div class="pill-row">
  <span class="pill">Lifetime revshare on recurring memberships and ALL token sales</span>
  <span class="pill">Lifetime cookie attribution</span>
  <span class="pill">Token &amp; membership sales</span>
  <span class="pill">Fast, flexible payouts</span>
</div>

<!-- WHY PROMOTE -->
<div style="margin-top: 40px;">
  <h2>Why promote FantasyXXX.ai?</h2>
  <p class="section-intro">
    Built by adult industry veterans for performance marketers, FantasyXXX.ai is optimized to convert your traffic into paying, returning users.
  </p>
</div>';
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title><?php echo htmlspecialchars($page_title); ?></title>
  <link rel="icon" type="image/x-icon" href="/images/favicon.ico">
  <link rel="icon" type="image/png" sizes="192x192" href="/images/favicon-192.png">
  <link rel="icon" type="image/png" sizes="512x512" href="/images/favicon-512.png">
  <link rel="apple-touch-icon" href="/images/favicon-192.png">

<style>
    :root {
      --bg-primary: #f7f5fa;
      --bg-secondary: #ffffff;
      --text-primary: #1a1a1a;
      --text-secondary: #4b4b4b;
      --text-muted: #777;
      --border-light: #e1d9f2;
      --border-card: #efe8fc;
      --accent-pink: #ff2e92;
      --accent-light-pink: #ff7ad9;
      --eyebrow-color: #8b7abf;
      --card-shadow: rgba(31, 13, 58, 0.12);
    }

    body.dark-mode {
      --bg-primary: #1a1a2e;
      --bg-secondary: #2d2d44;
      --text-primary: #f0f0f0;
      --text-secondary: #d0d0d0;
      --text-muted: #999;
      --border-light: #3d3d5c;
      --border-card: #3d3d5c;
      --eyebrow-color: #b8a8d8;
      --card-shadow: rgba(0, 0, 0, 0.3);
    }

    * { box-sizing: border-box; margin: 0; padding: 0; }
    body {
      font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
      background: var(--bg-primary);
      color: var(--text-primary);
      line-height: 1.6;
      transition: background 0.3s, color 0.3s;
    }
    a { text-decoration: none; }
    .page-wrap { max-width: 1120px; margin: 0 auto; padding: 24px 16px 64px; }
    .topbar {
      display: flex; align-items: center; justify-content: space-between;
      padding: 8px 0 24px;
    }
    .logo { font-weight: 700; font-size: 20px; color: var(--text-primary); }
    .logo span { color: #ff2e92; }
    .topbar-actions {
      display: flex; gap: 12px; align-items: center;
      flex-wrap: wrap;
      justify-content: flex-end;
    }
    .theme-toggle {
      display: inline-flex;
      align-items: center;
      justify-content: center;
      width: 36px;
      height: 36px;
      border-radius: 999px;
      background: var(--bg-secondary);
      border: 1px solid var(--border-light);
      cursor: pointer;
      font-size: 18px;
      transition: all 0.3s;
    }
    .theme-toggle:hover {
      background: var(--border-light);
    }
    .btn {
      display: inline-flex; align-items: center; justify-content: center;
      padding: 10px 18px; border-radius: 999px; font-weight: 600;
      font-size: 14px; border: 1px solid transparent;
      cursor: pointer;
      transition: all 0.3s;
    }
    .btn-outline {
      background: var(--bg-secondary);
      border-color: var(--border-light);
      color: var(--text-primary);
    }
    .btn-outline:hover {
      background: var(--border-light);
    }
    .btn-primary {
      background: linear-gradient(135deg, #ff2e92, #ff7ad9);
      color: #fff;
      box-shadow: 0 8px 24px rgba(255,46,146,0.25);
    }

    .hero {
      display: grid;
      grid-template-columns: minmax(0, 1fr) minmax(0, 1.1fr);
      gap: 32px;
      align-items: flex-start;
      margin-bottom: 16px;
    }
    .eyebrow {
      text-transform: uppercase;
      letter-spacing: .08em;
      font-size: 11px;
      font-weight: 600;
      color: var(--eyebrow-color);
      margin-bottom: 8px;
    }
    h1 {
      font-size: 32px;
      line-height: 1.2;
      margin-bottom: 12px;
      color: var(--text-primary);
    }
    .hero-sub {
      font-size: 15px;
      color: var(--text-secondary);
      margin-bottom: 18px;
    }
    .pill-row {
      display: flex;
      flex-wrap: wrap;
      gap: 8px;
      margin-bottom: 20px;
    }
    .pill {
      font-size: 11px;
      padding: 6px 10px;
      border-radius: 999px;
      background: var(--bg-secondary);
      border: 1px solid var(--border-light);
      color: var(--text-muted);
    }

    .signup-card {
      background: var(--bg-secondary);
      padding: 20px 18px;
      border-radius: 18px;
      box-shadow: 0 12px 30px var(--card-shadow);
      border: 1px solid var(--border-card);
    }
    .signup-card h2 {
      font-size: 18px;
      margin-bottom: 4px;
      color: var(--text-primary);
    }
    .signup-meta {
      font-size: 12px;
      color: var(--text-muted);
      margin-bottom: 14px;
    }
    .field {
      margin-bottom: 12px;
    }
    .field label {
      display: block;
      font-size: 12px;
      margin-bottom: 4px;
      font-weight: 500;
      color: var(--text-primary);
    }
    .field input[type="email"],
    .field input[type="password"],
    .field input[type="text"] {
      width: 100%;
      border-radius: 10px;
      border: 1px solid var(--border-light);
      padding: 8px 10px;
      font-size: 13px;
      background: var(--bg-primary);
      color: var(--text-primary);
    }
    .field input[type="email"]::placeholder,
    .field input[type="password"]::placeholder,
    .field input[type="text"]::placeholder {
      color: var(--text-muted);
    }
    .field-row {
      display: grid;
      grid-template-columns: repeat(2, minmax(0, 1fr));
      gap: 12px;
    }
    .pwd-meta {
      font-size: 11px;
      color: var(--text-muted);
      margin-top: 4px;
    }
    .checkbox-row {
      display: flex;
      align-items: flex-start;
      gap: 6px;
      margin: 10px 0 14px;
      font-size: 11px;
      color: var(--text-secondary);
    }
    .checkbox-row input { margin-top: 1px; }
    .checkbox-row a { color: #ff2e92; }
    .signup-footer {
      margin-top: 10px;
      font-size: 11px;
      color: var(--text-muted);
      text-align: center;
    }

    section {
      margin-bottom: 40px;
    }
    section h2 {
      font-size: 22px;
      margin-bottom: 10px;
      color: var(--text-primary);
    }
    section p.section-intro {
      font-size: 14px;
      color: var(--text-secondary);
      margin-bottom: 18px;
      max-width: 620px;
    }
    .grid-3 {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
      gap: 18px;
    }
    .card {
      background: var(--bg-secondary);
      border-radius: 14px;
      padding: 16px 14px;
      border: 1px solid var(--border-card);
      font-size: 13px;
      color: var(--text-secondary);
    }
    .card h3 {
      font-size: 15px;
      margin-bottom: 6px;
      color: var(--text-primary);
    }

    .two-col {
      display: grid;
      grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr);
      gap: 24px;
    }
    ul {
      padding-left: 16px;
      font-size: 14px;
      color: var(--text-secondary);
    }
    ul li { margin-bottom: 6px; }

    .steps {
      display: grid;
      grid-template-columns: repeat(3, minmax(0, 1fr));
      gap: 18px;
    }
    .how-it-works-inline {
      margin-top: 14px;
    }
    .how-it-works-inline h3 {
      font-size: 18px;
      margin-bottom: 10px;
      color: var(--text-primary);
    }
    .steps-inline {
      grid-template-columns: 1fr;
      max-width: 640px;
    }
    .steps-inline .card {
      width: 100%;
    }
    .step-num {
      display: inline-flex;
      width: 22px; height: 22px;
      align-items: center; justify-content: center;
      border-radius: 999px;
      background: #ffecf6;
      color: #d61870;
      font-size: 12px;
      font-weight: 700;
      margin-bottom: 6px;
    }

    .cta-strip {
      margin-top: 8px;
      padding: 16px 18px;
      border-radius: 18px;
      background: linear-gradient(135deg, #ffe5f5, #f2e6ff);
      text-align: center;
    }
    body.dark-mode .cta-strip {
      background: linear-gradient(135deg, rgba(255,46,146,0.15), rgba(242,230,255,0.1));
      border: 1px solid var(--border-card);
    }
    .cta-strip h2 { margin-bottom: 4px; color: var(--text-primary); font-size: 20px; }
    .cta-strip p { font-size: 14px; color: var(--text-secondary); margin-bottom: 10px; }

    .footer-links {
      margin-top: 0px;
      font-size: 12px;
      color: var(--text-muted);
      display: flex;
      flex-wrap: wrap;
      gap: 12px;
      justify-content: center;
    }
    .footer-links a { color: var(--text-muted); }
    .footer-links a:hover { color: #ff2e92; }

    @media (max-width: 1024px) {
      .hero {
        grid-template-columns: 1fr;
        gap: 20px;
      }
      .topbar {
        align-items: flex-start;
        gap: 10px;
      }
    }

    @media (max-width: 768px) {
      .hero, .two-col, .grid-3, .steps {
        grid-template-columns: 1fr;
      }
      .page-wrap { padding: 16px 12px 40px; }
      .field-row { grid-template-columns: 1fr; }
      .topbar {
        flex-direction: column;
      }
      .topbar-actions {
        width: 100%;
        justify-content: flex-start;
      }
      .topbar-actions .btn {
        flex: 1 1 auto;
        min-width: 180px;
      }
      .signup-card {
        padding: 16px 14px;
      }
      .how-it-works-inline h3 {
        font-size: 17px;
      }
      .steps-inline {
        max-width: 100%;
        gap: 12px;
      }
      .steps-inline .card {
        padding: 14px 12px;
      }
      .steps-inline .card h3 {
        font-size: 14px;
      }
    }

    @media (max-width: 480px) {
      h1 {
        font-size: 28px;
      }
      .btn {
        padding: 10px 14px;
      }
      .topbar-actions .btn {
        min-width: 0;
        width: 100%;
      }
      .theme-toggle {
        width: 40px;
        height: 40px;
      }
    }
</style>
</head>
<body>

<div class="page-wrap">
  <header class="topbar">
    <div class="logo">Fantasy<span>XXX</span>.ai Affiliates</div>
    <div class="topbar-actions">
      <?php if ($isAff): ?>
        <a href="<?php echo htmlspecialchars(BASE_URL . 'affiliate/dashboard.php'); ?>" class="btn btn-outline">Affiliate Dashboard</a>
        <a href="<?php echo htmlspecialchars(BASE_URL . 'affiliate/logout.php'); ?>" class="btn btn-outline">Affiliate Logout</a>
      <?php else: ?>
        <a href="<?php echo htmlspecialchars(BASE_URL . 'affiliate/login.php'); ?>" class="btn btn-outline">Affiliate Login</a>
      <?php endif; ?>
      
      <button class="theme-toggle" id="theme-toggle" title="Toggle dark mode" onclick="toggleTheme()">☀️</button>
    </div>
  </header>

  <script>
    // Initialize theme from localStorage
    function initTheme() {
      const savedTheme = localStorage.getItem('theme') || 'light';
      if (savedTheme === 'dark') {
        document.body.classList.add('dark-mode');
        document.getElementById('theme-toggle').textContent = '🌙';
      } else {
        document.body.classList.remove('dark-mode');
        document.getElementById('theme-toggle').textContent = '☀️';
      }
    }

    // Toggle theme
    function toggleTheme() {
      const isDark = document.body.classList.toggle('dark-mode');
      const theme = isDark ? 'dark' : 'light';
      localStorage.setItem('theme', theme);
      document.getElementById('theme-toggle').textContent = isDark ? '🌙' : '☀️';
    }

    // Initialize on page load
    document.addEventListener('DOMContentLoaded', initTheme);
    initTheme();
  </script>

  <main>
    <!-- HERO -->
    <section class="hero">
      <div>
        <?php echo $heroBoxHtml; ?>
      </div>

      <aside class="signup-card">
        <?php if ($isAff): ?>
          <h2>Affiliate Dashboard</h2>
          <p class="signup-meta">Track performance, manage commissions, and request payouts</p>
          <a href="<?php echo htmlspecialchars(BASE_URL . 'affiliate/dashboard.php'); ?>" class="btn btn-primary" style="width:100%;">
            Go to Dashboard
          </a>
          <div class="signup-footer">
            Real-time tracking • Commission management • Payout requests
          </div>
        <?php elseif ($signup_success): ?>
          <h2>✓ Signup Received!</h2>
          <p class="signup-meta" style="color: #22c55e; font-weight: 600;">Your affiliate account is pending approval.</p>
          <div style="background: rgba(34, 197, 94, 0.1); border: 1px solid rgba(34, 197, 94, 0.3); border-radius: 10px; padding: 1rem; margin: 1rem 0; color: #22c55e; font-size: 0.9rem; text-align: center;">
            Our affiliate manager will contact you shortly after approval.
          </div>
          <a href="<?php echo htmlspecialchars(BASE_URL . 'affiliate/login.php'); ?>" class="btn btn-primary" style="width:100%;">
            Go to Login
          </a>
          <div class="signup-footer">
          </div>
        <?php else: ?>
          <h2>Create your affiliate account</h2>
          <p class="signup-meta">Instant approval • Real-time tracking • Secure dashboard</p>
          <?php if ($signup_error): ?>
            <div style="background: rgba(239, 68, 68, 0.1); border: 1px solid rgba(239, 68, 68, 0.3); border-radius: 10px; padding: 0.75rem; margin-bottom: 1rem; color: #ef4444; font-size: 0.9rem;">
              ✕ <?php echo htmlspecialchars($signup_error); ?>
            </div>
          <?php endif; ?>
          <form method="post" action="">
            <input type="hidden" name="_csrf" value="<?php echo htmlspecialchars(csrf_token()); ?>">
            <input type="hidden" name="ref_code" value="<?php echo htmlspecialchars($refCode); ?>">
            <input type="hidden" name="referral_link_code" value="<?php echo htmlspecialchars($referralLinkCode); ?>">
            <div class="field">
              <label for="email">Email (required)</label>
              <input id="email" name="email" type="email" placeholder="you@example.com" required autocomplete="email" value="<?php echo htmlspecialchars((string)($_POST['email'] ?? '')); ?>">
            </div>
            <div class="field-row">
              <div class="field">
                <label for="password">Password</label>
                <input id="password" name="password" type="password" placeholder="Choose a strong password" required autocomplete="new-password" value="<?php echo htmlspecialchars((string)($_POST['password'] ?? '')); ?>">
                <div class="pwd-meta">8+ characters, mixed case recommended</div>
              </div>
              <div class="field">
                <label for="confirm_password">Confirm Password</label>
                <input id="confirm_password" name="confirm_password" type="password" placeholder="Retype password" required autocomplete="new-password" value="<?php echo htmlspecialchars((string)($_POST['confirm_password'] ?? '')); ?>">
              </div>
            </div>
            <div class="field-row">
              <div class="field">
                <label for="contact_name">Contact Name (required)</label>
                <input id="contact_name" name="contact_name" type="text" placeholder="Jamie Doe" required value="<?php echo htmlspecialchars((string)($_POST['contact_name'] ?? '')); ?>">
              </div>
              <div class="field">
                <label for="company_name">Company (required)</label>
                <input id="company_name" name="company_name" type="text" placeholder="My Media LLC" required value="<?php echo htmlspecialchars((string)($_POST['company_name'] ?? '')); ?>">
              </div>
            </div>
            <div class="field">
              <label for="preferred_instant_messenger">Name your instant messenger and enter your ID</label>
              <input id="preferred_instant_messenger" name="preferred_instant_messenger" type="text" placeholder="e.g., Telegram: @myusername, Discord: discord#1234, or WhatsApp: +1234567890" value="<?php echo htmlspecialchars((string)($_POST['preferred_instant_messenger'] ?? '')); ?>">
              <div class="pwd-meta">Example: Telegram, telegram_id or Discord, discord#1234 or WhatsApp, +1234567890</div>
            </div>
            <label class="checkbox-row">
              <input type="checkbox" id="accept_terms" name="accept_terms" value="1" required>
              <span>I agree to the <a href="#">Terms</a> and <a href="#">Privacy Policy</a>.</span>
            </label>
            <button type="submit" class="btn btn-primary" style="width:100%;">
              Create Account &amp; Get My Tracking Links
            </button>
            <div class="signup-footer">
              No setup fees • 60-second signup • Your data stays private
            </div>
          </form>
        <?php endif; ?>
      </aside>
    </section>

    <!-- PROGRAM HIGHLIGHTS -->
    <section>
      <div class="two-col">
        <div>
          <h2>Program highlights at a glance</h2>
          <p class="section-intro">
            Everything you need to scale profitable campaigns, from flexible payouts to battle-tested funnels.
          </p>
          <ul>
            <li><strong>High-converting AI offers:</strong> AI companions, fantasy chats, image and video generation are token-based content that keep users engaged, spending, and coming back.</li>
            <li><strong>Lifetime revshare:</strong> Earn on recurring memberships, token top-ups, and ALL token sales from returning users.</li>
            <li><strong>Lifetime cookie:</strong> Lifetime cookie attribution means you keep getting credit when your users come back and spend again.</li>
            <li><strong>Commission model:</strong> Lifetime revshare on recurring memberships and ALL token sales</li>
            <li><strong>Attribution:</strong> Lifetime cookie • last-click, affiliate-first — fair and transparent</li>
            <li><strong>Traffic types:</strong> SEO, blogs, socials (X, Reddit), tube sites, paid media, email.</li>
            <li><strong>Vertical:</strong> AI fantasy companions, chat, and premium token experiences.</li>
            <li><strong>Payouts:</strong> Monthly with custom options for high-volume partners.
              <div class="how-it-works-inline">
                <h3>How it works</h3>
                <div class="steps steps-inline">
                  <div class="card">
                    <div class="step-num">1</div>
                    <h3>Sign up</h3>
                    <p>Create your free account at <strong>affiliates.fantasyxxx.ai</strong> and get instant access to your dashboard &amp; tracking links.</p>
                  </div>
                  <div class="card">
                    <div class="step-num">2</div>
                    <h3>Launch campaigns</h3>
                    <p>Send traffic to our best-performing landing pages, AI demos, and token offers.</p>
                  </div>
                  <div class="card">
                    <div class="step-num">3</div>
                    <h3>Get Paid</h3>
                    <p>Payment done on 7th of each month upon request with a min $50 payout<br>
                    (PayPal - Bank - ACH - Crypto - Paxum)</p>
                  </div>
                </div>
              </div>
            </li>
          </ul>
        </div>
        <div style="display: flex; flex-direction: column; gap: 16px;">
          <div style="background: var(--bg-secondary); border-radius: 14px; border: 1px solid var(--border-card); overflow: hidden; box-shadow: 0 12px 30px var(--card-shadow); display: flex; flex-direction: column;">
            <video width="100%" style="display: block; background: #000; border-radius: 14px 14px 0 0; object-fit: cover; aspect-ratio: 1/1;" autoplay muted loop>
              <source src="https://fantasyxxx.ai/videos/video_694956a8c632e2.29509151.mp4" type="video/mp4">
              Your browser does not support the video tag.
            </video>
            <div style="padding: 16px 14px;">
              <h3 style="font-size: 16px; margin-bottom: 6px; color: var(--text-primary); font-weight: 600;">See It In Action</h3>
              <p style="font-size: 13px; color: var(--text-secondary); margin-bottom: 10px;">Watch how FantasyXXX.ai converts traffic into recurring revenue with AI-powered experiences.</p>
              <p style="font-size: 12px; color: var(--accent-pink); font-weight: 600; margin: 0;">💰 Turn AI fantasies into lifetime commissions</p>
            </div>
          </div>
        </div>
              </div>
    </section>

    <!-- CTA / FOOTER -->
    <section class="cta-strip">
      <h2>Ready to start earning with AI fantasy traffic?</h2>
      <?php if (!$isAff): ?>
        <a href="<?php echo htmlspecialchars(BASE_URL); ?>" class="btn btn-primary" style="margin-right:8px;">Join Program Now</a>
      <?php endif; ?>
      <a href="<?php echo htmlspecialchars(BASE_URL . 'affiliate/login.php'); ?>" class="btn btn-outline">Talk to our affiliate team</a>
    </section>

      </main>
      <?php require_once __DIR__ . '/shared/footer.php'; ?>
</div>


