/* ============================================================
   VRAJ ENGINEERING — Component Library v2
   Refined buttons, cards, tables, modals, forms, toasts
   ============================================================ */

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  font-size: var(--fs-base);
  font-weight: 500;
  padding: var(--space-2) var(--space-5);
  border-radius: var(--radius-button);
  transition: all var(--transition-fast);
  cursor: pointer;
  border: 1px solid transparent;
  white-space: nowrap;
  line-height: 1;
  height: 36px;
  letter-spacing: 0.01em;
  font-family: inherit;
}

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

.btn-primary {
  background: var(--primary-600);
  color: white;
  border-color: var(--primary-600);
}
.btn-primary:hover {
  background: var(--primary-700);
  border-color: var(--primary-700);
  box-shadow: var(--shadow-sm);
}
.btn-primary:active {
  background: var(--primary-800);
  transform: scale(0.98);
}

.btn-secondary {
  background: var(--bg-surface);
  color: var(--text-primary);
  border-color: var(--border-default);
}
.btn-secondary:hover {
  background: var(--bg-hover);
  border-color: var(--neutral-300);
}
.btn-secondary:active {
  background: var(--neutral-200);
}

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

.btn-danger {
  background: var(--error-bg);
  color: var(--error-text);
  border-color: var(--error-border);
}
.btn-danger:hover {
  background: #FEE2E2;
  border-color: #FCA5A5;
}

.btn-accent {
  background: var(--accent-600);
  color: white;
  border-color: var(--accent-600);
}
.btn-accent:hover {
  background: var(--accent-700);
  box-shadow: var(--shadow-sm);
}

.btn-sm {
  font-size: var(--fs-xs);
  padding: var(--space-1) var(--space-3);
  height: 28px;
  border-radius: var(--radius-sm);
}
.btn-sm svg {
  width: 14px;
  height: 14px;
}

.btn-lg {
  font-size: var(--fs-md);
  padding: var(--space-3) var(--space-6);
  height: 44px;
}

.btn-icon {
  width: 36px;
  height: 36px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

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

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  pointer-events: none;
}

.btn-group {
  display: inline-flex;
  gap: 0;
}
.btn-group .btn {
  border-radius: 0;
}
.btn-group .btn:first-child {
  border-radius: var(--radius-button) 0 0 var(--radius-button);
}
.btn-group .btn:last-child {
  border-radius: 0 var(--radius-button) var(--radius-button) 0;
}
.btn-group .btn + .btn {
  margin-left: -1px;
}

/* ---------- Cards ---------- */
.card {
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-xs);
  transition: box-shadow var(--transition-base), border-color var(--transition-base);
}

.card:hover {
  box-shadow: var(--shadow-sm);
  border-color: var(--neutral-300);
}

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

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

.card-footer {
  padding: var(--space-4) var(--space-6);
  border-top: 1px solid var(--border-divider);
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

/* KPI Card */
.kpi-card {
  padding: var(--space-5) var(--space-6);
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-xs);
  transition: all var(--transition-base);
  position: relative;
  overflow: hidden;
}

.kpi-card:hover {
  box-shadow: var(--shadow-sm);
  border-color: var(--neutral-300);
  transform: translateY(-1px);
}

.kpi-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  border-radius: var(--radius-card) var(--radius-card) 0 0;
}

.kpi-card.kpi-blue::before { background: var(--primary-500); }
.kpi-card.kpi-emerald::before { background: var(--success-text); }
.kpi-card.kpi-violet::before { background: var(--accent-500); }
.kpi-card.kpi-amber::before { background: var(--warning-text); }

.kpi-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: var(--space-3);
}

.kpi-icon {
  width: 38px;
  height: 38px;
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

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

.kpi-icon-blue {
  background: var(--primary-100);
  color: var(--primary-600);
}

.kpi-icon-violet {
  background: var(--accent-100);
  color: var(--accent-600);
}

.kpi-icon-emerald {
  background: var(--success-bg);
  color: var(--success-text);
}

.kpi-icon-amber {
  background: var(--warning-bg);
  color: var(--warning-text);
}

.kpi-label {
  font-size: var(--fs-sm);
  color: var(--text-secondary);
  font-weight: 500;
  margin-bottom: var(--space-1);
}

.kpi-value {
  font-size: var(--fs-2xl);
  font-weight: 700;
  color: var(--text-heading);
  font-family: var(--font-nums);
  letter-spacing: -0.02em;
  line-height: 1.2;
}

.kpi-change {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  font-size: var(--fs-xs);
  font-weight: 500;
  margin-top: var(--space-2);
  padding: 2px 8px;
  border-radius: 6px;
}

.kpi-change svg {
  width: 12px;
  height: 12px;
}

.kpi-change-positive {
  color: var(--success-text);
  background: var(--success-bg);
}

.kpi-change-negative {
  color: var(--error-text);
  background: var(--error-bg);
}

.kpi-change-neutral {
  color: var(--text-secondary);
  background: var(--neutral-100);
}

/* ---------- Badges ---------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: var(--fs-xs);
  font-weight: 500;
  padding: 2px 8px;
  border-radius: 6px;
  white-space: nowrap;
}

.badge-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
}

.badge-primary {
  background: var(--primary-100);
  color: var(--primary-700);
}
.badge-primary .badge-dot { background: var(--primary-500); }

.badge-accent {
  background: var(--accent-100);
  color: var(--accent-700);
}
.badge-accent .badge-dot { background: var(--accent-500); }

.badge-success {
  background: var(--success-bg);
  color: var(--success-text);
}
.badge-success .badge-dot { background: var(--success-text); }

.badge-warning {
  background: var(--warning-bg);
  color: var(--warning-text);
}
.badge-warning .badge-dot { background: var(--warning-text); }

.badge-error {
  background: var(--error-bg);
  color: var(--error-text);
}
.badge-error .badge-dot { background: var(--error-text); }

.badge-neutral {
  background: var(--neutral-100);
  color: var(--neutral-600);
}
.badge-neutral .badge-dot { background: var(--neutral-400); }

.badge-outline {
  background: transparent;
  border: 1px solid var(--border-default);
  color: var(--text-secondary);
}

/* ---------- Tables ---------- */
.table-wrapper {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
}

.data-table th {
  font-size: var(--fs-xs);
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  text-align: left;
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border-divider);
  white-space: nowrap;
  user-select: none;
}

.data-table th.sortable {
  cursor: pointer;
}

.data-table th.sortable:hover {
  color: var(--text-secondary);
}

.data-table th.sorted {
  color: var(--primary-600);
}

.data-table td {
  font-size: var(--fs-base);
  color: var(--text-primary);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border-divider);
  vertical-align: middle;
}

.data-table tbody tr {
  transition: background var(--transition-fast);
}

.data-table tbody tr:hover {
  background: var(--neutral-50);
}

.data-table tbody tr:active {
  background: var(--primary-50);
}

.data-table .cell-mono {
  font-family: var(--font-mono);
  font-size: var(--fs-xs);
}

.data-table .cell-nums {
  font-family: var(--font-nums);
}

.data-table .cell-right {
  text-align: right;
}

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

/* Table Pagination */
.table-pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-3) var(--space-4);
  border-top: 1px solid var(--border-divider);
  font-size: var(--fs-sm);
  color: var(--text-secondary);
}

.table-pagination-btns {
  display: flex;
  gap: var(--space-1);
}

.table-pagination-btns button {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--fs-sm);
  font-weight: 500;
  color: var(--text-secondary);
  transition: all var(--transition-fast);
  border: 1px solid transparent;
  background: none;
  cursor: pointer;
  font-family: inherit;
}

.table-pagination-btns button:hover {
  background: var(--bg-hover);
}

.table-pagination-btns button.active {
  background: var(--primary-600);
  color: white;
}

.table-pagination-btns button:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

/* ---------- Forms ---------- */
.form-group {
  margin-bottom: var(--space-5);
}

.form-label {
  display: block;
  font-size: var(--fs-sm);
  font-weight: 500;
  color: var(--text-primary);
  margin-bottom: var(--space-1);
}

.form-label .required {
  color: var(--error-text);
  margin-left: 2px;
}

.form-hint {
  font-size: var(--fs-xs);
  color: var(--text-muted);
  margin-top: var(--space-1);
}

.form-input,
.form-select,
.form-textarea {
  width: 100%;
  padding: var(--space-2) var(--space-3);
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-input);
  font-size: var(--fs-base);
  color: var(--text-primary);
  transition: all var(--transition-fast);
  line-height: 1.5;
  min-height: 38px;
  font-family: inherit;
}

.form-input:hover,
.form-select:hover,
.form-textarea:hover {
  border-color: var(--neutral-300);
}

.form-input:focus,
.form-select:focus,
.form-textarea:focus {
  outline: none;
  border-color: var(--primary-400);
  box-shadow: 0 0 0 3px var(--primary-100);
}

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

.form-input.error,
.form-select.error,
.form-textarea.error {
  border-color: var(--error-border);
  box-shadow: 0 0 0 3px #FEF2F2;
}

.form-error {
  font-size: var(--fs-xs);
  color: var(--error-text);
  margin-top: var(--space-1);
}

.form-textarea {
  min-height: 100px;
  resize: vertical;
}

.form-select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2394A3B8' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 36px;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-4);
}

@media (max-width: 768px) {
  .form-row {
    grid-template-columns: 1fr;
  }
}

/* Toggle Switch */
.toggle {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: var(--space-3);
  cursor: pointer;
}

.toggle input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-track {
  width: 36px;
  height: 20px;
  background: var(--neutral-300);
  border-radius: 10px;
  transition: all var(--transition-fast);
  position: relative;
  flex-shrink: 0;
}

.toggle-track::after {
  content: '';
  position: absolute;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: white;
  top: 2px;
  left: 2px;
  transition: all var(--transition-fast);
  box-shadow: var(--shadow-xs);
}

.toggle input:checked + .toggle-track {
  background: var(--primary-500);
}

.toggle input:checked + .toggle-track::after {
  left: 18px;
}

.toggle input:focus-visible + .toggle-track {
  box-shadow: 0 0 0 3px var(--primary-100);
}

.toggle-label {
  font-size: var(--fs-sm);
  color: var(--text-primary);
}

/* Checkbox / Radio */
.checkbox, .radio {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  cursor: pointer;
  font-size: var(--fs-base);
  color: var(--text-primary);
}

.checkbox input, .radio input {
  width: 18px;
  height: 18px;
  accent-color: var(--primary-600);
  cursor: pointer;
}

/* Avatar */
.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--primary-100);
  color: var(--primary-600);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: var(--fs-xs);
  flex-shrink: 0;
}

.avatar-sm {
  width: 28px;
  height: 28px;
  font-size: 10px;
}

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

/* ---------- Modals ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(4px);
  z-index: 1000;
  display: none;
  align-items: center;
  justify-content: center;
  padding: var(--space-6);
  animation: fadeIn 150ms ease-out;
}

.modal-overlay.open {
  display: flex;
}

.modal {
  background: var(--bg-surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xl);
  width: 100%;
  max-width: 520px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  animation: modalSlideUp 200ms cubic-bezier(0.16, 1, 0.3, 1);
}

.modal-sm { max-width: 400px; }
.modal-lg { max-width: 680px; }
.modal-xl { max-width: 880px; }

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-5) var(--space-6);
  border-bottom: 1px solid var(--border-divider);
  flex-shrink: 0;
}

.modal-title {
  font-size: var(--fs-lg);
  font-weight: 600;
  color: var(--text-heading);
}

.modal-close {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  transition: all var(--transition-fast);
  background: none;
  border: none;
  cursor: pointer;
}

.modal-close:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.modal-close svg {
  width: 18px;
  height: 18px;
}

.modal-body {
  padding: var(--space-6);
  overflow-y: auto;
  flex: 1;
}

.modal-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-6);
  border-top: 1px solid var(--border-divider);
  flex-shrink: 0;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes modalSlideUp {
  from { opacity: 0; transform: translateY(16px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

/* ---------- Toast Notifications ---------- */
.toast-container {
  position: fixed;
  top: var(--space-4);
  right: var(--space-4);
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  pointer-events: none;
}

.toast {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  min-width: 320px;
  max-width: 420px;
  pointer-events: auto;
  animation: toastSlideIn 200ms cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes toastSlideIn {
  from { opacity: 0; transform: translateX(24px); }
  to { opacity: 1; transform: translateX(0); }
}

.toast-icon {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  margin-top: 1px;
}

.toast-success .toast-icon { color: var(--success-text); }
.toast-error .toast-icon { color: var(--error-text); }
.toast-warning .toast-icon { color: var(--warning-text); }
.toast-info .toast-icon { color: var(--info-text); }

.toast-content {
  flex: 1;
  min-width: 0;
}

.toast-title {
  font-size: var(--fs-sm);
  font-weight: 600;
  color: var(--text-heading);
  margin-bottom: 2px;
}

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

.toast-close {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  color: var(--text-muted);
  cursor: pointer;
  transition: color var(--transition-fast);
}

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

/* ---------- Dropdown Menu ---------- */
.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-menu {
  display: none;
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  padding: var(--space-1);
  min-width: 180px;
  z-index: 100;
  animation: scaleIn 120ms cubic-bezier(0.16, 1, 0.3, 1);
  transform-origin: top right;
}

.dropdown-menu.open {
  display: block;
}

.dropdown-item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  font-size: var(--fs-sm);
  font-weight: 500;
  color: var(--text-primary);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition-fast);
}

.dropdown-item:hover {
  background: var(--bg-hover);
}

.dropdown-item svg {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
  color: var(--text-muted);
}

.dropdown-item-danger {
  color: var(--error-text);
}

.dropdown-item-danger svg {
  color: var(--error-text);
}

.dropdown-item-danger:hover {
  background: var(--error-bg);
}

.dropdown-divider {
  height: 1px;
  background: var(--border-divider);
  margin: var(--space-1) 0;
}

/* ---------- Spinner ---------- */
.spinner {
  border: 2px solid var(--neutral-200);
  border-top-color: var(--primary-500);
  border-radius: 50%;
  width: 20px;
  height: 20px;
  animation: spin 0.6s linear infinite;
}

.spinner-lg {
  width: 32px;
  height: 32px;
  border-width: 3px;
}

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