/* ===== 基础变量 ===== */
:root {
  --radius: 12px;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.08);
  --transition: 0.25s ease;
  --header-h: 64px;
}

.theme-light {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f5;
  --text: #1f2329;
  --text-2: #5c6370;
  --text-3: #8a93a1;
  --border: #e4e6eb;
  --primary: #3b6de8;
  --primary-hover: #2551c9;
  --danger: #e54d6d;
  --reader-bg: #ffffff;
}

.theme-sepia {
  --bg: #e8e0d4;
  --surface: #f4ecd8;
  --surface-2: #e9dfc7;
  --text: #433422;
  --text-2: #6b5d4d;
  --text-3: #9a8b78;
  --border: #d9cbb5;
  --primary: #8c6b3d;
  --primary-hover: #6b502d;
  --danger: #b84a4a;
  --reader-bg: #f4ecd8;
}

.theme-dark {
  --bg: #121418;
  --surface: #1c1f26;
  --surface-2: #252a33;
  --text: #e6e8ec;
  --text-2: #a0a8b6;
  --text-3: #6d7789;
  --border: #2f3541;
  --primary: #6b8cff;
  --primary-hover: #8aa6ff;
  --danger: #ff6b81;
  --reader-bg: #1c1f26;
}

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

html,
body {
  height: 100%;
  font-family: 'Noto Sans SC', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  color: var(--text);
  transition: background var(--transition), color var(--transition);
}

button,
input,
select {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  border: none;
  background: none;
}

/* ===== 通用组件 ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  transition: all var(--transition);
  background: var(--surface-2);
}

.btn:hover {
  background: var(--border);
}

.btn.primary {
  background: var(--primary);
  color: #fff;
}

.btn.primary:hover {
  background: var(--primary-hover);
}

.btn.icon {
  width: 40px;
  height: 40px;
  padding: 0;
}

.btn.small {
  padding: 6px 12px;
  font-size: 13px;
}

.btn svg {
  width: 18px;
  height: 18px;
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 4px 10px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 500;
  background: var(--surface-2);
  color: var(--text-2);
}

.hidden {
  display: none !important;
}

/* ===== 视图切换 ===== */
.view {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  opacity: 0;
  pointer-events: none;
  transform: translateY(12px);
  transition: opacity var(--transition), transform var(--transition);
}

.view.active {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

/* ===== 书库视图 ===== */
.app-header {
  height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 32px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

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

.brand .logo {
  width: 32px;
  height: 32px;
  color: var(--primary);
}

.brand h1 {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.3px;
}

.library-main {
  flex: 1;
  overflow-y: auto;
  padding: 32px;
  max-width: 1400px;
  width: 100%;
  margin: 0 auto;
}

.drop-zone {
  position: relative;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 48px;
  text-align: center;
  transition: all var(--transition);
  margin-bottom: 32px;
}

.drop-zone.dragover,
.drop-zone:hover {
  border-color: var(--primary);
  background: var(--surface-2);
}

.drop-zone input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
  width: 100%;
  height: 100%;
}

.drop-hint svg {
  width: 48px;
  height: 48px;
  color: var(--text-3);
  margin-bottom: 12px;
}

.drop-hint p {
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 6px;
}

.drop-hint .sub {
  font-size: 13px;
  color: var(--text-3);
}

.section-title {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 20px;
}

.section-title h2 {
  font-size: 18px;
  font-weight: 600;
}

.book-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 24px;
}

.book-card {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  transition: transform var(--transition), box-shadow var(--transition);
}

.book-card:hover {
  transform: translateY(-4px);
  box-shadow: var(--shadow);
}

.book-cover {
  height: 160px;
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-hover) 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  color: #fff;
}

.book-cover span {
  font-size: 18px;
  font-weight: 600;
  text-align: center;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  word-break: break-all;
}

.book-info {
  padding: 16px;
}

.book-info h3 {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.book-info p {
  font-size: 12px;
  color: var(--text-3);
  margin-bottom: 12px;
}

.book-actions {
  display: flex;
  gap: 8px;
}

.book-actions button {
  flex: 1;
  padding: 8px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  background: var(--surface-2);
}

.book-actions button:hover {
  background: var(--border);
}

.book-actions .btn-delete {
  color: var(--danger);
}

.empty-state {
  text-align: center;
  padding: 80px 20px;
  color: var(--text-2);
}

.empty-state p {
  font-size: 16px;
  margin-bottom: 8px;
}

.empty-state span {
  font-size: 13px;
  color: var(--text-3);
}

/* ===== 阅读视图 ===== */
.reader-header {
  height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  gap: 16px;
}

.reader-header h2 {
  flex: 1;
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
}

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

.reader-body {
  flex: 1;
  display: flex;
  overflow: hidden;
}

.toc-panel {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: 280px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform var(--transition);
  z-index: 80;
}

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

.toc-panel h3 {
  padding: 20px;
  font-size: 15px;
  font-weight: 600;
  border-bottom: 1px solid var(--border);
}

.toc-list {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
}

.toc-item {
  width: 100%;
  text-align: left;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 14px;
  color: var(--text-2);
  margin-bottom: 4px;
  transition: background var(--transition);
}

.toc-item:hover,
.toc-item.active {
  background: var(--surface-2);
  color: var(--text);
}

.reader-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--reader-bg);
  position: relative;
}

.search-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.search-bar input {
  flex: 1;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--reader-bg);
  outline: none;
}

.search-bar input:focus {
  border-color: var(--primary);
}

.reader-content {
  flex: 1;
  overflow-y: auto;
  padding: 40px var(--reader-margin, 24px);
  font-size: var(--reader-font-size, 18px);
  line-height: var(--reader-line-height, 1.8);
  font-family: var(--reader-font, 'Noto Serif SC', serif);
  color: var(--text);
  scroll-behavior: smooth;
}

.reader-content p {
  margin-bottom: 1em;
  text-align: justify;
  text-indent: 2em;
}

.reader-content h3 {
  font-size: 1.4em;
  font-weight: 700;
  margin: 1.5em 0 0.8em;
  color: var(--text);
}

.reader-content .search-highlight {
  background: rgba(59, 109, 232, 0.3);
  border-radius: 2px;
}

.reader-content .search-highlight.active {
  background: var(--primary);
  color: #fff;
}

.reader-progress {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.progress-track {
  flex: 1;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  width: 0%;
  background: var(--primary);
  transition: width 0.1s linear;
}

.progress-text {
  font-size: 12px;
  color: var(--text-3);
  min-width: 40px;
  text-align: right;
}

/* ===== 设置面板 ===== */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(2px);
  z-index: 90;
}

.settings-panel {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.96);
  width: 92%;
  max-width: 420px;
  max-height: 84vh;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 100;
  display: flex;
  flex-direction: column;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--transition), transform var(--transition);
}

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

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

.settings-header h3 {
  font-size: 16px;
  font-weight: 600;
}

.settings-body {
  padding: 20px;
  overflow-y: auto;
}

.setting-row {
  margin-bottom: 24px;
}

.setting-row:last-child {
  margin-bottom: 0;
}

.setting-row label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-2);
  margin-bottom: 10px;
}

.theme-options {
  display: flex;
  gap: 10px;
}

.theme-btn {
  flex: 1;
  padding: 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  font-size: 13px;
  transition: all var(--transition);
}

.theme-btn.active,
.theme-btn:hover {
  border-color: var(--primary);
  color: var(--primary);
}

.setting-row select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  outline: none;
}

.setting-row input[type='range'] {
  width: 100%;
  accent-color: var(--primary);
}

/* ===== Toast ===== */
.toast {
  position: fixed;
  bottom: 28px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  padding: 12px 24px;
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--transition), transform var(--transition);
  z-index: 200;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ===== 响应式 ===== */
@media (max-width: 768px) {
  .app-header,
  .library-main,
  .reader-header {
    padding-left: 16px;
    padding-right: 16px;
  }

  .library-main {
    padding-top: 20px;
  }

  .drop-zone {
    padding: 32px 20px;
  }

  .book-grid {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 16px;
  }

  .reader-content {
    padding-top: 24px;
    padding-bottom: 24px;
  }
}
