/* BHD UI Elements — the vendor-neutral element layer.
 *
 * WHY THIS FILE EXISTS
 * The registry (registry.bhd.om, @bhd) only reaches React products. Twelve of
 * our sites are PHP, Laravel, WordPress or plain static, and can never take a
 * React component. This file is the layer that reaches them: pure CSS, no JS,
 * no build step, brand-agnostic, so `data-brand` reskins every element for all
 * 12 brands automatically.
 *
 * PROVENANCE / LICENCE
 * Pattern family adapted from Uiverse.io, whose content is published under the
 * MIT License (https://uiverse.io). These are BHD re-implementations written
 * against our own tokens rather than copies: MIT permits reuse, modification
 * and redistribution provided the notice travels with the code, so this header
 * is the notice. Do not strip it.
 *
 * CONTRACT AND FALLBACKS
 * Every semantic name now carries a fallback, because a site without the token
 * contract used to render this layer SILENTLY TRANSPARENT: measured on
 * almahamarble.om, where the sheet loaded, the class applied, and the badge
 * still came out rgba(0,0,0,0) because --primary was empty. A layer that looks
 * like it did nothing is the worst possible failure.
 *
 * The fallbacks are CSS system colours (Canvas / CanvasText) and currentColor,
 * not BHD brand values, so a site with no tokens gets something visible and
 * neutral that follows its own text colour and colour scheme. A site that wants
 * its real brand supplies the contract (see contract.css) and overrides these.
 *
 * CONTRACT
 *   - Reads ONLY tokens.css semantic vars. Never a hard-coded brand colour.
 *   - Logical properties only (inline-start/end, margin-inline). Never
 *     left/right, so Arabic mirrors for free.
 *   - Never letter-spacing on text that may be Arabic.
 *   - Every animation is disabled under prefers-reduced-motion.
 *   - Real form controls (appearance:none on the input), never styled divs,
 *     so keyboard, VoiceOver and NVDA work without extra ARIA.
 */

:root {
  --bhd-el-ring: var(--ring, var(--primary, currentColor));
  --bhd-el-focus: 0 0 0 3px color-mix(in srgb, var(--bhd-el-ring) 32%, transparent);
  --bhd-el-muted: var(--muted, color-mix(in srgb, var(--foreground, CanvasText) 6%, transparent));
  --bhd-el-track: color-mix(in srgb, var(--foreground, CanvasText) 14%, transparent);
  --bhd-el-ease: var(--ease-spring, cubic-bezier(.2, .8, .2, 1));
  --bhd-el-fast: var(--duration-fast, 120ms);
  --bhd-el-base: var(--duration-slow, 220ms);
}

/* ============================================================ 1. SWITCH */

.bhd-switch {
  appearance: none;
  -webkit-appearance: none;
  position: relative;
  flex: 0 0 auto;
  inline-size: 2.75rem;
  block-size: 1.5rem;
  margin: 0;
  border-radius: 999px;
  background: var(--bhd-el-track);
  cursor: pointer;
  transition: background var(--bhd-el-base) var(--bhd-el-ease);
  -webkit-tap-highlight-color: transparent;
}
.bhd-switch::before {
  content: "";
  position: absolute;
  inset-block-start: 3px;
  inset-inline-start: 3px;
  inline-size: calc(1.5rem - 6px);
  block-size: calc(1.5rem - 6px);
  border-radius: 999px;
  background: var(--background, #fff);
  box-shadow: 0 1px 3px rgb(0 0 0 / .28);
  transition: transform var(--bhd-el-base) var(--bhd-el-ease);
}
.bhd-switch:checked { background: var(--primary, currentColor); }
/* translateX, not translate-inline: the track never mirrors visually */
.bhd-switch:checked::before { transform: translateX(1.25rem); }
[dir="rtl"] .bhd-switch:checked::before { transform: translateX(-1.25rem); }
.bhd-switch:focus-visible { outline: none; box-shadow: var(--bhd-el-focus); }
.bhd-switch:disabled { opacity: .45; cursor: not-allowed; }

/* ================================================= 2. CHECKBOX + RADIO */

.bhd-check,
.bhd-radio {
  appearance: none;
  -webkit-appearance: none;
  position: relative;
  flex: 0 0 auto;
  inline-size: 1.125rem;
  block-size: 1.125rem;
  margin: 0;
  border: 1.5px solid var(--border, var(--bhd-el-track));
  background: var(--background, transparent);
  cursor: pointer;
  display: inline-grid;
  place-content: center;
  transition: background var(--bhd-el-fast) ease, border-color var(--bhd-el-fast) ease;
}
.bhd-check { border-radius: calc(var(--radius, .5rem) * .5); }
.bhd-radio { border-radius: 999px; }
.bhd-check::after {
  content: "";
  inline-size: .62rem;
  block-size: .34rem;
  border-inline-start: 2px solid currentColor;
  border-block-end: 2px solid currentColor;
  transform: rotate(-45deg) translate(0, -8%) scale(0);
  transform-origin: center;
  transition: transform var(--bhd-el-fast) var(--bhd-el-ease);
  color: var(--primary-foreground, #fff);
}
.bhd-radio::after {
  content: "";
  inline-size: .44rem;
  block-size: .44rem;
  border-radius: 999px;
  background: var(--primary-foreground, #fff);
  transform: scale(0);
  transition: transform var(--bhd-el-fast) var(--bhd-el-ease);
}
.bhd-check:checked,
.bhd-radio:checked { background: var(--primary, currentColor); border-color: var(--primary, currentColor); }
.bhd-check:checked::after { transform: rotate(-45deg) translate(0, -8%) scale(1); }
.bhd-radio:checked::after { transform: scale(1); }
.bhd-check:focus-visible,
.bhd-radio:focus-visible { outline: none; box-shadow: var(--bhd-el-focus); }
.bhd-check:disabled,
.bhd-radio:disabled { opacity: .45; cursor: not-allowed; }

/* ========================================================== 3. SPINNERS */

.bhd-spinner {
  display: inline-block;
  inline-size: 1.25rem;
  block-size: 1.25rem;
  border: 2px solid color-mix(in srgb, currentColor 22%, transparent);
  border-block-start-color: currentColor;
  border-radius: 999px;
  animation: bhd-spin .7s linear infinite;
}
.bhd-spinner--sm { inline-size: .875rem; block-size: .875rem; border-width: 1.5px; }
.bhd-spinner--lg { inline-size: 2rem; block-size: 2rem; border-width: 3px; }

.bhd-dots { display: inline-flex; gap: .25rem; align-items: center; }
.bhd-dots > i {
  inline-size: .375rem;
  block-size: .375rem;
  border-radius: 999px;
  background: currentColor;
  animation: bhd-bounce 1.2s var(--bhd-el-ease) infinite;
}
.bhd-dots > i:nth-child(2) { animation-delay: .15s; }
.bhd-dots > i:nth-child(3) { animation-delay: .3s; }

@keyframes bhd-spin { to { transform: rotate(360deg); } }
@keyframes bhd-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: .45; }
  30%           { transform: translateY(-.3rem); opacity: 1; }
}

/* ========================================================== 4. PROGRESS */

.bhd-progress {
  --bhd-progress-value: 0%;
  position: relative;
  inline-size: 100%;
  block-size: .375rem;
  overflow: hidden;
  border-radius: 999px;
  background: var(--bhd-el-muted);
}
.bhd-progress::after {
  content: "";
  position: absolute;
  inset-block: 0;
  inset-inline-start: 0;
  inline-size: var(--bhd-progress-value);
  border-radius: inherit;
  background: var(--primary, currentColor);
  transition: inline-size var(--bhd-el-base) var(--bhd-el-ease);
}
.bhd-progress--indeterminate::after {
  inline-size: 40%;
  animation: bhd-indeterminate 1.35s var(--bhd-el-ease) infinite;
}
.bhd-progress--lg { block-size: .625rem; }

@keyframes bhd-indeterminate {
  0%   { inset-inline-start: 0; }
  50%  { inset-inline-start: 60%; }
  100% { inset-inline-start: 0; }
}

/* ============================================================= 5. BADGE */

.bhd-badge {
  display: inline-flex;
  align-items: center;
  gap: .375rem;
  padding-inline: .5rem;
  padding-block: .125rem;
  border-radius: 999px;
  font-size: .75rem;
  font-weight: 500;
  line-height: 1.4;
  white-space: nowrap;
  background: var(--bhd-el-muted);
  color: var(--foreground, CanvasText);
}
.bhd-badge--primary { background: color-mix(in srgb, var(--primary, currentColor) 14%, transparent); color: var(--primary, currentColor); }
.bhd-badge--success { background: color-mix(in srgb, var(--success, #16a34a) 16%, transparent); color: var(--success, #16a34a); }
.bhd-badge--danger  { background: color-mix(in srgb, var(--destructive, #dc2626) 16%, transparent); color: var(--destructive, #dc2626); }
.bhd-badge--warning { background: color-mix(in srgb, var(--warning, #d97706) 18%, transparent); color: var(--warning, #d97706); }

.bhd-badge__dot {
  inline-size: .4375rem;
  block-size: .4375rem;
  border-radius: 999px;
  background: currentColor;
  flex: 0 0 auto;
}
.bhd-badge--live .bhd-badge__dot {
  position: relative;
  background: var(--success, #16a34a);
}
.bhd-badge--live .bhd-badge__dot::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: inherit;
  animation: bhd-ping 1.6s var(--bhd-el-ease) infinite;
}
@keyframes bhd-ping {
  0%   { transform: scale(1);   opacity: .7; }
  70%  { transform: scale(2.6); opacity: 0;  }
  100% { transform: scale(2.6); opacity: 0;  }
}

/* ============================================================== 6. CHIP */

.bhd-chip {
  display: inline-flex;
  align-items: center;
  gap: .375rem;
  padding-inline: .625rem;
  padding-block: .25rem;
  border: 1px solid var(--border, var(--bhd-el-track));
  border-radius: 999px;
  background: var(--background, transparent);
  font-size: .8125rem;
  line-height: 1.4;
  color: var(--foreground, CanvasText);
}
.bhd-chip__x {
  display: inline-grid;
  place-content: center;
  inline-size: 1rem;
  block-size: 1rem;
  margin-inline-end: -.25rem;
  padding: 0;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--muted-foreground, currentColor);
  cursor: pointer;
  font: inherit;
  line-height: 1;
}
.bhd-chip__x:hover { background: var(--bhd-el-muted); color: var(--foreground, CanvasText); }
.bhd-chip__x:focus-visible { outline: none; box-shadow: var(--bhd-el-focus); }

/* ========================================================= 7. TOOLTIP */

/* Pure CSS, no JS. Markup:
 *   <span class="bhd-tip" data-bhd-tip="Text">…</span>
 * The tooltip is decorative: keep the same words in aria-describedby content
 * or a title on the focusable child if the text is essential. */
.bhd-tip { position: relative; display: inline-flex; }
.bhd-tip::after {
  content: attr(data-bhd-tip);
  position: absolute;
  inset-block-end: calc(100% + .5rem);
  inset-inline-start: 50%;
  translate: -50% 0;
  z-index: 60;
  max-inline-size: 16rem;
  width: max-content;
  padding: .3125rem .5rem;
  border-radius: calc(var(--radius, .5rem) * .6);
  background: var(--foreground, CanvasText);
  color: var(--background, Canvas);
  font-size: .75rem;
  font-weight: 500;
  line-height: 1.45;
  text-align: center;
  opacity: 0;
  transform: translateY(2px);
  pointer-events: none;
  transition: opacity var(--bhd-el-fast) ease, transform var(--bhd-el-fast) ease;
}
[dir="rtl"] .bhd-tip::after { translate: 50% 0; }
.bhd-tip:hover::after,
.bhd-tip:focus-within::after { opacity: 1; transform: translateY(0); }
.bhd-tip--end::after { inset-inline-start: auto; inset-inline-end: 0; translate: 0 0; }

/* ===================================================== 8. SEGMENTED NAV */

.bhd-segmented {
  display: inline-flex;
  gap: .125rem;
  padding: .1875rem;
  border-radius: calc(var(--radius, .5rem) * .8);
  background: var(--bhd-el-muted);
}
.bhd-segmented > button,
.bhd-segmented > label {
  display: inline-flex;
  align-items: center;
  gap: .375rem;
  padding-inline: .75rem;
  padding-block: .3125rem;
  border: 0;
  border-radius: calc(var(--radius, .5rem) * .6);
  background: transparent;
  color: var(--muted-foreground, var(--foreground, CanvasText));
  font: inherit;
  font-size: .8125rem;
  font-weight: 500;
  cursor: pointer;
  transition: background var(--bhd-el-fast) ease, color var(--bhd-el-fast) ease;
}
.bhd-segmented > button:hover,
.bhd-segmented > label:hover { color: var(--foreground, CanvasText); }
.bhd-segmented > [aria-selected="true"],
.bhd-segmented > [aria-pressed="true"],
.bhd-segmented > [data-active="true"] {
  background: var(--background, #fff);
  color: var(--foreground, CanvasText);
  box-shadow: 0 1px 2px rgb(0 0 0 / .12);
}
.bhd-segmented :focus-visible { outline: none; box-shadow: var(--bhd-el-focus); }

/* ========================================================== 9. DIVIDER */

.bhd-divider {
  display: flex;
  align-items: center;
  gap: .75rem;
  color: var(--muted-foreground, var(--foreground, CanvasText));
  font-size: .75rem;
  font-weight: 500;
  text-transform: none;
}
.bhd-divider::before,
.bhd-divider::after {
  content: "";
  flex: 1;
  block-size: 1px;
  background: var(--border, var(--bhd-el-track));
}
.bhd-divider:not(:empty)::before { flex: 1; }
.bhd-divider--plain::before,
.bhd-divider--plain::after { content: none; }
.bhd-divider--plain { block-size: 1px; background: var(--border, var(--bhd-el-track)); }

/* ======================================================== 10. CARD LIFT */

.bhd-lift {
  transition: transform var(--bhd-el-base) var(--bhd-el-ease),
    box-shadow var(--bhd-el-base) var(--bhd-el-ease);
  will-change: transform;
}
@media (hover: hover) {
  .bhd-lift:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-pop, 0 10px 30px -12px rgb(0 0 0 / .28));
  }
}
.bhd-lift:active { transform: translateY(0); }

/* ====================================================== 11. SKELETON TXT */

/* Complements skeleton.css (block skeletons) with an inline text shimmer. */
.bhd-loading-text {
  display: inline-block;
  border-radius: .25rem;
  color: transparent;
  background: linear-gradient(
    90deg,
    var(--bhd-el-muted) 25%,
    color-mix(in srgb, var(--foreground, CanvasText) 12%, transparent) 37%,
    var(--bhd-el-muted) 63%
  );
  background-size: 400% 100%;
  animation: bhd-shimmer 1.4s ease-in-out infinite;
  user-select: none;
}
@keyframes bhd-shimmer {
  0%   { background-position: 100% 50%; }
  100% { background-position: 0 50%; }
}

/* ==================================================== 12. REDUCED MOTION */

/* Accessibility requirement, not a nicety. Everything above is decorative
 * motion; nothing here carries meaning, so stopping it loses no information. */
@media (prefers-reduced-motion: reduce) {
  .bhd-switch,
  .bhd-switch::before,
  .bhd-check,
  .bhd-check::after,
  .bhd-radio,
  .bhd-radio::after,
  .bhd-progress::after,
  .bhd-tip::after,
  .bhd-segmented > *,
  .bhd-lift {
    transition-duration: 1ms !important;
  }
  .bhd-spinner,
  .bhd-dots > i,
  .bhd-badge--live .bhd-badge__dot::after,
  .bhd-progress--indeterminate::after,
  .bhd-loading-text {
    animation: none !important;
  }
  .bhd-spinner {
    border-block-start-color: currentColor;
    opacity: .6;
  }
  .bhd-progress--indeterminate::after { inline-size: 100%; opacity: .35; }
  .bhd-loading-text { background: var(--bhd-el-muted); color: inherit; }
  .bhd-lift:hover { transform: none; }
}
