/* === Claw Monitor v3 — Redesigned === */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; margin: 0; padding: 0; }

:root {
  /* Palette — mature dark (tenacitOS-inspired) */
  --bg: #0d0d0f;
  --bg-subtle: #141418;
  --surface: #1a1a1f;
  --surface-hover: #222228;
  --border: #2a2a32;
  --border-light: #3a3a44;

  /* Text */
  --text: #e8e8ec;
  --text-dim: #8a8a96;
  --text-muted: #5a5a66;

  /* Accent */
  --accent: #6366f1;
  --accent-dim: rgba(99,102,241,0.12);
  --accent-glow: rgba(99,102,241,0.25);

  /* Semantic */
  --green: #22c55e;
  --green-dim: rgba(34,197,94,0.12);
  --orange: #f59e0b;
  --orange-dim: rgba(245,158,11,0.12);
  --red: #ef4444;
  --red-dim: rgba(239,68,68,0.12);
  --cyan: #06b6d4;
  --cyan-dim: rgba(6,182,212,0.12);
  --purple: #a855f7;
  --purple-dim: rgba(168,85,247,0.12);

  /* Layout */
  --nav-h: 64px;
  --status-h: 36px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --radius: 12px;
  --radius-lg: 16px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', Inter, system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  overflow-x: hidden;
  padding-bottom: calc(var(--nav-h) + var(--status-h) + var(--safe-bottom));
  -webkit-font-smoothing: antialiased;
}

a { color: inherit; text-decoration: none; }

/* === BUTTONS === */
button {
  background: var(--accent);
  color: white; border: 0; border-radius: var(--radius);
  padding: 10px 16px; font-size: 14px; font-weight: 600;
  cursor: pointer; min-height: 40px;
  transition: opacity .15s, transform .1s;
}
button:active { transform: scale(0.97); }
button:disabled { opacity: .4; cursor: default; }

/* === INPUTS === */
input, select {
  background: var(--bg-subtle); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 12px; font-size: 14px; width: 100%;
  min-height: 40px; transition: border-color .15s;
}
input:focus, select:focus { outline: none; border-color: var(--accent); }

/* ─── LOGIN ───────────────────────────────────────────────── */
#loginOverlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.85);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000; padding: 20px;
  backdrop-filter: blur(8px);
}
.login-card {
  width: 100%; max-width: 360px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 20px;
  padding: 32px 24px; text-align: center;
}
.login-card h2 { font-size: 26px; margin-bottom: 4px; font-weight: 700; }
.login-card .sub { color: var(--text-dim); margin-bottom: 24px; font-size: 13px; }
.login-card .field { margin-bottom: 12px; text-align: left; }
.login-card label { display: block; font-size: 12px; color: var(--text-dim); margin-bottom: 4px; font-weight: 500; }
.login-card button { width: 100%; margin-top: 8px; }
#loginMsg { color: var(--red); font-size: 12px; margin-top: 10px; min-height: 16px; }

/* ─── SIDEBAR ─────────────────────────────────────────────── */
.sidebar {
  display: none; /* mobile: hidden */
}
.brand {
  font-size: 16px; font-weight: 700; color: var(--text);
  margin-bottom: 24px; padding: 0 8px;
  display: flex; align-items: center; gap: 8px;
}
.brand-icon { color: var(--accent); font-size: 20px; }
.sidebar nav { display: flex; flex-direction: column; gap: 2px; }
.sidebar a {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 12px; border-radius: 8px;
  font-size: 13px; color: var(--text-dim);
  transition: background .15s, color .15s;
}
.sidebar a:hover { background: var(--surface-hover); color: var(--text); }
.sidebar a.active { background: var(--accent-dim); color: var(--accent); font-weight: 600; }

/* ─── BOTTOM NAV ──────────────────────────────────────────── */
.bottom-nav {
  position: fixed; bottom: var(--status-h); left: 0; right: 0;
  height: var(--nav-h);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex; z-index: 100;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.nav-item {
  flex: 0 0 auto; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  min-width: 52px; padding: 0 6px;
  gap: 3px; font-size: 10px; color: var(--text-dim);
  cursor: pointer; transition: color .15s;
}
.nav-item .nav-icon { font-size: 20px; line-height: 1; }
.nav-item.active { color: var(--accent); }

/* ─── STATUS BAR (bottom of screen) ───────────────────────── */
.status-bar {
  position: fixed; bottom: 0; left: 0; right: 0;
  height: var(--status-h);
  background: var(--bg-subtle);
  border-top: 1px solid var(--border);
  display: flex; align-items: center;
  padding: 0 12px; gap: 16px;
  font-size: 11px; color: var(--text-dim);
  z-index: 101; overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.status-item {
  display: flex; align-items: center; gap: 4px;
  white-space: nowrap; flex-shrink: 0;
}
.status-dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--green); flex-shrink: 0;
}
.status-dot.warn { background: var(--orange); }
.status-dot.err { background: var(--red); }
.status-label { color: var(--text-muted); }
.status-value { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }
.status-bar .sep { width: 1px; height: 14px; background: var(--border); flex-shrink: 0; }

/* ─── MAIN ────────────────────────────────────────────────── */
.page { display: none; padding: 16px; padding-bottom: 24px; }
.page.active { display: block; }
.page-title {
  font-size: 20px; font-weight: 700; margin-bottom: 16px;
  display: flex; align-items: center; gap: 10px;
}

/* ─── SEARCH OVERLAY (Ctrl+K) ─────────────────────────────── */
.search-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.7);
  z-index: 500; display: none; align-items: flex-start; justify-content: center;
  padding-top: 20vh; backdrop-filter: blur(4px);
}
.search-overlay.open { display: flex; }
.search-box {
  width: 100%; max-width: 520px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius-lg);
  padding: 16px; box-shadow: 0 16px 64px rgba(0,0,0,.5);
}
.search-input {
  width: 100%; background: var(--bg-subtle); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 14px; font-size: 16px; min-height: 48px;
}
.search-input:focus { outline: none; border-color: var(--accent); }
.search-results { margin-top: 12px; max-height: 300px; overflow-y: auto; }
.search-result {
  padding: 10px 12px; border-radius: 8px; cursor: pointer;
  display: flex; align-items: center; gap: 10px;
  font-size: 14px; color: var(--text-dim);
  transition: background .1s;
}
.search-result:hover, .search-result.active { background: var(--accent-dim); color: var(--text); }
.search-result-icon { font-size: 16px; width: 24px; text-align: center; }
.search-hint { font-size: 11px; color: var(--text-muted); margin-top: 8px; text-align: center; }

/* ─── REFRESH BAR ─────────────────────────────────────────── */
.refresh-bar {
  text-align: center; padding: 6px; font-size: 12px; color: var(--text-dim);
}
.refresh-bar .time { color: var(--accent); font-weight: 500; }

/* ─── SECTION LABEL ───────────────────────────────────────── */
.section-label {
  font-size: 11px; color: var(--text-muted); text-transform: uppercase;
  letter-spacing: .08em; margin: 20px 0 10px; font-weight: 600;
}

/* ─── METRIC CARDS (redesigned — larger, cleaner) ─────────── */
.cards {
  display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 16px;
}
.card {
  background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius-lg);
  padding: 16px; position: relative; overflow: hidden;
  transition: border-color .15s, box-shadow .15s;
}
.card.clickable { cursor: pointer; }
.card.clickable:hover { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent-dim); }
.card.clickable:active { transform: scale(0.98); }
.card.full { grid-column: 1 / -1; }
.card .icon-row {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 8px;
}
.card .card-icon { font-size: 24px; }
.card .label {
  font-size: 11px; color: var(--text-dim); text-transform: uppercase;
  letter-spacing: .04em; font-weight: 600;
}
.card .value {
  font-size: 32px; font-weight: 800; line-height: 1.1;
  font-variant-numeric: tabular-nums;
  margin: 4px 0;
}
.card .meta { font-size: 12px; color: var(--text-dim); line-height: 1.4; }
.card.highlight { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent-dim); }
.card.danger { border-color: var(--red); box-shadow: 0 0 0 1px var(--red-dim); }
.card.success { border-color: var(--green); }

/* Color-coded metric accents */
.card .value.green { color: var(--green); }
.card .value.orange { color: var(--orange); }
.card .value.red { color: var(--red); }
.card .value.cyan { color: var(--cyan); }
.card .value.purple { color: var(--purple); }

/* ─── LIST ITEMS ──────────────────────────────────────────── */
.list { display: grid; gap: 8px; margin-bottom: 16px; }
.item {
  background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 14px; cursor: pointer;
  transition: border-color .15s, background .15s;
}
.item:hover { background: var(--surface-hover); border-color: var(--border-light); }
.item.level-critical { border-left: 3px solid var(--red); }
.item.level-warning { border-left: 3px solid var(--orange); }
.item.level-info { border-left: 3px solid var(--accent); }
.item .item-title { font-weight: 600; font-size: 14px; margin-bottom: 2px; }
.item .item-sub { font-size: 12px; color: var(--text-dim); }
.item .item-badges { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }

/* ─── BADGES ──────────────────────────────────────────────── */
.badge {
  display: inline-block; padding: 3px 8px; border-radius: 999px;
  font-size: 10px; font-weight: 600; background: var(--border);
  color: var(--text-dim);
}
.badge.ok { background: var(--green-dim); color: var(--green); }
.badge.warn { background: var(--orange-dim); color: var(--orange); }
.badge.err { background: var(--red-dim); color: var(--red); }
.badge.info { background: var(--accent-dim); color: var(--accent); }
.badge.dim { background: var(--bg-subtle); color: var(--text-muted); }

/* ─── AGENTS TREE (tier-colored borders) ──────────────────── */
.agents-tree { display: flex; flex-direction: column; align-items: center; gap: 0; }

.tree-node { width: 100%; max-width: 320px; }
.tree-node-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 14px; cursor: pointer;
  transition: border-color .15s, box-shadow .15s, transform .1s;
  margin: 0 auto;
}
.tree-node-card:hover { box-shadow: 0 0 0 2px var(--accent-dim); transform: translateY(-1px); }
.tree-node-card:active { transform: scale(0.98); }

/* Tier colors */
.tree-node-card[data-tier="orchestrator"] { border-left: 4px solid var(--orange); }
.tree-node-card[data-tier="specialists"] { border-left: 4px solid var(--green); }
.tree-node-card[data-tier="execution_backends"] { border-left: 4px solid var(--cyan); }
.tree-node-card[data-tier="experimental"] { border-left: 4px solid var(--purple); }
.tree-node-card[data-tier="default"] { border-left: 4px solid var(--text-muted); }

.tree-node-level-0 .tree-node-card { max-width: 360px; border-width: 2px; }

.tree-node-header { display: flex; align-items: center; gap: 6px; margin-bottom: 4px; }
.tree-node-icon { font-size: 14px; }
.tree-node-name { font-weight: 600; font-size: 14px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree-node-tier {
  font-size: 9px; color: var(--text-dim); background: var(--bg-subtle);
  padding: 2px 6px; border-radius: 4px; text-transform: uppercase; font-weight: 600;
  letter-spacing: .04em;
}
.tree-node-status { font-size: 12px; color: var(--text-dim); line-height: 1.3; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.tree-branch-line { width: 2px; height: 20px; background: var(--border); margin: 0 auto; }
.tree-level {
  display: flex; flex-wrap: wrap; justify-content: center; gap: 10px;
  position: relative; padding: 8px 0; width: 100%;
}
.tree-level::before {
  content: ''; position: absolute; top: 0; left: 25%; right: 25%;
  height: 2px; background: var(--border);
}

/* Collapsed section */
.agents-collapsed { margin-top: 20px; border-top: 1px solid var(--border); padding-top: 12px; }
.collapsed-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 14px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; cursor: pointer; font-size: 13px; color: var(--text-dim);
  transition: border-color .15s;
}
.collapsed-header:hover { border-color: var(--accent); color: var(--text); }
.collapsed-content { display: none; padding-top: 10px; }
.collapsed-content.show { display: flex; }
.collapsed-grid { display: flex; flex-wrap: wrap; gap: 6px; }
.collapsed-item {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 6px 10px; cursor: pointer; display: flex; gap: 8px; align-items: center;
  font-size: 12px; transition: border-color .15s;
}
.collapsed-item:hover { border-color: var(--accent); }

/* ─── ACTIONS GRID ────────────────────────────────────────── */
.actions-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.action-btn {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px; text-align: center;
  cursor: pointer; font-size: 13px; font-weight: 600;
  transition: border-color .15s, transform .1s;
  color: var(--text);
}
.action-btn:hover { border-color: var(--accent); }
.action-btn:active { transform: scale(0.97); }

/* ─── CHAT ────────────────────────────────────────────────── */
.chat-box {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: 16px;
}
.chat-messages { min-height: 100px; max-height: 360px; overflow-y: auto; margin-bottom: 12px; }
.chat-msg {
  padding: 10px 12px; margin-bottom: 6px; border-radius: var(--radius);
  font-size: 13px; line-height: 1.5;
}
.chat-msg.bot { background: var(--accent-dim); }
.chat-msg.user { background: var(--green-dim); text-align: right; }
.chat-input-row { display: flex; gap: 8px; }
.chat-input-row input { flex: 1; }
.chat-hints { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.chat-hint {
  background: var(--bg-subtle); border: 1px solid var(--border);
  border-radius: 999px; padding: 5px 12px; font-size: 11px;
  color: var(--text-dim); cursor: pointer;
  transition: border-color .15s, color .15s;
}
.chat-hint:hover { border-color: var(--accent); color: var(--text); }

/* ─── TOPOLOGY ────────────────────────────────────────────── */
.topo-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.topo-col {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px;
}
.topo-col h4 { font-size: 12px; color: var(--text-dim); margin-bottom: 8px; text-transform: uppercase; letter-spacing: .04em; }
.topo-node {
  background: var(--bg-subtle); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 10px; margin-bottom: 6px; font-size: 12px;
}
.topo-node .tn-name { font-weight: 600; margin-bottom: 2px; }

/* ─── LOGS ────────────────────────────────────────────────── */
#logs-content {
  background: var(--bg-subtle); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px;
  font-family: 'SF Mono', 'Cascadia Code', 'Fira Code', monospace;
  font-size: 12px; line-height: 1.5; color: var(--text-dim);
  overflow-x: auto; white-space: pre-wrap; word-break: break-all;
  max-height: 70vh;
}
.log-select-btn {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 999px; padding: 6px 14px; font-size: 12px;
  color: var(--text-dim); cursor: pointer;
  transition: border-color .15s, color .15s;
}
.log-select-btn:hover { color: var(--text); }
.log-select-btn.active { border-color: var(--accent); color: var(--accent); background: var(--accent-dim); }

/* ─── LOADING ─────────────────────────────────────────────── */
.spinner {
  width: 24px; height: 24px; border: 3px solid var(--border);
  border-top-color: var(--accent); border-radius: 50%;
  animation: spin .6s linear infinite; margin: 20px auto;
}
@keyframes spin { to { transform: rotate(360deg); } }
.empty-state { text-align: center; padding: 32px; color: var(--text-dim); font-size: 14px; }
.empty-state .emoji { font-size: 40px; margin-bottom: 8px; display: block; }

/* ─── MODAL ───────────────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.7);
  z-index: 200; display: none; align-items: flex-end; justify-content: center;
  padding: 0; backdrop-filter: blur(2px);
}
.modal-overlay.open { display: flex; }
.modal-sheet {
  width: 100%; max-width: 560px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 20px 20px 0 0;
  padding: 20px 16px; max-height: 80dvh; overflow-y: auto;
  animation: slideUp .2s ease-out;
}
@keyframes slideUp { from { transform: translateY(100%); } }
.modal-handle { width: 40px; height: 4px; background: var(--border-light); border-radius: 99px; margin: 0 auto 16px; }
.modal-close {
  background: none; border: none; color: var(--text-dim);
  font-size: 20px; padding: 4px; min-height: auto; cursor: pointer;
  float: right; margin-top: -8px;
}
.modal-close:hover { color: var(--text); }

/* ─── DESKTOP ─────────────────────────────────────────────── */
@media (min-width: 768px) {
  body { padding-bottom: 0; }
  .bottom-nav { display: none; }
  .status-bar {
    position: fixed; bottom: 0; left: 200px; right: 0;
    border-top: 1px solid var(--border);
  }

  .sidebar {
    position: fixed !important;
    top: 0; left: 0; bottom: 0;
    width: 200px;
    z-index: 100;
    background: var(--bg);
    border-right: 1px solid var(--border);
    padding: 20px 12px;
    display: flex !important;
    flex-direction: column;
    overflow-y: auto;
  }

  .main {
    margin-left: 200px;
    padding: 24px 28px;
    max-width: calc(100% - 200px);
    padding-bottom: 48px;
  }

  .page { padding: 0; }
  .cards { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
  .actions-grid { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }
  .topo-cols { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
}

/* ─── MOBILE (cleanup) ───────────────────────────────────── */
@media (max-width: 767px) {
  .sidebar { display: none !important; }
}

/* ─── CLICKABLE ───────────────────────────────────────────── */
.clickable { cursor: pointer; transition: border-color .15s, transform .1s; }
.clickable:hover { border-color: var(--accent); }
.clickable:active { transform: scale(0.98); }
