:root {
	--bg: #f3f5f9;
	--surface: #ffffff;
	--surface-2: #f8fafc;
	--text: #1f2933;
	--muted: #6b7785;
	--border: #e6eaf0;
	--accent: #2f6df6;
	--accent-soft: #e8f0ff;
	--ok: #2bb673;
	--ok-soft: #e4f7ee;
	--open: #e8554e;
	--open-soft: #fdeceb;
	--warn: #e6a700;
	--shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 8px 24px rgba(16, 24, 40, .06);
	--shadow-sm: 0 1px 2px rgba(16, 24, 40, .06);
	--radius: 18px;
	--radius-sm: 12px;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
	margin: 0;
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
	background: var(--bg);
	color: var(--text);
	line-height: 1.5;
	padding-top: 64px;
	-webkit-font-smoothing: antialiased;
	-moz-osx-font-smoothing: grayscale;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---- Top bar ---- */
.topbar {
	position: fixed;
	top: 0; left: 0; right: 0;
	height: 64px;
	background: rgba(255, 255, 255, .85);
	backdrop-filter: saturate(180%) blur(12px);
	-webkit-backdrop-filter: saturate(180%) blur(12px);
	border-bottom: 1px solid var(--border);
	z-index: 50;
}
.topbar-inner {
	max-width: 980px;
	margin: 0 auto;
	height: 100%;
	padding: 0 20px;
	display: flex;
	align-items: center;
	justify-content: space-between;
}
.brand {
	display: flex;
	align-items: center;
	gap: 10px;
	font-weight: 700;
	font-size: 18px;
	color: var(--text);
}
.brand:hover { text-decoration: none; }
.brand .logo {
	width: 30px; height: 30px;
	border-radius: 7px;
	overflow: hidden;
	display: inline-block;
	line-height: 0;
}
.brand .logo img { width: 100%; height: 100%; display: block; }
.topbar-actions { display: flex; align-items: center; gap: 18px; }
.topbar-actions a { color: var(--muted); font-weight: 500; }
.topbar-actions a:hover { color: var(--text); text-decoration: none; }

/* ---- Layout ---- */
.container {
	max-width: 980px;
	margin: 0 auto;
	padding: 28px 20px 96px;
}
.page-title { font-size: 22px; font-weight: 700; margin: 8px 0 4px; }
.page-subtitle { color: var(--muted); margin: 0 0 24px; font-size: 15px; }
.section-label {
	font-size: 13px;
	font-weight: 600;
	text-transform: uppercase;
	letter-spacing: .04em;
	color: var(--muted);
	margin: 28px 0 14px;
}

/* ---- Door grid ---- */
.door-grid {
	display: grid;
	grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
	gap: 16px;
}
.door-tile {
	position: relative;
	appearance: none;
	border: 1px solid var(--border);
	background: var(--surface);
	border-radius: var(--radius);
	padding: 22px 16px 18px;
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: 12px;
	cursor: pointer;
	box-shadow: var(--shadow-sm);
	transition: transform .15s ease, box-shadow .2s ease, border-color .2s ease, background .2s ease;
	font: inherit;
	color: inherit;
	text-align: center;
	width: 100%;
}
.door-tile:hover { transform: translateY(-2px); box-shadow: var(--shadow); border-color: #d7deea; }
.door-tile:active { transform: translateY(0) scale(.98); }
.door-tile:focus-visible { outline: 3px solid var(--accent-soft); outline-offset: 2px; }

.door-icon {
	position: relative;
	width: 56px; height: 56px;
	border-radius: 50%;
	display: grid; place-items: center;
	background: var(--accent-soft);
	color: var(--accent);
	transition: background .2s ease, color .2s ease;
}
.door-icon svg { width: 28px; height: 28px; }
.door-label { font-weight: 600; font-size: 15px; }

/* status dot on the tile */
.door-dot {
	position: absolute;
	top: 14px; right: 14px;
	width: 10px; height: 10px;
	border-radius: 50%;
	background: var(--border);
	transition: background .2s ease, box-shadow .2s ease;
}

/* active = open / relay engaged */
.door-tile.is-active { border-color: var(--open); background: var(--open-soft); }
.door-tile.is-active .door-icon { background: var(--open); color: #fff; }
.door-tile.is-active .door-dot { background: var(--open); box-shadow: 0 0 0 4px var(--open-soft); }

/* offline = controller not responding */
.door-tile.is-offline { opacity: .55; cursor: not-allowed; box-shadow: none; }
.door-tile.is-offline:hover { transform: none; border-color: var(--border); }
.door-tile.is-offline .door-icon { background: var(--surface-2); color: var(--muted); }
.door-tile.is-offline .door-dot { background: var(--muted); box-shadow: none; }

/* lightbulb indicator next to a label */
.light-dot {
	display: inline-flex;
	align-items: center;
	margin-left: 6px;
	vertical-align: middle;
	color: #c2cad6;
	transition: color .2s ease, filter .2s ease;
}
.light-dot svg { width: 18px; height: 18px; }
.light-dot.is-active { color: var(--warn); filter: drop-shadow(0 0 5px var(--warn)); }
.light-dot.is-active svg { fill: rgba(230, 167, 0, .3); }

/* sending: spinner on the icon, block re-tap */
.door-tile.is-sending { pointer-events: none; }
.door-tile.is-sending .door-icon svg { opacity: 0; }
.door-tile.is-sending .door-icon::after {
	content: "";
	position: absolute;
	width: 24px; height: 24px;
	border: 3px solid rgba(47, 109, 246, .25);
	border-top-color: var(--accent);
	border-radius: 50%;
	animation: spin .7s linear infinite;
}

/* server received the command: green check */
.door-tile.is-sent { border-color: var(--ok); background: var(--ok-soft); }
.door-tile.is-sent .door-icon { background: var(--ok); color: #fff; }
.door-tile.is-sent .door-icon svg { opacity: 0; }
.door-tile.is-sent .door-icon::after {
	content: "✓";
	position: absolute;
	font-size: 28px;
	font-weight: 700;
	line-height: 1;
}

/* press feedback */
@keyframes tile-pulse {
	0% { box-shadow: 0 0 0 0 rgba(47, 109, 246, .35); }
	100% { box-shadow: 0 0 0 16px rgba(47, 109, 246, 0); }
}
.door-tile.pulse { animation: tile-pulse .6s ease-out; }

/* ---- Cards ---- */
.card {
	background: var(--surface);
	border: 1px solid var(--border);
	border-radius: var(--radius);
	box-shadow: var(--shadow-sm);
	padding: 22px;
}

/* ---- Active links (card list) ---- */
.link-list { display: flex; flex-direction: column; gap: 14px; }
.link-card {
	background: var(--surface);
	border: 1px solid var(--border);
	border-radius: var(--radius);
	box-shadow: var(--shadow-sm);
	padding: 16px;
}
.link-head {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 10px;
	margin-bottom: 12px;
}
.link-name { font-weight: 600; font-size: 16px; }
.link-meta { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.meta-row {
	display: flex;
	align-items: center;
	gap: 10px;
	font-size: 14px;
}
.meta-label {
	flex: 0 0 90px;
	color: var(--muted);
	font-size: 12px;
	text-transform: uppercase;
	letter-spacing: .03em;
}
.meta-row > span:last-child { min-width: 0; word-break: break-word; }

.link-cell { display: flex; }
.link-cell input {
	width: 100%;
	min-width: 0;
	border: 1px solid var(--border);
	border-radius: 8px;
	padding: 10px 12px;
	font-size: 13px;
	background: var(--surface-2);
	color: var(--text);
	cursor: pointer;
}
.link-actions {
	display: flex;
	align-items: center;
	gap: 16px;
	margin-top: 12px;
}
.badge {
	display: inline-block;
	padding: 3px 10px;
	border-radius: 999px;
	font-size: 12px;
	font-weight: 600;
}
.badge-ok { background: var(--ok-soft); color: var(--ok); }
.btn-link-danger { color: var(--open); font-weight: 500; }

/* ---- Forms ---- */
.form-field { margin-bottom: 14px; }
.form-field label {
	display: block;
	font-size: 13px;
	font-weight: 600;
	color: var(--muted);
	margin-bottom: 6px;
}
.input, select.input {
	width: 100%;
	border: 1px solid var(--border);
	border-radius: var(--radius-sm);
	padding: 12px 14px;
	font-size: 16px;
	background: var(--surface-2);
	color: var(--text);
	transition: border-color .15s ease, box-shadow .15s ease;
	appearance: none;
}
.input:focus, select.input:focus {
	outline: none;
	border-color: var(--accent);
	box-shadow: 0 0 0 3px var(--accent-soft);
	background: var(--surface);
}
.form-error {
	background: var(--open-soft);
	color: var(--open);
	border: 1px solid var(--open);
	border-radius: var(--radius-sm);
	padding: 10px 14px;
	margin-bottom: 16px;
	font-size: 14px;
	font-weight: 500;
}
.form-ok {
	background: var(--ok-soft);
	color: var(--ok);
	border: 1px solid var(--ok);
	border-radius: var(--radius-sm);
	padding: 10px 14px;
	margin-bottom: 16px;
	font-size: 14px;
	font-weight: 500;
}
.badge-admin { background: var(--accent-soft); color: var(--accent); }
.badge-muted { background: var(--surface-2); color: var(--muted); }
.inline-form { display: flex; gap: 8px; margin-bottom: 4px; }
.inline-form .input { flex: 1; min-width: 0; }
.inline-form .btn { width: auto; white-space: nowrap; }

.device-list {
	display: grid;
	grid-template-columns: 1fr 1fr;
	gap: 8px;
}
.device-item {
	display: flex;
	align-items: center;
	gap: 10px;
	padding: 11px 12px;
	border: 1px solid var(--border);
	border-radius: var(--radius-sm);
	background: var(--surface-2);
	cursor: pointer;
	font-size: 14px;
	font-weight: 500;
	transition: border-color .15s ease, background .15s ease;
}
.device-item:has(input:checked) {
	border-color: var(--accent);
	background: var(--accent-soft);
	color: var(--accent);
}
.device-item input { width: 18px; height: 18px; accent-color: var(--accent); margin: 0; }

@media (max-width: 380px) {
	.device-list { grid-template-columns: 1fr; }
}

.field-hint { display: block; color: var(--muted); font-size: 12px; margin-top: 6px; }

/* day-of-week chips (form) */
.day-chips { display: flex; gap: 6px; flex-wrap: wrap; }
.day-chip { position: relative; cursor: pointer; }
.day-chip input { position: absolute; opacity: 0; pointer-events: none; }
.day-chip span {
	display: grid;
	place-items: center;
	width: 40px; height: 40px;
	border: 1px solid var(--border);
	border-radius: 50%;
	background: var(--surface-2);
	font-weight: 600;
	font-size: 14px;
	color: var(--muted);
	transition: background .15s ease, color .15s ease, border-color .15s ease;
}
.day-chip input:checked + span {
	background: var(--accent);
	border-color: var(--accent);
	color: #fff;
}

/* day-of-week display (links table) */
.day-row { display: inline-flex; gap: 3px; }
.day-row .day {
	display: grid;
	place-items: center;
	width: 20px; height: 20px;
	border-radius: 50%;
	font-size: 11px;
	font-weight: 600;
	background: var(--surface-2);
	color: var(--muted);
}
.day-row .day.on { background: var(--accent); color: #fff; }

/* link-button (history toggle, etc.) */
.btn-link {
	background: none;
	border: none;
	padding: 0;
	font: inherit;
	color: var(--accent);
	font-weight: 500;
	cursor: pointer;
}
.btn-link:hover { text-decoration: underline; }

/* per-link history */
.history-row {
	margin-top: 12px;
	border-top: 1px solid var(--border);
	overflow-x: auto;
}
table.history { width: 100%; border-collapse: collapse; font-size: 13px; }
table.history th {
	text-align: left;
	color: var(--muted);
	font-size: 11px;
	text-transform: uppercase;
	letter-spacing: .03em;
	padding: 8px 12px;
	border-bottom: 1px solid var(--border);
}
table.history td { padding: 8px 12px; border-bottom: 1px solid var(--border); }
table.history tr:last-child td { border-bottom: 0; }
.history-empty { padding: 14px; color: var(--muted); font-size: 13px; }

.btn {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	gap: 8px;
	width: 100%;
	border: none;
	border-radius: var(--radius-sm);
	padding: 13px 18px;
	font-size: 16px;
	font-weight: 600;
	cursor: pointer;
	background: var(--accent);
	color: #fff;
	transition: filter .15s ease, transform .1s ease;
}
.btn:hover { filter: brightness(1.05); }
.btn:active { transform: scale(.99); }
.btn-secondary { background: var(--surface); color: var(--text); border: 1px solid var(--border); }

.auth-wrap {
	min-height: calc(100vh - 64px);
	display: grid;
	place-items: center;
	padding: 20px;
}
.auth-card {
	width: 100%;
	max-width: 380px;
	background: var(--surface);
	border: 1px solid var(--border);
	border-radius: var(--radius);
	box-shadow: var(--shadow);
	padding: 32px 28px;
}
.auth-card h1 { font-size: 20px; margin: 0 0 4px; text-align: center; }
.auth-card .sub { color: var(--muted); text-align: center; margin: 0 0 22px; font-size: 14px; }
.auth-icon {
	width: 54px; height: 54px;
	margin: 0 auto 14px;
	border-radius: 14px;
	background: var(--accent);
	color: #fff;
	display: grid; place-items: center;
	overflow: hidden;
	text-decoration: none;
}
.auth-icon img { width: 100%; height: 100%; display: block; }
.auth-icon:hover { text-decoration: none; filter: brightness(1.05); }
.auth-icon.warn { background: var(--warn); }
.auth-icon.danger { background: var(--open); }
.auth-message { text-align: center; color: var(--text); font-size: 15px; }
.auth-message .big { font-size: 17px; font-weight: 600; margin-bottom: 6px; }

/* ---- Toast / messages ---- */
.toast-stack {
	position: fixed;
	left: 50%;
	bottom: 24px;
	transform: translateX(-50%);
	z-index: 60;
	display: flex;
	flex-direction: column;
	gap: 10px;
	width: calc(100% - 40px);
	max-width: 420px;
}
.toast {
	background: var(--text);
	color: #fff;
	padding: 12px 16px;
	border-radius: 12px;
	box-shadow: var(--shadow);
	font-size: 14px;
	opacity: 0;
	transform: translateY(8px);
	transition: opacity .2s ease, transform .2s ease;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { background: var(--open); }
.toast.warn { background: var(--warn); }

/* ---- Controller status bar ---- */
.status-bar {
	position: fixed;
	bottom: 0; left: 0; right: 0;
	background: rgba(255, 255, 255, .9);
	backdrop-filter: blur(10px);
	-webkit-backdrop-filter: blur(10px);
	border-top: 1px solid var(--border);
	padding: 8px 20px;
	display: flex;
	gap: 16px;
	justify-content: center;
	flex-wrap: wrap;
	font-size: 13px;
	color: var(--muted);
	z-index: 40;
}
.status-item { display: inline-flex; align-items: center; gap: 6px; }
.status-item .dot {
	width: 8px; height: 8px; border-radius: 50%;
	background: var(--muted);
}
.status-item.online .dot { background: var(--ok); box-shadow: 0 0 6px var(--ok); }
.status-item.online { color: var(--text); }

/* ---- Loading overlay ---- */
#overlay {
	position: fixed;
	inset: 0;
	background: rgba(243, 245, 249, .6);
	backdrop-filter: blur(2px);
	display: none;
	place-items: center;
	z-index: 70;
}
#overlay.show { display: grid; }
.spinner {
	width: 42px; height: 42px;
	border: 4px solid var(--border);
	border-top-color: var(--accent);
	border-radius: 50%;
	animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.footer-note {
	text-align: center;
	color: var(--muted);
	font-size: 13px;
	margin-top: 40px;
}

@media (max-width: 480px) {
	.container { padding: 20px 14px 96px; }
	.door-grid { grid-template-columns: repeat(2, 1fr); gap: 12px; }
}
