Conditions for prisoners in court custody suites in England are among the worst inspectors have ever seen and are "an accident waiting to happen", a report has revealed.
An investigation of 97 court houses with custody facilities found some detainees were kept in "squalid" cells full of racist and abusive graffiti with filthy toilets, while many were forced to spend up to 10 hours in tiny windowless rooms.
In some custody areas conditions were so bad they posed a threat to the health of those being held, and even the staff.
And there was a widespread "dangerous disregard" for the risks prisoners posed themselves and others.
Since the report was drafted a female custody officer, Lorraine Barwell, died after being attacked on duty at London's Blackfriars Crown Court in June in an alleged murder by a prisoner, and there has been the first death of a detainee in court custody for many years.
The report said: "We cannot make any link between the concerns we identify in this report and those terrible events ... (but) we are clear in our view that there is a real risk of further serious incidents in future."
The report's discoveries are so concerning Nick Hardwick, the chief inspector of prisons, recommended that ministers order an urgent overhaul of detainee treatment and court custody conditions by the courts and tribunals service.
He said: "We found filthy, squalid cells covered in old graffiti. The needs of women, children or other detainees with particular needs were often not understood or addressed. Routine security measures were often disproportionate or inconsistent. Health care was inadequate.
"Of most concern and despite, in many cases, the best efforts of custody staff, we found a dangerous disregard for the risks detainees might pose to themselves or others. Court custody is an accident waiting to happen."
The investigation was carried out by government prisons inspectors between 2012 and 2014.
Problems stemmed from "no single organisation" having a leadership responsibility at local or national level.
Most concerning was a lack of any meaningful risk assessment for detainees when they arrived in custody or were released, the report found.
Priorities for staff were getting prisoners to court on time, with little emphasis on ensuring that those detained did not spend long periods in court cells after their appearance.
Even when cases were heard promptly, many people spent too long in court custody awaiting transfer, while in some courts people who had failed to pay fines, even for very minor matters, were put in a cell.
The report said: "We found some of the worst custody conditions we have inspected. The treatment of detainees and the conditions in custody suites were very low priorities for the different organisations involved, which failed to adequately co-ordinate their custody roles."
:: Physical conditions of custody areas were poor - to which managers were often oblivious - with deep cleaning and decorating of cells "clearly" being neglected for years. Some had "scarcely an inch of cell wall that was not covered in graffiti, some of it racist, offensive to women, or containing abuse and threats against named individuals", while others had toilets so filthy and offering such little privacy that detainees were reluctant to use them.
:: People appearing in court with medication prescribed in police custody often did not have enough to last them through a long day, while court houses usually had inadequate first aid kits, inspectors found. None had defibrillators.
:: Provision for pregnant, elderly or disabled people was "almost always inadequate". Women, men and children were often transferred in cell vehicles together, exposing women to potential harassment.
:: Custody staff were not trained in risk assessment, meaning serious risks, such as detainees harming themselves or others, or falling ill, were not managed.
:: Detainees were handcuffed too often, and some disabled people were led to court rooms in cuffs through public areas of buildings because there were unsuitable facilities for them.
:: Insights made by independent observers were often overlooked.
Pockets of good practice and efforts by most court custody staff to treat detainees decently showed "it is not inevitable that poor conditions and degrading, unsafe practices will prevail", the report found.
Inspectors have "modest" expectations of court custody, it added, but do require clear leadership, respect for detainees and for people to be safe and treated decently in custody.
Recommendations made in the report included ensuring clearer leadership and responsibility, better co-ordination of roles, bringing in measures such as more virtual courts to reduce time spent in custody and the establishment of a complaints procedure.
Risk assessments should also be reviewed, along with cleaning and maintenance needs.
HM Courts and Tribunals Service said it was working with partner agencies Serco and GEOAmey, Prisoner Escort and Custody Services and the National Offender Management Service to improve its management of court custody areas.
A spokeswoman said: "A programme of deep cleaning is already in place for all court cell areas in England and Wales.
"As part of our drive to make the justice system more efficient, we are also working to avoid prisoners being brought to court unnecessarily with better use of video-link technology.
"Serco and GEOAmey continue to review their operating processes against the HMIP recommendations to improve and deliver a safe and secure custodial environment for detainees."