/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #fafafa;
  --surface: #ffffff;
  --text: #1a1a1a;
  --text-muted: #6b7280;
  --border: #e5e7eb;
  --primary: #5C6BC0;
  --primary-hover: #4a5ab0;
  --danger: #dc2626;
  --success: #16a34a;
  --sidebar-w: 280px;
  --topbar-h: 56px;
  --radius: 6px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}

/* === Top Bar === */
.top-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--topbar-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  z-index: 100;
}

.logo {
  font-weight: 700;
  font-size: 16px;
  color: var(--text);
  text-decoration: none;
}

.top-bar nav { display: flex; align-items: center; gap: 16px; }
.top-bar nav a { color: var(--text-muted); text-decoration: none; font-size: 14px; }
.top-bar nav a:hover { color: var(--text); }

/* === Layout === */
main { margin-top: var(--topbar-h); }
.container { max-width: 960px; margin: 0 auto; padding: 40px 40px; }

/* === Doc Layout (sidebar + content) === */
.doc-layout {
  display: flex;
  min-height: calc(100vh - var(--topbar-h));
  position: relative;
}

/* === Sidebar === */
.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 20px 0;
  overflow-y: auto;
  position: fixed;
  top: var(--topbar-h);
  bottom: 0;
  left: 0;
}

.sidebar-header { padding: 0 16px 16px; border-bottom: 1px solid var(--border); }
.sidebar-title {
  font-weight: 600;
  font-size: 14px;
  color: var(--text);
  text-decoration: none;
}

.sidebar-nav { padding: 12px 0; }

.nav-tree { list-style: none; padding: 0; }
.nav-tree .nav-tree { padding-left: 16px; }
.nav-tree li { margin: 1px 0; }

.nav-icon { width: 14px; height: 14px; margin-right: 6px; vertical-align: -2px; flex-shrink: 0; }
.nav-icon-dir { color: var(--primary); }
.nav-icon-file { color: var(--text-muted); }
.nav-file.active .nav-icon-file { color: var(--primary); }

.nav-dir-toggle {
  display: flex;
  align-items: center;
  padding: 6px 16px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
}
.nav-dir-toggle:hover { color: var(--text); }
.nav-dir-toggle::after { content: '\25B8'; margin-left: auto; font-size: 10px; transition: transform 0.15s; }
.nav-dir.open > .nav-dir-toggle::after { transform: rotate(90deg); }
.nav-dir:not(.open) > .nav-tree { display: none; }

.nav-file a {
  display: flex;
  align-items: center;
  padding: 5px 16px;
  font-size: 13px;
  color: var(--text-muted);
  text-decoration: none;
  border-radius: var(--radius);
  margin: 0 8px;
}
.nav-file a:hover { background: var(--bg); color: var(--text); }
.nav-file.active a { background: #eef0ff; color: var(--primary); font-weight: 500; }

/* === Document Content === */
.doc-content {
  flex: 1;
  margin-left: var(--sidebar-w);
  padding: 32px 48px;
  max-width: 820px;
  min-width: 0;
}

.doc-header h1 {
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 8px;
  line-height: 1.3;
}

.doc-body { margin-top: 24px; }

/* === Markdown Styling === */
.doc-body h1 { font-size: 24px; margin-top: 40px; margin-bottom: 16px; }
.doc-body h2 { font-size: 20px; margin-top: 32px; margin-bottom: 12px; border-bottom: 1px solid var(--border); padding-bottom: 8px; }
.doc-body h3 { font-size: 16px; margin-top: 24px; margin-bottom: 8px; }
.doc-body p { margin-bottom: 16px; }
.doc-body ul, .doc-body ol { margin-bottom: 16px; padding-left: 24px; }
.doc-body li { margin-bottom: 4px; }
.doc-body a { color: var(--primary); }

.doc-body code {
  font-family: var(--font-mono);
  font-size: 13px;
  background: #f3f4f6;
  padding: 2px 6px;
  border-radius: 4px;
}

.doc-body pre {
  background: #1e1e2e;
  color: #cdd6f4;
  padding: 16px;
  border-radius: var(--radius);
  overflow-x: auto;
  margin-bottom: 16px;
  font-size: 13px;
  line-height: 1.5;
}
.doc-body pre.mermaid {
  background: #ffffff;
  color: var(--text);
  text-align: center;
  padding: 24px 16px;
  border: 1px solid var(--border);
}
.doc-body pre code { background: none; padding: 0; color: inherit; }

.doc-body blockquote {
  border-left: 3px solid var(--primary);
  padding: 8px 16px;
  margin-bottom: 16px;
  color: var(--text-muted);
  background: #f8f9ff;
  border-radius: 0 var(--radius) var(--radius) 0;
}

.doc-body table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 16px;
}
.doc-body th, .doc-body td {
  padding: 8px 12px;
  border: 1px solid var(--border);
  text-align: left;
  font-size: 14px;
}
.doc-body th { background: var(--bg); font-weight: 600; }

.doc-body img { max-width: 100%; border-radius: var(--radius); }
.doc-body hr { border: none; border-top: 1px solid var(--border); margin: 32px 0; }

/* Callouts */
.callout {
  padding: 12px 16px;
  border-radius: var(--radius);
  margin-bottom: 16px;
  border-left: 4px solid;
}
.callout-title { font-weight: 600; margin-bottom: 4px; }
.callout-note { background: #eff6ff; border-color: #3b82f6; }
.callout-warning { background: #fffbeb; border-color: #f59e0b; }
.callout-danger, .callout-important { background: #fef2f2; border-color: #ef4444; }
.callout-tip { background: #f0fdf4; border-color: #22c55e; }
.callout-info { background: #eff6ff; border-color: #3b82f6; }

/* === Annotations Panel === */
.annotations-panel {
  position: fixed;
  top: var(--topbar-h);
  right: -360px;
  width: 360px;
  bottom: 0;
  background: var(--surface);
  border-left: 1px solid var(--border);
  transition: right 0.2s ease;
  overflow-y: auto;
  z-index: 90;
}
.annotations-panel.open { right: 0; }

.annotations-header {
  padding: 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.annotations-header h3 { font-size: 16px; }

.annotation {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.annotation.resolved { opacity: 0.5; }
.annotation-meta {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  margin-bottom: 4px;
}
.annotation-meta strong { color: var(--text); }
.annotation-meta time { color: var(--text-muted); }
.annotation-anchor {
  font-size: 12px;
  color: var(--text-muted);
  font-style: italic;
  margin-bottom: 6px;
}
.annotation-body { font-size: 14px; }
.annotation-actions { margin-top: 8px; display: flex; gap: 8px; }

/* Threaded replies */
.annotation-thread { border-bottom: 1px solid var(--border); }
.annotation-thread:last-child { border-bottom: none; }
.annotation-replies {
  padding-left: 16px;
  border-left: 2px solid #e0e0e0;
  margin-left: 16px;
}
.annotation.reply { padding: 8px 12px; border-bottom: none; }
.annotation.reply + .annotation.reply { border-top: 1px solid #f0f0f0; }
.btn-reply { color: var(--primary); border-color: var(--primary); }

.reply-form {
  padding: 8px 16px 12px;
  margin-left: 16px;
}
.reply-input {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 8px;
  font-family: var(--font);
  font-size: 13px;
  resize: vertical;
}
.reply-actions { display: flex; gap: 8px; margin-top: 6px; }

/* === Annotation Popover === */
.annotation-popover {
  position: absolute;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  width: 300px;
  z-index: 200;
}
.annotation-popover textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 8px;
  font-family: var(--font);
  font-size: 14px;
  resize: vertical;
}
.popover-actions { display: flex; gap: 8px; margin-top: 8px; justify-content: flex-end; }

/* Text selection highlight for annotated text */
.annotated-text {
  background: #fef3c7;
  cursor: pointer;
  border-bottom: 2px solid #f59e0b;
}

/* === Auth Pages === */
.auth-page {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding-top: 100px;
  min-height: calc(100vh - var(--topbar-h));
  padding-left: 24px;
  padding-right: 24px;
}
.auth-card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 36px 40px;
}
.auth-card h1 { font-size: 22px; margin-bottom: 24px; }

/* === Forms === */
.form { max-width: 600px; }
.form-group { margin-bottom: 20px; }

/* === Autocomplete === */
.autocomplete-wrap { position: relative; }
.autocomplete-list {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 var(--radius) var(--radius);
  max-height: 240px;
  overflow-y: auto;
  z-index: 50;
  box-shadow: 0 4px 12px rgba(0,0,0,0.08);
}
.autocomplete-item {
  padding: 8px 12px;
  font-size: 14px;
  cursor: pointer;
}
.autocomplete-item:hover { background: var(--bg); color: var(--primary); }
.form-group label { display: block; font-size: 14px; font-weight: 500; margin-bottom: 4px; }
.form-group input, .form-group textarea, .form-group select {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  font-family: var(--font);
}
.form-group input:focus, .form-group textarea:focus, .form-group select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(92, 107, 192, 0.1);
}
.form-group small { display: block; color: var(--text-muted); font-size: 12px; margin-top: 4px; }
.form-actions { display: flex; gap: 12px; align-items: center; margin-top: 24px; }

/* === Buttons === */
.btn {
  display: inline-flex;
  align-items: center;
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  font-family: var(--font);
  cursor: pointer;
  text-decoration: none;
  background: var(--surface);
  color: var(--text);
}
.btn:hover { background: var(--bg); }
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-full { width: 100%; justify-content: center; }
.btn-sm { font-size: 12px; padding: 4px 10px; }
.btn-link {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 14px;
  font-family: var(--font);
  padding: 0;
}
.btn-link:hover { color: var(--text); }
.btn-close { background: none; border: none; font-size: 24px; cursor: pointer; color: var(--text-muted); }

/* === Version History & Diff === */
.version-header { margin-bottom: 24px; }
.version-header h1 { margin-bottom: 8px; }
.breadcrumb { font-size: 14px; color: var(--text-muted); }
.breadcrumb a { color: var(--primary); text-decoration: none; }

.diff-selector { margin-top: 16px; }
.diff-selector .btn { margin-top: 16px; }

.diff-meta {
  display: flex;
  gap: 24px;
  margin-bottom: 16px;
  font-size: 14px;
}
.diff-meta code { margin-left: 8px; }

.diff-output { margin-top: 16px; }
.diff-pre {
  background: #1e1e2e;
  color: #cdd6f4;
  padding: 16px;
  border-radius: var(--radius);
  overflow-x: auto;
  font-size: 13px;
  line-height: 1.6;
  font-family: var(--font-mono);
}
.diff-add { color: #a6e3a1; background: rgba(166,227,161,0.1); display: inline-block; width: 100%; }
.diff-remove { color: #f38ba8; background: rgba(243,139,168,0.1); display: inline-block; width: 100%; }
.diff-same { color: #6c7086; }

/* === Alerts === */
.alert { padding: 10px 14px; border-radius: var(--radius); margin-bottom: 16px; font-size: 14px; }
.alert-error { background: #fef2f2; color: #dc2626; border: 1px solid #fecaca; }
.alert-success { background: #f0fdf4; color: #16a34a; border: 1px solid #bbf7d0; }

/* === Tables === */
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th, .table td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); }
.table th { font-weight: 600; color: var(--text-muted); font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px; }
.table tr:hover { background: var(--bg); }
.row-resolved { opacity: 0.5; }

/* === Badges === */
.badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 12px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}
.badge-admin { background: #ede9fe; color: #7c3aed; }
.badge-viewer { background: #e0f2fe; color: #0284c7; }
.badge-success { background: #dcfce7; color: #16a34a; }
.badge-pending { background: #fef3c7; color: #d97706; }
.badge-expired { background: #f3f4f6; color: #6b7280; }

/* === Admin === */
.admin-section { margin-bottom: 40px; }
.section-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.section-header h2 { font-size: 18px; }

.filter-bar { display: flex; gap: 12px; margin-bottom: 16px; align-items: center; }
.filter-bar select { padding: 6px 10px; border: 1px solid var(--border); border-radius: var(--radius); font-size: 14px; }

/* === Home === */
.home h1 { font-size: 24px; margin-bottom: 24px; }
.site-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; }
.site-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 24px;
  text-decoration: none;
  color: var(--text);
  transition: border-color 0.15s, box-shadow 0.15s;
}
.site-card:hover { border-color: var(--primary); box-shadow: 0 2px 8px rgba(92,107,192,0.1); }
.site-card h2 { font-size: 18px; margin-bottom: 8px; }
.site-card p { font-size: 14px; color: var(--text-muted); }

/* === Error Pages === */
.error-page { text-align: center; padding-top: 80px; }
.error-page h1 { font-size: 48px; color: var(--text-muted); }
.error-page p { margin: 16px 0; color: var(--text-muted); }

/* === Directory Listing === */
.dir-listing { display: flex; flex-direction: column; gap: 2px; }
.dir-item {
  display: flex;
  align-items: center;
  padding: 10px 14px;
  border-radius: var(--radius);
  text-decoration: none;
  color: var(--text);
  font-size: 14px;
  transition: background 0.1s;
}
.dir-item:hover { background: var(--bg); }
.dir-item-icon { width: 18px; height: 18px; margin-right: 10px; flex-shrink: 0; }
.dir-item-directory .dir-item-icon { color: var(--primary); }
.dir-item-file .dir-item-icon { color: var(--text-muted); }
.dir-item-name { flex: 1; }
.dir-item-count { font-size: 12px; color: var(--text-muted); }

/* === Misc === */
.empty-state { color: var(--text-muted); font-size: 14px; padding: 24px 0; }
.text-sm { font-size: 12px; }
code { font-family: var(--font-mono); }

/* === Responsive === */
@media (max-width: 768px) {
  .sidebar { display: none; }
  .doc-content { margin-left: 0; padding: 24px 16px; }
}
