*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg-deep: #0c0618;
  --bg-mid: #151028;
  --accent: #7c3aed;
  --accent-soft: #a78bfa;
  --cyan: #22d3ee;
  --text: #f8fafc;
  --text-muted: rgba(248, 250, 252, 0.55);
  --sheet-shadow:
    0 24px 60px rgba(0, 0, 0, 0.55),
    0 8px 24px rgba(124, 58, 237, 0.12),
    0 0 0 1px rgba(255, 255, 255, 0.06);
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
  --topbar-h: 3.25rem;
  --toolbar-h: 5.25rem;
}

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg-deep);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  -webkit-font-smoothing: antialiased;
  touch-action: none;
}

#app {
  width: 100%;
  height: 100%;
  height: 100dvh;
}

.hidden {
  display: none !important;
}

/* Logo — matches catalog: height only + object-fit contain + opacity pulse */
.brand-logo {
  display: block;
  width: auto;
  object-fit: contain;
}

.brand-logo--loader {
  height: 4rem;
}

.brand-logo--header {
  height: 2rem;
}

.brand-logo--pulse {
  animation: brand-pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite;
}

@keyframes brand-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

/* Initial loader — catalog pattern: full viewport, centered logo */
.loader {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  height: 100dvh;
  background:
    radial-gradient(ellipse 80% 60% at 50% 0%, rgba(124, 58, 237, 0.22), transparent 60%),
    radial-gradient(ellipse 60% 50% at 80% 100%, rgba(34, 211, 238, 0.08), transparent 55%),
    var(--bg-deep);
}

.loader__error {
  font-size: 0.92rem;
  color: var(--text-muted);
  padding: 0 1.5rem;
  text-align: center;
}

/* Viewer shell */
.viewer {
  display: grid;
  grid-template-rows: var(--topbar-h) 1fr var(--toolbar-h);
  width: 100%;
  height: 100%;
  height: 100dvh;
  background:
    radial-gradient(ellipse 90% 70% at 50% -10%, rgba(124, 58, 237, 0.18), transparent 55%),
    radial-gradient(ellipse 70% 55% at 100% 100%, rgba(34, 211, 238, 0.07), transparent 50%),
    linear-gradient(180deg, var(--bg-mid) 0%, var(--bg-deep) 100%);
  animation: viewer-in 0.55s var(--ease-out) both;
}

@keyframes viewer-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding:
    max(0.5rem, env(safe-area-inset-top))
    max(1rem, env(safe-area-inset-right))
    0
    max(1rem, env(safe-area-inset-left));
  z-index: 20;
}

.topbar__brand {
  display: inline-flex;
  align-items: center;
  text-decoration: none;
  line-height: 0;
}

.topbar__meta {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.topbar__page {
  font-size: 0.8rem;
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.topbar__badge {
  font-size: 0.68rem;
  padding: 0.2rem 0.45rem;
  border: 1px solid rgba(167, 139, 250, 0.25);
  background: rgba(124, 58, 237, 0.12);
  color: var(--accent-soft);
  border-radius: 999px;
  font-variant-numeric: tabular-nums;
  transition: opacity 0.35s ease, transform 0.35s ease;
}

.topbar__badge.is-done {
  opacity: 0;
  transform: translateY(-4px);
  pointer-events: none;
}

/* Stage */
.stage {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  flex: 1;
  padding: 0.5rem max(0.75rem, env(safe-area-inset-right)) 0.25rem max(0.75rem, env(safe-area-inset-left));
}

.stage__glow {
  position: absolute;
  inset: 8% 12% 12%;
  background: radial-gradient(ellipse at center, rgba(124, 58, 237, 0.14), transparent 70%);
  pointer-events: none;
  filter: blur(18px);
}

.page-loader {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(12, 6, 24, 0.68);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.book-wrap {
  position: relative;
  z-index: 2;
  width: 100%;
  max-width: min(920px, 100%);
  height: 100%;
  max-height: calc(100dvh - var(--topbar-h) - var(--toolbar-h) - 1rem);
  display: flex;
  align-items: center;
  justify-content: center;
}

.book {
  width: 100%;
  height: 100%;
}

.book .stf__parent {
  margin: 0 auto;
}

.bf-page {
  background: #fff;
  overflow: hidden;
  box-shadow: var(--sheet-shadow);
}

.bf-page img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  pointer-events: none;
  user-select: none;
  -webkit-user-drag: none;
}

/* Toolbar */
.toolbar {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  padding:
    0
    max(1rem, env(safe-area-inset-right))
    max(0.65rem, env(safe-area-inset-bottom))
    max(1rem, env(safe-area-inset-left));
  z-index: 20;
}

.toolbar__track {
  height: 3px;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 999px;
  overflow: hidden;
}

.toolbar__fill {
  height: 100%;
  width: 5%;
  background: linear-gradient(90deg, var(--accent), var(--cyan));
  border-radius: inherit;
  transition: width 0.35s var(--ease-out);
}

.toolbar__nav {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 0.75rem;
}

.toolbar__btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 3rem;
  height: 3rem;
  min-width: 48px;
  min-height: 48px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
  cursor: pointer;
  transition:
    transform 0.15s ease,
    background 0.15s ease,
    border-color 0.15s ease,
    opacity 0.15s ease;
}

.toolbar__btn:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(255, 255, 255, 0.2);
}

.toolbar__btn:active:not(:disabled) {
  transform: scale(0.96);
}

.toolbar__btn:disabled {
  opacity: 0.28;
  cursor: default;
}

.toolbar__btn--primary {
  border-color: rgba(124, 58, 237, 0.55);
  background: linear-gradient(145deg, rgba(124, 58, 237, 0.35), rgba(99, 102, 241, 0.22));
}

.toolbar__hint {
  text-align: center;
  font-size: 0.76rem;
  color: var(--text-muted);
  user-select: none;
}

@media (max-width: 640px) {
  :root {
    --toolbar-h: 4.75rem;
  }

  .toolbar__hint {
    font-size: 0.7rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .viewer,
  .toolbar__fill,
  .topbar__badge,
  .brand-logo--pulse {
    animation: none !important;
    transition: none !important;
  }
}
