/* ============================================
   Components — Buttons, Inputs, Cards, Badges,
   Modals, Toasts, Tables, Progress
   All touch-optimized (44px min targets)
   ============================================ */

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  padding: 0.625rem 1.25rem; min-height: var(--touch-min); border: none; border-radius: var(--radius);
  font-weight: 600; font-size: 0.875rem; cursor: pointer; transition: all var(--transition);
  white-space: nowrap; user-select: none; -webkit-user-select: none;
  touch-action: manipulation; /* prevent double-tap zoom on iOS */
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
.btn--primary { background: var(--primary); color: white; }
.btn--primary:hover { background: var(--primary-dark); }
.btn--success { background: var(--success); color: white; }
.btn--danger { background: var(--danger); color: white; }
.btn--warning { background: var(--warning); color: white; }
.btn--outline { background: transparent; border: 1.5px solid var(--border-color); color: var(--text-primary); }
.btn--outline:hover { background: var(--bg-tertiary); border-color: var(--text-muted); }
.btn--ghost { background: transparent; color: var(--text-secondary); }
.btn--ghost:hover { background: var(--bg-tertiary); color: var(--text-primary); }
.btn--sm { padding: 0.375rem 0.75rem; min-height: 36px; font-size: 0.8125rem; }
.btn--lg { padding: 0.75rem 1.5rem; min-height: 52px; font-size: 1rem; }
.btn--icon { width: var(--touch-min); padding: 0; }
.btn--block { width: 100%; }

/* Inputs */
.input, .select {
  width: 100%; padding: 0.625rem 0.875rem; min-height: var(--touch-min);
  background: var(--bg-primary); border: 1.5px solid var(--border-color); border-radius: var(--radius);
  font-size: 0.9375rem; color: var(--text-primary); transition: border-color var(--transition);
}
.input:focus, .select:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(15,118,110,0.1); outline: none; }
.input::placeholder { color: var(--text-muted); }
.input--lg { min-height: 52px; font-size: 1rem; padding: 0.75rem 1rem; }
.input-group { display: flex; gap: var(--space-2); align-items: stretch; }
.input-group .input { flex: 1; }
.form-group { margin-bottom: var(--space-4); }
.form-label { display: block; font-size: 0.8125rem; font-weight: 600; color: var(--text-secondary); margin-bottom: var(--space-1); }
textarea.input { min-height: 80px; resize: vertical; }
.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 12 12'%3E%3Cpath fill='%2394a3b8' d='M2 4l4 4 4-4'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 12px center; padding-right: 32px; }

/* Cards */
.card {
  background: var(--bg-primary); border: 1px solid var(--border-color); border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm); transition: box-shadow var(--transition);
}
.card:hover { box-shadow: var(--shadow); }
.card--clickable { cursor: pointer; }
.card--clickable:active { transform: scale(0.99); }

/* Badges */
.badge {
  display: inline-flex; align-items: center; padding: 2px 8px; border-radius: var(--radius-full);
  font-size: 0.6875rem; font-weight: 700; letter-spacing: 0.02em; text-transform: uppercase;
  line-height: 1.5;
}
.badge--success { background: var(--success-light); color: var(--success); }
.badge--danger { background: var(--danger-light); color: var(--danger); }
.badge--warning { background: var(--warning-light); color: var(--warning); }
.badge--info { background: var(--info-light); color: var(--info); }
.badge--otc { background: #ecfdf5; color: var(--otc); }
.badge--rx { background: #f5f3ff; color: var(--prescription); }
.badge--controlled { background: #fef2f2; color: var(--controlled); }
.badge--supplement { background: #ecfeff; color: var(--supplement); }
.badge--expired { background: var(--danger-light); color: var(--expired); animation: pulse-danger 2s infinite; }
@keyframes pulse-danger { 0%,100% { opacity: 1; } 50% { opacity: 0.7; } }

/* Tabs */
.tabs { display: flex; gap: 2px; background: var(--bg-tertiary); border-radius: var(--radius); padding: 3px; }
.tab {
  flex: 1; padding: 0.5rem 1rem; min-height: var(--touch-min); border: none; background: transparent;
  font-size: 0.8125rem; font-weight: 600; color: var(--text-muted); border-radius: var(--radius-sm);
  cursor: pointer; transition: all var(--transition); text-align: center;
}
.tab.active { background: var(--bg-primary); color: var(--text-primary); box-shadow: var(--shadow-sm); }

/* Progress */
.progress { height: 8px; background: var(--bg-tertiary); border-radius: var(--radius-full); overflow: hidden; }
.progress__bar { height: 100%; background: var(--primary); border-radius: var(--radius-full); transition: width 0.5s ease; }

/* Tables */
.data-table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: 0.8125rem; }
.data-table thead th { background: var(--bg-tertiary); font-weight: 600; text-align: left; padding: 0.75rem 1rem; border-bottom: 1px solid var(--border-color); position: sticky; top: 0; z-index: 1; }
.data-table tbody td { padding: 0.75rem 1rem; border-bottom: 1px solid var(--border-light); vertical-align: middle; }
.data-table tbody tr:hover { background: var(--bg-secondary); }
.data-table tbody tr:last-child td { border-bottom: none; }

/* Modals */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5); display: flex; align-items: flex-end;
  justify-content: center; z-index: 9000; backdrop-filter: blur(4px); animation: fadeIn 0.15s ease;
}
@media (min-width: 640px) { .modal-overlay { align-items: center; } }
.modal {
  background: var(--bg-primary); border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  width: 100%; max-width: 520px; max-height: 90vh; display: flex; flex-direction: column;
  box-shadow: var(--shadow-lg); animation: slideUp 0.2s ease;
}
@media (min-width: 640px) { .modal { border-radius: var(--radius-lg); } }
.modal__header { padding: var(--space-5) var(--space-5) var(--space-3); display: flex; align-items: center; justify-content: space-between; }
.modal__header h3 { font-size: 1.125rem; }
.modal__body { padding: 0 var(--space-5) var(--space-5); overflow-y: auto; flex: 1; }
.modal__footer { padding: var(--space-4) var(--space-5); border-top: 1px solid var(--border-color); display: flex; gap: var(--space-2); justify-content: flex-end; }
.modal--lg { max-width: 680px; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

/* Toast */
.toast-container { position: fixed; top: var(--space-4); right: var(--space-4); z-index: 9999; display: flex; flex-direction: column; gap: var(--space-2); pointer-events: none; }
@media (max-width: 640px) { .toast-container { left: var(--space-4); right: var(--space-4); top: auto; bottom: calc(var(--space-4) + env(safe-area-inset-bottom, 0px)); } }
.toast {
  padding: 0.75rem 1rem; border-radius: var(--radius); font-size: 0.875rem; font-weight: 500;
  background: var(--bg-dark); color: var(--text-inverse); box-shadow: var(--shadow-lg);
  pointer-events: auto; animation: slideIn 0.25s ease, fadeOut 0.3s ease 3.5s forwards;
  max-width: 400px;
}
.toast--success { border-left: 4px solid var(--success); }
.toast--warning { border-left: 4px solid var(--warning); }
.toast--danger { border-left: 4px solid var(--danger); }
.toast--info { border-left: 4px solid var(--info); }
@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes fadeOut { from { opacity: 1; } to { opacity: 0; } }

/* Toggle Switch */
.toggle { position: relative; display: inline-block; width: 44px; height: 24px; cursor: pointer; }
.toggle input { display: none; }
.toggle__track { position: absolute; inset: 0; background: var(--bg-tertiary); border-radius: var(--radius-full); transition: background var(--transition); border: 1.5px solid var(--border-color); }
.toggle input:checked + .toggle__track { background: var(--primary); border-color: var(--primary); }
.toggle__thumb { position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; background: white; border-radius: 50%; transition: transform var(--transition); box-shadow: 0 1px 3px rgba(0,0,0,0.2); }
.toggle input:checked ~ .toggle__thumb { transform: translateX(20px); }

/* Empty State */
.empty-state { text-align: center; padding: var(--space-10) var(--space-6); }
.empty-state__icon { font-size: 3rem; margin-bottom: var(--space-4); opacity: 0.7; }

/* Spinner */
.spinner { width: 32px; height: 32px; border: 3px solid var(--border-color); border-top-color: var(--primary); border-radius: 50%; animation: spin 0.7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Chip / Category pill */
.chip { display: inline-flex; align-items: center; padding: 6px 14px; border-radius: var(--radius-full); font-size: 0.8125rem; font-weight: 500; background: var(--bg-tertiary); color: var(--text-secondary); border: 1px solid var(--border-color); cursor: pointer; white-space: nowrap; min-height: 36px; transition: all var(--transition); touch-action: manipulation; }
.chip:hover { background: var(--primary-50); border-color: var(--primary); color: var(--primary); }
.chip.active { background: var(--primary); color: white; border-color: var(--primary); }

/* Stat Card */
.stat-card { padding: var(--space-4); text-align: center; }
.stat-card__value { font-size: 1.5rem; font-weight: 800; font-family: var(--font-mono); letter-spacing: -0.02em; line-height: 1.2; }
.stat-card__label { font-size: 0.75rem; color: var(--text-muted); font-weight: 500; margin-top: 2px; }

/* Alert Card (pharmacy) */
.alert-card { padding: var(--space-4); border-radius: var(--radius); border-left: 4px solid; display: flex; align-items: flex-start; gap: var(--space-3); }
.alert-card--danger { background: var(--danger-light); border-left-color: var(--danger); }
.alert-card--warning { background: var(--warning-light); border-left-color: var(--warning); }
.alert-card--info { background: var(--info-light); border-left-color: var(--info); }
.alert-card--success { background: var(--success-light); border-left-color: var(--success); }

/* Offline Banner */
.offline-banner { position: fixed; top: 0; left: 0; right: 0; z-index: 9500; background: var(--warning); color: #000; font-size: 0.8125rem; font-weight: 600; text-align: center; padding: 6px var(--space-4); display: flex; align-items: center; justify-content: center; gap: var(--space-2); }
.sync-bar { position: fixed; top: 0; left: 0; right: 0; height: 3px; z-index: 9500; background: var(--bg-tertiary); }
.sync-bar__progress { height: 100%; background: var(--primary); transition: width 0.3s; }

/* Button spinner */
.btn-spinner {
  display: inline-block;
  width: 14px; height: 14px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: btn-spin 0.6s linear infinite;
  vertical-align: middle;
}
@keyframes btn-spin { to { transform: rotate(360deg); } }
.btn:disabled { opacity: 0.65; cursor: not-allowed; pointer-events: none; }
