/* ===== Сообщения анкетам: шторка (chat.js) и раздел /chat =====
   Телефон — шторка снизу почти на весь экран; от 768px — панель справа. */

html.msg-lock,
html.msg-lock body {
    overflow: hidden;
}

.msg-drawer {
    position: fixed;
    inset: 0;
    z-index: calc(var(--z-modal) + 10);
}
.msg-drawer[hidden] {
    display: none;
}

.msg-drawer__backdrop {
    position: absolute;
    inset: 0;
    background: rgba(20, 24, 40, 0.48);
    opacity: 0;
    transition: opacity var(--transition-base);
}
.msg-drawer--open .msg-drawer__backdrop {
    opacity: 1;
}

.msg-drawer__panel {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: 92vh;
    height: 92dvh;
    display: flex;
    flex-direction: column;
    background: var(--color-bg);
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    box-shadow: var(--shadow-xl);
    transform: translateY(100%);
    transition: transform var(--transition-base);
    font-family: var(--font-family);
    color: var(--color-text);
}
.msg-drawer--open .msg-drawer__panel {
    transform: translateY(0);
}

@media (min-width: 768px) {
    .msg-drawer__panel {
        left: auto;
        top: 0;
        width: 440px;
        height: 100%;
        border-radius: 0;
        transform: translateX(100%);
    }
    .msg-drawer--open .msg-drawer__panel {
        transform: translateX(0);
    }
    .msg-drawer__grip {
        display: none;
    }
}

@media (prefers-reduced-motion: reduce) {
    .msg-drawer__panel,
    .msg-drawer__backdrop {
        transition: none;
    }
}

/* --- шапка --- */
.msg-drawer__head {
    position: relative;
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-4) var(--space-3) var(--space-3) var(--space-4);
    border-bottom: 1px solid var(--color-border-light);
    flex-shrink: 0;
}
.msg-drawer__grip {
    position: absolute;
    top: 6px;
    left: 50%;
    width: 40px;
    height: 4px;
    margin-left: -20px;
    border-radius: var(--radius-full);
    background: var(--color-border);
}
.msg-drawer__who {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    min-width: 0;
    flex: 1;
}
.msg-drawer__who-text {
    display: flex;
    flex-direction: column;
    min-width: 0;
}
.msg-drawer__name {
    font-weight: var(--font-weight-semibold);
    font-size: var(--font-size-md);
    line-height: var(--line-height-tight);
    color: var(--color-text);
    text-decoration: none;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
a.msg-drawer__name:hover {
    color: var(--color-primary-text);
}
.msg-drawer__sub {
    margin-top: 2px;
    font-size: var(--font-size-xs);
    color: var(--color-text-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.msg-drawer__close {
    flex-shrink: 0;
    width: 44px;
    height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 0;
    border-radius: var(--radius-full);
    background: transparent;
    color: var(--color-text-secondary);
    cursor: pointer;
}
.msg-drawer__close:hover {
    background: var(--color-bg-muted);
    color: var(--color-text);
}
.msg-drawer__close:focus-visible,
.msg-drawer__send:focus-visible,
.msg-thread:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: 2px;
}

/* --- аватар --- */
.msg-avatar {
    flex-shrink: 0;
    width: 44px;
    height: 44px;
    border-radius: var(--radius-full);
    object-fit: cover;
    background: var(--color-bg-muted);
}
.msg-avatar--letter {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: var(--gradient-brand);
    color: var(--color-text-inverse);
    font-weight: var(--font-weight-semibold);
    font-size: var(--font-size-md);
}
.msg-avatar--lg {
    width: 72px;
    height: 72px;
    font-size: var(--font-size-xl);
}

/* --- лента --- */
.msg-drawer__body {
    flex: 1;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    background: var(--color-bg-subtle);
    padding: var(--space-4);
    outline: none;
}
.msg-drawer--loading .msg-drawer__body {
    background-image: linear-gradient(90deg, transparent, var(--color-primary-light), transparent);
    background-size: 200% 3px;
    background-repeat: no-repeat;
    animation: msg-loading 1s linear infinite;
}
@keyframes msg-loading {
    from { background-position: 200% 0; }
    to { background-position: -200% 0; }
}

.msg-drawer__feed {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    min-height: 100%;
    justify-content: flex-end;
}

.msg-day {
    align-self: center;
    margin: var(--space-2) 0;
    font-size: var(--font-size-xs);
    color: var(--color-text-muted);
}
.msg-day span {
    display: inline-block;
    padding: 2px var(--space-3);
    border-radius: var(--radius-full);
    background: var(--color-bg);
    box-shadow: var(--shadow-sm);
}

.msg-bubble {
    align-self: flex-start;
    max-width: 82%;
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-lg) var(--radius-lg) var(--radius-lg) var(--radius-sm);
    background: var(--color-bg);
    box-shadow: var(--shadow-sm);
    font-size: var(--font-size-sm);
    line-height: var(--line-height-normal);
}
.msg-bubble--mine {
    align-self: flex-end;
    border-radius: var(--radius-lg) var(--radius-lg) var(--radius-sm) var(--radius-lg);
    background: var(--color-primary-text);
    color: var(--color-text-inverse);
}
.msg-bubble__text {
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}
.msg-bubble__meta {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 4px;
    margin-top: 2px;
    font-size: 0.6875rem;
    color: var(--color-text-muted);
}
.msg-bubble--mine .msg-bubble__meta {
    color: rgba(255, 255, 255, 0.85);
}
.msg-bubble__state {
    display: inline-flex;
}

.msg-empty {
    margin: auto 0;
    text-align: center;
    padding: var(--space-8) var(--space-4);
    display: flex;
    flex-direction: column;
    align-items: center;
}
.msg-empty__title {
    margin: var(--space-3) 0 var(--space-1);
    font-weight: var(--font-weight-semibold);
    font-size: var(--font-size-md);
}
.msg-empty__text {
    margin: 0 0 var(--space-4);
    max-width: 320px;
    font-size: var(--font-size-sm);
    color: var(--color-text-secondary);
}

/* --- подсказка гостю, форма, ошибка --- */
.msg-drawer__note {
    flex-shrink: 0;
    padding: var(--space-2) var(--space-4);
    font-size: var(--font-size-xs);
    line-height: var(--line-height-normal);
    color: var(--color-text-secondary);
    background: var(--color-primary-light);
}
.msg-drawer__note a,
.msg-page__guest-note a,
.msg-empty__text a {
    color: var(--color-primary-text);
    font-weight: var(--font-weight-medium);
}

.msg-drawer__form {
    flex-shrink: 0;
    display: flex;
    align-items: flex-end;
    gap: var(--space-2);
    padding: var(--space-3) var(--space-3) calc(var(--space-3) + env(safe-area-inset-bottom, 0px));
    border-top: 1px solid var(--color-border-light);
    background: var(--color-bg);
}
.msg-drawer__form[hidden] {
    display: none;
}
.msg-drawer__input {
    flex: 1;
    min-height: 44px;
    max-height: 140px;
    padding: 10px var(--space-4);
    border: 1px solid var(--color-border);
    border-radius: 22px;
    resize: none;
    font: inherit;
    /* 16px — иначе iOS увеличивает страницу при фокусе */
    font-size: var(--font-size-base);
    line-height: 1.4;
    color: var(--color-text);
    background: var(--color-bg);
}
.msg-drawer__input:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px var(--color-primary-light);
}
.msg-drawer__send {
    flex-shrink: 0;
    width: 44px;
    height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 0;
    border-radius: var(--radius-full);
    background: var(--gradient-brand);
    color: var(--color-text-inverse);
    cursor: pointer;
    transition: opacity var(--transition-fast);
}
.msg-drawer__send:disabled {
    opacity: 0.4;
    cursor: default;
}
.msg-drawer__error {
    flex-shrink: 0;
    padding: var(--space-2) var(--space-4) var(--space-3);
    font-size: var(--font-size-sm);
    color: var(--color-danger);
    background: var(--color-bg);
}
.msg-drawer__error[hidden] {
    display: none;
}

/* --- кнопка на странице анкеты --- */
.profile-contact-btns {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    margin: 10px 0 20px;
}
.profile-contact-btns .edit-profile-btn {
    margin: 0;
}
.msg-open-btn {
    width: 100%;
    gap: var(--space-2);
    padding-left: var(--space-3);
    padding-right: var(--space-3);
    white-space: nowrap;
}
.msg-open-btn svg {
    flex-shrink: 0;
}

/* --- значок непрочитанных в шапке --- */
.hdr-icon--chat {
    position: relative;
}

/* --- раздел /chat --- */
.msg-page {
    max-width: 760px;
    margin: var(--space-6) auto var(--space-12);
}
.msg-page__title {
    margin-bottom: var(--space-4);
}
.msg-page__guest-note {
    margin-bottom: var(--space-4);
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-md);
    background: var(--color-primary-light);
    font-size: var(--font-size-sm);
    color: var(--color-text-secondary);
}
.msg-page__guest-note[hidden] {
    display: none;
}
.msg-list-page__loading {
    padding: var(--space-8) 0;
    text-align: center;
    color: var(--color-text-muted);
}
.msg-empty--page {
    border: 1px dashed var(--color-border);
    border-radius: var(--radius-lg);
}

.msg-threads {
    list-style: none;
    margin: 0;
    padding: 0;
    border: 1px solid var(--color-border-light);
    border-radius: var(--radius-lg);
    overflow: hidden;
    background: var(--color-bg);
}
.msg-threads li + li {
    border-top: 1px solid var(--color-border-light);
}
.msg-thread {
    width: 100%;
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-4);
    border: 0;
    background: transparent;
    text-align: left;
    font: inherit;
    color: inherit;
    cursor: pointer;
}
.msg-thread:hover {
    background: var(--color-bg-subtle);
}
.msg-thread__main {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.msg-thread__top,
.msg-thread__bottom {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    min-width: 0;
}
.msg-thread__name {
    flex: 1;
    min-width: 0;
    font-weight: var(--font-weight-semibold);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.msg-thread__time {
    flex-shrink: 0;
    font-size: var(--font-size-xs);
    color: var(--color-text-muted);
}
.msg-thread__as {
    font-size: var(--font-size-xs);
    color: var(--color-primary-text);
}
.msg-thread__preview {
    flex: 1;
    min-width: 0;
    font-size: var(--font-size-sm);
    color: var(--color-text-secondary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.msg-thread--unread .msg-thread__preview {
    color: var(--color-text);
    font-weight: var(--font-weight-medium);
}
.msg-thread__badge {
    flex-shrink: 0;
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    border-radius: var(--radius-full);
    background: var(--color-accent);
    color: var(--color-text-inverse);
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-semibold);
    line-height: 20px;
    text-align: center;
}
