/* ═══════════════════════════════════════════════════════
   MDREAD — A refined Markdown reader
   ═══════════════════════════════════════════════════════ */

:root {
  /* Light theme — warm paper */
  --bg:         #F5F1E8;
  --bg-paper:   #FBF8F1;
  --bg-raised:  #EEE8D9;
  --ink:        #1A1714;
  --ink-2:      #453F37;
  --ink-3:      #8A8278;
  --accent:     #8B3A1F;   /* terracotta */
  --accent-2:   rgba(139, 58, 31, 0.08);
  --accent-3:   rgba(139, 58, 31, 0.25);
  --line:       rgba(26, 23, 20, 0.12);
  --line-soft:  rgba(26, 23, 20, 0.06);
  --code-bg:    #F0EAD9;
  --code-ink:   #3A332B;
  --quote-bg:   rgba(139, 58, 31, 0.04);
  --shadow:     0 1px 2px rgba(26, 23, 20, 0.04), 0 8px 32px rgba(26, 23, 20, 0.06);
  --shadow-lg:  0 2px 4px rgba(26, 23, 20, 0.06), 0 24px 64px rgba(26, 23, 20, 0.12);
  --grain-opacity: 0.035;

  --font-display: 'Fraunces', 'Times New Roman', serif;
  --font-serif:   'Source Serif 4', Georgia, serif;
  --font-ui:      'Inter Tight', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono:    'JetBrains Mono', ui-monospace, 'SF Mono', monospace;

  --reading-width: 38rem;
}

[data-theme="dark"] {
  --bg:         #15130F;
  --bg-paper:   #1C1915;
  --bg-raised:  #24201B;
  --ink:        #F3EEE2;
  --ink-2:      #BCB4A4;
  --ink-3:      #7A7267;
  --accent:     #E8A87C;   /* warm amber */
  --accent-2:   rgba(232, 168, 124, 0.10);
  --accent-3:   rgba(232, 168, 124, 0.28);
  --line:       rgba(243, 238, 226, 0.10);
  --line-soft:  rgba(243, 238, 226, 0.05);
  --code-bg:    #0E0C0A;
  --code-ink:   #E8DFCC;
  --quote-bg:   rgba(232, 168, 124, 0.06);
  --shadow:     0 1px 2px rgba(0,0,0,0.2), 0 8px 32px rgba(0,0,0,0.3);
  --shadow-lg:  0 2px 4px rgba(0,0,0,0.3), 0 24px 64px rgba(0,0,0,0.5);
  --grain-opacity: 0.06;
}

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

html { scroll-behavior: smooth; }

body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-serif);
  font-size: 18px;
  line-height: 1.6;
  min-height: 100vh;
  overflow-x: hidden;
  transition: background-color 0.4s ease, color 0.4s ease;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

/* Grain texture overlay */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 1;
  opacity: var(--grain-opacity);
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='3' seed='2'/%3E%3CfeColorMatrix values='0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}

/* ─── Progress bar ─── */
#progress {
  position: fixed;
  top: 0; left: 0;
  height: 2px;
  width: 0;
  background: var(--accent);
  z-index: 100;
  transition: width 0.15s ease-out;
  box-shadow: 0 0 8px var(--accent-3);
}

/* ─── Top bar ─── */
#topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 64px;
  padding: 0 32px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  z-index: 50;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(16px) saturate(1.2);
  -webkit-backdrop-filter: blur(16px) saturate(1.2);
  border-bottom: 1px solid var(--line-soft);
  transition: all 0.4s ease;
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 10px;
  font-family: var(--font-display);
  font-variation-settings: 'opsz' 144, 'wght' 400, 'SOFT' 30;
  font-size: 22px;
  font-style: italic;
  letter-spacing: -0.01em;
  color: var(--ink);
}

.brand .dot {
  display: inline-block;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--accent);
  transform: translateY(-4px);
}

.brand small {
  font-family: var(--font-ui);
  font-size: 10px;
  font-style: normal;
  font-weight: 500;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-3);
  margin-left: 6px;
}

.filename {
  font-family: var(--font-ui);
  font-size: 13px;
  font-weight: 500;
  color: var(--ink-2);
  letter-spacing: -0.005em;
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: 40vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.filename::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  flex-shrink: 0;
  animation: pulse 2.4s ease-in-out infinite;
}

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

.controls {
  display: flex;
  align-items: center;
  gap: 4px;
}

.btn {
  background: transparent;
  border: 1px solid transparent;
  color: var(--ink-2);
  font-family: var(--font-ui);
  font-size: 13px;
  font-weight: 500;
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  transition: all 0.2s ease;
  letter-spacing: -0.005em;
}

.btn:hover {
  background: var(--accent-2);
  color: var(--ink);
  border-color: var(--line);
}

.btn svg {
  width: 15px;
  height: 15px;
  stroke-width: 1.8;
}

.btn-icon {
  width: 36px;
  height: 36px;
  padding: 0;
  justify-content: center;
}

.btn-primary {
  background: var(--ink);
  color: var(--bg-paper);
}
.btn-primary:hover {
  background: var(--accent);
  color: var(--bg-paper);
  border-color: transparent;
}

/* ─── Upload view ─── */
#upload-view {
  min-height: 100vh;
  padding: 128px 32px 64px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  z-index: 2;
}

.upload-hero {
  max-width: 600px;
  text-align: center;
  margin-bottom: 48px;
  animation: fadeInUp 0.8s cubic-bezier(0.22, 1, 0.36, 1);
}

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

.eyebrow {
  font-family: var(--font-ui);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 20px;
  display: inline-flex;
  align-items: center;
  gap: 12px;
}

.eyebrow::before, .eyebrow::after {
  content: '';
  display: block;
  width: 24px;
  height: 1px;
  background: var(--accent);
  opacity: 0.5;
}

h1.display {
  font-family: var(--font-display);
  font-variation-settings: 'opsz' 144, 'wght' 350, 'SOFT' 50;
  font-size: clamp(48px, 8vw, 88px);
  line-height: 0.95;
  letter-spacing: -0.03em;
  color: var(--ink);
  margin-bottom: 18px;
}

h1.display em {
  font-style: italic;
  color: var(--accent);
  font-variation-settings: 'opsz' 144, 'wght' 300, 'SOFT' 100;
}

.tagline {
  font-family: var(--font-serif);
  font-style: italic;
  font-size: 19px;
  color: var(--ink-2);
  max-width: 420px;
  margin: 0 auto;
  line-height: 1.5;
}

/* Drop zone */
.dropzone {
  width: 100%;
  max-width: 560px;
  background: var(--bg-paper);
  border: 1.5px dashed var(--line);
  border-radius: 20px;
  padding: 48px 40px;
  text-align: center;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: all 0.3s cubic-bezier(0.22, 1, 0.36, 1);
  box-shadow: var(--shadow);
  animation: fadeInUp 0.8s 0.15s cubic-bezier(0.22, 1, 0.36, 1) both;
}

.dropzone::before {
  content: '';
  position: absolute;
  inset: 0;
  background: radial-gradient(circle at 50% 0%, var(--accent-2) 0%, transparent 60%);
  opacity: 0;
  transition: opacity 0.3s ease;
  pointer-events: none;
}

.dropzone:hover, .dropzone.dragging {
  border-color: var(--accent);
  border-style: solid;
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
}

.dropzone:hover::before, .dropzone.dragging::before {
  opacity: 1;
}

.dropzone.dragging {
  transform: scale(1.01);
}

.paper-illustration {
  width: 72px;
  height: 88px;
  margin: 0 auto 24px;
  position: relative;
  transition: transform 0.4s cubic-bezier(0.22, 1, 0.36, 1);
}

.dropzone:hover .paper-illustration {
  transform: translateY(-4px) rotate(-2deg);
}

.paper-illustration svg {
  width: 100%;
  height: 100%;
  display: block;
  filter: drop-shadow(0 8px 16px rgba(26, 23, 20, 0.08));
}

.dropzone-text {
  font-family: var(--font-display);
  font-variation-settings: 'opsz' 72, 'wght' 400;
  font-size: 22px;
  color: var(--ink);
  margin-bottom: 6px;
  letter-spacing: -0.015em;
}

.dropzone-text em {
  font-style: italic;
  color: var(--accent);
}

.dropzone-hint {
  font-family: var(--font-ui);
  font-size: 13px;
  color: var(--ink-3);
  letter-spacing: -0.005em;
}

.sample-link {
  margin-top: 28px;
  font-family: var(--font-ui);
  font-size: 13px;
  color: var(--ink-3);
  animation: fadeInUp 0.8s 0.3s cubic-bezier(0.22, 1, 0.36, 1) both;
}

.sample-link button {
  background: none;
  border: none;
  color: var(--accent);
  font: inherit;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
  text-decoration-thickness: 1px;
  text-decoration-color: var(--accent-3);
  transition: text-decoration-color 0.2s;
}

.sample-link button:hover {
  text-decoration-color: var(--accent);
}

/* Decorative marks */
.deco-mark {
  position: absolute;
  color: var(--accent);
  opacity: 0.14;
  font-family: var(--font-display);
  font-style: italic;
  pointer-events: none;
  user-select: none;
}

.deco-mark.top-left {
  top: 15%;
  left: 8%;
  font-size: 240px;
  transform: rotate(-12deg);
}

.deco-mark.bottom-right {
  bottom: 10%;
  right: 10%;
  font-size: 180px;
  transform: rotate(8deg);
}

/* ─── Reader view ─── */
#reader-view {
  display: none;
  padding-top: 64px;
  min-height: 100vh;
  position: relative;
  z-index: 2;
}

#reader-view.active {
  display: block;
  animation: fadeIn 0.5s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.reader-layout {
  display: grid;
  grid-template-columns: 260px 1fr;
  max-width: 1200px;
  margin: 0 auto;
  gap: 48px;
  padding: 56px 48px 120px;
}

/* TOC */
#toc {
  position: sticky;
  top: 96px;
  align-self: start;
  max-height: calc(100vh - 128px);
  overflow-y: auto;
  padding-right: 16px;
  border-right: 1px solid var(--line-soft);
}

#toc::-webkit-scrollbar { width: 3px; }
#toc::-webkit-scrollbar-track { background: transparent; }
#toc::-webkit-scrollbar-thumb { background: var(--line); border-radius: 2px; }

.toc-title {
  font-family: var(--font-ui);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--ink-3);
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line-soft);
}

.toc-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.toc-list a {
  display: block;
  padding: 6px 0 6px 12px;
  border-left: 2px solid transparent;
  font-family: var(--font-ui);
  font-size: 13px;
  font-weight: 500;
  color: var(--ink-3);
  text-decoration: none;
  line-height: 1.45;
  letter-spacing: -0.005em;
  transition: all 0.2s ease;
}

.toc-list a:hover {
  color: var(--ink);
  border-left-color: var(--line);
}

.toc-list a.active {
  color: var(--accent);
  border-left-color: var(--accent);
  font-weight: 600;
}

.toc-list li.level-3 a { padding-left: 24px; font-size: 12px; }
.toc-list li.level-4 a { padding-left: 36px; font-size: 12px; opacity: 0.8; }

/* ─── Article ─── */
article {
  max-width: var(--reading-width);
  margin: 0 auto;
  font-family: var(--font-serif);
  font-size: 18.5px;
  line-height: 1.72;
  color: var(--ink-2);
  font-variation-settings: 'opsz' 24;
  position: relative;
}

article > *:first-child { margin-top: 0; }
article > *:last-child { margin-bottom: 0; }

article h1, article h2, article h3, article h4, article h5, article h6 {
  font-family: var(--font-display);
  color: var(--ink);
  letter-spacing: -0.02em;
  line-height: 1.15;
  scroll-margin-top: 88px;
  font-weight: 400;
}

article h1 {
  font-variation-settings: 'opsz' 144, 'wght' 350, 'SOFT' 40;
  font-size: clamp(40px, 6vw, 62px);
  line-height: 1;
  margin-bottom: 32px;
  letter-spacing: -0.03em;
  position: relative;
}

article h1::after {
  content: '';
  display: block;
  width: 48px;
  height: 2px;
  background: var(--accent);
  margin-top: 28px;
}

article h2 {
  font-variation-settings: 'opsz' 72, 'wght' 500;
  font-size: 34px;
  margin: 64px 0 20px;
  letter-spacing: -0.022em;
}

article h3 {
  font-variation-settings: 'opsz' 48, 'wght' 500;
  font-size: 24px;
  margin: 48px 0 16px;
  letter-spacing: -0.015em;
}

article h4 {
  font-family: var(--font-ui);
  font-size: 15px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink-3);
  margin: 36px 0 12px;
}

article p {
  margin: 0 0 1.3em;
  hyphens: auto;
  -webkit-hyphens: auto;
}

article p:first-of-type {
  font-size: 1.12em;
  color: var(--ink);
}

/* Drop cap on very first paragraph after h1 (skip front matter subtitle) */
article h1 + p:not(.fm-subtitle)::first-letter,
article .fm-cover + p::first-letter {
  font-family: var(--font-display);
  font-variation-settings: 'opsz' 144, 'wght' 500;
  font-size: 5.4em;
  float: left;
  line-height: 0.85;
  padding: 0.08em 0.1em 0 0;
  margin-right: 0.02em;
  color: var(--accent);
}

article a {
  color: var(--ink);
  text-decoration: underline;
  text-underline-offset: 3px;
  text-decoration-thickness: 1px;
  text-decoration-color: var(--accent-3);
  transition: all 0.2s;
}

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

article strong {
  font-weight: 700;
  color: var(--ink);
}

article em {
  font-style: italic;
}

article blockquote {
  margin: 2em 0;
  padding: 4px 0 4px 28px;
  border-left: 3px solid var(--accent);
  background: linear-gradient(90deg, var(--quote-bg), transparent 60%);
  font-family: var(--font-display);
  font-style: italic;
  font-variation-settings: 'opsz' 72, 'wght' 350;
  font-size: 1.22em;
  line-height: 1.45;
  color: var(--ink);
  letter-spacing: -0.01em;
  position: relative;
}

article blockquote p {
  margin-bottom: 0.6em;
}
article blockquote p:last-child { margin-bottom: 0; }

article ul, article ol {
  margin: 1.2em 0;
  padding-left: 1.6em;
}

article li {
  margin-bottom: 0.45em;
}

article ul li::marker {
  color: var(--accent);
}

article ol li::marker {
  color: var(--accent);
  font-family: var(--font-display);
  font-weight: 500;
  font-style: italic;
}

article hr {
  border: none;
  margin: 3.5em auto;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 1em;
  position: relative;
}

article hr::before {
  content: '❋';
  font-family: var(--font-display);
  color: var(--accent);
  font-size: 18px;
  opacity: 0.6;
  background: var(--bg);
  padding: 0 16px;
  position: relative;
  z-index: 1;
}

article hr::after {
  content: '';
  position: absolute;
  inset: 50% 0;
  height: 1px;
  background: var(--line);
  z-index: 0;
}

/* Code */
article code {
  font-family: var(--font-mono);
  font-size: 0.86em;
  background: var(--code-bg);
  color: var(--code-ink);
  padding: 0.15em 0.45em;
  border-radius: 4px;
  border: 1px solid var(--line-soft);
}

article pre {
  background: var(--code-bg);
  border: 1px solid var(--line-soft);
  border-radius: 12px;
  padding: 24px 28px;
  margin: 2em 0;
  overflow-x: auto;
  font-size: 14px;
  line-height: 1.65;
  position: relative;
}

article pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: inherit;
  color: var(--code-ink);
}

/* Tables */
article table {
  width: 100%;
  margin: 2em 0;
  border-collapse: collapse;
  font-family: var(--font-ui);
  font-size: 15px;
}

article th {
  font-family: var(--font-ui);
  font-weight: 600;
  text-align: left;
  padding: 12px 18px 12px 0;
  border-bottom: 2px solid var(--ink);
  color: var(--ink);
  font-size: 12px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

article td {
  padding: 14px 18px 14px 0;
  border-bottom: 1px solid var(--line-soft);
  vertical-align: top;
  color: var(--ink-2);
}

article tr:hover td {
  color: var(--ink);
}

/* Images */
article img {
  max-width: 100%;
  height: auto;
  border-radius: 10px;
  margin: 1.8em 0;
  box-shadow: var(--shadow);
}

/* Task lists */
article input[type="checkbox"] {
  margin-right: 8px;
  accent-color: var(--accent);
}

/* End marker */
.end-mark {
  text-align: center;
  margin-top: 80px;
  padding-top: 40px;
  font-family: var(--font-display);
  font-style: italic;
  font-size: 14px;
  color: var(--ink-3);
  letter-spacing: 0.15em;
}

.end-mark::before {
  content: '';
  display: block;
  width: 40px;
  height: 1px;
  background: var(--line);
  margin: 0 auto 20px;
}

/* Highlight.js theme — adapted to our palette */
.hljs-comment, .hljs-quote { color: var(--ink-3); font-style: italic; }
.hljs-keyword, .hljs-selector-tag, .hljs-subst { color: var(--accent); font-weight: 500; }
.hljs-number, .hljs-literal, .hljs-variable, .hljs-template-variable, .hljs-tag .hljs-attr { color: #B4572C; }
.hljs-string, .hljs-doctag { color: #5B7A4E; }
.hljs-title, .hljs-section, .hljs-selector-id { color: var(--code-ink); font-weight: 600; }
.hljs-type, .hljs-class .hljs-title { color: #8B6D2E; }
.hljs-tag, .hljs-name, .hljs-attribute { color: var(--accent); }
.hljs-regexp, .hljs-link { color: #5B7A4E; }
.hljs-symbol, .hljs-bullet { color: #B4572C; }
.hljs-built_in, .hljs-builtin-name { color: #8B6D2E; }
.hljs-meta { color: var(--ink-3); }
.hljs-deletion { background: rgba(180, 87, 44, 0.2); }
.hljs-addition { background: rgba(91, 122, 78, 0.2); }
.hljs-emphasis { font-style: italic; }
.hljs-strong { font-weight: 700; }

[data-theme="dark"] .hljs-keyword,
[data-theme="dark"] .hljs-selector-tag,
[data-theme="dark"] .hljs-subst { color: #E8A87C; }
[data-theme="dark"] .hljs-number,
[data-theme="dark"] .hljs-literal,
[data-theme="dark"] .hljs-variable,
[data-theme="dark"] .hljs-tag .hljs-attr { color: #E89A6C; }
[data-theme="dark"] .hljs-string { color: #A8C090; }
[data-theme="dark"] .hljs-title,
[data-theme="dark"] .hljs-section { color: #F3EEE2; }
[data-theme="dark"] .hljs-type,
[data-theme="dark"] .hljs-class .hljs-title { color: #D4B87A; }
[data-theme="dark"] .hljs-tag,
[data-theme="dark"] .hljs-name { color: #E8A87C; }

/* Responsive */
@media (max-width: 960px) {
  .reader-layout {
    grid-template-columns: 1fr;
    padding: 32px 24px 80px;
    gap: 0;
  }
  #toc {
    display: none;
  }
  article {
    font-size: 17.5px;
  }
  article h1 { font-size: 42px; }
  article h2 { font-size: 28px; }
  #topbar { padding: 0 16px; height: 56px; }
  #reader-view { padding-top: 56px; }
  .btn span { display: none; }
  .btn { padding: 8px; }
  .filename { display: none; }
  .deco-mark { display: none; }
}

@media (max-width: 520px) {
  article h1 + p::first-letter {
    font-size: 4em;
  }
}

.hidden { display: none !important; }

/* ─── Library drawer (Wave 1) ─── */
#library-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(26, 23, 20, 0.32);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
  z-index: 90;
}

#library-backdrop.visible {
  opacity: 1;
  pointer-events: auto;
}

#library-drawer {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  width: min(380px, 92vw);
  background: var(--bg-paper);
  border-left: 1px solid var(--line);
  box-shadow: -8px 0 32px rgba(26, 23, 20, 0.12);
  transform: translateX(100%);
  transition: transform 0.32s cubic-bezier(0.22, 1, 0.36, 1);
  z-index: 95;
  display: flex;
  flex-direction: column;
}

#library-drawer.open {
  transform: translateX(0);
}

.lib-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px 12px;
  border-bottom: 1px solid var(--line-soft);
}

.lib-heading {
  font-family: var(--font-display);
  font-variation-settings: 'opsz' 72, 'wght' 400;
  font-style: italic;
  font-size: 24px;
  letter-spacing: -0.02em;
  color: var(--ink);
  margin: 0;
}

.lib-search-wrap {
  position: relative;
  padding: 16px 20px 12px;
}

.lib-search-icon {
  position: absolute;
  left: 32px;
  top: 50%;
  transform: translateY(-50%);
  width: 14px;
  height: 14px;
  color: var(--ink-3);
  stroke-width: 2;
  pointer-events: none;
}

#library-search {
  width: 100%;
  padding: 10px 14px 10px 36px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  font-family: var(--font-ui);
  font-size: 14px;
  color: var(--ink);
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}

#library-search:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-2);
}

.lib-list {
  list-style: none;
  margin: 0;
  padding: 8px 12px 24px;
  overflow-y: auto;
  flex: 1;
}

.lib-list::-webkit-scrollbar { width: 4px; }
.lib-list::-webkit-scrollbar-thumb { background: var(--line); border-radius: 2px; }

.lib-item {
  display: flex;
  align-items: stretch;
  border-radius: 8px;
  margin-bottom: 2px;
  transition: background 0.15s ease;
}

.lib-item:hover {
  background: var(--accent-2);
}

.lib-item.active {
  background: var(--accent-2);
}

.lib-item.active .lib-title {
  color: var(--accent);
  font-weight: 600;
}

.lib-open {
  flex: 1;
  background: transparent;
  border: none;
  padding: 12px 14px;
  text-align: left;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-family: var(--font-ui);
  color: inherit;
  border-radius: 8px;
  min-width: 0;
}

.lib-title {
  font-size: 14px;
  font-weight: 500;
  color: var(--ink);
  letter-spacing: -0.005em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.lib-meta {
  font-size: 11px;
  color: var(--ink-3);
  letter-spacing: 0.02em;
}

.lib-delete {
  background: transparent;
  border: none;
  padding: 0 12px;
  cursor: pointer;
  color: var(--ink-3);
  opacity: 0;
  transition: opacity 0.15s, color 0.15s;
  display: flex;
  align-items: center;
  border-radius: 8px;
}

.lib-delete svg {
  width: 14px;
  height: 14px;
}

.lib-item:hover .lib-delete {
  opacity: 1;
}

.lib-delete:hover {
  color: var(--accent);
}

.lib-empty {
  padding: 48px 24px;
  text-align: center;
  font-family: var(--font-ui);
  color: var(--ink-3);
  font-size: 14px;
  display: none;
}

.lib-empty p { margin: 0 0 6px; }

.lib-empty-hint {
  font-size: 12px;
  opacity: 0.8;
}

.lib-empty code {
  font-family: var(--font-mono);
  font-size: 0.9em;
  background: var(--accent-2);
  padding: 1px 6px;
  border-radius: 3px;
  color: var(--accent);
}

/* ─── Stats badge + modal (Wave 2) ─── */
.stats-badge {
  background: transparent;
  border: 1px solid transparent;
  padding: 6px 12px;
  border-radius: 8px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-family: var(--font-ui);
  font-size: 12px;
  font-weight: 500;
  color: var(--ink-2);
  letter-spacing: -0.005em;
  transition: all 0.2s ease;
  margin-right: auto;
  margin-left: 12px;
}

.stats-badge:hover {
  background: var(--accent-2);
  color: var(--ink);
  border-color: var(--line);
}

.stats-flame {
  display: inline-flex;
  width: 14px;
  height: 14px;
  color: var(--ink-3);
  transition: color 0.3s ease, transform 0.3s ease;
}

.stats-flame svg { width: 14px; height: 14px; }

.stats-flame.active {
  color: var(--accent);
  animation: flicker 2.4s ease-in-out infinite;
}

@keyframes flicker {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.08); }
}

.stats-streak-num {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
}

.stats-sep {
  color: var(--ink-3);
  opacity: 0.6;
}

.stats-today {
  font-variant-numeric: tabular-nums;
  color: var(--ink-3);
}

@media (max-width: 720px) {
  .stats-badge .stats-sep,
  .stats-badge .stats-today { display: none; }
}

/* Modal */
#stats-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(26, 23, 20, 0.36);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
  z-index: 110;
}

#stats-backdrop.visible {
  opacity: 1;
  pointer-events: auto;
}

#stats-modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -45%) scale(0.96);
  opacity: 0;
  pointer-events: none;
  width: min(560px, 92vw);
  max-height: 85vh;
  overflow-y: auto;
  background: var(--bg-paper);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--shadow-lg);
  z-index: 115;
  transition: transform 0.32s cubic-bezier(0.22, 1, 0.36, 1), opacity 0.25s ease;
  padding: 28px 32px;
}

#stats-modal.open {
  transform: translate(-50%, -50%) scale(1);
  opacity: 1;
  pointer-events: auto;
}

.stats-modal-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 28px;
}

.stats-modal-header h2 {
  font-family: var(--font-display);
  font-variation-settings: 'opsz' 72, 'wght' 400;
  font-style: italic;
  font-size: 26px;
  letter-spacing: -0.02em;
  color: var(--ink);
  margin: 0;
}

.stats-hero {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  padding: 20px;
  background: var(--bg);
  border-radius: 12px;
  margin-bottom: 28px;
}

.stats-hero-streak {
  text-align: center;
  border-right: 1px solid var(--line-soft);
  padding-right: 24px;
}

.stats-hero-num {
  font-family: var(--font-display);
  font-variation-settings: 'opsz' 144, 'wght' 350;
  font-size: 64px;
  line-height: 1;
  color: var(--accent);
  letter-spacing: -0.04em;
  margin-bottom: 6px;
}

.stats-hero-label {
  font-family: var(--font-ui);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-3);
}

.stats-hero-today {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 12px;
}

.stats-hero-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-family: var(--font-ui);
}

.stats-hero-key {
  font-size: 13px;
  color: var(--ink-3);
  letter-spacing: -0.005em;
}

.stats-hero-val {
  font-size: 18px;
  font-weight: 600;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
}

.stats-section-title {
  font-family: var(--font-ui);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--ink-3);
  margin: 0 0 14px;
}

.stats-chart-section { margin-bottom: 28px; }

.stats-chart {
  display: grid;
  grid-template-columns: repeat(14, 1fr);
  gap: 4px;
  height: 80px;
  align-items: end;
  padding: 8px 4px;
  background: var(--bg);
  border-radius: 8px;
}

.stats-bar-col {
  height: 100%;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.stats-bar {
  width: 100%;
  background: var(--line);
  border-radius: 3px 3px 0 0;
  min-height: 2px;
  transition: background 0.2s;
}

.stats-bar.eligible {
  background: var(--accent);
  opacity: 0.7;
}

.stats-bar.eligible:hover {
  opacity: 1;
}

.stats-totals {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.stats-total {
  text-align: center;
  padding: 14px 8px;
  background: var(--bg);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.stats-total-num {
  font-family: var(--font-display);
  font-variation-settings: 'opsz' 72, 'wght' 500;
  font-size: 22px;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  line-height: 1;
}

.stats-total-label {
  font-family: var(--font-ui);
  font-size: 10px;
  font-weight: 500;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--ink-3);
}

@media (max-width: 520px) {
  .stats-hero { grid-template-columns: 1fr; }
  .stats-hero-streak { border-right: none; border-bottom: 1px solid var(--line-soft); padding: 0 0 20px; }
  .stats-totals { grid-template-columns: repeat(2, 1fr); }
  .stats-hero-num { font-size: 52px; }
}

/* ─── Annotations (Wave 3) ─── */
.ann-mark {
  border-radius: 2px;
  padding: 0 1px;
  cursor: pointer;
  background: transparent;
  color: inherit;
  transition: filter 0.15s ease;
  position: relative;
  text-decoration-skip-ink: none;
}

.ann-mark:hover {
  filter: brightness(0.97);
}

.ann-yellow { background: rgba(255, 215, 95, 0.42); }
.ann-orange { background: rgba(232, 168, 124, 0.45); }
.ann-green  { background: rgba(170, 200, 130, 0.42); }

[data-theme="dark"] .ann-yellow { background: rgba(255, 215, 95, 0.22); }
[data-theme="dark"] .ann-orange { background: rgba(232, 168, 124, 0.28); }
[data-theme="dark"] .ann-green  { background: rgba(170, 200, 130, 0.22); }

.ann-mark.has-note::after {
  content: '';
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  margin-left: 2px;
  vertical-align: super;
  font-size: 50%;
}

#ann-toolbar {
  position: absolute;
  transform: translate(-50%, 0);
  background: var(--ink);
  border-radius: 10px;
  padding: 6px 8px;
  display: flex;
  align-items: center;
  gap: 6px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.18), 0 1px 3px rgba(0,0,0,0.12);
  opacity: 0;
  pointer-events: none;
  transform-origin: bottom center;
  transition: opacity 0.16s ease, transform 0.16s ease;
  transform: translate(-50%, 4px) scale(0.96);
  z-index: 200;
}

#ann-toolbar.visible {
  opacity: 1;
  pointer-events: auto;
  transform: translate(-50%, 0) scale(1);
}

#ann-toolbar::after {
  content: '';
  position: absolute;
  bottom: -5px;
  left: 50%;
  transform: translateX(-50%) rotate(45deg);
  width: 10px;
  height: 10px;
  background: var(--ink);
  border-radius: 2px;
}

.ann-color {
  width: 22px;
  height: 22px;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  padding: 0;
  position: relative;
  z-index: 1;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}

.ann-color:hover {
  transform: scale(1.15);
  box-shadow: 0 0 0 2px rgba(255,255,255,0.2);
}

.ann-color.ann-yellow { background: #FFD75F; }
.ann-color.ann-orange { background: #E8A87C; }
.ann-color.ann-green  { background: #AAC882; }

.ann-sep {
  width: 1px;
  height: 18px;
  background: rgba(255,255,255,0.18);
  margin: 0 2px;
  position: relative;
  z-index: 1;
}

.ann-note-btn {
  background: transparent;
  border: none;
  color: rgba(255, 255, 255, 0.85);
  cursor: pointer;
  width: 24px;
  height: 24px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 5px;
  padding: 0;
  position: relative;
  z-index: 1;
  transition: background 0.15s, color 0.15s;
}

.ann-note-btn:hover {
  background: rgba(255, 255, 255, 0.1);
  color: white;
}

.ann-note-btn svg { width: 13px; height: 13px; }

/* ─── Focus mode (Wave 3) ─── */
body.focus-mode #content > * {
  opacity: 0.32;
  transition: opacity 0.45s cubic-bezier(0.22, 1, 0.36, 1);
}

body.focus-mode #content > .focus-active {
  opacity: 1;
}

body.focus-mode #content > .end-mark { opacity: 1; }

body.focus-mode #toc {
  opacity: 0.5;
  transition: opacity 0.4s ease;
}

body.focus-mode #toc:hover {
  opacity: 1;
}

#focus-btn.active {
  background: var(--accent-2);
  color: var(--accent);
}

#focus-btn.active svg circle {
  fill: var(--accent);
}

/* ─── Front matter cover (Tier 2) ─── */
.fm-cover {
  margin-bottom: 56px;
  padding-bottom: 32px;
  border-bottom: 1px solid var(--line-soft);
}

.fm-eyebrow {
  font-family: var(--font-ui);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 16px;
}

.fm-title {
  font-family: var(--font-display);
  font-variation-settings: 'opsz' 144, 'wght' 350;
  font-size: clamp(40px, 6vw, 62px);
  line-height: 1;
  letter-spacing: -0.03em;
  color: var(--ink);
  margin: 0 0 14px;
  position: relative;
}

.fm-title::after {
  display: none;
}

.fm-subtitle {
  font-family: var(--font-display);
  font-variation-settings: 'opsz' 72, 'wght' 350;
  font-style: italic;
  font-size: 22px;
  color: var(--ink-2);
  letter-spacing: -0.01em;
  line-height: 1.4;
  margin: 0 0 20px;
}

.fm-meta {
  font-family: var(--font-ui);
  font-size: 13px;
  color: var(--ink-3);
  display: inline-flex;
  align-items: center;
  gap: 8px;
  letter-spacing: -0.005em;
}

.fm-author { font-weight: 500; color: var(--ink-2); }

.fm-sep { opacity: 0.5; }

.fm-tags {
  margin-top: 18px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.fm-tag {
  font-family: var(--font-ui);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.04em;
  padding: 3px 10px;
  border-radius: 99px;
  background: var(--accent-2);
  color: var(--accent);
}

/* ─── Editor drawer (Tier 2) ─── */
#editor-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(26, 23, 20, 0.36);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
  z-index: 100;
}

#editor-backdrop.visible {
  opacity: 1;
  pointer-events: auto;
}

#editor-drawer {
  position: fixed;
  top: 0; left: 0; bottom: 0;
  width: min(640px, 96vw);
  background: var(--bg-paper);
  border-right: 1px solid var(--line);
  box-shadow: 8px 0 32px rgba(26, 23, 20, 0.12);
  transform: translateX(-100%);
  transition: transform 0.32s cubic-bezier(0.22, 1, 0.36, 1);
  z-index: 105;
  display: flex;
  flex-direction: column;
}

#editor-drawer.open {
  transform: translateX(0);
}

.editor-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--line-soft);
}

#editor-title {
  flex: 1;
  background: transparent;
  border: none;
  font-family: var(--font-display);
  font-variation-settings: 'opsz' 72, 'wght' 400;
  font-style: italic;
  font-size: 22px;
  letter-spacing: -0.02em;
  color: var(--ink);
  outline: none;
  padding: 4px 0;
  border-bottom: 1px solid transparent;
  transition: border-color 0.2s;
}

#editor-title:focus {
  border-bottom-color: var(--accent-3);
}

.editor-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

#editor-save {
  background: var(--ink);
  color: var(--bg-paper);
  border: 1px solid var(--ink);
}

#editor-save:hover {
  background: var(--accent);
  border-color: var(--accent);
}

#editor-save:disabled {
  background: var(--bg-raised);
  color: var(--ink-3);
  border-color: var(--line);
  cursor: not-allowed;
}

#editor-textarea {
  flex: 1;
  width: 100%;
  resize: none;
  border: none;
  outline: none;
  background: var(--bg-paper);
  color: var(--ink);
  font-family: var(--font-mono);
  font-size: 14px;
  line-height: 1.65;
  padding: 24px 24px 24px 28px;
  letter-spacing: -0.005em;
  tab-size: 2;
}

#editor-textarea:focus {
  outline: none;
}

.editor-footer {
  padding: 10px 20px;
  border-top: 1px solid var(--line-soft);
  background: var(--bg);
}

.editor-hint {
  font-family: var(--font-ui);
  font-size: 11px;
  color: var(--ink-3);
  letter-spacing: 0.02em;
}

/* ─── Print / PDF export (Tier 2) ─── */
@media print {
  body {
    background: white !important;
    color: #1A1714 !important;
  }
  body::before { display: none !important; }

  #topbar,
  #toc,
  #upload-view,
  #library-drawer,
  #library-backdrop,
  #editor-drawer,
  #editor-backdrop,
  #stats-modal,
  #stats-backdrop,
  #ann-toolbar,
  #progress,
  .deco-mark,
  #new-file-btn,
  .stats-badge {
    display: none !important;
  }

  .reader-layout {
    grid-template-columns: 1fr !important;
    padding: 0 !important;
    gap: 0 !important;
    max-width: none !important;
  }

  #reader-view {
    padding-top: 0 !important;
  }

  article {
    max-width: none !important;
    font-size: 11pt !important;
    line-height: 1.55 !important;
    color: #1A1714 !important;
  }

  article h1 {
    font-size: 26pt !important;
    page-break-after: avoid;
  }

  article h2, article h3, article h4 {
    page-break-after: avoid;
  }

  article p, article li {
    orphans: 3;
    widows: 3;
  }

  article blockquote, article pre, article table, article img {
    page-break-inside: avoid;
  }

  article a {
    color: #1A1714 !important;
    text-decoration: underline !important;
  }

  article a[href^="http"]::after {
    content: " (" attr(href) ")";
    font-size: 8pt;
    color: #555;
    word-break: break-all;
  }

  .ann-yellow, .ann-orange, .ann-green {
    background: rgba(0, 0, 0, 0.07) !important;
    color: inherit !important;
  }

  .end-mark {
    margin-top: 40px !important;
  }

  body.focus-mode #content > * {
    opacity: 1 !important;
  }
}
