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

:root {
  --bg-primary: #0f0f13;
  --bg-secondary: #1a1a24;
  --bg-card: #22223a;
  --bg-hover: #2a2a45;
  --text-primary: #f0f0f5;
  --text-secondary: #a0a0b8;
  --accent: #e8405c;
  --accent-hover: #d63050;
  --border: #2e2e48;
  --success: #4caf50;
  --radius: 12px;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.hidden {
  display: none !important;
}

.login-container {
  max-width: 400px;
  margin: 0 auto;
  padding: 80px 24px;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-header h1 {
  font-size: 2.5rem;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 8px;
}

.login-header p {
  color: var(--text-secondary);
  font-size: 1.05rem;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 6px;
  font-size: 0.9rem;
  color: var(--text-secondary);
  font-weight: 500;
}

.form-group input {
  width: 100%;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-size: 1rem;
  transition: border-color 0.2s;
}

.form-group input:focus {
  outline: none;
  border-color: var(--accent);
}

.form-group input::placeholder {
  color: #555;
}

button[type="submit"],
.btn-primary {
  width: 100%;
  padding: 14px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: white;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
  margin-top: 8px;
}

button[type="submit"]:hover,
.btn-primary:hover {
  background: var(--accent-hover);
}

button[type="submit"]:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-secondary {
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: transparent;
  color: var(--text-secondary);
  font-size: 0.85rem;
  cursor: pointer;
  transition: all 0.2s;
}

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

.error {
  margin-top: 16px;
  padding: 12px 16px;
  border-radius: 8px;
  background: rgba(232, 64, 92, 0.15);
  color: var(--accent);
  font-size: 0.9rem;
  text-align: center;
}

.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 32px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.top-bar h1 {
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--accent);
}

.top-bar-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

#user-info {
  font-size: 0.9rem;
  color: var(--text-secondary);
}

.search-bar {
  background: var(--bg-secondary);
  padding: 16px 32px;
  border-bottom: 1px solid var(--border);
}

.search-container {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  gap: 10px;
}

#search-input {
  flex: 1;
  padding: 10px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 0.95rem;
}

#search-input:focus {
  outline: none;
  border-color: var(--accent);
}

#search-input::placeholder {
  color: #555;
}

#search-btn {
  padding: 10px 24px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: white;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

#search-btn:hover {
  background: var(--accent-hover);
}

.url-add-container {
  max-width: 1200px;
  margin: 10px auto 0;
  display: flex;
  gap: 10px;
  align-items: center;
}

#url-input {
  flex: 1;
  padding: 10px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 0.85rem;
}

#url-input:focus {
  outline: none;
  border-color: var(--accent);
}

#url-input::placeholder {
  color: #555;
}

#url-add-btn {
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: white;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
  white-space: nowrap;
}

#url-add-btn:hover {
  background: var(--accent-hover);
}

#url-add-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.url-status {
  font-size: 0.85rem;
  white-space: nowrap;
}

.url-status.success {
  color: var(--success);
}

.url-status.error {
  color: var(--accent);
}

.section {
  margin-bottom: 40px;
}

.no-results {
  color: var(--text-secondary);
  font-size: 0.95rem;
  padding: 20px 0;
}

.library-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 32px 24px;
}

.library-content h2 {
  font-size: 1.5rem;
  margin-bottom: 24px;
  font-weight: 600;
}

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

.book-card {
  cursor: pointer;
  transition: transform 0.2s;
  border-radius: var(--radius);
  overflow: hidden;
}

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

.book-cover-wrapper {
  position: relative;
  aspect-ratio: 2 / 3;
  background: var(--bg-card);
  border-radius: var(--radius);
  overflow: hidden;
}

.book-cover-wrapper img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.book-cover-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  font-size: 2rem;
  color: var(--text-secondary);
  background: var(--bg-card);
}

.book-badges {
  position: absolute;
  bottom: 8px;
  left: 8px;
  display: flex;
  gap: 4px;
}

.badge {
  padding: 3px 8px;
  border-radius: 4px;
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.badge-audio {
  background: rgba(76, 175, 80, 0.9);
  color: white;
}

.badge-ebook {
  background: rgba(33, 150, 243, 0.9);
  color: white;
}

.book-info {
  padding: 10px 4px;
}

.book-title {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 4px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.book-author {
  font-size: 0.75rem;
  color: var(--text-secondary);
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 64px;
  gap: 16px;
}

.loading p {
  color: var(--text-secondary);
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
}

.modal-content {
  position: relative;
  background: var(--bg-secondary);
  border-radius: var(--radius);
  max-width: 700px;
  width: 90%;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: var(--shadow);
}

.modal-close {
  position: absolute;
  top: 12px;
  right: 16px;
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 1.8rem;
  cursor: pointer;
  z-index: 2;
  line-height: 1;
  padding: 4px;
}

.modal-close:hover {
  color: var(--text-primary);
}

.detail-header {
  display: flex;
  gap: 24px;
  padding: 32px;
}

.detail-cover {
  flex-shrink: 0;
  width: 180px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: var(--shadow);
}

.detail-cover img {
  width: 100%;
  display: block;
}

.detail-meta {
  flex: 1;
  min-width: 0;
}

.detail-meta h2 {
  font-size: 1.5rem;
  font-weight: 700;
  margin-bottom: 8px;
}

.detail-authors {
  color: var(--text-secondary);
  font-size: 0.95rem;
  margin-bottom: 16px;
}

.detail-badges {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.detail-badges .badge {
  font-size: 0.75rem;
  padding: 4px 12px;
}

.detail-rating {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
}

.stars {
  color: #f5a623;
  font-size: 1.1rem;
}

.rating-count {
  color: var(--text-secondary);
  font-size: 0.85rem;
}

.detail-description {
  padding: 0 32px 32px;
  color: var(--text-secondary);
  font-size: 0.9rem;
  line-height: 1.6;
}

.detail-downloads {
  display: flex;
  gap: 10px;
  margin-top: 16px;
  flex-wrap: wrap;
}

.btn-download {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  color: white;
}

.btn-download:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.btn-download-audio {
  background: #4caf50;
}

.btn-download-audio:hover:not(:disabled) {
  background: #43a047;
}

.btn-download-ebook {
  background: #2196f3;
}

.btn-download-ebook:hover:not(:disabled) {
  background: #1e88e5;
}

.dl-icon {
  font-size: 1rem;
}

.detail-cache-actions {
  display: flex;
  gap: 10px;
  margin-top: 8px;
  flex-wrap: wrap;
}

.btn-cache {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  color: white;
}

.btn-cache:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.btn-cache-audio {
  background: #7b1fa2;
}

.btn-cache-audio:hover:not(:disabled) {
  background: #6a1b9a;
}

.btn-cache-ebook {
  background: #f57c00;
}

.btn-cache-ebook:hover:not(:disabled) {
  background: #ef6c00;
}

.btn-cache-done {
  background: var(--success) !important;
}

.badge-type {
  background: rgba(156, 39, 176, 0.9);
  color: white;
}

.detail-shelf-action {
  margin-top: 12px;
}

.btn-shelf {
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  color: white;
}

.btn-shelf:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.btn-shelf-add {
  background: var(--accent);
}

.btn-shelf-add:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-shelf-remove {
  background: #555;
}

.btn-shelf-remove:hover:not(:disabled) {
  background: #666;
}

.btn-shelf-done {
  background: #4caf50;
}

.reactions-section {
  padding: 0 32px 24px;
}

.reactions-section h3 {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 12px;
  color: var(--text-primary);
}

.reaction-tag {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px;
}

.reaction-name {
  font-size: 0.85rem;
  color: var(--text-secondary);
  min-width: 120px;
}

.reaction-bar-bg {
  flex: 1;
  height: 8px;
  background: var(--bg-primary);
  border-radius: 4px;
  overflow: hidden;
}

.reaction-bar-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 4px;
  transition: width 0.5s ease;
}

.reaction-pct {
  font-size: 0.8rem;
  color: var(--text-secondary);
  min-width: 35px;
  text-align: right;
}

.reviews-section {
  padding: 0 32px 32px;
  border-top: 1px solid var(--border);
  margin-top: 8px;
  padding-top: 24px;
}

.reviews-section h3 {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 16px;
  color: var(--text-primary);
}

.review-item {
  padding: 16px;
  background: var(--bg-primary);
  border-radius: 8px;
  margin-bottom: 12px;
}

.review-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.review-author {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-primary);
}

.review-rating {
  color: #f5a623;
  font-size: 0.9rem;
}

.review-text {
  font-size: 0.85rem;
  color: var(--text-secondary);
  line-height: 1.5;
}

.detail-loading {
  padding: 40px;
  text-align: center;
  color: var(--text-secondary);
}

.detail-length {
  color: var(--text-secondary);
  font-size: 0.85rem;
  margin-bottom: 8px;
}

.player-overlay,
.reader-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(0, 0, 0, 0.92);
  display: flex;
  align-items: center;
  justify-content: center;
}

.player-container {
  background: var(--bg-secondary);
  border-radius: var(--radius);
  padding: 40px;
  max-width: 420px;
  width: 90%;
  text-align: center;
  position: relative;
  box-shadow: var(--shadow);
}

.player-close {
  position: absolute;
  top: 12px;
  right: 16px;
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 1.8rem;
  cursor: pointer;
  line-height: 1;
  padding: 4px;
}

.player-close:hover {
  color: var(--text-primary);
}

.player-cover {
  width: 200px;
  height: 200px;
  margin: 0 auto 24px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
}

.player-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.player-title {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player-author {
  font-size: 0.9rem;
  color: var(--text-secondary);
  margin-bottom: 24px;
}

.player-progress {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
}

.player-progress span {
  font-size: 0.8rem;
  color: var(--text-secondary);
  min-width: 45px;
  font-variant-numeric: tabular-nums;
}

#player-current-time {
  text-align: right;
}

#player-duration {
  text-align: left;
}

#player-seek {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  height: 6px;
  background: var(--border);
  border-radius: 3px;
  outline: none;
  cursor: pointer;
}

#player-seek::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  background: var(--accent);
  border-radius: 50%;
  cursor: pointer;
}

#player-seek::-moz-range-thumb {
  width: 16px;
  height: 16px;
  background: var(--accent);
  border-radius: 50%;
  cursor: pointer;
  border: none;
}

.player-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 20px;
  margin-bottom: 20px;
}

.player-ctrl-btn {
  background: var(--bg-card);
  border: none;
  color: var(--text-primary);
  font-size: 0.9rem;
  font-weight: 700;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  cursor: pointer;
  transition: background 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.player-ctrl-btn:hover {
  background: var(--bg-hover);
}

.player-ctrl-main {
  width: 64px;
  height: 64px;
  font-size: 1.4rem;
  background: var(--accent);
  color: white;
}

.player-ctrl-main:hover {
  background: var(--accent-hover);
}

.player-volume {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.player-vol-icon {
  font-size: 1.1rem;
  color: var(--text-secondary);
}

#player-volume {
  width: 120px;
  -webkit-appearance: none;
  appearance: none;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  outline: none;
  cursor: pointer;
}

#player-volume::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  background: var(--text-secondary);
  border-radius: 50%;
  cursor: pointer;
}

#player-volume::-moz-range-thumb {
  width: 14px;
  height: 14px;
  background: var(--text-secondary);
  border-radius: 50%;
  cursor: pointer;
  border: none;
}

.player-loading {
  padding: 60px 0;
}

.reader-container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--bg-secondary);
}

.reader-header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 20px;
  background: var(--bg-primary);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.reader-close-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 1.6rem;
  cursor: pointer;
  padding: 4px;
  line-height: 1;
}

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

.reader-header-title {
  flex: 1;
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.reader-toc-btn {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  padding: 6px 14px;
  border-radius: 6px;
  font-size: 0.85rem;
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
}

.reader-toc-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

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

.reader-toc {
  width: 280px;
  background: var(--bg-primary);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  flex-shrink: 0;
}

.reader-toc ul {
  list-style: none;
  padding: 12px 0;
}

.reader-toc li {
  padding: 10px 20px;
  font-size: 0.9rem;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.2s;
  border-left: 3px solid transparent;
}

.reader-toc li:hover {
  color: var(--text-primary);
  background: var(--bg-secondary);
}

.reader-toc li.active {
  color: var(--accent);
  border-left-color: var(--accent);
  background: var(--bg-secondary);
}

.reader-area {
  flex: 1;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.reader-nav {
  display: flex;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--bg-primary);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.reader-nav-btn {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  padding: 8px 20px;
  border-radius: 6px;
  font-size: 0.85rem;
  cursor: pointer;
  transition: all 0.2s;
}

.reader-nav-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.btn-listen,
.btn-read {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  color: white;
}

.btn-listen {
  background: #7c4dff;
}

.btn-listen:hover:not(:disabled) {
  background: #651fff;
}

.btn-read {
  background: #00bcd4;
}

.btn-read:hover:not(:disabled) {
  background: #00a0b4;
}

.btn-listen:disabled,
.btn-read:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.detail-actions {
  display: flex;
  gap: 10px;
  margin-top: 12px;
  flex-wrap: wrap;
}

@media (max-width: 600px) {
  .book-grid {
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    gap: 16px;
  }

  .detail-header {
    flex-direction: column;
    align-items: center;
    text-align: center;
  }

  .detail-cover {
    width: 140px;
  }

  .detail-badges {
    justify-content: center;
  }

  .top-bar {
    padding: 12px 16px;
  }

  .library-content {
    padding: 20px 16px;
  }

  .search-bar {
    padding: 12px 16px;
  }

  .search-container {
    flex-wrap: wrap;
  }

  #search-input {
    width: 100%;
  }

  .reaction-name {
    min-width: 80px;
    font-size: 0.8rem;
  }

  .player-container {
    padding: 24px;
  }

  .player-cover {
    width: 160px;
    height: 160px;
  }

  .reader-toc {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 260px;
    z-index: 10;
    box-shadow: 4px 0 16px rgba(0, 0, 0, 0.4);
  }
}

.sync-status {
  text-align: center;
  font-size: 0.75rem;
  color: var(--accent);
  opacity: 0;
  transition: opacity 0.3s ease;
  padding: 4px 0;
  pointer-events: none;
}

.sync-status.visible {
  opacity: 1;
}
