/* Reference letter viewer (assets/js/reference-viewer.js).
   A full-screen <dialog> that shows a letter as page images, in the same
   floating chrome as the canvas rail: white panel, 14px radius, 44px tools.
   The letters are never linked as files; see scripts/reference-thumbs.mjs. */

.refViewer {
  --rv-panel: #fff;
  --rv-ink: #050038;
  --rv-ink-soft: #656479;
  --rv-line: #e6e6ec;
  --rv-hover: #f2f3f7;
  --rv-focus: #4262ff;
  --rv-scrim: rgba(5, 0, 56, 0.8);
  --rv-zoom: 1;
  position: fixed;
  inset: 0;
  box-sizing: border-box;
  width: 100vw;
  max-width: none;
  height: 100vh;
  height: 100dvh;
  max-height: none;
  margin: 0;
  padding: 0;
  border: 0;
  overflow: hidden;
  background: var(--rv-scrim);
  color: var(--rv-ink);
  font-family: var(--font-text, "Helvetica Neue", Helvetica, Arial, sans-serif);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}

.refViewer::backdrop {
  background: transparent;
}

.refViewer[open] {
  animation: refViewerIn 200ms cubic-bezier(0.22, 1, 0.36, 1);
}

@keyframes refViewerIn {
  from { opacity: 0; }
}

/* The page behind must not scroll under the viewer (wheel over the bar chains up) */
html:has(.refViewer[open]) {
  overflow: hidden;
}

/* ---- Bar ---------------------------------------------------------------- */

.refViewer__bar {
  position: absolute;
  z-index: 1;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 4px;
  box-sizing: border-box;
  width: max-content;
  max-width: calc(100% - 32px);
  padding: 6px 6px 6px 16px;
  background: var(--rv-panel);
  border: 1px solid var(--rv-line);
  border-radius: 14px;
  box-shadow: 0 6px 24px rgba(5, 0, 56, 0.24), 0 1px 3px rgba(5, 0, 56, 0.16);
}

/* Wide enough for the longest organisation: the bar keeps its size while the
   name changes from letter to letter in the all-letters view. */
.refViewer__heading {
  min-width: 10.5em;
  margin: 0 12px 0 0;
  padding: 0;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: var(--weight-label, 600);
  line-height: 1.25;
  letter-spacing: 0;
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.refViewer__kicker {
  display: block;
  font-size: 0.6875rem;
  font-weight: var(--weight-ui, 500);
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--rv-ink-soft);
}

.refViewer__count {
  flex: none;
  margin: 0;
  padding: 0 12px;
  border-left: 1px solid var(--rv-line);
  font-size: 0.875rem;
  font-weight: var(--weight-ui, 500);
  font-variant-numeric: tabular-nums;
  line-height: 32px;
  white-space: nowrap;
  color: var(--rv-ink-soft);
}

.refViewer__tools {
  flex: none;
  display: flex;
  align-items: center;
  gap: 2px;
  padding-left: 4px;
  border-left: 1px solid var(--rv-line);
}

.refViewer__tool {
  -webkit-appearance: none;
  appearance: none;
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--rv-ink);
  cursor: pointer;
  transition: background-color 120ms ease;
}

.refViewer__tool svg {
  display: block;
  width: 21px;
  height: 21px;
}

.refViewer__tool:hover {
  background: var(--rv-hover);
}

.refViewer__tool:focus-visible {
  outline: 2px solid var(--rv-focus);
  outline-offset: -2px;
}

.refViewer__tool:disabled {
  color: color-mix(in srgb, var(--rv-ink) 30%, transparent);
  background: transparent;
  cursor: default;
}

.refViewer__sep {
  flex: none;
  width: 1px;
  height: 24px;
  margin: 0 4px;
  background: var(--rv-line);
}

/* ---- Pages -------------------------------------------------------------- */

.refViewer__scroll {
  position: absolute;
  inset: 0;
  box-sizing: border-box;
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 92px 24px 56px;
  outline: none;
}

.refViewer__scroll:focus-visible {
  box-shadow: inset 0 0 0 2px var(--rv-focus);
}

/* Block layout, not flex: once a zoomed sheet is wider than the screen its
   auto margins collapse and the left edge stays reachable by scrolling. */
.refViewer__pages {
  display: block;
  box-sizing: border-box;
  width: calc(min(920px, 100%) * var(--rv-zoom));
  margin: 0 auto;
  padding: 0;
  list-style: none;
}

.refViewer__page {
  margin: 0 0 24px;
  padding: 0;
  font-size: 0;
  line-height: 0;
  background: #fff;
  border-radius: 3px;
  box-shadow: 0 0 0 1px rgba(5, 0, 56, 0.2), 0 12px 40px rgba(5, 0, 56, 0.4);
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.refViewer__page:last-child {
  margin-bottom: 0;
}

/* A4 until the image arrives, then its own ratio (two letters are US Letter).
   No pointer events: a press or right-click lands on the sheet, not the image. */
.refViewer__page img {
  display: block;
  width: 100%;
  max-width: none;
  height: auto;
  aspect-ratio: auto 210 / 297;
  border-radius: 3px;
  pointer-events: none;
  -webkit-user-drag: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

/* ---- Small screens ------------------------------------------------------ */

@media (max-width: 640px) {
  .refViewer__bar {
    top: 8px;
    width: calc(100% - 16px);
    max-width: none;
    padding-left: 12px;
  }

  .refViewer__heading {
    flex: 1 1 auto;
    min-width: 0;
    margin-right: 8px;
  }

  .refViewer__count {
    padding: 0 8px;
  }

  .refViewer__scroll {
    padding: 80px 8px 32px;
  }

  .refViewer__page {
    margin-bottom: 12px;
  }
}

/* Pinch does the zooming on a phone; the two buttons would squeeze the title */
@media (max-width: 480px) {
  .refViewer__zoom,
  .refViewer__sep {
    display: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .refViewer[open] {
    animation: none;
  }

  .refViewer__tool {
    transition: none;
  }
}

@media (forced-colors: active) {
  .refViewer__bar,
  .refViewer__page {
    border: 1px solid CanvasText;
  }
}

/* The letters are for reading on screen */
@media print {
  .refViewer {
    display: none !important;
  }
}
