<?php
/**
 * Design System settings helper.
 *
 * @package Polanger_Dashboard_Tools
 */

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

/**
 * Provides settings, defaults, and sanitization.
 */
class PDT_Design_Settings {
  const OPTION_KEY = 'pdt_design_settings';
  const CSS_META_OPTION = 'pdt_design_css_meta';

  /**
   * Defaults.
   *
   * @return array
   */
  public function get_defaults() {
    return array(
      'enabled'                 => false,
      'scope'                   => 'polanger_only',
      'selected_screens'        => array( 'polanger' ),
      'smart_contrast'          => true,
      'preset'                  => 'default',
      'color_primary'           => '#4f46e5',
      'color_bg'                => '#f4f7fb',
      'color_surface'           => '#ffffff',
      'color_text'              => '#0f172a',
      'color_muted'             => '#66758a',
      'sidebar_bg'              => '#111827',
      'sidebar_text'            => '#f8fafc',
      'adminbar_bg'             => '#0f172a',
      'adminbar_text'           => '#f8fafc',
      'adminbar_submenu_bg'     => '#172033',
      'adminbar_submenu_text'   => '#f8fafc',
      'color_border'            => '#d7dfeb',
      'color_hover'             => '#edf2ff',
      'color_focus'             => '#93c5fd',
      'color_success'           => '#16a34a',
      'color_warning'           => '#d97706',
      'color_error'             => '#dc2626',
      'color_table_head'        => '#e8eeff',
      'color_input_bg'          => '#ffffff',
      'font_family'             => 'Inter, sans-serif',
      'type_scale'              => '100',
      'density'                 => 'cozy',
      'spacing_scale'           => '100',
      'border_radius'           => '10',
      'button_radius_preset'    => 'rounded',
      'link_enabled'            => false,
      'link_scope'              => 'content_only',
      'link_color'              => '#4338ca',
      'link_hover'              => '#312e81',
      'link_visited'            => '#7c3aed',
      'link_active'             => '#3730a3',
      'link_decoration'         => 'hover',
      'link_weight'             => 'inherit',
      'custom_css'              => '',
    );
  }

  /**
   * Scope options.
   *
   * @return array<string,string>
   */
  public function get_scope_options() {
    return array(
      'polanger_only'             => __( 'Polanger pages only', 'polanger-admin-menu-manager' ),
      'all_admin'                 => __( 'All admin pages', 'polanger-admin-menu-manager' ),
      'selected_screens'          => __( 'Only selected admin screens', 'polanger-admin-menu-manager' ),
      'all_admin_exclude_plugins' => __( 'All admin pages except plugin-owned screens', 'polanger-admin-menu-manager' ),
    );
  }

  /**
   * Screen selection options.
   *
   * @return array<string,string>
   */
  public function get_screen_options() {
    return array(
      'polanger'          => __( 'Polanger screens', 'polanger-admin-menu-manager' ),
      'dashboard'         => __( 'Dashboard', 'polanger-admin-menu-manager' ),
      'posts'             => __( 'Posts', 'polanger-admin-menu-manager' ),
      'media'             => __( 'Media', 'polanger-admin-menu-manager' ),
      'pages'             => __( 'Pages', 'polanger-admin-menu-manager' ),
      'comments'          => __( 'Comments', 'polanger-admin-menu-manager' ),
      'appearance'        => __( 'Appearance', 'polanger-admin-menu-manager' ),
      'plugins'           => __( 'Plugins', 'polanger-admin-menu-manager' ),
      'users'             => __( 'Users', 'polanger-admin-menu-manager' ),
      'tools'             => __( 'Tools', 'polanger-admin-menu-manager' ),
      'settings'          => __( 'Settings', 'polanger-admin-menu-manager' ),
      'custom_post_types' => __( 'Custom post types', 'polanger-admin-menu-manager' ),
    );
  }

  /**
   * Font options.
   *
   * @return array<string,string>
   */
  public function get_font_options() {
    return array(
      'Inter, sans-serif'                                                   => __( 'Inter', 'polanger-admin-menu-manager' ),
      'system-ui, sans-serif'                                               => __( 'System UI', 'polanger-admin-menu-manager' ),
      '"Segoe UI", sans-serif'                                              => __( 'Segoe UI', 'polanger-admin-menu-manager' ),
      'Roboto, sans-serif'                                                  => __( 'Roboto', 'polanger-admin-menu-manager' ),
      '"Helvetica Neue", sans-serif'                                        => __( 'Helvetica Neue', 'polanger-admin-menu-manager' ),
      'system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif' => __( 'System Sans Stack', 'polanger-admin-menu-manager' ),
      'ui-sans-serif, system-ui, sans-serif'                                => __( 'UI Sans', 'polanger-admin-menu-manager' ),
      '-apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif'           => __( 'Apple/Windows UI', 'polanger-admin-menu-manager' ),
      'Arial, Helvetica, sans-serif'                                        => __( 'Arial', 'polanger-admin-menu-manager' ),
      'Tahoma, "Segoe UI", sans-serif'                                      => __( 'Tahoma UI', 'polanger-admin-menu-manager' ),
      'Verdana, Geneva, sans-serif'                                         => __( 'Verdana', 'polanger-admin-menu-manager' ),
      'Georgia, "Times New Roman", serif'                                   => __( 'Georgia Serif', 'polanger-admin-menu-manager' ),
      'Cambria, Georgia, serif'                                             => __( 'Cambria Serif', 'polanger-admin-menu-manager' ),
      '"Trebuchet MS", "Segoe UI", sans-serif'                              => __( 'Trebuchet Sans', 'polanger-admin-menu-manager' ),
      'Consolas, "Courier New", monospace'                                  => __( 'Consolas Mono', 'polanger-admin-menu-manager' ),
    );
  }

  /**
   * Type scale options.
   *
   * @return array<string,string>
   */
  public function get_type_scale_options() {
    return array(
      '95'  => __( 'Compact (95%)', 'polanger-admin-menu-manager' ),
      '100' => __( 'Default (100%)', 'polanger-admin-menu-manager' ),
      '105' => __( 'Comfortable (105%)', 'polanger-admin-menu-manager' ),
      '110' => __( 'Large (110%)', 'polanger-admin-menu-manager' ),
    );
  }

  /**
   * Density options.
   *
   * @return array<string,string>
   */
  public function get_density_options() {
    return array(
      'compact'     => __( 'Compact', 'polanger-admin-menu-manager' ),
      'cozy'        => __( 'Cozy', 'polanger-admin-menu-manager' ),
      'comfortable' => __( 'Comfortable', 'polanger-admin-menu-manager' ),
    );
  }

  /**
   * Spacing scale options.
   *
   * @return array<string,string>
   */
  public function get_spacing_scale_options() {
    return array(
      '90'  => __( 'Tight (90%)', 'polanger-admin-menu-manager' ),
      '100' => __( 'Default (100%)', 'polanger-admin-menu-manager' ),
      '115' => __( 'Relaxed (115%)', 'polanger-admin-menu-manager' ),
    );
  }

  /**
   * Button radius options.
   *
   * @return array<string,string>
   */
  public function get_button_radius_options() {
    return array(
      'inherit' => __( 'Match global radius', 'polanger-admin-menu-manager' ),
      'soft'    => __( 'Soft corners', 'polanger-admin-menu-manager' ),
      'rounded' => __( 'Rounded buttons', 'polanger-admin-menu-manager' ),
      'pill'    => __( 'Pill buttons', 'polanger-admin-menu-manager' ),
    );
  }

  /**
   * Presets map.
   *
   * @return array<string,array>
   */
  public function get_presets() {
    $defaults = $this->get_defaults();

		$presets = array(
			'default' => $defaults,
			'dark'    => array_merge(
				$defaults,
        array(
          'color_primary'         => '#7c83ff',
          'color_bg'              => '#08111f',
          'color_surface'         => '#0f1729',
          'color_text'            => '#e6edf7',
          'color_muted'           => '#92a1ba',
          'sidebar_bg'            => '#050b16',
          'sidebar_text'          => '#f8fbff',
          'adminbar_bg'           => '#050b16',
          'adminbar_text'         => '#f8fbff',
          'adminbar_submenu_bg'   => '#0d1627',
          'adminbar_submenu_text' => '#f8fbff',
          'color_border'          => '#1e2a40',
          'color_hover'           => '#132038',
          'color_focus'           => '#60a5fa',
          'color_success'         => '#22c55e',
          'color_warning'         => '#f59e0b',
          'color_error'           => '#f87171',
          'color_table_head'      => '#121d31',
          'color_input_bg'        => '#0b1424',
          'font_family'           => 'Inter, sans-serif',
          'type_scale'            => '100',
          'density'               => 'cozy',
          'spacing_scale'         => '105',
          'border_radius'         => '12',
          'button_radius_preset'  => 'rounded',
          'link_color'            => '#9cc3ff',
          'link_hover'            => '#d0e2ff',
          'link_visited'          => '#c4b5fd',
          'link_active'           => '#ffffff',
        )
      ),
      'minimal' => array_merge(
        $defaults,
        array(
          'color_primary'         => '#1d4ed8',
          'color_bg'              => '#f8fafc',
          'color_surface'         => '#ffffff',
          'color_text'            => '#0f172a',
          'color_muted'           => '#64748b',
          'sidebar_bg'            => '#f8fafc',
          'sidebar_text'          => '#111827',
          'adminbar_bg'           => '#ffffff',
          'adminbar_text'         => '#0f172a',
          'adminbar_submenu_bg'   => '#f1f5f9',
          'adminbar_submenu_text' => '#0f172a',
          'color_border'          => '#dde5ef',
          'color_hover'           => '#f3f6fb',
          'color_focus'           => '#93c5fd',
          'color_success'         => '#15803d',
          'color_warning'         => '#b45309',
          'color_error'           => '#b91c1c',
          'color_table_head'      => '#f6f8fb',
          'color_input_bg'        => '#ffffff',
          'font_family'           => 'system-ui, sans-serif',
          'type_scale'            => '100',
          'density'               => 'compact',
          'spacing_scale'         => '95',
          'border_radius'         => '8',
          'button_radius_preset'  => 'soft',
          'link_color'            => '#1d4ed8',
          'link_hover'            => '#1e40af',
          'link_visited'          => '#6d28d9',
          'link_active'           => '#1e3a8a',
        )
      ),
      'agency'  => array_merge(
        $defaults,
        array(
          'color_primary'         => '#0284c7',
          'color_bg'              => '#edf5ff',
          'color_surface'         => '#ffffff',
          'color_text'            => '#0b1324',
          'color_muted'           => '#516072',
          'sidebar_bg'            => '#0b1324',
          'sidebar_text'          => '#f8fafc',
          'adminbar_bg'           => '#0b1324',
          'adminbar_text'         => '#f8fafc',
          'adminbar_submenu_bg'   => '#111d34',
          'adminbar_submenu_text' => '#f8fafc',
          'color_border'          => '#cfddf1',
          'color_hover'           => '#e0efff',
          'color_focus'           => '#7dd3fc',
          'color_success'         => '#16a34a',
          'color_warning'         => '#f59e0b',
          'color_error'           => '#ef4444',
          'color_table_head'      => '#dbeafe',
          'color_input_bg'        => '#ffffff',
          'font_family'           => 'Inter, sans-serif',
          'type_scale'            => '105',
          'density'               => 'comfortable',
          'spacing_scale'         => '115',
          'border_radius'         => '12',
          'button_radius_preset'  => 'rounded',
          'link_color'            => '#0369a1',
          'link_hover'            => '#075985',
          'link_visited'          => '#7c3aed',
          'link_active'           => '#0c4a6e',
        )
      ),
      'forest'  => array_merge(
        $defaults,
        array(
          'color_primary'         => '#2f7a5c',
          'color_bg'              => '#f4faf6',
          'color_surface'         => '#ffffff',
          'color_text'            => '#0f172a',
          'color_muted'           => '#5f6f64',
          'sidebar_bg'            => '#123524',
          'sidebar_text'          => '#f7fcf8',
          'adminbar_bg'           => '#123524',
          'adminbar_text'         => '#f7fcf8',
          'adminbar_submenu_bg'   => '#194830',
          'adminbar_submenu_text' => '#f7fcf8',
          'color_border'          => '#d5e5db',
          'color_hover'           => '#eaf5ee',
          'color_focus'           => '#6ee7b7',
          'color_success'         => '#15803d',
          'color_warning'         => '#ca8a04',
          'color_error'           => '#dc2626',
          'color_table_head'      => '#e3f2e8',
          'color_input_bg'        => '#ffffff',
          'font_family'           => 'system-ui, sans-serif',
          'type_scale'            => '100',
          'density'               => 'cozy',
          'spacing_scale'         => '105',
          'border_radius'         => '10',
          'button_radius_preset'  => 'soft',
          'link_color'            => '#166534',
          'link_hover'            => '#14532d',
          'link_visited'          => '#6d28d9',
          'link_active'           => '#14532d',
        )
      ),
      'warm'    => array_merge(
        $defaults,
        array(
          'color_primary'         => '#c2410c',
          'color_bg'              => '#fdf8f3',
          'color_surface'         => '#ffffff',
          'color_text'            => '#1f2937',
          'color_muted'           => '#6b7280',
          'sidebar_bg'            => '#3f2416',
          'sidebar_text'          => '#fff7ed',
          'adminbar_bg'           => '#3f2416',
          'adminbar_text'         => '#fff7ed',
          'adminbar_submenu_bg'   => '#5b3320',
          'adminbar_submenu_text' => '#fff7ed',
          'color_border'          => '#eadccd',
          'color_hover'           => '#fff1e6',
          'color_focus'           => '#fdba74',
          'color_success'         => '#15803d',
          'color_warning'         => '#d97706',
          'color_error'           => '#dc2626',
          'color_table_head'      => '#ffedd5',
          'color_input_bg'        => '#fffdfa',
          'font_family'           => '"Helvetica Neue", sans-serif',
          'type_scale'            => '100',
          'density'               => 'comfortable',
          'spacing_scale'         => '108',
          'border_radius'         => '12',
          'button_radius_preset'  => 'soft',
          'link_color'            => '#9a3412',
          'link_hover'            => '#7c2d12',
          'link_visited'          => '#7c3aed',
          'link_active'           => '#7c2d12',
        )
			),
		);

		if ( class_exists( 'PDT_Design_Themes' ) ) {
			$presets = array_merge( $presets, PDT_Design_Themes::get_presets( $defaults ) );
		}

		return $presets;
	}

  /**
   * Human-friendly preset labels.
   *
   * @return array<string,string>
   */
  public function get_preset_labels() {
		$labels = array(
			'default' => __( 'Classic Blue', 'polanger-admin-menu-manager' ),
			'dark'    => __( 'Midnight', 'polanger-admin-menu-manager' ),
			'minimal' => __( 'Minimal Editorial', 'polanger-admin-menu-manager' ),
      'agency'  => __( 'Agency Pro', 'polanger-admin-menu-manager' ),
			'forest'  => __( 'Forest Calm', 'polanger-admin-menu-manager' ),
			'warm'    => __( 'Warm Studio', 'polanger-admin-menu-manager' ),
		);

		if ( class_exists( 'PDT_Design_Themes' ) ) {
			$labels = array_merge( $labels, PDT_Design_Themes::get_labels() );
		}

		return $labels;
	}

  /**
   * Get merged settings.
   *
   * @return array
   */
  public function get_settings() {
    $stored = get_option( self::OPTION_KEY, array() );
    if ( ! is_array( $stored ) ) {
      $stored = array();
    }

    return array_merge( $this->get_defaults(), $stored );
  }

  /**
   * Save settings after sanitization.
   *
   * @param array $raw Raw payload.
   * @return array
   */
  public function save_settings( $raw ) {
    $previous = $this->get_settings();
    $raw = $this->prepare_raw_settings( $raw, $previous );
    $settings = $this->sanitize_settings( $raw );
    update_option( self::OPTION_KEY, $settings );
    return $settings;
  }

  /**
   * Prepare raw settings before sanitization.
   *
   * @param array $raw      Raw submitted settings.
   * @param array $previous Optional previously saved settings.
   * @return array
   */
  public function prepare_raw_settings( $raw, $previous = null ) {
    if ( null === $previous ) {
      $previous = $this->get_settings();
    }

    return $this->apply_selected_preset_to_raw( $raw, $previous );
  }

  /**
   * Apply a newly selected preset while preserving fields the user edited.
   *
   * @param array $raw Raw submitted settings.
   * @param array $previous Previously saved settings.
   * @return array
   */
  private function apply_selected_preset_to_raw( $raw, $previous ) {
    $raw = is_array( $raw ) ? $raw : array();
    $previous = is_array( $previous ) ? $previous : $this->get_defaults();

    $preset_key = isset( $raw['preset'] ) ? sanitize_key( $raw['preset'] ) : '';
    $presets = $this->get_presets();
    if ( ! isset( $presets[ $preset_key ] ) ) {
      return $raw;
    }

    $previous_preset = isset( $previous['preset'] ) ? sanitize_key( $previous['preset'] ) : 'default';
    if ( $preset_key === $previous_preset ) {
      return $raw;
    }

    // Selecting a preset should make the chosen design visible immediately.
    $raw['enabled'] = '1';

    $preset = $presets[ $preset_key ];
    foreach ( $this->get_preset_managed_keys() as $key ) {
      if ( ! array_key_exists( $key, $preset ) ) {
        continue;
      }

      $previous_value = array_key_exists( $key, $previous ) ? $previous[ $key ] : null;
      $submitted_value = array_key_exists( $key, $raw ) ? $raw[ $key ] : null;

      if ( array_key_exists( $key, $raw ) && ! $this->setting_values_match( $submitted_value, $previous_value ) ) {
        continue;
      }

      $raw[ $key ] = $preset[ $key ];
    }

    return $raw;
  }

  /**
   * Return fields controlled by design presets.
   *
   * @return array
   */
  private function get_preset_managed_keys() {
    return array(
      'color_primary',
      'color_bg',
      'color_surface',
      'color_text',
      'color_muted',
      'sidebar_bg',
      'sidebar_text',
      'adminbar_bg',
      'adminbar_text',
      'adminbar_submenu_bg',
      'adminbar_submenu_text',
      'color_border',
      'color_hover',
      'color_focus',
      'color_success',
      'color_warning',
      'color_error',
      'color_table_head',
      'color_input_bg',
      'font_family',
      'type_scale',
      'density',
      'spacing_scale',
      'border_radius',
      'button_radius_preset',
      'link_color',
      'link_hover',
      'link_visited',
      'link_active',
      'link_decoration',
      'link_weight',
      'link_scope',
    );
  }

  /**
   * Compare submitted and saved scalar settings.
   *
   * @param mixed $left First value.
   * @param mixed $right Second value.
   * @return bool
   */
  private function setting_values_match( $left, $right ) {
    if ( is_array( $left ) || is_array( $right ) ) {
      return wp_json_encode( $left ) === wp_json_encode( $right );
    }

    return strtolower( trim( (string) $left ) ) === strtolower( trim( (string) $right ) );
  }

  /**
   * Get CSS metadata.
   *
   * @return array
   */
  public function get_css_meta() {
    $meta = get_option( self::CSS_META_OPTION, array() );
    return is_array( $meta ) ? $meta : array();
  }

  /**
   * Store CSS metadata.
   *
   * @param array $meta Meta data.
   * @return void
   */
  public function set_css_meta( $meta ) {
    update_option( self::CSS_META_OPTION, $meta );
  }

  /**
   * Sanitize settings payload.
   *
   * @param array $raw Raw data.
   * @return array
   */
  public function sanitize_settings( $raw ) {
    $raw = is_array( $raw ) ? $raw : array();
    $defaults = $this->get_defaults();

    $settings = $defaults;
    $settings['enabled'] = ! empty( $raw['enabled'] );
    $settings['smart_contrast'] = ! empty( $raw['smart_contrast'] );

    $scope = isset( $raw['scope'] ) ? sanitize_key( $raw['scope'] ) : $defaults['scope'];
    $scope_options = array_keys( $this->get_scope_options() );
    $settings['scope'] = in_array( $scope, $scope_options, true ) ? $scope : $defaults['scope'];

    $selected_screens = isset( $raw['selected_screens'] ) && is_array( $raw['selected_screens'] ) ? wp_unslash( $raw['selected_screens'] ) : array();
    $allowed_screens = array_keys( $this->get_screen_options() );
    $selected_screens = array_values( array_intersect( array_map( 'sanitize_key', $selected_screens ), $allowed_screens ) );
    $settings['selected_screens'] = ! empty( $selected_screens ) ? $selected_screens : $defaults['selected_screens'];

    $preset = isset( $raw['preset'] ) ? sanitize_key( $raw['preset'] ) : 'default';
    $settings['preset'] = array_key_exists( $preset, $this->get_presets() ) ? $preset : 'default';

    $color_keys = array(
      'color_primary',
      'color_bg',
      'color_surface',
      'color_text',
      'color_muted',
      'sidebar_bg',
      'sidebar_text',
      'adminbar_bg',
      'adminbar_text',
      'adminbar_submenu_bg',
      'adminbar_submenu_text',
      'color_border',
      'color_hover',
      'color_focus',
      'color_success',
      'color_warning',
      'color_error',
      'color_table_head',
      'color_input_bg',
      'link_color',
      'link_hover',
      'link_visited',
      'link_active',
    );

    foreach ( $color_keys as $key ) {
      if ( isset( $raw[ $key ] ) ) {
        $sanitized = sanitize_hex_color( $raw[ $key ] );
        $settings[ $key ] = $sanitized ? $sanitized : $defaults[ $key ];
      }
    }

    $settings['link_enabled'] = ! empty( $raw['link_enabled'] );

    $link_scope = isset( $raw['link_scope'] ) ? sanitize_key( $raw['link_scope'] ) : 'content_only';
    $settings['link_scope'] = in_array( $link_scope, array( 'content_only', 'all_admin' ), true ) ? $link_scope : 'content_only';

    $link_decoration = isset( $raw['link_decoration'] ) ? sanitize_key( $raw['link_decoration'] ) : 'hover';
    $settings['link_decoration'] = in_array( $link_decoration, array( 'none', 'underline', 'hover' ), true ) ? $link_decoration : 'hover';

    $allowed_weights = array( 'inherit', '400', '500', '600', '700' );
    $link_weight = isset( $raw['link_weight'] ) ? sanitize_text_field( $raw['link_weight'] ) : 'inherit';
    $settings['link_weight'] = in_array( $link_weight, $allowed_weights, true ) ? $link_weight : 'inherit';

    $allowed_fonts = array_keys( $this->get_font_options() );
    $font_family = isset( $raw['font_family'] ) ? sanitize_text_field( $raw['font_family'] ) : $defaults['font_family'];
    $settings['font_family'] = in_array( $font_family, $allowed_fonts, true ) ? $font_family : $defaults['font_family'];

    $type_scale = isset( $raw['type_scale'] ) ? sanitize_text_field( $raw['type_scale'] ) : $defaults['type_scale'];
    $settings['type_scale'] = array_key_exists( $type_scale, $this->get_type_scale_options() ) ? $type_scale : $defaults['type_scale'];

    $density = isset( $raw['density'] ) ? sanitize_key( $raw['density'] ) : $defaults['density'];
    $settings['density'] = array_key_exists( $density, $this->get_density_options() ) ? $density : $defaults['density'];

    $spacing_scale = isset( $raw['spacing_scale'] ) ? sanitize_text_field( $raw['spacing_scale'] ) : $defaults['spacing_scale'];
    $settings['spacing_scale'] = array_key_exists( $spacing_scale, $this->get_spacing_scale_options() ) ? $spacing_scale : $defaults['spacing_scale'];

    $button_radius_preset = isset( $raw['button_radius_preset'] ) ? sanitize_key( $raw['button_radius_preset'] ) : $defaults['button_radius_preset'];
    $settings['button_radius_preset'] = array_key_exists( $button_radius_preset, $this->get_button_radius_options() ) ? $button_radius_preset : $defaults['button_radius_preset'];

    $radius = isset( $raw['border_radius'] ) ? absint( $raw['border_radius'] ) : absint( $defaults['border_radius'] );
    $settings['border_radius'] = (string) min( 24, max( 0, $radius ) );

    $custom_css = isset( $raw['custom_css'] ) ? wp_unslash( $raw['custom_css'] ) : '';
    $settings['custom_css'] = $this->sanitize_custom_css( $custom_css );

    return $settings;
  }

  /**
   * Sanitize developer-provided custom CSS.
   *
   * @param string $css Raw CSS.
   * @return string
   */
  private function sanitize_custom_css( $css ) {
    $css = is_string( $css ) ? $css : '';
    $css = wp_kses_no_null( $css );
    $css = preg_replace( '#</?(?:style|script)[^>]*>#i', '', $css );

    return trim( (string) $css );
  }
}
