:root {
  --wood: #241812;
  --wood-2: #2e2016;
  --wood-3: #1b120d;
  --paper: #f3ecda;
  --paper-2: #eae1c7;
  --paper-shadow: #c9ba92;
  --ink: #2b2013;
  --ink-soft: #5a4c36;
  --cream-text: #e7dac0;
  --cream-dim: #a8967a;
  --cream-faint: #6e6047;
  --brass: #c7963f;
  --brass-bright: #e0b15e;
  --brass-soft: rgba(199, 150, 63, 0.18);
  --brass-line: rgba(199, 150, 63, 0.4);
  --spine-1: #6e3b3b;
  --spine-2: #3b5249;
  --spine-3: #33475b;
  --spine-4: #7a5c2e;
  --spine-5: #4b3b57;
  --spine-6: #5c6b3e;
  --serif: "Source Serif 4", Georgia, "Noto Serif TC", serif;
  --sans:
    -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC",
    "Microsoft JhengHei", "Noto Sans TC", Helvetica, Arial, sans-serif;
  --radius: 10px;
}

* {
  box-sizing: border-box;
}
html,
body {
  margin: 0;
  padding: 0;
}
body {
  min-height: 100vh;
  font-family: var(--sans);
  color: var(--cream-text);
  background:
    radial-gradient(
      1000px 500px at 20% -10%,
      rgba(199, 150, 63, 0.07),
      transparent 60%
    ),
    radial-gradient(
      800px 600px at 100% 100%,
      rgba(199, 150, 63, 0.04),
      transparent 55%
    ),
    linear-gradient(180deg, var(--wood), var(--wood-2) 60%, var(--wood-3));
  -webkit-font-smoothing: antialiased;
}
::selection {
  background: var(--brass-soft);
  color: var(--cream-text);
}

button {
  font-family: inherit;
}

/* ============ Layout shells ============ */
.screen {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
.hidden {
  display: none !important;
}

/* ============ Top bar ============ */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 28px;
  border-bottom: 1px solid rgba(231, 218, 192, 0.08);
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}
.brand-mark {
  width: 26px;
  height: 26px;
  border-radius: 4px;
  background: linear-gradient(160deg, var(--brass-bright), var(--brass));
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(199, 150, 63, 0.35);
}
.brand-mark svg {
  width: 15px;
  height: 15px;
}
.brand-name {
  font-family: var(--serif);
  font-size: 17px;
  letter-spacing: 0.01em;
  color: var(--cream-text);
}
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.btn {
  font-size: 13.5px;
  font-weight: 600;
  border-radius: 8px;
  cursor: pointer;
  padding: 9px 16px;
  border: 1px solid rgba(231, 218, 192, 0.16);
  background: rgba(255, 255, 255, 0.03);
  color: var(--cream-text);
  transition: 0.15s;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.btn:hover {
  background: rgba(255, 255, 255, 0.06);
  border-color: var(--brass-line);
}
.btn.primary {
  background: linear-gradient(160deg, var(--brass-bright), var(--brass));
  color: #231703;
  border-color: transparent;
  font-weight: 700;
  box-shadow: 0 4px 14px rgba(199, 150, 63, 0.28);
}
.btn.primary:hover {
  filter: brightness(1.06);
}
.btn.ghost {
  background: transparent;
  border-color: rgba(231, 218, 192, 0.14);
}
.btn.small {
  padding: 6px 11px;
  font-size: 12.5px;
}
.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
.avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 1px solid rgba(231, 218, 192, 0.25);
}

/* ============ Gate (sign-in) screen ============ */
.gate {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
}
.gate-card {
  max-width: 420px;
  text-align: center;
}
.gate-emblem {
  width: 64px;
  height: 64px;
  margin: 0 auto 22px;
  border-radius: 14px;
  background: linear-gradient(160deg, var(--brass-bright), var(--brass));
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 10px 30px rgba(199, 150, 63, 0.3);
}
.gate-emblem svg {
  width: 32px;
  height: 32px;
}
.gate h1 {
  font-family: var(--serif);
  font-size: 26px;
  font-weight: 600;
  margin: 0 0 10px;
  color: var(--cream-text);
}
.gate p {
  color: var(--cream-dim);
  font-size: 14px;
  line-height: 1.7;
  margin: 0 0 26px;
}
.gate .btn.primary {
  padding: 12px 24px;
  font-size: 14px;
  margin: 0 auto;
}
.gate-foot {
  margin-top: 26px;
  font-size: 12px;
  color: var(--cream-faint);
  line-height: 1.8;
}

/* ============ Shelf (library) view ============ */
.shelf-wrap {
  flex: 1;
  padding: 8px 28px 60px;
}
.shelf-header {
  max-width: 1180px;
  margin: 22px auto 0;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 16px;
}
.shelf-title {
  font-family: var(--serif);
  font-size: 24px;
  color: var(--cream-text);
  margin: 0 0 4px;
}
.shelf-sub {
  font-size: 13px;
  color: var(--cream-dim);
  margin: 0;
}
.shelf-tools {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}
.search-box {
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(231, 218, 192, 0.12);
  border-radius: 8px;
  padding: 9px 13px;
  color: var(--cream-text);
  font-size: 13.5px;
  min-width: 180px;
}
.search-box::placeholder {
  color: var(--cream-faint);
}
.filter-chips {
  display: flex;
  gap: 6px;
}
.chip {
  font-size: 12px;
  padding: 7px 12px;
  border-radius: 99px;
  border: 1px solid rgba(231, 218, 192, 0.12);
  background: rgba(255, 255, 255, 0.02);
  color: var(--cream-dim);
  cursor: pointer;
  user-select: none;
  transition: 0.15s;
}
.chip:hover {
  border-color: var(--brass-line);
  color: var(--cream-text);
}
.chip.active {
  background: var(--brass-soft);
  border-color: var(--brass-line);
  color: var(--brass-bright);
}

.shelf-overall {
  max-width: 1180px;
  margin: 18px auto 6px;
  display: flex;
  align-items: center;
  gap: 14px;
}
.overall-track {
  flex: 1;
  height: 5px;
  border-radius: 99px;
  background: rgba(255, 255, 255, 0.06);
  overflow: hidden;
}
.overall-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--brass), var(--brass-bright));
  transition: width 0.5s ease;
}
.overall-label {
  font-size: 12px;
  color: var(--cream-faint);
  white-space: nowrap;
}

.shelf-grid {
  max-width: 1180px;
  margin: 26px auto 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(168px, 1fr));
  gap: 22px 20px;
}

.book {
  position: relative;
  cursor: pointer;
  perspective: 800px;
}
.book-cover {
  position: relative;
  height: 230px;
  border-radius: 3px 8px 8px 3px;
  padding: 18px 16px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  box-shadow:
    -3px 0 0 rgba(0, 0, 0, 0.25) inset,
    0 10px 22px rgba(0, 0, 0, 0.35);
  transition:
    transform 0.25s ease,
    box-shadow 0.25s ease;
  overflow: hidden;
}
.book:hover .book-cover {
  transform: translateY(-6px) rotate(-0.4deg);
  box-shadow:
    -3px 0 0 rgba(0, 0, 0, 0.25) inset,
    0 18px 34px rgba(0, 0, 0, 0.45);
}
.book-cover::before {
  /* spine highlight */
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 7px;
  background: linear-gradient(
    180deg,
    rgba(255, 255, 255, 0.18),
    rgba(0, 0, 0, 0.15)
  );
}
.book-cover::after {
  /* page edges on the right */
  content: "";
  position: absolute;
  right: 0;
  top: 3px;
  bottom: 3px;
  width: 5px;
  background: repeating-linear-gradient(
    180deg,
    #efe6cc 0px,
    #efe6cc 2px,
    #dccfa8 2px,
    #dccfa8 3px
  );
  border-radius: 0 3px 3px 0;
}
.book-title {
  font-family: var(--serif);
  font-size: 14.5px;
  line-height: 1.4;
  font-weight: 600;
  color: rgba(255, 255, 255, 0.94);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
  display: -webkit-box;
  -webkit-line-clamp: 5;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.book-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}
.book-pct {
  font-size: 10.5px;
  color: rgba(255, 255, 255, 0.75);
  font-variant-numeric: oldstyle-nums;
}
.book-mark {
  position: absolute;
  top: -2px;
  right: 16px;
  width: 20px;
  height: 30px;
  background: var(--brass);
  clip-path: polygon(0 0, 100% 0, 100% 100%, 50% 78%, 0 100%);
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.3);
  opacity: 0;
  transform: translateY(-6px);
  transition: 0.2s;
}
.book.done .book-mark {
  opacity: 1;
  transform: translateY(0);
}
.book-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.3);
  flex: none;
}
.book.reading .book-dot {
  background: #fff;
}
.book.done .book-dot {
  background: var(--brass-bright);
}
.book-remove {
  position: absolute;
  top: 7px;
  right: 11px;
  z-index: 3;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  background: rgba(0, 0, 0, 0.35);
  color: rgba(255, 255, 255, 0.9);
  font-size: 15px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: 0.15s;
}
.book:hover .book-remove {
  opacity: 1;
}
.book-remove:hover {
  background: rgba(0, 0, 0, 0.6);
}

.shelf-plank {
  max-width: 1180px;
  margin: 2px auto 40px;
  height: 14px;
  border-radius: 2px;
  background: linear-gradient(180deg, #4a331f, #3a2717);
  box-shadow: 0 8px 14px rgba(0, 0, 0, 0.35);
}

.empty-state {
  max-width: 1180px;
  margin: 40px auto;
  text-align: center;
  padding: 50px 20px;
  border: 1px dashed rgba(231, 218, 192, 0.18);
  border-radius: 12px;
  color: var(--cream-faint);
  font-size: 13.5px;
}

.footer-note {
  max-width: 1180px;
  margin: 10px auto 0;
  font-size: 11.5px;
  color: var(--cream-faint);
  text-align: center;
  line-height: 1.8;
}

/* ============ Reader ============ */
.reader {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: var(--wood-3);
  display: flex;
  flex-direction: column;
}
.reader-top {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 22px;
  border-bottom: 1px solid rgba(231, 218, 192, 0.08);
}
.reader-title {
  font-family: var(--serif);
  font-size: 14.5px;
  color: var(--cream-text);
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.reader-stage {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 26px;
  overflow: hidden;
  perspective: 1800px;
}
.book-open {
  position: relative;
  width: min(760px, 92vw);
  max-height: 82vh;
  background: var(--paper);
  border-radius: 2px 8px 8px 2px;
  box-shadow:
    -6px 0 0 var(--paper-shadow) inset,
    0 24px 60px rgba(0, 0, 0, 0.55);
}
.book-open::before {
  /* gutter shadow */
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 26px;
  background: linear-gradient(90deg, rgba(0, 0, 0, 0.16), transparent);
  pointer-events: none;
  z-index: 2;
}
.page-face {
  position: relative;
  padding: 16px 18px 16px 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  transform-origin: left center;
  backface-visibility: hidden;
}
.page-face canvas {
  max-width: 100%;
  max-height: 74vh;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.18);
  border-radius: 2px;
}
.page-face.flipping {
  animation: flipPage 0.38s ease;
}
@keyframes flipPage {
  0% {
    transform: rotateY(0deg);
    filter: brightness(1);
  }
  45% {
    transform: rotateY(-8deg);
    filter: brightness(0.92);
  }
  100% {
    transform: rotateY(0deg);
    filter: brightness(1);
  }
}
@media (prefers-reduced-motion: reduce) {
  .page-face.flipping {
    animation: none;
  }
}
.reader-loading {
  color: var(--ink-soft);
  font-family: var(--serif);
  font-size: 14px;
}

.reader-bottom {
  padding: 14px 22px 20px;
  border-top: 1px solid rgba(231, 218, 192, 0.08);
}
.page-edges {
  display: flex;
  gap: 2px;
  height: 20px;
  margin: 0 auto 12px;
  max-width: 520px;
  align-items: stretch;
  cursor: pointer;
}
.page-edges .tick {
  flex: 1;
  height: 100%;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 1px 1px 0 0;
  transition: background 0.15s;
}
.page-edges .tick.read {
  background: var(--brass);
}
.reader-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
}
.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid rgba(231, 218, 192, 0.14);
  background: rgba(255, 255, 255, 0.03);
  color: var(--cream-text);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
}
.icon-btn:hover {
  border-color: var(--brass-line);
  color: var(--brass-bright);
}
.icon-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}
.page-readout {
  font-family: var(--serif);
  font-size: 13.5px;
  color: var(--cream-dim);
  font-variant-numeric: oldstyle-nums;
  min-width: 70px;
  text-align: center;
}
.mark-done {
  margin-left: 10px;
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 12.5px;
  color: var(--cream-dim);
  cursor: pointer;
  user-select: none;
}
.mark-done input {
  accent-color: var(--brass);
}

/* ============ small helpers ============ */
.spinner {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.2);
  border-top-color: var(--brass-bright);
  animation: spin 0.7s linear infinite;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%);
  background: var(--wood-2);
  border: 1px solid rgba(231, 218, 192, 0.15);
  color: var(--cream-text);
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 13px;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.4);
  z-index: 200;
  opacity: 0;
  pointer-events: none;
  transition: 0.25s;
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(-4px);
}

@media (max-width: 640px) {
  .topbar,
  .shelf-wrap {
    padding-left: 16px;
    padding-right: 16px;
  }
  .shelf-grid {
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
    gap: 16px 12px;
  }
  .book-cover {
    height: 190px;
    padding: 14px 12px;
  }
}
