:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1d212b;
  --border: #2a2f3a;
  --text: #e6e8ec;
  --muted: #8b93a3;
  --accent: #d97757;
  --accent-2: #e8916f;
  --green: #4caf7d;
  --amber: #e6b450;
  --red: #e06c5f;
  --radius: 10px;
}

* { box-sizing: border-box; }

/* hidden 属性必须压过一切 display 规则（否则遮罩层永远盖着页面） */
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.6 -apple-system, "Segoe UI", "Microsoft YaHei", Roboto, sans-serif;
}

button {
  font: inherit;
  color: inherit;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 5px 12px;
  cursor: pointer;
}
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { background: var(--accent-2); }

/* ---------- token gate ---------- */

#token-gate {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 100;
}
.gate-card {
  width: min(420px, 92vw);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  text-align: center;
}
.gate-card h1 { font-size: 20px; margin: 0 0 8px; }
.gate-card p { color: var(--muted); font-size: 13px; }
.gate-card .gate-tip { margin-top: 14px; font-size: 12px; }
.gate-card form { display: flex; flex-direction: column; gap: 8px; margin-top: 16px; }
.gate-card form button { margin-top: 4px; }

input, textarea {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  outline: none;
}
input:focus, textarea:focus { border-color: var(--accent); }

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

#app {
  display: flex;
  height: 100vh;
}

#sidebar {
  width: 280px;
  flex: none;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 15px;
  padding: 14px 16px 10px;
}
.logo { color: var(--accent); }

.add-node {
  display: flex;
  gap: 6px;
  padding: 0 10px 10px;
}
.add-node input { flex: 1; min-width: 0; font-size: 12px; }
.add-node select {
  font-size: 12px;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px;
}
.add-node button { padding: 4px 9px; }

.nodes-pane { flex: none; max-height: 30vh; overflow-y: auto; }
.node-type {
  font-size: 10.5px;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 99px;
  padding: 0 7px;
  flex: none;
}

.add-project {
  display: flex;
  gap: 6px;
  padding: 0 12px 12px;
}
.add-project input {
  flex: 1;
  min-width: 0;
  font-size: 12px;
  font-family: Consolas, monospace;
}
.add-project button { padding: 4px 10px; }

.pane {
  flex: 1;
  overflow-y: auto;
  border-top: 1px solid var(--border);
  padding: 10px 8px;
}
.projects-pane { flex: none; max-height: 35vh; }
.pane-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: var(--muted);
  padding: 2px 8px 8px;
}
.pane-title button {
  font-size: 12px;
  padding: 2px 8px;
  color: var(--muted);
}

.list { list-style: none; margin: 0; padding: 0; }
.list li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
}
.list li:hover { background: var(--panel-2); }
.list li.active { background: var(--panel-2); box-shadow: inset 2px 0 0 var(--accent); }
.list li .name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.list li .del {
  visibility: hidden;
  border: none;
  background: none;
  color: var(--muted);
  padding: 0 4px;
}
.list li:hover .del { visibility: visible; }
.list li .del:hover { color: var(--red); }
/* 触屏没有悬停，删除按钮常显 */
@media (hover: none) {
  .list li .del { visibility: visible; }
}

.sidebar-foot {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 12px;
}
.sidebar-foot #conn-text { flex: 1; min-width: 52px; }
.foot-toggle {
  font-size: 12px;
  color: var(--muted);
  padding: 3px 8px;
  border-radius: 99px;
}
.foot-toggle:hover { color: var(--text); }

.import-toggle {
  display: block;
  width: calc(100% - 16px);
  margin: 8px auto 4px;
  font-size: 12px;
  color: var(--muted);
  padding: 5px 8px;
  text-align: center;
}
.native-list {
  padding: 0 8px 8px;
}
.native-list li { font-size: 12.5px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--red); display: inline-block; }
.dot.on { background: var(--green); }
.dot.off { background: var(--red); }

/* ---------- main / chat ---------- */

#main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

#chat-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
#sidebar-toggle { display: none; padding: 4px 10px; }
.head-info { flex: 1; min-width: 0; }
.head-project {
  font-size: 12px;
  color: var(--muted);
  font-family: Consolas, monospace;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.head-session {
  font-weight: 600;
  font-size: 15px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.head-actions { display: flex; align-items: center; gap: 8px; }

.pill {
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 99px;
  border: 1px solid var(--border);
  color: var(--muted);
}
.pill.running { color: var(--accent); border-color: var(--accent); }
.pill.waiting_permission { color: var(--amber); border-color: var(--amber); }
.pill.queued { color: var(--amber); }

#chat {
  flex: 1;
  overflow-y: auto;
  padding: 18px 0;
}
.chat-empty {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 14px;
}

.msg {
  max-width: 820px;
  margin: 0 auto;
  padding: 4px 20px;
}
.msg .role {
  font-size: 11px;
  color: var(--muted);
  margin-bottom: 3px;
}
.msg.user .bubble {
  background: color-mix(in srgb, var(--accent) 16%, transparent);
  border: 1px solid color-mix(in srgb, var(--accent) 35%, transparent);
}
.msg.assistant .bubble {
  background: var(--panel);
  border: 1px solid var(--border);
}
.bubble {
  border-radius: var(--radius);
  padding: 10px 14px;
  overflow-wrap: break-word;
}
.bubble code, .perm-text code {
  font-family: Consolas, monospace;
  font-size: 12.5px;
  background: rgba(255, 255, 255, 0.07);
  padding: 1px 5px;
  border-radius: 4px;
}
.bubble pre {
  background: #0b0d11;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  overflow-x: auto;
  margin: 8px 0;
}
.bubble pre code { background: none; padding: 0; }

.msg .err {
  border: 1px solid var(--red);
  color: var(--red);
  border-radius: var(--radius);
  padding: 10px 14px;
  background: color-mix(in srgb, var(--red) 8%, transparent);
  white-space: pre-wrap;
  font-size: 13px;
}

.meta-line {
  max-width: 820px;
  margin: 6px auto 6px;
  padding: 0 20px;
  color: var(--muted);
  font-size: 11.5px;
  text-align: center;
}

.tool-block {
  max-width: 820px;
  margin: 6px auto 0;
  padding: 0 20px;
}
.tool-block details {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  overflow: hidden;
}
.tool-block summary {
  cursor: pointer;
  padding: 7px 12px;
  font-size: 12.5px;
  color: var(--muted);
  display: flex;
  gap: 8px;
  align-items: center;
  list-style: none;
  font-family: Consolas, monospace;
}
.tool-block summary::before { content: "▸"; color: var(--muted); }
.tool-block details[open] summary::before { content: "▾"; }
.tool-block .tname { color: var(--accent); }
.tool-block .tpreview {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tool-block .tstate.done { color: var(--green); }
.tool-block .tstate.fail { color: var(--red); }
.tool-block .tbody {
  border-top: 1px solid var(--border);
  padding: 10px 12px;
}
.tool-block .tbody h4 {
  margin: 0 0 4px;
  font-size: 11px;
  color: var(--muted);
  font-weight: normal;
}
.tool-block pre {
  margin: 0 0 10px;
  max-height: 300px;
  overflow: auto;
  font-size: 12px;
  font-family: Consolas, monospace;
  background: #0b0d11;
  border-radius: 6px;
  padding: 8px 10px;
  white-space: pre-wrap;
  word-break: break-all;
}

/* ---------- permission banner ---------- */

/* ---------- 运行中提示（模型缓冲/思考期间不至于假死） ---------- */

.running-hint {
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: 860px;
  margin: 0 auto 4px;
  width: calc(100% - 32px);
  color: var(--muted);
  font-size: 13px;
}
.pulse-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--accent);
  animation: cc-pulse 1.2s ease-in-out infinite;
  flex: none;
}
@keyframes cc-pulse {
  0%, 100% { opacity: 0.35; transform: scale(0.85); }
  50% { opacity: 1; transform: scale(1.1); }
}
#live-think {
  max-width: 820px;
  margin: 0 auto 0;
  padding: 4px 20px;
  color: var(--muted);
  font-size: 12.5px;
  font-style: italic;
  white-space: pre-wrap;
  max-height: 160px;
  overflow-y: auto;
  border-left: 2px solid var(--border);
}

/* ---------- 提问勾选对话框 ---------- */

#question-banner {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 860px;
  margin: 0 auto;
  width: calc(100% - 32px);
  border: 1px solid color-mix(in srgb, var(--accent) 45%, transparent);
  background: color-mix(in srgb, var(--accent) 8%, transparent);
  border-radius: var(--radius);
  padding: 12px 14px;
}
#question-banner .q-text b { color: var(--accent); font-size: 13px; }
#question-banner .q-text p {
  margin: 6px 0 0;
  font-size: 13px;
  white-space: pre-wrap;
  max-height: 110px;
  overflow-y: auto;
  color: var(--text);
}
.q-options {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.q-options button {
  text-align: left;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.q-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}
button.ghost {
  border: none;
  background: none;
  color: var(--muted);
  font-size: 12.5px;
  padding: 4px 8px;
}
button.ghost:hover { color: var(--text); }

#permission-banner {
  display: flex;
  align-items: center;
  gap: 14px;
  max-width: 860px;
  margin: 0 auto;
  width: calc(100% - 32px);
  border: 1px solid var(--amber);
  background: color-mix(in srgb, var(--amber) 10%, transparent);
  border-radius: var(--radius);
  padding: 10px 14px;
}
.perm-text { flex: 1; min-width: 0; }
.perm-text b { color: var(--amber); }
.perm-text pre {
  margin: 6px 0 0;
  max-height: 120px;
  overflow: auto;
  font-size: 12px;
  font-family: Consolas, monospace;
  color: var(--muted);
  white-space: pre-wrap;
  word-break: break-all;
}
.perm-actions { display: flex; gap: 8px; flex: none; }

/* ---------- composer ---------- */

#composer {
  display: flex;
  gap: 10px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  max-width: 860px;
  margin: 0 auto;
  width: 100%;
}
#composer textarea {
  flex: 1;
  resize: none;
  max-height: 180px;
  min-height: 40px;
  font-size: 14px;
}
#send-btn { flex: none; padding: 0 20px; }

/* ---------- modal（内嵌 webview 里原生 confirm 不可靠，用页内弹窗） ---------- */

#modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 90;
}
.modal-card {
  width: min(400px, 88vw);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}
.modal-card p { margin: 0 0 18px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }
.modal-code {
  background: #0b0d11;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-family: Consolas, monospace;
  font-size: 12px;
  color: var(--accent-2);
  overflow-x: auto;
  white-space: pre;
  user-select: all;
  margin: 0 0 14px;
}

/* ---------- toast ---------- */

#perm-mode {
  font: inherit;
  font-size: 12px;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 6px;
  cursor: pointer;
}
#perm-mode:focus { border-color: var(--accent); }

#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--panel-2);
  border: 1px solid var(--red);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 16px;
  font-size: 13px;
  z-index: 50;
  max-width: 80vw;
}

/* ---------- mobile ---------- */

@media (max-width: 768px) {
  #sidebar-toggle { display: block; }
  #sidebar {
    position: fixed;
    z-index: 40;
    height: 100%;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }
  #sidebar.open { transform: none; box-shadow: 0 0 40px rgba(0, 0, 0, 0.5); }
  .msg, .tool-block, .meta-line { padding-left: 12px; padding-right: 12px; }
  .head-project { display: none; }
  #chat-head { padding: 8px 10px; }
}
