:root {
  --bg: #f6f7f9; --fg: #1d2129; --muted: #6b7280; --line: #e3e6ea;
  --card: #ffffff; --accent: #2563eb; --accent-fg: #fff; --sel: #fff3bf; --sel-line: #f59e0b;
  --code-bg: #ffffff; --ln: #9aa3ad; --hover: #eef1f5; --danger: #dc2626;
  --sheet: #ffffff; --shadow: 0 -4px 24px rgba(0,0,0,.12);
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Noto Sans CJK SC", "Microsoft YaHei", sans-serif;
  --code-size: 13px;
  --safe-top: env(safe-area-inset-top, 0px); --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418; --fg: #e6e8eb; --muted: #9aa3ad; --line: #2a2f36;
    --card: #191d23; --accent: #60a5fa; --accent-fg: #0b1220; --sel: #3b3410; --sel-line: #f59e0b;
    --code-bg: #0d1117; --ln: #5c6670; --hover: #22272e; --danger: #f87171;
    --sheet: #191d23; --shadow: 0 -4px 24px rgba(0,0,0,.5);
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--fg); font: 15px/1.5 var(--sans); overscroll-behavior-y: none; }
#app { min-height: 100%; display: flex; flex-direction: column; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
button.icon { font-size: 20px; width: 40px; height: 40px; border-radius: 10px; line-height: 1; display: inline-flex; align-items: center; justify-content: center; }
button.icon:active, .btn:active { background: var(--hover); }
.btn { padding: 8px 14px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); font-size: 14px; white-space: nowrap; }
.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: transparent; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .5; }
input[type=text], textarea { font: inherit; color: var(--fg); background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; width: 100%; outline: none; }
input[type=text]:focus, textarea:focus { border-color: var(--accent); }
a { color: var(--accent); text-decoration: none; }
code { font-family: var(--mono); font-size: .92em; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
[hidden] { display: none !important; }

/* ---- top bar */
#topbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 4px; padding: calc(var(--safe-top) + 6px) 8px 6px; background: var(--bg); border-bottom: 1px solid var(--line); }
#title { flex: 1; font-weight: 600; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
#title .sub { font-weight: 400; color: var(--muted); font-size: 12px; display: block; line-height: 1.2; }
#actions { display: flex; gap: 2px; }
#view { flex: 1; padding-bottom: calc(var(--safe-bottom) + 80px); }

/* ---- generic lists */
.pad { padding: 14px 16px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 14px; margin: 0 16px 12px; }
.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; align-items: center; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--line); min-height: 48px; }
.list li:active { background: var(--hover); }
.list li .grow { flex: 1; min-width: 0; }
.list li .name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row { display: flex; gap: 8px; align-items: center; }
.tag { font-size: 11px; padding: 1px 6px; border-radius: 6px; background: var(--hover); color: var(--muted); }
.tag.method, .tag.function { color: #7c3aed; }
.tag.class, .tag.struct, .tag.interface, .tag.trait, .tag.enum, .tag.impl { color: #0891b2; }
.tag.module { color: #ca8a04; }

.radio { width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--line); flex: 0 0 auto; }
.radio.on { border-color: var(--accent); background: radial-gradient(var(--accent) 45%, transparent 50%); }

/* ---- tabs */
.tabs { display: flex; border-bottom: 1px solid var(--line); background: var(--bg); position: sticky; top: calc(var(--safe-top) + 53px); z-index: 10; }
.tabs button { flex: 1; padding: 10px 0; font-size: 14px; color: var(--muted); border-bottom: 2px solid transparent; }
.tabs button.on { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

/* ---- file tree */
.tree { padding: 6px 0; }
.tree .node { display: flex; align-items: center; gap: 6px; padding: 9px 12px; min-height: 42px; border-radius: 8px; }
.tree .node:active { background: var(--hover); }
.tree .node .caret { width: 18px; text-align: center; color: var(--muted); font-size: 12px; transition: transform .12s; }
.tree .node.open .caret { transform: rotate(90deg); }
.tree .node .ico { width: 20px; text-align: center; }
.tree .node .nm { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tree .node .sz { color: var(--muted); font-size: 11px; }
.tree .node.current { background: var(--sel); }
.tree .kids { display: none; }
.tree .node.open + .kids { display: block; }
.filterbox { padding: 8px 12px; position: sticky; top: calc(var(--safe-top) + 94px); background: var(--bg); z-index: 9; }

/* ---- markdown */
.md { padding: 6px 16px 16px; line-height: 1.6; word-break: break-word; }
.md h1 { font-size: 20px; } .md h2 { font-size: 17px; margin: 22px 0 8px; padding-bottom: 4px; border-bottom: 1px solid var(--line); } .md h3 { font-size: 15px; margin: 16px 0 6px; }
.md p { margin: 8px 0; } .md ul, .md ol { padding-left: 22px; margin: 6px 0; } .md li { margin: 3px 0; }
.md pre { background: var(--code-bg); border: 1px solid var(--line); border-radius: 8px; padding: 10px; overflow-x: auto; font-size: 12.5px; line-height: 1.45; }
.md :not(pre) > code { background: var(--hover); padding: 1px 5px; border-radius: 5px; }
.md table { border-collapse: collapse; display: block; overflow-x: auto; font-size: 13px; }
.md td, .md th { border: 1px solid var(--line); padding: 4px 8px; }
.md blockquote { margin: 8px 0; padding: 4px 12px; border-left: 3px solid var(--line); color: var(--muted); }
.md a.fp { border-bottom: 1px dashed var(--accent); }

/* ---- code view */
.codebar { display: flex; gap: 6px; align-items: center; padding: 6px 10px; border-bottom: 1px solid var(--line); background: var(--bg); position: sticky; top: calc(var(--safe-top) + 53px); z-index: 10; overflow-x: auto; }
.codebar .path { flex: 1; font-family: var(--mono); font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; direction: rtl; text-align: left; }
.codebar .path span { direction: ltr; unicode-bidi: bidi-override; }
.codebar button { padding: 4px 8px; border-radius: 8px; font-size: 13px; border: 1px solid var(--line); white-space: nowrap; }
.code { font-family: var(--mono); font-size: var(--code-size); line-height: 1.55; background: var(--code-bg); overflow-x: auto; padding-bottom: 20px; }
.code.wrap .lc { white-space: pre-wrap; word-break: break-all; }
.line { display: flex; min-height: 1.55em; }
.line .ln { flex: 0 0 auto; width: 3.6em; padding-right: .8em; text-align: right; color: var(--ln); user-select: none; -webkit-user-select: none; font-size: .85em; line-height: 1.82; position: sticky; left: 0; background: var(--code-bg); }
.line .lc { white-space: pre; padding-right: 16px; flex: 1; }
.line.sym-start .ln { color: var(--accent); font-weight: 600; }
.line.sel { background: var(--sel); }
.line.sel .ln { background: var(--sel); box-shadow: inset 3px 0 0 var(--sel-line); }
.line.flash { animation: flash 1.4s ease-out; }
@keyframes flash { 0% { background: var(--sel); } 100% { background: transparent; } }
.hljs { background: transparent; padding: 0; }
.more-lines { padding: 12px 16px 24px; color: var(--accent); font-size: 13px; font-family: var(--sans); cursor: pointer; }
.more-lines:empty { display: none; }

/* ---- action bar */
#actionbar { position: fixed; left: 12px; right: 12px; bottom: calc(var(--safe-bottom) + 14px); z-index: 30; display: flex; gap: 8px; align-items: center; background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 8px 8px 8px 14px; box-shadow: 0 6px 24px rgba(0,0,0,.18); }
#actionbar .info { flex: 1; min-width: 0; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#actionbar .info b { font-family: var(--mono); }

/* ---- sheets */
#sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.25); z-index: 40; }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; z-index: 50; background: var(--sheet); border-radius: 16px 16px 0 0; box-shadow: var(--shadow); display: flex; flex-direction: column; max-height: 92dvh; height: 58dvh; transition: height .18s; }
.sheet.full { height: 92dvh; }
.sheet.mini { height: 48px; overflow: hidden; }
body.chat-open #view { padding-bottom: 62dvh; }
body.chat-open #actionbar { bottom: calc(58dvh + 10px); }
body.chat-open:has(#chat-sheet.mini) #actionbar { bottom: 60px; }
.sheet-head { display: flex; align-items: center; gap: 6px; padding: 8px 8px 6px 16px; border-bottom: 1px solid var(--line); font-weight: 600; }
.sheet-head span { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sheet-head::before { content: ""; position: absolute; top: 6px; left: 50%; width: 36px; height: 4px; margin-left: -18px; border-radius: 2px; background: var(--line); }
.sheet-body { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
#outline-sheet .sheet-body { padding: 6px 0; }
#outline-sheet .oitem { display: flex; align-items: center; gap: 8px; padding: 9px 16px; min-height: 42px; font-family: var(--mono); font-size: 13px; }
#outline-sheet .oitem:active { background: var(--hover); }
#outline-sheet .oitem .nm { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#outline-sheet .oitem .ln { color: var(--muted); font-size: 11px; }

/* ---- chat */
.chat-msgs { padding: 10px 14px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 100%; }
.msg.user { align-self: flex-end; background: var(--accent); color: var(--accent-fg); padding: 8px 12px; border-radius: 14px 14px 4px 14px; max-width: 85%; white-space: pre-wrap; word-break: break-word; font-size: 14px; }
.msg.assistant { align-self: stretch; }
.msg.assistant .md { padding: 0; font-size: 14.5px; }
.msg.assistant .md pre { font-size: 12px; }
.msg.error { color: var(--danger); font-size: 13px; }
.msg.note { align-self: center; color: var(--muted); font-size: 12.5px; text-align: center; padding: 4px 10px; border: 1px dashed var(--line); border-radius: 10px; }
.msg .ctx { font-size: 12px; color: var(--muted); font-family: var(--mono); margin-bottom: 4px; }
.msg .ctx .ctx-text { margin-top: 4px; padding: 6px 8px; background: var(--hover); border-radius: 6px; white-space: pre-wrap; word-break: break-all; color: var(--fg); font-size: 11.5px; max-height: 96px; overflow: hidden; }
.code .lc::selection, .code .lc *::selection { background: #3b82f6; color: #fff; }
.code .lc { -webkit-user-select: text; user-select: text; -webkit-touch-callout: none; }
.tool-status { font-size: 12px; color: var(--muted); margin-top: 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tool-trace { font-size: 11.5px; color: var(--muted); margin-top: 8px; opacity: .8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.typing::after { content: "▍"; animation: blink 1s infinite; color: var(--muted); }
@keyframes blink { 50% { opacity: 0; } }
.chat-input { display: flex; gap: 6px; align-items: flex-end; padding: 8px 10px calc(var(--safe-bottom) + 8px); border-top: 1px solid var(--line); }
.chat-input textarea { flex: 1; resize: none; max-height: 120px; line-height: 1.4; overflow-y: hidden; }
.chat-input button { width: 40px; height: 40px; border-radius: 10px; background: var(--accent); color: var(--accent-fg); font-size: 18px; flex: 0 0 auto; }
.chat-input button.stop { background: var(--danger); color: #fff; font-size: 14px; }

#chat-sheet .resizer { display: none; }

/* ---- toast */
#toast { position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 90px); transform: translateX(-50%); background: #222; color: #fff; padding: 8px 14px; border-radius: 10px; font-size: 13px; z-index: 99; max-width: 90vw; }

/* ---- home */
.hero { padding: 24px 16px 8px; }
.hero h1 { margin: 0 0 4px; font-size: 22px; }
.openbox { display: flex; gap: 8px; padding: 8px 16px 12px; }
.empty { padding: 40px 16px; text-align: center; color: var(--muted); }
.spinner { display: inline-block; width: 16px; height: 16px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; vertical-align: -3px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- search */
.sres .file { padding: 8px 16px 2px; font-family: var(--mono); font-size: 12px; color: var(--accent); background: var(--hover); position: sticky; top: calc(var(--safe-top) + 94px + 52px); }
.sres .hit { padding: 6px 16px; font-family: var(--mono); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; border-bottom: 1px solid var(--line); }
.sres .hit .n { color: var(--muted); margin-right: 8px; }
.sres .hit mark { background: var(--sel); color: inherit; }

/* ---- 大屏适配 */
@media (min-width: 900px) {
  /* 列表类页面居中限宽;文件页保持全宽 */
  body[data-view="page"] #view { max-width: 1000px; margin: 0 auto; width: 100%; }
  body[data-view="page"] .tabs { max-width: 1000px; margin: 0 auto; }
  #topbar { padding-left: 16px; padding-right: 16px; }
  .hero { padding-top: 40px; }
  .md { font-size: 15.5px; }
  .sheet { left: 50%; transform: translateX(-50%); width: min(720px, 100%); border-radius: 16px 16px 0 0; }
}
@media (min-width: 1200px) {
  body[data-view="file"] #view { display: grid; grid-template-columns: 300px minmax(0, 1fr); padding-bottom: 0; }
  #side { position: sticky; top: calc(var(--safe-top) + 53px); height: calc(100dvh - var(--safe-top) - 53px); overflow-y: auto; border-right: 1px solid var(--line); background: var(--bg); }
  .side-tabs { display: flex; position: sticky; top: 0; background: var(--bg); border-bottom: 1px solid var(--line); z-index: 2; }
  .side-tabs button { flex: 1; padding: 8px 0; font-size: 13px; color: var(--muted); border-bottom: 2px solid transparent; }
  .side-tabs button.on { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
  #side .filterbox { top: 33px; padding: 6px 8px; }
  #side .tree .node { padding: 5px 8px; min-height: 30px; font-size: 13px; }
  #side .oitem { display: flex; align-items: center; gap: 8px; padding: 5px 12px; font-family: var(--mono); font-size: 12.5px; }
  #side .oitem:hover { background: var(--hover); cursor: pointer; }
  #side .oitem .nm { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  #side .oitem .ln { color: var(--muted); font-size: 11px; }
  #main { min-width: 0; padding-bottom: 80px; }
  #main .codebar { top: calc(var(--safe-top) + 53px); }
  .code { font-size: calc(var(--code-size) + 0.5px); }
  .line .ln { width: 4.2em; }
  #actionbar { left: 320px; right: auto; min-width: 420px; }
  body.split #actionbar { left: 320px; }
}

/* ---- tablet / desktop: chat as side panel */
@media (min-width: 900px) {
  :root { --chat-w: 440px; }
  body.split #view { margin-right: var(--chat-w); }
  body.split[data-view="page"] #view { max-width: none; margin-left: 0; width: auto; }
  body.split #chat-sheet { left: auto; right: 0; transform: none; width: var(--chat-w); top: calc(var(--safe-top) + 53px); height: auto; max-height: none; border-radius: 0; border-left: 1px solid var(--line); box-shadow: none; }
  body.split #chat-sheet .expand { display: none; }
  body.split #actionbar { right: calc(var(--chat-w) + 12px); bottom: calc(var(--safe-bottom) + 14px); }
  /* 左边缘拖动条 */
  body.split #chat-sheet .resizer { display: block; position: absolute; left: -4px; top: 0; bottom: 0; width: 9px; cursor: col-resize; z-index: 5; }
  body.split #chat-sheet .resizer::after { content: ""; position: absolute; left: 3px; top: 0; bottom: 0; width: 3px; background: transparent; transition: background .15s; }
  body.split #chat-sheet .resizer:hover::after, body.resizing #chat-sheet .resizer::after { background: var(--accent); }
  body.resizing { cursor: col-resize; user-select: none; -webkit-user-select: none; }
  body.resizing #chat-sheet { transition: none; }
  body.split #view { padding-bottom: 80px; }
  .sheet-head::before { display: none; }
}
