/* Global styles using native dark theme variables */
/* Theme variables are defined in native-dark-theme.css */

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden; /* Prevent iOS bounce scrolling */
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Segoe UI', 
               system-ui, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  
  /* Apply safe areas for iPhone X+ */
  padding-top: var(--safe-area-top);
  padding-bottom: var(--safe-area-bottom);
  padding-left: var(--safe-area-left);
  padding-right: var(--safe-area-right);
  
  /* Prevent iOS bounce scrolling */
  position: fixed;
  width: 100%;
  height: 100%;
  height: 100dvh; /* Dynamic viewport height for mobile */
  
  /* Prevent text selection on long press */
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  
  /* Prevent zoom on double tap */
  touch-action: pan-x pan-y;
  
  font-size: var(--font-size-base);
}

/* Allow text selection in content areas */
.app-container,
.content-wrapper,
.customer-card,
.card-body {
  -webkit-user-select: text;
  user-select: text;
}

/* Scrollable content container */
.app-container {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  overscroll-behavior-y: contain;
  width: 100%;
  height: 100%;
}

.container {
  max-width: 100%;
  margin: 0 auto;
  background: var(--bg-secondary);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-lg);
  overflow: hidden;
  height: 100%;
  display: flex;
  flex-direction: column;
  flex: 1;
}

/* Mobile-First Header */
.header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-primary);
  flex-shrink: 0;
  
  /* Account for notch on iPhone */
  padding-top: calc(var(--safe-area-top) + 12px);
  padding-bottom: 12px;
  padding-left: var(--spacing-mobile);
  padding-right: var(--spacing-mobile);
}

.header-content {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.header h1 {
  font-size: var(--font-size-xl);
  margin-bottom: 4px;
  font-weight: 600;
  letter-spacing: -0.025em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
}

.header p {
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
  text-align: center;
}

.header-actions {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
  justify-content: center;
}

.header-actions::-webkit-scrollbar {
  display: none;
}

/* Tablet and up */
@media (min-width: 768px) {
  .header {
    padding-left: var(--spacing-tablet);
    padding-right: var(--spacing-tablet);
  }
  
  .header-content {
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
  }
  
  .header h1 {
    font-size: var(--font-size-2xl);
    text-align: left;
  }
  
  .header p {
    text-align: left;
  }
}

/* Desktop */
@media (min-width: 1024px) {
  .header {
    padding-left: var(--spacing-desktop);
    padding-right: var(--spacing-desktop);
  }
}

.footer {
  padding: var(--spacing-sm) var(--spacing-lg);
  background: var(--bg-secondary);
  text-align: center;
  color: var(--text-tertiary);
  border-top: 1px solid var(--border-primary);
  font-size: 0.7em;
  flex-shrink: 0;
}

.content-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: var(--spacing-sm) var(--spacing-md);
  gap: var(--spacing-sm);
}

@media print {
  body {
    background: white;
    padding: 0;
  }
  
  .container {
    box-shadow: none;
  }
}

/* Mobile optimizations for iPhone 14 Pro Max and newer */
@media (max-width: 430px) {
  body {
    padding: 4px;
    -webkit-text-size-adjust: 100%;
    -webkit-font-smoothing: antialiased;
  }

  .container {
    border-radius: var(--radius-md);
    margin: 0;
    max-width: 100%;
    height: calc(100vh - 8px);
  }

  .header {
    padding: var(--spacing-sm) var(--spacing-md);
  }

  .header h1 {
    font-size: 0.9em;
    line-height: 1.2;
  }

  .header p {
    font-size: 0.65em;
  }

  .footer {
    padding: 6px 8px;
    font-size: 0.7em;
  }

  .content-wrapper {
    padding: var(--spacing-xs) var(--spacing-sm);
    gap: var(--spacing-xs);
  }
}

@media (max-width: 768px) and (min-width: 431px) {
  body {
    padding: 15px 10px;
  }

  .header h1 {
    font-size: 1.8em;
  }

  .header {
    padding: var(--spacing-lg) var(--spacing-xl);
  }
}

/* iPhone 14 Pro Max specific optimizations */
@media screen and (max-width: 428px) and (-webkit-min-device-pixel-ratio: 3) {
  body {
    padding: 4px;
  }

  .container {
    border-radius: var(--radius-md);
    height: calc(100vh - 8px);
  }

  .header {
    padding: var(--spacing-sm) var(--spacing-md);
  }

  .header h1 {
    font-size: 0.85em;
  }
}

/* Touch-Optimized Buttons */
.btn {
  /* Minimum touch target size for iOS */
  min-height: var(--touch-target-min);
  min-width: var(--touch-target-min);
  padding: 12px 20px;
  font-size: 16px; /* Prevents iOS zoom on focus */
  
  /* Better touch feedback */
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
  
  /* Prevent text selection on long press */
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  
  /* GPU acceleration */
  transform: translateZ(0);
  will-change: transform;
}

.btn:active {
  transform: translateZ(0) scale(0.98);
}

/* Mobile button group */
@media (max-width: 767px) {
  .btn-group {
    display: flex;
    flex-direction: column;
    width: 100%;
    gap: 8px;
  }
  
  .btn-group .btn {
    width: 100%;
    justify-content: center;
  }
}

/* Responsive Forms and Inputs */
.input,
.select,
.textarea {
  width: 100%;
  min-height: var(--touch-target-min);
  padding: 12px 16px;
  font-size: 16px; /* Prevents zoom on iOS */
  border-radius: 8px;
  border: 1px solid var(--border-primary);
  background: var(--bg-primary);
  color: var(--text-primary);
  
  /* iOS specific */
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
}

/* Fix iOS select arrow */
.select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12'%3E%3Cpath fill='%23999' d='M6 9L2 5h8z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 16px center;
  padding-right: 40px;
}

/* Number inputs - prevent spinners on mobile */
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

input[type="number"] {
  -moz-appearance: textfield;
}

/* Prevent text overflow on mobile */
* {
  word-wrap: break-word;
  overflow-wrap: break-word;
  hyphens: auto;
}

/* Truncate long text */
.truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.truncate-2 {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* Responsive Typography */
h1 { font-size: var(--font-size-3xl); }
h2 { font-size: var(--font-size-2xl); }
h3 { font-size: var(--font-size-xl); }
h4 { font-size: var(--font-size-lg); }

/* Touch-friendly optimizations */
@media (hover: none) and (pointer: coarse) {
  .btn {
    min-height: var(--touch-target-comfortable);
    padding: 14px 24px;
    -webkit-tap-highlight-color: rgba(59, 130, 246, 0.3);
  }

  input[type="checkbox"] {
    width: 24px;
    height: 24px;
    min-width: 24px;
    min-height: 24px;
    -webkit-tap-highlight-color: rgba(59, 130, 246, 0.3);
  }
  
  input[type="radio"] {
    width: 24px;
    height: 24px;
    min-width: 24px;
    min-height: 24px;
  }
}

/* Performance optimizations */
.accelerated {
  transform: translateZ(0);
  will-change: transform;
  backface-visibility: hidden;
}

.contain {
  contain: layout style paint;
}

/* Reduce motion for accessibility */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* Responsive Images */
img {
  max-width: 100%;
  height: auto;
  display: block;
}

img[loading="lazy"] {
  background: var(--bg-elevated);
  min-height: 100px;
}

/* Responsive video */
.video-container {
  position: relative;
  padding-bottom: 56.25%; /* 16:9 */
  height: 0;
  overflow: hidden;
}

.video-container iframe,
.video-container video {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

/* Dark mode images */
@media (prefers-color-scheme: dark) {
  img {
    opacity: 0.9;
  }
  
  img:hover {
    opacity: 1;
  }
}

/* Search input with clear button */
.search-container {
  position: relative;
}

.search-input {
  padding-right: 40px;
}

.search-clear {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--bg-hover);
  border: none;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  min-width: 44px;
  min-height: 44px;
}

/* Mobile Modals and Overlays */
@media (max-width: 767px) {
  .modal {
    position: fixed;
    inset: 0;
    margin: 0;
    max-width: 100%;
    max-height: 100%;
    width: 100%;
    height: 100%;
    border-radius: 0;
    animation: slideUp 0.3s ease;
  }
  
  @keyframes slideUp {
    from {
      transform: translateY(100%);
    }
    to {
      transform: translateY(0);
    }
  }
  
  .modal-header {
    padding-top: calc(var(--safe-area-top) + 16px);
    position: sticky;
    top: 0;
    background: var(--bg-secondary);
    z-index: 10;
  }
  
  .modal-body {
    padding-bottom: calc(var(--safe-area-bottom) + 20px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    max-height: calc(100vh - 120px);
    max-height: calc(100dvh - 120px);
  }
  
  .modal-footer {
    position: sticky;
    bottom: 0;
    background: var(--bg-secondary);
    padding-bottom: calc(var(--safe-area-bottom) + 16px);
    box-shadow: 0 -2px 10px rgba(0,0,0,0.2);
  }
  
  /* Sheet modal (partial screen) */
  .sheet-modal {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    max-height: 90vh;
    max-height: 90dvh;
    border-radius: 16px 16px 0 0;
    animation: slideUp 0.3s ease;
  }
  
  .sheet-handle {
    width: 40px;
    height: 4px;
    background: var(--border-secondary);
    border-radius: 2px;
    margin: 8px auto;
  }
}

/* Bottom Navigation for Mobile */
@media (max-width: 767px) {
  .bottom-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: calc(56px + var(--safe-area-bottom));
    background: var(--bg-secondary);
    border-top: 1px solid var(--border-primary);
    display: flex;
    justify-content: space-around;
    align-items: center;
    padding-bottom: var(--safe-area-bottom);
    z-index: 100;
  }
  
  .bottom-nav-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-secondary);
    text-decoration: none;
    transition: all var(--transition-fast);
    min-height: 44px;
  }
  
  .bottom-nav-item.active {
    color: var(--accent-primary);
  }
  
  .bottom-nav-icon {
    font-size: 20px;
    margin-bottom: 4px;
  }
  
  .bottom-nav-label {
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
  }
  
  /* Adjust content for bottom nav */
  .app-container.has-bottom-nav {
    padding-bottom: calc(56px + var(--safe-area-bottom));
  }
}

/* Floating Action Button for mobile */
@media (max-width: 767px) {
  .fab {
    position: fixed;
    bottom: calc(var(--safe-area-bottom) + 20px);
    right: 20px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    box-shadow: 0 4px 12px rgba(0,0,0,0.4);
    z-index: 1000;
    background: var(--accent-primary);
    color: white;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 56px;
    min-height: 56px;
  }
}
