:root {
  --gold: #d9b26a;
  --gold-bright: #f3d99a;
  --ink: #12100d;
  --paper: #f5efe2;
  --panel: rgba(12, 10, 8, 0.72);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  background: #0b0a08;
  color: #eee6d5;
  font-family: Georgia, 'Times New Roman', serif;
  overflow: hidden;
}

.hidden { display: none !important; }

/* ---------- LIBRARY ---------- */

.library {
  position: relative;
  width: 100%;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

/* Use the dynamic viewport on mobile browsers (URL bar shows/hides) */
@supports (height: 100dvh) {
  .library, .reader { height: 100dvh; }
}

.library-bg, .reader-bg {
  position: absolute;
  inset: 0;
  background-image: url("Assets/Background_Website.webp");
  background-size: cover;
  background-position: center;
  filter: brightness(0.62) saturate(1.05);
  z-index: 0;
}

/* Keep the library scene bright — the night artwork is dark by itself. */
.library-bg { filter: brightness(0.82) saturate(1.05); }

.library-content {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 4vh 5vw;
  max-width: 980px;
}

/* The title lettering, cut out of the original artwork */
.site-title { margin: 0; }

.site-title img {
  width: min(52vw, 640px);
  height: auto;
  filter: drop-shadow(0 4px 10px rgba(0, 0, 0, 0.65)) drop-shadow(0 0 26px rgba(60, 30, 90, 0.55));
}

.subtitle {
  margin-top: 1.1rem;
  font-size: clamp(0.95rem, 1.6vw, 1.2rem);
  font-style: italic;
  color: #e6dcc5;
  text-shadow: 0 1px 3px rgba(0,0,0,0.8);
  max-width: 40em;
}

.shelf {
  margin-top: 3.2rem;
  display: flex;
  gap: 2rem;
  flex-wrap: wrap;
  justify-content: center;
}

.book-card {
  background: none;
  border: none;
  cursor: pointer;
  color: inherit;
  font-family: inherit;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.9rem;
  padding: 0;
  transition: transform 0.25s ease;
}

.book-card:hover, .book-card:focus-visible {
  transform: translateY(-8px);
}

.book-card img {
  width: min(42vw, 210px);
  border-radius: 4px;
  box-shadow: 0 14px 34px rgba(0,0,0,0.65), 0 0 0 1px rgba(217,178,106,0.25);
}

.book-card-label {
  font-size: 1rem;
  letter-spacing: 0.04em;
  color: var(--gold-bright);
}

/* ---------- READER ---------- */

.reader {
  position: relative;
  width: 100%;
  height: 100vh;
}

.reader-bg { filter: brightness(0.4) saturate(0.9); }

.stage {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* --- The open book (two facing pages) --- */

.book {
  position: relative;
  display: flex;
  /* Two facing pages of 1055 x 1491 each — the format of the page images */
  width: min(86vw, 118vh);
  aspect-ratio: 2110 / 1491;
  perspective: 3200px;
}

.half {
  position: relative;
  width: 50%;
  height: 100%;
}

.page-content {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background: var(--paper);
  box-shadow: 0 18px 48px rgba(0,0,0,0.55);
}

.half-left .page-content { border-radius: 10px 0 0 10px; }
.half-right .page-content { border-radius: 0 10px 10px 0; }

.page-content img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  /* The page images match the book halves exactly and carry their own
     white margins, so they fill the paper edge to edge. */
}

/* page types */
.page-content.none {
  visibility: hidden;
}

.page-content.endpaper {
  background: radial-gradient(130% 100% at 100% 50%, #43301d 0%, #2c1f12 55%, #1e150c 100%);
}

.page-content.blank { background: var(--paper); }

.blank-orn {
  color: #cbb98f;
  font-size: 1.5rem;
  opacity: 0.7;
}

/* Table of contents page */
.page-content.toc {
  background: linear-gradient(160deg, #f7f0de 0%, #efe3c8 100%);
  color: #3c2f1e;
}

.toc-inner {
  width: 100%;
  height: 100%;
  padding: 5% 8%;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  overflow-y: auto;
}

.toc-book {
  margin: 0;
  font-family: 'Cinzel', Georgia, serif;
  font-weight: 600;
  font-size: clamp(0.7rem, 1.2vw, 0.95rem);
  letter-spacing: 0.35em;
  text-indent: 0.35em;
  color: #8a6f45;
}

.toc-title {
  margin: 0.15em 0 0.1em;
  font-family: 'Cinzel Decorative', 'Cinzel', Georgia, serif;
  font-weight: 700;
  font-size: clamp(1.1rem, 2.4vw, 1.9rem);
  color: #4d3a20;
}

.toc-rule {
  color: #a8894f;
  font-size: 0.8rem;
  margin-bottom: 3.5%;
  letter-spacing: 0.6em;
  text-indent: 0.6em;
}

.toc-list {
  width: 100%;
  display: flex;
  flex-direction: column;
}

.toc-item {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 1em;
  width: 100%;
  background: none;
  border: none;
  border-bottom: 1px dotted #c4ad7e;
  cursor: pointer;
  font-family: Georgia, serif;
  font-size: clamp(0.7rem, 1.45vw, 1.02rem);
  color: #3c2f1e;
  padding: 0.85% 0.6%;
  text-align: left;
}

.toc-item:hover { background: rgba(168, 137, 79, 0.16); }

.toc-item.locked { opacity: 0.5; cursor: default; }
.toc-item.locked:hover { background: none; }
.toc-item.locked .toc-name { color: #8a7a5c; }

.toc-num { font-weight: bold; white-space: nowrap; }
.toc-name { font-style: italic; text-align: right; }

/* spine shadow down the middle */
.spine {
  position: absolute;
  left: 50%;
  top: 0;
  bottom: 0;
  width: 7%;
  transform: translateX(-50%);
  background: linear-gradient(90deg,
    rgba(0,0,0,0) 0%,
    rgba(0,0,0,0.22) 42%,
    rgba(0,0,0,0.4) 50%,
    rgba(0,0,0,0.22) 58%,
    rgba(0,0,0,0) 100%);
  pointer-events: none;
  z-index: 2;
}

.book.cover-mode .spine { opacity: 0; }

/* --- Single-page mode (phones): one big readable page --- */

.book.single {
  aspect-ratio: 1055 / 1491;
  width: min(94vw, 60vh);
}

@supports (height: 100dvh) {
  .book.single { width: min(94vw, 60dvh); }
}

.book.single .half-left,
.book.single .spine { display: none; }

.book.single .half { width: 100%; }

.book.single .half-right .page-content { border-radius: 8px; }

.book.single .flip-leaf { width: 100%; }

/* the turning leaf */
.flip-leaf {
  position: absolute;
  top: 0;
  width: 50%;
  height: 100%;
  transform-style: preserve-3d;
  visibility: hidden;
  z-index: 4;
  /* Keep the leaf on its own GPU layer so starting a flip never
     stalls on layer creation. */
  will-change: transform;
}

.flip-leaf.animating {
  transition: transform 0.8s cubic-bezier(.42,.05,.35,1);
  /* No filter here! A CSS filter flattens transform-style: preserve-3d,
     which would hide the back face (the new page) during the turn. */
}

.flip-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  overflow: hidden;
  box-shadow: 0 12px 30px rgba(0,0,0,0.45);
}

.flip-front { transform: rotateY(0deg); }
.flip-back { transform: rotateY(180deg); }

.flip-face .page-content { box-shadow: none; border-radius: 0; }

.flip-face::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, rgba(0,0,0,0.14), rgba(0,0,0,0) 28%);
  pointer-events: none;
}

/* nav + chrome */
.nav-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(20, 16, 12, 0.55);
  border: 1px solid rgba(217, 178, 106, 0.35);
  color: var(--gold-bright);
  width: 3rem;
  height: 3rem;
  border-radius: 50%;
  font-size: 1.1rem;
  cursor: pointer;
  transition: background 0.2s ease;
  z-index: 5;
}

.nav-prev { left: 1rem; }
.nav-next { right: 1rem; }

.nav-btn:hover:not(:disabled) { background: rgba(217,178,106,0.25); }
.nav-btn:disabled { opacity: 0.25; cursor: default; }

.icon-btn {
  position: absolute;
  top: 1.2rem;
  z-index: 6;
  background: rgba(20,16,12,0.55);
  border: 1px solid rgba(217,178,106,0.35);
  color: var(--gold-bright);
  width: 2.6rem;
  height: 2.6rem;
  border-radius: 50%;
  font-size: 1.2rem;
  cursor: pointer;
  line-height: 1;
}

.icon-btn:hover { background: rgba(217,178,106,0.25); }

.close-btn { right: 1.2rem; }
.menu-btn { left: 1.2rem; }

.sidebar {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: min(78vw, 300px);
  background: var(--panel);
  backdrop-filter: blur(6px);
  border-right: 1px solid rgba(217,178,106,0.25);
  z-index: 7;
  padding: 5rem 1.2rem 1.5rem;
  transform: translateX(-100%);
  transition: transform 0.3s ease;
  overflow-y: auto;
}

.sidebar.open { transform: translateX(0); }

.sidebar h2 {
  color: var(--gold-bright);
  font-variant: small-caps;
  letter-spacing: 0.08em;
  margin: 0 0 1rem 0.4rem;
  font-size: 1.3rem;
}

.sidebar-item {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  color: #e6dcc5;
  font-family: inherit;
  font-size: 0.95rem;
  padding: 0.55rem 0.7rem;
  border-radius: 6px;
  cursor: pointer;
}

.sidebar-item:hover { background: rgba(217,178,106,0.15); }
.sidebar-item.active { background: rgba(217,178,106,0.28); color: var(--gold-bright); }

.sidebar-item.locked { opacity: 0.45; cursor: default; }
.sidebar-item.locked:hover { background: none; }

.sidebar-item .ch-title {
  display: block;
  font-size: 0.8rem;
  font-style: italic;
  color: #b9ac8c;
}

.page-indicator {
  position: absolute;
  bottom: 1.1rem;
  left: 50%;
  transform: translateX(-50%);
  z-index: 5;
  color: #cfc3a4;
  font-size: 0.85rem;
  letter-spacing: 0.05em;
  background: rgba(20,16,12,0.5);
  padding: 0.3rem 0.9rem;
  border-radius: 999px;
  white-space: nowrap;
}

/* --- Phone-sized chrome --- */
@media (max-width: 900px) and (orientation: portrait), (max-width: 550px) {
  .nav-btn {
    width: 2.4rem;
    height: 2.4rem;
    font-size: 0.95rem;
    background: rgba(20, 16, 12, 0.42);
  }
  .nav-prev { left: 0.4rem; }
  .nav-next { right: 0.4rem; }

  .icon-btn { width: 2.3rem; height: 2.3rem; top: 0.8rem; font-size: 1.05rem; }
  .close-btn { right: 0.8rem; }
  .menu-btn { left: 0.8rem; }

  .page-indicator {
    font-size: 0.75rem;
    bottom: 0.7rem;
    padding: 0.25rem 0.8rem;
  }

  /* The painted background title is cropped away on narrow screens,
     so show the compact title and frame the scenery instead. */
  /* Zoom in on the lower part of the artwork so the phone frames the
     characters and the valley instead of a heavily cropped wide shot. */
  .library-bg {
    background-size: auto 170%;
    background-position: 50% 100%;
    filter: brightness(0.6) saturate(1.05);
  }
  .library-content { padding-top: 7vh; }
  .site-title img { width: min(86vw, 480px); }

  .shelf { margin-top: 2.2rem; }
  .book-card img { width: min(58vw, 240px); }
  .subtitle { padding: 0 1rem; }

  /* Contents page fills the whole phone screen — roomier type */
  .toc-inner { padding: 7% 6%; }
  .toc-book { font-size: 0.85rem; }
  .toc-title { font-size: 1.5rem; }
  .toc-item { font-size: 0.92rem; padding: 2.2% 1%; }
}
