Preheat the oven to 375 F and lightly grease a 9x13 inch baking dish.
Bring a large pot of salted water to a boil. Cook the ziti pasta until al dente, then drain and set aside.
Heat the olive oil in a large skillet over medium-low heat. Add the sliced onions and cook slowly, stirring often, until deeply golden and caramelized, about 15 to 20 minutes.
Add the garlic to the skillet and cook for 1 more minute until fragrant.
Stir in the marinara sauce, salt, black pepper, and red pepper flakes if using. Let the sauce simmer for 5 minutes.
Add the fresh spinach to the warm sauce and stir until just wilted. Remove the skillet from the heat.
In a mixing bowl, combine the ricotta cheese, half of the parmesan cheese, lemon zest, basil, and parsley. Mix until smooth.
Spread a thin layer of the sauce in the prepared baking dish. Layer half of the cooked pasta over the sauce, then dollop half of the ricotta mixture on top.
Pour more sauce over the pasta and sprinkle with half of the mozzarella cheese. Repeat the layers with the remaining pasta, ricotta mixture, sauce, and mozzarella.
Bake uncovered for 25 minutes, until the cheese is melted, bubbly, and golden brown.
Remove the dish from the oven and let it rest for 10 minutes before serving. Garnish with extra basil or parsley if desired.