:root {
    --primary-color: #2563eb;
    --primary-hover: #1d4ed8;
    --background-color: #0f172a;
    --surface-color: #1e293b;
    --surface-hover: #334155;
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --user-msg-bg: #1e40af;
    --ai-msg-bg: #1e293b;
    --input-bg: #334155;
    --border-color: #2d3748;
    --success-color: #10b981;
    --error-color: #ef4444;
    --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
    --transition: all 0.2s ease-in-out;
}

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap');

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: var(--font-sans);
    background-color: var(--background-color);
    color: var(--text-primary);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    /* Removed background-image and background-size for animated background */
}

.chat-container {
    width: 100%;
    max-width: 800px;
    background: rgba(30, 41, 59, 0.75); /* --surface-color: #1e293b; */
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border-radius: 20px;
    box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.17); /* Softer shadow for glassmorphism */
    overflow: hidden;
    display: flex;
    flex-direction: column;
    height: 90vh;
    border: 1px solid rgba(255, 255, 255, 0.18);
    transition: var(--transition);
}

.chat-header {
    background: rgba(30, 41, 59, 0.65); /* Slightly more transparent than chat-container */
    padding: 1.25rem 1.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    position: relative;
    z-index: 10;
    /* backdrop-filter is inherited or can be specified if needed */
}

.chat-header h1 {
    font-size: 1.25rem;
    font-weight: 600;
    margin: 0;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

#model-select {
    background-color: rgba(51, 65, 85, 0.6); /* --input-bg: #334155; */
    color: var(--text-primary);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 0.5rem;
    padding: 0.5rem 2.5rem 0.5rem 1rem;
    font-size: 0.875rem;
    font-weight: 500;
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23cccccc' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E"); /* Adjusted stroke for better visibility */
    background-repeat: no-repeat;
    background-position: right 0.75rem center;
    background-size: 1rem;
    cursor: pointer;
    transition: var(--transition);
    min-width: 140px;
}

#model-select:hover {
    background-color: rgba(51, 65, 85, 0.75); /* Darken slightly on hover */
    border-color: rgba(255, 255, 255, 0.3);
}

#model-select:focus {
    outline: none;
    border-color: rgba(255, 255, 255, 0.5);
    box-shadow: 0 0 0 2px rgba(51, 65, 85, 0.3); /* Adjusted shadow for glass effect */
}

.model-info {
    text-align: center;
    font-style: italic;
    color: #a0a0a0; /* Dunkelgrauer Farbton */
    background-color: rgba(30, 41, 59, 0.7); /* Dunklerer, halbtransparenter Hintergrund */
    padding: 8px;
    margin: 10px 0;
    border-radius: 20px;
    font-size: 0.9em;
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
}

#chatbox {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
    scroll-behavior: smooth;
}

/* Custom scrollbar */
#chatbox::-webkit-scrollbar {
    width: 6px;
}

#chatbox::-webkit-scrollbar-track {
    background: transparent;
}

#chatbox::-webkit-scrollbar-thumb {
    background-color: var(--border-color);
    border-radius: 3px;
}

#chatbox::-webkit-scrollbar-thumb:hover {
    background-color: var(--text-secondary);
}

#chatbox {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    scroll-behavior: smooth;
    padding-bottom: 1rem;
    min-height: 0; /* Fix for Firefox */
}

.message {
    position: relative;
    padding: 0.875rem 1.25rem;
    border-radius: 1.25rem;
    max-width: 85%;
    word-wrap: break-word;
    line-height: 1.6;
    font-size: 0.9375rem;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    animation: fadeIn 0.2s ease-out;
    opacity: 0;
    animation-fill-mode: forwards;
    transform-origin: center bottom;
}

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

.user-message {
    background: rgba(37, 99, 235, 0.7); /* --primary-color: #2563eb */
    color: var(--text-primary); /* Ensure good contrast */
    margin-left: auto;
    margin-right: 0.5rem;
    border-radius: 1.25rem 1.25rem 0.5rem 1.25rem;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 1px solid rgba(37, 99, 235, 0.4);
}

.ai-message {
    background: rgba(30, 41, 59, 0.7); /* --ai-msg-bg: #1e293b */
    color: var(--text-primary); /* Ensure good contrast */
    margin-right: auto;
    margin-left: 0.5rem;
    border-radius: 1.25rem 1.25rem 1.25rem 0.5rem;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 1px solid rgba(255, 255, 255, 0.15);
}

/* Ensure the chat scrolls to the bottom when new messages arrive */
#chatbox {
    scroll-behavior: smooth;
}

/* Make sure the last message is always visible */
#chatbox > *:last-child {
    scroll-margin-bottom: 1rem;
}

/* Fix for WebKit scrollbar */
#chatbox::-webkit-scrollbar {
    width: 6px;
}

#chatbox::-webkit-scrollbar-thumb {
    background-color: var(--border-color);
    border-radius: 3px;
}

#chatbox::-webkit-scrollbar-track {
    background: transparent;
}

/* Ensure proper spacing at the bottom of the chat */
#chatbox::after {
    content: '';
    display: block;
    height: 1rem;
    flex-shrink: 0;
}

.message-content {
    position: relative;
    z-index: 1;
}

.message-content p {
    margin: 0.5rem 0;
}

.message-content p:first-child {
    margin-top: 0;
}

.message-content p:last-child {
    margin-bottom: 0;
}

/* Markdown styling */
.message-content pre {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 0.5rem;
    padding: 1rem;
    overflow-x: auto;
    margin: 0.75rem 0;
    font-size: 0.875rem;
    line-height: 1.5;
}

.message-content code {
    font-family: 'Fira Code', 'Courier New', monospace;
    font-size: 0.875em;
}

.message-content pre code {
    background: transparent;
    padding: 0;
}

.message-content a {
    color: var(--primary-color);
    text-decoration: none;
    border-bottom: 1px solid var(--primary-color);
    transition: var(--transition);
}

.message-content a:hover {
    color: var(--primary-hover);
    border-color: var(--primary-hover);
}

.message-meta {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.75rem;
    color: var(--text-secondary);
    margin-top: 0.75rem;
    padding-top: 0.75rem;
    border-top: 1px solid var(--border-color);
}

.message-meta span {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.message-meta .model-name {
    font-weight: 500;
    color: var(--primary-color);
}

/* Loading animation */
@keyframes bounce {
    0%, 80%, 100% { 
        transform: scale(0);
        opacity: 0.4;
    }
    40% { 
        transform: scale(1);
        opacity: 0.8;
    }
}

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

/* Status indicators */
.status-indicator {
    font-size: 0.75rem;
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    margin-left: 0.5rem;
    color: var(--text-secondary);
}

.status-indicator.online {
    color: var(--success-color);
}

.status-indicator.offline {
    color: var(--error-color);
}

@media (max-width: 640px) {
    html, body {
        height: 100dvh; /* Use dynamic viewport height */
        overflow: hidden;
        margin: 0;
    }

    body {
        padding: 0; /* Override global padding */
        /* display: flex; is global */
        /* flex-direction: column; is global */
        align-items: stretch; /* Override global center */
        justify-content: flex-start; /* Override global center */
        background-image: none; /* Ensure no body image on mobile */
        background-color: var(--background-color); /* Match main body background for canvas visibility */
    }

    .chat-container {
        flex-grow: 1;
        min-height: 0; /* Allow shrinking in flex context */
        width: 100%;   /* Ensure full width */
        max-width: 100%; /* Ensure it doesn't exceed viewport */
        border-radius: 0; /* Existing */
        border: none; /* Remove border for seamless full screen */
        box-shadow: none; /* Remove shadow for full-screen mobile view */
        /* height: 100vh; Removed as flex-grow and body height manage this */
    }
}

/* Responsive adjustments */
@media (max-width: 480px) {
    .message {
        max-width: 90%;
        padding: 0.75rem 1rem;
    }
    
    #chatbox {
        padding: 1rem;
    }
    
    .input-area {
        padding: 0.75rem;
    }
    
    #userinput {
        font-size: 1rem;
    }

    html {
        font-size: 18px;
    }

    body {
        font-size: 1rem; /* This will now be 18px */
    }
}

.input-area {
    display: flex;
    padding: 1rem 1.5rem;
    background: rgba(30, 41, 59, 0.65); /* Similar to .chat-header */
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    gap: 0.75rem;
    align-items: center;
    position: relative;
    z-index: 10;
    /* backdrop-filter is inherited or can be specified if needed */
}

#userinput {
    flex: 1;
    background: rgba(51, 65, 85, 0.5); /* --input-bg: #334155; */
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 1.5rem;
    padding: 0.75rem 1rem;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.9375rem;
    line-height: 1.5;
    resize: none;
    min-height: 3rem;
    max-height: 12rem;
    transition: var(--transition);
    box-shadow: var(--shadow-sm);
    align-self: center;
    margin: 0;
    box-sizing: border-box;
    /* Hide scrollbar for WebKit browsers */
    -ms-overflow-style: none;  /* IE and Edge */
    scrollbar-width: none;  /* Firefox */
}

/* Hide scrollbar for WebKit browsers */
#userinput::-webkit-scrollbar {
    display: none;
}

#userinput:focus {
    outline: none;
    border-color: rgba(255, 255, 255, 0.3); /* Lighter border on focus */
    box-shadow: 0 0 0 3px rgba(51, 65, 85, 0.25); /* Lighter, more diffused shadow */
}

#userinput::placeholder {
    color: var(--text-secondary);
    opacity: 1;
}

#send, #file-upload-btn {
    background: var(--primary-color);
    color: white;
    border: none;
    border-radius: 1.5rem;
    width: 3rem;
    height: 3rem;
    min-width: 3rem;
    min-height: 3rem;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: var(--transition);
    flex-shrink: 0;
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}

#send:hover, #file-upload-btn:hover {
    background: var(--primary-hover);
    transform: translateY(-1px);
    box-shadow: var(--shadow);
}

#send:active, #file-upload-btn:active {
    transform: translateY(0);
}

#send:disabled {
    background: var(--text-secondary);
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
    opacity: 0.7;
}

#file-upload-btn {
    background: transparent;
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
}

#file-upload-btn:hover {
    background: var(--surface-hover);
    border-color: var(--primary-color);
    color: var(--primary-color);
}

/* Make sure icons are centered */
#send i, #file-upload-btn i {
    font-size: 1.25rem;
    line-height: 1;
}

/* Typing indicator animation */
@keyframes blink {
    0%, 100% { opacity: 0.4; }
    50% { opacity: 1; }
}

.typing-indicator {
    display: flex;
    gap: 0.5rem;
    padding: 0.5rem 1rem;
    background: var(--ai-msg-bg);
    border-radius: 1rem;
    width: fit-content;
    margin: 0.5rem 0;
    align-items: center;
}

.typing-indicator span {
    width: 8px;
    height: 8px;
    background: var(--primary-color);
    border-radius: 50%;
    display: inline-block;
    animation: blink 1.4s infinite both;
}

.typing-indicator span:nth-child(1) { animation-delay: 0s; }
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

/* Cursor animation */
@keyframes blink-cursor {
    0%, 100% { opacity: 1; }
    50% { opacity: 0; }
}

.typing-cursor {
    display: inline-block;
    width: 2px;
    height: 1em;
    background-color: currentColor;
    margin-left: 2px;
    animation: blink-cursor 0.7s step-end infinite;
    vertical-align: middle;
}

/* Markdown content styles */
.message-content {
    line-height: 1.6;
}

.message-content p {
    margin: 0.5em 0;
}

.message-content p:first-child {
    margin-top: 0;
}

.message-content p:last-child {
    margin-bottom: 0;
}

.message-content ul,
.message-content ol {
    margin: 0.5em 0;
    padding-left: 1.5em;
}

.message-content li {
    margin: 0.25em 0;
}

.message-content code {
    font-family: 'Fira Code', 'Courier New', monospace;
    font-size: 0.9em;
    background: rgba(0, 0, 0, 0.2);
    padding: 0.2em 0.4em;
    border-radius: 0.25em;
}

.message-content pre {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 0.5rem;
    padding: 1rem;
    overflow-x: auto;
    margin: 0.75rem 0;
    line-height: 1.5;
}

.message-content pre code {
    background: transparent;
    padding: 0;
    font-size: 0.9em;
}

.message-content blockquote {
    border-left: 3px solid var(--primary-color);
    margin: 0.75rem 0;
    padding: 0.25rem 0 0.25rem 1rem;
    color: var(--text-secondary);
    font-style: italic;
}

.message-content img {
    max-width: 100%;
    height: auto;
    border-radius: 0.5rem;
    margin: 0.5rem 0;
}

.message-content a {
    color: var(--primary-color);
    text-decoration: none;
    border-bottom: 1px solid var(--primary-color);
    transition: var(--transition);
}

.message-content a:hover {
    color: var(--primary-hover);
    border-color: var(--primary-hover);
}

/* Responsive adjustments */
@media (max-width: 480px) {
    html {
        font-size: 18px; /* Base font size for small screens */
    }

    body {
        font-size: 1rem; /* Body font size (18px) */
    }

    .chat-header {
        padding: 1.5rem; /* Increased padding */
        flex-wrap: wrap; /* Allow items to wrap */
        justify-content: center; /* Center items when wrapped */
        gap: 0.5rem; /* Spacing for wrapped items */
    }

    .chat-header h1 {
        font-size: 1.5rem; /* Increased title size */
    }

    #model-select {
        font-size: 1rem; /* Increased font size */
        padding: 0.75rem 3rem 0.75rem 1.25rem; /* Adjusted padding */
    }

    .message {
        max-width: 90%;
        padding: 1rem 1.25rem; /* Increased padding */
        font-size: 1rem; /* Increased font size (18px) */
    }
    
    #chatbox {
        padding: 1rem; /* Existing padding, ensure it's still here */
        gap: 1rem; /* Increased gap between messages */
    }
    
    .input-area {
        padding: 1rem; /* Increased padding */
        gap: 1rem; /* Increased gap between input elements */
    }
    
    #userinput {
        font-size: 1rem; /* Font size is now 18px */
        padding: 0.75rem 1rem; /* Reduced padding */
        min-height: 3rem; /* Reduced min-height */
    }

    #send, #file-upload-btn {
        width: 3rem; /* Reduced size */
        height: 3rem; /* Reduced size */
    }

    #send i, #file-upload-btn i {
        font-size: 1.25rem; /* Reduced icon size */
    }
}