:root {
  --bg: #ffffff;
  --side: #f7f7f5;
  --text: #37352f;
  --muted: #787774;
  --faint: #9b9a97;
  --line: #e9e9e7;
  --hover: rgba(55, 53, 47, 0.08);
  --active: rgba(35, 131, 226, 0.14);
  --accent: #2383e2;
  --danger: #d44c47;
  --code-bg: rgba(135, 131, 120, 0.15);
  --pop: #ffffff;
  --shadow: 0 0 0 1px rgba(15, 15, 15, 0.05), 0 3px 6px rgba(15, 15, 15, 0.1), 0 9px 24px rgba(15, 15, 15, 0.2);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", Helvetica, Arial, sans-serif;
  --mono: "SFMono-Regular", Menlo, Consolas, "D2Coding", monospace;
  --side-width: 260px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #191919;
    --side: #202020;
    --text: #e6e6e4;
    --muted: #9b9b98;
    --faint: #7f7f7c;
    --line: #2f2f2f;
    --hover: rgba(255, 255, 255, 0.06);
    --active: rgba(35, 131, 226, 0.22);
    --accent: #529cca;
    --danger: #e5726e;
    --code-bg: rgba(255, 255, 255, 0.08);
    --pop: #252525;
    --shadow: 0 0 0 1px rgba(255, 255, 255, 0.08), 0 3px 6px rgba(0, 0, 0, 0.3), 0 9px 24px rgba(0, 0, 0, 0.45);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text); font: 16px/1.6 var(--font); -webkit-text-size-adjust: 100%; overflow-x: hidden; }
a { color: inherit; }
button { font: inherit; color: inherit; }
kbd { font: 12px var(--mono); padding: 1px 5px; border: 1px solid var(--line); border-radius: 4px; background: var(--side); }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
[hidden] { display: none !important; }

.notes-shell { display: flex; min-height: 100vh; min-height: 100dvh; }

/* Sidebar */
.sidebar { position: fixed; top: 0; bottom: 0; left: 0; z-index: 20; width: var(--side-width); display: flex; flex-direction: column; background: var(--side); border-right: 1px solid var(--line); font-size: 14px; }
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 12px 6px 14px; }
.sidebar-home { font-weight: 600; text-decoration: none; padding: 4px 6px; border-radius: 6px; }
.sidebar-home:hover { background: var(--hover); }
.search { display: block; padding: 4px 12px 8px; }
.search input { width: 100%; padding: 6px 10px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); color: var(--text); font: inherit; }
.tree, .search-results { flex: 1; overflow-y: auto; padding: 2px 6px 12px; }
.tree-row { display: flex; align-items: center; gap: 2px; min-height: 30px; padding-left: calc(var(--depth) * 14px); border-radius: 6px; }
.tree-row:hover { background: var(--hover); }
.tree-row.is-current { background: var(--active); font-weight: 600; }
.tree-toggle { flex: 0 0 22px; height: 22px; border: 0; background: none; border-radius: 4px; color: var(--faint); font-size: 11px; cursor: pointer; }
.tree-toggle:disabled { cursor: default; font-size: 8px; }
.tree-toggle:not(:disabled):hover, .tree-action:hover, .icon-button:hover { background: var(--hover); }
.tree-link { flex: 1; min-width: 0; padding: 4px 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-decoration: none; }
.tree-action { flex: 0 0 24px; height: 24px; border: 0; background: none; border-radius: 4px; color: var(--muted); cursor: pointer; opacity: 0; }
.tree-row:hover .tree-action, .tree-row:focus-within .tree-action { opacity: 1; }
.tree-empty { margin: 8px 10px; color: var(--faint); }
.search-hit { display: block; padding: 6px 8px; border-radius: 6px; text-decoration: none; }
.search-hit:hover { background: var(--hover); }
.search-hit strong { display: block; font-weight: 600; }
.search-hit span { display: block; color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-foot { padding: 8px 6px 12px; border-top: 1px solid var(--line); }
.sidebar-action { display: block; width: 100%; padding: 6px 10px; border: 0; background: none; border-radius: 6px; text-align: left; text-decoration: none; color: var(--muted); cursor: pointer; }
.sidebar-action:hover { background: var(--hover); color: var(--text); }
.account { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 10px 0; color: var(--faint); font-size: 12px; }
.account-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.scrim { display: none; }

/* Main */
.main { flex: 1; min-width: 0; margin-left: var(--side-width); }
.topbar { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 8px; height: 45px; padding: 0 12px; background: var(--bg); }
.crumbs { flex: 1; min-width: 0; display: flex; align-items: center; gap: 4px; overflow: hidden; white-space: nowrap; font-size: 14px; color: var(--muted); }
.crumbs a { padding: 2px 6px; border-radius: 4px; text-decoration: none; overflow: hidden; text-overflow: ellipsis; max-width: 200px; }
.crumbs a:hover { background: var(--hover); }
.crumbs a[aria-current] { color: var(--text); }
.crumb-sep { color: var(--faint); }
.save-state { font-size: 12px; color: var(--faint); white-space: nowrap; }
.save-state[data-kind="offline"] { color: #c58b00; }
.save-state[data-kind="error"] { color: var(--danger); font-weight: 600; }
.icon-button { display: inline-grid; place-items: center; width: 30px; height: 30px; border: 0; background: none; border-radius: 6px; color: var(--muted); cursor: pointer; font-size: 16px; }
.icon-button.menu-button, .icon-button.sidebar-close { display: none; }
.link-button { border: 0; background: none; padding: 2px 6px; color: var(--accent); cursor: pointer; border-radius: 4px; }
.link-button:hover { background: var(--hover); }
.link-button.danger { color: var(--danger); }
.primary-button { padding: 8px 14px; border: 0; border-radius: 6px; background: var(--accent); color: #fff; font-weight: 600; cursor: pointer; }

.view { max-width: 760px; margin: 0 auto; padding: 48px 56px 30vh; }
.view h1 { font-size: 32px; line-height: 1.25; margin: 0 0 16px; }
.view h2 { font-size: 14px; color: var(--muted); font-weight: 600; margin: 32px 0 8px; }
.view-gate { text-align: center; padding-top: 18vh; }
.view-gate p { color: var(--muted); }
.hint { margin-top: 32px; color: var(--faint); font-size: 14px; }
.page-list { list-style: none; margin: 0; padding: 0; }
.page-list li { display: flex; align-items: center; gap: 4px; }
.page-list li.empty { color: var(--faint); padding: 6px 8px; }
.page-row { flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 6px; text-decoration: none; }
.page-row:hover { background: var(--hover); }
.page-row-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.page-row-meta { color: var(--faint); font-size: 13px; }

/* Page */
.view-page { padding-top: 64px; }
.trash-banner { margin: -40px 0 24px; padding: 8px 12px; border-radius: 6px; background: rgba(212, 76, 71, 0.12); color: var(--danger); font-size: 14px; }
.page-head { margin-bottom: 8px; }
.page-icon { display: block; height: 72px; padding: 0; border: 0; background: none; border-radius: 8px; font-size: 64px; line-height: 72px; cursor: pointer; }
.page-icon:hover { background: var(--hover); }
.page-icon.is-empty { height: 28px; font-size: 14px; line-height: 28px; color: var(--faint); opacity: 0; padding: 0 6px; }
.page-icon.is-empty::before { content: "☺ 아이콘 추가"; }
.page-head:hover .page-icon.is-empty, .page-icon.is-empty:focus-visible { opacity: 1; }
.page-title { display: block; width: 100%; resize: none; overflow: hidden; border: 0; outline: 0; padding: 0; background: none; color: var(--text); font: 700 40px/1.2 var(--font); }
.page-title::placeholder { color: var(--line); }
.page-loading { color: var(--faint); }
.editor-wrap { position: relative; }
.block-handle { position: absolute; left: -30px; width: 22px; height: 26px; margin-top: 2px; padding: 0; border: 0; border-radius: 4px; background: none; color: var(--faint); font-size: 12px; letter-spacing: -2px; cursor: pointer; }
.block-handle:hover { background: var(--hover); }
.backlinks, .children { margin-top: 48px; padding-top: 8px; border-top: 1px solid var(--line); }
.backlinks h2, .children h2 { margin-top: 8px; }

/* Editor content */
.notes-prose { outline: none; min-height: 40vh; caret-color: var(--text); word-break: keep-all; overflow-wrap: anywhere; }
.notes-prose > * + * { margin-top: 2px; }
.notes-prose p { margin: 0; padding: 3px 2px; }
.notes-prose h1, .notes-prose h2, .notes-prose h3 { margin: 1.4em 0 2px; padding: 3px 2px; line-height: 1.3; color: var(--text); font-weight: 700; }
.notes-prose h1 { font-size: 30px; }
.notes-prose h2 { font-size: 24px; }
.notes-prose h3 { font-size: 20px; }
.notes-prose ul, .notes-prose ol { margin: 0; padding-left: 1.6em; }
.notes-prose li > p { padding: 2px 2px; }
.notes-prose ul[data-type="taskList"] { list-style: none; padding-left: 2px; }
.notes-prose ul[data-type="taskList"] li { display: flex; align-items: flex-start; gap: 8px; }
.notes-prose ul[data-type="taskList"] li > label { flex: 0 0 auto; margin-top: 6px; user-select: none; }
.notes-prose ul[data-type="taskList"] li > label input { width: 16px; height: 16px; margin: 0; accent-color: var(--accent); cursor: pointer; }
.notes-prose ul[data-type="taskList"] li > div { flex: 1; min-width: 0; }
.notes-prose ul[data-type="taskList"] li[data-checked="true"] > div > p { color: var(--faint); text-decoration: line-through; }
.notes-prose blockquote { margin: 4px 0; padding: 0 14px; border-left: 3px solid currentColor; }
.notes-prose code { padding: 0.15em 0.35em; border-radius: 4px; background: var(--code-bg); color: #eb5757; font: 0.88em var(--mono); }
.notes-prose pre { margin: 6px 0; padding: 16px 20px; border-radius: 6px; background: var(--code-bg); overflow-x: auto; }
.notes-prose pre code { padding: 0; background: none; color: inherit; font-size: 14px; }
.notes-prose hr { margin: 14px 0; border: 0; border-top: 1px solid var(--line); }
.notes-prose a { color: var(--accent); text-decoration: underline; text-decoration-color: color-mix(in srgb, var(--accent) 40%, transparent); }
.notes-prose .page-link { color: var(--text); font-weight: 500; text-decoration: none; border-bottom: 1px solid var(--line); cursor: pointer; }
.notes-prose .page-link.is-missing { color: var(--faint); }
.notes-prose .ProseMirror-selectednode { outline: 2px solid var(--active); border-radius: 4px; }
.notes-prose table { width: 100%; margin: 8px 0; border-collapse: collapse; table-layout: fixed; display: block; overflow-x: auto; }
.notes-prose td, .notes-prose th { min-width: 90px; padding: 6px 8px; border: 1px solid var(--line); vertical-align: top; text-align: left; }
.notes-prose th { background: var(--side); font-weight: 600; }
.notes-prose .selectedCell { background: var(--active); }
.notes-prose .toggle { display: flex; gap: 2px; margin: 2px 0; }
.notes-prose .toggle > button { flex: 0 0 24px; height: 24px; margin-top: 3px; padding: 0; border: 0; background: none; border-radius: 4px; cursor: pointer; color: var(--text); }
.notes-prose .toggle > button::before { content: "▸"; display: inline-block; transition: transform 0.12s; }
.notes-prose .toggle.is-open > button::before { transform: rotate(90deg); }
.notes-prose .toggle > button:hover { background: var(--hover); }
.notes-prose .toggle > div { flex: 1; min-width: 0; }
.notes-prose .toggle summary { list-style: none; padding: 3px 2px; }
.notes-prose .toggle summary::-webkit-details-marker { display: none; }
.notes-prose .toggle:not(.is-open) > div > [data-type="detailsContent"] { display: none; }
.notes-prose [data-type="detailsContent"] { padding-left: 2px; }
.notes-prose .is-empty::before, .notes-prose p.is-editor-empty:first-child::before { content: attr(data-placeholder); float: left; height: 0; color: var(--faint); pointer-events: none; }
.notes-prose .suggestion { color: var(--muted); }

/* Menus */
.notes-menu { z-index: 50; width: 280px; max-height: 340px; overflow-y: auto; padding: 6px; border-radius: 8px; background: var(--pop); box-shadow: var(--shadow); font-size: 14px; }
.notes-menu.is-anchored { position: fixed; }
.notes-menu.is-sheet { width: auto; max-height: none; padding: 0; box-shadow: none; background: none; font-size: 16px; }
.notes-menu-section { padding: 8px 8px 4px; color: var(--faint); font-size: 12px; font-weight: 600; }
.notes-menu-item { display: flex; align-items: center; gap: 10px; width: 100%; min-height: 36px; padding: 4px 8px; border: 0; background: none; border-radius: 6px; text-align: left; cursor: pointer; }
.notes-menu-item:hover, .notes-menu-item.is-active { background: var(--hover); }
.notes-menu-item.is-danger { color: var(--danger); }
.notes-menu-icon { flex: 0 0 28px; height: 28px; display: grid; place-items: center; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); font-size: 13px; }
.notes-menu-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.notes-menu-hint { color: var(--faint); font-size: 12px; }
.notes-menu-empty { padding: 8px; color: var(--faint); }

.bubble { position: absolute; z-index: 40; display: flex; gap: 2px; padding: 4px; border-radius: 8px; background: var(--pop); box-shadow: var(--shadow); visibility: hidden; }
.bubble button { min-width: 30px; height: 30px; border: 0; background: none; border-radius: 4px; cursor: pointer; font-size: 14px; }
.bubble button:hover { background: var(--hover); }
.bubble button.is-on { color: var(--accent); background: var(--active); }

.sheet { position: fixed; inset: 0; z-index: 60; }
.sheet-scrim { position: absolute; inset: 0; background: rgba(15, 15, 15, 0.4); }
.sheet-panel { position: absolute; left: 0; right: 0; bottom: 0; max-height: 75vh; overflow-y: auto; padding: 8px 12px calc(16px + env(safe-area-inset-bottom)); border-radius: 14px 14px 0 0; background: var(--pop); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 4px 4px 8px; }
.sheet-filter { width: 100%; margin-bottom: 8px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--text); font: inherit; }
@media (min-width: 721px) {
  .sheet-panel { left: 50%; right: auto; bottom: auto; top: 15vh; width: 440px; transform: translateX(-50%); border-radius: 12px; box-shadow: var(--shadow); }
}

.keybar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; display: flex; gap: 2px; overflow-x: auto; padding: 4px 6px calc(4px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); background: var(--side); }
.keybar button { flex: 0 0 auto; min-width: 42px; height: 40px; border: 0; background: none; border-radius: 6px; font-size: 18px; }
.keybar button:active { background: var(--hover); }
body.keybar-open .view-page { padding-bottom: 60vh; }

.toast { position: fixed; left: 50%; bottom: 24px; z-index: 70; display: flex; align-items: center; gap: 12px; max-width: calc(100vw - 32px); padding: 10px 14px; border-radius: 8px; background: #2f2f2f; color: #fff; font-size: 14px; transform: translateX(-50%); box-shadow: var(--shadow); }
.toast .link-button { color: #8ac2ff; }
body.keybar-open .toast { bottom: 72px; }

/* Phone */
@media (max-width: 720px) {
  .main { margin-left: 0; }
  .sidebar { z-index: 55; width: min(86vw, 320px); transform: translateX(-100%); transition: transform 0.2s; box-shadow: var(--shadow); }
  body.sidebar-open .sidebar { transform: none; }
  body.sidebar-open .scrim { display: block; position: fixed; inset: 0; z-index: 54; background: rgba(15, 15, 15, 0.35); }
  .icon-button.sidebar-close, .icon-button.menu-button { display: inline-grid; }
  .tree-row { min-height: 40px; }
  .tree-action { opacity: 1; }
  .view { padding: 24px 16px 40vh; }
  .view-page { padding-top: 24px; }
  .trash-banner { margin: 0 0 16px; }
  .page-icon { height: 56px; font-size: 48px; line-height: 56px; }
  .page-icon.is-empty { opacity: 1; }
  .page-title { font-size: 30px; }
  .block-handle { display: none; }
  .crumbs a { max-width: 120px; }
  .notes-prose h1 { font-size: 26px; }
  .notes-prose h2 { font-size: 21px; }
}
