:root {
  --bg: #ffffff;
  --bg-side: #fafaf9;
  --bg-hover: #f0efed;
  --bg-active: #eae9e6;
  --border: #ececea;
  --text: #2b2b28;
  --text-soft: #6b6b66;
  --text-faint: #9a9a94;
  --accent: #1f6feb;
  --danger: #d64545;
  --radius: 8px;
  --sidebar-w: 264px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1a19;
    --bg-side: #161615;
    --bg-hover: #232322;
    --bg-active: #2c2c2a;
    --border: #2a2a28;
    --text: #e8e8e4;
    --text-soft: #a5a5a0;
    --text-faint: #6f6f6a;
    --accent: #4f8cf7;
    --danger: #e06565;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }

/* Thin, subtle scrollbars — the chunky default kills the aesthetic. */
* { scrollbar-width: thin; scrollbar-color: transparent transparent; }
*:hover { scrollbar-color: color-mix(in oklab, var(--text-faint) 45%, transparent) transparent; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: transparent;
  border: 3px solid transparent;
  border-radius: 10px;
  background-clip: content-box;
}
:hover > ::-webkit-scrollbar-thumb,
*:hover::-webkit-scrollbar-thumb {
  background: color-mix(in oklab, var(--text-faint) 45%, transparent);
  background-clip: content-box;
}
::-webkit-scrollbar-thumb:hover {
  background: var(--text-faint);
  background-clip: content-box;
}
::-webkit-scrollbar-corner { background: transparent; }
body {
  margin: 0;
  font-family: var(--font);
  color: var(--text);
  background: var(--bg);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

#app { display: flex; height: 100vh; overflow: hidden; }

/* ---------- Sidebar ---------- */
#sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: var(--bg-side);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.side-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 14px 8px;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; letter-spacing: -0.01em; }
.brand-mark { color: var(--accent); font-size: 15px; }
.brand-name { font-size: 14px; }
.search-wrap { padding: 4px 12px 10px; }
#search {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  outline: none;
}
#search:focus { border-color: var(--accent); }

.doc-list { flex: 1; overflow-y: auto; padding: 4px 8px 12px; min-height: 0; }
.doc-item {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  color: var(--text);
  padding: 8px 10px;
  border-radius: var(--radius);
  cursor: pointer;
  font: inherit;
  margin-bottom: 1px;
}
.doc-item:hover { background: var(--bg-hover); }
.doc-item.is-active { background: var(--bg-active); }
.doc-item .di-title { font-size: 14px; font-weight: 500; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-item .di-meta { font-size: 12px; color: var(--text-faint); display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 1px; }
.side-foot { padding: 10px 16px; border-top: 1px solid var(--border); font-size: 12px; color: var(--text-faint); }
.list-empty { padding: 16px 12px; color: var(--text-faint); font-size: 13px; }

/* ---------- Main ---------- */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }

#topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 28px;
  border-bottom: 1px solid var(--border);
}
.title-input {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--text);
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.01em;
  padding: 4px 0;
  outline: none;
  min-width: 0;
}
.title-input::placeholder { color: var(--text-faint); }
.top-actions { display: flex; align-items: center; gap: 10px; }
.status { font-size: 12px; color: var(--text-faint); min-width: 52px; text-align: right; transition: opacity .3s; }

.seg { display: inline-flex; background: var(--bg-hover); border-radius: var(--radius); padding: 2px; }
.seg-btn {
  border: 0; background: transparent; color: var(--text-soft);
  font: inherit; font-size: 13px; padding: 4px 12px; border-radius: 6px; cursor: pointer;
}
.seg-btn.is-active { background: var(--bg); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.06); }

.icon-btn {
  border: 0; background: transparent; color: var(--text-soft);
  width: 30px; height: 30px; border-radius: var(--radius); cursor: pointer;
  font-size: 16px; line-height: 1; display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }
.icon-btn.danger:hover { color: var(--danger); }

.primary-btn {
  border: 0; background: var(--accent); color: #fff;
  font: inherit; font-weight: 500; padding: 9px 18px; border-radius: var(--radius); cursor: pointer;
}
.primary-btn:hover { filter: brightness(1.05); }

/* ---------- Content ---------- */
.content { flex: 1; overflow-y: auto; min-height: 0; }
.prose, .editor { max-width: 760px; margin: 0 auto; padding: 40px 28px 120px; }

.editor {
  display: none;
  width: 100%;
  min-height: calc(100vh - 160px);
  border: 0;
  outline: none;
  resize: none;
  background: transparent;
  color: var(--text);
  font-family: var(--mono);
  font-size: 14.5px;
  line-height: 1.7;
}
body.mode-write .editor { display: block; }
body.mode-write .prose { display: none; }

/* ---------- Prose (rendered markdown) ---------- */
.prose { color: var(--text); }
.prose > *:first-child { margin-top: 0; }
.prose h1 { font-size: 30px; font-weight: 700; letter-spacing: -0.02em; margin: 0 0 8px; line-height: 1.2; }
.prose h2 { font-size: 22px; font-weight: 650; letter-spacing: -0.01em; margin: 34px 0 10px; }
.prose h3 { font-size: 18px; font-weight: 600; margin: 26px 0 8px; }
.prose h4 { font-size: 15px; font-weight: 600; margin: 22px 0 6px; }
.prose p { margin: 12px 0; }
.prose a { color: var(--accent); text-decoration: none; }
.prose a:hover { text-decoration: underline; }
.prose ul, .prose ol { margin: 12px 0; padding-left: 26px; }
.prose li { margin: 4px 0; }
.prose li > ul, .prose li > ol { margin: 4px 0; }
.prose blockquote {
  margin: 16px 0; padding: 2px 16px; border-left: 3px solid var(--border); color: var(--text-soft);
}
.prose code {
  font-family: var(--mono); font-size: 0.88em;
  background: var(--bg-hover); padding: 2px 6px; border-radius: 5px;
}
.prose pre {
  background: var(--bg-hover); padding: 16px 18px; border-radius: 10px;
  overflow-x: auto; margin: 16px 0;
}
.prose pre code { background: transparent; padding: 0; font-size: 13.5px; }
.prose hr { border: 0; border-top: 1px solid var(--border); margin: 28px 0; }
.prose img { max-width: 100%; border-radius: 8px; }
.prose table { border-collapse: collapse; margin: 16px 0; width: 100%; font-size: 14px; display: block; overflow-x: auto; }
.prose th, .prose td { border: 1px solid var(--border); padding: 7px 12px; text-align: left; }
.prose th { background: var(--bg-hover); font-weight: 600; }
.prose input[type="checkbox"] { margin-right: 8px; }

/* ---------- Empty state ---------- */
.empty { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; background: var(--bg); }
.empty-inner { text-align: center; color: var(--text-soft); }
.empty-mark { font-size: 34px; color: var(--accent); opacity: .8; margin-bottom: 10px; }
.empty-inner h2 { font-size: 20px; font-weight: 600; color: var(--text); margin: 0 0 6px; }
.empty-inner p { margin: 0 0 18px; }
body.has-doc .empty { display: none; }

/* ---------- Sign-in gate ---------- */
.link-btn {
  border: 0; background: transparent; color: var(--text-faint);
  font: inherit; font-size: 12px; cursor: pointer; padding: 0; text-decoration: underline;
}
.link-btn:hover { color: var(--text-soft); }
.side-foot { display: flex; align-items: center; justify-content: space-between; }

.gate {
  position: fixed; inset: 0; z-index: 50;
  display: none; align-items: center; justify-content: center;
  background: var(--bg);
}
.gate.show { display: flex; }
.gate-card {
  width: 320px; max-width: 88vw; text-align: center;
  padding: 34px 28px; border: 1px solid var(--border); border-radius: 14px;
  background: var(--bg-side);
}
.gate-mark { font-size: 30px; color: var(--accent); margin-bottom: 10px; }
.gate-card h1 { font-size: 19px; font-weight: 650; margin: 0 0 6px; }
.gate-card p { font-size: 13.5px; color: var(--text-soft); margin: 0 0 18px; }
.gate-card input {
  width: 100%; padding: 10px 12px; margin-bottom: 12px;
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--bg); color: var(--text); font: inherit; outline: none;
}
.gate-card input:focus { border-color: var(--accent); }
.gate-card .primary-btn { width: 100%; }
.gate-card .link-btn { display: block; margin: 12px auto 0; }
.gate-err { color: var(--danger); font-size: 12.5px; min-height: 16px; margin-bottom: 8px; }
.gate-strong { color: var(--text); font-weight: 600; }
#gate-code {
  text-align: center; font-size: 22px; letter-spacing: 8px;
  font-family: var(--mono); padding-left: 20px;
}
#gate-code::placeholder { letter-spacing: 8px; }

/* ---------- Editor (Toast UI overrides for a clean, Notion-like look) ---------- */
.content { padding: 0; overflow: hidden; }
#editor { height: 100%; }

/* Strip the library's outer chrome; blend into our surfaces */
.toastui-editor-defaultUI { border: 0; }
.toastui-editor-defaultUI,
.toastui-editor-main,
.toastui-editor-ww-container,
.toastui-editor-md-container,
.toastui-editor-contents { background: var(--bg); }

/* Slim toolbar: a quiet strip, not a ribbon */
.toastui-editor-defaultUI-toolbar {
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding: 6px 24px;
  height: auto;
}
.toastui-editor-toolbar-icons { border: 0 !important; }
.toastui-editor-toolbar-divider { background-color: var(--border) !important; }
.toastui-editor-defaultUI-toolbar .toastui-editor-toolbar-icons:hover {
  background-color: var(--bg-hover) !important;
  border-radius: 6px;
}

/* Centered reading column, our typography */
.toastui-editor-ww-container .toastui-editor-contents {
  max-width: 760px;
  margin: 0 auto;
  padding: 36px 28px 140px;
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.7;
  color: var(--text);
}
.toastui-editor-contents p,
.toastui-editor-contents li,
.toastui-editor-contents div { font-family: var(--font); color: var(--text); }
.toastui-editor-contents h1,
.toastui-editor-contents h2,
.toastui-editor-contents h3,
.toastui-editor-contents h4 {
  font-family: var(--font);
  color: var(--text);
  border: 0;
  padding: 0;
  letter-spacing: -0.01em;
}
.toastui-editor-contents h1 { font-size: 30px; font-weight: 700; margin: 28px 0 8px; }
.toastui-editor-contents h2 { font-size: 22px; font-weight: 650; margin: 30px 0 8px; }
.toastui-editor-contents h3 { font-size: 18px; font-weight: 600; margin: 24px 0 6px; }
.toastui-editor-contents a { color: var(--accent); }
.toastui-editor-contents blockquote {
  border-left: 3px solid var(--border);
  color: var(--text-soft);
  background: transparent;
}
.toastui-editor-contents code {
  font-family: var(--mono);
  background: var(--bg-hover);
  color: var(--text);
  padding: 2px 6px;
  border-radius: 5px;
}
.toastui-editor-contents pre {
  background: var(--bg-hover);
  border-radius: 10px;
}
.toastui-editor-contents pre code { background: transparent; }
/* Center the bullet dot on the line (Toast UI pins it near the top for its
   own smaller line-height). */
.toastui-editor-ww-container .toastui-editor-contents ul > li::before {
  margin-top: 0;
  top: 0.66em;
}
.toastui-editor-contents hr { border-top: 1px solid var(--border); }
/* Tables — clean, bordered, with a header fill and hover rows */
.toastui-editor-contents table {
  border-collapse: separate;
  border-spacing: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  font-size: 14px;
}
.toastui-editor-contents table th,
.toastui-editor-contents table td {
  border: 0;
  border-bottom: 1px solid var(--border);
  border-right: 1px solid var(--border);
  padding: 8px 12px;
  color: var(--text);
}
.toastui-editor-contents table th:last-child,
.toastui-editor-contents table td:last-child { border-right: 0; }
.toastui-editor-contents table tr:last-child td { border-bottom: 0; }
.toastui-editor-contents table th {
  background: var(--bg-hover);
  font-weight: 600;
  text-align: left;
}
.toastui-editor-contents table tbody tr:hover td { background: var(--bg-hover); }

/* Code blocks — keep our surface behind Prism's colored tokens */
.toastui-editor-contents pre,
.toastui-editor-contents pre[class*='language-'] {
  background: var(--bg-hover);
  border: 1px solid var(--border);
  border-radius: 10px;
}
/* Keep code text inside the block: fill the column, wrap long lines.
   Lines wrap, so there's nothing to scroll — no horizontal scrollbar. */
.toastui-editor-ww-code-block-highlighting,
.toastui-editor-contents pre {
  width: 100%;
  box-sizing: border-box;
  overflow-x: hidden;
}
/* Force wrapping — Prism sets `white-space: pre` on language-* elements with
   high specificity, so override it (else long lines overflow/clip). */
.toastui-editor-contents pre,
.toastui-editor-contents pre code,
.toastui-editor-contents pre[class*='language-'],
.toastui-editor-contents pre[class*='language-'] code,
.toastui-editor-contents code[class*='language-'] {
  white-space: pre-wrap !important;
  overflow-wrap: anywhere;
  word-break: break-word;
}
/* Header must use our palette in both themes (beats Toast UI's theme rules). */
.toastui-editor-contents table th {
  color: var(--text) !important;
  background: var(--bg-hover) !important;
}
.toastui-editor-contents pre[class*='language-'],
.toastui-editor-contents pre[class*='language-'] code,
.toastui-editor-contents code[class*='language-'],
.toastui-editor-contents .token { text-shadow: none; }
.toastui-editor-ww-code-block { background: var(--bg-hover) !important; }

/* Prism token colors tuned for a dark surface (light mode uses the base theme) */
@media (prefers-color-scheme: dark) {
  .toastui-editor-contents .token.comment,
  .toastui-editor-contents .token.prolog,
  .toastui-editor-contents .token.doctype,
  .toastui-editor-contents .token.cdata { color: #7c8a99; }
  .toastui-editor-contents .token.punctuation { color: #c9d1d9; }
  .toastui-editor-contents .token.property,
  .toastui-editor-contents .token.tag,
  .toastui-editor-contents .token.constant,
  .toastui-editor-contents .token.symbol,
  .toastui-editor-contents .token.deleted { color: #79c0ff; }
  .toastui-editor-contents .token.boolean,
  .toastui-editor-contents .token.number { color: #b5cea8; }
  .toastui-editor-contents .token.selector,
  .toastui-editor-contents .token.attr-name,
  .toastui-editor-contents .token.string,
  .toastui-editor-contents .token.char,
  .toastui-editor-contents .token.builtin,
  .toastui-editor-contents .token.inserted { color: #ce9178; }
  .toastui-editor-contents .token.operator,
  .toastui-editor-contents .token.entity,
  .toastui-editor-contents .token.url { color: #d4d4d4; }
  .toastui-editor-contents .token.atrule,
  .toastui-editor-contents .token.attr-value,
  .toastui-editor-contents .token.keyword { color: #569cd6; }
  .toastui-editor-contents .token.function,
  .toastui-editor-contents .token.class-name { color: #dcdcaa; }
  .toastui-editor-contents .token.regex,
  .toastui-editor-contents .token.important,
  .toastui-editor-contents .token.variable { color: #d16969; }
}
/* Placeholder for an empty doc */
.toastui-editor-contents .placeholder,
.toastui-editor-ww-container .placeholder { color: var(--text-faint); }

/* ---------- Sidebar tree: Shared + Private spaces ---------- */
.space { margin-bottom: 4px; }
.space-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px 4px;
}
.space-label {
  font-size: 11px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase;
  color: var(--text-faint);
}
.space-head .row-actions { display: inline-flex; gap: 2px; }
.space-empty { padding: 4px 14px 8px; font-size: 12.5px; color: var(--text-faint); }

.folder-row, .doc-item {
  display: flex; align-items: center; gap: 5px;
  padding: 5px 8px; margin: 1px 6px; border-radius: 7px;
  cursor: pointer; user-select: none; font-size: 14px; color: var(--text);
  transition: background 0.12s ease;
}
.folder-row:hover, .doc-item:hover { background: var(--bg-hover); }
.doc-item.is-active { background: var(--bg-active); font-weight: 500; }

.chevron {
  flex: none; width: 13px; text-align: center; font-size: 10px; color: var(--text-faint);
  transition: transform 0.15s ease;
}
.chevron.open { transform: rotate(90deg); }
.row-icon { flex: none; font-size: 13px; line-height: 1; }
.row-name, .di-title {
  flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.row-actions { display: none; gap: 1px; flex: none; margin-left: auto; }
.folder-row:hover .row-actions, .doc-item:hover .row-actions { display: inline-flex; }
.row-btn {
  border: 0; background: transparent; color: var(--text-faint); cursor: pointer;
  width: 21px; height: 21px; padding: 0; border-radius: 5px; font-size: 12px; line-height: 1;
  display: inline-flex; align-items: center; justify-content: center;
}
.row-btn:hover { background: var(--bg-active); color: var(--text); }

.rename-input {
  flex: 1; min-width: 0; font: inherit; font-size: 14px; padding: 1px 5px;
  border: 1px solid var(--accent); border-radius: 5px; outline: none;
  background: var(--bg); color: var(--text);
}

/* Drag + drop feedback */
.folder-row.dragging, .doc-item.dragging { opacity: 0.4; }
.drop-target {
  background: color-mix(in oklab, var(--accent) 14%, transparent);
  box-shadow: inset 0 0 0 2px var(--accent);
  border-radius: 7px;
}
.drop-before { box-shadow: inset 0 2px 0 0 var(--accent); }
.space-body { min-height: 8px; border-radius: 8px; }

@media (max-width: 680px) {
  :root { --sidebar-w: 100%; }
  #app { flex-direction: column; }
  #sidebar { height: 42vh; flex-basis: auto; }
  .doc-list { max-height: none; }
}
