:root {
  --reader-topbar-h: 50px;
  --reader-footer-h: 50px;

  --brand-purple: #6d28d9;
  --brand-purple-2: #8b5cf6;
  --brand-purple-3: #cbb8f7;
  --ink: rgba(38, 14, 71, 0.92);
  --ink-muted: rgba(38, 14, 71, 0.62);
}

@media (max-width: 768px) {
  :root {
    --reader-topbar-h: 46px;
    --reader-footer-h: 50px;
  }
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  overflow: hidden;
  font-family: system-ui;
  background:
    radial-gradient(
      circle at 20% 10%,
      rgba(184, 179, 255, 0.22),
      transparent 45%
    ),
    radial-gradient(
      circle at 80% 30%,
      rgba(122, 114, 242, 0.16),
      transparent 55%
    ),
    #0b0b10;
}

#mainContainer {
  min-height: 100svh;
  display: grid;
  place-items: center;
  padding: 0;
}

@media (max-width: 600px) and (orientation: portrait) {
  #mainContainer {
    place-items: stretch;
  }
}

/* ===== outer reader wrapper ===== */
.wrap {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.reader-shell {
  position: relative;
  width: min(980px, 96vw);
  height: min(820px, 96vh);
  min-height: 520px;
  border-radius: 18px;
  overflow: hidden;
  background: #fff;
  border: 1px solid rgba(0, 0, 0, 0.08);
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.08);
}

@media (max-width: 900px) {
  .reader-shell {
    width: 100vw;
    height: 100vh;
    min-height: 0;
    border-radius: 0;
    border: 0;
    box-shadow: none;
  }
}

/* ===== stage between topbar and footer ===== */
.reader-stage {
  position: absolute;
  z-index: 1;
  top: var(--reader-topbar-h);
  right: 0;
  bottom: var(--reader-footer-h);
  left: 0;
  min-height: 0;
  box-sizing: border-box;
  overflow: hidden;
}

#book {
  width: 100%;
  height: 100%;
  min-height: 0;
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  overflow: hidden;
  display: flex;
  align-items: stretch;
  justify-content: center;
}

.page {
  background: #fff;
}

/* ===== cover ===== */
.cover-inner {
  position: relative;
  width: 100%;
  height: 100%;
  min-height: 0;
  overflow: hidden;
  padding: 0;
  margin: 0;
  --coverShadeStrong: 0.62;
  --coverShadeMid: 0.22;
}

.cover-bg {
  position: absolute;
  inset: 0;
  background-size: cover;
  background-position: center;
  transform: scale(1.02);
}

.cover-video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center;
  transform: scale(1.02);
}

.cover-titlebar {
  position: absolute;
  top: 22px;
  left: 0;
  right: 0;
  z-index: 2;
  margin: 0 14px;
  padding: 18px 18px 22px;
  border-radius: 14px;
  text-align: center;
  color: #fff;
  background: linear-gradient(
    to bottom,
    rgba(0, 0, 0, var(--coverShadeStrong)),
    rgba(0, 0, 0, var(--coverShadeMid)) 70%,
    rgba(0, 0, 0, 0)
  );
}

.cover-title {
  margin: 0;
  font-size: 28px;
  font-weight: 950;
  line-height: 1.08;
  letter-spacing: 0.3px;
  text-shadow: 0 10px 22px rgba(0, 0, 0, 0.35);
}

.cover-author {
  margin: 8px 0 0;
  font-size: 13px;
  font-weight: 750;
  opacity: 0.96;
  text-shadow: 0 10px 22px rgba(0, 0, 0, 0.35);
}

.cover-titlebar.backcover {
  top: auto;
  bottom: clamp(16px, 4vh, 36px);
  margin: 0 80px;
}

.cover-title.backcover {
  color: #ddd;
  font-size: clamp(24px, 4.8vw, 30px);
  letter-spacing: 0.6px;
}

/* ===== page inner ===== */
.page-inner {
  height: 100%;
  min-height: 0;
  box-sizing: border-box;
  pointer-events: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 18px;
}

.page-inner.page-inner--full {
  padding: 0;
  gap: 0;
}

.page-meta {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: rgba(0, 0, 0, 0.55);
}

.content-body {
  flex: 1;
  min-height: 0;
  display: grid;
  gap: 12px;
}

.content-body > * {
  min-height: 0;
}

.content-body .text,
.text.page-text {
  min-height: 0;
  max-height: 100%;
}

.content-body.body-top {
  --mediaRow: 0.55fr;
  --textRow: 0.45fr;
  grid-template-rows: minmax(0, var(--mediaRow)) minmax(0, var(--textRow));
}

.content-body.body-bottom {
  --mediaRow: 0.55fr;
  --textRow: 0.45fr;
  grid-template-rows: minmax(0, var(--textRow)) minmax(0, var(--mediaRow));
}

.content-body.body-full {
  width: 100%;
  height: 100%;
  min-height: 0;
  gap: 0;
  grid-template-rows: minmax(0, 1fr);
}

.content-body.body-full,
.content-body.body-full .media-full {
  padding: 0 !important;
  margin: 0 !important;
  gap: 0 !important;
}

.content-body.body-full .media-full {
  width: 100%;
  height: 100%;
  min-height: 0;
  border-radius: 0;
  border: 0;
  box-shadow: none;
  background: transparent;
}

/* ===== prepage ===== */
.prepage {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
}

.prepage-card {
  position: relative;
  overflow: hidden;
  width: min(540px, 92%);
  padding: 18px 18px 16px;
  border-radius: 26px;
  background: linear-gradient(
    180deg,
    rgba(217, 198, 249, 0.32),
    rgba(199, 180, 232, 0.22)
  );
  border: 1px solid rgba(109, 40, 217, 0.14);
  box-shadow: 0 22px 60px rgba(38, 14, 71, 0.12);
}

.prepage-card::before {
  content: "";
  position: absolute;
  inset: -50px;
  pointer-events: none;
  filter: blur(2px);
  background:
    radial-gradient(
      circle at 20% 20%,
      rgba(109, 40, 217, 0.18),
      transparent 45%
    ),
    radial-gradient(
      circle at 85% 18%,
      rgba(139, 92, 246, 0.14),
      transparent 46%
    ),
    radial-gradient(
      circle at 65% 85%,
      rgba(203, 184, 247, 0.22),
      transparent 52%
    );
}

.prepage-top,
.prepage-body,
.prepage-footer {
  position: relative;
  z-index: 1;
}

.prepage-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 999px;
  font-size: 0.82rem;
  font-weight: 900;
  color: #fff;
  background: rgba(38, 14, 71, 0.92);
  box-shadow: 0 10px 22px rgba(38, 14, 71, 0.12);
}

.prepage-title {
  margin-top: 12px;
  font-size: 1.55rem;
  font-weight: 1000;
  letter-spacing: 0.01em;
  color: var(--ink);
}

.prepage-sub {
  margin-top: 6px;
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--ink-muted);
}

.prepage-body {
  margin-top: 14px;
  padding: 14px 14px 12px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.86);
  border: 1px solid rgba(109, 40, 217, 0.1);
}

.prepage-summary {
  font-size: 1rem;
  line-height: 1.6;
  font-weight: 650;
  color: rgba(38, 14, 71, 0.88);
}

.prepage-footer {
  margin-top: 12px;
  display: flex;
  justify-content: center;
}

.prepage-hint {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.6);
  border: 1px solid rgba(109, 40, 217, 0.12);
  backdrop-filter: blur(8px);
  color: rgba(38, 14, 71, 0.82);
  font-size: 0.92rem;
  font-weight: 900;
}

.prepage-hint .dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: linear-gradient(
    90deg,
    var(--brand-purple),
    var(--brand-purple-2)
  );
  box-shadow: 0 0 0 4px rgba(109, 40, 217, 0.12);
}

@media (max-width: 640px) {
  .prepage {
    padding: 14px;
  }

  .prepage-card {
    width: min(520px, 94%);
    padding: 16px;
    border-radius: 24px;
  }

  .prepage-title {
    font-size: 1.42rem;
  }

  .prepage-summary {
    font-size: 0.98rem;
  }
}

.hidden {
  display: none !important;
}

.readerContainer.hidden {
  display: none !important;
}

/* ===== phone tuning ===== */
@media (max-width: 600px) {
  .wrap {
    align-items: stretch;
    justify-content: flex-start;
    padding: 0;
  }

  .page-inner {
    padding: 10px;
    gap: 8px;
  }

  .content-body {
    gap: 8px;
  }
}

@media (max-width: 600px) and (max-height: 700px) {
  .page-inner {
    padding: 8px;
    gap: 6px;
  }

  .content-body {
    gap: 6px;
  }

  .content-body.body-top,
  .content-body.body-bottom {
    --mediaH: clamp(190px, 34vh, 280px);
  }
}

@media (max-width: 600px) and (orientation: portrait) {
  .reader-shell {
    width: 100vw;
    height: calc(var(--vh) * 100);
    max-height: calc(var(--vh) * 100);
    min-height: 0;
    border-radius: 0;
    border: 0;
    box-shadow: none;
  }

  .reader-stage {
    top: var(--reader-topbar-h);
    right: 0;
    bottom: var(--reader-footer-h);
    left: 0;
  }

  #book {
    width: 100%;
    height: 100%;
    margin: 0;
  }

  .content-body.body-top {
    grid-template-rows:
      minmax(0, var(--mediaH, clamp(220px, 38vh, 320px)))
      minmax(0, 1fr);
  }

  .content-body.body-bottom {
    grid-template-rows:
      minmax(0, 1fr)
      minmax(0, var(--mediaH, clamp(220px, 38vh, 320px)));
  }

  .content-body .text {
    min-height: 0;
    max-height: 100%;
  }

  .content-body .media-half,
  .content-body .media-full {
    height: 100%;
    min-height: 0;
  }

  .page-inner.cover-inner,
  .content-body.body-full,
  .content-body.body-full .media-full {
    padding: 0 !important;
    margin: 0 !important;
    gap: 0 !important;
  }

  .content-body.body-full .media-full {
    width: 100%;
    height: 100%;
    border-radius: 0;
    border: 0;
    box-shadow: none;
    background: transparent;
  }

  .content-body.body-full .media-full > img,
  .content-body.body-full .media-full > video,
  .cover-video,
  .cover-bg {
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: center;
  }

  .cover-titlebar {
    top: 12px;
    margin: 0 10px;
    padding: 14px 14px 18px;
    border-radius: 12px;
  }
}

/* ===== stage / flip safety ===== */
.reader-stage,
#book,
.stf__parent,
.stf__wrapper,
.stf__block {
  min-height: 0;
}

.reader-stage .stf__wrapper,
.reader-stage .stf__block {
  max-height: 100%;
}

.reader-stage .stf__wrapper {
  overflow: hidden;
}

.reader-footer {
  isolation: isolate;
}
