<?php
/**
 * MenuX Free — Footer CSS Generator
 * Generates the front-end CSS for the footer from saved style settings.
 * @package MenuX
 */

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

function menux_generate_footer_css( $style = array() ) {
    $s = wp_parse_args( (array) $style, menux_footer_style_defaults() );

    $hex = function( $v, $fallback ) {
        $c = sanitize_hex_color( $v );
        return $c ? $c : $fallback;
    };
    $int = function( $v, $fallback, $min, $max ) {
        return ( $v !== '' && is_numeric( $v ) ) ? max( $min, min( $max, intval( $v ) ) ) : $fallback;
    };

    $bg        = $hex( $s['bg'], '#0f172a' );
    $grad      = menux_sanitize_bg_gradient( $s['bg_gradient'] ?? '' );
    $headc     = $hex( $s['heading_color'], '#f1f5f9' );
    $textc     = $hex( $s['text_color'], '#94a3b8' );
    $linkc     = $hex( $s['link_color'], '#cbd5e1' );
    $linkh     = $hex( $s['link_hover_color'], '#ffffff' );
    $accent    = $hex( $s['accent_color'], '#667eea' );
    $py        = $int( $s['padding_y'], 56, 16, 160 );
    $px        = $int( $s['padding_x'], 24, 0, 120 );
    $gap       = $int( $s['col_gap'], 48, 8, 120 );
    $maxw      = $int( $s['max_width'], 1180, 600, 1920 );
    $lgap      = $int( $s['link_gap'], 12, 2, 40 );
    $hsize     = $int( $s['heading_size'], 14, 10, 28 );
    $hweight   = in_array( $s['heading_weight'], array( '400', '500', '600', '700', '800' ), true ) ? $s['heading_weight'] : '700';
    $htrans    = $s['heading_transform'] === 'none' ? 'none' : 'uppercase';
    $fsize     = $int( $s['font_size'], 14, 11, 20 );
    $hover_fx  = in_array( $s['link_hover_effect'], array( 'none', 'underline', 'slide' ), true ) ? $s['link_hover_effect'] : 'slide';
    $soc_style = in_array( $s['social_style'], array( 'plain', 'circle', 'square' ), true ) ? $s['social_style'] : 'circle';
    $soc_size  = $int( $s['social_size'], 16, 12, 28 );
    $bbg       = $hex( $s['bottom_bg'], '#0b1120' );
    $btc       = $hex( $s['bottom_text_color'], '#64748b' );
    $m_align   = $s['mobile_align'] === 'center' ? 'center' : 'left';

    $bg_css = $grad ? 'background:' . $grad . ';' : 'background:' . $bg . ';';

    $css = '';

    // ── Container ──
    $css .= '.menux-footer{' . $bg_css . 'color:' . $textc . ';font-size:' . $fsize . 'px;line-height:1.7;position:relative;width:100%;box-sizing:border-box;}';
    $css .= '.menux-footer *,.menux-footer *::before,.menux-footer *::after{box-sizing:border-box;}';
    if ( ( $s['accent_bar'] ?? '1' ) === '1' ) {
        $ah   = $int( $s['accent_height'], 3, 1, 10 );
        $css .= '.menux-footer::before{content:"";display:block;height:' . $ah . 'px;background:linear-gradient(90deg,' . $accent . ',' . $accent . 'cc);}';
    }
    $css .= '.menux-footer-inner{max-width:' . $maxw . 'px;margin:0 auto;padding:' . $py . 'px ' . $px . 'px;}';

    // ── Columns grid (layout class set by the renderer) ──
    $css .= '.menux-footer-cols{display:grid;gap:' . $gap . 'px;grid-template-columns:repeat(var(--menux-fcols,4),1fr);}';
    $css .= '.menux-footer-cols.menux-flayout-wide-first{grid-template-columns:1.6fr repeat(calc(var(--menux-fcols,4) - 1),1fr);}';
    $css .= '.menux-footer-cols.menux-flayout-centered{grid-template-columns:repeat(var(--menux-fcols,4),1fr);text-align:center;}';
    $css .= '.menux-footer-cols.menux-flayout-centered .menux-footer-social{justify-content:center;}';

    // ── Headings ──
    $css .= '.menux-footer-heading{margin:0 0 18px;font-size:' . $hsize . 'px;font-weight:' . $hweight . ';color:' . $headc . ';text-transform:' . $htrans . ';letter-spacing:' . ( $htrans === 'uppercase' ? '.08em' : '0' ) . ';}';

    // ── Logo in About column ──
    $css .= '.menux-footer-logo{display:block;margin-bottom:14px;}';

    // ── Text & lists ──
    $css .= '.menux-footer p{margin:0 0 12px;}';
    $css .= '.menux-footer-list{list-style:none;margin:0;padding:0;display:flex;flex-direction:column;gap:' . $lgap . 'px;}';
    $css .= '.menux-footer a{color:' . $linkc . ';text-decoration:none;transition:color .2s ease' . ( $hover_fx === 'slide' ? ',padding-left .2s ease' : '' ) . ';}';
    $css .= '.menux-footer a:hover{color:' . $linkh . ';}';
    if ( $hover_fx === 'underline' ) {
        $css .= '.menux-footer-list a{background-image:linear-gradient(' . $linkh . ',' . $linkh . ');background-size:0 1px;background-position:0 100%;background-repeat:no-repeat;transition:background-size .25s ease,color .2s ease;}';
        $css .= '.menux-footer-list a:hover{background-size:100% 1px;}';
    } elseif ( $hover_fx === 'slide' ) {
        $css .= '.menux-footer-list a:hover{padding-left:6px;}';
    }

    // ── Contact rows ──
    $css .= '.menux-footer-contact{list-style:none;margin:0;padding:0;display:flex;flex-direction:column;gap:' . $lgap . 'px;}';
    $css .= '.menux-footer-contact li{display:flex;align-items:flex-start;gap:10px;}';
    $css .= '.menux-footer-contact i{color:' . $accent . ';font-size:' . $fsize . 'px;margin-top:4px;flex-shrink:0;}';
    $css .= '.menux-flayout-centered .menux-footer-contact li{justify-content:center;}';

    // ── Social icons ──
    $css .= '.menux-footer-social{display:flex;flex-wrap:wrap;gap:10px;margin-top:4px;}';
    if ( $soc_style === 'plain' ) {
        $css .= '.menux-footer-social a{display:inline-flex;align-items:center;justify-content:center;font-size:' . ( $soc_size + 2 ) . 'px;color:' . $linkc . ';}';
        $css .= '.menux-footer-social a:hover{color:' . $accent . ';transform:translateY(-2px);}';
    } else {
        $radius = $soc_style === 'circle' ? '50%' : '8px';
        $box    = $soc_size + 20;
        $css .= '.menux-footer-social a{display:inline-flex;align-items:center;justify-content:center;width:' . $box . 'px;height:' . $box . 'px;border-radius:' . $radius . ';font-size:' . $soc_size . 'px;color:' . $linkc . ';background:rgba(128,128,128,.14);transition:all .2s ease;}';
        $css .= '.menux-footer-social a:hover{background:' . $accent . ';color:#ffffff;transform:translateY(-2px);}';
    }
    $css .= '.menux-footer-social a{text-decoration:none;}';

    // ── Bottom bar ──
    $css .= '.menux-footer-bottom{background:' . $bbg . ';color:' . $btc . ';font-size:' . max( 11, $fsize - 1 ) . 'px;' . ( ( $s['bottom_divider'] ?? '1' ) === '1' ? 'border-top:1px solid rgba(128,128,128,.16);' : '' ) . '}';
    $css .= '.menux-footer-bottom-inner{max-width:' . $maxw . 'px;margin:0 auto;padding:18px ' . $px . 'px;display:flex;align-items:center;justify-content:space-between;gap:16px;flex-wrap:wrap;}';
    $css .= '.menux-footer-bottom a{color:' . $btc . ';}';
    $css .= '.menux-footer-bottom a:hover{color:' . $linkh . ';}';
    $css .= '.menux-footer-legal{list-style:none;margin:0;padding:0;display:flex;gap:20px;flex-wrap:wrap;}';
    $css .= '.menux-footer-bottom .menux-footer-social{margin-top:0;}';

    // ── Newsletter form ──
    $css .= '.menux-footer-newsletter p{margin:0 0 14px;}';
    $css .= '.menux-newsletter-form{display:flex;flex-direction:column;gap:8px;}';
    $css .= '.menux-newsletter-row{display:flex;}';
    $css .= '.menux-newsletter-row input[type="email"]{flex:1;padding:10px 14px;border:1px solid rgba(128,128,128,.22);background:rgba(255,255,255,.06);color:' . $textc . ';font-size:' . $fsize . 'px;border-radius:6px 0 0 6px;outline:none;min-width:0;}';
    $css .= '.menux-newsletter-row input[type="email"]::placeholder{color:' . $textc . ';opacity:.55;}';
    $css .= '.menux-newsletter-row button[type="submit"]{padding:10px 18px;background:' . $accent . ';color:#ffffff;border:none;border-radius:0 6px 6px 0;font-size:' . $fsize . 'px;font-weight:600;cursor:pointer;white-space:nowrap;transition:opacity .2s;}';
    $css .= '.menux-newsletter-row button[type="submit"]:hover{opacity:.85;}';
    $css .= '.menux-newsletter-gdpr{font-size:11px;color:' . $textc . ';opacity:.75;display:flex;align-items:flex-start;gap:6px;cursor:pointer;line-height:1.5;}';
    $css .= '.menux-newsletter-gdpr input[type="checkbox"]{margin-top:2px;flex-shrink:0;}';

    // ── Scroll-to-top (only emit when feature is enabled to avoid class-name collisions) ──
    if ( ( $s['scroll_top'] ?? '0' ) === '1' ) {
        $css .= '.menux-scroll-top{position:fixed;bottom:28px;right:24px;width:44px;height:44px;border-radius:50%;background:' . $accent . ';color:#ffffff;border:none;cursor:pointer;font-size:17px;display:flex;align-items:center;justify-content:center;opacity:0;transform:translateY(10px);transition:opacity .25s,transform .25s;z-index:9998;box-shadow:0 4px 18px rgba(0,0,0,.22);pointer-events:none;}';
        $css .= '.menux-scroll-top.is-visible{opacity:1;transform:translateY(0);pointer-events:auto;}';
        $css .= '.menux-scroll-top:hover{filter:brightness(1.12);}';
    }

    // ── Mobile-hidden columns ──
    $css .= '@media(max-width:600px){.menux-fcol-hidden-mobile{display:none!important;}}';

    // ── Tablet (≤900px): 2-column grid for all layouts ──
    $css .= '@media(max-width:900px){'
          . '.menux-footer-cols,.menux-footer-cols.menux-flayout-wide-first,.menux-footer-cols.menux-flayout-centered'
          . '{grid-template-columns:repeat(2,1fr);}' // centered loses text-align:center at this width — restored at 600px
          . '}';

    // ── Mobile (≤600px): single column ──
    $css .= '@media(max-width:600px){';
    $css .= '.menux-footer-cols,.menux-footer-cols.menux-flayout-wide-first,.menux-footer-cols.menux-flayout-centered{grid-template-columns:1fr;gap:' . max( 24, intval( $gap * 0.7 ) ) . 'px;text-align:' . $m_align . ';}';
    if ( $m_align === 'center' ) {
        $css .= '.menux-footer-social{justify-content:center;}.menux-footer-contact li{justify-content:center;}';
    }
    $css .= '.menux-footer-inner{padding:' . max( 28, intval( $py * 0.7 ) ) . 'px 20px;}';
    $css .= '.menux-footer-bottom-inner{flex-direction:column;justify-content:center;text-align:center;}';
    $css .= '.menux-footer-legal{justify-content:center;}';
    $css .= '}';

    // ── Newsletter form: stack on very narrow screens ──
    $css .= '@media(max-width:480px){'
          . '.menux-newsletter-row{flex-wrap:wrap;}'
          . '.menux-newsletter-row input[type="email"]{border-radius:6px;min-width:100%;}'
          . '.menux-newsletter-row button[type="submit"]{border-radius:6px;width:100%;margin-top:4px;}'
          . '}';

    // ── User custom CSS (scoped to footer) ──
    $custom = isset( $s['custom_css'] ) ? trim( wp_strip_all_tags( $s['custom_css'] ) ) : '';
    if ( $custom !== '' ) {
        $css .= $custom;
    }

    return $css;
}
