:root {
    --primary-color: #29b6f6;
    --primary-dark: #0288d1;
    --background: #ffffff;
    --surface: #f5f5f5;
    --surface-hover: #e8e8e8;
    --text-primary: #000000;
    --text-secondary: #757575;
    --border-color: #e0e0e0;
    --message-out: #dcf8c6;
    --message-in: #ffffff;
    --danger: #f44336;
    --success: #4caf50;
    --warning: #ff9800;
    --sidebar-width: 360px;
    --header-height: 60px;
}

[data-theme="dark"] {
    --background: #0e1621;
    --surface: #17212b;
    --surface-hover: #242f3d;
    --text-primary: #ffffff;
    --text-secondary: #aaaaaa;
    --border-color: #2b3942;
    --message-out: #0b5e2d;
    --message-in: #17212b;
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background: var(--background);
    color: var(--text-primary);
    overflow: hidden;
}

/* Login Page */
.login-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    overflow: auto;
}

.login-container {
    width: 100%;
    max-width: 420px;
    padding: 20px;
}

.login-box {
    background: var(--background);
    border-radius: 12px;
    padding: 40px;
    box-shadow: 0 10px 40px rgba(0,0,0,0.2);
}

.login-header {
    text-align: center;
    margin-bottom: 30px;
}

.telegram-icon {
    width: 80px;
    height: 80px;
    color: var(--primary-color);
    margin-bottom: 20px;
}

.login-header h1 {
    font-size: 28px;
    margin-bottom: 8px;
}

.login-header p {
    color: var(--text-secondary);
    font-size: 14px;
}

.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    margin-bottom: 8px;
    font-weight: 500;
    font-size: 14px;
}

.form-group input {
    width: 100%;
    padding: 12px 16px;
    border: 2px solid var(--border-color);
    border-radius: 8px;
    font-size: 15px;
    background: var(--background);
    color: var(--text-primary);
    transition: border-color 0.2s;
}

.form-group input:focus {
    outline: none;
    border-color: var(--primary-color);
}

.btn {
    width: 100%;
    padding: 12px 24px;
    border: none;
    border-radius: 8px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

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

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

.btn-secondary {
    background: var(--surface);
    color: var(--text-primary);
}

.btn-secondary:hover:not(:disabled) {
    background: var(--surface-hover);
}

.btn-danger {
    background: var(--danger);
    color: white;
}

.btn-danger:hover:not(:disabled) {
    background: #d32f2f;
}

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

.error-message {
    background: #ffebee;
    color: var(--danger);
    padding: 12px 16px;
    border-radius: 8px;
    margin-bottom: 16px;
    font-size: 14px;
}

.login-footer {
    margin-top: 24px;
    text-align: center;
    font-size: 13px;
    color: var(--text-secondary);
}

.login-footer p {
    margin: 8px 0;
}

.login-footer .warning {
    color: var(--warning);
}

.spinner {
    width: 20px;
    height: 20px;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Main App Container */
.app-container {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* Sidebar */
.sidebar {
    width: var(--sidebar-width);
    background: var(--surface);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    transition: transform 0.3s;
}

.sidebar-header {
    height: var(--header-height);
    padding: 0 16px;
    display: flex;
    align-items: center;
    gap: 12px;
    border-bottom: 1px solid var(--border-color);
}

.sidebar-header h2 {
    flex: 1;
    font-size: 20px;
    font-weight: 600;
}

.menu-toggle {
    display: none;
    background: none;
    border: none;
    cursor: pointer;
    padding: 8px;
    color: var(--text-primary);
}

.menu-toggle svg {
    width: 24px;
    height: 24px;
}

.btn-icon {
    background: none;
    border: none;
    cursor: pointer;
    padding: 8px;
    border-radius: 50%;
    color: var(--text-primary);
    transition: background 0.2s;
}

.btn-icon:hover {
    background: var(--surface-hover);
}

.btn-icon svg {
    width: 24px;
    height: 24px;
    display: block;
}

.sidebar-search {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-color);
}

.sidebar-search input {
    width: 100%;
    padding: 10px 16px;
    border: none;
    border-radius: 20px;
    background: var(--background);
    color: var(--text-primary);
    font-size: 14px;
}

.sidebar-search input:focus {
    outline: none;
}

.conversations-list {
    flex: 1;
    overflow-y: auto;
}

.conversation-item {
    padding: 12px 16px;
    display: flex;
    align-items: center;
    gap: 12px;
    cursor: pointer;
    border-bottom: 1px solid var(--border-color);
    transition: background 0.2s;
}

.conversation-item:hover {
    background: var(--surface-hover);
}

.conversation-item.active {
    background: var(--surface-hover);
}

.user-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--primary-color);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 18px;
    color: white;
    flex-shrink: 0;
}

.conversation-info {
    flex: 1;
    min-width: 0;
}

.conversation-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 4px;
}

.conversation-name {
    font-weight: 600;
    font-size: 15px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.conversation-time {
    font-size: 12px;
    color: var(--text-secondary);
    flex-shrink: 0;
    margin-left: 8px;
}

.conversation-preview {
    display: flex;
    align-items: center;
    gap: 4px;
}

.conversation-message {
    font-size: 14px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
}

.unread-badge {
    background: var(--primary-color);
    color: white;
    border-radius: 12px;
    padding: 2px 8px;
    font-size: 12px;
    font-weight: 600;
    min-width: 20px;
    text-align: center;
}

/* Main Content */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--background);
}

.welcome-screen {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    color: var(--text-secondary);
}

.welcome-icon {
    width: 120px;
    height: 120px;
    opacity: 0.3;
}

.welcome-screen h2 {
    font-size: 24px;
    color: var(--text-primary);
}

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

.chat-header {
    height: var(--header-height);
    padding: 0 16px;
    display: flex;
    align-items: center;
    gap: 12px;
    border-bottom: 1px solid var(--border-color);
    background: var(--surface);
}

.back-btn {
    display: none;
    background: none;
    border: none;
    cursor: pointer;
    padding: 8px;
    color: var(--text-primary);
}

.back-btn svg {
    width: 24px;
    height: 24px;
}

.chat-header-info {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
}

.chat-header-info .user-avatar {
    width: 40px;
    height: 40px;
    font-size: 16px;
}

.user-details {
    flex: 1;
    min-width: 0;
}

.user-details h3 {
    font-size: 16px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.user-details p {
    font-size: 13px;
    color: var(--text-secondary);
}

.chat-header-actions {
    display: flex;
    gap: 4px;
}

/* Messages Container */
.messages-container {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
}

.load-more-container {
    text-align: center;
    padding: 16px;
}

.btn-load-more {
    padding: 8px 24px;
    background: var(--surface);
    border: 1px solid var(--border-color);
    border-radius: 20px;
    cursor: pointer;
    color: var(--text-primary);
    font-size: 14px;
    transition: background 0.2s;
}

.btn-load-more:hover {
    background: var(--surface-hover);
}

.messages-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.message {
    max-width: 65%;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.message.incoming {
    align-self: flex-start;
}

.message.outgoing {
    align-self: flex-end;
}

.message-bubble {
    padding: 8px 12px;
    border-radius: 12px;
    word-wrap: break-word;
    box-shadow: 0 1px 2px rgba(0,0,0,0.1);
}

.message.incoming .message-bubble {
    background: var(--message-in);
    border-bottom-left-radius: 4px;
}

.message.outgoing .message-bubble {
    background: var(--message-out);
    border-bottom-right-radius: 4px;
}

.message-text {
    font-size: 15px;
    line-height: 1.4;
    white-space: pre-wrap;
}

.message-media {
    max-width: 100%;
    border-radius: 8px;
    margin-bottom: 4px;
}

.message-media img,
.message-media video {
    max-width: 100%;
    border-radius: 8px;
    display: block;
}

.message-file {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px;
    background: var(--surface);
    border-radius: 8px;
    margin-bottom: 4px;
}

.file-icon {
    width: 40px;
    height: 40px;
    background: var(--primary-color);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
}

.file-info {
    flex: 1;
    min-width: 0;
}

.file-name {
    font-size: 14px;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.file-size {
    font-size: 12px;
    color: var(--text-secondary);
}

.message-time {
    font-size: 11px;
    color: var(--text-secondary);
    align-self: flex-end;
    margin-top: 2px;
}

/* Message Input */
.message-input-container {
    padding: 12px 16px;
    border-top: 1px solid var(--border-color);
    background: var(--surface);
    display: flex;
    align-items: flex-end;
    gap: 8px;
}

.attach-btn {
    flex-shrink: 0;
}

.message-input-wrapper {
    flex: 1;
    background: var(--background);
    border-radius: 20px;
    padding: 8px 16px;
}

.message-input-wrapper textarea {
    width: 100%;
    border: none;
    background: none;
    resize: none;
    font-size: 15px;
    color: var(--text-primary);
    font-family: inherit;
    max-height: 120px;
}

.message-input-wrapper textarea:focus {
    outline: none;
}

.btn-send {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: none;
    background: var(--primary-color);
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
    flex-shrink: 0;
}

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

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

.btn-send svg {
    width: 20px;
    height: 20px;
}

/* User Info Panel */
.user-info-panel {
    width: 360px;
    background: var(--surface);
    border-left: 1px solid var(--border-color);
    display: none;
    flex-direction: column;
}

.user-info-panel.active {
    display: flex;
}

.user-info-header {
    height: var(--header-height);
    padding: 0 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--border-color);
}

.user-info-header h3 {
    font-size: 18px;
}

.user-info-content {
    flex: 1;
    overflow-y: auto;
    padding: 24px 16px;
}

.user-profile {
    text-align: center;
    margin-bottom: 24px;
}

.user-profile .user-avatar {
    width: 100px;
    height: 100px;
    font-size: 40px;
    margin: 0 auto 16px;
}

.user-profile h3 {
    font-size: 20px;
    margin-bottom: 4px;
}

.user-profile p {
    color: var(--text-secondary);
    font-size: 14px;
}

.info-group {
    margin-bottom: 24px;
}

.info-group h4 {
    font-size: 14px;
    color: var(--text-secondary);
    margin-bottom: 12px;
    text-transform: uppercase;
    font-weight: 600;
}

.info-item {
    display: flex;
    justify-content: space-between;
    padding: 12px 0;
    border-bottom: 1px solid var(--border-color);
}

.info-label {
    color: var(--text-secondary);
    font-size: 14px;
}

.info-value {
    font-weight: 500;
    font-size: 14px;
}

/* Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 20px;
}

.modal-content {
    background: var(--background);
    border-radius: 12px;
    max-width: 600px;
    width: 100%;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 10px 40px rgba(0,0,0,0.3);
}

.modal-small {
    max-width: 400px;
}

.modal-header {
    padding: 20px 24px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.modal-header h3 {
    font-size: 20px;
}

.modal-body {
    padding: 24px;
    overflow-y: auto;
}

.settings-group {
    margin-bottom: 32px;
}

.settings-group:last-child {
    margin-bottom: 0;
}

.settings-group h4 {
    font-size: 16px;
    margin-bottom: 16px;
    font-weight: 600;
}

.setting-item {
    padding: 12px 0;
    border-bottom: 1px solid var(--border-color);
}

.setting-item:last-child {
    border-bottom: none;
}

.setting-item label {
    display: flex;
    align-items: center;
    justify-content: space-between;
    cursor: pointer;
}

.setting-item input[type="checkbox"] {
    width: 48px;
    height: 24px;
    cursor: pointer;
    appearance: none;
    background: var(--border-color);
    border-radius: 12px;
    position: relative;
    transition: background 0.2s;
}

.setting-item input[type="checkbox"]:checked {
    background: var(--primary-color);
}

.setting-item input[type="checkbox"]::before {
    content: '';
    position: absolute;
    width: 20px;
    height: 20px;
    background: white;
    border-radius: 50%;
    top: 2px;
    left: 2px;
    transition: transform 0.2s;
}

.setting-item input[type="checkbox"]:checked::before {
    transform: translateX(24px);
}

.webhook-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin-bottom: 16px;
}

.webhook-result {
    padding: 12px;
    border-radius: 8px;
    font-size: 13px;
    margin-top: 12px;
}

.webhook-result.success {
    background: rgba(76, 175, 80, 0.1);
    color: var(--success);
}

.webhook-result.error {
    background: rgba(244, 67, 54, 0.1);
    color: var(--danger);
}

/* Loading */
.loading-spinner {
    text-align: center;
    padding: 40px 20px;
    color: var(--text-secondary);
}

.loading-spinner .spinner {
    width: 40px;
    height: 40px;
    margin: 0 auto 16px;
    stroke: var(--primary-color);
    stroke-width: 2;
}

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

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

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

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

/* Mobile Responsive */
@media (max-width: 768px) {
    :root {
        --sidebar-width: 100%;
    }

    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        z-index: 100;
        transform: translateX(-100%);
    }

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

    .menu-toggle,
    .back-btn {
        display: block;
    }

    .chat-header-info .user-avatar {
        display: none;
    }

    .message {
        max-width: 85%;
    }

    .user-info-panel {
        position: fixed;
        right: 0;
        top: 0;
        bottom: 0;
        z-index: 100;
        transform: translateX(100%);
        transition: transform 0.3s;
    }

    .user-info-panel.active {
        transform: translateX(0);
    }
}

@media (max-width: 480px) {
    .login-box {
        padding: 24px;
    }

    .modal-content {
        border-radius: 0;
        max-height: 100vh;
        height: 100vh;
    }

    .webhook-actions {
        flex-direction: column;
    }

    .webhook-actions .btn {
        width: 100%;
    }
}
