<?php
/**
 * قالب محصول تکی Dima Shop
 * نسخه: 2.0.0 - با پشتیبانی از گالری تصاویر
 */
if (!defined('ABSPATH')) exit;

global $post;
$product_id = get_the_ID();

// دریافت اطلاعات محصول
$price = Dima_Product::get_price($product_id);
$stock = Dima_Product::get_stock($product_id);
$sku = Dima_Product::get_sku($product_id);
$currency = get_option('dima_currency', 'تومان');
$is_in_stock = ($stock === '' || intval($stock) > 0);

// دسته‌بندی‌ها و ویژگی‌ها
$categories = wp_get_post_terms($product_id, 'dima_product_cat');
$attributes = wp_get_post_terms($product_id, 'dima_attribute');

// امتیازدهی و نظرات
$rating = get_post_meta($product_id, '_dima_avg_rating', true);
$review_count = get_post_meta($product_id, '_dima_review_count', true);

// Wishlist و Compare
$wishlist = Dima_Wishlist::get_instance();
$is_in_wishlist = $wishlist->is_in_wishlist($product_id);

// Flash Sale
$flash_sale = class_exists('Dima_Flash_Sale') ? Dima_Flash_Sale::get_instance()->get_flash_price($product_id) : null;
$has_flash_sale = !empty($flash_sale);

// گالری تصاویر
$gallery = class_exists('Dima_Gallery') ? Dima_Gallery::get_gallery($product_id) : array();
$has_gallery = !empty($gallery);

do_action('dima_before_single_product');
?>

<article id="product-<?php echo esc_attr($product_id); ?>" 
         class="dima-single-product" 
         itemscope 
         itemtype="https://schema.org/Product">
    
    <!-- Breadcrumbs -->
    <?php do_action('dima_before_content'); ?>
    
    <div class="product-layout">
        
        <!-- بخش گالری تصاویر -->
        <div class="product-gallery-section">
            
            <!-- تصویر اصلی -->
            <div class="main-image-container">
                <?php if (has_post_thumbnail()): ?>
                    <img src="<?php echo esc_url(get_the_post_thumbnail_url($product_id, 'large')); ?>" 
                         alt="<?php echo esc_attr(get_the_title()); ?>" 
                         itemprop="image"
                         id="main-product-image"
                         class="main-image">
                <?php else: ?>
                    <div class="no-image-placeholder">
                        <span>📦</span>
                        <p>تصویر محصول</p>
                    </div>
                <?php endif; ?>
                
                <!-- بج‌های روی تصویر -->
                <div class="image-badges">
                    <?php if ($has_flash_sale): 
                        $original_price = $price;
                        $sale_price = $flash_sale->sale_price;
                        $discount_percent = round((($original_price - $sale_price) / $original_price) * 100);
                    ?>
                        <span class="badge badge-sale">🔥 <?php echo $discount_percent; ?>٪ تخفیف</span>
                    <?php endif; ?>
                    
                    <?php if (!$is_in_stock): ?>
                        <span class="badge badge-out-of-stock">ناموجود</span>
                    <?php endif; ?>
                </div>
            </div>
            
            <!-- Thumbnails -->
            <?php if ($has_gallery || has_post_thumbnail()): ?>
            <div class="thumbnail-list">
                <?php if (has_post_thumbnail()): ?>
                    <button class="thumb-item active" data-image="<?php echo esc_url(get_the_post_thumbnail_url($product_id, 'large')); ?>">
                        <?php the_post_thumbnail('thumbnail'); ?>
                    </button>
                <?php endif; ?>
                
                <?php foreach ($gallery as $img): ?>
                <button class="thumb-item" data-image="<?php echo esc_url($img['full_url']); ?>">
                    <img src="<?php echo esc_url($img['thumb_url']); ?>" alt="<?php echo esc_attr($img['alt']); ?>">
                </button>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
            
        </div>
        
        <!-- بخش اطلاعات محصول -->
        <div class="product-info-section">
            
            <!-- دسته‌بندی -->
            <?php if (!empty($categories)): ?>
            <div class="product-categories">
                <?php foreach ($categories as $cat): 
                    $cat_data = Dima_Product::get_category_data($cat->term_id);
                ?>
                    <a href="<?php echo esc_url(get_term_link($cat)); ?>" 
                       class="category-tag" 
                       style="background: <?php echo esc_attr($cat_data['color']); ?>">
                        <?php echo esc_html($cat->name); ?>
                    </a>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
            
            <!-- عنوان -->
            <h1 class="product-title" itemprop="name"><?php the_title(); ?></h1>
            
            <!-- SKU -->
            <?php if ($sku): ?>
            <div class="product-sku">
                <span>کد محصول:</span>
                <strong itemprop="sku"><?php echo esc_html($sku); ?></strong>
            </div>
            <?php endif; ?>
            
            <!-- امتیازدهی -->
            <?php if ($rating): ?>
            <div class="product-rating-summary">
                <div class="stars-display">
                    <?php for ($i = 1; $i <= 5; $i++): ?>
                        <span class="star <?php echo $i <= round($rating) ? 'filled' : ''; ?>">★</span>
                    <?php endfor; ?>
                </div>
                <span class="rating-text">
                    <?php echo esc_html($rating); ?> از ۵ 
                    <span class="review-count">(<?php echo intval($review_count); ?> نظر)</span>
                </span>
                <a href="#reviews" class="view-reviews-link">مشاهده نظرات</a>
            </div>
            <?php endif; ?>
            
            <!-- قیمت -->
            <div class="product-price-box" itemprop="offers" itemscope itemtype="https://schema.org/Offer">
                <meta itemprop="priceCurrency" content="IRR">
                <meta itemprop="availability" content="<?php echo $is_in_stock ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock'; ?>">
                
                <?php if ($has_flash_sale): ?>
                    <div class="price-row sale-price">
                        <span class="price-label">قیمت ویژه:</span>
                        <span class="price-value" itemprop="price" content="<?php echo esc_attr($sale_price); ?>">
                            <?php echo number_format($sale_price); ?>
                        </span>
                        <span class="price-currency"><?php echo esc_html($currency); ?></span>
                    </div>
                    <div class="price-row original-price">
                        <span>قیمت اصلی:</span>
                        <del><?php echo number_format($original_price); ?> <?php echo esc_html($currency); ?></del>
                        <span class="discount-percent">-<?php echo $discount_percent; ?>٪</span>
                    </div>
                <?php else: ?>
                    <div class="price-row">
                        <span class="price-label">قیمت:</span>
                        <span class="price-value" itemprop="price" content="<?php echo esc_attr($price); ?>">
                            <?php echo number_format($price); ?>
                        </span>
                        <span class="price-currency"><?php echo esc_html($currency); ?></span>
                    </div>
                <?php endif; ?>
            </div>
            
            <!-- وضعیت موجودی -->
            <div class="product-stock-status">
                <?php if ($is_in_stock): ?>
                    <span class="stock-badge in-stock">
                        <span class="dashicons dashicons-yes-alt"></span>
                        موجود در انبار
                        <?php if ($stock !== '' && intval($stock) > 0): ?>
                            (<span itemprop="inventoryLevel"><?php echo intval($stock); ?></span> عدد)
                        <?php endif; ?>
                    </span>
                <?php else: ?>
                    <span class="stock-badge out-of-stock">
                        <span class="dashicons dashicons-dismiss"></span>
                        ناموجود
                    </span>
                <?php endif; ?>
            </div>
            
            <!-- توضیحات کوتاه -->
            <?php if (has_excerpt()): ?>
            <div class="product-short-description" itemprop="description">
                <?php the_excerpt(); ?>
            </div>
            <?php endif; ?>
            
            <!-- ویژگی‌ها -->
            <?php if (!empty($attributes)): ?>
            <div class="product-attributes">
                <h3>ویژگی‌ها:</h3>
                <ul class="attribute-list">
                    <?php foreach ($attributes as $attr): ?>
                        <li>
                            <span class="dashicons <?php echo esc_attr(get_term_meta($attr->term_id, 'category_icon', true) ?: 'dashicons-yes'); ?>"></span>
                            <?php echo esc_html($attr->name); ?>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>
            <?php endif; ?>
            
            <!-- دکمه‌های اکشن -->
            <div class="product-actions">
                <?php if ($is_in_stock): ?>
                    <button class="add-to-cart-btn dima-ripple" data-product-id="<?php echo esc_attr($product_id); ?>">
                        <span class="dashicons dashicons-cart"></span>
                        افزودن به سبد خرید
                    </button>
                <?php else: ?>
                    <button class="add-to-cart-btn disabled" disabled>
                        <span class="dashicons dashicons-dismiss"></span>
                        ناموجود
                    </button>
                <?php endif; ?>
                
                <button class="action-btn wishlist-btn <?php echo $is_in_wishlist ? 'active' : ''; ?>" 
                        data-product-id="<?php echo esc_attr($product_id); ?>" 
                        title="<?php echo $is_in_wishlist ? 'حذف از علاقه‌مندی‌ها' : 'افزودن به علاقه‌مندی‌ها'; ?>">
                    <span class="heart-icon"><?php echo $is_in_wishlist ? '♥' : '♡'; ?></span>
                </button>
                
                <button class="action-btn compare-btn" 
                        data-product-id="<?php echo esc_attr($product_id); ?>" 
                        title="افزودن به مقایسه">
                    ⚖️
                </button>
            </div>
            
            <!-- اشتراک‌گذاری -->
            <div class="product-share">
                <span>اشتراک‌گذاری:</span>
                <a href="https://t.me/share/url?url=<?php echo urlencode(get_permalink()); ?>&text=<?php echo urlencode(get_the_title()); ?>" 
                   target="_blank" rel="noopener noreferrer" class="share-btn telegram" title="تلگرام">
                    📱
                </a>
                <a href="https://wa.me/?text=<?php echo urlencode(get_the_title() . ' ' . get_permalink()); ?>" 
                   target="_blank" rel="noopener noreferrer" class="share-btn whatsapp" title="واتساپ">
                    💬
                </a>
                <a href="#" class="share-btn copy-link" onclick="copyProductLink(event)" title="کپی لینک">
                    🔗
                </a>
            </div>
            
        </div>
        
    </div>
    
    <!-- تب‌های توضیحات، مشخصات، نظرات -->
    <div class="product-tabs-section">
        <div class="tabs-header">
            <button class="tab-btn active" data-tab="description">توضیحات</button>
            <button class="tab-btn" data-tab="specs">مشخصات فنی</button>
            <button class="tab-btn" data-tab="reviews">نظرات (<?php echo intval($review_count); ?>)</button>
        </div>
        
        <div class="tabs-content">
            
            <!-- تب توضیحات -->
            <div class="tab-pane active" id="tab-description">
                <div class="tab-content-inner">
                    <?php the_content(); ?>
                </div>
            </div>
            
            <!-- تب مشخصات -->
            <div class="tab-pane" id="tab-specs">
                <div class="tab-content-inner">
                    <?php if (!empty($attributes)): ?>
                        <table class="specs-table">
                            <tbody>
                                <?php foreach ($attributes as $attr): ?>
                                <tr>
                                    <th><?php echo esc_html($attr->name); ?></th>
                                    <td>✓</td>
                                </tr>
                                <?php endforeach; ?>
                                <?php if ($sku): ?>
                                <tr><th>کد محصول (SKU)</th><td><?php echo esc_html($sku); ?></td></tr>
                                <?php endif; ?>
                                <?php if ($stock !== ''): ?>
                                <tr><th>موجودی</th><td><?php echo intval($stock) > 0 ? 'موجود' : 'ناموجود'; ?></td></tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    <?php else: ?>
                        <p>مشخصات فنی برای این محصول ثبت نشده است.</p>
                    <?php endif; ?>
                </div>
            </div>
            
            <!-- تب نظرات -->
            <div class="tab-pane" id="tab-reviews">
                <div class="tab-content-inner" id="reviews">
                    <?php
                    // دریافت نظرات
                    $reviews = class_exists('Dima_Review') ? Dima_Review::get_instance()->get_product_reviews($product_id, 20) : array();
                    ?>
                    
                    <?php if (empty($reviews)): ?>
                        <div class="no-reviews">
                            <span class="no-reviews-icon">💬</span>
                            <h3>هنوز نظری ثبت نشده است</h3>
                            <p>اولین نفری باشید که نظر می‌دهد!</p>
                        </div>
                    <?php else: ?>
                        <div class="reviews-list">
                            <?php foreach ($reviews as $review): ?>
                            <div class="review-item">
                                <div class="review-header">
                                    <div class="review-author">
                                        <div class="author-avatar">
                                            <?php echo mb_substr($review->author_name, 0, 1); ?>
                                        </div>
                                        <div class="author-info">
                                            <strong class="author-name"><?php echo esc_html($review->author_name); ?></strong>
                                            <?php if ($review->verified_purchase): ?>
                                                <span class="verified-badge">✓ خریدار تایید شده</span>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                    <div class="review-meta">
                                        <div class="review-stars">
                                            <?php for ($i = 1; $i <= 5; $i++): ?>
                                                <span class="star <?php echo $i <= $review->rating ? 'filled' : ''; ?>">★</span>
                                            <?php endfor; ?>
                                        </div>
                                        <span class="review-date"><?php echo date_i18n('j F Y', strtotime($review->created_at)); ?></span>
                                    </div>
                                </div>
                                
                                <?php if ($review->title): ?>
                                    <h4 class="review-title"><?php echo esc_html($review->title); ?></h4>
                                <?php endif; ?>
                                
                                <p class="review-content"><?php echo nl2br(esc_html($review->content)); ?></p>
                            </div>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                    
                    <!-- فرم ثبت نظر -->
                    <?php if (get_option('dima_reviews_enabled')): ?>
                    <div class="review-form-wrapper">
                        <h3>✍️ ثبت نظر جدید</h3>
                        <form class="review-form" id="review-form">
                            <div class="form-group">
                                <label>امتیاز شما:</label>
                                <div class="rating-input" id="rating-input">
                                    <?php for ($i = 5; $i >= 1; $i--): ?>
                                        <span class="rate-star" data-value="<?php echo $i; ?>">★</span>
                                    <?php endfor; ?>
                                </div>
                                <input type="hidden" name="rating" id="rating-value" value="5">
                            </div>
                            
                            <div class="form-group">
                                <label>عنوان نظر:</label>
                                <input type="text" name="title" placeholder="خلاصه نظر شما">
                            </div>
                            
                            <div class="form-group">
                                <label>متن نظر: *</label>
                                <textarea name="content" required placeholder="تجربه خود را با دیگران به اشتراک بگذارید..." rows="4"></textarea>
                            </div>
                            
                            <input type="hidden" name="product_id" value="<?php echo esc_attr($product_id); ?>">
                            <?php wp_nonce_field('dima_shop_nonce', 'nonce'); ?>
                            
                            <button type="submit" class="btn btn-primary submit-review-btn">ارسال نظر</button>
                        </form>
                    </div>
                    <?php endif; ?>
                    
                </div>
            </div>
            
        </div>
    </div>
    
    <!-- محصولات مرتبط -->
    <?php
    $related_args = array(
        'post_type' => 'dima_product',
        'posts_per_page' => 4,
        'post__not_in' => array($product_id),
        'orderby' => 'rand',
        'post_status' => 'publish'
    );
    
    // اگر دسته‌بندی دارد، محصولات هم‌دسته را نشان بده
    if (!empty($categories)) {
        $related_args['tax_query'] = array(array(
            'taxonomy' => 'dima_product_cat',
            'field' => 'term_id',
            'terms' => wp_list_pluck($categories, 'term_id')
        ));
    }
    
    $related_products = new WP_Query($related_args);
    
    if ($related_products->have_posts()):
    ?>
    <section class="related-products-section">
        <h2>محصولات مرتبط</h2>
        <div class="products-grid">
            <?php while ($related_products->have_posts()): $related_products->the_post(); 
                $rel_id = get_the_ID();
                $rel_price = Dima_Product::get_price($rel_id);
                $rel_stock = Dima_Product::get_stock($rel_id);
                $rel_is_in_stock = ($rel_stock === '' || intval($rel_stock) > 0);
            ?>
                <article class="product-card-mini">
                    <a href="<?php the_permalink(); ?>" class="product-thumb-link">
                        <?php if (has_post_thumbnail()): ?>
                            <img src="<?php echo esc_url(get_the_post_thumbnail_url($rel_id, 'medium')); ?>" alt="<?php echo esc_attr(get_the_title()); ?>">
                        <?php endif; ?>
                    </a>
                    <div class="product-info-mini">
                        <h3 class="product-title-mini">
                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                        </h3>
                        <div class="product-price-mini">
                            <?php echo number_format($rel_price); ?> <span><?php echo esc_html($currency); ?></span>
                        </div>
                        <?php if ($rel_is_in_stock): ?>
                            <button class="mini-add-to-cart" data-product-id="<?php echo esc_attr($rel_id); ?>">
                                <span class="dashicons dashicons-cart"></span>
                            </button>
                        <?php else: ?>
                            <span class="out-of-stock-text">ناموجود</span>
                        <?php endif; ?>
                    </div>
                </article>
            <?php endwhile; ?>
        </div>
    </section>
    <?php 
    endif; 
    wp_reset_postdata();
    ?>
    
</article>

<style>
/* ═══════════════════════════════════════════════════════════
   Single Product Styles
   ══════════════════════════════════════════════════════════ */
.dima-single-product {
    max-width: 1200px;
    margin: 0 auto;
    padding: 30px 20px;
    font-family: 'Vazir', Tahoma, sans-serif;
}

/* Layout */
.product-layout {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 40px;
    margin-bottom: 40px;
}

@media (max-width: 968px) {
    .product-layout {
        grid-template-columns: 1fr;
        gap: 30px;
    }
}

/* Gallery Section */
.product-gallery-section {
    position: sticky;
    top: 100px;
    align-self: start;
}

.main-image-container {
    position: relative;
    background: #f9fafb;
    border-radius: 16px;
    overflow: hidden;
    aspect-ratio: 1/1;
    margin-bottom: 15px;
    border: 1px solid #e5e7eb;
}

.main-image {
    width: 100%;
    height: 100%;
    object-fit: contain;
    transition: transform 0.3s ease;
}

.main-image:hover {
    transform: scale(1.05);
}

.no-image-placeholder {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: #9ca3af;
}

.no-image-placeholder span {
    font-size: 64px;
    margin-bottom: 10px;
}

/* Image Badges */
.image-badges {
    position: absolute;
    top: 15px;
    right: 15px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    z-index: 2;
}

.badge {
    padding: 6px 14px;
    border-radius: 20px;
    font-size: 13px;
    font-weight: 700;
    color: white;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.badge-sale {
    background: linear-gradient(135deg, #ef4444, #dc2626);
}

.badge-out-of-stock {
    background: linear-gradient(135deg, #6b7280, #4b5563);
}

/* Thumbnails */
.thumbnail-list {
    display: flex;
    gap: 10px;
    overflow-x: auto;
    padding-bottom: 5px;
}

.thumb-item {
    width: 80px;
    height: 80px;
    border: 2px solid #e5e7eb;
    border-radius: 10px;
    overflow: hidden;
    cursor: pointer;
    padding: 0;
    background: white;
    transition: all 0.2s;
    flex-shrink: 0;
}

.thumb-item:hover,
.thumb-item.active {
    border-color: #6366f1;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.2);
}

.thumb-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* Product Info Section */
.product-info-section {
    padding-top: 10px;
}

/* Categories */
.product-categories {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 15px;
}

.category-tag {
    padding: 4px 14px;
    border-radius: 20px;
    color: white;
    font-size: 12px;
    font-weight: 600;
    text-decoration: none;
    transition: opacity 0.2s;
}

.category-tag:hover {
    opacity: 0.85;
}

/* Title */
.product-title {
    font-size: 28px;
    font-weight: 800;
    color: #1f2937;
    margin: 0 0 12px 0;
    line-height: 1.4;
}

/* SKU */
.product-sku {
    font-size: 13px;
    color: #6b7280;
    margin-bottom: 16px;
}

.product-sku strong {
    color: #374151;
    font-family: monospace;
}

/* Rating */
.product-rating-summary {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 20px;
    flex-wrap: wrap;
}

.stars-display {
    display: flex;
    gap: 2px;
    color: #fbbf24;
    font-size: 18px;
}

.star.filled { color: #fbbf24; }
.star { color: #e5e7eb; }

.rating-text {
    font-size: 14px;
    color: #4b5563;
}

.review-count {
    color: #6b7280;
}

.view-reviews-link {
    font-size: 13px;
    color: #6366f1;
    text-decoration: underline;
}

/* Price Box */
.product-price-box {
    background: linear-gradient(135deg, #f0f4ff 0%, #ede9fe 100%);
    padding: 20px 24px;
    border-radius: 12px;
    margin-bottom: 20px;
    border: 1px solid #e0e7ff;
}

.price-row {
    display: flex;
    align-items: baseline;
    gap: 10px;
    margin-bottom: 8px;
}

.price-row:last-child {
    margin-bottom: 0;
}

.price-label {
    font-size: 14px;
    color: #4b5563;
}

.price-value {
    font-size: 32px;
    font-weight: 900;
    color: #6366f1;
}

.price-currency {
    font-size: 16px;
    font-weight: 600;
    color: #6b7280;
}

.original-price {
    font-size: 14px;
    color: #6b7280;
}

.original-price del {
    color: #9ca3af;
}

.discount-percent {
    background: #ef4444;
    color: white;
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: 700;
}

/* Stock Status */
.product-stock-status {
    margin-bottom: 20px;
}

.stock-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border-radius: 20px;
    font-size: 14px;
    font-weight: 600;
}

.in-stock {
    background: #d1fae5;
    color: #065f46;
}

.out-of-stock {
    background: #fee2e2;
    color: #991b1b;
}

/* Short Description */
.product-short-description {
    color: #4b5563;
    line-height: 1.8;
    margin-bottom: 20px;
    padding-bottom: 20px;
    border-bottom: 1px solid #e5e7eb;
}

/* Attributes */
.product-attributes {
    margin-bottom: 24px;
}

.product-attributes h3 {
    font-size: 16px;
    font-weight: 700;
    margin: 0 0 12px 0;
    color: #1f2937;
}

.attribute-list {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.attribute-list li {
    display: flex;
    align-items: center;
    gap: 6px;
    background: #f3f4f6;
    padding: 6px 14px;
    border-radius: 20px;
    font-size: 13px;
    border: 1px solid #e5e7eb;
    color: #374151;
}

/* Actions */
.product-actions {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
    flex-wrap: wrap;
}

.add-to-cart-btn {
    flex: 1;
    min-width: 200px;
    padding: 16px 24px;
    background: linear-gradient(135deg, #6366f1, #a855f7);
    color: white;
    border: none;
    border-radius: 12px;
    font-family: inherit;
    font-size: 16px;
    font-weight: 700;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    transition: all 0.2s;
    position: relative;
    overflow: hidden;
}

.add-to-cart-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(99, 102, 241, 0.35);
}

.add-to-cart-btn.disabled {
    background: #d1d5db;
    cursor: not-allowed;
    box-shadow: none;
}

.add-to-cart-btn.disabled:hover {
    transform: none;
}

.action-btn {
    width: 52px;
    height: 52px;
    border: 2px solid #e5e7eb;
    background: white;
    border-radius: 12px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    transition: all 0.2s;
}

.wishlist-btn {
    color: #d1d5db;
}

.wishlist-btn:hover,
.wishlist-btn.active {
    border-color: #ef4444;
    color: #ef4444;
    background: #fef2f2;
}

.compare-btn:hover {
    border-color: #6366f1;
    color: #6366f1;
    background: #eef2ff;
}

/* Share */
.product-share {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 13px;
    color: #6b7280;
    padding-top: 20px;
    border-top: 1px solid #e5e7eb;
}

.share-btn {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: #f3f4f6;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #4b5563;
    text-decoration: none;
    transition: all 0.2s;
    font-size: 16px;
}

.share-btn:hover {
    background: #6366f1;
    color: white;
    transform: translateY(-2px);
}

/* Tabs Section */
.product-tabs-section {
    background: white;
    border-radius: 16px;
    box-shadow: 0 2px 12px rgba(0,0,0,0.06);
    border: 1px solid #e5e7eb;
    margin-bottom: 40px;
    overflow: hidden;
}

.tabs-header {
    display: flex;
    border-bottom: 2px solid #e5e7eb;
    background: #f9fafb;
}

.tab-btn {
    flex: 1;
    padding: 16px 20px;
    background: transparent;
    border: none;
    font-family: inherit;
    font-size: 15px;
    font-weight: 600;
    color: #6b7280;
    cursor: pointer;
    transition: all 0.2s;
    position: relative;
}

.tab-btn:hover {
    color: #6366f1;
    background: #f3f4f6;
}

.tab-btn.active {
    color: #6366f1;
    background: white;
}

.tab-btn.active::after {
    content: '';
    position: absolute;
    bottom: -2px;
    left: 0;
    right: 0;
    height: 3px;
    background: #6366f1;
}

.tabs-content {
    padding: 30px;
}

.tab-pane {
    display: none;
}

.tab-pane.active {
    display: block;
}

.tab-content-inner {
    line-height: 1.8;
    color: #374151;
}

/* Specs Table */
.specs-table {
    width: 100%;
    border-collapse: collapse;
}

.specs-table th,
.specs-table td {
    padding: 12px 16px;
    text-align: right;
    border-bottom: 1px solid #f3f4f6;
}

.specs-table th {
    background: #f9fafb;
    font-weight: 600;
    color: #374151;
    width: 40%;
}

.specs-table tr:last-child th,
.specs-table tr:last-child td {
    border-bottom: none;
}

/* Reviews */
.no-reviews {
    text-align: center;
    padding: 50px 20px;
    background: #f9fafb;
    border-radius: 12px;
    border: 2px dashed #e5e7eb;
}

.no-reviews-icon {
    font-size: 56px;
    margin-bottom: 15px;
    opacity: 0.5;
}

.no-reviews h3 {
    margin: 10px 0;
    color: #374151;
}

.no-reviews p {
    color: #6b7280;
}

.reviews-list {
    display: flex;
    flex-direction: column;
    gap: 20px;
    margin-bottom: 30px;
}

.review-item {
    padding: 20px;
    background: #f9fafb;
    border-radius: 12px;
    border: 1px solid #e5e7eb;
}

.review-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 12px;
    flex-wrap: wrap;
    gap: 10px;
}

.review-author {
    display: flex;
    align-items: center;
    gap: 12px;
}

.author-avatar {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    background: linear-gradient(135deg, #6366f1, #a855f7);
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-weight: 700;
    font-size: 16px;
}

.author-info {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.author-name {
    font-weight: 700;
    color: #1f2937;
    font-size: 14px;
}

.verified-badge {
    font-size: 11px;
    color: #10b981;
    display: flex;
    align-items: center;
    gap: 3px;
}

.review-meta {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 4px;
}

.review-stars {
    color: #fbbf24;
    font-size: 14px;
}

.review-date {
    font-size: 12px;
    color: #6b7280;
}

.review-title {
    font-weight: 700;
    font-size: 15px;
    margin: 0 0 8px 0;
    color: #1f2937;
}

.review-content {
    color: #4b5563;
    font-size: 14px;
    line-height: 1.7;
    margin: 0;
}

/* Review Form */
.review-form-wrapper {
    margin-top: 30px;
    padding-top: 30px;
    border-top: 2px solid #e5e7eb;
}

.review-form-wrapper h3 {
    font-size: 18px;
    font-weight: 700;
    margin: 0 0 20px 0;
    color: #1f2937;
}

.review-form .form-group {
    margin-bottom: 15px;
}

.review-form label {
    display: block;
    font-weight: 600;
    font-size: 14px;
    margin-bottom: 6px;
    color: #374151;
}

.review-form input,
.review-form textarea {
    width: 100%;
    padding: 12px 14px;
    border: 2px solid #e5e7eb;
    border-radius: 10px;
    font-family: inherit;
    font-size: 14px;
    transition: all 0.2s;
}

.review-form input:focus,
.review-form textarea:focus {
    outline: none;
    border-color: #6366f1;
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

.rating-input {
    display: flex;
    gap: 8px;
    direction: ltr;
}

.rate-star {
    font-size: 32px;
    color: #e5e7eb;
    cursor: pointer;
    transition: all 0.2s;
}

.rate-star:hover,
.rate-star.active {
    color: #fbbf24;
    transform: scale(1.15);
}

.submit-review-btn {
    margin-top: 10px;
}

/* Related Products */
.related-products-section {
    margin-top: 40px;
}

.related-products-section h2 {
    font-size: 24px;
    font-weight: 800;
    color: #1f2937;
    margin: 0 0 20px 0;
    padding-bottom: 12px;
    border-bottom: 2px solid #e5e7eb;
}

.products-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 20px;
}

.product-card-mini {
    background: white;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0,0,0,0.06);
    border: 1px solid #e5e7eb;
    transition: all 0.2s;
}

.product-card-mini:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 20px rgba(0,0,0,0.1);
}

.product-thumb-link {
    display: block;
    aspect-ratio: 1/1;
    overflow: hidden;
}

.product-thumb-link img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.3s;
}

.product-card-mini:hover .product-thumb-link img {
    transform: scale(1.05);
}

.product-info-mini {
    padding: 15px;
}

.product-title-mini {
    font-size: 14px;
    font-weight: 700;
    margin: 0 0 8px 0;
    line-height: 1.4;
}

.product-title-mini a {
    color: #1f2937;
    text-decoration: none;
}

.product-title-mini a:hover {
    color: #6366f1;
}

.product-price-mini {
    font-size: 16px;
    font-weight: 800;
    color: #6366f1;
    margin-bottom: 10px;
}

.product-price-mini span {
    font-size: 12px;
    font-weight: 500;
    color: #6b7280;
}

.mini-add-to-cart {
    width: 100%;
    padding: 10px;
    background: #f3f4f6;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 18px;
    transition: all 0.2s;
}

.mini-add-to-cart:hover {
    background: #6366f1;
    color: white;
}

.out-of-stock-text {
    display: block;
    text-align: center;
    padding: 10px;
    background: #fee2e2;
    color: #991b1b;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 600;
}

/* Ripple Effect */
.dima-ripple {
    position: relative;
    overflow: hidden;
}

.dima-ripple::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 0;
    height: 0;
    border-radius: 50%;
    background: rgba(255,255,255,0.3);
    transform: translate(-50%, -50%);
    transition: width 0.6s, height 0.6s;
}

.dima-ripple:active::after {
    width: 300px;
    height: 300px;
}
</style>

<script>
jQuery(document).ready(function($) {
    
    // تغییر تصویر اصلی با کلیک روی thumbnail
    $('.thumb-item').on('click', function() {
        var newImage = $(this).data('image');
        $('#main-product-image').attr('src', newImage);
        $('.thumb-item').removeClass('active');
        $(this).addClass('active');
    });
    
    // تب‌ها
    $('.tab-btn').on('click', function() {
        var tabId = $(this).data('tab');
        $('.tab-btn').removeClass('active');
        $(this).addClass('active');
        $('.tab-pane').removeClass('active');
        $('#tab-' + tabId).addClass('active');
    });
    
    // امتیازدهی در فرم نظر
    $('.rate-star').on('click', function() {
        var val = $(this).data('value');
        $('#rating-value').val(val);
        $('.rate-star').removeClass('active');
        $(this).addClass('active').prevAll().addClass('active');
    });
    
    $('.rate-star').on('mouseenter', function() {
        $(this).addClass('active').prevAll().addClass('active');
    }).on('mouseleave', function() {
        if (!$('#rating-value').val()) {
            $('.rate-star').removeClass('active');
        }
    });
    
    // ارسال نظر
    $('#review-form').on('submit', function(e) {
        e.preventDefault();
        var $form = $(this);
        var $btn = $form.find('.submit-review-btn');
        
        $btn.prop('disabled', true).text('در حال ارسال...');
        
        $.ajax({
            url: dima_shop.ajax_url,
            type: 'POST',
            data: $form.serialize() + '&action=dima_submit_review',
            success: function(response) {
                if (response.success) {
                    alert('✅ ' + response.data.message);
                    $form[0].reset();
                    $('.rate-star').removeClass('active');
                    location.reload();
                } else {
                    alert('❌ ' + response.data.message);
                }
            },
            complete: function() {
                $btn.prop('disabled', false).text('ارسال نظر');
            }
        });
    });
    
    // کپی لینک محصول
    window.copyProductLink = function(e) {
        e.preventDefault();
        navigator.clipboard.writeText(window.location.href).then(function() {
            alert('لینک محصول کپی شد!');
        });
    };
    
});
</script>

<?php do_action('dima_after_single_product'); ?>