/* =========================================================
   THE NODE — PAGE SYSTEM
   Shared structural, responsive, accessibility, and component
   corrections for every community and administration page.
   Loaded after the base design system and before page-specific CSS.
   ========================================================= */

:root {
    --page-max-width: 1180px;
    --page-wide-width: 1440px;
    --page-gutter: clamp(1rem, 3vw, 2rem);
    --page-section-gap: clamp(1.5rem, 4vw, 3rem);
    --page-card-radius: var(--radius-xl, 1rem);
}

html {
    min-width: 320px;
    overflow-x: clip;
}

body {
    overflow-x: clip;
}

.main-wrapper {
    min-height: calc(100vh - 72px);
    padding-block: clamp(1.25rem, 3vw, 2.5rem);
}

.main-wrapper:has(> .hero:first-child),
.main-wrapper:has(> .landing-hero:first-child) {
    padding-top: 0;
}

.container,
.page-container,
.content-container,
.dashboard-container,
.settings-container,
.profile-container,
.messages-container,
.notifications-container,
.resources-container,
.forum-container,
.admin-content-inner {
    width: min(var(--page-max-width), calc(100% - (var(--page-gutter) * 2)));
    margin-inline: auto;
    min-width: 0;
}

.container-fluid,
.social-page-container,
.social-dashboard-container {
    width: min(var(--page-wide-width), calc(100% - (var(--page-gutter) * 2)));
    margin-inline: auto;
    min-width: 0;
}

/* Prevent grid and flex descendants from forcing horizontal overflow. */
:where(.main-wrapper, .admin-main, .admin-content, .dashboard-grid, .content-grid,
       .settings-grid, .profile-grid, .market-grid, .cards-grid, .stats-grid,
       .form-grid, .social-grid, .team-grid, .job-grid, .events-grid) > * {
    min-width: 0;
}

:where(.card, .panel, .widget, .admin-card, .dashboard-card, .stat-card,
       .market-card, .job-card, .event-card, .team-card, .resource-card,
       .post-card, .settings-section, .profile-section) {
    max-width: 100%;
}

:where(h1, h2, h3, h4, h5, h6, p, li, dt, dd, td, th, label, a, button, span) {
    overflow-wrap: anywhere;
}

/* Standard page heading contract. */
.page-header,
.dashboard-header,
.section-header,
.content-header,
.admin-page-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    flex-wrap: wrap;
    gap: 1rem;
    margin-bottom: clamp(1.25rem, 3vw, 2rem);
}

.page-header > div:first-child,
.dashboard-header > div:first-child,
.section-header > div:first-child,
.content-header > div:first-child,
.admin-page-header > div:first-child {
    min-width: 0;
}

.page-header h1,
.dashboard-header h1,
.content-header h1,
.admin-page-header h1 {
    margin: 0 0 .4rem;
    font-size: clamp(1.75rem, 4vw, 2.5rem);
    line-height: 1.12;
}

.page-header p,
.dashboard-header p,
.content-header p,
.admin-page-header p {
    max-width: 72ch;
    margin: 0;
}

.header-actions,
.page-actions,
.card-actions,
.form-actions,
.modal-actions,
.bulk-actions,
.filter-actions {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: .75rem;
}

/* Forms */
form {
    min-width: 0;
}

:where(input:not([type="checkbox"]):not([type="radio"]):not([type="range"]),
       select, textarea) {
    width: 100%;
    max-width: 100%;
    min-height: 44px;
}

textarea {
    min-height: 120px;
    resize: vertical;
}

input[type="file"] {
    min-height: auto;
}

:where(input, select, textarea, button):focus-visible,
[tabindex]:focus-visible,
a:focus-visible {
    outline: 3px solid color-mix(in srgb, var(--primary, #4f46e5) 30%, transparent);
    outline-offset: 2px;
}

.form-row,
.form-grid,
.fields-grid,
.settings-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 1rem;
}

.form-group,
.form-field,
.field-group {
    min-width: 0;
}

.form-help,
.help-text,
.field-hint {
    display: block;
    margin-top: .35rem;
    color: var(--text-muted, #6b7280);
    font-size: .875rem;
}

.form-error,
.field-error,
.invalid-feedback {
    display: block;
    margin-top: .35rem;
    color: var(--danger, #dc2626);
    font-size: .875rem;
}

.is-invalid,
.error input,
.error select,
.error textarea,
input.error,
select.error,
textarea.error {
    border-color: var(--danger, #dc2626) !important;
}

/* Tables */
.table-responsive,
.admin-table-wrapper,
.table-wrapper,
.data-table-wrapper {
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    overscroll-behavior-inline: contain;
    -webkit-overflow-scrolling: touch;
    border-radius: var(--radius-lg, .75rem);
}

table {
    width: 100%;
    max-width: 100%;
    border-collapse: collapse;
}

th,
td {
    vertical-align: middle;
}

/* Images, charts, embeds, code, and rich content. */
img,
svg,
video,
canvas,
iframe,
embed,
object {
    max-width: 100%;
}

iframe,
video {
    width: 100%;
}

pre {
    max-width: 100%;
    overflow: auto;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

/* Modals and dialogs */
.modal-overlay,
.modal-backdrop,
.dialog-backdrop {
    padding: clamp(.75rem, 3vw, 2rem);
    overflow-y: auto;
    overscroll-behavior: contain;
}

.modal,
.modal-content,
.dialog,
.dialog-content {
    width: min(680px, 100%);
    max-height: calc(100dvh - 2rem);
    overflow-y: auto;
}

body.modal-open,
body.dialog-open,
body.nav-open {
    overflow: hidden;
}

/* Tabs remain usable at narrow widths. */
.tabs,
.tabs-nav,
.tab-list,
.dashboard-tabs,
.settings-tabs {
    max-width: 100%;
    overflow-x: auto;
    overscroll-behavior-inline: contain;
    scrollbar-width: thin;
}

.tab-content:not(.active):not([data-always-visible]) {
    display: none;
}

.tab-content.active,
.tab-content[data-always-visible] {
    display: block;
}

/* Loading and disabled states. */
.is-loading {
    position: relative;
    pointer-events: none;
    opacity: .72;
}

button.is-loading,
.btn.is-loading {
    color: transparent !important;
}

button.is-loading::after,
.btn.is-loading::after {
    content: "";
    position: absolute;
    inset: 50% auto auto 50%;
    width: 1rem;
    height: 1rem;
    margin: -.5rem 0 0 -.5rem;
    border: 2px solid currentColor;
    border-right-color: transparent;
    border-radius: 50%;
    animation: node-page-spin .7s linear infinite;
    color: var(--text-inverse, #fff);
}

@keyframes node-page-spin {
    to { transform: rotate(360deg); }
}

/* Universal empty, alert, and status presentation. */
.empty-state,
.no-results,
.blank-state {
    width: 100%;
    padding: clamp(2rem, 6vw, 4rem) clamp(1rem, 4vw, 2rem);
    text-align: center;
}

.alert,
.notice,
.flash-message {
    max-width: 100%;
}

/* Legacy page wrapper used to isolate older page-specific styles. */
.node-legacy-page {
    width: 100%;
    min-width: 0;
    color: var(--text-primary, #111827);
}

.node-legacy-page > .container {
    padding-inline: 0;
}

/* Keep fixed controls clear of phones with safe-area insets. */
.back-to-top,
.floating-action,
.ai-helpdesk-launcher {
    margin-bottom: env(safe-area-inset-bottom, 0);
    margin-right: env(safe-area-inset-right, 0);
}

/* Accessibility and reduced-motion support. */
[hidden] {
    display: none !important;
}

.sr-only,
.visually-hidden {
    position: absolute !important;
    width: 1px !important;
    height: 1px !important;
    padding: 0 !important;
    margin: -1px !important;
    overflow: hidden !important;
    clip: rect(0, 0, 0, 0) !important;
    white-space: nowrap !important;
    border: 0 !important;
}

@media (max-width: 900px) {
    .form-row,
    .form-grid,
    .fields-grid,
    .settings-grid {
        grid-template-columns: 1fr;
    }

    .sidebar-layout,
    .content-with-sidebar,
    .two-column-layout,
    .three-column-layout {
        grid-template-columns: 1fr !important;
    }

    .sticky-sidebar,
    .dashboard-sidebar,
    .market-sidebar {
        position: static !important;
        top: auto !important;
    }
}

@media (max-width: 640px) {
    :root {
        --page-gutter: .875rem;
    }

    .main-wrapper {
        padding-block: 1rem 1.5rem;
    }

    .page-header,
    .dashboard-header,
    .section-header,
    .content-header,
    .admin-page-header {
        display: block;
    }

    .header-actions,
    .page-actions,
    .form-actions,
    .modal-actions,
    .bulk-actions,
    .filter-actions {
        width: 100%;
        margin-top: 1rem;
    }

    .header-actions > *,
    .page-actions > *,
    .form-actions > *,
    .modal-actions > * {
        flex: 1 1 auto;
        justify-content: center;
    }

    .modal-overlay,
    .modal-backdrop,
    .dialog-backdrop {
        align-items: flex-end !important;
        padding: 0;
    }

    .modal,
    .modal-content,
    .dialog,
    .dialog-content {
        width: 100%;
        max-width: none;
        max-height: 92dvh;
        border-radius: 1rem 1rem 0 0;
    }
}

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

@media print {
    .navbar,
    .site-footer,
    .admin-sidebar,
    .admin-topbar,
    .back-to-top,
    .ai-helpdesk-widget,
    .no-print {
        display: none !important;
    }

    body,
    .main-wrapper,
    .admin-main,
    .admin-content {
        background: #fff !important;
        color: #000 !important;
        padding: 0 !important;
        margin: 0 !important;
    }

    .container,
    .container-fluid,
    .page-container {
        width: 100% !important;
        max-width: none !important;
    }
}


/* Standalone landing-page mobile navigation */
@media (max-width: 768px) {
    .nav-container { position: relative; }
    .nav-links.mobile-open {
        display: flex !important;
        position: absolute;
        top: calc(100% + .75rem);
        left: 1rem;
        right: 1rem;
        z-index: 1000;
        flex-direction: column;
        align-items: stretch;
        gap: .25rem;
        padding: .75rem;
        border: 1px solid rgba(148, 163, 184, .3);
        border-radius: 12px;
        background: rgba(15, 23, 42, .98);
        box-shadow: 0 18px 45px rgba(2, 6, 23, .35);
    }
    .nav-links.mobile-open a { width: 100%; padding: .7rem .8rem; }
}

/* =========================================================
   SHARED FEEDBACK & FIXED CONTROLS
   Centralized here to avoid late body-level <style> overrides.
   ========================================================= */
.flash-container {
    position: fixed;
    top: calc(72px + 1rem);
    right: max(1rem, env(safe-area-inset-right));
    z-index: 20000;
    width: min(400px, calc(100vw - 2rem));
    pointer-events: none;
}

.flash-message {
    display: flex;
    align-items: flex-start;
    gap: 1rem;
    padding: 1rem 1.125rem;
    margin-bottom: .75rem;
    border: 1px solid var(--border-medium, #e5e7eb);
    border-left-width: 4px;
    border-radius: var(--radius-lg, .75rem);
    background: var(--bg-primary, #fff);
    color: var(--text-primary, #111827);
    box-shadow: var(--shadow-xl, 0 20px 25px -5px rgb(0 0 0 / .1));
    pointer-events: auto;
    animation: node-flash-in .25s ease-out both;
    transition: opacity .2s ease, transform .2s ease;
}

.flash-message.is-closing {
    opacity: 0;
    transform: translateX(1rem);
}

.flash-success { border-left-color: var(--success, #10b981); background: var(--success-light, #d1fae5); }
.flash-error { border-left-color: var(--danger, #ef4444); background: var(--danger-light, #fee2e2); }
.flash-warning { border-left-color: var(--warning, #f59e0b); background: var(--warning-light, #fef3c7); }
.flash-info { border-left-color: var(--info, #3b82f6); background: var(--info-light, #dbeafe); }
.flash-icon { flex: 0 0 auto; margin-top: .1rem; font-size: 1.25rem; }
.flash-success .flash-icon { color: var(--success, #10b981); }
.flash-error .flash-icon { color: var(--danger, #ef4444); }
.flash-warning .flash-icon { color: var(--warning, #f59e0b); }
.flash-info .flash-icon { color: var(--info, #3b82f6); }
.flash-content { flex: 1 1 auto; min-width: 0; color: inherit; line-height: 1.5; }
.flash-close {
    flex: 0 0 auto;
    display: inline-grid;
    place-items: center;
    width: 2rem;
    height: 2rem;
    padding: 0;
    border: 0;
    border-radius: var(--radius-md, .5rem);
    background: transparent;
    color: var(--text-muted, #6b7280);
    cursor: pointer;
}
.flash-close:hover { background: rgb(255 255 255 / .55); color: var(--text-primary, #111827); }

@keyframes node-flash-in {
    from { opacity: 0; transform: translateX(1rem); }
    to { opacity: 1; transform: none; }
}

.skip-to-main {
    position: fixed;
    top: .5rem;
    left: .5rem;
    z-index: 30000;
    padding: .625rem 1rem;
    border-radius: var(--radius-md, .5rem);
    background: var(--primary, #4f46e5);
    color: #fff;
    font-weight: 700;
    transform: translateY(calc(-100% - 1rem));
    transition: transform .15s ease;
}
.skip-to-main:focus { transform: translateY(0); color: #fff; }

.page-loading {
    position: fixed;
    inset: 0 auto auto 0;
    z-index: 30001;
    width: 100%;
    height: 3px;
    overflow: hidden;
    pointer-events: none;
    opacity: 0;
    background: color-mix(in srgb, var(--primary, #4f46e5) 18%, transparent);
    transition: opacity .12s ease;
}
.page-loading::after {
    content: "";
    display: block;
    width: 38%;
    height: 100%;
    background: linear-gradient(90deg, transparent, var(--primary, #4f46e5), transparent);
    transform: translateX(-120%);
}
.page-loading.active { opacity: 1; }
.page-loading.active::after { animation: node-page-loading 1s ease-in-out infinite; }
@keyframes node-page-loading { to { transform: translateX(320%); } }

.back-to-top {
    position: fixed;
    right: max(1.25rem, env(safe-area-inset-right));
    bottom: max(1.25rem, env(safe-area-inset-bottom));
    z-index: 1100;
    display: inline-grid;
    place-items: center;
    width: 3rem;
    height: 3rem;
    padding: 0;
    border: 0;
    border-radius: 999px;
    background: var(--primary, #4f46e5);
    color: #fff;
    box-shadow: var(--shadow-lg, 0 10px 15px -3px rgb(0 0 0 / .1));
    cursor: pointer;
    opacity: 0;
    visibility: hidden;
    transform: translateY(.75rem);
    transition: opacity .2s ease, visibility .2s ease, transform .2s ease, background .2s ease;
}
.back-to-top.visible { opacity: 1; visibility: visible; transform: none; }
.back-to-top:hover { background: var(--primary-hover, #4338ca); }

@media (max-width: 640px) {
    .flash-container { top: calc(64px + .5rem); right: .5rem; width: calc(100vw - 1rem); }
    .flash-message { padding: .875rem; }
    .back-to-top { right: max(.75rem, env(safe-area-inset-right)); bottom: max(.75rem, env(safe-area-inset-bottom)); }
}
