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

:root {
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --bg: #f8fafc;
  --card-bg: #ffffff;
  --border: #e2e8f0;
  --text: #1e293b;
  --text-secondary: #64748b;
  --header-bg: #0f172a;
  --sidebar-bg: #1e293b;
  --sidebar-text: #cbd5e1;
  --sidebar-active: #3b82f6;
  --scope-public: #22c55e;
  --scope-internal: #3b82f6;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.1), 0 1px 2px rgba(0,0,0,0.06);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  line-height: 1.6;
}

/* ===== Header ===== */
.header {
  background: var(--header-bg);
  color: #fff;
  padding: 16px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.header h1 {
  font-size: 20px;
  font-weight: 600;
}

.header-actions {
  display: flex;
  gap: 8px;
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border: none;
  border-radius: var(--radius);
  font-size: 14px;
  cursor: pointer;
  transition: background 0.15s;
  text-decoration: none;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover {
  background: var(--primary-hover);
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}
.btn-danger:hover {
  background: var(--danger-hover);
}

.btn-outline {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}
.btn-outline:hover {
  background: #f1f5f9;
}

.btn-sm {
  padding: 4px 10px;
  font-size: 12px;
}

/* ===== Admin Page ===== */
.container {
  max-width: 960px;
  margin: 0 auto;
  padding: 24px;
}

.collection-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.collection-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.card-title {
  font-size: 18px;
  font-weight: 600;
}

.scope-badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
  color: #fff;
}

.scope-public {
  background: var(--scope-public);
}

.scope-internal {
  background: var(--scope-internal);
}

.card-meta {
  color: var(--text-secondary);
  font-size: 14px;
  margin-bottom: 12px;
}

.card-link {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: #f1f5f9;
  border-radius: var(--radius);
  font-size: 13px;
  word-break: break-all;
}

.card-link code {
  flex: 1;
  color: var(--text);
  font-family: 'SF Mono', Monaco, 'Cascadia Code', monospace;
}

.card-actions {
  display: flex;
  gap: 8px;
  margin-top: 12px;
  justify-content: flex-end;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-secondary);
}

.empty-state h2 {
  font-size: 20px;
  margin-bottom: 8px;
  color: var(--text);
}

.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  padding: 12px 20px;
  background: var(--header-bg);
  color: #fff;
  border-radius: var(--radius);
  font-size: 14px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  transform: translateY(80px);
  opacity: 0;
  transition: all 0.3s ease;
  z-index: 1000;
}

.toast.show {
  transform: translateY(0);
  opacity: 1;
}

/* ===== Viewer Page ===== */
.viewer-layout {
  display: flex;
  height: 100vh;
}

.viewer-single {
  height: 100vh;
  overflow-y: auto;
}

.viewer-single .content {
  max-width: 100%;
  margin: 0 auto;
  padding: 40px;
}

.sidebar {
  width: 260px;
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
}

.sidebar-header {
  padding: 16px 20px;
  border-bottom: 1px solid rgba(255,255,255,0.1);
}

.sidebar-header h2 {
  font-size: 16px;
  font-weight: 600;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
}

.sidebar-item {
  display: block;
  width: 100%;
  padding: 10px 20px;
  background: none;
  border: none;
  text-align: left;
  color: var(--sidebar-text);
  font-size: 14px;
  cursor: pointer;
  transition: background 0.15s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-item:hover {
  background: rgba(255,255,255,0.05);
}

.sidebar-item.active {
  background: var(--sidebar-active);
  color: #fff;
}

/* ===== Content Area ===== */
.content {
  flex: 1;
  overflow-y: auto;
  padding: 40px;
}

.content.single-mode {
  max-width: 860px;
  margin: 0 auto;
}

/* ===== Markdown Styles ===== */
.markdown-body {
  max-width: 900px;
  font-size: 16px;
  line-height: 1.7;
  word-wrap: break-word;
}

.markdown-body > *:first-child {
  margin-top: 0 !important;
}

.markdown-body > *:last-child {
  margin-bottom: 0 !important;
}

.markdown-body h1, .markdown-body h2, .markdown-body h3,
.markdown-body h4, .markdown-body h5, .markdown-body h6 {
  margin-top: 24px;
  margin-bottom: 16px;
  font-weight: 600;
  line-height: 1.25;
}

.markdown-body h1 { font-size: 2em; padding-bottom: 0.3em; border-bottom: 1px solid var(--border); }
.markdown-body h2 { font-size: 1.5em; padding-bottom: 0.3em; border-bottom: 1px solid var(--border); }
.markdown-body h3 { font-size: 1.25em; }
.markdown-body h4 { font-size: 1em; }

.markdown-body p {
  margin-bottom: 16px;
}

.markdown-body a {
  color: var(--primary);
  text-decoration: none;
}
.markdown-body a:hover {
  text-decoration: underline;
}

.markdown-body ul, .markdown-body ol {
  margin-bottom: 16px;
  padding-left: 2em;
}

.markdown-body li {
  margin-top: 4px;
}

.markdown-body li > p {
  margin-bottom: 8px;
}

/* Task list (checkbox) styles */
.markdown-body ul.contains-task-list,
.markdown-body ul:has(> li > input[type="checkbox"]) {
  list-style: none;
  padding-left: 1.5em;
}

.markdown-body li > input[type="checkbox"] {
  margin-right: 8px;
  margin-left: -1.5em;
  vertical-align: middle;
}

.markdown-body blockquote {
  margin: 0 0 16px;
  padding: 12px 16px;
  color: var(--text-secondary);
  border-left: 4px solid var(--primary);
  background: #f8fafc;
  border-radius: 0 var(--radius) var(--radius) 0;
}

.markdown-body blockquote > *:last-child {
  margin-bottom: 0;
}

.markdown-body code {
  padding: 2px 6px;
  background: #f1f5f9;
  border-radius: 4px;
  font-family: 'SF Mono', Monaco, 'Cascadia Code', monospace;
  font-size: 0.9em;
}

.markdown-body pre {
  margin-bottom: 16px;
  padding: 16px;
  background: #0f172a;
  border-radius: var(--radius);
  overflow-x: auto;
}

.markdown-body pre code {
  padding: 0;
  background: transparent;
  color: #e2e8f0;
  font-size: 14px;
  line-height: 1.6;
}

.markdown-body table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 16px;
  display: block;
  overflow-x: auto;
}

.markdown-body th, .markdown-body td {
  padding: 10px 14px;
  border: 1px solid var(--border);
  text-align: left;
}

.markdown-body th {
  background: #f1f5f9;
  font-weight: 600;
}

.markdown-body tr:nth-child(even) {
  background: #f8fafc;
}

.markdown-body img {
  max-width: 100%;
  height: auto;
  border-radius: var(--radius);
  margin: 8px 0;
}

.markdown-body hr {
  margin: 24px 0;
  border: none;
  border-top: 2px solid var(--border);
}

/* Mermaid chart styles */
.markdown-body .mermaid {
  margin: 20px 0;
  padding: 20px;
  background: #fafbfc;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-align: center;
  overflow-x: auto;
}

.markdown-body .mermaid-rendered {
  background: transparent;
  border: none;
  padding: 10px 0;
}

.markdown-body .mermaid svg {
  max-width: 100%;
  height: auto;
}

.markdown-body .mermaid-error {
  color: var(--danger);
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: var(--radius);
  padding: 12px;
  font-size: 13px;
  white-space: pre-wrap;
}

/* Plain text styles */
.markdown-body .plain-text {
  white-space: pre-wrap;
  word-break: break-all;
  font-family: 'SF Mono', Monaco, 'Cascadia Code', monospace;
  font-size: 14px;
  line-height: 1.6;
  background: transparent;
  padding: 0;
  margin: 0;
}

/* ===== Loading & Error ===== */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  color: var(--text-secondary);
  font-size: 16px;
}

.error-page {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  color: var(--text-secondary);
}

.error-page h1 {
  font-size: 48px;
  color: var(--text);
  margin-bottom: 8px;
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
  .viewer-layout {
    flex-direction: column;
  }
  .sidebar {
    width: 100%;
    height: auto;
    max-height: 200px;
  }
  .content {
    padding: 20px;
  }
  .container {
    padding: 16px;
  }
}
