/* The shared photo viewer (task 402, PRD 023 §7.2).
 *
 * Loaded by the public album page and by the curator's admin tool, from one URL, so a fix lands on both.
 * There is no build step behind this file and no framework in front of it: it is served from the Go binary
 * exactly as written.
 *
 * # Conventions this file keeps, and why
 *
 * Every class is prefixed `hv-`, and the root is `.hv`. The two host surfaces have their own stylesheets —
 * the public site's is inline in its template, the admin tool's is Pico plus `page.css` — and this file is
 * loaded alongside both. An unprefixed `.stage` or `.info` would collide with one of them eventually, and the
 * failure would look like a viewer bug on one surface only.
 *
 * Hex colours, no nesting, no custom-property fallbacks: the same constraints the public site's stylesheet
 * works under (task 204, a 2013 iPad). This file is newer than that rule but shares its audience.
 *
 * PICO: the admin tool loads Pico, which styles bare `dialog` heavily — `display: flex`, its own background, a
 * backdrop filter — and puts it at `z-index: 999`, while the tool's own overlays sit at 1040/1050 above it.
 * Everything here is therefore scoped to `.hv` rather than to the element, and `.hv` sits above both. The
 * public site has no Pico and is unaffected either way.
 */

.hv {
  /* Fills the viewport rather than being centred in it: this is a photograph, not a message. */
  position: fixed;
  inset: 0;
  z-index: 1100;
  display: block;
  width: 100%;
  max-width: 100%;
  height: 100%;
  max-height: 100%;
  margin: 0;
  padding: 0;
  border: 0;
  /* Dark, not black. #111 behind a photograph reads as a room; pure black makes a dark photograph look like
     a loading failure, and this album is full of pictures taken at night. */
  background: #111;
  color: #f4f4f5;
  overflow: hidden;
  /* **Nothing in here is selectable** (task 420).
   *
   * Two fast presses on the next arrow are a double-click as far as the browser is concerned, and a double-click
   * selects — so moving quickly through an album left a blue highlight over the photograph. That is the whole bug:
   * the clicks themselves register fine, it is the *selection* that is unwanted.
   *
   * The whole overlay rather than just the arrows, because the same thing happens on any of the controls and
   * because there is nothing here a visitor would want to select. The caption is the only text, and it is already
   * `pointer-events: none`, so this takes nothing away that was reachable. The editor's field opts back in — see
   * `.hv-edit-field`.
   *
   * Prefixed as well as not: Safari wanted `-webkit-user-select` until well after the 16.4 floor in `.rules`. */
  -webkit-user-select: none;
  user-select: none;
}

.hv::backdrop {
  background: #111;
}

/* **A closed dialog must be told to hide, because the rule above unhides it** (task 415).
 *
 * The browser's own stylesheet has `dialog:not([open]) { display: none }`, and it is tempting to think the rule
 * above loses to it on specificity — `dialog:not([open])` is (0,1,1) against `.hv`'s (0,1,0). It does not, and
 * that is the trap: **the cascade compares origin before specificity**, and any author declaration beats a
 * user-agent one however specific. So `display: grid` above applies to a *closed* dialog too, and `.close()`
 * leaves a full-viewport dark box sitting over the page with nothing in it — which is exactly the bug reported
 * from Brave on macOS: the photograph vanished (the JavaScript ran) and the viewer stayed (the CSS won).
 *
 * This is also why Pico ships the same rule for the elements it styles. Anything that sets `display` on a
 * `dialog` has to hide it again by hand. */
.hv:not([open]) {
  display: none;
}

/* The frame is the whole viewer, and **the element that goes fullscreen** (task 419).
 *
 * It exists because none of the simpler candidates works: the dialog is refused for being in the top layer
 * already, the document element joins the top layer above the dialog and paints the album page over the
 * photograph, and the stage alone leaves the filmstrip out because the strip is its sibling.
 *
 * It carries the layout — photograph above, filmstrip below — so that the same two rows apply whether it is
 * filling the dialog or filling the screen. That is the point of putting the grid here rather than on the dialog:
 * one layout, two containers.
 *
 * A background and a backdrop of its own, because in fullscreen the dialog that supplies the colour is no longer
 * behind it, and the browser's default backdrop is a different black from the viewer's. */
.hv-frame {
  display: grid;
  grid-template-rows: 1fr auto;
  width: 100%;
  height: 100%;
  min-height: 0;
}

.hv-frame:fullscreen {
  background: #111;
}

.hv-frame::backdrop {
  background: #111;
}

/* The photograph, and the two arrows that sit over it. */
.hv-stage {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  padding: 0.5rem;
}

.hv-img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  display: block;
}

/* The video player (PRD 029, task 499). Sized like the photograph, and only one of the two is ever shown. */
.hv-video {
  display: none;
  max-width: 100%;
  max-height: 100%;
  background: #000;
}

.hv.is-video .hv-video {
  display: block;
}

.hv.is-video .hv-img {
  display: none;
}

.hv.is-video.is-missing .hv-video {
  display: none;
}

/* Above the native controls, centred, and large enough to hit with a thumb. */
.hv-unmute {
  position: absolute;
  left: 50%;
  bottom: 4.5rem;
  transform: translateX(-50%);
  min-height: 44px;
  padding: 0.5rem 1rem;
  border: 0;
  border-radius: 999px;
  background: rgba(24, 24, 27, 0.85);
  color: #fafafa;
  font: inherit;
  cursor: pointer;
}

.hv-unmute[hidden] {
  display: none;
}

/* HD/SD (task 500): bottom-left, above the native controls and clear of the unmute button. */
.hv-quality {
  position: absolute;
  left: 0.75rem;
  bottom: 4.5rem;
  min-width: 44px;
  min-height: 44px;
  border: 1px solid rgba(250, 250, 250, 0.5);
  border-radius: 0.375rem;
  background: rgba(24, 24, 27, 0.85);
  color: #fafafa;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.hv-quality[hidden] {
  display: none;
}

/* Hidden until an image fails, and then it replaces the photograph rather than sitting under a broken icon.
   A photograph can disappear between the page loading and a visitor reaching it — that is what a takedown
   does — so this is an ordinary state, not an error. */
.hv-missing {
  display: none;
  max-width: 20rem;
  margin: 0;
  color: #a1a1aa;
  text-align: center;
}

.hv.is-missing .hv-img {
  display: none;
}

.hv.is-missing .hv-missing {
  display: block;
}

/* The arrows (task 416).
 *
 * **A small icon with a large hit area**, which the maintainer asked for in those words. The button is a tall
 * column down the side of the photograph — easy to hit with a thumb on a phone and with a wandering cursor on a
 * laptop — while the thing you see is only the icon's own circle. The visible size and the clickable size are
 * different properties and there is no reason for them to be the same number.
 *
 * The circle is painted on the `svg` rather than on a wrapper element, so the hit area is a plain CSS matter and
 * the markup stays one button with one icon in it. */
.hv-nav {
  position: absolute;
  top: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 22%;
  max-width: 7rem;
  padding: 0;
  border: 0;
  background: transparent;
  color: #fff;
  cursor: pointer;
}

.hv-prev { left: 0; }
.hv-next { right: 0; }

.hv-nav:disabled {
  opacity: 0.25;
  cursor: default;
}

.hv-nav svg {
  background: rgba(0, 0, 0, 0.45);
  border-radius: 50%;
  padding: 0.5rem;
}

.hv-nav svg,
.hv-bar button svg {
  width: 1.5rem;
  height: 1.5rem;
}

/* The action row, top right (§7.2). Absolutely positioned over the stage so the photograph keeps the height:
   a reserved strip at the top would cost every photograph 3rem for the sake of three buttons. */
.hv-bar {
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  z-index: 2;
  display: flex;
  justify-content: flex-end;
  gap: 0.25rem;
  padding: 0.5rem;
  /* A gradient rather than a bar: a white photograph behind a white icon is unreadable, and a solid strip
     across the top of a dark one is a stripe of nothing. */
  background: linear-gradient(to bottom, rgba(0, 0, 0, 0.55), rgba(0, 0, 0, 0));
  pointer-events: none;
}

.hv-bar button {
  pointer-events: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2.75rem;
  height: 2.75rem;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: #fff;
  cursor: pointer;
}

.hv-bar button:hover,
.hv-nav:hover:not(:disabled) svg {
  background: rgba(255, 255, 255, 0.18);
}

.hv :focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
}

/* The info panel: the caption, then the credit under it, quieter (task 393's reasoning — the caption is what
   the photograph is of, the credit is who took it, and one sentence containing both reads as though the
   photographer were part of the scene).

   **Over the photograph, not below it** (task 416). As a row of the dialog's grid it took its own height, so a
   captioned photograph made the stage shorter and the arrows sat somewhere else than they did on the one before —
   furniture moving under the cursor between one press and the next. Absolutely positioned inside the stage, the
   geometry is identical for every photograph, captioned or not.

   **Rendered only when it has content**: an empty grey box under an uncaptioned photograph is furniture
   pretending to be information.

   `pointer-events: none` because nothing in here is interactive, and it sits over the middle of the picture where
   a swipe starts and beside the arrows' columns. A caption that swallowed a swipe would be a caption that broke
   the album. */
.hv-info {
  position: absolute;
  bottom: 0.75rem;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1;
  width: max-content;
  max-width: min(44rem, calc(100% - 16rem));
  padding: 0.6rem 0.9rem;
  border-radius: 0.5rem;
  background: rgba(63, 63, 70, 0.72);
  color: #f4f4f5;
  font-size: 0.95rem;
  line-height: 1.4;
  text-align: center;
  pointer-events: none;
}

.hv-info:empty {
  display: none;
}

.hv-caption {
  display: block;
}

.hv-credit {
  display: block;
  margin-top: 0.15rem;
  color: #a1a1aa;
  font-size: 0.85rem;
}

/* The photographer's own filename (task 475), under the credit and quieter than it.
   Only the curator's tool provides one, so this rule never applies on the public pages. Monospace, because a
   filename is an identifier a photographer scans and compares against a folder on their own computer —
   `DSC_0142.JPG` against `DSC_0143.JPG` — and a proportional font makes that harder than it needs to be. */
.hv-filename {
  display: block;
  margin-top: 0.15rem;
  color: #8b8b93;
  font-size: 0.8rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  word-break: break-all;
}

/* A photograph the curator has deleted from the library (task 406). It still appears in their sheet, marked, and
   it must stay marked here: presenting it as live is how something taken down on purpose gets re-published. Amber
   rather than red — it is a state, not a failure. */
.hv-deleted {
  display: block;
  margin-top: 0.15rem;
  color: #fcd34d;
  font-size: 0.85rem;
}

/* The edit panel (tasks 407, 408). Built by the host page — the admin tool — and dropped into this dialog's stage,
   so the styling lives here with the rest of the overlay rather than in a stylesheet that knows nothing about it.

   The shared file styling a control it does not create is a small asymmetry, and the alternative is worse: the
   admin tool's `page.css` would be describing the inside of a dialog it does not own, and a change to this
   layout would need edits in two files that are cached separately.

   Inside the stage and over the photograph (task 418), for two reasons that turned out to be the same reason:
   the stage is the element that goes fullscreen, so a panel outside it would open where nobody could see it; and
   a panel in the flow would change the stage's height and move the arrows, which is the bug task 416 fixed for
   the info panel. It takes that panel's place while it is open — see `.hv.is-editing`. */
.hv-edit {
  position: absolute;
  bottom: 0.75rem;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2;
  width: min(34rem, calc(100% - 2rem));
  box-sizing: border-box;
  padding: 0.75rem 0.9rem;
  border-radius: 0.5rem;
  background: rgba(39, 39, 42, 0.94);
}

/* While an editor is open, the read-only copy of the same text underneath it would be one sentence out of date. */
.hv.is-editing .hv-info {
  display: none;
}

.hv-edit[hidden] {
  display: none;
}

.hv-edit-heading {
  margin: 0;
  font-weight: 600;
}

.hv-edit-hint {
  margin: 0.15rem 0 0.4rem;
  color: #a1a1aa;
  font-size: 0.85rem;
  line-height: 1.35;
}

.hv-edit-field {
  width: 100%;
  box-sizing: border-box;
  padding: 0.4rem 0.5rem;
  border: 1px solid #52525b;
  border-radius: 0.25rem;
  background: #18181b;
  color: #f4f4f5;
  font: inherit;
  resize: vertical;
  /* The one place in the overlay where text must be selectable, because it is text being written: the rest is
     `user-select: none` so that a fast double-press on an arrow does not highlight the photograph (task 420).
     Without this, a curator could not select a word of their own caption to replace it. */
  -webkit-user-select: text;
  user-select: text;
}

/* The public form of a credit, shown while it is being typed rather than after it is saved: a curator putting a
   colleague's name on a public page should be looking at the public version of it as they write. */
.hv-edit-preview {
  margin: 0.35rem 0 0;
  color: #fcd34d;
  font-size: 0.85rem;
}

.hv-edit-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin: 0.5rem 0 0;
}

.hv-edit-row button {
  padding: 0.35rem 0.75rem;
  border: 1px solid #52525b;
  border-radius: 0.25rem;
  background: #3f3f46;
  color: #f4f4f5;
  font: inherit;
  cursor: pointer;
}

.hv-edit-row button:hover {
  background: #52525b;
}

/* Clearing a credit is its own button, and it is styled as the destructive one it is — removing an attribution
   should look different from saving one, not merely be a different button in the same row. */
.hv-edit-clear {
  border-color: #b45309 !important;
}

.hv-edit-clear[hidden] {
  display: none;
}

.hv-edit-note {
  margin: 0.35rem 0 0;
  min-height: 1.2em;
  color: #a1a1aa;
  font-size: 0.85rem;
}

/* The filmstrip. One row, scrolled horizontally, the current photograph ringed and kept centred. */
.hv-strip {
  display: flex;
  gap: 0.35rem;
  padding: 0.5rem;
  overflow-x: auto;
  /* Kinetic scrolling on iOS and no scrollbar gutter stealing height on the desktop. */
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.hv-strip::-webkit-scrollbar {
  display: none;
}

.hv-strip button {
  flex: 0 0 auto;
  width: 3.5rem;
  height: 3.5rem;
  padding: 0;
  border: 2px solid transparent;
  border-radius: 0.25rem;
  background: #27272a;
  cursor: pointer;
  overflow: hidden;
}

.hv-strip img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.hv-strip button[aria-current="true"] {
  border-color: #fff;
}

/* **The filmstrip goes on a small screen** (PRD 023 §6, answering §11.3).
 *
 * Both dimensions, and the second one is the one that gets forgotten: a landscape phone is not a narrow
 * viewport, it is a short one, and that is precisely where 15% of the height for five thumbnails hurts most.
 * Swipe and the arrows already cover moving through the album.
 *
 * A media query rather than JavaScript measuring the window, deliberately: this re-evaluates on rotation for
 * free, while a branch taken once at open is wrong the moment a phone turns sideways.
 */
@media (max-width: 40rem), (max-height: 34rem) {
  .hv-strip {
    display: none;
  }
}

/* Transitions only where motion is wanted. The photograph fading in is the one piece of movement here worth
   having, because it covers the gap between two images loading. */
@media (prefers-reduced-motion: no-preference) {
  .hv-img {
    transition: opacity 0.12s ease-in-out;
  }

  .hv.is-loading .hv-img {
    opacity: 0.35;
  }
}
