/* ==========================================================================
   Component library (spec §42)
   --------------------------------------------------------------------------
   Every component uses logical properties, so RTL is automatic. Interactive
   controls meet a 44px touch target on mobile (spec §48).
   ========================================================================== */

/* --------------------------------------------------------------------------
   Buttons
   -------------------------------------------------------------------------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-5);
  min-block-size: 42px;
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  font-weight: var(--weight-semibold);
  white-space: nowrap;
  border: 1px solid transparent;
  transition: background-color var(--duration-fast) var(--ease-out),
    border-color var(--duration-fast) var(--ease-out),
    transform var(--duration-fast) var(--ease-out),
    opacity var(--duration-fast) var(--ease-out);
}

/* Press feedback (spec §19). */
.btn:active:not(:disabled) {
  transform: translateY(1px);
}

.btn:disabled,
.btn[aria-disabled='true'] {
  opacity: 0.5;
  pointer-events: none;
}

.btn-primary {
  background: linear-gradient(135deg, #0284c7, #2563eb);
  color: #ffffff;
  border: 1px solid rgba(255, 255, 255, 0.15);
  box-shadow: 0 4px 14px rgba(2, 132, 199, 0.35);
  border-radius: 12px;
  font-weight: 600;
}

.btn-primary:hover:not(:disabled) {
  background: linear-gradient(135deg, #0369a1, #1d4ed8);
  box-shadow: 0 6px 20px rgba(2, 132, 199, 0.45);
}

.btn-secondary {
  background: rgba(255, 255, 255, 0.04);
  color: #e2e8f0;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 12px;
}

.btn-secondary:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(255, 255, 255, 0.2);
  color: #ffffff;
}

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--surface-hover);
  color: var(--text-primary);
}

.btn-danger {
  background: rgba(239, 68, 68, 0.16);
  color: #f87171;
  border: 1px solid rgba(239, 68, 68, 0.35);
  border-radius: 12px;
}

.btn-danger:hover:not(:disabled) {
  background: #ef4444;
  color: #fff;
  box-shadow: 0 4px 14px rgba(239, 68, 68, 0.4);
}

.btn-sm {
  padding: var(--space-2) var(--space-3);
  min-block-size: 34px;
  font-size: var(--text-xs);
}

.btn-lg {
  padding: var(--space-4) var(--space-8);
  min-block-size: 52px;
  font-size: var(--text-base);
}

.btn-block {
  inline-size: 100%;
}

.btn-icon {
  padding: var(--space-2);
  min-inline-size: 40px;
  min-block-size: 40px;
  border-radius: var(--radius-md);
}

/* Loading state */
.btn[data-loading='true'] {
  color: transparent;
  pointer-events: none;
  position: relative;
}

.btn[data-loading='true']::after {
  content: '';
  position: absolute;
  inline-size: 16px;
  block-size: 16px;
  border: 2px solid currentcolor;
  border-block-start-color: transparent;
  border-radius: var(--radius-full);
  animation: spin 0.6s linear infinite;
  color: var(--primary-text);
}

.btn-secondary[data-loading='true']::after,
.btn-ghost[data-loading='true']::after {
  color: var(--text-primary);
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* --------------------------------------------------------------------------
   Cards
   -------------------------------------------------------------------------- */

.card {
  position: relative;
  background: rgba(10, 17, 32, 0.75);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 18px;
  padding: var(--space-6);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(12px);
  overflow: hidden;
}

.card-interactive {
  transition: transform var(--duration-base) var(--ease-out),
    box-shadow var(--duration-base) var(--ease-out),
    border-color var(--duration-base) var(--ease-out);
}

.card-interactive:hover {
  transform: translateY(-3px);
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.4);
  border-color: rgba(255, 255, 255, 0.15);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-block-end: var(--space-5);
}

.card-title {
  font-size: var(--text-lg);
  font-weight: var(--weight-semibold);
  color: #f1f5f9;
}

/* Hero card */
.card-hero {
  background: linear-gradient(135deg, #091328 0%, #0d1a37 55%, #071022 100%);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 18px;
  padding: var(--space-8);
  position: relative;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
}

.stat-card {
  background: rgba(10, 17, 32, 0.75);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 16px;
  padding: var(--space-5);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
}

.stat-value {
  font-size: var(--text-3xl);
  font-weight: var(--weight-bold);
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
  color: #ffffff;
}

.stat-label {
  font-size: var(--text-sm);
  color: #8e9cb4;
  margin-block-start: var(--space-1);
}

/* --------------------------------------------------------------------------
   Badges
   -------------------------------------------------------------------------- */

.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 6px 14px;
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  white-space: nowrap;
  border: 1px solid transparent;
}

.badge-success {
  background: rgba(16, 185, 129, 0.16);
  border-color: rgba(16, 185, 129, 0.4);
  color: #34d399;
}

.badge-warning {
  background: rgba(245, 158, 11, 0.16);
  border-color: rgba(245, 158, 11, 0.4);
  color: #fbbf24;
}

.badge-danger {
  background: rgba(239, 68, 68, 0.16);
  border-color: rgba(239, 68, 68, 0.4);
  color: #f87171;
}

.badge-info {
  background: rgba(56, 189, 248, 0.16);
  border-color: rgba(56, 189, 248, 0.4);
  color: #38bdf8;
}

.badge-neutral {
  background: rgba(255, 255, 255, 0.03);
  border-color: rgba(255, 255, 255, 0.08);
  color: #64748b;
}

.badge-primary {
  background: rgba(2, 132, 199, 0.16);
  border-color: rgba(2, 132, 199, 0.4);
  color: #38bdf8;
  box-shadow: 0 0 10px rgba(2, 132, 199, 0.2);
}

/* Status dot */
.status-dot {
  inline-size: 8px;
  block-size: 8px;
  border-radius: var(--radius-full);
  background: currentcolor;
  flex-shrink: 0;
}

/* --------------------------------------------------------------------------
   Forms
   -------------------------------------------------------------------------- */

.field {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.label {
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  color: #8e9cb4;
}

.label-required::after {
  content: ' *';
  color: var(--danger);
}

.input,
.select,
.textarea {
  inline-size: 100%;
  padding: var(--space-3) var(--space-4);
  min-block-size: 44px;
  background: rgba(6, 11, 23, 0.8);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  color: #ffffff;
  font-size: var(--text-sm);
  transition: border-color var(--duration-fast) var(--ease-out),
    box-shadow var(--duration-fast) var(--ease-out);
}

.input::placeholder,
.textarea::placeholder {
  color: #64748b;
}

.input:focus,
.select:focus,
.textarea:focus {
  border-color: #38bdf8;
  box-shadow: 0 0 12px rgba(56, 189, 248, 0.25);
  outline: none;
}

.input[aria-invalid='true'],
.textarea[aria-invalid='true'],
.select[aria-invalid='true'] {
  border-color: var(--danger);
}

.textarea {
  min-block-size: 120px;
  resize: vertical;
  line-height: var(--leading-normal);
}

.select {
  cursor: pointer;
  /* The native arrow is unstyleable and sits on the wrong side in RTL, so it
     is replaced by a background image positioned with a logical property. */
  appearance: none;
  padding-inline-end: var(--space-10);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%236b7a9c' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right var(--space-4) center;
  background-size: 16px;
}

[dir='rtl'] .select {
  background-position: left var(--space-4) center;
}

/* Input with a trailing control, e.g. show/hide password or a copy button. */
.input-group {
  position: relative;
  display: flex;
  align-items: center;
}

.input-group .input {
  padding-inline-end: var(--space-12);
}

.input-affix {
  position: absolute;
  inset-inline-end: var(--space-2);
  display: flex;
  align-items: center;
  justify-content: center;
  inline-size: 36px;
  block-size: 36px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  transition: color var(--duration-fast) var(--ease-out),
    background-color var(--duration-fast) var(--ease-out);
}

.input-affix:hover {
  color: var(--text-primary);
  background: var(--surface-hover);
}

.field-error {
  font-size: var(--text-xs);
  color: var(--danger);
  min-block-size: 1em;
}

.field-hint {
  font-size: var(--text-xs);
  color: var(--text-muted);
}

/* Checkbox and toggle */

.checkbox {
  display: inline-flex;
  align-items: center;
  gap: var(--space-3);
  cursor: pointer;
  font-size: var(--text-sm);
}

.checkbox input[type='checkbox'] {
  inline-size: 18px;
  block-size: 18px;
  accent-color: var(--primary);
  cursor: pointer;
  flex-shrink: 0;
}

.toggle {
  position: relative;
  display: inline-flex;
  align-items: center;
  inline-size: 44px;
  block-size: 24px;
  flex-shrink: 0;
}

.toggle input {
  position: absolute;
  opacity: 0;
  inline-size: 100%;
  block-size: 100%;
  margin: 0;
  cursor: pointer;
}

.toggle-track {
  inline-size: 100%;
  block-size: 100%;
  background: var(--border-strong);
  border-radius: var(--radius-full);
  transition: background-color var(--duration-base) var(--ease-out);
  pointer-events: none;
}

.toggle-thumb {
  position: absolute;
  inset-inline-start: 3px;
  inline-size: 18px;
  block-size: 18px;
  background: #fff;
  border-radius: var(--radius-full);
  transition: transform var(--duration-base) var(--ease-out);
  pointer-events: none;
}

.toggle input:checked ~ .toggle-track {
  background: var(--primary);
}

/* `translate` in the inline direction flips automatically under RTL. */
.toggle input:checked ~ .toggle-thumb {
  transform: translateX(20px);
}

[dir='rtl'] .toggle input:checked ~ .toggle-thumb {
  transform: translateX(-20px);
}

.toggle input:focus-visible ~ .toggle-track {
  outline: 2px solid var(--border-focus);
  outline-offset: 2px;
}

/* --------------------------------------------------------------------------
   Progress
   -------------------------------------------------------------------------- */

.progress {
  inline-size: 100%;
  block-size: 8px;
  background: var(--bg-subtle);
  border-radius: var(--radius-full);
  overflow: hidden;
}

.progress-bar {
  block-size: 100%;
  background: var(--gradient-brand);
  border-radius: var(--radius-full);
  transition: inline-size var(--duration-slow) var(--ease-out);
}

.progress-bar.is-warning {
  background: var(--warning);
}

.progress-bar.is-danger {
  background: var(--danger);
}

/* --------------------------------------------------------------------------
   Tables (spec §48 — become cards on mobile)
   -------------------------------------------------------------------------- */

.table-wrap {
  overflow-x: auto;
  background: rgba(10, 17, 32, 0.75);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 18px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.25);
}

.table {
  inline-size: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
}

.table th {
  text-align: start;
  padding: 14px 20px;
  background: rgba(255, 255, 255, 0.02);
  color: #8e9cb4;
  font-weight: 600;
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  white-space: nowrap;
  border-bottom: 1px solid rgba(255, 255, 255, 0.07);
}

.table td {
  padding: 16px 20px;
  border-block-start: 1px solid rgba(255, 255, 255, 0.04);
  color: #e2e8f0;
}

.table tbody tr:hover {
  background: rgba(255, 255, 255, 0.025);
}

/* --------------------------------------------------------------------------
   Modal (spec §21)
   -------------------------------------------------------------------------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: var(--z-modal);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  animation: fade-in var(--duration-base) var(--ease-out);
}

.modal {
  inline-size: 100%;
  max-inline-size: 520px;
  max-block-size: 90vh;
  overflow-y: auto;
  background: var(--surface-overlay, #0f1a33);
  border: 1px solid var(--border-strong, rgba(255, 255, 255, 0.1));
  border-radius: 20px;
  box-shadow: var(--shadow-lg, 0 24px 64px rgba(0, 0, 0, 0.5));
  backdrop-filter: blur(20px);
  animation: modal-in var(--duration-base) var(--ease-out);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  padding: var(--space-6);
  border-block-end: 1px solid var(--border, rgba(255, 255, 255, 0.07));
}

.modal-body {
  padding: var(--space-6);
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-3);
  padding: var(--space-6);
  border-block-start: 1px solid var(--border, rgba(255, 255, 255, 0.07));
}

@keyframes fade-in {
  from {
    opacity: 0;
  }
}

@keyframes modal-in {
  from {
    opacity: 0;
    transform: translateY(16px) scale(0.97);
  }
}

/* --------------------------------------------------------------------------
   Toasts (spec §20)
   -------------------------------------------------------------------------- */

.toast-region {
  position: fixed;
  inset-block-end: var(--space-6);
  inset-inline-end: var(--space-6);
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  max-inline-size: min(400px, calc(100vw - var(--space-8)));
  pointer-events: none;
}

.toast {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-4);
  background: rgba(10, 17, 32, 0.92);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-inline-start: 3px solid #38bdf8;
  border-radius: 14px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(16px);
  pointer-events: auto;
  animation: toast-in var(--duration-base) var(--ease-out);
}

.toast[data-state='leaving'] {
  animation: toast-out var(--duration-base) var(--ease-in-out) forwards;
}

.toast-success {
  border-inline-start-color: var(--success);
}
.toast-error {
  border-inline-start-color: var(--danger);
}
.toast-warning {
  border-inline-start-color: var(--warning);
}

.toast-icon {
  flex-shrink: 0;
  margin-block-start: 2px;
}

.toast-success .toast-icon {
  color: var(--success);
}
.toast-error .toast-icon {
  color: var(--danger);
}
.toast-warning .toast-icon {
  color: var(--warning);
}
.toast-info .toast-icon {
  color: var(--info);
}

.toast-content {
  flex: 1;
  font-size: var(--text-sm);
}

.toast-title {
  font-weight: var(--weight-semibold);
}

.toast-message {
  color: var(--text-secondary);
  margin-block-start: var(--space-1);
}

/* Slides in from the inline-end edge, which is the correct side in both
   directions because the transform uses a logical sign per direction. */
@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
}

@keyframes toast-out {
  to {
    opacity: 0;
    transform: translateY(8px);
  }
}

/* --------------------------------------------------------------------------
   Skeletons and empty / error states (spec §22)
   -------------------------------------------------------------------------- */

.skeleton {
  background: linear-gradient(
    90deg,
    var(--skeleton-base) 25%,
    var(--skeleton-shine) 50%,
    var(--skeleton-base) 75%
  );
  background-size: 200% 100%;
  animation: shimmer 1.4s ease-in-out infinite;
  border-radius: var(--radius-sm);
}

@keyframes shimmer {
  from {
    background-position: 200% 0;
  }
  to {
    background-position: -200% 0;
  }
}

.skeleton-text {
  block-size: 0.9em;
  margin-block: 0.3em;
}

.skeleton-title {
  block-size: 1.6em;
  inline-size: 40%;
}

.state-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-4);
  padding: var(--space-12) var(--space-6);
  text-align: center;
}

.state-icon {
  inline-size: 56px;
  block-size: 56px;
  display: grid;
  place-items: center;
  border-radius: var(--radius-full);
  background: rgba(2, 132, 199, 0.12);
  border: 1px solid rgba(2, 132, 199, 0.3);
  color: #38bdf8;
  box-shadow: 0 0 18px rgba(2, 132, 199, 0.2);
}

.state-title {
  font-size: var(--text-lg);
  font-weight: var(--weight-semibold);
  color: #f1f5f9;
}

.state-message {
  color: #8e9cb4;
  font-size: var(--text-sm);
  max-inline-size: 46ch;
}

/* --------------------------------------------------------------------------
   Accordion (spec §14)
   -------------------------------------------------------------------------- */

.accordion-item {
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 14px;
  background: rgba(10, 17, 32, 0.75);
  overflow: hidden;
  transition: border-color 0.25s ease;
}

.accordion-item + .accordion-item {
  margin-block-start: var(--space-3);
}

.accordion-trigger {
  inline-size: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  padding: var(--space-5);
  text-align: start;
  font-weight: var(--weight-medium);
  color: #e2e8f0;
  transition: background-color var(--duration-fast) var(--ease-out);
}

.accordion-trigger:hover {
  background: rgba(255, 255, 255, 0.03);
}

.accordion-chevron {
  flex-shrink: 0;
  color: #64748b;
  transition: transform var(--duration-base) var(--ease-out);
}

.accordion-trigger {
  list-style: none;
  cursor: pointer;
}

.accordion-trigger::-webkit-details-marker {
  display: none;
}

.accordion-item[open] .accordion-chevron,
.accordion-trigger[aria-expanded='true'] .accordion-chevron {
  transform: rotate(180deg);
}

.accordion-panel {
  padding: 0 var(--space-5) var(--space-5);
  color: #8e9cb4;
  font-size: var(--text-sm);
  line-height: var(--leading-relaxed);
}

/* --------------------------------------------------------------------------
   Tabs
   -------------------------------------------------------------------------- */

.tabs {
  display: flex;
  gap: var(--space-1);
  border-block-end: 1px solid var(--border);
  overflow-x: auto;
}

.tab {
  padding: var(--space-3) var(--space-5);
  color: var(--text-secondary);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  white-space: nowrap;
  border-block-end: 2px solid transparent;
  transition: color var(--duration-fast) var(--ease-out),
    border-color var(--duration-fast) var(--ease-out);
}

.tab:hover {
  color: var(--text-primary);
}

.tab[aria-selected='true'] {
  color: var(--primary);
  border-block-end-color: var(--primary);
}

/* --------------------------------------------------------------------------
   Dropdown
   -------------------------------------------------------------------------- */

.dropdown {
  position: relative;
}

.dropdown-menu {
  position: absolute;
  inset-block-start: calc(100% + var(--space-2));
  inset-inline-end: 0;
  z-index: var(--z-dropdown);
  min-inline-size: 220px;
  padding: var(--space-2);
  background: rgba(8, 14, 28, 0.95);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 14px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(16px);
  animation: dropdown-in var(--duration-fast) var(--ease-out);
}

@keyframes dropdown-in {
  from {
    opacity: 0;
    transform: translateY(-6px);
  }
}

.dropdown-item {
  inline-size: 100%;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3);
  border-radius: var(--radius-sm);
  font-size: var(--text-sm);
  color: var(--text-secondary);
  text-align: start;
  transition: background-color var(--duration-fast) var(--ease-out),
    color var(--duration-fast) var(--ease-out);
}

.dropdown-item:hover {
  background: var(--surface-hover);
  color: var(--text-primary);
}

.dropdown-item.is-danger {
  color: var(--danger);
}

.dropdown-divider {
  block-size: 1px;
  background: var(--border);
  margin: var(--space-2) 0;
}

/* --------------------------------------------------------------------------
   Avatar
   -------------------------------------------------------------------------- */

.avatar {
  display: grid;
  place-items: center;
  inline-size: 38px;
  block-size: 38px;
  border-radius: var(--radius-full);
  background: var(--gradient-brand);
  color: #fff;
  font-weight: var(--weight-semibold);
  font-size: var(--text-sm);
  flex-shrink: 0;
  overflow: hidden;
}

.avatar-lg {
  inline-size: 56px;
  block-size: 56px;
  font-size: var(--text-lg);
}

/* --------------------------------------------------------------------------
   Timeline (spec §12)
   -------------------------------------------------------------------------- */

.timeline {
  position: relative;
  padding-inline-start: var(--space-6);
}

.timeline::before {
  content: '';
  position: absolute;
  inset-inline-start: 5px;
  inset-block: var(--space-2) var(--space-2);
  inline-size: 2px;
  background: var(--border);
}

.timeline-item {
  position: relative;
  padding-block-end: var(--space-5);
}

.timeline-dot {
  position: absolute;
  inset-inline-start: calc(var(--space-6) * -1);
  inset-block-start: 6px;
  inline-size: 12px;
  block-size: 12px;
  border-radius: var(--radius-full);
  background: var(--surface);
  border: 2px solid var(--primary);
}

/* --------------------------------------------------------------------------
   Copy field — masked secrets with reveal and copy (spec §9)
   -------------------------------------------------------------------------- */

.copy-field {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
  background: rgba(6, 11, 23, 0.85);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 12px;
  transition: border-color 0.2s ease;
}

.copy-field:hover {
  border-color: rgba(56, 189, 248, 0.3);
}

.copy-value {
  flex: 1;
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  /* Values are rendered LTR even in an RTL page: a URL or a numeric code
     reads left-to-right regardless of the surrounding language. */
  direction: ltr;
  text-align: start;
}

[dir='rtl'] .copy-value {
  text-align: right;
}

.copy-value[data-masked='true'] {
  letter-spacing: 0.15em;
}
