/* Layout CSS - Two-pane grid layout */

.app-container {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100vh; /* fallback for browsers without dynamic viewport units */
    height: 100dvh;
    max-height: 100dvh;
    overflow: hidden;
}

/* Header */
.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1rem;
    background: var(--bg-primary);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.logo {
    font-size: 1.5rem;
}

.app-header h1 {
    font-size: 1.25rem;
    color: var(--text-primary);
}

.header-right {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

/* Theme toggle button */
#theme-toggle {
    position: relative;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
    transition: background-color 0.2s;
}

#theme-toggle:hover {
    background: var(--bg-hover);
}

.theme-icon-light,
.theme-icon-dark {
    position: absolute;
    transition: opacity 0.2s, transform 0.2s;
}

[data-theme="light"] .theme-icon-dark {
    opacity: 0;
    transform: rotate(-90deg);
}

[data-theme="dark"] .theme-icon-light {
    opacity: 0;
    transform: rotate(90deg);
}

/* Main content area */
.app-content {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* Sidebar */
.sidebar {
    display: flex;
    flex-direction: column;
    width: 320px;
    min-width: 320px;
    background: var(--bg-primary);
    border-right: 1px solid var(--border);
    flex-shrink: 0;
}

.sidebar-header {
    padding: 1rem;
    border-bottom: 1px solid var(--border);
}

.search-container {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
}

.search-input {
    width: 100%;
    padding: 0.625rem 0.875rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text-primary);
    font-size: 0.9rem;
    transition: border-color 0.2s;
}

.search-input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-light);
}

.search-input::placeholder {
    color: var(--text-muted);
}

.conversations-list {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
    padding: 0.5rem;
}

/* Chat area */
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--bg-primary);
    overflow: hidden;
    position: relative;
}

/* Empty state */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    padding: 2rem;
    text-align: center;
    color: var(--text-secondary);
}

.empty-state-icon {
    font-size: 4rem;
    margin-bottom: 1rem;
    opacity: 0.5;
}

.empty-state h2 {
    font-size: 1.5rem;
    margin-bottom: 0.5rem;
    color: var(--text-primary);
}

.empty-state p {
    margin-bottom: 1.5rem;
    max-width: 300px;
}

/* Active chat */
.active-chat {
    display: flex;
    flex-direction: column;
    height: 100%;
}

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

.chat-header-info {
    display: flex;
    flex-direction: column;
}

.participant-name {
    font-weight: 600;
    color: var(--text-primary);
}

/* Messages container */
.messages-container {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
    padding: 1rem;
    display: flex;
    flex-direction: column;
}

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

.loading-indicator {
    text-align: center;
    padding: 1rem;
    color: var(--text-muted);
    font-size: 0.875rem;
}

/* Composer */
.composer {
    display: flex;
    align-items: flex-end;
    gap: 0.5rem;
    padding: 1rem;
    border-top: 1px solid var(--border);
    background: var(--bg-primary);
    flex-shrink: 0;
}

.message-input {
    flex: 1;
    padding: 0.75rem 1rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 20px;
    color: var(--text-primary);
    font-size: 0.95rem;
    resize: none;
    max-height: 120px;
    line-height: 1.4;
    min-height: 44px;
}

.message-input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-light);
}

.message-input::placeholder {
    color: var(--text-muted);
}

.btn-send {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--accent);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.2s, transform 0.1s;
    flex-shrink: 0;
}

.btn-send:hover {
    opacity: 0.9;
}

.btn-send:active {
    transform: scale(0.95);
}

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

/* Modal */
.modal {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
}

.modal-content {
    position: relative;
    background: var(--bg-primary);
    border-radius: 12px;
    width: 100%;
    max-width: 450px;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow-lg);
}

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

.modal-header h2 {
    font-size: 1.125rem;
    color: var(--text-primary);
}

.modal-close {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    color: var(--text-secondary);
    transition: background-color 0.2s;
}

.modal-close:hover {
    background: var(--bg-hover);
}

.modal-body {
    padding: 1rem 1.25rem;
    overflow-y: auto;
}

.search-user-container {
    margin-bottom: 1rem;
}

/* PWA / safe-area support for notched devices and installed apps */
.app-header {
    padding-top: max(0.75rem, env(safe-area-inset-top));
    padding-left: max(1rem, env(safe-area-inset-left));
    padding-right: max(1rem, env(safe-area-inset-right));
}

.composer,
.sidebar-header {
    padding-bottom: max(1rem, env(safe-area-inset-bottom));
}

.conversations-list {
    padding-bottom: env(safe-area-inset-bottom);
}

.messages-container {
    padding-bottom: env(safe-area-inset-bottom);
}

/* Installed PWA / iOS standalone: lock app to the device screen like a native app */
html.is-standalone,
html.is-standalone body {
    width: 100vw;
    height: 100dvh;
    overflow: hidden;
    overscroll-behavior: none;
}

html.is-standalone body {
    touch-action: none;
}

html.is-standalone .app-container {
    position: fixed;
    inset: 0;
    width: 100vw;
    min-width: 100vw;
    max-width: 100vw;
    height: 100dvh;
    min-height: 100dvh;
    max-height: 100dvh;
    overflow: hidden;
}

html.is-standalone .app-header {
    padding-top: env(safe-area-inset-top);
    padding-left: max(1rem, env(safe-area-inset-left));
    padding-right: max(1rem, env(safe-area-inset-right));
    min-height: calc(3rem + env(safe-area-inset-top));
}

html.is-standalone .composer,
html.is-standalone .sidebar-header {
    padding-bottom: max(1rem, env(safe-area-inset-bottom));
    padding-left: max(1rem, env(safe-area-inset-left));
    padding-right: max(1rem, env(safe-area-inset-right));
}

html.is-standalone .conversations-list,
html.is-standalone .messages-container {
    padding-bottom: env(safe-area-inset-bottom);
}

html.is-standalone .modal-content {
    max-height: 80dvh;
}

/* Prevent iOS auto-zoom on form fields in installed PWAs. */
html.is-standalone input,
html.is-standalone textarea,
html.is-standalone select {
    font-size: 16px;
}

/* Keep the CSS-only media query block in sync with the html.is-standalone block above. */
@media (display-mode: standalone) {
    html,
    html body {
        width: 100vw;
        height: 100dvh;
        overflow: hidden;
        overscroll-behavior: none;
    }

    html body {
        touch-action: none;
    }

    .app-container {
        position: fixed;
        inset: 0;
        width: 100vw;
        min-width: 100vw;
        max-width: 100vw;
        height: 100dvh;
        min-height: 100dvh;
        max-height: 100dvh;
        overflow: hidden;
    }

    .app-header {
        padding-top: env(safe-area-inset-top);
        padding-left: max(1rem, env(safe-area-inset-left));
        padding-right: max(1rem, env(safe-area-inset-right));
        min-height: calc(3rem + env(safe-area-inset-top));
    }

    .composer,
    .sidebar-header {
        padding-bottom: max(1rem, env(safe-area-inset-bottom));
        padding-left: max(1rem, env(safe-area-inset-left));
        padding-right: max(1rem, env(safe-area-inset-right));
    }

    .conversations-list,
    .messages-container {
        padding-bottom: env(safe-area-inset-bottom);
    }

    .modal-content {
        max-height: 80dvh;
    }

    /* Prevent iOS auto-zoom on form fields in installed PWAs. */
    input,
    textarea,
    select {
        font-size: 16px;
    }
}

/* Installed PWA auth pages also fill the screen */
html.is-standalone .auth-container {
    width: 100vw;
    min-height: 100dvh;
    overflow-y: auto;
    touch-action: pan-y;
    overscroll-behavior: contain;
}

@media (display-mode: standalone) {
    .auth-container {
        width: 100vw;
        min-height: 100dvh;
        overflow-y: auto;
        touch-action: pan-y;
        overscroll-behavior: contain;
    }
}

.search-results {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.search-result-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem;
    border-radius: 8px;
    cursor: pointer;
    transition: background-color 0.2s;
}

.search-result-item:hover {
    background: var(--bg-hover);
}

.search-result-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--accent);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 1rem;
    flex-shrink: 0;
}

.search-result-info {
    flex: 1;
    min-width: 0;
}

.search-result-name {
    font-weight: 500;
    color: var(--text-primary);
}

.search-result-handle {
    font-size: 0.8rem;
    color: var(--text-muted);
}

/* Mobile responsive */
@media (max-width: 720px) {
    .sidebar {
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        z-index: 100;
        transform: translateX(-100%);
        transition: transform 0.3s ease;
        box-shadow: var(--shadow-lg);
    }

    .sidebar.show {
        transform: translateX(0);
    }

    .chat-area::before {
        content: '←';
        display: none;
        position: absolute;
        left: 1rem;
        top: 1rem;
        z-index: 50;
        width: 36px;
        height: 36px;
        background: var(--bg-primary);
        border: 1px solid var(--border);
        border-radius: 50%;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        font-size: 1.25rem;
        color: var(--text-primary);
    }

    .chat-area.has-active::before {
        display: flex;
    }

    /* Prevent iOS auto-zoom on form fields on small screens. */
    input,
    textarea,
    select {
        font-size: 16px;
    }
}
