:root {
  --bg: #07090d;
  --panel: #10161e;
  --panel-2: #161e28;
  --line: #243040;
  --text: #e8eef6;
  --muted: #8b9bb0;
  --ok: #22c55e;
  --warn: #f59e0b;
  --fault: #ef4444;
  --off: #64748b;
  --ok-bg: rgba(34, 197, 94, 0.12);
  --warn-bg: rgba(245, 158, 11, 0.12);
  --fault-bg: rgba(239, 68, 68, 0.14);
  --off-bg: rgba(100, 116, 139, 0.12);
  --pad: 16px;
}

* { box-sizing: border-box; }
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
html, body { margin: 0; min-height: 100%; }
body {
  font-family: "Segoe UI", "IBM Plex Sans", sans-serif;
  background:
    radial-gradient(1200px 500px at 10% -10%, #122033 0%, transparent 50%),
    var(--bg);
  color: var(--text);
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

button, input {
  font: inherit;
  font-size: 16px;
}

.topbar, .summary, .toolbar, .grid, .empty {
  width: min(1440px, calc(100% - (var(--pad) * 2)));
  margin-left: auto;
  margin-right: auto;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 18px 0 10px;
}
.brand { display: flex; gap: 12px; align-items: center; min-width: 0; }
.brand-mark {
  width: 12px; height: 36px; border-radius: 99px; flex-shrink: 0;
  background: linear-gradient(180deg, var(--ok), #16a34a);
  box-shadow: 0 0 18px rgba(34,197,94,.45);
}
h1 { margin: 0; font-size: 22px; letter-spacing: .02em; }
.sub { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.broker {
  display: flex; align-items: center; gap: 8px;
  background: var(--panel); border: 1px solid var(--line);
  padding: 8px 12px; border-radius: 999px; font-size: 13px; color: var(--muted);
  max-width: 100%; min-width: 0;
}
.broker #brokerText {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.summary {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 10px;
  margin-top: 14px;
}
.stat {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
}
.stat b { display: block; font-size: 28px; line-height: 1.1; }
.stat span { color: var(--muted); font-size: 12px; }
.stat.ok b { color: var(--ok); }
.stat.warn b { color: var(--warn); }
.stat.fault b { color: var(--fault); }
.stat.off b { color: var(--off); }

.toolbar {
  display: flex; gap: 10px; align-items: center; flex-wrap: wrap;
  margin: 14px auto;
}
.filters {
  display: flex; gap: 8px; flex-wrap: wrap;
}
.chip {
  background: var(--panel); color: var(--muted); border: 1px solid var(--line);
  border-radius: 999px; padding: 8px 14px; cursor: pointer;
  min-height: 40px;
}
.chip.active { color: var(--text); border-color: #3b82f6; background: #132033; }
#search {
  flex: 1; min-width: 180px;
  background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px;
}
.clock { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }

.switch-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 13px;
  user-select: none;
  cursor: pointer;
  white-space: nowrap;
}
.switch-wrap input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch {
  width: 42px;
  height: 24px;
  border-radius: 99px;
  background: #2a3544;
  border: 1px solid var(--line);
  position: relative;
  flex-shrink: 0;
}
.switch::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #cbd5e1;
  transition: transform .15s ease, background .15s ease;
}
.switch-wrap input:checked + .switch {
  background: #14532d;
  border-color: var(--ok);
}
.switch-wrap input:checked + .switch::after {
  transform: translateX(18px);
  background: var(--ok);
}
.switch-wrap input:focus-visible + .switch {
  outline: 2px solid #3b82f6;
  outline-offset: 2px;
}

.controls {
  margin-top: 10px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px;
}
.mode-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 8px;
}
.mode-btn {
  background: var(--panel);
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 10px;
  min-height: 36px;
  cursor: pointer;
}
.mode-btn.active {
  color: var(--text);
  border-color: #3b82f6;
  background: #132033;
}
.fan-pct {
  margin-left: auto;
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: 13px;
}
.fan-slider {
  width: 100%;
  accent-color: #3b82f6;
  height: 22px;
}
.light.ignored { opacity: .45; }
.cmd-status { color: var(--muted); font-size: 11px; margin-top: 4px; min-height: 14px; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 320px), 1fr));
  gap: 12px;
  padding-bottom: 40px;
}

.tile {
  background: var(--panel);
  border: 1px solid var(--line);
  border-left: 5px solid var(--off);
  border-radius: 14px;
  padding: 12px;
  cursor: pointer;
  min-width: 0;
}
.tile:hover { border-color: #3b4b5e; }
.tile.ok { border-left-color: var(--ok); background: linear-gradient(180deg, var(--ok-bg), var(--panel) 42%); }
.tile.warn { border-left-color: var(--warn); background: linear-gradient(180deg, var(--warn-bg), var(--panel) 42%); }
.tile.fault { border-left-color: var(--fault); background: linear-gradient(180deg, var(--fault-bg), var(--panel) 42%); }
.tile.offline { border-left-color: var(--off); opacity: .86; }

.tile-head { display: flex; justify-content: space-between; gap: 8px; align-items: flex-start; }
.last-seen {
  margin: 8px 0 2px;
  font-size: 13px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.last-seen.fresh { color: var(--ok); }
.last-seen.aging { color: var(--warn); }
.last-seen.stale { color: var(--fault); }
.tile h3 { margin: 0; font-size: 18px; }
.tile .id { color: var(--muted); font-size: 11px; word-break: break-all; margin-top: 2px; }
.badge {
  font-size: 11px; font-weight: 700; letter-spacing: .04em;
  padding: 4px 8px; border-radius: 999px; text-transform: uppercase;
  flex-shrink: 0;
}
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.fault { background: var(--fault-bg); color: var(--fault); }
.badge.offline { background: var(--off-bg); color: #cbd5e1; }

.lights {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 6px;
  margin: 12px 0 10px;
}
.light {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 7px 2px;
  text-align: center;
  min-width: 0;
}
.light small {
  display: block; color: var(--muted); font-size: 10px; margin-top: 4px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.led {
  width: 10px; height: 10px; border-radius: 50%; display: inline-block;
  background: #334155; box-shadow: inset 0 0 0 1px #0006;
}
.led.ok { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.led.warn { background: var(--warn); box-shadow: 0 0 8px var(--warn); }
.led.fault, .led.offline { background: var(--fault); box-shadow: 0 0 8px var(--fault); }
.led.idle { background: #64748b; }

.metrics { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.col {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px;
  min-width: 0;
}
.col h4 { margin: 0 0 6px; font-size: 11px; color: var(--muted); letter-spacing: .06em; }
.kv { display: flex; justify-content: space-between; font-size: 12px; gap: 8px; padding: 1px 0; }
.kv span { color: var(--muted); }
.kv b { font-variant-numeric: tabular-nums; white-space: nowrap; }
.foot {
  display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap;
  margin-top: 8px; color: var(--muted); font-size: 11px;
}
.faults { color: #fca5a5; font-size: 11px; margin-top: 6px; min-height: 14px; }

.empty { color: var(--muted); text-align: center; padding: 40px 0; }
.hidden { display: none; }

.drawer {
  position: fixed; inset: 0; background: #0008;
  display: none;
  place-items: end;
  z-index: 40;
}
.drawer:not([hidden]) {
  display: grid;
}
.drawer[hidden] {
  display: none !important;
}
.drawer-inner {
  width: min(720px, 100%);
  height: min(88vh, 100%);
  background: #0d131b;
  border-left: 1px solid var(--line);
  padding: 16px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom));
  overflow: auto;
}
.drawer-inner header { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; }
#dTitle { margin: 0; font-size: 18px; }
#dSub { margin: 4px 0 0; color: var(--muted); font-size: 12px; word-break: break-all; }
#dJson {
  background: #070b10; color: #d6e4f5; border: 1px solid var(--line);
  border-radius: 10px; padding: 12px; overflow: auto; font-size: 12px;
  max-width: 100%;
}
#closeDrawer {
  background: var(--panel); color: var(--text); border: 1px solid var(--line);
  border-radius: 8px; padding: 10px 14px; cursor: pointer;
  min-height: 44px; flex-shrink: 0;
}

@media (max-width: 820px) {
  .topbar {
    flex-direction: column;
    align-items: stretch;
    padding-top: 14px;
  }
  .broker { align-self: flex-start; }
  .summary {
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 6px;
  }
  .stat { padding: 8px 6px; text-align: center; border-radius: 10px; }
  .stat b { font-size: 20px; }
  .stat span { font-size: 10px; }
  .toolbar {
    position: sticky;
    top: 0;
    z-index: 20;
    background: color-mix(in srgb, var(--bg) 92%, transparent);
    backdrop-filter: blur(10px);
    padding: 8px 0 10px;
    margin-top: 8px;
  }
  .filters {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    width: 100%;
    padding-bottom: 2px;
  }
  .filters::-webkit-scrollbar { display: none; }
  .chip { flex: 0 0 auto; }
  #search { min-width: 100%; width: 100%; }
  .switch-wrap { width: 100%; }
  .clock { width: 100%; }
}

@media (max-width: 640px) {
  :root { --pad: 12px; }
  h1 { font-size: 18px; }
  .sub { font-size: 12px; }
  .brand-mark { height: 28px; width: 10px; }
  .grid {
    grid-template-columns: 1fr;
    gap: 10px;
    padding-bottom: calc(24px + env(safe-area-inset-bottom));
  }
  .tile { padding: 12px 10px; }
  .tile .id { display: none; }
  .lights { gap: 4px; }
  .light { padding: 8px 0; }
  .led { width: 12px; height: 12px; }
  .col h4 { font-size: 10px; letter-spacing: .04em; }
  .kv { font-size: 13px; padding: 3px 0; }
  .drawer { place-items: end center; }
  .drawer-inner {
    width: 100%;
    height: min(92dvh, 100%);
    border-left: 0;
    border-top: 1px solid var(--line);
    border-radius: 16px 16px 0 0;
    padding-top: 12px;
  }
  .drawer-inner::before {
    content: "";
    display: block;
    width: 42px;
    height: 4px;
    border-radius: 99px;
    background: #3a4a5c;
    margin: 0 auto 12px;
  }
}

@media (max-width: 380px) {
  .metrics { gap: 6px; }
  .col { padding: 8px 6px; }
  .kv { font-size: 12px; gap: 4px; }
  .light small { font-size: 9px; }
}
