:root {
  --bg: #0b1215;
  --bg-elev: #121c21;
  --bg-soft: #182429;
  --line: #2a3a42;
  --ink: #e8f0f2;
  --muted: #7a939c;
  --copper: #c47a3a;
  --copper-hot: #e0944f;
  --phosphor: #3db8a0;
  --warn: #d45b4a;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
  --radius: 14px;
  --font-ui: "Outfit", system-ui, sans-serif;
  --font-display: "Source Serif 4", Georgia, serif;
  --font-mono: "IBM Plex Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  color: var(--ink);
  font-family: var(--font-ui);
  background:
    radial-gradient(1200px 600px at 10% -10%, rgba(196, 122, 58, 0.12), transparent 55%),
    radial-gradient(900px 500px at 90% 0%, rgba(61, 184, 160, 0.08), transparent 50%),
    linear-gradient(180deg, #0e171b 0%, var(--bg) 40%, #091014 100%);
  min-height: 100%;
}

.noise {
  pointer-events: none;
  position: fixed;
  inset: 0;
  opacity: 0.04;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  z-index: 0;
}

.trace {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  height: 56px;
  width: 100%;
  opacity: 0.55;
  z-index: 1;
  pointer-events: none;
}

/* Author display rules must not defeat [hidden] */
[hidden] {
  display: none !important;
}

.gate, .app { position: relative; z-index: 2; }

.gate {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  position: relative;
  overflow: hidden;
}

.gate-visual {
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
}
.gate-visual img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: 72% center;
  opacity: 0.88;
  filter: saturate(1.05) contrast(1.08);
}
.gate-visual::after {
  content: "";
  position: absolute;
  inset: 0;
  background:
    linear-gradient(90deg, rgba(9, 16, 20, 0.88) 0%, rgba(9, 16, 20, 0.55) 38%, rgba(9, 16, 20, 0.18) 100%),
    radial-gradient(ellipse at 28% 48%, rgba(11, 18, 21, 0.4), transparent 58%);
}

body.authed .gate,
body:not(.authed) .app {
  display: none !important;
}

body:not(.authed) .trace {
  opacity: 0.35;
}

.gate-panel {
  position: relative;
  z-index: 1;
  width: min(420px, 100%);
  background: color-mix(in srgb, var(--bg-elev) 88%, transparent);
  border: 1px solid var(--line);
  border-radius: calc(var(--radius) + 4px);
  padding: 36px 32px 28px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(12px);
  animation: rise 480ms ease both;
}

.gate-mark {
  display: block;
  width: 56px;
  height: 56px;
  border-radius: 14px;
  margin-bottom: 18px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}

@keyframes rise {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: none; }
}

.eyebrow {
  margin: 0 0 8px;
  text-transform: uppercase;
  letter-spacing: 0.16em;
  font-size: 0.72rem;
  color: var(--copper);
  font-weight: 600;
}

h1 {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: clamp(2rem, 4vw, 2.5rem);
  letter-spacing: -0.02em;
}

.lede { color: var(--muted); margin: 10px 0 28px; line-height: 1.5; }

.login-form, .stack-form, .inline-form {
  display: grid;
  gap: 14px;
}

label { display: grid; gap: 6px; font-size: 0.92rem; color: var(--muted); }
label span em { color: var(--ink); font-style: normal; font-family: var(--font-mono); }

input {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  border-radius: 10px;
  padding: 12px 14px;
  font: inherit;
  outline: none;
  transition: border-color 160ms ease, box-shadow 160ms ease;
}
input:focus {
  border-color: color-mix(in srgb, var(--copper) 70%, var(--line));
  box-shadow: 0 0 0 3px rgba(196, 122, 58, 0.18);
}

.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--bg-soft);
  color: var(--ink);
  border-radius: 999px;
  padding: 11px 18px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: transform 120ms ease, background 160ms ease, border-color 160ms ease;
}
.btn:hover { transform: translateY(-1px); }
.btn:focus-visible { outline: 2px solid var(--phosphor); outline-offset: 2px; }
.btn.primary {
  background: linear-gradient(180deg, var(--copper-hot), var(--copper));
  border-color: transparent;
  color: #1a1008;
}
.btn.ghost { background: transparent; }
.btn.warn {
  background: color-mix(in srgb, var(--warn) 18%, var(--bg-soft));
  border-color: color-mix(in srgb, var(--warn) 45%, var(--line));
  color: #ffd5cf;
}

.error {
  margin: 0;
  color: #ffb4a8;
  background: color-mix(in srgb, var(--warn) 16%, transparent);
  border: 1px solid color-mix(in srgb, var(--warn) 40%, transparent);
  border-radius: 10px;
  padding: 10px 12px;
}
.flash {
  margin: 0 0 12px;
  color: var(--phosphor);
  font-size: 0.92rem;
}

.app {
  display: grid;
  grid-template-columns: 240px 1fr;
  min-height: 100vh;
}

.rail {
  border-right: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg-elev) 88%, transparent);
  padding: 28px 18px;
  display: flex;
  flex-direction: column;
  gap: 28px;
}

.brand { display: flex; gap: 12px; align-items: center; }
.brand-mark {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  display: block;
  object-fit: cover;
  border: 1px solid var(--line);
  box-shadow: inset 0 0 18px rgba(61, 184, 160, 0.18);
  background: var(--bg-soft);
}
.brand strong { display: block; font-size: 1rem; }
.brand small { color: var(--muted); font-family: var(--font-mono); font-size: 0.72rem; }

.nav { display: grid; gap: 6px; }
.nav-btn {
  text-align: left;
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 10px 12px;
  border-radius: 10px;
  font: inherit;
  font-weight: 500;
  cursor: pointer;
}
.nav-btn:hover { background: var(--bg-soft); color: var(--ink); }
.nav-btn.active {
  background: color-mix(in srgb, var(--copper) 16%, var(--bg-soft));
  color: var(--ink);
  box-shadow: inset 3px 0 0 var(--copper);
}
.logout { margin-top: auto; }

.main { padding: 28px 28px 48px; }
.top {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: end;
  margin-bottom: 22px;
}
.top h2 {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: clamp(1.6rem, 2.4vw, 2rem);
}
.top-meta { display: flex; gap: 10px; align-items: center; }

.pill {
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 7px 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg-soft);
}
.pill.ok {
  color: #baf5e6;
  border-color: color-mix(in srgb, var(--phosphor) 50%, var(--line));
  background: color-mix(in srgb, var(--phosphor) 14%, var(--bg-soft));
}
.pill.bad {
  color: #ffd0c8;
  border-color: color-mix(in srgb, var(--warn) 50%, var(--line));
  background: color-mix(in srgb, var(--warn) 14%, var(--bg-soft));
}

.stat-row {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.hero-strip {
  position: relative;
  margin: 0 0 16px;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--line);
  min-height: 148px;
  background: var(--bg-elev);
  animation: rise 520ms ease both;
}
.hero-strip img {
  display: block;
  width: 100%;
  height: clamp(140px, 22vw, 220px);
  object-fit: cover;
  object-position: center 45%;
  opacity: 0.9;
}
.hero-strip::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, rgba(9, 16, 20, 0.82) 0%, rgba(9, 16, 20, 0.35) 55%, rgba(9, 16, 20, 0.15) 100%);
  pointer-events: none;
}
.hero-strip figcaption {
  position: absolute;
  left: 18px;
  bottom: 16px;
  z-index: 1;
  display: grid;
  gap: 4px;
}
.hero-strip figcaption strong {
  font-family: var(--font-display);
  font-size: 1.25rem;
  font-weight: 600;
}
.stat {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 16px 14px;
}
.stat-label {
  display: block;
  color: var(--muted);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 8px;
}
.stat strong {
  font-size: 1.35rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.split {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}
.panel {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
}
.panel h3 {
  margin: 0 0 14px;
  font-size: 1rem;
  font-weight: 600;
}
.hint { color: var(--muted); font-size: 0.88rem; line-height: 1.45; margin: 0 0 14px; }

.kv {
  display: grid;
  grid-template-columns: 140px 1fr;
  gap: 8px 12px;
  margin: 0;
  font-size: 0.92rem;
}
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-family: var(--font-mono); font-size: 0.84rem; word-break: break-all; }

.mono {
  margin: 0;
  white-space: pre-wrap;
  font-family: var(--font-mono);
  font-size: 0.78rem;
  line-height: 1.45;
  color: #b7c8ce;
  max-height: 360px;
  overflow: auto;
}

.inline-form {
  grid-template-columns: 1fr auto;
  align-items: end;
  margin-bottom: 16px;
}
.device-list { display: grid; gap: 10px; }
.device {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  align-items: center;
  padding: 14px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--bg-elev);
  animation: rise 360ms ease both;
}
.device h4 { margin: 0 0 4px; font-size: 1.05rem; }
.device .meta {
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: 0.78rem;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}
.device-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: end; }
.dot {
  width: 8px; height: 8px; border-radius: 50%; display: inline-block;
  background: var(--muted);
  margin-right: 6px;
}
.dot.on { background: var(--phosphor); box-shadow: 0 0 10px rgba(61, 184, 160, 0.7); }

.log-list {
  display: grid;
  gap: 8px;
  max-height: 360px;
  overflow: auto;
}
.log-item {
  display: grid;
  grid-template-columns: 92px 1fr;
  gap: 8px;
  font-size: 0.82rem;
  padding-bottom: 8px;
  border-bottom: 1px solid color-mix(in srgb, var(--line) 70%, transparent);
}
.log-item time { color: var(--muted); font-family: var(--font-mono); font-size: 0.74rem; }
.log-item strong { font-weight: 600; }
.log-item span { color: var(--muted); font-family: var(--font-mono); }

#bw-chart {
  width: 100%;
  height: auto;
  background: #0a1114;
  border-radius: 10px;
  border: 1px solid var(--line);
}

.danger-zone { margin-top: 14px; }

dialog {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg-elev);
  color: var(--ink);
  padding: 0;
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(4, 8, 10, 0.72); }
.qr-box { padding: 20px; display: grid; gap: 14px; justify-items: center; }
.qr-box img {
  width: min(280px, 70vw);
  height: auto;
  background: #fff;
  padding: 12px;
  border-radius: 12px;
}

@media (max-width: 960px) {
  .app { grid-template-columns: 1fr; }
  .rail {
    border-right: 0;
    border-bottom: 1px solid var(--line);
    padding-bottom: 16px;
  }
  .nav { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .logout { margin-top: 0; }
  .stat-row, .split, .inline-form { grid-template-columns: 1fr; }
  .device { grid-template-columns: 1fr; }
  .device-actions { justify-content: start; }
  .gate-visual img { object-position: 85% center; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation: none !important;
    transition: none !important;
  }
}
