-- =============================================================
-- File: Core/MoreFeatures.lua
-- ArenaCore More Features Window
-- Smaller-scale UI for additional features
-- =============================================================

local AC = _G.ArenaCore
if not AC then return end

AC.MoreFeatures = AC.MoreFeatures or {}
local MoreFeatures = AC.MoreFeatures

-- UI Constants (smaller scale than main UI)
local WINDOW_WIDTH = 600
local WINDOW_HEIGHT = 400
local SIDEBAR_WIDTH = 140
local HEADER_HEIGHT = 50

-- Color scheme matching main UI exactly
local _COLOR_DEFAULTS = {
PRIMARY = {0.545, 0.271, 1.000, 1},
TEXT = {1.000, 1.000, 1.000, 1},
TEXT_2 = {0.706, 0.706, 0.706, 1},
TEXT_MUTED = {0.600, 0.600, 0.600, 1},
DANGER = {0.863, 0.176, 0.176, 1},
SUCCESS = {0.133, 0.667, 0.267, 1},
WARNING = {0.800, 0.533, 0.000, 1},
BG = {0.200, 0.200, 0.200, 1},
HEADER_BG = {0.200, 0.200, 0.200, 1},
INPUT_DARK = {0.200, 0.200, 0.200, 1},
GROUP_BG = {0.200, 0.200, 0.200, 1},
BORDER = {0.196, 0.196, 0.196, 1},
BORDER_LIGHT = {0.278, 0.278, 0.278, 1},
ICON_BG = {0.220, 0.220, 0.220, 1},
NAV_ACTIVE_BG = {0.200, 0.200, 0.200, 1},
NAV_INACTIVE_BG= {0.120, 0.120, 0.120, 1},
INSET = {0.090, 0.090, 0.090, 1},
}


local COLORS = setmetatable({}, {
__index = function(_, k)
local v
if AC and AC.COLORS then v = AC.COLORS[k] end
if v ~= nil then return v end
return _COLOR_DEFAULTS[k]
end
})

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

-- Feature pages data
local FEATURE_PAGES = {
    {id = "general", name = "General", icon = "Interface\\Icons\\INV_Misc_Gear_01"},
    {id = "themes", name = "Themes", icon = "Interface\\Icons\\INV_Misc_Paint_01"},
    {id = "font", name = "Font", icon = "Interface\\Icons\\INV_Inscription_Tradeskill01"},
    {id = "feature4", name = "Coming Soon...", icon = "Interface\\Icons\\INV_Misc_QuestionMark"},
    {id = "feature5", name = "Coming Soon...", icon = "Interface\\Icons\\INV_Misc_QuestionMark"},
    {id = "feature6", name = "Coming Soon...", icon = "Interface\\Icons\\INV_Misc_QuestionMark"},
    {id = "feature7", name = "Coming Soon...", icon = "Interface\\Icons\\INV_Misc_QuestionMark"},
    {id = "feature8", name = "Coming Soon...", icon = "Interface\\Icons\\INV_Misc_QuestionMark"},
}

-- Create the main window frame
function MoreFeatures:CreateWindow()
    if self.window then return self.window end
    
    -- Main window frame
    local window = CreateFrame("Frame", "ArenaCoreMoreFeaturesFrame", UIParent)
    window:SetSize(WINDOW_WIDTH, WINDOW_HEIGHT)
    window:SetPoint("CENTER", 0, 0)
    window:SetFrameStrata("DIALOG")
    window:SetFrameLevel(50)  -- FIXED: Reduced from 100 to 50 to prevent text overlapping other UI elements
    window:EnableMouse(true)
    window:SetMovable(true)
    window:SetClampedToScreen(true)
    window:RegisterForDrag("LeftButton")
    window:SetScript("OnDragStart", window.StartMoving)
    window:SetScript("OnDragStop", window.StopMovingOrSizing)
    
    -- CRITICAL FIX: Add OnHide handler to clean up page containers
    window:SetScript("OnHide", function()
        if MoreFeatures and MoreFeatures.pageContainers then
            for _, container in pairs(MoreFeatures.pageContainers) do
                if container then
                    container:Hide()
                end
            end
        end
    end)
    
    window:Hide()
    
    -- Window backdrop with proper border
    local border = AC:CreateFlatTexture(window, "BACKGROUND", 1, COLORS.BORDER, 1)
    border:SetAllPoints()
    
    local bg = AC:CreateFlatTexture(window, "BACKGROUND", 2, COLORS.BG, 1)
    bg:SetPoint("TOPLEFT", 1, -1)
    bg:SetPoint("BOTTOMRIGHT", -1, 1)
    
    -- Add window edge details like main UI
    if AC.AddWindowEdge then AC:AddWindowEdge(window, 1, 0) end

    
    self.window = window
    return window
end

-- Create the header section
function MoreFeatures:CreateHeader(parent)
    local header = CreateFrame("Frame", nil, parent)
    header:SetPoint("TOPLEFT", 2, -2)
    header:SetPoint("TOPRIGHT", -2, -2)
    header:SetHeight(HEADER_HEIGHT)
    
    
    
    
    local headerBg = AC:CreateFlatTexture(header, "BACKGROUND", 1, COLORS.HEADER_BG, 1)
    headerBg:SetAllPoints()
    
    -- Purple accent line like main UI
    local accent = AC:CreateFlatTexture(header, "OVERLAY", 3, COLORS.PRIMARY, 1)
    accent:SetPoint("TOPLEFT", 0, 0)
    accent:SetPoint("TOPRIGHT", 0, 0)
    accent:SetHeight(3)
    
    -- Hairline borders
    local hbLight = AC:CreateFlatTexture(header, "OVERLAY", 2, COLORS.BORDER_LIGHT, 0.8)
    hbLight:SetPoint("BOTTOMLEFT", 0, 0)
    hbLight:SetPoint("BOTTOMRIGHT", 0, 0)
    hbLight:SetHeight(1)
    
    local hbDark = AC:CreateFlatTexture(header, "OVERLAY", 1, COLORS.BORDER, 1)
    hbDark:SetPoint("BOTTOMLEFT", 0, 1)
    hbDark:SetPoint("BOTTOMRIGHT", 0, 1)
    hbDark:SetHeight(1)
    
    -- Title text with custom font
    local title = AC:CreateStyledText(header, "Arena Core - More Features", 16, COLORS.TEXT, "OVERLAY", CUSTOM_FONT)
    title:SetPoint("LEFT", 15, 0)
    
    -- Close button with red X texture like main UI
    local closeBtn = AC:CreateTexturedButton(header, 36, 36, "", "button-close")
    closeBtn:SetPoint("RIGHT", -10, 0)
    local xText = AC:CreateStyledText(closeBtn, "×", 18, COLORS.TEXT, "OVERLAY", CUSTOM_FONT)
    xText:SetPoint("CENTER", 0, 0)
    closeBtn._xText = xText
    closeBtn:SetScript("OnClick", function() 
        parent:Hide()
        -- CRITICAL FIX: Clear all page content when window closes to prevent text leaking to other frames
        if MoreFeatures and MoreFeatures.pageContainers then
            for pageId, container in pairs(MoreFeatures.pageContainers) do
                if container then
                    container:Hide()
                end
            end
        end
    end)
    
    return header
end

-- Create the sidebar navigation
function MoreFeatures:CreateSidebar(parent)
    local sidebar = CreateFrame("Frame", nil, parent)
    sidebar:SetPoint("TOPLEFT", 2, -HEADER_HEIGHT - 2)
    sidebar:SetPoint("BOTTOMLEFT", 2, 2)
    sidebar:SetWidth(SIDEBAR_WIDTH)
    
    local sidebarBg = AC:CreateFlatTexture(sidebar, "BACKGROUND", 1, COLORS.BG, 1)
    sidebarBg:SetAllPoints()
    
    -- Purple hairline accent on right edge like main UI
    local sAccent = AC:CreateFlatTexture(sidebar, "OVERLAY", 2, COLORS.BORDER_LIGHT, 0.6)
    sAccent:SetPoint("TOPRIGHT", -1, 0)
    sAccent:SetPoint("BOTTOMRIGHT", -1, 0)
    sAccent:SetWidth(1)
    
    -- Create navigation buttons
    self.navButtons = {}
    local buttonHeight = 35
    local buttonSpacing = 5
    
    for i, page in ipairs(FEATURE_PAGES) do
        local btn = CreateFrame("Button", nil, sidebar)
        btn:SetSize(SIDEBAR_WIDTH - 10, buttonHeight)
        btn:SetPoint("TOP", 0, -10 - (i - 1) * (buttonHeight + buttonSpacing))
        
        local bg = AC:CreateFlatTexture(btn, "BACKGROUND", 0, COLORS.NAV_INACTIVE_BG, 1)
        bg:SetAllPoints()
        
        -- Purple outline (like main UI)
        local outline = CreateFrame("Frame", nil, btn)
        outline:SetAllPoints()
        outline:Hide()
        
        local outlineTop = AC:CreateFlatTexture(outline, "OVERLAY", 3, COLORS.PRIMARY, 1)
        outlineTop:SetPoint("TOPLEFT", 0, 0)
        outlineTop:SetPoint("TOPRIGHT", 0, 0)
        outlineTop:SetHeight(2)
        
        local outlineBottom = AC:CreateFlatTexture(outline, "OVERLAY", 3, COLORS.PRIMARY, 1)
        outlineBottom:SetPoint("BOTTOMLEFT", 0, 0)
        outlineBottom:SetPoint("BOTTOMRIGHT", 0, 0)
        outlineBottom:SetHeight(2)
        
        local outlineLeft = AC:CreateFlatTexture(outline, "OVERLAY", 3, COLORS.PRIMARY, 1)
        outlineLeft:SetPoint("TOPLEFT", 0, 0)
        outlineLeft:SetPoint("BOTTOMLEFT", 0, 0)
        outlineLeft:SetWidth(2)
        
        local outlineRight = AC:CreateFlatTexture(outline, "OVERLAY", 3, COLORS.PRIMARY, 1)
        outlineRight:SetPoint("TOPRIGHT", 0, 0)
        outlineRight:SetPoint("BOTTOMRIGHT", 0, 0)
        outlineRight:SetWidth(2)
        
        local indicator = AC:CreateFlatTexture(btn, "OVERLAY", 2, COLORS.PRIMARY, 1)
        indicator:SetPoint("LEFT", -10, 0)
        indicator:SetSize(5, buttonHeight)
        indicator:Hide()
        
        local hover = AC:CreateFlatTexture(btn, "ARTWORK", 1, COLORS.BORDER_LIGHT, 0.15)
        hover:SetAllPoints()
        hover:Hide()
        
        btn.bg = bg
        btn.outline = outline
        btn.indicator = indicator
        btn.hover = hover
        
        -- Button text with custom font
        local text = AC:CreateStyledText(btn, page.name, 14, COLORS.TEXT_2, "OVERLAY", CUSTOM_FONT)
        text:SetPoint("CENTER", 0, 0)
        
        -- Button hover effects like main UI
        btn:SetScript("OnEnter", function(self)
            if not self.isSelected then
                self.hover:Show()
                self.indicator:SetColorTexture(COLORS.PRIMARY[1], COLORS.PRIMARY[2], COLORS.PRIMARY[3], 0.6)
                self.indicator:Show()
                self.text:SetTextColor(COLORS.TEXT[1], COLORS.TEXT[2], COLORS.TEXT[3], 1)
            end
        end)
        btn:SetScript("OnLeave", function(self)
            if not self.isSelected then
                self.hover:Hide()
                self.indicator:Hide()
                self.text:SetTextColor(COLORS.TEXT_2[1], COLORS.TEXT_2[2], COLORS.TEXT_2[3], 1)
            end
        end)
        
        -- Button click handler
        btn:SetScript("OnClick", function(self)
            MoreFeatures:SelectPage(page.id)
        end)
        
        btn.pageId = page.id
        btn.text = text
        self.navButtons[page.id] = btn
    end
    
    -- Store reference but don't select yet (will be done in ShowWindow)
    self.defaultPage = "general"
    
    return sidebar
end

-- Create the content area
function MoreFeatures:CreateContentArea(parent)
    local content = CreateFrame("ScrollFrame", nil, parent)
    content:SetPoint("TOPLEFT", SIDEBAR_WIDTH + 5, -HEADER_HEIGHT - 2)
    content:SetPoint("BOTTOMRIGHT", -5, 5)
    content:EnableMouseWheel(true)
-- (intentionally blank) -- removed redundant contentBg to eliminate the background rectangle
    -- Scroll child with dynamic height
    local child = CreateFrame("Frame", nil, content)
    child:SetWidth(450)
    child:SetHeight(1)
    content:SetScrollChild(child)

    -- Content box styled like Core/UI.lua
    local contentBox = CreateFrame("Frame", nil, child)
    contentBox:SetPoint("TOPLEFT", 2, 0)
    contentBox:SetPoint("TOPRIGHT", -12, 0) -- room for scrollbar
    contentBox:SetHeight(1)

    -- Use the correct dark background fill to match the main UI
    local fill = AC:CreateFlatTexture(contentBox, "BACKGROUND", 1, COLORS.INPUT_DARK, 1)
    fill:SetPoint("TOPLEFT", 1, -1)
    fill:SetPoint("BOTTOMRIGHT", -1, 1)

    local borderCol = COLORS.BORDER_LIGHT
    local ht = AC:CreateFlatTexture(contentBox, "OVERLAY", 6, borderCol, 0.9)
    ht:SetPoint("TOPLEFT", 0, 0); ht:SetPoint("TOPRIGHT", 0, 0); ht:SetHeight(1)
    local hb = AC:CreateFlatTexture(contentBox, "OVERLAY", 6, borderCol, 0.9)
    hb:SetPoint("BOTTOMLEFT", 0, 0); hb:SetPoint("BOTTOMRIGHT", 0, 0); hb:SetHeight(1)
    local hl = AC:CreateFlatTexture(contentBox, "OVERLAY", 6, borderCol, 0.9)
    hl:SetPoint("TOPLEFT", 0, 0); hl:SetPoint("BOTTOMLEFT", 0, 0); hl:SetWidth(1)
    local hr = AC:CreateFlatTexture(contentBox, "OVERLAY", 6, borderCol, 0.9)
    hr:SetPoint("TOPRIGHT", 0, 0); hr:SetPoint("BOTTOMRIGHT", 0, 0); hr:SetWidth(1)

    -- Custom scrollbar matching Core/UI.lua assets
    local scrollbar = CreateFrame("Slider", nil, parent)
    scrollbar:SetPoint("TOPRIGHT", -6, -HEADER_HEIGHT - 6)
    scrollbar:SetPoint("BOTTOMRIGHT", -6, 6)
    scrollbar:SetWidth(14)
    scrollbar:SetOrientation("VERTICAL")
    scrollbar:SetMinMaxValues(0, 1)
    scrollbar:SetValue(0)

    local trackBg = AC:CreateFlatTexture(scrollbar, "BACKGROUND", 1, COLORS.INPUT_DARK, 1)
    trackBg:SetAllPoints()
    local thumbTexture = scrollbar:CreateTexture(nil, "OVERLAY")
    thumbTexture:SetTexture("Interface\\AddOns\\ArenaCore\\Media\\UI\\slider-thumb-square.tga")
    thumbTexture:SetWidth(12)
    thumbTexture:SetHeight(20)
    scrollbar:SetThumbTexture(thumbTexture)

    -- Hide default buttons if present
    if scrollbar.ScrollUpButton then scrollbar.ScrollUpButton:Hide() end
    if scrollbar.ScrollDownButton then scrollbar.ScrollDownButton:Hide() end

    local function UpdateScrollbar()
        local maxScroll = content:GetVerticalScrollRange()
        if maxScroll > 0 then
            scrollbar:Show()
            scrollbar:SetMinMaxValues(0, maxScroll)
            scrollbar:SetValue(content:GetVerticalScroll())
        else
            scrollbar:Hide()
        end
    end

    local function UpdateContentHeight()
        local total = 0
        for _, ch in ipairs({child:GetChildren()}) do
            if ch:IsShown() then
                local _, _, _, _, bottom = ch:GetPoint(1)
                if bottom then total = math.max(total, math.abs(bottom) + ch:GetHeight() + 16) end
            end
        end
        total = math.max(total, 600) + 40
        child:SetHeight(total)
        contentBox:SetHeight(total - 12)
        C_Timer.After(0.05, UpdateScrollbar)
    end

    scrollbar:SetScript("OnValueChanged", function(_, value)
        content:SetVerticalScroll(value)
    end)

    content:SetScript("OnScrollRangeChanged", UpdateScrollbar)
    content:SetScript("OnVerticalScroll", UpdateScrollbar)
    content:EnableMouseWheel(true)
    content:SetScript("OnMouseWheel", function(_, delta)
        local current = scrollbar:GetValue()
        local step = 20
        scrollbar:SetValue(current - (delta * step))
    end)

    -- Create padded page container anchored inside contentBox
    self.pageContainers = {}
    for _, page in ipairs(FEATURE_PAGES) do
        local pageFrame = CreateFrame("Frame", nil, contentBox)
        pageFrame:SetPoint("TOPLEFT", 10, -10)
        pageFrame:SetPoint("TOPRIGHT", -10, -10)
        pageFrame:SetHeight(1)
        pageFrame:Hide()
        self.pageContainers[page.id] = pageFrame
    end

    self.contentArea = content
    self.scrollChild = child
    self.contentBox = contentBox
    self.scrollbar = scrollbar
    self.UpdateContentHeight = UpdateContentHeight
    return content
end

-- Select a page in the navigation
function MoreFeatures:SelectPage(pageId)
    -- Update button states like main UI
    for id, btn in pairs(self.navButtons) do
        btn.hover:Hide() -- Always hide hover on state change
        
        if id == pageId then
            -- ACTIVE STATE
            local activeColor = COLORS.NAV_ACTIVE_BG
            btn.bg:SetColorTexture(activeColor[1], activeColor[2], activeColor[3], 1)
            btn.indicator:Show()
            btn.outline:Show() -- Show purple outline
            btn.text:SetTextColor(COLORS.TEXT[1], COLORS.TEXT[2], COLORS.TEXT[3], 1)
            btn.isSelected = true
        else
            -- INACTIVE STATE
            local inactiveColor = COLORS.NAV_INACTIVE_BG
            btn.bg:SetColorTexture(inactiveColor[1], inactiveColor[2], inactiveColor[3], 1)
            btn.indicator:Hide()
            btn.outline:Hide() -- Hide purple outline
            btn.text:SetTextColor(COLORS.TEXT_2[1], COLORS.TEXT_2[2], COLORS.TEXT_2[3], 1)
            btn.isSelected = false
        end
    end
    
    -- Hide all page containers
    if self.pageContainers then
        for id, container in pairs(self.pageContainers) do
            container:Hide()
        end
    end
    
    -- Show the selected page container and load content if needed
    if self.pageContainers and self.pageContainers[pageId] then
        local container = self.pageContainers[pageId]
        container:Show()
        -- Reset scroll to top on page change
        if self.contentArea then
            self.contentArea:SetVerticalScroll(0)
        end
        
        -- Only load content if container is empty
        if container:GetNumChildren() == 0 then
            self:LoadPageContent(pageId, container)
        end
    end
    
    self.currentPage = pageId
end

-- Clear content from a specific page container
function MoreFeatures:ClearPageContent(pageId)
    if not self.pageContainers or not self.pageContainers[pageId] then return end
    
    local container = self.pageContainers[pageId]
    local children = {container:GetChildren()}
    for _, child in ipairs(children) do
        if child then
            child:Hide()
            child:ClearAllPoints()
            child:SetParent(nil)
        end
    end
end

-- Load content for a specific page
function MoreFeatures:LoadPageContent(pageId, container)
    if not container then return end
    
    if pageId == "general" then
        self:LoadGeneralContent(container)
    elseif pageId == "themes" then
        self:LoadThemesContent(container)
    elseif pageId == "font" then
        self:LoadFontContent(container)
    else
        self:LoadComingSoonContent(pageId, container)
    end
end

-- Load General page content
function MoreFeatures:LoadGeneralContent(container)
    -- Exact copy of Blackout.lua pattern
   
    
    -- Settings group box inside the section
    local settingsGroup = CreateFrame("Frame", nil, container)
    settingsGroup:SetPoint("TOPLEFT", 10, -10)
    settingsGroup:SetPoint("TOPRIGHT", -10, -10)
    settingsGroup:SetHeight(480)
    AC:HairlineGroupBox(settingsGroup)

    -- Group title
    local groupTitle = AC:CreateStyledText(settingsGroup, "BLIZZARD FRAMES", 13, COLORS.PRIMARY, "OVERLAY", CUSTOM_FONT)
   groupTitle:SetPoint("TOPLEFT", 15, -18)

    -- Blizzard Frames checkbox row
-- Blizzard Frames checkbox row
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, "Turn off Blizzard Default Arena Frames", 12, COLORS.TEXT_2, "OVERLAY", CUSTOM_FONT)
checkboxLabel1:ClearAllPoints()
checkboxLabel1:SetPoint("LEFT", 15, 0)
    checkboxLabel1:SetPoint("RIGHT", -46, 0) -- reserve space for the checkbox area

    local checkbox1 = AC:CreateFlatCheckbox(checkboxRow1, 20, true)
    checkbox1:SetPoint("RIGHT", -10, 0)
    checkbox1:SetScript("OnClick", function(selfBtn)
        local isChecked = selfBtn:GetChecked()
        MoreFeatures:OnBlizzFrameToggle(isChecked)
        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.hideBlizzardArenaFrames = isChecked
    end)

    -- Description under Blizzard Frames
    local description1 = AC:CreateStyledText(settingsGroup, "Hides the default Blizzard arena frames introduced in Dragonflight.\nRecommended for better performance and cleaner UI.", 11, COLORS.TEXT_MUTED, "OVERLAY", CUSTOM_FONT)
   description1:SetPoint("TOPLEFT", checkboxRow1, "BOTTOMLEFT", 0, -8)
    description1:SetPoint("TOPRIGHT", checkboxRow1, "BOTTOMRIGHT", 0, -8)
    description1:SetJustifyH("LEFT"); description1:SetJustifyV("TOP")
    if description1.SetWordWrap then description1:SetWordWrap(true) end
    description1:SetWidth(settingsGroup:GetWidth() - 40)

    -- Tooltips section title
    local tooltipTitle = AC:CreateStyledText(settingsGroup, "TOOLTIPS", 13, COLORS.PRIMARY, "OVERLAY", CUSTOM_FONT)
    tooltipTitle:SetPoint("TOPLEFT", description1, "BOTTOMLEFT", 0, -22)

    -- Tooltip IDs checkbox row
    local checkboxRow2 = CreateFrame("Frame", nil, settingsGroup)
    checkboxRow2:SetPoint("TOPLEFT", tooltipTitle, "BOTTOMLEFT", 0, -14)
    checkboxRow2:SetPoint("RIGHT", settingsGroup, "RIGHT", -20, 0)
    checkboxRow2:SetHeight(26)

    local checkboxLabel2 = AC:CreateStyledText(checkboxRow2, "Show Spell/Item IDs in Tooltips", 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 db = (AC.DB and AC.DB.profile and AC.DB.profile.tooltipIDs) or {}
    local checkbox2 = AC:CreateFlatCheckbox(checkboxRow2, 20, db.enabled ~= false)
    checkbox2:SetPoint("RIGHT", -10, 0)
    checkbox2:SetScript("OnClick", function(selfBtn)
        local isChecked = selfBtn:GetChecked()
        AC.DB = AC.DB or {}; AC.DB.profile = AC.DB.profile or {}; AC.DB.profile.tooltipIDs = AC.DB.profile.tooltipIDs or {}
        AC.DB.profile.tooltipIDs.enabled = isChecked
        if isChecked then
            print("|cff8B45FFArena Core:|r Tooltip IDs enabled globally")
        else
            print("|cff8B45FFArena Core:|r Tooltip IDs disabled (except in editors)")
        end
    end)

    -- Tooltip section description
    local description2 = AC:CreateStyledText(settingsGroup, "Shows spell and item IDs in tooltips throughout the game.\nAlways enabled in ArenaCore editors for spell management.", 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)

    -- Class Portrait Swap section
    local portraitsTitle = AC:CreateStyledText(settingsGroup, "CLASS PORTRAITS", 13, COLORS.PRIMARY, "OVERLAY", CUSTOM_FONT)
    portraitsTitle:SetPoint("TOPLEFT", description2, "BOTTOMLEFT", 0, -22)

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

    local checkboxLabel3 = AC:CreateStyledText(checkboxRow3, "Class Portrait Swap", 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 dbPortrait = (AC.DB and AC.DB.profile and AC.DB.profile.classPortraitSwap) or {}
    local checkbox3 = AC:CreateFlatCheckbox(checkboxRow3, 20, dbPortrait.enabled == true)
    checkbox3:SetPoint("RIGHT", -10, 0)
    checkbox3:SetScript("OnClick", function(selfBtn)
        local desired = selfBtn:GetChecked()
        AC.DB = AC.DB or {}; AC.DB.profile = AC.DB.profile or {}; AC.DB.profile.classPortraitSwap = AC.DB.profile.classPortraitSwap or {}
        AC.DB.profile.classPortraitSwap.enabled = desired
        if AC.ClassPortraitSwap and AC.ClassPortraitSwap.RefreshAll then
            C_Timer.After(0.05, function() AC.ClassPortraitSwap:RefreshAll() end)
        end
    end)

    local description3 = AC:CreateStyledText(settingsGroup, "Swap all player portraits to class icons using ArenaCore's custom assets located in Media/Classicons/.", 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)

    -- SURRENDER section title
    local surrenderTitle = AC:CreateStyledText(settingsGroup, "SURRENDER", 13, COLORS.PRIMARY, "OVERLAY", CUSTOM_FONT)
    surrenderTitle:SetPoint("TOPLEFT", description3, "BOTTOMLEFT", 0, -22)

    -- Surrender /gg checkbox row (under Class Portraits)
    local checkboxRow4 = CreateFrame("Frame", nil, settingsGroup)
    checkboxRow4:SetPoint("TOPLEFT", surrenderTitle, "BOTTOMLEFT", 0, -14)
    checkboxRow4:SetPoint("RIGHT", settingsGroup, "RIGHT", -20, 0)
    checkboxRow4:SetHeight(26)

    local checkboxLabel4 = AC:CreateStyledText(checkboxRow4, "Surrender /gg", 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 dbSurr = (AC.DB and AC.DB.profile and AC.DB.profile.moreFeatures) or {}
    local checkbox4 = AC:CreateFlatCheckbox(checkboxRow4, 20, (dbSurr.surrenderGGEnabled ~= false)) -- default ON
    checkbox4:SetPoint("RIGHT", -10, 0)
    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.surrenderGGEnabled = enabled
        if MoreFeatures and MoreFeatures.ApplySurrenderSetting then
            MoreFeatures:ApplySurrenderSetting()
        end
    end)

    -- Description under Surrender
    local description4 = AC:CreateStyledText(settingsGroup, "Enable /gg feature inside of arena to easily surrender.", 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)
end

-- Load Themes page content
function MoreFeatures:LoadThemesContent(container)
    -- Theme data (matches AchievementUnlocks.lua)
    local themes = {
        {
            id = "default",
            name = "Arena Core Default",
            description = "The classic Arena Core theme",
            rating = nil, -- Always unlocked
            icon = "Interface\\Icons\\INV_Misc_Gear_01",
        },
        {
            id = "shade_ui",
            name = "Shade UI",
            description = "Premium dark theme inspired by Shade UI addon",
            rating = 2100,
            icon = "Interface\\Icons\\Spell_Shadow_Twilight",
        },
        {
            id = "elite_2400",
            name = "Elite",
            description = "Exclusive theme unlocked at 2400 rating",
            rating = 2400,
            icon = "Interface\\Icons\\Achievement_Arena_2v2_7",
        },
        {
            id = "rank1_3000",
            name = "Rank 1",
            description = "Exclusive theme unlocked at 3000 rating",
            rating = 3000,
            icon = "Interface\\Icons\\Achievement_Arena_3v3_7",
        },
    }
    
    -- Settings group box
    local settingsGroup = CreateFrame("Frame", nil, container)
    settingsGroup:SetPoint("TOPLEFT", 10, -10)
    settingsGroup:SetPoint("TOPRIGHT", -10, -10)
    settingsGroup:SetHeight(560) -- Increased to fit 4 themes (default + 3 unlockables)
    AC:HairlineGroupBox(settingsGroup)
    
    -- Group title
    local groupTitle = AC:CreateStyledText(settingsGroup, "UNLOCKABLE THEMES", 13, COLORS.PRIMARY, "OVERLAY", CUSTOM_FONT)
    groupTitle:SetPoint("TOPLEFT", 15, -18)
    
    -- Description
    local desc = AC:CreateStyledText(settingsGroup, "Unlock exclusive UI themes by reaching rating milestones in PvP.", 11, COLORS.TEXT_MUTED, "OVERLAY", CUSTOM_FONT)
    desc:SetPoint("TOPLEFT", groupTitle, "BOTTOMLEFT", 0, -8)
    desc:SetJustifyH("LEFT")
    
    local yOffset = -55
    
    -- Create theme cards
    for i, theme in ipairs(themes) do
        local card = CreateFrame("Frame", nil, settingsGroup)
        card:SetPoint("TOPLEFT", 15, yOffset)
        card:SetPoint("TOPRIGHT", -15, yOffset)
        card:SetHeight(100)
        
        -- Check if theme is unlocked (default theme is always unlocked)
        local isUnlocked = (theme.id == "default") or (AC.IsThemeUnlocked and AC:IsThemeUnlocked(theme.id)) or false
        
        -- Card background
        local cardBg = AC:CreateFlatTexture(card, "BACKGROUND", 1, COLORS.INPUT_DARK, 1)
        cardBg:SetAllPoints()
        
        -- Purple border if unlocked, gray if locked
        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)
        
        -- Theme icon
        local icon = card:CreateTexture(nil, "ARTWORK")
        icon:SetSize(64, 64)
        icon:SetPoint("LEFT", 15, 0)
        icon:SetTexture(theme.icon)
        
        -- Desaturate if locked
        if not isUnlocked then
            icon:SetDesaturated(true)
            icon:SetAlpha(0.5)
        end
        
        -- Theme name
        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)
        
        -- Rating requirement (or "Always Available" for default)
        local ratingLabel = theme.rating and string.format("Requires %d Rating", theme.rating) or "Always Available"
        local ratingText = AC:CreateStyledText(card, ratingLabel, 11, COLORS.TEXT_2, "OVERLAY", CUSTOM_FONT)
        ratingText:SetPoint("TOPLEFT", themeName, "BOTTOMLEFT", 0, -4)
        
        -- Description
        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
        
        -- Status/Action button
        local btn = AC:CreateTexturedButton(card, 100, 32, "", "UI\\tab-purple-matte")
        btn:SetPoint("RIGHT", -15, 0)
        
        if isUnlocked then
            -- Check if this theme is currently active
            local isActive = AC.ThemeManager and AC.ThemeManager:IsThemeActive(theme.id) or false
            
            if isActive then
                -- Show "ACTIVE" status
                local btnText = AC:CreateStyledText(btn, "ACTIVE", 12, COLORS.TEXT, "OVERLAY", CUSTOM_FONT)
                btnText:SetPoint("CENTER", 0, 0)
                btnText:SetTextColor(0.133, 0.667, 0.267, 1) -- Green
                btn:SetAlpha(1.0)
                btn:Disable()
            else
                -- Show "ACTIVATE" button
                local btnText = AC:CreateStyledText(btn, "ACTIVATE", 12, COLORS.TEXT, "OVERLAY", CUSTOM_FONT)
                btnText:SetPoint("CENTER", 0, 0)
                btnText:SetTextColor(1, 1, 1, 1) -- White
                btn:SetAlpha(1.0)
                btn:Enable()
                btn:SetScript("OnClick", function()
                    -- Activate this theme
                    if AC.ThemeManager then
                        AC.ThemeManager:ApplyTheme(theme.id)
                        -- Refresh the themes page to update button states
                        if MoreFeatures and MoreFeatures.pageContainers and MoreFeatures.pageContainers["themes"] then
                            MoreFeatures:ClearPageContent("themes")
                            MoreFeatures:LoadThemesContent(MoreFeatures.pageContainers["themes"])
                        end
                    end
                end)
            end
        else
            -- Show locked status with custom padlock icon (larger)
            local lockIcon = btn:CreateTexture(nil, "OVERLAY")
            lockIcon:SetSize(20, 20) -- Increased from 16x16
            lockIcon:SetPoint("CENTER", -24, 0)
            lockIcon:SetTexture("Interface\\AddOns\\ArenaCore\\Media\\Achievements\\padlock_icon.png")
            
            local btnText = AC:CreateStyledText(btn, "LOCKED", 12, COLORS.TEXT, "OVERLAY", CUSTOM_FONT)
            btnText:SetPoint("CENTER", 10, 0) -- Offset right to make room for larger icon
            btnText:SetTextColor(0.863, 0.176, 0.176, 1) -- Red
            btn:SetAlpha(0.5)
            btn:Disable()
        end
        
        yOffset = yOffset - 105
    end
    
    -- Info text at bottom (properly positioned inside the box)
    local infoText = AC:CreateStyledText(settingsGroup, "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

-- Load Font page content
function MoreFeatures:LoadFontContent(container)
    -- Settings group box
    local settingsGroup = CreateFrame("Frame", nil, container)
    settingsGroup:SetPoint("TOPLEFT", 10, -10)
    settingsGroup:SetPoint("TOPRIGHT", -10, -10)
    settingsGroup:SetHeight(380)
    AC:HairlineGroupBox(settingsGroup)

    -- Group title
    local groupTitle = AC:CreateStyledText(settingsGroup, "GLOBAL ARENACORE FONT", 13, COLORS.PRIMARY, "OVERLAY", CUSTOM_FONT)
    groupTitle:SetPoint("TOPLEFT", 15, -18)

    -- Global Font checkbox row
    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, "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)
    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 MoreFeatures and MoreFeatures.ApplyGlobalFont then
            MoreFeatures:ApplyGlobalFont(enabled)
        end
    end)

    -- Description under Global Font
    local description1 = AC:CreateStyledText(settingsGroup, "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)
    
    -- Font Outline checkbox
    local checkboxRow2 = CreateFrame("Frame", nil, settingsGroup)
    checkboxRow2:SetPoint("TOPLEFT", description1, "BOTTOMLEFT", 0, -18)
    checkboxRow2:SetPoint("RIGHT", settingsGroup, "RIGHT", -20, 0)
    checkboxRow2:SetHeight(26)

    local checkboxLabel2 = AC:CreateStyledText(checkboxRow2, "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)) -- default ON
    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)
    
    -- Description under Font Outline
    local description2 = AC:CreateStyledText(settingsGroup, "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)
    
    -- Font Shadow checkbox
    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, "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)) -- default ON
    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)
    
    -- Description under Font Shadow
    local description3 = AC:CreateStyledText(settingsGroup, "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)
    
    -- ACTION BAR FONT SETTINGS section title
    local actionBarTitle = AC:CreateStyledText(settingsGroup, "ACTIONBAR FONT SETTINGS", 13, COLORS.PRIMARY, "OVERLAY", CUSTOM_FONT)
    actionBarTitle:SetPoint("TOPLEFT", description3, "BOTTOMLEFT", 0, -22)
    
    -- Helper function to create sliders with +/- buttons and text input
    local function CreateActionBarSlider(parent, label, anchorTo, yOffset, dbKey, minVal, maxVal, defaultVal)
        local row = CreateFrame("Frame", nil, parent)
        row:SetPoint("TOPLEFT", anchorTo, "BOTTOMLEFT", 0, yOffset)
        row:SetPoint("RIGHT", parent, "RIGHT", -20, 0)
        row:SetHeight(40)
        
        local labelText = AC:CreateStyledText(row, label, 12, COLORS.TEXT_2, "OVERLAY", CUSTOM_FONT)
        labelText:SetPoint("TOPLEFT", 0, -5)
        
        -- Get current value from database (same as working sliders)
        local db = AC.DB and AC.DB.profile and AC.DB.profile.actionBarFont or {}
        local currentValue = db[dbKey] or defaultVal
        
        -- Minus button
        local minusBtn = AC:CreateTexturedButton(row, 20, 20, "-", "button-minus")
        minusBtn:SetPoint("TOPLEFT", labelText, "BOTTOMLEFT", 0, -8)
        local minusText = AC:CreateStyledText(minusBtn, "-", 16, COLORS.TEXT, "OVERLAY", CUSTOM_FONT)
        minusText:SetPoint("CENTER", 0, 0)
        
        -- Text input box
        local inputBox = CreateFrame("EditBox", nil, row)
        inputBox:SetPoint("LEFT", minusBtn, "RIGHT", 5, 0)
        inputBox:SetSize(50, 20)
        inputBox:SetAutoFocus(false)
        inputBox:SetFontObject(GameFontNormal)
        inputBox:SetMaxLetters(5)
        inputBox:SetNumeric(false)
        
        local inputBg = AC:CreateFlatTexture(inputBox, "BACKGROUND", 1, COLORS.INPUT_DARK, 1)
        inputBg:SetAllPoints()
        
        local inputBorder = AC:CreateFlatTexture(inputBox, "BORDER", 1, COLORS.BORDER, 1)
        inputBorder:SetAllPoints()
        
        inputBox:SetText(tostring(currentValue))
        inputBox:SetCursorPosition(0)
        
        -- Plus button
        local plusBtn = AC:CreateTexturedButton(row, 20, 20, "+", "button-plus")
        plusBtn:SetPoint("LEFT", inputBox, "RIGHT", 5, 0)
        local plusText = AC:CreateStyledText(plusBtn, "+", 16, COLORS.TEXT, "OVERLAY", CUSTOM_FONT)
        plusText:SetPoint("CENTER", 0, 0)
        
        -- Slider
        local slider = CreateFrame("Slider", nil, row)
        slider:SetPoint("LEFT", plusBtn, "RIGHT", 10, 0)
        slider:SetPoint("RIGHT", row, "RIGHT", -10, 0)
        slider:SetHeight(16)
        slider:SetOrientation("HORIZONTAL")
        slider:SetMinMaxValues(minVal, maxVal)
        slider:SetValue(currentValue)
        slider:SetValueStep(1)
        
        local sliderBg = AC:CreateFlatTexture(slider, "BACKGROUND", 1, COLORS.INSET, 1)
        sliderBg:SetAllPoints()
        
        local sliderThumb = slider:CreateTexture(nil, "OVERLAY")
        sliderThumb:SetSize(12, 16)
        sliderThumb:SetColorTexture(COLORS.PRIMARY[1], COLORS.PRIMARY[2], COLORS.PRIMARY[3], 1)
        slider:SetThumbTexture(sliderThumb)
        
        -- Update function
        local function UpdateValue(value)
            value = math.floor(value + 0.5)
            value = math.max(minVal, math.min(maxVal, value))
            
            slider:SetValue(value)
            inputBox:SetText(tostring(value))
            
            -- Save to AC.DB.profile (EXACT pattern from working sliders in Helpers.lua)
            if AC.DB and AC.DB.profile then
                AC.DB.profile.actionBarFont = AC.DB.profile.actionBarFont or {}
                AC.DB.profile.actionBarFont[dbKey] = value
            end
            
            -- Apply changes immediately to ALL bars (MattActionBarFont pattern)
            if AC.MoreFeatures and AC.MoreFeatures.ApplyActionBarFonts then
                AC.MoreFeatures:ApplyActionBarFonts()
            end
        end
        
        minusBtn:SetScript("OnClick", function() UpdateValue(slider:GetValue() - 1) end)
        plusBtn:SetScript("OnClick", function() UpdateValue(slider:GetValue() + 1) end)
        slider:SetScript("OnValueChanged", function(self, value) UpdateValue(value) end)
        
        inputBox:SetScript("OnEnterPressed", function(self)
            local value = tonumber(self:GetText())
            if value then UpdateValue(value) end
            self:ClearFocus()
        end)
        
        inputBox:SetScript("OnEscapePressed", function(self)
            self:SetText(tostring(slider:GetValue()))
            self:ClearFocus()
        end)
        
        return row
    end
    
    -- Scale slider
    local scaleRow = CreateActionBarSlider(settingsGroup, "Scale", actionBarTitle, -14, "scale", 50, 200, 100)
    
    -- Horizontal position slider
    local horizRow = CreateActionBarSlider(settingsGroup, "Horizontal", scaleRow, -8, "horizontal", -500, 500, 0)
    
    -- Vertical position slider
    local vertRow = CreateActionBarSlider(settingsGroup, "Vertical", horizRow, -8, "vertical", -500, 500, 0)
    
    -- Update settingsGroup height to accommodate new sliders
    settingsGroup:SetHeight(620)
end

-- Load Coming Soon content (simple placeholder page)
function MoreFeatures:LoadComingSoonContent(pageId, container)
    if not container then return end
    local title = AC:CreateStyledText(container, "Coming Soon...", 16, COLORS.TEXT, "OVERLAY", CUSTOM_FONT)
    title:SetPoint("TOP", 0, -20)

    local description = AC:CreateStyledText(container, "This feature is planned for a future update.\nStay tuned for more exciting additions!", 12, COLORS.TEXT_2, "OVERLAY", CUSTOM_FONT)
    description:SetPoint("TOP", title, "BOTTOM", 0, -15)
    description:SetJustifyH("CENTER")
end

-- Handle Blizzard frame toggle
function MoreFeatures:OnBlizzFrameToggle(enabled)
    if AC.BlizzFrameHider then
        if enabled then
            AC.BlizzFrameHider:EnableHiding()
        else
            AC.BlizzFrameHider:DisableHiding()
        end
    end
end

-- Apply or remove the /gg slash command based on setting
function MoreFeatures:ApplySurrenderSetting()
    local enabled = AC and AC.DB and AC.DB.profile and AC.DB.profile.moreFeatures and AC.DB.profile.moreFeatures.surrenderGGEnabled
    -- Clear existing bindings first
    SlashCmdList.SURRENDERGG = nil
    _G.SLASH_SURRENDERGG1 = nil
    if enabled then
        _G.SLASH_SURRENDERGG1 = "/gg"
        SlashCmdList.SURRENDERGG = function(msg)
            local _, instanceType = IsInInstance()
            if instanceType == "arena" then
                if type(SurrenderArena) == "function" then
                    SurrenderArena()
                else
                    -- Fallback: show a hint if function isn't available in this client build
                    print("|cff8B45FFArena Core:|r Surrender command not available in this client build.")
                end
            else
                print("|cff8B45FFArena Core:|r /gg works only inside arenas.")
            end
        end
    end
end

-- Apply or remove global ArenaCore font
function MoreFeatures:ApplyGlobalFont(enabled)
    if enabled then
        -- Enable GlobalFont module
        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()
            print("|cff8B45FFArena Core:|r Global font enabled and applied.")
        else
            print("|cff8B45FFArena Core:|r Global font enabled. Please /reload for changes to take effect.")
        end
    else
        -- Disable GlobalFont module and revert fonts
        if AC.GlobalFont then
            AC.GlobalFont:Disable()
            print("|cff8B45FFArena Core:|r Global font disabled. Please /reload to fully restore default fonts.")
        else
            print("|cff8B45FFArena Core:|r Global font disabled. Please /reload to restore default fonts.")
        end
    end
end

-- Removed old debug function - now using ApplyToSingleButton directly

-- Setup event-driven action bar font system (MattActionBarFont pattern - ZERO TAINT!)
function MoreFeatures:SetupActionBarHook()
    if self.actionBarHookSetup then return end
    self.actionBarHookSetup = true
    
    -- Create event frame for action bar font updates
    local eventFrame = _G._AC_ABFont or CreateFrame("Frame")
    _G._AC_ABFont = eventFrame
    
    -- Register events (MattActionBarFont pattern)
    eventFrame:RegisterEvent("PLAYER_LOGIN")
    eventFrame:RegisterEvent("UPDATE_BINDINGS")
    
    eventFrame:SetScript("OnEvent", function(self, event)
        if event == "PLAYER_LOGIN" then
            -- Apply fonts on login
            if AC.MoreFeatures then
                AC.MoreFeatures:ApplyActionBarFonts()
            end
        elseif event == "UPDATE_BINDINGS" then
            -- Reapply fonts when bindings change (with 0.2s delay like MattActionBarFont)
            C_Timer.After(0.2, function()
                if AC.MoreFeatures then
                    AC.MoreFeatures:ApplyActionBarFonts()
                end
            end)
        end
    end)
    
    -- Hook QuickKeybindFrame to reapply after keybind mode closes
    if QuickKeybindFrame then
        QuickKeybindFrame:HookScript("OnHide", function()
            if AC.MoreFeatures then
                AC.MoreFeatures:ApplyActionBarFonts()
            end
        end)
    end
    
    -- Apply fonts immediately
    self:ApplyActionBarFonts()
end

-- Apply action bar fonts to all buttons (MattActionBarFont pattern)
function MoreFeatures:ApplyActionBarFonts()
    -- Get database settings (same as working sliders)
    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
    
    -- List of action bar button prefixes (including StanceButton - MattActionBarFont does this!)
    local bars = {
        "ActionButton", "MultiBarBottomLeftButton", "MultiBarBottomRightButton",
        "MultiBarRightButton", "MultiBarLeftButton", "MultiBar5Button", 
        "MultiBar6Button", "MultiBar7Button", "StanceButton"
    }
    
    -- Apply to all buttons
    for _, bar in pairs(bars) do
        for i = 1, 12 do
            local button = _G[bar .. i]
            if button then
                self:ApplyToSingleButton(button, scale, offsetX, offsetY)
            end
        end
    end
end

-- Apply font settings to a single button (MattActionBarFont pattern - NO PROTECTION CHECKS!)
function MoreFeatures:ApplyToSingleButton(btn, scale, offsetX, offsetY)
    if not btn then return end
    
    -- Get HotKey element (MattActionBarFont pattern)
    local hk = btn.HotKey
    if not hk then return end
    
    -- Apply ArenaCore font with custom size
    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")
    
    -- Apply position
    hk:ClearAllPoints()
    hk:SetPoint("TOPRIGHT", btn, "TOPRIGHT", offsetX, offsetY)
    
    -- Hook SetText to reapply settings when Blizzard updates the text (THE SECRET!)
    -- MattActionBarFont uses this pattern to persist fonts without taint
    if not hk._AC_ABF_Hooked then
        local originalSetText = hk.SetText
        hk.SetText = function(self, text)
            -- Call original SetText
            originalSetText(self, text)
            
            -- Get current settings
            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
            
            -- Reapply ArenaCore font
            local arenaCoreFontPath = "Interface\\AddOns\\ArenaCore\\Media\\Fonts\\arenacore.ttf"
            local baseSize = 10
            local newSize = math.max(1, math.floor(baseSize * currentScale))
            self:SetFont(arenaCoreFontPath, newSize, "OUTLINE")
            
            -- Reapply position
            self:ClearAllPoints()
            self:SetPoint("TOPRIGHT", btn, "TOPRIGHT", currentOffsetX, currentOffsetY)
        end
        hk._AC_ABF_Hooked = true
    end
end

-- Test slash command for action bar font
SLASH_ACTESTABFONT1 = "/actestabfont"
SlashCmdList["ACTESTABFONT"] = function()
    if AC.MoreFeatures and AC.MoreFeatures.ApplyActionBarFonts then
        AC.MoreFeatures:ApplyActionBarFonts()
    end
end

-- Initialize the More Features window
function MoreFeatures:Initialize()
    local window = self:CreateWindow()
    local header = self:CreateHeader(window)
    local sidebar = self:CreateSidebar(window)
    local content = self:CreateContentArea(window)
    
    -- Setup action bar font hook (if not already done)
    if not self.actionBarHookSetup then
        self:SetupActionBarHook()
    end
    
    -- Load saved settings
    self:LoadSettings()
    -- Apply settings that need runtime hooks
    if self.ApplySurrenderSetting then self:ApplySurrenderSetting() end
    if self.ApplyGlobalFont then 
        local enabled = AC.DB and AC.DB.profile and AC.DB.profile.moreFeatures and AC.DB.profile.moreFeatures.globalFontEnabled
        if enabled then
            self:ApplyGlobalFont(true)
        end
    end
    
    -- Set General as default and select it
    self:SelectPage("general")
    
    self.initialized = true
end

-- Load saved settings from database
function MoreFeatures:LoadSettings()
    AC.DB = AC.DB or {}
    AC.DB.profile = AC.DB.profile or {}
    AC.DB.profile.moreFeatures = AC.DB.profile.moreFeatures or {}
    
    -- Set default for hiding Blizzard frames if not set
    if AC.DB.profile.moreFeatures.hideBlizzardArenaFrames == nil then
        AC.DB.profile.moreFeatures.hideBlizzardArenaFrames = true
    end

    -- Default enable Surrender /gg if not set
    if AC.DB.profile.moreFeatures.surrenderGGEnabled == nil then
        AC.DB.profile.moreFeatures.surrenderGGEnabled = true
    end
end

-- Show the More Features window
function MoreFeatures:ShowWindow()
    if not self.initialized then
        self:Initialize()
    end
    
    if self.window then
        self.window:Show()
        -- Always ensure General tab is selected when opening
        C_Timer.After(0.1, function()
            if self.SelectPage then
                self:SelectPage("general")
            end
        end)
    end
end

-- Hide the More Features window
function MoreFeatures:HideWindow()
    if self.window then
        self.window:Hide()
    end
end

-- Toggle the More Features window
function MoreFeatures:ToggleWindow()
    if not self.initialized then
        self:Initialize()
    end
    
    if self.window and self.window:IsShown() then
        self:HideWindow()
    else
        self:ShowWindow()
    end
end

-- Auto-initialize action bar fonts on login (CRITICAL FIX!)
local initFrame = CreateFrame("Frame")
initFrame:RegisterEvent("PLAYER_LOGIN")
initFrame:SetScript("OnEvent", function()
    -- Initialize action bar font system on login (before window is opened)
    if AC.MoreFeatures and not AC.MoreFeatures.actionBarHookSetup then
        AC.MoreFeatures:SetupActionBarHook()
    end
end)