/* ============================================
   Terprint Chat Widget Styles
   Mobile-first responsive design
   ============================================ */

/* ================================
   Base Widget Container
   ================================ */

.terprint-chat-widget {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    font-size: 14px;
    line-height: 1.5;
    box-sizing: border-box;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

.terprint-chat-widget *,
.terprint-chat-widget *::before,
.terprint-chat-widget *::after {
    box-sizing: border-box;
}

/* ================================
   Floating Mode (Default)
   ================================ */

.terprint-chat-widget.floating-mode {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 99999;
}

.terprint-chat-widget.floating-mode.position-left {
    right: auto;
    left: 20px;
}

/* Toggle Button - Mobile Optimized */
.chat-toggle-btn {
    width: 60px;
    height: 60px;
    min-width: 60px;
    min-height: 60px;
    border-radius: 50%;
    border: none;
    background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
    color: white;
    cursor: pointer;
    box-shadow: 0 4px 20px rgba(17, 153, 142, 0.4);
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    position: relative;
    /* Touch improvements */
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    -webkit-user-select: none;
}

.chat-toggle-btn i {
    font-size: 26px;
    line-height: 1;
}

.chat-toggle-btn:hover {
    transform: scale(1.08);
    box-shadow: 0 6px 25px rgba(17, 153, 142, 0.5);
}

.chat-toggle-btn:active {
    transform: scale(0.95);
}

/* Pulse animation */
.chat-btn-pulse {
    position: absolute;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
    opacity: 0;
    animation: pulse-ring 2s infinite;
    z-index: -1;
    pointer-events: none;
}

@keyframes pulse-ring {
    0% {
        transform: scale(1);
        opacity: 0.5;
    }
    100% {
        transform: scale(1.5);
        opacity: 0;
    }
}

/* ================================
   Chat Container
   ================================ */

.chat-container {
    width: 380px;
    max-width: calc(100vw - 40px);
    height: 520px;
    max-height: calc(100vh - 100px);
    background: #1a1a2e;
    border-radius: 16px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: slideUp 0.3s ease-out;
}

/* Hide container when floating mode is closed */
.terprint-chat-widget.floating-mode:not(.open) .chat-container {
    display: none;
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px) scale(0.95);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

/* ================================
   Chat Header
   ================================ */

.chat-header {
    background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
    color: white;
    padding: 14px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
    min-height: 56px;
}

.chat-title {
    font-size: 16px;
    font-weight: 600;
    display: flex;
    align-items: center;
}

.chat-title i {
    font-size: 20px;
}

.chat-header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.chat-action-btn,
.chat-close-btn {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: white;
    width: 36px;
    height: 36px;
    min-width: 36px;
    min-height: 36px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.chat-action-btn:hover,
.chat-close-btn:hover {
    background: rgba(255, 255, 255, 0.3);
}

.chat-action-btn:active,
.chat-close-btn:active {
    background: rgba(255, 255, 255, 0.4);
}

/* ================================
   Chat Messages Area
   ================================ */

.chat-messages {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 16px;
    background: linear-gradient(180deg, #1a1a2e 0%, #16213e 100%);
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}

.chat-messages::-webkit-scrollbar {
    width: 6px;
}

.chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: #3d3d5c;
    border-radius: 3px;
}

/* ================================
   Welcome Message
   ================================ */

.chat-welcome {
    text-align: center;
    padding: 20px 10px;
}

.chat-welcome p {
    color: #adb5bd;
    margin-bottom: 20px;
    font-size: 15px;
    line-height: 1.6;
}

.example-queries {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.example-btn {
    background: #16213e;
    border: 1px solid #0f3460;
    border-radius: 20px;
    padding: 12px 16px;
    font-size: 13px;
    color: #e9ecef;
    cursor: pointer;
    transition: all 0.2s;
    text-align: left;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.example-btn:hover {
    background: #0f3460;
    border-color: #11998e;
    color: #ffffff;
}

.example-btn:active {
    background: #11998e;
    transform: scale(0.98);
}

/* ================================
   Chat Messages
   ================================ */

.chat-message {
    margin-bottom: 16px;
    animation: fadeIn 0.3s ease-out;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.chat-message.user {
    display: flex;
    justify-content: flex-end;
}

.chat-message.user .message-content {
    background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
    color: white;
    border-radius: 18px 18px 4px 18px;
    padding: 12px 16px;
    max-width: 85%;
    word-wrap: break-word;
    overflow-wrap: break-word;
}

.chat-message.assistant .message-content {
    background: #16213e;
    border: 1px solid #0f3460;
    border-radius: 18px 18px 18px 4px;
    padding: 12px 16px;
    max-width: 90%;
    color: #e9ecef;
    word-wrap: break-word;
    overflow-wrap: break-word;
    line-height: 1.6;
}

/* Message content links */
.chat-message.assistant .message-content a {
    color: #38ef7d;
    text-decoration: underline;
}

.chat-message.assistant .message-content a:hover {
    color: #11998e;
}

/* Product Links */
.product-links {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.product-link-btn {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 6px 12px;
    border-radius: 16px;
    font-size: 12px;
    text-decoration: none;
    background: #0f3460;
    color: #e9ecef;
    border: 1px solid #1a4a7a;
    transition: all 0.2s;
}

.product-link-btn:hover {
    background: #1a4a7a;
    color: #ffffff;
    text-decoration: none;
}

.product-link-btn.strain-link { border-color: #28a745; }
.product-link-btn.batch-link { border-color: #17a2b8; }
.product-link-btn.grower-link { border-color: #007bff; }

/* Message Sources */
.message-sources {
    margin-top: 8px;
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}

.source-badge {
    background: #0f3460;
    color: #adb5bd;
    font-size: 11px;
    padding: 2px 8px;
    border-radius: 10px;
}

/* ================================
   Loading Indicator
   ================================ */

.chat-message.loading .message-content {
    background: #16213e;
    border: 1px solid #0f3460;
    border-radius: 18px 18px 18px 4px;
    padding: 12px 16px;
}

.typing-indicator {
    display: flex;
    gap: 4px;
    align-items: center;
}

.typing-indicator span {
    width: 8px;
    height: 8px;
    background: #38ef7d;
    border-radius: 50%;
    animation: typing 1.4s infinite ease-in-out;
}

.typing-indicator span:nth-child(1) { animation-delay: 0s; }
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing {
    0%, 60%, 100% { transform: translateY(0); }
    30% { transform: translateY(-8px); }
}

/* ================================
   Chat Input
   ================================ */

.chat-input-container {
    padding: 12px 16px;
    border-top: 1px solid #0f3460;
    display: flex;
    gap: 10px;
    background: #1a1a2e;
    flex-shrink: 0;
    align-items: center;
}

.chat-input {
    flex: 1;
    border: 1px solid #0f3460;
    border-radius: 24px;
    padding: 12px 18px;
    font-size: 15px;
    outline: none;
    transition: border-color 0.2s;
    background: #16213e;
    color: #e9ecef;
    min-height: 48px;
    /* Mobile keyboard improvements */
    -webkit-appearance: none;
    appearance: none;
}

.chat-input::placeholder {
    color: #6c757d;
}

.chat-input:focus {
    border-color: #11998e;
    box-shadow: 0 0 0 2px rgba(17, 153, 142, 0.2);
}

.chat-input:disabled {
    background: #0f3460;
    opacity: 0.7;
}

.chat-send-btn {
    width: 48px;
    height: 48px;
    min-width: 48px;
    min-height: 48px;
    border: none;
    border-radius: 50%;
    background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
    color: white;
    cursor: pointer;
    font-size: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.chat-send-btn:hover:not(:disabled) {
    transform: scale(1.05);
    box-shadow: 0 4px 15px rgba(17, 153, 142, 0.4);
}

.chat-send-btn:active:not(:disabled) {
    transform: scale(0.95);
}

.chat-send-btn:disabled {
    background: #3d3d5c;
    cursor: not-allowed;
    opacity: 0.6;
}

/* ================================
   Powered By Footer
   ================================ */

.powered-by-terprint {
    text-align: center;
    padding: 8px;
    font-size: 11px;
    color: #6c757d;
    border-top: 1px solid #0f3460;
    background: #1a1a2e;
    flex-shrink: 0;
}

.powered-by-terprint a {
    color: #38ef7d;
    text-decoration: none;
}

.powered-by-terprint a:hover {
    text-decoration: underline;
}

/* ================================
   Inline Mode
   ================================ */

.terprint-chat-widget.inline-mode {
    position: relative;
    width: 100%;
}

.terprint-chat-widget.inline-mode .chat-container {
    width: 100%;
    max-width: 100%;
    height: 500px;
    border-radius: 12px;
    animation: none;
}

/* ================================
   Fullscreen Mode
   ================================ */

.terprint-chat-widget.fullscreen-mode {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 999999;
    padding: 0;
}

.terprint-chat-widget.fullscreen-mode .chat-container {
    width: 100%;
    max-width: 100%;
    height: 100%;
    max-height: 100%;
    border-radius: 0;
    animation: none;
}

/* ================================
   Homepage Chat Section
   ================================ */

.home-chat-section {
    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
    border-radius: 16px;
    padding: 4px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
}

.home-chat-container {
    max-width: 100%;
    margin: 0 auto;
}

.home-chat-container .chat-container {
    box-shadow: none;
}

/* ================================
   Mobile Responsive - Critical Fixes
   ================================ */

@media (max-width: 768px) {
    /* Floating mode - full screen on mobile */
    .terprint-chat-widget.floating-mode {
        bottom: 16px;
        right: 16px;
    }

    .terprint-chat-widget.floating-mode.position-left {
        left: 16px;
    }

    .terprint-chat-widget.floating-mode.open .chat-container {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100vw;
        max-width: 100vw;
        height: 100vh;
        max-height: 100vh;
        border-radius: 0;
        z-index: 999999;
    }

    /* Hide toggle button when chat is open */
    .terprint-chat-widget.floating-mode.open .chat-toggle-btn {
        display: none;
    }

    /* Ensure toggle button is always visible and tappable */
    .chat-toggle-btn {
        width: 56px;
        height: 56px;
        min-width: 56px;
        min-height: 56px;
        font-size: 22px;
        /* Ensure it's above everything */
        position: relative;
        z-index: 99999;
    }

    .chat-btn-pulse {
        width: 56px;
        height: 56px;
    }

    /* Input improvements for mobile */
    .chat-input {
        font-size: 16px; /* Prevent iOS zoom on focus */
        min-height: 46px;
        padding: 10px 16px;
    }

    .chat-send-btn {
        width: 46px;
        height: 46px;
        min-width: 46px;
        min-height: 46px;
    }

    .chat-action-btn,
    .chat-close-btn {
        width: 40px;
        height: 40px;
        min-width: 40px;
        min-height: 40px;
    }

    /* Inline mode on mobile */
    .terprint-chat-widget.inline-mode .chat-container {
        height: 450px;
        border-radius: 12px;
    }

    /* Example buttons - easier to tap */
    .example-btn {
        padding: 14px 16px;
        font-size: 14px;
    }

    /* Messages */
    .chat-message.assistant .message-content,
    .chat-message.user .message-content {
        max-width: 90%;
    }
}

/* Small mobile devices */
@media (max-width: 480px) {
    .terprint-chat-widget.floating-mode {
        bottom: 12px;
        right: 12px;
    }

    .terprint-chat-widget.floating-mode.position-left {
        left: 12px;
    }

    .chat-toggle-btn {
        width: 54px;
        height: 54px;
        min-width: 54px;
        min-height: 54px;
    }

    .chat-btn-pulse {
        width: 54px;
        height: 54px;
    }

    .chat-header {
        padding: 12px 14px;
    }

    .chat-messages {
        padding: 12px;
    }

    .chat-input-container {
        padding: 10px 12px;
    }

    .terprint-chat-widget.inline-mode .chat-container {
        height: 400px;
    }
}

/* Landscape mobile */
@media (max-height: 500px) and (orientation: landscape) {
    .terprint-chat-widget.floating-mode.open .chat-container {
        height: 100vh;
    }

    .terprint-chat-widget.inline-mode .chat-container {
        height: 300px;
    }
}

/* ================================
   Safe Area Insets (iPhone X+)
   ================================ */

@supports (padding-bottom: env(safe-area-inset-bottom)) {
    .terprint-chat-widget.floating-mode {
        bottom: calc(16px + env(safe-area-inset-bottom));
        right: calc(16px + env(safe-area-inset-right));
    }

    .terprint-chat-widget.floating-mode.position-left {
        left: calc(16px + env(safe-area-inset-left));
    }

    .terprint-chat-widget.floating-mode.open .chat-input-container {
        padding-bottom: calc(12px + env(safe-area-inset-bottom));
    }
}

/* ================================
   High Contrast / Accessibility
   ================================ */

@media (prefers-reduced-motion: reduce) {
    .chat-toggle-btn,
    .chat-container,
    .chat-message,
    .chat-btn-pulse {
        animation: none;
        transition: none;
    }

    .chat-btn-pulse {
        display: none;
    }
}

/* Focus visible for keyboard navigation */
.chat-toggle-btn:focus-visible,
.chat-send-btn:focus-visible,
.chat-close-btn:focus-visible,
.chat-action-btn:focus-visible,
.example-btn:focus-visible,
.chat-input:focus-visible {
    outline: 2px solid #38ef7d;
    outline-offset: 2px;
}
