-- Trigon Console UI
for _, v in pairs(gethui():GetChildren()) do
	if string.find(v.Name, "Trigon") then
		v:Destroy()
	end
end

local TextService = game:GetService("TextService")
local LogService = game:GetService("LogService")
local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")
local gui = Instance.new("ScreenGui")
gui.Name = "TrigonConsole"
gui.ResetOnSpawn = false
gui.IgnoreGuiInset = true
gui.DisplayOrder = 999
gui.ScreenInsets = Enum.ScreenInsets.DeviceSafeInsets
gui.ZIndexBehavior = Enum.ZIndexBehavior.Sibling
gui.Parent = gethui()

local consoleWindow = Instance.new("Frame")
consoleWindow.Name = "ConsoleWindow"
consoleWindow.AnchorPoint = Vector2.new(0.5, 0.5)
consoleWindow.Position = UDim2.new(0.5, 0, 0.5, 0)
consoleWindow.Size = UDim2.new(0, 520, 0, 360)
consoleWindow.BackgroundColor3 = Color3.fromRGB(16, 18, 22)
consoleWindow.BorderSizePixel = 0
consoleWindow.Parent = gui

local windowCorner = Instance.new("UICorner")
windowCorner.CornerRadius = UDim.new(0, 10)
windowCorner.Parent = consoleWindow

local windowStroke = Instance.new("UIStroke")
windowStroke.Color = Color3.fromRGB(50, 55, 65)
windowStroke.Thickness = 1
windowStroke.Transparency = 0.3
windowStroke.Parent = consoleWindow

local sizeConstraint = Instance.new("UISizeConstraint")
sizeConstraint.MaxSize = Vector2.new(900, 650)
sizeConstraint.MinSize = Vector2.new(300, 220)
sizeConstraint.Parent = consoleWindow

local titleBar = Instance.new("Frame")
titleBar.Name = "TitleBar"
titleBar.Size = UDim2.new(1, 0, 0, 36)
titleBar.BackgroundColor3 = Color3.fromRGB(13, 15, 18)
titleBar.BorderSizePixel = 0
titleBar.Parent = consoleWindow

local titleCorner = Instance.new("UICorner")
titleCorner.CornerRadius = UDim.new(0, 10)
titleCorner.Parent = titleBar

local titleBarFix = Instance.new("Frame")
titleBarFix.Size = UDim2.new(1, 0, 0, 10)
titleBarFix.Position = UDim2.new(0, 0, 1, -10)
titleBarFix.BackgroundColor3 = Color3.fromRGB(13, 15, 18)
titleBarFix.BorderSizePixel = 0
titleBarFix.Parent = titleBar

local titleText = Instance.new("TextLabel")
titleText.Name = "TitleText"
titleText.Position = UDim2.new(0, 12, 0, 0)
titleText.Size = UDim2.new(0, 150, 1, 0)
titleText.BackgroundTransparency = 1
titleText.Text = "Trigon Console"
titleText.TextColor3 = Color3.fromRGB(200, 200, 200)
titleText.Font = Enum.Font.GothamBold
titleText.TextSize = 13
titleText.TextXAlignment = Enum.TextXAlignment.Left
titleText.Parent = titleBar

local minButton = Instance.new("ImageButton")
minButton.Name = "MinButton"
minButton.BorderSizePixel = 0
minButton.BackgroundColor3 = Color3.fromRGB(39, 41, 46)
minButton.AnchorPoint = Vector2.new(0.5, 0.5)
minButton.Size = UDim2.new(0, 60, 0, 60)
minButton.Position = UDim2.new(0.9, 0, 0.9, 0)
minButton.BackgroundTransparency = 0
minButton.Visible = false
minButton.Parent = gui

local minCorner = Instance.new("UICorner")
minCorner.CornerRadius = UDim.new(1, 0)
minCorner.Parent = minButton

local minStroke = Instance.new("UIStroke")
minStroke.Color = Color3.fromRGB(64, 68, 75)
minStroke.Thickness = 3
minStroke.Parent = minButton

local minLogo = Instance.new("ImageLabel")
minLogo.Name = "Logo"
minLogo.AnchorPoint = Vector2.new(0.5, 0.5)
minLogo.Position = UDim2.new(0.5, 0, 0.5, 0)
minLogo.Size = UDim2.new(0.6, 0, 0.6, 0)
minLogo.BackgroundTransparency = 1
minLogo.Image = "rbxassetid://82500352718600"
minLogo.ScaleType = Enum.ScaleType.Fit
minLogo.Parent = minButton

local controls = Instance.new("Frame")
controls.Name = "Controls"
controls.AnchorPoint = Vector2.new(1, 0.5)
controls.Position = UDim2.new(1, -8, 0.5, 0)
controls.Size = UDim2.new(0, 60, 0, 24)
controls.BackgroundTransparency = 1
controls.Parent = titleBar

local controlsLayout = Instance.new("UIListLayout")
controlsLayout.FillDirection = Enum.FillDirection.Horizontal
controlsLayout.HorizontalAlignment = Enum.HorizontalAlignment.Right
controlsLayout.VerticalAlignment = Enum.VerticalAlignment.Center
controlsLayout.Padding = UDim.new(0, 4)
controlsLayout.Parent = controls

local minimizeBtn = Instance.new("TextButton")
minimizeBtn.Name = "MinimizeBtn"
minimizeBtn.Size = UDim2.new(0, 24, 0, 24)
minimizeBtn.BackgroundColor3 = Color3.fromRGB(35, 38, 45)
minimizeBtn.Text = "_"
minimizeBtn.TextColor3 = Color3.fromRGB(255, 255, 255)
minimizeBtn.BackgroundTransparency = 0.7
minimizeBtn.Font = Enum.Font.GothamBold
minimizeBtn.TextSize = 16
minimizeBtn.Parent = controls

local minimizeCorner = Instance.new("UICorner")
minimizeCorner.CornerRadius = UDim.new(0, 4)
minimizeCorner.Parent = minimizeBtn

local content = Instance.new("Frame")
content.Name = "Content"
content.Position = UDim2.new(0, 8, 0, 44)
content.Size = UDim2.new(1, -16, 1, -52)
content.BackgroundTransparency = 1
content.Parent = consoleWindow

local filterBar = Instance.new("Frame")
filterBar.Name = "FilterBar"
filterBar.Size = UDim2.new(1, 0, 0, 32)
filterBar.BackgroundColor3 = Color3.fromRGB(22, 24, 28)
filterBar.BorderSizePixel = 0
filterBar.Parent = content

local filterCorner = Instance.new("UICorner")
filterCorner.CornerRadius = UDim.new(0, 5)
filterCorner.Parent = filterBar

local filterLayout = Instance.new("UIListLayout")
filterLayout.FillDirection = Enum.FillDirection.Horizontal
filterLayout.VerticalAlignment = Enum.VerticalAlignment.Center
filterLayout.Padding = UDim.new(0, 6)
filterLayout.Parent = filterBar

local filterPadding = Instance.new("UIPadding")
filterPadding.PaddingLeft = UDim.new(0, 8)
filterPadding.PaddingRight = UDim.new(0, 8)
filterPadding.Parent = filterBar

local outputBtn = Instance.new("ImageButton")
outputBtn.Name = "OutputFilter"
outputBtn.Size = UDim2.new(0, 75, 0, 24)
outputBtn.BackgroundColor3 = Color3.fromRGB(30, 33, 38)
outputBtn.BorderSizePixel = 0
outputBtn.ScaleType = Enum.ScaleType.Fit
outputBtn.ImageColor3 = Color3.fromRGB(220, 220, 220)
outputBtn.Parent = filterBar

local outputCorner = Instance.new("UICorner")
outputCorner.CornerRadius = UDim.new(0, 4)
outputCorner.Parent = outputBtn

local outputCounter = Instance.new("TextLabel")
outputCounter.Name = "Counter"
outputCounter.AnchorPoint = Vector2.new(1, 0.5)
outputCounter.Position = UDim2.new(1, -5, 0.5, 0)
outputCounter.Size = UDim2.new(0, 30, 0, 18)
outputCounter.BackgroundColor3 = Color3.fromRGB(20, 22, 26)
outputCounter.Text = "0"
outputCounter.TextColor3 = Color3.fromRGB(220, 220, 220)
outputCounter.Font = Enum.Font.GothamBold
outputCounter.TextSize = 10
outputCounter.Parent = outputBtn

local outputCounterCorner = Instance.new("UICorner")
outputCounterCorner.CornerRadius = UDim.new(0, 3)
outputCounterCorner.Parent = outputCounter

local outputIcon = Instance.new("ImageLabel")
outputIcon.Name = "icon"
outputIcon.ImageColor3 = Color3.fromRGB(220, 220, 220)
outputIcon.BorderSizePixel = 0
outputIcon.BackgroundTransparency = 1
outputIcon.AnchorPoint = Vector2.new(1, 0.5)
outputIcon.Position = UDim2.new(1, -45, 0.5, 0)
outputIcon.Size = UDim2.new(0, 30, 0, 18)
outputIcon.Image = "rbxassetid://14906027134"
outputIcon.ScaleType = Enum.ScaleType.Fit
outputIcon.Parent = outputBtn

local warningBtn = Instance.new("ImageButton")
warningBtn.Name = "WarningFilter"
warningBtn.Size = UDim2.new(0, 75, 0, 24)
warningBtn.BackgroundColor3 = Color3.fromRGB(30, 33, 38)
warningBtn.BorderSizePixel = 0
warningBtn.ScaleType = Enum.ScaleType.Fit
warningBtn.ImageColor3 = Color3.fromRGB(255, 213, 46)
warningBtn.Parent = filterBar

local warningCorner = Instance.new("UICorner")
warningCorner.CornerRadius = UDim.new(0, 4)
warningCorner.Parent = warningBtn

local warningCounter = Instance.new("TextLabel")
warningCounter.Name = "Counter"
warningCounter.AnchorPoint = Vector2.new(1, 0.5)
warningCounter.Position = UDim2.new(1, -5, 0.5, 0)
warningCounter.Size = UDim2.new(0, 30, 0, 18)
warningCounter.BackgroundColor3 = Color3.fromRGB(20, 22, 26)
warningCounter.Text = "0"
warningCounter.TextColor3 = Color3.fromRGB(255, 213, 46)
warningCounter.Font = Enum.Font.GothamBold
warningCounter.TextSize = 10
warningCounter.Parent = warningBtn

local warningCounterCorner = Instance.new("UICorner")
warningCounterCorner.CornerRadius = UDim.new(0, 3)
warningCounterCorner.Parent = warningCounter

local warningIcon = Instance.new("ImageLabel")
warningIcon.Name = "icon"
warningIcon.ImageColor3 = Color3.fromRGB(255, 213, 46)
warningIcon.BorderSizePixel = 0
warningIcon.BackgroundTransparency = 1
warningIcon.AnchorPoint = Vector2.new(1, 0.5)
warningIcon.Position = UDim2.new(1, -45, 0.5, 0)
warningIcon.Size = UDim2.new(0, 30, 0, 18)
warningIcon.Image = "rbxassetid://14943812610"
warningIcon.ScaleType = Enum.ScaleType.Fit
warningIcon.Parent = warningBtn

local errorBtn = Instance.new("ImageButton")
errorBtn.Name = "ErrorFilter"
errorBtn.Size = UDim2.new(0, 75, 0, 24)
errorBtn.BackgroundColor3 = Color3.fromRGB(30, 33, 38)
errorBtn.BorderSizePixel = 0
errorBtn.ScaleType = Enum.ScaleType.Fit
errorBtn.ImageColor3 = Color3.fromRGB(255, 85, 85)
errorBtn.Parent = filterBar

local errorCorner = Instance.new("UICorner")
errorCorner.CornerRadius = UDim.new(0, 4)
errorCorner.Parent = errorBtn

local errorCounter = Instance.new("TextLabel")
errorCounter.Name = "Counter"
errorCounter.AnchorPoint = Vector2.new(1, 0.5)
errorCounter.Position = UDim2.new(1, -5, 0.5, 0)
errorCounter.Size = UDim2.new(0, 30, 0, 18)
errorCounter.BackgroundColor3 = Color3.fromRGB(20, 22, 26)
errorCounter.Text = "0"
errorCounter.TextColor3 = Color3.fromRGB(255, 85, 85)
errorCounter.Font = Enum.Font.GothamBold
errorCounter.TextSize = 10
errorCounter.Parent = errorBtn

local errorCounterCorner = Instance.new("UICorner")
errorCounterCorner.CornerRadius = UDim.new(0, 3)
errorCounterCorner.Parent = errorCounter

local errorIcon = Instance.new("ImageLabel")
errorIcon.Name = "icon"
errorIcon.ImageColor3 = Color3.fromRGB(255, 85, 85)
errorIcon.BorderSizePixel = 0
errorIcon.BackgroundTransparency = 1
errorIcon.AnchorPoint = Vector2.new(1, 0.5)
errorIcon.Position = UDim2.new(1, -45, 0.5, 0)
errorIcon.Size = UDim2.new(0, 30, 0, 18)
errorIcon.Image = "rbxassetid://14915625587"
errorIcon.ScaleType = Enum.ScaleType.Fit
errorIcon.Parent = errorBtn


local outputContainer = Instance.new("Frame")
outputContainer.Name = "OutputContainer"
outputContainer.Position = UDim2.new(0, 0, 0, 38)
outputContainer.Size = UDim2.new(1, 0, 1, -76)
outputContainer.BackgroundColor3 = Color3.fromRGB(13, 15, 18)
outputContainer.BorderSizePixel = 0
outputContainer.Parent = content

local outputCornerMain = Instance.new("UICorner")
outputCornerMain.CornerRadius = UDim.new(0, 5)
outputCornerMain.Parent = outputContainer

local scrollFrame = Instance.new("ScrollingFrame")
scrollFrame.Name = "ScrollFrame"
scrollFrame.Size = UDim2.new(1, -8, 1, -8)
scrollFrame.Position = UDim2.new(0, 4, 0, 4)
scrollFrame.BackgroundTransparency = 1
scrollFrame.BorderSizePixel = 0
scrollFrame.ScrollBarThickness = 5
scrollFrame.ScrollBarImageColor3 = Color3.fromRGB(70, 75, 85)
scrollFrame.CanvasSize = UDim2.new(0, 0, 0, 0)
scrollFrame.AutomaticCanvasSize = Enum.AutomaticSize.Y
scrollFrame.Parent = outputContainer

local logContainer = Instance.new("Frame")
logContainer.Name = "LogContainer"
logContainer.Size = UDim2.new(1, -8, 1, 0)
logContainer.BackgroundTransparency = 1
logContainer.AutomaticSize = Enum.AutomaticSize.Y
logContainer.Parent = scrollFrame

local logLayout = Instance.new("UIListLayout")
logLayout.SortOrder = Enum.SortOrder.LayoutOrder
logLayout.Padding = UDim.new(0, 3)
logLayout.Parent = logContainer

local printTemplate = Instance.new("Frame")
printTemplate.Name = "print"
printTemplate.Size = UDim2.new(1, 0, 0, 28)
printTemplate.BackgroundColor3 = Color3.fromRGB(22, 24, 28)
printTemplate.BorderSizePixel = 0
printTemplate.Visible = false
printTemplate.Parent = logContainer

local printCorner = Instance.new("UICorner")
printCorner.CornerRadius = UDim.new(0, 4)
printCorner.Parent = printTemplate

local printText = Instance.new("TextLabel")
printText.Name = "Text"
printText.Size = UDim2.new(1, -32, 1, -4)
printText.Position = UDim2.new(0, 8, 0, 2)
printText.BackgroundTransparency = 1
printText.Text = "[00:00:00] Message"
printText.TextColor3 = Color3.fromRGB(210, 210, 210)
printText.Font = Enum.Font.Code
printText.TextSize = 12
printText.TextXAlignment = Enum.TextXAlignment.Left
printText.TextYAlignment = Enum.TextYAlignment.Top
printText.TextWrapped = true
printText.RichText = true
printText.Parent = printTemplate

local printCopy = Instance.new("ImageButton")
printCopy.Name = "CopyBtn"
printCopy.AnchorPoint = Vector2.new(1, 0.5)
printCopy.Position = UDim2.new(1, -4, 0.5, 0)
printCopy.Size = UDim2.new(0, 20, 0, 20)
printCopy.BackgroundColor3 = Color3.fromRGB(35, 38, 43)
printCopy.BorderSizePixel = 0
printCopy.BackgroundTransparency = 0.7
printCopy.ScaleType = Enum.ScaleType.Fit
printCopy.ImageColor3 = Color3.fromRGB(180, 180, 180)
printCopy.Parent = printTemplate

local printCopyCorner = Instance.new("UICorner")
printCopyCorner.CornerRadius = UDim.new(0, 3)
printCopyCorner.Parent = printCopy

local printCopyIcon = Instance.new("ImageLabel")
printCopyIcon.Name = "icon"
printCopyIcon.ImageColor3 = Color3.fromRGB(180, 180, 180)
printCopyIcon.BorderSizePixel = 0
printCopyIcon.BackgroundTransparency = 1
printCopyIcon.AnchorPoint = Vector2.new(1, 0.5)
printCopyIcon.Position = UDim2.new(1, -3, 0.5, 0)
printCopyIcon.Size = UDim2.new(0, 14, 0, 14)
printCopyIcon.Image = "rbxassetid://13863565650"
printCopyIcon.ScaleType = Enum.ScaleType.Fit
printCopyIcon.Parent = printCopy

local warnTemplate = printTemplate:Clone()
warnTemplate.Name = "warn"
warnTemplate.BackgroundColor3 = Color3.fromRGB(35, 33, 22)
warnTemplate.Text.TextColor3 = Color3.fromRGB(255, 213, 46)
warnTemplate.Parent = logContainer

local errorTemplate = printTemplate:Clone()
errorTemplate.Name = "error"
errorTemplate.BackgroundColor3 = Color3.fromRGB(40, 22, 25)
errorTemplate.Text.TextColor3 = Color3.fromRGB(255, 85, 85)
errorTemplate.Parent = logContainer

local inputArea = Instance.new("Frame")
inputArea.Name = "InputArea"
inputArea.Position = UDim2.new(0, 0, 1, -34)
inputArea.Size = UDim2.new(1, 0, 0, 34)
inputArea.BackgroundTransparency = 1
inputArea.Parent = content

local inputLayout = Instance.new("UIListLayout")
inputLayout.FillDirection = Enum.FillDirection.Horizontal
inputLayout.VerticalAlignment = Enum.VerticalAlignment.Center
inputLayout.Padding = UDim.new(0, 6)
inputLayout.Parent = inputArea

local execClipboardBtn = Instance.new("ImageButton")
execClipboardBtn.Name = "ExecClipboard"
execClipboardBtn.Size = UDim2.new(0, 34, 0, 32)
execClipboardBtn.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
execClipboardBtn.BackgroundTransparency = 0.96
execClipboardBtn.BorderSizePixel = 0
execClipboardBtn.ScaleType = Enum.ScaleType.Fit
execClipboardBtn.LayoutOrder = 0
execClipboardBtn.Parent = inputArea

local execClipCorner = Instance.new("UICorner")
execClipCorner.CornerRadius = UDim.new(0, 5)
execClipCorner.Parent = execClipboardBtn

local execClipIcon = Instance.new("ImageLabel")
execClipIcon.Name = "icon"
execClipIcon.BorderSizePixel = 0
execClipIcon.BackgroundTransparency = 1
execClipIcon.AnchorPoint = Vector2.new(1, 0.5)
execClipIcon.Position = UDim2.new(1, -3, 0.5, 0)
execClipIcon.Size = UDim2.new(0, 28, 0, 20)
execClipIcon.Image = "rbxassetid://13846942895"
execClipIcon.ScaleType = Enum.ScaleType.Fit
execClipIcon.Parent = execClipboardBtn

local clearBtn = Instance.new("ImageButton")
clearBtn.Name = "ClearBtn"
clearBtn.Size = UDim2.new(0, 34, 0, 32)
clearBtn.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
clearBtn.BackgroundTransparency = 0.96
clearBtn.BorderSizePixel = 0
clearBtn.ScaleType = Enum.ScaleType.Fit
clearBtn.LayoutOrder = 1
clearBtn.Parent = inputArea

local clearCorner = Instance.new("UICorner")
clearCorner.CornerRadius = UDim.new(0, 5)
clearCorner.Parent = clearBtn

local clearIcon = Instance.new("ImageLabel")
clearIcon.Name = "icon"
clearIcon.BorderSizePixel = 0
clearIcon.BackgroundTransparency = 1
clearIcon.AnchorPoint = Vector2.new(1, 0.5)
clearIcon.Position = UDim2.new(1, -3, 0.5, 0)
clearIcon.Size = UDim2.new(0, 28, 0, 20)
clearIcon.Image = "rbxassetid://13850263673"
clearIcon.ScaleType = Enum.ScaleType.Fit
clearIcon.Parent = clearBtn

local inputBox = Instance.new("Frame")
inputBox.Name = "InputBox"
inputBox.Size = UDim2.new(1, -80, 0, 32)
inputBox.BackgroundColor3 = Color3.fromRGB(22, 24, 28)
inputBox.BorderSizePixel = 0
inputBox.Parent = inputArea

local inputBoxCorner = Instance.new("UICorner")
inputBoxCorner.CornerRadius = UDim.new(0, 5)
inputBoxCorner.Parent = inputBox

local textInput = Instance.new("TextBox")
textInput.Name = "TextInput"
textInput.Size = UDim2.new(1, -48, 1, 0)
textInput.Position = UDim2.new(0, 8, 0, 0)
textInput.BackgroundTransparency = 1
textInput.Text = ""
textInput.PlaceholderText = "Enter code..."
textInput.TextColor3 = Color3.fromRGB(210, 210, 210)
textInput.PlaceholderColor3 = Color3.fromRGB(110, 110, 110)
textInput.Font = Enum.Font.Code
textInput.TextSize = 12
textInput.TextXAlignment = Enum.TextXAlignment.Left
textInput.ClearTextOnFocus = false
textInput.Parent = inputBox

local execBtn = Instance.new("ImageButton")
execBtn.Name = "ExecBtn"
execBtn.AnchorPoint = Vector2.new(1, 0.5)
execBtn.Position = UDim2.new(1, -4, 0.5, 0)
execBtn.Size = UDim2.new(0, 34, 0, 26)
execBtn.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
execBtn.BackgroundTransparency = 0.96
execBtn.BorderSizePixel = 0
execBtn.ScaleType = Enum.ScaleType.Fit
execBtn.Parent = inputBox

local execBtnCorner = Instance.new("UICorner")
execBtnCorner.CornerRadius = UDim.new(0, 5)
execBtnCorner.Parent = execBtn

local execIcon = Instance.new("ImageLabel")
execIcon.Name = "icon"
execIcon.BorderSizePixel = 0
execIcon.BackgroundTransparency = 1
execIcon.AnchorPoint = Vector2.new(1, 0.5)
execIcon.Position = UDim2.new(1, -3, 0.5, 0)
execIcon.Size = UDim2.new(0, 28, 0, 20)
execIcon.Image = "rbxassetid://13857927803"
execIcon.ScaleType = Enum.ScaleType.Fit
execIcon.Parent = execBtn

local resizeHandle = Instance.new("Frame")
resizeHandle.Name = "ResizeHandle"
resizeHandle.AnchorPoint = Vector2.new(1, 1)
resizeHandle.Position = UDim2.new(1, 0, 1, 0)
resizeHandle.Size = UDim2.new(0, 15, 0, 15)
resizeHandle.BackgroundColor3 = Color3.fromRGB(55, 60, 70)
resizeHandle.BackgroundTransparency = 0.5
resizeHandle.BorderSizePixel = 0
resizeHandle.Parent = consoleWindow

local resizeCorner = Instance.new("UICorner")
resizeCorner.CornerRadius = UDim.new(0, 6)
resizeCorner.Parent = resizeHandle

local roundedCorner = Instance.new("ImageLabel")
roundedCorner.Name = "RoundedCorner"
roundedCorner.Image = "rbxassetid://8445471499"
roundedCorner.ImageRectOffset = Vector2.new(504, 904)
roundedCorner.ImageRectSize = Vector2.new(96, 96)
roundedCorner.Size = UDim2.new(0, 15, 0, 15)
roundedCorner.Rotation = 90
roundedCorner.BackgroundTransparency = 1
roundedCorner.Parent = resizeHandle

local Settings = {
	SHOW_OUTPUT = true,
	SHOW_ERROR = true,
	SHOW_WARNING = true,
	MAX_LOGS = 500,
	AUTO_CLEAR_INPUT = true
}

local logCounts = {
	output = 0,
	warning = 0,
	error = 0
}

local currentErrorFrame = nil
local errorTimeout = nil


local function createTooltip(button, text)
	local tooltip = Instance.new("Frame")
	tooltip.Name = "Tooltip"
	tooltip.Size = UDim2.new(0, 0, 0, 26)
	tooltip.BackgroundColor3 = Color3.fromRGB(30, 33, 38)
	tooltip.BorderSizePixel = 0
	tooltip.AnchorPoint = Vector2.new(0.5, 1)
	tooltip.Position = UDim2.new(0.5, 0, 0, -8)
	tooltip.Visible = false
	tooltip.ZIndex = 1000
	tooltip.Parent = button

	local tooltipCorner = Instance.new("UICorner")
	tooltipCorner.CornerRadius = UDim.new(0, 5)
	tooltipCorner.Parent = tooltip

	local tooltipStroke = Instance.new("UIStroke")
	tooltipStroke.Color = Color3.fromRGB(50, 55, 65)
	tooltipStroke.Thickness = 1
	tooltipStroke.Transparency = 0.5
	tooltipStroke.Parent = tooltip

	local tooltipLabel = Instance.new("TextLabel")
	tooltipLabel.Name = "Text"
	tooltipLabel.Size = UDim2.new(1, -12, 1, 0)
	tooltipLabel.Position = UDim2.new(0, 6, 0, 0)
	tooltipLabel.BackgroundTransparency = 1
	tooltipLabel.Text = text
	tooltipLabel.TextColor3 = Color3.fromRGB(220, 220, 220)
	tooltipLabel.Font = Enum.Font.Gotham
	tooltipLabel.TextSize = 11
	tooltipLabel.TextXAlignment = Enum.TextXAlignment.Center
	tooltipLabel.Parent = tooltip

	task.defer(function()
		local textSize = TextService:GetTextSize(
			text,
			tooltipLabel.TextSize,
			tooltipLabel.Font,
			Vector2.new(math.huge, 26)
		)
		tooltip.Size = UDim2.new(0, textSize.X + 16, 0, 26)
	end)

	button.MouseEnter:Connect(function()
		tooltip.Visible = true
		TweenService:Create(tooltip, TweenInfo.new(0.15, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {
			BackgroundTransparency = 0
		}):Play()
		TweenService:Create(tooltipLabel, TweenInfo.new(0.15, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {
			TextTransparency = 0
		}):Play()
	end)

	button.MouseLeave:Connect(function()
		TweenService:Create(tooltip, TweenInfo.new(0.15, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {
			BackgroundTransparency = 1
		}):Play()
		TweenService:Create(tooltipLabel, TweenInfo.new(0.15, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {
			TextTransparency = 1
		}):Play()
		task.wait(0.15)
		tooltip.Visible = false
	end)
end

local function updateCounters()
	outputCounter.Text = tostring(logCounts.output)
	warningCounter.Text = tostring(logCounts.warning)
	errorCounter.Text = tostring(logCounts.error)
end

local function createLogFrame(message, messageType)
	local template

	if messageType == Enum.MessageType.MessageError and Settings.SHOW_ERROR then
		template = errorTemplate:Clone()
		logCounts.error = logCounts.error + 1
		currentErrorFrame = template

		if errorTimeout then
			errorTimeout:Disconnect()
		end
		errorTimeout = game:GetService("RunService").Heartbeat:Connect(function()
			wait(5)
			currentErrorFrame = nil
			errorTimeout:Disconnect()
		end)
	elseif messageType == Enum.MessageType.MessageInfo and currentErrorFrame and Settings.SHOW_ERROR then
		template = currentErrorFrame
		template.Text.Text = template.Text.Text .. "\n" .. message

		task.wait()
		local availableWidth = scrollFrame.AbsoluteSize.X - 40
		local textSize = TextService:GetTextSize(
			template.Text.Text,
			template.Text.TextSize,
			template.Text.Font,
			Vector2.new(availableWidth, math.huge)
		)
		local frameHeight = math.max(textSize.Y + 10, 28)
		template.Size = UDim2.new(1, 0, 0, frameHeight)

		return template
	elseif messageType == Enum.MessageType.MessageWarning and Settings.SHOW_WARNING then
		template = warnTemplate:Clone()
		logCounts.warning = logCounts.warning + 1
	elseif Settings.SHOW_OUTPUT then
		template = printTemplate:Clone()
		logCounts.output = logCounts.output + 1
	else
		return nil
	end

	if template then
		template.Visible = true
		local timestamp = os.date("%X")
		template.Text.Text = "[" .. timestamp .. "] " .. message

		template.Parent = logContainer
		task.wait()

		local availableWidth = scrollFrame.AbsoluteSize.X - 40
		local textSize = TextService:GetTextSize(
			template.Text.Text,
			template.Text.TextSize,
			template.Text.Font,
			Vector2.new(availableWidth, math.huge)
		)

		local frameHeight = math.max(textSize.Y + 10, 28)
		template.Size = UDim2.new(1, 0, 0, frameHeight)

		template.CopyBtn.Activated:Connect(function()
			if setclipboard then
				setclipboard(template.Text.Text)
				template.CopyBtn.ImageColor3 = Color3.fromRGB(100, 255, 100)
				task.wait(0.5)
				template.CopyBtn.ImageColor3 = Color3.fromRGB(180, 180, 180)
			end
		end)

	end

	updateCounters()
	return template
end

local function clearConsole()
	for _, child in ipairs(logContainer:GetChildren()) do
		if child:IsA("Frame") and child.Visible and child ~= printTemplate and child ~= warnTemplate and child ~= errorTemplate then
			child:Destroy()
		end
	end
	logCounts = { output = 0, warning = 0, error = 0 }
	currentErrorFrame = nil
	updateCounters()
end

local function toggleFilter(filterType, button)
	Settings[filterType] = not Settings[filterType]
	button.ImageTransparency = Settings[filterType] and 0 or 0.6
end

local function consolePrint(...)
	local args = {...}
	local message = ""
	for i, v in ipairs(args) do
		message = message .. tostring(v)
		if i < #args then
			message = message .. " "
		end
	end
	createLogFrame(message, Enum.MessageType.MessageOutput)
	task.wait()
	scrollFrame.CanvasPosition = Vector2.new(0, scrollFrame.AbsoluteCanvasSize.Y)
end

local function consoleWarn(...)
	local args = {...}
	local message = ""
	for i, v in ipairs(args) do
		message = message .. tostring(v)
		if i < #args then
			message = message .. " "
		end
	end
	createLogFrame(message, Enum.MessageType.MessageWarning)
	task.wait()
	scrollFrame.CanvasPosition = Vector2.new(0, scrollFrame.AbsoluteCanvasSize.Y)
end

local function consoleError(...)
	local args = {...}
	local message = ""
	for i, v in ipairs(args) do
		message = message .. tostring(v)
		if i < #args then
			message = message .. " "
		end
	end
	createLogFrame(message, Enum.MessageType.MessageError)
	task.wait()
	scrollFrame.CanvasPosition = Vector2.new(0, scrollFrame.AbsoluteCanvasSize.Y)
end

getgenv().__trigon_console_print = function(...)
	local args = {...}
	task.spawn(function()
		local message = ""
		for i, v in ipairs(args) do
			message = message .. tostring(v)
			if i < #args then
				message = message .. " "
			end
		end
		createLogFrame(message, Enum.MessageType.MessageOutput)
		scrollFrame.CanvasPosition = Vector2.new(0, scrollFrame.AbsoluteCanvasSize.Y)
	end)
end

getgenv().__trigon_console_warn = function(...)
	local args = {...}
	task.spawn(function()
		local message = ""
		for i, v in ipairs(args) do
			message = message .. tostring(v)
			if i < #args then
				message = message .. " "
			end
		end
		createLogFrame(message, Enum.MessageType.MessageWarning)
		scrollFrame.CanvasPosition = Vector2.new(0, scrollFrame.AbsoluteCanvasSize.Y)
	end)
end

getgenv().__trigon_console_error = function(...)
	local args = {...}
	task.spawn(function()
		local message = ""
		for i, v in ipairs(args) do
			message = message .. tostring(v)
			if i < #args then
				message = message .. " "
			end
		end
		createLogFrame(message, Enum.MessageType.MessageError)
		scrollFrame.CanvasPosition = Vector2.new(0, scrollFrame.AbsoluteCanvasSize.Y)
	end)
end

local function executeWithRedirect(code)
	local wrappedCode = [[
(function()
	local print = getgenv().__trigon_console_print
	local warn = getgenv().__trigon_console_warn
	local error = getgenv().__trigon_console_error

]] .. code .. [[

end)()
]]

	local success, result = pcall(function()
		executecode(wrappedCode)
	end)

	if not success then
		getgenv().__trigon_console_error("Execution error: " .. tostring(result))
		return false
	end

	return true
end

LogService.MessageOut:Connect(function(message, messageType)
	local frame = createLogFrame(message, messageType)
	if frame then
		task.wait()
		scrollFrame.CanvasPosition = Vector2.new(0, scrollFrame.AbsoluteCanvasSize.Y)
	end
end)

clearBtn.Activated:Connect(clearConsole)

execClipboardBtn.Activated:Connect(function()
	local code = getclipboard()
	if code and code ~= "" then
		consolePrint("Executing from clipboard...")
		executeWithRedirect(code)
	else
		consoleWarn("Clipboard is empty")
	end
end)

execBtn.Activated:Connect(function()
	if textInput.Text ~= "" then
		consolePrint("Executing code...")
		executeWithRedirect(textInput.Text)
		if Settings.AUTO_CLEAR_INPUT then
			textInput.Text = ""
		end
	end
end)

textInput.FocusLost:Connect(function(enterPressed)
	if enterPressed and textInput.Text ~= "" then
		consolePrint("Executing code...")
		executeWithRedirect(textInput.Text)
		if Settings.AUTO_CLEAR_INPUT then
			textInput.Text = ""
		end
	end
end)

outputBtn.Activated:Connect(function()
	toggleFilter("SHOW_OUTPUT", outputBtn)
end)

warningBtn.Activated:Connect(function()
	toggleFilter("SHOW_WARNING", warningBtn)
end)

errorBtn.Activated:Connect(function()
	toggleFilter("SHOW_ERROR", errorBtn)
end)

createTooltip(execClipboardBtn, "Execute Clipboard")
createTooltip(clearBtn, "Clear Console")
createTooltip(execBtn, "Execute Code")
createTooltip(minimizeBtn, "Minimize Console")
createTooltip(outputBtn, "Toggle Output Messages")
createTooltip(warningBtn, "Toggle Warning Messages")
createTooltip(errorBtn, "Toggle Error Messages")

local function updateWindowSize()
	local viewportSize = workspace.CurrentCamera.ViewportSize

	if viewportSize.X < 500 or viewportSize.Y < 400 then
		consoleWindow.Size = UDim2.new(0.95, 0, 0.85, 0)
		sizeConstraint.MinSize = Vector2.new(280, 200)
	elseif viewportSize.X < 700 then
		consoleWindow.Size = UDim2.new(0.85, 0, 0.75, 0)
		sizeConstraint.MinSize = Vector2.new(300, 220)
	else
		consoleWindow.Size = UDim2.new(0, 520, 0, 360)
	end

	consoleWindow.Position = UDim2.new(0.5, 0, 0.5, 0)
end

updateWindowSize()
workspace.CurrentCamera:GetPropertyChangedSignal("ViewportSize"):Connect(updateWindowSize)

local isMinimized = false
local isMobile = UserInputService.TouchEnabled and not UserInputService.MouseEnabled

local function toggleMinimize()
	isMinimized = not isMinimized

	if isMinimized then
		TweenService:Create(consoleWindow, TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {
			Position = UDim2.new(-0.5, 0, 0.5, 0)
		}):Play()

		task.wait(0.3)
		consoleWindow.Visible = false
		minButton.Visible = true

		minButton.Size = UDim2.new(0, 0, 0, 0)
		TweenService:Create(minButton, TweenInfo.new(0.3, Enum.EasingStyle.Back, Enum.EasingDirection.Out), {
			Size = UDim2.new(0, 60, 0, 60)
		}):Play()
	else
		TweenService:Create(minButton, TweenInfo.new(0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {
			Size = UDim2.new(0, 0, 0, 0)
		}):Play()

		task.wait(0.2)
		minButton.Visible = false
		consoleWindow.Visible = true

		updateWindowSize()
		consoleWindow.Position = UDim2.new(1.5, 0, 0.5, 0)

		TweenService:Create(consoleWindow, TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {
			Position = UDim2.new(0.5, 0, 0.5, 0)
		}):Play()
	end
end

minimizeBtn.Activated:Connect(toggleMinimize)
minButton.Activated:Connect(toggleMinimize)

local isDraggingMin = false
local dragStartMin, startPosMin

minButton.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		isDraggingMin = true
		dragStartMin = input.Position
		startPosMin = minButton.Position

		TweenService:Create(minButton, TweenInfo.new(0.1), {Size = UDim2.new(0, 66, 0, 66)}):Play()

		input.Changed:Connect(function()
			if input.UserInputState == Enum.UserInputState.End then
				isDraggingMin = false
				TweenService:Create(minButton, TweenInfo.new(0.1), {Size = UDim2.new(0, 60, 0, 60)}):Play()
			end
		end)
	end
end)

minButton.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch then
		if isDraggingMin then
			local delta = input.Position - dragStartMin
			minButton.Position = UDim2.new(
				startPosMin.X.Scale,
				startPosMin.X.Offset + delta.X,
				startPosMin.Y.Scale,
				startPosMin.Y.Offset + delta.Y
			)
		end
	end
end)

local dragging = false
local dragInput, mousePos, framePos

titleBar.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		dragging = true
		mousePos = input.Position
		framePos = consoleWindow.Position

		input.Changed:Connect(function()
			if input.UserInputState == Enum.UserInputState.End then
				dragging = false
			end
		end)
	end
end)

titleBar.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch then
		dragInput = input
	end
end)

UserInputService.InputChanged:Connect(function(input)
	if input == dragInput and dragging then
		local delta = input.Position - mousePos
		consoleWindow.Position = UDim2.new(
			framePos.X.Scale,
			framePos.X.Offset + delta.X,
			framePos.Y.Scale,
			framePos.Y.Offset + delta.Y
		)
	end
end)

local resizing = false
local resizeStart, windowSizeStart

resizeHandle.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		resizing = true
		resizeStart = input.Position
		windowSizeStart = consoleWindow.AbsoluteSize

		input.Changed:Connect(function()
			if input.UserInputState == Enum.UserInputState.End then
				resizing = false
			end
		end)
	end
end)

UserInputService.InputChanged:Connect(function(input)
	if resizing and (input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch) then
		local delta = input.Position - resizeStart
		local newWidth = math.clamp(windowSizeStart.X + delta.X, sizeConstraint.MinSize.X, sizeConstraint.MaxSize.X)
		local newHeight = math.clamp(windowSizeStart.Y + delta.Y, sizeConstraint.MinSize.Y, sizeConstraint.MaxSize.Y)

		consoleWindow.Size = UDim2.new(0, newWidth, 0, newHeight)
	end
end)

task.defer(function()
	consolePrint("Trigon Console ready!")
	consolePrint("Code executed from this console will only show output here.")
end)
