/* Teachable Machine trainer modal + internal layout (Assignment 04).
 *
 * The .teachable-modal overlay is Assignment 04's own full-page dialog (its
 * rules mirror .lidar-modal but are kept independent). .tm-sim is the trainer
 * UI mounted inside the modal body. Colors reuse the global theme variables
 * from style.css (--bg, --fg, --border, --surface, --accent, --muted). */

/* ----------------------------------------------------------------------- */
/* Full-page modal overlay                                                  */
/* ----------------------------------------------------------------------- */

.teachable-modal {
	/* Google TM page tokens (shared with .tm-sim) */
	--tm-page: #e8eaed;
	--tm-card: #ffffff;
	--tm-blue: #1967d2;
	--tm-blue-soft: #e8f0fe;
	--tm-blue-soft-hover: #d2e3fc;
	--tm-disabled-bg: #f1f3f4;
	--tm-disabled-fg: #5f6368;
	--tm-wire: #bdc1c6;
	--tm-shadow: 0 2px 2px rgba(0, 0, 0, 0.2);

	position: fixed;
	inset: 0;
	z-index: 1000;
	display: flex;
	align-items: center;
	justify-content: center;
}

.teachable-modal[hidden] {
	display: none;
}

.teachable-modal__backdrop {
	position: absolute;
	inset: 0;
	background: rgba(0, 0, 0, 0.55);
}

.teachable-modal__dialog {
	position: relative;
	z-index: 1;
	width: 100vw;
	height: 100vh;
	max-width: 100vw;
	max-height: 100vh;
	display: flex;
	flex-direction: column;
	background: var(--tm-page);
	color: #202124;
	border: none;
	border-radius: 0;
	box-shadow: none;
	overflow: hidden;
}

.teachable-modal__header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 0.9rem 1.25rem;
	border-bottom: 1px solid #dadce0;
	background: var(--tm-card);
}

.teachable-modal__title {
	font-weight: 700;
}

.teachable-modal__close {
	appearance: none;
	border: none;
	background: transparent;
	color: var(--tm-disabled-fg);
	font-size: 1.5rem;
	line-height: 1;
	cursor: pointer;
}

.teachable-modal__close:hover {
	color: #202124;
}

.teachable-modal__body {
	flex: 1 1 auto;
	min-height: 0;
	padding: 1.25rem;
	overflow: auto;
	display: flex;
	flex-direction: column;
	background: var(--tm-page);
}

.teachable-modal__footer {
	display: flex;
	align-items: center;
	gap: 0.6rem;
	padding: 0.85rem 1.25rem;
	border-top: 1px solid #dadce0;
	background: var(--tm-card);
}

.teachable-modal__spacer {
	flex: 1;
}

.teachable-modal__save {
	appearance: none;
	border: 1px solid var(--accent);
	background: var(--accent);
	color: #fff;
	font: inherit;
	font-weight: 600;
	padding: 0.5rem 0.9rem;
	border-radius: 8px;
	cursor: pointer;
}

.teachable-modal__save:hover {
	filter: brightness(0.97);
}

body.teachable-modal-open {
	overflow: hidden;
}

/* ----------------------------------------------------------------------- */
/* Trainer layout                                                           */
/* ----------------------------------------------------------------------- */

.tm-sim {
	--tm-page: #e8eaed;
	--tm-card: #ffffff;
	--tm-blue: #1967d2;
	--tm-blue-soft: #e8f0fe;
	--tm-blue-soft-hover: #d2e3fc;
	--tm-disabled-bg: #f1f3f4;
	--tm-disabled-fg: #5f6368;
	--tm-wire: #bdc1c6;
	--tm-shadow: 0 2px 2px rgba(0, 0, 0, 0.2);

	flex: 1 1 auto;
	min-height: 0;
	display: flex;
	flex-direction: column;
}

.tm-sim__error {
	color: #b91c1c;
	font-weight: 600;
}

/* Three-column hub: Classes | Training | Preview */
.tm-sim__layout {
	position: relative;
	flex: 1 1 auto;
	min-height: 0;
	display: grid;
	grid-template-columns: minmax(240px, 1fr) minmax(200px, 240px) minmax(260px, 1fr);
	gap: 1.5rem;
	align-items: start;
	overflow: hidden;
}

.tm-sim__wires {
	position: absolute;
	inset: 0;
	width: 100%;
	height: 100%;
	pointer-events: none;
	z-index: 0;
	overflow: hidden;
}

.tm-sim__wires path {
	fill: none;
	stroke: var(--tm-wire);
	stroke-width: 2;
}

.tm-sim__classes,
.tm-sim__train-col,
.tm-sim__preview-col {
	position: relative;
	z-index: 1;
	min-width: 0;
	min-height: 0;
}

.tm-sim__classes {
	display: flex;
	flex-direction: column;
	gap: 0.85rem;
	overflow: auto;
	max-height: 100%;
	padding: 0.15rem 0.25rem 0.15rem 0;
}

.tm-sim__train-col {
	display: flex;
	flex-direction: column;
	align-items: stretch;
	justify-content: center;
	align-self: stretch;
	position: sticky;
	top: 0;
	max-height: 100%;
}

.tm-sim__preview-col {
	display: flex;
	flex-direction: column;
	gap: 1rem;
	overflow: auto;
	max-height: 100%;
	/* Room for .tm-panel box-shadow (overflow would otherwise clip it) */
	padding: 0.15rem 0.25rem 0.25rem 0.15rem;
}

.tm-sim__preview-col > .tm-panel {
	box-shadow: var(--tm-shadow);
}

.tm-panel {
	border: none;
	border-radius: 10px;
	background: var(--tm-card);
	box-shadow: var(--tm-shadow);
	padding: 1rem 1.1rem;
}

.tm-panel__title {
	margin: 0 0 0.75rem;
	padding-bottom: 0.55rem;
	border-bottom: 1px solid #e8eaed;
	font-size: 1.1rem;
	font-weight: 700;
	color: #202124;
}

.tm-panel__subtitle {
	margin: 1rem 0 0.5rem;
	font-size: 0.9rem;
	font-weight: 600;
	color: #5f6368;
}

/* Class cards (Google TM-style white shadowed drawers) */
.tm-class {
	border: none;
	border-radius: 10px;
	background: var(--tm-card);
	box-shadow: var(--tm-shadow);
	padding: 0.85rem;
	min-width: 0;
	max-width: 100%;
	box-sizing: border-box;
}

.tm-class__head {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 0.5rem;
	padding: 0 0 0.65rem;
	margin: 0 0 0.6rem;
	border-bottom: 1px solid #e8eaed;
}

.tm-class__name {
	flex: 1;
	font: inherit;
	font-size: 1.15rem;
	font-weight: 700;
	border: 1px solid transparent;
	background: transparent;
	color: #202124;
	padding: 0.15rem 0.25rem;
	border-radius: 4px;
}

.tm-class__name:hover,
.tm-class__name:focus {
	border-color: #dadce0;
	background: #f8f9fa;
	outline: none;
}

.tm-class__count {
	color: var(--muted);
	font-size: 0.8rem;
	white-space: nowrap;
	padding-right: 0.25rem;
}

.tm-class__samples {
	display: flex;
	flex-wrap: wrap;
	gap: 4px;
	min-height: 0;
	max-height: 140px;
	overflow-y: auto;
	margin: 0 0 0.6rem;
	padding: 0;
}

.tm-class__samples:empty {
	display: none;
	padding: 0;
}

.tm-class__sample {
	position: relative;
	width: 58px;
	height: 58px;
	border-radius: 6px;
	overflow: hidden;
}

.tm-class__sample img {
	width: 100%;
	height: 100%;
	object-fit: cover;
	display: block;
}

.tm-class__sample-del {
	position: absolute;
	top: 1px;
	right: 1px;
	width: 18px;
	height: 18px;
	border: none;
	border-radius: 50%;
	background: rgba(0, 0, 0, 0.6);
	color: #fff;
	font-size: 0.85rem;
	line-height: 1;
	cursor: pointer;
	padding: 0;
}

.tm-class__more {
	align-self: center;
	color: var(--muted);
	font-size: 0.8rem;
}

.tm-class__add-label {
	display: block;
	margin: 0 0 0.35rem;
	padding: 0;
	font-size: 0.85rem;
	font-weight: 500;
	color: #5f6368;
}

.tm-class__sources {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	gap: 0.5rem;
	min-width: 0;
}

.tm-class__cam {
	box-sizing: border-box;
	margin: 0 0 0.6rem;
	padding: 0;
	min-width: 0;
	width: 100%;
}

.tm-class__cam[hidden] {
	display: none !important;
}

.tm-class__cam canvas,
.tm-class__cam video {
	box-sizing: border-box;
	width: 100%;
	max-width: 240px;
	height: auto !important;
	border-radius: 8px;
	display: block;
	margin: 0 0 0.4rem;
}

.tm-class__cam-actions {
	display: flex;
	flex-wrap: wrap;
	gap: 0.4rem;
	min-width: 0;
}

/* Buttons */
.tm-btn {
	appearance: none;
	border: 1px solid var(--border);
	background: var(--bg);
	color: var(--fg);
	font: inherit;
	font-weight: 600;
	padding: 0.4rem 0.75rem;
	border-radius: 8px;
	cursor: pointer;
}

.tm-btn:hover {
	filter: brightness(0.97);
}

.tm-btn:disabled {
	cursor: default;
}

.tm-btn--primary {
	display: block;
	width: 100%;
	border: 2px solid transparent;
	border-radius: 4px;
	background: var(--tm-blue);
	color: #fff;
	font-size: 14px;
	font-weight: 500;
	padding: 7px 16px;
}

.tm-btn--primary:hover:not(:disabled) {
	filter: brightness(1.05);
}

.tm-btn--primary:disabled {
	background: var(--tm-disabled-bg);
	border-color: transparent;
	color: var(--tm-disabled-fg);
	opacity: 1;
}

.tm-btn--ghost {
	background: transparent;
	color: var(--tm-disabled-fg);
	border-color: transparent;
}

.tm-btn--record {
	user-select: none;
	font-size: 13px;
	font-weight: 500;
	border: 1px solid #dadce0;
	border-radius: 8px;
	background: #fff;
	color: #3c4043;
	padding: 0.45rem 0.85rem;
	box-shadow: none;
}

.tm-btn--record:hover:not(:disabled) {
	border-color: #bdc1c6;
	background: #f8f9fa;
	filter: none;
}

.tm-preview .tm-btn--record {
	align-self: center;
}

/* Google TM sample-source tiles (Webcam / Upload) */
.tm-btn--source {
	display: inline-flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	gap: 2px;
	min-width: 72px;
	min-height: 58px;
	padding: 1px 6px;
	border: 2px solid transparent;
	border-radius: 4px;
	background: var(--tm-blue-soft);
	color: var(--tm-blue);
	font-size: 12px;
	font-weight: 500;
	line-height: 1.2;
}

.tm-btn--source:hover {
	background: var(--tm-blue-soft-hover);
	filter: none;
}

.tm-btn--source .tm-btn__icon {
	display: block;
	width: 24px;
	height: 24px;
	flex-shrink: 0;
}

.tm-btn--source .tm-btn__label {
	display: block;
	text-align: center;
}

/* Advanced training settings */
.tm-advanced {
	margin: 0.85rem 0 0;
	border: none;
	padding: 0;
}

.tm-advanced summary {
	cursor: pointer;
	font-weight: 500;
	font-size: 0.9rem;
	color: var(--tm-blue);
	list-style: none;
}

.tm-advanced summary::-webkit-details-marker {
	display: none;
}

.tm-advanced__reset.tm-btn--ghost {
	display: block;
	width: 100%;
	margin-top: 0.85rem;
	padding-top: 0.75rem;
	border: none;
	border-top: 1px solid #e8eaed;
	border-radius: 0;
	text-align: left;
}

.tm-advanced__row {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 0.75rem;
	margin-top: 0.65rem;
	font-size: 0.85rem;
	font-weight: 500;
	color: #5f6368;
}

/* Custom blue steppers (native spin buttons can't be recolored reliably) */
.tm-stepper {
	display: inline-flex;
	align-items: stretch;
	width: 8rem;
	border: 1px solid #dadce0;
	border-radius: 8px;
	background: #fff;
	overflow: hidden;
	transition: border-color 0.12s ease, box-shadow 0.12s ease;
}

.tm-stepper:hover {
	border-color: #aecbfa;
}

.tm-stepper:focus-within {
	border-color: var(--tm-blue);
	box-shadow: 0 0 0 3px rgba(25, 103, 210, 0.18);
}

.tm-stepper__input {
	box-sizing: border-box;
	flex: 1 1 auto;
	min-width: 0;
	width: auto;
	font: inherit;
	font-variant-numeric: tabular-nums;
	padding: 0.45rem 0.5rem;
	border: none;
	border-radius: 0;
	background: transparent;
	color: #202124;
	text-align: right;
	appearance: textfield;
	-moz-appearance: textfield;
}

.tm-stepper__input::-webkit-outer-spin-button,
.tm-stepper__input::-webkit-inner-spin-button {
	-webkit-appearance: none;
	margin: 0;
}

.tm-stepper__input:focus {
	outline: none;
}

.tm-stepper__btns {
	display: flex;
	flex-direction: column;
	flex: 0 0 1.55rem;
	border-left: 1px solid #d2e3fc;
	background: var(--tm-blue-soft);
}

.tm-stepper__btn {
	appearance: none;
	flex: 1 1 50%;
	margin: 0;
	padding: 0;
	border: none;
	background: transparent;
	color: var(--tm-blue);
	font-size: 0.55rem;
	line-height: 1;
	cursor: pointer;
}

.tm-stepper__btn + .tm-stepper__btn {
	border-top: 1px solid #d2e3fc;
}

.tm-stepper__btn:hover {
	background: var(--tm-blue-soft-hover);
}

.tm-stepper__btn:active {
	background: #aecbfa;
}

.tm-training__status {
	margin: 0.75rem 0 0;
	color: #5f6368;
	font-size: 0.85rem;
	line-height: 1.35;
}

/* Preview */
.tm-preview__controls {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	gap: 0.5rem;
	margin-bottom: 0.75rem;
}

.tm-preview__stage {
	min-height: 140px;
	display: flex;
	align-items: center;
	justify-content: center;
	background: var(--tm-disabled-bg);
	border: none;
	border-radius: 8px;
	margin-bottom: 0.75rem;
}

.tm-preview__stage canvas,
.tm-preview__stage img {
	max-width: 220px;
	max-height: 220px;
	border-radius: 8px;
}

.tm-bars {
	display: flex;
	flex-direction: column;
	gap: 0.45rem;
}

.tm-bar {
	display: grid;
	grid-template-columns: 5.5rem 1fr 2.75rem;
	align-items: center;
	gap: 0.5rem;
}

.tm-bar__label {
	font-size: 0.85rem;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
	color: #202124;
}

.tm-bar__track {
	height: 14px;
	background: var(--tm-page);
	border: none;
	border-radius: 7px;
	overflow: hidden;
}

.tm-bar__fill {
	height: 100%;
	background: var(--tm-blue);
	width: 0;
	border-radius: 7px;
}

.tm-bar__pct {
	font-size: 0.8rem;
	text-align: right;
	color: #5f6368;
}

.tm-recorded {
	display: flex;
	flex-wrap: wrap;
	gap: 0.5rem;
}

.tm-recorded__item {
	position: relative;
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: 0.25rem;
	border: 1px solid #e8eaed;
	border-radius: 8px;
	background: #fff;
	padding: 0.35rem;
	width: 80px;
	box-shadow: var(--tm-shadow);
}

.tm-recorded__item img {
	width: 58px;
	height: 58px;
	object-fit: cover;
	border-radius: 6px;
}

.tm-recorded__label {
	font-size: 0.75rem;
	text-align: center;
}

.tm-recorded__intended {
	font-size: 0.65rem;
	color: #5f6368;
	text-align: center;
}

/* [hidden] must win over display:flex rules below */
.tm-eval[hidden],
.tm-recorded[hidden],
.tm-panel__subtitle[hidden],
.tm-btn--record[hidden],
.tm-intended[hidden],
.tm-reeval[hidden],
.tm-q7-slots[hidden] {
	display: none !important;
}

.tm-q7-slots {
	display: block;
}

.tm-q7-slots__row {
	display: flex;
	flex-wrap: wrap;
	gap: 0.45rem;
}

.tm-intended {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	gap: 0.4rem;
	margin: 0.5rem 0 0.25rem;
}

.tm-intended__label {
	font-size: 0.8rem;
	font-weight: 600;
	color: #3c4043;
	margin-right: 0.15rem;
}

.tm-intended__chip {
	border: 1px solid #dadce0;
	border-radius: 16px;
	background: #fff;
	padding: 0.25rem 0.7rem;
	font: inherit;
	font-size: 0.8rem;
	cursor: pointer;
	color: #3c4043;
}

.tm-intended__chip:hover {
	border-color: #bdc1c6;
}

.tm-intended__chip--selected {
	border-color: var(--tm-blue);
	background: #e8f0fe;
	color: #174ea6;
	font-weight: 600;
}

.tm-reeval {
	display: flex;
	flex-direction: column;
	gap: 0.65rem;
	margin-top: 0.35rem;
}

.tm-reeval__meta {
	font-size: 0.85rem;
	color: #3c4043;
}

.tm-reeval__cards {
	display: flex;
	flex-wrap: wrap;
	gap: 0.65rem;
}

.tm-reeval__card {
	display: flex;
	flex-direction: column;
	gap: 0.3rem;
	width: 140px;
	padding: 0.45rem;
	border: 1px solid #e8eaed;
	border-radius: 8px;
	background: #fff;
	box-shadow: var(--tm-shadow);
	font-size: 0.72rem;
	color: #5f6368;
}

.tm-reeval__card img {
	width: 100%;
	height: 90px;
	object-fit: cover;
	border-radius: 6px;
}

.tm-reeval__empty {
	font-size: 0.85rem;
	color: #5f6368;
}

/* Q5 intended-class eval buckets (2 success + 1 fail per class) */
.tm-eval {
	display: flex;
	flex-direction: column;
	gap: 0.65rem;
}

.tm-eval__row {
	display: flex;
	flex-direction: column;
	gap: 0.35rem;
}

.tm-eval__class {
	font-size: 0.8rem;
	font-weight: 600;
	color: #3c4043;
}

.tm-eval__slots {
	display: flex;
	flex-wrap: wrap;
	gap: 0.45rem;
}

.tm-eval__slot {
	position: relative;
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: 0.2rem;
	width: 92px;
	min-height: 108px;
	padding: 0.4rem 0.35rem 0.45rem;
	border: 1px dashed #dadce0;
	border-radius: 8px;
	background: #fff;
	cursor: pointer;
	font: inherit;
	color: inherit;
	box-shadow: none;
}

.tm-eval__slot:hover {
	border-color: #bdc1c6;
}

.tm-eval__slot--selected {
	border-style: solid;
	border-color: var(--tm-blue);
	box-shadow: 0 0 0 1px var(--tm-blue);
}

.tm-eval__slot--filled {
	border-style: solid;
	border-color: #e8eaed;
	box-shadow: var(--tm-shadow);
}

.tm-eval__slot img {
	width: 54px;
	height: 54px;
	object-fit: cover;
	border-radius: 6px;
}

.tm-eval__slot-kind {
	font-size: 0.68rem;
	font-weight: 600;
	color: #5f6368;
	text-transform: uppercase;
	letter-spacing: 0.02em;
}

.tm-eval__slot-empty {
	font-size: 0.72rem;
	color: #9aa0a6;
	padding: 1.1rem 0;
}

.tm-eval__slot-meta {
	font-size: 0.65rem;
	color: #5f6368;
	text-align: center;
	line-height: 1.2;
	max-width: 100%;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.tm-eval__slot .tm-class__sample-del {
	position: absolute;
	top: 2px;
	right: 2px;
}

/* Confusion matrix */
.tm-cm {
	margin-top: 0.75rem;
	overflow: auto;
}

.tm-cm__empty {
	color: var(--muted);
	font-size: 0.9rem;
}

.tm-cm__table {
	border-collapse: collapse;
	font-size: 0.9rem;
}

.tm-cm__table th,
.tm-cm__table td {
	border: 1px solid var(--border);
	padding: 0.4rem 0.7rem;
	text-align: center;
}

.tm-cm__table th {
	background: var(--bg);
	color: var(--muted);
	font-weight: 600;
}

.tm-cm__diag {
	background: color-mix(in srgb, var(--accent) 18%, transparent);
	font-weight: 700;
}

@media (max-width: 900px) {
	.tm-sim__layout {
		grid-template-columns: 1fr;
		overflow: auto;
	}

	.tm-sim__wires {
		display: none;
	}

	.tm-sim__train-col {
		position: static;
		justify-content: flex-start;
	}
}
