/* global jQuery, wp */
(function ($) {
  'use strict';

  /* ──────────────────────────────────────────── *
   *  Helpers
   * ──────────────────────────────────────────── */
  function getCurrentKirkiDevice() {
    return $('body').attr('data-kirki-device') || 'desktop';
  }
  function elementExists(selector) {
    return $(selector).length > 0;
  }

  /* ──────────────────────────────────────────── *
   *  Live-preview for CSS-type controls
   * ──────────────────────────────────────────── */
  function livePreview(settingId, selector, cssProperty, unit, defaultProperty) {
    unit = unit || '';
    defaultProperty = defaultProperty || null;

    wp.customize(settingId, function (value) {
      value.bind(function (newval) {

        if (!elementExists(selector)) { return; }

        /* — Change class — */
        if (cssProperty === 'class') {
          $(selector).each(function () {
            var $el = $(this);
            var oldClasses = ($el.attr('class') || '').split(' ');

            for (var i = 0; i < oldClasses.length; i++) {
              if (oldClasses[i].indexOf('author-kit-') === 0) {
                $el.removeClass(oldClasses[i]);
              }
            }
            if (newval) {
              $el.addClass(String(newval).replace('#', ''));
            }
          });
          return;
        }

        /* — Change id — */
        if (cssProperty === 'id') {
          $(selector).attr('id', String(newval).replace('#', ''));
          return;
        }

        /* — Responsive object values — */
        if ($.isPlainObject(newval)) {
          var device = getCurrentKirkiDevice();
          var responsiveValue = (newval[device] !== undefined) ? newval[device] : newval;

          if ($.isPlainObject(responsiveValue)) {

            var props = defaultProperty ?
              {
                top: defaultProperty + '-top',
                right: defaultProperty + '-right',
                bottom: defaultProperty + '-bottom',
                left: defaultProperty + '-left'
              } : {
                top: 'top',
                right: 'right',
                bottom: 'bottom',
                left: 'left'
              };

            $.each(responsiveValue, function (key, val) {
              if (val !== '') {
                $(selector).css(props[key] || cssProperty.replace('%key%', key), val + unit);
              }
            });

          } else if (responsiveValue !== '') {
            $(selector).css(cssProperty, responsiveValue + unit);
          }
          return;
        }

        /* — Simple scalar values — */
        if (newval !== '') {
          $(selector).css(cssProperty, newval + unit);
        }
      });
    });
  }

  /* ──────────────────────────────────────────── *
   *  Live-preview for text controls
   * ──────────────────────────────────────────── */
  function liveTextPreview(settingId, selector) {
    wp.customize(settingId, function (value) {
      value.bind(function (newval) {
        if (elementExists(selector)) {
          $(selector).text(newval);
        }
      });
    });
  }

  /* ──────────────────────────────────────────── *
   *  SETTINGS ARRAY  (defined confg)
   * ──────────────────────────────────────────── */
  var settings = [
    // Template 1 settings
    {
      template: 'template-default',
      css: [
        {
          settingId: 'author_kit_bio_box_size_default',
          selector: '#author-bio-container',
          property: '%key%', // This will be replaced with the key (width, height, etc.)
          unit: '' // Add unit if needed (like px or %)
        },
        {
          settingId: 'author_kit_avatar_size_default',
          selector: '#author-bio-container #author-kit-bio-box div #author-avatar-container',
          property: '%key%', // This will be replaced with the key (width, height, etc.)
          unit: 'px' // Add unit if needed (like px or %)
        },
        {
          settingId: 'author_kit_bio_box_margin_default',
          selector: '#author-bio-container',
          property: '%key%', // This will be replaced with the key (margins, etc.)
          unit: 'px' // Add unit if needed (like px or %)
        },
        {
          settingId: 'author_kit_icons_style_default',
          selector: 'a.author-kit-social-icon, .author-kit-social-icons-list a',
          property: 'id',
        },
        { settingId: 'author_kit_bio_box_bg_color_default', selector: '#author-bio-container', property: 'background-color' },
        { settingId: 'author_kit_bio_box_border_color_default', selector: '#author-bio-container', property: 'border-color' },
        { settingId: 'author_kit_bio_box_heading_bg_color_default', selector: '#about-author-heading', property: 'background-color' },
        { settingId: 'author_kit_bio_box_heading_txt_color_default', selector: '#about-author-heading', property: 'color' },
        { settingId: 'author_kit_subinfo_txt_color_default', selector: '#author-bio-container #author-info-placeholder', property: 'color' },
        { settingId: 'author_kit_bio_text_color_default', selector: '#author-bio-container #author-bio-text', property: 'color' },
        { settingId: 'author_kit_name_txt_color_default', selector: '#author-bio-container #author-name', property: 'color' },
        { settingId: 'author_kit_name_font_size_default', selector: '#author-name', property: 'font-size', unit: 'px' },
        { settingId: 'author_kit_subinfo_font_size_default', selector: '#author-info-placeholder', property: 'font-size', unit: 'px' },
        { settingId: 'author_kit_bio_font_size_default', selector: '#author-bio-text', property: 'font-size', unit: 'px' },
        { settingId: 'author_kit_bio_box_border_radius_default', selector: '#author-bio-container', property: 'border-radius' },
        { settingId: 'author_kit_bio_box_border_thickness_default', selector: '#author-bio-container', property: 'border-width' },
        { settingId: 'author_kit_icons_color_default', selector: '.author-kit-social-icons-list > a > i', property: 'color' },
        { settingId: 'author_kit_icons_container_color_default', selector: '.author-kit-social-icons-list > a', property: 'background-color' },
        { settingId: 'author_kit_icons_gap_default', selector: '#author-bio-container #author-kit-bio-box  div.author-kit-social-icons-list', property: 'gap', unit: 'px' },
        { settingId: 'author_kit_icons_size_default', selector: '.author-kit-social-icons-list > a > i', property: 'font-size', unit: 'px' },
        {
          settingId: 'author_kit_icons_size_default',
          selector: '.author-kit-social-icons-list a.author-kit-social-icon',
          property: 'width',
          unit: 'px'
        },
        {
          settingId: 'author_kit_icons_size_default',
          selector: '.author-kit-social-icons-list a.author-kit-social-icon',
          property: 'height',
          unit: 'px'
        },

      ],
      text: [
        { settingId: 'author_kit_bio_box_heading_text_default', selector: '#author-bio-container #about-author-heading' },
        { settingId: 'author_kit_subinfo_default_field_default', selector: '#author-bio-container #author-info-placeholder' }

      ]
    },
    // Template 2 settings
    {
      template: 'template-memoir',
      css: [
        {
          settingId: 'author_kit_bio_box_size_memoir',
          selector: '#author-bio-container',
          property: '%key%', // This will be replaced with the key (width, height, etc.)
          unit: '' // Add unit if needed (like px or %)
        },
        {
          settingId: 'author_kit_avatar_size_memoir',
          selector: '#author-bio-container #author-kit-bio-box div #author-avatar-container',
          property: '%key%', // This will be replaced with the key (width, height, etc.)
          unit: 'px' // Add unit if needed (like px or %)
        },
        {
          settingId: 'author_kit_bio_box_margin_memoir',
          selector: '#author-bio-container',
          property: '%key%', // This will be replaced with the key (margins, etc.)
          unit: 'px' // Add unit if needed (like px or %)
        },
        {
          settingId: 'author_kit_icons_style_memoir',
          selector: 'a.author-kit-social-icon, .author-kit-social-icons-list a ',
          property: 'id',
        },
        { settingId: 'author_kit_bio_box_bg_color_memoir', selector: '#author-bio-container', property: 'background-color' },
        { settingId: 'author_kit_bio_box_border_color_memoir', selector: '#author-bio-container', property: 'border-color' },
        { settingId: 'author_kit_bio_box_heading_bg_color_memoir', selector: '#about-author-heading', property: 'background-color' },
        { settingId: 'author_kit_bio_box_heading_txt_color_memoir', selector: '#about-author-heading', property: 'color' },
        { settingId: 'author_kit_subinfo_txt_color_memoir', selector: '#author-bio-container #author-info-placeholder', property: 'color' },
        { settingId: 'author_kit_bio_text_color_memoir', selector: '#author-bio-container #author-bio-text', property: 'color' },
        { settingId: 'author_kit_name_txt_color_memoir', selector: '#author-bio-container #author-name', property: 'color' },
        { settingId: 'author_kit_name_font_size_memoir', selector: '#author-name', property: 'font-size', unit: 'px' },
        { settingId: 'author_kit_subinfo_font_size_memoir', selector: '#author-info-placeholder', property: 'font-size', unit: 'px' },
        { settingId: 'author_kit_bio_font_size_memoir', selector: '#author-bio-text', property: 'font-size', unit: 'px' },
        { settingId: 'author_kit_bio_box_border_radius_memoir', selector: '#author-bio-container', property: 'border-radius' },
        { settingId: 'author_kit_bio_box_border_thickness_memoir', selector: '#author-bio-container', property: 'border-width' },
        { settingId: 'author_kit_icons_color_memoir', selector: '.author-kit-social-icons-list > a > i', property: 'color' },
        { settingId: 'author_kit_icons_container_color_memoir', selector: '.author-kit-social-icons-list > a', property: 'background-color' },
        { settingId: 'author_kit_icons_gap_memoir', selector: '.author-kit-social-icons-list', property: 'gap', unit: 'px' },
        { settingId: 'author_kit_icons_size_memoir', selector: '.author-kit-social-icons-list > a > i', property: 'font-size', unit: 'px' }
      ],
      text: [
        { settingId: 'author_kit_bio_box_heading_text_memoir', selector: '#author-bio-container #about-author-heading' }
      ]
    },
    // Template 3 settings
    {
      template: 'template-monarch',
      css: [
        {
          settingId: 'author_kit_bio_box_size_monarch',
          selector: '#author-bio-container',
          property: '%key%', // This will be replaced with the key (width, height, etc.)
          unit: '' // Add unit if needed (like px or %)
        },
        {
          settingId: 'author_kit_avatar_size_monarch',
          selector: '#author-bio-container #author-kit-bio-box div #author-avatar-container',
          property: '%key%', // This will be replaced with the key (width, height, etc.)
          unit: 'px' // Add unit if needed (like px or %)
        },
        {
          settingId: 'author_kit_bio_box_margin_monarch',
          selector: '#author-bio-container',
          property: '%key%', // This will be replaced with the key (margins, etc.)
          unit: 'px' // Add unit if needed (like px or %)
        },
        {
          settingId: 'author_kit_icons_style_monarch',
          selector: 'a.author-kit-social-icon, .author-kit-social-icons-list a ',
          property: 'id',
        },
        { settingId: 'author_kit_bio_box_bg_color_monarch', selector: '#author-bio-container', property: 'background-color' },
        { settingId: 'author_kit_bio_box_border_color_monarch', selector: '#author-bio-container', property: 'border-color' },
        { settingId: 'author_kit_bio_box_heading_bg_color_monarch', selector: '#about-author-heading', property: 'background-color' },
        { settingId: 'author_kit_bio_box_heading_txt_color_monarch', selector: '#about-author-heading', property: 'color' },
        { settingId: 'author_kit_subinfo_txt_color_monarch', selector: '#author-bio-container #author-info-placeholder', property: 'color' },
        { settingId: 'author_kit_bio_text_color_monarch', selector: '#author-bio-container #author-bio-text', property: 'color' },
        { settingId: 'author_kit_name_txt_color_monarch', selector: '#author-bio-container #author-name', property: 'color' },
        { settingId: 'author_kit_name_font_size_monarch', selector: '#author-name', property: 'font-size', unit: 'px' },
        { settingId: 'author_kit_subinfo_font_size_monarch', selector: '#author-info-placeholder', property: 'font-size', unit: 'px' },
        { settingId: 'author_kit_bio_font_size_monarch', selector: '#author-bio-text', property: 'font-size', unit: 'px' },
        { settingId: 'author_kit_bio_box_border_radius_monarch', selector: '#author-bio-container', property: 'border-radius' },
        { settingId: 'author_kit_bio_box_border_thickness_monarch', selector: '#author-bio-container', property: 'border-width' },
        { settingId: 'author_kit_icons_color_monarch', selector: '.author-kit-social-icons-list > a > i', property: 'color' },
        { settingId: 'author_kit_icons_container_color_monarch', selector: '.author-kit-social-icons-list > a', property: 'background-color' },
        { settingId: 'author_kit_icons_gap_monarch', selector: '.author-kit-social-icons-list', property: 'gap', unit: 'px' },
        { settingId: 'author_kit_icons_size_monarch', selector: '.author-kit-social-icons-list > a > i', property: 'font-size', unit: 'px' }
      ],
      text: [
        { settingId: 'author_kit_bio_box_heading_text_monarch', selector: '#author-bio-container #about-author-heading' }
      ]
    },


    // for byline template default
    {
      template: 'byline_template_default',
      css: [
        {
          settingId: 'author_kit_byline_dimensions_default',
          selector: '.author-kit-byline-container',
          property: '%key%' // dynamically replaced with width or height
        },
        {
          settingId: 'author_kit_byline_bg_color_default',
          selector: '.author-kit-byline-container',
          property: 'background-color'
        },
        {
          settingId: 'author_kit_byline_text_color_default',
          selector: '.author-kit-main-author-label, .author-kit-contributions-label, .author-kit-last-updated, .author-kit-published-date, .author-kit-byline-container a, .author-kit-category-label, .author-kit-category-label > i',
          property: 'color'
        },
        {
          settingId: 'author_kit_byline_font_size_default',
          selector: '.author-kit-main-author-label, .author-kit-contributions-label, .author-kit-last-updated, .author-kit-published-date, .author-kit-byline-container a, .author-kit-category-label, .author-kit-category-label > i, .author-kit-separator',
          property: 'font-size',
          unit: 'px'
        },
        {
          settingId: 'author_kit_byline_link_text_color_default',
          selector: '.author-kit-byline-container a.post-category, .author-kit-comment-link a.comments-section-link',
          property: 'color'
        },
        {
          settingId: 'author_kit_byline_hide_comments_default',
          selector: '.author-kit-comment-link a.comments-section-link, .author-kit-byline-container #comment-separator',
          property: 'display'
        },
        {
          settingId: 'author_kit_byline_hide_last_updated_date_default',
          selector: '.author-kit-last-updated, .author-kit-byline-container #last-updated-date-separator',
          property: 'display'
        },
        {
          settingId: 'author_kit_byline_hide_post_date_default',
          selector: '.author-kit-published-date, .author-kit-byline-container #published-date-separator',
          property: 'display'
        },
        {
          settingId: 'author_kit_byline_hide_byline_categories_default',
          selector: '.author-kit-byline-container #category-container, .author-kit-byline-container #categories-separator',
          property: 'display'
        },
        {
          settingId: 'author_kit_byline_hide_author_avatar_default',
          selector: '.author-kit-main-author > .author-kit-author-avatar, .author-kit-coauthor > .author-kit-coauthor-avatar',
          property: 'display'
        },
        {
          settingId: 'author_kit_byline_separator_color_default',
          selector: '.author-kit-separator, .author-kit-byline-container #comment-separator, .author-kit-byline-container #last-updated-date-separator, .author-kit-byline-container #published-date-separator, .author-kit-byline-container #categories-separator',
          property: 'color'
        },
        {
          settingId: 'author_kit_byline_alignment_default',
          selector: '.author-kit-main-author-contributors',
          property: 'justify-content'
        },
        {
          settingId: 'author_kit_byline_alignment_default',
          selector: '.author-kit-byline-container',
          property: 'justify-content'
        },

      ],
      text: [
        { settingId: 'author_kit_byline_written_by_label_default', selector: '.author-kit-main-author-label' },
        { settingId: 'author_kit_byline_contribution_from_label_default', selector: '.author-kit-contributions-label' },
        { settingId: 'author_kit_byline_separator_label_default', selector: '.author-kit-separator' }
      ]
    },

    // for byline template advo
    {
      template: 'byline_template_advo',
      css: [
        {
          settingId: 'author_kit_byline_dimensions_advo',
          selector: '.author-kit-byline-container',
          property: '%key%' // dynamically replaced with width or height
        },
        {
          settingId: 'author_kit_byline_bg_color_advo',
          selector: '.author-kit-byline-container',
          property: 'background-color'
        },
        {
          settingId: 'author_kit_byline_text_color_advo',
          selector: '.author-kit-main-author-label, .author-kit-contributions-label, .author-kit-last-updated, .author-kit-published-date, .author-kit-byline-container a, .author-kit-category-label, .author-kit-category-label > i',
          property: 'color'
        },
        {
          settingId: 'author_kit_byline_font_size_advo',
          selector: '.author-kit-main-author-label, .author-kit-contributions-label, .author-kit-last-updated, .author-kit-published-date, .author-kit-byline-container a, .author-kit-category-label, .author-kit-category-label > i, .author-kit-separator',
          property: 'font-size',
          unit: 'px'
        },
        {
          settingId: 'author_kit_byline_link_text_color_advo',
          selector: '.author-kit-byline-container a.post-category, .author-kit-comment-link a.comments-section-link',
          property: 'color'
        },
        {
          settingId: 'author_kit_byline_hide_comments_advo',
          selector: '.author-kit-comment-link a.comments-section-link, .author-kit-byline-container #comment-separator',
          property: 'display'
        },
        {
          settingId: 'author_kit_byline_hide_last_updated_date_advo',
          selector: '.author-kit-last-updated, .author-kit-byline-container #last-updated-date-separator',
          property: 'display'
        },
        {
          settingId: 'author_kit_byline_hide_post_date_advo',
          selector: '.author-kit-published-date, .author-kit-byline-container #published-date-separator',
          property: 'display'
        },
        {
          settingId: 'author_kit_byline_hide_byline_categories_advo',
          selector: '.author-kit-byline-container #category-container, .author-kit-byline-container #categories-separator',
          property: 'display'
        },
        {
          settingId: 'author_kit_byline_hide_author_avatar_advo',
          selector: '.author-kit-main-author > .author-kit-author-avatar, .author-kit-coauthor > .author-kit-coauthor-avatar',
          property: 'display'
        },
        {
          settingId: 'author_kit_byline_separator_color_advo',
          selector: '.author-kit-separator, .author-kit-byline-container #comment-separator, .author-kit-byline-container #last-updated-date-separator, .author-kit-byline-container #published-date-separator, .author-kit-byline-container #categories-separator',
          property: 'color'
        },
        {
          settingId: 'author_kit_byline_alignment_advo',
          selector: '.author-kit-main-author-contributors',
          property: 'justify-content'
        },
        {
          settingId: 'author_kit_byline_alignment_advo',
          selector: '.author-kit-byline-container',
          property: 'justify-content'
        }

      ],
      text: [
        { settingId: 'author_kit_byline_written_by_label_advo', selector: '.author-kit-main-author-label' },
        { settingId: 'author_kit_byline_contribution_from_label_advo', selector: '.author-kit-contributions-label' },
        { settingId: 'author_kit_byline_separator_label_advo', selector: '.author-kit-separator' }
      ]
    },

    // for byline template elantra
    {
      template: 'byline_template_elantra',
      css: [
        {
          settingId: 'author_kit_byline_dimensions_elantra',
          selector: '.author-kit-byline-container',
          property: '%key%' // dynamically replaced with width or height
        },
        {
          settingId: 'author_kit_byline_bg_color_elantra',
          selector: '.author-kit-byline-container',
          property: 'background-color'
        },
        {
          settingId: 'author_kit_byline_text_color_elantra',
          selector: '.author-kit-main-author-label, .author-kit-contributions-label, .author-kit-last-updated, .author-kit-published-date, .author-kit-byline-container a, .author-kit-category-label, .author-kit-category-label > i',
          property: 'color'
        },
        {
          settingId: 'author_kit_byline_font_size_elantra',
          selector: '.author-kit-main-author-label, .author-kit-contributions-label, .author-kit-last-updated, .author-kit-published-date, .author-kit-byline-container a, .author-kit-category-label, .author-kit-category-label > i, .author-kit-separator',
          property: 'font-size',
          unit: 'px'
        },
        {
          settingId: 'author_kit_byline_link_text_color_elantra',
          selector: '.author-kit-byline-container a.post-category, .author-kit-comment-link a.comments-section-link',
          property: 'color'
        },
        {
          settingId: 'author_kit_byline_hide_comments_elantra',
          selector: '.author-kit-comment-link a.comments-section-link, .author-kit-byline-container #comment-separator',
          property: 'display'
        },
        {
          settingId: 'author_kit_byline_hide_last_updated_date_elantra',
          selector: '.author-kit-last-updated, .author-kit-byline-container #last-updated-date-separator',
          property: 'display'
        },
        {
          settingId: 'author_kit_byline_hide_post_date_elantra',
          selector: '.author-kit-published-date, .author-kit-byline-container #published-date-separator',
          property: 'display'
        },
        {
          settingId: 'author_kit_byline_hide_byline_categories_elantra',
          selector: '.author-kit-byline-container #category-container, .author-kit-byline-container #categories-separator',
          property: 'display'
        },
        {
          settingId: 'author_kit_byline_hide_author_avatar_elantra',
          selector: '.author-kit-main-author > .author-kit-author-avatar, .author-kit-coauthor > .author-kit-coauthor-avatar',
          property: 'display'
        },
        {
          settingId: 'author_kit_byline_separator_color_elantra',
          selector: '.author-kit-separator, .author-kit-byline-container #comment-separator, .author-kit-byline-container #last-updated-date-separator, .author-kit-byline-container #published-date-separator, .author-kit-byline-container #categories-separator',
          property: 'color'
        },
        {
          settingId: 'author_kit_byline_alignment_elantra',
          selector: '.author-kit-main-author-contributors',
          property: 'justify-content'
        },
        {
          settingId: 'author_kit_byline_alignment_elantra',
          selector: '.author-kit-byline-container',
          property: 'justify-content'
        }

      ],
      text: [
        { settingId: 'author_kit_byline_written_by_label_elantra', selector: '.author-kit-main-author-label' },
        { settingId: 'author_kit_byline_contribution_from_label_elantra', selector: '.author-kit-contributions-label' },
        { settingId: 'author_kit_byline_separator_label_elantra', selector: '.author-kit-separator' }
      ]
    },

    {
      template: 'author-archive-template-default',
      css: [
        {
          settingId: 'author_kit_page_archive_width',
          selector: '#author-kit-archive-template',
          property: 'width'
        },
        {
          settingId: 'author_kit_background_color_achive',
          selector: '#author-kit-archive-template',
          property: 'background-color'
        },
        {
          settingId: 'author_kit_archive_bio_box_bg_color',
          selector: '.author-kit-bio-container',
          property: 'background-color'
        },
        {
          settingId: 'author_kit_archive_bio_box_width',
          selector: '.author-kit-bio-container',
          property: 'max-width',
          unit: '%'
        },
        {
          settingId: 'author_kit_archive_bio_box_padding',
          selector: '.author-kit-bio-container',
          property: 'padding'
        },
        {
          settingId: 'author_kit_archive_avatar_size',
          selector: '.author-kit-avatar',
          property: 'width',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_avatar_size',
          selector: '.author-kit-avatar',
          property: 'height',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_icon_style',
          selector: 'a.author-kit-social-icon, .author-kit-social-icons-list a ',
          property: 'id',
        },
        {
          settingId: 'author_kit_archive_social_icons_size',
          selector: 'a.author-kit-social-icon i',
          property: 'font-size',
        },

        {
          settingId: 'author_kit_archive_name_color',
          selector: '.author-kit-name',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_name_size',
          selector: '.author-kit-name',
          property: 'font-size',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_job_title_color',
          selector: '.author-kit-job-title',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_job_title_size',
          selector: '.author-kit-job-title',
          property: 'font-size',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_description_color',
          selector: '.author-kit-description',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_description_size',
          selector: '.author-kit-description',
          property: 'font-size',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_icon_container_color',
          selector: '.author-kit-social-icons-list  a.author-kit-social-icon',
          property: 'background-color'
        },
        {
          settingId: 'author_kit_archive_social_icon_color',
          selector: '.author-kit-social-icons-list > a > i',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_social_icons_gap',
          selector: '#author-kit-bio-box div.author-kit-social-icons-list',
          property: 'gap',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_grid_gap',
          selector: '.author-kit-posts-grid',
          property: 'gap',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_post_card_width',
          selector: '.author-kit-posts-grid',
          property: 'grid-template-columns',
        },
        {
          settingId: 'author_kit_archive_post_card_bg_color',
          selector: '.author-kit-post-item',
          property: 'background-color'
        },
        {
          settingId: 'author_kit_archive_thumbnail_border_radius',
          selector: '.author-kit-post-item  a  img',
          property: 'border-radius',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_post_title_size',
          selector: 'h4.author-kit-post-title, #author-kit-archive-template h4.author-kit-post-title a',
          property: 'font-size',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_title_color',
          selector: '.author-kit-post-title, #author-kit-archive-template h4.author-kit-post-title a',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_excerpt_size',
          selector: '.author-kit-post-item .author-kit-post-excerpt > p',
          property: 'font-size',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_excerpt_color',
          selector: '.author-kit-post-item div.author-kit-post-excerpt',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_post_meta_size',
          selector: '.author-kit-post-meta',
          property: 'font-size',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_post_meta_color',
          selector: '.author-kit-post-meta .author-name , .author-kit-post-meta .post-date ',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_post_meta_separator_color',
          selector: '.author-kit-post-meta',
          property: 'border-color'
        },
        {
          settingId: 'author_kit_archive_grid_text_alignment',
          selector: '.author-kit-posts-title',
          property: 'justify-content'
        },
        {
          settingId: 'author_kit_archive_pagination_alignment',
          selector: '#author-kit-archive-template .author-kit-pagination-container',
          property: 'justify-content'
        },
        {
          settingId: 'author_kit_grid_archive_title',
          selector: '#author-kit-archive-template .author-kit-posts-title',
          property: 'font-size',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_post_grid_title_color',
          selector: '#author-kit-archive-template .author-kit-posts-title h3',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_post_grid_bg_color',
          selector: '#author-kit-archive-template .author-kit-posts, #author-kit-archive-template .author-kit-pagination-container',
          property: 'background-color'
        },
        {
          settingId: 'author_kit_archive_button_color',
          selector: '.author-kit-pagination-prev,.author-kit-pagination-next',
          property: 'background-color'
        },
        {
          settingId: 'author_kit_archive_pagination_btn_color',
          selector: '.author-kit-pagination-prev,.author-kit-pagination-next',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_pagination_btn_color',
          selector: '.author-kit-pagination-container .current .author-kit-pagination-page',
          property: 'color'
        },

        {
          settingId: 'author_kit_archive_button_color',
          selector: '.author-kit-pagination-container .current .author-kit-pagination-page',
          property: 'background-color'
        },
        {
          settingId: 'author_kit_archive_pagination_number_color',
          selector: '.author-kit-pagination a.page-numbers:not(page-numbers.current) .author-kit-pagination-page',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_pagination_number_bg_color',
          selector: '.author-kit-pagination a.page-numbers:not(page-numbers.current) .author-kit-pagination-page',
          property: 'background-color'
        },




      ],
      text: [
        { settingId: 'author_kit_grid_archive_title', selector: '#author-kit-archive-template .author-kit-posts-title  #post-title' },


      ]
    },
    {
      template: 'author-archive-template-scribe',
      css: [
        {
          settingId: 'author_kit_page_archive_width_scribe',
          selector: '#author-kit-archive-template',
          property: 'width'
        },
        {
          settingId: 'author_kit_background_color_achive_scribe',
          selector: '#author-kit-archive-template',
          property: 'background-color'
        },
        {
          settingId: 'author_kit_archive_bio_box_bg_color_scribe',
          selector: '.author-kit-bio-container',
          property: 'background-color'
        },
        {
          settingId: 'author_kit_archive_bio_box_width_scribe',
          selector: '.author-kit-bio-container',
          property: 'max-width',
          unit: '%'
        },
        {
          settingId: 'author_kit_archive_bio_box_padding_scribe',
          selector: '.author-kit-bio-container',
          property: 'padding'
        },
        {
          settingId: 'author_kit_archive_avatar_size_scribe',
          selector: '.author-kit-avatar',
          property: 'width',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_avatar_size_scribe',
          selector: '.author-kit-avatar',
          property: 'height',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_icon_style_scribe',
          selector: 'a.author-kit-social-icon, .author-kit-social-icons-list a',
          property: 'id'
        },
        {
          settingId: 'author_kit_archive_social_icons_size_scribe',
          selector: 'a.author-kit-social-icon i',
          property: 'font-size'
        },
        {
          settingId: 'author_kit_archive_name_color_scribe',
          selector: '.author-kit-name',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_name_size_scribe',
          selector: '.author-kit-name',
          property: 'font-size',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_job_title_color_scribe',
          selector: '.author-kit-job-title',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_job_title_size_scribe',
          selector: '.author-kit-job-title',
          property: 'font-size',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_description_color_scribe',
          selector: '.author-kit-description',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_description_size_scribe',
          selector: '.author-kit-description',
          property: 'font-size',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_icon_container_color_scribe',
          selector: '.author-kit-social-icons-list  a.author-kit-social-icon',
          property: 'background-color'
        },
        {
          settingId: 'author_kit_archive_social_icon_color_scribe',
          selector: '.author-kit-social-icons-list > a > i',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_social_icons_gap_scribe',
          selector: '#author-kit-bio-box div.author-kit-social-icons-list',
          property: 'gap',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_grid_gap_scribe',
          selector: '.author-kit-posts-grid',
          property: 'gap',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_post_card_width_scribe',
          selector: '.author-kit-posts-grid',
          property: 'grid-template-columns'
        },
        {
          settingId: 'author_kit_archive_post_card_bg_color_scribe',
          selector: '.author-kit-post-item',
          property: 'background-color'
        },
        {
          settingId: 'author_kit_archive_thumbnail_border_radius_scribe',
          selector: '.author-kit-post-item  a  img',
          property: 'border-radius',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_post_title_size_scribe',
          selector: 'h4.author-kit-post-title, #author-kit-archive-template h4.author-kit-post-title a',
          property: 'font-size',
          unit: 'px'
        },
     
        {
          settingId: 'author_kit_archive_title_color_scribe',
          selector: '.author-kit-post-title, h4.author-kit-post-title, .author-kit-post-item  a h4 , #author-kit-archive-template h4',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_excerpt_size_scribe',
          selector: '.author-kit-post-item .author-kit-post-excerpt > p',
          property: 'font-size',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_excerpt_color_scribe',
          selector: '.author-kit-post-item div.author-kit-post-excerpt',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_post_meta_size_scribe',
          selector: '.author-kit-post-meta',
          property: 'font-size',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_post_meta_color_scribe',
          selector: '.author-kit-post-meta .author-name , .author-kit-post-meta .post-date ',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_post_meta_separator_color_scribe',
          selector: '.author-kit-post-meta',
          property: 'border-color'
        },
        {
          settingId: 'author_kit_archive_grid_text_alignment_scribe',
          selector: '.author-kit-posts-title',
          property: 'justify-content'
        },
        {
          settingId: 'author_kit_archive_pagination_alignment_scribe',
          selector: '#author-kit-archive-template .author-kit-pagination-container',
          property: 'justify-content'
        },
        {
          settingId: 'author_kit_archive_post_grid_title_size_scribe',
          selector: '#author-kit-archive-template .author-kit-posts-title',
          property: 'font-size',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_post_grid_title_color_scribe',
          selector: '#author-kit-archive-template .author-kit-posts-title h3',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_post_grid_bg_color_scribe',
          selector: '#author-kit-archive-template .author-kit-posts, #author-kit-archive-template .author-kit-pagination-container',
          property: 'background-color'
        },
        {
          settingId: 'author_kit_archive_button_color_scribe',
          selector: '.author-kit-pagination-prev,.author-kit-pagination-next',
          property: 'background-color'
        },
        {
          settingId: 'author_kit_archive_pagination_btn_color_scribe',
          selector: '.author-kit-pagination-prev,.author-kit-pagination-next',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_pagination_btn_color_scribe',
          selector: '.author-kit-pagination-container .current .author-kit-pagination-page',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_button_color_scribe',
          selector: '.author-kit-pagination-container .current .author-kit-pagination-page',
          property: 'background-color'
        },
        {
          settingId: 'author_kit_archive_pagination_number_color_scribe',
          selector: '.author-kit-pagination a.page-numbers:not(page-numbers.current) .author-kit-pagination-page',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_pagination_number_bg_color_scribe',
          selector: '.author-kit-pagination a.page-numbers:not(page-numbers.current) .author-kit-pagination-page',
          property: 'background-color'
        }
      ],
      text: [
        {
          settingId: 'author_kit_grid_archive_title_scribe',
          selector: '#author-kit-archive-template .author-kit-posts-title  #post-title'
        }
      ]
    },

    {
      template: 'author-archive-template-quill',
      css: [
        {
          settingId: 'author_kit_background_color_achive_quill',
          selector: '#author-kit-archive-template',
          property: 'background-color'
        },
        {
          settingId: 'author_kit_archive_bio_box_bg_color_quill',
          selector: '.author-kit-bio-container',
          property: 'background-color'
        },
        {
          settingId: 'author_kit_archive_bio_box_width_quill',
          selector: '.author-kit-bio-container',
          property: 'max-width',
          unit: '%'
        },
        {
          settingId: 'author_kit_archive_bio_box_padding_quill',
          selector: '.author-kit-bio-container',
          property: 'padding'
        },
        {
          settingId: 'author_kit_archive_avatar_size_quill',
          selector: '.author-kit-avatar',
          property: 'width',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_avatar_size_quill',
          selector: '.author-kit-avatar',
          property: 'height',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_icon_style_quill',
          selector: 'a.author-kit-social-icon, .author-kit-social-icons-list a',
          property: 'id'
        },
        {
          settingId: 'author_kit_archive_social_icons_size_quill',
          selector: 'a.author-kit-social-icon i',
          property: 'font-size'
        },
        {
          settingId: 'author_kit_archive_name_color_quill',
          selector: '.author-kit-name',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_name_size_quill',
          selector: '.author-kit-name',
          property: 'font-size',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_job_title_color_quill',
          selector: '.author-kit-job-title',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_job_title_size_quill',
          selector: '.author-kit-job-title',
          property: 'font-size',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_description_color_quill',
          selector: '.author-kit-description',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_description_size_quill',
          selector: '.author-kit-description',
          property: 'font-size',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_icon_container_color_quill',
          selector: '.author-kit-social-icons-list  a.author-kit-social-icon',
          property: 'background-color'
        },
        {
          settingId: 'author_kit_archive_social_icon_color_quill',
          selector: '.author-kit-social-icons-list > a > i',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_social_icons_gap_quill',
          selector: '#author-kit-bio-box div.author-kit-social-icons-list',
          property: 'gap',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_grid_gap_quill',
          selector: '.author-kit-posts-grid',
          property: 'gap',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_post_card_width_quill',
          selector: '.author-kit-posts-grid',
          property: 'grid-template-columns'
        },
        {
          settingId: 'author_kit_archive_post_card_bg_color_quill',
          selector: '.author-kit-post-item',
          property: 'background-color'
        },
        {
          settingId: 'author_kit_archive_thumbnail_border_radius_quill',
          selector: '.author-kit-post-item  a  img',
          property: 'border-radius',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_post_title_size_quill',
          selector: 'h4.author-kit-post-title, #author-kit-archive-template h4.author-kit-post-title a',
          property: 'font-size',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_title_color_quill',
          selector: '.author-kit-post-title, h4.author-kit-post-title, .author-kit-post-item  a h4 , #author-kit-archive-template h4',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_excerpt_size_quill',
          selector: '.author-kit-post-item .author-kit-post-excerpt > p',
          property: 'font-size',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_excerpt_color_quill',
          selector: '.author-kit-post-item div.author-kit-post-excerpt',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_post_meta_size_quill',
          selector: '.author-kit-post-meta',
          property: 'font-size',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_post_meta_color_quill',
          selector: '.author-kit-post-meta .author-name , .author-kit-post-meta .post-date ',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_post_meta_separator_color_quill',
          selector: '.author-kit-post-meta',
          property: 'border-color'
        },
        {
          settingId: 'author_kit_archive_grid_text_alignment_quill',
          selector: '.author-kit-posts-title',
          property: 'justify-content'
        },
        {
          settingId: 'author_kit_archive_pagination_alignment_quill',
          selector: '#author-kit-archive-template .author-kit-pagination-container',
          property: 'justify-content'
        },
        {
          settingId: 'author_kit_archive_post_grid_title_size_quill',
          selector: '#author-kit-archive-template .author-kit-posts-title',
          property: 'font-size',
          unit: 'px'
        },
        {
          settingId: 'author_kit_archive_post_grid_title_color_quill',
          selector: '#author-kit-archive-template .author-kit-posts-title h3',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_post_grid_bg_color_quill',
          selector: '#author-kit-archive-template .author-kit-posts, #author-kit-archive-template .author-kit-pagination-container',
          property: 'background-color'
        },
        {
          settingId: 'author_kit_archive_button_color_quill',
          selector: '.author-kit-pagination-prev,.author-kit-pagination-next',
          property: 'background-color'
        },
        {
          settingId: 'author_kit_archive_pagination_btn_color_quill',
          selector: '.author-kit-pagination-prev,.author-kit-pagination-next',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_pagination_btn_color_quill',
          selector: '.author-kit-pagination-container .current .author-kit-pagination-page',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_button_color_quill',
          selector: '.author-kit-pagination-container .current .author-kit-pagination-page',
          property: 'background-color'
        },
        {
          settingId: 'author_kit_archive_pagination_number_color_quill',
          selector: '.author-kit-pagination a.page-numbers:not(page-numbers.current) .author-kit-pagination-page',
          property: 'color'
        },
        {
          settingId: 'author_kit_archive_pagination_number_bg_color_quill',
          selector: '.author-kit-pagination a.page-numbers:not(page-numbers.current) .author-kit-pagination-page',
          property: 'background-color'
        }
      ],
      text: [
        {
          settingId: 'author_kit_grid_archive_title_quill',
          selector: '#author-kit-archive-template .author-kit-posts-title  #post-title'
        }
      ]
    },


  ]; // Define directly

  /* ──────────────────────────────────────────── *
   *  Init when preview is ready
   * ──────────────────────────────────────────── */
  wp.customize.bind('preview-ready', function () {

    if (!settings || !settings.length) { return; }

    for (var s = 0; s < settings.length; s++) {

      var cssArr = settings[s].css || [];
      var textArr = settings[s].text || [];

      for (var c = 0; c < cssArr.length; c++) {
        var cssRule = cssArr[c];
        if (cssRule.selector) {
          livePreview(
            cssRule.settingId,
            cssRule.selector,
            cssRule.property,
            cssRule.unit,
            cssRule.defaultProperty
          );
        }
      }

      for (var t = 0; t < textArr.length; t++) {
        var textRule = textArr[t];
        if (textRule.selector) {
          liveTextPreview(textRule.settingId, textRule.selector);
        }
      }
    }
  });

  /* ──────────────────────────────────────────── *
   *  Kirki device switch listener
   * ──────────────────────────────────────────── */
  $('body').on('click', '[data-kirki-control-device]', function () {
    var device = $(this).attr('data-kirki-control-device') || 'desktop';
    $('body').attr('data-kirki-device', device).trigger('kirki-device-switch');
  });

})(jQuery);
