:root {
    --bg-dark: #202225;
    --bg-sidebar: #2f3136;
    --bg-chat: #36393f;
    --bg-header: #2f3136;
    --text-main: #dcddde;
    --text-muted: #72767d;
    --accent-color: #5865f2;
    --target-color: #ed4245;
    --hover-color: #393c43;
    --border-color: #26272d;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: "Segoe UI", sans-serif;
    background: var(--bg-dark);
    color: var(--text-main);
    height: 100vh;
    /* 解决移动端地址栏遮挡问题 */
    height: 100dvh;
    overflow: hidden;
}

.app-layout {
    display: flex;
    height: 100%;
    width: 100%;
    position: relative;
}

/* Sidebar */
.sidebar {
    width: 260px;
    background: var(--bg-sidebar);
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--border-color);
    flex-shrink: 0;
}

.sidebar-header {
    padding: 20px;
    border-bottom: 1px solid var(--border-color);
}

.status-badge {
    font-size: 11px;
    color: var(--text-muted);
    background: rgba(0, 0, 0, 0.2);
    padding: 2px 6px;
    border-radius: 4px;
    display: inline-block;
    margin-top: 5px;
}

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

.room-item {
    display: flex;
    align-items: center;
    padding: 12px 10px;
    margin-bottom: 5px;
    border-radius: 5px;
    cursor: pointer;
    transition: 0.2s;
}

.room-item:hover {
    background: var(--hover-color);
}

.room-item.active {
    background: rgba(88, 101, 242, 0.3);
    border-left: 3px solid var(--accent-color);
}

.room-icon {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    margin-right: 12px;
    object-fit: cover;
    flex-shrink: 0;
}

.room-name {
    font-size: 15px;
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Main */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--bg-chat);
    min-width: 0;
}

.chat-header {
    height: 60px;
    background: var(--bg-header);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 15px;
    flex-shrink: 0;
}

.header-info {
    display: flex;
    align-items: center;
    overflow: hidden;
}

#current-room-icon {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    margin-right: 10px;
    display: none;
}

#current-room-name {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 16px;
}

/* Back Button (Default Hidden) */
.back-btn {
    display: none;
    background: none;
    border: none;
    color: var(--text-main);
    font-size: 18px;
    padding: 8px;
    margin-right: 10px;
    cursor: pointer;
}

/* Toggle */
.toggle-wrapper {
    display: flex;
    align-items: center;
    cursor: pointer;
}

.toggle-text {
    margin-left: 8px;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-muted);
    white-space: nowrap;
}

.toggle-wrapper input {
    display: none;
}

.toggle-label {
    width: 40px;
    height: 20px;
    background: #4f545c;
    display: block;
    border-radius: 100px;
    position: relative;
    cursor: pointer;
    transition: 0.3s;
    flex-shrink: 0;
}

.toggle-label::after {
    content: "";
    position: absolute;
    top: 2px;
    left: 2px;
    width: 16px;
    height: 16px;
    background: #fff;
    border-radius: 90px;
    transition: 0.3s;
}

input:checked+.toggle-label {
    background: var(--target-color);
}

input:checked+.toggle-label::after {
    transform: translateX(20px);
}

input:checked~.toggle-text {
    color: var(--target-color);
}

/* Chat Area */
.chat-scroll-area {
    flex: 1;
    overflow-y: auto;
    padding: 20px 0;
    display: flex;
    flex-direction: column;
    -webkit-overflow-scrolling: touch;
    position: relative;
    overflow-anchor: none;
}

.message-list {
    display: flex;
    flex-direction: column;
    padding: 0 15px;
    gap: 16px;
}

.spinner {
    text-align: center;
    color: var(--text-muted);
    padding: 10px;
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 10;
    background: rgba(32, 34, 37, 0.9);
    backdrop-filter: blur(2px);
}

/* Message */
.message-row {
    display: flex;
    align-items: flex-start;
    animation: fadeIn 0.2s ease;
}

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

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

.avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    margin-right: 12px;
    flex-shrink: 0;
    cursor: pointer;
}

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

.meta-info {
    margin-bottom: 4px;
    display: flex;
    align-items: baseline;
    flex-wrap: wrap;
}

.username {
    font-weight: bold;
    font-size: 15px;
    margin-right: 8px;
    color: #fff;
}

.timestamp {
    font-size: 11px;
    color: var(--text-muted);
}

/* Reply */
.reply-block {
    display: flex;
    align-items: center;
    background: rgba(0, 0, 0, 0.1);
    border-left: 3px solid var(--text-muted);
    padding: 5px 10px;
    margin-bottom: 6px;
    border-radius: 4px;
    font-size: 13px;
    color: var(--text-muted);
    cursor: pointer;
    max-width: 100%;
}

.reply-block:hover {
    background: rgba(0, 0, 0, 0.2);
}

.reply-user {
    font-weight: bold;
    margin-right: 5px;
    color: var(--text-main);
    white-space: nowrap;
}

/* Bubble */
.bubble {
    font-size: 15px;
    line-height: 1.5;
    color: var(--text-main);
    white-space: pre-wrap;
    word-break: break-word;
}

.is-target .username {
    color: var(--target-color);
}

.is-target .bubble {
    color: #fff;
    font-weight: 400;
}

/* Media */
.media-wrapper {
    margin-top: 8px;
    position: relative;
    display: inline-block;
    max-width: 100%;
}

.chat-media {
    max-width: 100%;
    max-height: 350px;
    border-radius: 8px;
    cursor: zoom-in;
    border: 1px solid rgba(0, 0, 0, 0.3);
    background: #000;
    display: block;
}

.dl-btn {
    position: absolute;
    bottom: 10px;
    right: 10px;
    background: rgba(0, 0, 0, 0.7);
    color: #fff;
    border-radius: 50%;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    text-decoration: none;
    transition: 0.2s;
}

/* Reactions */
.reaction-list {
    display: flex;
    gap: 6px;
    margin-top: 6px;
    flex-wrap: wrap;
}

.reaction-item {
    background: #2f3136;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 2px 6px;
    font-size: 12px;
    color: var(--text-muted);
}

::-webkit-scrollbar {
    width: 6px;
}

::-webkit-scrollbar-track {
    background: #2f3136;
}

::-webkit-scrollbar-thumb {
    background: #202225;
    border-radius: 4px;
}

/* ================= Mobile Adaptation ================= */
@media (max-width: 768px) {
    .app-layout {
        overflow: hidden;
    }

    .sidebar {
        width: 100%;
        height: 100%;
        position: absolute;
        z-index: 10;
        transform: translateX(0);
        transition: transform 0.3s ease-in-out;
    }

    .main-content {
        width: 100%;
        height: 100%;
        position: absolute;
        z-index: 5;
        background: var(--bg-dark);
        /* Prevent see-through */
    }

    .back-btn {
        display: block;
    }

    /* 状态管理：
       1. 默认状态：Sidebar在最上层 (transform: 0)
       2. 聊天状态 (.mobile-show-chat)：Sidebar移出屏幕 (transform: -100%)
    */

    /* 当激活聊天时，侧边栏向左滑出 */
    .app-layout.mobile-show-chat .sidebar {
        transform: translateX(-100%);
    }

    /* 优化移动端触摸体验 */
    .room-item {
        padding: 15px;
    }

    .toggle-text {
        font-size: 12px;
    }
}

/* Translation */
.translation-area {
    margin-top: 8px;
    font-size: 13px;
}

.translate-btn {
    color: var(--accent-color);
    text-decoration: none;
    font-size: 12px;
    opacity: 0.8;
}

.translate-btn:hover {
    opacity: 1;
    text-decoration: underline;
}

.translation-result {
    margin-top: 5px;
    padding: 8px;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 4px;
    border-left: 2px solid var(--accent-color);
    color: #e0e0e0;
    line-height: 1.4;
}

.translation-tag {
    font-weight: bold;
    color: var(--text-muted);
    margin-right: 5px;
    font-size: 11px;
    text-transform: uppercase;
}

.retranslate-btn {
    color: var(--text-muted);
    font-size: 14px;
    text-decoration: none;
    margin-left: 8px;
    vertical-align: middle;
    cursor: pointer;
    transition: 0.2s;
    user-select: none;
}

.retranslate-btn:hover {
    color: var(--accent-color);
    transform: rotate(180deg);
    display: inline-block;
}

/* Media Viewer Modal */
.media-viewer {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.9);
    z-index: 9999;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
}

.viewer-content {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
}

#media-container img,
#media-container video {
    max-width: 100%;
    max-height: 90vh;
    object-fit: contain;
    border-radius: 4px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}

.viewer-close {
    position: absolute;
    top: 20px;
    right: 20px;
    background: none;
    border: none;
    color: #fff;
    font-size: 40px;
    cursor: pointer;
    z-index: 10000;
    line-height: 1;
    opacity: 0.8;
    transition: 0.2s;
}

.viewer-close:hover {
    opacity: 1;
    transform: scale(1.1);
}

/* Custom Download Button (CSS Drawn) */
.viewer-download {
    position: absolute;
    bottom: 30px;
    right: 30px;
    width: 50px;
    height: 50px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    transition: 0.2s;
    border: 1px solid rgba(255, 255, 255, 0.3);
    z-index: 10000;
}

.viewer-download:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: translateY(-2px);
}

/* Refined Download Button (Standard Arrow + Tray) */
.download-icon {
    box-sizing: border-box;
    position: relative;
    display: block;
    width: 20px;
    height: 20px;
    border-bottom: 2px solid #fff;
}

.download-icon::before {
    content: "";
    display: block;
    box-sizing: border-box;
    position: absolute;
    width: 2px;
    height: 10px;
    background: #fff;
    left: 50%;
    transform: translateX(-50%);
    top: 0;
}

.download-icon::after {
    content: "";
    display: block;
    box-sizing: border-box;
    position: absolute;
    width: 8px;
    height: 8px;
    border-bottom: 2px solid #fff;
    border-right: 2px solid #fff;
    transform: translateX(-50%) rotate(45deg);
    left: 50%;
    top: 3px;
}