:root {
  --bg: #11131a;
  --bg-2: #181b24;
  --card: #1d212c;
  --line: #2a2f3c;
  --text: #eef1f7;
  --muted: #8b93a7;
  --accent: #5b8cff;
  --accent-2: #4674e6;
  --danger: #ff5b6e;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}
body { display: flex; flex-direction: column; }

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.pad { padding: 1.5rem; }
.spacer { flex: 1; }
.error { color: var(--danger); margin: 0.5rem 0 0; }

.view { flex: 1; min-height: 0; }
.center { display: flex; align-items: center; justify-content: center; padding: 1.5rem; }

/* Cards (login / user select) */
.card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 1.75rem; width: 100%; max-width: 380px;
  display: flex; flex-direction: column; gap: 0.9rem; box-shadow: 0 10px 40px rgba(0,0,0,0.4);
}
.brand { font-size: 1.6rem; font-weight: 700; margin: 0; letter-spacing: 0.5px; }
.brand.small { font-size: 1.1rem; }

input, select {
  background: var(--bg-2); border: 1px solid var(--line); color: var(--text);
  border-radius: 10px; padding: 0.8rem 0.9rem; font-size: 1rem; width: 100%;
}
input:focus, select:focus { outline: none; border-color: var(--accent); }
select { -webkit-appearance: none; appearance: none; }

.btn {
  background: var(--bg-2); border: 1px solid var(--line); color: var(--text);
  border-radius: 10px; padding: 0.8rem 1rem; font-size: 1rem; cursor: pointer;
  transition: filter 0.15s, background 0.15s;
}
.btn:active { filter: brightness(1.2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.ghost { background: transparent; }
.btn.small { padding: 0.55rem 0.8rem; font-size: 0.9rem; }
.btn.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn.danger:active { background: var(--danger); color: #fff; }

.user-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; margin: 0.5rem 0; }
.user-grid .user-btn {
  background: var(--bg-2); border: 1px solid var(--line); color: var(--text);
  border-radius: 12px; padding: 1.4rem 0.5rem; font-size: 1.05rem; font-weight: 600;
  cursor: pointer; text-align: center;
}
.user-grid .user-btn:active { border-color: var(--accent); }

/* Top bar */
.topbar {
  display: flex; align-items: center; gap: 0.5rem;
  padding: calc(0.7rem + var(--safe-top)) 0.9rem 0.7rem;
  background: var(--bg-2); border-bottom: 1px solid var(--line);
}
.chip { background: var(--card); border: 1px solid var(--line); border-radius: 999px; padding: 0.25rem 0.7rem; font-size: 0.85rem; }
.icon-btn {
  background: transparent; border: none; color: var(--text); font-size: 1.2rem;
  cursor: pointer; padding: 0.35rem 0.5rem; border-radius: 8px; line-height: 1;
}
.icon-btn.big { font-size: 1.5rem; }
.icon-btn:active { background: var(--card); }

/* Tabs */
.tabs { display: flex; background: var(--bg-2); border-bottom: 1px solid var(--line); }
.tab {
  flex: 1; background: transparent; border: none; color: var(--muted);
  padding: 0.85rem 0; font-size: 0.95rem; cursor: pointer; position: relative;
}
.tab.active { color: var(--text); font-weight: 600; }
.tab.active::after {
  content: ""; position: absolute; left: 20%; right: 20%; bottom: 0; height: 3px;
  background: var(--accent); border-radius: 3px 3px 0 0;
}

.content { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding-bottom: 90px; }
.search { margin: 0.9rem; width: calc(100% - 1.8rem); }

/* Book list */
.book-list { display: flex; flex-direction: column; }
.book-row {
  display: flex; align-items: center; gap: 0.85rem; padding: 0.7rem 0.9rem;
  border-bottom: 1px solid var(--line);
}
.row-actions { display: flex; align-items: center; gap: 0.1rem; flex: none; }
.row-btn {
  background: transparent; border: none; cursor: pointer;
  color: var(--muted); border-radius: 8px; line-height: 1;
  display: flex; align-items: center; justify-content: center;
  width: 2.2rem; height: 2.2rem; font-size: 1rem;
}
.row-btn:active { background: var(--card); color: var(--text); }
.book-cover {
  width: 56px; height: 56px; border-radius: 8px; object-fit: cover;
  background: linear-gradient(135deg, #2a2f3c, #1a1d27); flex: none;
}
.book-info { flex: 1; min-width: 0; }
.book-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.book-author { font-size: 0.85rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.book-meta { font-size: 0.75rem; color: var(--muted); margin-top: 2px; }
.progress-bar { height: 3px; background: var(--line); border-radius: 3px; margin-top: 6px; overflow: hidden; }
.progress-fill { height: 100%; background: var(--accent); }
.badge-done { color: #56d364; font-size: 0.75rem; }

/* Listen log */
.log-list { display: flex; flex-direction: column; }
.log-row { display: flex; gap: 0.75rem; padding: 0.7rem 0.9rem; border-bottom: 1px solid var(--line); align-items: center; cursor: pointer; }
.log-cover { width: 44px; height: 44px; border-radius: 6px; object-fit: cover; background: #222633; flex: none; }
.log-main { flex: 1; min-width: 0; }
.log-line { font-size: 0.92rem; }
.log-line b { font-weight: 600; }
.log-sub { font-size: 0.78rem; color: var(--muted); margin-top: 2px; }
.log-ev { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--accent); }

/* Mini player */
.mini-player {
  position: fixed; left: 0; right: 0; bottom: 0;
  display: flex; align-items: center; gap: 0.75rem;
  padding: 0.55rem 0.9rem calc(0.55rem + var(--safe-bottom));
  background: var(--bg-2); border-top: 1px solid var(--line);
  box-shadow: 0 -6px 20px rgba(0,0,0,0.35); cursor: pointer; z-index: 20;
}
.mini-cover { width: 44px; height: 44px; border-radius: 8px; object-fit: cover; background: #222633; flex: none; }
.mini-meta { flex: 1; min-width: 0; }
.mini-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mini-author { font-size: 0.8rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Full player overlay */
.view.player {
  position: fixed; inset: 0; z-index: 50;
  display: flex; flex-direction: column; overflow: hidden;
  padding-top: var(--safe-top); padding-bottom: var(--safe-bottom);
  background: var(--bg);
}

/* Blurred cover backdrop — sits behind everything in the player */
.player-bg {
  position: absolute; inset: -10%; z-index: 0;
  background-size: cover; background-position: center;
  filter: blur(28px) brightness(0.22) saturate(1.4);
  transition: background-image 0.6s;
}

/* Keep header + body above the backdrop */
.view.player > header,
.view.player > .player-body { position: relative; z-index: 1; }

.player-top { display: flex; align-items: center; gap: 0.5rem; padding: 0.7rem 0.9rem; }
.player-body { flex: 1; display: flex; flex-direction: column; align-items: center; padding: 1rem 1.5rem 2rem; gap: 0.4rem; overflow-y: auto; }
/* Cover wrapper positions the edit button over the image */
.player-cover-wrap {
  position: relative; display: inline-block;
  width: min(35vw, 150px); height: min(35vw, 150px);
  margin: 0.5rem 0 1rem;
}
.player-cover {
  width: 100%; height: 100%; border-radius: 18px; object-fit: cover;
  background: linear-gradient(135deg, #2a2f3c, #1a1d27); box-shadow: 0 18px 50px rgba(0,0,0,0.5);
  display: block;
}
.player-title { font-size: 1.3rem; text-align: center; margin: 0; }
.player-author { margin-bottom: 1.2rem; }

.seek { width: 100%; margin: 0.5rem 0 0; accent-color: var(--accent); height: 28px; }
.times { width: 100%; display: flex; justify-content: space-between; font-size: 0.8rem; }

.controls { display: flex; align-items: center; gap: 2rem; margin: 1.6rem 0 0.5rem; }
.ctrl {
  background: transparent; border: none; color: var(--text); cursor: pointer;
  display: flex; flex-direction: column; align-items: center; font-size: 1.8rem; line-height: 1;
  touch-action: manipulation;
}
.ctrl small { font-size: 0.65rem; color: var(--muted); margin-top: 2px; }
.ctrl.play {
  width: 76px; height: 76px; border-radius: 50%; background: var(--accent); color: #fff;
  align-items: center; justify-content: center; font-size: 2rem;
}
.ctrl.play:active { background: var(--accent-2); }

.player-extras { display: flex; gap: 2rem; margin-top: 1.5rem; }
.extra { display: flex; flex-direction: column; gap: 0.35rem; font-size: 0.8rem; color: var(--muted); align-items: center; }
.extra select { width: auto; padding: 0.45rem 0.7rem; }
.sleep-countdown {
  display: flex; align-items: center; gap: 0.6rem; justify-content: center;
  font-size: 0.78rem; color: var(--accent); margin-top: 0.6rem;
}
.sleep-reset-btn {
  padding: 0.25rem 0.55rem; font-size: 0.75rem;
  border-color: var(--accent); color: var(--accent);
}

/* Full-screen cover viewer */
.cover-fullscreen {
  position: fixed; inset: 0; z-index: 300;
  background: #000; display: flex; align-items: center; justify-content: center;
  cursor: zoom-out;
}
.cover-fullscreen img { max-width: 100%; max-height: 100%; object-fit: contain; }

/* Admin */
.admin-body { flex: 1; overflow-y: auto; padding: 1.2rem 1.4rem 2rem; display: flex; flex-direction: column; gap: 0.7rem; }
.admin-body h3 { margin: 1.2rem 0 0.2rem; font-size: 1rem; }
.field { display: flex; flex-direction: column; gap: 0.4rem; font-size: 0.85rem; color: var(--muted); }
.row { display: flex; gap: 0.6rem; align-items: center; margin-top: 0.3rem; }
.row input { flex: 1; }
.admin-users { display: flex; flex-direction: column; gap: 0.4rem; }
.admin-user { display: flex; align-items: center; gap: 0.6rem; background: var(--bg-2); border: 1px solid var(--line); border-radius: 8px; padding: 0.5rem 0.7rem; }
.admin-user .name { flex: 1; }
.admin-user .del { background: transparent; border: none; color: var(--danger); cursor: pointer; font-size: 1.1rem; }

/* Upload file picker */
.upload-label {
  display: flex; align-items: center; gap: 0.6rem;
  background: var(--bg-2); border: 1px solid var(--line); border-radius: 8px;
  padding: 0.6rem 0.8rem; cursor: pointer; font-size: 0.88rem; color: var(--text);
}
.upload-label input[type="file"] { display: none; }
.upload-label:active { background: var(--card); }
.upload-progress { margin-top: 0.4rem; }

/* Detail sheet delete zone */
.detail-danger-zone { margin-top: 1.5rem; padding-top: 1rem; border-top: 1px solid var(--line); }

/* No-cover placeholder consistent across book lists and log */
.no-cover { background: linear-gradient(135deg, #2a2f3c 0%, #1a1d27 100%); }

/* Bottom sheet modals */
.modal-overlay {
  position: fixed; inset: 0; z-index: 80;
  background: rgba(0,0,0,0.55); display: flex; align-items: flex-end;
  backdrop-filter: blur(3px);
}
.modal-sheet {
  width: 100%; max-height: 80vh; background: var(--bg-2);
  border-radius: 20px 20px 0 0; border-top: 1px solid var(--line);
  display: flex; flex-direction: column; overflow: hidden;
  padding-bottom: var(--safe-bottom);
}
.modal-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 1rem 1.2rem; border-bottom: 1px solid var(--line); font-weight: 600;
}
.modal-body { flex: 1; overflow-y: auto; padding: 1rem 1.2rem; display: flex; flex-direction: column; gap: 0.8rem; }
.modal-search-row { display: flex; gap: 0.5rem; }
.modal-search-row input { flex: 1; }
/* Book detail sheet */
.detail-sheet { max-height: 85vh; }
.detail-body { gap: 0; }
.detail-hero { display: flex; gap: 1rem; margin-bottom: 1rem; }
.detail-cover { width: 110px; height: 110px; border-radius: 12px; object-fit: cover; flex: none; cursor: zoom-in; }
.detail-cover.no-cover { cursor: default; }
.detail-hero-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 0.35rem; padding-top: 2px; }
.detail-title { font-size: 1.05rem; font-weight: 700; line-height: 1.3; }
.detail-sub { font-size: 0.85rem; }
.detail-description {
  font-size: 0.88rem; line-height: 1.6; color: var(--muted);
  margin: 0 0 1rem; white-space: pre-wrap;
}
.detail-section-heading {
  font-size: 0.8rem; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.06em; color: var(--muted); margin: 0.4rem 0 0.6rem;
}
.series-list { display: flex; flex-direction: column; gap: 0.35rem; margin-bottom: 0.5rem; }
.series-item {
  display: flex; align-items: center; gap: 0.75rem;
  padding: 0.5rem 0.6rem; border-radius: 10px; background: var(--bg);
}
.series-item.in-library { cursor: pointer; }
.series-item.in-library:active { background: var(--card); }
.series-item.not-owned { opacity: 0.4; }
.series-cover { width: 44px; height: 44px; border-radius: 6px; object-fit: cover; background: var(--card); flex: none; }
.series-info { flex: 1; min-width: 0; }
.series-item-title { font-size: 0.88rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.series-item-sub { font-size: 0.75rem; color: var(--muted); margin-top: 1px; }
.in-library-badge {
  font-size: 0.65rem; font-weight: 700; background: var(--accent); color: #fff;
  border-radius: 4px; padding: 1px 5px; white-space: nowrap; flex: none;
}
.detail-status-row { display: flex; gap: 0.4rem; align-items: center; flex-wrap: wrap; margin-bottom: 0.6rem; }
.detail-progress-wrap { margin-bottom: 0.75rem; }
.detail-progress-bar { margin-bottom: 0.25rem; }
.detail-path {
  font-size: 0.7rem; color: var(--muted); word-break: break-all;
  font-family: monospace; margin-bottom: 0.75rem; line-height: 1.4;
}
.edit-search-results {
  display: flex; gap: 0.5rem; overflow-x: auto; padding-bottom: 0.25rem; margin-bottom: 0.5rem;
  scrollbar-width: none;
}
.edit-search-results::-webkit-scrollbar { display: none; }
.edit-result-card {
  flex: none; width: 84px; cursor: pointer; border-radius: 8px; overflow: hidden;
  background: var(--card); border: 2px solid transparent;
}
.edit-result-card.selected { border-color: var(--accent); }
.edit-result-card img { width: 84px; height: 84px; object-fit: cover; display: block; }
.edit-result-card .edit-result-label {
  font-size: 0.62rem; padding: 3px 5px; line-height: 1.3;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
#detail-edit .field { margin-bottom: 0.65rem; }
#detail-edit label { display: block; font-size: 0.75rem; font-weight: 600; color: var(--muted); margin-bottom: 0.25rem; }
#detail-edit input, #detail-edit textarea {
  width: 100%; box-sizing: border-box;
  background: var(--bg); color: var(--fg); border: 1px solid var(--card);
  border-radius: 8px; padding: 0.5rem 0.6rem; font-size: 0.9rem; font-family: inherit;
}
#detail-edit textarea { resize: vertical; min-height: 90px; }

/* Metadata source badges on book rows */
.meta-badge {
  display: inline-block; font-size: 0.6rem; font-weight: 700; letter-spacing: 0.05em;
  text-transform: uppercase; border-radius: 3px; padding: 1px 4px; vertical-align: middle;
}
.meta-badge.itunes   { background: rgba(76,175,80,0.18);  color: #4caf50; }
.meta-badge.embedded { background: rgba(33,150,243,0.18); color: #5baef7; }
.meta-badge.manual   { background: rgba(255,152,0,0.18);  color: #ff9800; }
.meta-badge.searched { background: rgba(150,150,150,0.18);color: #888; }

/* Admin import progress */
.import-progress { margin-top: 0.4rem; }

/* Missing-book indicator in listen log */
.log-row--missing { opacity: 0.45; cursor: default; }
.missing-badge {
  display: inline-block; font-size: 0.68rem; font-weight: 600;
  background: var(--danger); color: #fff;
  border-radius: 4px; padding: 1px 5px; margin-left: 6px;
  vertical-align: middle; letter-spacing: 0.04em; text-transform: uppercase;
}
.log-finished {
  display: inline-block; font-size: 0.68rem; font-weight: 600;
  color: #56d364; margin-left: 6px; vertical-align: middle;
}

/* Toast notification */
.toast {
  position: fixed; bottom: calc(80px + var(--safe-bottom)); left: 50%; transform: translateX(-50%);
  background: #333; color: #fff; padding: 0.65rem 1.1rem;
  border-radius: 999px; font-size: 0.9rem; white-space: nowrap;
  z-index: 200; pointer-events: none;
  opacity: 0; transition: opacity 0.2s;
}
.toast--show { opacity: 1; }

@media (min-width: 720px) {
  .content, .tabs, .topbar { max-width: 720px; margin-left: auto; margin-right: auto; width: 100%; }
  .mini-player { max-width: 720px; left: 50%; transform: translateX(-50%); border-radius: 12px 12px 0 0; }
  .modal-sheet { max-width: 560px; margin: 0 auto; border-radius: 20px 20px 0 0; }
}
