Sub ConvertUGToCustomChordChartStyles()
    Dim doc As Document
    Dim para As Paragraph
    Dim txt As String
    Dim testTxt As String
    Dim currentStyleName As String
    Dim isChordLine As Boolean
    Dim processStarted As Boolean
    
    Set doc = ActiveDocument
    processStarted = False
    
    ' =========================================================================
    ' PASS 1: APPLY STYLES (FORWARD SCAN)
    ' =========================================================================
    For Each para In doc.Paragraphs
        currentStyleName = para.Style.NameLocal
        txt = Trim(para.Range.Text)
        
        ' Strip out the hidden paragraph mark character from the end
        If Len(txt) > 0 Then txt = Left(txt, Len(txt) - 1)
        txt = Trim(txt)
        
        ' Check if this line is a bracketed section header
        Dim isSectionHeader As Boolean
        isSectionHeader = (Left(txt, 1) = "[" And Right(txt, 1) = "]")
        
        ' Trigger processing the moment we hit the first section header
        If Not processStarted Then
            If isSectionHeader Then
                processStarted = True
            End If
        End If
        
        ' Process only lines belonging to the song body area
        If processStarted Then
            
            ' Handle empty rows inside the song body
            If txt = "" Then
                If StyleExists(doc, "2) Lyrics") Then
                    para.Style = doc.Styles("2) Lyrics")
                    para.Range.Font.Reset
                End If
                
            ' Format Section Headers like [Verse] or [Chorus]
            ElseIf isSectionHeader Then
                If StyleExists(doc, "3) Section") Then
                    para.Style = doc.Styles("3) Section")
                    para.Range.Font.Reset
                End If
                
            ' Sort and format Chords vs Lyrics
            ElseIf InStr(currentStyleName, "Preformatted") > 0 Or InStr(currentStyleName, "esjpp") > 0 Or InStr(currentStyleName, "Normal") > 0 Then
                
                ' Clean text markers like "Chorus:" or "(x2)" just for the chord evaluation
                testTxt = txt
                If InStr(testTxt, ":") > 0 Then testTxt = Trim(Mid(testTxt, InStr(testTxt, ":") + 1))
                If InStr(testTxt, "(") > 0 Then testTxt = Trim(Left(testTxt, InStr(testTxt, "(") - 1))
                testTxt = Trim(testTxt)
                
                isChordLine = False
                
                ' Check if it's an instrumental chord map line (solos, bar lines)
                If InStr(testTxt, "|") > 0 Or InStr(testTxt, "%") > 0 Then
                    isChordLine = True
                ' Check if it is a standard chord letter row (starts with A-G)
                ElseIf testTxt Like "[A-G]*" Or testTxt Like "[a-g]*" Or testTxt Like " *[A-G]*" Then
                    ' Ensure it does not contain a standard lowercase sentence word
                    If Not (testTxt Like "*[a-z][a-z][a-z][a-z][a-z]*") Then
                        isChordLine = True
                    End If
                End If
                
                ' Route to your exact numbered styles
                If isChordLine Then
                    If StyleExists(doc, "4) Chords") Then
                        para.Style = doc.Styles("4) Chords")
                        para.Range.Font.Reset
                    End If
                Else
                    If StyleExists(doc, "2) Lyrics") Then
                        para.Style = doc.Styles("2) Lyrics")
                        para.Range.Font.Reset
                    End If
                End If
            End If
        End If
    Next para
    
    ' =========================================================================
    ' PASS 2: BODY-ONLY SPACING CLEANUP (REVERSE SCAN FOR STABILITY)
    ' =========================================================================
    Dim i As Long
    For i = doc.Paragraphs.Count To 2 Step -1
        Dim currentPara As Paragraph
        Dim prevPara As Paragraph
        
        Set currentPara = doc.Paragraphs(i)
        Set prevPara = doc.Paragraphs(i - 1)
        
        ' Only optimize spacing if BOTH paragraphs belong to your custom song body styles
        If currentPara.Style.NameLocal = "2) Lyrics" And prevPara.Style.NameLocal = "2) Lyrics" Then
            If Trim(currentPara.Range.Text) = vbCr And Trim(prevPara.Range.Text) = vbCr Then
                currentPara.Range.Delete
            End If
        End If
    Next i
    
    ' =========================================================================
    ' PASS 3: PURGE IMPORTED WEB STYLES
    ' =========================================================================
    ' This loops through the document's styles. If it sees a leftover, non-built-in web 
    ' style style name like "esjpp" or "HTML Preformatted", it safely deletes it.
    Dim docStyle As Style
    On Error Resume Next ' Prevents crashing if Word protects a built-in style
    For Each docStyle In doc.Styles
        If docStyle.BuiltIn = False Then
            ' Check for "esjpp" or other web artifacts you might have seen in the past
            If InStr(docStyle.NameLocal, "esjpp") > 0 Or InStr(docStyle.NameLocal, "HTML") > 0 Then
                docStyle.Delete
            End If
        End If
    Next docStyle
    On Error GoTo 0
    
    MsgBox "Conversion complete! Web styles purged and spacing optimized.", vbInformation
End Sub

Function StyleExists(doc As Document, styleName As String) As Boolean
    On Error Resume Next
    StyleExists = Not doc.Styles(styleName) Is Nothing
    On Error GoTo 0
End Function
