import React from 'react';

const CSS = `
.ct-switch { display: inline-flex; align-items: center; gap: 9px; cursor: pointer; font: var(--text-body); color: var(--ink-1); }
.ct-switch--disabled { opacity: 0.5; cursor: not-allowed; }
.ct-switch__input { position: absolute; opacity: 0; width: 0; height: 0; }
.ct-switch__track {
  width: 34px; height: 20px; flex-shrink: 0;
  border-radius: var(--radius-round);
  background: var(--paper-3);
  border: 1px solid var(--line-2);
  position: relative;
  transition: background var(--duration-base) var(--ease-out), border-color var(--duration-base) var(--ease-out);
}
.ct-switch__track::after {
  content: '';
  position: absolute; top: 2px; left: 2px;
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--surface-card);
  box-shadow: var(--shadow-1);
  transition: transform var(--duration-base) var(--ease-out);
}
.ct-switch__input:checked + .ct-switch__track { background: var(--accent); border-color: var(--accent); }
.ct-switch__input:checked + .ct-switch__track::after { transform: translateX(14px); }
.ct-switch__input:focus-visible + .ct-switch__track { outline: 2px solid var(--focus-ring); outline-offset: 2px; }
`;
(function inject() {
  if (typeof document !== 'undefined' && !document.getElementById('ct-switch-css')) {
    const s = document.createElement('style'); s.id = 'ct-switch-css'; s.textContent = CSS;
    document.head.appendChild(s);
  }
})();

export function Switch({ label, checked, onChange, disabled = false, style, ...rest }) {
  return (
    <label className={`ct-switch${disabled ? ' ct-switch--disabled' : ''}`} style={style}>
      <input
        type="checkbox"
        role="switch"
        className="ct-switch__input"
        checked={checked}
        onChange={onChange}
        disabled={disabled}
        {...rest}
      />
      <span className="ct-switch__track"></span>
      {label ? <span>{label}</span> : null}
    </label>
  );
}
