<?php

if (!defined('ABSPATH')) {
    exit;
}

class SEO_Fury_Frontend_Inspector {

    private static $instance = null;

    public static function get_instance() {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function __construct() {
        if (!$this->is_enabled()) {
            return;
        }

        add_action('admin_bar_menu', array($this, 'add_admin_bar_node'), 100);
        add_action('wp_head', array($this, 'output_inline_css'), 99);
        add_action('wp_footer', array($this, 'output_inline_js'), 99);
    }

    /**
     * Check whether the frontend inspector feature is enabled.
     *
     * @return bool
     */
    private function is_enabled() {
        return (bool) get_option('seo_fury_frontend_inspector_enabled', 1);
    }

    /**
     * Whether the current request qualifies for the inspector.
     * Must be frontend, non-admin, user has edit_posts, admin bar is showing.
     *
     * @return bool
     */
    private function should_render() {
        if (is_admin()) {
            return false;
        }

        if (!is_user_logged_in()) {
            return false;
        }

        if (!current_user_can('edit_posts')) {
            return false;
        }

        if (!is_admin_bar_showing()) {
            return false;
        }

        return true;
    }

    /**
     * Get the brand name for display.
     *
     * @return string
     */
    private function get_brand_name() {
        return function_exists('seo_fury_get_brand_name') ? seo_fury_get_brand_name() : 'SEO Fury';
    }

    /**
     * Collect all SEO data for the current page.
     *
     * @return array
     */
    private function get_seo_data() {
        $data = array(
            'title'            => '',
            'description'      => '',
            'focus_keyword'    => '',
            'canonical'        => '',
            'robots'           => '',
            'schema_type'      => '',
            'og_title'         => '',
            'og_image'         => '',
            'edit_url'         => '',
            'page_type'        => '',
        );

        if (is_singular()) {
            $data = $this->get_singular_data($data);
        } elseif (is_front_page() || is_home()) {
            $data = $this->get_home_data($data);
        } elseif (is_category() || is_tag() || is_tax()) {
            $data = $this->get_taxonomy_data($data);
        } elseif (is_post_type_archive()) {
            $data = $this->get_post_type_archive_data($data);
        } elseif (is_author()) {
            $data = $this->get_author_data($data);
        } elseif (is_search()) {
            $data = $this->get_search_data($data);
        } elseif (is_date()) {
            $data = $this->get_date_data($data);
        } elseif (is_404()) {
            $data = $this->get_404_data($data);
        }

        // Fallback canonical to current URL if empty
        if (empty($data['canonical'])) {
            if (is_singular()) {
                $post = get_post();
                if ($post) {
                    $data['canonical'] = get_permalink($post->ID);
                }
            }
            if (empty($data['canonical'])) {
                $data['canonical'] = home_url(add_query_arg(array()));
            }
        }

        return $data;
    }

    /**
     * Get SEO data for singular posts/pages.
     *
     * @param array $data
     * @return array
     */
    private function get_singular_data($data) {
        $post = get_post();
        if (!$post) {
            return $data;
        }

        $data['page_type'] = get_post_type_object($post->post_type)
            ? get_post_type_object($post->post_type)->labels->singular_name
            : $post->post_type;

        // SEO Title - try _seo_fury_ prefix, fallback to _my_seo_ for backward compat
        $data['title'] = get_post_meta($post->ID, '_seo_fury_title', true);
        if (empty($data['title'])) {
            $data['title'] = get_post_meta($post->ID, '_my_seo_title', true);
        }
        if (empty($data['title'])) {
            $data['title'] = get_the_title($post->ID);
        }

        // Meta Description
        $data['description'] = get_post_meta($post->ID, '_seo_fury_description', true);
        if (empty($data['description'])) {
            $data['description'] = get_post_meta($post->ID, '_my_seo_description', true);
        }

        // Focus Keyword
        $data['focus_keyword'] = get_post_meta($post->ID, '_seo_fury_focus_keyword', true);
        if (empty($data['focus_keyword'])) {
            $data['focus_keyword'] = get_post_meta($post->ID, '_my_seo_focus_keyword', true);
        }

        // Canonical URL
        $data['canonical'] = get_post_meta($post->ID, '_seo_fury_canonical', true);
        if (empty($data['canonical'])) {
            $data['canonical'] = get_post_meta($post->ID, '_my_seo_canonical', true);
        }
        if (empty($data['canonical'])) {
            $data['canonical'] = get_permalink($post->ID);
        }

        // Robots meta
        $data['robots'] = $this->get_singular_robots($post->ID);

        // Schema type
        $schema_type = get_post_meta($post->ID, '_seo_fury_schema_type', true);
        if (empty($schema_type)) {
            $schema_type = get_post_meta($post->ID, '_my_seo_schema_type', true);
        }
        if (!empty($schema_type) && $schema_type !== 'none') {
            $data['schema_type'] = $schema_type;
        } else {
            // Check default schema for the post type
            $default_schema = get_option('seo_fury_default_schema_' . $post->post_type, '');
            if (!empty($default_schema) && $default_schema !== 'none') {
                $data['schema_type'] = $default_schema . ' ' . __('(default)', 'seo-fury');
            }
        }

        // OG Title
        $data['og_title'] = get_post_meta($post->ID, '_seo_fury_og_title', true);
        if (empty($data['og_title'])) {
            $data['og_title'] = get_post_meta($post->ID, '_my_seo_og_title', true);
        }

        // OG Image
        $data['og_image'] = get_post_meta($post->ID, '_seo_fury_og_image', true);
        if (empty($data['og_image'])) {
            $data['og_image'] = get_post_meta($post->ID, '_my_seo_og_image', true);
        }

        // Edit URL - link directly to the post editor with the SEO meta box anchor
        $data['edit_url'] = get_edit_post_link($post->ID, 'raw');
        if ($data['edit_url']) {
            $data['edit_url'] .= '#seo_fury_post_meta';
        }

        return $data;
    }

    /**
     * Get robots meta string for a singular post.
     *
     * @param int $post_id
     * @return string
     */
    private function get_singular_robots($post_id) {
        $parts = array();
        $post = get_post($post_id);

        // Per-page robots (highest priority)
        $noindex      = get_post_meta($post_id, '_seo_fury_page_robots_noindex', true);
        $nofollow     = get_post_meta($post_id, '_seo_fury_page_robots_nofollow', true);
        $noarchive    = get_post_meta($post_id, '_seo_fury_page_robots_noarchive', true);
        $nosnippet    = get_post_meta($post_id, '_seo_fury_page_robots_nosnippet', true);
        $noimageindex = get_post_meta($post_id, '_seo_fury_page_robots_noimageindex', true);

        $has_per_page = $noindex || $nofollow || $noarchive || $nosnippet || $noimageindex;

        if ($has_per_page) {
            if ($noindex) {
                $parts[] = 'noindex';
            } else {
                $parts[] = 'index';
            }
            if ($nofollow) {
                $parts[] = 'nofollow';
            } else {
                $parts[] = 'follow';
            }
            if ($noarchive)    $parts[] = 'noarchive';
            if ($nosnippet)    $parts[] = 'nosnippet';
            if ($noimageindex) $parts[] = 'noimageindex';
            return implode(', ', $parts);
        }

        // Per-post-type robots (medium priority)
        if ($post) {
            $pt = $post->post_type;
            $pt_enabled = get_option('seo_fury_pt_robots_enabled_' . $pt, 0);
            if ($pt_enabled) {
                if (get_option('seo_fury_pt_robots_noindex_' . $pt, 0)) {
                    $parts[] = 'noindex';
                } elseif (get_option('seo_fury_pt_robots_index_' . $pt, 0)) {
                    $parts[] = 'index';
                }
                if (get_option('seo_fury_pt_robots_nofollow_' . $pt, 0)) {
                    $parts[] = 'nofollow';
                } else {
                    $parts[] = 'follow';
                }
                if (get_option('seo_fury_pt_robots_noarchive_' . $pt, 0))    $parts[] = 'noarchive';
                if (get_option('seo_fury_pt_robots_noimageindex_' . $pt, 0)) $parts[] = 'noimageindex';
                if (get_option('seo_fury_pt_robots_nosnippet_' . $pt, 0))    $parts[] = 'nosnippet';
                return implode(', ', $parts);
            }
        }

        // Global robots (lowest priority)
        return $this->get_global_robots();
    }

    /**
     * Get the global robots meta string from settings.
     *
     * @return string
     */
    private function get_global_robots() {
        $parts = array();

        $noindex = get_option('seo_fury_robots_meta_noindex', 0);
        $index   = get_option('seo_fury_robots_meta_index', 0);

        if ($noindex) {
            $parts[] = 'noindex';
        } elseif ($index) {
            $parts[] = 'index';
        }

        if (get_option('seo_fury_robots_meta_nofollow', 0)) {
            $parts[] = 'nofollow';
        } else {
            $parts[] = 'follow';
        }

        if (get_option('seo_fury_robots_meta_noarchive', 0))    $parts[] = 'noarchive';
        if (get_option('seo_fury_robots_meta_noimageindex', 0))  $parts[] = 'noimageindex';
        if (get_option('seo_fury_robots_meta_nosnippet', 0))     $parts[] = 'nosnippet';

        return !empty($parts) ? implode(', ', $parts) : 'index, follow';
    }

    /**
     * Get SEO data for the home/front page.
     *
     * @param array $data
     * @return array
     */
    private function get_home_data($data) {
        $data['page_type'] = __('Home Page', 'seo-fury');

        // If front page is a static page, use singular data
        if (is_front_page() && is_page()) {
            $data = $this->get_singular_data($data);
            $data['page_type'] = __('Front Page (Static)', 'seo-fury');
            return $data;
        }

        // Blog/home page from templates
        if (is_front_page()) {
            $data['title']       = get_option('seo_fury_title_template_home', '');
            $data['description'] = get_option('seo_fury_description_template_home', '');
        } else {
            // Posts page (blog page when a static front page is set)
            $data['title']       = get_option('seo_fury_title_template_blog', '');
            $data['description'] = get_option('seo_fury_description_template_blog', '');

            $blog_page_id = get_option('page_for_posts');
            if ($blog_page_id) {
                $data['edit_url'] = get_edit_post_link($blog_page_id, 'raw');
                if ($data['edit_url']) {
                    $data['edit_url'] .= '#seo_fury_post_meta';
                }
            }
        }

        if (empty($data['title'])) {
            $data['title'] = get_bloginfo('name');
        }

        $data['robots']    = $this->get_global_robots();
        $data['canonical'] = home_url('/');

        return $data;
    }

    /**
     * Get SEO data for taxonomy archive pages.
     *
     * @param array $data
     * @return array
     */
    private function get_taxonomy_data($data) {
        $term = get_queried_object();
        if (!$term || empty($term->taxonomy)) {
            return $data;
        }

        $tax_obj = get_taxonomy($term->taxonomy);
        $data['page_type'] = $tax_obj ? $tax_obj->labels->singular_name : $term->taxonomy;

        $data['title']       = get_option('seo_fury_title_template_tax_' . $term->taxonomy, '');
        $data['description'] = get_option('seo_fury_description_template_tax_' . $term->taxonomy, '');

        if (empty($data['title'])) {
            $data['title'] = single_term_title('', false);
        }

        if (empty($data['description']) && !empty($term->description)) {
            $data['description'] = wp_trim_words($term->description, 25, '...');
        }

        // Taxonomy robots
        $tax_name = $term->taxonomy;
        $tax_robots_enabled = get_option('seo_fury_tax_robots_enabled_' . $tax_name, 0);
        if ($tax_robots_enabled) {
            $parts = array();
            if (get_option('seo_fury_tax_robots_noindex_' . $tax_name, 0))   $parts[] = 'noindex';
            if (get_option('seo_fury_tax_robots_nofollow_' . $tax_name, 0))  $parts[] = 'nofollow';
            if (get_option('seo_fury_tax_robots_noarchive_' . $tax_name, 0)) $parts[] = 'noarchive';
            $data['robots'] = !empty($parts) ? implode(', ', $parts) : 'index, follow';
        } else {
            $data['robots'] = $this->get_global_robots();
        }

        $data['canonical'] = get_term_link($term);
        if (is_wp_error($data['canonical'])) {
            $data['canonical'] = '';
        }

        return $data;
    }

    /**
     * Get SEO data for post type archive pages.
     *
     * @param array $data
     * @return array
     */
    private function get_post_type_archive_data($data) {
        $post_type = get_query_var('post_type');
        if (!$post_type) {
            return $data;
        }

        $pt_obj = get_post_type_object($post_type);
        $data['page_type'] = $pt_obj
            ? $pt_obj->labels->name . ' ' . __('Archive', 'seo-fury')
            : $post_type;

        $data['title']       = get_option('seo_fury_title_template_' . $post_type, '');
        $data['description'] = get_option('seo_fury_description_template_' . $post_type, '');

        if (empty($data['title']) && $pt_obj) {
            $data['title'] = $pt_obj->labels->name;
        }

        $data['robots']    = $this->get_global_robots();
        $data['canonical'] = get_post_type_archive_link($post_type);

        return $data;
    }

    /**
     * Get SEO data for author archive pages.
     *
     * @param array $data
     * @return array
     */
    private function get_author_data($data) {
        $data['page_type']   = __('Author Archive', 'seo-fury');
        $data['title']       = get_option('seo_fury_title_template_author', '');
        $data['description'] = get_option('seo_fury_description_template_author', '');

        if (empty($data['title'])) {
            $data['title'] = get_the_author();
        }

        $data['robots'] = $this->get_global_robots();

        return $data;
    }

    /**
     * Get SEO data for search results pages.
     *
     * @param array $data
     * @return array
     */
    private function get_search_data($data) {
        $data['page_type']   = __('Search Results', 'seo-fury');
        $data['title']       = get_option('seo_fury_title_template_search', '');
        $data['description'] = get_option('seo_fury_description_template_search', '');

        if (empty($data['title'])) {
            /* translators: %s: search query */
            $data['title'] = sprintf(__('Search: %s', 'seo-fury'), get_search_query());
        }

        $data['robots'] = 'noindex, follow';

        return $data;
    }

    /**
     * Get SEO data for date archive pages.
     *
     * @param array $data
     * @return array
     */
    private function get_date_data($data) {
        $data['page_type']   = __('Date Archive', 'seo-fury');
        $data['title']       = get_option('seo_fury_title_template_date', '');
        $data['description'] = get_option('seo_fury_description_template_date', '');

        if (empty($data['title'])) {
            $data['title'] = get_the_date();
        }

        $data['robots'] = $this->get_global_robots();

        return $data;
    }

    /**
     * Get SEO data for 404 pages.
     *
     * @param array $data
     * @return array
     */
    private function get_404_data($data) {
        $data['page_type']   = __('404 Not Found', 'seo-fury');
        $data['title']       = get_option('seo_fury_title_template_404', '');
        $data['description'] = get_option('seo_fury_description_template_404', '');

        if (empty($data['title'])) {
            $data['title'] = __('Page Not Found', 'seo-fury');
        }

        $data['robots'] = 'noindex, follow';

        return $data;
    }

    /**
     * Determine the SEO health indicator color.
     *
     * Green: has title + description + focus keyword
     * Yellow: has title + description but no keyword (or partial)
     * Red: missing title or description
     *
     * @param array $data
     * @return string green|yellow|red
     */
    private function get_indicator_color($data) {
        $has_title   = !empty($data['title']);
        $has_desc    = !empty($data['description']);
        $has_keyword = !empty($data['focus_keyword']);

        if ($has_title && $has_desc && $has_keyword) {
            return 'green';
        }

        if ($has_title && $has_desc) {
            return 'yellow';
        }

        return 'red';
    }

    /**
     * Get the character count label with color class for a given value and optimal range.
     *
     * @param string $value
     * @param int    $min  Minimum optimal length.
     * @param int    $max  Maximum optimal length.
     * @return array Array with 'count' (int) and 'class' (string: good|bad).
     */
    private function get_char_info($value, $min, $max) {
        $count = mb_strlen($value);
        if ($count >= $min && $count <= $max) {
            $class = 'good';
        } elseif ($count === 0) {
            $class = 'bad';
        } else {
            $class = 'bad';
        }
        return array('count' => $count, 'class' => $class);
    }

    /**
     * Add the SEO inspector node to the admin bar.
     *
     * @param WP_Admin_Bar $wp_admin_bar
     */
    public function add_admin_bar_node($wp_admin_bar) {
        if (!$this->should_render()) {
            return;
        }

        $data  = $this->get_seo_data();
        $color = $this->get_indicator_color($data);
        $brand = $this->get_brand_name();

        $color_hex = '#46b450'; // green
        if ($color === 'yellow') {
            $color_hex = '#ffb900';
        } elseif ($color === 'red') {
            $color_hex = '#dc3232';
        }

        // Top-level node
        $wp_admin_bar->add_node(array(
            'id'    => 'seo-fury-inspector',
            'title' => '<span class="seo-fury-indicator" style="display:inline-block;width:10px;height:10px;border-radius:50%;background:' . esc_attr($color_hex) . ';margin-right:6px;vertical-align:middle;"></span>' . esc_html($brand),
            'href'  => admin_url('admin.php?page=seo-fury'),
            'meta'  => array(
                'class' => 'seo-fury-admin-bar-inspector',
            ),
        ));

        // Page Type
        if (!empty($data['page_type'])) {
            $wp_admin_bar->add_node(array(
                'id'     => 'seo-fury-inspector-pagetype',
                'parent' => 'seo-fury-inspector',
                'title'  => '<span class="sfi-label">' . esc_html__('Page Type', 'seo-fury') . ':</span> <span class="sfi-value">' . esc_html($data['page_type']) . '</span>',
                'href'   => false,
            ));
        }

        // SEO Title
        $title_info    = $this->get_char_info($data['title'], 50, 60);
        $title_display = !empty($data['title']) ? $data['title'] : __('(not set)', 'seo-fury');
        $wp_admin_bar->add_node(array(
            'id'     => 'seo-fury-inspector-title',
            'parent' => 'seo-fury-inspector',
            'title'  => '<span class="sfi-label">' . esc_html__('SEO Title', 'seo-fury') . ':</span> <span class="sfi-value">' . esc_html($title_display) . '</span> <span class="sfi-count sfi-' . esc_attr($title_info['class']) . '">' . esc_html($title_info['count']) . ' ' . esc_html__('chars', 'seo-fury') . '</span>',
            'href'   => false,
        ));

        // Meta Description
        $desc_info    = $this->get_char_info($data['description'], 120, 160);
        $desc_display = !empty($data['description']) ? $data['description'] : __('(not set)', 'seo-fury');
        // Truncate long descriptions for display
        if (mb_strlen($desc_display) > 80) {
            $desc_display = mb_substr($desc_display, 0, 77) . '...';
        }
        $wp_admin_bar->add_node(array(
            'id'     => 'seo-fury-inspector-description',
            'parent' => 'seo-fury-inspector',
            'title'  => '<span class="sfi-label">' . esc_html__('Description', 'seo-fury') . ':</span> <span class="sfi-value">' . esc_html($desc_display) . '</span> <span class="sfi-count sfi-' . esc_attr($desc_info['class']) . '">' . esc_html($desc_info['count']) . ' ' . esc_html__('chars', 'seo-fury') . '</span>',
            'href'   => false,
        ));

        // Focus Keyword
        $keyword_display = !empty($data['focus_keyword']) ? $data['focus_keyword'] : __('(not set)', 'seo-fury');
        $keyword_class   = !empty($data['focus_keyword']) ? 'sfi-good' : 'sfi-bad';
        $wp_admin_bar->add_node(array(
            'id'     => 'seo-fury-inspector-keyword',
            'parent' => 'seo-fury-inspector',
            'title'  => '<span class="sfi-label">' . esc_html__('Focus Keyword', 'seo-fury') . ':</span> <span class="sfi-value ' . esc_attr($keyword_class) . '">' . esc_html($keyword_display) . '</span>',
            'href'   => false,
        ));

        // Canonical URL
        if (!empty($data['canonical'])) {
            $canonical_display = $data['canonical'];
            if (mb_strlen($canonical_display) > 60) {
                $canonical_display = mb_substr($canonical_display, 0, 57) . '...';
            }
            $wp_admin_bar->add_node(array(
                'id'     => 'seo-fury-inspector-canonical',
                'parent' => 'seo-fury-inspector',
                'title'  => '<span class="sfi-label">' . esc_html__('Canonical', 'seo-fury') . ':</span> <span class="sfi-value">' . esc_html($canonical_display) . '</span>',
                'href'   => false,
            ));
        }

        // Robots Meta
        $robots_display = !empty($data['robots']) ? $data['robots'] : 'index, follow';
        $robots_class   = '';
        if (strpos($robots_display, 'noindex') !== false) {
            $robots_class = 'sfi-bad';
        } elseif (strpos($robots_display, 'nofollow') !== false) {
            $robots_class = 'sfi-warning';
        }
        $wp_admin_bar->add_node(array(
            'id'     => 'seo-fury-inspector-robots',
            'parent' => 'seo-fury-inspector',
            'title'  => '<span class="sfi-label">' . esc_html__('Robots', 'seo-fury') . ':</span> <span class="sfi-value ' . esc_attr($robots_class) . '">' . esc_html($robots_display) . '</span>',
            'href'   => false,
        ));

        // Schema Type
        if (!empty($data['schema_type'])) {
            $wp_admin_bar->add_node(array(
                'id'     => 'seo-fury-inspector-schema',
                'parent' => 'seo-fury-inspector',
                'title'  => '<span class="sfi-label">' . esc_html__('Schema', 'seo-fury') . ':</span> <span class="sfi-value">' . esc_html($data['schema_type']) . '</span>',
                'href'   => false,
            ));
        }

        // OG Title
        if (!empty($data['og_title'])) {
            $wp_admin_bar->add_node(array(
                'id'     => 'seo-fury-inspector-og-title',
                'parent' => 'seo-fury-inspector',
                'title'  => '<span class="sfi-label">' . esc_html__('OG Title', 'seo-fury') . ':</span> <span class="sfi-value">' . esc_html($data['og_title']) . '</span>',
                'href'   => false,
            ));
        }

        // OG Image
        if (!empty($data['og_image'])) {
            $og_image_display = $data['og_image'];
            if (mb_strlen($og_image_display) > 50) {
                $og_image_display = mb_substr($og_image_display, 0, 47) . '...';
            }
            $wp_admin_bar->add_node(array(
                'id'     => 'seo-fury-inspector-og-image',
                'parent' => 'seo-fury-inspector',
                'title'  => '<span class="sfi-label">' . esc_html__('OG Image', 'seo-fury') . ':</span> <span class="sfi-value">' . esc_html($og_image_display) . '</span>',
                'href'   => false,
            ));
        }

        // Edit SEO link (only for singular posts)
        if (!empty($data['edit_url'])) {
            $wp_admin_bar->add_node(array(
                'id'     => 'seo-fury-inspector-edit',
                'parent' => 'seo-fury-inspector',
                'title'  => '<span class="sfi-edit-link">' . esc_html__('Edit SEO Settings', 'seo-fury') . ' &rarr;</span>',
                'href'   => esc_url($data['edit_url']),
            ));
        }
    }

    /**
     * Output inline CSS for the admin bar inspector dropdown.
     */
    public function output_inline_css() {
        if (!$this->should_render()) {
            return;
        }
        ?>
        <style id="seo-fury-frontend-inspector-css">
            /*
             * pointer-events:none on the wrapper has a useful side-effect:
             * when the cursor crosses from the dropdown background into the page
             * (nav items), the hit-target becomes the nav element — which is NOT
             * a descendant of the admin-bar li. The browser therefore fires
             * mouseleave on the li, WordPress admin-bar.js removes .hover, the
             * dropdown hides, and the nav item receives its own hover event.
             * pointer-events:auto on .ab-submenu keeps the actual items interactive.
             */
            #wpadminbar #wp-admin-bar-seo-fury-inspector .ab-sub-wrapper {
                pointer-events: none !important;
            }

            #wpadminbar #wp-admin-bar-seo-fury-inspector .ab-sub-wrapper .ab-submenu {
                pointer-events: auto !important;
            }

            #wpadminbar .seo-fury-admin-bar-inspector .ab-submenu {
                width: 420px !important;
                padding: 0 !important;
                background: #fff !important;
                border: 1px solid #ccd0d4 !important;
                box-shadow: 0 3px 8px rgba(0, 0, 0, 0.15) !important;
                border-radius: 0 0 4px 4px !important;
            }

            #wpadminbar .seo-fury-admin-bar-inspector .ab-submenu li {
                float: none !important;
                border-bottom: 1px solid #f0f0f1 !important;
            }

            #wpadminbar .seo-fury-admin-bar-inspector .ab-submenu li:last-child {
                border-bottom: none !important;
            }

            #wpadminbar .seo-fury-admin-bar-inspector .ab-submenu li .ab-item,
            #wpadminbar .seo-fury-admin-bar-inspector .ab-submenu li a.ab-item {
                display: block !important;
                padding: 8px 14px !important;
                line-height: 1.5 !important;
                height: auto !important;
                white-space: normal !important;
                color: #1d2327 !important;
                background: #fff !important;
                font-size: 12px !important;
            }

            #wpadminbar .seo-fury-admin-bar-inspector .ab-submenu li .ab-item:hover,
            #wpadminbar .seo-fury-admin-bar-inspector .ab-submenu li a.ab-item:hover {
                background: #f6f7f7 !important;
                color: #1d2327 !important;
            }

            /* Labels */
            #wpadminbar .sfi-label {
                font-weight: 600 !important;
                color: #1d2327 !important;
                margin-right: 4px !important;
            }

            /* Values */
            #wpadminbar .sfi-value {
                font-weight: 400 !important;
                color: #50575e !important;
                word-break: break-word !important;
            }

            /* Character counts */
            #wpadminbar .sfi-count {
                font-size: 11px !important;
                font-weight: 400 !important;
                margin-left: 6px !important;
                padding: 1px 6px !important;
                border-radius: 3px !important;
                display: inline-block !important;
            }

            /* Color classes */
            #wpadminbar .sfi-good {
                color: #00a32a !important;
            }

            #wpadminbar .sfi-count.sfi-good {
                background: #edfaef !important;
                color: #00a32a !important;
            }

            #wpadminbar .sfi-warning {
                color: #dba617 !important;
            }

            #wpadminbar .sfi-count.sfi-warning {
                background: #fef8e7 !important;
                color: #996800 !important;
            }

            #wpadminbar .sfi-bad {
                color: #d63638 !important;
            }

            #wpadminbar .sfi-count.sfi-bad {
                background: #fcf0f1 !important;
                color: #d63638 !important;
            }

            /* Edit SEO link */
            #wpadminbar .sfi-edit-link {
                color: #2271b1 !important;
                font-weight: 600 !important;
                font-size: 12px !important;
            }

            #wpadminbar .seo-fury-admin-bar-inspector .ab-submenu li:last-child .ab-item:hover .sfi-edit-link {
                color: #135e96 !important;
                text-decoration: underline !important;
            }

            /* Indicator dot in top bar */
            #wpadminbar .seo-fury-indicator {
                display: inline-block !important;
                width: 10px !important;
                height: 10px !important;
                border-radius: 50% !important;
                margin-right: 6px !important;
                vertical-align: middle !important;
                position: relative !important;
                top: -1px !important;
            }

            /* Responsive: narrower on smaller screens */
            @media screen and (max-width: 782px) {
                #wpadminbar .seo-fury-admin-bar-inspector .ab-submenu {
                    width: 300px !important;
                }
            }
        </style>
        <?php
    }

    /**
     * No custom JS needed.
     * WordPress admin-bar.js already manages hover open/close via .hover class.
     * The pointer-events CSS on .ab-sub-wrapper ensures the dropdown does not
     * block site navigation beneath it.
     */
    public function output_inline_js() {
        // Intentionally empty.
    }

    /**
     * Register the frontend inspector setting.
     * Called from class-general.php or wherever settings are registered.
     */
    public function register_settings() {
        register_setting('seo_fury_general_settings', 'seo_fury_frontend_inspector_enabled', array(
            'type'              => 'boolean',
            'default'           => 1,
            'sanitize_callback' => 'absint',
        ));
    }
}
