/**
 * JEANNIE Chat Widget V5 - Styles
 * Embedded Only | Responsive | iOS Safari Safe
 */

/* ============================================================
   CSS VARIABLES
   ============================================================ */
:root {
    --primary: #3f893a;
    --secondary: #2d6b29;
    --bg: #3e3d40;
    --fg: #e5e5e5;
    --msg-bot-bg: #84ba3e;
    --msg-bot-fg: #ffffff;
    --msg-user-bg: #2a2a2d;
    --msg-user-fg: #e5e5e5;
    --border: #555555;
    --sidebar-bg: #f8f9fa;
    --radius: 12px;
    --shadow: 0 4px 16px rgba(0,0,0,0.1);
    --transition: 0.2s ease;
    --red: #dc3545;
    --red-bright: #ff1744;
}


@media (prefers-reduced-motion: reduce) {
    * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

/* ============================================================
   RESET & BASE
   ============================================================ */
*, *::before, *::after { box-sizing: border-box; }

html {
    height: 100vh;
    height: 100dvh;
}

body {
    margin: 0;
    padding: 0;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    font-family: 'Montserrat', -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
    background: var(--bg);
    color: var(--fg);
}

/* iOS Safari: svh verhindert Layout-Sprung durch Toolbar */
@supports (-webkit-touch-callout: none) {
    html, body { height: 100svh; }
}

/* ============================================================
   CHAT WIDGET — MAIN LAYOUT
   ============================================================ */
#chat-widget {
    display: flex;
    flex-direction: row;
    height: 100%;
    width: 100%;
    background: var(--bg);
    overflow: hidden;
}

#chat-main {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: hidden;
}

/* ============================================================
   SIDEBAR
   ============================================================ */
#chat-sidebar {
    width: 60px;
    background: #000000;
    border-right: 1px solid #555;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 12px;
    gap: 8px;
    flex-shrink: 0;
    transition: width 0.25s ease;
    overflow: hidden;
}

#chat-sidebar.expanded {
    width: 220px;
    align-items: flex-start;
    padding: 12px 10px;
}

/* Toggle Button — optisch hervorgehoben */
#sidebar-toggle-btn {
    color: #c8c8c8;
    flex-shrink: 0;
}

#sidebar-toggle-btn:hover {
    background: rgba(255,255,255,0.15);
    color: #ffffff;
}

/* Trennlinie nach Toggle-Button */
#chat-sidebar.expanded #sidebar-toggle-btn {
    width: 100%;
    justify-content: flex-start;
    padding: 0 8px;
    margin-bottom: 4px;
    padding-bottom: 12px;
    border-bottom: 1px solid #555;
}

.sidebar-btn {
    width: 40px;
    height: 40px;
    border-radius: 8px;
    border: none;
    background: none;
    color: #b0b0b0;
    font-size: 16px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
    flex-shrink: 0;
}

.sidebar-btn:hover {
    background: rgba(255,255,255,0.1);
    color: #e0e0e0;
}

#chat-sidebar.expanded .sidebar-btn {
    width: 100%;
    justify-content: flex-start;
    gap: 10px;
    padding: 0 8px;
    font-size: 13px;
}

#chat-sidebar.expanded .sidebar-btn::after {
    content: attr(data-label);
}

/* Sidebar Chat Liste */
#sidebar-chat-list {
    display: none;
    flex-direction: column;
    gap: 4px;
    width: 100%;
    overflow-y: auto;
    flex: 1;
    margin-top: 4px;
}

#chat-sidebar.expanded #sidebar-chat-list {
    display: flex;
}

.sidebar-chat-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    border-radius: 8px;
    cursor: pointer;
    border: 1px solid transparent;
    transition: var(--transition);
    width: 100%;
}

.sidebar-chat-item:hover { background: rgba(255,255,255,0.08); }
.sidebar-chat-item.active { background: rgba(255,255,255,0.15); }

.sidebar-chat-item-title {
    font-size: 12px;
    color: #d0d0d0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
}

.sidebar-chat-item-delete {
    background: none;
    border: none;
    color: #888;
    font-size: 11px;
    cursor: pointer;
    padding: 2px 4px;
    border-radius: 4px;
    opacity: 0;
    transition: var(--transition);
    flex-shrink: 0;
}

.sidebar-chat-item:hover .sidebar-chat-item-delete { opacity: 1; }
.sidebar-chat-item-delete:hover { color: #ff6b6b; }

/* ============================================================
   HEADER
   ============================================================ */
#chat-header {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    padding: 16px 20px;
    background: #000000;
    color: white;
    flex-shrink: 0;
    min-height: 64px;
}

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

#chat-logo {
    width: auto;
    height: 44px;
    max-width: 200px;
    border-radius: 8px;
    background: transparent;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 22px;
    flex-shrink: 0;
    overflow: hidden;
}

#chat-logo img {
    width: auto;
    height: 100%;
    object-fit: contain;
    border-radius: 6px;
}

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

#menu-btn {
    background: rgba(255,255,255,0.15);
    border: none;
    color: white;
    width: 40px;
    height: 40px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 16px;
    display: none; /* Desktop: versteckt */
    align-items: center;
    justify-content: center;
    transition: var(--transition);
    flex-shrink: 0;
    margin-right: 12px;
}

@media (max-width: 480px) {
    #menu-btn { display: flex; }
}

/* ============================================================
   CHAT BODY
   ============================================================ */
#chat-body {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    scroll-behavior: smooth;
    max-width: 760px;
    width: 100%;
    align-self: center;
}

#chat-body::-webkit-scrollbar { width: 5px; }
#chat-body::-webkit-scrollbar-track { background: transparent; }
#chat-body::-webkit-scrollbar-thumb { background: rgba(0,0,0,0.15); border-radius: 3px; }

/* ============================================================
   MESSAGES
   ============================================================ */
.chat-msg {
    padding: 12px 16px;
    border-radius: var(--radius);
    font-size: 14px;
    line-height: 1.55;
    word-break: break-word;
    max-width: 82%;
    animation: msgIn 0.25s ease-out;
    box-shadow: var(--shadow);
}

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

.chat-msg.user {
    background: var(--msg-user-bg);
    color: var(--msg-user-fg);
    align-self: flex-end;
    margin-left: auto;
}

.chat-msg.bot,
.chat-msg.welcome {
    background: var(--msg-bot-bg);
    color: var(--msg-bot-fg);
    align-self: flex-start;
}

.chat-msg.bot p, .chat-msg.welcome p { margin: 0 0 6px; }
.chat-msg.bot p:last-child, .chat-msg.welcome p:last-child { margin-bottom: 0; }
.chat-msg.bot ul, .chat-msg.bot ol { padding-left: 18px; margin: 6px 0; }
.chat-msg.bot li { margin-bottom: 3px; }
.chat-msg.bot a { color: white; text-decoration: underline; }
.chat-msg.bot strong { font-weight: 600; }
.chat-msg.bot em { font-style: italic; }

/* Audio Message */
.chat-msg.audio-msg {
    display: flex;
    flex-direction: column;
    gap: 8px;
    max-width: 82%;
    background: var(--msg-bot-bg);
    color: var(--msg-bot-fg);
    align-self: flex-start;
}

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

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

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

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

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

.audio-time { font-size: 11px; opacity: 0.8; flex-shrink: 0; }
.audio-transcript { font-size: 12px; opacity: 0.85; font-style: italic; }

/* Loading */
.loading-indicator {
    display: flex;
    align-items: center;
    gap: 10px;
    color: #888;
    font-size: 13px;
    font-style: italic;
    padding: 4px 0;
}

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

.dots div {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--primary);
    animation: dotBounce 1.4s ease-in-out infinite;
}

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

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

/* Error */
.error-msg {
    background: #fef2f2;
    color: #dc2626;
    border-left: 3px solid #dc2626;
    padding: 10px 14px;
    border-radius: 8px;
    font-size: 13px;
    align-self: stretch;
}

.no-chats {
    color: #888;
    font-size: 13px;
    font-style: italic;
    text-align: center;
    padding: 16px 0;
}

/* ============================================================
   FOOTER
   ============================================================ */
#chat-footer {
    flex-shrink: 0;
    padding: 12px 16px;
    padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
    border-top: 1px solid var(--border);
    background: #000000;
    display: flex;
    flex-direction: column;
    gap: 10px;
    align-items: center;
}

#chat-controls {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 2px;
    max-width: 760px;
    width: 100%;
}

#chat-input-row {
    display: flex;
    gap: 8px;
    align-items: flex-end;
    max-width: 760px;
    width: 100%;
}

#chat-disclaimer {
    font-size: 11px;
    color: #ffffff;
    text-align: center;
    max-width: 760px;
    width: 100%;
}

/* TTS Toggle */
.tts-toggle {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 12px;
    color: #ffffff;
}

.toggle-switch {
    position: relative;
    display: inline-block;
    width: 36px;
    height: 20px;
    flex-shrink: 0;
}

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

.toggle-slider {
    position: absolute;
    inset: 0;
    background: #ccc;
    border-radius: 20px;
    cursor: pointer;
    transition: var(--transition);
}

.toggle-slider::before {
    content: '';
    position: absolute;
    width: 14px;
    height: 14px;
    left: 3px;
    top: 3px;
    background: white;
    border-radius: 50%;
    transition: var(--transition);
}

input:checked + .toggle-slider { background: var(--primary); }
input:checked + .toggle-slider::before { transform: translateX(16px); }

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

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

.recording-dot {
    width: 8px;
    height: 8px;
    background: var(--red-bright);
    border-radius: 50%;
    animation: blink 1s ease-in-out infinite;
}

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

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

#chat-input {
    flex: 1;
    padding: 12px 16px;
    border: 2px solid var(--border);
    border-radius: 22px;
    outline: none;
    font-size: 14px;
    font-family: inherit;
    line-height: 1.4;
    resize: none;
    min-height: 46px;
    max-height: 120px;
    background: var(--bg);
    color: var(--fg);
    transition: border-color var(--transition);
    overflow-y: hidden;
}

#chat-input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(63,137,58,0.12); }
#chat-input::placeholder { color: #aaa; }
#chat-input::-webkit-scrollbar { width: 5px; }
#chat-input::-webkit-scrollbar-track { background: transparent; }
#chat-input::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.15); border-radius: 3px; }

/* Mic Button */
#mic-btn {
    width: 46px;
    height: 46px;
    border-radius: 50%;
    border: none;
    background: var(--red);
    color: white;
    font-size: 16px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
    flex-shrink: 0;
    position: relative;
    user-select: none;
    -webkit-user-select: none;
}

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

#mic-btn.recording {
    background: var(--red-bright);
    animation: pulse 1.5s ease-in-out infinite;
}

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

#mic-btn.recording::after {
    content: '';
    position: absolute;
    width: 14px;
    height: 14px;
    background: white;
    border-radius: 2px;
}

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

#mic-btn:disabled, #mic-btn.no-permission {
    background: #aaa;
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

/* Send Button */
#send-btn {
    width: 46px;
    height: 46px;
    border-radius: 50%;
    border: none;
    background: var(--msg-bot-bg);
    color: white;
    font-size: 16px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
    flex-shrink: 0;
}

#send-btn:hover:not(:disabled) { background: var(--primary); transform: scale(1.05); }
#send-btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

/* ============================================================
   SLIDE-IN MENU (Mobile only)
   ============================================================ */
#chat-menu {
    position: fixed;
    top: 0;
    right: 0;
    width: 300px;
    height: 100%;
    height: 100dvh;
    background: var(--bg);
    border-left: 1px solid var(--border);
    box-shadow: -4px 0 20px rgba(0,0,0,0.12);
    transform: translateX(100%);
    transition: transform 0.28s ease-out;
    display: flex;
    flex-direction: column;
    z-index: 200;
}

@supports (-webkit-touch-callout: none) {
    #chat-menu { height: 100svh; }
}

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

#chat-menu-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    background: #000000;
    color: white;
    font-weight: 600;
    font-size: 15px;
    flex-shrink: 0;
}

#chat-menu-close {
    background: rgba(255,255,255,0.15);
    border: none;
    color: white;
    width: 36px;
    height: 36px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
}

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

#chat-menu-body {
    flex: 1;
    padding: 16px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

#new-chat-btn {
    background: #000000;
    color: white;
    border: none;
    padding: 11px 16px;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    transition: var(--transition);
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

#new-chat-btn:hover { background: #222222; }

#chat-list { display: flex; flex-direction: column; gap: 4px; }

.chat-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    border-radius: 8px;
    cursor: pointer;
    border: 1px solid transparent;
    transition: var(--transition);
}

.chat-item:hover { background: var(--sidebar-bg); border-color: var(--border); }
.chat-item.active { background: #000000; color: white; }

.chat-item-info { flex: 1; min-width: 0; }

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

.chat-item-date { font-size: 11px; opacity: 0.65; margin-top: 2px; }

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

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

/* Overlay */
.menu-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.45);
    z-index: 199;
}

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

/* ============================================================
   RESPONSIVE
   ============================================================ */
@media (max-width: 480px) {
    .chat-msg { max-width: 92%; font-size: 13px; }
    #chat-input { font-size: 16px; }
    #chat-header { padding: 12px 16px; }
    #chat-body { padding: 14px; }
    #chat-menu { width: 100%; }
    #chat-sidebar { display: none; }
}

@media (max-width: 360px) {
    #chat-footer { padding: 10px 12px; padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px)); }
    #mic-btn, #send-btn { width: 42px; height: 42px; }
}

/* Touch targets */
@media (pointer: coarse) {
    #menu-btn, #mic-btn, #send-btn, #chat-menu-close, #new-chat-btn,
    .chat-item, .chat-item-delete, .play-btn, .sidebar-btn {
        min-width: 44px;
        min-height: 44px;
    }
}

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