/**
 * Creator LMS — MAR learner runner (frontend).
 *
 * Scoped under .crlms-mar-runner so it never leaks to other quiz types.
 * Reuses the brand CSS variables emitted on the frontend by
 * CommonHook::creator_lms_add_internal_styles() (--creator-lms-primary-color
 * etc.), with sensible fallbacks for themes that don't apply them.
 */

.crlms-mar-runner {
	--crlms-mar-border: #d0d0d0;
	--crlms-mar-surface: #ffffff;
	--crlms-mar-surface-muted: #f6f8fa;
	--crlms-mar-border-strong: #cbd0d7;
	--crlms-mar-muted-text: #7a8b9a;
	--crlms-mar-side-col: 240px;

	font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
	color: var(--creator-lms-body-text-color, #52525b);
	box-sizing: border-box;
	width: 100%;
}

/*
 * The plugin's own quiz-box wrapper hard-caps every quiz question at 720px
 * with 40px padding — that's appropriate for textual question types but
 * crushes the wide MAR sheet. When a MAR runner is detected inside, drop
 * the cap and tighten the chrome so the sheet uses real horizontal space.
 *
 * `:has()` is supported in all current browsers (Chrome 105+, Safari 15.4+,
 * Firefox 121+). Older browsers fall back to the existing 720px cap.
 */
.creator-lms-page .creator-lms-quiz .creator-lms-quiz-form .creator-lms-quiz-box:has( .crlms-mar-runner ),
.creator-lms-quiz-review .creator-lms-quiz-box:has( .crlms-mar-runner ) {
	max-width: none;
	padding: 24px;
}

@media ( max-width: 768px ) {
	.creator-lms-page .creator-lms-quiz .creator-lms-quiz-form .creator-lms-quiz-box:has( .crlms-mar-runner ),
	.creator-lms-quiz-review .creator-lms-quiz-box:has( .crlms-mar-runner ) {
		padding: 14px;
		border-radius: 10px;
	}
}

/* Tighter header + title block when a MAR runner is the box's payload —
   the runner has its own scenario sidebar with a clearer label, so the
   plugin's "Question N" + 20px title + 25px margin stack is wasted height. */
.creator-lms-page .creator-lms-quiz .creator-lms-quiz-form .creator-lms-quiz-box:has( .crlms-mar-runner ) .quiz-box-header {
	margin-bottom: 8px;
}
.creator-lms-page .creator-lms-quiz .creator-lms-quiz-form .creator-lms-quiz-box:has( .crlms-mar-runner ) .the-question {
	font-size: 16px;
	font-weight: 500;
	margin-bottom: 12px;
	line-height: 1.3;
}
.creator-lms-page .creator-lms-quiz .creator-lms-quiz-form .creator-lms-quiz-box:has( .crlms-mar-runner ) .question-box {
	margin-bottom: 0;
}

.crlms-mar-runner * { box-sizing: border-box; }

.crlms-mar-runner .crlms-mar-bank-sidebar {
	background: var(--crlms-mar-surface-muted);
	border: 1px solid var(--crlms-mar-border);
	border-radius: 6px;
	padding: 14px;
	position: sticky;
	top: 16px;
	max-height: calc(100vh - 40px);
	overflow-y: auto;
}

.crlms-mar-runner .crlms-mar-runner-bank-title {
	font-size: 14px;
	font-weight: 600;
	color: var(--creator-lms-heading-color, #000d25);
	margin: 0 0 8px;
}

/* --- Top row: sheet-toggle on the left + scenario filling the row -- */

.crlms-mar-runner .crlms-mar-runner-toprow {
	display: flex;
	flex-wrap: wrap;
	align-items: flex-start;
	gap: 14px;
	margin-bottom: 12px;
}

.crlms-mar-runner .crlms-mar-runner-toprow .crlms-mar-sheet-toggle {
	margin-bottom: 0; /* parent gap handles spacing */
	flex: 0 0 auto;
}

.crlms-mar-runner .crlms-mar-runner-scenario {
	flex: 1 1 320px; /* fill remaining row, but wrap below toggle if narrow */
	padding: 10px 14px;
	background: var(--crlms-mar-surface-muted);
	border: 1px solid var(--crlms-mar-border);
	border-radius: 6px;
	font-size: 14px;
	line-height: 1.5;
	max-height: 160px;
	overflow-y: auto;
}

.crlms-mar-runner .crlms-mar-runner-scenario :first-child { margin-top: 0; }
.crlms-mar-runner .crlms-mar-runner-scenario :last-child  { margin-bottom: 0; }

/* --- Sheet toggle --------------------------------------------------- */

.crlms-mar-runner .crlms-mar-sheet-toggle {
	display: inline-flex;
	gap: 0;
	margin-bottom: 12px;
	border: 1px solid var(--crlms-mar-border-strong);
	border-radius: 6px;
	overflow: hidden;
}

.crlms-mar-runner .crlms-mar-sheet-toggle-btn {
	appearance: none;
	background: transparent;
	border: 0;
	padding: 8px 14px;
	font-size: 13px;
	font-weight: 500;
	color: var(--creator-lms-body-text-color, #52525b);
	cursor: pointer;
	font-family: inherit;
}

.crlms-mar-runner .crlms-mar-sheet-toggle-btn + .crlms-mar-sheet-toggle-btn {
	border-left: 1px solid var(--crlms-mar-border-strong);
}

.crlms-mar-runner .crlms-mar-sheet-toggle-btn.is-active {
	background: var(--creator-lms-primary-color, #6e42d3);
	color: #fff;
}

.crlms-mar-runner .crlms-mar-sheet-toggle-btn:hover:not(.is-active) {
	background: var(--crlms-mar-surface-muted);
}

/* --- Sheet + sidebar split ----------------------------------------- */

.crlms-mar-runner .crlms-mar-mode-split {
	display: grid;
	gap: 14px;
	align-items: start;
	grid-template-columns: minmax(0, 1fr) var(--crlms-mar-side-col);
}

/* <=1024px — stack bank below sheet; bank becomes a horizontally-
   scrollable strip so chips stay visible without crushing the sheet. */
@media ( max-width: 1024px ) {
	.crlms-mar-runner .crlms-mar-mode-split {
		grid-template-columns: 1fr;
	}
	.crlms-mar-runner .crlms-mar-bank-sidebar {
		position: static;
		max-height: none;
		order: 2;
	}
	.crlms-mar-runner .crlms-mar-mode-main { order: 1; }
	.crlms-mar-runner .crlms-mar-bank-chips {
		flex-direction: row;
		overflow-x: auto;
		flex-wrap: nowrap;
		padding-bottom: 4px;
		gap: 8px;
	}
	.crlms-mar-runner .crlms-mar-bank-chip {
		flex: 0 0 auto;
		max-width: 220px;
	}
}

/* <=768px — phones. Tighter chrome; toprow stacks (toggle above
   scenario) so the scenario doesn't get squeezed to two words wide. */
@media ( max-width: 768px ) {
	.crlms-mar-runner { font-size: 13px; }
	.crlms-mar-runner .crlms-mar-runner-toprow {
		flex-direction: column;
		gap: 10px;
	}
	.crlms-mar-runner .crlms-mar-runner-scenario {
		flex: 1 1 auto;
		max-height: 120px;
		padding: 8px 12px;
	}
	.crlms-mar-runner .crlms-mar-mode-split { gap: 10px; }
	.crlms-mar-runner .crlms-mar-bank-sidebar { padding: 10px; }
	.crlms-mar-runner .crlms-mar-runner-bank-title {
		font-size: 13px;
		margin-bottom: 6px;
	}
	.crlms-mar-runner .crlms-mar-sheet-toggle-btn {
		padding: 6px 10px;
		font-size: 12px;
	}
	.crlms-mar-runner .crlms-mar-sheet-wrapper { padding: 6px; }
}

.crlms-mar-runner .crlms-mar-mode-main { min-width: 0; }

.crlms-mar-runner .crlms-mar-sheet-wrapper {
	border: 1px solid var(--crlms-mar-border);
	border-radius: 6px;
	padding: 10px;
	background: var(--crlms-mar-surface-muted);
	overflow-x: auto;
}

/* --- Sidebars: scenario + answer-bank ------------------------------ */

.crlms-mar-runner .crlms-mar-runner-bank-help {
	color: var(--crlms-mar-muted-text);
	font-size: 12px;
	margin: 0 0 10px;
}

.crlms-mar-runner .crlms-mar-runner-bank-empty {
	color: var(--crlms-mar-muted-text);
	font-style: italic;
	font-size: 12px;
}

.crlms-mar-runner .crlms-mar-bank-chips {
	display: flex;
	flex-direction: column;
	gap: 6px;
}

.crlms-mar-runner .crlms-mar-bank-chip {
	display: flex;
	gap: 8px;
	align-items: center;
	padding: 6px 10px;
	background: #fff;
	border: 1px solid var(--crlms-mar-border-strong);
	border-radius: 6px;
	cursor: grab;
	font-size: 13px;
	user-select: none;
}

.crlms-mar-runner .crlms-mar-bank-chip:hover {
	border-color: var(--creator-lms-primary-color, #6e42d3);
	box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}

.crlms-mar-runner .crlms-mar-bank-chip:active,
.crlms-mar-runner .crlms-mar-bank-chip.is-dragging {
	cursor: grabbing;
	opacity: 0.55;
}

.crlms-mar-runner .crlms-mar-bank-chip-type {
	font-size: 10px;
	font-weight: 600;
	text-transform: uppercase;
	letter-spacing: 0.03em;
	color: var(--crlms-mar-muted-text);
	background: var(--crlms-mar-surface-muted);
	padding: 1px 6px;
	border-radius: 999px;
	flex-shrink: 0;
}

.crlms-mar-runner .crlms-mar-bank-chip-label {
	flex: 1;
	min-width: 0;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

/* --- Dropzone + dropped chips inside cells ------------------------- */

.crlms-mar-runner .crlms-mar-sheet [data-cell].is-dropzone {
	cursor: pointer;
	background: rgba(var(--creator-lms-primary-color-rgb, 110, 66, 211), 0.04);
	outline: 2px dashed rgba(var(--creator-lms-primary-color-rgb, 110, 66, 211), 0.45);
	outline-offset: -2px;
}

.crlms-mar-runner .crlms-mar-sheet [data-cell].is-drag-over {
	background: rgba(var(--creator-lms-primary-color-rgb, 110, 66, 211), 0.20);
	outline-style: solid;
}

.crlms-mar-runner .crlms-mar-dropped-chip {
	display: inline-flex;
	align-items: center;
	gap: 4px;
	padding: 2px 4px 2px 8px;
	border-radius: 999px;
	font-size: 10px;
	line-height: 1.4;
	background: var(--creator-lms-primary-color, #6e42d3);
	color: #fff;
	max-width: 100%;
	margin: 1px;
}

.crlms-mar-runner .crlms-mar-dropped-label {
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
	max-width: 140px;
}

.crlms-mar-runner .crlms-mar-dropped-remove {
	appearance: none;
	border: 0;
	background: rgba(255, 255, 255, 0.25);
	color: #fff;
	cursor: pointer;
	width: 16px;
	height: 16px;
	border-radius: 50%;
	font-size: 12px;
	line-height: 1;
	padding: 0;
	display: inline-flex;
	align-items: center;
	justify-content: center;
}

.crlms-mar-runner .crlms-mar-dropped-remove:hover {
	background: rgba(255, 255, 255, 0.5);
}

/* --- Review mode (read-only) --------------------------------------- */

/*
 * Phase 6: review mode renders a saved attempt with chips already in
 * place server-side, plus per-zone correctness overlays. We reuse the
 * runner's surface-level styles; these rules only swap interactive
 * affordances for read-only ones and add the result overlays.
 */

.crlms-mar-runner.is-review {
	--crlms-mar-correct: #16a34a;
	--crlms-mar-incorrect: #dc2626;
}

/*
 * Review's scenario is rendered as a top-level full-width block (not in
 * a sidebar like the runner), so re-apply box styling here.
 */
.crlms-mar-runner.is-review .crlms-mar-runner-scenario {
	padding: 14px 16px;
	background: var(--crlms-mar-surface-muted);
	border: 1px solid var(--crlms-mar-border);
	border-radius: 6px;
	margin-bottom: 14px;
}

.crlms-mar-runner.is-review .crlms-mar-mode-split--review {
	grid-template-columns: minmax(0, 1fr);
}

.crlms-mar-review-summary {
	display: flex;
	flex-wrap: wrap;
	gap: 14px;
	align-items: center;
	justify-content: space-between;
	padding: 12px 16px;
	margin-bottom: 14px;
	border: 1px solid var(--crlms-mar-border);
	border-radius: 6px;
	background: #fff;
}

.crlms-mar-review-summary.is-perfect {
	border-color: var(--crlms-mar-correct, #16a34a);
	background: rgba(22, 163, 74, 0.05);
}

.crlms-mar-review-summary-score {
	display: flex;
	flex-direction: column;
	gap: 2px;
}

.crlms-mar-review-summary-label {
	font-size: 11px;
	text-transform: uppercase;
	letter-spacing: 0.04em;
	color: var(--crlms-mar-muted-text);
}

.crlms-mar-review-summary-score strong {
	font-size: 20px;
	font-weight: 600;
	color: var(--creator-lms-heading-color, #000d25);
}

.crlms-mar-review-summary-counts {
	display: flex;
	gap: 8px;
	flex-wrap: wrap;
}

.crlms-mar-review-pill {
	font-size: 12px;
	font-weight: 600;
	padding: 4px 10px;
	border-radius: 999px;
	display: inline-flex;
	align-items: center;
	gap: 4px;
}

.crlms-mar-review-pill.is-correct {
	background: rgba(22, 163, 74, 0.12);
	color: var(--crlms-mar-correct, #16a34a);
}

.crlms-mar-review-pill.is-incorrect {
	background: rgba(220, 38, 38, 0.12);
	color: var(--crlms-mar-incorrect, #dc2626);
}

/* Per-cell correctness overlay (decorated by mar-review.php). */

.crlms-mar-runner.is-review .crlms-mar-sheet [data-cell].is-dropzone {
	cursor: default;
	outline-style: solid;
}

.crlms-mar-runner.is-review .crlms-mar-sheet [data-cell].is-dropzone.is-correct {
	background: rgba(22, 163, 74, 0.10);
	outline-color: var(--crlms-mar-correct, #16a34a);
}

.crlms-mar-runner.is-review .crlms-mar-sheet [data-cell].is-dropzone.is-incorrect {
	background: rgba(220, 38, 38, 0.10);
	outline-color: var(--crlms-mar-incorrect, #dc2626);
}

/* Read-only chip — no remove button, no hover affordance. */
.crlms-mar-runner.is-review .crlms-mar-dropped-chip.is-review-chip {
	background: var(--creator-lms-primary-color, #6e42d3);
	padding: 2px 8px;
	cursor: default;
}

.crlms-mar-runner.is-review .crlms-mar-dropped-chip.is-review-chip .crlms-mar-dropped-label {
	max-width: 200px;
}

/* Fallback message when a learner left a question blank. */
.crlms-mar-review-empty {
	padding: 12px 16px;
	background: var(--crlms-mar-surface-muted);
	border: 1px dashed var(--crlms-mar-border);
	border-radius: 6px;
	color: var(--crlms-mar-muted-text);
	font-style: italic;
	font-size: 13px;
}

/* Review fallback for non-MAR question types in the same review page. */
.creator-lms-quiz-review-fallback {
	padding: 8px 12px;
	background: #f6f8fa;
	border-radius: 6px;
	font-size: 13px;
	color: #52525b;
	margin-top: 8px;
}

/* "Reviewing X's attempt" subtitle on the review page header. */
.creator-lms-quiz-review .crlms-mar-review-viewer-note {
	margin: 4px 0 0;
	font-size: 12px;
	font-weight: 500;
	color: var(--crlms-mar-muted-text, #7a8b9a);
	letter-spacing: 0.02em;
}

/* Review-page close button — visually matches `.quiz-page-close` but
   uses its own class so the global JS handler that hijacks
   `.quiz-page-close` (to open the "exit quiz?" modal) doesn't swallow
   the click on the review page, where there's nothing to confirm. */
.creator-lms-page .creator-lms-quiz .creator-lms-quiz-header .crlms-mar-review-close {
	line-height: 1;
	display: block;
	padding: 3px;
	width: 22px;
	margin-left: auto;
	box-shadow: none !important;
	text-decoration: none !important;
}

/* "Review" link on the attempts table. */
.creator-lms-quiz-table .creator-lms-td.review {
	text-align: right;
}
.creator-lms-quiz-attempt-review-link {
	font-size: 13px;
	font-weight: 500;
	color: var(--creator-lms-primary-color, #6e42d3);
	text-decoration: none;
}
.creator-lms-quiz-attempt-review-link:hover {
	text-decoration: underline;
}
