:root {
  --bg: #f8fafc;
  --panel: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --accent: #2563eb;
  --accent-dark: #1d4ed8;
  --ai: #0f766e;
  --warn: #b45309;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.06);
  --topbar-h: 56px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif;
  font-size: 15px;
  line-height: 1.6;
  letter-spacing: 0;
}

button,
input,
textarea,
select {
  font: inherit;
  letter-spacing: 0;
}

.hidden {
  display: none !important;
}

.topbar {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 18px;
  background: #0f172a;
  color: #f8fafc;
  position: relative;
  z-index: 20;
}

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

.brand-mark {
  width: 30px;
  height: 30px;
  display: grid;
  place-items: center;
  background: var(--accent);
  color: #fff;
  border-radius: 8px;
  font-weight: 700;
}

.brand-name {
  font-weight: 600;
}

.sync-status {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #94a3b8;
}

.sync-status.on {
  background: #22c55e;
}

.sync-status.off {
  background: #ef4444;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--ink);
  cursor: pointer;
  white-space: nowrap;
  min-height: 36px;
}

.btn:hover {
  border-color: var(--accent);
  color: var(--accent-dark);
}

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

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

.btn.ghost {
  background: transparent;
  border-color: transparent;
}

.topbar .btn.ghost {
  color: #e2e8f0;
}

.topbar .btn.ghost:hover {
  color: #fff;
  border-color: #475569;
}

.btn.small {
  min-height: 30px;
  padding: 4px 10px;
  font-size: 13px;
}

.view {
  height: calc(100vh - var(--topbar-h));
  overflow: auto;
}

.library-view {
  padding: 28px 20px 60px;
}

.library-head {
  max-width: 1080px;
  margin: 0 auto 22px;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 20px;
  flex-wrap: wrap;
}

.library-head h1 {
  margin: 0 0 6px;
  font-size: 26px;
  line-height: 1.25;
}

.library-head p {
  margin: 0;
  color: var(--muted);
}

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

.book-grid {
  max-width: 1080px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 14px;
}

.book-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  cursor: pointer;
  min-height: 132px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  box-shadow: var(--shadow);
}

.book-card:hover {
  border-color: var(--accent);
}

.book-card h3 {
  margin: 0;
  font-size: 16px;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.book-card .book-meta {
  color: var(--muted);
  font-size: 13px;
}

.book-card .book-progress {
  margin-top: auto;
  font-size: 13px;
  color: var(--accent-dark);
}

.empty-hint {
  max-width: 1080px;
  margin: 40px auto;
  text-align: center;
  color: var(--muted);
}

.reader-view {
  display: grid;
  grid-template-columns: 236px minmax(0, 1fr) 360px;
  height: calc(100vh - var(--topbar-h));
  overflow: hidden;
  background: var(--panel);
}

.chapter-panel {
  border-right: 1px solid var(--line);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.panel-head {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.panel-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.panel-title strong {
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chapter-list {
  overflow: auto;
  padding: 8px;
  flex: 1;
}

.chapter-item {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  color: var(--ink);
  padding: 9px 10px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 14px;
  line-height: 1.4;
  border-left: 3px solid transparent;
}

.chapter-item:hover {
  background: #f1f5f9;
}

.chapter-item.active {
  background: #eff6ff;
  border-left-color: var(--accent);
  color: var(--accent-dark);
  font-weight: 600;
}

.chapter-item .chapter-summary {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 400;
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.reader-main {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.reader-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}

.chapter-title {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.reader-tools {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.book-view {
  flex: 1;
  overflow: auto;
  padding: 28px 34px 40px;
  background: #fdfdff;
}

.book-view iframe {
  border: 0;
  width: 100%;
  height: 100%;
}

.book-view .txt-page {
  max-width: 720px;
  margin: 0 auto;
  white-space: pre-wrap;
  font-size: 17px;
  line-height: 1.9;
}

.book-view .reading-page {
  max-width: 720px;
  margin: 0 auto;
  font-size: 17px;
  line-height: 1.9;
}

.reading-title {
  margin: 0 0 18px;
  font-size: 24px;
  line-height: 1.4;
}

.reading-body p {
  margin: 0 0 14px;
  text-align: justify;
}

.selection-bar {
  position: absolute;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  max-width: min(620px, calc(100% - 24px));
  background: #0f172a;
  color: #f8fafc;
  border-radius: var(--radius);
  padding: 10px 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  z-index: 10;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.25);
}

.selection-text {
  flex: 1;
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: #cbd5e1;
}

.selection-bar .btn {
  min-height: 30px;
  padding: 4px 10px;
  font-size: 13px;
  border-color: #475569;
  background: #1e293b;
  color: #f8fafc;
}

.side-panel {
  border-left: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
}

.tabs {
  display: flex;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}

.tab {
  flex: 1;
  border: 0;
  background: transparent;
  padding: 11px 8px;
  cursor: pointer;
  color: var(--muted);
  border-bottom: 2px solid transparent;
}

.tab.active {
  color: var(--accent-dark);
  border-bottom-color: var(--accent);
  font-weight: 600;
}

.tab-panel {
  display: none;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.tab-panel.active {
  display: flex;
  flex-direction: column;
}

.chat-messages {
  flex: 1;
  overflow: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.msg {
  max-width: 88%;
  padding: 9px 12px;
  border-radius: var(--radius);
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 14px;
}

.msg.user {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
}

.msg.ai {
  align-self: flex-start;
  background: #f1f5f9;
  border: 1px solid var(--line);
}

.msg .msg-mode {
  display: block;
  font-size: 11px;
  color: var(--muted);
  margin-bottom: 3px;
}

.chat-form {
  border-top: 1px solid var(--line);
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--panel);
}

.chat-mode,
.chat-form textarea,
.chat-form input {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 10px;
  width: 100%;
  background: #fff;
  color: var(--ink);
}

.chat-form textarea {
  resize: vertical;
  min-height: 48px;
  max-height: 140px;
}

.quiz-body,
.notes-body {
  flex: 1;
  overflow: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.quiz-intro {
  color: var(--muted);
  font-size: 13px;
}

.question {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
  background: #fff;
}

.question h4 {
  margin: 0 0 8px;
  font-size: 14px;
  line-height: 1.5;
}

.question .options {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.question label.option {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  font-size: 14px;
}

.question textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px;
  min-height: 64px;
  resize: vertical;
}

.quiz-result {
  border: 1px solid #cbd5e1;
  border-radius: var(--radius);
  padding: 12px;
  background: #f8fafc;
}

.quiz-result h4 {
  margin: 0 0 6px;
  font-size: 14px;
}

.quiz-result .verdict {
  font-weight: 600;
  color: var(--accent-dark);
}

.note-item {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px;
  background: #fff;
}

.note-item blockquote {
  margin: 0 0 6px;
  padding: 6px 10px;
  border-left: 3px solid var(--warn);
  background: #fffbeb;
  color: #78350f;
  font-size: 13px;
}

.note-item .note-ai {
  margin-top: 6px;
  padding: 8px 10px;
  background: #f0fdfa;
  border-left: 3px solid var(--ai);
  color: #134e4a;
  font-size: 13px;
  white-space: pre-wrap;
}

.note-item .note-empty {
  color: var(--muted);
  font-size: 13px;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.5);
  display: grid;
  place-items: center;
  z-index: 50;
  padding: 20px;
}

.modal-box {
  width: min(460px, 100%);
  background: var(--panel);
  border-radius: var(--radius);
  padding: 22px;
  box-shadow: 0 20px 60px rgba(15, 23, 42, 0.3);
}

.modal-box h2 {
  margin: 0 0 16px;
  font-size: 18px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 14px;
}

.field span {
  font-size: 13px;
  color: var(--muted);
}

.field-hint {
  margin: -8px 0 14px;
  font-size: 12px;
  color: var(--warn);
}

.field input {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 9px 10px;
  width: 100%;
}

.model-row {
  display: flex;
  gap: 8px;
  align-items: stretch;
}

.model-select {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 9px 10px;
  background: #fff;
  color: var(--ink);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 6px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: #0f172a;
  color: #f8fafc;
  padding: 10px 16px;
  border-radius: var(--radius);
  font-size: 14px;
  z-index: 60;
  max-width: min(480px, calc(100% - 24px));
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.3);
}

@media (max-width: 1100px) {
  .reader-view {
    grid-template-columns: 200px minmax(0, 1fr) 320px;
  }
}

@media (max-width: 860px) {
  .reader-view {
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  .chapter-panel {
    border-right: 0;
    border-bottom: 1px solid var(--line);
    max-height: 132px;
  }

  .panel-head {
    flex-direction: row;
    align-items: center;
    padding: 8px 10px;
  }

  .panel-title {
    flex: 1;
  }

  .chapter-list {
    display: flex;
    gap: 6px;
    padding: 8px 10px;
    overflow-x: auto;
  }

  .chapter-item {
    flex: 0 0 auto;
    width: auto;
    max-width: 180px;
    border-left: 0;
    border-bottom: 3px solid transparent;
    padding: 7px 10px;
    background: #f8fafc;
    border: 1px solid var(--line);
    border-radius: var(--radius);
  }

  .chapter-item.active {
    border-bottom-color: var(--accent);
  }

  .chapter-item .chapter-summary {
    display: none;
  }

  .reader-main {
    flex: 1;
    min-height: 0;
  }

  .reader-toolbar {
    padding: 8px 12px;
  }

  .book-view {
    padding: 18px 16px 70px;
  }

  .side-panel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    height: min(62vh, 560px);
    border-left: 0;
    border-top: 1px solid var(--line);
    z-index: 30;
    transform: translateY(105%);
    transition: transform 0.18s ease;
    background: var(--panel);
  }

  .side-panel.open {
    transform: translateY(0);
  }

  .library-head {
    flex-direction: column;
    align-items: stretch;
  }

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

@media (max-width: 480px) {
  .brand-name {
    display: none;
  }

  .book-grid {
    grid-template-columns: 1fr 1fr;
    gap: 10px;
  }

  .book-card {
    padding: 12px;
    min-height: 110px;
  }

  .reader-tools .btn {
    padding: 6px 8px;
    font-size: 13px;
  }
}
