:root {
  --bg: #0a0f15;
  --panel: #111922;
  --panel-2: #16202b;
  --line: #213041;
  --text: #e6edf3;
  --muted: #8a9ab0;
  --accent: #3b9eff;
  --ok: #22c55e;
  --warn: #f59e0b;
  --crit: #ef4444;
  --info: #3b9eff;
  --radius: 14px;
  --shadow: 0 8px 30px rgba(0, 0, 0, .35);
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f3f6fa;
    --panel: #ffffff;
    --panel-2: #f1f4f8;
    --line: #dde4ec;
    --text: #0f1b2a;
    --muted: #5d6b7e;
    --shadow: 0 6px 24px rgba(15, 27, 42, .08);
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
}
a { color: inherit; text-decoration: none; }
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -.01em; }
h1 { font-size: 24px; }
h2 { font-size: 18px; }
h3 { font-size: 15px; display: flex; align-items: center; gap: 8px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
[hidden] { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 20px;
  padding: 10px 20px;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; }
.logo {
  width: 22px; height: 22px; border-radius: 50%;
  background: radial-gradient(circle at 50% 50%, var(--accent) 0 30%, transparent 32%), conic-gradient(var(--accent), #22d3ee, var(--accent));
  -webkit-mask: radial-gradient(circle, #000 30%, transparent 32%, transparent 48%, #000 50%);
          mask: radial-gradient(circle, #000 30%, transparent 32%, transparent 48%, #000 50%);
}
.tabs { display: flex; gap: 4px; }
.tabs a { padding: 7px 14px; border-radius: 999px; color: var(--muted); font-weight: 550; }
.tabs a:hover { color: var(--text); background: var(--panel-2); }
.tabs a.active { color: var(--text); background: var(--panel-2); box-shadow: inset 0 0 0 1px var(--line); }
.status { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px; border-radius: 999px; font-size: 12px; font-weight: 600;
  background: var(--panel-2); border: 1px solid var(--line); color: var(--muted);
}
.pill i { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.pill.on i { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 25%, transparent); }
.pill.off i { background: var(--crit); }
.pill.on, .pill.off { color: var(--text); }
.pill.demo-src i { background: var(--warn); }
.pill.demo { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 45%, transparent); background: color-mix(in srgb, var(--warn) 12%, transparent); }
.icon-btn {
  position: relative; display: grid; place-items: center;
  width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--panel); color: var(--text); cursor: pointer;
}
.icon-btn:hover, .icon-btn.active { background: var(--panel-2); }
.icon-btn b {
  position: absolute; top: -6px; right: -6px; min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 999px; background: var(--crit); color: #fff; font-size: 11px; line-height: 18px; text-align: center;
}

main { max-width: 1320px; margin: 0 auto; padding: 24px 20px 60px; }

/* ---------- common ---------- */
.panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow);
}
.panel > h3 { margin-bottom: 12px; }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.actions, .row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 14px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--panel-2); color: var(--text); font: inherit; font-weight: 550; cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn.sm { padding: 5px 10px; font-size: 13px; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--crit); }
.btn.danger:hover { border-color: var(--crit); }
.btn:disabled { opacity: .45; cursor: default; }
.file-btn input { display: none; }
select, input[type=text], input[type=number] {
  font: inherit; color: var(--text); background: var(--panel-2);
  border: 1px solid var(--line); border-radius: 9px; padding: 7px 10px; min-width: 0;
}
select:focus, input:focus { outline: 2px solid color-mix(in srgb, var(--accent) 60%, transparent); outline-offset: 0; }
.empty { padding: 40px 20px; text-align: center; color: var(--muted); border: 1px dashed var(--line); border-radius: var(--radius); }

/* ---------- home ---------- */
.thumbs { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 20px; }
.thumb {
  background: var(--panel); border: 1px solid var(--line); border-radius: 18px; overflow: hidden;
  box-shadow: var(--shadow); transition: transform .15s, border-color .15s;
}
.thumb:hover { transform: translateY(-2px); border-color: var(--accent); }
.thumb-media { position: relative; aspect-ratio: 16 / 9; background: #000; overflow: hidden; }
.thumb-media canvas, .thumb-media img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.thumb-body { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; }
.thumb-body p { margin: 2px 0 0; }
.arrow { font-size: 20px; color: var(--muted); }
.thumb:hover .arrow { color: var(--accent); }
.badge-live {
  position: absolute; top: 12px; left: 12px; padding: 3px 8px; border-radius: 6px;
  background: var(--crit); color: #fff; font-size: 11px; font-weight: 700; letter-spacing: .06em;
}
.db-preview {
  background: var(--panel-2);
  display: grid; grid-template-columns: repeat(3, 1fr); grid-auto-rows: 1fr; gap: 10px; padding: 14px;
}
.mini {
  border-radius: 10px; background: var(--panel); border: 1px solid var(--line); padding: 10px 12px;
  display: flex; flex-direction: column; justify-content: space-between; min-width: 0;
  border-left: 3px solid var(--g, var(--line));
}
.mini .n { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mini .v { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.mini .v small { font-size: 12px; color: var(--muted); font-weight: 500; margin-left: 2px; }
.mini.warn .v { color: var(--warn); }
.mini.crit .v { color: var(--crit); }
.db-preview .empty { grid-column: 1 / -1; grid-row: 1 / -1; display: grid; place-items: center; border: 0; }

.pulse { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); animation: pulse 1.6s infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--ok) 60%, transparent); } 100% { box-shadow: 0 0 0 10px transparent; } }

.alert-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; max-height: 420px; overflow: auto; }
.alert-list li {
  display: grid; grid-template-columns: 4px 1fr auto; gap: 12px; align-items: center;
  padding: 10px 12px 10px 0; border-radius: 10px; background: var(--panel-2);
}
.alert-list li::before { content: ""; align-self: stretch; border-radius: 4px; background: var(--lv, var(--info)); }
.alert-list .t { font-weight: 600; }
.alert-list .m { color: var(--muted); font-size: 13px; }
.alert-list time { color: var(--muted); font-size: 12px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.alert-list .empty-li { display: block; padding: 24px; text-align: center; color: var(--muted); background: none; }
.alert-list .empty-li::before { display: none; }
.lv-info { --lv: var(--info); } .lv-ok { --lv: var(--ok); } .lv-warn { --lv: var(--warn); } .lv-crit { --lv: var(--crit); }

.media-offline {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
  background: repeating-linear-gradient(135deg, #0c1219 0 12px, #0f1720 12px 24px); color: #cbd5e1; text-align: center; padding: 16px;
}
.media-offline span { font-weight: 600; }
.media-offline small { color: #8a9ab0; word-break: break-all; }

/* ---------- live ---------- */
.live-grid { display: grid; grid-template-columns: 1fr 320px; gap: 20px; align-items: start; }
.player { position: relative; aspect-ratio: 16 / 9; background: #000; border-radius: 16px; overflow: hidden; box-shadow: var(--shadow); }
.player img, .player canvas { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
.player:fullscreen { border-radius: 0; }
.ov-tl, .ov-tr {
  position: absolute; top: 12px; display: flex; gap: 10px; align-items: center;
  padding: 4px 10px; border-radius: 8px; background: rgba(0, 0, 0, .55); color: #fff; font-size: 12px;
}
.ov-tl { left: 12px; } .ov-tr { right: 12px; font-variant-numeric: tabular-nums; }
.ov-tl #live-src { color: #cbd5e1; max-width: 40vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rec { color: #ff5a5a; font-weight: 700; animation: blink 1.4s steps(2) infinite; }
@keyframes blink { 50% { opacity: .35; } }
.ov-readings { position: absolute; left: 12px; right: 12px; bottom: 46px; display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  padding: 4px 10px; border-radius: 8px; background: rgba(0, 0, 0, .6); color: #fff; font-size: 12px;
  border-left: 3px solid var(--g, #fff); font-variant-numeric: tabular-nums;
}
.chip b { margin-left: 6px; }
.chip.warn b { color: #fbbf24; } .chip.crit b { color: #f87171; }
.player-bar { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.side .grp { margin-bottom: 14px; }
.side .grp h4 { margin: 0 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--g, var(--muted)); }
.side .rd { display: flex; justify-content: space-between; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--line); }
.side .rd span:last-child { font-weight: 600; font-variant-numeric: tabular-nums; }
.side .rd.warn span:last-child { color: var(--warn); } .side .rd.crit span:last-child { color: var(--crit); }

/* ---------- database ---------- */
.groups { display: flex; flex-direction: column; gap: 18px; margin-bottom: 20px; }
.group { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.group-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.group-head .dot { width: 10px; height: 10px; border-radius: 3px; background: var(--g); }
.group-head .cnt { color: var(--muted); font-size: 12px; }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px; }
.tile {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px;
  display: flex; flex-direction: column; gap: 6px; min-width: 0;
}
.tile.warn { border-color: color-mix(in srgb, var(--warn) 60%, var(--line)); }
.tile.crit { border-color: color-mix(in srgb, var(--crit) 70%, var(--line)); box-shadow: 0 0 0 1px color-mix(in srgb, var(--crit) 40%, transparent); }
.tile-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.tile-top .nm { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.type-chip { font-size: 11px; color: var(--muted); border: 1px solid var(--line); border-radius: 999px; padding: 1px 8px; white-space: nowrap; }
.tile .val { font-size: 28px; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.1; }
.tile .val small { font-size: 13px; color: var(--muted); font-weight: 500; margin-left: 4px; }
.tile.warn .val { color: var(--warn); } .tile.crit .val { color: var(--crit); }
.tile canvas { width: 100%; height: 44px; display: block; }
.tile .stats { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }

.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.chk { display: inline-flex; gap: 6px; align-items: center; color: var(--muted); }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
td.num, th.num { text-align: right; }
tbody tr:hover { background: var(--panel-2); }
.st { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; text-transform: uppercase; }
.st::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--c); }
.st.ok { --c: var(--ok); color: var(--muted); } .st.warn { --c: var(--warn); color: var(--warn); } .st.crit { --c: var(--crit); color: var(--crit); }
.gtag { display: inline-flex; align-items: center; gap: 6px; }
.gtag::before { content: ""; width: 8px; height: 8px; border-radius: 2px; background: var(--g); }
.pager { display: flex; justify-content: space-between; align-items: center; margin-top: 12px; }

/* ---------- settings ---------- */
.settings-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 20px; }
/* min-width: 0 lets wide tables scroll inside their panel instead of stretching the page on phones */
.settings-grid > * { min-width: 0; }
.settings-grid .wide { grid-column: 1 / -1; }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; font-weight: 550; }
.field input, .field select { font-weight: 400; }
.switch-row { display: flex; justify-content: space-between; align-items: center; gap: 16px; padding: 10px 0; border-bottom: 1px solid var(--line); margin-bottom: 8px; cursor: pointer; }
.switch-row small { display: block; color: var(--muted); font-weight: 400; margin-top: 2px; }
.switch { appearance: none; width: 42px; height: 24px; border-radius: 999px; background: var(--line); position: relative; cursor: pointer; flex: none; transition: background .15s; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: transform .15s; }
.switch:checked { background: var(--accent); }
.switch:checked::after { transform: translateX(18px); }
.test-out { margin-top: 10px; font-size: 13px; }
.test-out:empty { display: none; }
.ai-tag {
  display: inline-block; margin-left: 6px; padding: 1px 7px; border-radius: 999px; font-size: 11px; font-weight: 650; cursor: help; white-space: nowrap;
  color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, transparent); border: 1px solid color-mix(in srgb, var(--accent) 40%, transparent);
}
.test-out .ok, .test-out.ok { color: var(--ok); } .test-out .bad, .test-out.bad { color: var(--crit); }
.test-out ul { margin: 6px 0 0; padding-left: 18px; color: var(--muted); }
.test-out img { max-width: 100%; border-radius: 8px; margin-top: 8px; }
.th-table input { width: 100px; }
td select { padding: 4px 8px; }

/* ---------- drawer & toasts ---------- */
.drawer {
  position: fixed; top: 60px; right: 16px; z-index: 30; width: min(420px, calc(100vw - 32px)); max-height: calc(100vh - 80px);
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow);
  display: flex; flex-direction: column;
}
.drawer .alert-list { max-height: none; flex: 1; }
.toasts { position: fixed; right: 16px; bottom: 16px; z-index: 40; display: flex; flex-direction: column; gap: 8px; width: min(360px, calc(100vw - 32px)); }
.toast {
  display: grid; grid-template-columns: 4px 1fr; gap: 12px; padding: 12px 14px 12px 0;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
  animation: slide .2s ease-out; cursor: pointer;
}
.toast::before { content: ""; border-radius: 4px; background: var(--lv, var(--info)); }
.toast .t { font-weight: 650; }
.toast .m { color: var(--muted); font-size: 13px; }
.toast.out { opacity: 0; transform: translateX(20px); transition: .25s; }
@keyframes slide { from { opacity: 0; transform: translateX(20px); } }

/* ---------- responsive ---------- */
@media (max-width: 900px) {
  .live-grid, .settings-grid { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; gap: 10px; padding: 10px 16px; }
  .tabs { order: 3; width: 100%; overflow-x: auto; }
  .status .pill:not(.demo) { display: none; }
  main { padding: 16px 16px 40px; }
  .thumbs { grid-template-columns: 1fr; }
  .db-preview { grid-template-columns: repeat(2, 1fr); }
}
