/*
 * scan-viewer.css: the scan pane, <vm-scan-viewer> (js/components/scan-viewer.js).
 *
 * The pane is dark in BOTH themes, so it reads the theme-independent --scan-* tokens from tokens.css
 * and falls back to the palette in DESIGN-BRIEF.md. Local --sv-* variables are the only knobs:
 *   --sv-page-pad / --sv-page-gap   space around / between pages (read by the JS layout too)
 *   --sv-toolbar-h                  toolbar height
 *   --sv-max-fit-w                  widest a page gets at "fit width" (zoom goes further)
 * Optional extra tokens a theme may define: --scan-fg-muted, --scan-border, --scan-icon, --scan-accent.
 *
 * The element fills its parent: give the parent a height (the shell's .pane-scan grid cell does), or
 * make it a flex column. Nothing outside vm-scan-viewer is styled here.
 */

vm-scan-viewer {
  /* Every colour comes from the --scan-* tokens (tokens.css, always loaded with the shell). */
  --sv-bg: var(--scan-bg);
  --sv-toolbar-bg: var(--scan-toolbar-bg);
  --sv-control-bg: var(--scan-control-bg);
  --sv-active-bg: var(--scan-active-bg);
  --sv-fg: var(--scan-fg);
  --sv-icon: var(--scan-icon);
  --sv-muted: var(--scan-fg-muted);
  --sv-accent: var(--scan-accent);
  --sv-border: var(--scan-border);
  --sv-input-border: rgb(255 255 255 / 0.07);
  --sv-placeholder: var(--scan-placeholder);
  --sv-shadow: var(--shadow-paper);
  --sv-page-pad: 16px;
  --sv-page-gap: 16px;
  --sv-toolbar-h: 44px;
  --sv-btn: 32px;
  --sv-max-fit-w: 1100px;

  container: vm-scan / inline-size;
  position: relative;
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  height: 100%;
  overflow: hidden;
  background: var(--sv-bg);
  color: var(--sv-fg);
  color-scheme: dark;
  font: 400 var(--fs-search) / 1.4 var(--font-ui);
  -webkit-tap-highlight-color: transparent;
}

vm-scan-viewer *,
vm-scan-viewer *::before,
vm-scan-viewer *::after {
  box-sizing: border-box;
}

vm-scan-viewer [hidden] {
  display: none !important;
}

vm-scan-viewer .icon {
  width: 16px;
  height: 16px;
  flex: none;
}

/* ---------------------------------------------------------------- toolbar */

.sv-toolbar {
  position: relative;
  z-index: 2;
  display: flex;
  flex: none;
  align-items: center;
  gap: 8px;
  height: var(--sv-toolbar-h);
  padding: 0 8px;
  background: var(--sv-toolbar-bg);
  border-bottom: 1px solid var(--sv-border);
}

.sv-nav {
  display: flex;
  flex: 1 1 auto;
  align-items: center;
  min-width: 0;
}

.sv-tools {
  display: flex;
  flex: none;
  align-items: center;
}

.sv-btn {
  position: relative;
  display: inline-flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: var(--sv-btn);
  height: var(--sv-btn);
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--sv-icon);
  font: inherit;
  text-decoration: none;
  cursor: pointer;
  transition: background-color 0.12s ease, color 0.12s ease;
}

@media (hover: hover) {
  .sv-btn:hover {
    background: var(--sv-control-bg);
    color: var(--sv-fg);
  }
}

.sv-btn[aria-pressed="true"] {
  background: var(--sv-active-bg);
  color: var(--sv-accent);
}

/* Open panel (thumbnails): a neutral "on" state; the accent tint is kept for the sync toggle. */
.sv-btn[aria-expanded="true"] {
  background: var(--sv-control-bg);
  color: var(--sv-fg);
}

.sv-btn[aria-disabled="true"] {
  background: transparent;
  color: var(--sv-icon);
  opacity: 0.38;
  cursor: default;
}

.sv-btn:focus-visible,
.sv-page-input:focus-visible,
.sv-retry:focus-visible {
  outline: 2px solid var(--sv-accent);
  outline-offset: 1px;
}

/* Fullscreen only matters on phones; everywhere else the pane already has its own column. */
.sv-btn[data-cmd="fullscreen"] {
  display: none;
}

@media (max-width: 767.98px) {
  .sv-btn[data-cmd="fullscreen"] {
    display: inline-flex;
  }
}

.sv-page-input {
  width: 46px;
  height: 26px;
  margin: 0 7px 0 6px;
  padding: 0 4px;
  border: 1px solid var(--sv-input-border);
  border-radius: 4px;
  background: var(--sv-control-bg);
  color: var(--sv-fg);
  font: inherit;
  font-size: var(--fs-sm);
  font-variant-numeric: tabular-nums;
  text-align: center;
}

.sv-page-input:disabled {
  opacity: 0.38;
}

.sv-total {
  margin-right: 4px;
  color: var(--sv-muted);
  font-size: var(--fs-sm);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.sv-label {
  min-width: 0;
  margin-left: 6px;
  overflow: hidden;
  color: var(--sv-muted);
  font-size: var(--fs-sm);
  white-space: nowrap;
  text-overflow: ellipsis;
}

/* Tooltips (the accessible name is aria-label; these only add the visible hint + shortcut). */
.sv-btn[data-tip]::after {
  content: attr(data-tip);
  position: absolute;
  top: calc(100% + 6px);
  z-index: 3;
  padding: 6px 8px;
  border-radius: 4px;
  background: var(--scan-tip-bg);
  color: var(--scan-tip-fg);
  font-size: var(--fs-sm);
  line-height: 1;
  white-space: pre;
  pointer-events: none;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.12s ease, visibility 0s linear 0.12s;
}

.sv-nav .sv-btn[data-tip]::after {
  left: 0;
}

.sv-tools .sv-btn[data-tip]::after {
  right: 0;
}

@media (hover: hover) {
  .sv-btn[data-tip]:hover::after {
    opacity: 1;
    visibility: visible;
    transition-delay: 0.45s, 0.45s;
  }
}

.sv-btn[data-tip]:focus-visible::after {
  opacity: 1;
  visibility: visible;
  transition-delay: 0s;
}

/* Narrow panes (side by side on tablets and smaller desktops, phones) drop the least-needed controls
   first: fit page and open image (data-prio 3), then the printed page label, then rotate (2), and
   the zoom buttons (1) only on panes narrower than a portrait tablet's half. Their keyboard
   shortcuts and Ctrl+wheel keep working. (Full screen shows on phones only.) */
@container vm-scan (max-width: 540px) {
  .sv-tools [data-prio="3"] { display: none; }
}
@container vm-scan (max-width: 470px) {
  .sv-label { display: none; }
}
@container vm-scan (max-width: 400px) {
  .sv-tools [data-prio="2"] { display: none; }
}
@container vm-scan (max-width: 359.98px) {
  .sv-tools [data-prio="1"] { display: none; }
}

/* Touch screens: 40px buttons and a taller page field (the toolbar grows to fit them). */
@media (pointer: coarse) {
  vm-scan-viewer {
    --sv-btn: 40px;
    --sv-toolbar-h: 48px;
  }
  .sv-toolbar { gap: 2px; padding: 0 4px; }
  .sv-page-input { height: 36px; }
  /* The bigger buttons need more room: the same steps, at wider panes. A portrait tablet's side by
     side pane (380–420px) keeps zoom and gives up rotate. */
  @container vm-scan (max-width: 620px) {
    .sv-tools [data-prio="3"] { display: none; }
  }
  @container vm-scan (max-width: 560px) {
    .sv-label { display: none; }
  }
  @container vm-scan (max-width: 500px) {
    .sv-tools [data-prio="2"] { display: none; }
  }
  @container vm-scan (max-width: 379.98px) {
    .sv-tools [data-prio="1"] { display: none; }
  }
}
/* Phones: zoom is a pinch, so rotate keeps its place instead. */
@media (pointer: coarse) and (max-width: 767.98px) {
  .sv-tools [data-prio="1"] { display: none; }
  .sv-tools .sv-btn[data-prio="2"] { display: inline-flex; }
}

/* ---------------------------------------------------------------- pages */

.sv-body {
  position: relative;
  display: flex;
  flex: 1 1 auto;
  min-height: 0;
}

.sv-scroller {
  flex: 1 1 auto;
  min-width: 0;
  overflow: auto;
  overscroll-behavior: contain;
  scroll-behavior: auto;
  /* Both edges: the paper stays centred (a one-sided gutter pushed it 10px left). */
  scrollbar-gutter: stable both-edges;
  touch-action: pan-x pan-y;
  outline: none;
}

/* Scrollbars: rounded thumbs without arrow buttons (see base.css; the standard properties would
   switch Chromium to its native bar with arrows, so only other browsers get them). */
@supports not selector(::-webkit-scrollbar) {
  .sv-scroller, .sv-thumbs { scrollbar-width: thin; scrollbar-color: var(--scan-scrollbar) transparent; }
}
.sv-scroller::-webkit-scrollbar-thumb,
.sv-thumbs::-webkit-scrollbar-thumb { background-color: var(--scan-scrollbar); }

.sv-scroller:focus-visible {
  outline: 2px solid var(--sv-accent);
  outline-offset: -2px;
}

vm-scan-viewer[data-pannable] .sv-scroller {
  cursor: grab;
}

vm-scan-viewer[data-dragging] .sv-scroller {
  cursor: grabbing;
}

.sv-stack {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sv-page-gap);
  width: max-content;
  min-width: 100%;
  padding: var(--sv-page-pad);
}

.sv-page {
  position: relative;
  flex: none;
  overflow: hidden;
  background: var(--sv-placeholder);
  box-shadow: var(--sv-shadow);
  content-visibility: auto;
  contain-intrinsic-size: auto 800px;
}

.sv-page[data-state="loaded"] {
  background: var(--scan-paper);
}

.sv-sheet {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) rotate(var(--sv-rot, 0deg));
}

.sv-img {
  display: block;
  width: 100%;
  max-width: none;
  height: 100%;
  -webkit-user-drag: none;
  user-select: none;
}

.sv-ph {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--scan-placeholder-fg);
  font-size: var(--fs-search);
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}

.sv-page[data-state="loaded"] .sv-ph,
.sv-page[data-state="error"] .sv-ph {
  display: none;
}

/* In the upper part of the page (a fit-width page's middle is usually below the fold).
   Percentage padding is relative to the page width: about 200px down on desktop, 80px on phones. */
.sv-error {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 0.5em;
  padding: clamp(24px, 22%, 200px) 16px 16px;
  color: var(--sv-muted);
  text-align: center;
}

.sv-retry {
  margin: 0;
  padding: 2px 4px;
  border: 0;
  border-radius: 3px;
  background: none;
  color: var(--sv-fg);
  font: inherit;
  text-decoration: underline;
  text-decoration-color: rgb(232 230 220 / 0.4);
  text-underline-offset: 3px;
  cursor: pointer;
}

.sv-retry:hover {
  text-decoration-color: currentColor;
}

/* NCC column highlight: dims the other half of the sheet (rotates with it). Clicking it switches
   the highlighted column. */
.sv-mask {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 50%;
  background: rgb(20 18 17 / 0.62);
  cursor: pointer;
  animation: sv-fade-in 0.16s ease-out;
}

.sv-mask[data-dim="right"] {
  right: 0;
}

.sv-mask[data-dim="left"] {
  left: 0;
}

@keyframes sv-fade-in {
  from {
    opacity: 0;
  }
}

.sv-empty {
  margin: 0;
  padding: 48px 16px;
  color: var(--sv-muted);
  text-align: center;
}

/* ---------------------------------------------------------------- thumbnails */

.sv-thumbs {
  position: absolute;
  inset: 0;
  z-index: 1;
  overflow: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
  background: var(--sv-bg);
}

.sv-thumb-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 20px 16px;
  align-items: start;
  margin: 0;
  padding: 20px 16px;
  list-style: none;
}

.sv-thumb {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  width: 100%;
  margin: 0;
  padding: 0;
  border: 0;
  background: none;
  color: var(--sv-muted);
  font: inherit;
  font-size: var(--fs-sm);
  font-variant-numeric: tabular-nums;
  cursor: pointer;
}

.sv-thumb:focus,
.sv-thumb:focus-visible {
  outline: none;
}

/* Every thumbnail sits in a well of the usual page shape (--sv-well = height / width, set by the JS),
   bottom-aligned, so the page numbers line up even when a plate is landscape. */
.sv-thumb-well {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  width: 100%;
  aspect-ratio: 1 / var(--sv-well, 1.414);
}

.sv-thumb-img {
  position: relative;
  display: block;
  flex: none;
  width: 100%;
  overflow: hidden;
  background: var(--sv-placeholder);
  box-shadow: var(--sv-shadow);
  outline: 2px solid transparent;
  outline-offset: 3px;
  transition: outline-color 0.12s ease;
}

.sv-thumb-img img {
  display: block;
  width: 100%;
  max-width: none;
  height: 100%;
  object-fit: cover;
  background: var(--scan-paper);
}

@media (hover: hover) {
  .sv-thumb:hover .sv-thumb-img {
    outline-color: rgb(255 255 255 / 0.22);
  }
  .sv-thumb:hover {
    color: var(--sv-fg);
  }
}

.sv-thumb[aria-current="page"] .sv-thumb-img {
  outline-color: var(--sv-accent);
}

.sv-thumb[aria-current="page"] {
  color: var(--sv-accent);
}

.sv-thumb:focus-visible .sv-thumb-img {
  outline-color: var(--sv-fg);
}

/* ---------------------------------------------------------------- fullscreen, a11y, motion */

vm-scan-viewer[fullscreen] {
  position: fixed;
  inset: 0;
  z-index: 1000;
  height: auto;
}

html.sv-fullscreen-open {
  overflow: hidden;
}

.sv-live {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

@media (prefers-reduced-motion: reduce) {
  vm-scan-viewer *,
  vm-scan-viewer *::before,
  vm-scan-viewer *::after {
    transition: none !important;
    animation: none !important;
  }
}

@media (forced-colors: active) {
  .sv-btn[aria-pressed="true"] {
    outline: 2px solid Highlight;
  }
  .sv-thumb[aria-current="page"] .sv-thumb-img {
    outline-color: Highlight;
  }
}
