Unified theory of thermal transport in crystals and glasses