:root {
  --bg: #ebe9e6;
  --fg: #2b2a29;
  --muted: #8a8580;
  --line: rgba(0, 0, 0, .1);
  --panel: #f7f6f4;
  --hover: rgba(0, 0, 0, .06);
  --accent: #5f8f8b;
  --zoom-bg: rgba(30, 29, 28, .96);
  --bar-h: 56px;
  --foot-h: 52px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1a19;
    --fg: #e9e6e2;
    --muted: #8f8a85;
    --line: rgba(255, 255, 255, .1);
    --panel: #262523;
    --hover: rgba(255, 255, 255, .07);
    --accent: #8cbcb7;
    --zoom-bg: rgba(10, 10, 10, .97);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

/* ---------- barras ---------- */
.bar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 max(16px, env(safe-area-inset-left)) 0 max(16px, env(safe-area-inset-right));
  flex: none;
}
.top { height: var(--bar-h); justify-content: space-between; }
.bottom { height: var(--foot-h); justify-content: center; padding-bottom: env(safe-area-inset-bottom); }

.brand { display: flex; align-items: baseline; gap: 12px; min-width: 0; }
.brand-name { font-family: Georgia, "Times New Roman", serif; font-size: 19px; white-space: nowrap; }
.brand-sub { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.tools { display: flex; gap: 4px; align-items: center; }
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  height: 36px; padding: 0 10px;
  border: 0; border-radius: 8px;
  background: transparent; color: var(--fg);
  font: inherit; cursor: pointer;
}
.btn:hover, .btn[aria-expanded="true"] { background: var(--hover); }
.btn svg, .arrow svg {
  width: 20px; height: 20px;
  fill: none; stroke: currentColor; stroke-width: 1.6;
  stroke-linecap: round; stroke-linejoin: round;
}

/* ---------- índice ---------- */
.toc-wrap { position: relative; }
.toc {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 50;
  width: min(380px, calc(100vw - 32px));
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, .15);
  padding: 6px;
}
.toc button {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 12px;
  width: 100%; padding: 10px 12px;
  border: 0; border-radius: 8px;
  background: transparent; color: var(--fg);
  font: inherit; text-align: left; cursor: pointer;
}
.toc button:hover, .toc button.current { background: var(--hover); }
.toc .t { font-weight: 500; }
.toc .s { grid-column: 1; color: var(--muted); font-size: 13px; }
.toc .n { grid-row: 1 / span 2; grid-column: 2; align-self: center; color: var(--muted); font-variant-numeric: tabular-nums; }

/* ---------- livro ---------- */
.stage {
  flex: 1; min-height: 0;
  display: flex; align-items: center;
  padding: 8px 0;
}
.book-area {
  position: relative;
  flex: 1; height: 100%; min-width: 0;
  display: flex; align-items: center; justify-content: center;
}
#book-frame { flex: none; }
#book { width: 100%; height: 100%; }
#book.ready { animation: fadein .5s ease both; }
@keyframes fadein { from { opacity: 0; } }

.page { background: #fff; box-shadow: 0 4px 16px rgba(0, 0, 0, .12); }
.page img {
  display: block; width: 100%; height: 100%;
  object-fit: cover;
  user-select: none; -webkit-user-drag: none;
  opacity: 0; transition: opacity .25s;
}
.page img.loaded { opacity: 1; }


.arrow {
  flex: none;
  width: 64px; height: 120px;
  display: flex; align-items: center; justify-content: center;
  border: 0; background: transparent; color: var(--muted);
  cursor: pointer; border-radius: 12px;
  transition: color .15s, opacity .2s;
}
.arrow svg { width: 32px; height: 32px; stroke-width: 1.4; }
.arrow:hover { color: var(--fg); }
.arrow:disabled { opacity: 0; pointer-events: none; }

.loader { position: absolute; inset: 0; display: grid; place-items: center; pointer-events: none; }
.loader span {
  width: 28px; height: 28px; border-radius: 50%;
  border: 2px solid var(--line); border-top-color: var(--muted);
  animation: spin .8s linear infinite;
}
.loader.done { display: none; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- rodapé ---------- */
.counter {
  min-width: 72px; text-align: right;
  color: var(--muted); font-variant-numeric: tabular-nums;
}
#scrub {
  width: min(360px, 55vw);
  accent-color: var(--accent);
}

/* ---------- zoom ---------- */
.zoom {
  position: fixed; inset: 0; z-index: 100;
  background: var(--zoom-bg);
}
.zoom[hidden] { display: none; }
.zoom-scroll {
  position: absolute; inset: 0;
  overflow: auto;
  overscroll-behavior: contain;
}
.zoom-pages {
  display: flex; justify-content: center;
  min-width: 100%; min-height: 100%;
  width: max-content;
  margin: 0 auto;
  padding: 24px;
  align-items: center;
  cursor: zoom-in;
}
.zoom-pages img { display: block; height: calc(100dvh - 48px); width: auto; background: #fff; }
.zoom.big .zoom-pages { cursor: zoom-out; }
.zoom.big .zoom-pages img { height: calc((100dvh - 48px) * 2.4); }
.zoom-close {
  position: absolute; top: 12px; right: 12px;
  color: #fff; background: rgba(255, 255, 255, .1);
}
.zoom-close:hover { background: rgba(255, 255, 255, .2); }
.zoom-hint {
  position: absolute; left: 50%; bottom: 14px; transform: translateX(-50%);
  color: rgba(255, 255, 255, .7); font-size: 12px;
  background: rgba(0, 0, 0, .4); padding: 6px 12px; border-radius: 999px;
  pointer-events: none; white-space: nowrap;
  animation: fadeout 4s 2s forwards;
}
@keyframes fadeout { to { opacity: 0; } }

/* ---------- telemóvel ---------- */
@media (max-width: 640px) {
  :root { --bar-h: 52px; }
  .brand-sub, .btn .label { display: none; }
  .arrow { display: none; }
  .stage { padding: 4px 12px; }
}
@media (hover: none) {
  .zoom-hint { display: none; }
}
:fullscreen .brand-sub { display: none; }
