:root {
  --bg: #ececec;
  --bg-stage: #e4e4e4;
  --ink: #222;
  --muted: #666;
  --bar: rgba(255, 255, 255, 0.92);
  --line: rgba(0, 0, 0, 0.08);
  --accent: #111;
  --brand: rgba(84, 83, 81, 0.4);
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.22);
  --radius: 10px;
  --bar-h: 56px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
}

.app {
  display: flex;
  flex-direction: column;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  position: relative;
}

.toolbar {
  flex: 0 0 auto;
  min-height: var(--bar-h);
  padding: calc(8px + var(--safe-top)) 16px 8px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px;
  background: var(--bar);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(10px);
  z-index: 5;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-logo {
  flex: 0 0 auto;
  display: block;
  height: 18px;
  width: auto;
  color: var(--brand);
}

.brand h1 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--brand);
}

.pager,
.actions,
.zoom-controls {
  display: flex;
  align-items: center;
  gap: 4px;
}

.pager { justify-content: center; }
.actions { justify-content: flex-end; }

.icon-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--ink);
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 18px;
}

.icon-btn:hover { background: rgba(0, 0, 0, 0.06); }
.icon-btn:disabled { opacity: 0.35; cursor: default; }
.icon-btn:focus-visible { outline: 2px solid #111; outline-offset: 2px; }
a.icon-btn { text-decoration: none; color: inherit; }
.icon-btn svg { display: block; }

.page-indicator {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}

#page-input {
  width: 3.4rem;
  height: 32px;
  text-align: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  font: inherit;
  color: var(--ink);
}

.stage {
  flex: 1;
  min-height: 0;
  position: relative;
  overflow: hidden;
  background:
    radial-gradient(1200px 500px at 50% 0%, rgba(255, 255, 255, 0.7), transparent 60%),
    var(--bg-stage);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px 12px calc(12px + var(--safe-bottom));
}

#book-host {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  /* Room for a turning leaf to swing past the paper edge. */
  --flip-pad: min(72px, 8vh);
}

.stf__wrapper {
  position: relative;
}

#spine-stitch {
  display: none;
}

.page-gutter {
  display: none;
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 1;
}

.stf__parent.is-open-spread .page-gutter,
.zoom-leaf .page-gutter {
  display: block;
}

/* Subtle crease: soft left fold, tighter right, clipped to paper. */
.page-gutter[data-side="left"] {
  box-shadow:
    inset -1px 0 0 rgba(0, 0, 0, 0.08),
    inset -24px 0 18px -10px rgba(0, 0, 0, 0.1);
}

.page-gutter[data-side="right"] {
  box-shadow:
    inset 1px 0 0 rgba(0, 0, 0, 0.06),
    inset 10px 0 10px -6px rgba(0, 0, 0, 0.07);
}

#book-frame {
  position: relative;
  overflow: visible;
  width: 100%;
  height: 100%;
  max-width: calc(100% - 2 * var(--flip-pad));
  max-height: calc(100% - 2 * var(--flip-pad));
  display: flex;
  align-items: center;
  justify-content: center;
}

#book-frame.is-sized {
  display: block;
  width: var(--spread-w);
  height: var(--leaf-h);
  max-width: calc(100% - 2 * var(--flip-pad));
  max-height: calc(100% - 2 * var(--flip-pad));
  border-radius: 2px;
  box-shadow: var(--shadow);
}

#book-frame.is-sized.closed-front,
#book-frame.is-sized.closed-back {
  width: var(--leaf-w);
  overflow: hidden;
}

#book-frame.closed-front #book {
  margin-left: calc(-1 * var(--leaf-w));
}

#book {
  max-width: 100%;
  max-height: 100%;
}

#book-frame.is-sized #book {
  width: var(--spread-w);
  height: var(--leaf-h);
  max-width: none;
  max-height: none;
}

.page {
  position: relative;
  background: #fff;
  overflow: hidden;
}

.page-hits {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 2;
}

.page-hits .hit {
  position: absolute;
  background: rgba(255, 214, 0, 0.38);
  outline: 1px solid rgba(196, 146, 0, 0.55);
  border-radius: 1px;
}

.page img,
.page-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  user-select: none;
  -webkit-user-drag: none;
  pointer-events: none;
  background: #fff;
}

.stf__parent,
.stf__wrapper,
.stf__block {
  overflow: visible;
}

.stf__parent {
  margin: 0 auto;
  box-shadow: none;
  border-radius: 2px;
}

.loader {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 13px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  pointer-events: none;
}

.loader[hidden] { display: none; }

.zoom-overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(20, 20, 20, 0.94);
  color: #fff;
  display: flex;
  flex-direction: column;
}

.zoom-overlay[hidden] { display: none; }

.zoom-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(8px + var(--safe-top)) 12px 8px;
  font-size: 13px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.zoom-overlay .icon-btn { color: #fff; }
.zoom-overlay .icon-btn:hover { background: rgba(255, 255, 255, 0.1); }

.zoom-stage {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  touch-action: none;
  cursor: grab;
}

.zoom-stage.is-panning { cursor: grabbing; }

.zoom-spread {
  transform-origin: 0 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0;
  width: 100%;
  height: 100%;
  will-change: transform;
}

.zoom-leaf {
  position: relative;
  overflow: hidden;
  display: block;
  max-height: calc(100dvh - 72px);
  max-width: 46vw;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.45);
}

.zoom-leaf:only-child {
  max-width: 92vw;
}

.zoom-spread img {
  max-height: calc(100dvh - 72px);
  max-width: 46vw;
  width: auto;
  height: auto;
  object-fit: contain;
  display: block;
  user-select: none;
  -webkit-user-drag: none;
  background: #fff;
}

.zoom-leaf:only-child img {
  max-width: 92vw;
}

.search-panel {
  position: absolute;
  top: calc(var(--bar-h) + var(--safe-top));
  right: 12px;
  width: min(360px, calc(100% - 24px));
  max-height: min(420px, 60dvh);
  display: flex;
  flex-direction: column;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.16);
  z-index: 12;
  overflow: hidden;
}

.search-panel[hidden] { display: none; }

.search-field {
  display: block;
  padding: 10px 10px 6px;
}

#search-input {
  width: 100%;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px;
  font: inherit;
  color: var(--ink);
}

.search-status {
  padding: 0 12px 8px;
  font-size: 12px;
  color: var(--muted);
}

.search-results {
  overflow: auto;
  border-top: 1px solid var(--line);
}

.search-hit {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: #fff;
  padding: 10px 12px;
  cursor: pointer;
  font: inherit;
  color: var(--ink);
}

.search-hit:hover,
.search-hit:focus {
  background: #f4f4f4;
}

.search-hit .page-no {
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.search-hit .snippet {
  margin-top: 4px;
  font-size: 13px;
  line-height: 1.35;
}

.search-hit mark {
  background: #ffe08a;
  color: inherit;
  padding: 0 1px;
}

@media (max-width: 720px) {
  .toolbar {
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "brand actions"
      "pager pager";
  }
  .brand { grid-area: brand; }
  .actions { grid-area: actions; }
  .pager { grid-area: pager; }
  .brand h1 { font-size: 12px; }
  .stage { padding: 8px 8px calc(8px + var(--safe-bottom)); }
}
