.page-actions {
	position: relative;
	display: flex;
	flex-wrap: wrap;
	justify-content: flex-end;
	margin-left: -0.75rem; // Align to the margin
	font-size: 0.875rem;
	font-weight: var( --font-weight-medium );
	gap: var( --space-xxs );

	// TODO: Merge this with header__item
	&__item {
		display: flex;
	}

	// TODO: Merge this with header__button
	&__button {
		display: flex; // Fallback
		display: grid;
		justify-content: center;
		width: 2.75rem; // Same width as other buttons
		height: 2.25rem; // Same height as other buttons
		border-radius: var( --border-radius--medium );
		place-items: center;

		&:hover {
			background-color: var( --background-color-quiet--hover );

			.citizen-ui-icon::before {
				opacity: var( --opacity-icon-base--hover );
			}
		}

		&:active {
			background-color: var( --background-color-quiet--active );

			.citizen-ui-icon::before {
				opacity: var( --opacity-icon-base--active );
			}
		}
	}

	&__card {
		position: absolute;
		z-index: @z-index-page-header; // So the popups inside will be higher than content
		top: 100%;
		right: ~'calc( var( --space-xs ) * -1 )'; // counteract margin
		overflow: auto;
		max-height: 60vh;
		padding: 0.5rem 0;
		margin: var( --space-xs );
		font-size: 0.875rem;
		.citizen-card;
		.citizen-card-hide( top right );

		a {
			padding: 0.625rem var( --space-md );
			.menu-item-link;
			gap: unset;

			.citizen-ui-icon {
				margin-right: var( --space-sm );
			}

			&:hover {
				.menu-item-link-hover;
			}

			&:active {
				.menu-item-link-active;
			}
		}
	}

	// Visible page tools styles
	> .mw-portlet {
		ul {
			display: flex;
			flex-wrap: wrap;
			gap: var( --space-xxs );
		}

		li > a {
			display: flex;
			align-items: center;
			padding: var( --space-xs ) var( --space-sm );
			border-radius: var( --border-radius--medium );
			color: var( --color-base );
			font-size: 0;
			transition: @transition-background, @transition-color;

			&:hover {
				background-color: var( --background-color-quiet--hover );
				color: var( --color-base--emphasized );

				&::before {
					opacity: var( --opacity-icon-base--hover );
				}
			}

			&:active {
				background-color: var( --background-color-quiet--active );
				color: var( --color-base--subtle );

				&::before {
					opacity: var( --opacity-icon-base--active );
				}
			}
		}
	}

	ul {
		margin: 0;
		list-style: none;
	}

	li {
		margin: 0;
	}

	// TODO: Merge this with header styles
	.mw-checkbox-hack-checkbox:checked ~ .page-actions__button {
		background-color: var( --background-color-primary--active );
	}
}

// Language counter badge
// TODO: Maybe this should be in a mixin
#citizen-languages__buttonCheckbox {
	position: relative; // So that the badge doesn't take a stroll to the far left

	&::after {
		position: absolute;
		top: 0;
		right: 0;
		display: block;
		padding: 0.1em 0.4em;
		border-radius: var( --border-radius--pill );
		background: var( --color-primary );
		color: #fff;
		content: attr( data-counter-text );
		font-size: 0.65rem;
	}

	&[ data-counter-text='0' ]::after {
		display: none;
	}
}

.citizen-languages {
	// Special handling for ULS
	// ULS won't be triggered by the label button
	// so we need to overlay the checkbox on top of it
	.mw-interlanguage-selector {
		position: absolute;
		z-index: 1;
		display: block;
		width: var( --size-button--page );
		height: var( --size-button--page );
		padding: 0;
		margin: 0;
		cursor: pointer;
		opacity: 0;

		// Hover state needs to be define because the checkbox now overlays the label
		&:hover {
			~ .page-actions__button {
				background-color: var( --background-color-quiet--hover );

				.citizen-ui-icon::before {
					opacity: var( --opacity-icon-base--hover );
				}
			}
		}

		&:active {
			~ .page-actions__button {
				background-color: var( --background-color-quiet--active );

				.citizen-ui-icon::before {
					opacity: var( --opacity-icon-base--active );
				}
			}
		}
	}
}

.page-actions-more,
.citizen-languages {
	position: relative;
}

// Edit button
#ca-edit,
#ca-ve-edit {
	> a {
		background-color: var( --color-primary );
		color: #fff;

		&:hover {
			background-color: var( --color-primary--hover );
		}

		&:active {
			background-color: var( --color-primary--active );
		}
	}

	> a,
	.citizen-ui-icon {
		&::before {
			filter: invert( 1 );
			// white icon
			opacity: 1;
		}
	}
}

// VE styles shouldn't be core but it is just a few lines :/
#ca-ve-edit {
	order: 98; // Before source edit

	> a {
		// See SkinHooks.php for why VE is here
		&::before {
			width: var( --size-icon );
			height: var( --size-icon );
			background-position: center;
			background-repeat: no-repeat;
			background-size: var( --size-icon );
			content: '';
			transition: @transition-opacity;
		}
	}
}

#ca-edit {
	order: 99; // Align to last
}

// Merge two buttons together
.client-js .citizen-ve-edit-merged {
	&#ca-ve-edit {
		> a {
			border-bottom-right-radius: 0;
			border-top-right-radius: 0;
		}
	}

	&#ca-edit {
		border-left: 1px solid var( --color-primary--hover );
		margin-left: ~'calc( var( --space-xxs ) * -1 )';

		> a {
			border-bottom-left-radius: 0;
			border-top-left-radius: 0;
			font-size: 0; // hide edit source text
			gap: 0;
		}
	}
}

// Additional buttons
#Wikiplus-Edit-TopBtn,
#ca-redirect,
#ca-masseditregex {
	> a::before {
		width: var( --size-icon );
		height: var( --size-icon );
		background-position: center;
		background-repeat: no-repeat;
		background-size: var( --size-icon );
		content: '';
		transition: @transition-opacity;
	}
}

// Checkbox hack
#page-actions-more,
#citizen-languages {
	&__checkbox:checked {
		~ .page-actions__card {
			min-width: 10rem; // Add min-width
			.citizen-card-show();
		}
	}
}

.citizen-animations-ready {
	.page-actions__card {
		.citizen-card-transition();
	}
}

// To avoid more menu from overflow in narrow screen
@media ( max-width: @width-breakpoint-tablet ) {
	.page-actions {
		&__item {
			position: unset;
		}
	}
}

@media ( min-width: @width-breakpoint-desktop ) {
	.page-actions {
		> .mw-portlet {
			li > a {
				font-size: 0.75rem;
				gap: var( --space-xs );
			}
		}

		&__card {
			// Expand the width on narrow screens might overflow the page
			white-space: nowrap;
		}
	}
}

// Reset hover styles if it is a touch device
// This is dumb but hover:hover overrides active states
@media ( hover: none ) {
	.page-actions {
		&__button {
			&:hover {
				background-color: none;

				.citizen-ui-icon::before {
					opacity: var( --opacity-icon-base );
				}
			}
		}
	}
}
