:root {
	color-scheme: light;
	--bg: #f2f5fa;
	--panel: #ffffff;
	--border: #d0d8e7;
	--text: #152238;
	--muted: #5a6a84;
	--active-bg: #1f5fc2;
	--active-text: #ffffff;
	--hover-bg: #eaf2ff;
	--topbar-height: 56px;
	--layout-min-width: 1020px; /* slightly wider to allow for scrollbar */
}

* {
	box-sizing: border-box;
}

body {
	margin: 0;
	font-family: "Segoe UI", Arial, sans-serif;
	color: var(--text);
	background: var(--bg);
}

.topbar {
	position: sticky;
	top: 0;
	z-index: 20;
	min-width: calc(var(--layout-min-width) + 36px);
	padding: 0.45rem 0.9rem;
	display: grid;
	/* gap: 0.35rem; */
	background: #ffffff;
	border-bottom: 1px solid #d7dfed;
	box-shadow: none;
}

.topbar.expanded {
	padding: 0.45rem 0.9rem;
	/* gap: 0.35rem; */
}

.topbar-row {
	height: 52px;
	display: flex;
	align-items: center;
	justify-content: space-between;
}

.topbar-actions {
	display: flex;
	align-items: center;
	gap: 0.35rem;
}

.topbar-title {
	font-size: 1.05rem;
	font-weight: 700;
	letter-spacing: 0.01em;
}

.icon-btn {
	width: 36px;
	height: 36px;
	display: grid;
	place-items: center;
	border-radius: 4px;
	border: 1px solid #c8d3e8;
	background: #ffffff;
	font-size: 1rem;
	cursor: pointer;
	transition: background 120ms ease, border-color 120ms ease;
}

.icon-btn:hover {
	background: #f3f7ff;
	border-color: #b8c7e4;
}

.icon-btn:active {
	background: #ebf1fd;
}

.icon-btn.active {
	background: #e9f1ff;
	border-color: #9eb6e1;
}

.page-shell {
	width: min(1400px, 96vw);
	min-width: var(--layout-min-width);
	margin: 0.6rem auto 2rem;
	display: grid;
	gap: 1rem;
}

.controls-shell {
	overflow: hidden;
	max-height: 460px;
	opacity: 1;
	margin-top: 0.1rem;
	transform: translateY(0);
	transition: max-height 260ms ease, opacity 220ms ease, transform 220ms ease, margin-top 220ms ease;
}

.controls-shell.collapsed {
	max-height: 0;
	opacity: 0;
	margin-top: 0;
	transform: translateY(-8px);
	pointer-events: none;
}

.controls {
	display: grid;
	grid-template-columns: minmax(0, 1.65fr) repeat(2, minmax(0, 1fr));
	gap: 0.7rem;
}

.control-card {
	background: var(--panel);
	border: 1px solid var(--border);
	border-radius: 6px;
	padding: 0.72rem 0.75rem;
	display: grid;
	gap: 0.55rem;
	box-shadow: none;
}

.card-header-row {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 0.5rem;
	min-height: 36px;
}

.control-card h2 {
	margin: 0;
	font-size: 1rem;
	font-weight: 650;
}

.sub-control-group {
	display: grid;
	gap: 0.35rem;
	padding-top: 0.2rem;
	border-top: 1px solid #edf1f8;
}

.sub-control-group:first-of-type {
	border-top: 0;
	padding-top: 0;
}

.dual-sub-controls {
	display: grid;
	grid-template-columns: 1fr 1fr;
	gap: 0.5rem;
}

.dual-sub-controls .sub-control-group {
	border-top: 1px solid #edf1f8;
	padding-top: 0.2rem;
}

.sub-control-head {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 0.4rem;
	min-height: 34px;
}

.sub-control-head h3 {
	margin: 0;
	font-size: 1.02rem;
	font-weight: 650;
	color: #253a62;
}

.mode-toggle,
.value-grid {
	display: flex;
	flex-wrap: wrap;
	gap: 0.5rem;
}

.select-control {
	display: block;
}

.control-select {
	width: 100%;
	height: 34px;
	border: 1px solid #cdd8ea;
	border-radius: 4px;
	padding: 0 0.5rem;
	font-size: 0.9rem;
	font-weight: 600;
	background: #ffffff;
	color: var(--text);
}

.control-select:focus {
	outline: 2px solid #b8c6df;
	outline-offset: 1px;
}

button.control-btn {
	border: 1px solid #cdd8ea;
	background: #ffffff;
	color: var(--text);
	border-radius: 4px;
	padding: 0.46rem 0.8rem;
	min-width: 48px;
	font-size: 0.92rem;
	font-weight: 600;
	line-height: 1;
	cursor: pointer;
	transition: background 120ms ease, border-color 120ms ease, transform 120ms ease;
	box-shadow: none;
}

button.control-btn:hover {
	background: var(--hover-bg);
	border-color: #b9c9e6;
}

button.control-btn:active {
	transform: translateY(1px);
}

button.control-btn.active {
	border-color: #8b95a3;
	background: #c8ced8;
	color: #1f2733;
}

.mode-toggle .control-btn {
	min-width: 78px;
}

.segmented-toggle {
	background: #f3f6fc;
	padding: 0.2rem;
	border: 1px solid #d2dbec;
	border-radius: 5px;
	gap: 0.2rem;
}

.segmented-toggle .control-btn {
	min-width: 68px;
	border-radius: 4px;
	border-color: transparent;
	background: transparent;
	padding: 0.38rem 0.72rem;
	font-size: 0.9rem;
	box-shadow: none;
}

.segmented-toggle .control-btn.active {
	background: #c2c9d4;
	color: #1f2835;
	border-color: #909aaa;
	box-shadow: none;
}

#settings-card .card-header-row {
	height: 36px;
	min-height: 36px;
}

#settings-card h2 {
	white-space: nowrap;
}

.segmented-toggle .control-btn:disabled {
	opacity: 0.45;
	cursor: not-allowed;
}

.settings-nav {
	display: flex;
	gap: 0.25rem;
}

.settings-arrow {
	width: 30px;
	height: 28px;
	font-size: 0.78rem;
	padding: 0;
}

.settings-arrow:disabled {
	opacity: 0.42;
	cursor: not-allowed;
	background: #f3f6fc;
}

#settings-values {
	display: grid;
	grid-template-columns: 1fr;
	gap: 0.4rem;
	min-height: 124px;
	align-content: start;
}

#settings-values .control-btn {
	width: 100%;
	height: 36px;
	display: flex;
	align-items: center;
	justify-content: flex-start;
}

.viz-wrapper,
.timeseries-panel {
	background: var(--panel);
	border: 1px solid var(--border);
	border-radius: 6px;
	padding: 0.75rem;
}

.tables-panel {
	background: var(--panel);
	border: 1px solid var(--border);
	border-radius: 6px;
	padding: 0.75rem;
}

.tables-grid {
	display: grid;
	grid-template-columns: 1fr 1fr;
	gap: 0.7rem;
}

.table-card {
	border: 1px solid #d9e1ef;
	border-radius: 6px;
	padding: 0.55rem;
	display: grid;
	gap: 0.45rem;
}

.table-card h4 {
	margin: 0;
	font-size: 0.92rem;
	font-weight: 650;
}

.table-scroll {
	overflow-x: auto;
	border: 1px solid #e2e7f2;
	border-radius: 4px;
	padding-bottom: 7px;
	scrollbar-gutter: stable both-edges;
}

.table-scroll table {
	width: max-content;
	min-width: 100%;
	border-collapse: collapse;
	font-size: 0.86rem;
	margin-bottom: 2px;
}

.table-scroll th,
.table-scroll td {
	padding: 0.26rem 0.38rem;
	border-bottom: 1px solid #e6ebf5;
	border-right: 1px solid #edf1f8;
	white-space: nowrap;
	text-align: right;
}

.table-scroll th:first-child,
.table-scroll td:first-child {
	text-align: left;
	position: sticky;
	left: 0;
	background: #f8fbff;
	z-index: 1;
}

.table-scroll thead th {
	background: #f3f7ff;
	font-weight: 650;
}

.table-mode-row {
	display: flex;
	justify-content: flex-end;
}

.modal-backdrop {
	position: fixed;
	inset: 0;
	z-index: 50;
	background: rgba(12, 18, 30, 0.48);
	display: none;
	align-items: center;
	justify-content: center;
	padding: 1rem;
}

.modal-backdrop.visible {
	display: flex;
}

.modal-card {
	width: min(720px, 96vw);
	max-height: min(80vh, 640px);
	overflow: auto;
	background: #ffffff;
	border: 1px solid #d3dceb;
	border-radius: 6px;
	padding: 0.8rem;
}

.modal-header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 0.5rem;
	margin-bottom: 0.4rem;
}

.modal-header h3 {
	margin: 0;
	font-size: 1rem;
	font-weight: 700;
}

.modal-content p {
	margin: 0.3rem 0 0.45rem;
	font-size: 0.94rem;
}

.modal-content ul {
	margin: 0;
	padding-left: 1.1rem;
	display: grid;
	gap: 0.35rem;
	font-size: 0.92rem;
}

.section-title {
	margin: 0 0 0.45rem;
	font-size: 1rem;
	font-weight: 700;
}

.section-header-row {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 0.5rem;
	margin-bottom: 0.45rem;
}

.section-header-row .section-title {
	margin: 0;
}

.section-actions {
	display: flex;
	align-items: center;
	gap: 0.4rem;
}

.section-action-btn {
	height: 30px;
	padding: 0 0.65rem;
	border: 1px solid #c8d3e8;
	border-radius: 4px;
	background: #ffffff;
	color: var(--text);
	font-size: 0.92rem;
	font-weight: 600;
	cursor: pointer;
}

.section-action-btn:hover {
	background: #f3f7ff;
	border-color: #b8c7e4;
}

.section-link-btn {
	width: 30px;
	height: 30px;
	font-size: 0.92rem;
	padding: 0;
}

.viz-root {
	width: 100%;
	min-height: 620px;
}

.plot-label {
	font-size: 11px;
	pointer-events: none;
}

.plot-label.topic-vertical {
	font-size: 10px;
}

.box-segment {
	cursor: pointer;
	transition: opacity 100ms ease, stroke-width 100ms ease;
}

.box-segment.hovered {
	opacity: 0.82;
}

.box-segment.selected {
	opacity: 1;
}

.box-segment:focus-visible {
	outline: none;
	stroke: var(--active-bg);
	stroke-width: 2;
}

.selected-overlay {
	pointer-events: none;
}

.panel-header-row {
	display: flex;
	justify-content: space-between;
	align-items: center;
	gap: 0.7rem;
}

.timeseries-panel h3 {
	margin: 0;
	font-size: 1rem;
}

.timeseries-panel.hidden {
	display: none;
}

.timeseries-chart {
	min-height: 300px;
	margin-top: 0.6rem;
}

.timeseries-controls {
	display: flex;
	align-items: center;
	gap: 0.5rem;
}

.timeseries-table {
	margin-top: 0.6rem;
}

.timeseries-table table {
	width: 100%;
	border-collapse: collapse;
	font-size: 0.92rem;
}

.timeseries-table th,
.timeseries-table td {
	text-align: left;
	padding: 0.35rem 0.5rem;
	border-bottom: 1px solid #e5e9f2;
}

.timeseries-grid {
	display: grid;
	gap: 0.7rem;
	grid-template-columns: repeat(var(--series-columns, 1), minmax(0, 1fr));
}

.timeseries-grid.two-series {
	grid-template-columns: repeat(2, minmax(0, 1fr));
}

.timeseries-grid.four-series {
	grid-template-columns: repeat(2, minmax(0, 1fr));
}

.timeseries-card {
	border: 1px solid #d9e1ef;
	border-radius: 9px;
	padding: 0.45rem;
}

.timeseries-card h4 {
	margin: 0 0 0.35rem;
	font-size: 0.9rem;
	font-weight: 650;
}

.swatch {
	width: 12px;
	height: 12px;
	border-radius: 2px;
	display: inline-block;
}

.timeseries-empty {
	color: var(--muted);
	font-size: 0.95rem;
	padding: 0.4rem;
}

.custom-tooltip {
	position: fixed;
	z-index: 1000;
	pointer-events: none;
	background: rgba(15, 23, 41, 0.95);
	color: #ffffff;
	border-radius: 6px;
	padding: 0.42rem 0.52rem;
	font-size: 0.82rem;
	line-height: 1.3;
	opacity: 0;
	transform: translate(-50%, -110%);
	transition: opacity 90ms ease;
	white-space: nowrap;
}

.custom-tooltip.visible {
	opacity: 1;
}

@media (max-width: 980px) {
	.controls-shell {
		overflow-y: auto;
		overflow-x: hidden;
		max-height: min(360px, calc(100dvh - 170px));
		scrollbar-gutter: stable;
	}

	.controls-shell.collapsed {
		overflow: hidden;
	}

	.controls {
		grid-template-columns: 1fr;
	}

	.tables-grid {
		grid-template-columns: 1fr;
	}

	.dual-sub-controls {
		grid-template-columns: 1fr;
	}

	.timeseries-grid {
		grid-template-columns: 1fr;
	}
}
