:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --line: #e3e8ef;
  --text: #1f2937;
  --muted: #6b7280;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --ok: #16a34a;
  --warn: #dc2626;
  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", "Microsoft YaHei", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 13px; }
.small-note { font-weight: 400; font-size: 12px; }

/* ---------- auth (SENTRY / CROPTIVA branded) ---------- */
.auth-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at 50% 16%, #242a24 0%, #0b0d0c 46%, #070908 100%);
}
.auth-card {
  width: 400px;
  background: #161a17;
  border: 1px solid rgba(255, 179, 0, .38);
  border-radius: 18px;
  padding: 30px 28px 26px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, .55);
}
.auth-brand { display: flex; align-items: center; gap: 14px; margin-bottom: 22px; }
.auth-logo { width: 62px; height: 62px; object-fit: contain; }
.auth-brand-name {
  color: #ffb300; font-size: 30px; font-weight: 800;
  letter-spacing: 5px; line-height: 1.1;
}
.auth-brand-sub {
  color: #9da69c; font-size: 10px; letter-spacing: 2px; margin-top: 4px;
}
.auth-tabs {
  display: flex; gap: 8px; margin-bottom: 18px;
  border-bottom: 1px solid #2e352f;
}
.tab {
  flex: 1; padding: 10px 8px; border: none;
  background: transparent; color: #9da69c;
  font-size: 14px; font-weight: 600; cursor: pointer;
  border-bottom: 2px solid transparent;
}
.tab.active { color: #ffb300; border-bottom-color: #ffb300; }
.auth-form { display: flex; flex-direction: column; gap: 14px; }
.auth-form label { font-size: 13px; color: #9da69c; display: flex; flex-direction: column; gap: 6px; }
.auth-form input, .sn-dialog input {
  padding: 11px 12px;
  border: 1px solid #2e352f;
  background: #1f2420;
  color: #f2f3ee;
  border-radius: 10px;
  font-size: 14px;
  outline: none;
}
.auth-form input::placeholder { color: #6c756b; }
.auth-form input:focus, .sn-dialog input:focus { border-color: #ffb300; }
.auth-form .btn.primary {
  background: #ffb300; color: #0b0d0c; font-weight: 800;
  letter-spacing: 3px; padding: 12px; margin-top: 4px;
}
.auth-form .btn.primary:hover { background: #ffc233; }
.auth-form .btn.ghost { background: #1f2420; color: #ffb300; border-color: #2e352f; }
.auth-form a { color: #ffc233; }
.agree-row { flex-direction: row !important; align-items: center; gap: 8px !important; }
.agree-row input { width: auto; }
.code-row { display: flex; gap: 8px; }
.code-row input { flex: 1; }
.form-err { color: #ff6b6b; font-size: 13px; }
.auth-hint { font-size: 12px; color: #6c756b; text-align: center; }

/* ---------- buttons ---------- */
.btn {
  padding: 9px 14px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  transition: opacity .15s;
}
.btn:hover { opacity: .88; }
.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.ghost { background: #f1f5f9; color: var(--text); border: 1px solid var(--line); }
.btn.small { padding: 5px 10px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ---------- layout ---------- */
.topbar {
  height: 52px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar-title { font-weight: 600; color: #b8860b; display: flex; align-items: center; gap: 9px; }
.topbar-logo { width: 28px; height: 28px; object-fit: contain; }
.topbar-right { display: flex; align-items: center; gap: 10px; }
.badge {
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 999px;
  background: #f1f5f9;
  color: var(--muted);
}
.badge.on { background: #dcfce7; color: var(--ok); }
.badge.off { background: #fee2e2; color: var(--warn); }
.badge.user { background: #e0e7ff; color: var(--primary-dark); }

.layout {
  display: flex;
  min-height: calc(100vh - 52px);
}

/* ---------- sidebar ---------- */
.sidebar {
  width: 300px;
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
}
.side-head { padding: 14px; display: flex; gap: 8px; }
.sn-filter {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  outline: none;
}
.sn-filter:focus { border-color: var(--primary); }
.device-list {
  list-style: none;
  margin: 0;
  padding: 0;
  flex: 1;
  overflow-y: auto;
}
.device-list li {
  padding: 12px 16px;
  border-top: 1px solid var(--line);
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.device-list li:hover { background: #f8fafc; }
.device-list li.active { background: #eff6ff; border-left: 3px solid var(--primary); }
.device-list .sn { font-weight: 600; font-size: 14px; }
.device-list .sub { font-size: 12px; color: var(--muted); }
.side-actions { padding: 14px; display: flex; gap: 8px; }
.side-actions .btn { flex: 1; }

/* ---------- main ---------- */
.main { flex: 1; padding: 24px; }
.empty {
  height: 60vh;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
}
.detail-head { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; }
.detail-head h2 { margin: 0; }

h3 { margin: 26px 0 12px; font-size: 15px; }

.live-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 12px;
}
.live-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.live-card .k { font-size: 12px; color: var(--muted); }
.live-card .v { font-size: 26px; font-weight: 600; margin-top: 4px; }
.live-card .u { font-size: 12px; color: var(--muted); margin-left: 4px; }
.live-raw { margin-top: 10px; word-break: break-all; }

.hist-toolbar { display: flex; align-items: center; gap: 14px; margin-bottom: 10px; }
.hist-toolbar label { font-size: 13px; color: var(--muted); }
.hist-toolbar select { padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; }

.table {
  width: 100%;
  border-collapse: collapse;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.table th, .table td {
  padding: 9px 12px;
  border-bottom: 1px solid var(--line);
  font-size: 13px;
  text-align: left;
}
.table th { background: #f8fafc; color: var(--muted); font-weight: 600; }
.table tr:last-child td { border-bottom: none; }

.alarm-list { display: flex; flex-direction: column; gap: 8px; }
.alarm-item {
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 13px;
}
.alarm-item .t { color: var(--warn); font-weight: 600; margin-right: 8px; }

/* ---------- dialog / toast ---------- */
.sn-dialog {
  border: none;
  border-radius: var(--radius);
  padding: 22px;
  width: 360px;
}
.sn-dialog::backdrop { background: rgba(15, 23, 42, .35); }
.sn-dialog h3 { margin: 0 0 14px; }
.sn-dialog input { width: 100%; }
.sn-hint { margin-top: 6px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }

.toast {
  position: fixed;
  bottom: 28px;
  left: 50%;
  transform: translateX(-50%);
  background: #1f2937;
  color: #fff;
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 14px;
  z-index: 100;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .25);
}

/* ---------- agreement ---------- */
.agree-row {
  display: flex !important;
  flex-direction: row !important;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--muted);
}
.agree-row input { width: auto; }
.agree-row a { color: var(--primary); text-decoration: none; }
.agree-row a:hover { text-decoration: underline; }
.agree-dialog { width: 520px; max-width: 92vw; }
.agree-text {
  max-height: 46vh;
  overflow-y: auto;
  font-size: 13px;
  line-height: 1.7;
  color: var(--text);
}
.agree-text p { margin: 8px 0; }

/* ---------- view tabs ---------- */
.topbar-viewtabs { display: flex; gap: 4px; margin-left: 28px; }
.viewtab {
  padding: 6px 16px;
  border: 1px solid var(--line);
  background: #f8fafc;
  color: var(--muted);
  border-radius: 8px;
  font-size: 13px;
  cursor: pointer;
}
.viewtab.active { background: var(--primary); color: #fff; border-color: var(--primary); }

/* ---------- scene management ---------- */
.scene-wrap { display: flex; min-height: calc(100vh - 52px); }
.scene-side {
  width: 260px;
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
}
.scene-side .side-head strong { font-size: 15px; }
.scene-list { list-style: none; margin: 0; padding: 0; flex: 1; overflow-y: auto; }
.scene-list-item {
  padding: 12px 16px;
  border-top: 1px solid var(--line);
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
}
.scene-list-item:hover { background: #f8fafc; }
.scene-list-item.active { background: #eff6ff; border-left: 3px solid var(--primary); }
.scene-list-count { font-size: 12px; color: var(--muted); }
.scene-list-empty { padding: 16px; }
.scene-main { flex: 1; padding: 24px; overflow-y: auto; }
.scene-form-row { display: flex; align-items: flex-end; gap: 12px; }
.scene-form-row label { font-size: 13px; color: var(--muted); display: flex; flex-direction: column; gap: 6px; }
.scene-input {
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 14px;
  outline: none;
  background: #fff;
}
.scene-input:focus { border-color: var(--primary); }
.btn.danger { color: var(--warn); }

.scene-device-picker {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 8px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
}
.scene-dev-check { display: flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; }
.scene-dev-check input { cursor: pointer; }

.scene-params {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
}
.scene-targets { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.scene-targets label { font-size: 12px; color: var(--muted); display: flex; flex-direction: column; gap: 6px; }
.scene-rules-head { display: flex; align-items: center; justify-content: space-between; margin: 18px 0 8px; font-size: 14px; }
.scene-rules { display: flex; flex-direction: column; gap: 8px; }
.scene-rule-row { display: flex; gap: 8px; align-items: center; }
.scene-rule-row select, .scene-rule-row input {
  padding: 7px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 13px;
  outline: none;
}
.scene-rule-row select:focus, .scene-rule-row input:focus { border-color: var(--primary); }
.scene-rule-val { width: 90px; }
.scene-rule-action { flex: 1; }
.scene-params-actions { display: flex; align-items: center; gap: 12px; margin-top: 16px; }
.scene-danger { margin-top: 28px; padding-top: 16px; border-top: 1px solid var(--line); }

/* ---------- footer ---------- */
.site-footer {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 20px 14px;
  font-size: 12px;
  color: var(--muted);
}

/* ---------- mobile ---------- */
@media (max-width: 768px) {
  .topbar-viewtabs { margin-left: 8px; }
  .viewtab { padding: 5px 10px; font-size: 12px; }
  .layout, .scene-wrap { flex-direction: column; }
  .scene-side { width: 100%; border-right: none; border-bottom: 1px solid var(--line); max-height: 30vh; }
  .scene-main { padding: 14px; }
  .sidebar {
    width: 100%;
    border-right: none;
    border-bottom: 1px solid var(--line);
    max-height: 40vh;
  }
  .main { padding: 14px; }
  .topbar { padding: 0 12px; }
  .topbar-title { font-size: 14px; }
  #userBadge { max-width: 40vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .auth-card { width: 92vw; max-width: 380px; }
  .live-card .v { font-size: 22px; }
  .hist-toolbar { flex-wrap: wrap; gap: 8px; }
  /* wide tables scroll horizontally instead of breaking layout */
  #histTable, #userTable { display: block; overflow-x: auto; white-space: nowrap; }
}

/* ---------- sidebar: grouping (user -> device hierarchy) ---------- */
.side-group { display: flex; justify-content: flex-end; padding: 0 14px 8px; }
.side-group label { display: flex; align-items: center; gap: 6px; font-size: 12px; }
.side-group select {
  padding: 4px 6px; border: 1px solid var(--line); border-radius: 8px;
  font-size: 12px; background: var(--panel); color: var(--text);
}
.device-list li.group-head {
  position: sticky; top: 0; z-index: 1;
  display: flex; justify-content: space-between;
  padding: 7px 16px; cursor: default;
  background: #f1f5f9; color: var(--muted);
  font-size: 12px; font-weight: 600;
  border-top: 1px solid var(--line);
}
.device-list li.group-head:hover { background: #f1f5f9; }

/* ---------- device type tree (device -> module / peripheral) ---------- */
.type-tree {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 12px 14px; font-size: 13px;
}
.type-tree.muted { padding: 14px; }
.type-tree .tree-device { display: flex; gap: 10px; align-items: baseline; margin-bottom: 6px; }
.type-tree .tree-group { font-weight: 600; margin: 12px 0 4px; }
.type-tree .tree-group .muted { font-weight: 400; }
.type-tree .tree-slots {
  list-style: none; margin: 0; padding: 2px 0 2px 14px;
  border-left: 1px dashed var(--line);
}
.type-tree .tree-slots li { display: flex; align-items: center; gap: 10px; padding: 5px 0; }
.type-tree .slot { width: 56px; color: var(--muted); font-size: 12px; }
.type-tree .name { min-width: 88px; font-weight: 600; }
.type-tree .dot { width: 8px; height: 8px; border-radius: 50%; background: #cbd5e1; }
.type-tree .dot.on { background: var(--ok); }
.type-tree .dot.err { background: var(--warn); }
.type-tree .dot.warn { background: #d97706; }
.type-tree .state { font-size: 12px; color: var(--muted); }

/* ---------- history detail table ---------- */
.subhead { font-size: 13px; color: var(--muted); font-weight: 600; margin: 16px 0 8px; }.table-scroll { max-height: 420px; overflow: auto; border-radius: var(--radius); }
.table-scroll .table { border-radius: 0; }
.table-scroll .table thead th { position: sticky; top: 0; z-index: 1; }
.table td.mono {
  font-family: Consolas, "Courier New", monospace;
  font-size: 12px; color: var(--muted); white-space: nowrap;
}

/* ---------- history trend chart (PC mirror of the APP TrendChart) ---------- */
.trend-chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.trend-chips .chip {
  padding: 5px 14px; border-radius: 999px; border: 1px solid var(--line);
  background: #f1f5f9; color: var(--muted); font-size: 12px; cursor: pointer;
}
.trend-chips .chip:hover { background: #e8eef6; }
.trend-chips .chip.active {
  background: #2db7a3; border-color: #2db7a3; color: #fff; font-weight: 600;
}
.trend-chart-card {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 8px 6px 4px;
}
#trendCanvas {
  width: 100%; height: 280px; display: block;
  touch-action: none; cursor: grab;
}
#trendCanvas:active { cursor: grabbing; }
.trend-sel {
  margin-top: 10px; background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 10px 16px;
  display: flex; align-items: center; gap: 16px;
}
.trend-sel .t { color: var(--muted); font-size: 13px; white-space: nowrap; }
.trend-sel .f { color: var(--muted); font-size: 13px; }
.trend-sel .v { font-size: 22px; font-weight: 700; color: #2db7a3; }
.trend-sel .close {
  margin-left: auto; border: none; background: transparent;
  color: var(--muted); font-size: 18px; line-height: 1; cursor: pointer;
  padding: 2px 6px;
}
.trend-sel .close:hover { color: var(--text); }
.trend-hint { margin-top: 10px; font-size: 12px; }
