:root {
  color-scheme: dark;
  --bg: #0e1116;
  --panel: #151a21;
  --panel-2: #1b222c;
  --line: #27313d;
  --text: #e6edf3;
  --muted: #8b98a5;
  --accent: #58a6ff;
  --green: #3fb950;
  --red: #f85149;
  --yellow: #d29922;
  --composer-h: 72px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "PingFang SC", "Microsoft YaHei", sans-serif;
}

body { overflow: hidden; }

button, input, textarea, select { font: inherit; }

.app-shell {
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  background: rgba(14, 17, 22, 0.96);
  backdrop-filter: blur(8px);
  z-index: 10;
}

.topbar .brand { min-width: 0; flex: 1; }
.topbar h1 { font-size: 16px; margin: 0; white-space: nowrap; }
.project-line {
  margin-top: 2px;
  color: var(--muted);
  font-size: 11px;
  overflow-wrap: anywhere;
  line-height: 1.4;
}

.icon-button {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  width: 36px;
  height: 36px;
  font-size: 18px;
  cursor: pointer;
}

.connection {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}
.dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--muted);
}
.connection.ok .dot { background: var(--green); box-shadow: 0 0 8px var(--green); }
.connection.bad .dot { background: var(--red); }
.connection.warn .dot { background: var(--yellow); }
.connection.muted .dot { background: var(--muted); }

.workspace {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  overflow: hidden;
}

.conversation-column {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.drawer {
  flex: 0 0 320px;
  min-width: 0;
  background: var(--panel);
  border-right: 1px solid var(--line);
  padding: 12px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.drawer-head h2 { font-size: 14px; margin: 0; }
.drawer-head.compact { margin-top: 4px; }

.drawer-section label { margin: 0 0 6px; }

label {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin: 8px 0 6px;
}

input, textarea, select {
  width: 100%;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 10px;
  outline: none;
}
select { appearance: auto; cursor: pointer; }
textarea { resize: none; line-height: 1.45; }
input:focus, textarea:focus, select:focus { border-color: var(--accent); }

button.primary {
  background: var(--accent);
  color: #06111f;
  border: 0;
  border-radius: 8px;
  padding: 10px 14px;
  font-weight: 700;
  cursor: pointer;
}
button.secondary {
  width: 100%;
  margin-top: 8px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 12px;
  cursor: pointer;
}
button.danger {
  background: transparent;
  color: var(--red);
  border: 1px solid var(--red);
  border-radius: 8px;
  padding: 10px 14px;
  font-weight: 700;
  cursor: pointer;
}
button.ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 9px;
  cursor: pointer;
}
button:disabled { opacity: 0.5; cursor: not-allowed; }

.session-list {
  display: flex;
  flex-direction: column;
  gap: 7px;
  min-height: 0;
  overflow-y: auto;
}
.session-item {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 9px 10px;
  cursor: pointer;
  color: var(--text);
  text-align: left;
}
.session-item.active { border-color: var(--accent); }
.session-item .s-name {
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.session-item .s-meta {
  margin-top: 4px;
  color: var(--muted);
  font-size: 11px;
}

.debug-panel { margin-top: 4px; color: var(--muted); font-size: 12px; }
.raw-events {
  max-height: 220px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 10px;
  color: var(--muted);
}

.conversation {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  overflow-y: auto;
  padding: 18px 14px;
  scroll-behavior: smooth;
}

.messages {
  width: min(860px, 100%);
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.empty {
  color: var(--muted);
  text-align: center;
  padding: 60px 20px;
}

.message-row { display: flex; flex-direction: column; }
.message-row.user { align-items: flex-end; }
.message-row.assistant { align-items: flex-start; }
.message-label {
  color: var(--muted);
  font-size: 11px;
  margin: 0 4px 4px;
}
.bubble {
  max-width: min(720px, 88%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 10px 12px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  font-size: 14px;
  line-height: 1.55;
}
.message-row.user .bubble {
  background: #153a5b;
  border-color: rgba(88, 166, 255, 0.5);
}
.assistant-bubble { min-width: 72px; min-height: 22px; }

.markdown-paragraph,
.markdown-heading,
.markdown-list {
  margin: 0 0 8px;
  line-height: 1.55;
}
.markdown-heading {
  color: var(--text);
  font-size: 1.05em;
  font-weight: 700;
}
.markdown-heading:first-child { margin-top: 0; }
.markdown-paragraph:last-child,
.markdown-list:last-child,
.markdown-code:last-child,
.markdown-table-wrap:last-child { margin-bottom: 0; }
.markdown-list { padding-left: 20px; }
.inline-code {
  padding: 1px 5px;
  border-radius: 6px;
  background: #0c1016;
  border: 1px solid var(--line);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.9em;
}
.markdown-code {
  margin: 0 0 10px;
  padding: 10px;
  background: #0c1016;
  border: 1px solid var(--line);
  border-radius: 9px;
  color: #c9d1d9;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11px;
  line-height: 1.5;
  overflow-x: auto;
}
.token-comment { color: #8b98a5; font-style: italic; }
.token-string { color: #a5d6ff; }
.token-keyword { color: #ff7b72; }
.token-number { color: #79c0ff; }
.token-function { color: #d2a8ff; }
.markdown-table-wrap {
  margin: 0 0 10px;
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: 9px;
}
.markdown-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}
.markdown-table th,
.markdown-table td {
  padding: 7px 9px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}
.markdown-table th { background: var(--panel-2); color: var(--text); }
.markdown-table tr:last-child td { border-bottom: 0; }

.spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid rgba(230, 237, 243, 0.25);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.system-note {
  align-self: center;
  color: var(--muted);
  font-size: 11px;
  padding: 5px 9px;
  border: 1px dashed var(--line);
  border-radius: 999px;
}
.system-note.error { color: var(--red); border-color: rgba(248, 81, 73, 0.5); }

.tool-activity {
  align-self: stretch;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
}
.tool-head {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  background: transparent;
  color: var(--text);
  border: 0;
  padding: 9px 11px;
  cursor: pointer;
  text-align: left;
}
.tool-icon { color: var(--accent); font-size: 14px; width: 16px; flex: 0 0 auto; text-align: center; }
.tool-icon-completed { color: var(--green); }
.tool-icon-failed { color: var(--red); }
.tool-copy {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
}
.tool-name {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  overflow-wrap: anywhere;
}
.tool-summary {
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11px;
  line-height: 1.4;
  overflow-wrap: anywhere;
  word-break: break-word;
  white-space: pre-wrap;
}
.tool-status {
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 11px;
  white-space: nowrap;
}
.tool-status-completed { color: var(--green); }
.tool-status-failed { color: var(--red); }
.tool-body { border-top: 1px solid var(--line); padding: 9px 11px; display: flex; flex-direction: column; gap: 8px; }
.tool-body[hidden] { display: none; }
.tool-field-label { color: var(--muted); font-size: 10px; margin-bottom: 3px; }
.tool-pre {
  margin: 0;
  padding: 8px;
  background: #0c1016;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: #c9d1d9;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11px;
  line-height: 1.5;
  max-height: 300px;
  overflow-y: auto;
}

.tool-command {
  margin: 0;
  padding: 8px;
  background: #0c1016;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: #c9d1d9;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11px;
  line-height: 1.5;
}
.tool-output {
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: #0c1016;
}
.tool-output-label {
  padding: 5px 8px;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 10px;
  text-transform: lowercase;
}
.tool-output-stderr .tool-output-label { color: var(--red); }
.tool-output-pre {
  margin: 0;
  padding: 8px;
  color: #c9d1d9;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11px;
  line-height: 1.5;
  max-height: 320px;
  overflow-y: auto;
}
.tool-output-toggle {
  display: block;
  width: 100%;
  background: transparent;
  color: var(--accent);
  border: 0;
  border-top: 1px solid var(--line);
  padding: 6px 8px;
  cursor: pointer;
  text-align: left;
  font-size: 11px;
}
.tool-output-toggle:hover { background: rgba(88, 166, 255, 0.08); }
.tool-exit {
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11px;
}
.tool-exit-ok { color: var(--green); }
.tool-exit-bad { color: var(--red); }

.tool-changes {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.tool-changes-title {
  color: var(--text);
  font-size: 12px;
  font-weight: 700;
}
.tool-change {
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: #0c1016;
}
.tool-change-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 9px;
  border-bottom: 1px solid var(--line);
}
.tool-change-kind {
  flex: 0 0 auto;
  color: var(--accent);
  font-size: 11px;
}
.tool-change-path {
  min-width: 0;
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11px;
  overflow-wrap: anywhere;
  word-break: break-word;
}
.tool-diff {
  margin: 0;
  padding: 8px;
  color: #c9d1d9;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11px;
  line-height: 1.5;
  max-height: 320px;
  overflow-y: auto;
}

.composer {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: rgba(14, 17, 22, 0.96);
}
.composer textarea {
  flex: 1 1 auto;
  min-height: 42px;
  max-height: 160px;
}
.composer-actions {
  flex: 0 0 auto;
  display: flex;
  gap: 8px;
  align-items: center;
}

.auth-gate {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(14, 17, 22, 0.97);
}
.auth-gate[hidden] { display: none; }
.auth-card {
  width: min(420px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 20px;
}
.auth-card h2 { margin: 0 0 8px; font-size: 18px; }
.auth-card p { margin: 0 0 6px; }
.auth-card button { width: 100%; margin-top: 14px; }
.error { color: var(--red); font-size: 12px; margin: 8px 0 0; }
.muted { color: var(--muted); font-size: 12px; }

.select-row {
  display: flex;
  align-items: center;
  gap: 6px;
}
.select-row select { flex: 1; min-width: 0; }
.select-row .ghost {
  flex: 0 0 auto;
  width: 34px;
  height: 34px;
  font-size: 16px;
  line-height: 1;
  padding: 0;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(14, 17, 22, 0.88);
}
.modal[hidden] { display: none; }
.modal-card {
  width: min(420px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 20px;
}
.modal-card h2 { margin: 0 0 6px; font-size: 18px; }
.modal-card .muted { margin: 0 0 10px; }
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 14px;
}
.modal-actions .primary { min-width: 88px; }
.modal-actions .ghost { min-width: 72px; }

@media (max-width: 760px) {
  .workspace { display: block; position: relative; height: 100%; }
  .conversation-column { height: 100%; }
  .drawer {
    position: fixed;
    z-index: 30;
    top: 57px;
    bottom: 0;
    left: 0;
    width: min(84vw, 340px);
    transform: translateX(-105%);
    transition: transform 0.2s ease;
    box-shadow: 12px 0 30px rgba(0, 0, 0, 0.35);
  }
  body.drawer-open .drawer { transform: translateX(0); }
  .conversation { padding: 14px 10px; }
  .bubble { max-width: 92%; }
  .composer { padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); }
  .topbar h1 { font-size: 15px; }
}

.context-nav {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.nav-item,
.project-nav-name,
.nav-sub {
  background: transparent;
  color: var(--text);
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 7px 9px;
  text-align: left;
  cursor: pointer;
  font-size: 13px;
}
.nav-item:hover,
.project-nav-name:hover,
.nav-sub:hover { background: var(--panel-2); }
.nav-item.active,
.nav-sub.active { border-color: var(--accent); color: var(--accent); }
.nav-label {
  margin-top: 6px;
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.project-nav-group {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding-left: 4px;
}
.project-nav-name {
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.project-nav-subs {
  display: flex;
  gap: 6px;
  padding-left: 8px;
}
.project-nav-subs .nav-sub {
  flex: 1 1 0;
  text-align: center;
  font-size: 12px;
  padding: 5px 6px;
}

.approvals {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin: 0 0 12px;
}
.approval-card {
  border: 1px solid var(--line);
  border-left: 4px solid var(--accent);
  background: var(--panel-2);
  border-radius: 10px;
  padding: 10px 12px;
}
.approval-card.approval-approved { border-left-color: var(--green, #3fb950); }
.approval-card.approval-denied { border-left-color: var(--red, #f85149); }
.approval-card.approval-failed { border-left-color: var(--red, #f85149); }
.approval-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}
.approval-title { font-weight: 700; font-size: 13px; }
.approval-status { color: var(--muted); font-size: 12px; }
.approval-reason { color: var(--muted); font-size: 12px; margin: 0 0 6px; }
.approval-command {
  margin: 0 0 6px;
  padding: 8px;
  border-radius: 8px;
  background: var(--panel);
  overflow-x: auto;
  white-space: pre-wrap;
  font-size: 12px;
}
.approval-cwd { color: var(--muted); font-size: 11px; margin: 0 0 8px; }
.approval-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.approval-button {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 9px;
  font-size: 12px;
  cursor: pointer;
}
.approval-button:disabled { opacity: 0.45; cursor: not-allowed; }
.approval-button.allow { border-color: var(--green, #3fb950); color: var(--green, #3fb950); }
.approval-button.allow-session { border-color: var(--accent); color: var(--accent); }
.approval-button.deny { border-color: var(--red, #f85149); color: var(--red, #f85149); }
