:root {
  color-scheme: light;
  --bg: #eef2f6;
  --bg-elevated: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #d8e0ea;
  --line-soft: #e8eef5;
  --accent: #0f766e;
  --accent-hover: #0d9488;
  --accent-soft: #e6f5f3;
  --accent-ink: #115e59;
  --danger: #b91c1c;
  --danger-soft: #fef2f2;
  --warn: #b45309;
  --warn-soft: #fff7ed;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.04), 0 8px 24px rgba(15, 23, 42, 0.06);
  --radius: 12px;
  --radius-sm: 8px;
  --topbar-h: 64px;
  --font: "Plus Jakarta Sans", "Noto Sans SC", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-family: var(--font);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background:
    linear-gradient(180deg, #f8fafc 0%, var(--bg) 180px),
    var(--bg);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font: inherit; color: inherit; }

button {
  border: 1px solid var(--line);
  background: var(--bg-elevated);
  color: var(--ink);
  border-radius: var(--radius-sm);
  padding: 8px 14px;
  cursor: pointer;
  font-weight: 500;
  transition: border-color 0.15s ease, background 0.15s ease, color 0.15s ease;
}

button:hover { border-color: #94a3b8; }
button:disabled { opacity: 0.45; cursor: not-allowed; }

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
button.primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

button.ghost {
  background: transparent;
  border-color: transparent;
  color: var(--muted);
}
button.ghost:hover {
  background: var(--line-soft);
  color: var(--ink);
  border-color: transparent;
}

button.wide { width: 100%; }
button.danger-text { color: var(--danger); }
button.danger-text:hover { background: var(--danger-soft); color: var(--danger); }

.hidden { display: none !important; }

.brand-mark {
  margin: 0;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--accent);
}

.section-kicker {
  margin: 0 0 6px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent);
}

.error { min-height: 1.2em; margin: 8px 0 0; color: var(--danger); font-size: 13px; }
.hint, .lede, .main-head p, .wall-toolbar p, .rail-head p, .page-head .lede {
  color: var(--muted);
  line-height: 1.55;
}
.lede { margin: 0 0 8px; }

h1, h2, h3 { margin: 0; letter-spacing: -0.02em; }
h1 { font-size: 28px; font-weight: 700; }
h2 { font-size: 22px; font-weight: 650; }

label {
  display: grid;
  gap: 6px;
  margin: 0;
  font-size: 13px;
  font-weight: 500;
  color: var(--ink);
}

.check-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 4px;
}
.check-row input { width: auto; }

input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.12);
}
textarea { resize: vertical; line-height: 1.5; }

/* —— Login —— */
.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 32px 20px;
  background:
    radial-gradient(ellipse 80% 60% at 10% 20%, rgba(15, 118, 110, 0.12), transparent 55%),
    linear-gradient(160deg, #f8fafc, #eef2f6);
}

.login-card {
  width: min(380px, 100%);
  padding: 32px 28px;
  display: grid;
  gap: 14px;
  background: var(--bg-elevated);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
}
.login-card .primary { margin-top: 4px; padding: 12px 16px; }

/* —— App shell —— */
.app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  min-height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 0 28px;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 180px;
}
.brand-text {
  display: grid;
  gap: 2px;
}
.brand-text strong {
  font-size: 15px;
  font-weight: 650;
}
.pill {
  display: inline-flex;
  width: fit-content;
  padding: 2px 8px;
  border-radius: 6px;
  background: var(--accent-soft);
  color: var(--accent-ink);
  font-size: 11px;
  font-weight: 600;
}

.nav {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px;
  background: var(--bg);
  border: 1px solid var(--line-soft);
  border-radius: 10px;
}
.nav-btn {
  border: 0;
  background: transparent;
  border-radius: 7px;
  padding: 8px 14px;
  color: var(--muted);
  font-weight: 560;
}
.nav-btn:hover {
  background: rgba(255, 255, 255, 0.8);
  color: var(--ink);
  border-color: transparent;
}
.nav-btn.active {
  background: var(--bg-elevated);
  color: var(--accent-ink);
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.06);
}

.top-actions {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
  min-width: 180px;
}

.count {
  color: var(--muted);
  background: var(--bg);
  border: 1px solid var(--line-soft);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
}

.banner {
  margin: 16px 28px 0;
  padding: 12px 16px;
  background: var(--warn-soft);
  color: var(--warn);
  border: 1px solid #fde68a;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
}

/* —— Devices workspace —— */
.workspace {
  flex: 1;
  display: grid;
  grid-template-columns: 260px 1fr;
  min-height: 0;
  gap: 0;
}

.side-rail {
  position: sticky;
  top: var(--topbar-h);
  align-self: start;
  border-right: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.7);
  padding: 22px 16px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  height: auto;
  max-height: calc(100vh - var(--topbar-h));
  min-width: 0;
  overflow: hidden;
  transition: padding 0.2s ease;
}

.rail-toggle {
  position: absolute;
  top: 26px;
  right: -11px;
  z-index: 6;
  width: 22px;
  height: 40px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--bg-elevated);
  color: var(--muted);
  font-size: 18px;
  line-height: 1;
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.08);
  cursor: pointer;
  display: grid;
  place-items: center;
}
.rail-toggle:hover {
  color: var(--accent-ink);
  border-color: var(--accent);
  background: var(--accent-soft);
}

.workspace.is-rail-collapsed {
  grid-template-columns: 28px 1fr;
}
.workspace.is-rail-collapsed .side-rail {
  padding: 22px 0 28px;
  overflow: hidden;
}
.workspace.is-rail-collapsed .side-rail > *:not(.rail-toggle) {
  visibility: hidden;
  opacity: 0;
  pointer-events: none;
}
.workspace.is-rail-collapsed .rail-toggle {
  right: 3px;
  left: 3px;
  width: 22px;
}

.rail-head { flex: 0 0 auto; }
.rail-head h2 { font-size: 16px; }
.rail-head p { margin: 6px 0 0; font-size: 12px; }

.tree-col {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding-right: 2px;
}
.tree-col button {
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  border-radius: 8px;
  padding: 9px 12px;
  color: var(--ink);
  font-weight: 500;
}
.tree-col button.tree-item {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}
.tree-col .tree-label {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tree-col .tree-count {
  flex: 0 0 auto;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  font-weight: 500;
}
.tree-col button.child {
  padding-left: 22px;
  color: var(--muted);
  font-size: 13px;
}
.tree-col button:hover { background: var(--line-soft); }
.tree-col button.active {
  background: var(--accent-soft);
  color: var(--accent-ink);
  font-weight: 600;
}
.tree-col button.active .tree-count {
  color: var(--accent-ink);
  opacity: 0.85;
}
.title-count {
  margin-left: 6px;
  font-size: 0.72em;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  vertical-align: baseline;
}

.group-tools {
  display: grid;
  gap: 8px;
  flex: 0 0 auto;
  margin-top: 4px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}
.stack-form {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
}
.tool-row {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 6px;
}

.main-pane {
  padding: 28px 32px 40px;
  min-width: 0;
}

.main-head, .wall-toolbar, .page-head {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
  flex-wrap: wrap;
  margin-bottom: 20px;
}
.main-head h2, .wall-toolbar h2, .page-head h2 { margin-bottom: 6px; }
.main-head p, .wall-toolbar p { margin: 0; max-width: 52ch; font-size: 14px; }

.device-list {
  width: 100%;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg-elevated);
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.03);
}

.device-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.device-table th,
.device-table td {
  padding: 8px 12px;
  text-align: left;
  border-bottom: 1px solid var(--line-soft);
  vertical-align: middle;
  white-space: nowrap;
}
.device-table th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: #f8fafc;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.02em;
}
.device-table tbody tr:hover { background: #f8fafc; }
.device-table tbody tr:last-child td { border-bottom: 0; }
.device-table .mono {
  font-variant-numeric: tabular-nums;
  font-family: ui-monospace, "Cascadia Mono", "Consolas", monospace;
  font-size: 12px;
}
.device-table .muted { color: var(--muted); }
.device-table .group-select {
  min-width: 140px;
  width: auto;
  padding: 5px 8px;
  font-size: 12px;
}
.device-table .ops-cell {
  display: flex;
  gap: 6px;
  align-items: center;
}
.device-table .btn-sm {
  padding: 4px 10px;
  font-size: 12px;
  white-space: nowrap;
}
.row-offline td { opacity: 0.72; }

.badge {
  border-radius: 6px;
  padding: 3px 8px;
  font-size: 11px;
  font-weight: 650;
  letter-spacing: 0.02em;
  white-space: nowrap;
}
.badge.on { background: var(--accent-soft); color: var(--accent-ink); }
.badge.watch { background: #fff7ed; color: #c2410c; }
.badge.off { background: var(--line-soft); color: var(--muted); }

.status-cell {
  display: flex;
  align-items: center;
  gap: 8px;
  white-space: nowrap;
}
.watch-users {
  color: var(--muted);
  font-size: 12px;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.name-btn {
  border: 0;
  background: transparent;
  padding: 0;
  font-weight: 650;
  font-size: 13px;
  border-radius: 0;
  text-align: left;
  color: var(--ink);
}
.name-btn:hover:not(:disabled) {
  color: var(--accent);
  border-color: transparent;
  background: transparent;
}
.name-btn:disabled { opacity: 1; cursor: default; }

.empty-state {
  margin: 0;
  padding: 48px 24px;
  text-align: center;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.55);
  border: 1px dashed var(--line);
  border-radius: var(--radius);
}

/* —— Wall —— */
.wall-workspace {
  display: block;
  padding-bottom: 32px;
}
.wall-toolbar { padding: 28px 32px 0; }
.wall-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}
.wall-controls select {
  width: auto;
  min-width: 180px;
}

.wall-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  padding: 16px 32px 0;
}
.wall-cell {
  background: #0b1220;
  border-radius: 10px;
  overflow: hidden;
  min-height: 150px;
  position: relative;
  border: 1px solid #1e293b;
  aspect-ratio: 16 / 10;
}
.wall-cell img, .wall-cell canvas {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  background: #0b1220;
}
.wall-cell .meta {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 8px 10px;
  background: linear-gradient(transparent, rgba(2, 6, 23, 0.85));
  color: #f1f5f9;
  font-size: 12px;
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: center;
}
.wall-cell .meta button {
  padding: 4px 8px;
  font-size: 12px;
  background: rgba(255, 255, 255, 0.12);
  border-color: rgba(255, 255, 255, 0.18);
  color: #fff;
}
.wall-cell .wait {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: #94a3b8;
  font-size: 13px;
  padding: 12px;
  text-align: center;
}
.wall-cell.empty {
  background: rgba(255, 255, 255, 0.45);
  border: 1px dashed var(--line);
}

/* —— Settings pages —— */
.page-shell {
  width: min(760px, 100%);
  margin: 0 auto;
  padding: 32px 28px 48px;
}
.page-shell.narrow { width: min(480px, 100%); }

.settings-card {
  background: var(--bg-elevated);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px;
  display: grid;
  gap: 16px;
}

.form-actions { display: flex; gap: 8px; }

.update-scope {
  margin: 4px 0 0;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  display: grid;
  gap: 8px;
}
.update-scope legend {
  padding: 0 4px;
  font-size: 13px;
  font-weight: 600;
}
.update-group-list {
  display: grid;
  gap: 4px;
  max-height: 220px;
  overflow: auto;
  padding: 8px 10px;
  border: 1px solid var(--line-soft);
  border-radius: var(--radius-sm);
  background: #f8fafc;
}
.update-group-list .check-row {
  margin: 0;
  font-size: 13px;
  font-weight: 400;
}
.update-group-list .is-room {
  padding-left: 18px;
  color: var(--muted);
}

.keyword-assign-list {
  display: grid;
  gap: 10px;
}
.keyword-assign-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: flex-start;
  padding: 12px 14px;
  border: 1px solid var(--line-soft);
  border-radius: var(--radius-sm);
  background: #f8fafc;
}
.keyword-assign-main {
  display: grid;
  gap: 4px;
  min-width: 0;
}
.keyword-assign-main strong {
  font-size: 13px;
  font-weight: 650;
}
.keyword-assign-main span {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
  word-break: break-word;
}
.keyword-assign-row .badge { flex-shrink: 0; }
.field-block { display: grid; gap: 8px; }
.field-label { font-size: 13px; font-weight: 500; }

.user-form {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px;
}
.user-form .field-block,
.user-form .form-actions,
.user-form #user-edit-hint {
  grid-column: 1 / -1;
}
.user-form-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}
.user-row .ops-cell {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.user-groups {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.user-groups label {
  margin: 0;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  font-size: 13px;
  font-weight: 500;
}
.user-groups input { width: auto; }

.list-divider {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 4px 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.list-divider::before,
.list-divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--line-soft);
}

.user-list { display: grid; gap: 8px; }
.user-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  padding: 12px 14px;
  border: 1px solid var(--line-soft);
  border-radius: 10px;
  background: #f8fafc;
  flex-wrap: wrap;
  font-size: 14px;
}

/* —— Viewer —— */
body.viewer-page {
  min-height: 100vh;
  background: #0b1220;
  color: #e2e8f0;
}
.viewer-shell {
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr;
  padding: 12px;
  gap: 10px;
}
.viewer-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 10px;
}
.viewer-top .eyebrow,
.viewer-top .section-kicker { color: #5eead4; }
.viewer-actions { display: flex; gap: 8px; align-items: center; }
.viewer-page button {
  background: rgba(255, 255, 255, 0.06);
  border-color: rgba(255, 255, 255, 0.12);
  color: #e2e8f0;
}
.viewer-page .count {
  background: rgba(255, 255, 255, 0.06);
  border-color: rgba(255, 255, 255, 0.1);
  color: #94a3b8;
}
.stage {
  position: relative;
  background: #020617;
  border-radius: 12px;
  min-height: 0;
  height: 100%;
  display: grid;
  place-items: center;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.06);
}
.stage img, .stage canvas {
  width: 100%;
  max-height: calc(100vh - 90px);
  object-fit: contain;
  display: none;
}
.stage img.show, .stage canvas.show { display: block; }
#viewer-wait { color: #94a3b8; }

.toasts {
  position: fixed;
  right: 16px;
  bottom: 16px;
  display: grid;
  gap: 8px;
  z-index: 40;
}
.toast {
  background: var(--ink);
  color: #fff;
  border-radius: 10px;
  padding: 12px 14px;
  max-width: 360px;
  font-size: 13px;
  box-shadow: var(--shadow);
  animation: toast-in 0.22s ease;
}
@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

@media (max-width: 1100px) {
  .wall-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 900px) {
  .workspace { grid-template-columns: 1fr; }
  .workspace.is-rail-collapsed { grid-template-columns: 1fr; }
  .side-rail {
    position: relative;
    top: auto;
    max-height: none;
    overflow: visible;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }
  .rail-toggle { display: none; }
  .workspace.is-rail-collapsed .side-rail > *:not(.rail-toggle) {
    visibility: visible;
    opacity: 1;
    pointer-events: auto;
  }
  .tree-col {
    flex-direction: row;
    flex-wrap: wrap;
    overflow: visible;
  }
  .tree-col button { width: auto; }
  .topbar {
    flex-wrap: wrap;
    padding: 12px 16px;
    height: auto;
  }
  .brand, .top-actions { min-width: 0; }
  .nav { width: 100%; order: 3; overflow-x: auto; }
  .main-pane, .wall-toolbar, .wall-grid, .page-shell {
    padding-left: 16px;
    padding-right: 16px;
  }
  .banner { margin-left: 16px; margin-right: 16px; }
  .user-form { grid-template-columns: 1fr; }
}

@media (max-width: 640px) {
  .wall-grid { grid-template-columns: 1fr; }
  .login-card { padding: 24px 18px; }
}
