system
Given the following post, respond as if you're commenting on Reddit:
human
What causes a star to expand at the end of its life? Then what causes it to collapse after a certain point?

I understand that the fusion process triggers this expansion once the star starts fusing larger elements that don't release the same amounts of energy as hydrogen, helium, etc., but I don't understand why this translates to an expansion (e.g. a red giant).  Scientists will then say that the star "collapses on itself" leading to a supernova, dwarf, etc. but it's not intuitive to me why this happens.  Someone wanna school me?
gpt
Stars have what is called hydrostatic equilibrium for much of their life. In essence, this is a balance between pressure in the star against the force of gravity.

Gravity works to compress the star at all times, and as the star is "squeezed" this heats up the interior of the star. Eventually it gets hot enough to reach the ignition point of an element (initially hydrogen). As this element burns, it generates pressure that pushes outwards against the force of gravity, and where these forces balance is the surface of the star.

In a red giant, the hydrogen burning in the core is extinguished. As the "fire" goes out, the pressure balancing gravity lessens, and the star begins to contract. As it contracts, the heat rises. The core, where it is hottest and had been be burning, is depleted of hydrogen, but the outer shell of the star is not, and eventually the contraction raises the temperature there to ignite that hydrogen. As that hydrogen ignites, the pressure again rises and pushes out against gravity, swelling the size of the star. The greater temperature leads to an increase in reaction rates, which means a greater pressure pushing out against gravity, and thus the "balance" is further out, so the star expands.

When the star has depleted all of its fuel, there is no longer at element that it can burn to release energy, and thus nothing can balance out the force of gravity, causing the star to contract. At this point, the force of gravity compresses the matter of the star, resulting in various end of life stages depending on the mass collapsing.