:root {
	--bg-primary: #020617;
	--bg-secondary: #0f172a;
	--panel: rgba(15, 23, 42, 0.8);
	--panel-border: rgba(148, 163, 184, 0.15);
	--accent: #0ea5e9;
	--accent-2: #f97316;
	--text-muted: #94a3b8;
	--radius: 18px;
	--blur: 32px;
}

* { box-sizing: border-box; }

body {
	margin: 0;
	min-height: 100vh;
	font-family: 'Space Grotesk', 'Helvetica Neue', sans-serif;
	background: radial-gradient(circle at 20% 20%, rgba(14,165,233,0.15), transparent 55%),
							radial-gradient(circle at 80% 0%, rgba(248,113,113,0.12), transparent 60%),
							var(--bg-primary);
	color: #e2e8f0;
	position: relative;
	overflow-x: hidden;
}

body::after {
	content: '';
	position: fixed;
	inset: 0;
	background: radial-gradient(circle, rgba(14,165,233,0.12) 0%, transparent 50%);
	pointer-events: none;
	opacity: 0.8;
	animation: pulseBackdrop 12s ease-in-out infinite;
}

header {
	text-align: center;
	padding: 2.5rem 1.5rem 1.5rem;
}

header h1 {
	margin: 0.25rem;
	font-size: clamp(1.9rem, 4vw, 3rem);
	letter-spacing: -0.02em;
}

.tagline {
	text-transform: uppercase;
	letter-spacing: 0.35em;
	color: var(--text-muted);
	font-size: 0.75rem;
	margin: 0;
}

.subline {
	margin: 0 auto;
	max-width: 560px;
	color: var(--text-muted);
	font-size: 1rem;
}

/* Medium to wide: keep tagline above the H1, place subtitle to the right of the H1 */
@media (min-width: 900px) {
	header {
 		display: grid;
 		grid-template-columns: 1fr minmax(240px, 420px);
 		grid-template-areas: "tagline tagline" "title subline";
 		gap: 0.1rem;
 		align-items: center;
 		text-align: left;
 	}

 	.tagline {
 		grid-area: tagline;
 		margin: 0 0 0.25rem 0;
 		justify-self: start;
 		font-size: 0.9rem;
 		letter-spacing: 0.35em;
 	}

 	header h1 {
 		grid-area: title;
 		margin: 0;
 		justify-self: start;
 	}

 	.subline {
 		grid-area: subline;
 		margin: 0;
 		justify-self: end;
 		align-self: center;
 		max-width: none;
 		white-space: normal;
 	}
}

.layout {
	display: grid;
	grid-template-columns: minmax(260px, 320px) 1fr;
	gap: 0.1em;
	padding: 0 1.5rem 2rem;
	max-width: 1400px;
	margin: 0 auto;
	position: relative;
	z-index: 1;
}

.controls,
.simulation-wrapper,
.graph-wrapper {
	background: var(--panel);
	border: 1px solid var(--panel-border);
	border-radius: var(--radius);
	backdrop-filter: blur(var(--blur));
	box-shadow: 0 20px 60px rgba(15, 23, 42, 0.5);
}

.controls {
	padding: 1.25rem;
	display: flex;
	flex-direction: column;
	gap: 0.1rem;
}

.input-group label {
	display: flex;
	justify-content: space-between;
	font-weight: 500;
	margin-bottom: 0.6rem;
	color: #f8fafc;
}

.input-grid {
	display: grid;
	gap: 1rem;
	grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
}

input[type="range"] {
	-webkit-appearance: none;
	width: 100%;
	height: 6px;
	border-radius: 999px;
	background: rgba(148,163,184,0.25);
	outline: none;
	position: relative;
}

input[type="range"]::-webkit-slider-thumb {
	-webkit-appearance: none;
	width: 22px;
	height: 22px;
	border-radius: 50%;
	background: linear-gradient(135deg, var(--accent), var(--accent-2));
	box-shadow: 0 10px 25px rgba(14,165,233,0.35);
	border: 3px solid #0f172a;
	cursor: pointer;
}

input[type="range"]::-moz-range-thumb {
	width: 22px;
	height: 22px;
	border-radius: 50%;
	background: linear-gradient(135deg, var(--accent), var(--accent-2));
	border: none;
	cursor: pointer;
}

.legend {
	padding: 1rem;
	border-radius: 16px;
	background: rgba(15, 23, 42, 0.75);
	position: relative;
}

.legend-below { display: none; }

/* On narrow screens show legend items on a single horizontal row */
@media (max-width: 600px) {
	.legend ul {
		list-style: none;
		padding: 0;
		margin: 0;
		display: flex;
		flex-direction: row;
		gap: 1rem;
		-webkit-overflow-scrolling: touch;
	}

	.legend li {
		display: inline-flex;
		align-items: center;
		gap: 0.5rem;
		white-space: nowrap;
		margin: 0;
	}
	.legend ul::-webkit-scrollbar {
		height: 8px;
	}
	.legend ul::-webkit-scrollbar-thumb {
		background: rgba(255,255,255,0.06);
		border-radius: 999px;
	}
}

.legend h3 {
	margin: 0 0 0.5rem;
	font-size: 0.9rem;
	letter-spacing: 0.05em;
	text-transform: uppercase;
	color: var(--text-muted);
}

.legend ul {
	margin: 0;
	padding-left: 1rem;
	display: grid;
	gap: 0.4rem;
}

.legend-box {
	display: inline-block;
	width: 16px;
	height: 16px;
	margin-right: 6px;
	border-radius: 4px;
	border: 1px solid rgba(255,255,255,0.4);
}

.legend-box.enzyme { background: #38bdf8; }
.legend-box.substrate { background: #fbbf24; }
.legend-box.product { background: #4ade80; }
/* Denatured enzyme visual: spiky/red blob */
.legend-box.denatured {
	background: radial-gradient(circle at 35% 30%, #f97316 0%, #ef4444 50%, #a71313 100%);
	box-shadow: 0 4px 10px rgba(239,68,68,0.15);
}

.stats {
	padding: 1rem;
	border-radius: 16px;
	background: rgba(8, 47, 73, 0.5);
	border: 1px solid rgba(56, 189, 248, 0.2);
	display: grid;
	gap: 0.35rem;
}

.stats p {
	margin: 0;
	font-size: 0.95rem;
	display: flex;
	justify-content: space-between;
}

.simulation-wrapper,
.graph-wrapper {
	padding: 1rem;
	display: flex;
	justify-content: center;
	align-items: center;
	position: relative;
}

.graph-wrapper { grid-column: 1 / -1; }

canvas {
	background: rgba(2, 6, 23, 0.85);
	border: 1px solid rgba(148, 163, 184, 0.3);
	border-radius: 14px;
	image-rendering: pixelated;
}

/* Place legend and stats full-width under the simulation canvas */
.below-sim {
	display: flex;
	gap: 1rem;
	align-items: center;
	justify-content: space-between;
	margin-top: 1rem;
	flex-wrap: wrap;
}

.below-sim .legend,
.below-sim .stats {
	flex: 1 1 320px;
	min-width: 220px;
}

/* Ensure when legend or stats are direct children of the layout they span both columns */
.layout > .legend,
.layout > .stats,
.layout > .below-sim {
	grid-column: 1 / -1;
}

/* Medium screens: place legend and stats side-by-side below the sim; hide controls legend */
@media (min-width: 768px) and (max-width: 1199px) {
	.legend-controls { display: none; }
	.legend-below { display: block; }
	.below-sim {
		display: grid;
		grid-template-columns: repeat(2, minmax(260px, 1fr));
		align-items: stretch;
		gap: 1rem;
		width: 100%;
	}
	.below-sim .legend,
	.below-sim .stats {
		flex: initial;
		min-width: 0;
	}
}

/* Small screens: keep legend and stats side-by-side; everything else spans full width */
@media (max-width: 767px) {
	.legend-controls { display: none; }
	.legend-below { display: block; }
	.below-sim {
		display: grid;
		grid-template-columns: repeat(2, minmax(0, 1fr));
		gap: 0.75rem;
		width: 100%;
	}
	.below-sim .legend,
	.below-sim .stats {
		min-width: 0;
		width: 100%;
	}
}

/* Extra small: shrink legend text and allow wrapping */
@media (max-width: 480px) {
	.legend {
		font-size: 0.9rem;
	}
	.legend h3 {
		font-size: 0.8rem;
	}
	.legend ul {
		gap: 0.3rem;
	}
	.legend li {
		display: grid;
		grid-template-columns: auto 1fr;
		column-gap: 0.45rem;
		white-space: normal;
		word-break: break-word;
		line-height: 1.25;
	}
}

/* Large screens: keep legend in controls, hide the below legend */
@media (min-width: 1200px) {
	.legend-controls { display: block; }
	.legend-below { display: none; }
}

.optimum-badge {
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%) scale(.9);
	z-index: 2;
	background: linear-gradient(120deg, #06b6d4, #3b82f6);
	color: #fff;
	padding: 0.45rem 1.4rem;
	font-size: 0.9rem;
	font-weight: 600;
	border-radius: 999px;
	box-shadow: 0 0 0 0 rgba(56, 189, 248, 0.7);
	opacity: 0;
	transition: opacity .4s ease, transform .4s cubic-bezier(.4,.14,.3,1.4), box-shadow 1.4s ease;
	letter-spacing: 0.5px;
	pointer-events: none;
}

.optimum-badge.show {
	opacity: 1;
	transform: translate(-50%, -50%) scale(1);
	box-shadow: 0 0 12px 4px rgba(56,189,248,0.4),0 0 28px 10px rgba(59,130,246,0.25);
}

footer {
	text-align: center;
	padding: 1.25rem;
	color: var(--text-muted);
	font-size: 0.9rem;
}

@media (max-width: 1200px) {
	.layout {
		grid-template-columns: 1fr;
	}

	.controls {
		order: 1;
	}

	.simulation-wrapper {
		order: 2;
	}

	.below-sim {
		order: 2;
	}

	.graph-wrapper {
		order: 3;
	}
}

@media (max-width: 640px) {
	header {
		padding: 2rem 1rem;
	}

	.layout {
		padding: 0 1rem 1.5rem;
	}

	.controls {
		padding: 1rem;
	}

	.input-group label {
		flex-direction: column;
		gap: 0.3rem;
	}

	canvas {
		width: 100% !important;
		height: auto;
	}
}

/* Mobile factor chooser styles */
.mobile-factor-selector { display: none; position: relative; }

@media (max-width: 640px) {
	.mobile-factor-selector { display:flex; justify-content:space-between; align-items:center; gap:0.5rem; margin-bottom:0.35rem; overflow: visible; }
	.choose-factor-btn {
		background: linear-gradient(90deg, var(--accent), var(--accent-2));
		color: #021428;
		border: none;
		padding: 0.45rem 0.9rem;
		border-radius: 999px;
		font-weight: 600;
		box-shadow: 0 6px 18px rgba(14,165,233,0.12);
		display: inline-flex;
		align-items: center;
		gap: 0.6rem;
	}
	.choose-factor-btn .choose-caret {
		display: inline-flex;
		align-items: center;
		justify-content: center;
		width: 26px;
		height: 26px;
		border-radius: 8px;
		background: rgba(255,255,255,0.06);
		color: #000;
		font-size: 0.95rem;
	}
	.choose-factor-btn:focus .choose-caret { box-shadow: 0 4px 12px rgba(14,165,233,0.14); }
	.choose-factor-wrap { display:flex; align-items:center; gap:0.5rem; cursor:pointer; }
	.choose-factor-wrap:focus { outline: 3px solid rgba(14,165,233,0.12); border-radius: 999px; }
	.factor-dropdown {
 		position: absolute;
 		left: 0;
 		top: 100%;
 		transform: translateY(6px);
 		background: var(--panel);
 		border: 1px solid var(--panel-border);
 		border-radius: 10px;
 		padding: 0.2rem;
 		min-width: auto;
 		max-width: calc(100vw - 2rem);
 		z-index: 60;
 		box-shadow: 0 10px 30px rgba(2,6,23,0.6);
		display: grid;
		grid-template-columns: repeat(2, minmax(110px, 1fr));
		gap: 0.01rem;
 	}
	.factor-dropdown[hidden] { display: none; }
	.factor-dropdown ul { list-style: none; margin: 0; padding: 0; display: contents; }
	.factor-dropdown li { padding: 0.22rem 0.35rem; cursor: pointer; border-radius: 6px; color: var(--text-muted); display: flex; align-items: center; justify-content: center; font-size: 0.82rem; }
	.factor-dropdown li:hover { background: rgba(255,255,255,0.02); color: #fff; }

/* On very narrow screens a 2x2 grid looks cramped; switch to single column and left-align items */
@media (max-width: 380px) {
	.factor-dropdown { grid-template-columns: 1fr; gap: 0.01rem; padding: 0.08rem; }
	.factor-dropdown li { justify-content: flex-start; padding: 0.18rem 0.32rem; font-size: 0.8rem; }
	.factor-dropdown { max-width: calc(100vw - 1.25rem); left: 0.375rem; right: auto; }
}

/* On mobile, left-align the first item so it sits flush in the first column */
@media (max-width: 640px) {
	#factorDropdown > ul > li:nth-child(1) { justify-content: flex-start; }
}

	.input-grid { display: block; }
	.input-grid .input-group { display: none; position: relative; padding-top: 0.25rem; margin-bottom: 0.5rem; }
	.input-grid .input-group.active { display: block; }

	/* per-group dropdown toggles removed - managed by central chooser */
}

/* Make the label value + unit sit on a single row, and hide the title when the mobile chooser already shows it */
@media (max-width: 640px) {
 	.input-group label { display: flex; flex-direction: column; gap: 0.35rem; align-items: right; text-align: center; }
 	.input-group label .label-row { display: inline-flex; gap: 0.35rem; align-items: right; justify-content: right; width: 100%; font-size: 1.05rem; font-weight: 600; color: #fff; margin-top: -1.5rem; }
	.input-group label .label-title { color: var(--text-muted); font-size: 0.85rem; font-weight: 600; }
	/* hide the title because the choose-button already displays the factor name */
	.input-group.active label .label-title { display: none; }
 	/* center the label row vertically and horizontally within the active control */
 	.input-grid .input-group.active { min-height: 56px; display:flex; flex-direction:column; justify-content:right; align-items:right; }
	.input-group label .label-row { align-items: right; }
}

@keyframes pulseBackdrop {
 	0% { opacity: 0.6; }
 	50% { opacity: 0.9; }
 	100% { opacity: 0.6; }
}
