:root {
  --bg: #ffffff;
  --surface: #ffffff;
  --surface-2: #f7f8fa;
  --surface-3: #eef1f5;
  --text: #16181d;
  --muted: #697386;
  --line: #e3e7ed;
  --line-strong: #d2d8e1;
  --primary: #1f5eff;
  --primary-hover: #164bd6;
  --danger: #cf3333;
  --code: #f5f7fa;
  --shadow: 0 18px 55px rgba(25, 32, 45, .14);
  --sidebar-w: 278px;
  --toc-w: 230px;
  font-family: Inter, Pretendard, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #0f1115;
  --surface: #14171d;
  --surface-2: #191d24;
  --surface-3: #222832;
  --text: #edf0f5;
  --muted: #9aa4b3;
  --line: #2a303a;
  --line-strong: #39414d;
  --primary: #6991ff;
  --primary-hover: #86a7ff;
  --danger: #ff7474;
  --code: #191d24;
  --shadow: 0 18px 60px rgba(0, 0, 0, .34);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body { margin: 0; background: var(--bg); color: var(--text); font-size: 14px; }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: inherit; }
[hidden] { display: none !important; }

.topbar {
  position: fixed; inset: 0 0 auto 0; z-index: 50; height: 62px; display: grid;
  grid-template-columns: var(--sidebar-w) minmax(260px, 620px) 1fr; align-items: center;
  gap: 24px; padding: 0 22px; background: color-mix(in srgb, var(--surface) 92%, transparent);
  border-bottom: 1px solid var(--line); backdrop-filter: blur(12px);
}
.brand { display: inline-flex; align-items: center; gap: 10px; text-decoration: none; width: max-content; }
.brand-mark { display: grid; place-items: center; width: 31px; height: 31px; border-radius: 9px; background: var(--text); color: var(--bg); font-weight: 900; }
.brand > span:last-child { display: flex; align-items: baseline; gap: 5px; letter-spacing: -.02em; }
.brand small { color: var(--muted); font-weight: 600; }
.top-search { height: 38px; display: flex; align-items: center; gap: 8px; padding: 0 12px; border: 1px solid var(--line); background: var(--surface-2); border-radius: 9px; }
.top-search input { width: 100%; border: 0; outline: 0; background: transparent; }
.top-actions { display: flex; justify-content: flex-end; align-items: center; gap: 8px; }

.btn, .icon-btn, .mini-btn, .text-btn { border: 0; background: none; }
.btn { min-height: 36px; padding: 0 13px; border-radius: 8px; font-weight: 700; border: 1px solid transparent; }
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost { background: var(--surface); border-color: var(--line); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 30%, var(--line)); background: transparent; }
.btn:disabled { opacity: .55; cursor: default; }
.icon-btn { width: 36px; height: 36px; display: grid; place-items: center; border-radius: 8px; font-size: 17px; }
.icon-btn:hover { background: var(--surface-2); }
.mini-btn { width: 26px; height: 26px; border-radius: 6px; font-size: 18px; }
.mini-btn:hover { background: var(--surface-3); }
.text-btn { color: var(--muted); padding: 6px 0; font-weight: 700; }
.text-btn:hover { color: var(--text); }

.status-bar { position: fixed; top: 62px; left: 0; right: 0; z-index: 45; min-height: 32px; padding: 7px 22px; text-align: center; background: #fff8d7; color: #675509; border-bottom: 1px solid #eadf9a; font-size: 12px; }
.status-bar[data-type="error"] { background: #fff0f0; color: #a52a2a; border-color: #f0c0c0; }
:root[data-theme="dark"] .status-bar { background: #302b17; color: #e8d986; border-color: #4a4221; }
:root[data-theme="dark"] .status-bar[data-type="error"] { background: #361c20; color: #ff9da7; border-color: #56272e; }

.workspace { min-height: 100vh; padding-top: 62px; display: grid; grid-template-columns: var(--sidebar-w) minmax(0, 1fr) 0; }
.sidebar { position: fixed; top: 62px; bottom: 0; left: 0; z-index: 30; width: var(--sidebar-w); display: flex; flex-direction: column; border-right: 1px solid var(--line); background: var(--surface-2); }
.sidebar-head { height: 48px; padding: 0 14px 0 18px; display: flex; align-items: center; justify-content: space-between; color: var(--muted); font-size: 12px; font-weight: 800; text-transform: uppercase; letter-spacing: .08em; }
.doc-tree { flex: 1; overflow: auto; padding: 0 9px 22px; }
.sidebar-foot { min-height: 48px; display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 9px 14px; border-top: 1px solid var(--line); }
.badge { max-width: 150px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; padding: 4px 7px; border-radius: 999px; background: var(--surface-3); color: var(--muted); font-size: 10px; font-weight: 800; }
.badge[data-mode="connected"] { color: #167843; background: #e7f7ee; }
.badge[data-mode="error"] { color: #a52a2a; background: #ffe9e9; }
:root[data-theme="dark"] .badge[data-mode="connected"] { color: #83ddb0; background: #173127; }
:root[data-theme="dark"] .badge[data-mode="error"] { color: #ff9696; background: #371f22; }
.empty-sidebar { padding: 18px 11px; color: var(--muted); line-height: 1.7; font-size: 12px; }
.search-label { padding: 8px 10px 10px; color: var(--muted); font-size: 11px; font-weight: 800; }

.category-block { margin: 1px 0; }
.category-row { min-height: 32px; display: flex; align-items: center; gap: 4px; padding: 0 5px; border-radius: 7px; color: var(--text); font-weight: 750; }
.category-row:hover { background: var(--surface-3); }
.category-row.muted { padding-left: 10px; color: var(--muted); font-size: 12px; }
.category-toggle, .category-add, .category-edit { border: 0; background: none; color: var(--muted); border-radius: 5px; }
.category-toggle { width: 22px; height: 22px; font-size: 15px; }
.category-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }
.category-tools { display: none; }
.category-row:hover .category-tools { display: inline-flex; }
.category-add, .category-edit { width: 22px; height: 22px; }
.category-add:hover, .category-edit:hover { background: var(--surface); color: var(--text); }
.category-children { margin-left: 10px; padding-left: 6px; border-left: 1px solid var(--line); }
.category-block.collapsed > .category-children { display: none; }

.doc-link { width: 100%; min-height: 31px; display: flex; align-items: center; gap: 6px; padding: 5px 8px; border: 0; border-radius: 7px; background: transparent; text-align: left; color: var(--muted); }
.doc-link:hover { color: var(--text); background: var(--surface-3); }
.doc-link.active { color: var(--text); background: color-mix(in srgb, var(--primary) 12%, var(--surface)); }
.doc-icon { width: 16px; flex: none; opacity: .65; }
.doc-link-text { min-width: 0; display: flex; flex-direction: column; }
.doc-link strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12.5px; font-weight: 650; }
.doc-link small { margin-top: 2px; color: var(--muted); font-size: 10px; }

.content-pane { grid-column: 2; min-width: 0; }
.welcome-card { max-width: 880px; margin: 0 auto; padding: 100px 54px; }
.eyebrow { display: inline-block; margin-bottom: 14px; color: var(--primary); font-size: 11px; font-weight: 900; letter-spacing: .13em; }
.welcome-card h1 { max-width: 650px; margin: 0; font-size: clamp(34px, 5vw, 58px); line-height: 1.08; letter-spacing: -.055em; }
.welcome-card > p { max-width: 650px; margin: 22px 0; color: var(--muted); font-size: 17px; line-height: 1.75; }
.welcome-actions { display: flex; gap: 9px; margin-top: 28px; }
.feature-grid { margin-top: 64px; display: grid; grid-template-columns: repeat(4, 1fr); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.feature-grid article { min-height: 112px; display: flex; flex-direction: column; justify-content: flex-end; gap: 7px; padding: 18px; border-right: 1px solid var(--line); background: var(--surface); }
.feature-grid article:last-child { border-right: 0; }
.feature-grid strong { font-size: 13px; }
.feature-grid span { color: var(--muted); font-size: 11px; line-height: 1.45; }

.viewer { max-width: 880px; margin: 0 auto; padding: 70px 58px 130px; }
.doc-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 30px; padding-bottom: 28px; border-bottom: 1px solid var(--line); }
.doc-header h1 { margin: 7px 0 0; font-size: 38px; line-height: 1.15; letter-spacing: -.04em; }
.doc-meta { color: var(--muted); font-size: 11px; }
.doc-actions { display: flex; gap: 7px; flex: none; }

.markdown-body { font-size: 15.5px; line-height: 1.85; word-break: break-word; }
.viewer .markdown-body { padding-top: 34px; }
.markdown-body h1, .markdown-body h2, .markdown-body h3, .markdown-body h4 { scroll-margin-top: 90px; line-height: 1.35; letter-spacing: -.025em; }
.markdown-body h1 { margin: 0 0 24px; font-size: 34px; }
.markdown-body h2 { margin: 48px 0 14px; padding-bottom: 9px; border-bottom: 1px solid var(--line); font-size: 25px; }
.markdown-body h3 { margin: 34px 0 10px; font-size: 19px; }
.markdown-body h4 { margin: 28px 0 8px; font-size: 16px; }
.markdown-body p { margin: 12px 0; }
.markdown-body ul, .markdown-body ol { padding-left: 25px; }
.markdown-body li { margin: 4px 0; }
.markdown-body a { color: var(--primary); text-decoration-thickness: 1px; text-underline-offset: 3px; }
.markdown-body blockquote { margin: 22px 0; padding: 4px 16px; border-left: 3px solid var(--line-strong); color: var(--muted); }
.markdown-body code { padding: .18em .38em; border-radius: 5px; background: var(--code); border: 1px solid var(--line); font-family: "SFMono-Regular", Consolas, monospace; font-size: .9em; }
.markdown-body pre { position: relative; overflow: auto; margin: 22px 0; padding: 17px 18px; border: 1px solid var(--line); border-radius: 10px; background: var(--code); line-height: 1.6; }
.markdown-body pre code { padding: 0; border: 0; background: transparent; }
.copy-code { position: absolute; top: 8px; right: 8px; padding: 4px 7px; border: 1px solid var(--line); border-radius: 5px; background: var(--surface); color: var(--muted); font-size: 10px; }
.markdown-body table { display: block; max-width: 100%; overflow-x: auto; border-collapse: collapse; margin: 20px 0; }
.markdown-body th, .markdown-body td { padding: 9px 12px; border: 1px solid var(--line); text-align: left; }
.markdown-body th { background: var(--surface-2); }
.markdown-body img { max-width: 100%; height: auto; border-radius: 9px; border: 1px solid var(--line); }
.markdown-body hr { margin: 38px 0; border: 0; border-top: 1px solid var(--line); }

.editor { min-height: calc(100vh - 62px); }
.editor-top { min-height: 104px; display: flex; justify-content: space-between; align-items: center; gap: 20px; padding: 16px 24px; border-bottom: 1px solid var(--line); background: var(--surface); }
.editor-fields { flex: 1; min-width: 0; }
.title-input { width: 100%; margin-bottom: 9px; border: 0; outline: 0; background: transparent; font-size: 25px; font-weight: 800; letter-spacing: -.025em; }
.inline-fields { display: flex; gap: 7px; }
.inline-fields select, .inline-fields input { height: 32px; padding: 0 9px; border: 1px solid var(--line); border-radius: 7px; background: var(--surface-2); font-size: 11px; outline: 0; }
.inline-fields input { min-width: 250px; }
.editor-actions { display: flex; gap: 6px; flex: none; }
.split-editor { height: calc(100vh - 166px); display: grid; grid-template-columns: 1fr 1fr; }
#markdownInput { width: 100%; height: 100%; resize: none; padding: 28px 30px 80px; border: 0; border-right: 1px solid var(--line); outline: 0; background: var(--surface-2); color: var(--text); font: 13.5px/1.75 "SFMono-Regular", Consolas, monospace; tab-size: 2; }
.preview-pane { height: 100%; overflow: auto; padding: 28px 38px 80px; background: var(--surface); }

.toc-pane { display: none; }
@media (min-width: 1260px) {
  .workspace:has(.toc-pane.visible) { grid-template-columns: var(--sidebar-w) minmax(0, 1fr) var(--toc-w); }
  .toc-pane.visible { position: fixed; top: 62px; bottom: 0; right: 0; z-index: 10; width: var(--toc-w); display: block; padding: 78px 24px 30px; border-left: 1px solid var(--line); background: var(--bg); overflow: auto; }
  .workspace:has(.toc-pane.visible) .content-pane { padding-right: var(--toc-w); }
  .toc-title { margin-bottom: 10px; color: var(--muted); font-size: 11px; font-weight: 800; }
  #toc { display: flex; flex-direction: column; }
  #toc a { padding: 5px 0; color: var(--muted); text-decoration: none; font-size: 11px; line-height: 1.4; }
  #toc a:hover { color: var(--text); }
  #toc a[data-level="3"] { padding-left: 10px; }
  #toc a[data-level="4"] { padding-left: 20px; }
}

.modal-backdrop { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 20px; background: rgba(12, 16, 22, .56); backdrop-filter: blur(4px); }
.modal { width: min(680px, 100%); max-height: calc(100vh - 40px); overflow: auto; padding: 25px; border: 1px solid var(--line); border-radius: 14px; background: var(--surface); box-shadow: var(--shadow); }
.modal-head { display: flex; justify-content: space-between; gap: 20px; }
.modal-head .eyebrow { margin-bottom: 6px; }
.modal h2 { margin: 0; font-size: 24px; }
.modal-desc { color: var(--muted); line-height: 1.7; }
.modal-desc code { color: var(--text); }
.config-input { width: 100%; height: 245px; resize: vertical; padding: 14px; border: 1px solid var(--line); border-radius: 9px; outline: 0; background: var(--surface-2); font: 12px/1.6 "SFMono-Regular", Consolas, monospace; }
.config-input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 14%, transparent); }
.account-info { display: grid; grid-template-columns: auto 1fr; gap: 5px 12px; align-items: center; margin-top: 12px; padding: 12px; border: 1px solid var(--line); border-radius: 9px; background: var(--surface-2); font-size: 11px; }
.account-info strong { grid-row: span 2; }
.account-info code { word-break: break-all; }
.mini-copy { grid-column: 2; justify-self: start; border: 1px solid var(--line); border-radius: 5px; background: var(--surface); padding: 3px 7px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.setup-note { margin: 15px 0 0; color: var(--muted); font-size: 11px; }

.toast { position: fixed; left: 50%; bottom: 26px; z-index: 150; transform: translateX(-50%); max-width: min(520px, calc(100vw - 40px)); padding: 10px 14px; border-radius: 9px; background: #171a20; color: #fff; box-shadow: var(--shadow); font-size: 12px; font-weight: 700; }
.toast[data-type="error"] { background: #b52929; }
.mobile-only { display: none; }

@media (max-width: 900px) {
  :root { --sidebar-w: 290px; }
  .topbar { grid-template-columns: auto auto 1fr; gap: 8px; padding: 0 12px; }
  .mobile-only { display: grid; }
  .brand > span:last-child small { display: none; }
  .top-search { grid-column: 3; }
  .top-actions .btn-primary { display: none; }
  .top-actions { position: fixed; right: 12px; bottom: 14px; z-index: 35; padding: 6px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); box-shadow: var(--shadow); }
  .top-actions #newDocBtn { display: inline-block !important; }
  .workspace { grid-template-columns: 1fr; }
  .sidebar { transform: translateX(-100%); box-shadow: var(--shadow); transition: transform .2s ease; }
  .sidebar.open { transform: translateX(0); }
  .content-pane { grid-column: 1; }
  .welcome-card { padding: 70px 24px 120px; }
  .feature-grid { grid-template-columns: 1fr 1fr; }
  .feature-grid article:nth-child(2) { border-right: 0; }
  .feature-grid article:nth-child(-n+2) { border-bottom: 1px solid var(--line); }
  .viewer { padding: 50px 24px 120px; }
  .doc-header { flex-direction: column; }
  .editor-top { align-items: flex-start; flex-direction: column; }
  .editor-actions { width: 100%; overflow-x: auto; }
  .split-editor { height: auto; min-height: calc(100vh - 220px); grid-template-columns: 1fr; }
  #markdownInput { min-height: 55vh; border-right: 0; border-bottom: 1px solid var(--line); }
  .preview-pane { min-height: 55vh; }
  .inline-fields { flex-wrap: wrap; }
  .inline-fields input { min-width: 0; flex: 1 1 180px; }
}

@media (max-width: 620px) {
  .topbar { grid-template-columns: 36px 1fr; }
  .brand { grid-column: 2; }
  .top-search { position: fixed; top: 68px; left: 10px; right: 10px; z-index: 20; box-shadow: var(--shadow); display: none; }
  .top-search:focus-within { display: flex; }
  .status-bar { padding-left: 12px; padding-right: 12px; }
  .welcome-card h1 { font-size: 38px; }
  .feature-grid { grid-template-columns: 1fr; }
  .feature-grid article { border-right: 0; border-bottom: 1px solid var(--line); }
  .feature-grid article:last-child { border-bottom: 0; }
  .doc-header h1 { font-size: 31px; }
  .modal { padding: 18px; }
}
