/* Terminal / phosphor: all-mono chrome, square corners, one green accent.
   Dense old-school metrics kept — 1px borders, tight spacing, no chrome.
   Reference mockup: tmp/terminal.html */

:root {
  color-scheme: light;
  --bg: #f4f5f0;
  --fg: #20261f;
  --muted: #6a7269;
  --border: #c3cabe;
  --border-strong: #9aa394;
  --accent: #0c7c3f;
  --accent-soft: #dff0e2;
  --accent-visited: #456352;
  --bg-alt: #eaece4;
  --bg-hover: #e0e4d8;
  --ok: #0c7c3f;
  --err: #b3261e;
  --diff-add: #dcefdd;
  --diff-del: #f6dcd7;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --topbar-h: 30px;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0c100d;
  --fg: #bac7ba;
  --muted: #67766a;
  --border: #26302a;
  --border-strong: #3b4a40;
  --accent: #41e07f;
  --accent-soft: #10281a;
  --accent-visited: #7fae91;
  --bg-alt: #111611;
  --bg-hover: #18211a;
  --ok: #41e07f;
  --err: #ff6b5e;
  --diff-add: #12291a;
  --diff-del: #38160f;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0c100d;
    --fg: #bac7ba;
    --muted: #67766a;
    --border: #26302a;
    --border-strong: #3b4a40;
    --accent: #41e07f;
    --accent-soft: #10281a;
    --accent-visited: #7fae91;
    --bg-alt: #111611;
    --bg-hover: #18211a;
    --ok: #41e07f;
    --err: #ff6b5e;
    --diff-add: #12291a;
    --diff-del: #38160f;
  }
}

* { box-sizing: border-box; }

html { height: 100%; }
body {
  margin: 0;
  height: 100%;
  overflow: hidden; /* app shell: the panes scroll, never the page */
  font: 13px/1.4 var(--mono);
  background: var(--bg);
  color: var(--fg);
}
body.blog { overflow: auto; }

/* cross-document cross-fade on full-page navigations (htmx swaps unaffected);
   topbar and tree pane are paired separately so the static chrome doesn't fade */
@view-transition { navigation: auto; }
::view-transition-old(root), ::view-transition-new(root) { animation-duration: 130ms; }
#topbar { view-transition-name: topbar; }
#tree-pane { view-transition-name: tree-pane; }
@media (prefers-reduced-motion: reduce) {
  @view-transition { navigation: none; }
  ::view-transition-group(*), ::view-transition-old(*), ::view-transition-new(*) { animation: none !important; }
  #llm-panel .msg, .tool-card { animation: none !important; }
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
a:visited { color: var(--accent-visited); }
.muted { color: var(--muted); }
.error { color: var(--err); }
.ok { color: var(--ok); }

:focus-visible { outline: 1px solid var(--accent); outline-offset: 1px; }

/* ---- top bar ---- */
#topbar {
  display: flex;
  align-items: center;
  gap: 6px;
  height: var(--topbar-h);
  padding: 0 8px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-alt);
  z-index: 10;
}
#topbar .brand { font-weight: bold; color: var(--fg); padding: 2px 4px; }
#topbar .brand::before { content: "▮ "; color: var(--accent); }
#topbar .brand:hover { text-decoration: none; }
#quick-actions { display: flex; align-items: center; gap: 4px; margin-left: auto; }
#quick-actions a { padding: 2px 4px; }

button, input[type="submit"] {
  font: inherit;
  color: var(--fg);
  background: transparent;
  border: 1px solid var(--border-strong);
  border-radius: 0;
  padding: 1px 8px;
  cursor: pointer;
}
button:hover, input[type="submit"]:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-soft);
}
button.primary { color: var(--accent); border-color: var(--accent); font-weight: bold; }
button.danger { color: var(--err); }
button.danger:hover { border-color: var(--err); background: transparent; }
button.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
/* shortcut hints in labels: <u>x</u> = press x, <u class="mod">x</u> = ctrl+x */
button u, .key-legend u {
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}
button u.mod, .key-legend u.mod {
  text-decoration-style: double;
  font-weight: bold;
}

input[type="text"], input[type="password"], input[type="search"], input[type="url"], select, textarea {
  font: inherit;
  color: var(--fg);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 0;
  padding: 1px 6px;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); outline: none; }

#drawer-toggle { display: none; padding: 1px 8px; }

/* ---- layout ---- */
#layout { display: flex; height: calc(100% - var(--topbar-h)); }
#tree-pane {
  width: var(--tree-w, 240px);
  min-width: 160px;
  max-width: 480px;
  border-right: 1px solid var(--border);
  background: var(--bg-alt);
  overflow-y: auto;
  padding: 2px 0;
}
#main { flex: 1; overflow-y: auto; padding: 6px 10px; min-width: 0; background: var(--bg); }
/* layout pane resize handles (the editor's #split-handle keeps its own rule) */
.pane-handle { flex: none; width: 6px; cursor: col-resize; background: var(--border); touch-action: none; }
.pane-handle:hover, .pane-handle.dragging { background: var(--accent); }

@media (max-width: 767px) {
  #drawer-toggle { display: block; }
  #tree-pane {
    display: none;
    position: fixed;
    top: var(--topbar-h); left: 0; bottom: 0;
    width: 85%;
    max-width: 300px;
    background: var(--bg);
    z-index: 20;
    border-right: 1px solid var(--border);
  }
  body.drawer-open #tree-pane { display: block; }
  #tree-handle { display: none; } /* drawer mode has no resizable edge */
  /* keep buttons visually small but give a bigger hit area */
  #quick-actions button, #quick-actions a, #drawer-toggle { min-height: 28px; min-width: 34px; }
}

/* ---- tree ---- */
#tree-filter {
  display: block;
  width: calc(100% - 12px);
  margin: 3px 6px 4px;
}
.tree { list-style: none; margin: 0; padding: 0; }
.tree li[hidden] { display: none; }
.tree li {
  display: flex;
  align-items: center;
  gap: 3px;
  height: 22px;
  padding-right: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: default;
}
.tree li:hover { background: var(--bg-hover); }
.tree li.active { background: var(--accent-soft); box-shadow: inset 2px 0 0 var(--accent); }
.tree li.active a { color: var(--accent); font-weight: bold; }
.tree li a { color: var(--fg); overflow: hidden; text-overflow: ellipsis; flex: 1; }
.tree li a:hover { text-decoration: none; }
.tree li a:visited { color: var(--fg); }
.tree .twisty { width: 14px; text-align: center; cursor: pointer; color: var(--muted); flex: none; font-size: 10px; }
.tree li.trashed a { color: var(--muted); }
.tree li.trash-root { border-top: 1px solid var(--border); margin-top: 4px; padding-top: 2px; height: 25px; }
.tree li.trash-root .label { color: var(--muted); font-weight: bold; }
.tree li.drop-into { outline: 1px dashed var(--accent); outline-offset: -1px; }
.tree li.drop-before { box-shadow: 0 -2px 0 0 var(--accent); }
.tree li.drop-after { box-shadow: 0 2px 0 0 var(--accent); }
.tree li.dragging { opacity: 0.5; }
/* custom drag image chip (parked off-viewport; snapshot taken on dragstart) */
#drag-chip {
  position: fixed;
  top: -100px;
  left: -100px;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 1px 8px;
  font: 12px/1.6 var(--mono);
  color: var(--accent);
  background: var(--bg);
  border: 1px solid var(--accent);
  pointer-events: none;
}

/* ---- forms / tables ---- */
form.stack { display: flex; flex-direction: column; gap: 6px; max-width: 420px; }
form.stack label { display: flex; flex-direction: column; gap: 2px; }
table { border-collapse: collapse; }
th, td { border: 1px solid var(--border); padding: 2px 6px; text-align: left; }
th { background: var(--bg-alt); color: var(--accent); }

/* ---- badges ---- */
.badge {
  font-size: 11px;
  padding: 0 5px;
  border: 1px solid var(--muted);
  color: var(--muted);
  align-self: center;
  white-space: nowrap;
}
.badge.trash-badge { border-color: var(--err); color: var(--err); }
.badge.draft-badge { border-color: var(--muted); color: var(--muted); }
.badge.stable-badge { border-color: var(--accent); color: var(--accent); }

/* ---- note view / editor ---- */
.note-head { display: flex; align-items: baseline; gap: 8px; border-bottom: 1px solid var(--border); padding-bottom: 4px; margin-bottom: 6px; flex-wrap: wrap; }
.note-head h1 { font-size: 16px; margin: 0; }
.note-head h1::before { content: "# "; color: var(--accent); }
.note-head .tools { margin-left: auto; display: flex; gap: 4px; flex-wrap: wrap; }

/* edit mode fills the pane exactly; each half owns its own scrollbar */
#main:has(#editor-form) { overflow: hidden; display: flex; flex-direction: column; }
#main:has(#editor-form) #note-container { flex: 1; min-height: 0; display: flex; flex-direction: column; }

/* view mode: the note frame fills the pane immediately via CSS (no
   collapse-then-expand JS resize); header/attachments keep natural height */
#main:has(#note-frame) { overflow: hidden; display: flex; flex-direction: column; }
#main:has(#note-frame) #note-container { flex: 1; min-height: 0; display: flex; flex-direction: column; }

/* history: same CSS fill as view mode — one version frame shown at a time */
#main:has(.history-layout) { overflow: hidden; display: flex; flex-direction: column; }
#main:has(.history-layout) #note-container { flex: 1; min-height: 0; display: flex; flex-direction: column; }

#editor-form { display: flex; flex-direction: column; gap: 4px; flex: 1; min-height: 0; }
#editor-form .row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
#editor-msg { font-size: 12px; }
#editor-msg .ok::after { content: "▌"; animation: blink 1.1s steps(1) infinite; margin-left: 2px; }
@keyframes blink { 50% { opacity: 0; } }

.editor-body { display: flex; gap: 0; align-items: stretch; flex: 1; min-height: 0; }
.editor-body textarea {
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.45;
  flex: 0 1 var(--split, 50%);
  min-width: 0;
  min-height: 0;
  resize: none;
  overflow-y: auto;
  tab-size: 4;
  background: var(--bg-alt);
}
#split-handle {
  flex: none;
  width: 6px;
  margin: 0 1px;
  cursor: col-resize;
  background: var(--border);
  touch-action: none;
}
#split-handle:hover, #split-handle.dragging { background: var(--accent); }
.editor-body textarea.drop-target { outline: 1px dashed var(--accent); outline-offset: -2px; }
#editor-preview {
  flex: 1 1 0;
  min-width: 0;
  border: 1px solid var(--border);
  padding: 0; /* padding lives inside the frame body */
  overflow: hidden; /* the frame's own viewport scrolls */
  background: var(--bg);
}

/* sandboxed note frames (editor preview + rendered view + history) — all
   CSS-sized, no JS measures or sets frame height */
iframe.note-frame { display: block; width: 100%; border: 0; background: var(--bg); }
#editor-preview iframe.note-frame { height: 100%; }
#note-frame { flex: 1; min-height: 0; height: auto; }
#version-pane iframe.note-frame { flex: 1; min-height: 0; height: auto; }

/* the document INSIDE an md frame (frame_document links app.css back in):
   undo the app-shell body lock so the frame viewport scrolls/measures */
html.note-frame { height: auto; }
html.note-frame body { height: auto; overflow: auto; margin: 0; padding: 4px 10px; }
#editor-form.preview-hidden #editor-preview { display: none; }
#editor-form.preview-hidden #split-handle { display: none; }
#editor-form.preview-hidden .editor-body textarea { flex: 1; }
@media (max-width: 1000px) {
  .editor-body { flex-direction: column; gap: 8px; }
  #split-handle { display: none; }
  .editor-body textarea { width: 100%; flex: 1 1 50%; }
  #editor-preview { width: 100%; flex: 1 1 50%; }
}

/* ---- rendered note content ---- */
.note-content { max-width: 52rem; }
.note-content h1, .note-content h2, .note-content h3 { margin: 0.7em 0 0.3em; line-height: 1.2; }
.note-content h1 { font-size: 17px; } .note-content h2 { font-size: 15px; } .note-content h3 { font-size: 14px; }
.note-content h1::before { content: "# "; color: var(--accent); }
.note-content p, .note-content ul, .note-content ol { margin: 0.4em 0; }
.note-content pre {
  background: var(--bg-alt);
  border: 1px solid var(--border);
  padding: 4px 6px;
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.4;
}
.note-content code { font-family: var(--mono); font-size: 0.95em; background: var(--bg-alt); padding: 0 3px; }
.note-content pre code { background: none; padding: 0; }
.note-content blockquote { margin: 0.4em 0; padding: 1px 10px; border-left: 3px solid var(--accent); color: var(--muted); }
.note-content img { max-width: 100%; }
.note-content table { margin: 0.4em 0; }
/* diagram source, shown only while Kroki hasn't rendered it */
.note-content pre.mermaid { background: var(--bg); }

/* server-rendered mermaid: one <img> per theme, shown to match the page */
.mermaid-rendered { text-align: center; margin: 0.4em 0; }
.mermaid-rendered img { max-width: 100%; }
:root[data-theme="dark"] .diagram-light { display: none; }
:root:not([data-theme="dark"]) .diagram-dark { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) .diagram-light { display: none; }
  :root:not([data-theme]) .diagram-dark { display: inline; }
}

/* ---- history timeline ---- */
.history-layout { display: flex; gap: 10px; align-items: stretch; flex: 1; min-height: 0; }
.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
  width: 220px;
  flex: none;
  border: 1px solid var(--border);
  overflow-y: auto;
}
.history-list a {
  display: flex;
  gap: 5px;
  align-items: baseline;
  padding: 2px 8px;
  color: var(--fg);
  white-space: nowrap;
  font-size: 12px;
  border-bottom: 1px solid var(--border);
}
.history-list li:last-child a { border-bottom: 0; }
.history-list a:hover { background: var(--bg-hover); text-decoration: none; }
.history-list a:visited { color: var(--fg); }
.history-list a.active { background: var(--accent-soft); color: var(--accent); box-shadow: inset 2px 0 0 var(--accent); }
#version-pane { flex: 1; min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.version-meta { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; flex: none; }
@media (max-width: 767px) {
  .history-layout { flex-direction: column; }
  .history-list { width: 100%; max-height: 30vh; }
}

/* ---- search ---- */
#search-box { width: 100%; }
.search-hit { border-bottom: 1px solid var(--border); padding: 4px 0; }
.search-hit b { background: var(--diff-add); }
.search-hit .title { font-weight: bold; }

/* ---- diff preview ---- */
.diff { font-family: var(--mono); font-size: 12.5px; border: 1px solid var(--border); overflow-x: auto; }
.diff .line { display: flex; white-space: pre; }
.diff .ln { color: var(--muted); min-width: 3.2em; text-align: right; padding-right: 6px; user-select: none; flex: none; }
.diff .add { background: var(--diff-add); }
.diff .del { background: var(--diff-del); }
.diff .ctx-skip { color: var(--muted); background: var(--bg-alt); padding: 0 6px; }
.diff ins { background: var(--diff-add); text-decoration: none; font-weight: bold; }
.diff del { background: var(--diff-del); }

/* ---- llm panel ---- */
/* participates in the #layout flex row: content narrows instead of being
   covered, so approve/deny controls in #note-container stay visible */
#llm-slot { display: contents; }
#llm-panel {
  border-left: 1px solid var(--border);
  background: var(--bg);
  width: var(--llm-w, 380px);
  min-width: 280px;
  max-width: 45vw;
  flex: none;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  padding: 4px 6px;
  position: relative;
}
/* left-edge resize handle; invisible until hovered (border-left is the divider) */
#llm-handle { position: absolute; left: 0; top: 0; bottom: 0; width: 6px; cursor: col-resize; touch-action: none; }
#llm-handle:hover, #llm-handle.dragging { background: var(--accent); }
@media (max-width: 900px) {
  /* too narrow to squeeze: fall back to an overlay */
  #llm-panel {
    position: fixed;
    top: var(--topbar-h); right: 0; bottom: 0;
    max-width: 95vw;
    z-index: 15;
  }
  #llm-handle { display: none; } /* overlay mode: not resizable */
}
#llm-panel .messages { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; }
#llm-panel .msg { border: 1px solid var(--border); padding: 3px 5px; white-space: pre-wrap; overflow-wrap: break-word; }
#llm-panel .msg.user { background: var(--bg-alt); }
#llm-panel .msg.error { color: var(--err); }
#llm-panel form { display: flex; gap: 4px; margin-top: 4px; }
#llm-panel textarea { flex: 1; height: 3em; }
#llm-panel.busy #llm-chat-form button { opacity: 0.6; }
/* thinking placeholder: assistant bubble with a rotating ASCII spinner,
   cycled from JS in chat.js (CSS `content` isn't reliably animatable) */
#llm-thinking { color: var(--muted); min-height: 1.4em; }
.spin { display: inline-block; width: 1ch; }

/* tool-call cards in the chat */
.tool-card { border: 1px solid var(--border-strong); padding: 3px 5px; }
.tool-card .tool-head { display: flex; gap: 6px; align-items: baseline; flex-wrap: wrap; }
.tool-card .tool-name { color: var(--accent); font-weight: bold; }
.tool-card .tool-status { margin-left: auto; font-size: 11px; color: var(--muted); }
.tool-card.pending { border-color: var(--accent); }
.tool-card.pending .tool-status { color: var(--accent); }
.tool-card.failed .tool-status, .tool-card.denied .tool-status { color: var(--err); }
.tool-card .diff { max-height: 40vh; overflow-y: auto; margin-top: 3px; }

/* entrance animation: every chat item (own local echo, streamed assistant
   bubble, tool card, and its outerHTML-replaced status updates) is a freshly
   inserted DOM node — the animation just plays on insert, no JS needed */
@keyframes msg-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}
#llm-panel .msg, .tool-card { animation: msg-in 160ms ease-out; }

/* ---- blog ---- */
body.blog #main { max-width: 46rem; margin: 0 auto; padding: 12px; }
body.blog .blog-nav {
  max-width: 46rem;
  margin: 0 auto;
  padding: 10px 12px 6px;
  display: flex;
  align-items: baseline;
  gap: 6px;
  border-bottom: 1px solid var(--border);
}
body.blog .blog-nav .brand { font-weight: bold; color: var(--fg); }
body.blog .blog-nav .brand::before { content: "▮ "; color: var(--accent); }
body.blog .blog-nav .brand:hover { text-decoration: none; }
body.blog .blog-nav nav { margin-left: auto; }
.post-list { list-style: none; margin: 0; padding: 0; }
.post-item { padding: 6px 0; border-bottom: 1px solid var(--border); }
.post-item .date { margin-right: 8px; }
.post-item .title { font-weight: bold; }
.post-item .excerpt { margin: 2px 0 0; }
.post .post-date { margin-top: -2px; }
.post-back { margin-top: 16px; }
body.blog #search-box { width: 100%; }
body.blog #search-results { margin-top: 8px; }

/* ---- htmx indicators / llm ---- */
.htmx-indicator { display: none; }
.htmx-request .htmx-indicator, .htmx-request.htmx-indicator { display: inline; }
#llm-panel .summary { white-space: pre-wrap; border: 1px solid var(--border); padding: 4px 6px; margin: 4px 0; }
#llm-panel hr { margin: 6px 0; }
.diff .content { white-space: pre-wrap; word-break: break-word; }

/* ---- auth pages ---- */
.auth-page { max-width: 360px; margin: 8vh auto 0; padding: 0 12px; }
.auth-page h1 { font-size: 16px; }
.auth-page h1::before { content: "# "; color: var(--accent); }
.auth-page .qr { background: #fff; display: inline-block; padding: 6px; border: 1px solid var(--border); }

/* ---- misc ---- */
hr { border: 0; border-top: 1px solid var(--border); }
.attachments { margin-top: 8px; }
.toolbar { display: flex; gap: 4px; align-items: center; flex-wrap: wrap; margin: 4px 0; }
dialog { border: 1px solid var(--border); background: var(--bg); color: var(--fg); padding: 8px; min-width: 280px; }
dialog::backdrop { background: rgba(0, 0, 0, 0.35); }
dialog input[type="text"] { width: 100%; margin-bottom: 4px; }
