/* =========================================================
   PHOENIX V6.2 — UNIVERSAL VIEWPORT ENGINE
   Responsive layer for phones, tablets, laptops, 2K and 4K.
   Loaded last and intentionally overrides fixed legacy sizes.
   ========================================================= */

:root {
    --app-height: 100vh;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --topbar: clamp(62px, 6.8vh, 78px);
    --sidebar: clamp(228px, 16.5vw, 296px);
    --content-inline: clamp(12px, 2.6vw, 54px);
    --content-block: clamp(18px, 3.5vh, 42px);
    --content-max: 1920px;
    --fluid-gap: clamp(10px, 1.25vw, 22px);
    --fluid-radius: clamp(10px, .8vw, 18px);
    --fluid-text: clamp(13px, .82vw, 17px);
}

@supports (height: 100dvh) {
    :root { --app-height: 100dvh; }
}

html,
body {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    overflow-x: clip !important;
    scroll-behavior: auto !important;
}

body {
    background-attachment: scroll !important;
    font-size: var(--fluid-text);
    text-rendering: optimizeLegibility;
}

body::before {
    position: absolute !important;
    min-height: var(--app-height);
}

*,
*::before,
*::after {
    min-width: 0;
}

img,
picture,
video,
canvas,
svg,
iframe {
    max-width: 100%;
    height: auto;
}

img,
video {
    object-fit: contain;
}

pre,
code,
.kb-example,
.admin-table-wrap,
.dossier-tabs,
.report-tabs,
.studio-library-grid {
    max-width: 100%;
    overflow-x: auto;
}

p,
li,
dd,
td,
th,
strong,
small,
span,
a,
button,
label {
    overflow-wrap: anywhere;
}

input,
select,
textarea,
button {
    max-width: 100%;
    font-size: max(16px, 1em);
}

/* ---------- Global application frame ---------- */
.topbar {
    height: calc(var(--topbar) + var(--safe-top));
    padding: var(--safe-top) max(var(--content-inline), var(--safe-right)) 0 max(var(--content-inline), var(--safe-left));
    gap: clamp(8px, 1vw, 18px);
}

.brand {
    min-width: 0 !important;
    flex: 0 1 var(--sidebar);
}

.brand > span:last-child {
    min-width: 0;
}

.brand strong,
.brand small {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.top-status {
    min-width: 0;
    max-width: calc(100% - min(var(--sidebar), 38vw));
    gap: clamp(8px, 1.2vw, 22px);
    overflow: hidden;
}

.top-status > * {
    flex: 0 1 auto;
    white-space: nowrap;
}

.sidebar {
    top: calc(var(--topbar) + var(--safe-top));
    width: var(--sidebar);
    padding: clamp(16px, 2vh, 26px) clamp(12px, 1.2vw, 18px) calc(22px + var(--safe-bottom));
    overscroll-behavior: contain;
    scrollbar-gutter: stable;
}

.side-nav a {
    min-height: clamp(38px, 4.3vh, 46px);
    padding-inline: clamp(8px, .8vw, 12px);
}

.site-main {
    width: auto;
    min-height: calc(var(--app-height) - var(--topbar));
    margin-left: var(--sidebar);
    padding:
        calc(var(--topbar) + var(--safe-top) + var(--content-block))
        max(var(--content-inline), var(--safe-right))
        calc(58px + var(--safe-bottom))
        max(var(--content-inline), var(--safe-left));
}

body:not(.public-page) .page-frame,
.page-frame,
.public-frame {
    width: 100% !important;
    max-width: var(--content-max) !important;
    margin-inline: auto;
}

.site-footer {
    width: auto;
    margin-left: var(--sidebar);
    min-height: 58px;
    padding: 12px max(var(--content-inline), var(--safe-right)) calc(12px + var(--safe-bottom)) max(var(--content-inline), var(--safe-left));
    flex-wrap: wrap;
}

.page-heading {
    margin-bottom: clamp(16px, 2.5vh, 32px);
}

.page-heading h1,
.page-heading h2 {
    max-width: 100%;
    margin-block: clamp(6px, 1vh, 10px) clamp(10px, 1.5vh, 16px);
    font-size: clamp(30px, 3.25vw, 64px) !important;
    line-height: 1.04;
    letter-spacing: clamp(1px, .35vw, 5px);
    text-wrap: balance;
}

.hud-panel,
.public-card,
.application-panel,
.command-detail-panel,
.command-list-panel,
.report-editor-panel,
.report-detail-panel,
.auth-form-panel,
.profile-card,
.tech-widget,
.kb-block,
.dossier-preview,
.dossier-registry-list {
    max-width: 100%;
    min-width: 0;
}

/* Fluid card collections. */
.command-stat-grid,
.dossier-stat-grid,
.tech-overview-grid,
.tech-widget-grid,
.capability-grid,
.choice-grid,
.v3-module-grid,
.v3-feature-grid,
.recent-reports-grid,
.visual-gallery-grid,
.dossier-media-gallery,
.dossier-record-template-grid {
    grid-template-columns: repeat(auto-fit, minmax(min(230px, 100%), 1fr)) !important;
}

.content-grid,
.plan-columns,
.term-grid,
.dossier-section-grid,
.dossier-assessment-grid,
.dossier-overview-grid,
.application-summary-row,
.command-answer-grid,
.report-detail-data {
    grid-template-columns: repeat(auto-fit, minmax(min(290px, 100%), 1fr)) !important;
}

.platform-form.two-columns,
.application-form-grid,
.report-form-grid,
.review-form-grid,
.profile-editor-grid,
.dossier-media-form,
.v3-form-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

/* Prevent fixed media dimensions from breaking small viewports. */
.dossier-hero,
.public-hero,
.command-hero,
.auth-shell,
.auth-page-grid,
.profile-workspace,
.account-grid,
.tech-center-layout,
.command-workspace,
.reports-layout,
.dossier-registry-layout,
.channel-admin-grid,
.studio-workspace,
.knowledge-layout {
    max-width: 100%;
    min-width: 0;
}

.dossier-preview-photo,
.dossier-hero-photo,
.dossier-media-card img,
.profile-avatar,
.profile-banner {
    max-width: 100%;
}

/* Tables remain usable instead of stretching the whole page. */
.admin-table-wrap,
.table-scroll,
.dossier-table-wrap {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.admin-table,
table {
    max-width: 100%;
}

/* ---------- Chat: desktop, laptop, tablet and phone ---------- */
.chat-only-page {
    min-height: var(--app-height);
}

.chat-only-page .site-footer {
    display: none !important;
}

.chat-site-main {
    height: var(--app-height);
    min-height: 0 !important;
    padding-top: calc(var(--topbar) + var(--safe-top) + clamp(10px, 1.8vh, 24px)) !important;
    padding-bottom: max(10px, var(--safe-bottom)) !important;
    padding-inline: max(clamp(10px, 1.8vw, 32px), var(--safe-right)) max(clamp(10px, 1.8vw, 32px), var(--safe-left)) !important;
    overflow: hidden;
}

.chat-page-frame {
    height: 100%;
    max-width: 2200px !important;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    gap: clamp(8px, 1.2vh, 16px);
}

.chat-page-heading {
    margin: 0 !important;
}

.chat-page-heading .micro-label {
    display: none;
}

.chat-page-heading h1 {
    margin: 0 0 clamp(6px, .8vh, 10px) !important;
    font-size: clamp(26px, 2.6vw, 52px) !important;
}

.simple-chat-shell {
    width: 100%;
    height: auto !important;
    min-height: 0 !important;
    max-height: none !important;
    grid-template-columns: clamp(220px, 18vw, 310px) minmax(0, 1fr) !important;
    border-radius: var(--fluid-radius);
}

.simple-chat-channels,
.simple-chat-main,
.simple-chat-channel-list,
.simple-chat-messages {
    min-height: 0 !important;
}

.simple-chat-channels {
    grid-template-rows: auto auto minmax(0, 1fr) auto;
}

.simple-chat-server-head,
.simple-chat-header {
    min-height: clamp(58px, 7vh, 76px);
    padding: clamp(10px, 1.2vw, 16px);
}

.simple-chat-channel-list,
.simple-chat-messages {
    -webkit-overflow-scrolling: touch;
}

.simple-chat-messages {
    padding: clamp(10px, 1.25vw, 20px);
}

.simple-chat-form {
    padding: clamp(9px, 1vw, 15px) !important;
    padding-bottom: max(clamp(9px, 1vw, 15px), var(--safe-bottom)) !important;
}

.simple-chat-compose-row {
    grid-template-columns: auto minmax(0, 1fr) auto !important;
}

.simple-chat-form textarea {
    min-height: 46px;
    max-height: min(180px, 24vh);
}

.simple-chat-title small {
    max-width: min(60vw, 900px);
}

.chat-attachments {
    grid-template-columns: repeat(auto-fit, minmax(min(260px, 100%), 380px)) !important;
}

/* ---------- Large 2K / 4K and ultrawide screens ---------- */
@media (min-width: 1920px) {
    :root {
        --sidebar: clamp(270px, 14vw, 320px);
        --content-max: 2160px;
        --content-inline: clamp(36px, 3vw, 82px);
        --fluid-text: clamp(15px, .7vw, 18px);
    }

    .page-heading h1,
    .page-heading h2 {
        font-size: clamp(44px, 2.7vw, 72px) !important;
    }

    .simple-chat-shell {
        grid-template-columns: clamp(280px, 15vw, 350px) minmax(0, 1fr) !important;
    }

    .simple-chat-message-content p {
        max-width: 1100px;
    }
}

@media (min-width: 3000px) {
    :root {
        --content-max: 2520px;
        --sidebar: 340px;
        --topbar: 86px;
    }

    .side-nav a { font-size: 14px; }
    .simple-chat-message-content p { font-size: 18px; }
}

/* ---------- Compact laptop / browser side-by-side ---------- */
@media (max-width: 1280px) {
    :root {
        --sidebar: clamp(210px, 19vw, 244px);
        --content-inline: clamp(12px, 1.8vw, 28px);
    }

    .top-status { max-width: 58%; }
    .top-status > span:not(:last-child) { display: none; }

    .simple-chat-shell {
        grid-template-columns: clamp(205px, 21vw, 250px) minmax(0, 1fr) !important;
    }

    .dossier-registry-layout,
    .account-grid,
    .profile-workspace,
    .tech-center-layout {
        grid-template-columns: minmax(260px, .72fr) minmax(0, 1.28fr) !important;
    }
}

/* Sidebar becomes an off-canvas drawer before content gets cramped. */
@media (max-width: 1080px) {
    :root { --sidebar: min(86vw, 310px); }

    .menu-toggle { display: block !important; }

    .sidebar {
        transform: translate3d(-104%, 0, 0);
        transition: transform 180ms ease;
        box-shadow: 24px 0 70px rgba(0, 0, 0, .55);
    }

    body.menu-open .sidebar { transform: translate3d(0, 0, 0); }

    .sidebar-backdrop {
        position: fixed;
        inset: calc(var(--topbar) + var(--safe-top)) 0 0;
        z-index: 70;
        display: block;
        background: rgba(0, 4, 9, .74);
        opacity: 0;
        visibility: hidden;
        transition: opacity 160ms ease, visibility 160ms ease;
    }

    body.menu-open .sidebar-backdrop {
        opacity: 1;
        visibility: visible;
    }

    .site-main,
    .site-footer {
        margin-left: 0 !important;
    }

    .brand { flex-basis: auto; }

    .chat-site-main {
        margin-left: 0 !important;
    }

    .command-workspace,
    .reports-layout,
    .dossier-registry-layout,
    .account-grid,
    .profile-workspace,
    .tech-center-layout,
    .knowledge-layout,
    .studio-workspace {
        grid-template-columns: 1fr !important;
    }

    .command-list-panel,
    .reports-sidebar,
    .knowledge-index,
    .studio-library {
        position: static !important;
        max-height: none !important;
    }
}

/* ---------- Tablet ---------- */
@media (max-width: 820px) {
    :root {
        --topbar: 64px;
        --content-inline: 14px;
        --content-block: 18px;
    }

    .topbar {
        padding-inline: max(10px, var(--safe-left)) max(10px, var(--safe-right));
    }

    .brand small { display: none; }
    .top-status { max-width: 45%; }
    .top-status > *:not(#system-clock):not(.discord-header-link) { display: none !important; }

    .site-main {
        padding-top: calc(var(--topbar) + var(--safe-top) + 18px);
        padding-inline: max(12px, var(--safe-left)) max(12px, var(--safe-right));
    }

    .site-footer {
        justify-content: center;
        text-align: center;
    }

    .page-heading h1,
    .page-heading h2 {
        font-size: clamp(27px, 7.4vw, 44px) !important;
    }

    .platform-form.two-columns,
    .application-form-grid,
    .report-form-grid,
    .review-form-grid,
    .profile-editor-grid,
    .dossier-media-form,
    .v3-form-grid {
        grid-template-columns: 1fr !important;
    }

    .span-two,
    .full,
    .v3-span-2 {
        grid-column: auto !important;
    }

    .dossier-hero,
    .public-hero,
    .command-hero,
    .auth-shell,
    .auth-page-grid {
        grid-template-columns: 1fr !important;
    }

    .hero-copy,
    .auth-side,
    .auth-panel {
        padding: clamp(20px, 5vw, 36px) !important;
    }

    .hero-copy h2 {
        font-size: clamp(48px, 15vw, 82px) !important;
    }

    /* Chat uses a horizontal channel rail on tablets and phones. */
    .chat-site-main {
        padding-top: calc(var(--topbar) + var(--safe-top) + 8px) !important;
        padding-inline: max(6px, var(--safe-left)) max(6px, var(--safe-right)) !important;
        padding-bottom: max(6px, var(--safe-bottom)) !important;
    }

    .chat-page-frame {
        grid-template-rows: minmax(0, 1fr);
    }

    .chat-page-heading { display: none !important; }

    .simple-chat-shell {
        display: grid !important;
        grid-template-columns: 1fr !important;
        grid-template-rows: auto minmax(0, 1fr);
        border-radius: 12px;
    }

    .simple-chat-channels {
        display: grid !important;
        grid-template-columns: auto minmax(0, 1fr);
        grid-template-rows: auto;
        border-right: 0;
        border-bottom: 1px solid rgba(65, 153, 215, .2);
    }

    .simple-chat-server-head {
        min-height: 58px;
        grid-template-columns: 34px minmax(0, 1fr) 30px;
        border-bottom: 0;
        border-right: 1px solid rgba(65, 153, 215, .16);
    }

    .simple-chat-server-head small,
    .simple-chat-category-title,
    .simple-chat-user-panel {
        display: none !important;
    }

    .simple-chat-channel-list {
        display: flex !important;
        align-items: center;
        gap: 6px;
        overflow-x: auto;
        overflow-y: hidden;
        padding: 8px;
    }

    .simple-chat-channel {
        width: auto !important;
        min-width: clamp(145px, 34vw, 210px) !important;
        min-height: 44px;
        margin: 0 !important;
        padding: 8px 10px;
    }

    .simple-chat-channel-copy small { display: none; }
    .simple-chat-main { min-height: 0 !important; }
    .simple-chat-header { min-height: 58px; }
    .simple-chat-messages { padding: 10px; }
}

/* ---------- Phone ---------- */
@media (max-width: 560px) {
    :root {
        --topbar: 58px;
        --content-inline: 9px;
        --fluid-text: 14px;
    }

    .menu-toggle {
        width: 38px;
        height: 38px;
        margin-right: 4px;
    }

    .brand-mark {
        width: 34px;
        height: 34px;
        flex: 0 0 34px;
    }

    .brand strong {
        max-width: 120px;
        font-size: 10px;
        letter-spacing: 2px;
    }

    .top-status { max-width: 35%; }
    #system-clock { font-size: 8px; }

    .site-main {
        padding-inline: max(8px, var(--safe-left)) max(8px, var(--safe-right));
        padding-bottom: calc(36px + var(--safe-bottom));
    }

    .page-heading .micro-label { display: none; }

    .page-heading h1,
    .page-heading h2 {
        font-size: clamp(25px, 9vw, 36px) !important;
        letter-spacing: 1px;
    }

    .hud-panel,
    .public-card,
    .application-panel,
    .command-detail-panel,
    .command-list-panel,
    .report-editor-panel,
    .report-detail-panel,
    .auth-form-panel,
    .profile-card,
    .tech-widget,
    .dossier-preview,
    .dossier-registry-list {
        padding: 14px !important;
        border-radius: 10px !important;
    }

    .hud-button,
    .hero-actions > *,
    .public-hero-actions > *,
    .knowledge-actions > * {
        width: 100%;
    }

    .panel-header {
        gap: 8px;
        font-size: 7px;
        letter-spacing: .7px;
    }

    .dossier-preview-grid,
    .dossier-preview-data,
    .dossier-hero-metrics,
    .metric-row {
        grid-template-columns: 1fr !important;
    }

    .simple-chat-server-head {
        width: 54px;
        padding: 8px;
        grid-template-columns: 36px;
    }

    .simple-chat-server-head > div,
    .simple-chat-admin-link {
        display: none !important;
    }

    .simple-chat-channel-list { padding: 6px; }

    .simple-chat-channel {
        min-width: 126px !important;
        grid-template-columns: 18px minmax(0, 1fr);
        padding: 7px 8px;
    }

    .simple-chat-channel-symbol { font-size: 15px; }
    .simple-chat-channel-copy strong { font-size: 10px; }

    .simple-chat-header {
        padding: 9px 11px;
    }

    .simple-chat-title > span { font-size: 22px; }
    .simple-chat-title strong { font-size: 12px; }
    .simple-chat-title small { display: none; }
    .simple-chat-presence { display: none !important; }

    .simple-chat-message {
        grid-template-columns: 34px minmax(0, 1fr);
        gap: 8px;
        padding-inline: 2px;
    }

    .simple-chat-avatar {
        width: 32px;
        height: 32px;
        border-radius: 8px;
    }

    .simple-chat-form {
        padding: 7px !important;
        padding-bottom: max(7px, var(--safe-bottom)) !important;
    }

    .simple-chat-compose-row {
        grid-template-columns: 42px minmax(0, 1fr) !important;
        gap: 7px;
    }

    .simple-chat-file-button {
        width: 42px;
        min-height: 44px;
    }

    .simple-chat-file-button small { display: none; }

    .simple-chat-compose-row > button {
        grid-column: 1 / -1;
        min-height: 42px;
        width: 100%;
    }

    .simple-chat-upload-help { display: none; }
    .pending-upload { min-width: 100%; max-width: 100%; }

    .knowledge-modal,
    .platform-modal {
        padding: 4px !important;
    }

    .knowledge-modal-card,
    .modal-card,
    .phoenix-lightbox-panel {
        width: 100% !important;
        max-height: calc(var(--app-height) - 8px) !important;
        border-radius: 10px !important;
    }
}

/* Very narrow devices. */
@media (max-width: 380px) {
    .brand > span:last-child { display: none; }
    .top-status { max-width: 45%; }
    .simple-chat-channel { min-width: 112px !important; }
    .nav-code { display: none; }
    .side-nav a { grid-template-columns: 1fr 18px; }
}

/* ---------- Short laptop displays ---------- */
@media (max-height: 780px) and (min-width: 821px) {
    :root {
        --topbar: 58px;
        --content-block: 12px;
    }

    .topbar { backdrop-filter: none !important; }
    .sidebar { padding-top: 12px; }
    .sidebar-head { margin-bottom: 10px; padding-bottom: 10px; }
    .side-nav a { min-height: 34px; }
    .clearance-card { display: none; }

    .page-heading { margin-bottom: 10px; }
    .page-heading .micro-label { display: none; }
    .page-heading h1,
    .page-heading h2 { font-size: clamp(27px, 3vw, 44px) !important; }

    .chat-site-main {
        padding-top: calc(var(--topbar) + 8px) !important;
        padding-bottom: 6px !important;
    }
    .chat-page-heading h1 { font-size: 30px !important; }
    .simple-chat-server-head,
    .simple-chat-header { min-height: 54px; }
}

/* Phone landscape: keep the chat usable above the virtual keyboard. */
@media (max-height: 520px) and (max-width: 980px) and (orientation: landscape) {
    .topbar { display: none !important; }
    .chat-site-main {
        padding-top: max(4px, var(--safe-top)) !important;
        height: var(--app-height);
    }
    .simple-chat-header { min-height: 46px; }
    .simple-chat-channels { max-height: 54px; }
    .simple-chat-form textarea { min-height: 40px; }
}

/* Touch devices: remove hover-only motion and expensive overlays. */
@media (hover: none), (pointer: coarse) {
    .scanlines { display: none !important; }
    .hud-panel:hover,
    .public-card:hover,
    .choice-card:hover,
    .command-stat-card:hover,
    .v3-module-card:hover {
        transform: none !important;
    }
    .simple-chat-message-actions { opacity: 1; }
}

/* Performance-friendly mode for constrained displays. */
html.viewport-short .scanlines,
html.viewport-mobile .scanlines,
html.viewport-compact .v3-ambient,
html.viewport-compact .command-vignette {
    display: none !important;
}

html.viewport-keyboard-open .simple-chat-messages {
    padding-bottom: 6px;
}

html.viewport-keyboard-open .simple-chat-upload-help,
html.viewport-keyboard-open .chat-page-heading {
    display: none !important;
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation: none !important;
        transition-duration: .01ms !important;
        scroll-behavior: auto !important;
    }
}
