/* ── wpimage/compressor ───────────────────────────────────────────────────────
   Ported from the prototype's "upload tool" rules. Values are the original's.

   Everything is scoped under .wpi-compressor, which is what lets the inner class
   names stay as the prototype wrote them (.dropzone, .result, .layer, .tag …).
   Unscoped, names that generic would collide with the theme within a release or
   two. The design tokens are declared on the root here rather than :root for the
   same reason — Brandy owns some of these names globally.                      */

.wpi-compressor {
	--wpi-accent: #3858e9;
	--wpi-accent-dark: #2145e6;
	--wpi-accent-darker: #183ad6;
	--wpi-accent-tint: #eef1fd;

	--wpi-surface-muted: #f6f7f7;
	--wpi-border: #e0e0e0;
	--wpi-border-strong: #949494;
	--wpi-fg: #1e1e1e;
	--wpi-fg-muted: #757575;
	--wpi-gray-100: #f0f0f0;
	--wpi-gray-200: #e0e0e0;
	--wpi-gray-300: #dddddd;
	--wpi-gray-400: #cccccc;
	--wpi-nt-flame: #fc794c;
	--wpi-alert-green: #4ab866;

	--wpi-radius-sm: 2px;
	--wpi-radius-md: 4px;
	--wpi-card-radius: 8px;

	--wpi-elevation-small: 0px 1px 2px rgba(0, 0, 0, 0.04), 0px 3px 3px rgba(0, 0, 0, 0.03),
		0px 6px 4px rgba(0, 0, 0, 0.02), 0px 11px 5px rgba(0, 0, 0, 0.01);
	--wpi-elevation-medium: 0px 2px 3px rgba(0, 0, 0, 0.05), 0px 6px 6px rgba(0, 0, 0, 0.04),
		0px 13px 8px rgba(0, 0, 0, 0.02), 0px 24px 10px rgba(0, 0, 0, 0.01);

	--wpi-font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
		Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;

	/* Divider position for the before/after stage; view.js writes this. */
	--wpi-pos: 50%;

	font-family: var(--wpi-font-sans);
}

.wpi-compressor *,
.wpi-compressor *::before,
.wpi-compressor *::after {
	box-sizing: border-box;
}

/* Inline nt-ic icons: the <i> is the sized box the 100%-sized SVG resolves
   against. Without a size here the SVG collapses to 0x0 and never paints. */
.wpi-compressor .nt-ic {
	display: inline-block;
	width: 24px;
	height: 24px;
	flex: none;
	line-height: 0;
}

.wpi-compressor .nt-ic svg {
	display: block;
}

/* ---------- stage visibility ------------------------------------------------
   All four stages are in the DOM; data-stage picks one. Written as "hide unless"
   so that with JS off the root keeps its server-rendered data-stage="idle" and
   the block still shows a sensible dropzone.                                   */
.wpi-compressor .dropzone.is-preview,
.wpi-compressor .tool-progress,
.wpi-compressor .result {
	display: none;
}

.wpi-compressor[data-stage="idle"] .dropzone.is-preview {
	display: none;
}

.wpi-compressor:not([data-stage="idle"]) .dropzone:not(.is-preview) {
	display: none;
}

.wpi-compressor:not([data-stage="idle"]) .dropzone.is-preview {
	display: block;
}

.wpi-compressor[data-stage="working"] .tool-progress {
	display: block;
}

.wpi-compressor[data-stage="done"] .result {
	display: block;
}

/* Controls and the CTA are present until the result replaces them. */
.wpi-compressor[data-stage="done"] .tool-controls,
.wpi-compressor[data-stage="done"] .tool-cta {
	display: none;
}

/* "Change" only makes sense while the file is staged. */
.wpi-compressor:not([data-stage="ready"]) .dz-preview .change {
	display: none;
}

.wpi-compressor[data-stage="working"] .tool-select,
.wpi-compressor[data-stage="working"] .tool-range,
.wpi-compressor[data-stage="working"] .btn.primary.lg {
	pointer-events: none;
	opacity: 0.7;
}

/* ---------- card ---------- */
.wpi-compressor .tool-card {
	background: #fff;
	border: 1px solid var(--wpi-border);
	border-radius: var(--wpi-card-radius);
	box-shadow: var(--wpi-elevation-medium);
	padding: 28px;
}

.wpi-compressor .tool-head {
	display: flex;
	align-items: center;
	justify-content: space-between;
	margin-bottom: 18px;
}

.wpi-compressor .tool-head .ttl {
	display: flex;
	align-items: center;
	gap: 9px;
	font: 600 14px/1 var(--wpi-font-sans);
	color: var(--wpi-fg);
}

.wpi-compressor .tool-head .ttl .nt-ic {
	width: 18px;
	height: 18px;
	color: var(--wpi-accent);
}

.wpi-compressor .tool-head .free {
	font: 500 12px/1 var(--wpi-font-sans);
	color: var(--wpi-fg-muted);
}

/* ---------- dropzone ---------- */
.wpi-compressor .dropzone {
	border: 1.5px dashed var(--wpi-gray-400);
	border-radius: var(--wpi-radius-md);
	min-height: 250px;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	gap: 4px;
	text-align: center;
	padding: 28px;
	cursor: pointer;
	transition: border-color 0.12s, background-color 0.12s;
	/* it's a <button> for keyboard reachability — strip the UA chrome */
	width: 100%;
	background: none;
	font: inherit;
	color: inherit;
}

.wpi-compressor .dropzone:hover,
.wpi-compressor .dropzone.drag {
	border-color: var(--wpi-accent);
	background: var(--wpi-accent-tint);
}

.wpi-compressor .dropzone .glyph {
	width: 56px;
	height: 56px;
	margin-bottom: 10px;
	line-height: 0;
}

.wpi-compressor .dropzone .dz-title {
	font: 600 18px/1.3 var(--wpi-font-sans);
	color: var(--wpi-fg);
}

.wpi-compressor .dropzone .dz-sub {
	font: 400 14px/1.4 var(--wpi-font-sans);
	color: var(--wpi-fg-muted);
}

/* The staged-file strip reuses .dropzone's frame but not its layout — these are
   the original's inline overrides on that element. */
.wpi-compressor .dropzone.is-preview {
	min-height: 0;
	cursor: default;
	padding: 18px;
	text-align: left;
}

.wpi-compressor .dropzone.is-preview:hover {
	border-color: var(--wpi-gray-400);
	background: none;
}

/* ---------- selected preview ---------- */
.wpi-compressor .dz-preview {
	display: flex;
	align-items: center;
	gap: 16px;
	width: 100%;
}

.wpi-compressor .dz-preview .thumb {
	width: 84px;
	height: 84px;
	border-radius: var(--wpi-radius-md);
	overflow: hidden;
	flex: none;
	border: 1px solid var(--wpi-border);
}

.wpi-compressor .dz-preview .thumb svg {
	width: 100%;
	height: 100%;
	object-fit: cover;
	display: block;
}

.wpi-compressor .dz-preview .meta {
	text-align: left;
	min-width: 0;
}

.wpi-compressor .dz-preview .meta .fn {
	font: 600 15px/1.3 var(--wpi-font-sans);
	color: var(--wpi-fg);
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.wpi-compressor .dz-preview .meta .fs {
	font: 400 13px/1.4 var(--wpi-font-sans);
	color: var(--wpi-fg-muted);
	margin-top: 3px;
}

.wpi-compressor .dz-preview .change {
	margin-left: auto;
}

/* ---------- controls ---------- */
.wpi-compressor .tool-controls {
	display: grid;
	grid-template-columns: 1fr 1fr;
	gap: 22px;
	margin-top: 22px;
}

.wpi-compressor .ctrl-label {
	display: flex;
	align-items: center;
	justify-content: space-between;
	font: 600 11px/1 var(--wpi-font-sans);
	letter-spacing: 0.04em;
	text-transform: uppercase;
	color: var(--wpi-fg-muted);
	margin-bottom: 9px;
}

.wpi-compressor .ctrl-label .val {
	color: var(--wpi-accent);
	font-size: 15px;
}

.wpi-compressor select.tool-select {
	-webkit-appearance: none;
	appearance: none;
	width: 100%;
	height: 44px;
	padding: 0 32px 0 12px;
	font: 15px/1 var(--wpi-font-sans);
	color: var(--wpi-fg);
	background-color: var(--wpi-surface-muted);
	border: 1px solid var(--wpi-border);
	border-radius: var(--wpi-radius-md);
	cursor: pointer;
	background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24'%3E%3Cg transform='translate(6.5 9.2)'%3E%3Cpath d='M 11 1.2 L 5.5 5.6 L 0 1.2 L 0.9 0 L 5.5 3.6 L 10 0 L 11 1.2 Z' fill='%23757575'/%3E%3C/g%3E%3C/svg%3E");
	background-repeat: no-repeat;
	background-position: right 8px center;
	background-size: 20px;
}

.wpi-compressor select.tool-select:focus {
	outline: none;
	border-color: var(--wpi-accent);
	box-shadow: 0 0 0 1px var(--wpi-accent);
}

.wpi-compressor input.tool-range {
	-webkit-appearance: none;
	appearance: none;
	width: 100%;
	height: 4px;
	border-radius: 999px;
	background: var(--wpi-gray-300);
	margin: 20px 0 0;
	cursor: pointer;
}

.wpi-compressor input.tool-range::-webkit-slider-runnable-track {
	height: 4px;
	border-radius: 999px;
}

.wpi-compressor input.tool-range::-webkit-slider-thumb {
	-webkit-appearance: none;
	width: 18px;
	height: 18px;
	border-radius: 50%;
	background: var(--wpi-accent);
	margin-top: -7px;
	box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
	cursor: pointer;
}

.wpi-compressor input.tool-range::-moz-range-thumb {
	width: 18px;
	height: 18px;
	border: none;
	border-radius: 50%;
	background: var(--wpi-accent);
	cursor: pointer;
}

.wpi-compressor .tool-cta {
	margin-top: 24px;
}

/* ---------- progress ---------- */
.wpi-compressor .tool-progress {
	margin-top: 22px;
}

.wpi-compressor .tool-progress .track {
	height: 8px;
	border-radius: 999px;
	background: var(--wpi-gray-200);
	overflow: hidden;
}

.wpi-compressor .tool-progress .fill {
	height: 100%;
	background: var(--wpi-accent);
	border-radius: 999px;
	width: 0;
	transition: width 0.15s linear;
}

.wpi-compressor .tool-progress .label {
	display: flex;
	justify-content: space-between;
	font: 500 13px/1 var(--wpi-font-sans);
	color: var(--wpi-fg-muted);
	margin-bottom: 10px;
}

/* ---------- result ---------- */
.wpi-compressor .result {
	margin-top: 4px;
}

.wpi-compressor .result .success {
	display: flex;
	align-items: center;
	gap: 10px;
	padding: 12px 14px;
	border-radius: var(--wpi-radius-md);
	background: #e9f6ee;
	color: #1a7a3e;
	font: 500 14px/1.3 var(--wpi-font-sans);
	margin-bottom: 18px;
}

.wpi-compressor .result .success .nt-ic {
	width: 20px;
	height: 20px;
	flex: none;
}

.wpi-compressor .result-ba {
	margin-bottom: 16px;
}

.wpi-compressor .result-stats {
	display: grid;
	grid-template-columns: repeat(3, 1fr);
	gap: 10px;
	margin-bottom: 14px;
}

.wpi-compressor .result-stats .rs {
	text-align: center;
	padding: 12px 8px;
	border: 1px solid var(--wpi-border);
	border-radius: var(--wpi-radius-md);
	background: var(--wpi-surface-muted);
}

.wpi-compressor .result-stats .rs .k {
	font: 600 10px/1 var(--wpi-font-sans);
	letter-spacing: 0.05em;
	text-transform: uppercase;
	color: var(--wpi-fg-muted);
}

.wpi-compressor .result-stats .rs .v {
	font: 600 22px/1 var(--wpi-font-sans);
	margin-top: 7px;
	letter-spacing: -0.01em;
	color: var(--wpi-fg);
}

.wpi-compressor .result-stats .rs .v.after {
	color: var(--wpi-accent);
}

.wpi-compressor .result-stats .rs .v.save {
	color: #1a7a3e;
}

.wpi-compressor .result-attrs {
	display: flex;
	align-items: stretch;
	gap: 0;
	margin-bottom: 18px;
	border: 1px solid var(--wpi-border);
	border-radius: var(--wpi-radius-md);
	overflow: hidden;
	background: var(--wpi-surface-muted);
}

.wpi-compressor .result-attrs span {
	flex: 1;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	gap: 4px;
	padding: 11px 8px;
	border-left: 1px solid var(--wpi-border);
	min-width: 0;
}

.wpi-compressor .result-attrs span:first-child {
	border-left: none;
}

.wpi-compressor .result-attrs .cap {
	font: 600 10px/1 var(--wpi-font-sans);
	letter-spacing: 0.05em;
	text-transform: uppercase;
	color: var(--wpi-fg-muted);
	font-style: normal;
}

.wpi-compressor .result-attrs b {
	font: 600 14px/1 var(--wpi-font-sans);
	color: var(--wpi-fg);
	white-space: nowrap;
}

.wpi-compressor .result .actions {
	display: flex;
	gap: 10px;
	margin-top: 6px;
}

/* ---------- before / after stage ---------- */
.wpi-compressor .ba-stage {
	position: relative;
	border-radius: var(--wpi-card-radius);
	overflow: hidden;
	border: 1px solid var(--wpi-border);
	box-shadow: var(--wpi-elevation-small);
	user-select: none;
	aspect-ratio: 16 / 10;
}

.wpi-compressor .ba-stage.compact {
	aspect-ratio: 16 / 9;
	border-radius: var(--wpi-radius-md);
}

.wpi-compressor .ba-stage .layer {
	position: absolute;
	inset: 0;
}

.wpi-compressor .ba-stage .layer svg {
	width: 100%;
	height: 100%;
	display: block;
}

/* The right-hand ("WPImage") side carries the artefacts; the clip above it
   reveals the untouched original on the left. Named as the prototype named it. */
.wpi-compressor .ba-stage .layer.before {
	filter: contrast(0.94) saturate(0.82) blur(0.6px);
}

.wpi-compressor .ba-stage .clip {
	position: absolute;
	inset: 0;
	overflow: hidden;
	clip-path: inset(0 calc(100% - var(--wpi-pos)) 0 0);
}

.wpi-compressor .ba-stage .handle {
	position: absolute;
	top: 0;
	bottom: 0;
	left: var(--wpi-pos);
	width: 3px;
	background: #fff;
	box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.08);
	cursor: ew-resize;
	z-index: 3;
	/* the range input on top owns the pointer */
	pointer-events: none;
}

.wpi-compressor .ba-stage .handle .grip {
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	width: 38px;
	height: 38px;
	border-radius: 50%;
	background: #fff;
	box-shadow: var(--wpi-elevation-medium);
	display: flex;
	align-items: center;
	justify-content: center;
	color: var(--wpi-accent);
}

.wpi-compressor .ba-stage.compact .handle .grip {
	width: 32px;
	height: 32px;
}

.wpi-compressor .ba-stage .handle .grip .nt-ic {
	width: 22px;
	height: 22px;
}

.wpi-compressor .ba-stage.compact .handle .grip .nt-ic {
	width: 14px;
	height: 14px;
}

.wpi-compressor .ba-stage .tag {
	position: absolute;
	bottom: 14px;
	padding: 6px 11px;
	border-radius: var(--wpi-radius-sm);
	font: 600 12px/1 var(--wpi-font-sans);
	color: #fff;
	-webkit-backdrop-filter: blur(4px);
	backdrop-filter: blur(4px);
	z-index: 2;
}

.wpi-compressor .ba-stage.compact .tag {
	bottom: 10px;
	font-size: 11px;
	padding: 5px 9px;
}

.wpi-compressor .ba-stage .tag.l {
	left: 14px;
	background: var(--wpi-nt-flame);
}

.wpi-compressor .ba-stage .tag.r {
	right: 14px;
	background: var(--wpi-alert-green);
}

/* The real control: an invisible range stretched over the stage. Dragging it or
   clicking its track behaves exactly like the prototype's mousemove handler,
   and it brings keyboard + touch support the prototype never had. */
.wpi-compressor .ba-stage .ba-input {
	position: absolute;
	inset: 0;
	z-index: 4;
	width: 100%;
	height: 100%;
	margin: 0;
	-webkit-appearance: none;
	appearance: none;
	background: transparent;
	opacity: 0;
	cursor: ew-resize;
}

.wpi-compressor .ba-stage .ba-input:focus-visible {
	opacity: 1;
	outline: 2px solid var(--wpi-accent);
	outline-offset: 2px;
}

/* Give the invisible thumb real width so a drag has something to grab. */
.wpi-compressor .ba-stage .ba-input::-webkit-slider-thumb {
	-webkit-appearance: none;
	width: 38px;
	height: 100%;
	cursor: ew-resize;
}

.wpi-compressor .ba-stage .ba-input::-moz-range-thumb {
	width: 38px;
	height: 100%;
	border: 0;
	border-radius: 0;
	background: transparent;
	cursor: ew-resize;
}

/* ---------- buttons ---------- */
.wpi-compressor .btn {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	gap: 8px;
	height: 48px;
	padding: 0 22px;
	border: none;
	cursor: pointer;
	font: 500 15px/1 var(--wpi-font-sans);
	border-radius: var(--wpi-radius-sm);
	transition: background-color 0.12s, box-shadow 0.12s, color 0.12s;
	white-space: nowrap;
	text-decoration: none;
}

.wpi-compressor .btn .nt-ic {
	width: 20px;
	height: 20px;
}

.wpi-compressor .btn.primary {
	background: var(--wpi-accent);
	color: #fff;
}

.wpi-compressor .btn.primary:hover {
	background: var(--wpi-accent-dark);
}

.wpi-compressor .btn.primary:active {
	background: var(--wpi-accent-darker);
}

.wpi-compressor .btn.secondary {
	background: #fff;
	color: var(--wpi-fg);
	box-shadow: inset 0 0 0 1px var(--wpi-border-strong);
}

.wpi-compressor .btn.secondary:hover {
	box-shadow: inset 0 0 0 1.5px var(--wpi-accent);
	color: var(--wpi-accent);
}

.wpi-compressor .btn.ghost {
	background: transparent;
	color: var(--wpi-accent);
}

.wpi-compressor .btn.ghost:hover {
	background: var(--wpi-accent-tint);
}

.wpi-compressor .btn.sm {
	height: 38px;
	padding: 0 16px;
	font-size: 14px;
}

.wpi-compressor .btn.lg {
	height: 54px;
	padding: 0 28px;
	font-size: 16px;
}

.wpi-compressor .btn.block {
	width: 100%;
}

/* ---------- responsive (original breakpoint) ---------- */
@media (max-width: 600px) {
	.wpi-compressor .tool-card {
		padding: 18px;
		max-width: 100%;
	}

	.wpi-compressor .tool-head {
		flex-wrap: wrap;
		gap: 4px;
	}

	.wpi-compressor .tool-head .free {
		font-size: 11px;
		flex-basis: 100%;
	}

	.wpi-compressor .tool-head .ttl {
		font-size: 15px;
	}

	.wpi-compressor .tool-controls {
		grid-template-columns: 1fr;
		gap: 18px;
	}

	.wpi-compressor .result-stats {
		grid-template-columns: 1fr 1fr;
	}

	.wpi-compressor .result-stats .rs:nth-child(3) {
		grid-column: 1 / -1;
	}

	.wpi-compressor .result-stats .rs .v {
		font-size: 20px;
	}

	.wpi-compressor .result .actions {
		flex-direction: column;
	}

	.wpi-compressor .result .actions .btn {
		width: 100%;
	}

	.wpi-compressor .result .success {
		font-size: 13px;
	}

	.wpi-compressor .result-attrs span {
		padding: 10px 4px;
	}

	.wpi-compressor .result-attrs b {
		font-size: 13px;
	}
}

/* ── live mode ─────────────────────────────────────────────────────────────────
   Everything below is only reachable when data-mode="live" (an API is
   configured). Demo mode never renders a file input, real <img> layers, an error
   box or a quota badge, so these rules simply don't apply to it.               */

/* The dropzone is the positioning context for its overlaid file input. */
.wpi-compressor .dropzone {
	position: relative;
}

/* A transparent, full-bleed <input type="file">: clicking anywhere on the
   dropzone opens the picker, and the input stays natively keyboard-focusable. */
.wpi-compressor .dz-file {
	position: absolute;
	inset: 0;
	width: 100%;
	height: 100%;
	margin: 0;
	opacity: 0;
	cursor: pointer;
}

.wpi-compressor .dropzone:focus-within {
	border-color: var(--wpi-accent);
	background: var(--wpi-accent-tint);
}

/* Real images fill the same boxes the demo's inline SVGs did. */
.wpi-compressor .dz-preview .thumb img {
	width: 100%;
	height: 100%;
	object-fit: cover;
	display: block;
}

.wpi-compressor .ba-stage .layer img {
	width: 100%;
	height: 100%;
	object-fit: cover;
	display: block;
}

/* Error box (oversize file, API failure). */
.wpi-compressor .tool-error {
	margin-top: 16px;
	padding: 12px 14px;
	border-radius: var(--wpi-radius-md);
	background: #fdECEC;
	border: 1px solid #f3bcbc;
	color: #b3261e;
	font: 500 13px/1.4 var(--wpi-font-sans);
}

.wpi-compressor .tool-error[hidden] {
	display: none;
}

/* Quota badge, mirroring the tester's pill. */
.wpi-compressor .quota-badge {
	display: inline-flex;
	align-items: center;
	gap: 6px;
	align-self: flex-start;
	margin-bottom: 16px;
	padding: 6px 12px;
	border-radius: 999px;
	background: var(--wpi-accent-tint);
	border: 1px solid #cce0ff;
	color: var(--wpi-accent);
	font: 500 13px/1 var(--wpi-font-sans);
}

.wpi-compressor .quota-badge.low {
	background: #fff4f4;
	border-color: #ffcccc;
	color: #b3261e;
}

.wpi-compressor .quota-badge[hidden] {
	display: none;
}

/* Live compression gives no real progress percentage, so the bar animates
   indeterminately and the "42%" readout is hidden. */
.wpi-compressor[data-mode="live"] [data-wpi-progress-out] {
	display: none;
}

.wpi-compressor[data-mode="live"][data-stage="working"] .tool-progress .fill {
	width: 40%;
	animation: wpi-cmp-indeterminate 1.1s ease-in-out infinite;
}

@keyframes wpi-cmp-indeterminate {
	0% {
		margin-left: -40%;
	}
	100% {
		margin-left: 100%;
	}
}
