/* Mobile-specific enhancements */

/* Fix virtual keyboard issues on iOS */
@supports (-webkit-touch-callout: none) {
  .chat-input-container {
    position: relative !important;
    padding-bottom: env(safe-area-inset-bottom, 15px) !important;
  }
  
  #user-input {
    font-size: 16px !important; /* Prevents zoom on input focus */
  }
  
  /* Prevent scroll bounce */
  html, body {
    position: fixed;
    overflow: hidden;
    width: 100%;
    height: 100%;
  }
  
  .chat-container {
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    height: calc(100% - 120px);
    width: 100%;
  }
}

/* Improved file attachment display on small screens */
@media (max-width: 480px) {
  .file-attachment {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .attachment-icon {
    width: 100%;
    justify-content: flex-start;
    margin-bottom: 10px;
  }
  
  .attachment-info {
    width: 100%;
  }
  
  /* Better handle modals on small screens */
  .auth-modal-content {
    width: 95% !important;
    max-height: 80vh;
    overflow-y: auto;
  }
  
  /* Fix file preview sizes */
  #preview-image, #preview-pdf {
    max-height: 200px !important;
  }
  
  /* Enhanced footer mobile optimizations */
  .enhanced-footer {
    padding: 12px 8px !important;
    text-align: center !important;
    font-size: 11px !important;
    line-height: 1.5 !important;
  }
  
  .enhanced-footer p {
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
  }
  
  .portfolio-link {
    margin-top: 4px !important;
    padding: 3px 10px !important;
    border-radius: 20px !important;
    display: inline-block !important;
    width: auto !important;
  }
}

/* Fix for notched phones (iPhone X and newer) */
@supports (padding: max(0px)) {
  .app-header {
    padding-top: max(0.7rem, env(safe-area-inset-top));
  }
  
  .app-footer {
    padding-bottom: max(0.8rem, env(safe-area-inset-bottom));
  }
  
  .chat-input-container {
    padding-bottom: max(1.5rem, env(safe-area-inset-bottom));
  }
}

/**
 * Mobile-specific fixes and enhancements
 * Only loaded for mobile devices
 */

/* Mobile-only styles */
@media (max-width: 768px) {
  /* Increase touch target sizes */
  button, 
  .feature-button, 
  .user-button, 
  .theme-toggle {
    min-width: 42px;
    min-height: 42px;
  }
  
  /* Optimize scrolling */
  .chat-messages,
  .upload-preview,
  .auth-modal-content {
    -webkit-overflow-scrolling: touch;
  }
  
  /* Improve readability on small screens */
  .message-content {
    font-size: 1rem;
    line-height: 1.5;
  }
  
  /* Ensure code blocks don't overflow */
  .message-content pre {
    max-width: 100%;
    overflow-x: auto;
  }
  
  /* Better file attachment display */
  .file-attachment {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .attachment-icon {
    margin-bottom: 8px;
  }
  
  /* Fix modal positioning */
  .auth-modal {
    align-items: flex-end;
  }
  
  .auth-modal-content {
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
    max-height: 90vh;
  }
}

/* iPhone notch and bottom bar adjustments */
@supports (padding: max(0px)) {
  .app-header {
    padding-top: max(10px, env(safe-area-inset-top));
    padding-left: max(15px, env(safe-area-inset-left));
    padding-right: max(15px, env(safe-area-inset-right));
  }
  
  .chat-input-container {
    padding-bottom: max(15px, env(safe-area-inset-bottom));
    padding-left: max(15px, env(safe-area-inset-left));
    padding-right: max(15px, env(safe-area-inset-right));
  }
  
  .app-footer {
    padding-bottom: max(10px, env(safe-area-inset-bottom));
  }
}

/* Keyboard detection helper */
@media (max-width: 768px) {
  /* When keyboard is open, hide unnecessary elements */
  body.keyboard-open .app-header,
  body.keyboard-open .app-footer {
    display: none;
  }
  
  body.keyboard-open .chat-messages {
    max-height: 50vh;
  }
}

/* Fix for mobile input focusing */
input:focus,
textarea:focus {
  font-size: 16px !important;
}

/* Mobile-specific fixes for chat interface */

/* Only apply these styles on mobile devices */
@media (max-width: 768px) {
  /* Fix layout for iOS/Safari */
  html, body {
    height: 100%;
    overflow: hidden;
    position: fixed;
    width: 100%;
  }
  
  .app-container {
    height: 100%;
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }
  
  /* Fix chat container positioning */
  .chat-container {
    flex: 1;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    position: relative;
  }
  
  /* Chat messages - improve scrolling and touch */
  .chat-messages {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 10px;
    scroll-behavior: smooth;
    overscroll-behavior-y: contain;
    margin-bottom: 0;
  }
  
  /* Message bubbles improvement */
  .message-container {
    max-width: 100%;
    padding: 8px 10px;
    margin-bottom: 8px;
    border-radius: 12px;
  }
  
  .user .message-container {
    margin-left: 10px;
    margin-right: 5px;
  }
  
  .assistant .message-container {
    margin-left: 5px;
    margin-right: 10px;
  }
  
  /* Input area optimized for mobile */
  .chat-input-container {
    position: sticky;
    bottom: 0;
    padding: 8px 10px;
    background-color: var(--bg-primary);
    z-index: 10;
    border-top: 1px solid var(--border-color);
  }
  
  /* Input form optimization */
  .chat-input-form {
    display: flex;
    align-items: center;
    background-color: var(--bg-secondary);
    border-radius: 24px;
    padding: 2px 4px 2px 12px;
    border: 1px solid var(--border-color);
  }
  
  /* Message input optimization */
  #user-input {
    border: none;
    background-color: transparent;
    padding: 10px 0;
    margin: 0;
    min-height: 24px;
    max-height: 120px;
    font-size: 16px; /* Prevents zoom on iOS */
    border-radius: 0;
    box-shadow: none;
    flex: 1;
  }
  
  #user-input:focus {
    outline: none;
    box-shadow: none;
  }
  
  /* Send button optimization */
  .chat-input-form button {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: var(--accent-color);
    margin-left: 6px;
    /* Improve touch target */
    -webkit-tap-highlight-color: transparent;
  }
  
  /* Better tap feedback */
  button:active {
    transform: scale(0.95);
    transition: transform 0.1s;
  }
  
  /* Fix features row on mobile */
  .input-features {
    padding: 5px 0;
  }
  
  /* Fix code block overflow */
  .message-content pre {
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding: 10px;
    font-size: 13px;
  }
  
  /* Fix code display */
  .message-content pre code {
    font-family: "SF Mono", Consolas, Monaco, monospace;
    white-space: pre-wrap;
  }
  
  /* Improve image display */
  .message-content img {
    max-width: 100%;
    height: auto;
    border-radius: 8px;
    transition: max-height 0.2s ease;
    max-height: 200px;
    object-fit: contain;
  }
  
  .message-content img.expanded {
    max-height: 80vh;
  }
}

/* Fix for iOS notches */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .chat-input-container {
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
  }
  
  .app-footer {
    padding-bottom: env(safe-area-inset-bottom);
  }
  
  .enhanced-footer {
    padding-bottom: max(10px, env(safe-area-inset-bottom)) !important;
  }
}

/* iOS keyboard handling */
@supports (-webkit-touch-callout: none) {
  body.keyboard-open .app-footer {
    display: none;
  }
  
  body.keyboard-open .chat-input-container {
    border-bottom: none;
  }
}

/* Fix for Android tall virtual keyboards */
@media screen and (max-height: 500px) and (max-width: 768px) {
  .app-header {
    padding: 5px 10px;
  }
  
  .logo h1 {
    font-size: 1rem;
  }
  
  .chat-messages {
    padding: 5px;
  }
}

/* Fix scrolling for all touch devices */
@media (pointer: coarse) {
  .chat-messages {
    -webkit-overflow-scrolling: touch;
  }
  
  .message-container {
    padding: 8px 10px;
  }
}
