<?php
/**
 * Image analytics page for PinnerPress
 * This file displays image-level analytics for posts
 */

// If this file is called directly, abort.
if (!defined('WPINC')) {
    die;
}

// Check if analytics is enabled
$analytics_enabled = get_option('pinnerpress_analytics_enabled', '1') === '1';

if (!$analytics_enabled) {
    ?>
    <div class="wrap pinnerpress-admin">
        <h1><?php echo esc_html__('PinnerPress Image Analytics', 'pinnerpress'); ?></h1>

        <div class="pinnerpress-admin-header">
            <div class="pinnerpress-logo">
                <?php // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedImage -- Plugin logo asset ?>
                <img src="<?php echo esc_url(plugin_dir_url(dirname(__FILE__)) . 'images/pinnerpress-logo.png'); ?>" alt="<?php echo esc_attr__('PinnerPress Logo', 'pinnerpress'); ?>">
            </div>
            <div class="pinnerpress-header-content">
                <h2><?php esc_html_e('PinnerPress Image Analytics', 'pinnerpress'); ?></h2>
                <p><?php esc_html_e('Track individual image performance and Pinterest engagement.', 'pinnerpress'); ?></p>
            </div>
        </div>

        <div class="pinnerpress-analytics-disabled-notice" style="background: #fff; border: 1px solid #c3c4c7; border-radius: 4px; padding: 60px 40px; text-align: center; margin: 20px auto 0; max-width: 800px;">
            <span class="dashicons dashicons-chart-bar" style="font-size: 48px; width: 48px; height: 48px; color: #c3c4c7; margin-bottom: 20px;"></span>
            <h2 style="font-size: 24px; margin: 0 0 15px 0; color: #1d2327;"><?php esc_html_e('Analytics Disabled', 'pinnerpress'); ?></h2>
            <p style="font-size: 14px; color: #646970; margin: 0 0 25px 0; max-width: 500px; margin-left: auto; margin-right: auto;">
                <?php esc_html_e('Analytics tracking is currently turned off. Enable analytics to start collecting data about your Pinterest engagement.', 'pinnerpress'); ?>
            </p>
            <a href="<?php echo esc_url(admin_url('admin.php?page=pinnerpress-settings&tab=general#analytics-settings')); ?>" class="button button-primary button-hero">
                <?php esc_html_e('Enable Analytics in Settings', 'pinnerpress'); ?>
            </a>
        </div>
    </div>
    <?php
    return;
}

// Check if user has premium access
$is_premium = function_exists('pinnerpress_is_premium') ? pinnerpress_is_premium() : false;

// Check if user can use premium features (includes trial users)
$can_use_premium_features = function_exists('pinnerpress_can_use_premium_features') ? pinnerpress_can_use_premium_features() : false;

// Check if user has premium analytics access
$has_premium_analytics = pinnerpress_is_feature_enabled('analytics_unlimited');
$has_truly_premium = function_exists('pinnerpress_is_truly_premium') ? pinnerpress_is_truly_premium() : false;

// Set default date range
$default_end_date = current_time('Y-m-d');
$default_start_date = gmdate('Y-m-d', strtotime('-30 days', strtotime($default_end_date)));

?>

<div class="wrap pinnerpress-admin pinnerpress-image-analytics-page" data-server-date="<?php echo esc_attr($default_end_date); ?>">
    <h1><?php echo esc_html(get_admin_page_title()); ?></h1>
    
    <div class="pinnerpress-admin-header">
        <div class="pinnerpress-logo">
            <?php // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedImage -- Plugin logo asset ?>
            <img src="<?php echo esc_url(plugin_dir_url(dirname(__FILE__)) . 'images/pinnerpress-logo.png'); ?>" alt="<?php echo esc_attr__('PinnerPress Logo', 'pinnerpress'); ?>">
        </div>
        <div class="pinnerpress-header-content">
            <h2><?php esc_html_e('PinnerPress Image Analytics', 'pinnerpress'); ?></h2>
            <p><?php esc_html_e('Track individual image performance and Pinterest engagement.', 'pinnerpress'); ?></p>
        </div>
    </div>

<style>
.pinnerpress-analytics-limitation {
    border-left: 4px solid #E60023;
    padding: 12px;
    background-color: #fef4f4;
    margin: 20px 0;
}

.pinnerpress-analytics-limitation h4 {
    margin-top: 0;
    color: #E60023;
}

.pinnerpress-analytics-limitation p {
    margin-bottom: 10px;
}
</style>

    <div class="pinnerpress-admin-content">
        <?php if (!$can_use_premium_features) : ?>
            <?php 
            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Function returns properly escaped HTML
            echo pinnerpress_get_upgrade_notification(); 
            ?>
        <?php endif; ?>
        
        <!-- Date Controls -->
        <div class="pinnerpress-analytics-controls">
            <div class="pinnerpress-date-controls-row">
                <div class="pinnerpress-date-inputs">
                    <label for="pinnerpress-start-date"><?php esc_html_e('Start Date:', 'pinnerpress'); ?></label>
                    <input type="date" id="pinnerpress-start-date" value="<?php echo esc_attr($default_start_date); ?>" <?php echo !$can_use_premium_features ? 'disabled' : ''; ?> />
                    
                    <label for="pinnerpress-end-date"><?php esc_html_e('End Date:', 'pinnerpress'); ?></label>
                    <input type="date" id="pinnerpress-end-date" value="<?php echo esc_attr($default_end_date); ?>" <?php echo !$can_use_premium_features ? 'disabled' : ''; ?> />
                    
                    <button type="button" id="pinnerpress-update-chart" class="button button-primary" <?php echo !$can_use_premium_features ? 'disabled' : ''; ?>>
                        <?php esc_html_e('Update Chart', 'pinnerpress'); ?>
                    </button>
                    
                    <?php if (!$can_use_premium_features): ?>
                        <span class="pinnerpress-locked-icon" title="<?php esc_attr_e('Premium feature - upgrade to unlock custom date ranges', 'pinnerpress'); ?>">🔒</span>
                    <?php endif; ?>
                </div>
                
                <div class="pinnerpress-date-presets">
                    <button class="pinnerpress-date-preset" data-days="7"><?php esc_html_e('Last 7 Days', 'pinnerpress'); ?></button>
                    <button class="pinnerpress-date-preset active" data-days="30"><?php esc_html_e('Last 30 Days', 'pinnerpress'); ?></button>
                    <button class="pinnerpress-date-preset" data-days="90" <?php if (!$has_truly_premium) echo 'disabled'; ?>><?php esc_html_e('Last 90 Days', 'pinnerpress'); ?></button>
                </div>
            </div>
        </div>
        
        <!-- Post Selection Controls - Separate Row -->
        <div class="pinnerpress-post-controls">
            <div class="pinnerpress-post-selector-controls">
                <label for="post-select"><?php esc_html_e('Select Post:', 'pinnerpress'); ?></label>
                <select id="post-select" class="pinnerpress-post-select" style="width: 300px;">
                    <option value=""><?php esc_html_e('-- Select a Post --', 'pinnerpress'); ?></option>
                    <?php
                    // Always use Select2 for dynamic loading - don't pre-load any posts
                    // This keeps the initial page load fast and uses AJAX for all post loading
                    ?>
                </select>
            </div>
        </div>

        <!-- Main Content Area -->
        <div class="pinnerpress-content">
            <!-- Selected Post Info -->
            <div class="selected-post-info" style="display:none;">
                <h3 id="selected-post-title"></h3>
                <a href="#" id="selected-post-link" target="_blank" class="button button-secondary">
                    <span class="dashicons dashicons-external" style="vertical-align: middle;"></span>
                    <?php esc_html_e('View Post', 'pinnerpress'); ?>
                </a>
            </div>

            <!-- Post Analytics Summary -->
            <div class="post-analytics-summary" style="display:none;">
                <h3><?php esc_html_e('Post Analytics', 'pinnerpress'); ?></h3>
                <div class="post-stats-grid">
                    <div class="stat-card">
                        <h4><?php esc_html_e('Pin Clicks', 'pinnerpress'); ?></h4>
                        <p class="stat-value" id="post-pin-clicks">0</p>
                    </div>
                    <div class="stat-card">
                        <h4><?php esc_html_e('Banner Clicks', 'pinnerpress'); ?></h4>
                        <p class="stat-value" id="post-banner-clicks">0</p>
                    </div>
                    <div class="stat-card">
                        <h4><?php esc_html_e('Follow Clicks', 'pinnerpress'); ?></h4>
                        <p class="stat-value" id="post-follow-clicks">0</p>
                    </div>
                    <div class="stat-card">
                        <h4><?php esc_html_e('Callout Clicks', 'pinnerpress'); ?></h4>
                        <p class="stat-value" id="post-callout-clicks">0</p>
                    </div>
                </div>
            </div>

        <!-- Summary Stats -->
        <div class="summary-stats" style="display:none;">
            <div class="stat-card">
                <p class="stat-label"><?php esc_html_e('Total Images', 'pinnerpress'); ?></p>
                <p class="stat-value" id="total-images">0</p>
            </div>
            <div class="stat-card">
                <p class="stat-label"><?php esc_html_e('Total Clicks', 'pinnerpress'); ?></p>
                <p class="stat-value" id="total-clicks">0</p>
            </div>
            <div class="stat-card">
                <p class="stat-label"><?php esc_html_e('Best Performing', 'pinnerpress'); ?></p>
                <p class="stat-value" id="best-image">-</p>
            </div>
            <div class="stat-card">
                <p class="stat-label"><?php esc_html_e('Average Clicks/Image', 'pinnerpress'); ?></p>
                <p class="stat-value" id="avg-clicks">0</p>
            </div>
        </div>

        <!-- View Toggle and Export Controls -->
        <div class="analytics-controls" style="margin-bottom: 20px;">
            <div class="analytics-controls-right" style="margin-left: auto;">
                <div class="view-toggle">
                    <button type="button" class="view-toggle-btn" data-view="table" aria-pressed="true">
                        <span class="dashicons dashicons-list-view"></span>
                        <?php esc_html_e('Table', 'pinnerpress'); ?>
                    </button>
                    <button type="button" class="view-toggle-btn" data-view="grid" aria-pressed="false">
                        <span class="dashicons dashicons-grid-view"></span>
                        <?php esc_html_e('Grid', 'pinnerpress'); ?>
                    </button>
                </div>
                <button type="button"
                        class="button pinnerpress-export-button <?php echo pinnerpress_is_feature_enabled('analytics_export_csv') ? '' : 'pinnerpress-premium-disabled'; ?>"
                        id="export-csv"
                        <?php echo !pinnerpress_is_feature_enabled('analytics_export_csv') ? 'disabled title="' . esc_attr__('Upgrade to Premium for CSV export', 'pinnerpress') . '"' : ''; ?>>
                    <?php esc_html_e('Export CSV', 'pinnerpress'); ?>
                    <?php if (!pinnerpress_is_feature_enabled('analytics_export_csv')): ?>
                        <span class="pinnerpress-premium-badge">PRO</span>
                    <?php endif; ?>
                </button>
            </div>
        </div>
            
        <!-- Image Analytics Container -->
        <div class="image-analytics-container" style="display:none;">
                
                <!-- Table View -->
                <div class="image-table-container" style="display:block;">
                    <table class="wp-list-table widefat fixed striped">
                        <thead>
                            <tr>
                                <th class="column-preview" style="width: 80px;"><?php esc_html_e('Image', 'pinnerpress'); ?></th>
                                <th class="column-image-id sortable" data-sort="id">
                                    <a href="#">
                                        <?php esc_html_e('Image ID', 'pinnerpress'); ?>
                                        <span class="sorting-indicator"></span>
                                    </a>
                                </th>
                                <?php /* DISABLED: Impressions tracking removed
                                <th class="column-impressions sortable" data-sort="impressions" style="width: 100px;">
                                    <a href="#">
                                        <?php esc_html_e('Impressions', 'pinnerpress'); ?>
                                        <span class="sorting-indicator"></span>
                                    </a>
                                </th>
                                */ ?>
                                <th class="column-pin-clicks sortable" data-sort="pin-clicks" style="width: 80px;">
                                    <a href="#">
                                        <?php esc_html_e('Pin Clicks', 'pinnerpress'); ?>
                                        <span class="sorting-indicator"></span>
                                    </a>
                                </th>
                                <?php /* DISABLED: Pin CTR requires impressions
                                <th class="column-pin-ctr sortable" data-sort="pin-ctr" style="width: 80px;">
                                    <a href="#">
                                        <?php esc_html_e('Pin CTR', 'pinnerpress'); ?>
                                        <span class="sorting-indicator"></span>
                                    </a>
                                </th>
                                */ ?>
                                <th class="column-banner-clicks sortable" data-sort="banner-clicks" style="width: 80px;">
                                    <a href="#">
                                        <?php esc_html_e('Banner Clicks', 'pinnerpress'); ?>
                                        <span class="sorting-indicator"></span>
                                    </a>
                                </th>
                                <?php /* DISABLED: Banner CTR requires impressions
                                <th class="column-banner-ctr sortable" data-sort="banner-ctr" style="width: 80px;">
                                    <a href="#">
                                        <?php esc_html_e('Image Banner CTR', 'pinnerpress'); ?>
                                        <span class="sorting-indicator"></span>
                                    </a>
                                </th>
                                */ ?>
                                <?php /* DISABLED: Conversion rate requires impressions
                                <th class="column-conversion sortable" data-sort="conversion" style="width: 100px;">
                                    <a href="#" title="<?php esc_attr_e('Overall conversion rate for all clicks', 'pinnerpress'); ?>">
                                        <?php esc_html_e('Conversion Rate', 'pinnerpress'); ?>
                                        <span class="sorting-indicator"></span>
                                    </a>
                                </th>
                                */ ?>
                                <th class="column-actions" style="width: 140px;"><?php esc_html_e('Actions', 'pinnerpress'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <!-- Dynamically populated -->
                        </tbody>
                    </table>
                </div>
                
                <!-- Grid View -->
                <div class="image-grid-container" style="display:none;">
                    <!-- Dynamically populated -->
                </div>
            </div>
            
            <!-- No Data Message -->
            <div class="no-data-message">
                <p><?php esc_html_e('Select a post to view image analytics', 'pinnerpress'); ?></p>
            </div>
        </div>

    <!-- Image Detail Modal -->
    <div class="image-detail-modal" style="display: none;">
        <div class="modal-content">
            <span class="close">&times;</span>
            <div class="modal-body">
                <!-- Content will be dynamically inserted here -->
            </div>
        </div>
    </div>
</div>

<!-- Add nonce field -->
<?php wp_nonce_field('pinnerpress_analytics_nonce', 'pinnerpress_analytics_nonce'); ?>

<script>
// Localize variables for JavaScript
var pinnerpressVars = {
    ajaxurl: '<?php echo esc_js(admin_url('admin-ajax.php')); ?>',
    nonce: '<?php echo esc_js(wp_create_nonce('pinnerpress_analytics_nonce')); ?>',
    debug_enabled: <?php echo wp_json_encode(WP_DEBUG); ?>,
    pluginUrl: '<?php echo esc_js(plugin_dir_url(dirname(__FILE__))); ?>'
};

// Premium analytics feature check
const hasPremiumAnalytics = <?php echo wp_json_encode($has_premium_analytics); ?>;
</script>

<script>
jQuery(document).ready(function($) {
    // Core functionality for all users
    
    // Initialize date range based on user type
    if (!hasPremiumAnalytics) {
        // Set 7-day button as active for free users
        $('.pinnerpress-date-preset[data-days="7"]').addClass('active');
    }
    
    // Date preset button handlers (works for all users)
    $('.pinnerpress-date-preset').on('click', function() {
        const days = parseInt($(this).data('days'));
        
        // Check if this is a premium feature and user doesn't have premium
        if (!hasPremiumAnalytics && (days === 30 || days === 90)) {
            // Show upgrade message for disabled buttons
            alert('<?php echo esc_js(__('This feature requires PinnerPress Premium. Upgrade to unlock extended analytics periods.', 'pinnerpress')); ?>');
            return;
        }
        
        // Remove active class from all buttons
        $('.pinnerpress-date-preset').removeClass('active');
        // Add active class to clicked button
        $(this).addClass('active');
        
        const endDate = new Date();
        const startDate = new Date(endDate.getTime() - (days * 24 * 60 * 60 * 1000));
        
        const startDateStr = startDate.toISOString().split('T')[0];
        const endDateStr = endDate.toISOString().split('T')[0];
        
        $('#pinnerpress-start-date').val(startDateStr);
        $('#pinnerpress-end-date').val(endDateStr);
        
        // Trigger data refresh if a post is selected
        if ($('#post-select').val()) {
            // This would trigger the image analytics refresh
            // The actual implementation will be in pinnerpress-image-analytics.js
            $(document).trigger('pinnerpress-refresh-image-analytics');
        }
    });
    
    <?php if ($can_use_premium_features): ?>
    // Premium-only functionality
    
    // Update chart button (only for premium users)
    $('#pinnerpress-update-chart').on('click', function() {
        // Trigger data refresh if a post is selected
        if ($('#post-select').val()) {
            $(document).trigger('pinnerpress-refresh-image-analytics');
        }
    });
    <?php endif; ?>
    
});
</script>

<style>
.pinnerpress-image-analytics-page .pinnerpress-admin-content {
    display: block; /* Stack rows like Post Analytics */
}

.pinnerpress-image-analytics-page {
    /* Image analytics specific styles */
}

/* Header styles are inherited from main admin CSS */

.pinnerpress-analytics-container {
    margin-top: 20px;
}

.pinnerpress-analytics-controls {
    background: #fff;
    border: 1px solid #ccd0d4;
    border-radius: 4px;
    padding: 20px;
    margin-bottom: 20px;
}

.pinnerpress-post-controls {
    background: #fff;
    border: 1px solid #ccd0d4;
    border-radius: 4px;
    padding: 20px;
    margin-bottom: 20px;
}

.pinnerpress-date-controls {
    margin-bottom: 0;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 15px;
}

.pinnerpress-date-controls label {
    font-weight: 600;
    white-space: nowrap;
}

.pinnerpress-date-controls input[type="date"] {
    margin-right: 5px;
}

.pinnerpress-free-date-controls {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}

.pinnerpress-free-date-controls input[type="date"]:disabled {
    background-color: #f1f1f1;
    color: #666;
    cursor: not-allowed;
}

.pinnerpress-locked-icon {
    font-size: 16px;
    cursor: help;
    color: #d63638;
}

.pinnerpress-date-presets {
    display: flex;
    gap: 8px;
    align-items: center;
}

/* Layout fixes to match Post Analytics */
.pinnerpress-date-controls-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 15px;
}

.pinnerpress-date-inputs {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}

.pinnerpress-date-inputs label {
    font-weight: 400;
    color: #23282d;
    white-space: nowrap;
}

.pinnerpress-date-inputs input[type="date"] {
    padding: 3px 8px;
    border: 1px solid #7e8993;
    border-radius: 4px;
}

.pinnerpress-post-selector-section {
    background: #fff;
    border: 1px solid #ccd0d4;
    border-radius: 4px;
    padding: 20px;
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    gap: 15px;
}

.pinnerpress-post-selector-section label {
    font-weight: 400;
    color: #23282d;
    white-space: nowrap;
}

.pinnerpress-helper-text {
    color: #666;
    font-style: italic;
    margin-left: auto;
}

/* Table Controls */
.pinnerpress-table-controls {
    background: #fff;
    border: 1px solid #ccd0d4;
    border-radius: 4px;
    padding: 15px 20px;
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 15px;
}

.analytics-controls-left {
    display: flex;
    align-items: center;
    gap: 10px;
}

.analytics-controls-left label {
    font-weight: 400;
    color: #23282d;
}

.analytics-controls-left select {
    padding: 3px 8px;
    border: 1px solid #7e8993;
    border-radius: 4px;
}

.analytics-controls-right {
    display: flex;
    align-items: center;
    gap: 10px;
}

.pinnerpress-date-preset {
    position: relative;
    height: auto;
    min-height: 32px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 6px 12px;
    line-height: 1.4;
    white-space: nowrap;
}

.pinnerpress-date-preset.active {
    background-color: #E60023;
    color: white;
}

.pinnerpress-date-preset {
    background-color: #fff !important;
    color: #E60023 !important;
    border: 1px solid #E60023 !important;
    transition: all 0.3s ease;
    text-decoration: none !important;
    box-shadow: none !important;
}

.pinnerpress-date-preset:hover:not(.pinnerpress-premium-disabled) {
    background-color: #E60023 !important;
    color: white !important;
    border-color: #E60023 !important;
}

.pinnerpress-date-preset.active {
    background-color: #E60023 !important;
    color: white !important;
    border-color: #E60023 !important;
}

.pinnerpress-date-preset.pinnerpress-premium-disabled {
    background-color: #f1f1f1 !important;
    color: #666 !important;
    cursor: not-allowed !important;
    border-color: #ddd !important;
    opacity: 0.6;
}

.pinnerpress-date-preset.pinnerpress-premium-disabled .pinnerpress-premium-badge {
    opacity: 1;
}

/* Additional specificity for WordPress button overrides */
.pinnerpress-analytics-controls .pinnerpress-date-preset.button {
    background-color: #fff !important;
    color: #E60023 !important;
    border: 1px solid #E60023 !important;
}

.pinnerpress-analytics-controls .pinnerpress-date-preset.button:hover:not(.pinnerpress-premium-disabled) {
    background-color: #E60023 !important;
    color: white !important;
    border-color: #E60023 !important;
}

.pinnerpress-analytics-controls .pinnerpress-date-preset.button.active {
    background-color: #E60023 !important;
    color: white !important;
    border-color: #E60023 !important;
}

.pinnerpress-post-selector-controls {
    display: flex;
    align-items: center;
    gap: 15px;
    flex-wrap: wrap;
}

.pinnerpress-post-selector-controls label {
    font-weight: 600;
    white-space: nowrap;
}

.pinnerpress-analytics-limitation {
    border-left: 4px solid #E60023;
    padding: 12px;
    background-color: #fef4f4;
    margin: 20px 0;
}

.pinnerpress-analytics-limitation h4 {
    margin-top: 0;
    color: #E60023;
}

.pinnerpress-analytics-limitation p {
    margin-bottom: 10px;
}

.summary-stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
    margin-bottom: 30px;
}

.stat-card {
    background: #fff;
    padding: 20px;
    border: 1px solid #ccd0d4;
    text-align: center;
}

.stat-card .stat-label {
    margin: 0 0 10px 0;
    color: #23282d;
    font-size: 14px;
    font-weight: 600;
    text-transform: uppercase;
}

.stat-value {
    font-size: 32px;
    font-weight: 600; /* match bolder red style */
    color: #E60023;
    margin: 0;
}

/* Premium Feature Styles */
.pinnerpress-premium-message {
    margin: 20px 0;
}

.premium-features-box {
    background: #fff;
    border: 2px solid #E60023;
    border-radius: 8px;
    padding: 30px;
    text-align: center;
    max-width: 600px;
    margin: 0 auto;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.premium-features-box h3 {
    color: #E60023;
    font-size: 24px;
    margin-bottom: 15px;
    margin-top: 0;
}

.premium-features-box p {
    font-size: 16px;
    color: #666;
    margin-bottom: 20px;
}

.premium-features-box ul {
    text-align: left;
    margin: 20px auto;
    padding-left: 20px;
    display: inline-block;
    width: auto;
}

.premium-features-box li {
    margin-bottom: 8px;
    color: #666;
    font-size: 15px;
}

.premium-features-box .button-primary {
    font-size: 16px;
    padding: 12px 24px;
    height: auto;
    margin-top: 20px;
    display: block;
    width: fit-content;
    margin-left: auto;
    margin-right: auto;
    background-color: #E60023 !important;
    border-color: #E60023 !important;
    color: white !important;
    transition: all 0.3s ease;
}

.premium-features-box .button-primary:hover {
    background-color: #c7001e !important;
    border-color: #c7001e !important;
}

/* PinnerPress Button Styling for Image Analytics */
#pinnerpress-update-chart {
    background-color: #E60023 !important;
    border-color: #E60023 !important;
    color: white !important;
    transition: all 0.3s ease;
}

#pinnerpress-update-chart:hover {
    background-color: #c7001e !important;
    border-color: #c7001e !important;
}

#export-csv {
    background-color: #fff !important;
    color: #E60023 !important;
    border: 1px solid #E60023 !important;
    transition: all 0.3s ease;
}

#export-csv:hover:not(:disabled) {
    background-color: #E60023 !important;
    color: white !important;
    border-color: #E60023 !important;
}

#export-csv:disabled {
    background-color: #f1f1f1 !important;
    color: #666 !important;
    border-color: #ddd !important;
    cursor: not-allowed;
}

.selected-post-info .button-secondary {
    background-color: #fff !important;
    color: #E60023 !important;
    border: 1px solid #E60023 !important;
    transition: all 0.3s ease;
}

.selected-post-info .button-secondary:hover {
    background-color: #E60023 !important;
    color: white !important;
    border-color: #E60023 !important;
}

.pinnerpress-analytics-limitation .button-primary {
    background-color: #E60023 !important;
    border-color: #E60023 !important;
    color: white !important;
    transition: all 0.3s ease;
}

.pinnerpress-analytics-limitation .button-primary:hover {
    background-color: #c7001e !important;
    border-color: #c7001e !important;
}

.pinnerpress-premium-disabled .stat-value {
    color: #ccc;
}

.image-analytics-grid {
    display: block;
}

.selected-post-info {
    background: #f8f9fa;
    border: 1px solid #e1e3e5;
    border-radius: 4px;
    padding: 20px;
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.selected-post-info h3 {
    margin: 0;
    color: #23282d;
    font-size: 18px;
    font-weight: 600;
    flex: 1;
}

.selected-post-info .button {
    margin-left: 20px;
    white-space: nowrap;
}

.image-grid-header {
    background: #fff;
    border: 1px solid #ccd0d4;
    border-radius: 4px;
    padding: 15px 20px;
    margin-bottom: 20px;
}

.filter-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 20px;
}

.sort-controls {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}

.sort-controls label {
    font-weight: 600;
    white-space: nowrap;
}

.sort-controls select {
    min-width: 180px;
}

.view-controls {
    display: flex;
    align-items: center;
}

.view-controls label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
}

.image-grid-container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
    max-width: 1200px;
}

.image-card {
    background: #fff;
    border: 1px solid #ccd0d4;
    border-radius: 4px;
    overflow: hidden;
    transition: transform 0.2s, box-shadow 0.2s;
    cursor: pointer;
}

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

.image-card.no-clicks {
    opacity: 0.7;
    border-color: #ddd;
}

.image-card .image-preview {
    position: relative;
    overflow: hidden;
}

.image-card .image-preview::after {
    content: '\f177';
    font-family: dashicons;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(0, 0, 0, 0.7);
    color: white;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    opacity: 0;
    transition: opacity 0.2s;
}

.image-card:hover .image-preview::after {
    opacity: 1;
}

.image-card img {
    width: 100%;
    height: 200px;
    object-fit: cover;
    display: block;
    image-rendering: auto;
    image-rendering: -webkit-optimize-contrast;
    image-rendering: crisp-edges;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
}

.image-card-body {
    padding: 15px;
}

.image-name {
    font-size: 12px;
    color: #666;
    word-break: break-all;
    margin-bottom: 10px;
    max-height: 40px;
    overflow: hidden;
    font-weight: 600;
}

.image-card.no-clicks .image-name {
    color: #999;
}

.image-stats {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-top: 10px;
}

.stat-item {
    text-align: center;
}

.stat-label {
    font-size: 11px;
    color: #666;
    text-transform: uppercase;
}

.stat-number {
    font-size: 20px;
    font-weight: 600;
    color: #E60023;
}

.image-card.no-clicks .stat-number {
    color: #999;
}

.filter-info {
    grid-column: 1 / -1;
    text-align: center;
    color: #666;
    font-style: italic;
}

.no-data-message {
    background: #fff;
    padding: 60px;
    text-align: center;
    border: 1px solid #ccd0d4;
}

.no-data-message p {
    font-size: 16px;
    color: #666;
}

/* Modal styles */
.image-detail-modal {
    position: fixed;
    z-index: 100000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0,0,0,0.5);
}

.modal-content {
    background-color: #fefefe;
    margin: 5% auto;
    padding: 0;
    border: 1px solid #888;
    width: 90%;
    max-width: 800px;
    border-radius: 4px;
}



.modal-body {
    padding: 20px;
}

.close {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
    padding: 10px 20px;
    cursor: pointer;
}

.close:hover,
.close:focus {
    color: #000;
}

/* Loading spinner */
.pinnerpress-spinner {
    display: inline-block;
    width: 20px;
    height: 20px;
    border: 3px solid rgba(0,0,0,.1);
    border-radius: 50%;
    border-top-color: #E60023;
    animation: spin 1s ease-in-out infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Select2 styling improvements */
.select2-container {
    min-width: 300px !important;
}

.select2-container--default .select2-selection--single {
    height: 36px;
    line-height: 34px;
}

.select2-container--default .select2-selection--single .select2-selection__rendered {
    line-height: 34px;
}

.select2-container--default .select2-selection--single .select2-selection__arrow {
    height: 34px;
}

.select2-dropdown {
    z-index: 100100; /* Ensure dropdown appears above other elements */
}

/* High-DPI display optimizations */
@media (-webkit-min-device-pixel-ratio: 1.5), (min-resolution: 144dpi) {
    .image-card img {
        -ms-interpolation-mode: bicubic;
        image-rendering: -webkit-optimize-contrast;
        image-rendering: optimize-contrast;
    }
}

/* Responsive design for mobile devices */
@media (max-width: 782px) {
    .pinnerpress-admin-header {
        flex-direction: column;
        text-align: center;
    }
    
    .pinnerpress-logo {
        margin-right: 0;
        margin-bottom: 15px;
    }
    
    .pinnerpress-date-controls {
        flex-direction: column;
        align-items: stretch;
        gap: 15px;
    }
    
    .pinnerpress-date-presets {
        margin-left: 0;
        justify-content: center;
    }
    
    .pinnerpress-post-selector-controls {
        flex-direction: column;
        align-items: stretch;
        gap: 15px;
    }
    
    .selected-post-info {
        flex-direction: column;
        align-items: stretch;
        gap: 15px;
        text-align: center;
    }
    
    .selected-post-info .button {
        margin-left: 0;
        align-self: center;
    }
    
    .filter-row {
        flex-direction: column;
        align-items: stretch;
        gap: 15px;
    }
    
    .sort-controls {
        justify-content: center;
    }
    
    .view-controls {
        justify-content: center;
    }
    
    .analytics-controls {
        flex-direction: column;
        align-items: stretch;
    }
    
    .analytics-controls-left,
    .analytics-controls-right {
        justify-content: center;
    }
    
    .premium-overlay-content {
        margin: 20px;
        padding: 30px 20px;
    }
    
    .premium-overlay-content h3 {
        font-size: 20px;
    }
    
    .premium-overlay-content ul {
        margin: 15px 0;
        padding-left: 15px;
    }
}

/* Disable interactivity for free users */
.pinnerpress-premium-disabled input,
.pinnerpress-premium-disabled select,
.pinnerpress-premium-disabled button {
    cursor: not-allowed !important;
}

/* Premium badge for dropdowns and buttons */
.pinnerpress-premium-badge {
    background: #E60023;
    color: white;
    font-size: 10px;
    padding: 2px 6px;
    border-radius: 3px;
    margin-left: 5px;
    text-transform: uppercase;
    display: inline-block;
    vertical-align: middle;
    font-weight: 600;
    line-height: 1;
}

/* Premium badge in disabled buttons */
.pinnerpress-date-preset.pinnerpress-premium-disabled .pinnerpress-premium-badge {
    background: #999;
}

/* View Toggle Styles */
/* Analytics Controls Layout */
.analytics-controls {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 20px;
}

.analytics-controls-left {
    display: flex;
    align-items: center;
    gap: 15px;
    flex-wrap: wrap;
}

.analytics-controls-right {
    display: flex;
    align-items: center;
    gap: 10px;
}

.view-toggle {
    display: inline-flex;
    border: 1px solid #ccd0d4;
    border-radius: 4px;
    overflow: hidden;
}

.view-toggle-btn {
    background: #fff;
    border: none;
    padding: 6px 12px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 5px;
    font-size: 13px;
    color: #646970;
    transition: all 0.2s;
}

.view-toggle-btn:not(:last-child) {
    border-right: 1px solid #ccd0d4;
}

.view-toggle-btn[aria-pressed="true"] {
    background: #E60023;
    color: #fff;
}

.view-toggle-btn[aria-pressed="true"] .dashicons {
    color: #fff;
}

.view-toggle-btn:hover:not([aria-pressed="true"]) {
    background: #f0f0f1;
}

/* Table View Styles */
.image-table-container {
    margin-top: 20px;
    overflow-x: auto;
}

.image-table-container table {
    border-spacing: 0;
    table-layout: fixed;
    width: 100%;
}

.image-table-container th {
    font-weight: 600;
}

.image-table-container th.sortable a {
    display: flex;
    align-items: center;
    gap: 5px;
    text-decoration: none;
    color: inherit;
}

.image-table-container th.sortable:hover {
    background: #f0f0f1;
}

.image-table-container .sorting-indicator {
    display: inline-block;
    width: 0;
    height: 0;
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    margin-left: auto;
}

.image-table-container th.asc .sorting-indicator {
    border-bottom: 4px solid #E60023;
}

.image-table-container th.desc .sorting-indicator {
    border-top: 4px solid #E60023;
}

.image-table-container tbody tr {
    cursor: pointer;
}

.image-table-container tbody tr:hover {
    background: #f6f7f7;
}

.image-table-container .column-preview img {
    width: 60px;
    height: 60px;
    object-fit: cover;
    border: 1px solid #ddd;
    border-radius: 4px;
}

.image-table-container .column-total-clicks {
    font-weight: 600;
    color: #E60023;
}

.image-table-container .never-clicked {
    color: #999;
}

/* Export button styling - outline style */
.pinnerpress-export-button {
    position: relative;
    background-color: #ffffff !important;
    color: #E60023 !important;
    border: 1px solid #E60023 !important;
    padding: 4px 12px !important;
    font-size: 13px !important;
    font-weight: 500 !important;
    border-radius: 4px !important;
    cursor: pointer;
    transition: all 0.3s ease;
    text-shadow: none !important;
    box-shadow: none !important;
    display: inline-flex !important;
    align-items: center !important;
    white-space: nowrap !important;
}

.pinnerpress-export-button:hover:not(:disabled) {
    background-color: #E60023 !important;
    color: #ffffff !important;
    border-color: #E60023 !important;
}

.pinnerpress-export-button:hover:not(:disabled) .pinnerpress-premium-badge {
    background-color: #ffffff !important;
    color: #E60023 !important;
}

.pinnerpress-export-button:focus {
    outline: none !important;
    box-shadow: 0 0 0 2px rgba(230, 0, 35, 0.3) !important;
}

.pinnerpress-export-button.pinnerpress-premium-disabled {
    background-color: #f1f1f1 !important;
    color: #666 !important;
    border-color: #ddd !important;
    cursor: not-allowed !important;
    opacity: 0.6;
}

.pinnerpress-export-button.pinnerpress-premium-disabled .pinnerpress-premium-badge {
    background: #999 !important;
}

/* Post Analytics Summary Styles */
.post-analytics-summary {
    background: #fff;
    border: 1px solid #ccd0d4;
    border-radius: 4px;
    padding: 20px;
    margin-bottom: 20px;
}

.post-analytics-summary h3 {
    color: #23282d;
    font-size: 18px;
    font-weight: 600;
}

.post-stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 20px;
    margin-top: 20px;
}

.post-stats-grid .stat-card {
    background: #f9f9f9;
    border: 1px solid #e1e1e1;
    border-radius: 4px;
    padding: 15px;
    text-align: center;
}

.post-stats-grid .stat-card h4 {
    font-size: 12px;
    color: #666;
    text-transform: uppercase;
    margin: 0 0 8px 0;
    font-weight: 600;
}

.post-stats-grid .stat-card .stat-value {
    font-size: 24px;
    font-weight: 600;
    color: #4BC0C0;
    margin: 0;
    line-height: 1.2;
}

/* All post analytics metrics are red */
.post-stats-grid .stat-card:nth-child(1) .stat-value { color: #E60023; } /* Impressions */
.post-stats-grid .stat-card:nth-child(2) .stat-value { color: #E60023; } /* Pin Clicks */
.post-stats-grid .stat-card:nth-child(3) .stat-value { color: #E60023; } /* Banner Clicks */
.post-stats-grid .stat-card:nth-child(4) .stat-value { color: #E60023; } /* Follow Clicks */
.post-stats-grid .stat-card:nth-child(5) .stat-value { color: #E60023; } /* Callout clicks */
.post-stats-grid .stat-card:nth-child(6) .stat-value { color: #E60023; } /* Conversion Rate */

/* Table column alignment */
.image-table-container .column-impressions,
.image-table-container .column-pin-clicks,
.image-table-container .column-pin-ctr,
.image-table-container .column-banner-clicks,
.image-table-container .column-banner-ctr,
.image-table-container .column-conversion,
.image-table-container .column-actions {
    text-align: center;
    vertical-align: middle;
}

/* Conversion rate styling matching post analytics */
.image-table-container .conversion-rate {
    padding: 2px 8px;
    border-radius: 12px;
    font-weight: 600;
    font-size: 12px;
    display: inline-block;
}

.image-table-container .conversion-rate.excellent {
    background-color: rgba(76, 175, 80, 0.1);
    color: #4CAF50;
}

.image-table-container .conversion-rate.good {
    background-color: rgba(255, 152, 0, 0.1);
    color: #FF9800;
}

.image-table-container .conversion-rate.fair {
    background-color: rgba(255, 193, 7, 0.1);
    color: #FFC107;
}

.image-table-container .conversion-rate.poor {
    background-color: rgba(230, 0, 35, 0.1);
    color: #E60023;
}

/* Action buttons styling */
.image-table-container .column-actions .button {
    padding: 6px 8px;
    margin-right: 4px;
    min-width: auto;
    font-size: 12px;
    height: auto;
    line-height: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: transparent !important;
    border: none !important;
    box-shadow: none !important;
}

.image-table-container .column-actions .button:last-child {
    margin-right: 0;
}

.image-table-container .column-actions .button .dashicons {
    font-size: 18px;
    width: 18px;
    height: 18px;
    line-height: 1;
    color: #E60023;
}

.image-table-container .column-actions .button:hover {
    background: rgba(230, 0, 35, 0.1);
    border-radius: 4px;
}

.image-table-container .column-actions .button:hover .dashicons {
    color: #bd051f;
}

/* Match Top Images: prevent action buttons wrapping */
.image-table-container .column-actions {
    white-space: nowrap;
}

/* Remove focus outlines specifically for action buttons */
.image-table-container .column-actions .button:focus {
    outline: none !important;
    box-shadow: none !important;
}

@media (max-width: 782px) {
    .post-stats-grid {
        grid-template-columns: repeat(2, 1fr);
    }
    
    .post-analytics-summary h3 {
        font-size: 16px;
    }
    
    .post-stats-grid .stat-card .stat-value {
        font-size: 20px;
    }
}
</style>
    </div> <!-- Close pinnerpress-admin-content --> 
