An Islamic faith school's policy of segregating boys and girls is unlawful sex discrimination, a court has ruled.
The case was heard at the Court of Appeal as Ofsted challenged a High Court ruling clearing the Al-Hijrah school in Birmingham of discrimination.
Ofsted's lawyers argued the segregation left girls "unprepared for life in modern Britain".
Appeal judges ruled the school was discriminating against its pupils contrary to the Equality Act.
However, the court did not accept the argument the school's policy had disadvantaged girls more than boys.
The appeal judges also made it clear the government and Ofsted had failed to identify the problem earlier and other schools operating similarly should be given time "to put their houses in order".
About 20 schools - Islamic, Jewish and Christian - are thought to have similar segregation policies.
The three appeal judges heard boys and girls, aged four to 16, attend the Birmingham City Council-maintained Al-Hijrah school, in Bordesley Green.
But from Year Five, boys and girls are completely separated for lessons, breaks, school trips and school clubs.
In 2016, Ofsted ruled the school was inadequate and it was put in special measures, saying its policy of separating the sexes was discrimination under the 2010 Equality Act.
In November, High Court judge Mr Justice Jay overruled the inspectors, saying that they had taken an "erroneous" view on an issue "of considerable public importance".
Speaking after the Court of Appeal ruling Amanda Spielman, Her Majesty's Chief Inspector of Schools, said educational institutions should never treat pupils less favourably because of their sex.
"The school is teaching boys and girls entirely separately, making them walk down separate corridors, and keeping them apart at all times," she said.
"This is discrimination and is wrong. It places these boys and girls at a disadvantage for life beyond the classroom and the workplace, and fails to prepare them for life in modern Britain," she said.
In the ruling, the appeal judges said Ofsted had made it clear if the appeal succeeded, "it will apply a consistent approach to all similarly organised schools".
Given their failure to identify the problem earlier, the education secretary and Ofsted had "de facto sanctioned and accepted a state of affairs which is unlawful" and should give the affected schools time to "put their houses in order", the judges said.
The ruling means state schools which segregate pupils risk being given a lower rating by Ofsted. It only applies to mixed-sex schools.
During the appeal hearing, Peter Oldham QC, speaking for Al-Hijrah's interim executive board, said the boys and girls at the school were treated entirely equally while segregated.
He said Ofsted did not claim separation was discrimination until 2016 and its actions were "the antithesis of proper public decision-making".
Birmingham City Council said it took the High Court action it had because it felt Al-Hijrah school had been held to a different standard than other schools with similar arrangements, which had not been downgraded by Ofsted as a consequence.
Colin Diamond, corporate director of children and young people at the Labour-run council, said the case had always been about fairness and consistency in the inspection process.
"We would therefore highlight comments made in this judgement about the secretary of state's and Ofsted's 'failure to identify the problem'," he said.
He added the council had a strong history of encouraging all schools to practise equality but if it was national policy that schools with gender separation were discriminating against pupils then local authorities and the schools needed to be told so they knew the standards they were being inspected against.
Speaking to Radio 4, Mr Diamond said: "In questioning the judgement itself, the logic whereby you can say having, in one part of our city here, a boys' school and a girls' school adjacent to each other, with a fence between them... so that's okay is it?
"Whereas it's not okay to have boys and girls in the same school, when parents have signed up for that form of Islamic education. We don't see the logic, the equity in any of that."
Matt Bennett, shadow cabinet member for children and family services, said the verdict did not reflect well on Al-Hijrah, the council, Ofsted or the DfE.
"It is now clear that practices breaching the Equality Act 2010 have been allowed to continue at this school, and others across the country. Action is now required at local and national level," he said.