On long-haul flights, there are extra pilots on board so that the flight crew gets a chance to rest. When the pilot in command gets relieved and goes to the rest area, who is legally in charge of the airplane? Is the PIC role transferred to one of the pilots in the cockpit, or is the person who was relieved still ultimately in command? (I'm especially interested in what happens if the relieved PIC feels that the crew is mishandling the aircraft and wants to come back and take control).
The PIC is ultimately responsible for operation and safety, for everybody and everything on board, from pre-flight preparation till post-flight activities.
On long-haul flights there must be a sufficient number of relief crew members to substitute the nominal flight crew. This is regulated in national legislation and implemented in airline standard operation procedures (SOP). Details are apparently different from airline to airline.
The Captain is the highest ranking member of the flight crew, and will be designated as PIC. When the PIC takes a rest, there must be a qualified substitute available. That will be a Senior First Officer (SFO) in some airlines. In any case, the substitute will have gone through a dedicated training.
During the rest of the PIC, the substitute will act as PIC. The sleeping Captain remains PIC, and remains ultimately responsible. If there is an incident during his rest, it would have to be investigated if that incident was (partially) due to earlier decisions, actions or omissions, or if the problem was caused entirely by the relief crew.
Ultimately that the PIC (e.g. the Captain) is responsible for the entire flight. But responsibilities that form that role can be delegated (e.g. to the First Officer) as in the case of the crew member leaving the cockpit. Pilots operator in a type of no-blame culture. One pilot wouldn't be held accountable for another's action unless they knowingly encouraged or instructed them incorrectly whether they were sitting next to them or not.
Even if both crew are in the cockpit, the First Officer is encouraged to take action if they believe the Captain is making a poor decision. There have been accidents over the years where a Captain ruled whilst the First Officer sat there quietly. So, many years ago a cultural change was pushed to help reduce accidents where the outcome was avoidable if the other crew members spoke up.