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

:root {
    --primary: #007AFF;
    --primary-dark: #0051D5;
    --secondary: #5856D6;
    --bg: #000000;
    --surface: #1C1C1E;
    --surface-light: #2C2C2E;
    --text: #FFFFFF;
    --text-secondary: #8E8E93;
    --border: #38383A;
    --success: #34C759;
    --error: #FF3B30;
}

[data-theme="light"] {
    --primary: #007AFF;
    --primary-dark: #0051D5;
    --secondary: #5856D6;
    --bg: #FFFFFF;
    --surface: #F2F2F7;
    --surface-light: #E5E5EA;
    --text: #000000;
    --text-secondary: #6E6E73;
    --border: #C6C6C8;
    --success: #34C759;
    --error: #FF3B30;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    overflow: hidden;
    transition: background 0.3s ease, color 0.3s ease;
}

.theme-toggle {
    position: fixed;
    top: 1rem;
    right: 1rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 50%;
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 1000;
    transition: all 0.3s ease;
}

.theme-toggle:hover {
    background: var(--surface-light);
    transform: scale(1.05);
}

.theme-toggle .sun-icon {
    display: none;
}

.theme-toggle .moon-icon {
    display: block;
}

[data-theme="light"] .theme-toggle .sun-icon {
    display: block;
}

[data-theme="light"] .theme-toggle .moon-icon {
    display: none;
}

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

.screen {
    display: none;
    flex-direction: column;
    height: 100%;
}

.screen.active {
    display: flex;
}

.container {
    max-width: 500px;
    width: 100%;
    margin: 0 auto;
    padding: 2rem 1.5rem;
    display: flex;
    flex-direction: column;
    justify-content: center;
    min-height: 100vh;
}

h1 {
    font-size: 3rem;
    font-weight: 700;
    text-align: center;
    margin-bottom: 0.5rem;
    background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

h2 {
    font-size: 1.5rem;
    margin-bottom: 1.5rem;
    text-align: center;
}

.subtitle {
    text-align: center;
    color: var(--text-secondary);
    margin-bottom: 3rem;
    font-size: 1rem;
}

.btn {
    padding: 1rem 1.5rem;
    border: none;
    border-radius: 12px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    width: 100%;
    font-family: inherit;
}

.btn:active {
    transform: scale(0.98);
}

.btn-primary {
    background: var(--primary);
    color: white;
}

.btn-primary:hover {
    background: var(--primary-dark);
}

.btn-secondary {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
}

.btn-secondary:hover {
    background: var(--surface-light);
}

.btn-send {
    background: var(--primary);
    color: white;
    padding: 0.75rem 1rem;
    width: auto;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 48px;
    height: 48px;
}

.icon-btn {
    background: transparent;
    border: none;
    color: var(--text);
    cursor: pointer;
    padding: 0.5rem;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

.icon-btn:hover {
    background: var(--surface);
}

.divider {
    display: flex;
    align-items: center;
    margin: 2rem 0;
    color: var(--text-secondary);
    font-size: 0.875rem;
}

.divider::before,
.divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--border);
}

.divider span {
    padding: 0 1rem;
}

.join-section {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

input[type="text"] {
    padding: 1rem 1.5rem;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--surface);
    color: var(--text);
    font-size: 1rem;
    font-family: inherit;
    width: 100%;
}

input[type="text"]:focus {
    outline: none;
    border-color: var(--primary);
}

input[type="text"]::placeholder {
    color: var(--text-secondary);
}

.chat-header {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 1rem 1.5rem;
    flex-shrink: 0;
}

.header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.room-info {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

#room-code {
    font-family: 'Courier New', monospace;
    font-size: 1rem;
    font-weight: 600;
    color: var(--primary);
}

.messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.message {
    display: flex;
    flex-direction: column;
    animation: slideIn 0.2s ease-out;
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.25rem;
    font-size: 0.75rem;
}

.message-username {
    font-weight: 600;
    color: var(--text-secondary);
}

.message.own .message-username {
    color: var(--primary);
}

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

.message-content {
    padding: 0.75rem 1rem;
    border-radius: 16px;
    word-wrap: break-word;
    line-height: 1.4;
}

.message:not(.own) .message-content {
    background: var(--surface);
    border-bottom-left-radius: 4px;
}

.message.own .message-content {
    background: var(--primary);
    border-bottom-right-radius: 4px;
}

.system-message {
    align-self: center;
    color: var(--text-secondary);
    font-size: 0.875rem;
    padding: 0.5rem 1rem;
    background: var(--surface);
    border-radius: 12px;
    margin: 0.5rem 0;
    max-width: none;
}

.chat-input-container {
    padding: 1rem 1.5rem;
    background: var(--surface);
    border-top: 1px solid var(--border);
    display: flex;
    gap: 0.75rem;
    align-items: center;
    flex-shrink: 0;
}

#message-input {
    flex: 1;
    padding: 0.75rem 1rem;
    border: 1px solid var(--border);
    border-radius: 24px;
    background: var(--bg);
    color: var(--text);
    font-size: 1rem;
    font-family: inherit;
}

#message-input:focus {
    outline: none;
    border-color: var(--primary);
}

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

.messages::-webkit-scrollbar-track {
    background: var(--bg);
}

.messages::-webkit-scrollbar-thumb {
    background: var(--surface-light);
    border-radius: 3px;
}

.messages::-webkit-scrollbar-thumb:hover {
    background: var(--border);
}

@media (min-width: 768px) {
    .container {
        padding: 3rem 2rem;
    }

    h1 {
        font-size: 4rem;
    }

    .message-wrapper {
        max-width: 70%;
    }
}

@media (max-height: 600px) {
    .container {
        justify-content: flex-start;
        padding-top: 2rem;
    }

    h1 {
        font-size: 2.5rem;
    }

    .subtitle {
        margin-bottom: 2rem;
    }
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.user-count {
    position: absolute;
    top: -4px;
    right: -4px;
    background: var(--primary);
    color: white;
    font-size: 0.65rem;
    font-weight: 600;
    padding: 2px 5px;
    border-radius: 10px;
    min-width: 16px;
    text-align: center;
}

#toggle-users-btn {
    position: relative;
}

.users-panel {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.3s ease;
}

.users-panel.open {
    max-height: 300px;
    border-top: 1px solid var(--border);
    margin-top: 1rem;
}

.users-list {
    padding: 0.75rem 0;
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.user-badge {
    background: var(--surface-light);
    color: var(--text);
    padding: 0.5rem 0.75rem;
    border-radius: 16px;
    font-size: 0.875rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.user-badge::before {
    content: '';
    width: 8px;
    height: 8px;
    background: var(--success);
    border-radius: 50%;
}

.user-badge.inactive {
    opacity: 0.5;
}

.user-badge.inactive::before {
    background: var(--text-secondary);
}

.emoji-btn {
    padding: 0.5rem;
}

.emoji-picker {
    display: none;
    position: absolute;
    bottom: 80px;
    left: 1.5rem;
    right: 1.5rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 1rem;
    max-height: 250px;
    overflow-y: auto;
    z-index: 100;
}

.emoji-picker.open {
    display: block;
}

.emoji-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(40px, 1fr));
    gap: 0.5rem;
}

.emoji-item {
    font-size: 1.5rem;
    padding: 0.5rem;
    cursor: pointer;
    border-radius: 8px;
    text-align: center;
    transition: background 0.2s;
}

.emoji-item:hover {
    background: var(--surface-light);
}

.message-wrapper {
    display: flex;
    flex-direction: column;
    max-width: 85%;
}

.message-wrapper.own {
    align-self: flex-end;
}

.message-reactions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem;
    margin-top: 0.25rem;
}

.reaction {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 0.25rem 0.5rem;
    font-size: 0.875rem;
    display: flex;
    align-items: center;
    gap: 0.25rem;
    cursor: pointer;
    transition: all 0.2s;
}

.reaction:hover {
    background: var(--surface-light);
}

.reaction.active {
    background: var(--primary);
    border-color: var(--primary);
}

.reaction-emoji {
    font-size: 1rem;
}

.reaction-count {
    font-size: 0.75rem;
    font-weight: 600;
}

.add-reaction-btn {
    background: transparent;
    border: 1px dashed var(--border);
    border-radius: 12px;
    padding: 0.25rem 0.5rem;
    font-size: 1rem;
    cursor: pointer;
    opacity: 0;
    transition: all 0.2s;
}

.message-wrapper:hover .add-reaction-btn {
    opacity: 1;
}

.add-reaction-btn:hover {
    background: var(--surface);
    border-style: solid;
}

.reaction-picker {
    position: absolute;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 0.5rem;
    display: none;
    flex-wrap: wrap;
    gap: 0.25rem;
    z-index: 10;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.reaction-picker.open {
    display: flex;
}

.reaction-picker-emoji {
    font-size: 1.5rem;
    padding: 0.25rem;
    cursor: pointer;
    border-radius: 6px;
    transition: background 0.2s;
}

.reaction-picker-emoji:hover {
    background: var(--surface-light);
}

@media (min-width: 768px) {
    .emoji-picker {
        max-width: 350px;
    }
}
