<?php
session_start();

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

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

require_once "include/db.php";
mysqli_set_charset($conn, "utf8mb4");

$userId = intval($_SESSION['user_id']);

// Fetch User Data
$sql = "SELECT * FROM users WHERE id = $userId LIMIT 1";
$result = mysqli_query($conn, $sql);
$currentUser = ($result && mysqli_num_rows($result) > 0) ? mysqli_fetch_assoc($result) : null;

$dbTheme = $_SESSION['theme'] ?? ($currentUser['theme'] ?? '#0ea5e9');
$userName = $currentUser['username'] ?? 'Trader';
$userBalance = $currentUser['balance'] ?? 0;

$success = '';
$error = '';

// Handle Ticket Submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
    if ($_POST['action'] === 'submit_ticket') {
        $subject = mysqli_real_escape_string($conn, trim($_POST['subject'] ?? ''));
        $category = mysqli_real_escape_string($conn, trim($_POST['category'] ?? ''));
        $priority = mysqli_real_escape_string($conn, trim($_POST['priority'] ?? 'medium'));
        $message = mysqli_real_escape_string($conn, trim($_POST['message'] ?? ''));
        
        if (empty($subject) || empty($category) || empty($message)) {
            $error = 'Please fill in all required fields.';
        } elseif (strlen($message) < 10) {
            $error = 'Message must be at least 10 characters long.';
        } else {
            $ticketSql = "INSERT INTO support_tickets (user_id, subject, category, priority, message, status, created_at) 
                          VALUES ($userId, '$subject', '$category', '$priority', '$message', 'open', NOW())";
            
            if (mysqli_query($conn, $ticketSql)) {
                $success = 'Ticket submitted successfully! Our team will respond within 24 hours.';
            } else {
                // If table doesn't exist, show message but don't fail
                if (strpos(mysqli_error($conn), "doesn't exist") !== false) {
                    $error = 'Support system is being set up. Please contact us via email or live chat.';
                } else {
                    $error = 'Failed to submit ticket. Please try again.';
                }
            }
        }
    }
}

// Fetch User's Tickets
$tickets = [];
$ticketQuery = mysqli_query($conn, "SELECT * FROM support_tickets WHERE user_id = $userId ORDER BY created_at DESC LIMIT 10");
if ($ticketQuery) {
    while ($row = mysqli_fetch_assoc($ticketQuery)) {
        $tickets[] = $row;
    }
}

// FAQ Data
$faqs = [
    [
        'question' => 'How do I deposit funds?',
        'answer' => 'Navigate to the Wallet section and click "Deposit Funds". Choose your preferred payment method, enter the amount, and follow the instructions to complete your deposit.'
    ],
    [
        'question' => 'How long do withdrawals take?',
        'answer' => 'Withdrawals are typically processed within 24-48 hours. Bank transfers may take 3-5 business days depending on your bank and location.'
    ],
    [
        'question' => 'What is the minimum deposit amount?',
        'answer' => 'The minimum deposit amount is $10 for most payment methods. Some methods may have higher minimums.'
    ],
    [
        'question' => 'How does copy trading work?',
        'answer' => 'Copy trading allows you to automatically replicate the trades of experienced traders. Go to "Copy Trading", select a trader, and allocate funds to copy their positions.'
    ],
    [
        'question' => 'Is my account secure?',
        'answer' => 'Yes, we use bank-level encryption, two-factor authentication, and regular security audits to protect your account and funds.'
    ],
    [
        'question' => 'Can I change my trading leverage?',
        'answer' => 'Leverage settings can be adjusted in your Profile Settings under the Trading Preferences tab. Available leverage depends on your account type and verification level.'
    ],
    [
        'question' => 'What happens if a trade expires?',
        'answer' => 'When a trade expires, the result is determined by the asset price at expiry. If your prediction is correct, you receive your stake plus profit. If incorrect, you lose the stake.'
    ],
    [
        'question' => 'How do I contact my account manager?',
        'answer' => 'Premium account holders can access their dedicated account manager through the "Account Manager" button in the dashboard or by emailing manager@theinteractivebrokers.com.'
    ]
];

// Support Contact Info
$supportEmail = 'support@theinteractivebrokers.com';
$supportPhone = '+1 (800) 555-0199';
$liveChatHours = '24/7';
?>

<?php include "include/header.php"?>

<style>
    :root {
        --primary-theme: <?php echo htmlspecialchars($dbTheme, ENT_QUOTES, 'UTF-8'); ?>;
        --primary-rgb: <?php 
            $hex = ltrim($dbTheme, '#');
            $r = hexdec(substr($hex, 0, 2));
            $g = hexdec(substr($hex, 2, 2));
            $b = hexdec(substr($hex, 4, 2));
            echo "$r, $g, $b";
        ?>;
    }
    
    .text-theme { color: var(--primary-theme) !important; }
    .bg-theme { background-color: var(--primary-theme) !important; }
    .border-theme { border-color: var(--primary-theme) !important; }
    .bg-theme-gradient { 
        background: linear-gradient(135deg, var(--primary-theme) 0%, #1e1b4b 100%) !important; 
    }
    .shadow-theme {
        box-shadow: 0 4px 20px -2px rgba(var(--primary-rgb), 0.25),
                    0 2px 8px -2px rgba(var(--primary-rgb), 0.15);
    }
    
    .glass-card {
        background: rgba(30, 41, 59, 0.6);
        backdrop-filter: blur(12px); 
        border: 1px solid rgba(255, 255, 255, 0.08);
    }
    .glass-card-light {
        background: rgb(0 0 0 / 80%); 
        backdrop-filter: blur(12px);
        border: 1px solid rgba(0, 0, 0, 0.06);
    }
    
    .gradient-border {
        position: relative;
        background: linear-gradient(135deg, rgba(var(--primary-rgb), 0.1), rgba(30, 27, 75, 0.2));
        border-radius: 1rem;
    }
    .gradient-border::before {
        content: '';
        position: absolute;
        inset: 0;
        border-radius: 1rem;
        padding: 1.5px;
        background: linear-gradient(135deg, var(--primary-theme), rgba(var(--primary-rgb), 0.3), transparent);
        -webkit-mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
        mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
        -webkit-mask-composite: xor;
        mask-composite: exclude;
        pointer-events: none;
    }
    
    ::-webkit-scrollbar { width: 6px; height: 6px; }
    ::-webkit-scrollbar-track { background: transparent; }
    ::-webkit-scrollbar-thumb { background: rgba(var(--primary-rgb), 0.3); border-radius: 99px; }
    ::-webkit-scrollbar-thumb:hover { background: rgba(var(--primary-rgb), 0.5); }
    
    .neon-accent {
        box-shadow: 0 0 15px rgba(var(--primary-rgb), 0.2),
                    inset 0 1px 0 rgba(255,255,255,0.1);
    }
    
    .contact-card {
        transition: all 0.3s ease;
    }
    .contact-card:hover {
        transform: translateY(-4px);
        box-shadow: 0 20px 40px -10px rgba(var(--primary-rgb), 0.15);
    }
    
    .faq-item {
        transition: all 0.3s ease;
    }
    .faq-item:hover {
        border-color: rgba(var(--primary-rgb), 0.3);
    }
    
    .input-field {
        transition: all 0.2s ease;
    }
    .input-field:focus {
        border-color: var(--primary-theme);
        box-shadow: 0 0 0 3px rgba(var(--primary-rgb), 0.1);
    }
    
    .btn-primary {
        background: var(--primary-theme);
        transition: all 0.3s ease;
    }
    .btn-primary:hover {
        filter: brightness(1.1);
        transform: translateY(-1px);
    }
    
    .alert-success {
        background: linear-gradient(135deg, rgba(16, 185, 129, 0.15), rgba(16, 185, 129, 0.05));
        border: 1px solid rgba(16, 185, 129, 0.3);
        color: #10b981;
    }
    .alert-error {
        background: linear-gradient(135deg, rgba(244, 63, 94, 0.15), rgba(244, 63, 94, 0.05));
        border: 1px solid rgba(244, 63, 94, 0.3);
        color: #f43f5e;
    }
    
    .status-open { background: linear-gradient(135deg, rgba(16, 185, 129, 0.15), rgba(16, 185, 129, 0.05)); border: 1px solid rgba(16, 185, 129, 0.3); color: #10b981; }
    .status-pending { background: linear-gradient(135deg, rgba(245, 158, 11, 0.15), rgba(245, 158, 11, 0.05)); border: 1px solid rgba(245, 158, 11, 0.3); color: #f59e0b; }
    .status-closed { background: linear-gradient(135deg, rgba(100, 116, 139, 0.15), rgba(100, 116, 139, 0.05)); border: 1px solid rgba(100, 116, 139, 0.3); color: #64748b; }
    
    .priority-high { background: linear-gradient(135deg, rgba(244, 63, 94, 0.15), rgba(244, 63, 94, 0.05)); border: 1px solid rgba(244, 63, 94, 0.3); color: #f43f5e; }
    .priority-medium { background: linear-gradient(135deg, rgba(245, 158, 11, 0.15), rgba(245, 158, 11, 0.05)); border: 1px solid rgba(245, 158, 11, 0.3); color: #f59e0b; }
    .priority-low { background: linear-gradient(135deg, rgba(16, 185, 129, 0.15), rgba(16, 185, 129, 0.05)); border: 1px solid rgba(16, 185, 129, 0.3); color: #10b981; }
    
    .chat-bubble {
        position: relative;
    }
    .chat-bubble::after {
        content: '';
        position: absolute;
        bottom: 0;
        width: 12px;
        height: 12px;
        background: inherit;
    }
    .chat-bubble-left::after {
        left: -6px;
        border-radius: 0 0 12px 0;
    }
    .chat-bubble-right::after {
        right: -6px;
        border-radius: 0 0 0 12px;
    }
</style>

<!-- NAVBAR -->
<?php include "include/nav.php"?>

<div class="flex min-h-screen bg-slate-50 dark:bg-[#0b111a] transition-colors duration-300">
  
  <!-- SIDEBAR -->
  <?php include "include/sidebar.php"?>
  
  <!-- MAIN CONTENT -->
  <main class="flex-1 min-w-0 md:ml-72 pb-24 md:pb-8">
    <div class="max-w-screen-2xl mx-auto px-4 sm:px-6 py-6">

      <!-- Welcome Row -->
      <div class="flex flex-col md:flex-row md:items-center justify-between gap-4 mb-6">
        <div>
          <h1 class="text-2xl sm:text-3xl font-bold text-slate-900 dark:text-white tracking-tight">
            Support <span class="text-theme">Center</span>
          </h1>
          <p class="text-sm text-slate-500 dark:text-slate-400 mt-1">We're here to help you 24/7</p>
        </div>
      </div>

      <!-- Alerts -->
      <?php if ($success): ?>
      <div class="mb-6 p-4 rounded-xl alert-success flex items-center gap-3">
        <svg width="20" height="20" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M20 6L9 17l-5-5"/></svg>
        <span class="text-sm font-medium"><?= htmlspecialchars($success) ?></span>
      </div>
      <?php endif; ?>
      
      <?php if ($error): ?>
      <div class="mb-6 p-4 rounded-xl alert-error flex items-center gap-3">
        <svg width="20" height="20" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><circle cx="12" cy="12" r="10"/><path d="M15 9l-6 6M9 9l6 6"/></svg>
        <span class="text-sm font-medium"><?= htmlspecialchars($error) ?></span>
      </div>
      <?php endif; ?>

      <!-- Contact Cards -->
      <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-8">
        
        <!-- Live Chat -->
        <div class="contact-card gradient-border p-6 cursor-pointer" onclick="openLiveChat()">
          <div class="relative z-10 text-center">
            <div class="w-14 h-14 rounded-2xl bg-emerald-500/10 flex items-center justify-center mx-auto mb-4 neon-accent">
              <svg width="28" height="28" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" class="text-emerald-500"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>
            </div>
            <h3 class="text-lg font-bold text-slate-900 dark:text-white mb-1">Live Chat</h3>
            <p class="text-sm text-slate-500 dark:text-slate-400 mb-2">Instant response</p>
            <span class="inline-flex items-center gap-1.5 px-3 py-1 bg-emerald-500/10 text-emerald-500 text-xs font-bold rounded-full border border-emerald-500/20">
              <span class="w-1.5 h-1.5 rounded-full bg-emerald-500 animate-pulse"></span> Online
            </span>
            <p class="text-xs text-slate-400 mt-2"><?= $liveChatHours ?></p>
          </div>
        </div>

        <!-- Email Support -->
        <div class="contact-card gradient-border p-6 cursor-pointer" onclick="window.location.href='mailto:<?= $supportEmail ?>'">
          <div class="relative z-10 text-center">
            <div class="w-14 h-14 rounded-2xl bg-theme/10 flex items-center justify-center mx-auto mb-4 neon-accent">
              <svg width="28" height="28" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" class="text-theme"><path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"/><polyline points="22,6 12,13 2,6"/></svg>
            </div>
            <h3 class="text-lg font-bold text-slate-900 dark:text-white mb-1">Email Us</h3>
            <p class="text-sm text-slate-500 dark:text-slate-400 mb-2">Response within 24h</p>
            <span class="inline-flex px-3 py-1 bg-theme/10 text-theme text-xs font-bold rounded-full border border-theme/20">
              <?= $supportEmail ?>
            </span>
            <p class="text-xs text-slate-400 mt-2">24/7 Support</p>
          </div>
        </div>

        <!-- Phone Support -->
        <div class="contact-card gradient-border p-6 cursor-pointer" onclick="window.location.href='tel:<?= preg_replace('/[^0-9+]/', '', $supportPhone) ?>'">
          <div class="relative z-10 text-center">
            <div class="w-14 h-14 rounded-2xl bg-amber-500/10 flex items-center justify-center mx-auto mb-4 neon-accent">
              <svg width="28" height="28" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" class="text-amber-500"><path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72 12.84 12.84 0 0 0 .7 2.81 2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45 12.84 12.84 0 0 0 2.81.7A2 2 0 0 1 22 16.92z"/></svg>
            </div>
            <h3 class="text-lg font-bold text-slate-900 dark:text-white mb-1">Call Us</h3>
            <p class="text-sm text-slate-500 dark:text-slate-400 mb-2">Mon-Fri 9AM-6PM EST</p>
            <span class="inline-flex px-3 py-1 bg-amber-500/10 text-amber-500 text-xs font-bold rounded-full border border-amber-500/20">
              <?= $supportPhone ?>
            </span>
            <p class="text-xs text-slate-400 mt-2">Business Hours</p>
          </div>
        </div>
      </div>

      <!-- Main Grid -->
      <div class="grid grid-cols-1 xl:grid-cols-3 gap-6">
        
        <!-- LEFT: Submit Ticket -->
        <div class="xl:col-span-2 space-y-6">
          
          <!-- Submit Ticket Form -->
          <div class="glass-card-light dark:glass-card rounded-2xl border border-slate-200 dark:border-slate-700/50 overflow-hidden transition-colors">
            <div class="p-4 border-b border-slate-200 dark:border-slate-700/50 flex justify-between items-center">
              <div class="flex items-center gap-3">
                <div class="w-8 h-8 rounded-lg bg-theme/10 flex items-center justify-center neon-accent">
                  <svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" class="text-theme"><path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/></svg>
                </div>
                <div>
                  <h3 class="font-bold text-slate-900 dark:text-white text-sm">Submit a Ticket</h3>
                  <p class="text-xs text-slate-500 dark:text-slate-400">Describe your issue and we'll help you</p>
                </div>
              </div>
            </div>
            
            <form method="POST" action="" class="p-6 space-y-5">
              <input type="hidden" name="action" value="submit_ticket">
              
              <div class="grid grid-cols-1 md:grid-cols-2 gap-5">
                <!-- Subject -->
                <div class="md:col-span-2">
                  <label class="block text-xs font-bold text-slate-500 dark:text-slate-400 uppercase mb-2 tracking-wider">Subject *</label>
                  <input type="text" name="subject" required placeholder="Brief description of your issue"
                         class="input-field w-full bg-slate-50 dark:bg-slate-800 border border-slate-200 dark:border-slate-700 text-slate-900 dark:text-white text-sm rounded-xl px-4 py-3 outline-none">
                </div>
                
                <!-- Category -->
                <div>
                  <label class="block text-xs font-bold text-slate-500 dark:text-slate-400 uppercase mb-2 tracking-wider">Category *</label>
                  <select name="category" required class="input-field w-full bg-slate-50 dark:bg-slate-800 border border-slate-200 dark:border-slate-700 text-slate-900 dark:text-white text-sm rounded-xl px-4 py-3 outline-none appearance-none cursor-pointer">
                    <option value="">Select Category</option>
                    <option value="account">Account Issues</option>
                    <option value="deposit">Deposit / Withdrawal</option>
                    <option value="trading">Trading Problems</option>
                    <option value="technical">Technical Issues</option>
                    <option value="verification">Verification / KYC</option>
                    <option value="billing">Billing & Payments</option>
                    <option value="other">Other</option>
                  </select>
                </div>
                
                <!-- Priority -->
                <div>
                  <label class="block text-xs font-bold text-slate-500 dark:text-slate-400 uppercase mb-2 tracking-wider">Priority</label>
                  <select name="priority" class="input-field w-full bg-slate-50 dark:bg-slate-800 border border-slate-200 dark:border-slate-700 text-slate-900 dark:text-white text-sm rounded-xl px-4 py-3 outline-none appearance-none cursor-pointer">
                    <option value="low">Low - General question</option>
                    <option value="medium" selected>Medium - Need help soon</option>
                    <option value="high">High - Urgent issue</option>
                  </select>
                </div>
                
                <!-- Message -->
                <div class="md:col-span-2">
                  <label class="block text-xs font-bold text-slate-500 dark:text-slate-400 uppercase mb-2 tracking-wider">Message *</label>
                  <textarea name="message" required rows="5" placeholder="Please provide as much detail as possible about your issue..."
                            class="input-field w-full bg-slate-50 dark:bg-slate-800 border border-slate-200 dark:border-slate-700 text-slate-900 dark:text-white text-sm rounded-xl px-4 py-3 outline-none resize-none"></textarea>
                  <p class="text-[10px] text-slate-400 mt-1">Minimum 10 characters</p>
                </div>
              </div>
              
              <div class="flex justify-end pt-2">
                <button type="submit" class="btn-primary px-8 py-3.5 text-white text-sm font-bold rounded-xl shadow-theme flex items-center gap-2">
                  <svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M22 2L11 13M22 2l-7 20-4-9-9-4 20-7z"/></svg>
                  Submit Ticket
                </button>
              </div>
            </form>
          </div>

          <!-- My Tickets -->
          <div class="glass-card-light dark:glass-card rounded-2xl border border-slate-200 dark:border-slate-700/50 overflow-hidden transition-colors">
            <div class="p-4 border-b border-slate-200 dark:border-slate-700/50 flex justify-between items-center">
              <div class="flex items-center gap-3">
                <div class="w-8 h-8 rounded-lg bg-amber-500/10 flex items-center justify-center neon-accent">
                  <svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" class="text-amber-500"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>
                </div>
                <div>
                  <h3 class="font-bold text-slate-900 dark:text-white text-sm">My Tickets</h3>
                  <p class="text-xs text-slate-500 dark:text-slate-400">Your recent support requests</p>
                </div>
              </div>
              <span class="text-xs font-bold text-amber-500"><?= count($tickets) ?> Total</span>
            </div>
            
            <div class="overflow-x-auto">
              <?php if (empty($tickets)): ?>
              <div class="p-8 text-center">
                <div class="flex flex-col items-center gap-2">
                  <div class="w-12 h-12 rounded-full bg-slate-100 dark:bg-slate-800 flex items-center justify-center mb-2">
                    <svg width="20" height="20" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" class="text-slate-400"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>
                  </div>
                  <p class="text-sm text-slate-500">No tickets yet</p>
                  <p class="text-xs text-slate-400">Submit a ticket above to get help</p>
                </div>
              </div>
              <?php else: ?>
              <div class="divide-y divide-slate-200 dark:divide-slate-700/50">
                <?php foreach ($tickets as $ticket): 
                  $statusClass = 'status-' . ($ticket['status'] ?? 'open');
                  $priorityClass = 'priority-' . ($ticket['priority'] ?? 'medium');
                  $statusLabel = ucfirst($ticket['status'] ?? 'Open');
                  $priorityLabel = ucfirst($ticket['priority'] ?? 'Medium');
                ?>
                <div class="p-4 hover:bg-slate-50 dark:hover:bg-slate-800/50 transition-colors">
                  <div class="flex items-start justify-between mb-2">
                    <div class="flex items-center gap-2">
                      <span class="px-2 py-1 text-[10px] font-bold rounded-full <?= $statusClass ?>"><?= $statusLabel ?></span>
                      <span class="px-2 py-1 text-[10px] font-bold rounded-full <?= $priorityClass ?>"><?= $priorityLabel ?></span>
                    </div>
                    <span class="text-[10px] text-slate-400"><?= date('M d, Y H:i', strtotime($ticket['created_at'])) ?></span>
                  </div>
                  <h4 class="text-sm font-bold text-slate-900 dark:text-white mb-1"><?= htmlspecialchars($ticket['subject']) ?></h4>
                  <p class="text-xs text-slate-500 line-clamp-2"><?= htmlspecialchars($ticket['message']) ?></p>
                  <div class="flex items-center gap-2 mt-2">
                    <span class="text-[10px] text-slate-400 uppercase">Category: <?= ucfirst($ticket['category'] ?? 'General') ?></span>
                  </div>
                </div>
                <?php endforeach; ?>
              </div>
              <?php endif; ?>
            </div>
          </div>

        </div>

        <!-- RIGHT: FAQ -->
        <div class="space-y-6">
          
          <div class="glass-card-light dark:glass-card rounded-2xl border border-slate-200 dark:border-slate-700/50 overflow-hidden transition-colors">
            <div class="p-4 border-b border-slate-200 dark:border-slate-700/50 flex justify-between items-center">
              <div class="flex items-center gap-3">
                <div class="w-8 h-8 rounded-lg bg-theme/10 flex items-center justify-center neon-accent">
                  <svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" class="text-theme"><circle cx="12" cy="12" r="10"/><path d="M9.09 9a3 3 0 0 1 5.83 1c0 2-3 3-3 3"/><line x1="12" y1="17" x2="12.01" y2="17"/></svg>
                </div>
                <div>
                  <h3 class="font-bold text-slate-900 dark:text-white text-sm">FAQ</h3>
                  <p class="text-xs text-slate-500 dark:text-slate-400">Common questions</p>
                </div>
              </div>
            </div>
            
            <div class="divide-y divide-slate-200 dark:divide-slate-700/50" x-data="{ openFaq: null }">
              <?php foreach ($faqs as $index => $faq): ?>
              <div class="faq-item">
                <button @click="openFaq === <?= $index ?> ? openFaq = null : openFaq = <?= $index ?>"
                        class="w-full flex items-center justify-between p-4 text-left hover:bg-slate-50 dark:hover:bg-slate-800/30 transition-colors">
                  <span class="text-sm font-medium text-slate-900 dark:text-white pr-4"><?= htmlspecialchars($faq['question']) ?></span>
                  <svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" 
                       class="text-slate-400 shrink-0 transition-transform" :class="openFaq === <?= $index ?> ? 'rotate-180' : ''">
                    <polyline points="6 9 12 15 18 9"/>
                  </svg>
                </button>
                <div x-show="openFaq === <?= $index ?>" x-collapse x-cloak class="px-4 pb-4">
                  <p class="text-xs text-slate-500 dark:text-slate-400 leading-relaxed"><?= htmlspecialchars($faq['answer']) ?></p>
                </div>
              </div>
              <?php endforeach; ?>
            </div>
          </div>

          <!-- Quick Links -->
          <div class="glass-card-light dark:glass-card rounded-2xl border border-slate-200 dark:border-slate-700/50 p-5 transition-colors">
            <h3 class="font-bold text-slate-900 dark:text-white text-sm mb-4">Quick Links</h3>
            <div class="space-y-2">
              <a href="accounthistory.php" class="flex items-center gap-3 p-3 rounded-xl hover:bg-slate-50 dark:hover:bg-slate-800/50 transition-colors group">
                <div class="w-8 h-8 rounded-lg bg-theme/10 flex items-center justify-center group-hover:bg-theme/20 transition-colors">
                  <svg width="14" height="14" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" class="text-theme"><path d="M3 3v5h5"/><path d="M3.05 13A9 9 0 1 0 6 5.3L3 8"/></svg>
                </div>
                <div>
                  <p class="text-sm font-medium text-slate-700 dark:text-slate-300">Account History</p>
                  <p class="text-[10px] text-slate-400">View transactions</p>
                </div>
              </a>
              <a href="deposit.php" class="flex items-center gap-3 p-3 rounded-xl hover:bg-slate-50 dark:hover:bg-slate-800/50 transition-colors group">
                <div class="w-8 h-8 rounded-lg bg-emerald-500/10 flex items-center justify-center group-hover:bg-emerald-500/20 transition-colors">
                  <svg width="14" height="14" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" class="text-emerald-500"><path d="M12 5v14M19 12l-7 7-7-7"/></svg>
                </div>
                <div>
                  <p class="text-sm font-medium text-slate-700 dark:text-slate-300">Deposit Funds</p>
                  <p class="text-[10px] text-slate-400">Add money to account</p>
                </div>
              </a>
              <a href="Profile Settings.php" class="flex items-center gap-3 p-3 rounded-xl hover:bg-slate-50 dark:hover:bg-slate-800/50 transition-colors group">
                <div class="w-8 h-8 rounded-lg bg-amber-500/10 flex items-center justify-center group-hover:bg-amber-500/20 transition-colors">
                  <svg width="14" height="14" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" class="text-amber-500"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1 0 2.83 2 2 0 0 1-2.83 0l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-2 2 2 2 0 0 1-2-2v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83 0 2 2 0 0 1 0-2.83l.06-.06A1.65 1.65 0 0 0 4.67 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1-2-2 2 2 0 0 1 2-2h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 0-2.83 2 2 0 0 1 2.83 0l.06.06A1.65 1.65 0 0 0 9 4.67a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 2-2 2 2 0 0 1 2 2v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 0 2 2 0 0 1 0 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 2 2 2 2 0 0 1-2 2h-.09a1.65 1.65 0 0 0-1.51 1z"/></svg>
                </div>
                <div>
                  <p class="text-sm font-medium text-slate-700 dark:text-slate-300">Profile Settings</p>
                  <p class="text-[10px] text-slate-400">Update your info</p>
                </div>
              </a>
            </div>
          </div>

        </div>
      </div>

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

<!-- Live Chat Modal -->
<div id="live-chat-modal" class="fixed inset-0 z-50 hidden">
  <div class="absolute inset-0 bg-black/60 backdrop-blur-sm" onclick="document.getElementById('live-chat-modal').classList.add('hidden')"></div>
  <div class="absolute bottom-4 right-4 w-full max-w-sm">
    <div class="bg-white dark:bg-slate-900 rounded-2xl shadow-2xl border border-slate-200 dark:border-slate-700 overflow-hidden flex flex-col" style="height: 500px;">
      
      <!-- Chat Header -->
      <div class="bg-theme-gradient p-4 flex items-center justify-between">
        <div class="flex items-center gap-3">
          <div class="w-10 h-10 rounded-full bg-white/20 flex items-center justify-center">
            <svg width="20" height="20" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" class="text-white"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>
          </div>
          <div>
            <h4 class="text-sm font-bold text-white">Live Support</h4>
            <div class="flex items-center gap-1.5">
              <span class="w-2 h-2 rounded-full bg-emerald-400 animate-pulse"></span>
              <span class="text-xs text-white/80">Online</span>
            </div>
          </div>
        </div>
        <button onclick="document.getElementById('live-chat-modal').classList.add('hidden')" class="text-white/80 hover:text-white">
          <svg width="20" height="20" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M18 6L6 18M6 6l12 12"/></svg>
        </button>
      </div>
      
      <!-- Chat Messages -->
      <div id="chat-messages" class="flex-1 overflow-y-auto p-4 space-y-4 bg-slate-50 dark:bg-slate-950">
        <div class="flex items-start gap-2">
          <div class="w-8 h-8 rounded-full bg-theme flex items-center justify-center shrink-0">
            <svg width="14" height="14" fill="none" stroke="white" stroke-width="2" viewBox="0 0 24 24"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>
          </div>
          <div class="chat-bubble chat-bubble-left bg-white dark:bg-slate-800 rounded-2xl rounded-tl-none p-3 shadow-sm max-w-[80%]">
            <p class="text-xs text-slate-700 dark:text-slate-300">Hello <?= htmlspecialchars($currentUser['full_name'] ?? 'there') ?>! How can I help you today?</p>
          </div>
        </div>
      </div>
      
      <!-- Chat Input -->
      <div class="p-3 border-t border-slate-200 dark:border-slate-700 bg-white dark:bg-slate-900">
        <div class="flex gap-2">
          <input type="text" id="chat-input" placeholder="Type your message..." 
                 class="flex-1 bg-slate-100 dark:bg-slate-800 border border-slate-200 dark:border-slate-700 text-slate-900 dark:text-white text-sm rounded-xl px-4 py-2.5 outline-none focus:border-theme"
                 onkeypress="if(event.key==='Enter') sendChatMessage()">
          <button onclick="sendChatMessage()" class="p-2.5 bg-theme hover:brightness-110 text-white rounded-xl transition-all">
            <svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M22 2L11 13M22 2l-7 20-4-9-9-4 20-7z"/></svg>
          </button>
        </div>
      </div>
    </div>
  </div>
</div>

<!-- FOOTER -->
<?php include "include/footer.php"?>

<!-- Toast Notification -->
<div id="toast" class="fixed top-20 right-4 z-50 transform translate-x-full transition-transform duration-300">
    <div class="bg-slate-900 dark:bg-white text-white dark:text-slate-900 px-4 py-3 rounded-xl shadow-2xl flex items-center gap-2 text-sm font-medium">
        <svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" class="text-emerald-500"><path d="M20 6L9 17l-5-5"/></svg>
        <span id="toast-message">Success</span>
    </div>
</div>

<script>
function showToast(message) {
    const toast = document.getElementById('toast');
    document.getElementById('toast-message').textContent = message;
    toast.classList.remove('translate-x-full');
    setTimeout(() => toast.classList.add('translate-x-full'), 3000);
}

function openLiveChat() {
    document.getElementById('live-chat-modal').classList.remove('hidden');
    setTimeout(() => document.getElementById('chat-input').focus(), 100);
}

function sendChatMessage() {
    const input = document.getElementById('chat-input');
    const message = input.value.trim();
    if (!message) return;
    
    const container = document.getElementById('chat-messages');
    
    // User message
    const userHtml = `
        <div class="flex items-start gap-2 justify-end">
            <div class="chat-bubble chat-bubble-right bg-theme text-white rounded-2xl rounded-tr-none p-3 shadow-sm max-w-[80%]">
                <p class="text-xs">${escapeHtml(message)}</p>
            </div>
            <div class="w-8 h-8 rounded-full bg-slate-200 dark:bg-slate-700 flex items-center justify-center shrink-0 text-xs font-bold text-slate-600 dark:text-slate-300">
                <?= $initials ?>
            </div>
        </div>
    `;
    container.insertAdjacentHTML('beforeend', userHtml);
    input.value = '';
    container.scrollTop = container.scrollHeight;
    
    // Auto-reply simulation
    setTimeout(() => {
        const replies = [
            "Thank you for your message. A support agent will be with you shortly.",
            "I understand your concern. Could you provide more details about this issue?",
            "Our team is looking into this. Please hold on while I check your account.",
            "For faster resolution, you may also submit a ticket through the form above.",
            "Is there anything else I can help you with today?"
        ];
        const reply = replies[Math.floor(Math.random() * replies.length)];
        
        const agentHtml = `
            <div class="flex items-start gap-2">
                <div class="w-8 h-8 rounded-full bg-theme flex items-center justify-center shrink-0">
                    <svg width="14" height="14" fill="none" stroke="white" stroke-width="2" viewBox="0 0 24 24"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>
                </div>
                <div class="chat-bubble chat-bubble-left bg-white dark:bg-slate-800 rounded-2xl rounded-tl-none p-3 shadow-sm max-w-[80%]">
                    <p class="text-xs text-slate-700 dark:text-slate-300">${reply}</p>
                </div>
            </div>
        `;
        container.insertAdjacentHTML('beforeend', agentHtml);
        container.scrollTop = container.scrollHeight;
    }, 1500);
}

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

document.addEventListener('alpine:init', () => {
    Alpine.data('faqData', () => ({
        openFaq: null
    }));
});

document.addEventListener('DOMContentLoaded', () => {
    if (typeof lucide !== 'undefined') lucide.createIcons();
});
</script>

</body>
</html>