/* Cortex Component Library — Stage B1
 *
 * Vanilla CSS port of the shadcn/ui Stage-1 primitives. Each component
 * is invoked via a base class plus optional variant classes (BEM-light:
 * `.cx-button.cx-button-primary`). Consumers should NEVER use literal
 * colours, radii or spacing — pull everything from design-tokens.css.
 *
 * Components in this file:
 *   .cx-button + variants (primary | secondary | ghost | outline | destructive | link)
 *   .cx-button-icon (square icon-only button)
 *   .cx-input, .cx-textarea, .cx-label, .cx-field, .cx-field-error
 *   .cx-badge + variants (default | secondary | outline | success | warning | destructive)
 *   .cx-chip (interactive, removable)
 *   .cx-card + .cx-card-header / .cx-card-body / .cx-card-footer
 *   .cx-kbd (keyboard-shortcut hint)
 *   .cx-separator (horizontal hairline)
 *   .cx-spinner (loading indicator)
 *   .cx-focus-ring helper class for non-control elements that gain focus
 *
 * Accessibility baseline (applies to every interactive control):
 *   - visible :focus-visible outline using --color-ring
 *   - min hit target --hit-md (36 px) or --hit-lg (44 px) for primary CTAs
 *   - aria-busy support on async controls
 *   - prefers-reduced-motion neutralises transitions (see design-tokens.css)
 */

/* ============================================================ */
/* Focus ring                                                   */
/* ============================================================ */

:where(button, [role="button"], a, input, textarea, select, [tabindex]:not([tabindex="-1"])):focus-visible,
.cx-focus-ring:focus-visible {
  outline: 2px solid var(--color-ring);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

/* ============================================================ */
/* Button                                                       */
/* ============================================================ */

.cx-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);

  height: var(--hit-md);
  padding: 0 var(--space-4);
  min-width: 0;

  border: 1px solid transparent;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--color-text);

  font: inherit;
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  line-height: 1;
  text-decoration: none;
  white-space: nowrap;

  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;

  transition:
    background var(--duration-fast) var(--ease-out),
    border-color var(--duration-fast) var(--ease-out),
    color var(--duration-fast) var(--ease-out),
    box-shadow var(--duration-fast) var(--ease-out),
    opacity var(--duration-fast) var(--ease-out);
}

.cx-button:disabled,
.cx-button[aria-disabled="true"] {
  cursor: not-allowed;
  opacity: 0.55;
}

.cx-button .icon,
.cx-button > svg {
  width: 16px;
  height: 16px;
  flex: 0 0 16px;
}

/* Variants */
.cx-button-primary {
  background: var(--color-accent);
  color: var(--color-accent-foreground);
  border-color: var(--color-accent);
  box-shadow: var(--shadow-xs);
}
.cx-button-primary:hover:not(:disabled) {
  background: var(--color-accent-hover);
  border-color: var(--color-accent-hover);
}
.cx-button-primary:active:not(:disabled) {
  background: var(--color-accent-active);
}

.cx-button-secondary {
  background: var(--color-surface);
  color: var(--color-text);
  border-color: var(--color-border);
  box-shadow: var(--shadow-xs);
}
.cx-button-secondary:hover:not(:disabled) {
  background: var(--color-surface-subtle);
  border-color: var(--color-border-strong);
}

.cx-button-ghost {
  background: transparent;
  color: var(--color-text);
}
.cx-button-ghost:hover:not(:disabled) {
  background: var(--color-surface-subtle);
}

.cx-button-outline {
  background: transparent;
  color: var(--color-text);
  border-color: var(--color-border);
}
.cx-button-outline:hover:not(:disabled) {
  background: var(--color-surface-subtle);
  border-color: var(--color-border-strong);
}

.cx-button-destructive {
  background: var(--color-destructive);
  color: var(--color-destructive-foreground);
  border-color: var(--color-destructive);
  box-shadow: var(--shadow-xs);
}
.cx-button-destructive:hover:not(:disabled) {
  background: var(--color-destructive-hover);
  border-color: var(--color-destructive-hover);
}

.cx-button-link {
  background: transparent;
  color: var(--color-accent);
  height: auto;
  padding: 0;
  font-weight: var(--weight-medium);
  text-decoration: underline;
  text-underline-offset: 3px;
}
.cx-button-link:hover:not(:disabled) {
  color: var(--color-accent-hover);
}

.cx-button-active {
  background: var(--color-accent-soft);
  color: var(--color-accent);
  border-color: var(--color-accent);
}

/* Sizes */
.cx-button-sm {
  height: var(--hit-sm);
  padding: 0 var(--space-3);
  font-size: var(--text-xs);
  border-radius: var(--radius-sm);
}
.cx-button-lg {
  height: var(--hit-lg);
  padding: 0 var(--space-5);
  font-size: var(--text-md);
}

/* Icon-only — square, lucide icons rendered at 16-20 px */
.cx-button-icon {
  width: var(--hit-md);
  padding: 0;
  flex: 0 0 auto;
}
.cx-button-icon.cx-button-sm { width: var(--hit-sm); }
.cx-button-icon.cx-button-lg { width: var(--hit-lg); }
.cx-button-icon .icon,
.cx-button-icon > svg {
  width: 18px;
  height: 18px;
  flex: 0 0 18px;
}

/* aria-busy renders an inline spinner via opacity */
.cx-button[aria-busy="true"] { cursor: progress; }
.cx-button[aria-busy="true"] > * { opacity: 0.5; }

/* ============================================================ */
/* Input / Textarea / Field / Label                             */
/* ============================================================ */

.cx-field {
  display: flex;
  flex-direction: column;
  gap: var(--space-1_5);
  min-width: 0;
}

.cx-label {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1_5);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  color: var(--color-text);
  line-height: 1;
}
.cx-label[data-required]::after {
  content: "*";
  color: var(--color-destructive);
}

.cx-input,
.cx-textarea {
  width: 100%;
  height: var(--hit-md);
  padding: 0 var(--space-3);
  border: 1px solid var(--color-border-input);
  border-radius: var(--radius-md);
  background: var(--color-surface);
  color: var(--color-text);
  font: inherit;
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  line-height: 1;
  transition:
    border-color var(--duration-fast) var(--ease-out),
    box-shadow var(--duration-fast) var(--ease-out),
    background var(--duration-fast) var(--ease-out);
}

.cx-textarea {
  height: auto;
  min-height: calc(var(--hit-md) * 2);
  padding: var(--space-2_5) var(--space-3);
  line-height: var(--leading-normal);
  resize: vertical;
}

.cx-input::placeholder,
.cx-textarea::placeholder {
  color: var(--color-text-placeholder);
}

.cx-input:hover:not(:disabled),
.cx-textarea:hover:not(:disabled) {
  border-color: var(--color-border-strong);
}

.cx-input:focus,
.cx-textarea:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 3px var(--color-ring);
}

.cx-input:disabled,
.cx-textarea:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  background: var(--color-surface-subtle);
}

.cx-input[aria-invalid="true"],
.cx-textarea[aria-invalid="true"] {
  border-color: var(--color-destructive);
}
.cx-input[aria-invalid="true"]:focus,
.cx-textarea[aria-invalid="true"]:focus {
  box-shadow: 0 0 0 3px var(--color-destructive-soft);
}

.cx-field-help {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  line-height: var(--leading-snug);
}
.cx-field-error {
  font-size: var(--text-xs);
  color: var(--color-destructive);
  line-height: var(--leading-snug);
}

/* ============================================================ */
/* Badge                                                        */
/* ============================================================ */

.cx-badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1_5);
  min-width: 0;
  max-width: min(100%, 18rem);
  min-height: var(--hit-xs);
  padding: 0 var(--space-2);
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  background: var(--color-surface-subtle);
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
  line-height: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.cx-badge-secondary {
  background: var(--color-surface);
  border-color: var(--color-border);
  color: var(--color-text-muted);
}
.cx-badge-outline {
  background: transparent;
  border-color: var(--color-border);
  color: var(--color-text-muted);
}
.cx-badge-success {
  background: var(--color-success-soft);
  color: var(--color-success);
}
.cx-badge-warning {
  background: var(--color-warning-soft);
  color: var(--color-warning-foreground);
}
.cx-badge-destructive {
  background: var(--color-destructive-soft);
  color: var(--color-destructive);
}
.cx-badge-info {
  background: var(--color-info-soft);
  color: var(--color-info);
}
.cx-badge-accent {
  background: var(--color-accent-soft);
  color: var(--color-accent);
}

/* ============================================================ */
/* Chip — interactive, button-shaped tag                         */
/* ============================================================ */

.cx-chip {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1_5);
  height: 26px;
  padding: 0 var(--space-2_5);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-full);
  background: var(--color-surface);
  color: var(--color-text);
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
  line-height: 1;
  cursor: default;
  transition:
    background var(--duration-fast) var(--ease-out),
    border-color var(--duration-fast) var(--ease-out),
    color var(--duration-fast) var(--ease-out);
}

button.cx-chip,
a.cx-chip {
  cursor: pointer;
  user-select: none;
}
button.cx-chip:hover,
a.cx-chip:hover {
  background: var(--color-surface-subtle);
  border-color: var(--color-border-strong);
}

.cx-chip-removable {
  padding-right: var(--space-1);
}
.cx-chip-remove {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border: 0;
  background: transparent;
  border-radius: var(--radius-full);
  cursor: pointer;
  color: var(--color-text-muted);
}
.cx-chip-remove:hover { color: var(--color-destructive); }

/* Semantic chips */
.cx-chip-accent { background: var(--color-accent-soft); border-color: transparent; color: var(--color-accent); }
.cx-chip-success { background: var(--color-success-soft); border-color: transparent; color: var(--color-success); }
.cx-chip-warning { background: var(--color-warning-soft); border-color: transparent; color: var(--color-warning-foreground); }
.cx-chip-danger { background: var(--color-destructive-soft); border-color: transparent; color: var(--color-destructive); }

/* ============================================================ */
/* Card                                                         */
/* ============================================================ */

.cx-card {
  display: flex;
  flex-direction: column;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xs);
  /* overflow stays visible so floating children (suggestion lists,
   * popovers anchored inside the card) aren't clipped. Border-radius
   * is preserved by the corners of the header/footer alone. */
  overflow: visible;
}

.cx-card-header {
  display: flex;
  align-items: center;
  gap: var(--space-2_5);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--color-border);
  background: var(--color-surface-subtle);
  /* Inherit the parent card's top-corner radius so the tinted strip
   * stays inside the rounded outline now that the card uses overflow:
   * visible. */
  border-top-left-radius: var(--radius-lg);
  border-top-right-radius: var(--radius-lg);
}
.cx-card-header-icon {
  display: inline-flex;
  color: var(--color-text-muted);
}
.cx-card-header-icon .icon,
.cx-card-header-icon > svg {
  width: 16px;
  height: 16px;
}
.cx-card-header-title {
  margin: 0;
  font-size: var(--text-sm);
  font-weight: var(--weight-semibold);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--color-text-muted);
}
.cx-card-header-tools {
  margin-left: auto;
  display: inline-flex;
  gap: var(--space-1);
}

.cx-card-body {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
}

.cx-card-footer {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
  border-top: 1px solid var(--color-border);
  background: var(--color-surface-subtle);
  border-bottom-left-radius: var(--radius-lg);
  border-bottom-right-radius: var(--radius-lg);
}

/* ============================================================ */
/* Keyboard shortcut hint                                       */
/* ============================================================ */

.cx-kbd {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  padding: 0 4px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-surface-subtle);
  color: var(--color-text-muted);
  font-family: var(--font-mono);
  font-size: var(--text-2xs);
  line-height: 1;
}

/* ============================================================ */
/* Separator                                                    */
/* ============================================================ */

.cx-separator {
  display: block;
  width: 100%;
  height: 1px;
  background: var(--color-border);
  border: 0;
  margin: var(--space-3) 0;
}
.cx-separator-vertical {
  width: 1px;
  height: auto;
  align-self: stretch;
  margin: 0 var(--space-2);
}

/* ============================================================ */
/* Spinner                                                      */
/* ============================================================ */

.cx-spinner {
  width: 16px;
  height: 16px;
  border-radius: var(--radius-full);
  border: 2px solid var(--color-border);
  border-top-color: var(--color-accent);
  animation: cx-spin 0.9s linear infinite;
}
.cx-spinner-lg { width: 28px; height: 28px; border-width: 3px; }

@keyframes cx-spin {
  to { transform: rotate(360deg); }
}
