:root {
    --bg: #2b2f36;
    --bar: #1c1f24;
    --bar-line: #33373e;
    --text: #e9ecf1;
    --muted: #9aa3ae;
    --accent: #2f6fd6;
    --panel: #23272d;
    --toolbar-h: 52px;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    overflow: hidden;
    -webkit-tap-highlight-color: transparent;
}

button, input { font: inherit; color: inherit; }

/* ---------- Toolbar ---------- */
.toolbar {
    position: fixed;
    inset: 0 0 auto 0;
    height: var(--toolbar-h);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 0 12px;
    padding-top: env(safe-area-inset-top);
    background: var(--bar);
    border-bottom: 1px solid var(--bar-line);
    z-index: 30;
}
.tb-group { display: flex; align-items: center; gap: 2px; }
.tb-left, .tb-right { flex: 1; }
.tb-right { justify-content: flex-end; }
.tb-title {
    margin-left: 8px;
    font-weight: 600;
    letter-spacing: .02em;
    white-space: nowrap;
}

.tb-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    border: 0;
    border-radius: 8px;
    background: transparent;
    cursor: pointer;
    color: var(--text);
}
.tb-btn:hover { background: rgba(255,255,255,.08); }
.tb-btn.active { background: var(--accent); }
.tb-btn:disabled { opacity: .35; cursor: default; background: transparent; }
.tb-btn svg, .side-arrow svg {
    width: 20px;
    height: 20px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.page-box {
    display: flex;
    align-items: center;
    gap: 6px;
    margin: 0 4px;
    color: var(--muted);
    white-space: nowrap;
}
.page-box input {
    width: 58px;
    height: 30px;
    text-align: center;
    border: 1px solid var(--bar-line);
    border-radius: 6px;
    background: #111316;
}

/* ---------- Stage / book ---------- */
.stage {
    position: fixed;
    inset: var(--toolbar-h) 0 0 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 28px 72px;
    overflow: hidden;
}

.book {
    position: relative;
    flex: none;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    cursor: pointer;
}
.book-inner {
    position: absolute;
    inset: 0;
    transform-style: preserve-3d;
}

.slot {
    position: absolute;
    top: 0;
    left: 0;
    overflow: hidden;
}
.page {
    position: absolute;
    inset: 0;
    background: #fff;
    box-shadow: 0 10px 30px rgba(0,0,0,.35);
}
.page img {
    display: block;
    width: 100%;
    height: 100%;
    user-select: none;
    -webkit-user-drag: none;
    pointer-events: none;
}
/* Darkening toward the spine so the spread reads as a bound book */
.page::after {
    content: "";
    position: absolute;
    inset: 0;
    pointer-events: none;
}
.slot.left .page::after {
    background: linear-gradient(to left, rgba(0,0,0,.22), rgba(0,0,0,.06) 4%, transparent 10%);
}
.slot.right .page::after {
    background: linear-gradient(to right, rgba(0,0,0,.22), rgba(0,0,0,.06) 4%, transparent 10%);
}
.book.single .slot.right .page::after {
    background: linear-gradient(to right, rgba(0,0,0,.12), transparent 4%);
}
.spots { position: absolute; inset: 0; }
.page .hotspot {
    position: absolute;
    display: block;
    border-radius: 3px;
    cursor: pointer;
    z-index: 2;
}
.page .hotspot:hover { background: rgba(47,111,214,.18); }

/* Shadow cast by the turning page */
.cast {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 45%;
    opacity: 0;
    pointer-events: none;
    z-index: 3;
}
.cast.right { background: linear-gradient(to right, rgba(0,0,0,.45), rgba(0,0,0,.12) 35%, transparent); }
.cast.left { background: linear-gradient(to left, rgba(0,0,0,.45), rgba(0,0,0,.12) 35%, transparent); }

/* The turning page: a chain of strips bent in 3D */
.leaf {
    position: absolute;
    top: 0;
    transform-style: preserve-3d;
    pointer-events: none;
    z-index: 4;
}
.strip {
    position: absolute;
    top: 0;
    height: 100%;
    transform-origin: 0 50%;
    transform-style: preserve-3d;
}
.face {
    position: absolute;
    inset: 0;
    background-color: #fff;
    background-repeat: no-repeat;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
}
.face.back { transform: rotateY(180deg); background-color: #f3f3f1; }
.face i {
    position: absolute;
    inset: 0;
    background: #000;
    opacity: 0;
}

.side-arrow {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 48px;
    height: 72px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 0;
    border-radius: 8px;
    background: rgba(0,0,0,.25);
    color: #fff;
    cursor: pointer;
    z-index: 5;
}
.side-arrow svg { width: 28px; height: 28px; }
.side-arrow:hover { background: rgba(0,0,0,.5); }
.side-arrow.left { left: 8px; }
.side-arrow.right { right: 8px; }
.side-arrow[hidden] { display: none; }

.loader {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 14px;
    color: var(--muted);
    background: var(--bg);
    z-index: 6;
    transition: opacity .3s;
}
.loader.done { opacity: 0; pointer-events: none; }
.spinner {
    width: 36px;
    height: 36px;
    border: 3px solid rgba(255,255,255,.15);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Side panel ---------- */
.panel {
    position: fixed;
    top: var(--toolbar-h);
    bottom: 0;
    left: 0;
    width: 340px;
    max-width: 100%;
    display: flex;
    flex-direction: column;
    background: var(--panel);
    border-right: 1px solid var(--bar-line);
    transform: translateX(-100%);
    transition: transform .25s ease;
    z-index: 25;
}
.panel.open { transform: none; box-shadow: 8px 0 30px rgba(0,0,0,.35); }
.panel-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 10px 10px 18px;
    border-bottom: 1px solid var(--bar-line);
}
.panel-head h2 { margin: 0; font-size: 15px; }
.panel-body { flex: 1; overflow-y: auto; padding: 12px; }
.panel-body > * { display: none; }
.panel[data-mode="contents"] .contents-list,
.panel[data-mode="thumbs"] .thumb-grid,
.panel[data-mode="search"] .search-pane { display: block; }
.panel[data-mode="thumbs"] .thumb-grid { display: grid; }

.contents-list a {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    padding: 9px 10px;
    border-radius: 6px;
    color: var(--text);
    text-decoration: none;
    cursor: pointer;
}
.contents-list a:hover { background: rgba(255,255,255,.06); }
.contents-list a.section { font-weight: 600; margin-top: 6px; }
.contents-list a.sub { padding-left: 26px; color: var(--muted); }
.contents-list a span:last-child { color: var(--muted); font-variant-numeric: tabular-nums; }

.thumb-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 14px 10px;
}
.thumb {
    border: 0;
    padding: 0;
    background: none;
    cursor: pointer;
    text-align: center;
    color: var(--muted);
    font-size: 12px;
}
.thumb img {
    display: block;
    width: 100%;
    aspect-ratio: 612 / 792;
    object-fit: cover;
    margin-bottom: 4px;
    border: 2px solid transparent;
    border-radius: 3px;
    background: #3a3f47;
}
.thumb.current img { border-color: var(--accent); }
.thumb:hover img { border-color: rgba(255,255,255,.4); }

.search-pane input {
    width: 100%;
    height: 38px;
    padding: 0 12px;
    border: 1px solid var(--bar-line);
    border-radius: 8px;
    background: #111316;
}
.search-status { color: var(--muted); font-size: 13px; margin: 10px 2px; }
.search-results { list-style: none; margin: 0; padding: 0; }
.search-results li {
    padding: 10px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
}
.search-results li:hover { background: rgba(255,255,255,.06); }
.search-results .pg { display: block; font-weight: 600; margin-bottom: 2px; }
.search-results .snip { color: var(--muted); }
.search-results mark { background: rgba(47,111,214,.45); color: #fff; border-radius: 2px; }

/* ---------- Zoom viewer ---------- */
.zoom {
    position: fixed;
    inset: 0;
    background: #15171b;
    z-index: 40;
    display: none;
}
.zoom.open { display: block; }
.zoom-bar {
    position: absolute;
    top: 12px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 4px 8px;
    background: rgba(28,31,36,.92);
    border: 1px solid var(--bar-line);
    border-radius: 10px;
    z-index: 2;
}
#zoomLevel { min-width: 52px; text-align: center; color: var(--muted); font-variant-numeric: tabular-nums; }
.zoom-scroll {
    position: absolute;
    inset: 0;
    overflow: auto;
    cursor: grab;
    touch-action: pan-x pan-y;
}
.zoom-scroll.dragging { cursor: grabbing; }
.zoom-pages {
    display: flex;
    margin: 70px auto 40px;
    width: max-content;
}
.zoom-pages img {
    display: block;
    height: auto;
    background: #fff;
    user-select: none;
    -webkit-user-drag: none;
}

/* ---------- Toast ---------- */
.toast {
    position: fixed;
    left: 50%;
    bottom: 24px;
    transform: translate(-50%, 20px);
    padding: 10px 16px;
    border-radius: 8px;
    background: #111316;
    border: 1px solid var(--bar-line);
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s, transform .2s;
    z-index: 50;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Small screens ---------- */
@media (max-width: 760px) {
    .tb-title, .hide-sm, #btnFirst, #btnLast { display: none; }
    .toolbar { padding: 0 6px; }
    .tb-btn { width: 34px; height: 34px; }
    .page-box input { width: 44px; }
    .stage { padding: 14px 8px; }
    .side-arrow { display: none; }
    .panel { width: 100%; }
}
