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 shared = INTERNAL_API.LIDCOUNCIL_LootEditorShared_Impl
if type(shared) ~= "table" then
  error("LidCouncil missing loot editor shared helpers.")
end

local BindMouseWheelScroll = shared.BindMouseWheelScroll
local ApplyFrameSkin = shared.ApplyFrameSkin
local MakeYellowSubHeader = shared.MakeYellowSubHeader
local CreateCustomDropdownControl = shared.CreateCustomDropdownControl
local CreateSimpleEditBox = shared.CreateSimpleEditBox
local CreateSectionShell = shared.CreateSectionShell
local EnsureLidTableEditorRows = shared.EnsureLidTableEditorRows

local function CreateRollColumnHeader(parent, text)
  local header = parent:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
  header:SetJustifyH("LEFT")
  header:SetText(tostring(text or ""))
  return header
end

local function CreateProfileAccordionButton(parent, labelText)
  local button = CreateFrame("Button", nil, parent, "UIPanelButtonTemplate")
  button:SetSize(180, 28)
  button.baseLabel = tostring(labelText or "")
  button:SetText(button.baseLabel)
  return button
end

local function CreateProfileStatCard(parent, title)
  local card = CreateFrame("Frame", nil, parent, "BackdropTemplate")
  card:SetSize(128, 54)
  ApplyFrameSkin(card)
  card:EnableMouse(false)

  local valueText = card:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
  valueText:SetPoint("TOPLEFT", 10, -8)
  valueText:SetJustifyH("LEFT")
  valueText:SetText("0")

  local titleText = card:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  titleText:SetPoint("TOPLEFT", valueText, "BOTTOMLEFT", 0, -4)
  titleText:SetPoint("RIGHT", -8, 0)
  titleText:SetJustifyH("LEFT")
  titleText:SetText(tostring(title or ""))

  return {
    frame = card,
    value = valueText,
    title = titleText,
  }
end

local function ApplyDeveloperAccentButton(button)
  if not button then
    return
  end
  button.__lid_customBg = { 0.30, 0.07, 0.08, 0.98 }
  button.__lid_customHover = { 0.92, 0.32, 0.36, 0.28 }
  button.__lid_customPushed = { 0.42, 0.10, 0.12, 0.98 }
  button.__lid_customBorder = { 0.95, 0.46, 0.50, 0.98 }
  button.__lid_customText = { 1.00, 0.92, 0.92, 1.00 }
end

local function ApplyDeveloperAccentDropdown(dropdown)
  if not dropdown then
    return
  end
  dropdown.__lid_themeOverride = {
    active = { 0.95, 0.46, 0.50, 0.95 },
    border = { 0.95, 0.46, 0.50, 0.98 },
    text = { 1.00, 0.92, 0.92, 1.00 },
    dropdownButtonBg = { 0.30, 0.07, 0.08, 0.98 },
    dropdownPopupBg = { 0.18, 0.05, 0.06, 0.98 },
    dropdownCheckedText = { 1.00, 0.88, 0.88, 1.00 },
    dropdownTitleText = { 1.00, 0.78, 0.78, 1.00 },
  }
end

local function CreateDropdownControl(editor, parent, width, text)
  if type(CreateCustomDropdownControl) == "function" then
    return CreateCustomDropdownControl(editor, parent, {
      width = width,
      buttonHeight = 24,
      text = text,
    })
  end
  return CreateSimpleEditBox(parent, width, 22)
end

local function BuildLootProfilesPageScaffold(editor, page)
  if not editor or not page then
    return
  end

  local navBg, rightBg = CreateSectionShell(page, 240)
  editor.lootProfilesNavBg = navBg
  editor.lootProfilesRightBg = rightBg

  local navTitle = navBg:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
  navTitle:SetPoint("TOPLEFT", 8, -8)
  navTitle:SetText("Profiles")
  editor.lootProfilesNavTitle = navTitle

  local listScroll = CreateFrame("ScrollFrame", nil, navBg, "UIPanelScrollFrameTemplate")
  listScroll:SetPoint("TOPLEFT", 8, -32)
  listScroll:SetPoint("BOTTOMRIGHT", -26, 138)
  BindMouseWheelScroll(listScroll, 18)
  editor.lootProfilesListScroll = listScroll

  local listContent = CreateFrame("Frame", nil, listScroll)
  listContent:SetSize(208, 1)
  listScroll:SetScrollChild(listContent)
  editor.lootProfilesListContent = listContent
  editor.lootProfileButtons = {}

  local prevBtn = CreateFrame("Button", nil, navBg, "UIPanelButtonTemplate")
  prevBtn:SetSize(60, 20)
  prevBtn:SetPoint("BOTTOMLEFT", 8, 112)
  prevBtn:SetText("< Prev")
  editor.lootProfilesPrevBtn = prevBtn

  local nextBtn = CreateFrame("Button", nil, navBg, "UIPanelButtonTemplate")
  nextBtn:SetSize(60, 20)
  nextBtn:SetPoint("BOTTOMRIGHT", -8, 112)
  nextBtn:SetText("Next >")
  editor.lootProfilesNextBtn = nextBtn

  local pageText = navBg:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  pageText:SetPoint("LEFT", prevBtn, "RIGHT", 8, 0)
  pageText:SetPoint("RIGHT", nextBtn, "LEFT", -8, 0)
  pageText:SetJustifyH("CENTER")
  pageText:SetText("")
  editor.lootProfilesPageText = pageText

  local addGroupBtn = CreateFrame("Button", nil, navBg, "UIPanelButtonTemplate")
  addGroupBtn:SetSize(100, 22)
  addGroupBtn:SetPoint("BOTTOMLEFT", 8, 78)
  addGroupBtn:SetText("Create Group")
  editor.lootAddGroupBtn = addGroupBtn

  local addProfileBtn = CreateFrame("Button", nil, navBg, "UIPanelButtonTemplate")
  addProfileBtn:SetSize(100, 22)
  addProfileBtn:SetPoint("LEFT", addGroupBtn, "RIGHT", 8, 0)
  addProfileBtn:SetText("Create Profile")
  editor.lootAddProfileBtn = addProfileBtn

  local deleteGroupBtn = CreateFrame("Button", nil, navBg, "UIPanelButtonTemplate")
  deleteGroupBtn:SetSize(100, 22)
  deleteGroupBtn:SetPoint("BOTTOMLEFT", 8, 48)
  deleteGroupBtn:SetText("Delete Group")
  editor.lootDeleteGroupBtn = deleteGroupBtn

  local deleteBtn = CreateFrame("Button", nil, navBg, "UIPanelButtonTemplate")
  deleteBtn:SetSize(100, 22)
  deleteBtn:SetPoint("LEFT", deleteGroupBtn, "RIGHT", 8, 0)
  deleteBtn:SetText("Delete Profile")
  editor.lootDeleteProfileBtn = deleteBtn

  local feedback = navBg:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  feedback:SetPoint("BOTTOMLEFT", 8, 4)
  feedback:SetWidth(208)
  feedback:SetJustifyH("LEFT")
  feedback:SetText("")
  editor.lootProfilesFeedback = feedback

  local emptyState = CreateFrame("Frame", nil, rightBg, "BackdropTemplate")
  ApplyFrameSkin(emptyState)
  emptyState:SetPoint("TOPLEFT", 18, -18)
  emptyState:SetPoint("BOTTOMRIGHT", -18, 18)
  emptyState:Hide()
  editor.lootProfilesEmptyStateFrame = emptyState

  local emptyTitle = emptyState:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
  emptyTitle:SetPoint("TOPLEFT", 18, -18)
  emptyTitle:SetPoint("RIGHT", emptyState, "RIGHT", -18, 0)
  emptyTitle:SetJustifyH("LEFT")
  emptyTitle:SetText("No Profile selected")
  editor.lootProfilesEmptyStateTitle = emptyTitle

  local emptyText = emptyState:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
  emptyText:SetPoint("TOPLEFT", emptyTitle, "BOTTOMLEFT", 0, -12)
  emptyText:SetPoint("RIGHT", emptyState, "RIGHT", -18, 0)
  emptyText:SetJustifyH("LEFT")
  emptyText:SetJustifyV("TOP")
  emptyText:SetSpacing(4)
  emptyText:SetText(
    "Please create a new profile by pressing \"Create Profile\".\n\n" ..
    "A profile is made per season and will be used throughout the full raid season to collect Loot List points.\n\n" ..
    "When a season ends and a new season begins, create a new profile to reset the Loot List points and lock the new profile to the new raid season. " ..
    "Old profiles keep their original profile season for history and review."
  )
  editor.lootProfilesEmptyStateText = emptyText

  local rightScroll = CreateFrame("ScrollFrame", nil, rightBg, "UIPanelScrollFrameTemplate")
  rightScroll:SetPoint("TOPLEFT", 6, -6)
  rightScroll:SetPoint("BOTTOMRIGHT", -28, 6)
  BindMouseWheelScroll(rightScroll, 18)
  editor.lootProfilesRightScroll = rightScroll

  local rightContent = CreateFrame("Frame", nil, rightScroll)
  rightContent:SetSize(1, 1)
  rightScroll:SetScrollChild(rightContent)
  editor.lootProfilesRightContent = rightContent

  local title = rightContent:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
  title:SetPoint("TOPLEFT", 18, -16)
  title:SetText("Profile Settings")
  editor.lootProfilesTitle = title

  local sizeFrame = CreateFrame("Frame", nil, rightContent)
  sizeFrame:SetSize(260, 18)
  sizeFrame:EnableMouse(true)
  sizeFrame:Hide()
  editor.lootProfileSizeFrame = sizeFrame

  local sizeText = sizeFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  sizeText:SetPoint("CENTER", sizeFrame, "CENTER", 0, 0)
  sizeText:SetJustifyH("CENTER")
  sizeText:SetText("")
  editor.lootProfileSizeText = sizeText

  sizeFrame:SetScript("OnEnter", function(self)
    if not GameTooltip then return end
    GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
    GameTooltip:SetText("Profile Size", 1, 0.82, 0.28, 1, true)
    local lines = type(self.__tooltipLines) == "table" and self.__tooltipLines or {}
    for _, line in ipairs(lines) do
      GameTooltip:AddLine(tostring(line), 0.92, 0.92, 0.96, true)
    end
    GameTooltip:Show()
  end)
  sizeFrame:SetScript("OnLeave", function()
    if GameTooltip then GameTooltip:Hide() end
  end)

  local status = rightContent:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
  status:SetPoint("TOPLEFT", 18, -44)
  status:SetText("Status: Inactive")
  editor.lootProfileStatus = status

  local summary = rightContent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  summary:SetPoint("TOPLEFT", 18, -70)
  summary:SetWidth(520)
  summary:SetJustifyH("LEFT")
  summary:SetText("")
  editor.lootProfileSummary = summary

  local activateBtn = CreateFrame("Button", nil, rightContent, "UIPanelButtonTemplate")
  activateBtn:SetSize(110, 22)
  activateBtn:SetPoint("TOPLEFT", 18, -112)
  activateBtn:SetText("Activate")
  editor.lootActivateProfileBtn = activateBtn

  local deactivateBtn = CreateFrame("Button", nil, rightContent, "UIPanelButtonTemplate")
  deactivateBtn:SetSize(110, 22)
  deactivateBtn:SetPoint("LEFT", activateBtn, "RIGHT", 10, 0)
  deactivateBtn:SetText("Deactivate")
  editor.lootDeactivateProfileBtn = deactivateBtn

  local copyBtn = CreateFrame("Button", nil, rightContent, "UIPanelButtonTemplate")
  copyBtn:SetSize(110, 22)
  copyBtn:SetPoint("LEFT", deactivateBtn, "RIGHT", 10, 0)
  copyBtn:SetText("Copy Profile")
  editor.lootCopyProfileBtn = copyBtn

  local clearSimulationBtn = CreateFrame("Button", nil, rightContent, "UIPanelButtonTemplate")
  clearSimulationBtn:SetSize(144, 22)
  clearSimulationBtn:SetText("Clear Content")
  clearSimulationBtn:Hide()
  editor.lootClearSimulationContentBtn = clearSimulationBtn

  local sectionFrameLevel = math.max((rightContent:GetFrameLevel() or 1) - 1, 0)

  local heroFrame = CreateFrame("Frame", nil, rightContent, "BackdropTemplate")
  ApplyFrameSkin(heroFrame)
  heroFrame:EnableMouse(false)
  heroFrame:SetFrameLevel(sectionFrameLevel)
  editor.lootProfileHeroFrame = heroFrame

  local statusDot = heroFrame:CreateTexture(nil, "ARTWORK")
  statusDot:SetSize(10, 10)
  statusDot:SetTexture("Interface\\Buttons\\WHITE8X8")
  editor.lootProfileStatusDot = statusDot

  editor.lootProfileHeroRaidPreviewFrame = CreateFrame("Frame", nil, heroFrame, "BackdropTemplate")
  ApplyFrameSkin(editor.lootProfileHeroRaidPreviewFrame)
  editor.lootProfileHeroRaidPreviewFrame:EnableMouse(false)
  if editor.lootProfileHeroRaidPreviewFrame.SetBackdropColor then
    editor.lootProfileHeroRaidPreviewFrame:SetBackdropColor(0.05, 0.05, 0.07, 0.98)
  end

  editor.lootProfileHeroRaidPreviewTitle = editor.lootProfileHeroRaidPreviewFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  editor.lootProfileHeroRaidPreviewTitle:SetPoint("TOPLEFT", 10, -8)
  editor.lootProfileHeroRaidPreviewTitle:SetJustifyH("LEFT")
  editor.lootProfileHeroRaidPreviewTitle:SetText("Raid Preview")

  editor.lootProfileHeroRaidPreviewTexture = editor.lootProfileHeroRaidPreviewFrame:CreateTexture(nil, "ARTWORK")
  editor.lootProfileHeroRaidPreviewTexture:SetTexCoord(0, 1, 0, 1)
  editor.lootProfileHeroRaidPreviewTexture:SetVertexColor(1, 1, 1, 1)

  editor.lootProfileHeroRaidPreviewLabel = editor.lootProfileHeroRaidPreviewFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  editor.lootProfileHeroRaidPreviewLabel:SetJustifyH("LEFT")
  editor.lootProfileHeroRaidPreviewLabel:SetText("")

  editor.lootProfileHeroRaidPreviewEmpty = editor.lootProfileHeroRaidPreviewFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  editor.lootProfileHeroRaidPreviewEmpty:SetJustifyH("CENTER")
  editor.lootProfileHeroRaidPreviewEmpty:SetText("No profile-season raid selected.")

  editor.lootProfileHeroRaidPreviewButton = CreateFrame("Button", nil, editor.lootProfileHeroRaidPreviewFrame)
  editor.lootProfileHeroRaidPreviewButton:SetAllPoints(editor.lootProfileHeroRaidPreviewFrame)
  editor.lootProfileHeroRaidPreviewButton:RegisterForClicks("LeftButtonUp")
  editor.lootProfileHeroRaidPreviewButton:SetScript("OnClick", function()
    editor.__lootProfileOpenSection = "raid"
    if editor.Refresh then
      editor:Refresh()
    end
    if editor.lootRaidDropdown and editor.lootRaidDropdown.__lidcouncilCustomDropdown and editor.lootRaidDropdown.OpenMenu then
      editor.lootRaidDropdown:OpenMenu()
    end
  end)

  local profileStatCards = {}
  local profileStatLabels = {
    { key = "sessions", title = "Session History" },
    { key = "items", title = "Loot Items Awarded" },
    { key = "participants", title = "Participants" },
    { key = "rolls", title = "Roll Options" },
  }
  for _, info in ipairs(profileStatLabels) do
    profileStatCards[info.key] = CreateProfileStatCard(rightContent, info.title)
  end
  editor.lootProfileStatCards = profileStatCards

  local masterCard = CreateFrame("Frame", nil, rightContent, "BackdropTemplate")
  ApplyFrameSkin(masterCard)
  masterCard:EnableMouse(false)
  editor.lootProfileMasterCard = masterCard

  local masterCardTitle = masterCard:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  masterCardTitle:SetPoint("TOPLEFT", 12, -10)
  masterCardTitle:SetJustifyH("LEFT")
  masterCardTitle:SetText("Profile Master")
  editor.lootProfileMasterCardTitle = masterCardTitle

  local masterCardIcon = masterCard:CreateTexture(nil, "ARTWORK")
  masterCardIcon:SetSize(24, 24)
  masterCardIcon:SetPoint("TOPLEFT", 12, -28)
  editor.lootProfileMasterCardIcon = masterCardIcon

  local masterCardValue = masterCard:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
  masterCardValue:SetPoint("TOPLEFT", masterCardIcon, "TOPRIGHT", 10, 0)
  masterCardValue:SetJustifyH("LEFT")
  masterCardValue:SetText("Unassigned")
  editor.lootProfileMasterCardValue = masterCardValue

  local masterCardMeta = masterCard:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  masterCardMeta:SetPoint("TOPLEFT", masterCardValue, "BOTTOMLEFT", 0, -4)
  masterCardMeta:SetJustifyH("LEFT")
  masterCardMeta:SetText("Full profile control")
  editor.lootProfileMasterCardMeta = masterCardMeta

  local transferMasterBtn = CreateFrame("Button", nil, masterCard, "BackdropTemplate")
  ApplyFrameSkin(transferMasterBtn)
  transferMasterBtn:SetSize(136, 24)
  transferMasterBtn.label = transferMasterBtn:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  transferMasterBtn.label:SetPoint("CENTER", 0, 0)
  transferMasterBtn.label:SetText("Transfer Master")
  transferMasterBtn:SetScript("OnEnter", function(self)
    if self.SetBackdropColor and self.__hoverBackdrop then
      self:SetBackdropColor(self.__hoverBackdrop[1], self.__hoverBackdrop[2], self.__hoverBackdrop[3], self.__hoverBackdrop[4])
    end
  end)
  transferMasterBtn:SetScript("OnLeave", function(self)
    if self.SetBackdropColor and self.__normalBackdrop then
      self:SetBackdropColor(self.__normalBackdrop[1], self.__normalBackdrop[2], self.__normalBackdrop[3], self.__normalBackdrop[4])
    end
  end)
  editor.lootProfileTransferMasterBtn = transferMasterBtn

  local councilCard = CreateFrame("Frame", nil, rightContent, "BackdropTemplate")
  ApplyFrameSkin(councilCard)
  councilCard:EnableMouse(false)
  editor.lootProfileCouncilCard = councilCard

  local councilCardTitle = councilCard:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  councilCardTitle:SetPoint("TOPLEFT", 12, -10)
  councilCardTitle:SetJustifyH("LEFT")
  councilCardTitle:SetText("Council Members")
  editor.lootProfileCouncilCardTitle = councilCardTitle

  local councilCardValue = councilCard:CreateFontString(nil, "OVERLAY", "GameFontNormal")
  councilCardValue:SetPoint("TOPLEFT", 12, -28)
  councilCardValue:SetJustifyH("LEFT")
  councilCardValue:SetText("0 member(s)")
  editor.lootProfileCouncilCardValue = councilCardValue

  local councilCardMeta = councilCard:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  councilCardMeta:SetPoint("TOPLEFT", 12, -46)
  councilCardMeta:SetJustifyH("LEFT")
  councilCardMeta:SetText("No council members yet.")
  editor.lootProfileCouncilCardMeta = councilCardMeta

  local councilRowsFrame = CreateFrame("Frame", nil, councilCard)
  councilRowsFrame:EnableMouse(false)
  editor.lootProfileCouncilRowsFrame = councilRowsFrame
  editor.lootProfileCouncilRows = {}

  local raiderFrame = CreateFrame("Frame", nil, rightContent, "BackdropTemplate")
  ApplyFrameSkin(raiderFrame)
  raiderFrame:EnableMouse(false)
  editor.lootProfileRaiderFrame = raiderFrame

  local raiderToggleBtn = CreateFrame("Button", nil, raiderFrame, "BackdropTemplate")
  ApplyFrameSkin(raiderToggleBtn)
  if raiderToggleBtn.SetBackdropColor then
    raiderToggleBtn:SetBackdropColor(0.08, 0.08, 0.11, 0.98)
  end
  raiderToggleBtn:SetSize(180, 28)
  raiderToggleBtn.arrowText = raiderToggleBtn:CreateFontString(nil, "OVERLAY", "GameFontNormal")
  raiderToggleBtn.arrowText:SetPoint("LEFT", 10, 0)
  raiderToggleBtn.arrowText:SetTextColor(1.00, 0.82, 0.24, 1.00)
  raiderToggleBtn.labelText = raiderToggleBtn:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
  raiderToggleBtn.labelText:SetPoint("LEFT", raiderToggleBtn.arrowText, "RIGHT", 8, 0)
  raiderToggleBtn.labelText:SetPoint("RIGHT", -10, 0)
  raiderToggleBtn.labelText:SetJustifyH("LEFT")
  editor.lootProfileRaiderToggleBtn = raiderToggleBtn

  local raiderSummary = raiderFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  raiderSummary:SetPoint("TOPLEFT", 12, -40)
  raiderSummary:SetJustifyH("LEFT")
  raiderSummary:SetText("No class data yet.")
  editor.lootProfileRaiderSummary = raiderSummary

  local raiderRowsFrame = CreateFrame("Frame", nil, raiderFrame)
  raiderRowsFrame:EnableMouse(false)
  editor.lootProfileRaiderRowsFrame = raiderRowsFrame
  editor.lootProfileRaiderRows = {}

  local accessToggleBtn = CreateProfileAccordionButton(rightContent, "Council Settings")
  editor.lootProfileAccessToggleBtn = accessToggleBtn

  local accessHeader = MakeYellowSubHeader(rightContent, "Council Settings", 18, -152)
  editor.lootProfileAccessHeader = accessHeader

  local accessSummary = rightContent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  accessSummary:SetPoint("TOPLEFT", 18, -176)
  accessSummary:SetWidth(520)
  accessSummary:SetJustifyH("LEFT")
  accessSummary:SetText("")
  editor.lootProfileAccessSummary = accessSummary

  local accessTargetDropdown = CreateDropdownControl(editor, rightContent, 250, "(select character)")
  accessTargetDropdown:SetPoint("TOPLEFT", 18, -216)
  editor.lootProfileRoleTargetDropdown = accessTargetDropdown

  local accessRoleDropdown = CreateDropdownControl(editor, rightContent, 160, "Raider")
  accessRoleDropdown:SetPoint("LEFT", accessTargetDropdown, "RIGHT", 10, 0)
  editor.lootProfileRoleDropdown = accessRoleDropdown

  local applyRoleBtn = CreateFrame("Button", nil, rightContent, "UIPanelButtonTemplate")
  applyRoleBtn:SetSize(110, 22)
  applyRoleBtn:SetPoint("LEFT", accessRoleDropdown, "RIGHT", 10, 0)
  applyRoleBtn:SetText("Assign Role")
  editor.lootProfileApplyRoleBtn = applyRoleBtn

  local accessList = rightContent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  accessList:SetPoint("TOPLEFT", 18, -276)
  accessList:SetWidth(520)
  accessList:SetJustifyH("LEFT")
  accessList:SetText("")
  editor.lootProfileAccessList = accessList

  local accessFrame = CreateFrame("Frame", nil, rightContent, "BackdropTemplate")
  ApplyFrameSkin(accessFrame)
  accessFrame:EnableMouse(false)
  accessFrame:SetFrameLevel(sectionFrameLevel)
  editor.lootProfileAccessFrame = accessFrame

  editor.lootProfileRoleSelectionCard = CreateFrame("Frame", nil, accessFrame, "BackdropTemplate")
  ApplyFrameSkin(editor.lootProfileRoleSelectionCard)
  editor.lootProfileRoleSelectionCard:EnableMouse(false)
  if editor.lootProfileRoleSelectionCard.SetBackdropColor then
    editor.lootProfileRoleSelectionCard:SetBackdropColor(0.06, 0.06, 0.08, 0.98)
  end

  editor.lootProfileRoleSelectionTitle = editor.lootProfileRoleSelectionCard:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  editor.lootProfileRoleSelectionTitle:SetPoint("TOPLEFT", 12, -10)
  editor.lootProfileRoleSelectionTitle:SetText("Selected Member")

  editor.lootProfileRoleSelectionIcon = editor.lootProfileRoleSelectionCard:CreateTexture(nil, "ARTWORK")
  editor.lootProfileRoleSelectionIcon:SetSize(22, 22)
  editor.lootProfileRoleSelectionIcon:SetPoint("TOPLEFT", 12, -28)

  editor.lootProfileRoleSelectionName = editor.lootProfileRoleSelectionCard:CreateFontString(nil, "OVERLAY", "GameFontNormal")
  editor.lootProfileRoleSelectionName:SetJustifyH("LEFT")
  if editor.lootProfileRoleSelectionName.SetMaxLines then
    editor.lootProfileRoleSelectionName:SetMaxLines(1)
  end

  editor.lootProfileRoleSelectionMeta = editor.lootProfileRoleSelectionCard:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  editor.lootProfileRoleSelectionMeta:SetJustifyH("LEFT")
  if editor.lootProfileRoleSelectionMeta.SetMaxLines then
    editor.lootProfileRoleSelectionMeta:SetMaxLines(1)
  end

  editor.lootProfileRoleSelectionRole = editor.lootProfileRoleSelectionCard:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  editor.lootProfileRoleSelectionRole:SetJustifyH("LEFT")

  editor.lootProfileRoleSelectionHint = editor.lootProfileRoleSelectionCard:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  editor.lootProfileRoleSelectionHint:SetJustifyH("LEFT")

  editor.lootProfilePermissionPolicyTitle = accessFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  editor.lootProfilePermissionPolicyTitle:SetJustifyH("LEFT")
  editor.lootProfilePermissionPolicyTitle:SetText("Permission Policy")

  editor.lootProfilePermissionPolicySummary = accessFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  editor.lootProfilePermissionPolicySummary:SetJustifyH("LEFT")
  editor.lootProfilePermissionPolicySummary:SetText("")

  local permissionResetBtn = CreateFrame("Button", nil, accessFrame, "UIPanelButtonTemplate")
  permissionResetBtn:SetSize(190, 20)
  permissionResetBtn:SetText("Reset to default Permission")
  editor.lootProfilePermissionPolicyResetBtn = permissionResetBtn

  editor.lootProfilePermissionPolicyHeaderRight = accessFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  editor.lootProfilePermissionPolicyHeaderRight:SetJustifyH("LEFT")
  editor.lootProfilePermissionPolicyHeaderRight:SetText("Right")

  editor.lootProfilePermissionPolicyHeaderMaster = accessFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  editor.lootProfilePermissionPolicyHeaderMaster:SetJustifyH("CENTER")
  editor.lootProfilePermissionPolicyHeaderMaster:SetText("Master")

  editor.lootProfilePermissionPolicyHeaderSessionHost = accessFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  editor.lootProfilePermissionPolicyHeaderSessionHost:SetJustifyH("CENTER")
  editor.lootProfilePermissionPolicyHeaderSessionHost:SetText("Session Host")

  editor.lootProfilePermissionPolicyHeaderCouncil = accessFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  editor.lootProfilePermissionPolicyHeaderCouncil:SetJustifyH("CENTER")
  editor.lootProfilePermissionPolicyHeaderCouncil:SetText("Council")

  editor.lootProfilePermissionPolicyHeaderRaider = accessFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  editor.lootProfilePermissionPolicyHeaderRaider:SetJustifyH("CENTER")
  editor.lootProfilePermissionPolicyHeaderRaider:SetText("Raider")

  editor.lootProfilePermissionPolicyRows = {}

  local raidToggleBtn = CreateProfileAccordionButton(rightContent, "Raid Settings")
  editor.lootProfileRaidToggleBtn = raidToggleBtn

  local raidHeader = MakeYellowSubHeader(rightContent, "Raid", 18, -152)
  editor.lootRaidHeader = raidHeader

  local raidLabel = rightContent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  raidLabel:SetPoint("TOPLEFT", 18, -176)
  raidLabel:SetText("Select Raid Thumbnail:")
  editor.lootRaidLabel = raidLabel

  local raidSeasonLabel = rightContent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  raidSeasonLabel:SetPoint("TOPLEFT", 18, -176)
  raidSeasonLabel:SetText("Raid Season:")
  editor.lootRaidSeasonLabel = raidSeasonLabel

  local raidSeasonDropdown = CreateDropdownControl(editor, rightContent, 210, "(profile season)")
  raidSeasonDropdown:SetPoint("LEFT", raidSeasonLabel, "RIGHT", 8, 0)
  editor.lootRaidSeasonDropdown = raidSeasonDropdown

  local raidDropdown = CreateDropdownControl(editor, rightContent, 260, "(select raid)")
  raidDropdown:SetPoint("LEFT", raidLabel, "RIGHT", 8, 0)
  editor.lootRaidDropdown = raidDropdown

  local saveRaidBtn = CreateFrame("Button", nil, rightContent, "UIPanelButtonTemplate")
  saveRaidBtn:SetSize(96, 22)
  saveRaidBtn:SetPoint("LEFT", raidDropdown, "RIGHT", 10, 0)
  saveRaidBtn:SetText("Clear Raid")
  editor.lootSaveRaidBtn = saveRaidBtn

  local raidPreviewFrame = CreateFrame("Frame", nil, rightContent, "BackdropTemplate")
  ApplyFrameSkin(raidPreviewFrame)
  raidPreviewFrame:EnableMouse(false)
  raidPreviewFrame:SetFrameLevel(sectionFrameLevel + 1)
  editor.lootRaidPreviewFrame = raidPreviewFrame

  local raidPreviewTexture = raidPreviewFrame:CreateTexture(nil, "ARTWORK")
  raidPreviewTexture:SetPoint("TOPLEFT", 8, -8)
  raidPreviewTexture:SetSize(96, 54)
  raidPreviewTexture:SetTexCoord(0.05, 0.80, 0.04, 0.64)
  raidPreviewTexture:Hide()
  editor.lootRaidPreviewTexture = raidPreviewTexture

  local raidPreviewLabel = raidPreviewFrame:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
  raidPreviewLabel:SetPoint("TOPLEFT", raidPreviewTexture, "TOPRIGHT", 10, -2)
  raidPreviewLabel:SetJustifyH("LEFT")
  raidPreviewLabel:SetText("Selected Raid Thumbnail")
  editor.lootRaidPreviewLabel = raidPreviewLabel

  local raidPreviewMeta = raidPreviewFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  raidPreviewMeta:SetPoint("TOPLEFT", raidPreviewLabel, "BOTTOMLEFT", 0, -6)
  raidPreviewMeta:SetJustifyH("LEFT")
  raidPreviewMeta:SetText("No raid thumbnail selected.")
  editor.lootRaidPreviewMeta = raidPreviewMeta

  local raidHint = rightContent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  raidHint:SetPoint("TOPLEFT", 18, -202)
  raidHint:SetWidth(520)
  raidHint:SetJustifyH("LEFT")
  raidHint:SetText("Select a raid from the Adventure Guide dropdown. The selected raid also drives the item picker in Point Settings.")
  editor.lootRaidHint = raidHint

  local raidFrame = CreateFrame("Frame", nil, rightContent, "BackdropTemplate")
  ApplyFrameSkin(raidFrame)
  raidFrame:EnableMouse(false)
  raidFrame:SetFrameLevel(sectionFrameLevel)
  editor.lootProfileRaidFrame = raidFrame

  local rollHeader = MakeYellowSubHeader(rightContent, "Roll Settings", 18, -242)
  editor.lootRollHeader = rollHeader

  local rollText = rightContent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  rollText:SetPoint("TOPLEFT", 18, -266)
  rollText:SetWidth(520)
  rollText:SetJustifyH("LEFT")
  rollText:SetText("")
  editor.lootRollOptionsText = rollText

  local rollTemplateLabel = rightContent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  rollTemplateLabel:SetPoint("TOPLEFT", 18, -292)
  rollTemplateLabel:SetText("Select Template:")
  editor.lootRollTemplateLabel = rollTemplateLabel

  local rollTemplateDropdown = CreateDropdownControl(editor, rightContent, 220, "Default WoW Template")
  rollTemplateDropdown:SetPoint("LEFT", rollTemplateLabel, "RIGHT", 8, 0)
  editor.lootRollTemplateDropdown = rollTemplateDropdown

  local rollResponsiveHint = rightContent:CreateFontString(nil, "OVERLAY", "GameFontDisableSmall")
  rollResponsiveHint:SetPoint("TOPLEFT", 18, -320)
  rollResponsiveHint:SetWidth(520)
  rollResponsiveHint:SetJustifyH("LEFT")
  rollResponsiveHint:SetText("")
  rollResponsiveHint:Hide()
  editor.lootRollResponsiveHint = rollResponsiveHint

  editor.lootRollPriorityHeader = CreateRollColumnHeader(rightContent, "Priority")
  editor.lootRollDescriptionHeader = CreateRollColumnHeader(rightContent, "Description")
  editor.lootRollIconHeader = CreateRollColumnHeader(rightContent, "Icon")
  editor.lootRollColorHeader = CreateRollColumnHeader(rightContent, "Color")
  editor.lootRollPointDetailsHeader = CreateRollColumnHeader(rightContent, "Point Details")
  editor.lootRollCategoryHeader = CreateRollColumnHeader(rightContent, "Category")

  local rollEdit = CreateSimpleEditBox(rightContent, 220, 22)
  rollEdit:SetPoint("TOPLEFT", 18, -334)
  editor.lootRollOptionEdit = rollEdit

  local addRollBtn = CreateFrame("Button", nil, rightContent, "UIPanelButtonTemplate")
  addRollBtn:SetSize(110, 22)
  addRollBtn:SetPoint("LEFT", rollEdit, "RIGHT", 10, 0)
  addRollBtn:SetText("Add Option")
  editor.lootAddRollOptionBtn = addRollBtn

  local removeRollBtn = CreateFrame("Button", nil, rightContent, "UIPanelButtonTemplate")
  removeRollBtn:SetSize(120, 22)
  removeRollBtn:SetPoint("LEFT", addRollBtn, "RIGHT", 10, 0)
  removeRollBtn:SetText("Remove Last")
  editor.lootRemoveRollOptionBtn = removeRollBtn

  local testRollBtn = CreateFrame("Button", nil, rightContent, "UIPanelButtonTemplate")
  testRollBtn:SetSize(120, 22)
  testRollBtn:SetPoint("TOPLEFT", 18, -362)
  testRollBtn:SetText("Test Roll Popup")
  editor.lootTestRollPopupBtn = testRollBtn

  local previewHostBoardBtn = CreateFrame("Button", nil, rightContent, "UIPanelButtonTemplate")
  previewHostBoardBtn:SetSize(150, 22)
  previewHostBoardBtn:SetPoint("LEFT", testRollBtn, "RIGHT", 10, 0)
  previewHostBoardBtn:SetText("Preview Live Board")
  editor.lootPreviewHostBoardBtn = previewHostBoardBtn

  local rollExportBtn = CreateFrame("Button", nil, rightContent, "UIPanelButtonTemplate")
  rollExportBtn:SetSize(80, 22)
  rollExportBtn:SetPoint("LEFT", previewHostBoardBtn, "RIGHT", 10, 0)
  rollExportBtn:SetText("Export")
  ApplyDeveloperAccentButton(rollExportBtn)
  editor.lootRollExportBtn = rollExportBtn

  local rollImportStringBtn = CreateFrame("Button", nil, rightContent, "UIPanelButtonTemplate")
  rollImportStringBtn:SetSize(96, 22)
  rollImportStringBtn:SetPoint("LEFT", rollExportBtn, "RIGHT", 10, 0)
  rollImportStringBtn:SetText("Import Text")
  ApplyDeveloperAccentButton(rollImportStringBtn)
  editor.lootRollImportStringBtn = rollImportStringBtn

  local rollImportProfileDropdown = CreateDropdownControl(editor, rightContent, 170, "Import From Profile")
  rollImportProfileDropdown:SetPoint("LEFT", rollImportStringBtn, "RIGHT", 10, 0)
  ApplyDeveloperAccentDropdown(rollImportProfileDropdown)
  editor.lootRollImportProfileDropdown = rollImportProfileDropdown

  editor.lootRollOptionRows = {}

  local rollFrame = CreateFrame("Frame", nil, rightContent, "BackdropTemplate")
  ApplyFrameSkin(rollFrame)
  rollFrame:EnableMouse(false)
  rollFrame:SetFrameLevel(sectionFrameLevel)
  editor.lootProfileRollFrame = rollFrame

  local rollToggleBtn = CreateProfileAccordionButton(rightContent, "Roll Settings")
  editor.lootProfileRollToggleBtn = rollToggleBtn

  local pointsHeader = MakeYellowSubHeader(rightContent, "Point Settings", 18, -374)
  editor.lootPointsHeader = pointsHeader

  local pointModeLabel = rightContent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  pointModeLabel:SetPoint("TOPLEFT", 18, -398)
  pointModeLabel:SetText("Loot List Type:")
  editor.lootPointModeLabel = pointModeLabel

  local pointModeDropdown = CreateDropdownControl(editor, rightContent, 180, "LidTable")
  pointModeDropdown:SetPoint("LEFT", pointModeLabel, "RIGHT", 8, 0)
  editor.lootPointModeDropdown = pointModeDropdown

  local pointsHint = rightContent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  pointsHint:SetPoint("TOPLEFT", 18, -426)
  pointsHint:SetWidth(520)
  pointsHint:SetJustifyH("LEFT")
  pointsHint:SetText("")
  editor.lootPointsHint = pointsHint

  local lidTablePointsLabel = rightContent:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
  lidTablePointsLabel:SetText("Points")
  editor.lootLidTablePointsLabel = lidTablePointsLabel

  local lidTableItemLevelLabel = rightContent:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
  lidTableItemLevelLabel:SetText("Item Level")
  editor.lootLidTableItemLevelLabel = lidTableItemLevelLabel

  local lidTableSetPieceLabel = rightContent:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
  lidTableSetPieceLabel:SetText("Setpiece Points")
  editor.lootLidTableSetPieceLabel = lidTableSetPieceLabel

  editor.lootLidTableOffSpecLabel = rightContent:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
  editor.lootLidTableOffSpecLabel:SetText("Off-Spec")
  editor.lootLidTableTransmogLabel = rightContent:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
  editor.lootLidTableTransmogLabel:SetText("Transmog")
  editor.lootLidTableDisenchantLabel = rightContent:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
  editor.lootLidTableDisenchantLabel:SetText("Disenchant")

  editor.lootLidTableRows = EnsureLidTableEditorRows(editor.lootLidTableRows, rightContent, 15)

  local levelEdit = CreateSimpleEditBox(rightContent, 72, 22)
  levelEdit:SetPoint("TOPLEFT", 18, -398)
  editor.lootPointItemLevelEdit = levelEdit

  local itemDropdown = CreateDropdownControl(editor, rightContent, 210, "(select profile-season loot item)")
  itemDropdown:SetPoint("LEFT", levelEdit, "RIGHT", 10, 0)
  editor.lootPointItemDropdown = itemDropdown

  local pointsEdit = CreateSimpleEditBox(rightContent, 70, 22)
  pointsEdit:SetPoint("LEFT", itemDropdown, "RIGHT", 10, 0)
  editor.lootPointValueEdit = pointsEdit

  local addLevelRuleBtn = CreateFrame("Button", nil, rightContent, "UIPanelButtonTemplate")
  addLevelRuleBtn:SetSize(140, 22)
  addLevelRuleBtn:SetPoint("TOPLEFT", 18, -430)
  addLevelRuleBtn:SetText("Add iLvl Rule")
  editor.lootAddPointRuleBtn = addLevelRuleBtn

  local addItemRuleBtn = CreateFrame("Button", nil, rightContent, "UIPanelButtonTemplate")
  addItemRuleBtn:SetSize(140, 22)
  addItemRuleBtn:SetPoint("LEFT", addLevelRuleBtn, "RIGHT", 10, 0)
  addItemRuleBtn:SetText("Add Item Rule")
  editor.lootAddItemPointRuleBtn = addItemRuleBtn

  local removeRuleBtn = CreateFrame("Button", nil, rightContent, "UIPanelButtonTemplate")
  removeRuleBtn:SetSize(140, 22)
  removeRuleBtn:SetPoint("LEFT", addItemRuleBtn, "RIGHT", 10, 0)
  removeRuleBtn:SetText("Remove Last")
  editor.lootRemovePointRuleBtn = removeRuleBtn

  local pointExportBtn = CreateFrame("Button", nil, rightContent, "UIPanelButtonTemplate")
  pointExportBtn:SetSize(80, 22)
  pointExportBtn:SetPoint("TOPLEFT", 18, -458)
  pointExportBtn:SetText("Export")
  ApplyDeveloperAccentButton(pointExportBtn)
  editor.lootPointExportBtn = pointExportBtn

  local pointImportStringBtn = CreateFrame("Button", nil, rightContent, "UIPanelButtonTemplate")
  pointImportStringBtn:SetSize(96, 22)
  pointImportStringBtn:SetPoint("LEFT", pointExportBtn, "RIGHT", 10, 0)
  pointImportStringBtn:SetText("Import Text")
  ApplyDeveloperAccentButton(pointImportStringBtn)
  editor.lootPointImportStringBtn = pointImportStringBtn

  local pointImportProfileDropdown = CreateDropdownControl(editor, rightContent, 170, "Import From Profile")
  pointImportProfileDropdown:SetPoint("LEFT", pointImportStringBtn, "RIGHT", 10, 0)
  ApplyDeveloperAccentDropdown(pointImportProfileDropdown)
  editor.lootPointImportProfileDropdown = pointImportProfileDropdown

  local ruleText = rightContent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  ruleText:SetPoint("TOPLEFT", 18, -494)
  ruleText:SetWidth(520)
  ruleText:SetJustifyH("LEFT")
  ruleText:SetText("")
  editor.lootPointRulesText = ruleText

  local pointsFrame = CreateFrame("Frame", nil, rightContent, "BackdropTemplate")
  ApplyFrameSkin(pointsFrame)
  pointsFrame:EnableMouse(false)
  pointsFrame:SetFrameLevel(sectionFrameLevel)
  editor.lootProfilePointsFrame = pointsFrame

  local pointsToggleBtn = CreateProfileAccordionButton(rightContent, "Point Settings")
  editor.lootProfilePointsToggleBtn = pointsToggleBtn

  local backupToggleBtn = CreateProfileAccordionButton(rightContent, "Backups")
  editor.lootProfileBackupToggleBtn = backupToggleBtn

  local backupHeader = MakeYellowSubHeader(rightContent, "Local Backups", 18, -494)
  editor.lootProfileBackupHeader = backupHeader

  local backupFrame = CreateFrame("Frame", nil, rightContent, "BackdropTemplate")
  ApplyFrameSkin(backupFrame)
  backupFrame:EnableMouse(false)
  backupFrame:SetFrameLevel(sectionFrameLevel)
  editor.lootProfileBackupFrame = backupFrame

  local backupText = rightContent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  backupText:SetWidth(520)
  backupText:SetJustifyH("LEFT")
  backupText:SetText("Backups are saved locally on this client and include the Loot List, session history, and Live Board history.")
  editor.lootProfileBackupText = backupText

  local saveBackupBtn = CreateFrame("Button", nil, rightContent, "UIPanelButtonTemplate")
  saveBackupBtn:SetSize(118, 22)
  saveBackupBtn:SetText("Save Backup")
  editor.lootProfileSaveBackupBtn = saveBackupBtn

  local backupDropdown = CreateDropdownControl(editor, rightContent, 250, "Select Backup")
  editor.lootProfileBackupDropdown = backupDropdown

  local loadBackupBtn = CreateFrame("Button", nil, rightContent, "UIPanelButtonTemplate")
  loadBackupBtn:SetSize(108, 22)
  loadBackupBtn:SetText("Load Backup")
  editor.lootProfileLoadBackupBtn = loadBackupBtn

  local deleteBackupBtn = CreateFrame("Button", nil, rightContent, "UIPanelButtonTemplate")
  deleteBackupBtn:SetSize(118, 22)
  deleteBackupBtn:SetText("Delete Backup")
  editor.lootProfileDeleteBackupBtn = deleteBackupBtn

  if editor.__lootProfileRaidersCollapsed == nil then
    editor.__lootProfileRaidersCollapsed = true
  end

  page.__contentHeight = 1
end

INTERNAL_API.LIDCOUNCIL_BuildLootProfilesPageScaffold_Impl = BuildLootProfilesPageScaffold
