/* ============================================================================
   Jamlex — Components
   ----------------------------------------------------------------------------
   Reusable UI primitives — buttons, inputs, cards, chips, dialogs, toasts.
   Every component supports light + dark + LTR + RTL out of the box.
   ============================================================================ */

/* ── Buttons ──────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  padding: var(--sp-3) var(--sp-5);
  font-weight: var(--fw-semibold);
  font-size: var(--fs-body-l);
  line-height: 1;
  border-radius: var(--rd-3);
  cursor: pointer;
  transition: background var(--motion-fast) var(--ease-out),
              color var(--motion-fast) var(--ease-out),
              transform var(--motion-fast) var(--ease-out),
              box-shadow var(--motion-fast) var(--ease-out);
  user-select: none;
  white-space: nowrap;
  min-height: 44px; /* touch target */
}
.btn:disabled,
.btn[aria-disabled="true"] {
  opacity: 0.5;
  cursor: not-allowed;
  pointer-events: none;
}
.btn:active:not(:disabled):not([aria-disabled="true"]) {
  transform: scale(0.98);
}

.btn--filled {
  background: var(--jx-primary);
  color: var(--jx-text-on-brand);
  box-shadow: var(--sh-brand);
}
.btn--filled:hover:not(:disabled) { background: var(--jx-primary-dark); }

.btn--outlined {
  background: transparent;
  color: var(--jx-primary);
  border: 1px solid var(--jx-primary);
}
.btn--outlined:hover:not(:disabled) { background: var(--jx-primary-glow); }

.btn--text {
  background: transparent;
  color: var(--jx-primary);
  padding: var(--sp-2) var(--sp-3);
  min-height: 32px;
}
.btn--text:hover:not(:disabled) {
  background: var(--jx-primary-glow);
  text-decoration: none;
}

.btn--danger {
  background: var(--jx-danger);
  color: #fff;
}
.btn--danger:hover:not(:disabled) { filter: brightness(0.94); }

.btn--block { width: 100%; }
.btn--sm { padding: var(--sp-2) var(--sp-3); font-size: var(--fs-body-m); min-height: 36px; }
.btn--lg { padding: var(--sp-4) var(--sp-6); font-size: var(--fs-body-l); min-height: 52px; }

.btn--loading { color: transparent !important; position: relative; }
.btn--loading::after {
  content: "";
  position: absolute;
  inset: 0;
  margin: auto;
  width: 18px; height: 18px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  color: var(--jx-text-on-brand);
}
.btn--outlined.btn--loading::after { color: var(--jx-primary); }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Icon buttons ────────────────────────────────────────────────────── */
.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px; height: 40px;
  border-radius: var(--rd-pill);
  background: transparent;
  color: var(--jx-text-2);
  transition: background var(--motion-fast), color var(--motion-fast);
  cursor: pointer;
}
.btn-icon:hover { background: var(--jx-surface); color: var(--jx-text); }
.btn-icon[data-active="true"] { color: var(--jx-primary); background: var(--jx-primary-glow); }
.btn-icon--lg { width: 48px; height: 48px; }
.btn-icon--sm { width: 32px; height: 32px; }

/* ── Inputs ──────────────────────────────────────────────────────────── */
.field {
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
}
.field__label {
  font-size: var(--fs-label);
  font-weight: var(--fw-semibold);
  color: var(--jx-text-2);
}
.field__control {
  position: relative;
  display: flex;
  align-items: center;
}
.field__input {
  flex: 1;
  width: 100%;
  padding: var(--sp-3) var(--sp-4);
  background: var(--jx-surface);
  border: 1px solid var(--jx-border);
  border-radius: var(--rd-3);
  color: var(--jx-text);
  font-size: var(--fs-body-l);
  transition: border-color var(--motion-fast), box-shadow var(--motion-fast), background var(--motion-fast);
  min-height: 48px;
}
.field__input::placeholder { color: var(--jx-text-3); }
.field__input:focus {
  outline: none;
  border-color: var(--jx-primary);
  box-shadow: 0 0 0 3px var(--jx-primary-glow);
  background: var(--jx-bg);
}
.field__input:disabled { opacity: 0.6; cursor: not-allowed; }
.field__input[aria-invalid="true"] {
  border-color: var(--jx-danger);
}
.field__input[aria-invalid="true"]:focus {
  box-shadow: 0 0 0 3px rgba(244, 67, 54, 0.18);
}
.field__error {
  font-size: var(--fs-body-s);
  color: var(--jx-danger);
}
.field__help {
  font-size: var(--fs-body-s);
  color: var(--jx-text-2);
}

/* Inputs with leading/trailing slots (e.g. eye toggle, clear-X, country pick) */
.field__input--with-slot-end  { padding-inline-end: var(--sp-12); }
.field__input--with-slot-start { padding-inline-start: var(--sp-12); }

.field__slot-end,
.field__slot-start {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--jx-text-2);
}
.field__slot-end   { inset-inline-end:   var(--sp-3); }
.field__slot-start { inset-inline-start: var(--sp-3); }

/* OTP boxes */
.otp {
  display: flex;
  gap: var(--sp-2);
  justify-content: center;
  direction: ltr; /* OTP boxes always LTR regardless of UI locale */
}
.otp__box {
  width: 48px; height: 56px;
  text-align: center;
  font-size: 22px;
  font-weight: var(--fw-bold);
  background: var(--jx-surface);
  border: 1px solid var(--jx-border);
  border-radius: var(--rd-3);
  color: var(--jx-text);
  transition: border-color var(--motion-fast), box-shadow var(--motion-fast);
}
.otp__box:focus {
  outline: none;
  border-color: var(--jx-primary);
  box-shadow: 0 0 0 3px var(--jx-primary-glow);
}

/* Checkbox / radio */
.check {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  cursor: pointer;
  font-size: var(--fs-body-m);
  color: var(--jx-text);
}
.check__input {
  appearance: none;
  width: 20px; height: 20px;
  border: 1.5px solid var(--jx-border);
  border-radius: var(--rd-1);
  background: var(--jx-bg);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background var(--motion-fast), border-color var(--motion-fast);
}
.check__input:checked {
  background: var(--jx-primary);
  border-color: var(--jx-primary);
  position: relative;
}
.check__input:checked::after {
  content: "";
  width: 5px; height: 10px;
  border: solid #fff;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg) translate(-1px, -1px);
  display: block;
}

/* ── Cards ───────────────────────────────────────────────────────────── */
.card {
  background: var(--jx-bg);
  border: 1px solid var(--jx-border);
  border-radius: var(--rd-4);
  padding: var(--sp-4);
}
.card--elevated {
  border: 0;
  box-shadow: var(--sh-md);
}
.card--surface {
  background: var(--jx-surface);
}
.card--brand {
  background: linear-gradient(135deg, var(--jx-primary), var(--jx-primary-dark));
  color: var(--jx-text-on-brand);
  border: 0;
  box-shadow: var(--sh-brand);
}
.card__title {
  font-size: var(--fs-title);
  font-weight: var(--fw-bold);
  margin-bottom: var(--sp-2);
}
.card__subtitle {
  font-size: var(--fs-body-m);
  color: var(--jx-text-2);
}

/* ── Chips / Pills ───────────────────────────────────────────────────── */
.chip {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-1);
  padding: var(--sp-1) var(--sp-3);
  border-radius: var(--rd-pill);
  font-size: var(--fs-body-s);
  font-weight: var(--fw-semibold);
  background: var(--jx-surface-alt);
  color: var(--jx-text-2);
  cursor: default;
  white-space: nowrap;
}
.chip--selectable { cursor: pointer; transition: background var(--motion-fast), color var(--motion-fast); }
.chip--selectable:hover { background: var(--jx-primary-glow); color: var(--jx-primary); }
.chip[data-selected="true"] {
  background: var(--jx-primary);
  color: var(--jx-text-on-brand);
}
.chip--success { background: rgba(76, 175, 80, 0.12); color: var(--jx-success); }
.chip--warning { background: rgba(255, 152, 0, 0.12); color: var(--jx-warning); }
.chip--danger  { background: rgba(244, 67, 54, 0.12); color: var(--jx-danger); }

/* CEFR level badge: each level has its own color (Spec v2) */
.chip[data-level="A1"] { background: #E8F5E9; color: #2E7D32; }
.chip[data-level="A2"] { background: #DCEDC8; color: #558B2F; }
.chip[data-level="B1"] { background: #FFF9C4; color: #F57F17; }
.chip[data-level="B2"] { background: #FFE0B2; color: #E65100; }
.chip[data-level="C1"] { background: #FFCDD2; color: #C62828; }
.chip[data-level="C2"] { background: #D1C4E9; color: #4527A0; }

[data-theme="dark"] .chip[data-level="A1"] { background: rgba(46,125,50,0.18); color: #81C784; }
[data-theme="dark"] .chip[data-level="A2"] { background: rgba(85,139,47,0.18); color: #AED581; }
[data-theme="dark"] .chip[data-level="B1"] { background: rgba(245,127,23,0.18); color: #FFD54F; }
[data-theme="dark"] .chip[data-level="B2"] { background: rgba(230,81,0,0.18); color: #FFB74D; }
[data-theme="dark"] .chip[data-level="C1"] { background: rgba(198,40,40,0.18); color: #E57373; }
[data-theme="dark"] .chip[data-level="C2"] { background: rgba(69,39,160,0.18); color: #B39DDB; }

/* ── Divider ─────────────────────────────────────────────────────────── */
.divider {
  height: 1px;
  background: var(--jx-divider);
  width: 100%;
  margin-block: var(--sp-3);
}

/* ── Dialog ──────────────────────────────────────────────────────────── */
.scrim {
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--sp-5);
  z-index: var(--z-dialog);
  animation: fadeIn var(--motion-base) var(--ease-out);
}
.dialog {
  background: var(--jx-bg);
  border-radius: var(--rd-6);
  padding: var(--sp-6);
  max-width: 360px;
  width: 100%;
  box-shadow: var(--sh-xl);
  animation: popIn var(--motion-base) var(--ease-out);
}
.dialog__title {
  font-size: var(--fs-headline);
  font-weight: var(--fw-bold);
  margin-bottom: var(--sp-2);
}
.dialog__body {
  color: var(--jx-text-2);
  margin-bottom: var(--sp-5);
  font-size: var(--fs-body-m);
}
.dialog__actions {
  display: flex;
  gap: var(--sp-2);
  justify-content: flex-end;
}

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes popIn  { from { opacity: 0; transform: scale(0.94); } to { opacity: 1; transform: none; } }

/* ── Toast (centered popup, not corner SnackBar) ─────────────────────── */
.toast-host {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--sp-5);
  z-index: var(--z-toast);
  pointer-events: none;
}
.toast {
  background: var(--jx-bg);
  border-radius: var(--rd-6);
  box-shadow: var(--sh-xl);
  padding: var(--sp-6) var(--sp-5);
  max-width: 320px;
  width: 100%;
  text-align: center;
  pointer-events: auto;
  animation: popIn var(--motion-base) var(--ease-out);
}
.toast__icon {
  width: 56px; height: 56px;
  margin: 0 auto var(--sp-4);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
}
.toast--success .toast__icon { background: rgba(76, 175, 80, 0.14); color: var(--jx-success); }
.toast--error   .toast__icon { background: rgba(244, 67, 54, 0.14); color: var(--jx-danger); }
.toast--info    .toast__icon { background: var(--jx-primary-glow);  color: var(--jx-primary); }
.toast__msg {
  font-size: var(--fs-body-m);
  color: var(--jx-text);
  line-height: var(--lh-snug);
}

/* ── Skeleton (loading placeholder) ──────────────────────────────────── */
.skel {
  background: linear-gradient(
    90deg,
    var(--jx-surface) 0%,
    var(--jx-surface-alt) 50%,
    var(--jx-surface) 100%
  );
  background-size: 200% 100%;
  border-radius: var(--rd-2);
  animation: skelPulse 1.4s ease-in-out infinite;
}
@keyframes skelPulse {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}
.skel--line { height: 14px; width: 80%; }
.skel--title { height: 20px; width: 60%; }
.skel--avatar { width: 48px; height: 48px; border-radius: 50%; }
.skel--card { height: 80px; width: 100%; border-radius: var(--rd-4); }

/* ── Audio button (speaker icon) ─────────────────────────────────────── */
.audio-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px; height: 32px;
  border-radius: var(--rd-pill);
  background: var(--jx-primary-glow);
  color: var(--jx-primary);
  cursor: pointer;
  transition: background var(--motion-fast), transform var(--motion-fast);
}
.audio-btn:hover { background: var(--jx-primary); color: var(--jx-text-on-brand); }
.audio-btn:active { transform: scale(0.94); }
.audio-btn[data-playing="true"] {
  background: var(--jx-primary);
  color: var(--jx-text-on-brand);
}
.audio-btn[data-playing="true"] svg { animation: audioWiggle 0.6s ease-in-out infinite alternate; }
@keyframes audioWiggle {
  from { transform: scale(1); }
  to   { transform: scale(1.12); }
}

/* ── Segmented control ──────────────────────────────────────────────── */
.segmented {
  display: inline-flex;
  background: var(--jx-surface);
  border-radius: var(--rd-3);
  padding: 3px;
  gap: 2px;
}
.segmented__item {
  padding: var(--sp-2) var(--sp-4);
  font-size: var(--fs-body-m);
  font-weight: var(--fw-semibold);
  color: var(--jx-text-2);
  border-radius: var(--rd-2);
  cursor: pointer;
  transition: background var(--motion-fast), color var(--motion-fast);
}
.segmented__item[data-selected="true"] {
  background: var(--jx-bg);
  color: var(--jx-text);
  box-shadow: var(--sh-sm);
}
