:root {
  --bg: var(--tg-theme-secondary-bg-color, #f3f3f6);
  --surface: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, #ffffff));
  --text: var(--tg-theme-text-color, #141519);
  --muted: var(--tg-theme-hint-color, #6e727c);
  --accent: var(--tg-theme-button-color, #6d3aff);
  --accent-soft: rgba(109, 58, 255, 0.11);
  --border: rgba(120, 124, 138, 0.14);
  --ok: #22a06b;
  --ok-soft: rgba(34, 160, 107, 0.12);
  --warn: #d9820b;
  --warn-soft: rgba(217, 130, 11, 0.14);
  --crit: #e23d3d;
  --crit-soft: rgba(226, 61, 61, 0.13);
}

html[data-theme="dark"] {
  --bg: var(--tg-theme-secondary-bg-color, #0f1014);
  --surface: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, #191b22));
  --text: var(--tg-theme-text-color, #eceef2);
  --muted: var(--tg-theme-hint-color, #8c919e);
  --accent: var(--tg-theme-button-color, #9171ff);
  --accent-soft: rgba(145, 113, 255, 0.16);
  --border: rgba(225, 230, 245, 0.09);
  --ok: #33b87e;
  --ok-soft: rgba(51, 184, 126, 0.16);
  --warn: #f0a22e;
  --warn-soft: rgba(240, 162, 46, 0.16);
  --crit: #f25a5a;
  --crit-soft: rgba(242, 90, 90, 0.17);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
  overscroll-behavior-y: contain;
}

.topbar {
  position: sticky; top: 0; z-index: 20;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 10px 14px;
}
.topbar-main {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
.topbar-right {
  display: flex; align-items: center; gap: 8px;
}
.clock {
  font-size: 12px; color: var(--muted); font-weight: 500;
}

.overall-pill {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 5px 11px; border-radius: 999px;
  font-weight: 600; font-size: 13px;
  background: var(--border);
}
.overall-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--muted);
}
.overall-pill.ok { background: var(--ok-soft); color: var(--ok); }
.overall-pill.ok .overall-dot { background: var(--ok); }
.overall-pill.warning { background: var(--warn-soft); color: var(--warn); }
.overall-pill.warning .overall-dot { background: var(--warn); }
.overall-pill.critical { background: var(--crit-soft); color: var(--crit); }
.overall-pill.critical .overall-dot { background: var(--crit); }

.icon-btn {
  width: 32px; height: 32px; border-radius: 9px;
  border: 1px solid var(--border);
  background: transparent; color: var(--text);
  font-size: 16px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn.spinning { animation: spin 0.7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.banner {
  margin-top: 8px; padding: 7px 10px;
  border-radius: 8px; font-size: 12px;
  background: var(--warn-soft); color: var(--warn);
}

.view {
  padding: 12px 12px calc(76px + env(safe-area-inset-bottom, 0px));
  max-width: 720px; margin: 0 auto;
}

.section-title {
  font-size: 11px; font-weight: 700;
  letter-spacing: 0.07em; text-transform: uppercase;
  color: var(--muted);
  margin: 16px 4px 8px;
  display: flex; align-items: center; justify-content: space-between;
}
.section-title:first-child { margin-top: 2px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 13px 14px;
  margin-bottom: 10px;
}
.card-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  margin-bottom: 8px;
}
.card-title { font-weight: 650; font-size: 15px; }
.card-sub { font-size: 12px; color: var(--muted); }

.kpi-strip {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px;
  margin-bottom: 10px;
}
.kpi-box {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 11px 12px;
}
.kpi-label { font-size: 11.5px; color: var(--muted); margin-bottom: 3px; }
.kpi-val { font-size: 20px; font-weight: 700; letter-spacing: -0.02em; }
.kpi-sub { font-size: 11.5px; color: var(--muted); margin-top: 2px; }

.ok-box {
  display: flex; align-items: center; gap: 10px;
  background: var(--ok-soft); color: var(--ok);
  border-radius: 12px; padding: 12px 14px;
  font-weight: 600; margin-bottom: 10px;
}

.issue-list { display: flex; flex-direction: column; gap: 7px; margin-bottom: 12px; }
.issue-item {
  display: flex; align-items: flex-start; gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--warn);
  border-radius: 11px;
  padding: 10px 12px;
  text-align: left; width: 100%; color: var(--text); font: inherit;
}
.issue-item.critical { border-left-color: var(--crit); }
.issue-item.clickable { cursor: pointer; }
.issue-dot {
  width: 8px; height: 8px; border-radius: 50%; margin-top: 5px; flex-shrink: 0;
  background: var(--warn);
}
.issue-item.critical .issue-dot { background: var(--crit); }
.issue-body { flex: 1; min-width: 0; }
.issue-title { font-weight: 600; font-size: 13.5px; word-break: break-word; }
.issue-detail { font-size: 12px; color: var(--muted); margin-top: 2px; }
.issue-cta { font-size: 12px; color: var(--accent); font-weight: 600; align-self: center; flex-shrink: 0; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 4px 8px; border-radius: 7px;
  font-size: 12px; font-weight: 500;
  background: rgba(128, 132, 144, 0.1);
}
.chip.ok { background: var(--ok-soft); color: var(--ok); }
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.chip.crit { background: var(--crit-soft); color: var(--crit); }

.kv-list { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; font-size: 13px; }
.kv-row { display: flex; justify-content: space-between; gap: 10px; }
.kv-key { color: var(--muted); }
.kv-val { font-weight: 550; text-align: right; }

.filter-bar {
  display: flex; gap: 8px; margin-bottom: 10px; align-items: center;
}
.search-input {
  flex: 1; min-width: 0;
  padding: 8px 11px; border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
  font: inherit; font-size: 13.5px;
}
.filter-btn {
  padding: 8px 11px; border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
  font: inherit; font-size: 12.5px; font-weight: 600;
  cursor: pointer; white-space: nowrap;
}
.filter-btn.active {
  background: var(--accent-soft); color: var(--accent); border-color: var(--accent);
}

.room-row {
  padding: 9px 0; border-top: 1px solid var(--border);
}
.room-row:first-of-type { border-top: none; padding-top: 4px; }
.room-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  margin-bottom: 6px;
}
.room-name { font-weight: 600; font-size: 13.5px; }

.cam-btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 10px; border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg); color: var(--text);
  font: inherit; font-size: 12.5px; font-weight: 550;
  cursor: pointer;
}
.cam-btn.dead {
  border-color: var(--crit); background: var(--crit-soft); color: var(--crit);
}
.cam-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--ok); }
.cam-btn.dead .cam-dot { background: var(--crit); }

.meter-row { margin-top: 7px; }
.meter-head { display: flex; justify-content: space-between; font-size: 12px; margin-bottom: 3px; color: var(--muted); }
.meter-bar { height: 6px; border-radius: 99px; background: var(--border); overflow: hidden; }
.meter-fill { height: 100%; border-radius: 99px; background: var(--ok); }
.meter-fill.warn { background: var(--warn); }
.meter-fill.crit { background: var(--crit); }

.stack-bar {
  display: flex; height: 10px; border-radius: 99px; overflow: hidden;
  background: var(--border); margin: 10px 0; gap: 2px;
}
.stack-seg { height: 100%; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 6px 6px calc(6px + env(safe-area-inset-bottom, 0px));
}
.tab {
  position: relative;
  border: none; background: transparent; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 5px 4px; border-radius: 10px;
  font: inherit; font-size: 11px; font-weight: 600;
  cursor: pointer;
}
.tab.active { color: var(--accent); background: var(--accent-soft); }
.tab-icon { font-size: 16px; line-height: 1.1; }
.tab-badge {
  position: absolute; top: 3px; right: calc(50% - 20px);
  min-width: 16px; height: 16px; padding: 0 4px;
  border-radius: 99px; background: var(--crit); color: #fff;
  font-size: 10px; font-weight: 700; line-height: 16px;
}

.sheet-backdrop {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0, 0, 0, 0.55);
  display: flex; align-items: flex-end; justify-content: center;
}
.sheet-backdrop[hidden] { display: none; }
.sheet {
  width: 100%; max-width: 640px;
  background: var(--surface);
  border-radius: 18px 18px 0 0;
  padding: 8px 16px calc(16px + env(safe-area-inset-bottom, 0px));
  max-height: 88vh; display: flex; flex-direction: column;
}
.sheet-handle {
  width: 38px; height: 4px; border-radius: 99px;
  background: var(--border); margin: 2px auto 10px;
}
.sheet-head {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 10px;
  margin-bottom: 12px;
}
.sheet-sub { font-size: 12px; color: var(--muted); }
.sheet-title { font-size: 16px; font-weight: 700; }
.sheet-body {
  min-height: 190px; border-radius: 12px; overflow: hidden;
  background: var(--bg); display: flex; align-items: center; justify-content: center;
}
.sheet-body img { width: 100%; height: auto; display: block; max-height: 60vh; object-fit: contain; }
.sheet-foot {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  margin-top: 12px;
}
.sheet-meta { font-size: 12px; color: var(--muted); }
.btn-secondary {
  padding: 8px 14px; border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--accent-soft); color: var(--accent);
  font: inherit; font-size: 13px; font-weight: 600; cursor: pointer;
}
.btn-secondary:disabled { opacity: 0.5; cursor: default; }

.state-box {
  padding: 48px 18px; text-align: center; color: var(--muted);
}
.state-title { font-weight: 600; font-size: 15px; color: var(--text); margin-top: 12px; }
.state-desc { font-size: 13px; margin-top: 6px; }
.spinner {
  width: 28px; height: 28px; border-radius: 50%;
  border: 3px solid var(--border); border-top-color: var(--accent);
  margin: 0 auto; animation: spin 0.75s linear infinite;
}
