/* Material Design 3 Switch Component
 * ==========================================================================
 * MD3 Switches are used for binary on/off states
 * Reference: https://m3.material.io/components/switch
 */

/* Switch Container
 * -------------------------------------------------------------------------- */

.switch {
  /* Structure */
  display: inline-flex;
  align-items: center;
  gap: 0.75rem;
  position: relative;

  /* Default color variables */
  --switch-track-color: var(--color-bg--primary-container);
  --switch-border-color: var(--color-primary);
  --switch-thumb-color: var(--color-primary);

  /* Allow clicking anywhere on the label */
  cursor: pointer;
  user-select: none;
}

/* Switch Input (hidden but functional)
 * -------------------------------------------------------------------------- */

.switch__input {
  /* Visually hidden but still accessible */
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
  pointer-events: none;
}

/* Switch Track
 * -------------------------------------------------------------------------- */

.switch__track {
  /* Structure */
  position: relative;
  display: inline-block;
  flex-shrink: 0;

  /* Sizing */
  width: 52px;
  height: 32px;

  /* Appearance */
  background: var(--color-bg--surface);
  border: 2px solid var(--color-border);
  border-radius: 16px;

  /* Transition */
  transition: all 200ms cubic-bezier(0.4, 0, 0.2, 1);
}

/* Track states */
.switch__input:checked ~ .switch__track {
  background: var(--switch-track-color);
  border-color: var(--switch-border-color);
}

.switch__input:disabled ~ .switch__track {
  opacity: 0.38;
  cursor: not-allowed;
}

.switch:hover .switch__input:not(:disabled) ~ .switch__track {
  border-color: var(--switch-border-color);
}

.switch__input:focus-visible ~ .switch__track {
  outline: 3px solid var(--switch-border-color);
  outline-offset: 2px;
}

/* Switch Thumb
 * -------------------------------------------------------------------------- */

.switch__thumb {
  /* Structure */
  position: absolute;
  top: 50%;
  left: 2px;
  transform: translateY(-50%);

  /* Sizing */
  width: 24px;
  height: 24px;

  /* Appearance */
  background: var(--color-ink--surface);
  border-radius: 50%;
  box-shadow:
    0 1px 3px 0 oklch(0 0 0 / 0.2),
    0 1px 2px 0 oklch(0 0 0 / 0.12);

  /* Transition */
  transition: all 200ms cubic-bezier(0.4, 0, 0.2, 1);

  /* Icon container */
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Thumb states */
.switch__input:checked ~ .switch__track .switch__thumb {
  background: var(--switch-thumb-color);
  width: 28px;
  height: 28px;
  left: calc(100% - 30px);
}

.switch__input:disabled ~ .switch__track .switch__thumb {
  background: var(--color-ink--surface);
  opacity: 0.5;
}

/* Thumb hover state */
.switch:hover .switch__input:not(:disabled):not(:checked) ~ .switch__track .switch__thumb {
  width: 28px;
  height: 28px;
  left: 0;
}

.switch:hover .switch__input:not(:disabled):checked ~ .switch__track .switch__thumb {
  width: 28px;
  height: 28px;
  left: calc(100% - 30px);
}

/* Switch Icon (optional)
 * -------------------------------------------------------------------------- */

.switch__icon {
  width: 16px;
  height: 16px;
  color: var(--color-bg--surface);
  opacity: 0;
  transition: opacity 200ms ease;
}

.switch__input:checked ~ .switch__track .switch__icon {
  opacity: 1;
}

/* Switch Label
 * -------------------------------------------------------------------------- */

.switch__label {
  /* Typography */
  font-size: 0.875rem;
  line-height: 1.5;
  color: var(--color-ink--surface);

  /* Prevent selection */
  user-select: none;
}

.switch__input:disabled ~ .switch__label {
  opacity: 0.38;
  cursor: not-allowed;
}

/* Switch Variants
 * -------------------------------------------------------------------------- */

/* Small Switch */
.switch--small .switch__track {
  width: 44px;
  height: 24px;
  border-radius: 12px;
}

.switch--small .switch__thumb {
  width: 18px;
  height: 18px;
}

.switch--small .switch__input:checked ~ .switch__track .switch__thumb {
  width: 20px;
  height: 20px;
  left: calc(100% - 22px);
}

.switch--small .switch__icon {
  width: 12px;
  height: 12px;
}

/* Large Switch */
.switch--large .switch__track {
  width: 60px;
  height: 40px;
  border-radius: 20px;
}

.switch--large .switch__thumb {
  width: 32px;
  height: 32px;
}

.switch--large .switch__input:checked ~ .switch__track .switch__thumb {
  width: 36px;
  height: 36px;
  left: calc(100% - 38px);
}

.switch--large .switch__icon {
  width: 20px;
  height: 20px;
}

/* Color Variants
 * -------------------------------------------------------------------------- */

.switch--secondary {
  --switch-track-color: var(--color-bg--secondary-container);
  --switch-border-color: var(--color-secondary);
  --switch-thumb-color: var(--color-secondary);
}

.switch--tertiary {
  --switch-track-color: var(--color-bg--tertiary-container);
  --switch-border-color: var(--color-tertiary);
  --switch-thumb-color: var(--color-tertiary);
}

.switch--error {
  --switch-track-color: var(--color-bg--error-container);
  --switch-border-color: var(--color-error);
  --switch-thumb-color: var(--color-error);
}

/* Label Position
 * -------------------------------------------------------------------------- */

.switch--label-left {
  flex-direction: row-reverse;
}

/* Icon-only (visually hidden label)
 * -------------------------------------------------------------------------- */

.switch--icon-only .switch__label {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Switch Group (for multiple switches)
 * -------------------------------------------------------------------------- */

.switch-group {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.switch-group--horizontal {
  flex-direction: row;
  flex-wrap: wrap;
  gap: 2rem;
}

/* Loading state
 * -------------------------------------------------------------------------- */

.switch--loading .switch__track {
  animation: pulse 1.5s cubic-bezier(0.4, 0, 0.6, 1) infinite;
}

@keyframes pulse {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
}

/* Responsive behavior
 * -------------------------------------------------------------------------- */

@media (max-width: 40em) {
  .switch-group--horizontal {
    flex-direction: column;
  }
}

/* Examples of usage:
 * -------------------------------------------------------------------------- *
 * Basic switch:
 * <label class="switch">
 *   <input type="checkbox" class="switch__input">
 *   <span class="switch__track">
 *     <span class="switch__thumb"></span>
 *   </span>
 *   <span class="switch__label">Enable notifications</span>
 * </label>
 *
 * Switch with icon:
 * <label class="switch">
 *   <input type="checkbox" class="switch__input" checked>
 *   <span class="switch__track">
 *     <span class="switch__thumb">
 *       <%= icon(:check, size: 16, class: "switch__icon") %>
 *     </span>
 *   </span>
 *   <span class="switch__label">Enabled</span>
 * </label>
 *
 * Error switch:
 * <label class="switch switch--error">
 *   <input type="checkbox" class="switch__input">
 *   <span class="switch__track">
 *     <span class="switch__thumb"></span>
 *   </span>
 *   <span class="switch__label">Delete account</span>
 * </label>
 */