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

html,
body {
    width: 100%;
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

/* Login Screen */
.login-container {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    backdrop-filter: blur(10px);
}

.login-container.hidden {
    display: none !important;
}

.login-card {
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(20px);
    padding: 40px;
    border-radius: 20px;
    box-shadow: 0 8px 32px rgba(31, 38, 135, 0.15);
    text-align: center;
    width: 90%;
    max-width: 400px;
    border: 1px solid rgba(255, 255, 255, 0.7);
}

.login-card h1 {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    margin-bottom: 10px;
    font-size: 28px;
    font-weight: 700;
}

.login-card p {
    color: #666;
    margin-bottom: 30px;
    font-size: 14px;
}

.login-card input {
    width: 100%;
    padding: 12px;
    margin-bottom: 15px;
    border: 1px solid rgba(102, 126, 234, 0.3);
    border-radius: 12px;
    font-size: 14px;
    transition: all 0.3s;
    background: rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(10px);
}

.login-card input:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
    background: rgba(255, 255, 255, 0.95);
}

.login-card button {
    width: 100%;
    padding: 12px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: #fff;
    border: none;
    border-radius: 12px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
}

.login-card button:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6);
}

.login-card .error {
    color: #ef4444;
    font-size: 12px;
    margin-bottom: 15px;
}

.login-card .error.hidden {
    display: none;
}

/* Main App */
.app {
    display: flex;
    height: 100vh;
    background: linear-gradient(135deg, #f5f7fa 0%, #e8ecf1 100%);
}

.app.hidden {
    display: none;
}

/* Sidebar */
.sidebar {
    width: 260px;
    background: rgba(255, 255, 255, 0.85);
    backdrop-filter: blur(10px);
    border-right: 1px solid rgba(255, 255, 255, 0.6);
    display: flex;
    flex-direction: column;
    transition: width 0.3s, margin-left 0.3s;
}

.sidebar.offcanvas {
    position: fixed;
    left: 0;
    top: 0;
    height: 100%;
    z-index: 1100;
    transform: translateX(-100%);
    transition: transform .28s ease;
    box-shadow: 0 8px 32px rgba(31, 38, 135, 0.2);
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(20px);
}

.sidebar.offcanvas.open {
    transform: translateX(0);
}

.sidebar-header {
    padding: 14px;
    border-bottom: 1px solid rgba(102, 126, 234, 0.1);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.sidebar-header h2 {
    font-size: 20px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    font-weight: 700;
}

.sidebar-header button {
    background: rgba(102, 126, 234, 0.1);
    border: 1px solid rgba(102, 126, 234, 0.2);
    padding: 8px 12px;
    border-radius: 8px;
    cursor: pointer;
    color: #667eea;
    transition: all 0.2s;
}

.sidebar-header button:hover {
    background: rgba(102, 126, 234, 0.2);
    border-color: rgba(102, 126, 234, 0.3);
}

.new-chat-btn {
    width: calc(100% - 32px);
    margin: 12px 16px;
    padding: 10px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: 1px solid rgba(102, 126, 234, 0.4);
    border-radius: 12px;
    cursor: pointer;
    color: #fff;
    font-weight: 600;
    transition: all 0.2s;
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.2);
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 44px;
}

.new-chat-btn:active {
    transform: translateY(0px);
    box-shadow: 0 2px 10px rgba(102, 126, 234, 0.2);
}

.new-chat-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.3);
}

.new-chat-btn:focus {
    outline: none;
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4), 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.new-chat-btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: translateY(0px);
    box-shadow: 0 2px 10px rgba(102, 126, 234, 0.2);
}

.new-chat-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.3);
}

.sidebar-content {
    flex: 1;
    overflow-y: auto;
    padding: 8px 0;
}

.history-section {
    padding: 12px 16px;
    border-top: 1px solid rgba(102, 126, 234, 0.1);
}

.history-section h3 {
    font-size: 12px;
    color: #667eea;
    font-weight: 600;
    text-transform: uppercase;
    margin-bottom: 8px;
    opacity: 0.7;
}

.history-item {
    padding: 10px 12px;
    margin-bottom: 4px;
    background: rgba(102, 126, 234, 0.08);
    border-radius: 8px;
    color: #333;
    font-size: 13px;
    border: 1px solid rgba(102, 126, 234, 0.15);
    transition: all 0.2s;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
}

.history-item:hover {
    background: rgba(102, 126, 234, 0.12);
    border-color: rgba(102, 126, 234, 0.25);
}

.history-item.active {
    background: linear-gradient(135deg, rgba(102, 126, 234, 0.25), rgba(118, 75, 162, 0.15));
    color: #667eea;
    font-weight: 600;
    border-color: #667eea;
}

.history-item-text {
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    cursor: pointer;
}

.history-item-delete {
    background: rgba(239, 68, 68, 0.2);
    color: #ef4444;
    border: 1px solid rgba(239, 68, 68, 0.3);
    padding: 4px 8px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 11px;
    font-weight: 600;
    opacity: 0;
    transition: all 0.2s;
    white-space: nowrap;
}

.history-item:hover .history-item-delete {
    opacity: 1;
}

.history-item-delete:hover {
    background: rgba(239, 68, 68, 0.3);
    border-color: rgba(239, 68, 68, 0.5);
}

.sidebar-footer {
    padding: 12px 16px;
    border-top: 1px solid rgba(102, 126, 234, 0.1);
    display: flex;
    gap: 8px;
    flex-direction: column;
}

.user-info {
    padding: 8px;
    background: rgba(102, 126, 234, 0.1);
    border-radius: 8px;
    font-size: 12px;
    color: #667eea;
    border: 1px solid rgba(102, 126, 234, 0.2);
}

.logout-btn {
    background: rgba(239, 68, 68, 0.2);
    color: #ef4444;
    border: 1px solid rgba(239, 68, 68, 0.3);
    padding: 8px 12px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    transition: all 0.2s;
}

.logout-btn:hover {
    background: rgba(239, 68, 68, 0.3);
    border-color: rgba(239, 68, 68, 0.5);
}

/* Main Content */
.main {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: linear-gradient(135deg, #f5f7fa 0%, #e8ecf1 100%);
    padding: 0;
    margin: 0
}

.topbar {
    padding: 12px 12px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.6);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: rgba(255, 255, 255, 0.85);
    backdrop-filter: blur(10px);
}

.model-selector {
    display: flex;
    align-items: center;
    gap: 8px;
}

.model-selector select {
    padding: 8px 12px;
    border: 1px solid rgba(102, 126, 234, 0.3);
    border-radius: 8px;
    font-size: 14px;
    background: rgba(255, 255, 255, 0.9);
    cursor: pointer;
    color: #333;
    transition: all 0.2s;
}

#mobileMenuBtn {
    display: none
}

.model-selector select:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

/* Model description area shown under the selector */
.model-description {
    margin-left: 8px;
    margin-top: 8px;
    font-size: 13px;
    color: #374151;
    background: rgba(255,255,255,0.9);
    border: 1px solid rgba(102,126,234,0.08);
    padding: 10px 12px;
    border-radius: 10px;
    max-width: 520px;
    box-shadow: 0 6px 18px rgba(31,38,135,0.05);
}

/* Banner shown at top of chat area with model description */
.model-banner {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 6px 8px;
    color: #374151;
    font-size: 14px;
    text-align: center;
    margin: 8px 0;
}

.model-banner strong {
    color: #4f46e5;
    font-weight: 700;
}

.chat-area {
    flex: 1;
    overflow-y: auto;
    padding: 12px 12px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    background: linear-gradient(135deg, #f5f7fa 0%, #e8ecf1 100%);
    max-width: none;
    margin: 0;
    width: 100%;
    padding-bottom: 110px;
    align-items: stretch
}

/* Message rows */
.message-wrapper {
    display: flex;
    gap: 10px;
    animation: slideIn 0.18s ease;
    margin-bottom: 10px;
    width: 100%;
    padding: 6px
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(10px)
    }

    to {
        opacity: 1;
        transform: translateY(0)
    }
}

.message-wrapper.user-message {
    flex-direction: row-reverse;
    align-items: flex-end
}

.message-wrapper.ai-message {
    flex-direction: row;
    justify-content: flex-start;
    align-items: flex-start
}

.message-wrapper .message-content {
    display: flex;
    flex-direction: column
}

.message-wrapper.user-message .message-content {
    align-items: flex-end
}

.message-wrapper.ai-message .message-content {
    align-items: flex-start
}

.message-avatar {
    width: 36px;
    height: 36px;
    min-width: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    color: #fff;
    font-size: 16px;
    flex-shrink: 0
}

/* Avatar outer-edge spacing so avatars sit near container edges */
.message-wrapper.ai-message .message-avatar {
    margin-left: 0;
    margin-right: 12px
}

.message-wrapper.user-message .message-avatar {
    margin-right: 0;
    margin-left: 12px
}

.user-avatar {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.ai-avatar {
    background: linear-gradient(135deg, #10b981 0%, #059669 100%);
}

.message-content {
    max-width: 72%;
    flex-shrink: 0;
    display: flex;
    flex-direction: column
}

.message-content {
    flex: 1
}

/* Push bubbles fully to left or right within the content area */
.message-wrapper.user-message .bubble {
    margin-left: auto
}

.message-wrapper.ai-message .bubble {
    margin-right: auto
}

/* Make bubble fit and not stretch past a reasonable width */
.bubble {
    max-width: 72%
}

.message-wrapper.user-message .message-content {
    align-items: flex-end
}

.message-wrapper.ai-message .message-content {
    align-items: flex-start
}

.bubble {
    padding: 12px 16px;
    border-radius: 14px;
    line-height: 1.5;
    word-break: break-word;
    display: inline-block;
}

.user-message .bubble {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: #fff;
    border-radius: 18px 18px 4px 18px;
}

.ai-message .bubble {
    background: rgba(255, 255, 255, 0.85);
    backdrop-filter: blur(10px);
    color: #333;
    border-radius: 18px 18px 18px 4px;
    border: 1px solid rgba(255, 255, 255, 0.7);
}

.bubble {
    box-shadow: 0 8px 24px rgba(31, 38, 135, 0.1);
}

.message-meta {
    font-size: 11px;
    color: #7c8798;
    margin-top: 6px
}

.message-avatar {
    flex-shrink: 0
}

.typing-indicator {
    display: flex;
    gap: 4px;
    align-items: center;
    padding: 12px 16px;
}

.typing-dot {
    width: 8px;
    height: 8px;
    background: #999;
    border-radius: 50%;
    animation: blink 1.4s infinite;
}

.typing-dot:nth-child(2) {
    animation-delay: 0.2s
}

.typing-dot:nth-child(3) {
    animation-delay: 0.4s
}

@keyframes blink {

    0%,
    100% {
        opacity: 0.4
    }

    50% {
        opacity: 1
    }
}

/* Code Block */
.code-block {
    background: #1f2937;
    color: #e5e7eb;
    padding: 12px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 8px 0;
    font-size: 13px;
    font-family: 'Monaco', 'Courier New', monospace;
}

.code-block code {
    display: block;
    white-space: pre-wrap;
}

/* Lists */
.message-content ul,
.message-content ol {
    margin-left: 20px;
    margin-top: 8px;
}

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

.composer {
    display: flex;
    gap: 12px;
    padding: 9px;
    border-top: 1px solid rgba(255, 255, 255, 0.6);
    background: rgba(255, 255, 255, 0.85);
    backdrop-filter: blur(10px);
    align-items: center;
}

/* the input wrapper becomes a single visual container with a floating send button */
.input-wrapper {
    flex: 1;
    display: flex;
    position: relative;
    align-items: center;
    background: rgba(255, 255, 255, 0.95);
    border: 1px solid rgba(102, 126, 234, 0.3);
    padding: 6px;
    border-radius: 14px;
    transition: all 0.2s;
}

.input-wrapper:focus-within {
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

textarea {
    width: 100%;
    padding: 12px;
    border: 0;
    border-radius: 8px;
    font-size: 14px;
    font-family: inherit;
    resize: vertical;
    min-height: 44px;
    max-height: 200px;
    transition: border 0.2s;
    background: transparent;
    padding-right: 56px
}

textarea:focus {
    outline: none;
    border-color: transparent;
}

.send-btn {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: #fff;
    border: none;
    padding: 8px 14px;
    min-width: 72px;
    height: 40px;
    border-radius: 10px;
    cursor: pointer;
    font-weight: 700;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.send-btn:hover {
    transform: translateY(-50%) translateY(-2px);
    box-shadow: 0 6px 16px rgba(102, 126, 234, 0.4);
}

.send-btn.loading {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    opacity: 0.8;
}

.send-btn.stop {
    background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
}

.send-icon {
    width: 18px;
    height: 18px
}

/* ensure composer sticky and consistent background */
.composer {
    position: sticky;
    bottom: 0;
    z-index: 90;
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    box-shadow: 0 -4px 12px rgba(31, 38, 135, 0.08);
}

/* Empty State */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: #999;
}

.empty-state-icon {
    font-size: 64px;
    margin-bottom: 16px;
    opacity: 0.8;
}

.empty-state h2 {
    font-size: 24px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    margin-bottom: 8px;
    font-weight: 600;
}

.empty-state p {
    color: #999;
    font-size: 15px;
    max-width: 300px;
    text-align: center;
}

/* Responsive */
@media (max-width:768px) {
    .sidebar {
        width: 200px;
    }

    .sidebar-header h2 {
        font-size: 16px;
    }

    .message-content {
        max-width: 85%;
    }

    .composer {
        flex-direction: column;
        gap: 8px
    }

    .input-wrapper {
        width: 100%
    }

    .send-btn {
        position: static;
        transform: none;
        width: 100%;
        height: 44px;
        border-radius: 8px
    }
}

@media (max-width:640px) {
    .app {
        flex-direction: column;
    }

    /* sidebar as off-canvas full-height panel */
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        height: 100vh;
        transform: translateX(-100%);
        transition: transform .28s ease;
        z-index: 1100;
        background: rgba(255, 255, 255, 0.95);
        backdrop-filter: blur(10px);
        width: 320px;
        display: flex;
        flex-direction: column;
        overflow-y: auto;
        border-right: 1px solid rgba(255, 255, 255, 0.6);
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .main {
        height: auto;
        flex: 1;
    }

    .message-wrapper {
        flex-direction: column;
    }

    .message-content {
        max-width: 100%;
    }

    .message-avatar {
        width: 28px;
        height: 28px;
        font-size: 12px;
    }

    .topbar {
        flex-direction: column;
        gap: 8px;
    }

    #mobileMenuBtn {
        display: inline-flex
    }

    #sidebarOverlay {
        position: fixed;
        left: 0;
        top: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.45);
        z-index: 1050;
        display: none
    }

    #sidebarOverlay.show {
        display: block
    }

    /* Make chat area full width on mobile and composer stretch */
    .chat-area {
        max-width: 100%;
        padding-left: 12px;
        padding-right: 12px;
        margin: 0;
        padding-bottom: 120px
    }

    .composer {
        flex-direction: row;
        align-items: center;
        padding: 12px
    }

    .input-wrapper {
        width: 100%
    }

    textarea {
        width: 100%;
        min-height: 44px;
        border-radius: 10px
    }

    .send-btn {
        position: absolute;
        right: 8px;
        top: 50%;
        transform: translateY(-50%);
        width: 44px;
        height: 44px;
        border-radius: 10px
    }
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: rgba(102, 126, 234, 0.05);
}

::-webkit-scrollbar-thumb {
    background: rgba(102, 126, 234, 0.3);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: rgba(102, 126, 234, 0.5);
}