.ui-image-compare {
  --_text: var(--ui-text, #d6d6d6);
  --_dim: var(--ui-text-dim, #8a8a8a);
  --_line: var(--ui-line, #3a3a3a);
  /* Divider and grip: white, 70% transparent. */
  --_divider: rgba(255, 255, 255, .3);

  display: inline-flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  user-select: none;
}

/* Bar above the image: the split controls on the left, the view modes on the right. */
.ui-image-compare__bar {
  position: relative;
  display: flex;
  align-items: center;
  min-height: 24px;
  font-size: 12px;
}

.ui-image-compare__sides {
  display: flex;
  align-items: center;
  gap: 8px;
}

.ui-image-compare__side {
  padding: 0;
  font: inherit;
  color: var(--_dim);
  background: none;
  border: 0;
  cursor: pointer;
}

.ui-image-compare__side:hover,
.ui-image-compare[data-side="before"] [data-side="before"],
.ui-image-compare[data-side="after"] [data-side="after"] {
  color: var(--_text);
}

/* Switch: the knob is left for "before", right for "after", in the middle while both are visible. */
.ui-image-compare__switch {
  position: relative;
  width: 30px;
  height: 14px;
  padding: 0;
  background: var(--ui-field, #161616);
  border: 1px solid var(--_line);
  border-radius: 7px;
  cursor: pointer;
}

.ui-image-compare__switch::after {
  content: '';
  position: absolute;
  top: 2px;
  left: calc(50% - 4px);
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--ui-image-compare-knob, #d6d6d6);
}

.ui-image-compare[data-side="before"] .ui-image-compare__switch::after {
  left: 2px;
}

.ui-image-compare[data-side="after"] .ui-image-compare__switch::after {
  left: calc(100% - 10px);
}

/* Labels of the halves: each one starts over its half. */
.ui-image-compare__half-label {
  position: absolute;
  left: 0;
  display: none;
  color: var(--_text);
}

.ui-image-compare__half-label.is-after {
  left: 50%;
}

.ui-image-compare__modes {
  display: flex;
  align-items: center;
  gap: 2px;
  margin-left: auto;
}

.ui-image-compare__modes-label {
  margin-right: 6px;
  color: var(--_dim);
}

.ui-image-compare__mode {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 24px;
  padding: 0;
  color: var(--_dim);
  background: none;
  border: 0;
  border-radius: 3px;
  cursor: pointer;
}

.ui-image-compare__mode:hover {
  color: var(--_text);
  background: var(--ui-image-compare-mode-hover, #383838);
}

.ui-image-compare__mode[aria-pressed="true"] {
  color: var(--_text);
  background: var(--ui-image-compare-mode-active, #3a3a3a);
}

.ui-image-compare__mode svg {
  display: block;
}

.ui-image-compare button:focus-visible {
  outline: 1px solid var(--ui-focus, #3d7fd9);
  outline-offset: 1px;
}

.ui-image-compare__stage {
  position: relative;
  overflow: hidden;
  background: var(--ui-surround, #2a2a2a);
}

.ui-image-compare__stage:focus {
  outline: none;
}

/* Inset: the stage clips what lies outside it. */
.ui-image-compare__stage:focus-visible {
  box-shadow: inset 0 0 0 1px var(--ui-focus, #3d7fd9);
}

.ui-image-compare__pane {
  position: absolute;
  inset: 0;
  overflow: hidden;
}

.ui-image-compare canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.ui-image-compare__pane.is-after {
  clip-path: inset(0 0 0 var(--ui-split, 50%));
}

.ui-image-compare__divider {
  position: absolute;
  top: 0;
  bottom: 0;
  left: var(--ui-split, 50%);
  width: 28px;
  margin-left: -14px;
  color: var(--_divider);
  cursor: ew-resize;
  touch-action: none;
}

.ui-image-compare__divider::before {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: 13px;
  width: 2px;
  background: currentColor;
}

.ui-image-compare__divider:hover {
  --_divider: rgba(255, 255, 255, .8);
}

.ui-image-compare__grip {
  position: absolute;
  top: 50%;
  /* The grip is 32 px wide, the divider 28 px: 2 px out on each side keep it centered on the line. */
  left: -2px;
  display: block;
  transform: translateY(-50%);
}

/* Side by side: two halves, the image is as wide as the whole area and moves with --ui-pan. */
.ui-image-compare[data-mode="side-by-side"] .ui-image-compare__sides,
.ui-image-compare[data-mode="side-by-side"] .ui-image-compare__divider {
  display: none;
}

.ui-image-compare[data-mode="side-by-side"] .ui-image-compare__half-label {
  display: block;
}

.ui-image-compare[data-mode="side-by-side"] .ui-image-compare__stage {
  cursor: grab;
  touch-action: none;
}

.ui-image-compare.is-panning .ui-image-compare__stage {
  cursor: grabbing;
}

.ui-image-compare[data-mode="side-by-side"] .ui-image-compare__pane {
  right: auto;
  width: 50%;
}

.ui-image-compare[data-mode="side-by-side"] .ui-image-compare__pane.is-after {
  left: 50%;
  clip-path: none;
}

/* Line between the halves, over the images: a border of the pane would move its image. */
.ui-image-compare[data-mode="side-by-side"] .ui-image-compare__stage::after {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 1px;
  background: var(--_divider);
}

.ui-image-compare[data-mode="side-by-side"] canvas {
  width: 200%;
  transform: translateX(var(--ui-pan, 0));
}
