:root {
  --bg-app: #f8fafc;
  --bg-card: #ffffff;
  --bg-sidebar: #f1f5f9;
  --bg-sidebar-hover: #e2e8f0;
  --bg-badge: #e0e7ff;
  --text-badge: #3730a3;
  --border-color: #e2e8f0;
  --border-subtle: #f1f5f9;
  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-muted: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-light: #eff6ff;
  --header-bg: rgba(255, 255, 255, 0.85);
  --header-border: #e2e8f0;
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.08), 0 2px 4px -2px rgba(0, 0, 0, 0.06);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.08), 0 4px 6px -4px rgba(0, 0, 0, 0.04);
  --code-bg: #f8fafc;
  --highlight-bg: #fef08a;
  --highlight-text: #854d0e;
  --meta-label: #4338ca;
  --meta-label-bg: #eef2ff;
  --doi-bg: #ecfdf5;
  --doi-text: #065f46;
  --doi-border: #a7f3d0;
}

[data-theme="dark"] {
  --bg-app: #0b0f19;
  --bg-card: #111827;
  --bg-sidebar: #0f172a;
  --bg-sidebar-hover: #1e293b;
  --bg-badge: #1e1b4b;
  --text-badge: #a5b4fc;
  --border-color: #1f2937;
  --border-subtle: #172033;
  --text-primary: #f8fafc;
  --text-secondary: #cbd5e1;
  --text-muted: #94a3b8;
  --primary: #3b82f6;
  --primary-hover: #60a5fa;
  --primary-light: #1e293b;
  --header-bg: rgba(11, 15, 25, 0.85);
  --header-border: #1f2937;
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.5);
  --code-bg: #0d1117;
  --highlight-bg: #713f12;
  --highlight-text: #fef08a;
  --meta-label: #93c5fd;
  --meta-label-bg: #1e293b;
  --doi-bg: #064e3b;
  --doi-text: #a7f3d0;
  --doi-border: #047857;
}

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

html {
  scroll-behavior: smooth;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background-color: var(--bg-app);
  color: var(--text-primary);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  transition: background-color 0.2s ease, color 0.2s ease;
}

/* Scroll Progress Bar */
#progress-bar {
  position: fixed;
  top: 0;
  left: 0;
  height: 3px;
  width: 0%;
  background: linear-gradient(90deg, #3b82f6, #06b6d4, #10b981);
  z-index: 1000;
  transition: width 0.1s ease-out;
}

/* Header */
.app-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--header-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--header-border);
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.5rem;
  box-shadow: var(--shadow-sm);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.btn-sidebar-toggle {
  background: none;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 0.5rem;
  color: var(--text-primary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.15s;
}

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

.app-title-group {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.app-logo {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  background: linear-gradient(135deg, #3b82f6, #6366f1);
  color: white;
  border-radius: 8px;
  font-weight: 700;
  font-size: 0.85rem;
}

.file-title {
  font-weight: 600;
  font-size: 1.05rem;
  color: var(--text-primary);
  white-space: nowrap;
}

.file-badge {
  font-size: 0.72rem;
  padding: 0.2rem 0.55rem;
  border-radius: 9999px;
  background: var(--bg-badge);
  color: var(--text-badge);
  font-weight: 600;
}

.header-center {
  flex: 1;
  max-width: 420px;
  margin: 0 1.5rem;
  position: relative;
}

.search-box {
  display: flex;
  align-items: center;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 0.4rem 0.85rem;
  gap: 0.5rem;
  box-shadow: var(--shadow-sm);
  transition: border-color 0.15s, box-shadow 0.15s;
}

.search-box:focus-within {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

.search-icon {
  color: var(--text-muted);
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

.search-input {
  border: none;
  background: transparent;
  outline: none;
  color: var(--text-primary);
  font-size: 0.88rem;
  width: 100%;
}

.search-count {
  font-size: 0.75rem;
  color: var(--text-muted);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.search-nav-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0.2rem;
  border-radius: 4px;
  display: flex;
  align-items: center;
}

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

.header-right {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.stat-pill {
  font-size: 0.75rem;
  color: var(--text-muted);
  padding: 0.35rem 0.7rem;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 20px;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #10b981;
  display: inline-block;
}

.action-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.82rem;
  font-weight: 500;
  padding: 0.45rem 0.8rem;
  border-radius: 8px;
  border: 1px solid var(--border-color);
  background: var(--bg-card);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s ease;
  text-decoration: none;
}

.action-btn:hover {
  background: var(--bg-sidebar-hover);
  color: var(--text-primary);
  border-color: var(--text-muted);
}

.action-btn.primary {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.action-btn.primary:hover {
  background: var(--primary-hover);
}

/* Layout */
.app-container {
  display: flex;
  flex: 1;
  position: relative;
}

/* Sidebar */
.app-sidebar {
  width: 320px;
  flex-shrink: 0;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border-color);
  position: sticky;
  top: 64px;
  height: calc(100vh - 64px);
  overflow-y: auto;
  padding: 1.25rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  transition: transform 0.25s ease, margin-left 0.25s ease;
}

.app-sidebar.collapsed {
  margin-left: -320px;
}

.sidebar-section-title {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 0.6rem;
  padding: 0 0.5rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.file-list {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.file-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
  font-size: 0.85rem;
  color: var(--text-secondary);
  text-decoration: none;
  cursor: pointer;
  transition: background-color 0.15s;
}

.file-item:hover {
  background-color: var(--bg-sidebar-hover);
  color: var(--text-primary);
}

.file-item.active {
  background-color: var(--primary-light);
  color: var(--primary);
  font-weight: 600;
}

.toc-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.toc-link {
  display: block;
  font-size: 0.83rem;
  line-height: 1.4;
  color: var(--text-secondary);
  text-decoration: none;
  padding: 0.35rem 0.6rem;
  border-radius: 6px;
  border-left: 2px solid transparent;
  transition: all 0.15s ease;
  overflow: hidden;
  text-overflow: ellipsis;
}

.toc-link:hover {
  color: var(--primary);
  background: var(--bg-sidebar-hover);
}

.toc-link.active {
  color: var(--primary);
  font-weight: 600;
  background: var(--primary-light);
  border-left-color: var(--primary);
}

.toc-h2 {
  font-weight: 600;
  color: var(--text-primary);
}

.toc-h3 {
  padding-left: 1.1rem;
  font-size: 0.8rem;
}

.toc-h4 {
  padding-left: 1.7rem;
  font-size: 0.76rem;
  color: var(--text-muted);
}

/* Main Content Area */
.app-content {
  flex: 1;
  min-width: 0;
  padding: 2.5rem 3rem 5rem;
  max-width: 960px;
  margin: 0 auto;
}

.content-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 16px;
  padding: 3rem 3.5rem;
  box-shadow: var(--shadow-sm);
}

/* Enhancements for rendered markdown */
.markdown-body {
  font-family: inherit !important;
  color: var(--text-primary) !important;
  background-color: transparent !important;
  font-size: 16px;
  line-height: 1.75;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4 {
  color: var(--text-primary);
  font-weight: 700;
  border-bottom: none;
  margin-top: 2rem;
  margin-bottom: 0.9rem;
  scroll-margin-top: 80px;
}

.markdown-body h3 {
  font-size: 1.35rem;
  padding-bottom: 0.4rem;
  border-bottom: 1px solid var(--border-color);
  color: var(--primary);
}

.markdown-body hr {
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--border-color), transparent);
  border: none;
  margin: 2.5rem 0;
}

.markdown-body ol, .markdown-body ul {
  padding-left: 1.75rem;
  margin-bottom: 1.25rem;
}

.markdown-body ol > li {
  margin-top: 1.25rem;
  margin-bottom: 1.25rem;
  padding: 1.15rem 1.4rem;
  background: var(--bg-app);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  line-height: 1.75;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.markdown-body ol > li:hover {
  border-color: var(--primary);
  box-shadow: var(--shadow-sm);
}

.markdown-body ol > li > strong:first-child {
  display: block;
  font-size: 1.02rem;
  color: var(--primary);
  margin-bottom: 0.5rem;
  line-height: 1.5;
}

/* Academic Citation Badges */
.academic-meta-tag {
  display: inline-block;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.15rem 0.5rem;
  border-radius: 4px;
  background: var(--meta-label-bg);
  color: var(--meta-label);
  margin-right: 0.35rem;
  margin-top: 0.25rem;
}

.doi-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.78rem;
  font-weight: 600;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  padding: 0.2rem 0.6rem;
  border-radius: 6px;
  background: var(--doi-bg);
  color: var(--doi-text) !important;
  border: 1px solid var(--doi-border);
  text-decoration: none !important;
  margin: 0.2rem 0;
  transition: filter 0.15s ease;
}

.doi-badge:hover {
  filter: brightness(0.95);
  text-decoration: none;
}

.keyword-chip {
  display: inline-block;
  background: var(--bg-sidebar-hover);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  padding: 0.25rem 0.65rem;
  border-radius: 9999px;
  font-size: 0.82rem;
  margin: 0.25rem 0.35rem 0.25rem 0;
  cursor: pointer;
  transition: all 0.15s;
}

.keyword-chip:hover {
  background: var(--primary-light);
  border-color: var(--primary);
  color: var(--primary);
}

/* Search Highlights */
mark.search-highlight {
  background-color: var(--highlight-bg);
  color: var(--highlight-text);
  padding: 0.05rem 0.2rem;
  border-radius: 3px;
  box-shadow: 0 0 0 1px rgba(234, 179, 8, 0.4);
}

mark.search-highlight.current {
  background-color: #f97316;
  color: white;
  box-shadow: 0 0 0 2px #ea580c;
}

/* Floating Scroll Top Button */
.btn-scroll-top {
  position: fixed;
  bottom: 2rem;
  right: 2rem;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--primary);
  color: white;
  border: none;
  box-shadow: var(--shadow-lg);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  visibility: hidden;
  transform: translateY(10px);
  transition: all 0.2s ease;
  z-index: 90;
}

.btn-scroll-top.visible {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

.btn-scroll-top:hover {
  background: var(--primary-hover);
  transform: translateY(-2px);
}

/* Toast */
.toast {
  position: fixed;
  bottom: 2rem;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: #0f172a;
  color: white;
  padding: 0.6rem 1.25rem;
  border-radius: 9999px;
  font-size: 0.85rem;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  visibility: hidden;
  transition: all 0.25s cubic-bezier(0.16, 1, 0.3, 1);
  z-index: 1000;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

[data-theme="dark"] .toast {
  background: #f8fafc;
  color: #0f172a;
}

.toast.show {
  opacity: 1;
  visibility: visible;
  transform: translateX(-50%) translateY(0);
}

/* Print Styles */
@media print {
  #progress-bar,
  .app-header,
  .app-sidebar,
  .btn-scroll-top,
  .toast {
    display: none !important;
  }

  body {
    background: white !important;
    color: black !important;
  }

  .app-content {
    padding: 0 !important;
    max-width: 100% !important;
  }

  .content-card {
    border: none !important;
    box-shadow: none !important;
    padding: 0 !important;
  }
}

/* Mobile Responsiveness */
@media (max-width: 900px) {
  .app-header {
    padding: 0 1rem;
  }

  .header-center {
    display: none;
  }

  .app-sidebar {
    position: fixed;
    z-index: 95;
    box-shadow: var(--shadow-lg);
  }

  .app-content {
    padding: 1.5rem 1rem;
  }

  .content-card {
    padding: 1.5rem 1.25rem;
  }

  .stat-pill {
    display: none;
  }
}
