:root {
    --bg-color: #f8f9fa;
    --card-bg: #ffffff;
    --text-color: #2d3436;
    --accent-color: #6c5ce7;
    --accent-light: #a29bfe;
    --border-color: #e9ecef;
    --secondary-text: #636e72;
    --user-a-color: #0984e3;
    --user-b-color: #00b894;
    --shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

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

body {
    margin: 0;
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg-color);
    color: var(--text-color);
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

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

/* Utilities */
.hidden {
    display: none !important;
}

.error {
    color: #d63031;
    font-size: 0.8rem;
    margin-top: 8px;
}

/* Auth Overlay */
.overlay {
    position: fixed;
    inset: 0;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.auth-box {
    text-align: center;
    width: 85%;
    max-width: 320px;
    background: var(--card-bg);
    padding: 32px 24px;
    border-radius: 20px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}

.auth-box h2 {
    font-size: 1.3rem;
    font-weight: 600;
    margin: 0 0 24px 0;
    color: var(--text-color);
}

.user-select {
    display: flex;
    gap: 12px;
    margin-bottom: 20px;
}

.user-btn {
    flex: 1;
    padding: 12px;
    border: 2px solid var(--border-color);
    background: var(--card-bg);
    color: var(--secondary-text);
    border-radius: 12px;
    font-size: 0.9rem;
    font-weight: 500;
    transition: all 0.2s;
}

.user-btn.active {
    border-color: var(--accent-color);
    background: var(--accent-color);
    color: white;
}

.user-btn[data-user="A"].active {
    background: var(--user-a-color);
    border-color: var(--user-a-color);
}

.user-btn[data-user="B"].active {
    background: var(--user-b-color);
    border-color: var(--user-b-color);
}

input[type="password"],
input[type="text"] {
    width: 100%;
    padding: 14px 16px;
    border: 2px solid var(--border-color);
    border-radius: 12px;
    margin-bottom: 12px;
    font-size: 1rem;
    outline: none;
    transition: border-color 0.2s;
}

input:focus {
    border-color: var(--accent-color);
}

button {
    padding: 14px 24px;
    background: var(--accent-color);
    color: #fff;
    border: none;
    border-radius: 12px;
    cursor: pointer;
    font-size: 1rem;
    font-weight: 500;
    width: 100%;
    transition: transform 0.1s, opacity 0.2s;
}

button:active {
    transform: scale(0.98);
}

button.secondary {
    background: var(--border-color);
    color: var(--text-color);
    margin-top: 10px;
}

button.text-btn {
    background: none;
    color: var(--secondary-text);
    font-size: 0.85rem;
    padding: 8px;
    width: auto;
}

/* App Header */
header {
    padding: 16px 20px;
    background: var(--card-bg);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 12px;
    box-shadow: var(--shadow);
    position: relative;
    z-index: 10;
}

header h1 {
    margin: 0;
    font-size: 1.1rem;
    font-weight: 600;
    flex: 1;
}

.user-badge {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
    font-weight: 600;
    color: white;
    background: var(--user-a-color);
}

.user-badge.user-b {
    background: var(--user-b-color);
}

/* Message List */
main {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    -webkit-overflow-scrolling: touch;
    /* Push content to bottom when few messages */
    justify-content: flex-end;
    min-height: 0;
}

/* Message List Styling */
.note-item {
    background: var(--card-bg);
    padding: 10px 14px;
    border-radius: 12px;
    box-shadow: var(--shadow);
    position: relative;
    max-width: 80%;
    align-self: flex-start;
    /* Default left */
    border: none !important;
}

.note-item.is-me {
    align-self: flex-end;
    /* Self right */
    background: #eef2f5;
    /* Slight distinction but not WeChat green */
}

/* Sender Name & Time */
.note-header {
    display: flex;
    /* Restore visibility */
}

.note-time {
    display: block;
    /* Restore visibility */
}

/* Restore Default Colors */
.note-item.is-me .note-content {
    color: var(--text-color);
}

/* Reply Context - Default */
.note-item.is-me .reply-context {
    background: rgba(108, 92, 231, 0.1);
    border-left-color: var(--accent-color);
}

.note-header {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
}

.note-sender {
    font-size: 0.75rem;
    font-weight: 600;
    padding: 2px 8px;
    border-radius: 10px;
    color: white;
}

.note-sender.user-a {
    background: var(--user-a-color);
}

.note-sender.user-b {
    background: var(--user-b-color);
}

.note-time {
    font-size: 0.7rem;
    color: var(--secondary-text);
}

.note-content {
    font-size: 0.95rem;
    line-height: 1.5;
    white-space: pre-wrap;
    word-break: break-word;
}

.note-image {
    max-width: 100%;
    max-height: 300px;
    border-radius: 12px;
    margin-top: 8px;
    cursor: pointer;
    object-fit: cover;
}

.audio-player {
    width: 100%;
    height: 40px;
    margin-top: 8px;
    border-radius: 20px;
}

/* Reply Context */
.reply-context {
    background: rgba(108, 92, 231, 0.1);
    border-left: 3px solid var(--accent-color);
    padding: 8px 12px;
    margin-bottom: 10px;
    border-radius: 0 8px 8px 0;
    font-size: 0.85rem;
    color: var(--secondary-text);
}

.reply-context.reply-image {
    display: flex;
    align-items: center;
    gap: 10px;
}

.reply-thumb {
    width: 40px;
    height: 40px;
    border-radius: 6px;
    object-fit: cover;
}

/* Footer Input */
footer {
    flex-shrink: 0;
    padding: 12px 16px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
    background: var(--card-bg);
    border-top: 1px solid var(--border-color);
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
    position: relative;
}

.input-area {
    display: flex;
    gap: 10px;
    align-items: center;
}

.media-buttons {
    display: flex;
    gap: 4px;
}

.icon-btn {
    width: 40px;
    height: 40px;
    padding: 0;
    background: var(--bg-color);
    border-radius: 50%;
    font-size: 1.1rem;
    display: flex;
    align-items: center;
    justify-content: center;
}

#message-input {
    flex: 1;
    margin-bottom: 0;
    border: none;
    background: var(--bg-color);
    border-radius: 20px;
    padding: 12px 18px;
}

#send-btn {
    width: auto;
    padding: 12px 20px;
    border-radius: 20px;
    min-width: 70px;
}

#reply-preview {
    font-size: 0.8rem;
    color: var(--secondary-text);
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 12px;
    margin-bottom: 10px;
    background: var(--bg-color);
    border-radius: 10px;
}

#reply-preview button {
    width: 24px;
    height: 24px;
    padding: 0;
    background: var(--secondary-text);
    border-radius: 50%;
    font-size: 0.9rem;
}

/* Emoji Panel */
#emoji-panel {
    position: absolute;
    bottom: 100%;
    left: 16px;
    right: 16px;
    background: var(--card-bg);
    border-radius: 16px;
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.15);
    padding: 16px;
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    gap: 8px;
    max-height: 200px;
    overflow-y: auto;
}

#emoji-panel span {
    font-size: 1.5rem;
    text-align: center;
    cursor: pointer;
    padding: 6px;
    border-radius: 8px;
    transition: background 0.15s;
}

#emoji-panel span:hover {
    background: var(--bg-color);
}

/* Recording Indicator */
#recording-indicator {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: 12px;
    margin-top: 10px;
    background: #ff7675;
    color: white;
    border-radius: 12px;
    font-size: 0.9rem;
}

#recording-indicator .pulse {
    width: 12px;
    height: 12px;
    background: white;
    border-radius: 50%;
    animation: pulse 1s infinite;
}

@keyframes pulse {

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

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

#stop-record-btn {
    width: auto;
    padding: 8px 16px;
    background: white;
    color: #ff7675;
    font-size: 0.85rem;
}

/* Mobile optimizations */
@media (max-width: 480px) {
    .auth-box {
        width: 90%;
        padding: 24px 20px;
    }

    .icon-btn {
        width: 40px;
        height: 40px;
        font-size: 1.2rem;
    }

    #send-btn {
        padding: 12px 16px;
        min-width: 60px;
    }

    #emoji-panel {
        position: fixed;
        bottom: 80px;
        left: 8px;
        right: 8px;
        grid-template-columns: repeat(6, 1fr);
        max-height: 220px;
        z-index: 100;
    }

    #emoji-panel span {
        font-size: 1.8rem;
        padding: 8px;
    }

    .input-area {
        gap: 6px;
    }

    .media-buttons {
        gap: 2px;
    }

    #message-input {
        padding: 10px 14px;
        font-size: 16px;
        /* Prevent iOS zoom */
    }

    .note-item {
        padding: 12px;
    }

    .note-content {
        font-size: 1rem;
    }

    footer {
        position: relative;
    }
}