:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --line: #e3e6ea;
  --text: #1f2328;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-soft: #e8efff;
  --danger: #c62828;
  --warn: #b45309;
  --ok: #15803d;
  --mark: #fff1a8;
  --radius: 6px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  color: var(--text);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); }
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.warn { color: var(--warn); }
.pad { padding: 6px 10px; }
.right { margin-left: auto; }
mark { background: var(--mark); color: inherit; padding: 0 1px; border-radius: 2px; }

.btn { border: 1px solid var(--line); background: var(--panel); border-radius: var(--radius); padding: 5px 12px; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: #c5cbd3; background: #fafbfc; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #1d4fd8; }
.btn.danger { color: var(--danger); }
.btn.small { padding: 2px 8px; font-size: 12px; }
.icon-btn { border: 0; background: transparent; cursor: pointer; padding: 4px 7px; border-radius: var(--radius); font-size: 16px; line-height: 1; color: var(--muted); }
.icon-btn:hover { background: #eceef1; color: var(--text); }
.icon-btn.small { font-size: 12px; padding: 2px 5px; }

/* layout */
#app { display: grid; grid-template-columns: 250px 340px 1fr; grid-template-rows: 48px 1fr; height: 100vh; }
.top { grid-column: 1 / -1; display: flex; align-items: center; gap: 8px; padding: 0 12px; background: var(--panel); border-bottom: 1px solid var(--line); }
.brand { font-weight: 700; font-size: 17px; letter-spacing: .3px; margin-right: 14px; }
.search { flex: 0 1 460px; }
.search input { width: 100%; padding: 6px 10px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--bg); }
.search input:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); background: #fff; }
.spacer { flex: 1; }

.side { background: var(--panel); border-right: 1px solid var(--line); overflow-y: auto; padding: 8px 0 24px; }
.views { display: flex; flex-direction: column; padding: 0 6px 6px; border-bottom: 1px solid var(--line); }
.view { padding: 5px 10px; border-radius: var(--radius); text-decoration: none; color: var(--text); }
.view:hover { background: #f0f2f5; }
.view.active { background: var(--accent-soft); color: var(--accent); }
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px 4px; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .5px; }
.tree, .tags { padding: 0 6px; }
.folder, .tag-row { display: flex; align-items: center; gap: 4px; padding: 3px 6px; border-radius: var(--radius); cursor: pointer; user-select: none; min-height: 26px; }
.folder:hover, .tag-row:hover { background: #f0f2f5; }
.folder.active, .tag-row.active { background: var(--accent-soft); color: var(--accent); }
.folder.drop { outline: 2px dashed var(--accent); }
.folder.root .fname { color: var(--muted); font-style: italic; }
.twisty { width: 14px; text-align: center; color: var(--muted); font-size: 11px; }
.fname { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.count { color: var(--muted); font-size: 11px; }
.row-actions { display: none; }
.folder:hover .row-actions, .tag-row:hover .row-actions { display: inline-flex; }
.folder:hover .count, .tag-row:hover .count { display: none; }
.tag-row .chip { flex: 0 1 auto; }
.tag-row .count { margin-left: auto; }
.tag-row .row-actions { margin-left: auto; }

.chip { display: inline-flex; align-items: center; gap: 3px; background: #eef1f5; border-radius: 10px; padding: 1px 8px; font-size: 12px; white-space: nowrap; }
.chip.small { font-size: 11px; padding: 0 6px; }
.chip-x { border: 0; background: none; cursor: pointer; padding: 0 0 0 2px; color: var(--muted); font-size: 13px; line-height: 1; }

/* list */
.list { display: flex; flex-direction: column; border-right: 1px solid var(--line); min-height: 0; background: #fbfbfc; }
.list-head { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--line); }
.list-title { font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-items { overflow-y: auto; flex: 1; }
.group { padding: 10px 12px 4px; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
.item { padding: 8px 12px; border-bottom: 1px solid #eef0f2; cursor: pointer; }
.item:hover { background: #f3f5f8; }
.item.active { background: var(--accent-soft); }
.item.is-done .item-title { text-decoration: line-through; color: var(--muted); }
.item-top { display: flex; align-items: center; gap: 6px; }
.item-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.type-icon { font-size: 12px; opacity: .6; }
.task-check { margin: 0; cursor: pointer; }
.item-snippet { color: #4b5563; font-size: 12.5px; margin-top: 3px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; white-space: pre-line; }
.item-meta { display: flex; align-items: center; gap: 5px; margin-top: 4px; font-size: 11.5px; flex-wrap: wrap; }
.item-actions { display: flex; gap: 6px; margin-top: 6px; }
.badge { font-size: 11.5px; padding: 0 6px; border-radius: 8px; background: #eef1f5; white-space: nowrap; }
.badge.overdue { background: #fde8e8; color: var(--danger); }
.badge.done { background: #e6f4ea; color: var(--ok); }
.empty { color: var(--muted); padding: 40px 20px; text-align: center; }
.empty.small { padding: 24px 12px; }

/* editor */
.editor-pane { display: flex; flex-direction: column; min-height: 0; min-width: 0; background: var(--panel); overflow-y: auto; }
.banner { background: #fff7e6; border-bottom: 1px solid #f3d9a4; padding: 8px 16px; }
.ed-head { display: flex; align-items: center; gap: 6px; padding: 14px 20px 4px; }
.title-input { flex: 1; border: 0; font-size: 22px; font-weight: 600; padding: 4px 0; outline: none; background: transparent; min-width: 0; }
.ed-meta { display: flex; align-items: center; gap: 8px; padding: 2px 20px 8px; flex-wrap: wrap; }
.folder-select { border: 1px solid var(--line); border-radius: var(--radius); padding: 2px 6px; max-width: 240px; background: #fff; }
.tag-input { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; flex: 1; min-width: 200px; }
.tag-new { border: 0; outline: none; min-width: 90px; flex: 1; padding: 2px 4px; background: transparent; }

.task-panel { margin: 0 20px 8px; padding: 10px 12px; border: 1px solid var(--line); border-radius: var(--radius); background: #fafbfc; display: flex; flex-direction: column; gap: 8px; }
.task-panel[hidden] { display: none; }
.task-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.task-row .lbl { color: var(--muted); font-size: 12px; min-width: 64px; }
.task-row select, .task-row input[type=datetime-local], .rrule-input { border: 1px solid var(--line); border-radius: var(--radius); padding: 3px 6px; background: #fff; }
.rrule-input { min-width: 320px; font-family: ui-monospace, Consolas, monospace; font-size: 12px; }
.repeat-extra { display: inline-flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.day { display: inline-flex; align-items: center; gap: 2px; font-size: 12px; }
.check { display: inline-flex; align-items: center; gap: 4px; font-size: 13px; }
.task-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

.toolbar { position: sticky; top: 0; z-index: 2; display: flex; flex-wrap: wrap; gap: 2px; padding: 4px 16px; background: var(--panel); border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.tb-btn { border: 0; background: transparent; border-radius: 4px; padding: 3px 7px; min-width: 28px; cursor: pointer; font-size: 13px; }
.tb-btn:hover { background: #eceef1; }
.tb-btn.active { background: var(--accent-soft); color: var(--accent); }
.tb-btn.tb-small { font-size: 11.5px; min-width: 0; }
.tb-sep { width: 1px; background: var(--line); margin: 3px 4px; }
.tb-table { display: inline-flex; align-items: center; }
.tb-table-ops { display: none; gap: 1px; margin-left: 2px; padding-left: 4px; border-left: 1px dashed var(--line); }

.ed-body { flex: 1 0 auto; padding: 12px 20px 30px; min-height: 260px; }
.tiptap { outline: none; line-height: 1.55; font-size: 15px; max-width: 980px; }
.tiptap > * + * { margin-top: .6em; }
.tiptap p { margin: 0; }
.tiptap p + p { margin-top: .5em; }
.tiptap h1, .tiptap h2, .tiptap h3 { line-height: 1.25; margin: 1em 0 .4em; }
.tiptap img { max-width: 100%; height: auto; border-radius: 4px; }
.tiptap img.ProseMirror-selectednode { outline: 3px solid var(--accent); }
.tiptap blockquote { border-left: 3px solid var(--line); margin-left: 0; padding-left: 12px; color: #4b5563; }
.tiptap code { background: #f1f3f5; border-radius: 4px; padding: 1px 4px; font-family: ui-monospace, Consolas, monospace; font-size: .9em; }
.tiptap pre { background: #1e2430; color: #e6e9ef; border-radius: var(--radius); padding: 10px 12px; overflow-x: auto; font-family: ui-monospace, Consolas, monospace; font-size: 13px; line-height: 1.45; }
.tiptap pre code { background: none; padding: 0; color: inherit; font-size: inherit; }
.tiptap table { border-collapse: collapse; table-layout: fixed; width: 100%; margin: .6em 0; overflow: hidden; }
.tiptap td, .tiptap th { border: 1px solid #cfd5dc; padding: 4px 8px; vertical-align: top; position: relative; min-width: 40px; }
.tiptap th { background: #f3f5f8; font-weight: 600; text-align: left; }
.tiptap .selectedCell::after { content: ""; position: absolute; inset: 0; background: rgba(37, 99, 235, .12); pointer-events: none; }
.tiptap .column-resize-handle { position: absolute; right: -2px; top: 0; bottom: 0; width: 4px; background: var(--accent); pointer-events: none; }
.tiptap.resize-cursor { cursor: col-resize; }
.tiptap .tableWrapper { overflow-x: auto; }
.tiptap ul[data-type="taskList"] { list-style: none; padding-left: 4px; }
.tiptap ul[data-type="taskList"] li { display: flex; gap: 6px; align-items: flex-start; }
.tiptap ul[data-type="taskList"] li > label { margin-top: 3px; }
.tiptap ul[data-type="taskList"] li[data-checked="true"] > div { text-decoration: line-through; color: var(--muted); }
.tiptap p.is-editor-empty:first-child::before { content: attr(data-placeholder); color: #a0a7b1; float: left; height: 0; pointer-events: none; }
.tiptap a { cursor: text; }
/* code highlight (lowlight / highlight.js classes) */
.hljs-comment, .hljs-quote { color: #8b95a7; font-style: italic; }
.hljs-keyword, .hljs-selector-tag, .hljs-literal { color: #c792ea; }
.hljs-string, .hljs-attr, .hljs-template-tag { color: #c3e88d; }
.hljs-number, .hljs-built_in, .hljs-type { color: #f78c6c; }
.hljs-title, .hljs-function, .hljs-section { color: #82aaff; }
.hljs-variable, .hljs-name, .hljs-tag { color: #f07178; }

.attachments { margin: 0 20px 10px; border: 1px dashed var(--line); border-radius: var(--radius); padding: 8px 12px; }
.attachments.drop { border-color: var(--accent); background: var(--accent-soft); }
.att-head { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.att-list { display: flex; flex-direction: column; gap: 2px; margin-top: 6px; }
.att { display: flex; align-items: center; gap: 10px; }
.att a:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 420px; }
.drop-hint { margin-top: 4px; }
.ed-foot { display: flex; justify-content: space-between; padding: 6px 20px 12px; border-top: 1px solid var(--line); }

/* modal / toast */
.modal-back { position: fixed; inset: 0; background: rgba(15, 23, 42, .35); display: flex; align-items: flex-start; justify-content: center; padding-top: 8vh; z-index: 50; }
.modal { background: #fff; border-radius: 10px; box-shadow: 0 20px 50px rgba(0, 0, 0, .2); width: min(560px, 94vw); max-height: 84vh; display: flex; flex-direction: column; }
.modal.wide { width: min(1000px, 96vw); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.modal-head h3 { margin: 0; font-size: 16px; }
.modal-body { padding: 14px 16px; overflow-y: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 10px 16px; border-top: 1px solid var(--line); }
.settings { display: flex; flex-direction: column; gap: 12px; }
.settings label { display: flex; flex-direction: column; gap: 4px; }
.settings select { padding: 5px; border: 1px solid var(--line); border-radius: var(--radius); }
.settings h4 { margin: 6px 0 0; }
.tg { display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.tg .row { display: flex; gap: 8px; }
.grid { border-collapse: collapse; width: 100%; font-size: 13px; }
.grid th, .grid td { border-bottom: 1px solid var(--line); padding: 4px 8px; text-align: left; }
.rev { display: grid; grid-template-columns: 220px 1fr; gap: 12px; min-height: 50vh; }
.rev-list { border-right: 1px solid var(--line); overflow-y: auto; max-height: 64vh; }
.rev-item { padding: 6px 8px; cursor: pointer; border-radius: var(--radius); display: flex; flex-direction: column; }
.rev-item:hover { background: #f3f5f8; }
.rev-item.active { background: var(--accent-soft); }
.rev-preview { overflow-y: auto; max-height: 64vh; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translate(-50%, 20px); background: #1f2937; color: #fff; padding: 8px 16px; border-radius: 8px; opacity: 0; transition: all .25s; z-index: 100; max-width: 80vw; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); }

/* auth */
.auth-body { display: flex; align-items: center; justify-content: center; }
.auth-card { background: #fff; padding: 28px 32px; border-radius: 12px; box-shadow: 0 8px 30px rgba(0, 0, 0, .08); width: 340px; }
.auth-card .brand { font-size: 22px; margin: 0 0 18px; }
.auth-card form { display: flex; flex-direction: column; gap: 12px; }
.auth-card label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.auth-card input { padding: 7px 10px; border: 1px solid var(--line); border-radius: var(--radius); font-size: 14px; color: var(--text); }
.auth-card .btn { padding: 8px; }
.auth-error { color: var(--danger); font-size: 13px; }
.auth-alt { margin: 14px 0 0; font-size: 13px; }

@media (max-width: 900px) {
  #app { grid-template-columns: 1fr; grid-template-rows: 48px auto auto 1fr; height: auto; }
  .side, .list { border-right: 0; border-bottom: 1px solid var(--line); max-height: 40vh; }
}
.tg-step { display: flex; flex-direction: column; gap: 6px; padding: 8px 0; border-top: 1px solid var(--line); width: 100%; }
.tg-step:first-child { border-top: 0; padding-top: 0; }
.tg-step .row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.token-input { flex: 1; min-width: 260px; padding: 5px 8px; border: 1px solid var(--line); border-radius: var(--radius); font-family: ui-monospace, Consolas, monospace; font-size: 12px; }
.tg details { margin-top: 2px; }
