/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 14px; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: #f4f6f9;
    color: #2d3748;
    display: flex;
    min-height: 100vh;
}

a { color: #4a6cf7; text-decoration: none; }
a:hover { text-decoration: underline; }

/* ===== Sidebar ===== */
.sidebar {
    width: 220px;
    background: #1e293b;
    color: #e2e8f0;
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0; left: 0; bottom: 0;
    z-index: 10;
}
.sidebar-header {
    padding: 1.5rem 1.25rem 1rem;
    border-bottom: 1px solid #334155;
}
.sidebar-header h2 { font-size: 1.25rem; color: #fff; }
.sidebar-header .subtitle { font-size: 0.75rem; color: #94a3b8; text-transform: uppercase; letter-spacing: 0.05em; }

.nav-links { list-style: none; padding: 0.75rem 0; }
.nav-links li a {
    display: block;
    padding: 0.6rem 1.25rem;
    color: #cbd5e1;
    font-size: 0.9rem;
    transition: background 0.15s, color 0.15s;
}
.nav-links li a:hover { background: #334155; color: #fff; text-decoration: none; }
.nav-links li a.active { background: #4a6cf7; color: #fff; }

/* ===== Content ===== */
.content {
    margin-left: 220px;
    flex: 1;
    padding: 1.5rem 2rem;
    max-width: 1200px;
}

.page-header { margin-bottom: 1.5rem; }
.page-header h1 { font-size: 1.5rem; font-weight: 600; color: #1e293b; }

/* ===== Stats Grid ===== */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
    margin-bottom: 1.5rem;
}
.stat-card {
    background: #fff;
    border-radius: 8px;
    padding: 1.25rem;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}
.stat-label { font-size: 0.8rem; color: #64748b; text-transform: uppercase; letter-spacing: 0.03em; margin-bottom: 0.25rem; }
.stat-value { font-size: 1.75rem; font-weight: 700; color: #1e293b; }

/* ===== Cards ===== */
.card {
    background: #fff;
    border-radius: 8px;
    padding: 1.25rem;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
    margin-bottom: 1.25rem;
}
.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 1rem;
}
.card-header h3 { font-size: 1.1rem; font-weight: 600; }

.link-small { font-size: 0.8rem; }

/* ===== Tables ===== */
.data-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.data-table thead th {
    text-align: left;
    padding: 0.6rem 0.75rem;
    border-bottom: 2px solid #e2e8f0;
    color: #64748b;
    font-weight: 600;
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}
.data-table tbody td {
    padding: 0.6rem 0.75rem;
    border-bottom: 1px solid #f1f5f9;
    vertical-align: top;
}
.data-table tbody tr:hover { background: #f8fafc; }

td.truncate { max-width: 250px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ===== Badges ===== */
.badge {
    display: inline-block;
    padding: 0.15rem 0.5rem;
    border-radius: 9999px;
    font-size: 0.72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}
.badge-completed { background: #dcfce7; color: #166534; }
.badge-failed { background: #fee2e2; color: #991b1b; }
.badge-in_progress { background: #fef9c3; color: #854d0e; }
.badge-error { background: #fee2e2; color: #991b1b; }

/* ===== Filters ===== */
.filters-bar { margin-bottom: 1.25rem; }
.filter-form {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
    align-items: center;
}
.filter-form input,
.filter-form select {
    padding: 0.45rem 0.75rem;
    border: 1px solid #cbd5e1;
    border-radius: 6px;
    font-size: 0.85rem;
    background: #fff;
    color: #334155;
}
.filter-form input:focus,
.filter-form select:focus { outline: none; border-color: #4a6cf7; box-shadow: 0 0 0 2px rgba(74,108,247,0.15); }

/* ===== Buttons ===== */
.btn {
    display: inline-block;
    padding: 0.45rem 1rem;
    border-radius: 6px;
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    border: none;
    transition: background 0.15s;
}
.btn-primary { background: #4a6cf7; color: #fff; }
.btn-primary:hover { background: #3b5de7; text-decoration: none; }
.btn-secondary { background: #e2e8f0; color: #334155; }
.btn-secondary:hover { background: #cbd5e1; text-decoration: none; }

/* ===== Pagination ===== */
.pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    margin-top: 1rem;
    font-size: 0.85rem;
    color: #64748b;
}

/* ===== Trace Detail ===== */
.trace-summary { display: flex; flex-direction: column; gap: 0.5rem; }
.trace-meta { display: flex; gap: 1rem; align-items: center; flex-wrap: wrap; font-size: 0.9rem; }
.trace-id { font-size: 0.8rem; color: #64748b; font-family: monospace; }
.trace-time { font-size: 0.8rem; color: #94a3b8; }

.code-block {
    background: #1e293b;
    color: #e2e8f0;
    padding: 1rem;
    border-radius: 6px;
    font-family: "SF Mono", "Fira Code", "Cascadia Code", monospace;
    font-size: 0.82rem;
    overflow-x: auto;
    white-space: pre-wrap;
    word-break: break-word;
    max-height: 300px;
    overflow-y: auto;
}

/* ===== Span Tree (hierarchical) ===== */
.span-tree { padding: 0.5rem 0; }

/* Each tree node: connector on the left, body on the right */
.tree-node {
    display: flex;
    position: relative;
    min-height: 2rem;
}

/* Vertical + horizontal connector lines */
.tree-connector {
    position: relative;
    width: 24px;
    flex-shrink: 0;
}
/* Vertical line running the full height */
.tree-connector .tree-line-v {
    position: absolute;
    top: 0;
    left: 11px;
    bottom: 0;
    width: 2px;
    background: #cbd5e1;
}
/* Last child: stop the vertical line at the dot */
.tree-node--last > .tree-connector .tree-line-v {
    bottom: auto;
    height: 14px;
}
/* Colored dot on the connector */
.tree-dot {
    position: absolute;
    top: 10px;
    left: 6px;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    border: 2px solid #fff;
    z-index: 1;
}
.tree-dot--agent { background: #4a6cf7; }
.tree-dot--llm   { background: #8b5cf6; }
.tree-dot--tool  { background: #f59e0b; }

/* Body: everything to the right of the connector */
.tree-body {
    flex: 1;
    min-width: 0;
    padding-bottom: 0.5rem;
}

/* Nested children indentation */
.tree-children {
    margin-left: 0;
    padding-left: 0;
}

/* ── Span header row ── */
.span-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.88rem;
    font-weight: 500;
    cursor: pointer;
    padding: 0.25rem 0;
    border-radius: 4px;
    transition: background 0.1s;
}
.span-header:hover { background: #f8fafc; }

/* Span type badge (colored pill) */
.span-type-badge {
    display: inline-block;
    padding: 0.1rem 0.45rem;
    border-radius: 4px;
    font-size: 0.68rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    flex-shrink: 0;
}
.span-type-badge--agent { background: #dbeafe; color: #1d4ed8; }
.span-type-badge--llm   { background: #ede9fe; color: #6d28d9; }
.span-type-badge--tool  { background: #fef3c7; color: #92400e; }

.span-name { color: #1e293b; }
.span-label { color: #64748b; font-weight: 400; }
.span-duration { font-size: 0.78rem; color: #94a3b8; }

/* ── Waterfall timeline bar ── */
.waterfall-bar-container {
    position: relative;
    height: 6px;
    background: #f1f5f9;
    border-radius: 3px;
    margin: 0.15rem 0 0.35rem;
    overflow: hidden;
}
.waterfall-bar {
    position: absolute;
    top: 0;
    height: 100%;
    border-radius: 3px;
    min-width: 2px;
}
.waterfall-bar--agent { background: #4a6cf7; }
.waterfall-bar--llm   { background: #8b5cf6; }
.waterfall-bar--tool  { background: #f59e0b; }

/* ── Expandable span details ── */
.span-details {
    margin-top: 0.35rem;
    padding-left: 0;
    font-size: 0.82rem;
    color: #475569;
}
.detail-row { margin-bottom: 0.2rem; }
.detail-row strong { color: #334155; }
.detail-row.error { color: #dc2626; }

/* ===== Chain Controls ===== */
.chain-controls {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
}
.btn-sm { padding: 0.25rem 0.6rem; font-size: 0.78rem; }

/* ===== Toggle Arrow ===== */
.toggle-arrow, .section-arrow {
    display: inline-block;
    font-size: 0.65rem;
    transition: transform 0.15s;
    color: #94a3b8;
    width: 0.8rem;
    flex-shrink: 0;
}
.toggle-arrow.expanded, .section-arrow.expanded {
    transform: rotate(90deg);
}

/* ===== Chain Header Inline Stats ===== */
.token-summary {
    font-size: 0.72rem;
    color: #64748b;
    font-family: monospace;
    margin-left: 0.25rem;
}
.cost-summary {
    font-size: 0.72rem;
    color: #059669;
    font-weight: 600;
    font-family: monospace;
}
.cost-inline {
    font-size: 0.85em;
    color: #059669;
    font-family: monospace;
}

/* ===== Detail Sections (expandable) ===== */
.detail-section {
    margin: 0.5rem 0;
    border: 1px solid #e2e8f0;
    border-radius: 6px;
    overflow: hidden;
}
.detail-section-header {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.45rem 0.75rem;
    background: #f8fafc;
    font-size: 0.82rem;
    font-weight: 600;
    color: #334155;
    cursor: pointer;
    user-select: none;
    transition: background 0.1s;
}
.detail-section-header:hover {
    background: #f1f5f9;
}
.detail-section-body {
    padding: 0.5rem 0.75rem;
    border-top: 1px solid #e2e8f0;
    font-size: 0.82rem;
}
.detail-section-body .code-block {
    margin: 0.25rem 0;
    max-height: 500px;
}

/* ===== Key-Value Tables ===== */
.detail-kv-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.82rem;
}
.detail-kv-table td {
    padding: 0.3rem 0.5rem;
    border-bottom: 1px solid #f1f5f9;
}
.detail-kv-table td:first-child {
    color: #64748b;
    width: 180px;
    white-space: nowrap;
}
.detail-kv-table td:last-child {
    color: #1e293b;
    font-family: monospace;
    font-size: 0.8rem;
}
.detail-kv-table .mono { font-family: monospace; font-size: 0.78rem; word-break: break-all; }

/* ===== Tool Call Cards ===== */
.tool-call-card {
    border: 1px solid #e2e8f0;
    border-radius: 6px;
    margin: 0.4rem 0;
    overflow: hidden;
}
.tool-call-card.tool-call-error {
    border-color: #fecaca;
}
.tool-call-header {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.4rem 0.75rem;
    background: #fafbfc;
    font-size: 0.8rem;
    cursor: pointer;
    user-select: none;
}
.tool-call-header:hover { background: #f1f5f9; }
.tool-call-error .tool-call-header { background: #fff5f5; }
.tool-meta {
    font-size: 0.7rem;
    color: #94a3b8;
    font-family: monospace;
    margin-left: auto;
}
.tool-subsection { margin: 0.35rem 0; }
.tool-subsection-label {
    font-size: 0.75rem;
    font-weight: 600;
    color: #64748b;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    margin-bottom: 0.2rem;
}
.code-block-sm {
    max-height: 200px;
    font-size: 0.78rem;
}

/* ===== Tool Detail Cards ===== */
.tool-detail-card {
    border: 1px solid #e2e8f0;
    border-radius: 6px;
    margin: 0.4rem 0;
    overflow: hidden;
}
.tool-detail-header {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.4rem 0.75rem;
    background: #fafbfc;
    font-size: 0.8rem;
    cursor: pointer;
    user-select: none;
}
.tool-detail-header:hover { background: #f1f5f9; }
.tool-detail-name {
    font-weight: 600;
    color: #1e293b;
    font-family: "SF Mono", SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace;
    font-size: 0.82rem;
}
.tool-description-preview {
    font-size: 0.75rem;
    color: #94a3b8;
    margin-left: 0.5rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 400px;
}
.tool-description-full {
    font-size: 0.82rem;
    color: #475569;
    line-height: 1.4;
    padding: 0.25rem 0;
}

/* ===== Tool Name Chips ===== */
.tool-name-list {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
    padding: 0.25rem 0;
}
.tool-name-chip {
    display: inline-block;
    background: #e2e8f0;
    color: #334155;
    font-family: "SF Mono", SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace;
    font-size: 0.78rem;
    padding: 0.2rem 0.55rem;
    border-radius: 4px;
    border: 1px solid #cbd5e1;
}

/* ===== Usage Chart ===== */
.usage-chart-container {
    position: relative;
    width: 100%;
    height: 240px;
    margin-top: 0.5rem;
}
.usage-chart-container canvas {
    width: 100% !important;
    height: 100% !important;
}
.chart-controls {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
}
.chart-controls select {
    padding: 0.3rem 0.6rem;
    border: 1px solid #cbd5e1;
    border-radius: 4px;
    font-size: 0.85rem;
    background: #fff;
    color: #334155;
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
    .sidebar { width: 180px; }
    .content { margin-left: 180px; padding: 1rem; }
    .stats-grid { grid-template-columns: repeat(2, 1fr); }
}
