/*
   HIGEXT NEXA — Mobile & Responsive v7.0 "Obsidian Glass"
   Full-viewport containment: every device, every screen, no overflow.
   Uses dvh (dynamic viewport height) with vh fallback.
*/

/* ═══════════════════════════════════════════ */
/*  Global viewport lock                       */
/* ═══════════════════════════════════════════ */

html, body, #app, .app {
    height: 100vh;
    height: 100dvh;
    max-height: 100vh;
    max-height: 100dvh;
    overflow: hidden;
}

/* ═══════════════════════════════════════════ */
/*  Large Desktop (1440px+)                    */
/* ═══════════════════════════════════════════ */

@media (min-width: 1440px) {
    .app-container {
        grid-template-columns: 68px 300px minmax(0, 1fr) 0;
    }
    .app-container:has(#membersSidebar:not(.hidden-sidebar)) {
        grid-template-columns: 68px 300px minmax(0, 1fr) 300px;
    }
    .members-sidebar:not(.hidden-sidebar) { width: 300px; }
    .messages-container { padding: var(--s-4) var(--s-6) var(--s-2); }
}

/* ═══════════════════════════════════════════ */
/*  Standard Desktop — Members sidebar hidden  */
/* ═══════════════════════════════════════════ */

@media (max-width: 1200px) {
    .app-container {
        grid-template-columns: 68px 256px minmax(0, 1fr) 0;
    }
    .app-container:has(#membersSidebar:not(.hidden-sidebar)) {
        grid-template-columns: 68px 256px minmax(0, 1fr) 0;
    }
    .members-sidebar {
        position: fixed;
        right: 0;
        top: 0;
        bottom: 0;
        width: 280px;
        z-index: calc(var(--z-overlay) + 1);
        transform: translateX(100%);
        transition: transform 0.3s var(--ease-out);
        box-shadow: none;
        border-left: 1px solid var(--border-faint);
    }
    .members-sidebar.slide-in {
        transform: translateX(0);
        box-shadow: var(--shadow-2xl);
    }
}

/* ═══════════════════════════════════════════ */
/*  Tablet & Small Laptop (1024px)             */
/* ═══════════════════════════════════════════ */

@media (max-width: 1024px) {
    .app-container {
        grid-template-columns: 68px 220px minmax(0, 1fr) 0;
    }
    .app-container:has(#membersSidebar:not(.hidden-sidebar)) {
        grid-template-columns: 68px 220px minmax(0, 1fr) 0;
    }
    .chat-header-topic { display: none; }
}

/* ═══════════════════════════════════════════ */
/*  MOBILE — Full mobile experience (768px)    */
/* ═══════════════════════════════════════════ */

@media (max-width: 768px) {
    html { font-size: 14px; }

    /* Lock viewport — dvh handles address bar */
    html, body, #app, .app, .main-app {
        height: 100vh; height: 100dvh;
        max-height: 100vh; max-height: 100dvh;
        overflow: hidden;
    }

    /* Full-width single panel layout */
    .app-container {
        grid-template-columns: 1fr;
        grid-template-rows: 1fr 60px;
        height: 100vh; height: 100dvh;
        max-height: 100vh; max-height: 100dvh;
    }

    /* Server sidebar → bottom nav bar */
    .server-sidebar {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        top: auto;
        width: 100%;
        height: 60px;
        flex-direction: row;
        align-items: center;
        justify-content: flex-start;
        padding: 0 var(--s-2);
        gap: var(--s-1);
        z-index: calc(var(--z-overlay) + 2);
        background: rgba(5, 5, 8, 0.95);
        backdrop-filter: blur(16px);
        -webkit-backdrop-filter: blur(16px);
        border-top: 1px solid var(--border-faint);
        border-right: none;
        overflow-x: auto;
        overflow-y: hidden;
        -webkit-overflow-scrolling: touch;
    }

    .server-sidebar .server-separator {
        width: 2px;
        height: 24px;
        margin: 0 var(--s-1);
    }

    .server-icon {
        width: 40px;
        height: 40px;
        font-size: 0.85rem;
        flex-shrink: 0;
        border-radius: var(--r-md);
    }

    .server-icon.active::before,
    .server-icon.has-unread::after {
        display: none;
    }

    .server-icon.active {
        outline: 2px solid var(--brand);
        outline-offset: 2px;
    }

    .server-icon[onclick*="admin.html"] { display: none; }

    /* Channels panel — slide in from left */
    .channels-panel {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 60px;
        width: 85%;
        max-width: 320px;
        height: calc(100vh - 60px); height: calc(100dvh - 60px);
        z-index: calc(var(--z-overlay) + 1);
        transform: translateX(-100%);
        transition: transform 0.3s var(--ease-out);
        box-shadow: none;
        border-right: 1px solid var(--border-faint);
        overflow: hidden;
        display: flex;
        flex-direction: column;
    }
    .channels-panel.slide-in {
        transform: translateX(0);
        box-shadow: var(--shadow-2xl);
    }

    /* Chat area — full width, contained above bottom nav */
    .chat-area {
        grid-column: 1;
        grid-row: 1;
        height: calc(100vh - 60px); height: calc(100dvh - 60px);
        max-height: calc(100vh - 60px); max-height: calc(100dvh - 60px);
        min-height: 0;
        overflow: hidden;
        display: flex;
        flex-direction: column;
    }

    /* Members sidebar — slide in from right */
    .members-sidebar {
        position: fixed;
        right: 0;
        top: 0;
        bottom: 60px;
        width: 85%;
        max-width: 300px;
        height: calc(100vh - 60px); height: calc(100dvh - 60px);
        z-index: calc(var(--z-overlay) + 1);
        transform: translateX(100%);
        transition: transform 0.3s var(--ease-out);
        box-shadow: none;
        overflow: hidden;
        display: flex;
        flex-direction: column;
    }
    .members-sidebar.slide-in {
        transform: translateX(0);
        box-shadow: var(--shadow-2xl);
    }

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

    /* Mobile overlay */
    .mobile-overlay.visible {
        display: block;
        z-index: var(--z-overlay);
    }

    /* Chat header adjustments */
    .chat-header {
        padding: 0 var(--s-3);
        flex-shrink: 0;
    }
    .chat-header-topic { display: none; }
    .chat-header-actions { gap: 0; }
    .chat-header-actions button { width: 36px; height: 36px; }

    /* Messages — fill remaining space, never overflow parent */
    .messages-container {
        padding: var(--s-3) var(--s-3) var(--s-2);
        flex: 1;
        min-height: 0;
        overflow-y: auto;
        overflow-x: hidden;
    }
    .message-group { padding: var(--s-2); gap: var(--s-2); }

    /* Message input — pinned at bottom, never push out */
    .message-input-area {
        padding: 0 var(--s-3) var(--s-3);
        flex-shrink: 0;
    }

    /* Typing indicator — don't grow */
    .typing-indicator,
    #typingIndicator {
        flex-shrink: 0;
    }

    /* Touch-friendly targets */
    .channel-item { padding: 10px 12px; min-height: 40px; }
    .member-item { padding: 8px 10px; min-height: 42px; }
    button { min-height: 36px; }
    .btn { padding: 12px 20px; }
    .btn-icon { width: 40px; height: 40px; }

    /* Modal adjustments — contained in viewport */
    .modal-content {
        min-width: unset !important;
        width: 94vw !important;
        max-width: 440px !important;
        max-height: 85vh !important; max-height: 85dvh !important;
        margin: var(--s-3);
        border-radius: var(--r-xl) !important;
    }

    /* Settings modal — stack vertically */
    .settings-layout {
        flex-direction: column !important;
    }
    .settings-nav {
        flex-direction: row !important;
        overflow-x: auto !important;
        border-right: none !important;
        border-bottom: 1px solid var(--border-faint) !important;
        min-width: unset !important;
        width: 100% !important;
        padding: var(--s-2) !important;
        flex-shrink: 0;
    }
    .settings-nav-group {
        display: flex !important;
        gap: var(--s-1) !important;
        flex-wrap: nowrap !important;
    }
    .settings-nav-title { display: none !important; }
    .settings-nav-item { white-space: nowrap !important; padding: 8px 12px !important; }
    .settings-content {
        width: 100% !important;
        height: auto !important;
        flex: 1;
        min-height: 0;
        overflow-y: auto;
    }

    /* Auth screen — full viewport, no scroll */
    .auth-screen,
    #authScreen {
        height: 100vh; height: 100dvh;
        overflow: hidden;
    }
    .auth-container {
        padding: var(--s-4);
        max-width: 100%;
        max-height: 100%;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    .auth-card {
        padding: var(--s-4);
        border-radius: var(--r-xl);
    }
    .auth-logo-icon { width: 46px; height: 46px; font-size: 1.25rem; }
    .auth-logo h1 { font-size: 1.6rem; }

    /* Password input — mobile touch friendly */
    .password-input-wrapper {
        padding: 0 0.25em;
        min-height: 44px;
    }
    .password-input-wrapper input[type="password"],
    .password-input-wrapper input[type="text"] {
        font-size: 1rem;
        padding: 12px 0 12px 0.5em;
    }
    .password-input-wrapper .password-toggle {
        font-size: 1.15rem;
        margin-left: 0.25em;
        padding: 0 0.25em;
    }
    .form-group label {
        font-size: 0.95rem;
    }
    .form-group {
        margin-bottom: 1.1em;
    }
    .password-strength {
        margin-top: 0.3em;
    }

    /* Command palette */
    .command-palette-content { width: 94vw !important; max-width: 440px !important; }

    /* Notification panel */
    .notification-panel { right: var(--s-2) !important; width: calc(100vw - 16px) !important; max-width: 380px !important; }

    /* Explore grid */
    .explore-grid { grid-template-columns: 1fr; }

    /* Shortcuts grid */
    .shortcuts-grid { grid-template-columns: 1fr !important; }

    /* Status selector — above bottom nav */
    .status-selector {
        position: fixed;
        bottom: 120px;
        left: var(--s-2);
        right: var(--s-2);
        top: auto;
        width: auto;
        z-index: calc(var(--z-overlay) + 3);
    }

    /* Scroll to top — above bottom nav */
    .scroll-to-top {
        bottom: 140px;
        right: var(--s-3);
    }

    /* GIF picker — above bottom nav */
    .gif-picker {
        width: calc(100vw - 32px) !important;
        max-width: 380px;
        right: 0 !important;
        bottom: 70px !important;
        max-height: 50vh !important; max-height: 50dvh !important;
    }

    /* Emoji picker — above bottom nav */
    .emoji-picker {
        width: calc(100vw - 32px) !important;
        max-width: 360px;
        right: 0 !important;
        bottom: 70px !important;
        max-height: 50vh !important; max-height: 50dvh !important;
    }

    /* Touch-friendly input sizing */
    input, select, textarea {
        min-height: 44px;
    }

    /* Search overlay — mobile contained */
    .search-overlay {
        padding-top: var(--s-4);
    }
    .search-panel {
        width: 94vw;
        max-width: 440px;
        max-height: 70vh; max-height: 70dvh;
    }

    /* Home content — contained scroll */
    .home-content,
    .friends-view {
        max-height: calc(100vh - 120px); max-height: calc(100dvh - 120px);
    }

    /* Connection status bar — shrink-proof */
    .connection-status { flex-shrink: 0; }

    /* Load older messages button */
    .load-older-messages {
        flex-shrink: 0;
    }
}

/* ═══════════════════════════════════════════ */
/*  Small phones (400px)                       */
/* ═══════════════════════════════════════════ */

@media (max-width: 400px) {
    html { font-size: 13px; }

    .server-sidebar { height: 54px; }
    .server-icon { width: 36px; height: 36px; font-size: 0.8rem; }

    /* Recalculate all heights for 54px bottom nav */
    .app-container {
        grid-template-rows: 1fr 54px;
    }

    .chat-area {
        height: calc(100vh - 54px); height: calc(100dvh - 54px);
        max-height: calc(100vh - 54px); max-height: calc(100dvh - 54px);
    }

    .channels-panel {
        width: 90%;
        max-width: 280px;
        bottom: 54px;
        height: calc(100vh - 54px); height: calc(100dvh - 54px);
    }
    .members-sidebar {
        bottom: 54px;
        height: calc(100vh - 54px); height: calc(100dvh - 54px);
    }

    .chat-header-actions button { width: 32px; height: 32px; font-size: 0.8rem; }

    .modal-content {
        width: 98vw !important;
        margin: var(--s-2) !important;
        max-height: 90vh !important; max-height: 90dvh !important;
    }

    .message-group .message-avatar .avatar { width: 30px; height: 30px; font-size: 0.7rem; }
    .message-author { font-size: 0.8125rem; }
    .message-text { font-size: 0.8125rem; }

    /* Emoji/GIF pickers — shorter on small phones */
    .emoji-picker,
    .gif-picker {
        max-height: 45vh !important; max-height: 45dvh !important;
    }

    /* Status selector above smaller nav */
    .status-selector { bottom: 110px; }
    .scroll-to-top { bottom: 120px; }
}

/* ═══════════════════════════════════════════ */
/*  Very small phones (360px)                  */
/* ═══════════════════════════════════════════ */

@media (max-width: 360px) {
    html { font-size: 12.5px; }

    .auth-card { padding: var(--s-3); }
    .auth-header { margin-bottom: var(--s-3); }
    .auth-logo-icon { width: 40px; height: 40px; font-size: 1.1rem; }
    .auth-logo h1 { font-size: 1.4rem; }

    .chat-header { min-height: 44px; }
    .chat-header-name { font-size: 0.85rem; }

    .message-input-wrapper { padding: 2px; }
    .message-input-left button,
    .message-input-right button { width: 30px; height: 30px; }
}

/* ═══════════════════════════════════════════ */
/*  PWA Standalone Mode                        */
/* ═══════════════════════════════════════════ */

@media (display-mode: standalone) {
    /* Safe area insets for notched phones */
    html, body, #app, .app, .main-app {
        height: 100vh; height: 100dvh;
        max-height: 100vh; max-height: 100dvh;
    }

    .server-sidebar {
        padding-bottom: env(safe-area-inset-bottom, 0);
        height: calc(60px + env(safe-area-inset-bottom, 0));
    }
    .chat-area {
        padding-top: env(safe-area-inset-top, 0);
    }
    .chat-header {
        padding-top: env(safe-area-inset-top, 0);
    }
    .channels-panel {
        padding-top: env(safe-area-inset-top, 0);
        bottom: calc(60px + env(safe-area-inset-bottom, 0));
    }
    .members-sidebar {
        padding-top: env(safe-area-inset-top, 0);
        bottom: calc(60px + env(safe-area-inset-bottom, 0));
    }
    .auth-screen {
        padding-top: env(safe-area-inset-top, 0);
    }
}

@media (display-mode: standalone) and (max-width: 768px) {
    .chat-area {
        height: calc(100dvh - 60px - env(safe-area-inset-bottom, 0));
        max-height: calc(100dvh - 60px - env(safe-area-inset-bottom, 0));
    }
}

@media (display-mode: standalone) and (max-width: 400px) {
    .server-sidebar {
        height: calc(54px + env(safe-area-inset-bottom, 0));
    }
    .chat-area {
        height: calc(100dvh - 54px - env(safe-area-inset-bottom, 0));
        max-height: calc(100dvh - 54px - env(safe-area-inset-bottom, 0));
    }
    .channels-panel {
        bottom: calc(54px + env(safe-area-inset-bottom, 0));
    }
    .members-sidebar {
        bottom: calc(54px + env(safe-area-inset-bottom, 0));
    }
}

/* ═══════════════════════════════════════════ */
/*  Landscape Mobile                           */
/* ═══════════════════════════════════════════ */

@media (max-height: 500px) and (orientation: landscape) {
    html, body, #app, .app, .main-app {
        height: 100vh; height: 100dvh;
        max-height: 100vh; max-height: 100dvh;
    }

    .server-sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        right: auto;
        width: 52px;
        height: 100vh; height: 100dvh;
        flex-direction: column;
        border-top: none;
        border-right: 1px solid var(--border-faint);
        overflow-y: auto;
        overflow-x: hidden;
    }

    .server-icon { width: 36px; height: 36px; }

    .app-container {
        grid-template-columns: 1fr;
        grid-template-rows: 1fr;
        height: 100vh; height: 100dvh;
    }

    .chat-area {
        padding-bottom: 0;
        padding-left: 52px;
        height: 100vh; height: 100dvh;
        max-height: 100vh; max-height: 100dvh;
    }

    .channels-panel {
        left: 52px;
        top: 0;
        bottom: 0;
        height: 100vh; height: 100dvh;
    }

    .members-sidebar {
        top: 0;
        bottom: 0;
        height: 100vh; height: 100dvh;
    }

    .modal-content {
        max-height: 92vh !important; max-height: 92dvh !important;
    }

    /* Compact header/input in landscape */
    .chat-header { min-height: 42px; }
    .message-input-area { padding: 0 var(--s-2) var(--s-2); }

    /* Auth — scroll if form is tall */
    .auth-container {
        max-height: 100vh; max-height: 100dvh;
        overflow-y: auto;
    }
}

/* ═══════════════════════════════════════════ */
/*  Tablet portrait (601–768px)                */
/* ═══════════════════════════════════════════ */

@media (min-width: 601px) and (max-width: 768px) {
    .auth-container {
        max-width: 430px;
    }
    .modal-content {
        max-width: 500px !important;
    }
}

/* ═══════════════════════════════════════════ */
/*  Print Styles                               */
/* ═══════════════════════════════════════════ */

@media print {
    .server-sidebar,
    .channels-panel,
    .members-sidebar,
    .chat-header,
    .message-input-area,
    .mobile-overlay,
    .page-loader,
    #toastContainer { display: none !important; }

    .app-container {
        display: block !important;
        height: auto !important;
        max-height: none !important;
    }
    .chat-area {
        display: block !important;
        height: auto !important;
        max-height: none !important;
        overflow: visible !important;
    }
    .messages-container {
        height: auto !important;
        max-height: none !important;
        overflow: visible !important;
    }
    body {
        background: white !important;
        color: #333 !important;
        height: auto !important;
        overflow: visible !important;
    }
    .message-text { color: #333 !important; }
    .message-author { color: #000 !important; }
}

/* ═══════════════════════════════════════════ */
/*  Accessibility — Reduced Motion             */
/* ═══════════════════════════════════════════ */

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* ═══════════════════════════════════════════ */
/*  High Contrast                              */
/* ═══════════════════════════════════════════ */

@media (prefers-contrast: high) {
    :root {
        --border-subtle: rgba(148, 163, 184, 0.25);
        --border-muted: rgba(148, 163, 184, 0.35);
        --text-400: #E2E8F0;
        --text-500: #CBD5E1;
    }
}

/* ═══════════════════════════════════════════ */
/*  Load Older Messages button styling         */
/* ═══════════════════════════════════════════ */

.load-older-messages {
    display: block;
    width: auto;
    margin: var(--s-2) auto;
    padding: 6px 16px;
    border: 1px solid var(--border-subtle);
    border-radius: var(--r-full);
    background: var(--bg-raised);
    color: var(--text-400);
    font-size: 0.8rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--t-fast);
    flex-shrink: 0;
}

.load-older-messages:hover {
    background: var(--bg-overlay);
    color: var(--text-200);
    border-color: var(--brand);
}
