/* ============================================================
   Flipbook — premium dark theme
   ============================================================ */

:root{
  --ink-950: #0b0d10;
  --ink-900: #12151a;
  --ink-800: #1b1f26;
  --ink-700: #262b33;
  --ink-600: #333a44;
  --paper: #f6f3ec;
  --amber: #c9a46a;
  --amber-soft: rgba(201,164,106,0.16);
  --text-dim: #9aa1ac;
  --text: #e9e9e6;
  --radius: 3px;
  --bar-h: 58px;
  --ctrl-h: 76px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

*{ box-sizing: border-box; }
html,body{
  margin:0; padding:0; height:100%;
  background: var(--ink-950);
  overscroll-behavior: none;
}
body{
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
}

#app{
  position: fixed; inset: 0;
  display: flex; flex-direction: column;
  background:
    radial-gradient(ellipse at 50% 18%, #1c2028 0%, var(--ink-950) 62%),
    linear-gradient(180deg, var(--ink-950), #08090b);
  overflow: hidden;
}

/* ---------------- top bar ---------------- */
.topbar{
  height: calc(var(--bar-h) + var(--safe-top));
  padding-top: var(--safe-top);
  display:flex; align-items:center; justify-content:space-between;
  padding-left: 18px; padding-right: 14px;
  flex: 0 0 auto;
  background: linear-gradient(180deg, rgba(0,0,0,.35), rgba(0,0,0,0));
  z-index: 40;
}
.brand{ display:flex; align-items:center; gap:10px; }
.brand-mark{
  width:9px; height:9px; border-radius:50%;
  background: var(--amber);
  box-shadow: 0 0 10px 1px rgba(201,164,106,.6);
}
.brand-text{
  font-size: 14px; letter-spacing: .04em; color: var(--text-dim);
  font-weight: 500;
}
.topbar-actions{ display:flex; align-items:center; gap:4px; }

.icon-btn{
  appearance:none; border:none; background:transparent; color: var(--text-dim);
  width:38px; height:38px; border-radius: 50%;
  display:flex; align-items:center; justify-content:center;
  cursor:pointer; transition: background .15s ease, color .15s ease;
  text-decoration:none;
}
.icon-btn svg{ width:18px; height:18px; fill:none; stroke:currentColor; stroke-width:1.7; stroke-linecap:round; stroke-linejoin:round; }
.icon-btn:hover{ background: rgba(255,255,255,.08); color: var(--text); }
.icon-btn:active{ background: rgba(255,255,255,.14); }
.icon-btn.active{ color: var(--amber); background: var(--amber-soft); }

/* ---------------- stage ---------------- */
.stage{
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
}

.nav-zone{
  position:absolute; top:0; bottom:0; width: 34%;
  background:transparent; border:none; cursor: pointer;
  z-index: 5; padding:0;
}
.nav-zone--prev{ left:0; }
.nav-zone--next{ right:0; }
.nav-zone:focus{ outline:none; }

.viewport{
  position:absolute; inset:0;
  display:flex; align-items:center; justify-content:center;
  z-index: 10;
  touch-action: none;
}

.book-shadow{
  position:absolute;
  border-radius: 6px;
  background: radial-gradient(ellipse at center, rgba(0,0,0,.55) 0%, rgba(0,0,0,0) 72%);
  filter: blur(2px);
  pointer-events:none;
  transition: width .25s ease, height .25s ease, opacity .25s ease;
}

.book{
  position: relative;
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
  -webkit-perspective: 2600px;
  perspective: 2600px;
  cursor: grab;
}
.book.dragging{ cursor: grabbing; }
.book.zoomed{ cursor: default; }

/* single static page slot */
.leaf-slot{
  position:absolute; top:0;
  background: var(--paper);
  overflow:hidden;
  box-shadow: 0 0 0 1px rgba(0,0,0,.25);
}
.leaf-slot img{
  width:100%; height:100%; display:block; object-fit: fill;
  user-select:none; -webkit-user-drag:none; pointer-events:none;
}
.leaf-slot.left{ left:0; border-radius: 5px 0 0 5px; }
.leaf-slot.right{ right:0; border-radius: 0 5px 5px 0; }
.leaf-slot.single{ left:0; border-radius: 5px; }

/* inner curved shading near spine, static pages */
.leaf-slot .spine-shade{
  position:absolute; top:0; bottom:0; width: 7%;
  pointer-events:none;
}
.leaf-slot.left .spine-shade{
  right:0;
  background: linear-gradient(90deg, rgba(0,0,0,0), rgba(0,0,0,.22));
}
.leaf-slot.right .spine-shade{
  left:0;
  background: linear-gradient(270deg, rgba(0,0,0,0), rgba(0,0,0,.22));
}
.leaf-slot .outer-edge{
  position:absolute; top:0; bottom:0; width: 2.5%;
  pointer-events:none;
  background: linear-gradient(90deg, rgba(0,0,0,.10), rgba(0,0,0,0));
}
.leaf-slot.right .outer-edge{ right:0; left:auto; background: linear-gradient(270deg, rgba(0,0,0,.10), rgba(0,0,0,0)); }

/* the animated flipping leaf */
.leaf-flip{
  position:absolute; top:0;
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
  z-index: 20;
  will-change: transform;
}
.leaf-face{
  position:absolute; inset:0;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  background: var(--paper);
  box-shadow: 0 0 0 1px rgba(0,0,0,.25);
}
/* overflow:hidden must live on an INNER wrapper, not the face itself —
   Chromium fails to cull the backface correctly when overflow:hidden and
   backface-visibility:hidden sit on the same element. */
.leaf-face .face-inner{
  position:absolute; inset:0;
  overflow:hidden;
}
.leaf-face img{
  width:100%; height:100%; display:block; object-fit:fill;
  user-select:none; -webkit-user-drag:none; pointer-events:none;
}
.leaf-face.back{ -webkit-transform: rotateY(180deg); transform: rotateY(180deg); }

.leaf-face .shade{
  position:absolute; inset:0; pointer-events:none;
  background: linear-gradient(90deg, rgba(0,0,0,0), rgba(0,0,0,0));
  transition: none;
}

.leaf-face.blank{ background: var(--paper); }
.leaf-face.blank::after{
  content:""; position:absolute; inset:0;
  background: linear-gradient(135deg, rgba(0,0,0,.03), rgba(0,0,0,.08));
}

/* drag corner affordance */
.drag-corner{
  position:absolute; width:64px; height:64px;
  z-index: 25; cursor: grab;
  background: transparent;
}
.drag-corner.tr{ top:0; right:0; }
.drag-corner.br{ bottom:0; right:0; }
.drag-corner.tl{ top:0; left:0; }
.drag-corner.bl{ bottom:0; left:0; }

/* ---------------- loading ---------------- */
.loading{
  position:absolute; inset:0; z-index:60;
  display:flex; flex-direction:column; align-items:center; justify-content:center;
  gap:14px; background: var(--ink-950);
  transition: opacity .4s ease;
}
.loading.hidden{ opacity:0; pointer-events:none; }
.loading-spinner{
  width:34px; height:34px; border-radius:50%;
  border: 2px solid rgba(201,164,106,.18);
  border-top-color: var(--amber);
  animation: spin 0.9s linear infinite;
}
@keyframes spin{ to{ transform: rotate(360deg); } }
.loading-text{ font-size:13px; color: var(--text-dim); letter-spacing:.03em; }

.hint{
  position:absolute; left:50%; bottom: calc(var(--ctrl-h) + 14px + var(--safe-bottom));
  transform: translateX(-50%);
  font-size:12px; color: var(--text-dim);
  background: rgba(0,0,0,.35);
  padding:7px 14px; border-radius: 20px;
  backdrop-filter: blur(6px);
  pointer-events:none; z-index: 30;
  transition: opacity .6s ease;
  white-space:nowrap;
}
.hint.hidden{ opacity:0; }

/* ---------------- control bar ---------------- */
.controlbar{
  flex: 0 0 auto;
  height: calc(var(--ctrl-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  display:flex; align-items:center; gap:14px;
  padding-left:18px; padding-right:18px;
  background: linear-gradient(0deg, rgba(0,0,0,.42), rgba(0,0,0,0));
  z-index: 40;
}
.ctrl-btn{
  appearance:none; border: 1px solid rgba(255,255,255,.1);
  background: rgba(255,255,255,.04); color: var(--text);
  width:42px; height:42px; border-radius:50%; flex:0 0 auto;
  display:flex; align-items:center; justify-content:center; cursor:pointer;
  transition: background .15s ease, transform .1s ease;
}
.ctrl-btn svg{ width:18px; height:18px; fill:none; stroke:currentColor; stroke-width:2; stroke-linecap:round; stroke-linejoin:round; }
.ctrl-btn:hover{ background: rgba(255,255,255,.1); }
.ctrl-btn:active{ transform: scale(.92); }
.ctrl-btn:disabled{ opacity:.3; cursor:default; }
.ctrl-btn:disabled:active{ transform:none; }

.page-indicator{
  flex:1 1 auto; display:flex; flex-direction:column; align-items:center; gap:6px;
  min-width:0;
}
#page-label{
  font-size:12.5px; color: var(--text-dim); letter-spacing:.03em;
  font-variant-numeric: tabular-nums;
}
#page-slider{
  -webkit-appearance:none; appearance:none;
  width:100%; max-width:420px; height:3px;
  background: var(--ink-600); border-radius: 2px; outline:none;
}
#page-slider::-webkit-slider-thumb{
  -webkit-appearance:none; width:13px; height:13px; border-radius:50%;
  background: var(--amber); cursor:pointer;
  box-shadow: 0 0 0 3px rgba(201,164,106,.18);
}
#page-slider::-moz-range-thumb{
  width:13px; height:13px; border-radius:50%; border:none;
  background: var(--amber); cursor:pointer;
}

/* ---------------- thumbnails drawer ---------------- */
.thumbs-overlay{
  position: fixed; inset:0; background: rgba(0,0,0,.55);
  backdrop-filter: blur(2px);
  opacity:0; pointer-events:none; transition: opacity .25s ease;
  z-index: 70;
}
.thumbs-overlay.open{ opacity:1; pointer-events:auto; }

.thumbs-drawer{
  position: fixed; top:0; right:0; bottom:0;
  width: min(360px, 86vw);
  background: var(--ink-900);
  border-left: 1px solid rgba(255,255,255,.08);
  transform: translateX(100%);
  transition: transform .3s cubic-bezier(.2,.8,.2,1);
  z-index: 80;
  display:flex; flex-direction:column;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}
.thumbs-drawer.open{ transform: translateX(0); }

.thumbs-header{
  display:flex; align-items:center; justify-content:space-between;
  padding: 16px 18px; font-size:13px; letter-spacing:.04em; color: var(--text-dim);
  border-bottom: 1px solid rgba(255,255,255,.06);
  flex:0 0 auto;
}
.thumbs-grid{
  flex:1 1 auto; overflow-y:auto;
  display:grid; grid-template-columns: repeat(3, 1fr);
  gap:10px; padding:16px;
}
.thumb-item{
  cursor:pointer; border-radius: var(--radius); overflow:hidden;
  border: 1.5px solid transparent;
  background: var(--ink-800);
  transition: border-color .15s ease, transform .1s ease;
  position:relative;
}
.thumb-item img{ width:100%; display:block; }
.thumb-item:hover{ transform: translateY(-2px); }
.thumb-item.current{ border-color: var(--amber); }
.thumb-num{
  position:absolute; bottom:0; left:0; right:0;
  font-size:10px; text-align:center; padding:3px 0;
  background: linear-gradient(0deg, rgba(0,0,0,.7), rgba(0,0,0,0));
  color:#fff;
}

/* ---------------- responsive ---------------- */
@media (max-width: 720px){
  .topbar{ padding-left:12px; padding-right:8px; }
  .brand-text{ display:none; }
  .icon-btn{ width:34px; height:34px; }
  .controlbar{ gap:8px; padding-left:10px; padding-right:10px; }
  .ctrl-btn{ width:38px; height:38px; }
  .hint{ font-size:11px; padding:6px 12px; }
  #btn-download{ display:none; }
}

@media (max-width: 480px){
  .icon-btn:nth-child(2){ } /* keep order */
}

/* short viewports (mobile landscape phones): reclaim vertical space */
@media (max-height: 480px){
  :root{ --bar-h: 42px; --ctrl-h: 54px; }
  .hint{ display:none; }
}

/* reduced motion */
@media (prefers-reduced-motion: reduce){
  .loading-spinner{ animation-duration: 1.6s; }
}

/* fullscreen tweaks */
#app:fullscreen,
#app:-webkit-full-screen{
  background: var(--ink-950);
}
