/*
 * Page layouts (app shell, auth shell, plain pages), page structure and the
 * utility classes. Loaded after components.css so utilities win.
 */

/* ------------------------------------------------------------------ Skip link */

.skip-link {
    position: fixed;
    top: var(--space-2);
    left: var(--space-2);
    z-index: calc(var(--z-toast) + 1);
    padding: 8px 14px;
    border-radius: var(--radius);
    background: var(--primary);
    color: var(--primary-fg);
    font-weight: 500;
    transform: translateY(-200%);
}

.skip-link:focus {
    transform: none;
    color: var(--primary-fg);
}

/* ------------------------------------------------------------------ App shell */

.app-shell {
    display: grid;
    grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
    min-height: 100vh;
    min-height: 100dvh;
}

.sidebar {
    position: sticky;
    top: 0;
    z-index: var(--z-sidebar);
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    border-right: 1px solid var(--border);
    background: var(--bg-sidebar);
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--topbar-h);
    padding: 0 var(--space-4) 0 var(--space-5);
    flex: none;
}

.sidebar-logo {
    display: inline-flex;
    align-items: center;
    border-radius: var(--radius-sm);
}

.sidebar-logo img {
    width: 128px;
    height: auto;
}

.logo-on-dark {
    display: none !important;
}

:root[data-theme="dark"] .logo-on-light {
    display: none !important;
}

:root[data-theme="dark"] .logo-on-dark {
    display: block !important;
}

.sidebar-close {
    display: none;
}

.sidebar-body {
    flex: 1;
    min-height: 0;
    padding: var(--space-2) var(--space-3) var(--space-4);
    overflow-y: auto;
}

.nav-section + .nav-section {
    margin-top: var(--space-5);
}

.nav-section-label {
    padding: 0 10px var(--space-2);
    color: var(--text-subtle);
    font-size: var(--text-xs);
    font-weight: 500;
}

.nav-list {
    display: grid;
    gap: 2px;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 10px;
    height: 34px;
    padding: 0 10px;
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    font-weight: 500;
    transition: var(--transition-colors);
}

.nav-item > i,
.nav-item > svg {
    flex: none;
    width: 18px;
    color: var(--text-subtle);
    font-size: 15px;
    text-align: center;
    transition: color var(--duration-fast) var(--ease);
}

.nav-item:hover {
    background: var(--nav-hover-bg);
    color: var(--text);
    text-decoration: none;
}

.nav-item:hover > i,
.nav-item:hover > svg {
    color: var(--text-muted);
}

.nav-item.is-active {
    background: var(--nav-active-bg);
    color: var(--nav-active-text);
}

.nav-item.is-active > i,
.nav-item.is-active > svg {
    color: var(--nav-active-icon);
}

.nav-item:focus-visible {
    outline: none;
    box-shadow: inset 0 0 0 2px var(--focus);
}

.nav-item-label {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.nav-item .count {
    background: var(--surface-active);
}

.nav-item.is-active .count,
.nav-item .count-accent {
    background: var(--accent-solid);
    color: #ffffff;
}

.sidebar-footer {
    flex: none;
    padding: var(--space-3);
    border-top: 1px solid var(--border);
}

.sidebar-account {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 6px;
    border-radius: var(--radius);
}

.sidebar-account-icon {
    display: inline-flex;
    flex: none;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--text-muted);
    font-size: 13px;
}

.sidebar-account-text {
    min-width: 0;
    line-height: 1.3;
}

.sidebar-account-name {
    display: block;
    overflow: hidden;
    color: var(--text);
    font-size: var(--text-sm);
    font-weight: 600;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sidebar-account-meta {
    display: block;
    overflow: hidden;
    color: var(--text-subtle);
    font-size: var(--text-xs);
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sidebar-backdrop {
    display: none;
}

.app-main {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

/* Top bar */
.topbar {
    position: sticky;
    top: 0;
    z-index: var(--z-sticky);
    display: flex;
    align-items: center;
    gap: var(--space-3);
    height: var(--topbar-h);
    padding: 0 var(--space-6);
    border-bottom: 1px solid var(--border);
    /* Opaque: content scrolled under the bar must not show through as tinted shapes. */
    background: var(--bg);
}

.topbar-menu {
    display: none;
}

.topbar-title {
    display: flex;
    flex: 1;
    align-items: center;
    min-width: 0;
}

/* The breadcrumb landmark may shrink below its content so the current page's label gets its ellipsis. */
.topbar-title > nav {
    display: flex;
    min-width: 0;
}

.breadcrumbs {
    display: flex;
    align-items: center;
    gap: 6px;
    min-width: 0;
    color: var(--text-muted);
    font-size: var(--text-sm);
    white-space: nowrap;
}

.breadcrumbs li {
    display: flex;
    align-items: center;
    gap: 6px;
    min-width: 0;
}

.breadcrumbs li + li::before {
    content: "/";
    color: var(--text-disabled);
}

.breadcrumbs a {
    color: var(--text-muted);
}

.breadcrumbs a:hover {
    color: var(--text);
    text-decoration: none;
}

.breadcrumbs [aria-current="page"] {
    overflow: hidden;
    color: var(--text);
    font-weight: 500;
    text-overflow: ellipsis;
}

.topbar-actions {
    display: flex;
    flex: none;
    align-items: center;
    gap: var(--space-1);
}

.topbar-divider {
    width: 1px;
    height: 20px;
    margin: 0 var(--space-2);
    background: var(--border);
}

.user-trigger {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    height: 36px;
    padding: 0 8px 0 4px;
    border-radius: var(--radius);
    color: var(--text);
    font-size: var(--text-sm);
    font-weight: 500;
    transition: background-color var(--duration-fast) var(--ease);
}

.user-trigger:hover,
.dropdown.is-open > .user-trigger {
    background: var(--surface-hover);
}

.user-trigger:focus-visible {
    outline: var(--focus-width) solid var(--focus);
    outline-offset: var(--focus-offset);
    border-radius: var(--radius);
}

.user-trigger-name {
    max-width: 160px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.user-trigger > i,
.user-trigger > svg {
    color: var(--text-subtle);
    font-size: 11px;
}

/* Theme toggle icon: [data-theme-icon="light|dark"] shows the icon of the resolved theme. */
[data-theme-icon="dark"],
:root[data-theme="dark"] [data-theme-icon="light"] {
    display: none !important;
}

:root[data-theme="dark"] [data-theme-icon="dark"] {
    display: inline-block !important;
}

:root.has-dialog {
    overflow: hidden;
}

.locale-code {
    font-size: var(--text-xs);
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}

/* Page content */
.page {
    flex: 1;
    width: 100%;
    max-width: calc(var(--container) + 2 * var(--space-8));
    margin: 0 auto;
    padding: var(--space-8) var(--space-8) var(--space-16);
}

.page-narrow {
    max-width: calc(var(--container-narrow) + 2 * var(--space-8));
}

.page-wide {
    max-width: calc(var(--container-wide) + 2 * var(--space-8));
}

.page-full {
    max-width: none;
}

/*
 * Page header: .page-header > (.page-back) + .page-header-body > .page-header-main + .page-actions.
 * The back link sits on its own line above the whole heading row (aligned with a leading icon, not
 * centred beside it) and keeps a short, fixed distance to the title.
 */
.page-header {
    margin-bottom: var(--space-6);
}

.page-header-body {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: var(--space-4);
}

.page-header-main {
    display: flex;
    align-items: center;
    gap: var(--space-4);
    min-width: 0;
    flex: 1 1 320px;
}

.page-header-text {
    display: flex;
    flex-direction: column;
    gap: var(--title-gap-lg);
    min-width: 0;
}

.page-eyebrow {
    margin-bottom: calc(var(--space-1) - var(--title-gap-lg));
    color: var(--text-muted);
    font-size: var(--text-sm);
    font-weight: 500;
}

.page-title {
    font-size: var(--text-2xl);
    font-weight: 600;
    letter-spacing: -0.022em;
    line-height: 1.2;
}

.page-subtitle {
    color: var(--text-muted);
    font-size: var(--text-base);
    line-height: var(--leading);
    text-wrap: pretty;
}

/* Title followed by status badges; long titles wrap inside words rather than overflow. */
.page-title-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-2);
    min-width: 0;
}

.page-title-row > .page-title {
    min-width: 0;
    overflow-wrap: anywhere;
}

/* Sub-page heading (level 2) below an area header and its tabs: section-sized title and text. */
.page-header-sub .page-header-text {
    gap: var(--title-gap);
}

.page-header-sub .page-title {
    font-size: var(--text-lg);
    letter-spacing: -0.01em;
    line-height: 1.3;
}

.page-header-sub .page-subtitle {
    font-size: var(--text-sm);
}

.page-actions {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: var(--space-2);
}

/* Back link above a page or sub-page heading. */
.page-back {
    display: flex;
    align-items: center;
    gap: 6px;
    width: fit-content;
    margin-bottom: var(--space-2);
    color: var(--text-muted);
    font-size: var(--text-sm);
    font-weight: 500;
    line-height: 20px;
}

.page-back > i,
.page-back > svg {
    font-size: 12px;
}

.page-back:hover {
    color: var(--text);
    text-decoration: none;
}

.section {
    margin-top: var(--space-8);
}

.section-header {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: var(--space-3);
    margin-bottom: var(--space-4);
}

.section-title {
    font-size: var(--text-lg);
    font-weight: 600;
    line-height: 1.3;
}

.section-description {
    margin-top: var(--title-gap);
    color: var(--text-muted);
    font-size: var(--text-sm);
    line-height: var(--leading);
    text-wrap: pretty;
}

/* Off-canvas sidebar below 1024 px */
@media (max-width: 1023.98px) {
    .app-shell {
        grid-template-columns: minmax(0, 1fr);
    }

    .sidebar {
        position: fixed;
        top: 0;
        bottom: 0;
        left: 0;
        z-index: var(--z-drawer);
        width: min(var(--sidebar-w), calc(100vw - 48px));
        height: auto;
        box-shadow: var(--shadow-xl);
        transform: translateX(-100%);
        visibility: hidden;
        transition: transform var(--duration-slow) var(--ease-out), visibility 0s linear var(--duration-slow);
    }

    .sidebar-open .sidebar {
        transform: none;
        visibility: visible;
        transition-delay: 0s;
    }

    .sidebar-close {
        display: inline-flex;
    }

    .sidebar-backdrop {
        position: fixed;
        inset: 0;
        z-index: calc(var(--z-drawer) - 1);
        display: block;
        background: var(--bg-overlay);
        opacity: 0;
        visibility: hidden;
        transition: opacity var(--duration) var(--ease), visibility 0s linear var(--duration);
    }

    .sidebar-open .sidebar-backdrop {
        opacity: 1;
        visibility: visible;
        transition-delay: 0s;
    }

    .sidebar-open {
        overflow: hidden;
    }

    .topbar {
        padding: 0 var(--space-4);
    }

    .topbar-menu {
        display: inline-flex;
        margin-left: calc(var(--space-2) * -1);
    }

    .page {
        padding: var(--space-6) var(--space-6) var(--space-12);
    }
}

@media (max-width: 640px) {
    .page {
        padding: var(--space-5) var(--space-4) var(--space-12);
    }

    .page-header {
        margin-bottom: var(--space-5);
    }

    .page-header-body {
        align-items: flex-start;
    }

    .page-title {
        font-size: var(--text-xl);
    }

    .page-actions {
        width: 100%;
    }

    .user-trigger-name,
    .topbar-divider {
        display: none;
    }

    /* Only the current crumb is shown on phones. */
    .breadcrumbs li.hide-mobile + li::before {
        display: none;
    }

    .topbar-actions .btn-label {
        display: none;
    }
}

/* ------------------------------------------------------------------ Auth shell */

.auth-shell {
    position: relative;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    min-height: 100dvh;
    background:
        radial-gradient(1200px 520px at 50% -180px, rgba(69, 69, 255, 0.07), transparent 70%),
        var(--bg);
}

:root[data-theme="dark"] .auth-shell {
    background:
        radial-gradient(1200px 520px at 50% -180px, rgba(99, 99, 255, 0.1), transparent 70%),
        var(--bg);
}

/* Public top bar and footer (layouts/_public-bar.php, _public-footer.php): the same in the auth and plain layouts. */
.public-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-4);
    width: 100%;
    padding: var(--space-4) var(--space-6);
}

.public-logo {
    display: inline-flex;
    flex: none;
    border-radius: var(--radius-sm);
}

.public-logo img {
    width: 136px;
    height: auto;
}

.public-bar-actions {
    display: flex;
    align-items: center;
    gap: var(--space-1);
}

.public-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-wrap: wrap;
    gap: var(--space-2) var(--space-5);
    padding: var(--space-5) var(--space-4) var(--space-6);
    color: var(--text-subtle);
    font-size: var(--text-sm);
}

.public-footer a {
    color: var(--text-muted);
}

.public-footer a:hover {
    color: var(--text);
}

@media (max-width: 640px) {
    .public-bar {
        padding: var(--space-3) var(--space-4);
    }
}

.auth-main {
    display: flex;
    flex: 1;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: var(--space-4) var(--space-4) var(--space-10);
}

.auth-card {
    width: 100%;
    max-width: 420px;
    padding: var(--space-8);
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    background: var(--surface);
    box-shadow: var(--shadow-md);
}

.auth-card-wide {
    max-width: 560px;
}

.auth-title {
    font-size: var(--text-xl);
    font-weight: 600;
    letter-spacing: -0.018em;
}

.auth-subtitle {
    margin-top: 6px;
    color: var(--text-muted);
}

.auth-header {
    margin-bottom: var(--space-6);
}

.auth-alt {
    margin-top: var(--space-6);
    color: var(--text-muted);
    font-size: var(--text-sm);
    text-align: center;
}

.auth-flash {
    display: grid;
    gap: var(--space-2);
    margin-bottom: var(--space-5);
}

@media (max-width: 480px) {
    .auth-main {
        justify-content: flex-start;
        padding-top: var(--space-2);
    }

    .auth-card {
        padding: var(--space-6) var(--space-5);
    }
}

/* ------------------------------------------------------------------ Plain shell (public pages, errors) */

.plain-shell {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    min-height: 100dvh;
}

.plain-main {
    display: flex;
    flex: 1;
    flex-direction: column;
    width: 100%;
    max-width: calc(var(--container-narrow) + 2 * var(--space-8));
    margin: 0 auto;
    padding: var(--space-8);
}

@media (max-width: 640px) {
    .plain-main {
        padding: var(--space-4);
    }
}

/* Error page */
.error-page {
    display: flex;
    flex: 1;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: var(--space-10) 0 var(--space-16);
    text-align: center;
}

.error-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 56px;
    height: 56px;
    margin-bottom: var(--space-5);
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    background: var(--surface);
    color: var(--text-muted);
    box-shadow: var(--shadow-sm);
    font-size: 22px;
}

.error-code {
    color: var(--accent-text);
    font-size: var(--text-sm);
    font-weight: 600;
    letter-spacing: 0.02em;
}

.error-title {
    margin-top: var(--space-2);
    font-size: var(--text-3xl);
    font-weight: 600;
    letter-spacing: -0.025em;
}

.error-message {
    max-width: 460px;
    margin-top: var(--space-3);
    color: var(--text-muted);
    font-size: var(--text-md);
}

.error-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: var(--space-2);
    margin-top: var(--space-8);
}

.error-debug {
    width: 100%;
    margin-top: var(--space-10);
    text-align: left;
}

.error-debug pre {
    max-height: 50vh;
    font-size: var(--text-xs);
    white-space: pre-wrap;
    word-break: break-word;
}

/* ------------------------------------------------------------------ Grids */

.grid {
    display: grid;
    gap: var(--space-4);
}

.grid-2 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.grid-3 {
    grid-template-columns: repeat(3, minmax(0, 1fr));
}

.grid-4 {
    grid-template-columns: repeat(4, minmax(0, 1fr));
}

/* Main column + side column */
.grid-sidebar {
    grid-template-columns: minmax(0, 1fr) 340px;
    align-items: start;
}

.grid-auto {
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
}

@media (max-width: 1279.98px) {
    .grid-4 {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .grid-sidebar {
        grid-template-columns: minmax(0, 1fr) 300px;
    }
}

@media (max-width: 1023.98px) {
    .grid-3 {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .grid-sidebar {
        grid-template-columns: minmax(0, 1fr);
    }
}

@media (max-width: 640px) {
    .grid-2,
    .grid-3,
    .grid-4 {
        grid-template-columns: minmax(0, 1fr);
    }
}

/* ------------------------------------------------------------------ Utilities */

/* Visually hidden, still read by assistive technology. The box is 1 px and clips its content, so
   nothing inside it takes layout space or extends the scroll area. A table box cannot be made shorter
   than its rows: put the class on a wrapper around a table, never on the table itself. */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.flex { display: flex; }
.inline-flex { display: inline-flex; }
.block { display: block; }
.hidden { display: none !important; }
.flex-col { flex-direction: column; }
.flex-wrap { flex-wrap: wrap; }
.flex-1 { flex: 1 1 0%; min-width: 0; }
.flex-none { flex: none; }
.items-start { align-items: flex-start; }
.items-center { align-items: center; }
.items-end { align-items: flex-end; }
.items-baseline { align-items: baseline; }
.justify-between { justify-content: space-between; }
.justify-center { justify-content: center; }
.justify-end { justify-content: flex-end; }
.ml-auto { margin-left: auto; }

.gap-1 { gap: var(--space-1); }
.gap-2 { gap: var(--space-2); }
.gap-3 { gap: var(--space-3); }
.gap-4 { gap: var(--space-4); }
.gap-5 { gap: var(--space-5); }
.gap-6 { gap: var(--space-6); }
.gap-8 { gap: var(--space-8); }

/* Vertical rhythm: direct children spaced by the given step. */
.stack { display: flex; flex-direction: column; gap: var(--space-4); }
.stack-1 { display: flex; flex-direction: column; gap: var(--space-1); }
.stack-2 { display: flex; flex-direction: column; gap: var(--space-2); }
.stack-3 { display: flex; flex-direction: column; gap: var(--space-3); }
.stack-4 { display: flex; flex-direction: column; gap: var(--space-4); }
.stack-5 { display: flex; flex-direction: column; gap: var(--space-5); }
.stack-6 { display: flex; flex-direction: column; gap: var(--space-6); }
.stack-8 { display: flex; flex-direction: column; gap: var(--space-8); }

.mt-0 { margin-top: 0; }
.mt-1 { margin-top: var(--space-1); }
.mt-2 { margin-top: var(--space-2); }
.mt-3 { margin-top: var(--space-3); }
.mt-4 { margin-top: var(--space-4); }
.mt-6 { margin-top: var(--space-6); }
.mt-8 { margin-top: var(--space-8); }
.mb-0 { margin-bottom: 0; }
.mb-1 { margin-bottom: var(--space-1); }
.mb-2 { margin-bottom: var(--space-2); }
.mb-3 { margin-bottom: var(--space-3); }
.mb-4 { margin-bottom: var(--space-4); }
.mb-6 { margin-bottom: var(--space-6); }
.mb-8 { margin-bottom: var(--space-8); }
.p-0 { padding: 0; }

.w-full { width: 100%; }
.min-w-0 { min-width: 0; }

.text-xs { font-size: var(--text-xs); }
.text-sm { font-size: var(--text-sm); }
.text-base { font-size: var(--text-base); }
.text-lg { font-size: var(--text-lg); }
.text-xl { font-size: var(--text-xl); }
.font-normal { font-weight: 400; }
.font-medium { font-weight: 500; }
.font-semibold { font-weight: 600; }
.text-left { text-align: left; }
.text-center { text-align: center; }
.text-right { text-align: right; }
.text-default { color: var(--text); }
.text-muted { color: var(--text-muted); }
.text-subtle { color: var(--text-subtle); }
.text-accent { color: var(--accent-text); }
.text-success { color: var(--success); }
.text-warning { color: var(--warning); }
.text-danger { color: var(--danger); }
.text-info { color: var(--info); }
.tabular { font-variant-numeric: tabular-nums; }
.nowrap { white-space: nowrap; }
.break-words { overflow-wrap: anywhere; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mono { font-family: var(--font-mono); font-size: 0.92em; }

@media (max-width: 640px) {
    .hide-mobile { display: none !important; }
}

@media (min-width: 641px) {
    .show-mobile { display: none !important; }
}
