/* ========================================
   SHARED STYLES - Centralized Design System
   ======================================== */

:root {
  /* ===== COLOR SYSTEM ===== */
  /* Primary Colors */
  --color-primary: #3b82f6;
  --color-primary-hover: #2563eb;
  --color-primary-active: #1d4ed8;
  
  /* Secondary Colors */
  --color-secondary: #64748b;
  --color-secondary-hover: #475569;
  
  /* Background Colors */
  --color-bg-primary: #f8fafc;
  --color-bg-secondary: #ffffff;
  --color-bg-tertiary: #f1f5f9;
  --color-bg-hover: #f1f5f9;
  
  /* Text Colors */
  --color-text-primary: #1e293b;
  --color-text-secondary: #64748b;
  --color-text-muted: #94a3b8;
  --color-text-inverse: #ffffff;
  
  /* Border Colors */
  --color-border-primary: #e2e8f0;
  --color-border-secondary: #cbd5e1;
  --color-border-hover: #cbd5e1;
  
  /* Status Colors */
  --color-success: #10b981;
  --color-success-hover: #059669;
  --color-warning: #f59e0b;
  --color-warning-hover: #d97706;
  --color-error: #ef4444;
  --color-error-hover: #dc2626;
  
  /* ===== SHADOW SYSTEM ===== */
  --shadow-xs: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  --shadow-sm: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
  --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
  --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
  --shadow-xl: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
  --shadow-2xl: 0 25px 50px -12px rgb(0 0 0 / 0.25);
  
  /* ===== BORDER RADIUS SYSTEM ===== */
  --radius-xs: 0.125rem;
  --radius-sm: 0.25rem;
  --radius-md: 0.375rem;
  --radius-lg: 0.5rem;
  --radius-xl: 0.75rem;
  --radius-2xl: 1rem;
  --radius-3xl: 1.5rem;
  --radius-full: 9999px;
  
  /* ===== SPACING SYSTEM ===== */
  --space-xs: 0.25rem;
  --space-sm: 0.5rem;
  --space-md: 0.75rem;
  --space-lg: 1rem;
  --space-xl: 1.5rem;
  --space-2xl: 2rem;
  --space-3xl: 3rem;
  --space-4xl: 4rem;
  
  /* ===== TYPOGRAPHY SYSTEM ===== */
  --font-family-primary: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-family-secondary: 'DM Sans', 'Futura', sans-serif;
  --font-family-mono: 'JetBrains Mono', 'Fira Code', monospace;
  
  --font-size-xs: 0.75rem;
  --font-size-sm: 0.875rem;
  --font-size-base: 1rem;
  --font-size-lg: 1.125rem;
  --font-size-xl: 1.25rem;
  --font-size-2xl: 1.5rem;
  --font-size-3xl: 1.875rem;
  --font-size-4xl: 2.25rem;
  
  --font-weight-normal: 400;
  --font-weight-medium: 500;
  --font-weight-semibold: 600;
  --font-weight-bold: 700;
  
  --line-height-tight: 1.25;
  --line-height-normal: 1.5;
  --line-height-relaxed: 1.75;
  
  /* ===== TRANSITION SYSTEM ===== */
  --transition-fast: 0.15s ease;
  --transition-normal: 0.3s ease;
  --transition-slow: 0.5s ease;
  --transition-bounce: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  
  /* ===== Z-INDEX SYSTEM ===== */
  --z-dropdown: 1000;
  --z-sticky: 1020;
  --z-fixed: 1030;
  --z-modal-backdrop: 1040;
  --z-modal: 1050;
  --z-popover: 1060;
  --z-tooltip: 1070;
  --z-toast: 1080;
}

/* ===== DARK THEME OVERRIDES ===== */
[data-theme="dark"] {
  /* Primary Colors */
  --color-primary: #60a5fa;
  --color-primary-hover: #3b82f6;
  --color-primary-active: #2563eb;
  
  /* Secondary Colors */
  --color-secondary: #94a3b8;
  --color-secondary-hover: #cbd5e1;
  
  /* Background Colors */
  --color-bg-primary: #0f172a;
  --color-bg-secondary: #1e293b;
  --color-bg-tertiary: #334155;
  --color-bg-hover: #334155;
  
  /* Text Colors */
  --color-text-primary: #f1f5f9;
  --color-text-secondary: #cbd5e1;
  --color-text-muted: #94a3b8;
  --color-text-inverse: #1e293b;
  
  /* Border Colors */
  --color-border-primary: #334155;
  --color-border-secondary: #475569;
  --color-border-hover: #475569;
  
  /* Status Colors */
  --color-success: #34d399;
  --color-success-hover: #10b981;
  --color-warning: #fbbf24;
  --color-warning-hover: #f59e0b;
  --color-error: #f87171;
  --color-error-hover: #ef4444;
  
  /* Enhanced shadows for dark theme */
  --shadow-xs: 0 1px 2px 0 rgb(0 0 0 / 0.3);
  --shadow-sm: 0 1px 3px 0 rgb(0 0 0 / 0.4), 0 1px 2px -1px rgb(0 0 0 / 0.4);
  --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.4), 0 2px 4px -2px rgb(0 0 0 / 0.4);
  --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.5), 0 4px 6px -4px rgb(0 0 0 / 0.5);
  --shadow-xl: 0 20px 25px -5px rgb(0 0 0 / 0.5), 0 8px 10px -6px rgb(0 0 0 / 0.5);
  --shadow-2xl: 0 25px 50px -12px rgb(0 0 0 / 0.6);
}

/* ===== BASE STYLES ===== */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-family-primary);
  background: var(--color-bg-primary);
  color: var(--color-text-primary);
  line-height: var(--line-height-normal);
  transition: background-color var(--transition-normal), color var(--transition-normal);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ===== BUTTON SYSTEM ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-sm);
  padding: var(--space-md) var(--space-xl);
  border: none;
  border-radius: var(--radius-md);
  font-family: inherit;
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-semibold);
  line-height: var(--line-height-tight);
  text-decoration: none;
  cursor: pointer;
  transition: all var(--transition-normal);
  position: relative;
  user-select: none;
  white-space: nowrap;
}

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

/* Button Variants */
.btn-primary {
  background: var(--color-primary);
  color: var(--color-text-inverse);
}

.btn-primary:hover {
  background: var(--color-primary-hover);
  box-shadow: var(--shadow-md);
}

.btn-secondary {
  background: var(--color-bg-secondary);
  color: var(--color-text-secondary);
  border: 2px solid var(--color-border-primary);
}

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

/* Button Sizes */
.btn-sm {
  padding: var(--space-sm) var(--space-md);
  font-size: var(--font-size-xs);
}

/* ===== FORM SYSTEM ===== */
.form-group {
  margin-bottom: var(--space-lg);
  position: relative;
}

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

.form-input {
  width: 100%;
  padding: var(--space-md) var(--space-lg);
  border: 2px solid var(--color-border-primary);
  border-radius: var(--radius-md);
  font-family: inherit;
  font-size: var(--font-size-base);
  color: var(--color-text-primary);
  background: var(--color-bg-secondary);
  transition: all var(--transition-normal);
  appearance: none;
}

.form-input:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
  background: var(--color-bg-hover);
}

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

/* Form Validation States */
.form-input.error {
  border-color: var(--color-error) !important;
  box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.1) !important;
}

/* Form Layouts */
.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-lg);
  margin-bottom: var(--space-lg);
}

.form-row .form-group {
  margin-bottom: 0;
}

/* ===== CARD SYSTEM ===== */
.card {
  background: var(--color-bg-secondary);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--color-border-primary);
  transition: all var(--transition-normal);
  position: relative;
  overflow: hidden;
}

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

/* ===== MODAL SYSTEM ===== */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: var(--z-modal);
  backdrop-filter: blur(4px);
}

.modal.show {
  display: flex;
}

.modal-content {
  background: var(--color-bg-secondary);
  border-radius: var(--radius-xl);
  padding: var(--space-2xl);
  max-width: 500px;
  width: 90%;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow-2xl);
  border: 1px solid var(--color-border-primary);
  animation: modalSlideIn var(--transition-normal) ease-out;
}

@keyframes modalSlideIn {
  from {
    opacity: 0;
    transform: translateY(-20px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

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

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

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

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

/* ===== LOADING SYSTEM ===== */
.loading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.9);
  display: none;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-xl);
  backdrop-filter: blur(4px);
  z-index: var(--z-modal);
}

[data-theme="dark"] .loading-overlay {
  background: rgba(15, 23, 42, 0.9);
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid var(--color-border-primary);
  border-top: 4px solid var(--color-primary);
  border-radius: var(--radius-full);
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading-text {
  margin-top: var(--space-lg);
  color: var(--color-text-secondary);
  font-weight: var(--font-weight-medium);
}

/* ===== THEME TOGGLE SYSTEM ===== */
.theme-toggle {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: var(--radius-full);
  background: var(--color-bg-secondary);
  color: var(--color-text-primary);
  cursor: pointer;
  box-shadow: var(--shadow-md);
  transition: all var(--transition-bounce);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--font-size-lg);
  border: 2px solid var(--color-border-primary);
}

.theme-toggle:hover {
  transform: scale(1.05);
  box-shadow: var(--shadow-lg);
  background: var(--color-primary);
  color: var(--color-text-inverse);
  border-color: var(--color-primary);
}

.theme-toggle:active {
  transform: scale(0.95);
}

.theme-icon {
  transition: all var(--transition-bounce);
  display: block;
}

.theme-icon.sun {
  transform: rotate(0deg);
}

.theme-icon.moon {
  transform: rotate(180deg);
}

[data-theme="dark"] .theme-icon.sun {
  opacity: 0;
  transform: rotate(180deg) scale(0.8);
}

[data-theme="dark"] .theme-icon.moon {
  opacity: 1;
  transform: rotate(0deg) scale(1);
}

[data-theme="light"] .theme-icon.sun {
  opacity: 1;
  transform: rotate(0deg) scale(1);
}

[data-theme="light"] .theme-icon.moon {
  opacity: 0;
  transform: rotate(-180deg) scale(0.8);
}

/* ===== ANIMATIONS ===== */
.fade-in {
  animation: fadeIn var(--transition-normal) ease-in-out;
}

@keyframes fadeIn {
  from { 
    opacity: 0; 
    transform: translateY(20px); 
  }
  to { 
    opacity: 1; 
    transform: translateY(0); 
  }
}

/* ===== RESPONSIVE DESIGN ===== */
@media (max-width: 1024px) {
  .card-body {
    padding: var(--space-lg);
  }
}

@media (max-width: 768px) {
  .modal-content {
    width: 95%;
    padding: var(--space-lg);
  }

  .btn {
    width: 100%;
    text-align: center;
  }
}

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

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

  .modal-content {
    padding: var(--space-lg);
    width: 95%;
  }

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

/* ===== ACCESSIBILITY ===== */
@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* Focus styles for keyboard navigation */
.btn:focus-visible,
.form-input:focus-visible,
.theme-toggle:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

/* High contrast mode support */
@media (prefers-contrast: high) {
  :root {
    --color-border-primary: #000000;
    --color-text-primary: #000000;
    --color-bg-secondary: #ffffff;
  }
  
  [data-theme="dark"] {
    --color-border-primary: #ffffff;
    --color-text-primary: #ffffff;
    --color-bg-secondary: #000000;
  }
}

/* =============================================
   iPAD TOUCH BASE — (hover:none) & (pointer:coarse)
   Additive only — desktop styles unchanged
   ============================================= */
@media (hover: none) and (pointer: coarse) and (min-width: 768px) {
  body {
    overscroll-behavior: none;
  }

  /* Prevent iOS from treating canvas touches as page scroll.
     touch-action does NOT inherit — must be set on the actual event targets. */
  #canvas-wrapper,
  #canvas-wrapper .upper-canvas,
  #canvas-wrapper .lower-canvas,
  #fabricCanvas {
    touch-action: none;
  }
}
