system
Given the following post, respond as if you're commenting on Reddit:
human
ELI5: What happens to your body muscle/fat when you work out in a calorie surplus vs a calorie deficit?

Like if you work out but you are in a deficit, how do you gain muscle?
gpt
It is complicated. 

Losing fat is one process. Working out and bulking up are a different process.  They have some overlap depending on overall nutrition.

Your cells prefers to metabolize sugars in the form of glucose and fructose, but is able to metabolize other compounds. As a simplification, glucose is stored in fat cells, and gets released to the blood as blood sugar. The compound, C6H12O6 for both glucose and fructose but in slightly different shapes, gets metabolized with oxygen to create water H2O and carbon dioxide CO2. There are other materials in fat cells, the stored glucose is just one of many things.

Your cells can also metabolize fatty acids which are different from fat cells. Fat cells store a lot of glucose, and they also contain fatty acids which can be released to the blood. When your cells metabolize these, they don't transform into water and carbon dioxide, they have additional compounds called ketones that are generated as part of a series of chemical reactions, the ketones which is where the keto diet gets its name. The switch to metabolizing fatty acids triggers a bunch of other body changes.

When you have a short-term calorie deficit your body releases glucose, often a first blast from the liver and and then a longer dose from the fat cells and other cells that keep it around. The body can refill those when you eat again. With a long-term calorie deficit the body causes fat cells to release fatty acids in higher doses. These can also be refilled in the longer term when you start eating again. If the calorie deficit continues, having lost both their stored glucose and their fatty acids, fat cells start to actually shrink and more permanent weight. That's why you need to maintain a calorie deficit for multiple weeks before you start to see permanent weight loss, it cannot be on-again, off-again dieting, since the cells will just refill themselves when you are in the off-again phase. 

So that's the burning side.

Gaining muscle is a different process involving muscle injury and damage. As part of working out you injure the cells, as a simplification they break open and cell organelles leak out. The damaged muscle materials fuse into satellite cells and repair into bigger, stronger muscle protein strands. This requires proper nutrition, especially having adequate proteins to build up and repair them.

If you don't have proper nutrition, your body will have difficulty with the repairs and won't make bigger, stronger muscle protein strands. Depending on your nutrition it might pull the protein from other areas, pull it from other cells, or it might just make a bad repair that leaves you weaker than before. This is why body builders who know what they're doing are sure to get protein shakes, meat, legumes, and lots of other building blocks. 

The chemical processes around metabolizing different materials, the processes of managing fatty acids, and the processes of muscle repair all have complex interactions.