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

:root {
    --primary: #9b87f5;
    --primary-hover: #b5a3ff;
    --primary-dark: #7c6bc9;
    --background: #0f0f0f;
    --surface: #141414;
    --surface-light: #222222;
    --surface-hover: #272727;
    --text: #e8e6f0;
    --text-dim: #8b86a3;
    --accent: #d946ef;
    --ping: #ef4444;
    --success: #22c55e;
    --border: #2a2738;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background: var(--background);
    color: var(--text);
    height: 100dvh;
    touch-action: pan-y;
}

#auth-screen,
#chat-screen {
    display: none;
    height: 100dvh;
}

#auth-screen.active,
#chat-screen.active {
    display: flex;
}

#auth-screen {
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 20px;
    text-align: center;
}

@keyframes float {

    0%,
    100% {
        transform: translateY(0px);
    }

    50% {
        transform: translateY(-10px);
    }
}

h1 {
    font-size: 32px;
    margin-bottom: 10px;
}

p {
    color: var(--text-dim);
    margin-bottom: 30px;
}

.btn {
    background-color: #7c6bc9;
    color: white;
    border: none;
    padding: 15px 40px;
    border-radius: 12px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 4px 16px rgba(155, 135, 245, 0.3);
}

.btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 24px rgba(155, 135, 245, 0.4);
}

.btn:active {
    transform: translateY(0);
}

.server-input {
    width: 100%;
    max-width: 400px;
    padding: 15px;
    background: var(--surface);
    border: 2px solid var(--border);
    border-radius: 12px;
    color: var(--text);
    font-size: 16px;
    margin-bottom: 15px;
    transition: border-color 0.3s ease;
}

.server-input:focus {
    outline: none;
    border-color: var(--primary);
}

#chat-screen {
    flex-direction: column;
}

.header {
    background: var(--surface);
    padding: 7px;
    display: flex;
    align-items: center;
    gap: 10px;
    border-bottom: 1px solid var(--border);
    backdrop-filter: blur(10px);
}

.server-info {
    display: flex;
    align-items: center;
    gap: 10px;
    cursor: pointer;
    padding: 8px;
    border-radius: 8px;
    transition: background 0.2s ease;
    position: relative;
}

.server-info:hover {
    background: var(--surface-hover);
}

.server-icon {
    width: 40px;
    height: 40px;
    border-radius: 12px;
    background: linear-gradient(135deg, var(--primary), var(--accent));
    box-shadow: 0 2px 8px rgba(155, 135, 245, 0.3);
}

.header-text {
    flex: 1;
}

.server-name {
    font-weight: 600;
    font-size: 16px;
    display: flex;
    align-items: center;
    gap: 5px;
}

.dropdown-arrow {
    font-size: 12px;
    color: var(--text-dim);
    transition: transform 0.3s ease;
}

.dropdown-arrow.open {
    transform: rotate(180deg);
}

.server-dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    background: var(--surface-light);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 8px;
    min-width: 250px;
    max-height: 400px;
    overflow-y: auto;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
    z-index: 10000;
    display: none;
    animation: slideDown 0.3s ease;
}

@keyframes slideDown {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }

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

.server-dropdown.active {
    display: block;
}

.server-dropdown-item {
    padding: 12px;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 10px;
    transition: background 0.2s ease;
}

.server-dropdown-item:hover {
    background: var(--surface-hover);
}

.server-dropdown-item.active {
    background: var(--primary);
    color: white;
}

.server-dropdown-item img {
    width: 32px;
    height: 32px;
    border-radius: 8px;
}

.add-server-btn {
    background: var(--surface-hover);
    color: var(--primary);
    font-weight: 600;
    border-top: 1px solid var(--border);
    margin-top: 8px;
    padding-top: 8px;
}

.channel-name {
    font-size: 14px;
    color: var(--text-dim);
}

.content {
    display: flex;
    flex: 1;
    overflow: hidden;
}

.channels {
    width: 250px;
    background: var(--surface);
    overflow-y: auto;
    padding: 10px;
    transition: transform 0.3s ease;
    border-right: 1px solid var(--border);
}

#members-list {
    background: #141414;
    border-left: 1px solid #222;
    padding: 12px;
    overflow-y: auto;
    color: #ccc;
    font-size: 14px;
    min-width: 250px;
}

#members-list h2 {
    font-size: 12px;
    font-weight: 600;
    color: #888;
    margin-bottom: 8px;
    text-transform: uppercase;
}

@media (max-width: 768px) {
    .channels {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        z-index: 100;
        transform: translateX(-100%);
        box-shadow: 2px 0 10px rgba(0, 0, 0, 0.5);
    }

    .channels.open {
        transform: translateX(0);
    }

    #members-list {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.7);
        z-index: 100;
        transform: translateX(100%);
        box-shadow: 2px 0 10px rgba(0, 0, 0, 0.5);
    }

    #members-list.open {
        transform: translateX(0);
    }

    .overlay {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.7);
        z-index: 99;
        backdrop-filter: blur(4px);
    }

    .overlay.active {
        display: block;
    }
}

.channel-item {
    padding: 7px 7px;
    margin-bottom: 2px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 15px;
    display: flex;
    align-items: center;
    gap: 5px;
    position: relative;
}

.channel-item.active {
    border-color: var(--surface-light);
    border-radius: 10px;
    border-style: solid;
    color: white;
}

.channel-item.has-ping::after {
    content: '';
    position: absolute;
    right: 10px;
    width: 8px;
    height: 8px;
    background: var(--ping);
    border-radius: 50%;
}

@keyframes pulse {

    0%,
    100% {
        opacity: 1;
        transform: scale(1);
    }

    50% {
        opacity: 0.5;
        transform: scale(1.2);
    }
}

.channel-separator {
    height: 10px;
    border-top: 1px solid var(--border);
    margin-top: 10px;
    margin-bottom: 0;
}

.menu-btn {
    display: none;
    width: 40px;
    height: 40px;
    background: none;
    border: none;
    color: var(--text);
    font-size: 24px;
    cursor: pointer;
}

@media (max-width: 768px) {
    .menu-btn {
        display: flex;
        align-items: center;
        justify-content: center;
    }
}

.messages-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    height: 100%;
}

.messages {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 15px;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.messages::before {
    content: '';
    flex: 1;
    min-height: 0;
}

.message {
    margin-bottom: 15px;
    display: flex;
    gap: 10px;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

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

.avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    flex-shrink: 0;
    background: var(--surface-light);
    border: 2px solid var(--border);
}

.message-content {
    flex: 1;
    min-width: 0;
}

.message-header {
    display: flex;
    align-items: baseline;
    gap: 8px;
    margin-bottom: 4px;
}

.username {
    font-weight: 600;
    font-size: 14px;
}

.timestamp {
    font-size: 12px;
    color: var(--text-dim);
}

.message-text {
    font-size: 15px;
    line-height: 1.4;
    word-wrap: break-word;
    white-space: pre-wrap;
    margin-left: 0;
    padding: 1px 0;
    font-size: 15px;
    color: #ddd;
}

.mention {
    background: rgba(155, 135, 245, 0.2);
    color: var(--primary);
    padding: 2px 4px;
    border-radius: 4px;
    font-weight: 600;
}

.mention.me {
    background: rgba(239, 68, 68, 0.2);
    color: var(--ping);
    animation: mentionGlow 2s ease-in-out infinite;
}

@keyframes mentionGlow {

    0%,
    100% {
        box-shadow: 0 0 0 rgba(239, 68, 68, 0);
    }

    50% {
        box-shadow: 0 0 8px rgba(239, 68, 68, 0.4);
    }
}

.reply-preview {
    background: var(--surface);
    border-left: 3px solid var(--primary);
    padding: 8px 12px;
    margin-bottom: 5px;
    border-radius: 4px;
    font-size: 13px;
    color: var(--text-dim);
}

.input-area {
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding: 10px;
}

.reply-bar {
    display: none;
    background: var(--surface-light);
    padding: 10px 15px;
    margin-bottom: 5px;
    border-radius: 8px;
    font-size: 13px;
    justify-content: space-between;
    align-items: center;
    border-left: 3px solid var(--primary);
}

.reply-bar.active {
    display: flex;
}

.close-reply {
    cursor: pointer;
    padding: 5px;
    color: var(--text-dim);
    transition: color 0.2s ease;
}

.close-reply:hover {
    color: var(--text);
}

.input-wrapper {
    display: flex;
    gap: 10px;
    align-items: flex-end;
}

#message-input {
    flex: 1;
    background: var(--surface-light);
    border: 2px solid var(--border);
    padding: 12px 15px;
    border-radius: 12px;
    color: var(--text);
    font-size: 15px;
    resize: none;
    max-height: 100px;
    font-family: inherit;
}

#message-input:focus {
    outline: none;
}

.send-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    border: none;
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    box-shadow: 0 2px 8px rgba(155, 135, 245, 0.3);
    margin-bottom: 4px;
}

.send-btn:hover {
    transform: scale(1.05);
    box-shadow: 0 4px 12px rgba(155, 135, 245, 0.4);
}

.send-btn:active {
    transform: scale(0.95);
}

.loading {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
}

.spinner {
    width: 30px;
    height: 30px;
    border: 3px solid var(--surface-light);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

.error {
    color: var(--ping);
    padding: 10px;
    text-align: center;
}

::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: var(--surface);
}

::-webkit-scrollbar-thumb {
    background: var(--surface-light);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--surface-hover);
}

.message-group {
    display: flex;
    align-items: flex-start;
}

.message-group .avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    margin-right: 10px;
    flex-shrink: 0;
}

.message-group-content {
    display: flex;
    flex-direction: column;
    width: 100%;
}

.message-header {
    display: flex;
    align-items: baseline;
    gap: 6px;
    margin-bottom: 2px;
    margin-top: 10px;
}

.username {
    font-weight: 600;
    font-size: 14px;
}

.timestamp {
    color: #999;
    font-size: 12px;
}

/* Compact grouped messages */
.message-single {
    display: flex;
    align-items: flex-start;
    margin-left: 45px;
}

#chat-container {
    display: grid;
    grid-template-columns: 1fr 240px;
    /* main chat + sidebar */
    height: 100%;
    background: #111;
}

#messages-container {
    overflow-y: auto;
    padding: 16px;
}

.member {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 6px;
    padding: 4px 6px;
    border-radius: 4px;
    cursor: pointer;
    transition: background 0.15s;
    position: relative;
}

.member:hover {
    background: rgba(255, 255, 255, 0.05);
}

.member img {
    width: 28px;
    height: 28px;
    border-radius: 50%;
}

.member .name {
    color: #fff;
    font-size: 14px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.member::after {
    content: "";
    position: absolute;
    bottom: 2px;
    right: 2px;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--status-color, #555);
}

.member.online::after {
    background: #43b581;
}

.member.offline::after {
    background: #555;
}

.member.idle::after {
    background: #faa61a;
}

.channels {
    width: 250px;
    background: var(--surface);
    overflow-y: auto;
    padding: 10px 10px 0 10px;
    transition: transform 0.3s ease;
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
}

.channels-list {
    flex: 1;
    overflow-y: auto;
    padding-bottom: 10px;
}

.user-section {
    background: var(--surface-light);
    border-radius: 8px;
    padding: 8px;
    margin-bottom: 8px;
    margin-top: auto;
    display: flex;
    align-items: center;
    gap: 8px;
    border: 1px solid var(--border);
    cursor: pointer;
    transition: background 0.2s ease;
    position: relative;
}

.user-section:hover {
    background: var(--surface-hover);
}

.user-section .avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    flex-shrink: 0;
}

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

.user-info .username {
    font-size: 14px;
    font-weight: 600;
    color: var(--text);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.user-info .status {
    font-size: 12px;
    color: var(--text-dim);
}

.user-menu {
    position: absolute;
    bottom: 100%;
    left: 0;
    right: 0;
    background: var(--surface-light);
    border: 1px solid var(--border);
    border-radius: 8px;
    margin-bottom: 8px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
    display: none;
    animation: slideUp 0.2s ease;
    z-index: 1000;
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

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

.user-menu.active {
    display: block;
}

.user-menu-item {
    padding: 12px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 10px;
    transition: background 0.2s ease;
    font-size: 14px;
}

.user-menu-item:first-child {
    border-radius: 8px 8px 0 0;
}

.user-menu-item:last-child {
    border-radius: 0 0 8px 8px;
}

.user-menu-item:hover {
    background: var(--surface-hover);
}

.user-menu-item.danger {
    color: var(--ping);
}

.user-menu-item.danger:hover {
    background: rgba(239, 68, 68, 0.1);
}

.ping-badge {
    background: #ff4444;
    color: white;
    border-radius: 10px;
    padding: 2px 6px;
    font-size: 11px;
    font-weight: bold;
    margin-left: auto;
}

.message-text.mentioned {
    background: rgba(255, 200, 0, 0.1);
    border-left: 3px solid #ffc800;
    padding-left: 8px;
    margin-left: -8px;
}

.channel-item {
    display: flex;
    align-items: center;
    gap: 8px;
}

.message-reply {
    max-width: 60%;
    overflow: hidden;
    text-overflow: ellipsis;
    display: flex;
    align-items: center;
}

.message-reply img {
    width: 32px;
    height: 32px;
    border-radius: 50%;

    margin-right: 8px;
}

.message-reply div {
    font-size: 14px;
    color: var(--text-dim);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
    min-width: 0;
    margin-right: 8px;
}

.context-menu {
    position: fixed;
    background: #1e1f22;
    border: 1px solid #2b2d31;
    border-radius: 6px;
    padding: 6px 0;
    width: 180px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.4);
    z-index: 99999;
    display: none;
}

.emoji {
    width: 24px;
    height: 24px;
}

.context-menu-item {
    padding: 8px 12px;
    font-size: 14px;
    color: #fff;
    cursor: pointer;
    user-select: none;
}

.context-menu-item:hover {
    background: #5865f2;
}

.typing {
    display: block;
    align-items: center;
    gap: 8px;
    padding: 8px;
    margin-bottom: 8px;
    font-size: 15px;
}

.mention {
    color: #9b87f5;
    background: rgba(155, 135, 245, 0.1);
    padding: 2px 6px;
    border-radius: 6px;
    font-weight: 600;
    cursor: pointer;
}
.mention:hover {
    background: rgba(155, 135, 245, 0.2);
}