* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

:root {
    --bg: #FAFAF7;
    --surface: #FFFFFF;
    --ink: #1a1a17;
    --muted: #8A867F;
    --line: rgba(26, 26, 23, 0.08);
    --line-soft: rgba(26, 26, 23, 0.04);
    --accent: #2a5A3D;
    --accent-soft: #E3EDDF;
    --red: #B23A3A;
    --red-soft: #F8ECEC;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --serif: 'Fraunces', Georgia, serif;
    --mono: 'JetBrains Mono', ui-monospace, monospace;
    --transition: 0.2s ease;
}

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    overflow: hidden;
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: var(--bg);
    color: var(--ink);
    overscroll-behavior: none;
    -webkit-font-smoothing: antialiased;
}

.screen {
    display: none;
    flex-direction: column;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    height: 100dvh;
    overflow: hidden;
    opacity: 0;
}

.screen.active {
    display: flex;
    opacity: 1;
    animation: screenIn 0.25s ease forwards;
}

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

/* === Центр (загрузка / авторизация) === */
.center-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex: 1;
    width: 100%;
    padding: 24px 20px;
    gap: 8px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.auth-center {
    justify-content: flex-start;
    padding-top: 10vh;
}

.logo {
    flex-shrink: 0;
    margin-bottom: 4px;
}

.center-container h1 {
    font-family: var(--serif);
    color: var(--ink);
    font-size: 28px;
    font-weight: 600;
    letter-spacing: -0.03em;
    margin-bottom: 4px;
}
.center-container p { color: var(--muted); font-size: 14px; letter-spacing: -0.01em; }

.spinner {
    width: 32px; height: 32px;
    border: 2px solid var(--line);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin-top: 8px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* === Авторизация === */
.auth-card {
    width: 100%; max-width: 340px;
    background: var(--surface);
    border-radius: 16px; padding: 20px;
    border: 0.5px solid var(--line);
    margin-top: 16px; flex-shrink: 0;
}

.auth-tabs {
    display: flex; margin-bottom: 16px;
    border-radius: 10px; overflow: hidden;
    background: var(--bg);
    border: 0.5px solid var(--line);
    padding: 3px; gap: 3px;
}

.auth-tab {
    flex: 1; padding: 9px; border: none;
    background: transparent; color: var(--muted);
    font-size: 14px; font-weight: 500; cursor: pointer;
    border-radius: 8px;
    transition: all var(--transition);
    font-family: inherit; letter-spacing: -0.01em;
}
.auth-tab.active {
    background: var(--surface);
    color: var(--ink);
    box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}

.auth-form { display: flex; flex-direction: column; gap: 10px; }
.auth-form input {
    padding: 13px 14px; border: 1px solid var(--line);
    border-radius: 12px; font-size: 15px; outline: none; width: 100%;
    transition: border-color var(--transition);
    background: var(--surface); color: var(--ink);
    font-family: inherit; letter-spacing: -0.01em;
}
.auth-form input:focus {
    border-color: var(--accent);
}
.btn-full { width: 100%; padding: 13px 18px; }

/* === Хедер === */
.header {
    background: var(--bg);
    color: var(--ink);
    padding: 10px 12px;
    padding-top: calc(10px + env(safe-area-inset-top, 0px));
    display: flex; align-items: center; gap: 8px;
    flex-shrink: 0; min-height: 48px;
    border-bottom: 0.5px solid var(--line);
    position: relative;
    z-index: 10;
}
.header h1 {
    font-size: 16px; font-weight: 600; flex: 1;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    letter-spacing: -0.02em; color: var(--ink);
}
.header-btn {
    color: var(--ink); font-size: 16px; flex-shrink: 0;
    opacity: 0.75;
}
.header-btn:active { opacity: 0.5; }

/* === Кнопки === */
.btn-primary {
    background: var(--accent);
    color: #fff;
    border: none; border-radius: 12px; padding: 13px 18px;
    font-size: 15px; font-weight: 500; cursor: pointer;
    white-space: nowrap; flex-shrink: 0;
    letter-spacing: -0.01em;
    transition: opacity var(--transition);
    font-family: inherit;
}
.btn-primary:active { opacity: 0.82; }

.btn-secondary {
    background: transparent; color: var(--ink);
    border: 1px solid var(--line); border-radius: 12px;
    padding: 13px 18px; font-size: 15px; font-weight: 500; cursor: pointer;
    transition: background var(--transition);
    font-family: inherit; letter-spacing: -0.01em;
}
.btn-secondary:active { background: var(--line-soft); }

.btn-danger {
    background: var(--red);
    color: #fff;
    border: none; border-radius: 12px; padding: 13px 18px;
    font-size: 15px; font-weight: 500; cursor: pointer; width: 100%;
    transition: opacity var(--transition);
    font-family: inherit;
}
.btn-danger:active { opacity: 0.82; }

.btn-danger-outline {
    background: transparent; color: var(--red);
    border: 1px solid var(--line); border-radius: 12px;
    padding: 13px 18px; font-size: 15px; font-weight: 500;
    cursor: pointer; width: 100%;
    transition: background var(--transition);
    font-family: inherit;
}
.btn-danger-outline:active { background: var(--red-soft); }

.btn-icon {
    background: none; border: none;
    cursor: pointer; padding: 6px; border-radius: 8px;
    display: flex; align-items: center; justify-content: center;
    transition: opacity var(--transition);
    color: var(--ink);
}
.btn-icon:active { opacity: 0.5; }

/* === Папки/табы === */
.folder-tabs {
    display: flex;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    background: var(--bg);
    padding: 8px 12px;
    gap: 6px;
    flex-shrink: 0;
    scrollbar-width: none;
    border-bottom: 0.5px solid var(--line);
}
.folder-tabs::-webkit-scrollbar { display: none; }

.folder-tab {
    padding: 6px 14px;
    border-radius: 20px;
    background: transparent;
    color: var(--muted);
    font-size: 13px;
    font-weight: 500;
    white-space: nowrap;
    cursor: pointer;
    flex-shrink: 0;
    user-select: none;
    border: 1px solid transparent;
    transition: all var(--transition);
    letter-spacing: -0.01em;
}
.folder-tab.active {
    background: var(--accent-soft);
    color: var(--accent);
    border-color: transparent;
}
.folder-tab-add {
    color: var(--muted);
    font-size: 16px;
    padding: 4px 12px;
}

/* === Главный экран === */
.main-content {
    flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
    padding: 16px; padding-bottom: calc(16px + var(--safe-bottom));
}

.your-id-section, .connect-section {
    background: var(--surface); border-radius: 14px;
    padding: 16px; margin-bottom: 10px;
    border: 0.5px solid var(--line);
}

.label { font-size: 12px; color: var(--muted); margin-bottom: 8px; font-weight: 500; letter-spacing: 0.02em; text-transform: uppercase; }
.text-light { color: var(--muted); font-size: 14px; }
.small { font-size: 12px; }
.hint { font-size: 12px; color: var(--muted); margin-top: 6px; }

.id-display { display: flex; align-items: center; gap: 8px; }
.id-display span {
    font-size: 20px; font-weight: 600; color: var(--ink);
    letter-spacing: 0.04em; word-break: break-all;
    font-family: var(--mono);
}

.input-row { display: flex; gap: 8px; }
.input-row input, .form-section input, .form-section textarea {
    flex: 1; min-width: 0; padding: 12px 14px;
    border: 1px solid var(--line); border-radius: 12px;
    font-size: 15px; outline: none; font-family: inherit;
    transition: border-color var(--transition);
    background: var(--surface); color: var(--ink);
    letter-spacing: -0.01em;
}
.input-row input:focus, .form-section input:focus, .form-section textarea:focus {
    border-color: var(--accent);
}
.form-section textarea { resize: vertical; min-height: 60px; }

.action-row { margin-bottom: 10px; }

.status { font-size: 13px; margin-top: 8px; min-height: 18px; letter-spacing: -0.01em; }
.status.waiting { color: #8F5A2A; }
.status.success { color: var(--accent); }
.status.error { color: var(--red); }

/* === Список чатов === */
.chats-section { margin-top: 4px; }

.chat-item {
    background: var(--surface); border-radius: 14px;
    padding: 13px 14px; margin-top: 8px;
    border: 0.5px solid var(--line);
    display: flex; align-items: center; gap: 12px;
    cursor: pointer;
    transition: background var(--transition);
}
.chat-item:active { background: var(--bg); }

.chat-item-icon {
    width: 42px; height: 42px; border-radius: 50%;
    background: var(--accent-soft);
    color: var(--accent);
    display: flex; align-items: center; justify-content: center;
    font-weight: 500; font-size: 16px; flex-shrink: 0;
    letter-spacing: -0.02em;
}

.chat-item-body { flex: 1; min-width: 0; }
.chat-item-name { font-weight: 600; font-size: 15px; color: var(--ink); letter-spacing: -0.02em; }
.chat-item-preview {
    font-size: 13px; color: var(--muted); margin-top: 2px;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    letter-spacing: -0.01em;
}
.chat-item-folder {
    font-size: 10px; color: var(--accent); margin-top: 2px;
    font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase;
}

.chat-item-badge {
    min-width: 20px; height: 20px; padding: 0 5px;
    border-radius: 10px;
    background: var(--accent);
    color: #fff;
    font-size: 11px; font-weight: 600;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
    font-family: var(--mono);
}

.chat-item-arrow { color: var(--muted); flex-shrink: 0; opacity: 0.5; }
.no-chats { text-align: center; color: var(--muted); font-size: 14px; padding: 24px; letter-spacing: -0.01em; }

.chat-item-draft {
    font-size: 13px; color: var(--red); margin-top: 2px;
    font-style: italic;
}

/* === Участники === */
.members-list { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.member-chip {
    display: flex; align-items: center; gap: 4px;
    background: var(--accent-soft);
    color: var(--accent);
    padding: 5px 10px; border-radius: 20px; font-size: 13px; font-weight: 500;
    letter-spacing: -0.01em;
}
.member-chip .remove-member {
    background: none; border: none; color: var(--muted);
    font-size: 14px; cursor: pointer; padding: 0 2px; line-height: 1;
}
.member-chip.creator { border: 1px solid var(--accent); }

/* === Форма === */
.form-section {
    background: var(--surface); border-radius: 14px;
    padding: 16px; margin-bottom: 10px;
    border: 0.5px solid var(--line);
}
.form-section input, .form-section textarea { width: 100%; margin-bottom: 8px; }
.form-section input:last-child, .form-section textarea:last-child { margin-bottom: 0; }

.group-info-header h2 { color: var(--ink); font-size: 20px; margin-bottom: 4px; letter-spacing: -0.02em; font-family: var(--serif); }
.info-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }

/* === Профиль === */
.profile-value {
    font-size: 17px; font-weight: 600; color: var(--ink);
    letter-spacing: -0.02em;
}
.verified-badge {
    font-size: 13px; color: var(--accent); margin-bottom: 8px; font-weight: 500;
    letter-spacing: -0.01em;
}

/* === Чат === */
.chat-header { gap: 8px; }
.chat-header .btn-icon { color: var(--ink); }
.chat-header-info { flex: 1; min-width: 0; cursor: pointer; }
.chat-header-info h1 { font-size: 16px; letter-spacing: -0.02em; }
.chat-subtitle { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; letter-spacing: -0.01em; }

/* === Поиск по чату === */
.search-bar {
    background: var(--surface);
    border-bottom: 0.5px solid var(--line);
    flex-shrink: 0;
    animation: slideDown 0.2s ease forwards;
}
@keyframes slideDown {
    from { opacity: 0; transform: translateY(-6px); }
    to { opacity: 1; transform: translateY(0); }
}

.search-bar-row {
    display: flex; gap: 6px; padding: 8px 10px;
    align-items: center;
}
.search-bar-row input {
    flex: 1; min-width: 0; padding: 8px 12px;
    border: 1px solid var(--line); border-radius: 10px;
    font-size: 14px; outline: none; font-family: inherit;
    background: var(--bg); color: var(--ink);
    transition: border-color var(--transition);
}
.search-bar-row input:focus { border-color: var(--accent); }
.search-bar-row select {
    padding: 8px 6px;
    border: 1px solid var(--line); border-radius: 10px;
    font-size: 13px; outline: none; background: var(--bg); color: var(--ink);
    max-width: 120px;
    transition: border-color var(--transition);
    font-family: inherit;
}
.search-bar-row select:focus { border-color: var(--accent); }
.search-bar-row .btn-icon { color: var(--muted); }

.search-results {
    max-height: 200px; overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}
.search-result-item {
    padding: 8px 12px;
    border-bottom: 0.5px solid var(--line);
    cursor: pointer;
    font-size: 13px;
    transition: background var(--transition);
}
.search-result-item:active { background: var(--bg); }
.search-result-sender { font-weight: 600; color: var(--accent); font-size: 12px; letter-spacing: -0.01em; }
.search-result-text { color: var(--ink); margin-top: 2px; }
.search-result-text mark { background: rgba(42,90,61,0.15); border-radius: 3px; padding: 0 2px; }
.search-result-time { color: var(--muted); font-size: 11px; margin-top: 2px; }
.search-no-results { padding: 12px; text-align: center; color: var(--muted); font-size: 13px; }

/* === Закреплённые сообщения === */
.pinned-bar {
    background: var(--surface);
    border-bottom: 0.5px solid var(--line);
    flex-shrink: 0;
}
.pinned-bar-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 6px 12px;
    font-size: 13px; font-weight: 500; color: var(--muted);
    letter-spacing: -0.01em;
}
.pinned-toggle { font-size: 12px; color: var(--muted); padding: 2px 6px; }

.pinned-messages { padding: 0 12px 8px; }
.pinned-messages.collapsed { display: none; }

.pinned-item {
    padding: 6px 10px; margin-top: 4px;
    background: var(--bg); border-radius: 10px;
    border-left: 2px solid var(--accent);
    font-size: 13px; cursor: pointer;
    display: flex; align-items: center; gap: 8px;
    transition: background var(--transition);
}
.pinned-item:active { background: var(--accent-soft); }
.pinned-item-text { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pinned-item-sender { font-weight: 600; color: var(--accent); font-size: 11px; letter-spacing: -0.01em; }
.pinned-item .btn-unpin {
    background: none; border: none; font-size: 14px; color: var(--muted);
    cursor: pointer; padding: 2px 4px; flex-shrink: 0;
}
.pinned-item .btn-unpin:active { color: var(--red); }

/* === Сообщения === */
.messages {
    flex: 1; min-height: 0; overflow-y: auto;
    -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
    padding: 12px; display: flex; flex-direction: column; gap: 4px;
    background: var(--bg);
}

.message {
    max-width: 82%; padding: 9px 12px;
    border-radius: 16px; font-size: 15px; line-height: 1.45;
    word-wrap: break-word; overflow-wrap: break-word;
    position: relative;
    animation: msgIn 0.2s ease forwards;
    letter-spacing: -0.01em;
}
@keyframes msgIn {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: translateY(0); }
}

.message.mine {
    align-self: flex-end;
    background: var(--accent);
    color: #fff; border-bottom-right-radius: 4px;
}
.message.theirs {
    align-self: flex-start;
    background: var(--surface);
    color: var(--ink); border-bottom-left-radius: 4px;
    border: 0.5px solid var(--line);
}
.message .msg-sender {
    font-size: 11px; font-weight: 600; color: var(--accent);
    margin-bottom: 2px; letter-spacing: -0.01em;
}
.message.mine .msg-sender { color: rgba(255,255,255,0.75); }
.message .time { font-size: 10px; opacity: 0.6; margin-top: 3px; text-align: right; font-family: var(--mono); }
.message .edited-mark { font-size: 10px; opacity: 0.55; font-style: italic; }
.message.system {
    align-self: center; background: transparent; color: var(--muted);
    font-size: 12px; text-align: center; padding: 4px 8px; max-width: 90%;
    border: none;
}
.message.deleted { opacity: 0.45; font-style: italic; }
.message.highlight { animation: msg-highlight 2s ease-out; }
@keyframes msg-highlight {
    0% { box-shadow: 0 0 0 2px var(--accent); }
    100% { box-shadow: none; }
}

/* === Статус сообщения (галочки) === */
.msg-status {
    display: inline-block;
    font-size: 11px;
    margin-left: 4px;
    opacity: 0.6;
    font-weight: 500;
    vertical-align: baseline;
}
.message.mine .msg-status { color: rgba(255,255,255,0.8); }
.msg-status.read { opacity: 1; }
.message.mine .msg-status.read { color: #a8d8b8; }

/* === Ввод сообщения === */
.chat-input {
    display: flex; gap: 8px; padding: 8px 10px;
    padding-bottom: calc(8px + var(--safe-bottom));
    background: var(--surface); border-top: 0.5px solid var(--line);
    flex-shrink: 0; flex-wrap: wrap;
}
.draft-indicator {
    width: 100%; font-size: 11px; color: var(--muted);
    font-style: italic; padding: 0 4px 4px;
}
.chat-input input {
    flex: 1; min-width: 0; padding: 10px 14px;
    border: 1px solid var(--line); border-radius: 20px;
    font-size: 15px; outline: none;
    transition: border-color var(--transition);
    background: var(--bg); color: var(--ink); font-family: inherit;
}
.chat-input input:focus { border-color: var(--accent); }
.chat-input .btn-primary {
    border-radius: 50%; width: 42px; height: 42px; padding: 0;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
}

/* === Контекстное меню сообщения === */
.msg-context-overlay {
    position: fixed; top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(26,26,23,0.4); z-index: 100;
    display: flex; align-items: flex-end; justify-content: center;
    padding: 20px;
    animation: overlayIn 0.15s ease forwards;
}
@keyframes overlayIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.msg-context-card {
    background: var(--surface); border-radius: 18px;
    padding: 10px; width: 100%; max-width: 320px;
    border: 0.5px solid var(--line);
    margin-bottom: env(safe-area-inset-bottom, 0px);
    animation: sheetUp 0.25s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}
@keyframes sheetUp {
    from { opacity: 0; transform: translateY(32px); }
    to { opacity: 1; transform: translateY(0); }
}

.msg-context-card button {
    width: 100%; padding: 13px; margin-bottom: 4px;
    border: none; border-radius: 10px; font-size: 15px;
    cursor: pointer; font-weight: 500; font-family: inherit;
    background: transparent; color: var(--ink);
    transition: background var(--transition);
    letter-spacing: -0.01em;
}
.msg-context-card button:last-child { margin-bottom: 0; }
.msg-context-card button:active { background: var(--bg); }
.msg-context-card button.ctx-danger { color: var(--red); }
.msg-context-card button.ctx-cancel { color: var(--muted); }

/* === Модальное окно === */
.modal-overlay {
    position: fixed; top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(26,26,23,0.4); z-index: 100;
    display: flex; align-items: center; justify-content: center;
    padding: 20px;
    animation: overlayIn 0.15s ease forwards;
}
.modal-card {
    background: var(--surface); border-radius: 18px;
    padding: 22px; width: 100%; max-width: 320px;
    border: 0.5px solid var(--line);
    animation: modalIn 0.25s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}
@keyframes modalIn {
    from { opacity: 0; transform: scale(0.94) translateY(8px); }
    to { opacity: 1; transform: scale(1) translateY(0); }
}

.modal-card h3 { font-size: 17px; margin-bottom: 16px; color: var(--ink); font-weight: 600; letter-spacing: -0.02em; }
.modal-card button { width: 100%; margin-bottom: 8px; }
.modal-card button:last-child { margin-bottom: 0; }
.modal-card textarea {
    width: 100%; padding: 12px 14px; border: 1px solid var(--line);
    border-radius: 10px; font-size: 15px; outline: none; font-family: inherit;
    resize: vertical; min-height: 60px; margin-bottom: 12px; color: var(--ink);
    background: var(--bg);
    transition: border-color var(--transition);
}
.modal-card textarea:focus { border-color: var(--accent); }

.btn-modal-cancel {
    background: transparent; color: var(--muted);
    border: 1px solid var(--line); border-radius: 12px; padding: 13px;
    font-size: 15px; cursor: pointer; width: 100%; font-family: inherit;
    transition: background var(--transition);
}
.btn-modal-cancel:active { background: var(--bg); }

/* === Ответ на сообщение === */
.reply-preview {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 4px 8px 10px;
    border-left: 2px solid var(--accent);
    margin-bottom: 2px;
    animation: slideDown 0.15s ease forwards;
}
.reply-preview-content { flex: 1; min-width: 0; }
.reply-preview-sender {
    font-size: 12px; font-weight: 600; color: var(--accent);
    letter-spacing: -0.01em; margin-bottom: 1px;
}
.reply-preview-text {
    font-size: 13px; color: var(--muted);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* Цитата внутри сообщения */
.msg-reply-quote {
    background: rgba(0,0,0,0.07);
    border-radius: 8px;
    padding: 5px 8px;
    margin-bottom: 5px;
    border-left: 2px solid rgba(255,255,255,0.4);
    cursor: pointer;
    transition: opacity 0.15s;
}
.msg-reply-quote:active { opacity: 0.7; }
.message.theirs .msg-reply-quote {
    background: rgba(42,90,61,0.07);
    border-left-color: var(--accent);
}
.msg-reply-quote-sender {
    font-size: 11px; font-weight: 600;
    opacity: 0.85; margin-bottom: 1px;
}
.msg-reply-quote-text {
    font-size: 12px; opacity: 0.7;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* Плавный возврат после свайпа */
.message { transition: transform 0.18s ease; }
.message.swiping { transition: none; }

/* === toggle-row (оставлен для совместимости) === */
.toggle-row {
    display: flex; align-items: center; justify-content: space-between;
}
.toggle-row span { font-size: 15px; }
