/* ---------- Цвета: светлая и тёмная тема подхватываются из настроек телефона/ПК ---------- */
:root {
    color-scheme: light dark;

    --page: linear-gradient(160deg, #19a9b8, #0a6a86);
    --veil: rgba(255, 255, 255, 0);          /* затемнение поверх фоновой картинки */
    --panel: rgba(255, 255, 255, 0.93);
    --panel-solid: #ffffff;
    --ink: #12303a;
    --muted: #547079;
    --line: rgba(10, 100, 120, 0.2);
    --accent: #0a7a90;
    --accent-strong: #08667a;
    --accent-ink: #086579;                    /* акцентный цвет для текста на светлом */
    --on-accent: #ffffff;
    --field: #e9f6f7;
    --bubble-theirs: #ffffff;
    --bubble-mine: #0a7a90;
    --ink-mine: #ffffff;
    --time-mine: rgba(255, 255, 255, 0.85);
    --author-l: 33%;
    --shadow: 0 12px 40px rgba(4, 60, 80, 0.28);
    --warn: #b4540a;
    --danger: #b3261e;
}

@media (prefers-color-scheme: dark) {
    :root {
        --page: linear-gradient(160deg, #0b2a33, #05161c);
        --veil: rgba(4, 24, 30, 0.8);
        --panel: rgba(11, 33, 41, 0.94);
        --panel-solid: #0f2a33;
        --ink: #e3f1f3;
        --muted: #8eb0b8;
        --line: rgba(120, 200, 215, 0.2);
        --accent: #2bb5c9;
        --accent-strong: #52c7d8;
        --accent-ink: #5cd0e0;
        --on-accent: #04252c;
        --field: #17323b;
        --bubble-theirs: #17323b;
        --bubble-mine: #0e6b80;
        --ink-mine: #ffffff;
        --time-mine: rgba(255, 255, 255, 0.8);
        --author-l: 70%;
        --shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
        --warn: #f0a35c;
        --danger: #ff8a80;
    }
}

/* ---------- Общая основа ---------- */
* {
    box-sizing: border-box;
}

html,
body {
    height: 100%;
    margin: 0;
}

body {
    height: 100vh;
    height: 100dvh;
    display: flex;
    justify-content: center;
    background: var(--page);
    color: var(--ink);
    font: 16px/1.4 ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    -webkit-text-size-adjust: 100%;
}

button,
input,
textarea {
    font: inherit;
    color: inherit;
}

button {
    -webkit-tap-highlight-color: transparent;
}

:focus-visible {
    outline: 3px solid var(--accent);
    outline-offset: 2px;
}

[hidden] {
    display: none !important;
}

.view {
    width: 100%;
}

/* ---------- Окно чата: на телефоне во весь экран, фон из присланной картинки ---------- */
.app {
    max-width: 820px;
    height: 100%;
    display: flex;
    flex-direction: column;
    position: relative;
    background:
        linear-gradient(var(--veil), var(--veil)),
        url("bg.jpg") center / cover no-repeat,
        #6ddddd;
}

/* На ПК окно по центру, с отступами и скруглением */
@media (min-width: 860px) {
    body {
        padding: 22px;
    }

    .app {
        border-radius: 22px;
        overflow: hidden;
        box-shadow: var(--shadow);
    }
}

/* ---------- Верхняя панель ---------- */
.topbar {
    flex: none;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 14px;
    padding-top: calc(8px + env(safe-area-inset-top));
    padding-left: calc(14px + env(safe-area-inset-left));
    padding-right: calc(14px + env(safe-area-inset-right));
    background: var(--panel);
    border-bottom: 1px solid var(--line);
    -webkit-backdrop-filter: blur(10px);
    backdrop-filter: blur(10px);
    z-index: 2;
}

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

.title {
    margin: 0;
    font-size: 18px;
    font-weight: 700;
    line-height: 1.2;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sub {
    font-size: 12.5px;
    line-height: 1.25;
    color: var(--warn);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.me-btn {
    flex: none;
    max-width: 55%;
    display: flex;
    align-items: center;
    gap: 9px;
    padding: 3px 3px 3px 12px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: transparent;
    cursor: pointer;
}

.me-btn:hover {
    background: var(--field);
}

.me-nick {
    min-width: 0;
    font-size: 14px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

@media (max-width: 380px) {
    .me-nick {
        display: none;
    }

    .me-btn {
        padding: 3px;
    }
}

/* ---------- Аватарки ---------- */
.avatar {
    --s: 36px;
    flex: none;
    width: var(--s);
    height: var(--s);
    display: grid;
    place-items: center;
    overflow: hidden;
    border-radius: 50%;
    background: hsl(var(--hue, 190) 52% 36%);
    color: #fff;
    font-size: calc(var(--s) * 0.46);
    font-weight: 700;
    line-height: 1;
    user-select: none;
}

.avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.avatar.small {
    --s: 32px;
}

.avatar.big {
    --s: 104px;
}

/* ---------- Лента сообщений ---------- */
.chat-main {
    position: relative;
    flex: 1;
    min-height: 0;
}

.messages {
    position: absolute;
    inset: 0;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    padding: 10px 12px 8px;
    padding-left: calc(12px + env(safe-area-inset-left));
    padding-right: calc(12px + env(safe-area-inset-right));
    overscroll-behavior: contain;
    scroll-behavior: auto;
}

.empty {
    position: absolute;
    left: 50%;
    top: 42%;
    transform: translate(-50%, -50%);
    padding: 14px 22px;
    border-radius: 18px;
    background: var(--panel);
    color: var(--muted);
    text-align: center;
    pointer-events: none;
}

.msg {
    display: flex;
    flex-direction: column;
    margin-top: 2px;
}

.msg.first {
    margin-top: 9px;
}

.day-sep {
    align-self: center;
    margin: 10px 0 4px;
    padding: 3px 12px;
    border-radius: 999px;
    background: var(--panel);
    color: var(--muted);
    font-size: 12.5px;
    font-weight: 600;
}

.row {
    display: flex;
    align-items: flex-end;
    gap: 7px;
}

.msg.mine .row {
    justify-content: flex-end;
}

.msg.mine .row > .avatar {
    display: none;
}

.msg.theirs:not(.last) .row > .avatar {
    visibility: hidden;
}

.bubble {
    min-width: 0;
    max-width: min(78%, 540px);
    padding: 6px 11px 4px;
    border-radius: 18px;
    background: var(--bubble-theirs);
    color: var(--ink);
    box-shadow: 0 1px 2px rgba(0, 40, 60, 0.12);
}

.msg.theirs .bubble {
    border-bottom-left-radius: 6px;
}

.msg.theirs.last .bubble {
    border-bottom-left-radius: 18px;
}

.msg.mine .bubble {
    background: var(--bubble-mine);
    color: var(--ink-mine);
    border-bottom-right-radius: 6px;
}

.msg.mine.last .bubble {
    border-bottom-right-radius: 18px;
}

.author {
    margin-bottom: 1px;
    font-size: 13px;
    font-weight: 700;
    color: hsl(var(--hue, 200) 60% var(--author-l));
}

.msg.mine .author,
.msg:not(.first) .author {
    display: none;
}

.media {
    margin: 2px 0 4px;
    line-height: 0;
}

.media img,
.media video {
    display: block;
    width: auto;
    height: auto;
    max-width: min(62vw, 360px);
    max-height: 340px;
    border-radius: 12px;
    background: rgba(0, 40, 60, 0.12);
}

.media img {
    cursor: zoom-in;
}

.media video {
    min-width: 200px;
    background: #000;
}

.media.loading {
    min-width: 200px;
    min-height: 150px;
    border-radius: 12px;
    background: rgba(0, 40, 60, 0.12);
}

.media.broken {
    padding: 14px 16px;
    line-height: 1.3;
    font-size: 14px;
    color: var(--muted);
    border-radius: 12px;
    background: rgba(0, 40, 60, 0.08);
}

.body {
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.time {
    margin-top: 1px;
    font-size: 11px;
    line-height: 1.3;
    text-align: right;
    color: var(--muted);
    cursor: default;
}

.msg.mine .time {
    color: var(--time-mine);
}

@keyframes pop {
    from {
        opacity: 0;
        transform: translateY(6px);
    }
}

.msg.live {
    animation: pop 0.18s ease-out;
}

/* ---------- Кнопка "к новым сообщениям" ---------- */
.to-bottom {
    position: absolute;
    right: 14px;
    bottom: 10px;
    width: 42px;
    height: 42px;
    display: grid;
    place-items: center;
    border: 1px solid var(--line);
    border-radius: 50%;
    background: var(--panel-solid);
    color: var(--accent-ink);
    box-shadow: 0 3px 12px rgba(0, 40, 60, 0.25);
    cursor: pointer;
}

.badge {
    position: absolute;
    top: -7px;
    right: -5px;
    min-width: 20px;
    height: 20px;
    padding: 0 5px;
    border-radius: 999px;
    background: var(--accent);
    color: var(--on-accent);
    font-size: 12px;
    font-weight: 700;
    line-height: 20px;
    text-align: center;
}

/* ---------- "Печатает…" ---------- */
.typing {
    flex: none;
    min-height: 28px;
    padding: 0 14px 3px;
    display: flex;
    align-items: flex-end;
}

.typing-pill {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    max-width: 100%;
    padding: 3px 11px;
    border-radius: 999px;
    background: var(--panel);
    color: var(--muted);
    font-size: 13px;
}

.typing-pill > span:first-child {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.dots {
    display: inline-flex;
    gap: 3px;
}

.dots i {
    width: 5px;
    height: 5px;
    border-radius: 50%;
    background: currentColor;
    animation: blink 1.2s infinite ease-in-out;
}

.dots i:nth-child(2) {
    animation-delay: 0.2s;
}

.dots i:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes blink {
    0%,
    80%,
    100% {
        opacity: 0.25;
        transform: translateY(0);
    }

    40% {
        opacity: 1;
        transform: translateY(-2px);
    }
}

/* ---------- Поле ввода ---------- */
.composer {
    flex: none;
    background: var(--panel);
    border-top: 1px solid var(--line);
    padding-bottom: env(safe-area-inset-bottom);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
    -webkit-backdrop-filter: blur(10px);
    backdrop-filter: blur(10px);
}

.app.drag .composer {
    outline: 3px dashed var(--accent);
    outline-offset: -6px;
}

.composer-row {
    display: flex;
    align-items: flex-end;
    gap: 6px;
    padding: 8px 10px;
}

.composer textarea {
    flex: 1;
    min-width: 0;
    max-height: 140px;
    padding: 10px 15px;
    border: 1px solid transparent;
    border-radius: 22px;
    background: var(--field);
    resize: none;
    outline: none;
    line-height: 1.35;
}

.composer textarea:focus {
    border-color: var(--accent);
}

.icon-btn {
    flex: none;
    width: 44px;
    height: 44px;
    display: grid;
    place-items: center;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: var(--accent-ink);
    cursor: pointer;
}

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

.icon-btn.small {
    width: 34px;
    height: 34px;
    color: var(--muted);
}

.icon-btn:disabled {
    opacity: 0.45;
    cursor: default;
}

.send-btn {
    flex: none;
    width: 46px;
    height: 46px;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: transparent;
    cursor: pointer;
    transition: transform 0.12s ease, opacity 0.2s ease, filter 0.2s ease;
}

.send-btn img {
    display: block;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.send-btn:hover:not(:disabled) {
    transform: scale(1.07);
}

.send-btn:active:not(:disabled) {
    transform: scale(0.93);
}

.send-btn:disabled {
    opacity: 0.62;
    filter: grayscale(0.35);
    cursor: default;
}

/* ---------- Вложение перед отправкой ---------- */
.attach {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px 0;
}

.attach-thumb {
    flex: none;
    width: 54px;
    height: 54px;
    display: grid;
    place-items: center;
    overflow: hidden;
    border-radius: 12px;
    background: var(--field);
    color: var(--accent-ink);
}

.attach-thumb img,
.attach-thumb video {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

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

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

.attach-size {
    font-size: 12.5px;
    color: var(--muted);
}

.attach-bar {
    height: 5px;
    margin-top: 5px;
    overflow: hidden;
    border-radius: 999px;
    background: var(--field);
}

.attach-bar > div {
    width: 0;
    height: 100%;
    border-radius: 999px;
    background: var(--accent);
    transition: width 0.15s linear;
}

/* ---------- Экран входа ---------- */
.auth {
    min-height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px 16px;
    overflow-y: auto;
    background:
        linear-gradient(var(--veil), var(--veil)),
        url("bg.jpg") center / cover no-repeat,
        #6ddddd;
}

.auth-card {
    width: 100%;
    max-width: 380px;
    padding: 26px 24px 24px;
    border-radius: 24px;
    background: var(--panel);
    box-shadow: var(--shadow);
    -webkit-backdrop-filter: blur(12px);
    backdrop-filter: blur(12px);
    text-align: center;
}

.brand {
    width: 62px;
    height: 62px;
    margin: 0 auto 10px;
    display: grid;
    place-items: center;
    border-radius: 50%;
    background: var(--accent);
    color: var(--on-accent);
}

.auth-card h1 {
    margin: 0;
    font-size: 24px;
}

.lead {
    margin: 2px 0 16px;
    color: var(--muted);
}

.tabs {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 4px;
    margin-bottom: 16px;
    padding: 4px;
    border-radius: 999px;
    background: var(--field);
}

.tab {
    padding: 8px 6px;
    border: none;
    border-radius: 999px;
    background: transparent;
    color: var(--muted);
    font-weight: 600;
    cursor: pointer;
}

.tab[aria-selected="true"] {
    background: var(--panel-solid);
    color: var(--accent-ink);
    box-shadow: 0 1px 4px rgba(0, 40, 60, 0.18);
}

#auth-form {
    text-align: left;
}

.field {
    display: block;
    margin-bottom: 12px;
}

.field > span:first-child {
    display: block;
    margin: 0 0 4px 4px;
    font-size: 13px;
    font-weight: 600;
    color: var(--muted);
}

.field input {
    width: 100%;
    padding: 11px 14px;
    border: 1px solid var(--line);
    border-radius: 14px;
    background: var(--field);
    outline: none;
}

.field input:focus {
    border-color: var(--accent);
}

.pw {
    position: relative;
    display: block;
}

.pw input {
    padding-right: 86px;
}

.pw-toggle {
    position: absolute;
    top: 50%;
    right: 6px;
    transform: translateY(-50%);
    padding: 6px 10px;
    border: none;
    border-radius: 10px;
    background: transparent;
    color: var(--accent-ink);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
}

.hint,
.note,
.error {
    margin: 0 0 12px;
    font-size: 13.5px;
    line-height: 1.35;
}

.hint {
    color: var(--muted);
}

.note {
    color: var(--accent-ink);
}

.error {
    color: var(--danger);
}

.primary,
.secondary {
    padding: 11px 20px;
    border: none;
    border-radius: 14px;
    font-weight: 700;
    cursor: pointer;
}

.primary {
    background: var(--accent);
    color: var(--on-accent);
}

.primary:hover:not(:disabled) {
    background: var(--accent-strong);
}

.secondary {
    background: var(--field);
    color: var(--ink);
}

.primary:disabled,
.secondary:disabled {
    opacity: 0.6;
    cursor: default;
}

#auth-submit {
    width: 100%;
    margin-top: 2px;
}

/* ---------- Окна поверх чата ---------- */
.overlay {
    position: fixed;
    inset: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    background: rgba(2, 20, 26, 0.6);
}

.dialog {
    width: 100%;
    max-width: 360px;
    padding: 22px 22px 20px;
    border-radius: 22px;
    background: var(--panel-solid);
    box-shadow: var(--shadow);
    text-align: center;
}

.dialog h2 {
    margin: 0 0 14px;
    font-size: 19px;
}

.avatar-edit {
    position: relative;
    display: inline-block;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: transparent;
    cursor: pointer;
}

.avatar-edit-badge {
    position: absolute;
    right: 0;
    bottom: 2px;
    width: 32px;
    height: 32px;
    display: grid;
    place-items: center;
    border: 3px solid var(--panel-solid);
    border-radius: 50%;
    background: var(--accent);
    color: var(--on-accent);
}

.profile-nick {
    margin-top: 10px;
    font-size: 19px;
    font-weight: 700;
    overflow-wrap: anywhere;
}

.profile-hint {
    margin: 4px 0 14px;
    font-size: 13.5px;
    color: var(--muted);
}

.dialog-buttons {
    display: flex;
    justify-content: center;
    gap: 10px;
    margin-top: 6px;
}

/* ---------- Просмотр фото ---------- */
.lightbox {
    flex-direction: column;
    gap: 12px;
    background: rgba(0, 0, 0, 0.9);
    cursor: zoom-out;
}

.lightbox img {
    max-width: 100%;
    max-height: calc(100% - 56px);
    object-fit: contain;
    border-radius: 6px;
}

.lightbox-close {
    position: absolute;
    top: calc(10px + env(safe-area-inset-top));
    right: 12px;
    width: 44px;
    height: 44px;
    display: grid;
    place-items: center;
    border: none;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.16);
    color: #fff;
    cursor: pointer;
}

.lightbox-download {
    padding: 8px 18px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.16);
    color: #fff;
    font-weight: 600;
    text-decoration: none;
    cursor: pointer;
}

/* ---------- Всплывающие подсказки ---------- */
.toast {
    position: fixed;
    left: 50%;
    bottom: calc(86px + env(safe-area-inset-bottom));
    transform: translateX(-50%);
    z-index: 30;
    width: max-content;
    max-width: min(92vw, 420px);
    padding: 10px 16px;
    border-radius: 14px;
    background: #12303a;
    color: #fff;
    font-size: 14.5px;
    line-height: 1.35;
    text-align: center;
    box-shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
}

@media (prefers-color-scheme: dark) {
    .toast {
        background: #e3f1f3;
        color: #0b2229;
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation: none !important;
        transition: none !important;
    }
}
