:root {
  --bg: #ffffff;
  --surface: #ffffff;
  --surface-soft: #f7f7f8;
  --surface-hover: #ececf1;
  --user-bg: #f4f4f4;
  --detour-bg: #fbfbfb;
  --text: #0d0d0d;
  --muted: #6b7280;
  --line: #e5e7eb;
  --line-strong: #d1d5db;
  --accent: #10a37f;
  --danger: #b42318;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
  --branch-card-side-gap: 96px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app {
  width: 100%;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  background: var(--bg);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 22px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(10px);
}

.topbar h1 {
  margin: 0;
  font-size: 1.08rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.topbar p {
  margin: 3px 0 0;
  color: var(--muted);
  font-size: 0.84rem;
}

.ghost-btn,
.back-main {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  border-radius: 999px;
  padding: 7px 12px;
  transition: background 150ms ease, border-color 150ms ease;
}

.ghost-btn:hover,
.back-main:hover {
  background: var(--surface-soft);
  border-color: var(--line-strong);
}

.ghost-btn.small {
  padding: 5px 10px;
  font-size: 0.82rem;
}

.chat-window {
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  scroll-behavior: smooth;
}

.message-list {
  width: min(900px, 100%);
  margin: 0 auto;
  padding: 34px 20px 150px;
}

.message {
  position: relative;
  margin: 18px 0;
}

.message-card {
  max-width: 760px;
  border-radius: 18px;
  padding: 13px 16px;
  line-height: 1.58;
  white-space: pre-wrap;
  word-break: break-word;
}

.message-content {
  min-width: 0;
}

.message.user {
  display: flex;
  justify-content: flex-end;
}

.message.user .message-card {
  max-width: min(70%, 560px);
  background: var(--user-bg);
  color: var(--text);
}

.message.assistant .message-card {
  width: 100%;
  background: transparent;
  border: none;
  padding: 8px 0;
}

.message.assistant.branchable {
  min-height: 48px;
}

.message.assistant.branchable .message-card {
  margin-left: var(--branch-card-side-gap);
  margin-right: 0;
}

.branch-row {
  position: relative;
  width: 100%;
  overflow: hidden;
  margin: 22px 0;
}

.branch-lanes {
  display: flex;
  width: 200%;
  transition: transform 260ms cubic-bezier(0.22, 1, 0.36, 1);
  will-change: transform;
}

.branch-lane {
  flex: 0 0 50%;
  min-width: 0;
  padding: 0 4px;
}

.branch-row[data-active="detour"] .branch-lanes {
  transform: translateX(0);
}

.branch-row[data-active="main"] .branch-lanes {
  transform: translateX(-50%);
}

.deep-branch {
  position: absolute;
  left: 8px;
  top: 50%;
  z-index: 5;
  max-width: 78px;
  min-height: 34px;
  padding: 7px 10px;
  transform: translateY(-50%);
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.04);
  font-size: 0.76rem;
  line-height: 1.05;
  opacity: 0.72;
  transition: opacity 150ms ease, background 150ms ease, color 150ms ease, transform 150ms ease, border-color 150ms ease;
}

.deep-branch[data-has-detour="true"]::after {
  content: "";
  position: absolute;
  right: 6px;
  top: 5px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
}

.message.assistant.branchable:hover .deep-branch,
.deep-branch:focus-visible {
  opacity: 1;
}

.deep-branch:hover {
  background: var(--surface-soft);
  border-color: var(--line-strong);
  color: var(--text);
  transform: translateY(-50%) scale(1.03);
}

.detour-tags {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 7px;
  margin-top: 9px;
}

.detour-label {
  color: var(--muted);
  font-size: 0.78rem;
}

.detour-tag {
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface-soft);
  color: var(--muted);
  padding: 5px 10px;
  font-size: 0.78rem;
  transition: background 150ms ease, color 150ms ease, border-color 150ms ease;
}

.detour-tag:hover {
  background: var(--surface-hover);
  border-color: var(--line-strong);
  color: var(--text);
}

.detour-lane {
  background: var(--detour-bg);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 14px 16px 16px;
}

.detour-head {
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-areas:
    "back title"
    "origin origin";
  align-items: center;
  gap: 6px 10px;
  padding-bottom: 12px;
  margin-bottom: 12px;
  border-bottom: 1px solid var(--line);
}

.detour-head .back-main {
  grid-area: back;
  width: fit-content;
  padding: 5px 10px;
  font-size: 0.82rem;
}

.detour-title {
  grid-area: title;
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 700;
}

.detour-origin {
  grid-area: origin;
  grid-column: 1 / -1;
  color: var(--muted);
  font-size: 0.82rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.detour-empty {
  color: var(--muted);
  background: var(--surface);
  border: 1px dashed var(--line-strong);
  border-radius: 14px;
  padding: 14px;
  font-size: 0.9rem;
}

.detour-thread {
  display: grid;
  gap: 10px;
}

.detour-thread .message {
  margin: 0;
}

.detour-thread .message-card {
  max-width: 100%;
}

.detour-thread .message.assistant .message-card {
  padding: 10px 0;
}

.composer {
  position: sticky;
  bottom: 0;
  border-top: 1px solid transparent;
  background: linear-gradient(to top, #ffffff 82%, rgba(255, 255, 255, 0));
  padding: 18px 20px 24px;
}

.composer-inner {
  width: min(760px, 100%);
  margin: 0 auto;
}

.composer-meta {
  min-height: 31px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 7px;
}

.branch-hint {
  color: var(--muted);
  font-size: 0.84rem;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.composer-back {
  flex: 0 0 auto;
  font-size: 0.82rem;
}

.compose-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 9px;
  align-items: end;
}

textarea {
  min-height: 52px;
  max-height: 180px;
  width: 100%;
  resize: none;
  border: 1px solid var(--line-strong);
  border-radius: 24px;
  background: var(--surface);
  color: var(--text);
  padding: 14px 16px;
  box-shadow: 0 2px 16px rgba(0, 0, 0, 0.07);
  outline: none;
}

textarea:focus {
  border-color: #b5b5b5;
  box-shadow: 0 2px 18px rgba(0, 0, 0, 0.1);
}

#sendBtn {
  min-width: 54px;
  height: 44px;
  border: none;
  border-radius: 999px;
  background: var(--text);
  color: #ffffff;
  padding: 0 18px;
  font-weight: 700;
}

#sendBtn:hover {
  background: #2f2f2f;
}

.status {
  min-height: 18px;
  margin-top: 5px;
  color: var(--danger);
  font-size: 0.8rem;
}

.copy-btn {
  margin-top: 8px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface-soft);
  color: var(--muted);
  padding: 4px 10px;
  font-size: 0.75rem;
}

.meta-panel {
  position: fixed;
  right: 20px;
  top: 72px;
  z-index: 20;
  width: min(380px, calc(100vw - 40px));
  max-height: 70vh;
  overflow: auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 14px;
  box-shadow: var(--shadow);
}

.meta-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.meta-panel h2 {
  margin: 0;
  font-size: 1rem;
}

#contextPreview {
  margin: 0;
  white-space: pre-wrap;
  color: var(--text);
  font-size: 0.82rem;
  line-height: 1.5;
}

.hidden {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121214;
    --surface: #1a1a1f;
    --surface-soft: #202027;
    --surface-hover: #292933;
    --user-bg: #252530;
    --detour-bg: #181821;
    --text: #f5f5f7;
    --muted: #a8acb8;
    --line: #2f3340;
    --line-strong: #414757;
    --danger: #ff8f8f;
    --shadow: 0 12px 35px rgba(0, 0, 0, 0.35);
  }

  .topbar {
    background: rgba(20, 20, 24, 0.9);
  }

  .composer {
    background: linear-gradient(to top, #121214 82%, rgba(18, 18, 20, 0));
  }

  #sendBtn:hover {
    background: #3a3a3a;
  }
}

@media (max-width: 720px) {
  :root {
    --branch-card-side-gap: 82px;
  }

  .topbar p {
    display: none;
  }

  .message-list {
    padding-inline: 14px;
  }

  .deep-branch {
    left: 0;
    max-width: 72px;
    padding-inline: 8px;
  }

  .detour-lane {
    padding: 12px;
  }
}

