/*
 * The paper viewer: the pages of a PDF the app drew, on a quiet desk (templates/core/paper_viewer.html).
 * The post previews its letters with it, and every PDF download shows it first in the preview
 * dialog (bds/core/download_preview.py). Paper stays white in every theme, as it will in the
 * envelope. The pager, zoom and thumbnails come from paper.js; without it the pages stand
 * one below the other and the page scrolls. Motion: the sheets rise onto the desk one after
 * the other, the skeleton breathes while the server draws the pages.
 */

.paper-tool {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; min-width: 40px; padding: 0 12px;
  font: inherit; font-size: 14px; font-weight: var(--btn-weight);
  color: var(--btn-ink); background: var(--surface);
  border: 1px solid var(--btn-border); border-radius: 999px; cursor: pointer;
  transition: background-color .2s var(--ease-out), border-color .2s var(--ease-out), transform .2s var(--ease-pop), opacity .2s;
}
.paper-tool .icon { width: 18px; height: 18px; }
/* The pressed choice keeps its colour under the pointer; the hover tint would turn its
   light text unreadable. */
.paper-tool:hover:not(:disabled):not([aria-pressed="true"]) { background: var(--btn-hover-bg); }
.paper-tool:active:not(:disabled) { transform: scale(.94); }
.paper-tool:disabled { opacity: .38; cursor: default; }
.paper-tool:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.paper-tool[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }


.paper-viewer {
  --desk: color-mix(in srgb, var(--surface-2) 82%, var(--page));
  --sheet-shadow: 0 1px 1px rgba(20, 24, 22, .08), 0 2px 6px rgba(20, 24, 22, .06), 0 22px 44px -18px rgba(20, 24, 22, .34);
  --sheet-width: min(100%, 720px);
  position: relative; border-radius: var(--r-lg); overflow: hidden;
  background:
    radial-gradient(120% 80% at 50% -10%, color-mix(in srgb, var(--surface) 70%, transparent), transparent 60%),
    var(--desk);
  border: 1px solid var(--line);
}
[data-theme="dark"] .paper-viewer { --sheet-shadow: 0 1px 2px rgba(0, 0, 0, .5), 0 26px 50px -16px rgba(0, 0, 0, .8); }
.paper-viewer.is-width { --sheet-width: 100%; }

.paper-problem { margin: 16px 16px 0; }

.paper-toolbar {
  position: sticky; top: 0; z-index: 2;
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 16px;
  padding: 10px 14px;
  background: color-mix(in srgb, var(--surface) 86%, transparent);
  backdrop-filter: blur(10px) saturate(1.2); -webkit-backdrop-filter: blur(10px) saturate(1.2);
  border-bottom: 1px solid var(--line);
}
.paper-pager, .paper-zoom { display: flex; align-items: center; gap: 6px; }
.paper-count { margin: 0; min-width: 10ch; text-align: center; font-size: 14px; font-weight: 700; color: var(--ink); font-variant-numeric: tabular-nums; }
.paper-open { margin-left: auto; }
/* The download is what the preview leads to: in the accent colour, a link without the underline of a link. */
a.paper-tool { text-decoration: none; }
.paper-download { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
/* As specific as the hover of every tool above, so the download keeps its colour under the pointer. */
.paper-tool.paper-download:hover:not(:disabled) { background: var(--accent-hover); }

/* The preview of a list or an XML file (templates/core/file_viewer.html). */
.file-summary { margin: 0; font-size: 14px; font-weight: 700; color: var(--ink); font-variant-numeric: tabular-nums; }
.file-stage { display: flex; flex-direction: column; gap: 28px; padding: clamp(16px, 3vw, 28px) clamp(12px, 3vw, 28px) 28px; }
.file-part { display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.file-part > .paper-note { margin: 0; }
/* The table scrolls both ways inside the window, so its head stays in view over the rows. */
.file-table { max-height: calc(100dvh - 300px); overflow: auto; overscroll-behavior: contain; box-shadow: var(--sheet-shadow); }
.file-table:focus-visible, .file-text:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.file-table .data-table { width: max-content; min-width: 100%; font-size: 14px; }
.file-table .data-table th { position: sticky; top: 0; z-index: 1; }
.file-table .data-table td { padding: 9px 12px; white-space: nowrap; }
.file-table .data-table td:not(.num) { overflow-wrap: normal; max-width: 28em; overflow: hidden; text-overflow: ellipsis; }
.file-text {
  margin: 0; max-height: calc(100dvh - 300px); overflow: auto; overscroll-behavior: contain;
  padding: 16px 18px; border: 1px solid var(--line); border-radius: var(--r); background: var(--surface);
  box-shadow: var(--sheet-shadow); color: var(--ink); font-size: 13px; line-height: 1.55; tab-size: 2;
}

.paper-body { display: grid; grid-template-columns: minmax(0, 1fr); }

.paper-stage {
  max-height: min(78vh, 1100px); overflow: auto; overscroll-behavior: contain;
  padding: clamp(16px, 3vw, 36px) clamp(12px, 3vw, 40px) 40px;
  scroll-behavior: smooth; scroll-padding-top: 16px;
  display: flex; flex-direction: column; align-items: center; gap: 28px;
}
.paper-stage:focus-visible { outline: 3px solid var(--focus); outline-offset: -3px; }

.paper-document { width: var(--sheet-width); display: flex; flex-direction: column; gap: 18px; transition: width .38s var(--ease-out); }
.paper-document-title { margin: 0; font-size: 13px; font-weight: 800; letter-spacing: .06em; text-transform: uppercase; color: var(--ink); }
.paper-document-title .muted { color: inherit; opacity: .7; font-weight: 700; letter-spacing: .02em; text-transform: none; }

.paper-sheet {
  margin: 0; position: relative; aspect-ratio: 210 / 297;
  background: #FFFFFF; border-radius: 2px; box-shadow: var(--sheet-shadow);
  transform-origin: 50% 0;
}
.paper-sheet img { display: block; width: 100%; height: 100%; border-radius: 2px; }
.paper-sheet::after {
  /* A whisper of light across the paper, so the sheet reads as paper and not as a box. */
  content: ""; position: absolute; inset: 0; border-radius: 2px; pointer-events: none;
  background: linear-gradient(135deg, rgba(255, 255, 255, .0) 40%, rgba(255, 255, 255, .22) 50%, rgba(255, 255, 255, 0) 60%);
  background-size: 260% 260%; background-position: 100% 100%;
}
.paper-sheet.is-current { box-shadow: var(--sheet-shadow), 0 0 0 2px color-mix(in srgb, var(--focus) 55%, transparent); }

/* The e-mail as it arrives, above the files it carries. */
.mail-card {
  width: var(--sheet-width); background: var(--surface); color: var(--ink);
  border: 1px solid var(--line); border-radius: var(--r-lg); box-shadow: var(--sheet-shadow);
  padding: 20px 24px 22px; transition: width .38s var(--ease-out);
}
.mail-head { display: flex; align-items: center; gap: 12px; padding-bottom: 14px; border-bottom: 1px solid var(--line); }
.mail-avatar { display: grid; place-items: center; width: 40px; height: 40px; flex: none; border-radius: 50%; background: var(--accent-soft); color: var(--accent-text); }
.mail-avatar .icon { width: 20px; height: 20px; }
.mail-meta { min-width: 0; }
.mail-from { margin: 0; font-weight: 800; overflow-wrap: anywhere; }
.mail-to { margin: 2px 0 0; font-size: 14px; color: var(--muted); overflow-wrap: anywhere; }
.mail-subject { margin: 16px 0 12px; font-size: 18px; font-weight: var(--h-weight); overflow-wrap: anywhere; }
.mail-text p { margin: 0 0 12px; overflow-wrap: anywhere; }
.mail-note { margin: 16px 0 0; font-size: 14px; font-weight: 700; color: var(--ink); }
.mail-attachments { list-style: none; margin: 16px 0 0; padding: 14px 0 0; border-top: 1px dashed var(--line-strong); display: flex; flex-wrap: wrap; gap: 8px; }
.mail-attachment { display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; border-radius: 999px; background: var(--surface-2); color: var(--ink); font-size: 14px; font-weight: 700; min-width: 0; overflow-wrap: anywhere; }
.mail-attachment .icon { width: 16px; height: 16px; flex: none; }

/* Thumbnails: a rail beside the pages on wide screens, hidden where there is no room. */
.paper-thumbs { display: none; }
@media (min-width: 1100px) {
  .paper-body:has(.paper-thumbs) { grid-template-columns: 112px minmax(0, 1fr); }
  .paper-thumbs {
    display: flex; flex-direction: column; gap: 12px; padding: 20px 0 20px 16px;
    max-height: min(78vh, 1100px); overflow: auto;
  }
}
.paper-thumb {
  position: relative; display: block; width: 84px; padding: 0; border: 0; background: none; cursor: pointer;
  border-radius: 3px; transition: transform .25s var(--ease-pop);
}
.paper-thumb img { display: block; width: 100%; height: auto; border-radius: 2px; box-shadow: 0 1px 3px rgba(20, 24, 22, .25); opacity: .72; transition: opacity .25s var(--ease-out), box-shadow .25s var(--ease-out); }
.paper-thumb:hover img, .paper-thumb[aria-current="true"] img { opacity: 1; }
.paper-thumb[aria-current="true"] img { box-shadow: 0 0 0 3px var(--focus), 0 6px 14px -4px rgba(20, 24, 22, .4); }
.paper-thumb:focus-visible { outline: 3px solid var(--focus); outline-offset: 3px; }
.paper-thumb-number { position: absolute; right: 4px; bottom: 4px; min-width: 20px; padding: 1px 5px; border-radius: 999px; background: var(--accent); color: var(--accent-ink); font-size: 12px; font-weight: 800; text-align: center; }

/* Skeleton while the server sets the letter on the letterhead. */
.paper-skeleton { display: flex; flex-direction: column; align-items: center; gap: 14px; padding: 32px 16px; border-radius: var(--r-lg); background: color-mix(in srgb, var(--surface-2) 82%, var(--page)); border: 1px solid var(--line); }
.skeleton-sheet {
  position: relative; width: min(100%, 420px); aspect-ratio: 210 / 297; padding: 14% 12% 10%;
  background: #FFFFFF; border-radius: 2px; box-shadow: 0 1px 2px rgba(20, 24, 22, .08), 0 22px 44px -18px rgba(20, 24, 22, .3);
  display: flex; flex-direction: column; gap: 10px; overflow: hidden;
}
.skeleton-logo { position: absolute; top: 5%; right: 11%; width: 18%; aspect-ratio: 1.57; border-radius: 6px; background: #E4EBA8; }
.skeleton-line { display: block; height: 9px; border-radius: 5px; background: #E7EAE6; }
.skeleton-line-short { width: 38%; }
.skeleton-line-mid { width: 72%; }
.skeleton-line-strong { width: 46%; height: 11px; background: #DADFD9; }
.skeleton-gap { height: 18%; }
.skeleton-caption { margin: 0; font-size: 14px; font-weight: 700; color: var(--ink); }

/* The preview window of single and edited letters. */
.paper-dialog {
  width: min(100vw - 24px, 1040px); max-width: none; max-height: calc(100dvh - 24px);
  padding: 0; border: 0; border-radius: var(--r-lg); background: var(--surface); color: var(--ink);
  box-shadow: var(--shadow-pop); overflow: hidden;
}
.paper-dialog::backdrop { background: rgba(14, 18, 17, .55); backdrop-filter: blur(3px); }
.paper-dialog-frame { display: flex; flex-direction: column; max-height: calc(100dvh - 24px); }
.paper-dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 16px 12px 22px; border-bottom: 1px solid var(--line); }
.paper-dialog-title { margin: 0; font-size: 20px; font-weight: var(--h-weight); color: var(--heading); }
.paper-dialog-body { overflow: auto; padding: 12px; }
.paper-dialog .paper-stage { max-height: calc(100dvh - 210px); }

@media (prefers-reduced-motion: no-preference) {
  .paper-sheet, .mail-card { animation: paper-rise .7s var(--ease-out) both; }
  .paper-sheet::after { animation: paper-sheen 1.6s .35s var(--ease-out) both; }
  .paper-document:nth-of-type(2) .paper-sheet { animation-delay: .08s; }
  .paper-sheet:nth-of-type(2) { animation-delay: .12s; }
  .paper-sheet:nth-of-type(3) { animation-delay: .2s; }
  .paper-sheet:nth-of-type(4) { animation-delay: .28s; }
  .paper-sheet:nth-of-type(n+5) { animation-delay: .34s; }
  .paper-toolbar { animation: paper-fade .5s var(--ease-out) both; }
  .paper-thumb { animation: paper-fade .5s .2s var(--ease-out) both; }
  .skeleton-sheet { animation: skeleton-breathe 1.8s var(--ease-in-out) infinite; }
  .skeleton-line, .skeleton-logo {
    background-image: linear-gradient(100deg, transparent 20%, rgba(255, 255, 255, .7) 50%, transparent 80%);
    background-size: 220% 100%; background-repeat: no-repeat;
    animation: skeleton-shimmer 1.4s linear infinite;
  }
  .paper-dialog[open] { animation: dialog-in .42s var(--ease-pop) both; }
  .paper-dialog[open]::backdrop { animation: paper-fade .3s var(--ease-out) both; }
}

@media (hover: hover) and (prefers-reduced-motion: no-preference) {
  .paper-thumb:hover { transform: translateY(-2px); }
}
@media (pointer: coarse) {
  .paper-tool { min-height: 44px; min-width: 44px; }
}

/* The note of a long file that shows its first pages only. */
.paper-note { margin: 12px 16px 0; font-size: 14px; color: var(--muted); }

@keyframes paper-rise {
  from { opacity: 0; transform: translateY(26px) scale(.975) rotateX(6deg); }
  60% { opacity: 1; }
  to { opacity: 1; transform: none; }
}
@keyframes paper-sheen { from { background-position: 100% 100%; } to { background-position: 0 0; } }
@keyframes paper-fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes skeleton-breathe { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-4px); } }
@keyframes skeleton-shimmer { from { background-position: 160% 0; } to { background-position: -60% 0; } }
@keyframes dialog-in { from { opacity: 0; transform: translateY(18px) scale(.96); } to { opacity: 1; transform: none; } }

@media (max-width: 599px) {
  .paper-toolbar { padding: 8px 10px; }
  .paper-zoom { display: none; }
  /* The pager needs the room; the download keeps its word, it is what the dialog is for. */
  .paper-open:not(.paper-download) span { display: none; }
  .paper-stage { max-height: none; overflow: visible; padding: 14px 10px 24px; gap: 18px; }
  .mail-card { padding: 16px; }
  .paper-dialog { width: 100vw; max-height: 100dvh; border-radius: 0; }
  .paper-dialog-frame { max-height: 100dvh; }
  .paper-dialog .paper-stage { max-height: none; }
}

@media print {
  .paper-toolbar, .paper-thumbs, .paper-dialog { display: none; }
}
