/* ✨ Lottie's Fairy Bookshelf — Rainbow Magic princess theme ✨ */

:root {
  --pink: #f783ac;
  --pink-deep: #e64980;
  --gold: #ffd43b;
  --lilac: #b197fc;
  --sky: #74c0fc;
  --mint: #63e6be;
  --ink: #5f3160;
  --ink-soft: #8a5a8b;
  --paper: #fff5fa;
  --card: #ffffff;
  --rainbow: linear-gradient(90deg, #ff6b6b, #ffa94d, #ffd43b, #69db7c, #4dabf7, #b197fc, #f783ac);
  --shadow: 0 6px 18px rgba(95, 49, 96, 0.14);
  --radius: 20px;
  --font-display: "Chewy", cursive;
  --font-body: "Quicksand", "Segoe UI", sans-serif;
}

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

[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  font-family: var(--font-body);
  color: var(--ink);
  background:
    radial-gradient(circle at 15% 10%, rgba(255, 212, 59, 0.18), transparent 40%),
    radial-gradient(circle at 85% 20%, rgba(177, 151, 252, 0.20), transparent 45%),
    radial-gradient(circle at 50% 90%, rgba(116, 192, 252, 0.16), transparent 50%),
    var(--paper);
  min-height: 100vh;
  padding-bottom: 84px;
  overflow-x: hidden;
}

/* ---------- floating sparkles ---------- */
.sparkle-layer { position: fixed; inset: 0; pointer-events: none; z-index: 0; }
.sparkle-layer i {
  position: absolute;
  font-style: normal;
  animation: floaty linear infinite;
  opacity: 0.5;
}
@keyframes floaty {
  0% { transform: translateY(105vh) rotate(0deg); }
  100% { transform: translateY(-10vh) rotate(360deg); }
}

/* ---------- header ---------- */
.app-header {
  position: relative;
  z-index: 1;
  padding: calc(18px + env(safe-area-inset-top)) calc(48px + env(safe-area-inset-right)) 14px calc(48px + env(safe-area-inset-left));
  text-align: center;
  background: linear-gradient(160deg, rgba(247, 131, 172, 0.95), rgba(177, 151, 252, 0.92));
  border-radius: 0 0 28px 28px;
  box-shadow: var(--shadow);
}
.header-top {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}
.app-header h1 {
  font-family: var(--font-display);
  font-size: clamp(1.5rem, 5.5vw, 2.4rem);
  color: #fff;
  text-shadow: 0 2px 0 rgba(95, 49, 96, 0.25);
  letter-spacing: 0.5px;
}
.tiara { font-size: 1.6rem; animation: bob 2.5s ease-in-out infinite; }
.tiara.flip { animation-delay: 1.2s; }
@keyframes bob { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-6px); } }

.settings-btn {
  position: absolute;
  top: calc(12px + env(safe-area-inset-top));
  right: calc(10px + env(safe-area-inset-right));
  z-index: 2;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: #fff;
  font-size: 1.35rem;
  line-height: 1;
  cursor: pointer;
  text-shadow: 0 2px 3px rgba(95, 49, 96, 0.35);
  transition: background 0.15s ease, transform 0.15s ease;
}
.settings-btn:hover, .settings-btn:focus-visible, .settings-btn.active { background: rgba(255, 255, 255, 0.2); }
.settings-btn:active { transform: scale(0.92); }

.progress-wrap { margin-top: 10px; }
.progress-bar {
  height: 14px;
  background: rgba(255, 255, 255, 0.4);
  border-radius: 999px;
  overflow: hidden;
  border: 2px solid rgba(255, 255, 255, 0.7);
}
.progress-fill {
  height: 100%;
  width: 0;
  background: var(--rainbow);
  background-size: 200% 100%;
  border-radius: 999px;
  transition: width 0.7s cubic-bezier(0.22, 1, 0.36, 1);
  animation: shimmer 4s linear infinite;
}
@keyframes shimmer { to { background-position: 200% 0; } }
.progress-text { color: #fff; font-weight: 700; font-size: 0.85rem; margin-top: 6px; }

/* ---------- views ---------- */
main { position: relative; z-index: 1; max-width: 1100px; margin: 0 auto; padding: 14px; }
.view { display: none; animation: fadeUp 0.35s ease; }
.view.active { display: block; }
@keyframes fadeUp { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }

.view-title {
  font-family: var(--font-display);
  font-size: 1.6rem;
  margin: 6px 4px 14px;
}
.view-intro {
  margin: -8px 4px 14px;
  color: var(--ink-soft);
  font-size: 0.82rem;
  font-weight: 700;
}
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ---------- toolbar ---------- */
.toolbar { display: flex; flex-direction: column; gap: 10px; margin-bottom: 14px; }
.search-box {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--card);
  border-radius: 999px;
  padding: 10px 16px;
  box-shadow: var(--shadow);
  border: 2px solid rgba(247, 131, 172, 0.35);
}
.search-box input {
  flex: 1;
  border: none;
  outline: none;
  font-family: var(--font-body);
  font-size: 1rem;
  font-weight: 600;
  color: var(--ink);
  background: transparent;
}
.search-box input::placeholder { color: var(--ink-soft); opacity: 0.7; }
.filter-row { display: flex; width: 100%; min-width: 0; gap: 10px; }
.filter-row select {
  flex: 1 1 0;
  width: 0;
  min-width: 0;
  font-family: var(--font-body);
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--ink);
  padding: 10px 34px 10px 12px;
  border-radius: 14px;
  border: 2px solid rgba(177, 151, 252, 0.45);
  appearance: none;
  background-color: var(--card);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='none' stroke='%235f3160' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.8' d='m1 1.5 5 5 5-5'/%3E%3C/svg%3E");
  background-position: right 12px center;
  background-repeat: no-repeat;
  background-size: 12px 8px;
  box-shadow: var(--shadow);
}
.favourites-toolbar { max-width: 280px; }
.read-sort-row { max-width: 280px; }

/* ---------- book grid & cards ---------- */
.book-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 14px;
}
@media (min-width: 640px) {
  .book-grid { grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 18px; }
}

.book-card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  display: flex;
  flex-direction: column;
  border: 2px solid transparent;
}
.book-card:hover { transform: translateY(-4px) rotate(-0.5deg); box-shadow: 0 12px 26px rgba(95, 49, 96, 0.2); }
.book-card.is-owned { border-color: var(--lilac); }
.book-card.is-read { border-color: var(--gold); }

.cover {
  position: relative;
  aspect-ratio: 2 / 3;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 12px;
  text-align: center;
  color: #fff;
  overflow: hidden;
}
.cover::before {
  content: "";
  position: absolute;
  inset: 0;
  background:
    radial-gradient(circle at 20% 15%, rgba(255,255,255,0.35) 0 6px, transparent 7px),
    radial-gradient(circle at 80% 25%, rgba(255,255,255,0.3) 0 4px, transparent 5px),
    radial-gradient(circle at 30% 80%, rgba(255,255,255,0.3) 0 5px, transparent 6px),
    radial-gradient(circle at 70% 65%, rgba(255,255,255,0.25) 0 3px, transparent 4px);
}
.cover-fallback {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 12px;
}
.cover-image {
  position: absolute;
  inset: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #fff;
  opacity: 0;
  transition: opacity 0.2s ease;
}
.cover.has-image .cover-image { opacity: 1; }
.cover.has-image::before, .cover.has-image .cover-fallback { opacity: 0; }
.cover-emoji { font-size: 2.8rem; filter: drop-shadow(0 3px 4px rgba(0,0,0,0.25)); z-index: 1; }
.cover-name {
  font-family: var(--font-display);
  font-size: 1.05rem;
  line-height: 1.15;
  text-shadow: 0 2px 3px rgba(0,0,0,0.3);
  z-index: 1;
}
.cover-series {
  font-size: 0.62rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  background: rgba(255,255,255,0.28);
  padding: 3px 8px;
  border-radius: 999px;
  z-index: 1;
}
.cover-num {
  position: absolute;
  top: 8px;
  left: 8px;
  background: rgba(255,255,255,0.9);
  color: var(--ink);
  font-weight: 700;
  font-size: 0.7rem;
  border-radius: 999px;
  padding: 3px 8px;
  z-index: 3;
}
.read-stamp {
  position: absolute;
  top: 8px;
  right: 8px;
  font-size: 1.3rem;
  z-index: 3;
  filter: drop-shadow(0 2px 2px rgba(0,0,0,0.3));
  animation: stampIn 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.favourite-position {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 3;
  width: 34px;
  max-width: 20%;
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  border: 2px solid rgba(255,255,255,0.9);
  border-radius: 50%;
  background: linear-gradient(145deg, var(--pink-deep), #9c36b5);
  color: #fff;
  box-shadow: 0 2px 6px rgba(95,49,96,0.35);
  font-size: clamp(0.5rem, 2.2vw, 0.7rem);
  font-weight: 800;
  line-height: 1;
}
.cover.has-favourite-position .read-stamp { top: auto; bottom: 8px; }
@keyframes stampIn { from { transform: scale(2.5); opacity: 0; } to { transform: scale(1); opacity: 1; } }

.card-actions {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 38px;
  align-items: center;
  padding: 8px 10px;
  gap: 6px;
}
.favourite-order-controls {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 6px;
  padding: 8px 10px 0;
}
.order-btn, .drag-handle {
  min-width: 34px;
  height: 30px;
  border: 0;
  border-radius: 999px;
  background: #f3ecf7;
  color: var(--ink);
  font-family: var(--font-body);
  font-weight: 800;
  cursor: pointer;
}
.order-btn:disabled { opacity: 0.35; cursor: default; }
.order-number { min-width: 2.2rem; text-align: center; font-size: 0.75rem; font-weight: 800; color: var(--ink-soft); }
.drag-handle {
  grid-column: 1 / -1;
  width: 100%;
  color: #fff;
  background: linear-gradient(90deg, var(--pink), var(--lilac));
  letter-spacing: 2px;
  touch-action: none;
  cursor: grab;
}
.drag-handle:active { cursor: grabbing; }
.favourite-card.dragging { opacity: 0.55; transform: scale(0.97); }
.favourite-card.drag-target { outline: 3px dashed var(--pink); outline-offset: -5px; }
.own-btn, .fav-btn, .read-btn {
  border: none;
  cursor: pointer;
  font-family: var(--font-body);
}
.own-btn {
  min-width: 0;
  min-height: 34px;
  border-radius: 999px;
  padding: 7px 8px;
  overflow: hidden;
  background: #f3ecf7;
  color: var(--ink);
  font-size: 0.72rem;
  font-weight: 800;
  white-space: nowrap;
}
.own-btn.done { background: linear-gradient(90deg, var(--lilac), var(--sky)); color: #fff; }
.fav-btn {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: #fff0f6;
  font-size: 1.25rem;
  transition: transform 0.15s ease;
  line-height: 1;
}
.fav-btn:active { transform: scale(1.4); }
.read-btn {
  grid-column: 1 / -1;
  font-weight: 700;
  font-size: 0.78rem;
  padding: 8px 6px;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--mint), var(--sky));
  color: #14504a;
  transition: filter 0.15s ease;
}
.read-btn:active { filter: brightness(0.92); }
.read-btn.done { background: linear-gradient(90deg, var(--gold), #ffe066); color: #7a5b00; }

.empty-msg {
  text-align: center;
  font-weight: 700;
  color: var(--ink-soft);
  padding: 40px 20px;
  font-size: 1.05rem;
}

/* ---------- badges ---------- */
.badge-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 18px;
}
.stat-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 14px 8px;
  text-align: center;
  box-shadow: var(--shadow);
}
.stat-card .num { font-family: var(--font-display); font-size: 1.7rem; color: var(--pink-deep); }
.stat-card .lbl { font-size: 0.72rem; font-weight: 700; color: var(--ink-soft); text-transform: uppercase; letter-spacing: 0.5px; }

.badge-heading { font-family: var(--font-display); font-size: 1.2rem; margin: 16px 4px 10px; }
.badge-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 12px;
}
.badge-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px 10px;
  text-align: center;
  box-shadow: var(--shadow);
  border: 2px solid transparent;
}
.badge-card.earned { border-color: var(--gold); background: linear-gradient(160deg, #fffbe6, #fff); }
.badge-card.locked { filter: grayscale(1); opacity: 0.5; }
.badge-emoji { font-size: 2.2rem; }
.badge-card.earned .badge-emoji { animation: bob 2s ease-in-out infinite; display: inline-block; }
.badge-name { font-family: var(--font-display); font-size: 0.98rem; margin-top: 6px; }
.badge-desc { font-size: 0.74rem; font-weight: 600; color: var(--ink-soft); margin-top: 4px; }

/* ---------- settings ---------- */
.settings-card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  margin-bottom: 14px;
}
.settings-card h3 { font-family: var(--font-display); margin-bottom: 6px; }
.settings-card p { font-size: 0.88rem; font-weight: 600; color: var(--ink-soft); margin-bottom: 12px; }
.settings-card.danger { border: 2px solid rgba(230, 73, 128, 0.3); }
.settings-label {
  display: block;
  margin-bottom: 6px;
  font-size: 0.78rem;
  font-weight: 800;
  color: var(--ink-soft);
}
.settings-input-row { display: flex; align-items: center; gap: 8px; }
.settings-input-row input {
  min-width: 0;
  flex: 1;
  border: 2px solid rgba(177, 151, 252, 0.45);
  border-radius: 14px;
  padding: 11px 12px;
  color: var(--ink);
  background: #fff;
  font-family: var(--font-body);
  font-size: 1rem;
  font-weight: 700;
}
.settings-input-row .btn { flex: 0 0 auto; }
.settings-status { display: block; min-height: 1.2em; margin-top: 6px; color: #2b8a3e; font-size: 0.75rem; font-weight: 700; }
.credits { text-align: center; font-size: 0.78rem; font-weight: 600; color: var(--ink-soft); margin-top: 22px; line-height: 1.6; }

.btn {
  font-family: var(--font-body);
  font-weight: 700;
  font-size: 0.95rem;
  border: none;
  border-radius: 999px;
  padding: 12px 22px;
  cursor: pointer;
  transition: transform 0.15s ease, filter 0.15s ease;
}
.btn:active { transform: scale(0.96); }
.btn-primary { background: linear-gradient(90deg, var(--pink), var(--lilac)); color: #fff; box-shadow: 0 4px 12px rgba(230, 73, 128, 0.35); }
.btn-danger { background: #ffe3ec; color: var(--pink-deep); }

/* ---------- bottom nav ---------- */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 10;
  display: flex;
  background: rgba(255, 255, 255, 0.94);
  backdrop-filter: blur(10px);
  border-top: 2px solid rgba(247, 131, 172, 0.25);
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
}
.nav-btn {
  flex: 1;
  border: none;
  background: none;
  font-family: var(--font-body);
  font-weight: 700;
  font-size: 0.64rem;
  color: var(--ink-soft);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px 0;
  cursor: pointer;
  border-radius: 14px;
  transition: color 0.15s ease, background 0.15s ease;
}
.nav-btn span { font-size: 1.45rem; transition: transform 0.2s ease; }
.nav-btn.active { color: var(--pink-deep); background: rgba(247, 131, 172, 0.12); }
.nav-btn.active span { transform: scale(1.18); }
.page-back-to-top {
  position: fixed;
  right: calc(14px + env(safe-area-inset-right));
  bottom: calc(76px + env(safe-area-inset-bottom));
  z-index: 9;
  min-height: 44px;
  border: 2px solid rgba(255,255,255,0.9);
  border-radius: 999px;
  padding: 8px 15px;
  background: linear-gradient(90deg, var(--pink-deep), #9c36b5);
  box-shadow: 0 4px 16px rgba(95,49,96,0.35);
  color: #fff;
  font: 800 0.8rem var(--font-body);
  cursor: pointer;
}
.page-back-to-top span { font-size: 1.05rem; }

/* ---------- modal ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(95, 49, 96, 0.45);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
}
.modal {
  position: relative;
  background: var(--card);
  border-radius: 26px;
  max-width: 420px;
  width: 100%;
  max-height: 86vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 50px rgba(0,0,0,0.3);
  animation: popIn 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes popIn { from { transform: scale(0.8); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.modal-toolbar {
  position: sticky;
  top: 0;
  z-index: 5;
  height: 0;
  display: flex;
  justify-content: flex-end;
  padding: 10px 12px 0;
  pointer-events: none;
}
.modal-scroll {
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
}
.modal .cover { aspect-ratio: 2 / 3; padding: 0; border-radius: 26px 26px 0 0; }
.modal .read-stamp { top: 14px; right: 62px; }
.modal .cover-emoji { font-size: 4rem; }
.modal .cover-name { font-size: 1.5rem; }
.modal-body { padding: 18px; }
.modal-body .series-tag {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 700;
  background: rgba(177, 151, 252, 0.18);
  color: #6741d9;
  padding: 4px 10px;
  border-radius: 999px;
  margin-bottom: 10px;
}
.modal-body .bio { font-size: 0.92rem; font-weight: 600; line-height: 1.55; color: var(--ink-soft); }
.cover-source { display: inline-block; margin-top: 10px; color: #6741d9; font-size: 0.78rem; font-weight: 700; }
.modal-body .read-date {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 6px 10px;
  color: #7a5b00;
  background: #fff8db;
  border-radius: 12px;
  padding: 10px 12px;
  margin-top: 12px;
  font-size: 0.8rem;
  font-weight: 700;
}
.read-date input {
  min-width: 0;
  width: 100%;
  border: 1px solid rgba(122,91,0,0.3);
  border-radius: 9px;
  padding: 6px 8px;
  color: var(--ink);
  background: #fff;
  font: 700 0.8rem var(--font-body);
}
.read-date-status { grid-column: 1 / -1; color: var(--ink-soft); font-size: 0.68rem; }
.modal-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin-top: 16px; }
.modal-actions .btn { flex: 1; font-size: 0.88rem; padding: 12px 10px; }
.modal-actions [data-act="read"] { grid-column: 1 / -1; }
.btn-owned { background: linear-gradient(90deg, var(--lilac), var(--sky)); color: #fff; }
.btn-ghost { background: #f3ecf7; color: var(--ink); }
.modal-close {
  border: none;
  background: rgba(255,255,255,0.92);
  width: 40px;
  height: 40px;
  border-radius: 50%;
  box-shadow: 0 3px 10px rgba(95,49,96,0.28);
  font-size: 1.1rem;
  cursor: pointer;
  font-weight: 700;
  color: var(--ink);
  pointer-events: auto;
}

/* ---------- badge popup ---------- */
.badge-popup {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: rgba(95, 49, 96, 0.55);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.badge-popup-inner {
  background: linear-gradient(160deg, #fffbe6, #fff);
  border: 3px solid var(--gold);
  border-radius: 30px;
  text-align: center;
  padding: 30px 26px;
  max-width: 340px;
  width: 100%;
  animation: popIn 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
  box-shadow: 0 0 60px rgba(255, 212, 59, 0.5);
}
.badge-popup-emoji { font-size: 4.5rem; animation: bob 1.6s ease-in-out infinite; }
.badge-popup-inner h2 { font-family: var(--font-display); font-size: 1.6rem; margin: 10px 0 6px; color: var(--pink-deep); }
.badge-popup-inner p { font-weight: 600; color: var(--ink-soft); margin-bottom: 18px; }

/* ---------- confetti ---------- */
#confetti { position: fixed; inset: 0; pointer-events: none; z-index: 40; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

@media (max-width: 420px) {
  .nav-btn { font-size: 0.58rem; }
  .nav-btn span { font-size: 1.3rem; }
  .settings-input-row { align-items: stretch; flex-direction: column; }
  .settings-input-row .btn { width: 100%; }
}
