@mixin grid-focus() {
	outline: none;
	border: 1px solid var(--gray-400) !important;
}
.form-grid {
	border: 1px solid var(--table-border-color);
	border-radius: var(--border-radius-md);
	color: var(--text-color);
	min-height: 75px;
	background-color: var(--subtle-accent);
	overflow-y: hidden;
}

.form-grid.error {
	border-color: var(--error-border);
	overflow: hidden;
}

.grid-heading-row {
	border-bottom: 1px solid var(--table-border-color);
	color: var(--gray-600);
	@include get_textstyle("sm", "regular");
	height: 32px;
	padding: 0 !important;
	border-top-left-radius: var(--border-radius-md);
	border-top-right-radius: var(--border-radius-md);

	.grid-row {
		background-color: var(--subtle-fg);
		border-top-left-radius: var(--border-radius-md);
		border-top-right-radius: var(--border-radius-md);

		.row-check {
			border-top-left-radius: var(--border-radius-md);
		}
		.row .col:last-child {
			border-top-right-radius: var(--border-radius-md);
		}
	}

	&.with-filter {
		height: 66px;
		.grid-row:nth-child(2) {
			.row-check,
			.row-index {
				background-color: var(--fg-color);
			}
		}
	}
	.filter-row {
		background-color: var(--bg-color);

		.search {
			// TODO: Align with table grid without overwriting padding if possible
			padding: 4px 7px 4px 7px !important;
			background-color: var(--fg-color);
		}
	}

	.grid-static-col,
	.row-check,
	.row-index {
		height: 32px;
		padding: 4px 8px !important;
		background-color: var(--subtle-fg);
	}
	.row-check,
	.row-index {
		position: sticky;
		left: 0;
		z-index: 1;
	}
	.row-index {
		left: 31px;
	}
	.grid-static-col {
		padding: 6px 8px !important;
		&.grid-data-column {
			flex: 1;
			max-width: none;
		}
		.static-area {
			&.reqd:after {
				content: " *";
				color: var(--red-400);
			}
		}
	}
}

.rows .grid-row .data-row,
.rows .grid-row .grid-footer-toolbar,
.grid-form-heading {
	cursor: pointer;
}

.data-row textarea {
	height: 38px;
}

.form-grid .data-row {
	// padding: 10px 15px;

	&.highlight {
		background-color: var(--yellow-highlight-color);
	}

	&.sortable-handle {
		cursor: move;
	}
}

// hide row index in 6/4 column child tables
.form-column.col-sm-6,
.form-column.col-sm-4 {
	.form-grid {
		.row-index {
			display: none;
		}
	}
}

.modal .form-grid .row-index {
	display: none;
}

.form-grid .grid-heading-row .template-row {
	margin-left: 8px;
}

.form-grid .template-row {
	width: calc(100% - 30px);
	padding: 8px 15px;
}

.grid-body .data-row {
	@include get_textstyle("sm", "regular");
	color: var(--text-muted);
	.row-check,
	.row-index {
		position: sticky;
		left: 0;
		background-color: var(--fg-color);
		z-index: 1;
	}
	.row-index {
		left: 31px;
	}
	button.col:focus-visible {
		outline: 1px solid #c9c9c9;
	}
}

.grid-empty,
.list-loading {
	@include flex(flex, center, center, column);
	padding: var(--padding-lg) var(--padding-sm);
	color: var(--text-muted);

	img {
		margin-bottom: var(--margin-sm);
	}
}

.grid-empty {
	height: 43px;
	background-color: var(--subtle-accent);
	display: flex;
	vertical-align: middle;
}
.grid-static-col:focus-visible {
	@include grid-focus();
}
.grid-static-col,
.row-index {
	height: 43px;
	max-height: 200px;

	&.search {
		padding: 7px !important;

		input {
			height: 100%;
			padding: 3px 7px;
		}
	}
}

.row-check {
	// height: 38px;
	text-align: center;
	flex-grow: 0;

	input {
		margin-right: 0 !important;
		margin-bottom: -3px;
	}

	&.search {
		padding: var(--grid-padding) !important;
	}
}

.grid-row-check {
	margin-top: 3px;

	&::after {
		// Extend the checkbox's clickable area
		display: block;
		content: "";
		inset: -8px;
		position: absolute;
		opacity: 0;
	}
}

.template-row-index {
	float: left;
	margin-left: 15px;
	margin-top: 8px;
	margin-right: -20px;

	span {
		margin-left: 5px;
	}
}

.editable-form .grid-static-col.bold {
	font-weight: bold;
}

.editable-form .grid-static-col.invalid {
	background-color: var(--error-bg);
}

.validated-form .grid-static-col.error {
	background-color: var(--error-bg);
}

.row-index {
	text-align: center;
	flex-grow: 0;
}

.grid-row > .row {
	.col:last-child {
		border-right: none;
		min-width: 0;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.col {
		padding: var(--grid-padding);
		border-right: 1px solid var(--table-border-color);
		// overflow: hidden;
	}
	.sticky-grid-col {
		position: sticky;
		z-index: 1;
	}
}

.grid-body {
	background-color: var(--subtle-accent);
	border-bottom-left-radius: var(--border-radius);
	border-bottom-right-radius: var(--border-radius);
	margin-bottom: -1px; // double bottom border

	.col:last-child {
		border: none;
		background-color: var(--fg-color);
	}

	.btn-open-row {
		line-height: unset;
		div {
			margin-left: var(--margin-xs);
		}
	}

	.editable-row {
		// TODO: Dark and Light Mode
		--control-bg: var(--neutral);
		--input-disabled-bg: var(--gray-50);
		.grid-static-col {
			padding: 0px !important;
		}
		.grid-static-col.row-index {
			padding: var(--grid-padding) !important;
		}

		.frappe-control[data-fieldtype="Select"].form-group .select-icon {
			top: 9px;
		}

		.checkbox {
			margin: 0px;
			text-align: center;
			margin-top: var(--padding-sm);
		}

		textarea {
			height: 43px !important;
		}

		.form-control,
		.ql-editor {
			border-radius: 0px;
			border: 0px;
			padding-top: 10px;
			padding-bottom: calc(var(--padding-md) - 3px);
			height: auto;
		}

		.link-btn {
			background-color: var(--bg-color);
			height: calc(100% - 4px);
			margin-top: 2px;
		}

		.form-control:focus {
			border-color: $text-muted;
			box-shadow: inset 0px 0px 0 2px rgba(124, 124, 124, 0.25);
		}

		.has-error .form-control {
			&:focus {
				border-color: var(--error-border);
			}
		}

		input.form-control.invalid {
			background-color: var(--error-bg);
		}

		input[data-fieldtype="Int"],
		input[data-fieldtype="Float"],
		input[data-fieldtype="Currency"] {
			text-align: right;
		}
	}

	.grid-static-col[data-fieldtype="Button"] .field-area {
		margin-top: var(--margin-xs);
		margin-left: var(--margin-xs);

		button {
			height: 24px;
		}
	}

	.grid-static-col[data-fieldtype="Check"] .static-area {
		padding-top: 4px;
		margin-bottom: -4px;
	}

	.grid-static-col[data-fieldtype="Rating"] .field-area {
		margin-top: 0.6rem;
		margin-left: 0.5rem;
	}

	.grid-static-col[data-fieldtype="Code"],
	.grid-static-col[data-fieldtype="HTML Editor"] {
		overflow: hidden;

		.static-area {
			margin-top: calc(-1 * var(--margin-xs));

			pre {
				background: none;
				border: none;
			}
		}
	}

	.grid-static-col[data-fieldtype="Text Editor"] {
		overflow: hidden;
		margin: 0 !important;

		.ql-editor {
			overflow-y: auto !important;
			min-height: 0 !important;
			max-height: unset !important;
			line-height: 1.3 !important;
		}
	}

	.grid-static-col {
		background-color: var(--fg-color);
		&.sticky-grid-col {
			position: sticky;
			z-index: 1;
		}
	}
	// check if the awesomplete dropdown is hidden
	.grid-static-col:has(.awesomplete > ul:not([hidden])) {
		z-index: 2;
	}
}

@media (max-width: 767px) {
	// lesser padding for controls
	.editable-row .frappe-control {
		padding-top: 0px !important;
		padding-bottom: 0px !important;
		margin-left: -1px !important;
		margin-right: -1px !important;
	}
}

.row-data > .row {
	margin-left: var(--margin-md);
}

.grid-row {
	border-bottom: 1px solid var(--table-border-color);
	background-color: var(--fg-color);

	@include transition(0.2s);

	td {
		vertical-align: top;
	}

	p {
		margin-bottom: var(--margin-xs);
	}

	.frappe-control {
		margin-bottom: 0px !important;
		position: relative;
	}

	.col-sm-6 {
		.editor-toolbar-text-group,
		.editor-toolbar-align-group {
			display: none;
		}
	}

	.editable-row {
		.markdown-container {
			position: relative;
			z-index: 1;
		}
	}

	.grid-currency-input {
		position: relative;
		display: flex;
		align-items: center;
		width: 100%;
	}

	.grid-currency-prefix,
	.grid-currency-suffix {
		position: absolute;
		color: var(--text-muted);
		pointer-events: none;
		z-index: 2;
		display: none;
	}

	.grid-currency-prefix {
		left: 8px;
	}

	.grid-currency-suffix {
		right: 8px;
	}

	// for left aligned currency symbol
	.grid-currency-input.grid-currency-has-value:not(.grid-currency-symbol-right) .form-control {
		padding-left: calc(4px + 1em);
		position: relative;
		z-index: 1;
		text-align: left;
	}

	// for right aligned currency symbol
	.grid-currency-input.grid-currency-has-value.grid-currency-symbol-right .form-control {
		padding-right: calc(4px + 1em);
		position: relative;
		z-index: 1;
	}

	.grid-currency-input.grid-currency-has-value .grid-currency-prefix,
	.grid-currency-input.grid-currency-has-value .grid-currency-suffix {
		display: block;
	}
}

@mixin base-grid() {
	background-color: var(--modal-bg);
	position: relative;
	transition: opacity 0.2s ease;
}

.form-in-grid {
	overflow: hidden;
	height: 0;
	opacity: 0;
	z-index: 1021;
	border-radius: var(--border-radius-md);
	@include base-grid();

	.panel-title {
		line-height: 1.8em;
	}

	.btn {
		box-shadow: none;
	}

	.form-section {
		padding: var(--padding-md);

		&:not(:first-child) {
			border-top: 1px solid var(--border-color);
		}

		.section-head {
			padding: 0;
			@include get_textstyle("lg", "medium");
		}

		.form-column:first-child {
			padding-left: 0;
		}
	}

	.grid-delete-row {
		.icon use {
			stroke: var(--fg-color);
		}
	}

	.grid-append-row {
		margin-top: calc(-1 * var(--margin-xs));
	}
}

#freeze.grid-form {
	z-index: 1020;
}

.recorder-form-in-grid {
	z-index: 0;
	@include base-grid();
}

.grid-row-open .form-in-grid {
	opacity: 1;
	height: auto;
	overflow: auto;
	margin: auto;
	padding: var(--padding-sm) var(--padding-md);
	position: fixed;
	top: 5%;
	left: 50%;
	transform: translate(-50%, 0%);
	width: 80%;

	body:not(.full-width) & {
		max-width: var(--page-max-width);
	}

	.grid-form-body {
		max-height: 80vh;
		overflow: auto;
	}

	// Tabs styling for child table grid row forms
	.form-tabs-list {
		position: relative;
		margin-bottom: var(--margin-sm);
		background-color: var(--card-bg);
		border-bottom: 1px solid var(--border-color);

		.form-tabs {
			display: flex;
			flex-wrap: nowrap;
			overflow-x: auto;
			padding: 0 var(--padding-sm);
			margin: 0;
			list-style: none;

			.nav-item {
				flex-shrink: 0;

				&.hide {
					display: none !important;
				}

				.nav-link {
					padding: var(--padding-sm) var(--padding-md);
					color: var(--text-muted);
					border: none;
					background: none;
					white-space: nowrap;
					cursor: pointer;
					border-bottom: 2px solid transparent;

					&:hover {
						color: var(--text-color);
					}

					&.active {
						/*color: var(--primary);*/
						font-weight: 600;
						border-bottom-color: var(--primary);
					}
				}
			}
		}
	}

	.form-tab-content {
		margin-top: 0;

		> .tab-pane {
			display: none;

			&.show.active {
				display: block;
			}
		}
	}
}

.grid-form-heading {
	padding: var(--padding-sm) var(--padding-md);
	font-size: var(--text-xl);
	font-weight: 600;
	// border-bottom: 1px solid var(--border-color);
}

.grid-form-body {
	.form-area.scrollable {
		max-height: calc(100vh - 3.5rem);
		overflow-y: auto;
	}
}

.grid-header-toolbar {
	display: flex;
	justify-content: space-between;
	.row-actions {
		button:not(:last-child) {
			margin-left: var(--margin-xs);
			margin-bottom: var(--margin-xs);
		}
	}
}

.grid-buttons,
.grid-bulk-actions {
	display: inline-flex;
}

.grid-custom-buttons:empty {
	padding: 0;
}
.grid-footer {
	margin-top: var(--margin-sm);
}
.grid-footer,
.grid-custom-buttons {
	padding: var(--padding-sm) 0px;
	background-color: var(--fg-color);
	.btn {
		box-shadow: none;
		margin-top: -3px;
		margin-bottom: -3px;
	}

	.btn:not(:last-child) {
		margin-right: 4px;
	}
}

.grid-pagination {
	padding: 0;
	display: flex;
	.last-page {
		margin-left: 5px;
	}
}

.page-text {
	display: inline-block;
	cursor: default;
	margin-right: 6px;
}

.current-page-number {
	width: 16px;
	text-align: center;
	border: none;
	cursor: text;

	&:focus {
		outline: none;
	}
}

.prev-page,
.next-page {
	.icon {
		width: 10px;
	}
}

.prev-page {
	margin-left: var(--margin-xs);
	text-decoration: none;
}

.next-page {
	margin-right: var(--margin-xs);
	text-decoration: none;
}

.page-number {
	background-color: var(--fg-color);
	padding: 0 3px;
}

.grid-footer-toolbar {
	padding: var(--padding-md) var(--padding-sm) var(--padding-xs) var(--padding-sm);
	// border-top: 1px solid var(--border-color);
	span {
		margin-right: var(--margin-xs);
	}

	button {
		margin-left: var(--margin-xs);
	}
}

.grid-overflow-no-ellipsis {
	word-wrap: break-word;
	overflow: hidden;
	padding-right: 0px;
}

.grid-overflow-ellipsis {
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
	padding-right: 0px;
}

.grid-label {
	margin-right: 8px;
	margin-bottom: 4px;
}

.form-grid-container {
	position: relative;
	overflow-x: auto;
	width: 100%;
}

.form-grid-container:has(.grid-row.grid-row-open) {
	overflow-x: clip;
	white-space: normal;
	overflow-y: unset;
	.form-grid {
		left: 0px !important;
	}
}
.sortable-handle span {
	width: 31px;
	display: block;
}

.data-row.row {
	flex-wrap: nowrap;
}
.frappe-control[data-fieldtype="Table"].form-group:has(.column-limit-reached):not(.highlight) {
	overflow-x: clip;
}
.column-limit-reached {
	background-color: var(--subtle-accent);
	border-radius: var(--border-radius-md);
	border: 1px solid var(--table-border-color);
	overflow-y: hidden;
	.form-grid {
		display: grid;
		grid-auto-rows: min-content;
		border: unset;
		&:focus-visible {
			@include grid-focus();
		}
		.grid-static-col .grid-body {
			height: 40px;
		}
		.grid-static-col.col-xs-1 {
			flex: 1 0 60px;
			width: 60px;
		}
		.grid-static-col.col-xs-2 {
			flex: 1 0 100px;
			width: 100px;
		}
		.grid-static-col.col-xs-3 {
			flex: 1 0 140px;
			width: 140px;
		}
		.grid-static-col.col-xs-4 {
			flex: 1 0 200px;
			width: 200px;
		}
		.grid-static-col.col-xs-5 {
			flex: 1 0 250px;
			width: 250px;
		}
		.grid-static-col.col-xs-6 {
			flex: 1 0 300px;
			width: 300px;
		}
		.grid-static-col.col-xs-7 {
			flex: 1 0 350px;
			width: 350px;
		}
		.grid-static-col.col-xs-8 {
			flex: 1 0 400px;
			width: 400px;
		}
		.grid-static-col.col-xs-9 {
			flex: 1 0 450px;
			width: 450px;
		}
		.grid-static-col.col-xs-10 {
			flex: 1 0 500px;
			width: 500px;
		}
		.grid-static-col.col-xs-11 {
			flex: 1 0 550px;
			width: 550px;
		}
		.grid-static-col.col-xs-12 {
			flex: 1 0 600px;
			width: 600px;
		}
		.grid-row > .row .col:last-child,
		.grid-row > .dialog-assignment-row .col:first-child,
		.grid-row > .dialog-assignment-row .col:last-child {
			max-width: 30px;
			min-width: 30px;
			position: sticky;
			right: 0;
			z-index: 1;
			line-height: 1;
		}
		.grid-row > .row .col:nth-child(2),
		.grid-row > .dialog-assignment-row .col:nth-child(2) {
			max-width: 40px;
			min-width: 40px;
		}
		.grid-heading-row .grid-row .data-row.row,
		.grid-body .rows .grid-row .data-row.row {
			justify-content: space-between;
		}
	}
	scrollbar-width: auto;
	scrollbar-color: auto;
}
.grid-scroll-bar {
	overflow-x: auto;
	height: 12px;
	position: relative;
	scrollbar-width: auto;
	scrollbar-color: auto;
}

.grid-scroll-bar::-webkit-scrollbar {
	height: 11px !important;
}

.grid-scroll-bar-rows {
	height: 100%;
}

.sticky-col-container {
	display: flex;
	justify-content: center;
	align-items: center;
}

@media (max-width: map-get($grid-breakpoints, "md")) {
	.form-column.col-sm-6 .form-grid {
		.row-index {
			display: block;
		}
	}
	.column-limit-reached {
		.form-grid {
			.grid-static-col.col-xs-2 {
				flex: 1 0 90px;
				width: 90px;
			}
			.grid-static-col.col-xs-3 {
				flex: 1 0 120px;
				width: 120px;
			}
			.grid-static-col.col-xs-4 {
				flex: 1 0 160px;
				width: 160px;
			}
			.grid-static-col.col-xs-5 {
				flex: 1 0 200px;
				width: 200px;
			}
			.grid-static-col.col-xs-6 {
				flex: 1 0 240px;
				width: 240px;
			}
			.grid-static-col.col-xs-7 {
				flex: 1 0 300px;
				width: 300px;
			}
			.grid-static-col.col-xs-8 {
				flex: 1 0 350px;
				width: 350px;
			}
			.grid-static-col.col-xs-9 {
				flex: 1 0 400px;
				width: 400px;
			}
			.grid-static-col.col-xs-10 {
				flex: 1 0 450px;
				width: 450px;
			}
			.grid-static-col.col-xs-11 {
				flex: 1 0 500px;
				width: 500px;
			}
			.grid-static-col.col-xs-12 {
				flex: 1 0 550px;
				width: 550px;
			}
		}
	}
}

@media (max-width: map-get($grid-breakpoints, "sm")) {
	.form-in-grid .form-section .form-column {
		padding-left: 0 !important;
		padding-right: 0 !important;
	}
}
