/*
 * assets/css/pwa.css — the install prompt.
 * ===========================================================================
 *
 *  Plain CSS, deliberately, not Tailwind.
 *
 *  Everything else on this site is Tailwind compiled from
 *  assets/css/tailwind.input.css into assets/css/tailwind.css, and the normal
 *  way to add a component is to write utility classes and run `npm run
 *  build:css`. That is the right way for a component that belongs to the site.
 *
 *  This is not that. The install prompt is the one piece of UI that has to
 *  appear on a page the visitor arrived at from a link in a chat, on a phone
 *  that may be on a slow or expensive connection, in an app that may have been
 *  installed weeks ago — and it has to work even if the compiled stylesheet on
 *  the server is an old build that predates this file entirely.
 *
 *  Compiling it into tailwind.css would tie it to a build step and to whatever
 *  version of the bundle that particular phone's cache happens to hold. Shipping
 *  it as its own small file, linked with the mtime cache-buster the rest of the
 *  site uses (asset_url), means it can be fixed or restyled by replacing one
 *  file, with no rebuild and no chance of a stale bundle hiding the change.
 *
 *  It also means this component can be deleted by deleting two files.
 *
 *  Everything here is namespaced under #pwaPrompt so nothing can leak into the
 *  site's own class names, and the file is only linked when the app feature is
 *  switched on.
 */

/* ===========================================================================
 *  THE CARD
 * ===========================================================================
 *
 *  A bottom sheet on a phone, a docked card in the corner on a desktop. Two
 *  shapes from one element, because they are answering two different problems:
 *
 *    phone     the bottom edge is the one place a thumb reaches without the
 *              hand moving, and it is the gesture everybody already knows from
 *              every app that has ever asked them to install something
 *
 *    desktop   a corner card gets out of the way of the content. This site's
 *              home page is a scrolling grid of posters, and a full-width bar
 *              across the bottom of that hides a row of them.
 */

#pwaPrompt {
  position: fixed;
  z-index: 200;                 /* above the header (50) and the tab bar (30) */
  left: 0;
  right: 0;
  bottom: 0;

  /* Above the mobile tab bar rather than under it. The bar is 30 and has
     env(safe-area-inset-bottom) of its own, so the prompt has to clear both or
     its button ends up underneath the one the visitor is already aiming at. */
  bottom: calc(var(--nav-tap, 56px) + env(safe-area-inset-bottom, 0px) + 10px);

  padding: 0 12px;
  margin: 0 auto;
  max-width: 560px;

  /* Off-screen until the script adds .is-open. Not display:none — the slide-in
     needs it laid out to animate from, and a hidden-then-shown element has
     nothing to transition. visibility is used so it is also out of the tab
     order and off the accessibility tree while it waits. */
  visibility: hidden;
  opacity: 0;
  transform: translateY(16px);
  transition: opacity .26s cubic-bezier(.22, 1, .36, 1),
              transform .26s cubic-bezier(.22, 1, .36, 1),
              visibility 0s linear .26s;
  pointer-events: none;
}

#pwaPrompt.is-open {
  visibility: visible;
  opacity: 1;
  transform: translateY(0);
  transition: opacity .26s cubic-bezier(.22, 1, .36, 1),
              transform .26s cubic-bezier(.22, 1, .36, 1),
              visibility 0s;
  pointer-events: auto;
}

#pwaCard {
  display: flex;
  align-items: flex-start;
  gap: 13px;

  position: relative;
  padding: 14px;

  background: #1c1c1c;
  border: 1px solid #303030;
  border-radius: 16px;

  /* The brand colour as a glow behind the card, and as a hairline along the
     top. It is the one place the operator's logo colour reaches the interface,
     and it is what stops this looking like a browser notification bolted onto
     somebody's site. --pwa-brand is written inline by includes/pwa.php from
     pwa_theme_color(), the same value the status bar and the app icon use. */
  box-shadow: 0 10px 34px rgba(0, 0, 0, .55),
              0 0 0 1px rgba(255, 255, 255, .04);
}

#pwaCard::before {
  content: "";
  position: absolute;
  inset: 0 0 auto 0;
  height: 2px;
  border-radius: 16px 16px 0 0;
  background: linear-gradient(90deg,
              var(--pwa-brand, #ff0000) 0%,
              color-mix(in srgb, var(--pwa-brand, #ff0000) 35%, transparent) 100%);
  opacity: .85;
  pointer-events: none;
}

/* ===========================================================================
 *  THE ICON
 * ===========================================================================
 *
 *  The app icon the visitor is being asked to put on their home screen, drawn
 *  from the site's own uploaded logo by appicon.php — so it is genuinely a
 *  preview of what they will get, not a generic download glyph.
 */

#pwaIcon {
  flex: 0 0 auto;
  width: 52px;
  height: 52px;
  border-radius: 13px;
  overflow: hidden;
  background: var(--pwa-brand, #ff0000);
  display: grid;
  place-items: center;
  box-shadow: 0 3px 12px rgba(0, 0, 0, .45);
}

#pwaIcon img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

/* ===========================================================================
 *  THE TEXT
 * ===========================================================================
 *
 *  min-width:0 on the middle column, which looks like nothing and is the whole
 *  reason the title ellipsises instead of pushing the button off the edge of a
 *  narrow phone. Without it a flex child refuses to shrink below its content
 *  width, and a long site name takes the card apart.
 */

#pwaBody {
  flex: 1 1 auto;
  min-width: 0;
  padding-top: 1px;
}

#pwaTitle {
  margin: 0 0 3px;
  font-size: 15px;
  font-weight: 600;
  line-height: 1.3;
  letter-spacing: -.01em;
  color: #f1f1f1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#pwaText {
  margin: 0;
  font-size: 12.5px;
  line-height: 1.45;
  color: #aaaaaa;

  /* Two lines, then ellipsis. A third line pushes the card tall enough to cover
     a poster row on a small phone, which is the trade this is avoiding. */
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* ===========================================================================
 *  THE ACTIONS
 * ===========================================================================
 */

#pwaActions {
  flex: 0 0 auto;
  align-self: center;
  display: flex;
  align-items: center;
  gap: 8px;
}

/* The install button. Weight 600 at 13px rather than the site's usual 15px,
   because this is a compact card action rather than a page button, and the
   colour is the brand's so it reads as the primary action on the card. */
#pwaInstall {
  appearance: none;
  border: 0;
  cursor: pointer;
  height: 38px;
  padding: 0 16px;
  border-radius: 10px;

  background: var(--pwa-brand, #ff0000);
  color: #fff;
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: .01em;
  white-space: nowrap;

  transition: filter .15s ease, transform .1s ease;
}

#pwaInstall:hover  { filter: brightness(1.1); }
#pwaInstall:active { transform: scale(.97); }
#pwaInstall:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
}

/* While the browser's own install sheet is open the button is inert, and says
   so. Leaving it live underneath a modal sheet is how a second install gets
   queued behind the first. */
#pwaInstall[disabled] {
  opacity: .55;
  cursor: default;
  transform: none;
}

#pwaClose {
  appearance: none;
  border: 0;
  cursor: pointer;
  width: 32px;
  height: 32px;
  border-radius: 50%;

  background: transparent;
  color: #aaaaaa;
  display: grid;
  place-items: center;

  transition: background-color .15s ease, color .15s ease;
}

#pwaClose:hover  { background: #272727; color: #f1f1f1; }
#pwaClose:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 1px;
}

#pwaClose svg { width: 16px; height: 16px; fill: currentColor; }

/* The dismiss button on desktop, where there is room for both actions on the
   same line and a card that says "Install" alone leaves somebody unsure whether
   they are being asked or told. */
#pwaLater {
  appearance: none;
  border: 0;
  cursor: pointer;
  background: transparent;
  color: #aaaaaa;
  font-family: inherit;
  font-size: 12.5px;
  font-weight: 500;
  padding: 8px 4px;
  white-space: nowrap;
  transition: color .15s ease;
}

#pwaLater:hover { color: #f1f1f1; }
#pwaLater:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
  border-radius: 6px;
}

/* ===========================================================================
 *  THE MANUAL INSTRUCTIONS
 * ===========================================================================
 *
 *  Some browsers have no install API at all.
 *
 *  iOS Safari is the one everybody meets: there is no beforeinstallprompt, no
 *  way to ask it for the install sheet, only the Share button followed by "Add
 *  to Home Screen" — done by hand, every time. macOS Safari is the same
 *  problem with different words (File → Add to Dock).
 *
 *  So on those, the Install button is replaced by the actual steps, and the
 *  steps themselves are written by includes/js/pwa.js, which knows which
 *  platform it is on. Showing a button that silently does nothing would be
 *  worse than showing none, and showing iOS's steps to a Mac would be worse
 *  than that.
 */

#pwaSteps {
  display: none;
  margin: 0 0 0 65px;          /* aligned under the text, not under the icon */
  padding: 0;
  list-style: none;
  counter-reset: pwaStep;
}

/* One class, set by the script, rather than a per-platform attribute in CSS —
   the text and the presentation are decided in the same place, so a new
   platform is one array in the JS and not a selector here too. */
#pwaPrompt.is-instructions #pwaSteps { display: block; }
#pwaPrompt.is-instructions #pwaInstall { display: none; }
#pwaPrompt.is-instructions #pwaText   { -webkit-line-clamp: 3; }

#pwaSteps li {
  counter-increment: pwaStep;
  position: relative;
  padding-left: 24px;
  margin-bottom: 5px;
  font-size: 12.5px;
  line-height: 1.45;
  color: #aaaaaa;
}

#pwaSteps li:last-child { margin-bottom: 0; }

#pwaSteps li::before {
  content: counter(pwaStep);
  position: absolute;
  left: 0;
  top: 1px;

  width: 17px;
  height: 17px;
  border-radius: 50%;
  background: var(--pwa-brand, #ff0000);
  color: #fff;

  font-size: 10px;
  font-weight: 700;
  line-height: 17px;
  text-align: center;
}

/* The glyphs inline in a step — a share arrow, a plus — are the two things the
   instructions are actually about, so they are drawn rather than described. */
#pwaSteps .pwa-glyph {
  display: inline-flex;
  vertical-align: -3px;
  margin: 0 1px;
  color: #f1f1f1;
}

#pwaSteps .pwa-glyph svg {
  width: 14px;
  height: 14px;
  fill: currentColor;
}

/* ===========================================================================
 *  THE "INSTALLED" STATE
 * ===========================================================================
 *
 *  Once the app is on the home screen the prompt has nothing to say, so it is
 *  removed from the DOM entirely rather than hidden. Nothing on screen, and no
 *  button for a screen reader to walk past on every single page.
 */

/* ===========================================================================
 *  THE MANUAL BUTTON
 * ===========================================================================
 *
 *  For the case the automatic prompt cannot cover: a visitor who said "not now"
 *  three weeks ago and now wants the app. Lives in the sidebar's More menu, and
 *  is hidden by the same installed check that hides the sheet — so there is
 *  never a button that leads nowhere.
 */

#pwaMenuItem { display: none; }

#pwaMenuItem.is-available {
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
}

/* ===========================================================================
 *  DESKTOP
 * ===========================================================================
 *
 *  Above 768px the sidebar owns navigation and there is no tab bar to clear, so
 *  the card moves to the bottom-right corner and stops being a full-width sheet.
 *  Same element, same markup, no JavaScript involved.
 */

@media (min-width: 768px) {
  #pwaPrompt {
    left: auto;
    right: 20px;
    bottom: 20px;
    max-width: 400px;
    padding: 0;
  }

  #pwaCard {
    padding: 15px;
    gap: 14px;
  }

  #pwaTitle { font-size: 15.5px; }
  #pwaText  { font-size: 13px; }

  /* A corner card is not a sheet, so it grows in from the corner rather than
     rising from the bottom edge. */
  #pwaPrompt.is-open {
    transform: translateY(0) scale(1);
  }

  #pwaPrompt:not(.is-open) {
    transform: translateY(10px) scale(.97);
    transform-origin: bottom right;
  }
}

/* ===========================================================================
 *  A VERY SHORT PHONE
 * ===========================================================================
 *
 *  Below roughly 360px of width the button, the title and the icon stop fitting
 *  on one line. The text moves below rather than the card growing to three
 *  rows, because the point of the card is that it is smaller than the thing it
 *  is interrupting.
 */

@media (max-width: 359px) {
  #pwaCard { flex-wrap: wrap; }
  #pwaBody { flex: 1 1 100%; order: 2; padding-top: 2px; }
  #pwaActions { order: 3; margin-left: auto; }
  #pwaSteps { margin-left: 0; }
}

/* ===========================================================================
 *  MOTION AND CONTRAST PREFERENCES
 * ===========================================================================
 *
 *  Not a nicety. A card that slides up from the bottom of the screen is exactly
 *  the kind of thing that triggers a vestibular reaction, and this site already
 *  locks zoom on mobile (no-zoom.js), which means a visitor who cannot scroll
 *  away from a moving element has no way to escape it. The reduced-motion
 *  branch removes the movement and keeps only the appearance.
 */

@media (prefers-reduced-motion: reduce) {
  #pwaPrompt,
  #pwaPrompt.is-open {
    transition: opacity .01s linear;
    transform: none;
  }

  #pwaPrompt { transform: none; }
  #pwaInstall,
  #pwaClose,
  #pwaLater { transition: none; }
}

/* Windows High Contrast / forced-colours: the brand colour is not available
   there, so the card needs a border that is. */
@media (forced-colors: active) {
  #pwaCard { border: 1px solid CanvasText; }
  #pwaInstall { border: 1px solid ButtonText; forced-color-adjust: none; }
}

/* ===========================================================================
 *  PRINT
 * ===========================================================================
 *
 *  A prompt to install an app is meaningless on paper, and printing a web page
 *  with a fixed-position card on it puts the prompt over the last row of
 *  whatever was being printed.
 */

@media print {
  #pwaPrompt,
  #pwaMenuItem { display: none !important; }
}