Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

122 KB
Raw Blame
<script setup lang="ts">
import { ref, reactive, onMounted, onBeforeUnmount, watch, computed, nextTick } from 'vue'
import { MessagePlugin } from 'tdesign-vue-next'
import { useI18n } from 'vue-i18n'
import AssistantActionButton from '@/components/assistant/AssistantActionButton.vue'
import { copyWithToast } from '@/utils/clipboard'
import { getKnowledgeSpans, reparseKnowledge, cancelKnowledgeParse, getKnowledgeDetails } from '@/api/knowledge-base/index'
import {
  groupPostprocessGraphSpans,
  knowledgeSpansPayloadHasTrace,
  postprocessElapsedMs,
  summarizePostprocessTasks,
  type KnowledgeTraceNode,
} from '@/utils/knowledgeTrace'
import { resolveTimelineHeaderStatus, shouldShowProcessingError } from '@/utils/knowledgeProcessingStatus'
import { shownStall, stalledMinutes, STALLED_POLL_INTERVAL_MS } from '@/utils/knowledgeProcessingStall'
import { axisGridStepPct, buildAxisTicks, computeTraceAxis } from '@/utils/traceAxis'
import type { KnowledgeProcessOverrides } from '@/types/knowledgeProcess'

type SpanNode = KnowledgeTraceNode

interface LastError {
  name: string
  error_code: string
  error_message: string
  finished_at?: string
}

interface SpansResponse {
  knowledge_id: string
  attempt: number
  latest_attempt: number
  current_attempt?: number
  parse_status: string
  current_stage?: string
  trace: SpanNode
  last_error?: LastError | null
  // Latest row or span write; only sent while the parse is in flight.
  last_activity_at?: string
  // Server verdict once quiet past the stall hint: 'queued' or 'stalled'.
  stall_state?: string
}

// IMPORTANT: Vue 3 coerces missing Boolean props to `false`, NOT
// `undefined` — so an optional boolean parent omits is indistinguishable
// from one explicitly set to false. That bit us: every parent that
// didn't pass auto-poll silently got polling disabled. Use withDefaults
// to make the intent explicit: polling is ON unless a parent opts out.
const props = withDefaults(
  defineProps<{
    knowledgeId: string
    parseStatus?: string
    autoPoll?: boolean
    compact?: boolean
    // gracePoll selects the polling rule. See shouldPollNow() for
    // the full semantics:
    //   true  — follow parse_status + any running subspan + a
    //           QUIESCE_GRACE_MS grace window after the tree quiesces.
    //           This is what the user-visible drawer mount wants:
    //           late-arriving subspans (wiki has a 30s debounce, etc.)
    //           surface without forcing a manual refresh.
    //   false — strict mode. Poll ONLY while parse_status itself is
    //           non-terminal; ignore post-pipeline async subspans.
    //           This is what background mounts (the hidden badge
    //           driver in doc-content.vue) want, so they stop firing
    //           /spans the instant parsing finishes — async wiki/
    //           summary work doesn't keep a headless poller alive
    //           after the user closed the trace drawer.
    gracePoll?: boolean
    /** Document title shown as the drawer primary heading. */
    docTitle?: string
    /** Show a close control (secondary trace drawer). */
    showClose?: boolean
  }>(),
  {
    autoPoll: true,
    compact: false,
    gracePoll: true,
    docTitle: '',
    showClose: false,
  },
)

const emit = defineEmits<{
  (e: 'update:hasSpans', has: boolean): void
  (e: 'update:summary', summary: { totalMs: number; status: string; stageIndex: number; stageTotal: number; stageLabel: string }): void
  (e: 'close'): void
}>()

const { t } = useI18n()

const STAGES = ['docreader', 'chunking', 'embedding', 'multimodal', 'postprocess'] as const
const POLL_INTERVAL_MS = 2000

const data = ref<SpansResponse | null>(null)
const processOverrides = ref<KnowledgeProcessOverrides | null>(null)
const currentKnowledgeFileType = ref('')
const loading = ref(false)
const refreshing = ref(false)
const selectedAttempt = ref<number | undefined>(undefined)
const expandedRows = ref<Set<string>>(new Set(['__root__']))
const selectedSpanId = ref<string | null>(null)
// Guards the one-shot auto-selection below. Cleared once the panel has
// opened itself, or the moment the user closes it — a 2s poll must never
// reopen a panel the user just dismissed.
const autoSelectPending = ref(true)
const expandedJsonKeys = ref<Set<string>>(new Set())
const nowTick = ref(Date.now())
const detailTab = ref<'overview' | 'input' | 'output' | 'metadata' | 'raw'>('overview')
const lastFetchedAt = ref<number>(0)
const scrollRef = ref<HTMLElement | null>(null)
// Stages the user has explicitly toggled (in either direction). Once
// a stage is in this set, the auto-expand-when-children-appear rule
// stops applying to it — the user's last action wins forever.
//
// Why this beats the old "firstLoadDone once" approach:
//   - Old logic only auto-expanded on the very first fetch. If a stage
//     had no subspans yet at that moment, it'd stay collapsed even
//     after children arrived later, forcing a manual click.
//   - New logic re-evaluates on every fetch: stages with children get
//     expanded unless the user has spoken. Discoverability + respect
//     for manual collapse, both.
// Tracks every row the user has manually expanded or collapsed (by
// row.key). Auto-expand checks this set so deeper subspans that the
// user explicitly hid stay hidden across polls, instead of being
// reopened by the next fetch's re-evaluation pass.
const userToggledRows = ref<Set<string>>(new Set())
// Tracks consecutive fetch failures so the "更新于" caption can surface
// staleness. When the parse_status is mid-flight but every fetch is
// hitting an error, the loop keeps going silently — without this
// indicator the user sees a spinning auto-refresh icon while the
// caption ages without explanation.
const failedAttempts = ref<number>(0)
const lastFetchOk = ref<boolean>(true)

const attemptStatuses = reactive<Map<number, string>>(new Map())

// ===== Polling, take N+1: brutally simple =====
//
// Prior incarnations tried "smart" polling (self-rescheduling chains,
// watchers, watchdogs, fetchInFlight gates routing through tick
// callbacks). They all had edge cases where the loop silently died
// while the LIVE badge kept showing.
//
// New design: ONE setInterval that fires every tick for the entire
// lifetime of the component. The tick callback checks state and
// either calls fetchSpans or no-ops. No re-arming, no clearing, no
// watchers, no chains. The interval cannot get "stranded" because
// nothing ever stops it until unmount.
//
// A 2-second no-op (3 ref reads) is free. The simplicity is the win.
let pollTimer: ReturnType<typeof setInterval> | null = null
let nowTimer: ReturnType<typeof setInterval> | null = null
let unmounted = false
// Drops overlapping fetches. Does NOT gate the polling decision —
// the tick callback owns scheduling. If fetchInFlight is stuck (e.g.
// network black hole), the worst case is one stuck request blocking
// retries until axios timeout (30s); the loop itself stays alive.
let fetchInFlight = false

const stages = computed<SpanNode[]>(() => {
  const trace = data.value?.trace
  const children = trace?.children || []
  const byName = new Map<string, SpanNode>()
  for (const c of children) {
    if (c && c.kind === 'stage' && c.name) {
      byName.set(c.name, c.name === 'postprocess' ? groupPostprocessGraphSpans(c) : c)
    }
  }
  return STAGES.map((n) => byName.get(n) || ({ name: n, kind: 'stage', status: 'pending' } as SpanNode))
})

const currentStageLabel = computed(() => {
  const running = stages.value.find((s) => s.status === 'running')
  const failed = stages.value.find((s) => s.status === 'failed')
  const target = failed || running || stages.value.find((s) => s.status === 'pending') || stages.value[stages.value.length - 1]
  return target ? t(`knowledgeStages.stage.${target.name}`) : ''
})

const currentStageIndex = computed(() => {
  const idx = stages.value.findIndex((s) => s.status === 'running' || s.status === 'failed')
  if (idx >= 0) return idx + 1
  const traversed = stages.value.filter(
    (s) => s.status === 'done' || s.status === 'skipped',
  ).length
  return Math.min(traversed + 1, stages.value.length)
})

function formatDuration(ms?: number): string {
  if (ms === undefined || ms === null || isNaN(ms) || ms < 0) return '—'
  if (ms < 1000) return `${Math.round(ms)}ms`
  if (ms < 60000) return `${(ms / 1000).toFixed(2)}s`
  const mins = Math.floor(ms / 60000)
  const rem = ((ms % 60000) / 1000).toFixed(1)
  return `${mins}m${rem}s`
}

function formatRelativeTime(ts: number): string {
  if (!ts) return '—'
  const sec = Math.max(0, Math.floor((nowTick.value - ts) / 1000))
  if (sec < 1) return t('knowledgeStages.justNow')
  if (sec < 60) return t('knowledgeStages.secondsAgo', { n: sec })
  const min = Math.floor(sec / 60)
  return t('knowledgeStages.minutesAgo', { n: min })
}

// A skipped stage did not execute. Its tiny bookkeeping interval is not
// processing time, so do not present it as (for example) multimodal time.
function formatSpanDuration(node: SpanNode, includeChildren = false): string {
  if (node.status === 'skipped' || node.status === 'pending') return '—'
  if (includeChildren) {
    const elapsed = postprocessElapsedMs(node, postprocessDisplayNow())
    if (elapsed !== null) return formatDuration(elapsed)
  }
  return formatDuration(node.duration_ms)
}

// A terminal parse can leave a stale running child in the last response.
// Once polling stops, freeze its displayed elapsed time at that response
// rather than counting indefinitely without fresh worker evidence.
function postprocessDisplayNow(): number {
  return shouldPollNow() ? nowTick.value : (lastFetchedAt.value || nowTick.value)
}

function isPolling(status?: string): boolean {
  // finalizing is the post-process fan-out window — subspans
  // (summary / question / graph.chunk[*]) are still actively producing
  // events, so we must keep polling and drawing LIVE.
  return status === 'pending' || status === 'processing' || status === 'finalizing'
}

// Hard-terminal statuses override traceActive: even if child spans were
// left in 'running' state (e.g. a cancel raced ahead of a worker's
// FailSpan), the parse pipeline is definitively done for this attempt
// and we should stop polling instead of refreshing forever.
function isHardTerminal(status?: string): boolean {
  return status === 'cancelled' || status === 'failed' || status === 'completed'
}

// True if any node in the trace is still running/pending. Crucial for
// postprocess — its subspans (summary/question/graph.chunk[*]) run
// asynchronously AFTER the main pipeline closes, so parse_status
// flips to 'completed' while there's plenty of work still happening.
// Without checking the tree we'd stop polling at that moment and
// strand the user staring at half-rendered postprocess until they
// manually refresh.
function spanTreeActive(node?: SpanNode): boolean {
  if (!node) return false
  const s = node.status
  if (s === 'running' || s === 'pending') return true
  const kids = node.children || []
  for (let i = 0; i < kids.length; i++) {
    if (spanTreeActive(kids[i])) return true
  }
  return false
}

const traceActive = computed<boolean>(() => spanTreeActive(data.value?.trace))

// Drives BOTH the LIVE badge AND polling. Single source of truth:
//   1. If we have data: still live while parse_status is polling
//      OR the span tree has any running descendant.
//   2. If we don't have data yet (initial paint): trust the parent's
//      parseStatus hint so the UI shows LIVE immediately.
const isLive = computed<boolean>(() => {
  if (data.value) {
    // Hard-terminal parse_status wins over a stale traceActive: cancel
    // and irrecoverable failure can leave child spans stranded as
    // 'running' (worker process died, cancel raced FailSpan, etc.),
    // and we must NOT keep polling forever on those.
    if (isHardTerminal(data.value.parse_status)) return false
    return isPolling(data.value.parse_status) || traceActive.value
  }
  if (isHardTerminal(props.parseStatus)) return false
  return isPolling(props.parseStatus)
})

// Walk every node in the tree and return the freshest updated_at /
// finished_at timestamp we can see. Used by the quiescent grace
// window below to decide "did this trace finish recently, or is it
// just an old completed one we shouldn't waste polls on?"
function spanTreeLastActivity(node?: SpanNode): number {
  if (!node) return 0
  let max = 0
  const stamps: (string | null | undefined)[] = [
    (node as any).updated_at,
    node.finished_at,
    node.started_at,
    (node as any).created_at,
  ]
  for (const s of stamps) {
    const t = parseTime(s || undefined)
    if (t !== null && t > max) max = t
  }
  const kids = node.children || []
  for (let i = 0; i < kids.length; i++) {
    const t = spanTreeLastActivity(kids[i])
    if (t > max) max = t
  }
  return max
}

// Async post-pipeline tasks (summary, question, graph.chunk[*],
// wiki) open their postprocess.* subspans AFTER the parse pipeline
// has finalised — wiki in particular fires 30s after enqueue thanks
// to wikiIngestDelay. At that exact moment parse_status is already
// 'completed' AND every existing span is 'done', so isLive flips to
// false and polling stops. The user is then stranded watching a
// stale tree until they hit refresh.
//
// QUIESCE_GRACE_MS keeps the poll loop alive for a bounded window
// after the trace appears quiescent so late subspans surface
// automatically. Sized generously past wikiIngestDelay (30s) plus a
// typical ingest run, but short enough that opening an
// already-completed knowledge from days ago doesn't waste many
// polls before the loop falls silent.
const QUIESCE_GRACE_MS = 2 * 60 * 1000

const lastTraceActivityAt = computed<number>(() =>
  spanTreeLastActivity(data.value?.trace),
)

const isWithinQuiesceGrace = computed<boolean>(() => {
  if (isLive.value) return false
  const last = lastTraceActivityAt.value
  if (!last) return false
  return nowTick.value - last < QUIESCE_GRACE_MS
})

// shouldPollNow encodes the per-tick "do I fetch?" decision. Split
// from isLive (which also drives the LIVE badge — its semantics are
// "trace is actually running") so the polling rules can diverge from
// the badge rules without entangling them.
//
// Two modes, selected by the gracePoll prop:
//
//   gracePoll = true  (default — for the visible drawer mount):
//     Follow the trace through everything the user might want to see
//     live: the main parse pipeline (parse_status), any running
//     subspan (traceActive), AND the QUIESCE_GRACE_MS window after
//     the tree quiesces (so wiki ingest's 30s-debounced subspan
//     surfaces on its own without a manual refresh).
//
//   gracePoll = false (for background mounts e.g. doc-content's
//     hidden badge driver):
//     Strict mode. Poll ONLY while the parse pipeline itself is
//     non-terminal. Post-pipeline async work (wiki / summary /
//     question / graph subspans) is intentionally ignored —
//     otherwise the hidden mount would keep firing /spans every
//     2s until those finish, even though the user has closed the
//     trace drawer and is only looking at the header badge (whose
//     job is "is parsing done yet?", not "is async post-work done
//     yet?"). The next user-initiated drawer-open will get a fresh
//     fetch via onMounted, picking up any post-pipeline progress.
function shouldPollNow(): boolean {
  if (!data.value) return isPolling(props.parseStatus)
  if (props.gracePoll) {
    return isLive.value || isWithinQuiesceGrace.value
  }
  return isPolling(data.value.parse_status)
}

async function fetchSpans(opts: { manual?: boolean } = {}) {
  if (!props.knowledgeId) return
  if (fetchInFlight) return
  fetchInFlight = true
  if (opts.manual) refreshing.value = true
  if (!data.value) loading.value = true
  let attemptOk = false
  try {
    const res: any = await getKnowledgeSpans(props.knowledgeId, selectedAttempt.value)
    if (res?.success && res.data) {
      data.value = res.data as SpansResponse
      attemptOk = true
      if (selectedAttempt.value === undefined) {
        selectedAttempt.value = data.value.attempt
      }
      // Auto-expand rule, applied on EVERY fetch and walked over
      // EVERY level of the tree (stage + subspan + sub-subspan, …):
      //   - Row has children + user hasn't toggled it → expand
      //   - Row has no children → leave whatever state it was in
      //   - User has toggled the row (either direction) → don't touch
      // Walking the full depth (not just stages) is what makes deep
      // subspans like postprocess.wiki — whose own children like
      // postprocess.wiki.extract / .summary / .classify / .page[*]
      // appear later in the run — surface automatically. Without this
      // the user would click the chevron on postprocess.wiki and see
      // nothing change, because the row would default to collapsed
      // and the auto-expand pass would skip it.
      const expanded = new Set(expandedRows.value)
      expanded.add('__root__')
      const autoExpand = (n: SpanNode) => {
        const key = n.span_id || `stage:${n.name}`
        const kids = n.children || []
        if (kids.length > 0 && !userToggledRows.value.has(key)) {
          expanded.add(key)
        }
        for (const c of kids) autoExpand(c)
      }
      for (const stage of data.value.trace?.children || []) autoExpand(stage)
      expandedRows.value = expanded
      const latestAttempt = data.value.latest_attempt || data.value.attempt || 0
      const tabStatus = resolveTimelineHeaderStatus({
        parseStatus: data.value.parse_status,
        traceStatus: data.value.trace?.status,
        isLatestAttempt: data.value.attempt === latestAttempt,
      }) || 'running'
      attemptStatuses.set(data.value.attempt, tabStatus)
      ensureAttemptStatuses()
      emit('update:hasSpans', knowledgeSpansPayloadHasTrace(data.value))
      // flatRows is derived from data.value; wait for it to recompute
      // before picking a row out of it.
      nextTick(autoSelectSpan)
    } else {
      emit('update:hasSpans', false)
    }
  } catch (e) {
    // Surface the error in the console — silent failures here is
    // exactly what hid the polling-stalled bug from us before.
    console.warn('[KnowledgeTimeline] fetchSpans failed', e)
    emit('update:hasSpans', false)
  } finally {
    // Track every attempt, not just successful ones — otherwise a
    // failing endpoint would leave "更新于 X 秒前" frozen forever while
    // the spinner spins. Pair with failedAttempts to render a "fetch
    // failed" hint when consecutive errors pile up.
    lastFetchedAt.value = Date.now()
    lastFetchOk.value = attemptOk
    if (attemptOk) {
      failedAttempts.value = 0
    } else {
      failedAttempts.value += 1
    }
    loading.value = false
    refreshing.value = false
    fetchInFlight = false
  }
}

function ensureAttemptStatuses() {
  const latest = data.value?.latest_attempt || 0
  if (latest <= 1) return
  for (let n = 1; n <= latest; n++) {
    if (attemptStatuses.has(n)) continue
    getKnowledgeSpans(props.knowledgeId, n)
      .then((res: any) => {
        if (res?.success && res.data?.trace) {
          attemptStatuses.set(n, resolveTimelineHeaderStatus({
            parseStatus: res.data.parse_status,
            traceStatus: res.data.trace?.status,
            isLatestAttempt: n === latest,
          }) || 'running')
        }
      })
      .catch(() => { })
  }
}

function localizedErrorTitle(code?: string): string {
  if (!code) return ''
  const key = `knowledgeStages.errorCode.${code}`
  const localized = t(key)
  return localized === key ? code : localized
}

function localizedErrorSuggestion(code?: string): string {
  if (!code) return ''
  const key = `knowledgeStages.errorCode.${code}_SUGGESTION`
  const localized = t(key)
  if (localized !== key) return localized
  const fallback = t('knowledgeStages.errorCode.UNKNOWN_SUGGESTION')
  return fallback === 'knowledgeStages.errorCode.UNKNOWN_SUGGESTION' ? '' : fallback
}

async function copyValue(value: any) {
  const text = typeof value === 'string' ? value : JSON.stringify(value, null, 2)
  await copyWithToast(text, 'knowledgeStages.copied', 'knowledgeStages.copyDetails')
}

async function copySpan(node: SpanNode) {
  await copyValue(node)
}

async function onRetry() {
  if (!props.knowledgeId) return
  try {
    await reparseKnowledge(props.knowledgeId)
    selectedAttempt.value = undefined
    attemptStatuses.clear()
    selectedSpanId.value = null
    autoSelectPending.value = true
    await fetchSpans()
  } catch {
    // ignore
  }
}

async function onManualRefresh() {
  if (refreshing.value || loading.value) return
  await fetchSpans({ manual: true })
}

const cancelling = ref(false)

// Minutes the latest attempt has gone without progress; 0 unless it looks
// stuck. Historical attempts are never "stalled".
const stalledMin = computed<number>(() => {
  const d = data.value
  if (!d || d.attempt !== (d.latest_attempt || d.attempt)) return 0
  return stalledMinutes(d, nowTick.value)
})

const stalledStageLabel = computed(() => {
  const stage = data.value?.current_stage
  return stage && (STAGES as readonly string[]).includes(stage)
    ? t(`knowledgeStages.stage.${stage}`)
    : ''
})

// Mirrors the backend CancelKnowledgeParse gate (pending / processing /
// finalizing). Uses the freshest status we have: live span data first,
// the parent's hint before the first fetch lands.
const canCancelParse = computed<boolean>(() => {
  const status = data.value?.parse_status ?? props.parseStatus
  return isPolling(status)
})

async function onCancelParseConfirm() {
  if (cancelling.value) return
  const id = props.knowledgeId
  if (!id) return
  cancelling.value = true
  try {
    await cancelKnowledgeParse(id)
    MessagePlugin.success(t('knowledgeBase.cancelParseSubmitted'))
    await fetchSpans({ manual: true })
  } catch (e: any) {
    MessagePlugin.error(e?.message || t('knowledgeBase.cancelParseFailed'))
  } finally {
    cancelling.value = false
  }
}

function onAttemptChange(n: number) {
  if (Number.isNaN(n)) return
  selectedAttempt.value = n
  selectedSpanId.value = null
  autoSelectPending.value = true
  // New attempt: forget per-row user choices so the auto-expand
  // rule re-evaluates cleanly against the new attempt's tree.
  userToggledRows.value = new Set()
  expandedRows.value = new Set(['__root__'])
  fetchSpans()
}

watch(
  () => props.knowledgeId,
  () => {
    selectedAttempt.value = undefined
    data.value = null
    processOverrides.value = null
    currentKnowledgeFileType.value = ''
    expandedRows.value = new Set(['__root__'])
    selectedSpanId.value = null
    autoSelectPending.value = true
    attemptStatuses.clear()
    userToggledRows.value = new Set()
    fetchSpans()
    fetchProcessOverrides()
  },
)

function onKeydown(ev: KeyboardEvent) {
  if (ev.key === 'Escape' && selectedSpanId.value) {
    closeDetail()
  }
}

async function fetchProcessOverrides() {
  if (props.compact || !props.knowledgeId) return
  try {
    const res: any = await getKnowledgeDetails(props.knowledgeId)
    if (res?.success && res.data) {
      processOverrides.value = res.data.metadata?.process_overrides ?? null
      currentKnowledgeFileType.value = normalizeFileType(
        res.data.file_type || getFileTypeFromName(res.data.file_name || res.data.title || ''),
      )
    }
  } catch {
    processOverrides.value = null
    currentKnowledgeFileType.value = ''
  }
}

onMounted(() => {
  fetchSpans()
  fetchProcessOverrides()
  // One permanent interval for the entire component lifetime. The
  // tick decides whether to actually fetch — no clearing, no
  // re-arming, no watchers wired into it. If this interval ever
  // stops firing, the entire JS event loop is wedged (in which case
  // nothing else would work either).
  if (props.autoPoll) {
    pollTimer = setInterval(() => {
      if (unmounted) return
      if (fetchInFlight) return
      if (!shouldPollNow()) return
      // Nothing is moving on a stalled trace; check back less often.
      if (stalledMin.value > 0 && Date.now() - lastFetchedAt.value < STALLED_POLL_INTERVAL_MS) return
      fetchSpans()
    }, POLL_INTERVAL_MS)
  }
  nowTimer = setInterval(() => {
    nowTick.value = Date.now()
  }, 1000)
  window.addEventListener('keydown', onKeydown)
})

onBeforeUnmount(() => {
  unmounted = true
  if (pollTimer) {
    clearInterval(pollTimer)
    pollTimer = null
  }
  if (nowTimer) {
    clearInterval(nowTimer)
    nowTimer = null
  }
  window.removeEventListener('keydown', onKeydown)
})

// ---------- Waterfall helpers ----------

function rowKey(node: SpanNode, fallback: string): string {
  return node.span_id || fallback
}

function parseTime(s?: string | null): number | null {
  if (!s) return null
  const t = Date.parse(s)
  return Number.isNaN(t) ? null : t
}

function nodeStart(node: SpanNode): number | null {
  return parseTime(node.started_at || undefined)
}

function nodeEnd(node: SpanNode): number | null {
  const e = parseTime(node.finished_at || undefined)
  if (e !== null) return e
  const s = nodeStart(node)
  if (s !== null && typeof node.duration_ms === 'number' && node.duration_ms > 0) {
    return s + node.duration_ms
  }
  return null
}

function collectStarts(node: SpanNode | undefined, out: number[]) {
  if (!node) return
  const s = nodeStart(node)
  if (s !== null) out.push(s)
  for (const c of node.children || []) collectStarts(c, out)
}

function collectEnds(node: SpanNode | undefined, out: number[]) {
  if (!node) return
  const e = nodeEnd(node)
  if (e !== null) out.push(e)
  for (const c of node.children || []) collectEnds(c, out)
}

const traceRoot = computed<SpanNode | null>(() => {
  const trace = data.value?.trace
  if (!trace) return null
  const synthChildren: SpanNode[] = stages.value.map((stage) => stage)
  return {
    ...trace,
    name: trace.name || 'knowledge_processing',
    kind: trace.kind || 'root',
    children: synthChildren,
  }
})

const t0 = computed<number | null>(() => {
  const root = traceRoot.value
  if (!root) return null
  const direct = nodeStart(root)
  if (direct !== null) return direct
  const all: number[] = []
  collectStarts(root, all)
  if (all.length === 0) return null
  return Math.min(...all)
})

const tEnd = computed<number | null>(() => {
  const root = traceRoot.value
  if (!root) return null
  const direct = parseTime(root.finished_at || undefined)
  const all: number[] = []
  collectEnds(root, all)
  let candidate: number | null = direct
  if (all.length > 0) {
    const max = Math.max(...all)
    candidate = candidate === null ? max : Math.max(candidate, max)
  }
  // Extend the right edge to "now" whenever the trace is still
  // actively producing spans — this covers both parse_status mid-flight
  // AND the postprocess-async case where the top-level status closes
  // but subspans keep ticking. Both conditions are captured by isLive.
  if (isLive.value) {
    const now = nowTick.value
    candidate = candidate === null ? now : Math.max(candidate, now)
  }
  return candidate
})

const totalMs = computed<number>(() => {
  if (t0.value === null || tEnd.value === null) return 0
  // The trace's own duration_ms only covers the parsing pipeline up to
  // FinalizeAttempt. Async post-processing subspans (summary / question /
  // graph) keep producing rows AFTER the root closes — so the time axis
  // must scale to the latest descendant end, otherwise their bars get
  // clipped past the right edge. Take the max of (root duration, observed
  // span tail) regardless of polling state.
  const observed = Math.max(0, tEnd.value - t0.value)
  const traceDur = data.value?.trace?.duration_ms
  if (typeof traceDur === 'number' && traceDur > 0) {
    return Math.max(traceDur, observed)
  }
  return observed
})

const showRuler = computed(() => totalMs.value >= 50)

// ---------- Time axis ----------
// See utils/traceAxis: the ruler is a rounded 1/2/2.5/5 axis, and every
// bar is positioned against axis.maxMs rather than the raw total so the
// bars and the ruler above them cannot disagree.
const timeAxis = computed(() => computeTraceAxis(totalMs.value))
const axisMaxMs = computed(() => timeAxis.value.maxMs)
const gridStepPct = computed(() => axisGridStepPct(timeAxis.value))
const rulerTicks = computed(() => (showRuler.value ? buildAxisTicks(timeAxis.value) : []))

// "Now" position on the waterfall scale, used to draw the live cursor
// while polling so the user can see time advancing even when the running
// stage's bar grows slowly toward the right edge.
const nowMarkerPct = computed<number | null>(() => {
  if (!isLive.value || !t0.value || !axisMaxMs.value) return null
  const pct = ((nowTick.value - t0.value) / axisMaxMs.value) * 100
  return Math.max(0, Math.min(100, pct))
})

interface FlatRow {
  key: string
  depth: number
  node: SpanNode
  hasChildren: boolean
  isRoot: boolean
  isStage: boolean
  parentKey?: string
}

const flatRows = computed<FlatRow[]>(() => {
  const root = traceRoot.value
  if (!root) return []
  const rows: FlatRow[] = []

  const rootKey = rowKey(root, '__root__')
  rows.push({
    key: rootKey,
    depth: 0,
    node: root,
    hasChildren: (root.children || []).length > 0,
    isRoot: true,
    isStage: false,
  })

  for (const stage of root.children || []) {
    const stageKey = rowKey(stage, `stage:${stage.name}`)
    const stageChildren = stage.children || []
    rows.push({
      key: stageKey,
      depth: 1,
      node: stage,
      hasChildren: stageChildren.length > 0,
      isRoot: false,
      isStage: true,
      parentKey: rootKey,
    })
    if (!expandedRows.value.has(stageKey)) continue

    const walk = (n: SpanNode, depth: number, idxPath: string, parentKey: string) => {
      const key = rowKey(n, `${idxPath}:${n.name}`)
      const kids = n.children || []
      rows.push({
        key,
        depth,
        node: n,
        hasChildren: kids.length > 0,
        isRoot: false,
        isStage: false,
        parentKey,
      })
      // Honour the expand/collapse state for subspans too — without
      // this gate, clicking the chevron on a subspan (e.g.
      // postprocess.wiki) only rotated the icon but the children
      // were always rendered, so the toggle looked broken.
      if (!expandedRows.value.has(key)) return
      kids.forEach((c, i) => walk(c, depth + 1, `${idxPath}/${i}`, key))
    }
    stageChildren.forEach((c, i) => walk(c, 2, `${stageKey}/${i}`, stageKey))
  }

  return rows
})

const selectedRow = computed<FlatRow | null>(() => {
  const id = selectedSpanId.value
  if (!id) return null
  return flatRows.value.find((r) => r.key === id) || null
})

const detailOpen = computed(() => selectedSpanId.value !== null && selectedRow.value !== null)

function barStyle(node: SpanNode): Record<string, string> {
  const total = axisMaxMs.value
  if (!total || t0.value === null) return { display: 'none' }
  const start = nodeStart(node)
  if (start === null) return { display: 'none' }
  // For a span with no recorded finished_at, use "now" as the end
  // whenever it's plausibly still running — either the trace overall
  // is live, or this individual span's status says it's in flight.
  // Without the second clause, postprocess subspans that survived past
  // parse_status='completed' would collapse to zero width.
  const liveBar = isLive.value || node.status === 'running' || node.status === 'pending'
  const end = nodeEnd(node) ?? (liveBar ? nowTick.value : start)
  const leftPct = ((start - t0.value) / total) * 100
  const widthPct = Math.max(0.4, ((end - start) / total) * 100)
  return {
    left: `${Math.max(0, Math.min(100, leftPct))}%`,
    width: `${Math.min(100 - Math.max(0, leftPct), widthPct)}%`,
  }
}

// Wrapping outline bar — when a span's children extend past the parent's
// own finished_at (typical for postprocess: stage closes in ~9ms but its
// async summary/question subspans run for tens of seconds), we render a
// faint outline from the parent's start to the latest descendant end.
// This makes "this stage's downstream work took N seconds total" visible
// at a glance without conflating it with the stage's self-duration.
function descendantMaxEnd(node: SpanNode): number | null {
  const ends: number[] = []
  for (const c of node.children || []) {
    collectEnds(c, ends)
  }
  if (ends.length === 0) return null
  return Math.max(...ends)
}

function wrapStyle(node: SpanNode): Record<string, string> | null {
  const total = axisMaxMs.value
  if (!total || t0.value === null) return null
  const start = nodeStart(node)
  if (start === null) return null
  const selfEnd = nodeEnd(node) ?? start
  const childEnd = descendantMaxEnd(node)
  if (childEnd === null) return null
  // Only render the wrapping bar when descendants extend at least 50ms
  // past the parent — otherwise the outline is indistinguishable from
  // the solid self-bar and only adds visual noise.
  if (childEnd - selfEnd < 50) return null
  const leftPct = ((start - t0.value) / total) * 100
  const widthPct = Math.max(0.4, ((childEnd - start) / total) * 100)
  return {
    left: `${Math.max(0, Math.min(100, leftPct))}%`,
    width: `${Math.min(100 - Math.max(0, leftPct), widthPct)}%`,
  }
}

function wrapDurationMs(node: SpanNode): number {
  const start = nodeStart(node)
  const childEnd = descendantMaxEnd(node)
  if (start === null || childEnd === null) return 0
  return Math.max(0, childEnd - start)
}

function barOffsetPct(node: SpanNode): number | null {
  const total = axisMaxMs.value
  if (!total || t0.value === null) return null
  const start = nodeStart(node)
  if (start === null) return null
  return Math.max(0, Math.min(100, ((start - t0.value) / total) * 100))
}

function barOffsetMs(node: SpanNode): number {
  const start = nodeStart(node)
  if (start === null || t0.value === null) return 0
  return Math.max(0, start - t0.value)
}

function liveElapsedMs(node: SpanNode): number {
  const s = nodeStart(node)
  if (s === null) return 0
  return Math.max(0, nowTick.value - s)
}

function isPlaceholder(node: SpanNode): boolean {
  return !node.span_id && !node.started_at
}

// The trace has reached a terminal state and nothing is still ticking.
const traceTerminal = computed<boolean>(
  () => isHardTerminal(data.value?.parse_status) && !traceActive.value,
)

// A synthesized stage still marked 'pending' on a finished trace never
// ran and never will — docreader failed, so chunking/embedding/... were
// abandoned. v1 rendered them as ordinary pending rows with a dashed
// stub pinned to the right edge of the track, which reads as leftover
// rendering junk. Say "not run" in words and let the row recede.
function isNotRun(node: SpanNode): boolean {
  return traceTerminal.value && node.status === 'pending' && isPlaceholder(node)
}

function isRowExpanded(key: string): boolean {
  return expandedRows.value.has(key)
}

function treeToggleAriaLabel(row: FlatRow): string {
  if (!row.hasChildren || row.isRoot) return ''
  return isRowExpanded(row.key)
    ? t('knowledgeStages.collapseBranch')
    : t('knowledgeStages.expandBranch')
}

function scrollRowIntoView(key: string) {
  nextTick(() => {
    const root = scrollRef.value
    if (!root) return
    const safeKey = typeof CSS !== 'undefined' && CSS.escape ? CSS.escape(key) : key.replace(/"/g, '\\"')
    const el = root.querySelector(`.kp-row[data-span-key="${safeKey}"]`)
    el?.scrollIntoView({ block: 'nearest', behavior: 'smooth' })
  })
}

function toggleTree(row: FlatRow, ev?: MouseEvent) {
  if (ev) ev.stopPropagation()
  if (!row.hasChildren) return
  const wasExpanded = expandedRows.value.has(row.key)
  const next = new Set(expandedRows.value)
  if (next.has(row.key)) next.delete(row.key)
  else next.add(row.key)
  expandedRows.value = next
  // Record the manual toggle at every depth so the next poll's
  // auto-expand pass leaves this row alone. Without this, a user
  // who collapsed a noisy subspan (e.g. postprocess.wiki with a
  // dozen page[*] children) would see it pop back open on the
  // next 2s tick.
  const touched = new Set(userToggledRows.value)
  touched.add(row.key)
  userToggledRows.value = touched
  // Expanding via chevron should also surface the detail panel for
  // this stage/span — otherwise the name column only toggles the tree.
  if (!wasExpanded && next.has(row.key)) {
    selectRow(row)
  }
}

function selectRow(row: FlatRow) {
  if (selectedSpanId.value === row.key) {
    return
  }
  selectedSpanId.value = row.key
  detailTab.value = 'overview'
  scrollRowIntoView(row.key)
}

function closeDetail() {
  selectedSpanId.value = null
  autoSelectPending.value = false
}

// v1 opened with nothing selected, so a short trace showed a handful of
// rows above half a panel of white space, and nothing suggested the rows
// were clickable at all. Open on the span the reader came for: the one
// that failed, or the root (whose detail is the stage breakdown).
function autoSelectSpan() {
  if (props.compact) return
  if (!autoSelectPending.value || selectedSpanId.value) return
  const rows = flatRows.value
  if (rows.length === 0) return
  const failed = rows.find((r) => !r.isRoot && r.node.status === 'failed')
  selectedSpanId.value = (failed || rows[0]).key
  detailTab.value = 'overview'
  autoSelectPending.value = false
}

function isObjectWithKeys(v: any): boolean {
  return v && typeof v === 'object' && !Array.isArray(v) && Object.keys(v).length > 0
}

function hasContent(v: any): boolean {
  if (v === null || v === undefined || v === '') return false
  if (Array.isArray(v)) return v.length > 0
  if (typeof v === 'object') return Object.keys(v).length > 0
  return true
}

function prettyJSON(v: any): string {
  try {
    return JSON.stringify(v, null, 2)
  } catch {
    return String(v)
  }
}

function localizedStatus(status: string): string {
  const key = `knowledgeStages.status.${status}`
  const localized = t(key)
  return localized === key ? status : localized
}

function rowLabel(row: FlatRow): string {
  if (row.isRoot) return t('knowledgeStages.root')
  if (row.isStage) return t(`knowledgeStages.stage.${row.node.name}`)
  if (row.node.name === 'postprocess.graph') return t('knowledgeStages.processConfig.graph')
  const graphChunk = /^postprocess\.graph\.chunk\[(\d+)\]$/.exec(row.node.name)
  if (graphChunk) return `${t('knowledgeStages.processConfig.graph')} #${Number(graphChunk[1]) + 1}`
  return row.node.name
}

// v1 stamped ROOT / STAGE / SPAN on every row and flung the label to the
// far right of the name column with margin-left:auto, which opened a
// ~200px gap mid-row and told the reader nothing that indentation hadn't
// already said. Only `generation` (an LLM/VLM call) is worth a chip.
function rowKindLabel(row: FlatRow): string {
  if (row.isRoot || row.isStage) return ''
  return row.node.kind === 'generation' ? 'generation' : ''
}

function jsonExpandKey(section: string, key: string): string {
  return `${selectedSpanId.value || ''}::${section}::${key}`
}

function toggleJsonKey(section: string, key: string) {
  const k = jsonExpandKey(section, key)
  const next = new Set(expandedJsonKeys.value)
  if (next.has(k)) next.delete(k)
  else next.add(k)
  expandedJsonKeys.value = next
}

function isJsonExpanded(section: string, key: string): boolean {
  return expandedJsonKeys.value.has(jsonExpandKey(section, key))
}

function formatTime(s?: string | null): string {
  if (!s) return '—'
  const ts = Date.parse(s)
  if (Number.isNaN(ts)) return s
  const d = new Date(ts)
  const ms = String(d.getMilliseconds()).padStart(3, '0')
  const hh = String(d.getHours()).padStart(2, '0')
  const mm = String(d.getMinutes()).padStart(2, '0')
  const ss = String(d.getSeconds()).padStart(2, '0')
  // Date prefix: omit year when same as current year to keep the row
  // compact, but always show month+day so traces from yesterday/last
  // week aren't ambiguous. The full ISO date is preserved in the
  // tooltip via the original string.
  const now = new Date()
  const yyyy = d.getFullYear()
  const mo = String(d.getMonth() + 1).padStart(2, '0')
  const dd = String(d.getDate()).padStart(2, '0')
  const datePart = yyyy === now.getFullYear() ? `${mo}-${dd}` : `${yyyy}-${mo}-${dd}`
  return `${datePart} ${hh}:${mm}:${ss}.${ms}`
}

function humanizeKey(k: string): string {
  return k
    .replace(/[_-]+/g, ' ')
    .replace(/\s+/g, ' ')
    .trim()
    .replace(/\b([a-z])/g, (_, c: string) => c.toUpperCase())
}

interface KvEntry {
  key: string
  label: string
  kind: 'scalar' | 'bool' | 'array' | 'object'
  display: string
  raw: any
  // True for short payloads — the panel skips the click-to-expand
  // affordance and renders the JSON inline directly so users see the
  // values without an extra click. Long payloads stay folded so the
  // panel doesn't blow up vertically when an output has hundreds of
  // entries.
  defaultExpanded: boolean
}

function buildKvEntries(obj: any): KvEntry[] {
  if (!isObjectWithKeys(obj)) return []
  const entries: KvEntry[] = []
  for (const [key, value] of Object.entries(obj)) {
    entries.push(toKvEntry(key, value))
  }
  return entries
}

// Threshold for inline auto-expansion. Short payloads — small arrays /
// shallow objects — render inline directly. Anything larger keeps the
// click-to-expand summary so the detail panel doesn't grow without bound.
const KV_INLINE_ARRAY_LIMIT = 8
const KV_INLINE_OBJECT_KEY_LIMIT = 8
const KV_INLINE_JSON_BYTES_LIMIT = 600

function shouldInlineExpand(value: any): boolean {
  if (Array.isArray(value)) {
    if (value.length > KV_INLINE_ARRAY_LIMIT) return false
    try {
      return JSON.stringify(value).length <= KV_INLINE_JSON_BYTES_LIMIT
    } catch {
      return false
    }
  }
  if (value && typeof value === 'object') {
    const keys = Object.keys(value)
    if (keys.length > KV_INLINE_OBJECT_KEY_LIMIT) return false
    try {
      return JSON.stringify(value).length <= KV_INLINE_JSON_BYTES_LIMIT
    } catch {
      return false
    }
  }
  return false
}

function toKvEntry(key: string, value: any): KvEntry {
  const label = humanizeKey(key)
  if (value === null || value === undefined) {
    return { key, label, kind: 'scalar', display: '—', raw: value, defaultExpanded: false }
  }
  if (typeof value === 'boolean') {
    return { key, label, kind: 'bool', display: value ? 'true' : 'false', raw: value, defaultExpanded: false }
  }
  if (typeof value === 'number') {
    return { key, label, kind: 'scalar', display: value.toLocaleString(), raw: value, defaultExpanded: false }
  }
  if (typeof value === 'string') {
    return { key, label, kind: 'scalar', display: value, raw: value, defaultExpanded: false }
  }
  if (Array.isArray(value)) {
    return {
      key, label, kind: 'array',
      display: `Array · ${value.length}`, raw: value,
      defaultExpanded: shouldInlineExpand(value),
    }
  }
  if (typeof value === 'object') {
    const n = Object.keys(value as object).length
    return {
      key, label, kind: 'object',
      display: `Object · ${n} keys`, raw: value,
      defaultExpanded: shouldInlineExpand(value),
    }
  }
  return { key, label, kind: 'scalar', display: String(value), raw: value, defaultExpanded: false }
}

interface AttemptTab {
  n: number
  status: string
  active: boolean
}

const attemptTabs = computed<AttemptTab[]>(() => {
  const latest = data.value?.latest_attempt || 0
  if (latest <= 1) return []
  const active = selectedAttempt.value ?? data.value?.attempt ?? latest
  const out: AttemptTab[] = []
  for (let n = 1; n <= latest; n++) {
    out.push({
      n,
      status: attemptStatuses.get(n) || 'unknown',
      active: n === active,
    })
  }
  return out
})

function attemptGlyph(status: string): { ch: string; cls: string } {
  switch (status) {
    case 'done':
      return { ch: '✓', cls: 'kp-glyph-done' }
    case 'failed':
      return { ch: '✗', cls: 'kp-glyph-failed' }
    case 'running':
    case 'pending':
    case 'processing':
      return { ch: '●', cls: 'kp-glyph-running' }
    default:
      return { ch: '–', cls: 'kp-glyph-unknown' }
  }
}

// True when the panel is showing the most recent attempt (or there's
// only one). Historical attempts must keep their own per-attempt
// trace.status; only the latest attempt's header should defer to the
// knowledge-level parse_status.
const viewingLatestAttempt = computed<boolean>(() => {
  const latest = data.value?.latest_attempt || 0
  if (latest <= 1) return true
  const active = selectedAttempt.value ?? data.value?.attempt ?? latest
  return active === latest
})

// The authoritative status for the header badge. During the async
// post-pipeline window (summary / question / graph / wiki), the latest
// attempt's ROOT span closes — so trace.status reads 'done' — while
// those subspans can still be running or can later make the knowledge fail.
// The latest knowledge row is therefore authoritative for ALL statuses,
// including terminal ones. Historical attempts keep their own root status.
const headerStatus = computed(() => {
  return resolveTimelineHeaderStatus({
    parseStatus: data.value?.parse_status,
    traceStatus: data.value?.trace?.status,
    isLatestAttempt: viewingLatestAttempt.value,
  })
})

const headerStatusText = computed(() => {
  const s = headerStatus.value
  return s ? localizedStatus(s) : ''
})

const headerStatusTheme = computed(() => {
  switch (headerStatus.value) {
    case 'done':
    case 'completed':
      return 'success'
    case 'failed':
      return 'danger'
    case 'running':
    case 'processing':
    case 'pending':
    case 'finalizing':
      return 'warning'
    default:
      return 'default'
  }
})

const showLastError = computed(() =>
  shouldShowProcessingError(Boolean(data.value?.last_error), data.value?.parse_status, data.value?.trace?.status),
)

const failedStage = computed<SpanNode | null>(
  () => stages.value.find((s) => s.status === 'failed') || null,
)

// v1's headline was localizedErrorTitle(code), which falls back to the
// raw code when no translation exists — so the card printed
// DOCREADER_PARSE_FAILED as its title AND again as the chip beside it,
// while the one line that actually explains the failure
// ("Unsupported file type: htm") sat at the bottom in 11px grey mono.
// The headline now says WHERE it broke, the code appears exactly once,
// and the message is promoted to the card's primary line.
const errorHeadline = computed(() => {
  const err = data.value?.last_error
  const localized = localizedErrorTitle(err?.error_code)
  if (localized && localized !== err?.error_code) return localized
  // Prefer a stage row that actually carries the failure; fall back to
  // the span name the backend recorded on last_error, which covers a
  // failure raised before any stage span opened.
  const name = failedStage.value?.name
    || (STAGES as readonly string[]).find((n) => n === err?.name)
  if (name) {
    return t('knowledgeStages.stageFailed', { stage: t(`knowledgeStages.stage.${name}`) })
  }
  return t('knowledgeStages.status.failed')
})

// Retry stays reachable from the toolbar when a failure has no
// last_error payload to hang the in-card button off.
const showToolbarRetry = computed(
  () => data.value?.parse_status === 'failed' && !showLastError.value,
)

const stagesStatDisplay = computed(() => {
  const total = stages.value.length
  const completedCount = stages.value.filter(
    (s) => s.status === 'done' || s.status === 'skipped',
  ).length
  const inProgress = stages.value.some(
    (s) => s.status === 'running' || s.status === 'failed' || s.status === 'pending',
  )
  if (inProgress) {
    return {
      label: t('knowledgeStages.head.stagesProgress'),
      value: `${currentStageIndex.value}/${total}`,
    }
  }
  return {
    label: t('knowledgeStages.head.stagesDone'),
    value: `${completedCount}/${total}`,
  }
})

const postprocessTaskStats = computed(() =>
  summarizePostprocessTasks(data.value?.trace),
)

interface HeadStat {
  key: string
  label: string
  value: string
  mono: boolean
  note?: boolean
}

// v1 glued five unrelated facts into one dot-separated grey sentence —
// "处理流水线 · 总耗时 494ms · 当前阶段 1/5 · 第 1 次尝试" — which reads
// as a run-on and buries the numbers among the words. Label/value pairs
// with the numbers in tabular mono scan in one pass instead. The panel's
// own name moves out of the sentence and onto the overline above the
// title, where it belongs.
const headStats = computed<HeadStat[]>(() => {
  if (!data.value) return []
  const out: HeadStat[] = []
  if (totalMs.value > 0) {
    out.push({
      key: 'duration',
      label: t('knowledgeStages.stat.duration'),
      value: formatDuration(totalMs.value),
      mono: true,
    })
  }
  const st = stagesStatDisplay.value
  out.push({ key: 'stages', label: st.label, value: st.value, mono: true })

  const postprocess = postprocessTaskStats.value
  if (postprocess.total > 0) {
    out.push({
      key: 'tasks',
      label: t('knowledgeStages.stat.tasks'),
      value: t('knowledgeStages.stat.tasksValue', {
        running: postprocess.running,
        failed: postprocess.failed,
        completed: postprocess.completed,
      }),
      mono: false,
      note: postprocess.running > 0 || postprocess.failed > 0,
    })
  }
  if (attemptTabs.value.length === 0 && data.value.current_attempt) {
    out.push({
      key: 'attempt',
      label: t('knowledgeStages.stat.attempt'),
      value: `#${data.value.current_attempt}`,
      mono: true,
    })
  }
  const lastActivity = parseTime(data.value.last_activity_at)
  if (lastActivity !== null && isLive.value) {
    out.push({
      key: 'progress',
      label: t('knowledgeStages.head.lastProgress'),
      value: formatRelativeTime(lastActivity),
      mono: false,
      note: stalledMin.value > 0,
    })
  }
  if (lastFetchedAt.value && isLive.value) {
    let updated = formatRelativeTime(lastFetchedAt.value)
    if (!lastFetchOk.value) {
      updated += ` (${t('knowledgeStages.fetchFailedShort')})`
    }
    out.push({
      key: 'updated',
      label: t('knowledgeStages.head.updated'),
      value: updated,
      mono: false,
      note: !lastFetchOk.value,
    })
  }
  return out
})

const primaryHeadTitle = computed(() => props.docTitle || t('knowledgeStages.title'))

// Emit summary upstream so the doc-content drawer can show a one-line
// status pill without mounting a second copy of the tree.
watch(
  [
    () => totalMs.value,
    () => headerStatus.value,
    () => currentStageIndex.value,
    () => currentStageLabel.value,
    () => stages.value.length,
  ],
  () => {
    emit('update:summary', {
      totalMs: totalMs.value,
      status: headerStatus.value,
      stageIndex: currentStageIndex.value,
      stageTotal: stages.value.length,
      stageLabel: currentStageLabel.value,
    })
  },
  { immediate: true },
)

function tabHasContent(tab: 'input' | 'output' | 'metadata'): boolean {
  const node = selectedRow.value?.node
  if (!node) return false
  return hasContent((node as any)[tab])
}

const traceMetadata = computed(() => {
  const m = data.value?.trace?.metadata
  return hasContent(m) ? m : null
})

watch([selectedSpanId, detailTab], () => {
  if (detailTab.value === 'metadata' && !tabHasContent('metadata')) {
    detailTab.value = 'overview'
  }
})

// Identity / lineage info for the Overview tab. Surfaces fields that
// were previously buried in the raw payload so the panel doesn't feel
// thin even when the span has no input/output/metadata.
interface IdentityField {
  key: string
  label: string
  value: string
  mono: boolean
  copyable: boolean
}

function identityFields(row: FlatRow): IdentityField[] {
  const out: IdentityField[] = []
  const node = row.node as any
  out.push({ key: 'name', label: t('knowledgeStages.detail.name'), value: rowLabel(row), mono: false, copyable: false })
  out.push({ key: 'kind', label: t('knowledgeStages.detail.kind'), value: rowKindLabel(row), mono: true, copyable: false })
  out.push({ key: 'status', label: t('knowledgeStages.detail.status'), value: localizedStatus(row.node.status), mono: false, copyable: false })
  if (row.isStage) {
    const idx = stages.value.findIndex((s) => s.name === row.node.name)
    if (idx >= 0) {
      out.push({ key: 'stageIndex', label: t('knowledgeStages.detail.stageOrder'), value: `${idx + 1} / ${stages.value.length}`, mono: true, copyable: false })
    }
  }
  if (row.hasChildren) {
    out.push({ key: 'children', label: t('knowledgeStages.detail.childCount'), value: String((row.node.children || []).length), mono: true, copyable: false })
  }
  if (node.span_id) out.push({ key: 'span_id', label: 'span_id', value: node.span_id, mono: true, copyable: true })
  if (node.parent_span_id) out.push({ key: 'parent_span_id', label: 'parent_span_id', value: node.parent_span_id, mono: true, copyable: true })
  if (data.value?.knowledge_id) out.push({ key: 'knowledge_id', label: 'knowledge_id', value: data.value.knowledge_id, mono: true, copyable: true })
  if (data.value?.current_attempt) out.push({ key: 'attempt', label: t('knowledgeStages.head.attempt'), value: `#${data.value.current_attempt}`, mono: true, copyable: false })
  return out
}

// Quick stage-by-stage breakdown shown inside root's overview.
interface StageRowSummary {
  name: string
  label: string
  status: string
  duration_ms?: number
  pct: number
}

const stageBreakdown = computed<StageRowSummary[]>(() => {
  const total = totalMs.value || 1
  return stages.value.map((s) => ({
    name: s.name,
    label: t(`knowledgeStages.stage.${s.name}`),
    status: s.status,
    duration_ms: s.duration_ms,
    pct: typeof s.duration_ms === 'number' && s.duration_ms > 0 ? Math.min(100, (s.duration_ms / total) * 100) : 0,
  }))
})

function normalizeFileType(value: string): string {
  return String(value || '').trim().replace(/^\./, '').toLowerCase()
}

function getFileTypeFromName(name: string): string {
  const clean = String(name || '').split(/[?#]/)[0]
  const dot = clean.lastIndexOf('.')
  return dot >= 0 ? clean.slice(dot + 1) : ''
}

function formatParserRulesForCurrentFile(
  rules: Array<{ file_types?: string[]; engine?: string }>,
): string {
  const currentType = currentKnowledgeFileType.value
  if (currentType) {
    const matched = rules.find((rule) =>
      (rule.file_types || []).some((ft) => normalizeFileType(ft) === currentType),
    )
    if (matched?.engine) {
      return `${currentType}→${matched.engine}`
    }
  }
  return rules.map(r => `${(r.file_types || []).join('/')}→${r.engine}`).join(', ')
}

// Human-readable summary of the per-upload parse overrides stored in
// knowledge.metadata.process_overrides. Empty overrides → KB defaults.
const processConfigLines = computed<string[]>(() => {
  const o = processOverrides.value
  if (!o) return [t('knowledgeStages.processConfig.kbDefault')]
  const k = (s: string) => `knowledgeStages.processConfig.${s}`
  const onOff = (v: boolean) => (v ? t(k('on')) : t(k('off')))
  const lines: string[] = []

  const cc = o.chunking_config
  if (cc) {
    const parts: string[] = []
    if (cc.chunk_size != null) parts.push(t(k('chunkSize'), { n: cc.chunk_size }))
    if (cc.enable_parent_child != null) {
      parts.push(cc.enable_parent_child ? t(k('parentChildOn')) : t(k('parentChildOff')))
    }
    if (parts.length) lines.push(`${t(k('chunking'))}: ${parts.join(' · ')}`)
  }

  const rules = o.parser_engine_rules || cc?.parser_engine_rules
  if (rules?.length) {
    lines.push(`${t(k('parser'))}: ${formatParserRulesForCurrentFile(rules)}`)
  }

  const mm = o.vlm_config?.enabled ?? o.enable_multimodel
  if (mm != null) lines.push(`${t(k('multimodal'))}: ${onOff(mm)}`)

  if (o.asr_config?.enabled != null) lines.push(`${t(k('asr'))}: ${onOff(o.asr_config.enabled)}`)

  const qg = o.question_generation_config
  if (o.summary_enabled != null) {
    lines.push(`${t('uploadConfirm.documentSummary')}: ${onOff(o.summary_enabled)}`)
  }
  if (qg?.enabled != null) {
    lines.push(`${t(k('question'))}: ${qg.enabled ? t(k('questionOn'), { n: qg.question_count ?? 3 }) : t(k('off'))}`)
  }

  const graph = o.graph_enabled ?? o.extract_config?.enabled
  if (graph != null) lines.push(`${t(k('graph'))}: ${onOff(graph)}`)

  return lines.length ? lines : [t('knowledgeStages.processConfig.kbDefault')]
})
</script>

<template>
  <div class="kp-timeline" :class="{ 'kp-compact': compact }">
    <!-- =========================================================
         COMPACT MODE — used by the card hover popover. Untouched.
         ========================================================= -->
    <template v-if="compact">
      <div class="kp-compact-row">
        <span v-for="s in stages" :key="s.name" class="kp-dot" :class="['kp-dot-' + s.status]"
          :title="t(`knowledgeStages.stage.${s.name}`) + ' · ' + t(`knowledgeStages.status.${s.status}`)" />
      </div>
      <div class="kp-compact-caption">
        <template v-if="totalMs > 0">
          {{ t('knowledgeStages.totalDuration', { d: formatDuration(totalMs) }) }}
        </template>
        <template v-else>
          <span>{{ t('knowledgeStages.title') }}:</span>
          <span class="kp-stage-emph">{{ currentStageIndex }}/{{ stages.length }}</span>
          <span> · {{ currentStageLabel }}</span>
        </template>
      </div>
    </template>

    <!-- =========================================================
         FULL MODE — Langfuse-style waterfall, lives inside the
         secondary drawer. Bottom-docked detail panel.
         ========================================================= -->
    <template v-else>
      <div class="kp-shell">
        <!-- ============== HEADER ============== -->
        <div class="kp-head">
          <div class="kp-head-toolbar">
            <div class="kp-head-titles">
              <div class="kp-head-overline">{{ t('knowledgeStages.title') }}</div>
              <div class="kp-head-title-row">
                <h2 class="kp-head-doc-title" :title="primaryHeadTitle">{{ primaryHeadTitle }}</h2>
                <t-tag v-if="data && headerStatusText" size="small" :theme="headerStatusTheme" variant="light"
                  class="kp-head-status-tag">
                  {{ headerStatusText }}
                </t-tag>
                <span v-if="isLive" class="kp-live-badge" :title="t('knowledgeStages.liveTooltip')">
                  <span class="kp-live-dot" />
                  <span class="kp-live-text">{{ t('knowledgeStages.live') }}</span>
                </span>
              </div>
            </div>
            <div class="kp-head-actions">
              <t-popup trigger="hover" placement="bottom-right" :overlay-style="{ maxWidth: '340px' }">
                <button type="button" class="kp-icon-btn"
                  :title="t('knowledgeStages.processConfig.title')"
                  :aria-label="t('knowledgeStages.processConfig.title')">
                  <t-icon name="info-circle" size="16px" />
                </button>
                <template #content>
                  <div class="kp-proccfg-pop">
                    <div class="kp-proccfg-title">{{ t('knowledgeStages.processConfig.title') }}</div>
                    <div v-for="(line, i) in processConfigLines" :key="i" class="kp-proccfg-line">{{ line }}</div>
                  </div>
                </template>
              </t-popup>
              <button type="button" class="kp-icon-btn" :class="{
                'kp-icon-btn-spin': refreshing,
                'kp-icon-btn-autoflow': isLive && !refreshing,
              }" :disabled="loading || refreshing"
                :title="isLive ? t('knowledgeStages.autoRefreshOn') : t('knowledgeStages.refresh')"
                :aria-label="isLive ? t('knowledgeStages.autoRefreshOn') : t('knowledgeStages.refresh')"
                @click="onManualRefresh">
                <t-icon name="refresh" size="16px" />
              </button>
              <t-popconfirm
                v-if="canCancelParse"
                theme="warning"
                :content="t('knowledgeBase.cancelParseConfirmBody', { title: props.docTitle || props.knowledgeId })"
                :confirm-btn="{ content: t('knowledgeBase.cancelParse'), theme: 'danger' }"
                :cancel-btn="{ content: t('common.cancel') }"
                placement="bottom"
                @confirm="onCancelParseConfirm"
              >
                <button type="button" class="kp-icon-btn kp-icon-btn-danger"
                  :class="{ 'kp-icon-btn-spin': cancelling }" :disabled="cancelling"
                  :title="t('knowledgeBase.cancelParse')" :aria-label="t('knowledgeBase.cancelParse')"
                  @click.stop>
                  <t-icon :name="cancelling ? 'loading' : 'close-circle'" size="15px" />
                </button>
              </t-popconfirm>
              <t-button v-if="showToolbarRetry" size="small" theme="danger" variant="outline" @click="onRetry">
                <t-icon name="refresh" size="14px" />
                <span style="margin-left: 4px">{{ t('knowledgeStages.retry') }}</span>
              </t-button>
              <button v-if="showClose" type="button" class="kp-icon-btn" :aria-label="t('knowledgeStages.close')"
                :title="t('knowledgeStages.close')" @click="emit('close')">
                <t-icon name="close" size="16px" />
              </button>
            </div>
          </div>

          <div v-if="headStats.length > 0" class="kp-stat-strip">
            <span v-for="stat in headStats" :key="stat.key" class="kp-stat">
              <span class="kp-stat-label">{{ stat.label }}</span>
              <span class="kp-stat-value"
                :class="{ 'kp-stat-value-text': !stat.mono, 'kp-stat-note': stat.note }">{{ stat.value }}</span>
            </span>
          </div>

          <div v-if="attemptTabs.length > 0" class="kp-attempts">
            <button v-for="tab in attemptTabs" :key="tab.n" type="button" class="kp-attempt"
              :class="{ 'kp-attempt-active': tab.active }" @click="onAttemptChange(tab.n)">
              <span class="kp-attempt-num kp-mono">#{{ tab.n }}</span>
              <span class="kp-attempt-glyph" :class="attemptGlyph(tab.status).cls">{{ attemptGlyph(tab.status).ch
                }}</span>
            </button>
          </div>

          <div v-if="shownStall(data?.stall_state, stalledMin) === 'queued'" class="kp-stall kp-stall-queued"
            role="status">
            <t-icon name="time" size="16px" class="kp-stall-icon" />
            <div class="kp-stall-body">
              <div class="kp-stall-title">{{ t('knowledgeStages.stall.queuedTitle', { minutes: stalledMin }) }}</div>
              <div class="kp-stall-hint">{{ t('knowledgeStages.stall.queuedHint') }}</div>
            </div>
          </div>
          <div v-else-if="shownStall(data?.stall_state, stalledMin) === 'stalled'" class="kp-stall" role="status">
            <t-icon name="time" size="16px" class="kp-stall-icon" />
            <div class="kp-stall-body">
              <div class="kp-stall-title">{{ t('knowledgeStages.stall.title', { minutes: stalledMin }) }}</div>
              <div class="kp-stall-hint">
                {{ stalledStageLabel
                  ? t('knowledgeStages.stall.hintAtStage', { stage: stalledStageLabel })
                  : t('knowledgeStages.stall.hint') }}
              </div>
            </div>
            <t-popconfirm
              v-if="canCancelParse"
              theme="warning"
              :content="t('knowledgeBase.cancelParseConfirmBody', { title: props.docTitle || props.knowledgeId })"
              :confirm-btn="{ content: t('knowledgeBase.cancelParse'), theme: 'danger' }"
              :cancel-btn="{ content: t('common.cancel') }"
              placement="bottom"
              @confirm="onCancelParseConfirm"
            >
              <button type="button" class="kp-stall-btn" :disabled="cancelling" @click.stop>
                <t-icon :name="cancelling ? 'loading' : 'close-circle'" size="14px" />
                <span>{{ t('knowledgeBase.cancelParse') }}</span>
              </button>
            </t-popconfirm>
          </div>

          <div v-if="showLastError && data?.last_error" class="kp-last-error" role="alert">
            <div class="kp-err-top">
              <span class="kp-err-glyph" aria-hidden="true">!</span>
              <span class="kp-err-where">{{ errorHeadline }}</span>
              <span v-if="data.last_error.error_code" class="kp-err-code kp-mono">{{ data.last_error.error_code }}</span>
              <button type="button" class="kp-err-copy" :title="t('knowledgeStages.copyError')"
                :aria-label="t('knowledgeStages.copyError')" @click="copyValue(data.last_error)">
                <t-icon name="copy" size="14px" />
              </button>
            </div>
            <div v-if="data.last_error.error_message" class="kp-err-message">{{ data.last_error.error_message }}</div>
            <div v-if="localizedErrorSuggestion(data.last_error.error_code)" class="kp-err-hint">
              {{ localizedErrorSuggestion(data.last_error.error_code) }}
            </div>
            <div class="kp-err-actions">
              <AssistantActionButton :label="t('assistant.actions.diagnose')"
                :query="t('assistant.prompts.documentFailure', { title: docTitle || knowledgeId, id: knowledgeId })"
                :selection="{ field: errorHeadline, text: [data.last_error.error_code, data.last_error.error_message].filter(Boolean).join('\n') }" />
              <button v-if="data.parse_status === 'failed'" type="button" class="kp-err-btn" @click="onRetry">
                <t-icon name="refresh" size="14px" />
                <span>{{ t('knowledgeStages.retry') }}</span>
              </button>
            </div>
          </div>
        </div>

        <!-- ============== BODY (Waterfall) ============== -->
        <div class="kp-body" :class="{ 'kp-body-with-detail': detailOpen }">
          <div v-if="loading && !data" class="kp-state">
            <t-loading size="small" />
          </div>
          <div v-else-if="!data && !loading" class="kp-state kp-state-empty">
            <span>{{ t('knowledgeStages.noActivity') }}</span>
          </div>

          <template v-else-if="data">
            <!-- Ruler sits outside the scroll region so the time axis never
                 scrolls away or fights position:sticky inside overflow. -->
            <div v-if="showRuler" class="kp-ruler">
              <div class="kp-ruler-spacer-name" />
              <div class="kp-ruler-spacer-meta" />
              <div class="kp-ruler-track">
                <span v-for="(tick, i) in rulerTicks" :key="i" class="kp-tick"
                  :class="{ 'kp-tick-first': i === 0, 'kp-tick-last': i === rulerTicks.length - 1 }"
                  :style="{ left: tick.left }">
                  <span class="kp-tick-line" />
                  <span class="kp-tick-label kp-mono">{{ tick.label }}</span>
                </span>
              </div>
            </div>

            <div ref="scrollRef" class="kp-scroll">
            <div class="kp-rows" :class="{ 'kp-rows-gridless': !showRuler }"
              :style="{ '--kp-grid-step': gridStepPct }">
              <div v-for="row in flatRows" :key="row.key" class="kp-row" :data-span-key="row.key" :class="{
                'kp-row-active': selectedSpanId === row.key,
                'kp-row-root': row.isRoot,
                'kp-row-stage': row.isStage,
                'kp-row-span': !row.isRoot && !row.isStage,
                'kp-row-expandable': row.hasChildren && !row.isRoot,
                'kp-row-inert': isNotRun(row.node),
              }" :title="row.hasChildren && !row.isRoot ? t('knowledgeStages.rowSelectHint') : undefined"
                @click="selectRow(row)">
                <div class="kp-cell-name">
                  <div class="kp-name-inner">
                    <span v-for="d in row.depth" :key="d" class="kp-name-guide" aria-hidden="true" />
                    <button v-if="row.hasChildren && !row.isRoot" type="button" class="kp-tree-toggle"
                      :aria-expanded="isRowExpanded(row.key)" :aria-label="treeToggleAriaLabel(row)"
                      @click="toggleTree(row, $event)">
                      <t-icon :name="isRowExpanded(row.key) ? 'chevron-down' : 'chevron-right'" size="14px" />
                    </button>
                    <span v-else class="kp-tree-toggle-spacer" />
                    <span class="kp-status-dot"
                      :class="['kp-dot-' + row.node.status, { 'kp-dot-placeholder': isPlaceholder(row.node) }]" />
                    <span class="kp-name-text" :title="rowLabel(row)"
                      :class="{ 'kp-name-root': row.isRoot, 'kp-name-mono': !row.isRoot && !row.isStage }">{{
                        rowLabel(row) }}</span>
                    <span v-if="rowKindLabel(row)" class="kp-name-kind">{{ rowKindLabel(row) }}</span>
                  </div>
                </div>

                <div class="kp-cell-dur" :class="isNotRun(row.node) ? 'kp-dur-inert' : 'kp-mono'">
                  <template v-if="isNotRun(row.node)">
                    {{ t('knowledgeStages.notRun') }}
                  </template>
                  <template v-else-if="row.node.status === 'running'">
                    <span class="kp-running-time">{{ formatDuration(liveElapsedMs(row.node)) }}</span>
                  </template>
                  <template v-else>
                    <span :title="postprocessElapsedMs(row.node, postprocessDisplayNow()) !== null
                      ? t('knowledgeStages.detail.includingChildren') : undefined">
                      {{ formatSpanDuration(row.node, true) }}
                    </span>
                  </template>
                </div>

                <div class="kp-cell-bar">
                  <span v-if="nowMarkerPct !== null && row.isRoot" class="kp-now-marker"
                    :style="{ left: nowMarkerPct + '%' }" />
                  <div v-if="isPlaceholder(row.node) && !isNotRun(row.node)" class="kp-bar kp-bar-placeholder" />
                  <template v-else>
                    <!-- Wrapping outline: descendants extend past this
                         span's own end (e.g. async postprocess subspans
                         under a closed stage). Renders behind the solid
                         self-bar so both are visible. -->
                    <div v-if="wrapStyle(row.node)" class="kp-bar-wrap" :class="['kp-bar-wrap-' + row.node.status]"
                      :style="wrapStyle(row.node) || {}">
                      <span class="kp-bar-tip">
                        <span class="kp-bar-tip-name">{{ rowLabel(row) }}</span>
                        <span class="kp-bar-tip-sep">·</span>
                        <span class="kp-mono">{{ formatDuration(wrapDurationMs(row.node)) }}</span>
                        <span class="kp-bar-tip-sep">·</span>
                        <span>{{ t('knowledgeStages.detail.includingChildren') }}</span>
                      </span>
                    </div>
                    <div class="kp-bar"
                      :class="['kp-bar-' + row.node.status, { 'kp-bar-running-anim': row.node.status === 'running' }]"
                      :style="barStyle(row.node)">
                      <span class="kp-bar-tip">
                        <span class="kp-bar-tip-name">{{ rowLabel(row) }}</span>
                        <span class="kp-bar-tip-sep">·</span>
                        <span class="kp-mono">{{ row.node.status === 'running'
                          ? formatDuration(liveElapsedMs(row.node))
                          : formatSpanDuration(row.node) }}</span>
                        <span class="kp-bar-tip-sep">·</span>
                        <span>{{ localizedStatus(row.node.status) }}</span>
                      </span>
                    </div>
                    <span v-if="barOffsetPct(row.node) !== null && barOffsetMs(row.node) > 0"
                      class="kp-bar-offset kp-mono"
                      :style="{ '--kp-offset-position': barOffsetPct(row.node) + '%' }"
                      :title="'+' + formatDuration(barOffsetMs(row.node))">
                      +{{ formatDuration(barOffsetMs(row.node)) }}
                    </span>
                  </template>
                </div>
              </div>
            </div>
            </div>
          </template>
        </div>

        <!-- ============== DETAIL PANEL ============== -->
        <div class="kp-detail" :class="{ 'kp-detail-open': detailOpen }">
          <template v-if="selectedRow">
            <div class="kp-detail-head">
              <div class="kp-detail-title">
                <span class="kp-status-dot kp-detail-dot" :class="['kp-dot-' + selectedRow.node.status]" />
                <span class="kp-detail-name">{{ rowLabel(selectedRow) }}</span>
                <span class="kp-detail-kind">{{ rowKindLabel(selectedRow) }}</span>
                <span class="kp-status-chip" :class="'kp-chip-' + selectedRow.node.status">
                  {{ localizedStatus(selectedRow.node.status) }}
                </span>
              </div>
              <div class="kp-detail-actions">
                <button type="button" class="kp-icon-btn" :title="t('knowledgeStages.copyDetails')"
                  @click.stop="copySpan(selectedRow.node)">
                  <t-icon name="copy" size="18px" />
                </button>
                <button type="button" class="kp-icon-btn" :title="t('knowledgeStages.close')" @click="closeDetail">
                  <t-icon name="close" size="18px" />
                </button>
              </div>
            </div>

            <div class="kp-tabs">
              <button type="button" class="kp-tab" :class="{ 'kp-tab-active': detailTab === 'overview' }"
                @click="detailTab = 'overview'">{{ t('knowledgeStages.tab.overview') }}</button>
              <button type="button" class="kp-tab"
                :class="{ 'kp-tab-active': detailTab === 'input', 'kp-tab-empty': !tabHasContent('input') }"
                @click="detailTab = 'input'">{{ t('knowledgeStages.detail.input') }}</button>
              <button type="button" class="kp-tab"
                :class="{ 'kp-tab-active': detailTab === 'output', 'kp-tab-empty': !tabHasContent('output') }"
                @click="detailTab = 'output'">{{ t('knowledgeStages.detail.output') }}</button>
              <button v-if="tabHasContent('metadata')" type="button" class="kp-tab"
                :class="{ 'kp-tab-active': detailTab === 'metadata' }" @click="detailTab = 'metadata'">{{
                  t('knowledgeStages.detail.metadata') }}</button>
              <button type="button" class="kp-tab" :class="{ 'kp-tab-active': detailTab === 'raw' }"
                @click="detailTab = 'raw'">{{ t('knowledgeStages.tab.raw') }}</button>
            </div>

            <div class="kp-detail-body">
              <!-- Overview tab -->
              <template v-if="detailTab === 'overview'">
                <!-- Timing -->
                <div class="kp-section">
                  <div class="kp-section-title">{{ t('knowledgeStages.detail.timing') }}</div>
                  <div class="kp-kv">
                    <div class="kp-kv-row">
                      <span class="kp-kv-key">{{ t('knowledgeStages.detail.started') }}</span>
                      <span class="kp-kv-val kp-mono">{{ formatTime(selectedRow.node.started_at) }}</span>
                    </div>
                    <div class="kp-kv-row">
                      <span class="kp-kv-key">{{ t('knowledgeStages.detail.finished') }}</span>
                      <span class="kp-kv-val kp-mono">
                        <template v-if="selectedRow.node.status === 'running'">
                          <span class="kp-kv-running">{{ t('knowledgeStages.detail.inProgress') }}</span>
                        </template>
                        <template v-else>
                          {{ formatTime(selectedRow.node.finished_at) }}
                        </template>
                      </span>
                    </div>
                    <div class="kp-kv-row">
                      <span class="kp-kv-key">{{ t('knowledgeStages.detail.duration') }}</span>
                      <span class="kp-kv-val kp-mono">
                        <template v-if="selectedRow.node.status === 'running'">
                          {{ formatDuration(liveElapsedMs(selectedRow.node)) }}
                          <span class="kp-kv-tag-live">{{ t('knowledgeStages.detail.elapsed') }}</span>
                        </template>
                        <template v-else>
                          {{ formatSpanDuration(selectedRow.node) }}
                        </template>
                      </span>
                    </div>
                    <div v-if="postprocessElapsedMs(selectedRow.node, postprocessDisplayNow()) !== null" class="kp-kv-row">
                      <span class="kp-kv-key">{{ t('knowledgeStages.detail.includingChildren') }}</span>
                      <span class="kp-kv-val kp-mono">
                        {{ formatDuration(postprocessElapsedMs(selectedRow.node, postprocessDisplayNow()) ?? 0) }}
                      </span>
                    </div>
                    <div v-if="!selectedRow.isRoot && barOffsetMs(selectedRow.node) > 0" class="kp-kv-row">
                      <span class="kp-kv-key">{{ t('knowledgeStages.detail.offset') }}</span>
                      <span class="kp-kv-val kp-mono">+{{ formatDuration(barOffsetMs(selectedRow.node)) }}</span>
                    </div>
                  </div>
                </div>

                <!-- Identity / lineage -->
                <div class="kp-section">
                  <div class="kp-section-title">{{ t('knowledgeStages.detail.identity') }}</div>
                  <div class="kp-kv">
                    <div v-for="entry in identityFields(selectedRow)" :key="entry.key" class="kp-kv-row">
                      <span class="kp-kv-key">{{ entry.label }}</span>
                      <span class="kp-kv-val" :class="{ 'kp-mono': entry.mono, 'kp-kv-truncate': entry.copyable }">
                        <span class="kp-kv-text">{{ entry.value }}</span>
                        <button v-if="entry.copyable" type="button" class="kp-kv-copy"
                          :title="t('knowledgeStages.copy')" @click.stop="copyValue(entry.value)">
                          <t-icon name="copy" size="14px" />
                        </button>
                      </span>
                    </div>
                  </div>
                </div>

                <div v-if="traceMetadata" class="kp-section">
                  <div class="kp-section-title">{{ t('knowledgeStages.detail.traceMetadata') }}</div>
                  <p class="kp-section-desc">{{ t('knowledgeStages.detail.metadataHint') }}</p>
                  <div class="kp-kv">
                    <div v-for="entry in buildKvEntries(traceMetadata)" :key="entry.key" class="kp-kv-row kp-kv-row-multiline">
                      <span class="kp-kv-key kp-mono">{{ entry.key }}</span>
                      <span class="kp-kv-val kp-kv-scalar">{{ entry.display }}</span>
                    </div>
                  </div>
                </div>

                <!-- Stage breakdown (root only) -->
                <div v-if="selectedRow.isRoot" class="kp-section">
                  <div class="kp-section-title">{{ t('knowledgeStages.detail.stageBreakdown') }}</div>
                  <div class="kp-breakdown">
                    <div v-for="s in stageBreakdown" :key="s.name" class="kp-breakdown-row"
                      :class="['kp-breakdown-' + s.status]">
                      <span class="kp-breakdown-label">
                        <span class="kp-status-dot" :class="['kp-dot-' + s.status]" />
                        {{ s.label }}
                      </span>
                      <div class="kp-breakdown-track">
                        <div class="kp-breakdown-bar" :class="['kp-bar-' + s.status]" :style="{ width: s.pct + '%' }" />
                      </div>
                      <span class="kp-breakdown-dur kp-mono">{{ s.status === 'skipped' || s.status === 'pending'
                        ? '—'
                        : formatDuration(s.duration_ms) }}</span>
                    </div>
                  </div>
                </div>

                <!-- Error -->
                <div
                  v-if="(selectedRow.node.status === 'failed' || selectedRow.node.status === 'cancelled') && (selectedRow.node.error_code || selectedRow.node.error_message)"
                  class="kp-error-block">
                  <div class="kp-error-head">
                    <span class="kp-error-glyph">!</span>
                    <span class="kp-error-title">{{ localizedErrorTitle(selectedRow.node.error_code) ||
                      t('knowledgeStages.detail.error') }}</span>
                    <span v-if="selectedRow.node.error_code" class="kp-error-code kp-mono">{{
                      selectedRow.node.error_code }}</span>
                  </div>
                  <pre v-if="selectedRow.node.error_message"
                    class="kp-error-msg kp-mono">{{ selectedRow.node.error_message }}</pre>
                </div>

                <div v-if="!selectedRow.node.span_id && !selectedRow.node.started_at" class="kp-detail-hint">
                  {{ t('knowledgeStages.detail.placeholderHint') }}
                </div>
              </template>

              <!-- Input / Output / Metadata tabs -->
              <template v-else-if="detailTab === 'input' || detailTab === 'output' || detailTab === 'metadata'">
                <div v-if="!tabHasContent(detailTab)" class="kp-detail-empty">
                  <span>{{ detailTab === 'metadata' ? t('knowledgeStages.detail.metadataEmpty') :
                    t('knowledgeStages.detail.empty') }}</span>
                </div>
                <template v-else>
                  <div class="kp-section">
                    <div class="kp-section-bar">
                      <span class="kp-section-title">{{ t('knowledgeStages.detail.' + detailTab) }}</span>
                      <button type="button" class="kp-section-action"
                        @click="copyValue((selectedRow.node as any)[detailTab])">
                        <t-icon name="copy" size="14px" />
                        <span>{{ t('knowledgeStages.copy') }}</span>
                      </button>
                    </div>

                    <div v-if="isObjectWithKeys((selectedRow.node as any)[detailTab])" class="kp-kv">
                      <div v-for="entry in buildKvEntries((selectedRow.node as any)[detailTab])" :key="entry.key"
                        class="kp-kv-row kp-kv-row-multiline">
                        <span class="kp-kv-key kp-mono">{{ entry.key }}</span>
                        <div class="kp-kv-val kp-kv-multiline">
                          <span v-if="entry.kind === 'bool'" class="kp-mono"
                            :class="{ 'kp-bool-true': entry.raw, 'kp-bool-false': !entry.raw }">{{ entry.display
                            }}</span>
                          <span v-else-if="entry.kind === 'scalar'" class="kp-kv-scalar">{{ entry.display }}</span>
                          <!-- Short payloads render inline so the user
                               sees the data without an extra click. The
                               summary chip ("Array · 3") is shown above
                               the JSON for context. -->
                          <div v-else-if="entry.defaultExpanded" class="kp-kv-inline">
                            <span class="kp-kv-summary kp-mono kp-kv-summary-static">{{ entry.display }}</span>
                            <pre class="kp-json kp-mono">{{ prettyJSON(entry.raw) }}</pre>
                          </div>
                          <div v-else class="kp-kv-collapsible">
                            <button type="button" class="kp-kv-toggle"
                              @click.stop="toggleJsonKey(detailTab, entry.key)">
                              <span class="kp-kv-summary kp-mono">{{ entry.display }}</span>
                              <span class="kp-kv-toggle-label">{{
                                isJsonExpanded(detailTab, entry.key)
                                  ? t('knowledgeStages.detail.hideJson')
                                  : t('knowledgeStages.detail.showJson')
                              }}</span>
                            </button>
                            <pre v-if="isJsonExpanded(detailTab, entry.key)"
                              class="kp-json kp-mono">{{ prettyJSON(entry.raw) }}</pre>
                          </div>
                        </div>
                      </div>
                    </div>
                    <pre v-else class="kp-json kp-mono">{{ prettyJSON((selectedRow.node as any)[detailTab]) }}</pre>
                  </div>
                </template>
              </template>

              <!-- Raw JSON tab -->
              <template v-else-if="detailTab === 'raw'">
                <div class="kp-section">
                  <div class="kp-section-bar">
                    <span class="kp-section-title">{{ t('knowledgeStages.tab.raw') }}</span>
                    <button type="button" class="kp-section-action" @click="copyValue(selectedRow.node)">
                      <t-icon name="copy" size="14px" />
                      <span>{{ t('knowledgeStages.copy') }}</span>
                    </button>
                  </div>
                  <pre class="kp-json kp-mono kp-json-large">{{ prettyJSON(selectedRow.node) }}</pre>
                </div>
              </template>
            </div>
          </template>
        </div>
      </div>
    </template>
  </div>
</template>

<style scoped lang="less">
.kp-timeline {
  font-family: var(--app-font-family);
  font-size: var(--app-text-md);
  color: var(--td-text-color-primary);
  width: 100%;
  height: 100%;
  /* Defensive: never let waterfall rows / detail panel push past the
     drawer's visible bounds even if the host width is unexpectedly
     narrow. The horizontal scroll inside .kp-body handles legit cases
     where labels are long. */
  overflow: hidden;
}

.kp-shell {
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
  min-height: 0;
  min-width: 0;
  background: var(--td-bg-color-container);
  overflow: hidden;
}

/* ============== HEADER ============== */
.kp-head {
  flex: 0 0 auto;
  padding: 12px 20px;
  border-bottom: 1px solid var(--td-component-stroke);
  background: var(--td-bg-color-container);
}

.kp-head-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.kp-head-titles {
  flex: 1;
  min-width: 0;
}

/* The panel's own name. It used to be the first crumb of the grey meta
   sentence below the title, where it read as just another fact about the
   document rather than as the label for the view you are looking at. */
.kp-head-overline {
  margin-bottom: 3px;
  font-size: var(--app-text-2xs);
  font-weight: 600;
  letter-spacing: 0.08em;
  line-height: 1.2;
  color: var(--td-text-color-placeholder);
}

.kp-head-title-row {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

/* Natural width, so the status tag sits against the filename instead of
   being pushed across the header to sit among the toolbar buttons. */
.kp-head-doc-title {
  flex: 0 1 auto;
  min-width: 0;
  margin: 0;
  font-size: var(--app-text-xl);
  font-weight: 600;
  line-height: 1.3;
  color: var(--td-text-color-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.kp-head-status-tag {
  flex-shrink: 0;
}

/* LIVE badge — sits next to the title while polling, telegraphs the
   pipeline is actively refreshing. Pulsing dot + uppercase mono label. */
.kp-live-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 8px;
  border-radius: var(--td-radius-medium);
  background: var(--td-warning-color-light);
  color: var(--td-warning-color);
  font-size: var(--app-text-2xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  line-height: 1;
}

.kp-live-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--td-warning-color);
  animation: kpLivePulse 1.4s ease-in-out infinite;
}

.kp-live-text {
  font-family: var(--app-font-family-mono);
}

@keyframes kpLivePulse {

  0%,
  100% {
    opacity: 1;
    transform: scale(1);
  }

  50% {
    opacity: 0.45;
    transform: scale(0.8);
  }
}

.kp-head-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
  margin-left: auto;
}

/* Label/value pairs in place of the old dot-joined run-on sentence.
   Values are tabular mono so successive polls don't jiggle the row. */
.kp-stat-strip {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 18px;
  margin-top: 9px;
}

.kp-stat {
  display: inline-flex;
  align-items: baseline;
  gap: 5px;
  min-width: 0;
}

.kp-stat-label {
  font-size: var(--app-text-xs);
  color: var(--td-text-color-placeholder);
  white-space: nowrap;
}

.kp-stat-value {
  font-family: var(--app-font-family-mono);
  font-size: var(--app-text-sm);
  font-weight: 600;
  color: var(--td-text-color-secondary);
  font-variant-numeric: tabular-nums;
}

/* Prose values (task counts, "just now") read badly in tabular mono. */
.kp-stat-value-text {
  font-family: var(--app-font-family);
  font-weight: 500;
}

.kp-stat-note {
  color: var(--td-warning-color);
}

.kp-icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: none;
  background: transparent;
  color: var(--td-text-color-placeholder);
  cursor: pointer;
  border-radius: var(--td-radius-default);
  transition: background var(--app-motion-fast) ease, color var(--app-motion-fast) ease;
}

.kp-icon-btn:hover:not(:disabled) {
  background: var(--td-bg-color-secondarycontainer);
  color: var(--td-text-color-primary);
}

.kp-icon-btn:disabled {
  cursor: not-allowed;
  opacity: 0.4;
}

/* Stop-parse control — stays a quiet placeholder icon until hover, then
   reveals its destructive intent with the error tint. Matches the other
   header icon buttons rather than shouting with a full outline button. */
.kp-icon-btn-danger:hover:not(:disabled) {
  background: var(--td-error-color-light);
  color: var(--td-error-color);
}

.kp-icon-btn-spin :deep(.t-icon) {
  animation: wk-spin 0.9s linear infinite;
}

/* Slow rotation while auto-polling — visually distinct from the
   manual-refresh fast spin. Tells the user "refresh is happening on
   its own" without an extra label or badge. */
.kp-icon-btn-autoflow :deep(.t-icon) {
  animation: wk-spin 4s linear infinite;
  color: var(--td-warning-color);
}

.kp-meta-glyph {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 12px;
  height: 12px;
  font-size: var(--app-text-xs);
  line-height: 1;
}

.kp-glyph-done {
  color: var(--td-success-color);
}

.kp-glyph-failed {
  color: var(--td-error-color);
}

.kp-glyph-running {
  color: var(--td-warning-color);
  animation: kpLivePulse 1.4s ease-in-out infinite;
}

.kp-glyph-unknown {
  color: var(--td-text-color-placeholder);
}

/* Attempts strip */
.kp-attempts {
  display: flex;
  gap: 6px;
  margin-top: 10px;
  overflow-x: auto;
  padding-bottom: 2px;
}

.kp-attempt {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 10px;
  border: 1px solid var(--td-component-border);
  border-radius: var(--td-radius-default);
  background: var(--td-bg-color-container);
  color: var(--td-text-color-secondary);
  font-size: var(--app-text-sm);
  line-height: 1.4;
  cursor: pointer;
  white-space: nowrap;
  transition: background var(--app-motion-fast) ease, border-color var(--app-motion-fast) ease, color var(--app-motion-fast) ease;
}

.kp-attempt:not(.kp-attempt-active):hover {
  background: var(--td-bg-color-secondarycontainer);
  border-color: var(--td-text-color-placeholder);
  color: var(--td-text-color-primary);
}

.kp-attempt-active {
  background: var(--td-brand-color);
  color: var(--td-text-color-anti);
  border-color: var(--td-brand-color);
}

.kp-attempt-active:hover {
  background: var(--td-brand-color);
  color: var(--td-text-color-anti);
  border-color: var(--td-brand-color);
}

.kp-attempt-active .kp-attempt-glyph,
.kp-attempt-active:hover .kp-attempt-glyph {
  color: var(--td-text-color-anti) !important;
}

.kp-attempt-num {
  font-weight: 600;
  font-size: var(--app-text-xs);
}

.kp-attempt-glyph {
  font-size: 9px;
  line-height: 1;
}

/* ============== BODY (Waterfall) ============== */
.kp-body {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--td-bg-color-container);
}

/* Sized by its rows once the detail panel is open — see .kp-detail. */
.kp-body-with-detail {
  flex: 0 0 auto;
  max-height: 60%;
}

.kp-scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: auto;
  padding-bottom: 16px;
}

.kp-state {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  flex: 1 1 auto;
  padding: 56px 20px;
  font-size: var(--app-text-md);
  color: var(--td-text-color-placeholder);
}

/* Ruler — pinned above .kp-scroll, not inside the scroller */
.kp-ruler {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: minmax(210px, 32%) 76px 1fr;
  height: 24px;
  align-items: end;
  padding: 10px 20px 6px;
  background: var(--td-bg-color-container);
  border-bottom: 1px dashed var(--td-component-stroke);
  box-shadow: 0 4px 8px -6px rgba(0, 0, 0, 0.12);
}

.kp-ruler-spacer-name,
.kp-ruler-spacer-meta {
  height: 100%;
}

.kp-ruler-track {
  position: relative;
  height: 100%;
  margin-right: 16px;
}

.kp-tick {
  position: absolute;
  bottom: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  transform: translateX(-50%);
  font-size: var(--app-text-2xs);
  color: var(--td-text-color-placeholder);
}

.kp-tick-first {
  transform: translateX(0);
  align-items: flex-start;
}

.kp-tick-last {
  transform: translateX(-100%);
  align-items: flex-end;
}

.kp-tick-line {
  width: 1px;
  height: 5px;
  background: var(--td-component-border);
}

.kp-tick-label {
  margin-top: 2px;
  font-size: var(--app-text-2xs);
  letter-spacing: 0.02em;
}

/* Rows */
.kp-rows {
  display: flex;
  flex-direction: column;
}

.kp-row {
  display: grid;
  grid-template-columns: minmax(210px, 32%) 76px 1fr;
  align-items: center;
  height: 34px;
  cursor: pointer;
  position: relative;
  padding: 0 20px;
  transition: background var(--app-motion-fast) ease;
}

.kp-row::before {
  content: "";
  position: absolute;
  left: 0;
  top: 4px;
  bottom: 4px;
  width: 2px;
  background: transparent;
  border-radius: 0 2px 2px 0;
  transition: background var(--app-motion-fast) ease;
}

.kp-row:hover {
  background: var(--td-bg-color-secondarycontainer);
}

.kp-row-active {
  background: var(--td-bg-color-secondarycontainer);
}

.kp-row-active::before {
  background: var(--td-brand-color);
}

/* v1 painted three near-identical tints (root / stage / subspan) that
   read as banding artifacts rather than as hierarchy, and fought the
   hover tint on top of them. Depth is carried by the indent guides in
   the name cell; background is reserved for hover and selection. */
.kp-row-root {
  font-weight: 600;
  border-bottom: 1px solid var(--td-component-stroke);
}

/* Stages that never ran because an upstream stage failed. */
.kp-row-inert {
  opacity: 0.45;
}

.kp-row-expandable:hover .kp-tree-toggle {
  color: var(--td-text-color-primary);
  background: var(--td-bg-color-container-hover);
}

/* Name cell */
.kp-cell-name {
  min-width: 0;
}

.kp-name-inner {
  display: flex;
  align-items: center;
  gap: 7px;
  min-width: 0;
  height: 100%;
}

/* One hairline per depth level, replacing v1's blind left padding. It
   costs the same horizontal space but actually shows which parent a
   nested subspan belongs to. */
.kp-name-guide {
  width: 1px;
  align-self: stretch;
  flex-shrink: 0;
  margin-right: 8px;
  background: var(--td-component-stroke);
}

.kp-tree-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  padding: 0;
  cursor: pointer;
  color: var(--td-text-color-placeholder);
  width: 22px;
  height: 22px;
  margin: -3px 0;
  transition: color var(--app-motion-instant) ease, background var(--app-motion-fast) ease;
  flex-shrink: 0;
  border-radius: var(--td-radius-default);
}

.kp-tree-toggle:hover {
  color: var(--td-text-color-primary);
  background: var(--td-bg-color-container-hover);
}

.kp-tree-toggle-spacer {
  width: 22px;
  height: 22px;
  display: inline-block;
  flex-shrink: 0;
  margin: -3px 0;
}

.kp-status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
  background: var(--td-text-color-placeholder);
}

.kp-name-text {
  font-size: var(--app-text-sm);
  color: var(--td-text-color-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.kp-name-mono {
  font-family: var(--app-font-family-mono);
  font-size: var(--app-text-xs);
}

.kp-name-root {
  font-weight: 600;
  font-size: var(--app-text-md);
}

/* Only rendered for `generation` spans now. v1 pushed a ROOT / STAGE /
   SPAN label to the far right of the name column with margin-left:auto,
   which opened a wide empty gap in every single row. */
.kp-name-kind {
  flex-shrink: 0;
  margin-left: 6px;
  padding: 1px 4px;
  border-radius: var(--td-radius-small);
  font-family: var(--app-font-family-mono);
  font-size: var(--app-text-2xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--td-text-color-secondary);
  background: var(--td-bg-color-component);
}

/* Duration cell */
.kp-cell-dur {
  font-size: var(--app-text-xs);
  color: var(--td-text-color-secondary);
  text-align: right;
  padding-right: 14px;
  letter-spacing: 0.02em;
  font-variant-numeric: tabular-nums;
}

/* "Never ran" beats an em dash the reader has to decode. */
.kp-dur-inert {
  font-family: var(--app-font-family);
  color: var(--td-text-color-placeholder);
}

.kp-running-time {
  color: var(--td-warning-color);
  font-weight: 600;
}

/* Bar cell */
.kp-cell-bar {
  position: relative;
  height: 34px;
  margin-right: 16px;
  /* Gridlines on every ruler tick. An 8px bar floating in 600px of
     white cannot be read against an axis that only exists at the top of
     a scrolling list; --kp-grid-step is set from the axis step. */
  background-image: repeating-linear-gradient(to right,
      var(--td-component-stroke) 0 1px,
      transparent 1px var(--kp-grid-step, 25%));
  border-right: 1px solid var(--td-component-stroke);
}

/* No ruler (sub-50ms trace) means no axis for the gridlines to line up
   with, so they would just be decoration. */
.kp-rows-gridless .kp-cell-bar {
  background-image: none;
  border-right: none;
}

/* Vertical "now" cursor — animates left during polling so the user can
   visually confirm time is advancing even when the running bar grows
   slowly toward the right edge of the trace. */
.kp-now-marker {
  position: absolute;
  top: 4px;
  bottom: 4px;
  width: 1px;
  background: var(--td-warning-color);
  opacity: 0.65;
  z-index: 1;
  pointer-events: none;
  transition: left 1s linear;
}

.kp-now-marker::before {
  content: "";
  position: absolute;
  top: -2px;
  left: -3px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--td-warning-color);
  animation: kpLivePulse 1.4s ease-in-out infinite;
}

.kp-bar {
  position: absolute;
  top: 13px;
  height: 8px;
  border-radius: var(--td-radius-small);
  background: var(--td-text-color-placeholder);
  min-width: 2px;
  /* Smooth left/width changes so polling-driven re-renders feel like
     the bar is growing, not jumping. The 1s easing matches the nowTick
     1Hz cadence. */
  transition: left 800ms cubic-bezier(0.2, 0.8, 0.2, 1),
    width 800ms cubic-bezier(0.2, 0.8, 0.2, 1),
    filter 150ms ease;
  z-index: 2;
}

.kp-row:hover .kp-bar {
  filter: brightness(1.05);
}

.kp-bar-tip {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--td-text-color-primary);
  color: var(--td-text-color-anti);
  font-size: var(--app-text-xs);
  padding: 4px 8px;
  border-radius: var(--td-radius-default);
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--app-motion-fast) ease;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 4px;
}

.kp-bar-tip-name {
  font-weight: 500;
}

.kp-bar-tip-sep {
  color: var(--td-font-white-3);
}

.kp-bar:hover .kp-bar-tip {
  opacity: 1;
}

/* ROOT row is the first row under the fixed ruler. The default
   `bottom: calc(100% + 8px)` tooltip placement can clip against the
   ruler band, so flip tooltips below the bar for the root row. */
.kp-row-root .kp-bar-tip {
  bottom: auto;
  top: calc(100% + 8px);
}

/* Status palette — NOT all green. The project brand color happens
   to be green, which made done/running visually identical (both
   solid green). Done stays green (universal "success" semantic);
   running goes amber + striped (CI-style "in progress" — recognized
   everywhere from GitHub Actions to Jenkins). The two are now
   unmistakably different at a glance. */
.kp-bar-done {
  background: var(--td-success-color);
}

.kp-bar-failed {
  background: var(--td-error-color);
}

.kp-bar-cancelled {
  background: transparent;
  border: 1px dashed var(--td-error-color);
  height: 6px;
  top: 13px;
}

.kp-bar-skipped {
  background: var(--td-text-color-placeholder);
  opacity: 0.4;
}

.kp-bar-pending {
  display: none;
}

.kp-bar-running {
  /* Muted amber base. The diagonal stripes do the "in flight"
     signaling — the tone just supplies a subtle hint. Earlier
     iteration used full --td-warning-color + halo shadow + hard
     white stripes; users found that visually screaming. */
  background-color: var(--td-warning-color-3);
  background-image: linear-gradient(135deg,
      rgba(255, 255, 255, 0.22) 25%,
      transparent 25%,
      transparent 50%,
      rgba(255, 255, 255, 0.22) 50%,
      rgba(255, 255, 255, 0.22) 75%,
      transparent 75%,
      transparent);
  background-size: 14px 14px;
  animation: kpStripes 1.6s linear infinite;
}

@keyframes kpStripes {
  to {
    background-position: 14px 0;
  }
}

/* Wrapping outline bar — shows the full window from this span's start
   to the latest descendant end. Used when async children extend past
   the parent's own finished_at (e.g. postprocess stage closes fast but
   its summary/question subspans run for a long time). */
.kp-bar-wrap {
  position: absolute;
  top: 9px;
  height: 14px;
  border: 1px dashed var(--td-component-border);
  border-radius: var(--td-radius-small);
  background: transparent;
  min-width: 4px;
  z-index: 1;
  pointer-events: auto;
  transition: left 800ms cubic-bezier(0.2, 0.8, 0.2, 1),
    width 800ms cubic-bezier(0.2, 0.8, 0.2, 1);
}

.kp-bar-wrap:hover {
  border-color: var(--td-text-color-secondary);
}

.kp-bar-wrap:hover .kp-bar-tip {
  opacity: 1;
}

.kp-bar-wrap-done {
  border-color: color-mix(in srgb, var(--td-brand-color) 35%, transparent);
}

.kp-bar-wrap-failed {
  border-color: rgba(229, 87, 64, 0.5);
}

.kp-bar-wrap-running {
  border-color: rgba(250, 157, 59, 0.5);
}

.kp-bar-wrap-cancelled {
  border-color: rgba(229, 87, 64, 0.3);
}

/* Indeterminate sweep on the running bar — gives obvious motion while
   waiting for the next poll. */
.kp-bar-running-anim {
  position: relative;
  overflow: hidden;
}

.kp-bar-running-anim::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg,
      transparent 0%,
      rgba(255, 255, 255, 0.5) 50%,
      transparent 100%);
  animation: kpSweep 1.6s linear infinite;
}

.kp-bar-placeholder {
  right: 4px;
  top: 13px;
  height: 6px;
  width: 14px;
  background: transparent;
  border: 1px dashed var(--td-component-border);
  border-radius: var(--td-radius-small);
  position: absolute;
}

.kp-bar-offset {
  position: absolute;
  bottom: 0;
  /* Keep the offset centered on its start time where space allows, then
     clamp the label inside the chart instead of letting it cover duration. */
  left: clamp(0px, calc(var(--kp-offset-position) - 45px), max(0px, calc(100% - 90px)));
  width: min(90px, 100%);
  line-height: 11px;
  font-size: 9px;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--td-text-color-placeholder);
  white-space: nowrap;
  opacity: 0;
  transition: opacity var(--app-motion-fast) ease;
}

.kp-row:hover .kp-bar-offset,
.kp-row-active .kp-bar-offset {
  opacity: 1;
}

@keyframes kpSweep {
  0% {
    transform: translateX(-100%);
  }

  100% {
    transform: translateX(100%);
  }
}

/* Status dots (shared with compact mode) */
.kp-dot-done {
  background: var(--td-success-color);
}

.kp-dot-running {
  background: var(--td-warning-color);
  animation: kpLivePulse 1.4s ease-in-out infinite;
}

.kp-dot-failed {
  background: var(--td-error-color);
}

.kp-dot-cancelled {
  background: transparent;
  border: 1px dashed var(--td-text-color-placeholder);
}

.kp-dot-skipped {
  background: var(--td-text-color-placeholder);
  opacity: 0.4;
}

.kp-dot-pending {
  background: transparent;
  border: 1px solid var(--td-component-border);
}

.kp-dot-placeholder {
  background: transparent;
  border: 1px dashed var(--td-component-border);
}

.kp-dot-completed {
  background: var(--td-success-color);
}

.kp-dot-processing {
  background: var(--td-warning-color);
  animation: kpLivePulse 1.4s ease-in-out infinite;
}

.kp-dot-unknown {
  background: var(--td-text-color-placeholder);
}

/* ============== LAST ERROR ==============
   Pinned in the header so long trace trees don't bury it.

   v1 stacked this the wrong way up: the headline was the error code, a
   solid red pill repeated that same code on the right, the generic
   "check the logs" filler sat in the middle at body size, and the one
   line that actually explains the failure — the backend's message —
   was last, smallest and greyest. v2 leads with the message, states the
   code once quietly, and puts the recovery action in the card. */
.kp-last-error {
  margin-top: 12px;
  padding: 12px 14px;
  background: var(--td-error-color-light);
  border: 1px solid var(--td-error-color-3);
  border-left: 3px solid var(--td-error-color);
  border-radius: var(--td-radius-medium);
}

.kp-err-top {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.kp-err-glyph {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  background: var(--td-error-color);
  color: var(--td-text-color-anti);
  border-radius: 50%;
  font-size: var(--app-text-xs);
  font-weight: 700;
  line-height: 1;
  flex-shrink: 0;
}

/* Says WHERE it broke ("文档解析阶段失败"), which the raw code never did.
   Deliberately NOT --td-error-color: that tone lands around 3:1 against
   the card's own error tint in both themes. The red bar, glyph and
   border already say "error"; the words just have to be readable. */
.kp-err-where {
  font-size: var(--app-text-sm);
  font-weight: 600;
  color: var(--td-text-color-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.kp-err-code {
  margin-left: auto;
  flex-shrink: 0;
  padding: 2px 6px;
  border-radius: var(--td-radius-small);
  font-size: var(--app-text-2xs);
  color: var(--td-error-color-7);
  background: color-mix(in srgb, var(--td-error-color) 12%, transparent);
  white-space: nowrap;
}

.kp-err-copy {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  flex-shrink: 0;
  border: none;
  background: transparent;
  color: var(--td-error-color);
  opacity: 0.7;
  border-radius: var(--td-radius-default);
  cursor: pointer;
  transition: opacity var(--app-motion-fast) ease, background var(--app-motion-fast) ease;
}

.kp-err-copy:hover {
  opacity: 1;
  background: color-mix(in srgb, var(--td-error-color) 12%, transparent);
}

/* The actual diagnostic, promoted from 11px grey to the card's primary
   line. Stays mono because it is verbatim backend output, and stays
   selectable because people paste it into bug reports. */
.kp-err-message {
  margin-top: 7px;
  font-family: var(--app-font-family-mono);
  font-size: var(--app-text-md);
  line-height: 1.5;
  color: var(--td-text-color-primary);
  white-space: pre-wrap;
  word-break: break-word;
  user-select: text;
}

.kp-err-hint {
  margin-top: 5px;
  font-size: var(--app-text-sm);
  line-height: 1.5;
  color: var(--td-text-color-secondary);
}

.kp-err-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
}

/* Recovery, not destruction — hence the outline rather than a solid
   danger fill. It also replaces v1's brand-green outline button, which
   sat in the toolbar glowing green against a red failure. */
.kp-err-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 28px;
  padding: 0 12px;
  border: 1px solid var(--td-error-color);
  border-radius: var(--td-radius-default);
  background: var(--td-bg-color-container);
  color: var(--td-error-color-7);
  font-size: var(--app-text-sm);
  cursor: pointer;
  transition: background var(--app-motion-fast) ease, color var(--app-motion-fast) ease;
}

.kp-err-btn:hover {
  background: var(--td-error-color);
  color: var(--td-text-color-anti);
}

/* Same card shape as .kp-last-error in the warning tone: the run may still
   finish, so this nudges rather than alarms. */
.kp-stall {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin-top: 12px;
  padding: 12px 14px;
  background: var(--td-warning-color-light);
  border: 1px solid var(--td-warning-color-3);
  border-left: 3px solid var(--td-warning-color);
  border-radius: var(--td-radius-medium);
}

/* Backlogged, not stuck: neutral tone, and no stop button to invite
   cancelling work that will still finish. */
.kp-stall-queued {
  background: var(--td-bg-color-secondarycontainer);
  border-color: var(--td-component-border);
  border-left-color: var(--td-text-color-placeholder);
}

.kp-stall-queued .kp-stall-icon {
  color: var(--td-text-color-secondary);
}

.kp-stall-icon {
  flex-shrink: 0;
  margin-top: 2px;
  color: var(--td-warning-color);
}

.kp-stall-body {
  flex: 1;
  min-width: 0;
}

.kp-stall-title {
  font-size: var(--app-text-sm);
  font-weight: 600;
  color: var(--td-text-color-primary);
}

.kp-stall-hint {
  margin-top: 4px;
  font-size: var(--app-text-sm);
  line-height: 1.5;
  color: var(--td-text-color-secondary);
}

.kp-stall-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  flex-shrink: 0;
  height: 28px;
  padding: 0 12px;
  border: 1px solid var(--td-warning-color);
  border-radius: var(--td-radius-default);
  background: var(--td-bg-color-container);
  color: var(--td-warning-color-7);
  font-size: var(--app-text-sm);
  cursor: pointer;
  transition: background var(--app-motion-fast) ease, color var(--app-motion-fast) ease;
}

.kp-stall-btn:hover:not(:disabled) {
  background: var(--td-warning-color);
  color: var(--td-text-color-anti);
}

.kp-stall-btn:disabled {
  cursor: default;
  opacity: 0.6;
}

/* ============== DETAIL PANEL ==============
   v1 gave the waterfall flex:1 and the detail a fixed 50% / 320px. On a
   short trace that meant ~55% of the panel was blank whenever nothing
   was selected, and rows got clipped mid-height as soon as something
   was. Now the waterfall is content-sized (capped at 60% so a long
   trace still leaves room) and the detail absorbs whatever is left. */
.kp-detail {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  border-top: 1px solid var(--td-component-stroke);
  background: var(--td-bg-color-container);
  height: 0;
  overflow: hidden;
}

/* flex-basis 0 (not auto) so the detail claims the remaining space
   instead of its own content height — otherwise a tall detail body
   squeezes the waterfall back out of view. */
.kp-detail-open {
  flex: 1 1 0;
  height: auto;
  min-height: 220px;
  animation: kpDetailIn var(--app-motion-base) ease both;
}

@keyframes kpDetailIn {
  from {
    opacity: 0;
    transform: translateY(6px);
  }

  to {
    opacity: 1;
    transform: none;
  }
}

.kp-detail-head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 12px 20px 10px;
  border-bottom: 1px solid var(--td-component-stroke);
}

.kp-detail-title {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.kp-detail-dot {
  width: 8px;
  height: 8px;
  flex-shrink: 0;
}

.kp-detail-name {
  font-weight: 600;
  font-size: var(--app-text-md);
  color: var(--td-text-color-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.kp-detail-kind {
  font-family: var(--app-font-family-mono);
  font-size: var(--app-text-2xs);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--td-text-color-placeholder);
  flex-shrink: 0;
}

/* Status chip — soft tinted background using the brand/error/success
   palette. Matches TDesign tag aesthetics. */
.kp-status-chip {
  display: inline-flex;
  align-items: center;
  padding: 1px 8px;
  border-radius: var(--td-radius-default);
  font-size: var(--app-text-xs);
  font-weight: 500;
  background: var(--td-bg-color-component);
  color: var(--td-text-color-primary);
  flex-shrink: 0;
}

.kp-chip-done {
  background: var(--td-success-color-light);
  color: var(--td-success-color);
}

.kp-chip-running {
  background: var(--td-warning-color-light);
  color: var(--td-warning-color);
}

.kp-chip-failed {
  background: var(--td-error-color-light);
  color: var(--td-error-color);
}

.kp-chip-cancelled {
  background: var(--td-bg-color-component);
  color: var(--td-text-color-secondary);
}

.kp-chip-skipped {
  background: var(--td-bg-color-component);
  color: var(--td-text-color-placeholder);
}

.kp-chip-pending {
  background: var(--td-bg-color-component);
  color: var(--td-text-color-secondary);
}

.kp-detail-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* Tabs */
.kp-tabs {
  flex: 0 0 auto;
  display: flex;
  gap: 0;
  padding: 0 20px;
  border-bottom: 1px solid var(--td-component-stroke);
  background: var(--td-bg-color-container);
}

.kp-tab {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 9px 14px 10px;
  border: none;
  background: transparent;
  color: var(--td-text-color-secondary);
  font-size: var(--app-text-md);
  cursor: pointer;
  position: relative;
  transition: color var(--app-motion-fast) ease;
}

.kp-tab:hover {
  color: var(--td-text-color-primary);
}

.kp-tab-active {
  color: var(--td-text-color-primary);
  font-weight: 600;
}

.kp-tab-active::after {
  content: "";
  position: absolute;
  left: 14px;
  right: 14px;
  bottom: -1px;
  height: 2px;
  background: var(--td-brand-color);
  border-radius: 2px 2px 0 0;
}

.kp-tab-empty {
  color: var(--td-text-color-placeholder);
}

.kp-detail-body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 16px 20px 18px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.kp-detail-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 48px 0;
  font-size: var(--app-text-md);
  color: var(--td-text-color-placeholder);
}

.kp-detail-hint {
  font-size: var(--app-text-sm);
  color: var(--td-text-color-secondary);
  padding: 10px 12px;
  background: var(--td-bg-color-secondarycontainer);
  border-radius: var(--td-radius-medium);
  border-left: 2px solid var(--td-component-border);
}

/* Sections */
.kp-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.kp-section-title {
  font-size: var(--app-text-xs);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--td-text-color-secondary);
}

.kp-section-desc {
  margin: 4px 0 8px;
  font-size: var(--app-text-sm);
  line-height: 1.5;
  color: var(--td-text-color-placeholder);
}

.kp-section-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.kp-section-action {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  border: 1px solid var(--td-component-border);
  background: var(--td-bg-color-container);
  color: var(--td-text-color-secondary);
  font-size: var(--app-text-xs);
  padding: 3px 8px;
  border-radius: var(--td-radius-default);
  cursor: pointer;
  transition: background var(--app-motion-fast) ease, color var(--app-motion-fast) ease, border-color var(--app-motion-fast) ease;
}

.kp-section-action:hover {
  background: var(--td-brand-color);
  color: var(--td-text-color-anti);
  border-color: var(--td-brand-color);
}

.kp-section-action:hover :deep(.t-icon) {
  color: var(--td-text-color-anti);
}

/* KV grid — TDesign description list aesthetic. White card on the gray
   page bg, soft 1px row separators, key/label color hierarchy. */
.kp-kv {
  display: flex;
  flex-direction: column;
  border: 1px solid var(--td-component-stroke);
  border-radius: var(--td-radius-medium);
  background: var(--td-bg-color-container);
  overflow: hidden;
}

.kp-kv-row {
  display: grid;
  grid-template-columns: 130px 1fr;
  gap: 12px;
  align-items: center;
  font-size: var(--app-text-sm);
  min-width: 0;
  padding: 8px 12px;
  background: var(--td-bg-color-container);
}

.kp-kv-row+.kp-kv-row {
  border-top: 1px solid var(--td-bg-color-secondarycontainer);
}

.kp-kv-row-multiline {
  align-items: flex-start;
}

.kp-kv-key {
  color: var(--td-text-color-secondary);
  font-size: var(--app-text-xs);
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.kp-kv-val {
  color: var(--td-text-color-primary);
  overflow-wrap: anywhere;
  word-break: break-word;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.kp-kv-truncate {
  overflow: hidden;
}

.kp-kv-truncate .kp-kv-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
  flex: 1;
}

.kp-kv-multiline {
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: stretch;
}

.kp-kv-scalar {
  font-size: var(--app-text-sm);
}

.kp-kv-running {
  color: var(--td-warning-color);
  font-style: italic;
  font-family: var(--app-font-family);
  font-size: var(--app-text-xs);
}

.kp-kv-tag-live {
  display: inline-block;
  margin-left: 6px;
  padding: 0 6px;
  font-size: 9px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  background: var(--td-warning-color-light);
  color: var(--td-warning-color);
  border-radius: var(--td-radius-small);
  font-family: var(--app-font-family);
}

.kp-kv-copy {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border: none;
  background: transparent;
  color: var(--td-text-color-placeholder);
  cursor: pointer;
  border-radius: var(--td-radius-small);
  flex-shrink: 0;
}

.kp-kv-copy:hover {
  background: var(--td-bg-color-container-hover);
  color: var(--td-brand-color);
}

.kp-mono {
  font-family: var(--app-font-family-mono);
  font-size: var(--app-text-xs);
  letter-spacing: 0;
}

.kp-bool-true {
  color: var(--td-success-color);
  font-weight: 500;
}

.kp-bool-false {
  color: var(--td-error-color);
  font-weight: 500;
}

.kp-kv-collapsible {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

/* Same vertical layout as collapsible, but with no toggle button —
   used for short payloads that auto-expand inline. */
.kp-kv-inline {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.kp-kv-summary-static {
  color: var(--td-text-color-placeholder);
  font-size: var(--app-text-xs);
}

.kp-kv-toggle {
  border: none;
  background: transparent;
  padding: 0;
  text-align: left;
  cursor: pointer;
  display: flex;
  align-items: baseline;
  gap: 8px;
  color: var(--td-text-color-secondary);
  font-size: var(--app-text-sm);
  flex-wrap: wrap;
}

.kp-kv-summary {
  color: var(--td-text-color-secondary);
}

.kp-kv-toggle-label {
  font-size: var(--app-text-xs);
  color: var(--td-brand-color);
  font-weight: 500;
}

.kp-kv-toggle:hover .kp-kv-toggle-label {
  text-decoration: underline;
}

/* Stage breakdown table inside root overview */
.kp-breakdown {
  display: flex;
  flex-direction: column;
  gap: 6px;
  border: 1px solid var(--td-component-stroke);
  border-radius: var(--td-radius-medium);
  background: var(--td-bg-color-container);
  padding: 10px 12px;
}

.kp-breakdown-row {
  display: grid;
  grid-template-columns: 110px 1fr 64px;
  gap: 10px;
  align-items: center;
  font-size: var(--app-text-sm);
}

.kp-breakdown-label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--td-text-color-primary);
}

.kp-breakdown-track {
  position: relative;
  height: 6px;
  background: var(--td-bg-color-secondarycontainer);
  border-radius: var(--td-radius-small);
  overflow: hidden;
}

.kp-breakdown-bar {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  border-radius: var(--td-radius-small);
  transition: width 800ms cubic-bezier(0.2, 0.8, 0.2, 1);
}

.kp-breakdown-row.kp-breakdown-pending .kp-breakdown-bar {
  display: none;
}

.kp-breakdown-dur {
  text-align: right;
  color: var(--td-text-color-secondary);
}

/* Error block in overview */
.kp-error-block {
  border: 1px solid var(--td-error-color-3);
  border-radius: var(--td-radius-medium);
  background: var(--td-error-color-light);
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.kp-error-head {
  display: flex;
  align-items: center;
  gap: 8px;
}

.kp-error-glyph {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  background: var(--td-error-color);
  color: var(--td-text-color-anti);
  border-radius: 50%;
  font-size: var(--app-text-xs);
  font-weight: 700;
  flex-shrink: 0;
}

.kp-error-title {
  font-weight: 600;
  font-size: var(--app-text-sm);
  color: var(--td-error-color);
}

.kp-error-code {
  margin-left: auto;
  font-size: var(--app-text-2xs);
  background: var(--td-error-color);
  color: var(--td-text-color-anti);
  padding: 1px 6px;
  border-radius: var(--td-radius-small);
}

.kp-error-msg {
  margin: 0;
  font-size: var(--app-text-xs);
  color: var(--td-text-color-secondary);
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 160px;
  overflow: auto;
  padding: 8px 10px;
  background: var(--td-bg-color-container);
  border-radius: var(--td-radius-default);
  border: 1px solid var(--td-component-stroke);
}

/* JSON viewer */
.kp-json {
  margin: 0;
  padding: 10px 12px;
  background: var(--td-bg-color-secondarycontainer);
  border: 1px solid var(--td-component-stroke);
  border-radius: var(--td-radius-medium);
  max-height: 360px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: var(--app-text-xs);
  color: var(--td-text-color-primary);
  line-height: 1.6;
}

.kp-json-large {
  max-height: 480px;
}

/* ============== COMPACT MODE (untouched) ============== */
.kp-compact {
  max-width: 320px;
  height: auto;
}

.kp-compact-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.kp-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--td-text-color-placeholder);
  display: inline-block;
}

.kp-compact-caption {
  margin-top: 4px;
  font-size: var(--app-text-sm);
  color: var(--td-text-color-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.kp-stage-emph {
  color: var(--td-brand-color);
  font-weight: 600;
}

.kp-proccfg-pop {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 2px 0;
  max-width: 320px;
}

.kp-proccfg-title {
  font-weight: 600;
  font-size: var(--app-text-md);
  color: var(--td-text-color-primary);
  margin-bottom: 2px;
}

.kp-proccfg-line {
  font-size: var(--app-text-sm);
  line-height: 1.6;
  color: var(--td-text-color-secondary);
  word-break: break-word;
}
</style>