
-- rChat: GUI
-- Modern tabbed configuration panel

local A, L = ...

local configFrame
local activeTab = 1

-- Reload UI popup
StaticPopupDialogs["RCHAT_RELOAD_UI"] = {
    text = "Settings changed. Reload UI now?",
    button1 = "Yes",
    button2 = "No",
    OnAccept = function() ReloadUI() end,
    timeout = 0,
    whileDead = true,
    hideOnEscape = true,
    preferredIndex = 3,
}

-- Color scheme
local colors = {
    primary = {0.2, 0.8, 0.6},      -- Teal accent
    bg = {0.08, 0.08, 0.08, 0.95},  -- Dark background
    panel = {0.12, 0.12, 0.12, 0.9},-- Panel background
    border = {0.3, 0.3, 0.3},       -- Border color
    text = {0.9, 0.9, 0.9},         -- Text color
    textDim = {0.6, 0.6, 0.6},      -- Dim text
    hover = {0.25, 0.9, 0.7},       -- Hover accent
}

-- Helper: Create styled panel
local function CreatePanel(parent, width, height)
    local panel = CreateFrame("Frame", nil, parent, "BackdropTemplate")
    panel:SetSize(width, height)
    panel:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8X8",
        edgeFile = "Interface\\Buttons\\WHITE8X8",
        tile = false,
        edgeSize = 1,
        insets = {left = 0, right = 0, top = 0, bottom = 0}
    })
    panel:SetBackdropColor(unpack(colors.panel))
    panel:SetBackdropBorderColor(unpack(colors.border))
    return panel
end

-- Helper: Create section header
local function CreateSectionHeader(parent, text, yOffset)
    local header = parent:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
    header:SetPoint("TOPLEFT", parent, "TOPLEFT", 20, yOffset)
    header:SetTextColor(unpack(colors.primary))
    header:SetText(text)
    
    local line = parent:CreateTexture(nil, "ARTWORK")
    line:SetHeight(2)
    line:SetPoint("TOPLEFT", header, "BOTTOMLEFT", 0, -8)
    line:SetPoint("RIGHT", parent, "RIGHT", -20, 0)
    line:SetColorTexture(colors.primary[1], colors.primary[2], colors.primary[3], 0.3)
    
    return header, line
end

-- Helper: Create modern checkbox
local function CreateModernCheckbox(parent, text, desc, yOffset, getter, setter)
    local container = CreateFrame("Frame", nil, parent)
    container:SetSize(640, 50)
    container:SetPoint("TOPLEFT", parent, "TOPLEFT", 20, yOffset)
    
    -- Checkbox
    local cb = CreateFrame("CheckButton", nil, container)
    cb:SetSize(24, 24)
    cb:SetPoint("LEFT", 0, 0)
    
    -- Custom checkbox appearance
    cb.bg = cb:CreateTexture(nil, "BACKGROUND")
    cb.bg:SetAllPoints()
    cb.bg:SetColorTexture(0.15, 0.15, 0.15, 1)
    
    cb.border = cb:CreateTexture(nil, "BORDER")
    cb.border:SetAllPoints()
    cb.border:SetColorTexture(colors.border[1], colors.border[2], colors.border[3], 1)
    cb.border:SetDrawLayer("BORDER", 1)
    
    cb.check = cb:CreateTexture(nil, "OVERLAY")
    cb.check:SetSize(16, 16)
    cb.check:SetPoint("CENTER")
    cb.check:SetTexture("Interface\\Buttons\\UI-CheckBox-Check")
    cb.check:SetVertexColor(colors.primary[1], colors.primary[2], colors.primary[3], 1)
    cb.check:Hide()
    
    cb:SetScript("OnEnter", function(self)
        self.border:SetColorTexture(colors.primary[1], colors.primary[2], colors.primary[3], 0.6)
    end)
    
    cb:SetScript("OnLeave", function(self)
        self.border:SetColorTexture(colors.border[1], colors.border[2], colors.border[3], 1)
    end)
    
    -- Label
    local label = container:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    label:SetPoint("LEFT", cb, "RIGHT", 10, 5)
    label:SetTextColor(unpack(colors.text))
    label:SetText(text)
    
    -- Description
    local description = container:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
    description:SetPoint("TOPLEFT", label, "BOTTOMLEFT", 0, -3)
    description:SetPoint("RIGHT", container, "RIGHT", -10, 0)
    description:SetJustifyH("LEFT")
    description:SetTextColor(unpack(colors.textDim))
    description:SetText(desc)
    
    -- Set initial state
    local initialValue = getter()
    cb:SetChecked(initialValue)
    if initialValue then
        cb.check:Show()
    end
    
    -- OnClick handler
    cb:SetScript("OnClick", function(self)
        local checked = self:GetChecked()
        if checked then
            self.check:Show()
        else
            self.check:Hide()
        end
        setter(checked)
    end)
    
    return container
end

-- Helper: Create modern slider
local function CreateModernSlider(parent, text, desc, min, max, step, yOffset, getter, setter)
    local container = CreateFrame("Frame", nil, parent)
    container:SetSize(640, 70)
    container:SetPoint("TOPLEFT", parent, "TOPLEFT", 20, yOffset)
    
    -- Label
    local label = container:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    label:SetPoint("TOPLEFT", 0, 0)
    label:SetTextColor(unpack(colors.text))
    label:SetText(text)
    
    -- Value display
    local value = getter() or min
    local valueText = container:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
    valueText:SetPoint("TOPRIGHT", 0, 0)
    valueText:SetTextColor(unpack(colors.primary))
    valueText:SetText(string.format("%.1f", value))
    
    -- Description
    local description = container:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
    description:SetPoint("TOPLEFT", label, "BOTTOMLEFT", 0, -3)
    description:SetTextColor(unpack(colors.textDim))
    description:SetText(desc)
    
    -- Slider background
    local sliderBg = container:CreateTexture(nil, "BACKGROUND")
    sliderBg:SetHeight(4)
    sliderBg:SetPoint("TOPLEFT", description, "BOTTOMLEFT", 5, -15)
    sliderBg:SetPoint("RIGHT", container, "RIGHT", -5, 0)
    sliderBg:SetColorTexture(0.15, 0.15, 0.15, 1)
    
    -- Slider
    local slider = CreateFrame("Slider", nil, container)
    slider:SetHeight(20)
    slider:SetPoint("LEFT", sliderBg, "LEFT", 0, 0)
    slider:SetPoint("RIGHT", sliderBg, "RIGHT", 0, 0)
    slider:SetMinMaxValues(min, max)
    slider:SetValueStep(step)
    slider:SetObeyStepOnDrag(true)
    slider:SetValue(value)
    slider:SetOrientation("HORIZONTAL")
    
    -- Slider thumb
    local thumb = slider:CreateTexture(nil, "OVERLAY")
    thumb:SetSize(16, 16)
    thumb:SetTexture("Interface\\Buttons\\WHITE8X8")
    thumb:SetVertexColor(unpack(colors.primary))
    slider:SetThumbTexture(thumb)
    
    -- Filled portion
    local fill = container:CreateTexture(nil, "ARTWORK")
    fill:SetHeight(4)
    fill:SetPoint("LEFT", sliderBg, "LEFT", 0, 0)
    fill:SetColorTexture(colors.primary[1], colors.primary[2], colors.primary[3], 0.6)
    
    local function UpdateFill()
        local pct = (slider:GetValue() - min) / (max - min)
        fill:SetWidth(sliderBg:GetWidth() * pct)
    end
    
    UpdateFill()
    
    slider:SetScript("OnValueChanged", function(self, v)
        valueText:SetText(string.format("%.1f", v))
        UpdateFill()
        setter(v)
    end)
    
    slider:SetScript("OnEnter", function(self)
        thumb:SetVertexColor(unpack(colors.hover))
    end)
    
    slider:SetScript("OnLeave", function(self)
        thumb:SetVertexColor(unpack(colors.primary))
    end)
    
    return container
end

-- Helper: Create radio group
local function CreateRadioGroup(parent, text, desc, options, yOffset, getter, setter)
    local container = CreateFrame("Frame", nil, parent)
    container:SetSize(640, 80)
    container:SetPoint("TOPLEFT", parent, "TOPLEFT", 20, yOffset)
    
    -- Label
    local label = container:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    label:SetPoint("TOPLEFT", 0, 0)
    label:SetTextColor(unpack(colors.text))
    label:SetText(text)
    
    -- Description
    local description = container:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
    description:SetPoint("TOPLEFT", label, "BOTTOMLEFT", 0, -3)
    description:SetTextColor(unpack(colors.textDim))
    description:SetText(desc)
    
    local buttons = {}
    local currentValue = getter()
    
    for i, option in ipairs(options) do
        local btn = CreateFrame("CheckButton", nil, container, "UIRadioButtonTemplate")
        btn:SetSize(20, 20)
        if i == 1 then
            btn:SetPoint("TOPLEFT", description, "BOTTOMLEFT", 0, -10)
        else
            btn:SetPoint("LEFT", buttons[i-1], "RIGHT", 120, 0)
        end
        
        local btnText = btn:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        btnText:SetPoint("LEFT", btn, "RIGHT", 5, 0)
        btnText:SetTextColor(unpack(colors.text))
        btnText:SetText(option.label)
        
        btn:SetChecked(currentValue == option.value)
        
        btn:SetScript("OnClick", function(self)
            for j, b in ipairs(buttons) do
                b:SetChecked(j == i)
            end
            setter(option.value)
        end)
        
        buttons[i] = btn
    end
    
    return container
end

-- Create config frame
local function CreateConfigFrame()
    if configFrame then return configFrame end
    
    -- Main frame with custom backdrop
    local frame = CreateFrame("Frame", "rChatConfigFrame", UIParent, "BackdropTemplate")
    frame:SetSize(720, 580)
    frame:SetPoint("CENTER")
    frame:SetMovable(true)
    frame:EnableMouse(true)
    frame:RegisterForDrag("LeftButton")
    frame:SetScript("OnDragStart", frame.StartMoving)
    frame:SetScript("OnDragStop", frame.StopMovingOrSizing)
    frame:SetFrameStrata("DIALOG")
    frame:Hide()
    
    -- Custom backdrop
    frame:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8X8",
        edgeFile = "Interface\\Buttons\\WHITE8X8",
        tile = false,
        edgeSize = 2,
        insets = {left = 0, right = 0, top = 0, bottom = 0}
    })
    frame:SetBackdropColor(unpack(colors.bg))
    frame:SetBackdropBorderColor(colors.primary[1], colors.primary[2], colors.primary[3], 0.8)
    
    -- Title bar
    local titleBg = CreateFrame("Frame", nil, frame, "BackdropTemplate")
    titleBg:SetPoint("TOPLEFT", 2, -2)
    titleBg:SetPoint("TOPRIGHT", -2, -2)
    titleBg:SetHeight(45)
    titleBg:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8X8",
        tile = false,
    })
    titleBg:SetBackdropColor(colors.primary[1], colors.primary[2], colors.primary[3], 0.15)
    
    -- Title text
    local title = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalHuge")
    title:SetPoint("LEFT", titleBg, "LEFT", 20, 0)
    title:SetTextColor(unpack(colors.primary))
    title:SetText("rChat")
    
    -- Close button
    local closeBtn = CreateFrame("Button", nil, frame, "UIPanelCloseButton")
    closeBtn:SetPoint("TOPRIGHT", frame, "TOPRIGHT", 0, 0)
    closeBtn:SetScript("OnClick", function() frame:Hide() end)
    
    -- Tab system
    local tabs = {}
    local tabContent = {}
    
    -- Create tab buttons
    local tabNames = {
        {name = "Chicchai", icon = "⚡"},
        {name = "Chat", icon = "💬"},
        {name = "About", icon = "ℹ"},
    }
    
    local function SelectTab(index)
        activeTab = index
        for i, tab in ipairs(tabs) do
            if i == index then
                tab.bg:SetBackdropColor(colors.primary[1], colors.primary[2], colors.primary[3], 0.2)
                tab.text:SetTextColor(unpack(colors.primary))
                tabContent[i]:Show()
            else
                tab.bg:SetBackdropColor(0.08, 0.08, 0.08, 0.5)
                tab.text:SetTextColor(unpack(colors.textDim))
                tabContent[i]:Hide()
            end
        end
    end
    
    for i, tabData in ipairs(tabNames) do
        local tab = CreateFrame("Button", nil, frame)
        tab:SetSize(120, 40)
        if i == 1 then
            tab:SetPoint("TOPLEFT", titleBg, "BOTTOMLEFT", 10, -10)
        else
            tab:SetPoint("LEFT", tabs[i-1], "RIGHT", 5, 0)
        end
        
        -- Tab background
        tab.bg = CreateFrame("Frame", nil, tab, "BackdropTemplate")
        tab.bg:SetAllPoints()
        tab.bg:SetBackdrop({
            bgFile = "Interface\\Buttons\\WHITE8X8",
            edgeFile = "Interface\\Buttons\\WHITE8X8",
            tile = false,
            edgeSize = 1,
            insets = {left = 0, right = 0, top = 0, bottom = 0}
        })
        tab.bg:SetBackdropColor(0.08, 0.08, 0.08, 0.5)
        tab.bg:SetBackdropBorderColor(unpack(colors.border))
        
        -- Tab text
        tab.text = tab:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        tab.text:SetPoint("CENTER")
        tab.text:SetText(tabData.icon .. " " .. tabData.name)
        tab.text:SetTextColor(unpack(colors.textDim))
        
        -- Hover effect
        tab:SetScript("OnEnter", function(self)
            if activeTab ~= i then
                self.bg:SetBackdropColor(0.12, 0.12, 0.12, 0.7)
                self.text:SetTextColor(unpack(colors.hover))
            end
        end)
        
        tab:SetScript("OnLeave", function(self)
            if activeTab ~= i then
                self.bg:SetBackdropColor(0.08, 0.08, 0.08, 0.5)
                self.text:SetTextColor(unpack(colors.textDim))
            end
        end)
        
        tab:SetScript("OnClick", function() SelectTab(i) end)
        
        tabs[i] = tab
    end
    
    -- Content container
    local contentFrame = CreateFrame("Frame", nil, frame, "BackdropTemplate")
    contentFrame:SetPoint("TOPLEFT", tabs[1], "BOTTOMLEFT", 0, -5)
    contentFrame:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -10, 50)
    contentFrame:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8X8",
        edgeFile = "Interface\\Buttons\\WHITE8X8",
        tile = false,
        edgeSize = 1,
        insets = {left = 0, right = 0, top = 0, bottom = 0}
    })
    contentFrame:SetBackdropColor(0.05, 0.05, 0.05, 0.8)
    contentFrame:SetBackdropBorderColor(unpack(colors.border))
    
    -- TAB 1: Chicchai Settings
    local tab1 = CreateFrame("ScrollFrame", nil, contentFrame, "UIPanelScrollFrameTemplate")
    tab1:SetPoint("TOPLEFT", contentFrame, "TOPLEFT", 5, -5)
    tab1:SetPoint("BOTTOMRIGHT", contentFrame, "BOTTOMRIGHT", -25, 5)
    
    local tab1Content = CreateFrame("Frame", nil, tab1)
    tab1Content:SetSize(640, 800)
    tab1:SetScrollChild(tab1Content)
    
    local y = -20
    
    CreateSectionHeader(tab1Content, L.Strings["CHICCHAI_HEADER"], y)
    y = y - 45
    
    CreateModernCheckbox(tab1Content, 
        L.Strings["CHICCHAI_ENABLE"], 
        L.Strings["CHICCHAI_ENABLE_DESC"], 
        y,
        function() return L:GetConfig("chicchai", "enabled") end,
        function(v) L:SetConfig("chicchai", "enabled", v); StaticPopup_Show("RCHAT_RELOAD_UI") end)
    y = y - 65
    
    -- Info panel
    local infoPanel = CreatePanel(tab1Content, 620, 50)
    infoPanel:SetPoint("TOPLEFT", tab1Content, "TOPLEFT", 20, y)
    
    local infoIcon = infoPanel:CreateFontString(nil, "OVERLAY", "GameFontNormalHuge")
    infoIcon:SetPoint("LEFT", infoPanel, "LEFT", 15, 0)
    infoIcon:SetText("💡")
    
    local infoText = infoPanel:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
    infoText:SetPoint("LEFT", infoIcon, "RIGHT", 10, 0)
    infoText:SetPoint("RIGHT", infoPanel, "RIGHT", -15, 0)
    infoText:SetJustifyH("LEFT")
    infoText:SetTextColor(unpack(colors.textDim))
    infoText:SetText(L.Strings["CHICCHAI_EDITMODE_NOTE"])
    y = y - 70
    
    CreateModernSlider(tab1Content,
        L.Strings["CHICCHAI_ANIM_TIME"],
        L.Strings["CHICCHAI_ANIM_TIME_DESC"],
        0.1, 2.0, 0.1, y,
        function() return L:GetConfig("chicchai", "animTime") end,
        function(v) L:SetConfig("chicchai", "animTime", v) end)
    y = y - 85
    
    CreateModernSlider(tab1Content,
        L.Strings["CHICCHAI_MIN_TIME"],
        L.Strings["CHICCHAI_MIN_TIME_DESC"],
        1, 60, 1, y,
        function() return L:GetConfig("chicchai", "minimizeTime") end,
        function(v) L:SetConfig("chicchai", "minimizeTime", v) end)
    y = y - 85
    
    CreateModernSlider(tab1Content,
        L.Strings["CHICCHAI_MIN_LINES"],
        L.Strings["CHICCHAI_MIN_LINES_DESC"],
        1, 5, 1, y,
        function() return L:GetConfig("chicchai", "minimizedLines") end,
        function(v) L:SetConfig("chicchai", "minimizedLines", v) end)
    y = y - 85
    
    CreateModernCheckbox(tab1Content,
        L.Strings["CHICCHAI_MOUSE_OVER"],
        L.Strings["CHICCHAI_MOUSE_OVER_DESC"],
        y,
        function() return L:GetConfig("chicchai", "maximizeOnEnter") end,
        function(v) L:SetConfig("chicchai", "maximizeOnEnter", v) end)
    y = y - 65
    
    CreateModernCheckbox(tab1Content,
        L.Strings["CHICCHAI_COMBAT_LOCK"],
        L.Strings["CHICCHAI_COMBAT_LOCK_DESC"],
        y,
        function() return L:GetConfig("chicchai", "lockInCombat") end,
        function(v) L:SetConfig("chicchai", "lockInCombat", v) end)
    y = y - 65
    
    CreateModernCheckbox(tab1Content,
        L.Strings["CHICCHAI_COMBAT_LOG"],
        L.Strings["CHICCHAI_COMBAT_LOG_DESC"],
        y,
        function() return L:GetConfig("chicchai", "maximizeCombatLog") end,
        function(v) L:SetConfig("chicchai", "maximizeCombatLog", v); StaticPopup_Show("RCHAT_RELOAD_UI") end)
    
    tabContent[1] = tab1
    
    -- TAB 2: Chat Settings
    local tab2 = CreateFrame("ScrollFrame", nil, contentFrame, "UIPanelScrollFrameTemplate")
    tab2:SetPoint("TOPLEFT", contentFrame, "TOPLEFT", 5, -5)
    tab2:SetPoint("BOTTOMRIGHT", contentFrame, "BOTTOMRIGHT", -25, 5)
    
    local tab2Content = CreateFrame("Frame", nil, tab2)
    tab2Content:SetSize(640, 900)
    tab2:SetScrollChild(tab2Content)
    
    y = -20
    
    CreateSectionHeader(tab2Content, L.Strings["CHAT_HEADER"], y)
    y = y - 45
    
    CreateModernCheckbox(tab2Content,
        L.Strings["CHAT_ALWAYS_SHOW_TABS"],
        L.Strings["CHAT_ALWAYS_SHOW_TABS_DESC"],
        y,
        function() return L:GetConfig("chat", "alwaysShowTabs") end,
        function(v) L:SetConfig("chat", "alwaysShowTabs", v); StaticPopup_Show("RCHAT_RELOAD_UI") end)
    y = y - 65
    
    CreateModernCheckbox(tab2Content,
        L.Strings["CHAT_SHOW_TIMESTAMP"],
        L.Strings["CHAT_SHOW_TIMESTAMP_DESC"],
        y,
        function() return L:GetConfig("chat", "showTimestamp") end,
        function(v) L:SetConfig("chat", "showTimestamp", v); StaticPopup_Show("RCHAT_RELOAD_UI") end)
    y = y - 65
    
    CreateModernCheckbox(tab2Content,
        L.Strings["CHAT_SHOW_PLAYER_LEVEL"],
        L.Strings["CHAT_SHOW_PLAYER_LEVEL_DESC"],
        y,
        function() return L:GetConfig("chat", "showPlayerLevel") end,
        function(v) L:SetConfig("chat", "showPlayerLevel", v); StaticPopup_Show("RCHAT_RELOAD_UI") end)
    y = y - 65
    
    CreateModernCheckbox(tab2Content,
        L.Strings["CHAT_ENABLE_URL_COPY"],
        L.Strings["CHAT_ENABLE_URL_COPY_DESC"],
        y,
        function() return L:GetConfig("chat", "enableURLCopy") end,
        function(v) L:SetConfig("chat", "enableURLCopy", v) end)
    y = y - 65
    
    CreateModernCheckbox(tab2Content,
        L.Strings["CHAT_ENABLE_COPY_BUTTON"],
        L.Strings["CHAT_ENABLE_COPY_BUTTON_DESC"],
        y,
        function() return L:GetConfig("chat", "enableChatCopyButton") end,
        function(v) L:SetConfig("chat", "enableChatCopyButton", v); StaticPopup_Show("RCHAT_RELOAD_UI") end)
    y = y - 65
    
    CreateModernSlider(tab2Content,
        L.Strings["CHAT_FADE_TIME"],
        L.Strings["CHAT_FADE_TIME_DESC"],
        5, 120, 5, y,
        function() return L:GetConfig("chat", "chatFadeTime") end,
        function(v) L:SetConfig("chat", "chatFadeTime", v); StaticPopup_Show("RCHAT_RELOAD_UI") end)
    y = y - 95
    
    CreateRadioGroup(tab2Content,
        L.Strings["CHAT_EDITBOX_POSITION"],
        L.Strings["CHAT_EDITBOX_POSITION_DESC"],
        {
            {label = L.Strings["CHAT_EDITBOX_TOP"], value = "top"},
            {label = L.Strings["CHAT_EDITBOX_BOTTOM"], value = "bottom"}
        },
        y,
        function() return L:GetConfig("chat", "editboxPosition") end,
        function(v) L:SetConfig("chat", "editboxPosition", v); L:UpdateEditBoxPositions() end)
    y = y - 95
    
    -- Minimap Icon Settings
    CreateSectionHeader(tab2Content, "Minimap", y)
    y = y - 45
    
    CreateModernCheckbox(tab2Content,
        L.Strings["MINIMAP_ICON_SHOW"],
        L.Strings["MINIMAP_ICON_SHOW_DESC"],
        y,
        function() return not L:GetConfig("minimap", "hide") end,
        function(v) 
            L:SetConfig("minimap", "hide", not v)
            L:UpdateMinimapIcon()
        end)
    
    tabContent[2] = tab2
    tab2:Hide()
    
    -- TAB 3: About
    local tab3 = CreateFrame("Frame", nil, contentFrame)
    tab3:SetAllPoints(contentFrame)
    
    local aboutPanel = CreatePanel(tab3, 620, 350)
    aboutPanel:SetPoint("CENTER", tab3, "CENTER", 0, 20)
    
    local logo = aboutPanel:CreateTexture(nil, "ARTWORK")
    logo:SetTexture("Interface\\AddOns\\rChat\\core\\logo")
    logo:SetSize(200, 208)
    logo:SetPoint("TOP", aboutPanel, "TOP", 0, -30)
    logo:SetAlpha(0.9)
    
    local aboutTitle = aboutPanel:CreateFontString(nil, "OVERLAY", "GameFontNormalHuge")
    aboutTitle:SetPoint("TOP", logo, "BOTTOM", 0, -15)
    aboutTitle:SetTextColor(unpack(colors.primary))
    aboutTitle:SetText("rChat")
    
    local aboutDesc = aboutPanel:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
    aboutDesc:SetPoint("TOP", aboutTitle, "BOTTOM", 0, -10)
    aboutDesc:SetWidth(500)
    aboutDesc:SetJustifyH("CENTER")
    aboutDesc:SetTextColor(unpack(colors.text))
    aboutDesc:SetText("A modern, minimalist chat enhancement addon\nfeaturing Chicchai auto-minimize mode")
    
    local featuresList = aboutPanel:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    featuresList:SetPoint("TOP", aboutDesc, "BOTTOM", 0, -20)
    featuresList:SetWidth(500)
    featuresList:SetJustifyH("LEFT")
    featuresList:SetTextColor(unpack(colors.textDim))
    featuresList:SetText(
        "✓ Auto-minimize chat after inactivity\n" ..
        "✓ Customizable animation and timing\n" ..
        "✓ URL copy functionality\n" ..
        "✓ Timestamps and player levels\n" ..
        "✓ Modern, clean interface"
    )
    
    tabContent[3] = tab3
    tab3:Hide()
    
    -- Bottom action bar
    local actionBar = CreateFrame("Frame", nil, frame, "BackdropTemplate")
    actionBar:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 10, 10)
    actionBar:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -10, 10)
    actionBar:SetHeight(35)
    actionBar:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8X8",
        tile = false,
    })
    actionBar:SetBackdropColor(0.05, 0.05, 0.05, 0.5)
    
    -- Reload warning
    local warningIcon = actionBar:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    warningIcon:SetPoint("LEFT", actionBar, "LEFT", 15, 0)
    warningIcon:SetText("⚠")
    warningIcon:SetTextColor(1, 0.6, 0)
    
    local warning = actionBar:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
    warning:SetPoint("LEFT", warningIcon, "RIGHT", 8, 0)
    warning:SetTextColor(unpack(colors.textDim))
    warning:SetText(L.Strings["GUI_RELOAD_WARNING"])
    
    -- Reload button
    local reloadBtn = CreateFrame("Button", nil, actionBar, "BackdropTemplate")
    reloadBtn:SetSize(140, 28)
    reloadBtn:SetPoint("RIGHT", actionBar, "RIGHT", -10, 0)
    reloadBtn:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8X8",
        edgeFile = "Interface\\Buttons\\WHITE8X8",
        tile = false,
        edgeSize = 1,
        insets = {left = 0, right = 0, top = 0, bottom = 0}
    })
    reloadBtn:SetBackdropColor(colors.primary[1], colors.primary[2], colors.primary[3], 0.3)
    reloadBtn:SetBackdropBorderColor(unpack(colors.primary))
    
    local reloadText = reloadBtn:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    reloadText:SetPoint("CENTER")
    reloadText:SetTextColor(unpack(colors.primary))
    reloadText:SetText("🔄 " .. L.Strings["GUI_RELOAD_BUTTON"])
    
    reloadBtn:SetScript("OnEnter", function(self)
        self:SetBackdropColor(colors.primary[1], colors.primary[2], colors.primary[3], 0.5)
        reloadText:SetTextColor(1, 1, 1)
    end)
    
    reloadBtn:SetScript("OnLeave", function(self)
        self:SetBackdropColor(colors.primary[1], colors.primary[2], colors.primary[3], 0.3)
        reloadText:SetTextColor(unpack(colors.primary))
    end)
    
    reloadBtn:SetScript("OnClick", function() ReloadUI() end)
    
    -- Select first tab
    SelectTab(1)
    
    configFrame = frame
    return frame
end

function L:ShowConfig()
    local frame = CreateConfigFrame()
    if frame:IsShown() then frame:Hide() else frame:Show() end
end

function L:RegisterInterfaceOptions()
    local panel = CreateFrame("Frame", "rChatInterfacePanel")
    panel.name = "rChat"
    
    local title = panel:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
    title:SetPoint("TOP", 0, -16)
    title:SetText("rChat")
    
    local info = panel:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
    info:SetPoint("TOP", title, "BOTTOM", 0, -16)
    info:SetWidth(600)
    info:SetJustifyH("LEFT")
    info:SetText("Click the button below to open rChat settings.\n\nFeatures: Chicchai (auto-minimize chat)")
    
    local btn = CreateFrame("Button", nil, panel, "UIPanelButtonTemplate")
    btn:SetSize(200, 40)
    btn:SetPoint("TOP", info, "BOTTOM", 0, -20)
    btn:SetText("Open rChat Settings")
    btn:SetScript("OnClick", function() L:ShowConfig() end)
    
    local logo = panel:CreateTexture(nil, "ARTWORK")
    logo:SetTexture("Interface\\AddOns\\rChat\\core\\logo")
    logo:SetSize(235, 244)
    logo:SetPoint("BOTTOM", panel, "BOTTOM", 0, 50)
    logo:SetAlpha(0.8)
    
    local category = Settings.RegisterCanvasLayoutCategory(panel, panel.name)
    Settings.RegisterAddOnCategory(category)
    
    -- Store category ID globally for minimap icon to use
    rChatSettingsCategoryID = category:GetID()
    L.settingsCategory = category
end

SLASH_RCHAT1 = "/rchat"
SlashCmdList["RCHAT"] = function(msg)
    msg = msg:lower():trim()
    if msg == "config" or msg == "cfg" or msg == "" then
        L:ShowConfig()
    elseif msg == "toggle" then
        local enabled = L:GetConfig("chicchai", "enabled")
        L:SetConfig("chicchai", "enabled", not enabled)
        print("|cff33ff99rChat:|r Chicchai", not enabled and "|cff00ff00enabled|r" or "|cffff0000disabled|r")
        ReloadUI()
    elseif msg == "reset" then
        L:ResetConfig()
    else
        print("|cff33ff99rChat:|r " .. L.Strings["CMD_HELP"])
        print("  " .. L.Strings["CMD_CONFIG"])
        print("  " .. L.Strings["CMD_TOGGLE"])
        print("  " .. L.Strings["CMD_RESET"])
    end
end
