/* ==========================================================================
   Components: buttons, forms, cards, badges, tables, dialogs, toasts,
   menus, avatars, empty states, skeletons.
   ========================================================================== */

/* Buttons ----------------------------------------------------------------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  height: var(--control-height);
  padding: 0 var(--space-4);
  border: 1px solid transparent;
  border-radius: var(--radius-button);
  font-size: var(--text-base);
  font-weight: var(--weight-semibold);
  line-height: 1;
  white-space: nowrap;
  transition: background var(--duration-fast) var(--ease),
    border-color var(--duration-fast) var(--ease), color var(--duration-fast) var(--ease),
    box-shadow var(--duration-fast) var(--ease);
}

.btn svg {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

.btn:disabled,
.btn[aria-disabled="true"] {
  opacity: 0.55;
  pointer-events: none;
}

.btn--primary {
  background: var(--color-primary);
  color: var(--color-on-primary);
}

.btn--primary:hover {
  background: var(--color-primary-hover);
}

.btn--secondary {
  background: var(--color-surface);
  border-color: var(--color-border-strong);
  color: var(--color-text);
}

.btn--secondary:hover {
  background: var(--color-surface-secondary);
}

.btn--ghost {
  background: transparent;
  color: var(--color-text-secondary);
}

.btn--ghost:hover {
  background: var(--color-surface-secondary);
  color: var(--color-text);
}

.btn--danger {
  background: var(--color-danger);
  color: #fff;
}

.btn--danger:hover {
  filter: brightness(1.08);
}

.btn--danger-outline {
  background: transparent;
  border-color: var(--color-danger);
  color: var(--color-danger);
}

.btn--danger-outline:hover {
  background: var(--color-danger-soft);
}

.btn--success {
  background: var(--color-success);
  color: #fff;
}

.btn--success:hover {
  filter: brightness(1.08);
}

.btn--sm {
  height: var(--control-height-sm);
  padding: 0 var(--space-3);
  font-size: var(--text-sm);
  border-radius: 8px;
}

.btn--sm svg {
  width: 15px;
  height: 15px;
}

.btn--lg {
  height: var(--control-height-lg);
  padding: 0 var(--space-6);
  font-size: var(--text-md);
}

.btn--block {
  width: 100%;
}

.btn--icon {
  width: var(--control-height);
  padding: 0;
}

.btn--icon.btn--sm {
  width: var(--control-height-sm);
}

.btn--icon.btn--lg {
  width: var(--call-control-size);
  height: var(--call-control-size);
  border-radius: var(--radius-circle);
  padding: 0;
}

.btn--icon.btn--lg svg {
  width: 24px;
  height: 24px;
}

.btn.is-loading {
  position: relative;
  color: transparent;
  pointer-events: none;
}

.btn.is-loading::after {
  content: "";
  position: absolute;
  width: 18px;
  height: 18px;
  border: 2px solid rgba(255, 255, 255, 0.4);
  border-top-color: #fff;
  border-radius: var(--radius-circle);
  animation: spin 0.7s linear infinite;
}

.btn--secondary.is-loading::after,
.btn--ghost.is-loading::after {
  border-color: var(--color-border-strong);
  border-top-color: var(--color-primary);
}

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

/* Forms ------------------------------------------------------------------- */

.field {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.field-label {
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  color: var(--color-text-secondary);
}

.field-hint {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

.field-error {
  font-size: var(--text-xs);
  color: var(--color-danger);
}

.input,
.select,
.textarea {
  width: 100%;
  height: var(--control-height);
  padding: 0 var(--space-3);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-input);
  background: var(--color-surface);
  color: var(--color-text);
  transition: border-color var(--duration-fast) var(--ease), box-shadow var(--duration-fast) var(--ease);
}

.textarea {
  height: auto;
  min-height: 88px;
  padding: var(--space-2) var(--space-3);
  resize: vertical;
}

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

.input:hover,
.select:hover,
.textarea:hover {
  border-color: var(--color-text-muted);
}

.input:focus,
.select:focus,
.textarea:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: var(--shadow-focus);
}

.input[aria-invalid="true"],
.select[aria-invalid="true"],
.textarea[aria-invalid="true"] {
  border-color: var(--color-danger);
}

.input:disabled,
.select:disabled,
.textarea:disabled {
  background: var(--color-surface-secondary);
  color: var(--color-text-muted);
  cursor: not-allowed;
}

.select {
  appearance: none;
  padding-right: var(--space-8);
  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='%2364748b' 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-3) center;
}

.input-group {
  position: relative;
  display: flex;
  align-items: center;
}

.input-group .input {
  padding-left: calc(var(--space-3) + 26px);
}

.input-group-icon {
  position: absolute;
  left: var(--space-3);
  display: inline-flex;
  color: var(--color-text-muted);
  pointer-events: none;
}

.input-group-icon svg {
  width: 18px;
  height: 18px;
}

.input-group-action {
  position: absolute;
  right: var(--space-1);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 8px;
  color: var(--color-text-muted);
}

.input-group-action:hover {
  background: var(--color-surface-secondary);
  color: var(--color-text);
}

.input--with-action {
  padding-right: calc(var(--space-3) + 34px);
}

/* Checkbox / switch ------------------------------------------------------- */

.checkbox-row {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  cursor: pointer;
}

.checkbox-row input[type="checkbox"] {
  width: 18px;
  height: 18px;
  margin-top: 2px;
  accent-color: var(--color-primary);
  flex-shrink: 0;
}

.switch-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  padding: var(--space-3) 0;
}

.switch {
  position: relative;
  display: inline-flex;
  width: 42px;
  height: 24px;
  flex-shrink: 0;
  cursor: pointer;
}

.switch input {
  position: absolute;
  opacity: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  cursor: pointer;
}

.switch-track {
  width: 100%;
  height: 100%;
  border-radius: var(--radius-pill);
  background: var(--color-border-strong);
  transition: background var(--duration-fast) var(--ease);
}

.switch-thumb {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 18px;
  height: 18px;
  border-radius: var(--radius-circle);
  background: #fff;
  box-shadow: var(--shadow-sm);
  transition: transform var(--duration-fast) var(--ease);
}

.switch input:checked ~ .switch-track {
  background: var(--color-primary);
}

.switch input:checked ~ .switch-thumb {
  transform: translateX(18px);
}

.switch input:focus-visible ~ .switch-track {
  box-shadow: var(--shadow-focus);
}

/* Cards ------------------------------------------------------------------- */

.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-sm);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-5);
  border-bottom: 1px solid var(--color-border);
}

.card-title {
  font-size: var(--text-md);
  font-weight: var(--weight-semibold);
}

.card-body {
  padding: var(--space-5);
}

.card-body--flush {
  padding: 0;
}

/* Badges / status --------------------------------------------------------- */

.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 2px 10px;
  border-radius: var(--radius-pill);
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  line-height: 1.6;
  white-space: nowrap;
}

.badge--neutral {
  background: var(--color-surface-secondary);
  color: var(--color-text-secondary);
}

.badge--primary {
  background: var(--color-primary-light);
  color: var(--color-primary-deep);
}

[data-theme="dark"] .badge--primary {
  color: var(--color-primary);
}

.badge--success {
  background: var(--color-success-soft);
  color: var(--color-success);
}

.badge--warning {
  background: var(--color-warning-soft);
  color: var(--color-warning);
}

.badge--danger {
  background: var(--color-danger-soft);
  color: var(--color-danger);
}

.badge--info {
  background: var(--color-info-soft);
  color: var(--color-info);
}

.badge-dot-inline {
  width: 7px;
  height: 7px;
  border-radius: var(--radius-circle);
  background: currentColor;
}

/* Avatars ----------------------------------------------------------------- */

.avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-circle);
  background: var(--color-primary-light);
  color: var(--color-primary-deep);
  font-size: var(--text-sm);
  font-weight: var(--weight-semibold);
  flex-shrink: 0;
  overflow: hidden;
  user-select: none;
}

[data-theme="dark"] .avatar {
  color: var(--color-primary);
}

.avatar--sm {
  width: 28px;
  height: 28px;
  font-size: var(--text-xs);
}

.avatar--lg {
  width: 48px;
  height: 48px;
  font-size: var(--text-lg);
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* Tables ------------------------------------------------------------------ */

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-card);
  background: var(--color-surface);
}

.table {
  width: 100%;
  font-size: var(--text-base);
}

.table th,
.table td {
  padding: var(--space-3) var(--space-4);
  text-align: left;
  border-bottom: 1px solid var(--color-border);
  vertical-align: middle;
}

.table th {
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--color-text-muted);
  background: var(--color-surface-secondary);
  position: sticky;
  top: 0;
}

.table tbody tr:last-child td {
  border-bottom: 0;
}

.table tbody tr:hover td {
  background: var(--color-surface-secondary);
}

.table td.actions {
  text-align: right;
  white-space: nowrap;
}

/* Menus (dropdowns) ------------------------------------------------------- */

.menu {
  position: absolute;
  min-width: 200px;
  padding: var(--space-1);
  background: var(--color-surface-raised);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-lg);
  z-index: var(--z-overlay);
}

.menu-item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  width: 100%;
  min-height: 38px;
  padding: var(--space-2) var(--space-3);
  border-radius: 8px;
  color: var(--color-text);
  font-size: var(--text-base);
  text-align: left;
  text-decoration: none;
}

.menu-item:hover {
  background: var(--color-surface-secondary);
}

.menu-item--danger {
  color: var(--color-danger);
}

.menu-item svg {
  width: 16px;
  height: 16px;
  color: var(--color-text-muted);
}

.menu-item--danger svg {
  color: var(--color-danger);
}

.menu-separator {
  height: 1px;
  margin: var(--space-1) 0;
  background: var(--color-border);
}

.menu-label {
  padding: var(--space-2) var(--space-3);
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-text-muted);
}

/* Dialogs ----------------------------------------------------------------- */

.dialog-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
  background: rgba(15, 23, 42, 0.5);
  z-index: var(--z-dialog);
}

.dialog {
  width: 100%;
  max-width: 440px;
  max-height: min(85vh, 640px);
  display: flex;
  flex-direction: column;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}

.dialog--wide {
  max-width: 640px;
}

.dialog-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-5) var(--space-5) var(--space-3);
}

.dialog-title {
  font-size: var(--text-xl);
  font-weight: var(--weight-semibold);
}

.dialog-body {
  padding: 0 var(--space-5) var(--space-4);
  overflow-y: auto;
  color: var(--color-text-secondary);
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
  padding: var(--space-4) var(--space-5);
  border-top: 1px solid var(--color-border);
  background: var(--color-surface-secondary);
}

/* Toasts ------------------------------------------------------------------ */

.toast-region {
  position: fixed;
  right: var(--space-4);
  bottom: calc(var(--statusbar-height) + var(--space-4));
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  z-index: var(--z-toast);
  pointer-events: none;
}

.toast {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  min-width: 280px;
  max-width: 400px;
  padding: var(--space-3) var(--space-4);
  background: var(--color-surface-raised);
  border: 1px solid var(--color-border);
  border-left: 4px solid var(--color-text-muted);
  border-radius: var(--radius-button);
  box-shadow: var(--shadow-lg);
  pointer-events: auto;
  animation: toast-in var(--duration-base) var(--ease);
}

.toast--success {
  border-left-color: var(--color-success);
}

.toast--error {
  border-left-color: var(--color-danger);
}

.toast--warning {
  border-left-color: var(--color-warning);
}

.toast--info {
  border-left-color: var(--color-info);
}

.toast-title {
  font-weight: var(--weight-semibold);
  color: var(--color-text);
}

.toast-message {
  font-size: var(--text-sm);
  color: var(--color-text-secondary);
}

.toast-close {
  margin-left: auto;
  color: var(--color-text-muted);
  flex-shrink: 0;
}

.toast-close:hover {
  color: var(--color-text);
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Empty states / skeletons ------------------------------------------------ */

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-3);
  padding: var(--space-12) var(--space-6);
  text-align: center;
}

.empty-state svg {
  width: 40px;
  height: 40px;
  color: var(--color-text-muted);
}

.empty-state-title {
  font-size: var(--text-lg);
  font-weight: var(--weight-semibold);
  color: var(--color-text);
}

.empty-state-text {
  max-width: 360px;
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

.skeleton {
  background: linear-gradient(90deg, var(--color-surface-secondary) 25%, var(--color-border) 37%, var(--color-surface-secondary) 63%);
  background-size: 400% 100%;
  border-radius: 8px;
  animation: skeleton 1.4s ease infinite;
}

@keyframes skeleton {
  0% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0 50%;
  }
}

/* Tabs -------------------------------------------------------------------- */

.tabs {
  display: flex;
  gap: var(--space-1);
  border-bottom: 1px solid var(--color-border);
  padding: 0 var(--space-6);
}

.tab {
  position: relative;
  padding: var(--space-3) var(--space-3);
  color: var(--color-text-secondary);
  font-size: var(--text-base);
  font-weight: var(--weight-medium);
  text-decoration: none;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}

.tab:hover {
  color: var(--color-text);
}

.tab[aria-selected="true"],
.tab.is-active {
  color: var(--color-primary-deep);
  border-bottom-color: var(--color-primary);
}

[data-theme="dark"] .tab[aria-selected="true"],
[data-theme="dark"] .tab.is-active {
  color: var(--color-primary);
}

/* Segmented control ------------------------------------------------------- */

.segmented {
  display: inline-flex;
  padding: 3px;
  background: var(--color-surface-secondary);
  border-radius: var(--radius-button);
  gap: 2px;
}

.segmented button {
  min-height: 32px;
  padding: 0 var(--space-3);
  border-radius: 8px;
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  color: var(--color-text-secondary);
}

.segmented button[aria-pressed="true"] {
  background: var(--color-surface);
  color: var(--color-text);
  box-shadow: var(--shadow-sm);
}

/* Pagination -------------------------------------------------------------- */

.pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-3) 0;
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

/* Tooltip ----------------------------------------------------------------- */

[data-tooltip] {
  position: relative;
}

[data-tooltip]::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  padding: 4px 8px;
  border-radius: 6px;
  background: var(--color-text);
  color: var(--color-bg);
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--duration-fast) var(--ease);
  z-index: var(--z-overlay);
}

[data-tooltip]:hover::after,
[data-tooltip]:focus-visible::after {
  opacity: 1;
}
