:root {
  --bg: #FFFFFF;
  --bg-secondary: #F5F5F7;
  --bg-sidebar: #FBFBFD;
  --bg-hover: #F0F0F2;
  --bg-active: #E8F0FE;
  --text: #1D1D1F;
  --text-secondary: #86868B;
  --text-link: #0066CC;
  --accent2: #8250DF;
  --green: #1a7f37;
  --border: rgba(0,0,0,0.06);
  --border-light: rgba(0,0,0,0.04);
  --code-bg: #F5F5F7;
  --badge-bg: #E8F0FE;
  --badge-text: #0066CC;
  --sidebar-width: 280px;
  --scrollbar-thumb: #c1c1c1;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --bg-secondary: #1C1C1E;
    --bg-sidebar: #0A0A0A;
    --bg-hover: #2C2C2E;
    --bg-active: #1A3050;
    --text: #F5F5F7;
    --text-secondary: #86868B;
    --text-link: #2997FF;
    --accent2: #bc8cff;
    --green: #3fb950;
    --border: rgba(255,255,255,0.08);
    --border-light: rgba(255,255,255,0.04);
    --code-bg: #1C1C1E;
    --badge-bg: #1A3050;
    --badge-text: #2997FF;
    --scrollbar-thumb: #484f58;
  }
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'SF Pro Text', 'Inter', -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.75;
  font-size: 16px;
  letter-spacing: -0.01em;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  font-feature-settings: 'kern', 'liga';
  transition: background 0.2s, color 0.2s;
}

::selection {
  background: rgba(0, 102, 204, 0.2);
}

@media (prefers-color-scheme: dark) {
  ::selection {
    background: rgba(41, 151, 255, 0.2);
  }
}

*:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px rgba(0, 102, 204, 0.3);
  border-radius: 4px;
}

#app {
  display: flex;
  min-height: 100vh;
}

/* Sidebar */
#sidebar {
  width: var(--sidebar-width);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 100;
  overflow: hidden;
  transition: background 0.2s, border-color 0.2s;
}

.sidebar-header {
  padding: 16px 16px 8px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  transition: border-color 0.2s;
}

.sidebar-header h1 {
  font-family: 'SF Pro Display', 'Inter', system-ui, sans-serif;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.02em;
}

#sidebar-collapse {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 12px;
  padding: 4px 6px;
  border-radius: 4px;
  line-height: 1;
  transition: background 0.2s, color 0.2s;
}
#sidebar-collapse:hover { background: var(--bg-hover); color: var(--text); }

#sidebar-expand {
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 101;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 12px;
  padding: 6px 8px;
  border-radius: 6px;
  line-height: 1;
  transition: background 0.2s, color 0.2s, border-color 0.2s;
}
#sidebar-expand:hover { background: var(--bg-hover); color: var(--text); }
#sidebar-expand.hidden { display: none; }

#sidebar-resize {
  width: 4px;
  cursor: col-resize;
  background: transparent;
  position: fixed;
  top: 0;
  bottom: 0;
  left: var(--sidebar-width);
  z-index: 101;
  transition: background 0.15s;
}
#sidebar-resize:hover, #sidebar-resize.dragging {
  background: var(--text-link);
}
#sidebar-resize.hidden { display: none; }

#app.sidebar-collapsed #sidebar { display: none; }
#app.sidebar-collapsed #sidebar-resize { display: none; }
#app.sidebar-collapsed main { margin-left: 0; }

.search-box {
  padding: 8px 12px;
}

.search-box input {
  width: 100%;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s, background 0.2s;
}

.search-box input:focus {
  border-color: var(--text-link);
  box-shadow: 0 0 0 3px rgba(0, 102, 204, 0.15);
}

#tree, #search-results {
  flex: 1;
  overflow-y: auto;
  padding: 4px 0;
}

#search-results.hidden { display: none; }

/* Tree */
.tree-dir, .tree-file {
  cursor: pointer;
  user-select: none;
}

.tree-dir-label, .tree-file {
  display: flex;
  align-items: center;
  padding: 3px 8px 3px calc(var(--depth, 0) * 16px + 8px);
  font-size: 13px;
  border-radius: 4px;
  margin: 0 4px;
  gap: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background 0.15s;
}

.tree-dir-label:hover, .tree-file:hover {
  background: var(--bg-hover);
}

.tree-file.active {
  background: var(--bg-active);
  font-weight: 500;
}

.tree-chevron {
  display: inline-block;
  width: 16px;
  font-size: 10px;
  text-align: center;
  transition: transform 0.15s;
  flex-shrink: 0;
  color: var(--text-secondary);
}

.tree-chevron.open { transform: rotate(90deg); }

.tree-icon { flex-shrink: 0; font-size: 14px; }

.tree-name { overflow: hidden; text-overflow: ellipsis; }

.tree-count {
  margin-left: auto;
  font-size: 11px;
  color: var(--text-secondary);
  flex-shrink: 0;
}

.tree-children.collapsed { display: none; }

.status-badge {
  font-size: 11px;
  flex-shrink: 0;
}

/* Search results */
.search-result {
  padding: 8px 12px;
  cursor: pointer;
  border-bottom: 1px solid var(--border-light);
  transition: background 0.15s;
}

.search-result:hover { background: var(--bg-hover); }

.search-result-title {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-link);
}

.search-result-path {
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 2px;
}

/* Main content */
main {
  flex: 1;
  margin-left: var(--sidebar-width);
  padding: 24px 32px;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  transition: background 0.2s;
}

main > * {
  width: 100%;
  max-width: 800px;
}

#viewer {
  overflow-wrap: break-word;
  word-break: break-word;
  min-width: 0;
}

#breadcrumb {
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 4px;
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
}

#breadcrumb a {
  color: var(--text-link);
  text-decoration: none;
  font-weight: 500;
  transition: color 0.2s;
}

#breadcrumb a:hover { text-decoration: underline; }

#breadcrumb .sep { margin: 0 4px; }

#metadata {
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 16px;
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
}

#metadata.hidden { display: none; }

.tag {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 500;
  background: var(--badge-bg);
  color: var(--badge-text);
}

/* Article / markdown rendered content */
#viewer {
  font-size: 17.5px;
  line-height: 1.75;
  letter-spacing: -0.01em;
}

#viewer h1 {
  font-family: 'SF Pro Display', 'Inter', system-ui, sans-serif;
  font-size: 36px;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.2;
  margin: 48px 0 20px;
}

#viewer h1:first-child,
#viewer > h1:first-child {
  margin-top: 0;
}

#viewer h2 {
  font-family: 'SF Pro Display', 'Inter', system-ui, sans-serif;
  font-size: 27px;
  font-weight: 650;
  letter-spacing: -0.02em;
  line-height: 1.25;
  margin: 40px 0 14px;
}

#viewer h3 {
  font-family: 'SF Pro Display', 'Inter', system-ui, sans-serif;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.3;
  color: var(--text);
  margin: 32px 0 10px;
}

#viewer h4 {
  font-family: 'SF Pro Display', 'Inter', system-ui, sans-serif;
  font-size: 17px;
  margin: 26px 0 8px;
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.35;
}

#viewer p { margin: 0 0 1.25em; }

#viewer ul, #viewer ol { margin: 0 0 1.25em; padding-left: 24px; }

#viewer ul { list-style: none; }
#viewer ul > li { position: relative; padding-left: 0; }
#viewer ul > li::before {
  content: '';
  position: absolute;
  left: -16px;
  top: 0.65em;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--text-link);
}

#viewer li { margin: 0.5em 0; }

#viewer li > ul, #viewer li > ol { margin-top: 0.25em; margin-bottom: 0; }

#viewer a {
  color: var(--text-link);
  text-decoration: none;
  font-weight: 500;
  transition: text-decoration-color 0.2s;
}

#viewer a:hover {
  text-decoration: underline;
  text-underline-offset: 2px;
}

/* Inline code */
#viewer code {
  font-family: 'SF Mono', 'JetBrains Mono', monospace;
  font-size: 0.875em;
  background: var(--code-bg);
  color: var(--green);
  padding: 2px 6px;
  border-radius: 4px;
  transition: background 0.2s, color 0.2s;
}

/* Code blocks */
#viewer pre {
  background: var(--code-bg);
  border-radius: 12px;
  padding: 20px 24px;
  overflow-x: auto;
  margin: 0 0 1.25em;
  position: relative;
  box-shadow: 0 1px 3px rgba(0,0,0,0.04);
  transition: background 0.2s, box-shadow 0.2s;
  max-width: 100%;
}

/* Right-edge fade for horizontal overflow */
#viewer pre::after {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: 32px;
  background: linear-gradient(to right, transparent, var(--code-bg));
  border-radius: 0 12px 12px 0;
  pointer-events: none;
  opacity: 0.8;
}

#viewer pre code {
  background: none;
  color: inherit;
  padding: 0;
  font-size: 13.5px;
  line-height: 1.6;
  border-radius: 0;
}

/* Language label on code blocks */
.code-lang-label {
  position: absolute;
  top: 8px;
  right: 12px;
  font-size: 11px;
  font-family: 'SF Mono', 'JetBrains Mono', monospace;
  color: var(--text-secondary);
  opacity: 0.6;
  pointer-events: none;
  text-transform: lowercase;
}

/* Tables */
#viewer table {
  border-collapse: collapse;
  width: 100%;
  margin: 0 0 1.25em;
  font-size: 14px;
  display: block;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

#viewer th, #viewer td {
  border: none;
  padding: 12px 16px;
  text-align: left;
  transition: background 0.2s;
}

#viewer th {
  background: var(--bg-secondary);
  font-weight: 600;
}

#viewer tr:nth-child(even) td {
  background: var(--bg-secondary);
}

/* Blockquotes */
#viewer blockquote {
  border-left: 3px solid var(--accent2);
  padding: 8px 20px;
  margin: 0 0 1.25em;
  color: var(--text-secondary);
  font-style: italic;
  font-size: 0.95em;
  background: var(--bg-secondary);
  border-radius: 0 8px 8px 0;
  transition: background 0.2s, border-color 0.2s;
}

#viewer img {
  max-width: 100%;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}

#viewer hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 32px 0;
}

/* Welcome / empty state */
.welcome { text-align: center; padding-top: 10vh; }
.welcome h2 {
  font-family: 'SF Pro Display', 'Inter', system-ui, sans-serif;
  font-size: 24px;
  margin-bottom: 8px;
  font-weight: 700;
  letter-spacing: -0.02em;
}
.welcome .stats { font-size: 14px; color: var(--text-secondary); margin-bottom: 24px; }
.welcome .recent-list { text-align: left; max-width: 500px; margin: 0 auto; }
.welcome .recent-item {
  display: flex;
  justify-content: space-between;
  padding: 6px 0;
  border-bottom: 1px solid var(--border-light);
  font-size: 14px;
  cursor: pointer;
  transition: color 0.2s;
}
.welcome .recent-item:hover { color: var(--text-link); }
.welcome .recent-item .date { color: var(--text-secondary); font-size: 12px; }

/* Hamburger */
#menu-toggle {
  display: none;
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 200;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 18px;
  cursor: pointer;
  color: var(--text);
  transition: background 0.2s, border-color 0.2s;
}

#overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 90;
}

#overlay.hidden { display: none; }

/* Mobile */
@media (max-width: 768px) {
  #menu-toggle { display: block; }

  #sidebar {
    transform: translateX(-100%);
    transition: transform 0.25s ease;
  }

  #sidebar.open { transform: translateX(0); }

  main {
    margin-left: 0;
    padding: 16px;
    padding-top: 52px;
  }
}

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 3px; }

/* Auth gate */
#auth-gate {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: var(--bg);
  transition: background 0.2s;
}
#auth-gate.hidden { display: none; }
.auth-card {
  text-align: center;
  padding: 3rem 2.5rem;
  border-radius: 16px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  max-width: 360px;
  width: 90%;
  transition: background 0.2s, border-color 0.2s;
}
.auth-icon { font-size: 3rem; margin-bottom: 0.5rem; }
.auth-card h2 {
  margin: 0 0 0.25rem;
  font-family: 'SF Pro Display', 'Inter', system-ui, sans-serif;
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
}
.auth-card p { margin: 0 0 1.5rem; color: var(--text-secondary); font-size: 0.9rem; }
#auth-form { display: flex; flex-direction: column; gap: 0.75rem; }
#auth-input {
  padding: 0.75rem 1rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
  outline: none;
  transition: border-color 0.2s, background 0.2s;
}
#auth-input:focus { border-color: var(--text-link); }
#auth-form button {
  padding: 0.75rem;
  border-radius: 8px;
  border: none;
  background: var(--text-link);
  color: #fff;
  font-size: 1rem;
  font-family: inherit;
  font-weight: 500;
  cursor: pointer;
  transition: opacity 0.15s;
}
#auth-form button:hover { opacity: 0.85; }
#auth-error {
  margin-top: 0.75rem;
  color: #cf222e;
  font-size: 0.85rem;
}
#auth-error.hidden { display: none; }

/* Theme toggle */
#theme-toggle {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 14px;
  padding: 4px 6px;
  border-radius: 4px;
  line-height: 1;
  transition: background 0.2s, color 0.2s;
}
#theme-toggle:hover { background: var(--bg-hover); }

/* Manual theme overrides */
html[data-theme="dark"] {
  --bg: #000000;
  --bg-secondary: #1C1C1E;
  --bg-sidebar: #0A0A0A;
  --bg-hover: #2C2C2E;
  --bg-active: #1A3050;
  --text: #F5F5F7;
  --text-secondary: #86868B;
  --text-link: #2997FF;
  --accent2: #bc8cff;
  --green: #3fb950;
  --border: rgba(255,255,255,0.08);
  --border-light: rgba(255,255,255,0.04);
  --code-bg: #1C1C1E;
  --badge-bg: #1A3050;
  --badge-text: #2997FF;
  --scrollbar-thumb: #484f58;
}

html[data-theme="dark"] ::selection {
  background: rgba(41, 151, 255, 0.2);
}

html[data-theme="dark"] #viewer pre {
  box-shadow: none;
}

/* Back to top button */
#back-to-top {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 200;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  font-size: 18px;
  cursor: pointer;
  opacity: 1;
  transition: opacity 0.25s, background 0.2s, color 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}
#back-to-top:hover { background: var(--bg-hover); color: var(--text); }
#back-to-top.hidden { opacity: 0; pointer-events: none; }

/* Sidebar footer / sync status */
.sidebar-footer {
  padding: 8px 16px;
  border-top: 1px solid var(--border);
  font-size: 11px;
  color: var(--text-secondary);
  flex-shrink: 0;
}

/* Code copy button */
.code-copy-btn {
  position: absolute;
  top: 8px;
  right: 48px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 8px;
  cursor: pointer;
  font-size: 14px;
  opacity: 0;
  transition: opacity 0.15s;
  z-index: 2;
}
#viewer pre:hover .code-copy-btn { opacity: 1; }
.code-copy-btn:hover { background: var(--bg-hover); }
.code-copy-btn.copied { color: var(--green); }
/* If no lang label, JS positions right:8px */

html[data-theme="light"] {
  --bg: #FFFFFF;
  --bg-secondary: #F5F5F7;
  --bg-sidebar: #FBFBFD;
  --bg-hover: #F0F0F2;
  --bg-active: #E8F0FE;
  --text: #1D1D1F;
  --text-secondary: #86868B;
  --text-link: #0066CC;
  --accent2: #8250DF;
  --green: #1a7f37;
  --border: rgba(0,0,0,0.06);
  --border-light: rgba(0,0,0,0.04);
  --code-bg: #F5F5F7;
  --badge-bg: #E8F0FE;
  --badge-text: #0066CC;
  --scrollbar-thumb: #c1c1c1;
}
}
