/* ==========================================================================
   Reusable Components
   ========================================================================== */

/* --------------------------------------------------------------------------
   Buttons
   -------------------------------------------------------------------------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-sm);
  padding: 0.625rem 1.25rem;
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  font-weight: 600;
  line-height: 1.4;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--transition-fast);
  white-space: nowrap;
  text-decoration: none;
}

/* Single focus treatment: outline only (UI-review #33). The paired
   shadow was redundant and, combined with the outline, caused adjacent
   layout shift in dense forms. Relies on the global `:focus-visible`
   ring from style.css so all focusable elements look the same. */
.btn:focus-visible {
  outline: 2px solid var(--focus-ring-color);
  outline-offset: 2px;
}

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

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

.btn-primary:hover:not(:disabled) {
  background: var(--color-primary-hover);
  border-color: var(--color-primary-hover);
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

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

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

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

.btn-danger:hover:not(:disabled) {
  background: #b91c1c;
  border-color: #b91c1c;
}

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

.btn-ghost:hover:not(:disabled) {
  background: var(--color-border-light);
  color: var(--color-text);
}

.btn-sm {
  padding: 0.375rem 0.75rem;
  font-size: var(--text-xs);
}

.btn-lg {
  padding: 0.875rem 2rem;
  font-size: var(--text-base);
  border-radius: var(--radius-lg);
}

.btn-block {
  width: 100%;
}

/* --------------------------------------------------------------------------
   Cards
   -------------------------------------------------------------------------- */
.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-xl);
  box-shadow: var(--shadow-xs);
  transition: box-shadow var(--transition-base), transform var(--transition-base);
}

.card:hover {
  box-shadow: var(--shadow-md);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-md);
}

.card-title {
  font-size: var(--text-lg);
  font-weight: 600;
}

.card-body {
  color: var(--color-text-secondary);
}

.card-footer {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  margin-top: var(--space-lg);
  padding-top: var(--space-lg);
  border-top: 1px solid var(--color-border-light);
}

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

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

.form-input {
  width: 100%;
  padding: 0.625rem 0.875rem;
  font-family: var(--font-sans);
  font-size: var(--text-base);
  color: var(--color-text);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
  outline: none;
}

.form-input:focus {
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-light);
  outline: 2px solid transparent; /* fallback for high-contrast mode */
}

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

.form-input.error {
  border-color: var(--color-danger);
  box-shadow: 0 0 0 3px var(--color-danger-light);
}

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

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

/* --------------------------------------------------------------------------
   Badges / Status Pills
   -------------------------------------------------------------------------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.125rem 0.625rem;
  font-size: var(--text-xs);
  font-weight: 600;
  border-radius: var(--radius-full);
  text-transform: capitalize;
}

.badge-pending,
.badge-uploading,
.badge-uploaded {
  background: var(--color-border-light);
  color: var(--color-text-secondary);
}

.badge-queued {
  background: var(--color-primary-light);
  color: #1e40af;
}

.badge-rendering {
  background: var(--color-warning-light);
  color: #78350f;
}

.badge-completed {
  background: var(--color-success-light);
  color: #15803d;
}

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

.badge-deleted {
  background: var(--color-border-light);
  color: var(--color-text-muted);
}

/* --------------------------------------------------------------------------
   Progress Bar
   -------------------------------------------------------------------------- */
.progress-bar {
  width: 100%;
  height: 6px;
  background: var(--color-border-light);
  border-radius: var(--radius-full);
  overflow: hidden;
}

.progress-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--color-primary), #60a5fa);
  border-radius: var(--radius-full);
  transition: width var(--transition-slow);
  min-width: 0;
}

.progress-bar-fill.rendering {
  background: linear-gradient(90deg, var(--color-warning), #fbbf24);
  animation: progress-pulse 2s ease-in-out infinite;
}

/* Indeterminate bar for queued / "starting" states where percent is
   unknown. A moving stripe conveys activity without lying about
   progress (UI-review #17). */
.progress-bar-fill.indeterminate {
  width: 40%;
  background: linear-gradient(
    90deg,
    transparent,
    var(--color-primary) 40%,
    var(--color-primary) 60%,
    transparent
  );
  background-size: 200% 100%;
  animation: progress-indeterminate 1.4s ease-in-out infinite;
}

@keyframes progress-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.7; }
}

@keyframes progress-indeterminate {
  from { transform: translateX(-100%); }
  to   { transform: translateX(250%); }
}

@media (prefers-reduced-motion: reduce) {
  .progress-bar-fill.rendering,
  .progress-bar-fill.indeterminate {
    animation: none;
  }
}

/* --------------------------------------------------------------------------
   Alerts
   -------------------------------------------------------------------------- */
.alert {
  padding: var(--space-md) var(--space-lg);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  display: flex;
  align-items: flex-start;
  gap: var(--space-sm);
}

/* Alert foregrounds use --color-*-dark variants to pass WCAG AA on the
   corresponding pale backgrounds. The base --color-* tokens are retained
   for use on white backgrounds (buttons, solid icons). */
.alert-error {
  background: var(--color-danger-light);
  color: var(--color-danger-dark);
  border: 1px solid #fecaca;
}

.alert-success {
  background: var(--color-success-light);
  color: var(--color-success-dark);
  border: 1px solid #bbf7d0;
}

.alert-info {
  background: var(--color-primary-light);
  color: var(--color-primary-dark);
  border: 1px solid #bfdbfe;
}

/* --------------------------------------------------------------------------
   Empty State
   -------------------------------------------------------------------------- */
.empty-state {
  text-align: center;
  padding: var(--space-3xl) var(--space-xl);
  color: var(--color-text-muted);
}

.empty-state-icon {
  font-size: 3rem;
  margin-bottom: var(--space-lg);
  opacity: 0.5;
}

.empty-state h3 {
  color: var(--color-text-secondary);
  margin-bottom: var(--space-sm);
}

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

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

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

/* --------------------------------------------------------------------------
   Upload Zone
   -------------------------------------------------------------------------- */
/* Rendered as a <label for="fileInput"> so native click/keyboard activation
   opens the file picker without custom JS. Must be display:block since
   <label> is inline by default. */
.upload-zone {
  display: block;
  border: 2px dashed var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-2xl);
  text-align: center;
  cursor: pointer;
  transition: all var(--transition-base);
  background: var(--color-surface);
}

.upload-zone:hover,
.upload-zone.dragover,
.upload-zone:focus-within {
  border-color: var(--color-primary);
  background: var(--color-primary-light);
}

.upload-zone-icon {
  font-size: 2.5rem;
  margin-bottom: var(--space-md);
  opacity: 0.6;
}

.upload-zone h3 {
  margin-bottom: var(--space-xs);
}

.upload-zone p {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

.upload-zone input[type="file"] {
  display: none;
}

/* --------------------------------------------------------------------------
   Modal / Dialog
   -------------------------------------------------------------------------- */
/* Controlled by the native [hidden] attribute: JS adds/removes it.
   When hidden, the attribute also takes the element out of the tab order. */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

.modal-overlay[hidden] {
  display: none;
}

.modal {
  background: var(--color-surface);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-xl);
  width: 90%;
  max-width: 480px;
  padding: var(--space-xl);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-md);
}

.modal-header h3 {
  margin: 0;
  font-size: var(--text-xl);
}

.modal-body {
  margin-bottom: var(--space-lg);
  color: var(--color-text-secondary);
  line-height: 1.5;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-sm);
}

.modal-close {
  background: none;
  border: none;
  cursor: pointer;
  font-size: var(--text-xl);
  color: var(--color-text-muted);
  padding: var(--space-xs);
  line-height: 1;
  border-radius: var(--radius-sm);
}

.modal-close:hover {
  color: var(--color-text);
  background: var(--color-border-light);
}

.modal-close:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

/* --------------------------------------------------------------------------
   Toast region
   -------------------------------------------------------------------------- */
.toast-region {
  position: fixed;
  right: var(--space-lg);
  bottom: var(--space-lg);
  z-index: 150;
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  max-width: 420px;
  pointer-events: none; /* children re-enable */
}

.toast {
  pointer-events: auto;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-md);
  box-shadow: var(--shadow-md);
  animation: toast-in 180ms ease-out;
}

.toast-dismissing {
  animation: toast-out 180ms ease-in forwards;
}

.toast-message {
  flex: 1;
}

.toast-close {
  background: none;
  border: none;
  color: inherit;
  cursor: pointer;
  font-size: var(--text-xl);
  line-height: 1;
  padding: 0 var(--space-xs);
  opacity: 0.7;
  border-radius: var(--radius-sm);
}

.toast-close:hover { opacity: 1; }

.toast-close:focus-visible {
  outline: 2px solid currentColor;
  outline-offset: 2px;
}

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

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

@media (prefers-reduced-motion: reduce) {
  .toast, .toast-dismissing { animation: none; }
}

/* --------------------------------------------------------------------------
   Grid
   -------------------------------------------------------------------------- */
.grid {
  display: grid;
  gap: var(--space-lg);
}

.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }

@media (max-width: 768px) {
  .grid-2, .grid-3 {
    grid-template-columns: 1fr;
  }
}

/* --------------------------------------------------------------------------
   Utility: visually-hidden (accessible but not visible)
   -------------------------------------------------------------------------- */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

/* --------------------------------------------------------------------------
   Skeleton loading (UI-review #20)
   Shown while data loads so placeholder dashes ("--") never flash.
   Parent elements flip data-loading="false" from JS to hide them.
   -------------------------------------------------------------------------- */
.skeleton {
  display: inline-block;
  background: linear-gradient(
    90deg,
    var(--color-border-light) 0%,
    var(--color-border) 50%,
    var(--color-border-light) 100%
  );
  background-size: 200% 100%;
  border-radius: var(--radius-sm);
  animation: skeleton-shimmer 1.4s ease-in-out infinite;
}

.skeleton-text {
  height: 1em;
  vertical-align: middle;
}

.skeleton-plan-name   { width: 5rem; height: 1.25em; }
.skeleton-plan-status { width: 3.5rem; height: 1em; border-radius: var(--radius-full); }
.skeleton-usage       { width: 3rem; }

/* Hide real content while loading; replaced by .skeleton spans inside. */
[data-loading="false"] .skeleton { display: none; }

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

@media (prefers-reduced-motion: reduce) {
  .skeleton { animation: none; }
}

/* --------------------------------------------------------------------------
   Empty-state CTA (UI-review #18)
   -------------------------------------------------------------------------- */
.empty-state-cta {
  margin-top: var(--space-lg);
}

/* --------------------------------------------------------------------------
   Small spinner (inline, e.g. "Processing..." label)
   -------------------------------------------------------------------------- */
.spinner-sm {
  width: 14px;
  height: 14px;
  border-width: 2px;
}
