:root {
	--bg: #f4f5f7;
	--surface: #ffffff;
	--surface-2: #f0f2f5;
	--border: #dfe3e8;
	--text: #1c2026;
	--muted: #66707c;
	--accent: #1f6feb;
	--accent-text: #ffffff;
	--ok: #1a7f37;
	--warn: #b35900;
	--danger: #c62828;
	--spark: #1f6feb;
	--radius: 10px;
	--shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
	color-scheme: light;
}

@media (prefers-color-scheme: dark) {
	:root {
		--bg: #0f1318;
		--surface: #171c22;
		--surface-2: #1e252d;
		--border: #2b333d;
		--text: #e6eaef;
		--muted: #93a0ad;
		--accent: #4c8dff;
		--accent-text: #0b0f14;
		--ok: #3fb950;
		--warn: #e3a03a;
		--danger: #f26b6b;
		--spark: #4c8dff;
		--shadow: none;
		color-scheme: dark;
	}
}

* { box-sizing: border-box; }

html, body {
	margin: 0;
	background: var(--bg);
	color: var(--text);
	font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

button, input, select, textarea { font: inherit; color: inherit; }

/* ---------- Kopf */

header {
	position: sticky;
	top: 0;
	z-index: 10;
	display: flex;
	align-items: center;
	gap: 16px;
	padding: 10px 20px;
	background: var(--surface);
	border-bottom: 1px solid var(--border);
}

.brand { font-weight: 650; white-space: nowrap; }

nav { display: flex; gap: 4px; overflow-x: auto; flex: 1; }

nav button {
	border: 0;
	background: none;
	padding: 7px 12px;
	border-radius: 8px;
	color: var(--muted);
	cursor: pointer;
	white-space: nowrap;
}

nav button:hover { background: var(--surface-2); color: var(--text); }
nav button.active { background: var(--surface-2); color: var(--text); font-weight: 600; }

.status {
	display: flex;
	align-items: center;
	gap: 6px;
	color: var(--muted);
	font-size: 13px;
	white-space: nowrap;
}

.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--danger); }
.dot.on { background: var(--ok); }

.usermenu { position: relative; }

.usermenu > button {
	border: 1px solid var(--border);
	background: var(--surface);
	border-radius: 8px;
	padding: 6px 10px;
	cursor: pointer;
	white-space: nowrap;
}

.menu {
	position: absolute;
	right: 0;
	top: calc(100% + 6px);
	min-width: 190px;
	background: var(--surface);
	border: 1px solid var(--border);
	border-radius: var(--radius);
	box-shadow: 0 8px 24px rgba(0, 0, 0, .15);
	padding: 6px;
}

.menu .who { padding: 6px 10px; color: var(--muted); font-size: 13px; }

.menu button {
	display: block;
	width: 100%;
	text-align: left;
	border: 0;
	background: none;
	padding: 8px 10px;
	border-radius: 6px;
	cursor: pointer;
}

.menu button:hover { background: var(--surface-2); }

/* ---------- Inhalt */

main { max-width: 1280px; margin: 0 auto; padding: 20px; }

h2 { font-size: 17px; margin: 26px 0 12px; display: flex; align-items: center; gap: 8px; }
h2:first-child { margin-top: 4px; }
h2 .sub { color: var(--muted); font-weight: 400; font-size: 13px; }

.grid {
	display: grid;
	grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
	gap: 12px;
}

.card {
	background: var(--surface);
	border: 1px solid var(--border);
	border-radius: var(--radius);
	box-shadow: var(--shadow);
	padding: 14px 16px;
	position: relative;
	min-width: 0;
}

.tile .label { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding-right: 22px; }
.tile .value { font-size: 26px; font-weight: 600; font-variant-numeric: tabular-nums; margin-top: 2px; overflow-wrap: anywhere; }
.tile .value .unit { font-size: 15px; font-weight: 400; color: var(--muted); margin-left: 4px; }
.tile .value.text { font-size: 16px; font-weight: 500; }
.tile .meta { color: var(--muted); font-size: 12px; margin-top: 6px; display: flex; justify-content: space-between; gap: 8px; }
.tile.stale .value { color: var(--muted); }
.tile svg { display: block; width: 100%; height: 34px; margin-top: 8px; }
.tile svg path { fill: none; stroke: var(--spark); stroke-width: 1.6; vector-effect: non-scaling-stroke; }
.tile svg .area { fill: var(--spark); opacity: .08; stroke: none; }
.tile.flash { animation: flash .8s ease-out; }

@keyframes flash { from { background: var(--surface-2); } to { background: var(--surface); } }

.edit {
	position: absolute;
	top: 8px;
	right: 8px;
	border: 0;
	background: none;
	color: var(--muted);
	cursor: pointer;
	padding: 4px;
	border-radius: 6px;
	line-height: 1;
	opacity: .55;
}

.edit:hover { opacity: 1; background: var(--surface-2); }

.empty {
	color: var(--muted);
	background: var(--surface);
	border: 1px dashed var(--border);
	border-radius: var(--radius);
	padding: 28px;
	text-align: center;
}

/* ---------- Steuerelemente */

.control .name { font-weight: 600; }
.control .topic { color: var(--muted); font-size: 12px; font-family: ui-monospace, monospace; overflow-wrap: anywhere; }
.control .row { display: flex; align-items: center; gap: 10px; margin-top: 12px; flex-wrap: wrap; }
.control .state { color: var(--muted); font-size: 13px; margin-top: 8px; }
.control input[type=range] { flex: 1; min-width: 120px; accent-color: var(--accent); }
.control input[type=number], .control input[type=text] { flex: 1; min-width: 80px; }

.switch { position: relative; width: 52px; height: 30px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }

.switch span {
	position: absolute;
	inset: 0;
	background: var(--border);
	border-radius: 30px;
	cursor: pointer;
	transition: background .15s;
}

.switch span::before {
	content: "";
	position: absolute;
	width: 24px;
	height: 24px;
	left: 3px;
	top: 3px;
	background: #fff;
	border-radius: 50%;
	transition: transform .15s;
	box-shadow: 0 1px 2px rgba(0, 0, 0, .3);
}

.switch input:checked + span { background: var(--ok); }
.switch input:checked + span::before { transform: translateX(22px); }
.switch input:disabled + span { opacity: .5; cursor: not-allowed; }

/* ---------- Formulare */

input, select, textarea {
	background: var(--surface);
	border: 1px solid var(--border);
	border-radius: 8px;
	padding: 8px 10px;
	min-width: 0;
}

input:focus, select:focus, textarea:focus, button:focus-visible {
	outline: 2px solid var(--accent);
	outline-offset: 1px;
}

textarea { width: 100%; min-height: 90px; font-family: ui-monospace, monospace; font-size: 13px; }

.btn {
	border: 1px solid var(--border);
	background: var(--surface);
	border-radius: 8px;
	padding: 8px 14px;
	cursor: pointer;
	white-space: nowrap;
}

.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.form { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.form label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
.form .full { grid-column: 1 / -1; }
.actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; flex-wrap: wrap; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-weight: 500; font-size: 13px; }
td.mono, .mono { font-family: ui-monospace, monospace; font-size: 13px; overflow-wrap: anywhere; }
.table-wrap { overflow-x: auto; }

.badge {
	display: inline-block;
	padding: 1px 8px;
	border-radius: 20px;
	font-size: 12px;
	background: var(--surface-2);
	border: 1px solid var(--border);
}

.log { max-height: 460px; overflow-y: auto; }
.log td { font-size: 13px; }
.log td.payload { max-width: 640px; }

/* ---------- Dialog & Meldungen */

dialog {
	border: 1px solid var(--border);
	border-radius: 14px;
	background: var(--surface);
	color: var(--text);
	padding: 22px;
	width: min(560px, calc(100vw - 32px));
	box-shadow: 0 20px 50px rgba(0, 0, 0, .3);
}

dialog::backdrop { background: rgba(10, 14, 20, .45); }
dialog h3 { margin: 0 0 16px; font-size: 17px; }

.toast {
	position: fixed;
	bottom: 20px;
	left: 50%;
	transform: translateX(-50%);
	background: var(--text);
	color: var(--bg);
	padding: 10px 18px;
	border-radius: 10px;
	box-shadow: 0 8px 24px rgba(0, 0, 0, .25);
	z-index: 100;
	max-width: calc(100vw - 32px);
}

.toast.error { background: var(--danger); color: #fff; }

.hidden { display: none !important; }

/* ---------- Login */

.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login { width: min(380px, 100%); padding: 28px; }
.login h1 { font-size: 20px; margin: 0 0 4px; }
.login p { color: var(--muted); margin: 0 0 20px; }
.login label { display: flex; flex-direction: column; gap: 4px; margin-bottom: 14px; font-size: 13px; color: var(--muted); }
.login .btn { width: 100%; padding: 10px; }
.login .error { color: var(--danger); min-height: 1.4em; margin-bottom: 8px; font-size: 14px; }

/* ---------- Mobil */

@media (max-width: 720px) {
	header { flex-wrap: wrap; padding: 10px 16px; gap: 10px; }
	nav { order: 3; flex-basis: 100%; }
	.brand { flex: 1; }
	main { padding: 16px; }
	.grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
	.tile .value { font-size: 22px; }
}

.hint { color: var(--muted); font-size: 13px; }
