/**
 * Animated outline button.
 *
 * Built on the `.button_box` interaction from operaticagency.com, with the two
 * states swapped: at rest the outline is a single dash sitting part-way around
 * the perimeter, and on hover it travels back to close into a solid loop.
 *
 * The outline is an inline <svg><rect> injected into the button markup by
 * homewood_theme_button_frame() in functions.php — CSS alone cannot animate a
 * dash around a rounded border. The rect carries pathLength="100", so every
 * dash value below is a plain percentage of the perimeter and stays correct at
 * any button size.
 *
 * @package homewood_theme
 */

.wp-block-button .wp-block-button__link {
	/* Recolour by overriding these on any ancestor. */
	--hw-btn-ink: var(--wp--preset--color--ink, #252b23);
	--hw-btn-stroke: var(--wp--preset--color--bronze, #896d44);

	--hw-btn-radius: 25px;
	--hw-btn-stroke-width: 2;
	--hw-btn-speed: 0.6s;

	/* Percentages of the outline's perimeter. */
	--hw-btn-dash: 29;   /* length of the dash shown at rest */
	--hw-btn-gap: 81;    /* empty space that follows it */
	--hw-btn-travel: 57; /* where it starts; it unwinds to 0 on hover */

	position: relative;
	z-index: 0;
	display: inline-flex;
	align-items: center;
	justify-content: center;
	min-width: 280px;
	max-width: 100%;
	min-height: 50px;
	background: transparent;
	color: var(--hw-btn-ink);
	border: 0;
	border-radius: var(--hw-btn-radius);
	text-align: center;
	text-decoration: none;
	transition: color var(--hw-btn-speed) ease;
}

.wp-block-button .wp-block-button__link:hover,
.wp-block-button .wp-block-button__link:focus,
.wp-block-button .wp-block-button__link:active {
	background: transparent;
	color: var(--hw-btn-ink);
	text-decoration: none;
}

/* The block's own width settings must win over min-width. */
.wp-block-button[class*="wp-block-button__width-"] .wp-block-button__link {
	min-width: 0;
	width: 100%;
}

/* -- The animated outline ------------------------------------------------- */

.hw-button-outline__frame {
	position: absolute;
	inset: 0;
	width: 100%;
	height: 100%;
	overflow: visible;
	pointer-events: none;
}

.hw-button-outline__frame rect {
	/* x/y/rx also ship as attributes so the shape survives without CSS. */
	width: calc(100% - 2px);
	height: calc(100% - 2px);
	rx: var(--hw-btn-radius);
	fill: none;
	stroke: var(--hw-btn-stroke);
	stroke-width: var(--hw-btn-stroke-width);
	stroke-dasharray: var(--hw-btn-dash) var(--hw-btn-gap);
	stroke-dashoffset: var(--hw-btn-travel);
	transition:
		stroke-dasharray var(--hw-btn-speed) ease,
		stroke-dashoffset var(--hw-btn-speed) ease,
		stroke var(--hw-btn-speed) ease;
}

.wp-block-button .wp-block-button__link:hover .hw-button-outline__frame rect,
.wp-block-button .wp-block-button__link:focus-visible .hw-button-outline__frame rect {
	stroke-dasharray: 100 0;
	stroke-dashoffset: 0;
}

.hw-button-outline__label {
	position: relative;
}

/* -- Focus ---------------------------------------------------------------- */

/*
 * A closing outline is decorative, not a focus indicator, so keep a real ring
 * as well.
 */
.wp-block-button .wp-block-button__link:focus-visible {
	outline: 2px solid var(--hw-btn-ink);
	outline-offset: 4px;
}

/* -- Buttons on dark sections --------------------------------------------- */

.has-ink-background-color .wp-block-button .wp-block-button__link,
.has-forest-background-color .wp-block-button .wp-block-button__link,
.has-sage-background-color .wp-block-button .wp-block-button__link {
	--hw-btn-ink: var(--wp--preset--color--cream, #f6f1ea);
	--hw-btn-stroke: var(--wp--preset--color--wheat, #c3a87f);
}

/* -- Fallback: editor canvas, and any button rendered without the SVG ------ */

/*
 * A CSS border cannot be drawn part-way round, so the resting dash is stood in
 * for by a faded outline that comes up to full strength on hover.
 */
.wp-block-button .wp-block-button__link:not(:has(.hw-button-outline__frame)) {
	border: calc(var(--hw-btn-stroke-width) * 1px) solid
		color-mix(in srgb, var(--hw-btn-stroke) 45%, transparent);
	transition:
		border-color var(--hw-btn-speed) ease,
		color var(--hw-btn-speed) ease;
}

.wp-block-button .wp-block-button__link:not(:has(.hw-button-outline__frame)):hover,
.wp-block-button .wp-block-button__link:not(:has(.hw-button-outline__frame)):focus-visible {
	border-color: var(--hw-btn-stroke);
}

/* -- Reduced motion ------------------------------------------------------- */

/*
 * Without the transition the outline still snaps closed on hover, but a fill
 * makes the change unmistakable when nothing is allowed to move.
 */
@media (prefers-reduced-motion: reduce) {
	.hw-button-outline__frame rect {
		transition: none;
	}

	.wp-block-button .wp-block-button__link:hover .hw-button-outline__frame rect,
	.wp-block-button .wp-block-button__link:focus-visible .hw-button-outline__frame rect {
		stroke-dasharray: 100 0;
		stroke-dashoffset: 0;
		fill: var(--hw-btn-stroke);
	}

	.wp-block-button .wp-block-button__link {
		transition: none;
	}

	.wp-block-button .wp-block-button__link:hover,
	.wp-block-button .wp-block-button__link:focus-visible {
		color: var(--wp--preset--color--cream, #f6f1ea);
	}
}
