jQuery(document).ready(function($) {
    // Auto-size buttons based on container
    function adjustButtonSizes() {
        $('.wc-wp-inquiry-by-xd-button:not(.fixed-size)').each(function() {
            var $button = $(this);
            var $container = $button.closest('.product, .summary, .cart');
            
            if ($container.length) {
                var containerWidth = $container.width();
                var buttonWidth = $button.outerWidth();
                
                // If button is wider than 80% of container, make it full width
                if (buttonWidth > (containerWidth * 0.8)) {
                    $button.css({
                        'width': '100%',
                        'display': 'block'
                    });
                } else {
                    $button.css({
                        'width': 'auto',
                        'display': 'inline-block'
                    });
                }
            }
        });
    }
    
    // Run on load and window resize
    adjustButtonSizes();
    $(window).on('resize', adjustButtonSizes);
    
    // Track inquiries
    $('.wc-wp-inquiry-by-xd-button').on('click', function() {
        var productId = $(this).closest('.product').data('product_id') || 0;
        if (productId) {
            $.ajax({
                url: wc_wp_inquiry_by_xd_vars.ajaxurl,
                type: 'POST',
                data: {
                    action: 'wc_wp_inquiry_by_xd_track',
                    product_id: productId,
                    security: wc_wp_inquiry_by_xd_vars.nonce
                }
            });
        }
    });
});