:root {
  color-scheme: light dark;
  --bg: var(--tg-theme-bg-color, #fff);
  --text: var(--tg-theme-text-color, #111);
  --muted: var(--tg-theme-hint-color, #888);
  --accent: var(--tg-theme-button-color, #2481cc);
  --section-bg: var(--tg-theme-secondary-bg-color, #f4f4f5);
  --border: rgba(128, 128, 128, 0.25);
  --error: #d33;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
header {
  display: flex; align-items: center; gap: 12px;
  padding: max(16px, env(safe-area-inset-top, 0px)) 12px 8px;
  position: sticky; top: 0; background: var(--bg); z-index: 10;
  border-bottom: 1px solid var(--border);
}
header h1 { margin: 0; font-size: 18px; font-weight: 600; flex: 1; }
header .back { color: var(--accent); text-decoration: none; font-size: 14px; }
.refresh-btn { background: transparent; border: 1px solid var(--border); border-radius: 6px;
  width: 32px; height: 32px; font-size: 18px; color: var(--text); cursor: pointer; }
main { padding: 12px; }
section { margin-bottom: 18px; }
section h2 { margin: 0 0 8px; font-size: 16px; font-weight: 600; }
.section-header { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 8px; }
.section-header select { background: var(--section-bg); color: var(--text); border: 1px solid var(--border);
  padding: 4px 8px; border-radius: 6px; font-size: 13px; }
.muted { color: var(--muted); font-weight: normal; font-size: 12px; }
.error { color: var(--error); }

/* Agent cards */
.agent-card {
  background: var(--section-bg); border-radius: 10px;
  padding: 12px; margin-bottom: 10px;
}
.agent-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.agent-id { display: flex; align-items: center; gap: 8px; }
.alive-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.alive-dot.on { background: #2ecc71; box-shadow: 0 0 6px rgba(46,204,113,0.5); }
.alive-dot.off { background: #999; }
.agent-name { font-weight: 600; font-size: 16px; }
.agent-pct {
  font-weight: 700; font-size: 18px; font-variant-numeric: tabular-nums;
  padding: 2px 8px; border-radius: 6px;
}
.agent-pct.cool { background: rgba(46, 204, 113, 0.18); color: #2ecc71; }
.agent-pct.warm { background: rgba(243, 156, 18, 0.18); color: #f39c12; }
.agent-pct.hot { background: rgba(231, 76, 60, 0.22); color: #e74c3c; }
.agent-pct.crit { background: #e74c3c; color: white; animation: crit-pulse 1.4s ease-in-out infinite; }
@keyframes crit-pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.65; } }

.stack-bar-wrap { position: relative; height: 14px; margin-bottom: 6px; }
.stack-bar { display: flex; height: 14px; border-radius: 4px; overflow: hidden;
  background: rgba(128,128,128,0.15); }
.stack-seg { height: 100%; }
.thr-tick {
  position: absolute; top: -2px; height: 18px; width: 2px; pointer-events: auto;
  transform: translateX(-1px);
}
.thr-tick.thr-warn { background: #f39c12; }
.thr-tick.thr-fire { background: #e74c3c; }
.thr-tick.thr-ceiling { background: #c0392b; box-shadow: 0 0 4px rgba(192, 57, 43, 0.6); }
.seg-input { background: #2ecc71; }
.seg-create { background: #f39c12; }
.seg-read { background: #2481cc; }

.stack-legend { display: flex; gap: 12px; font-size: 11px; color: var(--muted); flex-wrap: wrap; }
.lg-item { display: flex; align-items: center; gap: 4px; }
.lg-dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.lg-dot.seg-input { background: #2ecc71; }
.lg-dot.seg-create { background: #f39c12; }
.lg-dot.seg-read { background: #2481cc; }

.agent-foot { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 6px;
  font-size: 12px; padding-top: 6px; border-top: 1px dashed var(--border); }
.agent-model { font-family: ui-monospace, SFMono-Regular, monospace; font-size: 11px; }

/* Factory list */
.fac-row {
  background: var(--section-bg); border-radius: 8px;
  padding: 10px 12px; margin-bottom: 6px;
  border-left: 3px solid #999;
}
.fac-row.ok { border-left-color: #2ecc71; }
.fac-row.err { border-left-color: #e74c3c; }
.fac-row.inflight { border-left-color: #f39c12; }
.fac-id { font-weight: 600; font-size: 13px; word-break: break-all; }
.fac-meta1 { display: flex; gap: 10px; font-size: 12px; margin-top: 2px; flex-wrap: wrap; }
.fac-status { font-weight: 500; }
.fac-row.ok .fac-status { color: #2ecc71; }
.fac-row.err .fac-status { color: #e74c3c; }
.fac-row.inflight .fac-status { color: #f39c12; }
.fac-meta2 { font-size: 12px; margin-top: 2px; font-variant-numeric: tabular-nums; }
.fac-result { font-size: 11px; color: var(--muted); margin-top: 6px;
  max-height: 3em; overflow: hidden; text-overflow: ellipsis;
  white-space: nowrap;
}

.toast {
  position: fixed; left: 50%; bottom: 16px; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 18px;
  border-radius: 8px; font-size: 14px; z-index: 100;
  max-width: 90%; box-shadow: 0 4px 16px rgba(0,0,0,0.3);
}
.toast.error { background: var(--error); color: white; }
.toast.success { background: #2ecc71; color: white; }
.toast.warning { background: #f39c12; color: white; }
.toast[hidden] { display: none; }

.agent-actions {
  display: flex; gap: 6px; margin-top: 10px; padding-top: 8px;
  border-top: 1px dashed var(--border); flex-wrap: wrap;
}
.act-btn {
  padding: 6px 10px; border-radius: 6px;
  background: var(--section-bg); color: var(--text);
  border: 1px solid var(--border);
  font-size: 13px; cursor: pointer; flex: 1; min-width: 90px;
}
.act-btn:hover { background: var(--bg); }
.act-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.act-btn.warn { border-color: rgba(243, 156, 18, 0.5); color: #f39c12; }
.act-btn.danger { border-color: rgba(231, 76, 60, 0.5); color: #e74c3c; }

.modal {
  position: fixed; inset: 0; background: rgba(0, 0, 0, 0.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 200; padding: 16px;
}
.modal[hidden] { display: none; }
.modal-card {
  background: var(--bg); color: var(--text);
  border-radius: 12px; padding: 18px; max-width: 480px; width: 100%;
  box-shadow: 0 8px 32px rgba(0,0,0,0.3);
}
.modal-card h3 { margin: 0 0 8px; font-size: 16px; }
.modal-card p { margin: 0 0 14px; font-size: 14px; line-height: 1.4; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }
.modal-actions button {
  padding: 8px 14px; border-radius: 6px; border: 1px solid var(--border);
  font-size: 14px; cursor: pointer;
}
.modal-actions .ghost { background: var(--section-bg); color: var(--text); }
.modal-actions .danger {
  background: var(--accent); color: white; border-color: transparent;
  font-weight: 600;
}
