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

:root {
  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --bg: #fafafa;
  --surface: #fff;
  --text-primary: #111;
  --text-secondary: #555;
  --text-muted: #999;
  --border: #e5e5e5;
  --border-light: #f0f0f0;
  --hover: #f5f5f5;
  --accent: #111;
  --sidebar-w: 272px;
  --article-w: 680px;
  --topbar-h: 48px;
  --ease: cubic-bezier(.4, 0, .2, 1);
}

/* ===== Base ===== */
html {
  font-size: 15px;
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text-primary);
  line-height: 1.7;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ===== Reading Progress ===== */
.progress-track {
  position: fixed;
  top: 0;
  left: var(--sidebar-w);
  right: 0;
  height: 2px;
  z-index: 80;
}

.progress-bar {
  height: 100%;
  width: 0;
  background: var(--accent);
  opacity: 0.4;
  transition: width 80ms linear;
}

/* ===== Topbar (mobile) ===== */
.topbar {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--topbar-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 90;
  align-items: center;
  padding: 0 16px;
  gap: 12px;
}

.topbar-menu {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: none;
  background: none;
  cursor: pointer;
  border-radius: 6px;
  color: var(--text-primary);
}

.topbar-menu:hover { background: var(--hover); }

.topbar-title {
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

/* ===== Overlay ===== */
.overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.25);
  z-index: 99;
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
}

.overlay.show { display: block; }

/* ===== Sidebar ===== */
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  width: var(--sidebar-w);
  height: 100vh;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 100;
  transition: transform 0.28s var(--ease);
  overflow: hidden;
}

.sidebar-brand {
  padding: 32px 20px 20px;
}

.brand-title {
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-primary);
}

.brand-sub {
  font-size: 0.68rem;
  color: var(--text-muted);
  margin-top: 2px;
  letter-spacing: 0.02em;
}

/* Mode switch */
.mode-switch {
  display: flex;
  margin: 0 12px 8px;
  background: var(--bg);
  border-radius: 8px;
  padding: 3px;
}

.mode-btn {
  flex: 1;
  padding: 7px 0;
  font: 500 0.72rem/1 var(--font);
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  border-radius: 6px;
  transition: all 0.2s var(--ease);
}

.mode-btn.active {
  background: var(--surface);
  color: var(--text-primary);
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}

.mode-btn:not(.active):hover {
  color: var(--text-secondary);
}

/* Search */
.sidebar-search {
  position: relative;
  margin: 4px 12px 4px;
}

.search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
}

.sidebar-search input {
  width: 100%;
  padding: 7px 36px 7px 32px;
  font: 400 0.78rem/1.4 var(--font);
  border: 1px solid var(--border-light);
  border-radius: 7px;
  background: var(--bg);
  color: var(--text-primary);
  outline: none;
  transition: border-color 0.15s;
}

.sidebar-search input:focus {
  border-color: var(--text-muted);
}

.sidebar-search input::placeholder {
  color: var(--text-muted);
}

.search-shortcut {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  font: 500 0.6rem/1 var(--font);
  color: var(--text-muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 5px;
  pointer-events: none;
}

/* Nav */
.nav {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.nav::-webkit-scrollbar { width: 4px; }
.nav::-webkit-scrollbar-track { background: transparent; }
.nav::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.nav-item {
  display: flex;
  align-items: baseline;
  gap: 0;
  width: 100%;
  padding: 8px 20px;
  font: 400 0.8rem/1.4 var(--font);
  border: none;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  text-align: left;
  transition: all 0.12s;
}

.nav-item:hover {
  background: var(--hover);
  color: var(--text-primary);
}

.nav-item.active {
  color: var(--text-primary);
  font-weight: 500;
  background: var(--hover);
}

.nav-num {
  display: inline-block;
  width: 34px;
  flex-shrink: 0;
  font-size: 0.68rem;
  font-weight: 500;
  color: var(--text-muted);
  letter-spacing: 0.01em;
}

.nav-label {
  flex: 1;
}

/* Sidebar footer */
.sidebar-footer {
  padding: 14px 20px;
  border-top: 1px solid var(--border-light);
}

.sidebar-footer a {
  font-size: 0.7rem;
  color: var(--text-muted);
  text-decoration: none;
  transition: color 0.15s;
}

.sidebar-footer a:hover {
  color: var(--text-primary);
}

/* ===== Search Dropdown ===== */
.search-dropdown {
  display: none;
  position: fixed;
  left: 12px;
  top: 180px;
  width: calc(var(--sidebar-w) - 24px);
  max-height: 340px;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 30px rgba(0,0,0,0.1);
  z-index: 200;
}

.search-dropdown.show { display: block; }

.search-hit {
  padding: 10px 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--border-light);
  transition: background 0.1s;
}

.search-hit:last-child { border-bottom: none; }
.search-hit:hover { background: var(--hover); }

.search-hit-title {
  font-size: 0.78rem;
  font-weight: 500;
  color: var(--text-primary);
}

.search-hit-snippet {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-top: 2px;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.search-hit-snippet mark {
  background: #fef08a;
  color: inherit;
  border-radius: 2px;
  padding: 0 1px;
}

.search-empty {
  padding: 16px;
  text-align: center;
  font-size: 0.78rem;
  color: var(--text-muted);
}

/* ===== Main ===== */
.main {
  margin-left: var(--sidebar-w);
  min-height: 100vh;
}

.article {
  max-width: var(--article-w);
  margin: 0 auto;
  padding: 56px 28px 140px;
}

/* Loading */
.loading-state {
  display: flex;
  justify-content: center;
  padding: 120px 0;
}

.loading-dot {
  width: 6px;
  height: 6px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: pulse 1s ease infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 0.3; transform: scale(1); }
  50% { opacity: 1; transform: scale(1.3); }
}

/* Fade in */
.article.loaded {
  animation: fadeUp 0.3s var(--ease);
}

@keyframes fadeUp {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ===== Markdown Typography ===== */
.article h1 {
  font-size: 1.85rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1.2;
  margin-bottom: 12px;
}

.article h2 {
  font-size: 1.25rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.3;
  margin-top: 52px;
  margin-bottom: 16px;
}

.article h3 {
  font-size: 1.02rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  margin-top: 40px;
  margin-bottom: 12px;
}

.article h4 {
  font-size: 0.88rem;
  font-weight: 600;
  margin-top: 32px;
  margin-bottom: 8px;
}

.article p {
  margin-bottom: 18px;
  font-size: 0.94rem;
}

.article > p:first-child {
  font-size: 1.05rem;
  color: var(--text-secondary);
  line-height: 1.75;
}

.article hr {
  border: none;
  height: 1px;
  background: var(--border);
  margin: 48px 0;
}

.article strong { font-weight: 600; }
.article em { font-style: italic; }

.article a {
  color: var(--text-primary);
  text-decoration-line: underline;
  text-decoration-color: var(--border);
  text-underline-offset: 3px;
  text-decoration-thickness: 1px;
  transition: text-decoration-color 0.15s;
}

.article a:hover {
  text-decoration-color: var(--text-primary);
}

.article ul, .article ol {
  margin-bottom: 18px;
  padding-left: 22px;
}

.article li {
  font-size: 0.94rem;
  margin-bottom: 8px;
}

.article li p {
  margin-bottom: 6px;
}

.article blockquote {
  border-left: 2px solid var(--border);
  padding: 4px 0 4px 20px;
  margin: 24px 0;
}

.article blockquote p {
  color: var(--text-secondary);
  font-size: 0.92rem;
}

.article code {
  font-family: 'SF Mono', 'Fira Code', 'JetBrains Mono', monospace;
  font-size: 0.82em;
  background: var(--hover);
  padding: 2px 6px;
  border-radius: 4px;
}

.article pre {
  background: #f5f5f5;
  border-radius: 8px;
  padding: 18px 20px;
  overflow-x: auto;
  margin: 20px 0;
  font-size: 0.8rem;
  line-height: 1.65;
}

.article pre code {
  background: none;
  padding: 0;
  font-size: inherit;
}

/* Tables */
.article table {
  width: 100%;
  border-collapse: collapse;
  margin: 0;
  font-size: 0.84rem;
}

.article thead th {
  text-align: left;
  font-weight: 600;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
  padding: 8px 10px;
  border-bottom: 2px solid var(--border);
}

.article tbody td {
  padding: 9px 10px;
  border-bottom: 1px solid var(--border-light);
  vertical-align: top;
}

.article tbody tr:hover {
  background: var(--hover);
}

.article tbody tr:last-child td {
  border-bottom: none;
}

.article img {
  max-width: 100%;
  border-radius: 8px;
  margin: 8px 0;
}

/* ===== Figures with captions ===== */
.article figure {
  margin: 32px 0;
  padding: 0;
}

.article figure img {
  max-width: 100%;
  border-radius: 8px;
  display: block;
}

.article figure figcaption {
  font-size: 0.78rem;
  line-height: 1.5;
  color: var(--text-secondary);
  margin-top: 8px;
  padding: 0 4px;
}

.article figure figcaption .fig-source {
  color: var(--text-muted);
  font-size: 0.72rem;
}

.article figure figcaption .fig-source a {
  color: var(--text-muted);
  text-decoration-color: var(--border);
}

.article figure figcaption .fig-source a:hover {
  color: var(--text-secondary);
}

/* Charts: bleed slightly past text column */
.article figure.figure--chart {
  margin-left: -20px;
  margin-right: -20px;
}

.article figure.figure--chart img {
  width: 100%;
}

/* Photos: subtle border */
.article figure.figure--photo img {
  border: 1px solid var(--border-light);
}

/* ===== Table overflow fix ===== */
.table-wrap {
  width: 100%;
  overflow-x: auto;
  margin: 24px 0 32px;
  -webkit-overflow-scrolling: touch;
}

.table-wrap table {
  margin: 0;
  width: 100%;
  table-layout: auto;
}

/* ===== Evidence tags ===== */
.evidence-tag {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  font-size: 9px;
  font-weight: 700;
  line-height: 1;
  vertical-align: middle;
  margin: 0 1px 0 3px;
  cursor: default;
  position: relative;
}

.evidence-tag::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--text-primary);
  color: var(--surface);
  font-size: 0.68rem;
  font-weight: 400;
  padding: 4px 8px;
  border-radius: 4px;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s;
  z-index: 10;
}

.evidence-tag:hover::after {
  opacity: 1;
}

.tag-fact {
  background: #dcfce7;
  color: #166534;
}

.tag-extrapolation {
  background: #fef3c7;
  color: #92400e;
}

.tag-speculation {
  background: #fee2e2;
  color: #991b1b;
}

/* ===== Citation group (punctuation + citation + evidence tags — kept on one line) ===== */
.cite-group {
  white-space: nowrap;
  margin-right: 0.15em;
}

/* ===== Wikipedia-style citation refs ===== */
.cite-ref {
  font-size: 0.7em;
  line-height: 1;
  vertical-align: super;
  position: relative;
  top: -0.1em;
}

.cite-ref a {
  color: #3366cc !important;
  text-decoration: none !important;
  cursor: pointer;
}

.cite-ref a:visited {
  color: #795cb2 !important;
}

.cite-ref a:hover {
  text-decoration: underline !important;
}

/* Citation tooltip on hover */
.cite-ref .cite-tooltip {
  display: none;
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: 0 4px 16px rgba(0,0,0,0.12);
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 0.78rem;
  line-height: 1.5;
  color: var(--text-primary);
  white-space: normal;
  width: max-content;
  max-width: 360px;
  z-index: 50;
  vertical-align: baseline;
}

.cite-ref:hover .cite-tooltip {
  display: block;
}

/* ===== Wikipedia-style Sources section ===== */
.sources-section {
  font-size: 0.78rem;
  line-height: 1.6;
  color: var(--text-secondary);
}

.sources-section h2 {
  font-size: 1.05rem !important;
  margin-bottom: 12px !important;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
}

.sources-columns {
  display: flex;
  gap: 28px;
}

.sources-col {
  flex: 1;
  list-style: none;
  padding: 0 !important;
  margin: 0 !important;
  min-width: 0;
}

.sources-col li {
  font-size: 0.74rem !important;
  line-height: 1.5 !important;
  margin-bottom: 4px !important;
  padding-left: 0;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.sources-col li .src-num {
  font-weight: 600;
  color: var(--text-muted);
}

.sources-col li a {
  color: #3366cc !important;
  text-decoration: none !important;
}

.sources-col li a:visited {
  color: #795cb2 !important;
}

.sources-col li a:hover {
  text-decoration: underline !important;
}

@media (max-width: 480px) {
  .sources-columns {
    flex-direction: column;
    gap: 0;
  }
}

/* ===== Chapter Nav Footer ===== */
.chapter-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 72px;
  padding-top: 24px;
  border-top: 1px solid var(--border-light);
  gap: 16px;
}

.chapter-nav-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  font: 500 0.8rem/1 var(--font);
  color: var(--text-secondary);
  background: none;
  border: 1px solid var(--border);
  padding: 10px 18px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.15s var(--ease);
  max-width: 50%;
}

.chapter-nav-btn:hover {
  color: var(--text-primary);
  border-color: var(--text-secondary);
}

.chapter-nav-btn:disabled {
  opacity: 0.25;
  cursor: default;
  pointer-events: none;
}

.chapter-nav-btn span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chapter-nav-arrow {
  flex-shrink: 0;
  font-size: 1rem;
  line-height: 1;
}

/* ===== Responsive ===== */
@media (max-width: 860px) {
  .sidebar {
    transform: translateX(-100%);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .topbar {
    display: flex;
  }

  .main {
    margin-left: 0;
    padding-top: var(--topbar-h);
  }

  .article {
    padding: 28px 20px 100px;
  }

  .progress-track {
    left: 0;
    top: var(--topbar-h);
  }

  .search-dropdown {
    left: 12px;
    width: calc(var(--sidebar-w) - 24px);
  }

  .article figure.figure--chart {
    margin-left: -12px;
    margin-right: -12px;
  }
}

@media (max-width: 480px) {
  .article h1 { font-size: 1.45rem; }
  .article h2 { font-size: 1.1rem; }

  .chapter-nav {
    flex-direction: column;
  }

  .chapter-nav-btn {
    width: 100%;
    max-width: 100%;
    justify-content: center;
  }

  .article figure.figure--chart {
    margin-left: -20px;
    margin-right: -20px;
  }

  .article figure figcaption {
    font-size: 0.72rem;
    padding: 0;
  }
}
