/**
 * Responsive Design Enhancements
 * Mobile-first approach with progressive enhancement
 */

/* === BASE STYLES === */
* {
  -webkit-tap-highlight-color: transparent;
}

html {
  scroll-behavior: smooth;
  overflow-x: hidden;
  width: 100%;
}

body {
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  width: 100%;
  max-width: 100vw;
  position: relative;
}

#root {
  width: 100%;
  max-width: 100vw;
  overflow-x: hidden;
}

/* Better touch scrolling */
.scroll-smooth {
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
}

/* Prevent text selection on buttons and interactive elements */
button,
.no-select {
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

/* === MOBILE OPTIMIZATIONS === */
@media (max-width: 640px) {
  /* Reduce font sizes on small screens */
  body {
    font-size: 14px;
  }
  
  /* Container padding */
  .container {
    padding-left: 1rem;
    padding-right: 1rem;
  }
  
  /* Stack cards vertically */
  .loan-card,
  .borrower-card,
  .budget-card {
    width: 100% !important;
  }
  
  /* Full-width buttons on mobile */
  button.mobile-full {
    width: 100%;
  }
  
  /* Reduce padding in cards */
  .card-mobile-compact {
    padding: 0.75rem !important;
  }
  
  /* Compact form inputs */
  input[type="text"],
  input[type="number"],
  select,
  textarea {
    font-size: 16px !important; /* Prevents zoom on iOS */
    padding: 0.5rem !important;
  }
  
  /* Hide long text, show icons only */
  .mobile-icon-only span {
    display: none;
  }
  
  /* Scrollable tables */
  .table-responsive {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  
  table {
    min-width: 600px;
    font-size: 0.875rem;
  }
  
  /* Compact table cells */
  table td,
  table th {
    padding: 0.5rem !important;
  }
  
  /* Mobile modal adjustments */
  .modal-content {
    max-height: 90vh;
    overflow-y: auto;
  }
  
  /* Reduce chart height on mobile */
  canvas {
    max-height: 250px !important;
  }
  
  /* Stack flex items */
  .flex-mobile-stack {
    flex-direction: column !important;
  }
  
  /* Touch-friendly tap targets (min 44x44px) */
  button,
  a,
  input[type="checkbox"],
  input[type="radio"] {
    min-height: 44px;
    min-width: 44px;
  }
  
  /* Spacing adjustments */
  .gap-4 {
    gap: 0.75rem !important;
  }
  
  .gap-6 {
    gap: 1rem !important;
  }
  
  /* Typography adjustments */
  h1 {
    font-size: 1.5rem !important;
  }
  
  h2 {
    font-size: 1.25rem !important;
  }
  
  h3 {
    font-size: 1.1rem !important;
  }
  
  /* Bottom navigation for mobile */
  .mobile-bottom-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: white;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
    z-index: 100;
    padding: 0.5rem;
  }
  
  /* Ensure icons scale properly */
  svg {
    max-width: 24px;
    max-height: 24px;
  }
  
  /* Fix for select dropdowns */
  select {
    background-position: right 0.5rem center !important;
    padding-right: 2.5rem !important;
  }
  
  /* Compact list items */
  li {
    padding: 0.5rem !important;
  }
  
  /* Fix modal widths */
  [class*="max-w-"] {
    max-width: 95vw !important;
  }
  
  /* Reduce header sizes in modals */
  .modal h1,
  .modal h2,
  .modal h3 {
    font-size: 1.25rem !important;
  }
}

/* === TABLET OPTIMIZATIONS === */
@media (min-width: 641px) and (max-width: 1024px) {
  /* Two-column grid on tablets */
  .grid-responsive {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* === ANIMATIONS === */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateX(-20px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes pulse {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

/* Apply animations */
.animate-fade-in {
  animation: fadeIn 0.3s ease-out;
}

.animate-slide-in {
  animation: slideIn 0.3s ease-out;
}

.animate-pulse {
  animation: pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite;
}

.animate-spin {
  animation: spin 1s linear infinite;
}

/* === UTILITY CLASSES === */
.shadow-glow {
  box-shadow: 0 0 15px rgba(99, 102, 241, 0.3);
}

.shadow-hover:hover {
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
  transform: translateY(-2px);
  transition: all 0.3s ease;
}

.shadow-card {
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05), 0 1px 3px rgba(0, 0, 0, 0.1);
}

.shadow-card:hover {
  box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1), 0 4px 6px rgba(0, 0, 0, 0.05);
}

.gradient-border {
  border: 2px solid transparent;
  background-image: 
    linear-gradient(white, white),
    linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  background-origin: border-box;
  background-clip: padding-box, border-box;
}

.gradient-text {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

/* Glassmorphism effect */
.glass {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.3);
}

/* Smooth transitions for all interactive elements */
button,
a,
input,
select,
textarea {
  transition: all 0.2s ease;
}

/* Prevent horizontal overflow */
* {
  max-width: 100%;
  box-sizing: border-box;
}

img,
video,
canvas,
iframe {
  max-width: 100%;
  height: auto;
}

/* Table responsiveness */
@media (max-width: 640px) {
  table {
    font-size: 0.75rem !important;
  }
  
  table th,
  table td {
    padding: 0.5rem !important;
    white-space: nowrap;
  }
  
  .overflow-x-auto {
    -webkit-overflow-scrolling: touch;
  }
}

/* Card hover effects */
.card-hover {
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.card-hover:hover {
  transform: translateY(-4px);
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.15);
}

/* Mobile utility classes */
@media (max-width: 640px) {
  .mobile-text-sm {
    font-size: 0.875rem !important;
  }
  
  .mobile-text-xs {
    font-size: 0.75rem !important;
  }
  
  .mobile-p-2 {
    padding: 0.5rem !important;
  }
  
  .mobile-p-4 {
    padding: 1rem !important;
  }
  
  .mobile-mt-2 {
    margin-top: 0.5rem !important;
  }
  
  .mobile-mb-2 {
    margin-bottom: 0.5rem !important;
  }
  
  .mobile-flex-col {
    flex-direction: column !important;
  }
  
  .mobile-w-full {
    width: 100% !important;
  }
  
  .mobile-hidden {
    display: none !important;
  }
  
  .mobile-block {
    display: block !important;
  }
}

/* === BUTTON ENHANCEMENTS === */
button:focus {
  outline: 2px solid rgba(99, 102, 241, 0.5);
  outline-offset: 2px;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

button:not(:disabled):active {
  transform: scale(0.98);
}

/* === LOADING STATES === */
.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.loading-spinner {
  width: 50px;
  height: 50px;
  border: 4px solid rgba(255, 255, 255, 0.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

/* === TOAST NOTIFICATIONS === */
.toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  left: 20px;
  max-width: 400px;
  margin: 0 auto;
  background: white;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
  padding: 16px;
  animation: slideIn 0.3s ease-out;
  z-index: 1000;
}

@media (min-width: 640px) {
  .toast {
    left: auto;
    margin: 0;
  }
}

.toast.success {
  border-left: 4px solid #10b981;
  background: linear-gradient(to right, #d1fae5, white);
}

.toast.error {
  border-left: 4px solid #ef4444;
  background: linear-gradient(to right, #fee2e2, white);
}

.toast.info {
  border-left: 4px solid #3b82f6;
  background: linear-gradient(to right, #dbeafe, white);
}

.toast.warning {
  border-left: 4px solid #f59e0b;
  background: linear-gradient(to right, #fef3c7, white);
}

/* === ACCESSIBILITY === */
@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
  button,
  input,
  select,
  textarea {
    border-width: 2px;
  }
}

/* === PRINT STYLES === */
@media print {
  .no-print {
    display: none !important;
  }
  
  body {
    background: white;
  }
  
  .bg-gradient-to-br {
    background: white !important;
  }
  
  button {
    display: none;
  }
}

/* === SCROLLBAR STYLING === */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: #f1f5f9;
  border-radius: 4px;
}

::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: #94a3b8;
}

/* Firefox scrollbar */
* {
  scrollbar-width: thin;
  scrollbar-color: #cbd5e1 #f1f5f9;
}

/* === FOCUS VISIBLE === */
*:focus-visible {
  outline: 2px solid #6366f1;
  outline-offset: 2px;
}

/* === RESPONSIVE GRID === */
@media (max-width: 640px) {
  /* Prevent text overflow */
  * {
    word-wrap: break-word;
    overflow-wrap: break-word;
  }
  
  /* Adjust padding for cards */
  .p-8 {
    padding: 1rem !important;
  }
  
  .p-6 {
    padding: 0.75rem !important;
  }
  
  /* Adjust large text to prevent overflow */
  .text-5xl {
    font-size: 2rem !important;
    line-height: 1.2 !important;
  }
  
  .text-4xl {
    font-size: 1.75rem !important;
  }
  
  .text-3xl {
    font-size: 1.5rem !important;
  }
  
  .text-2xl {
    font-size: 1.25rem !important;
  }
  
  /* Adjust gap spacing */
  .gap-8 {
    gap: 1rem !important;
  }
  
  .gap-6 {
    gap: 0.75rem !important;
  }
}
