/* Agent Monitor. Tokens first. graph.js reads the same custom properties
   for the Cytoscape stylesheet, so light and dark are defined once. */

/* The `hidden` attribute always hides. Without this, any class that sets
   `display` (.badge, .btn, …) beats the user-agent rule and the element shows:
   the "mock data" badge appeared on the live page that way. */
[hidden] { display: none !important; }

:root {
  color-scheme: light;
  --surface: #fcfcfb;
  --surface-raised: #ffffff;
  --surface-sunken: #f3f2ef;
  --text: #0b0b0b;
  --text-muted: #52514e;
  --border: #d9d7d0;
  --tok-person: #2a78d6;
  --tok-auth: #eb6834;
  --tok-chain: #1baf7a;
  --budget: #4a3aa7;
  --budget-track: #dddbe9;
  --good: #0a8a0a;
  --warning: #8a5a00;
  --critical: #d03b3b;
  --focus: #2a78d6;
  --radius: 8px;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --surface: #1a1a19;
    --surface-raised: #242422;
    --surface-sunken: #121211;
    --text: #ffffff;
    --text-muted: #c3c2b7;
    --border: #3d3d39;
    --tok-person: #3987e5;
    --tok-auth: #d95926;
    --tok-chain: #199e70;
    --budget: #9085e9;
    --budget-track: #3a3852;
    --good: #2fbf2f;
    --warning: #fab219;
    --critical: #e66767;
    --focus: #6da7ec;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--surface);
  color: var(--text);
  font: 14px/1.45 var(--font);
  display: flex;
  flex-direction: column;
}

h2, h3, h4, h5 { margin: 0; font-weight: 600; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }
h4 { font-size: 13px; }
h5 { font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-muted); margin: 14px 0 4px; }
p { margin: 6px 0; }
code { font: 12px var(--mono); }
hr { border: 0; border-top: 1px solid var(--border); margin: 16px 0; }

:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.grow { flex: 1; }
.dim { color: var(--text-muted); }
.break { overflow-wrap: anywhere; }
.error { color: var(--critical); min-height: 1.2em; }

/* ── Top bar ── */

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-raised);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; color: inherit; text-decoration: none; }
.brand-name { font-size: 16px; font-weight: 600; }

.status { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-muted); }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-muted); }
.status[data-state="live"] .status-dot { background: var(--good); }
.status[data-state="live"] { color: var(--text); }
.status[data-state="reconnecting"] .status-dot,
.status[data-state="connecting"] .status-dot,
.status[data-state="loading"] .status-dot { background: var(--warning); animation: blink 1s infinite alternate; }
.status[data-state="revoked"] .status-dot,
.status[data-state="error"] .status-dot { background: var(--critical); }
@keyframes blink { from { opacity: 1; } to { opacity: 0.3; } }

.banner { padding: 8px 16px; background: var(--surface-sunken); border-bottom: 1px solid var(--border); color: var(--critical); }

/* ── Account menu ── */

.account { position: relative; }
.btn.account-button { display: inline-flex; align-items: center; gap: 8px; max-width: min(340px, 70vw); }
.account-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.caret { color: var(--text-muted); font-size: 11px; }
.acct-dot { width: 9px; height: 9px; border-radius: 50%; flex: none; border: 1.5px solid var(--text-muted); }
.acct-dot.live { background: var(--good); border-color: var(--good); }
.acct-dot.warn { background: var(--warning); border-color: var(--warning); }
.acct-dot.bad { background: var(--critical); border-color: var(--critical); }
.acct-unseen { min-width: 18px; padding: 0 5px; border-radius: 9px; background: var(--tok-person); color: #fff; font-size: 11px; line-height: 18px; text-align: center; font-weight: 600; }
.acct-unseen[hidden] { display: none; }
.account-menu {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  z-index: 30;
  width: min(380px, calc(100vw - 24px));
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-raised);
  box-shadow: 0 8px 30px rgb(0 0 0 / 0.2);
}
.account-menu[hidden] { display: none; }
.acct-row { display: flex; align-items: center; gap: 6px; border-radius: var(--radius); }
.acct-row.is-current { background: var(--surface-sunken); }
.acct-select { flex: 1; min-width: 0; display: flex; align-items: center; gap: 10px; padding: 8px; border: 0; border-radius: var(--radius); background: none; color: inherit; font: inherit; text-align: left; cursor: pointer; }
.acct-select:hover { background: var(--surface-sunken); }
.acct-text { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.acct-label { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.acct-sub { font-size: 12px; color: var(--text-muted); }
.acct-remove { margin-right: 6px; }
.acct-confirm { border: 1px solid var(--critical); border-radius: var(--radius); padding: 10px; margin: 4px 0; font-size: 13px; }
.acct-actions { display: flex; gap: 8px; padding: 8px 4px 2px; margin-top: 6px; border-top: 1px solid var(--border); }

/* ── Token popup ── */

.token-popup {
  position: fixed;
  z-index: 50;
  width: min(440px, calc(100vw - 16px));
  max-height: min(70vh, 560px);
  overflow: auto;
  padding: 10px 12px 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-raised);
  color: var(--text);
  box-shadow: 0 10px 34px rgb(0 0 0 / 0.28);
  font-size: 12px;
}
.token-popup[hidden] { display: none; }
.token-popup[data-pinned="true"] { border-color: var(--focus); }
.token-popup h5 { margin: 8px 0 0; }
.tp-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; margin-bottom: 4px; }
.tp-title { min-width: 0; font-size: 13px; }
.tp-actions { display: flex; gap: 4px; flex: none; }
.tp-chips { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 8px; padding-bottom: 8px; border-bottom: 1px solid var(--border); }
.tp-chip { border: 1px solid var(--border); border-left-width: 4px; border-radius: 999px; padding: 1px 8px; background: none; color: var(--text); font: 11px var(--mono); cursor: pointer; }
.tp-chip.person { border-left-color: var(--tok-person); }
.tp-chip.auth { border-left-color: var(--tok-auth); }
.tp-chip[aria-selected="true"] { background: var(--surface-sunken); border-color: var(--text); }
.token-badges { margin: 2px 0; }
.token-times { margin: 6px 0; }
.json.json-pre { margin: 4px 0 0; white-space: pre; max-height: 260px; overflow: auto; tab-size: 2; user-select: text; }
.graph.has-hover { cursor: pointer; }

/* ── Layout: graph left, panels right. Phone: graph above, panels below. ── */

.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(340px, 420px);
}

.graph-panel { position: relative; display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--border); }
.graph-toolbar { position: absolute; top: 10px; left: 12px; z-index: 5; display: flex; gap: 6px; flex-wrap: wrap; }
.graph { flex: 1; min-height: 320px; }
/* The rule Cytoscape would inject (see index.html). */
.__________cytoscape_container { position: relative; }

.legend {
  list-style: none;
  margin: 0;
  padding: 8px 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 4px 16px;
  font-size: 11px;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
  background: var(--surface-raised);
}
.legend li { display: inline-flex; align-items: center; gap: 6px; }
.key { display: inline-block; flex: none; }
.key.line { width: 26px; height: 0; border-top: 3px solid var(--text-muted); }
.key.line.person { border-top-color: var(--tok-person); }
.key.line.auth { border-top-color: var(--tok-auth); }
.key.line.policy { border-top: 3px dashed var(--tok-auth); }
.key.line.inactive { border-top-width: 1px; opacity: 0.45; }
.key.line.chain { border-top: 3px dotted var(--tok-chain); }
.key.line.as { border-top-width: 2px; }
.key.ring { width: 14px; height: 14px; border-radius: 50%; border: 3px solid var(--budget-track); border-top-color: var(--budget); border-right-color: var(--budget); }
.key.self { width: 14px; height: 14px; border-radius: 3px; border: 4px double var(--good); }

.side { display: flex; flex-direction: column; min-height: 0; background: var(--surface-raised); }

.detail { padding: 12px 14px; border-bottom: 1px solid var(--border); max-height: 55%; overflow: auto; background: var(--surface); }
.detail-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; margin-bottom: 6px; }
.detail-block { border-top: 1px solid var(--border); margin-top: 12px; padding-top: 10px; }

.tabs { display: flex; border-bottom: 1px solid var(--border); }
.tabs [role="tab"] {
  flex: 1;
  padding: 10px 6px;
  border: 0;
  border-bottom: 2px solid transparent;
  background: none;
  color: var(--text-muted);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.tabs [role="tab"][aria-selected="true"] { color: var(--text); border-bottom-color: var(--text); }
.count { font-weight: 400; color: var(--text-muted); font-size: 12px; }

.panel { flex: 1; min-height: 0; overflow: auto; padding: 8px 10px 16px; }
.panel[hidden] { display: none; }
.panel-summary { color: var(--text-muted); font-size: 12px; margin: 2px 4px 8px; }
.empty, .note { color: var(--text-muted); font-size: 13px; }
.note.ok { color: var(--good); }
.note.error { color: var(--critical); }

/* ── Rows ── */

ul.rows, ol.rows, ul.plain { list-style: none; margin: 0; padding: 0; }
ul.plain > li { padding: 4px 0; }
ul.plain ul.plain { padding-left: 14px; }

.row {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 3px;
  text-align: left;
  padding: 8px;
  margin: 0 0 4px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: none;
  color: inherit;
  font: inherit;
  cursor: pointer;
}
.row:hover:not(:disabled) { background: var(--surface-sunken); }
.row:disabled { cursor: default; }
.row.is-selected { border-color: var(--focus); background: var(--surface-sunken); }
.row.is-inactive .row-title { color: var(--text-muted); }
.row-head { display: flex; align-items: center; gap: 6px; min-width: 0; }
.row-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-sub { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 6px; font-size: 12px; color: var(--text-muted); }
.state-dot { width: 8px; height: 8px; border-radius: 50%; border: 1.5px solid var(--text-muted); flex: none; }
.state-dot.on { background: var(--good); border-color: var(--good); }
.countdown, .ago { font-variant-numeric: tabular-nums; font-size: 12px; color: var(--text-muted); white-space: nowrap; }
.countdown { font-family: var(--mono); color: var(--text); }

.badge {
  display: inline-block;
  padding: 0 6px;
  border-radius: 999px;
  border: 1px solid var(--border);
  font-size: 11px;
  line-height: 17px;
  color: var(--text-muted);
  white-space: nowrap;
  margin-right: 2px;
}
/* Text stays in text colours. The left rule carries the series colour. */
.badge.person { border-left: 4px solid var(--tok-person); color: var(--text); }
.badge.auth { border-left: 4px solid var(--tok-auth); color: var(--text); }
.badge.chain { border-left: 4px solid var(--tok-chain); color: var(--text); }
.badge.good { border-color: var(--good); color: var(--text); }
.badge.bad { border-color: var(--critical); color: var(--text); }
.badge.warn { border-color: var(--warning); border-style: dashed; color: var(--text); }

.token { border: 1px solid var(--border); border-radius: var(--radius); padding: 8px !important; margin-bottom: 6px; font-size: 12px; }
.consent { margin: 4px 0; font-size: 13px; }

/* ── Tokens panel and token inspector ── */

.panel-bar { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.toggle[aria-pressed="true"] { background: var(--text); color: var(--surface); border-color: var(--text); }
.row.is-recent { opacity: 0.62; }
.row.is-recent .row-title { font-weight: 400; text-decoration: line-through; text-decoration-color: var(--text-muted); }
.row.is-recent:hover, .row.is-recent:focus-visible, .row.is-recent.is-selected { opacity: 1; }
.badge.muted { border-style: dotted; }
.mono { font-family: var(--mono); font-size: 12px; }

.json-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 8px; }
.json {
  margin-top: 4px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-sunken);
  font: 12px/1.55 var(--mono);
  overflow-x: auto;
  white-space: nowrap;
}
.json-children { padding-left: 16px; border-left: 1px solid var(--border); margin-left: 4px; }
.json-node > summary { cursor: pointer; color: inherit; margin: 0; }
.json-line { padding-left: 14px; }
.json-key { color: var(--text-muted); }
.json-string { color: var(--text); }
.json-number, .json-boolean { color: var(--budget); }
.json-null { color: var(--text-muted); font-style: italic; }

/* ── Budget ── */

.budget, .ledger { margin-top: 8px; }
.ledger { border: 1px solid var(--border); border-radius: var(--radius); padding: 8px; font-size: 12px; }
.budget-head { display: flex; justify-content: space-between; gap: 8px; font-size: 12px; color: var(--text-muted); }
.budget-value { color: var(--text); font-variant-numeric: tabular-nums; }
.meter { height: 8px; border-radius: 4px; background: var(--budget-track); overflow: hidden; margin-top: 4px; display: flex; gap: 2px; }
.meter-fill { height: 100%; background: var(--budget); border-radius: 4px; min-width: 0; transition: width 400ms ease; }
.meter-fill.high { background: var(--critical); }
.meter-fill.reserved { background: var(--tok-person); }
.swatch { display: inline-block; width: 8px; height: 8px; border-radius: 2px; margin-right: 5px; }
.swatch.metered { background: var(--budget); }
.swatch.reserved { background: var(--tok-person); }
.swatch.available { background: var(--budget-track); }

dl.kv { display: grid; grid-template-columns: max-content 1fr; gap: 3px 12px; margin: 8px 0; font-size: 13px; }
dl.kv dt { color: var(--text-muted); }
dl.kv dd { margin: 0; }
dl.kv.compact { font-size: 12px; }
details summary { cursor: pointer; color: var(--text-muted); margin-top: 6px; }

/* ── Buttons ── */

.btn {
  display: inline-block;
  padding: 6px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface-raised);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  min-height: 32px;
}
.btn:hover { background: var(--surface-sunken); }
.btn.small { padding: 3px 10px; font-size: 12px; min-height: 28px; }
.btn.primary { background: var(--text); color: var(--surface); border-color: var(--text); }
.btn.danger { background: var(--critical); border-color: var(--critical); color: #fff; }
.btn.danger-outline { border-color: var(--critical); color: var(--critical); }
.linklike { border: 0; background: none; padding: 0; font: inherit; color: var(--text); text-decoration: underline; cursor: pointer; text-align: left; }
.row-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.confirm { border: 1px solid var(--critical); border-radius: var(--radius); padding: 10px; margin-top: 8px; }

/* ── Overlay and dialog ── */

.overlay { position: absolute; inset: 0; z-index: 10; display: grid; place-items: center; padding: 16px; background: color-mix(in srgb, var(--surface) 80%, transparent); }
.overlay[hidden] { display: none; }
.overlay-card { max-width: 440px; padding: 20px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface-raised); box-shadow: 0 8px 30px rgb(0 0 0 / 0.18); }

dialog { border: 1px solid var(--border); border-radius: 12px; background: var(--surface-raised); color: var(--text); padding: 20px; width: min(460px, calc(100vw - 32px)); }
dialog::backdrop { background: rgb(0 0 0 / 0.45); }
dialog label { display: block; margin: 12px 0 4px; font-weight: 600; }
dialog input { width: 100%; padding: 8px; border-radius: var(--radius); border: 1px solid var(--border); background: var(--surface); color: var(--text); font: 13px var(--mono); }

/* ── Phone ── */

@media (max-width: 900px) {
  html, body { height: auto; }
  .layout { display: block; }
  .graph-panel { border-right: 0; border-bottom: 1px solid var(--border); }
  .graph { height: 58vh; min-height: 300px; flex: none; }
  .detail { max-height: none; }
  .panel { overflow: visible; }
  .row { padding: 10px 8px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
