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

:root {
    --safe-area-inset-top: env(safe-area-inset-top, 0px);
    --safe-area-inset-bottom: env(safe-area-inset-bottom, 0px);
    --safe-area-inset-left: env(safe-area-inset-left, 0px);
    --safe-area-inset-right: env(safe-area-inset-right, 0px);
}

[data-theme="light"] {
    --bg: #f0f2f5;
    --msg-mine-bg: #e3f2fd;
    --msg-mine-text: #1a1a1a;
    --msg-mine-border: #667eea;
    --msg-other-bg: #ffffff;
    --msg-other-text: #1a1a1a;
    --msg-other-border: #e0e0e0;
    --input-bg: #fff;
    --input-border: #667eea;
    --header-1: #667eea;
    --header-2: #4c5fd5;
    --btn: #667eea;
    --sidebar-bg: #2c2c3a;
    --hover-bg: rgba(102,126,234,0.1);
    --upload-bar-bg: #e8f0fe;
    --upload-bar-border: #c6dafc;
}

[data-theme="dark"] {
    --bg: #1a1a2e;
    --msg-mine-bg: #2a2a4a;
    --msg-mine-text: #e0e0e0;
    --msg-mine-border: #7c5ce7;
    --msg-other-bg: #16213e;
    --msg-other-text: #e0e0e0;
    --msg-other-border: #3a3a5a;
    --input-bg: #2a2a4a;
    --input-border: #7c5ce7;
    --header-1: #2a1a4e;
    --header-2: #1a0a3e;
    --btn: #7c5ce7;
    --sidebar-bg: #0f0f1a;
    --hover-bg: rgba(124,92,231,0.2);
    --upload-bar-bg: #1a1a3e;
    --upload-bar-border: #3a3a6a;
}

[data-theme="green"] {
    --bg: #e8f5e9;
    --msg-mine-bg: #c8e6c9;
    --msg-mine-text: #1a1a1a;
    --msg-mine-border: #2d8a4e;
    --msg-other-bg: #ffffff;
    --msg-other-text: #1a1a1a;
    --msg-other-border: #c0d0c0;
    --input-bg: #fff;
    --input-border: #2d8a4e;
    --header-1: #2d8a4e;
    --header-2: #1a6a3e;
    --btn: #2d8a4e;
    --sidebar-bg: #1a332a;
    --hover-bg: rgba(45,138,78,0.1);
    --upload-bar-bg: #d4edda;
    --upload-bar-border: #c3e6cb;
}

[data-theme="orange"] {
    --bg: #fff3e0;
    --msg-mine-bg: #ffe0b2;
    --msg-mine-text: #1a1a1a;
    --msg-mine-border: #e67e22;
    --msg-other-bg: #ffffff;
    --msg-other-text: #1a1a1a;
    --msg-other-border: #d0c0a0;
    --input-bg: #fff;
    --input-border: #e67e22;
    --header-1: #e67e22;
    --header-2: #d35400;
    --btn: #e67e22;
    --sidebar-bg: #2a1a0a;
    --hover-bg: rgba(230,126,34,0.1);
    --upload-bar-bg: #fff3e0;
    --upload-bar-border: #ffe0b2;
}

[data-font-size="small"] { --font-size-base: 13px; --font-size-message: 14px; --font-size-sender: 11px; --font-size-time: 10px; }
[data-font-size="medium"] { --font-size-base: 16px; --font-size-message: 16px; --font-size-sender: 13px; --font-size-time: 12px; }
[data-font-size="large"] { --font-size-base: 18px; --font-size-message: 18px; --font-size-sender: 14px; --font-size-time: 13px; }
[data-font-size="xlarge"] { --font-size-base: 21px; --font-size-message: 21px; --font-size-sender: 16px; --font-size-time: 14px; }

[data-wallpaper="gradient"] .chat-area { background: linear-gradient(180deg, rgba(102,126,234,0.08) 0%, rgba(118,75,162,0.08) 100%); }
[data-wallpaper="custom"] .chat-area { background-size: cover; background-position: center; background-repeat: no-repeat; }

body, input, button, .message, .sidebar, .room-item { font-size: var(--font-size-base); font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Arial, sans-serif; }

html, body { height: 100%; width: 100%; overflow: hidden; position: fixed; top: 0; left: 0; background: var(--bg); }

#login-screen { display: flex; justify-content: center; align-items: center; height: 100%; width: 100%; background: linear-gradient(135deg, #667eea, #764ba2); position: fixed; top: 0; left: 0; z-index: 100; }
.login-box { background: #fff; padding: 35px; border-radius: 14px; box-shadow: 0 18px 55px rgba(0,0,0,0.3); width: 400px; max-width: 90%; text-align: center; }
.login-box h1 { margin-bottom: 22px; font-size: 24px; color: #1a1a1a; }
.login-box input { width: 100%; padding: 14px; margin: 8px 0; border: 2px solid #e0e0e0; border-radius: 9px; font-size: 16px; display: block; }
.login-box button { width: 100%; padding: 14px; background: #667eea; color: #fff; border: none; border-radius: 9px; font-size: 16px; font-weight: 600; cursor: pointer; margin-top: 16px; -webkit-tap-highlight-color: transparent; }
.login-box button:active { opacity: 0.8; }

#chat-screen { display: none; height: 100%; width: 100%; position: fixed; top: 0; left: 0; z-index: 100; }

.sidebar-overlay { display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); z-index: 200; -webkit-tap-highlight-color: transparent; }
.sidebar-overlay.open { display: block; }

.sidebar { position: fixed; top: 0; left: -310px; width: 290px; height: 100%; background: var(--sidebar-bg); color: #fff; z-index: 300; display: flex; flex-direction: column; transition: left 0.3s; padding-top: var(--safe-area-inset-top); }
.sidebar.open { left: 0; }
.sidebar-header { padding: 16px; border-bottom: 1px solid rgba(255,255,255,0.1); display: flex; justify-content: space-between; align-items: center; }
.sidebar-header h3 { font-size: 18px; }
.sidebar-header button { background: rgba(255,255,255,0.2); color: #fff; border: none; width: 34px; height: 34px; border-radius: 50%; font-size: 20px; cursor: pointer; -webkit-tap-highlight-color: transparent; }
.rooms-list { flex: 1; overflow-y: auto; padding: 10px; -webkit-overflow-scrolling: touch; }
.room-item { padding: 14px 16px; border-radius: 9px; cursor: pointer; margin-bottom: 5px; display: flex; align-items: center; gap: 10px; -webkit-tap-highlight-color: transparent; }
.room-item:active { opacity: 0.7; }
.room-item:hover { background: rgba(255,255,255,0.1); }
.room-item.active { background: #667eea; }
.room-item .room-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-footer { padding: 16px; padding-bottom: calc(16px + var(--safe-area-inset-bottom)); border-top: 1px solid rgba(255,255,255,0.1); display: flex; align-items: center; gap: 10px; }
.sidebar-footer .user-info { flex: 1; font-size: 14px; opacity: 0.8; overflow: hidden; text-overflow: ellipsis; }
.sidebar-footer button { background: rgba(255,255,255,0.2); color: #fff; border: none; padding: 8px 16px; border-radius: 7px; cursor: pointer; -webkit-tap-highlight-color: transparent; }

.chat-area { width: 100%; height: 100%; display: flex; flex-direction: column; background: var(--bg); }
.chat-header { background: linear-gradient(135deg, var(--header-1), var(--header-2)); color: #fff; padding: 12px 16px; padding-top: calc(12px + var(--safe-area-inset-top)); display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.chat-header h2 { font-size: 17px; flex: 1; overflow: hidden; text-overflow: ellipsis; }
.menu-toggle, .settings-btn, .leave-btn { background: none; border: none; color: #fff; cursor: pointer; width: 34px; height: 34px; border-radius: 50%; font-size: 20px; -webkit-tap-highlight-color: transparent; }
.menu-toggle:active, .settings-btn:active, .leave-btn:active { background: rgba(255,255,255,0.2); }
.settings-btn { background: rgba(255,255,255,0.2); }
.leave-btn { background: rgba(255,255,255,0.2); font-size: 18px; }

.messages { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 8px; -webkit-overflow-scrolling: touch; }
.message { max-width: 85%; padding: 10px 14px; border-radius: 15px; word-wrap: break-word; animation: fadeIn 0.2s ease-out; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }
.message.mine { background: var(--msg-mine-bg); color: var(--msg-mine-text); align-self: flex-end; border: 2px solid var(--msg-mine-border); border-bottom-right-radius: 5px; }
.message.other { background: var(--msg-other-bg); color: var(--msg-other-text); align-self: flex-start; border: 2px solid var(--msg-other-border); border-bottom-left-radius: 5px; }
.message .sender { font-size: var(--font-size-sender); font-weight: bold; margin-bottom: 3px; display: block; opacity: 0.8; }
.message .text { font-size: var(--font-size-message); line-height: 1.4; }
.message .time { font-size: var(--font-size-time); opacity: 0.6; margin-top: 5px; display: block; text-align: right; }
.message .attachment { margin-top: 7px; }
.message .attachment img { max-width: 100%; max-height: 220px; border-radius: 9px; cursor: pointer; display: block; transition: transform 0.2s; }
.message .attachment img:hover { transform: scale(1.02); }
.message .attachment .file-link { display: flex; align-items: center; gap: 9px; padding: 9px 12px; background: var(--hover-bg); border-radius: 9px; text-decoration: none; color: inherit; cursor: pointer; -webkit-tap-highlight-color: transparent; word-break: break-all; transition: background 0.2s; }
.message .attachment .file-link:hover { background: rgba(102,126,234,0.2); }
.message .attachment .file-link:active { opacity: 0.7; }
.file-icon { font-size: 24px; flex-shrink: 0; }
.file-info { flex: 1; min-width: 0; }
.file-name-display { font-size: var(--font-size-message); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.input-area { display: flex; padding: 10px 12px; padding-bottom: calc(10px + var(--safe-area-inset-bottom)); background: var(--input-bg); border-top: 1px solid var(--input-border); gap: 10px; align-items: stretch; flex-shrink: 0; }
.input-area input[type="text"] { flex: 1; padding: 12px 16px; border: 2px solid var(--input-border); border-radius: 25px; font-size: var(--font-size-base); background: var(--input-bg); color: var(--msg-other-text); min-width: 0; }

#send-btn {
    padding: 12px 24px;
    background: #ffffff;
    color: var(--btn);
    border: 2px solid var(--btn);
    border-radius: 25px;
    font-weight: bold;
    font-size: 20px;
    cursor: pointer;
    flex-shrink: 0;
    -webkit-tap-highlight-color: transparent;
    white-space: nowrap;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.15s, transform 0.15s;
    line-height: 1;
}
#send-btn:active {
    opacity: 0.8;
    transform: scale(0.95);
}

.attach-btn {
    width: auto;
    aspect-ratio: 1 / 1;
    border-radius: 50%;
    background: var(--hover-bg);
    border: 2px solid var(--btn);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 20px;
    flex-shrink: 0;
    -webkit-tap-highlight-color: transparent;
    padding: 0 12px;
    color: var(--btn);
}
.attach-btn:active { opacity: 0.7; }

.upload-bar { display: none; padding: 10px 14px; background: var(--upload-bar-bg); border-top: 1px solid var(--upload-bar-border); align-items: center; gap: 10px; flex-shrink: 0; }
.upload-bar.visible { display: flex; }
.upload-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.progress { width: 80px; height: 5px; background: var(--upload-bar-border); border-radius: 3px; overflow: hidden; flex-shrink: 0; }
.progress-fill { height: 100%; background: var(--btn); width: 0%; transition: width 0.3s; }
.cancel-upload { cursor: pointer; font-size: 18px; color: #999; padding: 4px; -webkit-tap-highlight-color: transparent; }

.drop-hint { display: none; padding: 12px; text-align: center; background: rgba(102,126,234,0.1); border: 2px dashed #667eea; border-radius: 9px; margin: 8px; flex-shrink: 0; }
.drop-hint.active { display: block; }
.loading-text { text-align: center; color: #999; padding: 45px; }
.no-chat-selected { flex: 1; display: flex; align-items: center; justify-content: center; color: #999; text-align: center; }

.modal-overlay { display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); z-index: 500; justify-content: center; align-items: center; padding: 20px; }
.modal-overlay.show { display: flex; }
.modal-content { background: #fff; padding: 22px; border-radius: 14px; width: 100%; max-width: 450px; max-height: 80vh; overflow-y: auto; color: #1a1a1a; }
.modal-content h3 { margin-bottom: 16px; }
.modal-content p { margin-bottom: 16px; line-height: 1.5; }
.modal-content input[type="text"] { width: 100%; padding: 12px; border: 2px solid #e0e0e0; border-radius: 9px; font-size: 16px; margin-bottom: 12px; }
.settings-section { margin-bottom: 16px; }
.settings-section h4 { font-size: 12px; color: #999; text-transform: uppercase; margin-bottom: 9px; }
.settings-grid, .settings-grid-4 { display: grid; gap: 9px; }
.settings-grid { grid-template-columns: repeat(3, 1fr); }
.settings-grid-4 { grid-template-columns: repeat(4, 1fr); }
.theme-btn, .wallpaper-btn, .fontsize-btn { padding: 12px 8px; border: 2px solid #e0e0e0; border-radius: 9px; cursor: pointer; text-align: center; font-size: 12px; -webkit-tap-highlight-color: transparent; }
.theme-btn.active, .wallpaper-btn.active, .fontsize-btn.active { border-color: #667eea; background: #667eea; color: #fff; }
.theme-preview, .wallpaper-preview { width: 100%; height: 40px; border-radius: 5px; margin-bottom: 7px; background-size: cover; }
.modal-actions { display: flex; gap: 9px; justify-content: flex-end; margin-top: 16px; }
.btn-secondary, .btn-primary { padding: 10px 20px; border-radius: 9px; cursor: pointer; border: none; font-weight: 600; -webkit-tap-highlight-color: transparent; }
.btn-secondary { background: #f0f2f5; color: #1a1a1a; }
.btn-primary { background: #667eea; color: #fff; }
.btn-danger { background: #e74c3c; color: #fff; }

#file-input, #wallpaper-file-input { display: none; }

.toast { position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%); background: rgba(0,0,0,0.8); color: white; padding: 8px 16px; border-radius: 20px; font-size: 14px; z-index: 1000; pointer-events: none; opacity: 0; transition: opacity 0.3s; white-space: nowrap; }
.toast.show { opacity: 1; }

@media (max-width: 480px) {
    .message { max-width: 90%; }
    .input-area { padding: 8px 10px; gap: 8px; }
    .input-area input[type="text"] { padding: 10px 14px; }
    #send-btn { padding: 10px 20px; }
    .chat-header { padding: 10px 12px; }
    .chat-header h2 { font-size: 16px; }
    .settings-grid { grid-template-columns: repeat(2, 1fr); }
    .settings-grid-4 { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 360px) {
    .message { max-width: 95%; padding: 8px 10px; }
    .input-area { padding: 6px 8px; gap: 6px; }
    #send-btn { padding: 10px 16px; }
}