/* ==========================================================================
 * components.css — Reusable UI Component Styles
 *
 * Shared styles for buttons, modals, toasts, forms, spinners, badges,
 * and skeleton loaders used across the application.
 *
 * All values reference CSS custom properties from variables.css.
 * Interactive elements meet 44px minimum touch target on mobile.
 *
 * Design reference: design.md Section 2.7 (lines 1802-2099)
 * Requirements: 6.4.1 (responsive design), 6.4.2 (touch gestures),
 *               6.4.3 (user-friendly messages)
 * ========================================================================== */


/* ==========================================================================
 * 1. BUTTONS
 * ========================================================================== */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-4);
  font-size: var(--font-size-sm);
  font-weight: 600;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: background-color var(--transition-fast),
              border-color var(--transition-fast);
  white-space: nowrap;
  min-height: 40px;
  line-height: 1;
  text-decoration: none;
}

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

/* --- Button variants --- */

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

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

.btn--danger {
  background: var(--color-danger);
  color: var(--color-text-inverse);
}

.btn--danger:hover:not(:disabled) {
  opacity: 0.9;
}

.btn--success {
  background: var(--color-success);
  color: var(--color-text-inverse);
}

.btn--success:hover:not(:disabled) {
  opacity: 0.9;
}

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

.btn--outline:hover:not(:disabled) {
  background: var(--color-bg);
}

/* --- Button sizes --- */

.btn--sm {
  padding: var(--space-1) var(--space-3);
  font-size: var(--font-size-xs);
  min-height: 32px;
}

.btn--lg {
  padding: var(--space-3) var(--space-6);
  font-size: var(--font-size-base);
  min-height: 48px;
}

/* --- Button modifiers --- */

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

/* Touch-friendly: minimum 44px tap target on mobile */
@media (max-width: 767px) {
  .btn {
    min-height: 44px;
    min-width: 44px;
  }
}


/* ==========================================================================
 * 2. MODALS
 * ========================================================================== */

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

.modal-content {
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 480px;
  max-height: 90vh;
  overflow-y: auto;
  z-index: var(--z-modal);
}

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

.modal-content__header h3 {
  font-size: var(--font-size-lg);
  font-weight: 600;
}

.modal-content__close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  font-size: var(--font-size-xl);
  color: var(--color-text-secondary);
  border-radius: var(--radius-sm);
  cursor: pointer;
  background: none;
  border: none;
  transition: background-color var(--transition-fast);
}

.modal-content__close:hover {
  background: var(--color-bg);
}

.modal-content__body {
  padding: var(--space-6);
}

.modal-content__footer {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-6);
  border-top: 1px solid var(--color-border);
}

/* Mobile bottom-sheet behavior: fixed to bottom, full width */
@media (max-width: 767px) {
  .modal-backdrop {
    align-items: flex-end;
    padding: 0;
  }

  .modal-content {
    max-width: 100%;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    max-height: 85vh;
    padding-bottom: env(safe-area-inset-bottom);
  }

  .modal-content__close {
    width: 44px;
    height: 44px;
  }
}

.modal-content__title {
  font-size: var(--font-size-lg);
  font-weight: 600;
  margin-bottom: var(--space-3);
}

.modal-content__actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-3);
  margin-top: var(--space-5);
}

/* Table selection grid for transfer/merge modals */
.modal-table-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: var(--space-2);
  max-height: 300px;
  overflow-y: auto;
}

.modal-table-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-1);
  padding: var(--space-3);
  border: 2px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface);
  cursor: pointer;
  transition: border-color 0.15s, background-color 0.15s;
  position: relative;
}

.modal-table-card:hover {
  border-color: var(--color-primary);
  background: var(--color-primary-light, rgba(33, 150, 243, 0.05));
}

.modal-table-card--selected {
  border-color: var(--color-primary);
  background: var(--color-primary-light, rgba(33, 150, 243, 0.1));
}

.modal-table-card__name {
  font-weight: 600;
  font-size: var(--font-size-sm);
}

.modal-table-card__label {
  font-size: var(--font-size-xs);
  color: var(--color-text-secondary);
}

.modal-table-card__check {
  position: absolute;
  top: 4px;
  right: 6px;
  color: var(--color-primary);
  font-weight: 700;
  font-size: var(--font-size-sm);
}

/* Locked notice for awaiting-payment state */
.order-detail__locked-notice {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3);
  background: var(--color-warning-bg, #fff3cd);
  border: 1px solid var(--color-warning-border, #ffc107);
  border-radius: var(--radius-md);
  color: var(--color-warning-text, #856404);
  font-size: var(--font-size-sm);
  width: 100%;
}


/* ==========================================================================
 * 3. TOASTS
 * ========================================================================== */

.toast-container {
  position: fixed;
  top: var(--space-4);
  right: var(--space-4);
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  max-width: 400px;
}

.toast {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  background: var(--color-surface);
  border-left: 4px solid;
}

/* --- Toast variants with left border accent color --- */

.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-primary);
}

.toast__icon {
  display: flex;
  align-items: center;
  flex-shrink: 0;
}

.toast__message {
  flex: 1;
  font-size: var(--font-size-sm);
  color: var(--color-text);
}

.toast__dismiss {
  background: none;
  border: none;
  cursor: pointer;
  font-size: var(--font-size-lg);
  color: var(--color-text-secondary);
  padding: var(--space-1);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

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

/* Alpine.js transition classes for toast enter/leave */
.toast-enter {
  opacity: 0;
  transform: translateX(100%);
}

.toast-leave {
  opacity: 0;
  transform: translateX(100%);
}

/* Mobile: reposition toasts above bottom navigation */
@media (max-width: 767px) {
  .toast-container {
    top: auto;
    bottom: calc(var(--bottom-nav-height) + env(safe-area-inset-bottom) + var(--space-4));
    right: var(--space-2);
    left: var(--space-2);
    max-width: 100%;
  }
}


/* ==========================================================================
 * 4. FORMS
 * ========================================================================== */

.form-group {
  margin-bottom: var(--space-4);
}

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

.form-input,
.form-select,
.form-textarea {
  width: 100%;
  padding: var(--space-2) var(--space-3);
  font-size: var(--font-size-base);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface);
  color: var(--color-text);
  transition: border-color var(--transition-fast);
  min-height: 40px;
}

.form-input:focus,
.form-select:focus,
.form-textarea:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

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

/* Form select: override default browser appearance for consistent styling */
.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 12 12'%3E%3Cpath fill='%2364748b' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right var(--space-3) center;
  padding-right: var(--space-8);
}

/* Error state */
.form-input--error {
  border-color: var(--color-danger);
}

.form-input--error:focus {
  box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.1);
}

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

/* Mobile: 16px font to prevent iOS zoom on focus, 44px touch target */
@media (max-width: 767px) {
  .form-input,
  .form-select,
  .form-textarea {
    font-size: 16px; /* Prevent iOS zoom on focus */
    min-height: 44px;
  }
}


/* ==========================================================================
 * 5. SPINNER (CSS-only loading indicator)
 * ========================================================================== */

.spinner {
  display: inline-block;
  width: 24px;
  height: 24px;
  border: 2px solid rgba(0, 0, 0, 0.1);
  border-left-color: currentColor;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

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

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

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

/* Full-screen loading overlay */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(255, 255, 255, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: var(--z-modal);
}

.loading-overlay__content {
  text-align: center;
}

.loading-overlay__content p {
  margin-top: var(--space-4);
  color: var(--color-text-secondary);
  font-size: var(--font-size-sm);
}


/* ==========================================================================
 * 6. BADGES (status indicators)
 * ========================================================================== */

.badge {
  display: inline-block;
  padding: var(--space-1) var(--space-2);
  font-size: var(--font-size-xs);
  font-weight: 600;
  border-radius: var(--radius-full);
  white-space: nowrap;
  line-height: 1.2;
}

.badge--success {
  background: #dcfce7;
  color: #166534;
}

.badge--warning {
  background: #fefce8;
  color: #854d0e;
}

.badge--danger {
  background: #fef2f2;
  color: #991b1b;
}

.badge--muted {
  background: #f1f5f9;
  color: #64748b;
}


/* ==========================================================================
 * 7. SKELETON LOADERS (loading placeholders with shimmer animation)
 * ========================================================================== */

.skeleton {
  background: linear-gradient(90deg, #e2e8f0 25%, #f1f5f9 50%, #e2e8f0 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  border-radius: var(--radius-sm);
}

.skeleton--text {
  height: 14px;
  margin-bottom: var(--space-2);
}

.skeleton--rect {
  border-radius: var(--radius-md);
}

.skeleton--row {
  display: flex;
  gap: var(--space-4);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--color-border);
}

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


/* ==========================================================================
 * 8. DATA TABLE (reusable table component)
 * ========================================================================== */

.data-table-wrapper {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

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

.data-table th {
  text-align: left;
  padding: var(--space-3) var(--space-4);
  font-size: var(--font-size-xs);
  font-weight: 600;
  text-transform: uppercase;
  color: var(--color-text-secondary);
  border-bottom: 2px solid var(--color-border);
  white-space: nowrap;
}

.data-table td {
  padding: var(--space-3) var(--space-4);
  font-size: var(--font-size-sm);
  border-bottom: 1px solid var(--color-border);
}

.data-table tr:hover {
  background: var(--color-bg);
}

/* ── Focus States (WCAG 2.1 AA) ── */
:focus-visible {
  outline: 3px solid var(--color-focus-ring);
  outline-offset: 2px;
}
:focus:not(:focus-visible) {
  outline: none;
}

/* ── Form: Success State ── */
.input--success {
  border-color: var(--color-success);
  box-shadow: 0 0 0 3px rgba(22, 163, 74, 0.2);
}

/* ── Modal Enhancement ── */
.modal-backdrop {
  backdrop-filter: blur(var(--backdrop-blur));
  -webkit-backdrop-filter: blur(var(--backdrop-blur));
}
.modal-content {
  animation: modal-enter 200ms ease;
}
@keyframes modal-enter {
  from { opacity: 0; transform: scale(0.95); }
  to { opacity: 1; transform: scale(1); }
}

/* ── Cart Item Animation ── */
.cart-item--added {
  animation: highlight-pop 300ms ease;
}
@keyframes highlight-pop {
  0% { background-color: var(--color-primary-light); transform: scale(1.02); }
  100% { background-color: transparent; transform: scale(1); }
}
