:root {
  --bg: #eef1f5; --surface: #ffffff; --surface-2: #f4f6f9; --line: #dde2ea;
  --fg: #151a23; --muted: #5d6879; --accent: #1f6f78; --accent-soft: #dcefef; --accent-fg: #ffffff;
  --add: #1c7a43; --add-bg: #e1f3e8; --del: #b3372f; --del-bg: #fbe6e3; --mod: #8a6410; --mod-bg: #f8efd5;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1117; --surface: #171d27; --surface-2: #1e2633; --line: #2a3445;
    --fg: #e6ebf2; --muted: #92a0b5; --accent: #55c2c9; --accent-soft: #17383c; --accent-fg: #07181a;
    --add: #5fcf8a; --add-bg: #15301f; --del: #f08a82; --del-bg: #3a1d1b; --mod: #e4bd62; --mod-bg: #33290f;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg); font: 15px/1.5 var(--font); -webkit-text-size-adjust: 100%; }
a { color: var(--accent); }
button { font: inherit; color: inherit; cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

brain-app { display: block; height: 100%; max-width: 760px; margin: 0 auto; background: var(--surface); position: relative; }
.top { display: flex; align-items: center; gap: 10px; padding: calc(10px + env(safe-area-inset-top, 0px)) 16px 10px; border-bottom: 1px solid var(--line); min-height: 52px; }
.top h1 { margin: 0; font-size: 18px; font-weight: 650; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.top .back { background: none; border: 0; color: var(--accent); font-size: 22px; padding: 0 6px 0 0; line-height: 1; }
.badge { font-size: 11px; font-weight: 650; letter-spacing: .04em; padding: 3px 9px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); }
.badge.api { background: var(--mod-bg); color: var(--mod); }
.badge.busy::after { content: " ·"; animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .2; } }

main { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
brain-chat { display: block; height: 100%; }
.view { padding: 14px 16px 20px; display: flex; flex-direction: column; gap: 14px; }

nav.tabs { display: flex; border-top: 1px solid var(--line); background: var(--surface); padding: 6px 4px calc(8px + env(safe-area-inset-bottom, 0px)); }
nav.tabs a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 11px; color: var(--muted); text-decoration: none; position: relative; padding: 4px 0; }
nav.tabs a.on { color: var(--accent); font-weight: 650; }
nav.tabs svg { width: 22px; height: 22px; stroke: currentColor; fill: none; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
nav.tabs .dot { position: absolute; top: 0; left: calc(50% + 6px); min-width: 16px; height: 16px; border-radius: 8px; background: var(--accent); color: var(--accent-fg); font-size: 10px; font-weight: 650; display: grid; place-items: center; padding: 0 4px; }

.label { font-size: 11px; font-weight: 650; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.card { background: var(--surface-2); border: 1px solid var(--line); border-radius: 14px; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
textarea, input[type=search], input[type=text] { width: 100%; border: 1px solid var(--line); border-radius: 12px; background: var(--surface-2); color: var(--fg); font: inherit; padding: 10px 12px; resize: none; }
.card textarea { border: 0; background: transparent; padding: 2px; min-height: 52px; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.chip { border: 1px solid var(--line); background: var(--surface); color: var(--muted); border-radius: 999px; padding: 5px 12px; font-size: 13px; }
.send { margin-left: auto; width: 38px; height: 38px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-fg); font-size: 17px; }
.send:disabled { opacity: .5; }
.action, .item { display: flex; align-items: center; gap: 10px; padding: 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); width: 100%; text-align: left; text-decoration: none; color: var(--fg); }
.action .t, .item .t { flex: 1; min-width: 0; font-weight: 550; }
.action .t b, .item .t b { display: block; font-weight: 600; overflow-wrap: anywhere; }
.action .n, .item .t span { font-size: 12.5px; color: var(--muted); font-weight: 400; }
.undo { font-size: 13px; font-weight: 650; color: var(--accent); background: none; border: 0; padding: 4px 6px; }
.muted { color: var(--muted); font-size: 13px; }
.err { color: var(--del); font-size: 13px; }

/* layout shell */
.shell { display: flex; flex-direction: column; height: 100%; }
.col { flex: 1; min-width: 0; min-height: 0; display: flex; flex-direction: column; }

/* chat */
.chat { display: flex; flex-direction: column; height: 100%; }
.chat .log { flex: 1; overflow-y: auto; padding: 14px 16px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 90%; padding: 9px 13px; border-radius: 16px; overflow-wrap: anywhere; }
.msg.me { align-self: flex-end; background: var(--accent); color: var(--accent-fg); border-bottom-right-radius: 4px; white-space: pre-wrap; }
.msg.ai { align-self: flex-start; background: var(--surface-2); border-bottom-left-radius: 4px; }
.msg.ai > :first-child { margin-top: 0; } .msg.ai > :last-child { margin-bottom: 0; }
.tool { align-self: flex-start; font: 12px var(--mono); color: var(--muted); padding-left: 4px; overflow-wrap: anywhere; }
.writeback { align-self: stretch; text-align: center; font-size: 13px; font-weight: 550; color: var(--accent); background: none; border: 1px dashed var(--accent); border-radius: 10px; padding: 9px; }
.composer { display: flex; gap: 8px; align-items: flex-end; padding: 8px 12px 10px; border-top: 1px solid var(--line); }
.composer textarea { flex: 1; border-radius: 20px; max-height: 140px; }
.composer .send { margin: 0; }
.empty { color: var(--muted); text-align: center; padding: 24px 12px; }

/* wiki */
.wiki { display: flex; flex-direction: column; gap: 14px; }
.crumb { font: 12px var(--mono); color: var(--muted); overflow-wrap: anywhere; }
.crumb a { text-decoration: none; }
.tree { font: 13px/1.9 var(--mono); }
.tree details { margin-left: 0; } .tree summary { cursor: pointer; color: var(--muted); }
.tree .kids { padding-left: 16px; border-left: 1px solid var(--line); margin-left: 4px; }
.tree a { color: var(--fg); text-decoration: none; display: block; overflow-wrap: anywhere; }
.tree a.on { color: var(--accent); font-weight: 600; }
.doc h1 { font-size: 24px; line-height: 1.2; margin: 6px 0 10px; }
.doc h2 { font-size: 19px; margin: 20px 0 6px; } .doc h3 { font-size: 16px; margin: 16px 0 4px; }
.doc p, .doc ul, .doc ol { margin: 0 0 10px; } .doc ul, .doc ol { padding-left: 22px; }
.doc blockquote { margin: 0 0 10px; padding: 2px 12px; border-left: 3px solid var(--line); color: var(--muted); }
.doc code { font: 0.9em var(--mono); background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }
.doc pre { background: var(--surface-2); padding: 10px 12px; border-radius: 10px; overflow-x: auto; }
.doc pre code { background: none; padding: 0; }
.doc hr { border: 0; border-top: 1px solid var(--line); margin: 16px 0; }
.tbl { overflow-x: auto; margin-bottom: 10px; } .doc table { border-collapse: collapse; font-size: 14px; }
.doc th, .doc td { border: 1px solid var(--line); padding: 5px 9px; text-align: left; }
.wl { background: var(--accent-soft); color: var(--accent); border-radius: 6px; padding: 0 6px; text-decoration: none; font-size: .95em; }
.wl.missing { background: var(--surface-2); color: var(--muted); text-decoration: line-through dotted; }
.fm { font: 12px/1.6 var(--mono); background: var(--surface-2); border-radius: 10px; padding: 8px 10px; color: var(--muted); white-space: pre-wrap; }
.backs { border-top: 1px solid var(--line); padding-top: 10px; display: flex; flex-direction: column; gap: 6px; align-items: flex-start; }
.editor { min-height: 50vh; font: 13px/1.5 var(--mono); }
.hit { font-size: 13px; } .hit b { font: 600 12px var(--mono); display: block; color: var(--accent); }

/* diff / history */
.file { display: flex; align-items: center; gap: 8px; padding: 7px 10px; border-radius: 10px; font: 12.5px var(--mono); }
.file .k { width: 18px; height: 18px; border-radius: 5px; display: grid; place-items: center; font-weight: 700; flex: none; }
.file.A { background: var(--add-bg); } .file.A .k { color: var(--add); }
.file.M { background: var(--mod-bg); } .file.M .k { color: var(--mod); }
.file.D { background: var(--del-bg); } .file.D .k { color: var(--del); }
.file.R { background: var(--surface-2); } .file.R .k { color: var(--muted); }
.file span:last-child { min-width: 0; overflow-wrap: anywhere; }
.diff { border: 1px solid var(--line); border-radius: 10px; font: 12px/1.55 var(--mono); overflow-x: auto; }
.diff div { padding: 0 10px; white-space: pre; min-width: max-content; }
.diff .p { background: var(--add-bg); color: var(--add); } .diff .m { background: var(--del-bg); color: var(--del); }
.diff .h { background: var(--surface-2); color: var(--muted); }
.btn { border: 1px solid var(--line); border-radius: 12px; padding: 11px 14px; font-weight: 650; background: var(--surface); }
.btn.pri { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }
.btn:disabled { opacity: .5; }

.toast { position: absolute; left: 16px; right: 16px; bottom: calc(80px + env(safe-area-inset-bottom, 0px)); background: var(--fg); color: var(--bg); padding: 10px 14px; border-radius: 12px; font-size: 14px; text-align: center; z-index: 5; }

@media (min-width: 900px) {
  brain-app { max-width: 1080px; border-left: 1px solid var(--line); border-right: 1px solid var(--line); }
  .shell { flex-direction: row; }
  nav.tabs { order: -1; flex-direction: column; width: 92px; border-top: 0; border-right: 1px solid var(--line); padding: 16px 4px; gap: 8px; justify-content: flex-start; }
  nav.tabs a { padding: 10px 0; }
  .wiki { flex-direction: row; align-items: flex-start; }
  .wiki .side { width: 250px; flex: none; position: sticky; top: 0; max-height: calc(100vh - 100px); overflow-y: auto; }
  .wiki .page { flex: 1; min-width: 0; }
}
@media (max-width: 899px) { .wiki .side.hide-mobile { display: none; } }

[hidden] { display: none !important; }
@media (min-width: 900px) { nav.tabs a { flex: none; } }
