/*
 * Flatbed: the preorder form's switch between counting by the flatbed and by the unit, and the drawing of the
 * order on its way (the flatbed controller). Drawn in the story's palette: emerald homes, a terracotta cab like
 * the story's forklift, charcoal steel.
 */

/* "Increment by:" and the switch, on a line of their own above the units */
.unit-switch-row {
  display: flex;
  flex-basis: 100%;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
}

.unit-switch-row[hidden] {
  display: none;
}

/* The switch: a pill with the three options, and a thumb behind the one in use (data-mode) */
.unit-switch {
  position: relative;
  display: inline-grid;
  grid-template-columns: repeat(3, 1fr);
  padding: 2px;
  border: 1px solid var(--color-action);
  border-radius: 999px;
}

.unit-switch::before {
  content: "";
  position: absolute;
  inset: 2px auto 2px 2px;
  width: calc((100% - 4px) / 3);
  border-radius: 999px;
  background: var(--color-action);
  transition: translate 0.25s ease;
}

.unit-switch[data-mode="units"]::before {
  translate: 100% 0;
}

.unit-switch[data-mode="custom"]::before {
  translate: 200% 0;
}

.unit-switch__option {
  position: relative;
  padding: var(--space-1) var(--space-3);
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--color-action);
  font: inherit;
  text-align: center;
  cursor: pointer;
  transition: color 0.25s ease;
}

.unit-switch__option[aria-pressed="true"] {
  color: var(--color-action-text);
}

/* The drawing, the form's full width under Units and Preorder */
.flatbed {
  flex-basis: 100%;
}

.flatbed svg {
  display: block;
  width: 100%;
  max-width: 760px;
  height: auto;
  overflow: hidden;
}

.flatbed__road {
  fill: var(--color-sage-40);
}

.flatbed__lane {
  stroke: var(--color-cream);
  stroke-width: 2;
  stroke-dasharray: 14 12;
}

/* Counting one by one, the road bends up into the background, where the full flatbeds wait. */
.flatbed__far {
  opacity: 0;
  transition: opacity 0.6s ease;
}

.flatbed--one-by-one .flatbed__far {
  opacity: 1;
}

.flatbed__hill {
  fill: var(--color-emerald-80);
}

.flatbed__lane--far {
  stroke-width: 1;
  stroke-dasharray: 8 7;
}

.flatbed__plus {
  fill: var(--color-heading);
  font-size: 30px;
  font-weight: 700;
}

.flatbed__count {
  fill: var(--color-heading);
  font-size: 30px;
  font-weight: 700;
  transform-box: fill-box;
  transform-origin: center;
  transition: font-size 0.8s ease-in-out;
}

/* Grows as the convoy shrinks into the background, so the count stays readable there */
.flatbed--one-by-one .flatbed__count {
  font-size: 46px;
}

.flatbed__steel,
.flatbed__skirt,
.flatbed__cap,
.flatbed__eye {
  fill: var(--color-charcoal);
}

/* Rubber, a little greyer than the steel, as in the story */
.flatbed__tire {
  fill: color-mix(in srgb, var(--color-charcoal), white 22%);
}

.flatbed__hub,
.flatbed__light,
.flatbed__face {
  fill: var(--color-sand);
}

.flatbed__spoke {
  stroke: var(--color-charcoal);
  stroke-width: 2;
  stroke-linecap: round;
}

.flatbed__cab,
.flatbed__forklift-body,
.flatbed__strap {
  fill: var(--color-terracotta);
}

.flatbed__panel {
  fill: color-mix(in srgb, var(--color-terracotta) 85%, var(--color-charcoal));
}

.flatbed__window,
.flatbed__glass {
  fill: var(--color-emerald-80);
}

.flatbed__seat {
  fill: var(--color-sage-60);
}

.flatbed__guard {
  fill: none;
  stroke: var(--color-charcoal);
  stroke-width: 4;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.flatbed__face {
  stroke: var(--color-charcoal);
  stroke-width: 1.2;
}

.flatbed__eye-white {
  fill: #fff;
  stroke: var(--color-charcoal);
  stroke-width: 0.8;
}

.flatbed__line {
  fill: none;
  stroke: var(--color-charcoal);
  stroke-width: 1.8;
  stroke-linecap: round;
}

/* A full flatbed's driver gives a slight smile; until then, a level, neutral mouth. */
.flatbed__truck[data-mood="happy"] .flatbed__mood--neutral,
.flatbed__truck:not([data-mood="happy"]) .flatbed__mood--happy {
  display: none;
}

.flatbed__wall {
  fill: var(--color-emerald);
}

.flatbed__door,
.flatbed__frame,
.flatbed__starlink {
  fill: var(--color-cream);
}

.flatbed__vent,
.flatbed__sign {
  fill: var(--color-sage-80);
}

.flatbed__home--gone {
  visibility: hidden;
}

/* Wheels turn while a truck drives, backwards when it reverses. */
.flatbed__wheel {
  transform-box: fill-box;
  transform-origin: center;
}

.flatbed__truck--driving .flatbed__wheel {
  animation: flatbed-wheel 0.5s linear infinite;
}

.flatbed__truck--reversing .flatbed__wheel {
  animation-direction: reverse;
}

@keyframes flatbed-wheel {
  to {
    rotate: 1turn;
  }
}

/* The blow-up, when a typed count isn't one (the flatbed controller's explode). 2.2s in all, BOOM_MS in the
   controller: a flash, the front truck's homes thrown off and its paint scorched, then fire and smoke that die
   down before the truck is put back. Individual transform properties, so they add to the truck's place on the
   road and each home's slot rather than replacing them. Every animation is declared under .flatbed--boom, so
   adding the class starts it: Chrome keeps animating SVG hidden by display: none, which would play it unseen. */
.flatbed__boom {
  display: none;
  pointer-events: none;
}

.flatbed--boom .flatbed__boom {
  display: inline;
}

.flatbed__burst {
  fill: #ffd95a;
  stroke: #f08a24;
  stroke-width: 4;
  transform-box: fill-box;
  transform-origin: center;
}

.flatbed--boom .flatbed__burst {
  animation: flatbed-burst 0.5s ease-out both;
}

.flatbed--boom .flatbed__flame {
  animation: flatbed-fade 2.2s ease-in both;
}

.flatbed__flame-outer {
  fill: #e8572a;
}

.flatbed__flame-inner {
  fill: #ffc43d;
}

.flatbed__flame-outer,
.flatbed__flame-inner {
  transform-box: fill-box;
  transform-origin: bottom;
}

.flatbed--boom :is(.flatbed__flame-outer, .flatbed__flame-inner) {
  animation: flatbed-flicker 0.24s ease-in-out infinite alternate;
}

.flatbed--boom .flatbed__flame--2 :is(.flatbed__flame-outer, .flatbed__flame-inner) {
  animation-delay: -0.12s;
}

.flatbed__smoke {
  fill: var(--color-sage-40);
  opacity: 0;
}

.flatbed--boom .flatbed__smoke {
  animation: flatbed-smoke 2s ease-out 0.3s both;
}

.flatbed--boom .flatbed__smoke--2 {
  animation-delay: 0.5s;
}

.flatbed--boom .flatbed__smoke--3 {
  animation-delay: 0.7s;
}

/* Rocks on its wheels. Only the front truck is wrecked, and it's drawn at scale 1, so moving its origin is safe. */
.flatbed__truck--wrecked {
  transform-box: fill-box;
  transform-origin: 50% 100%;
  animation: flatbed-jolt 0.4s ease-out;
}

.flatbed__truck--wrecked > :not(.flatbed__count) {
  animation: flatbed-scorch 2.2s ease-in both;
}

.flatbed__truck--wrecked .flatbed__home {
  transform-box: fill-box;
  transform-origin: center;
  animation: flatbed-thrown 1.1s cubic-bezier(0.2, 0.6, 0.4, 1) both;
}

/* The homes are a truck's first four <g>s (_flatbed_truck), each thrown its own way. */
.flatbed__truck--wrecked .flatbed__home:nth-of-type(1) { --thrown-x: -160px; --thrown-turn: -200deg; }
.flatbed__truck--wrecked .flatbed__home:nth-of-type(2) { --thrown-x: -60px;  --thrown-turn: -120deg; }
.flatbed__truck--wrecked .flatbed__home:nth-of-type(3) { --thrown-x: 70px;   --thrown-turn: 140deg; }
.flatbed__truck--wrecked .flatbed__home:nth-of-type(4) { --thrown-x: 170px;  --thrown-turn: 220deg; }

@keyframes flatbed-burst {
  from { scale: 0.1; opacity: 1; }
  60% { scale: 1.15; opacity: 1; }
  to { scale: 1.3; opacity: 0; }
}

@keyframes flatbed-flicker {
  from { scale: 1 1; }
  to { scale: 0.9 1.12; }
}

@keyframes flatbed-fade {
  0%, 10% { opacity: 0; }
  20%, 75% { opacity: 1; }
  to { opacity: 0; }
}

@keyframes flatbed-smoke {
  from { opacity: 0.8; translate: 0 0; scale: 0.6; }
  to { opacity: 0; translate: 0 -90px; scale: 1.6; }
}

@keyframes flatbed-jolt {
  0%, 100% { rotate: 0deg; }
  25% { rotate: -3deg; }
  60% { rotate: 2deg; }
}

@keyframes flatbed-scorch {
  0%, 10% { filter: none; }
  20%, 80% { filter: brightness(0.25) saturate(0); }
  to { filter: none; }
}

@keyframes flatbed-thrown {
  from { translate: 0 0; rotate: 0deg; opacity: 1; }
  45% { translate: calc(var(--thrown-x) * 0.6) -150px; }
  to { translate: var(--thrown-x) 40px; rotate: var(--thrown-turn); opacity: 0; }
}
