:root {
  --bg: #0f1115;
  --surface: #181b22;
  --surface-2: #222732;
  --text: #e9edf4;
  --muted: #8b93a3;
  --accent: #5b8cff;
  --danger: #ff5b6e;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC", "Microsoft JhengHei",
    Roboto, sans-serif;
  overscroll-behavior-y: none;
}

#app {
  min-height: 100vh;
  min-height: 100dvh;
}

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

img {
  display: block;
  max-width: 100%;
}

/* ---------- top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(var(--safe-top) + 10px) 14px 10px;
  background: rgba(15, 17, 21, 0.86);
  backdrop-filter: saturate(160%) blur(12px);
  border-bottom: 1px solid #00000040;
}
.topbar h1 {
  font-size: 18px;
  margin: 0;
  flex: 1;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topbar .back {
  font-size: 22px;
  width: 32px;
  margin-left: -6px;
}
.avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--surface-2);
  object-fit: cover;
}
.icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  font-size: 20px;
  display: grid;
  place-items: center;
  background: var(--surface);
}

/* ---------- content ---------- */
.content {
  padding: 14px 14px calc(var(--safe-bottom) + 80px);
}

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

/* folder card */
.folder {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px 12px;
  aspect-ratio: 4 / 3;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  position: relative;
}
.folder .fi {
  font-size: 30px;
}
.folder .fname {
  font-weight: 600;
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.folder .fcount {
  color: var(--muted);
  font-size: 12px;
}

/* book cover */
.book {
  position: relative;
}
.book .cover {
  width: 100%;
  aspect-ratio: 3 / 4;
  object-fit: cover;
  border-radius: var(--radius);
  background: var(--surface-2);
}
.book .cover.placeholder {
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: 12px;
  text-align: center;
  padding: 8px;
}
.book .btitle {
  font-size: 12.5px;
  margin-top: 6px;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.book .badge {
  position: absolute;
  top: 6px;
  left: 6px;
  background: #000000aa;
  color: #fff;
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 20px;
}

/* red "new chapters" badge (top-right of folder & book cards) */
.update-badge {
  position: absolute;
  top: -6px;
  right: -6px;
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: 11px;
  background: var(--danger);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  line-height: 22px;
  text-align: center;
  box-shadow: 0 2px 8px #000a;
  z-index: 5;
  animation: badge-pop 0.4s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}
@keyframes badge-pop {
  from { opacity: 0; transform: scale(0); }
  to { opacity: 1; transform: none; }
}

/* add tile */
.add-tile {
  border: 1.5px dashed #3a4150;
  border-radius: var(--radius);
  aspect-ratio: 3 / 4;
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: 13px;
  background: transparent;
  width: 100%;
}
.add-tile .plus {
  font-size: 28px;
}

.empty {
  text-align: center;
  color: var(--muted);
  padding: 60px 20px;
  line-height: 1.6;
}

/* ---------- floating action ---------- */
.fab {
  position: fixed;
  right: 18px;
  bottom: calc(var(--safe-bottom) + 18px);
  width: 56px;
  height: 56px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 30px;
  line-height: 1;
  font-weight: 400;
  display: grid;
  place-items: center;
  box-shadow: 0 8px 24px #0008;
  z-index: 30;
  animation: fab-pop 0.55s cubic-bezier(0.34, 1.56, 0.64, 1) 0.15s backwards;
  transition: transform 0.16s ease, box-shadow 0.2s ease;
}
.fab:active {
  transform: scale(0.88) rotate(90deg);
  box-shadow: 0 4px 14px #0008;
}
@keyframes fab-pop {
  from { opacity: 0; transform: scale(0) rotate(-120deg); }
  to { opacity: 1; transform: none; }
}

/* ---------- login ---------- */
.login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
  text-align: center;
}
.login .logo {
  font-size: 56px;
  margin-bottom: 10px;
}
.login h1 {
  margin: 0 0 6px;
}
.login p {
  color: var(--muted);
  margin: 0 0 28px;
}
.gbtn {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  background: #fff;
  color: #1f1f1f;
  font-weight: 600;
  padding: 12px 22px;
  border-radius: 30px;
}

/* ---------- reader ---------- */
.reader {
  background: #000;
  min-height: 100dvh;
}
.reader .page {
  width: 100%;
  display: block;
}
.reader .loading,
.reader .end {
  color: #aaa;
  text-align: center;
  padding: 40px 20px;
}

/* bottom-of-chapter navigation (上一集 / 目錄 / 下一集) */
.chapter-end {
  padding: 10px 16px calc(var(--safe-bottom) + 90px);
}
.chapter-end .end {
  padding: 24px 20px 18px;
}
.end-nav {
  display: flex;
  gap: 10px;
  max-width: 520px;
  margin: 0 auto;
}
.end-btn {
  flex: 1;
  background: var(--surface);
  color: var(--text);
  padding: 15px 10px;
  border-radius: 12px;
  font-weight: 600;
  font-size: 15px;
  white-space: nowrap;
  box-shadow: 0 4px 14px #0006;
  transition: transform 0.13s ease, filter 0.2s ease;
}
.end-btn.primary {
  background: var(--accent);
  color: #fff;
}
.end-btn:active {
  transform: scale(0.95);
  filter: brightness(1.08);
}

/* the single control dot — pinned to the bottom-right corner, always on top */
.dot {
  position: fixed;
  right: 16px;
  bottom: calc(var(--safe-bottom) + 16px);
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #ffffffdd;
  box-shadow: 0 0 0 8px #ffffff22, 0 2px 10px #000a;
  z-index: 45;
  cursor: pointer;
  transition: transform 0.12s;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}
.dot:active {
  transform: scale(1.15);
}
/* enlarge the tap/drag target well beyond the visible circle */
.dot::after {
  content: '';
  position: absolute;
  inset: -14px;
  border-radius: 50%;
}
/* backdrop: just a light dim + tap-to-close, the menu itself hangs off the dot */
.reader-menu {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: #00000055;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}
.reader-menu.open {
  opacity: 1;
  pointer-events: auto;
}

/* the pill stack that pops out of the dot */
.dot-fan {
  position: fixed;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 10px;
}
.dot-fan.down {
  flex-direction: column-reverse;
}
.dot-fan > * {
  /* start collapsed into the dot (which sits at the bottom-right of the stack) */
  transform-origin: bottom right;
  transform: scale(0.2) translateY(24px);
  opacity: 0;
  transition: transform 0.32s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.22s ease;
}
.dot-fan.down > * {
  transform-origin: top right;
  transform: scale(0.2) translateY(-24px);
}
.reader-menu.open .dot-fan > * {
  transform: none;
  opacity: 1;
}
/* stagger so they spring out one after another, nearest the dot first */
.reader-menu.open .dot-fan > *:nth-last-child(1) { transition-delay: 0.00s; }
.reader-menu.open .dot-fan > *:nth-last-child(2) { transition-delay: 0.05s; }
.reader-menu.open .dot-fan > *:nth-last-child(3) { transition-delay: 0.10s; }
.reader-menu.open .dot-fan > *:nth-last-child(4) { transition-delay: 0.15s; }
.reader-menu.open .dot-fan > *:nth-last-child(5) { transition-delay: 0.20s; }

.fan-item {
  border: 0;
  background: var(--surface);
  color: var(--text);
  padding: 13px 20px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 15px;
  white-space: nowrap;
  box-shadow: 0 6px 20px #0007;
  cursor: pointer;
}
.fan-item:active {
  transform: scale(0.94);
}
.fan-item.primary {
  background: var(--accent);
  color: #fff;
}
.fan-item.ghost {
  background: var(--surface-2);
  color: var(--muted);
  font-size: 14px;
}
.fan-title {
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  background: #000000aa;
  color: #fff;
  padding: 7px 14px;
  border-radius: 999px;
  font-size: 12px;
}
.reader-menu .ch-title {
  text-align: center;
  color: var(--muted);
  font-size: 13px;
  padding: 4px 0 12px;
}
.nav-row {
  display: flex;
  gap: 10px;
}
.nav-row button {
  flex: 1;
  background: var(--surface-2);
  padding: 14px;
  border-radius: 12px;
  font-weight: 600;
  font-size: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.nav-row button.primary {
  background: var(--accent);
  color: #fff;
}
.nav-row button:disabled {
  opacity: 0.35;
}
.toc-btn {
  width: 100%;
  margin-top: 10px;
  background: var(--surface-2);
  padding: 14px;
  border-radius: 12px;
  font-weight: 600;
}

/* ---------- modal / sheet ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: #000000aa;
  display: flex;
  align-items: flex-end;
  animation: fade-in 0.25s ease backwards;
}
.modal.closing {
  animation: fade-out 0.28s ease forwards;
}
.modal .panel {
  width: 100%;
  background: var(--surface);
  border-radius: 18px 18px 0 0;
  padding: 18px 16px calc(var(--safe-bottom) + 18px);
  max-height: 85dvh;
  overflow: auto;
  animation: sheet-up 0.4s cubic-bezier(0.22, 1, 0.36, 1) backwards;
}
.modal.closing .panel {
  animation: sheet-down 0.28s cubic-bezier(0.4, 0, 1, 1) forwards;
}
.modal .panel > * {
  animation: row-rise 0.4s cubic-bezier(0.22, 1, 0.36, 1) backwards;
}
.modal .panel > *:nth-child(1) { animation-delay: 0.06s; }
.modal .panel > *:nth-child(2) { animation-delay: 0.11s; }
.modal .panel > *:nth-child(3) { animation-delay: 0.16s; }
.modal .panel > *:nth-child(4) { animation-delay: 0.21s; }
.modal .panel > *:nth-child(n + 5) { animation-delay: 0.26s; }
@keyframes sheet-up { from { transform: translateY(100%); } to { transform: none; } }
@keyframes sheet-down { from { transform: none; } to { transform: translateY(105%); } }
@keyframes row-rise { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }
.modal h2 {
  margin: 0 0 14px;
  font-size: 18px;
}
.field {
  width: 100%;
  background: var(--surface-2);
  border-radius: 12px;
  padding: 14px;
  color: var(--text);
  border: 1px solid #ffffff14;
  margin-bottom: 12px;
  font-size: 16px; /* >=16px stops iOS zoom-on-focus */
}
.btn {
  width: 100%;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  padding: 15px;
  border-radius: 12px;
  font-size: 16px;
}
.btn.ghost {
  background: var(--surface-2);
  color: var(--text);
}
.btn.danger {
  background: var(--danger);
}
.btn + .btn {
  margin-top: 10px;
}
.preview {
  display: flex;
  gap: 12px;
  background: var(--surface-2);
  border-radius: 12px;
  padding: 12px;
  margin-bottom: 14px;
}
.preview img {
  width: 70px;
  height: 94px;
  object-fit: cover;
  border-radius: 8px;
  background: var(--bg);
  flex: none;
}
.preview .pt {
  font-weight: 600;
  font-size: 15px;
  line-height: 1.35;
}
.preview .ps {
  color: var(--muted);
  font-size: 13px;
  margin-top: 6px;
}

/* table of contents list */
.toc-list {
  list-style: none;
  margin: 0;
  padding: 0;
}
.toc-list li {
  padding: 14px 6px;
  border-bottom: 1px solid #ffffff10;
  font-size: 15px;
}
.toc-list li.current {
  color: var(--accent);
  font-weight: 700;
}

.spinner {
  width: 26px;
  height: 26px;
  border: 3px solid #ffffff30;
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 30px auto;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--safe-bottom) + 90px);
  transform: translateX(-50%);
  background: #000000e0;
  color: #fff;
  padding: 10px 18px;
  border-radius: 22px;
  font-size: 14px;
  z-index: 100;
  max-width: 90vw;
  text-align: center;
  animation: toast-in 0.34s cubic-bezier(0.34, 1.56, 0.64, 1) backwards;
}
.toast.out {
  animation: toast-out 0.28s cubic-bezier(0.4, 0, 1, 1) forwards;
}
@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, 24px) scale(0.9); }
  to { opacity: 1; transform: translate(-50%, 0) scale(1); }
}
@keyframes toast-out {
  to { opacity: 0; transform: translate(-50%, 14px) scale(0.96); }
}

/* ============================================================
   Motion system — applied across every view & interaction
   ============================================================ */
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes fade-out { from { opacity: 1; } to { opacity: 0; } }

/* --- top bar drops in --- */
.topbar {
  animation: bar-in 0.42s cubic-bezier(0.22, 1, 0.36, 1) backwards;
}
@keyframes bar-in {
  from { opacity: 0; transform: translateY(-14px); }
  to { opacity: 1; transform: none; }
}

/* --- view enter, directional (forward slides from right, back from left) --- */
.content.nav-fwd { animation: view-fwd 0.42s cubic-bezier(0.22, 1, 0.36, 1) backwards; }
.content.nav-back { animation: view-back 0.42s cubic-bezier(0.22, 1, 0.36, 1) backwards; }
.reader.nav-fwd { animation: view-fwd 0.4s cubic-bezier(0.22, 1, 0.36, 1) backwards; }
.reader.nav-back { animation: view-back 0.4s cubic-bezier(0.22, 1, 0.36, 1) backwards; }
@keyframes view-fwd {
  from { opacity: 0; transform: translateX(26px); }
  to { opacity: 1; transform: none; }
}
@keyframes view-back {
  from { opacity: 0; transform: translateX(-26px); }
  to { opacity: 1; transform: none; }
}

/* --- cards spring in with a per-card stagger (--i set in JS) --- */
.folder, .book {
  animation: card-in 0.5s cubic-bezier(0.34, 1.56, 0.64, 1) backwards;
  animation-delay: calc(var(--i, 0) * 45ms + 0.1s);
  transition: transform 0.16s ease, box-shadow 0.22s ease;
}
@keyframes card-in {
  from { opacity: 0; transform: translateY(20px) scale(0.9); }
  to { opacity: 1; transform: none; }
}
.folder:active, .book:active {
  transform: scale(0.95);
}
.book:active .cover {
  box-shadow: 0 4px 18px #000a;
}

/* --- tactile press feedback on all buttons --- */
.btn, .gbtn, .icon-btn, .topbar .back {
  transition: transform 0.13s ease, filter 0.2s ease;
}
.btn:active, .gbtn:active, .icon-btn:active, .topbar .back:active {
  transform: scale(0.94);
  filter: brightness(1.08);
}
.toc-list li { transition: background 0.15s ease, padding-left 0.15s ease; }
.toc-list li:active { background: #ffffff14; padding-left: 12px; }

/* --- images fade + settle in once decoded --- */
/* Opacity-only fade. No scale: a transformed image's box swells past its
   layout slot and visibly overlaps the pages above/below while fading in
   (very noticeable on long webtoons with many tiles, e.g. rouman). */
.fade-img {
  opacity: 0;
  transition: opacity 0.5s ease;
}
.fade-img.in {
  opacity: 1;
}

/* --- login screen entrance --- */
.login > div > * {
  animation: row-rise 0.55s cubic-bezier(0.22, 1, 0.36, 1) backwards;
}
.login .logo { animation: pop-spin 0.7s cubic-bezier(0.34, 1.56, 0.64, 1) backwards; }
.login h1 { animation-delay: 0.12s; }
.login p { animation-delay: 0.2s; }
.login .gbtn, .login .empty { animation-delay: 0.3s; }
@keyframes pop-spin {
  from { opacity: 0; transform: scale(0.3) rotate(-25deg); }
  to { opacity: 1; transform: none; }
}

/* --- avatar settles in --- */
.avatar, .topbar .icon-btn {
  animation: pop-spin 0.5s cubic-bezier(0.34, 1.56, 0.64, 1) 0.15s backwards;
}

/* respect users who prefer less motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001s !important;
    animation-delay: 0s !important;
    transition-duration: 0.001s !important;
  }
}
