/* === Variables CSS === */
:root {
    --bg-primary: #0f0f1a;
    --bg-secondary: #1a1a2e;
    --bg-tertiary: #252542;
    --bg-input: #2d2d4a;
    --text-primary: #e8e8e8;
    --text-secondary: #a0a0b0;
    --text-muted: #6a6a7a;
    --accent: #6366f1;
    --accent-hover: #818cf8;
    --accent-light: rgba(99, 102, 241, 0.1);
    --success: #10b981;
    --error: #ef4444;
    --warning: #f59e0b;
    --border-color: #3a3a5a;
    --shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
    --radius: 12px;
    --radius-sm: 8px;
    --transition: all 0.2s ease;
}

/* === Reset & Base === */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
}

#app {
    height: 100%;
    display: flex;
}

.screen {
    display: none;
    width: 100%;
    height: 100%;
}

.screen.active {
    display: flex;
}

/* === Auth Screen === */
.auth-container {
    width: 100%;
    max-width: 400px;
    margin: auto;
    padding: 40px 24px;
}

.logo {
    text-align: center;
    margin-bottom: 40px;
}

.logo-icon {
    font-size: 64px;
    display: block;
    margin-bottom: 16px;
    filter: drop-shadow(0 0 20px rgba(99, 102, 241, 0.5));
}

.logo h1 {
    font-size: 36px;
    font-weight: 700;
    background: linear-gradient(135deg, var(--accent), #a855f7);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.tagline {
    color: var(--text-secondary);
    margin-top: 8px;
}

.auth-tabs {
    display: flex;
    background: var(--bg-secondary);
    border-radius: var(--radius);
    padding: 4px;
    margin-bottom: 24px;
}

.tab-btn {
    flex: 1;
    padding: 12px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    border-radius: var(--radius-sm);
    transition: var(--transition);
}

.tab-btn.active {
    background: var(--accent);
    color: white;
}

.auth-form {
    display: none;
}

.auth-form.active {
    display: block;
}

.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    margin-bottom: 8px;
    font-size: 14px;
    color: var(--text-secondary);
}

.form-group input {
    width: 100%;
    padding: 14px 16px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--bg-input);
    color: var(--text-primary);
    font-size: 16px;
    transition: var(--transition);
}

.form-group input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-light);
}

.form-group input::placeholder {
    color: var(--text-muted);
}

.btn-primary {
    width: 100%;
    padding: 14px;
    border: none;
    border-radius: var(--radius-sm);
    background: var(--accent);
    color: white;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
}

.btn-primary:hover {
    background: var(--accent-hover);
    transform: translateY(-1px);
}

.btn-primary:active {
    transform: translateY(0);
}

.form-error {
    margin-top: 16px;
    padding: 12px;
    background: rgba(239, 68, 68, 0.1);
    border: 1px solid var(--error);
    border-radius: var(--radius-sm);
    color: var(--error);
    font-size: 14px;
    display: none;
}

.form-error.visible {
    display: block;
}

/* === Chat Screen === */
#chat-screen {
    display: none;
    flex-direction: row;
}

#chat-screen.active {
    display: flex;
}

/* === Sidebar === */
.sidebar {
    width: 280px;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

.sidebar-header {
    padding: 16px;
    border-bottom: 1px solid var(--border-color);
}

.btn-new-chat {
    width: 100%;
    padding: 12px;
    border: 1px dashed var(--border-color);
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-primary);
    font-size: 14px;
    cursor: pointer;
    transition: var(--transition);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.btn-new-chat:hover {
    background: var(--accent-light);
    border-color: var(--accent);
}

.btn-new-chat span {
    font-size: 18px;
}

.conversations-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.conversation-item {
    padding: 12px 16px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    margin-bottom: 4px;
    transition: var(--transition);
}

.conversation-item:hover {
    background: var(--bg-tertiary);
}

.conversation-item.active {
    background: var(--accent-light);
    border-left: 3px solid var(--accent);
}

.conversation-item .title {
    font-size: 14px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.conversation-item .date {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 4px;
}

.sidebar-footer {
    padding: 16px;
    border-top: 1px solid var(--border-color);
}

.user-info {
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: 12px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sidebar-actions {
    display: flex;
    gap: 8px;
}

.btn-settings {
    flex: 1;
    padding: 10px;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    font-size: 13px;
    cursor: pointer;
    text-decoration: none;
    text-align: center;
    transition: var(--transition);
}

.btn-settings:hover {
    background: var(--bg-hover);
}

.btn-logout {
    flex: 1;
    padding: 10px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-secondary);
    font-size: 13px;
    cursor: pointer;
    transition: var(--transition);
}

.btn-logout:hover {
    background: rgba(239, 68, 68, 0.1);
    border-color: var(--error);
    color: var(--error);
}

/* === Chat Main === */
.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.chat-header {
    padding: 16px 20px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 16px;
}

.menu-toggle {
    display: none;
    padding: 8px;
    border: none;
    background: transparent;
    color: var(--text-primary);
    font-size: 20px;
    cursor: pointer;
}

.chat-header h2 {
    flex: 1;
    font-size: 16px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.btn-delete {
    padding: 8px;
    border: none;
    background: transparent;
    font-size: 16px;
    cursor: pointer;
    opacity: 0.6;
    transition: var(--transition);
}

.btn-delete:hover {
    opacity: 1;
}

/* === Messages Container === */
.messages-container {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
}

.welcome-message {
    text-align: center;
    padding: 40px 20px;
    max-width: 500px;
    margin: auto;
}

.welcome-icon {
    font-size: 64px;
    margin-bottom: 20px;
}

.welcome-message h2 {
    margin-bottom: 16px;
    color: var(--text-primary);
}

.welcome-message p {
    color: var(--text-secondary);
    margin-bottom: 16px;
}

.welcome-message ul {
    list-style: none;
    text-align: left;
    display: inline-block;
}

.welcome-message li {
    padding: 8px 0;
    color: var(--text-secondary);
}

.welcome-message .hint {
    margin-top: 24px;
    font-style: italic;
    color: var(--text-muted);
}

/* === Message Bubbles === */
.message {
    max-width: 80%;
    margin-bottom: 16px;
    animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.message.user {
    align-self: flex-end;
}

.message.assistant {
    align-self: flex-start;
}

.message-content {
    padding: 14px 18px;
    border-radius: var(--radius);
    line-height: 1.5;
}

.message.user .message-content {
    background: var(--accent);
    color: white;
    border-bottom-right-radius: 4px;
}

.message.assistant .message-content {
    background: var(--bg-tertiary);
    border-bottom-left-radius: 4px;
}

.message-content p {
    margin-bottom: 8px;
}

.message-content p:last-child {
    margin-bottom: 0;
}

.message-content code {
    background: rgba(0, 0, 0, 0.3);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'SF Mono', Monaco, monospace;
    font-size: 13px;
}

.message-content pre {
    background: rgba(0, 0, 0, 0.3);
    padding: 12px;
    border-radius: var(--radius-sm);
    overflow-x: auto;
    margin: 8px 0;
}

.message-content pre code {
    background: none;
    padding: 0;
}

.tools-used {
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    font-size: 12px;
    color: var(--text-muted);
}

.tool-badge {
    display: inline-block;
    padding: 2px 8px;
    background: var(--accent-light);
    color: var(--accent);
    border-radius: 20px;
    margin-left: 4px;
    font-weight: 500;
}

/* === Chat Input === */
.chat-input-container {
    padding: 16px 20px;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border-color);
}

.chat-form {
    display: flex;
    gap: 12px;
    align-items: flex-end;
}

#message-input {
    flex: 1;
    padding: 14px 16px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    background: var(--bg-input);
    color: var(--text-primary);
    font-size: 15px;
    resize: none;
    max-height: 150px;
    line-height: 1.4;
    font-family: inherit;
}

#message-input:focus {
    outline: none;
    border-color: var(--accent);
}

#message-input::placeholder {
    color: var(--text-muted);
}

.btn-send {
    width: 48px;
    height: 48px;
    border: none;
    border-radius: var(--radius);
    background: var(--accent);
    color: white;
    cursor: pointer;
    transition: var(--transition);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.btn-send:hover {
    background: var(--accent-hover);
}

.btn-send:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.send-icon {
    font-size: 18px;
}

/* === Typing Indicator === */
.typing-indicator {
    display: none;
    align-items: center;
    gap: 8px;
    padding: 8px 0;
    color: var(--text-muted);
    font-size: 13px;
}

.typing-indicator.visible {
    display: flex;
}

.typing-indicator span {
    width: 6px;
    height: 6px;
    background: var(--accent);
    border-radius: 50%;
    animation: bounce 1.4s infinite ease-in-out both;
}

.typing-indicator span:nth-child(1) { animation-delay: -0.32s; }
.typing-indicator span:nth-child(2) { animation-delay: -0.16s; }
.typing-indicator span:nth-child(3) { animation-delay: 0s; }

@keyframes bounce {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1); }
}

/* === Mobile Responsive === */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: -100%;
        top: 0;
        bottom: 0;
        z-index: 100;
        transition: left 0.3s ease;
        box-shadow: var(--shadow);
    }

    .sidebar.open {
        left: 0;
    }

    .sidebar-overlay {
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 99;
        display: none;
    }

    .sidebar-overlay.visible {
        display: block;
    }

    .menu-toggle {
        display: block;
    }

    .message {
        max-width: 90%;
    }

    .auth-container {
        padding: 24px 16px;
    }

    .logo-icon {
        font-size: 48px;
    }

    .logo h1 {
        font-size: 28px;
    }
}

/* === Scrollbar === */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* === Selection === */
::selection {
    background: var(--accent);
    color: white;
}

/* === Inline Media (Images & PDFs) === */
.inline-media {
    margin: 12px 0;
    border-radius: var(--radius);
    overflow: hidden;
    background: var(--bg-tertiary);
}

.inline-image {
    max-width: 100%;
    max-height: 400px;
    width: auto;
    height: auto;
    display: block;
    cursor: pointer;
    transition: transform 0.2s ease;
    border-radius: var(--radius);
}

.inline-image:hover {
    transform: scale(1.02);
}

.media-caption {
    display: block;
    padding: 8px 12px;
    font-size: 12px;
    color: var(--text-secondary);
    background: var(--bg-tertiary);
}

/* PDF Preview */
.pdf-preview {
    border: 1px solid var(--border-color);
}

.inline-pdf {
    width: 100%;
    height: 400px;
    border: none;
    display: block;
}

.pdf-fallback {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    background: var(--bg-tertiary);
    border-top: 1px solid var(--border-color);
}

.pdf-icon {
    font-size: 24px;
}

.pdf-link {
    flex: 1;
    color: var(--accent);
    text-decoration: none;
    font-weight: 500;
}

.pdf-link:hover {
    text-decoration: underline;
}

.pdf-download {
    color: var(--text-secondary);
    text-decoration: none;
    padding: 6px 12px;
    background: var(--bg-input);
    border-radius: var(--radius-sm);
    font-size: 14px;
}

.pdf-download:hover {
    background: var(--accent);
    color: white;
}

/* Media Modal */
.media-modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.9);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    cursor: pointer;
    animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.media-modal-content {
    max-width: 90vw;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    cursor: default;
}

.media-modal-content img {
    max-width: 100%;
    max-height: 80vh;
    object-fit: contain;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.media-modal-actions {
    display: flex;
    gap: 16px;
    margin-top: 16px;
}

.media-modal-actions a {
    color: var(--text-primary);
    text-decoration: none;
    padding: 8px 16px;
    background: var(--bg-tertiary);
    border-radius: var(--radius-sm);
    font-size: 14px;
    transition: var(--transition);
}

.media-modal-actions a:hover {
    background: var(--accent);
}

/* Multiple images in a row */
.inline-media + .inline-media {
    margin-top: 8px;
}

/* Webcam specific styling */
.message .inline-media img[src*="webcam"] {
    border: 2px solid var(--accent);
}
