:root {
  --accent: #7c4dff;
  --accent-2: #ffb300;
}
* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  overflow: hidden;
  background: radial-gradient(circle at top, #e8dfc8, #c9b99a);
  color: #2a1f0e;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
}

/* Stage fills the viewport and centers the book. */
.stage {
  position: relative;
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: clamp(8px, 1.8vh, 16px) clamp(44px, 7vw, 80px);
}
#flipbook { transition: none; }

/* Each page is the rendered PDF image filling the leaf. */
.page {
  position: relative;
  background: #fff;
  overflow: hidden;
}
.page img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  user-select: none;
  -webkit-user-drag: none;
}

/* ===== INDEX LEAF — Table of Contents =====
   StPageFlip sizes the .page div in px. We author .index-inner at a fixed
   base size (matching INDEX_BASE_W × INDEX_BASE_H in JS) and use the same
   scale+translate trick as the applets so fonts render identically at any
   resolution. --index-scale, --index-base-w, --index-base-h are set by JS. */
.page.index-page {
  background: #fff;
  color: #111;
  padding: 0;
  overflow: hidden;
  font-family: "Times New Roman", Times, Georgia, serif;
  position: relative;
}

/* Mirrors the applet .responsive-wrapper pattern exactly:
   fixed px size, scaled from top-left, translated to centre. */
.index-inner {
  width: var(--index-base-w, 600px);
  height: var(--index-base-h, 800px);
  position: absolute;
  top: 50%;
  left: 50%;
  transform-origin: top left;
  /* Applet pattern: top-left anchored at parent centre, translate back by own
     half-size, then scale — net result is a perfectly centred scaled box. */
  transform: scale(var(--index-scale, 1)) translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  padding: 3% 5%;
  box-sizing: border-box;
}

.index-header {
  text-align: center;
  padding-bottom: 2.5%;
  margin-bottom: 2%;
  border-bottom: 1.5px solid #222;
}
.index-inner h2 {
  margin: 0 0 1%;
  font-size: 32px;
  font-weight: 700;
  color: #111;
  letter-spacing: 0.01em;
}
.index-inner h2 .chapter-subject { color: #6c2eb9; }
.index-inner .sub {
  margin: 0;
  font-size: 12px;
  color: #555;
  font-style: italic;
  line-height: 1.3;
}

/* TOC rows */
.toc-list {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}
.toc-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 1.8% 0;
  border-bottom: 1px solid #e0e0e0;
  cursor: pointer;
}
.toc-row:hover .toc-topic { color: #6c2eb9; }
.toc-row:last-child { border-bottom: none; }
.toc-left {
  display: flex;
  align-items: baseline;
  gap: 7px;
  flex: 1 1 auto;
  min-width: 0;
}
.toc-pg {
  flex: 0 0 auto;
  font-size: 12px;
  color: #888;
  font-variant-numeric: tabular-nums;
  font-family: "Times New Roman", Times, serif;
  min-width: 2em;
}
.toc-topic {
  font-size: 17px;
  color: #111;
  line-height: 1.2;
  font-family: "Times New Roman", Times, serif;
}
/* Applet link — right side */
.toc-right {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
}
.toc-applet-btn {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  font-size: 11px;
  font-weight: 700;
  font-family: "Segoe UI", system-ui, sans-serif;
  color: #1a1228;
  background: var(--accent-2);
  padding: 2px 9px;
  border-radius: 20px;
  cursor: pointer;
  border: none;
  white-space: nowrap;
  transition: background 0.15s ease, transform 0.1s ease;
  line-height: 1.7;
}
.toc-applet-btn:hover { background: #ffc930; transform: scale(1.04); }
.index-foot {
  margin-top: 2%;
  padding-top: 2%;
  border-top: 1px solid #ddd;
  font-size: 10px;
  color: #999;
  text-align: center;
  font-style: italic;
  font-family: "Times New Roman", Times, serif;
}

/* ===== Clickable hotspot overlay on a page (percentage-based) =====
   position/size use % so they follow the page automatically.
   Decorative px values (border, radius, glow, label) scale with
   --page-scale set on each .page div by JS (same pattern as applets). */
.hotspot {
  position: absolute;
  cursor: pointer;
  border: calc(2px * var(--page-scale, 1)) dashed rgba(255, 179, 0, 0.7);
  border-radius: calc(8px * var(--page-scale, 1));
  background: rgba(124, 77, 255, 0);
  transition: background 0.25s ease, box-shadow 0.25s ease;
  z-index: 5;
  animation: ring 1.8s ease-out infinite;
}
.hotspot:hover {
  background: rgba(124, 77, 255, 0.18);
  border-color: var(--accent-2);
  box-shadow: 0 0 0 calc(4px * var(--page-scale, 1)) rgba(255, 179, 0, 0.25);
}
.hotspot::after {
  content: var(--label, "▶ Open Applet");
  position: absolute;
  right: calc(-4px * var(--page-scale, 1));
  bottom: calc(-28px * var(--page-scale, 1));
  background: var(--accent-2);
  color: #2b2330;
  font-size: calc(11px * var(--page-scale, 1));
  font-weight: 700;
  line-height: 1;
  padding: calc(5px * var(--page-scale, 1)) calc(9px * var(--page-scale, 1));
  border-radius: calc(5px * var(--page-scale, 1));
  white-space: nowrap;
  opacity: 0;
  transform: translateY(calc(-4px * var(--page-scale, 1)));
  transition: opacity 0.25s ease, transform 0.25s ease;
  pointer-events: none;
}
.hotspot:hover::after { opacity: 1; transform: translateY(0); }
@keyframes ring {
  0% { box-shadow: 0 0 0 0 rgba(255, 179, 0, 0.55); }
  100% { box-shadow: 0 0 0 calc(14px * var(--page-scale, 1)) rgba(255, 179, 0, 0); }
}

/* ===== Side navigation arrows (left / right edges) ===== */
.arrow {
  position: fixed;
  top: 50%;
  transform: translateY(-50%);
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  background: rgba(124, 77, 255, 0.85);
  color: #fff;
  font-size: 28px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 30;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
  transition: background 0.2s ease, transform 0.12s ease, opacity 0.2s ease;
}
.arrow:hover:not(:disabled) { background: #9670ff; }
.arrow:active:not(:disabled) { transform: translateY(-50%) scale(0.92); }
.arrow:disabled { opacity: 0.25; cursor: not-allowed; }
#arrowPrev { left: 14px; }
#arrowNext { right: 14px; }

/* ===== Floating page indicator + hint (bottom center) ===== */
.pill {
  position: fixed;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(43, 35, 48, 0.82);
  backdrop-filter: blur(2px);
  border: 1px solid rgba(124, 77, 255, 0.6);
  padding: 5px 16px;
  border-radius: 20px;
  font-size: 14px;
  font-variant-numeric: tabular-nums;
  color: #fff;
  z-index: 30;
  text-align: center;
  max-width: 92vw;
}
#hint { bottom: 48px; font-size: clamp(10px, 1.6vh, 13px); pointer-events: none; }

/* ===== Expandable page picker ===== */
#indicator-wrap {
  position: fixed;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 30;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}
#indicator-wrap .pill {
  position: static;
  transform: none;
  cursor: pointer;
  user-select: none;
  transition: background 0.15s;
}
#indicator-wrap .pill:hover { background: rgba(60,50,70,0.92); }
#page-picker {
  background: rgba(43,35,48,0.92);
  backdrop-filter: blur(4px);
  border: 1px solid rgba(124,77,255,0.55);
  border-radius: 24px;
  padding: 6px 10px;
  max-width: 88vw;
  overflow: hidden;
  animation: picker-in 0.18s ease;
}
@keyframes picker-in {
  from { opacity: 0; transform: scaleX(0.7) scaleY(0.8); }
  to   { opacity: 1; transform: none; }
}
#page-picker-inner {
  display: flex;
  flex-wrap: nowrap;
  gap: 4px;
  overflow-x: auto;
  scrollbar-width: none;
  max-width: 80vw;
}
#page-picker-inner::-webkit-scrollbar { display: none; }
.pg-btn {
  flex: 0 0 auto;
  min-width: 28px;
  height: 28px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.08);
  color: #e0d8f0;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
  display: flex; align-items: center; justify-content: center;
}
.pg-btn:hover { background: rgba(124,77,255,0.55); color: #fff; }
.pg-btn.active { background: #6c2eb9; color: #fff; font-weight: 700; }

/* ===== Centre spine (gutter) shadow =====
   StPageFlip's HTML mode turns each of OUR `.page` divs into the leaf
   itself by adding `stf__item --left/--right` straight onto the same
   element — so `.page` IS the leaf, not a child of it.

   Left page  → shadow hugs its RIGHT edge.
   Right page → shadow hugs its LEFT edge. */
.page.--left::before,
.page.--right::before {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  width: 9%;
  pointer-events: none;
  z-index: 6;
}
.page.--left::before {
  right: 0;
  background: linear-gradient(to right,
    rgba(0,0,0,0) 0%,
    rgba(0,0,0,0.08) 55%,
    rgba(0,0,0,0.30) 100%);
}
.page.--right::before {
  left: 0;
  background: linear-gradient(to left,
    rgba(0,0,0,0) 0%,
    rgba(0,0,0,0.08) 55%,
    rgba(0,0,0,0.30) 100%);
}

/* ===== Applet modal ===== */
#applet-modal {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 100;
  background: transparent;
  pointer-events: none;
}
#applet-modal.open { display: block; }

#modal-frame-wrap {
  position: fixed;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 8px 40px rgba(0,0,0,0.55), 0 0 0 1.5px rgba(124,77,255,0.5);
  background: #fff;
  pointer-events: all;
  transition: top 0.35s cubic-bezier(.4,0,.2,1),
              left 0.35s cubic-bezier(.4,0,.2,1),
              width 0.35s cubic-bezier(.4,0,.2,1),
              height 0.35s cubic-bezier(.4,0,.2,1),
              border-radius 0.35s ease,
              box-shadow 0.35s ease;
}

#applet-modal.expanded #modal-frame-wrap {
  border-radius: 0;
  box-shadow: none;
}
#applet-modal.expanded::before {
  content: '';
  position: fixed;
  inset: 0;
  background: rgba(14,10,20,0.88);
  backdrop-filter: blur(6px);
  z-index: -1;
}

#modal-iframe {
  width: 100%;
  height: 100%;
  border: none;
  display: block;
}

/* macOS-style traffic-light chrome — top-left corner.
   --modal-scale is set on #modal-frame-wrap by JS (modal height / 600px base). */
#modal-chrome {
  position: absolute;
  top: calc(10px * var(--modal-scale, 1));
  left: calc(12px * var(--modal-scale, 1));
  display: flex;
  align-items: center;
  gap: calc(8px * var(--modal-scale, 1));
  z-index: 10;
}

/* Both buttons share the traffic-light dot shape */
#modal-close,
#modal-fullscreen {
  width: calc(14px * var(--modal-scale, 1));
  height: calc(14px * var(--modal-scale, 1));
  border-radius: 50%;
  border: none;
  cursor: pointer;
  padding: 0;
  position: relative;
  transition: filter 0.15s ease;
  flex-shrink: 0;
}

/* Red dot — close */
#modal-close {
  background: #ff5f57;
  box-shadow: inset 0 0 0 0.5px rgba(0,0,0,0.18);
}
#modal-close:hover { filter: brightness(1.15); }

/* Icon appears on hover */
#modal-close::after {
  content: '✕';
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(8px * var(--modal-scale, 1));
  color: rgba(80,10,10,0.85);
  opacity: 0;
  transition: opacity 0.12s ease;
  line-height: 1;
}
#modal-close:hover::after { opacity: 1; }

/* Green dot — fullscreen */
#modal-fullscreen {
  background: #28c840;
  box-shadow: inset 0 0 0 0.5px rgba(0,0,0,0.18);
}
#modal-fullscreen:hover { filter: brightness(1.12); }

#modal-fullscreen::after {
  content: '⛶';
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(9px * var(--modal-scale, 1));
  color: rgba(0,60,10,0.85);
  opacity: 0;
  transition: opacity 0.12s ease;
  line-height: 1;
}
#modal-fullscreen:hover::after { opacity: 1; }

/* When expanded, swap the fullscreen icon */
#applet-modal.expanded #modal-fullscreen::after { content: '⊠'; }

#rotate-notice {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 1000;
  align-items: center;
  justify-content: center;
  padding: 28px;
  background: #1c1524;
  color: #fff;
  text-align: center;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
}
.rotate-card { max-width: 360px; }
.rotate-icon {
  width: 64px;
  height: 64px;
  margin: 0 auto 16px;
  border: 3px solid #ffb300;
  border-radius: 14px;
  transform: rotate(90deg);
}
.rotate-title {
  margin: 0 0 8px;
  font-size: 24px;
  font-weight: 800;
}
.rotate-copy {
  margin: 0;
  font-size: 15px;
  line-height: 1.45;
  color: #e6ddf3;
}

@media (hover: none) {
  .hotspot::after {
    opacity: 1;
    transform: none;
  }
}

@media (max-height: 520px) {
  .arrow {
    width: 44px;
    height: 44px;
    font-size: 24px;
  }
}

@media (orientation: portrait) and (max-width: 900px) {
  #rotate-notice { display: flex; }
}
