Optical coupling and light trapping in thin-film solar cells are studied numerically using rigorous solutions of Maxwell's equations. The solar cell investigated consists of a ZnO/a-Si/ZnO/Ag structure, though results may be generalized to any thin-film solar cell technology. Varying diffraction gratings were studied, including periodic rectangular gratings, a four-level rectangular grating, and an arbitrary grating resembling a randomly textured surface. A genetic algorithm was used to optimize multi-level rectangular and arbitrary gratings. Solar cells with optimized multi-level rectangular gratings exhibit a 23% improvement over planar cells and 3.8% improvement over the optimal cell with periodic gratings. Solar cells with optimized arbitrarily shaped gratings exhibit a 29% improvement over planar cells and 9.0% improvement over the optimal cell with periodic gratings. The enhanced solar cell efficiencies for multi-level rectangular and arbitrary gratings are attributed to improved optical coupling and light trapping across the solar spectrum.