:root {
    --primary: #0056b3;
    --primary-dark: #003d80;
    --bg: #f5f6f7;
    --surface: #ffffff;
    --text: #222;
    --muted: #6c757d;
    --border: #dee2e6;
    --radius: 8px;
    --danger: #dc3545;
    --success: #28a745;
    --nav-h: 56px;
    --sidebar-w: 200px;
    --topbar-h: 50px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
    margin: 0;
    padding: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    height: 100%;
    overflow: hidden;
}

#login-view, #app-view {
    height: 100%;
    display: flex;
    flex-direction: column;
}

#login-view { overflow-y: auto; }

.app-header {
    background: var(--primary);
    color: white;
    padding: 1.5rem 1rem;
    text-align: center;
}
.app-header h1 { margin: 0 0 0.25rem; font-size: 1.8rem; }
.app-header p { margin: 0; opacity: 0.9; }

.app-main {
    padding: 1rem;
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.auth-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.5rem;
    width: 100%;
    max-width: 360px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.06);
}

input, textarea, select {
    width: 100%;
    padding: 0.75rem;
    margin-bottom: 0.75rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 1rem;
    font-family: inherit;
}
input:focus, textarea:focus { outline: none; border-color: var(--primary); }

.btn {
    background: var(--primary);
    color: white;
    border: none;
    padding: 0.75rem 1rem;
    border-radius: 6px;
    cursor: pointer;
    font-size: 1rem;
    width: 100%;
    text-align: center;
}
.btn:hover, .btn:active { background: var(--primary-dark); }
.btn-secondary { background: #6c757d; }
.btn-sm {
    background: var(--primary);
    color: white;
    border: none;
    padding: 0.4rem 0.75rem;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.9rem;
}
.btn-icon { padding: 0.4rem 0.6rem; }
.hint { text-align: center; color: var(--muted); font-size: 0.9rem; margin-top: 1rem; }

.top-bar {
    height: var(--topbar-h);
    background: var(--primary);
    color: white;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0 0.75rem;
    flex-shrink: 0;
    position: relative;
}
.logo { font-weight: 600; font-size: 1.1rem; }
.user-info { display: flex; align-items: center; gap: 0.5rem; font-size: 0.85rem; }
.user-info span { opacity: 0.9; }

#main-container {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    background: var(--bg);
}

.module-view { display: none; }
.module-view.active { display: block; }

.call-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.5);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}
.call-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.5rem;
    width: 100%;
    max-width: 360px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}
.call-card h2 { margin-top: 0; }
#voicecall-view.active,
#videocall-view.active { display: flex; }

#chat-view.active {
    display: flex;
    flex-direction: column;
}

#chat-list {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
}

#chat-conversations {
    flex: 1;
    overflow-y: auto;
    min-height: 0;
}

.welcome { text-align: center; margin-bottom: 1rem; }
.welcome h2 { margin: 0; }
.welcome p { color: var(--muted); margin: 0.25rem 0 0; }

.module-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: 1rem;
}
.module-tile {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.5rem 0.5rem;
    text-align: center;
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    color: var(--text);
    box-shadow: 0 2px 6px rgba(0,0,0,0.04);
}
.module-tile:active { background: var(--bg); }

.module-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.75rem;
}
.module-header h2 { margin: 0; font-size: 1.25rem; }

.search {
    background: var(--surface);
    margin-bottom: 0.75rem;
}

.list { display: flex; flex-direction: column; gap: 0.5rem; }

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.75rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.5rem;
    box-shadow: 0 1px 3px rgba(0,0,0,0.04);
}
.card-info { flex: 1; min-width: 0; }
.card-title { font-weight: 600; margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-sub { color: var(--muted); font-size: 0.85rem; margin: 0.15rem 0 0; }
.card-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 0.25rem; }

.form-actions { display: flex; gap: 0.5rem; margin-top: 0.5rem; }
.form-actions .btn { flex: 1; }

.bottom-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: var(--nav-h);
    background: var(--surface);
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: space-around;
    align-items: center;
    z-index: 100;
}
.nav-item {
    flex: 1;
    background: none;
    border: none;
    color: var(--muted);
    font-size: 0.75rem;
    padding: 0.5rem 0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}
.nav-item.active { color: var(--primary); font-weight: 600; }

.placeholder {
    padding: 3rem 1rem;
    text-align: center;
    color: var(--muted);
    border: 1px dashed var(--border);
    border-radius: var(--radius);
    background: var(--surface);
}

.toast {
    position: fixed;
    bottom: calc(var(--nav-h) + 0.5rem);
    left: 50%;
    transform: translateX(-50%);
    background: #333;
    color: white;
    padding: 0.6rem 1rem;
    border-radius: 20px;
    font-size: 0.9rem;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
    z-index: 200;
}
.toast.show { opacity: 1; }

.empty-state { text-align: center; color: var(--muted); padding: 2rem; }

#profile-view {
    max-width: 560px;
    margin: 0 auto;
}

#profile-view h3 {
    border-top: 1px solid var(--border);
    padding-top: 1rem;
    margin-top: 1.5rem;
}

.online-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--success);
    margin-right: 4px;
    vertical-align: middle;
}
.user-registered {
    text-decoration: underline;
}

.verified-badge,
.unverified-badge {
    font-size: 0.75rem;
    padding: 0.15rem 0.4rem;
    border-radius: 12px;
    margin-left: 0.25rem;
    font-weight: 500;
}
.verified-badge {
    background: #d4edda;
    color: #155724;
}
.unverified-badge {
    background: #fff3cd;
    color: #856404;
}

.requests-list {
    margin-bottom: 0.75rem;
}
.requests-heading {
    font-size: 1rem;
    margin: 0 0 0.5rem;
    color: var(--text);
}
.requests-count-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    background: var(--danger);
    color: white;
    font-size: 0.65rem;
    font-weight: 700;
    min-width: 18px;
    height: 18px;
    line-height: 18px;
    text-align: center;
    border-radius: 50%;
    padding: 0 4px;
}
#contact-requests-modal-list .card {
    margin-bottom: 0.5rem;
}

#app-layout {
    display: flex;
    height: 100%;
}

#app-sidebar {
    width: var(--sidebar-w);
    background: var(--primary);
    color: white;
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    overflow-y: auto;
    overflow-x: hidden;
    z-index: 100;
    position: relative;
}

.sidebar-brand {
    height: var(--topbar-h);
    width: 100%;
    background: var(--primary);
    color: white;
    border: none;
    border-bottom: 1px solid rgba(0,0,0,0.15);
    border-right: 1px solid rgba(0,0,0,0.15);
    padding: 0.25rem;
    font-size: 0.85rem;
    font-weight: 600;
    line-height: 1.2;
    text-align: center;
    cursor: pointer;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: clip;
}

.sidebar-brand:hover { background: rgba(255,255,255,0.15); }

.sidebar-item {
    background: transparent;
    color: white;
    border: none;
    border-bottom: 1px solid rgba(255,255,255,0.1);
    padding: 0.6rem 0.25rem;
    font-size: 0.8rem;
    font-weight: 500;
    line-height: 1.2;
    text-align: center;
    cursor: pointer;
    flex-shrink: 0;
    min-height: 42px;
    display: flex;
    align-items: center;
    justify-content: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: clip;
}

.sidebar-item:hover, .sidebar-item.active {
    background: rgba(255,255,255,0.15);
    font-weight: 600;
}

#app-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
}

.top-bar-left {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-shrink: 0;
}

.avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: 2px solid rgba(255,255,255,0.6);
    background: rgba(255,255,255,0.2);
    color: white;
    font-weight: 600;
    font-size: 0.9rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

.avatar:hover { background: rgba(255,255,255,0.35); }

.user-menu {
    position: fixed;
    top: var(--topbar-h);
    left: 0;
    width: var(--sidebar-w);
    background: white;
    border: 1px solid var(--border);
    border-top: none;
    border-radius: 0 0 var(--radius) var(--radius);
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    display: none;
    flex-direction: column;
    z-index: 200;
}

.user-menu button {
    background: white;
    color: var(--text);
    border: none;
    padding: 0.75rem 1rem;
    text-align: left;
    cursor: pointer;
    font-size: 0.95rem;
    width: 100%;
}

.user-menu button:first-child { border-radius: var(--radius) var(--radius) 0 0; }
.user-menu button:last-child { border-radius: 0 0 var(--radius) var(--radius); }
.user-menu button:hover { background: var(--bg); }

#global-search {
    width: 200px;
    margin: 0;
    padding: 0.45rem 0.6rem;
    border: none;
    border-radius: 4px;
    font-size: 0.9rem;
    background: rgba(255,255,255,0.9);
    color: var(--text);
    flex-shrink: 0;
}

.top-bar-menu {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    flex: 1;
    overflow-x: auto;
    min-width: 0;
    padding: 0 0.5rem;
}

.menu-btn {
    background: rgba(255,255,255,0.15);
    color: white;
    border: 1px solid rgba(255,255,255,0.25);
    padding: 0.35rem 0.6rem;
    border-radius: 4px;
    font-size: 0.85rem;
    cursor: pointer;
    white-space: nowrap;
}

.menu-btn:hover:not(:disabled) {
    background: white;
    color: var(--primary);
}

.menu-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.menu-btn.active {
    background: white;
    color: var(--primary);
    font-weight: 600;
}

.sidebar-toggle {
    display: none;
    background: rgba(255,255,255,0.15);
    color: white;
    border: 1px solid rgba(255,255,255,0.3);
    border-radius: 4px;
    padding: 0.35rem 0.6rem;
    font-size: 0.85rem;
    cursor: pointer;
    flex-shrink: 0;
}

.bottom-nav { display: none; }

@media (max-width: 768px) {
    #app-sidebar {
        position: fixed;
        top: 0;
        bottom: 0;
        left: 0;
        transform: translateX(-100%);
        transition: transform 0.2s ease;
        box-shadow: 2px 0 8px rgba(0,0,0,0.2);
    }
    #app-sidebar.open { transform: translateX(0); }

    .top-bar {
        flex-wrap: wrap;
        height: auto;
        min-height: var(--topbar-h);
        padding: 0.5rem 0.75rem;
    }
    .top-bar-left {
        flex: 1 1 0;
        min-width: 0;
        width: 100%;
    }
    .sidebar-toggle {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0;
    }
    #global-search {
        width: auto;
        flex: 1 1 auto;
        min-width: 0;
        margin: 0;
    }
    .top-bar-menu {
        order: 2;
        flex-basis: 100%;
        flex-wrap: wrap;
        justify-content: center;
        gap: 0.25rem;
        padding: 0.5rem 0 0;
    }
    .top-bar-menu .menu-btn {
        width: calc((100% - 0.5rem) / 3);
        flex: 1 1 auto;
        min-width: 0;
        text-align: center;
        white-space: normal;
        padding: 0.3rem 0.45rem;
        font-size: 0.8rem;
    }
}

.call-active-view,
.call-incoming-view {
    position: fixed;
    inset: 0;
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: #1a1a2e;
    color: white;
    z-index: 1002;
    padding: 1rem;
}

.call-incoming-view { z-index: 1003; }

.call-active-view.active,
.call-incoming-view.active { display: flex; }

.call-top { text-align: center; margin-bottom: 0.5rem; }
.call-type { margin: 0.25rem 0; opacity: 0.8; }
.call-status { margin: 0.25rem 0; font-size: 0.95rem; opacity: 0.9; }

.call-video-wrap {
    position: relative;
    width: 100%;
    max-width: 900px;
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 0;
    margin: 0.5rem 0;
}

#remote-video {
    width: 100%;
    height: 100%;
    max-height: 70vh;
    object-fit: cover;
    background: #000;
    border-radius: var(--radius);
    display: none;
}

#local-video {
    position: absolute;
    bottom: 1rem;
    right: 1rem;
    width: 140px;
    height: 180px;
    object-fit: cover;
    background: #000;
    border: 2px solid white;
    border-radius: 8px;
    display: none;
    z-index: 10;
}

.call-actions {
    display: flex;
    justify-content: center;
    gap: 1rem;
    padding: 0.75rem 0;
}

.call-end-btn {
    width: 72px;
    height: 72px;
    border-radius: 50%;
    border: none;
    background: var(--danger);
    color: white;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 4px 10px rgba(0,0,0,0.3);
}
.call-end-btn:hover { background: #bd2130; }

.call-incoming-card {
    background: rgba(0,0,0,0.35);
    border-radius: var(--radius);
    padding: 2rem;
    text-align: center;
    width: 100%;
    max-width: 360px;
}
.call-incoming-actions {
    display: flex;
    justify-content: center;
    gap: 1.5rem;
    margin-top: 1.5rem;
}
.call-accept-btn,
.call-reject-btn {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    border: none;
    color: white;
    font-weight: 600;
    cursor: pointer;
}
.call-accept-btn { background: var(--success); }
.call-accept-btn:hover { background: #1e7e34; }
.call-reject-btn { background: var(--danger); }
.call-reject-btn:hover { background: #bd2130; }

@media (max-width: 768px) {
    #local-video { width: 100px; height: 130px; bottom: 0.5rem; right: 0.5rem; }
    .call-incoming-actions { gap: 1rem; }
}

.contact-card {
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
    cursor: pointer;
}

#contact-actions-modal h3 {
    margin-bottom: 1rem;
}

#contact-actions-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

#contact-actions-list .btn {
    margin-bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

#contact-actions-close {
    margin-top: 1rem;
}
