/* The widget carries its own box model: a host without a global border-box
   reset would otherwise add the padding outside every width:100% box. */
pdf-viewer,
pdf-viewer *,
pdf-viewer *::before,
pdf-viewer *::after {
  box-sizing: border-box;
}

:where(pdf-viewer) {
  --pv-bg: #171717;
  --pv-stage-bg: #111110;
  --pv-page-bg: #f4f4ef;
  --pv-ink: #f4f4ef;
  --pv-ink-dim: rgba(244, 244, 239, .72);
  --pv-accent: #faff69;
  --pv-accent-hi: #f4f4ef;
  --pv-focus: #faff69;
  --pv-line: rgba(244, 244, 239, .16);
  --pv-line-soft: rgba(244, 244, 239, .08);
  --pv-font-ui: "JetBrains Mono", ui-monospace, monospace;
  --pv-stage-h: min(calc(100dvh - 170px), 820px);
  --pv-stage-h-mobile: calc(100dvh - 200px);
  --pv-stage-min-h: 480px;
  --pv-stage-min-h-mobile: 400px;
  /* The cap the stage obeys, and the floor it keeps only while the cap has
     room for it. A viewport shorter than the floor would otherwise hold the
     stage taller than the screen, and in the expanded overlay nothing can
     scroll a control row that has been pushed past the fold. */
  --pv-stage-cap: max(var(--pv-stage-h), var(--pv-stage-min-h));
  --pv-stage-pad: 16px;
  --pv-control: 44px;
  --pv-controls-h: 66px;
  --pv-shadow: 0 20px 60px -30px rgba(0, 0, 0, .8);
  display: block;
  /* the stage and the controls are built by script; reserving their height on
     the host keeps a cold load from moving the page once they exist */
  min-height: calc(var(--pv-stage-h) + var(--pv-controls-h));
  background: var(--pv-bg);
  border: 1px solid var(--pv-line);
  box-shadow: var(--pv-shadow);
  color: var(--pv-ink);
  font-family: var(--pv-font-ui);
}

pdf-viewer:not(:defined) .pdf-viewer__fallback {
  display: block;
  padding: 18px;
  color: var(--pv-accent);
}

pdf-viewer:defined .pdf-viewer__fallback {
  display: none;
}

pdf-viewer .pv-spread {
  display: flex;
  flex: none;
  align-items: center;
  justify-content: center;
  gap: clamp(6px, 0.8vw, 16px);
  max-width: 100%;
}

pdf-viewer .pv-spread .pv-page {
  min-width: 0;
}

/* The reader is width-bound: the stage takes the column's width and its height
   is the page's own aspect ratio, capped at --pv-stage-h. When the cap is
   shorter than the page the stage scrolls, so the page keeps the column's width
   instead of being shrunk to a letterboxed thumbnail. */
pdf-viewer .pv-stage {
  position: relative;
  width: 100%;
  max-width: 100%;
  /* the cap never outruns what the viewport leaves once the bars, the header
     and the control row are paid for, or the controls sit below the fold */
  max-height: min(var(--pv-stage-cap), calc(100dvh - 200px));
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
  min-height: min(var(--pv-stage-min-h), var(--pv-stage-cap), calc(100dvh - 200px));
  padding: var(--pv-stage-pad);
  /* the "click to open" chip's band, so the page never sits under it */
  padding-top: calc(var(--pv-stage-pad) + 18px);
  overflow-x: hidden;
  overflow-y: auto;
  background: var(--pv-stage-bg);
  touch-action: pan-y;
  cursor: pointer;
}

pdf-viewer .pv-page {
  display: block;
  aspect-ratio: var(--pv-aspect, 1240 / 1754);
  width: auto;
  height: auto;
  max-width: 100%;
  object-fit: contain;
  background: var(--pv-page-bg);
  box-shadow: 0 10px 30px -12px rgba(0, 0, 0, .7);
}

/* `hidden` loses to the display rule above at equal specificity, and a hidden
   facing page still takes the spread's flex gap. */
pdf-viewer .pv-page[hidden] {
  display: none;
}

pdf-viewer .pv-controls {
  position: relative;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-top: 1px solid var(--pv-line-soft);
}

pdf-viewer .pv-right {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  /* the counter is out of flow on a wide screen, so auto-placement would drop
     this cluster into the flexible middle column and leave a trailing gap
     between it and the controls' content edge */
  grid-column: 3;
}

pdf-viewer button {
  -webkit-appearance: none;
  appearance: none;
  min-width: var(--pv-control);
  min-height: var(--pv-control);
  padding: 6px 14px;
  background: transparent;
  border: 1px solid var(--pv-line);
  color: var(--pv-ink);
  font-family: var(--pv-font-ui);
  font-size:1.375rem;
  line-height: 1;
  cursor: pointer;
}

pdf-viewer button:hover:enabled {
  border-color: var(--pv-accent);
  color: var(--pv-accent-hi);
}

pdf-viewer button:disabled {
  opacity: .32;
  cursor: default;
}

pdf-viewer .pv-prev {
  justify-self: start;
}

pdf-viewer .pv-counter {
  position: absolute;
  left: 50%;
  top: 50%;
  margin: 0;
  transform: translate(-50%, -50%);
  color: var(--pv-ink-dim);
  font-size:0.6562rem;
  letter-spacing: .18em;
  line-height: 1.2;
  text-align: center;
  text-transform: uppercase;
  white-space: nowrap;
  pointer-events: none;
}

pdf-viewer .pv-pdf {
  display: inline-flex;
  align-items: center;
  min-width: var(--pv-control);
  min-height: var(--pv-control);
  padding: 0 8px;
  justify-content: center;
  border-bottom: 1px solid var(--pv-line-soft);
  color: var(--pv-ink-dim);
  font-size:0.625rem;
  letter-spacing: .16em;
  line-height: 1.1;
  text-transform: uppercase;
  text-decoration: none;
  white-space: nowrap;
}

pdf-viewer .pv-pdf:hover {
  color: var(--pv-accent-hi);
  border-bottom-color: var(--pv-accent);
}

pdf-viewer :focus-visible {
  outline: 2px solid var(--pv-focus);
  outline-offset: 3px;
}

@media (max-width: 700px) {
  :where(pdf-viewer) {
    min-height: calc(var(--pv-stage-h-mobile) + var(--pv-controls-h));
    --pv-stage-cap: max(var(--pv-stage-h-mobile), var(--pv-stage-min-h-mobile));
  }

  pdf-viewer .pv-stage {
    min-height: min(var(--pv-stage-min-h-mobile), var(--pv-stage-cap), calc(100dvh - 200px));
  }

  pdf-viewer .pv-controls,
  pdf-viewer .pv-right {
    gap: 6px;
  }

  pdf-viewer .pv-pdf {
    padding: 0 6px;
    font-size:0.5625rem;
    letter-spacing: .1em;
  }

  pdf-viewer .pv-counter {
    position: static;
    transform: none;
    justify-self: center;
    max-width: 100%;
    white-space: normal;
    line-height: 1.45;
  }
}

@media print {
  pdf-viewer {
    display: none;
  }
}

/* A pinch scales the page under the fingers; only then does the stage take the
   gesture away from the browser, so one-finger scrolling and swiping survive. */
pdf-viewer .pv-stage.is-pinching {
  overflow: hidden;
  touch-action: none;
}

pdf-viewer .pv-stage.is-pinching .pv-page {
  transition: none;
  will-change: transform;
}

/* On a wide screen a click takes the reader to the viewport; the spread is
   decided again at that width, so a 1400px window shows two pages and a
   narrow one still shows a single page. */
pdf-viewer.is-expanded {
  position: fixed;
  inset: 0;
  z-index: 90;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: clamp(8px, 1.4vh, 18px);
  border: 0;
  background: var(--pv-stage-bg);
}

pdf-viewer.is-expanded .pv-stage {
  height: auto;
  /* the overlay's own cap: whatever the viewport leaves under the control row,
     minus the fixed trustbar it would otherwise sit behind */
  --pv-stage-cap: calc(100dvh - 100px - 2.0625rem);
  min-height: 0;
  flex: 1 1 auto;
  cursor: zoom-out;
}

pdf-viewer.is-expanded .pv-page {
  cursor: zoom-out;
}

html.pv-locked {
  overflow: hidden;
}

/* Nothing told a desktop reader that the page opens. The stage says it once, in
   mono, and gets out of the way. The chip keeps it legible when the page, not
   the stage's own field, is what sits under the corner: the stage's own top
   padding is set aside for the chip in the main stage block above. */
pdf-viewer .pv-stage::after {
  content: "click or press enter to open";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1;
  padding: 4px 6px;
  text-align: center;
  background: var(--pv-stage-bg);
  font-family: var(--pv-font-ui);
  font-size:0.5312rem;
  letter-spacing: .16em;
  text-transform: uppercase;
  color: var(--pv-ink-dim);
  opacity: .85;
  pointer-events: none;
}

@media (max-width: 700px) {
  pdf-viewer .pv-stage::after { display: none; }
}

pdf-viewer.is-expanded .pv-stage::after { content: "esc to close"; }
