/* ============================================================================
   Chemistry Editor Modal
   Extends latex-equations.css via dual-class approach:
   overlay has class="latex-equations-overlay latex-chemistry-overlay"
   All shared structural styles are inherited from latex-equations.css.
   Only chemistry-specific overrides live here.
   ========================================================================== */

/* Prevent grid columns from expanding beyond their fr allocation when KaTeX renders.
   Without min-width:0, rendered math content forces the right column wider,
   squeezing the left column (symbol groups) and causing overflow. */
.latex-chemistry-overlay .latex-equations-column {
	min-width: 0;
	overflow: hidden;
}

/* Both tabs: symbol-only display with ellipsis for overflow */
.latex-chemistry-overlay .latex-equations-symbol-btn {
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

/* Structures tab only: extra-compact symbol buttons */
.latex-chemistry-structures-body .latex-equations-symbol-btn {
	min-width: 0;
	min-height: 30px;
	padding: 4px 6px;
	font-size: 0.78rem;
}

/* Structures tab only: dense grids — scrollable favorites so nothing clips silently */
.latex-chemistry-structures-body .latex-equations-symbols.favorites {
	grid-template-columns: repeat(auto-fit, minmax(44px, 1fr));
	gap: 3px;
	overflow-x: auto;
	overflow-y: hidden;
	scrollbar-width: none;
}
.latex-chemistry-structures-body .latex-equations-symbols.favorites::-webkit-scrollbar {
	display: none;
}

.latex-chemistry-structures-body .latex-equations-symbols.collapsible {
	min-width: 240px;
	max-width: min(480px, calc(100vw - 24px));
	grid-template-columns: repeat(auto-fit, minmax(40px, 1fr));
	gap: 3px;
}

/* ─── Tab Bar ─────────────────────────────────────────────────────────── */

.latex-chemistry-tab-bar {
	display: flex;
	gap: 4px;
	padding: 0 16px 8px;
	border-bottom: 1px solid rgba(255, 255, 255, 0.08);
	margin-bottom: 0;
}

.latex-chemistry-tab {
	padding: 8px 20px;
	border: none;
	border-bottom: 2px solid transparent;
	background: transparent;
	color: rgba(255, 255, 255, 0.5);
	font-size: 0.9rem;
	font-weight: 500;
	cursor: pointer;
	transition: color 0.15s, border-color 0.15s;
	border-radius: 6px 6px 0 0;
}

.latex-chemistry-tab:hover {
	color: rgba(255, 255, 255, 0.8);
	background: rgba(255, 255, 255, 0.04);
}

.latex-chemistry-tab.active {
	color: #fff;
	border-bottom-color: #7c6fe0;
	background: rgba(124, 111, 224, 0.08);
}

/* ─── Source Tab Switcher ────────────────────────────────────────────── */

.latex-chemistry-source-tabs {
	display: inline-flex;
	border: 1px solid #343434;
	border-radius: 999px;
	overflow: hidden;
	background: #151515;
	margin-bottom: 0.4rem;
}

.latex-chemistry-source-tabs button {
	background: transparent;
	color: #d0d0d0;
	border: none;
	padding: 0.35rem 0.7rem;
	cursor: pointer;
	font-size: 0.85rem;
	transition: background 0.2s ease, color 0.2s ease;
}

.latex-chemistry-source-tabs button.active {
	background: #2f2f2f;
	color: #7c6fe0;
}

/* ─── Preview Mode Switcher ─────────────────────────────────────────── */

.latex-chemistry-preview-modes {
	display: inline-flex;
	border: 1px solid #343434;
	border-radius: 999px;
	overflow: hidden;
	background: #151515;
	margin-bottom: 0.5rem;
}

.latex-chemistry-preview-modes button {
	background: transparent;
	color: #d0d0d0;
	border: none;
	padding: 0.35rem 0.7rem;
	cursor: pointer;
	font-size: 0.82rem;
	transition: background 0.2s ease, color 0.2s ease;
}

.latex-chemistry-preview-modes button.active {
	background: #2f2f2f;
	color: #7c6fe0;
}

/* ─── Preview Viewport ──────────────────────────────────────────────── */

.latex-chemistry-preview-viewport {
	position: relative;
	min-height: 280px;
}

.latex-chemistry-preview-viewport > div {
	display: none;
}

.latex-chemistry-preview-viewport > div.active {
	display: flex;
}

/* ─── Search Bar ──────────────────────────────────────────────────────── */

.latex-chemistry-search-bar {
	padding: 0;
}

.latex-chemistry-search-input {
	width: 100%;
	padding: 8px 12px;
	border: 1px solid rgba(255, 255, 255, 0.1);
	border-radius: 8px;
	background: rgba(255, 255, 255, 0.04);
	color: #e0e0e0;
	font-size: 0.85rem;
	outline: none;
	transition: border-color 0.15s, background 0.15s;
	box-sizing: border-box;
}

.latex-chemistry-search-input:focus {
	border-color: rgba(124, 111, 224, 0.5);
	background: rgba(255, 255, 255, 0.06);
}

.latex-chemistry-search-input::placeholder {
	color: rgba(255, 255, 255, 0.3);
}

/* ─── Category Pills ─────────────────────────────────────────────────── */

.latex-chemistry-category-bar {
	display: flex;
	gap: 4px;
	overflow-x: auto;
	padding: 2px 0;
	scrollbar-width: thin;
	scrollbar-color: rgba(255, 255, 255, 0.1) transparent;
	flex-wrap: wrap;
}

.latex-chemistry-category-bar::-webkit-scrollbar {
	height: 4px;
}

.latex-chemistry-category-bar::-webkit-scrollbar-thumb {
	background: rgba(255, 255, 255, 0.1);
	border-radius: 2px;
}

.latex-chemistry-category-pill {
	padding: 4px 10px;
	border: 1px solid rgba(255, 255, 255, 0.08);
	border-radius: 12px;
	background: transparent;
	color: rgba(255, 255, 255, 0.45);
	font-size: 0.72rem;
	cursor: pointer;
	white-space: nowrap;
	transition: all 0.15s;
	flex-shrink: 0;
}

.latex-chemistry-category-pill:hover {
	color: rgba(255, 255, 255, 0.7);
	border-color: rgba(255, 255, 255, 0.15);
	background: rgba(255, 255, 255, 0.04);
}

.latex-chemistry-category-pill.active {
	color: #fff;
	border-color: #7c6fe0;
	background: rgba(124, 111, 224, 0.15);
}

/* ─── Molecule Card Grid ─────────────────────────────────────────────── */

.latex-chemistry-molecule-grid {
	display: grid;
	grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
	gap: 6px;
	overflow-y: auto;
	max-height: clamp(180px, calc(100vh - 520px), 400px);
	padding: 2px;
	scrollbar-width: thin;
	scrollbar-color: rgba(255, 255, 255, 0.1) transparent;
}

.latex-chemistry-molecule-grid::-webkit-scrollbar {
	width: 4px;
}

.latex-chemistry-molecule-grid::-webkit-scrollbar-thumb {
	background: rgba(255, 255, 255, 0.1);
	border-radius: 2px;
}

.latex-chemistry-molecule-card {
	display: flex;
	flex-direction: column;
	align-items: center;
	padding: 6px;
	border: 1px solid rgba(255, 255, 255, 0.06);
	border-radius: 8px;
	background: rgba(255, 255, 255, 0.02);
	cursor: pointer;
	transition: all 0.15s;
	min-height: 90px;
}

.latex-chemistry-molecule-card:hover {
	border-color: rgba(124, 111, 224, 0.3);
	background: rgba(124, 111, 224, 0.06);
	transform: translateY(-1px);
}

.latex-chemistry-molecule-card.selected {
	border-color: #7c6fe0;
	background: rgba(124, 111, 224, 0.12);
	box-shadow: 0 0 8px rgba(124, 111, 224, 0.2);
}

.latex-chemistry-molecule-thumb {
	width: 80px;
	height: 60px;
	display: flex;
	align-items: center;
	justify-content: center;
	overflow: hidden;
}

.latex-chemistry-molecule-thumb svg {
	max-width: 100%;
	max-height: 100%;
}

.latex-chemistry-molecule-info {
	text-align: center;
	width: 100%;
	overflow: hidden;
}

.latex-chemistry-molecule-name {
	font-size: 0.7rem;
	font-weight: 500;
	color: rgba(255, 255, 255, 0.85);
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}

.latex-chemistry-molecule-desc {
	font-size: 0.6rem;
	color: rgba(255, 255, 255, 0.35);
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}

/* ─── SMILES Preview ─────────────────────────────────────────────────── */

.latex-chemistry-smiles-preview {
	min-height: 220px;
	background: #1e1e2e;
	border-radius: 8px;
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 12px;
}

.latex-chemistry-smiles-preview svg {
	max-width: 100%;
	max-height: 220px;
}

.latex-chemistry-chemfig-input {
	font-family: 'Fira Code', 'JetBrains Mono', 'Cascadia Code', monospace;
	font-size: 0.85rem;
}

.latex-chemistry-jsme-container {
	min-height: 320px;
	display: flex;
	align-items: center;
	justify-content: center;
	background: #1a1a2e;
	border-radius: 8px;
	overflow: hidden;
}

.latex-chemistry-jsme-container iframe,
.latex-chemistry-jsme-container > div {
	border: none;
	filter: invert(0.85) hue-rotate(180deg) saturate(1.2) contrast(0.95);
}

.latex-chemistry-minicompile-preview {
	flex: 1;
	min-height: 160px;
	background: #1e1e2e;
	border-radius: 8px;
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 12px;
}

.latex-chemistry-minicompile-preview embed {
	background: white;
	border-radius: 4px;
}

/* ─── Export Panel ────────────────────────────────────────────────────── */

.latex-chemistry-export-card {
	display: flex;
	flex-wrap: wrap;
	gap: 6px;
}

.latex-chemistry-export-btn {
	padding: 6px 12px;
	border: 1px solid rgba(255, 255, 255, 0.1);
	border-radius: 6px;
	background: rgba(255, 255, 255, 0.04);
	color: rgba(255, 255, 255, 0.7);
	font-size: 0.78rem;
	cursor: pointer;
	transition: all 0.15s;
}

.latex-chemistry-export-btn:hover {
	border-color: rgba(124, 111, 224, 0.4);
	background: rgba(124, 111, 224, 0.1);
	color: #fff;
}

/* ─── Export Panel Collapsible ────────────────────────────────────────── */

.latex-chemistry-export-panel-wrap {
	display: none;
	padding-top: 6px;
	border-top: 1px solid rgba(255, 255, 255, 0.06);
	margin-top: 6px;
}

.latex-chemistry-export-panel-wrap.visible {
	display: block;
}

/* ─── Settings Panel ─────────────────────────────────────────────────── */

.latex-chemistry-settings-panel {
	display: none;
	flex-direction: column;
	gap: 8px;
	padding: 8px 0;
}

.latex-chemistry-settings-panel.visible {
	display: flex;
}

.latex-chemistry-setting-row {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 8px;
}

.latex-chemistry-setting-label {
	font-size: 0.78rem;
	color: rgba(255, 255, 255, 0.6);
	white-space: nowrap;
}

.latex-chemistry-setting-row select,
.latex-chemistry-setting-row input[type="range"] {
	accent-color: #7c6fe0;
}

.latex-chemistry-setting-row select {
	padding: 4px 8px;
	border: 1px solid rgba(255, 255, 255, 0.1);
	border-radius: 4px;
	background: rgba(255, 255, 255, 0.04);
	color: #e0e0e0;
	font-size: 0.78rem;
}

.latex-chemistry-setting-row input[type="range"] {
	width: 100px;
}

.latex-chemistry-setting-row input[type="checkbox"] {
	accent-color: #7c6fe0;
	width: 14px;
	height: 14px;
}

/* ─── Responsive: Chemistry Tab ────────────────────────────────────── */

@media (max-width: 1100px) {
	.latex-chemistry-molecule-grid {
		grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
		max-height: 300px;
	}
	.latex-chemistry-preview-viewport {
		min-height: 220px;
	}
	.latex-chemistry-jsme-container {
		min-height: 260px;
	}
}

@media (max-width: 900px) {
	.latex-chemistry-tab-bar {
		padding: 0 8px 6px;
	}
	.latex-chemistry-tab {
		padding: 6px 14px;
		font-size: 0.85rem;
	}
	.latex-chemistry-molecule-grid {
		grid-template-columns: repeat(auto-fill, minmax(70px, 1fr));
		max-height: 250px;
	}
	.latex-chemistry-molecule-card {
		min-height: 80px;
		padding: 4px;
	}
	.latex-chemistry-molecule-thumb {
		width: 60px;
		height: 45px;
	}
	.latex-chemistry-jsme-container {
		min-height: 240px;
	}
	.latex-chemistry-category-bar {
		gap: 3px;
	}
	.latex-chemistry-category-pill {
		font-size: 0.65rem;
		padding: 3px 8px;
	}
}

@media (max-width: 640px) {
	.latex-chemistry-tab {
		padding: 5px 10px;
		font-size: 0.8rem;
	}
	.latex-chemistry-molecule-grid {
		grid-template-columns: repeat(auto-fill, minmax(60px, 1fr));
		max-height: 200px;
		gap: 4px;
	}
	.latex-chemistry-molecule-card {
		min-height: 70px;
	}
	.latex-chemistry-molecule-thumb {
		width: 50px;
		height: 40px;
	}
	.latex-chemistry-molecule-name {
		font-size: 0.6rem;
	}
	.latex-chemistry-molecule-desc {
		display: none;
	}
	.latex-chemistry-preview-viewport {
		min-height: 180px;
	}
	.latex-chemistry-jsme-container {
		min-height: 200px;
	}
	.latex-chemistry-source-tabs button,
	.latex-chemistry-preview-modes button {
		padding: 0.25rem 0.5rem;
		font-size: 0.75rem;
	}
	.latex-chemistry-structures-body .latex-equations-symbol-btn {
		min-height: 26px;
		padding: 3px 4px;
		font-size: 0.7rem;
	}
}
