/* =============================================================================
   CSS Variables & Theme
   ============================================================================= */

:root {
    /* Dark theme (default) */
    --bg-primary: #1a1a1a;
    --bg-secondary: #242424;
    --bg-tertiary: #2d2d2d;
    --bg-hover: #363636;
    --text-primary: #e8e8e8;
    --text-secondary: #a0a0a0;
    --text-muted: #707070;
    --border-color: rgba(255, 255, 255, 0.1);
    --border-light: rgba(255, 255, 255, 0.05);
    --accent-color: #4a9d5b;
    --accent-light: #5cb86c;
    --accent-dark: #3a7d4b;
    --error-color: #e05555;
    --success-color: #4a9d5b;
    --message-user-bg: #2a3a4a;
    --message-assistant-bg: #2a3d2a;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.2);
    --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 10px 25px rgba(0, 0, 0, 0.4);
    --radius-sm: 4px;
    --radius-md: 8px;
    --radius-lg: 12px;
    --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    --font-mono: 'SF Mono', Monaco, 'Cascadia Code', monospace;
}

/* Light theme */
@media (prefers-color-scheme: light) {
    :root {
        --bg-primary: #ffffff;
        --bg-secondary: #f5f5f5;
        --bg-tertiary: #ebebeb;
        --bg-hover: #e0e0e0;
        --text-primary: #1a1a1a;
        --text-secondary: #555555;
        --text-muted: #888888;
        --border-color: rgba(0, 0, 0, 0.1);
        --border-light: rgba(0, 0, 0, 0.05);
        --message-user-bg: #e3f2fd;
        --message-assistant-bg: #e8f5e9;
    }
}

/* =============================================================================
   Base Styles
   ============================================================================= */

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

html, body {
    height: 100%;
    font-family: var(--font-sans);
    font-size: 16px;
    line-height: 1.5;
    color: var(--text-primary);
    background-color: var(--bg-primary);
}

body {
    display: flex;
    flex-direction: column;
}

a {
    color: var(--accent-light);
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

/* =============================================================================
   Header & Footer (Branding)
   ============================================================================= */

.app-header {
    padding: 0.625rem 1.5rem;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    color: var(--text-primary);
}

.app-footer {
    padding: 0.5rem 1.5rem;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border-color);
    font-size: 0.8125rem;
    color: var(--text-muted);
    text-align: center;
}

.app-main {
    flex: 1;
    display: flex;
    overflow: hidden;
}

/* =============================================================================
   Login Page
   ============================================================================= */

.login-container {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100%;
    width: 100%;
    padding: 2rem;
    background: var(--bg-primary);
}

.login-card {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    padding: 2.5rem;
    width: 100%;
    max-width: 400px;
}

.login-title {
    font-size: 1.75rem;
    text-align: center;
    margin-bottom: 1.5rem;
    color: var(--accent-light);
}

.welcome-message {
    margin-bottom: 1.5rem;
    padding: 1rem;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-md);
    font-size: 0.9375rem;
    color: var(--text-secondary);
}

.error-message {
    padding: 0.75rem 1rem;
    background: rgba(224, 85, 85, 0.15);
    border: 1px solid rgba(224, 85, 85, 0.3);
    color: var(--error-color);
    border-radius: var(--radius-md);
    margin-bottom: 1rem;
    font-size: 0.875rem;
}

.login-form .form-group {
    margin-bottom: 1.25rem;
}

.login-form label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 500;
    color: var(--text-secondary);
}

.login-form input {
    width: 100%;
    padding: 0.75rem 1rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    font-size: 1rem;
    background: var(--bg-primary);
    color: var(--text-primary);
    transition: border-color 0.2s, box-shadow 0.2s;
}

.login-form input:focus {
    outline: none;
    border-color: var(--accent-color);
    box-shadow: 0 0 0 2px rgba(74, 157, 91, 0.2);
}

.login-form input::placeholder {
    color: var(--text-muted);
}

/* =============================================================================
   Buttons
   ============================================================================= */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.625rem 1.25rem;
    font-size: 0.9375rem;
    font-weight: 500;
    border: 1px solid transparent;
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: all 0.2s;
    color: var(--text-primary);
    background: var(--bg-tertiary);
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-primary {
    background: var(--accent-color);
    color: white;
    border-color: var(--accent-color);
}

.btn-primary:hover:not(:disabled) {
    background: var(--accent-dark);
    border-color: var(--accent-dark);
}

.btn-block {
    width: 100%;
}

.btn-small {
    padding: 0.375rem 0.75rem;
    font-size: 0.8125rem;
}

.btn-new-chat {
    width: 100%;
    background: var(--accent-color);
    color: white;
    padding: 0.625rem;
    border: none;
}

.btn-new-chat:hover:not(:disabled) {
    background: var(--accent-dark);
}

.btn-logout {
    padding: 0.375rem 0.75rem;
    background: transparent;
    color: var(--text-secondary);
    border: 1px solid var(--border-color);
    font-size: 0.8125rem;
}

.btn-logout:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.btn-send {
    width: 40px;
    height: 40px;
    padding: 0;
    background: var(--accent-color);
    color: white;
    border: none;
    border-radius: 50%;
    flex-shrink: 0;
}

.btn-send:hover:not(:disabled) {
    background: var(--accent-dark);
}

.btn-delete-dialog {
    opacity: 0;
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 0.25rem 0.5rem;
    font-size: 0.75rem;
    color: var(--text-muted);
    transition: opacity 0.2s, color 0.2s;
    border-radius: var(--radius-sm);
}

.btn-delete-dialog:hover {
    color: var(--error-color);
    background: rgba(224, 85, 85, 0.1);
}

.dialog-item:hover .btn-delete-dialog {
    opacity: 1;
}

/* =============================================================================
   Chat Layout
   ============================================================================= */

.chat-layout {
    display: flex;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

/* Sidebar */
.sidebar {
    width: 360px;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    overflow: hidden;
}

.sidebar-header {
    padding: 0.75rem;
    border-bottom: 1px solid var(--border-color);
}

.sidebar-section {
    flex: 1;
    overflow-y: auto;
    padding: 0.75rem;
}

.sidebar-section h3 {
    font-size: 0.6875rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    margin-bottom: 0.5rem;
    padding: 0 0.5rem;
}

.dialog-list {
    list-style: none;
}

.dialog-item {
    display: flex;
    align-items: center;
    margin-bottom: 2px;
    border-radius: var(--radius-md);
    transition: background 0.15s;
}

.dialog-item:hover {
    background: var(--bg-hover);
}

.dialog-item a {
    flex: 1;
    display: block;
    padding: 0.5rem 0.625rem;
    color: var(--text-primary);
    border-radius: var(--radius-md);
    transition: none;
}

.dialog-item a:hover {
    text-decoration: none;
}

.dialog-item.active {
    background: var(--accent-color);
}

.dialog-item.active a {
    color: white;
}

.dialog-item.active:hover {
    background: var(--accent-dark);
}

.dialog-preview {
    display: block;
    font-size: 0.8125rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.4;
}

.dialog-date {
    display: block;
    font-size: 0.6875rem;
    color: var(--text-muted);
    margin-top: 2px;
}

.dialog-item.active .dialog-date {
    color: rgba(255, 255, 255, 0.7);
}

.sidebar-footer {
    padding: 0.75rem;
    border-top: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
}

.username {
    font-size: 0.8125rem;
    color: var(--text-secondary);
}

/* Chat Main - теперь на всю ширину */
.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.chat-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    max-width: 1400px;
    width: 100%;
    margin: 0 auto;
}

.messages-area {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
    scroll-behavior: smooth;
}

.welcome-chat {
    max-width: 600px;
    margin: 2rem auto;
    padding: 2rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    text-align: center;
}

.welcome-chat h1, .welcome-chat h2 {
    color: var(--accent-light);
    margin-bottom: 1rem;
}

/* Messages - единый стиль */
.message {
    max-width: 90%;
    margin-bottom: 1rem;
    animation: fadeIn 0.2s ease;
}

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

.message-user {
    margin-left: auto;
}

.message-assistant {
    margin-right: auto;
}

.message-content {
    padding: 0.75rem 1rem;
    border-radius: var(--radius-lg);
    border: 1px solid var(--border-light);
    line-height: 1.6;
    font-size: 0.9375rem;
}

.message-user .message-content {
    background: var(--message-user-bg);
    border-bottom-right-radius: var(--radius-sm);
}

.message-assistant .message-content {
    background: var(--message-assistant-bg);
    border-bottom-left-radius: var(--radius-sm);
}

/* Inline plant images */
.plant-citation {
    display: inline-block;
    vertical-align: middle;
    margin: 0 2px;
}

.inline-plant-image {
    height: auto;
    max-height: 120px;
    width: auto;
    border-radius: var(--radius-sm);
    vertical-align: middle;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
    border: 1px solid var(--border-color);
}

.inline-plant-image:hover {
    transform: scale(1.05);
    box-shadow: var(--shadow-md);
}

/* Markdown в сообщениях */
.message-content h1,
.message-content h2,
.message-content h3 {
    margin-top: 1rem;
    margin-bottom: 0.5rem;
    color: var(--text-primary);
}

.message-content h1:first-child,
.message-content h2:first-child,
.message-content h3:first-child {
    margin-top: 0;
}

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

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

.message-content ul,
.message-content ol {
    margin-left: 1.25rem;
    margin-bottom: 0.5rem;
}

.message-content li {
    margin-bottom: 0.25rem;
}

.message-content code {
    font-family: var(--font-mono);
    background: rgba(255, 255, 255, 0.1);
    padding: 0.125rem 0.375rem;
    border-radius: var(--radius-sm);
    font-size: 0.875em;
}

.message-content pre {
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    padding: 0.75rem 1rem;
    border-radius: var(--radius-md);
    overflow-x: auto;
    margin: 0.5rem 0;
}

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

.message-content .error {
    color: var(--error-color);
}

/* Chat Input */
.chat-input-form {
    padding: 1rem 1.5rem;
    border-top: 1px solid var(--border-color);
    background: var(--bg-primary);
}

.input-wrapper {
    display: flex;
    align-items: flex-end;
    gap: 0.75rem;
    max-width: 800px;
    margin: 0 auto;
}

.input-wrapper textarea {
    flex: 1;
    padding: 0.625rem 1rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    font-size: 0.9375rem;
    font-family: inherit;
    resize: none;
    min-height: 40px;
    max-height: 200px;
    line-height: 1.4;
    background: var(--bg-secondary);
    color: var(--text-primary);
}

.input-wrapper textarea::placeholder {
    color: var(--text-muted);
}

.input-wrapper textarea:focus {
    outline: none;
    border-color: var(--accent-color);
    box-shadow: 0 0 0 2px rgba(74, 157, 91, 0.2);
}

.readonly-notice {
    padding: 1rem 1.5rem;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border-color);
    text-align: center;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    flex-wrap: wrap;
    font-size: 0.875rem;
}

/* Loading indicator */
.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 0.5rem 0;
}

.typing-indicator span {
    width: 6px;
    height: 6px;
    background: var(--accent-color);
    border-radius: 50%;
    animation: typing 1.4s infinite;
}

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

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

/* Session invalidated notice */
.session-notice {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
}

.session-notice-content {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 2rem;
    text-align: center;
    max-width: 400px;
}

.session-notice-content h3 {
    color: var(--accent-light);
    margin-bottom: 1rem;
}

.session-notice-content p {
    color: var(--text-secondary);
    margin-bottom: 1.5rem;
}

/* Toggle sidebar button (mobile) */
.btn-toggle-sidebar {
    display: none;
    position: fixed;
    bottom: 1rem;
    left: 1rem;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--accent-color);
    color: white;
    border: none;
    font-size: 1.25rem;
    box-shadow: var(--shadow-md);
    z-index: 1000;
    cursor: pointer;
}

/* =============================================================================
   Responsive
   ============================================================================= */

@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        z-index: 200;
        transform: translateX(-100%);
        transition: transform 0.3s;
    }

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

    .btn-toggle-sidebar {
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .message {
        max-width: 95%;
    }

    .login-card {
        padding: 1.5rem;
    }

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

@media (max-width: 480px) {
    .chat-input-form {
        padding: 0.75rem;
    }

    .messages-area {
        padding: 0.75rem;
    }
}

/* =============================================================================
   Sources Section
   ============================================================================= */

.sources-section {
    margin-top: 1.5rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border-color);
}

.sources-section h4 {
    font-size: 0.875rem;
    color: var(--text-secondary);
    margin-bottom: 0.75rem;
    font-weight: 500;
}

.sources-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.source-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.5rem;
    background: var(--bg-tertiary);
    border-radius: var(--radius-md);
    border: 1px solid var(--border-light);
}

.source-citations {
    font-size: 0.75rem;
    color: var(--text-muted);
    min-width: 3rem;
    font-family: var(--font-mono);
}

.source-image {
    width: 60px;
    height: 60px;
    object-fit: cover;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-color);
    flex-shrink: 0;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
}

.source-image:hover {
    transform: scale(1.1);
    box-shadow: var(--shadow-md);
}

.source-name {
    font-size: 0.8125rem;
    color: var(--text-primary);
    line-height: 1.4;
}

/* Убираем старые inline-plant-image стили если есть */
.plant-citation,
.inline-plant-image {
    display: none !important;
}

/* =============================================================================
   Image Preview Popup (hover)
   ============================================================================= */

.image-preview-popup {
    position: fixed;
    z-index: 10000;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.2s ease;
    padding: 8px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    max-width: 90vw;
    max-height: 90vh;
}

.image-preview-popup.visible {
    opacity: 1;
}

.image-preview-popup img {
    display: block;
    max-width: 600px;
    max-height: 80vh;
    width: auto;
    height: auto;
    border-radius: var(--radius-md);
}

/* Добавляем cursor pointer для source-image чтобы было понятно что можно навести */
.source-image {
    cursor: zoom-in;
}


