:root {
    --bg: #f3f6fb;
    --card: #ffffff;
    --surface: #f8fafc;
    --text: #1f2937;
    --muted: #6b7280;
    --primary: #2563eb;
    --secondary: #10b981;
    --border: #d1d5db;
    --danger: #dc2626;
    --input-bg: #f8fafc;
    --message-you: #dbeafe;
    --card-shadow: rgba(15, 23, 42, 0.08);
}

* {
    box-sizing: border-box;
}

html, body {
    height: 100%;
}

body {
    margin: 0;
    font-family: Inter, system-ui, sans-serif;
    background: radial-gradient(circle at top, #e0efff, var(--bg));
    color: var(--text);
    min-height: 100%;
}

.app-shell {
    max-width: 1200px;
    margin: 0 auto;
    padding: 24px;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 20px;
}

.app-header h1 {
    margin: 0;
    font-size: 28px;
}

.subtitle {
    margin: 8px 0 0;
    color: var(--muted);
}

.header-right {
    display: flex;
    align-items: center;
    gap: 10px;
}

.hidden {
    display: none !important;
}

.card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 18px;
    padding: 20px;
    box-shadow: 0 14px 34px var(--card-shadow);
}

.tabs {
    display: flex;
    gap: 8px;
    margin-bottom: 20px;
}

.tab {
    flex: 1;
    padding: 12px 16px;
    border: 1px solid var(--border);
    background: transparent;
    border-radius: 12px;
    cursor: pointer;
    color: var(--muted);
}

.tab.active {
    background: var(--primary);
    color: white;
    border-color: transparent;
}

.auth-form {
    display: none;
    flex-direction: column;
    gap: 12px;
}

.auth-form.active {
    display: flex;
}

label {
    display: block;
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 4px;
}

input[type="text"],
input[type="password"] {
    width: 100%;
    padding: 12px 14px;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: #ffffff;
    color: #0b1220;
    font-size: 15px;
}

input::placeholder,
textarea::placeholder {
    color: #9ca3af;
}

button {
    border: none;
    border-radius: 12px;
    cursor: pointer;
    font-weight: 700;
}

.primary-button {
    background: var(--primary);
    color: white;
    padding: 12px 16px;
}

.secondary-button {
    background: var(--secondary);
    color: white;
    padding: 10px 14px;
    margin-right: 10px;
}

/* Toolbar action menu */
.action-menu {
    display: inline-block;
}
.toolbar-menu {
    position: absolute;
    right: 0;
    left: auto;
    top: calc(100% + 8px);
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.12);
    padding: 8px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-width: 160px;
    max-width: 320px;
    z-index: 100;
}
.toolbar-menu .menu-item {
    background: transparent;
    border: none;
    text-align: left;
    padding: 8px 12px;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 700;
    color: var(--text);
}
.toolbar-menu .menu-item:hover {
    background: var(--surface);
}

@media (max-width: 640px) {
    /* On small screens, ensure the menu appears below the button and aligns to the left edge so it's visible */
    .toolbar-menu {
        left: 0;
        right: auto;
        top: calc(100% + 8px);
        min-width: 180px;
        width: auto;
        margin-left: 0;
    }
    /* Expand the menu a little to be easily tappable */
    .toolbar-menu .menu-item {
        padding: 12px 14px;
        font-size: 16px;
    }
}
.status {
    margin-top: 16px;
    padding: 12px 14px;
    border-radius: 12px;
    font-weight: 600;
}

#main-status {
    display: none !important;
}

.group-members {
    display: grid;
    gap: 10px;
    margin-top: 12px;
}

.group-members-title {
    font-weight: 700;
}

.group-members-list {
    max-height: 200px;
    overflow-y: auto;
    padding: 10px;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: var(--surface);
}

.group-member-item {
    display: flex;
    align-items: center;
    gap: 10px;
}

.group-actions {
    margin-top: 16px;
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

#add-contact-panel {
    margin-top: 20px;
}

#group-creation-panel {
    margin-top: 20px;
}

#add-contact-panel input {
    width: 100%;
    padding: 12px 14px;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: #ffffff;
    color: #0b1220;
    font-size: 15px;
    margin-top: 8px;
}

/* Ensure these panels use the card background so expanding areas match dark mode */
#add-contact-panel,
#group-creation-panel {
    background: var(--card);
    border: 1px solid var(--border);
}

.status.info {
    background: #eff6ff;
    color: #1d4ed8;
}

.status.success {
    background: #ecfdf5;
    color: #047857;
}

.status.error {
    background: #fee2e2;
    color: var(--danger);
}

.chat-layout {
    display: grid;
    grid-template-columns: 320px 1fr;
    gap: 20px;
    margin-top: 20px;
    min-height: calc(100vh - 220px);
    height: calc(100vh - 220px);
}

.sidebar {
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.chat-card {
    display: flex;
    flex-direction: column;
    padding: 0;
    height: 100%;
    min-height: 0;
}

.sidebar-title {
    font-size: 16px;
    font-weight: 700;
    margin-bottom: 14px;
}

.contact-list {
    list-style: none;
    padding: 0;
    margin: 0;
    display: grid;
    gap: 10px;
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    grid-auto-rows: max-content;
}

.contact-item {
    padding: 14px 16px;
    border-radius: 14px;
    border: 1px solid var(--border);
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.contact-item.active {
    background: #e0efff;
    border-color: var(--primary);
}

/* In dark mode the active contact background remains light — ensure label text stays readable */
.dark-mode .contact-item.active {
    /* Ensure the label text and status are readable against the light active background */
    color: inherit;
}
.dark-mode .contact-item.active > span:first-child {
    color: #0b1220;
}
.dark-mode .contact-item.active .type {
    color: #0b1220;
}

/* Tone down status panels in dark mode so they don't contrast harshly */
.dark-mode .status {
    background: rgba(255, 255, 255, 0.03);
    color: var(--muted);
    border: 1px solid rgba(255, 255, 255, 0.04);
}

.contact-item .type {
    color: var(--muted);
    font-size: 12px;
}

.chat-header {
    padding: 18px 22px;
    border-bottom: 1px solid var(--border);
}

.header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
}

.chat-header h2 {
    margin: 0;
    font-size: 18px;
    flex: 1;
}

.see-members-button {
    background: var(--secondary);
    color: white;
    padding: 8px 16px;
    border-radius: 8px;
    border: none;
    cursor: pointer;
    font-weight: 600;
    font-size: 14px;
    transition: background 0.2s;
}

.see-members-button:hover:not(.hidden) {
    opacity: 0.9;
}

.load-more-button {
    width: 100%;
    margin: 0 0 12px;
    padding: 10px 14px;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-weight: 700;
    cursor: pointer;
}

.load-more-button:hover {
    background: #eff6ff;
}

.load-more-button:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.modal.hidden {
    display: none !important;
}

.modal-content {
    background: var(--card);
    border-radius: 16px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    max-width: 400px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 24px;
    border-bottom: 1px solid var(--border);
}

.modal-header h3 {
    margin: 0;
    font-size: 18px;
}

.modal-close {
    background: transparent;
    border: none;
    font-size: 24px;
    cursor: pointer;
    color: var(--muted);
    padding: 0;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-close:hover {
    color: var(--text);
}

.members-list {
    padding: 16px 24px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    max-height: 400px;
    overflow-y: auto;
}

.member-item {
    padding: 12px 16px;
    background: var(--surface);
    border-radius: 10px;
    border: 1px solid var(--border);
    font-weight: 500;
}

.message-list {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
    display: grid;
    gap: 10px;
    grid-auto-rows: max-content;
}

.load-more-wrapper {
    display: flex;
    justify-content: center;
    padding: 8px 0;
    position: sticky;
    top: 0;
    z-index: 2;
    background: transparent;
}

.message-separator {
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--muted);
    font-size: 12px;
    margin: 10px 0;
    padding: 6px 0;
    position: relative;
}

.message-separator::before,
.message-separator::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--border);
    margin: 0 10px;
}

.message-separator::before {
    margin-left: 0;
}

.message-separator::after {
    margin-right: 0;
}

.message {
    padding: 14px 16px;
    border-radius: 16px;
    background: var(--surface);
    border: 1px solid var(--border);
    max-width: 80%;
    
    white-space: pre-wrap;      
    overflow-wrap: break-word;  
    word-break: break-word;     
}

.message.you {
    background: var(--message-you);
    margin-left: auto;
}

.message-header {
    font-size: 12px;
    color: var(--muted);
    margin-bottom: 6px;
}

.composer {
    padding: 20px;
    border-top: 1px solid var(--border);
    display: grid;
    grid-template-columns: auto 1fr auto;
    gap: 12px;
    align-items: center;
}

.composer input[type="text"] {
    width: 100%;
    padding: 14px 16px;
    border-radius: 14px;
    border: 1px solid var(--border);
    background: #ffffff;
    color: #0b1220;
    font-size: 15px;
}

.theme-toggle {
    margin-top: 20px;
    display: flex;
    align-items: center;
    gap: 12px;
}




.mode-switch {
    position: relative;
    width: 54px;
    height: 28px;
    border-radius: 999px;
    border: none;
    background: var(--border);
    cursor: pointer;
    transition: background 0.3s ease;
}

.mode-switch .switch-knob {
    position: absolute;
    top: 3px;
    left: 3px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: white;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.12);
    transition: transform 0.25s ease;
    transform: translateX(26px);
}

.mode-switch.dark {
    background: #4b5563;
}

.mode-switch.dark .switch-knob {
    transform: translateX(3px);
}

.theme-mode-text {
    font-size: 14px;
    color: var(--muted);
}

.dark-mode {
    --bg: #121827;
    --card: #1f2937;
    --surface: #111827;
    --text: #f8fafc;
    --muted: #94a3b8;
    --primary: #60a5fa;
    --secondary: #34d399;
    --border: #374151;
    --danger: #f87171;
    --input-bg: #1f2937;
    --message-you: #2563eb;
    --card-shadow: rgba(0, 0, 0, 0.45);
}

/* Use a solid dark background in dark mode to avoid radial/light artifacts when content grows */
body.dark-mode {
    background: var(--bg);
}

/* ==================== MOBILITY & RESPONSIVE ARCHITECTURE ==================== */

@media (max-width: 768px) {
    /* Flatten the 2-column layout into a single, full-screen slot stack */
    .chat-layout {
        grid-template-columns: 1fr;
        height: calc(100vh - 180px);
        min-height: calc(100vh - 180px);
        position: relative;
        overflow: hidden;
    }

    /* Set up both screens to scale dynamically to 100% of the visible viewport area */
    .sidebar, .chat-card {
        width: 100%;
        height: 100%;
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }

    /* SCENARIO A: No active conversation is open (Show list, hide chat off-screen) */
    .chat-layout:not(.chat-selected) .sidebar {
        display: flex;
    }
    .chat-layout:not(.chat-selected) .chat-card {
        display: none;
    }

    /* SCENARIO B: A conversation is open (Hide list off-screen, display active chat room) */
    .chat-layout.chat-selected .sidebar {
        display: none;
    }
    .chat-layout.chat-selected .chat-card {
        display: flex;
    }

    /* Structural cleanup for header elements */
    .app-shell {
        padding: 12px;
    }
    .app-header {
        flex-wrap: wrap;
        gap: 8px;
        margin-bottom: 12px;
    }
    .app-header h1 {
        font-size: 22px;
    }
    .header-right {
        font-size: 13px;
    }
    
    /* Ensure messages expand nicely on compact displays */
    .message {
        max-width: 90%;
        padding: 10px 12px;
    }
}
@media (max-width: 768px) {
    #mobile-back-button {
        display: inline-block !important;
    }
}
/* Extra small layout fixes for utilities and wrappers */
@media (max-width: 480px) {
    .panel.toolbar {
        display: flex;
        gap: 8px;
    }
    .panel.toolbar button {
        flex: 1;
        margin-right: 0;
        font-size: 13px;
        padding: 8px 10px;
    }
}
#receipts-modal {
    z-index: 1001 !important;
}

#members-modal {
    z-index: 1000 !important;
}

#attach-button {
    width: 40px;
    min-width: 40px;
    height: 40px;

    padding: 0;
    flex: 0 0 auto;

    font-size: 20px;
}

.pdf-modal-content {
    width: 95vw;
    max-width: 1200px;
    height: 92vh;
    max-height: 92vh;
    display: flex;
    flex-direction: column;
}

#pdf-frame {
    flex: 1;
    width: 100%;
    border: none;
}


.image-modal-content {
    width: 95vw;
    max-width: 1200px;
    height: 92vh;
    max-height: 92vh;
    display: flex;
    flex-direction: column;
}

.image-modal-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

#image-viewer-body {
    flex: 1;
    overflow: auto;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #111827;
}

#image-viewer-img {
    max-width: 100%;
    max-height: 100%;
    transform-origin: center center;
    transition: transform 0.1s ease;
}

#image-download {
    text-decoration: none;
}

.contact-options-button {
    background: transparent;
    color: var(--muted);
    font-size: 20px;
    padding: 4px 8px;
    border-radius: 8px;
}

.contact-options-button:hover {
    background: var(--surface);
    color: var(--text);
}

.contact-options-menu {
    position: absolute;
    right: 0;
    top: calc(100% + 6px);
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.15);
    padding: 8px;
    min-width: 180px;
    z-index: 200;
}

.contact-options-menu-item {
    width: 100%;
    background: transparent;
    color: var(--text);
    text-align: left;
    padding: 10px 12px;
    border-radius: 8px;
    font-size: 14px;
}

.contact-options-menu-item:hover {
    background: var(--surface);
}

.contact-options-menu-item.danger {
    color: var(--danger);
}

.btn-danger {
    background: var(--danger);
    color: white;
    padding: 10px 14px;
}


/* --- App Loading Screen Styles --- */
#app-loading-screen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background-color: var(--background, #ffffff); /* Fallback to white if variables aren't loaded */
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 9999; /* Sit on top of all modals and shells */
    transition: opacity 0.25s ease-in-out;
}

/* Ensure the utility hidden class works on the loading screen */
#app-loading-screen.hidden {
    display: none !important;
    opacity: 0;
    pointer-events: none;
}

.spinner-container {
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
}

.loading-spinner {
    width: 50px;
    height: 50px;
    border: 5px solid var(--border, #e2e8f0);
    border-top: 5px solid var(--primary, #007bff); 
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

.loading-text {
    font-family: inherit;
    font-size: 16px;
    font-weight: 500;
    color: var(--text, #333333);
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

#settings-menu-options {
    display: flex;
    flex-direction: column;
    gap: 14px;
    padding: 20px;
}

.theme-settings-row {
    display: flex;
    align-items: center;
    justify-content: space-between;

    width: 100%;
    min-height: 52px;

    padding: 12px 16px;

    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
}

#settings-menu-options button:not(.mode-switch) {
    width: 100%;
    min-height: 52px;

    padding: 12px 16px;

    border-radius: 12px;
}

#settings-modal .modal-content {
    max-width: 460px;
}
.modal-close-btn {
    background: transparent;
    border: none;
    font-size: 24px;
    cursor: pointer;
    color: var(--muted);
    padding: 0;
    width: 32px;
    height: 32px;
}