* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
}

:root {
    --bg-primary: #1a1a2e;
    --bg-secondary: #16213e;
    --bg-tertiary: #0f3460;
    --accent: #e94560;
    --text-primary: #eee;
    --text-secondary: #888;
    --border: #2a2a4e;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
}

html, body {
    overflow: hidden;
    height: 100%;
    width: 100%;
    touch-action: manipulation;
    position: fixed;
    overscroll-behavior: none;
}

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

/* Login */
.login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    padding: 1rem;
}

.login-box {
    text-align: center;
    padding: 2rem;
    background: var(--bg-secondary);
    border-radius: 16px;
    border: 1px solid var(--border);
    max-width: 400px;
    width: 100%;
}

.logo {
    font-size: 3rem;
    margin-bottom: 0.5rem;
}

.login-box h1 {
    margin-bottom: 0.5rem;
    font-size: 1.5rem;
}

.tagline {
    color: var(--text-secondary);
    margin-bottom: 1.5rem;
}

.google-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    background: #fff;
    color: #333;
    padding: 12px 24px;
    border-radius: 8px;
    text-decoration: none;
    font-weight: 500;
    transition: transform 0.2s;
    width: 100%;
}

.google-btn:hover {
    transform: scale(1.02);
}

/* App Layout */
.app {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

.sidebar {
    width: 240px;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

.sidebar-header {
    padding: 1rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    border-bottom: 1px solid var(--border);
}

.logo {
    font-size: 1.5rem;
}

.brand {
    font-weight: 600;
    font-size: 1.1rem;
}

.channels, .users {
    padding: 0.75rem;
    flex: 1;
    overflow-y: auto;
}

.channels h3, .users h3 {
    font-size: 0.7rem;
    text-transform: uppercase;
    color: var(--text-secondary);
    margin-bottom: 0.5rem;
    padding: 0 0.5rem;
}

#channel-list, #user-list {
    list-style: none;
}

#channel-list li, #user-list li {
    padding: 0.6rem 0.75rem;
    border-radius: 6px;
    cursor: pointer;
    margin-bottom: 2px;
    font-size: 0.9rem;
}

#channel-list li:hover, #user-list li:hover {
    background: var(--bg-tertiary);
}

#channel-list li.active {
    background: var(--accent);
}

.user-info {
    padding: 0.75rem;
    border-top: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

#user-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 0.9rem;
    flex-shrink: 0;
}

#user-name {
    flex: 1;
    font-size: 0.9rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.logout {
    color: var(--text-secondary);
    text-decoration: none;
    font-size: 0.8rem;
    flex-shrink: 0;
}

.logout:hover {
    color: var(--accent);
}

/* Chat Area */
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    overflow: hidden;
}

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

.chat-header h2 {
    font-size: 1rem;
    font-weight: 600;
}

.messages {
    flex: 1;
    padding: 0.75rem;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.message {
    margin-bottom: 0.75rem;
    line-height: 1.4;
    word-break: break-word;
}

.msg-user {
    font-weight: 600;
}

.msg-time {
    color: var(--text-secondary);
    font-size: 0.7rem;
    margin-left: 0.5rem;
}

.msg-text {
    display: block;
    margin-top: 0.2rem;
}

.input-area {
    padding: 0.75rem;
    border-top: 1px solid var(--border);
    display: flex;
    gap: 0.5rem;
    flex-shrink: 0;
}

#message-input {
    flex: 1;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.75rem;
    color: var(--text-primary);
    font-size: 1rem;
    min-width: 0;
}

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

#send-btn {
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 8px;
    padding: 0 1.25rem;
    font-weight: 600;
    cursor: pointer;
    flex-shrink: 0;
    font-size: 0.9rem;
}

#send-btn:hover {
    background: #ff6b6b;
}

/* Username Modal */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.85);
    z-index: 1000;
    justify-content: center;
    align-items: center;
    padding: 1rem;
}

.modal-content {
    background: var(--bg-secondary);
    padding: 1.5rem;
    border-radius: 16px;
    text-align: center;
    max-width: 350px;
    width: 100%;
}

.modal-content .logo {
    font-size: 2.5rem;
}

.modal-content h2 {
    margin: 0.5rem 0;
    font-size: 1.25rem;
}

.modal-content p {
    color: var(--text-secondary);
    margin-bottom: 0.75rem;
    font-size: 0.9rem;
}

#username-input {
    width: 100%;
    padding: 0.75rem;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text-primary);
    font-size: 1rem;
    margin-bottom: 0.75rem;
}

#username-input:focus {
    outline: none;
    border-color: var(--accent);
}

#username-btn {
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 8px;
    padding: 0.75rem 1.5rem;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    width: 100%;
}

#username-btn:hover {
    background: #ff6b6b;
}

.error {
    color: #ff6b6b;
    font-size: 0.8rem;
    margin-top: 0.5rem;
}

/* Admin Panel */
.admin-panel {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 1rem;
    z-index: 2000;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
}

.admin-panel-content h4 {
    margin-bottom: 0.75rem;
    font-size: 0.9rem;
}

.admin-panel-content button {
    display: block;
    width: 100%;
    margin-bottom: 0.5rem;
    padding: 0.5rem;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text-primary);
    cursor: pointer;
    font-size: 0.85rem;
}

.admin-panel-content button:hover {
    background: var(--accent);
}

.admin-panel-content button.danger {
    border-color: #ff4444;
}

.admin-panel-content button.danger:hover {
    background: #ff4444;
}

.admin-btn {
    cursor: pointer;
    font-size: 0.7rem;
    margin-left: 0.25rem;
    opacity: 0.6;
}

.admin-btn:hover {
    opacity: 1;
}

/* Mobile Menu Toggle */
.menu-toggle {
    display: none;
    position: fixed;
    top: 0.5rem;
    left: 0.5rem;
    z-index: 1001;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.5rem 0.75rem;
    color: var(--text-primary);
    cursor: pointer;
    font-size: 1.25rem;
}

.sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.7);
    z-index: 998;
}

/* Mobile */
@media (max-width: 768px) {
    .menu-toggle {
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    .app {
        flex-direction: column;
        padding-top: var(--safe-top);
    }
    
    .sidebar-overlay.active {
        display: block;
    }
    
    .sidebar {
        position: fixed;
        top: 0;
        left: -280px;
        width: 260px;
        height: 100%;
        max-height: 100vh;
        z-index: 999;
        transition: left 0.3s ease;
        border-right: 1px solid var(--border);
        border-bottom: none;
        padding-top: var(--safe-top);
        padding-bottom: var(--safe-bottom);
    }
    
    .sidebar.open {
        left: 0;
    }
    
    .sidebar-header {
        padding-top: 1.5rem;
    }
    
    .channels, .users {
        max-height: calc(50vh - 120px);
        overflow-y: auto;
    }
    
    #channel-list, #user-list {
        display: block;
    }
    
    #channel-list li, #user-list li {
        padding: 0.75rem;
        font-size: 0.9rem;
    }
    
    .user-info {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        padding: 0.75rem;
        background: var(--bg-secondary);
        border-top: 1px solid var(--border);
        padding-bottom: calc(0.75rem + var(--safe-bottom));
    }
    
    .chat-area {
        flex: 1;
        min-height: 0;
        height: 100vh;
        padding-top: 3rem;
    }
    
    .chat-header {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        background: var(--bg-secondary);
        border-bottom: 1px solid var(--border);
        z-index: 100;
        padding: 0.75rem 1rem;
        padding-left: 3.5rem;
        padding-top: calc(0.75rem + var(--safe-top));
    }
    
    .chat-header h2 {
        font-size: 1rem;
    }
    
    .messages {
        padding: 0.5rem;
        padding-bottom: calc(4rem + var(--safe-bottom));
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
    }
    
    .message {
        margin-bottom: 0.75rem;
        padding: 0.5rem;
        background: var(--bg-tertiary);
        border-radius: 8px;
    }
    
    .msg-user {
        font-size: 0.9rem;
    }
    
    .msg-time {
        display: inline;
        margin-left: 0.5rem;
        font-size: 0.7rem;
    }
    
    .msg-text {
        margin-top: 0.25rem;
        font-size: 0.95rem;
        line-height: 1.4;
        word-break: break-word;
    }
    
    .input-area {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: var(--bg-secondary);
        border-top: 1px solid var(--border);
        padding: 0.5rem;
        padding-bottom: calc(0.5rem + var(--safe-bottom));
        z-index: 100;
    }
    
    #message-input {
        padding: 0.75rem;
        font-size: 16px;
        border-radius: 20px;
    }
    
    #send-btn {
        padding: 0.75rem 1.25rem;
        font-size: 0.9rem;
        border-radius: 20px;
    }
    
    .modal {
        padding: env(safe-area-inset-top, 0) env(safe-area-inset-right, 0) env(safe-area-inset-bottom, 0) env(safe-area-inset-left, 0);
    }
    
    .modal-content {
        padding: 1.5rem;
        border-radius: 16px;
        max-width: 90vw;
    }
    
    .modal-content h2 {
        font-size: 1.1rem;
    }
    
    .modal-content .logo {
        font-size: 3rem;
    }
    
    #username-input {
        font-size: 16px;
    }
    
    .admin-panel {
        width: 90vw;
        max-width: 300px;
    }
    
    /* Hide admin button on mobile, use long press */
    .admin-btn {
        display: none;
    }
}

/* Long press for admin actions on mobile */
@media (max-width: 768px) and (hover: none) {
    .message {
        -webkit-user-select: none;
        user-select: none;
    }
}

/* Landscape phone */
@media (max-width: 768px) and (orientation: landscape) {
    .sidebar {
        width: 240px;
    }
    
    .chat-header {
        padding-top: var(--safe-top);
    }
    
    .input-area {
        padding-bottom: var(--safe-bottom);
    }
    
    .messages {
        padding-bottom: calc(3.5rem + var(--safe-bottom));
    }
}

/* Very small screens */
@media (max-width: 360px) {
    .sidebar {
        width: 100%;
    }
    
    .login-box {
        padding: 1rem;
    }
    
    .modal-content {
        padding: 1rem;
    }
    
    .google-btn {
        padding: 10px 16px;
        font-size: 0.9rem;
    }
}

/* Tablet */
@media (min-width: 769px) and (max-width: 1024px) {
    .sidebar {
        width: 200px;
    }
    
    .chat-area {
        flex: 1;
    }
}

/* Desktop - always show sidebar */
@media (min-width: 769px) {
    .menu-toggle {
        display: none !important;
    }
    
    .sidebar {
        position: relative;
        left: auto;
    }
    
    .sidebar-overlay {
        display: none !important;
    }
    
    .chat-header {
        padding-left: 1rem;
    }
}

/* Touch device improvements */
@media (hover: none) and (pointer: coarse) {
    #channel-list li, #user-list li {
        min-height: 44px;
        display: flex;
        align-items: center;
    }
    
    .google-btn {
        min-height: 48px;
    }
    
    #send-btn {
        min-width: 70px;
    }
}

/* Dark mode scrollbar */
::-webkit-scrollbar {
    width: 6px;
}

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

::-webkit-scrollbar-thumb {
    background: var(--bg-tertiary);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--accent);
}