/**
 * JEANNIE© Chat Widget - Enhanced Styles V6
 * Version 6.0 - Embedded Only, iOS Safari Fix
 * KEIN POPUP - Nur Fullscreen Embedded Layout
 */

/* CSS Custom Properties für einfache Anpassung */
:root {
    --primary-color: #3f893a;
    --secondary-color: #2d6b29;
    --widget-bg-color: #ffffff;
    --font-color: #333333;
    --border-radius: 12px;
    --shadow-light: 0 4px 16px rgba(0, 0, 0, 0.1);
    --shadow-heavy: 0 8px 32px rgba(0, 0, 0, 0.12);
    --transition-fast: 0.2s ease;
    --transition-smooth: 0.3s ease-out;
    --chat-sidebar-width: 280px;
    --audio-button-color: #dc3545;
    --audio-recording-color: #ff1744;
}

/* Dark Mode Support */
@media (prefers-color-scheme: dark) {
    :root {
        --widget-bg-color: #1f1f1f;
        --font-color: #e5e5e5;
    }
}

/* High Contrast Mode Support */
@media (prefers-contrast: high) {
    :root {
        --primary-color: #000000;
        --secondary-color: #000000;
        --widget-bg-color: #ffffff;
        --font-color: #000000;
    }
}

/* Reduced Motion Support */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* Base Font Setup */
body,
#chat-widget-container,
#chat-sidebar,
#chat-menu {
    font-family: 'Montserrat', -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
}

/* HTML & Body - Fullscreen Setup mit iOS Safari Fix */
html {
    height: 100vh; /* Fallback */
    height: 100dvh; /* Modern browsers */
}

body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100vh; /* Fallback */
    height: 100dvh; /* Modern browsers */
    overflow: auto;
    display: flex;
    flex-direction: row;
}

/* iOS Safari spezifisch: svh für stabiles Layout */
@supports (-webkit-touch-callout: none) {
    html {
        height: 100svh; /* Small Viewport Height - Toolbar immer sichtbar */
    }
    
    body {
        height: 100svh;
    }
}

/* Chat Sidebar */
#chat-sidebar {
    width: var(--chat-sidebar-width);
    background: #f8f9fa;
    border-right: 1px solid #e5e7eb;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    flex-shrink: 0;
}

#chat-sidebar-header {
    background: var(--primary-color);
    color: white;
    padding: 16px;
    font-weight: 600;
    font-size: 14px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
}

#new-chat-btn {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: white;
    padding: 6px 12px;
    border-radius: 6px;
    font-size: 12px;
    cursor: pointer;
    transition: var(--transition-fast);
}

#new-chat-btn:hover {
    background: rgba(255, 255, 255, 0.3);
}

#chat-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.chat-item {
    padding: 12px;
    border-radius: 8px;
    margin-bottom: 4px;
    cursor: pointer;
    transition: var(--transition-fast);
    border: 1px solid transparent;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
}

.chat-item > div:first-child {
    flex: 1;
    min-width: 0;
    overflow: hidden;
}

.chat-item:hover {
    background: #e9ecef;
    border-color: #dee2e6;
}

.chat-item.active {
    background: var(--primary-color);
    color: white;
}

.chat-item-title {
    font-size: 13px;
    font-weight: 500;
    margin-bottom: 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-item-date {
    font-size: 11px;
    opacity: 0.7;
}

.chat-item-delete {
    background: none;
    border: none;
    color: inherit;
    font-size: 12px;
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    opacity: 0.7;
    transition: var(--transition-fast);
    flex-shrink: 0;
}

.chat-item-delete:hover {
    opacity: 1;
    background: rgba(0, 0, 0, 0.1);
}

.chat-item.active .chat-item-delete:hover {
    background: rgba(255, 255, 255, 0.2);
}

/* Chat Widget Container - Fullscreen Embedded */
#chat-widget-container {
    flex: 1;
    background: var(--widget-bg-color);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    color: var(--font-color);
}

/* Header Styling */
#chat-widget-header {
    background: var(--primary-color);
    color: white;
    padding: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    min-height: 60px;
    flex-shrink: 0;
}

#chat-widget-branding {
    display: flex;
    align-items: center;
    gap: 12px;
}

#chat-widget-logo {
    width: 54px;
    height: 54px;
    border-radius: 8px;
    object-fit: cover;
    background: rgba(255, 255, 255, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    flex-shrink: 0;
}

#chat-widget-logo img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 4px;
}

#chat-widget-title {
    font-weight: 600;
    font-size: 16px;
    line-height: 1.2;
}

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

#menu-button {
    background: none;
    border: none;
    color: white;
    font-size: 18px;
    cursor: pointer;
    padding: 8px;
    border-radius: 6px;
    transition: var(--transition-fast);
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
}

#menu-button:hover {
    background: rgba(255, 255, 255, 0.2);
}

/* Chat Body */
#chat-widget-body {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
    overflow-x: hidden;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
}

/* Custom Scrollbar */
#chat-widget-body::-webkit-scrollbar,
#chat-list::-webkit-scrollbar,
#chat-menu-body::-webkit-scrollbar {
    width: 6px;
}

#chat-widget-body::-webkit-scrollbar-track,
#chat-list::-webkit-scrollbar-track,
#chat-menu-body::-webkit-scrollbar-track {
    background: transparent;
}

#chat-widget-body::-webkit-scrollbar-thumb,
#chat-list::-webkit-scrollbar-thumb,
#chat-menu-body::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 3px;
}

#chat-widget-body::-webkit-scrollbar-thumb:hover,
#chat-list::-webkit-scrollbar-thumb:hover,
#chat-menu-body::-webkit-scrollbar-thumb:hover {
    background: rgba(0, 0, 0, 0.3);
}

/* Message Styling */
.chat-message {
    padding: 14px 18px;
    border-radius: var(--border-radius);
    font-size: 14px;
    line-height: 1.5;
    word-wrap: break-word;
    word-break: break-word;
    max-width: 85%;
    animation: messageAppear var(--transition-smooth);
    position: relative;
}

.chat-message.bot ul,
.chat-message.bot ol {
    padding-left: 20px;
    margin-top: 8px;
    margin-bottom: 8px;
}

.chat-message.bot li {
    margin-bottom: 4px;
}

.chat-message.bot a {
    color: #ffffff;
    text-decoration: underline;
    transition: var(--transition-fast);
}

.chat-message.bot a:hover {
    opacity: 0.8;
}

.chat-message.bot strong {
    font-weight: 600;
}

.chat-message.bot em {
    font-style: italic;
}

/* Audio Message Styles */
.chat-message.audio {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.audio-player {
    background: rgba(255, 255, 255, 0.2);
    padding: 8px 12px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.audio-play-btn {
    background: rgba(255, 255, 255, 0.3);
    border: none;
    color: white;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    transition: var(--transition-fast);
}

.audio-play-btn:hover {
    background: rgba(255, 255, 255, 0.4);
}

.audio-progress {
    flex: 1;
    height: 4px;
    background: rgba(255, 255, 255, 0.3);
    border-radius: 2px;
    overflow: hidden;
}

.audio-progress-bar {
    height: 100%;
    background: white;
    width: 0%;
    transition: width 0.1s linear;
}

.audio-duration {
    font-size: 12px;
    opacity: 0.8;
}

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

.chat-message.user {
    background: #f1f1f1;
    color: #333333;
    align-self: flex-end;
    margin-left: auto;
    box-shadow: var(--shadow-light);
}

.chat-message.bot {
    background: #84ba3e;
    color: #ffffff;
    align-self: flex-start;
    box-shadow: var(--shadow-light);
}

.chat-message.welcome {
    background: #84ba3e;
    color: #ffffff;
    align-self: flex-start;
    font-weight: 500;
    box-shadow: var(--shadow-light);
}

/* Message Status Indicators */
.message-status {
    font-size: 10px;
    color: #666;
    margin-top: 6px;
    display: flex;
    align-items: center;
    gap: 4px;
    opacity: 0.7;
}

.status-icon {
    width: 12px;
    height: 12px;
}

/* Footer - KEINE fixed Position */
#chat-widget-footer {
    padding: 16px;
    padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    border-top: 1px solid #e5e7eb;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: var(--widget-bg-color);
    flex-shrink: 0;
}

#chat-widget-controls {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 4px;
}

.response-mode-toggle {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 12px;
    color: #666;
}

.toggle-switch {
    position: relative;
    display: inline-block;
    width: 40px;
    height: 20px;
}

.toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.toggle-slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccc;
    transition: var(--transition-fast);
    border-radius: 20px;
}

.toggle-slider:before {
    position: absolute;
    content: "";
    height: 16px;
    width: 16px;
    left: 2px;
    bottom: 2px;
    background-color: white;
    transition: var(--transition-fast);
    border-radius: 50%;
}

input:checked + .toggle-slider {
    background-color: var(--primary-color);
}

input:checked + .toggle-slider:before {
    transform: translateX(20px);
}

#chat-widget-input-row {
    display: flex;
    gap: 12px;
    align-items: flex-end;
}

#chat-widget-input {
    background: #ffffff;
    color: #333333;
    flex: 1;
    padding: 14px 18px;
    border: 2px solid #d1d5db;
    border-radius: 24px;
    outline: none;
    font-size: 14px;
    resize: none;
    min-height: 20px;
    max-height: 100px;
    font-family: inherit;
    transition: var(--transition-fast);
    line-height: 1.4;
}

#chat-widget-input:focus {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(63, 137, 58, 0.15);
}

#chat-widget-input::placeholder {
    color: #9ca3af;
}

/* Audio Recording Button */
#audio-record-btn {
    background: var(--audio-button-color);
    color: white;
    border: none;
    padding: 14px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 16px;
    transition: var(--transition-fast);
    width: 48px;
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    min-width: 44px;
    min-height: 44px;
}

#audio-record-btn:hover:not(:disabled) {
    background: #c82333;
    transform: scale(1.05);
}

#audio-record-btn.recording {
    background: var(--audio-recording-color);
    animation: recordingPulse 1.5s ease-in-out infinite;
}

#audio-record-btn.recording::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 12px;
    height: 12px;
    background: white;
    border-radius: 2px;
}

#audio-record-btn.recording .fa-microphone {
    display: none;
}

#audio-record-btn:disabled {
    background: #6c757d;
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

#audio-record-btn.no-permission {
    opacity: 0.3;
    cursor: not-allowed;
}

@keyframes recordingPulse {
    0%, 100% {
        box-shadow: 0 0 0 0 rgba(255, 23, 68, 0.7);
    }
    50% {
        box-shadow: 0 0 0 10px rgba(255, 23, 68, 0);
    }
}

/* Recording Status */
.recording-status {
    display: none;
    align-items: center;
    gap: 8px;
    color: var(--audio-recording-color);
    font-size: 12px;
    font-weight: 500;
    padding: 8px 0;
}

.recording-status.active {
    display: flex;
}

.recording-indicator {
    width: 8px;
    height: 8px;
    background: var(--audio-recording-color);
    border-radius: 50%;
    animation: recordingBlink 1s ease-in-out infinite;
}

@keyframes recordingBlink {
    0%, 50% { opacity: 1; }
    51%, 100% { opacity: 0.3; }
}

.recording-timer {
    font-family: 'Courier New', monospace;
}

#chat-widget-send {
    background: #84ba3e;
    color: #ffffff;
    border: none;
    padding: 14px 20px;
    border-radius: 22px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
    transition: var(--transition-fast);
    min-width: 70px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    white-space: nowrap;
}

#chat-widget-send:hover:not(:disabled) {
    background: #84ba3e;
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(132, 186, 62, 0.3);
}

#chat-widget-send:focus {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}

#chat-widget-send:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

/* Popup Menu für Chat-Management */
#chat-menu {
    position: fixed;
    top: 0;
    right: 0;
    width: 320px;
    height: 100vh;
    height: 100svh;
    background: var(--widget-bg-color);
    border-left: 1px solid #e5e7eb;
    box-shadow: -4px 0 16px rgba(0, 0, 0, 0.1);
    transform: translateX(100%);
    transition: transform var(--transition-smooth);
    display: flex;
    flex-direction: column;
    z-index: 1002;
}

#chat-menu.open {
    transform: translateX(0);
}

#chat-menu-header {
    background: var(--primary-color);
    color: white;
    padding: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

#chat-menu-title {
    font-weight: 600;
    font-size: 16px;
}

#chat-menu-close {
    background: none;
    border: none;
    color: white;
    font-size: 18px;
    cursor: pointer;
    padding: 8px;
    border-radius: 6px;
    transition: var(--transition-fast);
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
}

#chat-menu-close:hover {
    background: rgba(255, 255, 255, 0.2);
}

#chat-menu-body {
    flex: 1;
    padding: 16px;
    overflow-y: auto;
}

#chat-menu-new-chat {
    background: var(--primary-color);
    color: white;
    border: none;
    padding: 12px 16px;
    border-radius: 8px;
    font-size: 14px;
    cursor: pointer;
    transition: var(--transition-fast);
    width: 100%;
    margin-bottom: 16px;
    font-weight: 500;
}

#chat-menu-new-chat:hover {
    background: var(--secondary-color);
    transform: translateY(-1px);
}

/* Loading Indicator */
.loading-indicator {
    display: flex;
    align-items: center;
    gap: 10px;
    color: #666;
    font-size: 14px;
    padding: 12px 16px;
    font-style: italic;
}

.loading-dots {
    display: flex;
    gap: 4px;
}

.loading-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--primary-color);
    animation: loadingBounce 1.4s ease-in-out infinite both;
}

.loading-dot:nth-child(1) { animation-delay: -0.32s; }
.loading-dot:nth-child(2) { animation-delay: -0.16s; }

@keyframes loadingBounce {
    0%, 80%, 100% {
        transform: scale(0.7);
        opacity: 0.5;
    }
    40% {
        transform: scale(1.1);
        opacity: 1;
    }
}

/* Enhanced Status Messages */
.error-message {
    background: #fef2f2;
    color: #dc2626;
    border-left: 4px solid #dc2626;
    padding: 14px 18px;
    border-radius: 8px;
    font-size: 14px;
    margin: 8px 0;
    animation: slideInError var(--transition-smooth);
    box-shadow: var(--shadow-light);
}

.offline-notice {
    background: #fef3cd;
    color: #856404;
    border-left: 4px solid #f59e0b;
    padding: 12px 16px;
    border-radius: 8px;
    font-size: 13px;
    text-align: center;
    margin: 8px 0;
    animation: slideIn var(--transition-smooth);
    box-shadow: var(--shadow-light);
}

.success-message {
    background: #f0fdf4;
    color: #166534;
    border-left: 4px solid #22c55e;
    padding: 12px 16px;
    border-radius: 8px;
    font-size: 13px;
    margin: 8px 0;
    animation: slideIn var(--transition-smooth);
}

.audio-error-message {
    background: #fef2f2;
    color: #dc2626;
    border-left: 4px solid #dc2626;
    padding: 10px 14px;
    border-radius: 6px;
    font-size: 12px;
    margin: 4px 0;
    display: flex;
    align-items: center;
    gap: 8px;
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(-15px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes slideInError {
    from {
        opacity: 0;
        transform: translateX(-15px);
    }
    to {
        opacity: 1;
        transform: translateX(0);
    }
}

/* Retry Button */
.retry-button {
    background: var(--primary-color);
    color: white;
    border: none;
    padding: 6px 12px;
    border-radius: 6px;
    font-size: 12px;
    cursor: pointer;
    margin-left: 10px;
    transition: var(--transition-fast);
    font-weight: 500;
}

.retry-button:hover {
    background: var(--secondary-color);
    transform: translateY(-1px);
}

/* Responsive Design - Mobile */
@media (max-width: 768px) {
    body {
        flex-direction: column;
    }
    
    #chat-sidebar {
        width: 100%;
        height: 200px;
        border-right: none;
        border-bottom: 1px solid #e5e7eb;
    }
    
    #chat-sidebar-header {
        padding: 12px 16px;
        font-size: 13px;
    }
    
    #chat-list {
        padding: 4px;
    }
    
    .chat-item {
        padding: 8px;
    }
    
    .chat-item-title {
        font-size: 12px;
    }
    
    #chat-menu {
        width: 100%;
    }
}

@media (max-width: 480px) {
    :root {
        --chat-sidebar-width: 100%;
    }

    #chat-widget-header {
        padding: 16px;
    }

    #chat-widget-logo {
        width: 44px;
        height: 44px;
        font-size: 20px;
    }

    #chat-widget-title {
        font-size: 15px;
    }

    .chat-message {
        max-width: 90%;
        font-size: 13px;
        padding: 12px 16px;
    }

    #chat-widget-input {
        font-size: 16px;
        padding: 12px 16px;
    }

    #chat-widget-send {
        padding: 12px 16px;
        min-width: 60px;
    }

    #audio-record-btn {
        width: 44px;
        height: 44px;
        font-size: 14px;
    }
    
    #chat-widget-controls {
        flex-direction: column;
        align-items: stretch;
        gap: 8px;
    }
    
    .response-mode-toggle {
        justify-content: center;
    }
}

/* Extra Small Devices */
@media (max-width: 360px) {
    #chat-widget-header {
        padding: 12px;
    }

    #chat-widget-body {
        padding: 16px;
    }

    #chat-widget-footer {
        padding: 12px;
        padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
    }

    .chat-message {
        font-size: 12px;
        padding: 10px 14px;
    }
}

/* Tablet Styles */
@media (min-width: 768px) and (max-width: 1024px) {
    #chat-sidebar {
        width: 250px;
    }
}

/* Large Screens */
@media (min-width: 1200px) {
    #chat-sidebar {
        width: 300px;
    }
}

/* Touch Device Optimizations */
@media (pointer: coarse) {
    #menu-button,
    #chat-widget-send,
    #audio-record-btn,
    #chat-menu-close,
    .retry-button,
    .chat-item,
    .chat-item-delete,
    .audio-play-btn {
        min-width: 44px;
        min-height: 44px;
    }

    .chat-message {
        padding: 16px 20px;
    }
    
    .toggle-switch {
        width: 44px;
        height: 24px;
    }
    
    .toggle-slider:before {
        height: 20px;
        width: 20px;
    }
    
    input:checked + .toggle-slider:before {
        transform: translateX(20px);
    }
}

/* Print Styles */
@media print {
    #chat-widget-container,
    #chat-sidebar,
    #chat-menu {
        display: none !important;
    }
}

/* Focus-visible für bessere Accessibility */
@supports selector(:focus-visible) {
    #chat-widget-send:focus:not(:focus-visible),
    #chat-widget-input:focus:not(:focus-visible),
    #audio-record-btn:focus:not(:focus-visible),
    #menu-button:focus:not(:focus-visible),
    #chat-menu-close:focus:not(:focus-visible),
    .chat-item:focus:not(:focus-visible),
    .audio-play-btn:focus:not(:focus-visible) {
        outline: none;
    }
}

/* Animation für Chat-Wechsel */
@keyframes chatTransition {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.chat-transition {
    animation: chatTransition 0.3s ease-out;
}

/* Menu Overlay für Mobile */
.menu-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1001;
}

.menu-overlay.active {
    display: block;
}