/* =========================================================
   PTL - WhatsApp Style Modern Dark Theme CSS (Mobile & Chrome Viewport Optimized)
   ========================================================= */

:root {
    --bg-main: #0b141a;
    --bg-panel: #111b21;
    --bg-header: #202c33;
    --bg-hover: #222e35;
    --bg-active: #2a3942;
    --bg-input: #2a3942;
    --bg-bubble-in: #202c33;
    --bg-bubble-out: #005c4b;
    --text-primary: #e9edef;
    --text-secondary: #8696a0;
    --accent-green: #00a884;
    --accent-green-hover: #008069;
    --danger-red: #ea868f;
    --border-color: #222d34;
    --font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, Roboto, sans-serif;
    --shadow-soft: 0 4px 20px rgba(0, 0, 0, 0.4);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    height: 100%;
    height: 100dvh;
    font-family: var(--font-family);
    background-color: var(--bg-main);
    color: var(--text-primary);
    overflow: hidden;
}

body {
    display: flex;
}

/* App Container */
#app {
    width: 100%;
    height: 100%;
    height: 100dvh;
    display: flex;
    position: relative;
    overflow: hidden;
}

/* Common Components */
.btn {
    background-color: var(--accent-green);
    color: #fff;
    border: none;
    padding: 10px 18px;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.2s ease, transform 0.1s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    touch-action: manipulation;
}

.btn:hover {
    background-color: var(--accent-green-hover);
}

.btn:active {
    transform: scale(0.98);
}

.btn-secondary {
    background-color: #3b4a54;
    color: var(--text-primary);
}

.btn-secondary:hover {
    background-color: #4a5c68;
}

.btn-danger {
    background-color: #842029;
    color: #f8d7da;
}

.btn-danger:hover {
    background-color: #b02a37;
}

.form-group {
    margin-bottom: 16px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.form-group label {
    font-size: 13px;
    color: var(--text-secondary);
}

.form-control {
    background-color: var(--bg-input);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 12px 14px;
    border-radius: 8px;
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s ease;
}

.form-control:focus {
    border-color: var(--accent-green);
}

/* Alert Boxes */
.alert {
    padding: 12px 16px;
    border-radius: 8px;
    font-size: 13px;
    margin-bottom: 16px;
}

.alert-danger {
    background-color: rgba(234, 134, 143, 0.15);
    color: var(--danger-red);
    border: 1px solid rgba(234, 134, 143, 0.3);
}

.alert-success {
    background-color: rgba(0, 168, 132, 0.15);
    color: var(--accent-green);
    border: 1px solid rgba(0, 168, 132, 0.3);
}

/* Login View */
.login-container {
    width: 100%;
    height: 100%;
    height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: radial-gradient(circle at top, #111b21 0%, #0b141a 100%);
    padding: 16px;
}

.login-card {
    background-color: var(--bg-panel);
    width: 100%;
    max-width: 420px;
    padding: 36px 30px;
    border-radius: 16px;
    box-shadow: var(--shadow-soft);
    border: 1px solid var(--border-color);
    text-align: center;
}

.login-brand {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    margin-bottom: 24px;
}

.login-brand svg {
    fill: var(--accent-green);
    width: 44px;
    height: 44px;
}

.login-brand h1 {
    font-size: 26px;
    font-weight: 700;
    color: var(--text-primary);
    letter-spacing: 0.5px;
}

.security-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background-color: rgba(0, 168, 132, 0.1);
    color: var(--accent-green);
    font-size: 12px;
    padding: 6px 12px;
    border-radius: 20px;
    margin-bottom: 20px;
}

/* Chat Main Layout */
.chat-layout {
    display: flex;
    width: 100%;
    height: 100%;
    height: 100dvh;
    overflow: hidden;
}

/* Sidebar */
.sidebar {
    width: 380px;
    min-width: 320px;
    background-color: var(--bg-panel);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    height: 100%;
    height: 100dvh;
}

.sidebar-header {
    height: 60px;
    min-height: 60px;
    background-color: var(--bg-header);
    padding: 10px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}

.user-profile-info {
    display: flex;
    align-items: center;
    gap: 10px;
}

.avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background-color: var(--accent-green);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    font-size: 16px;
    text-transform: uppercase;
    flex-shrink: 0;
}

.user-name {
    font-size: 15px;
    font-weight: 600;
}

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

.icon-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 8px;
    border-radius: 50%;
    transition: background-color 0.2s, color 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: manipulation;
    flex-shrink: 0;
}

.icon-btn:hover {
    background-color: rgba(255, 255, 255, 0.08);
    color: var(--text-primary);
}

.mobile-back-btn {
    display: none;
}

.sidebar-search {
    padding: 10px 14px;
    background-color: var(--bg-panel);
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}

.search-input-wrapper {
    position: relative;
    display: flex;
    align-items: center;
}

.search-input-wrapper input {
    width: 100%;
    background-color: var(--bg-input);
    border: none;
    border-radius: 8px;
    padding: 8px 12px 8px 36px;
    color: var(--text-primary);
    font-size: 14px;
    outline: none;
}

.search-input-wrapper svg {
    position: absolute;
    left: 10px;
    fill: var(--text-secondary);
    width: 16px;
    height: 16px;
}

/* Contacts / Conversations List */
.conversations-list {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.conv-item {
    display: flex;
    align-items: center;
    padding: 12px 16px;
    gap: 12px;
    cursor: pointer;
    border-bottom: 1px solid rgba(255, 255, 255, 0.03);
    transition: background-color 0.15s ease;
    touch-action: manipulation;
}

.conv-item:hover {
    background-color: var(--bg-hover);
}

.conv-item.active {
    background-color: var(--bg-active);
}

.conv-details {
    flex: 1;
    overflow: hidden;
}

.conv-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 4px;
}

.conv-title {
    font-size: 15px;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.conv-time {
    font-size: 12px;
    color: var(--text-secondary);
}

.conv-bottom {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.conv-last-msg {
    font-size: 13px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Main Chat Area */
.chat-window {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
    height: 100dvh;
    background-color: var(--bg-main);
    position: relative;
    overflow: hidden;
}

.chat-header {
    height: 60px;
    min-height: 60px;
    background-color: var(--bg-header);
    padding: 10px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}

.chat-header-info {
    display: flex;
    align-items: center;
    gap: 12px;
}

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

.e2e-notice-bar {
    background-color: #182229;
    color: #ffd279;
    font-size: 12px;
    padding: 8px 16px;
    text-align: center;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    flex-shrink: 0;
}

.messages-container {
    flex: 1;
    min-height: 0; /* Ensures flex container shrinks so chat-input-bar stays visible */
    padding: 20px 40px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    display: flex;
    flex-direction: column;
    gap: 10px;
    background: radial-gradient(circle at center, #111b21 0%, #0b141a 100%);
}

/* Message Bubbles */
.message-bubble {
    max-width: 65%;
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 14.5px;
    line-height: 1.4;
    position: relative;
    word-break: break-word;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.message-bubble.incoming {
    align-self: flex-start;
    background-color: var(--bg-bubble-in);
    border-top-left-radius: 0;
}

.message-bubble.outgoing {
    align-self: flex-end;
    background-color: var(--bg-bubble-out);
    border-top-right-radius: 0;
}

.message-sender {
    font-size: 12px;
    font-weight: 600;
    color: var(--accent-green);
    margin-bottom: 3px;
}

.message-meta {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 6px;
    font-size: 11px;
    color: rgba(241, 241, 241, 0.6);
    margin-top: 4px;
}

.message-actions-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 14px;
    padding: 2px 4px;
    border-radius: 4px;
    opacity: 0;
    transition: opacity 0.15s ease;
}

.message-bubble:hover .message-actions-btn {
    opacity: 1;
}

.message-deleted-text {
    font-style: italic;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13.5px;
}

/* Attachment Boxes in Chat */
.message-attachment-box {
    display: flex;
    align-items: center;
    gap: 10px;
    background-color: rgba(0, 0, 0, 0.2);
    padding: 10px 14px;
    border-radius: 6px;
    margin-bottom: 6px;
    cursor: pointer;
    border: 1px solid rgba(255, 255, 255, 0.08);
    transition: background-color 0.2s ease;
}

.message-attachment-box:hover {
    background-color: rgba(0, 0, 0, 0.35);
}

.message-attachment-icon {
    font-size: 20px;
}

.message-attachment-details {
    flex: 1;
    overflow: hidden;
}

.message-attachment-name {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.message-attachment-status {
    font-size: 11px;
    color: var(--accent-green);
}

/* Attachment Preview Bar */
.attachment-preview-bar {
    background-color: var(--bg-header);
    border-top: 1px solid var(--border-color);
    padding: 8px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 13px;
    color: var(--accent-green);
    flex-shrink: 0;
}

/* Emoji Picker Popup */
.emoji-picker-popup {
    position: absolute;
    bottom: 70px;
    left: 20px;
    width: 320px;
    height: 280px;
    background-color: var(--bg-panel);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    box-shadow: var(--shadow-soft);
    display: flex;
    flex-direction: column;
    z-index: 100;
    overflow: hidden;
}

.emoji-tabs {
    display: flex;
    background-color: var(--bg-header);
    border-bottom: 1px solid var(--border-color);
    padding: 4px;
    gap: 4px;
    overflow-x: auto;
}

.emoji-tab {
    background: none;
    border: none;
    font-size: 18px;
    padding: 6px 8px;
    border-radius: 6px;
    cursor: pointer;
    transition: background-color 0.2s;
}

.emoji-tab.active, .emoji-tab:hover {
    background-color: var(--bg-active);
}

.emoji-grid {
    flex: 1;
    padding: 10px;
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 6px;
    overflow-y: auto;
}

.emoji-item {
    font-size: 22px;
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px;
    border-radius: 6px;
    transition: transform 0.1s, background-color 0.15s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.emoji-item:hover {
    background-color: var(--bg-hover);
    transform: scale(1.2);
}

/* Chat Input Bar (Always Forced Visible at Bottom) */
.chat-input-bar {
    min-height: 60px;
    background-color: var(--bg-header);
    padding: 10px 16px;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
    display: flex;
    align-items: center;
    gap: 10px;
    border-top: 1px solid var(--border-color);
    flex-shrink: 0; /* Guarantee input bar NEVER gets squished off-screen */
    z-index: 20;
}

.chat-input-bar input[type="text"] {
    flex: 1;
    min-width: 0;
    background-color: var(--bg-input);
    border: none;
    border-radius: 8px;
    padding: 12px 16px;
    color: var(--text-primary);
    font-size: 14.5px;
    outline: none;
}

.send-btn {
    background-color: var(--accent-green);
    border: none;
    color: #fff;
    width: 42px;
    height: 42px;
    min-width: 42px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.2s;
    touch-action: manipulation;
    flex-shrink: 0;
}

.send-btn:hover {
    background-color: var(--accent-green-hover);
}

/* Empty State Window */
.empty-chat-state {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    color: var(--text-secondary);
    text-align: center;
}

.empty-chat-state svg {
    fill: #222e35;
    width: 120px;
    height: 120px;
}

/* Modal Windows */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    background-color: rgba(0, 0, 0, 0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    backdrop-filter: blur(4px);
}

.modal-content {
    background-color: var(--bg-panel);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    width: 90%;
    max-width: 750px;
    max-height: 85vh;
    max-height: 85dvh;
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow-soft);
    overflow: hidden;
}

.modal-header {
    padding: 16px 24px;
    background-color: var(--bg-header);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-title {
    font-size: 18px;
    font-weight: 600;
}

.modal-body {
    padding: 24px;
    overflow-y: auto;
}

.modal-footer {
    padding: 16px 24px;
    background-color: var(--bg-header);
    border-top: 1px solid var(--border-color);
    display: flex;
    justify-content: flex-end;
    gap: 12px;
}

/* Admin Table */
.admin-table-wrapper {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.admin-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13.5px;
    text-align: left;
}

.admin-table th, .admin-table td {
    padding: 12px 14px;
    border-bottom: 1px solid var(--border-color);
}

.admin-table th {
    background-color: var(--bg-header);
    color: var(--text-secondary);
    font-weight: 600;
}

.admin-table tr:hover {
    background-color: var(--bg-hover);
}

/* Badges */
.badge {
    display: inline-block;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
}

.badge-admin {
    background-color: rgba(234, 134, 143, 0.2);
    color: var(--danger-red);
}

.badge-user {
    background-color: rgba(134, 150, 160, 0.2);
    color: var(--text-secondary);
}

.badge-active {
    background-color: rgba(0, 168, 132, 0.2);
    color: var(--accent-green);
}

.badge-inactive {
    background-color: rgba(255, 193, 7, 0.2);
    color: #ffc107;
}

.hidden {
    display: none !important;
}

/* =========================================================
   Mobile Optimization (@media <= 768px)
   ========================================================= */
@media (max-width: 768px) {
    /* Auto-zoom fix & sizing for mobile inputs */
    .form-control, 
    .search-input-wrapper input {
        font-size: 16px !important;
    }

    .chat-input-bar {
        padding: 8px 10px;
        padding-bottom: max(8px, env(safe-area-inset-bottom));
        gap: 6px;
        min-height: 56px;
    }

    .chat-input-bar input[type="text"] {
        font-size: 16px !important;
        padding: 10px 12px;
        min-width: 0;
        flex: 1;
    }

    .chat-input-bar .icon-btn {
        padding: 6px;
    }

    .send-btn {
        width: 38px;
        height: 38px;
        min-width: 38px;
    }

    /* Layout Toggle: Default shows sidebar only */
    .sidebar {
        width: 100%;
        min-width: 100%;
        border-right: none;
    }

    .chat-window {
        display: none;
        width: 100%;
    }

    /* When active chat is selected on mobile */
    .chat-layout.mobile-active-chat .sidebar {
        display: none;
    }

    .chat-layout.mobile-active-chat .chat-window {
        display: flex;
        width: 100%;
        height: 100dvh;
    }

    /* Mobile Back Button */
    .mobile-back-btn {
        display: flex !important;
        margin-right: 4px;
    }

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

    .message-bubble {
        max-width: 88%;
    }

    /* Mobile Emoji Picker Overlay */
    .emoji-picker-popup {
        left: 8px;
        right: 8px;
        width: calc(100% - 16px);
        bottom: 64px;
    }

    /* Admin Form on Mobile */
    .admin-create-form {
        grid-template-columns: 1fr !important;
    }

    .admin-submit-wrapper {
        grid-column: span 1 !important;
    }
}
