* {
  box-sizing: border-box;
}

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  margin: 0;
  font-family: var(--font-ui);
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
}

/* -- Top bar -- */
#trace-topbar {
  flex: 0 0 auto;
  z-index: 10;
  height: 60px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 20px;
  gap: 0;
  box-shadow: var(--shadow-sm);
}

.trace-back {
  color: var(--text-muted);
  font-size: 12px;
  text-decoration: none;
  display: flex;
  align-items: center;
  padding: 0 12px 0 0;
  flex-shrink: 0;
}

.trace-back:hover {
  color: var(--accent);
}

.trace-brand {
  color: var(--text);
  font-size: 15px;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 7px;
  flex-shrink: 0;
  padding-right: 12px;
}

.trace-brand-icon {
  background: linear-gradient(135deg, var(--accent), #7c3aed);
  width: 30px;
  height: 30px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: #fff;
}

.btn-trace-refresh {
  padding: 5px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-dim);
  cursor: pointer;
  font-size: 12px;
  font-family: var(--font-ui);
  font-weight: 500;
  transition: all 0.12s;
}

.btn-trace-refresh:hover {
  background: var(--bg);
  color: var(--text);
  border-color: var(--text-dim);
}

.trace-status {
  font-size: 12px;
  color: var(--text-muted);
  min-height: 18px;
}

.trace-status.error {
  color: var(--red);
}

/* -- Main layout -- */
.container {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
}

/* -- Left rail (mode tabs) -- */
.left-rail {
  flex: 0 0 58px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px 0 10px;
  gap: 4px;
}

.rail-sidebar-toggle {
  width: 32px;
  height: 28px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: var(--text-muted);
  transition: all 0.12s;
  padding: 0;
  margin-bottom: 12px;
}

.rail-sidebar-toggle:hover {
  background: var(--bg);
  color: var(--text);
  border-color: var(--text-dim);
}

.rail-tab {
  width: 50px;
  padding: 10px 0;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  transition: all 0.12s;
  color: var(--text-muted);
}

.rail-tab:hover {
  background: var(--accent-soft);
  color: var(--text);
}

.rail-tab.active {
  background: var(--accent-soft);
  color: var(--accent);
}

.rail-icon {
  font-size: 18px;
  line-height: 1;
}

.rail-label {
  font-size: 11px;
  font-weight: 700;
  line-height: 1.25;
  text-align: center;
}

/* -- Sidebar -- */
.sidebar {
  width: 520px;
  min-width: 0;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  background: var(--bg);
  transition: width 0.2s ease, min-width 0.2s ease;
  overflow: hidden;
  flex-shrink: 0;
}

.sidebar.collapsed {
  flex: 0 0 0 !important;
  width: 0 !important;
  min-width: 0 !important;
  max-width: 0 !important;
  margin: 0 !important;
  padding: 0 !important;
  border: 0 !important;
  overflow: hidden !important;
}

.sidebar-header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

.sidebar-title {
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  text-align: center;
}

/* -- Session dropdown + turn list -- */
.session-select-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.session-select {
  flex: 1;
  min-width: 0;
  padding: 6px 8px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 12px;
  font-family: var(--font-ui);
  cursor: pointer;
  outline: none;
  transition: border-color 0.12s;
}

.session-select:focus {
  border-color: var(--accent);
}

.session-actions {
  flex-shrink: 0;
  display: flex;
  gap: 4px;
  align-items: center;
}

.turn-list {
  list-style: none;
  padding: 8px;
  margin: 0;
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
}

/* -- Trace toggle button (record / play) -- */
.btn-trace-toggle,
.btn-trace-delete {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: none;
  background: transparent;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: background 0.12s;
}

.btn-trace-toggle:hover,
.btn-trace-delete:hover {
  background: var(--border-light);
}

.btn-trace-toggle .icon-recording {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid #333;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-trace-toggle .icon-recording::after {
  content: '';
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #dc2626;
}

.btn-trace-toggle .icon-play {
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 5px 0 5px 9px;
  border-color: transparent transparent transparent #16a34a;
  margin-left: 2px;
}

.btn-trace-delete .icon-trash {
  width: 12px;
  height: 13px;
  position: relative;
  border: 1.5px solid var(--text-muted);
  border-top: none;
  border-radius: 0 0 2px 2px;
}

.btn-trace-delete .icon-trash::before {
  content: '';
  position: absolute;
  top: -4px;
  left: -3px;
  width: 16px;
  height: 0;
  border-top: 1.5px solid var(--text-muted);
}

.btn-trace-delete .icon-trash::after {
  content: '';
  position: absolute;
  top: -7px;
  left: 2px;
  width: 6px;
  height: 0;
  border-top: 1.5px solid var(--text-muted);
}

.btn-trace-delete:hover .icon-trash,
.btn-trace-delete:hover .icon-trash::before,
.btn-trace-delete:hover .icon-trash::after {
  border-color: #dc2626;
}

.btn-trace-delete[disabled] {
  opacity: 0.3;
  cursor: not-allowed;
}

.session-capturing {
  color: var(--green);
  font-size: 10px;
}

/* -- Delete confirmation dialog -- */
.dialog-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
}

.dialog-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px 24px;
  max-width: 360px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
}

.dialog-text {
  font-size: 14px;
  color: var(--text);
  margin: 0 0 16px;
  line-height: 1.5;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.btn-dialog {
  padding: 6px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  font-size: 13px;
  font-family: var(--font-ui);
  cursor: pointer;
  transition: all 0.12s;
}

.btn-cancel {
  background: var(--surface);
  color: var(--text-dim);
}

.btn-cancel:hover {
  background: var(--bg);
}

.btn-danger {
  background: #dc2626;
  color: #fff;
  border-color: #dc2626;
}

.btn-danger:hover {
  background: #b91c1c;
}

/* -- Turn items (inside session tree) -- */
.turn-item {
  padding: 7px 10px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--surface);
  cursor: pointer;
  margin-bottom: 4px;
  transition: all 0.12s;
  box-shadow: var(--shadow-sm);
}

.turn-item:hover {
  background: var(--accent-soft);
  border-color: rgba(37, 99, 235, 0.25);
  transform: translateY(-1px);
}

.turn-item.active {
  background: var(--accent-soft);
  border-color: rgba(37, 99, 235, 0.45);
  box-shadow: 0 0 0 1px rgba(37, 99, 235, 0.12);
}

.turn-label {
  font-size: 12px;
  font-weight: 500;
  line-height: 1.3;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.turn-error-icon {
  color: var(--red);
  font-weight: 700;
}

.turn-item .turn-meta {
  font-size: 11px;
  color: var(--text-muted);
}

.empty {
  padding: 12px;
  border-radius: var(--radius-md);
  border: 1px dashed var(--border);
  color: var(--text-muted);
  text-align: center;
  font-size: 13px;
}

/* -- Content area -- */
.content {
  flex: 1;
  overflow: auto;
  padding: 14px 16px;
  background: var(--bg);
  display: flex;
  flex-direction: column;
}

.detail {
  flex: 1 1 auto;
  min-height: 0;
}

.raw-log-panel {
  margin-top: 14px;
}

.detail .hint {
  color: var(--text-muted);
  line-height: 1.4;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100%;
  text-align: center;
}

/* -- Meta grid -- */
.meta {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 16px;
  padding: 12px 14px;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  background: var(--surface);
  margin-bottom: 14px;
  box-shadow: var(--shadow-sm);
}

/* -- Sections -- */
.section {
  margin-bottom: 18px;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}

.section-title {
  font-weight: 600;
  font-size: 13px;
  padding: 10px 14px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  color: var(--text);
}

details {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 8px 12px;
  margin-bottom: 10px;
  background: var(--surface);
}

details.subdetails {
  margin: 8px 0 10px 0;
  padding: 6px 10px;
  background: var(--bg);
}

summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--text);
}

.blocks {
  padding: 12px 14px;
}

.blocks > :last-child {
  margin-bottom: 0;
}

/* -- Code blocks -- */
.code {
  white-space: pre-wrap;
  word-break: break-word;
  background: #f6f8fa;
  padding: 12px 12px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.35;
  margin: 10px 0 0 0;
  color: var(--text);
}

/* -- Tags -- */
.taglist {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.tag-wrap {
  display: inline-block;
  scroll-margin-top: 140px;
}

.tag-btn {
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
  font-size: 12px;
  transition: all 0.12s;
}

.tag-btn:hover {
  background: var(--accent-soft);
  border-color: rgba(37, 99, 235, 0.25);
  color: var(--accent);
}

.tag-detail {
  display: none;
  margin-top: 8px;
}

/* -- Message items -- */
.msg-item {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 10px 12px;
  margin-bottom: 12px;
  box-shadow: var(--shadow-sm);
}

.msg-role {
  font-weight: 700;
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 8px;
  text-transform: uppercase;
}

/* -- Message collapsible blocks -- */
.msg-details {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  margin-bottom: 8px;
  background: var(--surface);
}

.msg-summary {
  cursor: pointer;
  padding: 8px 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: normal;
  color: var(--text);
  list-style: none;
}

.msg-summary::-webkit-details-marker { display: none; }
.msg-summary::marker { display: none; }

.msg-details[open] > .msg-summary {
  border-bottom: 1px solid var(--border-light);
}

.msg-details > .msg-summary::before {
  content: "\25B6";
  font-size: 9px;
  opacity: 0.5;
  transition: transform 0.15s;
}
.msg-details[open] > .msg-summary::before {
  transform: rotate(90deg);
}

.msg-role-badge {
  font-weight: 700;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* Role color variants */
.msg-role-system    > summary .msg-role-badge { background: rgba(150, 120, 220, 0.1);  border-color: rgba(150, 120, 220, 0.3); color: #7c5cbf; }
.msg-role-user      > summary .msg-role-badge { background: rgba(37, 99, 235, 0.08);   border-color: rgba(37, 99, 235, 0.25);  color: var(--accent); }
.msg-role-assistant > summary .msg-role-badge { background: rgba(5, 150, 105, 0.08);   border-color: rgba(5, 150, 105, 0.25);  color: var(--green); }
.msg-role-tool      > summary .msg-role-badge,
.msg-role-function  > summary .msg-role-badge { background: rgba(217, 119, 6, 0.08);   border-color: rgba(217, 119, 6, 0.25);  color: var(--yellow); }

.msg-meta-hint {
  font-size: 11px;
  color: var(--text-muted);
}

.msg-body {
  padding: 10px 12px;
}

.msg-tool-calls {
  margin-top: 10px;
}

.msg-sub-label {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-muted);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.msg-meta-row {
  margin-top: 8px;
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.msg-meta-kv {
  font-size: 12px;
  color: var(--text-dim);
}

.msg-empty {
  font-size: 12px;
  color: var(--text-muted);
  font-style: italic;
}

/* -- Turn Header (tools/skills/tool_calls/tool_results) -- */
.turn-header {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 16px;
  padding: 12px 14px;
  border-radius: var(--radius-lg);
  border: 1px solid rgba(37, 99, 235, 0.15);
  background: var(--accent-soft);
}

.turn-header-block {
  scroll-margin-top: 140px;
}

.turn-header-title {
  font-weight: 700;
  font-size: 13px;
  margin-bottom: 8px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text);
}

.turn-header-count {
  font-weight: 600;
  font-size: 11px;
  padding: 1px 6px;
  border-radius: 999px;
  background: var(--border-light);
  color: var(--text-dim);
  margin-left: 4px;
}

/* Reference links from messages to header */
.msg-ref-link {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid rgba(37, 99, 235, 0.25);
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 12px;
  text-decoration: none;
  cursor: pointer;
  transition: background 0.15s;
}

.msg-ref-link:hover {
  background: rgba(37, 99, 235, 0.15);
}

.msg-ref-link-small {
  padding: 1px 6px;
  font-size: 11px;
  vertical-align: middle;
  margin-left: 4px;
}

.msg-ref-pill {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg);
  font-size: 12px;
  color: var(--text-dim);
}

.msg-ref-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

/* -- Nested tool_use / tool_result items in messages -- */
.msg-tool-uses {
  margin-top: 10px;
}

.tool-use-item,
.tool-result-item {
  border: 1px solid rgba(217, 119, 6, 0.2);
  border-radius: var(--radius-md);
  padding: 6px 10px;
  margin-bottom: 6px;
  background: rgba(217, 119, 6, 0.04);
}

.tool-use-item > summary,
.tool-result-item > summary {
  font-weight: normal;
}

.tool-use-signature {
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.4;
  color: #b45309;
  cursor: pointer;
  word-break: break-all;
}

.tool-use-item[open] > .tool-use-signature,
.tool-result-item[open] > .tool-use-signature {
  border-bottom: 1px solid rgba(217, 119, 6, 0.15);
  padding-bottom: 6px;
  margin-bottom: 4px;
}

/* Anchor highlight animation */
.anchor-highlight {
  animation: anchor-flash 1.5s ease-out;
}

@keyframes anchor-flash {
  0%   { background: rgba(37, 99, 235, 0.15); }
  100% { background: transparent; }
}

/* -- JSON Tree -- */
.json-tree {
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.4;
  background: #f6f8fa;
  padding: 12px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  margin: 10px 0 0 0;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-x: auto;
}

.json-key { color: #0550ae; }
.json-str { color: #a0522d; }
.json-num { color: #0a7e38; }
.json-bool { color: #2563eb; }
.json-null { color: #94a3b8; font-style: italic; }

.json-str-toggle {
  display: inline;
  vertical-align: top;
}

.json-str-collapsed {
  cursor: pointer;
}

.json-str-collapsed:hover .json-str {
  text-decoration: underline;
}

.json-str-expanded {
  display: block;
}

.json-str-collapse-btn {
  display: inline-block;
  cursor: pointer;
  font-size: 11px;
  color: var(--accent);
  opacity: 0.7;
  margin-top: 4px;
}

.json-str-collapse-btn:hover {
  opacity: 1;
  text-decoration: underline;
}

.json-str-block {
  white-space: pre-wrap;
  word-break: break-word;
  background: var(--bg);
  padding: 10px 12px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  margin: 6px 0;
  color: var(--text);
  font-size: 12px;
  line-height: 1.35;
  max-height: 400px;
  overflow-y: auto;
}

/* -- Child session (sub-agent) tree nodes -- */

.child-session-item {
  margin: 4px 0 4px 10px;
  border-left: 2px solid var(--accent);
  padding-left: 6px;
  list-style: none;
}

.child-session-row {
  display: flex;
  align-items: baseline;
  gap: 6px;
  padding: 7px 10px;
  cursor: pointer;
  border-radius: var(--radius-md);
  font-size: 12px;
  font-weight: 500;
  color: var(--text);
}

.child-session-row:hover {
  background: var(--accent-soft);
}

.child-session-arrow {
  font-size: 9px;
  transition: transform 0.15s;
  color: var(--text-muted);
}

.child-session-item.expanded > .child-session-row > .child-session-arrow {
  transform: rotate(90deg);
}

.child-session-label {
  word-break: break-word;
}

.child-session-turns {
  display: none;
  list-style: none;
  padding: 2px 0 2px 12px;
  margin: 0;
}

.child-session-item.expanded > .child-session-turns {
  display: block;
}

.turn-item.child-turn {
  font-size: 11px;
  padding: 5px 8px;
  opacity: 0.9;
}

