* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Arial, sans-serif;
    background: #667eea;
    height: 100%;
    overflow: hidden;
    position: fixed;
    width: 100%;
    top: 0;
    left: 0;
}
.chat-container {
    width: 100%;
    max-width: 500px;
    height: 100%;
    height: -webkit-fill-available;
    display: flex;
    flex-direction: column;
    background: #f7fafc;
    margin: 0 auto;
    position: relative;
}
.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 5px 20px;
    padding-top: max(5px, env(safe-area-inset-top));
    flex-shrink: 0;
}
.header-left {
    flex-shrink: 0;
    line-height: 0;
}
.header-avatar {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    cursor: pointer;
    transition: transform 0.1s ease;
    background-color: #e2e8f0;
}

.header-avatar:active {
    transform: scale(0.95);
}

.header-avatar:active {
    transform: scale(0.95);
}
.header-center {
    text-align: center;
    flex: 1;
}
.header h1 {
    font-size: 20px;
    margin-bottom: 5px;
}
.header p {
    font-size: 12px;
    opacity: 0.9;
}
.online {
    font-size: 12px;
    margin-top: 5px;
    opacity: 0.9;
}
.settings-icon {
    background: none;
    border: none;
    color: white;
    font-size: 24px;
    cursor: pointer;
    padding: 5px;
    border-radius: 50%;
    transition: background 0.2s;
    flex-shrink: 0;
}
.settings-icon:active {
    background: rgba(255,255,255,0.2);
}
@media (max-width: 480px) {
    .header-avatar {
        width: 44px;
        height: 44px;
    }
}
.settings-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.5);
    z-index: 200;
    display: none;
    justify-content: center;
    align-items: center;
}
.settings-panel {
    background: white;
    width: 90%;
    max-width: 400px;
    border-radius: 20px;
    padding: 20px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.3);
}
.settings-panel h3 {
    margin-bottom: 15px;
    color: #2d3748;
}
.settings-panel input {
    width: 100%;
    padding: 12px;
    border: 2px solid #e2e8f0;
    border-radius: 25px;
    font-size: 16px;
    margin-bottom: 15px;
}
.settings-panel button {
    background: #667eea;
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 25px;
    font-size: 14px;
    cursor: pointer;
    margin-right: 10px;
}
.settings-panel button:active {
    transform: scale(0.98);
}
.status {
    background: #f56565;
    color: white;
    padding: 10px;
    text-align: center;
    font-size: 13px;
    font-weight: 500;
    flex-shrink: 0;
}
.status.connected {
    background: #48bb78;
}
.messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    background: #f7fafc;
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
}
.message {
    display: flex;
    margin-bottom: 12px;
    max-width: 85%;
    animation: messageAppear 0.3s ease-out forwards;
    opacity: 0;
    transform: translateY(20px);
}

.message.sent {
    margin-left: auto;
    flex-direction: row-reverse;
}
.message.received {
    margin-right: auto;
    flex-direction: row;
}
.message .avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
    margin: 0 8px;
}
.message.sent .avatar {
    margin-left: 8px;
    margin-right: 0;
}
.message.received .avatar {
    margin-right: 8px;
    margin-left: 0;
}
.message .message-content {
    background: #667eea;
    color: white;
    padding: 8px 14px;
    border-radius: 18px;
    word-wrap: break-word;
    max-width: 100%;
}
.message.received .message-content {
    background: white;
    color: #2d3748;
    box-shadow: 0 1px 2px rgba(0,0,0,0.1);
}
.message.sent .message-content {
    background: #667eea;
    color: white;
    border-bottom-right-radius: 4px;
}
.message.received .message-content {
    border-bottom-left-radius: 4px;
}
.message .sender {
    font-size: 11px;
    font-weight: 600;
    margin-bottom: 4px;
    display: block;
    opacity: 0.8;
}
.message.sent .sender {
    text-align: right;
}
.message .text {
    font-size: 14px;
    line-height: 1.4;
}
.message .time {
    font-size: 10px;
    opacity: 0.7;
    margin-top: 5px;
    display: block;
}
.message.sent .time {
    text-align: right;
}
.message.system {
    background: #e2e8f0;
    color: #4a5568;
    text-align: center;
    margin: 10px auto;
    font-size: 12px;
    padding: 6px 12px;
    border-radius: 20px;
    max-width: 90%;
    display: block;
}
@keyframes messageAppear {
    0% {
        opacity: 0;
        transform: translateY(20px);
    }
    100% {
        opacity: 1;
        transform: translateY(0);
    }
}
.input-section {
    background: white;
    border-top: 1px solid #e2e8f0;
    padding: 12px 16px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
    flex-shrink: 0;
    width: 100%;
}
.controls {
    display: flex;
    gap: 10px;
    margin-bottom: 10px;
}
.controls button {
    flex: 1;
    padding: 10px;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.1s;
    min-width: 0;
}
.controls button:active {
    transform: scale(0.98);
}
.btn-connect {
    background: #48bb78;
    color: white;
}
.btn-disconnect {
    background: #f56565;
    color: white;
}
.input-row {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
}
.input-row input {
    flex: 1 1 auto;
    min-width: 0;
    padding: 12px 16px;
    border: 2px solid #e2e8f0;
    border-radius: 25px;
    font-size: 16px;
    background: white;
    font-family: inherit;
    transition: border-color 0.2s;
}
.input-row input:focus {
    outline: none;
    border-color: #667eea;
}
.input-row input:disabled {
    background: #f7fafc;
    cursor: not-allowed;
}
.input-row button {
    flex-shrink: 0;
    padding: 12px 20px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border: none;
    border-radius: 25px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.1s;
    white-space: nowrap;
    min-width: 44px;
}
.input-row button:active {
    transform: scale(0.97);
}
.input-row button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}
@media (max-width: 480px) {
    .input-row {
        flex-wrap: wrap;
        gap: 8px;
    }
    .input-row input {
        flex: 1 1 100%;
        order: 1;
    }
    .input-row button {
        flex: 1 1 auto;
        text-align: center;
        order: 2;
    }
    .input-row button:first-of-type {
        margin-right: 4px;
    }
    .input-row button:last-of-type {
        margin-left: 4px;
    }
}
.scroll-btn {
    position: fixed;
    bottom: calc(100px + env(safe-area-inset-bottom));
    right: 20px;
    background: #667eea;
    color: white;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 2px 10px rgba(0,0,0,0.2);
    opacity: 0;
    transition: opacity 0.2s;
    pointer-events: none;
    z-index: 100;
    font-size: 20px;
    font-weight: bold;
}
.scroll-btn.show {
    opacity: 1;
    pointer-events: auto;
}
.scroll-btn:active {
    transform: scale(0.95);
}
@media (min-width: 768px) {
    body {
        background: #e2e8f0;
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative;
        overflow: auto;
    }
    .chat-container {
        max-width: 500px;
        height: 90vh;
        border-radius: 12px;
        overflow: hidden;
        box-shadow: 0 10px 40px rgba(0,0,0,0.15);
        position: relative;
    }
    .scroll-btn {
        right: calc(50% - 250px + 20px);
        bottom: 110px;
    }
    .controls button:hover {
        transform: translateY(-1px);
        filter: brightness(1.05);
    }
    .input-row button:hover {
        transform: translateY(-1px);
        filter: brightness(1.05);
    }
}
@media (max-width: 767px) {
    .message {
        max-width: 85%;
    }
    .input-row input {
        font-size: 16px;
    }
    .input-row button {
        padding: 12px 16px;
    }
    .scroll-btn {
        bottom: calc(100px + env(safe-area-inset-bottom));
        right: 20px;
    }
}
@media (max-width: 768px) {
    input, button {
        font-size: 16px !important;
    }
}

/* Toast notification */
.toast {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #333;
    color: white;
    padding: 16px 32px;
    border-radius: 12px;
    font-size: 18px;
    font-weight: 600;
    z-index: 1000;
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none;
    text-align: center;
    box-shadow: 0 4px 20px rgba(0,0,0,0.3);
    min-width: 200px;
    max-width: 80%;
    white-space: nowrap;
    letter-spacing: 0.5px;
    
    /* Prevent text selection */
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    
    /* Prevent text highlighting */
    -webkit-tap-highlight-color: transparent;
}

@media (max-width: 600px) {
    .toast {
        font-size: 16px;
        padding: 12px 24px;
        white-space: normal;
        word-break: break-word;
        max-width: 90%;
    }
}

.toast.show {
    opacity: 1;
}

/* Info toast (default) */
.toast-info {
    background: #4299e1;
    box-shadow: 0 4px 20px rgba(66, 153, 225, 0.3);
}

/* Success toast */
.toast-success {
    background: #48bb78;
    box-shadow: 0 4px 20px rgba(72, 187, 120, 0.3);
}

/* Warning toast */
.toast-warning {
    background: #ed8936;
    box-shadow: 0 4px 20px rgba(237, 137, 54, 0.3);
}

/* Error toast */
.toast-error {
    background: #f56565;
    box-shadow: 0 4px 20px rgba(245, 101, 101, 0.3);
}







/* Delete confirmation dialog */
.delete-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.5);
    z-index: 1050;
    display: none;
}

.delete-dialog {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: white;
    border-radius: 16px;
    padding: 20px;
    box-shadow: 0 10px 40px rgba(0,0,0,0.2);
    z-index: 1100;
    text-align: center;
    min-width: 280px;
    display: none;
    flex-direction: column;
    gap: 20px;
    
    /* Prevent text selection */
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

.delete-dialog p {
    font-size: 16px;
    color: #2d3748;
    margin: 0;
    
    /* Prevent text selection */
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

.delete-dialog-buttons {
    display: flex;
    gap: 12px;
    justify-content: center;
}

.delete-dialog-buttons button {
    padding: 8px 24px;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.1s;
    
    /* Prevent text selection on buttons */
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

.delete-dialog-buttons button:active {
    transform: scale(0.97);
}

.btn-delete-confirm {
    background: #f56565;
    color: white;
}

.btn-delete-cancel {
    background: #e2e8f0;
    color: #4a5568;
}

#loadingOverlay {
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}


/* Prevent text selection on messages */
.message,
.message .text,
.message .sender,
.message .time,
.message-content,
.voice-message,
.voice-message button,
.voice-message canvas {
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

/* Allow selection on input fields and buttons that need it */
.input-row input,
#messageInput,
.usernameInput,
.settings-panel input {
    user-select: text;
    -webkit-user-select: text;
    -moz-user-select: text;
    -ms-user-select: text;
}

/* For voice messages, keep the play button and canvas non-selectable */
.voice-message button,
.voice-message canvas {
    user-select: none;
    -webkit-user-select: none;
}

/* Keep images non-selectable */
.message .text img {
    user-select: none;
    -webkit-user-select: none;
    pointer-events: auto;
}


/* Prevent selection on avatars and header */
.avatar,
.header-avatar,
.header h1,
.header p,
.online {
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

/* Prevent selection on status and buttons */
.status,
.controls button,
.input-row button,
.scroll-btn {
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

.toast {
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

.combined-message-content {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
}

.message.sent .combined-message-content {
    align-items: flex-end;
}

.combined-message-content img {
    max-width: 100%;
    max-height: 200px;
    border-radius: 8px;
    cursor: pointer;
    user-select: none;
    -webkit-user-drag: none;
    user-drag: none;
}

.clear-data-btn {
    background: none;
    border: none;
    color: white;
    font-size: 20px;
    cursor: pointer;
    padding: 5px;
    border-radius: 50%;
    transition: background 0.2s;
    margin-left: 10px;
}
.clear-data-btn:active {
    background: rgba(255,255,255,0.2);
}
.clear-data-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.message,
.message * {
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

/* Avatar container with connection indicator */
.avatar-container {
    position: relative;
    display: inline-block;
}

.connection-indicator {
    position: absolute;
    bottom: 2px;
    right: 2px;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    border: 2px solid white;
    transition: background-color 0.3s ease;
}

.connection-indicator.online {
    background-color: #48bb78;
    box-shadow: 0 0 4px rgba(72, 187, 120);
}

.connection-indicator.offline {
    background-color: #f56565;
    box-shadow: 0 0 4px rgba(245, 101, 101);
}

.connection-indicator.connecting {
    background-color: #ed8936;
    animation: pulse 1s infinite;
}

@keyframes pulse {
    0% {
        opacity: 1;
        transform: scale(1);
    }
    50% {
        opacity: 0.7;
        transform: scale(1.1);
    }
    100% {
        opacity: 1;
        transform: scale(1);
    }
}

/* Online Users Modal */
.online-users-modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1100;
    justify-content: center;
    align-items: center;
}

.online-users-content {
    background: white;
    border-radius: 20px;
    width: 90%;
    max-width: 400px;
    max-height: 80vh;
    overflow: hidden;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
    animation: modalFadeIn 0.2s ease;
}

@keyframes modalFadeIn {
    from {
        opacity: 0;
        transform: scale(0.95);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

.online-users-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 20px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.online-users-header h3 {
    margin: 0;
    font-size: 18px;
}

.close-modal-btn {
    background: none;
    border: none;
    color: white;
    font-size: 24px;
    cursor: pointer;
    padding: 0;
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: background 0.2s;
}

.close-modal-btn:hover {
    background: rgba(255, 255, 255, 0.2);
}

.online-users-list {
    padding: 15px;
    max-height: 400px;
    overflow-y: auto;
}

.online-user-item {
    display: flex;
    align-items: center;
    padding: 10px;
    border-bottom: 1px solid #e2e8f0;
    transition: background 0.2s;
}

.online-user-item:hover {
    background: #f7fafc;
}

.online-user-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    object-fit: cover;
    margin-right: 12px;
}

.online-user-info {
    flex: 1;
}

.online-user-name {
    font-weight: 600;
    color: #2d3748;
    margin-bottom: 2px;
}

.online-user-status {
    font-size: 11px;
    color: #48bb78;
    display: flex;
    align-items: center;
    gap: 4px;
}

.online-user-status::before {
    content: "●";
    font-size: 10px;
}

.loading-users {
    text-align: center;
    padding: 30px;
    color: #a0aec0;
}

.no-users {
    text-align: center;
    padding: 30px;
    color: #a0aec0;
}

/* Sound Settings Row */
.sound-setting-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 0;
    border-bottom: 1px solid #e2e8f0;
    margin-bottom: 15px;
}

.sound-setting-info {
    display: flex;
    align-items: center;
    gap: 10px;
}

.sound-setting-icon {
    font-size: 20px;
}

.sound-setting-label {
    font-size: 14px;
    color: #2d3748;
    font-weight: 500;
}

/* Toggle Switch */
.toggle-switch {
    position: relative;
    display: inline-block;
    width: 50px;
    height: 24px;
}

.toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.toggle-slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #cbd5e0;
    transition: 0.3s;
    border-radius: 24px;
}

.toggle-slider:before {
    position: absolute;
    content: "";
    height: 18px;
    width: 18px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: 0.3s;
    border-radius: 50%;
}

.toggle-switch input:checked + .toggle-slider {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.toggle-switch input:checked + .toggle-slider:before {
    transform: translateX(26px);
}

/* Settings Buttons */
.settings-buttons {
    display: flex;
    gap: 10px;
    margin-top: 20px;
    justify-content: center;
}

.settings-btn {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 25px;
    font-size: 14px;
    cursor: pointer;
    transition: transform 0.1s, opacity 0.2s;
    flex: 1;
    max-width: 230px;
}

.settings-btn:active {
    transform: scale(0.98);
}

.settings-btn:hover {
    opacity: 0.9;
}

.save-btn {
    background: linear-gradient(135deg, #48bb78 0%, #38a169 100%);
}

.cancel-btn {
    background: linear-gradient(135deg, #a0aec0 0%, #718096 100%);
}

.header-buttons {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.home-btn {
    background: none;
    border: none;
    color: white;
    font-size: 22px;
    cursor: pointer;
    padding: 5px;
    border-radius: 50%;
    transition: background 0.2s;
    text-decoration: none;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
}

.home-btn:active {
    transform: scale(0.95);
    background: rgba(255,255,255,0.2);
}

.clear-data-btn {
    background: none;
    border: none;
    color: white;
    font-size: 20px;
    cursor: pointer;
    padding: 5px;
    border-radius: 50%;
    transition: background 0.2s;
    width: 36px;
    height: 36px;
}

.clear-data-btn:active {
    background: rgba(255,255,255,0.2);
}

.settings-icon {
    background: none;
    border: none;
    color: white;
    font-size: 22px;
    cursor: pointer;
    padding: 5px;
    border-radius: 50%;
    transition: background 0.2s;
    width: 36px;
    height: 36px;
}

.message-limit {
    font-size: 10px;
    opacity: 0.8;
    margin-top: 2px;
}
.message-limit.warning {
    color: #f56565;
    font-weight: bold;
}