:root {
    --primary: #4a6fa5;
    --primary-dark: #345d96;
    --success: #06d6a0;
    --danger: #ef476f;
    --bg-main: #111827;
    --bg-surface: #1f2937;
    --bg-surface-soft: #374151;
    --text-primary: #f3f4f6;
    --text-secondary: #cbd5e1;
    --border: #374151;
    --user-bubble: #4a6fa5;
    --assistant-bubble: #374151;
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --shadow: 0 10px 20px rgba(0, 0, 0, 0.25);
    --scrollbar-track: #1f2937;
    --scrollbar-thumb: #4a6fa5;
    --scrollbar-thumb-hover: #5d83bd;
}

[data-theme="light"] {
    --bg-main: #f5f7fb;
    --bg-surface: #ffffff;
    --bg-surface-soft: #eef2f7;
    --text-primary: #1f2937;
    --text-secondary: #4b5563;
    --border: #e5e7eb;
    --assistant-bubble: #f3f4f6;
    --scrollbar-track: #e5e7eb;
    --scrollbar-thumb: #8aa6d1;
    --scrollbar-thumb-hover: #7397ce;
}

* { box-sizing: border-box; }

html,
body {
    margin: 0;
    padding: 0;
    height: 100%;
    overflow: hidden;
    background: var(--bg-main);
    color: var(--text-primary);
    font-family: 'Inter', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
}

a { color: inherit; }

.hidden {
    display: none !important;
    visibility: hidden !important;
}

.chat-app {
    width: 100%;
    max-width: 1000px;
    margin: 0 auto;
    height: 100vh;
    height: 100dvh;
    max-height: 100vh;
    max-height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--bg-surface);
    box-shadow: var(--shadow);
}

.chat-header {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    padding: 12px 16px;
    background: linear-gradient(135deg, var(--primary) 0%, #204275 100%);
    color: #fff;
}

[data-theme="light"] .chat-header {
    background: linear-gradient(135deg, #6495ed 0%, #4a76a8 100%);
}

.chat-header-left {
    flex: 1 1 auto;
    min-width: 0;
    display: flex;
    align-items: center;
    gap: 10px;
}

.chat-title {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.online-indicator {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--success);
    flex: 0 0 10px;
}

.model-select {
    display: inline-flex;
    align-items: center;
    flex-wrap: nowrap;
    flex: 0 0 auto;
    width: fit-content;
    max-width: fit-content;
    gap: 4px;
    padding: 2px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.15);
    border: 1px solid rgba(255, 255, 255, 0.2);
}

.model-option {
    border: 0;
    cursor: pointer;
    border-radius: 999px;
    font-size: 0.78rem;
    padding: 0.35rem 0.7rem;
    background: transparent;
    color: rgba(255, 255, 255, 0.92);
    white-space: nowrap;
}

.model-option.active {
    background: rgba(255, 255, 255, 0.95);
    color: #1e3a5f;
}

.chat-header-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: 8px;
    min-width: 0;
}

.chat-header-actions > div[style*="width: 5px"] { display: none !important; }

.btn {
    border: 1px solid rgba(255, 255, 255, 0.35);
    border-radius: var(--radius-sm);
    padding: 0.38rem 0.72rem;
    font-size: 0.82rem;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
}

.chat-header .btn-primary,
.chat-header .btn-secondary,
.chat-header .btn-danger {
    background: rgba(255, 255, 255, 0.16);
    color: #fff;
}

.chat-header .btn-danger {
    border-color: rgba(239, 71, 111, 0.55);
    background: rgba(239, 71, 111, 0.2);
}

.user-info,
.user-profile-link {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    text-decoration: none;
}

.user-avatar {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    background: rgba(255, 255, 255, 0.2);
    border: 2px solid rgba(255, 255, 255, 0.3);
}

.chat-menu {
    position: relative;
    display: inline-flex;
}

.chat-options-button {
    width: 32px;
    height: 32px;
    border: 0;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    text-decoration: none;
    background: rgba(255, 255, 255, 0.15);
    color: #fff;
}

.dots { font-size: 20px; line-height: 1; }

.header-action-group {
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

.language-toggle {
    display: inline-flex;
    position: static;
    width: 22px;
    height: 16px;
    text-decoration: none;
}

.lang-flag {
    position: relative;
    width: 22px;
    height: 16px;
    border-radius: 3px;
    overflow: hidden;
    border: 1px solid rgba(255, 255, 255, 0.3);
}

.flag { width: 100%; height: 100%; display: block; }
.flag-standard { width: 22px !important; height: 16px !important; }
.flag-nl { display: flex; flex-direction: column; }
.flag-nl-top { background: #ae1c28; height: 33.33%; }
.flag-nl-middle { background: #fff; height: 33.33%; }
.flag-nl-bottom { background: #21468b; height: 33.33%; }
.flag-us { position: relative; background: #fff; overflow: hidden; }
.flag-us:before { content: ""; position: absolute; inset: 0; background: repeating-linear-gradient(to bottom, #b22234 0 8%, #fff 8% 16%); }
.flag-us:after { content: ""; position: absolute; left: 0; top: 0; width: 40%; height: 54%; background: #3c3b6e; }
.flag-de { display: flex; flex-direction: column; }
.flag-de-top { background: #000; height: 33.33%; }
.flag-de-middle { background: #dd0000; height: 33.33%; }
.flag-de-bottom { background: #ffce00; height: 33.33%; }
.flag-fr { display: flex; }
.flag-fr-left { width: 33.33%; background: #0055a4; }
.flag-fr-middle { width: 33.33%; background: #fff; }
.flag-fr-right { width: 33.33%; background: #ef4135; }
.flag-es { display: flex; flex-direction: column; }
.flag-es-top { height: 25%; background: #c60b1e; }
.flag-es-middle { height: 50%; background: #ffc400; }
.flag-es-bottom { height: 25%; background: #c60b1e; }
.flag-pt { position: relative; background: #ff0000; overflow: hidden; }
.flag-pt:before { content: ""; position: absolute; left: 0; top: 0; width: 40%; height: 100%; background: #006600; }
.flag-pt:after { content: ""; position: absolute; left: 16%; top: 50%; transform: translateY(-50%); width: 24%; height: 50%; border-radius: 50%; background: #ffcc00; }

.theme-switch-wrapper {
    display: inline-flex;
    align-items: center;
    gap: 4px;
}

.theme-switch {
    width: 48px;
    height: 24px;
    position: relative;
    display: inline-block;
}

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

.slider {
    position: absolute;
    inset: 0;
    border-radius: 999px;
    border: 1px solid rgba(255,255,255,0.3);
    background: rgba(0,0,0,0.25);
    cursor: pointer;
}

.slider:before {
    content: "";
    position: absolute;
    left: 3px;
    bottom: 2px;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: #fff;
    transition: transform 0.25s ease;
}

input:checked + .slider:before { transform: translateX(24px); }

.chat-container {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.chat-messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: var(--bg-main);
    scrollbar-width: thin;
    scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track);
}

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

.chat-messages::-webkit-scrollbar-track {
    background: var(--scrollbar-track);
    border-radius: 999px;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: var(--scrollbar-thumb);
    border-radius: 999px;
    border: 2px solid var(--scrollbar-track);
}

.chat-messages::-webkit-scrollbar-thumb:hover {
    background: var(--scrollbar-thumb-hover);
}

.code-block {
    scrollbar-width: thin;
    scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track);
}

.code-block::-webkit-scrollbar {
    width: 9px;
    height: 9px;
}

.code-block::-webkit-scrollbar-track {
    background: var(--scrollbar-track);
}

.code-block::-webkit-scrollbar-thumb {
    background: var(--scrollbar-thumb);
    border-radius: 999px;
    border: 2px solid var(--scrollbar-track);
}

.message { display: flex; max-width: 85%; }
.message.user { margin-left: auto; }
.message.assistant { margin-right: auto; }

.message-content {
    background: var(--assistant-bubble);
    color: var(--text-primary);
    padding: 12px;
    border-radius: var(--radius-lg);
    border-bottom-left-radius: 6px;
}

.user .message-content {
    background: var(--user-bubble);
    color: #fff;
    border-bottom-right-radius: 6px;
    border-bottom-left-radius: var(--radius-lg);
}

.message-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    margin-bottom: 6px;
    font-size: 0.72rem;
}

.message-role { font-weight: 600; }
.message-time { opacity: 0.85; }

.message-content p { margin: 0; white-space: normal; word-break: break-word; }
.message-error { background: #feecef !important; color: #7f1d1d !important; border-left: 3px solid var(--danger); }

.history-separator {
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--text-secondary);
    font-size: 0.8rem;
    opacity: 0.8;
}

.history-separator hr {
    flex: 1;
    border: 0;
    border-top: 1px dashed var(--border);
}

.code-block {
    margin: 6px 0 0;
    padding: 10px;
    border-radius: var(--radius-sm);
    background: rgba(0,0,0,0.25);
    overflow-x: auto;
}

.inline-code {
    padding: 1px 4px;
    border-radius: 4px;
    background: rgba(0,0,0,0.25);
}

@keyframes typingBounce {
    0%, 80%, 100% { transform: scale(1); opacity: 0.45; }
    40% { transform: scale(1.45); opacity: 1; }
}

.typing-dots { display: inline-flex; gap: 5px; margin-left: 4px; }
.typing-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #ff8a3d;
    animation: typingBounce 1s infinite ease-in-out;
}
.typing-dot:nth-child(2) { animation-delay: 0.15s; }
.typing-dot:nth-child(3) { animation-delay: 0.3s; }

.chat-input-container {
    background: var(--bg-surface);
    border-top: 1px solid var(--border);
    padding: 10px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0));
}

.chat-input-wrapper {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: var(--bg-main);
    padding: 6px;
}

.chat-input {
    flex: 1;
    border: 0;
    outline: none;
    resize: none;
    min-height: 22px;
    max-height: 150px;
    padding: 6px 8px;
    color: var(--text-primary);
    background: transparent;
    font-size: 0.95rem;
}

.chat-send-button {
    border: 0;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: var(--primary);
    color: #fff;
    cursor: pointer;
}

.chat-send-button svg { width: 18px; height: 18px; }

.chat-footer {
    border-top: 1px solid var(--border);
    background: var(--bg-surface);
    color: var(--text-secondary);
    text-align: center;
    padding: 8px 12px;
    padding-bottom: calc(8px + env(safe-area-inset-bottom, 0));
    font-size: 0.74rem;
}

.chat-footer > div { margin: 3px 0; }
.footer-links {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 8px;
}

.privacy-notice {
    margin: 0 auto;
    max-width: 660px;
    line-height: 1.35;
    text-align: center;
}

@media (max-width: 900px) {
    .chat-header {
        position: sticky;
        flex-wrap: wrap;
        align-items: flex-start;
        justify-content: flex-start;
    }

    .chat-header-left {
        flex: 1 1 100%;
        width: 100%;
        max-width: 100%;
        flex-direction: column;
        align-items: flex-start;
        gap: 6px;
        min-width: 0;
        padding-right: 92px;
    }

    .chat-title {
        width: 100%;
        max-width: 100%;
    }

    .model-select {
        width: fit-content;
        max-width: fit-content;
        flex: 0 0 auto;
        display: inline-flex;
        justify-content: flex-start;
    }

    .chat-header-actions {
        position: relative;
        flex: 1 1 100%;
        width: 100%;
        max-width: 100%;
        margin-left: 0;
        justify-content: flex-start;
        flex-wrap: wrap;
        align-items: center;
        column-gap: 6px;
        row-gap: 4px;
        padding-top: 4px;
    }

    /* Keep language/theme/menu fixed on top-right row */
    .chat-header-actions .header-action-group,
    .chat-header-actions .chat-menu {
        position: absolute;
        top: -38px;
        order: 1;
        flex: 0 0 auto;
        white-space: nowrap;
    }

    .chat-header-actions .header-action-group {
        right: 42px;
    }

    .chat-header-actions .chat-menu {
        right: 0;
    }

    /* Let longer auth/user buttons flow to the next row when needed */
    .chat-header-actions .login-btn,
    .chat-header-actions .register-btn,
    .chat-header-actions .donate-btn,
    .chat-header-actions .user-info {
        order: 2;
    }

    .user-name { display: none; }

    .message { max-width: 92%; }
}

@media print {
    .chat-header,
    .chat-input-container,
    .chat-footer { display: none !important; }

    .chat-app { box-shadow: none; }
    .chat-messages { overflow: visible; }
}
