-- =============================================================================
-- Core/Pages/BlackoutMods.lua - Arena Arsenal (sub-page system)
-- =============================================================================

local AddonName, _ns = ...
if type(_G.ArenaCore) ~= "table" then _G.ArenaCore = {} end
local AC = _G.ArenaCore

local COLORS = AC.COLORS
local V = AC.Vanity

-- Localization helper
local function L(key, fallback)
    if AC.UILocalization then
        return AC.UILocalization:Get("BLACKOUT_MODS", key, fallback or key)
    end
    return fallback or key
end

-- ============================================================================
-- Tab Button (same style as ArenaFrames tabs)
-- ============================================================================
local function CreateArsenalTabButton(parent, text, width, height)
    local btn = CreateFrame("Button", nil, parent)
    btn:SetSize(width or 110, height or 30)

    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)
        if t.SetSnapToPixelGrid then t:SetSnapToPixelGrid(true) end
        if t.SetTexelSnappingBias then t:SetTexelSnappingBias(0) 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

-- ============================================================================
-- Main Page Builder
-- ============================================================================
local function CreateArenaArsenalPage(root)
    if V and V.EnsureMottoStrip then V:EnsureMottoStrip(root) end

    -- Tab strip (taller for two rows)
    local strip = CreateFrame("Frame", nil, root)
    strip:SetHeight(100)
    if strip.SetClipsChildren then strip:SetClipsChildren(false) end

    if root._mottoStrip then
        strip:SetPoint("TOPLEFT", root._mottoStrip, "BOTTOMLEFT", 0, -8)
        strip:SetPoint("TOPRIGHT", root._mottoStrip, "BOTTOMRIGHT", 0, -8)
    else
        strip:SetPoint("TOPLEFT", 6, -52)
        strip:SetPoint("TOPRIGHT", -6, -52)
    end

    local sBorder = AC:CreateFlatTexture(strip, "BACKGROUND", 1, COLORS.BORDER_LIGHT, 0.6)
    sBorder:SetAllPoints()

    local sFill = AC:CreateFlatTexture(strip, "BACKGROUND", 2, {0.12, 0.12, 0.12, 1}, 1)
    sFill:SetPoint("TOPLEFT", 1, -1)
    sFill:SetPoint("BOTTOMRIGHT", -1, 1)

    -- Row 1 (top row): Misc Reminders, PvP Queue Notifier, Party Trinkets
    local tabRow1 = CreateFrame("Frame", nil, strip)
    tabRow1:SetHeight(40)
    tabRow1:SetFrameLevel((strip:GetFrameLevel() or 1) + 2)
    if tabRow1.SetClipsChildren then tabRow1:SetClipsChildren(false) end
    tabRow1:SetPoint("TOPLEFT", 0, -8)
    tabRow1:SetPoint("TOPRIGHT", 0, -8)

    -- Row 2 (bottom row): Big Debuffs, Safe Queue
    local tabRow2 = CreateFrame("Frame", nil, strip)
    tabRow2:SetHeight(40)
    tabRow2:SetFrameLevel((strip:GetFrameLevel() or 1) + 2)
    if tabRow2.SetClipsChildren then tabRow2:SetClipsChildren(false) end
    tabRow2:SetPoint("TOPLEFT", 0, -48)
    tabRow2:SetPoint("TOPRIGHT", 0, -48)

    local tabs = {}
    local contentFrames = {}
    local activeTab = "misc"
    local contentRoot  -- declared here so the closure below can actually see it

    local function SetActiveTab(id)
        activeTab = id
        for _, b in ipairs(tabs) do
            b:SetSelected(b._tabId == id)
        end
        for pid, f in pairs(contentFrames) do
            if pid == id then
                f:Show()
            else
                f:Hide()
            end
        end
        if AC.UpdateContentHeight then
            C_Timer.After(0.05, function() AC:UpdateContentHeight() end)
        end
    end

    local function EnsureContentFrame(id)
        if contentFrames[id] then return contentFrames[id] end
        local f = CreateFrame("Frame", nil, contentRoot)
        f:SetAllPoints()
        f:Hide()
        contentFrames[id] = f
        return f
    end

    -- Tab setup: id, label, width, which row it lives on
    local tabDefs = {
        { id = "misc",      label = L("tab_misc_reminders", "Misc Reminders"),      width = 110, row = 1 },
        { id = "pvpqueue", label = L("tab_pvp_queue", "PvP Queue Notifier"),        width = 130, row = 1 },
        { id = "party",    label = L("tab_party_trinkets", "Party Trinkets"),        width = 110, row = 1 },
        { id = "bigdebuffs",label = L("tab_big_debuffs", "Big Debuffs/Buffs"),        width = 130, row = 2 },
        { id = "safequeue", label = L("tab_safe_queue", "Safe Queue"),              width = 100, row = 2 },
        { id = "targetoftargets", label = L("tab_target_of_targets", "Target of Targets"), width = 120, row = 2 },
    }

    local prevBtn = nil
    local currentRow = 1
    for i, def in ipairs(tabDefs) do
        local rowFrame = (def.row == 2) and tabRow2 or tabRow1
        local btn = CreateArsenalTabButton(rowFrame, def.label, def.width, 30)
        btn._tabId = def.id

        if def.row ~= currentRow then
            -- New row starting fresh, reset the anchor chain
            btn:SetPoint("TOPLEFT", 14, -2)
            currentRow = def.row
            prevBtn = btn
        elseif i == 1 then
            btn:SetPoint("TOPLEFT", 14, -2)
            prevBtn = btn
        else
            btn:SetPoint("LEFT", prevBtn, "RIGHT", 8, 0)
            prevBtn = btn
        end
        table.insert(tabs, btn)
    end

    -- Content root sits below the strip
    contentRoot = CreateFrame("Frame", nil, root)
    contentRoot:SetPoint("TOPLEFT", strip, "BOTTOMLEFT", 0, -22)
    contentRoot:SetPoint("TOPRIGHT", strip, "BOTTOMRIGHT", 0, -22)
    contentRoot:SetPoint("BOTTOMLEFT", 0, 0)
    contentRoot:SetPoint("BOTTOMRIGHT", 0, 0)

    -- Scroll hint so folks know there is more down below
    local scrollHint = AC:CreateStyledText(root, L("scroll_hint", "Scroll down for more mods/options!"), 10, {1.0, 0.55, 0.0, 1}, "OVERLAY", "")
    scrollHint:SetPoint("BOTTOM", strip, "BOTTOM", 0, -14)
    scrollHint:SetJustifyH("CENTER")

    -- =========================================================================
    -- Placeholder builder used by all tabs
    -- =========================================================================
    local function BuildPlaceholder(tabId, titleKey)
        local f = EnsureContentFrame(tabId)
        if f._built then return end

        local g = CreateFrame("Frame", nil, f)
        g:SetPoint("TOPLEFT", 18, -12)
        g:SetPoint("TOPRIGHT", -18, -12)
        g:SetHeight(220)
        AC:HairlineGroupBox(g)

        local sectionTitle = AC:CreateStyledText(g, L("page_title", "Arena Arsenal"), 13, COLORS.PRIMARY, "OVERLAY", "")
        sectionTitle:SetPoint("TOPLEFT", 20, -18)

        local tabTitle = AC:CreateStyledText(g, L(titleKey, tabId), 14, COLORS.TEXT, "OVERLAY", "")
        tabTitle:SetPoint("TOP", 0, -60)
        tabTitle:SetJustifyH("CENTER")

        local placeholderTitle = AC:CreateStyledText(g, L("placeholder_title", "Coming Soon"), 16, COLORS.TEXT, "OVERLAY", "")
        placeholderTitle:SetPoint("TOP", 0, -100)
        placeholderTitle:SetJustifyH("CENTER")

        local desc = AC:CreateStyledText(g, L("placeholder_desc", "This module is currently under development. Check back in a future update!"), 11, COLORS.TEXT_MUTED, "OVERLAY", "")
        desc:SetPoint("TOP", 0, -140)
        desc:SetPoint("LEFT", 30, 0)
        desc:SetPoint("RIGHT", -30, 0)
        desc:SetJustifyH("CENTER")
        desc:SetWordWrap(true)

        f._built = true
    end

    -- =========================================================================
    -- Build Target of Targets page
    -- =========================================================================
    local function BuildTargetOfTargets()
        local f = EnsureContentFrame("targetoftargets")
        if f._built then return end

        -- Load up the TargetOfTargets page if it exists
        if AC.BuildTargetOfTargetsPage then
            AC.BuildTargetOfTargetsPage(f)
        else
            -- If the module went missing, show a placeholder instead
            BuildPlaceholder("targetoftargets", "Target of Targets")
        end

        f._built = true
    end

    -- =========================================================================
    -- Build functions for each tab
    -- =========================================================================
    local function BuildMiscReminders()
        local f = EnsureContentFrame("misc")
        if f._built then return end

        local g = CreateFrame("Frame", nil, f)
        g:SetPoint("TOPLEFT", 18, -12)
        g:SetPoint("TOPRIGHT", -18, -12)
        g:SetHeight(640)
        AC:HairlineGroupBox(g)

        local sectionTitle = AC:CreateStyledText(g, L("page_title", "Arena Arsenal"), 13, COLORS.PRIMARY, "OVERLAY", "")
        sectionTitle:SetPoint("TOPLEFT", 20, -18)

        local tabTitle = AC:CreateStyledText(g, L("buff_reminders_title", "Buff Reminders"), 14, COLORS.TEXT, "OVERLAY", "")
        tabTitle:SetPoint("TOPLEFT", sectionTitle, "BOTTOMLEFT", 0, -18)

        local descText = AC:CreateStyledText(g, L("buff_reminders_desc", "Shows glowing icons for class buffs you or your group are missing. Drag the icons to reposition."), 11, COLORS.TEXT_MUTED, "OVERLAY", "")
        descText:SetPoint("TOPLEFT", tabTitle, "BOTTOMLEFT", 0, -6)
        descText:SetWidth(g:GetWidth() - 40)
        descText:SetJustifyH("LEFT")
        descText:SetWordWrap(true)

        -- Master switch for the whole thing
        local db = AC.DB and AC.DB.profile and AC.DB.profile.buffReminder
        if not db then db = {} end

        local enableRow = CreateFrame("Frame", nil, g)
        enableRow:SetPoint("TOPLEFT", descText, "BOTTOMLEFT", 0, -14)
        enableRow:SetPoint("RIGHT", g, "RIGHT", -20, 0)
        enableRow:SetHeight(26)

        local enableLabel = AC:CreateStyledText(enableRow, L("enable_buff_reminder", "Enable Buff Reminder"), 12, COLORS.TEXT_2, "OVERLAY", "")
        enableLabel:SetPoint("LEFT", 0, 0)
        enableLabel:SetPoint("RIGHT", -46, 0)
        enableLabel:SetJustifyH("LEFT")

        local enableCheckbox = AC:CreateFlatCheckbox(enableRow, 20, db.enabled ~= false, function(value)
            if not AC.DB.profile.buffReminder then AC.DB.profile.buffReminder = {} end
            AC.DB.profile.buffReminder.enabled = value
            if AC.BuffReminder and AC.BuffReminder.Toggle then
                AC.BuffReminder:Toggle(value)
            end
        end)
        enableCheckbox:SetPoint("RIGHT", -10, 0)

        -- Explain what this actually does
        local tooltipText = L("buff_reminder_tooltip", "Tracks your class buffs and lights up when you or a group member is missing one.")
        enableRow:SetScript("OnEnter", function(self)
            GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
            GameTooltip:SetText(tooltipText, 1, 1, 1, 1, true)
            GameTooltip:Show()
        end)
        enableRow:SetScript("OnLeave", function() GameTooltip:Hide() end)

        -- Slider row builder with +/- buttons for precise adjustments
        local function CreateSimpleSliderRow(parent, anchorFrame, anchorPoint, labelTextStr, minVal, maxVal, currentVal, dbKey, isDecimal, tooltipStr)
            local row = CreateFrame("Frame", nil, parent)
            row:SetHeight(26)
            row:SetPoint("TOPLEFT", anchorFrame, anchorPoint, 0, -14)
            row:SetPoint("RIGHT", parent, "RIGHT", -20, 0)

            local label = AC:CreateStyledText(row, labelTextStr, 11, COLORS.TEXT_2, "OVERLAY", "")
            label:SetPoint("LEFT", 0, 0)
            label:SetWidth(80)
            label:SetJustifyH("LEFT")

            local valueText = AC:CreateStyledText(row, "", 11, COLORS.TEXT, "OVERLAY", "")
            valueText:SetPoint("RIGHT", -10, 0)
            valueText:SetWidth(50)
            valueText:SetJustifyH("RIGHT")

            -- Minus button for when you need to be precise
            local downBtn = CreateFrame("Button", nil, row)
            downBtn:SetSize(16, 16)
            downBtn:SetPoint("LEFT", label, "RIGHT", 6, 0)
            local downBg = downBtn:CreateTexture(nil, "BACKGROUND")
            downBg:SetAllPoints(); downBg:SetColorTexture(0.2, 0.2, 0.2, 0.8)
            local downBorder = downBtn:CreateTexture(nil, "BORDER")
            downBorder:SetAllPoints(); downBorder:SetColorTexture(0.4, 0.4, 0.4, 1)
            downBorder:SetPoint("TOPLEFT", 1, -1); downBorder:SetPoint("BOTTOMRIGHT", -1, 1)
            local downTxt = downBtn:CreateFontString(nil, "OVERLAY")
            downTxt:SetFont(AC.FONT_PATH or "Interface/AddOns/ArenaCore/Media/Fonts/arenacore.ttf", 12, "")
            downTxt:SetText("-"); downTxt:SetTextColor(0.8, 0.8, 0.8, 1); downTxt:SetPoint("CENTER")

            local function OnSliderChange(val)
                if not AC.DB.profile.buffReminder then AC.DB.profile.buffReminder = {} end
                if isDecimal then
                    AC.DB.profile.buffReminder[dbKey] = tonumber(string.format("%.2f", val))
                    valueText:SetText(string.format("%.2f", val))
                else
                    AC.DB.profile.buffReminder[dbKey] = math.floor(val + 0.5)
                    valueText:SetText(string.format("%.0f", val))
                end
                if AC.BuffReminder then
                    if dbKey == "iconSize" and AC.BuffReminder.UpdateIconSize then
                        AC.BuffReminder:UpdateIconSize(AC.DB.profile.buffReminder[dbKey])
                    elseif dbKey == "spacing" and AC.BuffReminder.UpdateSpacing then
                        AC.BuffReminder:UpdateSpacing(AC.DB.profile.buffReminder[dbKey])
                    elseif dbKey == "iconZoom" and AC.BuffReminder.UpdateIconZoom then
                        AC.BuffReminder:UpdateIconZoom(AC.DB.profile.buffReminder[dbKey])
                    end
                end
            end

            local sliderContainer = AC:CreateFlatSlider(row, 160, 18, minVal, maxVal, currentVal, false, OnSliderChange)
            sliderContainer:SetPoint("LEFT", downBtn, "RIGHT", 4, 0)
            if isDecimal and sliderContainer.slider then
                sliderContainer.slider:SetValueStep(0.01)
            end

            -- Plus button for fine-tuning
            local upBtn = CreateFrame("Button", nil, row)
            upBtn:SetSize(16, 16)
            upBtn:SetPoint("LEFT", sliderContainer, "RIGHT", 4, 0)
            local upBg = upBtn:CreateTexture(nil, "BACKGROUND")
            upBg:SetAllPoints(); upBg:SetColorTexture(0.2, 0.2, 0.2, 0.8)
            local upBorder = upBtn:CreateTexture(nil, "BORDER")
            upBorder:SetAllPoints(); upBorder:SetColorTexture(0.4, 0.4, 0.4, 1)
            upBorder:SetPoint("TOPLEFT", 1, -1); upBorder:SetPoint("BOTTOMRIGHT", -1, 1)
            local upTxt = upBtn:CreateFontString(nil, "OVERLAY")
            upTxt:SetFont(AC.FONT_PATH or "Interface/AddOns/ArenaCore/Media/Fonts/arenacore.ttf", 12, "")
            upTxt:SetText("+"); upTxt:SetTextColor(0.8, 0.8, 0.8, 1); upTxt:SetPoint("CENTER")

            -- Plus button for when minus wasn't enough
            if isDecimal then
                valueText:SetText(string.format("%.2f", currentVal))
            else
                valueText:SetText(string.format("%.0f", currentVal))
            end

            -- Wire up the click handlers
            local step = isDecimal and 0.01 or 1
            downBtn:SetScript("OnClick", function()
                local v = sliderContainer.slider:GetValue()
                local newVal = math.max(minVal, v - step)
                sliderContainer.slider:SetValue(newVal)
                OnSliderChange(newVal)
            end)
            upBtn:SetScript("OnClick", function()
                local v = sliderContainer.slider:GetValue()
                local newVal = math.min(maxVal, v + step)
                sliderContainer.slider:SetValue(newVal)
                OnSliderChange(newVal)
            end)

            if tooltipStr then
                row:SetScript("OnEnter", function(self)
                    GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
                    GameTooltip:SetText(tooltipStr, 1, 1, 1, 1, true)
                    GameTooltip:Show()
                end)
                row:SetScript("OnLeave", function() GameTooltip:Hide() end)
            end

            return row
        end

        -- Icon Size slider
        local iconSizeRow = CreateSimpleSliderRow(
            g, enableRow, "BOTTOMLEFT",
            L("icon_size", "Icon Size"), 20, 80, db.iconSize or 40, "iconSize", false,
            L("icon_size_desc", "Size of the buff reminder icons on screen.")
        )

        -- Spacing slider
        local spacingRow = CreateSimpleSliderRow(
            g, iconSizeRow, "BOTTOMLEFT",
            L("spacing", "Spacing"), 0, 20, db.spacing or 6, "spacing", false,
            L("spacing_desc", "Space between each buff reminder icon.")
        )

        -- Icon Zoom slider
        local zoomRow = CreateSimpleSliderRow(
            g, spacingRow, "BOTTOMLEFT",
            L("icon_zoom", "Icon Zoom"), 0, 0.15, db.iconZoom or 0.08, "iconZoom", true,
            L("icon_zoom_desc", "Crop amount around the icon edges (0 = full icon, 0.15 = zoomed in).")
        )

        -- =========================================================================
        -- Talent Reminder (because we all forget)
        -- =========================================================================
        local talentTitle = AC:CreateStyledText(g, L("talent_reminder_title", "Talent Reminder"), 14, COLORS.TEXT, "OVERLAY", "")
        talentTitle:SetPoint("TOPLEFT", zoomRow, "BOTTOMLEFT", 0, -18)

        local talentDesc = AC:CreateStyledText(g, L("talent_reminder_desc", "Displays an on-screen reminder to check your talents during arena prep room and battleground starting room."), 11, COLORS.TEXT_MUTED, "OVERLAY", "")
        talentDesc:SetPoint("TOPLEFT", talentTitle, "BOTTOMLEFT", 0, -6)
        talentDesc:SetWidth(g:GetWidth() - 40)
        talentDesc:SetJustifyH("LEFT")
        talentDesc:SetWordWrap(true)

        local talentDb = AC.DB and AC.DB.profile and AC.DB.profile.talentReminder
        if not talentDb then talentDb = {} end

        local talentRow = CreateFrame("Frame", nil, g)
        talentRow:SetPoint("TOPLEFT", talentDesc, "BOTTOMLEFT", 0, -14)
        talentRow:SetPoint("RIGHT", g, "RIGHT", -20, 0)
        talentRow:SetHeight(26)

        local talentLabel = AC:CreateStyledText(talentRow, L("enable_talent_reminder", "Enable Talent Reminder"), 12, COLORS.TEXT_2, "OVERLAY", "")
        talentLabel:SetPoint("LEFT", 0, 0)
        talentLabel:SetPoint("RIGHT", -46, 0)
        talentLabel:SetJustifyH("LEFT")

        local talentCheckbox = AC:CreateFlatCheckbox(talentRow, 20, talentDb.enabled ~= false, function(value)
            if not AC.DB.profile.talentReminder then AC.DB.profile.talentReminder = {} end
            AC.DB.profile.talentReminder.enabled = value
            if AC.TalentReminder and AC.TalentReminder.SetEnabled then
                AC.TalentReminder:SetEnabled(value)
            end
        end)
        talentCheckbox:SetPoint("RIGHT", -10, 0)

        local talentTooltip = L("talent_reminder_tooltip", "Shows a text overlay during arena prep room and BG starting room reminding you to check talents. Hides automatically when the match begins.")
        talentRow:SetScript("OnEnter", function(self)
            GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
            GameTooltip:SetText(talentTooltip, 1, 1, 1, 1, true)
            GameTooltip:Show()
        end)
        talentRow:SetScript("OnLeave", function() GameTooltip:Hide() end)

        -- =========================================================================
        -- Gear Reminder (no PvE gear allowed)
        -- =========================================================================
        local gearTitle = AC:CreateStyledText(g, L("gear_reminder_title", "Gear Reminder"), 14, COLORS.TEXT, "OVERLAY", "")
        gearTitle:SetPoint("TOPLEFT", talentRow, "BOTTOMLEFT", 0, -18)

        local gearDesc = AC:CreateStyledText(g, L("gear_reminder_desc", "Checks your equipped gear during arena prep room and battleground starting room. Warns you if any item does not scale in PvP (PvE gear)."), 11, COLORS.TEXT_MUTED, "OVERLAY", "")
        gearDesc:SetPoint("TOPLEFT", gearTitle, "BOTTOMLEFT", 0, -6)
        gearDesc:SetWidth(g:GetWidth() - 40)
        gearDesc:SetJustifyH("LEFT")
        gearDesc:SetWordWrap(true)

        local gearDb = AC.DB and AC.DB.profile and AC.DB.profile.gearReminder
        if not gearDb then gearDb = {} end

        local gearRow = CreateFrame("Frame", nil, g)
        gearRow:SetPoint("TOPLEFT", gearDesc, "BOTTOMLEFT", 0, -14)
        gearRow:SetPoint("RIGHT", g, "RIGHT", -20, 0)
        gearRow:SetHeight(26)

        local gearLabel = AC:CreateStyledText(gearRow, L("enable_gear_reminder", "Enable Gear Reminder"), 12, COLORS.TEXT_2, "OVERLAY", "")
        gearLabel:SetPoint("LEFT", 0, 0)
        gearLabel:SetPoint("RIGHT", -46, 0)
        gearLabel:SetJustifyH("LEFT")

        local gearCheckbox = AC:CreateFlatCheckbox(gearRow, 20, gearDb.enabled ~= false, function(value)
            if not AC.DB.profile.gearReminder then AC.DB.profile.gearReminder = {} end
            AC.DB.profile.gearReminder.enabled = value
            if AC.GearReminder and AC.GearReminder.SetEnabled then
                AC.GearReminder:SetEnabled(value)
            end
        end)
        gearCheckbox:SetPoint("RIGHT", -10, 0)

        local gearTooltip = L("gear_reminder_tooltip", "Scans all equipped items during prep room. If any piece does not have PvP item-level scaling (PvE gear), a warning appears so you can swap before the match starts. Hides automatically when gates open.")
        gearRow:SetScript("OnEnter", function(self)
            GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
            GameTooltip:SetText(gearTooltip, 1, 1, 1, 1, true)
            GameTooltip:Show()
        end)
        gearRow:SetScript("OnLeave", function() GameTooltip:Hide() end)

        -- =========================================================================
        -- Honor Cap Reminder (don't let points rot)
        -- =========================================================================
        local honorTitle = AC:CreateStyledText(g, L("honor_cap_reminder_title", "Honor Cap Reminder"), 14, COLORS.TEXT, "OVERLAY", "")
        honorTitle:SetPoint("TOPLEFT", gearRow, "BOTTOMLEFT", 0, -18)

        local honorDesc = AC:CreateStyledText(g, L("honor_cap_reminder_desc", "Alerts you when your honor points reach a threshold. Go spend them before they go to waste!"), 11, COLORS.TEXT_MUTED, "OVERLAY", "")
        honorDesc:SetPoint("TOPLEFT", honorTitle, "BOTTOMLEFT", 0, -6)
        honorDesc:SetWidth(g:GetWidth() - 40)
        honorDesc:SetJustifyH("LEFT")
        honorDesc:SetWordWrap(true)

        local honorDb = AC.DB and AC.DB.profile and AC.DB.profile.honorCapReminder
        if not honorDb then honorDb = {} end

        local honorRow = CreateFrame("Frame", nil, g)
        honorRow:SetPoint("TOPLEFT", honorDesc, "BOTTOMLEFT", 0, -14)
        honorRow:SetPoint("RIGHT", g, "RIGHT", -20, 0)
        honorRow:SetHeight(26)

        local honorLabel = AC:CreateStyledText(honorRow, L("enable_honor_cap_reminder", "Enable Honor Cap Reminder"), 12, COLORS.TEXT_2, "OVERLAY", "")
        honorLabel:SetPoint("LEFT", 0, 0)
        honorLabel:SetPoint("RIGHT", -46, 0)
        honorLabel:SetJustifyH("LEFT")

        local honorCheckbox = AC:CreateFlatCheckbox(honorRow, 20, honorDb.enabled == true, function(value)
            if not AC.DB.profile.honorCapReminder then AC.DB.profile.honorCapReminder = {} end
            AC.DB.profile.honorCapReminder.enabled = value
            if AC.HonorCapReminder and AC.HonorCapReminder.Toggle then
                AC.HonorCapReminder:Toggle()
            end
        end)
        honorCheckbox:SetPoint("RIGHT", -10, 0)

        local honorTooltip = L("honor_cap_reminder_tooltip", "Shows a pulsing alert when your honor points reach the set threshold. Drag the alert to reposition. Dismisses for the session only.")
        honorRow:SetScript("OnEnter", function(self)
            GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
            GameTooltip:SetText(honorTooltip, 1, 1, 1, 1, true)
            GameTooltip:Show()
        end)
        honorRow:SetScript("OnLeave", function() GameTooltip:Hide() end)

        -- When should we start yelling about honor?
        local thresholdLabel = AC:CreateStyledText(g, L("alert_threshold", "Alert Threshold:"), 11, COLORS.TEXT_2, "OVERLAY", "")
        thresholdLabel:SetPoint("TOPLEFT", honorRow, "BOTTOMLEFT", 0, -14)
        thresholdLabel:SetWidth(100)
        thresholdLabel:SetJustifyH("LEFT")

        local thresholdInput = CreateFrame("EditBox", nil, g, "BackdropTemplate")
        thresholdInput:SetSize(100, 24)
        thresholdInput:SetPoint("LEFT", thresholdLabel, "RIGHT", 10, 0)
        thresholdInput:SetBackdrop({
            bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
            edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
            tile = true, tileSize = 16, edgeSize = 1,
            insets = { left = 1, right = 1, top = 1, bottom = 1 }
        })
        thresholdInput:SetBackdropColor(0.1, 0.1, 0.1, 0.9)
        thresholdInput:SetBackdropBorderColor(0.3, 0.3, 0.3, 1)
        thresholdInput:SetFont(AC.FONT_PATH or "Fonts\\FRIZQT__.ttf", 11, "")
        thresholdInput:SetTextColor(0.9, 0.9, 0.9, 1)
        thresholdInput:SetAutoFocus(false)
        thresholdInput:SetMaxLetters(6)
        thresholdInput:SetNumeric(true)
        thresholdInput:SetText(tostring(honorDb.threshold or 12000))
        thresholdInput:SetCursorPosition(0)

        thresholdInput:SetScript("OnEnter", function(self)
            self:SetBackdropColor(0.4, 0.15, 0.5, 0.9)
            self:SetBackdropBorderColor(0.8, 0.4, 0.9, 1)
        end)

        thresholdInput:SetScript("OnLeave", function(self)
            self:SetBackdropColor(0.1, 0.1, 0.1, 0.9)
            self:SetBackdropBorderColor(0.3, 0.3, 0.3, 1)
        end)

        local function SaveThresholdInput(self)
            local value = tonumber(self:GetText())
            if value and value >= 0 then
                if not AC.DB.profile.honorCapReminder then AC.DB.profile.honorCapReminder = {} end
                AC.DB.profile.honorCapReminder.threshold = value
                if AC.HonorCapReminder and AC.HonorCapReminder.UpdateThreshold then
                    AC.HonorCapReminder:UpdateThreshold()
                end
            else
                self:SetText(tostring((AC.DB.profile.honorCapReminder and AC.DB.profile.honorCapReminder.threshold) or 12000))
            end
        end

        thresholdInput:SetScript("OnEnterPressed", function(self)
            SaveThresholdInput(self)
            self:ClearFocus()
        end)

        thresholdInput:SetScript("OnEditFocusLost", function(self)
            SaveThresholdInput(self)
        end)

        thresholdInput:SetScript("OnEscapePressed", function(self)
            self:SetText(tostring((AC.DB.profile.honorCapReminder and AC.DB.profile.honorCapReminder.threshold) or 12000))
            self:ClearFocus()
        end)

        -- Reset button for when you drag it off-screen
        local resetBtn = CreateFrame("Button", nil, g, "BackdropTemplate")
        resetBtn:SetSize(140, 24)
        resetBtn:SetPoint("TOPLEFT", thresholdLabel, "BOTTOMLEFT", 0, -12)
        resetBtn:SetBackdrop({
            bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
            edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
            tile = true, tileSize = 16, edgeSize = 1,
            insets = { left = 1, right = 1, top = 1, bottom = 1 }
        })
        resetBtn:SetBackdropColor(0.1, 0.1, 0.1, 0.9)
        resetBtn:SetBackdropBorderColor(0.3, 0.3, 0.3, 1)

        local resetText = resetBtn:CreateFontString(nil, "OVERLAY")
        resetText:SetFont(AC.FONT_PATH or "Fonts\\FRIZQT__.ttf", 11)
        resetText:SetText(L("reset_alert_position", "Reset Alert Position"))
        resetText:SetTextColor(0.9, 0.9, 0.9, 1)
        resetText:SetPoint("CENTER", 0, 0)

        resetBtn:SetScript("OnEnter", function(self)
            self:SetBackdropColor(0.4, 0.15, 0.5, 0.9)
            self:SetBackdropBorderColor(0.8, 0.4, 0.9, 1)
            resetText:SetTextColor(1, 1, 1, 1)
        end)

        resetBtn:SetScript("OnLeave", function(self)
            self:SetBackdropColor(0.1, 0.1, 0.1, 0.9)
            self:SetBackdropBorderColor(0.3, 0.3, 0.3, 1)
            resetText:SetTextColor(0.9, 0.9, 0.9, 1)
        end)

        resetBtn:SetScript("OnClick", function()
            if AC.HonorCapReminder and AC.HonorCapReminder.ResetPosition then
                AC.HonorCapReminder:ResetPosition()
            end
        end)

        f._built = true
    end

    local function BuildPvPQueue()
        local f = EnsureContentFrame("pvpqueue")
        if f._built then return end

        local g = CreateFrame("Frame", nil, f)
        g:SetPoint("TOPLEFT", 18, -12)
        g:SetPoint("TOPRIGHT", -18, -12)
        g:SetHeight(280)
        AC:HairlineGroupBox(g)

        local sectionTitle = AC:CreateStyledText(g, L("page_title", "Arena Arsenal"), 13, COLORS.PRIMARY, "OVERLAY", "")
        sectionTitle:SetPoint("TOPLEFT", 20, -18)

        local tabTitle = AC:CreateStyledText(g, L("tab_pvp_queue", "PvP Queue Notify"), 14, COLORS.TEXT, "OVERLAY", "")
        tabTitle:SetPoint("TOPLEFT", sectionTitle, "BOTTOMLEFT", 0, -18)

        local descText = AC:CreateStyledText(g, L("pvp_queue_desc", "Flashes a green overlay and plays a sound when your selected queue types pop."), 11, COLORS.TEXT_MUTED, "OVERLAY", "")
        descText:SetPoint("TOPLEFT", tabTitle, "BOTTOMLEFT", 0, -6)
        descText:SetWidth(g:GetWidth() - 40)
        descText:SetJustifyH("LEFT")

        -- Master switch for the whole thing
        local db = AC.DB and AC.DB.profile and AC.DB.profile.pvpQueueNotify
        if not db then db = {} end

        local enableRow = CreateFrame("Frame", nil, g)
        enableRow:SetPoint("TOPLEFT", descText, "BOTTOMLEFT", 0, -14)
        enableRow:SetPoint("RIGHT", g, "RIGHT", -20, 0)
        enableRow:SetHeight(26)

        local enableLabel = AC:CreateStyledText(enableRow, L("enable_pvp_queue_notify", "Enable PvP Queue Notify"), 12, COLORS.TEXT_2, "OVERLAY", "")
        enableLabel:SetPoint("LEFT", 0, 0)
        enableLabel:SetPoint("RIGHT", -46, 0)
        enableLabel:SetJustifyH("LEFT")

        local enableCheckbox = AC:CreateFlatCheckbox(enableRow, 20, db.enabled == true, function(value)
            if not AC.DB.profile.pvpQueueNotify then AC.DB.profile.pvpQueueNotify = {} end
            AC.DB.profile.pvpQueueNotify.enabled = value
            if AC.SetPvPQueueNotifyEnabled then
                AC:SetPvPQueueNotifyEnabled(value)
            end
        end)
        enableCheckbox:SetPoint("RIGHT", -10, 0)

        -- Explain what this actually does
        local tooltipText = L("pvp_queue_tooltip", "Flashes a green screen overlay and plays a sound when a PvP queue pops. Press ESC to cancel.")
        enableRow:SetScript("OnEnter", function(self)
            GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
            GameTooltip:SetText(tooltipText, 1, 1, 1, 1, true)
            GameTooltip:Show()
        end)
        enableRow:SetScript("OnLeave", function() GameTooltip:Hide() end)

        -- Which queues should we actually care about?
        local modeOptions = {
            { key = "randomBattlegrounds", label = L("random_battlegrounds", "Random Battlegrounds") },
            { key = "randomEpicBattlegrounds", label = L("random_epic_battlegrounds", "Random Epic BGs") },
            { key = "arenaSkirmish", label = L("arena_skirmish", "Arena Skirmish") },
            { key = "brawl", label = L("brawl", "Brawl") },
            { key = "rated2v2", label = L("rated_2v2", "2v2 Rated") },
            { key = "rated3v3", label = L("rated_3v3", "3v3 Rated") },
            { key = "soloShuffle", label = L("solo_shuffle", "Solo Shuffle") },
            { key = "soloBlitz", label = L("solo_blitz", "Solo Blitz") },
        }

        local notifyModes = AC:GetPvPQueueNotifyModeConfig()

        local function GetEnabledCount()
            local count = 0
            for _, info in ipairs(modeOptions) do
                if notifyModes[info.key] ~= false then
                    count = count + 1
                end
            end
            return count
        end

        local modeLabel = AC:CreateStyledText(g, L("queue_types_label", "Queue Types:"), 11, COLORS.TEXT_MUTED, "OVERLAY", "")
        modeLabel:SetPoint("TOPLEFT", enableRow, "BOTTOMLEFT", 0, -16)

        local modeDropdown = CreateFrame("Frame", nil, g)
        modeDropdown:SetSize(170, 22)
        modeDropdown:SetPoint("TOPLEFT", modeLabel, "BOTTOMLEFT", 0, -6)

        local modeButton = CreateFrame("Button", nil, modeDropdown, "BackdropTemplate")
        modeButton:SetAllPoints(modeDropdown)
        modeButton:SetBackdrop({
            bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
            edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
            tile = true, tileSize = 16, edgeSize = 1,
            insets = { left = 1, right = 1, top = 1, bottom = 1 }
        })
        modeButton:SetBackdropColor(0.1, 0.1, 0.1, 0.9)
        modeButton:SetBackdropBorderColor(0.3, 0.3, 0.3, 1)

        local modeText = modeButton:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        modeText:SetPoint("LEFT", 8, 0)
        modeText:SetTextColor(0.9, 0.9, 0.9, 1)
        if modeText.SetFont then
            modeText:SetFont(AC.FONT_PATH or "Interface/AddOns/ArenaCore/Media/Fonts/arenacore.ttf", 10, "")
        end

        local function RefreshModeButtonText()
            modeText:SetText(string.format(L("enabled_count_format", "%d / %d Enabled"), GetEnabledCount(), #modeOptions))
        end
        RefreshModeButtonText()

        local modeArrow = modeButton:CreateTexture(nil, "OVERLAY")
        modeArrow:SetSize(12, 12)
        modeArrow:SetPoint("RIGHT", -8, 0)
        modeArrow:SetTexture("Interface\\Buttons\\UI-SpellbookIcon-NextPage-Up")
        modeArrow:SetTexCoord(1, 0, 0, 1)
        modeArrow:SetVertexColor(0.6, 0.6, 0.6, 1)

        local modePopup = CreateFrame("Frame", nil, UIParent, "BackdropTemplate")
        modePopup:SetSize(190, (#modeOptions * 22) + 8)
        modePopup:SetBackdrop({
            bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
            edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
            tile = true, tileSize = 16, edgeSize = 1,
            insets = { left = 1, right = 1, top = 1, bottom = 1 }
        })
        modePopup:SetBackdropColor(0.05, 0.05, 0.05, 0.95)
        modePopup:SetBackdropBorderColor(0.6, 0.2, 0.8, 1)
        modePopup:SetFrameStrata("TOOLTIP")
        modePopup:SetFrameLevel(9999)
        modePopup:Hide()

        local function HideModePopup()
            modePopup:Hide()
        end

        local function UpdateModePopupPosition()
            if not modePopup:IsShown() then return end
            local x, yPos = modeButton:GetCenter()
            if not x or not yPos then
                HideModePopup()
                return
            end
            modePopup:ClearAllPoints()
            modePopup:SetPoint("TOP", UIParent, "BOTTOMLEFT", x, yPos - (modeButton:GetHeight() / 2))
        end

        for i, info in ipairs(modeOptions) do
            local rowCheck = AC:CreateFlatCheckbox(modePopup, 16, notifyModes[info.key] ~= false, function(checked)
                notifyModes[info.key] = (checked == true)
                if not AC.DB.profile.pvpQueueNotify then AC.DB.profile.pvpQueueNotify = {} end
                if not AC.DB.profile.pvpQueueNotify.modes then AC.DB.profile.pvpQueueNotify.modes = {} end
                AC.DB.profile.pvpQueueNotify.modes[info.key] = (checked == true)
                RefreshModeButtonText()
            end)
            rowCheck:SetPoint("TOPLEFT", 10, -8 - ((i-1) * 22))

            local rowLabel = AC:CreateStyledText(modePopup, info.label, 11, COLORS.TEXT_2, "OVERLAY", "")
            rowLabel:SetPoint("LEFT", rowCheck, "RIGHT", 6, 0)
        end

        modeButton:SetScript("OnClick", function()
            if modePopup:IsShown() then
                HideModePopup()
            else
                UpdateModePopupPosition()
                modePopup:Show()
            end
        end)

        modeButton:SetScript("OnEnter", function()
            modeButton:SetBackdropColor(0.4, 0.15, 0.5, 0.9)
            modeButton:SetBackdropBorderColor(0.8, 0.4, 0.9, 1)
            modeText:SetTextColor(1, 1, 1, 1)
            modeArrow:SetVertexColor(0.8, 0.4, 0.9, 1)
        end)

        modeButton:SetScript("OnLeave", function()
            modeButton:SetBackdropColor(0.1, 0.1, 0.1, 0.9)
            modeButton:SetBackdropBorderColor(0.3, 0.3, 0.3, 1)
            modeText:SetTextColor(0.9, 0.9, 0.9, 1)
            modeArrow:SetVertexColor(0.6, 0.6, 0.6, 1)
        end)

        modePopup:SetScript("OnUpdate", function()
            UpdateModePopupPosition()
        end)

        -- Don't leave the popup hanging when the UI closes
        if AC.configFrame then
            AC.configFrame:HookScript("OnHide", HideModePopup)
        end

        f._built = true
    end

    local function BuildPartyTrinkets()
        local f = EnsureContentFrame("party")
        if f._built then return end

        local g = CreateFrame("Frame", nil, f)
        g:SetPoint("TOPLEFT", 18, -12)
        g:SetPoint("TOPRIGHT", -18, -12)
        g:SetHeight(620)
        AC:HairlineGroupBox(g)

        local sectionTitle = AC:CreateStyledText(g, L("page_title", "Arena Arsenal"), 13, COLORS.PRIMARY, "OVERLAY", "")
        sectionTitle:SetPoint("TOPLEFT", 20, -18)

        local tabTitle = AC:CreateStyledText(g, L("tab_party_trinkets", "Party Trinkets"), 14, COLORS.TEXT, "OVERLAY", "")
        tabTitle:SetPoint("TOPLEFT", sectionTitle, "BOTTOMLEFT", 0, -18)

        local descText = AC:CreateStyledText(g, L("party_trinkets_desc", "Show friendly party members PvP trinket cooldowns on party frames."), 11, COLORS.TEXT_MUTED, "OVERLAY", "")
        descText:SetPoint("TOPLEFT", tabTitle, "BOTTOMLEFT", 0, -6)
        descText:SetWidth(g:GetWidth() - 40)
        descText:SetJustifyH("LEFT")
        descText:SetWordWrap(true)

        local db = AC.DB and AC.DB.profile and AC.DB.profile.partyTrinkets
        if not db then db = {} end

        -- Slider helper with +/- buttons (borrowed from BigDebuffs pattern)
        local function CreateSliderRow(parent, anchorFrame, anchorPoint, labelTextStr, minVal, maxVal, currentVal, dbKey, tooltipStr)
            local row = CreateFrame("Frame", nil, parent)
            row:SetHeight(26)
            row:SetPoint("TOPLEFT", anchorFrame, anchorPoint, 0, -14)
            row:SetPoint("RIGHT", parent, "RIGHT", -20, 0)

            local label = AC:CreateStyledText(row, labelTextStr, 11, COLORS.TEXT_2, "OVERLAY", "")
            label:SetPoint("LEFT", 0, 0)
            label:SetWidth(100)
            label:SetJustifyH("LEFT")

            local valueText = AC:CreateStyledText(row, "", 11, COLORS.TEXT, "OVERLAY", "")
            valueText:SetPoint("RIGHT", -10, 0)
            valueText:SetWidth(50)
            valueText:SetJustifyH("RIGHT")

            -- Minus button for fine-tuning
            local downBtn = CreateFrame("Button", nil, row)
            downBtn:SetSize(16, 16)
            downBtn:SetPoint("LEFT", label, "RIGHT", 6, 0)
            local downBg = downBtn:CreateTexture(nil, "BACKGROUND")
            downBg:SetAllPoints(); downBg:SetColorTexture(0.2, 0.2, 0.2, 0.8)
            local downBorder = downBtn:CreateTexture(nil, "BORDER")
            downBorder:SetAllPoints(); downBorder:SetColorTexture(0.4, 0.4, 0.4, 1)
            downBorder:SetPoint("TOPLEFT", 1, -1); downBorder:SetPoint("BOTTOMRIGHT", -1, 1)
            local downTxt = downBtn:CreateFontString(nil, "OVERLAY")
            downTxt:SetFont(AC.FONT_PATH or "Interface/AddOns/ArenaCore/Media/Fonts/arenacore.ttf", 12, "")
            downTxt:SetText("-"); downTxt:SetTextColor(0.8, 0.8, 0.8, 1); downTxt:SetPoint("CENTER")

            local function OnSliderChange(val)
                if not AC.DB.profile.partyTrinkets then AC.DB.profile.partyTrinkets = {} end
                AC.DB.profile.partyTrinkets[dbKey] = math.floor(val + 0.5)
                valueText:SetText(string.format("%.0f", val))
                if AC.PartyTrinkets and AC.PartyTrinkets.Apply then
                    AC.PartyTrinkets:Apply()
                end
            end

            local sliderContainer = AC:CreateFlatSlider(row, 140, 18, minVal, maxVal, currentVal, false, OnSliderChange)
            sliderContainer:SetPoint("LEFT", downBtn, "RIGHT", 4, 0)

            -- Plus button for fine-tuning
            local upBtn = CreateFrame("Button", nil, row)
            upBtn:SetSize(16, 16)
            upBtn:SetPoint("LEFT", sliderContainer, "RIGHT", 4, 0)
            local upBg = upBtn:CreateTexture(nil, "BACKGROUND")
            upBg:SetAllPoints(); upBg:SetColorTexture(0.2, 0.2, 0.2, 0.8)
            local upBorder = upBtn:CreateTexture(nil, "BORDER")
            upBorder:SetAllPoints(); upBorder:SetColorTexture(0.4, 0.4, 0.4, 1)
            upBorder:SetPoint("TOPLEFT", 1, -1); upBorder:SetPoint("BOTTOMRIGHT", -1, 1)
            local upTxt = upBtn:CreateFontString(nil, "OVERLAY")
            upTxt:SetFont(AC.FONT_PATH or "Interface/AddOns/ArenaCore/Media/Fonts/arenacore.ttf", 12, "")
            upTxt:SetText("+"); upTxt:SetTextColor(0.8, 0.8, 0.8, 1); upTxt:SetPoint("CENTER")

            valueText:SetText(string.format("%.0f", currentVal))

            -- Click handlers
            local step = 1
            downBtn:SetScript("OnClick", function()
                local v = sliderContainer.slider:GetValue()
                local newVal = math.max(minVal, v - step)
                sliderContainer.slider:SetValue(newVal)
                OnSliderChange(newVal)
            end)
            upBtn:SetScript("OnClick", function()
                local v = sliderContainer.slider:GetValue()
                local newVal = math.min(maxVal, v + step)
                sliderContainer.slider:SetValue(newVal)
                OnSliderChange(newVal)
            end)

            if tooltipStr then
                row:SetScript("OnEnter", function(self)
                    GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
                    GameTooltip:SetText(tooltipStr, 1, 1, 1, 1, true)
                    GameTooltip:Show()
                end)
                row:SetScript("OnLeave", function() GameTooltip:Hide() end)
            end
            return row
        end

        -- Enable checkbox
        local enableRow = CreateFrame("Frame", nil, g)
        enableRow:SetPoint("TOPLEFT", descText, "BOTTOMLEFT", 0, -14)
        enableRow:SetPoint("RIGHT", g, "RIGHT", -20, 0)
        enableRow:SetHeight(26)

        local enableLabel = AC:CreateStyledText(enableRow, L("enable_party_trinkets", "Enable Party Trinkets"), 12, COLORS.TEXT_2, "OVERLAY", "")
        enableLabel:SetPoint("LEFT", 0, 0)
        enableLabel:SetPoint("RIGHT", -46, 0)
        enableLabel:SetJustifyH("LEFT")

        local enableCheckbox = AC:CreateFlatCheckbox(enableRow, 20, db.enabled == true, function(value)
            if not AC.DB.profile.partyTrinkets then AC.DB.profile.partyTrinkets = {} end
            AC.DB.profile.partyTrinkets.enabled = value
            if AC.PartyTrinkets and AC.PartyTrinkets.Apply then
                AC.PartyTrinkets:Apply()
            end
        end)
        enableCheckbox:SetPoint("RIGHT", -10, 0)

        -- Explain what this actually does
        local enableTooltip = L("party_trinkets_tooltip", "Shows PvP trinket cooldowns on friendly party frames in arena.")
        enableRow:SetScript("OnEnter", function(self)
            GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
            GameTooltip:SetText(enableTooltip, 1, 1, 1, 1, true)
            GameTooltip:Show()
        end)
        enableRow:SetScript("OnLeave", function() GameTooltip:Hide() end)

        -- Should we show your own trinket too?
        local ownRow = CreateFrame("Frame", nil, g)
        ownRow:SetPoint("TOPLEFT", enableRow, "BOTTOMLEFT", 0, -8)
        ownRow:SetPoint("RIGHT", g, "RIGHT", -20, 0)
        ownRow:SetHeight(26)

        local ownLabel = AC:CreateStyledText(ownRow, L("show_own_trinket", "Show Your Own Trinket"), 12, COLORS.TEXT_2, "OVERLAY", "")
        ownLabel:SetPoint("LEFT", 0, 0)
        ownLabel:SetPoint("RIGHT", -46, 0)
        ownLabel:SetJustifyH("LEFT")

        local ownCheckbox = AC:CreateFlatCheckbox(ownRow, 20, db.showOwnTrinket ~= false, function(value)
            if not AC.DB.profile.partyTrinkets then AC.DB.profile.partyTrinkets = {} end
            AC.DB.profile.partyTrinkets.showOwnTrinket = value
            if AC.PartyTrinkets and AC.PartyTrinkets.Apply then
                AC.PartyTrinkets:Apply()
            end
        end)
        ownCheckbox:SetPoint("RIGHT", -10, 0)

        -- Explain what this actually does
        local ownTooltip = L("show_own_trinket_tooltip", "Also show your own trinket cooldown on your party frame.")
        ownRow:SetScript("OnEnter", function(self)
            GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
            GameTooltip:SetText(ownTooltip, 1, 1, 1, 1, true)
            GameTooltip:Show()
        end)
        ownRow:SetScript("OnLeave", function() GameTooltip:Hide() end)

        -- Hide it when the trinket is ready (less clutter)
        local cooldownRow = CreateFrame("Frame", nil, g)
        cooldownRow:SetPoint("TOPLEFT", ownRow, "BOTTOMLEFT", 0, -8)
        cooldownRow:SetPoint("RIGHT", g, "RIGHT", -20, 0)
        cooldownRow:SetHeight(26)

        local cooldownLabel = AC:CreateStyledText(cooldownRow, L("only_show_on_cooldown", "Only Show When On Cooldown"), 12, COLORS.TEXT_2, "OVERLAY", "")
        cooldownLabel:SetPoint("LEFT", 0, 0)
        cooldownLabel:SetPoint("RIGHT", -46, 0)
        cooldownLabel:SetJustifyH("LEFT")

        local cooldownCheckbox = AC:CreateFlatCheckbox(cooldownRow, 20, db.onlyShowOnCooldown == true, function(value)
            if not AC.DB.profile.partyTrinkets then AC.DB.profile.partyTrinkets = {} end
            AC.DB.profile.partyTrinkets.onlyShowOnCooldown = value
            if AC.PartyTrinkets and AC.PartyTrinkets.Apply then
                AC.PartyTrinkets:Apply()
            end
        end)
        cooldownCheckbox:SetPoint("RIGHT", -10, 0)

        -- Explain what this actually does
        local cooldownTooltip = L("only_show_on_cooldown_tooltip", "Only shows the trinket icon while it is on cooldown. Hides it again once the cooldown finishes and the trinket is available.")
        cooldownRow:SetScript("OnEnter", function(self)
            GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
            GameTooltip:SetText(cooldownTooltip, 1, 1, 1, 1, true)
            GameTooltip:Show()
        end)
        cooldownRow:SetScript("OnLeave", function() GameTooltip:Hide() end)

        -- Solo Raid Frame checkbox
        local soloRow = CreateFrame("Frame", nil, g)
        soloRow:SetPoint("TOPLEFT", cooldownRow, "BOTTOMLEFT", 0, -8)
        soloRow:SetPoint("RIGHT", g, "RIGHT", -20, 0)
        soloRow:SetHeight(26)

        local soloLabel = AC:CreateStyledText(soloRow, L("show_in_solo_raid_frame", "Show in Solo (Raid Frame)"), 12, COLORS.TEXT_2, "OVERLAY", "")
        soloLabel:SetPoint("LEFT", 0, 0)
        soloLabel:SetPoint("RIGHT", -46, 0)
        soloLabel:SetJustifyH("LEFT")

        local soloCheckbox = AC:CreateFlatCheckbox(soloRow, 20, db.soloRaidFrame == true, function(value)
            if not AC.DB.profile.partyTrinkets then AC.DB.profile.partyTrinkets = {} end
            AC.DB.profile.partyTrinkets.soloRaidFrame = value
            if AC.PartyTrinkets and AC.PartyTrinkets.UpdateSoloRaidFrame then
                AC.PartyTrinkets:UpdateSoloRaidFrame()
            end
        end)
        soloCheckbox:SetPoint("RIGHT", -10, 0)

        -- Explain what this actually does
        local soloTooltip = L("show_in_solo_tooltip", "Show party frames when solo so you can position and preview trinkets.")
        soloRow:SetScript("OnEnter", function(self)
            GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
            GameTooltip:SetText(soloTooltip, 1, 1, 1, 1, true)
            GameTooltip:Show()
        end)
        soloRow:SetScript("OnLeave", function() GameTooltip:Hide() end)

        -- Pick your border flavor
        local styleRow = CreateFrame("Frame", nil, g)
        styleRow:SetHeight(30)
        styleRow:SetPoint("TOPLEFT", soloRow, "BOTTOMLEFT", 0, -10)
        styleRow:SetPoint("RIGHT", g, "RIGHT", -20, 0)

        local styleLabel = AC:CreateStyledText(styleRow, L("border_style", "Border Style"), 12, COLORS.TEXT_2, "OVERLAY", "")
        styleLabel:SetPoint("LEFT", 0, 0)
        styleLabel:SetWidth(100)
        styleLabel:SetJustifyH("LEFT")

        local styleOptions = { L("style_arenacore", "Arena Core"), L("style_arenacore_black", "Arena Core Black"), L("style_skill_issue_outline", "Skill Issue Outline") }
        local styleValues  = { arenacore = L("style_arenacore", "Arena Core"), arenacore_black = L("style_arenacore_black", "Arena Core Black"), pixel_black = L("style_skill_issue_outline", "Skill Issue Outline") }
        local currentStyle = db.style or "arenacore"
        local currentDisplay = styleValues[currentStyle] or L("style_arenacore", "Arena Core")

        local styleDropdown = AC:CreateFlatDropdown(styleRow, 150, 22, styleOptions, currentDisplay, function(displayText)
            local valueMap = { [L("style_arenacore", "Arena Core")] = "arenacore", [L("style_arenacore_black", "Arena Core Black")] = "arenacore_black", [L("style_skill_issue_outline", "Skill Issue Outline")] = "pixel_black" }
            local val = valueMap[displayText] or "arenacore"
            if not AC.DB.profile.partyTrinkets then AC.DB.profile.partyTrinkets = {} end
            AC.DB.profile.partyTrinkets.style = val
            if AC.PartyTrinkets and AC.PartyTrinkets.Apply then
                AC.PartyTrinkets:Apply()
            end
        end)
        styleDropdown:SetPoint("LEFT", styleLabel, "RIGHT", 10, 0)

        -- Border thickness (matters for pixel borders, kept for the rest)
        local thicknessRow = CreateSliderRow(g, styleRow, "BOTTOMLEFT", L("border_thickness_pt", "Border Thickness"), 1, 5, db.borderThickness or 2, "borderThickness",
            L("border_thickness_pt_tooltip", "Thickness of the pixel border when Skill Issue Outline is active."))

        -- Sliders
        local scaleRow = CreateSliderRow(g, thicknessRow, "BOTTOMLEFT", L("icon_scale_pt", "Icon Scale"), 50, 300, db.iconScale or 100, "iconScale",
            L("icon_scale_pt_tooltip", "Scales the trinket icon relative to the compact frame height."))
        local xRow = CreateSliderRow(g, scaleRow, "BOTTOMLEFT", L("horizontal_offset_pt", "Horizontal"), -250, 250, db.offsetX or 0, "offsetX",
            L("horizontal_offset_pt_tooltip", "Horizontal offset from the center of the party frame."))
        local yRow = CreateSliderRow(g, xRow, "BOTTOMLEFT", L("vertical_offset_pt", "Vertical"), -250, 250, db.offsetY or 0, "offsetY",
            L("vertical_offset_pt_tooltip", "Vertical offset from the center of the party frame."))

        -- Test button to see it in action
        local testRow = CreateFrame("Frame", nil, g)
        testRow:SetHeight(32)
        testRow:SetPoint("TOPLEFT", yRow, "BOTTOMLEFT", 0, -14)
        testRow:SetPoint("RIGHT", g, "RIGHT", -20, 0)

        local testBtn = CreateFrame("Button", nil, testRow, "BackdropTemplate")
        testBtn:SetSize(90, 26)
        testBtn:SetPoint("LEFT", 0, 0)
        testBtn:SetBackdrop({
            bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
            edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
            tile = true, tileSize = 8, edgeSize = 12,
            insets = { left = 2, right = 2, top = 2, bottom = 2 }
        })
        testBtn:SetBackdropColor(0.18, 0.14, 0.24, 1)
        testBtn:SetBackdropBorderColor(0.5, 0.3, 0.7, 1)

        local testBtnText = AC:CreateStyledText(testBtn, L("test_btn", "TEST"), 11, COLORS.TEXT, "OVERLAY", "")
        testBtnText:SetPoint("CENTER", 0, 0)

        testBtn:SetScript("OnEnter", function(self)
            self:SetBackdropColor(0.25, 0.18, 0.35, 1)
            self:SetBackdropBorderColor(0.7, 0.4, 0.9, 1)
        end)
        testBtn:SetScript("OnLeave", function(self)
            local isActive = AC.PartyTrinkets and AC.PartyTrinkets.IsTestActive and AC.PartyTrinkets:IsTestActive()
            if isActive then
                self:SetBackdropColor(0.3, 0.15, 0.15, 1)
                self:SetBackdropBorderColor(0.7, 0.2, 0.2, 1)
                testBtnText:SetText(L("hide_btn", "HIDE"))
            else
                self:SetBackdropColor(0.18, 0.14, 0.24, 1)
                self:SetBackdropBorderColor(0.5, 0.3, 0.7, 1)
                testBtnText:SetText(L("test_btn", "TEST"))
            end
        end)
        testBtn:SetScript("OnClick", function(self)
            if AC.PartyTrinkets and AC.PartyTrinkets.ToggleTest then
                AC.PartyTrinkets:ToggleTest()
            end
            local isActive = AC.PartyTrinkets and AC.PartyTrinkets.IsTestActive and AC.PartyTrinkets:IsTestActive()
            if isActive then
                self:SetBackdropColor(0.3, 0.15, 0.15, 1)
                self:SetBackdropBorderColor(0.7, 0.2, 0.2, 1)
                testBtnText:SetText(L("hide_btn", "HIDE"))
            else
                self:SetBackdropColor(0.18, 0.14, 0.24, 1)
                self:SetBackdropBorderColor(0.5, 0.3, 0.7, 1)
                testBtnText:SetText(L("test_btn", "TEST"))
            end
        end)

        local testNote = AC:CreateStyledText(testRow, L("test_note_party_trinkets", "Shows trinket icons on your real solo raid / party frames so you can position them live."), 10, COLORS.TEXT_MUTED, "OVERLAY", "")
        testNote:SetPoint("LEFT", testBtn, "RIGHT", 10, 0)
        testNote:SetWidth(g:GetWidth() - 160)
        testNote:SetJustifyH("LEFT")
        testNote:SetWordWrap(true)

        f._built = true
    end

    local function BuildBigDebuffs()
        local f = EnsureContentFrame("bigdebuffs")
        if f._built then return end

        local g = CreateFrame("Frame", nil, f)
        g:SetPoint("TOPLEFT", 18, -12)
        g:SetPoint("TOPRIGHT", -18, -12)
        g:SetHeight(780)
        AC:HairlineGroupBox(g)

        local sectionTitle = AC:CreateStyledText(g, L("page_title", "Arena Arsenal"), 13, COLORS.PRIMARY, "OVERLAY", "")
        sectionTitle:SetPoint("TOPLEFT", 20, -18)

        local tabTitle = AC:CreateStyledText(g, L("big_debuffs_tab_title", "Crowd Control"), 14, COLORS.TEXT, "OVERLAY", "")
        tabTitle:SetPoint("TOPLEFT", sectionTitle, "BOTTOMLEFT", 0, -18)

        local descText = AC:CreateStyledText(g, L("big_debuffs_desc", "Shows the highest priority crowd control effects on party and raid frames as a larger, customizable icon. Uses direct Blizzard data so you always know whos getting shafted."), 11, COLORS.TEXT_MUTED, "OVERLAY", "")
        descText:SetPoint("TOPLEFT", tabTitle, "BOTTOMLEFT", 0, -6)
        descText:SetWidth(g:GetWidth() - 40)
        descText:SetJustifyH("LEFT")
        descText:SetWordWrap(true)

        local db = AC.DB and AC.DB.profile and AC.DB.profile.bigDebuffs
        if not db then db = {} end

        -- Slider helper with +/- buttons for precision
        local function CreateSliderRow(parent, anchorFrame, anchorPoint, labelTextStr, minVal, maxVal, currentVal, dbKey, tooltipStr, isDecimal)
            local row = CreateFrame("Frame", nil, parent)
            row:SetHeight(26)
            row:SetPoint("TOPLEFT", anchorFrame, anchorPoint, 0, -14)
            row:SetPoint("RIGHT", parent, "RIGHT", -20, 0)

            local label = AC:CreateStyledText(row, labelTextStr, 11, COLORS.TEXT_2, "OVERLAY", "")
            label:SetPoint("LEFT", 0, 0)
            label:SetWidth(100)
            label:SetJustifyH("LEFT")

            local valueText = AC:CreateStyledText(row, "", 11, COLORS.TEXT, "OVERLAY", "")
            valueText:SetPoint("RIGHT", -10, 0)
            valueText:SetWidth(50)
            valueText:SetJustifyH("RIGHT")

            -- Minus button for when you need to be precise
            local downBtn = CreateFrame("Button", nil, row)
            downBtn:SetSize(16, 16)
            downBtn:SetPoint("LEFT", label, "RIGHT", 6, 0)
            local downBg = downBtn:CreateTexture(nil, "BACKGROUND")
            downBg:SetAllPoints(); downBg:SetColorTexture(0.2, 0.2, 0.2, 0.8)
            local downBorder = downBtn:CreateTexture(nil, "BORDER")
            downBorder:SetAllPoints(); downBorder:SetColorTexture(0.4, 0.4, 0.4, 1)
            downBorder:SetPoint("TOPLEFT", 1, -1); downBorder:SetPoint("BOTTOMRIGHT", -1, 1)
            local downTxt = downBtn:CreateFontString(nil, "OVERLAY")
            downTxt:SetFont(AC.FONT_PATH or "Interface/AddOns/ArenaCore/Media/Fonts/arenacore.ttf", 12, "")
            downTxt:SetText("-"); downTxt:SetTextColor(0.8, 0.8, 0.8, 1); downTxt:SetPoint("CENTER")

            local function OnSliderChange(val)
                if not AC.DB.profile.bigDebuffs then AC.DB.profile.bigDebuffs = {} end
                if isDecimal then
                    AC.DB.profile.bigDebuffs[dbKey] = tonumber(string.format("%.2f", val))
                    valueText:SetText(string.format("%.2f", val))
                else
                    AC.DB.profile.bigDebuffs[dbKey] = math.floor(val + 0.5)
                    valueText:SetText(string.format("%.0f", val))
                end
                if AC.BigDebuffs and AC.BigDebuffs.Apply then
                    AC.BigDebuffs:Apply()
                end
            end

            local sliderContainer = AC:CreateFlatSlider(row, 140, 18, minVal, maxVal, currentVal, false, OnSliderChange)
            sliderContainer:SetPoint("LEFT", downBtn, "RIGHT", 4, 0)
            if isDecimal and sliderContainer.slider then
                sliderContainer.slider:SetValueStep(0.01)
            end

            -- Plus button for when minus wasn't enough
            local upBtn = CreateFrame("Button", nil, row)
            upBtn:SetSize(16, 16)
            upBtn:SetPoint("LEFT", sliderContainer, "RIGHT", 4, 0)
            local upBg = upBtn:CreateTexture(nil, "BACKGROUND")
            upBg:SetAllPoints(); upBg:SetColorTexture(0.2, 0.2, 0.2, 0.8)
            local upBorder = upBtn:CreateTexture(nil, "BORDER")
            upBorder:SetAllPoints(); upBorder:SetColorTexture(0.4, 0.4, 0.4, 1)
            upBorder:SetPoint("TOPLEFT", 1, -1); upBorder:SetPoint("BOTTOMRIGHT", -1, 1)
            local upTxt = upBtn:CreateFontString(nil, "OVERLAY")
            upTxt:SetFont(AC.FONT_PATH or "Interface/AddOns/ArenaCore/Media/Fonts/arenacore.ttf", 12, "")
            upTxt:SetText("+"); upTxt:SetTextColor(0.8, 0.8, 0.8, 1); upTxt:SetPoint("CENTER")

            if isDecimal then
                valueText:SetText(string.format("%.2f", currentVal))
            else
                valueText:SetText(string.format("%.0f", currentVal))
            end

            -- Wire up the click handlers
            local step = isDecimal and 0.01 or 1
            downBtn:SetScript("OnClick", function()
                local v = sliderContainer.slider:GetValue()
                local newVal = math.max(minVal, v - step)
                sliderContainer.slider:SetValue(newVal)
                OnSliderChange(newVal)
            end)
            upBtn:SetScript("OnClick", function()
                local v = sliderContainer.slider:GetValue()
                local newVal = math.min(maxVal, v + step)
                sliderContainer.slider:SetValue(newVal)
                OnSliderChange(newVal)
            end)

            -- Hover effects
            downBtn:SetScript("OnEnter", function() downBg:SetColorTexture(0.3, 0.3, 0.3, 0.9); downTxt:SetTextColor(1, 1, 1, 1) end)
            downBtn:SetScript("OnLeave", function() downBg:SetColorTexture(0.2, 0.2, 0.2, 0.8); downTxt:SetTextColor(0.8, 0.8, 0.8, 1) end)
            upBtn:SetScript("OnEnter", function() upBg:SetColorTexture(0.3, 0.3, 0.3, 0.9); upTxt:SetTextColor(1, 1, 1, 1) end)
            upBtn:SetScript("OnLeave", function() upBg:SetColorTexture(0.2, 0.2, 0.2, 0.8); upTxt:SetTextColor(0.8, 0.8, 0.8, 1) end)

            if tooltipStr then
                row:SetScript("OnEnter", function(self)
                    GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
                    GameTooltip:SetText(tooltipStr, 1, 1, 1, 1, true)
                    GameTooltip:Show()
                end)
                row:SetScript("OnLeave", function() GameTooltip:Hide() end)
            end
            return row
        end

        -- Enable checkbox
        local enableRow = CreateFrame("Frame", nil, g)
        enableRow:SetPoint("TOPLEFT", descText, "BOTTOMLEFT", 0, -14)
        enableRow:SetPoint("RIGHT", g, "RIGHT", -20, 0)
        enableRow:SetHeight(26)

        local enableLabel = AC:CreateStyledText(enableRow, L("enable_big_debuffs", "Enable Big Debuffs"), 12, COLORS.TEXT_2, "OVERLAY", "")
        enableLabel:SetPoint("LEFT", 0, 0)
        enableLabel:SetPoint("RIGHT", -46, 0)
        enableLabel:SetJustifyH("LEFT")

        local enableCheckbox = AC:CreateFlatCheckbox(enableRow, 20, db.enabled == true, function(value)
            if not AC.DB.profile.bigDebuffs then AC.DB.profile.bigDebuffs = {} end
            AC.DB.profile.bigDebuffs.enabled = value
            if AC.BigDebuffs and AC.BigDebuffs.Apply then
                AC.BigDebuffs:Apply()
            end
        end)
        enableCheckbox:SetPoint("RIGHT", -10, 0)

        -- Explain what this actually does
        local enableTooltip = L("enable_big_debuffs_tooltip", "Shows the highest-priority active crowd control icon on compact party frames.")
        enableRow:SetScript("OnEnter", function(self)
            GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
            GameTooltip:SetText(enableTooltip, 1, 1, 1, 1, true)
            GameTooltip:Show()
        end)
        enableRow:SetScript("OnLeave", function() GameTooltip:Hide() end)

        -- Yell at you when your healer gets CC'd
        local healerCCRow = CreateFrame("Frame", nil, g)
        healerCCRow:SetPoint("TOPLEFT", enableRow, "BOTTOMLEFT", 0, -14)
        healerCCRow:SetPoint("RIGHT", g, "RIGHT", -20, 0)
        healerCCRow:SetHeight(26)

        local healerCCLabel = AC:CreateStyledText(healerCCRow, L("healer_in_cc_alert", "Healer In CC Alert"), 12, COLORS.TEXT_2, "OVERLAY", "")
        healerCCLabel:SetPoint("LEFT", 0, 0)
        healerCCLabel:SetPoint("RIGHT", -46, 0)
        healerCCLabel:SetJustifyH("LEFT")

        local healerCCCheckbox = AC:CreateFlatCheckbox(healerCCRow, 20, db.healerCCAlert == true, function(value)
            if not AC.DB.profile.bigDebuffs then AC.DB.profile.bigDebuffs = {} end
            AC.DB.profile.bigDebuffs.healerCCAlert = value
            if AC.HealerCCAlert then
                AC.HealerCCAlert:Toggle(value)
            end
        end)
        healerCCCheckbox:SetPoint("RIGHT", -10, 0)

        local healerCCTooltip = L("healer_in_cc_tooltip", "Shows a center-screen alert when your party healer is crowd controlled in arena. Displays the CC icon and remaining duration.")
        healerCCRow:SetScript("OnEnter", function(self)
            GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
            GameTooltip:SetText(healerCCTooltip, 1, 1, 1, 1, true)
            GameTooltip:Show()
        end)
        healerCCRow:SetScript("OnLeave", function() GameTooltip:Hide() end)

        -- Test the alert so you can see where it lands
        local testAlertRow = CreateFrame("Frame", nil, g)
        testAlertRow:SetHeight(32)
        testAlertRow:SetPoint("TOPLEFT", healerCCRow, "BOTTOMLEFT", 0, -10)
        testAlertRow:SetPoint("RIGHT", g, "RIGHT", -20, 0)

        local testAlertBtn = CreateFrame("Button", nil, testAlertRow, "BackdropTemplate")
        testAlertBtn:SetSize(90, 26)
        testAlertBtn:SetPoint("LEFT", 0, 0)
        testAlertBtn:SetBackdrop({
            bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
            edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
            tile = true, tileSize = 8, edgeSize = 12,
            insets = { left = 2, right = 2, top = 2, bottom = 2 }
        })
        testAlertBtn:SetBackdropColor(0.18, 0.14, 0.24, 1)
        testAlertBtn:SetBackdropBorderColor(0.5, 0.3, 0.7, 1)

        local testAlertBtnText = AC:CreateStyledText(testAlertBtn, L("test_alert_btn", "TEST ALERT"), 11, COLORS.TEXT, "OVERLAY", "")
        testAlertBtnText:SetPoint("CENTER", 0, 0)

        local function UpdateTestAlertButtonState()
            local isActive = AC.HealerCCAlert and AC.HealerCCAlert.IsTestAlertActive and AC.HealerCCAlert:IsTestAlertActive()
            if isActive then
                testAlertBtn:SetBackdropColor(0.3, 0.15, 0.15, 1)
                testAlertBtn:SetBackdropBorderColor(0.7, 0.2, 0.2, 1)
                testAlertBtnText:SetText(L("hide_alert_btn", "HIDE ALERT"))
            else
                testAlertBtn:SetBackdropColor(0.18, 0.14, 0.24, 1)
                testAlertBtn:SetBackdropBorderColor(0.5, 0.3, 0.7, 1)
                testAlertBtnText:SetText(L("test_alert_btn", "TEST ALERT"))
            end
        end

        testAlertBtn:SetScript("OnEnter", function(self)
            self:SetBackdropColor(0.25, 0.18, 0.35, 1)
            self:SetBackdropBorderColor(0.7, 0.4, 0.9, 1)
        end)
        testAlertBtn:SetScript("OnLeave", function(self)
            UpdateTestAlertButtonState()
        end)
        testAlertBtn:SetScript("OnClick", function()
            if AC.HealerCCAlert then
                local isActive = AC.HealerCCAlert.IsTestAlertActive and AC.HealerCCAlert:IsTestAlertActive()
                if isActive then
                    AC.HealerCCAlert:HideTestAlert()
                else
                    AC.HealerCCAlert:ShowTestAlert()
                end
                UpdateTestAlertButtonState()
            end
        end)

        local testAlertNote = AC:CreateStyledText(testAlertRow, L("test_alert_note", "Preview the center-screen alert with a looping countdown so you can position and scale it."), 10, COLORS.TEXT_MUTED, "OVERLAY", "")
        testAlertNote:SetPoint("LEFT", testAlertBtn, "RIGHT", 10, 0)
        testAlertNote:SetWidth(g:GetWidth() - 160)
        testAlertNote:SetJustifyH("LEFT")
        testAlertNote:SetWordWrap(true)

        -- Also show spells on enemy portraits
        local portraitRow = CreateFrame("Frame", nil, g)
        portraitRow:SetPoint("TOPLEFT", testAlertRow, "BOTTOMLEFT", 0, -14)
        portraitRow:SetPoint("RIGHT", g, "RIGHT", -20, 0)
        portraitRow:SetHeight(26)

        local portraitLabel = AC:CreateStyledText(portraitRow, L("show_spells_on_portraits", "Show Spells On Portraits"), 12, COLORS.TEXT_2, "OVERLAY", "")
        portraitLabel:SetPoint("LEFT", 0, 0)
        portraitLabel:SetPoint("RIGHT", -46, 0)
        portraitLabel:SetJustifyH("LEFT")

        local portraitCheckbox = AC:CreateFlatCheckbox(portraitRow, 20, db.showSpellsOnPortraits == true, function(value)
            if not AC.DB.profile.bigDebuffs then AC.DB.profile.bigDebuffs = {} end
            AC.DB.profile.bigDebuffs.showSpellsOnPortraits = value
            if AC.BigDebuffs and AC.BigDebuffs.Apply then
                AC.BigDebuffs:Apply()
            end
        end)
        portraitCheckbox:SetPoint("RIGHT", -10, 0)

        local portraitTooltip = L("show_spells_on_portraits_tooltip", "Shows the highest-priority active spell (Kick, CC, Defensive, Important) on arena enemy class portraits.")
        portraitRow:SetScript("OnEnter", function(self)
            GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
            GameTooltip:SetText(portraitTooltip, 1, 1, 1, 1, true)
            GameTooltip:Show()
        end)
        portraitRow:SetScript("OnLeave", function() GameTooltip:Hide() end)

        -- Disable the spiral glow line on portrait spell cooldowns for a cleaner look
        local portraitGlowRow = CreateFrame("Frame", nil, g)
        portraitGlowRow:SetPoint("TOPLEFT", portraitRow, "BOTTOMLEFT", 0, -14)
        portraitGlowRow:SetPoint("RIGHT", g, "RIGHT", -20, 0)
        portraitGlowRow:SetHeight(26)

        local portraitGlowLabel = AC:CreateStyledText(portraitGlowRow, L("disable_portrait_glow_line", "Disable Portrait Glow Line"), 12, COLORS.TEXT_2, "OVERLAY", "")
        portraitGlowLabel:SetPoint("LEFT", 0, 0)
        portraitGlowLabel:SetPoint("RIGHT", -46, 0)
        portraitGlowLabel:SetJustifyH("LEFT")

        local portraitGlowCheckbox = AC:CreateFlatCheckbox(portraitGlowRow, 20, db.disablePortraitGlowLine == true, function(value)
            if not AC.DB.profile.bigDebuffs then AC.DB.profile.bigDebuffs = {} end
            AC.DB.profile.bigDebuffs.disablePortraitGlowLine = value
            if AC.ClassIconAuras and AC.ClassIconAuras.UpdatePortraitGlowLine then
                AC.ClassIconAuras:UpdatePortraitGlowLine()
            end
        end)
        portraitGlowCheckbox:SetPoint("RIGHT", -10, 0)

        local portraitGlowTooltip = L("disable_portrait_glow_line_tooltip", "Removes the spiral animation glow line from portrait spell cooldowns for a cleaner visual.")
        portraitGlowRow:SetScript("OnEnter", function(self)
            GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
            GameTooltip:SetText(portraitGlowTooltip, 1, 1, 1, 1, true)
            GameTooltip:Show()
        end)
        portraitGlowRow:SetScript("OnLeave", function() GameTooltip:Hide() end)

        -- Show raid frame when solo (for testing)
        local soloRow = CreateFrame("Frame", nil, g)
        soloRow:SetPoint("TOPLEFT", portraitGlowRow, "BOTTOMLEFT", 0, -14)
        soloRow:SetPoint("RIGHT", g, "RIGHT", -20, 0)
        soloRow:SetHeight(26)

        local soloLabel = AC:CreateStyledText(soloRow, L("enable_solo_raid_frame_bd", "Enable Solo Raid Frame"), 12, COLORS.TEXT_2, "OVERLAY", "")
        soloLabel:SetPoint("LEFT", 0, 0)
        soloLabel:SetPoint("RIGHT", -46, 0)
        soloLabel:SetJustifyH("LEFT")

        local soloCheckbox = AC:CreateFlatCheckbox(soloRow, 20, db.soloRaidFrame == true, function(value)
            if not AC.DB.profile.bigDebuffs then AC.DB.profile.bigDebuffs = {} end
            AC.DB.profile.bigDebuffs.soloRaidFrame = value
            if AC.BigDebuffs and AC.BigDebuffs.UpdateSoloRaidFrame then
                AC.BigDebuffs:UpdateSoloRaidFrame()
            end
        end)
        soloCheckbox:SetPoint("RIGHT", -10, 0)

        local soloTooltip = L("enable_solo_raid_frame_bd_tooltip", "Show the raid frame even when not in a group. Perfect for testing or keeping a consistent layout.")
        soloRow:SetScript("OnEnter", function(self)
            GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
            GameTooltip:SetText(soloTooltip, 1, 1, 1, 1, true)
            GameTooltip:Show()
        end)
        soloRow:SetScript("OnLeave", function() GameTooltip:Hide() end)

        -- Test button to preview the CC icon
        local testRow = CreateFrame("Frame", nil, g)
        testRow:SetPoint("TOPLEFT", soloRow, "BOTTOMLEFT", 0, -18)
        testRow:SetPoint("RIGHT", g, "RIGHT", -20, 0)
        testRow:SetHeight(32)

        local testBtn = CreateFrame("Button", nil, testRow, "BackdropTemplate")
        testBtn:SetSize(100, 28)
        testBtn:SetPoint("LEFT", 0, 0)
        testBtn:SetBackdrop({
            bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
            edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
            tile = true, tileSize = 16, edgeSize = 12,
            insets = { left = 3, right = 3, top = 3, bottom = 3 }
        })
        testBtn:SetBackdropColor(0.15, 0.15, 0.15, 1)
        testBtn:SetBackdropBorderColor(0.4, 0.4, 0.4, 1)

        local testBtnText = testBtn:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        testBtnText:SetFont(AC.FONT_PATH or "Interface/AddOns/ArenaCore/Media/Fonts/arenacore.ttf", 11, "")
        testBtnText:SetText(L("test_cc_btn", "Test CC"))
        testBtnText:SetPoint("CENTER")
        testBtnText:SetTextColor(0.9, 0.9, 0.9, 1)

        testBtn:SetScript("OnEnter", function(self)
            self:SetBackdropColor(0.25, 0.25, 0.25, 1)
            self:SetBackdropBorderColor(0.6, 0.3, 0.7, 1)
        end)
        testBtn:SetScript("OnLeave", function(self)
            local isActive = AC.BigDebuffs and AC.BigDebuffs.IsTestActive and AC.BigDebuffs:IsTestActive()
            if isActive then
                self:SetBackdropColor(0.3, 0.15, 0.15, 1)
                self:SetBackdropBorderColor(0.7, 0.2, 0.2, 1)
            else
                self:SetBackdropColor(0.15, 0.15, 0.15, 1)
                self:SetBackdropBorderColor(0.4, 0.4, 0.4, 1)
            end
        end)
        testBtn:SetScript("OnClick", function(self)
            if AC.BigDebuffs and AC.BigDebuffs.ToggleTest then
                AC.BigDebuffs:ToggleTest()
            end
            local isActive = AC.BigDebuffs and AC.BigDebuffs.IsTestActive and AC.BigDebuffs:IsTestActive()
            if isActive then
                self:SetBackdropColor(0.3, 0.15, 0.15, 1)
                self:SetBackdropBorderColor(0.7, 0.2, 0.2, 1)
                testBtnText:SetText(L("stop_test_btn", "Stop Test"))
            else
                self:SetBackdropColor(0.15, 0.15, 0.15, 1)
                self:SetBackdropBorderColor(0.4, 0.4, 0.4, 1)
                testBtnText:SetText(L("test_cc_btn", "Test CC"))
            end
        end)

        local testNote = AC:CreateStyledText(testRow, L("test_note_big_debuffs", "Shows a fake CC icon on your Solo Raid Frame or party frame for preview."), 10, COLORS.TEXT_MUTED, "OVERLAY", "")
        testNote:SetPoint("LEFT", testBtn, "RIGHT", 10, 0)
        testNote:SetWidth(g:GetWidth() - 160)
        testNote:SetJustifyH("LEFT")
        testNote:SetWordWrap(true)

        -- Visual divider because this section is getting long
        local divider = CreateFrame("Frame", nil, g)
        divider:SetPoint("TOPLEFT", testRow, "BOTTOMLEFT", 0, -10)
        divider:SetPoint("RIGHT", g, "RIGHT", -20, 0)
        divider:SetHeight(1)
        local divTex = divider:CreateTexture(nil, "OVERLAY")
        divTex:SetColorTexture(0.2, 0.2, 0.2, 0.5)
        divTex:SetAllPoints()

        local themeRow = CreateFrame("Frame", nil, g)
        themeRow:SetPoint("TOPLEFT", divider, "BOTTOMLEFT", 0, -14)
        themeRow:SetPoint("RIGHT", g, "RIGHT", -20, 0)
        themeRow:SetHeight(26)

        local themeLabel = AC:CreateStyledText(themeRow, L("icon_theme", "Icon Theme"), 11, COLORS.TEXT_2, "OVERLAY", "")
        themeLabel:SetPoint("LEFT", 0, 0)
        themeLabel:SetWidth(100)
        themeLabel:SetJustifyH("LEFT")

        local themeOptions = { L("theme_none", "None"), L("theme_blackout", "Blackout"), L("theme_skill_issue_outline", "Skill Issue Outline"), L("theme_pulsing_glow", "Pulsing Glow"), L("theme_blizzard_glow", "Blizzard Glow") }
        local themeMap = { [L("theme_none", "None")] = "None", [L("theme_blackout", "Blackout")] = "Blackout", [L("theme_skill_issue_outline", "Skill Issue Outline")] = "pixel_black", [L("theme_pulsing_glow", "Pulsing Glow")] = "pulsing_glow", [L("theme_blizzard_glow", "Blizzard Glow")] = "blizzard_glow" }
        local displayMap = { ["None"] = L("theme_none", "None"), ["Blackout"] = L("theme_blackout", "Blackout"), ["pixel_black"] = L("theme_skill_issue_outline", "Skill Issue Outline"), ["pulsing_glow"] = L("theme_pulsing_glow", "Pulsing Glow"), ["blizzard_glow"] = L("theme_blizzard_glow", "Blizzard Glow") }
        local currentTheme = db.theme or "Blackout"
        local currentDisplay = displayMap[currentTheme] or currentTheme

        local themeBtn = CreateFrame("Button", nil, themeRow, "BackdropTemplate")
        themeBtn:SetSize(120, 22)
        themeBtn:SetPoint("LEFT", themeLabel, "RIGHT", 10, 0)
        themeBtn:SetBackdrop({
            bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
            edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
            tile = true, tileSize = 16, edgeSize = 1,
            insets = { left = 1, right = 1, top = 1, bottom = 1 }
        })
        themeBtn:SetBackdropColor(0.1, 0.1, 0.1, 0.9)
        themeBtn:SetBackdropBorderColor(0.3, 0.3, 0.3, 1)

        local themeBtnText = themeBtn:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        themeBtnText:SetPoint("LEFT", 8, 0)
        themeBtnText:SetTextColor(0.9, 0.9, 0.9, 1)
        if themeBtnText.SetFont then
            themeBtnText:SetFont(AC.FONT_PATH or "Interface/AddOns/ArenaCore/Media/Fonts/arenacore.ttf", 10, "")
        end
        themeBtnText:SetText(currentDisplay)

        local themeArrow = themeBtn:CreateTexture(nil, "OVERLAY")
        themeArrow:SetSize(12, 12)
        themeArrow:SetPoint("RIGHT", -8, 0)
        themeArrow:SetTexture("Interface\\Buttons\\UI-SpellbookIcon-NextPage-Up")
        themeArrow:SetTexCoord(1, 0, 0, 1)
        themeArrow:SetVertexColor(0.6, 0.6, 0.6, 1)

        local themePopup = CreateFrame("Frame", nil, UIParent, "BackdropTemplate")
        themePopup:SetSize(130, (#themeOptions * 22) + 8)
        themePopup:SetBackdrop({
            bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
            edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
            tile = true, tileSize = 16, edgeSize = 1,
            insets = { left = 1, right = 1, top = 1, bottom = 1 }
        })
        themePopup:SetBackdropColor(0.05, 0.05, 0.05, 0.95)
        themePopup:SetBackdropBorderColor(0.6, 0.2, 0.8, 1)
        themePopup:SetFrameStrata("TOOLTIP")
        themePopup:SetFrameLevel(9999)
        themePopup:Hide()

        for i, opt in ipairs(themeOptions) do
            local optBtn = CreateFrame("Button", nil, themePopup)
            optBtn:SetSize(110, 20)
            optBtn:SetPoint("TOPLEFT", 10, -6 - ((i - 1) * 22))
            local optText = optBtn:CreateFontString(nil, "OVERLAY", "GameFontNormal")
            optText:SetAllPoints()
            optText:SetTextColor(0.9, 0.9, 0.9, 1)
            if optText.SetFont then
                optText:SetFont(AC.FONT_PATH or "Interface/AddOns/ArenaCore/Media/Fonts/arenacore.ttf", 10, "")
            end
            optText:SetText(opt)
            optBtn:SetScript("OnEnter", function() optText:SetTextColor(0.8, 0.4, 0.9, 1) end)
            optBtn:SetScript("OnLeave", function() optText:SetTextColor(0.9, 0.9, 0.9, 1) end)
            optBtn:SetScript("OnClick", function()
                local val = themeMap[opt] or opt
                if not AC.DB.profile.bigDebuffs then AC.DB.profile.bigDebuffs = {} end
                AC.DB.profile.bigDebuffs.theme = val
                themeBtnText:SetText(opt)
                themePopup:Hide()
                if AC.BigDebuffs and AC.BigDebuffs.Apply then
                    AC.BigDebuffs:Apply()
                end
            end)
        end

        themeBtn:SetScript("OnClick", function() if themePopup:IsShown() then themePopup:Hide() else themePopup:ClearAllPoints(); themePopup:SetPoint("TOPLEFT", themeBtn, "BOTTOMLEFT", 0, -2); themePopup:Show() end end)
        themeBtn:SetScript("OnEnter", function() themeBtn:SetBackdropColor(0.4, 0.15, 0.5, 0.9); themeBtn:SetBackdropBorderColor(0.8, 0.4, 0.9, 1); themeBtnText:SetTextColor(1, 1, 1, 1) end)
        themeBtn:SetScript("OnLeave", function() themeBtn:SetBackdropColor(0.1, 0.1, 0.1, 0.9); themeBtn:SetBackdropBorderColor(0.3, 0.3, 0.3, 1); themeBtnText:SetTextColor(0.9, 0.9, 0.9, 1) end)
        if AC.configFrame then AC.configFrame:HookScript("OnHide", function() themePopup:Hide() end) end

        -- Sliders
        local scaleRow = CreateSliderRow(g, themeRow, "BOTTOMLEFT", L("icon_scale_bd", "Icon Scale"), 50, 300, db.iconScale or 140, "iconScale", L("icon_scale_bd_tooltip", "Scales the CC icon relative to the compact frame height."))
        local spacingRow = CreateSliderRow(g, scaleRow, "BOTTOMLEFT", L("icon_spacing", "Icon Spacing"), 0, 100, db.iconSpacing or 15, "iconSpacing", L("icon_spacing_tooltip", "Space between multiple CC icons on the same target."))
        local textSizeRow = CreateSliderRow(g, spacingRow, "BOTTOMLEFT", L("cooldown_text_size", "Cooldown Text Size"), 8, 32, db.cooldownTextSize or 12, "cooldownTextSize", L("cooldown_text_size_tooltip", "Size of the cooldown timer text on CC icons."))
        local zoomRow   = CreateSliderRow(g, textSizeRow, "BOTTOMLEFT", L("icon_zoom_bd", "Icon Zoom"), 0, 0.15, db.iconZoom or 0.08, "iconZoom", L("icon_zoom_bd_tooltip", "Crop amount around the icon edges (0 = full icon, 0.15 = zoomed in)."), true)
        local thicknessRow = CreateSliderRow(g, zoomRow, "BOTTOMLEFT", L("border_thickness_bd", "Border Thickness"), 1, 5, db.borderThickness or 2, "borderThickness", L("border_thickness_bd_tooltip", "Thickness of the pixel border when Skill Issue Outline is active."))
        local xRow     = CreateSliderRow(g, thicknessRow, "BOTTOMLEFT", L("horizontal_offset_bd", "Horizontal"), -250, 250, db.offsetX or 0, "offsetX", L("horizontal_offset_bd_tooltip", "Horizontal offset from the center of the party frame."))
        local yRow     = CreateSliderRow(g, xRow, "BOTTOMLEFT", L("vertical_offset_bd", "Vertical"), -250, 250, db.offsetY or 0, "offsetY", L("vertical_offset_bd_tooltip", "Vertical offset from the center of the party frame."))

        -- Add a custom glow around CC icons
        local glowRow = CreateFrame("Frame", nil, g)
        glowRow:SetPoint("TOPLEFT", yRow, "BOTTOMLEFT", 0, -14)
        glowRow:SetPoint("RIGHT", g, "RIGHT", -20, 0)
        glowRow:SetHeight(26)

        local glowLabel = AC:CreateStyledText(glowRow, L("custom_overlay_glow", "Custom Overlay Glow"), 12, COLORS.TEXT_2, "OVERLAY", "")
        glowLabel:SetPoint("LEFT", 0, 0)
        glowLabel:SetPoint("RIGHT", -46, 0)
        glowLabel:SetJustifyH("LEFT")

        local glowCheckbox = AC:CreateFlatCheckbox(glowRow, 20, db.customGlow ~= false, function(value)
            if not AC.DB.profile.bigDebuffs then AC.DB.profile.bigDebuffs = {} end
            AC.DB.profile.bigDebuffs.customGlow = value
            if AC.BigDebuffs and AC.BigDebuffs.Apply then
                AC.BigDebuffs:Apply()
            end
        end)
        glowCheckbox:SetPoint("RIGHT", -10, 0)

        local glowTooltip = L("custom_overlay_glow_tooltip", "Adds a customizable glow around active crowd control icons.")
        glowRow:SetScript("OnEnter", function(self)
            GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
            GameTooltip:SetText(glowTooltip, 1, 1, 1, 1, true)
            GameTooltip:Show()
        end)
        glowRow:SetScript("OnLeave", function() GameTooltip:Hide() end)

        -- Color picker for the glow (only matters if custom glow is on)
        local glowColorRow = CreateFrame("Frame", nil, g)
        glowColorRow:SetPoint("TOPLEFT", glowRow, "BOTTOMLEFT", 0, -10)
        glowColorRow:SetPoint("RIGHT", g, "RIGHT", -20, 0)
        glowColorRow:SetHeight(26)

        local glowColorLabel = AC:CreateStyledText(glowColorRow, L("glow_color", "Glow Color"), 12, COLORS.TEXT_2, "OVERLAY", "")
        glowColorLabel:SetPoint("LEFT", 0, 0)
        glowColorLabel:SetWidth(100)
        glowColorLabel:SetJustifyH("LEFT")

        local glowColorBtn = CreateFrame("Button", nil, glowColorRow)
        glowColorBtn:SetSize(150, 24)
        glowColorBtn:SetPoint("LEFT", glowColorLabel, "RIGHT", 10, 0)

        local glowColorBorder = AC:CreateFlatTexture(glowColorBtn, "BACKGROUND", 1, {0.196, 0.196, 0.196, 1}, 1)
        glowColorBorder:SetAllPoints()

        local glowColorBg = AC:CreateFlatTexture(glowColorBtn, "BACKGROUND", 2, {0.102, 0.102, 0.102, 1}, 1)
        glowColorBg:SetPoint("TOPLEFT", 1, -1)
        glowColorBg:SetPoint("BOTTOMRIGHT", -1, 1)

        local glowColorSwatch = glowColorBtn:CreateTexture(nil, "OVERLAY")
        glowColorSwatch:SetPoint("LEFT", 6, 0)
        glowColorSwatch:SetSize(18, 18)
        glowColorSwatch:SetTexture("Interface\\Buttons\\WHITE8X8")

        local glowColorText = AC:CreateStyledText(glowColorBtn, L("pick_color", "Pick"), 11, {0.706, 0.706, 0.706, 1}, "OVERLAY", "")
        glowColorText:SetPoint("LEFT", glowColorSwatch, "RIGHT", 8, 0)
        glowColorText:SetJustifyH("LEFT")

        local function GetGlowColor()
            local c = AC.DB and AC.DB.profile and AC.DB.profile.bigDebuffs and AC.DB.profile.bigDebuffs.glowColor
            if type(c) == "table" then
                return c.r or 0.8, c.g or 0.4, c.b or 0.9, c.a or 0.85
            end
            return 0.8, 0.4, 0.9, 0.85
        end

        local function SaveGlowColor(r, g, b, a)
            if not AC.DB.profile.bigDebuffs then AC.DB.profile.bigDebuffs = {} end
            AC.DB.profile.bigDebuffs.glowColor = { r = r, g = g, b = b, a = a or 0.85 }
            if AC.BigDebuffs and AC.BigDebuffs.Apply then
                AC.BigDebuffs:Apply()
            end
        end

        local function RefreshGlowColorSwatch()
            local r, g, b, a = GetGlowColor()
            glowColorSwatch:SetVertexColor(r, g, b, a or 1)
        end
        RefreshGlowColorSwatch()

        -- Track if the picker is open so we can save properly when it closes
        local glowPickerActive, glowPickerCanceled = false, false

        local function OpenGlowColorPicker()
            if not ColorPickerFrame then return end
            local r, g, b, a = GetGlowColor()

            -- Save the original color in case you change your mind
            local previousR, previousG, previousB, previousA = r, g, b, a

            local function OnGlowColorChanged()
                local nr, ng, nb = ColorPickerFrame:GetColorRGB()
                local na = 1
                if OpacitySliderFrame and OpacitySliderFrame.GetValue then
                    na = 1 - OpacitySliderFrame:GetValue()
                elseif ColorPickerFrame.opacity ~= nil then
                    na = 1 - ColorPickerFrame.opacity
                end
                SaveGlowColor(nr, ng, nb, na)
                RefreshGlowColorSwatch()
            end

            local function OnGlowColorCancel()
                glowPickerCanceled = true
                SaveGlowColor(previousR, previousG, previousB, previousA)
                RefreshGlowColorSwatch()
            end

            local opacity = 1 - (a or 0.85)

            glowPickerActive = true
            glowPickerCanceled = false

            if ColorPickerFrame.SetupColorPickerAndShow then
                ColorPickerFrame:SetupColorPickerAndShow({
                    r = r, g = g, b = b,
                    opacity = opacity,
                    hasOpacity = true,
                    swatchFunc = OnGlowColorChanged,
                    opacityFunc = OnGlowColorChanged,
                    cancelFunc = OnGlowColorCancel,
                })
            else
                ColorPickerFrame.hasOpacity = true
                ColorPickerFrame.opacity = opacity
                ColorPickerFrame.func = OnGlowColorChanged
                ColorPickerFrame.opacityFunc = OnGlowColorChanged
                ColorPickerFrame.cancelFunc = OnGlowColorCancel
                ColorPickerFrame.previousValues = { r, g, b, a or 0.85 }
                if ColorPickerFrame.SetColorRGB then
                    ColorPickerFrame:SetColorRGB(r, g, b)
                end
                ColorPickerFrame:Show()
            end
        end

        -- One-time hook: grab the final color when the picker closes.
        -- This catches hex input + OK when the normal callback didn't fire.
        ColorPickerFrame:HookScript("OnHide", function()
            if not glowPickerActive then return end
            glowPickerActive = false
            if glowPickerCanceled then
                glowPickerCanceled = false
                return
            end
            local nr, ng, nb = ColorPickerFrame:GetColorRGB()
            local na = 1
            if OpacitySliderFrame and OpacitySliderFrame.GetValue then
                na = 1 - OpacitySliderFrame:GetValue()
            elseif ColorPickerFrame.opacity ~= nil then
                na = 1 - ColorPickerFrame.opacity
            end
            SaveGlowColor(nr, ng, nb, na)
            RefreshGlowColorSwatch()
        end)

        glowColorBtn:SetScript("OnClick", OpenGlowColorPicker)
        glowColorBtn:SetScript("OnShow", RefreshGlowColorSwatch)

        local glowColorTooltip = L("glow_color_tooltip", "Choose the overlay glow color for active crowd control icons.")
        glowColorRow:SetScript("OnEnter", function(self)
            GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
            GameTooltip:SetText(glowColorTooltip, 1, 1, 1, 1, true)
            GameTooltip:Show()
        end)
        glowColorRow:SetScript("OnLeave", function() GameTooltip:Hide() end)

        f._built = true
    end

    local function BuildSafeQueue()
        local f = EnsureContentFrame("safequeue")
        if f._built then return end

        local g = CreateFrame("Frame", nil, f)
        g:SetPoint("TOPLEFT", 18, -12)
        g:SetPoint("TOPRIGHT", -18, -12)
        g:SetHeight(280)
        AC:HairlineGroupBox(g)

        local sectionTitle = AC:CreateStyledText(g, L("page_title", "Arena Arsenal"), 13, COLORS.PRIMARY, "OVERLAY", "")
        sectionTitle:SetPoint("TOPLEFT", 20, -18)

        local tabTitle = AC:CreateStyledText(g, L("tab_safe_queue", "Safe Queue"), 14, COLORS.TEXT, "OVERLAY", "")
        tabTitle:SetPoint("TOPLEFT", sectionTitle, "BOTTOMLEFT", 0, -18)

        local descText = AC:CreateStyledText(g, L("safe_queue_desc", "Adds timer under the queue pop window so you don't miss the accept timer. Works for both PvP queues and LFG dungeon queues."), 11, COLORS.TEXT_MUTED, "OVERLAY", "")
        descText:SetPoint("TOPLEFT", tabTitle, "BOTTOMLEFT", 0, -6)
        descText:SetWidth(g:GetWidth() - 40)
        descText:SetJustifyH("LEFT")
        descText:SetWordWrap(true)

        -- Master switch for the whole thing
        local db = AC.DB and AC.DB.profile and AC.DB.profile.safeQueue
        if not db then db = {} end

        local enableRow = CreateFrame("Frame", nil, g)
        enableRow:SetPoint("TOPLEFT", descText, "BOTTOMLEFT", 0, -14)
        enableRow:SetPoint("RIGHT", g, "RIGHT", -20, 0)
        enableRow:SetHeight(26)

        local enableLabel = AC:CreateStyledText(enableRow, L("enable_safe_queue", "Enable Safe Queue"), 12, COLORS.TEXT_2, "OVERLAY", "")
        enableLabel:SetPoint("LEFT", 0, 0)
        enableLabel:SetPoint("RIGHT", -46, 0)
        enableLabel:SetJustifyH("LEFT")

        local enableCheckbox = AC:CreateFlatCheckbox(enableRow, 20, db.enabled == true, function(value)
            if not AC.DB.profile.safeQueue then AC.DB.profile.safeQueue = {} end
            AC.DB.profile.safeQueue.enabled = value
            if AC.SetSafeQueueEnabled then
                AC:SetSafeQueueEnabled(value)
            end
        end)
        enableCheckbox:SetPoint("RIGHT", -10, 0)

        -- Explain what this actually does
        local tooltipText = L("enable_safe_queue_tooltip", "When enabled, a countdown bar appears under queue pop dialogs showing how much time you have left to accept.")
        enableRow:SetScript("OnEnter", function(self)
            GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
            GameTooltip:SetText(tooltipText, 1, 1, 1, 1, true)
            GameTooltip:Show()
        end)
        enableRow:SetScript("OnLeave", function() GameTooltip:Hide() end)

        -- Queue Status Tracker (see all your queues in one place)
        local qstDb = AC.DB and AC.DB.profile and AC.DB.profile.queueStatusTracker
        if not qstDb then qstDb = {} end

        local qstRow = CreateFrame("Frame", nil, g)
        qstRow:SetPoint("TOPLEFT", enableRow, "BOTTOMLEFT", 0, -14)
        qstRow:SetPoint("RIGHT", g, "RIGHT", -20, 0)
        qstRow:SetHeight(26)

        local qstLabel = AC:CreateStyledText(qstRow, L("enable_queue_status_window", "Enable Queue Status Window"), 12, COLORS.TEXT_2, "OVERLAY", "")
        qstLabel:SetPoint("LEFT", 0, 0)
        qstLabel:SetPoint("RIGHT", -46, 0)
        qstLabel:SetJustifyH("LEFT")

        local qstCheckbox = AC:CreateFlatCheckbox(qstRow, 20, qstDb.enabled == true, function(value)
            if not AC.DB.profile.queueStatusTracker then AC.DB.profile.queueStatusTracker = {} end
            AC.DB.profile.queueStatusTracker.enabled = value
            if AC.SetQueueStatusTrackerEnabled then
                AC:SetQueueStatusTrackerEnabled(value)
            end
        end)
        qstCheckbox:SetPoint("RIGHT", -10, 0)

        -- Explain the queue status window
        local qstTooltipText = L("queue_status_window_tooltip", "Shows a draggable window with all your active queues, wait times, and what you're queued for.")
        qstRow:SetScript("OnEnter", function(self)
            GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
            GameTooltip:SetText(qstTooltipText, 1, 1, 1, 1, true)
            GameTooltip:Show()
        end)
        qstRow:SetScript("OnLeave", function() GameTooltip:Hide() end)

        -- Quick summary of what these features do
        local detailsTitle = AC:CreateStyledText(g, L("how_it_works", "How it works:"), 11, COLORS.TEXT, "OVERLAY", "")
        detailsTitle:SetPoint("TOPLEFT", qstRow, "BOTTOMLEFT", 0, -18)

        local detailsText = AC:CreateStyledText(g, L("safe_queue_details", "- Safe Queue: Timer under queue pop dialogs\n- Queue Status: Draggable window showing all active queues\n- Both features work for PvP and LFG queues\n- Chat notifications when queues pop"), 11, COLORS.TEXT_MUTED, "OVERLAY", "")
        detailsText:SetPoint("TOPLEFT", detailsTitle, "BOTTOMLEFT", 0, -4)
        detailsText:SetWidth(g:GetWidth() - 40)
        detailsText:SetJustifyH("LEFT")
        detailsText:SetWordWrap(true)

        f._built = true
    end

    -- Wire up the tab buttons to actually do something
    for _, btn in ipairs(tabs) do
        btn:SetScript("OnClick", function()
            local id = btn._tabId
            if id == "misc" then      BuildMiscReminders()
            elseif id == "pvpqueue" then BuildPvPQueue()
            elseif id == "party" then    BuildPartyTrinkets()
            elseif id == "bigdebuffs" then BuildBigDebuffs()
            elseif id == "safequeue" then  BuildSafeQueue()
            elseif id == "targetoftargets" then BuildTargetOfTargets()
            end
            SetActiveTab(id)
        end)
    end

    -- Start on the Misc tab by default
    BuildMiscReminders()
    SetActiveTab("misc")

    -- Re-enable mouse wheel when the page closes
    root:HookScript("OnHide", function()
        if AC.contentArea and AC.contentArea.EnableMouseWheel then
            AC.contentArea:EnableMouseWheel(true)
        end
    end)

    AC:HideLegacyJunk(strip)
end

-- Register the page with the AC system (keep key for nav compatibility)
AC:RegisterPage("BlackoutMods", CreateArenaArsenalPage)
