-- =============================================================================
-- Core/Pages/TargetOfTargets.lua - Target of Targets configuration page
-- =============================================================================

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("TARGET_OF_TARGETS", key, fallback or key)
    end
    return fallback or key
end

-- ============================================================================
-- PAGE BUILDER
-- ============================================================================

local function BuildTargetOfTargetsPage(root)
    local g = CreateFrame("Frame", nil, root)
    g:SetPoint("TOPLEFT", 18, -12)
    g:SetPoint("TOPRIGHT", -18, -12)
    g:SetHeight(1060)
    AC:HairlineGroupBox(g)

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

    local descText = AC:CreateStyledText(g, "Shows who your party members are targeting on arena frames, and which arena opponents are targeting your party members. Perfect for tracking focus fire and target swaps.", 11, COLORS.TEXT_MUTED, "OVERLAY", "")
    descText:SetPoint("TOPLEFT", sectionTitle, "BOTTOMLEFT", 0, -12)
    descText:SetWidth(g:GetWidth() - 40)
    descText:SetJustifyH("LEFT")
    descText:SetWordWrap(true)

    -- ============================================================================
    -- FRIENDLY TARGETS ON ARENA FRAMES
    -- ============================================================================

    local friendlyGroup = CreateFrame("Frame", nil, g)
    friendlyGroup:SetPoint("TOPLEFT", descText, "BOTTOMLEFT", 0, -20)
    friendlyGroup:SetPoint("TOPRIGHT", -18, 0)
    friendlyGroup:SetHeight(270)

    local friendlyTitle = AC:CreateStyledText(friendlyGroup, "Friendly Targets on Arena Frames", 12, COLORS.TEXT, "OVERLAY", "")
    friendlyTitle:SetPoint("TOPLEFT", 20, -16)

    local friendlyDesc = AC:CreateStyledText(friendlyGroup, "Shows class icons on arena frames indicating which party members are targeting each opponent.", 10, COLORS.TEXT_MUTED, "OVERLAY", "")
    friendlyDesc:SetPoint("TOPLEFT", friendlyTitle, "BOTTOMLEFT", 0, -4)
    friendlyDesc:SetWidth(friendlyGroup:GetWidth() - 40)
    friendlyDesc:SetJustifyH("LEFT")
    friendlyDesc:SetWordWrap(true)

    local db = AC.DB and AC.DB.profile and AC.DB.profile.targetOfTargets
    if not db then db = {} end
    local friendlySettings = db.alliesOnArena or {}

    -- Enable checkbox
    local enableRow = CreateFrame("Frame", nil, friendlyGroup)
    enableRow:SetPoint("TOPLEFT", friendlyDesc, "BOTTOMLEFT", 0, -12)
    enableRow:SetPoint("RIGHT", friendlyGroup, "RIGHT", -20, 0)
    enableRow:SetHeight(26)

    local enableLabel = AC:CreateStyledText(enableRow, "Enable Friendly Targets", 11, COLORS.TEXT_2, "OVERLAY", "")
    enableLabel:SetPoint("LEFT", 0, 0)
    enableLabel:SetPoint("RIGHT", -46, 0)
    enableLabel:SetJustifyH("LEFT")

    local enableCheckbox = AC:CreateFlatCheckbox(enableRow, 20, friendlySettings.enabled == true, function(value)
        if not AC.DB.profile.targetOfTargets then AC.DB.profile.targetOfTargets = {} end
        if not AC.DB.profile.targetOfTargets.alliesOnArena then AC.DB.profile.targetOfTargets.alliesOnArena = {} end
        AC.DB.profile.targetOfTargets.alliesOnArena.enabled = value
        if AC.TargetOfTargets and AC.TargetOfTargets.Refresh then
            AC.TargetOfTargets:Refresh()
        end
    end)
    enableCheckbox:SetPoint("RIGHT", -10, 0)

    -- Class Colored Borders checkbox
    local classColorRow = CreateFrame("Frame", nil, friendlyGroup)
    classColorRow:SetPoint("TOPLEFT", enableRow, "BOTTOMLEFT", 0, -12)
    classColorRow:SetPoint("RIGHT", friendlyGroup, "RIGHT", -20, 0)
    classColorRow:SetHeight(26)

    local classColorLabel = AC:CreateStyledText(classColorRow, "Class Color'd Borders", 11, COLORS.TEXT_2, "OVERLAY", "")
    classColorLabel:SetPoint("LEFT", 0, 0)
    classColorLabel:SetPoint("RIGHT", -46, 0)
    classColorLabel:SetJustifyH("LEFT")

    local classColorCheckbox = AC:CreateFlatCheckbox(classColorRow, 20, friendlySettings.classColoredBorders == true, function(value)
        if not AC.DB.profile.targetOfTargets then AC.DB.profile.targetOfTargets = {} end
        if not AC.DB.profile.targetOfTargets.alliesOnArena then AC.DB.profile.targetOfTargets.alliesOnArena = {} end
        AC.DB.profile.targetOfTargets.alliesOnArena.classColoredBorders = value
        if AC.TargetOfTargets and AC.TargetOfTargets.Refresh then
            AC.TargetOfTargets:Refresh()
        end
    end)
    classColorCheckbox:SetPoint("RIGHT", -10, 0)

    -- Show Icons checkbox
    local iconsRow = CreateFrame("Frame", nil, friendlyGroup)
    iconsRow:SetPoint("TOPLEFT", classColorRow, "BOTTOMLEFT", 0, -12)
    iconsRow:SetPoint("RIGHT", friendlyGroup, "RIGHT", -20, 0)
    iconsRow:SetHeight(26)

    local iconsLabel = AC:CreateStyledText(iconsRow, "Show Class Icons", 11, COLORS.TEXT_2, "OVERLAY", "")
    iconsLabel:SetPoint("LEFT", 0, 0)
    iconsLabel:SetPoint("RIGHT", -46, 0)
    iconsLabel:SetJustifyH("LEFT")

    local iconsCheckbox = AC:CreateFlatCheckbox(iconsRow, 20, friendlySettings.showIcons == true, function(value)
        if not AC.DB.profile.targetOfTargets then AC.DB.profile.targetOfTargets = {} end
        if not AC.DB.profile.targetOfTargets.alliesOnArena then AC.DB.profile.targetOfTargets.alliesOnArena = {} end
        AC.DB.profile.targetOfTargets.alliesOnArena.showIcons = value
        if AC.TargetOfTargets and AC.TargetOfTargets.Refresh then
            AC.TargetOfTargets:Refresh()
        end
    end)
    iconsCheckbox:SetPoint("RIGHT", -10, 0)

    -- Show Text checkbox
    local textRow = CreateFrame("Frame", nil, friendlyGroup)
    textRow:SetPoint("TOPLEFT", iconsRow, "BOTTOMLEFT", 0, -12)
    textRow:SetPoint("RIGHT", friendlyGroup, "RIGHT", -20, 0)
    textRow:SetHeight(26)

    local textLabel = AC:CreateStyledText(textRow, "Show Target Name (Text)", 11, COLORS.TEXT_2, "OVERLAY", "")
    textLabel:SetPoint("LEFT", 0, 0)
    textLabel:SetPoint("RIGHT", -46, 0)
    textLabel:SetJustifyH("LEFT")

    local textCheckbox = AC:CreateFlatCheckbox(textRow, 20, friendlySettings.showText == true, function(value)
        if not AC.DB.profile.targetOfTargets then AC.DB.profile.targetOfTargets = {} end
        if not AC.DB.profile.targetOfTargets.alliesOnArena then AC.DB.profile.targetOfTargets.alliesOnArena = {} end
        AC.DB.profile.targetOfTargets.alliesOnArena.showText = value
        if AC.TargetOfTargets and AC.TargetOfTargets.Refresh then
            AC.TargetOfTargets:Refresh()
        end
    end)
    textCheckbox:SetPoint("RIGHT", -10, 0)

    -- Icon Scale slider
    local scaleRow = CreateFrame("Frame", nil, friendlyGroup)
    scaleRow:SetPoint("TOPLEFT", textRow, "BOTTOMLEFT", 0, -12)
    scaleRow:SetPoint("RIGHT", friendlyGroup, "RIGHT", -20, 0)
    scaleRow:SetHeight(30)

    local scaleSlider = AC:CreateEnhancedSliderRow(scaleRow, "Icon Scale", 50, "targetOfTargets.alliesOnArena.iconScale", "sizing_scale")
    scaleSlider:SetPoint("TOPLEFT", 0, 0)
    scaleSlider:SetPoint("RIGHT", 0, 0)
    scaleSlider:SetHeight(30)

    -- Spacing slider
    local spacingRow = CreateFrame("Frame", nil, friendlyGroup)
    spacingRow:SetPoint("TOPLEFT", scaleRow, "BOTTOMLEFT", 0, -8)
    spacingRow:SetPoint("RIGHT", friendlyGroup, "RIGHT", -20, 0)
    spacingRow:SetHeight(30)

    local spacingSlider = AC:CreateEnhancedSliderRow(spacingRow, "Icon Spacing", 2, "targetOfTargets.alliesOnArena.spacing", "spacing")
    spacingSlider:SetPoint("TOPLEFT", 0, 0)
    spacingSlider:SetPoint("RIGHT", 0, 0)
    spacingSlider:SetHeight(30)

    -- ============================================================================
    -- ARENA TARGETS ON PARTY FRAMES
    -- ============================================================================

    local arenaGroup = CreateFrame("Frame", nil, g)
    arenaGroup:SetPoint("TOPLEFT", friendlyGroup, "BOTTOMLEFT", 0, -20)
    arenaGroup:SetPoint("TOPRIGHT", -18, 0)
    arenaGroup:SetHeight(310)

    local arenaTitle = AC:CreateStyledText(arenaGroup, "Arena Targets on Party Frames", 12, COLORS.TEXT, "OVERLAY", "")
    arenaTitle:SetPoint("TOPLEFT", 20, -16)

    local arenaDesc = AC:CreateStyledText(arenaGroup, "Shows class icons on party frames indicating which arena opponents are targeting each party member.", 10, COLORS.TEXT_MUTED, "OVERLAY", "")
    arenaDesc:SetPoint("TOPLEFT", arenaTitle, "BOTTOMLEFT", 0, -4)
    arenaDesc:SetWidth(arenaGroup:GetWidth() - 40)
    arenaDesc:SetJustifyH("LEFT")
    arenaDesc:SetWordWrap(true)

    local arenaSettings = db.attackersOnParty or {}

    -- Enable checkbox
    local arenaEnableRow = CreateFrame("Frame", nil, arenaGroup)
    arenaEnableRow:SetPoint("TOPLEFT", arenaDesc, "BOTTOMLEFT", 0, -12)
    arenaEnableRow:SetPoint("RIGHT", arenaGroup, "RIGHT", -20, 0)
    arenaEnableRow:SetHeight(26)

    local arenaEnableLabel = AC:CreateStyledText(arenaEnableRow, "Enable Arena Targets", 11, COLORS.TEXT_2, "OVERLAY", "")
    arenaEnableLabel:SetPoint("LEFT", 0, 0)
    arenaEnableLabel:SetPoint("RIGHT", -46, 0)
    arenaEnableLabel:SetJustifyH("LEFT")

    local arenaEnableCheckbox = AC:CreateFlatCheckbox(arenaEnableRow, 20, arenaSettings.enabled == true, function(value)
        if not AC.DB.profile.targetOfTargets then AC.DB.profile.targetOfTargets = {} end
        if not AC.DB.profile.targetOfTargets.attackersOnParty then AC.DB.profile.targetOfTargets.attackersOnParty = {} end
        AC.DB.profile.targetOfTargets.attackersOnParty.enabled = value
        if AC.TargetOfTargets and AC.TargetOfTargets.Refresh then
            AC.TargetOfTargets:Refresh()
        end
    end)
    arenaEnableCheckbox:SetPoint("RIGHT", -10, 0)

    -- Class Colored Borders checkbox
    local arenaClassColorRow = CreateFrame("Frame", nil, arenaGroup)
    arenaClassColorRow:SetPoint("TOPLEFT", arenaEnableRow, "BOTTOMLEFT", 0, -12)
    arenaClassColorRow:SetPoint("RIGHT", arenaGroup, "RIGHT", -20, 0)
    arenaClassColorRow:SetHeight(26)

    local arenaClassColorLabel = AC:CreateStyledText(arenaClassColorRow, "Class Color'd Borders", 11, COLORS.TEXT_2, "OVERLAY", "")
    arenaClassColorLabel:SetPoint("LEFT", 0, 0)
    arenaClassColorLabel:SetPoint("RIGHT", -46, 0)
    arenaClassColorLabel:SetJustifyH("LEFT")

    local arenaClassColorCheckbox = AC:CreateFlatCheckbox(arenaClassColorRow, 20, arenaSettings.classColoredBorders == true, function(value)
        if not AC.DB.profile.targetOfTargets then AC.DB.profile.targetOfTargets = {} end
        if not AC.DB.profile.targetOfTargets.attackersOnParty then AC.DB.profile.targetOfTargets.attackersOnParty = {} end
        AC.DB.profile.targetOfTargets.attackersOnParty.classColoredBorders = value
        if AC.TargetOfTargets and AC.TargetOfTargets.Refresh then
            AC.TargetOfTargets:Refresh()
        end
    end)
    arenaClassColorCheckbox:SetPoint("RIGHT", -10, 0)

    -- Show Icons checkbox
    local arenaIconsRow = CreateFrame("Frame", nil, arenaGroup)
    arenaIconsRow:SetPoint("TOPLEFT", arenaClassColorRow, "BOTTOMLEFT", 0, -12)
    arenaIconsRow:SetPoint("RIGHT", arenaGroup, "RIGHT", -20, 0)
    arenaIconsRow:SetHeight(26)

    local arenaIconsLabel = AC:CreateStyledText(arenaIconsRow, "Show Class Icons", 11, COLORS.TEXT_2, "OVERLAY", "")
    arenaIconsLabel:SetPoint("LEFT", 0, 0)
    arenaIconsLabel:SetPoint("RIGHT", -46, 0)
    arenaIconsLabel:SetJustifyH("LEFT")

    local arenaIconsCheckbox = AC:CreateFlatCheckbox(arenaIconsRow, 20, arenaSettings.showIcons == true, function(value)
        if not AC.DB.profile.targetOfTargets then AC.DB.profile.targetOfTargets = {} end
        if not AC.DB.profile.targetOfTargets.attackersOnParty then AC.DB.profile.targetOfTargets.attackersOnParty = {} end
        AC.DB.profile.targetOfTargets.attackersOnParty.showIcons = value
        if AC.TargetOfTargets and AC.TargetOfTargets.Refresh then
            AC.TargetOfTargets:Refresh()
        end
    end)
    arenaIconsCheckbox:SetPoint("RIGHT", -10, 0)

    -- Show Text checkbox
    local arenaTextRow = CreateFrame("Frame", nil, arenaGroup)
    arenaTextRow:SetPoint("TOPLEFT", arenaIconsRow, "BOTTOMLEFT", 0, -12)
    arenaTextRow:SetPoint("RIGHT", arenaGroup, "RIGHT", -20, 0)
    arenaTextRow:SetHeight(26)

    local arenaTextLabel = AC:CreateStyledText(arenaTextRow, "Show Target Name (Text)", 11, COLORS.TEXT_2, "OVERLAY", "")
    arenaTextLabel:SetPoint("LEFT", 0, 0)
    arenaTextLabel:SetPoint("RIGHT", -46, 0)
    arenaTextLabel:SetJustifyH("LEFT")

    local arenaTextCheckbox = AC:CreateFlatCheckbox(arenaTextRow, 20, arenaSettings.showText == true, function(value)
        if not AC.DB.profile.targetOfTargets then AC.DB.profile.targetOfTargets = {} end
        if not AC.DB.profile.targetOfTargets.attackersOnParty then AC.DB.profile.targetOfTargets.attackersOnParty = {} end
        AC.DB.profile.targetOfTargets.attackersOnParty.showText = value
        if AC.TargetOfTargets and AC.TargetOfTargets.Refresh then
            AC.TargetOfTargets:Refresh()
        end
    end)
    arenaTextCheckbox:SetPoint("RIGHT", -10, 0)

    -- Icon Scale slider
    local arenaScaleRow = CreateFrame("Frame", nil, arenaGroup)
    arenaScaleRow:SetPoint("TOPLEFT", arenaTextRow, "BOTTOMLEFT", 0, -12)
    arenaScaleRow:SetPoint("RIGHT", arenaGroup, "RIGHT", -20, 0)
    arenaScaleRow:SetHeight(30)

    local arenaScaleSlider = AC:CreateEnhancedSliderRow(arenaScaleRow, "Icon Scale", 100, "targetOfTargets.attackersOnParty.iconScale", "sizing_scale")
    arenaScaleSlider:SetPoint("TOPLEFT", 0, 0)
    arenaScaleSlider:SetPoint("RIGHT", 0, 0)
    arenaScaleSlider:SetHeight(30)

    -- Spacing slider
    local arenaSpacingRow = CreateFrame("Frame", nil, arenaGroup)
    arenaSpacingRow:SetPoint("TOPLEFT", arenaScaleRow, "BOTTOMLEFT", 0, -8)
    arenaSpacingRow:SetPoint("RIGHT", arenaGroup, "RIGHT", -20, 0)
    arenaSpacingRow:SetHeight(30)

    local arenaSpacingSlider = AC:CreateEnhancedSliderRow(arenaSpacingRow, "Icon Spacing", 2, "targetOfTargets.attackersOnParty.spacing", "spacing")
    arenaSpacingSlider:SetPoint("TOPLEFT", 0, 0)
    arenaSpacingSlider:SetPoint("RIGHT", 0, 0)
    arenaSpacingSlider:SetHeight(30)

    local arenaTestRow = CreateFrame("Frame", nil, arenaGroup)
    arenaTestRow:SetHeight(32)
    arenaTestRow:SetPoint("TOPLEFT", arenaSpacingRow, "BOTTOMLEFT", 0, -14)
    arenaTestRow:SetPoint("RIGHT", arenaGroup, "RIGHT", -20, 0)

    local arenaTestBtn = CreateFrame("Button", nil, arenaTestRow, "BackdropTemplate")
    arenaTestBtn:SetSize(90, 26)
    arenaTestBtn:SetPoint("LEFT", 0, 0)
    arenaTestBtn: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 }
    })
    arenaTestBtn:SetBackdropColor(0.18, 0.14, 0.24, 1)
    arenaTestBtn:SetBackdropBorderColor(0.5, 0.3, 0.7, 1)

    local arenaTestBtnText = AC:CreateStyledText(arenaTestBtn, "TEST", 11, COLORS.TEXT, "OVERLAY", "")
    arenaTestBtnText:SetPoint("CENTER", 0, 0)

    local function RefreshArenaTestButton()
        local isActive = AC.TargetOfTargets and AC.TargetOfTargets.IsPartyFrameTestActive and AC.TargetOfTargets:IsPartyFrameTestActive()
        if isActive then
            arenaTestBtn:SetBackdropColor(0.3, 0.15, 0.15, 1)
            arenaTestBtn:SetBackdropBorderColor(0.7, 0.2, 0.2, 1)
            arenaTestBtnText:SetText("HIDE")
        else
            arenaTestBtn:SetBackdropColor(0.18, 0.14, 0.24, 1)
            arenaTestBtn:SetBackdropBorderColor(0.5, 0.3, 0.7, 1)
            arenaTestBtnText:SetText("TEST")
        end
    end

    arenaTestBtn:SetScript("OnEnter", function(self)
        self:SetBackdropColor(0.25, 0.18, 0.35, 1)
        self:SetBackdropBorderColor(0.7, 0.4, 0.9, 1)
    end)
    arenaTestBtn:SetScript("OnLeave", RefreshArenaTestButton)
    arenaTestBtn:SetScript("OnClick", function()
        if AC.TargetOfTargets and AC.TargetOfTargets.TogglePartyFrameTest then
            AC.TargetOfTargets:TogglePartyFrameTest()
        end
        RefreshArenaTestButton()
    end)

    local arenaTestHint = AC:CreateStyledText(arenaTestRow, "Shows fake arena targets on your Solo Raid Frame for positioning.", 10, COLORS.TEXT_MUTED, "OVERLAY", "")
    arenaTestHint:SetPoint("LEFT", arenaTestBtn, "RIGHT", 10, 0)
    arenaTestHint:SetPoint("RIGHT", arenaTestRow, "RIGHT", 0, 0)
    arenaTestHint:SetJustifyH("LEFT")

    -- ============================================================================
    -- POSITIONING
    -- ============================================================================

    local posGroup = CreateFrame("Frame", nil, g)
    posGroup:SetPoint("TOPLEFT", arenaGroup, "BOTTOMLEFT", 0, -20)
    posGroup:SetPoint("TOPRIGHT", -18, 0)
    posGroup:SetHeight(360)

    local posTitle = AC:CreateStyledText(posGroup, "Positioning", 12, COLORS.TEXT, "OVERLAY", "")
    posTitle:SetPoint("TOPLEFT", 20, -16)

    local posDesc = AC:CreateStyledText(posGroup, "Fine-tune icon positions. Note: Arena frame positions also use theme-specific offsets for optimal placement.", 10, COLORS.TEXT_MUTED, "OVERLAY", "")
    posDesc:SetPoint("TOPLEFT", posTitle, "BOTTOMLEFT", 0, -4)
    posDesc:SetWidth(posGroup:GetWidth() - 40)
    posDesc:SetJustifyH("LEFT")
    posDesc:SetWordWrap(true)

    -- ============================================================================
    -- FRIENDLY TARGETS ON ARENA FRAMES POSITIONING
    -- ============================================================================

    local friendlyPosTitle = AC:CreateStyledText(posGroup, "Friendly Targets on Arena Frames", 11, COLORS.TEXT_2, "OVERLAY", "")
    friendlyPosTitle:SetPoint("TOPLEFT", posDesc, "BOTTOMLEFT", 0, -12)

    -- Horizontal Offset slider (Friendly on Arena)
    local hOffsetRow = CreateFrame("Frame", nil, posGroup)
    hOffsetRow:SetPoint("TOPLEFT", friendlyPosTitle, "BOTTOMLEFT", 0, -8)
    hOffsetRow:SetPoint("RIGHT", posGroup, "RIGHT", -20, 0)
    hOffsetRow:SetHeight(30)

    local hOffsetSlider = AC:CreateEnhancedSliderRow(hOffsetRow, "Horizontal Offset", 0, "targetOfTargets.alliesOnArena.offsetX", "offset")
    hOffsetSlider:SetPoint("TOPLEFT", 0, 0)
    hOffsetSlider:SetPoint("RIGHT", 0, 0)
    hOffsetSlider:SetHeight(30)

    -- Vertical Offset slider (Friendly on Arena)
    local vOffsetRow = CreateFrame("Frame", nil, posGroup)
    vOffsetRow:SetPoint("TOPLEFT", hOffsetRow, "BOTTOMLEFT", 0, -8)
    vOffsetRow:SetPoint("RIGHT", posGroup, "RIGHT", -20, 0)
    vOffsetRow:SetHeight(30)

    local vOffsetSlider = AC:CreateEnhancedSliderRow(vOffsetRow, "Vertical Offset", 0, "targetOfTargets.alliesOnArena.offsetY", "offset")
    vOffsetSlider:SetPoint("TOPLEFT", 0, 0)
    vOffsetSlider:SetPoint("RIGHT", 0, 0)
    vOffsetSlider:SetHeight(30)

    -- ============================================================================
    -- ARENA TARGETS ON PARTY FRAMES POSITIONING
    -- ============================================================================

    local arenaPosTitle = AC:CreateStyledText(posGroup, "Arena Targets on Party Frames", 11, COLORS.TEXT_2, "OVERLAY", "")
    arenaPosTitle:SetPoint("TOPLEFT", vOffsetRow, "BOTTOMLEFT", 0, -12)

    -- Horizontal Offset slider (Arena on Party)
    local arenaHOffsetRow = CreateFrame("Frame", nil, posGroup)
    arenaHOffsetRow:SetPoint("TOPLEFT", arenaPosTitle, "BOTTOMLEFT", 0, -8)
    arenaHOffsetRow:SetPoint("RIGHT", posGroup, "RIGHT", -20, 0)
    arenaHOffsetRow:SetHeight(30)

    local arenaHOffsetSlider = AC:CreateEnhancedSliderRow(arenaHOffsetRow, "Horizontal Offset", 0, "targetOfTargets.attackersOnParty.offsetX", "offset")
    arenaHOffsetSlider:SetPoint("TOPLEFT", 0, 0)
    arenaHOffsetSlider:SetPoint("RIGHT", 0, 0)
    arenaHOffsetSlider:SetHeight(30)

    -- Vertical Offset slider (Arena on Party)
    local arenaVOffsetRow = CreateFrame("Frame", nil, posGroup)
    arenaVOffsetRow:SetPoint("TOPLEFT", arenaHOffsetRow, "BOTTOMLEFT", 0, -8)
    arenaVOffsetRow:SetPoint("RIGHT", posGroup, "RIGHT", -20, 0)
    arenaVOffsetRow:SetHeight(30)

    local arenaVOffsetSlider = AC:CreateEnhancedSliderRow(arenaVOffsetRow, "Vertical Offset", 0, "targetOfTargets.attackersOnParty.offsetY", "offset")
    arenaVOffsetSlider:SetPoint("TOPLEFT", 0, 0)
    arenaVOffsetSlider:SetPoint("RIGHT", 0, 0)
    arenaVOffsetSlider:SetHeight(30)

    -- Hook sliders to refresh TargetOfTargets
    C_Timer.After(0.1, function()
        if AC.sliderWidgets then
            local sliderPaths = {
                "targetOfTargets.alliesOnArena.iconScale",
                "targetOfTargets.alliesOnArena.spacing",
                "targetOfTargets.alliesOnArena.offsetX",
                "targetOfTargets.alliesOnArena.offsetY",
                "targetOfTargets.attackersOnParty.iconScale",
                "targetOfTargets.attackersOnParty.spacing",
                "targetOfTargets.attackersOnParty.offsetX",
                "targetOfTargets.attackersOnParty.offsetY",
            }
            
            for _, path in ipairs(sliderPaths) do
                local slider = AC.sliderWidgets[path]
                if slider and slider.HookScript then
                    slider:HookScript("OnValueChanged", function()
                        if AC.TargetOfTargets and AC.TargetOfTargets.Refresh then
                            AC.TargetOfTargets:Refresh()
                        end
                    end)
                end
            end
        end
    end)
end

-- Export the page builder
AC.BuildTargetOfTargetsPage = BuildTargetOfTargetsPage
