(function initLeftbar() {
    const menuIcons = {
      'menu-item-notifications_button_small': 'https://fs.getcourse.ru/fileservice/file/download/a/951813/sc/307/h/ccb63abf9ee8fe703dd583b894737a53.svg',
      'menu-item-cms': 'https://fs.getcourse.ru/fileservice/file/download/a/951813/sc/146/h/f69255a720eef6672d82a0760e5ba2f1.svg',
      'menu-item-teach': 'https://fs.getcourse.ru/fileservice/file/download/a/951813/sc/386/h/d9eaeb2aa2179a6d963de41f92ed05fa.svg',
      'menu-item-user': 'https://fs.getcourse.ru/fileservice/file/download/a/951813/sc/111/h/e676d3284a001d6991697600fdce86af.svg',
      'menu-item-tasks': 'https://fs.getcourse.ru/fileservice/file/download/a/951813/sc/318/h/11e464394420319a9d255490084a6093.svg',
      'menu-item-notifications': 'https://fs.getcourse.ru/fileservice/file/download/a/951813/sc/93/h/34b036c5b49657254e4db7b9a21506af.svg',
      'menu-item-sales': 'https://fs.getcourse.ru/fileservice/file/download/a/951813/sc/238/h/409e5c6070a9e92da816d8aed023ebd3.svg',
      'menu-item-chatium': 'https://fs.getcourse.ru/fileservice/file/download/a/951813/sc/291/h/2f06409831f5f025a66d6dbc9ccdbc7e.svg',
    };

    const svgCache = {};
    let applying = false;

    function ensureGradientDefs(svg) {
      if (svg.querySelector('linearGradient.kb-icon-gradient')) return svg.querySelector('linearGradient.kb-icon-gradient').id;
      const gradId = `kb-icon-gradient-${Math.random().toString(36).slice(2, 9)}`;
      const defs = document.createElementNS('http://www.w3.org/2000/svg', 'defs');
      defs.innerHTML = `
        <linearGradient class="kb-icon-gradient" id="${gradId}" x1="0%" y1="0%" x2="100%" y2="100%" gradientUnits="objectBoundingBox">
          <stop offset="0%" stop-color="#11A2C5"/>
          <stop offset="100%" stop-color="#07B5EA"/>
        </linearGradient>`;
      svg.insertBefore(defs, svg.firstChild);
      return gradId;
    }

    function paintSvg(svg, selected) {
      const gradId = ensureGradientDefs(svg);
      const color = selected ? `url(#${gradId})` : '#ffffff';
      svg.querySelectorAll('[stroke]').forEach((el) => {
        if (el.getAttribute('stroke') === 'none') return;
        el.setAttribute('stroke', color);
      });
      svg.querySelectorAll('[fill]').forEach((el) => {
        const fill = el.getAttribute('fill');
        if (!fill || fill === 'none') return;
        el.setAttribute('fill', color);
      });
    }

    function syncIconColors(root) {
      const scope = root || document;
      scope.querySelectorAll('.gc-account-user-menu li .kb-menu-icon').forEach((svg) => {
        const li = svg.closest('li');
        paintSvg(svg, !!(li && li.classList.contains('selected')));
      });
    }

    async function loadSvg(url) {
      if (svgCache[url]) return svgCache[url];
      const res = await fetch(url, { credentials: 'omit' });
      const text = await res.text();
      svgCache[url] = text;
      return text;
    }

    async function applyMenuIcons(root) {
      if (applying) return;
      applying = true;
      try {
        const scope = root || document;
        for (const [className, src] of Object.entries(menuIcons)) {
          const items = scope.querySelectorAll(`.gc-account-user-menu li.${className}`);
          for (const li of items) {
            const link = li.querySelector('a');
            if (!link) continue;

            let svg = link.querySelector('svg.kb-menu-icon');
            if (!svg) {
              const img = link.querySelector('img.menu-item-icon');
              try {
                const markup = await loadSvg(src);
                const wrap = document.createElement('div');
                wrap.innerHTML = markup.trim();
                svg = wrap.querySelector('svg');
                if (!svg) continue;
                svg.classList.add('kb-menu-icon', 'menu-item-icon');
                svg.setAttribute('width', '22');
                svg.setAttribute('height', '22');
                svg.dataset.kbIcon = src;
                if (img) img.replaceWith(svg);
                else link.appendChild(svg);
              } catch (e) {
                console.warn('leftbar icon load failed', src, e);
                continue;
              }
            }

            // GC may re-inject vanilla img on select — remove it
            link.querySelectorAll('img.menu-item-icon').forEach((img) => img.remove());
            paintSvg(svg, li.classList.contains('selected'));
          }
        }
      } finally {
        applying = false;
      }
    }

    function run() {
      const menu = document.querySelector('.gc-account-user-menu');

      applyMenuIcons();

      if (menu && !menu.dataset.kbIconsObserved) {
        menu.dataset.kbIconsObserved = '1';
        const observer = new MutationObserver((mutations) => {
          let needApply = false;
          let needColor = false;
          for (const mutation of mutations) {
            if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
              needColor = true;
            }
            if (mutation.type === 'attributes' && mutation.attributeName === 'src') {
              needApply = true;
            }
            if (mutation.type === 'childList' && mutation.addedNodes.length) {
              needApply = true;
            }
          }
          if (needApply) applyMenuIcons(menu);
          else if (needColor) syncIconColors(menu);
        });
        observer.observe(menu, {
          subtree: true,
          childList: true,
          attributes: true,
          attributeFilter: ['src', 'class'],
        });
      }
    }

    if (document.readyState === 'loading') {
      document.addEventListener('DOMContentLoaded', run);
    } else {
      run();
    }
  })();