:root {
  --bg: #eef1eb;
  --panel: rgba(255,255,255,.92);
  --ink: #263128;
  --muted: #69756d;
  --line: rgba(28,48,34,.12);
  --accent: #6ea13f;
  --page-ratio: 0.7056;
  --shadow: none;
  --radius: 16px;
  --zoom: 1;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--ink); }
body {
  min-height: 100dvh;
  overflow: hidden;
  font-family: Tahoma, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}
button { font: inherit; }

.app {
  height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  position: relative;
  background:
    radial-gradient(circle at 20% 10%, rgba(171,205,133,.16), transparent 28%),
    radial-gradient(circle at 85% 90%, rgba(91,155,70,.10), transparent 32%),
    var(--bg);
}

.topbar, .bottombar {
  z-index: 50;
  background: var(--panel);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-color: var(--line);
}
.topbar {
  min-height: 66px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px clamp(12px, 2.2vw, 28px);
}
.brand-title { font-size: clamp(14px, 1.45vw, 18px); font-weight: 700; }
.brand-subtitle { color: var(--muted); font-size: 11px; margin-top: 3px; }
.toolbar { display: flex; align-items: center; gap: 7px; direction: ltr; }
.icon-btn {
  border: 1px solid var(--line);
  background: rgba(255,255,255,.8);
  color: var(--ink);
  min-width: 38px;
  height: 38px;
  border-radius: 10px;
  display: inline-grid;
  place-items: center;
  cursor: pointer;
  transition: transform .15s ease, background .15s ease, border-color .15s ease;
}
.icon-btn:hover { background: #fff; border-color: rgba(60,105,53,.28); transform: translateY(-1px); }
.icon-btn:active { transform: translateY(0) scale(.97); }
.icon-btn:focus-visible, .edge-nav:focus-visible { outline: 3px solid rgba(97,151,66,.25); outline-offset: 2px; }
.zoom-value { min-width: 58px; font-size: 12px; }

.viewer {
  min-height: 0;
  position: relative;
  display: grid;
  place-items: center;
  overflow: hidden;
  padding: 18px 64px;
}
.book-viewport {
  width: 100%;
  height: 100%;
  overflow: auto;
  scrollbar-width: thin;
  display: grid;
  place-items: center;
  overscroll-behavior: contain;
}
.book-wrap {
  transform: scale(var(--zoom));
  transform-origin: center center;
  transition: transform .18s ease;
  will-change: transform;
}
.book {
  --page-h: min(78vh, 790px);
  --page-w: calc(var(--page-h) * var(--page-ratio));
  width: calc(var(--page-w) * 2);
  height: var(--page-h);
  position: relative;
  perspective: 2500px;
  transform-style: preserve-3d;
  transition: transform .42s cubic-bezier(.2,.78,.2,1);
  will-change: transform;
}
.book.closed { transform: translateX(calc(var(--page-w) / 2)); }
.book.finished { transform: translateX(calc(var(--page-w) / -2)); }
.book::after {
  content: "";
  position: absolute;
  top: 1.1%;
  bottom: 1.1%;
  left: 50%;
  width: 1px;
  transform: translateX(-.5px);
  background: transparent;
  z-index: 1000;
  pointer-events: none;
  opacity: 0;
}
.book.closed::after, .book.finished::after { opacity: 0; }

.sheet {
  position: absolute;
  inset: 0 auto 0 0;
  width: 50%;
  height: 100%;
  transform-origin: 100% 50%;
  transform-style: preserve-3d;
  transition: transform .82s cubic-bezier(.2,.63,.18,1);
  will-change: transform;
}
.sheet.turned { transform: rotateY(180deg); }
.sheet.is-turning { transition-duration: .68s; }
.page-face {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #fff;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  box-shadow: var(--shadow);
}
.page-face.front { border-radius: var(--radius) 4px 4px var(--radius); }
.page-face.back {
  transform: rotateY(180deg);
  border-radius: 4px var(--radius) var(--radius) 4px;
}
.page-face img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #fff;
  pointer-events: none;
}
.page-face::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: .22;
}
.front::after { background: transparent; }
.back::after { background: transparent; }
.sheet.is-turning .page-face { box-shadow: none; }

.edge-nav {
  position: absolute;
  z-index: 40;
  top: 50%;
  transform: translateY(-50%);
  width: 44px;
  height: 72px;
  border: 1px solid var(--line);
  background: rgba(255,255,255,.8);
  color: var(--ink);
  border-radius: 14px;
  font-size: 34px;
  line-height: 1;
  cursor: pointer;
  backdrop-filter: blur(8px);
  box-shadow: 0 8px 30px rgba(31,45,34,.08);
  transition: opacity .2s ease, transform .15s ease, background .15s ease;
}
.edge-nav:hover { background: #fff; }
.edge-nav:active { transform: translateY(-50%) scale(.97); }
.edge-nav:disabled { opacity: .25; cursor: default; }
.edge-prev { right: 12px; }
.edge-next { left: 12px; }

.bottombar {
  min-height: 52px;
  border-top: 1px solid var(--line);
  display: grid;
  grid-template-columns: auto minmax(120px, 420px) 1fr;
  align-items: center;
  gap: 16px;
  padding: 8px clamp(12px, 2.2vw, 28px);
}
.page-status { font-size: 13px; white-space: nowrap; }
.progress { height: 5px; border-radius: 999px; background: rgba(55,77,61,.12); overflow: hidden; direction: rtl; }
.progress-fill { height: 100%; width: 0; background: var(--accent); border-radius: inherit; transition: width .25s ease; }
.hint { color: var(--muted); font-size: 11px; text-align: left; }

.thumbs-panel {
  position: fixed;
  z-index: 100;
  top: 0;
  bottom: 0;
  right: 0;
  width: min(410px, 88vw);
  background: rgba(248,250,247,.97);
  backdrop-filter: blur(18px);
  box-shadow: -18px 0 60px rgba(24,38,28,.18);
  transform: translateX(105%);
  transition: transform .28s ease;
  display: grid;
  grid-template-rows: auto 1fr;
  border-left: 1px solid var(--line);
}
.thumbs-panel.open { transform: translateX(0); }
.thumbs-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.thumbs-grid {
  overflow: auto;
  padding: 14px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0,1fr));
  gap: 12px;
  align-content: start;
}
.thumb {
  border: 1px solid var(--line);
  background: #fff;
  padding: 6px;
  border-radius: 11px;
  cursor: pointer;
  position: relative;
  transition: transform .15s ease, border-color .15s ease, box-shadow .15s ease;
}
.thumb:hover { transform: translateY(-2px); border-color: rgba(91,145,62,.4); box-shadow: 0 9px 24px rgba(26,42,31,.09); }
.thumb.active { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(110,161,63,.14); }
.thumb img { width: 100%; aspect-ratio: var(--page-ratio); object-fit: cover; display: block; border-radius: 7px; }
.thumb span { position: absolute; bottom: 9px; left: 9px; min-width: 25px; height: 25px; padding: 0 6px; border-radius: 999px; display: grid; place-items: center; color: #fff; background: rgba(24,35,28,.75); font-size: 11px; }

.loading {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 12px;
  background: #f5f7f2;
  transition: opacity .3s ease, visibility .3s ease;
}
.loading.hidden { opacity: 0; visibility: hidden; }
.spinner { width: 32px; height: 32px; border-radius: 50%; border: 3px solid rgba(90,122,78,.15); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.mobile-page { display: none; }

@media (max-width: 900px) {
  .brand-subtitle { display: none; }
  .topbar { min-height: 58px; }
  .viewer { padding: 10px 50px; }
  .book { --page-h: min(75vh, 700px); }
  .hint { display: none; }
  .bottombar { grid-template-columns: auto 1fr; }
}

@media (max-width: 720px), (orientation: portrait) and (max-width: 900px) {
  body { overflow: hidden; }
  .topbar { gap: 8px; padding: 8px 10px; }
  .brand-title { max-width: 44vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .toolbar { gap: 4px; }
  .icon-btn { min-width: 34px; height: 34px; border-radius: 9px; }
  #zoomOutBtn, #zoomInBtn, #zoomResetBtn { display: none; }
  .viewer { padding: 8px 42px; }
  .book { display: none; }
  .book-wrap { transform: none !important; }
  .mobile-page {
    display: block;
    position: relative;
    width: min(82vw, calc(76dvh * var(--page-ratio)));
    height: min(76dvh, calc(82vw / var(--page-ratio)));
    border-radius: 13px;
    overflow: hidden;
    background: #fff;
    box-shadow: var(--shadow);
    touch-action: pan-y;
  }
  .mobile-page img { width: 100%; height: 100%; object-fit: contain; background: #fff; display: block; }
  .mobile-page.turn-next { animation: mobileNext .34s ease; transform-origin: right center; }
  .mobile-page.turn-prev { animation: mobilePrev .34s ease; transform-origin: left center; }
  @keyframes mobileNext { 0% { transform: rotateY(0) scale(1); opacity: 1; } 48% { transform: rotateY(28deg) scale(.985); opacity: .55; } 100% { transform: rotateY(0) scale(1); opacity: 1; } }
  @keyframes mobilePrev { 0% { transform: rotateY(0) scale(1); opacity: 1; } 48% { transform: rotateY(-28deg) scale(.985); opacity: .55; } 100% { transform: rotateY(0) scale(1); opacity: 1; } }
  .edge-nav { width: 34px; height: 62px; font-size: 28px; border-radius: 11px; }
  .edge-prev { right: 5px; }
  .edge-next { left: 5px; }
  .bottombar { min-height: 45px; padding: 7px 10px; gap: 10px; }
  .page-status { font-size: 12px; }
  .thumbs-grid { grid-template-columns: repeat(3,minmax(0,1fr)); gap: 8px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; scroll-behavior: auto !important; }
}
