-- ══════════════════════════════════════════════════════════════
-- UI SUB-MODULE: Standalone Windows
-- Settings, HallOfFame, Statistics, KillStats, Milestones, Titles
-- Nutzt dieselbe UI-Table via addon._UI
-- ══════════════════════════════════════════════════════════════

local addonName, addon = ...
local GDL = _G["GuildDeathLog"]
local UI = addon._UI

-- Local refs
local CLASS_COLORS = UI.CLASS_COLORS
local GDL_ScaleLerp = UI.ScaleLerp

function UI:CreateSettings()
    local f = CreateFrame("Frame", "GDLEinstellungen", UIParent, "BackdropTemplate")
    f:SetSize(340, 490)
    f:SetPoint("CENTER", 260, -60)
    f:SetFrameStrata("HIGH")
    f:SetFrameLevel(100)
    f:SetMovable(true)
    f:EnableMouse(true)
    f:RegisterForDrag("LeftButton")
    f:SetScript("OnDragStart", f.StartMoving)
    f:SetScript("OnDragStop", f.StopMovingOrSizing)
    f:Hide()

    -- ── Schatten ────────────────────────────────────────────────
    local shadow = CreateFrame("Frame", nil, f, "BackdropTemplate")
    shadow:SetPoint("TOPLEFT", -6, 6)
    shadow:SetPoint("BOTTOMRIGHT", 6, -6)
    shadow:SetFrameLevel(f:GetFrameLevel() - 1)
    shadow:SetBackdrop({ bgFile = "Interface\\ChatFrame\\ChatFrameBackground" })
    shadow:SetBackdropColor(0, 0, 0, 0.55)

    -- ── Pergament + Gold-Rahmen ──────────────────────────────────
    f:SetBackdrop({
        bgFile   = "Interface\\ACHIEVEMENTFRAME\\UI-Achievement-Parchment-Horizontal",
        edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Gold-Border",
        tile     = false,
        edgeSize = 28,
        insets   = { left = 6, right = 6, top = 6, bottom = 6 }
    })
    f:SetBackdropColor(1, 1, 1, 1)
    f:SetBackdropBorderColor(0.72, 0.58, 0.32, 1)

    -- ── Titel-Trennlinie oben ────────────────────────────────────
    local headerLine = f:CreateTexture(nil, "ARTWORK")
    headerLine:SetPoint("TOPLEFT",  18, -44)
    headerLine:SetPoint("TOPRIGHT", -18, -44)
    headerLine:SetHeight(1)
    headerLine:SetColorTexture(0.55, 0.40, 0.18, 0.60)

    -- ── Titel ────────────────────────────────────────────────────
    local title = f:CreateFontString(nil, "OVERLAY")
    title:SetFont("Fonts\\MORPHEUS.TTF", 20, "")
    title:SetPoint("TOP", 0, -14)
    title:SetText("|cff3a2510Einstellungen|r")
    title:SetShadowOffset(1, -1)
    title:SetShadowColor(0, 0, 0, 0.15)

    -- ── Close Button ─────────────────────────────────────────────
    local close = CreateFrame("Button", nil, f, "UIPanelCloseButton")
    close:SetPoint("TOPRIGHT", 2, 2)

    -- ── Elegante Checkbox-Funktion ────────────────────────────────
    local function MakeCheck(textLabel, key, y)
        -- Custom checkbox: kleines Quadrat statt WoW-Standard
        local cbFrame = CreateFrame("Button", nil, f, "BackdropTemplate")
        cbFrame:SetSize(16, 16)
        cbFrame:SetPoint("TOPLEFT", 22, y)
        cbFrame:SetBackdrop({
            bgFile   = "Interface\\Buttons\\WHITE8x8",
            edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
            edgeSize = 6,
            insets   = { left=1, right=1, top=1, bottom=1 }
        })
        cbFrame:SetBackdropBorderColor(0.55, 0.42, 0.22, 0.80)

        local checked = GuildDeathLogDB.settings[key] and true or false

        local function UpdateVisual()
            if checked then
                cbFrame:SetBackdropColor(0.38, 0.28, 0.10, 0.95)
            else
                cbFrame:SetBackdropColor(0.82, 0.74, 0.56, 0.40)
            end
        end

        -- Haken-Textur
        local checkTex = cbFrame:CreateTexture(nil, "OVERLAY")
        checkTex:SetSize(14, 14)
        checkTex:SetPoint("CENTER", 0, 0)
        checkTex:SetTexture("Interface\\Buttons\\UI-CheckBox-Check")
        checkTex:SetVertexColor(0.85, 0.65, 0.20, 1)
        cbFrame.checkTex = checkTex

        local function UpdateAll()
            if checked then
                checkTex:Show()
                cbFrame:SetBackdropColor(0.38, 0.28, 0.10, 0.95)
            else
                checkTex:Hide()
                cbFrame:SetBackdropColor(0.82, 0.74, 0.56, 0.40)
            end
        end
        UpdateAll()

        cbFrame:SetScript("OnClick", function()
            checked = not checked
            GuildDeathLogDB.settings[key] = checked
            UpdateAll()
        end)
        cbFrame:SetScript("OnEnter", function()
            cbFrame:SetBackdropBorderColor(0.85, 0.68, 0.32, 1)
        end)
        cbFrame:SetScript("OnLeave", function()
            cbFrame:SetBackdropBorderColor(0.55, 0.42, 0.22, 0.80)
        end)

        -- Label
        local lbl = f:CreateFontString(nil, "OVERLAY")
        lbl:SetFont("Fonts\\FRIZQT__.TTF", 10, "")
        lbl:SetPoint("LEFT", cbFrame, "RIGHT", 8, 0)
        lbl:SetText(textLabel)
        lbl:SetTextColor(0.22, 0.15, 0.08)

        return cbFrame
    end

    local y = -56
    local dy = -27

    MakeCheck("Gildenchat-Ankuendigung",    "announce",            y); y = y + dy
    MakeCheck("Sound abspielen",            "sound",               y); y = y + dy
    MakeCheck("Todes-Popup anzeigen",       "overlay",             y); y = y + dy
    MakeCheck("Karten-Markierungen (Tode)", "mapMarkers",          y); y = y + dy
    MakeCheck("Gilden-Karte (Live-Pos.)",   "guildTracker",        y); y = y + dy
    MakeCheck("Titel auf Nameplates",       "nameplateTitles",     y); y = y + dy
    MakeCheck("Meilenstein-Nachrichten",    "milestoneAnnounce",   y); y = y + dy
    MakeCheck("Meilenstein-Popup",          "milestonePopup",      y); y = y + dy
    MakeCheck("Blizzard HC-Channel",        "useBlizzardChannel",  y); y = y + dy
    MakeCheck("Addon-Sync aktiv",           "useAddonChannel",     y); y = y + dy
    MakeCheck("Debug-Ausgaben",             "debugPrint",          y); y = y + dy

    -- ── Trennlinie Slider ────────────────────────────────────────
    local divLine = f:CreateTexture(nil, "ARTWORK")
    divLine:SetPoint("TOPLEFT",  18, -360)
    divLine:SetPoint("TOPRIGHT", -18, -360)
    divLine:SetHeight(1)
    divLine:SetColorTexture(0.55, 0.40, 0.18, 0.45)

    -- ── Slider Label ─────────────────────────────────────────────
    local sliderLabel = f:CreateFontString(nil, "OVERLAY")
    sliderLabel:SetFont("Fonts\\FRIZQT__.TTF", 9, "")
    sliderLabel:SetPoint("TOPLEFT", 22, -370)
    sliderLabel:SetText("|cff5E3E1C" .. GDL:T("Popup-Groesse:") .. "|r")

    -- ── Slider ───────────────────────────────────────────────────
    local slider = CreateFrame("Slider", "GDLPoupScaleSlider", f, "OptionsSliderTemplate")
    slider:SetPoint("TOPLEFT", 22, -395)
    slider:SetWidth(294)
    slider:SetMinMaxValues(0.5, 2.0)
    slider:SetValueStep(0.05)
    slider:SetObeyStepOnDrag(true)
    slider:SetValue(GuildDeathLogDB.settings.overlayScale or 1.0)

    local low  = _G[slider:GetName() .. "Low"]
    local high = _G[slider:GetName() .. "High"]
    local txt  = _G[slider:GetName() .. "Text"]
    if low  then low:SetText("50%")  end
    if high then high:SetText("200%") end
    if txt  then txt:SetText("") end  -- Label oben gesetzt

    slider:SetScript("OnValueChanged", function(self, value)
        value = math.max(0.5, math.min(2.0, value))
        GuildDeathLogDB.settings.overlayScale = value
        if UI.overlay then
            UI.overlay:SetScale(value)
            UI.overlay.currentScale = value
        end
    end)

    -- ── Trennlinie Footer ────────────────────────────────────────
    local footLine = f:CreateTexture(nil, "ARTWORK")
    footLine:SetPoint("BOTTOMLEFT",  18, 52)
    footLine:SetPoint("BOTTOMRIGHT", -18, 52)
    footLine:SetHeight(1)
    footLine:SetColorTexture(0.55, 0.40, 0.18, 0.50)

    -- ── Elegante Buttons ─────────────────────────────────────────
    local function MakeElegantBtn(labelText, w)
        local btn = CreateFrame("Button", nil, f, "BackdropTemplate")
        btn:SetSize(w, 26)
        btn:SetBackdrop({
            bgFile   = "Interface\\Buttons\\WHITE8x8",
            edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
            edgeSize = 10,
            insets   = { left=2, right=2, top=2, bottom=2 }
        })
        btn:SetBackdropColor(0.32, 0.22, 0.10, 0.92)
        btn:SetBackdropBorderColor(0.62, 0.46, 0.22, 0.90)
        local lbl = btn:CreateFontString(nil, "OVERLAY")
        lbl:SetFont("Fonts\\FRIZQT__.TTF", 10, "")
        lbl:SetPoint("CENTER", 0, 0)
        lbl:SetText("|cffC8A050" .. labelText .. "|r")
        btn:SetScript("OnEnter", function(self)
            self:SetBackdropColor(0.48, 0.32, 0.14, 1.0)
            self:SetBackdropBorderColor(0.85, 0.65, 0.30, 1)
        end)
        btn:SetScript("OnLeave", function(self)
            self:SetBackdropColor(0.32, 0.22, 0.10, 0.92)
            self:SetBackdropBorderColor(0.62, 0.46, 0.22, 0.90)
        end)
        return btn
    end

    local btnTest = MakeElegantBtn("Test", 88)
    btnTest:SetPoint("BOTTOMLEFT", 20, 18)
    btnTest:SetScript("OnClick", function()
        local dummy = {
            name      = UnitName("player") or "DEINNAME",
            level     = UnitLevel("player") or 1,
            classId   = select(3, UnitClass("player")) or 1,
            zone      = GetZoneText() or "",
            killerName = "Test",
            killer    = "Test",
            lastWords = "Nur ein Test...",
        }
        UI:ShowOverlay(dummy, true)
    end)

    local btnReset = MakeElegantBtn("Reset Pos", 100)
    btnReset:SetPoint("BOTTOM", 0, 18)
    btnReset:SetScript("OnClick", function()
        GuildDeathLogDB.settings.overlayPosition = nil
        if UI.overlay then
            UI.overlay:ClearAllPoints()
            UI.overlay:SetPoint("TOP", 0, -80)
        end
        GDL:Print("|cffFFD100[GDL]|r " .. GDL:T("Overlay-Position zurueckgesetzt."))
    end)

    local btnOk = MakeElegantBtn("OK", 88)
    btnOk:SetPoint("BOTTOMRIGHT", -20, 18)
    btnOk:SetScript("OnClick", function() f:Hide() end)

    self.settingsFrame = f
end


function UI:ShowSettings()
    if not self.settingsFrame then self:CreateSettings() end
    self.settingsFrame:Show()
end

-- Separates the optional supporter UI from the normal settings so the
-- settings window stays clean and doesn't overflow.


-- â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•
-- RUHMESHALLE / HALL OF FAME (v4.0)
-- â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•
function UI:ShowHallOfFame()
    if not self.hallOfFameFrame then
        self:CreateHallOfFameWindow()
    end
    self.hallOfFameFrame:Show()
    self:UpdateHallOfFame()
end

function UI:CreateHallOfFameWindow()
    local HoF = GDL:GetModule("HallOfFame")
    if not HoF then return end
    
    local f = CreateFrame("Frame", "GDLHallOfFame", UIParent, "BackdropTemplate")
    f:SetSize(450, 500)
    f:SetPoint("CENTER", UIParent, "CENTER", 520, 0)  -- Rechts-mitte (cascade)
    f:SetFrameStrata("HIGH")
    f:SetFrameLevel(110)
    f:SetMovable(true)
    f:EnableMouse(true)
    f:RegisterForDrag("LeftButton")
    f:SetScript("OnDragStart", f.StartMoving)
    f:SetScript("OnDragStop", f.StopMovingOrSizing)
    f:SetScript("OnMouseDown", function(self) self:Raise() end)
    
    f:SetBackdrop({
        bgFile = "Interface\\ACHIEVEMENTFRAME\\UI-Achievement-Parchment-Horizontal",
        edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Gold-Border",
        edgeSize = 26,
        insets = {left = 5, right = 5, top = 5, bottom = 5}
    })
    f:SetBackdropColor(1, 1, 1, 1)
    f:SetBackdropBorderColor(0.8, 0.7, 0.4, 1)
    
    -- Close Button
    local close = CreateFrame("Button", nil, f, "UIPanelCloseButton")
    close:SetPoint("TOPRIGHT", -5, -5)
    
    -- Content
    self.hallOfFameTab = HoF:CreateTab(f)
    self.hallOfFameTab:Show()
    
    self.hallOfFameFrame = f
end

function UI:UpdateHallOfFame()
    if self.hallOfFameTab and self.hallOfFameTab.Update then
        self.hallOfFameTab.Update()
    end
end

-- â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•
-- STATISTIKEN / STATISTICS (v4.0)
-- â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•
function UI:ShowStatistics()
    if not self.statsFrame then
        self:CreateStatisticsWindow()
    end
    self.statsFrame:Show()
    self:UpdateStatistics()
end

function UI:CreateStatisticsWindow()
    local f = CreateFrame("Frame", "GDLStatistics", UIParent, "BackdropTemplate")
    f:SetSize(420, 480)
    f:SetPoint("CENTER", UIParent, "CENTER", 500, -300)
    f:SetFrameStrata("HIGH")
    f:SetFrameLevel(110)
    f:SetMovable(true)
    f:EnableMouse(true)
    f:RegisterForDrag("LeftButton")
    f:SetScript("OnDragStart", f.StartMoving)
    f:SetScript("OnDragStop", f.StopMovingOrSizing)
    f:SetScript("OnMouseDown", function(self) self:Raise() end)
    
    f:SetBackdrop({
        bgFile = "Interface\\ACHIEVEMENTFRAME\\UI-Achievement-Parchment-Horizontal",
        edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Gold-Border",
        edgeSize = 26,
        insets = {left = 5, right = 5, top = 5, bottom = 5}
    })
    
    local close = CreateFrame("Button", nil, f, "UIPanelCloseButton")
    close:SetPoint("TOPRIGHT", -5, -5)
    
    -- Title mit Icon
    local titleIcon = f:CreateTexture(nil, "ARTWORK")
    titleIcon:SetSize(28, 28)
    titleIcon:SetPoint("TOP", -100, -12)
    titleIcon:SetTexture("Interface\\Icons\\INV_Misc_Book_09")
    
    local title = f:CreateFontString(nil, "OVERLAY")
    title:SetFont("Fonts\\MORPHEUS.TTF", 16, "")
    title:SetPoint("LEFT", titleIcon, "RIGHT", 8, 0)
    local statsTitle = GetLocale() == "deDE" and "Statistiken" or "Statistics"
    title:SetText("|cff2A1606" .. statsTitle .. "|r")
    
    -- Scroll für Content
    local scroll = CreateFrame("ScrollFrame", nil, f, "UIPanelScrollFrameTemplate")
    scroll:SetPoint("TOPLEFT", 20, -50)
    scroll:SetPoint("BOTTOMRIGHT", -35, 20)
    
    local child = CreateFrame("Frame", nil, scroll)
    child:SetSize(scroll:GetWidth(), 1200)
    child:SetHeight(1200)  -- Wird durch content:SetHeight() dynamisch angepasst
    scroll:SetScrollChild(child)
    f.contentFrame = child
    
    self.statsFrame = f
end

function UI:UpdateStatistics()
    local Stats = GDL:GetModule("Statistics")
    if not Stats or not self.statsFrame then return end
    
    local content = self.statsFrame.contentFrame
    -- Clear
    for _, c in ipairs({content:GetChildren()}) do c:Hide() end
    for _, r in ipairs({content:GetRegions()}) do r:Hide() end
    
    local y = 0
    local summary = Stats:GetSummary()
    local zones = Stats:GetDangerousZones(5)
    local killers = Stats:GetDeadliestKillers(5)
    local classes = Stats:GetDeathsByClass()
    
    -- â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•
    -- ZUSAMMENFASSUNG BOX
    -- â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•
    local sumBox = CreateFrame("Frame", nil, content, "BackdropTemplate")
    sumBox:SetSize(360, 95)
    sumBox:SetPoint("TOPLEFT", 0, y)
    sumBox:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8X8",
        edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
        edgeSize = 12,
        insets = {left = 2, right = 2, top = 2, bottom = 2}
    })
    sumBox:SetBackdropColor(0.1, 0.08, 0.05, 0.7)
    sumBox:SetBackdropBorderColor(0.6, 0.5, 0.3, 0.9)
    
    local sumIcon = sumBox:CreateTexture(nil, "ARTWORK")
    sumIcon:SetSize(32, 32)
    sumIcon:SetPoint("TOPLEFT", 10, -10)
    sumIcon:SetTexture("Interface\\Icons\\INV_Misc_Note_01")
    
    local sumTitle = sumBox:CreateFontString(nil, "OVERLAY")
    sumTitle:SetFont("Fonts\\FRIZQT__.TTF", 11, "")
    sumTitle:SetPoint("TOPLEFT", sumIcon, "TOPRIGHT", 8, -2)
    sumTitle:SetText("|cff8A5E14Zusammenfassung / Summary|r")
    
    -- Statistik-Werte in 2 Spalten
    local col1X, col2X = 15, 190
    local row1Y, row2Y, row3Y = -50, -65, -80
    
    local function StatLabel(parent, x, y, label, value)
        local lbl = parent:CreateFontString(nil, "OVERLAY")
        lbl:SetFont("Fonts\\FRIZQT__.TTF", 10, "")
        lbl:SetPoint("TOPLEFT", x, y)
        lbl:SetText("|cff5E3E1C" .. label .. ":|r")
        
        local val = parent:CreateFontString(nil, "OVERLAY")
        val:SetFont("Fonts\\FRIZQT__.TTF", 10, "")
        val:SetPoint("TOPLEFT", x + 80, y)
        val:SetText("|cff2A1606" .. tostring(value) .. "|r")
    end
    
    StatLabel(sumBox, col1X, row1Y, "Gesamt/Total", summary.total .. " Tode/Deaths")
    StatLabel(sumBox, col2X, row1Y, "Heute/Today", summary.today)
    StatLabel(sumBox, col1X, row2Y, "Woche/Week", summary.thisWeek)
    StatLabel(sumBox, col2X, row2Y, "Avg Level", string.format("%.1f", summary.avgLevel))
    StatLabel(sumBox, col1X, row3Y, "Max Level", summary.highestLevel)
    StatLabel(sumBox, col2X, row3Y, "Min Level", summary.lowestLevel)
    
    y = y - 105
    
    -- â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•
    -- GEFAEHRLICHSTE ZONEN
    -- â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•
    local zoneBox = CreateFrame("Frame", nil, content, "BackdropTemplate")
    zoneBox:SetSize(175, 120)
    zoneBox:SetPoint("TOPLEFT", 0, y)
    zoneBox:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8X8",
        edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
        edgeSize = 12,
        insets = {left = 2, right = 2, top = 2, bottom = 2}
    })
    zoneBox:SetBackdropColor(0.15, 0.05, 0.05, 0.6)
    zoneBox:SetBackdropBorderColor(0.6, 0.3, 0.3, 0.9)
    zoneBox:SetClipsChildren(true)
    
    local zoneIcon = zoneBox:CreateTexture(nil, "ARTWORK")
    zoneIcon:SetSize(20, 20)
    zoneIcon:SetPoint("TOPLEFT", 8, -8)
    zoneIcon:SetTexture("Interface\\Icons\\Ability_Hunter_Pathfinding")
    
    local zoneTitle = zoneBox:CreateFontString(nil, "OVERLAY")
    zoneTitle:SetFont("Fonts\\FRIZQT__.TTF", 9, "")
    zoneTitle:SetPoint("LEFT", zoneIcon, "RIGHT", 5, 0)
    zoneTitle:SetText("|cff7A2C14Zonen / Zones|r")
    
    local zy = -35
    if #zones == 0 then
        local noData = zoneBox:CreateFontString(nil, "OVERLAY")
        noData:SetFont("Fonts\\FRIZQT__.TTF", 9, "")
        noData:SetPoint("TOPLEFT", 10, zy)
        noData:SetText("|cff5E3E1CKeine Daten / No data|r")
    else
        for i, zone in ipairs(zones) do
            if i > 5 then break end
            local zt = zoneBox:CreateFontString(nil, "OVERLAY")
            zt:SetFont("Fonts\\FRIZQT__.TTF", 9, "")
            zt:SetPoint("TOPLEFT", 10, zy)
            local zoneName = (zone.zone or "?"):sub(1, 12)
            zt:SetText(string.format("|cff7A2C14%d.|r |cff2A1606%s|r |cff5E3E1C(%d)|r", i, zoneName, zone.count))
            zy = zy - 14
        end
    end
    
    -- â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•
    -- TOEDLICHSTE MONSTER
    -- â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•
    local killerBox = CreateFrame("Frame", nil, content, "BackdropTemplate")
    killerBox:SetSize(175, 120)
    killerBox:SetPoint("TOPLEFT", 185, y)
    killerBox:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8X8",
        edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
        edgeSize = 12,
        insets = {left = 2, right = 2, top = 2, bottom = 2}
    })
    killerBox:SetBackdropColor(0.1, 0.05, 0.15, 0.6)
    killerBox:SetBackdropBorderColor(0.5, 0.3, 0.6, 0.9)
    killerBox:SetClipsChildren(true)
    
    local killerIcon = killerBox:CreateTexture(nil, "ARTWORK")
    killerIcon:SetSize(20, 20)
    killerIcon:SetPoint("TOPLEFT", 8, -8)
    killerIcon:SetTexture("Interface\\Icons\\Ability_Creature_Cursed_02")
    
    local killerTitle = killerBox:CreateFontString(nil, "OVERLAY")
    killerTitle:SetFont("Fonts\\FRIZQT__.TTF", 9, "")
    killerTitle:SetPoint("LEFT", killerIcon, "RIGHT", 5, 0)
    killerTitle:SetText("|cff5C3A54Monster / Killers|r")
    
    local ky = -35
    if #killers == 0 then
        local noData = killerBox:CreateFontString(nil, "OVERLAY")
        noData:SetFont("Fonts\\FRIZQT__.TTF", 9, "")
        noData:SetPoint("TOPLEFT", 10, ky)
        noData:SetText("|cff5E3E1CKeine Daten / No data|r")
    else
        for i, killer in ipairs(killers) do
            if i > 5 then break end
            local kt = killerBox:CreateFontString(nil, "OVERLAY")
            kt:SetFont("Fonts\\FRIZQT__.TTF", 9, "")
            kt:SetPoint("TOPLEFT", 10, ky)
    local killerName = (UI.CleanKillerName(killer.name or "") or "?"):sub(1, 12)
            kt:SetText(string.format("|cff5C3A54%d.|r |cff2A1606%s|r |cff5E3E1C(%d)|r", i, killerName, killer.count))
            ky = ky - 14
        end
    end
    
    y = y - 130
    
    -- â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•
    -- TODE NACH KLASSE
    -- â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•
    local classBox = CreateFrame("Frame", nil, content, "BackdropTemplate")
    classBox:SetSize(360, 145)
    classBox:SetPoint("TOPLEFT", 0, y)
    classBox:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8X8",
        edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
        edgeSize = 12,
        insets = {left = 2, right = 2, top = 2, bottom = 2}
    })
    classBox:SetBackdropColor(0.08, 0.08, 0.12, 0.6)
    classBox:SetBackdropBorderColor(0.4, 0.4, 0.6, 0.9)
    classBox:SetClipsChildren(true)
    
    local classIcon = classBox:CreateTexture(nil, "ARTWORK")
    classIcon:SetSize(20, 20)
    classIcon:SetPoint("TOPLEFT", 8, -8)
    classIcon:SetTexture("Interface\\Icons\\INV_Misc_GroupLooking")
    
    local classTitle = classBox:CreateFontString(nil, "OVERLAY")
    classTitle:SetFont("Fonts\\FRIZQT__.TTF", 10, "")
    classTitle:SetPoint("LEFT", classIcon, "RIGHT", 5, 0)
    classTitle:SetText("|cff3A5068Klassen / Classes|r")
    
    -- 2 Spalten fuer Klassen
    local cy = -35
    local classCol = 0
    for i, class in ipairs(classes) do
        local col = self:GetClassColor(class.classId)
        local xOffset = classCol == 0 and 10 or 185
        
        -- Klassen-Icon
        local cIcon = classBox:CreateTexture(nil, "ARTWORK")
        cIcon:SetSize(16, 16)
        cIcon:SetPoint("TOPLEFT", xOffset, cy)
        cIcon:SetTexture(self:GetClassIcon(class.classId))
        
        -- Klassen-Name und Count - helle Klassenfarben
        local ct = classBox:CreateFontString(nil, "OVERLAY")
        ct:SetFont("Fonts\\FRIZQT__.TTF", 10, "")
        ct:SetPoint("LEFT", cIcon, "RIGHT", 5, 0)
        -- Farben aufhellen
        local r = math.min(1, col[1] + 0.3)
        local g = math.min(1, col[2] + 0.3)
        local b = math.min(1, col[3] + 0.3)
        ct:SetText(string.format("|cff%02x%02x%02x%s|r: |cff2A1606%d|r", 
            r*255, g*255, b*255, 
            (class.name or "?"):sub(1,10), class.count))
        
        classCol = classCol + 1
        if classCol >= 2 then
            classCol = 0
            cy = cy - 22
        end
    end
    
    y = y - 155

    -- â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•
    -- LEVEL-VERTEILUNG (GetDeathsByLevel)
    -- â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•
    local levelData = Stats:GetDeathsByLevel()
    local maxLevelDeaths = 0
    for _, count in ipairs(levelData) do
        if count > maxLevelDeaths then maxLevelDeaths = count end
    end

    if maxLevelDeaths > 0 then
        local lvlBox = CreateFrame("Frame", nil, content, "BackdropTemplate")
        lvlBox:SetSize(360, 130)
        lvlBox:SetPoint("TOPLEFT", 0, y)
        lvlBox:SetBackdrop({
            bgFile = "Interface\\Buttons\\WHITE8X8",
            edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
            edgeSize = 12, insets = {left=2, right=2, top=2, bottom=2}
        })
        lvlBox:SetBackdropColor(0.05, 0.1, 0.15, 0.6)
        lvlBox:SetBackdropBorderColor(0.4, 0.6, 0.9, 0.9)
        lvlBox:SetClipsChildren(true)

        local lvlIcon = lvlBox:CreateTexture(nil, "ARTWORK")
        lvlIcon:SetSize(20, 20); lvlIcon:SetPoint("TOPLEFT", 8, -8)
        lvlIcon:SetTexture("Interface\\Icons\\INV_Misc_Note_02")
        local lvlTitle = lvlBox:CreateFontString(nil, "OVERLAY")
        lvlTitle:SetFont("Fonts\\FRIZQT__.TTF", 9, "")
        lvlTitle:SetPoint("LEFT", lvlIcon, "RIGHT", 5, 0)
        lvlTitle:SetText("|cff3A5068" .. GDL:T("Level-Verteilung (Tode pro Level)") .. "|r")

        -- Balken-Chart: Level 1-60 in 12 Gruppen Ã  5 Level
        -- Innerer Container: Balken werden NUR in diesem Frame gezeichnet
        -- Position: direkt unter Titel, über den Labels
        local barArea = CreateFrame("Frame", nil, lvlBox)
        barArea:SetPoint("TOPLEFT", lvlBox, "TOPLEFT", 8, -34)   -- 34px unter Box-Top (Titel endet bei ~30)
        barArea:SetPoint("BOTTOMRIGHT", lvlBox, "BOTTOMRIGHT", -8, 16) -- 16px vom Bottom (Platz fuer Labels)
        barArea:SetClipsChildren(true)

        local barW = 26
        local barAreaH = 130 - 34 - 16  -- = 80px verfuegbar
        for group = 1, 12 do
            local groupCount = 0
            local startLvl = (group-1)*5 + 1
            local endLvl = group*5
            for lvl = startLvl, endLvl do
                groupCount = groupCount + (levelData[lvl] or 0)
            end
            if groupCount > 0 then
                local barH = math.max(2, math.floor((groupCount / maxLevelDeaths) * barAreaH))
                local barFrame = CreateFrame("Frame", nil, barArea)
                barFrame:SetSize(barW - 2, barH)
                barFrame:SetPoint("BOTTOMLEFT", barArea, "BOTTOMLEFT", 2 + (group-1)*barW, 0)
                local bar = barFrame:CreateTexture(nil, "ARTWORK")
                bar:SetAllPoints(barFrame)
                -- Farbe: niedrig=grün, hoch=rot
                local r = math.min(1, group/12 * 1.5)
                local g = math.min(1, (1 - group/12) * 1.5)
                bar:SetColorTexture(r, g, 0.2, 0.8)
            end
            -- Label
            local lbl = lvlBox:CreateFontString(nil, "OVERLAY")
            lbl:SetFont("Fonts\\FRIZQT__.TTF", 7, "")
            lbl:SetPoint("BOTTOMLEFT", 8 + (group-1)*barW, 8)
            lbl:SetText(tostring(startLvl))
            lbl:SetTextColor(0.26, 0.2, 0.12)
        end
        y = y - 140
    end

    -- â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•
    -- STUNDEN-VERTEILUNG (GetDeathsByTime)
    -- â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•
    local hourData, dayData = Stats:GetDeathsByTime()
    local maxHour = 0
    for h = 0, 23 do if (hourData[h] or 0) > maxHour then maxHour = hourData[h] end end

    if maxHour > 0 then
        local timeBox = CreateFrame("Frame", nil, content, "BackdropTemplate")
        timeBox:SetSize(360, 120)
        timeBox:SetPoint("TOPLEFT", 0, y)
        timeBox:SetBackdrop({
            bgFile = "Interface\\Buttons\\WHITE8X8",
            edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
            edgeSize = 12, insets = {left=2, right=2, top=2, bottom=2}
        })
        timeBox:SetBackdropColor(0.08, 0.12, 0.08, 0.6)
        timeBox:SetBackdropBorderColor(0.4, 0.8, 0.4, 0.9)
        timeBox:SetClipsChildren(true)

        local tIcon = timeBox:CreateTexture(nil, "ARTWORK")
        tIcon:SetSize(20, 20); tIcon:SetPoint("TOPLEFT", 8, -8)
        tIcon:SetTexture("Interface\\Icons\\INV_Misc_PocketWatch_01")
        local tTitle = timeBox:CreateFontString(nil, "OVERLAY")
        tTitle:SetFont("Fonts\\FRIZQT__.TTF", 9, "")
        tTitle:SetPoint("LEFT", tIcon, "RIGHT", 5, 0)
        tTitle:SetText("|cff1A5A24Tode nach Uhrzeit (Server)|r")

        local barW2 = math.floor(340/24)
        -- Innerer Container fuer Balken
        local barArea2 = CreateFrame("Frame", nil, timeBox)
        barArea2:SetPoint("TOPLEFT", timeBox, "TOPLEFT", 8, -34)
        barArea2:SetPoint("BOTTOMRIGHT", timeBox, "BOTTOMRIGHT", -8, 16)
        barArea2:SetClipsChildren(true)

        local barAreaH2 = 120 - 34 - 16  -- = 70px
        for h = 0, 23 do
            local count = hourData[h] or 0
            if count > 0 then
                local barH = math.max(2, math.floor((count / maxHour) * barAreaH2))
                local barF = CreateFrame("Frame", nil, barArea2)
                barF:SetSize(barW2 - 1, barH)
                barF:SetPoint("BOTTOMLEFT", barArea2, "BOTTOMLEFT", 2 + h*barW2, 0)
                local bar = barF:CreateTexture(nil, "ARTWORK")
                bar:SetAllPoints(barF)
                -- Nacht=blau, Tag=gelb
                local isDaytime = (h >= 6 and h <= 20)
                bar:SetColorTexture(isDaytime and 0.9 or 0.3, isDaytime and 0.8 or 0.4, isDaytime and 0.2 or 0.9, 0.8)
            end
            if h % 6 == 0 then
                local lbl = timeBox:CreateFontString(nil, "OVERLAY")
                lbl:SetFont("Fonts\\FRIZQT__.TTF", 8, "")
                lbl:SetPoint("BOTTOMLEFT", 8 + h*barW2, 6)
                lbl:SetText(string.format("%02d", h))
                lbl:SetTextColor(0.25, 0.2, 0.12)
            end
        end
        y = y - 130
    end

    content:SetHeight(math.abs(y) + 20)
end


-- â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•
-- KILLSTATS FENSTER (Tötungen nach Kreaturtyp + Meilensteine)
-- â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•

function UI:ShowKillStats()
    if not self.killStatsFrame then
        self:CreateKillStatsWindow()
    end
    self.killStatsFrame:Show()
    self:UpdateKillStats()
end

function UI:CreateKillStatsWindow()
    local f = CreateFrame("Frame", "GDLKillStats", UIParent, "BackdropTemplate")
    f:SetSize(400, 440)
    f:SetPoint("CENTER", UIParent, "CENTER", -500, 200)
    f:SetFrameStrata("HIGH")
    f:SetFrameLevel(110)
    f:SetMovable(true)
    f:EnableMouse(true)
    f:RegisterForDrag("LeftButton")
    f:SetScript("OnDragStart", f.StartMoving)
    f:SetScript("OnDragStop", f.StopMovingOrSizing)
    f:SetScript("OnMouseDown", function(self) self:Raise() end)
    f:SetBackdrop({
        bgFile = "Interface\\ACHIEVEMENTFRAME\\UI-Achievement-Parchment-Horizontal",
        edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Gold-Border",
        edgeSize = 26, insets = {left=5, right=5, top=5, bottom=5}
    })

    local close = CreateFrame("Button", nil, f, "UIPanelCloseButton")
    close:SetPoint("TOPRIGHT", -5, -5)

    local titleIcon = f:CreateTexture(nil, "ARTWORK")
    titleIcon:SetSize(28, 28)
    titleIcon:SetPoint("TOP", -90, -12)
    titleIcon:SetTexture("Interface\\Icons\\Ability_Warrior_Sunder")

    local title = f:CreateFontString(nil, "OVERLAY")
    title:SetFont("Fonts\\MORPHEUS.TTF", 15, "")
    title:SetPoint("LEFT", titleIcon, "RIGHT", 8, 0)
    local t = GetLocale() == "deDE" and "Kill-Statistiken" or "Kill Statistics"
    title:SetText("|cff2A1606" .. t .. "|r")

    local scroll = CreateFrame("ScrollFrame", nil, f, "UIPanelScrollFrameTemplate")
    scroll:SetPoint("TOPLEFT", 20, -50)
    scroll:SetPoint("BOTTOMRIGHT", -35, 20)
    local child = CreateFrame("Frame", nil, scroll)
    child:SetSize(340, 800)
    scroll:SetScrollChild(child)
    f.contentFrame = child
    self.killStatsFrame = f
end

function UI:UpdateKillStats()
    local KS = GDL:GetModule("KillStats")
    if not KS or not self.killStatsFrame then return end

    local content = self.killStatsFrame.contentFrame
    for _, c in ipairs({content:GetChildren()}) do c:Hide() end
    for _, r in ipairs({content:GetRegions()}) do r:Hide() end

    local charKey = KS:GetCharacterKey()
    local stats = KS:GetStats(charKey)
    local totalKills = KS:GetTotalKills(charKey)
    local y = 0

    -- Header: Gesamtkills
    local totalBox = CreateFrame("Frame", nil, content, "BackdropTemplate")
    totalBox:SetSize(330, 50)
    totalBox:SetPoint("TOPLEFT", 0, y)
    totalBox:SetBackdrop({bgFile="Interface\\Buttons\\WHITE8X8", edgeFile="Interface\\Tooltips\\UI-Tooltip-Border", edgeSize=10, insets={left=2,right=2,top=2,bottom=2}})
    totalBox:SetBackdropColor(0.1, 0.08, 0.05, 0.7)
    totalBox:SetBackdropBorderColor(0.7, 0.6, 0.3, 0.9)

    local charName = UnitName("player") or "?"
    local charRealm = GetRealmName() or ""
    local displayName = charName .. (charRealm ~= "" and ("-" .. charRealm) or "")
    local totalLbl = totalBox:CreateFontString(nil, "OVERLAY")
    totalLbl:SetFont("Fonts\\FRIZQT__.TTF", 11, "")
    totalLbl:SetPoint("TOPLEFT", 10, -10)
    totalLbl:SetText("|cff8A5E14" .. displayName .. ":|r |cff2A1606" .. totalKills .. " |cff5E3E1C" .. GDL:T("Kills gesamt") .. "|r")

    local unlockedMilestones = KS:CountUnlockedMilestones()
    local milestoneLbl = totalBox:CreateFontString(nil, "OVERLAY")
    milestoneLbl:SetFont("Fonts\\FRIZQT__.TTF", 10, "")
    milestoneLbl:SetPoint("TOPLEFT", 10, -28)
    milestoneLbl:SetText("|cff1A5A24" .. (unlockedMilestones or 0) .. " Meilensteine freigeschaltet|r")
    y = y - 60

    -- Kills nach Kreaturtyp
    local typeOrder = {"humanoid","undead","demon","dragonkin","beast","elemental","giant","mechanical","critter"}
    local typeNames = {
        humanoid="Humanoid", undead="Untoter", demon="Dämon",
        dragonkin="Drachkin", beast="Wildtier", elemental="Elementar",
        giant="Riese", mechanical="Mechanisch", critter="Kleintier"
    }
    local typeIcons = {
        humanoid="Interface\\Icons\\INV_Misc_EngGizmos_19",
        undead="Interface\\Icons\\Spell_Shadow_AnimateDead",
        demon="Interface\\Icons\\Ability_Warlock_SummonImp",
        dragonkin="Interface\\Icons\\Ability_Dragonkin",
        beast="Interface\\Icons\\Ability_Hunter_BeastCall",
        elemental="Interface\\Icons\\Spell_Fire_SelfDestruct",
        giant="Interface\\Icons\\INV_Misc_MonsterHead_02",
        mechanical="Interface\\Icons\\INV_Misc_EngGizmos_04",
        critter="Interface\\Icons\\Ability_Hunter_Pet_Moth",
    }

    local headerLbl = content:CreateFontString(nil, "OVERLAY")
    headerLbl:SetFont("Fonts\\FRIZQT__.TTF", 10, "")
    headerLbl:SetPoint("TOPLEFT", 5, y)
    headerLbl:SetText("|cff5E3E1C" .. GDL:T("Tötungen nach Kreaturtyp:") .. "|r")
    y = y - 18

    local maxCount = 1
    for _, typeKey in ipairs(typeOrder) do
        local c = KS:GetKillCount(typeKey, charKey)
        if c > maxCount then maxCount = c end
    end

    for _, typeKey in ipairs(typeOrder) do
        local count = KS:GetKillCount(typeKey, charKey)
        if count > 0 then
            local row = CreateFrame("Frame", nil, content)
            row:SetSize(330, 22)
            row:SetPoint("TOPLEFT", 0, y)

            local icon = row:CreateTexture(nil, "ARTWORK")
            icon:SetSize(18, 18)
            icon:SetPoint("LEFT", 0, 0)
            icon:SetTexture(typeIcons[typeKey] or "Interface\\Icons\\INV_Misc_QuestionMark")

            local nameLbl = row:CreateFontString(nil, "OVERLAY")
            nameLbl:SetFont("Fonts\\FRIZQT__.TTF", 10, "")
            nameLbl:SetPoint("LEFT", 22, 0)
            nameLbl:SetWidth(90)
            nameLbl:SetText(typeNames[typeKey] or typeKey)
            nameLbl:SetTextColor(0.28, 0.2, 0.08)

            -- Balken
            local barBg = row:CreateTexture(nil, "BACKGROUND")
            barBg:SetSize(150, 12)
            barBg:SetPoint("LEFT", 115, 0)
            barBg:SetColorTexture(0.1, 0.1, 0.1, 0.8)

            local barFill = row:CreateTexture(nil, "ARTWORK")
            local barW = math.max(2, math.floor((count / maxCount) * 150))
            barFill:SetSize(barW, 12)
            barFill:SetPoint("LEFT", 115, 0)
            barFill:SetColorTexture(0.6, 0.3, 0.1, 0.9)

            local countLbl = row:CreateFontString(nil, "OVERLAY")
            countLbl:SetFont("Fonts\\FRIZQT__.TTF", 10, "")
            countLbl:SetPoint("LEFT", 270, 0)
            countLbl:SetText("|cff2A1606" .. count .. "|r")

            y = y - 24
        end
    end

    content:SetHeight(math.abs(y) + 20)
end

-- â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•
-- ERFOLGE / ACHIEVEMENTS (v4.0)
-- â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•
function UI:ShowAchievements()
    if not self.achieveFrame then
        self:CreateMilestonesWindow()
    end
    -- Ohne Supporter-Code direkt auf Support-Tab springen,
    -- damit der User den Code eingeben kann
    if not GDL:IsSupporterActive() then
        self.achieveFrame.selectedCategory = "support"
    end
    self.achieveFrame:Show()
    self:UpdateMilestones()
end

function UI:CreateMilestonesWindow()
    local f = CreateFrame("Frame", "GDLMilestones", UIParent, "BackdropTemplate")
    f:SetSize(450, 550)
    f:SetPoint("CENTER", UIParent, "CENTER", 560, -330)  -- Rechts-unten diagonal versetzt (cascade)
    f:SetFrameStrata("HIGH")
    f:SetFrameLevel(110)
    f:SetMovable(true)
    f:EnableMouse(true)
    f:RegisterForDrag("LeftButton")
    f:SetScript("OnDragStart", f.StartMoving)
    f:SetScript("OnDragStop", f.StopMovingOrSizing)
    f:SetScript("OnMouseDown", function(self) self:Raise() end)
    
    f:SetBackdrop({
        bgFile = "Interface\\ACHIEVEMENTFRAME\\UI-Achievement-Parchment-Horizontal",
        edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Gold-Border",
        edgeSize = 26,
        insets = {left = 5, right = 5, top = 5, bottom = 5}
    })
    
    local close = CreateFrame("Button", nil, f, "UIPanelCloseButton")
    close:SetPoint("TOPRIGHT", -5, -5)
    
    local title = f:CreateFontString(nil, "OVERLAY")
    title:SetFont("Fonts\\MORPHEUS.TTF", 18, "")
    title:SetPoint("TOP", 0, -15)
    title:SetText("|cff2A1606" .. GDL:L("BTN_MILESTONES") .. "|r")
    
    -- Charakter-Name Anzeige
    local charInfo = f:CreateFontString(nil, "OVERLAY")
    charInfo:SetFont("Fonts\\FRIZQT__.TTF", 12, "")
    charInfo:SetPoint("TOP", title, "BOTTOM", 0, -5)
    charInfo:SetTextColor(0.2, 0.15, 0.1)
    f.charInfo = charInfo

    -- Supporter-Status (nur Info / kosmetisch)
    local supInfo = f:CreateFontString(nil, "OVERLAY")
    -- NOTE: Needs a valid font path or SetText() may error with "Font not set" on Classic.
    supInfo:SetFont(STANDARD_TEXT_FONT or "Fonts\\FRIZQT__.TTF", 10, "")
    supInfo:SetPoint("TOP", charInfo, "BOTTOM", 0, -2)
    supInfo:SetTextColor(0.25, 0.2, 0.15)
    f.supInfo = supInfo

    
    -- Kategorie-Namen lokalisiert (für Tooltips)
    local catTooltips = {
        level = GetLocale() == "deDE" and "Level-Meilensteine" or "Level Milestones",
        survival = GetLocale() == "deDE" and "Spielzeit" or "Playtime",
        dungeon = GetLocale() == "deDE" and "Dungeon-Bosse" or "Dungeon Bosses",
        raid = GetLocale() == "deDE" and "Raid-Bosse" or "Raid Bosses",
        profession = GetLocale() == "deDE" and "Berufe" or "Professions",
        kills = GetLocale() == "deDE" and "Kill-Statistiken" or "Kill Stats",
        wealth = GetLocale() == "deDE" and "Gold-Meilensteine" or "Gold Milestones",
        meta = GetLocale() == "deDE" and "Meta-Erfolge" or "Meta Achievements",
        secret = GetLocale() == "deDE" and "Geheime Erfolge" or "Secret Achievements",
        support = GetLocale() == "deDE" and "Unterstützung" or "Support",
    }
    
    -- Kategorie-Buttons - NUR Icons in einer Reihe
    local categories = {
        {id = "level", icon = "Interface\\Icons\\Spell_Holy_WordFortitude", color = {0.2, 0.8, 0.2}},
        {id = "survival", icon = "Interface\\Icons\\Spell_Nature_TimeStop", color = {0.4, 0.6, 0.9}},
        {id = "dungeon", icon = "Interface\\Icons\\INV_Misc_Key_04", color = {0.5, 0.5, 0.9}},
        {id = "raid", icon = "Interface\\Icons\\INV_Misc_Head_Dragon_01", color = {0.9, 0.5, 0.2}},
        {id = "profession", icon = "Interface\\Icons\\Trade_BlackSmithing", color = {0.7, 0.6, 0.3}},
        {id = "kills", icon = "Interface\\Icons\\Ability_DualWield", color = {0.9, 0.3, 0.3}},
        {id = "wealth", icon = "Interface\\Icons\\INV_Misc_Coin_06", color = {0.9, 0.8, 0.2}},
        {id = "meta", icon = "Interface\\Icons\\INV_Misc_AhnQirajTrinket_03", color = {0.6, 0.4, 0.8}},
        {id = "secret", icon = "Interface\\Icons\\INV_Misc_QuestionMark", color = {0.8, 0.2, 0.8}},
        {id = "support", icon = "Interface\\Icons\\INV_ValentinesCard01", color = {0.9, 0.3, 0.5}},
    }
    
    f.categoryButtons = {}
    f.selectedCategory = "level"
    
    -- Icon-only Buttons in einer Reihe
    local iconSize = 32
    local iconSpacing = 6
    local totalWidth = #categories * iconSize + (#categories - 1) * iconSpacing
    local startX = -totalWidth / 2
    
    for i, cat in ipairs(categories) do
        local btn = CreateFrame("Button", nil, f, "BackdropTemplate")
        btn:SetSize(iconSize + 6, iconSize + 6)
        btn:SetPoint("TOP", startX + (i-1) * (iconSize + iconSpacing) + iconSize/2, -52)
        
        btn:SetBackdrop({
            bgFile = "Interface\\Buttons\\WHITE8X8",
            edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
            edgeSize = 8,
            insets = {left = 2, right = 2, top = 2, bottom = 2}
        })
        btn:SetBackdropColor(0.05, 0.05, 0.05, 0.9)
        btn:SetBackdropBorderColor(0.3, 0.3, 0.3, 1)
        
        local icon = btn:CreateTexture(nil, "ARTWORK")
        icon:SetSize(iconSize - 4, iconSize - 4)
        icon:SetPoint("CENTER", 0, 0)
        icon:SetTexture(cat.icon)
        btn.icon = icon
        btn.color = cat.color
        
        btn.categoryId = cat.id
        btn:SetScript("OnClick", function()
            f.selectedCategory = cat.id
            self:UpdateMilestones()
        end)
        
        btn:SetScript("OnEnter", function(self)
            self:SetBackdropBorderColor(unpack(cat.color))
            self.icon:SetVertexColor(1, 1, 1, 1)
            GameTooltip:SetOwner(self, "ANCHOR_BOTTOM")
            GameTooltip:SetText(catTooltips[cat.id], 1, 1, 1)
            GameTooltip:Show()
        end)
        btn:SetScript("OnLeave", function(self)
            if f.selectedCategory == self.categoryId then
                self:SetBackdropBorderColor(unpack(cat.color))
            else
                self:SetBackdropBorderColor(0.3, 0.3, 0.3, 1)
                self.icon:SetVertexColor(0.7, 0.7, 0.7, 1)
            end
            GameTooltip:Hide()
        end)
        
        -- Initial: Nicht ausgewählt = leicht ausgegraut
        if cat.id ~= "level" then
            icon:SetVertexColor(0.7, 0.7, 0.7, 1)
        else
            btn:SetBackdropBorderColor(unpack(cat.color))
        end
        
        f.categoryButtons[cat.id] = btn
    end
    
    local scroll = CreateFrame("ScrollFrame", nil, f, "UIPanelScrollFrameTemplate")
    scroll:SetPoint("TOPLEFT", 20, -95)
    scroll:SetPoint("BOTTOMRIGHT", -35, 20)
    
    local child = CreateFrame("Frame", nil, scroll)
    child:SetSize(scroll:GetWidth(), 1200)
    child:SetHeight(1200)  -- Wird durch content:SetHeight() dynamisch angepasst
    scroll:SetScrollChild(child)
    f.contentFrame = child
    
    self.achieveFrame = f
end

function UI:UpdateMilestones()
    local Milestones = GDL:GetModule("Milestones")
    if not Milestones or not self.achieveFrame then return end
    
    local f = self.achieveFrame
    local content = f.contentFrame
    local category = f.selectedCategory or "level"
    
    -- Charakter-Info aktualisieren
    local charKey = Milestones:GetCharacterKey()
    local charName = UnitName("player")
    local unlocked, total = Milestones:GetMilestoneProgress(charKey)
    local infoText = GetLocale() == "deDE" and "Meilensteine" or "Milestones"
    f.charInfo:SetText("|cff8A5E14" .. charName .. "|r |cff3a2a10- " .. unlocked .. "/" .. total .. " " .. infoText .. "|r")

    if f.supInfo then
        if GDL and GDL.IsSupporterActive and GDL:IsSupporterActive() then
            local untilStr = GDL:GetSupporterExpiry() or ""
                    local untilTS = 0
                    if untilStr ~= "" then
                        local y,m,d = untilStr:match("(%d+)-(%d+)-(%d+)")
                        if y then untilTS = time({year=tonumber(y),month=tonumber(m),day=tonumber(d)}) end
                    end
            f.supInfo:SetText("|cff1A5A24[OK] Supporter+|r " .. (GetLocale()=="deDE" and "aktiv bis " or "active until ") .. (GDL:GetSupporterExpiry() or "?"))
        else
            f.supInfo:SetText("|cff7A2C14Code eingeben >> |cff8A5E14<3 Support-Tab|r")
        end
    end

    
    -- Button-Farben aktualisieren (Icon-Only Design)
    for catId, btn in pairs(f.categoryButtons) do
        if catId == category then
            btn:SetBackdropBorderColor(unpack(btn.color))
            btn.icon:SetVertexColor(1, 1, 1, 1)
        else
            btn:SetBackdropBorderColor(0.3, 0.3, 0.3, 1)
            btn.icon:SetVertexColor(0.7, 0.7, 0.7, 1)
        end
    end
    
    -- Content leeren
    for _, c in ipairs({content:GetChildren()}) do c:Hide() end
    for _, r in ipairs({content:GetRegions()}) do r:Hide() end

    -- â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€
    -- GATE: Alle Tabs außer "support" sind ohne Code gesperrt.
    -- Support-Tab ist immer zugänglich (Code-Eingabe).
    -- â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€
    if category ~= "support" and not GDL:IsSupporterActive() then
        -- Lock-Screen anzeigen statt Content
        if not f.lockScreen then
            local ls = content:CreateFontString(nil, "OVERLAY")
            ls:SetFont("Fonts\\MORPHEUS.TTF", 16, "")
            ls:SetPoint("TOP", 0, -60)
            ls:SetJustifyH("CENTER")
            f.lockScreen = ls
            local ls2 = content:CreateFontString(nil, "OVERLAY")
            ls2:SetFont("Fonts\\FRIZQT__.TTF", 11, "")
            ls2:SetPoint("TOP", ls, "BOTTOM", 0, -20)
            ls2:SetJustifyH("CENTER")
            f.lockScreen2 = ls2
            local lsBtn = CreateFrame("Button", nil, content, "BackdropTemplate")
            lsBtn:SetSize(200, 30)
            lsBtn:SetPoint("TOP", ls2, "BOTTOM", 0, -20)
            lsBtn:SetBackdrop({bgFile="Interface\\Buttons\\WHITE8X8", edgeFile="Interface\\Tooltips\\UI-Tooltip-Border", edgeSize=12, insets={left=3,right=3,top=3,bottom=3}})
            lsBtn:SetBackdropColor(0.6, 0.3, 0.1, 0.8)
            lsBtn:SetBackdropBorderColor(1, 0.7, 0.2, 1)
            local lsBtnTxt = lsBtn:CreateFontString(nil, "OVERLAY")
            lsBtnTxt:SetFont("Fonts\\FRIZQT__.TTF", 12, "")
            lsBtnTxt:SetPoint("CENTER")
            lsBtnTxt:SetText(GetLocale()=="deDE" and "Code eingeben >>" or "Enter Code >>")
            lsBtn:SetScript("OnClick", function()
                f.selectedCategory = "support"
                self:UpdateMilestones()
            end)
            lsBtn:SetScript("OnEnter", function(s) s:SetBackdropColor(0.8, 0.4, 0.1, 0.9) end)
            lsBtn:SetScript("OnLeave", function(s) s:SetBackdropColor(0.6, 0.3, 0.1, 0.8) end)
            f.lockScreenBtn = lsBtn
        end
        local isDE = GetLocale() == "deDE"
        f.lockScreen:SetText("|cff7A2C14|TInterface\\Buttons\\LockButton-Locked-Up:14:14:0:-1|t " .. (isDE and "Supporter-Feature" or "Supporter Feature") .. "|r")
        f.lockScreen2:SetText("|cff5E3E1C" .. (isDE
            and "Gib deinen Patreon-Code ein um\nMeilensteine & Titel freizuschalten."
            or  "Enter your Patreon code to unlock\nMilestones & Titles.") .. "|r")
        f.lockScreen:Show()
        f.lockScreen2:Show()
        f.lockScreenBtn:Show()
        content:SetHeight(200)
        return
    end
    -- Lock-Screen verstecken wenn Supporter aktiv
    if f.lockScreen then f.lockScreen:Hide() end
    if f.lockScreen2 then f.lockScreen2:Hide() end
    if f.lockScreenBtn then f.lockScreenBtn:Hide() end

-- ==============================================================
-- SUPPORT-SEITE - gleicher Stil wie andere Meilenstein-Tabs
-- ==============================================================
if category == "support" then
    local isDE = GetLocale() == "deDE"

    if not f.supportPage then
        local p = CreateFrame("Frame", nil, content)
        p:SetPoint("TOPLEFT", 0, 0)
        p:SetPoint("BOTTOMRIGHT", 0, 0)
        f.supportPage = p

        -- â”€â”€ Abschnitts-Trennlinie (wie im Meilenstein-Tab-Header) â”€â”€
        local function MakeDivider(parent, yOff)
            local d = parent:CreateTexture(nil, "ARTWORK")
            d:SetTexture("Interface\\Common\\UI-TooltipDivider-Transparent")
            d:SetSize(360, 8)
            d:SetPoint("TOPLEFT", 10, yOff)
            d:SetAlpha(0.6)
            return d
        end

        -- â”€â”€ Abschnitts-Header (wie Kategorie-Header im Meilenstein-Tab) â”€â”€
        local function MakeSectionHeader(parent, text, yOff)
            local lbl = parent:CreateFontString(nil, "OVERLAY")
            lbl:SetFont("Fonts\\FRIZQT__.TTF", 13, "")
            lbl:SetPoint("TOPLEFT", 10, yOff)
            lbl:SetText("|cff2A1606" .. text .. "|r")
            return lbl
        end

        -- â”€â”€ Fließtext (wie Meilenstein-Beschreibungen) â”€â”€
        local function MakeBodyText(parent, text, yOff, rightPad)
            rightPad = rightPad or 18
            local t = parent:CreateFontString(nil, "OVERLAY")
            t:SetFont("Fonts\\FRIZQT__.TTF", 11, "")
            t:SetPoint("TOPLEFT", 14, yOff)
            t:SetPoint("TOPRIGHT", -rightPad, yOff)
            t:SetJustifyH("LEFT")
            t:SetJustifyV("TOP")
            t:SetTextColor(0.25, 0.18, 0.12)
            t:SetText(text)
            return t
        end

        -- â”€â”€ Status-Box (wie ein freigeschalteter Meilenstein-Row) â”€â”€
        local statusBox = CreateFrame("Frame", nil, p, "BackdropTemplate")
        statusBox:SetSize(362, 44)
        statusBox:SetPoint("TOPLEFT", 8, -8)
        statusBox:SetBackdrop({
            bgFile   = "Interface\\Buttons\\WHITE8X8",
            edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
            edgeSize = 10,
            insets   = {left=3, right=3, top=3, bottom=3},
        })
        statusBox:SetBackdropColor(0.12, 0.09, 0.06, 0.85)
        statusBox:SetBackdropBorderColor(0.55, 0.40, 0.18, 0.9)

        local statusIcon = statusBox:CreateTexture(nil, "ARTWORK")
        statusIcon:SetSize(30, 30)
        statusIcon:SetPoint("LEFT", 8, 0)
        statusIcon:SetTexture("Interface\\Icons\\INV_ValentinesCard01")
        p.statusIcon = statusIcon

        local statusText = statusBox:CreateFontString(nil, "OVERLAY")
        statusText:SetFont("Fonts\\FRIZQT__.TTF", 12, "")
        statusText:SetPoint("LEFT", statusIcon, "RIGHT", 8, 2)
        statusText:SetPoint("RIGHT", statusBox, "RIGHT", -8, 0)
        statusText:SetJustifyH("LEFT")
        statusText:SetTextColor(0.25, 0.18, 0.12)
        p.status = statusText

        -- â”€â”€ Was ist Supporter+? â”€â”€
        MakeDivider(p, -58)
        MakeSectionHeader(p, isDE and "Was ist Supporter+?" or "What is Supporter+?", -70)
        MakeBodyText(p, isDE
            and "Dieses Addon ist und bleibt komplett kostenlos.\n\nAls freiwillige Unterstützung kannst du über Patreon einen Monatsbeitrag leisten. Als Dankeschön schaltest du rein kosmetische Extras frei: eigene Titel auf Nameplates & Tooltips sowie das vollständige Meilenstein-System.\n\nKeine Gameplay-Vorteile. Kein Pay-to-Win."
            or  "This addon is and stays completely free.\n\nAs voluntary support you can contribute monthly via Patreon. As a thank-you, you unlock purely cosmetic extras: custom titles on nameplates & tooltips and the full milestone system.\n\nNo gameplay advantages. No pay-to-win.",
            -84)
        p.body = MakeBodyText(p, "", -84)  -- reference kept for compat

        -- â”€â”€ Code einlösen â”€â”€
        MakeDivider(p, -220)
        MakeSectionHeader(p, isDE and "Supporter-Code einlösen" or "Redeem Supporter Code", -232)

        -- Discord-ID Label + EditBox (ersetzt Patreon-Name)
        local discordLbl = p:CreateFontString(nil, "OVERLAY")
        discordLbl:SetFont("Fonts\\FRIZQT__.TTF", 11, "")
        discordLbl:SetPoint("TOPLEFT", 14, -252)
        discordLbl:SetTextColor(0.30, 0.22, 0.14)
        discordLbl:SetText(isDE and "Discord-ID  |cff5E3E1C(aus der Bot-DM)|r" or "Discord ID  |cff5E3E1C(from Bot DM)|r")

        local discordEB = CreateFrame("EditBox", nil, p, "InputBoxTemplate")
        discordEB:SetAutoFocus(false)
        discordEB:SetSize(340, 22)
        discordEB:SetPoint("TOPLEFT", 14, -268)
        discordEB:SetMaxLetters(20)
        discordEB:SetNumeric(true)
        discordEB:SetScript("OnEscapePressed", function(s) s:ClearFocus() end)
        discordEB:SetScript("OnEnterPressed", function(s)
            local id = s:GetText()
            if id and id ~= "" then
                GuildDeathLogDB.discordId = id
                GDL:Print("|cff00FF00Discord-ID gespeichert:|r " .. id)
            end
            s:ClearFocus()
        end)
        discordEB:SetScript("OnTextChanged", function(s)
            local id = s:GetText()
            if id and id ~= "" then
                GuildDeathLogDB.discordId = id
            end
        end)
        -- Gespeicherte ID laden
        local savedId = GuildDeathLogDB and GuildDeathLogDB.discordId or ""
        if savedId ~= "" then
            discordEB:SetText(savedId)
        end
        p.discordEdit = discordEB

        -- Status-Indikator Discord-ID (klein, diskret)
        local discordStatus = p:CreateFontString(nil, "OVERLAY")
        discordStatus:SetFont("Fonts\\FRIZQT__.TTF", 9, "")
        discordStatus:SetPoint("TOPLEFT", 14, -293)
        if savedId ~= "" then
            discordStatus:SetText("|cff00CC44[OK] ID gespeichert|r")
        else
            discordStatus:SetText("|cff7A2C14Bitte Discord-ID eingeben|r")
        end
        p.discordStatus = discordStatus

        -- Code Label + EditBox (größer, prominent)
        local codeLbl = p:CreateFontString(nil, "OVERLAY")
        codeLbl:SetFont("Fonts\\FRIZQT__.TTF", 12, "")
        codeLbl:SetPoint("TOPLEFT", 14, -300)
        codeLbl:SetTextColor(0.541, 0.369, 0.078)
        codeLbl:SetText(isDE and "Supporter-Code  |cff5E3E1C(GDL-YYYYMM-XXXXXXXX-CC)|r" or "Supporter Code  |cff5E3E1C(GDL-YYYYMM-XXXXXXXX-CC)|r")

        local codeEB = CreateFrame("EditBox", nil, p, "InputBoxTemplate")
        codeEB:SetAutoFocus(false)
        codeEB:SetSize(340, 22)
        codeEB:SetPoint("TOPLEFT", 14, -318)
        codeEB:SetMaxLetters(64)
        codeEB:SetScript("OnEscapePressed", function(s) s:ClearFocus() end)
        codeEB:SetScript("OnEnterPressed", function(s) s:ClearFocus() end)
        p.codeEdit = codeEB

        -- Einlösen-Button (wie Milestone-Row, goldener Rahmen)
        local redeemBtn = CreateFrame("Button", nil, p, "BackdropTemplate")
        redeemBtn:SetSize(150, 28)
        redeemBtn:SetPoint("TOPLEFT", 14, -350)
        redeemBtn:SetBackdrop({
            bgFile   = "Interface\\Buttons\\WHITE8X8",
            edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
            edgeSize = 12,
            insets   = {left=3, right=3, top=3, bottom=3},
        })
        redeemBtn:SetBackdropColor(0.18, 0.14, 0.08, 0.85)
        redeemBtn:SetBackdropBorderColor(0.75, 0.55, 0.18, 1.0)
        local rtxt = redeemBtn:CreateFontString(nil, "OVERLAY")
        rtxt:SetFont("Fonts\\FRIZQT__.TTF", 12, "")
        rtxt:SetPoint("CENTER", 0, 0)
        rtxt:SetText(isDE and "|cff8A5E14Einlösen|r" or "|cff8A5E14Redeem|r")
        redeemBtn:SetScript("OnEnter", function(self)
            self:SetBackdropColor(0.28, 0.22, 0.12, 0.95)
            self:SetBackdropBorderColor(1.0, 0.75, 0.25, 1.0)
        end)
        redeemBtn:SetScript("OnLeave", function(self)
            self:SetBackdropColor(0.18, 0.14, 0.08, 0.85)
            self:SetBackdropBorderColor(0.75, 0.55, 0.18, 1.0)
        end)
        redeemBtn:SetScript("OnClick", function()
            local code = p.codeEdit and p.codeEdit:GetText() or ""
            code = strtrim(code)
            if code == "" then
                GDL:Print(isDE and "Bitte einen Code eingeben." or "Please enter a code.")
                p.codeEdit:SetFocus()
                return
            end
            if GDL and GDL.RedeemSupporterCode then
                local success = GDL:RedeemSupporterCode(code)
                if success then
                    local untilStr = GDL:GetSupporterExpiry() or ""
                    local untilTS = 0
                    if untilStr ~= "" then
                        local y,m,d = untilStr:match("(%d+)-(%d+)-(%d+)")
                        if y then untilTS = time({year=tonumber(y),month=tonumber(m),day=tonumber(d)}) end
                    end
                    if p.status then
                        p.status:SetTextColor(0.2, 0.65, 0.2)
                        p.status:SetText(
                            "|cff1A5A24" .. (isDE and "[OK] Supporter+ aktiv bis " or "[OK] Supporter+ active until ")
                            .. "|r" .. (untilTS > 0 and date("%d.%m.%Y", untilTS) or (untilStr ~= "" and untilStr or "?"))
                            .. "  |cff8A5E14" .. (isDE and "Titel & Meilensteine freigeschaltet!" or "Titles & Milestones unlocked!") .. "|r")
                    end
                    if statusBox then
                        statusBox:SetBackdropBorderColor(0.2, 0.7, 0.2, 1.0)
                        statusBox:SetBackdropColor(0.05, 0.18, 0.05, 0.85)
                    end
                    GDL:Print(isDE
                        and "|cff1A5A24Danke! Supporter+ aktiviert.|r Meilensteine & Titel freigeschaltet!"
                        or  "|cff1A5A24Thank you! Supporter+ activated.|r Milestones & Titles unlocked!")
                    -- Level-Tab öffnen nach Erfolg
                    f.selectedCategory = "level"
                    for catId, btn in pairs(f.categoryButtons) do
                        if catId == "level" then
                            btn:SetBackdropBorderColor(unpack(btn.color))
                            btn.icon:SetVertexColor(1, 1, 1, 1)
                        else
                            btn:SetBackdropBorderColor(0.3, 0.3, 0.3, 1)
                            btn.icon:SetVertexColor(0.7, 0.7, 0.7, 1)
                        end
                    end
                else
                    GDL:Print(isDE
                        and "|cffFF4444Ungültiger Code.|r Format: GDL-202602-XXXXXXXX-CC"
                        or  "|cffFF4444Invalid code.|r Format: GDL-202602-XXXXXXXX-CC")
                end
            end
            UI:UpdateMilestones()
        end)
        p.redeemBtn = redeemBtn

        -- Patreon-Link Button (dezenter, lila Akzent wie auf den anderen Tabs)
        local patBtn = CreateFrame("Button", nil, p, "BackdropTemplate")
        patBtn:SetSize(150, 28)
        patBtn:SetPoint("LEFT", redeemBtn, "RIGHT", 10, 0)
        patBtn:SetBackdrop({
            bgFile   = "Interface\\Buttons\\WHITE8X8",
            edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
            edgeSize = 12,
            insets   = {left=3, right=3, top=3, bottom=3},
        })
        patBtn:SetBackdropColor(0.14, 0.10, 0.18, 0.7)
        patBtn:SetBackdropBorderColor(0.50, 0.35, 0.70, 0.9)
        local ptxt = patBtn:CreateFontString(nil, "OVERLAY")
        ptxt:SetFont("Fonts\\FRIZQT__.TTF", 12, "")
        ptxt:SetPoint("CENTER", 0, 0)
        ptxt:SetText("|cff5C3A54<3 Patreon|r")
        patBtn:SetScript("OnEnter", function(self)
            self:SetBackdropColor(0.22, 0.16, 0.28, 0.9)
            self:SetBackdropBorderColor(0.70, 0.50, 0.90, 1.0)
        end)
        patBtn:SetScript("OnLeave", function(self)
            self:SetBackdropColor(0.14, 0.10, 0.18, 0.7)
            self:SetBackdropBorderColor(0.50, 0.35, 0.70, 0.9)
        end)
        patBtn:SetScript("OnClick", function()
            GDL:Print((isDE and "Patreon: " or "Patreon: ") .. "patreon.com/PsYke86")
        end)
        p.patBtn = patBtn

        -- Hinweis-Text (klein, wie Meilenstein-Unlock-Datum)
        local note = p:CreateFontString(nil, "OVERLAY")
        note:SetFont("Fonts\\FRIZQT__.TTF", 10, "")
        note:SetPoint("TOPLEFT", 14, -388)
        note:SetPoint("TOPRIGHT", -14, -388)
        note:SetJustifyH("LEFT")
        note:SetTextColor(0.369, 0.243, 0.110)
        note:SetText(isDE
            and "|cff5E3E1CHinweis:|r Der Code wird nach Patreon-Support per Discord zugesendet."
            or  "|cff5E3E1CNote:|r The code is sent via Discord after Patreon support.")
        p.note = note
    end -- end if not f.supportPage

    -- â”€â”€ Dynamische Updates bei jedem Öffnen â”€â”€
    local isDE = GetLocale() == "deDE"

    -- Patreon-Name laden/speichern
    if GuildDeathLogDB and GuildDeathLogDB.settings and f.supportPage.patreonEdit then
        if not f.supportPage.patreonEdit._loaded then
            f.supportPage.patreonEdit:SetText(GuildDeathLogDB.settings.patreonName or "")
            f.supportPage.patreonEdit._loaded = true
        end
        f.supportPage.patreonEdit:SetScript("OnTextChanged", function(self)
            GuildDeathLogDB.settings.patreonName = self:GetText() or ""
        end)
    end

    -- Status-Feld aktualisieren
    if GDL and GDL.IsSupporterActive and GDL:IsSupporterActive() then
        local untilStr = GDL:GetSupporterExpiry() or ""
        local untilTS = 0
        if untilStr ~= "" then
            local y,m,d = untilStr:match("(%d+)-(%d+)-(%d+)")
            if y then untilTS = time({year=tonumber(y),month=tonumber(m),day=tonumber(d)}) end
        end
        f.supportPage.status:SetTextColor(0.2, 0.65, 0.2)
        f.supportPage.status:SetText(
            "|cff1A5A24" .. (isDE and "[OK] Supporter+ aktiv bis " or "[OK] Supporter+ active until ")
            .. "|r" .. (untilTS > 0 and date("%d.%m.%Y", untilTS) or (untilStr ~= "" and untilStr or "?"))
            .. "  |cff8A5E14" .. (isDE and "Titel & Meilensteine freigeschaltet!" or "Titles & Milestones unlocked!") .. "|r")
    else
        f.supportPage.status:SetTextColor(0.369, 0.243, 0.110)
        f.supportPage.status:SetText(
            isDE and "|cff5E3E1CCode eingeben um Titel & Meilensteine freizuschalten.|r"
                  or "|cff5E3E1CEnter code to unlock Titles & Milestones.|r")
        -- Sofort Code-Feld fokussieren
        C_Timer.After(0.05, function()
            if f.supportPage.codeEdit then f.supportPage.codeEdit:SetFocus() end
        end)
    end

    f.supportPage:Show()
    content:SetHeight(420)
    return
end

    local milestones = Milestones:GetMilestonesByCategory(category)
    local charMilestones = Milestones:GetCharacterMilestones(charKey)
    local y = 0
    
    -- Kategorie-Header
    local catUnlocked, catTotal = Milestones:GetCategoryProgress(category, charKey)
    local header = content:CreateFontString(nil, "OVERLAY")
    header:SetFont("Fonts\\FRIZQT__.TTF", 13, "")
    header:SetPoint("TOPLEFT", 5, y)
    
    local categoryNames = {
        level = GetLocale() == "deDE" and "Level-Meilensteine" or "Level Milestones",
        survival = GetLocale() == "deDE" and "Spielzeit-Meilensteine" or "Playtime Milestones",
        dungeon = GetLocale() == "deDE" and "Dungeon-Bosse" or "Dungeon Bosses",
        raid = GetLocale() == "deDE" and "Raid-Bosse" or "Raid Bosses",
        profession = GetLocale() == "deDE" and "Berufe-Meilensteine" or "Profession Milestones",
        kills = GetLocale() == "deDE" and "Kill-Meilensteine" or "Kill Milestones",
        wealth = GetLocale() == "deDE" and "Gold-Meilensteine" or "Gold Milestones",
        meta = GetLocale() == "deDE" and "Meta-Erfolge" or "Meta Achievements",
        secret = GetLocale() == "deDE" and "Geheime Meilensteine" or "Secret Milestones",
        support = GetLocale() == "deDE" and "Unterstützen" or "Support",
    }
    header:SetText("|cff2A1606" .. (categoryNames[category] or category) .. "|r |cff5E3E1C- " .. catUnlocked .. "/" .. catTotal .. "|r")
    y = y - 25
    
    -- Sortiere nach Threshold
    table.sort(milestones, function(a, b)
        return (a.threshold or 0) < (b.threshold or 0)
    end)
    
    for _, m in ipairs(milestones) do
        local isUnlocked = charMilestones[m.id] ~= nil
        local unlockData = charMilestones[m.id]
        
        -- Kill-Meilensteine brauchen mehr Platz für Progress-Leiste
        -- Konstante Zeilenhöhe für alle
        local rowHeight = 55
        
        local row = CreateFrame("Frame", nil, content, "BackdropTemplate")
        row:SetSize(380, rowHeight)
        row:SetPoint("TOPLEFT", 0, y)
        
        row:SetBackdrop({
            bgFile = "Interface\\Buttons\\WHITE8X8",
            edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
            edgeSize = 10,
            insets = {left = 2, right = 2, top = 2, bottom = 2}
        })
        
        -- Farbe basierend auf Kategorie und Status
        local catColors = {
            level = {0.2, 0.6, 0.2},
            survival = {0.3, 0.5, 0.7},
            dungeon = {0.4, 0.4, 0.8},
            raid = {0.8, 0.4, 0.2},
            profession = {0.6, 0.5, 0.2},
            kills = {0.7, 0.2, 0.2},
            wealth = {0.8, 0.7, 0.2},
            meta = {0.5, 0.3, 0.7},
            secret = {0.6, 0.1, 0.6},
        }
        local color = catColors[category] or {0.5, 0.5, 0.5}
        
        if isUnlocked then
            row:SetBackdropColor(color[1] * 0.3, color[2] * 0.3, color[3] * 0.3, 0.8)
            row:SetBackdropBorderColor(color[1], color[2], color[3], 0.9)
        else
            row:SetBackdropColor(0.08, 0.08, 0.08, 0.5)
            row:SetBackdropBorderColor(0.2, 0.2, 0.2, 0.5)
        end
        
        -- Icon
        local icon = row:CreateTexture(nil, "ARTWORK")
        icon:SetSize(42, 42)
        icon:SetPoint("LEFT", 6, 0)
        
        -- Geheime Meilensteine: Fragezeichen-Icon wenn nicht freigeschaltet
        if m.secret and not isUnlocked then
            icon:SetTexture("Interface\\Icons\\INV_Misc_QuestionMark")
        else
            icon:SetTexture(m.icon)
        end
        
        if not isUnlocked then 
            icon:SetDesaturated(true) 
            icon:SetAlpha(0.65) 
        end
        
        -- Name
        local name = row:CreateFontString(nil, "OVERLAY")
        name:SetFont("Fonts\\FRIZQT__.TTF", 12, "")
        name:SetPoint("TOPLEFT", icon, "TOPRIGHT", 10, -5)
        name:SetWidth(280)
        if isUnlocked then
            name:SetText("|cff1A5A24[+]|r " .. m.name)
        elseif m.secret then
            name:SetText("|cffA8761E" .. m.name .. "|r")
        else
            name:SetText("|cff666666" .. m.name .. "|r")
        end
        
        -- Beschreibung
        local desc = row:CreateFontString(nil, "OVERLAY")
        desc:SetFont("Fonts\\FRIZQT__.TTF", 9, "")
        desc:SetPoint("TOPLEFT", name, "BOTTOMLEFT", 0, -3)
        desc:SetWidth(220)
        
        local descText = m.desc
        if m.secret and not isUnlocked then
            descText = "???"
        end
        desc:SetText("|cff5E3E1C" .. descText .. "|r")
        
        -- Fortschritts-Anzeige (Zahlen, keine Balken!) für alle nicht-freigeschalteten mit Threshold
        if not isUnlocked and m.threshold then
            local current = 0
            local threshold = m.threshold
            
            if m.type == "kills" and m.creatureType then
                -- Kreaturtyp-Kills
                local KillStats = GDL:GetModule("KillStats")
                if KillStats then
                    current = KillStats:GetKillCount(m.creatureType)
                end
            elseif m.type == "total_kills" then
                -- Total Kills
                local KillStats = GDL:GetModule("KillStats")
                if KillStats then
                    current = KillStats:GetTotalKills()
                end
            elseif m.type == "gold" then
                -- Gold (in Kupfer)
                current = GetMoney()
            elseif m.type == "playtime" then
                -- Spielzeit (wird separat getrackt)
                local charData = Milestones:GetCharacterData(charKey)
                current = charData.stats and charData.stats.totalPlaytime or 0
            elseif m.type == "level" then
                -- Level
                current = UnitLevel("player")
            elseif m.type == "profession" or m.type == "fishing" or m.type == "cooking" or m.type == "firstaid" then
                -- Berufe über Classic-kompatible API
                local profSkills = Milestones:GetProfessionSkills()
                if m.type == "profession" then
                    current = profSkills.maxMain or 0
                elseif m.type == "fishing" then
                    current = profSkills.fishing or 0
                elseif m.type == "cooking" then
                    current = profSkills.cooking or 0
                elseif m.type == "firstaid" then
                    current = profSkills.firstaid or 0
                end
            end
            
            -- Prüfe ob Meilenstein erreicht aber noch nicht freigeschaltet
            if current >= threshold then
                local charName = UnitName("player")
                local level = UnitLevel("player")
                Milestones:UnlockMilestone(m.id, charKey, charName, level, true)
                
                -- Aktualisiere Anzeige
                row:SetBackdropColor(color[1] * 0.3, color[2] * 0.3, color[3] * 0.3, 0.8)
                row:SetBackdropBorderColor(color[1], color[2], color[3], 0.9)
                icon:SetDesaturated(false)
                icon:SetAlpha(1)
                name:SetText("|cff1A5A24[+]|r " .. m.name)
            else
                -- Zeige Fortschritt als Zahlen
                local progressText = row:CreateFontString(nil, "OVERLAY")
                progressText:SetFont("Fonts\\FRIZQT__.TTF", 10, "")
                progressText:SetPoint("BOTTOMLEFT", icon, "BOTTOMRIGHT", 10, 3)
                
                -- Formatiere die Zahlen je nach Typ
                local currentStr, thresholdStr
                if m.type == "gold" then
                    -- Gold: Kupfer zu Gold konvertieren
                    currentStr = math.floor(current / 10000)
                    thresholdStr = math.floor(threshold / 10000) .. "g"
                    currentStr = currentStr .. "g"
                elseif m.type == "playtime" then
                    -- Spielzeit: Sekunden zu Stunden
                    currentStr = math.floor(current / 3600) .. "h"
                    thresholdStr = math.floor(threshold / 3600) .. "h"
                else
                    currentStr = current
                    thresholdStr = threshold
                end
                
                progressText:SetText("|cff2A1606" .. currentStr .. "/" .. thresholdStr .. "|r")
            end
        end
        
        -- Datum wenn freigeschaltet
        if isUnlocked and unlockData then
            local dateText = row:CreateFontString(nil, "OVERLAY")
            dateText:SetFont("Fonts\\FRIZQT__.TTF", 9, "")
            dateText:SetPoint("BOTTOMRIGHT", -10, 8)
            local dateStr = unlockData.timestamp and date("%d.%m.%Y", unlockData.timestamp) or "?"
            dateText:SetText("|cff00AA00" .. dateStr .. "|r")
            
            if unlockData.level then
                local lvlText = row:CreateFontString(nil, "OVERLAY")
                lvlText:SetFont("Fonts\\FRIZQT__.TTF", 9, "")
                lvlText:SetPoint("TOPRIGHT", -10, -8)
                lvlText:SetText("|cff8A5E14Lvl " .. unlockData.level .. "|r")
            end
        end
        
        y = y - (rowHeight + 5)
    end
    
    content:SetHeight(math.abs(y) + 30)
end

-- â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•
-- TITLES WINDOW - Titel-Auswahl Fenster
-- â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•â•

function UI:ToggleTitles()
    if not GDL:RequireSupporter() then return end
    if not self.titlesFrame then
        self:CreateTitlesWindow()
    end
    
    if self.titlesFrame:IsShown() then
        self.titlesFrame:Hide()
    else
        self.titlesFrame:Show()
        self:UpdateTitles()
    end
end

function UI:CreateTitlesWindow()
    local f = CreateFrame("Frame", "GDLTitles", UIParent, "BackdropTemplate")
    f:SetSize(420, 500)
    f:SetPoint("CENTER", UIParent, "CENTER", 550, 150)
    f:SetFrameStrata("HIGH")
    f:SetFrameLevel(110)
    f:SetMovable(true)
    f:EnableMouse(true)
    f:RegisterForDrag("LeftButton")
    f:SetScript("OnDragStart", f.StartMoving)
    f:SetScript("OnDragStop", f.StopMovingOrSizing)
    f:SetScript("OnMouseDown", function(self) self:Raise() end)
    
    f:SetBackdrop({
        bgFile = "Interface\\ACHIEVEMENTFRAME\\UI-Achievement-Parchment-Horizontal",
        edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Gold-Border",
        edgeSize = 26,
        insets = {left = 5, right = 5, top = 5, bottom = 5}
    })
    
    local close = CreateFrame("Button", nil, f, "UIPanelCloseButton")
    close:SetPoint("TOPRIGHT", -5, -5)
    
    -- Titel-Icon oben
    local headerIcon = f:CreateTexture(nil, "ARTWORK")
    headerIcon:SetSize(40, 40)
    headerIcon:SetPoint("TOP", 0, -15)
    headerIcon:SetTexture("Interface\\Icons\\INV_Crown_01")
    
    local title = f:CreateFontString(nil, "OVERLAY")
    title:SetFont("Fonts\\MORPHEUS.TTF", 18, "")
    title:SetPoint("TOP", headerIcon, "BOTTOM", 0, -5)
    title:SetText("|cff2A1606Titel / Titles|r")
    
    -- Aktueller Titel Anzeige
    local currentFrame = CreateFrame("Frame", nil, f, "BackdropTemplate")
    currentFrame:SetSize(380, 60)
    currentFrame:SetPoint("TOP", title, "BOTTOM", 0, -15)
    currentFrame:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8X8",
        edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
        edgeSize = 12,
        insets = {left = 3, right = 3, top = 3, bottom = 3}
    })
    currentFrame:SetBackdropColor(0.1, 0.08, 0.05, 0.9)
    currentFrame:SetBackdropBorderColor(0.8, 0.6, 0.2, 1)
    
    local currentLabel = currentFrame:CreateFontString(nil, "OVERLAY")
    currentLabel:SetFont("Fonts\\FRIZQT__.TTF", 10, "")
    currentLabel:SetPoint("TOPLEFT", 10, -8)
    currentLabel:SetText("|cff8A5E14" .. GDL:T("Aktueller Titel:") .. "|r")
    
    local currentTitle = currentFrame:CreateFontString(nil, "OVERLAY")
    currentTitle:SetFont("Fonts\\MORPHEUS.TTF", 16, "")
    currentTitle:SetPoint("LEFT", 50, -5)
    -- Initialwert: leer bis UpdateTitles() aufgerufen wird - kein weißes Blank
    currentTitle:SetText("|cff5E3E1C-|r")
    f.currentTitleText = currentTitle
    
    local currentIcon = currentFrame:CreateTexture(nil, "ARTWORK")
    currentIcon:SetSize(32, 32)
    currentIcon:SetPoint("LEFT", 10, -5)
    f.currentTitleIcon = currentIcon
    
    -- Progress-Anzeige
    local progressText = f:CreateFontString(nil, "OVERLAY")
    progressText:SetFont("Fonts\\FRIZQT__.TTF", 11, "")
    progressText:SetPoint("TOP", currentFrame, "BOTTOM", 0, -10)
    f.progressText = progressText
    
    -- Info-Text
    local infoText = f:CreateFontString(nil, "OVERLAY")
    infoText:SetFont("Fonts\\FRIZQT__.TTF", 9, "")
    infoText:SetPoint("TOP", progressText, "BOTTOM", 0, -5)
    local titlesInfo = GetLocale() == "deDE" and "Titel sind für alle Gildenmitglieder mit dem Addon sichtbar!" or "Titles are visible to all guild members with the addon!"
    infoText:SetText("|cff5E3E1C" .. titlesInfo .. "|r")
    
    -- Scroll-Bereich für Titel-Liste
    local scroll = CreateFrame("ScrollFrame", nil, f, "UIPanelScrollFrameTemplate")
    scroll:SetPoint("TOPLEFT", 15, -175)
    scroll:SetPoint("BOTTOMRIGHT", -35, 15)
    
    local child = CreateFrame("Frame", nil, scroll)
    child:SetSize(scroll:GetWidth(), 600)
    scroll:SetScrollChild(child)
    f.contentFrame = child
    
    f:Hide()
    self.titlesFrame = f
end

function UI:UpdateTitles()
    local Titles = GDL:GetModule("Titles")
    if not Titles or not self.titlesFrame then return end
    
    local f = self.titlesFrame
    local content = f.contentFrame
    local isDE = GetLocale() == "deDE"
    
    -- Content leeren
    for _, child in ipairs({content:GetChildren()}) do
        child:Hide()
    end
    
    -- Aktueller Titel - sauberer Fallback, kein leerer String
    local selected = Titles:GetSelectedTitle()
    if selected then
        local color = selected.color or {1, 1, 1}
        local isFemale = (Titles.IsPlayerFemale ~= nil) and Titles:IsPlayerFemale() or false
        local titleName = (isFemale and selected.nameFemale) or selected.name
        f.currentTitleText:SetText(string.format("|cff%02x%02x%02x<%s>|r",
            color[1]*255, color[2]*255, color[3]*255, titleName))
        f.currentTitleIcon:SetTexture(selected.icon)
        f.currentTitleIcon:Show()
    else
        -- Kein Titel vorhanden - neutraler Hinweis, kein leerer String
        local noTitle = isDE and "|cff5E3E1C- kein Titel ausgewählt -|r" or "|cff5E3E1C- no title selected -|r"
        f.currentTitleText:SetText(noTitle)
        f.currentTitleIcon:SetTexture("")
    end
    
    -- Progress
    local unlocked, total = Titles:GetStats()
    f.progressText:SetText("|cff8A5E14" .. unlocked .. "|r / |cff5E3E1C" .. total .. " " .. GDL:T("Titel freigeschaltet") .. "|r")
    
    -- Titel-Liste
    local allTitles = Titles:GetUnlockedTitles()
    local y = 0
    
    for _, t in ipairs(allTitles) do
        local isSelected = selected and selected.id == t.id
        
        local row = CreateFrame("Button", nil, content, "BackdropTemplate")
        row:SetSize(350, 55)
        row:SetPoint("TOPLEFT", 0, y)
        
        row:SetBackdrop({
            bgFile = "Interface\\Buttons\\WHITE8X8",
            edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
            edgeSize = 10,
            insets = {left = 2, right = 2, top = 2, bottom = 2}
        })
        
        local color = t.color or {0.5, 0.5, 0.5}
        
        if isSelected then
            row:SetBackdropColor(color[1] * 0.4, color[2] * 0.4, color[3] * 0.4, 0.9)
            row:SetBackdropBorderColor(0, 1, 0, 1)  -- Grüner Rahmen
        else
            row:SetBackdropColor(0.1, 0.08, 0.05, 0.7)
            row:SetBackdropBorderColor(color[1] * 0.6, color[2] * 0.6, color[3] * 0.6, 0.8)
        end
        
        -- Icon
        local icon = row:CreateTexture(nil, "ARTWORK")
        icon:SetSize(40, 40)
        icon:SetPoint("LEFT", 8, 0)
        icon:SetTexture(t.icon)
        
        -- Titel-Name
        local nameText = row:CreateFontString(nil, "OVERLAY")
        nameText:SetFont("Fonts\\MORPHEUS.TTF", 14, "")
        nameText:SetPoint("TOPLEFT", icon, "TOPRIGHT", 10, -5)
        nameText:SetText(string.format("|cff%02x%02x%02x<%s>|r", 
            color[1]*255, color[2]*255, color[3]*255, t.name))
        
        -- Beschreibung
        local descText = row:CreateFontString(nil, "OVERLAY")
        descText:SetFont("Fonts\\FRIZQT__.TTF", 9, "")
        descText:SetPoint("TOPLEFT", nameText, "BOTTOMLEFT", 0, -3)
        descText:SetText("|cff5E3E1C" .. t.desc .. "|r")
        
        -- Aktiv-Marker
        if isSelected then
            local marker = row:CreateFontString(nil, "OVERLAY")
            marker:SetFont("Fonts\\FRIZQT__.TTF", 10, "")
            marker:SetPoint("RIGHT", -10, 0)
            marker:SetText("|cff1A5A24< AKTIV|r")
        end
        
        -- Hover-Effekte
        row:SetScript("OnEnter", function(self)
            if not isSelected then
                self:SetBackdropColor(color[1] * 0.3, color[2] * 0.3, color[3] * 0.3, 0.9)
            end
            
            local isDE = GetLocale() == "deDE"
            GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
            GameTooltip:ClearLines()
            GameTooltip:AddLine(t.name, color[1], color[2], color[3])
            GameTooltip:AddLine(t.desc, 1, 1, 1, true)
            GameTooltip:AddLine(" ")
            if t.requirement then
                local reqLabel = isDE and "Benoetigt: " or "Requires: "
                GameTooltip:AddLine(reqLabel .. t.requirement, 0.7, 0.7, 0.7)
            end
            local prioLabel = isDE and "Prioritaet: " or "Priority: "
            GameTooltip:AddLine(prioLabel .. (t.priority or 0), 0.5, 0.5, 0.5)
            GameTooltip:AddLine(" ")
            local clickLabel = isDE and "Klicken zum Auswaehlen" or "Click to select"
            GameTooltip:AddLine(clickLabel, 0, 1, 0)
            GameTooltip:Show()
        end)
        
        row:SetScript("OnLeave", function(self)
            if not isSelected then
                self:SetBackdropColor(0.1, 0.08, 0.05, 0.7)
            end
            GameTooltip:Hide()
        end)
        
        -- Klick zum Auswählen
        row:SetScript("OnClick", function()
            Titles:SetSelectedTitle(t.id)
            self:UpdateTitles()
        end)
        
        y = y - 60
    end
    
    content:SetHeight(math.abs(y) + 20)
end


-- ══════════════════════════════════════════════════════════════
-- CALENDAR EVENT TOAST (Guild Push Notification)
-- ══════════════════════════════════════════════════════════════


