Preheat the oven to 375°F and lightly grease a 9-inch baking dish with butter or nonstick spray.
If using fresh spinach, place it in a large dry skillet over medium heat and cook, tossing frequently, until fully wilted, then transfer to a colander to cool slightly.
When the spinach is cool enough to handle, squeeze it very firmly in small handfuls over the sink or in a clean towel to remove as much liquid as possible, then chop it roughly and set aside.
If using thawed frozen spinach, place it in a colander and squeeze it thoroughly with your hands or in a clean towel until almost no liquid drips out, then chop if needed and set aside.
In the same large skillet, melt the butter over medium heat and add the minced garlic, cooking and stirring just until fragrant and lightly softened, about one minute, without letting it brown.
Add the cream cheese cubes to the skillet and stir frequently as they soften and begin to melt into the butter and garlic, using the spoon to press out any lumps.
Pour in the heavy cream and continue cooking, stirring often, until the sauce is smooth, slightly thickened, and gently bubbling around the edges.
Stir in the grated Parmesan, nutmeg, salt, black pepper, and red pepper flakes if using, and cook for another minute until the cheese is fully melted and the sauce is velvety.
Add the well-drained chopped spinach to the skillet and fold it into the cream sauce, breaking up any clumps, until the spinach is evenly coated and the mixture is thick and creamy rather than watery.
Taste the creamed spinach and adjust the seasoning with additional salt, pepper, or Parmesan to taste, remembering that the topping will also add some saltiness.
Transfer the creamed spinach mixture to the prepared baking dish and spread it into an even layer with the back of the spoon or spatula.
In a small bowl, toss together the shredded Gruyère and the grated Parmesan for the topping, then sprinkle the cheese mixture evenly over the spinach in the baking dish.
Dot the top of the cheese with the small pieces of butter to encourage browning and extra richness as the gratin bakes.
Bake the gratin in the preheated oven until the spinach is hot and bubbling around the edges and the cheese is melted, about fifteen to eighteen minutes.
Switch the oven to broil on high and broil the gratin for one to three minutes, watching closely, until the top is deeply golden and crisp in spots without burning.
Remove the baking dish from the oven and let the gratin rest for five to ten minutes to set slightly before scooping into portions and serving warm.
If preparing ahead, assemble the creamed spinach in the baking dish with the cheese topping, cover tightly, and refrigerate up to two days, then bake straight from the fridge, adding a few extra minutes to the baking time before broiling to brown the top.
For reheating leftovers, warm the gratin in a 350°F oven until heated through and the top is re-crisped, or gently reheat individual portions in the microwave if needed, adding a splash of cream if the mixture seems too thick.