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

:root {
  --primary:      #2563eb;
  --primary-dim:  #1d4ed8;
  --primary-bg:   #eff6ff;
  --text:         #1e293b;
  --text-muted:   #64748b;
  --border:       #e2e8f0;
  --bg:           #ffffff;
  --sidebar-bg:   #f8fafc;
  --code-bg:      #f1f5f9;
  --sidebar-w:    272px;
  --header-h:     52px;
}

html { scroll-behavior: smooth; }
html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui,
               'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
  color: var(--text);
  background: var(--bg);
  line-height: 1.65;
  overflow: hidden;
}

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

/* ── Header ──────────────────────────────────────────────────────────────── */
#header {
  position: fixed; top: 0; left: 0; right: 0;
  height: var(--header-h);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 1rem;
  gap: 0.75rem;
  z-index: 200;
  box-shadow: 0 1px 4px rgba(0,0,0,0.06);
}

#sidebar-toggle {
  background: none; border: none; cursor: pointer;
  padding: 0.375rem;
  border-radius: 6px;
  color: var(--text);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
#sidebar-toggle:hover { background: var(--border); }

#site-title {
  font-weight: 700; font-size: 1rem;
  color: var(--text); white-space: nowrap; flex-shrink: 0;
}

#breadcrumb {
  font-size: 0.78rem; color: var(--text-muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  flex: 1; min-width: 0;
}
#breadcrumb a { color: var(--text-muted); }
#breadcrumb a:hover { color: var(--primary); text-decoration: none; }

/* ── Layout ──────────────────────────────────────────────────────────────── */
#layout {
  display: flex;
  height: 100vh;
  padding-top: var(--header-h);
}

/* ── Backdrop (mobile overlay) ───────────────────────────────────────────── */
#sidebar-backdrop {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.35);
  z-index: 99;
}
#sidebar-backdrop.show { display: block; }

/* ── Sidebar ─────────────────────────────────────────────────────────────── */
#sidebar {
  position: fixed;
  top: var(--header-h); left: 0; bottom: 0;
  width: var(--sidebar-w);
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  transform: translateX(-100%);
  transition: transform 0.22s ease, box-shadow 0.22s;
  z-index: 100;
  -webkit-overflow-scrolling: touch;
}
#sidebar.open {
  transform: translateX(0);
  box-shadow: 2px 0 12px rgba(0,0,0,0.12);
}

.sidebar-header {
  padding: 0.875rem 1rem 0.75rem;
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 1;
  background: var(--sidebar-bg);
}
.sidebar-back {
  display: inline-block; font-size: 0.78rem;
  color: var(--text-muted); margin-bottom: 0.35rem;
}
.sidebar-back:hover { color: var(--primary); text-decoration: none; }
.sidebar-collection-name {
  font-size: 0.82rem; font-weight: 600;
  color: var(--text); line-height: 1.4;
}

.file-nav { list-style: none; padding: 0.375rem 0 1rem; }
.file-nav li { border-left: 3px solid transparent; }
.file-nav li a {
  display: block;
  padding: 0.45rem 0.875rem 0.45rem 0.75rem;
  font-size: 0.8rem;
  color: var(--text); line-height: 1.5;
}
.file-nav li a:hover { background: var(--primary-bg); color: var(--primary); text-decoration: none; }
.file-nav li.active { border-left-color: var(--primary); }
.file-nav li.active a { background: var(--primary-bg); color: var(--primary); font-weight: 500; }

/* ── Main content ────────────────────────────────────────────────────────── */
#content {
  flex: 1; min-width: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 2rem 1.25rem 3rem;
  transition: margin-left 0.22s ease;
}

/* Desktop: shift content when sidebar open */
@media (min-width: 768px) {
  #sidebar-backdrop { display: none !important; }
  #layout.sidebar-open #content { margin-left: var(--sidebar-w); }
}

#content-inner { max-width: 728px; margin: 0 auto; }

/* ── Home ─────────────────────────────────────────────────────────────────── */
.home h1 { font-size: 1.75rem; font-weight: 700; margin-bottom: 0.4rem; }
.home .subtitle { color: var(--text-muted); margin-bottom: 2rem; font-size: 0.95rem; }

.collection-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 1rem;
}
.collection-card {
  display: block; padding: 1.25rem;
  border: 1px solid var(--border); border-radius: 10px;
  color: var(--text);
  transition: border-color 0.15s, box-shadow 0.15s, transform 0.1s;
}
.collection-card:hover {
  border-color: var(--primary);
  box-shadow: 0 4px 14px rgba(37,99,235,0.1);
  transform: translateY(-1px);
  text-decoration: none; color: var(--text);
}
.card-name { font-weight: 600; font-size: 0.98rem; margin-bottom: 0.4rem; }
.card-desc { font-size: 0.82rem; color: var(--text-muted); margin-bottom: 0.75rem; min-height: 2.5em; }
.card-meta { font-size: 0.78rem; color: var(--primary); font-weight: 500; }

/* ── Collection intro ────────────────────────────────────────────────────── */
.collection-intro h1 { font-size: 1.5rem; margin-bottom: 0.5rem; }
.collection-intro .col-desc { color: var(--text-muted); margin-bottom: 0.5rem; }
.collection-intro .file-count { font-size: 0.85rem; color: var(--text-muted); margin-bottom: 1.5rem; }

.file-list a.file-row {
  display: flex; align-items: baseline; gap: 0.75rem;
  padding: 0.55rem 0;
  border-bottom: 1px solid var(--border);
  color: var(--text); font-size: 0.875rem; line-height: 1.5;
}
.file-list a.file-row:hover { color: var(--primary); text-decoration: none; }
.file-list a.file-row.pinned { color: var(--primary); font-weight: 500; }
.file-num { min-width: 1.6rem; font-size: 0.72rem; color: var(--text-muted); flex-shrink: 0; }

/* ── Article ─────────────────────────────────────────────────────────────── */
.article {
  font-size: 1rem; line-height: 1.85;
  word-break: break-word;
}

/* Headings */
.article h1 {
  font-size: 1.45rem; font-weight: 700;
  border-bottom: 2px solid var(--border);
  padding-bottom: 0.5rem;
  margin: 0 0 1.25rem;
}
.article h2 { font-size: 1.2rem; font-weight: 600; margin: 1.75rem 0 0.75rem; }
.article h3 { font-size: 1.05rem; font-weight: 600; margin: 1.4rem 0 0.5rem; }
.article h4 { font-size: 0.95rem; font-weight: 600; margin: 1rem 0 0.4rem; }
.article h2::before { content: ""; display: block; height: 1px; background: var(--border); margin-bottom: 0.75rem; }

/* Text */
.article p { margin-bottom: 0.9rem; }
.article strong { font-weight: 600; }
.article em { font-style: italic; }

/* Lists */
.article ul, .article ol { margin: 0 0 0.9rem 1.5rem; }
.article li { margin-bottom: 0.2rem; }
.article li > p { margin-bottom: 0.2rem; }
.article li ul, .article li ol { margin-top: 0.2rem; margin-bottom: 0; }

/* Blockquote — used for goal meta info */
.article blockquote {
  border-left: 4px solid var(--primary);
  background: var(--primary-bg);
  padding: 0.7rem 1rem;
  margin: 1rem 0;
  border-radius: 0 8px 8px 0;
  font-size: 0.88rem;
  color: #1e40af;
}
.article blockquote p { margin: 0; line-height: 1.7; }
.article blockquote strong { color: #1e3a8a; }

/* Tables */
.article .table-wrap { overflow-x: auto; margin: 1rem 0; }
.article table { border-collapse: collapse; font-size: 0.85rem; white-space: nowrap; }
.article th, .article td { border: 1px solid var(--border); padding: 0.45rem 0.75rem; text-align: left; }
.article th { background: var(--sidebar-bg); font-weight: 600; }
.article tr:nth-child(even) td { background: #fafafa; }

/* Code */
.article pre {
  background: var(--code-bg); border-radius: 8px;
  padding: 0.9rem 1rem; overflow-x: auto;
  font-size: 0.82rem; margin: 1rem 0;
  border: 1px solid var(--border);
}
.article code {
  font-family: 'Menlo', 'Consolas', 'Monaco', 'Courier New', monospace;
}
.article :not(pre) > code {
  background: var(--code-bg); padding: 0.1em 0.35em;
  border-radius: 4px; font-size: 0.875em;
  border: 1px solid var(--border);
}

/* HR */
.article hr { border: none; border-top: 1px solid var(--border); margin: 1.5rem 0; }

/* KaTeX display blocks */
.article .katex-display { overflow-x: auto; padding: 0.5rem 0; margin: 0.5rem 0; }

/* Task list checkboxes */
.article input[type="checkbox"] { margin-right: 0.35rem; }

/* ── Article prev/next nav ─────────────────────────────────────────────── */
.article-nav {
  display: flex; justify-content: space-between; align-items: flex-start;
  gap: 1rem;
  margin-top: 2.5rem; padding-top: 1.5rem;
  border-top: 1px solid var(--border);
}
.article-nav a {
  font-size: 0.82rem; color: var(--primary);
  line-height: 1.5; max-width: 46%;
  display: flex; align-items: flex-start; gap: 0.3rem;
}
.article-nav .nav-next { text-align: right; }
.article-nav a:hover { text-decoration: underline; }

/* ── Misc ─────────────────────────────────────────────────────────────────── */
.loading { text-align: center; padding: 4rem 1rem; color: var(--text-muted); font-size: 0.9rem; }
.error-msg { color: #dc2626; padding: 1rem; font-size: 0.9rem; }

/* ── Mobile tweaks ────────────────────────────────────────────────────────── */
@media (max-width: 480px) {
  #content { padding: 1.25rem 0.875rem 3rem; }
  .article { font-size: 0.95rem; }
  .article h1 { font-size: 1.25rem; }
  .article h2 { font-size: 1.1rem; }
  .article pre { font-size: 0.78rem; }
  .collection-grid { grid-template-columns: 1fr; }
}
