:root {
    --ink: #1B1C1C;
    --muted: #44474A;
    --sidebar-bg: #EFEDED;
    --page-bg: #F9F9F9;
    --border: #C5C6CB;
    --gray-ui: #A0A5AC;
    --gray-ui-hover: #898f9c;
    --user-bubble: #DCE0E6;
    --avatar-bg: #5A5F65;
    --white: #FFFFFF;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    font-family: 'Lato', Arial, sans-serif;
    color: var(--ink);
    background: var(--white);
}

body {
    display: block; /* Standardmäßig versteckt bis Auth bestätigt */
    flex-direction: column;
}

body.authenticated {
    display: block;
}

body.chatbot-page {
    display: none;
}

body.chatbot-page.authenticated {
    display: block;
}

/* ============ APP LAYOUT ============ */
.app-wrapper {
    display: flex;
    height: 100vh;
    min-height: 0;
    border-bottom: 1px solid var(--border);
    position: relative;
}

/* ============ MOBILE TOPBAR ============ */
.mobile-topbar {
    display: none;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    background: var(--sidebar-bg);
    border-bottom: 1px solid var(--border);
}

.burger-btn {
    width: 40px;
    height: 40px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    cursor: pointer;
    color: var(--ink);
    padding: 0;
}
.burger-btn svg { width: 24px; height: 24px; }
.burger-btn:hover { background: rgba(0,0,0,0.05); }
.mobile-topbar .mobile-logo img { max-height: 28px; display: block; }

/* ============ SIDEBAR ============ */
.sidebar {
    width: 288px;
    flex-shrink: 0;
    background: var(--sidebar-bg);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
}

.sidebar-logo { padding: 20px 20px 0; }
.sidebar-logo a { display: block; }
.sidebar-logo img { max-width: 160px; display: block; }

.new-chat-wrap { padding: 20px 20px 16px; }

.new-chat-btn {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    background: var(--gray-ui);
    border: none;
    color: var(--white);
    padding: 12px;
    font-family: inherit;
    font-size: 14px;
    font-weight: 700;
    letter-spacing: .7px;
    text-transform: uppercase;
    cursor: pointer;
    border-radius: 0;
}
.new-chat-btn:hover { background: var(--gray-ui-hover); }
.new-chat-btn svg { width: 16px; height: 16px; flex-shrink: 0; }

.chat-history {
    flex: 1;
    overflow-y: auto;
    padding-bottom: 8px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.history-label {
    padding: 0 16px 8px 20px;
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0;
    color: var(--muted);
    text-transform: uppercase;
    opacity: .6;
    margin-top: 16px;
}

.history-label:first-child {
    margin-top: 0;
}

.chat-item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    background: transparent;
    border: none;
    text-align: left;
    padding: 10px 16px 10px 20px;
    font-family: inherit;
    font-size: 14px;
    color: var(--muted);
    cursor: pointer;
    line-height: 24px;
}

.chat-item svg {
    width: 14px;
    height: 14px;
    flex-shrink: 0;
    opacity: .6;
}
.chat-item:hover,
.chat-item.active { background: rgba(0,0,0,0.05); color: var(--ink); }

.chat-item span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.history-empty {
    padding: 12px 16px 12px 20px;
    font-size: 13px;
    color: var(--muted);
    opacity: .6;
}

.profile-settings {
    border-top: 1px solid var(--border);
    padding: 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.profile-info { display: flex; align-items: center; gap: 12px; }
.avatar {
    width: 40px;
    height: 40px;
    background: var(--avatar-bg);
    color: var(--white);
    font-size: 16px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}
.profile-name { font-size: 14px; font-weight: 700; color: var(--ink); line-height: 24px; }
.profile-role { font-size: 12px; color: var(--muted); line-height: 18px; }

.settings-btn {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    cursor: pointer;
    color: var(--avatar-bg);
}
.settings-btn:hover { background: rgba(0,0,0,0.05); }
.settings-btn svg { width: 20px; height: 20px; }

/* ============ CHAT AREA ============ */
.chat-area {
    flex: 1;
    background: var(--page-bg);
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.chat-feed {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.chat-feed-inner {
    width: 100%;
    max-width: 793px;
    padding: 30px 0 40px 0;
    display: flex;
    flex-direction: column;
    gap: 32px;
    margin-top: auto;
}

.system-greeting {
    text-align: center;
    opacity: .5;
    padding-bottom: 40px;
}
.system-greeting h1 {
    font-size: 24px;
    font-weight: 700;
    letter-spacing: 0;
    line-height: 32px;
    margin: 0;
    color: var(--ink);
}

.message-row { display: flex; }
.message-row.user { justify-content: flex-end; }
.message-row.ai { justify-content: flex-start; }

.bubble {
    max-width: 627px;
    padding: 16px;
    font-size: 14px;
    line-height: 24px;
}
.bubble.user { background: var(--user-bubble); color: var(--ink); }
.bubble.ai { background: var(--white); border: 1px solid var(--border); color: var(--ink); }

.bubble.ai,
.bubble.ai .markdown-content {
    text-align: left;
}

.bubble.ai .markdown-content p,
.bubble.ai .markdown-content li,
.bubble.ai .markdown-content h1,
.bubble.ai .markdown-content h2,
.bubble.ai .markdown-content h3 {
    text-align: left;
}

.bubble .timestamp {
    display: block;
    margin-top: 8px;
    font-size: 10px;
    line-height: 15px;
    color: var(--muted);
    opacity: .6;
}
.bubble.user .timestamp { text-align: right; }

.file-chip {
    display: flex;
    align-items: center;
    gap: 12px;
    background: var(--page-bg);
    border: 1px solid var(--border);
    padding: 8px 12px;
    margin-top: 12px;
}
.file-chip svg { width: 16px; height: 20px; flex-shrink: 0; color: var(--avatar-bg); }
.file-chip span { font-size: 12px; font-weight: 700; letter-spacing: .6px; }

.typing-indicator {
    width: 70px;
    height: 34px;
    background: var(--white);
    border: 1px solid var(--border);
    border-radius: 17px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
}
.typing-indicator span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--muted);
    opacity: .5;
    animation: bounce 1.2s infinite ease-in-out;
}
.typing-indicator span:nth-child(2) { animation-delay: .15s; }
.typing-indicator span:nth-child(3) { animation-delay: .3s; }

@keyframes bounce {
    0%, 60%, 100% { transform: translateY(0); opacity: .5; }
    30% { transform: translateY(-4px); opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
    .typing-indicator span { animation: none; opacity: .8; }
}

/* ============ INPUT AREA ============ */
.input-area-wrap {
    display: flex;
    justify-content: center;
    padding: 20px 32px 30px;
    border-top: 1px solid var(--border);
    background: var(--page-bg);
}
.input-area { width: 100%; max-width: 793px; }

.textarea-wrapper { position: relative; }

.chat-textarea {
    width: 100%;
    min-height: 56px;
    max-height: 160px;
    background: var(--gray-ui);
    border: none;
    color: var(--white);
    font-family: inherit;
    font-size: 14px;
    line-height: 24px;
    padding: 16px 64px 16px 16px;
    resize: none;
    border-radius: 0;
}
.chat-textarea::placeholder { color: rgba(255,255,255,0.6); }
.chat-textarea:focus { outline: 2px solid var(--ink); outline-offset: -2px; }

.send-btn {
    position: absolute;
    right: 16px;
    top: 50%;
    transform: translateY(-50%);
    background: transparent;
    border: none;
    cursor: pointer;
    color: var(--white);
    padding: 4px;
    display: flex;
}
.send-btn svg { width: 19px; height: 16px; }
.send-btn:disabled { opacity: .4; cursor: default; }

.disclaimer {
    text-align: center;
    font-size: 10px;
    line-height: 15px;
    color: var(--muted);
    opacity: .6;
    margin: 8px 0 0;
}

/* ============ SIDEBAR OVERLAY (Mobile) ============ */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 999;
}

/* ============ RESPONSIVE ============ */
@media (max-width: 860px) {
    .app-wrapper {
        flex-direction: column;
        height: auto;
        min-height: 100vh;
        min-height: 100dvh; /* korrigiert die Höhe bei mobilen Adressleisten */
        position: relative;
    }
    .mobile-topbar { display: flex; }

    .sidebar {
        position: fixed;
        top: 0;
        left: -300px;
        height: 100%;
        width: 288px;
        max-width: 85vw;
        z-index: 1000;
        flex-direction: column;
        flex-wrap: nowrap;
        transition: left .28s ease;
        box-shadow: 2px 0 12px rgba(0,0,0,0.15);
    }
    .sidebar.open { left: 0; }
    .sidebar-overlay.open { display: block; }

    .chat-history { display: flex; }
    .bubble { max-width: 85%; }
}

.sidebar-logo {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.sidebar-collapse-btn,
.sidebar-expand-btn {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    cursor: pointer;
    color: var(--muted);
    flex-shrink: 0;
}
.sidebar-collapse-btn svg,
.sidebar-expand-btn svg { width: 18px; height: 18px; }
.sidebar-collapse-btn:hover,
.sidebar-expand-btn:hover { background: rgba(0,0,0,0.06); color: var(--ink); }

.sidebar-expand-btn {
    position: fixed;
    top: 20px;
    left: 20px;
    z-index: 500;
    background: var(--sidebar-bg);
    border: 1px solid var(--border);
}

/* Sidebar collapse-Verhalten (Desktop) */
.sidebar {
    transition: width .2s ease, opacity .15s ease;
    overflow: hidden;
}

.app-wrapper.sidebar-collapsed .sidebar {
    width: 0;
    opacity: 0;
    border-right: none;
}

/* Auf Mobile ist die Collapse-Logik irrelevant, dort regelt .open alles */
@media (max-width: 860px) {
    .sidebar-collapse-btn { display: none; }
    .sidebar-expand-btn { display: none; }
}

.sidebar-expand-btn[hidden] {
    display: none;
}

.chat-item {
    position: relative;
    padding-right: 38px; /* Platz für die drei Punkte */
}

.chat-item-menu-btn {
    position: absolute;
    right: 6px;
    top: 50%;
    transform: translateY(-50%);
    width: 26px;
    height: 26px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    cursor: pointer;
    color: var(--muted);
    opacity: 0;
    transition: opacity .12s ease;
}
.chat-item:hover .chat-item-menu-btn,
.chat-item-menu-btn.active {
    opacity: 1;
}
.chat-item-menu-btn:hover {
    background: rgba(0,0,0,0.08);
    color: var(--ink);
}
.chat-item-menu-btn svg { width: 16px; height: 16px; }

.chat-item-dropdown {
    position: fixed;
    min-width: 168px;
    background: var(--white);
    border: 1px solid var(--border);
    box-shadow: 0 4px 16px rgba(0,0,0,0.14);
    z-index: 1100;
    padding: 4px;
}
.chat-item-dropdown button {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 9px 10px;
    background: transparent;
    border: none;
    font-family: inherit;
    font-size: 13px;
    color: var(--ink);
    cursor: pointer;
    text-align: left;
}
.chat-item-dropdown button:hover { background: rgba(0,0,0,0.06); }
.chat-item-dropdown button.danger { color: #B3261E; }
.chat-item-dropdown svg { width: 14px; height: 14px; flex-shrink: 0; }

.chat-item-rename-input {
    flex: 1;
    min-width: 0;
    font-family: inherit;
    font-size: 14px;
    color: var(--ink);
    background: var(--white);
    border: 1px solid var(--gray-ui);
    padding: 3px 6px;
}

.chat-item-star {
    display: none;
    color: #C9A227;
    flex-shrink: 0;
}
.chat-item-star svg { width: 14px; height: 14px; }
.chat-item.is-favorite .chat-item-star {
    display: flex;
}
.chat-item.is-favorite span:not(.chat-item-star) {
    overflow: hidden;
    text-overflow: ellipsis;
}

/* ============ SETTINGS DROPDOWN ============ */
.settings-wrap {
    position: relative;
}

.settings-dropdown {
    position: absolute;
    bottom: calc(100% + 8px);
    right: 0;
    min-width: 200px;
    background: var(--white);
    border: 1px solid var(--border);
    box-shadow: 0 4px 16px rgba(0,0,0,0.14);
    z-index: 1100;
    padding: 4px;
}

.settings-dropdown[hidden] {
    display: none;
}

.settings-dropdown button {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 9px 10px;
    background: transparent;
    border: none;
    font-family: inherit;
    font-size: 13px;
    color: var(--ink);
    cursor: pointer;
    text-align: left;
}

.settings-dropdown button[hidden] {
    display: none !important;
}

.settings-dropdown button:hover { background: rgba(0,0,0,0.06); }
.settings-dropdown button.danger { color: #B3261E; }
.settings-dropdown svg { width: 14px; height: 14px; flex-shrink: 0; }

/* ============ FILE CHIP (jetzt als Button, klickbar) ============ */
.sources-container {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.sources-container:empty {
    display: none;
}

.file-chip {
    display: flex;
    align-items: center;
    gap: 12px;
    background: var(--page-bg);
    border: 1px solid var(--border);
    padding: 8px 12px;
    margin-top: 12px;
    font-family: inherit;
    cursor: pointer;
    text-align: left;
    width: 100%;
    max-width: 320px;
    transition: background .12s ease, border-color .12s ease;
}
.file-chip:hover {
    background: var(--white);
    border-color: var(--avatar-bg);
}
.file-chip svg { width: 16px; height: 20px; flex-shrink: 0; color: var(--avatar-bg); }
.file-chip span { font-size: 12px; font-weight: 700; letter-spacing: .6px; color: var(--ink); }

/* ============ PREVIEW MODAL ============ */
.preview-modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
}
.preview-modal-overlay[hidden] { display: none; }

.preview-modal {
    background: var(--white);
    width: 100%;
    max-width: 900px;
    height: 100%;
    max-height: 85vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 8px 40px rgba(0,0,0,0.25);
}

.preview-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 18px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.preview-modal-title {
    font-size: 14px;
    font-weight: 700;
    color: var(--ink);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.preview-modal-close {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    cursor: pointer;
    color: var(--muted);
    flex-shrink: 0;
}
.preview-modal-close:hover { background: rgba(0,0,0,0.06); color: var(--ink); }
.preview-modal-close svg { width: 18px; height: 18px; }

.preview-modal-body {
    flex: 1;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--sidebar-bg);
    overflow: auto;
}

.preview-modal-loading {
    font-size: 14px;
    color: var(--muted);
}

.preview-modal-pdf {
    width: 100%;
    height: 100%;
    border: none;
}

.preview-modal-image {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

.preview-modal-fallback {
    text-align: center;
    padding: 32px;
}
.preview-modal-fallback p {
    color: var(--muted);
    font-size: 14px;
    margin-bottom: 16px;
}
.preview-modal-download-btn {
    display: inline-block;
    background: var(--gray-ui);
    color: var(--white);
    text-decoration: none;
    padding: 10px 20px;
    font-size: 13px;
    font-weight: 700;
    letter-spacing: .5px;
    text-transform: uppercase;
}
.preview-modal-download-btn:hover { background: var(--gray-ui-hover); }

@media (max-width: 860px) {
    .preview-modal-overlay { padding: 0; }
    .preview-modal { max-width: 100%; max-height: 100%; height: 100%; }
    .file-chip { max-width: 100%; }
}