/* Admin SPA stylesheet — dark, modern, responsive from tablet width upwards.
   No external fonts or CDNs: system-ui stack only. */

:root {
  --bg: #0e1320;
  --bg-2: #151c2e;
  --bg-3: #1c2540;
  --bg-hover: #212c4d;
  --border: #2a3554;
  --border-soft: #222c49;
  --text: #e9edf6;
  --text-dim: #94a1bf;
  --text-faint: #64718f;
  --accent: #4f8ef7;
  --accent-soft: rgba(79, 142, 247, 0.14);
  --ok: #2ec98c;
  --ok-soft: rgba(46, 201, 140, 0.15);
  --warn: #f4a63a;
  --warn-soft: rgba(244, 166, 58, 0.14);
  --err: #f15b5b;
  --err-soft: rgba(241, 91, 91, 0.14);
  --radius: 10px;
  --radius-sm: 7px;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  --sidebar-w: 232px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
}

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

h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: 0.01em; }
h1 { font-size: 1.45rem; }
h2 { font-size: 1.1rem; }
h3 { font-size: 1rem; }

.boot, .noscript {
  padding: 3rem;
  text-align: center;
  color: var(--text-dim);
}

/* ---------- App layout ---------- */

#app { min-height: 100vh; }

.layout {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 100vh;
}

.sidebar {
  background: var(--bg-2);
  border-right: 1px solid var(--border-soft);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand {
  padding: 1.1rem 1.2rem 0.9rem;
  border-bottom: 1px solid var(--border-soft);
}
.brand .brand-title { font-weight: 700; font-size: 1.02rem; }
.brand .brand-sub { color: var(--text-faint); font-size: 0.78rem; margin-top: 2px; }

.nav {
  flex: 1;
  overflow-y: auto;
  padding: 0.7rem 0.6rem;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.nav a {
  display: block;
  padding: 0.52rem 0.75rem;
  border-radius: var(--radius-sm);
  color: var(--text-dim);
  font-weight: 500;
}
.nav a:hover { background: var(--bg-hover); color: var(--text); text-decoration: none; }
.nav a.active { background: var(--accent-soft); color: var(--accent); }

.sidebar-foot {
  border-top: 1px solid var(--border-soft);
  padding: 0.8rem 1rem;
  font-size: 0.83rem;
  color: var(--text-dim);
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.sidebar-foot .who { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-foot .role-tag { color: var(--text-faint); font-size: 0.75rem; }
/* Server version footer (SPEC-VERSIONING §8) — deliberately unobtrusive. */
.sidebar-foot .version-tag { color: var(--text-faint); font-size: 0.72rem; }

.main {
  padding: 1.4rem 1.8rem 3rem;
  min-width: 0;
  max-width: 1280px;
  width: 100%;
}

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.1rem;
  flex-wrap: wrap;
}
.page-head .sub { color: var(--text-dim); font-size: 0.88rem; margin-top: 3px; }
.page-actions { display: flex; gap: 0.5rem; flex-wrap: wrap; }

/* ---------- Topbar (tenant switcher, superadmin only) ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
  background: var(--bg-2);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 0.45rem 0.8rem;
  margin-bottom: 1.1rem;
  font-size: 0.88rem;
}
.topbar .topbar-label {
  color: var(--text-dim);
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.topbar select { width: auto; min-width: 180px; padding: 0.35rem 0.5rem; }
.topbar .topbar-spacer { flex: 1; }

/* ---------- Banner ---------- */

.banner {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0.7rem 1rem;
  border-radius: var(--radius);
  margin-bottom: 1.1rem;
  font-size: 0.92rem;
}
.banner.warn {
  background: var(--warn-soft);
  border: 1px solid rgba(244, 166, 58, 0.4);
  color: var(--warn);
}
.banner.warn a { color: var(--warn); font-weight: 650; text-decoration: underline; }
.banner.err {
  background: var(--err-soft);
  border: 1px solid rgba(241, 91, 91, 0.4);
  color: var(--err);
}
.banner.err a { color: var(--err); font-weight: 650; text-decoration: underline; }

/* ---------- Cards & tiles ---------- */

.card {
  background: var(--bg-2);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 1.1rem 1.2rem;
  margin-bottom: 1.1rem;
}
.card > h2, .card > h3 { margin-bottom: 0.8rem; }
.card .card-sub { color: var(--text-dim); font-size: 0.85rem; margin: -0.5rem 0 0.9rem; }

.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 0.9rem;
  margin-bottom: 1.2rem;
}
.tile {
  background: var(--bg-2);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 0.95rem 1.05rem;
  display: block;
  color: inherit;
}
a.tile:hover { text-decoration: none; border-color: var(--accent); }
.tile .tile-label { color: var(--text-dim); font-size: 0.8rem; }
.tile .tile-value { font-size: 1.7rem; font-weight: 700; margin-top: 0.2rem; }
.tile .tile-extra { color: var(--text-faint); font-size: 0.78rem; margin-top: 0.25rem; }
.tile.warn .tile-value { color: var(--warn); }
.tile.err .tile-value { color: var(--err); }
.tile.ok .tile-value { color: var(--ok); }

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.1rem;
  align-items: start;
}
@media (max-width: 1100px) { .grid-2 { grid-template-columns: 1fr; } }

/* ---------- Buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  background: var(--bg-3);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 0.45rem 0.85rem;
  font-size: 0.88rem;
  font-family: inherit;
  font-weight: 550;
  cursor: pointer;
  white-space: nowrap;
  text-decoration: none;
}
.btn:hover { background: var(--bg-hover); text-decoration: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.1); }
.btn.danger { background: transparent; border-color: rgba(241, 91, 91, 0.5); color: var(--err); }
.btn.danger:hover { background: var(--err-soft); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--text-dim); }
.btn.ghost:hover { background: var(--bg-hover); color: var(--text); }
.btn.small { padding: 0.25rem 0.55rem; font-size: 0.8rem; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-row { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: center; }

.icon-btn {
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 1rem;
  cursor: pointer;
  padding: 0.3rem 0.5rem;
  border-radius: var(--radius-sm);
  line-height: 1;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }
.icon-btn:disabled { opacity: 0.3; cursor: not-allowed; }

/* ---------- Tables ---------- */

.table-wrap { overflow-x: auto; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}
th {
  text-align: left;
  color: var(--text-faint);
  font-weight: 600;
  font-size: 0.76rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 0.5rem 0.7rem;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
td {
  padding: 0.58rem 0.7rem;
  border-bottom: 1px solid var(--border-soft);
  vertical-align: middle;
}
tbody tr:hover { background: rgba(255, 255, 255, 0.02); }
td.empty { text-align: center; color: var(--text-faint); padding: 1.4rem; }
td.num, th.num { text-align: right; }
td.actions { text-align: right; white-space: nowrap; }
td .mono, .mono { font-family: var(--mono); font-size: 0.85em; }

/* Software version cell of the device list (SPEC-VERSIONING §8). */
.version-cell { display: inline-flex; align-items: center; gap: 0.4rem; white-space: nowrap; }
.version-cell .mono { color: var(--text-dim); }

/* ---------- Status dots & badges ---------- */

.dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-right: 2px;
  vertical-align: baseline;
}
.dot.on { background: var(--ok); box-shadow: 0 0 6px rgba(46, 201, 140, 0.7); }
.dot.off { background: var(--text-faint); }

.badge {
  display: inline-block;
  padding: 0.12rem 0.5rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  background: var(--bg-3);
  color: var(--text-dim);
  border: 1px solid var(--border-soft);
  white-space: nowrap;
}
.badge.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.err { background: var(--err-soft); color: var(--err); border-color: transparent; }
.badge.accent { background: var(--accent-soft); color: var(--accent); border-color: transparent; }

/* ---------- Forms ---------- */

.field { display: flex; flex-direction: column; gap: 0.3rem; margin-bottom: 0.85rem; }
.field .field-label { font-size: 0.82rem; color: var(--text-dim); font-weight: 600; }
.field .hint { font-size: 0.76rem; color: var(--text-faint); }

input[type="text"], input[type="password"], input[type="email"], input[type="number"],
input[type="date"], input[type="url"], select, textarea {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.65rem;
  font-family: inherit;
  font-size: 0.92rem;
  width: 100%;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79, 142, 247, 0.18);
}
input[type="date"] { color-scheme: dark; }
select { appearance: auto; }
textarea { resize: vertical; min-height: 90px; }
input[type="file"] { color: var(--text-dim); font-size: 0.88rem; }
input::placeholder, textarea::placeholder { color: var(--text-faint); }

.check-field {
  display: flex;
  align-items: flex-start;
  gap: 0.55rem;
  margin-bottom: 0.85rem;
  cursor: pointer;
  font-size: 0.92rem;
}
.check-field input[type="checkbox"] {
  width: 17px; height: 17px;
  margin-top: 2px;
  accent-color: var(--accent);
  flex-shrink: 0;
}
.check-field .check-text { display: flex; flex-direction: column; gap: 0.15rem; }
.check-field .hint { font-size: 0.76rem; color: var(--text-faint); }
.check-field.emph {
  background: var(--accent-soft);
  border: 1px solid rgba(79, 142, 247, 0.35);
  border-radius: var(--radius-sm);
  padding: 0.6rem 0.7rem;
}

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 1rem; }
.form-grid .span-2 { grid-column: span 2; }
@media (max-width: 640px) { .form-grid { grid-template-columns: 1fr; } .form-grid .span-2 { grid-column: auto; } }

.filter-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr auto;
  gap: 0 1rem;
  align-items: end;
}
@media (max-width: 900px) { .filter-grid { grid-template-columns: 1fr 1fr; } }

.form-error { color: var(--err); font-size: 0.86rem; margin: 0.3rem 0 0.6rem; }
.form-info { color: var(--text-dim); font-size: 0.83rem; margin: 0.3rem 0 0.6rem; }

/* ---------- Toasts ---------- */

.toasts {
  position: fixed;
  right: 1.2rem;
  bottom: 1.2rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  z-index: 1000;
  max-width: min(420px, calc(100vw - 2rem));
}
.toast {
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  color: var(--text);
  padding: 0.65rem 0.95rem;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  font-size: 0.9rem;
  animation: toast-in 0.22s ease;
}
.toast.ok { border-left-color: var(--ok); }
.toast.err { border-left-color: var(--err); }
.toast.warn { border-left-color: var(--warn); }
.toast.out { opacity: 0; transition: opacity 0.3s; }
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---------- Modal ---------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(5, 8, 16, 0.72);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 4vh 1rem;
  z-index: 500;
  overflow-y: auto;
}
.modal {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: 100%;
  max-width: 540px;
  box-shadow: var(--shadow);
  animation: toast-in 0.18s ease;
}
.modal.wide { max-width: 820px; }
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.9rem 1.1rem;
  border-bottom: 1px solid var(--border-soft);
}
.modal-body { padding: 1.1rem; max-height: 70vh; overflow-y: auto; }
.modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: 0.6rem;
  padding: 0.85rem 1.1rem;
  border-top: 1px solid var(--border-soft);
}

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

.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background:
    radial-gradient(1000px 500px at 20% -10%, rgba(79, 142, 247, 0.12), transparent),
    radial-gradient(800px 500px at 90% 110%, rgba(124, 92, 255, 0.1), transparent),
    var(--bg);
}
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg-2);
  border: 1px solid var(--border-soft);
  border-radius: 14px;
  padding: 2rem 1.8rem;
  box-shadow: var(--shadow);
}
.login-card h1 { font-size: 1.25rem; margin-bottom: 0.2rem; }
.login-card .login-sub { color: var(--text-dim); font-size: 0.87rem; margin-bottom: 1.4rem; }
.login-card .btn { width: 100%; margin-top: 0.4rem; padding: 0.6rem; }

/* ---------- Progress / enroll ---------- */

.progress {
  height: 8px;
  background: var(--bg);
  border: 1px solid var(--border-soft);
  border-radius: 999px;
  overflow: hidden;
}
.progress > div {
  height: 100%;
  background: var(--accent);
  border-radius: 999px;
  width: 0%;
  transition: width 0.25s linear;
}
.progress.ok > div { background: var(--ok); }

.enroll-status {
  text-align: center;
  padding: 1rem 0.5rem;
}
.enroll-status .enroll-big { font-size: 1.05rem; font-weight: 650; margin-bottom: 0.4rem; }
.enroll-status .enroll-count {
  font-size: 2.2rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  margin: 0.5rem 0;
}
.enroll-status.ok .enroll-count, .enroll-status.ok .enroll-big { color: var(--ok); }
.enroll-status.err .enroll-big { color: var(--err); }

.spinner {
  display: inline-block;
  width: 16px; height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Misc ---------- */

.kv { display: grid; grid-template-columns: 180px 1fr; gap: 0.35rem 1rem; font-size: 0.92rem; }
.kv dt { color: var(--text-dim); }
.kv dd { margin: 0; word-break: break-all; }

.token-box {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.7rem;
  font-family: var(--mono);
  font-size: 0.85rem;
  word-break: break-all;
}
.token-box code { flex: 1; }

.muted { color: var(--text-faint); }
.dim { color: var(--text-dim); }
.small-text { font-size: 0.82rem; }
.mt { margin-top: 1rem; }
.mb0 { margin-bottom: 0; }

.energy-tiles {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 0.6rem;
}
.energy-tile {
  background: var(--bg);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  padding: 0.6rem 0.7rem;
}
.energy-tile .et-label { color: var(--text-faint); font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.04em; }
.energy-tile .et-value { font-size: 1.15rem; font-weight: 700; margin-top: 0.15rem; font-variant-numeric: tabular-nums; }
.energy-tiles.stale .energy-tile .et-value { color: var(--text-faint); }

.preview-box {
  background: var(--bg);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  padding: 0.6rem;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 200px;
}
.preview-box img, .preview-box video { max-width: 100%; max-height: 60vh; border-radius: 4px; }
.preview-box iframe { width: 100%; height: 55vh; border: none; background: #fff; border-radius: 4px; }
.preview-box .preview-text { font-size: 1.3rem; text-align: center; padding: 1.5rem; white-space: pre-wrap; }

pre.code {
  background: var(--bg);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  padding: 0.8rem;
  font-family: var(--mono);
  font-size: 0.82rem;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-word;
}

.pl-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  background: var(--bg);
  margin-bottom: 0.45rem;
}
.pl-item .pl-pos { color: var(--text-faint); font-variant-numeric: tabular-nums; width: 1.6rem; text-align: right; }
.pl-item .pl-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pl-item input.pl-dur { width: 75px; text-align: right; }

/* ---------- Responsive (tablet width and up is the target) ---------- */

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar {
    position: static;
    height: auto;
    border-right: none;
    border-bottom: 1px solid var(--border-soft);
  }
  .nav {
    flex-direction: row;
    flex-wrap: wrap;
    padding: 0.5rem 0.8rem;
  }
  .sidebar-foot { flex-direction: row; align-items: center; justify-content: space-between; }
  .main { padding: 1rem 1rem 2.5rem; }
  .kv { grid-template-columns: 1fr; gap: 0.1rem 0; }
  .kv dt { margin-top: 0.5rem; }
}
