<?php
/**
 * MenuX Free — CSS Generator
 * @package MenuX
 */

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

function menux_hex_to_rgba($hex, $opacity) {
    $hex = ltrim($hex, '#');
    if (strlen($hex) === 3) {
        $hex = $hex[0].$hex[0].$hex[1].$hex[1].$hex[2].$hex[2];
    }
    return 'rgba('.hexdec(substr($hex,0,2)).','.hexdec(substr($hex,2,2)).','.hexdec(substr($hex,4,2)).','.round(floatval($opacity),2).')';
}

function menux_generate_css($style) {
    $s = wp_parse_args((array) $style, menux_style_defaults());

    // Announcement bar
    if ( ! empty( $s['announcement_enabled'] ) && $s['announcement_enabled'] === '1' && ! empty( $s['announcement_text'] ) ) {
        $ann_bg    = ! empty( $s['announcement_bg'] )         ? sanitize_hex_color( $s['announcement_bg'] )         : '#667eea';
        $ann_tc    = ! empty( $s['announcement_text_color'] ) ? sanitize_hex_color( $s['announcement_text_color'] ) : '#ffffff';
        $ann_fs    = max( 10, min( 24, intval( $s['announcement_font_size'] ?? 14 ) ) );
        $ann_py    = max( 4,  min( 32, intval( $s['announcement_padding_y']  ?? 10 ) ) );
        $css_ann   = '.menux-wrap{display:flex;flex-direction:column;}';
        $css_ann  .= '.menux-announcement-bar{display:flex;align-items:center;justify-content:center;background:' . $ann_bg . ';color:' . $ann_tc . ';font-size:' . $ann_fs . 'px;padding:' . $ann_py . 'px 48px;position:relative;text-align:center;line-height:1.5;box-sizing:border-box;margin:0;}';
        $css_ann  .= '.menux-announcement-bar a{color:inherit;text-decoration:underline;text-underline-offset:2px;}';
        $css_ann  .= '.menux-announcement-bar a:hover{opacity:.85;}';
        $css_ann  .= '.menux-ann-close{position:absolute;right:14px;top:50%;transform:translateY(-50%);background:none;border:none;color:inherit;font-size:20px;line-height:1;cursor:pointer;padding:4px 8px;opacity:.7;transition:opacity .15s;}';
        $css_ann  .= '.menux-ann-close:hover{opacity:1;}';
        $css_ann  .= '.menux-announcement-bar.menux-ann-hidden{display:none;}';
        // Countdown widget
        $css_ann  .= '.menux-ann-countdown{display:inline-flex;align-items:center;gap:6px;margin-left:14px;font-weight:600;}';
        $css_ann  .= '.menux-ann-cd-label{font-size:.88em;opacity:.85;white-space:nowrap;}';
        $css_ann  .= '.menux-ann-cd-time{display:inline-flex;align-items:center;gap:3px;}';
        $css_ann  .= '.menux-ann-cd-seg{display:inline-flex;align-items:baseline;gap:1px;}';
        $css_ann  .= '.menux-ann-cd-num{display:inline-block;min-width:1.5em;text-align:center;font-variant-numeric:tabular-nums;background:rgba(0,0,0,.18);border-radius:3px;padding:1px 4px;letter-spacing:0;}';
        $css_ann  .= '.menux-ann-cd-unit{font-size:.7em;opacity:.8;margin-left:1px;}';
        $css_ann  .= '.menux-ann-cd-sep{opacity:.6;font-weight:700;margin:0 1px;}';
        // Prepend so it precedes all other plugin rules — bar is a separate element, no specificity conflicts.
        return $css_ann . menux_generate_css_body( $s );
    }

    return menux_generate_css_body( $s );
}

function menux_generate_css_body($s) {

    // Google Font import
    $css = '';
    if (!empty($s['google_font'])) {
        // Allow only characters valid in a Google Font family name; block url() escape sequences.
        $gf_slug = str_replace(' ', '+', preg_replace('/[^a-zA-Z0-9 ,]/', '', $s['google_font']));
        $css .= "@import url('https://fonts.googleapis.com/css2?family={$gf_slug}:wght@300;400;500;600;700&display=swap');\n";
    }

    // Dark mode attribute
    $dark_mode = $s['dark_mode'] ?? 'light';

    // Container
    $cr = '';
    if (!empty($s['container_bg_gradient'])) $cr .= 'background:' . str_replace( array( ';', '{', '}' ), '', wp_strip_all_tags($s['container_bg_gradient']) ) . ';';
    elseif ($s['container_bg'] !== '')       $cr .= 'background:' . sanitize_hex_color($s['container_bg']) . ';';
    if ($s['container_border'] !== '') $cr .= 'border-bottom:1px solid ' . sanitize_hex_color($s['container_border']) . ';';
    if (!empty($s['container_border_radius'])) $cr .= 'border-radius:' . intval($s['container_border_radius']) . 'px;';
    if (!empty($s['google_font']))     $cr .= "font-family:'" . preg_replace('/[^a-zA-Z0-9 ,]/', '', $s['google_font']) . "', sans-serif;";
    elseif (!empty($s['font_family'])) $cr .= 'font-family:' . str_replace( array( ';', '{', '}' ), '', sanitize_text_field($s['font_family']) ) . ';';
    $strans = isset($s['sticky_transition']) && is_numeric($s['sticky_transition']) ? floatval($s['sticky_transition']) : 0.3;
    $css .= '.menux-container{position:relative;display:flex;flex-direction:row;align-items:center;flex-wrap:wrap;transition:background '.$strans.'s ease,box-shadow '.$strans.'s ease,padding '.$strans.'s ease;' . $cr . '}';

    // CTA button base class — per-item colours come from CSS custom properties set inline.
    $css .= '.menux-cta-btn.menux-link{'
        . 'display:inline-flex!important;align-items:center;justify-content:center;'
        . 'padding:8px 22px!important;'
        . 'margin:0 6px;'                                    // breathing room from adjacent items
        . 'border-radius:var(--cta-r,6px);'
        . 'background:var(--cta-bg,#667eea)!important;'
        . 'color:var(--cta-color,#fff)!important;'
        . 'font-weight:600;letter-spacing:.02em;'
        . 'text-decoration:none;white-space:nowrap;'
        . 'border:2px solid transparent;'                    // holds space so layout doesn't shift on outline variant
        . 'transition:background .2s ease,opacity .2s ease,box-shadow .2s ease;'
        . '}';
    $css .= '.menux-cta-btn.menux-link:hover{'
        . 'background:var(--cta-hover,var(--cta-bg,#667eea))!important;'
        . 'color:var(--cta-color,#fff)!important;'
        . 'opacity:.88;'                                     // subtle dim instead of translateY (no layout jitter)
        . '}';
    $css .= '.menux-cta-btn.menux-link:active{opacity:.72;}';

    // Dark mode CSS variables
    if ($dark_mode === 'dark') {
        $css .= '[data-bs-theme="dark"] .menux-container,.menux-container[data-bm-theme="dark"]{color-scheme:dark;}';
    } elseif ($dark_mode === 'auto') {
        $css .= '@media(prefers-color-scheme:dark){.menux-container{color-scheme:dark;}}';
    }

    // Sticky
    if ($s['sticky'] === '1') {
        $sz = !empty($s['sticky_z_index']) ? intval($s['sticky_z_index']) : 9999;
        if (($s['sticky_mirror'] ?? '1') === '1') {
            $css .= '.menux-container.menux-sticky-fixed{position:fixed;top:0;left:0;width:100%;z-index:'.$sz.';}';
        } else {
            $sbg    = !empty($s['sticky_bg'])           ? 'background:' . sanitize_hex_color($s['sticky_bg']) . ';' : '';
            $sshad  = $s['sticky_shadow'] !== '0'        ? 'box-shadow:0 2px 12px rgba(0,0,0,.12);' : '';
            $sjust  = !empty($s['sticky_justify'])       ? esc_attr($s['sticky_justify'])      : 'flex-start';
            $salign = !empty($s['sticky_align_items'])   ? esc_attr($s['sticky_align_items'])  : 'center';
            $spx    = !empty($s['sticky_padding_x'])     ? 'padding-left:'.intval($s['sticky_padding_x']).'px;padding-right:'.intval($s['sticky_padding_x']).'px;' : '';
            $spy    = !empty($s['sticky_padding_y'])     ? 'padding-top:'.intval($s['sticky_padding_y']).'px;padding-bottom:'.intval($s['sticky_padding_y']).'px;' : '';
            $css   .= '.menux-container.menux-sticky-fixed{position:fixed;top:0;left:0;width:100%;z-index:'.$sz.';justify-content:'.$sjust.';align-items:'.$salign.';'.$sbg.$sshad.$spx.$spy.'}';
        }
        $css   .= '.menux-sticky-spacer{display:block;}';
        // Shrink on sticky
        if (!empty($s['sticky_shrink']) && $s['sticky_shrink'] === '1') {
            $css .= '.menux-container.menux-sticky-fixed{padding-top:6px!important;padding-bottom:6px!important;}';
            $css .= '.menux-container.menux-sticky-fixed .menux-link{font-size:13px;}';
            $css .= '.menux-container.menux-sticky-fixed .menux-logo img{max-height:32px!important;}';
        }
    }
    // Progress bar (works with and without sticky)
    if (!empty($s['progress_bar_enabled']) && $s['progress_bar_enabled'] === '1') {
        $pb_color  = !empty($s['progress_bar_color'])  ? sanitize_hex_color($s['progress_bar_color'])  : '#667eea';
        $pb_height = !empty($s['progress_bar_height']) ? intval($s['progress_bar_height'])              : 3;
        $pb_pos    = (!empty($s['progress_bar_position']) && $s['progress_bar_position'] === 'top') ? 'top:0' : 'bottom:0';
        $css .= '.menux-progress-bar{position:absolute;'.$pb_pos.';left:0;height:'.$pb_height.'px;background:'.$pb_color.';width:0%;transition:width .1s linear;pointer-events:none;z-index:10;}';
    }

    // List layout
    $gap = ($s['gap'] !== '') ? max(0, intval($s['gap'])) : 20;
    $nav_justify = !empty($s['nav_justify']) ? esc_attr($s['nav_justify']) : 'flex-start';
    $css .= '.menux-list{list-style:none;display:flex;margin:0;padding:0;flex-wrap:wrap;gap:' . $gap . 'px;align-items:center;justify-content:' . $nav_justify . ';}';

    // ── Entrance animation ──
    $entrance     = $s['entrance_animation'] ?? 'none';
    $ent_dur      = isset($s['entrance_duration']) && is_numeric($s['entrance_duration']) ? floatval($s['entrance_duration']) : 0.5;
    $ent_delay    = isset($s['entrance_delay'])    && is_numeric($s['entrance_delay'])    ? floatval($s['entrance_delay'])    : 0;
    $ent_stagger  = isset($s['entrance_stagger'])  && is_numeric($s['entrance_stagger'])  ? floatval($s['entrance_stagger'])  : 0;

    if ($entrance !== 'none') {
        $css .= '@keyframes bm-entrance-fadeIn{from{opacity:0}to{opacity:1}}';
        $css .= '@keyframes bm-entrance-slideDown{from{opacity:0;transform:translateY(-24px)}to{opacity:1;transform:translateY(0)}}';
        $css .= '@keyframes bm-entrance-slideUp{from{opacity:0;transform:translateY(24px)}to{opacity:1;transform:translateY(0)}}';
        $css .= '@keyframes bm-entrance-slideLeft{from{opacity:0;transform:translateX(24px)}to{opacity:1;transform:translateX(0)}}';
        $css .= '@keyframes bm-entrance-slideRight{from{opacity:0;transform:translateX(-24px)}to{opacity:1;transform:translateX(0)}}';
        $css .= '@keyframes bm-entrance-zoomIn{from{opacity:0;transform:scale(0.85)}to{opacity:1;transform:scale(1)}}';
        $css .= '@keyframes bm-entrance-flipX{from{opacity:0;transform:perspective(400px) rotateX(-60deg)}to{opacity:1;transform:perspective(400px) rotateX(0)}}';

        $ent_anim_val = esc_attr($entrance);
        if ($ent_stagger > 0) {
            // Stagger: animate the first 20 items individually. Only hide those 20;
            // items beyond the cap stay visible (no animation) instead of being
            // stuck at opacity:0 forever.
            $css .= '.menux-list > li:nth-child(-n+20){opacity:0;}';
            for ($i = 0; $i < 20; $i++) {
                $d = round($ent_delay + $i * $ent_stagger, 3);
                $css .= '.menux-list > li:nth-child(' . ($i + 1) . '){animation:bm-entrance-' . $ent_anim_val . ' ' . $ent_dur . 's ease forwards ' . $d . 's;}';
            }
        } else {
            // Whole container animates
            $css .= '.menux-container{animation:bm-entrance-' . $ent_anim_val . ' ' . $ent_dur . 's ease backwards ' . $ent_delay . 's;}';
        }
    }

    // Link animation keyframes
    $anim = $s['link_animation'] ?? 'none';
    $css .= '@keyframes bm-pulse{0%,100%{transform:scale(1)}50%{transform:scale(1.08)}}';
    $css .= '@keyframes bm-shake{0%,100%{transform:translateX(0)}20%,60%{transform:translateX(-4px)}40%,80%{transform:translateX(4px)}}';
    $css .= '@keyframes bm-bounce{0%,100%{transform:translateY(0)}50%{transform:translateY(-5px)}}';
    $css .= '@keyframes bm-glow{0%,100%{text-shadow:none}50%{text-shadow:0 0 8px currentColor}}';
    $css .= '@keyframes bm-underline-grow{from{width:0}to{width:100%}}';

    // Link base
    $trans_dur = !empty($s['link_transition']) ? floatval($s['link_transition']) : 0.3;
    $lr = 'text-decoration:none;display:inline-flex;align-items:center;transition:all '.$trans_dur.'s ease;position:relative;';
    if ($s['link_color'] !== '')       $lr .= 'color:' . sanitize_hex_color($s['link_color']) . ';';
    if ($s['font_size'] !== '')        $lr .= 'font-size:' . intval($s['font_size']) . 'px;';
    if ($s['font_weight'] !== '')      $lr .= 'font-weight:' . esc_attr($s['font_weight']) . ';';
    if (!empty($s['letter_spacing']))  $lr .= 'letter-spacing:' . floatval($s['letter_spacing']) . 'px;';
    if (!empty($s['text_transform']))  $lr .= 'text-transform:' . sanitize_text_field($s['text_transform']) . ';';
    $px = ($s['padding_x'] !== '') ? intval($s['padding_x']) : 0;
    $py = ($s['padding_y'] !== '') ? intval($s['padding_y']) : 0;
    if ($px || $py) $lr .= 'padding:' . $py . 'px ' . $px . 'px;';
    if ($s['link_border_radius'] !== '') $lr .= 'border-radius:' . intval($s['link_border_radius']) . 'px;';
    $css .= '.menux-list > li > a.menux-link{' . $lr . '}';
    // Give the container the same right breathing room as the link horizontal padding,
    // so the last element (cart, search, etc.) isn't flush against the edge.
    $cpr = $px > 0 ? $px : 8;
    $css .= '.menux-container{padding-right:' . $cpr . 'px;}';

    // Link animation on hover
    $anim_css = '';
    if ($anim === 'pulse')         $anim_css = 'animation:bm-pulse .4s ease;';
    elseif ($anim === 'shake')     $anim_css = 'animation:bm-shake .4s ease;';
    elseif ($anim === 'bounce')    $anim_css = 'animation:bm-bounce .4s ease;';
    elseif ($anim === 'glow')      $anim_css = 'animation:bm-glow .6s ease infinite;';
    elseif ($anim === 'lift')      $anim_css = 'transform:translateY(-3px);';
    elseif ($anim === 'scale')     $anim_css = 'transform:scale(1.1);';
    elseif ($anim === 'underline') {
        $css .= '.menux-list > li > a.menux-link::after{content:"";position:absolute;bottom:0;left:0;width:0;height:2px;background:currentColor;transition:width .3s ease;}';
        $anim_css = '&::after{width:100%}';
    }

    // Hover
    $hr = '';
    if ($s['link_hover_color'] !== '') $hr .= 'color:' . sanitize_hex_color($s['link_hover_color']) . ';';
    if (!empty($s['link_hover_bg_gradient']))  $hr .= 'background:' . str_replace( array( ';', '{', '}' ), '', wp_strip_all_tags($s['link_hover_bg_gradient']) ) . ';';
    elseif (!empty($s['link_hover_bg']))       $hr .= 'background:' . sanitize_hex_color($s['link_hover_bg']) . ';';
    if ($anim_css && $anim !== 'underline') $hr .= $anim_css;
    if ($hr) $css .= '.menux-list > li > a.menux-link:hover{' . $hr . '}';

    // Active
    $ar = '';
    if ($s['link_active_color'] !== '')       $ar .= 'color:' . sanitize_hex_color($s['link_active_color']) . ';';
    if ($s['link_active_border'] !== '')      $ar .= 'border-bottom:2px solid ' . sanitize_hex_color($s['link_active_border']) . ';';
    if (!empty($s['link_active_font_weight'])) $ar .= 'font-weight:' . esc_attr($s['link_active_font_weight']) . '!important;';
    if (!empty($s['link_active_bg_gradient']))  $ar .= 'background:' . str_replace( array( ';', '{', '}' ), '', wp_strip_all_tags($s['link_active_bg_gradient']) ) . ';';
    elseif (!empty($s['link_active_bg']))       $ar .= 'background:' . sanitize_hex_color($s['link_active_bg']) . ';';
    if ($ar) $css .= '.menux-list > li > a.menux-link.active,.menux-list > li > a.menux-link.menux-spy-active{' . $ar . '}';
    // When no custom active styles are set, spy-active still shows a visible indicator.
    if (!$ar) $css .= '.menux-list > li > a.menux-link.menux-spy-active{font-weight:600;}';

    // Push last item to the right
    if ($s['push_last_item'] === '1') {
        $css .= '.menux-list > li:last-child{margin-left:auto;}';
    }

    // Last item custom color
    $licr = '';
    if (!empty($s['last_item_color'])) $licr .= 'color:' . sanitize_hex_color($s['last_item_color']) . '!important;';
    if (!empty($s['last_item_bg']))    $licr .= 'background:' . sanitize_hex_color($s['last_item_bg']) . ';';
    if ($licr) $css .= '.menux-list > li:last-child > a.menux-link{' . $licr . '}';
    if (!empty($s['last_item_hover_color'])) {
        $css .= '.menux-list > li:last-child > a.menux-link:hover{color:' . sanitize_hex_color($s['last_item_hover_color']) . '!important;}';
    }

    // Badge etichette
    $css .= '.menux-badge{display:inline-block;font-size:9px;font-weight:700;padding:1px 5px;border-radius:10px;margin-left:5px;vertical-align:middle;text-transform:uppercase;letter-spacing:.5px;line-height:1.6;}';

    // ──── FEATURE 2: Notification Dot animato ────
    $css .= '.menux-notif-dot{display:inline-block;width:8px;height:8px;background:#ef4444;border-radius:50%;margin-left:6px;vertical-align:top;position:relative;animation:menux-pulse 2s infinite;box-shadow:0 0 0 0 rgba(239,68,68,.5);}';
    $css .= '@keyframes menux-pulse{0%{box-shadow:0 0 0 0 rgba(239,68,68,.5)}70%{box-shadow:0 0 0 8px rgba(239,68,68,0)}100%{box-shadow:0 0 0 0 rgba(239,68,68,0)}}';

    // ──── FEATURE 5: Auto-hide on scroll ────
    $css .= '.menux-sticky-fixed.menux-autohide{transition:transform .3s cubic-bezier(.4,0,.2,1);}';
    $css .= '.menux-sticky-fixed.menux-autohide.menux-hidden{transform:translateY(-100%);pointer-events:none;}';

    // Submenu
    $smbc   = !empty($s['submenu_bg'])              ? sanitize_hex_color($s['submenu_bg'])              : '#ffffff';
    $smbd   = !empty($s['submenu_border'])           ? sanitize_hex_color($s['submenu_border'])           : '#e5e7eb';
    $smlc   = !empty($s['submenu_link_color'])       ? sanitize_hex_color($s['submenu_link_color'])       : '#374151';
    // Hover: se non configurato, overlay traslucido adattivo alla luminosità dello
    // sfondo del submenu (velo scuro su sfondi chiari, chiaro su sfondi scuri) e
    // colore testo ereditato dal link — così ogni tema resta coerente di default.
    $smhbg  = !empty($s['submenu_hover_bg'])         ? sanitize_hex_color($s['submenu_hover_bg'])         : '';
    $smhlc  = !empty($s['submenu_hover_link_color']) ? sanitize_hex_color($s['submenu_hover_link_color']) : '';
    if ( ! $smhbg ) {
        $hexv = ltrim( $smbc ?: '#ffffff', '#' );
        if ( strlen( $hexv ) === 3 ) {
            $hexv = $hexv[0] . $hexv[0] . $hexv[1] . $hexv[1] . $hexv[2] . $hexv[2];
        }
        $lum   = ( 0.2126 * hexdec( substr( $hexv, 0, 2 ) ) + 0.7152 * hexdec( substr( $hexv, 2, 2 ) ) + 0.0722 * hexdec( substr( $hexv, 4, 2 ) ) ) / 255;
        $smhbg = $lum > 0.5 ? 'rgba(0,0,0,.05)' : 'rgba(255,255,255,.08)';
    }
    $smsh   = ($s['submenu_shadow']  ?? '1') === '1' ? 'box-shadow:0 8px 24px rgba(0,0,0,.12);' : '';
    $smanim = $s['submenu_animation'] ?? 'fade';
    $sm_br  = isset($s['submenu_border_radius']) && $s['submenu_border_radius'] !== '' ? max(0, min(32, intval($s['submenu_border_radius']))) : 10;
    $sm_px  = isset($s['submenu_padding_x'])     && $s['submenu_padding_x']     !== '' ? max(0, min(48, intval($s['submenu_padding_x'])))     : 16;
    $sm_py  = isset($s['submenu_padding_y'])     && $s['submenu_padding_y']     !== '' ? max(0, min(32, intval($s['submenu_padding_y'])))     : 9;
    $sm_fs  = isset($s['submenu_font_size'])     && $s['submenu_font_size']     !== '' ? max(10, min(22, intval($s['submenu_font_size'])))     : 14;
    $sm_fw  = in_array($s['submenu_font_weight'] ?? '400', array('300','400','500','600','700'), true) ? $s['submenu_font_weight'] : '400';
    $sm_mw  = isset($s['submenu_min_width'])     && $s['submenu_min_width']     !== '' ? max(80, min(600, intval($s['submenu_min_width'])))    : 200;
    $sm_anim_init = '';
    $sm_anim_show = '';
    if ($smanim === 'fade') {
        $sm_anim_init = 'opacity:0;pointer-events:none;transform:translateY(6px);';
        $sm_anim_show = 'opacity:1;pointer-events:auto;transform:translateY(0);';
    } elseif ($smanim === 'slide') {
        $sm_anim_init = 'opacity:0;pointer-events:none;transform:translateY(-10px);max-height:0;overflow:hidden;';
        $sm_anim_show = 'opacity:1;pointer-events:auto;transform:translateY(0);max-height:600px;';
    } else {
        $sm_anim_init = 'display:none;opacity:0;pointer-events:none;';
        $sm_anim_show = 'display:block;opacity:1;pointer-events:auto;';
    }

    $css .= '.menux-list li{position:relative;}';
    $css .= '.menux-submenu{list-style:none;margin:0;padding:6px 0;position:absolute;top:100%;left:0;min-width:'.$sm_mw.'px;background:'.$smbc.';border:1px solid '.$smbd.';border-radius:'.$sm_br.'px;z-index:10001;'.$smsh.'transition:all .25s ease;'.$sm_anim_init.'}';
    $css .= '.menux-list li:hover > .menux-submenu{' . $sm_anim_show . '}';
    $css .= '.menux-submenu li{position:relative;}';
    $css .= '.menux-submenu a.menux-link{display:flex!important;align-items:center;padding:'.$sm_py.'px '.$sm_px.'px!important;white-space:nowrap;color:'.$smlc.'!important;font-size:'.$sm_fs.'px!important;font-weight:'.$sm_fw.'!important;width:100%;box-sizing:border-box;}';
    $css .= '.menux-submenu a.menux-link:hover{background:'.$smhbg.';'.($smhlc ? 'color:'.$smhlc.'!important;' : '').'}';
    // 3° livello: apre a destra
    $css .= '.menux-submenu .menux-submenu{top:0;left:100%;margin-top:-6px;}';
    // Dropdown indicator — style, color, size, rotate on open
    $ind_style  = $s['dropdown_indicator']        ?? 'triangle';
    $ind_clr    = ! empty( $s['dropdown_indicator_color'] ) ? sanitize_hex_color( $s['dropdown_indicator_color'] ) : '';
    $ind_size   = isset( $s['dropdown_indicator_size'] ) && is_numeric( $s['dropdown_indicator_size'] )
                    ? max( 6, min( 24, intval( $s['dropdown_indicator_size'] ) ) ) : 10;
    $ind_rotate = ( $s['dropdown_indicator_rotate'] ?? '1' ) === '1';

    if ( $ind_style === 'none' ) {
        $css .= '.menux-has-children > a.menux-link::after{display:none;}';
    } elseif ( $ind_style === 'chevron' ) {
        $sz  = max( 4, (int) round( $ind_size * 0.55 ) );
        $bclr = $ind_clr ? 'border-color:' . $ind_clr . ';' : '';
        $css .= '.menux-has-children > a.menux-link::after{content:"";display:inline-block;flex-shrink:0;width:' . $sz . 'px;height:' . $sz . 'px;border-right:2px solid;border-bottom:2px solid;' . $bclr . 'transform:rotate(45deg);transition:transform .2s;margin-left:6px;vertical-align:middle;}';
        // Nested items: submenu opens rightward — chevron points right
        $css .= '.menux-has-children > .menux-submenu li .menux-has-children > a.menux-link::after{transform:rotate(-45deg);}';
        if ( $ind_rotate ) {
            $css .= '.menux-list > .menux-has-children:hover > a.menux-link::after{transform:rotate(225deg);}';
            $css .= '.menux-has-mega.menux-open > a.menux-link::after{transform:rotate(225deg);}';
            $css .= '.menux-has-children > a.mobile-sm-open::after{transform:rotate(225deg);}';
            // Nested items: override the base rotate(-45deg) rule — match same specificity so source order wins.
            $css .= '.menux-has-children > .menux-submenu li .menux-has-children > a.menux-link.mobile-sm-open::after{transform:rotate(135deg);}';
            $css .= '.menux-has-children > .menux-submenu li .menux-has-children:hover > a.menux-link::after{transform:rotate(135deg);}';
        }
    } elseif ( $ind_style === 'dot' ) {
        $dsz = max( 4, (int) round( $ind_size * 0.45 ) );
        $dbg = $ind_clr ? 'background:' . $ind_clr . ';' : 'background:currentColor;';
        $css .= '.menux-has-children > a.menux-link::after{content:"";display:inline-block;flex-shrink:0;width:' . $dsz . 'px;height:' . $dsz . 'px;border-radius:50%;' . $dbg . 'margin-left:6px;vertical-align:middle;transition:transform .2s;}';
        if ( $ind_rotate ) {
            $css .= '.menux-list > .menux-has-children:hover > a.menux-link::after,.menux-has-mega.menux-open > a.menux-link::after,.menux-has-children > a.mobile-sm-open::after{transform:scale(1.6);}';
        }
    } else { // triangle (default ▾)
        $tclr = $ind_clr ? 'color:' . $ind_clr . ';' : '';
        $css .= '.menux-has-children > a.menux-link::after{content:"▾";display:inline-block;margin-left:5px;font-size:' . $ind_size . 'px;' . $tclr . 'transition:transform .2s;}';
        $css .= '.menux-has-children > .menux-submenu li .menux-has-children > a.menux-link::after{content:"▸";}';
        if ( $ind_rotate ) {
            $css .= '.menux-list > .menux-has-children:hover > a.menux-link::after{transform:rotate(180deg);}';
            $css .= '.menux-has-mega.menux-open > a.menux-link::after{transform:rotate(180deg);}';
            $css .= '.menux-has-children > a.mobile-sm-open::after{transform:rotate(180deg);}';
        }
    }

    // Hamburger
    // Button-reset first (handles <button> default styles: grey bg, border, padding)
    // then apply user-defined background only when explicitly set.
    $hbg = ($s['hamburger_bg'] !== '')
        ? 'background:' . sanitize_hex_color($s['hamburger_bg']) . '!important;border-radius:4px;'
        : 'background:transparent!important;';
    $h_gap = '5px'; $h_height = '3px'; $h_radius = '3px'; $h_width = '30px';
    if ($s['hamburger_style'] === 'modern')  { $h_height = '4px'; $h_radius = '10px'; $h_gap = '6px'; }
    elseif ($s['hamburger_style'] === 'minimal') { $h_height = '2px'; $h_radius = '0px'; $h_gap = '7px'; $h_width = '35px'; }
    $css .= '.menux-hamburger{display:none;cursor:pointer;padding:12px;flex-direction:column;align-items:center;gap:'.$h_gap.';border:none!important;outline:none;-webkit-appearance:none!important;appearance:none!important;box-shadow:none!important;' . $hbg . '}';
    $hc = ($s['hamburger_color'] !== '') ? sanitize_hex_color($s['hamburger_color']) : '#333333';
    // Explicit width on the span avoids inheriting a theme-forced border-box that would
    // shrink the bar width to (button-width − 2×padding), making the icon look cut.
    $css .= '.menux-hamburger span{height:'.$h_height.';background:' . $hc . ';width:'.$h_width.';flex-shrink:0;display:block;transition:all 0.3s ease-in-out;border-radius:'.$h_radius.';}';

    // ════════════════════════════════════════════════════
    // MOBILE RESPONSIVE — breakpoint + modalità apertura
    // ════════════════════════════════════════════════════
    $bp              = ($s['mobile_breakpoint'] !== '') ? intval($s['mobile_breakpoint']) : 768;
    $bp_mode         = $s['mobile_breakpoint_mode'] ?? 'manual';
    $mobile_pad      = ($s['mobile_menu_pad'] !== '') ? intval($s['mobile_menu_pad']) : 0;
    $mobile_shadow   = ($s['mobile_menu_shadow'] == '1') ? 'box-shadow: 0 4px 12px rgba(0,0,0,0.1);' : '';
    $hamburger_align = esc_attr($s['hamburger_align']);
    $open_style      = $s['mobile_open_style'] ?? 'dropdown';
    $ov_bg           = ( sanitize_hex_color( $s['mobile_overlay_bg'] ?? '' ) ) ?: '#000000';
    $ov_opacity      = isset($s['mobile_overlay_opacity']) ? floatval($s["mobile_overlay_opacity"]) : 0.9;
    $ov_blur         = isset($s['mobile_overlay_blur']) ? intval($s['mobile_overlay_blur']) : 0;
    $fs_align        = esc_attr($s['mobile_fullscreen_align'] ?? 'center');
    $drawer_w        = isset($s['mobile_drawer_width']) ? intval($s['mobile_drawer_width']) : 280;
    $open_anim       = $s['mobile_open_animation'] ?? 'fade';

    // ──── Selettore CSS mobile: .menux-is-mobile (auto) oppure media query (manual) ────
    $mob_sel_open  = $bp_mode === 'auto' ? '.menux-is-mobile' : "@media(max-width:{$bp}px)";
    $desk_sel_open = $bp_mode === 'auto' ? '.menux-is-desktop' : "@media(min-width:".($bp+1)."px)";

    // Desktop: fila orizzontale
    if ($bp_mode === 'manual') {
        $css .= "@media(min-width:".($bp+1)."px){.menux-container{display:flex;flex-direction:row;align-items:center;flex-wrap:nowrap;}.menux-hamburger{display:none!important;}.menux-list{display:flex!important;flex-direction:row;align-items:center;flex:1;min-width:0;justify-content:".$nav_justify.";}}";
    } else {
        // In auto mode il JS aggiunge le classi — usiamo classi statiche
        $css .= '.menux-is-desktop .menux-hamburger{display:none!important;}';
        $css .= '.menux-is-desktop .menux-list{display:flex!important;flex-direction:row;align-items:center;flex:1;min-width:0;justify-content:'.$nav_justify.';}';
        $css .= '.menux-is-desktop .menux-container{flex-direction:row;align-items:center;flex-wrap:nowrap;}';
    }

    // ──── Overlay backdrop (usato da fullscreen e drawer) ────
    $css .= '.menux-overlay{position:fixed;inset:0;background:'.esc_attr($ov_bg).';opacity:0;z-index:99998;transition:opacity .3s ease;cursor:pointer;pointer-events:none;display:none;}';
    $css .= '.menux-overlay.visible{display:block;opacity:'.esc_attr($ov_opacity).';pointer-events:auto;}';
    if ($ov_blur > 0) {
        $css .= '.menux-overlay.visible{backdrop-filter:blur('.intval($ov_blur).'px);-webkit-backdrop-filter:blur('.intval($ov_blur).'px);}';
    }

    // ──── Animazioni keyframes ────
    $css .= '@keyframes bm-fadeIn{from{opacity:0}to{opacity:1}}';
    $css .= '@keyframes bm-slideDown{from{opacity:0;transform:translateY(-20px)}to{opacity:1;transform:translateY(0)}}';
    $css .= '@keyframes bm-slideLeft{from{opacity:0;transform:translateX(-100%)}to{opacity:1;transform:translateX(0)}}';
    $css .= '@keyframes bm-slideRight{from{opacity:0;transform:translateX(100%)}to{opacity:1;transform:translateX(0)}}';
    $css .= '@keyframes bm-scaleIn{from{opacity:0;transform:scale(.95)}to{opacity:1;transform:scale(1)}}';
    $css .= '@keyframes fadeInMenu{from{opacity:0;transform:translateY(-10px)}to{opacity:1;transform:translateY(0)}}';

    // Animazione per stile corrente
    $anim_map = array(
        'fade'  => 'bm-fadeIn .3s ease both',
        'slide' => 'bm-slideDown .3s ease both',
        'scale' => 'bm-scaleIn .3s ease both',
    );
    $anim_css = $anim_map[$open_anim] ?? 'bm-fadeIn .3s ease both';
    $anim_drawer_l = 'bm-slideLeft .3s ease both';
    $anim_drawer_r = 'bm-slideRight .3s ease both';

    // Chiudi pulsante (X) usato in fullscreen e drawer
    $close_color = ! empty( $s['close_btn_color'] ) ? sanitize_hex_color( $s['close_btn_color'] ) : '';
    $close_bg    = ! empty( $s['close_btn_bg'] )    ? sanitize_hex_color( $s['close_btn_bg'] )    : '';
    $close_color_rule = $close_color ? 'color:' . $close_color . '!important;' : 'color:#333333!important;';
    $close_bg_rule    = $close_bg    ? 'background:' . $close_bg . '!important;border-radius:50%;' : 'background:none!important;';
    $css .= '.menux-close-btn{display:none;position:fixed;top:16px;right:20px;' . $close_bg_rule . 'border:none!important;font-size:32px;line-height:1;cursor:pointer;' . $close_color_rule . 'opacity:.7;z-index:100000;padding:8px;transition:opacity .2s;}';
    $css .= '.menux-close-btn:hover{opacity:1;}';
    // Mostra il close-btn solo quando il menu è aperto E la modalità è fullscreen o drawer
    $css .= '.menux-list.show ~ .menux-close-btn{display:block;}';
    // Alternativa robusta: classe esplicita aggiunta via JS
    $css .= '.menux-close-btn.visible{display:block;}';

    // ════ BLOCCO MOBILE ════
    if ($bp_mode === 'manual') {
        $media_open  = "@media(max-width:{$bp}px){";
        $media_close = "}";
    } else {
        $media_open  = ".menux-is-mobile{";
        $media_close = "}";
    }

    // Hamburger visibile su mobile
    $mob_link_color        = ! empty( $s['mobile_link_color'] ) ? sanitize_hex_color( $s['mobile_link_color'] ) : '';
    $mob_link_hover_color  = ! empty( $s['mobile_link_hover_color'] ) ? sanitize_hex_color( $s['mobile_link_hover_color'] ) : '';
    $mob_link_active_color = ! empty( $s['mobile_link_active_color'] ) ? sanitize_hex_color( $s['mobile_link_active_color'] ) : '';
    $mob_link_color_css  = $mob_link_color        ? '.menux-list a.menux-link{color:' . $mob_link_color . '!important;}' : '';
    $mob_link_hover_css  = $mob_link_hover_color  ? '.menux-list a.menux-link:hover{color:' . $mob_link_hover_color . '!important;}' : '';
    $mob_link_active_css = $mob_link_active_color ? '.menux-list a.menux-link.menux-active,.menux-list li.current-menu-item>a.menux-link{color:' . $mob_link_active_color . '!important;}' : '';
    $css .= $media_open
        . '.menux-container{display:flex;flex-direction:row;align-items:center;flex-wrap:wrap;}'
        . '.menux-container.menux-sticky-fixed{padding-top:8px!important;padding-bottom:8px!important;}'
        . '.menux-hamburger{display:flex;' . ( $hamburger_align === 'flex-start' ? 'margin-right:auto;' : ( $hamburger_align === 'center' ? 'margin-left:auto;margin-right:auto;' : 'margin-left:auto;' ) ) . '}'
        // CTA on mobile: align-self contained, full-width in list, no side margins that break layout
        . '.menux-list .menux-cta-btn.menux-link{margin:4px 0;width:auto;align-self:center;}'
        . $mob_link_color_css
        . $mob_link_hover_css
        . $mob_link_active_css
        . $media_close;

    // ════ STILI SPECIFICI PER MODALITÀ ════
    if ($open_style === 'dropdown') {
        // ── Dropdown classico ──
        $css .= $media_open
            . '.menux-list{display:none;flex-direction:column;width:100%;gap:0;padding:'.$mobile_pad.'px;box-sizing:border-box;'.$mobile_shadow.'}'
            . '.menux-list>li{width:100%;text-align:center;border-bottom:1px solid rgba(0,0,0,0.05);}'
            . '.menux-list>li:last-child{border-bottom:none;}'
            . '.menux-list>li>a.menux-link{padding:15px;display:flex;justify-content:center;}'
            . '.menux-list.show{display:flex;animation:'.$anim_css.';position:relative;z-index:9999;}'
            . '.menux-logo{flex-shrink:0;display:inline-flex;align-items:center;}'
            . '.menux-search-wrap{margin-left:auto;}'
            . $media_close;

    } elseif ($open_style === 'fullscreen') {
        // ── Fullscreen overlay ──
        $css .= $media_open
            . '.menux-list{'
            .   'display:none;position:fixed;inset:0;z-index:99999;'
            .   'flex-direction:column;align-items:'.$fs_align.';justify-content:center;'
            .   'padding:60px '.$mobile_pad.'px '.$mobile_pad.'px;box-sizing:border-box;'
            .   'background:'.menux_hex_to_rgba($ov_bg, $ov_opacity).';'
            .   'overflow-y:auto;'
            . '}'
            . '.menux-list.show{display:flex;animation:'.$anim_css.';}'
            . '.menux-hamburger.open{display:none!important;}'
            . '.menux-list>li{width:100%;text-align:'.($fs_align==='center'?'center':($fs_align==='flex-end'?'right':'left')).';border-bottom:none;}'
            . '.menux-list>li>a.menux-link{'
            .   'padding:16px 32px;font-size:clamp(20px,4vw,32px);font-weight:700;'
            .   'display:flex;justify-content:'.$fs_align.';'
            .   'letter-spacing:.02em;transition:opacity .2s,transform .2s;'
            . '}'
            . '.menux-list>li>a.menux-link:hover{opacity:.6;transform:scale(1.04);}'
            // Stagger animazione voci
            . '.menux-list.show>li{animation:bm-slideDown .4s ease both;}'
            . '.menux-list.show>li:nth-child(1){animation-delay:.05s}'
            . '.menux-list.show>li:nth-child(2){animation-delay:.1s}'
            . '.menux-list.show>li:nth-child(3){animation-delay:.15s}'
            . '.menux-list.show>li:nth-child(4){animation-delay:.2s}'
            . '.menux-list.show>li:nth-child(5){animation-delay:.25s}'
            . '.menux-list.show>li:nth-child(6){animation-delay:.3s}'
            . '.menux-list.show>li:nth-child(n+7){animation-delay:.35s}'
            . $media_close;

    } elseif ($open_style === 'drawer-left' || $open_style === 'drawer-right') {
        // ── Drawer laterale ──
        $is_right = ($open_style === 'drawer-right');
        $drawer_side = $is_right ? 'right:0;' : 'left:0;';
        $drawer_anim = $is_right ? $anim_drawer_r : $anim_drawer_l;
        $css .= $media_open
            . '.menux-list{'
            .   'display:none;position:fixed;top:0;'.$drawer_side
            .   'width:min('.$drawer_w.'px,90vw);height:100vh;z-index:99999;'
            .   'flex-direction:column;align-items:flex-start;justify-content:flex-start;'
            .   'padding:64px 0 24px;box-sizing:border-box;'
            .   'background:'.esc_attr($ov_bg).';'
            .   ($mobile_shadow ?: 'box-shadow:'.($is_right?'-4px':' 4px').' 0 24px rgba(0,0,0,.2);')
            .   'overflow-y:auto;'
            . '}'
            . '.menux-list.show{display:flex;animation:'.$drawer_anim.';}'
            . '.menux-hamburger.open{display:none!important;}'
            . '.menux-list>li{width:100%;border-bottom:1px solid rgba(128,128,128,.1);}'
            . '.menux-list>li:last-child{border-bottom:none;}'
            . '.menux-list>li>a.menux-link{padding:14px 24px;display:flex;justify-content:flex-start;font-size:15px;}'
            . $media_close;
    }

    // ── Submenu mobile (accordion) — uguale per tutti le modalità ──
    $css .= $media_open
        . '.menux-submenu{position:static!important;box-shadow:none!important;border:none!important;border-left:3px solid rgba(0,0,0,.1)!important;border-radius:0!important;margin-left:15px;background:rgba(0,0,0,.02)!important;opacity:1!important;transform:none!important;pointer-events:auto!important;display:none!important;max-height:none!important;}'
        . '.menux-submenu.mobile-open{display:block!important;}'
        . '.menux-has-children>a.menux-link{justify-content:center;}'
        . '.menux-search-field.open{width:140px!important;}'
        . '.menux-search-results{right:auto;left:0;min-width:240px;}'
        . $media_close;

    // ── Hamburger X animation ──
    $css .= $media_open
        . '.menux-hamburger.open span:nth-child(1){transform:translateY(calc('.$h_gap.' + '.$h_height.')) rotate(45deg);}'
        . '.menux-hamburger.open span:nth-child(2){opacity:0;}'
        . '.menux-hamburger.open span:nth-child(3){transform:translateY(calc(-'.$h_gap.' - '.$h_height.')) rotate(-45deg);}'
        . $media_close;

    // ── Logo (advanced system) ──
    if ( class_exists( 'Menux_Logo' ) ) {
        $css .= Menux_Logo::generate_css();
    }

    // ── Mega Menu ──
    if ( class_exists( 'Menux_MegaMenu' ) ) {
        $css .= Menux_MegaMenu::generate_css( $s );
    }

    // ── Search ──
    if ( ! empty( $s['search_enabled'] ) && $s['search_enabled'] === '1' ) {
        $srch_color = ! empty( $s['search_icon_color'] ) ? sanitize_hex_color( $s['search_icon_color'] ) : 'currentColor';

        // Nav button
        $css .= '.menux-search-wrap{display:flex;align-items:center;flex-shrink:0;}';
        $css .= '.menux-search-btn{'
            . 'display:flex;align-items:center;justify-content:center;'
            . 'background:none;border:none;cursor:pointer;padding:8px;border-radius:8px;'
            . 'color:' . $srch_color . ';transition:opacity .2s;'
            . '}';
        $css .= '.menux-search-btn:hover{opacity:.65;}';
        $css .= '.menux-search-btn svg{width:20px;height:20px;stroke:currentColor;fill:none;stroke-width:2;stroke-linecap:round;}';

        // Modal overlay
        $css .= '#menux-search-modal{'
            . 'display:none;position:fixed;inset:0;z-index:999999;'
            . 'align-items:flex-start;justify-content:center;padding:60px 16px 16px;'
            . 'box-sizing:border-box;'
            . '}';
        $css .= '#menux-search-modal.bm-sm-open{display:flex;}';
        $css .= '#menux-search-backdrop{'
            . 'position:fixed;inset:0;background:rgba(0,0,0,.55);'
            . 'backdrop-filter:blur(4px);-webkit-backdrop-filter:blur(4px);'
            . 'animation:bm-fadeIn .2s ease both;'
            . '}';

        // Modal box
        $css .= '#menux-search-box{'
            . 'position:relative;width:100%;max-width:600px;'
            . 'background:#fff;border-radius:16px;'
            . 'box-shadow:0 24px 60px rgba(0,0,0,.3);'
            . 'overflow:hidden;display:flex;flex-direction:column;'
            . 'max-height:calc(100vh - 120px);'
            . 'animation:bm-entrance-slideDown .22s ease both;'
            . '}';

        // Header (icon + input + close)
        $css .= '.bm-sm-header{'
            . 'display:flex;align-items:center;gap:10px;'
            . 'padding:14px 16px;border-bottom:1px solid #f1f5f9;'
            . 'flex-shrink:0;'
            . '}';
        $css .= '.bm-sm-icon{display:flex;align-items:center;flex-shrink:0;color:#94a3b8;}';
        $css .= '.bm-sm-icon svg{width:18px;height:18px;stroke:currentColor;fill:none;stroke-width:2;stroke-linecap:round;}';
        $css .= '#menux-search-input-modal{'
            . 'flex:1;border:none;outline:none;font-size:16px;color:#1e293b;background:none;'
            . 'padding:0;font-family:inherit;'
            . '}';
        $css .= '#menux-search-input-modal::placeholder{color:#cbd5e1;}';
        $css .= '#menux-search-close{'
            . 'flex-shrink:0;display:flex;align-items:center;justify-content:center;'
            . 'width:28px;height:28px;border-radius:8px;border:none;background:#f1f5f9;'
            . 'color:#64748b;font-size:14px;cursor:pointer;transition:background .15s;'
            . '}';
        $css .= '#menux-search-close:hover{background:#e2e8f0;}';

        // Tabs
        $css .= '.bm-sm-tabs{'
            . 'display:flex;gap:4px;padding:8px 12px;border-bottom:1px solid #f1f5f9;'
            . 'flex-shrink:0;'
            . '}';
        $css .= '.bm-sm-tab{'
            . 'padding:5px 14px;border-radius:20px;border:none;background:none;'
            . 'font-size:12px;font-weight:600;color:#94a3b8;cursor:pointer;transition:all .15s;'
            . '}';
        $css .= '.bm-sm-tab.active{background:#f0f9ff;color:#0369a1;}';
        $css .= '.bm-sm-tab:hover:not(.active){background:#f8fafc;color:#475569;}';

        // Results body
        $css .= '.bm-sm-body{overflow-y:auto;flex:1;padding:6px 0;}';

        // Result item (menu tab)
        $css .= 'a.bm-sm-result{'
            . 'display:flex;align-items:center;gap:12px;padding:10px 16px;'
            . 'text-decoration:none;color:#1e293b;border-left:3px solid transparent;'
            . 'transition:background .12s;'
            . '}';
        $css .= 'a.bm-sm-result:hover,a.bm-sm-result.bm-sm-focused{'
            . 'background:#f8fafc;border-left-color:#0ea5e9;'
            . '}';
        $css .= '.bm-sm-result-icon{'
            . 'width:32px;height:32px;border-radius:8px;background:#f1f5f9;'
            . 'display:flex;align-items:center;justify-content:center;'
            . 'flex-shrink:0;font-size:14px;color:#64748b;'
            . '}';
        $css .= '.bm-sm-result-title{font-size:13px;font-weight:600;color:#1e293b;line-height:1.4;}';
        $css .= '.bm-sm-result-title mark{background:#fef08a;color:inherit;border-radius:2px;padding:0 1px;}';
        $css .= '.bm-sm-result-sub{font-size:11px;color:#94a3b8;margin-top:2px;word-break:break-all;}';

        // Page hit item (in-page tab)
        $css .= '.bm-sm-page-hit{'
            . 'padding:10px 16px;cursor:pointer;border-left:3px solid transparent;transition:background .12s;'
            . '}';
        $css .= '.bm-sm-page-hit:hover,.bm-sm-page-hit.bm-sm-focused{background:#f8fafc;border-left-color:#0ea5e9;}';
        $css .= '.bm-sm-ph-ctx{font-size:12px;color:#64748b;margin-top:4px;line-height:1.5;}';
        $css .= '.bm-sm-ph-ctx mark{background:#fef08a;color:inherit;border-radius:2px;padding:0 1px;}';
        $css .= '.bm-sm-ph-pos{font-size:10px;color:#94a3b8;margin-top:4px;font-weight:600;text-transform:uppercase;letter-spacing:.5px;}';

        // Empty / no-results state
        $css .= '.bm-sm-empty{'
            . 'display:flex;flex-direction:column;align-items:center;justify-content:center;'
            . 'padding:40px 20px;gap:12px;color:#94a3b8;text-align:center;'
            . '}';
        $css .= '.bm-sm-empty svg{width:32px;height:32px;stroke:currentColor;fill:none;stroke-width:1.5;stroke-linecap:round;opacity:.5;}';

        // Footer keyboard hints
        $css .= '.bm-sm-footer{'
            . 'display:flex;align-items:center;gap:12px;flex-wrap:wrap;'
            . 'padding:8px 16px;border-top:1px solid #f1f5f9;flex-shrink:0;background:#fafafa;'
            . '}';
        $css .= '.bm-sm-kbd{display:flex;align-items:center;gap:4px;font-size:11px;color:#94a3b8;}';
        $css .= '.bm-sm-kbd kbd{'
            . 'display:inline-flex;align-items:center;justify-content:center;'
            . 'min-width:20px;height:20px;padding:0 4px;'
            . 'background:#f1f5f9;border:1px solid #e2e8f0;border-radius:4px;'
            . 'font-size:11px;font-family:inherit;color:#475569;'
            . '}';
        $css .= '.bm-sm-count{margin-left:auto;font-size:11px;color:#94a3b8;font-weight:600;}';

        // Page text highlights (after modal closes, nav bar remains)
        $css .= 'mark.menux-highlight{background:#fef08a;color:inherit;border-radius:2px;padding:0 1px;}';
        $css .= 'mark.menux-highlight-current{background:#fb923c;color:#fff;border-radius:2px;padding:0 1px;}';

        // Floating nav bar (prev/next match)
        $css .= '#bm-search-nav-bar{'
            . 'display:none;position:fixed;bottom:20px;left:50%;transform:translateX(-50%);'
            . 'background:#1e293b;color:#f8fafc;border-radius:40px;'
            . 'padding:8px 16px;gap:8px;align-items:center;'
            . 'box-shadow:0 8px 24px rgba(0,0,0,.25);z-index:999999;white-space:nowrap;'
            . 'font-size:13px;font-weight:500;'
            . '}';
        $css .= '#bm-search-nav-bar.show{display:flex;animation:bm-fadeIn .2s ease both;}';
        $css .= '#bm-snb-label{min-width:48px;text-align:center;color:#94a3b8;}';
        $css .= '#bm-snb-prev,#bm-snb-next{'
            . 'background:rgba(255,255,255,.1);border:none;color:#f8fafc;'
            . 'padding:4px 12px;border-radius:20px;cursor:pointer;font-size:12px;font-weight:600;'
            . 'transition:background .15s;'
            . '}';
        $css .= '#bm-snb-prev:hover,#bm-snb-next:hover{background:rgba(255,255,255,.2);}';
        $css .= '.bm-snb-close{'
            . 'background:none;border:none;color:#94a3b8;cursor:pointer;'
            . 'padding:4px 8px;border-radius:20px;font-size:12px;transition:color .15s;'
            . '}';
        $css .= '.bm-snb-close:hover{color:#f8fafc;}';

        // Mobile adjustments for modal
        $css .= '@media(max-width:480px){'
            . '#menux-search-modal{padding:40px 8px 8px;}'
            . '#menux-search-box{border-radius:12px;}'
            . '.bm-sm-footer{display:none;}'
            . '}';
    }

    // ──── WooCommerce cart button + mini-cart ────
    if ( ! empty( $s['woo_cart_enabled'] ) && $s['woo_cart_enabled'] === '1' ) {
        $cart_color = ! empty( $s['woo_cart_icon_color'] ) ? sanitize_hex_color( $s['woo_cart_icon_color'] ) : 'currentColor';
        $badge_bg   = ! empty( $s['woo_cart_badge_bg'] )   ? sanitize_hex_color( $s['woo_cart_badge_bg'] )   : '#ef4444';

        $css .= '.menux-woo-cart-wrap{display:flex;align-items:center;flex-shrink:0;}';
        $css .= '.menux-woo-cart{position:relative;display:inline-flex;}';
        $css .= '.menux-woo-cart.menux-woo-empty{display:none;}';
        $css .= '.menux-woo-cart-btn{'
            . 'position:relative;display:inline-flex;align-items:center;gap:8px;'
            . 'color:' . $cart_color . ';text-decoration:none;font-size:18px;line-height:1;'
            . 'padding:6px;transition:opacity .15s;'
            . '}';
        $css .= '.menux-woo-cart-btn:hover{opacity:.7;}';
        $css .= '.menux-woo-cart-btn i{font-size:20px;}';
        $css .= '.menux-woo-count{'
            . 'position:absolute;top:-4px;left:18px;'
            . 'min-width:18px;height:18px;padding:0 5px;box-sizing:border-box;'
            . 'display:inline-flex;align-items:center;justify-content:center;'
            . 'background:' . $badge_bg . ';color:#fff;border-radius:9px;'
            . 'font-size:11px;font-weight:700;line-height:1;'
            . '}';
        $css .= '.menux-woo-count[data-empty="1"]{display:none;}';
        $css .= '.menux-woo-total{font-size:13px;font-weight:600;color:' . $cart_color . ';}';

        // Mini-cart dropdown
        $css .= '.menux-woo-minicart{'
            . 'display:none;position:absolute;top:100%;right:0;margin-top:8px;z-index:100000;'
            . 'width:320px;max-width:90vw;max-height:70vh;overflow-y:auto;'
            . 'background:#fff;border:1px solid #e5e7eb;border-radius:12px;'
            . 'box-shadow:0 12px 32px rgba(0,0,0,.16);padding:16px;'
            . 'color:#1e293b;text-align:left;'
            . '}';
        $css .= '.menux-woo-cart[data-minicart="hover"]:hover .menux-woo-minicart{display:block;}';
        // Open state lives on the wrapper so it survives WooCommerce fragment swaps.
        $css .= '.menux-woo-cart-wrap.menux-woo-open .menux-woo-minicart{display:block;}';
        $css .= '.menux-woo-minicart-empty{margin:0;padding:12px 0;color:#94a3b8;font-size:13px;text-align:center;}';
        // Light touch-up of WooCommerce's default mini-cart markup
        $css .= '.menux-woo-minicart ul.cart_list,.menux-woo-minicart ul.product_list_widget{list-style:none;margin:0;padding:0;}';
        $css .= '.menux-woo-minicart ul.cart_list li,.menux-woo-minicart ul.product_list_widget li{'
            . 'position:relative;padding:10px 0 10px 4px;border-bottom:1px solid #f1f5f9;font-size:13px;line-height:1.5;'
            . '}';
        $css .= '.menux-woo-minicart ul.cart_list li a{color:#1e293b;text-decoration:none;font-weight:600;}';
        $css .= '.menux-woo-minicart ul.cart_list li img{width:48px;height:auto;float:right;margin-left:8px;border-radius:6px;}';
        $css .= '.menux-woo-minicart .woocommerce-mini-cart__total{display:flex;justify-content:space-between;align-items:center;padding:12px 0;font-size:14px;border-top:1px solid #e5e7eb;margin-top:4px;}';
        $css .= '.menux-woo-minicart .woocommerce-mini-cart__buttons{display:flex;gap:8px;flex-wrap:wrap;margin:0;}';
        $css .= '.menux-woo-minicart .woocommerce-mini-cart__buttons .button{'
            . 'flex:1;text-align:center;padding:8px 12px;border-radius:8px;font-size:13px;font-weight:600;text-decoration:none;white-space:nowrap;'
            . '}';
        // Position-aware dropdown anchoring
        $css .= '.menux-woo-pos-left .menux-woo-minicart{right:auto;left:0;}';

        // On mobile, keep the dropdown within the viewport
        $css .= '@media(max-width:480px){.menux-woo-minicart{position:fixed;top:auto;left:8px;right:8px;width:auto;}}';
    }

    return $css;
}
