Thermodynamic theory of epitaxial ferroelectric thin films with dense domain structures