/* 基礎佈局 */
body {
    margin: 0;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    transition: background-color 0.3s, color 0.3s;
}

#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

header {
    background: #4CAF50;
    color: white;
    padding: 10px 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

header h1 {
    margin: 0;
    font-size: 1.5em;
}

.controls button {
    margin-left: 10px;
    padding: 6px 12px;
    background: #fff;
    color: #333;
    border: 1px solid #ccc;
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.2s;
}

.controls button:hover {
    background: #f0f0f0;
}

main {
    flex: 1;
    display: flex;
    overflow: hidden;
}

#chat {
    flex: 3;
    padding: 15px; /* 為外部用戶名保留一些空間，如果用戶名很長，可能需要調整 */
    overflow-y: auto;
    background: #f9f9f9;
    border-right: 1px solid #ddd;
}

#userlist {
    flex: 1;
    min-width: 180px; /* 使用者列表最小寬度 */
    padding: 15px;
    border-left: 1px solid #ccc;
    overflow-y: auto;
    background: #fff;
}

#userlist.hidden {
    display: none;
}

#userlist h3 {
    margin-top: 0;
    color: #333;
}
#userlist div, #userlist p {
    padding: 4px 0;
    color: #555;
}


footer {
    display: flex;
    padding: 10px 15px;
    background: #eee;
    align-items: center;
    border-top: 1px solid #ddd;
    gap: 10px; /* 頁腳項目之間的間距 */
}

footer label {
    margin-right: 5px; /* 減小邊距 */
    font-size: 14px;
    white-space: nowrap;
}

footer input[type="text"] {
    padding: 8px;
    border: 1px solid #ccc;
    border-radius: 4px;
}

footer #username {
    width: 150px;
}

footer #message {
    flex: 1; /* 訊息輸入框佔據剩餘空間 */
}

footer button {
    padding: 8px 12px; /* 統一內邊距 */
    border: 1px solid #007bff;
    background-color: #007bff;
    color: white;
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.2s;
}
footer button:hover {
    background-color: #0056b3;
}
footer button:disabled {
    background-color: #aaa;
    border-color: #aaa;
    cursor: not-allowed;
}
footer #image-button {
    background-color: #6c757d;
    border-color: #6c757d;
    padding: 8px 10px;
}
footer #image-button:hover:not(:disabled) {
    background-color: #545b62;
}


/* 訊息氣泡 */
.message-left, .message-right {
    padding: 10px 15px;
    margin-top: 15px; /* 保持原有的上下間距 */
    margin-bottom: 15px; /* 保持原有的上下間距 */
    border-radius: 12px; /* 整體圓角 */
    max-width: 65%; /* 聊天框的最大寬度上限 */
    width: -webkit-fit-content; /* 讓寬度適應內容 (Chrome/Safari) */
    width: -moz-fit-content;    /* 讓寬度適應內容 (Firefox) */
    width: fit-content;         /* 讓寬度適應內容 (標準) */
    word-wrap: break-word; /* 確保長單詞換行 */
    line-height: 1.4;
    position: relative; /* 為偽元素及用戶名定位 */
}

.message-left {
    background: #e0f7fa; /* 接收訊息的淺藍色 */
    color: #333;
    text-align: left; /* 其他用戶的內容靠左對齊 */
    margin-right: auto; /* 推到左邊 */
    margin-left: 50px; /* 為左側用戶名留出空間 */
    border-bottom-left-radius: 0px; /* 使左下角變直角以連接尾巴 */
}

.message-right {
    background: #c8e6c9; /* 發送訊息的淺綠色 */
    color: #333;
    text-align: right; /* 自身用戶的內容靠右對齊 */
    margin-left: auto; /* 推到右邊 */
    margin-right: 50px; /* 為右側用戶名留出空間 */
    border-bottom-right-radius: 0px; /* 使右下角變直角以連接尾巴 */
}

/* 語音氣泡尾巴 */
.message-left::after {
    content: '';
    position: absolute;
    bottom: 0px; /* 調整以使其與氣泡底部對齊 */
    left: -10px; /* 尾巴在氣泡外部 */
    width: 0;
    height: 0;
    border: 12px solid transparent; /* 增加尾巴大小 */
    border-right-color: #e0f7fa; /* 匹配氣泡顏色 */
    border-left: 0;
    border-bottom: 0; /* 調整邊框以形成朝左的尾巴 */
    margin-top: -6px; /* 垂直居中尾巴 */
}

.message-right::after {
    content: '';
    position: absolute;
    bottom: 0px; /* 調整以使其与气泡底部对齐 */
    right: -10px; /* 尾巴在气泡外部 */
    width: 0;
    height: 0;
    border: 12px solid transparent; /* 增加尾巴大小 */
    border-left-color: #c8e6c9; /* 匹配气泡颜色 */
    border-right: 0;
    border-bottom: 0; /* 调整边框以形成朝右的尾巴 */
    margin-top: -6px; /* 垂直居中尾巴 */
}

.message-username-display {
    position: absolute;
    bottom: 0px;      /* 與氣泡尾巴底部大致對齊 */
    font-size: 0.8em;   /* 使字型略小於訊息內文 */
    white-space: nowrap; /* 防止用戶名換行 */
    padding: 0 2px;     /* 微調內邊距 */
    line-height: 1;     /* 調整行高以更好地垂直對齊 */
}

.message-right .message-username-display {
    left: calc(100% + 12px); /* 定位到氣泡右側，尾巴寬度約10-12px，再加一點間隙 */
    color: #387002;
}

.message-left .message-username-display {
    right: calc(100% + 12px); /* 定位到氣泡左側，尾巴寬度約10-12px，再加一點間隙 */
    color: #00796b;
}


.chat-image {
    max-width: 80%; /* 相对于消息气泡宽度，减小以避免过宽 */
    max-height: 200px; /* 减小最大高度 */
    border-radius: 8px;
    margin-top: 5px;
    display: block; /* 确保它遵守边距 */
    cursor: pointer; /* 可选：以后用于灯箱效果 */
    object-fit: contain; /* 确保图片在调整大小时保持其纵横比并且不会被裁剪 */
}

/* 自身用戶發送的圖片靠右對齊 */
.message-right .chat-image {
    margin-left: auto;
    margin-right: 0;
}

/* 其他用戶發送的圖片（默認靠左） */
.message-left .chat-image {
    margin-right: auto;
    margin-left: 0;
}


.system-message {
    font-style: italic;
    color: #777;
    text-align: center;
    margin: 10px 0;
    font-size: 0.9em;
}


/* 深色模式 */
body.dark-mode {
    background: #121212;
    color: #e0e0e0;
}
body.dark-mode header {
    background: #1e1e1e;
    box-shadow: 0 2px 4px rgba(0,0,0,0.3);
}
body.dark-mode .controls button {
    background: #333;
    color: #e0e0e0;
    border-color: #444;
}
body.dark-mode .controls button:hover {
    background: #444;
}
body.dark-mode #chat {
    background: #1a1a1a;
    border-right-color: #2c2c2c;
}
body.dark-mode #userlist {
    background: #1e1e1e;
    border-left-color: #2c2c2c;
}
body.dark-mode #userlist h3, body.dark-mode #userlist div, body.dark-mode #userlist p {
    color: #ccc;
}
body.dark-mode footer {
    background: #1e1e1e;
    border-top-color: #2c2c2c;
}
body.dark-mode footer input[type="text"] {
    background-color: #2c2c2c;
    border-color: #444;
    color: #e0e0e0;
}
body.dark-mode footer button {
    background-color: #0056b3;
    border-color: #0056b3;
}
body.dark-mode footer button:hover:not(:disabled) {
    background-color: #004085;
}
body.dark-mode footer button:disabled {
    background-color: #444;
    border-color: #444;
}
body.dark-mode footer #image-button {
    background-color: #4a4a4a;
    border-color: #4a4a4a;
}
body.dark-mode footer #image-button:hover:not(:disabled) {
    background-color: #383838;
}

body.dark-mode .message-left {
    background: #2c3e50; /* 深色蓝灰色 */
    color: #ecf0f1;
    text-align: left;
    border-bottom-left-radius: 0px; /* 同樣，深色模式下也使左下角變直角 */
}
body.dark-mode .message-right {
    background: #27ae60; /* 深色绿色 */
    color: #ecf0f1;
    text-align: right;
    border-bottom-right-radius: 0px; /* 同樣，深色模式下也使右下角變直角 */
}

/* 深色模式下的语音气泡尾巴 */
body.dark-mode .message-left::after {
    border-right-color: #2c3e50;
}
body.dark-mode .message-right::after {
    border-left-color: #27ae60;
}


body.dark-mode .message-right .message-username-display {
    color: #a6e22e; /* 深色绿上的亮色 */
}
body.dark-mode .message-left .message-username-display {
    color: #bdc3c7; /* 深色模式下其他用戶的用戶名顏色 */
}

body.dark-mode .system-message {
    color: #aaa;
}

/* 移动端适配 */
@media (max-width: 768px) {
    main {
        flex-direction: column;
    }
    #chat {
        flex: 1;
        height: 60vh;
        border-right: none;
    }
    #userlist {
        flex: 1;
        height: 30vh;
        border-left: none;
        border-top: 1px solid #ccc;
    }
    body.dark-mode #userlist {
        border-top-color: #2c2c2c;
    }
    #userlist.hidden {
        display: none !important;
    }

    /* --- CORRECTED SECTION --- */
    .message-left {
        max-width: 85%;
        margin-left: 35px; /* 移動端調整左側用戶名空間 */
        margin-right: auto; /* 確保靠左 */
    }
    .message-right {
        max-width: 85%;
        margin-right: 35px; /* 移動端調整右側用戶名空間 */
        margin-left: auto; /* 確保靠右 */
    }
    /* --- END CORRECTED SECTION --- */

    .message-right .message-username-display {
      left: calc(100% + 8px);
    }
    .message-left .message-username-display {
      right: calc(100% + 8px);
    }
}

@media (max-width: 600px) {
    header h1 {
        font-size: 1.2em;
    }
    .controls button {
        padding: 5px 8px;
        font-size: 0.9em;
    }
    footer {
        flex-wrap: wrap;
        gap: 8px;
    }
    footer label#username-label {
        width: 100%;
        margin-right: 0;
        text-align: left;
        margin-bottom: -5px;
    }
    footer #username {
        width: calc(70% - 8px);
    }
    footer #join {
        width: calc(30% - 8px);
    }
    footer #message {
        width: calc(100% - 58px - 58px - 16px); /* Assuming image and send buttons are approx 58px wide */
        order: 1;
    }
      footer #image-button, footer #send {
        padding: 8px;
        min-width: 45px; /* Ensure buttons have a minimum width */
        order: 2;
    }

    /* --- CORRECTED SECTION --- */
    .message-left {
        max-width: 90%;
        margin-left: 30px; /* 更小移動端調整左側用戶名空間 */
        margin-right: auto; /* 確保靠左 */
    }
    .message-right {
        max-width: 90%;
        margin-right: 30px; /* 更小移動端調整右側用戶名空間 */
        margin-left: auto; /* 確保靠右 */
    }
    /* --- END CORRECTED SECTION --- */
}

/* 旧的销毁房间按钮样式（未在HTML中实现，但保留以供参考） */
.controls .destroy-room {
    margin-left: 10px;
    padding: 6px 12px;
    background: #dc3545;
    color: white;
    border: 1px solid #dc3545;
    border-radius: 4px;
    cursor: pointer;
}
.controls .destroy-room:hover {
    background: #c82333;
}
body.dark-mode .controls .destroy-room {
    background: #c82333;
    border-color: #c82333;
}
body.dark-mode .controls .destroy-room:hover {
    background: #a71d2a;
}
