/* Agent Monitoring: Traces (viewer 1's stylesheet, copied). Opened full screen or in an
   iframe, which cannot see the site's own light/dim switch, so it follows
   the reader's system preference, as the other viewers do (their controls,
   cards and flights are copied here, so the series look alike). */

:root {
  --bg: #ffffff;
  --card: #ffffff;
  --border: #dfe3e8;
  --text: #22272e;
  --dim: #5f6b78;
  --accent: #326ce5;
  --btn: #f4f5f7;
  --panel: #0d2436;
  --panel-text: #eaf1f6;
  --raw: #f7f8fa;
  --flash: rgba(255, 196, 0, 0.3);
  --code-bg: #f6f8fa;
  --ok: #1f883d;
  --late: #cf222e;
  --busy: #bf8700;
  /* the runs: ours, for the reader; the records carry none of them */
  --rA: #2563eb; --rB: #0d9488; --rC: #ea580c; --rD: #7c3aed; --rE: #db2777;
  /* the spans, by what they are */
  --s-run: #64748b; --s-agent: #3b82f6; --s-chat: #8b5cf6; --s-tool: #d97706;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1d2b;
    --card: #112b40;
    --border: #234862;
    --text: #eaf1f6;
    --dim: #9fb2c2;
    --accent: #6f9bff;
    --btn: #16374f;
    --panel: #081b29;
    --raw: #0d2233;
    --flash: rgba(255, 196, 0, 0.18);
    --code-bg: #0d2233;
    --ok: #4ac26b;
    --late: #ff7b72;
    --busy: #e3b341;
    --rA: #6f9bff; --rB: #2dd4bf; --rC: #fb923c; --rD: #a78bfa; --rE: #f472b6;
    --s-run: #94a3b8; --s-agent: #60a5fa; --s-chat: #a78bfa; --s-tool: #f59e0b;
  }
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; }
code, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.dim { color: var(--dim); font-weight: 400; }
.kv-app { padding: 0.5rem 16px 0.8rem; max-width: 1800px; margin: 0 auto; }
.kv-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0.2rem 0.9rem; margin: 0 0 0.45rem; }
.kv-head h1 { font-size: 1.3rem; margin: 0; letter-spacing: -0.01em; }
.kv-head p { margin: 0; color: var(--dim); font-size: 0.92rem; }
.in-viewer .kv-head { display: none; }

/* --- the controls, with the step's explanation beside them */
.am-top { display: grid; grid-template-columns: 15rem minmax(0, 1fr); gap: 12px; height: 128px; margin-bottom: 10px; }
.kv-bar { display: flex; flex-direction: column; gap: 0.45rem; }
.kv-bar .row { display: flex; align-items: center; gap: 0.5rem; }
.kv-bar .count { font-family: ui-monospace, Menlo, monospace; font-size: 0.8rem; color: var(--dim); margin-left: auto; }
.kv-btn { background: var(--btn); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 0.35rem 0.9rem; font-size: 0.88rem; cursor: pointer; font-family: inherit; }
.kv-btn:hover:not(:disabled) { border-color: var(--accent); }
.kv-btn:disabled { opacity: 0.45; cursor: default; }
.kv-btn.play { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: left; }
.kv-bar select { background: var(--btn); color: var(--text); border: 1px solid var(--border); border-radius: 7px;
  padding: 0.3rem 0.4rem; font-size: 0.86rem; font-family: inherit; min-width: 0; max-width: 100%; }
.am-explain { overflow: auto; border: 1px solid var(--border); border-left: 4px solid var(--accent); border-radius: 10px;
  padding: 0.45rem 0.8rem; font-size: 0.86rem; line-height: 1.42; background: var(--card); }
.am-explain p { margin: 0 0 0.3rem; }
.am-explain .main { font-weight: 650; font-size: 1.02rem; }
.am-explain ul.steps { margin: 0 0 0.3rem; padding-left: 1.1rem; columns: 2 26rem; column-gap: 2rem; }
.am-explain ul.steps li { break-inside: avoid; margin: 0 0 0.15rem; }
.am-explain ul.steps ul.steps { columns: auto; margin: 0.1rem 0; list-style: circle; }
.am-explain code { font-size: 0.92em; background: var(--code-bg); padding: 0 0.2em; border-radius: 3px; }

/* --- the stage: the crew over the three records, their gaps at the bottom */
.am-stage { position: relative; display: grid; gap: 10px;
  grid-template-columns: minmax(0, 0.95fr) minmax(0, 1.05fr);
  grid-template-rows: 172px minmax(0, 1fr) auto;
  grid-template-areas: "crew crew" "logs jaeger" "gaps gaps";
  height: max(520px, calc(100vh - 128px - 3.6rem)); }
@media (min-width: 1101px) { .in-viewer .am-stage { height: max(480px, calc(100vh - 128px - 1.4rem)); } }
.am-crew { grid-area: crew; } #am-logs { grid-area: logs; } #am-jaeger { grid-area: jaeger; }
.am-gaps { grid-area: gaps; }
@media (max-width: 1100px) {
  .am-top { grid-template-columns: minmax(0, 1fr); height: auto; position: sticky; top: 0; z-index: 40; background: var(--bg);
    margin: 0 -16px 10px; padding: 0.4rem 16px; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12); }
  .kv-bar { flex-direction: row; flex-wrap: wrap; }
  .am-explain { max-height: 30vh; }
  .am-stage { grid-template-columns: minmax(0, 1fr); height: auto;
    grid-template-rows: auto 420px 640px auto; grid-template-areas: "crew" "logs" "jaeger" "gaps"; }
}

.kv-card { display: flex; flex-direction: column; min-height: 0; min-width: 0; overflow: hidden;
  border: 1px solid var(--border); border-radius: 12px; background: var(--card); font-size: 0.84rem;
  transition: box-shadow 0.25s, border-color 0.25s, opacity 0.4s; }
.kv-card.here { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 35%, transparent); }
.kv-card > header { flex: none; display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap;
  padding: 0.4rem 0.7rem 0.35rem; border-bottom: 1px solid var(--border); }
.kv-card > header h2 { font-size: 0.92rem; margin: 0; flex: 1; min-width: 0; }
.kv-card > header h2 code { font-size: 0.72rem; color: var(--dim); font-weight: 400; margin-left: 0.3rem; }
.kv-body { flex: 1; min-height: 0; overflow: auto; padding: 0.45rem 0.7rem; }
h3 { font-size: 0.66rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--dim); margin: 0 0 0.3rem; }

/* a run's letter: ours, for the reader */
.run { display: inline-block; min-width: 1.25rem; text-align: center; font: 700 0.72rem ui-monospace, Menlo, monospace;
  color: #fff; border-radius: 4px; padding: 0.02rem 0.25rem; margin-right: 0.15rem; background: var(--r); }
.r-A { --r: var(--rA); } .r-B { --r: var(--rB); } .r-C { --r: var(--rC); } .r-D { --r: var(--rD); } .r-E { --r: var(--rE); }

/* --- the crew: requests, the four agents, the APIs they call */
.am-crew .clock { font: 600 0.8rem ui-monospace, Menlo, monospace; color: var(--dim); }
.endpoint { font-size: 0.74rem; border: 1px solid var(--border); border-radius: 5px; padding: 0.05rem 0.4rem; background: var(--raw); }
.crew-body { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(0, 4fr) minmax(0, 0.9fr); gap: 12px; overflow: hidden; }
.reqs { min-height: 0; overflow: auto; }
.reqs ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 0.18rem; }
.reqs li { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; gap: 0.3rem; align-items: baseline; font-size: 0.76rem; }
.reqs .q { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.reqs .st { font-size: 0.7rem; white-space: nowrap; }
.st.done { color: var(--ok); } .st.busy { color: var(--busy); } .st.late { color: var(--late); font-weight: 700; }
.reqs p { margin: 0.25rem 0 0; font-size: 0.72rem; }
.agents { display: flex; align-items: stretch; gap: 4px; min-height: 0; border-radius: 8px; }
.agent { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 0.22rem; border: 1px solid var(--border); border-radius: 9px;
  padding: 0.35rem 0.45rem; background: var(--raw); overflow: hidden; }
.agent b { font-size: 0.86rem; }
.agent .idle { color: var(--dim); font-size: 0.74rem; }
.work { font: 0.72rem ui-monospace, Menlo, monospace; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; border-radius: 4px; }
.hand { align-self: center; color: var(--dim); font-weight: 700; }
.svcs { display: flex; flex-direction: column; gap: 6px; min-height: 0; }
.svc { flex: 1; border: 1px solid var(--border); border-radius: 8px; padding: 0.25rem 0.45rem; display: flex; flex-wrap: wrap; align-items: baseline; gap: 0.1rem 0.4rem; }
.svc b { font-size: 0.8rem; }
.svc code { font-size: 0.72rem; color: var(--dim); }
.svc .calls { flex-basis: 100%; min-height: 1rem; }
.svc.slow { border-color: var(--late); }
.badge { font-size: 0.68rem; font-weight: 700; color: var(--late); }

/* --- logs: lines of text; each carries its trace ID and span ID */
.logs-body { padding: 0.35rem 0.5rem; }
.lines { list-style: none; margin: 0; padding: 0; font: 0.71rem/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.lines li { padding: 0 0.25rem; border-radius: 3px; white-space: pre-wrap; overflow-wrap: anywhere; }
.lines .t { color: var(--dim); }
.lines .lv { color: var(--ok); }
.lines .who { font-weight: 700; }
.lines li.more { color: var(--dim); text-align: center; font-style: italic; padding: 0.15rem 0; }

/* --- the trace: spans as bars on the run's time line */
.trace-body { padding: 0.35rem 0.6rem; }
.tid { font-size: 0.72rem; color: var(--dim); }
.ticks { position: relative; height: 1rem; margin-left: 42%; border-bottom: 1px solid var(--border); font: 0.66rem ui-monospace, Menlo, monospace; color: var(--dim); }
.ticks span { position: absolute; transform: translateX(-50%); }
.ticks span:first-child { transform: none; }
.spans { list-style: none; margin: 0.2rem 0 0; padding: 0; }
.spans li { display: grid; grid-template-columns: 42% minmax(0, 1fr); align-items: center; min-height: 1.35rem; border-radius: 4px; }
.spans .nm { padding-left: calc(var(--d) * 0.85rem); font: 0.72rem ui-monospace, Menlo, monospace; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.spans .track { position: relative; height: 1.1rem; }
.spans .bar { position: absolute; top: 0.2rem; height: 0.7rem; border-radius: 3px; background: var(--c); }
.spans .dur { position: absolute; top: 0; padding: 0 0.3rem; font: 0.66rem ui-monospace, Menlo, monospace; color: var(--dim); white-space: nowrap; }
.spans .k-run { --c: var(--s-run); } .spans .k-agent { --c: var(--s-agent); } .spans .k-chat { --c: var(--s-chat); } .spans .k-tool { --c: var(--s-tool); }
/* open: still in the program, not yet sent; Jaeger has nothing of it */
.spans li.open .bar { background: repeating-linear-gradient(90deg, color-mix(in srgb, var(--c) 35%, transparent) 0 6px, transparent 6px 10px);
  border: 1px dashed var(--c); }
.spans .dur.in { background: var(--card); border-radius: 3px; top: 0.05rem; line-height: 1; padding: 0.1rem 0.3rem; }
.spans li.open .nm, .spans li.open .dur { color: var(--dim); font-style: italic; }

/* --- a step's spotlight: what it names pulses, the rest dims */
.am-stage.spotlight .kv-card:not(.here) { opacity: 0.35; }
.am-stage .act { outline: 3px solid color-mix(in srgb, var(--accent) 60%, transparent); outline-offset: 1px;
  background-color: color-mix(in srgb, var(--flash) 80%, transparent); }

/* --- what is in the air: a message flying along its arrow */
#am-fly { position: absolute; left: 0; top: 0; width: 0; height: 0; overflow: visible; pointer-events: none; z-index: 50; }
#am-fly .trail { position: absolute; left: 0; top: 0; overflow: visible; }
#am-fly .trail polyline { fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-linecap: round; stroke-dasharray: 3 7; }
#am-fly .trail path { fill: var(--accent); }
#am-fly .msg { position: absolute; transform: translate(-50%, -50%); background: var(--panel); color: var(--panel-text);
  border-radius: 6px; padding: 0.12rem 0.5rem; font-size: 0.74rem; font-weight: 600; white-space: nowrap;
  font-family: ui-monospace, Menlo, Consolas, monospace; box-shadow: 0 6px 16px rgba(0, 0, 0, 0.3); }
#am-fly .msg.doc { background: #fffdf4; color: #22272e; border: 1px solid #c9b98a; border-radius: 3px 10px 3px 3px; font-size: 0.78rem; }

/* --- IDs: a trace ID takes its run's colour; a span ID a colour of its own,
   so a parent's ID matches its child's "parent" chip at a glance */
.tid-c { color: var(--r); font-weight: 700; }
.sid { display: inline-block; font: 600 0.66rem ui-monospace, Menlo, monospace; border-radius: 4px; padding: 0 0.25rem;
  background: hsl(var(--h) 75% 88%); color: hsl(var(--h) 60% 22%); }
.sid.par { background: none; border: 1px dashed hsl(var(--h) 55% 45%); }
.sid.none { --h: 0; background: none; border: 1px dashed var(--border); color: var(--dim); }
@media (prefers-color-scheme: dark) { .sid { background: hsl(var(--h) 45% 28%); color: hsl(var(--h) 80% 88%); } .sid.par { border-color: hsl(var(--h) 60% 65%); } }
.lines .ids { color: var(--dim); }
.lines .ids .tid-c { font-weight: 700; }
.lines .lv.warn { color: var(--busy); font-weight: 700; }
.lines li.on { background: color-mix(in srgb, var(--accent) 14%, transparent); }
.lines li.more { cursor: default; }
.logs-body .flt { margin: 0 0 0.3rem; font: 0.72rem ui-monospace, Menlo, monospace; color: var(--dim); }
.logs-body .flt b { color: var(--text); }
.lines .tid-c { cursor: pointer; }

/* --- the crew's context strip: each run's current span */
.ctx { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 0.3rem 1.2rem; align-items: center; font-size: 0.74rem;
  border-top: 1px dashed var(--border); padding-top: 0.3rem; }
.ctx > b { font-size: 0.66rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--dim); }
.ctx span.c { display: inline-flex; gap: 0.3rem; align-items: center; border-radius: 5px; padding: 0.05rem 0.25rem; }
.ctx code { font-size: 0.72rem; }
.crew-body.has-ctx { grid-template-rows: minmax(0, 1fr) auto; }
.svc.err { border-color: var(--late); }

/* --- Jaeger: the search over the trace's time line */
.jaeger-body { display: flex; flex-direction: column; gap: 0.5rem; padding: 0.4rem 0.6rem; overflow: hidden; }
.j-search { flex: none; display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.25fr); gap: 0.6rem; height: 150px; border-radius: 8px; }
.j-search .form { grid-column: 1 / -1; display: flex; gap: 0.6rem; align-items: center; font-size: 0.74rem; color: var(--dim); }
.j-search .form code { color: var(--text); border: 1px solid var(--border); border-radius: 5px; padding: 0 0.35rem; background: var(--raw); }
.j-search .form code.q { border-color: var(--accent); }
.j-search { grid-template-rows: auto minmax(0, 1fr); }
.scatter { width: 100%; height: 100%; min-height: 0; }
.scatter .grid { stroke: var(--border); }
.scatter .ax { fill: var(--dim); font: 9px ui-monospace, Menlo, monospace; }
.scatter circle { fill: color-mix(in srgb, var(--dim) 45%, transparent); }
.scatter circle.run { fill: var(--r); }
.scatter circle.sel { stroke: var(--text); stroke-width: 2; }
.scatter circle.err { stroke: var(--late); stroke-width: 2.5; }
.scatter circle.faded { opacity: 0.18; }
.tlist { list-style: none; margin: 0; padding: 0; overflow: auto; font-size: 0.74rem; }
.tlist li { display: grid; grid-template-columns: auto minmax(0, 1fr) auto auto; gap: 0.4rem; align-items: baseline;
  padding: 0.12rem 0.3rem; border-radius: 5px; border-bottom: 1px dashed var(--border); }
.tlist li[data-run] { cursor: pointer; }
.tlist li[data-run]:hover { background: var(--raw); }
.tlist li.sel { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.tlist .nm { font-family: ui-monospace, Menlo, monospace; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tlist .n { color: var(--dim); font-size: 0.7rem; white-space: nowrap; }
.tlist .e { color: var(--late); font-weight: 700; font-size: 0.7rem; }
.tlist .d { font: 700 0.72rem ui-monospace, Menlo, monospace; }
.tlist li.dim { color: var(--dim); font-style: italic; display: block; border: none; }
.j-trace { flex: 1; min-height: 0; overflow: auto; border-top: 1px solid var(--border); padding-top: 0.3rem; }
.j-trace > .th { display: flex; gap: 0.5rem; align-items: center; font-size: 0.78rem; margin-bottom: 0.15rem; }
.j-trace > .th code { font-size: 0.72rem; }

/* --- the trace: spans as bars on the run's time line */
.spans li { cursor: pointer; }
.spans li:hover { background: var(--raw); }
.spans .nm .sid { margin-left: 0.3rem; font-style: normal; }
.spans li.err .bar { background: var(--late); }
.spans li.err .nm { color: var(--late); font-weight: 700; }
.spans li.det { display: block; cursor: default; background: var(--raw); border: 1px solid var(--border); margin: 0.1rem 0 0.3rem; padding: 0.3rem 0.5rem; }
.spans li.det:hover { background: var(--raw); }
.det table { border-collapse: collapse; font: 0.7rem ui-monospace, Menlo, monospace; }
.det td { padding: 0.04rem 0.6rem 0.04rem 0; vertical-align: top; overflow-wrap: anywhere; }
.det td:first-child { color: var(--dim); white-space: nowrap; }
.det tr.grp td { padding-top: 0.25rem; font: 700 0.62rem -apple-system, 'Segoe UI', sans-serif; text-transform: uppercase; letter-spacing: 0.05em; color: var(--dim); }
.det tr.bad td:last-child { color: var(--late); font-weight: 700; }

/* --- the gaps: viewer 1's, the one this viewer fills drawn as filled */
.am-gaps { display: grid; gap: 10px; grid-template-columns: repeat(3, minmax(0, 1fr)); }
.gap { min-height: 4.4rem; border: 1px solid var(--late); border-left-width: 4px; border-radius: 10px; padding: 0.35rem 0.6rem;
  display: flex; flex-direction: column; gap: 0.1rem; font-size: 0.8rem; background: color-mix(in srgb, var(--late) 5%, var(--card)); }
.gap b { font-size: 0.86rem; }
.gap .fix { color: var(--accent); font-weight: 600; font-size: 0.78rem; }
.gap.filled { border-color: var(--ok); background: color-mix(in srgb, var(--ok) 7%, var(--card)); }
.gap.filled .fix { color: var(--ok); }
@media (max-width: 1100px) { .am-gaps { grid-template-columns: minmax(0, 1fr); } }
