Legally, it is worse than you might think.
For many years, there were private, cash or insurance paid “mental clinics”, for mostly insubordinate children, or the children of rich people who were “in the way”, for whatever reason.
Some parents who had enough of their children’s antics, would get a rubber stamp diagnosis that their children were mentally ill, and put them in these places until they had turned 18, or the money ran out. In either case, they would then be declared “cured” and released.
In some cases, children would be set to inherit a lot of money from a deceased parent, and their step parent wanted the money, so they would institutionalize them, and spend all the money in the inheritance, as a trustee, while they were incarcerated.
Children in such lockups have no recourse to habeus corpus, nor any other legal remedy.
For people with little money or insurance, they found out they could kick the child out of their house, then call the police to report them as runaways. After doing this a few times, the children would be called incorrigible and just kept in juvenile detention.
(I helped a girl get out of this insanity by taking her to a church that cared for her until they could arrange legal adoption. Her birth parents were hideous people.)
In many states, if children or their parents can no longer stand each other, if the child can get alternative support, one or the other can ask a court to “emancipate” the child from their parents. (Think emancipation proclamation to free slaves from their status as property. Same idea.)
One of the sisters of John F. Kennedy was a victim of this abuse.