/**
 * LifeOS Design System — Reusable UI Components
 * Complete 16 Foundation Components
 */

/* ==========================================================================
   1, 2, 3. BUTTONS (Primary, Secondary, Outline, Sizes, Icons)
   ========================================================================== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: 0.55rem 1.15rem;
  font-family: var(--font-family);
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
  line-height: 1.25;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
  text-decoration: none;
  transition: all var(--transition-fast);
  white-space: nowrap;
  user-select: none;
}

.btn:disabled, .btn[disabled] {
  opacity: 0.45;
  cursor: not-allowed;
  pointer-events: none;
}

/* Primary Button */
.btn-primary {
  background-color: var(--accent-primary);
  color: #0F1117;
  font-weight: var(--font-weight-semibold);
  border-color: var(--accent-primary);
  box-shadow: 0 1px 3px rgba(129, 140, 248, 0.3);
}

.btn-primary:hover:not(:disabled) {
  background-color: #939BF9;
  border-color: #939BF9;
  color: #0B0D12;
  box-shadow: var(--shadow-glow);
}

.btn-primary:active:not(:disabled) {
  background-color: var(--accent-primary-hover);
  border-color: var(--accent-primary-hover);
}

/* Secondary Button */
.btn-secondary {
  background-color: var(--bg-card);
  color: var(--text-primary);
  border-color: var(--border);
}

.btn-secondary:hover:not(:disabled) {
  background-color: var(--bg-card-hover);
  border-color: var(--text-muted);
  color: var(--text-primary);
}

.btn-secondary:active:not(:disabled) {
  background-color: var(--border);
}

/* Outline Button */
.btn-outline {
  background-color: transparent;
  color: var(--accent-primary);
  border-color: var(--accent-primary);
}

.btn-outline:hover:not(:disabled) {
  background-color: var(--accent-primary-subtle);
  border-color: var(--accent-secondary);
  color: var(--accent-secondary);
}

/* Ghost / Subdued Button */
.btn-ghost {
  background-color: transparent;
  color: var(--text-secondary);
  border-color: transparent;
}

.btn-ghost:hover:not(:disabled) {
  background-color: var(--accent-primary-subtle);
  color: var(--text-primary);
}

/* Button Sizes */
.btn-sm {
  padding: 0.35rem 0.75rem;
  font-size: var(--font-size-xs);
}

.btn-lg {
  padding: 0.75rem 1.5rem;
  font-size: var(--font-size-base);
}

.btn-icon-only {
  padding: 0.55rem;
  width: 36px;
  height: 36px;
}

/* ==========================================================================
   4, 5, 6. FORMS & INPUTS (Input field, Textarea, Select, Labels)
   ========================================================================== */
.form-group {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  margin-bottom: var(--space-4);
}

.form-label {
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
  color: var(--text-primary);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.form-label-optional {
  font-size: var(--font-size-xs);
  color: var(--text-muted);
}

.form-control, .form-select {
  width: 100%;
  padding: 0.6rem 0.9rem;
  font-family: var(--font-family);
  font-size: var(--font-size-sm);
  color: var(--text-primary);
  background-color: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.form-control::placeholder {
  color: var(--text-muted);
}

.form-control:focus, .form-select:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(129, 140, 248, 0.18);
}

.form-control:disabled, .form-select:disabled {
  opacity: 0.5;
  background-color: var(--bg-primary);
  cursor: not-allowed;
}

textarea.form-control {
  min-height: 90px;
  resize: vertical;
  line-height: var(--line-height-normal);
}

.form-select {
  appearance: none;
  -webkit-appearance: none;
  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='%239CA3AF' 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 0.85rem center;
  background-size: 16px 16px;
  padding-right: 2.25rem;
  cursor: pointer;
}

.form-select option {
  background-color: var(--bg-card);
  color: var(--text-primary);
}

.form-help-text {
  font-size: var(--font-size-xs);
  color: var(--text-muted);
}

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

/* ==========================================================================
   7, 8. CHECKBOX & TOGGLE SWITCH
   ========================================================================== */
.custom-checkbox {
  display: inline-flex;
  align-items: center;
  gap: var(--space-3);
  cursor: pointer;
  user-select: none;
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
}

.custom-checkbox input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  border: 1px solid var(--border);
  border-radius: var(--radius-xs);
  background-color: var(--bg-input);
  display: grid;
  place-content: center;
  cursor: pointer;
  transition: all var(--transition-fast);
}

.custom-checkbox input[type="checkbox"]:checked {
  background-color: var(--accent-primary);
  border-color: var(--accent-primary);
}

.custom-checkbox input[type="checkbox"]:checked::before {
  content: "";
  width: 10px;
  height: 6px;
  border-left: 2px solid #0F1117;
  border-bottom: 2px solid #0F1117;
  transform: rotate(-45deg) translate(1px, -1px);
}

.custom-checkbox input[type="checkbox"]:focus-visible {
  outline: 2px solid var(--accent-primary);
  outline-offset: 2px;
}

/* Toggle Switch */
.toggle-switch {
  display: inline-flex;
  align-items: center;
  gap: var(--space-3);
  cursor: pointer;
  user-select: none;
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
}

.toggle-switch input[type="checkbox"] {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-track {
  width: 40px;
  height: 22px;
  background-color: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  position: relative;
  transition: background-color var(--transition-fast), border-color var(--transition-fast);
}

.toggle-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background-color: var(--text-secondary);
  transition: transform var(--transition-fast), background-color var(--transition-fast);
}

.toggle-switch input:checked + .toggle-track {
  background-color: var(--accent-primary-subtle);
  border-color: var(--accent-primary);
}

.toggle-switch input:checked + .toggle-track .toggle-thumb {
  transform: translateX(18px);
  background-color: var(--accent-primary);
}

/* ==========================================================================
   9. CARDS & METRIC CARDS
   ========================================================================== */
.card {
  background-color: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.card:hover {
  border-color: var(--border-focus);
}

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

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

.card-subtitle {
  font-size: var(--font-size-xs);
  color: var(--text-muted);
}

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

.card-footer {
  padding: var(--space-4) var(--space-5);
  border-top: 1px solid var(--border-subtle);
  background-color: rgba(15, 17, 23, 0.4);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

/* Metric / Analytic Cards */
.metric-card {
  padding: var(--space-5);
  background-color: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  position: relative;
  transition: border-color var(--transition-fast), transform var(--transition-fast);
}

.metric-card:hover {
  border-color: var(--accent-primary);
  transform: translateY(-2px);
}

.metric-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.metric-label {
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-medium);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.metric-icon-wrap {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  background-color: var(--accent-primary-subtle);
  color: var(--accent-primary);
  display: flex;
  align-items: center;
  justify-content: center;
}

.metric-value {
  font-size: var(--font-size-2xl);
  font-weight: var(--font-weight-bold);
  color: var(--text-primary);
  line-height: 1.1;
}

.metric-trend {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-medium);
}

.trend-up { color: var(--success); }
.trend-down { color: var(--danger); }
.trend-neutral { color: var(--text-muted); }

/* ==========================================================================
   10. MODAL DIALOG
   ========================================================================== */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background-color: var(--bg-modal-backdrop);
  z-index: var(--z-modal);
  display: none;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  animation: modalFadeIn var(--transition-fast);
}

.modal-backdrop.show {
  display: flex;
}

.modal-dialog {
  width: 100%;
  max-width: 520px;
  background-color: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: modalSlideIn var(--transition-fast);
}

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

@keyframes modalSlideIn {
  from { transform: scale(0.96) translateY(-8px); }
  to { transform: scale(1) translateY(0); }
}

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

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

.modal-close-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: var(--space-1);
  border-radius: var(--radius-xs);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color var(--transition-fast);
}

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

.modal-body {
  padding: var(--space-5);
  overflow-y: auto;
  max-height: 70vh;
}

.modal-footer {
  padding: var(--space-4) var(--space-5);
  border-top: 1px solid var(--border-subtle);
  background-color: rgba(15, 17, 23, 0.4);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-3);
}

/* ==========================================================================
   11. BADGES
   ========================================================================== */
.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 0.2rem 0.55rem;
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-medium);
  border-radius: var(--radius-full);
  border: 1px solid transparent;
  line-height: 1.1;
  white-space: nowrap;
}

.badge-accent {
  background-color: var(--accent-primary-subtle);
  color: var(--accent-primary);
  border-color: rgba(129, 140, 248, 0.25);
}

.badge-success {
  background-color: var(--success-subtle);
  color: var(--success);
  border-color: rgba(52, 211, 153, 0.25);
}

.badge-warning {
  background-color: var(--warning-subtle);
  color: var(--warning);
  border-color: rgba(251, 191, 36, 0.25);
}

.badge-danger {
  background-color: var(--danger-subtle);
  color: var(--danger);
  border-color: rgba(248, 113, 113, 0.25);
}

.badge-neutral {
  background-color: var(--bg-input);
  color: var(--text-secondary);
  border-color: var(--border);
}

/* ==========================================================================
   12. PROGRESS BARS
   ========================================================================== */
.progress-bar-wrap {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  width: 100%;
}

.progress-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: var(--font-size-xs);
}

.progress-label {
  color: var(--text-secondary);
}

.progress-value {
  color: var(--text-primary);
  font-weight: var(--font-weight-semibold);
}

.progress-track {
  width: 100%;
  height: 8px;
  background-color: var(--bg-input);
  border-radius: var(--radius-full);
  overflow: hidden;
  border: 1px solid var(--border-subtle);
}

.progress-fill {
  height: 100%;
  border-radius: var(--radius-full);
  background-color: var(--accent-primary);
  transition: width var(--transition-normal);
}

.progress-fill.success { background-color: var(--success); }
.progress-fill.warning { background-color: var(--warning); }
.progress-fill.danger { background-color: var(--danger); }
.progress-fill.secondary { background-color: var(--accent-secondary); }

/* ==========================================================================
   13. TABLES
   ========================================================================== */
.table-container {
  width: 100%;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background-color: var(--bg-card);
  box-shadow: var(--shadow-sm);
}

.table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
  font-size: var(--font-size-sm);
}

.table th {
  padding: var(--space-3) var(--space-4);
  background-color: rgba(15, 17, 23, 0.6);
  color: var(--text-muted);
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-semibold);
  letter-spacing: 0.05em;
  text-transform: uppercase;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

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

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

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

.table tbody tr:hover {
  background-color: rgba(129, 140, 248, 0.04);
}

.table td.primary-cell {
  color: var(--text-primary);
  font-weight: var(--font-weight-medium);
}

/* ==========================================================================
   14. EMPTY STATE
   ========================================================================== */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: var(--space-10) var(--space-6);
  border: 1px dashed var(--border);
  border-radius: var(--radius-md);
  background-color: rgba(21, 24, 33, 0.4);
}

.empty-state-icon {
  width: 48px;
  height: 48px;
  color: var(--text-muted);
  margin-bottom: var(--space-3);
  stroke-width: 1.5;
}

.empty-state-title {
  font-size: var(--font-size-md);
  font-weight: var(--font-weight-semibold);
  color: var(--text-primary);
  margin-bottom: var(--space-1);
}

.empty-state-desc {
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
  max-width: 400px;
  margin-bottom: var(--space-5);
}

/* ==========================================================================
   15. ALERTS & NOTIFICATIONS
   ========================================================================== */
.alert {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-size: var(--font-size-sm);
  margin-bottom: var(--space-4);
  position: relative;
}

.alert-icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  margin-top: 2px;
}

.alert-content {
  flex: 1;
}

.alert-title {
  font-weight: var(--font-weight-semibold);
  margin-bottom: 2px;
}

.alert-close-btn {
  background: transparent;
  border: none;
  color: inherit;
  opacity: 0.6;
  cursor: pointer;
  padding: 2px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity var(--transition-fast);
}

.alert-close-btn:hover {
  opacity: 1;
}

.alert-info {
  background-color: var(--info-subtle);
  color: #93C5FD;
  border-color: rgba(96, 165, 250, 0.25);
}

.alert-success {
  background-color: var(--success-subtle);
  color: #6EE7B7;
  border-color: rgba(52, 211, 153, 0.25);
}

.alert-warning {
  background-color: var(--warning-subtle);
  color: #FCD34D;
  border-color: rgba(251, 191, 36, 0.25);
}

.alert-danger {
  background-color: var(--danger-subtle);
  color: #FCA5A5;
  border-color: rgba(248, 113, 113, 0.25);
}

/* ==========================================================================
   16. LOADING INDICATORS (Spinners, Skeletons)
   ========================================================================== */
.spinner {
  width: 22px;
  height: 22px;
  border: 2px solid var(--border);
  border-top-color: var(--accent-primary);
  border-radius: 50%;
  animation: spinnerRotate 0.8s linear infinite;
  display: inline-block;
}

.spinner-sm {
  width: 16px;
  height: 16px;
  border-width: 1.5px;
}

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

/* Skeleton Loading Shimmer */
.skeleton-box {
  background: linear-gradient(90deg, var(--bg-card) 25%, var(--border) 50%, var(--bg-card) 75%);
  background-size: 200% 100%;
  animation: skeletonShimmer 1.5s infinite;
  border-radius: var(--radius-xs);
  height: 1rem;
}

@keyframes skeletonShimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}
