/*
    DbqChartControl (Weblibs/Scripts/DbqChartControl/DbqChartControl.js)
    - The card, header, toolbar and legend are HTML; ECharts only draws the plot.
    - The legend picks its position from the width of the card (container queries), not from the viewport.
*/

$dbqchc-text-color: color(text-color-default);
$dbqchc-surface-color: color(default-background);

.dbqchc {
	// Colors read by DbqChartControl when it builds the ECharts options
	--dbqchc-text: #{$dbqchc-text-color};
	--dbqchc-text-muted: #{rgba($dbqchc-text-color, 0.62)};
	--dbqchc-grid: #{rgba(color(graph-grid-line), 0.1)};
	--dbqchc-surface: #{$dbqchc-surface-color};
	--dbqchc-tooltip-background: #{$dbqchc-surface-color};
	--dbqchc-tooltip-text: #{$dbqchc-text-color};
	--dbqchc-rest: #{rgba($dbqchc-text-color, 0.25)};

	container: dbqchc / inline-size;
	display: flex;
	flex-direction: column;
	gap: 8px;
	width: 100%;
	box-sizing: border-box;
	padding: 12px 14px 14px 14px;
	color: $dbqchc-text-color;
	font-family: "Red Hat Text", sans-serif;
}

// Header with the title and the actions
.dbqchc-header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 8px;
	min-height: 28px;
}

.dbqchc-title {
	min-width: 0;
	font-size: 1rem;
	font-weight: 600;
	line-height: 1.3;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.dbqchc-actions {
	display: flex;
	gap: 2px;
	margin-left: auto;
	flex-shrink: 0;
}

.dbqchc-action {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	width: 28px;
	height: 28px;
	padding: 0;
	border: 0;
	border-radius: 6px;
	background: none;
	color: var(--dbqchc-text-muted);
	cursor: pointer;

	svg {
		width: 18px;
		height: 18px;
		fill: currentColor;
	}

	&:hover {
		color: $color-primary;
		background: rgba($dbqchc-text-color, 0.06);
	}

	&:focus-visible {
		outline: 2px solid $color-primary;
		outline-offset: 1px;
	}

	&[hidden] {
		display: none;
	}
}

// Body: the plot and the legend. Narrow cards show the legend under the plot.
.dbqchc-body {
	display: flex;
	flex-direction: column;
	gap: 10px;
	min-width: 0;
}

.dbqchc-plot {
	width: 100%;
	height: var(--dbqchc-height, 400px);
	min-width: 0;
}

// Data points that can be clicked (ECharts sets an inline cursor on its canvas, so this needs !important)
.dbqchc-plot.is-selectable,
.dbqchc-plot.is-selectable canvas {
	cursor: pointer !important;
}

// Pies never get taller than the card is wide
.dbqchc[data-chart-kind=pie] .dbqchc-plot {
	height: min(var(--dbqchc-height, 400px), 80cqw);
}

// Legend
.dbqchc-legend {
	display: grid;
	grid-template-columns: minmax(0, 1fr);
	gap: 2px;
	max-height: 168px;
	margin: 0;
	padding: 0;
	list-style: none;
	overflow-y: auto;
	@include custom-scrollbar(false, 'small');

	&:empty {
		display: none;
	}
}

.dbqchc-legend-item {
	display: grid;
	grid-template-columns: 12px minmax(0, 1fr) auto auto;
	gap: 8px;
	align-items: center;
	padding: 3px 6px;
	border-radius: 5px;
	font-size: 0.82rem;
	line-height: 1.35;
	font-variant-numeric: tabular-nums;
	cursor: pointer;

	&:hover,
	&:focus-visible {
		background: rgba($dbqchc-text-color, 0.06);
		outline: none;
	}

	&.is-hidden {
		opacity: 0.4;
	}
}

.dbqchc-legend-marker {
	width: 12px;
	height: 12px;
	border-radius: 3px;
}

.dbqchc-legend-name {
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.dbqchc-legend-name,
.dbqchc-legend-value {
	color: var(--dbqchc-text);
}

.dbqchc-legend-value {
	text-align: right;
}

.dbqchc-legend-percentage {
	min-width: 3.2em;
	text-align: right;
	color: var(--dbqchc-text-muted);
}

// Bar, column and line charts: the series legend sits above the plot as a row of chips
.dbqchc[data-chart-kind=cartesian] {
	.dbqchc-legend {
		order: -1;
		display: flex;
		flex-wrap: wrap;
		gap: 2px 10px;
		max-height: none;
		overflow: visible;

		&:empty {
			display: none;
		}
	}

	.dbqchc-legend-item {
		grid-template-columns: 12px auto;
	}
}

// Medium cards: pie legend in columns under the plot, but only as many as fit with room for a readable name
// (a column is at least 240px). auto-fit collapses empty columns, so a single item uses the full width.
@container dbqchc (min-width: 360px) {
	.dbqchc[data-chart-kind=pie] .dbqchc-legend {
		grid-template-columns: repeat(auto-fit, minmax(min(100%, 240px), 1fr));
		column-gap: 14px;
	}
}

// Wide cards: pie legend to the right of the plot, with values and percentages
@container dbqchc (min-width: 540px) {
	.dbqchc[data-chart-kind=pie] {
		.dbqchc-body {
			display: grid;
			grid-template-columns: minmax(0, 1fr) minmax(200px, 42%);
			align-items: center;
			gap: 20px;
		}

		.dbqchc-plot {
			height: min(var(--dbqchc-height, 400px), 50cqw);
		}

		.dbqchc-legend {
			grid-template-columns: minmax(0, 1fr);
			max-height: var(--dbqchc-height, 400px);
		}
	}
}

// Narrow cards: hide the legend unless the user asked for it, and drop the values
@container dbqchc (max-width: 299px) {
	.dbqchc[data-legend-mode=auto] .dbqchc-legend {
		display: none;
	}

	.dbqchc-legend-item {
		grid-template-columns: 12px minmax(0, 1fr) auto;

		.dbqchc-legend-value {
			display: none;
		}
	}
}

// Explicit legend choice from the legend button
.dbqchc[data-legend-mode=hidden] .dbqchc-legend {
	display: none !important;
}

.dbqchc[data-chart-kind=pie][data-legend-mode=shown] .dbqchc-legend:not(:empty) {
	display: grid;
}

.dbqchc[data-chart-kind=cartesian][data-legend-mode=shown] .dbqchc-legend:not(:empty) {
	display: flex;
}

// Compact tile (dashboards above or below a narrow list, see DbqChartControl.updateCompactState).
// The plot and legend are replaced by the total and a 100% bar. Clicking the tile opens the full chart.
.dbqchc-compact {
	display: none;
}

.dbqchc[data-compact=true]:not(.dbqchc-fullscreen) {
	gap: 6px;
	padding: 10px 12px;

	.dbqchc-header {
		min-height: 0;
	}

	.dbqchc-title {
		font-size: 0.9rem;
	}

	.dbqchc-actions,
	.dbqchc-plot,
	.dbqchc-legend {
		display: none !important;
	}

	.dbqchc-compact {
		display: flex;
		flex-direction: column;
		gap: 6px;
		border-radius: 5px;
		cursor: pointer;

		&:focus-visible {
			outline: 2px solid $color-primary;
			outline-offset: 3px;
		}
	}
}

.dbqchc-compact-main {
	display: flex;
	align-items: center;
	gap: 10px;
	min-width: 0;
}

.dbqchc-compact-total {
	flex-shrink: 0;
	font-size: 1.25rem;
	font-weight: 600;
	line-height: 1;
	font-variant-numeric: tabular-nums;
	color: var(--dbqchc-text);
}

.dbqchc-compact-bar {
	display: flex;
	flex: 1;
	gap: 2px;
	min-width: 0;
	height: 10px;
	border-radius: 5px;
	overflow: hidden;
}

.dbqchc-compact-segment {
	min-width: 3px;

	&.is-selectable:hover {
		filter: brightness(1.2);
	}
}

.dbqchc-compact-labels {
	display: flex;
	flex-wrap: wrap;
	gap: 2px 10px;
	max-height: 2.8em;
	margin: 0;
	padding: 0;
	overflow: hidden;
	list-style: none;
	font-size: 0.78rem;
	line-height: 1.35;
	color: var(--dbqchc-text-muted);
}

.dbqchc-compact-label {
	display: inline-flex;
	align-items: center;
	gap: 5px;
	min-width: 0;
	max-width: 100%;
	white-space: nowrap;

	.dbqchc-legend-marker {
		flex-shrink: 0;
		width: 8px;
		height: 8px;
		border-radius: 2px;
	}

	b {
		font-weight: 600;
		font-variant-numeric: tabular-nums;
		color: var(--dbqchc-text);
	}
}

.dbqchc-compact-label-name {
	overflow: hidden;
	text-overflow: ellipsis;
}

// Empty chart (DocumentElementGraph.showNoDataAvailableView): the same card header as a chart, a body that fills the
// rest of the card, and a message with a small sketch of the chart type above it. Flex instead of absolute positioning,
// so the message never overlaps the title, however low the card is.
.dbqchc.dbqchc-empty {
	--dbqchc-skeleton: #{rgba($dbqchc-text-color, 0.14)};
	height: 100%;
	min-height: 170px;
}

.dbqchc-empty-body {
	flex: 1;
	display: grid;
	place-items: center;
	min-height: 110px;
}

// Small sketch of the chart type (about the size of an icon), in the flow above the text
.dbqchc-empty-sketch {
	display: flex;
	align-items: flex-end;
	justify-content: center;
	gap: 5px;
	width: 52px;
	height: 40px;
	margin-bottom: 6px;

	i {
		display: block;
		background: var(--dbqchc-skeleton);
	}

	&[data-kind=columns] i {
		width: 9px;
		border-radius: 2px 2px 0 0;
	}

	&[data-kind=hbars] {
		flex-direction: column;
		align-items: flex-start;
		justify-content: center;
		gap: 4px;

		i {
			height: 7px;
			border-radius: 0 2px 2px 0;
		}
	}

	&[data-kind=ring] {
		align-items: center;

		i {
			width: 38px;
			height: 38px;
			border: 9px solid var(--dbqchc-skeleton);
			border-radius: 50%;
			background: none;
		}
	}
}

.dbqchc-empty-message {
	display: grid;
	justify-items: center;
	gap: 2px;
	max-width: 92%;
	padding: 10px 14px;
	text-align: center;

	b {
		font-size: 0.86rem;
		font-weight: 600;
		color: var(--dbqchc-text);
	}

	span {
		font-size: 0.76rem;
		color: var(--dbqchc-text-muted);
	}

	.dbqchc-empty-icon {
		display: inline-flex;
		margin-bottom: 2px;

		svg {
			width: 22px;
			height: 22px;
			fill: var(--dbqchc-text-muted);
			opacity: 0.7;
		}
	}

	&[data-state=error] .dbqchc-empty-icon svg {
		fill: color(text-color-error);
		opacity: 1;
	}
}

.dbqchc-empty-retry {
	margin-top: 6px;
	padding: 3px 12px;
	border: 1px solid $color-primary;
	border-radius: 999px;
	background: none;
	color: $color-primary;
	font: 600 0.76rem "Red Hat Text", sans-serif;
	cursor: pointer;

	&:hover {
		background: rgba($color-primary, 0.1);
	}

	&:focus-visible {
		outline: 2px solid $color-primary;
		outline-offset: 2px;
	}
}

// A value tile without a value: the normal tile with a dash and a short line
.dbqchc-empty-value {
	.graph-value-container {
		opacity: 0.45;
	}

	.graph-value-empty-text {
		margin-top: 2px;
		font-size: 0.75rem;
		color: rgba($dbqchc-text-color, 0.62);
	}
}

// Full screen
.dbqchc.dbqchc-fullscreen {
	position: fixed;
	inset: 0;
	z-index: 10000;
	padding: 24px;
	background: $dbqchc-surface-color;
	overflow: auto;

	.dbqchc-title {
		font-size: 1.25rem;
	}

	&[data-chart-kind=cartesian] .dbqchc-plot {
		height: calc(100vh - 140px);
	}

	&[data-chart-kind=pie] .dbqchc-plot {
		height: min(calc(100vh - 140px), 80cqw);
	}

	.dbqchc-legend {
		max-height: none;
	}

	.dbqchc-legend-item {
		font-size: 0.95rem;
	}
}

@container dbqchc (min-width: 540px) {
	.dbqchc.dbqchc-fullscreen[data-chart-kind=pie] {
		.dbqchc-body {
			grid-template-columns: minmax(0, 1fr) minmax(240px, 30%);
		}

		.dbqchc-plot {
			height: calc(100vh - 120px);
		}
	}
}
