:root {
  --ink: #17251f;
  --gold: #bd833b;
  --paper: #f5f2ea;
  --chrome-bottom: 54px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #111;
  color: var(--ink);
  font-family: 'DM Sans', Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; touch-action: manipulation; }
.hidden { display: none !important; }

.welcome {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: var(--paper);
  padding: 24px;
}
.welcome-card { width: min(420px, 100%); text-align: center; }
.brand-mark {
  width: 58px; height: 58px;
  border: 1.5px solid var(--gold);
  border-radius: 50%;
  display: grid; place-items: center;
  margin: auto;
  color: var(--gold);
  font: 800 22px 'Playfair Display', serif;
}
.eyebrow {
  font-size: 9px;
  letter-spacing: 2px;
  color: #9b743f;
  font-weight: 800;
  margin: 14px 0 5px;
}
.welcome h1 {
  font: 700 42px 'Playfair Display', serif;
  margin: 0;
}
.welcome-copy { color: #777; font-size: 13px; margin: 8px 0 25px; }
.choice {
  width: 100%;
  height: 58px;
  margin: 6px 0;
  border: 1px solid #d9d0c1;
  border-radius: 8px;
  background: white;
  color: var(--ink);
  font-weight: 700;
  font-size: 16px;
  text-align: left;
  padding: 0 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.choice span { color: var(--gold); font-size: 23px; }
.choice.nonac { background: #f9f1e5; }
.call {
  display: inline-block;
  margin-top: 20px;
  color: #8b622e;
  text-decoration: none;
  font-size: 12px;
  font-weight: 700;
}

/* Reader: column layout so page sits between top + bottom chrome */
.reader {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  background: #080808;
  overflow: hidden;
  isolation: isolate;
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

.menu-title {
  padding: 5px 10px;
  border: 1px solid #ffffff2b;
  border-radius: 30px;
  background: #1119;
  color: #fff;
  font-size: 9px;
  font-weight: 800;
  letter-spacing: 1.2px;
  white-space: nowrap;
  pointer-events: none;
  opacity: .9;
  flex: none;
}

.page-viewport {
  position: relative;
  z-index: 1;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  width: 100%;
  display: block;
  overflow: hidden;
  background: #080808;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  overscroll-behavior: contain;
}
.zoom-sizer {
  box-sizing: border-box;
  min-width: 100%;
  min-height: 100%;
  display: grid;
  place-items: center;
  overflow: hidden; /* clip scaled page — keeps scroll correct */
}
.reader.is-zoomed .page-viewport {
  overflow: scroll;
  -webkit-overflow-scrolling: touch;
  touch-action: none;
  cursor: grab;
}
.reader.is-zoomed .page-viewport.is-dragging,
.reader.is-zoomed .page-viewport:active {
  cursor: grabbing;
}
.reader.is-zoomed .zoom-sizer {
  min-width: 0;
  min-height: 0;
  display: block;
  place-items: unset;
}
.reader.is-zoomed .page-stage {
  max-width: none;
  max-height: none;
  will-change: transform;
}
.reader.is-zoomed .page-leaf {
  transition: none !important;
}
.reader.is-zoomed .side-rail {
  pointer-events: none;
}
.reader.is-zoomed .spiral-bind {
  opacity: 0;
  visibility: hidden;
  transition: opacity .2s ease;
}

/* Rail + spiral + PDF — notebook feel */
.page-stage {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  max-width: 100%;
  max-height: 100%;
  height: auto;
}

.book-pane {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  max-width: 100%;
  max-height: 100%;
  transform-origin: 0 0;
}

/* Metal spiral binding — stays on page edge only */
.spiral-bind {
  flex: none;
  width: 18px;
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  align-items: center;
  padding: 8px 0;
  margin-right: -6px;
  z-index: 2;
  pointer-events: none;
  transition: opacity .2s ease;
}
.reader.is-zoomed .spiral-bind {
  opacity: 0;
  visibility: hidden;
}
.spiral-bind .coil {
  position: relative;
  width: 18px;
  height: 15px;
  border-radius: 50%;
  background: transparent;
  box-shadow:
    inset 0 0 0 2.5px #b8b8b8,
    inset 0 0 0 3.5px #5a5a5a,
    1px 2px 3px #0009;
}
.spiral-bind .coil::before {
  content: '';
  position: absolute;
  left: 1px;
  top: 2px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #fff 0%, #cfcfcf 45%, transparent 70%);
  opacity: .85;
}
.spiral-bind .coil::after {
  content: '';
  position: absolute;
  right: -2px;
  top: 50%;
  width: 6px;
  height: 2.5px;
  transform: translateY(-50%);
  border-radius: 2px;
  background: linear-gradient(90deg, #9a9a9a, #e8e8e8 40%, #6a6a6a);
  box-shadow: 0 1px 2px #0007;
}

.flip-scene {
  position: relative;
  flex: 0 1 auto;
  max-height: 100%;
  perspective: 2200px;
  perspective-origin: left center;
  transform-style: preserve-3d;
}

.page-under {
  position: absolute;
  inset: 0;
  z-index: 1;
  border-radius: 0 5px 5px 0;
  overflow: hidden;
  background: #f4efe6;
  box-shadow: 2px 4px 16px #0005;
  transform: translateZ(-1px);
}
.page-under img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: center top;
  pointer-events: none;
}

.page-leaf {
  position: relative;
  z-index: 2;
  transform-origin: left center;
  transform-style: preserve-3d;
  transition: transform .7s cubic-bezier(.45, .05, .25, 1);
  will-change: transform;
  filter: drop-shadow(4px 6px 14px rgba(0,0,0,.35));
}
.page-leaf.turning-next {
  transform: rotateY(-178deg);
  transition: transform .72s cubic-bezier(.45, .02, .2, 1);
}
/* Previous: destination page swings in from left → right */
.page-leaf.turning-prev {
  animation: flipPrevIn .72s cubic-bezier(.45, .02, .2, 1) both;
}
@keyframes flipPrevIn {
  from { transform: rotateY(-178deg); filter: brightness(.82); }
  to { transform: rotateY(0deg); filter: brightness(1); }
}
.page-leaf.notebook-open {
  animation: notebookOpen .85s cubic-bezier(.22, .7, .2, 1) both;
}
@keyframes notebookOpen {
  0% { transform: rotateY(-95deg); filter: brightness(.7); }
  60% { filter: brightness(.95); }
  100% { transform: rotateY(0deg); filter: brightness(1); }
}

.page-face {
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}
.page-front {
  position: relative;
}
.page-back {
  position: absolute;
  inset: 0;
  background:
    linear-gradient(90deg, #cfc7b6 0%, #e8e1d2 14%, #f3eee4 45%, #f7f3ea 100%);
  transform: rotateY(180deg);
  border-radius: 0 5px 5px 0;
  box-shadow: inset 12px 0 24px #00000018;
  pointer-events: none;
}
.page-back::after {
  content: '';
  position: absolute;
  inset: 0;
  background: repeating-linear-gradient(
    0deg,
    transparent,
    transparent 27px,
    #00000008 28px
  );
  opacity: .35;
}

.page-curl {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 2;
  background:
    linear-gradient(90deg, #00000033 0%, transparent 10%),
    linear-gradient(105deg, transparent 58%, #00000022 78%, #ffffff55 92%, transparent 100%);
  opacity: 0;
  transition: opacity .25s ease;
  mix-blend-mode: multiply;
}
.page-leaf.turning-next .page-curl,
.page-leaf.turning-prev .page-curl {
  opacity: 1;
}

@media (prefers-reduced-motion: reduce) {
  .page-leaf,
  .page-leaf.notebook-open,
  .page-leaf.turning-prev,
  .page-curl {
    transition: none !important;
    animation: none !important;
  }
}

.side-rail {
  flex: none;
  width: 54px;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-evenly;
  gap: 0;
  padding: 4px 3px;
  background: #101010;
  border: 1px solid #ffffff14;
  border-right: 0;
  border-radius: 8px 0 0 8px;
  scrollbar-width: none;
}
.side-rail::-webkit-scrollbar { display: none; }

.rail-link {
  width: 100%;
  min-height: 0;
  flex: 1 1 0;
  padding: 2px 1px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: #c8c2b6;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  cursor: pointer;
}
.rail-link:active { background: #ffffff12; }
.rail-link.active {
  background: #bd833b28;
  color: #f0d2a4;
}
.rail-link.active .rail-ico {
  background: #bd833b;
  color: #fff;
  border-color: transparent;
}
.rail-ico {
  width: 26px;
  height: 26px;
  border-radius: 8px;
  border: 1px solid #ffffff22;
  background: #1a1a1a;
  color: #d8b07a;
  display: grid;
  place-items: center;
}
.rail-ico svg {
  width: 14px;
  height: 14px;
  display: block;
}
.rail-name {
  font-size: 7px;
  font-weight: 700;
  letter-spacing: .1px;
  line-height: 1.05;
  text-align: center;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.page-holder {
  position: relative;
  flex: 0 1 auto;
  max-height: 100%;
  line-height: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  border-radius: 0 5px 5px 0;
  box-shadow:
    3px 6px 20px #0008,
    inset 10px 0 18px #00000014,
    0 0 0 1px #ffffff12;
  overflow: hidden;
  background: #f4efe6;
}

.page-holder img {
  display: block;
  width: auto;
  height: auto;
  max-height: calc(100dvh - var(--chrome-bottom) - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
  max-width: calc(100vw - 78px - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px));
  object-fit: contain;
  pointer-events: none;
  user-select: none;
  -webkit-user-drag: none;
}

.loading {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: #101010;
  color: #fff;
  font-size: 13px;
}
.spinner {
  width: 28px;
  height: 28px;
  border: 2px solid #ffffff33;
  border-top-color: var(--gold);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Bottom bar is in-flow — never covers the menu page */
.bottom-controls {
  position: relative;
  z-index: 12;
  flex: none;
  height: var(--chrome-bottom);
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 6px 10px;
  border: 0;
  border-top: 1px solid #ffffff18;
  border-radius: 0;
  background: #121212;
  box-shadow: none;
  backdrop-filter: none;
  -webkit-backdrop-filter: none;
  transform: none;
  left: auto;
  bottom: auto;
}
.bottom-controls button {
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: #ffffff12;
  color: #fff;
  font-size: 22px;
  display: grid;
  place-items: center;
  cursor: pointer;
}
.bottom-controls button:active { background: #bd833b; }
.bottom-controls button:disabled { opacity: .35; }
.bottom-controls #pageCounter {
  min-width: 44px;
  text-align: center;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
}
.bottom-controls i {
  width: 1px;
  height: 22px;
  background: #ffffff35;
  margin: 0 2px;
}

.drawer-backdrop {
  position: absolute;
  z-index: 19;
  inset: 0;
  background: #0009;
  opacity: 0;
  visibility: hidden;
  transition: opacity .22s, visibility .22s;
}
.side-drawer {
  position: absolute;
  z-index: 20;
  left: 0;
  top: 0;
  bottom: 0;
  width: min(320px, 86vw);
  padding: calc(env(safe-area-inset-top) + 19px) 17px calc(env(safe-area-inset-bottom) + 18px);
  background: #f8f5ed;
  box-shadow: 12px 0 35px #0006;
  transform: translateX(-105%);
  transition: transform .25s ease;
  overflow-y: auto;
  overscroll-behavior: contain;
}
.reader.drawer-open .drawer-backdrop { opacity: 1; visibility: visible; }
.reader.drawer-open .side-drawer { transform: translateX(0); }
.drawer-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 16px;
  border-bottom: 1px solid #ded6c8;
}
.drawer-head strong {
  display: block;
  font: 700 18px 'Playfair Display', serif;
}
.drawer-head small {
  display: block;
  margin-top: 4px;
  color: #9b743f;
  font-size: 8px;
  font-weight: 800;
  letter-spacing: 1.5px;
}
.drawer-head button {
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 50%;
  background: #eae4d8;
  font-size: 24px;
  color: #17251f;
}
.drawer-label {
  margin: 20px 4px 10px;
  color: #9b743f;
  font-size: 9px;
  font-weight: 800;
  letter-spacing: 1.5px;
}
.drawer-link {
  width: 100%;
  min-height: 48px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 7px 9px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: #26342d;
  text-align: left;
  font-size: 13px;
  font-weight: 700;
}
.drawer-link b {
  width: 34px;
  height: 34px;
  flex: none;
  display: grid;
  place-items: center;
  border-radius: 9px;
  background: #eee7da;
  color: #98652b;
}
.drawer-link b svg {
  width: 18px;
  height: 18px;
  display: block;
}
.drawer-link.active { background: #e9e1d3; }
.drawer-link.active b { background: #bd833b; color: #fff; }
.drawer-foot {
  margin-top: 18px;
  padding-top: 13px;
  border-top: 1px solid #ded6c8;
  color: #888;
  font-size: 10px;
}

.reader:fullscreen,
.reader.pseudo-fullscreen {
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  background: #080808;
}
.reader.pseudo-fullscreen .bottom-controls,
.reader.pseudo-fullscreen .side-rail {
  display: none;
}

/* Mobile-first: maximize page area */
@media (max-width: 768px) {
  :root {
    --chrome-bottom: 52px;
  }
  .menu-title { font-size: 8px; padding: 4px 8px; letter-spacing: 1px; }
  .side-rail { width: 50px; }
  .rail-ico { width: 24px; height: 24px; }
  .rail-ico svg { width: 13px; height: 13px; }
  .rail-name { font-size: 6.5px; }
  .spiral-bind { width: 20px; margin-right: -7px; }
  .spiral-bind .coil { width: 16px; height: 13px; }
  .page-holder img {
    max-width: calc(100vw - 72px - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px));
  }
  .bottom-controls { gap: 4px; padding: 5px 8px; }
  .bottom-controls button { width: 38px; height: 38px; font-size: 21px; }
  .bottom-controls #pageCounter { min-width: 40px; font-size: 10px; }
}

@media (max-width: 380px) {
  .side-rail { width: 46px; }
  .rail-ico { width: 22px; height: 22px; }
  .page-holder img {
    max-width: calc(100vw - 62px - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px));
  }
  .bottom-controls { gap: 3px; }
  .bottom-controls button { width: 34px; height: 34px; font-size: 19px; }
}

@media (orientation: landscape) and (max-height: 500px) {
  :root {
    --chrome-bottom: 46px;
  }
  .bottom-controls button { width: 34px; height: 34px; }
}
