"""State detection and transition logic."""