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 RequireApi = shared.RequireApi
local BindMouseWheelScroll = shared.BindMouseWheelScroll
local RootDB = shared.RootDB
local ApplyFrameSkin = shared.ApplyFrameSkin
local MakeYellowSubHeader = shared.MakeYellowSubHeader
local GetCharacterKey = shared.GetCharacterKey
local GetEnabledLootProfileName = shared.GetEnabledLootProfileName or RequireApi("LIDCOUNCIL_GetEnabledLootProfileName", _G.LIDCOUNCIL_GetEnabledLootProfileName)
local GetLootProfile = shared.GetLootProfile
local GetPrimaryLootProfileName = shared.GetPrimaryLootProfileName
local GetLootProfileNames = shared.GetLootProfileNames
local IsLootProfileVisibleInEditor = shared.IsLootProfileVisibleInEditor or function(_, profileName)
  return profileName ~= nil and profileName ~= ""
end
local GetLootProfileAccessInfo = shared.GetLootProfileAccessInfo
local SetCurrentLootProfile = shared.SetCurrentLootProfile
local CreateLootProfile = shared.CreateLootProfile
local DeleteLootProfile = shared.DeleteLootProfile
local CopyLootProfile = shared.CopyLootProfile
local SetLootProfileEnabled = shared.SetLootProfileEnabled
local SetLootProfileRaid = shared.SetLootProfileRaid
local AddLootRollOption = shared.AddLootRollOption
local UpdateLootRollOption = shared.UpdateLootRollOption
local RemoveLootRollOption = shared.RemoveLootRollOption
local UpdateLootPointMode = shared.UpdateLootPointMode
local UpdateLootLidTableRow = shared.UpdateLootLidTableRow
local AddLootPointRule = shared.AddLootPointRule
local RemoveLootPointRule = shared.RemoveLootPointRule
local StartLootSession = shared.StartLootSession
local StopLootSession = shared.StopLootSession
local GetActiveLootSession = shared.GetActiveLootSession
local AddLootLedgerEntry = shared.AddLootLedgerEntry
local GetLootLedgerCharacters = shared.GetLootLedgerCharacters
local ResolveOptionalApiFunction = shared.ResolveOptionalApiFunction
local CreateCustomDropdownControl = shared.CreateCustomDropdownControl
local TrimText = shared.TrimText
local FormatDateTime = shared.FormatDateTime
local FormatDuration = shared.FormatDuration
local CreateSimpleEditBox = shared.CreateSimpleEditBox
local SetListButtonSelected = shared.SetListButtonSelected
local SetTextListButtonTextColor = shared.SetTextListButtonTextColor
local EnsureTextListButtons = shared.EnsureTextListButtons
local CreateSectionShell = shared.CreateSectionShell
local ClampLayoutValue = shared.ClampLayoutValue
local GetResponsiveSectionLayout = shared.GetResponsiveSectionLayout
local SetControlWidth = shared.SetControlWidth
local GetFontStringWidth = shared.GetFontStringWidth
local CreateSectionDivider = shared.CreateSectionDivider
local FlowLayoutControls = shared.FlowLayoutControls
local LayoutRollOptionRow = shared.LayoutRollOptionRow
local LayoutLidTableEditorRow = shared.LayoutLidTableEditorRow
local BuildRollOptionsText = shared.BuildRollOptionsText
local DEFAULT_ROLL_OPTION_ICON_CHOICES = shared.DEFAULT_ROLL_OPTION_ICON_CHOICES
local ResolveTextureAsset = shared.ResolveTextureAsset
local ResolveDefaultRollOptionIcon = shared.ResolveDefaultRollOptionIcon
local ResolveRollOptionIcon = shared.ResolveRollOptionIcon
local GetRollOptionIconLabel = shared.GetRollOptionIconLabel
local EnsureRollOptionRows = shared.EnsureRollOptionRows
local EnsureLidTableEditorRows = shared.EnsureLidTableEditorRows
local ExtractItemID = shared.ExtractItemID
local ExtractItemLinkName = shared.ExtractItemLinkName
local FormatPointRuleItemLabel = shared.FormatPointRuleItemLabel
local FormatRaidLootItemLabel = shared.FormatRaidLootItemLabel
local LOOT_ITEM_MENU_PAGE_SIZE = shared.LOOT_ITEM_MENU_PAGE_SIZE
local NormalizeInstanceTuple = shared.NormalizeInstanceTuple
local NormalizeEncounterTuple = shared.NormalizeEncounterTuple
local NormalizeLootInfo = shared.NormalizeLootInfo
local EnsureEncounterJournalLoaded = shared.EnsureEncounterJournalLoaded
local CollectRaidEntries = shared.CollectRaidEntries
local CollectRaidLootEntries = shared.CollectRaidLootEntries
local FindRaidEntryByProfile = shared.FindRaidEntryByProfile
local FindRaidLootEntry = shared.FindRaidLootEntry
local EnsureLootLiveBoardMode = shared.EnsureLootLiveBoardMode
local ClampMenuPage = shared.ClampMenuPage
local GetSelectedValuePage = shared.GetSelectedValuePage

local ACTIVATE_PROFILE_REQUIRED_TEXT = "Please go to Profile and activate one Profile first"
local NO_ACTIVE_PROFILE_CURRENT_TEXT = "Current Profile: No profile activated :("
local NO_ACTIVE_PROFILE_TEXT_COLOR = { 1.00, 0.35, 0.25, 1.00 }

local function SetCurrentProfileFont(fontString, text, color)
  if not fontString then
    return
  end
  fontString:SetText(text)
  fontString.__lid_customTextColor = color
  if color and fontString.SetTextColor then
    fontString:SetTextColor(color[1], color[2], color[3], color[4] or 1)
  elseif fontString.SetTextColor then
    fontString:SetTextColor(0.92, 0.96, 1.00, 1.00)
  end
end

local function GetLiveBoardContextProfileName()
  local root = RootDB()
  local enabledProfileName = TrimText(GetEnabledLootProfileName() or "")
  if enabledProfileName ~= "" and IsLootProfileVisibleInEditor(root, enabledProfileName) then
    return enabledProfileName
  end
  return nil
end
local EnsureLootProfileGroups = shared.EnsureLootProfileGroups
local FindLootProfileGroupIndex = shared.FindLootProfileGroupIndex
local RemoveLootProfileFromGroups = shared.RemoveLootProfileFromGroups
local ToggleLootProfileGroupCollapsed = shared.ToggleLootProfileGroupCollapsed
local GetLootProfileGroupName = shared.GetLootProfileGroupName
local BuildLootProfileNavRows = shared.BuildLootProfileNavRows
local BuildPointRulesText = shared.BuildPointRulesText
local ResolvePointMode = shared.ResolvePointMode
local GetLidTableRows = shared.GetLidTableRows
local BuildLidTableSummaryText = shared.BuildLidTableSummaryText
local POINT_MODE_CHOICES = shared.POINT_MODE_CHOICES
local GetSelectedProfileName = shared.GetSelectedProfileName

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

  local function EnsureLootLiveBoardEditorView(root)
    root = type(root) == "table" and root or RootDB()
    root.ui = root.ui or {}
    if root.ui.lootLiveBoardMode ~= "POPOUT" and root.ui.lootLiveBoardMode ~= "EMBEDDED" then
      root.ui.lootLiveBoardMode = "EMBEDDED"
    end
    if root.ui.lootLiveBoardView ~= "HISTORY" then
      root.ui.lootLiveBoardView = "BOARD"
    end
    return root.ui.lootLiveBoardView
  end

  local status = page:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
  status:SetPoint("TOPLEFT", 0, -46)
  status:SetText("Live Board ready")
  editor.lootLiveBoardStatus = status

  local summary = page:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  summary:SetPoint("TOPLEFT", 0, -70)
  summary:SetWidth(720)
  summary:SetJustifyH("LEFT")
  summary:SetText("")
  editor.lootLiveBoardSummary = summary

  local embedBtn = CreateFrame("Button", nil, page, "UIPanelButtonTemplate")
  embedBtn:SetSize(140, 22)
  embedBtn:SetPoint("TOPLEFT", 0, -102)
  embedBtn:SetText("Show Live Board")
  editor.lootLiveBoardEmbedBtn = embedBtn

  local historyBtn = CreateFrame("Button", nil, page, "UIPanelButtonTemplate")
  historyBtn:SetSize(120, 22)
  historyBtn:SetPoint("LEFT", embedBtn, "RIGHT", 10, 0)
  historyBtn:SetText("Show History")
  editor.lootLiveBoardHistoryBtn = historyBtn

  local popoutBtn = CreateFrame("Button", nil, page, "UIPanelButtonTemplate")
  popoutBtn:SetSize(120, 22)
  popoutBtn:SetPoint("LEFT", historyBtn, "RIGHT", 10, 0)
  popoutBtn:SetText("Pop Out Board")
  editor.lootLiveBoardPopoutBtn = popoutBtn

  local simulateBtn = CreateFrame("Button", nil, page, "UIPanelButtonTemplate")
  simulateBtn:SetSize(150, 22)
  simulateBtn:SetPoint("LEFT", popoutBtn, "RIGHT", 10, 0)
  simulateBtn:SetText("Run Fake Simulation")
  editor.lootLiveBoardSimulateBtn = simulateBtn

  local realSimulateBtn = CreateFrame("Button", nil, page, "UIPanelButtonTemplate")
  realSimulateBtn:SetSize(150, 22)
  realSimulateBtn:SetPoint("LEFT", simulateBtn, "RIGHT", 10, 0)
  realSimulateBtn:SetText("Run Real Simulation")
  editor.lootLiveBoardRealSimulateBtn = realSimulateBtn

  local hint = page:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  hint:SetPoint("TOPLEFT", 0, -132)
  hint:SetWidth(720)
  hint:SetJustifyH("LEFT")
  hint:SetText("Show Live Board opens the embedded host board by default. Show History switches the embedded area to saved Live Board history. Pop Out Board opens the Live Board in a separate resizable window. Run Fake Simulation uses the local Simulation preview. Run Real Simulation sends five simulated raid items from the current raid when available, otherwise from the selected profile raid or profile-season pool, without writing Loot List points.")
  editor.lootLiveBoardHint = hint

  local holder = CreateFrame("Frame", nil, page)
  holder:SetPoint("TOPLEFT", 0, -166)
  holder:SetPoint("TOPRIGHT", -6, -166)
  holder:SetHeight(480)
  editor.lootLiveBoardHolder = holder

  local inactiveOverlay = CreateFrame("Frame", nil, page, "BackdropTemplate")
  ApplyFrameSkin(inactiveOverlay)
  inactiveOverlay:SetPoint("TOPLEFT", page, "TOPLEFT", 0, -44)
  inactiveOverlay:SetPoint("BOTTOMRIGHT", page, "BOTTOMRIGHT", 0, 0)
  inactiveOverlay:EnableMouse(true)
  inactiveOverlay:SetFrameStrata("HIGH")
  inactiveOverlay:SetFrameLevel((page.GetFrameLevel and page:GetFrameLevel() or 0) + 20)
  inactiveOverlay:Hide()
  editor.lootLiveBoardInactiveOverlay = inactiveOverlay

  local inactiveText = inactiveOverlay:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
  inactiveText:SetPoint("CENTER", 0, 0)
  inactiveText:SetWidth(520)
  inactiveText:SetJustifyH("CENTER")
  inactiveText:SetJustifyV("MIDDLE")
  inactiveText:SetText(ACTIVATE_PROFILE_REQUIRED_TEXT)
  editor.lootLiveBoardInactiveText = inactiveText

  page.__contentHeight = 700

  local function LayoutLootLiveBoardPage(self)
    if not self then
      return
    end

    local contentWidth = math.max(260, math.floor(((page.GetWidth and page:GetWidth()) or 760) - 12))
    local viewportHeight = (
      editor.scrollFrame and editor.scrollFrame.GetHeight and editor.scrollFrame:GetHeight()
    ) or (
      page.GetHeight and page:GetHeight()
    ) or 680
    local pageHeight = math.max(320, math.floor(viewportHeight))
    local buttonHeight
    local hintTop
    local hintHeight
    local holderTop
    local holderHeight

    if self.lootLiveBoardSummary then
      self.lootLiveBoardSummary:SetWidth(contentWidth)
    end
    if self.lootLiveBoardHint then
      self.lootLiveBoardHint:SetWidth(contentWidth)
    end

    buttonHeight = FlowLayoutControls({
      self.lootLiveBoardEmbedBtn,
      self.lootLiveBoardHistoryBtn,
      self.lootLiveBoardPopoutBtn,
      self.lootLiveBoardSimulateBtn,
      self.lootLiveBoardRealSimulateBtn,
    }, 0, -102, contentWidth, 10, 8)
    hintTop = -102 - buttonHeight - 8
    if self.lootLiveBoardHint then
      self.lootLiveBoardHint:ClearAllPoints()
      self.lootLiveBoardHint:SetPoint("TOPLEFT", 0, hintTop)
    end

    hintHeight = math.max(18, math.ceil((self.lootLiveBoardHint and self.lootLiveBoardHint:GetStringHeight() or 0)))
    holderTop = hintTop - hintHeight - 12
    holderHeight = math.max(260, pageHeight - math.abs(holderTop) - 8)
    if self.lootLiveBoardHolder then
      self.lootLiveBoardHolder:ClearAllPoints()
      self.lootLiveBoardHolder:SetPoint("TOPLEFT", 0, holderTop)
      self.lootLiveBoardHolder:SetPoint("TOPRIGHT", -6, holderTop)
      self.lootLiveBoardHolder:SetHeight(holderHeight)
    end

    page.__contentHeight = math.max(pageHeight, math.abs(holderTop) + holderHeight + 8)
  end

  embedBtn:SetScript("OnClick", function()
    local selectedProfileName = TrimText(GetLiveBoardContextProfileName())
    if selectedProfileName == "" then
      return
    end
    local access = GetLootProfileAccessInfo(selectedProfileName)
    if not access.canOpenLiveBoard then
      return
    end
    local root = RootDB()
    root.ui = root.ui or {}
    root.ui.lootLiveBoardMode = "EMBEDDED"
    root.ui.lootLiveBoardView = "BOARD"
    local attachBoard = ResolveOptionalApiFunction("LIDCOUNCIL_AttachLiveLootBoard")
    if attachBoard then
      attachBoard(holder)
    end
    if editor.Refresh then editor:Refresh() end
  end)

  historyBtn:SetScript("OnClick", function()
    local root = RootDB()
    local profileName = TrimText(GetLiveBoardContextProfileName())
    if profileName == "" then
      return
    end
    root.ui = root.ui or {}
    root.ui.lootLiveBoardMode = "EMBEDDED"
    root.ui.lootLiveBoardView = "HISTORY"
    local attachHistory = ResolveOptionalApiFunction("LIDCOUNCIL_AttachLiveLootBoardHistory")
    if attachHistory then
      attachHistory(holder, profileName)
    end
    if editor.Refresh then editor:Refresh() end
  end)

  popoutBtn:SetScript("OnClick", function()
    local selectedProfileName = TrimText(GetLiveBoardContextProfileName())
    if selectedProfileName == "" then
      return
    end
    local access = GetLootProfileAccessInfo(selectedProfileName)
    if not access.canOpenLiveBoard then
      return
    end
    local root = RootDB()
    root.ui = root.ui or {}
    root.ui.lootLiveBoardMode = "POPOUT"
    local popoutBoard = ResolveOptionalApiFunction("LIDCOUNCIL_PopoutLiveLootBoard")
    if popoutBoard then
      popoutBoard()
    else
      local openBoard = ResolveOptionalApiFunction("LIDCOUNCIL_OpenLiveLootBoard")
      if openBoard then
        openBoard()
      end
    end
  end)

  simulateBtn:SetScript("OnClick", function()
    local selectedProfileName = TrimText(GetLiveBoardContextProfileName())
    if selectedProfileName == "" then
      return
    end
    local access = GetLootProfileAccessInfo(selectedProfileName)
    if not access.canOpenLiveBoard then
      return
    end
    local root = RootDB()
    root.ui = root.ui or {}
    root.ui.lootLiveBoardMode = "EMBEDDED"
    root.ui.lootLiveBoardView = "BOARD"
    local attachBoard = ResolveOptionalApiFunction("LIDCOUNCIL_AttachLiveLootBoard")
    if attachBoard then
      attachBoard(holder)
    end
    local runSimulation = ResolveOptionalApiFunction("LIDCOUNCIL_RunLiveLootSimulation")
    if runSimulation then
      runSimulation(selectedProfileName)
    end
    if editor.Refresh then editor:Refresh() end
  end)

  realSimulateBtn:SetScript("OnClick", function()
    local selectedProfileName = TrimText(GetLiveBoardContextProfileName())
    if selectedProfileName == "" then
      return
    end
    local access = GetLootProfileAccessInfo(selectedProfileName)
    if not access.canOpenLiveBoard then
      return
    end
    local root = RootDB()
    root.ui = root.ui or {}
    root.ui.lootLiveBoardMode = "EMBEDDED"
    root.ui.lootLiveBoardView = "BOARD"
    local attachBoard = ResolveOptionalApiFunction("LIDCOUNCIL_AttachLiveLootBoard")
    if attachBoard then
      attachBoard(holder)
    end
    local runRealSimulation = ResolveOptionalApiFunction("LIDCOUNCIL_RunLiveLootRealSimulation")
    if runRealSimulation then
      runRealSimulation(selectedProfileName)
    end
    if editor.Refresh then editor:Refresh() end
  end)

  editor.RefreshLootLiveBoardPage = function(self)
    local root = RootDB()
    local profileName = TrimText(GetLiveBoardContextProfileName())
    local hasActiveProfile = profileName ~= ""
    if self.lootLiveBoardInactiveOverlay then
      self.lootLiveBoardInactiveOverlay:SetShown(not hasActiveProfile)
    end
    if self.liveBoardTitle then
      self.liveBoardTitle:SetText(hasActiveProfile and "Live Board" or "Profile")
    end
    if hasActiveProfile then
      SetCurrentProfileFont(self.liveBoardCurrentLabel, "Current Profile: " .. profileName, nil)
    else
      SetCurrentProfileFont(self.liveBoardCurrentLabel, NO_ACTIVE_PROFILE_CURRENT_TEXT, NO_ACTIVE_PROFILE_TEXT_COLOR)
    end
    if self.lootLiveBoardHolder then
      self.lootLiveBoardHolder:SetShown(hasActiveProfile)
    end
    if self.lootLiveBoardStatus then
      self.lootLiveBoardStatus:SetShown(hasActiveProfile)
    end
    if self.lootLiveBoardSummary then
      self.lootLiveBoardSummary:SetShown(hasActiveProfile)
    end
    if self.lootLiveBoardHint then
      self.lootLiveBoardHint:SetShown(hasActiveProfile)
    end
    for _, control in ipairs({
      self.lootLiveBoardEmbedBtn,
      self.lootLiveBoardHistoryBtn,
      self.lootLiveBoardPopoutBtn,
      self.lootLiveBoardSimulateBtn,
      self.lootLiveBoardRealSimulateBtn,
    }) do
      if control and control.SetShown then
        control:SetShown(hasActiveProfile)
      end
    end
    if not hasActiveProfile then
      if self.lootLiveBoardStatus then
        self.lootLiveBoardStatus:SetText("No active profile")
        self.lootLiveBoardStatus:SetTextColor(1.00, 0.35, 0.25, 1.00)
      end
      for _, button in ipairs({
        self.lootLiveBoardEmbedBtn,
        self.lootLiveBoardHistoryBtn,
        self.lootLiveBoardPopoutBtn,
        self.lootLiveBoardSimulateBtn,
        self.lootLiveBoardRealSimulateBtn,
      }) do
        if button and button.Disable then
          button:Disable()
        end
      end
      LayoutLootLiveBoardPage(self)
      if self.currentPage == "LiveBoard" and self.UpdateScrollLayout then
        self:UpdateScrollLayout()
      end
      return
    end
    local access = GetLootProfileAccessInfo(profileName)
    local view = EnsureLootLiveBoardEditorView(root)
    local mode = EnsureLootLiveBoardMode(root)
    local liveSummaryProvider = ResolveOptionalApiFunction("LIDCOUNCIL_GetLiveLootBoardSummary")
    local liveSummaryState = liveSummaryProvider and liveSummaryProvider() or nil
    local activeSession, activeProfileName = GetActiveLootSession()
    local selfKey = TrimText(GetCharacterKey() or "")
    local simulationAvailable = type(root.profiles) == "table" and type(root.profiles.Simulation) == "table"
    local canRunSimulation = access.canOpenLiveBoard and simulationAvailable
    local canRunRealSimulation = access.canOpenLiveBoard
      and type(activeSession) == "table"
      and string.upper(TrimText(activeSession.status or "ACTIVE")) == "ACTIVE"
      and tostring(activeProfileName or "") == tostring(profileName)
      and selfKey ~= ""
      and string.lower(TrimText(activeSession.host or "")) == string.lower(selfKey)

    if not access.canOpenLiveBoard and view ~= "HISTORY" then
      root.ui = root.ui or {}
      root.ui.lootLiveBoardView = "HISTORY"
      view = "HISTORY"
    end

    if self.currentPage == "LiveBoard" then
      if view == "HISTORY" then
        local attachHistory = ResolveOptionalApiFunction("LIDCOUNCIL_AttachLiveLootBoardHistory")
        if attachHistory then
          attachHistory(holder, profileName)
        end
      elseif mode == "EMBEDDED" then
        local attachBoard = ResolveOptionalApiFunction("LIDCOUNCIL_AttachLiveLootBoard")
        if attachBoard then
          attachBoard(holder)
        end
      end
    end

    local disabledTextColor = { 0.46, 0.46, 0.50, 1.00 }
    local normalTextColor = { 0.92, 0.92, 0.96, 1.00 }
    SetListButtonSelected(self.lootLiveBoardEmbedBtn, access.canOpenLiveBoard and view == "BOARD")
    SetListButtonSelected(self.lootLiveBoardHistoryBtn, view == "HISTORY")
    SetTextListButtonTextColor(self.lootLiveBoardEmbedBtn, access.canOpenLiveBoard and (view == "BOARD" and { 1.00, 0.88, 0.32, 1.00 } or normalTextColor) or disabledTextColor)
    SetTextListButtonTextColor(self.lootLiveBoardHistoryBtn, view == "HISTORY" and { 1.00, 0.88, 0.32, 1.00 } or { 0.92, 0.92, 0.96, 1.00 })
    SetTextListButtonTextColor(self.lootLiveBoardPopoutBtn, access.canOpenLiveBoard and normalTextColor or disabledTextColor)
    SetTextListButtonTextColor(self.lootLiveBoardSimulateBtn, canRunSimulation and normalTextColor or disabledTextColor)
    SetTextListButtonTextColor(self.lootLiveBoardRealSimulateBtn, canRunRealSimulation and normalTextColor or disabledTextColor)
    if self.lootLiveBoardEmbedBtn and self.lootLiveBoardEmbedBtn.Enable then
      if access.canOpenLiveBoard then self.lootLiveBoardEmbedBtn:Enable() else self.lootLiveBoardEmbedBtn:Disable() end
    end
    if self.lootLiveBoardEmbedBtn and self.lootLiveBoardEmbedBtn.EnableMouse then
      self.lootLiveBoardEmbedBtn:EnableMouse(access.canOpenLiveBoard and true or false)
    end
    if self.lootLiveBoardPopoutBtn and self.lootLiveBoardPopoutBtn.Enable then
      if access.canOpenLiveBoard then self.lootLiveBoardPopoutBtn:Enable() else self.lootLiveBoardPopoutBtn:Disable() end
    end
    if self.lootLiveBoardPopoutBtn and self.lootLiveBoardPopoutBtn.EnableMouse then
      self.lootLiveBoardPopoutBtn:EnableMouse(access.canOpenLiveBoard and true or false)
    end
    if self.lootLiveBoardSimulateBtn and self.lootLiveBoardSimulateBtn.Enable then
      if canRunSimulation then self.lootLiveBoardSimulateBtn:Enable() else self.lootLiveBoardSimulateBtn:Disable() end
    end
    if self.lootLiveBoardSimulateBtn and self.lootLiveBoardSimulateBtn.EnableMouse then
      self.lootLiveBoardSimulateBtn:EnableMouse(canRunSimulation and true or false)
    end
    if self.lootLiveBoardRealSimulateBtn and self.lootLiveBoardRealSimulateBtn.Enable then
      if canRunRealSimulation then self.lootLiveBoardRealSimulateBtn:Enable() else self.lootLiveBoardRealSimulateBtn:Disable() end
    end
    if self.lootLiveBoardRealSimulateBtn and self.lootLiveBoardRealSimulateBtn.EnableMouse then
      self.lootLiveBoardRealSimulateBtn:EnableMouse(canRunRealSimulation and true or false)
    end
    if self.lootLiveBoardPopoutBtn and self.lootLiveBoardPopoutBtn.SetButtonState then
      self.lootLiveBoardPopoutBtn:SetButtonState(access.canOpenLiveBoard and "NORMAL" or "DISABLED", false)
    end
    if self.lootLiveBoardEmbedBtn and self.lootLiveBoardEmbedBtn.SetAlpha then
      self.lootLiveBoardEmbedBtn:SetAlpha(access.canOpenLiveBoard and 1 or 0.55)
    end
    if self.lootLiveBoardPopoutBtn and self.lootLiveBoardPopoutBtn.SetAlpha then
      self.lootLiveBoardPopoutBtn:SetAlpha(access.canOpenLiveBoard and 1 or 0.55)
    end
    if self.lootLiveBoardSimulateBtn and self.lootLiveBoardSimulateBtn.SetAlpha then
      self.lootLiveBoardSimulateBtn:SetAlpha(canRunSimulation and 1 or 0.55)
    end
    if self.lootLiveBoardRealSimulateBtn and self.lootLiveBoardRealSimulateBtn.SetAlpha then
      self.lootLiveBoardRealSimulateBtn:SetAlpha(canRunRealSimulation and 1 or 0.55)
    end

    if activeSession and tostring(activeProfileName or "") == tostring(profileName) then
      self.lootLiveBoardStatus:SetText("Active session board")
      self.lootLiveBoardStatus:SetTextColor(0.20, 0.90, 0.20, 1.00)
    elseif type(liveSummaryState) == "table" and liveSummaryState.isPreview then
      self.lootLiveBoardStatus:SetText("Simulation board")
      self.lootLiveBoardStatus:SetTextColor(1.00, 0.82, 0.32, 1.00)
    else
      self.lootLiveBoardStatus:SetText("No active session")
      self.lootLiveBoardStatus:SetTextColor(1.00, 0.35, 0.25, 1.00)
    end

    if not access.canOpenLiveBoard then
      self.lootLiveBoardStatus:SetText("History only access")
      self.lootLiveBoardStatus:SetTextColor(1.00, 0.82, 0.32, 1.00)
    end

    self.lootLiveBoardSummary:SetText("Embedded View: " .. (view == "HISTORY" and "History" or "Live Board"))

    if type(liveSummaryState) == "table" and tonumber(liveSummaryState.itemCount) and tonumber(liveSummaryState.itemCount) > 0 then
      local latestText = liveSummaryState.latestItem and (" | Latest: " .. tostring(liveSummaryState.latestItem)) or ""
      local prefix = liveSummaryState.isPreview and "Simulation" or "Tracked"
      self.lootLiveBoardSummary:SetText(
        string.format(
          "Embedded View: %s | %s Items: %d | Pending Choices: %d%s",
          (view == "HISTORY") and "History" or "Live Board",
          prefix,
          tonumber(liveSummaryState.itemCount) or 0,
          tonumber(liveSummaryState.pendingChoices) or 0,
          latestText
        )
      )
    end

    LayoutLootLiveBoardPage(self)

    if self.currentPage == "LiveBoard" and self.UpdateScrollLayout then
      self:UpdateScrollLayout()
    end
  end

  if page and page.HookScript then
    page:HookScript("OnSizeChanged", function()
      LayoutLootLiveBoardPage(editor)
    end)
  end
  editor.LayoutLootLiveBoardPage = LayoutLootLiveBoardPage
end

INTERNAL_API.LIDCOUNCIL_BuildLootLiveBoardPage_Impl = BuildLootLiveBoardPage

