/* ============================================
   MOBILE & TABLET RESPONSIVE ENHANCEMENTS
   ============================================ */

/* Touch-friendly base improvements */
* {
  -webkit-tap-highlight-color: rgba(66, 153, 225, 0.2);
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

input, textarea, select {
  -webkit-user-select: text;
  user-select: text;
}

/* Smooth scrolling for all devices */
html {
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

/* Prevent zoom on input focus (iOS) */
@media screen and (max-width: 768px) {
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="number"],
  select,
  textarea {
    font-size: 16px !important; /* Prevents zoom on iOS */
  }

  /* Make viewport responsive */
  body {
    overflow-x: hidden;
  }
  
  .container {
    width: 100%;
    overflow-x: hidden;
  }
}

/* ============================================
   MOBILE FIRST (320px - 480px)
   ============================================ */
@media (max-width: 480px) {
  body {
    font-size: 14px;
    padding: 0;
    margin: 0;
  }

  .container {
    max-width: 100%;
    padding: 10px;
    margin: 0;
  }

  /* Header */
  header {
    flex-direction: column;
    align-items: stretch;
    padding: 15px;
    gap: 12px;
    position: sticky;
    top: 0;
    z-index: 100;
    background: white;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  }

  header h1 {
    font-size: 1.4rem;
    margin: 0;
    line-height: 1.3;
  }

  .user {
    font-size: 13px;
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
  }

  .user a, .user button {
    padding: 8px 16px;
    font-size: 14px;
    white-space: nowrap;
  }

  /* Sections */
  section {
    margin-bottom: 15px;
    padding: 15px;
    border-radius: 8px;
  }

  section h2 {
    font-size: 1.1rem;
    margin-bottom: 12px;
  }

  /* Forms */
  form {
    display: flex;
    flex-direction: column;
    gap: 12px;
  }

  form label {
    font-size: 13px;
    margin-bottom: 5px;
  }

  input, select, textarea {
    width: 100% !important;
    padding: 12px;
    font-size: 16px;
    min-height: 44px; /* Apple's recommended touch target */
    border-radius: 6px;
  }

  button {
    width: 100%;
    padding: 14px;
    font-size: 16px;
    min-height: 48px;
    border-radius: 8px;
    margin: 5px 0;
  }

  /* Tables - Make scrollable horizontally */
  .table-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0 -10px;
    padding: 0 10px;
  }

  table {
    width: 100%;
    min-width: 600px;
    font-size: 12px;
    border-collapse: collapse;
  }

  th, td {
    padding: 10px 8px;
    font-size: 12px;
    white-space: nowrap;
    text-align: left;
  }

  th {
    background-color: #f7fafc;
    position: sticky;
    top: 0;
    z-index: 10;
  }

  /* Barcode images */
  .barcode-cell img {
    max-width: 100px;
    height: auto;
    display: block;
  }

  /* Action buttons in table */
  .actions-cell {
    display: flex;
    flex-wrap: wrap;
    gap: 5px;
    min-width: 150px;
  }

  .actions-cell button {
    flex: 1;
    min-width: 60px;
    padding: 8px 10px;
    font-size: 11px;
  }

  /* Stats grid */
  .stats-grid {
    grid-template-columns: 1fr !important;
    gap: 10px;
  }

  .stats-controls {
    flex-direction: column;
    gap: 10px;
  }

  .stats-controls select,
  .stats-controls input,
  .stats-controls button {
    width: 100%;
  }

  /* Charts */
  .charts-row {
    flex-direction: column;
  }

  .chart-container {
    width: 100% !important;
    height: 250px !important;
    margin-bottom: 15px;
  }

  /* Metrics grid */
  .metrics-grid {
    grid-template-columns: 1fr !important;
  }

  /* Form rows */
  .form-row {
    flex-direction: column;
  }

  .form-group {
    width: 100%;
  }

  /* User table actions */
  .actions-cell {
    display: flex;
    flex-direction: column;
    gap: 5px;
  }

  .btn-sm {
    width: 100%;
    margin: 2px 0;
  }
}

/* ============================================
   TABLET PORTRAIT (481px - 768px)
   ============================================ */
@media (min-width: 481px) and (max-width: 768px) {
  .container {
    max-width: 100%;
    padding: 15px;
  }

  header {
    flex-wrap: wrap;
    padding: 20px 15px;
  }

  header h1 {
    font-size: 1.5rem;
  }

  section {
    padding: 20px;
    margin-bottom: 20px;
  }

  /* Stats grid - 2 columns */
  .stats-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 15px;
  }

  /* Metrics - 2 columns */
  .metrics-grid {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  /* Forms - 2 columns where appropriate */
  .form-row {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 15px;
  }

  /* Tables */
  table {
    font-size: 13px;
  }

  th, td {
    padding: 10px 8px;
  }

  /* Charts side by side */
  .charts-row {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 15px;
  }

  .chart-container {
    height: 280px;
  }

  /* Touch-friendly buttons */
  button {
    min-height: 44px;
    padding: 12px 20px;
  }
}

/* ============================================
   TABLET LANDSCAPE / SMALL DESKTOP (769px - 1024px)
   ============================================ */
@media (min-width: 769px) and (max-width: 1024px) {
  .container {
    max-width: 95%;
    padding: 20px;
  }

  /* Stats grid - 3 columns */
  .stats-grid {
    grid-template-columns: repeat(3, 1fr) !important;
  }

  /* Metrics - 3 columns */
  .metrics-grid {
    grid-template-columns: repeat(3, 1fr) !important;
  }

  /* Charts - 2 columns */
  .charts-row {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
  }

  /* Forms */
  .form-row {
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
  }
}

/* ============================================
   IPAD SPECIFIC (iPad Pro, Air, Mini)
   ============================================ */
@media only screen 
  and (min-device-width: 768px) 
  and (max-device-width: 1024px) 
  and (-webkit-min-device-pixel-ratio: 2) {
  
  /* iPad optimizations */
  body {
    font-size: 15px;
  }

  .container {
    max-width: 95%;
    padding: 25px;
  }

  header h1 {
    font-size: 2rem;
  }

  section {
    padding: 25px;
  }

  /* Larger touch targets for iPad */
  button {
    min-height: 48px;
    font-size: 16px;
  }

  input, select {
    min-height: 48px;
    font-size: 16px;
  }

  /* Stats - 4 columns on iPad landscape */
  @media (orientation: landscape) {
    .stats-grid {
      grid-template-columns: repeat(4, 1fr) !important;
    }

    .metrics-grid {
      grid-template-columns: repeat(4, 1fr) !important;
    }

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

  /* Stats - 2 columns on iPad portrait */
  @media (orientation: portrait) {
    .stats-grid {
      grid-template-columns: repeat(2, 1fr) !important;
    }

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

    .charts-row {
      grid-template-columns: 1fr;
    }
  }
}

/* ============================================
   LANDSCAPE MODE OPTIMIZATIONS
   ============================================ */
@media (max-height: 500px) and (orientation: landscape) {
  header {
    padding: 10px 15px;
  }

  header h1 {
    font-size: 1.2rem;
  }

  section {
    padding: 15px;
    margin-bottom: 15px;
  }

  .stats-grid,
  .metrics-grid {
    grid-template-columns: repeat(4, 1fr) !important;
    gap: 10px;
  }
}

/* ============================================
   TOUCH IMPROVEMENTS
   ============================================ */

/* Larger touch targets */
a, button, input[type="submit"], input[type="button"], select {
  min-height: 44px;
  min-width: 44px;
}

/* Better active states for touch */
button:active,
a:active {
  transform: scale(0.98);
  opacity: 0.8;
}

/* Disable hover effects on touch devices */
@media (hover: none) {
  button:hover,
  a:hover {
    transform: none;
    box-shadow: none;
  }
}

/* ============================================
   SCANNER PAGE MOBILE OPTIMIZATIONS
   ============================================ */
@media (max-width: 768px) {
  .scanner-container {
    padding: 10px;
    width: 100%;
    box-sizing: border-box;
  }

  .scanner-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
    padding: 15px;
    background: white;
    border-radius: 12px;
  }

  .scanner-header h1 {
    font-size: 1.4rem;
    margin: 0;
  }

  .scanner-section {
    padding: 15px;
    margin-bottom: 15px;
    background: white;
    border-radius: 12px;
  }

  #qr-reader {
    width: 100% !important;
    max-width: 100% !important;
    height: 300px !important;
    border-radius: 12px;
    overflow: hidden;
  }

  #qr-reader video {
    width: 100% !important;
    height: 100% !important;
    object-fit: cover !important;
  }

  .btn-scan,
  .btn-stop,
  .btn-submit,
  .btn-action {
    width: 100%;
    padding: 16px;
    font-size: 16px;
    margin: 8px 0;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 50px;
  }

  .input-group {
    flex-direction: column;
    width: 100%;
  }

  .input-group input {
    width: 100%;
    margin-bottom: 10px;
    padding: 14px;
    font-size: 16px;
    border-radius: 10px;
  }

  .action-buttons {
    display: flex;
    flex-direction: column;
    gap: 10px;
    width: 100%;
  }

  .result-card {
    padding: 15px;
    border-radius: 12px;
    background: #f8fafc;
    margin-bottom: 15px;
  }

  .info-row {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
    padding: 10px 0;
    border-bottom: 1px solid #e2e8f0;
  }

  .info-row:last-child {
    border-bottom: none;
  }

  .info-row strong {
    font-size: 13px;
    color: #718096;
    text-transform: uppercase;
    letter-spacing: 0.5px;
  }

  .info-row span {
    font-size: 15px;
    color: #2d3748;
    font-weight: 600;
  }

  /* Scanner camera controls */
  #html5-qrcode-button-camera-start,
  #html5-qrcode-button-camera-stop {
    width: 100% !important;
    padding: 14px !important;
    font-size: 16px !important;
    border-radius: 10px !important;
    margin: 10px 0 !important;
  }

  #html5-qrcode-select-camera {
    width: 100% !important;
    padding: 12px !important;
    font-size: 16px !important;
    border-radius: 10px !important;
    margin-bottom: 10px !important;
  }
}

/* ============================================
   LOGIN PAGE MOBILE
   ============================================ */
@media (max-width: 480px) {
  .login-background {
    padding: 15px;
    align-items: center;
    display: flex;
    min-height: 100vh;
  }

  .login-container {
    padding: 0;
    max-width: 100%;
    width: 100%;
  }

  .login-card {
    padding: 30px 20px;
    margin: 0;
    border-radius: 16px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  }

  .login-header {
    margin-bottom: 30px;
    text-align: center;
  }

  .login-header h1 {
    font-size: 1.6rem;
    margin-bottom: 8px;
  }

  .login-header p {
    font-size: 14px;
  }

  .app-icon {
    width: 64px;
    height: 64px;
    margin: 0 auto 16px;
    display: block;
  }

  .login-form {
    width: 100%;
  }

  .input-group {
    margin-bottom: 16px;
  }

  .input-group label {
    font-size: 14px;
    margin-bottom: 8px;
    display: block;
    font-weight: 600;
    text-align: left;
  }

  .input-wrapper {
    position: relative;
    width: 100%;
    display: flex;
    align-items: center;
  }

  .input-wrapper input {
    width: 100%;
    font-size: 16px;
    padding: 14px 50px 14px 48px !important;
    border: 2px solid #e2e8f0;
    border-radius: 10px;
    box-sizing: border-box;
  }

  .input-wrapper .input-icon {
    position: absolute;
    left: 14px;
    top: 50%;
    transform: translateY(-50%);
    color: #718096;
    font-size: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    pointer-events: none;
    z-index: 2;
  }

  .password-toggle {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    min-height: 44px;
    min-width: 44px;
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 3;
  }

  .login-actions {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
    margin-top: 20px;
  }

  .remember-me {
    order: 1;
    justify-content: flex-start;
    display: flex;
    align-items: center;
    gap: 8px;
  }

  .login-button {
    order: 2;
    width: 100%;
    font-size: 16px;
    padding: 16px;
    font-weight: 600;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
  }

  .scanner-link {
    padding: 14px;
    font-size: 15px;
    margin-top: 15px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    text-align: center;
  }

  .scanner-link .scanner-subtitle {
    display: block;
    width: 100%;
  }

  .error-message {
    padding: 14px;
    font-size: 14px;
    border-radius: 10px;
    margin-bottom: 20px;
  }
}

/* Tablet login page */
@media (min-width: 481px) and (max-width: 768px) {
  .login-container {
    max-width: 500px;
    padding: 20px;
  }

  .login-card {
    padding: 40px 35px;
  }

  .login-header h1 {
    font-size: 1.8rem;
  }

  .input-wrapper input {
    padding: 15px 15px 15px 50px;
    font-size: 16px;
  }

  .login-button {
    padding: 16px;
    font-size: 17px;
  }
}

/* ============================================
   PRINT STYLES (Optional)
   ============================================ */
@media print {
  .user, button, .btn-download, .stats-controls {
    display: none !important;
  }

  table {
    font-size: 10px;
  }

  .container {
    max-width: 100%;
  }
}

/* ============================================
   ACCESSIBILITY IMPROVEMENTS
   ============================================ */

/* Focus visible for keyboard navigation */
*:focus-visible {
  outline: 3px solid #106EBE;
  outline-offset: 2px;
}

/* Skip to main content link */
.skip-to-main {
  position: absolute;
  top: -40px;
  left: 0;
  background: #106EBE;
  color: white;
  padding: 8px;
  z-index: 100;
}

.skip-to-main:focus {
  top: 0;
}

/* Reduced motion for users who prefer it */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* Dark mode support (if needed) */
@media (prefers-color-scheme: dark) {
  /* Add dark mode styles here if desired */
}

/* ============================================
   MOBILE NAVIGATION MENU WITH HAMBURGER
   ============================================ */
@media (max-width: 768px) {
  /* Hamburger menu button */
  .mobile-menu-toggle {
    display: flex !important;
    flex-direction: column;
    justify-content: space-around;
    width: 36px;
    height: 32px;
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 4px;
    z-index: 1001;
    position: relative;
    flex-shrink: 0;
  }

  .mobile-menu-toggle span {
    width: 100%;
    height: 3px;
    background: #106EBE;
    border-radius: 3px;
    transition: all 0.3s ease;
    transform-origin: center;
    display: block;
  }

  .mobile-menu-toggle.active span:nth-child(1) {
    transform: rotate(45deg) translateY(9px);
  }

  .mobile-menu-toggle.active span:nth-child(2) {
    opacity: 0;
    transform: translateX(-20px);
  }

  .mobile-menu-toggle.active span:nth-child(3) {
    transform: rotate(-45deg) translateY(-9px);
  }

  /* Mobile navigation overlay */
  .mobile-nav-overlay {
    position: fixed;
    top: 0;
    left: -100%;
    width: 80%;
    max-width: 320px;
    height: 100vh;
    background: white;
    box-shadow: 2px 0 12px rgba(0,0,0,0.15);
    z-index: 999;
    transition: left 0.3s ease;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .mobile-nav-overlay.active {
    left: 0;
  }

  /* Backdrop */
  .mobile-nav-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100vh;
    background: rgba(0,0,0,0.5);
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s ease;
    z-index: 998;
  }

  .mobile-nav-backdrop.active {
    opacity: 1;
    visibility: visible;
  }

  /* Navigation links in sidebar */
  .mobile-nav-content {
    padding: 80px 20px 20px;
  }

  .mobile-nav-user {
    padding: 20px;
    background: #f7fafc;
    border-bottom: 2px solid #e2e8f0;
    margin-bottom: 20px;
    border-radius: 0;
  }

  .mobile-nav-user strong {
    display: block;
    font-size: 16px;
    color: #2d3748;
    margin-bottom: 4px;
  }

  .mobile-nav-user small {
    font-size: 13px;
    color: #718096;
  }

  .navigation-links {
    display: flex;
    flex-direction: column;
    gap: 8px;
    width: 100%;
  }

  .navigation-links a {
    display: block;
    padding: 16px 20px;
    background: transparent;
    border-radius: 8px;
    text-decoration: none;
    color: #2d3748;
    font-weight: 500;
    font-size: 16px;
    transition: all 0.2s;
    border-left: 4px solid transparent;
  }

  .navigation-links a:active,
  .navigation-links a:hover {
    background: #f0f9ff;
    border-left-color: #106EBE;
    transform: translateX(4px);
  }

  /* Header adjustments for hamburger */
  header {
    display: flex !important;
    flex-direction: row !important;
    align-items: center !important;
    justify-content: flex-start !important;
    padding: 15px !important;
    gap: 15px !important;
    position: sticky;
    top: 0;
    z-index: 100;
    background: white;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  }

  header h1 {
    flex: 1;
    margin: 0 !important;
    font-size: 1.3rem !important;
    line-height: 1.2 !important;
    text-align: left;
  }

  header .user {
    display: none !important; /* Hide in header, show in sidebar */
  }

  /* Show hamburger only on mobile */
  .mobile-menu-toggle {
    display: flex !important;
    order: -1; /* Put hamburger first (left side) */
  }

  .mobile-nav-overlay,
  .mobile-nav-backdrop {
    display: block;
  }

  /* Dashboard cards on mobile */
  .dashboard-nav {
    display: grid;
    grid-template-columns: 1fr;
    gap: 12px;
    margin-bottom: 20px;
  }

  .dashboard-nav a {
    display: flex;
    align-items: center;
    padding: 16px;
    background: white;
    border: 2px solid #e2e8f0;
    border-radius: 12px;
    text-decoration: none;
    color: #2d3748;
    font-weight: 600;
    transition: all 0.2s;
    gap: 12px;
  }

  .dashboard-nav a:active {
    transform: scale(0.98);
    border-color: #106EBE;
    background: #f0f9ff;
  }

  /* Card grid improvements */
  .card-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 15px;
  }

  .card {
    padding: 16px;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  }

  /* Protocol filter section */
  .filter-section {
    background: #f7fafc;
    padding: 15px;
    border-radius: 12px;
    margin-bottom: 15px;
  }

  .filter-section select,
  .filter-section input,
  .filter-section button {
    width: 100%;
    margin-bottom: 10px;
  }

  /* Protocol code display */
  .protocol-code {
    font-size: 16px;
    font-weight: 700;
    color: #106EBE;
    word-break: break-all;
  }

  /* Sample info cards */
  .sample-info {
    background: #f7fafc;
    padding: 12px;
    border-radius: 8px;
    margin: 10px 0;
  }

  .sample-info p {
    margin: 8px 0;
    font-size: 14px;
    line-height: 1.6;
  }

  .sample-info strong {
    display: block;
    color: #4a5568;
    font-size: 12px;
    margin-bottom: 4px;
  }

  /* Mobile icon improvements - prevent emoji from being too large */
  .metric-icon,
  .stock-icon {
    font-size: 24px !important;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
  }

  .navigation-links a {
    font-size: 15px !important;
    line-height: 1.4;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* Prevent icon overflow in nav */
  .navigation-links a::before {
    content: '';
    display: inline-block;
    width: 24px;
    text-align: center;
    flex-shrink: 0;
  }

  /* Show hamburger only on mobile */
  .mobile-menu-toggle {
    display: flex !important;
  }

  .mobile-nav-overlay,
  .mobile-nav-backdrop {
    display: block;
  }

  /* Fix emoji alignment everywhere */
  body {
    font-feature-settings: "liga" 0;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
  }

  /* Constrain all emoji/icon sizes */
  section h2::before,
  .metric-card .metric-icon,
  .stat-icon,
  button::before {
    font-size: inherit;
    line-height: 1;
    vertical-align: middle;
  }
}

/* Hide hamburger menu on desktop */
@media (min-width: 769px) {
  .mobile-menu-toggle,
  .mobile-nav-overlay,
  .mobile-nav-backdrop {
    display: none !important;
  }
}

/* ============================================
   PERFORMANCE OPTIMIZATIONS
   ============================================ */

/* GPU acceleration for animations */
button,
.card,
.scanner-section {
  will-change: transform;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

/* Optimize images for mobile */
img {
  max-width: 100%;
  height: auto;
  image-rendering: -webkit-optimize-contrast;
}
