function UpdateSoundButtonStates()
    if not PedroMainUI or not PedroMainUI.soundButtons then return end
	
    G.soundIntensity = G.soundIntensity or {}
	
    for i, btn in ipairs(PedroMainUI.soundButtons) do
        local sound = PedroSounds[i]
        local isActive = (G.selectedSound == sound.path)
		
        btn:SetChecked(isActive)
		
        -- Handle intensity buttons if they exist
        if btn.quietBtn and btn.normalBtn and btn.loudBtn then
            local intensity = G.soundIntensity[sound.path] or "normal"
			
            local function Update(button, isSelected)
				if not isActive then
					button:SetEnabled(false)
					button:GetFontString():SetTextColor(0.6, 0.6, 0.6)
					else
					button:SetEnabled(true)
					
					if isSelected then
						button:GetFontString():SetTextColor(1, 0.82, 0)
						else
						button:GetFontString():SetTextColor(1, 1, 1)
					end
				end
			end
			
            Update(btn.quietBtn, intensity == "quiet")
            Update(btn.normalBtn, intensity == "normal")
            Update(btn.loudBtn, intensity == "loud")
		end
	end
end

function RefreshSoundListUI()
    if not PedroMainUI or not PedroMainUI.soundLabel then return end
	
    G.soundIntensity = G.soundIntensity or {}
	
    -- Clear old buttons
    if PedroMainUI.soundButtons then
        for _, btn in ipairs(PedroMainUI.soundButtons) do
			if btn.quietBtn then btn.quietBtn:Hide() end
			if btn.normalBtn then btn.normalBtn:Hide() end
			if btn.loudBtn then btn.loudBtn:Hide() end
			if btn.removeBtn then btn.removeBtn:Hide() end
			
			btn:Hide()
		end
	end
	
    local soundButtons = {}
    PedroMainUI.soundButtons = soundButtons
	
    local previous = PedroMainUI.soundLabel
	
    for i, sound in ipairs(PedroSounds) do
        local btn = CreateFrame("CheckButton", nil, PedroMainUI, "UIRadioButtonTemplate")
        btn:SetPoint("TOPLEFT", previous, "BOTTOMLEFT", 0, -4)
        btn.text:SetText(sound.name)
		
        local lastElement = btn
		
        if not sound.isCustom then
            -- QUIET
            local quietBtn = CreateFrame("Button", nil, PedroMainUI, "UIPanelButtonTemplate")
            quietBtn:SetSize(50, 18)
            quietBtn:SetPoint("LEFT", btn.text, "RIGHT", 10, 0)
            quietBtn:SetText("Quiet")
			
            quietBtn:SetScript("OnClick", function()
                if G.selectedSound ~= sound.path then return end
                G.soundIntensity[sound.path] = "quiet"
                print(sound.name .. ": Volume set to Quiet")
                UpdateSoundButtonStates()
			end)
			
            -- NORMAL
            local normalBtn = CreateFrame("Button", nil, PedroMainUI, "UIPanelButtonTemplate")
            normalBtn:SetSize(60, 18)
            normalBtn:SetPoint("LEFT", quietBtn, "RIGHT", 5, 0)
            normalBtn:SetText("Normal")
			
            normalBtn:SetScript("OnClick", function()
                if G.selectedSound ~= sound.path then return end
                G.soundIntensity[sound.path] = "normal"
                print(sound.name .. ": Volume set to Normal")
                UpdateSoundButtonStates()
			end)
			
            -- LOUD
            local loudBtn = CreateFrame("Button", nil, PedroMainUI, "UIPanelButtonTemplate")
            loudBtn:SetSize(50, 18)
            loudBtn:SetPoint("LEFT", normalBtn, "RIGHT", 5, 0)
            loudBtn:SetText("Loud")
			
            loudBtn:SetScript("OnClick", function()
                if G.selectedSound ~= sound.path then return end
                G.soundIntensity[sound.path] = "loud"
                print(sound.name .. ": Volume set to Loud")
                UpdateSoundButtonStates()
			end)
			
            btn.quietBtn = quietBtn
            btn.normalBtn = normalBtn
            btn.loudBtn = loudBtn
			
            lastElement = loudBtn
			else
            -- Remove button
            local removeBtn = CreateFrame("Button", nil, PedroMainUI, "UIPanelButtonTemplate")
            removeBtn:SetSize(60, 18)
            removeBtn:SetPoint("LEFT", btn.text, "RIGHT", 10, 0)
            removeBtn:SetText("Remove")
			
            removeBtn:SetScript("OnClick", function()
                for idx, s in ipairs(G.customSongs) do
                    if s.path == sound.path then
                        table.remove(G.customSongs, idx)
                        break
					end
				end
				
                -- Clean up stored intensity
                if G.soundIntensity then
                    G.soundIntensity[sound.path] = nil
				end
				
                if G.selectedSound == sound.path then
					G.selectedSound = BasePedroSounds[2].path
					
					-- Ensure intensity exists for the new selected sound
					G.soundIntensity = G.soundIntensity or {}
					if not G.soundIntensity[G.selectedSound] then
						G.soundIntensity[G.selectedSound] = "normal"
					end
					
					print("Pedro: Selected song was removed. Reverting to default.")
				end
				
                RebuildPedroSounds()
                RefreshSoundListUI()
			end)
			
            btn.removeBtn = removeBtn
            lastElement = removeBtn
		end
		
        -- Radio button logic
        btn:SetScript("OnClick", function(self)
            if not self:GetChecked() then
                self:SetChecked(true)
                return
			end
			
            for _, other in ipairs(soundButtons) do
                other:SetChecked(false)
			end
			
            self:SetChecked(true)
            G.selectedSound = sound.path
			
            -- Set default intensity if none exists
            if not G.soundIntensity[sound.path] then
                G.soundIntensity[sound.path] = "normal"
			end
			
            print("Pedro: Selected sound " .. sound.name)
			
            UpdateSoundButtonStates()
		end)
		
        table.insert(soundButtons, btn)
        previous = btn
	end
	
    if PedroMainUI.channelLabel then
        local lastButton = PedroMainUI.soundButtons[#PedroMainUI.soundButtons]
        PedroMainUI.channelLabel:ClearAllPoints()
        PedroMainUI.channelLabel:SetPoint("TOPLEFT", lastButton, "BOTTOMLEFT", 0, -10)
	end
	
    ResizePedroMainUI()
	
    UpdateSoundButtonStates()
end