local addonName, privateDB = ...

-- ============================================================================
-- CONFIG TAB MODULE
-- Settings UI and contributor credits
-- ============================================================================

local CONFIG = privateDB.CONFIG
local L = HDG_L or {}  -- Localization
local HDG_ConfigTab = {}
privateDB.HDG_ConfigTab = HDG_ConfigTab

local mainFrame = nil

-- ============================================================================
-- THEME HELPERS (delegating to ThemeEngine canonical helpers)
-- ============================================================================

local HDG = _G.HousingDecorGuide
local function GetScheme() return HDG.Theme.GetCurrentScheme() end
local function RegisterWidget(w, t) HDG.Theme.RegisterWidget(w, t) end
local ApplyFont = function(fs, s, ft) HDG.Theme.ApplyFont(fs, s, ft) end
local function AddSectionStyle(fs, _) RegisterWidget(fs, "SectionHeader") end

-- Color code helpers (deferred lookup - ThemeEngine may not be initialized at load time)
-- Only used for print() statements and multi-color text where SetTextColor won't work
local function C_Text() return HDG.Theme.C_Text() end
local function C_Dim() return HDG.Theme.C_Dim() end
local function C_Accent() return HDG.Theme.C_Accent() end
local function C_Warning() return HDG.Theme.C_Warning() end

-- ============================================================================
-- STORE HELPERS
-- ============================================================================

local function GetStore()
    local HDG = _G.HousingDecorGuide
    return HDG and HDG.Store
end

local function GetState()
    local store = GetStore()
    return store and store:GetState()
end

local function Dispatch(action, payload)
    local store = GetStore()
    if store then
        store:Dispatch(action, payload)
    end
end

local function GetConfig(key)
    local state = GetState()
    return state and state.config and state.config[key]
end

local function GetUIState(key)
    local state = GetState()
    return state and state.ui and state.ui[key]
end

-- ============================================================================
-- UI HELPERS
-- ============================================================================

-- Apply theme text color to a font string (inline color codes don't work reliably)
local function SetThemedText(fontString, text, scheme, colorKey)
    fontString:SetText(text)
    local s = scheme or GetScheme()
    if s then
        local color = s[colorKey or "text"]
        if color then
            fontString:SetTextColor(color.r, color.g, color.b, color.a or 1)
        end
    end
end

-- ============================================================================
-- INITIALIZATION
-- ============================================================================

function HDG_ConfigTab.Init(frame)
    mainFrame = frame
end

-- ============================================================================
-- CREATE PANEL
-- ============================================================================

function HDG_ConfigTab.Create()
    if not mainFrame then return end

    -- Use factory for panel structure
    local layout = HDG.UI:TabPanel(mainFrame, {
        title = "Configuration",
        hasHeaderBar = true,
        hasScroll = true,
    })

    local configFrame = layout.frame
    configFrame:Hide()
    mainFrame.configFrame = configFrame

    local scrollChild = layout.scrollChild
    scrollChild:SetWidth(CONFIG.MAIN_FRAME_WIDTH - 60)
    scrollChild:SetHeight(1200) -- Will be adjusted based on content

    local yOffset = -10
    local rightColumnX = 380  -- X position for right column

    -- Helper function to create separator lines
    local function CreateSeparator(yPos)
        local sep = scrollChild:CreateTexture(nil, "ARTWORK")
        sep:SetHeight(1)
        sep:SetPoint("TOPLEFT", 10, yPos)
        sep:SetPoint("TOPRIGHT", -10, yPos)
        local sepScheme = GetScheme()
        if sepScheme then
            sep:SetColorTexture(sepScheme.border.r, sepScheme.border.g, sepScheme.border.b, 0.5)
        else
            sep:SetColorTexture(0.3, 0.3, 0.35, 0.5)
        end
        return sep
    end

    -- ========================================
    -- APPEARANCE SECTION (UI Scale + Font Scale)
    -- ========================================
    local appearanceScheme = GetScheme()

    local appearanceHeader = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    appearanceHeader:SetPoint("TOPLEFT", 10, yOffset)
    ApplyFont(appearanceHeader, appearanceScheme, "body")
    appearanceHeader:SetText("Appearance")
    if appearanceScheme then
        appearanceHeader:SetTextColor(appearanceScheme.accent.r, appearanceScheme.accent.g, appearanceScheme.accent.b, appearanceScheme.accent.a)
    end
    AddSectionStyle(appearanceHeader, scrollChild)
    RegisterWidget(appearanceHeader, "SectionTitle")

    -- Track active dropdown menu for closing (shared by font + waypoint dropdowns)
    local activeFontMenu = nil

    -- Waypoint System dropdown (right side of Appearance row)
    local wpLabel = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    wpLabel:SetPoint("TOPLEFT", rightColumnX, yOffset)
    ApplyFont(wpLabel, appearanceScheme, "body")
    SetThemedText(wpLabel, "Waypoint System:", appearanceScheme, "text")

    local wpDropdown = CreateFrame("Button", "HDG_WaypointDropdown", scrollChild, "BackdropTemplate")
    wpDropdown:SetSize(130, 22)
    wpDropdown:SetPoint("LEFT", wpLabel, "RIGHT", 8, 0)
    wpDropdown:SetBackdrop({
        bgFile = CONFIG.TEXTURES.WHITE_BG,
        edgeFile = CONFIG.TEXTURES.WHITE_BG,
        edgeSize = 1,
    })
    if appearanceScheme then
        wpDropdown:SetBackdropColor(appearanceScheme.panel.r, appearanceScheme.panel.g, appearanceScheme.panel.b, 0.9)
        wpDropdown:SetBackdropBorderColor(appearanceScheme.border.r, appearanceScheme.border.g, appearanceScheme.border.b, 1)
    end
    RegisterWidget(wpDropdown, "Panel")

    wpDropdown.text = wpDropdown:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    wpDropdown.text:SetPoint("LEFT", 8, 0)
    ApplyFont(wpDropdown.text, appearanceScheme, "small")

    wpDropdown.arrow = wpDropdown:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    wpDropdown.arrow:SetPoint("RIGHT", -8, 0)
    ApplyFont(wpDropdown.arrow, appearanceScheme, "small")
    SetThemedText(wpDropdown.arrow, "v", appearanceScheme, "dim")

    local wpOptions = {
        { key = "auto",   label = "Auto" },
        { key = "native", label = "Blizzard Native" },
        { key = "tomtom", label = "TomTom Only" },
    }
    local wpDisplayMap = { auto = "Auto", native = "Blizzard Native", tomtom = "TomTom Only" }
    local currentWP = GetConfig("waypointProvider") or "auto"
    wpDropdown.text:SetText(wpDisplayMap[currentWP] or "Auto")

    local wpHint = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    wpHint:SetPoint("TOPLEFT", rightColumnX, yOffset - 16)
    SetThemedText(wpHint, "(Auto uses TomTom if installed)", appearanceScheme, "dim")
    ApplyFont(wpHint, appearanceScheme, "small")

    wpDropdown:SetScript("OnClick", function(self)
        if activeFontMenu then activeFontMenu:Hide(); activeFontMenu = nil end
        local menu = CreateFrame("Frame", nil, self, "BackdropTemplate")
        menu:SetSize(self:GetWidth(), #wpOptions * 20 + 4)
        menu:SetPoint("TOP", self, "BOTTOM", 0, -2)
        menu:SetBackdrop({
            bgFile = CONFIG.TEXTURES.WHITE_BG,
            edgeFile = CONFIG.TEXTURES.WHITE_BG,
            edgeSize = 1,
        })
        local mScheme = GetScheme()
        if mScheme then
            menu:SetBackdropColor(mScheme.panel.r, mScheme.panel.g, mScheme.panel.b, 0.95)
            menu:SetBackdropBorderColor(mScheme.border.r, mScheme.border.g, mScheme.border.b, 1)
        end
        menu:SetFrameStrata("TOOLTIP")
        activeFontMenu = menu

        for i, opt in ipairs(wpOptions) do
            local btn = CreateFrame("Button", nil, menu)
            btn:SetSize(menu:GetWidth() - 4, 20)
            btn:SetPoint("TOPLEFT", 2, -2 - (i - 1) * 20)
            btn.text = btn:CreateFontString(nil, "OVERLAY", "GameFontNormal")
            btn.text:SetPoint("LEFT", 8, 0)
            ApplyFont(btn.text, mScheme, "small")
            btn.text:SetText(opt.label)
            if mScheme then btn.text:SetTextColor(mScheme.text.r, mScheme.text.g, mScheme.text.b, 1) end
            btn:SetScript("OnEnter", function(b)
                local s = GetScheme()
                if s then b.text:SetTextColor(s.accent.r, s.accent.g, s.accent.b, 1) end
            end)
            btn:SetScript("OnLeave", function(b)
                local s = GetScheme()
                if s then b.text:SetTextColor(s.text.r, s.text.g, s.text.b, 1) end
            end)
            btn:SetScript("OnClick", function()
                Dispatch("SET_CONFIG", { key = "waypointProvider", value = opt.key })
                self.text:SetText(opt.label)
                menu:Hide()
                activeFontMenu = nil
            end)
        end
    end)

    yOffset = yOffset - 22

    -- Helper to create +/- button
    local function CreateAdjustButton(parent, label, width, height)
        local btn = CreateFrame("Button", nil, parent, "BackdropTemplate")
        btn:SetSize(width, height)
        btn:SetBackdrop({
            bgFile = CONFIG.TEXTURES.WHITE_BG,
            edgeFile = CONFIG.TEXTURES.WHITE_BG,
            edgeSize = 1,
        })
        btn:SetBackdropColor(appearanceScheme.panel.r, appearanceScheme.panel.g, appearanceScheme.panel.b, 0.9)
        btn:SetBackdropBorderColor(appearanceScheme.border.r, appearanceScheme.border.g, appearanceScheme.border.b, 1)
        btn.text = btn:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        btn.text:SetPoint("CENTER", 0, 0)
        ApplyFont(btn.text, appearanceScheme, "body")
        btn.text:SetText(label)
        btn.text:SetTextColor(appearanceScheme.text.r, appearanceScheme.text.g, appearanceScheme.text.b, 1)
        btn:SetScript("OnEnter", function(self)
            self:SetBackdropColor(appearanceScheme.button_hover.r, appearanceScheme.button_hover.g, appearanceScheme.button_hover.b, 0.9)
            self.text:SetTextColor(appearanceScheme.accent.r, appearanceScheme.accent.g, appearanceScheme.accent.b, 1)
        end)
        btn:SetScript("OnLeave", function(self)
            self:SetBackdropColor(appearanceScheme.panel.r, appearanceScheme.panel.g, appearanceScheme.panel.b, 0.9)
            self.text:SetTextColor(appearanceScheme.text.r, appearanceScheme.text.g, appearanceScheme.text.b, 1)
        end)
        RegisterWidget(btn, "Button")
        return btn
    end

    -- UI Scale +/- control
    local uiScaleLabel = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    uiScaleLabel:SetPoint("TOPLEFT", 10, yOffset)
    ApplyFont(uiScaleLabel, appearanceScheme, "body")
    SetThemedText(uiScaleLabel, "UI Scale:", appearanceScheme, "text")

    local uiScaleDownBtn = CreateAdjustButton(scrollChild, "-", 24, 22)
    uiScaleDownBtn:SetPoint("LEFT", uiScaleLabel, "RIGHT", 8, 0)

    local uiScaleValueText = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    uiScaleValueText:SetPoint("LEFT", uiScaleDownBtn, "RIGHT", 6, 0)
    ApplyFont(uiScaleValueText, appearanceScheme, "body")
    uiScaleValueText:SetTextColor(appearanceScheme.accent.r, appearanceScheme.accent.g, appearanceScheme.accent.b, 1)

    local function UpdateUIScaleText(value)
        uiScaleValueText:SetText(string.format("%.0f%%", value * 100))
    end
    UpdateUIScaleText(GetConfig("scale") or 1.0)

    local uiScaleUpBtn = CreateAdjustButton(scrollChild, "+", 24, 22)
    uiScaleUpBtn:SetPoint("LEFT", uiScaleValueText, "RIGHT", 6, 0)

    uiScaleDownBtn:SetScript("OnClick", function()
        local current = GetConfig("scale") or 1.0
        local newScale = math.max(current - 0.1, 0.8)
        newScale = math.floor(newScale * 10 + 0.5) / 10  -- Round to 1 decimal
        Dispatch("SET_CONFIG", { key = "scale", value = newScale })
        UpdateUIScaleText(newScale)
        if mainFrame then mainFrame:SetScale(newScale) end
    end)

    uiScaleUpBtn:SetScript("OnClick", function()
        local current = GetConfig("scale") or 1.0
        local newScale = math.min(current + 0.1, 1.5)
        newScale = math.floor(newScale * 10 + 0.5) / 10  -- Round to 1 decimal
        Dispatch("SET_CONFIG", { key = "scale", value = newScale })
        UpdateUIScaleText(newScale)
        if mainFrame then mainFrame:SetScale(newScale) end
    end)

    -- Font Scale control hidden (code retained for future use)

    yOffset = yOffset - 30

    -- Font Family dropdown (row 2 of appearance)
    local fontFamilyLabel = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    fontFamilyLabel:SetPoint("TOPLEFT", 10, yOffset)
    ApplyFont(fontFamilyLabel, appearanceScheme, "body")
    SetThemedText(fontFamilyLabel, "Font:", appearanceScheme, "text")

    local fontDropdown = CreateFrame("Button", "HDG_FontDropdown", scrollChild, "BackdropTemplate")
    fontDropdown:SetSize(140, 22)
    fontDropdown:SetPoint("LEFT", fontFamilyLabel, "RIGHT", 8, 0)
    fontDropdown:SetBackdrop({
        bgFile = CONFIG.TEXTURES.WHITE_BG,
        edgeFile = CONFIG.TEXTURES.WHITE_BG,
        edgeSize = 1,
    })
    if appearanceScheme then
        fontDropdown:SetBackdropColor(appearanceScheme.panel.r, appearanceScheme.panel.g, appearanceScheme.panel.b, 0.9)
        fontDropdown:SetBackdropBorderColor(appearanceScheme.border.r, appearanceScheme.border.g, appearanceScheme.border.b, 1)
    end
    RegisterWidget(fontDropdown, "Panel")

    fontDropdown.text = fontDropdown:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    fontDropdown.text:SetPoint("LEFT", 8, 0)
    ApplyFont(fontDropdown.text, appearanceScheme, "small")

    fontDropdown.arrow = fontDropdown:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    fontDropdown.arrow:SetPoint("RIGHT", -8, 0)
    ApplyFont(fontDropdown.arrow, appearanceScheme, "small")
    SetThemedText(fontDropdown.arrow, "v", appearanceScheme, "dim")

    -- Set current font display
    local currentFont = GetConfig("fontFamily") or "Expressway"
    local currentFontName = HDG.Colors.FontDisplayNames[currentFont] or currentFont
    fontDropdown.text:SetText(currentFontName)
    fontDropdown.selected = currentFont


    fontDropdown:SetScript("OnClick", function(self)
        -- Close existing menu
        if activeFontMenu then
            activeFontMenu:Hide()
            activeFontMenu = nil
            return
        end

        -- Create dropdown menu
        local menu = CreateFrame("Frame", nil, UIParent, "BackdropTemplate")
        menu:SetFrameStrata("FULLSCREEN_DIALOG")
        menu:SetBackdrop({
            bgFile = CONFIG.TEXTURES.WHITE_BG,
            edgeFile = CONFIG.TEXTURES.WHITE_BG,
            edgeSize = 1,
        })
        local menuScheme = GetScheme()
        if menuScheme then
            menu:SetBackdropColor(menuScheme.bg.r, menuScheme.bg.g, menuScheme.bg.b, 0.98)
            menu:SetBackdropBorderColor(menuScheme.border.r, menuScheme.border.g, menuScheme.border.b, 1)
        end

        local menuHeight = #HDG.Colors.FontOrder * 22 + 4
        menu:SetSize(140, menuHeight)
        menu:SetPoint("TOPLEFT", self, "BOTTOMLEFT", 0, -2)

        activeFontMenu = menu

        for i, fontKey in ipairs(HDG.Colors.FontOrder) do
            local btn = CreateFrame("Button", nil, menu, "BackdropTemplate")
            btn:SetSize(136, 20)
            btn:SetPoint("TOPLEFT", 2, -2 - (i-1) * 22)
            btn:SetBackdrop({ bgFile = "Interface\\Buttons\\WHITE8x8" })
            btn:SetBackdropColor(0, 0, 0, 0)

            btn.text = btn:CreateFontString(nil, "OVERLAY", "GameFontNormal")
            btn.text:SetPoint("LEFT", 6, 0)
            -- Use the actual font for preview
            local fontPath = HDG.Colors.FontFiles[fontKey]
            btn.text:SetFont(fontPath, 11, "")
            btn.text:SetText(HDG.Colors.FontDisplayNames[fontKey] or fontKey)
            if menuScheme then
                btn.text:SetTextColor(menuScheme.text.r, menuScheme.text.g, menuScheme.text.b)
            end

            btn:SetScript("OnEnter", function(b)
                if menuScheme then
                    b:SetBackdropColor(menuScheme.accent.r, menuScheme.accent.g, menuScheme.accent.b, 0.3)
                end
            end)
            btn:SetScript("OnLeave", function(b)
                b:SetBackdropColor(0, 0, 0, 0)
            end)

            btn:SetScript("OnClick", function()
                Dispatch("SET_CONFIG", { key = "fontFamily", value = fontKey })
                self.text:SetText(HDG.Colors.FontDisplayNames[fontKey] or fontKey)
                self.selected = fontKey
                menu:Hide()
                activeFontMenu = nil
                -- Trigger live theme refresh
                if HDG.EventBus then
                    HDG.EventBus:Trigger("HDG_THEME_UPDATE", {})
                end
                print(C_Accent() .. "[HDG]|r Font changed to " .. (HDG.Colors.FontDisplayNames[fontKey] or fontKey))
            end)
        end

        -- Close on click outside
        menu:SetScript("OnHide", function()
            activeFontMenu = nil
        end)
    end)

    yOffset = yOffset - 32
    CreateSeparator(yOffset)
    yOffset = yOffset - 12

    -- ========================================
    -- TWO-COLUMN LAYOUT: General Settings (left) | Lumber Tracking (right)
    -- ========================================

    -- Section headers
    local ghScheme = GetScheme()
    local generalHeader = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    generalHeader:SetPoint("TOPLEFT", 10, yOffset)
    ApplyFont(generalHeader, ghScheme, "body")
    generalHeader:SetText("General")
    if ghScheme then
        generalHeader:SetTextColor(ghScheme.accent.r, ghScheme.accent.g, ghScheme.accent.b, ghScheme.accent.a)
    end
    AddSectionStyle(generalHeader, scrollChild)
    RegisterWidget(generalHeader, "SectionTitle")

    local lumberHeader = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    lumberHeader:SetPoint("TOPLEFT", rightColumnX, yOffset)
    ApplyFont(lumberHeader, ghScheme, "body")
    lumberHeader:SetText("Lumber Tracking")
    if ghScheme then
        lumberHeader:SetTextColor(ghScheme.accent.r, ghScheme.accent.g, ghScheme.accent.b, ghScheme.accent.a)
    end
    AddSectionStyle(lumberHeader, scrollChild)
    RegisterWidget(lumberHeader, "SectionTitle")

    yOffset = yOffset - 20
    local row1Y = yOffset

    -- === LEFT COLUMN ROW 1: Minimap Button ===
    local minimapCheckbox = CreateFrame("CheckButton", "HDG_MinimapCheckbox", scrollChild, "UICheckButtonTemplate")
    minimapCheckbox:SetPoint("TOPLEFT", 10, row1Y)
    minimapCheckbox:SetSize(26, 26)
    local showMinimap = HDG_Settings.minimap and HDG_Settings.minimap.showMinimapButton
    if showMinimap == nil then showMinimap = true end
    minimapCheckbox:SetChecked(showMinimap)

    local minimapLabel = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    minimapLabel:SetPoint("LEFT", minimapCheckbox, "RIGHT", 5, 0)
    ApplyFont(minimapLabel, ghScheme, "body")
    SetThemedText(minimapLabel, "Show Minimap Button", ghScheme, "text")

    local minimapHint = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    minimapHint:SetPoint("TOPLEFT", minimapCheckbox, "BOTTOMLEFT", 26, 2)
    ApplyFont(minimapHint, ghScheme, "small")
    SetThemedText(minimapHint, "(Draggable icon around the minimap)", ghScheme, "dim")

    minimapCheckbox:SetScript("OnClick", function(self)
        local minimap = privateDB.HDG_Minimap
        if minimap and minimap.SetShowMinimapButton then
            minimap.SetShowMinimapButton(self:GetChecked())
        end
    end)

    -- === RIGHT COLUMN ROW 1: Auto-Enable Lumber Tracker ===
    local lumberAutoShowCheckbox = CreateFrame("CheckButton", "HDG_LumberAutoShowCheckbox", scrollChild, "UICheckButtonTemplate")
    lumberAutoShowCheckbox:SetPoint("TOPLEFT", rightColumnX, row1Y)
    lumberAutoShowCheckbox:SetSize(26, 26)
    local autoShow = GetConfig("lumberAutoShow")
    if autoShow == nil then autoShow = false end
    lumberAutoShowCheckbox:SetChecked(autoShow)

    local lumberAutoShowLabel = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    lumberAutoShowLabel:SetPoint("LEFT", lumberAutoShowCheckbox, "RIGHT", 5, 0)
    ApplyFont(lumberAutoShowLabel, ghScheme, "body")
    SetThemedText(lumberAutoShowLabel, "Auto-Enable Lumber Tracker", ghScheme, "text")

    local lumberAutoShowHint = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    lumberAutoShowHint:SetPoint("TOPLEFT", lumberAutoShowCheckbox, "BOTTOMLEFT", 26, 2)
    SetThemedText(lumberAutoShowHint, "(Auto-show tracker when collecting lumber)", ghScheme, "dim")
    ApplyFont(lumberAutoShowHint, ghScheme, "small")

    lumberAutoShowCheckbox:SetScript("OnClick", function(self)
        Dispatch("SET_CONFIG", { key = "lumberAutoShow", value = self:GetChecked() })
    end)

    local row2Y = row1Y - 38

    -- === LEFT COLUMN ROW 2: Addon Compartment ===
    local minimap = privateDB.HDG_Minimap
    local compartmentAvailable = minimap and minimap.IsCompartmentAvailable and minimap.IsCompartmentAvailable()

    local compartmentCheckbox = CreateFrame("CheckButton", "HDG_CompartmentCheckbox", scrollChild, "UICheckButtonTemplate")
    compartmentCheckbox:SetPoint("TOPLEFT", 10, row2Y)
    compartmentCheckbox:SetSize(26, 26)
    local showCompartment = HDG_Settings.minimap and HDG_Settings.minimap.showInCompartment
    if showCompartment == nil then showCompartment = true end
    compartmentCheckbox:SetChecked(showCompartment)

    local compartmentLabel = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    compartmentLabel:SetPoint("LEFT", compartmentCheckbox, "RIGHT", 5, 0)
    ApplyFont(compartmentLabel, ghScheme, "body")
    SetThemedText(compartmentLabel, "Show in Addon Compartment", ghScheme, "text")

    local compartmentHint = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    compartmentHint:SetPoint("TOPLEFT", compartmentCheckbox, "BOTTOMLEFT", 26, 2)
    SetThemedText(compartmentHint, "(Minimap drawer menu - requires /reload)", ghScheme, "dim")
    ApplyFont(compartmentHint, ghScheme, "small")

    if not compartmentAvailable then
        compartmentCheckbox:Disable()
        compartmentCheckbox:SetAlpha(0.5)
        compartmentLabel:SetAlpha(0.5)
        compartmentHint:SetAlpha(0.5)
    end

    compartmentCheckbox:SetScript("OnClick", function(self)
        if HDG_Settings.minimap then
            HDG_Settings.minimap.showInCompartment = self:GetChecked()
        end
        print(C_Warning() .. "[HDG]|r Addon compartment setting changed. Use " .. C_Text() .. "/reload|r to apply.")
    end)

    -- === RIGHT COLUMN ROW 2: Auto-Show Lumber Radar ===
    local lumberHUDCheckbox = CreateFrame("CheckButton", "HDG_LumberHUDCheckbox", scrollChild, "UICheckButtonTemplate")
    lumberHUDCheckbox:SetPoint("TOPLEFT", rightColumnX, row2Y)
    lumberHUDCheckbox:SetSize(26, 26)
    local hudEnabled = GetConfig("lumberHUDEnabled")
    if hudEnabled == nil then hudEnabled = false end
    lumberHUDCheckbox:SetChecked(hudEnabled)

    local lumberHUDLabel = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    lumberHUDLabel:SetPoint("LEFT", lumberHUDCheckbox, "RIGHT", 5, 0)
    ApplyFont(lumberHUDLabel, ghScheme, "body")
    SetThemedText(lumberHUDLabel, "Auto-Show Lumber Radar", ghScheme, "text")

    local lumberHUDHint = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    lumberHUDHint:SetPoint("TOPLEFT", lumberHUDCheckbox, "BOTTOMLEFT", 26, 2)
    SetThemedText(lumberHUDHint, "(/lumberhud to toggle manually)", ghScheme, "dim")
    ApplyFont(lumberHUDHint, ghScheme, "small")

    lumberHUDCheckbox:SetScript("OnClick", function(self)
        Dispatch("SET_CONFIG", { key = "lumberHUDEnabled", value = self:GetChecked() })
        if not self:GetChecked() and privateDB.HDG_LumberHUD then
            privateDB.HDG_LumberHUD.Hide()
        end
    end)

    local row3Y = row2Y - 38

    -- === LEFT COLUMN ROW 3: Profession Window Buttons ===
    local profButtonCheckbox = CreateFrame("CheckButton", "HDG_ProfButtonCheckbox", scrollChild, "UICheckButtonTemplate")
    profButtonCheckbox:SetPoint("TOPLEFT", 10, row3Y)
    profButtonCheckbox:SetSize(26, 26)
    local showProfButtons = GetConfig("showProfessionButtons")
    if showProfButtons == nil then showProfButtons = true end
    profButtonCheckbox:SetChecked(showProfButtons)

    local profButtonLabel = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    profButtonLabel:SetPoint("LEFT", profButtonCheckbox, "RIGHT", 5, 0)
    ApplyFont(profButtonLabel, ghScheme, "body")
    SetThemedText(profButtonLabel, "Show Profession Window Buttons", ghScheme, "text")

    local profButtonHint = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    profButtonHint:SetPoint("TOPLEFT", profButtonCheckbox, "BOTTOMLEFT", 26, 2)
    SetThemedText(profButtonHint, "(Decor Guide & Filter Decor buttons)", ghScheme, "dim")
    ApplyFont(profButtonHint, ghScheme, "small")

    profButtonCheckbox:SetScript("OnClick", function(self)
        Dispatch("SET_CONFIG", { key = "showProfessionButtons", value = self:GetChecked() })
        if HDG_UI and HDG_UI.UpdateProfessionButtonVisibility then
            HDG_UI.UpdateProfessionButtonVisibility()
        end
    end)

    -- === RIGHT COLUMN ROW 3: Radar Scale Slider ===
    local radarScaleLabel = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    radarScaleLabel:SetPoint("TOPLEFT", rightColumnX + 26, row3Y)
    ApplyFont(radarScaleLabel, ghScheme, "body")
    SetThemedText(radarScaleLabel, "Radar Scale:", ghScheme, "text")

    local radarScaleSlider = CreateFrame("Slider", "HDG_RadarScaleSlider", scrollChild, "OptionsSliderTemplate")
    radarScaleSlider:SetPoint("LEFT", radarScaleLabel, "RIGHT", 8, 0)
    radarScaleSlider:SetSize(100, 16)
    radarScaleSlider:SetMinMaxValues(0.5, 2.5)
    radarScaleSlider:SetValueStep(0.1)
    radarScaleSlider:SetObeyStepOnDrag(true)
    radarScaleSlider:SetValue(GetConfig("lumberHUDScale") or 1.0)

    radarScaleSlider.Low:SetText("")
    radarScaleSlider.High:SetText("")
    radarScaleSlider.Text:SetText("")

    local radarScaleValueText = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    radarScaleValueText:SetPoint("LEFT", radarScaleSlider, "RIGHT", 6, 0)
    ApplyFont(radarScaleValueText, ghScheme, "body")

    local function UpdateRadarScaleText(value)
        radarScaleValueText:SetText(string.format("%.1fx", value))
        local s = GetScheme()
        if s and s.success then
            radarScaleValueText:SetTextColor(s.success.r, s.success.g, s.success.b, s.success.a or 1)
        end
    end
    UpdateRadarScaleText(GetConfig("lumberHUDScale") or 1.0)

    local radarScaleHint = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    radarScaleHint:SetPoint("TOPLEFT", rightColumnX + 26, row3Y - 18)
    SetThemedText(radarScaleHint, "(0.5x to 2.5x - resize the radar)", ghScheme, "dim")
    ApplyFont(radarScaleHint, ghScheme, "small")

    radarScaleSlider:SetScript("OnValueChanged", function(self, value)
        value = math.floor(value * 10 + 0.5) / 10
        Dispatch("SET_CONFIG", { key = "lumberHUDScale", value = value })
        UpdateRadarScaleText(value)
        if privateDB.HDG_LumberHUD and privateDB.HDG_LumberHUD.UpdateScale then
            privateDB.HDG_LumberHUD.UpdateScale(value)
        end
    end)

    local row4Y = row3Y - 38

    -- === LEFT COLUMN ROW 4: Pin Lumber Tracker ===
    local lumberPinCheckbox = CreateFrame("CheckButton", "HDG_LumberPinCheckbox", scrollChild, "UICheckButtonTemplate")
    lumberPinCheckbox:SetPoint("TOPLEFT", 10, row4Y)
    lumberPinCheckbox:SetSize(26, 26)
    lumberPinCheckbox:SetChecked(GetUIState("lumberPinned") or false)

    local lumberPinLabel = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    lumberPinLabel:SetPoint("LEFT", lumberPinCheckbox, "RIGHT", 5, 0)
    ApplyFont(lumberPinLabel, ghScheme, "body")
    SetThemedText(lumberPinLabel, "Pin Lumber Tracker", ghScheme, "text")

    local lumberPinHint = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    lumberPinHint:SetPoint("TOPLEFT", lumberPinCheckbox, "BOTTOMLEFT", 26, 2)
    SetThemedText(lumberPinHint, "(Floating widget - also via pin button)", ghScheme, "dim")
    ApplyFont(lumberPinHint, ghScheme, "small")

    lumberPinCheckbox:SetScript("OnClick", function(self)
        if self:GetChecked() then
            if HDG_LumberTracker and HDG_LumberTracker.ShowPinned then
                HDG_LumberTracker.ShowPinned()
            end
        else
            Dispatch("SET_LUMBER_PINNED", { pinned = false })
            if HDG_LumberTracker and HDG_LumberTracker.HidePinned then
                HDG_LumberTracker.HidePinned()
            end
            if HDG_UI and HDG_UI.UpdateLumberLayout then
                HDG_UI.UpdateLumberLayout()
            end
        end
    end)

    -- Store reference for external updates
    HDG_ConfigTab.lumberPinCheckbox = lumberPinCheckbox

    -- === RIGHT COLUMN ROW 4: Radar Framerate Slider ===
    local radarFPSLabel = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    radarFPSLabel:SetPoint("TOPLEFT", rightColumnX + 26, row4Y)
    ApplyFont(radarFPSLabel, ghScheme, "body")
    SetThemedText(radarFPSLabel, "Radar FPS:", ghScheme, "text")

    local radarFPSSlider = CreateFrame("Slider", "HDG_RadarFPSSlider", scrollChild, "OptionsSliderTemplate")
    radarFPSSlider:SetPoint("LEFT", radarFPSLabel, "RIGHT", 8, 0)
    radarFPSSlider:SetSize(100, 16)
    radarFPSSlider:SetMinMaxValues(10, 30)
    radarFPSSlider:SetValueStep(5)
    radarFPSSlider:SetObeyStepOnDrag(true)
    radarFPSSlider:SetValue(GetConfig("lumberHUDFPS") or 20)

    radarFPSSlider.Low:SetText("")
    radarFPSSlider.High:SetText("")
    radarFPSSlider.Text:SetText("")

    local radarFPSValueText = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    radarFPSValueText:SetPoint("LEFT", radarFPSSlider, "RIGHT", 6, 0)
    ApplyFont(radarFPSValueText, ghScheme, "body")

    local function UpdateRadarFPSText(value)
        radarFPSValueText:SetText(string.format("%d", value))
        local s = GetScheme()
        if s and s.success then
            radarFPSValueText:SetTextColor(s.success.r, s.success.g, s.success.b, s.success.a or 1)
        end
    end
    UpdateRadarFPSText(GetConfig("lumberHUDFPS") or 20)

    local radarFPSHint = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    radarFPSHint:SetPoint("TOPLEFT", rightColumnX + 26, row4Y - 18)
    SetThemedText(radarFPSHint, "(10-30 fps - smoothness vs CPU)", ghScheme, "dim")
    ApplyFont(radarFPSHint, ghScheme, "small")

    radarFPSSlider:SetScript("OnValueChanged", function(self, value)
        value = math.floor(value + 0.5)
        Dispatch("SET_CONFIG", { key = "lumberHUDFPS", value = value })
        UpdateRadarFPSText(value)
        if privateDB.HDG_LumberHUD and privateDB.HDG_LumberHUD.UpdateFramerate then
            privateDB.HDG_LumberHUD.UpdateFramerate(value)
        end
    end)

    -- === LEFT COLUMN ROW 5: (empty - waypoint moved to appearance row) ===
    local row5Y = row4Y - 38

    -- === RIGHT COLUMN ROW 5: Pin Radar to Tracker ===

    local radarPinCheckbox = CreateFrame("CheckButton", "HDG_RadarPinCheckbox", scrollChild, "UICheckButtonTemplate")
    radarPinCheckbox:SetPoint("TOPLEFT", rightColumnX, row5Y)
    radarPinCheckbox:SetSize(26, 26)
    radarPinCheckbox:SetChecked(GetConfig("lumberRadarPinToTracker") or false)

    local radarPinLabel = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    radarPinLabel:SetPoint("LEFT", radarPinCheckbox, "RIGHT", 5, 0)
    ApplyFont(radarPinLabel, ghScheme, "body")
    SetThemedText(radarPinLabel, "Pin Radar to Tracker", ghScheme, "text")

    local radarPinHint = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    radarPinHint:SetPoint("TOPLEFT", radarPinCheckbox, "BOTTOMLEFT", 26, 2)
    SetThemedText(radarPinHint, "(Radar follows lumber tracker)", ghScheme, "dim")
    ApplyFont(radarPinHint, ghScheme, "small")

    radarPinCheckbox:SetScript("OnClick", function(self)
        Dispatch("SET_CONFIG", { key = "lumberRadarPinToTracker", value = self:GetChecked() })
        if privateDB.HDG_LumberHUD and privateDB.HDG_LumberHUD.UpdateAnchor then
            privateDB.HDG_LumberHUD.UpdateAnchor()
        end
    end)

    -- Update yOffset to after the 5-row grid
    yOffset = row5Y - 38

    -- Separator after settings grid
    CreateSeparator(yOffset)
    yOffset = yOffset - 18

    -- ========================================
    -- FARMING & CRAFTING QUOTES SECTION
    -- ========================================
    local qhScheme = GetScheme()
    local quotesHeader = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    quotesHeader:SetPoint("TOPLEFT", 10, yOffset)
    ApplyFont(quotesHeader, qhScheme, "body")
    quotesHeader:SetText("Farming & Crafting Quotes")
    if qhScheme then
        quotesHeader:SetTextColor(qhScheme.accent.r, qhScheme.accent.g, qhScheme.accent.b, qhScheme.accent.a)
    end
    AddSectionStyle(quotesHeader, scrollChild)
    RegisterWidget(quotesHeader, "SectionTitle")

    yOffset = yOffset - 20

    -- ========================================
    -- Enable Quotes Master Toggle
    -- ========================================
    local quotesEnabled = GetConfig("quotesEnabled")
    if quotesEnabled == nil then quotesEnabled = true end

    local quotesCheckbox = CreateFrame("CheckButton", "HDG_QuotesCheckbox", scrollChild, "UICheckButtonTemplate")
    quotesCheckbox:SetPoint("TOPLEFT", 10, yOffset)
    quotesCheckbox:SetSize(26, 26)
    quotesCheckbox:SetChecked(quotesEnabled)

    local quotesLabel = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    quotesLabel:SetPoint("LEFT", quotesCheckbox, "RIGHT", 5, 0)
    ApplyFont(quotesLabel, qhScheme, "body")
    SetThemedText(quotesLabel, "Enable Quotes", qhScheme, "text")

    local quotesHint = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    quotesHint:SetPoint("LEFT", quotesLabel, "RIGHT", 8, 0)
    SetThemedText(quotesHint, "(Master toggle for quote systems)", qhScheme, "dim")
    ApplyFont(quotesHint, qhScheme, "small")

    yOffset = yOffset - 24

    -- ========================================
    -- Only Print Quotes to Chat Option
    -- ========================================
    local quotesOnlyChat = GetConfig("quotesOnlyChat") or false

    local quotesOnlyChatCheckbox = CreateFrame("CheckButton", "HDG_QuotesOnlyChatCheckbox", scrollChild, "UICheckButtonTemplate")
    quotesOnlyChatCheckbox:SetPoint("TOPLEFT", 30, yOffset)  -- Indented under master toggle
    quotesOnlyChatCheckbox:SetSize(26, 26)
    quotesOnlyChatCheckbox:SetChecked(quotesOnlyChat)

    local quotesOnlyChatLabel = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    quotesOnlyChatLabel:SetPoint("LEFT", quotesOnlyChatCheckbox, "RIGHT", 5, 0)
    ApplyFont(quotesOnlyChatLabel, qhScheme, "body")
    SetThemedText(quotesOnlyChatLabel, "Only print quotes to chat", qhScheme, "text")

    local quotesOnlyChatHint = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    quotesOnlyChatHint:SetPoint("LEFT", quotesOnlyChatLabel, "RIGHT", 8, 0)
    SetThemedText(quotesOnlyChatHint, "(No talking head display)", qhScheme, "dim")
    ApplyFont(quotesOnlyChatHint, qhScheme, "small")

    quotesOnlyChatCheckbox:SetScript("OnClick", function(self)
        Dispatch("SET_CONFIG", { key = "quotesOnlyChat", value = self:GetChecked() })
    end)

    yOffset = yOffset - 24

    -- ========================================
    -- Lumber Quotes Sub-Option
    -- ========================================
    local lumberQuotesEnabled = GetConfig("lumberQuotesEnabled")
    if lumberQuotesEnabled == nil then lumberQuotesEnabled = true end

    local lumberQuotesCheckbox = CreateFrame("CheckButton", "HDG_LumberQuotesCheckbox", scrollChild, "UICheckButtonTemplate")
    lumberQuotesCheckbox:SetPoint("TOPLEFT", 30, yOffset)  -- Indented under master toggle
    lumberQuotesCheckbox:SetSize(26, 26)
    lumberQuotesCheckbox:SetChecked(lumberQuotesEnabled)

    local lumberQuotesLabel = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    lumberQuotesLabel:SetPoint("LEFT", lumberQuotesCheckbox, "RIGHT", 5, 0)
    ApplyFont(lumberQuotesLabel, qhScheme, "body")
    SetThemedText(lumberQuotesLabel, "Lumber Quotes", qhScheme, "text")

    local lumberQuotesHint = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    lumberQuotesHint:SetPoint("LEFT", lumberQuotesLabel, "RIGHT", 8, 0)
    SetThemedText(lumberQuotesHint, "(Vamoose, Squirrel, Gazlowe while lumber farming)", qhScheme, "dim")
    ApplyFont(lumberQuotesHint, qhScheme, "small")

    yOffset = yOffset - 24

    -- Store references for lumber quote sub-options
    local vamooseSlider, vamooseLabel, vamooseValueText, vamooseHint
    local vamooseChatCheckbox, vamooseChatLabel

    -- ========================================
    -- Vamoose Chattiness Slider (under Lumber Quotes)
    -- ========================================
    vamooseLabel = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    vamooseLabel:SetPoint("TOPLEFT", 61, yOffset)  -- Aligned with "Lumber Quotes" label (30 + 26 + 5)
    ApplyFont(vamooseLabel, qhScheme, "body")
    SetThemedText(vamooseLabel, "Chattiness:", qhScheme, "text")

    -- Slider (inline with label)
    vamooseSlider = CreateFrame("Slider", "HDG_VamooseChattinessSlider", scrollChild, "OptionsSliderTemplate")
    vamooseSlider:SetPoint("LEFT", vamooseLabel, "RIGHT", 10, 0)
    vamooseSlider:SetSize(120, 16)
    vamooseSlider:SetMinMaxValues(0, 99)
    vamooseSlider:SetValueStep(1)
    vamooseSlider:SetObeyStepOnDrag(true)
    vamooseSlider:SetValue(GetConfig("vamooseChattiness") or 30)

    -- Style the slider - hide low/high text, show value instead
    vamooseSlider.Low:SetText("")
    vamooseSlider.High:SetText("")
    vamooseSlider.Text:SetText("")

    -- Current value display (to the right of slider)
    vamooseValueText = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    vamooseValueText:SetPoint("LEFT", vamooseSlider, "RIGHT", 8, 0)
    ApplyFont(vamooseValueText, qhScheme, "body")

    local function UpdateVamooseValueText(value)
        local s = GetScheme()
        if value == 0 then
            vamooseValueText:SetText("Off")
            if s and s.error then
                vamooseValueText:SetTextColor(s.error.r, s.error.g, s.error.b, s.error.a or 1)
            end
        else
            vamooseValueText:SetText(string.format("%d", value))
            if s and s.success then
                vamooseValueText:SetTextColor(s.success.r, s.success.g, s.success.b, s.success.a or 1)
            end
        end
    end

    UpdateVamooseValueText(GetConfig("vamooseChattiness") or 30)

    vamooseSlider:SetScript("OnValueChanged", function(self, value)
        value = math.floor(value + 0.5)  -- Round to integer
        Dispatch("SET_CONFIG", { key = "vamooseChattiness", value = value })
        UpdateVamooseValueText(value)
    end)

    vamooseHint = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    vamooseHint:SetPoint("LEFT", vamooseValueText, "RIGHT", 8, 0)
    SetThemedText(vamooseHint, "(0=Off, or quote after 1 to N lumber)", qhScheme, "dim")
    ApplyFont(vamooseHint, qhScheme, "small")

    yOffset = yOffset - 24

    -- ========================================
    -- Craft Quotes Sub-Option
    -- ========================================
    local craftQuotesEnabled = GetConfig("craftQuotesEnabled")
    if craftQuotesEnabled == nil then craftQuotesEnabled = true end

    local craftQuotesCheckbox = CreateFrame("CheckButton", "HDG_CraftQuotesCheckbox", scrollChild, "UICheckButtonTemplate")
    craftQuotesCheckbox:SetPoint("TOPLEFT", 30, yOffset)  -- Indented under master toggle
    craftQuotesCheckbox:SetSize(26, 26)
    craftQuotesCheckbox:SetChecked(craftQuotesEnabled)

    local craftQuotesLabel = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    craftQuotesLabel:SetPoint("LEFT", craftQuotesCheckbox, "RIGHT", 5, 0)
    ApplyFont(craftQuotesLabel, qhScheme, "body")
    SetThemedText(craftQuotesLabel, "Craft Quotes", qhScheme, "text")

    local craftQuotesHint = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    craftQuotesHint:SetPoint("LEFT", craftQuotesLabel, "RIGHT", 8, 0)
    SetThemedText(craftQuotesHint, "(When crafting/acquiring decor)", qhScheme, "dim")
    ApplyFont(craftQuotesHint, qhScheme, "small")

    -- ========================================
    -- Quote Options Update Logic
    -- ========================================
    local function UpdateLumberQuotesSubOptions()
        local enabled = quotesCheckbox:GetChecked() and lumberQuotesCheckbox:GetChecked()
        local alpha = enabled and 1.0 or 0.5

        if vamooseSlider then
            if enabled then vamooseSlider:Enable() else vamooseSlider:Disable() end
            vamooseSlider:SetAlpha(alpha)
        end
        if vamooseLabel then vamooseLabel:SetAlpha(alpha) end
        if vamooseValueText then vamooseValueText:SetAlpha(alpha) end
        if vamooseHint then vamooseHint:SetAlpha(alpha) end
        if vamooseChatCheckbox then
            if enabled then vamooseChatCheckbox:Enable() else vamooseChatCheckbox:Disable() end
            vamooseChatCheckbox:SetAlpha(alpha)
        end
        if vamooseChatLabel then vamooseChatLabel:SetAlpha(alpha) end
    end

    local function UpdateQuotesSubOptions()
        local masterEnabled = quotesCheckbox:GetChecked()
        local subAlpha = masterEnabled and 1.0 or 0.5

        -- Enable/disable "only chat" checkbox
        if masterEnabled then quotesOnlyChatCheckbox:Enable() else quotesOnlyChatCheckbox:Disable() end
        quotesOnlyChatCheckbox:SetAlpha(subAlpha)
        quotesOnlyChatLabel:SetAlpha(subAlpha)
        quotesOnlyChatHint:SetAlpha(subAlpha)

        -- Enable/disable lumber quotes checkbox
        if masterEnabled then lumberQuotesCheckbox:Enable() else lumberQuotesCheckbox:Disable() end
        lumberQuotesCheckbox:SetAlpha(subAlpha)
        lumberQuotesLabel:SetAlpha(subAlpha)
        lumberQuotesHint:SetAlpha(subAlpha)

        -- Enable/disable craft quotes checkbox
        if masterEnabled then craftQuotesCheckbox:Enable() else craftQuotesCheckbox:Disable() end
        craftQuotesCheckbox:SetAlpha(subAlpha)
        craftQuotesLabel:SetAlpha(subAlpha)
        craftQuotesHint:SetAlpha(subAlpha)

        -- Update lumber quote sub-options (slider, chat output)
        UpdateLumberQuotesSubOptions()
    end

    local function SyncMasterQuotesCheckbox()
        -- If both sub-options are unchecked, uncheck master
        local lumberEnabled = lumberQuotesCheckbox:GetChecked()
        local craftEnabled = craftQuotesCheckbox:GetChecked()

        if not lumberEnabled and not craftEnabled then
            quotesCheckbox:SetChecked(false)
            Dispatch("SET_CONFIG", { key = "quotesEnabled", value = false })
        end

        UpdateQuotesSubOptions()
    end

    quotesCheckbox:SetScript("OnClick", function(self)
        Dispatch("SET_CONFIG", { key = "quotesEnabled", value = self:GetChecked() })
        UpdateQuotesSubOptions()
    end)

    lumberQuotesCheckbox:SetScript("OnClick", function(self)
        Dispatch("SET_CONFIG", { key = "lumberQuotesEnabled", value = self:GetChecked() })
        SyncMasterQuotesCheckbox()
    end)

    craftQuotesCheckbox:SetScript("OnClick", function(self)
        Dispatch("SET_CONFIG", { key = "craftQuotesEnabled", value = self:GetChecked() })
        SyncMasterQuotesCheckbox()
    end)

    -- Apply initial enabled/disabled state based on master toggle
    UpdateQuotesSubOptions()

    yOffset = yOffset - 39
    CreateSeparator(yOffset)
    yOffset = yOffset - 18

    -- ========================================
    -- Locale Override Section
    -- ========================================
    local locScheme = GetScheme()
    local localeLabel = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    localeLabel:SetPoint("TOPLEFT", 10, yOffset)
    SetThemedText(localeLabel, (L["LOCALE_OVERRIDE"] or "Language Override") .. ":", locScheme, "text")
    ApplyFont(localeLabel, locScheme, "body")

    -- Dropdown button
    local localeDropdown = CreateFrame("Frame", "HDG_LocaleDropdown", scrollChild, "BackdropTemplate")
    localeDropdown:SetSize(150, 24)
    localeDropdown:SetPoint("LEFT", localeLabel, "RIGHT", 10, 0)
    localeDropdown:SetBackdrop({
        bgFile = CONFIG.TEXTURES.WHITE_BG,
        edgeFile = CONFIG.TEXTURES.WHITE_BG,
        tile = false,
        edgeSize = 1,
        insets = { left = 0, right = 0, top = 0, bottom = 0 }
    })
    local dropdownScheme = GetScheme()
    if dropdownScheme then
        localeDropdown:SetBackdropColor(dropdownScheme.panel.r, dropdownScheme.panel.g, dropdownScheme.panel.b, 1)
        localeDropdown:SetBackdropBorderColor(dropdownScheme.border.r, dropdownScheme.border.g, dropdownScheme.border.b, dropdownScheme.border.a)
        RegisterWidget(localeDropdown, "Panel")
    else
        localeDropdown:SetBackdropColor(0.08, 0.08, 0.1, 1)
        localeDropdown:SetBackdropBorderColor(unpack(HDG_UIHelpers.BORDER_COLOR))
    end

    local localeText = localeDropdown:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    localeText:SetPoint("LEFT", 8, 0)
    localeText:SetPoint("RIGHT", -20, 0)
    localeText:SetJustifyH("LEFT")
    ApplyFont(localeText, dropdownScheme, "body")
    localeDropdown.text = localeText

    local localeArrow = localeDropdown:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    localeArrow:SetPoint("RIGHT", -6, 0)
    localeArrow:SetText("v")  -- Simple ASCII arrow (Unicode not supported in WoW fonts)
    ApplyFont(localeArrow, dropdownScheme, "body")
    if dropdownScheme then
        localeArrow:SetTextColor(dropdownScheme.text_dim.r, dropdownScheme.text_dim.g, dropdownScheme.text_dim.b)
    end

    -- Get available locales and current setting
    local availableLocales = HDG_L:GetAvailableLocales()
    local currentLocale = HDG_Settings.localeOverride or "auto"

    -- Find display name for current locale
    local function GetLocaleDisplayName(code)
        for _, loc in ipairs(availableLocales) do
            if loc.code == code then
                return loc.name
            end
        end
        return code
    end
    localeText:SetText(GetLocaleDisplayName(currentLocale))

    -- Create dropdown menu
    local localeMenu = CreateFrame("Frame", "HDG_LocaleMenu", localeDropdown, "BackdropTemplate")
    localeMenu:SetPoint("TOPLEFT", localeDropdown, "BOTTOMLEFT", 0, -2)
    localeMenu:SetWidth(150)
    localeMenu:SetBackdrop({
        bgFile = CONFIG.TEXTURES.WHITE_BG,
        edgeFile = CONFIG.TEXTURES.WHITE_BG,
        tile = false,
        edgeSize = 1,
        insets = { left = 0, right = 0, top = 0, bottom = 0 }
    })
    local menuScheme = GetScheme()
    if menuScheme then
        localeMenu:SetBackdropColor(menuScheme.panel.r, menuScheme.panel.g, menuScheme.panel.b, 0.98)
        localeMenu:SetBackdropBorderColor(menuScheme.border.r, menuScheme.border.g, menuScheme.border.b, menuScheme.border.a)
        RegisterWidget(localeMenu, "Panel")
    else
        localeMenu:SetBackdropColor(0.1, 0.1, 0.12, 0.98)
        localeMenu:SetBackdropBorderColor(unpack(HDG_UIHelpers.BORDER_COLOR))
    end
    localeMenu:SetFrameStrata("DIALOG")
    localeMenu:Hide()

    -- Create menu items
    local menuYOffset = -4
    for _, loc in ipairs(availableLocales) do
        local item = CreateFrame("Button", nil, localeMenu)
        item:SetHeight(18)
        item:SetPoint("TOPLEFT", 4, menuYOffset)
        item:SetPoint("TOPRIGHT", -4, menuYOffset)

        local itemText = item:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        itemText:SetPoint("LEFT", 4, 0)
        ApplyFont(itemText, menuScheme, "small")
        itemText:SetText(loc.name)
        item.text = itemText

        item:SetScript("OnEnter", function(self)
            local hoverScheme = GetScheme()
            if hoverScheme then
                self.text:SetTextColor(hoverScheme.warning.r, hoverScheme.warning.g, hoverScheme.warning.b)
            else
                self.text:SetTextColor(1, 0.82, 0.3)
            end
        end)
        item:SetScript("OnLeave", function(self)
            local leaveScheme = GetScheme()
            if leaveScheme then
                self.text:SetTextColor(leaveScheme.text.r, leaveScheme.text.g, leaveScheme.text.b)
            else
                self.text:SetTextColor(1, 1, 1)
            end
        end)
        item:SetScript("OnClick", function()
            HDG_Settings.localeOverride = loc.code
            localeText:SetText(loc.name)
            localeMenu:Hide()
            -- Note: Full change requires reload
        end)

        menuYOffset = menuYOffset - 18
    end
    localeMenu:SetHeight(-menuYOffset + 4)

    -- Toggle menu on click
    localeDropdown:EnableMouse(true)
    localeDropdown:SetScript("OnMouseDown", function()
        if localeMenu:IsShown() then
            localeMenu:Hide()
        else
            localeMenu:Show()
        end
    end)

    -- Close menu when clicking elsewhere
    localeMenu:SetScript("OnShow", function()
        localeMenu:SetFrameLevel(100)
    end)

    -- Reload button
    local reloadBtn = CreateFrame("Button", nil, scrollChild, "BackdropTemplate")
    reloadBtn:SetSize(60, 22)
    reloadBtn:SetPoint("LEFT", localeDropdown, "RIGHT", 10, 0)
    reloadBtn:SetBackdrop({
        bgFile = CONFIG.TEXTURES.WHITE_BG,
        edgeFile = CONFIG.TEXTURES.WHITE_BG,
        tile = false,
        edgeSize = 1,
        insets = { left = 0, right = 0, top = 0, bottom = 0 }
    })
    local reloadScheme = GetScheme()
    if reloadScheme then
        reloadBtn:SetBackdropColor(reloadScheme.button_normal.r, reloadScheme.button_normal.g, reloadScheme.button_normal.b, 1)
        reloadBtn:SetBackdropBorderColor(reloadScheme.border.r, reloadScheme.border.g, reloadScheme.border.b, reloadScheme.border.a)
        RegisterWidget(reloadBtn, "Button")
    else
        reloadBtn:SetBackdropColor(0.08, 0.23, 0.23, 1)
        reloadBtn:SetBackdropBorderColor(unpack(HDG_UIHelpers.BORDER_COLOR))
    end

    local reloadText = reloadBtn:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    reloadText:SetPoint("CENTER", 0, 0)
    ApplyFont(reloadText, reloadScheme, "small")
    SetThemedText(reloadText, "Reload", reloadScheme, "text")

    reloadBtn:SetScript("OnEnter", function(self)
        local hoverScheme = GetScheme()
        if hoverScheme then
            self:SetBackdropColor(hoverScheme.button_hover.r, hoverScheme.button_hover.g, hoverScheme.button_hover.b, 1)
        else
            self:SetBackdropColor(0.12, 0.35, 0.35, 1)
        end
        GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
        GameTooltip:AddLine("Reload UI", 1, 0.82, 0.3)
        GameTooltip:AddLine("Apply language change immediately", 0.7, 0.7, 0.7)
        GameTooltip:Show()
    end)
    reloadBtn:SetScript("OnLeave", function(self)
        local leaveScheme = GetScheme()
        if leaveScheme then
            self:SetBackdropColor(leaveScheme.button_normal.r, leaveScheme.button_normal.g, leaveScheme.button_normal.b, 1)
        else
            self:SetBackdropColor(0.08, 0.23, 0.23, 1)
        end
        GameTooltip:Hide()
    end)
    reloadBtn:SetScript("OnClick", function()
        ReloadUI()
    end)

    -- Note about locale override limitation
    local localeNote = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    localeNote:SetPoint("TOPLEFT", 10, yOffset - 28)
    SetThemedText(localeNote, L["LOCALE_NOTE"] or "Only affects addon UI text, not item/recipe names.", locScheme, "dim")
    ApplyFont(localeNote, locScheme, "small")

    yOffset = yOffset - 50
    CreateSeparator(yOffset)
    yOffset = yOffset - 18

    -- ========================================
    -- Discord Section
    -- ========================================
    local DISCORD_INVITE = "https://discord.gg/RWZaxJaHFP"

    -- Note about translation contributions (above Discord link)
    local translateNote = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    translateNote:SetPoint("TOPLEFT", 10, yOffset)
    SetThemedText(translateNote, L["TRANSLATE_NOTE"] or "Help translate! Submit corrections via Discord below.", locScheme, "dim")
    ApplyFont(translateNote, locScheme, "small")
    yOffset = yOffset - 18

    -- Discord icon
    local discordIcon = scrollChild:CreateTexture(nil, "ARTWORK")
    discordIcon:SetSize(24, 24)
    discordIcon:SetPoint("TOPLEFT", 10, yOffset)
    discordIcon:SetTexture(CONFIG.ADDON_TEXTURES.DISCORD)

    -- Discord link edit box (copyable)
    local discordEditBox = CreateFrame("EditBox", nil, scrollChild, "BackdropTemplate")
    discordEditBox:SetSize(250, 24)
    discordEditBox:SetPoint("LEFT", discordIcon, "RIGHT", 10, 0)
    discordEditBox:SetBackdrop({
        bgFile = CONFIG.TEXTURES.WHITE_BG,
        edgeFile = CONFIG.TEXTURES.WHITE_BG,
        tile = false,
        edgeSize = 1,
        insets = { left = 0, right = 0, top = 0, bottom = 0 }
    })
    local discordScheme = GetScheme()
    if discordScheme then
        discordEditBox:SetBackdropColor(discordScheme.bg.r, discordScheme.bg.g, discordScheme.bg.b, 1)
    else
        discordEditBox:SetBackdropColor(0.05, 0.05, 0.07, 1)
    end
    discordEditBox:SetBackdropBorderColor(0.35, 0.40, 0.98, 0.6)  -- Discord blurple border (intentional)
    ApplyFont(discordEditBox, discordScheme, "body")
    discordEditBox:SetText(DISCORD_INVITE)
    discordEditBox:SetAutoFocus(false)
    discordEditBox:SetTextInsets(8, 8, 0, 0)
    discordEditBox:SetScript("OnEditFocusGained", function(self)
        self:HighlightText()
    end)
    discordEditBox:SetScript("OnEscapePressed", function(self)
        self:ClearFocus()
    end)
    discordEditBox:SetScript("OnEnterPressed", function(self)
        self:ClearFocus()
    end)

    -- Hint to the right of the link
    local discordHint = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    discordHint:SetPoint("LEFT", discordEditBox, "RIGHT", 8, 0)
    ApplyFont(discordHint, discordScheme, "small")
    SetThemedText(discordHint, "Ctrl+C to copy", discordScheme, "dim")

    yOffset = yOffset - 36
    CreateSeparator(yOffset)
    yOffset = yOffset - 18


    -- ========================================
    -- How to Use Guide
    -- ========================================
    local guideScheme = GetScheme()
    local guideHeader = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    guideHeader:SetPoint("TOPLEFT", 10, yOffset)
    ApplyFont(guideHeader, guideScheme, "header")
    guideHeader:SetText("How to Use")
    if guideScheme then
        guideHeader:SetTextColor(guideScheme.accent.r, guideScheme.accent.g, guideScheme.accent.b, guideScheme.accent.a)
    end
    AddSectionStyle(guideHeader, scrollChild)
    RegisterWidget(guideHeader, "SectionTitle")
    yOffset = yOffset - 30

    -- Guide sections with tab explanations
    local guideSections = {
        {
            title = "Recipes Tab",
            lines = {
                "Browse all craftable housing decor organized by profession",
                "Click a recipe to see required materials in the right panel",
                "Click '+' to add items to your crafting queue",
                "|cFFfcd34dGold text|r = known by you, |cFF888888Gray|r = not known by anyone",
                "Toggle 'Raw Mats' to see base gathering materials vs direct recipe mats",
                "Use search box to filter by recipe or material name",
                "Shift-click materials to search in Auctionator",
                "Right-click materials to add to Auctionator shopping list",
                "'Craft These First' section shows intermediate items to craft",
            }
        },
        {
            title = "Crafting Queue",
            lines = {
                "Items automatically grouped by which character can craft them",
                "Your current character's craftable items appear first",
                "'Not Known' section shows recipes none of your alts have",
                "Queue persists across characters - no need to recreate!",
                "Left-click to decrement quantity, right-click to remove",
            }
        },
        {
            title = "Preview Tab",
            lines = {
                "View 3D models of all housing decor items",
                "Drag to rotate models horizontally and vertically",
                "Use Expansion filter + tag to see all decor for that expansion",
                "Example: Filter 'Expansion' then 'Cata' to see all Cata decor",
                "Shift-click any item to add it to your crafting queue",
                "|cFFfcd34dBright Star|r = Known recipe, |cFF888888Dim Check|r = Learnable",
                "Toggle 'Collection' to show ALL decor, not just craftable",
                "Collection view shows live data of what you've collected",
            }
        },
        {
            title = "Alts Tab",
            lines = {
                "View profession skills across all your characters",
                "Colors show if skill meets decor recipe thresholds:",
                "  |cFF2dd4bfTeal|r = Can learn recipes, |cFFfcd34dYellow|r = Below threshold",
                "Hover over skills to see which characters have them",
                "Data updates automatically when you open profession windows",
                "Use the Refresh button to reload all character data",
            }
        },
        {
            title = "Goblin Tab",
            lines = {
                "Analyze profit margins on all housing decor items",
                "Toggle between TSM and Auctionator price sources",
                "Click column headers to sort by profit, margin, etc.",
                "Click a row to expand and see material cost breakdown",
                "Ctrl-click to add profitable items to your queue",
                "Use 'Hide Unprofitable' to focus on money-makers",
                "Red prices = unprofitable, Green prices = profitable",
            }
        },
        {
            title = "Trainers Tab",
            lines = {
                "Find profession trainers for any expansion",
                "Click the waypoint icon to set a TomTom destination",
                "Trainers listed by profession with coordinates",
                "Requires TomTom addon for waypoint functionality",
            }
        },
        {
            title = "Data Tab",
            lines = {
                "View your crafting and acquisition history",
                "Track lumber farming sessions with rate statistics",
                "Export data to CSV for spreadsheet analysis",
                "Click column headers to sort history entries",
                "Session history shows items per hour farming rates",
            }
        },
        {
            title = "Lumber Tracker",
            lines = {
                "Pin the lumber widget to track inventory while playing",
                "Farming sessions start automatically when you gather lumber",
                "Ctrl-click any material in recipes to add to tracking",
                "Focus mode hides other rows while actively farming an item",
                "'Needed' column shows quantity required for your queue",
                "'All Recipes' column shows quantity for all uncollected decor",
            }
        },
        {
            title = "Lumber Radar HUD",
            lines = {
                "A circular radar that tracks your lumber gathering locations",
                "and helps you plan efficient farming routes based on respawn timers.",
                "",
                "|cFFfcd34dHow It Works:|r",
                "  You are always at the center (green dot with direction line)",
                "  Blips show locations where you've gathered lumber",
                "  N/E/S/W labels rotate as you turn - always shows true north",
                "  Blips move around you as you move and rotate",
                "",
                "|cFFfcd34dBlip Colors (Respawn Tracking):|r",
                "  |cFFff3333Red|r = Just cut (0-5 min) - don't return yet",
                "  |cFFffff00Yellow|r = Respawning (5-9 min) - almost ready",
                "  |cFF00ff00Green (pulsing)|r = Ready! (9-10+ min) - safe to return",
                "  |cFF888888Grey (faded)|r = Old (1+ hour) - stale data",
                "",
                "|cFFfcd34dDistance Rings:|r",
                "  Inner ring = 150 yards",
                "  Outer ring = 300 yards",
                "  Maximum range = 450 yards",
                "",
                "|cFFfcd34dConfiguration (in Config tab):|r",
                "  Radar Scale: 0.5x to 2.5x - resize the display",
                "  Radar Framerate: 10-30 fps - balance smoothness vs CPU",
                "",
                "|cFFfcd34dUsage Tips:|r",
                "  Drag the radar to reposition it on screen",
                "  Fly toward green blips - they approach center as you get closer",
                "  Avoid red areas - those nodes are depleted",
                "  Plan circular routes: by the time you return, nodes respawn!",
                "  Routes can be smaller than you think - respawns are fast!",
                "  You can farm efficiently in a circle not much bigger than the radar",
                "",
                "|cFFfcd34dCommands:|r",
                "  |cFF2dd4bf/lumberhud|r or |cFF2dd4bf/hdg hud|r - Toggle radar",
            }
        },
        {
            title = "Example Workflow",
            lines = {
                "Want to craft all Cata decor? Here's how:",
                "1. Go to Preview tab, select 'Expansion' filter, then 'Cata' tag",
                "2. Shift-click each item to add to your crafting queue",
                "3. Switch back to Recipes tab - all items sorted by crafter!",
                "4. Queue persists across characters, no need to recreate",
                "",
                "Want to see ALL decor (not just craftable)?",
                "Toggle 'Collection' in Preview - shows live collection data",
            }
        },
        {
            title = "Tips & Tricks",
            lines = {
                "|cFFfcd34dProfession Window Buttons:|r Open any profession window and",
                "look for 'Filter Decor' and 'Decor Guide' buttons at the top!",
                "  - |cFF2dd4bfFilter Decor|r: Filters to only housing decor items",
                "  - |cFF2dd4bfDecor Guide|r: Opens this addon at that profession",
                "",
                "|cFFfcd34dLumber Auto-Show:|r The lumber tracker automatically appears",
                "when you start farming wood (can be disabled in settings above)",
            }
        },
    }

    local guideScheme = GetScheme()
    for _, section in ipairs(guideSections) do
        -- Section header
        local sectionTitle = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        sectionTitle:SetPoint("TOPLEFT", 15, yOffset)
        ApplyFont(sectionTitle, guideScheme, "header")
        SetThemedText(sectionTitle, section.title, guideScheme, "warning")
        yOffset = yOffset - 22

        -- Section lines
        for _, line in ipairs(section.lines) do
            local lineText = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
            lineText:SetPoint("TOPLEFT", 30, yOffset)
            lineText:SetWidth(CONFIG.MAIN_FRAME_WIDTH - 100)
            lineText:SetJustifyH("LEFT")
            ApplyFont(lineText, guideScheme, "body")
            SetThemedText(lineText, "- " .. line, guideScheme, "text")
            yOffset = yOffset - 18
        end

        yOffset = yOffset - 10
    end

    yOffset = yOffset - 10
    CreateSeparator(yOffset)
    yOffset = yOffset - 15

    -- ========================================
    -- Advanced Section (v2.2.2)
    -- ========================================
    local advScheme = GetScheme()
    local advancedHeader = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    advancedHeader:SetPoint("TOPLEFT", 10, yOffset)
    ApplyFont(advancedHeader, advScheme, "header")
    SetThemedText(advancedHeader, "Advanced", advScheme, "accent")
    AddSectionStyle(advancedHeader, scrollChild)

    yOffset = yOffset - 30

    -- Debug Mode Checkbox
    local debugCheckbox = CreateFrame("CheckButton", "HDG_DebugModeCheckbox", scrollChild, "UICheckButtonTemplate")
    debugCheckbox:SetPoint("TOPLEFT", 15, yOffset)
    debugCheckbox:SetSize(26, 26)
    debugCheckbox:SetChecked(GetConfig("debugModeEnabled") or false)

    local debugLabel = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    debugLabel:SetPoint("LEFT", debugCheckbox, "RIGHT", 5, 0)
    ApplyFont(debugLabel, advScheme, "body")
    SetThemedText(debugLabel, "Enable Debug Mode", advScheme, "text")

    local debugHint = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    debugHint:SetPoint("TOPLEFT", debugCheckbox, "BOTTOMLEFT", 26, 2)
    SetThemedText(debugHint, "(Enables /hdgdebug slash commands for troubleshooting)", advScheme, "dim")
    ApplyFont(debugHint, advScheme, "small")

    debugCheckbox:SetScript("OnClick", function(self)
        local enabled = self:GetChecked()
        Dispatch("SET_CONFIG", { key = "debugModeEnabled", value = enabled })
        -- Sync session state with HDG_Debug module
        if privateDB.HDG_Debug and privateDB.HDG_Debug.SetUnlocked then
            privateDB.HDG_Debug.SetUnlocked(enabled)
        end
        if enabled then
            print("|cFF00FFFF[HDG]|r Debug mode |cFF00FF00enabled|r - /hdgdebug commands now available")
        else
            print("|cFF00FFFF[HDG]|r Debug mode |cFFFF0000disabled|r")
        end
    end)

    yOffset = yOffset - 50
    CreateSeparator(yOffset)
    yOffset = yOffset - 15

    -- ========================================
    -- Contributors Section (v2.0.2)
    -- ========================================
    local contribScheme = GetScheme()
    local contributorsHeader = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    contributorsHeader:SetPoint("TOPLEFT", 10, yOffset)
    ApplyFont(contributorsHeader, contribScheme, "header")
    contributorsHeader:SetText("|cFF5865F2Special Thanks|r")

    yOffset = yOffset - 25

    -- Contributors message
    local contributorsMsg = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    contributorsMsg:SetPoint("TOPLEFT", 10, yOffset)
    contributorsMsg:SetWidth(CONFIG.MAIN_FRAME_WIDTH - 80)
    contributorsMsg:SetJustifyH("LEFT")
    ApplyFont(contributorsMsg, contribScheme, "body")
    SetThemedText(contributorsMsg, "This addon wouldn't be possible without the amazing community. " ..
        "Special thanks to the following contributors for their feedback, bug reports, and feature suggestions:", contribScheme, "text")

    yOffset = yOffset - 50

    -- Contributors list
    local contributors = {
        {name = "Abased", note = "Feature requests & testing"},
        {name = "Castianna", note = "Bug reports"},
        {name = "Daxxlia", note = "Testing & feedback"},
        {name = "Discomanco", note = "Warband grouping debugging & testing"},
        {name = "Edengonedark", note = "Feature suggestions"},
        {name = "Filurina", note = "Quality variant bug report"},
        {name = "Fracture93", note = "Goblin Mode feature suggestion"},
        {name = "GnuclearGnome", note = "Bug reports & UI feedback"},
        {name = "Gravebait", note = "Feature suggestions"},
        {name = "Hydra", note = "Bug reports & testing"},
        {name = "JodouKast", note = "Community support & feature ideas"},
        {name = "Jonnyne", note = "Early adopter & support"},
        {name = "Katsmiaou", note = "Feature suggestions"},
        {name = "KeeperHarvest", note = "Detailed vendor audit & bug reports"},
        {name = "Nunuman", note = "Korean localization testing & bug reports"},
        {name = "Rylls", note = "Bug reports & UX feedback"},
        {name = "Tzunamis", note = "Trainer NPC ID bug report"},
    }

    for _, contributor in ipairs(contributors) do
        local line = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        line:SetPoint("TOPLEFT", 20, yOffset)
        line:SetText(C_Accent() .. "-|r " .. C_Warning() .. contributor.name .. "|r " .. C_Dim() .. "- " .. contributor.note .. "|r")
        ApplyFont(line, contribScheme, "body")
        yOffset = yOffset - 18
    end

    yOffset = yOffset - 15

    -- Join us message
    local joinMsg = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    joinMsg:SetPoint("TOPLEFT", 10, yOffset)
    joinMsg:SetWidth(CONFIG.MAIN_FRAME_WIDTH - 80)
    joinMsg:SetJustifyH("LEFT")
    ApplyFont(joinMsg, contribScheme, "body")
    SetThemedText(joinMsg, "Want to contribute? Join our Discord or report issues on CurseForge!", contribScheme, "dim")

    yOffset = yOffset - 30

    -- Update scroll child height based on content
    local contentHeight = math.abs(yOffset) + 50
    scrollChild:SetHeight(contentHeight)

    return configFrame
end

-- ============================================================================
-- UPDATE FUNCTIONS
-- ============================================================================

-- Update lumber pin checkbox to match current setting
function HDG_ConfigTab.UpdateLumberPinCheckbox()
    if HDG_ConfigTab.lumberPinCheckbox then
        HDG_ConfigTab.lumberPinCheckbox:SetChecked(GetUIState("lumberPinned") or false)
    end
end

-- ============================================================================
-- EVENTBUS SUBSCRIPTIONS
-- Decouple UI refresh from direct function calls
-- ============================================================================

local function RegisterEventBusHandlers()
    local HDG = _G.HousingDecorGuide
    if not HDG or not HDG.EventBus then return end

    HDG.EventBus:Register("HDG_STATE_CHANGED", function(payload)
        if not payload or not payload.action then return end

        local action = payload.action

        -- UI state changes (e.g., lumber pin toggled externally)
        if action == "SET_UI" then
            HDG_ConfigTab.UpdateLumberPinCheckbox()
        end
    end)
end

-- Register handlers after a short delay to ensure EventBus is ready
C_Timer.After(0.1, RegisterEventBusHandlers)

-- ============================================================================
-- EXPORT TO GLOBAL
-- ============================================================================

_G.HDG_ConfigTab = HDG_ConfigTab

return HDG_ConfigTab
