/* ── Reset & base ───────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* Prevent display:flex/grid rules from overriding the HTML hidden attribute */
[hidden] { display: none !important; }

:root {
    --bg:          #f0f2f5;
    --surface:     #ffffff;
    --border:      #e4e6ea;
    --accent:      #0078ff;
    --accent-h:    #005fd6;
    --text:        #1c1e21;
    --text-muted:  #65676b;
    --online:      #31a24c;
    --offline:     #c4c6cc;
    --danger:      #e53935;
    --success:     #2e7d32;
    --msg-out-bg:  #0078ff;
    --msg-in-bg:   #f0f2f5;
    --radius:      12px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    font-size: 15px;
    color: var(--text);
    background: var(--bg);
}

/* ── Login ──────────────────────────────────────────────────────────────── */
.page-login {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

.login-box {
    background: var(--surface);
    border-radius: 16px;
    box-shadow: 0 4px 24px rgba(0,0,0,.12);
    padding: 2.5rem 2rem;
    width: 360px;
    text-align: center;
}

.login-logo { font-size: 2.5rem; margin-bottom: .5rem; }
.login-box h1 { font-size: 1.5rem; margin-bottom: 1.75rem; color: var(--accent); }

.field { text-align: left; margin-bottom: 1rem; }
.field label { display: block; font-size: .85rem; font-weight: 600; color: var(--text-muted); margin-bottom: .35rem; }
.field input {
    width: 100%;
    padding: .65rem .9rem;
    border: 1.5px solid var(--border);
    border-radius: 8px;
    font-size: .95rem;
    outline: none;
    transition: border-color .15s;
}
.field input:focus { border-color: var(--accent); }

.login-box button[type=submit] {
    width: 100%;
    margin-top: .25rem;
    padding: .75rem;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background .15s;
}
.login-box button[type=submit]:hover { background: var(--accent-h); }

.error {
    margin-bottom: .75rem;
    padding: .5rem .75rem;
    background: #ffebee;
    border: 1px solid #ffcdd2;
    border-radius: 6px;
    color: var(--danger);
    font-size: .875rem;
    text-align: left;
}

/* ── Chat layout ────────────────────────────────────────────────────────── */
.page-chat { height: 100vh; overflow: hidden; }

.layout {
    display: flex;
    height: 100vh;
}

/* ── Sidebar ────────────────────────────────────────────────────────────── */
.sidebar {
    width: 280px;
    min-width: 280px;
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
}

.sidebar-header {
    padding: .85rem 1rem;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.my-name { font-weight: 700; font-size: .95rem; }

.icon-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-muted);
    padding: .35rem;
    border-radius: 8px;
    display: flex;
    align-items: center;
    transition: background .15s, color .15s;
}
.icon-btn:hover { background: var(--bg); color: var(--text); }

.users-list { flex: 1; overflow-y: auto; }

.user-item {
    display: flex;
    align-items: center;
    gap: .75rem;
    padding: .7rem 1rem;
    cursor: pointer;
    transition: background .12s;
    position: relative;
}
.user-item:hover  { background: var(--bg); }
.user-item.active { background: #e8f0fe; }

/* Avatar */
.avatar {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--accent);
    color: #fff;
    font-weight: 700;
    font-size: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    position: relative;
    user-select: none;
}
.avatar.sm { width: 36px; height: 36px; font-size: .85rem; }

.status-dot {
    position: absolute;
    bottom: 1px;
    right: 1px;
    width: 11px;
    height: 11px;
    border-radius: 50%;
    border: 2px solid var(--surface);
    background: var(--offline);
}
.status-dot.online { background: var(--online); }

.user-info { flex: 1; min-width: 0; }
.user-name   { font-weight: 600; font-size: .9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-status { font-size: .78rem; color: var(--text-muted); margin-top: .1rem; }

.unread-badge {
    background: var(--accent);
    color: #fff;
    border-radius: 10px;
    padding: .1rem .45rem;
    font-size: .72rem;
    font-weight: 700;
    min-width: 20px;
    text-align: center;
}

/* ── Chat main ──────────────────────────────────────────────────────────── */
.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    background: var(--bg);
}

.placeholder {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}
.placeholder-inner { text-align: center; color: var(--text-muted); }
.placeholder-icon  { font-size: 3rem; margin-bottom: .75rem; }

.chat-window {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
    background: var(--surface);
}

.chat-header {
    padding: .75rem 1.25rem;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--surface);
}

.chat-header-info {
    display: flex;
    align-items: center;
    gap: .75rem;
}

.chat-with-name   { font-weight: 700; font-size: .95rem; }
.chat-with-status { font-size: .78rem; color: var(--text-muted); }

.call-icon-btn { color: var(--accent); }
.call-icon-btn:hover { background: #e8f0fe; color: var(--accent-h); }

/* Messages */
.messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: .35rem;
    background: #f7f8fa;
}

.msg {
    display: flex;
    flex-direction: column;
    max-width: 68%;
}
.msg.out { align-self: flex-end; align-items: flex-end; }
.msg.in  { align-self: flex-start; }

.msg-bubble {
    padding: .55rem .95rem;
    border-radius: 16px;
    line-height: 1.45;
    word-break: break-word;
    white-space: pre-wrap;
}
.msg.out .msg-bubble {
    background: var(--msg-out-bg);
    color: #fff;
    border-bottom-right-radius: 4px;
}
.msg.in .msg-bubble {
    background: var(--msg-in-bg);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
}
.msg-time {
    font-size: .7rem;
    color: var(--text-muted);
    margin-top: .2rem;
    padding: 0 .3rem;
}

/* Date separator */
.date-sep {
    text-align: center;
    font-size: .75rem;
    color: var(--text-muted);
    margin: .5rem 0;
}
.date-sep span {
    background: #e2e5ea;
    border-radius: 10px;
    padding: .2rem .65rem;
}

/* Input bar */
.input-bar {
    display: flex;
    align-items: center;
    gap: .5rem;
    padding: .65rem 1rem;
    border-top: 1px solid var(--border);
    background: var(--surface);
}
.input-bar input {
    flex: 1;
    padding: .6rem 1rem;
    border: 1.5px solid var(--border);
    border-radius: 20px;
    font-size: .95rem;
    outline: none;
    transition: border-color .15s;
}
.input-bar input:focus { border-color: var(--accent); }

.send-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--accent);
    color: #fff;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background .15s;
}
.send-btn:hover { background: var(--accent-h); }

/* ── Modal (incoming call) ──────────────────────────────────────────────── */
.modal {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
}
.modal-box {
    background: var(--surface);
    border-radius: 16px;
    padding: 2rem 2.5rem;
    text-align: center;
    min-width: 280px;
    box-shadow: 0 8px 32px rgba(0,0,0,.25);
}
.modal-icon { font-size: 2.5rem; margin-bottom: .75rem; }
.modal-box p { font-size: 1rem; margin-bottom: 1.5rem; }
.modal-actions { display: flex; gap: .75rem; justify-content: center; }

.btn-success, .btn-danger {
    padding: .65rem 1.4rem;
    border: none;
    border-radius: 8px;
    font-size: .95rem;
    font-weight: 600;
    cursor: pointer;
    transition: opacity .15s;
}
.btn-success:hover, .btn-danger:hover { opacity: .85; }
.btn-success { background: var(--success); color: #fff; }
.btn-danger  { background: var(--danger);  color: #fff; }

/* ── Video overlay ──────────────────────────────────────────────────────── */
.video-overlay {
    position: fixed;
    inset: 0;
    background: #111;
    z-index: 200;
    display: flex;
    align-items: center;
    justify-content: center;
}

#remoteVideo {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.local-video {
    position: absolute;
    bottom: 90px;
    right: 20px;
    width: 180px;
    height: 135px;
    object-fit: cover;
    border-radius: 10px;
    border: 2px solid rgba(255,255,255,.6);
    background: #333;
}

.connecting-label {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: rgba(255,255,255,.8);
    font-size: 1.1rem;
    pointer-events: none;
}

.video-controls {
    position: absolute;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: .75rem;
}

.vc-btn {
    padding: .65rem 1.2rem;
    border: none;
    border-radius: 24px;
    font-size: 1.1rem;
    cursor: pointer;
    background: rgba(255,255,255,.18);
    color: #fff;
    backdrop-filter: blur(4px);
    transition: background .15s;
}
.vc-btn:hover { background: rgba(255,255,255,.28); }
.vc-btn--end  { background: var(--danger); }
.vc-btn--end:hover { background: #c62828; }

/* ── Scrollbar ──────────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ── Back button (mobile only) ──────────────────────────────────────────── */
.back-btn { display: none; }

/* ── Tablet (≤ 900px) ───────────────────────────────────────────────────── */
@media (max-width: 900px) {
    .sidebar { width: 240px; min-width: 240px; }
    .msg { max-width: 80%; }
}

/* ── Mobile (≤ 600px) ───────────────────────────────────────────────────── */
@media (max-width: 600px) {
    /* Single-column layout: sidebar and chat-main are stacked in a clip */
    .layout { position: relative; overflow: hidden; }

    .sidebar {
        width: 100%;
        min-width: 0;
        position: absolute;
        inset: 0;
        z-index: 10;
        transition: transform .25s ease;
    }
    .sidebar.mobile-hidden {
        transform: translateX(-100%);
        pointer-events: none;
    }

    .chat-main {
        width: 100%;
        position: absolute;
        inset: 0;
        transform: translateX(100%);
        transition: transform .25s ease;
    }
    .chat-main.mobile-visible {
        transform: translateX(0);
    }

    /* Show back button */
    .back-btn { display: flex; }

    /* Wider bubbles on narrow screens */
    .msg { max-width: 85%; }

    /* Safe-area padding for iOS home indicator */
    .input-bar {
        padding-bottom: calc(.65rem + env(safe-area-inset-bottom, 0px));
    }
    .video-controls {
        bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    }

    /* Smaller PiP video */
    .local-video {
        width: 100px;
        height: 75px;
        bottom: 90px;
        right: 12px;
    }

    /* Bigger touch targets */
    .user-item { padding: .9rem 1rem; }
    .send-btn { width: 44px; height: 44px; }
    .vc-btn { padding: .75rem 1.4rem; font-size: 1.25rem; }

    /* Full-width login card */
    .login-box {
        width: calc(100% - 2rem);
        padding: 2rem 1.25rem;
    }
}