* {
  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 (matches settings page pattern) -- */
#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;
}

/* -- Sidebar -- */
.sidebar {
  width: 300px;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  background: var(--surface);
}

.sidebar-pane {
  flex: 1 1 50%;
  min-height: 0;
  overflow-y: auto;
  padding: 10px 14px;
}

.sidebar-pane + .sidebar-pane {
  border-top: 1px solid var(--border);
}

.sidebar-title {
  font-size: 10px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.6px;
  margin-bottom: 10px;
}

.sidebar-subtitle {
  font-size: 10px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.6px;
  margin-bottom: 8px;
}

/* -- Session list -- */
.session-list {
  list-style: none;
  padding: 0;
  margin: 0 0 4px 0;
}

.session-item {
  padding: 8px 10px;
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  background: transparent;
  cursor: pointer;
  margin-bottom: 4px;
  transition: all 0.12s;
}

.session-item:hover {
  background: var(--accent-soft);
  border-color: var(--border);
}

.session-item.active {
  background: var(--accent-soft);
  border-color: rgba(37, 99, 235, 0.3);
}

.session-label {
  font-size: 13px;
  font-weight: 500;
  line-height: 1.3;
  word-break: break-word;
  color: var(--text);
}

.session-item.active .session-label {
  color: var(--accent);
}

.session-meta {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

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

/* -- Turn list -- */
.turn-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.turn-item {
  padding: 10px 10px;
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  background: transparent;
  cursor: pointer;
  margin-bottom: 6px;
  transition: all 0.12s;
}

.turn-item:hover {
  background: var(--accent-soft);
  border-color: var(--border);
}

.turn-item.active {
  background: var(--accent-soft);
  border-color: rgba(37, 99, 235, 0.3);
}

.turn-label {
  font-size: 13px;
  font-weight: 500;
  line-height: 1.3;
  word-break: break-word;
  color: var(--text);
}

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

.turn-item .turn-meta {
  font-size: 12px;
  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);
}

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

.detail .hint {
  color: var(--text-muted);
  line-height: 1.4;
}

/* -- 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;
}
