#papers-view {
  display: none;
  height: calc(100vh - 56px);
}

#papers-view.active {
  display: flex;
}

.papers-sidebar {
  width: 300px;
  min-width: 300px;
  border-right: 1px solid var(--border-primary);
  background: var(--bg-secondary);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: background-color var(--transition-normal);
}

.papers-sidebar-header {
  padding: 16px;
  border-bottom: 1px solid var(--border-primary);
}

.papers-sidebar-header h2 {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 4px;
}

.papers-sidebar-header p {
  font-size: 12px;
  color: var(--text-tertiary);
}

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

.paper-list-item {
  padding: 14px 12px;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--transition-fast);
  margin-bottom: 4px;
}

.paper-list-item:hover {
  background: var(--bg-hover);
}

.paper-list-item.active {
  background: var(--accent-light);
}

.paper-list-item h3 {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 2px;
  line-height: 1.3;
}

.paper-list-item .paper-list-meta {
  font-size: 11px;
  color: var(--text-tertiary);
}

.paper-list-item.active h3 {
  color: var(--accent-primary);
}

.papers-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

.papers-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  border-bottom: 1px solid var(--border-primary);
  background: var(--bg-secondary);
  flex-shrink: 0;
  gap: 16px;
  transition: background-color var(--transition-normal);
}

.papers-toolbar-left {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.papers-toolbar-center {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1 1 0;
  min-width: 0;
  overflow: hidden;
}

.toolbar-paper-info {
  display: flex;
  align-items: baseline;
  gap: 10px;
  overflow: hidden;
  white-space: nowrap;
}

.toolbar-paper-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
}

.toolbar-paper-meta {
  font-size: 11px;
  color: var(--text-tertiary);
  flex-shrink: 0;
}

.papers-toolbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.view-toggle {
  display: flex;
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-md);
  overflow: hidden;
}

.view-toggle-btn {
  padding: 5px 12px;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
  background: var(--bg-primary);
  border: none;
  border-right: 1px solid var(--border-primary);
  transition: all var(--transition-fast);
  cursor: pointer;
}

.view-toggle-btn:last-child {
  border-right: none;
}

.view-toggle-btn:hover {
  background: var(--bg-hover);
}

.view-toggle-btn.active {
  background: var(--accent-primary);
  color: white;
}

.download-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: var(--radius-md);
  font-size: 12px;
  font-weight: 500;
  background: var(--bg-primary);
  color: var(--text-secondary);
  border: 1px solid var(--border-primary);
  transition: all var(--transition-fast);
}

.download-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.download-btn svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  fill: currentColor;
}

.papers-content {
  flex: 1;
  overflow: auto;
  scroll-behavior: smooth;
}

.ref-link {
  color: var(--accent-primary);
  text-decoration: none;
  font-size: 0.85em;
  font-weight: 600;
  vertical-align: super;
  line-height: 0;
}

.ref-link:hover {
  text-decoration: underline;
}

[id^="ref-"] {
  scroll-margin-top: 16px;
}

[id^="ref-"] .ref-link {
  vertical-align: baseline;
  font-size: 1em;
}

.papers-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-tertiary);
  font-size: 15px;
  flex-direction: column;
  gap: 12px;
}

.papers-empty svg {
  width: 48px;
  height: 48px;
  fill: var(--text-tertiary);
  opacity: 0.5;
}

.paper-pdf-view {
  width: 100%;
  height: 100%;
  border: none;
}

.paper-pdf-fallback {
  padding: 40px;
  text-align: center;
  color: var(--text-secondary);
}

.paper-pdf-fallback a {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 16px;
  padding: 10px 20px;
  background: var(--accent-primary);
  color: white;
  border-radius: var(--radius-md);
  font-weight: 500;
  text-decoration: none;
}

.markdown-formatted {
  max-width: 860px;
  margin: 0 auto;
  padding: 40px 48px;
  line-height: 1.7;
}

.markdown-formatted h1 {
  font-size: 32px;
  font-weight: 700;
  margin-bottom: 8px;
  padding-bottom: 12px;
  border-bottom: 2px solid var(--border-primary);
}

.markdown-formatted h2 {
  font-size: 24px;
  font-weight: 600;
  margin-top: 40px;
  margin-bottom: 16px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border-secondary);
}

.markdown-formatted h3 {
  font-size: 20px;
  font-weight: 600;
  margin-top: 32px;
  margin-bottom: 12px;
}

.markdown-formatted h4 {
  font-size: 16px;
  font-weight: 600;
  margin-top: 24px;
  margin-bottom: 8px;
}

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

.markdown-formatted ul, .markdown-formatted ol {
  margin-bottom: 16px;
  padding-left: 28px;
}

.markdown-formatted li {
  margin-bottom: 6px;
}

.markdown-formatted pre {
  background: var(--bg-code);
  border: 1px solid var(--border-secondary);
  border-radius: var(--radius-md);
  padding: 16px 20px;
  overflow-x: auto;
  margin-bottom: 16px;
  font-size: 13px;
  line-height: 1.5;
}

.markdown-formatted code {
  background: var(--bg-code);
  padding: 2px 6px;
  border-radius: var(--radius-sm);
  font-size: 0.9em;
}

.markdown-formatted pre code {
  background: none;
  padding: 0;
}

.markdown-formatted blockquote {
  border-left: 4px solid var(--accent-primary);
  margin: 0 0 16px 0;
  padding: 12px 20px;
  background: var(--bg-secondary);
  border-radius: 0 var(--radius-md) var(--radius-md) 0;
  color: var(--text-secondary);
}

.markdown-formatted table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 16px;
}

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

.markdown-formatted th {
  background: var(--bg-secondary);
  font-weight: 600;
}

.markdown-formatted hr {
  border: none;
  border-top: 1px solid var(--border-primary);
  margin: 32px 0;
}

.markdown-formatted strong {
  font-weight: 600;
}

.markdown-formatted img {
  max-width: 100%;
  border-radius: var(--radius-md);
}

.markdown-raw {
  padding: 24px 32px;
}

.markdown-raw pre {
  background: var(--bg-code);
  padding: 24px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-secondary);
  font-size: 13px;
  line-height: 1.6;
  overflow-x: auto;
  white-space: pre-wrap;
  word-wrap: break-word;
}

@media (max-width: 768px) {
  #papers-view.active { flex-direction: column; height: auto; min-height: calc(100vh - 56px); }
  .papers-sidebar { width: 100%; min-width: 100%; max-height: 35vh; border-right: none; border-bottom: 1px solid var(--border-primary); }
  .papers-toolbar {
    position: sticky;
    top: 56px;
    z-index: 10;
    padding: 8px 12px;
    flex-wrap: wrap;
  }
  .papers-toolbar-center {
    order: 3;
    width: 100%;
    justify-content: flex-start;
  }
  .markdown-formatted { padding: 24px 20px; }
  .markdown-raw { padding: 16px; }
}

/* ─────────────────────────────────────────────────────────────────────────
 * GitHub-like README rendering (shared by code-browser + paper-viewer)
 * ──────────────────────────────────────────────────────────────────────── */

.markdown-formatted h5 {
  font-size: 14px;
  font-weight: 600;
  margin-top: 20px;
  margin-bottom: 6px;
}

.markdown-formatted h6 {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-top: 16px;
  margin-bottom: 6px;
}

.markdown-formatted h1,
.markdown-formatted h2,
.markdown-formatted h3,
.markdown-formatted h4,
.markdown-formatted h5,
.markdown-formatted h6 {
  scroll-margin-top: 16px;
}

.markdown-formatted .heading-anchor {
  display: inline-block;
  margin-right: 8px;
  color: var(--text-tertiary);
  opacity: 0;
  transition: opacity var(--transition-fast);
  vertical-align: middle;
  text-decoration: none;
}

.markdown-formatted h1:hover .heading-anchor,
.markdown-formatted h2:hover .heading-anchor,
.markdown-formatted h3:hover .heading-anchor,
.markdown-formatted h4:hover .heading-anchor,
.markdown-formatted h5:hover .heading-anchor,
.markdown-formatted h6:hover .heading-anchor,
.markdown-formatted .heading-anchor:focus {
  opacity: 0.7;
}

.markdown-formatted .heading-anchor:hover {
  opacity: 1 !important;
  color: var(--accent-primary);
}

.markdown-formatted .heading-anchor svg {
  display: block;
  fill: currentColor;
}

/* Centered hero block: anything wrapped in <div align="center"> (common
 * GitHub-flavored pattern for logos / badges). */
.markdown-formatted div[align="center"] {
  text-align: center;
}

.markdown-formatted div[align="center"] img,
.markdown-formatted p img[alt*="badge" i] {
  display: inline-block;
  margin: 2px 3px;
}

/* Copy button on every rendered code block (not mermaid). */
.markdown-formatted pre {
  position: relative;
}

.markdown-formatted pre .md-copy-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  padding: 0;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-primary);
  background: var(--bg-primary);
  color: var(--text-tertiary);
  cursor: pointer;
  opacity: 0;
  transition: opacity var(--transition-fast), background var(--transition-fast), color var(--transition-fast);
}

.markdown-formatted pre:hover .md-copy-btn,
.markdown-formatted pre .md-copy-btn:focus {
  opacity: 1;
}

.markdown-formatted pre .md-copy-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.markdown-formatted pre .md-copy-btn.copied {
  opacity: 1;
  color: var(--accent-primary);
  border-color: var(--accent-primary);
}

.markdown-formatted pre .md-copy-btn svg {
  width: 14px;
  height: 14px;
  fill: currentColor;
}

/* Table zebra striping + subtler borders. */
.markdown-formatted table {
  font-size: 13px;
  display: block;
  overflow-x: auto;
}

.markdown-formatted tbody tr:nth-child(2n) {
  background: var(--bg-secondary);
}

/* GFM task list items. */
.markdown-formatted ul li input[type="checkbox"] {
  margin-right: 6px;
  vertical-align: middle;
}

.markdown-formatted ul:has(> li > input[type="checkbox"]),
.markdown-formatted ul li:has(> input[type="checkbox"]) {
  list-style: none;
}

.markdown-formatted ul li:has(> input[type="checkbox"]) {
  margin-left: -20px;
}

/* Mermaid block container. */
.markdown-formatted .mermaid {
  display: flex;
  justify-content: center;
  padding: 20px;
  margin: 16px 0;
  background: var(--bg-secondary);
  border: 1px solid var(--border-secondary);
  border-radius: var(--radius-md);
  overflow-x: auto;
  text-align: center;
}

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

.markdown-formatted .mermaid-error {
  background: var(--bg-code);
  color: var(--text-secondary);
  font-family: var(--font-mono, monospace);
  font-size: 12px;
  white-space: pre-wrap;
  text-align: left;
  justify-content: flex-start;
}

/* README card used for both directory-level READMEs and .md file view. */
.readme-card {
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-md);
  background: var(--bg-primary);
  margin: 16px 0;
  overflow: hidden;
}

.readme-card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border-primary);
  background: var(--bg-secondary);
  font-size: 13px;
  color: var(--text-secondary);
}

.readme-card-name {
  font-weight: 600;
  color: var(--text-primary);
}

.readme-card-body {
  padding: 32px 40px;
}

/* Raw source view takes over the full card body without extra padding
 * so the line-numbered gutter sits flush against the card edge. */
.readme-card-body.markdown-raw {
  padding: 0;
}

.dir-readme {
  margin-top: 16px;
  padding: 0 16px 24px;
}

/* Markdown view inside code-browser (.md file route).
 * The card is full-width inside `.markdown-view-body` and the
 * `.markdown-formatted` class on `.readme-card-body` centers the
 * content at max-width: 860px — matching the directory-listing README. */
.markdown-view {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.markdown-view .markdown-toolbar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border-primary);
  background: var(--bg-secondary);
  flex-shrink: 0;
}

.markdown-view-body {
  flex: 1;
  overflow: auto;
  padding: 16px;
}

.markdown-view-body > .readme-card {
  margin: 0;
}

.view-toggle--compact .view-toggle-btn {
  padding: 4px 10px;
  font-size: 11px;
}

@media (max-width: 768px) {
  .readme-card-body {
    padding: 20px 18px;
  }
  .markdown-view-body {
    padding: 8px;
  }
}
