/* WagerOS Agreements — proxy-layer white-label overlay for Documenso CE v2.18.0.
 *
 * Injected by Caddy (replace-response) as the last stylesheet in <head>, so it
 * wins on source order at equal specificity. The Documenso image is never
 * modified; `docker compose pull` stays the upgrade path.
 *
 * Selectors mirror the ones in the compiled bundle (/assets/app-*.css):
 *   :root,.dark-mode-disabled        light-mode design tokens
 *   .dark:not(.dark-mode-disabled)   dark-mode design tokens
 * Documenso's tokens are space-separated HSL channels consumed as
 * hsl(var(--token)), so overrides must keep that format (no #hex here).
 *
 * WagerOS cyan  #06B6D4 -> 188.7 94.5% 42.7%
 * WagerOS navy  #0F172A -> 222 47% 11%
 *
 * Every token declaration carries !important, because "last in <head>" is not
 * actually the last word: on route-level code-split pages (/articles/* is the
 * signer-facing one - the Electronic Signature Disclosure) react-router appends
 * a SECOND <link> to the same /assets/app-*.css during hydration, after this
 * file, and its :root block - identical specificity - then wins on order and
 * restores Documenso's lime. !important survives that; raising specificity with
 * an `html` prefix would not, because .dark-mode-disabled is applied to a
 * wrapper element on some routes, not to <html>. Nothing sets these tokens in
 * an inline style attribute, so no per-team branding is being overridden.
 */

:root,
.dark-mode-disabled {
  --primary: 188.7 94.5% 42.7% !important;
  /* Navy on cyan = 8.6:1. White on #06B6D4 is only 2.45:1 and fails WCAG AA,
     so the foreground stays dark — the same dark-on-bright pattern Documenso
     itself uses for its lime primary. */
  --primary-foreground: 222 47% 11% !important;
  --ring: 188.7 94.5% 42.7% !important;

  /* subtle hover/rest surface, tinted toward the brand instead of neutral grey */
  --accent: 187 60% 95% !important;
  --accent-foreground: 222 47% 11% !important;

  /* signing-field cards (lime in stock Documenso) */
  --field-card: 187 74% 92% !important;
  --field-card-border: 188.7 70% 62% !important;

  /* card sheen tint: plain RGB triplet upstream, not HSL */
  --card-border-tint: 6 182 212 !important;

  /* Documenso's own brand ramp, kept in case a component reads the var */
  --documenso-50: #f0fdff !important;
  --documenso-100: #dcfbff !important;
  --documenso-200: #cffafe !important;
  --documenso-300: #a5f3fc !important;
  --documenso-400: #67e8f9 !important;
  --documenso-500: #22d3ee !important;
  --documenso-600: #06b6d4 !important;
  --documenso-700: #0e7490 !important;
  --documenso-800: #155e75 !important;
  --documenso-900: #164e63 !important;
  --documenso-950: #083344 !important;
  --documenso: #06b6d4 !important;
}

.dark:not(.dark-mode-disabled) {
  --primary: 188.7 94.5% 42.7% !important;
  --primary-foreground: 222 47% 11% !important;
  --ring: 188.7 94.5% 42.7% !important;
  --secondary-foreground: 188.7 94.5% 52% !important;
  --accent-foreground: 188.7 94.5% 52% !important;
  --card-border-tint: 6 182 212 !important;
}

/* --- utility classes the bundle compiled to LITERAL lime, not to a var --------
 * Attribute selectors are used so the Tailwind `dark:` colon needs no CSS
 * escaping and so the rules survive changes to the surrounding :is() wrapper.
 * Both light and dark variants resolve to the same cyan, so applying the
 * dark:* rule in light mode is a no-op visually. */
[class~='bg-documenso'],
[class~='dark:bg-documenso'],
[class~='hover:bg-documenso']:hover {
  background-color: hsl(var(--primary)) !important;
}

[class~='bg-documenso-200'] {
  background-color: #cffafe !important;
}

[class~='border-documenso-300'] {
  border-color: #a5f3fc !important;
}

[class~='text-documenso'] {
  color: hsl(var(--primary)) !important;
}

[class~='text-documenso-700'] {
  color: #0e7490 !important;
}

[class~='text-documenso-800'] {
  color: #155e75 !important;
}

[class~='text-documenso-900'] {
  color: #164e63 !important;
}

/* Documenso's decorative page-background PNG is a faint lime wash - the last
 * visible cue of their palette on the auth pages. Rotate it onto the WagerOS
 * cyan instead of replacing the asset. */
img[alt='background pattern'] {
  filter: hue-rotate(95deg) saturate(0.9);
}

.dark:not(.dark-mode-disabled) img[alt='background pattern'] {
  filter: brightness(0.95) contrast(70%) invert(1) sepia(1) hue-rotate(140deg);
}

/* --- brand mark above the card on the unauthenticated layouts ----------------
 * Rendered as a ::before on the auth layout's <main>, which is a centred flex
 * column, so the mark becomes an in-flow flex item above the card and can never
 * overlap it at any viewport size.
 *
 * It MUST be a pseudo-element. Documenso is a hydrated React app: a real <img>
 * injected into <main> by the proxy is removed as soon as the client bundle
 * hydrates, because React does not own that node. Pseudo-elements are invisible
 * to React.
 *
 * The class chain below is Documenso v2.18.0's auth/card layout
 * (`relative flex min-h-screen flex-col items-center justify-center
 *   overflow-hidden px-4 py-12 md:p-12 lg:p-24`). It deliberately does not
 * match the signed-in app shell, which already renders the WagerOS branding
 * logo in its header. If an upgrade changes those utility classes the mark
 * simply stops appearing — re-check this selector after `docker compose pull`.
 */
main.min-h-screen.flex-col.items-center.justify-center.overflow-hidden::before {
  content: '';
  display: block;
  flex: none;
  width: 168px;
  height: 47px;
  margin-bottom: 2rem;
  background-image: url('/wageros/logo.png');
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
}

@media (max-width: 480px) {
  main.min-h-screen.flex-col.items-center.justify-center.overflow-hidden::before {
    width: 140px;
    height: 39px;
    margin-bottom: 1.5rem;
  }
}

/* The navy wordmark needs to lighten on Documenso's dark theme. */
.dark:not(.dark-mode-disabled) main.min-h-screen.flex-col.items-center.justify-center.overflow-hidden::before {
  filter: invert(1) brightness(2);
}

/* ===========================================================================
 * IN-APP BRANDING (2026-09-13) — the signed-in shell and the signer's chrome.
 *
 * The block above only covers the UNAUTHENTICATED layouts. Everything below
 * covers what a logged-in user or a signer sees. See README §4a.
 *
 * Text is NOT handled here: user-visible "Documenso" strings live in the
 * compiled JS chunks (i18n catalogues, the app-header component) and are
 * rewritten by brand/patch-assets.sh. CSS carries the visuals only.
 * ======================================================================== */

/* --- Documenso wordmark in the signed-in header ------------------------------
 * Two places render it, both as the SAME inline SVG, identified by its unique
 * viewBox ("0 0 2248 320" — the gear + "Documenso" lockup, 7.03:1):
 *   1. the app shell header:  [data-testid="app-header-container"] > a > svg
 *   2. the envelope editor's own nav bar:  nav > … > a[href="/"] > svg
 * A third place, the mobile navigation sheet, uses a hashed PNG instead:
 *   a > img[src^="/assets/logo-"]  (alt="Documenso Logo" pre-patch)
 *
 * All three are handled the same way: the Documenso mark is removed and the
 * WagerOS wordmark is painted as a background on the PARENT ANCHOR, which keeps
 * the link clickable and correctly sized. The anchor needs an explicit box
 * because its only child is now display:none.
 *
 * Matching the parent needs :has(); if a browser lacks it the mark simply does
 * not render (graceful — the link still works). Sizes preserve logo.png's
 * 725x202 aspect ratio at the header's own mark height (h-6 = 24px desktop,
 * 25px in the sheet).
 *
 * The img selector keys on `src`, not on `alt`: patch-assets.sh rewrites the
 * alt text to "WagerOS Agreements Logo", so an alt-based selector would break
 * the moment the patch is applied. */
a:has(> svg[viewBox='0 0 2248 320']) {
  display: inline-block !important;
  width: 87px;
  height: 24px;
  background-image: url('/wageros/logo.png');
  background-repeat: no-repeat;
  background-position: left center;
  background-size: contain;
}

a:has(> img[src^='/assets/logo-']) {
  display: inline-block !important;
  width: 90px;
  height: 25px;
  background-image: url('/wageros/logo.png');
  background-repeat: no-repeat;
  background-position: left center;
  background-size: contain;
}

svg[viewBox='0 0 2248 320'],
img[src^='/assets/logo-'] {
  display: none !important;
}

.dark:not(.dark-mode-disabled) a:has(> svg[viewBox='0 0 2248 320']),
.dark:not(.dark-mode-disabled) a:has(> img[src^='/assets/logo-']) {
  filter: invert(1) brightness(2);
}

/* --- empty-state bird mascot -> neutral document glyph -----------------------
 * Documenso draws a lucide "bird" in the documents and templates empty states.
 * lucide icons stroke with `stroke="currentColor"`, so `color: transparent`
 * erases the bird without touching layout, and the replacement glyph is painted
 * as a background on the same <svg> box (an SVG root is a replaced element, so
 * `background-image` applies). Keeping the element means the empty state keeps
 * its spacing and its `h-12 w-12` sizing. */
svg.lucide-bird {
  color: transparent !important;
  background-image: url('/wageros/doc-icon.svg');
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
}

/* --- no outbound links to Documenso's own channels ---------------------------
 * Every one of these is rendered client-side by React, so the proxy's HTML
 * rewriter never sees them; CSS is the only layer that can reach them.
 * Covers: the "Powered by Documenso" corner badge on signing pages
 * (documenso.com), "Check out Documenso" on a spent signing link, the
 * enterprise-licence "Learn more" in /admin, and the Documentation / Discord
 * cards on the org support page (docs.documenso.com, documen.so/discord).
 * The support ROUTE is separately redirected in the Caddyfile so a typed URL
 * cannot reach it either. */
a[href*='documenso.com'],
a[href*='documen.so'],
a[href*='documenso.dev'] {
  display: none !important;
}

/* The account menu's "Support" entry points at Documenso's own support-channel
 * page (/o/<orgUrl>/support). Scoped to that route's two href shapes so an
 * unrelated future WagerOS link containing "support" is not caught. */
a[href$='/support'],
a[href*='/support?'] {
  display: none !important;
}

/* --- lime that is compiled to a literal inside `group-hover:` variants -------
 * Same problem as the [class~='bg-documenso'] block above, but these only paint
 * while an ancestor `.group` is hovered, so the rule has to be scoped that way
 * rather than applied unconditionally. Seen on the envelope editor's
 * "Add a document" drop-zone illustration. */
.group:hover [class~='group-hover:bg-documenso'] {
  background-color: hsl(var(--primary)) !important;
}

.group:hover [class~='group-hover:text-documenso'] {
  color: hsl(var(--primary)) !important;
}

.group:hover [class~='group-hover:border-documenso/80'] {
  border-color: hsl(var(--primary) / 0.8) !important;
}

/* ===========================================================================
 * SIGNER EVIDENCE & CONTROLS (README §4b) — styles for the elements that
 * brand/wageros-signer.js injects, plus the two body-class-driven behaviours.
 *
 * All of it lives HERE rather than in the script because the app's CSP is
 * `style-src-elem 'self' 'nonce-…'`: a stylesheet this proxy injects as a file
 * is allowed ('self'), a <style> block the script writes at runtime is not.
 * The script therefore only toggles classes on <body> and sets the one
 * per-signer value (the watermark image) through CSSOM.
 *
 * Every element is a direct child of <body>, outside the React root, so React
 * cannot reconcile it away.
 * ======================================================================== */

/* --- sender: "WagerOS Signer Controls" card (document detail + editor) ----- */
/* Bottom-LEFT, and collapsed by default: Documenso's editor keeps its own
 * primary actions ("Add Fields", "Send Document") in the bottom-right, and a
 * fixed panel there swallows the clicks meant for them. */
#wageros-signer-controls {
  position: fixed !important;
  left: 16px;
  bottom: 16px;
  z-index: 2147483000;
  width: 250px;
  padding: 12px 14px;
  border: 1px solid #cffafe;
  border-radius: 10px;
  background: #ffffff;
  box-shadow: 0 8px 24px rgb(15 23 42 / 14%);
  font: 13px/1.45 -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  color: #1f2937;
}

#wageros-signer-controls .wageros-card-title {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  margin: 0 0 8px;
  padding: 0 0 6px;
  border: 0;
  border-bottom: 1px solid #e5e7eb;
  background: none;
  font: inherit;
  font-weight: 600;
  font-size: 13px;
  color: #0f172a;
  text-align: left;
  cursor: pointer;
}

#wageros-signer-controls .wageros-card-caret {
  margin-left: auto;
  width: 0;
  height: 0;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-bottom: 5px solid #6b7280;
}

/* Collapsed: a compact launcher that covers almost nothing. */
#wageros-signer-controls.wageros-collapsed {
  width: auto;
  padding: 7px 11px;
}

#wageros-signer-controls.wageros-collapsed .wageros-card-title {
  margin: 0;
  padding: 0;
  border-bottom: 0;
}

#wageros-signer-controls.wageros-collapsed .wageros-card-caret {
  border-bottom: 0;
  border-top: 5px solid #6b7280;
}

#wageros-signer-controls.wageros-collapsed .wageros-card-body {
  display: none;
}

#wageros-signer-controls .wageros-card-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 3px 0;
  cursor: pointer;
}

#wageros-signer-controls .wageros-card-row input {
  margin: 2px 0 0;
  accent-color: #06b6d4;
}

#wageros-signer-controls .wageros-card-link {
  display: inline-block;
  margin-top: 8px;
  color: #0e7490;
  font-weight: 600;
  text-decoration: none;
}

#wageros-signer-controls .wageros-card-status {
  margin-top: 6px;
  font-size: 11px;
  color: #6b7280;
}

@media (max-width: 640px) {
  #wageros-signer-controls {
    left: 8px;
    bottom: 8px;
    width: 210px;
  }
}

/* --- signer: the gate ------------------------------------------------------
 * The overlay is a real element sitting on top of the sign/complete button.
 * `disabled` alone would be undone by React's next render; a transparent
 * sibling that swallows the click cannot be. Positioned by the script from the
 * button's own bounding box and repositioned on scroll/resize. */
.wageros-gate-overlay {
  position: fixed;
  z-index: 2147483001;
  background: rgb(255 255 255 / 1%);
  border-radius: 6px;
  cursor: not-allowed;
}

/* Progress / requirement note, bottom-centre so it cannot cover the widget. */
#wageros-signer-note {
  position: fixed !important;
  left: 50%;
  bottom: 14px;
  transform: translateX(-50%);
  z-index: 2147483002;
  max-width: min(560px, calc(100vw - 24px));
  padding: 10px 14px;
  border: 1px solid #a5f3fc;
  border-radius: 999px;
  background: #ecfeff;
  color: #0f172a;
  font: 13px/1.4 -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  text-align: center;
  box-shadow: 0 6px 20px rgb(15 23 42 / 12%);
}

@media (max-width: 640px) {
  #wageros-signer-note {
    border-radius: 12px;
  }
}

.wageros-note-bump {
  animation: wageros-note-bump 420ms ease-out 1;
}

@keyframes wageros-note-bump {
  0% { box-shadow: 0 0 0 0 rgb(6 182 212 / 55%); }
  100% { box-shadow: 0 0 0 14px rgb(6 182 212 / 0%); }
}

@media (prefers-reduced-motion: reduce) {
  .wageros-note-bump { animation: none; }
}

/* --- signer: per-signer watermark -----------------------------------------
 * A fixed, non-interactive overlay whose background is an SVG data URI built
 * by the script from the recipient's own name/email and the date, tiled
 * diagonally. The PDF bytes are never touched: this is screen furniture only,
 * so a screenshot carries the signer's identity while the sealed document
 * stays byte-identical. */
.wageros-watermark {
  position: fixed;
  inset: 0;
  z-index: 2147482000;
  pointer-events: none;
  opacity: 0.13;
  background-repeat: repeat;
  background-position: 0 0;
}

/* --- signer: anti-copy deterrents ----------------------------------------- */
body.wageros-anticopy .embed--DocumentContainer,
body.wageros-anticopy .embed--DocumentViewer,
body.wageros-anticopy .react-pdf__Page {
  user-select: none !important;
  -webkit-user-select: none !important;
  -webkit-touch-callout: none !important;
}

.wageros-print-notice {
  display: none;
}

@media print {
  body.wageros-anticopy {
    visibility: hidden;
  }

  body.wageros-anticopy .wageros-print-notice {
    display: block;
    visibility: visible;
    position: fixed;
    inset: 0;
    padding: 25vh 12% 0;
    font: 14px/1.6 Helvetica, Arial, sans-serif;
    color: #0f172a;
    text-align: center;
  }
}
