:root {
  --color-1808: #f5c211;
  --color-1826: #c01c28;
  --color-1849: #8e44ad;
  --unified-text-scale: 1;
  --header-offset: 272px; /* updated at runtime */
}

* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: 'Crimson Text', Georgia, serif; line-height: 2; color: #2c3e50; background: #f8f9fa; }

/* Header */
.header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 2rem; box-shadow: 0 4px 20px rgba(0,0,0,0.1); position: sticky; top: 0; z-index: 1000; }
.header-content { max-width: 1400px; margin: 0 auto; }
.header h1 { font-size: 2rem; font-weight: 300; letter-spacing: 2px; }

/* Layout */
.layout { display: grid; grid-template-columns: 280px 1fr; gap: 2rem; max-width: 1400px; margin: 2rem auto; padding: 0 1rem; }
.sidebar { position: sticky; top: 100px; height: calc(100vh - 120px); overflow-y: auto; background: white; border-radius: 12px; padding: 1.5rem; box-shadow: 0 2px 16px rgba(0,0,0,0.08); }
.sidebar h3 { font-size: 1rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; color: #495057; margin-bottom: 1rem; padding-bottom: 0.5rem; border-bottom: 2px solid #e9ecef; }

/* Reading mode */
.reading-mode { margin-bottom: 2rem; margin-top: 2rem; }
.reading-mode-item { display: flex; align-items: center; gap: 0.5rem; padding: 0.6rem 0.8rem; cursor: pointer; border-radius: 6px; margin-bottom: 0.4rem; transition: background 0.2s; font-size: 0.9rem; }
.reading-mode-item:hover { background: #f8f9fa; }
.reading-mode-item.active { background: #667eea; color: white; font-weight: 600; }
.reading-mode-radio { width: 16px; height: 16px; cursor: pointer; }

/* TOC */
.toc { font-size: 0.85rem; }
.toc-group { margin-bottom: 0.3rem; }
.toc-group-header { padding: 0.4rem 0.5rem; cursor: pointer; border-radius: 4px; transition: background 0.2s; display: flex; justify-content: space-between; align-items: center; font-weight: 600; }
.toc-group-header:hover { background: #f8f9fa; }
.toc-group-header.expanded { background: #e9ecef; }
.toc-group-items { display: none; padding-left: 0.5rem; margin-top: 0.2rem; }
.toc-group-items.expanded { display: block; }
.toc-item { padding: 0.3rem 0.5rem; cursor: pointer; border-radius: 4px; transition: background 0.2s; display: flex; justify-content: space-between; align-items: center; font-size: 0.85rem; }
.toc-item:hover { background: #f8f9fa; }
.toc-item.active { background: #667eea; color: white; }
.toc-badge { font-size: 0.7rem; padding: 0.1rem 0.3rem; border-radius: 3px; background: #e9ecef; color: #495057; }
.toc-item.active .toc-badge { background: rgba(255,255,255,0.3); color: white; }

/* Legend */
.main-content { min-height: 100vh; }

.legend-shell {
  position: sticky;
  top: var(--header-offset);
  z-index: 900; /* below header */
  background: white;
  padding: 0.8rem 1rem 0rem 1rem;
  border-radius: 12px;
  box-shadow: 0 2px 16px rgba(0,0,0,0.08);
}

.legend-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  flex-wrap: wrap;
  background: white;
  padding: 0.35rem 1rem;
  border-radius: 12px;
  margin-bottom: 0.6rem;
  position: relative;
}

#legend-title {
  margin: 0 0 0.4rem 0;
  color: #495057;
  font-size: 0.9rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.legend-items { display: flex; flex-wrap: wrap; gap: 1.2rem; }
.legend-item { display: flex; align-items: center; gap: 0.5rem; font-size: 0.9rem; }
.color-box { width: 16px; height: 16px; border-radius: 3px; }
.color-box.blue { background: var(--color-1808); }
.color-box.red { background: var(--color-1826); }
.color-box.black { background: var(--color-1849); }
.legend-actions { display: inline-flex; align-items: center; gap: 0.35rem; }
#color-settings, .font-btn { border: 1px solid #dde1e7; background: #fff; cursor: pointer; font-size: 0.95rem; border-radius: 6px; padding: 0.2rem 0.5rem; }

#legend-panel { display: none; margin-top: 0.6rem; }
#legend-panel.expanded { display: block; }

#color-panel { display: grid; gap: 1rem; }
#color-panel label { display: inline-flex; align-items: center; gap: 0.5rem; margin-right: 1rem; font-size: 0.9rem; }
.inline-toggle { display:inline-flex;align-items:center;gap:0.35rem; }
.settings-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); align-items: start; gap: 1rem; }
.settings-col { display: flex; flex-direction: column; gap: 0.6rem; }
.settings-group { display: flex; flex-direction: column; gap: 0.35rem; }

/* Paragraph cards */
.paragraph-card {
  background: white;
  border-radius: 12px;
  padding: 2rem;
  margin-bottom: 1.5rem;
  box-shadow: 0 2px 16px rgba(0,0,0,0.08);
  scroll-margin-top: 180px;
  position: relative;
}

.paragraph-header { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 1rem; }
.paragraph-number { font-size: 1rem; font-weight: 700; color: #667eea; display: flex; align-items: center; gap: 0.5rem; }
.paragraph-number a { color: inherit; text-decoration: none; display: flex; align-items: center; gap: 0.5rem; }
.paragraph-number .anchor-icon { opacity: 0; transition: opacity 0.2s; font-size: 0.9rem; color: #cbd5e0; }
.paragraph-number:hover .anchor-icon { opacity: 1; }

.paragraph-badges-group { display: inline-flex; align-items: center; justify-content: center; gap: 0.5rem; }
.paragraph-badges-group.plain-edition-chip { justify-content: center; }
.paragraph-badges { display: flex; gap: 0.5rem; align-items: center; }
.edition-chips { display: inline-flex; gap: 0.35rem; align-items: center; }

.similarity-badge { padding: 0.2rem 0.6rem; border-radius: 4px; font-size: 0.75rem; font-weight: 600; }
.similarity-badge.high { background: #d4edda; color: #155724; }
.similarity-badge.medium { background: #fff3cd; color: #856404; }
.similarity-badge.low { background: #f8d7da; color: #721c24; }

.new-badge { padding: 0.25rem 0.7rem; border-radius: 4px; font-size: 0.8rem; font-weight: 600; background: #2c3e50; color: white; }
.info-icon { font-size: 0.75rem; color: #6c757d; cursor: help; padding: 0 0.2rem; }

.paragraph-body { display: grid; grid-template-columns: 1fr 220px; gap: 2rem; align-items: start; }
.unified-text { font-size: calc(1.05rem * var(--unified-text-scale, 1)); line-height: 2.2; text-align: justify; }

.word { display: inline; transition: opacity 0.2s, text-decoration 0.2s, background 0.2s; }
.word.filtered-out { opacity: 0.2; }
.word .char-variant { font-weight: 500; padding: 0 1px; border-radius: 2px; background: transparent; color: inherit; }

.apparatus {
  font-size: 0.82rem;
  color: #6c757d;
  padding-left: 1rem;
  border-left: 2px solid #e9ecef;
  max-height: 400px;
  overflow-y: auto;
  position: sticky;
  top: 120px;
}
.apparatus::-webkit-scrollbar { width: 6px; }
.apparatus::-webkit-scrollbar-track { background: #f1f1f1; border-radius: 3px; }
.apparatus::-webkit-scrollbar-thumb { background: #cbd5e0; border-radius: 3px; }
.apparatus::-webkit-scrollbar-thumb:hover { background: #a0aec0; }

.apparatus-note { margin-bottom: 0.8rem; padding: 0.4rem; background: #f8f9fa; border-radius: 4px; line-height: 1.6; transition: background 0.2s, transform 0.2s; }
.apparatus-note.hovering { background: #fff8e1; }
.apparatus-empty { font-style: italic; color: #adb5bd; }

.inline-highlight { background: #fff3cd; box-shadow: 0 0 0 2px #ffe08a; transition: background 0.15s ease, box-shadow 0.15s ease; }

/* Plain edition view: full isolation */
.paragraph-body.plain-edition-view { grid-template-columns: 1fr; }
.paragraph-body.plain-edition-view .apparatus { display: none; }
.word.plain-edition { color: inherit; text-decoration: none; }

/* Correction mode: bottom-left drawer */
.correction-drawer {
  position: fixed;
  left: 16px;
  bottom: 16px;
  z-index: 2500;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 10px;
  user-select: none;
}

.correction-fab {
  width: 44px;
  height: 44px;
  border-radius: 999px;
  border: 1px solid #dde1e7;
  background: #fff;
  cursor: pointer;
  font-size: 18px;
  box-shadow: 0 6px 18px rgba(0,0,0,0.12);
}

.correction-panel {
  width: 320px;
  background: #fff;
  border: 1px solid #dde1e7;
  border-radius: 12px;
  box-shadow: 0 10px 26px rgba(0,0,0,0.14);
  padding: 10px 12px 12px 12px;
}

.correction-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.correction-title {
  font-weight: 700;
  color: #2c3e50;
}

.correction-close {
  border: 1px solid #dde1e7;
  background: #fff;
  cursor: pointer;
  border-radius: 8px;
  width: 30px;
  height: 30px;
  font-size: 18px;
  line-height: 1;
}

.correction-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 6px 0 10px 0;
}

.correction-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
}

.correction-status {
  font-size: 0.85rem;
  color: #6c757d;
  white-space: nowrap;
}

.correction-section { margin-top: 10px; }
.correction-section-title {
  font-size: 0.78rem;
  color: #6c757d;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 6px;
}

.correction-filters {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 10px;
  font-size: 0.86rem;
  color: #2c3e50;
}

.correction-nav {
  display: flex;
  gap: 10px;
  margin-top: 6px;
}
.correction-nav button {
  flex: 1;
  border: 1px solid #dde1e7;
  background: #fff;
  cursor: pointer;
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 1rem;
}

.correction-hint {
  margin-top: 8px;
  font-size: 0.78rem;
  color: #6c757d;
}

.correction-hr {
  border: 0;
  border-top: 1px solid #eef1f5;
  margin: 10px 0;
}

.correction-explain {
  margin-top: 6px;
  font-size: 0.78rem;
  color: #6c757d;
  line-height: 1.3;
}

.correction-jump {
  display: grid;
  grid-template-columns: 26px 1fr 52px;
  gap: 8px;
  align-items: center;
}

.correction-jump-label {
  font-size: 0.9rem;
  color: #6c757d;
  text-align: right;
}

.cm-jump-input {
  border: 1px solid #dde1e7;
  border-radius: 8px;
  padding: 6px 8px;
  font-size: 0.9rem;
}

.cm-jump-go {
  border: 1px solid #dde1e7;
  background: #fff;
  cursor: pointer;
  border-radius: 8px;
  padding: 6px 8px;
  font-size: 0.9rem;
}

.correction-filters-2col {
  grid-template-columns: 1fr 1fr;
}

/* Active variant highlight */
.cm-active {
  background: #fff3cd;
  box-shadow: 0 0 0 2px #ffe08a;
  border-radius: 2px;
}

.cm-active-note {
  background: #fff8e1 !important;
  box-shadow: 0 0 0 1px rgba(255, 200, 80, 0.55);
  border-radius: 4px;
}

/* Correction focus: hide apparatus column */
body.cm-hide-apparatus .paragraph-body {
  grid-template-columns: 1fr;
}
body.cm-hide-apparatus .apparatus {
  display: none !important;
}

/* Correction focus: hide paragraph stats (right header column) */
body.cm-hide-stats .paragraph-header .paragraph-badges {
  display: none !important;
}

/* Misc */
.loading { text-align: center; padding: 4rem 2rem; font-size: 1.2rem; color: #6c757d; font-style: italic; }
.error { background: #f8d7da; color: #721c24; padding: 1.5rem; border-radius: 8px; margin: 2rem; border-left: 4px solid #721c24; }
#sentinel { height: 20px; margin: 2rem 0; }


/* -------------------------------------------------------
   Responsive layout
------------------------------------------------------- */

/* small-screen notice: hidden by default */
.min-width-notice {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 5000;
  background: rgba(248, 249, 250, 0.98);
  padding: 24px;
}

.min-width-notice-inner {
  max-width: 720px;
  margin: 15vh auto 0 auto;
  background: #fff;
  border: 1px solid #dde1e7;
  border-radius: 12px;
  padding: 16px 18px;
  box-shadow: 0 10px 26px rgba(0,0,0,0.12);
  font-size: 1rem;
  color: #2c3e50;
}

/* 1) Hide sidebar on narrower viewports; main gets full width */
@media (max-width: 1100px) {
  .layout {
    grid-template-columns: 1fr;
    gap: 1rem;
  }
  .sidebar {
    display: none;
  }
}

/* 2) Ultra-small screens: show notice and hide the app */
@media (max-width: 760px) {
  .min-width-notice {
    display: block;
  }

  /* hide underlying app UI to avoid unusable layout */
  .header,
  .layout,
  .correction-drawer {
    display: none !important;
  }
}
