:root {
    --red: #ec0000;
    --red-dark: #c00000;
    --ink: #1a1a1a;
    --muted: #6b7280;
    --border: #e5e7eb;
    --bg: #f3f4f6;
    --surface: #ffffff;
    --tg-green: #e7f8e3;
    --tg-blue: #3390ec;
    --tg-sidebar: #ffffff;
    --tg-chat-bg: #e6ebee;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    color: var(--ink);
    background: var(--bg);
}

/* User flow */
.chat-user-page {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
}

.chat-card {
    width: 100%;
    max-width: 520px;
    background: var(--surface);
    border-radius: 16px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.08);
    border: 1px solid var(--border);
    overflow: hidden;
}

.chat-card__header {
    background: linear-gradient(135deg, var(--red), var(--red-dark));
    color: #fff;
    padding: 24px 28px;
}

.chat-card__header h1 {
    margin: 0 0 6px;
    font-size: 1.5rem;
}

.chat-card__header p {
    margin: 0;
    opacity: 0.92;
    font-size: 0.95rem;
}

.chat-card__body {
    padding: 28px;
}

.chat-steps {
    display: flex;
    gap: 8px;
    margin-bottom: 24px;
}

.chat-step {
    flex: 1;
    height: 4px;
    border-radius: 999px;
    background: #e5e7eb;
}

.chat-step.is-active,
.chat-step.is-done {
    background: var(--red);
}

.chat-form-group {
    margin-bottom: 18px;
}

.chat-form-group label {
    display: block;
    font-size: 0.85rem;
    font-weight: 600;
    margin-bottom: 6px;
    color: #374151;
}

.chat-form-group input,
.chat-form-group select {
    width: 100%;
    padding: 14px 16px;
    border: 1.5px solid var(--border);
    border-radius: 10px;
    font-size: 1rem;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.chat-form-group input:focus,
.chat-form-group select:focus {
    border-color: var(--red);
    box-shadow: 0 0 0 3px rgba(236, 0, 0, 0.1);
}

.chat-btn {
    width: 100%;
    padding: 14px 20px;
    border: none;
    border-radius: 10px;
    background: var(--red);
    color: #fff;
    font-size: 1rem;
    font-weight: 700;
    cursor: pointer;
    transition: background 0.2s, transform 0.1s;
}

.chat-btn:hover { background: var(--red-dark); }
.chat-btn:active { transform: scale(0.98); }
.chat-btn:disabled { opacity: 0.55; cursor: not-allowed; }

.chat-error {
    display: none;
    margin-top: 12px;
    padding: 12px 14px;
    border-radius: 8px;
    background: #fef2f2;
    color: #b91c1c;
    font-size: 0.9rem;
}

.chat-error.visible { display: block; }

.apartment-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.apartment-item {
    display: block;
    border: 1.5px solid var(--border);
    border-radius: 12px;
    padding: 16px 18px;
    cursor: pointer;
    transition: border-color 0.2s, background 0.2s, box-shadow 0.2s;
    text-decoration: none;
    color: inherit;
}

.apartment-item:hover {
    border-color: var(--red);
    background: #fff8f8;
}

.apartment-item.is-selected {
    border-color: var(--red);
    background: #fff5f5;
    box-shadow: 0 0 0 3px rgba(236, 0, 0, 0.12);
}

.apartment-item h3 {
    margin: 0 0 6px;
    font-size: 1rem;
}

.apartment-item p {
    margin: 0;
    color: var(--muted);
    font-size: 0.9rem;
}

.apartment-meta {
    display: flex;
    gap: 12px;
    margin-top: 8px;
    font-size: 0.85rem;
    color: #374151;
}

/* User chat room */
body.chat-page,
body.chat-page .chat-room {
    height: 100dvh;
    height: 100vh;
}

body.chat-page {
    overflow: hidden;
    position: fixed;
    width: 100%;
    inset: 0;
}

.chat-room {
    display: flex;
    flex-direction: column;
    background: var(--tg-chat-bg);
    max-height: 100dvh;
    max-height: 100vh;
    overflow: hidden;
}

.chat-room__header {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 14px 20px;
    padding-top: max(14px, env(safe-area-inset-top));
    display: flex;
    align-items: center;
    gap: 12px;
    flex-shrink: 0;
}

.chat-room__avatar {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    background: var(--red);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
}

.chat-room__title {
    margin: 0;
    font-size: 1rem;
}

.chat-room__header > div:last-child {
    min-width: 0;
    flex: 1;
}

.chat-room__subtitle {
    margin: 2px 0 0;
    font-size: 0.82rem;
    color: var(--muted);
}

.chat-avatar-wrap {
    position: relative;
    flex-shrink: 0;
}

.chat-avatar-wrap--sm .chat-admin__avatar {
    position: relative;
}

.chat-presence {
    position: absolute;
    right: -1px;
    bottom: -1px;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    border: 2px solid var(--surface);
    box-sizing: border-box;
}

.chat-presence--online {
    background: #22c55e;
}

.chat-presence--offline {
    background: #9ca3af;
}

.chat-admin__item.is-active .chat-presence {
    border-color: #3390ec;
}

.chat-room__messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    touch-action: pan-y;
    padding: 20px 16px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.chat-bubble {
    max-width: 78%;
    padding: 10px 14px;
    border-radius: 14px;
    font-size: 0.95rem;
    line-height: 1.5;
    white-space: pre-wrap;
    word-break: break-word;
    overflow-wrap: anywhere;
    position: relative;
    flex-shrink: 0;
}

.chat-bubble--incoming {
    align-self: flex-start;
    background: #fff;
    border-top-left-radius: 4px;
}

.chat-bubble--outgoing {
    align-self: flex-end;
    background: var(--tg-green);
    border-top-right-radius: 4px;
}

.chat-bubble__time {
    display: block;
    margin-top: 4px;
    font-size: 0.7rem;
    color: #6b7280;
    text-align: right;
}

.chat-typing {
    display: none;
    align-self: flex-start;
    background: #fff;
    padding: 10px 14px;
    border-radius: 14px;
    border-top-left-radius: 4px;
}

.chat-typing.visible { display: flex; gap: 4px; align-items: center; }

.chat-typing span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #9ca3af;
    animation: chat-bounce 1.2s infinite;
}

.chat-typing span:nth-child(2) { animation-delay: 0.15s; }
.chat-typing span:nth-child(3) { animation-delay: 0.3s; }

@keyframes chat-bounce {
    0%, 80%, 100% { transform: translateY(0); opacity: 0.5; }
    40% { transform: translateY(-5px); opacity: 1; }
}

.chat-room__composer {
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding: 12px 16px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
    display: flex;
    gap: 10px;
    align-items: flex-end;
    flex-shrink: 0;
}

.chat-room__attach {
    width: 44px;
    height: 44px;
    border: 1px solid var(--border);
    border-radius: 50%;
    background: #fff;
    color: #4b5563;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.chat-room__attach:hover {
    border-color: var(--tg-blue);
    color: var(--tg-blue);
}

.chat-room__attach input {
    display: none;
}

.chat-room__composer textarea {
    flex: 1;
    resize: none;
    min-height: 44px;
    max-height: 120px;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: 22px;
    font: inherit;
    outline: none;
}

.chat-room__composer textarea:focus {
    border-color: var(--tg-blue);
}

.chat-room__send {
    width: 44px;
    height: 44px;
    border: none;
    border-radius: 50%;
    background: var(--tg-blue);
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.chat-room__send:disabled { opacity: 0.45; cursor: not-allowed; }

.chat-room__delete {
    border: 1px solid #fecaca;
    background: #fff;
    color: #dc2626;
    border-radius: 8px;
    padding: 8px 12px;
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    flex-shrink: 0;
    transition: background 0.15s, border-color 0.15s;
}

.chat-room__delete:hover {
    background: #fef2f2;
    border-color: #f87171;
}

.chat-admin__item-delete {
    border: none;
    background: transparent;
    color: #9ca3af;
    cursor: pointer;
    padding: 4px;
    border-radius: 6px;
    flex-shrink: 0;
    opacity: 0;
    transition: opacity 0.15s, color 0.15s, background 0.15s;
}

.chat-admin__item:hover .chat-admin__item-delete,
.chat-admin__item.is-active .chat-admin__item-delete {
    opacity: 1;
}

.chat-admin__item-delete:hover {
    color: #dc2626;
    background: #fef2f2;
}

/* Admin Telegram-like layout */
body.chat-page .chat-admin {
    height: calc(100dvh - 42px);
    height: calc(100vh - 42px);
    max-height: calc(100dvh - 42px);
    max-height: calc(100vh - 42px);
    overflow: hidden;
}

.chat-admin {
    display: grid;
    grid-template-columns: 340px 1fr;
    background: var(--tg-sidebar);
}

.chat-admin__sidebar {
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    background: #fff;
    min-height: 0;
    overflow: hidden;
}

.chat-admin__sidebar-header {
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.chat-admin__sidebar-header h2 {
    margin: 0;
    font-size: 1rem;
}

.chat-admin__search {
    margin: 10px 12px;
    padding: 10px 12px;
    border: none;
    border-radius: 8px;
    background: #f0f2f5;
    font: inherit;
    outline: none;
}

.chat-admin__list {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.chat-admin__item {
    display: flex;
    gap: 12px;
    padding: 12px 16px;
    cursor: pointer;
    border-bottom: 1px solid #f3f4f6;
    transition: background 0.15s;
    text-decoration: none;
    color: inherit;
    align-items: center;
}

.chat-admin__item:hover { background: #f9fafb; }
.chat-admin__item.is-active { background: #3390ec; color: #fff; }
.chat-admin__item.is-active .chat-admin__preview,
.chat-admin__item.is-active .chat-admin__time { color: rgba(255,255,255,0.85); }

.chat-admin__avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: #dbeafe;
    color: #1d4ed8;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    flex-shrink: 0;
}

.chat-admin__item.is-active .chat-admin__avatar {
    background: rgba(255,255,255,0.2);
    color: #fff;
}

.chat-admin__item-body { flex: 1; min-width: 0; }

.chat-admin__item-top {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    align-items: baseline;
}

.chat-admin__name {
    font-weight: 600;
    font-size: 0.95rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-admin__time {
    font-size: 0.75rem;
    color: var(--muted);
    flex-shrink: 0;
}

.chat-admin__preview {
    margin-top: 4px;
    font-size: 0.84rem;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-admin__badge {
    min-width: 22px;
    height: 22px;
    padding: 0 6px;
    border-radius: 11px;
    background: var(--red);
    color: #fff;
    font-size: 0.72rem;
    font-weight: 700;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-left: auto;
}

.chat-admin__main {
    display: flex;
    flex-direction: column;
    background: var(--tg-chat-bg);
    min-height: 0;
    overflow: hidden;
}

.chat-admin__chat-panel {
    display: none;
    flex: 1;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
}

.chat-admin__empty {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--muted);
    font-size: 1rem;
}

.chat-admin__toolbar {
    display: flex;
    align-items: center;
    gap: 10px;
}

.chat-notif-btn {
    border: 1px solid var(--border);
    background: #fff;
    border-radius: 8px;
    padding: 6px 10px;
    font-size: 0.8rem;
    cursor: pointer;
}

.chat-bubble__sender {
    font-size: 0.72rem;
    font-weight: 700;
    color: var(--tg-blue);
    margin-bottom: 4px;
}

.chat-bubble__text {
    white-space: pre-wrap;
    word-break: break-word;
}

.chat-bubble__text a,
.chat-bubble__file {
    color: var(--tg-blue);
    text-decoration: underline;
}

.chat-bubble__image {
    display: block;
    max-width: 260px;
    width: 100%;
    border-radius: 10px;
    margin-bottom: 6px;
}

.chat-bubble__file {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    background: rgba(0,0,0,0.04);
    border-radius: 10px;
    text-decoration: none;
    margin-bottom: 6px;
}

.chat-bubble__file-icon { font-size: 1.1rem; }
.chat-bubble__file-name { font-weight: 600; font-size: 0.9rem; }

.chat-notif-btn.active {
    border-color: var(--red);
    color: var(--red);
    background: #fff5f5;
}

.chat-uploading {
    font-size: 0.82rem;
    color: var(--muted);
    padding: 0 16px 8px;
    flex-shrink: 0;
}

@media (max-width: 900px) {
    .chat-admin {
        grid-template-columns: 1fr;
    }
    .chat-admin__sidebar.is-hidden {
        display: none;
    }
}

@media (max-width: 600px) {
    .chat-bubble {
        max-width: 92%;
        font-size: 0.93rem;
    }

    .chat-room__header {
        padding: 12px 14px;
        padding-top: max(12px, env(safe-area-inset-top));
    }

    .chat-room__messages {
        padding: 12px 10px;
    }

    .chat-room__composer {
        padding: 10px 12px;
        padding-bottom: max(10px, env(safe-area-inset-bottom));
    }

    .chat-room__title {
        font-size: 0.95rem;
    }

    .chat-room__subtitle {
        font-size: 0.78rem;
    }

    .chat-room__delete {
        padding: 6px 8px;
        font-size: 0.72rem;
    }

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