import React from 'react';
import { Icon } from '../core/Icon';

const CSS = `
.ct-check { display: inline-flex; align-items: center; gap: 9px; cursor: pointer; font: var(--text-body); color: var(--ink-1); }
.ct-check--disabled { opacity: 0.5; cursor: not-allowed; }
.ct-check__input { position: absolute; opacity: 0; width: 0; height: 0; }
.ct-check__box {
  width: 16px; height: 16px; flex-shrink: 0;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--surface-card);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-1);
  color: transparent;
  transition: background var(--duration-fast) var(--ease-out), border-color var(--duration-fast) var(--ease-out);
}
.ct-check:hover:not(.ct-check--disabled) .ct-check__box { border-color: var(--border-strong); }
.ct-check__input:checked + .ct-check__box { background: var(--accent); border-color: var(--accent); color: var(--ink-inverse); }
.ct-check__input:focus-visible + .ct-check__box { outline: 2px solid var(--focus-ring); outline-offset: 2px; }
`;
(function inject() {
  if (typeof document !== 'undefined' && !document.getElementById('ct-check-css')) {
    const s = document.createElement('style'); s.id = 'ct-check-css'; s.textContent = CSS;
    document.head.appendChild(s);
  }
})();

export function Checkbox({ label, checked, onChange, disabled = false, style, ...rest }) {
  return (
    <label className={`ct-check${disabled ? ' ct-check--disabled' : ''}`} style={style}>
      <input
        type="checkbox"
        className="ct-check__input"
        checked={checked}
        onChange={onChange}
        disabled={disabled}
        {...rest}
      />
      <span className="ct-check__box"><Icon name="check" size={12} strokeWidth={2.5} /></span>
      {label ? <span>{label}</span> : null}
    </label>
  );
}
