@import 'mediawiki.skin.variables.less';
@import './codemirror.mixins.less';

.cm-editor {
	border: @border-width-base @border-style-base @border-color-subtle;
	overflow: hidden;
	resize: vertical;

	&.cm-focused {
		outline: 0;
	}

	&.cm-focused.cm-mw-mode-mediawiki > .cm-scroller > .cm-selectionLayer .cm-selectionBackground {
		background: #d7d4f0;
		.darkmode( background, #233 );
	}
}

.cm-scroller {
	overflow: auto;
}

.cm-matchingBracket,
.cm-focused .cm-matchingBracket {
	background-color: #eee;
	outline: 1px solid #999;

	/* Dark mode: cool / valid */
	.darkmode( background-color, rgba( 140, 170, 255, 0.35 ) );
	.darkmode( outline, 1px solid rgba( 140, 170, 255, 0.9 ) );
}

.cm-nonmatchingBracket,
.cm-focused .cm-nonmatchingBracket {
	background-color: #fdd;
	outline: 1px solid #a22;

	/* Dark mode: warm / error */
	.darkmode( background-color, rgba( 255, 110, 110, 0.45 ) );
	.darkmode( outline, 1px solid rgba( 255, 110, 110, 1 ) );
}

.cm-editor .cm-specialChar {
	color: @color-destructive--hover;
}

.cm-special-char-nbsp {
	color: @color-placeholder;
}

.cm-mw-slow-feature {
	color: @color-subtle;
}

.cm-tooltip-fold {
	cursor: @cursor-base--hover;
	line-height: 1.2;
	padding: 0 1px;
	opacity: 0.6;

	&:hover {
		opacity: 1;
	}
}

.cm-gutter-lint {
	order: -1;
}

.cm-bidi-isolate {
	/* @noflip */
	direction: ltr;
	unicode-bidi: isolate;
}

// T415461: Style for JavaScript/Lua globals
// reuses CodeMirror style for `tags.standard(tags.variableName)` */
.cm-globals,
.cm-globals > * {
	color: #164;
	.darkmode( color, #d19a66 );
}

.ext-codemirror-wrapper {
	height: 100%;

	textarea {
		/* stylelint-disable-next-line declaration-no-important */
		display: none !important;
	}

	&--hidden {
		.cm-editor {
			/* stylelint-disable-next-line declaration-no-important */
			display: none !important;
		}

		textarea {
			/* stylelint-disable-next-line declaration-no-important */
			display: initial !important;
		}
	}
}

// These styles are prefixed to have higher specificity than CodeMirror's
// default styles. We only do this for the extension-supplied themes powered
// by Less, so other JS-provided themes can take precedence.
.cm-mw-theme-default,
.cm-mw-theme-colorblind,
.cm-mw-theme-no-highlight {
	.cm-foldPlaceholder {
		background-color: @background-color-interactive;
		border-color: @border-color-subtle;
		color: @color-base;
	}

	.cm-selectionBackground {
		background: #d9d9d9;
		.darkmode( background, #3e4450 );
	}

	// For the highlightSelectionMatches extension, only used in non-wikitext.
	.cm-selectionMatch {
		background: #f6ebeb;
		.darkmode( background, rgba(170, 254, 102, 0.1) );
	}

	.cm-cursor {
		border-left: 2px solid @color-emphasized;
	}

	.cm-activeLine {
		background-color: rgba( 204, 238, 255, 0.27 );
		.darkmode( background-color, rgba( 71, 71, 124, 0.2 ) );
	}

	.cm-tooltip {
		background-color: @background-color-neutral-subtle;
		border-color: @border-color-base;
	}
}

// Gutters get the same styling across all themes.
.cm-editor .cm-gutters {
	background-color: @background-color-interactive-subtle;
	border-right-color: @border-color-subtle;
	color: @color-subtle;
}

// Panels

.cm-editor .cm-panels {
	background-color: @background-color-neutral-subtle;
	border-bottom: 0;
	color: @color-base;
	direction: ltr;
	z-index: @z-index-above-content;

	.cdx-button-group {
		.cdx-button,
		.cdx-toggle-button {
			min-width: @min-width-toggle-switch;
		}
	}

	.cm-mw-panel {
		border-bottom: @border-style-base @border-width-base @border-color-subtle;
		font-family: @font-family-base;
		padding: @spacing-50;
		position: relative;
	}

	.cm-mw-panel__fieldset legend {
		margin-bottom: @spacing-50;
	}

	.cm-mw-panel__text-input {
		flex-basis: 0;
		flex-grow: 1;
	}

	.cm-mw-panel__checkbox {
		margin-bottom: @spacing-25;
	}

	.cm-mw-panel__row {
		align-items: center;
		column-gap: @spacing-50;
		display: flex;

		&:not( :last-child ) {
			margin-bottom: @spacing-50;
		}
	}

	.cm-mw-panel__button {
		margin-bottom: 0;
	}

	.cm-mw-panel__toggle-button.cdx-toggle-button--toggled-on {
		&:enabled:active {
			background-color: @color-progressive--active;
		}

		.cdx-icon {
			background-color: @color-inverted;
		}
	}
}

// Remove top margin on nested fields
.cm-mw-panel__fieldset .cdx-field:first-of-type {
	margin-top: 0;
}

// Space the select from the checkbox that it belongs to.
.cm-mw-panel__fieldset .cm-mw-panel__field-group .cm-mw-panel__select {
	margin-top: @spacing-25;

	&.cm-mw-panel__select--hidden {
		display: none;
	}
}

// Inline checkboxes carry no top margin, so a select has to space
// itself from whatever follows it.
.cm-mw-panel__fieldset .cm-mw-panel__select:not( :last-child ) {
	margin-bottom: @spacing-75;
}

// Dialogs

.cm-mw-dialog-backdrop {
	opacity: @opacity-transparent;

	&.cm-mw-dialog--hidden {
		display: none;
	}

	&.cm-mw-dialog-animate-show {
		opacity: @opacity-base;
	}
}

.cdx-dialog.cm-mw-dialog {
	// Puts the help links flush left, with the footer actions to the right.
	&.cm-mw-preferences-dialog .cdx-dialog__footer {
		align-items: center;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
	}

	// Flex rather than inline text, so the links reverse in RTL even when the
	// messages themselves fall back to LTR English.
	.cm-mw-dialog__help {
		display: flex;
		font-size: @font-size-small;
		gap: @spacing-25;
	}

	.cm-mw-dialog--columns {
		columns: 3 19.5em;
		font-size: @font-size-small;

		&--two-col {
			column-count: 2;
		}

		.cdx-checkbox,
		legend {
			display: inline-block;
			width: 100%;
		}
	}
}

// Find and replace

.cm-editor .cm-panels .cm-mw-panel__search-panel {
	container-type: inline-size;

	.cm-mw-panel__text-input {
		min-width: auto;
	}
	// Responsive design (T386777)
	@container ( max-width: 400px ) {
		// NOTE: We can't use & here yet, see https://github.com/wikimedia/less.php/issues/91
		.cm-editor .cm-panels .cm-mw-panel__row {
			flex-wrap: wrap;
			row-gap: @spacing-50;

			.cm-mw-panel__text-input {
				flex-basis: 100%;
			}

			.cm-mw-panel__button {
				flex-grow: 1;
			}
		}
	}
}

.cm-mw-find-results {
	color: @color-placeholder;
	position: absolute;
	right: 9px;
	top: 50%;
	transform: translateY( -50% );
}

.cm-mw-icon--match-case {
	background-color: @color-base;
	.cdx-mixin-css-icon( @cdx-icon-search-case-sensitive, @color-base, @size-icon-medium, true );
}

.cm-mw-icon--regexp {
	background-color: @color-base;
	.cdx-mixin-css-icon( @cdx-icon-search-regular-expression, @color-base, @size-icon-medium, true );
}

.cm-mw-icon--quotes {
	background-color: @color-base;
	.cdx-mixin-css-icon( @cdx-icon-quotes, @color-base, @size-icon-medium, true );
}

.cm-mw-icon--previous {
	background-color: @color-base;
	.cdx-mixin-css-icon( @cdx-icon-previous, @color-base, @size-icon-medium, true );
}

.cm-mw-icon--next {
	background-color: @color-base;
	.cdx-mixin-css-icon( @cdx-icon-next, @color-base, @size-icon-medium, true );
}

.cm-mw-icon--close {
	background-color: @color-base;
	.cdx-mixin-css-icon( @cdx-icon-close, @color-base, @size-icon-medium, true );
}

// Compact panels (T436422)
// Each selector carries one more class than the docked rules above,
// so it wins regardless of source order.
.cm-editor.cm-mw-compact-panels .cm-panels {
	&.cm-panels-top {
		border: @border-width-base @border-style-base @border-color-subtle;
		border-radius: @border-radius-base;
		box-shadow: @box-shadow-drop-medium;
		font-size: @font-size-small;
		// CodeMirror sets `top` inline, so inset from the top with a margin.
		margin-top: @spacing-50;
		max-width: ~'calc( 100% - @{spacing-200} )';
		position: absolute;
		// The search panel is a size container, so this box has no intrinsic
		// width to shrink to. 28rem fits the English labels on one row each.
		width: @size-2800;
		// The base theme pins both edges; release the start one. CSSJanus flips
		// this pair, so the panel floats at the inline end either way.
		left: auto;
		// Keeps the panel clear of the scroller's scrollbar, which CSS cannot measure.
		right: @spacing-150;
	}

	.cm-mw-panel {
		padding: @spacing-25 @spacing-50;
		// Button labels must never break mid-word; the row wraps instead.
		white-space: nowrap;

		// The container above draws the outer border.
		&:last-child {
			border-bottom: 0;
		}
	}

	.cm-mw-panel__row {
		flex-wrap: wrap;
		gap: @spacing-25;

		&:not( :last-child ) {
			margin-bottom: @spacing-25;
		}
	}

	// 24px is the smallest target size WCAG 2.2 allows. Do not go below @size-150.
	.cdx-button,
	.cdx-toggle-button,
	.cdx-text-input__input {
		min-height: @size-150;
		padding: 0 @spacing-50;
	}

	.cdx-button--icon-only,
	.cdx-toggle-button {
		padding: 0 @spacing-25;
	}

	.cdx-button-group,
	.cdx-toggle-button-group {
		// A group is a block box of inline buttons, which makes it a pixel taller
		// than the buttons beside it and leaves the whole row on a half pixel.
		display: flex;

		.cdx-button,
		.cdx-toggle-button {
			min-width: @size-150;
		}
	}

	// Track the reduced input padding. The width is measured at runtime by
	// CodeMirrorSearch#updateSearchInputPadding(), so the smaller font is handled.
	.cm-mw-find-results {
		right: @spacing-50;
	}

	.cm-mw-icon--match-case {
		.cdx-mixin-css-icon( @cdx-icon-search-case-sensitive, @color-base, @size-icon-small, true );
	}

	.cm-mw-icon--regexp {
		.cdx-mixin-css-icon( @cdx-icon-search-regular-expression, @color-base, @size-icon-small, true );
	}

	.cm-mw-icon--quotes {
		.cdx-mixin-css-icon( @cdx-icon-quotes, @color-base, @size-icon-small, true );
	}

	.cm-mw-icon--previous {
		.cdx-mixin-css-icon( @cdx-icon-previous, @color-base, @size-icon-small, true );
	}

	.cm-mw-icon--next {
		.cdx-mixin-css-icon( @cdx-icon-next, @color-base, @size-icon-small, true );
	}

	.cm-mw-icon--close {
		.cdx-mixin-css-icon( @cdx-icon-close, @color-base, @size-icon-small, true );
	}
}

// Status bar

.cm-mw-panel__status-worker,
.cm-mw-panel__status-message,
.cm-mw-panel__status-line {
	padding: 0 0.3em;
	display: table-cell;
}

.cm-mw-panel__status-worker {
	user-select: none;
	cursor: pointer;
}

.cm-mw-panel__status-worker > * {
	display: table-cell;
	white-space: nowrap;
	padding-right: 8px;
}

.cm-mw-panel__status-worker > * > div {
	display: inline-block;
	vertical-align: middle;
}

.cm-mw-panel__status-worker > * > div:first-child {
	margin-right: 4px;
	height: 1em;
}

.cm-mw-panel__status-message {
	border-left: @border-subtle;
	border-right: @border-subtle;
	width: 100%;
}

.cm-mw-panel__status-message .cm-diagnosticAction {
	padding-top: 0;
	padding-bottom: 0;
}

.cm-mw-panel__status-line {
	cursor: pointer;
	text-align: right;
	white-space: nowrap;
	// Looks better with the resize handle
	padding-right: 0.6em;
}

.cm-diagnosticText-clickable {
	cursor: pointer;
}

.cm-indent-guide {
	background-image: linear-gradient( to left, @border-color-muted 0 1px, transparent 1px 100% );
	background-size: 4ch 100%;
	background-repeat: repeat;
}

// Fully transparent ::selection style is broken in Firefox 153 (T433213)
.cm-scroller .cm-line ::selection,
.cm-scroller .cm-line::selection {
	/* stylelint-disable-next-line declaration-no-important */
	background-color: #ffffff01 !important;
}

.cm-editor .cm-css-color-picker-wrapper {
	background-image: linear-gradient( 45deg, #ccc 25%, transparent 25%, transparent 75%, #ccc 75% ), linear-gradient( 45deg, #ccc 25%, #fff 25%, #fff 75%, #ccc 75% );
	background-size: 1em;
	background-position: 0 0, 0.5em 0.5em;
}
