local API = rawget(_G, "LIDCOUNCIL_API")
local INTERNAL_API = type(API) == "table" and type(API.__internal) == "table" and API.__internal or nil
if type(INTERNAL_API) ~= "table" then
  error("LidCouncil missing internal API registry.")
end

local RequireApi = INTERNAL_API.LIDCOUNCIL_RequireApi_Impl
if type(RequireApi) ~= "function" then
  error("LidCouncil missing shared editor page helpers.")
end

local DB = RequireApi("LIDCOUNCIL_DB", _G.LIDCOUNCIL_DB)
local ApplyFrameSkin = RequireApi("LIDCOUNCIL_ApplyFrameSkin", _G.LIDCOUNCIL_ApplyFrameSkin)
local OpenColorPickerRGBOnly = RequireApi("LIDCOUNCIL_OpenColorPickerRGBOnly", _G.LIDCOUNCIL_OpenColorPickerRGBOnly)
local MakeYellowSubHeader = RequireApi("LIDCOUNCIL_MakeYellowSubHeader", _G.LIDCOUNCIL_MakeYellowSubHeader)
local MakeSlider = RequireApi("LIDCOUNCIL_MakeSlider", _G.LIDCOUNCIL_MakeSlider)
local RebuildAllPanels = RequireApi("LIDCOUNCIL_RebuildAllPanels", _G.LIDCOUNCIL_RebuildAllPanels)
local GetSelectedPanelName = RequireApi("LIDCOUNCIL_GetSelectedPanelName", _G.LIDCOUNCIL_GetSelectedPanelName)

local function BuildPanelDiagnosticsSection(editor, props, deps)
  if not editor or not props then return end

  deps = deps or {}
  local EnsurePanelExtraData = deps.EnsurePanelExtraData
  local PANEL_STAT_CHOICES = deps.panelStatChoices or {}
  local PANEL_SPECIAL_FONT_CHOICES = deps.panelSpecialFontChoices or {}
  if type(EnsurePanelExtraData) ~= "function" then
    error("LidCouncil missing diagnostics section dependency: EnsurePanelExtraData")
  end

  local diagHeader = MakeYellowSubHeader(props, "Diagnostic Panel", 0, -760)
  editor.diagHeader = diagHeader

  local diagFpsGraphCheck = CreateFrame("CheckButton", "LIDCOUNCIL_DiagFpsGraphCheck", props, "UICheckButtonTemplate")
  diagFpsGraphCheck:SetPoint("TOPLEFT", 0, -784)
  _G[diagFpsGraphCheck:GetName() .. "Text"]:SetText("FPS Graph")
  editor.diagFpsGraphCheck = diagFpsGraphCheck

  local diagGraphSettingsLabel = props:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  diagGraphSettingsLabel:SetText("Add Info:")
  editor.diagGraphSettingsLabel = diagGraphSettingsLabel

  local function SetDiagGraphFlag(flag, checked)
    if not GetSelectedPanelName() then return end
    local p = DB().panels[GetSelectedPanelName()]
    if not p then return end
    local diag = EnsurePanelExtraData(p)
    diag.graph[flag] = checked and true or false
    if editor and editor.UpdateColorDesignUI then editor:UpdateColorDesignUI() end
    RebuildAllPanels()
  end

  local function GetDiagInfoIntervalDefaults(metric)
    if metric == "AVG" then return 10 end
    return 0.1
  end

  local function GetDiagInfoIntervalKey(metric)
    if metric == "AVG" then return "infoIntervalAVG" end
    if metric == "LOW" then return "infoIntervalLOW" end
    return "infoIntervalFPS"
  end

  local function GetDiagInfoInterval(diag, metric)
    if not diag or not diag.graph then
      return GetDiagInfoIntervalDefaults(metric)
    end
    local key = GetDiagInfoIntervalKey(metric)
    local val = tonumber(diag.graph[key]) or GetDiagInfoIntervalDefaults(metric)
    if val < 0.1 then val = 0.1 end
    if val > 300 then val = 300 end
    val = math.floor((val * 10) + 0.5) / 10
    return val
  end

  local function SetDiagInfoInterval(diag, metric, value)
    if not diag or not diag.graph then return end
    local key = GetDiagInfoIntervalKey(metric)
    local val = tonumber(value) or GetDiagInfoIntervalDefaults(metric)
    if val < 0.1 then val = 0.1 end
    if val > 300 then val = 300 end
    val = math.floor((val * 10) + 0.5) / 10
    diag.graph[key] = val
  end

  local function EnsureDiagInfoIntervalPopup()
    if editor.diagInfoIntervalPopup then return editor.diagInfoIntervalPopup end
    local dlg = CreateFrame("Frame", nil, editor, "BackdropTemplate")
    dlg:SetSize(420, 170)
    dlg:SetPoint("CENTER")
    dlg:SetFrameStrata("DIALOG")
    dlg:SetFrameLevel((editor:GetFrameLevel() or 1) + 30)
    ApplyFrameSkin(dlg)
    dlg:Hide()

    local title = dlg:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
    title:SetPoint("TOPLEFT", 12, -10)
    title:SetText("Set Intervall")
    dlg.title = title

    local desc = dlg:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
    desc:SetPoint("TOPLEFT", 12, -40)
    desc:SetWidth(396)
    desc:SetJustifyH("LEFT")
    desc:SetText("")
    dlg.desc = desc

    local edit = CreateFrame("EditBox", nil, dlg, "InputBoxTemplate")
    edit:SetSize(80, 20)
    edit:SetPoint("TOPLEFT", 12, -86)
    edit:SetAutoFocus(false)
    dlg.edit = edit

    local ok = CreateFrame("Button", nil, dlg, "UIPanelButtonTemplate")
    ok:SetSize(120, 22)
    ok:SetPoint("BOTTOMRIGHT", -12, 12)
    ok:SetText("Apply")
    dlg.okBtn = ok

    local cancel = CreateFrame("Button", nil, dlg, "UIPanelButtonTemplate")
    cancel:SetSize(120, 22)
    cancel:SetPoint("RIGHT", ok, "LEFT", -8, 0)
    cancel:SetText("Cancel")
    cancel:SetScript("OnClick", function() dlg:Hide() end)
    dlg.cancelBtn = cancel

    ok:SetScript("OnClick", function()
      if not GetSelectedPanelName() then dlg:Hide() return end
      local p = DB().panels[GetSelectedPanelName()]
      if not p then dlg:Hide() return end
      local diag = EnsurePanelExtraData(p)
      local raw = tonumber(dlg.edit:GetText() or "")
      if not raw then raw = dlg.defaultVal or 0.1 end
      SetDiagInfoInterval(diag, dlg.metric or "FPS", raw)
      dlg:Hide()
      RebuildAllPanels()
      if editor and editor.UpdateColorDesignUI then editor:UpdateColorDesignUI() end
    end)

    editor.diagInfoIntervalPopup = dlg
    return dlg
  end

  local function OpenDiagInfoIntervalPopup(metric, defaultVal)
    if not GetSelectedPanelName() then return end
    local p = DB().panels[GetSelectedPanelName()]
    if not p then return end
    local diag = EnsurePanelExtraData(p)
    local cur = GetDiagInfoInterval(diag, metric)
    local dlg = EnsureDiagInfoIntervalPopup()
    dlg.metric = metric
    dlg.defaultVal = tonumber(defaultVal) or 0.1
    dlg.desc:SetText(string.format("Define the intervall: (Default = %.1fs)", dlg.defaultVal))
    dlg.edit:SetText(string.format("%.1f", cur))
    dlg:Show()
    if editor and editor.ApplyTheme then editor:ApplyTheme() end
  end

  local diagGraphShowFpsCheck = CreateFrame("CheckButton", "LIDCOUNCIL_DiagGraphShowFPSCheck", props, "UICheckButtonTemplate")
  _G[diagGraphShowFpsCheck:GetName() .. "Text"]:SetText("Show FPS")
  diagGraphShowFpsCheck:SetScript("OnClick", function(self)
    SetDiagGraphFlag("showFPS", self:GetChecked())
  end)
  editor.diagGraphShowFpsCheck = diagGraphShowFpsCheck

  local diagGraphFpsIntervalBtn = CreateFrame("Button", nil, props, "UIPanelButtonTemplate")
  diagGraphFpsIntervalBtn:SetSize(100, 20)
  diagGraphFpsIntervalBtn:SetText("Set Intervall")
  diagGraphFpsIntervalBtn:SetScript("OnClick", function()
    OpenDiagInfoIntervalPopup("FPS", 0.1)
  end)
  editor.diagGraphFpsIntervalBtn = diagGraphFpsIntervalBtn

  local diagGraphShowAvgCheck = CreateFrame("CheckButton", "LIDCOUNCIL_DiagGraphShowAvgCheck", props, "UICheckButtonTemplate")
  _G[diagGraphShowAvgCheck:GetName() .. "Text"]:SetText("AVG FPS")
  diagGraphShowAvgCheck:SetScript("OnClick", function(self)
    SetDiagGraphFlag("showAvg", self:GetChecked())
  end)
  editor.diagGraphShowAvgCheck = diagGraphShowAvgCheck

  local diagGraphAvgIntervalBtn = CreateFrame("Button", nil, props, "UIPanelButtonTemplate")
  diagGraphAvgIntervalBtn:SetSize(100, 20)
  diagGraphAvgIntervalBtn:SetText("Set Intervall")
  diagGraphAvgIntervalBtn:SetScript("OnClick", function()
    OpenDiagInfoIntervalPopup("AVG", 10)
  end)
  editor.diagGraphAvgIntervalBtn = diagGraphAvgIntervalBtn

  local diagGraphShowLowCheck = CreateFrame("CheckButton", "LIDCOUNCIL_DiagGraphShowLowCheck", props, "UICheckButtonTemplate")
  _G[diagGraphShowLowCheck:GetName() .. "Text"]:SetText("Lowest FPS")
  diagGraphShowLowCheck:SetScript("OnClick", function(self)
    SetDiagGraphFlag("showLow", self:GetChecked())
  end)
  editor.diagGraphShowLowCheck = diagGraphShowLowCheck

  local diagGraphLowIntervalBtn = CreateFrame("Button", nil, props, "UIPanelButtonTemplate")
  diagGraphLowIntervalBtn:SetSize(100, 20)
  diagGraphLowIntervalBtn:SetText("Set Intervall")
  diagGraphLowIntervalBtn:SetScript("OnClick", function()
    OpenDiagInfoIntervalPopup("LOW", 0.1)
  end)
  editor.diagGraphLowIntervalBtn = diagGraphLowIntervalBtn

  local function CommitDiagGraphInterval()
    if not GetSelectedPanelName() then return end
    local p = DB().panels[GetSelectedPanelName()]
    if not p then return end
    local diag = EnsurePanelExtraData(p)
    local raw = tonumber(editor.diagGraphUpdateEdit and editor.diagGraphUpdateEdit:GetText())
    if not raw then raw = diag.graph.updateInterval or 0.1 end
    if raw < 0.1 then raw = 0.1 end
    if raw > 2.0 then raw = 2.0 end
    raw = math.floor((raw * 10) + 0.5) / 10
    diag.graph.updateInterval = raw
    if editor.diagGraphUpdateEdit then
      editor.diagGraphUpdateEdit:SetText(string.format("%.1f", raw))
    end
    RebuildAllPanels()
  end

  local diagGraphUpdateLabel = props:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  diagGraphUpdateLabel:SetText("Graph Refresh (sec):")
  editor.diagGraphUpdateLabel = diagGraphUpdateLabel

  local diagGraphUpdateEdit = CreateFrame("EditBox", nil, props, "InputBoxTemplate")
  diagGraphUpdateEdit:SetSize(52, 20)
  diagGraphUpdateEdit:SetAutoFocus(false)
  diagGraphUpdateEdit:SetScript("OnEnterPressed", function(self)
    self:ClearFocus()
    CommitDiagGraphInterval()
  end)
  diagGraphUpdateEdit:SetScript("OnEditFocusLost", function()
    CommitDiagGraphInterval()
  end)
  editor.diagGraphUpdateEdit = diagGraphUpdateEdit

  local diagGraphStyleLabel = props:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  diagGraphStyleLabel:SetText("Graph style:")
  editor.diagGraphStyleLabel = diagGraphStyleLabel

  local diagGraphStyleDrop = CreateFrame("Frame", "LIDCOUNCIL_DiagGraphStyleDrop", props, "UIDropDownMenuTemplate")
  UIDropDownMenu_Initialize(diagGraphStyleDrop, function(self, level)
    if level ~= 1 then return end
    local cur = "BARS"
    if GetSelectedPanelName() then
      local p = DB().panels[GetSelectedPanelName()]
      if p then
        local diag = EnsurePanelExtraData(p)
        cur = diag.graph.style or "BARS"
      end
    end
    local opts = {
      { text = "Bars", value = "BARS" },
      { text = "Wave (Smooth)", value = "WAVE" },
    }
    for _, opt in ipairs(opts) do
      local info = UIDropDownMenu_CreateInfo()
      info.text = opt.text
      info.value = opt.value
      info.checked = (cur == opt.value)
      info.func = function()
        UIDropDownMenu_SetSelectedValue(diagGraphStyleDrop, opt.value)
        UIDropDownMenu_SetText(diagGraphStyleDrop, opt.text)
        if not GetSelectedPanelName() then return end
        local p = DB().panels[GetSelectedPanelName()]
        if not p then return end
        local diag = EnsurePanelExtraData(p)
        diag.graph.style = opt.value
        RebuildAllPanels()
        if editor and editor.UpdateColorDesignUI then editor:UpdateColorDesignUI() end
      end
      UIDropDownMenu_AddButton(info, level)
    end
  end)
  UIDropDownMenu_SetWidth(diagGraphStyleDrop, 180)
  editor.diagGraphStyleDrop = diagGraphStyleDrop

  local specialHeader = MakeYellowSubHeader(props, "Informations", 0, -834)
  editor.specialHeader = specialHeader

  local function SetSpecialMode(newMode, checked)
    if not GetSelectedPanelName() then return end
    local p = DB().panels[GetSelectedPanelName()]
    if not p then return end
    local diag, special = EnsurePanelExtraData(p)
    if checked then
      special.mode = newMode
      if newMode == "CUSTOM_TEXT" and tostring(special.customText and special.customText.text or "") == "" then
        special.customText.text = "Custom Text"
      end
    elseif special.mode == newMode then
      special.mode = "NONE"
    end
    special.showGold = (special.mode == "GOLD")
    special.showFPS = (special.mode == "FPS")
    special.showStats = (special.mode == "STATS")
    special.showCustomText = (special.mode == "CUSTOM_TEXT")
    diag.fpsGraph = (special.mode == "FPS_GRAPH")
    if editor and editor.UpdateColorDesignUI then editor:UpdateColorDesignUI() end
    RebuildAllPanels()
  end

  local specialGoldCheck = CreateFrame("CheckButton", "LIDCOUNCIL_SpecialGoldCheck", props, "UICheckButtonTemplate")
  specialGoldCheck:SetPoint("TOPLEFT", 0, -858)
  _G[specialGoldCheck:GetName() .. "Text"]:SetText("Show Gold")
  specialGoldCheck:SetScript("OnClick", function(self)
    SetSpecialMode("GOLD", self:GetChecked())
  end)
  editor.specialGoldCheck = specialGoldCheck

  local specialFpsCheck = CreateFrame("CheckButton", "LIDCOUNCIL_SpecialFPSCheck", props, "UICheckButtonTemplate")
  specialFpsCheck:SetPoint("TOPLEFT", 0, -880)
  _G[specialFpsCheck:GetName() .. "Text"]:SetText("Show FPS")
  specialFpsCheck:SetScript("OnClick", function(self)
    SetSpecialMode("FPS", self:GetChecked())
  end)
  editor.specialFpsCheck = specialFpsCheck

  local specialStatsCheck = CreateFrame("CheckButton", "LIDCOUNCIL_SpecialStatsCheck", props, "UICheckButtonTemplate")
  specialStatsCheck:SetPoint("TOPLEFT", 0, -902)
  _G[specialStatsCheck:GetName() .. "Text"]:SetText("Show Stats")
  specialStatsCheck:SetScript("OnClick", function(self)
    SetSpecialMode("STATS", self:GetChecked())
  end)
  editor.specialStatsCheck = specialStatsCheck

  local specialCustomTextCheck = CreateFrame("CheckButton", "LIDCOUNCIL_SpecialCustomTextCheck", props, "UICheckButtonTemplate")
  specialCustomTextCheck:SetPoint("TOPLEFT", 0, -924)
  _G[specialCustomTextCheck:GetName() .. "Text"]:SetText("Add Custom Text")
  specialCustomTextCheck:SetScript("OnClick", function(self)
    SetSpecialMode("CUSTOM_TEXT", self:GetChecked())
  end)
  editor.specialCustomTextCheck = specialCustomTextCheck

  diagFpsGraphCheck:SetScript("OnClick", function(self)
    SetSpecialMode("FPS_GRAPH", self:GetChecked())
  end)

  local function PickSpecialColor(current, applyFn)
    local c = current or {1, 1, 1, 1}
    OpenColorPickerRGBOnly({ c[1] or 1, c[2] or 1, c[3] or 1 }, function(r, g, b)
      applyFn(r, g, b)
      RebuildAllPanels()
    end)
  end

  local function SetSpecialFont(pathKey, targetPath)
    if not GetSelectedPanelName() then return end
    local p = DB().panels[GetSelectedPanelName()]
    if not p then return end
    local _, special = EnsurePanelExtraData(p)
    targetPath(special, pathKey)
    RebuildAllPanels()
    if editor and editor.UpdateColorDesignUI then editor:UpdateColorDesignUI() end
  end

  local function InitSpecialFontDropdown(drop, getter, setter)
    UIDropDownMenu_Initialize(drop, function(self, level)
      if level ~= 1 then return end
      local cur = "GameFontHighlightSmall"
      if GetSelectedPanelName() then
        local p = DB().panels[GetSelectedPanelName()]
        local _, special = EnsurePanelExtraData(p)
        cur = getter(special) or cur
      end
      for _, key in ipairs(PANEL_SPECIAL_FONT_CHOICES) do
        if _G[key] then
          local info = UIDropDownMenu_CreateInfo()
          info.text = key
          info.value = key
          info.func = function()
            UIDropDownMenu_SetSelectedValue(drop, key)
            UIDropDownMenu_SetText(drop, key)
            SetSpecialFont(key, setter)
          end
          info.checked = (key == cur)
          UIDropDownMenu_AddButton(info, level)
        end
      end
    end)
    UIDropDownMenu_SetWidth(drop, 220)
  end

  local specialGoldColorBtn = CreateFrame("Button", nil, props, "UIPanelButtonTemplate")
  specialGoldColorBtn:SetSize(100, 22)
  specialGoldColorBtn:SetText("Color")
  specialGoldColorBtn:SetScript("OnClick", function()
    if not GetSelectedPanelName() then return end
    local p = DB().panels[GetSelectedPanelName()]
    if not p then return end
    local _, special = EnsurePanelExtraData(p)
    PickSpecialColor(special.gold.color, function(r, g, b)
      special.gold.color[1], special.gold.color[2], special.gold.color[3] = r, g, b
    end)
  end)
  editor.specialGoldColorBtn = specialGoldColorBtn

  local specialGoldConfigLabel = props:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  specialGoldConfigLabel:SetText("Set Color and Font")
  editor.specialGoldConfigLabel = specialGoldConfigLabel

  local specialGoldFontLabel = props:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  specialGoldFontLabel:SetText("Font:")
  editor.specialGoldFontLabel = specialGoldFontLabel

  local specialGoldFontDrop = CreateFrame("Frame", "LIDCOUNCIL_SpecialGoldFontDrop", props, "UIDropDownMenuTemplate")
  InitSpecialFontDropdown(specialGoldFontDrop, function(special) return special.gold.font end, function(special, key)
    special.gold.font = key
  end)
  editor.specialGoldFontDrop = specialGoldFontDrop

  local specialGoldSizeSlider = MakeSlider(props, "Font Size", 0, -1030, 8, 40, 1, function(val)
    if not GetSelectedPanelName() then return end
    local p = DB().panels[GetSelectedPanelName()]
    if not p then return end
    local _, special = EnsurePanelExtraData(p)
    special.gold.size = math.floor((tonumber(val) or 14) + 0.5)
    RebuildAllPanels()
  end)
  editor.specialGoldSizeSlider = specialGoldSizeSlider

  local specialGoldAlphaSlider = MakeSlider(props, "Transparency (%)", 0, -1078, 0, 100, 1, function(val)
    if not GetSelectedPanelName() then return end
    local p = DB().panels[GetSelectedPanelName()]
    if not p then return end
    local _, special = EnsurePanelExtraData(p)
    special.gold.color[4] = math.max(0, math.min(1, (tonumber(val) or 100) / 100))
    RebuildAllPanels()
  end)
  editor.specialGoldAlphaSlider = specialGoldAlphaSlider

  local specialFpsFontLabel = props:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  specialFpsFontLabel:SetText("Font:")
  editor.specialFpsFontLabel = specialFpsFontLabel

  local specialFpsFontDrop = CreateFrame("Frame", "LIDCOUNCIL_SpecialFPSFontDrop", props, "UIDropDownMenuTemplate")
  InitSpecialFontDropdown(specialFpsFontDrop, function(special) return special.fps.font end, function(special, key)
    special.fps.font = key
  end)
  editor.specialFpsFontDrop = specialFpsFontDrop

  local specialFpsSizeSlider = MakeSlider(props, "Font Size", 0, -1030, 8, 40, 1, function(val)
    if not GetSelectedPanelName() then return end
    local p = DB().panels[GetSelectedPanelName()]
    if not p then return end
    local _, special = EnsurePanelExtraData(p)
    special.fps.size = math.floor((tonumber(val) or 14) + 0.5)
    RebuildAllPanels()
  end)
  editor.specialFpsSizeSlider = specialFpsSizeSlider

  local specialFpsAlphaSlider = MakeSlider(props, "Transparency (%)", 0, -1078, 0, 100, 1, function(val)
    if not GetSelectedPanelName() then return end
    local p = DB().panels[GetSelectedPanelName()]
    if not p then return end
    local _, special = EnsurePanelExtraData(p)
    special.fps.alpha = math.max(0, math.min(1, (tonumber(val) or 100) / 100))
    RebuildAllPanels()
  end)
  editor.specialFpsAlphaSlider = specialFpsAlphaSlider

  local specialFpsColorLabel = props:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  specialFpsColorLabel:SetText("Set threshhold and colors:")
  editor.specialFpsColorLabel = specialFpsColorLabel

  local specialFpsLowColorBtn = CreateFrame("Button", nil, props, "UIPanelButtonTemplate")
  specialFpsLowColorBtn:SetSize(100, 22)
  specialFpsLowColorBtn:SetText("Color")
  specialFpsLowColorBtn:SetScript("OnClick", function()
    if not GetSelectedPanelName() then return end
    local p = DB().panels[GetSelectedPanelName()]
    if not p then return end
    local _, special = EnsurePanelExtraData(p)
    PickSpecialColor(special.fps.lowColor, function(r, g, b)
      special.fps.lowColor[1], special.fps.lowColor[2], special.fps.lowColor[3] = r, g, b
    end)
  end)
  editor.specialFpsLowColorBtn = specialFpsLowColorBtn

  local specialFpsMidColorBtn = CreateFrame("Button", nil, props, "UIPanelButtonTemplate")
  specialFpsMidColorBtn:SetSize(100, 22)
  specialFpsMidColorBtn:SetText("Color")
  specialFpsMidColorBtn:SetScript("OnClick", function()
    if not GetSelectedPanelName() then return end
    local p = DB().panels[GetSelectedPanelName()]
    if not p then return end
    local _, special = EnsurePanelExtraData(p)
    PickSpecialColor(special.fps.midColor, function(r, g, b)
      special.fps.midColor[1], special.fps.midColor[2], special.fps.midColor[3] = r, g, b
    end)
  end)
  editor.specialFpsMidColorBtn = specialFpsMidColorBtn

  local specialFpsHighColorBtn = CreateFrame("Button", nil, props, "UIPanelButtonTemplate")
  specialFpsHighColorBtn:SetSize(100, 22)
  specialFpsHighColorBtn:SetText("Color")
  specialFpsHighColorBtn:SetScript("OnClick", function()
    if not GetSelectedPanelName() then return end
    local p = DB().panels[GetSelectedPanelName()]
    if not p then return end
    local _, special = EnsurePanelExtraData(p)
    PickSpecialColor(special.fps.highColor, function(r, g, b)
      special.fps.highColor[1], special.fps.highColor[2], special.fps.highColor[3] = r, g, b
    end)
  end)
  editor.specialFpsHighColorBtn = specialFpsHighColorBtn

  local specialFpsRuleLabel = props:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  specialFpsRuleLabel:SetText("")
  editor.specialFpsRuleLabel = specialFpsRuleLabel

  local specialFpsResetBtn = CreateFrame("Button", nil, props, "UIPanelButtonTemplate")
  specialFpsResetBtn:SetSize(220, 22)
  specialFpsResetBtn:SetText("Reset Threshhold and Colors")
  specialFpsResetBtn:SetScript("OnClick", function()
    if not GetSelectedPanelName() then return end
    local p = DB().panels[GetSelectedPanelName()]
    if not p then return end
    local _, special = EnsurePanelExtraData(p)
    special.fps.lowValue = 30
    special.fps.highValue = 60
    special.fps.lowColor = { 0.95, 0.24, 0.24, 1.00 }
    special.fps.midColor = { 1.00, 0.62, 0.16, 1.00 }
    special.fps.highColor = { 0.20, 0.90, 0.20, 1.00 }
    if editor and editor.UpdateColorDesignUI then editor:UpdateColorDesignUI() end
    RebuildAllPanels()
  end)
  editor.specialFpsResetBtn = specialFpsResetBtn

  local function CommitFpsRange()
    if not GetSelectedPanelName() then return end
    local p = DB().panels[GetSelectedPanelName()]
    if not p then return end
    local _, special = EnsurePanelExtraData(p)
    local low = tonumber(editor.specialFpsLowEdit and editor.specialFpsLowEdit:GetText())
    local high = tonumber(editor.specialFpsHighEdit and editor.specialFpsHighEdit:GetText())
    low = math.floor((low or special.fps.lowValue or 30) + 0.5)
    high = math.floor((high or special.fps.highValue or 60) + 0.5)
    if low < 1 then low = 1 end
    if high <= low then high = low + 1 end
    special.fps.lowValue = low
    special.fps.highValue = high
    if editor.specialFpsLowEdit then editor.specialFpsLowEdit:SetText(tostring(low)) end
    if editor.specialFpsHighEdit then editor.specialFpsHighEdit:SetText(tostring(high)) end
    RebuildAllPanels()
  end

  local specialFpsLowEdit = CreateFrame("EditBox", nil, props, "InputBoxTemplate")
  specialFpsLowEdit:SetSize(52, 20)
  specialFpsLowEdit:SetAutoFocus(false)
  specialFpsLowEdit:SetNumeric(true)
  specialFpsLowEdit:SetScript("OnEnterPressed", function(self)
    self:ClearFocus()
    CommitFpsRange()
  end)
  specialFpsLowEdit:SetScript("OnEditFocusLost", function()
    CommitFpsRange()
  end)
  editor.specialFpsLowEdit = specialFpsLowEdit

  local specialFpsMidLabel = props:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  specialFpsMidLabel:SetText("< Midrange >")
  editor.specialFpsMidLabel = specialFpsMidLabel

  local specialFpsHighEdit = CreateFrame("EditBox", nil, props, "InputBoxTemplate")
  specialFpsHighEdit:SetSize(52, 20)
  specialFpsHighEdit:SetAutoFocus(false)
  specialFpsHighEdit:SetNumeric(true)
  specialFpsHighEdit:SetScript("OnEnterPressed", function(self)
    self:ClearFocus()
    CommitFpsRange()
  end)
  specialFpsHighEdit:SetScript("OnEditFocusLost", function()
    CommitFpsRange()
  end)
  editor.specialFpsHighEdit = specialFpsHighEdit

  local specialStatsLabel = props:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  specialStatsLabel:SetText("Select Stats:")
  editor.specialStatsLabel = specialStatsLabel

  local specialStatsColorHeader = props:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  specialStatsColorHeader:SetText("Set colors")
  editor.specialStatsColorHeader = specialStatsColorHeader

  editor.specialStatChecks = {}
  editor.specialStatTitleBtns = {}
  editor.specialStatValueBtns = {}
  for i, def in ipairs(PANEL_STAT_CHOICES) do
    local cbName = "LIDCOUNCIL_SpecialStat_" .. def.key
    local cb = CreateFrame("CheckButton", cbName, props, "UICheckButtonTemplate")
    cb:SetPoint("TOPLEFT", 0, -930 - ((i - 1) * 22))
    _G[cb:GetName() .. "Text"]:SetText(def.label)
    cb:SetScript("OnClick", function(self)
      if not GetSelectedPanelName() then return end
      local p = DB().panels[GetSelectedPanelName()]
      if not p then return end
      local _, special = EnsurePanelExtraData(p)
      special.stats[def.key] = self:GetChecked() and true or false
      if editor and editor.UpdateColorDesignUI then editor:UpdateColorDesignUI() end
      RebuildAllPanels()
    end)
    editor.specialStatChecks[def.key] = cb

    local titleBtn = CreateFrame("Button", nil, props, "UIPanelButtonTemplate")
    titleBtn:SetSize(56, 18)
    titleBtn:SetText("Title")
    titleBtn:SetScript("OnClick", function()
      if not GetSelectedPanelName() then return end
      local p = DB().panels[GetSelectedPanelName()]
      if not p then return end
      local _, special = EnsurePanelExtraData(p)
      PickSpecialColor(special.statsTitleColors[def.key], function(r, g, b)
        special.statsTitleColors[def.key][1], special.statsTitleColors[def.key][2], special.statsTitleColors[def.key][3] = r, g, b
      end)
    end)
    editor.specialStatTitleBtns[def.key] = titleBtn

    local valueBtn = CreateFrame("Button", nil, props, "UIPanelButtonTemplate")
    valueBtn:SetSize(56, 18)
    valueBtn:SetText("Value")
    valueBtn:SetScript("OnClick", function()
      if not GetSelectedPanelName() then return end
      local p = DB().panels[GetSelectedPanelName()]
      if not p then return end
      local _, special = EnsurePanelExtraData(p)
      PickSpecialColor(special.statsValueColors[def.key], function(r, g, b)
        special.statsValueColors[def.key][1], special.statsValueColors[def.key][2], special.statsValueColors[def.key][3] = r, g, b
      end)
    end)
    editor.specialStatValueBtns[def.key] = valueBtn
  end

  local specialStatsSizeSlider = MakeSlider(props, "Stats Font Size", 0, -1140, 8, 40, 1, function(val)
    if not GetSelectedPanelName() then return end
    local p = DB().panels[GetSelectedPanelName()]
    if not p then return end
    local _, special = EnsurePanelExtraData(p)
    special.statsSize = math.floor((tonumber(val) or 14) + 0.5)
    RebuildAllPanels()
  end)
  editor.specialStatsSizeSlider = specialStatsSizeSlider

  local specialStatsAlphaSlider = MakeSlider(props, "Transparency (%)", 0, -1188, 0, 100, 1, function(val)
    if not GetSelectedPanelName() then return end
    local p = DB().panels[GetSelectedPanelName()]
    if not p then return end
    local _, special = EnsurePanelExtraData(p)
    special.statsAlpha = math.max(0, math.min(1, (tonumber(val) or 100) / 100))
    RebuildAllPanels()
  end)
  editor.specialStatsAlphaSlider = specialStatsAlphaSlider

  local specialCustomTextLabel = props:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  specialCustomTextLabel:SetText("Text:")
  editor.specialCustomTextLabel = specialCustomTextLabel

  local function CommitSpecialCustomText()
    if not GetSelectedPanelName() then return end
    local p = DB().panels[GetSelectedPanelName()]
    if not p then return end
    local _, special = EnsurePanelExtraData(p)
    local text = tostring(editor.specialCustomTextEdit and editor.specialCustomTextEdit:GetText() or "")
    if text == "" then
      text = "Custom Text"
    end
    special.customText.text = text
    if editor.specialCustomTextEdit then
      editor.specialCustomTextEdit:SetText(text)
    end
    RebuildAllPanels()
  end

  local specialCustomTextEdit = CreateFrame("EditBox", nil, props, "InputBoxTemplate")
  specialCustomTextEdit:SetSize(300, 20)
  specialCustomTextEdit:SetAutoFocus(false)
  specialCustomTextEdit:SetScript("OnEnterPressed", function(self)
    self:ClearFocus()
    CommitSpecialCustomText()
  end)
  specialCustomTextEdit:SetScript("OnEditFocusLost", function()
    CommitSpecialCustomText()
  end)
  editor.specialCustomTextEdit = specialCustomTextEdit

  local specialCustomTextFontLabel = props:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  specialCustomTextFontLabel:SetText("Font:")
  editor.specialCustomTextFontLabel = specialCustomTextFontLabel

  local specialCustomTextFontDrop = CreateFrame("Frame", "LIDCOUNCIL_SpecialCustomTextFontDrop", props, "UIDropDownMenuTemplate")
  InitSpecialFontDropdown(specialCustomTextFontDrop, function(special) return special.customText.font end, function(special, key)
    special.customText.font = key
  end)
  editor.specialCustomTextFontDrop = specialCustomTextFontDrop

  local specialCustomTextSizeSlider = MakeSlider(props, "Font Size", 0, -1236, 8, 40, 1, function(val)
    if not GetSelectedPanelName() then return end
    local p = DB().panels[GetSelectedPanelName()]
    if not p then return end
    local _, special = EnsurePanelExtraData(p)
    special.customText.size = math.floor((tonumber(val) or 14) + 0.5)
    RebuildAllPanels()
  end)
  editor.specialCustomTextSizeSlider = specialCustomTextSizeSlider

  local specialCustomTextAlphaSlider = MakeSlider(props, "Transparency (%)", 0, -1284, 0, 100, 1, function(val)
    if not GetSelectedPanelName() then return end
    local p = DB().panels[GetSelectedPanelName()]
    if not p then return end
    local _, special = EnsurePanelExtraData(p)
    special.customText.color[4] = math.max(0, math.min(1, (tonumber(val) or 100) / 100))
    RebuildAllPanels()
  end)
  editor.specialCustomTextAlphaSlider = specialCustomTextAlphaSlider

  local specialCustomTextColorBtn = CreateFrame("Button", nil, props, "UIPanelButtonTemplate")
  specialCustomTextColorBtn:SetSize(100, 22)
  specialCustomTextColorBtn:SetText("Color")
  specialCustomTextColorBtn:SetScript("OnClick", function()
    if not GetSelectedPanelName() then return end
    local p = DB().panels[GetSelectedPanelName()]
    if not p then return end
    local _, special = EnsurePanelExtraData(p)
    PickSpecialColor(special.customText.color, function(r, g, b)
      special.customText.color[1], special.customText.color[2], special.customText.color[3] = r, g, b
    end)
  end)
  editor.specialCustomTextColorBtn = specialCustomTextColorBtn

  specialGoldConfigLabel:Hide()
  specialGoldColorBtn:Hide()
  specialGoldFontLabel:Hide()
  specialGoldFontDrop:Hide()
  specialGoldSizeSlider:Hide()
  if specialGoldSizeSlider.valueBox then specialGoldSizeSlider.valueBox:Hide() end
  specialGoldAlphaSlider:Hide()
  if specialGoldAlphaSlider.valueBox then specialGoldAlphaSlider.valueBox:Hide() end
  specialFpsFontLabel:Hide()
  specialFpsFontDrop:Hide()
  specialFpsSizeSlider:Hide()
  if specialFpsSizeSlider.valueBox then specialFpsSizeSlider.valueBox:Hide() end
  specialFpsAlphaSlider:Hide()
  if specialFpsAlphaSlider.valueBox then specialFpsAlphaSlider.valueBox:Hide() end
  specialFpsColorLabel:Hide()
  specialFpsLowColorBtn:Hide()
  specialFpsMidColorBtn:Hide()
  specialFpsHighColorBtn:Hide()
  specialFpsRuleLabel:Hide()
  specialFpsLowEdit:Hide()
  specialFpsMidLabel:Hide()
  specialFpsHighEdit:Hide()
  specialFpsResetBtn:Hide()
  diagGraphSettingsLabel:Hide()
  diagGraphShowFpsCheck:Hide()
  diagGraphShowAvgCheck:Hide()
  diagGraphShowLowCheck:Hide()
  diagGraphFpsIntervalBtn:Hide()
  diagGraphAvgIntervalBtn:Hide()
  diagGraphLowIntervalBtn:Hide()
  diagGraphUpdateLabel:Hide()
  diagGraphUpdateEdit:Hide()
  diagGraphStyleLabel:Hide()
  diagGraphStyleDrop:Hide()
  specialStatsLabel:Hide()
  specialStatsColorHeader:Hide()
  specialStatsSizeSlider:Hide()
  if specialStatsSizeSlider.valueBox then specialStatsSizeSlider.valueBox:Hide() end
  specialStatsAlphaSlider:Hide()
  if specialStatsAlphaSlider.valueBox then specialStatsAlphaSlider.valueBox:Hide() end
  specialCustomTextLabel:Hide()
  specialCustomTextEdit:Hide()
  specialCustomTextFontLabel:Hide()
  specialCustomTextFontDrop:Hide()
  specialCustomTextSizeSlider:Hide()
  if specialCustomTextSizeSlider.valueBox then specialCustomTextSizeSlider.valueBox:Hide() end
  specialCustomTextAlphaSlider:Hide()
  if specialCustomTextAlphaSlider.valueBox then specialCustomTextAlphaSlider.valueBox:Hide() end
  specialCustomTextColorBtn:Hide()
  for _, def in ipairs(PANEL_STAT_CHOICES) do
    if editor.specialStatChecks and editor.specialStatChecks[def.key] then editor.specialStatChecks[def.key]:Hide() end
    if editor.specialStatTitleBtns and editor.specialStatTitleBtns[def.key] then editor.specialStatTitleBtns[def.key]:Hide() end
    if editor.specialStatValueBtns and editor.specialStatValueBtns[def.key] then editor.specialStatValueBtns[def.key]:Hide() end
  end
end

INTERNAL_API.LIDCOUNCIL_BuildPanelDiagnosticsSection_Impl = BuildPanelDiagnosticsSection
