:root {
  --bg: #0d1117;
  --panel: #161b22;
  --panel2: #1c2330;
  --line: #262c36;
  --fg: #e6edf3;
  --dim: #8b949e;
  --accent: #4d6bfe;
  --me: #1f6feb;
  --ok: #2ea043;
  --err: #d1242f;
  --warn: #bb8009;
}
@media (prefers-color-scheme: light) {
  :root { --bg: #f6f8fa; --panel: #fff; --panel2: #f2f5f8; --line: #d0d7de; --fg: #1f2328; --dim: #656d76; }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; background: var(--bg); color: var(--fg);
  font: 15px/1.65 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
        "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  overscroll-behavior-y: none;
}
.hidden { display: none !important; }

/* ---------------- 令牌门 ---------------- */
.gate { position: fixed; inset: 0; display: grid; place-items: center; padding: 24px; background: var(--bg); z-index: 50; }
.gate-card { width: 100%; max-width: 340px; text-align: center; }
.gate-card h1 { margin: 0 0 6px; font-size: 30px; letter-spacing: 2px; }
.gate-sub { margin: 0 0 22px; color: var(--dim); font-size: 13px; }
#tokenInput { width: 100%; padding: 13px 14px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel); color: var(--fg); font-size: 16px; outline: none; }
#tokenInput:focus { border-color: var(--accent); }
#gateBtn { width: 100%; margin-top: 12px; padding: 13px; border: 0; border-radius: 10px; background: var(--accent); color: #fff; font-size: 15px; font-weight: 600; cursor: pointer; }
#gateBtn:active { opacity: .8; }
.gate-err { min-height: 20px; margin: 12px 0 0; color: var(--err); font-size: 13px; }

/* ---------------- 布局 ---------------- */
.app { display: flex; flex-direction: column; height: 100dvh; padding-top: env(safe-area-inset-top); }
.bar { display: flex; align-items: center; justify-content: space-between; padding: 9px 13px; border-bottom: 1px solid var(--line); background: var(--panel); flex: 0 0 auto; }
.bar-l { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--dim); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--dim); transition: background .3s; }
.dot.online { background: var(--ok); box-shadow: 0 0 7px #2ea04388; }
.dot.busy { background: var(--accent); animation: pulse 1.1s infinite; }
@keyframes pulse { 0%,100% { opacity: 1 } 50% { opacity: .25 } }
.bar-btn { border: 1px solid var(--line); background: transparent; color: var(--dim); padding: 5px 11px; border-radius: 8px; font-size: 12px; cursor: pointer; }

/* ---------------- 上下文用量状态栏 ---------------- */
.ctx-bar {
  flex: 1 1 auto; margin: 0 9px; min-width: 0;
  border: 1px solid var(--line); background: transparent; color: var(--dim);
  padding: 4px 10px; border-radius: 8px; font-size: 11.5px; cursor: pointer;
  font-variant-numeric: tabular-nums;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.ctx-bar.ok   { color: var(--dim); border-color: #2ea04344; }
.ctx-bar.warn { color: #d29922; border-color: #d2992266; }
.ctx-bar.danger { color: #f85149; border-color: #f8514966; }

.ctx-panel { flex: 0 0 auto; border-bottom: 1px solid var(--line); background: var(--panel); padding: 9px 13px 11px; }
.ctx-row { display: flex; align-items: center; gap: 10px; }
.ctx-bar-track { flex: 1 1 auto; height: 6px; border-radius: 3px; background: #21262d; overflow: hidden; }
.ctx-bar-fill { height: 100%; width: 0; border-radius: 3px; background: var(--accent); transition: width .45s ease; }
.ctx-bar-fill.warn { background: #d29922; }
.ctx-bar-fill.danger { background: #f85149; }
.ctx-pct { flex: 0 0 auto; font-size: 12px; color: var(--dim); font-variant-numeric: tabular-nums; min-width: 46px; text-align: right; }

.ctx-detail { display: flex; flex-wrap: wrap; gap: 5px 8px; margin-top: 9px; }
.ctx-kv { display: inline-flex; align-items: baseline; gap: 5px; border: 1px solid var(--line); border-radius: 7px; padding: 3px 8px; font-size: 11.5px; }
.ctx-kv b { color: var(--dim); font-weight: 400; }
.ctx-kv i { color: var(--fg); font-style: normal; font-variant-numeric: tabular-nums; }

.log { flex: 1 1 auto; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 13px 11px 6px; display: flex; flex-direction: column; gap: 11px; }

/* ---------------- 气泡 ---------------- */
.bubble { max-width: 88%; padding: 9px 13px 10px; border-radius: 14px; word-wrap: break-word; overflow-wrap: anywhere; }
.bubble.user { align-self: flex-end; background: var(--me); color: #fff; border-bottom-right-radius: 5px; }
.bubble.assistant { align-self: flex-start; background: var(--panel); border: 1px solid var(--line); border-bottom-left-radius: 5px; padding-bottom: 6px; }
.bubble.assistant.error { border-color: var(--err); }
.bubble .head { display: flex; align-items: center; gap: 7px; margin-bottom: 4px; }
.bubble .who { font-size: 11px; font-weight: 600; color: var(--dim); letter-spacing: .5px; }
.bubble .took { font-size: 11px; color: var(--dim); }
.live-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); animation: pulse 1s infinite; }
.bubble .body { white-space: pre-wrap; }
.msg.system { align-self: center; max-width: 92%; background: transparent; color: var(--dim); font-size: 12.5px; text-align: center; border: 1px dashed var(--line); padding: 7px 12px; border-radius: 10px; }

.cursor { display: inline-block; color: var(--accent); animation: blink 1s steps(2) infinite; margin-left: 1px; }
@keyframes blink { 50% { opacity: 0 } }

.errbox { margin-top: 7px; padding: 7px 10px; border-radius: 8px; background: #d1242f1f; color: var(--err); font-size: 13px; white-space: pre-wrap; }

/* ---------------- 折叠区 ---------------- */
.fold { margin-top: 9px; border-top: 1px solid var(--line); padding-top: 7px; }
.fold-h {
  display: flex; align-items: center; gap: 7px; width: 100%;
  background: transparent; border: 0; color: var(--dim);
  font: inherit; font-size: 12.5px; padding: 4px 2px; cursor: pointer; text-align: left;
}
.fold-h:active { opacity: .65; }
.fold-caret { display: inline-block; transition: transform .15s; font-size: 10px; }
.fold-caret.open { transform: rotate(90deg); }
.fold-label { flex: 1; }
.fold-count { opacity: .7; font-size: 11.5px; }
.fold-b { padding: 5px 0 2px; }

.reason {
  white-space: pre-wrap; font-size: 13px; color: var(--dim);
  background: var(--panel2); border-radius: 8px; padding: 9px 11px;
  border-left: 2px solid var(--line);
}

.tool { margin-bottom: 8px; background: var(--panel2); border-radius: 8px; padding: 8px 10px; font-size: 13px; }
.tool-h { display: flex; align-items: center; gap: 7px; }
.tool .ic { font-size: 12px; width: 14px; text-align: center; }
.tool.done .ic { color: var(--ok); }
.tool.error .ic { color: var(--err); }
.tool.running .ic { color: var(--warn); }
.tool .tn { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-weight: 600; flex: 1; }
.tool .ts { font-size: 11px; color: var(--dim); }
.tool pre {
  margin: 7px 0 0; padding: 8px 10px; border-radius: 6px; background: #0003;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px; line-height: 1.5; white-space: pre-wrap; word-break: break-all;
  max-height: 320px; overflow: auto; -webkit-overflow-scrolling: touch;
}
.tool pre.in { border-left: 2px solid var(--accent); }
.tool pre.out { border-left: 2px solid var(--ok); }
.tool.error pre.out { border-left-color: var(--err); }

/* ---------------- 底部 ---------------- */
.busy { display: flex; align-items: center; justify-content: center; gap: 8px; padding: 6px; color: var(--dim); font-size: 12.5px; flex: 0 0 auto; }
.spin { width: 13px; height: 13px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg) } }

.composer { flex: 0 0 auto; display: flex; gap: 8px; align-items: flex-end; padding: 9px 12px calc(9px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); background: var(--panel); }
#input { flex: 1; resize: none; max-height: 140px; padding: 10px 13px; border: 1px solid var(--line); border-radius: 20px; background: var(--bg); color: var(--fg); font: inherit; font-size: 16px; outline: none; }
#input:focus { border-color: var(--accent); }
.send { flex: 0 0 auto; width: 40px; height: 40px; border: 0; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; cursor: pointer; }
