/* ============================================================
   Reset & Base
   ============================================================ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0f0f0f;
  --bg2: #1a1a1a;
  --bg3: #242424;
  --border: #2e2e2e;
  --text: #e8e8e8;
  --text-muted: #888;
  --accent: #c9a84c;
  --accent-dim: #7a6530;
  --danger: #c0392b;
  --radius: 6px;
  --player-h: 72px;
}

html { font-size: 15px; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  line-height: 1.5;
  min-height: 100vh;
  padding-bottom: calc(var(--player-h) + 16px);
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

button {
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
  font: inherit;
}

input, select {
  font: inherit;
  color: inherit;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.35rem 0.6rem;
}

input:focus, select:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* ============================================================
   Layout
   ============================================================ */
#app {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1.5rem 1rem;
}

/* ============================================================
   Header
   ============================================================ */
.site-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1.5rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border);
}

.site-title {
  font-size: 1.3rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--accent);
  flex: 1;
  cursor: pointer;
}

.btn {
  padding: 0.4rem 0.9rem;
  border-radius: var(--radius);
  font-size: 0.85rem;
  font-weight: 500;
  border: 1px solid var(--border);
  background: var(--bg3);
  color: var(--text);
  transition: background 0.15s, border-color 0.15s;
}
.btn:hover { background: var(--bg2); border-color: var(--accent-dim); }
.btn-accent { background: var(--accent); border-color: var(--accent); color: #000; }
.btn-accent:hover { background: #b8943e; border-color: #b8943e; }

/* ============================================================
   Library layout: sidebar + grid
   ============================================================ */
.library-layout {
  display: grid;
  grid-template-columns: 220px 1fr;
  gap: 1.5rem;
  align-items: start;
}

@media (max-width: 700px) {
  .library-layout { grid-template-columns: 1fr; }
  .filter-sidebar { display: none; }
  .filter-sidebar.open { display: block; }
  .filter-toggle { display: inline-flex !important; }
}

/* ============================================================
   Filter sidebar
   ============================================================ */
.filter-sidebar {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  position: sticky;
  top: 1rem;
}

.filter-toggle {
  display: none;
  margin-bottom: 0.75rem;
  gap: 0.5rem;
}

.filter-sidebar h3 {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
}

.filter-group { margin-bottom: 1rem; }
.filter-group label {
  display: block;
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 0.3rem;
}

.filter-group input[type="text"],
.filter-group select {
  width: 100%;
}

.filter-group select[multiple] {
  height: 100px;
  resize: vertical;
}

.btn-clear {
  width: 100%;
  margin-top: 0.5rem;
  font-size: 0.8rem;
  color: var(--text-muted);
}
.btn-clear:hover { color: var(--text); }

/* ============================================================
   Book grid
   ============================================================ */
.book-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 1rem;
}

.book-card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  transition: border-color 0.15s, transform 0.1s;
}
.book-card:hover {
  border-color: var(--accent-dim);
  transform: translateY(-2px);
}

.book-cover-wrap {
  aspect-ratio: 1 / 1;
  overflow: hidden;
  background: var(--bg3);
  display: flex;
  align-items: center;
  justify-content: center;
}

.book-cover-wrap img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.book-cover-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  font-weight: 700;
  color: var(--accent);
  background: var(--bg3);
  user-select: none;
}

.book-info {
  padding: 0.6rem 0.7rem;
}

.book-title {
  font-size: 0.85rem;
  font-weight: 600;
  line-height: 1.3;
  margin-bottom: 0.2rem;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.book-author {
  font-size: 0.75rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.book-series-badge {
  display: inline-block;
  margin-top: 0.3rem;
  font-size: 0.7rem;
  color: var(--accent);
  background: rgba(201,168,76,0.12);
  padding: 0.1rem 0.4rem;
  border-radius: 3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.tag-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  margin-top: 0.35rem;
}

.tag-chip {
  font-size: 0.65rem;
  padding: 0.1rem 0.35rem;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 3px;
  color: var(--text-muted);
}

.empty-state {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--text-muted);
  grid-column: 1 / -1;
}

/* ============================================================
   Book detail view
   ============================================================ */
.book-detail {
  max-width: 800px;
}

.back-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  color: var(--text-muted);
  font-size: 0.85rem;
  margin-bottom: 1.25rem;
  cursor: pointer;
}
.back-btn:hover { color: var(--text); }

.book-detail-header {
  display: grid;
  grid-template-columns: 180px 1fr;
  gap: 1.5rem;
  margin-bottom: 1.5rem;
}

@media (max-width: 560px) {
  .book-detail-header { grid-template-columns: 1fr; }
  .detail-cover-wrap { max-width: 180px; }
}

.detail-cover-wrap {
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--bg3);
  aspect-ratio: 1 / 1;
}

.detail-cover-wrap img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.detail-cover-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 3rem;
  font-weight: 700;
  color: var(--accent);
}

.detail-meta h1 {
  font-size: 1.4rem;
  line-height: 1.25;
  margin-bottom: 0.3rem;
}

.detail-meta .author {
  font-size: 1rem;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
}

.detail-meta .series {
  font-size: 0.85rem;
  color: var(--accent);
  margin-bottom: 0.5rem;
}

.detail-actions {
  display: flex;
  gap: 0.6rem;
  margin-top: 1rem;
  flex-wrap: wrap;
}

/* ============================================================
   Book description (detail page)
   ============================================================ */
.book-description {
  margin: 0 0 1.5rem;
  padding: 1rem 1.25rem;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.9rem;
  line-height: 1.65;
  color: var(--text-muted);
}
.book-description p { margin: 0; }

/* ============================================================
   Track list
   ============================================================ */
.track-list-section { margin-top: 1.5rem; }
.track-list-section h3 {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
}

.track-list {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  max-height: 280px;
  overflow-y: auto;
}

.track-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.1s;
  font-size: 0.85rem;
}
.track-item:last-child { border-bottom: none; }
.track-item:hover { background: var(--bg3); }
.track-item.active { background: rgba(201,168,76,0.1); color: var(--accent); }

.track-num {
  color: var(--text-muted);
  min-width: 1.8rem;
  text-align: right;
  font-size: 0.75rem;
}

.track-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ============================================================
   Player bar (fixed bottom)
   ============================================================ */
.player-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--player-h);
  background: var(--bg2);
  border-top: 1px solid var(--border);
  display: grid;
  grid-template-columns: minmax(0,1.5fr) auto minmax(0,2fr) auto;
  align-items: center;
  gap: 1rem;
  padding: 0 1.2rem;
  z-index: 100;
}

.player-bar.hidden { display: none; }

.player-info {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  min-width: 0;
}

.player-cover {
  width: 44px;
  height: 44px;
  border-radius: 4px;
  object-fit: cover;
  flex-shrink: 0;
}

.player-meta { min-width: 0; }
.player-title {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.player-track {
  display: block;
  font-size: 0.75rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.player-controls {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

.icon-btn {
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  font-size: 0.85rem;
  color: var(--text-muted);
  transition: background 0.12s, color 0.12s;
}
.icon-btn:hover { background: var(--bg3); color: var(--text); }
.play-btn {
  width: 40px;
  height: 40px;
  font-size: 1rem;
  background: var(--accent);
  color: #000;
}
.play-btn:hover { background: #b8943e; }

.player-progress {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}

.time-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  flex-shrink: 0;
  width: 3rem;
  text-align: center;
}

.scrubber {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  height: 4px;
  background: var(--bg3);
  border-radius: 2px;
  outline: none;
  border: none;
  padding: 0;
  cursor: pointer;
}
.scrubber::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  background: var(--accent);
  border-radius: 50%;
  cursor: pointer;
}
.scrubber::-moz-range-thumb {
  width: 14px;
  height: 14px;
  background: var(--accent);
  border-radius: 50%;
  border: none;
  cursor: pointer;
}

.player-speed select {
  font-size: 0.75rem;
  padding: 0.2rem 0.4rem;
  flex-shrink: 0;
}

/* ============================================================
   Auth error / loading
   ============================================================ */
.centered-msg {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 60vh;
  gap: 1rem;
  text-align: center;
  color: var(--text-muted);
}
.centered-msg h2 { color: var(--text); font-size: 1.2rem; }

/* ============================================================
   Admin metadata editor
   ============================================================ */
.admin-toolbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1rem;
}

.admin-count {
  font-size: 0.8rem;
  color: var(--text-muted);
  white-space: nowrap;
}

.admin-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.admin-row {
  display: grid;
  grid-template-columns: 80px 1fr auto;
  gap: 1rem;
  align-items: start;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
  transition: border-color 0.15s;
}

.admin-row:hover { border-color: var(--accent-dim); }
.admin-row-missing { opacity: 0.55; }

/* Cover thumbnail + upload button */
.admin-cover-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
}

.admin-cover-thumb {
  width: 64px;
  height: 64px;
  border-radius: 4px;
  overflow: hidden;
  background: var(--bg3);
  flex-shrink: 0;
}

.admin-cover-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.admin-cover-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--accent);
}

.admin-upload-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.9rem;
  color: var(--text-muted);
  transition: background 0.12s, color 0.12s;
}
.admin-upload-btn:hover { background: var(--bg2); color: var(--accent); border-color: var(--accent-dim); }

/* Fields */
.admin-fields { min-width: 0; }

.admin-path {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.missing-badge {
  display: inline-block;
  font-size: 0.65rem;
  padding: 0.05rem 0.35rem;
  background: rgba(192,57,43,0.25);
  color: #e07060;
  border-radius: 3px;
  vertical-align: middle;
}

.admin-field-row {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.4rem;
  flex-wrap: wrap;
}

.admin-input {
  flex: 1;
  min-width: 120px;
  font-size: 0.85rem;
  padding: 0.3rem 0.5rem;
}

.admin-input-series { flex: 2; min-width: 160px; }
.admin-input-num { flex: 0 0 70px; min-width: 70px; }
.admin-input-tags { flex: 3; min-width: 180px; }

/* Actions */
.admin-actions-cell {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.4rem;
  flex-shrink: 0;
}

.admin-save-btn {
  font-size: 0.8rem;
  padding: 0.35rem 0.8rem;
  white-space: nowrap;
}

.admin-status {
  font-size: 0.75rem;
  height: 1.1em;
}
.admin-status-ok { color: #6dbf7e; }
.admin-status-err { color: #e07060; }

@media (max-width: 680px) {
  .admin-row { grid-template-columns: 64px 1fr; }
  .admin-actions-cell { grid-column: 1 / -1; flex-direction: row; align-items: center; }
  .admin-cover-cell { flex-direction: row; width: 64px; }
}

/* ============================================================
   Tag autocomplete dropdown
   ============================================================ */
.tag-dd {
  position: absolute;
  z-index: 200;
  background: var(--bg2);
  border: 1px solid var(--accent-dim);
  border-radius: var(--radius);
  box-shadow: 0 4px 14px rgba(0,0,0,0.55);
  overflow: hidden;
}
.tag-dd.hidden { display: none; }

.tag-dd-item {
  padding: 0.38rem 0.75rem;
  font-size: 0.85rem;
  cursor: pointer;
  white-space: nowrap;
}
.tag-dd-item:hover {
  background: rgba(201,168,76,0.15);
  color: var(--accent);
}

/* ============================================================
   Bulk apply panel
   ============================================================ */
.admin-bulk {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 1rem;
}
.admin-bulk.hidden { display: none; }

.admin-bulk-inner { padding: 1rem; }

.admin-bulk-title {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
}

.admin-bulk-form {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1.25rem;
  align-items: flex-start;
  margin-bottom: 0.75rem;
}

.admin-bulk-field {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}
.admin-bulk-field > label {
  font-size: 0.75rem;
  color: var(--text-muted);
}
.admin-bulk-field select,
.admin-bulk-field input[type="text"] {
  min-width: 160px;
}
.admin-bulk-field select { min-width: 240px; }

.field-hint {
  font-size: 0.68rem;
  color: var(--text-muted);
  font-style: italic;
  font-weight: 400;
}

.admin-bulk-check {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.75rem;
  color: var(--text-muted);
  cursor: pointer;
  margin-top: 0.3rem;
  user-select: none;
}
.admin-bulk-check input[type="checkbox"] {
  width: auto;
  background: none;
  border: 1px solid var(--border);
  padding: 0;
  cursor: pointer;
}

.admin-bulk-preview {
  font-size: 0.8rem;
  margin-bottom: 0.75rem;
  border-top: 1px solid var(--border);
  padding-top: 0.6rem;
}
.preview-count {
  font-weight: 600;
  color: var(--text);
  margin-bottom: 0.3rem;
}
.preview-list {
  list-style: none;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 0.1rem 1rem;
}
.preview-list li { color: var(--text-muted); font-size: 0.78rem; }
.preview-list li::before { content: "· "; }
.preview-more { font-style: italic; }

.admin-bulk-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

/* Description row */
.admin-field-desc {
  align-items: flex-start;
  flex-wrap: nowrap;
}

.admin-input-desc {
  resize: vertical;
  min-height: 4rem;
  font-size: 0.82rem;
  line-height: 1.5;
  flex: 1;
}

.admin-fetch-btn {
  font-size: 0.75rem;
  padding: 0.3rem 0.65rem;
  flex-shrink: 0;
  align-self: flex-start;
  color: var(--text-muted);
  white-space: nowrap;
}
.admin-fetch-btn:hover { color: var(--accent); border-color: var(--accent-dim); }

/* Fetch candidates */
.admin-fetch-results {
  margin-top: 0.4rem;
  border: 1px solid var(--accent-dim);
  border-radius: var(--radius);
  overflow: hidden;
}
.admin-fetch-results.hidden { display: none; }

.fetch-candidate {
  padding: 0.6rem 0.8rem;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.1s;
}
.fetch-candidate:last-child { border-bottom: none; }
.fetch-candidate:hover { background: rgba(201,168,76,0.08); }

.fetch-candidate-title {
  font-size: 0.82rem;
  font-weight: 600;
  margin-bottom: 0.2rem;
}
.fetch-candidate-authors {
  font-weight: 400;
  color: var(--text-muted);
}
.fetch-candidate-desc {
  font-size: 0.78rem;
  color: var(--text-muted);
  line-height: 1.4;
}

.fetch-none {
  padding: 0.6rem 0.8rem;
  font-size: 0.8rem;
  color: var(--text-muted);
  font-style: italic;
}

/* Strip _ button */
.admin-strip-btn {
  font-size: 0.75rem;
  padding: 0.25rem 0.55rem;
  color: var(--text-muted);
  white-space: nowrap;
}
.admin-strip-btn:hover { color: var(--accent); border-color: var(--accent-dim); }

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