We describe legume, a free electromagnetic solver that implements the guided-mode expansion method for patterned multilayer waveguides, or photonic crystal slabs. legume has a built-in tool for automatic differentiation, which makes it suitable for the inverse design of photonic crystal structures with desired physical properties. Compared to a previous version of the method (M. Minkov et al., 2020 [12]), here we introduce several new features of the code, we discuss additional technical aspects of the method and its numerical implementation. The novel features that are treated in this paper include: (i) the separation of modes according to their mirror symmetry with respect to a vertical symmetry plane of the photonic structure, (ii) the problem of polarization mixing in coupling to far-field radiation modes, and (iii) the description of active two-dimensional layers through a suitably formulated radiation-matter coupling Hamiltonian, allowing to describe the physics of both weakly and strongly coupled exciton-photon modes, the latter leading to photonic crystal polariton eigenmodes. Detailed and direct comparisons with rigorous coupled-wave analysis simulations are used to test the accuracy of the method and the numerical efficiency of the code. These newly added features of the legume code significantly increase the prospective applications of guided-mode expansion, making it a very practical and versatile tool enabling the design of advanced photonic structures and the description of radiation-matter interaction. Program summaryProgram Title:legumeCPC Library link to program files:https://doi.org/10.17632/kf3cwknx4d.1Developer's repository link:https://github.com/fancompute/legumeLicensing provisions: MITProgramming language: PythonNature of problem: Dispersion and radiative losses of photonic eigenmodes in patterned multilayer waveguides/photonic crystal slabs/periodic metasurfaces. Interaction of photonic modes with exciton resonances leading to exciton-polaritons. Inverse design by optimization of the parameters.Solution method: Finite-basis expansion using a basis of guided modes of an effective homogeneous waveguide, perturbation theory to describe coupling with far-field radiation. Quantum theory of excitons, photons and their interaction to describe the occurrence of exciton-polaritons. Automatic differentiation via Autograd to implement inverse design. In this upgraded version of the legume code we implement symmetrization with respect to a vertical mirror plane and light-matter interaction for exciton-polaritons. Inverse design has been described previously, here we focus on the new features and applications of the code.