:root {
  --bg: #f7f6f3;
  --surface: #ffffff;
  --surface-2: #f0eee9;
  --border: #e2dfd8;
  --text: #1f1e1c;
  --muted: #74716b;
  --accent: #c2603c;
  --accent-soft: #f6e6de;
  --user-bg: #eef2f8;
  --user-border: #cdd7e6;
  --code-bg: #f3f1ec;
  --pre-bg: #1e1e1c;
  --pre-text: #e8e6e1;
  --ok: #3f7d4e;
  --err: #b33a3a;
  --err-soft: #fbeaea;
  --add: #e3f3e6;
  --del: #fbe7e7;
  --shadow: 0 1px 2px rgba(0, 0, 0, .05);
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1a1a19;
    --surface: #242422;
    --surface-2: #2c2c2a;
    --border: #3a3936;
    --text: #e9e7e2;
    --muted: #9b978f;
    --accent: #e08a62;
    --accent-soft: #3d2c24;
    --user-bg: #232a35;
    --user-border: #36445a;
    --code-bg: #30302d;
    --pre-bg: #141413;
    --pre-text: #e8e6e1;
    --ok: #7cc28e;
    --err: #ef8a8a;
    --err-soft: #3a2424;
    --add: #1f3324;
    --del: #3d2323;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #1a1a19;
  --surface: #242422;
  --surface-2: #2c2c2a;
  --border: #3a3936;
  --text: #e9e7e2;
  --muted: #9b978f;
  --accent: #e08a62;
  --accent-soft: #3d2c24;
  --user-bg: #232a35;
  --user-border: #36445a;
  --code-bg: #30302d;
  --pre-bg: #141413;
  --pre-text: #e8e6e1;
  --ok: #7cc28e;
  --err: #ef8a8a;
  --err-soft: #3a2424;
  --add: #1f3324;
  --del: #3d2323;
  color-scheme: dark;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.65 var(--sans);
  -webkit-font-smoothing: antialiased;
}

code, pre { font-family: var(--mono); font-size: 13px; }

a { color: var(--accent); }

.muted { color: var(--muted); }
.error { color: var(--err); }

/* ---------- top bar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand { font-weight: 650; margin-right: auto; white-space: nowrap; }

.btn, .icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 8px;
  padding: 5px 12px;
  font: inherit;
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
}

.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.icon-btn { width: 34px; padding: 5px 0; }
.btn:hover, .icon-btn:hover { filter: brightness(.96); }

#search {
  width: 220px;
  max-width: 100%;
  padding: 5px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 14px;
}

#search-count { font-size: 13px; min-width: 0; }

.toggles { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 13px; }
.toggles label { display: inline-flex; align-items: center; gap: 4px; cursor: pointer; white-space: nowrap; }

#toggle-sidebar { display: none; }

/* ---------- drop zone ---------- */

#dropzone {
  display: grid;
  place-items: center;
  min-height: calc(100vh - var(--top, 56px));
  padding: 24px 16px;
}

.drop-card {
  max-width: 560px;
  width: 100%;
  text-align: center;
  padding: 40px 28px;
  border: 2px dashed var(--border);
  border-radius: 16px;
  background: var(--surface);
}

.drop-card h1 { font-size: 20px; margin: 8px 0; }
.drop-card p { margin: 6px 0; }
.drop-icon { font-size: 40px; color: var(--accent); }

#drag-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  background: rgba(194, 96, 60, .18);
  border: 3px dashed var(--accent);
  font-size: 22px;
  font-weight: 600;
  pointer-events: none;
}
#drag-overlay[hidden] { display: none; }

/* ---------- layout ---------- */

#viewer {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  min-height: calc(100vh - var(--top, 56px));
}
#viewer[hidden] { display: none; }

#sidebar {
  position: sticky;
  top: var(--top, 56px);
  align-self: start;
  height: calc(100vh - var(--top, 56px));
  overflow-y: auto;
  padding: 16px;
  border-right: 1px solid var(--border);
  background: var(--surface);
  font-size: 13px;
}

#transcript {
  width: 100%;
  max-width: 920px;
  margin: 0 auto;
  padding: 20px 24px 120px;
}

/* ---------- session meta ---------- */

.meta-title { font-size: 16px; font-weight: 650; line-height: 1.4; margin: 0 0 10px; }
.meta-grid { display: grid; grid-template-columns: auto 1fr; gap: 3px 10px; margin: 0; }
.meta-grid dt { color: var(--muted); white-space: nowrap; }
.meta-grid dd { margin: 0; overflow-wrap: anywhere; }

.toc-title {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  margin: 20px 0 6px;
}

#toc a {
  display: block;
  padding: 5px 8px;
  margin: 0 -8px;
  border-radius: 6px;
  color: var(--text);
  text-decoration: none;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#toc a:hover { background: var(--surface-2); }
#toc a.active { background: var(--accent-soft); color: var(--accent); }
#toc a .n { color: var(--muted); margin-right: 6px; font-variant-numeric: tabular-nums; }
#toc a[hidden] { display: none; }

/* ---------- transcript items ---------- */

.item { margin: 0 0 18px; scroll-margin-top: calc(var(--top, 56px) + 16px); }
.item[hidden] { display: none; }

.item-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 10px;
  margin-bottom: 6px;
  font-size: 12px;
  color: var(--muted);
}
.item-head .who { font-weight: 650; color: var(--text); font-size: 13px; }

.badge {
  display: inline-block;
  padding: 0 7px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 11px;
  line-height: 18px;
  color: var(--muted);
}
.badge.accent { background: var(--accent-soft); border-color: transparent; color: var(--accent); }

.bubble {
  padding: 12px 16px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  overflow-wrap: anywhere;
}

.item.user .bubble { background: var(--user-bg); border-color: var(--user-border); }
.item.user .who { color: #3d5f91; }
:root[data-theme="dark"] .item.user .who { color: #8fb0e0; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .item.user .who { color: #8fb0e0; }
}
.item.assistant .who { color: var(--accent); }

.plain { white-space: pre-wrap; }

.item.sidechain { margin-left: 24px; opacity: .92; }

/* turn blocks */

.turn > * + * { margin-top: 10px; }

.text-block > :first-child { margin-top: 0; }
.text-block > :last-child { margin-bottom: 0; }

.turn-foot { font-size: 12px; color: var(--muted); margin-top: 6px; }

/* collapsibles */

details.fold {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  overflow: hidden;
}

details.fold > summary {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 7px 12px;
  cursor: pointer;
  list-style: none;
  font-size: 13px;
  user-select: none;
  min-width: 0;
}
details.fold > summary::-webkit-details-marker { display: none; }
details.fold > summary::before {
  content: "▸";
  color: var(--muted);
  flex: none;
  transition: transform .12s;
}
details.fold[open] > summary::before { transform: rotate(90deg); }
details.fold > summary:hover { background: var(--surface-2); }

.sum-label { font-weight: 600; flex: none; }
.sum-detail {
  color: var(--muted);
  font-family: var(--mono);
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
  flex: 1;
}
.sum-right { margin-left: auto; flex: none; font-size: 12px; color: var(--muted); }

.fold-body { padding: 10px 12px; border-top: 1px solid var(--border); }
.fold-body > :first-child { margin-top: 0; }
.fold-body > :last-child { margin-bottom: 0; }

details.thinking { background: transparent; border-style: dashed; }
details.thinking .sum-label { color: var(--muted); font-weight: 500; font-style: italic; }
details.thinking .fold-body { color: var(--muted); font-size: 14px; }

details.tool .sum-label { color: var(--accent); font-family: var(--mono); font-size: 12.5px; }
details.tool.error { border-color: var(--err); }
details.tool.error .sum-right { color: var(--err); }
details.tool.pending .sum-right { color: var(--muted); }

details.meta-att { background: transparent; border-style: dotted; }
details.meta-att .sum-label { color: var(--muted); font-weight: 500; }

.section-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  margin: 10px 0 4px;
}
.section-label:first-child { margin-top: 0; }

pre.out {
  margin: 0;
  padding: 10px 12px;
  background: var(--code-bg);
  border-radius: 8px;
  overflow: auto;
  max-height: 480px;
  white-space: pre-wrap;
  word-break: break-word;
}
pre.out.error { background: var(--err-soft); color: var(--err); }

pre.diff { padding: 0; white-space: pre; }
pre.diff span { display: block; padding: 0 12px; }
pre.diff .add { background: var(--add); }
pre.diff .del { background: var(--del); }
pre.diff span:first-child { padding-top: 8px; }
pre.diff span:last-child { padding-bottom: 8px; }

.more-btn { margin-top: 6px; font-size: 12px; padding: 2px 10px; }

.tool-img { display: block; max-width: 100%; border-radius: 8px; border: 1px solid var(--border); margin: 4px 0; }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-family: var(--mono); font-size: 12.5px; overflow-wrap: anywhere; }

.question { margin: 0 0 10px; }
.question:last-child { margin-bottom: 0; }
.question ul { margin: 4px 0 0; padding-left: 20px; }
.question .chosen { color: var(--ok); font-weight: 600; }

.todo { list-style: none; padding-left: 0; margin: 0; }
.todo li::before { content: "☐ "; color: var(--muted); }
.todo li.completed::before { content: "☑ "; color: var(--ok); }
.todo li.in_progress::before { content: "◐ "; color: var(--accent); }
.todo li.completed { color: var(--muted); text-decoration: line-through; }

/* pasted / tagged segments inside prompts */

.tagged {
  margin: 6px 0;
  border-left: 3px solid var(--border);
  padding: 2px 0 2px 10px;
}
.tagged .tag-label { font-size: 11px; color: var(--muted); letter-spacing: .04em; }
.tagged .plain { font-size: 14px; }
.tagged.pasted { border-left-color: var(--user-border); }
.tagged.command .plain { font-family: var(--mono); font-size: 13px; }

/* dividers / notes */

.divider {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--muted);
  font-size: 12px;
  margin: 26px 0;
}
.divider::before, .divider::after { content: ""; flex: 1; border-top: 1px solid var(--border); }

.note {
  font-size: 13px;
  color: var(--muted);
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--surface-2);
}
.note.recap { border-left: 3px solid var(--accent); }

/* meta visibility toggles */

body:not(.show-meta) .meta-only { display: none !important; }
body.hide-thinking details.thinking { display: none; }
body.hide-tools details.tool { display: none; }

/* ---------- markdown ---------- */

.md p { margin: 0 0 .7em; }
.md h1, .md h2, .md h3, .md h4, .md h5, .md h6 { line-height: 1.35; margin: 1.1em 0 .5em; }
.md h1 { font-size: 1.35em; }
.md h2 { font-size: 1.2em; }
.md h3 { font-size: 1.07em; }
.md h4, .md h5, .md h6 { font-size: 1em; }
.md ul, .md ol { margin: 0 0 .7em; padding-left: 1.5em; }
.md li > p { margin: 0; }
.md li + li { margin-top: .15em; }
.md li > ul, .md li > ol { margin: .15em 0 0; }
.md blockquote { margin: 0 0 .7em; padding: 0 0 0 12px; border-left: 3px solid var(--border); color: var(--muted); }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 1em 0; }
.md code { background: var(--code-bg); padding: .1em .35em; border-radius: 4px; font-size: .88em; }
.md pre.code {
  margin: 0 0 .8em;
  padding: 12px 14px;
  background: var(--pre-bg);
  color: var(--pre-text);
  border-radius: 8px;
  overflow-x: auto;
  position: relative;
}
.md pre.code code { background: none; padding: 0; font-size: 13px; }
.md pre.code[data-lang]:not([data-lang=""])::after {
  content: attr(data-lang);
  position: absolute;
  top: 4px;
  right: 8px;
  font-size: 11px;
  color: #8d8a83;
}
.md .table-wrap { overflow-x: auto; margin: 0 0 .8em; }
.md table { border-collapse: collapse; font-size: 14px; }
.md th, .md td { border: 1px solid var(--border); padding: 5px 10px; vertical-align: top; }
.md th { background: var(--surface-2); text-align: left; }
.md > :last-child { margin-bottom: 0; }

mark { background: #f7d774; color: inherit; border-radius: 2px; }

/* ---------- small screens ---------- */

@media (max-width: 860px) {
  #toggle-sidebar { display: inline-flex; }
  #viewer { grid-template-columns: minmax(0, 1fr); }
  #sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 30;
    width: min(300px, 85vw);
    height: auto;
    transform: translateX(-105%);
    transition: transform .2s;
    box-shadow: 0 0 24px rgba(0, 0, 0, .2);
  }
  body.sidebar-open #sidebar { transform: none; }
  #transcript { padding: 16px 16px 96px; }
  .item.sidechain { margin-left: 10px; }
  #search { flex: 1 1 160px; }
  #theme-btn { order: 1; }
  .btn.primary { order: 2; }
  #search, #search-count { order: 3; }
  .toggles { order: 4; }
}
