/* ── Reset & Base ───────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
.hidden { display: none !important; }

:root {
  --sidebar-width: 280px;
  --header-height: 56px;
  --bg: #f7f8fa;
  --surface: #ffffff;
  --sidebar-bg: #1e2130;
  --sidebar-text: #c9cfe0;
  --sidebar-hover: #2e3348;
  --sidebar-active: #3d4463;
  --sidebar-active-text: #ffffff;
  --accent: #e8403a;
  --accent-hover: #c73530;
  --text: #1a1d2e;
  --text-muted: #6b7280;
  --border: #e5e7eb;
  --code-bg: #f3f4f6;
  --shadow: 0 1px 4px rgba(0,0,0,.08);
  --radius: 6px;
}

html, body { height: 100%; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; font-size: 15px; color: var(--text); background: var(--bg); }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ── Layout ─────────────────────────────────────────────────────────── */
#app { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }

/* ── Header ─────────────────────────────────────────────────────────── */
#header {
  display: flex; align-items: center; gap: 12px;
  height: var(--header-height); padding: 0 16px;
  background: var(--sidebar-bg); color: #fff;
  flex-shrink: 0; z-index: 100; box-shadow: 0 2px 6px rgba(0,0,0,.25);
}
#header .logo { font-size: 1.15rem; font-weight: 700; white-space: nowrap; cursor: pointer; }
#header .logo:hover { opacity: .85; }

.search-wrap { position: relative; flex: 1; max-width: 420px; }
#search-input {
  width: 100%; padding: 7px 14px; border-radius: 20px;
  border: none; background: rgba(255,255,255,.12); color: #fff;
  font-size: .9rem; outline: none; transition: background .2s;
}
#search-input::placeholder { color: rgba(255,255,255,.5); }
#search-input:focus { background: rgba(255,255,255,.2); }

#search-results {
  position: absolute; top: calc(100% + 6px); left: 0; right: 0;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: 0 4px 16px rgba(0,0,0,.12);
  max-height: 380px; overflow-y: auto; z-index: 200; display: none;
}
#search-results.visible { display: block; }
.search-item {
  padding: 10px 14px; cursor: pointer; border-bottom: 1px solid var(--border);
  transition: background .15s;
}
.search-item:last-child { border-bottom: none; }
.search-item:hover { background: var(--bg); }
.search-item .si-title { font-weight: 600; color: var(--text); font-size: .9rem; }
.search-item .si-path { font-size: .75rem; color: var(--text-muted); margin: 1px 0; }
.search-item .si-excerpt { font-size: .82rem; color: var(--text-muted); }
.search-item .si-excerpt em { color: var(--accent); font-style: normal; font-weight: 600; }

/* ── Search highlight in document ────────────────────────────── */
mark.search-highlight {
  background: #fde68a; color: var(--text);
  padding: 2px 0; border-radius: 2px;
  outline: 2px solid #f59e0b; outline-offset: 1px;
  transition: background .5s, outline-color .5s;
}
mark.search-highlight.fade {
  background: transparent; outline-color: transparent;
}

#btn-vscode {
  margin-left: auto; white-space: nowrap; padding: 6px 14px;
  background: rgba(255,255,255,.1); border: 1px solid rgba(255,255,255,.25);
  border-radius: var(--radius); color: #fff; cursor: pointer; font-size: .85rem;
  transition: background .15s;
}
#btn-vscode:hover { background: rgba(255,255,255,.2); }
#btn-vscode.hidden { display: none; }

/* ── Body ────────────────────────────────────────────────────────────── */
#body { display: flex; flex: 1; overflow: hidden; }

/* ── Sidebar ─────────────────────────────────────────────────────────── */
#sidebar {
  width: var(--sidebar-width); background: var(--sidebar-bg); color: var(--sidebar-text);
  display: flex; flex-direction: column; flex-shrink: 0; overflow: hidden;
}
#sidebar-actions {
  display: flex; gap: 8px; padding: 10px 12px; flex-shrink: 0;
  border-bottom: 1px solid rgba(255,255,255,.07);
  flex-wrap: wrap;
}
.sidebar-btn {
  flex: 1; padding: 6px 8px; border-radius: var(--radius);
  background: rgba(255,255,255,.08); border: none; color: var(--sidebar-text);
  cursor: pointer; font-size: .82rem; transition: background .15s;
}
.sidebar-btn:hover { background: rgba(255,255,255,.15); color: #fff; }
.sidebar-btn.accent { background: var(--accent); color: #fff; }
.sidebar-btn.accent:hover { background: var(--accent-hover); }

#tree { flex: 1; overflow-y: auto; padding: 8px 0; }
#tree::-webkit-scrollbar { width: 4px; }
#tree::-webkit-scrollbar-thumb { background: rgba(255,255,255,.15); border-radius: 4px; }

.tree-item {
  display: flex; align-items: center; gap: 6px;
  padding: 5px 12px 5px 0; cursor: pointer;
  border-radius: 4px; transition: background .12s; white-space: nowrap;
  font-size: .87rem; color: var(--sidebar-text);
}
.tree-item:hover { background: var(--sidebar-hover); }
.tree-item.active { background: var(--sidebar-active); color: var(--sidebar-active-text); }
.tree-item .ti-icon { width: 16px; text-align: center; flex-shrink: 0; }
.tree-item .ti-toggle { width: 16px; flex-shrink: 0; font-size: .65rem; color: rgba(255,255,255,.35); text-align: center; }
.tree-item .ti-name { overflow: hidden; text-overflow: ellipsis; }
.tree-item .ti-delete {
  margin-left: auto; font-size: .7rem; color: rgba(255,255,255,.25);
  padding: 2px 6px; border-radius: 3px; opacity: 0; transition: all .15s;
  flex-shrink: 0;
}
.tree-item:hover .ti-delete { opacity: 1; }
.tree-item .ti-delete:hover { color: #dc2626; background: rgba(220,38,38,.15); }

.tree-children { display: none; }
.tree-children.open { display: block; }

/* ── Main content ────────────────────────────────────────────────────── */
#main { flex: 1; display: flex; flex-direction: column; overflow: hidden; }

#breadcrumb {
  display: flex; align-items: center; gap: 4px;
  padding: 8px 24px; font-size: .82rem; color: var(--text-muted);
  background: var(--surface); border-bottom: 1px solid var(--border); flex-shrink: 0;
}
#breadcrumb span { cursor: pointer; }
#breadcrumb span:hover { color: var(--accent); }
#breadcrumb .sep { color: var(--border); }

#content-wrap { flex: 1; overflow-y: auto; padding: 0; }

/* ── Doc actions bar ─────────────────────────────────────────────────── */
#doc-actions {
  display: none; align-items: center; gap: 8px;
  padding: 10px 24px; background: var(--surface);
  border-bottom: 1px solid var(--border); flex-shrink: 0;
}
#doc-actions.visible { display: flex; }
.action-btn {
  padding: 5px 14px; border-radius: var(--radius); border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; font-size: .85rem; color: var(--text);
  transition: all .15s;
}
.action-btn:hover { border-color: var(--accent); color: var(--accent); }
.action-btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.action-btn.primary:hover { background: var(--accent-hover); }
.action-btn.danger:hover { border-color: #dc2626; color: #dc2626; }
.action-btn.save { background: #16a34a; color: #fff; border-color: #16a34a; }
.action-btn.save:hover { background: #15803d; }
#doc-meta { margin-left: auto; font-size: .78rem; color: var(--text-muted); }

/* ── Home page ───────────────────────────────────────────────────────── */
#home-page { padding: 32px 32px; max-width: 900px; }
.home-hero { margin-bottom: 32px; }
.home-hero h1 { font-size: 2rem; margin-bottom: 8px; }
.home-hero p { color: var(--text-muted); font-size: 1.05rem; }

.home-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; }
.home-card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; }
.home-card h3 { font-size: 1rem; margin-bottom: 14px; color: var(--text); display: flex; align-items: center; gap: 6px; }
.home-card ul { list-style: none; }
.home-card li { padding: 5px 0; border-bottom: 1px solid var(--border); font-size: .88rem; }
.home-card li:last-child { border-bottom: none; }
.home-card li a { color: var(--text); }
.home-card li a:hover { color: var(--accent); text-decoration: none; }
.home-card .meta { font-size: .75rem; color: var(--text-muted); }

/* ── Markdown rendering ──────────────────────────────────────────────── */
#doc-view { padding: 28px 32px; max-width: 860px; }
#doc-view h1 { font-size: 1.85rem; margin-bottom: 8px; padding-bottom: 10px; border-bottom: 2px solid var(--border); }
#doc-view h2 { font-size: 1.35rem; margin: 28px 0 10px; padding-bottom: 6px; border-bottom: 1px solid var(--border); }
#doc-view h3 { font-size: 1.1rem; margin: 20px 0 8px; }
#doc-view h4 { font-size: 1rem; margin: 16px 0 6px; }
#doc-view p { margin: 10px 0; line-height: 1.7; }
#doc-view ul, #doc-view ol { margin: 10px 0 10px 22px; line-height: 1.7; }
#doc-view li { margin: 4px 0; }
#doc-view blockquote {
  margin: 14px 0; padding: 10px 16px;
  border-left: 4px solid var(--accent); background: #fff5f5;
  border-radius: 0 var(--radius) var(--radius) 0; color: var(--text-muted);
}
#doc-view code {
  font-family: 'SF Mono', 'Fira Code', monospace; font-size: .88em;
  background: var(--code-bg); padding: 2px 5px; border-radius: 3px;
}
#doc-view pre { background: #1e2130; border-radius: var(--radius); padding: 16px 20px; overflow-x: auto; margin: 14px 0; }
#doc-view pre code { background: none; color: #e2e8f0; font-size: .87rem; padding: 0; }
#doc-view table { width: 100%; border-collapse: collapse; margin: 14px 0; font-size: .9rem; }
#doc-view th { background: var(--sidebar-bg); color: #fff; padding: 8px 12px; text-align: left; }
#doc-view td { padding: 7px 12px; border-bottom: 1px solid var(--border); }
#doc-view tr:hover td { background: var(--bg); }
#doc-view img { max-width: 100%; border-radius: var(--radius); margin: 10px 0; box-shadow: var(--shadow); }
#doc-view hr { border: none; border-top: 1px solid var(--border); margin: 24px 0; }
#doc-view a { color: var(--accent); }

/* ── Index page file tree ────────────────────────────────────────────── */
.index-tree-section {
  margin-top: 28px; padding-top: 20px; border-top: 2px solid var(--border);
}
.index-tree-section h3 {
  font-size: 1.05rem; margin-bottom: 14px; color: var(--text);
}
.index-tree {
  background: var(--bg); border-radius: var(--radius);
  border: 1px solid var(--border); padding: 14px 16px;
}
.index-tree-item {
  display: flex; align-items: center; gap: 8px;
  padding: 5px 0; font-size: .9rem; transition: background .12s;
  border-radius: 4px;
}
.index-tree-item:hover { background: rgba(0,0,0,.03); }
.index-tree-item .itree-icon { font-size: .85rem; flex-shrink: 0; width: 18px; text-align: center; }
.index-tree-item a { color: var(--text); text-decoration: none; }
.index-tree-item a:hover { color: var(--accent); }
.index-tree-item.folder a { font-weight: 600; }
.index-tree-item.file a { color: var(--text-muted); }
.index-tree-item.file a:hover { color: var(--accent); }

/* ── Editor ──────────────────────────────────────────────────────────── */
#editor-wrap { display: none; flex-direction: column; flex: 1; padding: 20px 24px; }
#editor-wrap.visible { display: flex; }
#editor-path-input {
  width: 100%; padding: 8px 12px; margin-bottom: 14px;
  border: 1px solid var(--border); border-radius: var(--radius);
  font-family: monospace; font-size: .9rem; background: var(--bg);
}
#editor-path-input:focus { outline: none; border-color: var(--accent); }
.EasyMDEContainer { flex: 1; display: flex; flex-direction: column; }
.EasyMDEContainer .CodeMirror { flex: 1; font-family: 'SF Mono', 'Fira Code', monospace; font-size: .9rem; }
.editor-toolbar { background: var(--surface); border-color: var(--border); }

/* ── Comments ────────────────────────────────────────────────────────── */
#comments-section { border-top: 1px solid var(--border); padding: 28px 32px; max-width: 860px; }
#comments-section h3 { font-size: 1rem; margin-bottom: 16px; color: var(--text-muted); }
.comment {
  display: flex; gap: 12px; margin-bottom: 16px;
  padding-bottom: 16px; border-bottom: 1px solid var(--border);
}
.comment:last-of-type { border-bottom: none; }
.comment-avatar {
  width: 36px; height: 36px; border-radius: 50%; background: var(--accent);
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 700; font-size: .9rem; flex-shrink: 0;
}
.comment-body { flex: 1; }
.comment-meta { font-size: .8rem; color: var(--text-muted); margin-bottom: 4px; }
.comment-meta strong { color: var(--text); }
.comment-text { font-size: .9rem; line-height: 1.6; }

.comment-form { display: flex; gap: 10px; margin-top: 18px; flex-wrap: wrap; }
.comment-form input, .comment-form textarea {
  padding: 8px 12px; border: 1px solid var(--border); border-radius: var(--radius);
  font-size: .88rem; font-family: inherit; background: var(--surface); color: var(--text);
  outline: none; transition: border-color .15s;
}
.comment-form input:focus, .comment-form textarea:focus { border-color: var(--accent); }
#comment-author { width: 200px; }
#comment-text { flex: 1; min-width: 200px; resize: vertical; min-height: 60px; }
#comment-submit { align-self: flex-end; }

/* ── New file dialog ─────────────────────────────────────────────────── */
#dialog-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  display: none; align-items: center; justify-content: center; z-index: 1000;
}
#dialog-overlay.visible { display: flex; }
#dialog-box {
  background: var(--surface); border-radius: 10px; padding: 28px;
  width: 440px; max-width: 90vw; box-shadow: 0 8px 32px rgba(0,0,0,.2);
}
#dialog-box h2 { font-size: 1.1rem; margin-bottom: 16px; }
.dialog-field { margin-bottom: 14px; }
.dialog-field label { display: block; font-size: .85rem; font-weight: 600; margin-bottom: 5px; color: var(--text-muted); }
.dialog-field input, .dialog-field select {
  width: 100%; padding: 8px 12px; border: 1px solid var(--border);
  border-radius: var(--radius); font-size: .9rem; color: var(--text); background: var(--bg);
}
.dialog-field input:focus, .dialog-field select:focus { outline: none; border-color: var(--accent); }
.dialog-hint { font-size: .78rem; color: var(--text-muted); margin-top: 4px; }
.dialog-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; }

/* ── Toast ───────────────────────────────────────────────────────────── */
#toast {
  position: fixed; bottom: 24px; right: 24px; padding: 10px 18px;
  background: #1e2130; color: #fff; border-radius: var(--radius);
  font-size: .88rem; box-shadow: 0 4px 14px rgba(0,0,0,.3);
  opacity: 0; transform: translateY(8px); transition: all .25s; z-index: 2000;
}
#toast.show { opacity: 1; transform: translateY(0); }
#toast.error { background: #dc2626; }

/* ── Spinner ─────────────────────────────────────────────────────────── */
.spinner {
  display: inline-block; width: 20px; height: 20px;
  border: 2px solid var(--border); border-top-color: var(--accent);
  border-radius: 50%; animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Scrollbars ──────────────────────────────────────────────────────── */
#content-wrap::-webkit-scrollbar { width: 6px; }
#content-wrap::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* ── Mobile menu button ──────────────────────────────────────────── */
.mobile-menu-btn {
  display: none;
  background: none;
  border: none;
  color: #fff;
  font-size: 1.4rem;
  cursor: pointer;
  padding: 4px 8px;
  line-height: 1;
}
.mobile-menu-btn:hover { opacity: .8; }

/* ── Language toggle ─────────────────────────────────────────────── */
.lang-btn {
  background: rgba(255,255,255,.1);
  border: 1px solid rgba(255,255,255,.25);
  border-radius: var(--radius);
  color: #fff;
  cursor: pointer;
  font-size: .85rem;
  padding: 6px 12px;
  white-space: nowrap;
  transition: background .15s;
}
.lang-btn:hover { background: rgba(255,255,255,.2); }

/* ── Sidebar overlay (mobile) ────────────────────────────────────── */
#sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  top: var(--header-height);
  background: rgba(0,0,0,.4);
  z-index: 49;
}
#sidebar-overlay.visible { display: block; }

/* ── New folder dialog ───────────────────────────────────────────── */
#folder-dialog-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  display: none; align-items: center; justify-content: center; z-index: 1000;
}
#folder-dialog-overlay.visible { display: flex; }
.dialog-box-styled {
  background: var(--surface); border-radius: 10px; padding: 28px;
  width: 440px; max-width: 90vw; box-shadow: 0 8px 32px rgba(0,0,0,.2);
}
.dialog-box-styled h2 { font-size: 1.1rem; margin-bottom: 16px; }

/* ── Rename dialog ───────────────────────────────────────────── */
#rename-dialog-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  display: none; align-items: center; justify-content: center; z-index: 1000;
}
#rename-dialog-overlay.visible { display: flex; }

/* ── Rename button in tree ───────────────────────────────────── */
.tree-item .ti-rename {
  font-size: .7rem; color: rgba(255,255,255,.25);
  padding: 2px 5px; border-radius: 3px; opacity: 0; transition: all .15s;
  flex-shrink: 0; cursor: pointer;
}
.tree-item:hover .ti-rename { opacity: 1; }
.tree-item .ti-rename:hover { color: var(--accent); background: rgba(232,64,58,.15); }

/* ── Responsive ──────────────────────────────────────────────────── */
@media (max-width: 768px) {
  .mobile-menu-btn { display: block; }
  #sidebar {
    position: fixed;
    top: var(--header-height);
    left: 0;
    bottom: 0;
    z-index: 50;
    width: var(--sidebar-width);
    transform: translateX(-100%);
    transition: transform .25s ease;
  }
  #sidebar.open {
    transform: translateX(0);
  }
  .home-grid { grid-template-columns: 1fr; }
  #doc-view { padding: 20px 16px; }
  #comments-section { padding: 20px 16px; }
  #btn-vscode { display: none !important; }
}

/* ── Recent Comments Widget ─────────────────────────────────────── */
.recent-comments-list { display: flex; flex-direction: column; gap: 10px; }

/* ── Document Link Widget (editor toolbar) ──────────────────────── */
.doc-link-widget {
  position: absolute; top: 100%; left: 0; right: 0;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 0 0 var(--radius) var(--radius);
  box-shadow: 0 4px 16px rgba(0,0,0,.12);
  z-index: 300; max-height: 320px; overflow: hidden;
  display: flex; flex-direction: column;
}
.doc-link-input {
  width: 100%; padding: 10px 14px; border: none;
  border-bottom: 1px solid var(--border); font-size: .9rem;
  outline: none; background: var(--bg); color: var(--text);
}
.doc-link-input:focus { background: #fff; }
.doc-link-results { overflow-y: auto; max-height: 260px; }
.dlr-item {
  padding: 10px 14px; cursor: pointer;
  transition: background .15s; border-bottom: 1px solid var(--border);
}
.dlr-item:last-child { border-bottom: none; }
.dlr-item:hover { background: var(--bg); }
.dlr-title { font-weight: 600; color: var(--text); font-size: .9rem; }
.dlr-path { font-size: .75rem; color: var(--text-muted); margin: 1px 0; }
.dlr-excerpt { font-size: .82rem; color: var(--text-muted); }
.dlr-excerpt em { color: var(--accent); font-style: normal; font-weight: 600; }
.dlr-empty { padding: 12px 14px; color: var(--text-muted); font-size: .85rem; text-align: center; }
.recent-comment {
  display: flex; align-items: flex-start; gap: 10px;
  padding: 8px 0; border-bottom: 1px solid var(--border);
}
.recent-comment:last-child { border-bottom: none; }
.rc-avatar {
  width: 30px; height: 30px; border-radius: 50%; background: var(--accent);
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 700; font-size: .78rem; flex-shrink: 0;
}
.rc-body { flex: 1; min-width: 0; }
.rc-meta { font-size: .78rem; color: var(--text-muted); margin-bottom: 2px; }
.rc-meta strong { color: var(--text); }
.rc-meta a { color: var(--accent); text-decoration: none; }
.rc-meta a:hover { text-decoration: underline; }
.rc-text { font-size: .85rem; color: var(--text); line-height: 1.5; word-break: break-word; }
