<?php
/**
 * Product Detail Page - PREMIUM VERSION
 * Digital Marketplace
 */

require_once __DIR__ . '/config/database.php';

// Get product slug
$slug = $_GET['slug'] ?? '';

if (empty($slug)) {
    set_flash_message('Product not found.', 'error');
    header('Location: ' . BASE_URL . '/products.php');
    exit;
}

// Get product
$product = get_product_by_slug($slug);

if (!$product) {
    set_flash_message('Product not found.', 'error');
    header('Location: ' . BASE_URL . '/products.php');
    exit;
}

// Increment product views
increment_product_views($product['id']);

$page_title = $product['name'];

// Get related products
$related_products = get_related_products($product['id'], $product['category_id'], 4);

// Get product images
$product_images = get_product_images($product['id']);

// Get product licenses
$licenses = get_product_licenses($product['id']);

// Get product reviews
$reviews = get_product_reviews($product['id']);

// Get user's existing negotiation for this product
$user_negotiation = null;
if (is_user_logged_in()) {
    $stmt = $pdo->prepare("SELECT * FROM negotiations WHERE product_id = ? AND user_id = ? ORDER BY created_at DESC LIMIT 1");
    $stmt->execute([$product['id'], get_current_user_id()]);
    $user_negotiation = $stmt->fetch();
}

// Handle add to cart
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_to_cart'])) {
    $quantity = max(1, (int)($_POST['quantity'] ?? 1));
    $license_type = sanitize($_POST['license_type'] ?? 'regular');
    
    if (add_to_cart($product['id'], $quantity, $license_type)) {
        set_flash_message('Product added to cart!', 'success');
        header('Location: ' . BASE_URL . '/cart.php');
        exit;
    } else {
        set_flash_message('Failed to add to cart.', 'error');
    }
}

// Handle negotiation
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['negotiate'])) {
    if (!is_user_logged_in()) {
        set_flash_message('Please login to make an offer.', 'warning');
        $_SESSION['redirect_after_login'] = $_SERVER['REQUEST_URI'];
        header('Location: ' . BASE_URL . '/login.php');
        exit;
    }
    
    $offer_price = (float)($_POST['offer_price'] ?? 0);
    $message = sanitize($_POST['message'] ?? '');
    
    if ($offer_price > 0) {
        if (create_negotiation($product['id'], get_current_user_id(), $offer_price, $message)) {
            set_flash_message('Your offer has been sent! We will review it shortly.', 'success');
            header('Location: ' . $_SERVER['REQUEST_URI']);
            exit;
        } else {
            set_flash_message('Failed to send offer. Please try again.', 'error');
        }
    } else {
        set_flash_message('Please enter a valid offer price.', 'error');
    }
}

// Handle review submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit_review'])) {
    if (!is_user_logged_in()) {
        set_flash_message('Please login to submit a review.', 'warning');
        header('Location: ' . BASE_URL . '/login.php');
        exit;
    }
    
    $rating = (int)($_POST['rating'] ?? 0);
    $review_text = sanitize($_POST['review'] ?? '');
    
    // Check if user has purchased this product
    $stmt = $pdo->prepare("SELECT COUNT(*) FROM orders o JOIN order_items oi ON o.id = oi.order_id 
                          WHERE o.user_id = ? AND oi.product_id = ? AND o.payment_status = 'paid'");
    $stmt->execute([get_current_user_id(), $product['id']]);
    $has_purchased = $stmt->fetchColumn() > 0;
    
    if (!$has_purchased) {
        set_flash_message('You can only review products you have purchased.', 'error');
    } elseif ($rating < 1 || $rating > 5) {
        set_flash_message('Please select a rating.', 'error');
    } elseif (empty($review_text)) {
        set_flash_message('Please write a review.', 'error');
    } else {
        if (add_review($product['id'], get_current_user_id(), $rating, $review_text)) {
            update_product_rating($product['id']);
            set_flash_message('Thank you for your review! It will be visible after approval.', 'success');
            header('Location: ' . $_SERVER['REQUEST_URI']);
            exit;
        } else {
            set_flash_message('Failed to submit review. Please try again.', 'error');
        }
    }
}

include __DIR__ . '/includes/header.php';
?>

<!-- Product Detail Section -->
<section class="section">
    <div class="container">
        <!-- Breadcrumb -->
        <nav aria-label="breadcrumb" class="mb-4">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="<?php echo BASE_URL; ?>/index.php">Home</a></li>
                <li class="breadcrumb-item"><a href="<?php echo BASE_URL; ?>/products.php">Products</a></li>
                <?php if ($product['category_name']): ?>
                <li class="breadcrumb-item">
                    <a href="<?php echo BASE_URL; ?>/products.php?category=<?php echo $product['category_slug']; ?>">
                        <?php echo sanitize($product['category_name']); ?>
                    </a>
                </li>
                <?php endif; ?>
                <li class="breadcrumb-item active" aria-current="page"><?php echo sanitize($product['name']); ?></li>
            </ol>
        </nav>
        
        <div class="row g-5">
            <!-- Product Images -->
            <div class="col-lg-6">
                <div class="product-gallery" data-aos="fade-right">
                    <div class="product-main-image-wrapper mb-3">
                        <img src="<?php echo get_product_image_url($product); ?>" 
                             alt="<?php echo sanitize($product['name']); ?>"
                             class="product-main-image img-fluid rounded"
                             id="mainImage"
                             onerror="this.src='<?php echo ASSETS_URL; ?>/images/no-image.jpg'">
                    </div>
                    
                    <?php if (!empty($product_images)): ?>
                    <div class="product-thumbnails d-flex gap-2">
                        <div class="product-thumbnail active" data-src="<?php echo get_product_image_url($product); ?>">
                            <img src="<?php echo get_product_image_url($product); ?>" alt="">
                        </div>
                        <?php foreach ($product_images as $image): ?>
                        <div class="product-thumbnail" data-src="<?php echo BASE_URL . '/' . $image['image']; ?>">
                            <img src="<?php echo BASE_URL . '/' . $image['image']; ?>" alt="">
                        </div>
                        <?php endforeach; ?>
                    </div>
                    <?php endif; ?>
                    
                    <?php if (!empty($product['youtube_url'])): ?>
                        <?php $embed_url = get_youtube_embed_url($product['youtube_url']); ?>
                        <?php if ($embed_url): ?>
                        <div class="product-video mt-4">
                            <h6 class="mb-3"><i class="fab fa-youtube me-2 text-danger"></i>Product Preview</h6>
                            <div class="ratio ratio-16x9 rounded">
                                <iframe src="<?php echo htmlspecialchars($embed_url); ?>" 
                                        frameborder="0"
                                        allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
                                        allowfullscreen>
                                </iframe>
                            </div>
                        </div>
                        <?php endif; ?>
                    <?php endif; ?>
                </div>
            </div>
            
            <!-- Product Info -->
            <div class="col-lg-6">
                <div class="product-detail" data-aos="fade-left">
                    <?php if ($product['category_name']): ?>
                    <span class="badge bg-primary mb-3">
                        <i class="fas fa-tag me-1"></i><?php echo sanitize($product['category_name']); ?>
                    </span>
                    <?php endif; ?>
                    
                    <h1 class="product-title mb-3"><?php echo sanitize($product['name']); ?></h1>
                    
                    <div class="product-meta mb-4">
                        <div class="d-flex align-items-center gap-4 flex-wrap">
                            <div class="product-rating">
                                <?php for ($i = 1; $i <= 5; $i++): ?>
                                    <i class="fas fa-star<?php echo $i > ($product['avg_rating'] ?? 5) ? '-half-alt' : ''; ?>"></i>
                                <?php endfor; ?>
                                <span class="ms-2">(<?php echo $product['review_count'] ?? 0; ?> reviews)</span>
                            </div>
                            <div class="product-sales">
                                <i class="fas fa-shopping-cart me-1"></i><?php echo number_format($product['times_sold'] ?? 0); ?> sales
                            </div>
                            <div class="product-views">
                                <i class="fas fa-eye me-1"></i><?php echo number_format($product['views'] ?? 0); ?> views
                            </div>
                        </div>
                    </div>
                    
                    <div class="product-price mb-4" id="priceDisplay">
                        <?php if ($product['sale_price'] && $product['sale_price'] > 0): ?>
                            <span class="display-4 fw-bold text-primary price-current" data-price="<?php echo $product['sale_price']; ?>">
                                <?php echo format_price($product['sale_price']); ?>
                            </span>
                            <span class="h4 text-muted text-decoration-line-through ms-3 price-original">
                                <?php echo format_price($product['price']); ?>
                            </span>
                            <span class="badge bg-danger ms-2">-<?php echo calculate_discount($product['price'], $product['sale_price']); ?>%</span>
                        <?php else: ?>
                            <span class="display-4 fw-bold text-primary price-current" data-price="<?php echo $product['price']; ?>">
                                <?php echo format_price($product['price']); ?>
                            </span>
                        <?php endif; ?>
                    </div>
                    
                    <div class="product-description mb-4">
                        <p class="lead"><?php echo nl2br(sanitize($product['short_description'])); ?></p>
                    </div>
                    
                    <!-- Features -->
                    <div class="product-features mb-4">
                        <div class="row g-3">
                            <div class="col-6">
                                <div class="d-flex align-items-center">
                                    <i class="fas fa-download text-primary me-2"></i>
                                    <span>Instant Download</span>
                                </div>
                            </div>
                            <div class="col-6">
                                <div class="d-flex align-items-center">
                                    <i class="fas fa-shield-alt text-primary me-2"></i>
                                    <span>Secure Payment</span>
                                </div>
                            </div>
                            <div class="col-6">
                                <div class="d-flex align-items-center">
                                    <i class="fas fa-undo text-primary me-2"></i>
                                    <span>30-Day Refund</span>
                                </div>
                            </div>
                            <div class="col-6">
                                <div class="d-flex align-items-center">
                                    <i class="fas fa-headset text-primary me-2"></i>
                                    <span>24/7 Support</span>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Add to Cart Form -->
                    <form method="POST" class="product-form mb-4">
                        <?php echo csrf_field(); ?>
                        
                        <!-- License Type -->
                        <?php if (!empty($licenses) || $product['regular_license_price'] || $product['extended_license_price']): ?>
                        <div class="mb-4">
                            <label class="form-label fw-bold">Select License</label>
                            <div class="license-options">
                                <?php if ($product['regular_license_price']): ?>
                                <div class="license-option">
                                    <input type="radio" name="license_type" id="license_regular" value="regular" 
                                           data-price="<?php echo $product['regular_license_price']; ?>"
                                           data-original-price="<?php echo $product['price']; ?>"
                                           checked>
                                    <label for="license_regular">
                                        <div class="license-header">
                                            <span class="license-name">Regular License</span>
                                            <span class="license-price"><?php echo format_price($product['regular_license_price']); ?></span>
                                        </div>
                                        <div class="license-description">For personal or client project</div>
                                    </label>
                                </div>
                                <?php endif; ?>
                                
                                <?php if ($product['extended_license_price']): ?>
                                <div class="license-option">
                                    <input type="radio" name="license_type" id="license_extended" value="extended"
                                           data-price="<?php echo $product['extended_license_price']; ?>"
                                           data-original-price="<?php echo $product['price'] * 5; ?>">
                                    <label for="license_extended">
                                        <div class="license-header">
                                            <span class="license-name">Extended License</span>
                                            <span class="license-price"><?php echo format_price($product['extended_license_price']); ?></span>
                                        </div>
                                        <div class="license-description">For resale or redistribution</div>
                                    </label>
                                </div>
                                <?php endif; ?>
                                
                                <?php foreach ($licenses as $license): ?>
                                <div class="license-option">
                                    <input type="radio" name="license_type" id="license_<?php echo $license['id']; ?>" value="<?php echo $license['license_type']; ?>"
                                           data-price="<?php echo $license['price']; ?>">
                                    <label for="license_<?php echo $license['id']; ?>">
                                        <div class="license-header">
                                            <span class="license-name"><?php echo sanitize($license['license_type']); ?> License</span>
                                            <span class="license-price"><?php echo format_price($license['price']); ?></span>
                                        </div>
                                        <?php if ($license['description']): ?>
                                        <div class="license-description"><?php echo sanitize($license['description']); ?></div>
                                        <?php endif; ?>
                                    </label>
                                </div>
                                <?php endforeach; ?>
                            </div>
                        </div>
                        <?php endif; ?>
                        
                        <!-- Quantity -->
                        <div class="mb-4">
                            <label class="form-label fw-bold">Quantity</label>
                            <div class="quantity-selector">
                                <button type="button" class="quantity-btn" data-action="decrease">
                                    <i class="fas fa-minus"></i>
                                </button>
                                <input type="number" name="quantity" class="quantity-input" value="1" min="1" max="99">
                                <button type="button" class="quantity-btn" data-action="increase">
                                    <i class="fas fa-plus"></i>
                                </button>
                            </div>
                        </div>
                        
                        <!-- Actions -->
                        <div class="d-flex gap-3">
                            <button type="submit" name="add_to_cart" class="btn btn-primary btn-lg flex-grow-1 add-to-cart-btn" data-product-id="<?php echo $product['id']; ?>">
                                <i class="fas fa-shopping-cart me-2"></i>Add to Cart
                            </button>
                            <?php if (is_user_logged_in()): ?>
                            <button type="button" class="wishlist-btn btn btn-secondary btn-lg<?php echo is_in_wishlist($product['id']) ? ' active' : ''; ?>" data-product-id="<?php echo $product['id']; ?>">
                                <i class="<?php echo is_in_wishlist($product['id']) ? 'fas' : 'far'; ?> fa-heart"></i>
                            </button>
                            <?php endif; ?>
                        </div>
                    </form>
                    
                    <!-- Demo & Documentation Links -->
                    <?php if ($product['demo_url'] || $product['documentation_url']): ?>
                    <div class="product-links mb-4">
                        <?php if ($product['demo_url']): ?>
                        <a href="<?php echo $product['demo_url']; ?>" target="_blank" rel="noopener" class="btn btn-outline-light me-2">
                            <i class="fas fa-external-link-alt me-2"></i>Live Demo
                        </a>
                        <?php endif; ?>
                        <?php if ($product['documentation_url']): ?>
                        <a href="<?php echo $product['documentation_url']; ?>" target="_blank" rel="noopener" class="btn btn-outline-light">
                            <i class="fas fa-book me-2"></i>Documentation
                        </a>
                        <?php endif; ?>
                    </div>
                    <?php endif; ?>
                    
                    <!-- Negotiation -->
                    <?php if ($product['allow_negotiation']): ?>
                    <div class="negotiation-section">
                        <button class="btn btn-outline-success w-100" type="button" data-bs-toggle="collapse" data-bs-target="#negotiateForm">
                            <i class="fas fa-hand-holding-usd me-2"></i>Make an Offer
                        </button>
                        <div class="collapse mt-3" id="negotiateForm">
                            <div class="card card-body bg-dark-card">
                                <?php if ($user_negotiation): ?>
                                    <div class="alert alert-info">
                                        <strong>Your Offer Status:</strong> 
                                        <span class="badge bg-<?php echo $user_negotiation['status'] === 'pending' ? 'warning' : ($user_negotiation['status'] === 'accepted' ? 'success' : 'danger'); ?>">
                                            <?php echo ucfirst($user_negotiation['status']); ?>
                                        </span>
                                        <?php if ($user_negotiation['admin_message']): ?>
                                            <p class="mt-2 mb-0"><strong>Message:</strong> <?php echo sanitize($user_negotiation['admin_message']); ?></p>
                                        <?php endif; ?>
                                    </div>
                                <?php else: ?>
                                    <form method="POST">
                                        <?php echo csrf_field(); ?>
                                        <div class="mb-3">
                                            <label class="form-label">Your Offer Price</label>
                                            <div class="input-group">
                                                <span class="input-group-text"><?php echo CURRENCY_SYMBOL; ?></span>
                                                <input type="number" name="offer_price" class="form-control" step="0.01" min="1" max="<?php echo $product['price']; ?>" required>
                                            </div>
                                            <small class="text-muted">Original price: <?php echo format_price($product['price']); ?></small>
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label">Message (Optional)</label>
                                            <textarea name="message" class="form-control" rows="2" placeholder="Why should we accept your offer?"></textarea>
                                        </div>
                                        <button type="submit" name="negotiate" class="btn btn-success w-100">
                                            <i class="fas fa-paper-plane me-2"></i>Send Offer
                                        </button>
                                    </form>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        
        <!-- Product Tabs -->
        <div class="row mt-5">
            <div class="col-12">
                <ul class="nav nav-tabs" id="productTabs" role="tablist">
                    <li class="nav-item" role="presentation">
                        <button class="nav-link active" id="description-tab" data-bs-toggle="tab" data-bs-target="#description" type="button" role="tab">
                            <i class="fas fa-align-left me-2"></i>Description
                        </button>
                    </li>
                    <?php if ($product['changelog']): ?>
                    <li class="nav-item" role="presentation">
                        <button class="nav-link" id="changelog-tab" data-bs-toggle="tab" data-bs-target="#changelog" type="button" role="tab">
                            <i class="fas fa-history me-2"></i>Changelog
                        </button>
                    </li>
                    <?php endif; ?>
                    <li class="nav-item" role="presentation">
                        <button class="nav-link" id="reviews-tab" data-bs-toggle="tab" data-bs-target="#reviews" type="button" role="tab">
                            <i class="fas fa-star me-2"></i>Reviews (<?php echo count($reviews); ?>)
                        </button>
                    </li>
                </ul>
                
                <div class="tab-content p-4 bg-dark-card border border-top-0 rounded-bottom" id="productTabsContent">
                    <!-- Description Tab -->
                    <div class="tab-pane fade show active" id="description" role="tabpanel">
                        <div class="product-full-description">
                            <?php echo nl2br(sanitize($product['description'])); ?>
                        </div>
                        
                        <div class="product-details-table mt-4">
                            <h5>Product Details</h5>
                            <table class="table table-dark table-borderless">
                                <tr>
                                    <td width="200"><strong>SKU:</strong></td>
                                    <td><?php echo sanitize($product['sku'] ?: 'N/A'); ?></td>
                                </tr>
                                <tr>
                                    <td><strong>Category:</strong></td>
                                    <td><?php echo sanitize($product['category_name'] ?: 'Uncategorized'); ?></td>
                                </tr>
                                <tr>
                                    <td><strong>Version:</strong></td>
                                    <td><?php echo sanitize($product['version'] ?: '1.0'); ?></td>
                                </tr>
                                <tr>
                                    <td><strong>File Type:</strong></td>
                                    <td><?php echo sanitize($product['file_type'] ?: 'Digital Download'); ?></td>
                                </tr>
                                <tr>
                                    <td><strong>Last Updated:</strong></td>
                                    <td><?php echo format_date($product['updated_at']); ?></td>
                                </tr>
                            </table>
                        </div>
                    </div>
                    
                    <?php if ($product['changelog']): ?>
                    <!-- Changelog Tab -->
                    <div class="tab-pane fade" id="changelog" role="tabpanel">
                        <div class="product-changelog">
                            <?php echo nl2br(sanitize($product['changelog'])); ?>
                        </div>
                    </div>
                    <?php endif; ?>
                    
                    <!-- Reviews Tab -->
                    <div class="tab-pane fade" id="reviews" role="tabpanel">
                        <div class="row">
                            <div class="col-lg-8">
                                <h5 class="mb-4">Customer Reviews</h5>
                                
                                <?php if (empty($reviews)): ?>
                                    <div class="alert alert-info">No reviews yet. Be the first to review!</div>
                                <?php else: ?>
                                    <?php foreach ($reviews as $review): ?>
                                    <div class="review-item mb-4 pb-4 border-bottom border-secondary">
                                        <div class="d-flex align-items-center mb-2">
                                            <div class="review-avatar me-3">
                                                <?php echo strtoupper(substr($review['user_name'], 0, 1)); ?>
                                            </div>
                                            <div>
                                                <strong><?php echo sanitize($review['user_name']); ?></strong>
                                                <div class="review-rating">
                                                    <?php for ($i = 1; $i <= 5; $i++): ?>
                                                        <i class="fas fa-star<?php echo $i > $review['rating'] ? '-half-alt' : ''; ?>"></i>
                                                    <?php endfor; ?>
                                                </div>
                                            </div>
                                            <small class="text-muted ms-auto"><?php echo time_ago($review['created_at']); ?></small>
                                        </div>
                                        <p class="mb-0"><?php echo sanitize($review['review']); ?></p>
                                    </div>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            </div>
                            
                            <div class="col-lg-4">
                                <div class="card bg-dark-light">
                                    <div class="card-body">
                                        <h6 class="mb-3">Write a Review</h6>
                                        <?php if (is_user_logged_in()): ?>
                                            <?php 
                                            // Check if user has purchased
                                            $stmt = $pdo->prepare("SELECT COUNT(*) FROM orders o JOIN order_items oi ON o.id = oi.order_id 
                                                                  WHERE o.user_id = ? AND oi.product_id = ? AND o.payment_status = 'paid'");
                                            $stmt->execute([get_current_user_id(), $product['id']]);
                                            $has_purchased = $stmt->fetchColumn() > 0;
                                            ?>
                                            <?php if ($has_purchased): ?>
                                                <form method="POST">
                                                    <?php echo csrf_field(); ?>
                                                    <div class="mb-3">
                                                        <label class="form-label">Rating</label>
                                                        <div class="rating-input">
                                                            <?php for ($i = 5; $i >= 1; $i--): ?>
                                                            <input type="radio" name="rating" id="star<?php echo $i; ?>" value="<?php echo $i; ?>">
                                                            <label for="star<?php echo $i; ?>"><i class="fas fa-star"></i></label>
                                                            <?php endfor; ?>
                                                        </div>
                                                    </div>
                                                    <div class="mb-3">
                                                        <label class="form-label">Your Review</label>
                                                        <textarea name="review" class="form-control" rows="4" placeholder="Share your experience..." required></textarea>
                                                    </div>
                                                    <button type="submit" name="submit_review" class="btn btn-primary w-100">
                                                        <i class="fas fa-paper-plane me-2"></i>Submit Review
                                                    </button>
                                                </form>
                                            <?php else: ?>
                                                <div class="alert alert-info">
                                                    <i class="fas fa-info-circle me-2"></i>
                                                    You can only review products you have purchased.
                                                </div>
                                            <?php endif; ?>
                                        <?php else: ?>
                                            <div class="alert alert-info">
                                                <i class="fas fa-info-circle me-2"></i>
                                                Please <a href="<?php echo BASE_URL; ?>/login.php">login</a> to write a review.
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Related Products -->
        <?php if (!empty($related_products)): ?>
        <div class="row mt-5">
            <div class="col-12">
                <h3 class="mb-4">Related Products</h3>
                <div class="row g-4">
                    <?php foreach ($related_products as $related): ?>
                    <div class="col-lg-3 col-md-4 col-sm-6">
                        <div class="product-card">
                            <div class="product-image-wrapper">
                                <img src="<?php echo get_product_image_url($related); ?>" 
                                     alt="<?php echo sanitize($related['name']); ?>"
                                     onerror="this.src='<?php echo ASSETS_URL; ?>/images/no-image.jpg'">
                            </div>
                            <div class="product-body">
                                <h6 class="product-title">
                                    <a href="<?php echo BASE_URL; ?>/product.php?slug=<?php echo $related['slug']; ?>">
                                        <?php echo sanitize($related['name']); ?>
                                    </a>
                                </h6>
                                <div class="product-price">
                                    <span class="price-current">
                                        <?php echo format_price($related['sale_price'] ?: $related['price']); ?>
                                    </span>
                                </div>
                                <a href="<?php echo BASE_URL; ?>/product.php?slug=<?php echo $related['slug']; ?>" class="btn btn-sm btn-primary w-100">
                                    View Details
                                </a>
                            </div>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
        <?php endif; ?>
    </div>
</section>

<style>
/* Product Gallery */
.product-main-image-wrapper {
    background: var(--dark);
    border-radius: var(--radius-lg);
    overflow: hidden;
}

.product-main-image {
    width: 100%;
    height: auto;
    transition: var(--transition);
}

.product-thumbnails {
    flex-wrap: wrap;
}

.product-thumbnail {
    width: 80px;
    height: 80px;
    border-radius: var(--radius);
    overflow: hidden;
    cursor: pointer;
    border: 2px solid transparent;
    transition: var(--transition);
}

.product-thumbnail.active,
.product-thumbnail:hover {
    border-color: var(--primary);
}

.product-thumbnail img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* Product Detail */
.product-detail .product-title {
    font-size: 2rem;
    font-weight: 700;
}

.product-detail .product-rating {
    color: var(--accent);
}

/* License Options */
.license-options {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.license-option {
    position: relative;
}

.license-option input {
    position: absolute;
    opacity: 0;
}

.license-option label {
    display: block;
    padding: 16px;
    background: var(--dark-lighter);
    border: 2px solid var(--dark-lighter);
    border-radius: var(--radius);
    cursor: pointer;
    transition: var(--transition);
}

.license-option input:checked + label {
    border-color: var(--primary);
    background: rgba(139, 92, 246, 0.1);
}

.license-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 4px;
}

.license-name {
    font-weight: 600;
    color: var(--white);
}

.license-price {
    font-weight: 700;
    color: var(--primary-light);
}

.license-description {
    font-size: 0.875rem;
    color: var(--gray-500);
}

/* Quantity Selector */
.quantity-selector {
    display: inline-flex;
    align-items: center;
    background: var(--dark-lighter);
    border-radius: var(--radius);
    overflow: hidden;
}

.quantity-btn {
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    color: var(--gray-400);
    cursor: pointer;
    transition: var(--transition);
}

.quantity-btn:hover {
    background: var(--dark-light);
    color: var(--white);
}

.quantity-input {
    width: 60px;
    height: 44px;
    background: transparent;
    border: none;
    color: var(--white);
    text-align: center;
    font-weight: 600;
    -moz-appearance: textfield;
}

.quantity-input::-webkit-outer-spin-button,
.quantity-input::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

/* Product Tabs */
.nav-tabs {
    border-bottom: 1px solid var(--dark-lighter);
}

.nav-tabs .nav-link {
    color: var(--gray-400);
    border: none;
    padding: 1rem 1.5rem;
    font-weight: 500;
}

.nav-tabs .nav-link:hover {
    color: var(--white);
    border: none;
}

.nav-tabs .nav-link.active {
    color: var(--primary);
    background: var(--dark-card);
    border: none;
    border-bottom: 2px solid var(--primary);
}

/* Reviews */
.review-item {
    border-color: var(--dark-lighter) !important;
}

.review-avatar {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background: var(--gradient-primary);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    color: var(--white);
}

.review-rating {
    color: var(--accent);
    font-size: 0.875rem;
}

/* Rating Input */
.rating-input {
    display: flex;
    flex-direction: row-reverse;
    justify-content: flex-end;
    gap: 8px;
}

.rating-input input {
    display: none;
}

.rating-input label {
    cursor: pointer;
    font-size: 1.5rem;
    color: var(--gray-600);
    transition: var(--transition);
}

.rating-input label:hover,
.rating-input label:hover ~ label,
.rating-input input:checked ~ label {
    color: var(--accent);
}
</style>

<?php include __DIR__ . '/includes/footer.php'; ?>
