Work and heat fluctuations in two-state systems: a trajectory thermodynamics formalism