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

INTERNAL_API.LIDCOUNCIL_CreateLootProfileLayoutHelpers_Impl = function(deps)
  deps = type(deps) == "table" and deps or {}

  local GetResponsiveSectionLayout = deps.GetResponsiveSectionLayout
  local ClampLayoutValue = deps.ClampLayoutValue
  local SetControlWidth = deps.SetControlWidth
  local GetFontStringWidth = deps.GetFontStringWidth
  local FlowLayoutControls = deps.FlowLayoutControls
  local LayoutRollOptionRow = deps.LayoutRollOptionRow
  local LayoutLidTableEditorRow = deps.LayoutLidTableEditorRow
  local TrimText = deps.TrimText

  local function RaiseDropdownControl(control, frameLevel)
    if not control then
      return
    end
    if control.SetFrameLevel then
      control:SetFrameLevel(frameLevel)
    end
    if control.button and control.button.SetFrameLevel then
      control.button:SetFrameLevel(frameLevel + 1)
    end
  end

  local function LayoutProfileContent(self, page)
    if not self or not self.lootProfilesRightContent then
      return
    end

    local _, navWidth = GetResponsiveSectionLayout(page, 240, 180, 300, 0.28)
    local navContentWidth = math.max(132, navWidth - 34)
    local profileRowWidth = math.max(116, navContentWidth - 16)
    local addButtonWidth = math.max(62, math.floor((navWidth - 24) / 2))
    local contentWidth = math.max(260, math.floor((self.lootProfilesRightScroll and self.lootProfilesRightScroll:GetWidth() or 560) - 28))
    local textWidth = math.max(220, contentWidth - 36)
    local raidLabelWidth = GetFontStringWidth(self.lootRaidLabel, 84)
    local pointModeLabelWidth = GetFontStringWidth(self.lootPointModeLabel, 92)
    local rollTemplateLabelWidth = GetFontStringWidth(self.lootRollTemplateLabel, 106)
    local raidWidth = ClampLayoutValue(textWidth - raidLabelWidth - 8 - 10 - (self.lootSaveRaidBtn and self.lootSaveRaidBtn:GetWidth() or 96), 120, 260)
    local rollTemplateWidth = ClampLayoutValue(textWidth - rollTemplateLabelWidth - 8, 160, 240)
    local pointModeWidth = ClampLayoutValue(textWidth - pointModeLabelWidth - 8, 120, 180)
    local pointLevelWidth = ClampLayoutValue(math.floor(textWidth * 0.16), 46, 72)
    local pointValueWidth = ClampLayoutValue(math.floor(textWidth * 0.18), 46, 70)
    local pointItemWidth = textWidth - pointLevelWidth - pointValueWidth - 20
    if pointItemWidth < 88 then
      local deficit = 88 - pointItemWidth
      local reduceValue = math.min(deficit, math.max(0, pointValueWidth - 40))
      pointValueWidth = pointValueWidth - reduceValue
      deficit = deficit - reduceValue
      if deficit > 0 then
        local reduceLevel = math.min(deficit, math.max(0, pointLevelWidth - 40))
        pointLevelWidth = pointLevelWidth - reduceLevel
      end
      pointItemWidth = textWidth - pointLevelWidth - pointValueWidth - 20
    end
    pointItemWidth = ClampLayoutValue(pointItemWidth, 88, 210)
    local rollEditWidth = ClampLayoutValue(math.floor(textWidth * 0.42), 120, 220)
    local accessTargetWidth = ClampLayoutValue(math.floor(textWidth * 0.46), 160, 250)
    local accessRoleWidth = ClampLayoutValue(math.floor(textWidth * 0.34), 140, 190)
    local sectionWidth = textWidth + 16
    local heroPreviewRatio = 0.41
    local heroPreviewHeightScale = 0.75
    if sectionWidth < 560 then
      heroPreviewRatio = 0.24
      heroPreviewHeightScale = 0.52
    elseif sectionWidth < 680 then
      heroPreviewRatio = 0.29
      heroPreviewHeightScale = 0.60
    elseif sectionWidth < 820 then
      heroPreviewRatio = 0.35
      heroPreviewHeightScale = 0.68
    end
    local heroPreviewWidth = ClampLayoutValue(math.floor(sectionWidth * heroPreviewRatio), 210, 390)
    local heroTextWidth = math.max(180, sectionWidth - heroPreviewWidth - 34)
    local heroActionButtonWidth = ClampLayoutValue(math.min(heroTextWidth - 16, 144), 110, 144)
    local masterInfoWidth = ClampLayoutValue(textWidth - 226, 110, textWidth - 74)
    local lidPointsWidth, lidItemLevelWidth, lidSetPieceWidth, lidOffSpecWidth, lidTransmogWidth, lidDisenchantWidth
    local heroButtonsHeight
    local accessControlsHeight
    local rollButtonsHeight
    local previewButtonsHeight
    local pointButtonsHeight
    local selectionCardHeight

    if self.lootProfilesNavBg then
      self.lootProfilesNavBg:SetWidth(navWidth)
    end
    if self.lootProfilesListContent then
      self.lootProfilesListContent:SetWidth(navContentWidth)
    end
    if self.lootProfilesPageText then
      self.lootProfilesPageText:SetWidth(math.max(48, navWidth - 152))
    end
    if self.lootAddGroupBtn then
      self.lootAddGroupBtn:SetWidth(addButtonWidth)
    end
    if self.lootAddProfileBtn then
      self.lootAddProfileBtn:SetWidth(addButtonWidth)
      self.lootAddProfileBtn:ClearAllPoints()
      self.lootAddProfileBtn:SetPoint("LEFT", self.lootAddGroupBtn, "RIGHT", 8, 0)
    end
    if self.lootDeleteGroupBtn then
      self.lootDeleteGroupBtn:SetWidth(addButtonWidth)
    end
    if self.lootDeleteProfileBtn then
      self.lootDeleteProfileBtn:SetWidth(addButtonWidth)
      self.lootDeleteProfileBtn:ClearAllPoints()
      self.lootDeleteProfileBtn:SetPoint("LEFT", self.lootDeleteGroupBtn, "RIGHT", 8, 0)
    end
    if self.lootProfilesFeedback then
      self.lootProfilesFeedback:SetWidth(navWidth - 16)
    end
    for _, button in ipairs(self.lootProfileButtons or {}) do
      local row = button.__row
      if row and row.type == "profile" then
        button:SetWidth(profileRowWidth)
      else
        button:SetWidth(navContentWidth)
      end
    end

    SetControlWidth(self.lootProfileSummary, heroTextWidth - 16)
    SetControlWidth(self.lootRaidHint, textWidth)
    SetControlWidth(self.lootRaidPreviewLabel, math.max(110, textWidth - 130))
    SetControlWidth(self.lootRaidPreviewMeta, math.max(110, textWidth - 130))
    SetControlWidth(self.lootRollOptionsText, textWidth)
    SetControlWidth(self.lootPointRulesText, textWidth)
    SetControlWidth(self.lootProfileAccessSummary, textWidth - 16)
    SetControlWidth(self.lootRaidDropdown, raidWidth)
    SetControlWidth(self.lootRollTemplateDropdown, rollTemplateWidth)
    SetControlWidth(self.lootPointModeDropdown, pointModeWidth)
    SetControlWidth(self.lootPointItemLevelEdit, pointLevelWidth)
    SetControlWidth(self.lootPointItemDropdown, pointItemWidth)
    SetControlWidth(self.lootPointValueEdit, pointValueWidth)
    SetControlWidth(self.lootRollOptionEdit, rollEditWidth)
    SetControlWidth(self.lootProfileRoleTargetDropdown, accessTargetWidth)
    SetControlWidth(self.lootProfileRoleDropdown, accessRoleWidth)
    SetControlWidth(self.lootProfileMasterCardValue, masterInfoWidth)
    SetControlWidth(self.lootProfileMasterCardMeta, masterInfoWidth)
    SetControlWidth(self.lootProfileCouncilCardValue, textWidth - 24)
    SetControlWidth(self.lootProfileCouncilCardMeta, textWidth - 24)
    SetControlWidth(self.lootProfileRaiderSummary, textWidth - 24)
    SetControlWidth(self.lootProfileRoleSelectionName, textWidth - 82)
    SetControlWidth(self.lootProfileRoleSelectionMeta, textWidth - 82)
    SetControlWidth(self.lootProfileRoleSelectionRole, textWidth - 24)
    SetControlWidth(self.lootProfileRoleSelectionHint, textWidth - 24)
    SetControlWidth(self.lootProfilePermissionPolicySummary, textWidth - 16)
    SetControlWidth(self.lootProfileHeroRaidPreviewLabel, heroPreviewWidth - 20)
    SetControlWidth(self.lootProfileHeroRaidPreviewEmpty, heroPreviewWidth - 20)
    for _, row in ipairs(self.lootProfilePermissionPolicyRows or {}) do
      if row and row.labelText then
        row.labelText:SetWidth(math.max(132, math.min(230, textWidth - 328)))
      end
    end

    for _, row in ipairs(self.lootRollOptionRows or {}) do
      if row:IsShown() then
        LayoutRollOptionRow(row, textWidth)
      end
    end
    for _, row in ipairs(self.lootLidTableRows or {}) do
      lidPointsWidth, lidItemLevelWidth, lidSetPieceWidth, lidOffSpecWidth, lidTransmogWidth, lidDisenchantWidth = LayoutLidTableEditorRow(row, textWidth)
    end

    local summaryHeight = math.max(38, math.ceil((self.lootProfileSummary and self.lootProfileSummary:GetStringHeight() or 0)))
    local accessSummaryHeight = math.max(18, math.ceil((self.lootProfileAccessSummary and self.lootProfileAccessSummary:GetStringHeight() or 0)))
    local permissionPolicySummaryHeight = math.max(16, math.ceil((self.lootProfilePermissionPolicySummary and self.lootProfilePermissionPolicySummary:GetStringHeight() or 0)))
    local councilMetaHeight = math.max(18, math.ceil((self.lootProfileCouncilCardMeta and self.lootProfileCouncilCardMeta:GetStringHeight() or 0)))
    local raiderSummaryHeight = math.max(18, math.ceil((self.lootProfileRaiderSummary and self.lootProfileRaiderSummary:GetStringHeight() or 0)))
    local raidHintHeight = math.max(18, math.ceil((self.lootRaidHint and self.lootRaidHint:GetStringHeight() or 0)))
    local raidPreviewHeight = 70
    local rollTextHeight = math.max(18, math.ceil((self.lootRollOptionsText and self.lootRollOptionsText:GetStringHeight() or 0)))
    local rollRowCount = tonumber(self.__lootRollOptionRowCount) or 0
    local rollRowSpacing = 32
    local rollRowsHeight = (rollRowCount > 0) and (((rollRowCount - 1) * rollRowSpacing) + 26) or 0
    local pointMode = tostring(self.__lootPointMode or "LIDTABLE")
    local isLidTable = pointMode ~= "CUSTOM"
    local ruleTextHeight = math.max(18, math.ceil((self.lootPointRulesText and self.lootPointRulesText:GetStringHeight() or 0)))
    local lidTableRowSpacing = 26
    local lidTableRowsHeight = (#(self.lootLidTableRows or {}) > 0) and ((((#self.lootLidTableRows) - 1) * lidTableRowSpacing) + 22) or 0
    local councilRowsHeight = tonumber(self.__lootProfileCouncilRowsHeight) or 0
    local raiderRowsHeight = tonumber(self.__lootProfileRaiderRowsHeight) or 0
    local councilRowsOffset = 56 + councilMetaHeight
    local raiderRowsOffset = 50 + raiderSummaryHeight
    local raidersCollapsed = self.__lootProfileRaidersCollapsed and true or false
    local raiderFrameHeight = raidersCollapsed and math.max(58, 52 + raiderSummaryHeight) or math.max(64, raiderRowsOffset + raiderRowsHeight + 12)
    local councilCardHeight = math.max(86, councilRowsOffset + councilRowsHeight + 12)
    local masterCardHeight = 82
    selectionCardHeight = 94
    local permissionPolicyRowCount = tonumber(self.__lootProfilePermissionPolicyRowCount) or #(self.lootProfilePermissionPolicyRows or {})
    local permissionPolicyRowHeight = 28
    local permissionPolicySectionGap = 12
    local permissionPolicyRowsHeight = 0
    for index = 1, permissionPolicyRowCount do
      local row = self.lootProfilePermissionPolicyRows and self.lootProfilePermissionPolicyRows[index] or nil
      if index > 1 and row and row.__isPermissionSection then
        permissionPolicyRowsHeight = permissionPolicyRowsHeight + permissionPolicySectionGap
      end
      permissionPolicyRowsHeight = permissionPolicyRowsHeight + ((index == permissionPolicyRowCount) and 24 or permissionPolicyRowHeight)
    end

    local heroFrameTop = -16
    local titleTop = heroFrameTop - 14
    local statusTop = heroFrameTop - 40
    local summaryTop = heroFrameTop - 62
    local actionTop = summaryTop - summaryHeight - 14

    self.lootProfilesTitle:ClearAllPoints()
    self.lootProfilesTitle:SetPoint("TOPLEFT", 34, titleTop)

    self.lootProfileStatus:ClearAllPoints()
    self.lootProfileStatus:SetPoint("TOPLEFT", 52, statusTop)

    if self.lootProfileStatusDot then
      self.lootProfileStatusDot:ClearAllPoints()
      self.lootProfileStatusDot:SetPoint("LEFT", self.lootProfileStatus, "LEFT", -16, 0)
    end

    self.lootProfileSummary:ClearAllPoints()
    self.lootProfileSummary:SetPoint("TOPLEFT", 34, summaryTop)

    local heroButtonTop = actionTop
    local heroButtonGap = 8
    heroButtonsHeight = 0
    for _, button in ipairs({
      self.lootActivateProfileBtn,
      self.lootDeactivateProfileBtn,
      self.lootCopyProfileBtn,
      self.lootClearSimulationContentBtn,
    }) do
      if button and button.IsShown and button:IsShown() then
        button:SetWidth(heroActionButtonWidth)
        button:ClearAllPoints()
        button:SetPoint("TOPLEFT", 34, heroButtonTop)
        local buttonHeight = math.floor((button.GetHeight and button:GetHeight()) or 22)
        heroButtonTop = heroButtonTop - buttonHeight - heroButtonGap
        heroButtonsHeight = heroButtonsHeight + buttonHeight + heroButtonGap
      end
    end
    if heroButtonsHeight > 0 then
      heroButtonsHeight = heroButtonsHeight - heroButtonGap
    end

    local heroFrameHeight = math.max(184, (heroFrameTop - actionTop) + heroButtonsHeight + 18)
    if self.lootProfileHeroFrame then
      self.lootProfileHeroFrame:ClearAllPoints()
      self.lootProfileHeroFrame:SetPoint("TOPLEFT", 18, heroFrameTop)
      self.lootProfileHeroFrame:SetSize(sectionWidth, heroFrameHeight)
    end
    if self.lootProfileHeroRaidPreviewFrame then
      local heroPreviewHeight = math.max(96, math.floor((heroFrameHeight - 28) * heroPreviewHeightScale))
      self.lootProfileHeroRaidPreviewFrame:ClearAllPoints()
      self.lootProfileHeroRaidPreviewFrame:SetPoint("TOPRIGHT", self.lootProfileHeroFrame, "TOPRIGHT", -4, -24)
      self.lootProfileHeroRaidPreviewFrame:SetSize(heroPreviewWidth, heroPreviewHeight)
      self.lootProfileHeroRaidPreviewFrame:Show()
    end
    if self.lootProfileSizeFrame then
      self.lootProfileSizeFrame:ClearAllPoints()
      if self.lootProfileHeroRaidPreviewFrame then
        self.lootProfileSizeFrame:SetPoint("TOPRIGHT", self.lootProfileHeroRaidPreviewFrame, "BOTTOMRIGHT", 0, -8)
      else
        self.lootProfileSizeFrame:SetPoint("TOPRIGHT", self.lootProfileHeroFrame, "TOPRIGHT", -8, -8)
      end
      self.lootProfileSizeFrame:SetSize(heroPreviewWidth, 18)
      if self.lootProfileSizeText then
        self.lootProfileSizeText:SetWidth(heroPreviewWidth)
      end
    end
    if self.lootProfileHeroRaidPreviewTexture then
      local frameWidth = heroPreviewWidth
      local frameHeight = math.max(96, math.floor((heroFrameHeight - 28) * heroPreviewHeightScale))
      local targetWidth = frameWidth
      local targetHeight = targetWidth * 9 / 16
      if targetHeight > frameHeight then
        targetHeight = frameHeight
        targetWidth = targetHeight * 16 / 9
      end
      self.lootProfileHeroRaidPreviewTexture:ClearAllPoints()
      self.lootProfileHeroRaidPreviewTexture:SetPoint("CENTER", self.lootProfileHeroRaidPreviewFrame, "CENTER", 0, 0)
      self.lootProfileHeroRaidPreviewTexture:SetSize(math.floor(targetWidth + 0.5), math.floor(targetHeight + 0.5))
    end
    if self.lootProfileHeroRaidPreviewEmpty then
      self.lootProfileHeroRaidPreviewEmpty:ClearAllPoints()
      self.lootProfileHeroRaidPreviewEmpty:SetPoint("CENTER", self.lootProfileHeroRaidPreviewFrame, "CENTER", 0, 0)
    end
    if self.lootProfileHeroRaidPreviewButton then
      self.lootProfileHeroRaidPreviewButton:SetAllPoints(self.lootProfileHeroRaidPreviewFrame)
    end

    local profileStatCards = self.lootProfileStatCards or {}
    local profileStatKeys = { "sessions", "items", "participants", "rolls" }
    local profileStatsPerRow = sectionWidth >= 560 and 4 or 2
    local profileStatGap = 8
    local profileStatCardWidth = math.floor((sectionWidth - ((profileStatsPerRow - 1) * profileStatGap)) / profileStatsPerRow)
    local profileStatCardHeight = 54
    local profileStatRows = math.ceil(#profileStatKeys / profileStatsPerRow)
    local profileStatsHeight = (profileStatRows * profileStatCardHeight) + math.max(0, profileStatRows - 1) * profileStatGap
    local profileStatsTop = heroFrameTop - heroFrameHeight - 14
    for index, key in ipairs(profileStatKeys) do
      local card = profileStatCards[key]
      if card and card.frame then
        local rowIndex = math.floor((index - 1) / profileStatsPerRow)
        local columnIndex = (index - 1) % profileStatsPerRow
        card.frame:ClearAllPoints()
        card.frame:SetPoint("TOPLEFT", 18 + (columnIndex * (profileStatCardWidth + profileStatGap)), profileStatsTop - (rowIndex * (profileStatCardHeight + profileStatGap)))
        card.frame:SetSize(profileStatCardWidth, profileStatCardHeight)
        card.frame:Show()
      end
    end

    local masterCardTop = profileStatsTop - profileStatsHeight - 16
    if self.lootProfileMasterCard then
      self.lootProfileMasterCard:ClearAllPoints()
      self.lootProfileMasterCard:SetPoint("TOPLEFT", 18, masterCardTop)
      self.lootProfileMasterCard:SetSize(sectionWidth, masterCardHeight)
    end
    if self.lootProfileTransferMasterBtn then
      self.lootProfileTransferMasterBtn:ClearAllPoints()
      self.lootProfileTransferMasterBtn:SetPoint("TOPRIGHT", self.lootProfileMasterCard, "TOPRIGHT", -12, -12)
    end

    local councilCardTop = masterCardTop - masterCardHeight - 14
    if self.lootProfileCouncilCard then
      self.lootProfileCouncilCard:ClearAllPoints()
      self.lootProfileCouncilCard:SetPoint("TOPLEFT", 18, councilCardTop)
      self.lootProfileCouncilCard:SetSize(sectionWidth, councilCardHeight)
    end
    if self.lootProfileCouncilRowsFrame then
      self.lootProfileCouncilRowsFrame:ClearAllPoints()
      self.lootProfileCouncilRowsFrame:SetPoint("TOPLEFT", self.lootProfileCouncilCard, "TOPLEFT", 12, -councilRowsOffset)
      self.lootProfileCouncilRowsFrame:SetPoint("TOPRIGHT", self.lootProfileCouncilCard, "TOPRIGHT", -12, -councilRowsOffset)
      self.lootProfileCouncilRowsFrame:SetHeight(math.max(1, councilRowsHeight))
    end

    local raiderFrameTop = councilCardTop - councilCardHeight - 14
    if self.lootProfileRaiderFrame then
      self.lootProfileRaiderFrame:ClearAllPoints()
      self.lootProfileRaiderFrame:SetPoint("TOPLEFT", 18, raiderFrameTop)
      self.lootProfileRaiderFrame:SetSize(sectionWidth, raiderFrameHeight)
      self.lootProfileRaiderFrame:Show()
    end
    if self.lootProfileRaiderToggleBtn then
      self.lootProfileRaiderToggleBtn:SetWidth(textWidth - 8)
      self.lootProfileRaiderToggleBtn:ClearAllPoints()
      self.lootProfileRaiderToggleBtn:SetPoint("TOPLEFT", self.lootProfileRaiderFrame, "TOPLEFT", 12, -10)
      self.lootProfileRaiderToggleBtn:SetHeight(28)
    end
    if self.lootProfileRaiderSummary then
      self.lootProfileRaiderSummary:ClearAllPoints()
      self.lootProfileRaiderSummary:SetPoint("TOPLEFT", self.lootProfileRaiderFrame, "TOPLEFT", 12, -48)
    end
    if self.lootProfileRaiderRowsFrame then
      self.lootProfileRaiderRowsFrame:ClearAllPoints()
      self.lootProfileRaiderRowsFrame:SetPoint("TOPLEFT", self.lootProfileRaiderFrame, "TOPLEFT", 12, -raiderRowsOffset)
      self.lootProfileRaiderRowsFrame:SetPoint("TOPRIGHT", self.lootProfileRaiderFrame, "TOPRIGHT", -12, -raiderRowsOffset)
      self.lootProfileRaiderRowsFrame:SetHeight(math.max(1, raidersCollapsed and 0 or raiderRowsHeight))
      if raidersCollapsed then
        self.lootProfileRaiderRowsFrame:Hide()
      else
        self.lootProfileRaiderRowsFrame:Show()
      end
    end

    local sectionButtonHeight = 28
    local sectionButtonGap = 12
    local sectionButtonGapX = 8
    local expandedSectionGap = 18
    local openSection = string.lower(TrimText(self.__lootProfileOpenSection))
    local accessExpanded = openSection == "access"
    local raidExpanded = openSection == "raid"
    local rollExpanded = openSection == "roll"
    local pointsExpanded = openSection == "points"
    local backupExpanded = openSection == "backups"

    local function SetAccordionButtonState(button, labelText, expanded)
      if not button then
        return
      end
      if button.SetText then
        button:SetText(tostring(labelText or button.baseLabel or ""))
      end
      if button.SetButtonState then
        button:SetButtonState(expanded and "PUSHED" or "NORMAL")
      end
      if button.LockHighlight and button.UnlockHighlight then
        if expanded then
          button:LockHighlight()
        else
          button:UnlockHighlight()
        end
      end
    end

    if self.lootProfileAccessHeader then self.lootProfileAccessHeader:Hide() end
    if self.lootRaidHeader then self.lootRaidHeader:Hide() end
    if self.lootRollHeader then self.lootRollHeader:Hide() end
    if self.lootPointsHeader then self.lootPointsHeader:Hide() end
    if self.lootProfileBackupHeader then self.lootProfileBackupHeader:Hide() end
    if self.lootProfileAccessList then self.lootProfileAccessList:Hide() end
    local sectionTabsTop = raiderFrameTop - raiderFrameHeight - 18
    local sectionTabsLeft = 34
    local sectionTabsWidth = math.max(220, textWidth - 8)
    local sectionTabWidth = math.max(78, math.floor((sectionTabsWidth - (sectionButtonGapX * 4)) / 5))

    if self.lootProfileAccessToggleBtn then
      self.lootProfileAccessToggleBtn:ClearAllPoints()
      self.lootProfileAccessToggleBtn:SetPoint("TOPLEFT", sectionTabsLeft, sectionTabsTop)
      self.lootProfileAccessToggleBtn:SetWidth(sectionTabWidth)
      self.lootProfileAccessToggleBtn:SetHeight(sectionButtonHeight)
      SetAccordionButtonState(self.lootProfileAccessToggleBtn, "Council Settings", accessExpanded)
      self.lootProfileAccessToggleBtn:Show()
    end
    if self.lootProfileRaidToggleBtn then
      self.lootProfileRaidToggleBtn:ClearAllPoints()
      self.lootProfileRaidToggleBtn:SetPoint("TOPLEFT", sectionTabsLeft + sectionTabWidth + sectionButtonGapX, sectionTabsTop)
      self.lootProfileRaidToggleBtn:SetWidth(sectionTabWidth)
      self.lootProfileRaidToggleBtn:SetHeight(sectionButtonHeight)
      SetAccordionButtonState(self.lootProfileRaidToggleBtn, "Raid Settings", raidExpanded)
      self.lootProfileRaidToggleBtn:Show()
    end
    if self.lootProfileRollToggleBtn then
      self.lootProfileRollToggleBtn:ClearAllPoints()
      self.lootProfileRollToggleBtn:SetPoint("TOPLEFT", sectionTabsLeft + ((sectionTabWidth + sectionButtonGapX) * 2), sectionTabsTop)
      self.lootProfileRollToggleBtn:SetWidth(sectionTabWidth)
      self.lootProfileRollToggleBtn:SetHeight(sectionButtonHeight)
      SetAccordionButtonState(self.lootProfileRollToggleBtn, "Roll Settings", rollExpanded)
      self.lootProfileRollToggleBtn:Show()
    end
    if self.lootProfilePointsToggleBtn then
      self.lootProfilePointsToggleBtn:ClearAllPoints()
      self.lootProfilePointsToggleBtn:SetPoint("TOPLEFT", sectionTabsLeft + ((sectionTabWidth + sectionButtonGapX) * 3), sectionTabsTop)
      self.lootProfilePointsToggleBtn:SetWidth(sectionTabWidth)
      self.lootProfilePointsToggleBtn:SetHeight(sectionButtonHeight)
      SetAccordionButtonState(self.lootProfilePointsToggleBtn, "Point Settings", pointsExpanded)
      self.lootProfilePointsToggleBtn:Show()
    end
    if self.lootProfileBackupToggleBtn then
      self.lootProfileBackupToggleBtn:ClearAllPoints()
      self.lootProfileBackupToggleBtn:SetPoint("TOPLEFT", sectionTabsLeft + ((sectionTabWidth + sectionButtonGapX) * 4), sectionTabsTop)
      self.lootProfileBackupToggleBtn:SetWidth(sectionTabWidth)
      self.lootProfileBackupToggleBtn:SetHeight(sectionButtonHeight)
      SetAccordionButtonState(self.lootProfileBackupToggleBtn, "Backups", backupExpanded)
      self.lootProfileBackupToggleBtn:Show()
    end

    local currentSectionTop = sectionTabsTop - sectionButtonHeight - sectionButtonGap

    if accessExpanded then
      local accessFrameTop = currentSectionTop
      local accessSummaryTop = accessFrameTop - 34
      local accessControlsTop = accessSummaryTop - accessSummaryHeight - 14

      self.lootProfileAccessSummary:Show()
      accessControlsHeight = FlowLayoutControls({
        self.lootProfileRoleTargetDropdown,
        self.lootProfileRoleDropdown,
        self.lootProfileApplyRoleBtn,
      }, 34, accessControlsTop, textWidth - 16, 10, 8)
      local accessSelectionTop = (accessControlsTop - accessFrameTop) - accessControlsHeight - 14
      local policySectionTop = accessSelectionTop - selectionCardHeight - 24
      local policyHeadersTop = policySectionTop - 18 - permissionPolicySummaryHeight - 18
      local policyRowsTop = policyHeadersTop - 22

      self.lootProfileAccessSummary:ClearAllPoints()
      self.lootProfileAccessSummary:SetPoint("TOPLEFT", 34, accessSummaryTop)

      local accessFrameHeight = math.max(440, math.abs(policyRowsTop) + permissionPolicyRowsHeight + 62)
      if self.lootProfileAccessFrame then
        self.lootProfileAccessFrame:ClearAllPoints()
        self.lootProfileAccessFrame:SetPoint("TOPLEFT", 18, accessFrameTop)
        self.lootProfileAccessFrame:SetSize(sectionWidth, accessFrameHeight)
        self.lootProfileAccessFrame:Show()
      end
      local accessControlFrameLevel = ((self.lootProfileAccessFrame and self.lootProfileAccessFrame.GetFrameLevel and self.lootProfileAccessFrame:GetFrameLevel()) or 1) + 6
      for _, control in ipairs({
        self.lootProfileRoleTargetDropdown,
        self.lootProfileRoleDropdown,
        self.lootProfileApplyRoleBtn,
      }) do
        if control and control.SetFrameLevel then
          control:SetFrameLevel(accessControlFrameLevel)
        end
        if control and control.button and control.button.SetFrameLevel then
          control.button:SetFrameLevel(accessControlFrameLevel + 1)
        end
      end
      if self.lootProfileRoleSelectionCard then
        self.lootProfileRoleSelectionCard:ClearAllPoints()
        self.lootProfileRoleSelectionCard:SetPoint("TOPLEFT", self.lootProfileAccessFrame, "TOPLEFT", 16, accessSelectionTop)
        self.lootProfileRoleSelectionCard:SetSize(sectionWidth - 32, selectionCardHeight)
        self.lootProfileRoleSelectionCard:Show()
      end
      if self.lootProfileRoleSelectionName then
        self.lootProfileRoleSelectionName:ClearAllPoints()
        self.lootProfileRoleSelectionName:SetPoint("TOPLEFT", self.lootProfileRoleSelectionCard, "TOPLEFT", 42, -26)
      end
      if self.lootProfileRoleSelectionMeta then
        self.lootProfileRoleSelectionMeta:ClearAllPoints()
        self.lootProfileRoleSelectionMeta:SetPoint("TOPLEFT", self.lootProfileRoleSelectionCard, "TOPLEFT", 42, -44)
      end
      if self.lootProfileRoleSelectionRole then
        self.lootProfileRoleSelectionRole:ClearAllPoints()
        self.lootProfileRoleSelectionRole:SetPoint("TOPLEFT", self.lootProfileRoleSelectionCard, "TOPLEFT", 12, -62)
      end
      if self.lootProfileRoleSelectionHint then
        self.lootProfileRoleSelectionHint:ClearAllPoints()
        self.lootProfileRoleSelectionHint:SetPoint("TOPLEFT", self.lootProfileRoleSelectionCard, "TOPLEFT", 12, -78)
      end
      if self.lootProfilePermissionPolicyTitle then
        self.lootProfilePermissionPolicyTitle:ClearAllPoints()
        self.lootProfilePermissionPolicyTitle:SetPoint("TOPLEFT", self.lootProfileAccessFrame, "TOPLEFT", 16, policySectionTop)
        self.lootProfilePermissionPolicyTitle:Show()
      end
      if self.lootProfilePermissionPolicySummary then
        self.lootProfilePermissionPolicySummary:ClearAllPoints()
        self.lootProfilePermissionPolicySummary:SetPoint("TOPLEFT", self.lootProfileAccessFrame, "TOPLEFT", 16, policySectionTop - 18)
        self.lootProfilePermissionPolicySummary:Show()
      end
      if self.lootProfilePermissionPolicyResetBtn then
        self.lootProfilePermissionPolicyResetBtn:ClearAllPoints()
        self.lootProfilePermissionPolicyResetBtn:SetPoint("TOPLEFT", self.lootProfileAccessFrame, "TOPLEFT", 16, policyRowsTop - permissionPolicyRowsHeight - 18)
        self.lootProfilePermissionPolicyResetBtn:SetWidth(190)
        self.lootProfilePermissionPolicyResetBtn:Show()
      end
      local headerLabelX = 16
      local policyCellWidth = ClampLayoutValue(math.floor((sectionWidth - 230) / 4), 54, 68)
      local policyColumnGap = 8
      local policyLabelWidth = math.max(132, sectionWidth - 32 - ((policyCellWidth + policyColumnGap) * 4))
      policyLabelWidth = math.min(policyLabelWidth, 230)
      local masterColumnX = headerLabelX + policyLabelWidth + 10
      local sessionHostColumnX = masterColumnX + policyCellWidth + policyColumnGap
      local councilColumnX = sessionHostColumnX + policyCellWidth + policyColumnGap
      local raiderColumnX = councilColumnX + policyCellWidth + policyColumnGap
      if self.lootProfilePermissionPolicyHeaderRight then
        self.lootProfilePermissionPolicyHeaderRight:ClearAllPoints()
        self.lootProfilePermissionPolicyHeaderRight:SetPoint("TOPLEFT", self.lootProfileAccessFrame, "TOPLEFT", headerLabelX, policyHeadersTop)
        self.lootProfilePermissionPolicyHeaderRight:SetWidth(policyLabelWidth)
        self.lootProfilePermissionPolicyHeaderRight:Show()
      end
      if self.lootProfilePermissionPolicyHeaderMaster then
        self.lootProfilePermissionPolicyHeaderMaster:ClearAllPoints()
        self.lootProfilePermissionPolicyHeaderMaster:SetPoint("TOPLEFT", self.lootProfileAccessFrame, "TOPLEFT", masterColumnX, policyHeadersTop)
        self.lootProfilePermissionPolicyHeaderMaster:SetWidth(policyCellWidth)
        self.lootProfilePermissionPolicyHeaderMaster:Show()
      end
      if self.lootProfilePermissionPolicyHeaderSessionHost then
        self.lootProfilePermissionPolicyHeaderSessionHost:ClearAllPoints()
        self.lootProfilePermissionPolicyHeaderSessionHost:SetPoint("TOPLEFT", self.lootProfileAccessFrame, "TOPLEFT", sessionHostColumnX, policyHeadersTop)
        self.lootProfilePermissionPolicyHeaderSessionHost:SetWidth(policyCellWidth)
        self.lootProfilePermissionPolicyHeaderSessionHost:Show()
      end
      if self.lootProfilePermissionPolicyHeaderCouncil then
        self.lootProfilePermissionPolicyHeaderCouncil:ClearAllPoints()
        self.lootProfilePermissionPolicyHeaderCouncil:SetPoint("TOPLEFT", self.lootProfileAccessFrame, "TOPLEFT", councilColumnX, policyHeadersTop)
        self.lootProfilePermissionPolicyHeaderCouncil:SetWidth(policyCellWidth)
        self.lootProfilePermissionPolicyHeaderCouncil:Show()
      end
      if self.lootProfilePermissionPolicyHeaderRaider then
        self.lootProfilePermissionPolicyHeaderRaider:ClearAllPoints()
        self.lootProfilePermissionPolicyHeaderRaider:SetPoint("TOPLEFT", self.lootProfileAccessFrame, "TOPLEFT", raiderColumnX, policyHeadersTop)
        self.lootProfilePermissionPolicyHeaderRaider:SetWidth(policyCellWidth)
        self.lootProfilePermissionPolicyHeaderRaider:Show()
      end
      local permissionPolicyRowCount = tonumber(self.__lootProfilePermissionPolicyRowCount) or 0
      local permissionPolicyRowOffset = 0
      for index, row in ipairs(self.lootProfilePermissionPolicyRows or {}) do
        if index > permissionPolicyRowCount then
          row:Hide()
        else
          if index > 1 and row.__isPermissionSection then
            permissionPolicyRowOffset = permissionPolicyRowOffset + permissionPolicySectionGap
          end
          local rowTop = policyRowsTop - permissionPolicyRowOffset
          row:ClearAllPoints()
          row:SetPoint("TOPLEFT", self.lootProfileAccessFrame, "TOPLEFT", 16, rowTop)
          row:SetPoint("TOPRIGHT", self.lootProfileAccessFrame, "TOPRIGHT", -16, rowTop)
          row:SetHeight(24)
          row:Show()
          if row.labelText then
            row.labelText:ClearAllPoints()
            row.labelText:SetPoint("LEFT", row, "LEFT", 8, 0)
            row.labelText:SetWidth(row.__isPermissionSection and (sectionWidth - 56) or (policyLabelWidth - 8))
          end
          if row.masterCell then
            row.masterCell:ClearAllPoints()
            row.masterCell:SetSize(policyCellWidth, 20)
            row.masterCell:SetPoint("LEFT", row, "LEFT", masterColumnX - 16, 0)
            row.masterCell:SetShown(not row.__isPermissionSection)
          end
          if row.sessionHostCell then
            row.sessionHostCell:ClearAllPoints()
            row.sessionHostCell:SetSize(policyCellWidth, 20)
            row.sessionHostCell:SetPoint("LEFT", row, "LEFT", sessionHostColumnX - 16, 0)
            row.sessionHostCell:SetShown(not row.__isPermissionSection)
          end
          if row.councilCell then
            row.councilCell:ClearAllPoints()
            row.councilCell:SetSize(policyCellWidth, 20)
            row.councilCell:SetPoint("LEFT", row, "LEFT", councilColumnX - 16, 0)
            row.councilCell:SetShown(not row.__isPermissionSection)
          end
          if row.raiderCell then
            row.raiderCell:ClearAllPoints()
            row.raiderCell:SetSize(policyCellWidth, 20)
            row.raiderCell:SetPoint("LEFT", row, "LEFT", raiderColumnX - 16, 0)
            row.raiderCell:SetShown(not row.__isPermissionSection)
          end
          permissionPolicyRowOffset = permissionPolicyRowOffset + permissionPolicyRowHeight
        end
      end
      currentSectionTop = accessFrameTop - accessFrameHeight - expandedSectionGap
    else
      self.lootProfileAccessSummary:Hide()
      if self.lootProfileAccessFrame then self.lootProfileAccessFrame:Hide() end
      if self.lootProfileRoleSelectionCard then self.lootProfileRoleSelectionCard:Hide() end
      if self.lootProfilePermissionPolicyTitle then self.lootProfilePermissionPolicyTitle:Hide() end
      if self.lootProfilePermissionPolicySummary then self.lootProfilePermissionPolicySummary:Hide() end
      if self.lootProfilePermissionPolicyResetBtn then self.lootProfilePermissionPolicyResetBtn:Hide() end
      if self.lootProfilePermissionPolicyHeaderRight then self.lootProfilePermissionPolicyHeaderRight:Hide() end
      if self.lootProfilePermissionPolicyHeaderMaster then self.lootProfilePermissionPolicyHeaderMaster:Hide() end
      if self.lootProfilePermissionPolicyHeaderSessionHost then self.lootProfilePermissionPolicyHeaderSessionHost:Hide() end
      if self.lootProfilePermissionPolicyHeaderCouncil then self.lootProfilePermissionPolicyHeaderCouncil:Hide() end
      if self.lootProfilePermissionPolicyHeaderRaider then self.lootProfilePermissionPolicyHeaderRaider:Hide() end
      for _, row in ipairs(self.lootProfilePermissionPolicyRows or {}) do
        row:Hide()
      end
      if self.lootProfileRoleTargetDropdown then self.lootProfileRoleTargetDropdown:Hide() end
      if self.lootProfileRoleDropdown then self.lootProfileRoleDropdown:Hide() end
      if self.lootProfileApplyRoleBtn then self.lootProfileApplyRoleBtn:Hide() end
    end

    if raidExpanded then
      local raidFrameTop = currentSectionTop
      local raidSelectTop = raidFrameTop - 40
      local raidPreviewTop = raidSelectTop - 34
      local raidHintTop = raidPreviewTop - raidPreviewHeight - 18
      local raidFrameHeight = math.max(110, (raidFrameTop - raidHintTop) + raidHintHeight + 18)

      if self.lootProfileRaidFrame then
        self.lootProfileRaidFrame:ClearAllPoints()
        self.lootProfileRaidFrame:SetPoint("TOPLEFT", 18, raidFrameTop)
        self.lootProfileRaidFrame:SetSize(sectionWidth, raidFrameHeight)
        self.lootProfileRaidFrame:Show()
      end

      self.lootRaidLabel:Show()
      self.lootRaidSeasonLabel:Hide()
      self.lootRaidSeasonDropdown:Hide()
      self.lootRaidDropdown:Show()
      self.lootSaveRaidBtn:Show()
      if self.lootRaidPreviewFrame then self.lootRaidPreviewFrame:Show() end
      if self.lootRaidPreviewLabel then self.lootRaidPreviewLabel:Show() end
      if self.lootRaidPreviewMeta then self.lootRaidPreviewMeta:Show() end
      self.lootRaidHint:Show()

      self.lootRaidLabel:ClearAllPoints()
      self.lootRaidLabel:SetPoint("TOPLEFT", 34, raidSelectTop)
      self.lootRaidDropdown:ClearAllPoints()
      self.lootRaidDropdown:SetPoint("LEFT", self.lootRaidLabel, "RIGHT", 8, 0)
      self.lootSaveRaidBtn:ClearAllPoints()
      self.lootSaveRaidBtn:SetPoint("LEFT", self.lootRaidDropdown, "RIGHT", 10, 0)
      if self.lootRaidPreviewFrame then
        self.lootRaidPreviewFrame:ClearAllPoints()
        self.lootRaidPreviewFrame:SetPoint("TOPLEFT", 34, raidPreviewTop)
        self.lootRaidPreviewFrame:SetSize(textWidth, raidPreviewHeight)
      end
      if self.lootRaidPreviewTexture and self.lootRaidPreviewFrame then
        self.lootRaidPreviewTexture:ClearAllPoints()
        self.lootRaidPreviewTexture:SetPoint("TOPLEFT", self.lootRaidPreviewFrame, "TOPLEFT", 8, -8)
        self.lootRaidPreviewTexture:SetSize(96, 54)
      end
      if self.lootRaidPreviewLabel and self.lootRaidPreviewTexture then
        self.lootRaidPreviewLabel:ClearAllPoints()
        self.lootRaidPreviewLabel:SetPoint("TOPLEFT", self.lootRaidPreviewTexture, "TOPRIGHT", 10, -2)
      end
      if self.lootRaidPreviewMeta and self.lootRaidPreviewLabel then
        self.lootRaidPreviewMeta:ClearAllPoints()
        self.lootRaidPreviewMeta:SetPoint("TOPLEFT", self.lootRaidPreviewLabel, "BOTTOMLEFT", 0, -6)
      end
      self.lootRaidHint:ClearAllPoints()
      self.lootRaidHint:SetPoint("TOPLEFT", 34, raidHintTop)

      currentSectionTop = raidFrameTop - raidFrameHeight - expandedSectionGap
    else
      if self.lootProfileRaidFrame then self.lootProfileRaidFrame:Hide() end
      self.lootRaidLabel:Hide()
      self.lootRaidSeasonLabel:Hide()
      self.lootRaidSeasonDropdown:Hide()
      self.lootRaidDropdown:Hide()
      self.lootSaveRaidBtn:Hide()
      if self.lootRaidPreviewFrame then self.lootRaidPreviewFrame:Hide() end
      if self.lootRaidPreviewTexture then self.lootRaidPreviewTexture:Hide() end
      if self.lootRaidPreviewLabel then self.lootRaidPreviewLabel:Hide() end
      if self.lootRaidPreviewMeta then self.lootRaidPreviewMeta:Hide() end
      self.lootRaidHint:Hide()
    end

    if rollExpanded then
      local rollFrameTop = currentSectionTop
      local rollTextTop = rollFrameTop - 40
      local hasRollText = TrimText(self.lootRollOptionsText and self.lootRollOptionsText:GetText() or "") ~= ""
      if not hasRollText then
        rollTextHeight = 0
      end
      local rollTemplateTop = hasRollText and (rollTextTop - rollTextHeight - 14) or rollTextTop
      local rollHintHeight = math.max(14, math.ceil((self.lootRollResponsiveHint and self.lootRollResponsiveHint:IsShown() and self.lootRollResponsiveHint:GetStringHeight() or 0)))
      local rollHeaderTop = rollTemplateTop - 34
      local rollRowsTop = rollHeaderTop - 18
      local rollEditTop = rollRowsTop - rollRowsHeight - 12

      self.lootRollOptionsText:SetShown(hasRollText)
      self.lootRollTemplateLabel:Show()
      self.lootRollTemplateDropdown:Show()
      if self.lootRollResponsiveHint then
        self.lootRollResponsiveHint:Hide()
      end
      self.lootRollPriorityHeader:Show()
      self.lootRollDescriptionHeader:Show()
      self.lootRollIconHeader:Show()
      self.lootRollColorHeader:Show()
      self.lootRollPointDetailsHeader:Show()
      self.lootRollCategoryHeader:Show()
      self.lootRollOptionEdit:Show()
      self.lootAddRollOptionBtn:Show()
      self.lootRemoveRollOptionBtn:Show()
      self.lootTestRollPopupBtn:Show()
      self.lootPreviewHostBoardBtn:Show()
      if self.lootRollExportBtn then self.lootRollExportBtn:Show() end
      if self.lootRollImportStringBtn then self.lootRollImportStringBtn:Show() end
      if self.lootRollImportProfileDropdown then self.lootRollImportProfileDropdown:Show() end

      self.lootRollOptionsText:ClearAllPoints()
      self.lootRollOptionsText:SetPoint("TOPLEFT", 34, rollTextTop)
      self.lootRollTemplateLabel:ClearAllPoints()
      self.lootRollTemplateLabel:SetPoint("TOPLEFT", 34, rollTemplateTop)
      self.lootRollTemplateDropdown:ClearAllPoints()
      self.lootRollTemplateDropdown:SetPoint("LEFT", self.lootRollTemplateLabel, "RIGHT", 8, 0)
      local rollHeaderMetrics = nil
      for i, row in ipairs(self.lootRollOptionRows or {}) do
        if row.__option then
          row:Show()
        end
        if row:IsShown() then
          row:ClearAllPoints()
          row:SetPoint("TOPLEFT", 34, rollRowsTop - ((i - 1) * rollRowSpacing))
          rollHeaderMetrics = rollHeaderMetrics or row.__layout
        end
      end
      if rollHeaderMetrics and self.lootRollResponsiveHint then
        local compactHint = rollHeaderMetrics.compactHint
        if compactHint and compactHint ~= "" then
          self.lootRollResponsiveHint:SetText(compactHint)
          self.lootRollResponsiveHint:ClearAllPoints()
          self.lootRollResponsiveHint:SetPoint("TOPLEFT", 34, rollTemplateTop - 30)
          self.lootRollResponsiveHint:SetWidth(textWidth)
          self.lootRollResponsiveHint:Show()
          rollHintHeight = math.max(14, math.ceil(self.lootRollResponsiveHint:GetStringHeight() or 0))
          rollHeaderTop = rollTemplateTop - 30 - rollHintHeight - 8
          rollRowsTop = rollHeaderTop - 18
          rollEditTop = rollRowsTop - rollRowsHeight - 12
          for i, row in ipairs(self.lootRollOptionRows or {}) do
            if row:IsShown() then
              row:ClearAllPoints()
              row:SetPoint("TOPLEFT", 34, rollRowsTop - ((i - 1) * rollRowSpacing))
            end
          end
        end
      end
      if rollHeaderMetrics then
        self.lootRollPriorityHeader:ClearAllPoints()
        self.lootRollPriorityHeader:SetPoint("TOPLEFT", 34 + (rollHeaderMetrics.priorityX or 0), rollHeaderTop)
        self.lootRollPriorityHeader:Show()
        self.lootRollDescriptionHeader:ClearAllPoints()
        self.lootRollDescriptionHeader:SetPoint("TOPLEFT", 34 + (rollHeaderMetrics.descriptionX or 88), rollHeaderTop)
        self.lootRollDescriptionHeader:Show()
        if rollHeaderMetrics.showIcon then
          self.lootRollIconHeader:ClearAllPoints()
          self.lootRollIconHeader:SetPoint("TOPLEFT", 34 + (rollHeaderMetrics.iconX or 0), rollHeaderTop)
          self.lootRollIconHeader:Show()
        else
          self.lootRollIconHeader:Hide()
        end
        if rollHeaderMetrics.showColor then
          self.lootRollColorHeader:ClearAllPoints()
          self.lootRollColorHeader:SetPoint("TOPLEFT", 34 + (rollHeaderMetrics.colorX or 0), rollHeaderTop)
          self.lootRollColorHeader:Show()
        else
          self.lootRollColorHeader:Hide()
        end
        if rollHeaderMetrics.showPointDetails then
          self.lootRollPointDetailsHeader:ClearAllPoints()
          self.lootRollPointDetailsHeader:SetPoint("TOPLEFT", 34 + (rollHeaderMetrics.pointDetailsX or 0), rollHeaderTop)
          self.lootRollPointDetailsHeader:Show()
        else
          self.lootRollPointDetailsHeader:Hide()
        end
        if rollHeaderMetrics.showCategory then
          self.lootRollCategoryHeader:ClearAllPoints()
          self.lootRollCategoryHeader:SetPoint("TOPLEFT", 34 + (rollHeaderMetrics.categoryX or 0), rollHeaderTop)
          self.lootRollCategoryHeader:Show()
        else
          self.lootRollCategoryHeader:Hide()
        end
      end
      rollButtonsHeight = FlowLayoutControls({
        self.lootRollOptionEdit,
        self.lootAddRollOptionBtn,
        self.lootRemoveRollOptionBtn,
      }, 34, rollEditTop, textWidth - 16, 10, 8)
      local testRollTop = rollEditTop - rollButtonsHeight - 34
      previewButtonsHeight = FlowLayoutControls({
        self.lootTestRollPopupBtn,
        self.lootPreviewHostBoardBtn,
        self.lootRollExportBtn,
        self.lootRollImportStringBtn,
        self.lootRollImportProfileDropdown,
      }, 34, testRollTop, textWidth - 16, 10, 8)
      local rollFrameHeight = math.max(134, (rollFrameTop - testRollTop) + previewButtonsHeight + 18)
      if self.lootProfileRollFrame then
        self.lootProfileRollFrame:ClearAllPoints()
        self.lootProfileRollFrame:SetPoint("TOPLEFT", 18, rollFrameTop)
        self.lootProfileRollFrame:SetSize(sectionWidth, rollFrameHeight)
        self.lootProfileRollFrame:Show()
      end
      currentSectionTop = rollFrameTop - rollFrameHeight - expandedSectionGap
    else
      if self.lootProfileRollFrame then self.lootProfileRollFrame:Hide() end
      self.lootRollOptionsText:Hide()
      self.lootRollTemplateLabel:Hide()
      self.lootRollTemplateDropdown:Hide()
      if self.lootRollResponsiveHint then self.lootRollResponsiveHint:Hide() end
      self.lootRollPriorityHeader:Hide()
      self.lootRollDescriptionHeader:Hide()
      self.lootRollIconHeader:Hide()
      self.lootRollColorHeader:Hide()
      self.lootRollPointDetailsHeader:Hide()
      self.lootRollCategoryHeader:Hide()
      self.lootRollOptionEdit:Hide()
      self.lootAddRollOptionBtn:Hide()
      self.lootRemoveRollOptionBtn:Hide()
      self.lootTestRollPopupBtn:Hide()
      self.lootPreviewHostBoardBtn:Hide()
      if self.lootRollExportBtn then self.lootRollExportBtn:Hide() end
      if self.lootRollImportStringBtn then self.lootRollImportStringBtn:Hide() end
      if self.lootRollImportProfileDropdown then self.lootRollImportProfileDropdown:Hide() end
      for _, row in ipairs(self.lootRollOptionRows or {}) do
        row:Hide()
      end
    end

    if pointsExpanded then
      local pointsFrameTop = currentSectionTop
      local pointModeTop = pointsFrameTop - 40
      local pointImportTop = pointModeTop - 34
      local pointImportHeight = 0
      local lidTableColumnsTop = pointImportTop - 34
      local lidTableRowsTop = lidTableColumnsTop - 24
      local customInputsTop = pointImportTop - 34
      local pointButtonsTop = customInputsTop - 32
      local ruleTextTop = (isLidTable and (lidTableRowsTop - lidTableRowsHeight - 14) or (pointButtonsTop - 36))

      self.lootPointModeLabel:Show()
      self.lootPointModeDropdown:Show()
      self.lootPointRulesText:Show()
      if self.lootPointExportBtn then self.lootPointExportBtn:Show() end
      if self.lootPointImportStringBtn then self.lootPointImportStringBtn:Show() end
      if self.lootPointImportProfileDropdown then self.lootPointImportProfileDropdown:Show() end

      self.lootPointModeLabel:ClearAllPoints()
      self.lootPointModeLabel:SetPoint("TOPLEFT", 34, pointModeTop)
      self.lootPointModeDropdown:ClearAllPoints()
      self.lootPointModeDropdown:SetPoint("LEFT", self.lootPointModeLabel, "RIGHT", 8, 0)
      self.lootPointsHint:Hide()
      pointImportHeight = FlowLayoutControls({
        self.lootPointExportBtn,
        self.lootPointImportStringBtn,
        self.lootPointImportProfileDropdown,
      }, 34, pointImportTop, textWidth - 16, 10, 8)
      lidTableColumnsTop = pointImportTop - pointImportHeight - 12
      lidTableRowsTop = lidTableColumnsTop - 24
      customInputsTop = pointImportTop - pointImportHeight - 12
      pointButtonsTop = customInputsTop - 32
      ruleTextTop = (isLidTable and (lidTableRowsTop - lidTableRowsHeight - 14) or (pointButtonsTop - 36))

      local lidTableLayout = nil
      if self.lootLidTableRows and self.lootLidTableRows[1] then
        lidTableLayout = self.lootLidTableRows[1].__layout
      end
      local showSetPiece = (lidTableLayout == nil) or (lidTableLayout.showSetPiece ~= false)
      local showOffSpec = (lidTableLayout == nil) or (lidTableLayout.showOffSpec ~= false)
      local showTransmog = (lidTableLayout == nil) or (lidTableLayout.showTransmog ~= false)
      local showDisenchant = (lidTableLayout == nil) or (lidTableLayout.showDisenchant ~= false)
      local pointsX = 84
      local itemLevelX = 64 + (lidPointsWidth or 52) + 10
      local setPieceX = itemLevelX + (lidItemLevelWidth or 68) + 10
      local offSpecX = setPieceX + (lidSetPieceWidth or 72) + 10
      local transmogX = offSpecX + (lidOffSpecWidth or 72) + 10
      local disenchantX = transmogX + (lidTransmogWidth or 72) + 10

      self.lootLidTablePointsLabel:ClearAllPoints()
      self.lootLidTablePointsLabel:SetPoint("TOPLEFT", pointsX, lidTableColumnsTop)
      self.lootLidTableItemLevelLabel:ClearAllPoints()
      self.lootLidTableItemLevelLabel:SetPoint("TOPLEFT", itemLevelX, lidTableColumnsTop)
      if showSetPiece then
        self.lootLidTableSetPieceLabel:ClearAllPoints()
        self.lootLidTableSetPieceLabel:SetPoint("TOPLEFT", setPieceX, lidTableColumnsTop)
      end
      if showOffSpec then
        self.lootLidTableOffSpecLabel:ClearAllPoints()
        self.lootLidTableOffSpecLabel:SetPoint("TOPLEFT", offSpecX, lidTableColumnsTop)
      end
      if showTransmog then
        self.lootLidTableTransmogLabel:ClearAllPoints()
        self.lootLidTableTransmogLabel:SetPoint("TOPLEFT", transmogX, lidTableColumnsTop)
      end
      if showDisenchant then
        self.lootLidTableDisenchantLabel:ClearAllPoints()
        self.lootLidTableDisenchantLabel:SetPoint("TOPLEFT", disenchantX, lidTableColumnsTop)
      end

      for i, row in ipairs(self.lootLidTableRows or {}) do
        row:ClearAllPoints()
        row:SetPoint("TOPLEFT", 34, lidTableRowsTop - ((i - 1) * lidTableRowSpacing))
        if isLidTable then
          row:Show()
        else
          row:Hide()
        end
      end

      if isLidTable then
        self.lootLidTablePointsLabel:Show()
        self.lootLidTableItemLevelLabel:Show()
        self.lootLidTableSetPieceLabel:SetShown(showSetPiece)
        self.lootLidTableOffSpecLabel:SetShown(showOffSpec)
        self.lootLidTableTransmogLabel:SetShown(showTransmog)
        self.lootLidTableDisenchantLabel:SetShown(showDisenchant)
        self.lootPointItemLevelEdit:Hide()
        self.lootPointItemDropdown:Hide()
        self.lootPointValueEdit:Hide()
        self.lootAddPointRuleBtn:Hide()
        self.lootAddItemPointRuleBtn:Hide()
        self.lootRemovePointRuleBtn:Hide()
      else
        self.lootLidTablePointsLabel:Hide()
        self.lootLidTableItemLevelLabel:Hide()
        self.lootLidTableSetPieceLabel:Hide()
        self.lootLidTableOffSpecLabel:Hide()
        self.lootLidTableTransmogLabel:Hide()
        self.lootLidTableDisenchantLabel:Hide()
        self.lootPointItemLevelEdit:Show()
        self.lootPointItemDropdown:Show()
        self.lootPointValueEdit:Show()
        self.lootAddPointRuleBtn:Show()
        self.lootAddItemPointRuleBtn:Show()
        self.lootRemovePointRuleBtn:Show()

        self.lootPointItemLevelEdit:ClearAllPoints()
        self.lootPointItemLevelEdit:SetPoint("TOPLEFT", 34, customInputsTop)
        self.lootPointItemDropdown:ClearAllPoints()
        self.lootPointItemDropdown:SetPoint("LEFT", self.lootPointItemLevelEdit, "RIGHT", 10, 0)
        self.lootPointValueEdit:ClearAllPoints()
        self.lootPointValueEdit:SetPoint("LEFT", self.lootPointItemDropdown, "RIGHT", 10, 0)
        pointButtonsHeight = FlowLayoutControls({
          self.lootAddPointRuleBtn,
          self.lootAddItemPointRuleBtn,
          self.lootRemovePointRuleBtn,
        }, 34, pointButtonsTop, textWidth - 16, 10, 8)
        ruleTextTop = pointButtonsTop - pointButtonsHeight - 12
      end

      self.lootPointRulesText:ClearAllPoints()
      self.lootPointRulesText:SetPoint("TOPLEFT", 34, ruleTextTop)

      local pointsFrameHeight = math.max(168, (pointsFrameTop - ruleTextTop) + ruleTextHeight + 18)
      if self.lootProfilePointsFrame then
        self.lootProfilePointsFrame:ClearAllPoints()
        self.lootProfilePointsFrame:SetPoint("TOPLEFT", 18, pointsFrameTop)
        self.lootProfilePointsFrame:SetSize(sectionWidth, pointsFrameHeight)
        self.lootProfilePointsFrame:Show()
      end
      currentSectionTop = pointsFrameTop - pointsFrameHeight - expandedSectionGap
    else
      if self.lootProfilePointsFrame then self.lootProfilePointsFrame:Hide() end
      self.lootPointModeLabel:Hide()
      self.lootPointModeDropdown:Hide()
      self.lootPointsHint:Hide()
      if self.lootPointExportBtn then self.lootPointExportBtn:Hide() end
      if self.lootPointImportStringBtn then self.lootPointImportStringBtn:Hide() end
      if self.lootPointImportProfileDropdown then self.lootPointImportProfileDropdown:Hide() end
      self.lootLidTablePointsLabel:Hide()
      self.lootLidTableItemLevelLabel:Hide()
      self.lootLidTableSetPieceLabel:Hide()
      self.lootLidTableOffSpecLabel:Hide()
      self.lootLidTableTransmogLabel:Hide()
      self.lootLidTableDisenchantLabel:Hide()
      self.lootPointItemLevelEdit:Hide()
      self.lootPointItemDropdown:Hide()
      self.lootPointValueEdit:Hide()
      self.lootAddPointRuleBtn:Hide()
      self.lootAddItemPointRuleBtn:Hide()
      self.lootRemovePointRuleBtn:Hide()
      self.lootPointRulesText:Hide()
      for _, row in ipairs(self.lootLidTableRows or {}) do
        row:Hide()
      end
    end

    if backupExpanded then
      local backupFrameTop = currentSectionTop
      local backupTextTop = backupFrameTop - 40
      local backupTextHeight = math.max(32, math.ceil((self.lootProfileBackupText and self.lootProfileBackupText:GetStringHeight() or 0)))
      local backupControlsTop = backupTextTop - backupTextHeight - 18
      local backupControlsHeight = 0

      if self.lootProfileBackupFrame then
        self.lootProfileBackupFrame:ClearAllPoints()
        self.lootProfileBackupFrame:SetPoint("TOPLEFT", 18, backupFrameTop)
        self.lootProfileBackupFrame:SetSize(sectionWidth, 130)
        self.lootProfileBackupFrame:Show()
      end
      if self.lootProfileBackupHeader then
        self.lootProfileBackupHeader:ClearAllPoints()
        self.lootProfileBackupHeader:SetPoint("TOPLEFT", 34, backupTextTop + 20)
        self.lootProfileBackupHeader:Show()
      end
      if self.lootProfileBackupText then
        self.lootProfileBackupText:ClearAllPoints()
        self.lootProfileBackupText:SetPoint("TOPLEFT", 34, backupTextTop)
        self.lootProfileBackupText:SetWidth(textWidth)
        self.lootProfileBackupText:Show()
      end
      if self.lootProfileSaveBackupBtn then self.lootProfileSaveBackupBtn:Show() end
      if self.lootProfileBackupDropdown then self.lootProfileBackupDropdown:Show() end
      if self.lootProfileLoadBackupBtn then self.lootProfileLoadBackupBtn:Show() end
      if self.lootProfileDeleteBackupBtn then self.lootProfileDeleteBackupBtn:Show() end
      backupControlsHeight = FlowLayoutControls({
        self.lootProfileSaveBackupBtn,
        self.lootProfileBackupDropdown,
        self.lootProfileLoadBackupBtn,
        self.lootProfileDeleteBackupBtn,
      }, 34, backupControlsTop, textWidth - 16, 10, 8)
      local backupFrameHeight = math.max(130, (backupFrameTop - backupControlsTop) + backupControlsHeight + 20)
      if self.lootProfileBackupFrame then
        self.lootProfileBackupFrame:SetSize(sectionWidth, backupFrameHeight)
      end
      currentSectionTop = backupFrameTop - backupFrameHeight - expandedSectionGap
    else
      if self.lootProfileBackupFrame then self.lootProfileBackupFrame:Hide() end
      if self.lootProfileBackupHeader then self.lootProfileBackupHeader:Hide() end
      if self.lootProfileBackupText then self.lootProfileBackupText:Hide() end
      if self.lootProfileSaveBackupBtn then self.lootProfileSaveBackupBtn:Hide() end
      if self.lootProfileBackupDropdown then self.lootProfileBackupDropdown:Hide() end
      if self.lootProfileLoadBackupBtn then self.lootProfileLoadBackupBtn:Hide() end
      if self.lootProfileDeleteBackupBtn then self.lootProfileDeleteBackupBtn:Hide() end
    end

    local dropdownFrameLevel = ((self.lootProfilesRightContent and self.lootProfilesRightContent.GetFrameLevel and self.lootProfilesRightContent:GetFrameLevel()) or 1) + 10
    if type(self.__lidcouncilCustomDropdowns) == "table" then
      for _, dropdown in ipairs(self.__lidcouncilCustomDropdowns) do
        RaiseDropdownControl(dropdown, dropdownFrameLevel)
      end
    end

    local contentBottom = math.abs(currentSectionTop) + 24
    page.__contentHeight = 1
    self.lootProfilesRightContent:SetSize(contentWidth, math.max(1, contentBottom))
    if self.lootProfilesRightScroll and self.lootProfilesRightScroll.UpdateScrollChildRect then
      self.lootProfilesRightScroll:UpdateScrollChildRect()
    end
    if self.lootProfilesListScroll and self.lootProfilesListScroll.UpdateScrollChildRect then
      self.lootProfilesListScroll:UpdateScrollChildRect()
    end
  end

  return {
    LayoutProfileContent = LayoutProfileContent,
  }
end
