:root {
    --indigo: #4f46e5;
    --indigo-strong: #4338ca;
    --violet: #7c3aed;

    --page: radial-gradient(1200px 600px at 15% -10%, #6366f1 0%, transparent 60%),
            radial-gradient(1000px 600px at 110% 10%, #8b5cf6 0%, transparent 55%),
            linear-gradient(160deg, #eef2ff 0%, #f8fafc 45%, #eef2ff 100%);
    --surface: #ffffff;
    --surface-sunken: #f8fafc;
    --surface-hover: #f1f5f9;
    --bubble-in: #f1f5f9;

    --text: #0f172a;
    --text-muted: #64748b;
    --text-faint: #94a3b8;
    --border: #e2e8f0;

    --online: #10b981;
    --danger: #e11d48;
    --shadow: 0 24px 60px -20px rgba(15, 23, 42, 0.35);
    --radius: 20px;
    --header-fg: #ffffff;
}

@media (prefers-color-scheme: dark) {
    :root {
        --page: radial-gradient(1200px 600px at 15% -10%, #312e81 0%, transparent 60%),
                radial-gradient(1000px 600px at 110% 10%, #4c1d95 0%, transparent 55%),
                linear-gradient(160deg, #0b1020 0%, #0f172a 50%, #0b1020 100%);
        --surface: #111827;
        --surface-sunken: #0d1424;
        --surface-hover: #1c2537;
        --bubble-in: #1e293b;

        --text: #e2e8f0;
        --text-muted: #94a3b8;
        --text-faint: #64748b;
        --border: #1e293b;
        --shadow: 0 24px 60px -20px rgba(0, 0, 0, 0.7);
    }
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    -webkit-text-size-adjust: 100%;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--page);
    background-attachment: fixed;
    color: var(--text);
    min-height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: clamp(0px, 2vw, 24px);
    /* Keeps the composer clear of the iOS home indicator and notch. */
    padding-bottom: max(clamp(0px, 2vw, 24px), env(safe-area-inset-bottom));
    padding-left: max(clamp(0px, 2vw, 24px), env(safe-area-inset-left));
    padding-right: max(clamp(0px, 2vw, 24px), env(safe-area-inset-right));
    -webkit-font-smoothing: antialiased;
}

.container {
    width: 100%;
    max-width: 1100px;
}

.hidden {
    display: none !important;
}

/* ---------------------------------------------------------------- *
 * Login
 * ---------------------------------------------------------------- */

.login-card {
    background: var(--surface);
    padding: clamp(28px, 5vw, 44px);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
    text-align: center;
    max-width: 420px;
    margin: 0 auto;
}

.lock-badge {
    width: 60px;
    height: 60px;
    margin: 0 auto 20px;
    display: grid;
    place-items: center;
    border-radius: 18px;
    color: #fff;
    background: linear-gradient(135deg, var(--indigo), var(--violet));
    box-shadow: 0 12px 24px -10px var(--indigo);
}

.lock-badge svg {
    width: 30px;
    height: 30px;
}

.login-card h1 {
    font-size: 1.6rem;
    letter-spacing: -0.02em;
}

.tagline {
    color: var(--text-muted);
    margin: 8px 0 28px;
    font-size: 0.95rem;
    line-height: 1.5;
}

.field-label {
    display: block;
    text-align: left;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 8px;
}

.signed-in-as {
    padding: 12px 14px;
    margin-bottom: 14px;
    border-radius: 12px;
    background: var(--surface-sunken);
    border: 1px solid var(--border);
    color: var(--text-muted);
    font-size: 0.9rem;
}

.signed-in-as strong {
    color: var(--text);
}

.alt-action {
    margin-top: 14px;
    font-size: 0.82rem;
    color: var(--text-muted);
}

.alt-action a {
    color: var(--indigo);
    font-weight: 600;
}

#usernameInput {
    width: 100%;
    padding: 14px 16px;
    margin-bottom: 14px;
    border: 1.5px solid var(--border);
    border-radius: 12px;
    background: var(--surface-sunken);
    color: var(--text);
    font-size: 16px; /* Below 16px iOS Safari zooms on focus. */
}

#usernameInput::placeholder {
    color: var(--text-faint);
}

#usernameInput:focus-visible,
#messageInput:focus-visible {
    outline: none;
    border-color: var(--indigo);
    box-shadow: 0 0 0 4px color-mix(in srgb, var(--indigo) 18%, transparent);
}

.primary-btn {
    width: 100%;
    padding: 14px;
    background: linear-gradient(135deg, var(--indigo), var(--violet));
    color: #fff;
    border: none;
    border-radius: 12px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.15s ease, box-shadow 0.15s ease, opacity 0.15s ease;
    box-shadow: 0 10px 20px -12px var(--indigo);
}

.primary-btn:hover:not(:disabled) {
    transform: translateY(-1px);
    box-shadow: 0 16px 28px -14px var(--indigo);
}

.primary-btn:active:not(:disabled) {
    transform: translateY(0);
}

.primary-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.feature-list {
    list-style: none;
    margin-top: 26px;
    padding-top: 22px;
    border-top: 1px solid var(--border);
    text-align: left;
    display: grid;
    gap: 10px;
}

.feature-list li {
    position: relative;
    padding-left: 26px;
    font-size: 0.85rem;
    color: var(--text-muted);
    line-height: 1.45;
}

.feature-list li::before {
    content: '';
    position: absolute;
    left: 6px;
    top: 0.45em;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--online);
}

/* ---------------------------------------------------------------- *
 * Chat shell
 * ---------------------------------------------------------------- */

.chat-screen {
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
    overflow: hidden;
    height: min(720px, calc(100dvh - 48px));
    display: flex;
    flex-direction: column;
}

.header {
    background: linear-gradient(120deg, var(--indigo), var(--violet));
    color: var(--header-fg);
    padding: 14px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 650;
    letter-spacing: -0.01em;
}

.brand-dot {
    width: 9px;
    height: 9px;
    border-radius: 50%;
    background: #4ade80;
    box-shadow: 0 0 0 4px rgba(74, 222, 128, 0.25);
}

.user-info {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.identity {
    font-size: 0.9rem;
    font-weight: 600;
    padding: 5px 12px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.18);
    max-width: 40vw;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.ghost-btn {
    padding: 7px 14px;
    background: rgba(255, 255, 255, 0.14);
    border: 1px solid rgba(255, 255, 255, 0.25);
    border-radius: 999px;
    color: inherit;
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
}

.ghost-btn:hover {
    background: rgba(255, 255, 255, 0.26);
}

.main-content {
    display: flex;
    flex: 1;
    min-height: 0;
}

/* ---------------------------------------------------------------- *
 * User list
 * ---------------------------------------------------------------- */

.users-panel {
    width: 260px;
    flex-shrink: 0;
    border-right: 1px solid var(--border);
    padding: 18px 14px;
    overflow-y: auto;
    background: var(--surface-sunken);
}

.panel-title {
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.09em;
    color: var(--text-muted);
    margin: 0 6px 14px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.count:not(:empty) {
    background: var(--surface-hover);
    color: var(--text-muted);
    border-radius: 999px;
    padding: 1px 8px;
    font-size: 0.7rem;
    letter-spacing: 0;
}

#usersList {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.user-item {
    width: 100%;
    padding: 10px 12px;
    background: transparent;
    border: none;
    border-radius: 12px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 11px;
    color: var(--text);
    font-family: inherit;
    font-size: 0.95rem;
    text-align: left;
    transition: background 0.15s ease;
}

.user-item:hover {
    background: var(--surface-hover);
}

.user-item.active {
    background: linear-gradient(135deg, var(--indigo), var(--violet));
    color: #fff;
}

.avatar {
    width: 34px;
    height: 34px;
    flex-shrink: 0;
    border-radius: 50%;
    display: grid;
    place-items: center;
    font-weight: 600;
    font-size: 0.85rem;
    color: #fff;
    background: linear-gradient(135deg, #64748b, #475569);
}

.user-item.active .avatar {
    background: rgba(255, 255, 255, 0.25);
}

.user-name {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-weight: 500;
}

.user-label {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: baseline;
    gap: 6px;
}

.guest-tag {
    flex-shrink: 0;
    font-size: 0.62rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    padding: 1px 6px;
    border-radius: 999px;
    background: var(--surface-hover);
    color: var(--text-faint);
}

.user-item.active .guest-tag {
    background: rgba(255, 255, 255, 0.22);
    color: #fff;
}

.user-status {
    width: 8px;
    height: 8px;
    flex-shrink: 0;
    background: var(--online);
    border-radius: 50%;
}

.user-status.offline {
    background: var(--text-faint);
    opacity: 0.5;
}

.unread-badge {
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    background: var(--danger);
    color: #fff;
    border-radius: 999px;
    font-size: 0.7rem;
    font-weight: 700;
    display: grid;
    place-items: center;
}

.users-empty {
    color: var(--text-faint);
    font-size: 0.85rem;
    line-height: 1.5;
    padding: 8px 6px;
}

/* ---------------------------------------------------------------- *
 * Conversation
 * ---------------------------------------------------------------- */

.chat-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
}

.chat-header {
    padding: 12px 18px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 60px;
}

.back-btn {
    display: none;
    width: 36px;
    height: 36px;
    flex-shrink: 0;
    border: none;
    border-radius: 10px;
    background: var(--surface-hover);
    color: var(--text);
    cursor: pointer;
    place-items: center;
}

.back-btn svg {
    width: 20px;
    height: 20px;
}

.chat-title {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
    flex: 1;
}

.ghost-btn.subtle {
    flex-shrink: 0;
    background: var(--surface-hover);
    border: 1px solid var(--border);
    color: var(--text-muted);
    font-size: 0.78rem;
    padding: 6px 12px;
}

.ghost-btn.subtle:hover {
    background: var(--border);
}

#chatWith {
    font-weight: 650;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.encryption-status {
    font-size: 0.75rem;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    gap: 6px;
}

.encryption-status.secure {
    color: var(--online);
}

.encryption-status.secure::before {
    content: '🔒';
    font-size: 0.7rem;
}

.encryption-status.pending {
    color: var(--text-faint);
}

.safety-number {
    padding: 10px 18px;
    background: var(--surface-sunken);
    border-bottom: 1px solid var(--border);
    display: grid;
    gap: 3px;
}

.safety-label {
    font-size: 0.65rem;
    text-transform: uppercase;
    letter-spacing: 0.09em;
    color: var(--text-faint);
}

.safety-number code {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.72rem;
    letter-spacing: 0.04em;
    color: var(--text-muted);
    word-break: break-word;
}

.safety-hint {
    font-size: 0.68rem;
    color: var(--text-faint);
}

.messages {
    flex: 1;
    padding: 20px 18px;
    overflow-y: auto;
    overscroll-behavior: contain;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.message {
    max-width: min(76%, 520px);
    padding: 9px 14px;
    border-radius: 18px;
    line-height: 1.45;
    font-size: 0.95rem;
    overflow-wrap: anywhere;
    animation: pop-in 0.18s ease-out;
}

@keyframes pop-in {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: none; }
}

@media (prefers-reduced-motion: reduce) {
    .message { animation: none; }
    .primary-btn { transition: none; }
}

.message.sent {
    align-self: flex-end;
    background: linear-gradient(135deg, var(--indigo), var(--violet));
    color: #fff;
    border-bottom-right-radius: 6px;
}

.message.received {
    align-self: flex-start;
    background: var(--bubble-in);
    color: var(--text);
    border-bottom-left-radius: 6px;
}

.message-info {
    font-size: 0.65rem;
    opacity: 0.65;
    margin-top: 3px;
    text-align: right;
}

.system-message {
    align-self: center;
    text-align: center;
    max-width: 80%;
    color: var(--text-faint);
    font-size: 0.75rem;
    padding: 6px 12px;
    margin: 4px 0;
    background: var(--surface-sunken);
    border-radius: 999px;
}

.input-area {
    padding: 14px 18px;
    padding-bottom: max(14px, env(safe-area-inset-bottom));
    border-top: 1px solid var(--border);
    display: flex;
    gap: 10px;
    align-items: center;
    background: var(--surface);
}

#messageInput {
    flex: 1;
    min-width: 0;
    padding: 12px 16px;
    border: 1.5px solid var(--border);
    border-radius: 999px;
    background: var(--surface-sunken);
    color: var(--text);
    font-family: inherit;
    font-size: 16px; /* Below 16px iOS Safari zooms on focus. */
}

#messageInput::placeholder {
    color: var(--text-faint);
}

.send-btn {
    width: 46px;
    height: 46px;
    flex-shrink: 0;
    padding: 0;
    border-radius: 50%;
    display: grid;
    place-items: center;
}

.send-btn svg {
    width: 20px;
    height: 20px;
}

#messageInput:disabled {
    opacity: 0.55;
    cursor: not-allowed;
}

/* ---------------------------------------------------------------- *
 * Toast
 * ---------------------------------------------------------------- */

.status {
    position: fixed;
    left: 50%;
    bottom: max(20px, env(safe-area-inset-bottom));
    transform: translate(-50%, 12px);
    width: max-content;
    max-width: min(90vw, 420px);
    padding: 12px 18px;
    background: #0f172a;
    color: #f8fafc;
    border-radius: 12px;
    font-size: 0.88rem;
    box-shadow: 0 16px 40px -12px rgba(0, 0, 0, 0.5);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease, transform 0.2s ease;
    z-index: 50;
}

.status.show {
    opacity: 1;
    transform: translate(-50%, 0);
}

.status.error {
    background: var(--danger);
}

.status.success {
    background: #059669;
}

/* ---------------------------------------------------------------- *
 * Mobile: one pane at a time
 * ---------------------------------------------------------------- */

@media (max-width: 720px) {
    body {
        padding: 0;
        align-items: stretch;
    }

    .container {
        max-width: none;
        display: flex;
        flex-direction: column;
        justify-content: center;
        min-height: 100dvh;
    }

    .login-card {
        margin: auto;
        border: none;
        border-radius: 18px;
    }

    .chat-screen {
        height: 100dvh;
        border-radius: 0;
        border: none;
    }

    .header {
        padding-top: max(14px, env(safe-area-inset-top));
    }

    .users-panel,
    .chat-panel {
        width: 100%;
        flex: 1 0 100%;
        border-right: none;
    }

    /* Both panes stay mounted; the container slides between them. */
    .main-content {
        overflow: hidden;
    }

    .main-content > * {
        transition: transform 0.25s ease;
    }

    .main-content.showing-chat > * {
        transform: translateX(-100%);
    }

    .back-btn {
        display: grid;
    }

    .messages {
        padding: 16px 12px;
    }

    .message {
        max-width: 85%;
    }

    .input-area {
        padding: 10px 12px;
        padding-bottom: max(10px, env(safe-area-inset-bottom));
    }

    .safety-hint {
        display: none;
    }

    .ghost-btn.subtle {
        font-size: 0.72rem;
        padding: 5px 10px;
    }
}

@media (max-width: 720px) and (prefers-reduced-motion: reduce) {
    .main-content > * {
        transition: none;
    }
}

