:root {
  --bg: #07122b;
  --bg-2: #12296a;
  --ink: #e9efff;
  --ink-dim: #9fb0d6;
  --accent: #fdb913;
  --glass: rgba(12, 25, 58, 0.78);
  --line: rgba(255, 255, 255, 0.1);
  --flip-ms: 900ms;
  --toolbar-h: 60px;
  --shift: 0;
}

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

html, body { height: 100%; margin: 0; }

body {
  background: radial-gradient(1300px 760px at 50% 30%, #17316f 0%, #0b1d45 45%, var(--bg) 100%) fixed;
  color: var(--ink);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

[hidden] { display: none !important; }

button { font: inherit; color: inherit; }

.ico {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  pointer-events: none;
}

#app {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
}

/* ---------- top bar ---------- */
#topbar {
  grid-row: 1;
  padding: 10px 18px 0;
  min-height: 44px;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark {
  flex: none;
  width: 22px; height: 22px; border-radius: 6px;
  background:
    linear-gradient(90deg, transparent 0 45%, #0b2a6f 45% 55%, transparent 55%),
    var(--accent);
  box-shadow: 0 0 0 1px rgba(255,255,255,.15);
}
.brand-text { min-width: 0; }
.brand-text strong { display: block; font-size: 14px; letter-spacing: .2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-text small { display: block; color: var(--ink-dim); font-size: 11.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- stage / book ---------- */
#stage {
  grid-row: 2;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px 12px;
  min-height: 0;
  overflow: hidden;
}

#bookWrap {
  position: relative;
  transform: translateX(calc(var(--shift) * 25%));
  transition: transform var(--flip-ms) cubic-bezier(.45, .05, .25, 1), opacity .5s ease;
  opacity: 0;
}
#bookWrap.ready { opacity: 1; }
#bookWrap.no-anim { transition: none; }

#bookWrap::before {
  content: "";
  position: absolute;
  left: 1%; right: 1%; bottom: -1.2%;
  height: 7%;
  background: radial-gradient(ellipse at 50% 50%, rgba(0, 0, 0, .6), rgba(0, 0, 0, 0) 70%);
  filter: blur(10px);
  pointer-events: none;
  transition: left var(--flip-ms) ease, right var(--flip-ms) ease;
}
#bookWrap.is-cover::before { left: 50%; }
#bookWrap.is-back::before { right: 50%; }
#bookWrap.portrait::before { left: 3%; right: 3%; }

#book { position: relative; margin: 0 auto; touch-action: pan-y; transition: opacity .17s ease; }
#book.jumping { opacity: 0; }

.stf__wrapper { position: relative; width: 100%; }
.stf__block { user-select: none; -webkit-user-select: none; }

/* ---------- page ---------- */
.page {
  background: #fff;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.page .ph, .page .pg { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.page .ph { background-size: 100% 100%; background-color: #eef1f6; }
.page .pg {
  opacity: 0;
  transition: opacity .25s ease;
  pointer-events: none;
  -webkit-user-drag: none;
  object-fit: fill;
}
.page .pg.on { opacity: 1; }

.page::after {
  content: "";
  position: absolute;
  top: 0; bottom: 0;
  width: 7%;
  pointer-events: none;
  opacity: .9;
}
.page.--right::after { left: 0; background: linear-gradient(to right, rgba(0, 0, 0, .22), rgba(0, 0, 0, .07) 35%, rgba(0, 0, 0, 0)); }
.page.--left::after { right: 0; background: linear-gradient(to left, rgba(0, 0, 0, .22), rgba(0, 0, 0, .07) 35%, rgba(0, 0, 0, 0)); }

.hs-layer { position: absolute; inset: 0; z-index: 2; }
.hs {
  position: absolute;
  display: block;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color .15s ease, box-shadow .15s ease;
  -webkit-tap-highlight-color: transparent;
}
.hs:hover, .hs:focus-visible {
  background: rgba(253, 185, 19, .24);
  box-shadow: 0 0 0 2px rgba(253, 185, 19, .95);
  outline: none;
}
body.show-links .hs {
  background: rgba(253, 185, 19, .14);
  box-shadow: 0 0 0 1.5px rgba(253, 185, 19, .85);
}

/* ---------- edge arrows ---------- */
.edge {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 44px; height: 88px;
  border: 0; border-radius: 12px;
  background: rgba(255, 255, 255, .06);
  color: var(--ink);
  cursor: pointer;
  opacity: .55;
  transition: opacity .2s, background .2s;
  display: grid; place-items: center;
}
.edge:hover { opacity: 1; background: rgba(255, 255, 255, .14); }
.edge:disabled { opacity: .12; cursor: default; }
#edgePrev { left: 8px; }
#edgeNext { right: 8px; }
.edge .ico { width: 26px; height: 26px; }
@media (hover: none), (max-width: 760px) { .edge { display: none; } }

/* ---------- toolbar ---------- */
#toolbar {
  grid-row: 3;
  position: relative;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  padding: 8px 14px calc(10px + env(safe-area-inset-bottom));
  min-height: var(--toolbar-h);
}
.tb-group { display: flex; align-items: center; gap: 2px; }
.tb-center {
  justify-self: center;
  background: var(--glass);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px 6px;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
.tb-right { justify-self: end; }

.tb {
  display: inline-grid; place-items: center;
  width: 40px; height: 40px;
  border: 0; border-radius: 50%;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
  transition: background .15s, color .15s, transform .1s;
}
.tb:hover { background: rgba(255, 255, 255, .12); }
.tb:active { transform: scale(.94); }
.tb:focus-visible, .edge:focus-visible, .tab:focus-visible, .toc-item:focus-visible, .thumb:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
.tb[aria-pressed="true"] { color: var(--accent); }
.tb:disabled { opacity: .3; cursor: default; background: transparent; }
.tb.sm { width: 32px; height: 32px; }
.tb.sm .ico { width: 17px; height: 17px; }
.tb-sep { width: 1px; height: 22px; background: var(--line); margin: 0 6px; }
#btnMusic.playing { color: var(--accent); }

.pager { display: flex; align-items: baseline; gap: 6px; padding: 0 8px; font-variant-numeric: tabular-nums; }
.pager input {
  width: 6.4ch; min-width: 4ch;
  background: transparent; border: 0; border-bottom: 1px solid transparent;
  color: var(--ink); font: inherit; font-weight: 600; text-align: right;
  padding: 2px 0; outline: none;
}
.pager input:focus { border-bottom-color: var(--accent); }
.pager span { color: var(--ink-dim); white-space: nowrap; }

/* ---------- popover ---------- */
.pop {
  position: absolute;
  right: 14px;
  bottom: calc(100% - 4px);
  width: 260px;
  padding: 12px 14px 12px;
  background: var(--glass);
  border: 1px solid var(--line);
  border-radius: 14px;
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  box-shadow: 0 10px 30px rgba(0, 0, 0, .4);
  z-index: 20;
}
.pop-title { font-size: 11px; text-transform: uppercase; letter-spacing: .8px; color: var(--ink-dim); }
.track-name { margin: 4px 0 8px; font-weight: 600; font-size: 13px; line-height: 1.3; min-height: 2.6em; }
.pop-row { display: flex; align-items: center; gap: 6px; }
.pop-row input[type=range] { flex: 1; accent-color: var(--accent); min-width: 0; }
.check { display: flex; align-items: center; gap: 8px; margin-top: 10px; color: var(--ink-dim); cursor: pointer; }
.check input { accent-color: var(--accent); }

/* ---------- side panel ---------- */
#scrim {
  position: fixed; inset: 0; z-index: 30;
  background: rgba(3, 8, 20, .55);
  opacity: 0; visibility: hidden;
  transition: opacity .25s, visibility .25s;
}
#scrim.open { opacity: 1; visibility: visible; }

#panel {
  position: fixed; z-index: 31;
  top: 0; bottom: 0; left: 0;
  width: min(360px, 92vw);
  display: flex; flex-direction: column;
  background: #0b1a3c;
  border-right: 1px solid var(--line);
  box-shadow: 10px 0 40px rgba(0, 0, 0, .45);
  transform: translateX(-102%);
  visibility: hidden;
  transition: transform .3s cubic-bezier(.3, .8, .3, 1), visibility .3s;
}
#panel.open { transform: none; visibility: visible; }

.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 10px 10px 8px 14px; border-bottom: 1px solid var(--line); }
.tabs { display: flex; gap: 4px; }
.tab { border: 0; background: transparent; color: var(--ink-dim); padding: 8px 12px; border-radius: 8px; cursor: pointer; font-weight: 600; }
.tab.on { background: rgba(255, 255, 255, .1); color: var(--ink); }
.panel-body { flex: 1; overflow: auto; padding: 8px; overscroll-behavior: contain; }

.toc-group { margin: 14px 8px 4px; font-size: 11px; text-transform: uppercase; letter-spacing: .8px; color: var(--ink-dim); }
.toc-item {
  display: flex; width: 100%; align-items: baseline; justify-content: space-between; gap: 12px;
  text-align: left; border: 0; background: transparent; color: var(--ink);
  padding: 10px 10px; border-radius: 10px; cursor: pointer; line-height: 1.3;
}
.toc-item:hover { background: rgba(255, 255, 255, .07); }
.toc-item.on { background: rgba(253, 185, 19, .14); box-shadow: inset 3px 0 0 var(--accent); }
.toc-item .pg-no { flex: none; color: var(--ink-dim); font-variant-numeric: tabular-nums; }

.thumbs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; align-content: start; }
.thumb { border: 0; padding: 0; background: transparent; cursor: pointer; text-align: center; color: var(--ink-dim); font-size: 11px; border-radius: 6px; }
.thumb img { display: block; width: 100%; aspect-ratio: 595 / 793; object-fit: cover; background: #1a2b57; border-radius: 4px; box-shadow: 0 2px 8px rgba(0, 0, 0, .4); }
.thumb span { display: block; padding-top: 4px; }
.thumb.on img { outline: 2px solid var(--accent); outline-offset: 1px; }
.thumb.on { color: var(--accent); font-weight: 600; }

/* ---------- zoom viewer ---------- */
#zoom { position: fixed; inset: 0; z-index: 40; background: rgba(4, 9, 22, .96); }
#zoomView { position: absolute; inset: 0; overflow: hidden; touch-action: none; cursor: grab; user-select: none; -webkit-user-select: none; }
#zoomView.grabbing { cursor: grabbing; }
#zoomPan { position: absolute; left: 0; top: 0; display: flex; transform-origin: 0 0; will-change: transform; box-shadow: 0 10px 50px rgba(0, 0, 0, .6); }
.zpage { position: relative; flex: none; background: #fff; }
.zpage img { position: absolute; inset: 0; width: 100%; height: 100%; display: block; pointer-events: none; -webkit-user-drag: none; }
.zbar {
  position: absolute; left: 50%; bottom: calc(14px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  display: flex; align-items: center; gap: 2px;
  background: var(--glass); border: 1px solid var(--line); border-radius: 999px; padding: 4px 8px;
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}

/* ---------- info dialog ---------- */
dialog#info {
  border: 1px solid var(--line); border-radius: 16px; padding: 0;
  background: #0d1f4a; color: var(--ink);
  width: min(520px, 92vw);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .55);
}
dialog#info::backdrop { background: rgba(3, 8, 20, .6); }
.info-box { padding: 20px 22px 18px; }
.info-box h2 { margin: 0 0 10px; font-size: 18px; }
.info-box ul { margin: 0 0 12px; padding-left: 18px; color: var(--ink); }
.info-box li { margin: 6px 0; }
.credit { color: var(--ink-dim); font-size: 12.5px; margin: 8px 0 16px; }
.btn {
  border: 0; border-radius: 10px; padding: 9px 16px; cursor: pointer; font-weight: 600;
  background: var(--accent); color: #1a1400;
}
.btn.ghost { background: rgba(255, 255, 255, .1); color: var(--ink); }
.btn:hover { filter: brightness(1.08); }

/* ---------- loader ---------- */
#loader {
  position: fixed; inset: 0; z-index: 100;
  display: grid; place-content: center; justify-items: center; gap: 14px;
  background: var(--bg);
  transition: opacity .5s ease, visibility .5s;
}
#loader.done { opacity: 0; visibility: hidden; }
#loader p { margin: 0; color: var(--ink-dim); }
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid rgba(255, 255, 255, .15); border-top-color: var(--accent); animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.noscript { position: fixed; inset: 0; z-index: 200; margin: 0; display: grid; place-items: center; background: var(--bg); }

/* ---------- responsive ---------- */
@media (max-width: 760px) {
  #topbar { padding: 8px 12px 0; min-height: 36px; }
  .brand-text small { display: none; }
  .brand-text strong { font-size: 13px; }
  #toolbar { grid-template-columns: auto 1fr auto; gap: 4px; padding: 6px 8px calc(8px + env(safe-area-inset-bottom)); }
  .hide-sm { display: none !important; }
  .tb { width: 38px; height: 38px; }
  .tb-right .tb-sep { display: none; }
  #stage { padding: 6px 6px; }
}
@media (max-height: 460px) {
  #topbar { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  #bookWrap, #bookWrap::before, #panel, #scrim { transition-duration: .01ms !important; }
}

#ytHost { position: fixed; left: -400px; bottom: 0; width: 200px; height: 113px; pointer-events: none; }
