:root {
  color-scheme: light;
  --bg: #f4f7fb;
  --card: #ffffff;
  --card-soft: #f8fbff;
  --text: #1f2937;
  --muted: #64748b;
  --line: #dbe3ef;
  --primary: #2563eb;
  --primary-soft: #dbeafe;
  --shadow: 0 16px 40px rgba(15, 23, 42, 0.08);
  --topbar-height: 64px;
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  font-family: "Microsoft YaHei", "PingFang SC", sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at top left, #eef5ff 0, #f4f7fb 28%, #f4f7fb 100%);
}

button {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
  border-radius: 10px;
  padding: 8px 12px;
  cursor: pointer;
  transition: all 0.2s ease;
}

button:hover {
  border-color: #b7c5da;
  transform: translateY(-1px);
}

.topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 40;
  height: var(--topbar-height);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  border-bottom: 1px solid rgba(219, 227, 239, 0.95);
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(12px);
}

.topbar h1 {
  margin: 0;
  font-size: 20px;
  flex: 1;
  letter-spacing: 0.5px;
}

.site-title {
  display: flex;
  align-items: center;
  gap: 10px;
}

.site-icon {
  width: 26px;
  height: 26px;
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.site-icon svg {
  width: 100%;
  height: 100%;
  display: block;
}

.site-title-text {
  min-width: 0;
}

.book-picker {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 14px;
}

.book-picker label {
  white-space: nowrap;
}

.book-picker select {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
  border-radius: 10px;
  padding: 8px 12px;
  min-width: 180px;
  font: inherit;
}

.toolbar,
.menu-toggle {
  display: flex;
  gap: 8px;
}

.layout {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  gap: 20px;
  align-items: start;
  padding: calc(var(--topbar-height) + 20px) 20px 24px;
}

.sidebar {
  position: sticky;
  top: calc(var(--topbar-height) + 20px);
  max-height: calc(100vh - var(--topbar-height) - 40px);
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.9);
  box-shadow: var(--shadow);
  padding: 16px 14px;
}

body.sidebar-collapsed .layout {
  grid-template-columns: 1fr;
}

body.sidebar-collapsed .sidebar {
  display: none;
}

.sidebar-title {
  position: sticky;
  top: -16px;
  z-index: 2;
  margin: -16px -14px 12px;
  padding: 16px 14px 12px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: linear-gradient(180deg, #ffffff 0%, rgba(255, 255, 255, 0.98) 100%);
  border-bottom: 1px solid #eef2f7;
}

.sidebar-actions {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.sidebar-actions button {
  padding: 6px 10px;
  font-size: 12px;
  border-radius: 10px;
}

.outline-book-block + .outline-book-block {
  margin-top: 14px;
}

.outline-book-title {
  margin-bottom: 8px;
  font-size: 15px;
  font-weight: 700;
  color: #0f172a;
}

.outline-chapter {
  padding: 8px 10px 10px;
  border: 1px solid #edf2f8;
  border-radius: 12px;
  background: var(--card-soft);
}

.outline-chapter + .outline-chapter {
  margin-top: 10px;
}

.outline-chapter-summary {
  list-style: none;
  cursor: pointer;
  user-select: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.outline-chapter-summary::-webkit-details-marker {
  display: none;
}

.outline-chapter-caret {
  width: 10px;
  height: 10px;
  border-right: 2px solid #94a3b8;
  border-bottom: 2px solid #94a3b8;
  transform: rotate(-45deg);
  transition: transform 0.15s ease;
  flex: 0 0 auto;
  margin-left: 8px;
}

.outline-chapter[open] .outline-chapter-caret {
  transform: rotate(45deg);
}

.outline-sections {
  display: grid;
  gap: 6px;
  padding-top: 6px;
}

.outline-link {
  display: block;
  border-radius: 8px;
  color: #1e3a8a;
  text-decoration: none;
  line-height: 1.5;
  padding: 6px 8px;
}

.outline-link:hover {
  background: #eef4ff;
}

.outline-link.active {
  background: #dbeafe;
  color: #1d4ed8;
  box-shadow: inset 0 0 0 1px rgba(37, 99, 235, 0.18);
}

.outline-link-chapter {
  font-weight: 700;
  color: #0f172a;
}

.outline-link-section {
  font-size: 14px;
  color: #475569;
  padding-left: 16px;
}

.main {
  min-width: 0;
}

.panel {
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid var(--line);
  border-radius: 24px;
  box-shadow:
    0 22px 45px rgba(15, 23, 42, 0.14),
    0 0 0 1px rgba(148, 163, 184, 0.18);
  padding: 24px 24px 22px;
}

.panel h2 {
  margin: 0 0 16px;
  font-size: 22px;
  border-left: 4px solid var(--primary);
  padding-left: 12px;
}

.book-block + .book-block {
  margin-top: 20px;
}

.book-title {
  margin: 0 0 14px;
  font-size: 22px;
  color: #0f172a;
}

.chapter,
.section {
  scroll-margin-top: calc(var(--topbar-height) + 18px);
}

.chapter {
  margin-top: 12px;
  border: 1px solid var(--line);
  border-radius: 16px;
  overflow: hidden;
  background: #fff;
}

.section {
  margin-top: 12px;
  border: 1px solid #e7edf6;
  border-radius: 14px;
  overflow: hidden;
  background: #fbfdff;
}

.chapter > summary,
.section > summary {
  list-style: none;
  cursor: pointer;
  user-select: none;
}

.chapter > summary::-webkit-details-marker,
.section > summary::-webkit-details-marker {
  display: none;
}

.summary-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
}

.chapter > .summary-row {
  background: linear-gradient(90deg, #eff6ff 0%, #ffffff 70%);
}

.section > .summary-row {
  background: linear-gradient(90deg, #f8fbff 0%, #ffffff 80%);
}

.summary-title {
  font-weight: 700;
  line-height: 1.5;
}

.summary-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.map-trigger {
  border-color: #bfdbfe;
  background: var(--primary-soft);
  color: #1d4ed8;
  font-weight: 600;
}

.map-trigger.compact {
  padding: 6px 10px;
  font-size: 13px;
}

.pdf-trigger {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #bfdbfe;
  background: #eef4ff;
  color: #1d4ed8;
  font-weight: 600;
  border-radius: 10px;
  padding: 8px 12px;
  text-decoration: none;
  cursor: pointer;
  transition: all 0.2s ease;
}

.pdf-trigger:hover {
  border-color: #b7c5da;
  transform: translateY(-1px);
}

.pdf-trigger.compact {
  padding: 6px 10px;
  font-size: 13px;
}

.pdf-viewer {
  margin: 12px 0 0;
  border: 1px solid #e5edf7;
  border-radius: 16px;
  overflow: hidden;
  background: #fff;
  box-shadow: 0 10px 24px rgba(15, 23, 42, 0.08);
}

.pdf-frame {
  width: 100%;
  height: 78vh;
  border: 0;
  display: block;
  background: #fff;
}

.chapter-content {
  padding: 0 14px 14px;
}

.section-body {
  padding: 4px 14px 16px;
}

.markdown-body {
  line-height: 1.8;
  font-size: 16px;
}

.markdown-body p,
.markdown-body li {
  line-height: 1.8;
}

.markdown-body ul,
.markdown-body ol {
  padding-left: 24px;
}

.markdown-body blockquote {
  margin: 12px 0;
  padding: 10px 14px;
  border-left: 4px solid #bfdbfe;
  background: #f8fbff;
  color: #334155;
  border-radius: 0 10px 10px 0;
}

.content-image {
  display: block;
  max-width: min(100%, 960px);
  margin: 12px auto;
  border: 1px solid #e5edf7;
  border-radius: 14px;
  background: #fff;
  box-shadow: 0 10px 24px rgba(15, 23, 42, 0.08);
  cursor: zoom-in;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.content-image.image-failed {
  min-height: 120px;
  background:
    linear-gradient(135deg, #f8fafc 25%, #eef2f7 25%, #eef2f7 50%, #f8fafc 50%, #f8fafc 75%, #eef2f7 75%, #eef2f7 100%);
  background-size: 24px 24px;
}

table {
  display: block;
  width: 100%;
  overflow: auto;
  border-collapse: collapse;
  background: #fff;
  border-radius: 10px;
}

th,
td {
  border: 1px solid #dbe3ef;
  padding: 8px 10px;
  vertical-align: top;
}

th {
  background: #f8fbff;
}

code {
  background: #eef2f7;
  border-radius: 6px;
  padding: 2px 6px;
  font-size: 0.95em;
}

.code-block {
  margin: 16px 0;
  border: 1px solid #1e293b;
  border-radius: 16px;
  overflow: hidden;
  background: #0d1117;
  box-shadow: 0 12px 28px rgba(15, 23, 42, 0.16);
}

.code-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  background: linear-gradient(180deg, #111827 0%, #0f172a 100%);
  border-bottom: 1px solid rgba(148, 163, 184, 0.2);
}

.code-lang {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 2px 10px;
  border-radius: 999px;
  background: rgba(59, 130, 246, 0.18);
  color: #bfdbfe;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.4px;
}

.code-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.code-toggle,
.code-copy {
  border: 1px solid rgba(148, 163, 184, 0.35);
  background: rgba(15, 23, 42, 0.6);
  color: #e5e7eb;
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 12px;
}

.code-toggle:hover,
.code-copy:hover {
  border-color: rgba(191, 219, 254, 0.55);
  background: rgba(30, 41, 59, 0.9);
}

.code-block.collapsed pre {
  display: none;
}

pre {
  margin: 0;
  background: #0d1117;
  color: #e2e8f0;
  padding: 0;
  overflow: auto;
}

pre code.hljs {
  display: block;
  padding: 16px 18px 18px;
  background: transparent;
  line-height: 1.7;
  font-size: 14px;
  font-family: Consolas, "Cascadia Code", "Courier New", monospace;
  tab-size: 2;
  white-space: pre;
}

.code-line {
  display: grid;
  grid-template-columns: 52px minmax(0, 1fr);
  align-items: start;
}

.line-number {
  display: block;
  padding: 0 12px 0 0;
  color: #64748b;
  text-align: right;
  user-select: none;
  border-right: 1px solid rgba(148, 163, 184, 0.18);
  margin-right: 14px;
}

.line-content {
  display: block;
  min-width: 0;
}

.markdown-body pre code {
  border-radius: 0;
  padding: 16px 18px 18px;
  background: transparent;
}

.markdown-body :not(pre) > code {
  word-break: break-word;
}

.muted {
  color: var(--muted);
}

.map-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.35);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 70;
}

.map-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

.map-panel {
  position: fixed;
  top: 0;
  left: calc(20px + 320px + 20px);
  right: 0;
  height: 100vh;
  padding: 18px 18px 20px;
  background: rgba(255, 255, 255, 0.98);
  border-left: 1px solid var(--line);
  box-shadow: -18px 0 40px rgba(15, 23, 42, 0.12);
  transform: translateX(100%);
  transition: transform 0.24s ease;
  z-index: 80;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  gap: 12px;
}

.map-panel.open {
  transform: translateX(0);
}

body.sidebar-collapsed .map-panel {
  left: 20px;
}

body.image-viewer-open {
  overflow: hidden;
}

.image-viewer {
  position: fixed;
  inset: 0;
  z-index: 120;
  display: none;
}

.image-viewer.open {
  display: block;
}

.image-viewer-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.72);
}

.image-viewer-panel {
  position: relative;
  height: 100%;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
}

.image-viewer-header {
  display: flex;
  justify-content: flex-end;
  padding: 12px 12px 0;
}

.image-viewer-close {
  border: 1px solid rgba(255, 255, 255, 0.22);
  background: rgba(15, 23, 42, 0.55);
  color: #f8fafc;
  border-radius: 999px;
  padding: 8px 14px;
}

.image-viewer-close:hover {
  border-color: rgba(255, 255, 255, 0.35);
  background: rgba(15, 23, 42, 0.72);
}

.image-viewer-stage {
  position: relative;
  min-height: 0;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
  cursor: grab;
}

.image-viewer-stage.grabbing {
  cursor: grabbing;
}

.image-viewer-img {
  max-width: 94vw;
  max-height: 84vh;
  transform-origin: center center;
  user-select: none;
  -webkit-user-drag: none;
  pointer-events: none;
}

.map-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.map-panel-label {
  font-size: 13px;
  color: var(--muted);
}

.map-panel h2 {
  margin: 4px 0 0;
  font-size: 24px;
}

.map-meta {
  color: var(--muted);
  font-size: 14px;
}

.global-map-host {
  min-height: 0;
  border: 1px solid #e5edf7;
  border-radius: 18px;
  overflow: auto;
  background:
    radial-gradient(circle at top left, #f8fbff 0, #ffffff 30%, #ffffff 100%);
}

.global-map-host svg {
  width: 100%;
  min-height: 82vh;
  display: block;
}

.global-map-host foreignObject {
  overflow: visible;
}

.global-map-host foreignObject img {
  display: block;
  max-width: 260px;
  max-height: 180px;
  height: auto;
}

@media (max-width: 1100px) {
  .layout {
    grid-template-columns: 280px minmax(0, 1fr);
  }

  .map-panel {
    left: calc(20px + 280px + 20px);
  }
}

@media (max-width: 900px) {
  :root {
    --topbar-height: 58px;
  }

  .topbar {
    padding: 10px 12px;
  }

  .topbar h1 {
    font-size: 16px;
  }

  .site-icon {
    width: 22px;
    height: 22px;
  }

  .book-picker {
    min-width: 0;
    flex: 1;
  }

  .book-picker label {
    display: none;
  }

  .book-picker select {
    min-width: 0;
    width: 100%;
    padding: 7px 9px;
    font-size: 12px;
  }

  .toolbar button,
  .menu-toggle {
    font-size: 12px;
    padding: 7px 9px;
  }

  .layout {
    grid-template-columns: 1fr;
    padding: calc(var(--topbar-height) + 14px) 12px 18px;
  }

  .sidebar {
    position: fixed;
    top: var(--topbar-height);
    left: 0;
    width: min(86vw, 340px);
    height: calc(100vh - var(--topbar-height));
    max-height: none;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    z-index: 60;
    border-radius: 0 18px 18px 0;
  }

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

  .panel {
    padding: 16px;
    border-radius: 18px;
  }

  .panel h2,
  .book-title,
  .map-panel h2 {
    font-size: 18px;
  }

  .summary-row {
    padding: 12px;
  }

  .markdown-body {
    font-size: 15px;
  }

  .map-panel {
    left: 0;
    width: 100vw;
    padding: 14px 12px 14px;
  }

  .global-map-host svg {
    min-height: 74vh;
  }
}
