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

local RequireApi = INTERNAL_API.LIDPANELS_RequireApi_Impl
local RegisterTutorialTexture = INTERNAL_API.LIDPANELS_RegisterTutorialTexture_Impl
local BindMouseWheelScroll = INTERNAL_API.LIDPANELS_BindMouseWheelScroll_Impl
if type(RequireApi) ~= "function"
  or type(RegisterTutorialTexture) ~= "function"
  or type(BindMouseWheelScroll) ~= "function" then
  error("LidPanels missing shared editor page helpers.")
end

local ApplyFrameSkin = RequireApi("LIDPANELS_ApplyFrameSkin", _G.LIDPANELS_ApplyFrameSkin)
local CARD_W = RequireApi("LIDPANELS_CARD_W", _G.LIDPANELS_CARD_W)
local CARD_H = RequireApi("LIDPANELS_CARD_H", _G.LIDPANELS_CARD_H)

local function BuildTutorialPage(editor, tutorialPage)
  if not editor or not tutorialPage then return end

  local tutorialNavBg = CreateFrame("Frame", nil, tutorialPage, "BackdropTemplate")
  tutorialNavBg:SetPoint("TOPLEFT", 0, -44)
  tutorialNavBg:SetPoint("BOTTOMLEFT", 0, 0)
  tutorialNavBg:SetWidth(240)
  ApplyFrameSkin(tutorialNavBg)
  editor.tutorialNavBg = tutorialNavBg

  local tutNavProfilesBtn = CreateFrame("Button", nil, tutorialNavBg, "UIPanelButtonTemplate")
  tutNavProfilesBtn:SetSize(200, 20)
  tutNavProfilesBtn:SetPoint("TOPLEFT", 6, -18)
  tutNavProfilesBtn:SetText("Profiles Tutorial")
  tutNavProfilesBtn:SetScript("OnClick", function()
    if editor and editor.ShowTutorialSection then editor:ShowTutorialSection("Profiles") end
  end)
  editor.tutorialNavProfilesBtn = tutNavProfilesBtn

  local tutNavPanelsBtn = CreateFrame("Button", nil, tutorialNavBg, "UIPanelButtonTemplate")
  tutNavPanelsBtn:SetSize(200, 20)
  tutNavPanelsBtn:SetPoint("TOPLEFT", 6, -44)
  tutNavPanelsBtn:SetText("Panels Tutorial")
  tutNavPanelsBtn:SetScript("OnClick", function()
    if editor and editor.ShowTutorialSection then editor:ShowTutorialSection("Panels") end
  end)
  editor.tutorialNavPanelsBtn = tutNavPanelsBtn

  local tutNavCustomBtn = CreateFrame("Button", nil, tutorialNavBg, "UIPanelButtonTemplate")
  tutNavCustomBtn:SetSize(200, 20)
  tutNavCustomBtn:SetPoint("TOPLEFT", 6, -70)
  tutNavCustomBtn:SetText("My Backgrounds")
  tutNavCustomBtn:SetScript("OnClick", function()
    if editor and editor.ShowTutorialSection then editor:ShowTutorialSection("Custom") end
  end)
  editor.tutorialNavCustomBtn = tutNavCustomBtn

  local tutNavAddFolderBtn = CreateFrame("Button", nil, tutorialNavBg, "UIPanelButtonTemplate")
  tutNavAddFolderBtn:SetSize(200, 20)
  tutNavAddFolderBtn:SetPoint("TOPLEFT", 6, -96)
  tutNavAddFolderBtn:SetText("Add image into folder")
  tutNavAddFolderBtn:SetScript("OnClick", function()
    if editor and editor.ShowTutorialSection then editor:ShowTutorialSection("AddFolder") end
  end)
  editor.tutorialNavAddFolderBtn = tutNavAddFolderBtn

  local tutNavGroupsBtn = CreateFrame("Button", nil, tutorialNavBg, "UIPanelButtonTemplate")
  tutNavGroupsBtn:SetSize(200, 20)
  tutNavGroupsBtn:SetPoint("TOPLEFT", 6, -122)
  tutNavGroupsBtn:SetText("Group Tutorial")
  tutNavGroupsBtn:SetScript("OnClick", function()
    if editor and editor.ShowTutorialSection then editor:ShowTutorialSection("Groups") end
  end)
  editor.tutorialNavGroupsBtn = tutNavGroupsBtn

  local tutorialRightBg = CreateFrame("Frame", nil, tutorialPage, "BackdropTemplate")
  tutorialRightBg:SetPoint("TOPLEFT", tutorialNavBg, "TOPRIGHT", 12, 0)
  tutorialRightBg:SetPoint("BOTTOMRIGHT", 0, 0)
  ApplyFrameSkin(tutorialRightBg)
  tutorialRightBg:SetClipsChildren(true)
  editor.tutorialRightBg = tutorialRightBg

  local function CreateTutorialSection(parent, headerTex, steps)
    local scroll = CreateFrame("ScrollFrame", nil, parent, "UIPanelScrollFrameTemplate")
    scroll:SetPoint("TOPLEFT", 6, -6)
    scroll:SetPoint("BOTTOMRIGHT", -28, 6)
    BindMouseWheelScroll(scroll)

    local frame = CreateFrame("Frame", nil, scroll)
    frame:SetPoint("TOPLEFT", 0, 0)
    frame:SetSize(1, 1)
    frame:SetClipsChildren(true)
    scroll:SetScrollChild(frame)

    scroll:SetScript("OnSizeChanged", function(self)
      local w = self:GetWidth() - 28
      if w < 1 then w = 1 end
      frame:SetWidth(w)
      if editor and editor.LayoutTutorial then editor:LayoutTutorial() end
      if self.UpdateScrollChildRect then self:UpdateScrollChildRect() end
    end)

    local header = nil
    if headerTex and headerTex ~= "" then
      header = frame:CreateTexture(nil, "ARTWORK")
      header:SetPoint("TOPLEFT", 18, -18)
      RegisterTutorialTexture(editor, header, headerTex)
      header:SetTexCoord(0, 1, 0, 1)
    end

    local cards = {}
    for _, tex in ipairs(steps) do
      local card = CreateFrame("Frame", nil, frame)
      card:SetPoint("TOPLEFT", 0, 0)
      card:SetSize(CARD_W, CARD_H)
      local texObj = card:CreateTexture(nil, "ARTWORK")
      texObj:SetPoint("TOPLEFT", 0, 0)
      texObj:SetPoint("BOTTOMRIGHT", 0, 0)
      RegisterTutorialTexture(editor, texObj, tex)
      texObj:SetTexCoord(0, 1, 0, 1)
      card.texObj = texObj
      cards[#cards + 1] = card
    end

    return scroll, frame, header, cards
  end

  local panelsSteps = {
    "Interface\\AddOns\\LidPanels\\Media\\Lidpanels\\Tutorials\\Panels_2_Step1.tga",
    "Interface\\AddOns\\LidPanels\\Media\\Lidpanels\\Tutorials\\Panels_3_Step2.tga",
    "Interface\\AddOns\\LidPanels\\Media\\Lidpanels\\Tutorials\\Panels_4_Step3.tga",
    "Interface\\AddOns\\LidPanels\\Media\\Lidpanels\\Tutorials\\Panels_5_Step4.tga",
  }
  local profilesSteps = {
    "Interface\\AddOns\\LidPanels\\Media\\Lidpanels\\Tutorials\\Profile_2_Step1.tga",
    "Interface\\AddOns\\LidPanels\\Media\\Lidpanels\\Tutorials\\Profile_3_Step2.tga",
    "Interface\\AddOns\\LidPanels\\Media\\Lidpanels\\Tutorials\\Profile_4_Step3.tga",
    "Interface\\AddOns\\LidPanels\\Media\\Lidpanels\\Tutorials\\Profile_5_Step4.tga",
  }
  local customSteps = {
    "Interface\\AddOns\\LidPanels\\Media\\Lidpanels\\Tutorials\\CBG_2_Step1.tga",
    "Interface\\AddOns\\LidPanels\\Media\\Lidpanels\\Tutorials\\CBG_3_Step2.tga",
    "Interface\\AddOns\\LidPanels\\Media\\Lidpanels\\Tutorials\\CBG_4_Step3.tga",
    "Interface\\AddOns\\LidPanels\\Media\\Lidpanels\\Tutorials\\CBG_5_Step4.tga",
  }

  local tPanelsScroll, tPanelsFrame, tPanelsHeader, tPanelsCards =
    CreateTutorialSection(tutorialRightBg, "Interface\\AddOns\\LidPanels\\Media\\Lidpanels\\Tutorials\\Panels_1_Title.tga", panelsSteps)
  local tProfilesScroll, tProfilesFrame, tProfilesHeader, tProfilesCards =
    CreateTutorialSection(tutorialRightBg, "Interface\\AddOns\\LidPanels\\Media\\Lidpanels\\Tutorials\\Profiles_1_Title.tga", profilesSteps)
  local tCustomScroll, tCustomFrame, tCustomHeader, tCustomCards =
    CreateTutorialSection(tutorialRightBg, "Interface\\AddOns\\LidPanels\\Media\\Lidpanels\\Tutorials\\CBG_1_Custom_Banner.tga", customSteps)
  local tAddFolderScroll, tAddFolderFrame, tAddFolderHeader, tAddFolderCards =
    CreateTutorialSection(tutorialRightBg, "Interface\\AddOns\\LidPanels\\Media\\Lidpanels\\Tutorials\\CBG_1_Custom_Banner.tga", {})
  local tGroupsScroll, tGroupsFrame, tGroupsHeader, tGroupsCards =
    CreateTutorialSection(tutorialRightBg, nil, {})

  local tAddFolderNote = tAddFolderFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  tAddFolderNote:SetJustifyH("LEFT")
  tAddFolderNote:SetText(
    "|cffa87ae81. Add your image into Your_Backgrounds|r\n" ..
    "- Convert your image from |cffff4a4a.PNG|r / |cffff4a4a.JPG|r into a |cff4adf6b.TGA|r\n" ..
    "- Move your file into the \"AddOns/LidPanels_UserMedia/Your_Backgrounds\" folder."
  )

  local tAddFolderImage = tAddFolderFrame:CreateTexture(nil, "ARTWORK")
  RegisterTutorialTexture(editor, tAddFolderImage, "Interface\\AddOns\\LidPanels\\Media\\Lidpanels\\Tutorials\\CBG_6_Folder.tga")

  local tAddFolderAfter = tAddFolderFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  tAddFolderAfter:SetJustifyH("LEFT")
  tAddFolderAfter:SetText(
    "\n\n|cffa87ae82. Add the background into LidPanels:|r\n" ..
    "- Start World of Warcraft (or /reload)\n" ..
    "- Open LidPanels Ingame\n" ..
    "- Go to My Backgrounds\n" ..
    "- Create a Category\n" ..
    "- Add Background\n" ..
    "- Enter the exact name of your |cff4adf6b.TGA|r file, for example Xalatath (for Xalatath.tga)"
  )

  local tAddFolderPost = tAddFolderFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  tAddFolderPost:SetJustifyH("LEFT")
  tAddFolderPost:SetText(
    "\n\n|cffa87ae83. Add your background into your Panel:|r\n" ..
    "- Go to Panels\n" ..
    "- Select your Panel\n" ..
    "- Select \"Background\"\n" ..
    "- Choose a \"Custom Background\""
  )

  local tAddFolderPreviewNote = tAddFolderFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  tAddFolderPreviewNote:SetJustifyH("LEFT")
  tAddFolderPreviewNote:SetText("If you set the name correctly, you should see the preview of your image.")

  local tAddFolderPreviewImage = tAddFolderFrame:CreateTexture(nil, "ARTWORK")
  RegisterTutorialTexture(editor, tAddFolderPreviewImage, "Interface\\AddOns\\LidPanels\\Media\\Lidpanels\\Tutorials\\CBG_7_preview.tga")

  local tAddFolderPostImage = tAddFolderFrame:CreateTexture(nil, "ARTWORK")
  RegisterTutorialTexture(editor, tAddFolderPostImage, "Interface\\AddOns\\LidPanels\\Media\\Lidpanels\\Tutorials\\CBG_8_Select_Background.tga")

  local tGroupsIntro = tGroupsFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
  tGroupsIntro:SetJustifyH("LEFT")
  tGroupsIntro:SetText("Group Tutorial")

  local tGroupsNote = tGroupsFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  tGroupsNote:SetJustifyH("LEFT")
  tGroupsNote:SetText(
    "|cffa87ae81. Why use groups?|r\n" ..
    "- Groups help you keep long lists clean and organized.\n" ..
    "- You can use them in |cff4adf6bProfiles|r and in |cff4adf6bMy Backgrounds|r.\n" ..
    "- Groups are only for organization. They do not change or delete the content inside them."
  )

  local tGroupsAfter = tGroupsFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  tGroupsAfter:SetJustifyH("LEFT")
  tGroupsAfter:SetText(
    "|cffa87ae82. How to create and fill a group|r\n" ..
    "- Open |cff4adf6bProfiles|r and click |cff4adf6bCreate Group|r, or open |cff4adf6bMy Backgrounds|r and click |cff4adf6bAdd Group|r.\n" ..
    "- Enter a group name.\n" ..
    "- Left-click and drag a profile or category.\n" ..
    "- Drop it onto the target group row.\n" ..
    "- While dragging, the target group highlights so you can clearly see where it will land."
  )

  local tGroupsPost = tGroupsFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  tGroupsPost:SetJustifyH("LEFT")
  tGroupsPost:SetText(
    "|cffa87ae83. Managing groups|r\n" ..
    "- Click the |cff4adf6bsmall arrow on the far left|r to collapse or expand a group.\n" ..
    "- Click the group row itself to open its settings on the right side.\n" ..
    "- Use |cff4adf6bRename Group|r to rename it.\n" ..
    "- Use |cff4adf6bDelete Group|r to remove the group only. The profiles or categories inside become |cff4adf6bgroupless|r and stay intact.\n" ..
    "- To remove a single item from a group, drag it and release it back inside the list instead of dropping it on another group."
  )

  editor.tutorialPanelsScroll = tPanelsScroll
  editor.tutorialPanelsFrame = tPanelsFrame
  editor.tutorialPanelsHeader = tPanelsHeader
  editor.tutorialPanelsCards = tPanelsCards
  editor.tutorialProfilesScroll = tProfilesScroll
  editor.tutorialProfilesFrame = tProfilesFrame
  editor.tutorialProfilesHeader = tProfilesHeader
  editor.tutorialProfilesCards = tProfilesCards

  tProfilesScroll:Hide()
  tCustomScroll:Hide()
  tAddFolderScroll:Hide()
  tGroupsScroll:Hide()

  editor.tutorialCustomScroll = tCustomScroll
  editor.tutorialCustomFrame = tCustomFrame
  editor.tutorialCustomHeader = tCustomHeader
  editor.tutorialCustomCards = tCustomCards
  editor.tutorialAddFolderScroll = tAddFolderScroll
  editor.tutorialAddFolderFrame = tAddFolderFrame
  editor.tutorialAddFolderHeader = tAddFolderHeader
  editor.tutorialAddFolderCards = tAddFolderCards
  editor.tutorialAddFolderNote = tAddFolderNote
  editor.tutorialAddFolderImage = tAddFolderImage
  editor.tutorialAddFolderAfter = tAddFolderAfter
  editor.tutorialAddFolderPost = tAddFolderPost
  editor.tutorialAddFolderPostImage = tAddFolderPostImage
  editor.tutorialAddFolderPreviewNote = tAddFolderPreviewNote
  editor.tutorialAddFolderPreviewImage = tAddFolderPreviewImage
  editor.tutorialGroupsScroll = tGroupsScroll
  editor.tutorialGroupsFrame = tGroupsFrame
  editor.tutorialGroupsHeader = tGroupsHeader
  editor.tutorialGroupsCards = tGroupsCards
  editor.tutorialGroupsIntro = tGroupsIntro
  editor.tutorialGroupsNote = tGroupsNote
  editor.tutorialGroupsAfter = tGroupsAfter
  editor.tutorialGroupsPost = tGroupsPost

  local tutorialBanner = CreateFrame("Frame", nil, tutorialRightBg)
  tutorialBanner:SetPoint("TOPLEFT", 6, -6)
  tutorialBanner:SetPoint("BOTTOMRIGHT", -6, 6)
  local bannerTex = tutorialBanner:CreateTexture(nil, "ARTWORK")
  bannerTex:SetPoint("TOPLEFT", 18, -18)
  RegisterTutorialTexture(editor, bannerTex, "Interface\\AddOns\\LidPanels\\Media\\Lidpanels\\Tutorials\\LidPanels-Banner.tga")
  bannerTex:SetTexCoord(0, 1, 0, 1)
  local BANNER_W, BANNER_H = 3173, 1117
  local function LayoutBanner()
    local avail = (tProfilesFrame and tProfilesFrame:GetWidth()) or (tutorialRightBg:GetWidth() - 62)
    if not avail or avail < 1 then avail = 1 end
    local leftPad = 18
    local cardGap = 20
    local rowW = avail - leftPad
    local cardW = math.max(1, math.floor((rowW - cardGap) / 2))
    local headerW = cardW * 2 + cardGap
    local scale = headerW / BANNER_W
    if scale > 1 then scale = 1 end
    local headerH = math.floor((BANNER_H * scale) + 0.5)
    bannerTex:SetSize(headerW, headerH)
  end
  tutorialBanner:SetScript("OnShow", LayoutBanner)
  tutorialRightBg:HookScript("OnSizeChanged", LayoutBanner)
  editor.tutorialBanner = tutorialBanner

  function editor:ShowTutorialSection(which)
    self.tutorialSection = which or "Profiles"
    local function ResetScrollTop(scroll)
      if scroll and scroll.SetVerticalScroll then
        scroll:SetVerticalScroll(0)
      end
    end
    if self.tutorialSection == "Profiles" then
      if self.tutorialBanner then self.tutorialBanner:Hide() end
      if self.tutorialPanelsScroll then self.tutorialPanelsScroll:Hide() end
      if self.tutorialCustomScroll then self.tutorialCustomScroll:Hide() end
      if self.tutorialAddFolderScroll then self.tutorialAddFolderScroll:Hide() end
      if self.tutorialGroupsScroll then self.tutorialGroupsScroll:Hide() end
      if self.tutorialProfilesScroll then self.tutorialProfilesScroll:Show() end
      ResetScrollTop(self.tutorialProfilesScroll)
    elseif self.tutorialSection == "Custom" then
      if self.tutorialBanner then self.tutorialBanner:Hide() end
      if self.tutorialPanelsScroll then self.tutorialPanelsScroll:Hide() end
      if self.tutorialProfilesScroll then self.tutorialProfilesScroll:Hide() end
      if self.tutorialAddFolderScroll then self.tutorialAddFolderScroll:Hide() end
      if self.tutorialGroupsScroll then self.tutorialGroupsScroll:Hide() end
      if self.tutorialCustomScroll then self.tutorialCustomScroll:Show() end
      ResetScrollTop(self.tutorialCustomScroll)
    elseif self.tutorialSection == "AddFolder" then
      if self.tutorialBanner then self.tutorialBanner:Hide() end
      if self.tutorialPanelsScroll then self.tutorialPanelsScroll:Hide() end
      if self.tutorialProfilesScroll then self.tutorialProfilesScroll:Hide() end
      if self.tutorialCustomScroll then self.tutorialCustomScroll:Hide() end
      if self.tutorialGroupsScroll then self.tutorialGroupsScroll:Hide() end
      if self.tutorialAddFolderScroll then self.tutorialAddFolderScroll:Show() end
      ResetScrollTop(self.tutorialAddFolderScroll)
    elseif self.tutorialSection == "Groups" then
      if self.tutorialBanner then self.tutorialBanner:Hide() end
      if self.tutorialPanelsScroll then self.tutorialPanelsScroll:Hide() end
      if self.tutorialProfilesScroll then self.tutorialProfilesScroll:Hide() end
      if self.tutorialCustomScroll then self.tutorialCustomScroll:Hide() end
      if self.tutorialAddFolderScroll then self.tutorialAddFolderScroll:Hide() end
      if self.tutorialGroupsScroll then self.tutorialGroupsScroll:Show() end
      ResetScrollTop(self.tutorialGroupsScroll)
    elseif self.tutorialSection == "Panels" then
      if self.tutorialBanner then self.tutorialBanner:Hide() end
      if self.tutorialProfilesScroll then self.tutorialProfilesScroll:Hide() end
      if self.tutorialCustomScroll then self.tutorialCustomScroll:Hide() end
      if self.tutorialAddFolderScroll then self.tutorialAddFolderScroll:Hide() end
      if self.tutorialGroupsScroll then self.tutorialGroupsScroll:Hide() end
      if self.tutorialPanelsScroll then self.tutorialPanelsScroll:Show() end
      ResetScrollTop(self.tutorialPanelsScroll)
    else
      if self.tutorialProfilesScroll then self.tutorialProfilesScroll:Hide() end
      if self.tutorialPanelsScroll then self.tutorialPanelsScroll:Hide() end
      if self.tutorialCustomScroll then self.tutorialCustomScroll:Hide() end
      if self.tutorialAddFolderScroll then self.tutorialAddFolderScroll:Hide() end
      if self.tutorialGroupsScroll then self.tutorialGroupsScroll:Hide() end
      if self.tutorialBanner then self.tutorialBanner:Show() end
    end
    if self.LayoutTutorial then self:LayoutTutorial() end
  end

  function editor:LayoutTutorial()
    local function LayoutSection(frame, header, cards, scroll, note, noteImage, noteAfter, postText, postImage, previewNote, previewImage, noteAfterCards, introText, stepTitle)
      if not frame or not cards then return 400 end
      local avail = frame:GetWidth()
      if not avail or avail < 1 then return 400 end

      local leftPad = 18
      local cardGap = 20
      local rowW = avail - leftPad
      local cardW = math.max(1, math.floor((rowW - cardGap) / 2))
      local cardH = math.floor(cardW * 0.75)
      local headerW = cardW * 2 + cardGap
      local headerH = 0

      if header then
        headerH = math.floor(cardH * 0.6)
        header:SetSize(headerW, headerH)
        header:ClearAllPoints()
        header:SetPoint("TOPLEFT", leftPad, -18)
      end
      local y = -18
      if header then
        y = y - headerH - 20
      end

      local function LayoutNoteBlocks()
        if note then
          note:ClearAllPoints()
          note:SetPoint("TOPLEFT", leftPad, y)
          note:SetWidth(headerW)
          local noteH = note.GetStringHeight and note:GetStringHeight() or 0
          y = y - noteH - 14
        end
        if noteImage then
          local imgW = math.min(headerW, 520)
          local imgH = math.floor(imgW * 0.55)
          noteImage:ClearAllPoints()
          noteImage:SetPoint("TOPLEFT", leftPad, y)
          noteImage:SetSize(imgW, imgH)
          y = y - imgH - 20
        end
        if noteAfter then
          noteAfter:ClearAllPoints()
          noteAfter:SetPoint("TOPLEFT", leftPad, y)
          noteAfter:SetWidth(headerW)
          local afterH = noteAfter.GetStringHeight and noteAfter:GetStringHeight() or 0
          y = y - afterH - 20
        end
      end

      local function LayoutCards()
        local idx = 1
        local total = (cards and #cards) or 0
        local rows = math.ceil(total / 2)
        for _ = 1, rows do
          local placed = 0
          for col = 1, 2 do
            local card = cards[idx]
            if not card then break end
            card:SetSize(cardW, cardH)
            local x = leftPad + (col - 1) * (cardW + cardGap)
            card:ClearAllPoints()
            card:SetPoint("TOPLEFT", x, y)
            idx = idx + 1
            placed = placed + 1
          end
          if placed > 0 then
            y = y - cardH - 20
          end
        end
      end

      if introText then
        introText:ClearAllPoints()
        introText:SetPoint("TOPLEFT", leftPad, y)
        introText:SetWidth(headerW)
        local introH = introText.GetStringHeight and introText:GetStringHeight() or 0
        y = y - introH - 14
      end

      if noteAfterCards then
        LayoutCards()
        if stepTitle then
          stepTitle:ClearAllPoints()
          stepTitle:SetPoint("TOPLEFT", leftPad, y)
          stepTitle:SetWidth(headerW)
          local stepTitleH = stepTitle.GetStringHeight and stepTitle:GetStringHeight() or 0
          y = y - stepTitleH - 18
        end
        LayoutNoteBlocks()
      else
        LayoutNoteBlocks()
        LayoutCards()
      end

      if previewNote then
        previewNote:ClearAllPoints()
        previewNote:SetPoint("TOPLEFT", leftPad, y)
        previewNote:SetWidth(headerW)
        local previewH = previewNote.GetStringHeight and previewNote:GetStringHeight() or 0
        y = y - previewH - 14
      end
      if previewImage then
        local imgW = math.min(headerW, 520)
        local imgH = math.floor(imgW * 0.55)
        previewImage:ClearAllPoints()
        previewImage:SetPoint("TOPLEFT", leftPad, y)
        previewImage:SetSize(imgW, imgH)
        y = y - imgH - 20
      end
      if postText then
        postText:ClearAllPoints()
        postText:SetPoint("TOPLEFT", leftPad, y)
        postText:SetWidth(headerW)
        local postH = postText.GetStringHeight and postText:GetStringHeight() or 0
        y = y - postH - 14
      end
      if postImage then
        local imgW = math.min(headerW, 520)
        local imgH = math.floor(imgW * 0.55)
        postImage:ClearAllPoints()
        postImage:SetPoint("TOPLEFT", leftPad, y)
        postImage:SetSize(imgW, imgH)
        y = y - imgH - 20
      end

      local contentHeight = math.max(400, (-y) + 120)
      frame:SetHeight(contentHeight)
      if scroll and scroll.UpdateScrollChildRect then scroll:UpdateScrollChildRect() end
      return contentHeight
    end

    local which = self.tutorialSection or "Profiles"
    if which == "Profiles" then
      tutorialPage.__contentHeight = LayoutSection(self.tutorialProfilesFrame, self.tutorialProfilesHeader, self.tutorialProfilesCards, self.tutorialProfilesScroll)
    elseif which == "Custom" then
      tutorialPage.__contentHeight = LayoutSection(self.tutorialCustomFrame, self.tutorialCustomHeader, self.tutorialCustomCards, self.tutorialCustomScroll)
    elseif which == "AddFolder" then
      tutorialPage.__contentHeight = LayoutSection(
        self.tutorialAddFolderFrame,
        self.tutorialAddFolderHeader,
        self.tutorialAddFolderCards,
        self.tutorialAddFolderScroll,
        self.tutorialAddFolderNote,
        self.tutorialAddFolderImage,
        self.tutorialAddFolderAfter,
        self.tutorialAddFolderPost,
        self.tutorialAddFolderPostImage,
        self.tutorialAddFolderPreviewNote,
        self.tutorialAddFolderPreviewImage,
        true,
        self.tutorialAddFolderIntro,
        self.tutorialAddFolderStepTitle
      )
    elseif which == "Groups" then
      tutorialPage.__contentHeight = LayoutSection(
        self.tutorialGroupsFrame,
        self.tutorialGroupsHeader,
        self.tutorialGroupsCards,
        self.tutorialGroupsScroll,
        self.tutorialGroupsNote,
        nil,
        self.tutorialGroupsAfter,
        self.tutorialGroupsPost,
        nil,
        nil,
        nil,
        false,
        self.tutorialGroupsIntro,
        nil
      )
    elseif which == "Panels" then
      tutorialPage.__contentHeight = LayoutSection(self.tutorialPanelsFrame, self.tutorialPanelsHeader, self.tutorialPanelsCards, self.tutorialPanelsScroll)
    else
      tutorialPage.__contentHeight = (self.tutorialBanner and self.tutorialBanner:GetHeight()) or 600
    end
  end

  editor:ShowTutorialSection("Profiles")
  tutorialPage.__contentHeight = 1200
end

INTERNAL_API.LIDPANELS_BuildTutorialPage_Impl = BuildTutorialPage
