China's President Xi Jinping warned on Saturday that any challenge to Beijing's control over Hong Kong crossed a "red line", as thousands calling for more democracy marched through the city 20 years since it was handed back by Britain.
Xi spoke in a televised address after swearing in new Hong Kong leader Carrie Lam, who is already being cast by critics as a China stooge in a city where many are angry at Beijing's tightening grip on the freedoms of its nearly eight million people.
A huge security operation shut down large parts of Hong Kong for Xi's three-day visit, reflecting Beijing's concern that there should be no embarrassment ahead of a key Communist Party congress later this year which is expected to cement his position as the most powerful Chinese leader in a generation.
The trip was his first since becoming president in 2013 and came three years after mass pro-democracy rallies crippled parts of the city for months.
Xi said Saturday that any threat to China's sovereignty and security or to the power of the central government "crosses the red line and is absolutely impermissible".
He also warned against anyone endangering Hong Kong's constitution or using the city "to carry out infiltration and sabotage activities against the mainland".
The message comes as young activists have emerged calling for self-determination or even full independence for Hong Kong, which has infuriated Beijing.
Hours after Xi flew out of Hong Kong, thousands rallied in Victoria Park for an annual pro-democracy.
Xi insisted that Hong Kong had "more extensive democratic rights and freedoms than at any other time in its history" and pledged to uphold its semi-autonomous status.
But Beijing's foreign ministry declared Friday that the document signed by Britain and China which initiated the handover "is no longer relevant".
The Sino-British Joint Declaration gave Hong Kong rights unseen on the mainland through a "one country, two systems" agreement, lasting 50 years.
There are growing fears that freedoms guaranteed by the handover deal are now under threat, with Chinese authorities accused of abductions and interfering in a range of areas, from politics to media and education.
Xi cautioned that political conflict would "severely hinder" Hong Kong's economic and social development and called upon Hong Kong to enhance education to promote China's culture and history.
A proposal to include patriotic education in Hong Kong schools met with huge protests in 2012 and has since been shelved.
Pro-China protesters targeted a small peaceful march by activists in memory of the victims of Beijing's 1989 Tiananmen Square crackdown Saturday morning as officials gathered for the swearing in.
As the campaigners prepared to carry a makeshift coffin towards the convention centre, as they do each year, a man ran across the street and kicked it.
Flag-waving pro-China protesters then blocked the march as police struggled to separate the two sides.
Democracy campaigners were taken away in police vans with two of them saying they had been attacked by officers inside, before all were released.
Leading activist Joshua Wong accused "pro-China gangsters and mobs" for instigating the attack.
Pro-democracy groups say they have been repeatedly targeted since Xi flew in on Thursday.
"What we've experienced this weekend was a whole new level of intimidation and direct violence that's unprecedented," said campaigner Avery Ng of League of Social Democrats.
Those protests were sparked by a Beijing-backed political reform package which said Hong Kong could have a public vote for leader, but that candidates must be vetted first.
Xi left Hong Kong Saturday with a large-scale pro-democracy protest due to take place in the afternoon.