:root { --size: 20px; --measure: 36em; }
body[data-theme="paper"] { --bg: #fbfaf7; --panel: #f2f0ea; --ink: #23201b; --dim: #7b766c; --line: #e2ded4; --accent: #b4532a; --note: #fde9a8; }
body[data-theme="sepia"] { --bg: #f4ecd8; --panel: #ebe1c8; --ink: #3b2f20; --dim: #8a7a5f; --line: #dccfae; --accent: #9b4a1c; --note: #f3d98c; }
body[data-theme="night"] { --bg: #16171b; --panel: #1d1f24; --ink: #d9d6cf; --dim: #8a877f; --line: #2c2e34; --accent: #e3925c; --note: #5a4a1a; color-scheme: dark; }
body[data-font="serif"] { --text: "Literata", Georgia, serif; }
body[data-font="sans"] { --text: "Inter", system-ui, sans-serif; }
body[data-font="mono"] { --text: "IBM Plex Mono", ui-monospace, monospace; }
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { display: grid; grid-template-columns: 270px 1fr 0; background: var(--bg); color: var(--ink); font: 14px/1.45 Inter, system-ui, sans-serif; overflow: hidden; transition: grid-template-columns .25s; }
body.show-notes { grid-template-columns: 270px 1fr 280px; }
button, select, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.tiny { font-size: 12px; color: var(--dim); }

/* ---- binder ---- */
#side { background: var(--panel); border-right: 1px solid var(--line); overflow: auto; padding: 14px 12px 20px; display: flex; flex-direction: column; gap: 8px; }
.brand { display: flex; justify-content: space-between; align-items: baseline; font: 600 22px/1 "Literata", Georgia, serif; }
.classic { font: 12px Inter, sans-serif; color: var(--dim); }
#projects, .ptitle { width: 100%; padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); }
.ptitle { font: 600 16px "Literata", Georgia, serif; }
.goalrow { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--dim); }
.bar { flex: 1; height: 5px; background: var(--line); border-radius: 3px; overflow: hidden; --p: 0; }
.bar i { display: block; height: 100%; width: calc(var(--p) * 100%); background: var(--accent); }
.tiny input { width: 90px; padding: 2px 6px; border: 1px solid var(--line); border-radius: 4px; background: var(--bg); }
.tree, .tree ul { list-style: none; margin: 0; padding: 0; }
.tree ul { padding-left: 14px; }
.tree li.collapsed > ul { display: none; }
.row { display: flex; align-items: center; gap: 2px; border-radius: 6px; padding: 1px 2px; }
.row:hover { background: color-mix(in srgb, var(--ink) 6%, transparent); }
li.on > .row { background: color-mix(in srgb, var(--accent) 16%, transparent); }
.row .open { flex: 1; text-align: left; border: 0; background: none; padding: 5px 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .wc { font-size: 11px; color: var(--dim); }
.row .twist, .row .more { border: 0; background: none; color: var(--dim); width: 22px; padding: 2px; }
.row .twist { transition: transform .15s; transform: rotate(90deg); }
li.collapsed > .row .twist { transform: rotate(0); }
.row .more { opacity: 0; } .row:hover .more, .row:focus-within .more { opacity: 1; }
.row.drop-before { box-shadow: 0 -2px 0 var(--accent); } .row.drop-after { box-shadow: 0 2px 0 var(--accent); } .row.drop-inside { outline: 2px solid var(--accent); }
.rename { flex: 1; padding: 4px; border: 1px solid var(--accent); border-radius: 4px; background: var(--bg); }
.add { border: 1px dashed var(--line); background: none; border-radius: 6px; padding: 7px; color: var(--dim); }
.add:hover { color: var(--accent); border-color: var(--accent); }
#trash summary { color: var(--dim); cursor: pointer; font-size: 12px; }
.trashed { display: flex; gap: 6px; align-items: center; font-size: 12px; padding: 3px 0; }
.trashed span { flex: 1; }
.trashed button { border: 0; background: none; color: var(--accent); font-size: 11px; }
.find { margin-top: auto; }
#find { width: 100%; padding: 7px 9px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); }
#results { max-height: 40vh; overflow: auto; margin-top: 6px; }
#results button { display: grid; gap: 2px; width: 100%; text-align: left; border: 0; background: none; padding: 6px 4px; border-bottom: 1px solid var(--line); }
#results span { color: var(--dim); font-size: 12px; }
mark { background: var(--note); color: inherit; }
::highlight(find) { background: var(--note); }
#menu { position: fixed; z-index: 50; display: grid; min-width: 200px; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 10px 30px #0003; padding: 4px; }
#menu button { text-align: left; border: 0; background: none; padding: 7px 10px; border-radius: 5px; }
#menu button:hover, #menu button:focus { background: color-mix(in srgb, var(--accent) 14%, transparent); outline: none; }

/* ---- writing ---- */
#writing { display: grid; grid-template-rows: auto 1fr auto; min-width: 0; height: 100vh; overflow: hidden; }
.bar-top { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; padding: 8px 14px; border-bottom: 1px solid var(--line); }
.bar-top button, .bar-top summary { border: 1px solid transparent; background: none; border-radius: 6px; padding: 4px 9px; color: var(--dim); }
.bar-top button:hover, .bar-top summary:hover { color: var(--ink); border-color: var(--line); }
.views { display: flex; gap: 2px; background: var(--panel); border-radius: 8px; padding: 2px; }
.views button[aria-pressed="true"] { background: var(--bg); color: var(--ink); box-shadow: 0 1px 2px #0002; }
.tools { display: flex; gap: 2px; align-items: center; }
.chk { display: inline-flex; gap: 5px; align-items: center; color: var(--dim); font-size: 12px; }
.right { margin-left: auto; display: flex; gap: 2px; align-items: center; }
.drop { position: relative; } .drop summary { list-style: none; cursor: pointer; }
.drop .pop { position: absolute; right: 0; top: 110%; z-index: 20; display: grid; gap: 6px; min-width: 250px; padding: 12px; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 12px 30px #0003; }
.pop button, .pop .file { text-align: left; border: 0; background: none; padding: 5px 6px; border-radius: 5px; color: var(--ink); cursor: pointer; }
.pop button:hover, .pop .file:hover { background: var(--panel); }
.pop label { display: grid; gap: 3px; color: var(--dim); font-size: 12px; }
.pop hr { border: 0; border-top: 1px solid var(--line); width: 100%; }
body:not([data-view="write"]) .write-only, body:not([data-view="outline"]) .outline-only, body:not([data-view="cork"]) .cork-only { display: none !important; }

#page { overflow: auto; padding: 6vh 24px 40vh; }
.doc { max-width: var(--measure); margin: 0 auto 4em; }
.doc[data-depth="1"] { --i: 1; } .doc[data-depth="2"] { --i: 2; }
.doc-title { width: 100%; border: 0; background: none; font: 600 calc(var(--size) * 1.45)/1.2 var(--text); color: var(--ink); padding: 0; margin-bottom: .6em; outline: none; }
.doc + .doc { border-top: 1px solid var(--line); padding-top: 2.5em; }
.text { font: var(--size)/1.7 var(--text); outline: none; min-height: 40vh; caret-color: var(--accent); hyphens: auto; }
.text p { margin: 0 0 .9em; }
.text h1, .text h2, .text h3 { font-family: var(--text); line-height: 1.25; margin: 1.4em 0 .5em; }
.text blockquote { margin: 1em 0; padding-left: 1em; border-left: 3px solid var(--line); color: var(--dim); font-style: italic; }
.text hr { border: 0; text-align: center; margin: 2em 0; } .text hr::after { content: "* * *"; color: var(--dim); letter-spacing: .5em; }
.note { background: color-mix(in srgb, var(--note) 70%, transparent); border-bottom: 2px solid color-mix(in srgb, var(--accent) 60%, transparent); cursor: pointer; }
.note.lit { background: var(--note); }
body.focus .text > * { opacity: .28; transition: opacity .25s; }
body.focus .text > .current, body.focus .text:not(:focus-within) > * { opacity: 1; }
.empty { text-align: center; color: var(--dim); margin-top: 20vh; }

.bar-bottom { display: flex; gap: 14px; align-items: center; padding: 6px 16px; border-top: 1px solid var(--line); font-size: 12px; color: var(--dim); }
.ring { --p: 0; width: 16px; height: 16px; border-radius: 50%; background: conic-gradient(var(--accent) calc(var(--p) * 360deg), var(--line) 0); display: grid; place-items: center; }
.ring i { width: 10px; height: 10px; border-radius: 50%; background: var(--bg); }

/* ---- notes column ---- */
#notes { overflow: auto; padding: 14px 12px; background: var(--panel); border-left: 1px solid var(--line); }
body:not(.show-notes) #notes { visibility: hidden; padding: 0; }
.notecard { position: relative; background: var(--bg); border: 1px solid var(--line); border-left: 3px solid var(--accent); border-radius: 6px; padding: 8px 10px; margin-bottom: 10px; }
.notecard blockquote { margin: 0 0 6px; color: var(--dim); font: italic 13px/1.4 var(--text); }
.notecard textarea { width: 100%; border: 0; background: none; resize: vertical; outline: none; }
.notecard .del { position: absolute; top: 4px; right: 6px; border: 0; background: none; color: var(--dim); }
.hint { color: var(--dim); font-size: 13px; }
kbd { border: 1px solid var(--line); border-radius: 3px; padding: 0 4px; font-size: 11px; }

/* ---- outline & corkboard ---- */
#outline, #cork { overflow: auto; padding: 20px; }
#outline table { width: 100%; max-width: 1100px; margin: 0 auto; border-collapse: collapse; }
#outline th { text-align: left; color: var(--dim); font-weight: 500; font-size: 12px; padding: 6px; border-bottom: 1px solid var(--line); }
#outline td { padding: 4px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
#outline .open, .card .open { border: 0; background: none; padding: 4px 0; font-weight: 600; text-align: left; }
#outline textarea, .card textarea { width: 100%; border: 1px solid transparent; background: none; resize: vertical; border-radius: 4px; padding: 4px; }
#outline textarea:focus, .card textarea:focus { border-color: var(--line); background: var(--bg); outline: none; }
#outline .num { text-align: right; color: var(--dim); }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px; max-width: 1200px; margin: 0 auto; }
.card { background: #fffef6; color: #2b261d; border-radius: 4px; padding: 10px 12px; box-shadow: 0 2px 6px #0002, 0 12px 20px -14px #0006; border-top: 5px solid #ccc; cursor: grab; transform: rotate(-.4deg); }
.card:nth-child(2n) { transform: rotate(.5deg); }
.card header { display: flex; justify-content: space-between; align-items: baseline; gap: 6px; }
.card header span { font-size: 11px; color: #8a8270; }
.card.status-idea { border-top-color: #9ab3d8; } .card.status-draft { border-top-color: #e2b65c; } .card.status-revised { border-top-color: #d98a6a; } .card.status-final { border-top-color: #7fb38a; }
.card textarea { color: #2b261d; font: 14px/1.45 "Literata", Georgia, serif; }

dialog#history { width: min(900px, 92vw); border: 1px solid var(--line); border-radius: 12px; background: var(--bg); color: var(--ink); padding: 18px; }
dialog::backdrop { background: #0006; }
#history .cols { display: grid; grid-template-columns: 280px 1fr; gap: 14px; max-height: 60vh; }
#history .list { overflow: auto; display: grid; align-content: start; gap: 4px; }
#history .list button { text-align: left; border: 1px solid var(--line); background: none; border-radius: 6px; padding: 6px 8px; font-size: 12px; }
#history .preview { overflow: auto; font: 16px/1.6 var(--text); padding: 0 10px; border-left: 1px solid var(--line); }
#history .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; }
#history .actions button { border: 1px solid var(--line); background: var(--panel); border-radius: 6px; padding: 6px 12px; }
#history .restore { background: var(--accent) !important; color: #fff; border: 0 !important; }
#toast { position: fixed; left: 50%; bottom: 40px; transform: translateX(-50%); max-width: 560px; background: var(--ink); color: var(--bg); padding: 9px 16px; border-radius: 10px; opacity: 0; transition: opacity .3s; pointer-events: none; z-index: 60; }
#toast.on { opacity: 1; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

@media (max-width: 860px) {
  body, body.show-notes { grid-template-columns: 1fr; grid-template-rows: auto 1fr; overflow: auto; }
  #side { max-height: 38vh; border-right: 0; border-bottom: 1px solid var(--line); }
  #writing { height: auto; min-height: 62vh; }
  #notes { display: none; } body.show-notes #notes { display: block; visibility: visible; }
  .tools .chk { display: none; }
}
