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

html {
    scroll-behavior: smooth;
    --primary: #667eea;
    --primary-dark: #5568d3;
    --bg-light: #fff;
    --bg-dark: #1a1a1a;
    --text-light: #333;
    --text-dark: #e0e0e0;
    --border-light: #e0e0e0;
    --border-dark: #333;
    --chat-bg-light: #f8f9fa;
    --chat-bg-dark: #121212;
    --msg-own-light: #667eea;
    --msg-own-dark: #5568d3;
    --msg-other-light: #fff;
    --msg-other-dark: #2a2a2a;
}

html[data-theme="dark"] {
    color-scheme: dark;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-light);
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    color: var(--text-light);
    transition: background 0.3s, color 0.3s;
}

html[data-theme="dark"] body {
    background: var(--bg-dark);
    color: var(--text-dark);
}

/* === ЭКРАНЫ === */
#loginScreen,
#roomListScreen,
#chatScreen {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    flex-direction: column;
}

#loginScreen.active,
#roomListScreen.active,
#chatScreen.active {
    display: flex;
}

/* === ЭКРАН ВХОДА === */
#loginScreen {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    justify-content: center;
    align-items: center;
    padding: 20px;
}

.login-container {
    background: var(--bg-light);
    border-radius: 16px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
    padding: 40px 24px;
    width: 100%;
    max-width: 380px;
    transition: background 0.3s;
}

html[data-theme="dark"] .login-container {
    background: #2a2a2a;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

.logo {
    font-size: 64px;
    text-align: center;
    margin-bottom: 16px;
    display: block;
}

.login-container h1 {
    text-align: center;
    color: var(--text-light);
    margin-bottom: 32px;
    font-size: 28px;
    font-weight: 700;
    transition: color 0.3s;
}

html[data-theme="dark"] .login-container h1 {
    color: var(--text-dark);
}

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

input[type="text"],
input[type="password"] {
    width: 100%;
    padding: 14px 16px;
    border: 2px solid var(--border-light);
    border-radius: 10px;
    font-size: 16px;
    font-family: inherit;
    transition: all 0.3s;
    background: var(--bg-light);
    color: var(--text-light);
    -webkit-appearance: none;
    appearance: none;
}

html[data-theme="dark"] input[type="text"],
html[data-theme="dark"] input[type="password"] {
    background: #333;
    color: var(--text-dark);
    border-color: var(--border-dark);
}

input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.1);
}

input::placeholder {
    color: #999;
}

/* === КНОПКИ === */
.btn {
    border: none;
    border-radius: 10px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    font-family: inherit;
    -webkit-appearance: none;
    appearance: none;
}

.btn-primary {
    background: var(--primary);
    color: white;
    padding: 12px 24px;
}

.btn-primary:active {
    background: var(--primary-dark);
    transform: scale(0.98);
}

.btn-primary:disabled {
    background: #ccc;
    cursor: not-allowed;
}

.btn-secondary {
    background: var(--border-light);
    color: var(--text-light);
    padding: 12px 24px;
    transition: background 0.3s;
}

html[data-theme="dark"] .btn-secondary {
    background: #333;
    color: var(--text-dark);
}

.btn-secondary:active {
    background: #d0d0d0;
}

html[data-theme="dark"] .btn-secondary:active {
    background: #444;
}

.btn-large {
    width: 100%;
    padding: 16px;
    font-size: 18px;
}

.btn-icon {
    width: 44px;
    height: 44px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    background: transparent;
    color: inherit;
    border-radius: 50%;
}

.btn-icon:active {
    background: rgba(0, 0, 0, 0.1);
}

html[data-theme="dark"] .btn-icon:active {
    background: rgba(255, 255, 255, 0.1);
}

.btn-send {
    width: 44px;
    height: 44px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    background: var(--primary);
    color: white;
    border-radius: 50%;
    flex-shrink: 0;
}

.btn-send:active {
    background: var(--primary-dark);
    transform: scale(0.95);
}

.error {
    margin-top: 16px;
    padding: 12px 16px;
    background: #f8d7da;
    color: #721c24;
    border-radius: 8px;
    font-size: 14px;
    border: 1px solid #f5c6cb;
}

.error.hidden {
    display: none;
}

/* === МОДАЛЬНОЕ ОКНО === */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: flex-end;
    z-index: 1000;
}

.modal.hidden {
    display: none !important;
}

.modal-content {
    background: var(--bg-light);
    border-radius: 20px 20px 0 0;
    padding: 0;
    width: 100%;
    max-width: 100%;
    max-height: 90vh;
    overflow-y: auto;
    animation: slideUp 0.3s ease-out;
    transition: background 0.3s;
}

html[data-theme="dark"] .modal-content {
    background: #2a2a2a;
}

.modal-small {
    max-width: 100%;
}

@keyframes slideUp {
    from {
        transform: translateY(100%);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.modal-header {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border-light);
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: sticky;
    top: 0;
    background: var(--bg-light);
    transition: background 0.3s, border-color 0.3s;
}

html[data-theme="dark"] .modal-header {
    background: #2a2a2a;
    border-bottom-color: var(--border-dark);
}

.modal-header h3 {
    margin: 0;
    font-size: 18px;
    color: var(--text-light);
    flex: 1;
    transition: color 0.3s;
}

html[data-theme="dark"] .modal-header h3 {
    color: var(--text-dark);
}

.modal-close {
    background: none;
    border: none;
    font-size: 28px;
    cursor: pointer;
    color: #999;
    padding: 0;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-close:active {
    color: var(--text-light);
}

html[data-theme="dark"] .modal-close:active {
    color: var(--text-dark);
}

.modal-body {
    padding: 20px;
}

.modal-body input {
    width: 100%;
}

.modal-footer {
    padding: 16px 20px;
    border-top: 1px solid var(--border-light);
    display: flex;
    gap: 12px;
    justify-content: flex-end;
    position: sticky;
    bottom: 0;
    background: var(--bg-light);
    transition: background 0.3s, border-color 0.3s;
}

html[data-theme="dark"] .modal-footer {
    background: #2a2a2a;
    border-top-color: var(--border-dark);
}

.modal-footer .btn {
    flex: 1;
    padding: 12px 16px;
}

/* === ЭКРАН СПИСКА === */
.app-container {
    width: 100%;
    height: 100%;
    background: var(--bg-light);
    display: flex;
    flex-direction: column;
    transition: background 0.3s;
}

html[data-theme="dark"] .app-container {
    background: var(--bg-dark);
}

.header {
    background: var(--primary);
    color: white;
    padding: 12px 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
    padding-top: max(12px, env(safe-area-inset-top));
}

.header h1,
.header h2 {
    margin: 0;
    font-size: 24px;
    font-weight: 700;
}

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

.room-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    -webkit-overflow-scrolling: touch;
}

.room-item {
    padding: 12px 16px;
    background: var(--bg-light);
    border: 1px solid var(--border-light);
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    gap: 12px;
    color: var(--text-light);
}

html[data-theme="dark"] .room-item {
    background: #2a2a2a;
    border-color: var(--border-dark);
    color: var(--text-dark);
}

.room-item:active {
    background: #f8f9fa;
    transform: scale(0.98);
}

html[data-theme="dark"] .room-item:active {
    background: #333;
}

.room-item-content {
    display: flex;
    align-items: center;
    gap: 12px;
    flex: 1;
    min-width: 0;
}

.room-avatar,
.room-avatar-placeholder {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    background: #f0f0f0;
    color: #333;
    font-size: 20px;
    transition: background 0.3s;
}

html[data-theme="dark"] .room-avatar-placeholder {
    background: #444;
    color: var(--text-dark);
}

.room-avatar {
    object-fit: cover;
}

.room-item-text {
    flex: 1;
    min-width: 0;
}

.room-item-name {
    font-weight: 600;
    color: var(--text-light);
    margin-bottom: 4px;
    font-size: 16px;
    transition: color 0.3s;
}

html[data-theme="dark"] .room-item-name {
    color: var(--text-dark);
}

.room-item-preview {
    font-size: 13px;
    color: #888;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.room-delete-btn {
    width: 36px;
    height: 36px;
    border: none;
    background: transparent;
    border-radius: 50%;
    cursor: pointer;
    font-size: 18px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.room-delete-btn:active {
    background: rgba(220, 53, 69, 0.2);
}

.loading {
    text-align: center;
    color: #888;
    padding: 40px 20px;
    font-size: 14px;
}

/* === ЭКРАН ЧАТА === */
.chat-container {
    width: 100%;
    height: 100%;
    background: var(--chat-bg-light);
    display: flex;
    flex-direction: column;
    transition: background 0.3s;
}

html[data-theme="dark"] .chat-container {
    background: var(--chat-bg-dark);
}

.chat-header {
    background: var(--primary);
    color: white;
    padding: 12px 16px;
    display: flex;
    align-items: center;
    gap: 12px;
    flex-shrink: 0;
    padding-top: max(12px, env(safe-area-inset-top));
}

.chat-header h2 {
    margin: 0;
    font-size: 18px;
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.message-timeline {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    -webkit-overflow-scrolling: touch;
}

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

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

/* === СООБЩЕНИЯ === */
.message {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    animation: fadeInUp 0.3s ease-out;
}

.message.deleting {
    animation: fadeOutDown 0.3s ease-in forwards;
}

.message.own {
    justify-content: flex-end;
}

.message.other {
    justify-content: flex-start;
}

.message.own .message-content {
    background: var(--msg-own-light);
    color: white;
    border-bottom-right-radius: 4px;
}

html[data-theme="dark"] .message.own .message-content {
    background: var(--msg-own-dark);
}

.message.other .message-content {
    background: var(--msg-other-light);
    color: var(--text-light);
    border-bottom-left-radius: 4px;
}

html[data-theme="dark"] .message.other .message-content {
    background: var(--msg-other-dark);
    color: var(--text-dark);
}

.message-avatar,
.message-avatar-placeholder {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    background: white;
    color: #333;
    font-size: 14px;
    border: 1px solid var(--border-light);
    transition: background 0.3s;
}

html[data-theme="dark"] .message-avatar-placeholder {
    background: #444;
    color: var(--text-dark);
    border-color: var(--border-dark);
}

.message-avatar {
    object-fit: cover;
}

.message-content {
    padding: 12px 16px;
    border-radius: 12px;
    word-wrap: break-word;
    line-height: 1.4;
    font-size: 15px;
    max-width: 75%;
}

/* === ИЗОБРАЖЕНИЯ В ЧАТЕ === */
.message-image {
    max-width: 75%;
    max-height: 300px;
    border-radius: 12px;
    object-fit: cover;
    animation: fadeInUp 0.3s ease-out;
}

.message.own .message-image {
    border-bottom-right-radius: 4px;
}

.message.other .message-image {
    border-bottom-left-radius: 4px;
}

.message-encrypted {
    font-size: 12px;
    opacity: 0.7;
    margin-left: 4px;
}

.message-input-container {
    padding: 12px 16px;
    background: var(--bg-light);
    border-top: 1px solid var(--border-light);
    flex-shrink: 0;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
    transition: background 0.3s, border-color 0.3s;
}

html[data-theme="dark"] .message-input-container {
    background: #2a2a2a;
    border-top-color: var(--border-dark);
}

#messageForm {
    display: flex;
    gap: 8px;
    align-items: center;
}

#messageInput {
    flex: 1;
    padding: 12px 16px;
    border: 2px solid var(--border-light);
    border-radius: 24px;
    font-size: 16px;
    font-family: inherit;
    background: var(--msg-other-light);
    color: var(--text-light);
    transition: all 0.3s;
    -webkit-appearance: none;
    appearance: none;
}

html[data-theme="dark"] #messageInput {
    background: #333;
    color: var(--text-dark);
    border-color: var(--border-dark);
}

#messageInput:focus {
    outline: none;
    border-color: var(--primary);
}

/* === АДАПТИВНОСТЬ === */
@media (min-width: 768px) {
    .modal {
        align-items: center;
    }

    .modal-content {
        border-radius: 16px;
        width: auto;
        max-width: 450px;
        max-height: 80vh;
    }

    .app-container,
    .chat-container {
        max-width: 800px;
        margin: 0 auto;
        border: 1px solid var(--border-light);
        height: 100vh;
    }

    html[data-theme="dark"] .app-container,
    html[data-theme="dark"] .chat-container {
        border-color: var(--border-dark);
    }

    .header,
    .chat-header {
        padding-top: 12px;
    }

    .room-item:hover {
        background: #f8f9fa;
    }

    html[data-theme="dark"] .room-item:hover {
        background: #333;
    }

    #messageInput {
        border-radius: 10px;
    }

    .message-content,
    .message-image {
        max-width: 60%;
    }
}

@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}
