local addonName, LRH = ...

LRH = LRH or {}
LRH.UI = LRH.UI or {}
LRH.UI.Quick = LRH.UI.Quick or {}

local L = LRH.L or {}
local U = LRH.Utils or {}

-------------------------------------------------
-- DEBUG
-------------------------------------------------
local DEBUG_LAYOUT = false
local DEBUG_SHOW_ROW_BOUNDS = false

-------------------------------------------------
-- Layout constants
-------------------------------------------------
local PAD_L = 20
local PAD_R = 20

local HEADER_X = PAD_L + 12
local LABEL_X  = PAD_L + 12

local CONTROL_VIS_X = 215
local DD_FRAME_X = CONTROL_VIS_X - 16

local QUICK_W = 440

local GAP_AFTER_TITLE   = 12
local GAP_AFTER_TEXT    = 14
local GAP_AFTER_HEADER  = 14
local GAP_BETWEEN_ROWS  = 10
local GAP_BETWEEN_GROUP = 16

local H_DROPDOWN   = 34
local H_SWATCH     = 30
local H_SLIDER_MIN = 74

local SLIDER_TOP_PAD    = 35
local SLIDER_BOTTOM_PAD = 15

local DD_Y     = -8
local SWATCH_Y = -2

local SLIDER_W = 320

local SWATCH_H       = 22
local SWATCH_GAP     = 12
local SWATCH_GROUP_W = 150

-------------------------------------------------
-- PixelUtil helpers
-------------------------------------------------
local function SetPointPX(f, point, rel, relPoint, x, y)
    if PixelUtil and PixelUtil.SetPoint then
        PixelUtil.SetPoint(f, point, rel, relPoint, x, y)
    else
        f:SetPoint(point, rel, relPoint, x, y)
    end
end

local function SetHeightPX(f, h)
    if PixelUtil and PixelUtil.SetHeight then
        PixelUtil.SetHeight(f, h)
    else
        f:SetHeight(h)
    end
end

local function CalcDropDownWidth(parent, strings)
    local m = parent:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
    m:Hide()
    local maxW = 0
    for _, s in ipairs(strings or {}) do
        m:SetText(s or "")
        local w = m:GetStringWidth() or 0
        if w > maxW then maxW = w end
    end
    local wanted = math.floor(maxW + 48 + 0.5)
    if wanted < 120 then wanted = 120 end
    if wanted > 170 then wanted = 170 end
    return wanted
end


local function NormalizeLastDDButton(level, xOffset)
    level = level or 1
    local list = _G["DropDownList" .. level]
    if not list or not list.numButtons then return end
    local idx = list.numButtons
    local btn = _G["DropDownList" .. level .. "Button" .. idx]
    if not btn or not btn.NormalText then return end
    xOffset = xOffset or 20
    -- Force consistent indentation (fixes first-entry-left-shift)
    btn.NormalText:ClearAllPoints()
    btn.NormalText:SetPoint("LEFT", btn, "LEFT", xOffset, 0)
    btn.NormalText:SetJustifyH("LEFT")
end

local function AddDDOption(level, text, checked, onClick)

    local info = UIDropDownMenu_CreateInfo()

    info.text = text
    info.func = onClick

    -- verhindert den Checkmark-Versatz
    info.checked = nil

    -- sorgt dafür dass alle Einträge exakt gleich eingerückt sind
    info.notCheckable = true

    UIDropDownMenu_AddButton(info, level)
    NormalizeLastDDButton(level, info.notCheckable and 12 or 20)
end
-------------------------------------------------
-- Widgets
-------------------------------------------------
local function CreateBlueButton(parent, text, width, height)
    local b = CreateFrame("Button", nil, parent, "BackdropTemplate")
    b:SetSize(width or 140, height or 22)
    b:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8X8",
        edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
        edgeSize = 14,
        insets = { left = 3, right = 3, top = 3, bottom = 3 },
    })
    b:SetBackdropColor(0.10, 0.40, 0.80, 0.95)
    b:SetBackdropBorderColor(0.00, 0.44, 0.87, 1)

    local fs = b:CreateFontString(nil, "ARTWORK", "GameFontNormal")
    fs:SetPoint("CENTER")
    fs:SetText(text or "")
    b._text = fs

    b:SetScript("OnEnter", function(self) self:SetBackdropColor(0.14, 0.50, 0.95, 1) end)
    b:SetScript("OnLeave", function(self) self:SetBackdropColor(0.10, 0.40, 0.80, 0.95) end)
    return b
end

local function CreateColorSwatch(parent, labelText, w, h)
    local f = CreateFrame("Button", nil, parent, "BackdropTemplate")
    f:SetSize(w or 84, h or SWATCH_H)

    f:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8X8",
        edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
        edgeSize = 14,
        insets = { left = 3, right = 3, top = 3, bottom = 3 },
    })
    f:SetBackdropColor(0, 0, 0, 0.20)
    f:SetBackdropBorderColor(0.00, 0.44, 0.87, 1)

    f.fill = f:CreateTexture(nil, "ARTWORK")
    f.fill:SetPoint("TOPLEFT", f, "TOPLEFT", 3, -3)
    f.fill:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT", -3, 3)
    f.fill:SetColorTexture(0.2, 0.2, 0.2, 1)

    local t = f:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
    t:SetPoint("CENTER")
    t:SetText(labelText or "")
    f.label = t
    return f
end

local function OpenColorPicker(targetKey, swatchFrame)
    LeftRightConfig[targetKey] = U.EnsureColorTable(LeftRightConfig[targetKey], 1, 1, 1)
    local c = LeftRightConfig[targetKey]

    local function ApplyRGB(r, g, b)
        c.r, c.g, c.b = r, g, b
        if swatchFrame and swatchFrame.fill then
            swatchFrame.fill:SetColorTexture(r, g, b, 1)
        end
        if LRH.ApplyFillTint then LRH:ApplyFillTint() end
    end

    if ColorPickerFrame and ColorPickerFrame.SetupColorPickerAndShow then
        local info = {
            r = c.r, g = c.g, b = c.b,
            hasOpacity = false,
            swatchFunc = function()
                local r2, g2, b2 = ColorPickerFrame:GetColorRGB()
                ApplyRGB(r2, g2, b2)
            end,
            cancelFunc = function(prev)
                if type(prev) == "table" then
                    local r2 = prev.r or prev[1] or c.r
                    local g2 = prev.g or prev[2] or c.g
                    local b2 = prev.b or prev[3] or c.b
                    ApplyRGB(r2, g2, b2)
                end
            end,
        }
        ColorPickerFrame:SetupColorPickerAndShow(info)
    end
end

-------------------------------------------------
-- Debug overlay helpers
-------------------------------------------------
local function DebugRowBox(rowFrame, r, g, b, a)
    local t = rowFrame:CreateTexture(nil, "BACKGROUND")
    t:SetAllPoints(rowFrame)
    t:SetColorTexture(r or 1, g or 0, b or 0, a or 0.06)
end

-------------------------------------------------
-- UI Build
-------------------------------------------------
function LRH.UI.CreateQuickOptions()
    if LRH.UI.Quick.frame then
        LRH.UI.Quick.frame:Hide()
        LRH.UI.Quick.frame:SetParent(nil)
        LRH.UI.Quick.frame = nil
        LRH.UI.Quick.Refresh = nil
    end

    local quick = CreateFrame("Frame", "LeftRightQuickOptions", UIParent, "BackdropTemplate")
    quick:SetWidth(QUICK_W)
    quick:SetFrameStrata("DIALOG")
    quick:SetClampedToScreen(true)
    quick:Hide()

    quick:SetBackdrop({
        bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
        edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
        edgeSize = 16,
        insets = { left = 4, right = 4, top = 4, bottom = 4 },
    })
    quick:SetBackdropColor(0, 0, 0, 0.85)
    quick:SetBackdropBorderColor(0, 0.44, 0.87, 1)
    quick:SetPoint("LEFT", UIParent, "LEFT", 40, 0)

    quick:SetMovable(true)
    quick:EnableMouse(true)
    quick:RegisterForDrag("LeftButton")
    quick:SetScript("OnDragStart", function(self) self:StartMoving() end)
    quick:SetScript("OnDragStop", function(self) self:StopMovingOrSizing() end)

    -------------------------------------------------
    -- Determine unified dropdown width
    -------------------------------------------------
    local designStrings = {}
    for _, key in ipairs(LRH.GetAvailableDesignKeys and LRH:GetAvailableDesignKeys() or { "CLASSIC" }) do
        local d = LRH.DESIGNS and LRH.DESIGNS[key]
        if d and d.name then table.insert(designStrings, d.name) end
    end
    if #designStrings == 0 then designStrings = { "Classic" } end

    local borderStrings = { (L.BORDER_GOLD or "Gold"), (L.BORDER_SILVER or "Silver"), (L.BORDER_BROWN or "Brown") }

    local fontStrings = {}
    do
        -- Font strings used only to compute dropdown width
        -- Show only: Standard + any custom presets registered into LRH.FONT_PRESETS.
        local keys = {}
        if LRH.FONT_PRESETS then
            for k, p in pairs(LRH.FONT_PRESETS) do
                if type(p) == "table" and p.name and p.path then
                    table.insert(keys, k)
                end
            end
        end

        table.sort(keys, function(a, b)
            local pa = (LRH.FONT_PRESETS[a] and LRH.FONT_PRESETS[a].name) or a
            local pb = (LRH.FONT_PRESETS[b] and LRH.FONT_PRESETS[b].name) or b
            if a == "STANDARD" then return true end
            if b == "STANDARD" then return false end
            return tostring(pa) < tostring(pb)
        end)

        for _, k in ipairs(keys) do
            local p = LRH.FONT_PRESETS[k]
            if p and p.name then table.insert(fontStrings, p.name) end
        end
        if #fontStrings == 0 then fontStrings = { "Standard" } end
    end

    local outlineStrings = {}
    for _, k in ipairs({ "NONE", "OUTLINE", "THICKOUTLINE" }) do
        local o = LRH.OUTLINE_PRESETS and LRH.OUTLINE_PRESETS[k]
        if o and o.name then table.insert(outlineStrings, o.name) end
    end
    if #outlineStrings == 0 then outlineStrings = { "Outline" } end

    local W_DESIGN  = CalcDropDownWidth(quick, designStrings)
    local W_BORDER  = CalcDropDownWidth(quick, borderStrings)
    local W_FONT    = CalcDropDownWidth(quick, fontStrings)
    local W_OUTLINE = CalcDropDownWidth(quick, outlineStrings)

    local CONTROL_W = math.max(W_DESIGN, W_BORDER, W_FONT, W_OUTLINE)

    -------------------------------------------------
    -- Row layout system
    -------------------------------------------------
    local ui = {}
    local y = 12

    local function NewRow(height)
        local row = CreateFrame("Frame", nil, quick)
        row:SetPoint("TOPLEFT", quick, "TOPLEFT", 0, -y)
        row:SetPoint("TOPRIGHT", quick, "TOPRIGHT", 0, -y)
        row:SetHeight(height)
        y = y + height + GAP_BETWEEN_ROWS
        return row
    end

    local function AddTitle(text)
        local fs = quick:CreateFontString(nil, "ARTWORK", "GameFontNormal")
        fs:SetPoint("TOP", quick, "TOP", 0, -y)
        fs:SetWidth(quick:GetWidth() - PAD_L - PAD_R)
        fs:SetJustifyH("CENTER")
        fs:SetText(text or "")
        y = y + fs:GetStringHeight() + GAP_AFTER_TITLE
    end

    local function AddText(text)
        local fs = quick:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
        fs:SetPoint("TOPLEFT", quick, "TOPLEFT", PAD_L, -y)
        fs:SetWidth(quick:GetWidth() - PAD_L - PAD_R)
        fs:SetJustifyH("LEFT")
        fs:SetText(text or "")
        y = y + fs:GetStringHeight() + GAP_AFTER_TEXT
    end

    local function AddHeader(text)
        y = y + GAP_BETWEEN_GROUP

        local fs = quick:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
        fs:SetPoint("TOPLEFT", quick, "TOPLEFT", HEADER_X, -y)
        fs:SetText(text or "")
        y = y + fs:GetStringHeight()

        -- pixel-perfect separator (1px + soft 1px)
        local baseLevel = (quick:GetFrameLevel() or 0) + 60

        local line1 = CreateFrame("Frame", nil, quick, "BackdropTemplate")
        line1:SetFrameLevel(baseLevel)
        line1:SetBackdrop({ bgFile = "Interface\\Buttons\\WHITE8X8" })
        line1:SetBackdropColor(0, 0.44, 0.87, 0.60)
        SetHeightPX(line1, 1)
        SetPointPX(line1, "TOPLEFT",  quick, "TOPLEFT",  PAD_L,  -y - 6)
        SetPointPX(line1, "TOPRIGHT", quick, "TOPRIGHT", -PAD_R, -y - 6)

        local line2 = CreateFrame("Frame", nil, quick, "BackdropTemplate")
        line2:SetFrameLevel(baseLevel)
        line2:SetBackdrop({ bgFile = "Interface\\Buttons\\WHITE8X8" })
        line2:SetBackdropColor(0, 0.44, 0.87, 0.20)
        SetHeightPX(line2, 1)
        SetPointPX(line2, "TOPLEFT",  line1, "BOTTOMLEFT",  0, -1)
        SetPointPX(line2, "TOPRIGHT", line1, "BOTTOMRIGHT", 0, -1)

        y = y + GAP_AFTER_HEADER
    end

    local function AddRowLabel(row, text)
        local fs = row:CreateFontString(nil, "ARTWORK", "GameFontNormal")
        fs:SetText(text or "")
        fs:SetPoint("LEFT", row, "LEFT", LABEL_X, 0)
        fs:SetJustifyH("LEFT")
        return fs
    end

    
local function AddDropDown(name, labelText)
    local row = NewRow(H_DROPDOWN)
    if DEBUG_LAYOUT and DEBUG_SHOW_ROW_BOUNDS then DebugRowBox(row, 0, 1, 0, 0.05) end

    local label = AddRowLabel(row, labelText)

    local dd = (LRH.UI and LRH.UI.Dropdown and LRH.UI.Dropdown.Create) and LRH.UI.Dropdown:Create(row, name, CONTROL_W, { maxVisible = 18 })
    if not dd then
        -- Fallback: should never happen, but prevents hard crash if file missing
        dd = CreateFrame("Button", name, row, "UIPanelButtonTemplate")
        dd:SetSize(CONTROL_W, 22)
        dd:SetText("Dropdown")
    end
    dd:SetPoint("TOPLEFT", row, "TOPLEFT", DD_FRAME_X, DD_Y)

    dd._row = row
    dd._label = label

    return dd
end

    local function AddSwatches(labelText)
    local row = NewRow(H_SWATCH)
    if DEBUG_LAYOUT and DEBUG_SHOW_ROW_BOUNDS then DebugRowBox(row, 1, 0, 0, 0.05) end

    AddRowLabel(row, labelText)

    -- Gemeinsamer Container: exakt so breit wie das Dropdown
    local group = CreateFrame("Frame", nil, row)
    group:SetPoint("TOPLEFT", row, "TOPLEFT", DD_FRAME_X, SWATCH_Y)
    group:SetSize(CONTROL_W, SWATCH_H)

    -- Pixelgenaue Teilung in zwei gleich breite Swatches
    local innerGap = SWATCH_GAP or 12
    local swW = math.floor((CONTROL_W - innerGap) / 2 + 0.5)

    local swL = CreateColorSwatch(group, L.LEFT_SHORT or "L", swW, SWATCH_H)
    local swR = CreateColorSwatch(group, L.RIGHT_SHORT or "R", swW, SWATCH_H)

    swL:SetPoint("LEFT", group, "LEFT", 0, 0)
    swR:SetPoint("RIGHT", group, "RIGHT", 0, 0)

    -- Referenzen, falls du später noch exakt nachjustieren willst
    swL._group = group
    swR._group = group

    return swL, swR, group
end

    local function AddSlider(minV, maxV, step, labelText, lowText, highText)
        local row = NewRow(H_SLIDER_MIN)
        if DEBUG_LAYOUT and DEBUG_SHOW_ROW_BOUNDS then DebugRowBox(row, 0, 0.7, 1, 0.04) end

        local s = CreateFrame("Slider", nil, row, "OptionsSliderTemplate")
        s:SetWidth(SLIDER_W)
        s:SetMinMaxValues(minV, maxV)
        s:SetValueStep(step)
        s:SetObeyStepOnDrag(true)
        s.Low:SetText(lowText or "")
        s.High:SetText(highText or "")

        s:SetPoint("TOP", row, "TOP", 0, -SLIDER_TOP_PAD)

        local fs = row:CreateFontString(nil, "ARTWORK", "GameFontNormal")
        fs:SetText(labelText or "")
        fs:SetPoint("LEFT", s, "LEFT", 0, 14)

        -- ensure bottom padding (dynamic grow)
        row:Show()
        s:Show()

        local top = row:GetTop()
        if top then
            local bottoms = {}

            local function AddBottom(obj)
                if obj and obj.GetBottom then
                    local b = obj:GetBottom()
                    if b then table.insert(bottoms, b) end
                end
            end

            AddBottom(s)
            AddBottom(s.Low)
            AddBottom(s.High)
            AddBottom(s.Text)

            local minBottom = nil
            for _, v in ipairs(bottoms) do
                if (not minBottom) or v < minBottom then minBottom = v end
            end

            if minBottom then
                local needed = (top - minBottom) + SLIDER_BOTTOM_PAD
                local cur = row:GetHeight()
                if needed > cur then
                    local grow = needed - cur
                    row:SetHeight(needed)
                    y = y + grow
                end
            end
        end

        return s
    end

    -------------------------------------------------
    -- Build UI
    -------------------------------------------------
    AddTitle((L.ADDON_NAME or "Left Right Helper") .. " - " .. (L.QUICK_OPTIONS or "Quick Options"))
    AddText(L.QUICK_CONTROLS_TEXT or "")

    AddHeader(L.H_ARROWS or "Arrows")
    ui.ddDesign = AddDropDown("LRH_Q_DesignDD", L.ARROW_DESIGN or "Arrow design")
    ui.sGap = AddSlider(LRH.MIN_GAP or 0, LRH.MAX_GAP or 700, LRH.GAP_STEP or 1,
        L.ARROW_SPACING or "Arrow spacing",
        tostring(LRH.MIN_GAP or 0),
        tostring(LRH.MAX_GAP or 700)
    )

    AddHeader(L.H_COLORS or "Colors")
    ui.swL, ui.swR, ui.swatchGroup = AddSwatches(L.FILL_COLORS or "Fill colors")
    ui.ddBorder = AddDropDown("LRH_Q_BorderDD", L.BORDER_COLOR or "Border color")

    ui.borderUnavailableText = quick:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    ui.borderUnavailableText:SetText(L.SIMPLE_NO_BORDER_COLOR or "Not available for Simple")
    ui.borderUnavailableText:SetTextColor(1, 0.2, 0.2)
    ui.borderUnavailableText:SetWidth(CONTROL_W)
    ui.borderUnavailableText:SetJustifyH("CENTER")
    ui.borderUnavailableText:SetJustifyV("MIDDLE")
    ui.borderUnavailableText:SetPoint("CENTER", ui.ddBorder, "CENTER", 0, 0)
    ui.borderUnavailableText:Hide()

    AddHeader(L.H_ALPHA or "Transparency")
    ui.sAlpha = AddSlider(0, 1, 0.01, L.TRANSPARENCY or "Transparency", "0%", "100%")

    AddHeader(L.H_TEXT or "Text")
    ui.ddFont = AddDropDown("LRH_Q_FontDD", L.FONT or "Font")
    ui.ddOutline = AddDropDown("LRH_Q_OutlineDD", L.FONT_OUTLINE or "Font outline")

    local rowClose = NewRow(34)
    ui.closeBtn = CreateBlueButton(rowClose, L.CLOSE or "Close", 140, 22)
    ui.closeBtn:SetPoint("CENTER", rowClose, "CENTER", 0, 0)

    quick:SetHeight(y + 12)

    -------------------------------------------------
    -- Behaviour / Refresh
    -------------------------------------------------
    local function Refresh()
        local dk = (LRH.GetDesignKey and LRH:GetDesignKey()) or LeftRightConfig.arrowDesign
        if not dk then dk = "CLASSIC" end
        ui.ddDesign:SetSelectedValue(dk, true)
        ui.ddDesign:SetText((LRH.DESIGNS and LRH.DESIGNS[dk] and LRH.DESIGNS[dk].name) or "Classic")

        local g = U.Clamp(LeftRightConfig.arrowGapPx or 150, LRH.MIN_GAP or 0, LRH.MAX_GAP or 700)
        ui.sGap:SetValue(g)
        if ui.sGap.Text then
            ui.sGap.Text:SetText((L.ARROW_SPACING or "Arrow spacing") .. " (" .. math.floor(g) .. " px)")
        end

        LeftRightConfig.leftColor  = U.EnsureColorTable(LeftRightConfig.leftColor,  0.20, 0.60, 1.00)
        LeftRightConfig.rightColor = U.EnsureColorTable(LeftRightConfig.rightColor, 1.00, 0.20, 0.20)
        ui.swL.fill:SetColorTexture(LeftRightConfig.leftColor.r, LeftRightConfig.leftColor.g, LeftRightConfig.leftColor.b, 1)
        ui.swR.fill:SetColorTexture(LeftRightConfig.rightColor.r, LeftRightConfig.rightColor.g, LeftRightConfig.rightColor.b, 1)

        local isSimple = (dk == "SIMPLE")
        if ui.ddBorder and ui.borderUnavailableText then
            if isSimple then
                ui.ddBorder:Hide()
                ui.borderUnavailableText:Show()
            else
                ui.ddBorder:Show()
                ui.borderUnavailableText:Hide()

                local bs = LeftRightConfig.borderStyle
                if not (LRH.BORDER_COLORS and LRH.BORDER_COLORS[bs]) then bs = "SILVER" end
                local borderName =
                    (bs == "GOLD" and (L.BORDER_GOLD or "Gold")) or
                    (bs == "BROWN" and (L.BORDER_BROWN or "Brown")) or
                    (L.BORDER_SILVER or "Silver")
                if ui.ddBorder.SetSelectedValue then ui.ddBorder:SetSelectedValue(bs, true) end
                ui.ddBorder:SetText(borderName)
            end
        end

        local a = U.Clamp(LeftRightConfig.alpha or 1, 0, 1)
        ui.sAlpha:SetValue(a)
        if ui.sAlpha.Text then
            ui.sAlpha.Text:SetText((L.TRANSPARENCY or "Transparency") .. " (" .. math.floor(a * 100) .. "%)")
        end

                local fn = LeftRightConfig.fontName or "Standard"
        if ui.ddFont.SetSelectedValue then ui.ddFont:SetSelectedValue(fn, true) end
        ui.ddFont:SetText(fn)

        local ok = LeftRightConfig.fontOutline
        if not (LRH.OUTLINE_PRESETS and LRH.OUTLINE_PRESETS[ok]) then ok = "OUTLINE" end
        if ui.ddOutline.SetSelectedValue then ui.ddOutline:SetSelectedValue(ok, true) end
        ui.ddOutline:SetText((LRH.OUTLINE_PRESETS and LRH.OUTLINE_PRESETS[ok] and LRH.OUTLINE_PRESETS[ok].name) or "Outline")
    end

    
-- Design dropdown (custom)
do
    local items = {}
    local keys = (LRH.GetAvailableDesignKeys and LRH:GetAvailableDesignKeys()) or { "CLASSIC" }
    for _, key in ipairs(keys) do
        local d = LRH.DESIGNS and LRH.DESIGNS[key]
        if d then
            table.insert(items, { text = d.name, value = key })
        end
    end
    ui.ddDesign:SetItems(items)
    ui.ddDesign:SetSelectedValue(LeftRightConfig.arrowDesign or "CLASSIC", true)
    ui.ddDesign.onSelect = function(item)
        LeftRightConfig.arrowDesign = item.value
        if LRH.ApplyDesign then LRH:ApplyDesign() end
        if LRH.ApplyFillTint then LRH:ApplyFillTint() end
        if LRH.ApplyBorderTint then LRH:ApplyBorderTint() end
        if LRH.ApplyAlpha then LRH:ApplyAlpha() end
        Refresh()
    end
end


    
-- Border dropdown (custom)
do
    local items = {}
    for _, key in ipairs({ "GOLD", "SILVER", "BROWN" }) do
        local label =
            (key == "GOLD" and (L.BORDER_GOLD or "Gold")) or
            (key == "BROWN" and (L.BORDER_BROWN or "Brown")) or
            (L.BORDER_SILVER or "Silver")
        table.insert(items, { text = label, value = key })
    end
    ui.ddBorder:SetItems(items)
    ui.ddBorder:SetSelectedValue(LeftRightConfig.borderStyle or "GOLD", true)
    ui.ddBorder.onSelect = function(item)
        LeftRightConfig.borderStyle = item.value
        if LRH.ApplyBorderTint then LRH:ApplyBorderTint() end
        if LRH.ApplyAlpha then LRH:ApplyAlpha() end
        Refresh()
    end
end


    

-- Font dropdown (custom, LSM-only + preview)
do
    local LSM = LRH and LRH.LSM
    local items = {}
    if LSM then
        local names = LSM:List("font") or {}
        table.sort(names, function(a,b) return tostring(a) < tostring(b) end)

        -- Keep "Standard" as the first entry
        local stdIndex
        for i, n in ipairs(names) do
            if n == "Standard" then stdIndex = i; break end
        end
        if stdIndex then table.remove(names, stdIndex) end
        table.insert(names, 1, "Standard")

        for _, name in ipairs(names) do
            local path = LSM:Fetch("font", name, true)
            table.insert(items, { text = name, value = name, fontPath = path })
        end
    end


-- Outline dropdown (custom)
do
    local items = {}
    for _, key in ipairs({ "NONE", "OUTLINE", "THICKOUTLINE" }) do
        local o = LRH.OUTLINE_PRESETS and LRH.OUTLINE_PRESETS[key]
        if o then
            table.insert(items, { text = o.name, value = key })
        end
    end
    ui.ddOutline:SetItems(items)
    ui.ddOutline:SetSelectedValue(LeftRightConfig.fontOutline or "NONE", true)
    ui.ddOutline.onSelect = function(item)
        LeftRightConfig.fontOutline = item.value
        if LRH.ApplyFont then LRH:ApplyFont() end
        Refresh()
    end
end

    ui.ddFont:SetItems(items)
    ui.ddFont:SetSelectedValue(LeftRightConfig.fontName or "Standard", true)
    ui.ddFont.onSelect = function(item)
        LeftRightConfig.fontName = item.value
        if LRH.ApplyFont then LRH:ApplyFont() end
        Refresh()
    end
end


    ui.sGap:SetScript("OnValueChanged", function(_, value)
        LeftRightConfig.arrowGapPx = U.Clamp(value, LRH.MIN_GAP or 0, LRH.MAX_GAP or 700)
        if LRH.ApplySizeAndLayout then LRH:ApplySizeAndLayout() end
        if LRH.ApplyPosition then LRH:ApplyPosition() end
        Refresh()
    end)

    ui.sAlpha:SetScript("OnValueChanged", function(_, value)
        LeftRightConfig.alpha = U.Clamp(value, 0, 1)
        if LRH.ApplyAlpha then LRH:ApplyAlpha() end
        Refresh()
    end)

    ui.swL:SetScript("OnClick", function() OpenColorPicker("leftColor", ui.swL) end)
    ui.swR:SetScript("OnClick", function() OpenColorPicker("rightColor", ui.swR) end)

    ui.closeBtn:SetScript("OnClick", function()
        if LRH.SetDragMode then LRH:SetDragMode(false) end
        quick:Hide()
    end)

    quick:SetScript("OnHide", function()
        if LeftRightDB and LeftRightDB.dragEnabled and LRH.SetDragMode then
            LRH:SetDragMode(false)
        end
    end)

    LRH.UI.Quick.frame = quick
    LRH.UI.Quick.Refresh = Refresh

    Refresh()
end

function LRH.UI.ShowQuickOptions()
    if not LRH.UI.Quick.frame then
        LRH.UI.CreateQuickOptions()
    end
    LRH.UI.Quick.frame:Show()
    if LRH.UI.Quick.Refresh then LRH.UI.Quick.Refresh() end
end

function LRH.UI.HideQuickOptions()
    if LRH.UI.Quick.frame then
        LRH.UI.Quick.frame:Hide()
    end
end
