A theory of thermodynamics for nanoscale quantum systems