:root {
  --ui-paper-texture: url("../assets/ui/paper.svg");
  --ui-paper-tile: 144px;
}

/* Mask the decoration only so labels and focus indicators can extend past it. */
:where(.paper-surface:not(dialog)) {
  position: relative;
}

.paper-surface:has(> .paper-skin),
.button.paper-surface:has(> .paper-skin),
.difficulty-switch button.paper-surface:has(> .paper-skin) {
  isolation: isolate;
  border-color: transparent;
  background: transparent;
  box-shadow: none;
}

.paper-surface > .paper-skin {
  --brush-width: 24px;
  display: block;
  position: absolute;
  z-index: -1;
  inset: -1px -3px;
  overflow: hidden;
  border-radius: inherit;
  background: var(--surface-paint, var(--panel));
  mask-image: url("../assets/ui/brush-left.svg"), linear-gradient(#000, #000), url("../assets/ui/brush-right.svg");
  mask-position: left, center, right;
  mask-size: var(--brush-width) 100%, calc(100% - 2 * var(--brush-width) + 2px) 100%, var(--brush-width) 100%;
  mask-repeat: no-repeat;
  pointer-events: none;
}

.paper-skin::before {
  position: absolute;
  inset: -24% -12%;
  background: var(--ui-paper-texture) 0 0 / var(--ui-paper-tile) var(--ui-paper-tile) repeat;
  transform: rotate(-3deg);
  content: "";
}

.site-header.paper-surface > .paper-skin {
  inset: 0 0 -8px;
  border-radius: 0;
  mask-image: linear-gradient(#000, #000), url("../assets/ui/brush-bottom.svg");
  mask-position: top, bottom;
  mask-size: 100% calc(100% - 17px), 240px 18px;
  mask-repeat: no-repeat, repeat-x;
  opacity: .95;
}

/* A wide, shallow header needs extra vertical bleed for the rotated tile. */
.site-header > .paper-skin::before {
  inset-block: min(-24%, -4vw);
}

:is(.difficulty-switch, .difficulty-switch button, .button, .control-play).paper-surface > .paper-skin {
  --brush-width: 14px;
  inset-inline: -2px;
}

/* Colored paint keeps the same rough edge and a faint impression of the tile. */
:is(.button-primary, .control-play, [data-difficulty]).paper-surface > .paper-skin {
  --surface-paint: var(--accent);
  filter: none;
}

:is(.button-primary, .control-play, [data-difficulty]).paper-surface > .paper-skin::before {
  opacity: .1;
  mix-blend-mode: soft-light;
}

:is(.button-primary, .control-play, [data-difficulty]).paper-surface:enabled:hover > .paper-skin {
  --surface-paint: var(--accent-strong);
}

.difficulty-switch button[aria-pressed="false"] > .paper-skin {
  display: none;
}

/* The user's complete play/pause drawing already supplies its painted shape. */
.control-play:not([data-playing="true"]):has(> .play-icon.has-art) > .paper-skin,
.control-play[data-playing="true"]:has(> .pause-icon.has-art) > .paper-skin {
  display: none;
}

.difficulty-switch.paper-surface > .paper-skin,
.button.paper-surface > .paper-skin {
  filter: brightness(.975) saturate(1.08);
}

.paper-surface.paper-round > .paper-skin {
  inset: 0;
  mask-image: none;
}

.paper-round,
.guest-scores li,
.guest-happiness,
.tool-slot,
.arrow-icon .piece-symbol,
.gap-control::before {
  background-image: var(--ui-paper-texture);
  background-size: var(--ui-paper-tile) var(--ui-paper-tile);
}

.button.paper-surface:enabled:hover > .paper-skin {
  filter: brightness(.96) saturate(1.08);
}

.button.paper-surface:enabled:active > .paper-skin {
  filter: brightness(.91) saturate(1.12);
}

@media (prefers-reduced-motion: no-preference) {
  .button > .paper-skin {
    transition: filter 120ms ease;
  }
}
