@use "../tokens" as *;

// Settings-app primitives (docs/redevelopment/appendix/B-settings-app-spec.md §0.2). Values copied from
// the prototype's inline styles.

// Card, group heading, rows ------------------------------------------------------------------------

.ssw-card {
	background: $surface;
	border: 1px solid $line-card;
	border-radius: $radius-card;
	padding: 18px 20px;
}

.ssw-group-heading {
	font-size: 12px;
	font-weight: 600;
	letter-spacing: 0.06em;
	text-transform: uppercase;
	color: $muted-2;
	padding: 22px 0 6px;
	margin-top: 10px;

	&.is-first {
		padding: 2px 0 6px;
		margin-top: 0;
	}
}

.ssw-row {
	display: flex;
	gap: 10px 16px;
	align-items: center;
	padding: 13px 0;
	flex-wrap: wrap;

	// First row after a group heading draws the hairline under the heading.
	&.is-first {
		padding: 16px 0 12px;
		margin-top: 4px;
		box-shadow: inset 0 1px 0 $line-row;
	}

	&.is-top {
		align-items: flex-start;
	}

	&.is-stacked {
		flex-direction: column;
		align-items: stretch;
		gap: 10px;
		flex-wrap: nowrap;
	}
}

.ssw-row__label {
	flex: 1;
	min-width: 120px;
	// Inline flow, not flex: when the label wraps (narrow cards), the info icon follows its last
	// word instead of standing apart at the far edge.
	display: block;
	font-size: 14px;
	font-weight: 500;
	color: $text;

	// Centred on its line of text, as the flex layout had it (16px icon, 18.2px admin line).
	> .ssw-info {
		margin: 1px 0 0 6px;
		vertical-align: top;
	}

	.ssw-row.is-stacked > & {
		flex: none;
		min-width: 0;
	}
}

.ssw-sublabel {
	font-size: 12px;
	color: $muted-2;
	margin-bottom: 5px;
}

// Info "(i)" tooltip ----------------------------------------------------------------------------

.ssw-info {
	position: relative;
	display: inline-flex;
	width: 16px;
	height: 16px;
	color: $faint-3;
	cursor: help;
	outline: none;
	transition: color 140ms ease;

	&:hover,
	&:focus-visible {
		color: $text;
	}
}

.ssw-info__body {
	position: absolute;
	left: 50%;
	bottom: calc(100% + 10px);
	z-index: 40;
	width: 260px;
	max-width: calc(100vw - 24px);
	padding: 10px 12px;
	border-radius: $radius-menu;
	background: $ink;
	color: #f0f0f1;
	font-size: 12px;
	font-weight: 400;
	line-height: 1.55;
	text-align: left;
	text-transform: none;
	letter-spacing: 0;
	box-shadow: $shadow-tip;
	pointer-events: none;
	opacity: 0;
	transform: translate(-50%, 4px);
	transition: opacity 150ms ease, transform 180ms $ease;

	.ssw-info:hover &,
	.ssw-info:focus & {
		opacity: 1;
		transform: translate(-50%, 0);
	}

	&.is-rich {
		width: 280px;
		padding: 12px 14px;
		display: flex;
		flex-direction: column;
		gap: 6px;
	}

	p {
		position: relative;
		margin: 0;
	}

	strong {
		font-weight: 600;
		color: #fff;
	}
}

.ssw-info__arrow {
	position: absolute;
	left: 50%;
	top: 100%;
	width: 10px;
	height: 10px;
	margin: -6px 0 0 -5px;
	background: $ink;
	transform: rotate(45deg);
	border-radius: 2px;
}

.ssw-info__text {
	position: relative;
}

// Toggle switch ---------------------------------------------------------------------------------

.ssw-toggle {
	width: 36px;
	height: 20px;
	border-radius: 999px;
	position: relative;
	flex: 0 0 36px;
	cursor: pointer;
	background: $off;
	transition: background 140ms $ease-std;

	@include focus-visible;

	&.is-on {
		background: $blue;
	}
}

.ssw-toggle.is-disabled {
	cursor: not-allowed;
	opacity: 0.5;
}

.ssw-toggle__knob {
	position: absolute;
	left: 3px;
	top: 4px;
	width: 12px;
	height: 12px;
	border-radius: 50%;
	background: #fff;
	box-shadow: 0 0 0 1px rgb(255, 255, 255), 0 2px 4px rgba(31, 41, 55, 0.12);
	transform: translateX(0);
	transition: transform 240ms $ease-spring;

	.is-on > & {
		transform: translateX(18px);
	}
}

// Checkbox --------------------------------------------------------------------------------------

.ssw-checkbox-list {
	display: flex;
	flex-direction: column;
	align-items: flex-start;
	gap: 10px;
}

.ssw-checkbox {
	display: flex;
	align-items: center;
	gap: 8px;
	cursor: pointer;
	font-size: 14px;
	font-weight: 400;
	color: $text;
	outline: none;
	white-space: nowrap;

	@include focus-visible;
}

.ssw-checkbox__box {
	flex: 0 0 16px;
	width: 16px;
	height: 16px;
	border-radius: 3px;
	background: #fff;
	box-shadow: inset 0 0 0 1px $faint;
	display: flex;
	align-items: center;
	justify-content: center;
	transition: background 140ms ease, box-shadow 140ms ease;

	svg {
		opacity: 0;
		transition: opacity 120ms ease;
	}

	.is-on > & {
		background: $blue;
		box-shadow: inset 0 0 0 1px $blue;

		svg {
			opacity: 1;
		}
	}
}

// Segmented control -----------------------------------------------------------------------------

.ssw-seg {
	display: inline-flex;
	gap: 2px;
	padding: 3px;
	border-radius: $radius-menu;
	box-shadow: inset 0 0 0 1px $line-card;
	background: $surface-2;

	&.is-fill {
		display: flex;
	}
}

.ssw-seg__item {
	border: 0;
	font-size: 14px;
	height: 32px;
	padding: 0 14px;
	white-space: nowrap;
	flex: 0 0 auto;
	cursor: pointer;
	border-radius: $radius-control;
	background: transparent;
	color: $text;
	font-weight: 400;
	box-shadow: none;
	transition: background 140ms $ease-std, box-shadow 140ms ease, color 140ms ease;

	@include focus-visible;

	.ssw-admin & {
		padding: 0 14px;
	}

	.ssw-seg.is-tight & {
		padding: 0 12px;
	}

	.ssw-seg.is-fill & {
		flex: 1 1 auto;
	}

	&.is-icon {
		width: 40px;
		padding: 0;
		display: inline-flex;
		align-items: center;
		justify-content: center;
	}

	&.is-on {
		background: #fff;
		color: $blue;
		box-shadow: $shadow-seg-on;
	}
}

// Settings dropdown ("sDrop") -----------------------------------------------------------------

.ssw-select {
	position: relative;
}

.ssw-select__trigger {
	display: flex;
	align-items: center;
	height: 40px;
	border-radius: $radius-control;
	box-shadow: inset 0 0 0 1px $line-ring;
	background: #fff;
	font-size: 14px;
	font-weight: 400;
	color: $text;
	padding: 0 10px 0 12px;
	cursor: pointer;
	outline: none;
	transition: box-shadow 140ms ease;

	&:hover {
		box-shadow: inset 0 0 0 1px $faint;
	}

	&:focus {
		box-shadow: inset 0 0 0 1px $text;
	}

	.ssw-select.is-compact > & {
		padding: 0 4px 0 10px;
	}

	.ssw-select.is-unit > & {
		padding: 0 6px 0 12px;
	}
}

.ssw-select__label {
	flex: 1;
	min-width: 0;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}

.ssw-select__caret {
	flex: 0 0 18px;
	transform: rotate(0deg);
	transition: transform 180ms ease;

	.is-open > .ssw-select__trigger > & {
		transform: rotate(180deg);
	}
}

.ssw-select__menu {
	position: absolute;
	top: calc(100% + 4px);
	left: 0;
	right: 0;
	z-index: 30;
	background: #fff;
	border-radius: $radius-menu;
	box-shadow: $shadow-menu;
	padding: 4px;
	// Closed menus are out of the layout: kept in it (only transparent), a menu near the end of the
	// page made the page longer than the admin wrapper, and the WordPress footer then floated above
	// a blank strip instead of sitting at the bottom. `allow-discrete` keeps the close animation and
	// @starting-style the open one; older browsers just show and hide the menu.
	display: none;
	flex-direction: column;
	gap: 3px;
	transform-origin: top;
	transition: opacity 160ms ease, transform 160ms $ease, display 160ms allow-discrete;
	opacity: 0;
	transform: translateY(-4px) scaleY(0.97);
	pointer-events: none;

	.is-open > & {
		display: flex;
		opacity: 1;
		transform: translateY(0) scaleY(1);
		pointer-events: auto;

		@starting-style {
			opacity: 0;
			transform: translateY(-4px) scaleY(0.97);
		}
	}

	&.is-right {
		left: auto;
		right: 0;
		transform-origin: top right;
	}

	&.is-content {
		width: max-content;
		min-width: 100%;
	}

	// Long lists (Image Size): cap the height and scroll the rest, without a visible scrollbar.
	&.is-scroll {
		max-height: 400px;
		overflow-y: auto;
		overscroll-behavior: contain;
		scrollbar-width: none; // Firefox
		-ms-overflow-style: none; // Legacy Edge

		&::-webkit-scrollbar {
			display: none; // Chrome, Safari
		}

		.ssw-select__option {
			flex: 0 0 auto;
		}
	}
}

.ssw-select__option {
	padding: 7px 10px;
	border-radius: $radius-control;
	font-size: 14px;
	cursor: pointer;
	background: transparent;
	color: $text;
	font-weight: 400;
	transition: background 120ms ease;

	&:hover,
	&.is-active {
		background: $tint;
	}

	&.is-selected {
		background: $tint;
		color: $blue;
	}

	&.is-center {
		text-align: center;
	}

	&.has-preview {
		display: flex;
		align-items: center;
		gap: 10px;
	}
}

.ssw-select__line {
	flex: 0 0 22px;
	height: 0;
	border-top: 2px solid currentcolor;

	&.is-none {
		opacity: 0;
	}
}

.ssw-select__pill {
	margin-left: auto;
	font-size: 11px;
	font-weight: 500;
	line-height: 18px;
	padding: 0 7px;
	border-radius: 999px;
	background: $success-tint;
	color: $success-ink;
}

// Number spinner -----------------------------------------------------------------------------

.ssw-spin {
	position: relative;
	display: inline-flex;
	align-items: stretch;
	border-radius: $radius-control;
	box-shadow: inset 0 0 0 1px $line-ring;
	background: #fff;
	height: 40px;
	overflow: hidden;
	min-width: 104px;

	// The prototype draws the ring a second time above the content.
	&::after {
		content: "";
		position: absolute;
		inset: 0;
		border-radius: inherit;
		box-shadow: inset 0 0 0 1px $line-ring;
		pointer-events: none;
		z-index: 1;
	}

	// Keyboard focus (the prototype shows none): same ring as a focused dropdown.
	&:focus-within::after {
		box-shadow: inset 0 0 0 1px $text;
	}

	&.is-block {
		display: flex;
		width: 100%;
		min-width: 0;
	}

	&.is-size {
		width: 100px;
		min-width: 0;
	}

	&.is-joined {
		border-radius: $radius-control 0 0 $radius-control;
		background: transparent;
		box-shadow: none;
		min-width: 96px;

		&::after {
			box-shadow: none;
		}
	}
}

.ssw-spin__value {
	flex: 1;
	min-width: 0;
	display: flex;
	align-items: center;
	padding: 0 10px 0 12px;
	font-size: 14px;
	font-weight: 400;
	font-variant-numeric: tabular-nums;
	color: $text;

	.ssw-spin.is-compact & {
		padding: 0 4px 0 10px;
	}

	.ssw-spin.is-all &,
	.ssw-spin.is-side &,
	.ssw-spin.is-size & {
		padding: 0 8px 0 12px;
	}

	input {
		width: 100%;
		min-width: 0;
		border: 0;
		outline: none;
		background: transparent;
		padding: 0;
		height: auto;
		font-variant-numeric: tabular-nums;
		color: inherit;
		box-shadow: none;
		-moz-appearance: textfield;

		// Beat wp-admin's `input[type=number]:focus` ring; the spinner's own ring shows focus.
		&[type="number"]:focus {
			border: 0;
			outline: none;
			box-shadow: none;
		}

		&::-webkit-outer-spin-button,
		&::-webkit-inner-spin-button {
			-webkit-appearance: none;
			margin: 0;
		}
	}
}

.ssw-spin__buttons {
	display: flex;
	flex-direction: column;
	width: 16px;
	flex: 0 0 16px;
	opacity: 0;
	transform: translateX(3px);
	transition: opacity 160ms ease, transform 160ms $ease;

	.ssw-spin:hover &,
	.ssw-spin:focus-within & {
		opacity: 1;
		transform: none;
	}

	.ssw-spin.is-compact & {
		width: 14px;
		flex-basis: 14px;
	}

	.ssw-spin.is-all &,
	.ssw-spin.is-side & {
		width: 15px;
		flex-basis: 15px;
	}

	button {
		flex: 1;
		border: 0;
		background: $line-list;
		color: $text-2;
		cursor: pointer;
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 0;
	}
}

.ssw-spin__divider {
	height: 1px;
	background: #fff;
}

.ssw-spin__unit {
	display: flex;
	align-items: center;
	justify-content: center;
	min-width: 38px;
	padding: 0 10px;
	font-size: 12px;
	color: $muted;
	background: $surface-2;
	box-shadow: inset 1px 0 0 $line-card;

	.ssw-spin.is-compact & {
		min-width: 34px;
		padding: 0 8px;
	}
}

// Spinner joined to a unit dropdown (Swatch Area Max Width).
.ssw-joined {
	position: relative;
	display: inline-flex;
	align-items: stretch;
	height: 40px;
	border-radius: $radius-control;
	background: #fff;

	// The ring is drawn above both parts, as .ssw-spin draws its own: the unit cell would paint over
	// the wrapper's edge.
	&::after {
		content: "";
		position: absolute;
		inset: 0;
		border-radius: inherit;
		box-shadow: inset 0 0 0 1px $line-ring;
		pointer-events: none;
		z-index: 1;
		transition: box-shadow 140ms ease;
	}

	&:hover::after {
		box-shadow: inset 0 0 0 1px $faint;
	}

	// Focus on the value or the unit: the ring of a focused spinner (Swatch Spacing) or dropdown.
	&:focus-within::after {
		box-shadow: inset 0 0 0 1px $text;
	}

	.ssw-select {
		width: 58px;
	}

	.ssw-select__trigger {
		border-radius: 0 $radius-control $radius-control 0;
		background: $surface-2;
		box-shadow: inset 1px 0 0 $line-card, inset 0 1px 0 $line-ring, inset -1px 0 0 $line-ring, inset 0 -1px 0 $line-ring;
		font-size: 13px;
		padding: 0 4px 0 10px;
		transition: background 140ms ease;

		&:hover,
		&:focus {
			background: #eef0f2;
			box-shadow: inset 1px 0 0 $line-card, inset 0 1px 0 $line-ring, inset -1px 0 0 $line-ring, inset 0 -1px 0 $line-ring;
		}
	}

	.ssw-select__menu {
		left: auto;
		width: 64px;
		transform-origin: top right;
	}
}

// Linked four-side dimension -------------------------------------------------------------------

.ssw-linked {
	display: flex;
	gap: 8px;
	align-items: center;
	margin-left: auto;
	flex-wrap: wrap;
	justify-content: flex-end;

	.ssw-spin.is-all {
		width: 96px;
		min-width: 0;
	}

	.ssw-select.is-unit {
		width: 64px;
	}
}

.ssw-link-toggle {
	width: 40px;
	height: 40px;
	flex: 0 0 40px;
	border: 0;
	border-radius: $radius-control;
	background: #fff;
	color: $faint;
	box-shadow: inset 0 0 0 1px $line-ring;
	display: flex;
	align-items: center;
	justify-content: center;
	cursor: pointer;
	padding: 0;
	transition: background 140ms ease, box-shadow 140ms ease, color 140ms ease;

	@include focus-visible;

	&:hover {
		box-shadow: inset 0 0 0 1px $faint;
	}

	&.is-linked {
		background: $tint;
		color: $blue;
		box-shadow: inset 0 0 0 1px $blue-ring;
	}
}

.ssw-sides {
	display: grid;
	grid-template-rows: 0fr;
	margin-top: 0;
	opacity: 0;
	visibility: hidden;
	transition: grid-template-rows 240ms $ease, margin-top 240ms $ease, opacity 180ms ease, visibility 0s linear 240ms;

	&.is-open {
		grid-template-rows: 1fr;
		margin-top: 12px;
		opacity: 1;
		visibility: visible;
		transition-delay: 0s;
	}

	> div {
		min-height: 0;
		overflow: hidden;
	}
}

.ssw-sides__grid {
	display: grid;
	grid-template-columns: repeat(4, minmax(0, 1fr));
	gap: 8px;
	padding: 1px;
}

// Conditional reveal --------------------------------------------------------------------------

.ssw-reveal {
	display: grid;
	grid-template-rows: 0fr;
	opacity: 0;
	transition: grid-template-rows 260ms $ease, opacity 200ms ease;

	> .ssw-reveal__inner {
		min-height: 0;
		overflow: hidden;

		> .ssw-reveal__content {
			transform: translateY(-6px);
			transition: transform 260ms $ease;
		}
	}

	&.is-open {
		grid-template-rows: 1fr;
		opacity: 1;

		> .ssw-reveal__inner > .ssw-reveal__content {
			transform: none;
		}
	}

	// After opening, overflow becomes visible so dropdowns and pickers can escape.
	&.is-settled > .ssw-reveal__inner {
		overflow: visible;
	}
}

// Text input (settings style) ------------------------------------------------------------------

.ssw-text {
	width: 104px;
	height: 40px;
	padding: 0 12px;
	border: 0;
	border-radius: $radius-control;
	box-shadow: inset 0 0 0 1px $line-ring;
	background: #fff;
	font-size: 14px;
	font-weight: 400;
	color: $text;
	outline: none;
	transition: box-shadow 140ms ease;

	.ssw-admin & {
		border: 0;
		box-shadow: inset 0 0 0 1px $line-ring;
		padding: 0 12px;
		height: 40px;
		line-height: normal;
	}

	.ssw-admin &:hover {
		box-shadow: inset 0 0 0 1px $faint;
	}

	.ssw-admin &:focus {
		box-shadow: inset 0 0 0 1px $text;
		outline: none;
	}
}

// Pro settings in the free edition (Core\Edition; design `data-pro`) ---------------------------------
// The row reads as unavailable: muted label with the Pro badge, the control at half strength and
// inert. A press anywhere on the row opens the upgrade dialog.

.ssw-row.is-locked,
.ssw-settings-row.is-locked {
	cursor: pointer;

	.ssw-row__label,
	.ssw-settings-row__label {
		display: flex;
		align-items: center;
		gap: 8px;
		color: #8c8f94;
	}
}

.ssw-row__locked {
	display: contents;

	> * {
		opacity: 0.5;
		pointer-events: none;
	}
}

// Pro options inside a dropdown: muted, with the Pro badge at the end.
.ssw-select__option.is-locked,
.ssw-ssopt.is-locked {
	display: flex;
	align-items: center;
	gap: 10px;
	color: #a7aaad;
	cursor: default;

	&:hover,
	&.is-active {
		background: transparent;
		color: #a7aaad;
	}

	// The badge: the option's only element in Select, after the label's span in WpSelect.
	&.ssw-select__option > :last-child,
	&.ssw-ssopt > :last-child:not(:first-child) {
		margin-left: auto;
	}
}
