/* Mobile-specific improvements for Plato chat interface */

/* Prevent zoom on input focus for mobile devices */
@media (max-width: 600px) {
  html, body {
    overflow: hidden; /* Prevent document scrolling */
    position: fixed; /* Lock the viewport */
    width: 100%;
    height: 100%;
    touch-action: manipulation; /* Improve touch handling */
    overscroll-behavior: none; /* Prevents bounce/rubber-band effect */
    max-width: 100vw;
    /* Removed safe area padding to prevent spacing in React Native webview */
    padding: 0;
    margin: 0;
  }
  
  input, textarea, select, button {
    font-size: 16px !important; /* This prevents iOS from zooming in on input focus */
    transform-origin: left top;
    transform: scale(1);
  }
  
  .chat-container {
    height: 100%;
    max-height: 100vh;
    /* Removed safe area calculations */
    padding: 0;
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    overflow: hidden;
    background-color: var(--background-color);
  }
  
  /* Make header much smaller on mobile */
  .chat-header {
    margin-bottom: 5px;
    padding: 8px 16px;
    flex-shrink: 0; /* Prevent header from shrinking */
    position: sticky;
    top: 0;
    z-index: 10;
    background-color: var(--background-color);
  }
  
  .header-icon {
    width: 35px;
    height: 52px;
  }
  
  .header-text h1 {
    font-size: 20px;
    margin: 0;
    color: var(--primary-color);
  }
  
  .header-text p {
    font-size: 11px;
    margin: 0;
    color: var(--secondary-color);
  }
  
  /* Enlarge the chat area and prevent content overflow */
  .chat-messages {
    flex: 1;
    overflow-y: auto; /* Only allow scrolling here */
    overflow-x: hidden; /* Prevent horizontal scrolling */
    height: auto; /* Let flex handle the height */
    max-height: none; /* Remove max-height restrictions */
    margin-bottom: 0;
    padding: 10px 16px;
    -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
    background-color: white;
    border: none; /* Remove border that might cause layout issues */
    border-radius: 0; /* Remove border radius for full width */
  }
  
  /* Fix footer to bottom of screen and prevent keyboard issues */
  .chat-footer {
    padding: 12px 16px;
    width: 100%;
    position: sticky;
    bottom: 0;
    z-index: 10;
    flex-shrink: 0; /* Prevent footer from shrinking */
    background-color: var(--background-color);
    box-sizing: border-box;
  }
  
  /* iMessage-style input area with proper sizing */
  .chat-input-container {
    display: flex;
    align-items: center;
    padding: 0;
    gap: 8px;
    background: white;
    border: 1px solid #ddd;
    border-radius: 25px;
    width: 100%;
    min-height: 44px;
    position: relative;
    z-index: 20;
    box-sizing: border-box;
  }
  
  .input-wrapper {
    flex-grow: 1;
    border: none;
    background: transparent;
    padding: 0;
    min-height: 44px; /* Apple's recommended minimum tap target size */
    display: flex;
    align-items: center;
    position: relative;
  }
  
  #chat-input {
    border: none;
    border-radius: 0;
    padding: 12px 15px;
    font-size: 16px !important; /* Prevent zoom */
    min-height: 20px;
    height: auto;
    max-height: 80px;
    line-height: 20px;
    box-sizing: border-box;
    background: transparent;
    outline: none;
    width: 100%;
    margin: 0;
    resize: none;
    overflow-y: auto;
    position: relative;
    z-index: 2;
    /* Prevent zoom and layout shifts */
    transform-origin: left top;
    transform: scale(1);
  }
  
  /* Ensure input doesn't cause zoom when focused */
  #chat-input:focus {
    outline: none;
    border: none;
    font-size: 16px !important;
    zoom: 1;
    transform: scale(1);
  }
  
  #send-button {
    height: 44px;
    width: 70px;
    padding: 0;
    border-radius: 22px;
    background-color: var(--primary-color);
    font-weight: 500;
    flex-shrink: 0;
    min-width: 60px;
    font-size: 14px;
    border: none;
    color: white;
    cursor: pointer;
  }
  
  /* Handle keyboard visibility better */
  body.keyboard-visible {
    height: 100vh;
    height: 100dvh; /* Use dynamic viewport height */
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    overflow: hidden;
  }
  
  body.keyboard-visible .chat-container {
    height: 100vh;
    height: 100dvh;
    max-height: 100vh;
    max-height: 100dvh;
    overflow: hidden;
  }
  
  body.keyboard-visible .chat-messages {
    flex: 1;
    overflow-y: auto;
    height: auto;
    padding-bottom: 10px;
  }
  
  body.keyboard-visible .chat-footer {
    position: sticky;
    bottom: 0;
    /* Removed safe area padding */
    padding-bottom: 12px;
  }
  
  /* Ensure page doesn't scroll */
  #app, #root, main, body > div {
    overflow: hidden;
    height: 100%;
  }
  
  /* Message styling improvements */
  .message {
    margin-bottom: 12px;
    padding: 10px 15px;
    border-radius: 18px;
    max-width: 85%;
    word-wrap: break-word;
    line-height: 1.4;
  }
  
  .message.user {
    background-color: var(--primary-color);
    color: white;
    margin-left: auto;
    border-bottom-right-radius: 4px;
  }
  
  .message.bot {
    background-color: var(--background-color);
    color: var(--text2-color);
    margin-right: auto;
    border-bottom-left-radius: 4px;
    -webkit-text-stroke: 0.3px rgba(255, 255, 255, 0.3);
    font-weight: 500;
  }
  
  /* Match typing indicator with main styles.css */
  .message.typing {
    background-color: var(--background-color);
    color: var(--secondary-color);
    font-style: italic;
    margin-right: auto;
  }
}

/* Fix for iPhone Safari additional height issues - safe areas removed */
@supports (-webkit-touch-callout: none) {
  @media (max-width: 600px) {
    html, body {
      height: 100vh;
      /* Removed safe area padding */
      padding: 0;
      margin: 0;
    }
    
    .chat-container {
      height: 100vh;
      top: 0;
      bottom: 0;
      /* Removed safe area padding */
      padding: 0;
    }
    
    body.keyboard-visible .chat-container {
      height: 100vh;
    }
    
    .chat-messages {
      height: auto;
      flex: 1;
    }
    
    body.keyboard-visible .chat-messages {
      height: auto;
      flex: 1;
    }
    
    /* Fixed padding without safe area calculations */
    .chat-header {
      padding-top: 8px;
    }
    
    .chat-footer {
      padding-bottom: 12px;
    }
  }
}

/* Additional mobile fixes for input handling */
@media screen and (max-width: 767px) {
  input:focus, textarea:focus, select:focus {
    zoom: 1 !important;
    transform: scale(1) !important;
    font-size: 16px !important;
  }
  
  /* Prevent horizontal scrolling */
  * {
    max-width: 100vw;
    box-sizing: border-box;
  }
  
  /* Ensure messages don't overflow */
  .message {
    max-width: calc(100vw - 40px);
    word-break: break-word;
    overflow-wrap: break-word;
  }
}

/* Fix for dynamic viewport height on mobile browsers */
@media (max-width: 600px) {
  .chat-container {
    height: 100vh;
    height: 100dvh; /* Use dynamic viewport height when available */
  }
  
  body.keyboard-visible .chat-container {
    height: 100vh;
    height: 100dvh;
  }
}