:root {
  --bg: #0d1117;
  --bg-2: #11161d;
  --panel: #161b22;
  --panel-2: #1c2330;
  --border: #2a3342;
  --border-2: #3a4557;
  --text: #e6edf3;
  --muted: #8b98a9;
  --accent: #2f81f7;
  --accent-2: #58a6ff;
  --green: #3fb950;
  --red: #f85149;
  --yellow: #d29922;
  --radius: 10px;
  --shadow: 0 8px 30px rgba(0,0,0,.45);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

/* ---------- Layout ---------- */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 230px; flex-shrink: 0;
  background: var(--bg-2);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh;
}
.brand {
  padding: 18px 20px; font-size: 18px; font-weight: 700;
  letter-spacing: .3px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 10px;
}
.brand .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--green); box-shadow: 0 0 8px var(--green); }
.nav { flex: 1; overflow-y: auto; padding: 10px; }
.nav-group { font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); padding: 14px 10px 6px; }
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px; border-radius: 8px; cursor: pointer;
  color: var(--muted); margin-bottom: 2px; user-select: none;
  transition: background .12s, color .12s;
}
.nav-item:hover { background: var(--panel); color: var(--text); }
.nav-item.active { background: var(--accent); color: #fff; }
.nav-item .ico { width: 18px; text-align: center; font-size: 15px; }
.sidebar-foot { padding: 14px 20px; border-top: 1px solid var(--border); font-size: 12px; color: var(--muted); }
.sidebar-foot .who { color: var(--text); font-weight: 600; }
.sidebar-foot .role { text-transform: capitalize; }
.logout { margin-top: 8px; color: var(--red); cursor: pointer; }
.logout:hover { text-decoration: underline; }

.main { flex: 1; padding: 24px 28px; min-width: 0; }
.page-head { margin-bottom: 20px; }
.page-head h1 { font-size: 22px; font-weight: 700; }
.page-head .sub { color: var(--muted); margin-top: 2px; }

/* ---------- Cards / panels ---------- */
.panel {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 18px; margin-bottom: 18px;
}
.panel-title { font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .6px; margin-bottom: 12px; }

/* ---------- Toolbar / filters ---------- */
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 16px; }
.toolbar .spacer { flex: 1; }
input, select, textarea, button {
  font-family: inherit; font-size: 14px; color: var(--text);
  background: var(--bg-2); border: 1px solid var(--border-2);
  border-radius: 8px; padding: 8px 12px; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(47,129,247,.15); }
input[type="date"], input[type="datetime-local"] { color-scheme: dark; }
select option { background: var(--panel-2); }
button { cursor: pointer; transition: background .12s, border-color .12s; }
button:hover { border-color: var(--accent); }
.btn { background: var(--panel-2); border: 1px solid var(--border-2); }
.btn:hover { background: var(--border); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn-primary:hover { background: var(--accent-2); }
.btn-danger { background: transparent; border-color: var(--red); color: var(--red); }
.btn-danger:hover { background: rgba(248,81,73,.12); }
.btn-sm { padding: 5px 10px; font-size: 13px; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn-ghost:hover { color: var(--text); border-color: var(--border-2); }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
thead th {
  text-align: left; padding: 10px 12px; background: var(--panel-2);
  color: var(--muted); font-weight: 600; font-size: 12px;
  text-transform: uppercase; letter-spacing: .4px; white-space: nowrap;
  border-bottom: 1px solid var(--border);
}
tbody td { padding: 9px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: rgba(47,129,247,.05); }
td.mono, .mono { font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace; font-size: 12px; }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
.cell-clamp { max-width: 420px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cell-clamp:hover { white-space: normal; overflow: visible; }
.empty { text-align: center; color: var(--muted); padding: 30px !important; }

/* ---------- Badges ---------- */
.badge { display: inline-block; padding: 2px 9px; border-radius: 20px; font-size: 11px; font-weight: 600; }
.badge-admin { background: rgba(210,153,34,.15); color: var(--yellow); border: 1px solid rgba(210,153,34,.4); }
.badge-user { background: rgba(47,129,247,.15); color: var(--accent-2); border: 1px solid rgba(47,129,247,.4); }
.badge-count { background: var(--panel-2); color: var(--muted); border: 1px solid var(--border-2); }

/* ---------- Image thumb ---------- */
.thumb { width: 90px; height: 60px; object-fit: cover; border-radius: 6px; border: 1px solid var(--border-2); cursor: zoom-in; background: var(--bg-2); }
.thumb:hover { border-color: var(--accent); }
.img-missing { width: 90px; height: 60px; border-radius: 6px; border: 1px dashed var(--border-2); display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: 11px; background: var(--bg-2); }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.7);
  display: flex; align-items: center; justify-content: center; z-index: 100; padding: 20px;
}
.modal {
  background: var(--panel); border: 1px solid var(--border-2); border-radius: 12px;
  max-width: 900px; width: 100%; max-height: 90vh; overflow: auto; box-shadow: var(--shadow);
}
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-head h3 { font-size: 16px; }
.modal-body { padding: 20px; }
.modal-close { background: none; border: none; font-size: 20px; color: var(--muted); cursor: pointer; }
.modal-close:hover { color: var(--text); }
.modal-img { max-width: 100%; border-radius: 8px; border: 1px solid var(--border); }

/* ---------- Forms ---------- */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid .full { grid-column: 1 / -1; }
.field label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 5px; }
.field input, .field select, .field textarea { width: 100%; }
.field textarea { min-height: 80px; resize: vertical; }
.form-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 18px; }

/* ---------- Login ---------- */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: radial-gradient(1200px 600px at 50% -10%, #1a2332 0%, var(--bg) 60%); }
.login-card { width: 360px; background: var(--panel); border: 1px solid var(--border-2); border-radius: 14px; padding: 32px; box-shadow: var(--shadow); }
.login-card .brand { border: none; padding: 0 0 6px; justify-content: center; }
.login-card h2 { text-align: center; margin-bottom: 4px; }
.login-card .sub { text-align: center; color: var(--muted); margin-bottom: 24px; }
.login-card .field { margin-bottom: 14px; }
.login-card .err { color: var(--red); font-size: 13px; margin-bottom: 10px; min-height: 18px; }
.login-card .btn-primary { width: 100%; padding: 10px; }

/* ---------- Misc ---------- */
.pill-row { display: flex; flex-wrap: wrap; gap: 8px; }
.kv { display: grid; grid-template-columns: 160px 1fr; gap: 6px 14px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { word-break: break-word; }
.dash-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 14px; }
.dash-cols { display: grid; grid-template-columns: 1.2fr 1.5fr; gap: 14px; margin-top: 18px; }
@media (max-width: 900px) { .dash-cols { grid-template-columns: 1fr; } }
.stat-card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.stat-card .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .5px; }
.stat-card .value { font-size: 26px; font-weight: 700; margin-top: 6px; }
.stat-card .value small { font-size: 14px; color: var(--muted); font-weight: 400; }
.stat-card .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.stat-card:hover { border-color: var(--accent); }
.spinner-wrap { padding: 20px 0; text-align: center; }
.dash-loading { display: flex; align-items: center; gap: 10px; color: var(--muted); padding: 18px 4px; min-height: 52px; }

/* Dashboard bar chart */
.bar-row { display: grid; grid-template-columns: 170px 1fr 46px; gap: 10px; align-items: center; margin-bottom: 7px; }
.bar-label { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { height: 10px; background: var(--bg-2); border-radius: 6px; overflow: hidden; }
.bar-fill { height: 100%; background: linear-gradient(90deg, var(--accent), var(--accent-2)); border-radius: 6px; transition: width .3s ease; }
.bar-val { text-align: right; font-size: 12px; color: var(--text); font-variant-numeric: tabular-nums; }

/* Dashboard hourly mini bar chart (small, same size as other stat boxes) */
.dash-hourly { }
.hr-chart { display: flex; align-items: flex-end; gap: 2px; height: 56px; margin-top: 6px; }
.hr-bar { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; gap: 1px; }
.hr-count { font-size: 8px; color: var(--muted); font-variant-numeric: tabular-nums; line-height: 1; }
.hr-fill { width: 100%; border-radius: 2px 2px 0 0; background: linear-gradient(180deg, var(--accent-2), var(--accent)); }
.hr-fill.now { background: linear-gradient(180deg, #f85149, #b6231c); }
.hr-bar > span:last-child { font-size: 8px; color: var(--muted); line-height: 1; }
.spin { display: inline-block; width: 16px; height: 16px; border: 2px solid var(--border-2); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast {
  position: fixed; bottom: 20px; right: 20px; z-index: 200;
  background: var(--panel-2); border: 1px solid var(--border-2); border-radius: 8px;
  padding: 12px 18px; box-shadow: var(--shadow); animation: slidein .2s ease;
}
.toast.ok { border-color: var(--green); }
.toast.err { border-color: var(--red); }
@keyframes slidein { from { transform: translateY(10px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---------- System Config ---------- */
.cfg-group { margin-bottom: 22px; }
.cfg-group:last-child { margin-bottom: 0; }
.cfg-group-title { font-size: 12px; font-weight: 700; color: var(--accent-2); text-transform: uppercase; letter-spacing: .6px; margin-bottom: 8px; padding-bottom: 6px; border-bottom: 1px solid var(--border); }
.cfg-row { display: grid; grid-template-columns: 220px 1fr; gap: 12px 16px; padding: 7px 4px; align-items: baseline; border-bottom: 1px solid rgba(42,51,66,.35); }
.cfg-row:last-child { border-bottom: none; }
.cfg-label { color: var(--muted); font-size: 13px; word-break: break-word; }
.cfg-value { word-break: break-word; white-space: pre-wrap; }
.mask-dots { letter-spacing: 2px; color: var(--muted); cursor: help; }
.sp-prompt-box { background: var(--bg-2); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; white-space: pre-wrap; word-break: break-word; font-size: 13px; }

/* ---------- Detail modal ---------- */
.detail-box { background: var(--bg-2); border: 1px solid var(--border); border-radius: 8px; padding: 12px; white-space: pre-wrap; word-break: break-word; font-size: 13px; }
.detail-box .json-pre { margin: 0; font-family: "SFMono-Regular", Consolas, monospace; font-size: 12px; white-space: pre-wrap; word-break: break-word; line-height: 1.5; }
.waiting { color: var(--yellow); font-style: italic; font-family: inherit; }
.modal-detail { max-width: 820px; }
.tool-block { border: 1px solid var(--border); border-radius: 8px; margin-bottom: 10px; overflow: hidden; background: var(--bg-2); }
.tool-block:last-child { margin-bottom: 0; }
.tool-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 8px 12px; background: var(--panel-2); border-bottom: 1px solid var(--border); }
.tool-time { font-family: "SFMono-Regular", Consolas, monospace; font-size: 12px; color: var(--muted); }
.tool-name { font-weight: 700; font-size: 13px; color: var(--accent-2); }
.tool-block summary { cursor: pointer; padding: 7px 12px; font-size: 12px; color: var(--muted); user-select: none; }
.tool-block summary:hover { color: var(--text); }
.tool-block pre { margin: 0; padding: 10px 12px; background: #0a0e13; border-top: 1px solid var(--border); font-family: "SFMono-Regular", Consolas, monospace; font-size: 12px; max-height: 260px; overflow: auto; white-space: pre-wrap; word-break: break-word; }
.tool-block .tool-args pre { color: var(--muted); }

/* ---------- Presets in PTZ detail ---------- */
.preset-block { border: 1px solid var(--border); border-radius: 8px; margin-bottom: 8px; overflow: hidden; background: var(--bg-2); }
.preset-block summary { cursor: pointer; padding: 9px 12px; font-size: 13px; font-weight: 600; display: flex; align-items: center; gap: 8px; user-select: none; }
.preset-block summary:hover { background: var(--panel-2); }
.preset-block[open] summary { border-bottom: 1px solid var(--border); }
.preset-badge { margin-left: auto; font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .4px; padding: 2px 8px; border-radius: 20px; }
.preset-badge.on { color: var(--green); border: 1px solid rgba(63,185,80,.4); }
.preset-badge.off { color: var(--muted); border: 1px solid var(--border-2); }
.preset-body { padding: 10px 12px; }
