/* ============================================================================
   LaTeX Equations Modal
   Large, professional equation builder with live preview
   ========================================================================== */

.latex-equations-overlay {
	position: fixed;
	inset: 0;
	background: rgba(var(--theme-bg-primary-rgb), 0.7);
	backdrop-filter: blur(8px);
	display: none;
	align-items: flex-start;
	justify-content: center;
	z-index: 32000;
	padding: 0.5rem;
}

body.latex-equations-open {
	overflow: hidden;
}

.latex-equations-overlay.active {
	display: flex;
}

.latex-equations-modal {
	background: var(--theme-bg-primary);
	border: 1px solid var(--theme-border-primary);
	border-radius: 14px;
	width: min(1800px, 98vw);
	min-height: 94vh;
	max-height: 98vh;
	zoom: 0.88;
	color: var(--theme-text-primary);
	box-shadow: var(--theme-shadow-primary);
	display: flex;
	flex-direction: column;
	gap: 1rem;
	padding: 1.25rem;
	/* No sideways scrolling: the closed symbol popovers reach past the right
	   edge, and an open one is moved back inside by positionPopover(). */
	overflow-x: hidden;
	overflow-y: auto;
	position: relative;
	z-index: 32100;
}

.latex-equations-header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 1rem;
}

.latex-equations-title {
	display: flex;
	flex-direction: column;
	gap: 0.35rem;
}

.latex-equations-title h2 {
	margin: 0;
	font-size: 1.4rem;
	letter-spacing: 0.4px;
}

.latex-equations-title p {
	margin: 0;
	color: var(--theme-text-secondary);
	font-size: 0.95rem;
}

.latex-equations-actions {
	display: flex;
	align-items: center;
	gap: 0.5rem;
	flex-wrap: wrap;
	justify-content: flex-end;
}

.latex-equations-chip {
	background: rgba(var(--theme-primary-rgb), 0.12);
	border: 1px solid rgba(var(--theme-primary-rgb), 0.35);
	color: var(--theme-text-accent);
	padding: 0.35rem 0.75rem;
	border-radius: 999px;
	font-size: 0.85rem;
	display: inline-flex;
	align-items: center;
	gap: 0.35rem;
	white-space: nowrap;
}

.latex-equations-mode {
	display: inline-flex;
	border: 1px solid var(--theme-border-primary);
	border-radius: 999px;
	overflow: hidden;
	background: var(--theme-bg-sunken);
}

.latex-equations-mode button {
	background: transparent;
	color: var(--theme-text-primary);
	border: none;
	padding: 0.45rem 0.75rem;
	cursor: pointer;
	font-size: 0.9rem;
	transition: background 0.2s ease, color 0.2s ease;
}

.latex-equations-mode button.active {
	background: var(--theme-bg-secondary);
	color: var(--theme-text-accent);
}

.latex-equations-btn {
	border-radius: 8px;
	border: 1px solid var(--theme-border-primary);
	background: var(--theme-bg-tertiary);
	color: var(--theme-text-primary);
	padding: 0.45rem 0.85rem;
	cursor: pointer;
	display: inline-flex;
	align-items: center;
	gap: 0.4rem;
	font-weight: 600;
	transition: all 0.2s ease;
}

.latex-equations-btn:hover {
	background: var(--theme-bg-secondary);
	border-color: var(--theme-primary);
	color: var(--theme-text-accent);
}

.latex-equations-btn.primary {
	background: var(--theme-primary);
	border-color: var(--theme-primary);
	color: var(--theme-on-primary);
}

.latex-equations-btn.primary:hover {
	filter: brightness(1.05);
}

.latex-equations-close {
	background: transparent;
	color: var(--theme-text-secondary);
	border: none;
	font-size: 1.4rem;
	cursor: pointer;
	width: 36px;
	height: 36px;
	border-radius: 50%;
	display: grid;
	place-items: center;
	transition: background 0.2s ease, color 0.2s ease;
}

.latex-equations-close:hover {
	background: rgba(var(--theme-overlay-rgb), 0.06);
	color: var(--theme-text-primary);
}

.latex-equations-body {
	display: grid;
	grid-template-columns: minmax(0, 1fr) minmax(0, 0.95fr);
	gap: 0.9rem;
	height: 100%;
	min-height: 0;
	align-items: start;
}

.latex-equations-column {
	display: flex;
	flex-direction: column;
	gap: 0.9rem;
	min-width: 0;
	min-height: 0;
	overflow: visible;
}

/* Layers of the dialog: the modal is bg-primary, a card bg-tertiary, a panel
   inside a card bg-primary again and a control bg-secondary. Neighbouring
   layers then differ in every palette, light and dark. */
.latex-equations-card {
	background: var(--theme-bg-tertiary);
	border: 1px solid var(--theme-border-primary);
	border-radius: 12px;
	padding: 0.9rem;
	display: flex;
	flex-direction: column;
	gap: 0.6rem;
	min-width: 0;
	min-height: 0;
}

.latex-equations-card-header {
	display: flex;
	align-items: baseline;
	justify-content: space-between;
	flex-wrap: wrap;
	gap: 0.15rem 0.75rem;
}

.latex-equations-card-title {
	font-weight: 700;
	font-size: 1rem;
	letter-spacing: 0.2px;
}

.latex-equations-card-subtitle {
	color: var(--theme-text-muted);
	font-size: 0.85rem;
}

.latex-equations-input {
	width: 100%;
	min-height: 160px;
	background: var(--theme-bg-primary);
	color: var(--theme-text-primary);
	border: 1px solid var(--theme-border-primary);
	border-radius: 10px;
	padding: 0.75rem;
	font-family: 'JetBrains Mono', 'Fira Code', monospace;
	font-size: 0.95rem;
	resize: vertical;
}

.latex-equations-input:focus {
	outline: none;
	border-color: var(--theme-primary);
	box-shadow: var(--theme-shadow-accent);
}

.latex-equations-quick {
	display: flex;
	flex-wrap: wrap;
	gap: 0.5rem;
}

.latex-equations-pill-btn {
	background: var(--theme-bg-secondary);
	border: 1px solid var(--theme-border-primary);
	border-radius: 999px;
	padding: 0.35rem 0.75rem;
	color: var(--theme-text-primary);
	cursor: pointer;
	font-size: 0.9rem;
	transition: all 0.2s ease;
}

.latex-equations-pill-btn:hover {
	border-color: var(--theme-primary);
	color: var(--theme-text-accent);
}

.latex-equations-symbol-groups {
	display: grid;
	grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
	gap: 0.6rem;
}

.latex-equations-symbol-group {
	border: 1px solid var(--theme-border-primary);
	border-radius: 10px;
	padding: 0.65rem;
	background: var(--theme-bg-primary);
	position: relative;
	overflow: visible;
}

.latex-equations-symbol-group-header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 0.5rem;
	color: var(--theme-text-primary);
	font-weight: 600;
	margin-bottom: 0.5rem;
	font-size: 0.9rem;
}

/* The sign in front of a group name (±, H₂O, Fe) is decoration, not part of the name. */
.latex-equations-symbol-group-icon {
	color: var(--theme-text-muted);
	font-weight: 500;
	margin-right: 0.45em;
}

.latex-equations-symbol-toggle {
	background: var(--theme-bg-secondary);
	border: 1px solid var(--theme-border-primary);
	border-radius: 999px;
	padding: 0.2rem 0.75rem;
	color: var(--theme-text-primary);
	cursor: pointer;
	font-size: 0.85rem;
	transition: all 0.2s ease;
}

.latex-equations-symbol-toggle:hover {
	border-color: var(--theme-primary);
	color: var(--theme-text-accent);
}

.latex-equations-symbols {
	display: grid;
	grid-template-columns: repeat(auto-fit, minmax(34px, 1fr));
	gap: 0.35rem;
}

.latex-equations-symbols.favorites {
	margin-bottom: 0.25rem;
}

.latex-equations-symbols.collapsible {
	position: absolute;
	top: calc(100% + 0.35rem);
	left: 0;
	min-width: 240px;
	max-width: min(420px, calc(100vw - 24px));
	background: var(--theme-bg-primary);
	border: 1px solid var(--theme-border-primary);
	border-radius: 10px;
	padding: 0.55rem;
	box-shadow: var(--theme-shadow-secondary);
	display: grid;
	grid-template-columns: repeat(auto-fit, minmax(38px, 1fr));
	gap: 0.4rem;
	opacity: 0;
	visibility: hidden;
	pointer-events: none;
	transform: translateY(8px) scale(0.98);
	transition:
		opacity 0.12s ease,
		transform 0.12s ease,
		visibility 0.12s ease;
	z-index: 32500;
	max-height: min(260px, calc(100vh - 140px));
	overflow-y: auto;
	overflow-x: auto;
}

.latex-equations-symbols.collapsible.drop-above {
	top: auto;
	bottom: calc(100% + 0.35rem);
	transform-origin: center bottom;
}

.latex-equations-symbol-group:hover .latex-equations-symbols.collapsible,
.latex-equations-symbol-group.expanded .latex-equations-symbols.collapsible {
	opacity: 1;
	visibility: visible;
	pointer-events: auto;
	transform: translateY(0);
}

.latex-equations-symbol-btn {
	background: var(--theme-bg-secondary);
	border: 1px solid var(--theme-border-primary);
	border-radius: 8px;
	color: var(--theme-text-primary);
	min-height: 36px;
	display: grid;
	place-items: center;
	font-size: 0.9rem;
	padding: 0.15rem;
	cursor: pointer;
	white-space: nowrap;
	transition: all 0.15s ease;
}

.latex-symbol-align-mini {
	display: block;
	text-align: center;
	line-height: 1.1;
	font-size: 0.85rem;
}

.latex-equations-symbols.collapsible.drop-above {
	transform: translateY(-8px) scale(0.98);
}

.latex-equations-input-card {
	position: static;
	top: auto;
	z-index: auto;
}

.latex-equations-symbol-btn:hover {
	border-color: var(--theme-primary);
	color: var(--theme-text-accent);
	background: rgba(var(--theme-primary-rgb), 0.08);
}

.latex-equations-preview {
	background: var(--theme-bg-primary);
	border: 1px solid var(--theme-border-primary);
	border-radius: 10px;
	min-height: 200px;
	display: grid;
	grid-template-columns: minmax(0, 1fr);
	place-items: center;
	padding: 1rem;
	position: relative;
}

/* A formula wider than the preview scrolls inside it. */
.latex-equations-preview-output {
	width: 100%;
	display: flex;
	align-items: center;
	justify-content: safe center;
	flex-wrap: wrap;
	gap: 0.5rem;
	text-align: center;
	overflow-x: auto;
	overflow-y: hidden;
	scrollbar-width: thin;
	scrollbar-color: var(--theme-border-primary) transparent;
}

.latex-equations-preview-output .katex-display > .katex > .katex-html {
	padding-right: 3em;
}

.latex-equations-preview-output .katex-display > .katex > .katex-html > .tag {
	right: 0;
}

.latex-equations-load-status {
	position: absolute;
	top: 8px;
	left: 12px;
	color: var(--theme-text-secondary);
	font-size: 0.85rem;
	pointer-events: none;
}

.latex-equations-preview .katex {
	font-size: 1.2rem;
}

.latex-equations-preview-error {
	color: var(--theme-text-error);
	font-size: 0.9rem;
	text-align: center;
}

.latex-equations-status {
	display: inline-flex;
	align-items: center;
	gap: 0.35rem;
	font-size: 0.9rem;
	color: var(--theme-text-secondary);
}

.latex-equations-status.ok {
	color: var(--theme-text-accent);
}

.latex-equations-examples {
	display: grid;
	grid-template-columns: repeat(auto-fit, minmax(min(100%, 250px), 1fr));
	gap: 0.75rem;
}

/* A card is a grid: name and buttons share the first row, the formula takes
   the second. min-width: 0 keeps the card as wide as its grid column; without
   it a long formula widens the card and runs into its neighbour. */
.latex-equations-example {
	background: var(--theme-bg-primary);
	border: 1px solid var(--theme-border-primary);
	border-radius: 10px;
	padding: 0.6rem 0.75rem 0.75rem;
	display: grid;
	grid-template-columns: minmax(0, 1fr) auto;
	grid-template-rows: auto 1fr;
	grid-template-areas:
		'label actions'
		'preview preview';
	align-items: center;
	gap: 0.4rem 0.5rem;
	min-width: 0;
	transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.latex-equations-example:hover {
	border-color: var(--theme-primary);
	box-shadow: var(--theme-shadow-accent);
}

.latex-equations-example-label {
	grid-area: label;
	font-weight: 600;
	font-size: 0.85rem;
	line-height: 1.25;
	color: var(--theme-text-secondary);
	overflow-wrap: break-word;
}

.latex-equations-example > .latex-equations-quick {
	grid-area: actions;
	flex-wrap: nowrap;
	gap: 0.35rem;
}

.latex-equations-example .latex-equations-pill-btn {
	padding: 0.2rem 0.55rem;
	font-size: 0.8rem;
}

/* The formula is centred while it fits and scrolls sideways when it does not.
   The auto margins do the centring, because justify-content: center would
   push the start of a formula that is too wide out of reach. */
.latex-equations-example-preview {
	grid-area: preview;
	min-width: 0;
	min-height: 56px;
	display: flex;
	align-items: center;
	padding: 0.25rem 0.15rem;
	overflow-x: auto;
	overflow-y: hidden;
	scrollbar-width: thin;
	scrollbar-color: var(--theme-border-primary) transparent;
}

/* Two classes, so the rule also beats the margin of KaTeX's .katex-display. */
.latex-equations-example .latex-equations-example-preview > * {
	margin: auto;
}

.latex-equations-example-preview .katex {
	font-size: 1.05em;
}

.latex-equations-badge {
	display: inline-flex;
	align-items: center;
	gap: 0.25rem;
	font-size: 0.8rem;
	color: var(--theme-text-secondary);
}

.latex-equations-footer {
	display: flex;
	justify-content: space-between;
	align-items: center;
	gap: 0.75rem;
	flex-wrap: wrap;
}

.latex-equations-help {
	color: var(--theme-text-secondary);
	font-size: 0.9rem;
}

/* Search bar */
.latex-equations-search-row {
	margin-bottom: 0.4rem;
}

.latex-equations-search {
	width: 100%;
	background: var(--theme-bg-primary);
	color: var(--theme-text-primary);
	border: 1px solid var(--theme-border-primary);
	border-radius: 8px;
	padding: 0.5rem 0.75rem;
	font-size: 0.9rem;
	font-family: inherit;
	transition: border-color 0.2s ease;
}

.latex-equations-search:focus {
	outline: none;
	border-color: var(--theme-primary);
}

.latex-equations-search::placeholder {
	color: var(--theme-text-muted);
}

/* Category tabs */
.latex-equations-category-tabs {
	display: flex;
	flex-wrap: wrap;
	gap: 0.35rem;
	margin-bottom: 0.5rem;
}

.latex-equations-category-tabs .latex-equations-pill-btn.active {
	background: rgba(var(--theme-primary-rgb), 0.15);
	border-color: var(--theme-primary);
	color: var(--theme-text-accent);
}

/* History */
.latex-equations-history-list {
	display: flex;
	flex-direction: column;
	gap: 0.5rem;
	max-height: 200px;
	overflow-y: auto;
	padding-right: 0.25rem;
}

.latex-equations-history-item {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 0.5rem;
	background: var(--theme-bg-primary);
	border: 1px solid var(--theme-border-primary);
	border-radius: 8px;
	padding: 0.5rem 0.75rem;
	transition: border-color 0.2s ease;
}

.latex-equations-history-item:hover {
	border-color: var(--theme-primary);
}

.latex-equations-history-preview {
	flex: 1;
	min-width: 0;
	overflow: hidden;
	font-size: 0.85rem;
}

.latex-equations-history-preview .katex {
	font-size: 0.9rem;
}

.latex-equations-history-actions {
	display: flex;
	gap: 0.35rem;
	flex-shrink: 0;
}

/* Danger pill button */
.latex-equations-pill-btn.danger {
	color: var(--theme-text-error);
}

.latex-equations-pill-btn.danger:hover {
	color: var(--theme-text-error);
	border-color: var(--theme-text-error);
}

/* Favorite active */
.latex-equations-pill-btn.active {
	color: var(--theme-text-accent);
	border-color: rgba(var(--theme-primary-rgb), 0.5);
}

/* One column. The body takes the height of its content and the modal scrolls;
   squeezed into the modal's height, its two rows are laid on top of each other. */
@media (max-width: 1100px) {
	.latex-equations-body {
		grid-template-columns: minmax(0, 1fr);
		height: auto;
		flex-shrink: 0;
	}
}

@media (max-width: 900px) {
	.latex-equations-modal {
		width: 100vw;
		height: 100vh;
		max-height: 100vh;
		padding: 0.9rem;
		border-radius: 0;
	}
	.latex-equations-header {
		flex-direction: column;
		align-items: flex-start;
		gap: 0.6rem;
	}
	.latex-equations-actions {
		justify-content: flex-start;
	}
	.latex-equations-body {
		gap: 0.75rem;
	}
	.latex-equations-symbol-groups {
		grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
	}
	.latex-equations-symbols.collapsible {
		max-width: calc(100vw - 32px);
		max-height: 50vh;
	}
	.latex-equations-input {
		min-height: 140px;
	}
}

@media (max-width: 640px) {
	.latex-equations-modal {
		padding: 0.75rem;
	}
	.latex-equations-card {
		padding: 0.75rem;
	}
	.latex-equations-symbols {
		grid-template-columns: repeat(auto-fit, minmax(30px, 1fr));
	}
	.latex-equations-symbol-btn {
		min-height: 32px;
	}
	.latex-equations-preview {
		min-height: 160px;
	}
}

