local NS = rawget(_G, "MCDVoiceCooldownNS") or {}
_G.MCDVoiceCooldownNS = NS

NS.UI = NS.UI or {}
NS.UI.Skin = NS.UI.Skin or {}

local Skin = NS.UI.Skin

Skin.Theme = Skin.Theme or {
    bg = { 0.04, 0.04, 0.04, 0.96 },
    panel = { 0.07, 0.07, 0.07, 0.92 },
    inset = { 0.02, 0.02, 0.02, 0.90 },
    gold = { 1.00, 0.82, 0.18, 1.00 },
    softGold = { 0.85, 0.70, 0.18, 0.55 },
    text = { 0.92, 0.92, 0.92, 1.00 },
    muted = { 0.62, 0.62, 0.62, 1.00 },
    hover = { 1.00, 0.82, 0.18, 0.08 },
    selected = { 1.00, 0.82, 0.18, 0.18 },
    danger = { 0.72, 0.20, 0.16, 0.22 },
}

local FONT = "Fonts\\FRIZQT__.TTF"
local BLANK = "Interface\\Buttons\\WHITE8x8"

local function ApplyFont(fontString, size, flags)
    if not fontString or not fontString.SetFont then
        return
    end

    local font = rawget(_G, "STANDARD_TEXT_FONT") or FONT
    if not fontString:SetFont(font, size, flags or "") then
        fontString:SetFont(FONT, size, flags or "")
    end
end

local function SetTextureColor(texture, color)
    if texture and texture.SetColorTexture then
        texture:SetColorTexture(color[1], color[2], color[3], color[4])
    end
end

local function HideRegion(region)
    if region and region.Hide then
        region:Hide()
    elseif region and region.SetAlpha then
        region:SetAlpha(0)
    end
end

local function HideNamedTexture(owner, suffix)
    local name = owner and owner.GetName and owner:GetName()
    if name then
        HideRegion(_G[name .. suffix])
    end
end

local function HideTextureRegions(frame)
    if not frame or not frame.GetRegions then
        return
    end

    for _, region in ipairs({ frame:GetRegions() }) do
        if region and region.GetObjectType and region:GetObjectType() == "Texture" then
            region:SetAlpha(0)
        end
    end
end

local function EnsureBackdrop(frame, bg, border)
    if not frame or not frame.SetBackdrop then
        return
    end
    frame:SetBackdrop({
        bgFile = BLANK,
        edgeFile = BLANK,
        edgeSize = 1,
        insets = { left = 1, right = 1, top = 1, bottom = 1 },
    })
    frame:SetBackdropColor(bg[1], bg[2], bg[3], bg[4])
    frame:SetBackdropBorderColor(border[1], border[2], border[3], border[4])
end

local function SetFontStringColor(fontString, color)
    if fontString and fontString.SetTextColor then
        fontString:SetTextColor(color[1], color[2], color[3], color[4])
    end
end

local function GetButtonText(button)
    return button and button.GetFontString and button:GetFontString() or (button and button.Text) or nil
end

local function SetButtonLineBorder(button, color)
    if not button then
        return
    end

    if not button.mcdLineBorder then
        local lines = {}
        local left = button:CreateTexture(nil, "OVERLAY")
        left:SetPoint("TOPLEFT", button, "TOPLEFT", 1, -1)
        left:SetPoint("BOTTOMLEFT", button, "BOTTOMLEFT", 1, 1)
        left:SetWidth(1)
        lines.left = left

        local right = button:CreateTexture(nil, "OVERLAY")
        right:SetPoint("TOPRIGHT", button, "TOPRIGHT", -1, -1)
        right:SetPoint("BOTTOMRIGHT", button, "BOTTOMRIGHT", -1, 1)
        right:SetWidth(1)
        lines.right = right

        local top = button:CreateTexture(nil, "OVERLAY")
        top:SetPoint("TOPLEFT", button, "TOPLEFT", 1, -1)
        top:SetPoint("TOPRIGHT", button, "TOPRIGHT", -1, -1)
        top:SetHeight(1)
        lines.top = top

        local bottom = button:CreateTexture(nil, "OVERLAY")
        bottom:SetPoint("BOTTOMLEFT", button, "BOTTOMLEFT", 1, 1)
        bottom:SetPoint("BOTTOMRIGHT", button, "BOTTOMRIGHT", -1, 1)
        bottom:SetHeight(1)
        lines.bottom = bottom

        button.mcdLineBorder = lines
    end

    for _, line in pairs(button.mcdLineBorder or {}) do
        SetTextureColor(line, color)
    end
end


function Skin:StyleFont(fontString, role)
    if not fontString then
        return fontString
    end

    local theme = self.Theme
    if role == "title" then
        ApplyFont(fontString, 21, "OUTLINE")
        SetFontStringColor(fontString, theme.gold)
    elseif role == "section" then
        ApplyFont(fontString, 15, "OUTLINE")
        SetFontStringColor(fontString, theme.gold)
    elseif role == "muted" then
        ApplyFont(fontString, 12, "")
        SetFontStringColor(fontString, theme.muted)
    elseif role == "accent" then
        ApplyFont(fontString, 14, "")
        SetFontStringColor(fontString, theme.gold)
    else
        ApplyFont(fontString, 14, "")
        SetFontStringColor(fontString, theme.text)
    end
    return fontString
end

function Skin:ApplyWindowChrome(frame, opts)
    if not frame then
        return
    end

    opts = opts or {}
    local theme = self.Theme
    local bg = opts.inset and theme.inset or (opts.panel and theme.panel or theme.bg)
    EnsureBackdrop(frame, bg, opts.borderColor or theme.softGold)

    if not frame.mcdChromeTop then
        local leftBorder = frame:CreateTexture(nil, "OVERLAY")
        leftBorder:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, 0)
        leftBorder:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 0, 0)
        leftBorder:SetWidth(1)
        frame.mcdChromeLeftBorder = leftBorder

        local rightBorder = frame:CreateTexture(nil, "OVERLAY")
        rightBorder:SetPoint("TOPRIGHT", frame, "TOPRIGHT", 0, 0)
        rightBorder:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", 0, 0)
        rightBorder:SetWidth(1)
        frame.mcdChromeRightBorder = rightBorder

        local topBorder = frame:CreateTexture(nil, "OVERLAY")
        topBorder:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, 0)
        topBorder:SetPoint("TOPRIGHT", frame, "TOPRIGHT", 0, 0)
        topBorder:SetHeight(1)
        frame.mcdChromeTopBorder = topBorder

        local bottomBorder = frame:CreateTexture(nil, "OVERLAY")
        bottomBorder:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 0, 0)
        bottomBorder:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", 0, 0)
        bottomBorder:SetHeight(1)
        frame.mcdChromeBottomBorder = bottomBorder

        local top = frame:CreateTexture(nil, "BORDER")
        top:SetPoint("TOPLEFT", frame, "TOPLEFT", 2, -2)
        top:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -2, -2)
        top:SetHeight(opts.topHeight or 34)
        frame.mcdChromeTop = top

        local topLine = frame:CreateTexture(nil, "ARTWORK")
        topLine:SetPoint("TOPLEFT", frame, "TOPLEFT", 12, -42)
        topLine:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -12, -42)
        topLine:SetHeight(1)
        frame.mcdChromeTopLine = topLine

        local bottom = frame:CreateTexture(nil, "BORDER")
        bottom:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 2, 2)
        bottom:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -2, 2)
        bottom:SetHeight(opts.footerHeight or 44)
        frame.mcdChromeBottom = bottom

        local footerGlow = frame:CreateTexture(nil, "ARTWORK")
        footerGlow:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 2, 2)
        footerGlow:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -2, 2)
        footerGlow:SetHeight(opts.footerHeight or 44)
        frame.mcdChromeFooterGlow = footerGlow

        local bottomLine = frame:CreateTexture(nil, "ARTWORK")
        bottomLine:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 12, (opts.footerHeight or 44) + 1)
        bottomLine:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -12, (opts.footerHeight or 44) + 1)
        bottomLine:SetHeight(1)
        frame.mcdChromeBottomLine = bottomLine

        local innerBottomLine = frame:CreateTexture(nil, "ARTWORK")
        innerBottomLine:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 12, (opts.footerHeight or 44) - 1)
        innerBottomLine:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -12, (opts.footerHeight or 44) - 1)
        innerBottomLine:SetHeight(1)
        frame.mcdChromeInnerBottomLine = innerBottomLine
    end

    local border = opts.borderColor or theme.softGold
    SetTextureColor(frame.mcdChromeLeftBorder, border)
    SetTextureColor(frame.mcdChromeRightBorder, border)
    SetTextureColor(frame.mcdChromeTopBorder, border)
    SetTextureColor(frame.mcdChromeBottomBorder, border)
    SetTextureColor(frame.mcdChromeTop, opts.inset and { 1, 0.82, 0.18, 0.03 } or { 1, 0.82, 0.18, 0.07 })
    SetTextureColor(frame.mcdChromeTopLine, opts.noTopLine and { 0, 0, 0, 0 } or { 0.85, 0.70, 0.18, 0.36 })
    SetTextureColor(frame.mcdChromeBottom, opts.inset and { 0, 0, 0, 0 } or { 1, 1, 1, 0.035 })
    SetTextureColor(frame.mcdChromeFooterGlow, opts.noFooter and { 0, 0, 0, 0 } or { 1, 0.82, 0.18, 0.045 })
    SetTextureColor(frame.mcdChromeBottomLine, opts.noFooter and { 0, 0, 0, 0 } or { 0.85, 0.70, 0.18, 0.20 })
    SetTextureColor(frame.mcdChromeInnerBottomLine, opts.noFooter and { 0, 0, 0, 0 } or { 1, 1, 1, 0.05 })
end

function Skin:SkinButton(button, variant)
    if not button then
        return button
    end

    variant = variant or "secondary"
    local theme = self.Theme
    button:SetNormalTexture(BLANK)
    button:SetHighlightTexture(BLANK)
    button:SetPushedTexture(BLANK)
    button:SetDisabledTexture(BLANK)
    local normal = button:GetNormalTexture()
    local highlight = button:GetHighlightTexture()
    local pushed = button:GetPushedTexture()
    local disabled = button:GetDisabledTexture()
    if normal then normal:SetAllPoints() end
    if highlight then highlight:SetAllPoints() end
    if pushed then pushed:SetAllPoints() end
    if disabled then disabled:SetAllPoints() end

    local border = CreateFrame("Frame", nil, button, "BackdropTemplate")
    -- 边框向内缩 1 像素，避免在缩放/贴边时右侧金边被裁掉。
    border:SetPoint("TOPLEFT", button, "TOPLEFT", 1, -1)
    border:SetPoint("BOTTOMRIGHT", button, "BOTTOMRIGHT", -1, 1)
    border:SetBackdrop({
        edgeFile = BLANK,
        edgeSize = 1,
    })
    button.mcdBorder = border

    button.mcdVariant = variant
    SetTextureColor(normal, variant == "primary" and { 0.22, 0.16, 0.05, 0.95 } or { 0.10, 0.10, 0.10, 0.94 })
    SetTextureColor(highlight, variant == "danger" and theme.danger or theme.hover)
    SetTextureColor(pushed, { 0.38, 0.28, 0.08, 0.88 })
    SetTextureColor(disabled, { 0.08, 0.08, 0.08, 0.72 })
    local borderColor = variant == "primary" and { 1, 0.82, 0.18, 0.65 } or theme.softGold
    border:SetBackdropBorderColor(borderColor[1], borderColor[2], borderColor[3], borderColor[4])
    if border.SetFrameLevel and button.GetFrameLevel then
        border:SetFrameLevel((button:GetFrameLevel() or 0) + 2)
    end
    SetButtonLineBorder(button, borderColor)

    local text = GetButtonText(button)
    self:StyleFont(text, variant == "primary" and "accent" or "body")
    if text then
        text:SetTextColor(1.0, 0.88, 0.42, 1)
    end
    button:SetDisabledFontObject(GameFontDisableSmall)

    if not button.mcdSkinHooked then
        button:HookScript("OnDisable", function(selfButton)
            local fs = GetButtonText(selfButton)
            SetFontStringColor(fs, theme.muted)
            if selfButton.mcdBorder then
                selfButton.mcdBorder:SetBackdropBorderColor(0.45, 0.42, 0.34, 0.35)
            end
            SetButtonLineBorder(selfButton, { 0.45, 0.42, 0.34, 0.35 })
        end)
        button:HookScript("OnEnable", function(selfButton)
            local fs = GetButtonText(selfButton)
            if fs then
                fs:SetTextColor(1.0, 0.88, 0.42, 1)
            end
            if selfButton.mcdBorder then
                local isPrimary = selfButton.mcdVariant == "primary"
                selfButton.mcdBorder:SetBackdropBorderColor(1, 0.82, 0.18, isPrimary and 0.65 or 0.50)
                SetButtonLineBorder(selfButton, { 1, 0.82, 0.18, isPrimary and 0.65 or 0.50 })
            end
        end)
        button.mcdSkinHooked = true
    end
    return button
end

function Skin:SkinCloseButton(button)
    if not button then
        return button
    end

    HideNamedTexture(button, "NormalTexture")
    HideNamedTexture(button, "PushedTexture")
    HideNamedTexture(button, "HighlightTexture")
    HideNamedTexture(button, "DisabledTexture")
    for _, region in ipairs({ button:GetRegions() }) do
        if region and region.GetObjectType and region:GetObjectType() == "Texture" then
            region:SetAlpha(0)
        end
    end

    local bg = button:CreateTexture(nil, "BACKGROUND")
    bg:SetAllPoints()
    bg:SetColorTexture(0.08, 0.08, 0.08, 0.95)
    local border = CreateFrame("Frame", nil, button, "BackdropTemplate")
    border:SetAllPoints()
    border:SetBackdrop({
        edgeFile = BLANK,
        edgeSize = 1,
    })
    border:SetBackdropBorderColor(0.85, 0.70, 0.18, 0.45)
    local x = button:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    x:SetPoint("CENTER", button, "CENTER", 0, 1)
    self:StyleFont(x, "accent")
    x:SetText("X")

    button.mcdCloseBorder = border
    button.mcdCloseText = x
    button:HookScript("OnEnter", function(selfButton)
        selfButton.mcdCloseBorder:SetBackdropBorderColor(1, 0.82, 0.18, 0.80)
        selfButton.mcdCloseText:SetTextColor(1, 0.95, 0.52, 1)
    end)
    button:HookScript("OnLeave", function(selfButton)
        selfButton.mcdCloseBorder:SetBackdropBorderColor(0.85, 0.70, 0.18, 0.45)
        selfButton.mcdCloseText:SetTextColor(1, 0.82, 0.18, 1)
    end)
    return button
end

function Skin:SkinEditBox(editBox)
    if not editBox then
        return editBox
    end

    HideNamedTexture(editBox, "Left")
    HideNamedTexture(editBox, "Middle")
    HideNamedTexture(editBox, "Right")
    HideTextureRegions(editBox)
    EnsureBackdrop(editBox, self.Theme.inset, self.Theme.softGold)
    editBox:SetTextColor(0.92, 0.92, 0.92, 1)
    editBox:SetHighlightColor(1, 0.82, 0.18, 0.20)
    ApplyFont(editBox, 14, "")

    if not editBox.mcdSkinHooked then
        editBox:HookScript("OnEditFocusGained", function(selfBox)
            if selfBox.SetBackdropBorderColor then
                selfBox:SetBackdropBorderColor(1, 0.82, 0.18, 0.85)
            end
        end)
        editBox:HookScript("OnEditFocusLost", function(selfBox)
            if selfBox.SetBackdropBorderColor then
                selfBox:SetBackdropBorderColor(0.85, 0.70, 0.18, 0.55)
            end
        end)
        editBox.mcdSkinHooked = true
    end
    return editBox
end

function Skin:SkinCheckButton(checkButton)
    if not checkButton then
        return checkButton
    end

    local label = checkButton.mcdLabel or checkButton.Text or checkButton.text
    checkButton.Text = label
    checkButton.text = label
    checkButton.mcdLabel = label
    self:StyleFont(label, "accent")
    if label then
        label:ClearAllPoints()
        label:SetPoint("LEFT", checkButton, "LEFT", 30, 0)
        label:SetJustifyH("LEFT")
        label:SetJustifyV("MIDDLE")
        label:SetTextColor(1.00, 0.82, 0.18, 1)
    end

    if not checkButton.mcdCheckBox then
        local box = CreateFrame("Frame", nil, checkButton, "BackdropTemplate")
        box:SetPoint("LEFT", checkButton, "LEFT", 5, 0)
        box:SetSize(18, 18)
        box:SetBackdrop({
            bgFile = BLANK,
            edgeFile = BLANK,
            edgeSize = 1,
        })
        checkButton.mcdCheckBox = box

        local hover = checkButton:CreateTexture(nil, "HIGHLIGHT")
        hover:SetPoint("TOPLEFT", box, "TOPLEFT", 0, 0)
        hover:SetPoint("BOTTOMRIGHT", box, "BOTTOMRIGHT", 0, 0)
        hover:SetColorTexture(1, 0.82, 0.18, 0.12)
        checkButton.mcdCheckHover = hover

        local markA = box:CreateTexture(nil, "OVERLAY")
        markA:SetColorTexture(1, 0.82, 0.18, 1)
        markA:SetSize(3, 7)
        markA:SetPoint("CENTER", box, "CENTER", -4, -2)
        markA:SetRotation(0.78)
        checkButton.mcdCheckMarkA = markA

        local markB = box:CreateTexture(nil, "OVERLAY")
        markB:SetColorTexture(1, 0.82, 0.18, 1)
        markB:SetSize(3, 13)
        markB:SetPoint("CENTER", box, "CENTER", 3, 1)
        markB:SetRotation(-0.62)
        checkButton.mcdCheckMarkB = markB
    end

    local function UpdateCheckVisual(selfButton)
        local enabled = selfButton:IsEnabled()
        local checkedNow = selfButton:GetChecked() == true
        local box = selfButton.mcdCheckBox
        local markA = selfButton.mcdCheckMarkA
        local markB = selfButton.mcdCheckMarkB
        if box then
            if enabled then
                box:SetBackdropColor(0.02, 0.02, 0.02, 0.90)
                box:SetBackdropBorderColor(0.85, 0.70, 0.18, checkedNow and 0.85 or 0.55)
            else
                box:SetBackdropColor(0.05, 0.05, 0.05, 0.55)
                box:SetBackdropBorderColor(0.45, 0.42, 0.34, 0.35)
            end
        end
        if markA then
            markA:SetShown(checkedNow)
            markA:SetAlpha(enabled and 1 or 0.45)
        end
        if markB then
            markB:SetShown(checkedNow)
            markB:SetAlpha(enabled and 1 or 0.45)
        end
        local textLabel = selfButton.mcdLabel or selfButton.Text or selfButton.text
        if textLabel then
            self:StyleFont(textLabel, "accent")
            if enabled then
                textLabel:SetTextColor(1.00, 0.82, 0.18, 1)
            else
                textLabel:SetTextColor(0.50, 0.50, 0.50, 1)
            end
        end
    end

    UpdateCheckVisual(checkButton)
    if not checkButton.mcdCheckHooked then
        checkButton:HookScript("OnClick", UpdateCheckVisual)
        checkButton:HookScript("OnShow", UpdateCheckVisual)
        checkButton:HookScript("OnEnable", UpdateCheckVisual)
        checkButton:HookScript("OnDisable", UpdateCheckVisual)
        checkButton:HookScript("OnShow", UpdateCheckVisual)
        if hooksecurefunc then
            hooksecurefunc(checkButton, "SetChecked", UpdateCheckVisual)
        end
        checkButton.mcdCheckHooked = true
    end
    return checkButton
end

function Skin:SkinDropDown(dropdown)
    if not dropdown then
        return dropdown
    end

    for _, suffix in ipairs({ "Left", "Middle", "Right", "ButtonNormalTexture", "ButtonDisabledTexture", "ButtonPushedTexture", "ButtonHighlightTexture" }) do
        HideNamedTexture(dropdown, suffix)
    end

    EnsureBackdrop(dropdown, self.Theme.inset, self.Theme.softGold)
    dropdown:SetHeight(30)

    local name = dropdown.GetName and dropdown:GetName()
    local text = name and _G[name .. "Text"]
    self:StyleFont(text, "body")
    if text then
        text:ClearAllPoints()
        text:SetTextColor(0.92, 0.92, 0.92, 1)
        text:SetPoint("LEFT", dropdown, "LEFT", 12, 1)
        text:SetPoint("RIGHT", dropdown, "RIGHT", -34, 1)
    end

    local button = name and _G[name .. "Button"]
    if button then
        button:ClearAllPoints()
        button:SetPoint("RIGHT", dropdown, "RIGHT", -3, 0)
        button:SetSize(26, 24)
        for _, region in ipairs({ button:GetRegions() }) do
            if region and region.GetObjectType and region:GetObjectType() == "Texture" then
                region:SetAlpha(0)
            end
        end
        local arrow = button:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        arrow:SetPoint("CENTER", button, "CENTER", 0, 0)
        self:StyleFont(arrow, "accent")
        arrow:SetText("v")
        dropdown.mcdArrow = arrow
    end
    return dropdown
end

function Skin:SkinScrollBar(slider)
    if not slider then
        return slider
    end

    for _, region in ipairs({ slider:GetRegions() }) do
        if region and region.GetObjectType and region:GetObjectType() == "Texture" then
            region:SetAlpha(0)
        end
    end
    EnsureBackdrop(slider, { 0.02, 0.02, 0.02, 0.55 }, { 0.85, 0.70, 0.18, 0.20 })

    local thumb = slider:GetThumbTexture()
    if thumb then
        thumb:SetTexture(BLANK)
        thumb:SetSize(12, 34)
        thumb:SetVertexColor(1, 0.82, 0.18, 0.45)
    end

    local name = slider.GetName and slider:GetName()
    for _, suffix in ipairs({ "ScrollUpButton", "ScrollDownButton" }) do
        local button = name and _G[name .. suffix]
        if button then
            self:SkinButton(button, "secondary")
            button:SetSize(16, 16)
        end
    end
    return slider
end

function Skin:SkinSlider(slider)
    if not slider then
        return slider
    end

    for _, region in ipairs({ slider:GetRegions() }) do
        if region and region.GetObjectType and region:GetObjectType() == "Texture" then
            region:SetAlpha(0)
        end
    end
    local name = slider.GetName and slider:GetName()
    if name then
        for _, suffix in ipairs({ "Left", "Middle", "Right", "Text", "Low", "High" }) do
            HideRegion(_G[name .. suffix])
        end
    end
    EnsureBackdrop(slider, { 0.02, 0.02, 0.02, 0.76 }, self.Theme.softGold)
    local thumb = slider:GetThumbTexture()
    if thumb then
        thumb:SetTexture(BLANK)
        thumb:SetSize(10, 20)
        thumb:SetVertexColor(1, 0.82, 0.18, 0.75)
    end
    return slider
end

function Skin:SkinTabButton(button)
    if not button then
        return button
    end
    if button.mcdTabBG then
        return button
    end

    self:SkinButton(button, "secondary")
    local bg = button:CreateTexture(nil, "BACKGROUND", nil, 1)
    bg:SetAllPoints()
    button.mcdTabBG = bg

    local border = CreateFrame("Frame", nil, button, "BackdropTemplate")
    border:SetPoint("TOPLEFT", button, "TOPLEFT", 1, -1)
    border:SetPoint("BOTTOMRIGHT", button, "BOTTOMRIGHT", -1, 1)
    border:SetBackdrop({
        edgeFile = BLANK,
        edgeSize = 1,
    })
    button.mcdTabBorder = border

    local glow = button:CreateTexture(nil, "OVERLAY")
    glow:SetPoint("TOPLEFT", button, "TOPLEFT", 1, -1)
    glow:SetPoint("BOTTOMRIGHT", button, "BOTTOMRIGHT", -1, 1)
    button.mcdTabGlow = glow

    local bottom = button:CreateTexture(nil, "OVERLAY")
    bottom:SetPoint("BOTTOMLEFT", button, "BOTTOMLEFT", 1, 0)
    bottom:SetPoint("BOTTOMRIGHT", button, "BOTTOMRIGHT", -1, 0)
    bottom:SetHeight(2)
    button.mcdTabBottom = bottom
    return button
end

function Skin:SetTabSelected(button, selected)
    if not button then
        return
    end

    self:SkinTabButton(button)
    button:SetButtonState(selected and "PUSHED" or "NORMAL")
    if selected then
        button.mcdTabBG:SetColorTexture(0.42, 0.30, 0.08, 0.98)
        button.mcdTabBorder:SetBackdropBorderColor(1, 0.88, 0.30, 0.95)
        button.mcdTabGlow:SetColorTexture(1, 0.82, 0.18, 0.18)
        button.mcdTabBottom:SetColorTexture(1, 0.88, 0.30, 0.95)
    else
        button.mcdTabBG:SetColorTexture(0.07, 0.07, 0.07, 0.92)
        button.mcdTabBorder:SetBackdropBorderColor(0.85, 0.70, 0.18, 0.28)
        button.mcdTabGlow:SetColorTexture(0, 0, 0, 0)
        button.mcdTabBottom:SetColorTexture(0, 0, 0, 0)
    end

    local text = GetButtonText(button)
    if text then
        if selected then
            text:SetTextColor(1, 0.96, 0.52, 1)
        else
            text:SetTextColor(0.82, 0.74, 0.48, 1)
        end
    end
end

function Skin:CreateSectionHeader(parent, text)
    local frame = CreateFrame("Button", nil, parent)
    frame:SetHeight(24)
    if frame.SetFrameLevel and parent and parent.GetFrameLevel then
        frame:SetFrameLevel((parent:GetFrameLevel() or 0) + 3)
    end

    local label = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    label:SetPoint("CENTER", frame, "CENTER", 0, 0)
    self:StyleFont(label, "section")
    label:SetText(tostring(text or ""))
    frame.label = label

    local leftLine = frame:CreateTexture(nil, "OVERLAY")
    leftLine:SetHeight(1)
    leftLine:SetPoint("LEFT", frame, "LEFT", 4, 0)
    leftLine:SetPoint("RIGHT", label, "LEFT", -10, 0)
    leftLine:SetColorTexture(0.85, 0.70, 0.18, 0.62)
    frame.leftLine = leftLine

    local rightLine = frame:CreateTexture(nil, "OVERLAY")
    rightLine:SetHeight(1)
    rightLine:SetPoint("LEFT", label, "RIGHT", 10, 0)
    rightLine:SetPoint("RIGHT", frame, "RIGHT", -4, 0)
    rightLine:SetColorTexture(0.85, 0.70, 0.18, 0.62)
    frame.rightLine = rightLine

    local hover = frame:CreateTexture(nil, "HIGHLIGHT")
    hover:SetPoint("TOPLEFT", frame, "TOPLEFT", -4, -2)
    hover:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", 4, 2)
    hover:SetColorTexture(1, 0.82, 0.18, 0.06)
    return frame
end
