/**
 * GGM Commerce Icon Picker
 *
 * A modal-based icon picker similar to Elementor's icon control.
 * Supports Font Awesome icons and SVG uploads.
 *
 * @package GGM_Commerce
 */

(function($) {
    'use strict';

    // Font Awesome 5 Free icons organized by category
    var iconLibrary = {
        'Solid': [
            'fas fa-ad', 'fas fa-address-book', 'fas fa-address-card', 'fas fa-adjust', 'fas fa-air-freshener',
            'fas fa-align-center', 'fas fa-align-justify', 'fas fa-align-left', 'fas fa-align-right', 'fas fa-allergies',
            'fas fa-ambulance', 'fas fa-american-sign-language-interpreting', 'fas fa-anchor', 'fas fa-angle-double-down',
            'fas fa-angle-double-left', 'fas fa-angle-double-right', 'fas fa-angle-double-up', 'fas fa-angle-down',
            'fas fa-angle-left', 'fas fa-angle-right', 'fas fa-angle-up', 'fas fa-angry', 'fas fa-ankh',
            'fas fa-apple-alt', 'fas fa-archive', 'fas fa-archway', 'fas fa-arrow-alt-circle-down',
            'fas fa-arrow-alt-circle-left', 'fas fa-arrow-alt-circle-right', 'fas fa-arrow-alt-circle-up',
            'fas fa-arrow-circle-down', 'fas fa-arrow-circle-left', 'fas fa-arrow-circle-right', 'fas fa-arrow-circle-up',
            'fas fa-arrow-down', 'fas fa-arrow-left', 'fas fa-arrow-right', 'fas fa-arrow-up', 'fas fa-arrows-alt',
            'fas fa-arrows-alt-h', 'fas fa-arrows-alt-v', 'fas fa-assistive-listening-systems', 'fas fa-asterisk',
            'fas fa-at', 'fas fa-atlas', 'fas fa-atom', 'fas fa-award', 'fas fa-baby', 'fas fa-baby-carriage',
            'fas fa-backspace', 'fas fa-backward', 'fas fa-bacon', 'fas fa-balance-scale', 'fas fa-balance-scale-left',
            'fas fa-balance-scale-right', 'fas fa-ban', 'fas fa-band-aid', 'fas fa-barcode', 'fas fa-bars',
            'fas fa-baseball-ball', 'fas fa-basketball-ball', 'fas fa-bath', 'fas fa-battery-empty',
            'fas fa-battery-full', 'fas fa-battery-half', 'fas fa-battery-quarter', 'fas fa-battery-three-quarters',
            'fas fa-bed', 'fas fa-beer', 'fas fa-bell', 'fas fa-bell-slash', 'fas fa-bezier-curve', 'fas fa-bible',
            'fas fa-bicycle', 'fas fa-biking', 'fas fa-binoculars', 'fas fa-biohazard', 'fas fa-birthday-cake',
            'fas fa-blender', 'fas fa-blender-phone', 'fas fa-blind', 'fas fa-blog', 'fas fa-bold', 'fas fa-bolt',
            'fas fa-bomb', 'fas fa-bone', 'fas fa-bong', 'fas fa-book', 'fas fa-book-dead', 'fas fa-book-medical',
            'fas fa-book-open', 'fas fa-book-reader', 'fas fa-bookmark', 'fas fa-border-all', 'fas fa-border-none',
            'fas fa-border-style', 'fas fa-bowling-ball', 'fas fa-box', 'fas fa-box-open', 'fas fa-boxes',
            'fas fa-braille', 'fas fa-brain', 'fas fa-bread-slice', 'fas fa-briefcase', 'fas fa-briefcase-medical',
            'fas fa-broadcast-tower', 'fas fa-broom', 'fas fa-brush', 'fas fa-bug', 'fas fa-building',
            'fas fa-bullhorn', 'fas fa-bullseye', 'fas fa-burn', 'fas fa-bus', 'fas fa-bus-alt', 'fas fa-business-time',
            'fas fa-calculator', 'fas fa-calendar', 'fas fa-calendar-alt', 'fas fa-calendar-check',
            'fas fa-calendar-day', 'fas fa-calendar-minus', 'fas fa-calendar-plus', 'fas fa-calendar-times',
            'fas fa-calendar-week', 'fas fa-camera', 'fas fa-camera-retro', 'fas fa-campground', 'fas fa-candy-cane',
            'fas fa-cannabis', 'fas fa-capsules', 'fas fa-car', 'fas fa-car-alt', 'fas fa-car-battery',
            'fas fa-car-crash', 'fas fa-car-side', 'fas fa-caravan', 'fas fa-caret-down', 'fas fa-caret-left',
            'fas fa-caret-right', 'fas fa-caret-square-down', 'fas fa-caret-square-left', 'fas fa-caret-square-right',
            'fas fa-caret-square-up', 'fas fa-caret-up', 'fas fa-carrot', 'fas fa-cart-arrow-down', 'fas fa-cart-plus',
            'fas fa-cash-register', 'fas fa-cat', 'fas fa-certificate', 'fas fa-chair', 'fas fa-chalkboard',
            'fas fa-chalkboard-teacher', 'fas fa-charging-station', 'fas fa-chart-area', 'fas fa-chart-bar',
            'fas fa-chart-line', 'fas fa-chart-pie', 'fas fa-check', 'fas fa-check-circle', 'fas fa-check-double',
            'fas fa-check-square', 'fas fa-cheese', 'fas fa-chess', 'fas fa-chess-bishop', 'fas fa-chess-board',
            'fas fa-chess-king', 'fas fa-chess-knight', 'fas fa-chess-pawn', 'fas fa-chess-queen', 'fas fa-chess-rook',
            'fas fa-chevron-circle-down', 'fas fa-chevron-circle-left', 'fas fa-chevron-circle-right',
            'fas fa-chevron-circle-up', 'fas fa-chevron-down', 'fas fa-chevron-left', 'fas fa-chevron-right',
            'fas fa-chevron-up', 'fas fa-child', 'fas fa-church', 'fas fa-circle', 'fas fa-circle-notch',
            'fas fa-city', 'fas fa-clinic-medical', 'fas fa-clipboard', 'fas fa-clipboard-check',
            'fas fa-clipboard-list', 'fas fa-clock', 'fas fa-clone', 'fas fa-closed-captioning', 'fas fa-cloud',
            'fas fa-cloud-download-alt', 'fas fa-cloud-meatball', 'fas fa-cloud-moon', 'fas fa-cloud-moon-rain',
            'fas fa-cloud-rain', 'fas fa-cloud-showers-heavy', 'fas fa-cloud-sun', 'fas fa-cloud-sun-rain',
            'fas fa-cloud-upload-alt', 'fas fa-cocktail', 'fas fa-code', 'fas fa-code-branch', 'fas fa-coffee',
            'fas fa-cog', 'fas fa-cogs', 'fas fa-coins', 'fas fa-columns', 'fas fa-comment', 'fas fa-comment-alt',
            'fas fa-comment-dollar', 'fas fa-comment-dots', 'fas fa-comment-medical', 'fas fa-comment-slash',
            'fas fa-comments', 'fas fa-comments-dollar', 'fas fa-compact-disc', 'fas fa-compass', 'fas fa-compress',
            'fas fa-compress-alt', 'fas fa-compress-arrows-alt', 'fas fa-concierge-bell', 'fas fa-cookie',
            'fas fa-cookie-bite', 'fas fa-copy', 'fas fa-copyright', 'fas fa-couch', 'fas fa-credit-card',
            'fas fa-crop', 'fas fa-crop-alt', 'fas fa-cross', 'fas fa-crosshairs', 'fas fa-crow', 'fas fa-crown',
            'fas fa-crutch', 'fas fa-cube', 'fas fa-cubes', 'fas fa-cut', 'fas fa-database', 'fas fa-deaf',
            'fas fa-democrat', 'fas fa-desktop', 'fas fa-dharmachakra', 'fas fa-diagnoses', 'fas fa-dice',
            'fas fa-dice-d20', 'fas fa-dice-d6', 'fas fa-dice-five', 'fas fa-dice-four', 'fas fa-dice-one',
            'fas fa-dice-six', 'fas fa-dice-three', 'fas fa-dice-two', 'fas fa-digital-tachograph', 'fas fa-directions',
            'fas fa-divide', 'fas fa-dizzy', 'fas fa-dna', 'fas fa-dog', 'fas fa-dollar-sign', 'fas fa-dolly',
            'fas fa-dolly-flatbed', 'fas fa-donate', 'fas fa-door-closed', 'fas fa-door-open', 'fas fa-dot-circle',
            'fas fa-dove', 'fas fa-download', 'fas fa-drafting-compass', 'fas fa-dragon', 'fas fa-draw-polygon',
            'fas fa-drum', 'fas fa-drum-steelpan', 'fas fa-drumstick-bite', 'fas fa-dumbbell', 'fas fa-dumpster',
            'fas fa-dumpster-fire', 'fas fa-dungeon', 'fas fa-edit', 'fas fa-egg', 'fas fa-eject', 'fas fa-ellipsis-h',
            'fas fa-ellipsis-v', 'fas fa-envelope', 'fas fa-envelope-open', 'fas fa-envelope-open-text',
            'fas fa-envelope-square', 'fas fa-equals', 'fas fa-eraser', 'fas fa-ethernet', 'fas fa-euro-sign',
            'fas fa-exchange-alt', 'fas fa-exclamation', 'fas fa-exclamation-circle', 'fas fa-exclamation-triangle',
            'fas fa-expand', 'fas fa-expand-alt', 'fas fa-expand-arrows-alt', 'fas fa-external-link-alt',
            'fas fa-external-link-square-alt', 'fas fa-eye', 'fas fa-eye-dropper', 'fas fa-eye-slash',
            'fas fa-fan', 'fas fa-fast-backward', 'fas fa-fast-forward', 'fas fa-fax', 'fas fa-feather',
            'fas fa-feather-alt', 'fas fa-female', 'fas fa-fighter-jet', 'fas fa-file', 'fas fa-file-alt',
            'fas fa-file-archive', 'fas fa-file-audio', 'fas fa-file-code', 'fas fa-file-contract', 'fas fa-file-csv',
            'fas fa-file-download', 'fas fa-file-excel', 'fas fa-file-export', 'fas fa-file-image',
            'fas fa-file-import', 'fas fa-file-invoice', 'fas fa-file-invoice-dollar', 'fas fa-file-medical',
            'fas fa-file-medical-alt', 'fas fa-file-pdf', 'fas fa-file-powerpoint', 'fas fa-file-prescription',
            'fas fa-file-signature', 'fas fa-file-upload', 'fas fa-file-video', 'fas fa-file-word', 'fas fa-fill',
            'fas fa-fill-drip', 'fas fa-film', 'fas fa-filter', 'fas fa-fingerprint', 'fas fa-fire', 'fas fa-fire-alt',
            'fas fa-fire-extinguisher', 'fas fa-first-aid', 'fas fa-fish', 'fas fa-fist-raised', 'fas fa-flag',
            'fas fa-flag-checkered', 'fas fa-flag-usa', 'fas fa-flask', 'fas fa-flushed', 'fas fa-folder',
            'fas fa-folder-minus', 'fas fa-folder-open', 'fas fa-folder-plus', 'fas fa-font', 'fas fa-football-ball',
            'fas fa-forward', 'fas fa-frog', 'fas fa-frown', 'fas fa-frown-open', 'fas fa-funnel-dollar',
            'fas fa-futbol', 'fas fa-gamepad', 'fas fa-gas-pump', 'fas fa-gavel', 'fas fa-gem', 'fas fa-genderless',
            'fas fa-ghost', 'fas fa-gift', 'fas fa-gifts', 'fas fa-glass-cheers', 'fas fa-glass-martini',
            'fas fa-glass-martini-alt', 'fas fa-glass-whiskey', 'fas fa-glasses', 'fas fa-globe', 'fas fa-globe-africa',
            'fas fa-globe-americas', 'fas fa-globe-asia', 'fas fa-globe-europe', 'fas fa-golf-ball', 'fas fa-gopuram',
            'fas fa-graduation-cap', 'fas fa-greater-than', 'fas fa-greater-than-equal', 'fas fa-grimace',
            'fas fa-grin', 'fas fa-grin-alt', 'fas fa-grin-beam', 'fas fa-grin-beam-sweat', 'fas fa-grin-hearts',
            'fas fa-grin-squint', 'fas fa-grin-squint-tears', 'fas fa-grin-stars', 'fas fa-grin-tears',
            'fas fa-grin-tongue', 'fas fa-grin-tongue-squint', 'fas fa-grin-tongue-wink', 'fas fa-grin-wink',
            'fas fa-grip-horizontal', 'fas fa-grip-lines', 'fas fa-grip-lines-vertical', 'fas fa-grip-vertical',
            'fas fa-guitar', 'fas fa-h-square', 'fas fa-hamburger', 'fas fa-hammer', 'fas fa-hamsa', 'fas fa-hand-holding',
            'fas fa-hand-holding-heart', 'fas fa-hand-holding-usd', 'fas fa-hand-lizard', 'fas fa-hand-middle-finger',
            'fas fa-hand-paper', 'fas fa-hand-peace', 'fas fa-hand-point-down', 'fas fa-hand-point-left',
            'fas fa-hand-point-right', 'fas fa-hand-point-up', 'fas fa-hand-pointer', 'fas fa-hand-rock',
            'fas fa-hand-scissors', 'fas fa-hand-spock', 'fas fa-hands', 'fas fa-hands-helping', 'fas fa-handshake',
            'fas fa-hanukiah', 'fas fa-hard-hat', 'fas fa-hashtag', 'fas fa-hat-cowboy', 'fas fa-hat-cowboy-side',
            'fas fa-hat-wizard', 'fas fa-hdd', 'fas fa-heading', 'fas fa-headphones', 'fas fa-headphones-alt',
            'fas fa-headset', 'fas fa-heart', 'fas fa-heart-broken', 'fas fa-heartbeat', 'fas fa-helicopter',
            'fas fa-highlighter', 'fas fa-hiking', 'fas fa-hippo', 'fas fa-history', 'fas fa-hockey-puck',
            'fas fa-holly-berry', 'fas fa-home', 'fas fa-horse', 'fas fa-horse-head', 'fas fa-hospital',
            'fas fa-hospital-alt', 'fas fa-hospital-symbol', 'fas fa-hot-tub', 'fas fa-hotdog', 'fas fa-hotel',
            'fas fa-hourglass', 'fas fa-hourglass-end', 'fas fa-hourglass-half', 'fas fa-hourglass-start',
            'fas fa-house-damage', 'fas fa-hryvnia', 'fas fa-i-cursor', 'fas fa-ice-cream', 'fas fa-icicles',
            'fas fa-icons', 'fas fa-id-badge', 'fas fa-id-card', 'fas fa-id-card-alt', 'fas fa-igloo', 'fas fa-image',
            'fas fa-images', 'fas fa-inbox', 'fas fa-indent', 'fas fa-industry', 'fas fa-infinity', 'fas fa-info',
            'fas fa-info-circle', 'fas fa-italic', 'fas fa-jedi', 'fas fa-joint', 'fas fa-journal-whills',
            'fas fa-kaaba', 'fas fa-key', 'fas fa-keyboard', 'fas fa-khanda', 'fas fa-kiss', 'fas fa-kiss-beam',
            'fas fa-kiss-wink-heart', 'fas fa-kiwi-bird', 'fas fa-landmark', 'fas fa-language', 'fas fa-laptop',
            'fas fa-laptop-code', 'fas fa-laptop-medical', 'fas fa-laugh', 'fas fa-laugh-beam', 'fas fa-laugh-squint',
            'fas fa-laugh-wink', 'fas fa-layer-group', 'fas fa-leaf', 'fas fa-lemon', 'fas fa-less-than',
            'fas fa-less-than-equal', 'fas fa-level-down-alt', 'fas fa-level-up-alt', 'fas fa-life-ring',
            'fas fa-lightbulb', 'fas fa-link', 'fas fa-lira-sign', 'fas fa-list', 'fas fa-list-alt', 'fas fa-list-ol',
            'fas fa-list-ul', 'fas fa-location-arrow', 'fas fa-lock', 'fas fa-lock-open', 'fas fa-long-arrow-alt-down',
            'fas fa-long-arrow-alt-left', 'fas fa-long-arrow-alt-right', 'fas fa-long-arrow-alt-up',
            'fas fa-low-vision', 'fas fa-luggage-cart', 'fas fa-magic', 'fas fa-magnet', 'fas fa-mail-bulk',
            'fas fa-male', 'fas fa-map', 'fas fa-map-marked', 'fas fa-map-marked-alt', 'fas fa-map-marker',
            'fas fa-map-marker-alt', 'fas fa-map-pin', 'fas fa-map-signs', 'fas fa-marker', 'fas fa-mars',
            'fas fa-mars-double', 'fas fa-mars-stroke', 'fas fa-mars-stroke-h', 'fas fa-mars-stroke-v', 'fas fa-mask',
            'fas fa-medal', 'fas fa-medkit', 'fas fa-meh', 'fas fa-meh-blank', 'fas fa-meh-rolling-eyes',
            'fas fa-memory', 'fas fa-menorah', 'fas fa-mercury', 'fas fa-meteor', 'fas fa-microchip',
            'fas fa-microphone', 'fas fa-microphone-alt', 'fas fa-microphone-alt-slash', 'fas fa-microphone-slash',
            'fas fa-microscope', 'fas fa-minus', 'fas fa-minus-circle', 'fas fa-minus-square', 'fas fa-mitten',
            'fas fa-mobile', 'fas fa-mobile-alt', 'fas fa-money-bill', 'fas fa-money-bill-alt', 'fas fa-money-bill-wave',
            'fas fa-money-bill-wave-alt', 'fas fa-money-check', 'fas fa-money-check-alt', 'fas fa-monument',
            'fas fa-moon', 'fas fa-mortar-pestle', 'fas fa-mosque', 'fas fa-motorcycle', 'fas fa-mountain',
            'fas fa-mouse', 'fas fa-mouse-pointer', 'fas fa-mug-hot', 'fas fa-music', 'fas fa-network-wired',
            'fas fa-neuter', 'fas fa-newspaper', 'fas fa-not-equal', 'fas fa-notes-medical', 'fas fa-object-group',
            'fas fa-object-ungroup', 'fas fa-oil-can', 'fas fa-om', 'fas fa-otter', 'fas fa-outdent', 'fas fa-pager',
            'fas fa-paint-brush', 'fas fa-paint-roller', 'fas fa-palette', 'fas fa-pallet', 'fas fa-paper-plane',
            'fas fa-paperclip', 'fas fa-parachute-box', 'fas fa-paragraph', 'fas fa-parking', 'fas fa-passport',
            'fas fa-pastafarianism', 'fas fa-paste', 'fas fa-pause', 'fas fa-pause-circle', 'fas fa-paw', 'fas fa-peace',
            'fas fa-pen', 'fas fa-pen-alt', 'fas fa-pen-fancy', 'fas fa-pen-nib', 'fas fa-pen-square', 'fas fa-pencil-alt',
            'fas fa-pencil-ruler', 'fas fa-people-carry', 'fas fa-pepper-hot', 'fas fa-percent', 'fas fa-percentage',
            'fas fa-person-booth', 'fas fa-phone', 'fas fa-phone-alt', 'fas fa-phone-slash', 'fas fa-phone-square',
            'fas fa-phone-square-alt', 'fas fa-phone-volume', 'fas fa-photo-video', 'fas fa-piggy-bank', 'fas fa-pills',
            'fas fa-pizza-slice', 'fas fa-place-of-worship', 'fas fa-plane', 'fas fa-plane-arrival',
            'fas fa-plane-departure', 'fas fa-play', 'fas fa-play-circle', 'fas fa-plug', 'fas fa-plus',
            'fas fa-plus-circle', 'fas fa-plus-square', 'fas fa-podcast', 'fas fa-poll', 'fas fa-poll-h', 'fas fa-poo',
            'fas fa-poo-storm', 'fas fa-poop', 'fas fa-portrait', 'fas fa-pound-sign', 'fas fa-power-off',
            'fas fa-pray', 'fas fa-praying-hands', 'fas fa-prescription', 'fas fa-prescription-bottle',
            'fas fa-prescription-bottle-alt', 'fas fa-print', 'fas fa-procedures', 'fas fa-project-diagram',
            'fas fa-puzzle-piece', 'fas fa-qrcode', 'fas fa-question', 'fas fa-question-circle', 'fas fa-quidditch',
            'fas fa-quote-left', 'fas fa-quote-right', 'fas fa-quran', 'fas fa-radiation', 'fas fa-radiation-alt',
            'fas fa-rainbow', 'fas fa-random', 'fas fa-receipt', 'fas fa-record-vinyl', 'fas fa-recycle', 'fas fa-redo',
            'fas fa-redo-alt', 'fas fa-registered', 'fas fa-remove-format', 'fas fa-reply', 'fas fa-reply-all',
            'fas fa-republican', 'fas fa-restroom', 'fas fa-retweet', 'fas fa-ribbon', 'fas fa-ring', 'fas fa-road',
            'fas fa-robot', 'fas fa-rocket', 'fas fa-route', 'fas fa-rss', 'fas fa-rss-square', 'fas fa-ruble-sign',
            'fas fa-ruler', 'fas fa-ruler-combined', 'fas fa-ruler-horizontal', 'fas fa-ruler-vertical', 'fas fa-running',
            'fas fa-rupee-sign', 'fas fa-sad-cry', 'fas fa-sad-tear', 'fas fa-satellite', 'fas fa-satellite-dish',
            'fas fa-save', 'fas fa-school', 'fas fa-screwdriver', 'fas fa-scroll', 'fas fa-sd-card', 'fas fa-search',
            'fas fa-search-dollar', 'fas fa-search-location', 'fas fa-search-minus', 'fas fa-search-plus',
            'fas fa-seedling', 'fas fa-server', 'fas fa-shapes', 'fas fa-share', 'fas fa-share-alt',
            'fas fa-share-alt-square', 'fas fa-share-square', 'fas fa-shekel-sign', 'fas fa-shield-alt',
            'fas fa-ship', 'fas fa-shipping-fast', 'fas fa-shoe-prints', 'fas fa-shopping-bag', 'fas fa-shopping-basket',
            'fas fa-shopping-cart', 'fas fa-shower', 'fas fa-shuttle-van', 'fas fa-sign', 'fas fa-sign-in-alt',
            'fas fa-sign-language', 'fas fa-sign-out-alt', 'fas fa-signal', 'fas fa-signature', 'fas fa-sim-card',
            'fas fa-sitemap', 'fas fa-skating', 'fas fa-skiing', 'fas fa-skiing-nordic', 'fas fa-skull',
            'fas fa-skull-crossbones', 'fas fa-slash', 'fas fa-sleigh', 'fas fa-sliders-h', 'fas fa-smile',
            'fas fa-smile-beam', 'fas fa-smile-wink', 'fas fa-smog', 'fas fa-smoking', 'fas fa-smoking-ban',
            'fas fa-sms', 'fas fa-snowboarding', 'fas fa-snowflake', 'fas fa-snowman', 'fas fa-snowplow',
            'fas fa-socks', 'fas fa-solar-panel', 'fas fa-sort', 'fas fa-sort-alpha-down', 'fas fa-sort-alpha-down-alt',
            'fas fa-sort-alpha-up', 'fas fa-sort-alpha-up-alt', 'fas fa-sort-amount-down', 'fas fa-sort-amount-down-alt',
            'fas fa-sort-amount-up', 'fas fa-sort-amount-up-alt', 'fas fa-sort-down', 'fas fa-sort-numeric-down',
            'fas fa-sort-numeric-down-alt', 'fas fa-sort-numeric-up', 'fas fa-sort-numeric-up-alt', 'fas fa-sort-up',
            'fas fa-spa', 'fas fa-space-shuttle', 'fas fa-spell-check', 'fas fa-spider', 'fas fa-spinner',
            'fas fa-splotch', 'fas fa-spray-can', 'fas fa-square', 'fas fa-square-full', 'fas fa-square-root-alt',
            'fas fa-stamp', 'fas fa-star', 'fas fa-star-and-crescent', 'fas fa-star-half', 'fas fa-star-half-alt',
            'fas fa-star-of-david', 'fas fa-star-of-life', 'fas fa-step-backward', 'fas fa-step-forward',
            'fas fa-stethoscope', 'fas fa-sticky-note', 'fas fa-stop', 'fas fa-stop-circle', 'fas fa-stopwatch',
            'fas fa-store', 'fas fa-store-alt', 'fas fa-stream', 'fas fa-street-view', 'fas fa-strikethrough',
            'fas fa-stroopwafel', 'fas fa-subscript', 'fas fa-subway', 'fas fa-suitcase', 'fas fa-suitcase-rolling',
            'fas fa-sun', 'fas fa-superscript', 'fas fa-surprise', 'fas fa-swatchbook', 'fas fa-swimmer',
            'fas fa-swimming-pool', 'fas fa-synagogue', 'fas fa-sync', 'fas fa-sync-alt', 'fas fa-syringe',
            'fas fa-table', 'fas fa-table-tennis', 'fas fa-tablet', 'fas fa-tablet-alt', 'fas fa-tablets',
            'fas fa-tachometer-alt', 'fas fa-tag', 'fas fa-tags', 'fas fa-tape', 'fas fa-tasks', 'fas fa-taxi',
            'fas fa-teeth', 'fas fa-teeth-open', 'fas fa-temperature-high', 'fas fa-temperature-low', 'fas fa-tenge',
            'fas fa-terminal', 'fas fa-text-height', 'fas fa-text-width', 'fas fa-th', 'fas fa-th-large', 'fas fa-th-list',
            'fas fa-theater-masks', 'fas fa-thermometer', 'fas fa-thermometer-empty', 'fas fa-thermometer-full',
            'fas fa-thermometer-half', 'fas fa-thermometer-quarter', 'fas fa-thermometer-three-quarters',
            'fas fa-thumbs-down', 'fas fa-thumbs-up', 'fas fa-thumbtack', 'fas fa-ticket-alt', 'fas fa-times',
            'fas fa-times-circle', 'fas fa-tint', 'fas fa-tint-slash', 'fas fa-tired', 'fas fa-toggle-off',
            'fas fa-toggle-on', 'fas fa-toilet', 'fas fa-toilet-paper', 'fas fa-toolbox', 'fas fa-tools', 'fas fa-tooth',
            'fas fa-torah', 'fas fa-torii-gate', 'fas fa-tractor', 'fas fa-trademark', 'fas fa-traffic-light',
            'fas fa-trailer', 'fas fa-train', 'fas fa-tram', 'fas fa-transgender', 'fas fa-transgender-alt',
            'fas fa-trash', 'fas fa-trash-alt', 'fas fa-trash-restore', 'fas fa-trash-restore-alt', 'fas fa-tree',
            'fas fa-trophy', 'fas fa-truck', 'fas fa-truck-loading', 'fas fa-truck-monster', 'fas fa-truck-moving',
            'fas fa-truck-pickup', 'fas fa-tshirt', 'fas fa-tty', 'fas fa-tv', 'fas fa-umbrella', 'fas fa-umbrella-beach',
            'fas fa-underline', 'fas fa-undo', 'fas fa-undo-alt', 'fas fa-universal-access', 'fas fa-university',
            'fas fa-unlink', 'fas fa-unlock', 'fas fa-unlock-alt', 'fas fa-upload', 'fas fa-user', 'fas fa-user-alt',
            'fas fa-user-alt-slash', 'fas fa-user-astronaut', 'fas fa-user-check', 'fas fa-user-circle',
            'fas fa-user-clock', 'fas fa-user-cog', 'fas fa-user-edit', 'fas fa-user-friends', 'fas fa-user-graduate',
            'fas fa-user-injured', 'fas fa-user-lock', 'fas fa-user-md', 'fas fa-user-minus', 'fas fa-user-ninja',
            'fas fa-user-nurse', 'fas fa-user-plus', 'fas fa-user-secret', 'fas fa-user-shield', 'fas fa-user-slash',
            'fas fa-user-tag', 'fas fa-user-tie', 'fas fa-user-times', 'fas fa-users', 'fas fa-users-cog',
            'fas fa-utensil-spoon', 'fas fa-utensils', 'fas fa-vector-square', 'fas fa-venus', 'fas fa-venus-double',
            'fas fa-venus-mars', 'fas fa-vial', 'fas fa-vials', 'fas fa-video', 'fas fa-video-slash', 'fas fa-vihara',
            'fas fa-voicemail', 'fas fa-volleyball-ball', 'fas fa-volume-down', 'fas fa-volume-mute', 'fas fa-volume-off',
            'fas fa-volume-up', 'fas fa-vote-yea', 'fas fa-vr-cardboard', 'fas fa-walking', 'fas fa-wallet', 'fas fa-warehouse',
            'fas fa-water', 'fas fa-wave-square', 'fas fa-weight', 'fas fa-weight-hanging', 'fas fa-wheelchair',
            'fas fa-wifi', 'fas fa-wind', 'fas fa-window-close', 'fas fa-window-maximize', 'fas fa-window-minimize',
            'fas fa-window-restore', 'fas fa-wine-bottle', 'fas fa-wine-glass', 'fas fa-wine-glass-alt', 'fas fa-won-sign',
            'fas fa-wrench', 'fas fa-x-ray', 'fas fa-yen-sign', 'fas fa-yin-yang'
        ],
        'Regular': [
            'far fa-address-book', 'far fa-address-card', 'far fa-angry', 'far fa-arrow-alt-circle-down',
            'far fa-arrow-alt-circle-left', 'far fa-arrow-alt-circle-right', 'far fa-arrow-alt-circle-up',
            'far fa-bell', 'far fa-bell-slash', 'far fa-bookmark', 'far fa-building', 'far fa-calendar',
            'far fa-calendar-alt', 'far fa-calendar-check', 'far fa-calendar-minus', 'far fa-calendar-plus',
            'far fa-calendar-times', 'far fa-caret-square-down', 'far fa-caret-square-left', 'far fa-caret-square-right',
            'far fa-caret-square-up', 'far fa-chart-bar', 'far fa-check-circle', 'far fa-check-square', 'far fa-circle',
            'far fa-clipboard', 'far fa-clock', 'far fa-clone', 'far fa-closed-captioning', 'far fa-comment',
            'far fa-comment-alt', 'far fa-comment-dots', 'far fa-comments', 'far fa-compass', 'far fa-copy',
            'far fa-copyright', 'far fa-credit-card', 'far fa-dizzy', 'far fa-dot-circle', 'far fa-edit',
            'far fa-envelope', 'far fa-envelope-open', 'far fa-eye', 'far fa-eye-slash', 'far fa-file',
            'far fa-file-alt', 'far fa-file-archive', 'far fa-file-audio', 'far fa-file-code', 'far fa-file-excel',
            'far fa-file-image', 'far fa-file-pdf', 'far fa-file-powerpoint', 'far fa-file-video', 'far fa-file-word',
            'far fa-flag', 'far fa-flushed', 'far fa-folder', 'far fa-folder-open', 'far fa-font-awesome-logo-full',
            'far fa-frown', 'far fa-frown-open', 'far fa-futbol', 'far fa-gem', 'far fa-grimace', 'far fa-grin',
            'far fa-grin-alt', 'far fa-grin-beam', 'far fa-grin-beam-sweat', 'far fa-grin-hearts', 'far fa-grin-squint',
            'far fa-grin-squint-tears', 'far fa-grin-stars', 'far fa-grin-tears', 'far fa-grin-tongue',
            'far fa-grin-tongue-squint', 'far fa-grin-tongue-wink', 'far fa-grin-wink', 'far fa-hand-lizard',
            'far fa-hand-paper', 'far fa-hand-peace', 'far fa-hand-point-down', 'far fa-hand-point-left',
            'far fa-hand-point-right', 'far fa-hand-point-up', 'far fa-hand-pointer', 'far fa-hand-rock',
            'far fa-hand-scissors', 'far fa-hand-spock', 'far fa-handshake', 'far fa-hdd', 'far fa-heart',
            'far fa-hospital', 'far fa-hourglass', 'far fa-id-badge', 'far fa-id-card', 'far fa-image',
            'far fa-images', 'far fa-keyboard', 'far fa-kiss', 'far fa-kiss-beam', 'far fa-kiss-wink-heart',
            'far fa-laugh', 'far fa-laugh-beam', 'far fa-laugh-squint', 'far fa-laugh-wink', 'far fa-lemon',
            'far fa-life-ring', 'far fa-lightbulb', 'far fa-list-alt', 'far fa-map', 'far fa-meh', 'far fa-meh-blank',
            'far fa-meh-rolling-eyes', 'far fa-minus-square', 'far fa-money-bill-alt', 'far fa-moon',
            'far fa-newspaper', 'far fa-object-group', 'far fa-object-ungroup', 'far fa-paper-plane',
            'far fa-pause-circle', 'far fa-play-circle', 'far fa-plus-square', 'far fa-question-circle',
            'far fa-registered', 'far fa-sad-cry', 'far fa-sad-tear', 'far fa-save', 'far fa-share-square',
            'far fa-smile', 'far fa-smile-beam', 'far fa-smile-wink', 'far fa-snowflake', 'far fa-square',
            'far fa-star', 'far fa-star-half', 'far fa-sticky-note', 'far fa-stop-circle', 'far fa-sun',
            'far fa-surprise', 'far fa-thumbs-down', 'far fa-thumbs-up', 'far fa-times-circle', 'far fa-tired',
            'far fa-trash-alt', 'far fa-user', 'far fa-user-circle', 'far fa-window-close', 'far fa-window-maximize',
            'far fa-window-minimize', 'far fa-window-restore'
        ]
    };

    // Common/popular icons for quick access
    var popularIcons = [
        'fas fa-star', 'fas fa-heart', 'fas fa-check', 'fas fa-times', 'fas fa-cog', 'fas fa-home',
        'fas fa-user', 'fas fa-search', 'fas fa-envelope', 'fas fa-phone', 'fas fa-map-marker-alt',
        'fas fa-shopping-cart', 'fas fa-tag', 'fas fa-tags', 'fas fa-box', 'fas fa-truck',
        'fas fa-weight-hanging', 'fas fa-ruler', 'fas fa-ruler-combined', 'fas fa-ruler-vertical',
        'fas fa-ruler-horizontal', 'fas fa-palette', 'fas fa-layer-group', 'fas fa-cubes',
        'fas fa-industry', 'fas fa-certificate', 'fas fa-award', 'fas fa-medal',
        'fas fa-bolt', 'fas fa-fire', 'fas fa-thermometer-half', 'fas fa-tint',
        'fas fa-sun', 'fas fa-moon', 'fas fa-cloud', 'fas fa-leaf',
        'fas fa-recycle', 'fas fa-shield-alt', 'fas fa-lock', 'fas fa-key',
        'fas fa-wrench', 'fas fa-tools', 'fas fa-hammer', 'fas fa-screwdriver',
        'fas fa-microchip', 'fas fa-memory', 'fas fa-hdd', 'fas fa-database',
        'fas fa-battery-full', 'fas fa-plug', 'fas fa-lightbulb', 'fas fa-power-off',
        'fas fa-clock', 'fas fa-calendar-alt', 'fas fa-stopwatch', 'fas fa-hourglass-half',
        'fas fa-chart-line', 'fas fa-chart-bar', 'fas fa-chart-pie', 'fas fa-percentage',
        'fas fa-dollar-sign', 'fas fa-euro-sign', 'fas fa-pound-sign', 'fas fa-coins',
        'fas fa-barcode', 'fas fa-qrcode', 'fas fa-fingerprint', 'fas fa-eye'
    ];

    // Preset attribute groups for common industries
    var presetGroups = {
        'real-estate': {
            name: 'Real Estate / Property',
            icon: 'fas fa-home',
            attributes: [
                { name: 'Bedrooms', icon: 'fas fa-bed', unit: '' },
                { name: 'Bathrooms', icon: 'fas fa-bath', unit: '' },
                { name: 'Reception Rooms', icon: 'fas fa-couch', unit: '' },
                { name: 'Floor Area', icon: 'fas fa-ruler-combined', unit: 'sq ft' },
                { name: 'Land Area', icon: 'fas fa-tree', unit: 'acres' },
                { name: 'Year Built', icon: 'fas fa-calendar-alt', unit: '' },
                { name: 'Parking Spaces', icon: 'fas fa-car', unit: '' },
                { name: 'Energy Rating', icon: 'fas fa-leaf', unit: '' },
                { name: 'Property Type', icon: 'fas fa-building', unit: '' },
                { name: 'Heating Type', icon: 'fas fa-fire', unit: '' }
            ]
        },
        'vehicles': {
            name: 'Vehicles / Automotive',
            icon: 'fas fa-car',
            attributes: [
                { name: 'Mileage', icon: 'fas fa-tachometer-alt', unit: 'miles' },
                { name: 'Engine Size', icon: 'fas fa-cog', unit: 'cc' },
                { name: 'Fuel Type', icon: 'fas fa-gas-pump', unit: '' },
                { name: 'Transmission', icon: 'fas fa-cogs', unit: '' },
                { name: 'Year', icon: 'fas fa-calendar-alt', unit: '' },
                { name: 'Doors', icon: 'fas fa-door-open', unit: '' },
                { name: 'Seats', icon: 'fas fa-chair', unit: '' },
                { name: 'Color', icon: 'fas fa-palette', unit: '' },
                { name: 'Body Type', icon: 'fas fa-car-side', unit: '' },
                { name: 'Horsepower', icon: 'fas fa-horse-head', unit: 'HP' },
                { name: 'CO2 Emissions', icon: 'fas fa-cloud', unit: 'g/km' },
                { name: 'MPG', icon: 'fas fa-leaf', unit: 'mpg' }
            ]
        },
        'electronics': {
            name: 'Electronics / Appliances',
            icon: 'fas fa-plug',
            attributes: [
                { name: 'Brand', icon: 'fas fa-tag', unit: '' },
                { name: 'Model', icon: 'fas fa-barcode', unit: '' },
                { name: 'Power', icon: 'fas fa-bolt', unit: 'W' },
                { name: 'Voltage', icon: 'fas fa-plug', unit: 'V' },
                { name: 'Energy Rating', icon: 'fas fa-leaf', unit: '' },
                { name: 'Dimensions', icon: 'fas fa-ruler-combined', unit: '' },
                { name: 'Weight', icon: 'fas fa-weight-hanging', unit: 'kg' },
                { name: 'Capacity', icon: 'fas fa-box', unit: 'L' },
                { name: 'Color', icon: 'fas fa-palette', unit: '' },
                { name: 'Warranty', icon: 'fas fa-shield-alt', unit: 'years' }
            ]
        },
        'clothing': {
            name: 'Clothing / Fashion',
            icon: 'fas fa-tshirt',
            attributes: [
                { name: 'Size', icon: 'fas fa-ruler', unit: '' },
                { name: 'Color', icon: 'fas fa-palette', unit: '' },
                { name: 'Material', icon: 'fas fa-layer-group', unit: '' },
                { name: 'Brand', icon: 'fas fa-tag', unit: '' },
                { name: 'Gender', icon: 'fas fa-venus-mars', unit: '' },
                { name: 'Style', icon: 'fas fa-star', unit: '' },
                { name: 'Fit', icon: 'fas fa-compress-arrows-alt', unit: '' },
                { name: 'Care Instructions', icon: 'fas fa-hand-sparkles', unit: '' }
            ]
        },
        'furniture': {
            name: 'Furniture / Home',
            icon: 'fas fa-couch',
            attributes: [
                { name: 'Material', icon: 'fas fa-layer-group', unit: '' },
                { name: 'Color', icon: 'fas fa-palette', unit: '' },
                { name: 'Width', icon: 'fas fa-arrows-alt-h', unit: 'cm' },
                { name: 'Height', icon: 'fas fa-arrows-alt-v', unit: 'cm' },
                { name: 'Depth', icon: 'fas fa-ruler', unit: 'cm' },
                { name: 'Weight', icon: 'fas fa-weight-hanging', unit: 'kg' },
                { name: 'Assembly Required', icon: 'fas fa-tools', unit: '' },
                { name: 'Seating Capacity', icon: 'fas fa-users', unit: '' },
                { name: 'Style', icon: 'fas fa-star', unit: '' }
            ]
        },
        'computers': {
            name: 'Computers / Tech',
            icon: 'fas fa-laptop',
            attributes: [
                { name: 'Processor', icon: 'fas fa-microchip', unit: '' },
                { name: 'RAM', icon: 'fas fa-memory', unit: 'GB' },
                { name: 'Storage', icon: 'fas fa-hdd', unit: 'GB' },
                { name: 'Screen Size', icon: 'fas fa-desktop', unit: 'inches' },
                { name: 'Resolution', icon: 'fas fa-th', unit: '' },
                { name: 'Graphics Card', icon: 'fas fa-gamepad', unit: '' },
                { name: 'Operating System', icon: 'fas fa-laptop-code', unit: '' },
                { name: 'Battery Life', icon: 'fas fa-battery-full', unit: 'hours' },
                { name: 'Weight', icon: 'fas fa-weight-hanging', unit: 'kg' },
                { name: 'Ports', icon: 'fas fa-usb', unit: '' }
            ]
        },
        'food-beverage': {
            name: 'Food & Beverage',
            icon: 'fas fa-utensils',
            attributes: [
                { name: 'Weight', icon: 'fas fa-weight-hanging', unit: 'g' },
                { name: 'Volume', icon: 'fas fa-flask', unit: 'ml' },
                { name: 'Calories', icon: 'fas fa-fire', unit: 'kcal' },
                { name: 'Servings', icon: 'fas fa-utensil-spoon', unit: '' },
                { name: 'Allergens', icon: 'fas fa-exclamation-triangle', unit: '' },
                { name: 'Ingredients', icon: 'fas fa-list', unit: '' },
                { name: 'Origin', icon: 'fas fa-globe', unit: '' },
                { name: 'Shelf Life', icon: 'fas fa-calendar-alt', unit: 'days' },
                { name: 'Storage', icon: 'fas fa-snowflake', unit: '' }
            ]
        },
        'jewelry': {
            name: 'Jewelry / Watches',
            icon: 'fas fa-gem',
            attributes: [
                { name: 'Material', icon: 'fas fa-layer-group', unit: '' },
                { name: 'Gemstone', icon: 'fas fa-gem', unit: '' },
                { name: 'Carat', icon: 'fas fa-balance-scale', unit: 'ct' },
                { name: 'Ring Size', icon: 'fas fa-ring', unit: '' },
                { name: 'Chain Length', icon: 'fas fa-link', unit: 'inches' },
                { name: 'Weight', icon: 'fas fa-weight-hanging', unit: 'g' },
                { name: 'Certification', icon: 'fas fa-certificate', unit: '' },
                { name: 'Water Resistance', icon: 'fas fa-tint', unit: 'm' }
            ]
        },
        'sports': {
            name: 'Sports / Fitness',
            icon: 'fas fa-running',
            attributes: [
                { name: 'Size', icon: 'fas fa-ruler', unit: '' },
                { name: 'Weight', icon: 'fas fa-weight-hanging', unit: 'kg' },
                { name: 'Material', icon: 'fas fa-layer-group', unit: '' },
                { name: 'Color', icon: 'fas fa-palette', unit: '' },
                { name: 'Sport Type', icon: 'fas fa-football-ball', unit: '' },
                { name: 'Skill Level', icon: 'fas fa-chart-line', unit: '' },
                { name: 'Age Group', icon: 'fas fa-users', unit: '' },
                { name: 'Brand', icon: 'fas fa-tag', unit: '' }
            ]
        },
        'books': {
            name: 'Books / Media',
            icon: 'fas fa-book',
            attributes: [
                { name: 'Author', icon: 'fas fa-user', unit: '' },
                { name: 'Publisher', icon: 'fas fa-building', unit: '' },
                { name: 'ISBN', icon: 'fas fa-barcode', unit: '' },
                { name: 'Pages', icon: 'fas fa-file-alt', unit: '' },
                { name: 'Language', icon: 'fas fa-language', unit: '' },
                { name: 'Format', icon: 'fas fa-book-open', unit: '' },
                { name: 'Publication Date', icon: 'fas fa-calendar-alt', unit: '' },
                { name: 'Genre', icon: 'fas fa-tags', unit: '' }
            ]
        },
        'health-beauty': {
            name: 'Health & Beauty',
            icon: 'fas fa-spa',
            attributes: [
                { name: 'Volume', icon: 'fas fa-flask', unit: 'ml' },
                { name: 'Weight', icon: 'fas fa-weight-hanging', unit: 'g' },
                { name: 'Skin Type', icon: 'fas fa-hand-sparkles', unit: '' },
                { name: 'Ingredients', icon: 'fas fa-list', unit: '' },
                { name: 'Fragrance', icon: 'fas fa-spray-can', unit: '' },
                { name: 'SPF', icon: 'fas fa-sun', unit: '' },
                { name: 'Cruelty Free', icon: 'fas fa-paw', unit: '' },
                { name: 'Organic', icon: 'fas fa-leaf', unit: '' }
            ]
        },
        'industrial': {
            name: 'Industrial / Tools',
            icon: 'fas fa-industry',
            attributes: [
                { name: 'Power', icon: 'fas fa-bolt', unit: 'W' },
                { name: 'Voltage', icon: 'fas fa-plug', unit: 'V' },
                { name: 'Weight', icon: 'fas fa-weight-hanging', unit: 'kg' },
                { name: 'Dimensions', icon: 'fas fa-ruler-combined', unit: 'mm' },
                { name: 'Material', icon: 'fas fa-layer-group', unit: '' },
                { name: 'Speed', icon: 'fas fa-tachometer-alt', unit: 'RPM' },
                { name: 'Capacity', icon: 'fas fa-box', unit: '' },
                { name: 'Warranty', icon: 'fas fa-shield-alt', unit: 'years' },
                { name: 'Certification', icon: 'fas fa-certificate', unit: '' }
            ]
        },
        'pets': {
            name: 'Pet Supplies',
            icon: 'fas fa-paw',
            attributes: [
                { name: 'Pet Type', icon: 'fas fa-paw', unit: '' },
                { name: 'Size', icon: 'fas fa-ruler', unit: '' },
                { name: 'Weight', icon: 'fas fa-weight-hanging', unit: 'kg' },
                { name: 'Age Group', icon: 'fas fa-birthday-cake', unit: '' },
                { name: 'Breed Size', icon: 'fas fa-dog', unit: '' },
                { name: 'Material', icon: 'fas fa-layer-group', unit: '' },
                { name: 'Flavor', icon: 'fas fa-bone', unit: '' },
                { name: 'Ingredients', icon: 'fas fa-list', unit: '' }
            ]
        },
        'garden': {
            name: 'Garden / Outdoor',
            icon: 'fas fa-seedling',
            attributes: [
                { name: 'Material', icon: 'fas fa-layer-group', unit: '' },
                { name: 'Dimensions', icon: 'fas fa-ruler-combined', unit: 'cm' },
                { name: 'Weight', icon: 'fas fa-weight-hanging', unit: 'kg' },
                { name: 'Color', icon: 'fas fa-palette', unit: '' },
                { name: 'Weather Resistant', icon: 'fas fa-cloud-sun', unit: '' },
                { name: 'Plant Type', icon: 'fas fa-seedling', unit: '' },
                { name: 'Sun Exposure', icon: 'fas fa-sun', unit: '' },
                { name: 'Watering Needs', icon: 'fas fa-tint', unit: '' },
                { name: 'Assembly Required', icon: 'fas fa-tools', unit: '' }
            ]
        },
        'toys': {
            name: 'Toys / Games',
            icon: 'fas fa-gamepad',
            attributes: [
                { name: 'Age Range', icon: 'fas fa-child', unit: '' },
                { name: 'Material', icon: 'fas fa-layer-group', unit: '' },
                { name: 'Dimensions', icon: 'fas fa-ruler-combined', unit: 'cm' },
                { name: 'Battery Required', icon: 'fas fa-battery-half', unit: '' },
                { name: 'Players', icon: 'fas fa-users', unit: '' },
                { name: 'Skill Level', icon: 'fas fa-chart-line', unit: '' },
                { name: 'Theme', icon: 'fas fa-star', unit: '' },
                { name: 'Safety Certifications', icon: 'fas fa-shield-alt', unit: '' }
            ]
        }
    };

    var GGMCIconPicker = {
        modal: null,
        currentInput: null,
        currentPreview: null,
        selectedIcon: null,
        uploadedSvgUrl: null,
        mediaFrame: null,

        init: function() {
            this.createModal();
            this.bindEvents();
        },

        createModal: function() {
            var modalHtml = '<div id="ggmc-icon-picker-modal" class="ggmc-icon-picker-modal">' +
                '<div class="ggmc-icon-picker-overlay"></div>' +
                '<div class="ggmc-icon-picker-container">' +
                    '<div class="ggmc-icon-picker-header">' +
                        '<h2>Select Icon</h2>' +
                        '<button type="button" class="ggmc-icon-picker-close">&times;</button>' +
                    '</div>' +
                    '<div class="ggmc-icon-picker-tabs">' +
                        '<button type="button" class="ggmc-icon-tab active" data-tab="library">Icon Library</button>' +
                        '<button type="button" class="ggmc-icon-tab" data-tab="presets">Presets</button>' +
                        '<button type="button" class="ggmc-icon-tab" data-tab="media">Media Library</button>' +
                        '<button type="button" class="ggmc-icon-tab" data-tab="upload">Upload SVG</button>' +
                    '</div>' +
                    '<div class="ggmc-icon-picker-content">' +
                        '<div class="ggmc-icon-tab-content active" data-tab="library">' +
                            '<div class="ggmc-icon-picker-search">' +
                                '<input type="text" placeholder="Search icons..." class="ggmc-icon-search-input">' +
                            '</div>' +
                            '<div class="ggmc-icon-picker-categories">' +
                                '<button type="button" class="ggmc-category-btn active" data-category="popular">Popular</button>' +
                                '<button type="button" class="ggmc-category-btn" data-category="Solid">Solid</button>' +
                                '<button type="button" class="ggmc-category-btn" data-category="Regular">Regular</button>' +
                            '</div>' +
                            '<div class="ggmc-icon-picker-grid"></div>' +
                        '</div>' +
                        '<div class="ggmc-icon-tab-content" data-tab="presets">' +
                            '<div class="ggmc-presets-area">' +
                                '<p class="ggmc-presets-description">Select a preset group to quickly find icons for common product attributes.</p>' +
                                '<div class="ggmc-presets-grid"></div>' +
                                '<div class="ggmc-preset-attributes" style="display:none;">' +
                                    '<div class="ggmc-preset-header">' +
                                        '<button type="button" class="ggmc-preset-back">&larr; Back to Groups</button>' +
                                        '<span class="ggmc-preset-title"></span>' +
                                    '</div>' +
                                    '<div class="ggmc-preset-attrs-grid"></div>' +
                                '</div>' +
                            '</div>' +
                        '</div>' +
                        '<div class="ggmc-icon-tab-content" data-tab="media">' +
                            '<div class="ggmc-media-library-area">' +
                                '<p class="ggmc-media-description">Select an SVG from your Media Library or upload a new one.</p>' +
                                '<button type="button" class="button button-primary ggmc-open-media-library">' +
                                    '<span class="dashicons dashicons-admin-media"></span> Open Media Library' +
                                '</button>' +
                                '<div class="ggmc-media-preview" style="display:none;">' +
                                    '<div class="ggmc-media-preview-icon"></div>' +
                                    '<span class="ggmc-media-filename"></span>' +
                                '</div>' +
                            '</div>' +
                        '</div>' +
                        '<div class="ggmc-icon-tab-content" data-tab="upload">' +
                            '<div class="ggmc-svg-upload-area">' +
                                '<div class="ggmc-svg-dropzone">' +
                                    '<i class="fas fa-cloud-upload-alt"></i>' +
                                    '<p>Drag & drop SVG file here or click to upload</p>' +
                                    '<input type="file" accept=".svg" class="ggmc-svg-file-input">' +
                                '</div>' +
                                '<div class="ggmc-svg-preview" style="display:none;">' +
                                    '<div class="ggmc-svg-preview-icon"></div>' +
                                    '<button type="button" class="ggmc-svg-remove">Remove</button>' +
                                '</div>' +
                            '</div>' +
                        '</div>' +
                    '</div>' +
                    '<div class="ggmc-icon-picker-footer">' +
                        '<div class="ggmc-icon-picker-selected">' +
                            '<span class="ggmc-selected-icon-preview"></span>' +
                            '<span class="ggmc-selected-icon-name">No icon selected</span>' +
                        '</div>' +
                        '<div class="ggmc-icon-picker-actions">' +
                            '<button type="button" class="button ggmc-icon-picker-clear">Clear</button>' +
                            '<button type="button" class="button button-primary ggmc-icon-picker-insert">Insert</button>' +
                        '</div>' +
                    '</div>' +
                '</div>' +
            '</div>';

            $('body').append(modalHtml);
            this.modal = $('#ggmc-icon-picker-modal');
        },

        bindEvents: function() {
            var self = this;

            // Open modal
            $(document).on('click', '.ggmc-icon-picker-trigger', function(e) {
                e.preventDefault();
                var $wrapper = $(this).closest('.ggmc-icon-picker-wrapper');
                self.currentInput = $wrapper.find('.ggmc-icon-value');
                self.currentPreview = $wrapper.find('.ggmc-icon-preview');
                self.openModal();
            });

            // Close modal
            this.modal.on('click', '.ggmc-icon-picker-close, .ggmc-icon-picker-overlay', function() {
                self.closeModal();
            });

            // Tab switching
            this.modal.on('click', '.ggmc-icon-tab', function() {
                var tab = $(this).data('tab');
                $('.ggmc-icon-tab').removeClass('active');
                $(this).addClass('active');
                $('.ggmc-icon-tab-content').removeClass('active');
                $('.ggmc-icon-tab-content[data-tab="' + tab + '"]').addClass('active');

                // Render presets grid when switching to presets tab
                if (tab === 'presets') {
                    self.renderPresetGroups();
                }
            });

            // Category switching
            this.modal.on('click', '.ggmc-category-btn', function() {
                var category = $(this).data('category');
                $('.ggmc-category-btn').removeClass('active');
                $(this).addClass('active');
                self.renderIcons(category);
            });

            // Search
            this.modal.on('input', '.ggmc-icon-search-input', function() {
                var query = $(this).val().toLowerCase();
                self.searchIcons(query);
            });

            // Select icon
            this.modal.on('click', '.ggmc-icon-item', function() {
                $('.ggmc-icon-item').removeClass('selected');
                $(this).addClass('selected');
                var iconClass = $(this).data('icon');
                self.selectIcon(iconClass, 'fa');
            });

            // SVG upload
            this.modal.on('click', '.ggmc-svg-dropzone', function() {
                $(this).find('.ggmc-svg-file-input').click();
            });

            this.modal.on('change', '.ggmc-svg-file-input', function(e) {
                if (e.target.files && e.target.files[0]) {
                    self.uploadSvg(e.target.files[0]);
                }
            });

            // SVG drag and drop
            this.modal.on('dragover', '.ggmc-svg-dropzone', function(e) {
                e.preventDefault();
                $(this).addClass('dragover');
            });

            this.modal.on('dragleave', '.ggmc-svg-dropzone', function(e) {
                e.preventDefault();
                $(this).removeClass('dragover');
            });

            this.modal.on('drop', '.ggmc-svg-dropzone', function(e) {
                e.preventDefault();
                $(this).removeClass('dragover');
                var files = e.originalEvent.dataTransfer.files;
                if (files && files[0] && files[0].type === 'image/svg+xml') {
                    self.uploadSvg(files[0]);
                }
            });

            // Remove SVG
            this.modal.on('click', '.ggmc-svg-remove', function() {
                self.removeSvg();
            });

            // Open WordPress Media Library
            this.modal.on('click', '.ggmc-open-media-library', function(e) {
                e.preventDefault();
                self.openMediaLibrary();
            });

            // Preset group click
            this.modal.on('click', '.ggmc-preset-group-item', function() {
                var groupKey = $(this).data('group');
                self.showPresetAttributes(groupKey);
            });

            // Preset back button
            this.modal.on('click', '.ggmc-preset-back', function() {
                self.showPresetGroups();
            });

            // Preset attribute icon click
            this.modal.on('click', '.ggmc-preset-attr-item', function() {
                $('.ggmc-preset-attr-item').removeClass('selected');
                $(this).addClass('selected');
                var iconClass = $(this).data('icon');
                self.selectIcon(iconClass, 'fa');
            });

            // Clear selection
            this.modal.on('click', '.ggmc-icon-picker-clear', function() {
                self.clearSelection();
            });

            // Insert icon
            this.modal.on('click', '.ggmc-icon-picker-insert', function() {
                self.insertIcon();
            });

            // ESC key to close
            $(document).on('keyup', function(e) {
                if (e.key === 'Escape' && self.modal.hasClass('open')) {
                    self.closeModal();
                }
            });
        },

        openModal: function() {
            this.modal.addClass('open');
            $('body').addClass('ggmc-modal-open');

            // Load current value
            var currentValue = this.currentInput.val();
            if (currentValue) {
                this.loadCurrentIcon(currentValue);
            } else {
                this.clearSelection();
            }

            // Render popular icons by default
            this.renderIcons('popular');
        },

        closeModal: function() {
            this.modal.removeClass('open');
            $('body').removeClass('ggmc-modal-open');
            this.selectedIcon = null;
            this.uploadedSvgUrl = null;
        },

        renderIcons: function(category) {
            var $grid = this.modal.find('.ggmc-icon-picker-grid');
            $grid.empty();

            var icons = category === 'popular' ? popularIcons : iconLibrary[category] || [];

            icons.forEach(function(iconClass) {
                var iconName = iconClass.replace(/^(fas|far|fab)\s+fa-/, '');
                var $item = $('<div class="ggmc-icon-item" data-icon="' + iconClass + '" title="' + iconName + '">' +
                    '<i class="' + iconClass + '"></i>' +
                '</div>');
                $grid.append($item);
            });
        },

        searchIcons: function(query) {
            var $grid = this.modal.find('.ggmc-icon-picker-grid');
            $grid.empty();

            if (query.length < 2) {
                // Show category icons
                var activeCategory = this.modal.find('.ggmc-category-btn.active').data('category');
                this.renderIcons(activeCategory);
                return;
            }

            // Search all icons
            var results = [];
            Object.keys(iconLibrary).forEach(function(category) {
                iconLibrary[category].forEach(function(iconClass) {
                    var iconName = iconClass.replace(/^(fas|far|fab)\s+fa-/, '');
                    if (iconName.indexOf(query) !== -1) {
                        results.push(iconClass);
                    }
                });
            });

            // Remove duplicates and render
            results = [...new Set(results)];
            results.forEach(function(iconClass) {
                var iconName = iconClass.replace(/^(fas|far|fab)\s+fa-/, '');
                var $item = $('<div class="ggmc-icon-item" data-icon="' + iconClass + '" title="' + iconName + '">' +
                    '<i class="' + iconClass + '"></i>' +
                '</div>');
                $grid.append($item);
            });

            if (results.length === 0) {
                $grid.html('<div class="ggmc-no-results">No icons found</div>');
            }
        },

        renderPresetGroups: function() {
            var $grid = this.modal.find('.ggmc-presets-grid');
            $grid.empty();

            // Show the groups grid, hide the attributes
            this.modal.find('.ggmc-presets-grid').show();
            this.modal.find('.ggmc-presets-description').show();
            this.modal.find('.ggmc-preset-attributes').hide();

            Object.keys(presetGroups).forEach(function(key) {
                var group = presetGroups[key];
                var $item = $('<div class="ggmc-preset-group-item" data-group="' + key + '">' +
                    '<div class="ggmc-preset-group-icon"><i class="' + group.icon + '"></i></div>' +
                    '<div class="ggmc-preset-group-name">' + group.name + '</div>' +
                    '<div class="ggmc-preset-group-count">' + group.attributes.length + ' attributes</div>' +
                '</div>');
                $grid.append($item);
            });
        },

        showPresetAttributes: function(groupKey) {
            var group = presetGroups[groupKey];
            if (!group) return;

            // Hide groups grid, show attributes
            this.modal.find('.ggmc-presets-grid').hide();
            this.modal.find('.ggmc-presets-description').hide();
            this.modal.find('.ggmc-preset-attributes').show();
            this.modal.find('.ggmc-preset-title').html('<i class="' + group.icon + '"></i> ' + group.name);

            var $attrsGrid = this.modal.find('.ggmc-preset-attrs-grid');
            $attrsGrid.empty();

            group.attributes.forEach(function(attr) {
                var unitText = attr.unit ? ' <span class="ggmc-preset-attr-unit">(' + attr.unit + ')</span>' : '';
                var $item = $('<div class="ggmc-preset-attr-item" data-icon="' + attr.icon + '">' +
                    '<div class="ggmc-preset-attr-icon"><i class="' + attr.icon + '"></i></div>' +
                    '<div class="ggmc-preset-attr-name">' + attr.name + unitText + '</div>' +
                '</div>');
                $attrsGrid.append($item);
            });
        },

        showPresetGroups: function() {
            this.modal.find('.ggmc-presets-grid').show();
            this.modal.find('.ggmc-presets-description').show();
            this.modal.find('.ggmc-preset-attributes').hide();
        },

        selectIcon: function(iconValue, library) {
            this.selectedIcon = {
                value: iconValue,
                library: library || 'fa-solid'
            };
            this.uploadedSvgUrl = null;

            // Update preview
            var $preview = this.modal.find('.ggmc-selected-icon-preview');
            var $name = this.modal.find('.ggmc-selected-icon-name');

            $preview.html('<i class="' + iconValue + '"></i>');
            $name.text(iconValue.replace(/^(fas|far|fab)\s+fa-/, ''));
        },

        uploadSvg: function(file) {
            var self = this;
            var formData = new FormData();
            formData.append('action', 'ggmc_upload_svg_icon');
            formData.append('nonce', ggmcIconPicker.nonce);
            formData.append('svg_file', file);

            $.ajax({
                url: ggmcIconPicker.ajaxUrl,
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function(response) {
                    if (response.success) {
                        self.uploadedSvgUrl = response.data.url;
                        self.selectedIcon = {
                            value: response.data.url,
                            library: 'svg'
                        };

                        // Show preview
                        self.modal.find('.ggmc-svg-dropzone').hide();
                        self.modal.find('.ggmc-svg-preview').show();
                        self.modal.find('.ggmc-svg-preview-icon').html('<img src="' + response.data.url + '" alt="SVG Icon">');

                        // Update footer preview
                        self.modal.find('.ggmc-selected-icon-preview').html('<img src="' + response.data.url + '" alt="SVG">');
                        self.modal.find('.ggmc-selected-icon-name').text('Custom SVG');
                    } else {
                        alert(response.data.message || 'Upload failed');
                    }
                },
                error: function() {
                    alert('Upload failed. Please try again.');
                }
            });
        },

        removeSvg: function() {
            this.uploadedSvgUrl = null;
            this.modal.find('.ggmc-svg-dropzone').show();
            this.modal.find('.ggmc-svg-preview').hide();
            this.modal.find('.ggmc-svg-preview-icon').empty();
            this.modal.find('.ggmc-svg-file-input').val('');

            // Also clear media library preview
            this.modal.find('.ggmc-media-preview').hide();
            this.modal.find('.ggmc-media-preview-icon').empty();
            this.modal.find('.ggmc-media-filename').empty();

            if (this.selectedIcon && this.selectedIcon.library === 'svg') {
                this.clearSelection();
            }
        },

        openMediaLibrary: function() {
            var self = this;

            // If frame already exists, open it
            if (this.mediaFrame) {
                this.mediaFrame.open();
                return;
            }

            // Create the media frame
            this.mediaFrame = wp.media({
                title: 'Select SVG Icon',
                button: {
                    text: 'Use this icon'
                },
                library: {
                    type: 'image/svg+xml'
                },
                multiple: false
            });

            // When an image is selected
            this.mediaFrame.on('select', function() {
                var attachment = self.mediaFrame.state().get('selection').first().toJSON();

                // Verify it's an SVG
                if (attachment.subtype !== 'svg+xml' && attachment.mime !== 'image/svg+xml') {
                    alert('Please select an SVG file.');
                    return;
                }

                self.uploadedSvgUrl = attachment.url;
                self.selectedIcon = {
                    value: attachment.url,
                    library: 'svg'
                };

                // Show preview in media tab
                self.modal.find('.ggmc-media-preview').show();
                self.modal.find('.ggmc-media-preview-icon').html('<img src="' + attachment.url + '" alt="SVG Icon">');
                self.modal.find('.ggmc-media-filename').text(attachment.filename || 'Selected SVG');

                // Update footer preview
                self.modal.find('.ggmc-selected-icon-preview').html('<img src="' + attachment.url + '" alt="SVG">');
                self.modal.find('.ggmc-selected-icon-name').text(attachment.filename || 'Media Library SVG');
            });

            // Open the media frame
            this.mediaFrame.open();
        },

        loadCurrentIcon: function(value) {
            // Try to parse as JSON
            try {
                var iconData = JSON.parse(value);
                if (iconData.library === 'svg') {
                    this.uploadedSvgUrl = iconData.value;
                    this.selectedIcon = iconData;
                    this.modal.find('.ggmc-selected-icon-preview').html('<img src="' + iconData.value + '" alt="SVG">');
                    this.modal.find('.ggmc-selected-icon-name').text('Custom SVG');
                } else {
                    this.selectedIcon = iconData;
                    this.modal.find('.ggmc-selected-icon-preview').html('<i class="' + iconData.value + '"></i>');
                    this.modal.find('.ggmc-selected-icon-name').text(iconData.value.replace(/^(fas|far|fab)\s+fa-/, ''));
                    // Highlight in grid
                    this.modal.find('.ggmc-icon-item[data-icon="' + iconData.value + '"]').addClass('selected');
                }
            } catch (e) {
                // Legacy format - just a class string
                if (value) {
                    this.selectedIcon = {
                        value: value,
                        library: 'fa-solid'
                    };
                    this.modal.find('.ggmc-selected-icon-preview').html('<i class="' + value + '"></i>');
                    this.modal.find('.ggmc-selected-icon-name').text(value.replace(/^(fas|far|fab)\s+fa-/, ''));
                }
            }
        },

        clearSelection: function() {
            this.selectedIcon = null;
            this.uploadedSvgUrl = null;
            this.modal.find('.ggmc-icon-item').removeClass('selected');
            this.modal.find('.ggmc-selected-icon-preview').empty();
            this.modal.find('.ggmc-selected-icon-name').text('No icon selected');
            this.removeSvg();
        },

        insertIcon: function() {
            if (!this.selectedIcon) {
                this.currentInput.val('');
                this.currentPreview.html('<span class="dashicons dashicons-image-rotate"></span>');
            } else {
                // Store as JSON
                this.currentInput.val(JSON.stringify(this.selectedIcon));

                // Update preview
                if (this.selectedIcon.library === 'svg') {
                    this.currentPreview.html('<img src="' + this.selectedIcon.value + '" alt="Icon">');
                } else {
                    this.currentPreview.html('<i class="' + this.selectedIcon.value + '"></i>');
                }
            }

            this.closeModal();
        }
    };

    // Initialize when document ready
    $(document).ready(function() {
        if (typeof ggmcIconPicker !== 'undefined') {
            GGMCIconPicker.init();
        }
    });

})(jQuery);
