@use "../../wc";

:host {
  --zn-toggle-margin: 8px 0;
  display: block;
}

.switch__wrapper {
  display: flex;
  flex-direction: column;

  label {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    cursor: pointer;
  }
}

.switch__input-wrapper {
  box-sizing: border-box;
  text-align: left;
  position: relative;
  display: inline-flex;
  width: 42px;
  height: 24px;
  flex-shrink: 0;
  cursor: pointer;
  border-radius: 12px;
  align-items: center;
  background-color: rgb(var(--zn-color-tab));
  // The unchecked track needs an edge of its own. --zn-color-tab sits close to
  // the surface behind it on dark themes, which left the switch reading as a
  // free-floating knob with no discoverable control boundary. The checked state
  // below repaints the border in the accent colour.
  border: 0.92px solid rgb(var(--zn-border-color));
  margin: var(--zn-toggle-margin);
  transition: color .2s ease-in-out, background-color .4s ease-in-out, border-color .2s ease-in-out;
}

:host(:focus) .switch__input-wrapper {
  outline: rgb(var(--zn-primary)) solid 1px;
}

:host([checked]) .switch__input-wrapper {
  background-color: rgb(var(--zn-color-primary));
  border-color: rgb(var(--zn-color-primary));

  .switch__control {
    left: calc(100% - 20.32px - 0.92px);
    // The knob carries the on/off state, so it takes the on-primary ink: a
    // white knob on a light accent (neon lime, amber) is invisible.
    background: rgb(var(--zn-color-on-primary, 255, 255, 255));
  }
}

.switch {
  height: 100%;
  width: 100%;
  padding: 0;
  margin: 0;
  cursor: pointer;
}

.switch-label {
  @include wc.text-style(input-label);

  margin: 0;
}

.switch__wrapper--small .switch-label {
  font-size: var(--zn-input-label-font-size-small);
}

.switch__description {
  display: none;
  margin-top: var(--zn-spacing-2x-small);
}

.switch__wrapper--has-description .switch__description {
  @include wc.text-style(paragraph);

  display: block;
  color: rgb(var(--zn-color-muted-text));
}

.switch__wrapper--small .switch__description {
  font-size: var(--zn-font-size-x-small);
}

.switch__wrapper--large .switch-label {
  font-size: var(--zn-input-label-font-size-large);
}

.switch__control {
  pointer-events: none;
  position: absolute;
  top: 50%;
  left: 0.92px;
  width: 20.32px;
  height: 20.32px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 4px 0 rgba(var(--zn-color-text), 0.1);

  transform: translateY(-50%);
  transition: left .3s ease-in-out, background-color .4s ease-in-out;
  outline: none;
}

.switch__input {
  position: absolute;
  opacity: 0;
  padding: 0;
  margin: 0;
  pointer-events: none;
}

// sizes
.switch__wrapper--small .switch__input-wrapper {
  height: 20px;
  width: 35px;
}

.switch__wrapper--small .switch__control {
  width: 14px;
  height: 14px;
}

:host([checked]) .switch__wrapper--small .switch__input-wrapper {
  .switch__control {
    left: calc(100% - 14px - 0.92px);
  }
}

// Label position
.switch__wrapper--label-left label,
.switch__wrapper--label-right label {
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.switch__wrapper--label-left label {
  flex-direction: row;
}

.switch__wrapper--label-right label {
  flex-direction: row-reverse;
}

// Support inline label
:host([inline]) {
  display: inline-block;
}

.switch__wrapper--inline label {
  flex: none;
  flex-direction: row;
  align-items: center;
  justify-content: flex-start;
  gap: 8px;

  .switch__input {
    margin-inline: 1px;
    margin-inline-end: 1px;
  }
}

.switch__wrapper--inline.switch__wrapper--label-right label {
  flex-direction: row-reverse;
}
