* { box-sizing: border-box; }

:root {
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --bg: #0f172a;
    --panel: #111827;
    --panel-2: #1e293b;
    --line: #334155;
    --blue: #2563eb;
    --green: #22c55e;
    --red: #ef4444;
    --text-soft: #94a3b8;
}

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    background: var(--bg);
    color: white;
    font-family: Arial, sans-serif;
    overscroll-behavior: none;
}

body {
    overflow: hidden;
}

.hidden { display: none !important; }

.auth-screen {
    min-height: 100dvh;
    padding: calc(20px + var(--safe-top)) 20px calc(20px + var(--safe-bottom));
    background:
        radial-gradient(circle at top left, rgba(37,99,235,0.35), transparent 30%),
        radial-gradient(circle at bottom right, rgba(34,197,94,0.22), transparent 25%),
        var(--bg);
    display: flex;
    align-items: center;
    justify-content: center;
}

.auth-card {
    width: 100%;
    max-width: 390px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 22px;
    padding: 24px;
    box-shadow: 0 25px 80px rgba(0,0,0,0.35);
    text-align: center;
}

.auth-logo {
    width: 92px;
    height: 92px;
    object-fit: contain;
    border-radius: 22px;
    margin-bottom: 10px;
}

.auth-card h1 {
    margin: 0;
    font-size: 34px;
}

.auth-card p {
    color: var(--text-soft);
    margin-bottom: 20px;
}

.auth-card input {
    margin-bottom: 12px;
    text-align: left;
}

.auth-actions {
    display: flex;
    gap: 10px;
}

.auth-actions button {
    flex: 1;
}

.auth-message {
    margin-top: 14px;
    color: #fca5a5;
    font-size: 14px;
    min-height: 18px;
}

.app {
    height: 100dvh;
    display: grid;
    grid-template-columns: 350px 1fr;
    overflow: hidden;
}

.sidebar {
    background: var(--panel);
    border-right: 1px solid var(--line);
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.top {
    padding: calc(16px + var(--safe-top)) 16px 16px;
    background: #020617;
    border-bottom: 1px solid var(--line);
}

.top-line {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    align-items: center;
}

.brand-line {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.brand-logo {
    width: 42px;
    height: 42px;
    object-fit: contain;
    border-radius: 10px;
    flex-shrink: 0;
}

.top h1 {
    margin: 0;
    font-size: 25px;
    line-height: 1.05;
}

.subtitle {
    margin: 4px 0 0;
    color: var(--text-soft);
    font-size: 13px;
}

.top-actions {
    display: flex;
    gap: 8px;
    align-items: center;
}

.logout-btn, .notify-btn {
    min-width: 46px;
    min-height: 46px;
    padding: 8px 10px;
    background: var(--panel-2);
    font-size: 13px;
    border-radius: 16px;
}

input {
    width: 100%;
    padding: 12px;
    border-radius: 14px;
    border: 1px solid #475569;
    background: #020617;
    color: white;
    font-size: 16px;
    outline: none;
}

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

button {
    border: 0;
    border-radius: 14px;
    padding: 12px 15px;
    background: var(--blue);
    color: white;
    font-weight: bold;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

.tabs {
    display: flex;
    gap: 10px;
    padding: 12px;
    border-bottom: 1px solid var(--line);
}

.tab {
    flex: 1;
    background: var(--panel-2);
    min-height: 48px;
    font-size: 16px;
}

.tab.active {
    background: var(--blue);
}

.search, .new-chat {
    padding: 12px;
    border-bottom: 1px solid var(--line);
}

.new-chat {
    display: flex;
    gap: 8px;
}

.new-chat button {
    min-width: 54px;
}

.chat-list {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 10px;
    -webkit-overflow-scrolling: touch;
}

.loading, .empty {
    text-align: center;
    color: var(--text-soft);
    margin-top: 40px;
}

.error {
    text-align: center;
    color: #fca5a5;
    margin: 20px;
    font-size: 14px;
}

.chat-item, .user-item {
    display: flex;
    gap: 12px;
    padding: 13px;
    border-radius: 18px;
    cursor: pointer;
    background: var(--panel-2);
    margin-bottom: 10px;
    transition: 0.2s;
    position: relative;
    align-items: center;
}

.chat-item:hover, .user-item:hover {
    background: #273449;
}

.chat-item.active {
    background: var(--blue);
}

.unread-badge {
    min-width: 22px;
    height: 22px;
    border-radius: 50%;
    background: var(--red);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: bold;
    padding: 0 6px;
}

.avatar {
    width: 46px;
    height: 46px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--blue), var(--green));
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    flex-shrink: 0;
}

.chat-info {
    min-width: 0;
    flex: 1;
}

.chat-topline {
    display: flex;
    justify-content: space-between;
    gap: 8px;
}

.chat-name {
    font-weight: bold;
    margin-bottom: 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-time {
    color: #cbd5e1;
    font-size: 12px;
    flex-shrink: 0;
}

.chat-preview {
    color: #cbd5e1;
    font-size: 13px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-window {
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    background: var(--bg);
    height: 100dvh;
}

.chat-header {
    min-height: calc(74px + var(--safe-top));
    padding: calc(10px + var(--safe-top)) 14px 10px;
    background: var(--panel);
    border-bottom: 1px solid var(--line);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    flex-shrink: 0;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
}

.room-meta {
    min-width: 0;
}

.header-title {
    font-weight: bold;
    font-size: 17px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.back-btn {
    display: none;
    background: var(--panel-2);
    min-width: 48px;
    min-height: 48px;
    padding: 0;
    border-radius: 16px;
    font-size: 22px;
    line-height: 1;
    flex-shrink: 0;
}

.status {
    color: var(--green);
    font-size: 13px;
    margin-top: 2px;
}

.status.offline {
    color: #fca5a5;
}

.call-buttons {
    display: flex;
    gap: 8px;
    flex-shrink: 0;
}

.call-buttons button {
    background: var(--panel-2);
    width: 48px;
    height: 48px;
    padding: 0;
    border-radius: 50%;
    font-size: 19px;
}

.messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 14px;
    background:
        radial-gradient(circle at top left, rgba(37,99,235,0.18), transparent 30%),
        radial-gradient(circle at bottom right, rgba(34,197,94,0.10), transparent 25%),
        var(--bg);
    -webkit-overflow-scrolling: touch;
}

.row {
    display: flex;
    margin-bottom: 10px;
}

.row.mine {
    justify-content: flex-end;
}

.bubble {
    max-width: 74%;
    padding: 11px 13px 8px;
    border-radius: 18px;
    background: var(--panel-2);
    word-wrap: break-word;
    line-height: 1.35;
    font-size: 16px;
}

.mine .bubble {
    background: var(--blue);
}

.name {
    font-size: 12px;
    color: #93c5fd;
    font-weight: bold;
    margin-bottom: 4px;
}

.time {
    font-size: 10px;
    color: #cbd5e1;
    text-align: right;
    margin-top: 7px;
}

.attachment {
    margin-top: 8px;
}

.attachment-image {
    max-width: 260px;
    max-height: 260px;
    border-radius: 12px;
    display: block;
}

.attachment-file {
    display: inline-block;
    color: white;
    background: rgba(255,255,255,0.12);
    padding: 8px 10px;
    border-radius: 10px;
    text-decoration: none;
}

.attachment-audio {
    width: 260px;
    max-width: 100%;
    margin-top: 8px;
}

.recording-bar {
    padding: 10px 12px;
    background: #7f1d1d;
    color: white;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-shrink: 0;
}

.recording-bar button {
    background: var(--red);
    padding: 8px 12px;
}

.send {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px calc(10px + var(--safe-bottom));
    background: var(--panel);
    border-top: 1px solid var(--line);
    flex-shrink: 0;
}

.send input {
    min-height: 48px;
    border-radius: 16px;
    font-size: 16px;
}

.attach-btn {
    background: var(--panel-2);
    width: 48px;
    min-width: 48px;
    height: 48px;
    padding: 0;
    border-radius: 16px;
    font-size: 19px;
}

.attach-btn.recording {
    background: var(--red);
}

.send-btn {
    min-width: 70px;
    height: 48px;
    padding: 0 14px;
    border-radius: 16px;
    font-size: 15px;
}

.call-overlay {
    position: fixed;
    inset: 0;
    background: rgba(2,6,23,0.88);
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: calc(20px + var(--safe-top)) 20px calc(20px + var(--safe-bottom));
}

.call-card {
    width: 100%;
    max-width: 360px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 24px;
    padding: 28px;
    text-align: center;
    box-shadow: 0 25px 90px rgba(0,0,0,0.45);
}

.call-icon {
    width: 86px;
    height: 86px;
    margin: 0 auto 16px;
    border-radius: 50%;
    background: linear-gradient(135deg,var(--blue),var(--green));
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 34px;
}

.call-card h2 {
    margin: 0 0 8px;
}

.call-card p {
    color: var(--text-soft);
    margin: 0 0 22px;
}

.call-actions {
    display: flex;
    gap: 12px;
    justify-content: center;
    flex-wrap: wrap;
}

.accept-btn { background: var(--green); }
.decline-btn { background: var(--red); }
.mute-btn { background: var(--panel-2); }
.mute-btn.active { background: #f59e0b; }
#remoteAudio { width: 100%; margin-bottom: 12px; }

@media (max-width: 768px) {
    .app {
        grid-template-columns: 1fr;
        height: 100dvh;
    }

    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        width: 100%;
        z-index: 20;
        transition: transform 0.25s ease;
    }

    .sidebar.hidden-mobile {
        transform: translateX(-100%);
    }

    .chat-window {
        width: 100%;
        height: 100dvh;
    }

    .back-btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    .chat-header .avatar {
        width: 50px;
        height: 50px;
    }

    .bubble {
        max-width: 82%;
        font-size: 16px;
        border-radius: 19px;
    }

    .messages {
        padding: 12px 12px 10px;
    }

    .send {
        gap: 7px;
    }

    .send input {
        font-size: 16px;
    }

    .attachment-image {
        max-width: 220px;
        max-height: 220px;
    }

    .top {
        padding-top: calc(14px + var(--safe-top));
    }
}

@media (max-width: 390px) {
    .chat-header {
        min-height: calc(78px + var(--safe-top));
        padding-left: 10px;
        padding-right: 10px;
    }

    .back-btn,
    .call-buttons button,
    .attach-btn {
        width: 46px;
        min-width: 46px;
        height: 46px;
    }

    .send-btn {
        min-width: 64px;
    }

    .header-title {
        font-size: 16px;
    }

    .bubble {
        max-width: 84%;
    }
}

/* Stage 19 small additions */
.remember-row {
    display: flex;
    align-items: center;
    gap: 8px;
    color: #cbd5e1;
    font-size: 14px;
    margin: 2px 0 14px;
    text-align: left;
}
.remember-row input {
    width: auto;
    margin: 0;
}
.brand-logo,
.auth-logo {
    background: transparent;
}
