:root {
  --primary: #05a55d;
  --on-primary: #ffffff;
  --surface: #f6f6f7;
  --surface-container-low: #b9bac0;
  --motion-standard: cubic-bezier(0.2, 0, 0, 1);
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  background: #4c4c4c;
}

.demo-stage {
  display: grid;
  min-height: 100vh;
  place-items: center;
}

.switch {
  position: relative;
  display: block;
  width: 52px;
  height: 32px;
  padding: 0;
  overflow: hidden;
  border: 0;
  border-radius: 100px;
  background: var(--surface-container-low);
  cursor: pointer;
  outline: none;
  transition: background-color 240ms var(--motion-standard);
  -webkit-tap-highlight-color: transparent;
}

.switch[aria-checked="true"] {
  background: var(--primary);
}

.handle {
  position: absolute;
  top: 2px;
  left: 2px;
  display: block;
  width: 28px;
  height: 28px;
  transform: translateX(20px);
  will-change: transform;
}

.switch[aria-checked="true"] .handle {
  transform: translateX(0);
}

.state-layer {
  position: absolute;
  top: 50%;
  left: 50%;
  display: grid;
  width: 28px;
  height: 28px;
  place-items: center;
  border-radius: 999px;
  background: transparent;
  transform: translate(-50%, -50%);
  transition:
    width 160ms var(--motion-standard),
    height 160ms var(--motion-standard),
    background-color 120ms ease;
}

.thumb {
  display: block;
  width: 28px;
  height: 28px;
  border-radius: 999px;
  background: var(--surface);
  transform-origin: center;
  will-change: transform, border-radius;
}

.switch[aria-checked="true"] .thumb {
  background: var(--on-primary);
}

.switch:is(:hover, :active)[aria-checked="false"] .state-layer {
  width: 36px;
  height: 36px;
  background: rgba(9, 9, 11, 0.08);
}

.switch:is(:hover, :active)[aria-checked="true"] .state-layer {
  width: 40px;
  height: 40px;
  background: rgba(5, 165, 93, 0.08);
}

.switch.to-on .handle {
  animation: handle-to-on 280ms var(--motion-standard) both;
}

.switch.to-off .handle {
  animation: handle-to-off 280ms var(--motion-standard) both;
}

.switch:is(.to-on, .to-off) .thumb {
  animation: reshape-thumb 280ms var(--motion-standard) both;
}

.switch:focus-visible {
  outline: 2px solid #ffffff;
  outline-offset: 4px;
}

@keyframes handle-to-on {
  from { transform: translateX(20px); }
  to { transform: translateX(0); }
}

@keyframes handle-to-off {
  from { transform: translateX(0); }
  to { transform: translateX(20px); }
}

@keyframes reshape-thumb {
  0%, 100% {
    border-radius: 999px;
    transform: scaleX(1);
  }
  46% {
    border-radius: 11px;
    transform: scaleX(1.14);
  }
}

@media (prefers-reduced-motion: reduce) {
  .switch,
  .state-layer,
  .switch.to-on .handle,
  .switch.to-off .handle,
  .switch:is(.to-on, .to-off) .thumb {
    transition-duration: 1ms;
    animation-duration: 1ms;
  }
}
