:root {
  color-scheme: dark;

  /* Surface */
  --bg: #0b1418;
  --panel: #121c21;
  --panel-raised: #182429;
  --border: #253138;
  --border-strong: #34434b;

  /* Text */
  --text: #eef3f2;
  --text-secondary: #9fb3ae;

  /* Relay on/off — warm/cool "active/inactive" metaphor, no product-specific meaning */
  --on: #f2a53c;
  --on-strong: #ffcf7a;
  --on-soft: rgba(242, 165, 60, 0.18);
  --off: #2f6f7a;
  --off-strong: #4a8b96;
  --off-soft: rgba(47, 111, 122, 0.16);

  /* Connectivity — separate from relay on/off state */
  --online: #4caf82;
  --offline: var(--text-secondary);

  /* Generic interactive chrome — separate from domain colors */
  --accent: #6ea8fe;
  --accent-strong: #8fc0ff;

  /* Feedback */
  --danger: #e8756a;
  --danger-strong: #ff8f83;

  /* Type */
  --font-display: 'Space Grotesk', 'Segoe UI', sans-serif;
  --font-body: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --mono: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;

  /* Spacing */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --space-8: 32px;

  /* Radius / shadow */
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-full: 999px;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.35);
  --shadow-md: 0 6px 20px rgba(0, 0, 0, 0.3);
  --focus-ring: 0 0 0 3px rgba(110, 168, 254, 0.35);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-bottom: 40px;
}

a { color: var(--accent); }

/* ─── Focus / motion ──────────────────────────────────────────────────── */

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  box-shadow: var(--focus-ring);
  border-radius: inherit;
}

button, .device-card, .relay-toggle {
  transition: background-color 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease, transform 0.1s ease;
}

/* ─── Header ──────────────────────────────────────────────────────────── */

.app-header {
  position: sticky;
  top: 0;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-4);
  padding: var(--space-4) var(--space-6);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  z-index: 10;
}
.app-header-inner {
  width: 100%;
  max-width: 1100px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
}
.app-header-brand {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}
.app-header-mark {
  width: 22px;
  height: 22px;
  color: var(--on);
  flex-shrink: 0;
}
.app-header-title {
  font-family: var(--font-display);
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 0.01em;
}
.app-header-summary {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: 13px;
  color: var(--text-secondary);
}

/* ─── Layout ──────────────────────────────────────────────────────────── */

.wrap {
  width: 100%;
  max-width: 960px;
  padding: var(--space-6) var(--space-5) 0;
}
@media (min-width: 1100px) {
  .wrap { max-width: 1100px; }
}

.panel {
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.025), rgba(255, 255, 255, 0)), var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: var(--space-6);
  margin-bottom: var(--space-5);
}

.eyebrow {
  font-family: var(--mono);
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-secondary);
  margin: 0 0 var(--space-2);
}

h1 { font-family: var(--font-display); font-size: 21px; font-weight: 600; margin: 0 0 var(--space-5); }
h2 { font-family: var(--font-display); font-size: 16px; font-weight: 600; margin: 0; }

/* ─── Form controls ───────────────────────────────────────────────────── */

input[type="password"], input[type="text"], input[type="time"], select {
  padding: 10px 12px;
  min-height: 44px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 14.5px;
  transition: border-color 0.15s ease;
}
input[type="password"] { width: 100%; margin-bottom: var(--space-3); }
input[type="text"] { width: 100%; }
input:focus { border-color: var(--accent); }
input[type="time"]::-webkit-calendar-picker-indicator { filter: invert(1) opacity(0.6); }

select {
  appearance: none;
  padding-right: 34px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' fill='none' stroke='%239fb3ae' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
}

button {
  padding: 12px 20px;
  min-height: 44px;
  border-radius: var(--radius-sm);
  border: none;
  font-size: 14.5px;
  font-weight: 600;
  font-family: var(--font-body);
  cursor: pointer;
  background: var(--accent);
  color: #0b1418;
}
button:hover:not(:disabled) { transform: translateY(-1px); }
button:disabled { opacity: 0.5; cursor: default; transform: none; }
button.secondary { background: transparent; border: 1px solid var(--border-strong); color: var(--text); }

.relay-toggle { background: var(--on); color: #241503; }
.relay-toggle.off-state { background: var(--off); color: var(--text); }
.relay-toggle:not(.off-state):not(:disabled) { box-shadow: 0 0 14px var(--on-soft); }

.error { color: var(--danger); font-size: 13px; margin: -4px 0 var(--space-3); min-height: 16px; }
.hint { color: var(--text-secondary); font-size: 12px; margin: var(--space-2) 0 0; }
.saved-note { font-size: 12px; color: var(--text-secondary); margin-left: 10px; }
.muted { color: var(--text-secondary); font-size: 13px; }

/* ─── Table ───────────────────────────────────────────────────────────── */

.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; min-width: 560px; border-collapse: collapse; font-family: var(--mono); font-size: 12px; }
th, td { text-align: left; padding: var(--space-2) 6px; border-bottom: 1px solid var(--border); }
th { color: var(--text-secondary); font-weight: 500; }

.filter-row { display: flex; align-items: center; gap: var(--space-2); margin-bottom: var(--space-3); font-size: 14px; }
.filter-row .label { color: var(--text-secondary); }

.hidden { display: none; }

/* ─── Device status ───────────────────────────────────────────────────── */

.status-dot {
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--offline);
  flex-shrink: 0;
}
.status-dot.online { background: var(--online); box-shadow: 0 0 8px var(--online); }
.status-label { font-size: 12px; color: var(--text-secondary); }
.status-label.online { color: var(--online); }

/* ─── Device cards ────────────────────────────────────────────────────── */

#device-list {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--space-4);
}
@media (min-width: 760px) {
  #device-list { grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); }
}

.device-card {
  border: 1px solid var(--border);
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.02), rgba(255, 255, 255, 0)), var(--panel-raised);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  padding: var(--space-4);
}
.device-card:hover { box-shadow: var(--shadow-md); }

.device-card-header {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-bottom: var(--space-4);
  flex-wrap: wrap;
}

.relay-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-4);
}
@media (max-width: 640px) {
  .relay-grid { grid-template-columns: 1fr; }
}

.relay-block {
  border-left: 3px solid var(--off);
  padding-left: var(--space-3);
}
.relay-block.is-on { border-left-color: var(--on); }

.relay-block-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-3);
}

.enable-toggle {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: 12px;
  color: var(--text-secondary);
  margin: 2px 0 var(--space-3);
  cursor: pointer;
  padding: var(--space-1) 0;
}
.enable-toggle input { accent-color: var(--on); width: 20px; height: 20px; }

.time-row { display: flex; gap: var(--space-3); margin-bottom: var(--space-1); }
.time-row label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-secondary); flex: 1; }

.timeline-track {
  position: relative;
  height: 12px;
  border-radius: var(--radius-full);
  background: var(--off);
  border: 1px solid var(--border);
  overflow: hidden;
  margin: var(--space-3) 0 4px;
  transition: opacity 0.2s, filter 0.2s;
}
.timeline-segment {
  position: absolute;
  top: 0;
  bottom: 0;
  background: linear-gradient(90deg, var(--on), var(--on-strong));
}
.timeline-labels {
  display: flex;
  justify-content: space-between;
  font-family: var(--mono);
  font-size: 10px;
  color: var(--text-secondary);
}
.timeline-caption {
  font-size: 12px;
  color: var(--text-secondary);
  margin: var(--space-1) 0 var(--space-3);
}
.relay-block.schedule-disabled .timeline-track,
.relay-block.schedule-disabled .time-row {
  opacity: 0.45;
  filter: grayscale(0.7);
}

.register-row { display: flex; gap: var(--space-2); margin-top: 4px; flex-wrap: wrap; }
.register-row input { flex: 1; min-width: 140px; }
.empty-note { color: var(--text-secondary); font-size: 13px; }

/* ─── Loading states ──────────────────────────────────────────────────── */

.boot-loading {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 100;
}
.boot-loading.hidden { display: none; }
.spinner {
  width: 28px;
  height: 28px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.skeleton {
  background: linear-gradient(90deg, var(--panel-raised) 25%, var(--border) 50%, var(--panel-raised) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s ease-in-out infinite;
  border-radius: var(--radius-md);
  min-height: 160px;
}
tr.skeleton td { border-bottom: none; padding: 6px; }
tr.skeleton { background: none; }
tr.skeleton td::before {
  content: '';
  display: block;
  height: 14px;
  border-radius: var(--radius-sm);
  background: linear-gradient(90deg, var(--panel-raised) 25%, var(--border) 50%, var(--panel-raised) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s ease-in-out infinite;
}
@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}
