Effective action approach to cosmological perturbations in dark energy and modified gravity