/* Book viewer. Builds on the site tokens in style.css. */

.viewer-page { overflow: hidden; }
.viewer-page .site-header { position: relative; }

.viewer {
  --bar: 3.1rem;
  --stage: var(--paper-2);
  --stage-ink: var(--ink);
  position: relative;
  display: grid;
  grid-template-columns: 0 1fr;
  grid-template-rows: var(--bar) 1fr;
  height: calc(100vh - 3.7rem);
  height: calc(100dvh - 3.7rem);
  background: var(--stage);
  color: var(--stage-ink);
  font-size: 1rem;
  line-height: 1.4;
  overflow: hidden;
}
.viewer:fullscreen { height: 100vh; height: 100dvh; }
.viewer.side-open { grid-template-columns: 20rem 1fr; }
.viewer[data-tone="night"] { --stage: #050f18; --stage-ink: #e8f0f3; }
.viewer[data-tone="sepia"] { --stage: #e3dccb; }

/* ---------- top bar ---------- */
.vbar {
  grid-column: 1 / -1;
  position: relative;
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: 0 .6rem;
  background: var(--paper);
  border-bottom: 1px solid var(--rule);
  z-index: 30;
}
.viewer[data-tone="night"] .vbar { background: #0a1b2b; border-color: #173043; }
.v-title { flex: 1 1 8rem; min-width: 0; display: flex; flex-direction: column; justify-content: center; padding: 0 .3rem; }
.v-title #v-chap { font-size: 1rem; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.v-title #v-sec { font-size: .8rem; color: var(--ink-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.v-nav, .v-tools { display: flex; align-items: center; gap: .15rem; }
.vb {
  display: inline-grid; place-items: center; min-width: 2.2rem; height: 2.2rem; padding: 0 .45rem;
  background: none; border: 1px solid transparent; border-radius: 6px; color: var(--ink-2);
  font: inherit; font-size: .9rem; cursor: pointer; text-decoration: none;
}
.vb:hover { background: var(--paper-2); color: var(--ink); }
.vb[aria-pressed="true"], .vb.on { background: var(--accent-soft); color: var(--accent-text); }
.vb svg { width: 1.2rem; height: 1.2rem; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.vb[hidden] { display: none; }
#v-dl { display: inline-flex; align-items: center; gap: .35rem; }
#v-dl .vb-label { font-size: .88rem; }
@media (max-width: 1100px) { #v-dl .vb-label { display: none; } }
.v-page {
  width: 4.2rem; height: 2rem; text-align: center; font: inherit; font-family: var(--mono); font-size: .9rem;
  background: var(--paper-2); color: var(--ink); border: 1px solid var(--rule); border-radius: 6px;
}
.v-of { font-family: var(--mono); font-size: .78rem; color: var(--ink-3); min-width: 5.2rem; padding-left: .3rem; white-space: nowrap; }
.v-zoom { font-family: var(--mono); font-size: .78rem; color: var(--ink-3); min-width: 2.6rem; text-align: center; }
.v-sep { width: 1px; height: 1.4rem; background: var(--rule); margin: 0 .3rem; }
.v-progress { position: absolute; left: 0; right: 0; bottom: -1px; height: 2px; background: transparent; }
.v-progress div { height: 100%; width: 0; background: var(--accent); transition: width .2s; }

@media (max-width: 960px) { .v-of, .v-zoom, #v-zout, #v-zin { display: none; } .v-title #v-sec { display: none; } }
@media (max-width: 640px) {
  .v-title { display: none; }
  .vbar { gap: .2rem; padding: 0 .3rem; justify-content: space-between; }
  .v-sep, #v-tone, #v-share { display: none; }
  .v-page { width: 3.4rem; }
}

/* ---------- sidebar ---------- */
.vside {
  grid-row: 2; grid-column: 1;
  width: 100%; min-width: 0; overflow: hidden; display: flex; flex-direction: column;
  background: var(--paper); border-right: 1px solid var(--rule);
  visibility: hidden;
}
.viewer.side-open .vside { visibility: visible; }
.viewer[data-tone="night"] .vside { background: #0a1b2b; border-color: #173043; }
.vtabs { display: flex; border-bottom: 1px solid var(--rule); flex: none; }
.vtabs button {
  flex: 1; padding: .7rem .4rem; background: none; border: 0; border-bottom: 2px solid transparent; color: var(--ink-3);
  font: inherit; font-size: .92rem; cursor: pointer;
}
.vtabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); font-weight: 500; }
.vpanel { flex: 1; overflow: auto; padding: .6rem .4rem 2rem; overscroll-behavior: contain; }
.vpanel[hidden] { display: none; }
.vempty { color: var(--ink-3); font-size: .95rem; padding: .8rem; }

.toc-root, .toc-root ul { list-style: none; margin: 0; padding: 0; }
.toc-root ul { margin-left: 1rem; border-left: 1px solid var(--rule); }
.toc-root ul[hidden] { display: none; }
.toc-row { display: flex; align-items: baseline; gap: .1rem; }
.toc-tg { flex: none; width: 1.4rem; background: none; border: 0; color: var(--ink-3); cursor: pointer; font-size: .8rem; padding: .3rem 0; }
.toc-tg:disabled { cursor: default; }
.toc-row a { flex: 1; display: block; padding: .28rem .4rem; color: var(--ink-2); text-decoration: none; border-radius: 4px; font-size: .95rem; line-height: 1.3; }
.toc-row a:hover { background: var(--paper-2); color: var(--ink); }
.toc-row a.cur { background: var(--accent-soft); color: var(--ink); font-weight: 500; }
.toc-root > li > .toc-row a { font-size: 1rem; color: var(--ink); }
.toc-root li[data-level="1"] .toc-row a { font-size: .92rem; }
.toc-root li[data-level="2"] .toc-row a, .toc-root li[data-level="3"] .toc-row a { font-size: .86rem; color: var(--ink-3); }

.thumbs { display: grid; grid-template-columns: repeat(2, 1fr); gap: .8rem .6rem; padding: .4rem; }
.thumb { display: block; padding: .3rem; border: 2px solid transparent; border-radius: 6px; background: none; cursor: pointer; color: var(--ink-3); font: inherit; font-size: .78rem; text-align: center; }
.thumb:hover { background: var(--paper-2); }
.thumb.cur { border-color: var(--accent); color: var(--ink); }
.thumb .tcv { display: block; width: 100%; aspect-ratio: 612 / 792; background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,.25); margin-bottom: .3rem; }
.thumb canvas { width: 100%; height: 100%; display: block; }
.viewer[data-tone="night"] .thumb canvas { filter: invert(.92) hue-rotate(180deg); }

.vsearch { padding: .4rem .5rem .6rem; display: flex; flex-direction: column; gap: .5rem; }
.vsearch-row { display: flex; gap: .3rem; }
.vsearch input { flex: 1; min-width: 0; font: inherit; font-size: 1rem; padding: .5rem .6rem; background: var(--paper-2); color: var(--ink); border: 1px solid var(--rule); border-radius: 6px; }
.vsearch-status { font-size: .85rem; color: var(--ink-3); min-height: 1.2rem; }
.vresults { list-style: none; margin: 0; padding: 0; }
.vresults button {
  display: block; width: 100%; text-align: left; background: none; border: 0; border-bottom: 1px solid var(--rule); padding: .55rem .5rem;
  font: inherit; font-size: .9rem; line-height: 1.35; color: var(--ink-2); cursor: pointer;
}
.vresults button:hover { background: var(--paper-2); }
.vresults button.cur { background: var(--accent-soft); color: var(--ink); }
.vresults .rp { display: block; font-family: var(--mono); font-size: .72rem; color: var(--ink-3); letter-spacing: .06em; text-transform: uppercase; }
.vresults mark { background: rgba(255, 196, 0, .45); color: inherit; border-radius: 2px; padding: 0 1px; }

/* ---------- stage and pages ---------- */
.vmain { grid-row: 2; grid-column: 2; position: relative; overflow: auto; outline: none; scroll-behavior: auto; }
.vpages { width: max-content; min-width: 100%; display: flex; flex-direction: column; align-items: center; gap: 16px; padding: 20px 0 48px; }
.vpage { position: relative; flex: none; background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.18), 0 8px 24px -10px rgba(0,0,0,.35); }
.vpage .vph { position: absolute; inset: 0; display: grid; place-items: center; color: #b8b2a2; font-size: 1.4rem; font-style: italic; user-select: none; }
.vpage canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.viewer[data-tone="sepia"] .vpage canvas { filter: sepia(.38) saturate(.9) brightness(.97); }
.viewer[data-tone="night"] .vpage { background: #0a1b2b; box-shadow: 0 1px 2px rgba(0,0,0,.5), 0 8px 24px -10px rgba(0,0,0,.8); }
.viewer[data-tone="night"] .vpage canvas { filter: invert(.9) hue-rotate(180deg) contrast(.95); }
.viewer[data-tone="night"] .vpage .vph { color: #2c4458; }

.vpage .textLayer { opacity: 1; }
.vpage .textLayer ::selection { background: rgba(70, 120, 210, .35); }
.vpage .textLayer mark.hl { background: rgba(255, 196, 0, .5); color: transparent; border-radius: 2px; padding: 0; }
.vpage .textLayer mark.hl.cur { background: rgba(255, 120, 0, .65); box-shadow: 0 0 0 2px rgba(255, 120, 0, .65); }

/* ---------- loading and errors ---------- */
.vload { position: absolute; inset: 0; z-index: 10; display: grid; place-items: center; background: var(--stage); text-align: center; padding: 2rem; }
.vload[hidden] { display: none; }
.vload h2 { font-size: 1.6rem; margin-bottom: .4rem; }
.vload p { color: var(--ink-3); max-width: 28rem; margin: 0 auto .8rem; font-size: 1rem; }
.vbarload { width: min(18rem, 70vw); height: 4px; background: var(--rule); border-radius: 2px; margin: 1rem auto .4rem; overflow: hidden; }
.vbarload div { height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.vload code { font-family: var(--mono); font-size: .85rem; background: var(--paper); border: 1px solid var(--rule); padding: .1rem .4rem; border-radius: 4px; }
.vload .err { color: var(--lost); }

.toast {
  position: absolute; left: 50%; bottom: 1.4rem; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--ink); color: var(--paper); padding: .6rem 1rem; border-radius: 8px; font-size: .95rem; z-index: 60;
  transition: opacity .25s, transform .25s; display: flex; gap: .8rem; align-items: center; max-width: 92%;
}
.toast.on { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
.toast button { background: none; border: 0; color: var(--accent-soft); font: inherit; text-decoration: underline; cursor: pointer; padding: 0; }

.vhelp { position: absolute; right: .8rem; bottom: .8rem; z-index: 25; }
.vhelp button { width: 2rem; height: 2rem; border-radius: 50%; border: 1px solid var(--rule); background: var(--paper); color: var(--ink-3); cursor: pointer; font: inherit; }
.vhelp-pop { position: absolute; right: 0; bottom: 2.6rem; width: 15rem; background: var(--paper); border: 1px solid var(--rule); border-radius: 8px; padding: .8rem 1rem; box-shadow: var(--shadow); font-size: .9rem; }
.vhelp-pop[hidden] { display: none; }
.vhelp-pop dl { display: grid; grid-template-columns: auto 1fr; gap: .25rem .8rem; margin: 0; }
.vhelp-pop dt { font-family: var(--mono); color: var(--accent-text); }
.vhelp-pop dd { margin: 0; color: var(--ink-2); }

/* ---------- mobile: sidebar becomes an overlay ---------- */
@media (max-width: 860px) {
  .viewer, .viewer.side-open { grid-template-columns: 0 1fr; }
  .vside { position: absolute; z-index: 40; left: 0; top: var(--bar); bottom: 0; width: min(88vw, 21rem); box-shadow: 6px 0 24px -8px rgba(0,0,0,.4); transform: translateX(-102%); visibility: visible; transition: transform .25s; }
  .viewer.side-open .vside { transform: none; }
  .vmain { grid-column: 1 / -1; }
  .vhelp { display: none; }
}
@media (prefers-reduced-motion: reduce) { .vside, .toast, .v-progress div { transition: none; } }

@media print { .viewer { display: none; } }
