window.updateMenuBadge = function (count) {
    var displayText = count > 99 ? '99+' : count.toString();
    var visible = count > 0 ? 'inline-flex' : 'none';

    function updateBadge(container) {
        var badge = container.querySelector('.kui-menu-badge');
        if (!badge) {
            badge = document.createElement('span');
            badge.className = 'kui-menu-badge';
            container.appendChild(badge);
        }
        badge.textContent = displayText;
        badge.style.display = visible;
    }

    // Top menu items in Known layout
    document.querySelectorAll('.kui-top-menu .item').forEach(function (item) {
        if (item.textContent.trim() === '站内聊天') {
            updateBadge(item);
        }
    });

    // Sidebar menu items (AntDesign MenuItem > a)
    document.querySelectorAll('.ant-menu-item a .icon-name').forEach(function (nameEl) {
        if (nameEl.textContent.trim() === '站内聊天') {
            updateBadge(nameEl.closest('a'));
        }
    });

    // SubMenu titles (ant-menu-submenu-title)
    document.querySelectorAll('.ant-menu-submenu-title .icon-name').forEach(function (nameEl) {
        if (nameEl.textContent.trim() === '站内聊天') {
            updateBadge(nameEl.closest('.ant-menu-submenu-title'));
        }
    });
};
