:root {
  --bg: #0f1115; --panel: #171a21; --panel-2: #1c2029; --line: #262b36;
  --text: #e7e9ee; --muted: #98a0b0; --accent: #4f8cff; --accent-ink: #fff;
  --ok: #35c47b; --warn: #e5a13a; --bad: #e2564a;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
* { box-sizing: border-box; }
/* The hidden attribute must always win, even over class rules that set a
   display value (e.g. the modal's display:grid). Without this the modal and the
   login/app toggle stay visible when they should be hidden. */
[hidden] { display: none !important; }
body {
  margin: 0; background: var(--bg); color: var(--text); min-height: 100vh;
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
button { font: inherit; cursor: pointer; border: none; border-radius: 8px; }
input {
  font: inherit; width: 100%; padding: 10px 12px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--bg); color: var(--text);
}
input:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }

.brand { font-weight: 700; font-size: 17px; letter-spacing: -0.01em; }
.brand-mark { margin-right: 4px; }

.primary { background: var(--accent); color: var(--accent-ink); padding: 10px 16px; font-weight: 600; }
.primary:hover { opacity: 0.9; }
.primary.small, .ghost.small { padding: 7px 12px; font-size: 13px; }
.ghost { background: transparent; color: var(--text); border: 1px solid var(--line); padding: 9px 14px; }
.ghost:hover { background: var(--panel-2); }
.danger { background: transparent; color: var(--bad); border: 1px solid var(--line); }
.danger:hover { background: rgba(226,86,74,0.12); }
.icon-btn { background: transparent; color: var(--muted); font-size: 16px; padding: 4px 8px; }
.icon-btn:hover { color: var(--text); }
.error { color: var(--bad); font-size: 13px; margin: 4px 0 0; }
.empty { color: var(--muted); text-align: center; padding: 24px; }

/* ---------------- login ---------------- */
.login-screen { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--panel);
  border: 1px solid var(--line); border-radius: 16px; padding: 28px;
}
.login-card .brand { font-size: 22px; }
.login-sub { color: var(--muted); font-size: 14px; margin: 6px 0 22px; }
.login-card label { display: block; font-size: 12px; color: var(--muted); margin: 14px 0 6px; font-weight: 500; }
.login-card button { width: 100%; margin-top: 20px; }

/* ---------------- topbar ---------------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 22px; border-bottom: 1px solid var(--line); background: var(--panel);
  position: sticky; top: 0; z-index: 5;
}
.topbar-right { display: flex; align-items: center; gap: 14px; }
.whoami { color: var(--muted); font-size: 13px; }
.content { max-width: 1100px; margin: 0 auto; padding: 24px 22px 60px; }

/* ---------------- cards ---------------- */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 14px; margin-bottom: 26px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px 18px; }
.card .num { font-size: 28px; font-weight: 700; letter-spacing: -0.02em; }
.card .lbl { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em; margin-top: 2px; }
.card .num.ok { color: var(--ok); }

/* ---------------- panels ---------------- */
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 18px 20px; margin-bottom: 22px; }
.panel-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.panel-head h2 { font-size: 16px; margin: 0; }
.panel-actions { display: flex; gap: 8px; }

/* ---------------- users table ---------------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); font-size: 14px; }
th { color: var(--muted); font-weight: 500; font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; }
tr:last-child td { border-bottom: none; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 100px; font-size: 11px; font-weight: 600; }
.badge.admin { background: rgba(79,140,255,0.15); color: var(--accent); }

/* ---------------- devices ---------------- */
.devices-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 14px; }
.device {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 12px; padding: 14px 16px;
}
.device-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.device-name { font-weight: 600; }
.device-owner { color: var(--muted); font-size: 12px; margin-top: 1px; }
.status { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; }
.status .dot { width: 8px; height: 8px; border-radius: 50%; }
.status.online .dot { background: var(--ok); }
.status.online { color: var(--ok); }
.status.on_unreachable .dot { background: var(--warn); }
.status.on_unreachable { color: var(--warn); }
.status.offline .dot { background: var(--muted); }
.status.offline { color: var(--muted); }
.device-meta { margin-top: 12px; display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; font-size: 12px; }
.device-meta dt { color: var(--muted); }
.device-meta dd { margin: 0; text-align: right; font-family: var(--mono); font-size: 11.5px; }
.device-actions { margin-top: 12px; display: flex; justify-content: flex-end; }

/* ---------------- modal ---------------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.55); display: grid; place-items: center; z-index: 20; padding: 20px; }
.modal { width: 100%; max-width: 420px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 18px; border-bottom: 1px solid var(--line); }
.modal-head h3 { margin: 0; font-size: 16px; }
.modal-body { padding: 18px; }
.modal-body label { display: block; font-size: 12px; color: var(--muted); margin: 12px 0 6px; font-weight: 500; }
.modal-body label:first-child { margin-top: 0; }
.modal-body .row { display: flex; gap: 8px; align-items: center; }
.modal-body .checkbox { display: flex; align-items: center; gap: 8px; margin-top: 14px; }
.modal-body .checkbox input { width: auto; }
.modal-actions { margin-top: 20px; display: flex; justify-content: flex-end; gap: 8px; }
.codebox {
  font-family: var(--mono); font-size: 20px; letter-spacing: 2px; text-align: center;
  background: var(--bg); border: 1px dashed var(--line); border-radius: 10px; padding: 16px; margin: 8px 0;
  user-select: all;
}
.hint { color: var(--muted); font-size: 13px; }
.copy-row { display: flex; gap: 8px; align-items: center; }
.copy-row input { font-family: var(--mono); }

.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--line); color: var(--text);
  padding: 11px 18px; border-radius: 10px; font-size: 14px; z-index: 40;
  box-shadow: 0 8px 30px rgba(0,0,0,0.4);
}
.toast.err { border-color: var(--bad); color: #ffb4ad; }

@media (max-width: 560px) {
  .content { padding: 16px; }
  .whoami { display: none; }
}
