/* If the root background isn't set, it defaults to transparent, which acts as
 * an uninvertable white. The :where pseudo-class has 0 specificity, so these
 * rules will be overwritten by absolutely anything that sets the root
 * background color. */
:where(:root[hc^="delumine"]:not([hc*="dynamic"][looks-dark]) body) {
  background-color: white;
}
:where(:root[hc^="delumine"][hc*="dynamic"][looks-dark] body) {
  background-color: black;
}
:where(:root[hc^="noinvert"]:not([hc*="kill_background"]) body) {
  background-color: white;
}
:where(:root[hc^="noinvert"][hc*="kill_background"] body) {
  background-color: black;
}

/* Invert the luminance iff any of the "delumine" styles are selected. */
html[hc^="delumine"] {
  filter: invert(100%) hue-rotate(180deg);
}
html[hc^="delumine"][hc*="dynamic"][looks-dark] {
  filter: none;
}
html[hc^="delumine"][hc*="kill_background"]:not([hc*="dynamic"][looks-dark]),
html[hc^="delumine"][hc*="kill_background"]:not([hc*="dynamic"][looks-dark]) body {
  background-image: none !important;
  background-color: #FFF !important;
  color: #000 !important;
}
html[hc^="delumine"][hc*="kill_background"][hc*="dynamic"][looks-dark],
html[hc^="delumine"][hc*="kill_background"][hc*="dynamic"][looks-dark] body,
html[hc^="noinvert"][hc*="kill_background"],
html[hc^="noinvert"][hc*="kill_background"] body {
  background-image: none !important;
  background-color: #000 !important;
  color: #FFF !important;
}
html[hc*="delumine"] #deluminate_backdrop {
  background: white !important;
}

/* Override the above with a low-contrast version if selected. */
html[hc^="delumine"][hc*="low-contrast"] {
  filter: invert(92%) hue-rotate(180deg);
}

/* Lower the contrast without inverting the luminance. */
html[hc^="noinvert-low-contrast"] {
  filter: contrast(85%);
}

/* Lower the brightness without inverting the luminance. */
html[hc^="noinvert-dim1"] {
  filter: brightness(90%);
}
html[hc^="noinvert-dim2"] {
  filter: brightness(80%);
}
html[hc^="noinvert-dim3"] {
  filter: brightness(70%);
}
html[hc^="noinvert-dim4"] {
  filter: brightness(60%);
}
html[hc^="noinvert-dim5"] {
  filter: brightness(50%);
}

/* The low-contrast version of the above. */
html[hc^="noinvert-dim1"][hc*="low-contrast"] {
  filter: contrast(85%) brightness(90%);
}
html[hc^="noinvert-dim2"][hc*="low-contrast"] {
  filter: contrast(85%) brightness(80%);
}
html[hc^="noinvert-dim3"][hc*="low-contrast"] {
  filter: contrast(85%) brightness(70%);
}
html[hc^="noinvert-dim4"][hc*="low-contrast"] {
  filter: contrast(85%) brightness(60%);
}
html[hc^="noinvert-dim5"][hc*="low-contrast"] {
  filter: contrast(85%) brightness(50%);
}

/* Uninvert select items within the document. */

/* For the -noimg variant, uninvert all images. */
html[hc*="delumine-noimg"]:not([hc*="dynamic"][looks-dark]) :-webkit-any(
img,
/* Check for "url" in the style attribute, which is usually a fancy way to specify an image */
[style*="url"]:not([style*="transparent"]),
[deluminate_imageType],
canvas,
embed,
object,
video) {
  filter: hue-rotate(180deg) invert(100%);
}
/* Avoid performing the uninversion process on anything that is a descendant of
 * something uninverted. */
html[hc*="delumine-noimg"]:not([hc*="dynamic"][looks-dark]) :-webkit-any(
img,
[style*="url"]:not([style*="transparent"]),
[deluminate_imageType],
canvas,
embed,
object,
video) :-webkit-any(
img,
[style*="url"]:not([style*="transparent"]),
[deluminate_imageType],
canvas,
embed,
object,
video) {
  filter: none;
}
/* Reinvert iframes inside tags with background-images specified. */
html[hc*="delumine-noimg"]:not([hc*="dynamic"][looks-dark]) :-webkit-any(
img,
/* Check for "url" in the style attribute, which is usually a fancy way to specify an image */
[style*="url"]:not([style*="transparent"]),
[deluminate_imageType],
canvas,
embed,
object,
video) iframe {
  filter: hue-rotate(180deg) invert(100%);
}

/* The "smart" filter is more selective about what to uninvert. */
html[hc*="delumine-smart"]:not([hc*="dynamic"][looks-dark]) body :-webkit-any(
	/* Leave PNG and GIF format images inverted, since they usually are
	 * identifiable even when inverted. */
	img:not([src$=".png"]):not([src$=".gif"]):not([src$=".PNG"]):not([src$=".GIF"]):not([src$=".svg"]):not([src$=".SVG"]):not([src^="data:image"]):not([src^="blob:"]):not([aria-hidden="true"][alt]:not([alt=""])),
	[itemprop="photo"],
	[itemprop="image"],
	[role="img"],
	[data-src],
	[data-canonical-src],
	[src^="data:image/jpeg"],
	[deluminate_imageType="jpg"],
	[deluminate_imageType="webp"],
	[deluminate_imageType="animated gif"],
	[style*=".jpg"],
	[style*=".JPG"],
	[style*=".jpeg"],
	[style*=".JPEG"],
	[style*=".webp"],
	[style*=".WEBP"],
	/* It's usually not desireable to invert these tags. */
	embed:not([type$="pdf"]),
	object,
	video) {
  filter: hue-rotate(180deg) invert(100%);
}
/* Avoid performing the uninversion process on anything that is a descendant of
 * something uninverted. */
html[hc*="delumine-smart"]:not([hc*="dynamic"][looks-dark]) body :-webkit-any(
	img:not([src$=".png"]):not([src$=".gif"]):not([src$=".PNG"]):not([src$=".GIF"]):not([src^="data:image"]):not([src^="blob:"]):not([aria-hidden="true"][alt]:not([alt=""])),
	[itemprop="photo"],
	[itemprop="image"],
	[role="img"],
	[data-src],
	[data-canonical-src],
	[src^="data:image/jpeg"],
	[deluminate_imageType="jpg"],
	[deluminate_imageType="webp"],
	[style*=".jpg"],
	[style*=".JPG"],
	[style*=".jpeg"],
	[style*=".JPEG"],
	[style*=".webp"],
	[style*=".WEBP"],
	embed:not([type$="pdf"]),
	object,
	video) :-webkit-any(
	img:not([src$=".png"]):not([src$=".gif"]):not([src$=".PNG"]):not([src$=".GIF"]):not([src^="data:image"]):not([src^="blob:"]):not([aria-hidden="true"][alt]:not([alt=""])),
	[itemprop="photo"],
	[itemprop="image"],
	[role="img"],
	[data-src],
	[data-canonical-src],
	[src^="data:image/jpeg"],
	[deluminate_imageType="jpg"],
	[deluminate_imageType="webp"],
	[style*=".jpg"],
	[style*=".JPG"],
	[style*=".jpeg"],
	[style*=".JPEG"],
	[style*=".webp"],
	[style*=".WEBP"],
	embed:not([type$="pdf"]),
	object,
	video) {
  filter: none;
}
/* Reinvert iframes inside tags with background-images specified. */
html[hc*="delumine-smart"]:not([hc*="dynamic"][looks-dark]) body :-webkit-any(
	img:not([src$=".png"]):not([src$=".gif"]):not([src$=".PNG"]):not([src$=".GIF"]):not([src^="data:image"]):not([src^="blob:"]):not([aria-hidden="true"][alt]:not([alt=""])),
	[itemprop="photo"],
	[itemprop="image"],
	[role="img"],
	[data-src],
	[data-canonical-src],
	[src^="data:image/jpeg"],
	[deluminate_imageType="jpg"],
	[deluminate_imageType="webp"],
	[style*=".jpg"],
	[style*=".JPG"],
	[style*=".jpeg"],
	[style*=".JPEG"],
	[style*=".webp"],
	[style*=".WEBP"],
	embed:not([type$="pdf"]),
	object,
	video) iframe {
  filter: hue-rotate(180deg) invert(100%);
}

/* Force text to plain black text on white (which, when inverted, is the
 * preferable white text on black. */
html[hc*="delumine"][hc*="force_text"] input[type="text"] {
  background-image: none !important;
  background-color: white !important;
  color: black !important;
}

/* ...unless it's uninverted because of a background image. Then set it
 * directly to white text on black. */
html[hc*="delumine-smart"][hc*="force_text"] input[type="text"]:-webkit-any(
	[src^="data:image/jpeg"],
	[deluminate_imageType="jpg"],
	[deluminate_imageType="webp"],
	[style*=".jpg"],
	[style*=".JPG"],
	[style*=".jpeg"],
	[style*=".JPEG"],
	[style*=".webp"],
	[style*=".WEBP"]) {
  background-image: none !important;
  background-color: black !important;
  color: white !important;
}

html[hc*="delumine-noimg"][hc*="force_text"] input[style*="url"]:-webkit-any(
  [type="text"], [dir]):not([style*="transparent"]) {
  background-image: none !important;
  background-color: black !important;
  color: white !important;
}

/* Crude workaround for my inability to inject deluminate into preview iframes
 * on the new tab page. */
html[hc*="delumine-noimg"] iframe[src^="chrome-search://most-visited"],
html[hc*="delumine-smart"] iframe[src^="chrome-search://most-visited"] {
  filter: hue-rotate(180deg) invert(100%);
}

/* Stupid workaround just for Medium's stupid canvas which, as far as I can
 * tell, has no value whatsoever. */
html[hc*="delumine-noimg"] body canvas.canvas-renderer,
html[hc*="delumine-smart"] body canvas.canvas-renderer {
  filter: none !important;
}
