:root {
	color-scheme: light dark;
	--primary: #12355b;
	--primary-2: #1a4a7d;
	--accent: #0ea5a3;
	--success: #27ae60;
	--danger: #c0392b;
	--bg: #f3f7fb;
	--surface: #ffffff;
	--surface-soft: #eef5fb;
	--border: #d8e2ed;
	--border-soft: #eeeeee;
	--text: #17324d;
	--muted: #5d7287;
	--row-hover: #edf8fb;
	--shadow: 0 2px 5px rgba(0,0,0,0.05);
	--toast-shadow: 0 4px 6px rgba(0,0,0,0.1);
}


:root[data-theme='dark'] {
	color-scheme: dark;
	--primary: #0f1722;
	--primary-2: #1d2938;
	--accent: #4aa3e8;
	--success: #2eb872;
	--danger: #d95b4d;
	--bg: #0b1118;
	--surface: #111a24;
	--surface-soft: #182331;
	--border: #243240;
	--border-soft: #2a3a4b;
	--text: #e5edf5;
	--muted: #9db0c2;
	--row-hover: #172434;
	--shadow: 0 2px 6px rgba(0,0,0,0.35);
	--toast-shadow: 0 6px 10px rgba(0,0,0,0.4);
}

:root[data-theme='light'] {
	color-scheme: light;
}

@media (prefers-color-scheme: dark) {
	:root:not([data-theme='light']) {
		--primary: #0f1722;
		--primary-2: #1d2938;
		--accent: #4aa3e8;
		--success: #2eb872;
		--danger: #d95b4d;
		--bg: #0b1118;
		--surface: #111a24;
		--surface-soft: #182331;
		--border: #243240;
		--border-soft: #2a3a4b;
		--text: #e5edf5;
		--muted: #9db0c2;
		--row-hover: #172434;
		--shadow: 0 2px 6px rgba(0,0,0,0.35);
		--toast-shadow: 0 6px 10px rgba(0,0,0,0.4);
	}
}

body { font-family: 'Inter', sans-serif; margin: 0; display: flex; height: 100vh; background: var(--bg); color: var(--text); }

#app-root {
	display: flex;
	flex: 1;
	min-width: 0;
}

.app-shell {
	display: flex;
	flex: 1;
	min-width: 0;
}

/* Sidebar */
aside { flex: 0 0 240px; background: var(--primary); color: #fff; display: flex; flex-direction: column; transition: width 0.2s ease; }
body.sidebar-hidden aside { flex: 0 0 0; overflow: hidden; }
.brand { padding: 14px 16px; font-size: 1.2rem; font-weight: bold; border-bottom: 1px solid var(--primary-2); display: flex; align-items: center; gap: 10px; }
.app-logo {
	width: 30px;
	height: 30px;
	border-radius: 8px;
	border: 1px solid rgba(255,255,255,0.25);
	background: #fff;
	object-fit: cover;
	flex: 0 0 auto;
}
.app-logo-sm {
	border-color: var(--border);
}
.title-wrap {
	display: inline-flex;
	align-items: center;
	gap: 10px;
}
.sidebar-title { white-space: nowrap; }
#sidebar-list { flex: 1; overflow-y: auto; }
.sidebar-footer {
	margin-top: auto;
	padding: 12px 14px;
	border-top: 1px solid var(--primary-2);
	background: rgba(255, 255, 255, 0.02);
}
.icon-btn { width: 32px; height: 32px; border: none; border-radius: 6px; cursor: pointer; font-size: 1rem; display: inline-flex; align-items: center; justify-content: center; padding: 0; background: none; }
.icon { display: inline-block; vertical-align: middle; flex-shrink: 0; }
.icon-small { display: inline-block; vertical-align: middle; flex-shrink: 0; }
.btn-icon-text { display: inline-flex; align-items: center; gap: 6px; }
#toggle-sidebar-btn {
	background: var(--primary-2);
	color: #fff;
}
#toggle-sidebar-float {
	position: fixed;
	top: 14px;
	left: 14px;
	z-index: 1000;
	background: var(--primary);
	color: #fff;
	display: none;
}
body.sidebar-hidden #toggle-sidebar-float {
	display: inline-flex;
}
.nav-item {
	width: 100%;
	padding: 15px 20px;
	cursor: pointer;
	border: 0;
	border-bottom: 1px solid var(--primary-2);
	border-radius: 0;
	transition: 0.2s;
	background: none;
	color: inherit;
	text-align: left;
	font: inherit;
}
.nav-item:hover { background: var(--primary-2); }
.nav-item.active { border-left: 4px solid var(--accent); background: var(--primary-2); }

/* Main Content */
main { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
#main-content { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
header { padding: 12px 30px; background: var(--surface); border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: flex-start; gap: 15px; }
header h2 { margin: 0; font-size: 1.3rem; }
.header-left {
	display: flex;
	align-items: center;
	gap: 10px;
	min-width: 0;
}

.header-right {
	display: flex;
	flex-direction: column;
	align-items: flex-end;
	gap: 10px;
}

.header-user-bar {
	display: flex;
	align-items: center;
	gap: 10px;
}

.header-user-pill {
	display: inline-flex;
	align-items: center;
	padding: 7px 12px;
	border-radius: 999px;
	background: var(--surface-soft);
	border: 1px solid var(--border);
	color: var(--muted);
	font-size: 0.82rem;
	font-weight: 700;
	letter-spacing: 0.02em;
	white-space: nowrap;
}

.header-actions { display: flex; gap: 10px; align-items: center; flex-wrap: nowrap; }

.header-logout-btn {
	background: var(--danger);
	color: #fff;
	box-shadow: var(--shadow);
}
.content-area { display: flex; flex: 1; overflow: hidden; padding: 20px; gap: 20px; }

/* Grid & Detail Panel */
#grid-container { flex: 2; overflow-y: auto; background: var(--surface); border-radius: 8px; box-shadow: var(--shadow); }
#detail-panel { flex: 1; background: var(--surface); border-radius: 8px; padding: 20px; box-shadow: var(--shadow); overflow-y: auto; }
body.details-hidden #detail-panel { display: none; }

/* Tables */
table { width: 100%; border-collapse: collapse; }
caption {
	text-align: left;
	padding: 12px;
	font-size: 0.85rem;
	color: var(--muted);
}
th { position: sticky; top: 0; background: var(--surface-soft); text-align: left; padding: 12px; border-bottom: 2px solid var(--border-soft); font-size: 0.85rem; color: var(--muted); }
td { padding: 12px; border-bottom: 1px solid var(--border-soft); font-size: 0.9rem; cursor: pointer; }
tr:hover { background: var(--row-hover); }
tr.selected-row { background: transparent; }
tr.selected-row td:first-child { box-shadow: inset 4px 0 0 var(--accent); }
.sort-header-btn {
	all: unset;
	cursor: pointer;
	font-weight: 700;
	color: inherit;
	display: inline-flex;
	align-items: center;
	gap: 6px;
}
.sort-header-btn:focus-visible {
	outline: 2px solid var(--accent);
	outline-offset: 2px;
	border-radius: 4px;
}

.grid-toolbar {
	display: grid;
	grid-template-columns: minmax(0, 1fr);
	align-items: center;
	gap: 10px;
	padding: 12px;
	border-bottom: 1px solid var(--border-soft);
	background: var(--surface);
	position: sticky;
	top: 0;
	z-index: 4;
}

.grid-toolbar-left {
	display: flex;
	align-items: center;
	gap: 8px;
	flex-wrap: nowrap;
}

.grid-toolbar-left {
	min-width: 0;
}

.grid-search-wrap {
	position: relative;
	width: 100%;
}

.grid-search-icon {
	position: absolute;
	top: 50%;
	left: 12px;
	transform: translateY(-50%);
	color: var(--muted);
	pointer-events: none;
	display: inline-flex;
	align-items: center;
}

.grid-search {
	width: 100%;
	max-width: none;
	padding-left: 40px;
}

.grid-pagination {
	display: flex;
	justify-content: space-between;
	align-items: center;
	flex-wrap: nowrap;
	padding: 10px 12px;
	border-top: 1px solid var(--border-soft);
	font-size: 0.85rem;
	color: var(--muted);
	gap: 10px;
}

.grid-pagination-controls {
	display: flex;
	gap: 8px;
	align-items: center;
	flex-wrap: nowrap;
	white-space: nowrap;
	overflow-x: auto;
}

.btn-small {
	padding: 6px 10px;
	font-size: 0.8rem;
	background: var(--primary-2);
	color: #fff;
}

.btn-small:disabled {
	opacity: 0.45;
	cursor: not-allowed;
}
.sr-only {
	position: absolute;
	width: 1px;
	height: 1px;
	padding: 0;
	margin: -1px;
	overflow: hidden;
	clip: rect(0, 0, 0, 0);
	white-space: nowrap;
	border: 0;
}

/* Forms */
.form-group { margin-bottom: 15px; }
label { display: block; margin-bottom: 5px; font-weight: bold; font-size: 0.8rem; color: var(--muted); }
input, select { width: 100%; padding: 8px; border: 1px solid var(--border); border-radius: 4px; box-sizing: border-box; background: var(--surface); color: var(--text); }
button { padding: 10px 15px; border: none; border-radius: 4px; cursor: pointer; font-weight: bold; }
.btn-save { background: var(--success); color: #fff; width: 100%; }
.btn-muted { background: var(--primary-2); color: #fff; }
.btn-accent { background: var(--accent); color: #fff; }

.row-actions-card {
	margin-bottom: 16px;
	padding-bottom: 14px;
	border-bottom: 1px solid var(--border-soft);
}

.row-actions-card h3 {
	margin: 0 0 10px 0;
}

.row-actions-list {
	display: flex;
	flex-wrap: wrap;
	gap: 8px;
}

.btn-action {
	background: var(--primary-2);
	color: #fff;
	padding: 8px 12px;
}

.action-form-card {
	margin-bottom: 16px;
	padding-bottom: 16px;
	border-bottom: 1px solid var(--border-soft);
}

.action-form-card h3 {
	margin: 0 0 12px 0;
}

.action-form-buttons {
	display: flex;
	gap: 8px;
	align-items: center;
}

.detail-card {
	display: grid;
	gap: 12px;
}

.detail-card h3 {
	margin: 0;
}

.detail-form-actions {
	display: flex;
	gap: 8px;
	align-items: center;
	flex-wrap: wrap;
}

.delete-form {
	margin-top: 8px;
}

.btn-danger {
	background: var(--danger);
	color: #fff;
}

.page-size-form {
	display: flex;
	align-items: center;
	gap: 8px;
}

.lookup-input-wrap {
	display: flex;
	gap: 8px;
}

.lookup-value {
	flex: 1;
}

.lookup-modal-backdrop {
	position: fixed;
	inset: 0;
	background: rgba(0, 0, 0, 0.4);
	display: flex;
	align-items: center;
	justify-content: center;
	z-index: 2000;
}

.lookup-modal {
	width: min(560px, 92vw);
	max-height: 80vh;
	overflow: hidden;
	background: var(--surface);
	border: 1px solid var(--border);
	border-radius: 10px;
	box-shadow: var(--shadow);
	display: flex;
	flex-direction: column;
}

.lookup-modal-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 12px;
	border-bottom: 1px solid var(--border-soft);
}

.lookup-modal-header h4 {
	margin: 0;
}

.lookup-modal-body {
	padding: 12px;
	overflow: auto;
}

.lookup-list {
	list-style: none;
	padding: 0;
	margin: 0;
	display: grid;
	gap: 8px;
}

.lookup-item-btn {
	width: 100%;
	text-align: left;
	background: var(--surface-soft);
	color: var(--text);
	border: 1px solid var(--border-soft);
	padding: 10px;
	border-radius: 6px;
	cursor: pointer;
}

.action-result-card {
	margin-top: 12px;
	padding-top: 12px;
	border-top: 1px solid var(--border-soft);
}

.action-result-card h4 {
	margin: 0 0 8px 0;
}

.action-result-table-wrap {
	overflow: auto;
	border: 1px solid var(--border-soft);
	border-radius: 6px;
}

.action-result-table {
	width: 100%;
	border-collapse: collapse;
}

.action-result-table th,
.action-result-table td {
	padding: 8px 10px;
	border-bottom: 1px solid var(--border-soft);
	font-size: 0.85rem;
}

.action-result-json {
	margin: 0;
	padding: 10px;
	background: var(--surface-soft);
	border: 1px solid var(--border-soft);
	border-radius: 6px;
	white-space: pre-wrap;
	word-break: break-word;
	font-size: 0.8rem;
}

.theme-toggle {
	display: inline-flex;
	align-items: center;
	gap: 8px;
	color: #fff;
	cursor: pointer;
	user-select: none;
}

.theme-mode-icon {
	opacity: 0.85;
}

#theme-toggle {
	position: absolute;
	opacity: 0;
	pointer-events: none;
}

.theme-toggle-track {
	position: relative;
	width: 44px;
	height: 24px;
	border-radius: 999px;
	background: rgba(255, 255, 255, 0.2);
	border: 1px solid rgba(255, 255, 255, 0.35);
	transition: background 0.2s ease;
}

.theme-toggle-thumb {
	position: absolute;
	top: 2px;
	left: 2px;
	width: 18px;
	height: 18px;
	border-radius: 50%;
	background: #fff;
	box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
	transition: transform 0.2s ease;
}

#theme-toggle:checked + .theme-toggle-track {
	background: rgba(74, 163, 232, 0.35);
}

#theme-toggle:checked + .theme-toggle-track .theme-toggle-thumb {
	transform: translateX(20px);
}

#theme-toggle:focus-visible + .theme-toggle-track {
	outline: 2px solid var(--accent);
	outline-offset: 2px;
}

.placeholder {
	color: var(--muted);
}

.padded {
	padding: 20px;
}

.delete-btn {
	background: none;
	border: none;
	cursor: pointer;
	color: var(--danger);
}

/* Toasts */
#toast-container { position: fixed; top: 20px; right: 20px; z-index: 9999; }
.toast { color: white; padding: 12px 20px; border-radius: 4px; margin-bottom: 10px; min-width: 200px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); animation: slideIn 0.3s ease; }

.auth-card {
	width: min(420px, 92vw);
	margin: auto;
	background: var(--surface);
	border: 1px solid var(--border);
	border-radius: 12px;
	padding: 20px;
	box-shadow: var(--shadow);
	display: grid;
	gap: 10px;
}

.auth-card h2 {
	margin: 0;
}

.auth-card button {
	justify-content: center;
}

@media (max-width: 900px) {
	body {
		overflow: hidden;
	}

	aside {
		position: fixed;
		top: 0;
		left: 0;
		height: 100vh;
		width: 260px;
		z-index: 1001;
		box-shadow: 0 8px 24px rgba(0,0,0,0.35);
		transition: transform 0.22s ease;
	}

	body.sidebar-hidden aside {
		width: 260px;
		overflow: hidden;
		transform: translateX(-100%);
	}

	main {
		width: 100%;
	}

	header {
		padding: 10px 12px;
		gap: 10px;
		align-items: flex-start;
		flex-direction: column;
	}

	body.sidebar-hidden header {
		padding-left: 56px;
	}

	h2#view-title {
		margin: 0;
		font-size: 1.1rem;
	}

	.header-actions {
		width: 100%;
		flex-wrap: wrap;
		gap: 8px;
	}

	.grid-toolbar {
		display: grid;
		grid-template-columns: minmax(0, 1fr);
		align-items: center;
	}

	.grid-toolbar-left,
	.grid-toolbar-right {
		width: auto;
	}

	.grid-search {
		width: 100%;
		max-width: none;
	}

	.grid-pagination {
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
	}

	.theme-toggle-track,
	.header-actions button {
		font-size: 0.9rem;
		padding: 8px 10px;
	}

	.theme-toggle-track {
		padding: 0;
	}

	.content-area {
		padding: 12px;
		gap: 12px;
		flex-direction: column;
	}

	#grid-container,
	#detail-panel {
		width: 100%;
		flex: initial;
	}

	#grid-container {
		min-height: 48vh;
	}

	#detail-panel {
		max-height: 42vh;
	}

	#toggle-sidebar-float {
		top: 10px;
		left: 10px;
	}

	header {
		padding: 12px;
		flex-direction: column;
		align-items: stretch;
	}

	.header-right {
		align-items: stretch;
	}

	.header-user-bar {
		justify-content: space-between;
	}

	.header-actions {
		flex-wrap: wrap;
		justify-content: flex-end;
	}
}
