/**
 * Mobile Chat Optimizations
 * Spezifische CSS-Regeln für optimale mobile Darstellung des Chatfensters
 */

/* Accessibility: Reduzierte Bewegung respektieren */
@media (prefers-reduced-motion: reduce) {
    .chat-modal,
    .chat-modal-content,
    .chat-bubble,
    .message,
    .message-content,
    .send-button,
    .followup-prompt,
    .btn,
    button {
        animation: none !important;
        transition: none !important;
    }
}

/* Basis Mobile Styles */
@media (max-width: 768px) {

    /* Verhindert horizontales Scrollen */
    body {
        overflow-x: hidden;
    }

    /* Chat Modal Vollbild-Optimierung */
    .chat-modal {
        padding: 0 !important;
        background-color: rgba(0, 0, 0, 0.95) !important;
        backdrop-filter: blur(3px);
        -webkit-backdrop-filter: blur(3px);
    }

    .chat-modal-content {
        width: 100vw !important;
        height: 100vh !important;
        height: 100dvh !important; /* Dynamic viewport height für iOS Safari */
        max-width: 100vw !important;
        max-height: 100vh !important;
        max-height: 100dvh !important; /* Dynamic viewport height für iOS Safari */
        border-radius: 0 !important;
        transform: none !important;
        margin: 0 !important;
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        box-shadow: none !important;
        display: flex !important;
        flex-direction: column !important;
        overflow: hidden !important;
    }

    /* Chat Header Mobile */
    .chat-modal-content .chat-header {
        flex-shrink: 0;
        padding: 1rem;
        border-bottom: 1px solid var(--border-color);
        background-color: var(--card-bg);
        position: sticky;
        top: 0;
        z-index: 10;
        /* Relativ zum Modal-Container, nicht zur Seite */
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    }

    .chat-header .chat-title {
        font-size: 1.1rem;
        font-weight: 600;
    }

    .chat-header .btn {
        padding: 0.5rem;
        font-size: 1rem;
        min-width: 44px;
        min-height: 44px;
        /* Apple Human Interface Guidelines: 44x44px Touch-Target */
    }

    /* Chat Body Mobile */
    .chat-body,
    .chat-body.expanded,
    .chat-modal-content .chat-body {
        flex: 1 !important;
        height: auto !important;
        max-height: none !important;
        overflow-y: auto !important;
        padding: 1rem !important;
        padding-bottom: 160px !important;
        /* Platz für Input-Container: Input (48px) + Gap (12px) + Button (48px) + Padding (32px) + Sicherheit (20px) = ~160px */
        -webkit-overflow-scrolling: touch;
        scroll-behavior: smooth;
    }

    /* Chat Messages Mobile */
    .message {
        max-width: 90% !important;
        margin-bottom: 1rem !important;
        word-wrap: break-word;
        overflow-wrap: break-word;
    }

    .message-content {
        font-size: 0.9rem !important;
        line-height: 1.5 !important;
        padding: 0.75rem 1rem !important;
        border-radius: 1rem !important;
    }

    .message-user {
        margin-left: auto;
    }

    .message-assistant {
        margin-right: auto;
    }

    /* Chat Input Container Mobile */
    .chat-input-container {
        position: fixed !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        margin: 0 !important;
        padding: 1rem !important;
        background-color: var(--card-bg) !important;
        border-top: 1px solid var(--border-color) !important;
        z-index: 9999 !important;
        /* oberste Ebene, über anderen Overlays */
        box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.15);
        backdrop-filter: blur(10px);
        -webkit-backdrop-filter: blur(10px);
        display: flex !important;
        flex-direction: column !important;
        gap: 0.75rem !important;
        pointer-events: auto !important;
        /* sicherstellen, dass Klicks ankommen */
    }

    .chat-input-container .chat-input {
        width: 100% !important;
        margin-bottom: 0 !important;
        font-size: 16px !important;
        /* Verhindert Zoom auf iOS */
        padding: 0.75rem 1rem !important;
        border-radius: 0.75rem !important;
        border: 1px solid var(--border-color) !important;
        background-color: var(--bg-color) !important;
        color: var(--text-color) !important;
        min-height: 48px;
        /* Bessere Touch-Targets */
        box-sizing: border-box !important;
    }

    .chat-input-container .send-button {
        width: 100% !important;
        padding: 0.75rem 1rem !important;
        font-size: 1rem !important;
        font-weight: 600 !important;
        border-radius: 0.75rem !important;
        min-height: 48px;
        /* Bessere Touch-Targets */
        background-color: var(--primary-color) !important;
        border: none !important;
    }

    /* Audio-Buttons Mobile-Optimierung */
    .audio-controls {
        display: flex !important;
        gap: 0.5rem !important;
        width: 100% !important;
        margin-bottom: 0.5rem !important;
    }

    .audio-controls button,
    .btn-audio,
    #microphoneButton,
    #ttsButton {
        min-width: 44px !important;
        min-height: 44px !important;
        /* Apple Human Interface Guidelines: 44x44px Touch-Target */
        padding: 0.75rem !important;
        font-size: 1.2rem !important;
        border-radius: 0.75rem !important;
        flex: 1 !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        gap: 0.5rem !important;
        touch-action: manipulation !important;
        /* Verhindert Doppel-Tap-Zoom */
        -webkit-tap-highlight-color: transparent !important;
        /* Entfernt iOS Tap-Highlight */
    }

    /* Mikrofon-Button Mobile */
    #microphoneButton {
        background-color: var(--primary-color) !important;
        color: white !important;
        border: none !important;
        transition: all 0.2s ease !important;
    }

    #microphoneButton:active {
        transform: scale(0.95) !important;
        background-color: var(--primary-dark) !important;
    }

    #microphoneButton.recording {
        background-color: #dc3545 !important;
        animation: pulse-recording 1.5s ease-in-out infinite !important;
    }

    @keyframes pulse-recording {

        0%,
        100% {
            opacity: 1;
            transform: scale(1);
        }

        50% {
            opacity: 0.8;
            transform: scale(1.05);
        }
    }

    /* TTS-Button Mobile */
    #ttsButton {
        background-color: var(--secondary-color, #6c757d) !important;
        color: white !important;
        border: none !important;
        transition: all 0.2s ease !important;
    }

    #ttsButton:active {
        transform: scale(0.95) !important;
        background-color: var(--secondary-dark, #5a6268) !important;
    }

    #ttsButton.playing {
        background-color: #28a745 !important;
        animation: pulse-playing 1.5s ease-in-out infinite !important;
    }

    @keyframes pulse-playing {

        0%,
        100% {
            opacity: 1;
        }

        50% {
            opacity: 0.7;
        }
    }

    /* Audio-Status-Anzeige Mobile */
    .audio-status {
        font-size: 0.85rem !important;
        padding: 0.5rem !important;
        text-align: center !important;
        border-radius: 0.5rem !important;
        margin-bottom: 0.5rem !important;
    }

    .audio-status.recording {
        background-color: rgba(220, 53, 69, 0.1) !important;
        color: #dc3545 !important;
        border: 1px solid rgba(220, 53, 69, 0.3) !important;
    }

    .audio-status.playing {
        background-color: rgba(40, 167, 69, 0.1) !important;
        color: #28a745 !important;
        border: 1px solid rgba(40, 167, 69, 0.3) !important;
    }

    /* Touch-Feedback für alle interaktiven Elemente */
    button:active,
    .btn:active {
        transform: scale(0.95) !important;
        transition: transform 0.1s ease !important;
        color: white !important;
        transition: all 0.2s ease;
        box-sizing: border-box !important;
    }

    .chat-input-container .send-button:hover,
    .chat-input-container .send-button:focus {
        background-color: var(--primary-hover) !important;
        transform: translateY(-1px);
        box-shadow: 0 4px 12px rgba(140, 82, 255, 0.3);
    }

    .chat-input-container .send-button:active {
        transform: translateY(0);
    }

    /* Follow-up Prompts Mobile */
    .followup-prompts {
        margin: 1rem 0 !important;
        display: flex;
        flex-direction: column;
        gap: 0.5rem;
    }

    .followup-prompt {
        font-size: 0.85rem !important;
        padding: 0.75rem 1rem !important;
        border-radius: 0.75rem !important;
        border: 1px solid var(--border-color) !important;
        background-color: var(--card-bg) !important;
        color: var(--text-color) !important;
        cursor: pointer;
        transition: all 0.2s ease;
        min-height: 44px;
        /* Touch-Target */
        display: flex;
        align-items: center;
        text-align: left;
        word-wrap: break-word;
    }

    .followup-prompt:hover,
    .followup-prompt:focus {
        background-color: var(--primary-color) !important;
        color: white !important;
        transform: translateY(-1px);
        box-shadow: 0 2px 8px rgba(140, 82, 255, 0.2);
    }

    .followup-prompt:active {
        transform: translateY(0);
    }

    /* Chat Forms Mobile */
    .chat-form-container {
        margin: 1rem 0 !important;
    }

    .chat-form {
        border-radius: 1rem !important;
        margin-bottom: 1rem !important;
        overflow: hidden;
    }

    .chat-form-header {
        padding: 1rem !important;
        font-size: 1rem !important;
        font-weight: 600;
    }

    .chat-form-body {
        padding: 1rem !important;
    }

    .chat-form .form-control {
        font-size: 16px !important;
        /* Verhindert Zoom auf iOS */
        margin-bottom: 0.75rem !important;
        padding: 0.75rem !important;
        border-radius: 0.5rem !important;
        min-height: 48px;
        /* Touch-Target */
    }

    .chat-form .btn {
        width: 100% !important;
        padding: 0.75rem !important;
        font-size: 1rem !important;
        font-weight: 600 !important;
        border-radius: 0.75rem !important;
        min-height: 48px;
        /* Touch-Target */
    }

    /* Typing Indicator Mobile */
    .typing-indicator {
        margin: 1rem 0;
        padding: 0.75rem 1rem;
        background-color: var(--card-bg);
        border-radius: 1rem 1rem 1rem 0.25rem;
        max-width: 80px;
        display: inline-block;
    }

    /* Scrollbar für Mobile */
    .chat-body::-webkit-scrollbar {
        width: 4px;
    }

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

    .chat-body::-webkit-scrollbar-thumb {
        background: rgba(0, 0, 0, 0.2);
        border-radius: 2px;
    }

    /* Dark Mode Anpassungen */
    .dark-mode .chat-input-container {
        background-color: var(--card-bg-dark) !important;
        border-top-color: var(--border-color-dark) !important;
    }

    .dark-mode .chat-input-container .chat-input {
        background-color: var(--bg-color-dark) !important;
        border-color: var(--border-color-dark) !important;
        color: var(--text-color-dark) !important;
    }

    .dark-mode .followup-prompt {
        background-color: var(--card-bg-dark) !important;
        border-color: var(--border-color-dark) !important;
        color: var(--text-color-dark) !important;
    }

    .dark-mode .chat-form {
        background-color: var(--card-bg-dark) !important;
        border-color: var(--border-color-dark) !important;
    }
}

/* Sehr kleine Bildschirme (< 480px) */
@media (max-width: 480px) {
    .chat-header .chat-title {
        font-size: 1rem;
    }

    .chat-header .btn {
        padding: 0.375rem;
        font-size: 0.9rem;
        min-width: 40px;
        min-height: 40px;
    }

    .message {
        max-width: 95% !important;
    }

    .message-content {
        font-size: 0.85rem !important;
        padding: 0.625rem 0.875rem !important;
    }

    .chat-input-container {
        padding: 0.75rem !important;
    }

    .followup-prompt {
        font-size: 0.8rem !important;
        padding: 0.625rem 0.875rem !important;
    }
}

/* Sehr kleine Bildschirme (≤ 390px, z.B. iPhone 12/13/14 Standard) */
@media (max-width: 390px) {
    .chat-header .chat-title {
        font-size: 0.9rem;
    }

    .chat-header .btn {
        padding: 0.25rem;
        font-size: 0.85rem;
        min-width: 36px;
        min-height: 36px;
    }

    .chat-header .chat-controls {
        gap: 0.25rem;
    }

    .message {
        max-width: 98% !important;
    }

    .message-content {
        font-size: 0.8rem !important;
        padding: 0.5rem 0.75rem !important;
        line-height: 1.4 !important;
    }

    .chat-input-container {
        padding: 0.5rem !important;
        gap: 0.5rem !important;
    }

    .chat-input-container .chat-input {
        padding: 0.5rem 0.75rem !important;
        min-height: 40px !important;
        font-size: 16px !important; /* Verhindert iOS Zoom */
    }

    .chat-input-container .send-button {
        min-height: 40px !important;
        padding: 0.5rem 0.75rem !important;
        font-size: 0.9rem !important;
    }

    .followup-prompt {
        font-size: 0.75rem !important;
        padding: 0.5rem 0.75rem !important;
        min-height: 36px !important;
    }

    .audio-controls button,
    .btn-audio,
    #microphoneButton,
    #ttsButton {
        min-width: 36px !important;
        min-height: 36px !important;
        padding: 0.5rem !important;
        font-size: 1rem !important;
    }

    /* Chat Body mit weniger Padding */
    .chat-body,
    .chat-body.expanded,
    .chat-modal-content .chat-body {
        padding: 0.75rem !important;
        padding-bottom: 140px !important;
    }
}

/* Landscape Orientation auf Mobile */
@media (max-width: 768px) and (orientation: landscape) {

    .chat-body,
    .chat-body.expanded,
    .chat-modal-content .chat-body {
        padding-bottom: 140px !important;
        /* Etwas weniger als Portrait, da horizontaler Platz vorhanden ist */
    }

    .chat-input-container {
        padding: 0.75rem !important;
    }

    .chat-input-container .chat-input {
        margin-bottom: 0.5rem !important;
    }
}

/* iOS Keyboard-Handling */
@supports (height: 100dvh) {
    @media (max-width: 768px) {
        .chat-modal-content {
            height: 100dvh !important;
            max-height: 100dvh !important;
        }
        
        .chat-input-container {
            /* Smooth transition für Keyboard-Anpassung */
            transition: bottom 0.15s ease-out !important;
        }
        
        .chat-body,
        .chat-body.expanded {
            /* Smooth transition für Padding-Anpassung */
            transition: padding-bottom 0.15s ease-out !important;
        }
    }
}

/* Safe Area Insets für iPhone X und neuer (Notch-Geräte) */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
    @media (max-width: 768px) {
        .chat-input-container {
            padding-bottom: calc(1rem + env(safe-area-inset-bottom)) !important;
        }
        
        .chat-body,
        .chat-body.expanded {
            padding-bottom: calc(160px + env(safe-area-inset-bottom)) !important;
        }
    }
}