function Buzzer_InitializeOptions()
    if BuzzerOptionsPanel then return end

    local panel = CreateFrame("Frame", "BuzzerOptionsPanel", UIParent)
    panel.name = "Buzzer"
    BuzzerOptionsPanel = panel
    BuzzerSettings = BuzzerSettings or {}

    -- Defaults
    BuzzerSettings.model = BuzzerSettings.model or 1
    BuzzerSettings.enableSound = BuzzerSettings.enableSound ~= false
    BuzzerSettings.soundChannel = BuzzerSettings.soundChannel or "Master"

    local leftMargin = 16
    local indent = 20
    local spacingY = -12
    local currentAnchor = nil

    -- Title
    local title = panel:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
    title:SetPoint("TOPLEFT", leftMargin, -16)
    title:SetText("Buzzer Settings")
    currentAnchor = title

    -- Radio Model Selection
    local modelLabel = panel:CreateFontString(nil, "ARTWORK", "GameFontNormal")
    modelLabel:SetPoint("TOPLEFT", currentAnchor, "BOTTOMLEFT", 0, -20)
    modelLabel:SetText("Select your radio model:")
    currentAnchor = modelLabel

    local radioButtons = {}
    for index, config in ipairs(Buzzer_Models or {}) do
        local btn = CreateFrame("CheckButton", nil, panel, "UICheckButtonTemplate")
        btn.Text:SetText(config.name or ("Model " .. index))
        btn:SetPoint("TOPLEFT", currentAnchor, "BOTTOMLEFT", 0, spacingY)
        currentAnchor = btn
    
        -- Capture this index and config in a closure
        do
            local modelIndex = index
    
            btn:SetScript("OnClick", function()
                for _, other in ipairs(radioButtons) do other:SetChecked(false) end
                btn:SetChecked(true)
                BuzzerSettings.model = modelIndex
                if Buzzer_SwitchModel then Buzzer_SwitchModel(modelIndex) end
            end)
        end
    
        if BuzzerSettings.model == index then
            btn:SetChecked(true)
        end
    
        table.insert(radioButtons, btn)
    end
    

    -- Divider 1
    local divider1 = panel:CreateTexture(nil, "ARTWORK")
    divider1:SetColorTexture(1, 1, 1, 0.2)
    divider1:SetSize(480, 1)
    divider1:SetPoint("TOPLEFT", currentAnchor, "BOTTOMLEFT", 0, -12)
    currentAnchor = divider1

    -- Custom Display Name
    local nameLabel = panel:CreateFontString(nil, "ARTWORK", "GameFontNormal")
    nameLabel:SetPoint("TOPLEFT", currentAnchor, "BOTTOMLEFT", 0, -14)
    nameLabel:SetText("Custom display name:")
    currentAnchor = nameLabel

    local nameInput = CreateFrame("EditBox", nil, panel, "InputBoxTemplate")
    nameInput:SetSize(200, 24)
    nameInput:SetAutoFocus(false)
    nameInput:SetPoint("TOPLEFT", nameLabel, "BOTTOMLEFT", indent, spacingY)
    panel.nameInput = nameInput

    local updateButton = CreateFrame("Button", nil, panel, "UIPanelButtonTemplate")
    updateButton:SetSize(80, 22)
    updateButton:SetText("Update")
    updateButton:SetPoint("LEFT", nameInput, "RIGHT", 10, 0)
    updateButton:SetScript("OnClick", function()
        local name = nameInput:GetText()
        BuzzerSettings.customName = name
        nameInput:SetText(name)
        print("Buzzer display name updated to: " .. (name ~= "" and name or UnitName("player")))
    end)

    panel:SetScript("OnShow", function()
        nameInput:SetText(BuzzerSettings.customName or "")
    end)

    -- Divider 2
    local divider2 = panel:CreateTexture(nil, "ARTWORK")
    divider2:SetColorTexture(1, 1, 1, 0.2)
    divider2:SetSize(480, 1)
    divider2:SetPoint("TOPLEFT", nameInput, "BOTTOMLEFT", -indent, -16)
    currentAnchor = divider2

    -- Sound Options
    local soundLabel = panel:CreateFontString(nil, "ARTWORK", "GameFontNormal")
    soundLabel:SetPoint("TOPLEFT", currentAnchor, "BOTTOMLEFT", 0, -14)
    soundLabel:SetText("Sound Options")
    currentAnchor = soundLabel

    local soundCheckbox = CreateFrame("CheckButton", nil, panel, "InterfaceOptionsCheckButtonTemplate")
    soundCheckbox.Text:SetText("Enable Buzzer sound effects")
    soundCheckbox:SetPoint("TOPLEFT", soundLabel, "BOTTOMLEFT", 2, spacingY)
    soundCheckbox:SetChecked(BuzzerSettings.enableSound)
    soundCheckbox:SetScript("OnClick", function(self)
        BuzzerSettings.enableSound = self:GetChecked()
    end)
    currentAnchor = soundCheckbox

    local channelLabel = panel:CreateFontString(nil, "ARTWORK", "GameFontNormal")
    channelLabel:SetPoint("TOPLEFT", soundCheckbox, "BOTTOMLEFT", 0, -16)
    channelLabel:SetText("Sound channel:")
    currentAnchor = channelLabel

    local soundChannels = { "Master", "SFX", "Music", "Ambience", "Dialog" }
    local previousButton = nil
    local channelButtons = {}

    for _, chan in ipairs(soundChannels) do
        local btn = CreateFrame("CheckButton", nil, panel, "InterfaceOptionsCheckButtonTemplate")
        btn.Text:SetText(chan)

        if previousButton then
            btn:SetPoint("LEFT", previousButton, "RIGHT", 50, 0)
        else
            btn:SetPoint("TOPLEFT", channelLabel, "BOTTOMLEFT", 2, -6)
            btn:SetPoint("LEFT", panel, "LEFT", leftMargin, 0)
        end

        btn:SetScript("OnClick", function(self)
            for _, other in ipairs(channelButtons) do other:SetChecked(false) end
            self:SetChecked(true)
            BuzzerSettings.soundChannel = chan
        end)

        table.insert(channelButtons, btn)
        previousButton = btn
    end

    C_Timer.After(0, function()
        for _, btn in ipairs(channelButtons) do
            if btn.Text:GetText() == BuzzerSettings.soundChannel then
                btn:SetChecked(true)
            end
        end
    end)

    -- Create virtual anchor frame for accurate placement
    local afterSoundRow = CreateFrame("Frame", nil, panel)
    afterSoundRow:SetSize(1, 1)
    afterSoundRow:SetPoint("TOPLEFT", channelLabel, "BOTTOMLEFT", 0, -40)
    currentAnchor = afterSoundRow

    -- Divider 3
    local divider3 = panel:CreateTexture(nil, "ARTWORK")
    divider3:SetColorTexture(1, 1, 1, 0.2)
    divider3:SetSize(480, 1)
    divider3:SetPoint("TOPLEFT", currentAnchor, "BOTTOMLEFT", 0, -12)
    currentAnchor = divider3

    -- Donate Dialog
    local dialog = CreateFrame("Frame", "BuzzerDonateDialog", UIParent, "BasicFrameTemplateWithInset")
    dialog:SetSize(300, 100)
    dialog:SetPoint("CENTER")
    dialog:SetFrameStrata("DIALOG")
    dialog:Hide()
    dialog:SetMovable(true)
    dialog:EnableMouse(true)
    dialog:RegisterForDrag("LeftButton")
    dialog:SetScript("OnDragStart", dialog.StartMoving)
    dialog:SetScript("OnDragStop", dialog.StopMovingOrSizing)

    dialog.title = dialog:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
    dialog.title:SetPoint("TOP", 0, -12)
    dialog.title:SetText("Support Buzzer")

    dialog.editBox = CreateFrame("EditBox", nil, dialog, "InputBoxTemplate")
    dialog.editBox:SetSize(240, 20)
    dialog.editBox:SetPoint("TOP", 0, -40)
    dialog.editBox:SetAutoFocus(false)
    dialog.editBox:SetText("https://buymeacoffee.com/hardhy")

    dialog.closeButton = CreateFrame("Button", nil, dialog, "UIPanelButtonTemplate")
    dialog.closeButton:SetSize(60, 20)
    dialog.closeButton:SetText("Close")
    dialog.closeButton:SetPoint("BOTTOM", 0, 10)
    dialog.closeButton:SetScript("OnClick", function() dialog:Hide() end)

    -- Coffee button
    local coffeeButton = CreateFrame("Button", nil, panel)
    coffeeButton:SetSize(192, 48)
    coffeeButton:SetPoint("TOPLEFT", currentAnchor, "BOTTOMLEFT", 0, -20)
    currentAnchor = coffeeButton

    local coffeeTexture = coffeeButton:CreateTexture(nil, "ARTWORK")
    coffeeTexture:SetAllPoints()
    coffeeTexture:SetTexture("Interface\\AddOns\\Buzzer\\Media\\bmc-button.blp")

    local pulse = coffeeButton:CreateTexture(nil, "OVERLAY")
    pulse:SetAllPoints()
    pulse:SetTexture("Interface\\Buttons\\UI-Panel-Button-Highlight")
    pulse:SetBlendMode("ADD")
    pulse:SetAlpha(0)

    local t = 0
    coffeeButton:SetScript("OnUpdate", function(self, elapsed)
        t = t + elapsed
        local pulseAlpha = 0.4 + 0.4 * math.sin(t * 3)
        pulse:SetAlpha(pulseAlpha)
    end)

    coffeeButton:SetScript("OnClick", function()
        BuzzerDonateDialog:Show()
        BuzzerDonateDialog.editBox:SetFocus()
        BuzzerDonateDialog.editBox:HighlightText()
    end)

    coffeeButton:SetScript("OnEnter", function()
        GameTooltip:SetOwner(coffeeButton, "ANCHOR_RIGHT")
        GameTooltip:SetText("Click to donate!", 1, 1, 1)
        GameTooltip:Show()
    end)
    coffeeButton:SetScript("OnLeave", GameTooltip_Hide)

    -- Coffee footer text
    local footerText = panel:CreateFontString(nil, "ARTWORK", "GameFontDisableSmall")
    footerText:SetPoint("TOPLEFT", coffeeButton, "BOTTOMLEFT", 0, -6)
    footerText:SetText("Like this addon? Consider buying me a coffee:")

    -- Register Settings Panel
    if Settings and Settings.RegisterCanvasLayoutCategory then
        BuzzerOptionsCategory = Settings.RegisterCanvasLayoutCategory(panel, "Buzzer")
        Settings.RegisterAddOnCategory(BuzzerOptionsCategory)
        BuzzerOptionsCategoryName = BuzzerOptionsCategory:GetID()
    else
        print("Buzzer: Could not register settings panel.")
    end
end
