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

body {
  font-family: var(--font-family);
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
}

#app {
  max-width: 800px;
  margin: 0 auto;
  min-height: 100vh;
}

.view { display: block; }
.view.hidden { display: none !important; }

/* ====== Topbar ====== */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-primary);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar h1 { font-size: 1.1em; font-weight: 500; }

.back-btn {
  background: none; border: none; color: var(--accent-blue);
  font-size: 0.95em; cursor: pointer; font-family: inherit;
}
.back-btn:hover { opacity: 0.8; }

.icon-btn {
  background: none; border: 1px solid var(--border-primary);
  color: var(--text-primary); cursor: pointer;
  width: 32px; height: 32px; border-radius: var(--radius-md);
  font-size: 1.1em; display: flex; align-items: center; justify-content: center;
  font-family: inherit;
}
.icon-btn:hover { background: var(--bg-hover); }

/* ====== Search Bar ====== */
.search-bar {
  display: flex; gap: 8px; padding: 16px;
}
.search-bar input {
  flex: 1; padding: 10px 16px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-lg);
  color: var(--text-primary);
  font-size: 0.95em; font-family: inherit;
  outline: none;
}
.search-bar input:focus { border-color: var(--accent-blue); }
.search-bar button {
  padding: 10px 20px;
  background: var(--gradient-btn-primary);
  border: none; border-radius: var(--radius-lg);
  color: var(--text-on-accent); font-size: 0.95em;
  cursor: pointer; font-family: inherit; font-weight: 500;
}
.search-bar button:hover { opacity: 0.9; }

/* ====== Shelf Grid ====== */
.shelf-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 16px;
  padding: 0 16px 24px;
}
.empty-state {
  grid-column: 1 / -1;
  text-align: center; padding: 60px 0;
  color: var(--text-muted); font-size: 0.95em;
}
.empty-state p:first-child { font-size: 3em; margin-bottom: 12px; }

.book-card {
  position: relative;
  background: var(--bg-secondary);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-lg);
  overflow: hidden; cursor: pointer;
  transition: all var(--transition-normal);
}
.book-card:hover {
  transform: translateY(-2px);
  border-color: var(--accent-blue);
  box-shadow: var(--shadow-card-hover);
}
.book-card .card-cover {
  width: 100%; height: 180px; object-fit: cover;
  background: var(--bg-tertiary);
  display: flex; align-items: center; justify-content: center;
  font-size: 3em; color: var(--text-muted);
}
.book-card .card-body { padding: 12px 28px 12px 12px; }
.card-delete-btn {
  position: absolute; top: 4px; right: 4px;
  width: 24px; height: 24px; border-radius: 50%;
  background: rgba(0,0,0,0.45); color: #fff; border: none;
  font-size: 14px; line-height: 1; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  opacity: 0; transition: opacity 0.2s;
  z-index: 2;
}
.book-card:hover .card-delete-btn { opacity: 1; }
.card-delete-btn:hover { background: rgba(220,38,38,0.9); }
.book-card .card-title {
  font-size: 0.9em; font-weight: 500;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.book-card .card-meta {
  font-size: 0.75em; color: var(--text-secondary); margin-top: 4px;
}
.book-card .card-progress {
  margin-top: 8px; height: 3px; background: var(--bg-tertiary);
  border-radius: 2px; overflow: hidden;
}
.book-card .card-progress .fill {
  height: 100%; background: var(--accent-blue); border-radius: 2px;
}

/* ====== Search Results ====== */
.search-status {
  padding: 0 16px; font-size: 0.85em; color: var(--text-secondary);
}
.search-results { padding: 0 16px 16px; }
.search-group { margin-bottom: 20px; }
.search-group h4 {
  font-size: 0.8em; color: var(--text-muted); margin-bottom: 10px;
  text-transform: uppercase; letter-spacing: 1px;
}

.search-item {
  display: flex; gap: 12px; padding: 12px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-lg);
  margin-bottom: 8px; cursor: pointer;
  transition: all var(--transition-normal);
}
.search-item:hover { border-color: var(--accent-blue); }
.search-item .si-cover {
  width: 52px; height: 68px; border-radius: var(--radius-sm);
  object-fit: cover; background: var(--bg-tertiary); flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.8em;
}
.search-item .si-info { flex: 1; min-width: 0; }
.search-item .si-title { font-size: 0.95em; font-weight: 500; }
.search-item .si-meta {
  font-size: 0.8em; color: var(--text-secondary); margin-top: 4px;
}
.search-item .si-source {
  font-size: 0.78em; color: var(--accent-blue); margin-top: 3px;
}
.search-item .si-intro {
  font-size: 0.75em; color: var(--text-muted); margin-top: 4px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.search-item .si-btn {
  align-self: center; flex-shrink: 0;
  padding: 6px 14px;
  background: var(--accent-blue); color: #fff;
  border: none; border-radius: var(--radius-md);
  font-size: 0.8em; cursor: pointer; font-family: inherit;
}

/* ====== Book Detail ====== */
.book-info { padding: 24px 16px; text-align: center; }
.book-cover {
  width: 120px; height: 160px; border-radius: var(--radius-lg);
  object-fit: cover; margin-bottom: 16px;
  background: var(--bg-tertiary); box-shadow: var(--shadow-card);
}
#detail-title { font-size: 1.3em; margin-bottom: 6px; }
.detail-meta { font-size: 0.85em; color: var(--text-secondary); margin-bottom: 12px; }
.detail-intro {
  font-size: 0.85em; color: var(--text-secondary); line-height: 1.7;
  max-height: 120px; overflow: hidden; text-align: left;
}

.action-bar {
  display: flex; flex-wrap: wrap; gap: 8px; padding: 0 16px 16px;
  justify-content: center;
}
.btn-primary {
  padding: 10px 24px;
  background: var(--gradient-btn-primary);
  border: none; border-radius: var(--radius-lg);
  color: var(--text-on-accent); font-size: 0.9em;
  cursor: pointer; font-family: inherit; font-weight: 500;
}
.btn-primary:hover { opacity: 0.9; }
.btn-secondary {
  padding: 8px 18px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-lg);
  color: var(--text-primary); font-size: 0.85em;
  cursor: pointer; font-family: inherit;
}
.btn-secondary:hover { background: var(--bg-hover); }
.btn-small {
  padding: 4px 12px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-md);
  color: var(--text-primary); font-size: 0.8em;
  cursor: pointer; font-family: inherit;
}
.btn-small.active { background: var(--accent-blue); color: #fff; border-color: var(--accent-blue); }

.chapter-list {
  padding: 0 16px 24px; max-height: 400px; overflow-y: auto;
}
.chapter-list .ch-item {
  display: block; padding: 10px 0;
  border-bottom: 1px solid var(--border-secondary);
  color: var(--text-primary); text-decoration: none;
  font-size: 0.9em; cursor: pointer;
}
.chapter-list .ch-item:hover { color: var(--accent-blue); }

/* ====== Download Progress ====== */
.download-progress { padding: 12px 16px; }
.progress-bar {
  height: 8px; background: var(--bg-tertiary);
  border-radius: 4px; overflow: hidden; margin-bottom: 6px;
}
.progress-fill {
  height: 100%; background: var(--accent-blue);
  border-radius: 4px; transition: width 0.3s;
  width: 0%;
}
.progress-text { font-size: 0.8em; color: var(--text-secondary); text-align: center; }

/* ====== Reader ====== */
.reader-topbar, .reader-bottombar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px;
  background: var(--bg-secondary);
  border-color: var(--border-primary);
  position: fixed; left: 0; right: 0; z-index: 20;
  max-width: 800px; margin: 0 auto;
  transition: transform 0.3s, opacity 0.3s;
}
.reader-topbar {
  top: 0; border-bottom: 1px solid var(--border-primary);
}
.reader-bottombar {
  bottom: 0; border-top: 1px solid var(--border-primary);
}
.reader-topbar.hidden { transform: translateY(-100%); opacity: 0; }
.reader-bottombar.hidden { transform: translateY(100%); opacity: 0; }

.reader-topbar .reader-title {
  font-size: 0.9em; color: var(--text-secondary);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  flex: 1; text-align: center; margin: 0 8px;
}

.reader-content {
  padding: 56px 20px 56px;
  min-height: 100vh;
  /* CSS variables set by JS for reader customization */
  font-family: var(--reader-font-family, var(--font-family));
  font-size: var(--reader-font-size, 1.05em);
  line-height: var(--reader-line-height, 1.8);
}
/* Reading themes */
.reader-content[data-reader-theme="white"] { background: #ffffff; color: #333333; }
.reader-content[data-reader-theme="white"] .reader-text { color: #333333; }
.reader-content[data-reader-theme="white"] .reader-loading { color: #666; }
.reader-content[data-reader-theme="cream"] { background: #f5ecd7; color: #4a3728; }
.reader-content[data-reader-theme="cream"] .reader-text { color: #4a3728; }
.reader-content[data-reader-theme="cream"] .reader-loading { color: #8a6d5b; }
.reader-content[data-reader-theme="green"] { background: #c8e6c9; color: #1b5e20; }
.reader-content[data-reader-theme="green"] .reader-text { color: #1b5e20; }
.reader-content[data-reader-theme="green"] .reader-loading { color: #388e3c; }
.reader-content[data-reader-theme="night"] { background: #1a1a2e; color: #d0d0e0; }
.reader-content[data-reader-theme="night"] .reader-text { color: #d0d0e0; }
.reader-content[data-reader-theme="night"] .reader-loading { color: #8888aa; }

.reader-loading {
  text-align: center; color: var(--text-muted); padding: 60px 20px;
}
.reader-text {
  word-break: break-word;
}
.reader-text p { margin-bottom: 0.8em; text-indent: 2em; }

.reader-bottombar button {
  background: var(--bg-tertiary); border: 1px solid var(--border-primary);
  color: var(--text-primary); padding: 8px 16px;
  border-radius: var(--radius-md); font-size: 0.85em;
  cursor: pointer; font-family: inherit;
}
.reader-bottombar button:hover { background: var(--bg-hover); }

/* Reader Settings */
.reader-settings {
  position: fixed; bottom: 56px; left: 0; right: 0;
  max-width: 800px; margin: 0 auto;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border-primary);
  padding: 16px 20px; z-index: 25;
  border-radius: 12px 12px 0 0;
  box-shadow: 0 -4px 20px rgba(0,0,0,0.15);
}
.settings-row {
  display: flex; align-items: center; gap: 8px; margin-bottom: 10px;
  font-size: 0.9em; flex-wrap: wrap;
}
.settings-label { width: 42px; color: var(--text-secondary); flex-shrink: 0; }
.settings-options { display: flex; gap: 4px; flex-wrap: wrap; }
.settings-options select {
  padding: 4px 8px; border-radius: var(--radius-sm);
  border: 1px solid var(--border-secondary);
  background: var(--bg-primary); color: var(--text-primary);
  font-size: 0.85em;
}
.theme-btn { width: 36px; height: 36px; padding: 0; font-size: 1.1em; }

/* TOC Overlay */
.toc-overlay {
  position: fixed; inset: 0; z-index: 30;
  background: rgba(0,0,0,0.5);
  display: flex; justify-content: flex-end;
}
.toc-panel {
  width: 300px; max-width: 80vw; height: 100%;
  background: var(--bg-primary);
  border-left: 1px solid var(--border-primary);
  display: flex; flex-direction: column;
}
.toc-panel h3 {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px; border-bottom: 1px solid var(--border-primary);
  font-size: 1em;
}
.toc-list {
  flex: 1; overflow-y: auto; padding: 8px 16px;
}
.toc-list a {
  display: block; padding: 8px 0; font-size: 0.85em;
  color: var(--text-primary); text-decoration: none;
  border-bottom: 1px solid var(--border-secondary);
}
.toc-list a:hover { color: var(--accent-blue); }
.toc-list a.current {
  color: var(--accent-blue); font-weight: 500;
}

/* ====== Web Search ====== */
.web-search-section {
  padding: 0 16px 24px; text-align: center;
}
.web-search-divider {
  display: flex; align-items: center; gap: 12px;
  margin-bottom: 16px; color: var(--text-muted); font-size: 0.85em;
}
.web-search-divider::before, .web-search-divider::after {
  content: ''; flex: 1; height: 1px;
  background: var(--border-primary);
}
.btn-web-search {
  padding: 12px 32px;
  background: var(--bg-secondary);
  border: 2px dashed var(--border-primary);
  border-radius: var(--radius-lg);
  color: var(--text-primary); font-size: 1em;
  cursor: pointer; font-family: inherit;
  transition: all var(--transition-normal);
}
.btn-web-search:hover {
  border-color: var(--accent-blue); color: var(--accent-blue);
}
.btn-web-search:disabled {
  opacity: 0.5; cursor: not-allowed;
}
.web-search-status {
  margin-top: 12px; font-size: 0.85em; color: var(--text-secondary);
}
.web-search-results {
  text-align: left; margin-top: 12px;
}
.ws-item {
  display: flex; gap: 12px; padding: 12px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-lg);
  margin-bottom: 8px;
  transition: all var(--transition-normal);
}
.ws-item:hover { border-color: var(--accent-blue); }
.ws-item .ws-info { flex: 1; min-width: 0; }
.ws-item .ws-title {
  font-size: 0.95em; font-weight: 500;
  color: var(--text-primary); margin-bottom: 4px;
}
.ws-item .ws-meta {
  font-size: 0.8em; color: var(--text-secondary);
}
.ws-item .ws-source {
  font-size: 0.78em; color: var(--accent-blue);
}
.ws-item .ws-snippet {
  font-size: 0.78em; color: var(--text-muted);
  margin-top: 4px; line-height: 1.5;
  overflow: hidden; text-overflow: ellipsis;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
}
.ws-item .ws-btn {
  align-self: center; flex-shrink: 0;
  padding: 6px 14px;
  border: none; border-radius: var(--radius-md);
  font-size: 0.8em; cursor: pointer; font-family: inherit;
  background: var(--accent-blue); color: #fff;
}
.ws-item .ws-btn.ws-btn-link {
  background: var(--bg-tertiary); color: var(--text-primary);
  border: 1px solid var(--border-primary);
}
.ws-tag {
  display: inline-block; padding: 1px 8px; border-radius: 10px;
  font-size: 0.72em; font-weight: 500; margin-left: 6px;
}
.ws-tag.matched { background: rgba(34,197,94,0.15); color: #22c55e; }
.ws-tag.unmatched { background: rgba(251,191,36,0.15); color: #fbbf24; }

/* ====== Modal ====== */
.modal-overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center;
}
.modal-overlay.hidden { display: none; }
.modal {
  background: var(--bg-secondary);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-xl);
  padding: 24px; width: 90%; max-width: 420px;
}
.modal h3 { margin-bottom: 16px; font-size: 1.1em; }
.modal input {
  width: 100%; padding: 10px 14px;
  background: var(--bg-primary);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-size: 0.9em; font-family: inherit; outline: none;
}
.modal input:focus { border-color: var(--accent-blue); }
.modal-btns { display: flex; gap: 8px; margin-top: 16px; justify-content: flex-end; }
.modal-status { margin-top: 10px; font-size: 0.85em; color: var(--text-secondary); }

/* ====== Scrollbar ====== */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-primary); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ====== Mobile responsive ====== */
@media (max-width: 600px) {
  .shelf-grid { grid-template-columns: repeat(3, 1fr); gap: 10px; }
  .book-card .card-cover { height: 140px; }
  .action-bar { flex-direction: column; align-items: stretch; }
  .reader-content { padding: 56px 12px 56px; }
  /* Discover mobile */
  .discover-book-card { padding: 8px; }
  .db-cover { width: 40px; height: 52px; }
}

/* ====== Discover / Explore ====== */
.discover-source-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-primary);
}
#discover-source-select {
  flex: 1;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-secondary);
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 0.95em;
  max-width: 280px;
}
.discover-source-count {
  color: var(--text-muted);
  font-size: 0.8em;
  white-space: nowrap;
}

.discover-categories {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 10px 16px;
  overflow-x: auto;
  background: var(--bg-primary);
  border-bottom: 1px solid var(--border-primary);
}
.discover-cat-btn {
  padding: 6px 14px;
  border-radius: 20px;
  border: 1px solid var(--border-secondary);
  background: var(--bg-secondary);
  color: var(--text-secondary);
  font-size: 0.85em;
  cursor: pointer;
  white-space: nowrap;
  transition: all var(--transition-normal);
}
.discover-cat-btn:hover {
  background: var(--bg-hover);
}
.discover-cat-btn.active {
  background: var(--accent-blue);
  color: var(--text-on-accent);
  border-color: var(--accent-blue);
}

.discover-books {
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 200px;
}

.discover-book-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-radius: var(--radius-md);
  background: var(--bg-secondary);
  cursor: pointer;
  transition: all var(--transition-normal);
  border: 1px solid var(--border-primary);
}
.discover-book-card:hover {
  border-color: var(--accent-blue);
  box-shadow: var(--shadow-card-hover);
}
.db-cover {
  width: 48px;
  height: 64px;
  border-radius: var(--radius-sm);
  background: var(--bg-tertiary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5em;
  flex-shrink: 0;
  overflow: hidden;
}
.db-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.db-info {
  flex: 1;
  min-width: 0;
}
.db-title {
  font-weight: 600;
  color: var(--text-primary);
  font-size: 0.95em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.db-meta {
  font-size: 0.8em;
  color: var(--text-muted);
  margin-top: 2px;
}
.db-intro {
  font-size: 0.75em;
  color: var(--text-muted);
  margin-top: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.db-add-btn {
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--accent-blue);
  background: transparent;
  color: var(--accent-blue);
  font-size: 0.8em;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
  transition: all var(--transition-normal);
}
.db-add-btn:hover {
  background: var(--accent-blue);
  color: var(--text-on-accent);
}

.discover-loadmore {
  padding: 16px;
  text-align: center;
}
