Meter-scale, submicron-thick lightsail spacecraft, propelled to relativistic velocities via photon pressure using high-power density laser radiation, offer a potentially new route to space exploration within and beyond the solar system, posing substantial challenges for materials science and engineering. We analyze the structural and photonic design of flexible lightsails by developing a mesh-based multiphysics simulator based on linear elastic theory. We observe spin-stabilized flexible lightsail shapes and designs that are immune to shape collapse during acceleration and exhibit beam-riding stability despite deformations caused by photon pressure and thermal expansion. Excitingly, nanophotonic lightsails based on planar silicon nitride membranes patterned with suitable optical metagratings exhibit both mechanically and dynamically stable propulsion along the pump laser axis. These advances suggest that laser-driven acceleration of membrane-like lightsails to the relativistic speeds needed to access interstellar distances is conceptually feasible, and that their fabrication could be achieved by scaling up modern microfabrication technology.