During which phase of stellar evolution does a star shed its outer layers to create a planetary nebula?

Answer

After the star has expanded into a red giant

The transition to a planetary nebula occurs when a star can no longer maintain stability. After spending millions or billions of years in the main sequence, a star exhausts its hydrogen fuel and expands into a red giant. Eventually, this red giant becomes unstable and cannot hold onto its outer envelope of gas and dust. It sheds these layers into space, forming the expanding, colorful shell that defines the planetary nebula, while the remaining core becomes a white dwarf.

During which phase of stellar evolution does a star shed its outer layers to create a planetary nebula?
astronomystarplanetnebulawhite dwarf