Preheat oven to 375 degrees F.
In a large skillet, heat the olive oil over medium-high heat. Add the onion, red pepper, and mushrooms. Cook until tender, about 5 minutes. Add the spinach and cook until wilted, about 2 minutes. Add the garlic and cook for 1 minute. Remove from the heat and set aside.
In a small bowl, combine the ricotta cheese, egg, dried basil, and oregano. Stir until smooth.
Use a sharp knife to thinly slice the polenta, about 1/4-inch thick.
Grease a 2-quart rectangular baking dish (7 X 11-inch). Spread about 1/2 cup of the sauce on the bottom of the pan.
Place polenta rounds to cover the bottom of the pan. If you need to cut some of the rounds in half to make them fit evenly, that is fine.
Top with the vegetable mixture. Spoon the ricotta mixture over the vegetables and spread out evenly with a spatula. Top with 1 cup of the shredded mozzarella cheese. Pour half of the marinara sauce over the cheese. Add another layer of polenta rounds. Top with remaining sauce and the remaining mozzarella cheese, ending with the cheese. Sprinkle Parmesan cheese over the top.
Cover the baking dish with aluminum foil and bake for 25 minutes. Carefully remove the foil and continue baking for an additional 10 minutes, or until the cheese is melted and the dish is bubbly.
Turn the oven to broil. Broil for 1 to 2 minutes, or until the cheese is lightly browned on top. Remove from the oven and let cool for 10 minutes. Garnish with fresh basil and crushed red pepper flakes, if using. Cut into squares and serve warm.