:root {
    color-scheme: dark;
    --bg-1: #070912;
    --bg-2: #12172a;
    --bg-glow: rgba(123, 92, 255, 0.28);
    --panel: rgba(19, 24, 42, 0.74);
    --panel-strong: rgba(25, 31, 52, 0.92);
    --text: #f7f7fb;
    --muted: #a8aec3;
    --line: rgba(255, 255, 255, 0.1);
    --input: rgba(255, 255, 255, 0.08);
    --incoming: rgba(255, 255, 255, 0.12);
    --outgoing: linear-gradient(135deg, #7c5cff 0%, #08aeea 100%);
    --accent: #8f7cff;
    --accent-2: #00d4ff;
    --shadow: 0 22px 70px rgba(0, 0, 0, 0.34);
    --bubble-shadow: 0 6px 18px rgba(0, 0, 0, 0.18);
}

:root[data-theme="light"] {
    color-scheme: light;
    --bg-1: #f8fbff;
    --bg-2: #e8edf7;
    --bg-glow: rgba(10, 132, 255, 0.2);
    --panel: rgba(255, 255, 255, 0.72);
    --panel-strong: rgba(255, 255, 255, 0.94);
    --text: #151821;
    --muted: #687082;
    --line: rgba(20, 25, 35, 0.1);
    --input: rgba(255, 255, 255, 0.82);
    --incoming: rgba(255, 255, 255, 0.92);
    --outgoing: linear-gradient(135deg, #0a84ff 0%, #62d2ff 100%);
    --accent: #0a84ff;
    --accent-2: #00a6d6;
    --shadow: 0 22px 70px rgba(29, 38, 64, 0.16);
    --bubble-shadow: 0 10px 28px rgba(42, 51, 78, 0.12);
}

* {
    box-sizing: border-box;
}

html,
body {
    height: 100%;
}

body {
    margin: 0;
    background:
        radial-gradient(circle at top left, var(--bg-glow), transparent 30rem),
        radial-gradient(circle at bottom right, rgba(0, 212, 255, 0.16), transparent 25rem),
        linear-gradient(135deg, var(--bg-1) 0%, var(--bg-2) 100%);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI", Roboto, sans-serif;
    overflow: hidden;
}

button,
input {
    font: inherit;
}

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

.name-screen {
    position: fixed;
    inset: 0;
    z-index: 10;
    display: grid;
    place-items: center;
    padding: 20px;
    background: rgba(5, 7, 15, 0.56);
    backdrop-filter: blur(24px);
}

.name-screen[hidden],
.chat-shell[hidden] {
    display: none;
}

.name-card {
    width: min(100%, 340px);
    padding: 24px;
    border: 1px solid var(--line);
    border-radius: 30px;
    background: var(--panel-strong);
    box-shadow: var(--shadow);
    text-align: center;
}

.name-card h1 {
    margin: 0 0 6px;
    font-size: 30px;
    letter-spacing: -0.04em;
}

.name-card p {
    margin: 0 0 18px;
    color: var(--muted);
    font-size: 14px;
}

.name-card input,
.sendMsg input {
    width: 100%;
    border: 1px solid var(--line);
    outline: none;
    background: var(--input);
    color: var(--text);
}

.name-card input::placeholder,
.sendMsg input::placeholder {
    color: var(--muted);
}

.name-card input {
    height: 46px;
    padding: 0 16px;
    border-radius: 17px;
}

.primary-btn {
    width: 100%;
    height: 46px;
    margin-top: 12px;
    border: 0;
    border-radius: 17px;
    background: var(--outgoing);
    color: white;
    font-weight: 800;
    cursor: pointer;
    box-shadow: 0 12px 24px rgba(10, 132, 255, 0.24);
}

.chat-shell {
    width: min(100%, 860px);
    height: 100dvh;
    margin: 0 auto;
    display: grid;
    grid-template-rows: auto auto 1fr auto;
    background: rgba(255, 255, 255, 0.04);
    box-shadow: 0 0 0 1px var(--line);
}

.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: max(10px, env(safe-area-inset-top)) 12px 8px;
    background: var(--panel);
    border-bottom: 1px solid var(--line);
    backdrop-filter: blur(20px);
}

.chat-header h1 {
    margin: 0;
    font-size: 17px;
    letter-spacing: -0.04em;
}

#connectionStatus {
    display: block;
    margin-top: 1px;
    color: var(--muted);
    font-size: 11px;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 6px;
}

.theme-toggle,
.members-pill,
.user-chip {
    border: 1px solid var(--line);
    background: rgba(255, 255, 255, 0.08);
    color: var(--muted);
}

.theme-toggle {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    cursor: pointer;
}

.members-pill {
    padding: 6px 9px;
    border-radius: 999px;
    font-size: 11px;
    white-space: nowrap;
}

.users-strip {
    display: flex;
    gap: 5px;
    padding: 4px 8px;
    overflow-x: auto;
    border-bottom: 1px solid var(--line);
}

.user-chip {
    flex: 0 0 auto;
    max-width: 82px;
    padding: 3px 6px;
    border-radius: 999px;
    font-size: 10px;
    line-height: 1.2;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.user-chip::before {
    content: "●";
    margin-right: 3px;
    color: #35d07f;
    font-size: 7px;
}

.msgBox {
    min-height: 0;
    padding: 6px 9px 5px;
    overflow-y: auto;
    scroll-behavior: smooth;
    background:
        linear-gradient(rgba(7, 9, 18, 0.76), rgba(7, 9, 18, 0.7)),
        url("wall.jpg") center / cover;
}

:root[data-theme="light"] .msgBox {
    background:
        linear-gradient(rgba(248, 251, 255, 0.58), rgba(232, 237, 247, 0.6)),
        url("wall.jpg") center / cover;
}

.message {
    position: relative;
    width: fit-content;
    max-width: min(82%, 500px);
    margin: 3px 0;
    clear: both;
    animation: popIn 0.18s ease-out;
    display: flex;
    align-items: flex-end;
    gap: 6px;
}

.message + .message {
    margin-top: 2px;
}

.message.left {
    float: left;
    flex-direction: row;
}

.message.right {
    float: right;
    flex-direction: row;
}

.message.center {
    float: none;
    max-width: 88%;
    margin: 4px auto;
    padding: 3px 8px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: rgba(120, 120, 128, 0.16);
    color: var(--muted);
    text-align: center;
    font-size: 9.5px;
    box-shadow: none;
}

.textbox {
    position: relative;
    min-width: 44px;
    padding: 7px 10px 6px;
    border: 1px solid var(--line);
    border-radius: 14px;
    font-size: 13px;
    line-height: 1.32;
    overflow-wrap: anywhere;
    box-shadow: var(--bubble-shadow);
    z-index: 1;
}

.message-text {
    display: block;
    padding-right: 0;
}

.left .textbox {
    background: var(--incoming);
}

.right .textbox {
    background: var(--outgoing);
    color: white;
    border-color: rgba(255, 255, 255, 0.18);
}

.username {
    padding: 0;
    color: var(--muted);
    font-size: 9.5px;
    line-height: 1.2;
    font-weight: 500;
}

.message-meta {
    display: block;
    color: var(--muted);
    font-size: 9px;
    line-height: 1.2;
    opacity: 0.75;
    white-space: nowrap;
}

.message-content {
    display: flex;
    flex-direction: column;
    gap: 3px;
    max-width: 100%;
}

.message-footer {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 0 4px;
    min-height: 16px;
}

.message.right .message-footer {
    justify-content: flex-end;
}

.message.left .message-footer {
    justify-content: flex-start;
}

.media .textbox {
    padding: 4px;
}

.message-meta {
    display: block;
    color: var(--muted);
    font-size: 9.5px;
    line-height: 1.3;
    opacity: 0.75;
    white-space: nowrap;
}

.right .message-meta {
    color: var(--muted);
    opacity: 0.7;
    text-align: right;
}

.media-message {
    display: grid;
    gap: 6px;
}

.chat-media {
    display: block;
    width: min(190px, 56vw);
    max-height: 210px;
    object-fit: cover;
    border-radius: 12px;
    background: rgba(0, 0, 0, 0.2);
}

video.chat-media {
    object-fit: contain;
}

.media-caption {
    max-width: min(190px, 56vw);
    padding: 0 4px 2px;
    color: inherit;
    opacity: 0.72;
    font-size: 10px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sendMsg {
    display: grid;
    gap: 5px;
    padding: 6px 9px max(6px, env(safe-area-inset-bottom));
    background: var(--panel);
    border-top: 1px solid var(--line);
    backdrop-filter: blur(20px);
}

.composer-row {
    display: flex;
    align-items: center;
    gap: 6px;
}

.sendMsg input {
    height: 36px;
    padding: 0 12px;
    border-radius: 999px;
    font-size: 13px;
}

.attach-btn,
.send-btn {
    width: 36px;
    height: 36px;
    flex: 0 0 36px;
    border: 0;
    border-radius: 50%;
    color: white;
    cursor: pointer;
}

.attach-btn {
    background: rgba(255, 255, 255, 0.12);
    color: var(--text);
    border: 1px solid var(--line);
    font-size: 18px;
    line-height: 1;
}

.send-btn {
    background: var(--outgoing);
    font-size: 14px;
    box-shadow: 0 12px 24px rgba(10, 132, 255, 0.22);
}

.reply-preview {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    min-width: 0;
    padding: 5px 7px;
    border-left: 3px solid var(--accent-2);
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.08);
}

.reply-preview[hidden] {
    display: none;
}

.reply-preview div {
    min-width: 0;
}

.reply-preview strong,
.reply-quote strong {
    display: block;
    color: var(--accent-2);
    font-size: 10px;
}

.reply-preview span,
.reply-quote span {
    display: block;
    color: var(--muted);
    font-size: 10px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.reply-preview button {
    width: 24px;
    height: 24px;
    flex: 0 0 24px;
    border: 0;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.12);
    color: var(--text);
    cursor: pointer;
}

.reply-quote {
    display: grid;
    gap: 1px;
    max-width: 100%;
    margin-bottom: 4px;
    padding: 4px 6px;
    border-left: 2px solid rgba(255, 255, 255, 0.55);
    border-radius: 8px;
    background: rgba(0, 0, 0, 0.14);
}

.left .reply-quote {
    border-left-color: var(--accent-2);
}

.reply-btn {
    position: static;
    display: inline-grid;
    place-items: center;
    width: 22px;
    height: 22px;
    flex: 0 0 22px;
    align-self: flex-start;
    margin-top: 0;
    border: 1px solid var(--line);
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.08);
    color: var(--muted);
    cursor: pointer;
    opacity: 0.6;
    font-size: 10px;
    line-height: 1;
    transition: opacity 0.15s ease, transform 0.15s ease, background 0.15s ease;
}

.message:hover .reply-btn,
.message:focus-within .reply-btn {
    opacity: 1;
    transform: scale(1.04);
    background: rgba(255, 255, 255, 0.14);
}

.message-reactions {
    position: absolute;
    z-index: 3;
    top: -8px;
    min-width: 20px;
    padding: 1px 5px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--panel-strong);
    box-shadow: var(--bubble-shadow);
    font-size: 12px;
    line-height: 1.2;
}

.message-reactions[hidden] {
    display: none;
}

.message.left .message-reactions {
    right: 25px;
}

.message.right .message-reactions {
    left: 25px;
}

.reaction-picker {
    position: absolute;
    z-index: 8;
    bottom: calc(100% + 6px);
    display: flex;
    gap: 3px;
    padding: 4px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--panel-strong);
    box-shadow: var(--shadow);
    backdrop-filter: blur(18px);
}

.message.left .reaction-picker {
    left: 0;
}

.message.right .reaction-picker {
    right: 0;
}

.reaction-picker button {
    width: 28px;
    height: 28px;
    border: 0;
    border-radius: 50%;
    background: transparent;
    cursor: pointer;
    font-size: 16px;
    transition: transform 0.12s ease, background 0.12s ease;
}

.reaction-picker button:hover {
    background: rgba(255, 255, 255, 0.1);
    transform: scale(1.14);
}

.msgBox::after {
    content: "";
    display: table;
    clear: both;
}

@keyframes popIn {
    from {
        opacity: 0;
        transform: translateY(5px) scale(0.98);
    }

    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

@media (min-width: 720px) {
    body {
        padding: 18px;
    }

    .chat-shell {
        height: calc(100dvh - 36px);
        border-radius: 32px;
        overflow: hidden;
    }

    .chat-header {
        padding: 14px 18px 10px;
    }

    .users-strip,
    .msgBox,
    .sendMsg {
        padding-left: 20px;
        padding-right: 20px;
    }

    .textbox {
        font-size: 13px;
    }
}

@media (hover: none) {
    .reply-btn {
        opacity: 0.58;
    }

    .message:active .reply-btn {
        opacity: 1;
    }
}
