:root {
  --bg: #f7f7f5; --panel: #fff; --line: #e4e4e0; --text: #1d1d1b; --muted: #75756f;
  --accent: #2f5bd3; --accent-soft: #e8eefc; --danger: #c43d3d; --warn: #fff4d6; --ok: #2e7d4f;
  --mono: ui-monospace, SFMono-Regular, Menlo, monospace;
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--text); background: var(--bg);
}
* { box-sizing: border-box; }
body { margin: 0; height: 100vh; }
[hidden] { display: none !important; }
.muted { color: var(--muted); font-size: 12px; }
.spacer { flex: 1; }

.btn { font: inherit; border: 1px solid var(--line); background: var(--panel); color: var(--text);
  padding: 5px 12px; border-radius: 6px; cursor: pointer; text-decoration: none; white-space: nowrap; }
.btn:hover:not(:disabled) { border-color: #c9c9c3; background: #fafaf8; }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { background: #2649ad; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.danger { color: var(--danger); }
.btn.small { padding: 3px 9px; font-size: 13px; }

.login { display: grid; place-items: center; height: 100vh; }
.login-card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 32px 36px;
  max-width: 380px; text-align: center; }
.login-card h1 { margin: 0 0 8px; font-size: 20px; }
.login-card p { color: var(--muted); margin: 0 0 20px; }

.app { display: grid; height: 100vh;
  grid-template: "top top top" auto "side main comments" 1fr "git git git" auto / 270px 1fr 310px; }
.app.no-comments { grid-template-columns: 270px 1fr 0; }
.btn.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.topbar { grid-area: top; display: flex; align-items: center; gap: 10px; padding: 8px 14px;
  background: var(--panel); border-bottom: 1px solid var(--line); }
.brand { font-weight: 650; margin-right: 12px; }
.branch-picker { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; }
.branch-picker { position: relative; }
.branch-btn { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-width: 240px;
  max-width: 360px; text-align: left; }
.branch-btn #branch-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.branch-btn .caret { color: var(--muted); }
.branch-pop { position: absolute; top: calc(100% + 4px); left: 50px; z-index: 20; width: 380px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .12); color: var(--text); }
.branch-pop input { font: inherit; width: calc(100% - 16px); margin: 8px; padding: 6px 8px;
  border: 1px solid var(--line); border-radius: 6px; box-sizing: border-box; }
.branch-list { max-height: 360px; overflow-y: auto; padding: 0 4px 6px; }
.branch-list .group { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); padding: 8px 8px 4px; }
.branch-list .opt { padding: 5px 8px; border-radius: 5px; cursor: pointer; overflow-wrap: anywhere; }
.branch-list .opt.active { background: var(--accent-soft); }
.branch-list .opt.on { font-weight: 600; color: var(--accent); }
.branch-list > p { padding: 4px 8px; }
.me { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; }
.me img { width: 22px; height: 22px; border-radius: 50%; }

.sidebar { grid-area: side; border-right: 1px solid var(--line); background: var(--panel);
  display: flex; flex-direction: column; min-height: 0; }
.sidebar-actions { display: flex; gap: 6px; padding: 10px; border-bottom: 1px solid var(--line); }
.note { margin: 10px; padding: 8px 10px; background: var(--warn); border-radius: 6px; font-size: 12px; }
.tree-search { font: inherit; font-size: 13px; margin: 8px 10px 2px; padding: 5px 8px; border: 1px solid var(--line);
  border-radius: 6px; }
.tree mark { background: #fff0a8; color: inherit; border-radius: 2px; }
.tree { overflow: auto; padding: 6px 0 20px; flex: 1; font-size: 13px; }
.tree ul { list-style: none; margin: 0; padding-left: 14px; }
.tree > ul { padding-left: 4px; }
.tree .row { display: flex; align-items: center; gap: 4px; padding: 2px 6px; border-radius: 4px; cursor: pointer;
  user-select: none; }
.tree .row:hover { background: #f1f1ee; }
.tree .row.active { background: var(--accent-soft); color: var(--accent); }
.tree .row .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree .row .caret { width: 12px; color: var(--muted); font-size: 10px; }
.tree .row .badge { font-size: 10px; font-weight: 700; color: #b07800; }
.tree .row .menu { visibility: hidden; border: 0; background: none; cursor: pointer; color: var(--muted);
  padding: 0 4px; font-size: 14px; }
.tree .row:hover .menu { visibility: visible; }
.tree .collapsed > ul { display: none; }

.main { grid-area: main; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.file-bar { display: flex; align-items: center; gap: 10px; padding: 7px 12px; border-bottom: 1px solid var(--line);
  background: var(--panel); }
.file-path { font-family: var(--mono); font-size: 12.5px; }
.dirty { color: #b07800; font-size: 12px; }
.seg { display: flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.seg button { font: inherit; font-size: 12px; border: 0; background: var(--panel); padding: 3px 10px; cursor: pointer; }
.seg button.on { background: var(--accent-soft); color: var(--accent); }
.panes { flex: 1; display: grid; min-height: 0; }
.panes.split { grid-template-columns: 1fr 1fr; }
.panes.edit .preview, .panes.preview .editor { display: none; }
.editor { min-height: 0; overflow: hidden; border-right: 1px solid var(--line); background: var(--panel); }
.editor .cm-editor { height: 100%; font-size: 13px; }
.editor .cm-scroller { font-family: var(--mono); }
.cm-commented { background: #fff6cf; }
.cm-comment-gutter .cm-gutterElement { cursor: pointer; font-size: 11px; }

.preview { position: relative; overflow: auto; padding: 18px 26px 60px; background: #fdfdfc; }
.preview h1 { font-size: 22px; } .preview h2 { font-size: 18px; } .preview h3 { font-size: 15px; }
.preview code { font-family: var(--mono); font-size: 12.5px; background: #f1f1ee; padding: 1px 4px; border-radius: 3px; }
.preview pre { background: #f4f4f1; padding: 10px 12px; border-radius: 6px; overflow: auto; }
.preview pre code { background: none; padding: 0; }
.preview table { border-collapse: collapse; display: block; overflow-x: auto; }
.preview th, .preview td { border: 1px solid var(--line); padding: 4px 8px; text-align: left; vertical-align: top; }
.preview blockquote { margin: 0; padding-left: 12px; border-left: 3px solid var(--line); color: var(--muted); }
.frontmatter { font-size: 12.5px; background: var(--accent-soft); border-radius: 6px; padding: 8px 12px; margin-bottom: 16px; }
.frontmatter dt { font-weight: 600; } .frontmatter dd { margin: 0 0 6px; }

.comments { grid-area: comments; border-left: 1px solid var(--line); background: var(--panel);
  display: flex; flex-direction: column; min-height: 0; }
.comments-head { display: flex; justify-content: space-between; align-items: center; padding: 10px 12px;
  border-bottom: 1px solid var(--line); }
.comments-head select { font: inherit; font-size: 12px; }
.new-comment { padding: 10px 12px; border-bottom: 1px solid var(--line); display: grid; gap: 6px; }
.new-comment textarea, .thread textarea { font: inherit; font-size: 13px; width: 100%; resize: vertical;
  border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; }
.new-comment button { justify-self: end; }
.threads { overflow: auto; padding: 8px 10px 30px; display: grid; gap: 10px; align-content: start; }
.thread { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-size: 13px; }
.thread.focus { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.thread.resolved { opacity: .7; }
.thread .quote { font-family: var(--mono); font-size: 11.5px; color: var(--muted); border-left: 2px solid #e8c95a;
  padding-left: 6px; margin-bottom: 6px; cursor: pointer; white-space: pre-wrap; max-height: 4.5em; overflow: hidden; }
.thread .quote.outdated { border-left-color: var(--line); text-decoration: line-through; }
.thread .msg { margin: 6px 0; }
.thread .msg .who { font-weight: 600; font-size: 12px; }
.thread .msg .when { color: var(--muted); font-size: 11px; margin-left: 4px; }
.thread .msg .text { white-space: pre-wrap; }
.thread .actions { display: flex; gap: 6px; justify-content: flex-end; margin-top: 6px; }
.tag { font-size: 10.5px; padding: 1px 6px; border-radius: 10px; background: #eee; color: var(--muted); }
.tag.ok { background: #e3f3e8; color: var(--ok); }

.gitbar { grid-area: git; display: flex; align-items: center; gap: 8px; padding: 8px 14px;
  border-top: 1px solid var(--line); background: var(--panel); }

dialog { border: 1px solid var(--line); border-radius: 10px; padding: 18px 20px; width: min(760px, 92vw); }
dialog::backdrop { background: rgba(0,0,0,.25); }
dialog h3 { margin: 0 0 12px; font-size: 16px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
#dialog-body input, #dialog-body textarea, #dialog-body select { font: inherit; width: 100%; padding: 6px 8px; border: 1px solid var(--line);
  border-radius: 6px; }
#dialog-body label { display: block; font-size: 12px; color: var(--muted); margin: 8px 0 4px; }
.diff { font-family: var(--mono); font-size: 12px; background: #fbfbfa; border: 1px solid var(--line);
  border-radius: 6px; padding: 8px 0; max-height: 60vh; overflow: auto; white-space: pre; }
.diff div { padding: 0 10px; }
.diff .add { background: #e6f6ea; } .diff .del { background: #fbe9e9; } .diff .hunk { color: var(--accent); }
.diff .meta { color: var(--muted); font-weight: 600; }
.changes-list { list-style: none; padding: 0; margin: 0; }
.changes-list li { display: flex; align-items: center; gap: 8px; padding: 5px 0; border-bottom: 1px solid var(--line); }
.changes-list .st { font-family: var(--mono); font-weight: 700; width: 16px; }
.changes-list .p { flex: 1; font-family: var(--mono); font-size: 12.5px; }
.problems { color: var(--danger); padding-left: 18px; }

.toast { position: fixed; bottom: 60px; left: 50%; transform: translateX(-50%); background: #222; color: #fff;
  padding: 8px 14px; border-radius: 8px; font-size: 13px; max-width: 70vw; z-index: 10; }
.toast.error { background: var(--danger); }

/* AI edit */
.ai-btn { position: fixed; z-index: 30; font: inherit; font-size: 12px; padding: 3px 9px; border-radius: 6px;
  border: 1px solid var(--accent); background: var(--panel); color: var(--accent); cursor: pointer;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .12); }
.ai-btn:hover { background: var(--accent-soft); }
.ai-pop { position: fixed; z-index: 30; width: 460px; max-width: calc(100vw - 24px); max-height: 70vh;
  display: flex; flex-direction: column; gap: 8px; padding: 10px 12px; background: var(--panel);
  border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 10px 30px rgba(0, 0, 0, .16); }
.ai-head { display: flex; align-items: center; gap: 8px; }
.ai-head .btn { margin-left: auto; }
.ai-thread { margin: 0; padding-left: 18px; font-size: 12px; color: var(--muted); }
.ai-thread:empty { display: none; }
.ai-out { flex: 1 1 auto; min-height: 40px; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere;
  font-family: var(--mono); font-size: 12px; padding: 8px; border: 1px solid var(--line); border-radius: 6px;
  background: var(--bg); }
.ai-out.error { color: var(--danger); font-family: inherit; }
.ai-out ins { background: #d9f2df; color: #145c2c; text-decoration: none; }
.ai-out del { background: #fbe0e0; color: #8a1f1f; }
.ai-actions { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.ai-form textarea { font: inherit; width: 100%; box-sizing: border-box; padding: 6px 8px; resize: vertical;
  border: 1px solid var(--line); border-radius: 6px; }
.ai-form-row { display: flex; align-items: center; justify-content: space-between; margin-top: 4px; }
.cm-ai-range { background: rgba(47, 91, 211, .14); }

/* editor cursor/selection mirrored in the preview */
::highlight(mirror) { background: rgba(47, 91, 211, .22); }
.preview .mirror-block { position: relative; }
.preview .mirror-block::before { content: ""; position: absolute; left: -14px; top: 2px; bottom: 2px; width: 3px;
  border-radius: 2px; background: var(--accent); opacity: .45; }
.preview-caret { position: absolute; width: 2px; margin-left: -1px; background: var(--accent); pointer-events: none; }
.preview-caret.blink { animation: caret-blink 1.1s steps(1) infinite; }
@keyframes caret-blink { 50% { opacity: 0; } }
