:root {
  --bg-primary: #0f1117;
  --bg-secondary: #161922;
  --bg-tertiary: #1e2230;
  --bg-hover: #262b3d;
  --text-primary: #f1f3f9;
  --text-secondary: #a6b0cf;
  --text-muted: #6c7693;
  --border-color: #282d3f;
  --accent-primary: #d4a359;
  --accent-secondary: #c94a4a;
  --accent-glow: rgba(212, 163, 89, 0.16);
  --badge-bg: rgba(212, 163, 89, 0.15);
  --badge-text: #eab86e;
  --canvas-bg: radial-gradient(circle at 50% 50%, #171a26 0%, #0d0f15 100%);
  --canvas-grid: rgba(255, 255, 255, 0.035);
  --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.45);
}

html[data-theme="light"] {
  --bg-primary: #f7f4eb;
  --bg-secondary: #efeadd;
  --bg-tertiary: #e5decb;
  --bg-hover: #dcd3bc;
  --text-primary: #231f1b;
  --text-secondary: #544b41;
  --text-muted: #827668;
  --border-color: #d6cbb5;
  --accent-primary: #8b1e24;
  --accent-secondary: #b06d1b;
  --accent-glow: rgba(139, 30, 36, 0.12);
  --badge-bg: rgba(139, 30, 36, 0.1);
  --badge-text: #8b1e24;
  --canvas-bg: radial-gradient(circle at 50% 50%, #fbf9f2 0%, #f1ece1 100%);
  --canvas-grid: rgba(40, 30, 20, 0.045);
  --shadow-lg: 0 10px 28px rgba(60, 45, 30, 0.12);
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Serif SC", sans-serif;
  background-color: var(--bg-primary);
  color: var(--text-primary);
  overflow: hidden;
  height: 100vh;
  width: 100vw;
  -webkit-font-smoothing: antialiased;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
}

.app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100vw;
}

/* Topbar */
.topbar {
  height: 58px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  z-index: 20;
  gap: 12px;
}

.topbar-left, .topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

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

.brand-badge {
  background: linear-gradient(135deg, #8b1e24, #b8373f);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.8px;
  padding: 3px 7px;
  border-radius: 4px;
}

.brand-title {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: 0.3px;
  white-space: nowrap;
}

.book-selector {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 5px 10px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  outline: none;
  transition: border-color 0.2s;
}

.book-selector:hover, .book-selector:focus {
  border-color: var(--accent-primary);
}

.view-tabs {
  display: flex;
  background: var(--bg-tertiary);
  padding: 3px;
  border-radius: 8px;
  border: 1px solid var(--border-color);
  gap: 2px;
}

.tab-btn {
  background: transparent;
  color: var(--text-secondary);
  border: none;
  padding: 5px 12px;
  font-size: 13px;
  font-weight: 500;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.18s;
  white-space: nowrap;
}

.tab-btn:hover {
  color: var(--text-primary);
}

.tab-btn.active {
  background: var(--accent-primary);
  color: #fff;
  font-weight: 600;
}

html[data-theme="dark"] .tab-btn.active {
  color: #121318;
}

.icon-btn {
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  width: 34px;
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.18s;
}

.icon-btn:hover {
  color: var(--text-primary);
  border-color: var(--accent-primary);
  background: var(--bg-hover);
}

.pill-btn {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  height: 34px;
  padding: 0 12px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.18s;
}

.pill-btn:hover, .pill-btn.active {
  border-color: var(--accent-primary);
  background: var(--accent-glow);
  color: var(--badge-text);
}

/* Main Layout */
.main-layout {
  display: flex;
  flex: 1;
  overflow: hidden;
  position: relative;
}

/* Sidebar */
.sidebar {
  width: 350px;
  min-width: 350px;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  transition: margin-left 0.25s ease;
  z-index: 15;
}

.sidebar.collapsed {
  margin-left: -350px;
}

.book-meta-card {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border-color);
  background: linear-gradient(180deg, var(--accent-glow) 0%, transparent 100%);
}

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

.book-meta-header h2 {
  font-size: 15px;
  font-weight: 700;
  color: var(--badge-text);
}

.book-author {
  font-size: 12px;
  color: var(--text-muted);
}

.book-desc {
  font-size: 12px;
  color: var(--text-secondary);
  line-height: 1.5;
  margin-bottom: 8px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.book-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.stat-chip {
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 4px;
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  border: 1px solid var(--border-color);
}

/* Search & Character Filter */
.search-panel {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.search-input-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

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

#chapterSearch {
  width: 100%;
  background: var(--bg-primary);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 7px 28px 7px 32px;
  font-size: 12.5px;
  outline: none;
  transition: border-color 0.2s;
}

#chapterSearch:focus {
  border-color: var(--accent-primary);
}

.clear-btn {
  position: absolute;
  right: 8px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 16px;
  cursor: pointer;
}

.clear-btn.hidden {
  display: none;
}

.char-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.char-tag {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 12px;
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  border: 1px solid var(--border-color);
  cursor: pointer;
  transition: all 0.15s;
}

.char-tag:hover {
  border-color: var(--accent-primary);
  color: var(--text-primary);
}

.char-tag.active {
  background: var(--accent-primary);
  color: #121318;
  border-color: var(--accent-primary);
  font-weight: 600;
}

html[data-theme="light"] .char-tag.active {
  color: #fff;
}

/* Navigation Lists */
.nav-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 10px 12px 24px;
}

.nav-scroll::-webkit-scrollbar {
  width: 6px;
}

.nav-scroll::-webkit-scrollbar-thumb {
  background: var(--border-color);
  border-radius: 3px;
}

.nav-section {
  margin-bottom: 16px;
}

.nav-section-title {
  font-size: 11.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-muted);
  margin-bottom: 6px;
  padding: 0 4px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.count-badge {
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  padding: 1px 6px;
  border-radius: 10px;
  font-size: 11px;
}

.nav-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.nav-item {
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 8px 10px;
  border-radius: 7px;
  border: 1px solid transparent;
  background: transparent;
  cursor: pointer;
  text-align: left;
  transition: all 0.15s;
}

.nav-item:hover {
  background: var(--bg-hover);
}

.nav-item.active {
  background: var(--accent-glow);
  border-color: var(--accent-primary);
}

.nav-item-top {
  display: flex;
  align-items: center;
  gap: 7px;
}

.nav-item-num {
  font-size: 11px;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 4px;
  background: var(--bg-tertiary);
  color: var(--badge-text);
  white-space: nowrap;
}

.nav-item-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
  line-height: 1.35;
}

.nav-item-sub {
  font-size: 11.5px;
  color: var(--text-muted);
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* Center Stage */
.canvas-stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--canvas-bg);
  position: relative;
  overflow: hidden;
}

.stage-header {
  height: 50px;
  padding: 0 18px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  z-index: 10;
}

.stage-title-group {
  display: flex;
  align-items: center;
  gap: 10px;
  overflow: hidden;
}

.stage-badge {
  background: var(--badge-bg);
  color: var(--badge-text);
  border: 1px solid var(--accent-primary);
  font-size: 11.5px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 4px;
  white-space: nowrap;
}

.stage-title {
  font-size: 15px;
  font-weight: 700;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.stage-nav-btns {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.nav-step-btn {
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  border: 1px solid var(--border-color);
  border-radius: 5px;
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.15s;
}

.nav-step-btn:hover:not(:disabled) {
  border-color: var(--accent-primary);
  color: var(--text-primary);
}

.nav-step-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* Mindmap SVG Wrapper */
.mindmap-wrapper {
  flex: 1;
  position: relative;
  width: 100%;
  height: 100%;
  background-image: radial-gradient(var(--canvas-grid) 1px, transparent 1px);
  background-size: 22px 22px;
}

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

/* Markmap custom text color for dark/light themes */
.markmap-svg {
  color: var(--text-primary);
}

.markmap-foreign {
  font-size: 14px;
  line-height: 1.45;
}

.markmap-foreign strong {
  color: var(--badge-text);
  font-weight: 700;
}

/* Floating Controls */
.canvas-controls {
  position: absolute;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 5px 8px;
  display: flex;
  align-items: center;
  gap: 5px;
  box-shadow: var(--shadow-lg);
  z-index: 12;
}

.ctrl-btn {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border: 1px solid transparent;
  border-radius: 5px;
  padding: 4px 10px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
}

.ctrl-btn:hover {
  border-color: var(--accent-primary);
  background: var(--accent-glow);
  color: var(--badge-text);
}

.ctrl-divider {
  width: 1px;
  height: 18px;
  background: var(--border-color);
  margin: 0 2px;
}

/* Right Outline Drawer */
.outline-drawer {
  width: 360px;
  min-width: 360px;
  background: var(--bg-secondary);
  border-left: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  transition: margin-right 0.25s ease;
  z-index: 15;
}

.outline-drawer.collapsed {
  margin-right: -360px;
}

.drawer-header {
  height: 50px;
  padding: 0 16px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.drawer-header h3 {
  font-size: 14px;
  font-weight: 700;
  color: var(--badge-text);
}

.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.summary-card {
  background: var(--bg-tertiary);
  border-left: 3px solid var(--accent-primary);
  padding: 12px 14px;
  border-radius: 6px;
  font-size: 13px;
  line-height: 1.6;
  color: var(--text-primary);
}

.chars-box {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chars-box:empty {
  display: none;
}

.char-pill {
  font-size: 11.5px;
  padding: 3px 9px;
  border-radius: 12px;
  background: var(--badge-bg);
  color: var(--badge-text);
  border: 1px solid var(--border-color);
  cursor: pointer;
}

.char-pill:hover {
  border-color: var(--accent-primary);
}

.markdown-preview {
  font-size: 13px;
  line-height: 1.65;
  color: var(--text-secondary);
}

.markdown-preview h1 {
  font-size: 15px;
  color: var(--text-primary);
  margin-bottom: 10px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border-color);
}

.markdown-preview h2 {
  font-size: 13.5px;
  color: var(--badge-text);
  margin: 12px 0 6px;
}

.markdown-preview h3 {
  font-size: 13px;
  color: var(--text-primary);
  margin: 8px 0 4px;
}

.markdown-preview ul {
  padding-left: 18px;
  margin-bottom: 8px;
}

.markdown-preview li {
  margin-bottom: 4px;
}

.markdown-preview strong {
  color: var(--text-primary);
}

/* Backdrop for off-canvas panels (mobile) */
.backdrop {
  display: none;
}

/* ========== Tablet (≤1100px) ========== */
@media (max-width: 1100px) {
  .topbar {
    flex-wrap: wrap;
    height: auto;
    padding: 8px 12px 0;
    row-gap: 6px;
  }
  .topbar-center {
    order: 3;
    width: 100%;
    overflow-x: auto;
    scrollbar-width: none;
    padding-bottom: 8px;
    -webkit-overflow-scrolling: touch;
  }
  .topbar-center::-webkit-scrollbar {
    display: none;
  }
  .view-tabs {
    width: max-content;
  }
  .outline-drawer {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    box-shadow: var(--shadow-lg);
  }
}

/* ========== Mobile (≤768px) ========== */
@media (max-width: 768px) {
  body,
  .app-shell {
    height: 100dvh;
  }

  .topbar {
    padding: calc(6px + env(safe-area-inset-top)) 10px 0;
    gap: 8px;
  }
  .topbar-left {
    flex: 1;
    min-width: 0;
    gap: 8px;
  }
  .topbar-right {
    gap: 6px;
  }
  .brand-title,
  #exportMdBtn {
    display: none;
  }
  .book-switcher-wrap {
    flex: 1;
    min-width: 0;
  }
  .book-selector {
    width: 100%;
    text-overflow: ellipsis;
    font-size: 13px;
    padding: 6px 8px;
  }
  .icon-btn {
    width: 36px;
    height: 36px;
    flex-shrink: 0;
  }
  .pill-btn {
    height: 36px;
    padding: 0 10px;
  }
  .pill-btn span {
    display: none;
  }
  .tab-btn {
    padding: 6px 11px;
    font-size: 12.5px;
  }

  /* Left sidebar → off-canvas drawer */
  .sidebar {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 86vw;
    min-width: 0;
    max-width: 360px;
    box-shadow: var(--shadow-lg);
    z-index: 40;
  }
  .sidebar.collapsed {
    margin-left: -100vw;
  }
  .nav-item {
    padding: 10px 12px;
  }
  .char-tags {
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
    padding-bottom: 2px;
  }
  .char-tags::-webkit-scrollbar {
    display: none;
  }
  .char-tag {
    flex-shrink: 0;
    padding: 4px 10px;
    font-size: 12px;
  }
  #chapterSearch {
    font-size: 16px; /* 防止 iOS 聚焦时自动放大 */
  }

  /* Right outline drawer → bottom sheet */
  .outline-drawer {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    min-width: 0;
    height: 62dvh;
    border-left: none;
    border-top: 1px solid var(--border-color);
    border-radius: 14px 14px 0 0;
    transition: transform 0.25s ease;
    z-index: 40;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .outline-drawer.collapsed {
    margin-right: 0;
    transform: translateY(105%);
  }
  .drawer-header {
    height: 46px;
    position: relative;
  }
  .drawer-header::before {
    content: "";
    position: absolute;
    top: 6px;
    left: 50%;
    transform: translateX(-50%);
    width: 38px;
    height: 4px;
    border-radius: 2px;
    background: var(--border-color);
  }
  .drawer-body {
    padding: 12px 14px 20px;
  }

  .backdrop {
    display: block;
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s;
    z-index: 30;
  }
  .backdrop.show {
    opacity: 1;
    pointer-events: auto;
  }

  /* Stage header */
  .stage-header {
    height: auto;
    min-height: 44px;
    padding: 6px 10px;
    gap: 8px;
  }
  .stage-title-group {
    gap: 6px;
    min-width: 0;
  }
  .stage-badge {
    font-size: 10.5px;
    padding: 2px 6px;
    max-width: 38vw;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .stage-title {
    font-size: 13.5px;
  }
  .nav-step-btn {
    width: 34px;
    height: 32px;
    padding: 0;
    font-size: 15px;
  }
  .nav-step-btn .btn-label {
    display: none;
  }

  /* Mindmap */
  .markmap-foreign {
    font-size: 13px;
  }
  .canvas-controls {
    left: 8px;
    right: 8px;
    transform: none;
    bottom: calc(10px + env(safe-area-inset-bottom));
    overflow-x: auto;
    scrollbar-width: none;
    justify-content: flex-start;
    padding: 5px 6px;
  }
  .canvas-controls::-webkit-scrollbar {
    display: none;
  }
  .ctrl-btn {
    flex-shrink: 0;
    padding: 7px 11px;
    font-size: 12.5px;
  }
}
