:root {
  --bg: #262523;
  --bg-stage: radial-gradient(ellipse at 50% 40%, #3a3835 0%, #262523 70%);
  --bar: rgba(22, 21, 20, .92);
  --panel: #1d1c1b;
  --line: rgba(255, 255, 255, .09);
  --text: #ece7de;
  --muted: #9d968a;
  --accent: #d9a441;
  --hover: rgba(255, 255, 255, .08);
  --bar-h: 52px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100dvh;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; }

/* ---------- bars ---------- */
.topbar, .bottombar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 10px;
  min-height: var(--bar-h);
  background: var(--bar);
  border-bottom: 1px solid var(--line);
  z-index: 5;
}
.topbar { padding-top: env(safe-area-inset-top); }
.bottombar {
  border-top: 1px solid var(--line);
  border-bottom: 0;
  gap: 12px;
  padding-bottom: env(safe-area-inset-bottom);
}
.title {
  flex: 1;
  margin: 0 6px;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: .02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.title small { font-weight: 400; color: var(--muted); margin-left: 10px; }
.actions { display: flex; gap: 2px; }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  cursor: pointer;
  flex: none;
}
.icon-btn:hover, .icon-btn.active { background: var(--hover); }
.icon-btn svg, .nav-arrow svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

#slider {
  flex: 1;
  min-width: 60px;
  accent-color: var(--accent);
  cursor: pointer;
}
.page-jump { display: flex; align-items: center; gap: 6px; color: var(--muted); white-space: nowrap; }
#page-input {
  width: 100px;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: rgba(255, 255, 255, .05);
  color: var(--text);
  text-align: center;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
#page-input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* ---------- stage & book ---------- */
.stage {
  position: relative;
  display: grid;
  place-items: center;
  min-height: 0;
  background: var(--bg-stage);
  overflow: hidden;
}
.book-host { position: relative; }
.book-host::after {          /* soft drop shadow under the book */
  content: "";
  position: absolute;
  inset: 3% 2% -2%;
  z-index: -1;
  box-shadow: 0 18px 50px rgba(0, 0, 0, .55);
}
.book-host.portrait::after { inset: 2% 1% -1%; }

.page { background: #fff; overflow: hidden; }
.page img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: fill;
  user-select: none;
  -webkit-user-drag: none;
  pointer-events: none;
}
.page img:not([src]) { visibility: hidden; }
/* gutter shading towards the spine */
.page.--left::after, .page.--right::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  width: 7%;
  pointer-events: none;
}
.page.--left::after { right: 0; background: linear-gradient(to left, rgba(0, 0, 0, .12), rgba(0, 0, 0, 0)); }
.page.--right::after { left: 0; background: linear-gradient(to right, rgba(0, 0, 0, .12), rgba(0, 0, 0, 0)); }
.book-host.portrait .page::after { display: none; }

.nav-arrow {
  position: absolute;
  top: 50%;
  translate: 0 -50%;
  z-index: 3;
  display: grid;
  place-items: center;
  width: 48px;
  height: 96px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.nav-arrow svg { width: 30px; height: 30px; }
.nav-arrow:hover { color: var(--text); background: var(--hover); }
.nav-arrow:disabled { opacity: .2; pointer-events: none; }
.nav-arrow.prev { left: 6px; }
.nav-arrow.next { right: 6px; }

.loading {
  position: absolute;
  color: var(--muted);
  pointer-events: none;
}
.loading[hidden] { display: none; }

/* ---------- side panel ---------- */
.panel {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  z-index: 20;
  width: min(380px, 92vw);
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--line);
  box-shadow: 10px 0 40px rgba(0, 0, 0, .45);
  translate: -105% 0;
  transition: translate .25s ease;
  padding-top: env(safe-area-inset-top);
}
.panel.open { translate: 0 0; }
.scrim {
  position: fixed;
  inset: 0;
  z-index: 15;
  background: rgba(0, 0, 0, .35);
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s;
}
.scrim.open { opacity: 1; pointer-events: auto; }
.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 8px 8px 12px;
  border-bottom: 1px solid var(--line);
}
.tabs { display: flex; gap: 4px; }
.tabs button {
  border: 0;
  background: transparent;
  padding: 8px 12px;
  border-radius: 7px;
  color: var(--muted);
  cursor: pointer;
}
.tabs button:hover { color: var(--text); }
.tabs button.active { color: var(--text); background: var(--hover); }
.tab { display: none; flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
.tab.active { display: flex; flex-direction: column; }

.toc, .results { list-style: none; margin: 0; padding: 6px 0 24px; }
.toc a, .results a {
  display: flex;
  gap: 12px;
  padding: 9px 18px;
  color: inherit;
  text-decoration: none;
  cursor: pointer;
}
.toc a:hover, .results a:hover { background: var(--hover); }
.toc a { align-items: baseline; font-size: 14px; letter-spacing: .03em; }
.toc .label { flex: 1; }
.toc .num { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 13px; }
.toc a.current { color: var(--accent); }
.toc a.current .num { color: var(--accent); }

.search-form { padding: 14px 14px 6px; }
#search-input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, .05);
  color: var(--text);
  font: inherit;
}
#search-input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.search-info { margin: 4px 18px 0; font-size: 13px; color: var(--muted); }
.search-info:empty { display: none; }
.results a { flex-direction: column; gap: 2px; border-bottom: 1px solid var(--line); }
.results .meta { font-size: 12px; color: var(--muted); }
.results .meta b { color: var(--text); font-weight: 600; }
.results .snip { font-size: 13.5px; color: #cfc8bc; }
.results mark { background: rgba(217, 164, 65, .35); color: #fff; border-radius: 2px; padding: 0 1px; }

.thumbs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px 0;
  padding: 14px;
}
.thumbs a { cursor: pointer; text-align: center; font-size: 12px; color: var(--muted); text-decoration: none; }
.thumbs a:nth-child(even) { padding-right: 1px; }   /* left page of a spread */
.thumbs a:nth-child(odd) { padding-left: 1px; }
.thumbs img {
  display: block;
  width: 100%;
  aspect-ratio: var(--page-ratio, 1.414);
  object-fit: cover;
  background: #3a3835;
  border-radius: 2px;
  margin-bottom: 4px;
  outline: 2px solid transparent;
  outline-offset: 2px;
}
.thumbs a:hover img { outline-color: rgba(255, 255, 255, .3); }
.thumbs a.current { color: var(--accent); }
.thumbs a.current img { outline-color: var(--accent); }

/* ---------- zoom ---------- */
.zoom {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: #141312;
  display: none;
  overflow: hidden;
  touch-action: none;
  cursor: grab;
}
.zoom.open { display: block; }
.zoom.dragging { cursor: grabbing; }
.zoom-stage {
  position: absolute;
  left: 0;
  top: 0;
  display: flex;
  transform-origin: 0 0;
  will-change: transform;
}
.zoom-stage img {
  display: block;
  background: #fff;
  user-select: none;
  -webkit-user-drag: none;
}
.zoom-tools {
  position: absolute;
  right: 12px;
  top: calc(12px + env(safe-area-inset-top));
  display: flex;
  gap: 4px;
  padding: 4px;
  border-radius: 10px;
  background: var(--bar);
}

/* ---------- small screens ---------- */
@media (max-width: 700px) {
  .nav-arrow { display: none; }
  .title small { display: none; }
  #first, #last { display: none; }
  .bottombar { gap: 10px; padding-left: 14px; padding-right: 14px; }
  #page-input { width: 64px; }
}
