-- Load Rayfield library for GUI
local Rayfield = loadstring(game:HttpGet('https://sirius.menu/rayfield'))()

-- Create the main GUI window
local Window = Rayfield:CreateWindow({
   Name = "REINEXE LABAS MGA CHEATER",
   LoadingTitle = "Custom Control Panel",
   LoadingSubtitle = "Customize your character chams and ESP",
})

-- Variables and Services
local Players = game:GetService("Players")
local CoreGui = game:GetService("CoreGui")
local RunService = game:GetService("RunService")
local lp = Players.LocalPlayer
local chamsEnabled = false
local chamsFillColor = Color3.fromRGB(204, 153, 255) -- Default Chams color
local chamsOutlineColor = Color3.fromRGB(255, 255, 255) -- Outline color
local FillTransparency = 0.5
local OutlineTransparency = 0
local ESPEnabled = false
local ESPColor = Color3.fromRGB(0, 255, 0) -- Default ESP color
local StudsLimit = 7000 -- Maximum distance for ESP visibility
local skyboxEnabled = false

-- SELFCHAMS FUNCTIONS

-- Function to apply color and transparency to highlights
local function applyColor(highlight)
    highlight.FillColor = chamsFillColor
    highlight.FillTransparency = FillTransparency
    highlight.OutlineColor = chamsOutlineColor
    highlight.OutlineTransparency = OutlineTransparency
end

-- Toggle chams function
local function toggleChams()
    if chamsEnabled then
        -- Create storage for highlights if it doesn't exist
        if not CoreGui:FindFirstChild("Highlight_Storage") then
            local Storage = Instance.new("Folder")
            Storage.Name = "Highlight_Storage"
            Storage.Parent = CoreGui
        end

        -- Create highlights for each part of the character
        local function Highlight(object)
            local highlight = Instance.new("Highlight")
            highlight.Name = object.Name .. "_Highlight"
            applyColor(highlight)
            highlight.DepthMode = Enum.HighlightDepthMode.AlwaysOnTop
            highlight.Parent = CoreGui:FindFirstChild("Highlight_Storage")
            highlight.Adornee = object
            if object:IsA("BasePart") then
                object.Material = Enum.Material.ForceField
            end
        end

        -- Highlight character parts
        local function HighlightCharacter(character)
            if character then
                for _, part in ipairs(character:GetDescendants()) do
                    if part:IsA("BasePart") then
                        Highlight(part)
                    end
                end
                for _, item in ipairs(character:GetChildren()) do
                    if item:IsA("Tool") or item:IsA("Model") then
                        Highlight(item)
                    end
                end
            end
        end

        HighlightCharacter(lp.Character)
        
        -- Ensure new character parts get chams after respawn
        lp.CharacterAdded:Connect(function(character)
            HighlightCharacter(character)
        end)
    else
        -- Remove existing chams
        local Storage = CoreGui:FindFirstChild("Highlight_Storage")
        if Storage then
            Storage:Destroy()
        end
    end
end

-- GUI Toggle for enabling/disabling chams
local ChamsTab = Window:CreateTab("🟦 SELFCHAMS", nil)
ChamsTab:CreateToggle({
   Name = "Enable Chams",
   CurrentValue = false,
   Callback = function(Value)
       chamsEnabled = Value
       toggleChams()
   end,
})

-- GUI Color Picker for selecting chams color
ChamsTab:CreateColorPicker({
   Name = "Select Chams Color",
   Color = chamsFillColor,
   Callback = function(Color)
       chamsFillColor = Color
       if chamsEnabled then
           local Storage = CoreGui:FindFirstChild("Highlight_Storage")
           if Storage then
               for _, highlight in ipairs(Storage:GetChildren()) do
                   if highlight:IsA("Highlight") then
                       applyColor(highlight)
                   end
               end
           end
       end
   end,
})

-- SKYBOX & VIOLET LIGHT FUNCTIONS

-- Nebula Skybox Configuration
local Lighting = game:GetService("Lighting")

-- Nebula skybox asset IDs
local NebulaSkybox = {
    SkyboxBk = "rbxassetid://159454299",
    SkyboxDn = "rbxassetid://159454296",
    SkyboxFt = "rbxassetid://159454293",
    SkyboxLf = "rbxassetid://159454286",
    SkyboxRt = "rbxassetid://159454300",
    SkyboxUp = "rbxassetid://159454288"
}

-- Function to apply the Nebula skybox
local function applyNebulaSkybox()
    local sky = Lighting:FindFirstChildOfClass("Sky") or Instance.new("Sky", Lighting)
    sky.SkyboxBk = NebulaSkybox.SkyboxBk
    sky.SkyboxDn = NebulaSkybox.SkyboxDn
    sky.SkyboxFt = NebulaSkybox.SkyboxFt
    sky.SkyboxLf = NebulaSkybox.SkyboxLf
    sky.SkyboxRt = NebulaSkybox.SkyboxRt
    sky.SkyboxUp = NebulaSkybox.SkyboxUp
    print("Nebula Skybox Enabled")
end

-- Function to enable violet light with strong shadows and fog
local function enableConstantVioletLight()
    local lighting = game:GetService("Lighting")

    -- Apply settings for constant violet light
    lighting.GlobalShadows = true -- Enable strong shadows
    lighting.Brightness = 1
    lighting.ClockTime = 12 -- Set to any time for consistent violet appearance
    lighting.FogStart = 0 -- Fog starts from the camera
    lighting.FogEnd = 100 -- Adjust for fog density
    lighting.FogColor = Color3.fromRGB(138, 43, 226) -- Violet color for fog
    lighting.Ambient = Color3.fromRGB(138, 43, 226) -- Ambient light color
    lighting.OutdoorAmbient = Color3.fromRGB(138, 43, 226) -- Outdoor ambient light color
    print("Violet Light Enabled")
end

-- Run the function once
enableConstantVioletLight()

-- Continuously reapply the violet light settings
RunService.RenderStepped:Connect(enableConstantVioletLight)

-- Function to reset lighting settings
local function resetLighting()
    local lighting = game:GetService("Lighting")
    local sky = Lighting:FindFirstChildOfClass("Sky")
    if sky then
        sky:Destroy()
    end
    lighting.GlobalShadows = false
    lighting.Brightness = 2
    lighting.FogColor = Color3.fromRGB(255, 255, 255) -- Reset to default
    lighting.Ambient = Color3.fromRGB(255, 255, 255)
    lighting.OutdoorAmbient = Color3.fromRGB(255, 255, 255)
    print("Skybox & Violet Light Disabled")
end

-- Skybox & Violet Light Toggle
ChamsTab:CreateToggle({
    Name = "Enable Skybox & Violet Light",
    CurrentValue = false,
    Callback = function(Value)
        skyboxEnabled = Value
        if skyboxEnabled then
            applyNebulaSkybox()
            enableConstantVioletLight()
        else
            resetLighting()
        end
    end,
})

-- ESP FUNCTIONS

-- Function to create and customize ESP box for a player
local function createESPBox(player)
    local espBox = Drawing.new("Text")
    espBox.Size = 14
    espBox.Color = ESPColor
    espBox.Outline = true
    espBox.Center = true
    espBox.Visible = false
    return espBox
end

-- Function to update ESP box position and visibility
local function updateESPBox(espBox, player)
    if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
        local camera = workspace.CurrentCamera
        local rootPart = player.Character.HumanoidRootPart
        local screenPosition, onScreen = camera:WorldToViewportPoint(rootPart.Position)
        local distance = (rootPart.Position - lp.Character.HumanoidRootPart.Position).magnitude

        -- Update ESP box properties
        if onScreen and distance <= StudsLimit then
            espBox.Position = Vector2.new(screenPosition.X, screenPosition.Y - 30)
            espBox.Text = player.Name .. " | " .. math.floor(distance) .. " studs"
            espBox.Color = ESPColor
            espBox.Visible = true
        else
            espBox.Visible = false
        end
    else
        espBox.Visible = false
    end
end

-- Main ESP Update Loop
local espBoxes = {}

RunService.RenderStepped:Connect(function()
    if ESPEnabled then
        for _, player in pairs(Players:GetPlayers()) do
            if player ~= lp then
                if not espBoxes[player] then
                    espBoxes[player] = createESPBox(player)
                end
                updateESPBox(espBoxes[player], player)
            end
        end
    else
        for _, box in pairs(espBoxes) do
            box.Visible = false
        end
    end
end)

-- Cleanup ESP boxes when player leaves
Players.PlayerRemoving:Connect(function(player)
    if espBoxes[player] then
        espBoxes[player]:Remove()
        espBoxes[player] = nil
    end
end)

-- ESP GUI Controls
local ESPTab = Window:CreateTab("🟩 ESP", nil)

-- ESP Toggle
ESPTab:CreateToggle({
    Name = "Enable ESP",
    CurrentValue = false,
    Callback = function(Value)
        ESPEnabled = Value
    end,
})

-- ESP Color Picker
ESPTab:CreateColorPicker({
    Name = "Select ESP Color",
    Color = ESPColor,
    Callback = function(Color)
        ESPColor = Color
        for _, box in pairs(espBoxes) do
            box.Color = ESPColor
        end
    end,
})

-- ESP Distance Limit Slider
ESPTab:CreateSlider({
    Name = "ESP Distance Limit",
    Range = {100, 7000},
    Increment = 50,
    Suffix = "studs",
    CurrentValue = StudsLimit,
    Callback = function(Value)
        StudsLimit = Value
    end,
})

-- Main Loop to continuously check GUI state
while true do
    task.wait(0.1)
end
