local addonName, _ns = ...
local AC = _G.ArenaCore
if not AC then return end

local UIL = AC.UILocalization
local V = AC.Vanity

local CUSTOM_FONT = "Interface\\AddOns\\ArenaCore\\Media\\Fonts\\arenacore.ttf"

local function ApplyAtlasCardBg(frame)
    if not frame then return end

    local COLORS = AC.COLORS or {}

    local fill = frame:CreateTexture(nil, "BACKGROUND", nil, 2)
    local bgColor = COLORS.HEADER_BG or {0.102, 0.102, 0.102, 1}
    fill:SetColorTexture(bgColor[1], bgColor[2], bgColor[3], bgColor[4] or 1)
    fill:SetPoint("TOPLEFT", 1, -1)
    fill:SetPoint("BOTTOMRIGHT", -1, 1)
    frame.__acFill = fill
    frame.__acFillIsGroupBox = true

    local borderFrame = CreateFrame("Frame", nil, frame)
    borderFrame:SetAllPoints(frame)

    local uniqueLevel = frame:GetFrameLevel() + 1
    if frame:GetParent() then
        uniqueLevel = uniqueLevel + (frame:GetFrameLevel() % 3)
    end
    borderFrame:SetFrameLevel(uniqueLevel)

    local purpleTop = borderFrame:CreateTexture(nil, "BORDER", nil, 1)
    purpleTop:SetColorTexture(0.545, 0.271, 1.000, 1)
    purpleTop:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, 0)
    purpleTop:SetPoint("TOPRIGHT", frame, "TOPRIGHT", 0, 0)
    purpleTop:SetHeight(1)
    if purpleTop.SetSnapToPixelGrid then purpleTop:SetSnapToPixelGrid(true) end
    if purpleTop.SetTexelSnappingBias then purpleTop:SetTexelSnappingBias(0) end

    local grayTop = borderFrame:CreateTexture(nil, "BORDER", nil, 0)
    grayTop:SetColorTexture(0.278, 0.278, 0.278, 0.9)
    grayTop:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, -1)
    grayTop:SetPoint("TOPRIGHT", frame, "TOPRIGHT", 0, -1)
    grayTop:SetHeight(1)

    local grayBottom = borderFrame:CreateTexture(nil, "BORDER", nil, 0)
    grayBottom:SetColorTexture(0.278, 0.278, 0.278, 0.9)
    grayBottom:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 0, 0)
    grayBottom:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", 0, 0)
    grayBottom:SetHeight(1)

    local grayLeft = borderFrame:CreateTexture(nil, "BORDER", nil, 0)
    grayLeft:SetColorTexture(0.278, 0.278, 0.278, 0.9)
    grayLeft:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, 0)
    grayLeft:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 0, 0)
    grayLeft:SetWidth(1)

    local grayRight = borderFrame:CreateTexture(nil, "BORDER", nil, 0)
    grayRight:SetColorTexture(0.278, 0.278, 0.278, 0.9)
    grayRight:SetPoint("TOPRIGHT", frame, "TOPRIGHT", 0, 0)
    grayRight:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", 0, 0)
    grayRight:SetWidth(1)

    frame.__acBorderFrame = borderFrame
end

local function ApplySurrenderSetting()
    local enabled = AC and AC.DB and AC.DB.profile and AC.DB.profile.moreFeatures and AC.DB.profile.moreFeatures.surrenderGGEnabled

    SlashCmdList.ACGG = nil
    _G.SLASH_ACGG1 = nil

    if enabled and SurrenderArena then
        _G.SLASH_ACGG1 = "/gg"
        SlashCmdList.ACGG = function()
            print("|cff8B45FFArenaCore:|r /gg command called - attempting surrender...")
            SurrenderArena()
        end
    elseif enabled and not SurrenderArena then
        print("|cffFF0000ArenaCore:|r Surrender enabled but SurrenderArena function not found")
    end
end

local function ShowFontReloadPopup(fontType)
    local popup = CreateFrame("Frame", "ArenaCoreFontReloadPopup", UIParent)
    popup:SetSize(400, 180)
    popup:SetPoint("CENTER")
    popup:SetFrameStrata("FULLSCREEN_DIALOG")
    popup:SetFrameLevel(9999)

    local bg = popup:CreateTexture(nil, "BACKGROUND")
    bg:SetAllPoints()
    bg:SetTexture("Interface\\AddOns\\ArenaCore\\Media\\button-hide.tga")
    bg:SetVertexColor(1, 0.2, 0.2, 1)

    local overlay = popup:CreateTexture(nil, "BORDER")
    overlay:SetAllPoints()
    overlay:SetColorTexture(0, 0, 0, 0.4)

    local title = popup:CreateFontString(nil, "OVERLAY")
    title:SetFont("Fonts\\FRIZQT__.TTF", 16, "")
    title:SetPoint("TOP", 0, -15)
    title:SetTextColor(1, 1, 1, 1)
    title:SetText(UIL and UIL:Get("MISC", "reload_required", "RELOAD REQUIRED") or "RELOAD REQUIRED")

    local message = popup:CreateFontString(nil, "OVERLAY")
    message:SetFont("Fonts\\FRIZQT__.TTF", 12, "")
    message:SetPoint("TOP", title, "BOTTOM", 0, -20)
    message:SetWidth(360)
    message:SetJustifyH("CENTER")

    local messageText = ""
    if fontType == "global" then
        messageText = UIL and UIL:Get("MISC", "reload_global_font", "Global font setting changed.\n\nYou must reload your UI for the font changes to fully take effect.\n\nClick OK to reload now, or Cancel to reload later.") or "Global font setting changed.\n\nYou must reload your UI for the font changes to fully take effect.\n\nClick OK to reload now, or Cancel to reload later."
    elseif fontType == "actionbar" then
        messageText = UIL and UIL:Get("MISC", "reload_actionbar_font", "Action bar font setting changed.\n\nYou must reload your UI for the font changes to fully take effect.\n\nClick OK to reload now, or Cancel to reload later.") or "Action bar font setting changed.\n\nYou must reload your UI for the font changes to fully take effect.\n\nClick OK to reload now, or Cancel to reload later."
    end
    message:SetText(messageText)

    local okBtn = AC:CreateTexturedButton(popup, 120, 35, UIL and UIL:Get("MISC", "ok_reload", "OK (Reload)") or "OK (Reload)", "button-save")
    okBtn:SetPoint("BOTTOM", -65, 15)
    okBtn:SetScript("OnClick", function()
        popup:Hide()
        ReloadUI()
    end)

    local cancelBtn = AC:CreateTexturedButton(popup, 120, 35, UIL and UIL:Get("MISC", "cancel", "Cancel") or "Cancel", "button-cancel")
    cancelBtn:SetPoint("BOTTOM", 65, 15)
    cancelBtn:SetScript("OnClick", function()
        popup:Hide()
    end)

    popup:Show()
end

local function ApplyGlobalFont(enabled, skipPopup)
    if enabled then
        if AC.GlobalFont then
            local fontPath = "Interface/AddOns/ArenaCore/Media/Fonts/arenacore.ttf"
            local useOutline = AC.DB.profile.moreFeatures.globalFontOutline ~= false
            local useShadow = AC.DB.profile.moreFeatures.globalFontShadow ~= false
            local fontFlags = useOutline and "OUTLINE" or ""

            AC.GlobalFont:UpdateSettings(fontPath, fontFlags, useShadow)
            AC.GlobalFont:Enable(false)
        end

        if AC.ApplyActionBarFonts then
            AC:ApplyActionBarFonts()
        end

        if not skipPopup then
            ShowFontReloadPopup("global")
        end
    else
        if AC.GlobalFont then
            AC.GlobalFont:Disable()
        end

        if AC.RevertActionBarFonts then
            AC:RevertActionBarFonts()
        end

        if not skipPopup then
            ShowFontReloadPopup("global")
        end
    end
end

local function ApplyActionBarFont(enabled, skipPopup)
    if enabled then
        if AC.GlobalFont then
            local fontPath = "Interface/AddOns/ArenaCore/Media/Fonts/arenacore.ttf"
            local useOutline = AC.DB.profile.moreFeatures.globalFontOutline ~= false
            local useShadow = AC.DB.profile.moreFeatures.globalFontShadow ~= false
            local fontFlags = useOutline and "OUTLINE" or ""

            AC.GlobalFont:UpdateSettings(fontPath, fontFlags, useShadow)
            AC.GlobalFont:Enable(true)
        end

        if not skipPopup then
            ShowFontReloadPopup("actionbar")
        end
    else
        if AC.GlobalFont then
            AC.GlobalFont:Disable()
        end

        if not skipPopup then
            ShowFontReloadPopup("actionbar")
        end
    end
end

local function ApplyToSingleActionButton(btn, scale, offsetX, offsetY)
    if not btn then return end
    local hk = btn.HotKey
    if not hk then return end

    local arenaCoreFontPath = "Interface\\AddOns\\ArenaCore\\Media\\Fonts\\arenacore.ttf"
    local baseSize = 10
    local newSize = math.max(1, math.floor(baseSize * scale))
    hk:SetFont(arenaCoreFontPath, newSize, "OUTLINE")

    hk:ClearAllPoints()
    hk:SetPoint("TOPRIGHT", btn, "TOPRIGHT", offsetX, offsetY)

    if not hk._AC_ABF_Hooked then
        local originalSetText = hk.SetText
        hk.SetText = function(self, text)
            originalSetText(self, text)

            local globalEnabled = _G.ArenaCoreDB and _G.ArenaCoreDB.profile and _G.ArenaCoreDB.profile.moreFeatures and _G.ArenaCoreDB.profile.moreFeatures.globalFontEnabled
            if not globalEnabled then return end

            local db = _G.ArenaCoreDB and _G.ArenaCoreDB.profile and _G.ArenaCoreDB.profile.actionBarFont
            if not db then return end

            local currentScale = (db.scale or 100) / 100
            local currentOffsetX = db.horizontal or 0
            local currentOffsetY = db.vertical or 0

            local newSize2 = math.max(1, math.floor(baseSize * currentScale))
            self:SetFont(arenaCoreFontPath, newSize2, "OUTLINE")
            self:ClearAllPoints()
            self:SetPoint("TOPRIGHT", btn, "TOPRIGHT", currentOffsetX, currentOffsetY)
        end
        hk._AC_ABF_Hooked = true
    end
end

function AC:ApplyActionBarFonts()
    local db = AC.DB and AC.DB.profile and AC.DB.profile.actionBarFont
    if not db then return end

    local scale = (db.scale or 100) / 100
    local offsetX = db.horizontal or 0
    local offsetY = db.vertical or 0

    local bars = {
        "ActionButton", "MultiBarBottomLeftButton", "MultiBarBottomRightButton",
        "MultiBarRightButton", "MultiBarLeftButton", "MultiBar5Button",
        "MultiBar6Button", "MultiBar7Button", "StanceButton"
    }

    for _, bar in pairs(bars) do
        for i = 1, 12 do
            local button = _G[bar .. i]
            if button then
                ApplyToSingleActionButton(button, scale, offsetX, offsetY)
            end
        end
    end
end

function AC:RevertActionBarFonts()
    local bars = {
        "ActionButton", "MultiBarBottomLeftButton", "MultiBarBottomRightButton",
        "MultiBarRightButton", "MultiBarLeftButton", "MultiBar5Button",
        "MultiBar6Button", "MultiBar7Button", "StanceButton"
    }

    for _, bar in pairs(bars) do
        for i = 1, 12 do
            local button = _G[bar .. i]
            if button and button.HotKey then
                local hk = button.HotKey
                local defaultFont = NumberFontNormal or GameFontNormalSmall
                if defaultFont then
                    local font, size, flags = defaultFont:GetFont()
                    hk:SetFont(font, size, flags)
                end
                hk:ClearAllPoints()
                hk:SetPoint("TOPRIGHT", button, "TOPRIGHT", 0, 0)
            end
        end
    end
end

function AC:SetupActionBarHook()
    if self.actionBarHookSetup then return end
    self.actionBarHookSetup = true

    local eventFrame = _G._AC_ABFont or CreateFrame("Frame")
    _G._AC_ABFont = eventFrame

    eventFrame:RegisterEvent("PLAYER_LOGIN")
    eventFrame:RegisterEvent("UPDATE_BINDINGS")

    eventFrame:SetScript("OnEvent", function(_, event)
        if event == "PLAYER_LOGIN" then
            local enabled = AC.DB and AC.DB.profile and AC.DB.profile.moreFeatures and AC.DB.profile.moreFeatures.globalFontEnabled
            if enabled then
                AC:ApplyActionBarFonts()
            end
        elseif event == "UPDATE_BINDINGS" then
            C_Timer.After(0.2, function()
                local enabled = AC.DB and AC.DB.profile and AC.DB.profile.moreFeatures and AC.DB.profile.moreFeatures.globalFontEnabled
                if enabled then
                    AC:ApplyActionBarFonts()
                end
            end)
        end
    end)

    if QuickKeybindFrame then
        QuickKeybindFrame:HookScript("OnHide", function()
            local enabled = AC.DB and AC.DB.profile and AC.DB.profile.moreFeatures and AC.DB.profile.moreFeatures.globalFontEnabled
            if enabled then
                AC:ApplyActionBarFonts()
            end
        end)
    end

    local enabled = AC.DB and AC.DB.profile and AC.DB.profile.moreFeatures and AC.DB.profile.moreFeatures.globalFontEnabled
    if enabled then
        AC:ApplyActionBarFonts()
    end
end

local function CreateMiscTabButton(parent, text, width, height)
    local COLORS = AC.COLORS or {}

    local btn = CreateFrame("Button", nil, parent)
    btn:SetSize(width or 90, height or 26)

    local bg = AC:CreateFlatTexture(btn, "BACKGROUND", 0, {0.06, 0.06, 0.06, 1}, 1)
    bg:SetAllPoints()
    btn.bg = bg

    local hover = AC:CreateFlatTexture(btn, "ARTWORK", 1, COLORS.BORDER_LIGHT or {0.278, 0.278, 0.278, 1}, 0.15)
    hover:SetAllPoints()
    hover:Hide()
    btn.hover = hover

    local glow = btn:CreateTexture(nil, "OVERLAY")
    glow:SetTexture("Interface\\AddOns\\ArenaCore\\Media\\UI\\tab-purple-matte.tga")
    glow:SetAllPoints()
    glow:SetBlendMode("ADD")
    glow:SetAlpha(0)
    btn.glow = glow

    local function CreateGlowLine()
        local t = btn:CreateTexture(nil, "OVERLAY")
        local c = COLORS.PRIMARY or {0.545, 0.271, 1.000, 1}
        t:SetColorTexture(c[1], c[2], c[3], 1)
        t:SetDrawLayer("OVERLAY")
        if t.SetSnapToPixelGrid then
            t:SetTexelSnappingBias(0)
            t:SetSnapToPixelGrid(false)
        end
        t:Hide()
        return t
    end

    local outlineTop = CreateGlowLine()
    outlineTop:SetPoint("TOPLEFT", btn, "TOPLEFT", 1, 0)
    outlineTop:SetPoint("TOPRIGHT", btn, "TOPRIGHT", -1, 0)
    outlineTop:SetHeight(1)

    local outlineBottom = CreateGlowLine()
    outlineBottom:SetPoint("BOTTOMLEFT", btn, "BOTTOMLEFT", 1, 1)
    outlineBottom:SetPoint("BOTTOMRIGHT", btn, "BOTTOMRIGHT", -1, 1)
    outlineBottom:SetHeight(1)

    local outlineLeft = CreateGlowLine()
    outlineLeft:SetPoint("TOPLEFT", btn, "TOPLEFT", 0, -1)
    outlineLeft:SetPoint("BOTTOMLEFT", btn, "BOTTOMLEFT", 0, 1)
    outlineLeft:SetWidth(1)

    local outlineRight = CreateGlowLine()
    outlineRight:SetPoint("TOPRIGHT", btn, "TOPRIGHT", 0, -1)
    outlineRight:SetPoint("BOTTOMRIGHT", btn, "BOTTOMRIGHT", 0, 1)
    outlineRight:SetWidth(1)

    btn.outlineTop = outlineTop
    btn.outlineBottom = outlineBottom
    btn.outlineLeft = outlineLeft
    btn.outlineRight = outlineRight

    local label = AC:CreateStyledText(btn, text or "", 11, (COLORS.TEXT_2 or {0.706, 0.706, 0.706, 1}), "OVERLAY", "")
    label:SetPoint("CENTER", 0, 0)
    btn.label = label

    btn.isSelected = false

    function btn:SetSelected(selected)
        self.isSelected = selected and true or false

        if self.isSelected then
            self.bg:SetColorTexture(0.11, 0.11, 0.11, 1)
            self.hover:Hide()
            self.outlineTop:Show()
            self.outlineBottom:Show()
            self.outlineLeft:Show()
            self.outlineRight:Show()
            if self.glow then
                self.glow:SetAlpha(0.22)
            end
            self.label:SetTextColor((COLORS.TEXT and COLORS.TEXT[1]) or 1, (COLORS.TEXT and COLORS.TEXT[2]) or 1, (COLORS.TEXT and COLORS.TEXT[3]) or 1, 1)
        else
            self.bg:SetColorTexture(0.06, 0.06, 0.06, 1)
            self.outlineTop:Hide()
            self.outlineBottom:Hide()
            self.outlineLeft:Hide()
            self.outlineRight:Hide()
            if self.glow then
                self.glow:SetAlpha(0)
            end
            self.label:SetTextColor((COLORS.TEXT_2 and COLORS.TEXT_2[1]) or 0.706, (COLORS.TEXT_2 and COLORS.TEXT_2[2]) or 0.706, (COLORS.TEXT_2 and COLORS.TEXT_2[3]) or 0.706, 1)
        end
    end

    btn:SetScript("OnEnter", function(selfBtn)
        if not selfBtn.isSelected then
            selfBtn.hover:Show()
            selfBtn.outlineTop:Show()
            selfBtn.outlineBottom:Show()
            selfBtn.outlineLeft:Show()
            selfBtn.outlineRight:Show()
            if selfBtn.glow then
                selfBtn.glow:SetAlpha(0.22)
            end
            selfBtn.label:SetTextColor((COLORS.TEXT and COLORS.TEXT[1]) or 1, (COLORS.TEXT and COLORS.TEXT[2]) or 1, (COLORS.TEXT and COLORS.TEXT[3]) or 1, 1)
        end
    end)

    btn:SetScript("OnLeave", function(selfBtn)
        if not selfBtn.isSelected then
            selfBtn.hover:Hide()
            selfBtn.outlineTop:Hide()
            selfBtn.outlineBottom:Hide()
            selfBtn.outlineLeft:Hide()
            selfBtn.outlineRight:Hide()
            if selfBtn.glow then
                selfBtn.glow:SetAlpha(0)
            end
            selfBtn.label:SetTextColor((COLORS.TEXT_2 and COLORS.TEXT_2[1]) or 0.706, (COLORS.TEXT_2 and COLORS.TEXT_2[2]) or 0.706, (COLORS.TEXT_2 and COLORS.TEXT_2[3]) or 0.706, 1)
        end
    end)

    return btn
end

local function ClearContainerChildren(container)
    if not container then return end
    local children = { container:GetChildren() }
    for _, child in ipairs(children) do
        if child then
            child:Hide()
            child:ClearAllPoints()
            child:SetParent(nil)
        end
    end
end

local function CreateMiscPage(container)
    if not container then return end

    if V and V.EnsureMottoStrip then
        V:EnsureMottoStrip(container)
    end

    local COLORS = AC.COLORS or {}

    local tabBar = CreateFrame("Frame", nil, container)
    tabBar:SetHeight(84)
    if tabBar.SetClipsChildren then
        tabBar:SetClipsChildren(false)
    end
    if container._mottoStrip then
        tabBar:SetPoint("TOPLEFT", container._mottoStrip, "BOTTOMLEFT", 12, -10)
        tabBar:SetPoint("TOPRIGHT", container._mottoStrip, "BOTTOMRIGHT", -12, -10)
    else
        tabBar:SetPoint("TOPLEFT", 12, -12)
        tabBar:SetPoint("TOPRIGHT", -12, -12)
    end

    local tabBorder = AC:CreateFlatTexture(tabBar, "BACKGROUND", 1, COLORS.BORDER_LIGHT, 0.6)
    tabBorder:SetAllPoints()
    local tabFill = AC:CreateFlatTexture(tabBar, "BACKGROUND", 2, COLORS.INPUT_DARK, 1)
    tabFill:SetPoint("TOPLEFT", 1, -1)
    tabFill:SetPoint("BOTTOMRIGHT", -1, 1)
    tabBorder.__acSkipThemeRefresh = true
    tabFill.__acSkipThemeRefresh = true

    local contentRoot = CreateFrame("Frame", nil, container)
    contentRoot:SetPoint("TOPLEFT", tabBar, "BOTTOMLEFT", 0, -10)
    contentRoot:SetPoint("TOPRIGHT", tabBar, "BOTTOMRIGHT", 0, -10)
    contentRoot:SetPoint("BOTTOMLEFT", 0, 0)
    contentRoot:SetPoint("BOTTOMRIGHT", 0, 0)

    local pages = {
        { id = "general", text = UIL and UIL:Get("MISC", "tab_general", "General") or "General" },
        { id = "themes", text = UIL and UIL:Get("MISC", "tab_themes", "UI Themes") or "UI Themes" },
        { id = "font", text = UIL and UIL:Get("MISC", "tab_font", "Font") or "Font" },
        { id = "coming1", text = UIL and UIL:Get("MISC", "tab_coming_soon", "Coming Soon") or "Coming Soon" },
        { id = "coming2", text = UIL and UIL:Get("MISC", "tab_coming_soon", "Coming Soon") or "Coming Soon" },
        { id = "coming3", text = UIL and UIL:Get("MISC", "tab_coming_soon", "Coming Soon") or "Coming Soon" },
    }

    local activeTab = "general"
    local tabs = {}
    local contentFrames = {}

    local function LoadComingSoonContent(pageId, f)
        if not f then return end
        local title = AC:CreateStyledText(f, UIL and UIL:Get("MISC", "coming_soon_title", "Coming Soon...") or "Coming Soon...", 16, (COLORS.TEXT or {1, 1, 1, 1}), "OVERLAY", "")
        title:SetPoint("TOP", 0, -20)

        local desc = AC:CreateStyledText(f, UIL and UIL:Get("MISC", "coming_soon_desc", "This feature is planned for a future update.") or "This feature is planned for a future update.", 12, (COLORS.TEXT_2 or {0.706, 0.706, 0.706, 1}), "OVERLAY", "")
        desc:SetPoint("TOP", title, "BOTTOM", 0, -15)
        desc:SetJustifyH("CENTER")
    end

    local function LoadGeneralContent(f)
        local y = -12

        local sectionTitle = AC:CreateStyledText(f, UIL and UIL:Get("MISC", "general_settings", "GENERAL SETTINGS") or "GENERAL SETTINGS", 13, COLORS.PRIMARY, "OVERLAY", CUSTOM_FONT)
        sectionTitle:SetPoint("TOPLEFT", 15, y)

        y = y - 26

        local card = CreateFrame("Frame", nil, f)
        card:SetPoint("TOPLEFT", 12, y)
        card:SetPoint("TOPRIGHT", -12, y)
        card:SetHeight(180)
        ApplyAtlasCardBg(card)

        local cy = -8

        local function AddToggle(label, desc, checked, onChange, disabled)
            cy = cy - 32

            local row = CreateFrame("Frame", nil, card)
            row:SetPoint("TOPLEFT", 10, cy)
            row:SetPoint("TOPRIGHT", -10, cy)
            row:SetHeight(34)

            local labelText = AC:CreateStyledText(row, label, 12, disabled and COLORS.TEXT_MUTED or COLORS.TEXT_2, "OVERLAY", CUSTOM_FONT)
            labelText:SetPoint("TOPLEFT", 4, -2)

            local descText = AC:CreateStyledText(row, desc, 10, COLORS.TEXT_MUTED, "OVERLAY", CUSTOM_FONT)
            descText:SetPoint("TOPLEFT", labelText, "BOTTOMLEFT", 0, -2)
            descText:SetPoint("RIGHT", -36, 0)
            descText:SetJustifyH("LEFT")
            if descText.SetWordWrap then descText:SetWordWrap(true) end

            local checkbox = AC:CreateFlatCheckbox(row, 20, checked)
            checkbox:SetPoint("RIGHT", -6, 0)

            if disabled then
                checkbox.checkButton:Disable()
            elseif onChange then
                checkbox:SetScript("OnClick", onChange)
            end

            local sep = row:CreateTexture(nil, "BACKGROUND")
            sep:SetColorTexture(0.278, 0.278, 0.278, 0.2)
            sep:SetPoint("BOTTOMLEFT", 0, 0)
            sep:SetPoint("BOTTOMRIGHT", 0, 0)
            sep:SetHeight(1)

            cy = cy - 10
            return checkbox
        end

        -- Tooltip IDs (currently disabled)
        local dbTooltip = (AC.DB and AC.DB.profile and AC.DB.profile.tooltipIDs) or {}
        AddToggle(
            UIL and UIL:Get("MISC", "show_spell_item_ids", "Show Spell/Item IDs in Tooltips") or "Show Spell/Item IDs in Tooltips",
            UIL and UIL:Get("MISC", "tooltips_disabled", "Temporarily disabled while Blizzard resolves tooltip API issues.") or "Temporarily disabled while Blizzard resolves tooltip API issues.",
            dbTooltip.enabled ~= false,
            nil,
            true
        )

        -- Surrender /gg
        local dbSurr = (AC.DB and AC.DB.profile and AC.DB.profile.moreFeatures) or {}
        AddToggle(
            UIL and UIL:Get("MISC", "surrender_gg", "Surrender /gg") or "Surrender /gg",
            UIL and UIL:Get("MISC", "surrender_desc", "Type /gg in arena to instantly surrender the match.") or "Type /gg in arena to instantly surrender the match.",
            dbSurr.surrenderGGEnabled ~= false,
            function(selfBtn)
                local enabled = selfBtn:GetChecked()
                AC.DB = AC.DB or {}; AC.DB.profile = AC.DB.profile or {}; AC.DB.profile.moreFeatures = AC.DB.profile.moreFeatures or {}
                AC.DB.profile.moreFeatures.surrenderGGEnabled = enabled
                ApplySurrenderSetting()
            end
        )

        -- Arena Chat Announcements
        local dbChat = (AC.DB and AC.DB.profile and AC.DB.profile.moreFeatures) or {}
        AddToggle(
            UIL and UIL:Get("MISC", "arena_chat_announcements", "Arena Chat Announcements") or "Arena Chat Announcements",
            UIL and UIL:Get("MISC", "arena_chat_announcements_desc", "Announce interrupts and feign death detection in arena chat.") or "Announce interrupts and feign death detection in arena chat.",
            dbChat.chatMessagesEnabled ~= false,
            function(selfBtn)
                local enabled = selfBtn:GetChecked()
                AC.DB = AC.DB or {}; AC.DB.profile = AC.DB.profile or {}; AC.DB.profile.moreFeatures = AC.DB.profile.moreFeatures or {}
                AC.DB.profile.moreFeatures.chatMessagesEnabled = enabled
                if enabled then
                    print("|cff8B45FFArena Core:|r Chat messages |cff00FF00ENABLED|r - You will see interrupt and feign death announcements")
                else
                    print("|cff8B45FFArena Core:|r Chat messages |cffFF0000DISABLED|r - Interrupt and feign death announcements are now hidden")
                end
            end
        )
    end

    local function LoadThemesContent(f)
        local themes = {
            { id = "default", name = UIL and UIL:Get("MISC", "theme_default_name", "Arena Core Default") or "Arena Core Default", description = UIL and UIL:Get("MISC", "theme_default_desc", "The classic Arena Core theme") or "The classic Arena Core theme", rating = nil, icon = "Interface\\Icons\\INV_Misc_Gear_01" },
            { id = "shade_ui", name = UIL and UIL:Get("MISC", "theme_shade_ui_name", "Shade UI") or "Shade UI", description = UIL and UIL:Get("MISC", "theme_shade_ui_desc", "Premium dark theme inspired by Shade UI addon") or "Premium dark theme inspired by Shade UI addon", rating = 2100, icon = "Interface\\Icons\\Spell_Shadow_Twilight" },
            { id = "elite_2400", name = UIL and UIL:Get("MISC", "theme_elite_name", "Elite") or "Elite", description = UIL and UIL:Get("MISC", "theme_elite_desc", "Exclusive theme unlocked at 2400 rating") or "Exclusive theme unlocked at 2400 rating", rating = 2400, icon = "Interface\\Icons\\Achievement_Arena_2v2_7" },
            { id = "rank1_3000", name = UIL and UIL:Get("MISC", "theme_rank1_name", "Rank 1") or "Rank 1", description = UIL and UIL:Get("MISC", "theme_rank1_desc", "Exclusive theme unlocked at 3000 rating") or "Exclusive theme unlocked at 3000 rating", rating = 3000, icon = "Interface\\Icons\\Achievement_Arena_3v3_7" },
        }

        local settingsGroup = CreateFrame("Frame", nil, f)
        settingsGroup:SetPoint("TOPLEFT", 12, -12)
        settingsGroup:SetPoint("TOPRIGHT", -12, -12)
        settingsGroup:SetHeight(580)
        ApplyAtlasCardBg(settingsGroup)

        local groupTitle = AC:CreateStyledText(settingsGroup, UIL and UIL:Get("MISC", "unlockable_themes", "UNLOCKABLE THEMES") or "UNLOCKABLE THEMES", 13, COLORS.PRIMARY, "OVERLAY", CUSTOM_FONT)
        groupTitle:SetPoint("TOPLEFT", 15, -18)

        local productionNotice = AC:CreateStyledText(settingsGroup, UIL and UIL:Get("MISC", "themes_production_notice", "This feature is still under production and will evolve as time goes on.") or "This feature is still under production and will evolve as time goes on.", 10, COLORS.WARNING, "OVERLAY", CUSTOM_FONT)
        productionNotice:SetPoint("TOPLEFT", groupTitle, "BOTTOMLEFT", 0, -6)
        productionNotice:SetPoint("RIGHT", settingsGroup, "RIGHT", -20, 0)
        productionNotice:SetJustifyH("LEFT")
        if productionNotice.SetWordWrap then productionNotice:SetWordWrap(true) end

        local desc = AC:CreateStyledText(settingsGroup, UIL and UIL:Get("MISC", "unlockable_themes_desc", "Unlock exclusive UI themes by reaching rating milestones in PvP.") or "Unlock exclusive UI themes by reaching rating milestones in PvP.", 11, COLORS.TEXT_MUTED, "OVERLAY", CUSTOM_FONT)
        desc:SetPoint("TOPLEFT", productionNotice, "BOTTOMLEFT", 0, -6)
        desc:SetJustifyH("LEFT")

        local yOffset = -75

        local function RebuildSelf()
            ClearContainerChildren(f)
            LoadThemesContent(f)
            if AC and AC.UpdateContentHeight then
                C_Timer.After(0.05, function() AC:UpdateContentHeight() end)
            end
        end

        for _, theme in ipairs(themes) do
            local card = CreateFrame("Frame", nil, settingsGroup)
            card:SetPoint("TOPLEFT", 15, yOffset)
            card:SetPoint("TOPRIGHT", -15, yOffset)
            card:SetHeight(100)

            local isUnlocked = (theme.id == "default") or (AC.IsThemeUnlocked and AC:IsThemeUnlocked(theme.id)) or false

            local cardBg = AC:CreateFlatTexture(card, "BACKGROUND", 1, COLORS.INPUT_DARK, 1)
            cardBg:SetAllPoints()

            local borderColor = isUnlocked and COLORS.PRIMARY or COLORS.BORDER
            local cardBorder = AC:CreateFlatTexture(card, "BORDER", 0, borderColor, 1)
            cardBorder:SetAllPoints()

            local cardInner = AC:CreateFlatTexture(card, "BORDER", 1, COLORS.INPUT_DARK, 1)
            cardInner:SetPoint("TOPLEFT", 2, -2)
            cardInner:SetPoint("BOTTOMRIGHT", -2, 2)

            local icon = card:CreateTexture(nil, "ARTWORK")
            icon:SetSize(64, 64)
            icon:SetPoint("LEFT", 15, 0)
            icon:SetTexture(theme.icon)

            if not isUnlocked then
                icon:SetDesaturated(true)
                icon:SetAlpha(0.5)
            end

            local nameColor = isUnlocked and COLORS.PRIMARY or COLORS.TEXT_MUTED
            local themeName = AC:CreateStyledText(card, theme.name, 14, nameColor, "OVERLAY", CUSTOM_FONT)
            themeName:SetPoint("TOPLEFT", icon, "TOPRIGHT", 15, -5)

            local ratingLabel = theme.rating and string.format(UIL and UIL:Get("MISC", "requires_rating", "Requires %d Rating") or "Requires %d Rating", theme.rating) or (UIL and UIL:Get("MISC", "always_available", "Always Available") or "Always Available")
            local ratingText = AC:CreateStyledText(card, ratingLabel, 11, COLORS.TEXT_2, "OVERLAY", CUSTOM_FONT)
            ratingText:SetPoint("TOPLEFT", themeName, "BOTTOMLEFT", 0, -4)

            local themeDesc = AC:CreateStyledText(card, theme.description, 11, COLORS.TEXT_MUTED, "OVERLAY", CUSTOM_FONT)
            themeDesc:SetPoint("TOPLEFT", ratingText, "BOTTOMLEFT", 0, -6)
            themeDesc:SetPoint("TOPRIGHT", card, "TOPRIGHT", -120, 0)
            themeDesc:SetJustifyH("LEFT")
            if themeDesc.SetWordWrap then themeDesc:SetWordWrap(true) end

            local btn = AC:CreateProfileStyleButton(card, 100, 32, "", "primary", 12)
            btn:SetPoint("RIGHT", -15, 0)

            if isUnlocked then
                local isActive = AC.ThemeManager and AC.ThemeManager:IsThemeActive(theme.id) or false

                if isActive then
                    local btnText = AC:CreateStyledText(btn, UIL and UIL:Get("MISC", "active", "ACTIVE") or "ACTIVE", 12, COLORS.TEXT, "OVERLAY", CUSTOM_FONT)
                    btnText:SetPoint("CENTER", 0, 0)
                    btnText:SetTextColor(0.133, 0.667, 0.267, 1)
                    btn:SetAlpha(1.0)
                    btn:Disable()
                else
                    local btnText = AC:CreateStyledText(btn, UIL and UIL:Get("MISC", "activate", "ACTIVATE") or "ACTIVATE", 12, COLORS.TEXT, "OVERLAY", CUSTOM_FONT)
                    btnText:SetPoint("CENTER", 0, 0)
                    btnText:SetTextColor(1, 1, 1, 1)
                    btn:SetAlpha(1.0)
                    btn:Enable()
                    btn:SetScript("OnClick", function()
                        if AC.ThemeManager then
                            AC.ThemeManager:ApplyTheme(theme.id)
                            C_Timer.After(0, RebuildSelf)
                        end
                    end)
                end
            else
                local lockIcon = btn:CreateTexture(nil, "OVERLAY")
                lockIcon:SetSize(20, 20)
                lockIcon:SetPoint("CENTER", -24, 0)
                lockIcon:SetTexture("Interface\\AddOns\\ArenaCore\\Media\\Achievements\\padlock_icon.png")

                local btnText = AC:CreateStyledText(btn, UIL and UIL:Get("MISC", "locked", "LOCKED") or "LOCKED", 12, COLORS.TEXT, "OVERLAY", CUSTOM_FONT)
                btnText:SetPoint("CENTER", 10, 0)
                btnText:SetTextColor(0.863, 0.176, 0.176, 1)
                btn:SetAlpha(0.5)
                btn:Disable()
            end

            yOffset = yOffset - 105
        end

        local infoText = AC:CreateStyledText(settingsGroup, UIL and UIL:Get("MISC", "themes_account_wide", "Themes are unlocked account-wide when you earn the achievement on any character.") or "Themes are unlocked account-wide when you earn the achievement on any character.", 11, COLORS.TEXT_MUTED, "OVERLAY", CUSTOM_FONT)
        infoText:SetPoint("BOTTOMLEFT", settingsGroup, "BOTTOMLEFT", 15, 12)
        infoText:SetPoint("BOTTOMRIGHT", settingsGroup, "BOTTOMRIGHT", -15, 12)
        infoText:SetJustifyH("CENTER")
        infoText:SetJustifyV("BOTTOM")
        if infoText.SetWordWrap then infoText:SetWordWrap(true) end
    end

    local function LoadFontContent(f)
        local settingsGroup = CreateFrame("Frame", nil, f)
        settingsGroup:SetPoint("TOPLEFT", 12, -12)
        settingsGroup:SetPoint("TOPRIGHT", -12, -12)
        settingsGroup:SetHeight(620)
        ApplyAtlasCardBg(settingsGroup)

        local groupTitle = AC:CreateStyledText(settingsGroup, UIL and UIL:Get("MISC", "global_arenacore_font", "GLOBAL ARENACORE FONT") or "GLOBAL ARENACORE FONT", 13, COLORS.PRIMARY, "OVERLAY", CUSTOM_FONT)
        groupTitle:SetPoint("TOPLEFT", 15, -18)

        local checkboxRow1 = CreateFrame("Frame", nil, settingsGroup)
        checkboxRow1:SetPoint("TOPLEFT", groupTitle, "BOTTOMLEFT", 0, -14)
        checkboxRow1:SetPoint("RIGHT", settingsGroup, "RIGHT", -20, 0)
        checkboxRow1:SetHeight(26)

        local checkboxLabel1 = AC:CreateStyledText(checkboxRow1, UIL and UIL:Get("MISC", "apply_global_font", "Apply ArenaCore Font Globally") or "Apply ArenaCore Font Globally", 12, COLORS.TEXT_2, "OVERLAY", CUSTOM_FONT)
        checkboxLabel1:SetPoint("LEFT", 0, 0)
        checkboxLabel1:SetPoint("RIGHT", -46, 0)
        if checkboxLabel1.SetWordWrap then checkboxLabel1:SetWordWrap(true) end
        checkboxLabel1:SetJustifyH("LEFT")

        local dbGlobalFont = (AC.DB and AC.DB.profile and AC.DB.profile.moreFeatures) or {}
        local checkbox1 = AC:CreateFlatCheckbox(checkboxRow1, 20, (dbGlobalFont.globalFontEnabled == true))
        checkbox1:SetPoint("RIGHT", -10, 0)
        local globalFontCheckbox = checkbox1

        local description1 = AC:CreateStyledText(settingsGroup, UIL and UIL:Get("MISC", "apply_global_font_desc", "Applies ArenaCore font to your entire game UI including chat, tooltips, action bars, and combat text.") or "Applies ArenaCore font to your entire game UI including chat, tooltips, action bars, and combat text.", 11, COLORS.TEXT_MUTED, "OVERLAY", CUSTOM_FONT)
        description1:SetPoint("TOPLEFT", checkboxRow1, "BOTTOMLEFT", 0, -10)
        description1:SetPoint("TOPRIGHT", checkboxRow1, "BOTTOMRIGHT", 0, -10)
        description1:SetJustifyH("LEFT"); description1:SetJustifyV("TOP")
        if description1.SetWordWrap then description1:SetWordWrap(true) end
        description1:SetWidth(settingsGroup:GetWidth() - 40)

        local checkboxRow4 = CreateFrame("Frame", nil, settingsGroup)
        checkboxRow4:SetPoint("TOPLEFT", description1, "BOTTOMLEFT", 0, -18)
        checkboxRow4:SetPoint("RIGHT", settingsGroup, "RIGHT", -20, 0)
        checkboxRow4:SetHeight(26)

        local checkboxLabel4 = AC:CreateStyledText(checkboxRow4, UIL and UIL:Get("MISC", "action_bar_font_only", "Action Bar Font Only") or "Action Bar Font Only", 12, COLORS.TEXT_2, "OVERLAY", CUSTOM_FONT)
        checkboxLabel4:SetPoint("LEFT", 0, 0)
        checkboxLabel4:SetPoint("RIGHT", -46, 0)
        if checkboxLabel4.SetWordWrap then checkboxLabel4:SetWordWrap(true) end
        checkboxLabel4:SetJustifyH("LEFT")

        local dbActionBarFont = (AC.DB and AC.DB.profile and AC.DB.profile.moreFeatures) or {}
        local checkbox4 = AC:CreateFlatCheckbox(checkboxRow4, 20, (dbActionBarFont.actionBarFontOnly == true))
        checkbox4:SetPoint("RIGHT", -10, 0)
        local actionBarFontCheckbox = checkbox4

        local description4 = AC:CreateStyledText(settingsGroup, UIL and UIL:Get("MISC", "action_bar_font_only_desc", "Applies ArenaCore custom font to action bars ONLY and nothing else. This option is mutually exclusive with 'Apply ArenaCore Font Globally'.") or "Applies ArenaCore custom font to action bars ONLY and nothing else. This option is mutually exclusive with 'Apply ArenaCore Font Globally'.", 11, COLORS.TEXT_MUTED, "OVERLAY", CUSTOM_FONT)
        description4:SetPoint("TOPLEFT", checkboxRow4, "BOTTOMLEFT", 0, -10)
        description4:SetPoint("TOPRIGHT", checkboxRow4, "BOTTOMRIGHT", 0, -10)
        description4:SetJustifyH("LEFT"); description4:SetJustifyV("TOP")
        if description4.SetWordWrap then description4:SetWordWrap(true) end
        description4:SetWidth(settingsGroup:GetWidth() - 40)

        checkbox1:SetScript("OnClick", function(selfBtn)
            local enabled = selfBtn:GetChecked()
            AC.DB = AC.DB or {}; AC.DB.profile = AC.DB.profile or {}; AC.DB.profile.moreFeatures = AC.DB.profile.moreFeatures or {}
            AC.DB.profile.moreFeatures.globalFontEnabled = enabled

            if enabled and actionBarFontCheckbox then
                actionBarFontCheckbox:SetChecked(false)
                AC.DB.profile.moreFeatures.actionBarFontOnly = false
                if AC.GlobalFont and AC.GlobalFont.isEnabled then
                    AC.GlobalFont:Disable()
                end
            end

            ApplyGlobalFont(enabled)
            AC:SetupActionBarHook()
        end)

        checkbox4:SetScript("OnClick", function(selfBtn)
            local enabled = selfBtn:GetChecked()
            AC.DB = AC.DB or {}; AC.DB.profile = AC.DB.profile or {}; AC.DB.profile.moreFeatures = AC.DB.profile.moreFeatures or {}
            AC.DB.profile.moreFeatures.actionBarFontOnly = enabled

            if enabled and globalFontCheckbox then
                globalFontCheckbox:SetChecked(false)
                AC.DB.profile.moreFeatures.globalFontEnabled = false
                if AC.GlobalFont and AC.GlobalFont.isEnabled then
                    AC.GlobalFont:Disable()
                end
            end

            ApplyActionBarFont(enabled)
            AC:SetupActionBarHook()
        end)

        local checkboxRow2 = CreateFrame("Frame", nil, settingsGroup)
        checkboxRow2:SetPoint("TOPLEFT", description4, "BOTTOMLEFT", 0, -18)
        checkboxRow2:SetPoint("RIGHT", settingsGroup, "RIGHT", -20, 0)
        checkboxRow2:SetHeight(26)

        local checkboxLabel2 = AC:CreateStyledText(checkboxRow2, UIL and UIL:Get("MISC", "font_outline", "Font Outline") or "Font Outline", 12, COLORS.TEXT_2, "OVERLAY", CUSTOM_FONT)
        checkboxLabel2:SetPoint("LEFT", 0, 0)
        checkboxLabel2:SetPoint("RIGHT", -46, 0)
        if checkboxLabel2.SetWordWrap then checkboxLabel2:SetWordWrap(true) end
        checkboxLabel2:SetJustifyH("LEFT")

        local dbFontOutline = (AC.DB and AC.DB.profile and AC.DB.profile.moreFeatures) or {}
        local checkbox2 = AC:CreateFlatCheckbox(checkboxRow2, 20, (dbFontOutline.globalFontOutline ~= false))
        checkbox2:SetPoint("RIGHT", -10, 0)
        checkbox2:SetScript("OnClick", function(selfBtn)
            local enabled = selfBtn:GetChecked()
            AC.DB = AC.DB or {}; AC.DB.profile = AC.DB.profile or {}; AC.DB.profile.moreFeatures = AC.DB.profile.moreFeatures or {}
            AC.DB.profile.moreFeatures.globalFontOutline = enabled
            print("|cff8B45FFArena Core:|r Font outline " .. (enabled and "enabled" or "disabled") .. ". /reload to apply.")
        end)

        local description2 = AC:CreateStyledText(settingsGroup, UIL and UIL:Get("MISC", "font_outline_desc", "Adds a black outline around text for better visibility against varied backgrounds. Recommended for numbers and icons.") or "Adds a black outline around text for better visibility against varied backgrounds. Recommended for numbers and icons.", 11, COLORS.TEXT_MUTED, "OVERLAY", CUSTOM_FONT)
        description2:SetPoint("TOPLEFT", checkboxRow2, "BOTTOMLEFT", 0, -10)
        description2:SetPoint("TOPRIGHT", checkboxRow2, "BOTTOMRIGHT", 0, -10)
        description2:SetJustifyH("LEFT"); description2:SetJustifyV("TOP")
        if description2.SetWordWrap then description2:SetWordWrap(true) end
        description2:SetWidth(settingsGroup:GetWidth() - 40)

        local checkboxRow3 = CreateFrame("Frame", nil, settingsGroup)
        checkboxRow3:SetPoint("TOPLEFT", description2, "BOTTOMLEFT", 0, -18)
        checkboxRow3:SetPoint("RIGHT", settingsGroup, "RIGHT", -20, 0)
        checkboxRow3:SetHeight(26)

        local checkboxLabel3 = AC:CreateStyledText(checkboxRow3, UIL and UIL:Get("MISC", "font_shadow", "Font Shadow") or "Font Shadow", 12, COLORS.TEXT_2, "OVERLAY", CUSTOM_FONT)
        checkboxLabel3:SetPoint("LEFT", 0, 0)
        checkboxLabel3:SetPoint("RIGHT", -46, 0)
        if checkboxLabel3.SetWordWrap then checkboxLabel3:SetWordWrap(true) end
        checkboxLabel3:SetJustifyH("LEFT")

        local dbFontShadow = (AC.DB and AC.DB.profile and AC.DB.profile.moreFeatures) or {}
        local checkbox3 = AC:CreateFlatCheckbox(checkboxRow3, 20, (dbFontShadow.globalFontShadow ~= false))
        checkbox3:SetPoint("RIGHT", -10, 0)
        checkbox3:SetScript("OnClick", function(selfBtn)
            local enabled = selfBtn:GetChecked()
            AC.DB = AC.DB or {}; AC.DB.profile = AC.DB.profile or {}; AC.DB.profile.moreFeatures = AC.DB.profile.moreFeatures or {}
            AC.DB.profile.moreFeatures.globalFontShadow = enabled
            print("|cff8B45FFArena Core:|r Font shadow " .. (enabled and "enabled" or "disabled") .. ". /reload to apply.")
        end)

        local description3 = AC:CreateStyledText(settingsGroup, UIL and UIL:Get("MISC", "font_shadow_desc", "Adds a subtle drop shadow to text for improved depth and readability. Enabled by default for optimal visual clarity.") or "Adds a subtle drop shadow to text for improved depth and readability. Enabled by default for optimal visual clarity.", 11, COLORS.TEXT_MUTED, "OVERLAY", CUSTOM_FONT)
        description3:SetPoint("TOPLEFT", checkboxRow3, "BOTTOMLEFT", 0, -10)
        description3:SetPoint("TOPRIGHT", checkboxRow3, "BOTTOMRIGHT", 0, -10)
        description3:SetJustifyH("LEFT"); description3:SetJustifyV("TOP")
        if description3.SetWordWrap then description3:SetWordWrap(true) end
        description3:SetWidth(settingsGroup:GetWidth() - 40)

		local function EnsureActionBarFontDB()
			AC.DB = AC.DB or {}
			AC.DB.profile = AC.DB.profile or {}
			AC.DB.profile.actionBarFont = AC.DB.profile.actionBarFont or {}
			if AC.DB.profile.actionBarFont.scale == nil then AC.DB.profile.actionBarFont.scale = 100 end
			if AC.DB.profile.actionBarFont.horizontal == nil then AC.DB.profile.actionBarFont.horizontal = 0 end
			if AC.DB.profile.actionBarFont.vertical == nil then AC.DB.profile.actionBarFont.vertical = 0 end
			return AC.DB.profile.actionBarFont
		end

		local function IsFontModeEnabled()
			local db = AC.DB and AC.DB.profile and AC.DB.profile.moreFeatures
			if not db then return false end
			return db.globalFontEnabled == true or db.actionBarFontOnly == true
		end

		local function ApplyActionBarIfEnabled()
			if not IsFontModeEnabled() then return end
			if AC.ApplyActionBarFonts then
				AC:ApplyActionBarFonts()
			end
		end

		local function CreateActionBarSlider(parentFrame, label, y, minV, maxV, key, suffix)
			local row = CreateFrame("Frame", nil, parentFrame)
			row:SetPoint("TOPLEFT", 20, y)
			row:SetPoint("TOPRIGHT", -20, y)
			row:SetHeight(26)

			local l = AC:CreateStyledText(row, label, 12, COLORS.TEXT_2, "OVERLAY", CUSTOM_FONT)
			l:SetPoint("LEFT", 0, 0)
			l:SetWidth(110)
			l:SetJustifyH("LEFT")

			local valueText = AC:CreateStyledText(row, "", 11, COLORS.TEXT_2, "OVERLAY", CUSTOM_FONT)
			valueText:SetPoint("RIGHT", -2, 0)
			valueText:SetWidth(90)
			valueText:SetJustifyH("RIGHT")

			local db = EnsureActionBarFontDB()
			local startValue = tonumber(db[key]) or 0

			local function OnChange(value)
				local db2 = EnsureActionBarFontDB()
				db2[key] = value
				if suffix then
					valueText:SetText(string.format("%.0f%s", value, suffix))
				else
					valueText:SetText(string.format("%.0f", value))
				end
				ApplyActionBarIfEnabled()
			end

			local slider = AC:CreateFlatSlider(row, 150, 18, minV, maxV, startValue, false, OnChange)
			slider:SetPoint("LEFT", l, "RIGHT", 10, 0)
			slider:SetPoint("RIGHT", valueText, "LEFT", -10, 0)
			OnChange(startValue)
			return row
		end

		local actionBarCard = CreateFrame("Frame", nil, settingsGroup)
		actionBarCard:SetPoint("TOPLEFT", description3, "BOTTOMLEFT", 0, -18)
		actionBarCard:SetPoint("TOPRIGHT", description3, "BOTTOMRIGHT", 0, -18)
		actionBarCard:SetHeight(180)
		ApplyAtlasCardBg(actionBarCard)

		        local actionBarTitle = AC:CreateStyledText(actionBarCard, UIL and UIL:Get("MISC", "action_bar_hotkeys", "ACTION BAR HOTKEYS") or "ACTION BAR HOTKEYS", 13, COLORS.PRIMARY, "OVERLAY", CUSTOM_FONT)
		actionBarTitle:SetPoint("TOPLEFT", 15, -18)

		local actionBarDesc = AC:CreateStyledText(actionBarCard, UIL and UIL:Get("MISC", "action_bar_hotkeys_desc", "Fine-tune hotkey size and position. These apply when global font is enabled, or when Action Bar Font Only mode is enabled.") or "Fine-tune hotkey size and position. These apply when global font is enabled, or when Action Bar Font Only mode is enabled.", 11, COLORS.TEXT_MUTED, "OVERLAY", CUSTOM_FONT)
		actionBarDesc:SetPoint("TOPLEFT", actionBarTitle, "BOTTOMLEFT", 0, -10)
		actionBarDesc:SetPoint("TOPRIGHT", actionBarCard, "TOPRIGHT", -20, 0)
		actionBarDesc:SetJustifyH("LEFT")
		if actionBarDesc.SetWordWrap then actionBarDesc:SetWordWrap(true) end

		CreateActionBarSlider(actionBarCard, UIL and UIL:Get("MISC", "font_size", "Font Size") or "Font Size", -78, 50, 200, "scale", "%")
		CreateActionBarSlider(actionBarCard, UIL and UIL:Get("MISC", "horizontal", "Horizontal") or "Horizontal", -108, -30, 30, "horizontal", "")
		CreateActionBarSlider(actionBarCard, UIL and UIL:Get("MISC", "vertical", "Vertical") or "Vertical", -138, -30, 30, "vertical", "")
    end

    for _, page in ipairs(pages) do
        local f = CreateFrame("Frame", nil, contentRoot)
        f:SetPoint("TOPLEFT", 0, 0)
        f:SetPoint("TOPRIGHT", 0, 0)
        f:SetHeight(1)
        f:Hide()
        contentFrames[page.id] = f
    end

    local function EnsureTabContent(tabId)
        local f = contentFrames[tabId]
        if not f or f.__built then return end

        ClearContainerChildren(f)

        if tabId == "general" then
            LoadGeneralContent(f)
        elseif tabId == "themes" then
            LoadThemesContent(f)
        elseif tabId == "font" then
            LoadFontContent(f)
        else
            LoadComingSoonContent(tabId, f)
        end

        f.__built = true
    end

    local function SelectTab(tabId)
        activeTab = tabId

        for id, tab in pairs(tabs) do
            tab:SetSelected(id == tabId)
        end

        for id, f in pairs(contentFrames) do
            if id == tabId then
                f:Show()
            else
                f:Hide()
            end
        end

        EnsureTabContent(tabId)

        if _G.ArenaCore and _G.ArenaCore.contentArea then
            _G.ArenaCore.contentArea:SetVerticalScroll(0)
        end
        if _G.ArenaCore and _G.ArenaCore.scrollbar then
            _G.ArenaCore.scrollbar:SetValue(0)
        end

        if AC and AC.UpdateContentHeight then
            C_Timer.After(0.05, function()
                AC:UpdateContentHeight()
            end)
        end
    end

    local tabWidth = 90
    local tabHeight = 30
    local tabSpacing = 8

    tabWidth = 102

    local firstRow = { pages[1], pages[2], pages[3] }
    local secondRow = { pages[4], pages[5], pages[6] }

    local firstRowWidth = (#firstRow * tabWidth) + ((#firstRow - 1) * tabSpacing)
    local secondRowWidth = (#secondRow * tabWidth) + ((#secondRow - 1) * tabSpacing)

    local function PositionTabs()
        local tabBarWidth = tabBar:GetWidth()
        if not tabBarWidth or tabBarWidth <= 0 then
            tabBarWidth = container:GetWidth()
        end
        if not tabBarWidth or tabBarWidth <= 0 then
            tabBarWidth = 460
        end

        local firstRowStartX = (tabBarWidth - firstRowWidth) / 2
        local secondRowStartX = (tabBarWidth - secondRowWidth) / 2

        for i, page in ipairs(firstRow) do
            local tab = tabs[page.id]
            if tab then
                tab:ClearAllPoints()
                tab:SetPoint("TOPLEFT", tabBar, "TOPLEFT", firstRowStartX + ((i - 1) * (tabWidth + tabSpacing)), -8)
            end
        end

        for i, page in ipairs(secondRow) do
            local tab = tabs[page.id]
            if tab then
                tab:ClearAllPoints()
                tab:SetPoint("TOPLEFT", tabBar, "TOPLEFT", secondRowStartX + ((i - 1) * (tabWidth + tabSpacing)), -46)
            end
        end
    end

    for _, page in ipairs(firstRow) do
        local tab = CreateMiscTabButton(tabBar, page.text, tabWidth, tabHeight)
        tab:SetScript("OnClick", function()
            SelectTab(page.id)
        end)
        tabs[page.id] = tab
    end

    for _, page in ipairs(secondRow) do
        local tab = CreateMiscTabButton(tabBar, page.text, tabWidth, tabHeight)
        tab:SetScript("OnClick", function()
            SelectTab(page.id)
        end)
        tabs[page.id] = tab
    end

    PositionTabs()
    C_Timer.After(0, PositionTabs)

    SelectTab(activeTab)
end

AC:RegisterPage("Misc", CreateMiscPage)

AC.ApplySurrenderSetting = ApplySurrenderSetting
AC.ApplyGlobalFont = ApplyGlobalFont
AC.ApplyActionBarFont = ApplyActionBarFont

local initFrame = CreateFrame("Frame")
initFrame:RegisterEvent("PLAYER_LOGIN")
initFrame:SetScript("OnEvent", function()
    if not AC.actionBarHookSetup then
        AC:SetupActionBarHook()
    end
end)
