/* ==========================================================================
   UI-01 — application shell (top bar, sidebar, main region) and the shared chrome controls.
   Consumes --ps-* tokens only; no literal colours (ThemeColourSourceScanTests).
   Geometry is from the "Shell enhanced" design: top bar 56px, sidebar 248px, nav item 36px / 6px
   radius, controls 36px / 8px radius, chips 32px, cards 12px radius.
   ========================================================================== */

.ps-skip-link {
    position: absolute;
    left: 8px;
    top: -48px;
    z-index: 1000;
    padding: 8px 14px;
    border-radius: var(--ps-radius);
    background: var(--ps-primary);
    color: var(--ps-primary-ink);
    font-weight: 600;
    text-decoration: none;
}

.ps-skip-link:focus {
    top: 8px;
}

.ps-shell {
    display: grid;
    grid-template-columns: var(--ps-sidebar-w) minmax(0, 1fr);
    grid-template-rows: var(--ps-topbar-h) minmax(0, 1fr);
    grid-template-areas: "top top" "side main";
    min-height: 100vh;
    background: var(--ps-bg);
    color: var(--ps-text);
    font-family: var(--ps-font);
    font-size: 14px;
}

/* Desktop collapse: remembered in the browser and applied from <head> before first paint. */
@media (min-width: 1024px) {
    html[data-sidebar="collapsed"] .ps-shell {
        grid-template-columns: 0 minmax(0, 1fr);
    }

    html[data-sidebar="collapsed"] .ps-sidebar {
        visibility: hidden;
        overflow: hidden;
        border-right: 0;
    }
}

/* ---- top bar ---------------------------------------------------------- */
.ps-topbar {
    grid-area: top;
    position: sticky;
    top: 0;
    z-index: 30;
    display: grid;
    grid-template-columns: auto auto minmax(0, 1fr) auto;
    align-items: center;
    gap: 16px;
    height: var(--ps-topbar-h);
    padding: 0 16px;
    background: var(--ps-surface);
    border-bottom: 1px solid var(--ps-border);
}

.ps-topbar__brand,
.ps-topbar__actions,
.ps-topbar__company {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
}

.ps-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--ps-text);
    text-decoration: none;
    font-weight: 600;
    font-size: 15px;
    min-width: 0;
}

.ps-brand__mark {
    display: grid;
    place-items: center;
    flex: none;
    width: 28px;
    height: 28px;
    border-radius: var(--ps-radius);
    background: var(--ps-primary);
    color: var(--ps-primary-ink);
    font-weight: 700;
}

.ps-brand__name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.ps-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    height: 32px;
    padding: 0 10px;
    border: 1px solid var(--ps-border);
    border-radius: var(--ps-radius);
    background: var(--ps-surface-2);
    color: var(--ps-text);
    font: inherit;
    cursor: pointer;
}

.ps-chip__text {
    max-width: 220px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.ps-chip--static {
    cursor: default;
}

.ps-chip:hover {
    border-color: var(--ps-border-strong);
}

.ps-chip .rzi {
    font-size: 18px;
    color: var(--ps-text-2);
}

.ps-icon-btn {
    display: inline-grid;
    place-items: center;
    width: 36px;
    height: 36px;
    padding: 0;
    border: 1px solid transparent;
    border-radius: var(--ps-radius);
    background: transparent;
    color: var(--ps-text-2);
    cursor: pointer;
    text-decoration: none;
}

.ps-icon-btn:hover {
    background: var(--ps-primary-tint);
    color: var(--ps-primary);
}

.ps-icon-btn .rzi {
    font-size: 22px;
}

.ps-icon-btn[aria-pressed="true"] {
    color: var(--ps-primary);
}

.ps-avatar {
    display: grid;
    place-items: center;
    width: 36px;
    height: 36px;
    padding: 0;
    border: 1px solid var(--ps-border-strong);
    border-radius: 50%;
    background: var(--ps-primary-tint);
    color: var(--ps-primary);
    font: inherit;
    font-weight: 600;
    cursor: pointer;
}

.ps-only-mobile {
    display: none;
}

/* Visible keyboard focus everywhere in the chrome. */
.ps-shell :focus-visible,
.ps-auth-shell :focus-visible {
    outline: 2px solid var(--ps-primary);
    outline-offset: 2px;
}

/* ---- avatar menu ------------------------------------------------------ */
.ps-user-menu {
    position: relative;
}

.ps-menu-scrim {
    position: fixed;
    inset: 0;
    z-index: 40;
    border: 0;
    background: transparent;
    cursor: default;
}

.ps-menu-panel {
    position: absolute;
    right: 0;
    top: calc(100% + 8px);
    z-index: 41;
    min-width: 220px;
    padding: 6px;
    border: 1px solid var(--ps-border);
    border-radius: var(--ps-radius-lg);
    background: var(--ps-surface);
    box-shadow: var(--ps-shadow);
}

.ps-menu-panel[hidden] {
    display: none;
}

.ps-menu-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 8px 10px 10px;
    border-bottom: 1px solid var(--ps-border);
    margin-bottom: 4px;
}

.ps-menu-name {
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.ps-menu-form {
    margin: 0;
}

.ps-menu-item {
    display: block;
    width: 100%;
    height: 36px;
    padding: 0 10px;
    border: 0;
    border-radius: var(--ps-radius-sm);
    background: transparent;
    color: var(--ps-text);
    font: inherit;
    text-align: left;
    cursor: pointer;
}

.ps-menu-item:hover {
    background: var(--ps-primary-tint);
    color: var(--ps-primary);
}

.ps-pill {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 600;
}

.ps-pill--primary {
    background: var(--ps-primary-tint);
    color: var(--ps-primary);
}

/* ---- sidebar ---------------------------------------------------------- */
.ps-sidebar {
    grid-area: side;
    position: sticky;
    top: var(--ps-topbar-h);
    align-self: start;
    height: calc(100vh - var(--ps-topbar-h));
    overflow-y: auto;
    padding: 12px;
    background: var(--ps-surface);
    border-right: 1px solid var(--ps-border);
}

.ps-nav-root,
.ps-nav-list,
.ps-nav-sub {
    margin: 0;
    padding: 0;
    list-style: none;
}

.ps-nav-section + .ps-nav-section {
    margin-top: 14px;
}

.ps-nav-group-label {
    padding: 4px 10px 6px;
    font-size: 11px;
    font-weight: 600;
    letter-spacing: .08em;
    text-transform: uppercase;
    color: var(--ps-text-3);
}

/* Top-level groups are collapsible: the heading is the toggle. */
.ps-nav-link.ps-nav-top {
    height: 32px;
    margin-top: 6px;
    padding: 0 10px;
    font-size: 11px;
    font-weight: 600;
    letter-spacing: .08em;
    text-transform: uppercase;
    color: var(--ps-text-3);
}

.ps-nav-link.ps-nav-top:hover {
    color: var(--ps-text);
}

.ps-nav-link {
    position: relative;
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    height: 36px;
    padding: 0 10px 0 calc(10px + var(--ps-nav-depth, 0) * 16px);
    border: 0;
    border-radius: var(--ps-radius-sm);
    background: transparent;
    color: var(--ps-text-2);
    font: inherit;
    text-align: left;
    text-decoration: none;
    cursor: pointer;
}

.ps-nav-link:hover {
    background: var(--ps-surface-2);
    color: var(--ps-text);
}

.ps-nav-link.is-active {
    background: var(--ps-primary-tint);
    color: var(--ps-primary);
    font-weight: 600;
}

/* The 3px indicator sits on the sidebar's left edge (the sidebar has 12px of padding). */
.ps-nav-link.is-active::before {
    content: "";
    position: absolute;
    left: -12px;
    top: 4px;
    bottom: 4px;
    width: 3px;
    border-radius: 0 3px 3px 0;
    background: var(--ps-primary);
}

.ps-nav-icon {
    flex: none;
    font-size: 20px;
}

.ps-nav-text {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.ps-nav-chevron {
    flex: none;
    font-size: 18px;
    color: var(--ps-text-3);
}

/* Optional right-aligned count (styles only; no data source yet). */
.ps-nav-count {
    flex: none;
    min-width: 20px;
    padding: 1px 6px;
    border-radius: 999px;
    background: var(--ps-badge-bg);
    color: var(--ps-badge-ink);
    font-family: var(--ps-font-mono);
    font-size: 11px;
    text-align: center;
}

/* Sidebar footer card (styles only; "Period close" is a follow-up). */
.ps-sidebar-card {
    margin-top: 16px;
    padding: 12px 14px;
    border: 1px solid var(--ps-border);
    border-radius: var(--ps-radius-lg);
    background: var(--ps-surface-2);
}

/* ---- main region ------------------------------------------------------ */
.ps-main {
    grid-area: main;
    min-width: 0;
    padding: 20px 28px 32px;
    outline: none;
}

.ps-page-header {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: 16px;
    flex-wrap: wrap;
    margin-bottom: 16px;
}

.ps-breadcrumb {
    margin: 0 0 4px;
    font-size: 12px;
    color: var(--ps-text-3);
}

.ps-page-title {
    margin: 0;
    font-size: 24px;
    font-weight: 600;
    letter-spacing: -.01em;
    color: var(--ps-text);
}

.ps-page-subtitle {
    margin: 4px 0 0;
    color: var(--ps-text-2);
}

.ps-page-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.ps-stat-strip {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 12px;
    margin-bottom: 16px;
}

.ps-card {
    padding: 14px 16px;
    border: 1px solid var(--ps-border);
    border-radius: var(--ps-radius-lg);
    background: var(--ps-surface);
    box-shadow: var(--ps-shadow);
}

.ps-stat-label {
    font-size: 12px;
    color: var(--ps-text-3);
}

.ps-stat-value {
    margin-top: 4px;
    font-family: var(--ps-font-mono);
    font-size: 20px;
    font-weight: 500;
    font-variant-numeric: tabular-nums;
}

.ps-stat-value--pos { color: var(--ps-pos); }
.ps-stat-value--neg { color: var(--ps-neg); }

/* ---- responsive: off-canvas drawer below 1024px ----------------------- */
.ps-drawer-scrim {
    position: fixed;
    inset: var(--ps-topbar-h) 0 0 0;
    z-index: 24;
    border: 0;
    background: var(--ps-overlay);
}

@media (max-width: 1023.98px) {
    .ps-shell {
        grid-template-columns: minmax(0, 1fr);
        grid-template-areas: "top" "main";
    }

    .ps-only-desktop {
        display: none;
    }

    .ps-only-mobile {
        display: inline-grid;
    }

    .ps-topbar {
        grid-template-columns: auto minmax(0, 1fr) auto;
        padding: 0 12px;
    }

    .ps-topbar__search {
        display: none;
    }

    .ps-topbar__company {
        justify-content: flex-end;
    }

    .ps-chip__text {
        max-width: 120px;
    }

    .ps-brand__name {
        display: none;
    }

    .ps-sidebar {
        position: fixed;
        left: 0;
        top: var(--ps-topbar-h);
        bottom: 0;
        z-index: 25;
        width: min(var(--ps-sidebar-w), 86vw);
        transform: translateX(-100%);
        transition: transform .18s ease;
    }

    .ps-shell--drawer-open .ps-sidebar {
        transform: translateX(0);
    }

    .ps-main {
        padding: 16px;
    }

    .ps-stat-strip {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

/* Sign-in pages have no shell, so the theme switch floats in the corner. */
.ps-auth-theme {
    position: fixed;
    top: 12px;
    right: 12px;
    z-index: 10;
}

/* ---- portal chrome ---------------------------------------------------- */
.ps-portal-nav {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    padding: 8px 28px 0;
}

.ps-portal-nav a {
    display: inline-flex;
    align-items: center;
    height: 36px;
    padding: 0 12px;
    border-radius: var(--ps-radius-sm);
    color: var(--ps-text-2);
    text-decoration: none;
}

.ps-portal-nav a:hover {
    background: var(--ps-surface-2);
    color: var(--ps-text);
}

.ps-portal-nav a.active {
    background: var(--ps-primary-tint);
    color: var(--ps-primary);
    font-weight: 600;
}

.ps-portal-shell {
    min-height: 100vh;
    background: var(--ps-bg);
    color: var(--ps-text);
    font-family: var(--ps-font);
}

.ps-portal-shell .ps-topbar {
    grid-template-columns: minmax(0, 1fr) auto;
}

/* ---- UI-02: page width -------------------------------------------------------------
   Working pages fill .ps-main at every width; nothing here sets a pixel ceiling. See
   CLAUDE.md "Page width". Constants only - no colours. */
.ps-page {
    width: 100%;
    min-width: 0;
}

/* Header fields (Customer, Date, Number, Due Date...) spread across the width and wrap on narrow
   screens instead of leaving an empty band on wide ones. */
.ps-form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(clamp(200px, 16vw, 320px), 1fr));
    gap: 0.75rem;
    align-items: start;
    width: 100%;
}

.ps-form-grid > * {
    min-width: 0;
}

/* Settings pages: a capped, LEFT-aligned form with help text beside it on wide screens. */
.ps-form-split {
    display: grid;
    grid-template-columns: minmax(0, 48rem) minmax(0, 1fr);
    column-gap: 2rem;
    row-gap: 1rem;
    align-items: start;
    width: 100%;
}

.ps-form-narrow {
    max-width: 48rem;
    width: 100%;
    min-width: 0;
}

.ps-form-help {
    color: var(--ps-text-2);
    min-width: 0;
}

@media (max-width: 1100px) {
    .ps-form-split {
        grid-template-columns: minmax(0, 1fr);
    }
}

/* Ultrawide guard: a long single-line text field stops growing; tables and grids never do. */
@media (min-width: 2200px) {
    .ps-form-grid .ps-cap-ultrawide {
        max-width: 40rem;
    }
}

/* Entry-grid action bar sits at the right under the grid. */
.ps-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 0.5rem;
    width: 100%;
}

@media print {
    .ps-topbar,
    .ps-sidebar,
    .ps-drawer-scrim,
    .ps-skip-link {
        display: none !important;
    }

    .ps-shell {
        display: block;
    }

    .ps-main {
        padding: 0;
    }
}
