/*
 * Service Cards block
 *
 * One markup, two layouts plus an optional accordion:
 * - below 90rem:  compact rows with a small illustration
 * - from 90rem:   five tall illustrated cards; the back with the description
 *                 and services fades in on hover or keyboard focus
 * - below 64rem with "Small screens: Accordion" (.c-service-cards--accordion):
 *                 accordion (view.js adds the toggle buttons)
 *
 * The look "Back side" (.c-service-cards--back) skips all of them and shows
 * white cards with the back always visible and the link as a button at the bottom.
 */

.c-service-cards {
	--card-pad: var(--space-xl);
	--card-ink: var(--color-dark-green);
	--card-illustration-bg: #c6e0c3; /* the tall cards, matches the illustrations' background */

	color: var(--card-ink);
}

.c-service-card {
	position: relative;
}

.c-service-card__heading {
	display: flex;
	flex-direction: column;
	color: var(--card-ink);
}

.c-service-card__eyebrow {
	font-size: var(--font-size-h5);
	font-weight: var(--font-weight-bold); /* H5, Bold */
	line-height: var(--line-height-h5);
	letter-spacing: var(--letter-spacing-h5);
}

.c-service-card__name {
	font-size: var(--font-size-h2);
	font-weight: var(--font-weight-bold); /* H2, Bold */
	line-height: var(--line-height-h2);
	letter-spacing: var(--letter-spacing-h2);
	text-wrap: balance;
}

.c-service-card__image,
.c-service-card__image-compact {
	display: block;
}

.c-service-card__link {
	color: var(--card-ink);
	text-decoration: none;

	&:focus-visible {
		outline: 3px solid var(--color-focus);
		outline-offset: 3px;
	}
}

.c-service-card__link-icon {
	display: inline-grid;
	place-items: center;
}

/* The chevron is only shown inside the accordion toggle */
.c-service-card__heading > .c-icon {
	display: none;
}

.c-service-card__panel[hidden] {
	display: none;
}

/* The wrapper and the back face only get a box on the tall cards */
.c-service-card__inner,
.c-service-card__back {
	display: contents;
}

.c-service-card__back-title,
.c-service-card__services-label,
.c-service-card__services {
	display: none;
}

:where(.c-service-cards:not(.c-service-cards--back)) {
	/* Layouts for the look "Reveal on hover". :where() keeps the specificity
	   the rules had before the look "Back side" was added. */

	/* ------------------------------------------------------------------
	 * Rows and cards: the whole card is the link
	 * ------------------------------------------------------------------ */
	&.c-service-cards {
		padding: var(--space-lg) var(--gutter);
	}

	/* Only part of the accordion design. Hidden entirely so no
	   invisible element can receive keyboard focus. */
	.c-service-cards__footer-link {
		display: none;
	}

	.c-service-cards__title,
	.c-service-card__link-text {
		position: absolute;
		inline-size: 1px;
		block-size: 1px;
		overflow: hidden;
		clip-path: inset(50%);
		white-space: nowrap;
	}

	.c-service-cards__list {
		display: grid;
		gap: var(--space-sm);
		max-inline-size: var(--container-max);
		margin-inline: auto;
	}

	.c-service-card {
		isolation: isolate;
		overflow: hidden;
		background: var(--card-bg, var(--color-pale-green));
		outline: 1px solid var(--color-white); /* white edge drawn over the illustration, a box-shadow would sit under it */
		outline-offset: -1px;

		/* Focus ring on the whole card when its link has keyboard focus */
		&:has(.c-service-card__link:focus-visible) {
			outline: 3px solid var(--color-focus);
			outline-offset: 3px;
		}
	}

	.c-service-card__heading {
		position: relative;
		z-index: 1;
		gap: var(--space-2xs);
		pointer-events: none;
	}

	/* Stretch the link over the whole card */
	.c-service-card__link {
		position: static;

		&::after {
			content: "";
			position: absolute;
			inset: 0;
			z-index: 2;
		}

		&:focus-visible {
			outline: none;
		}
	}

	/* Long Swedish compounds ("verksamhetsmodellen") don't fit the narrow cards and rows */
	.c-service-card__name {
		hyphens: auto;
		hyphenate-limit-chars: 12 6 6; /* only long words, never "Möjlig-gör" */
	}

	/* Compact rows */
	@media (max-width: 89.999rem) {
		.c-service-cards__list {
			max-inline-size: 57.125rem; /* 914 */
		}

		.c-service-card {
			display: flex;
			align-items: center;
			justify-content: space-between;
			gap: var(--space-md);
			min-block-size: 6.875rem;
			padding-inline: var(--card-pad) var(--space-md);
			border-radius: var(--radius-sm);
		}

		.c-service-card__eyebrow {
			font-size: var(--font-size-body);
			font-weight: var(--font-weight-body);
			letter-spacing: var(--letter-spacing-label-sm);
			text-transform: uppercase;
		}

		/* Long names wrap instead of pushing the illustration out of the card */
		.c-service-card__heading {
			min-inline-size: 0;
		}

		.c-service-card__panel {
			flex: none;
			align-self: stretch;
			display: flex;
			align-items: center;
			gap: var(--space-md);
			max-inline-size: 40%;
		}

		.c-service-card__image,
		.c-service-card__description {
			display: none;
		}

		/* Arrow at the far right, after the illustration; the image shrinks, not the arrow */
		.c-service-card__link {
			flex: none;
		}

		.c-service-card__link-icon {
			color: var(--color-ink); /* darker than the text, as in Figma */

			& .c-icon {
				inline-size: 1.5rem;
				block-size: 1.5rem;
			}
		}

		.c-service-card__image-compact {
			block-size: 100%;
			max-block-size: 6.875rem;
			inline-size: auto;
			max-inline-size: 100%;
			object-fit: contain;
			object-position: right center;
		}

		/* Phones: tighter padding and a smaller name so the text keeps room beside the illustration */
		@media (max-width: 47.999rem) {
			.c-service-card {
				padding: var(--space-md) var(--space-md) var(--space-md) var(--space-lg);
			}

			.c-service-card__panel {
				margin-block: calc(-1 * var(--space-md)); /* the illustration still fills the card height */
			}

			.c-service-card__name {
				font-size: var(--font-size-h3);
			}
		}
	}

	/* Tall illustrated cards. On hover or keyboard focus the white back
	   fades in over the illustration, as the Figma prototype (Dissolve,
	   ease out, 300 ms). */
	@media (min-width: 90rem) /* 1440: narrower cards overflow the back */ {
		.c-service-cards__list {
			grid-template-columns: repeat(auto-fit, minmax(0, 1fr));
			gap: 0.625rem;
		}

		.c-service-card {
			aspect-ratio: 326 / 620;
			border-radius: var(--radius-md);
			overflow: visible;
			background: none;
			outline: none;
			transition: z-index 0s 300ms; /* stays on top while the back fades out */
		}

		/* Heading on top, the illustration in the rest of the card, so the
		   illustration starts below the heading however many lines it has */
		.c-service-card__inner {
			display: grid;
			grid-template-columns: minmax(0, 1fr); /* a long word must not widen the column */
			grid-template-rows: auto minmax(0, 1fr);
			row-gap: var(--space-md);
			position: absolute;
			inset: 0;
			padding: var(--card-pad);
			border-radius: inherit;
			background: var(--card-illustration-bg);
			box-shadow: inset 0 0 0 1px var(--color-white);
			container-type: inline-size;
		}

		/* The back and the link's ::after are absolute and cover the inner box */
		.c-service-card__panel {
			display: contents;
		}

		/* Placed explicitly: auto-placement would push the heading to a third row.
		   The link has zero size (text visually hidden) and shares the first row. */
		.c-service-card__heading,
		.c-service-card__link {
			grid-area: 1 / 1;
		}

		/* Default: the whole card, behind the heading, out over the padding */
		.c-service-card__image {
			grid-area: 1 / 1 / -1;
			inline-size: calc(100% + 2 * var(--card-pad));
			max-inline-size: none; /* app.css caps img at 100% */
			block-size: calc(100% + 2 * var(--card-pad));
			margin: calc(-1 * var(--card-pad));
			border-radius: var(--radius-md);
			object-fit: var(--card-image-fit, contain); /* ACF "Image fit", per card */
			object-position: center bottom;
		}

		/* ACF "Image below heading": the rest of the card under the heading */
		.c-service-card--image-below .c-service-card__image {
			grid-area: 2 / 1;
			block-size: calc(100% + var(--card-pad));
			margin-block-start: 0;
			border-radius: 0 0 var(--radius-md) var(--radius-md);
		}

		.c-service-card__image-compact,
		.c-service-card__link-icon {
			display: none;
		}

		/* Sizes in cqi follow the card width, so the back keeps the Figma
		   proportions (326 px card) on narrower screens. */
		.c-service-card__back {
			display: block;
			position: absolute;
			inset: 0 0 auto; /* grows below the card when the text needs it; the row doesn't move */
			min-block-size: 100%;
			padding: var(--card-pad); /* as the front, so eyebrow and heading line up */
			border-radius: var(--radius-md);
			background: var(--color-white);
			color: var(--color-neutral-600);
			font-size: clamp(0.875rem, 4.9cqi, 1rem); /* Body, scaled with the card */
			line-height: var(--line-height-body);
			letter-spacing: var(--letter-spacing-body);
			z-index: 1; /* over the front heading (z-index 1, earlier in the markup) */
			opacity: 0;
			transition: opacity 300ms ease-out;
			pointer-events: none;

			& > * + * {
				margin-block-start: 1.5625em;
			}
		}

		/* Eyebrow and heading as on the front: same sizes and gap */
		.c-service-card__back-title {
			display: flex;
			flex-direction: column;
			gap: var(--space-2xs);

			& .c-service-card__eyebrow {
				color: var(--card-ink);
			}

			& .c-service-card__name {
				font-weight: var(--font-weight-regular); /* H2, Regular */
			}
		}

		.c-service-card__description {
			display: block;
		}

		.c-service-card__services-label {
			display: block;
			font-weight: var(--font-weight-bold);
		}

		.c-service-card__services {
			display: grid;
			gap: 0.5em;
			padding-inline-start: 1.5em;
			list-style: disc;
			line-height: 1.25; /* compact, so the back rarely needs to grow below the card */
		}

		/* The visible back lies over the neighbours and the next section, and takes
		   the pointer so the hover stays while the mouse is on its lower part */
		@media (hover: hover) {
			.c-service-card:hover {
				z-index: 2;
				transition-delay: 0s;

				& .c-service-card__back {
					opacity: 1;
					pointer-events: auto;
					box-shadow: 0 0.5rem 1.5rem rgb(17 38 43 / 0.15);
				}
			}
		}

		.c-service-card:has(.c-service-card__link:focus-visible) {
			z-index: 2;
			transition-delay: 0s;

			& .c-service-card__back {
				opacity: 1;
				box-shadow: 0 0.5rem 1.5rem rgb(17 38 43 / 0.15);
			}
		}
	}

	/* ------------------------------------------------------------------
	 * Accordion (small screens, "Small screens: Accordion"). Comes after the
	 * rows and overrides them, as the row rules apply at every width.
	 * ------------------------------------------------------------------ */
	@media (max-width: 63.999rem) {
		&.c-service-cards--accordion {
			padding: var(--space-xl) var(--gutter);
			background: var(--color-mint);

			/* Undo the row layout */
			.c-service-cards__list {
				display: block;
				max-inline-size: none;
			}

			.c-service-card {
				display: block;
				min-block-size: 0;
				padding: 0;
				overflow: visible;
				border-radius: 0;
				background: none;
				outline: none;

				&:has(.c-service-card__link:focus-visible) {
					outline: none;
				}
			}

			.c-service-cards__title,
			.c-service-card__link-text {
				position: static;
				inline-size: auto;
				block-size: auto;
				overflow: visible;
				clip-path: none;
				white-space: normal;
			}

			.c-service-cards__footer-link {
				display: revert;
			}

			.c-service-card__heading {
				pointer-events: auto;
			}

			.c-service-card__link {
				&::after {
					content: none;
				}

				&:focus-visible {
					outline: 3px solid var(--color-focus);
					outline-offset: 3px;
				}
			}

			.c-service-card__link-icon .c-icon {
				inline-size: 1rem;
				block-size: 1rem;
			}

			.c-service-card__image-compact {
				max-block-size: none;
			}

			.c-service-card__panel {
				max-inline-size: none;
				margin-block: 0;
			}

			.c-service-card__description {
				display: block;
			}

			.c-service-cards__title {
				margin-block-end: var(--space-sm);
				color: var(--card-ink);
				font-size: var(--font-size-h2);
				font-weight: var(--font-weight-h2);
				line-height: var(--line-height-h2);
				letter-spacing: var(--letter-spacing-h2);
			}

			.c-service-card {
				border-block-end: 1px solid var(--color-grass);
			}

			.c-service-card__heading {
				font-size: var(--font-size-body-sm);
				line-height: var(--line-height-body-sm);
				letter-spacing: var(--letter-spacing-body-sm);
			}

			.c-service-card__eyebrow,
			.c-service-card__toggle {
				font-size: var(--font-size-body-sm);
				font-weight: var(--font-weight-bold); /* Body Small Bold */
				line-height: var(--line-height-body-sm);
				letter-spacing: var(--letter-spacing-body-sm);
				text-transform: none; /* the rows' uppercase label */
			}

			/* The name is part of the heading for assistive tech, but the design
			   only shows the service label in the accordion row. */
			.c-service-card__name {
				position: absolute;
				inline-size: 1px;
				block-size: 1px;
				overflow: hidden;
				clip-path: inset(50%);
				white-space: nowrap;
			}

			.c-service-card__toggle {
				display: flex;
				align-items: center;
				justify-content: space-between;
				gap: var(--space-md);
				inline-size: 100%;
				min-block-size: 3.5rem;
				padding: var(--space-md) 0;
				border: 0;
				background: none;
				color: var(--card-ink);
				font-family: inherit;
				text-align: start;
				cursor: pointer;

				& .c-icon {
					flex: none;
					transition: rotate var(--duration-base) var(--ease-out);
				}

				&[aria-expanded="true"] .c-icon {
					rotate: 180deg;
				}

				&:focus-visible {
					outline: 3px solid var(--color-focus);
					outline-offset: 2px;
				}
			}

			/* Without JS the heading is plain text, so give it the row spacing. */
			.c-service-card__heading:not(:has(button)) {
				padding-block: 1.125rem var(--space-xs);
			}

			.c-service-card__panel {
				display: grid;
				grid-template-columns: 1fr auto;
				grid-template-areas:
					"description image"
					"link        image";
				align-items: start;
				gap: var(--space-sm) var(--space-md);
				padding-block-end: var(--space-lg);
			}

			.c-service-card__image {
				display: none;
			}

			.c-service-card__image-compact {
				grid-area: image;
				inline-size: 6rem;
				block-size: auto;
				border-radius: var(--radius-sm);
			}

			.c-service-card__description {
				grid-area: description;
				font-size: var(--font-size-body);
				line-height: var(--line-height-body);
				letter-spacing: var(--letter-spacing-body);
			}

			.c-service-card__link {
				grid-area: link;
				display: inline-flex;
				align-items: center;
				gap: var(--space-xs);
				font-weight: var(--font-weight-semibold);

				&:hover {
					text-decoration: underline;
					text-underline-offset: 0.2em;
				}
			}

			.c-service-cards__footer-link {
				margin-block-start: var(--space-lg);
			}
		}
	}

	@media (prefers-reduced-motion: reduce) {
		.c-service-card__toggle .c-icon,
		.c-service-card__back {
			transition: none;
		}
	}
}

/* ------------------------------------------------------------------
 * Look "Back side": the back of every card, no hover effect and no accordion.
 * The list item is a grid. The white box (::before) spans the heading,
 * the back and the link, which sits at the bottom of the card as a button.
 * ------------------------------------------------------------------ */
.c-service-cards--back {
	padding: var(--space-lg) var(--gutter) var(--space-3xl);

	& .c-service-cards__title {
		position: absolute;
		inline-size: 1px;
		block-size: 1px;
		overflow: hidden;
		clip-path: inset(50%);
		white-space: nowrap;
	}

	& .c-service-cards__list {
		display: grid;
		gap: var(--space-xl) 0.625rem;
		max-inline-size: var(--container-max);
		margin-inline: auto;

		@media (min-width: 48rem) {
			grid-template-columns: repeat(2, minmax(0, 1fr));
		}

		@media (min-width: 64rem) {
			grid-template-columns: repeat(3, minmax(0, 1fr));
		}

		@media (min-width: 90rem) /* as the hover look: narrower cards can't fit "Transformation –" */ {
			grid-template-columns: repeat(auto-fit, minmax(0, 1fr));
		}
	}

	& .c-service-card {
		isolation: isolate;
		display: grid;
		grid-template-columns: minmax(0, 1fr); /* an auto column grows to the longest word and pushes the card into the gap */
		grid-template-rows: auto 1fr auto;

		&::before {
			content: "";
			grid-area: 1 / 1 / 4 / 2;
			z-index: -1;
			border-radius: var(--radius-lg);
			background: var(--color-white);
		}
	}

	& .c-service-card__panel {
		display: contents;
	}

	/* Eyebrow and heading spaced as on the front of the card */
	& .c-service-card__heading {
		grid-area: 1 / 1;
		gap: var(--space-2xs);
		padding: var(--card-pad) var(--card-pad) 0;
	}

	& .c-service-card__name {
		color: var(--color-neutral-600);
		font-weight: var(--font-weight-regular); /* H2, Regular */
	}

	& .c-service-card__back {
		grid-area: 2 / 1;
		display: block;
		padding: 1.5625rem var(--card-pad) 0;
		color: var(--color-neutral-600);
		font-size: var(--font-size-body);
		line-height: var(--line-height-body);
		letter-spacing: var(--letter-spacing-body);

		& > * + * {
			margin-block-start: 1.5625em;
		}
	}

	& .c-service-card__services-label {
		display: block;
		font-weight: var(--font-weight-bold);
	}

	& .c-service-card__services {
		display: grid;
		gap: var(--space-md);
		padding-inline-start: 1.5em;
		list-style: disc;
		font-size: var(--font-size-body-sm);
		line-height: var(--line-height-body-sm);
		letter-spacing: var(--letter-spacing-body-sm);
	}

	/* .c-button--grass from tokens.css, inside the white box at the bottom of the card */
	& .c-service-card__link {
		grid-area: 3 / 1;
		justify-self: center;
		margin-block: var(--space-2xl);
		color: var(--color-white);
	}

	& .c-service-card__link-icon {
		display: none;
	}
}
