/* DECHEM Studio — catalogue viewer shell
   Sections: tokens · base · typography · icon buttons · top bar · stage & book · nav zones ·
   dock & scrubber · idle dimming · loader & error · overlays · toast · keyframes · responsive ·
   user preferences.
   Source order matters: several same-specificity rules (e.g. .chrome after .nav-zone, idle
   after hover) rely on it, and the responsive block must stay after the components. */

/* ========== tokens ========== */
:root {
  /* colour */
  --bg: #111415;
  --bg-2: #181c1d;
  --fg-rgb: 236, 232, 225;              /* rgb triplet, for alpha variants */
  --fg: rgb(var(--fg-rgb));             /* #ece8e1 */
  --muted: #9a9d99;
  --faint: rgba(var(--fg-rgb), .12);
  --hover: rgba(var(--fg-rgb), .08);    /* shared hover wash */
  --warm: 214, 178, 120;                /* link highlight, rgb triplet */
  --focus: rgb(var(--warm));

  /* type */
  --font: "Inter", "Helvetica Neue", Helvetica, "Segoe UI", system-ui, -apple-system, Arial, sans-serif;
  --fs-label: 10.5px;

  /* shape */
  --radius-sm: 3px;
  --radius-md: 8px;
  --radius-pill: 999px;

  /* motion */
  --ease: cubic-bezier(.2, .7, .2, 1);
  --dur-fast: .2s;
  --dur: .3s;
  --dur-slow: .6s;

  /* stacking */
  --z-zone: 5;
  --z-loader: 10;
  --z-chrome: 20;
  --z-overlay: 40;
  --z-toast: 60;
  --z-skip: 100;

  /* layout */
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --bar-h: 60px;
  --dock-h: 84px;
  --side: 88px;                         /* room for the desktop prev/next zones */
  --btn: 40px;
  --bar-total: calc(var(--bar-h) + var(--sat));     /* top bar incl. safe area */
  --dock-total: calc(var(--dock-h) + var(--sab));   /* dock area incl. safe area */
}

/* ========== base ========== */
*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font: 300 14px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}
body {
  background: radial-gradient(120% 90% at 50% 45%, #171b1c 0%, var(--bg) 70%);
}

button { font: inherit; color: inherit; }
/* bare buttons: no UA chrome (zero specificity, so component rules always win) */
:where(.icon-btn, .nav-zone, .overview__spread) {
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
}
/* outline (not box-shadow) so focus stays visible in forced-colors / high-contrast modes */
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.sr-only, .skip:not(:focus) {
  position: absolute !important;
  width: 1px; height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}
.skip {
  position: fixed; z-index: var(--z-skip); top: calc(8px + var(--sat)); left: 8px;
  padding: 10px 14px; border-radius: var(--radius-md);
  background: var(--fg); color: var(--bg); text-decoration: none;
}

/* ========== typography ========== */
.wordmark {
  color: var(--fg);
  text-decoration: none;
  font-weight: 500;
  font-size: 13px;
  letter-spacing: .42em;
  margin-right: -.42em;                 /* compensate trailing tracking */
  white-space: nowrap;
}
.bar__title, .overlay__title, .loader__label, .overview__label {
  font-size: var(--fs-label);
  font-weight: 400;
  letter-spacing: .22em;
  text-transform: uppercase;
  color: var(--muted);
}

/* ========== icon buttons ========== */
.icon-btn {
  position: relative;
  display: inline-grid;
  place-items: center;
  width: var(--btn);
  height: var(--btn);
  border-radius: 50%;
  color: var(--fg);
  text-decoration: none;
  transition: background-color var(--dur-fast), opacity var(--dur-fast), color var(--dur-fast);
}
.icon-btn svg, .nav-zone svg {
  width: 20px; height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.icon-btn svg circle[r=".6"] { fill: currentColor; }
.icon-btn:disabled { opacity: .28; cursor: default; }
#btn-fullscreen .i-exit, .is-fullscreen #btn-fullscreen .i-enter { display: none; }
.is-fullscreen #btn-fullscreen .i-exit { display: block; }

/* tooltips (hover devices + keyboard focus) */
.icon-btn[data-tip]::after {
  content: attr(data-tip);
  position: absolute;
  top: calc(100% + 8px);
  left: 50%;
  translate: -50% -4px;
  padding: 6px 10px;
  border-radius: 6px;
  background: rgba(10, 12, 12, .92);
  color: var(--fg);
  font-size: var(--fs-label);
  letter-spacing: .14em;
  text-transform: uppercase;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity .18s, translate .18s var(--ease);
}
.icon-btn[data-tip]:focus-visible::after { opacity: 1; translate: -50% 0; }
/* the last bar button sits at the screen edge: right-align its tooltip */
.bar__actions .icon-btn:last-child::after { left: auto; right: 0; translate: 0 -4px; }
.bar__actions .icon-btn:last-child:focus-visible::after { translate: 0 0; }

@media (hover: hover) and (pointer: fine) {
  .icon-btn:not(:disabled):hover { background: var(--hover); }
  .icon-btn[data-tip]:hover::after { opacity: 1; translate: -50% 0; transition-delay: .35s; }
  .bar__actions .icon-btn:last-child:hover::after { translate: 0 0; }
}

/* ========== top bar (shared with the overlay headers) ========== */
.bar, .overlay__bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--bar-total);
  padding: var(--sat) calc(16px + var(--sar)) 0 calc(24px + var(--sal));
}
.bar {
  position: fixed;
  z-index: var(--z-chrome);
  inset: 0 0 auto;
  gap: 16px;
}
.bar__brand { display: flex; align-items: baseline; gap: 22px; min-width: 0; }
.bar__title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding-left: 22px;
  border-left: 1px solid var(--faint);
  line-height: 1;
}
.bar__actions { display: flex; align-items: center; gap: 2px; }
.bar__sep { width: 1px; height: 18px; margin: 0 10px; background: var(--faint); }

/* ========== stage + book ========== */
.stage { position: fixed; inset: 0; }
.book-root {
  position: absolute;
  inset: var(--bar-total) calc(var(--side) + var(--sar)) var(--dock-total) calc(var(--side) + var(--sal));
  opacity: 0;
  transition: opacity var(--dur-slow) var(--ease);
}
.is-ready .book-root { opacity: 1; }
.book-root:focus-visible { outline: none; }

/* depth: hairline edge (keeps dark pages distinct from the backdrop) + soft drop shadow */
.book-slot { box-shadow: 0 0 0 1px rgba(var(--fg-rgb), .06), 0 28px 70px -24px rgba(0, 0, 0, .7), 0 6px 18px -8px rgba(0, 0, 0, .4); }
/* while fading in, the outgoing slot's shadow already sits in the same place: don't stack a second one */
.book-slot.is-incoming { box-shadow: none; }
/* hairline fold on two-page spreads */
.book-slot[data-pages*=","]::after {
  content: "";
  position: absolute;
  inset: 0 auto 0 50%;
  z-index: 1;
  width: 1px;
  translate: -50% 0;
  pointer-events: none;
  background: rgba(0, 0, 0, .08);
}

/* loading skeleton: book slots before their canvas lands, zoom pages before render */
.book-slot.is-loading, .page__skeleton {
  background:
    linear-gradient(100deg, transparent 30%, rgba(var(--fg-rgb), .045) 50%, transparent 70%) 0 0 / 250% 100%,
    var(--bg-2);
  animation: shimmer 1.6s linear infinite;
}

/* book slot content (filled by main.js: one canvas per slot + one links layer) */
.slot-canvas {
  position: absolute;
  inset: 0 auto auto 0;
  display: block;
  width: 100%; /* height set from js: exact pixel aspect, overhang clipped by the slot */
}
.slot-canvas.is-new { animation: fade-in .35s var(--ease); }

/* link hotspots (book slots + zoom pages) */
.links { position: absolute; inset: 0; z-index: 2; pointer-events: none; }
.links a {
  position: absolute;
  display: block;
  pointer-events: auto;
  border-radius: var(--radius-sm);
  transition: background-color .25s, box-shadow .25s;
}
.links a:focus-visible { outline-offset: -2px; background: rgba(var(--warm), .14); }
@media (hover: hover) and (pointer: fine) {
  .links a:hover { background: rgba(var(--warm), .16); box-shadow: inset 0 0 0 1px rgba(var(--warm), .35); }
}

/* ========== desktop prev/next zones ========== */
.nav-zone {
  position: absolute;
  z-index: var(--z-zone);
  top: var(--bar-total);
  bottom: var(--dock-total);
  width: var(--side);
  display: grid;
  place-items: center;
  color: var(--fg);
  opacity: 0;
  transition: opacity var(--dur); /* overridden by .chrome below */
}
.nav-zone--prev { left: var(--sal); }
.nav-zone--next { right: var(--sar); }
.nav-zone svg {
  width: 30px; height: 30px; stroke-width: 1;
  transition: translate var(--dur) var(--ease);
}
.nav-zone:disabled { visibility: hidden; }
@media (hover: hover) and (pointer: fine) {
  .stage:hover .nav-zone { opacity: .35; }
  .stage .nav-zone:hover { opacity: .95; }
  .nav-zone--prev:hover svg { translate: -3px 0; }
  .nav-zone--next:hover svg { translate: 3px 0; }
}

/* ========== bottom dock ========== */
.dock {
  position: fixed;
  z-index: var(--z-chrome);
  left: 50%;
  bottom: calc(18px + var(--sab));
  translate: -50% 0;
  pointer-events: none;
}
/* flat, like the top bar: no container, no motion beyond the shared hover background */
.dock__pill {
  pointer-events: auto;
  display: flex;
  align-items: center;
  padding: 4px;
}
.dock__page, .dock__total {
  font-size: 12px;
  font-weight: 400;
  letter-spacing: .08em;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
/* the page label is at least as wide as the widest one it can show (data-sizer, set from
   main.js, e.g. "000–000"), so the track never shifts as the numbers change */
.dock__page { display: inline-flex; flex-direction: column; align-items: flex-end; padding-left: 8px; color: var(--fg); }
.dock__page::after { content: attr(data-sizer); height: 0; overflow: hidden; visibility: hidden; }
.dock__total { min-width: 4ch; padding-right: 8px; color: var(--muted); }

/* scrubber: static visual track under a transparent native range input (which keeps
   pointer, keyboard and screen-reader behaviour) */
.scrubber {
  --thumb: 10px;
  --scrub-h: 32px;
  --f: 0;                               /* progress 0..1, set from main.js */
  --pos: calc(var(--thumb) / 2 + var(--f) * (100% - var(--thumb)));
  position: relative;
  width: 220px;
  height: var(--scrub-h);
  margin: 0 14px;
}
.scrubber__track {
  position: absolute;
  inset: 50% 0 auto;
  height: 1px;
  background: rgba(var(--fg-rgb), .2);
}
.scrubber__fill {
  position: absolute;
  inset: 0 auto 0 0;
  width: var(--pos);
  background: var(--fg);
}
.scrubber__knob {
  position: absolute;
  top: 50%;
  left: var(--pos);
  width: var(--thumb); height: var(--thumb);
  translate: -50% -50%;
  border-radius: 50%;
  background: var(--fg);
}
.scrubber input {
  position: absolute;
  inset: 0;
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  touch-action: none;
}
/* native thumb matches the visual one, so pointer position -> value lines up with --pos */
.scrubber input::-webkit-slider-thumb { -webkit-appearance: none; width: var(--thumb); height: var(--scrub-h); }
.scrubber input::-moz-range-thumb { width: var(--thumb); height: var(--scrub-h); border: 0; }
.scrubber:has(input:focus-visible) .scrubber__knob { box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--focus); }
.scrubber__bubble {
  position: absolute;
  bottom: calc(100% + 14px);
  left: var(--pos);
  translate: -50% 0;
  padding: 6px 11px;
  border-radius: var(--radius-md);
  background: rgba(var(--fg-rgb), .96);
  color: var(--bg);
  font-size: 11.5px;
  font-weight: 500;
  letter-spacing: .08em;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .4);
  pointer-events: none;
  opacity: 0;
  transition: opacity .15s;
}
.scrubber.is-dragging .scrubber__bubble { opacity: 1; }

/* ========== idle dimming (desktop only, set from JS) ========== */
/* must follow .nav-zone: same specificity, these win (nav zones also carry .chrome) */
.chrome { transition: opacity var(--dur-slow) var(--ease); }
.is-idle .chrome { opacity: 0; }
.is-idle .stage .nav-zone { opacity: 0; }
.is-idle, .is-idle .book-root { cursor: none; }

/* controls stay out until the first pages are drawn (the dock sizes itself from its labels),
   then fade in together with the book */
.bar__actions { transition: opacity var(--dur-slow) var(--ease); }
body:not(.is-ready) :is(.dock, .bar__actions, .nav-zone) { opacity: 0; visibility: hidden; pointer-events: none; }

/* ========== loader / error ========== */
.loader, .error {
  position: absolute;
  inset: 0;
  z-index: var(--z-loader);
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 22px;
  text-align: center;
  transition: opacity .7s var(--ease), visibility .7s;
}
.wordmark--loader { font-size: 15px; letter-spacing: .6em; margin-right: -.6em; }
.loader__bar { display: block; width: 180px; height: 1px; background: var(--faint); overflow: hidden; }
.loader__bar i {
  display: block;
  height: 100%;
  width: 100%;
  background: var(--fg);
  transform-origin: left;
  transform: scaleX(var(--p, 0));      /* --p: load progress 0..1, set from main.js */
  transition: transform var(--dur) var(--ease);
}
.loader.is-done { opacity: 0; visibility: hidden; }
.error p { margin: 0; max-width: 32ch; color: var(--muted); font-size: 15px; line-height: 1.6; }
.pill-link {
  display: inline-block;
  padding: 13px 24px;
  border: 1px solid rgba(var(--fg-rgb), .3);
  border-radius: var(--radius-pill);
  color: var(--fg);
  text-decoration: none;
  font-size: 11px;
  letter-spacing: .2em;
  text-transform: uppercase;
  transition: background-color var(--dur-fast), border-color var(--dur-fast);
}
.pill-link:hover { background: var(--hover); border-color: rgba(var(--fg-rgb), .5); }

/* ========== overlays ========== */
.overlay {
  position: fixed;
  z-index: var(--z-overlay);
  inset: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg); /* solid: a full-screen backdrop blur is costly and smears the page behind */
  opacity: 0;
  transition: opacity .28s var(--ease);
}
.overlay.is-open { opacity: 1; }
.overlay__bar { flex: none; }
.overlay__title { margin: 0; }
.overview__scroll, .zoom__scroll {
  flex: 1;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: rgba(var(--fg-rgb), .22) transparent;
}

/* overview grid */
.overview__scroll {
  overflow-y: auto;
  padding: 12px calc(24px + var(--sar)) calc(40px + var(--sab)) calc(24px + var(--sal));
}
.overview__grid {
  list-style: none;
  margin: 0 auto;
  padding: 0;
  max-width: 1400px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(190px, 40vw), 1fr));
  gap: 36px 28px;
}
/* first open: the DECHEM loader shows until the on-screen thumbs are drawn, then the grid fades in at once */
.overview__grid { transition: opacity .5s var(--ease); }
.overview:not(.is-ready) .overview__grid { opacity: 0; }
.overview__loader { pointer-events: none; } /* never blocks the close button */
.overview.is-ready .overview__loader { opacity: 0; visibility: hidden; }
.overview__item { display: grid; justify-items: center; gap: 10px; }
.overview__spread {
  display: flex;
  justify-content: center;
  width: 100%;
  border-radius: var(--radius-sm);
}
/* --cols (pages in the spread) and --aspect (page w/h) are set from main.js */
.overview__thumb {
  position: relative;
  width: calc(50% * var(--cols, 2));
  aspect-ratio: calc(var(--aspect, .7071) * var(--cols, 2));
  border-radius: 2px;
  background: var(--bg-2);
}
/* offset ring as its own layer: only its opacity animates (composited, no per-frame repaint),
   so hover fades cleanly instead of stepping like an animated outline-offset */
.overview__thumb::after {
  content: '';
  position: absolute;
  inset: -6px;
  border: 1.5px solid rgba(var(--fg-rgb), .35);
  border-radius: 5px;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--dur) var(--ease);
}
.overview__thumb canvas {
  position: absolute; inset: 0;
  display: block;
  width: 100%; height: 100%;
  border-radius: inherit;
  animation: fade-in .35s var(--ease); /* same fade as the page swap (.slot-canvas.is-new); runs on insertion */
}
.overview__label { letter-spacing: .18em; font-variant-numeric: tabular-nums; transition: color var(--dur-fast); }
.overview__item.is-current .overview__thumb { box-shadow: 0 0 28px rgba(var(--warm), .14), 0 12px 32px rgba(0, 0, 0, .45); }
.overview__item.is-current .overview__thumb::after { border: 2px solid rgb(var(--warm)); opacity: 1; }
.overview__item.is-current .overview__label { color: rgb(var(--warm)); }
.overview__spread:focus-visible { outline-offset: 9px; } /* clears the 6px ring */
@media (hover: hover) and (pointer: fine) {
  /* ring only: nothing moves, so the hover target never slides out from under the cursor */
  .overview__spread:hover .overview__thumb::after { opacity: 1; }
  .overview__spread:hover + .overview__label { color: var(--fg); }
}

/* zoom viewer */
.zoom__scroll {
  overflow: auto;
  touch-action: pan-x pan-y pinch-zoom;
  cursor: grab;
  padding: 0 0 var(--sab);
}
.zoom__scroll.is-panning { cursor: grabbing; }
.zoom__scroll.is-panning .links a { pointer-events: none; }
.zoom__pages {
  display: flex;
  width: max-content;
  min-width: 100%;
  min-height: 100%;
  margin: 0 auto;
  padding: 24px;
  justify-content: center;
  align-items: center;
}
/* zoom pages (--w: rendered css width, set from main.js) */
.page {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg-2);
}
.zoom__pages .page { flex: none; width: var(--w); height: auto; aspect-ratio: var(--aspect); box-shadow: 0 20px 60px rgba(0, 0, 0, .45); }
.page__canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
}
.page__skeleton {
  position: absolute;
  inset: 0;
  transition: opacity .35s;
}
.page.is-rendered .page__skeleton { opacity: 0; animation: none; }

/* ========== toast ========== */
.toast {
  position: fixed;
  z-index: var(--z-toast);
  left: 50%;
  bottom: calc(var(--dock-total) + 16px);
  translate: -50% 8px;
  padding: 10px 18px;
  border-radius: var(--radius-pill);
  background: rgba(var(--fg-rgb), .94);
  color: var(--bg);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: .16em;
  text-transform: uppercase;
  pointer-events: none;
  opacity: 0;
  transition: opacity .25s, translate var(--dur) var(--ease);
}
.toast.is-visible { opacity: 1; translate: -50% 0; }

/* ========== keyframes ========== */
@keyframes shimmer { from { background-position: 125% 0, 0 0; } to { background-position: -125% 0, 0 0; } }
@keyframes fade-in { from { opacity: 0; } }

/* ========== responsive: touch / small screens ========== */
@media (hover: none), (pointer: coarse) {
  :root { --btn: 44px; --side: 8px; }
}
/* no prev/next zones on touch or narrow screens (swipe + dock instead) */
@media (hover: none), (pointer: coarse), (max-width: 720px) {
  .nav-zone { display: none; }
}
@media (max-width: 720px) {
  :root { --bar-h: 52px; --dock-h: 76px; --side: 6px; --btn: 44px; }
  .bar { padding-left: calc(18px + var(--sal)); padding-right: calc(6px + var(--sar)); }
  .bar__brand { gap: 14px; }
  .bar__title { padding-left: 14px; font-size: 9.5px; letter-spacing: .18em; }
  .wordmark { font-size: 12px; letter-spacing: .34em; margin-right: -.34em; }
  .bar__actions { gap: 0; }
  .bar__sep { margin: 0 4px; }
  #btn-fullscreen { display: none; }
  .dock { bottom: calc(10px + var(--sab)); }
  .scrubber { width: 140px; margin: 0 10px; }
}
@media (max-width: 420px) {
  .bar__title { display: none; }
  .scrubber { width: 96px; margin: 0 8px; }
  .dock__page { padding-left: 2px; }
  .dock__total { min-width: 3.5ch; padding-right: 2px; }
}
@media (max-height: 520px) and (orientation: landscape) {
  :root { --bar-h: 44px; --dock-h: 62px; }
  .dock { bottom: calc(6px + var(--sab)); }
}

/* ========== user preferences ========== */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
  .page__skeleton, .book-slot.is-loading { animation: none; }
}

/* stronger contrast on request */
@media (prefers-contrast: more) {
  :root { --muted: #c9c6bf; --faint: rgba(var(--fg-rgb), .3); }
  .scrubber__track { background: rgba(var(--fg-rgb), .45); }
  .stage:hover .nav-zone { opacity: .7; }
}
/* Windows high contrast & co.: shadows/backgrounds vanish, so draw real borders */
@media (forced-colors: active) {
  .toast, .icon-btn[data-tip]::after, .scrubber__bubble { border: 1px solid CanvasText; }
  .book-slot, .scrubber__track { outline: 1px solid CanvasText; }
  .scrubber__fill, .scrubber__knob { forced-color-adjust: none; background: CanvasText; }
  .scrubber:has(input:focus-visible) .scrubber__knob { background: Highlight; }
  .overview__item.is-current .overview__thumb { outline: 2px solid Highlight; outline-offset: 2px; }
}
