.topbar {
  position: absolute;
  top: 0;
  right: 0;
  z-index: 20;
  padding: 0;
  pointer-events: none;
}
.top-tools {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 14px 16px 28px 40px;
  pointer-events: auto;
}
.top-tools .icon-btn {
  opacity: 0;
  transform: translateY(-6px);
  pointer-events: none;
  transition: opacity .22s var(--ease), transform .22s var(--ease), color .2s var(--ease), border-color .2s var(--ease);
}
.topbar:hover .top-tools .icon-btn,
.topbar:focus-within .top-tools .icon-btn,
.top-tools:hover .icon-btn {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}
.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 11px;
  border: 1px solid var(--panel-line);
  background: var(--panel);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  color: inherit;
  cursor: pointer;
  display: grid;
  place-items: center;
  font-size: 15px;
  transition: .2s var(--ease);
}
.icon-btn:hover {
  color: var(--text);
  border-color: rgba(92, 48, 18, .35);
  transform: translateY(-1px);
}
.icon-btn:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }

.stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 0 64px;
}
.stage::before {
  content: "";
  position: absolute;
  inset: 0;
  background: url("../images/theme-tools.png") center / cover no-repeat;
  opacity: .11;
  pointer-events: none;
  z-index: 0;
}
.viewer.theme-dark .stage::before { opacity: .08; }
.nav-arrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 52px;
  height: 52px;
  border-radius: 50%;
  z-index: 15;
  border: 1px solid var(--panel-line);
  background: var(--panel);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  color: var(--maroon);
  font-size: 20px;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: .2s var(--ease);
}
.viewer.theme-dark .nav-arrow { color: #fff; }
.nav-arrow.left { left: 10px; }
.nav-arrow.right { right: 10px; }
.nav-arrow:hover { color: var(--text); transform: translateY(-50%) scale(1.06); }
.nav-arrow:disabled { opacity: .25; cursor: default; }

.viewport {
  position: relative;
  min-width: 0;
  min-height: 0;
  max-width: 100%;
  z-index: 1;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  /* Clip here; keep perspective on a child — Safari breaks 3D when
     overflow:hidden shares the same node as perspective. */
  overflow: hidden;
}
.perspective-layer {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-perspective: 2600px;
  perspective: 2600px;
  -webkit-perspective-origin: 50% 42%;
  perspective-origin: 50% 42%;
}
.zoom-layer {
  position: relative;
  z-index: 1;
  transition: transform .25s var(--ease);
  will-change: transform;
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
}
.book-shift {
  transition: transform .6s var(--ease);
  will-change: transform;
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
}
.book {
  position: relative;
  flex: 0 0 auto;
  width: calc(var(--pw) * 2);
  height: var(--ph);
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
}
.book.single-page { width: var(--pw); }
.book-shadow {
  position: absolute;
  left: 50%;
  bottom: calc(50% - var(--ph) / 2 - 18px);
  width: calc(var(--pw) * 1.85);
  height: 42px;
  transform: translateX(-50%);
  -webkit-transform: translateX(-50%);
  background: radial-gradient(50% 60% at 50% 50%, rgba(74, 20, 12, .28), transparent 72%);
  filter: blur(6px);
  z-index: 0;
  pointer-events: none;
}
.leaf {
  position: absolute;
  top: 0;
  left: 50%;
  width: var(--pw);
  height: var(--ph);
  transform-origin: left center;
  -webkit-transform-origin: left center;
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
}
.book.single-page .leaf { left: 0; }
.face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  overflow: hidden;
  border-radius: 0;
  background: var(--paper);
  box-shadow: none;
  -webkit-transform: translateZ(0.1px);
  transform: translateZ(0.1px);
}
.face.back {
  -webkit-transform: rotateY(180deg) translateZ(0.1px);
  transform: rotateY(180deg) translateZ(0.1px);
  border-radius: 0;
}
.face::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  width: 34px;
  background: linear-gradient(90deg, rgba(0, 0, 0, .18), rgba(0, 0, 0, 0));
  pointer-events: none;
  z-index: 6;
}
.face.back::before {
  left: auto;
  right: 0;
  background: linear-gradient(270deg, rgba(0, 0, 0, .18), rgba(0, 0, 0, 0));
}
.shade {
  position: absolute;
  inset: 0;
  z-index: 8;
  pointer-events: none;
  opacity: 0;
  background: linear-gradient(90deg, rgba(0, 0, 0, .55), rgba(0, 0, 0, .05));
}
.face.back .shade { background: linear-gradient(270deg, rgba(0, 0, 0, .55), rgba(0, 0, 0, .05)); }
.edge {
  position: absolute;
  top: 2%;
  height: 96%;
  width: 9px;
  z-index: 1;
  border-radius: 3px;
  background: repeating-linear-gradient(90deg, #efe7d4 0 2px, #e0d6bd 2px 3px);
  box-shadow: 0 8px 20px -10px rgba(0, 0, 0, .5);
}
.edge.l { right: calc(50% + var(--pw) - 3px); }
.edge.r { left: calc(50% + var(--pw) - 3px); }
.book.single-page .edge,
.book.at-start .edge.l,
.book.at-end .edge.r { display: none; }

.page-slot {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--paper);
}
.page-canvas {
  display: block;
  max-width: 100%;
  max-height: 100%;
}
.page-pending {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--text-soft);
  font-size: 12px;
  z-index: 2;
  pointer-events: none;
  background: var(--paper);
}
.page-pending[hidden] { display: none; }
.face.is-blank .page-slot { background: var(--paper); }
.face.is-blank .page-pending { display: none; }

.toolbar {
  position: absolute;
  left: 50%;
  bottom: calc(10px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  -webkit-transform: translateX(-50%);
  z-index: 20;
  max-width: min(94vw, 980px);
  width: max-content;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px;
  color: var(--text);
  background: var(--panel);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid var(--panel-line);
  border-radius: 18px;
  box-shadow: 0 20px 50px -22px rgba(0, 0, 0, .7), inset 0 0 0 1px rgba(212, 175, 90, .12);
}
.toolbar .grp { display: flex; align-items: center; gap: 4px; flex: 0 0 auto; }
.toolbar .sep { width: 1px; height: 26px; background: var(--panel-line); margin: 0 4px; flex: 0 0 1px; }
.pageind {
  flex: 0 0 auto;
  white-space: nowrap;
  font-family: var(--serif);
  font-weight: 600;
  font-size: 13px;
  min-width: 104px;
  text-align: center;
  opacity: .95;
}
.gotowrap { display: flex; align-items: center; gap: 4px; }
.gotowrap input {
  width: 46px;
  height: 34px;
  border-radius: 9px;
  border: 1px solid var(--panel-line);
  background: rgba(255, 255, 255, .7);
  color: inherit;
  text-align: center;
  font-size: 13px;
}
.viewer.theme-dark .gotowrap input { background: rgba(255, 255, 255, .06); }
.gotowrap input:focus { outline: 2px solid var(--gold); outline-offset: 1px; }
.tb-btn {
  flex: 0 0 auto;
  height: 38px;
  min-width: 38px;
  padding: 0 10px;
  border-radius: 11px;
  border: 1px solid transparent;
  background: transparent;
  color: inherit;
  cursor: pointer;
  font-size: 14px;
  display: grid;
  place-items: center;
  transition: .18s var(--ease);
}
.tb-btn:hover { background: rgba(92, 48, 18, .08); color: var(--text); }
.tb-btn.active { color: var(--maroon); background: rgba(92, 48, 18, .10); }
.tb-btn:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
.tb-btn:disabled { opacity: .35; cursor: default; }

.progress {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 3px;
  background: rgba(212, 175, 90, .12);
  z-index: 19;
}
.progress .bar {
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, var(--red), var(--orange), var(--green), var(--navy));
  transition: width .5s var(--ease);
}

.panel {
  position: absolute;
  top: 0;
  bottom: 0;
  width: min(84vw, 320px);
  z-index: 30;
  background: var(--panel);
  backdrop-filter: blur(20px);
  border-right: 1px solid var(--panel-line);
  transform: translateX(-102%);
  transition: transform .32s var(--ease);
  display: flex;
  flex-direction: column;
  padding: 18px;
}
.panel.right {
  left: auto;
  right: 0;
  border-right: 0;
  border-left: 1px solid var(--panel-line);
  transform: translateX(102%);
}
.panel.open { transform: translateX(0); }
.panel h4 {
  font-family: var(--serif);
  font-weight: 700;
  font-size: 16px;
  margin-bottom: 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.panel h4 .icon-btn { width: 32px; height: 32px; }
.thumbgrid {
  overflow-y: auto;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  padding-right: 4px;
}
.thumb {
  cursor: pointer;
  border-radius: 8px;
  overflow: hidden;
  border: 2px solid transparent;
  background: #f3e6cc;
  aspect-ratio: .72;
  position: relative;
  transition: .18s;
  padding: 0;
  font: inherit;
  color: inherit;
  display: flex;
  align-items: center;
  justify-content: center;
}
.thumb:hover { border-color: rgba(212, 175, 90, .5); }
.thumb.active { border-color: var(--gold); }
.thumb canvas {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: var(--paper);
  display: block;
}
.thumb .cap {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  font-size: 9px;
  padding: 3px 5px;
  color: #fff;
  background: linear-gradient(0deg, rgba(0, 0, 0, .78), transparent);
}
.toclist { overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.toclist button {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  color: inherit;
  padding: 10px 12px;
  border-radius: 9px;
  cursor: pointer;
  font-family: var(--sans);
  font-size: 14px;
  transition: .15s;
}
.toclist button:hover { background: rgba(212, 175, 90, .10); }
.toclist button.active { background: rgba(92, 48, 18, .10); color: var(--text); }
.toclist button span:last-child {
  font-family: var(--serif);
  opacity: .7;
  font-size: 12px;
}

.hint {
  position: absolute;
  left: 50%;
  bottom: 78px;
  transform: translateX(-50%);
  z-index: 18;
  font-size: 12px;
  color: var(--text);
  background: rgba(255, 248, 236, .9);
  border: 1px solid var(--panel-line);
  padding: 8px 14px;
  border-radius: 20px;
  backdrop-filter: blur(8px);
  transition: opacity .5s;
  pointer-events: none;
  white-space: nowrap;
}
.hint i { color: var(--maroon); margin-right: 6px; }
.viewer.theme-dark .hint {
  color: #f8f0e4;
  background: rgba(46, 8, 5, .78);
}
.scrim {
  position: absolute;
  inset: 0;
  background: rgba(30, 5, 4, .55);
  z-index: 25;
  opacity: 0;
  pointer-events: none;
  transition: opacity .3s;
}
.scrim.show { opacity: 1; pointer-events: auto; }

.overlay {
  position: absolute;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  background: rgba(251, 243, 228, .78);
  backdrop-filter: blur(8px);
}
.overlay[hidden] { display: none !important; }
.status-card {
  width: min(92vw, 420px);
  background: rgba(255, 248, 236, .96);
  border: 1px solid var(--panel-line);
  border-radius: 18px;
  padding: 28px 24px 22px;
  text-align: center;
  box-shadow: 0 24px 60px -20px rgba(0, 0, 0, .35);
}
.viewer.theme-dark .overlay { background: rgba(30, 5, 4, .78); }
.viewer.theme-dark .status-card {
  background: rgba(46, 8, 5, .94);
  color: #f8f0e4;
}
.status-card h3 {
  font-family: var(--serif);
  font-weight: 700;
  font-size: 22px;
  margin: 14px 0 8px;
}
.status-card p {
  color: var(--text-soft);
  line-height: 1.55;
  font-size: 14px;
}
.viewer.theme-dark .status-card p { color: var(--gold-soft); }
.status-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 18px;
}
.status-actions button,
.password-row button {
  border: 1px solid var(--panel-line);
  background: rgba(212, 175, 90, .16);
  color: inherit;
  border-radius: 11px;
  padding: 10px 14px;
  cursor: pointer;
  font: inherit;
}
.status-actions button:hover,
.password-row button:hover { color: var(--gold); }
.password-row {
  display: none;
  gap: 8px;
  justify-content: center;
  margin-top: 16px;
}
.password-row.show { display: flex; }
.password-row input {
  width: 180px;
  border-radius: 9px;
  border: 1px solid var(--panel-line);
  background: rgba(255, 255, 255, .08);
  color: inherit;
  padding: 10px 12px;
}
.spinner {
  width: 42px;
  height: 42px;
  margin: 0 auto;
  border-radius: 50%;
  border: 3px solid rgba(212, 175, 90, .22);
  border-top-color: var(--gold);
  animation: spin .8s linear infinite;
}
.overlay.is-error .spinner,
.overlay.is-empty .spinner,
.overlay.is-password .spinner { display: none; }
@keyframes spin { to { transform: rotate(360deg); } }

.print-sheet { display: none; }
