/* =========================================================================
   Agreement List — design system
   Tokens · theming · motion · layout · components
   ========================================================================= */

/* ------------------------------------------------------------- 1. Tokens */

:root {
    color-scheme: light;

    /* Brand + semantic colour */
    --brand-50: #eff6ff;
    --brand-100: #dbeafe;
    --brand-200: #bfdbfe;
    --brand-500: #3b82f6;
    --brand-600: #2563eb;
    --brand-700: #1d4ed8;
    --brand-900: #1e3a8a;

    --ok-50: #ecfdf5;
    --ok-200: #a7f3d0;
    --ok-600: #059669;
    --ok-700: #047857;

    --warn-50: #fffbeb;
    --warn-200: #fde68a;
    --warn-600: #d97706;
    --warn-700: #b45309;

    --danger-50: #fef2f2;
    --danger-200: #fecaca;
    --danger-600: #dc2626;
    --danger-700: #b91c1c;

    /* Surfaces + text */
    --canvas: #f6f7fb;
    --surface: #ffffff;
    --surface-2: #f8fafc;
    --surface-3: #f1f5f9;
    --border: #e5e8f0;
    --border-strong: #cbd5e1;

    --text: #0f172a;
    --text-2: #475569;
    --text-3: #64748b;
    --text-inv: #ffffff;

    --nav-bg: #0b1220;
    --nav-bg-2: #131c31;
    --nav-text: #a8b6cf;
    --nav-text-strong: #f1f5f9;

    /* Radii */
    --r-sm: 8px;
    --r: 12px;
    --r-lg: 16px;
    --r-full: 999px;

    /* Elevation */
    --shadow-xs: 0 1px 2px rgba(15, 23, 42, .06);
    --shadow-sm: 0 1px 2px rgba(15, 23, 42, .06), 0 2px 6px rgba(15, 23, 42, .05);
    --shadow: 0 2px 4px rgba(15, 23, 42, .04), 0 12px 28px rgba(15, 23, 42, .07);
    --shadow-lg: 0 8px 16px rgba(15, 23, 42, .08), 0 24px 48px rgba(15, 23, 42, .12);

    /* Motion */
    --dur-fast: 140ms;
    --dur: 220ms;
    --dur-slow: 300ms;
    --ease: cubic-bezier(.4, 0, .2, 1);
    --ease-out: cubic-bezier(.16, 1, .3, 1);

    /* Structure */
    --sidebar-w: 260px;
    --sidebar-rail-w: 74px;
    --navbar-h: 64px;
    --gap: 16px;
    --tap: 44px;

    --font: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
    /* Latin glyphs inside Khmer runs must stay Times, matching the Word file. */
    --font-khmer: "Agreement Khmer", "Khmer OS Battambang", "Times New Roman", "Noto Sans Khmer", serif;
    --font-muol: "Agreement Muol", "Kh Muol", serif;
    --font-mono: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;

    --z-backdrop: 20;
    --z-drawer: 30;
    --z-sticky: 40;
}

[data-theme="dark"] {
    color-scheme: dark;

    --canvas: #070b14;
    --surface: #0f1626;
    --surface-2: #141d30;
    --surface-3: #1b2540;
    --border: #23304d;
    --border-strong: #33405f;

    --text: #e8edf7;
    --text-2: #a8b6cf;
    --text-3: #7d8ca8;

    --brand-50: #16213b;
    --brand-100: #1e2f52;
    --brand-600: #3b82f6;
    --brand-700: #60a5fa;
    --brand-900: #bfdbfe;

    --ok-50: #06281f;
    --ok-200: #10493a;
    --ok-600: #34d399;
    --ok-700: #6ee7b7;

    --warn-50: #2c1f05;
    --warn-200: #55410c;
    --warn-600: #fbbf24;
    --warn-700: #fcd34d;

    --danger-50: #2c0f12;
    --danger-200: #5b1a20;
    --danger-600: #f87171;
    --danger-700: #fca5a5;

    --nav-bg: #05080f;
    --nav-bg-2: #0d1424;
    --nav-text: #8fa0bd;
    --nav-text-strong: #eef2f9;

    --shadow-xs: 0 1px 2px rgba(0, 0, 0, .5);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, .5), 0 2px 8px rgba(0, 0, 0, .35);
    --shadow: 0 4px 10px rgba(0, 0, 0, .45), 0 16px 36px rgba(0, 0, 0, .4);
    --shadow-lg: 0 12px 24px rgba(0, 0, 0, .5), 0 30px 60px rgba(0, 0, 0, .55);
}

/* ---------------------------------------------------------- 2. Resets */

*, *::before, *::after { box-sizing: border-box; }

[x-cloak] { display: none !important; }

html, body { margin: 0; padding: 0; }

html { -webkit-text-size-adjust: 100%; }

body {
    font-family: var(--font);
    font-size: 14px;
    line-height: 1.6;
    background: var(--canvas);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    transition: background var(--dur) var(--ease), color var(--dur) var(--ease);
}

a { color: var(--brand-600); text-decoration: none; }
a:hover { text-decoration: underline; }

img, svg { display: block; }
svg { flex: 0 0 auto; }

h1, h2, h3, h4, p { margin: 0; }

/* Visible, consistent keyboard focus everywhere. */
:focus-visible {
    outline: 2px solid var(--brand-600);
    outline-offset: 2px;
    border-radius: 4px;
}

::selection { background: var(--brand-200); color: var(--text); }

/* ------------------------------------------------------ 3. App shell */

.app {
    display: grid;
    grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
    grid-template-rows: var(--navbar-h) minmax(0, 1fr);
    grid-template-areas:
        "sidebar navbar"
        "sidebar main";
    min-height: 100vh;
    min-height: 100dvh;
}

/* ------------------------------------------------------ 4. Sidebar */

.sidebar {
    grid-area: sidebar;
    background: var(--nav-bg);
    color: var(--nav-text);
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    height: 100vh;
    height: 100dvh;
    min-width: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    z-index: var(--z-drawer);
}

.sidebar__brand {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 0 20px;
    min-height: var(--navbar-h);
    border-bottom: 1px solid var(--nav-bg-2);
    flex: 0 0 auto;
}

/* Dismiss control for the off-canvas drawer; the docked sidebar has no need for it. */
.sidebar__close {
    display: none;
    color: var(--nav-text);
    background: rgba(255, 255, 255, .06);
    border-color: rgba(255, 255, 255, .12);
    flex: 0 0 auto;
}
.sidebar__close:hover { background: rgba(255, 255, 255, .13); color: var(--nav-text-strong); }
.sidebar__close:focus-visible { outline-color: var(--nav-text-strong); }

.sidebar__mark {
    width: 34px;
    height: 34px;
    border-radius: 10px;
    background: linear-gradient(140deg, var(--brand-500), var(--brand-700));
    display: grid;
    place-items: center;
    color: #fff;
    box-shadow: 0 4px 12px rgba(37, 99, 235, .4);
    flex: 0 0 auto;
}

.sidebar__title {
    font-weight: 650;
    color: var(--nav-text-strong);
    font-size: 14px;
    letter-spacing: -.01em;
    line-height: 1.25;
}

.sidebar__subtitle {
    font-size: 10.5px;
    color: var(--nav-text);
    opacity: .65;
    letter-spacing: .09em;
    text-transform: uppercase;
}

.sidebar__nav {
    padding: 14px 12px 20px;
    display: flex;
    flex-direction: column;
    gap: 2px;
    flex: 1 1 auto;
}

.sidebar__section {
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: .11em;
    color: var(--nav-text);
    opacity: .55;
    padding: 18px 12px 7px;
    font-weight: 700;
}

.sidebar__link {
    display: flex;
    align-items: center;
    gap: 11px;
    padding: 10px 12px;
    min-height: 42px;
    border-radius: var(--r-sm);
    color: var(--nav-text);
    font-size: 13.5px;
    font-weight: 500;
    position: relative;
    cursor: pointer;
    transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}

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

.sidebar__link:focus-visible { outline-color: var(--brand-500); }

.sidebar__link.is-active {
    background: linear-gradient(90deg, var(--brand-600), var(--brand-700));
    color: #fff;
    box-shadow: 0 4px 14px rgba(37, 99, 235, .35);
}

.sidebar__link.is-active::before {
    content: "";
    position: absolute;
    left: -12px;
    top: 50%;
    transform: translateY(-50%);
    width: 3px;
    height: 22px;
    border-radius: 0 3px 3px 0;
    background: var(--brand-500);
}

.sidebar__count {
    margin-left: auto;
    background: rgba(255, 255, 255, .1);
    color: inherit;
    border-radius: var(--r-full);
    padding: 2px 9px;
    font-size: 11px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.sidebar__link.is-active .sidebar__count { background: rgba(255, 255, 255, .22); }

.sidebar__count--warn { background: rgba(251, 191, 36, .16); color: #fbbf24; }
.sidebar__link.is-active .sidebar__count--warn { background: rgba(255, 255, 255, .22); color: #fff; }

.sidebar__link > svg { opacity: .82; transition: opacity var(--dur-fast) var(--ease); }
.sidebar__link:hover > svg, .sidebar__link.is-active > svg { opacity: 1; }

/* Collapsible groups — Agreements, Administration */
.sidebar__group { margin: 2px 0; }

.sidebar__group-toggle {
    display: flex;
    align-items: center;
    gap: 11px;
    padding: 10px 12px;
    min-height: 42px;
    border-radius: var(--r-sm);
    color: var(--nav-text);
    font-size: 13.5px;
    font-weight: 500;
    cursor: pointer;
    list-style: none;
    position: relative;
    user-select: none;
    transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}

.sidebar__group-toggle::-webkit-details-marker { display: none; }

.sidebar__group-toggle:hover {
    background: var(--nav-bg-2);
    color: var(--nav-text-strong);
}

.sidebar__group-toggle:focus-visible { outline-color: var(--brand-500); }

.sidebar__group-toggle > svg { opacity: .82; transition: opacity var(--dur-fast) var(--ease); }
.sidebar__group-toggle:hover > svg,
.sidebar__group.has-active > .sidebar__group-toggle > svg { opacity: 1; }

.sidebar__group.has-active > .sidebar__group-toggle { color: var(--nav-text-strong); }

.sidebar__group-label { flex: 1 1 auto; }

.sidebar__group-toggle > .sidebar__group-chevron {
    opacity: .5;
    transition: transform var(--dur-fast) var(--ease), opacity var(--dur-fast) var(--ease);
}

.sidebar__group-toggle:hover > .sidebar__group-chevron { opacity: .85; }

.sidebar__group[open] > .sidebar__group-toggle > .sidebar__group-chevron { transform: rotate(180deg); }

.sidebar__group-body {
    display: flex;
    flex-direction: column;
    gap: 2px;
    margin: 2px 0 6px 20px;
    padding-left: 10px;
    border-left: 1px solid rgba(255, 255, 255, .1);
}

.sidebar__group-body .sidebar__link {
    min-height: 38px;
    padding: 8px 10px;
    font-size: 13px;
}

/* -------------------------------------------------------- 5. Navbar */

.navbar {
    grid-area: navbar;
    background: color-mix(in srgb, var(--surface) 82%, transparent);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 0 clamp(14px, 2vw, 24px);
    position: sticky;
    top: 0;
    z-index: var(--z-sticky);
}

.navbar__heading {
    font-size: 15px;
    font-weight: 650;
    letter-spacing: -.015em;
    line-height: 1.25;
}

.navbar__titles { min-width: 0; }

.navbar__heading { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.navbar__spacer { flex: 1 1 auto; }

.icon-btn {
    display: inline-grid;
    place-items: center;
    width: var(--tap);
    height: var(--tap);
    border-radius: var(--r-sm);
    border: 1px solid transparent;
    background: transparent;
    color: var(--text-2);
    cursor: pointer;
    transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease),
                border-color var(--dur-fast) var(--ease);
}

.icon-btn:hover { background: var(--surface-3); color: var(--text); }
.icon-btn:active { transform: translateY(1px); }

/* Show only the icon for the active theme, before JavaScript runs. */
[data-theme="light"] [data-theme-toggle] [data-icon="dark"],
[data-theme="dark"] [data-theme-toggle] [data-icon="light"] { display: none; }

.navbar__toggle { display: none; }
.navbar__panel-toggle { display: none; }

.navbar__search { position: relative; width: min(300px, 34vw); flex: 0 0 auto; }

.navbar__search svg {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-3);
    pointer-events: none;
}

.navbar__search input {
    width: 100%;
    height: 40px;
    padding: 0 14px 0 38px;
    border: 1px solid var(--border);
    border-radius: var(--r-full);
    font: inherit;
    font-size: 13px;
    background: var(--surface-2);
    color: var(--text);
    transition: border-color var(--dur-fast) var(--ease), background var(--dur-fast) var(--ease),
                box-shadow var(--dur-fast) var(--ease);
}

.navbar__search input::placeholder { color: var(--text-3); }

.navbar__search input:focus {
    outline: none;
    border-color: var(--brand-600);
    background: var(--surface);
    box-shadow: 0 0 0 4px color-mix(in srgb, var(--brand-600) 15%, transparent);
}

.navbar__avatar {
    width: 36px;
    height: 36px;
    border-radius: var(--r-full);
    background: linear-gradient(140deg, var(--brand-500), var(--brand-700));
    color: #fff;
    display: grid;
    place-items: center;
    font-weight: 700;
    font-size: 12px;
    letter-spacing: .02em;
    flex: 0 0 auto;
}

/* ------------------------------------------------------ 6. Content */

.main {
    grid-area: main;
    padding: clamp(18px, 2.4vw, 30px) clamp(14px, 2.4vw, 32px) 72px;
    min-width: 0;
}

.page-head {
    display: flex;
    align-items: flex-start;
    gap: 16px;
    margin-bottom: 22px;
    flex-wrap: wrap;
}

.page-head__text { min-width: 0; flex: 1 1 320px; }

.page-title {
    font-size: clamp(20px, 2.4vw, 25px);
    font-weight: 700;
    letter-spacing: -.025em;
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}

.page-head__actions {
    margin-left: auto;
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

/* ------------------------------------------------------ 7. Cards */

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--r-lg);
    box-shadow: var(--shadow-sm);
    transition: box-shadow var(--dur) var(--ease), border-color var(--dur) var(--ease),
                transform var(--dur) var(--ease);
}

.card--lift:hover {
    box-shadow: var(--shadow);
    border-color: var(--border-strong);
    transform: translateY(-2px);
}

.card__head {
    padding: 15px 18px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 11px;
    flex-wrap: wrap;
}

.card__title {
    font-size: 14px;
    font-weight: 650;
    display: flex;
    align-items: center;
    gap: 8px;
}

.card__title svg { color: var(--brand-600); }

.card__hint { font-size: 12.5px; color: var(--text-3); }

.card__body { padding: 18px; }

.grid { display: grid; gap: var(--gap); }
.grid--stats { grid-template-columns: repeat(auto-fit, minmax(min(100%, 215px), 1fr)); }
.grid--2 { grid-template-columns: repeat(auto-fit, minmax(min(100%, 340px), 1fr)); }
.grid--side { grid-template-columns: minmax(0, 1.35fr) minmax(0, 1fr); }

@media (max-width: 1100px) { .grid--side { grid-template-columns: minmax(0, 1fr); } }

/* Stat tiles */

.stat {
    position: relative;
    overflow: hidden;
}

.stat::after {
    content: "";
    position: absolute;
    inset: 0 auto 0 0;
    width: 3px;
    background: var(--brand-600);
    opacity: .9;
}

.stat--ok::after { background: var(--ok-600); }
.stat--warn::after { background: var(--warn-600); }
.stat--danger::after { background: var(--danger-600); }

.stat__label {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: .08em;
    color: var(--text-3);
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 7px;
}

.stat__label svg { color: var(--brand-600); }
.stat--ok .stat__label svg { color: var(--ok-600); }
.stat--warn .stat__label svg { color: var(--warn-600); }

.stat__value {
    font-size: clamp(24px, 3vw, 30px);
    font-weight: 700;
    letter-spacing: -.03em;
    margin-top: 8px;
    font-variant-numeric: tabular-nums;
    line-height: 1.1;
    overflow-wrap: break-word;
}

.stat__note { font-size: 12px; color: var(--text-2); margin-top: 3px; }

/* ------------------------------------------------------ 8. Tables */

/*
 * The register is the widest thing in the app, and the off-canvas sidebar changes how
 * much room it actually has. A container query keys the collapse to that real content
 * width instead of the viewport, so the table never overflows its actions column.
 */
.table-wrap { overflow-x: auto; container-type: inline-size; }

.table { width: 100%; border-collapse: collapse; font-size: 13px; }

.table th {
    text-align: left;
    font-size: 10.5px;
    text-transform: uppercase;
    letter-spacing: .07em;
    color: var(--text-3);
    font-weight: 700;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
    background: var(--surface-2);
}

.table td { padding: 14px 16px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tbody tr { transition: background var(--dur-fast) var(--ease); }
.table tbody tr:last-child td { border-bottom: none; }
.table tbody tr:nth-child(even) { background: var(--surface-2); }
.table tbody tr:hover { background: var(--surface-3); }

.table__strong { font-weight: 650; }
.table__muted { color: var(--text-2); font-size: 12.5px; }
.table__mono { font-family: var(--font-mono); font-size: 12px; }
.table__right { text-align: right; }

/* Sortable column headers */
.table__sortable { padding: 0; }

.sort-link {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 12px 16px;
    color: inherit;
    font: inherit;
    letter-spacing: inherit;
    text-transform: inherit;
    text-decoration: none;
    white-space: nowrap;
    transition: color var(--dur-fast) var(--ease);
}

.sort-link:hover { color: var(--text); }
.sort-link.is-active { color: var(--brand-600); }

.sort-link__icon {
    flex: 0 0 auto;
    opacity: .45;
    transition: opacity var(--dur-fast) var(--ease);
}

.sort-link:hover .sort-link__icon { opacity: .85; }
.sort-link.is-active .sort-link__icon { opacity: 1; }

.table__right .sort-link { width: 100%; justify-content: flex-end; }

.row-actions { display: flex; gap: 6px; justify-content: flex-end; }

/* Cards-on-narrow: the table collapses into stacked records. */
@container (max-width: 980px) {
    .table, .table tbody, .table tr, .table td { display: block; width: 100%; }
    .table thead { display: none; }

    .table tr {
        border-bottom: 1px solid var(--border);
        padding: 14px 16px;
        transition: background var(--dur-fast) var(--ease);
    }

    .table tr:last-child { border-bottom: none; }

    /*
     * Grid, not flex: the label owns column 1 and every value line stacks underneath it
     * in column 2, so a long company name wraps cleanly instead of colliding with the
     * name beside it.
     */
    .table td {
        border: none;
        padding: 4px 0;
        display: grid;
        grid-template-columns: auto minmax(0, 1fr);
        align-items: baseline;
        gap: 3px 12px;
    }

    .table td::before {
        content: attr(data-label);
        grid-column: 1;
        grid-row: 1;
        align-self: center;
        font-size: 10.5px;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: .06em;
        color: var(--text-3);
    }

    .table td > * { grid-column: 2; text-align: right; min-width: 0; }
    .table td > .badge { justify-self: end; }
    .table td > .table__muted { justify-content: flex-end; }
    .table td > .permission-roles { justify-content: flex-end; }
    .table td > .permission-code { justify-self: end; }

    /* Actions get the full card width so each button is a comfortable tap target. */
    .table td > .row-actions {
        grid-column: 1 / -1;
        justify-content: stretch;
        margin-top: 10px;
    }
    .row-actions { justify-content: flex-end; }
    .row-actions .btn { flex: 1 1 0; justify-content: center; }
}

/* Fallback for engines without container queries: collapse on a plain viewport width. */
@supports not (container-type: inline-size) {
    @media (max-width: 1280px) {
        .table, .table tbody, .table tr, .table td { display: block; width: 100%; }
        .table thead { display: none; }
        .table tr { border-bottom: 1px solid var(--border); padding: 14px 16px; }
        .table tr:last-child { border-bottom: none; }
        .table td {
            border: none;
            padding: 4px 0;
            display: grid;
            grid-template-columns: auto minmax(0, 1fr);
            align-items: baseline;
            gap: 3px 12px;
        }
        .table td::before {
            content: attr(data-label);
            grid-column: 1;
            grid-row: 1;
            align-self: center;
            font-size: 10.5px;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: .06em;
            color: var(--text-3);
        }
        .table td > * { grid-column: 2; text-align: right; min-width: 0; }
        .table td > .badge { justify-self: end; }
        .table td > .table__muted { justify-content: flex-end; }
        .table td > .permission-roles { justify-content: flex-end; }
        .table td > .permission-code { justify-self: end; }
        .table td > .row-actions { grid-column: 1 / -1; justify-content: stretch; margin-top: 10px; }
        .row-actions { justify-content: flex-end; }
        .row-actions .btn { flex: 1 1 0; justify-content: center; }
    }
}

/* On tablets the stacked records keep their natural button width. */
@media (min-width: 561px) {
    .table td > .row-actions .btn { flex: 0 1 auto; }
}

/*
 * Agreements get a purpose-built card view instead of the generic stacked rows:
 * reference + status in the header, subtenant underneath, term and monthly as
 * side-by-side stats, actions pinned to the bottom.
 */
@container (max-width: 980px) {
    .table.table--agreements { display: block; width: 100%; }

    .table.table--agreements thead { display: none; }

    .table.table--agreements tbody {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 14px;
        padding: 14px;
    }

    .table.table--agreements tr,
    .table.table--agreements tbody tr:nth-child(even),
    .table.table--agreements tbody tr:hover {
        display: grid;
        grid-template-columns: minmax(0, 1fr) auto;
        grid-template-areas:
            "reference status"
            "subtenant subtenant"
            "term monthly"
            "actions actions";
        gap: 12px 16px;
        align-items: start;
        padding: 16px;
        border: 1px solid var(--border);
        border-radius: var(--r);
        background: var(--surface);
        box-shadow: var(--shadow-xs);
    }

    .table.table--agreements td {
        display: block;
        padding: 0;
        border: 0;
        text-align: left;
    }

    .table.table--agreements td > * { text-align: left; }

    .table.table--agreements td::before { display: none; }

    .table.table--agreements td:nth-child(1) { grid-area: reference; }
    .table.table--agreements td:nth-child(2) { grid-area: subtenant; }
    .table.table--agreements td:nth-child(3) { grid-area: term; }
    .table.table--agreements td:nth-child(4) { grid-area: monthly; }
    .table.table--agreements td:nth-child(5) { grid-area: status; justify-self: end; }
    .table.table--agreements td:nth-child(6) { grid-area: actions; }

    .table.table--agreements .table__muted { justify-content: flex-start; }

    .table.table--agreements td[data-label="Subtenant"] {
        padding-bottom: 12px;
        border-bottom: 1px dashed var(--border);
    }

    .table.table--agreements td[data-label="Subtenant"] .table__strong { font-size: 15px; }

    .table.table--agreements td[data-label="Term"],
    .table.table--agreements td[data-label="Monthly"] {
        padding: 10px 12px;
        border-radius: var(--r-sm);
        background: var(--surface-2);
    }

    .table.table--agreements td[data-label="Term"]::before,
    .table.table--agreements td[data-label="Monthly"]::before {
        display: block;
        content: attr(data-label);
        margin-bottom: 5px;
        font-size: 10px;
        font-weight: 700;
        letter-spacing: .07em;
        text-transform: uppercase;
        color: var(--text-3);
    }

    .table.table--agreements td[data-label="Term"] .nowrap { white-space: normal; }

    .table.table--agreements td[data-label="Actions"] {
        padding-top: 12px;
        border-top: 1px dashed var(--border);
    }

    .table.table--agreements .row-actions { margin-top: 0; justify-content: stretch; }
    .table.table--agreements .row-actions .btn { flex: 1 1 0; justify-content: center; }
}

@container (max-width: 700px) {
    .table.table--agreements tbody { grid-template-columns: minmax(0, 1fr); }
}

/* ------------------------------------------------------ 9. Badges */

.badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 3px 10px;
    border-radius: var(--r-full);
    font-size: 11.5px;
    font-weight: 650;
    white-space: nowrap;
    border: 1px solid transparent;
}

.badge svg { width: 13px; height: 13px; }

.badge--draft { background: var(--surface-3); color: var(--text-2); border-color: var(--border); }
.badge--in_review { background: var(--warn-50); color: var(--warn-700); border-color: var(--warn-200); }
.badge--approved { background: var(--brand-50); color: var(--brand-900); border-color: var(--brand-200); }
.badge--active { background: var(--ok-50); color: var(--ok-700); border-color: var(--ok-200); }
.badge--expired, .badge--terminated { background: var(--danger-50); color: var(--danger-700); border-color: var(--danger-200); }

/* ---------------------------------------------------- 10. Buttons */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 9px 15px;
    min-height: 40px;
    border-radius: var(--r-sm);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font: inherit;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
    transition: background var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease),
                color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease),
                transform var(--dur-fast) var(--ease);
}

.btn:hover { background: var(--surface-2); border-color: var(--border-strong); text-decoration: none; }
.btn:active { transform: translateY(1px); }

.btn--primary {
    background: var(--brand-600);
    border-color: var(--brand-600);
    color: #fff;
    box-shadow: 0 2px 8px rgba(37, 99, 235, .28);
}

.btn--primary:hover { background: var(--brand-700); border-color: var(--brand-700); box-shadow: 0 4px 14px rgba(37, 99, 235, .36); }

.btn--ghost { background: transparent; border-color: transparent; color: var(--text-2); }
.btn--ghost:hover { background: var(--surface-3); color: var(--text); }

.btn--danger { color: var(--danger-700); border-color: var(--danger-200); }
.btn--danger:hover { background: var(--danger-50); border-color: var(--danger-600); }

.btn--sm { padding: 6px 11px; min-height: 34px; font-size: 12.5px; }

@media (max-width: 720px) {
    .btn { min-height: var(--tap); }
}

/* ------------------------------------------------------ 11. Forms */

.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 240px), 1fr)); gap: 16px; }

.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field--wide { grid-column: 1 / -1; }

.field label {
    font-size: 12.5px;
    font-weight: 650;
    color: var(--text-2);
    display: flex;
    align-items: center;
    gap: 4px;
}

.field label .req { color: var(--danger-600); }

.field input, .field select, .field textarea {
    font: inherit;
    font-size: 13.5px;
    padding: 10px 12px;
    min-height: 42px;
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    background: var(--surface);
    color: var(--text);
    width: 100%;
    transition: border-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
}

.field textarea { resize: vertical; min-height: 78px; line-height: 1.6; }

.field input::placeholder, .field textarea::placeholder { color: var(--text-3); }

.field input:focus, .field select:focus, .field textarea:focus {
    outline: none;
    border-color: var(--brand-600);
    box-shadow: 0 0 0 4px color-mix(in srgb, var(--brand-600) 14%, transparent);
}

.field input.has-error, .field select.has-error, .field textarea.has-error { border-color: var(--danger-600); }

.field__hint { font-size: 11.5px; color: var(--text-3); }
.field__error { font-size: 11.5px; color: var(--danger-700); font-weight: 600; }

/* Password reveal */
.password-field { position: relative; }

.password-field input { padding-right: 46px; }

/* The custom toggle replaces Edge's built-in reveal control. */
.password-field input::-ms-reveal,
.password-field input::-ms-clear { display: none; }

.password-field__toggle {
    position: absolute;
    top: 50%;
    right: 3px;
    transform: translateY(-50%);
    width: 36px;
    height: 36px;
    display: grid;
    place-items: center;
    padding: 0;
    border: 0;
    border-radius: var(--r-sm);
    background: transparent;
    color: var(--text-3);
    cursor: pointer;
    transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}

.password-field__toggle:hover { background: var(--surface-3); color: var(--text); }

.password-field__toggle[aria-pressed="true"] { color: var(--brand-600); }

.password-field__toggle [data-icon="hide"] { display: none; }
.password-field__toggle[aria-pressed="true"] [data-icon="show"] { display: none; }
.password-field__toggle[aria-pressed="true"] [data-icon="hide"] { display: block; }

.fieldset { border: none; padding: 0; margin: 0 0 28px; }

.fieldset__legend {
    font-size: 11.5px;
    font-weight: 750;
    text-transform: uppercase;
    letter-spacing: .09em;
    color: var(--brand-900);
    padding-bottom: 10px;
    margin-bottom: 16px;
    border-bottom: 1px solid var(--border);
    width: 100%;
    display: flex;
    align-items: center;
    gap: 9px;
}

.fieldset__legend svg { color: var(--brand-600); }

.form-actions {
    display: flex;
    gap: 10px;
    align-items: center;
    flex-wrap: wrap;
    padding-top: 18px;
    border-top: 1px solid var(--border);
}

.form-actions .spacer { flex: 1 1 auto; }

@media (max-width: 560px) {
    .form-actions .btn { flex: 1 1 auto; }
}

/* ---------------------------------------------------- 12. Notices */

.notice {
    display: flex;
    align-items: flex-start;
    gap: 11px;
    padding: 13px 16px;
    border-radius: var(--r);
    font-size: 13px;
    font-weight: 550;
    margin-bottom: 18px;
    border: 1px solid transparent;
    animation: slide-in var(--dur-slow) var(--ease-out) both;
}

.notice svg { flex: 0 0 auto; margin-top: 1px; }
.notice ul { margin: 6px 0 0; padding-left: 18px; }

.notice--ok { background: var(--ok-50); color: var(--ok-700); border-color: var(--ok-200); }
.notice--error { background: var(--danger-50); color: var(--danger-700); border-color: var(--danger-200); }
.notice--warn { background: var(--warn-50); color: var(--warn-700); border-color: var(--warn-200); }

/* ---------------------------------------------------- 13. Checklist */

.checklist {
    display: grid;
    gap: 1px;
    background: var(--border);
    border-radius: var(--r);
    overflow: hidden;
}

.check {
    display: flex;
    align-items: center;
    gap: 11px;
    padding: 11px 14px;
    background: var(--surface);
    font-size: 12.5px;
    min-height: var(--tap);
    transition: background var(--dur-fast) var(--ease);
}

.check:hover { background: var(--surface-2); }

.check__dot {
    width: 20px;
    height: 20px;
    border-radius: var(--r-full);
    display: grid;
    place-items: center;
    flex: 0 0 auto;
}

.check__dot.badge { padding: 0; }

.check--done .check__dot { background: var(--ok-50); color: var(--ok-700); }
.check--missing .check__dot { background: var(--danger-50); color: var(--danger-700); }

.check__label { color: var(--text-2); flex: 0 0 auto; width: 176px; }
.check__value { color: var(--text); font-weight: 600; word-break: break-word; min-width: 0; }
.check--missing .check__value { color: var(--danger-700); font-style: italic; font-weight: 500; }

@media (max-width: 560px) {
    .check { flex-wrap: wrap; }
    .check__label { width: auto; flex: 1 1 100%; }
    .check__value { flex: 1 1 100%; }

    /* Dashboard deadline rows: code and date stack, action gets its own line. */
    .check--event {
        display: grid;
        grid-template-columns: auto minmax(0, 1fr);
        column-gap: 11px;
        row-gap: 3px;
    }

    .check--event .check__dot { grid-row: 1 / span 2; align-self: start; }

    .check--event .check__label {
        grid-column: 2;
        grid-row: 1;
        width: auto;
        flex: none;
    }

    .check--event .check__date {
        grid-column: 2;
        grid-row: 2;
        white-space: normal;
    }

    .check--event .btn {
        grid-column: 1 / -1;
        width: 100%;
        margin-top: 7px;
    }
}

/* ------------------------------------------------- 14. Doc preview */

/* The preview mirrors the .docx typography: Khmer OS Battambang for normal and
   bold Khmer, Kh Muol only in the places the Word file uses it, Times New Roman
   for English. */
@font-face {
    font-family: "Agreement Khmer";
    src: local("Khmer OS Battambang");
    font-weight: 400;
}

@font-face {
    font-family: "Agreement Khmer";
    src: local("Khmer OS Battambang Bold"), local("Khmer OS Battambang");
    font-weight: 700;
}

@font-face {
    font-family: "Agreement Muol";
    src: url("../fonts/kh-muol.ttf") format("truetype");
    font-weight: 400;
    font-display: block;
}

.doc-toolbar {
    display: flex;
    align-items: center;
    gap: 9px;
    padding: 12px 16px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--r-lg);
    margin-bottom: 18px;
    position: sticky;
    top: calc(var(--navbar-h) + 10px);
    z-index: 25;
    flex-wrap: wrap;
    box-shadow: var(--shadow-sm);
}

.doc-toolbar__title { font-weight: 650; font-size: 13.5px; display: flex; align-items: center; gap: 8px; }
.doc-toolbar__title svg { color: var(--brand-600); }
.doc-toolbar .spacer { flex: 1 1 auto; }

@media (max-width: 720px) {
    .doc-toolbar { position: static; }
}

.doc {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--r-lg);
    box-shadow: var(--shadow);
    max-width: 880px;
    margin: 0 auto;
    padding: clamp(28px, 5vw, 58px) clamp(20px, 5vw, 64px) clamp(36px, 6vw, 72px);
    overflow-wrap: break-word;
    /* Khmer glyphs inside English runs must resolve to Battambang, not a system fallback. */
    font-family: "Times New Roman", "Agreement Khmer", "Khmer OS Battambang", Times, serif;
}

.doc__title { text-align: center; margin-bottom: 28px; }
.doc__title .km { font-family: var(--font-muol); font-size: 18px; font-weight: 400; }
.doc__title .en { font-size: clamp(18px, 2.4vw, 22px); font-weight: 700; margin-top: 4px; }

.doc p { margin: 0 0 12px; font-size: 13.5px; line-height: 1.85; }
.doc p.km { font-family: var(--font-khmer); line-height: 2.05; }
.doc p.role { font-weight: 700; margin-bottom: 5px; }

.doc .value {
    font-weight: 700;
    color: inherit;
}

.doc .km.km-muol { font-family: var(--font-muol); }

.doc__clause { margin-bottom: 18px; }
.doc__clause-head { font-weight: 700; margin-bottom: 5px; }

.doc__signatures {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr));
    gap: 28px;
    margin-top: 36px;
    padding-top: 24px;
    border-top: 1px solid var(--border);
}

.sig__role { font-weight: 700; font-size: 13px; margin-bottom: 32px; }
.sig__line { border-bottom: 1px solid var(--border-strong); height: 42px; }
.sig__name { font-weight: 650; margin-top: 10px; }
.sig__company { color: var(--text-2); font-size: 12.5px; }
.sig__title { font-family: var(--font-khmer); color: var(--text-2); font-size: 12.5px; }
.sig__date { font-size: 12.5px; color: var(--text-2); margin-top: 16px; }

.doc__appendix { margin-top: 42px; padding-top: 24px; border-top: 1px solid var(--border); }
.doc__appendix h3 { font-size: 15px; margin: 0 0 4px; }
.doc__appendix h3.km { font-weight: 400; }
.doc__appendix h4 { font-size: 12.5px; margin: 18px 0 10px; color: var(--text-2); font-weight: 650; }

.appendix-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.appendix-table td {
    padding: 8px 10px;
    border-bottom: 1px solid var(--border);
    vertical-align: top;
    overflow-wrap: anywhere;
}
.appendix-table td:first-child { width: 42%; color: var(--text-2); font-family: var(--font-khmer); }
.appendix-table tr.en td:first-child { font-family: "Times New Roman", Times, serif; font-size: 12px; }

/* ---------------------------------------------------- 15. Utilities */

.muted { color: var(--text-3); }
.soft { color: var(--text-2); }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
.km { font-family: var(--font-khmer); }
.km-muol { font-family: var(--font-muol); }
.stack { display: grid; gap: var(--gap); }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

/* Icon that sits inside a run of text rather than in a flex row. */
.inline {
    display: inline-block;
    vertical-align: -2px;
    margin-right: 3px;
}

.table__muted, .stat__label { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }

.filters { display: flex; gap: 9px; align-items: center; flex-wrap: wrap; flex: 1 1 520px; min-width: 0; }

.filters input, .filters select {
    font: inherit;
    font-size: 13px;
    padding: 9px 12px;
    min-height: 40px;
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
}

.filters > input[type="search"] { flex: 1 1 220px; min-width: 0; }

.filters input:focus, .filters select:focus {
    outline: none;
    border-color: var(--brand-600);
    box-shadow: 0 0 0 4px color-mix(in srgb, var(--brand-600) 14%, transparent);
}

/* Alpine UI listbox: "Show N per page" */
.per-page {
    position: relative;
    display: flex;
    align-items: center;
    gap: 7px;
    flex: 0 0 auto;
    margin-left: auto;
    font-size: 12.5px;
    color: var(--text-2);
    white-space: nowrap;
}

.per-page__label { font-weight: 550; }
.per-page__control { position: relative; }

.per-page__button {
    display: inline-flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    min-width: 88px;
    min-height: 40px;
    padding: 8px 11px;
    font: inherit;
    font-size: 13px;
    font-weight: 650;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    cursor: pointer;
    transition: border-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
}

.per-page__button:hover { border-color: var(--border-strong); }

.per-page__button:focus-visible {
    outline: none;
    border-color: var(--brand-600);
    box-shadow: 0 0 0 4px color-mix(in srgb, var(--brand-600) 14%, transparent);
}

.per-page__chevron { color: var(--text-3); transition: transform var(--dur-fast) var(--ease); }
.per-page__button[aria-expanded="true"] .per-page__chevron { transform: rotate(180deg); }

.per-page__menu {
    position: absolute;
    top: calc(100% + 6px);
    left: 0;
    z-index: 40;
    width: 100%;
    min-width: 88px;
    margin: 0;
    padding: 5px;
    list-style: none;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    box-shadow: var(--shadow);
}

.per-page__menu:focus { outline: none; }

.per-page__option {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 8px 10px;
    border-radius: 6px;
    color: var(--text);
    font-size: 13px;
    font-weight: 550;
    cursor: pointer;
}

.per-page__option.is-active { background: var(--surface-3); }
.per-page__option.is-selected { color: var(--brand-600); font-weight: 700; }
.per-page__check { display: inline-flex; }

.empty { padding: 54px 22px; text-align: center; color: var(--text-3); }

.empty__icon {
    width: 56px;
    height: 56px;
    border-radius: var(--r-lg);
    background: var(--surface-3);
    color: var(--text-3);
    display: grid;
    place-items: center;
    margin: 0 auto 14px;
}

.empty__title { font-weight: 650; color: var(--text); margin-bottom: 4px; font-size: 14px; }

.table-foot {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px 16px;
    flex-wrap: wrap;
    padding: 11px 18px;
    border-top: 1px solid var(--border);
}

.table-foot .per-page { margin-left: 0; }
.table-foot .pagination { padding: 0; }

/* The selector lives at the bottom of the card, so its menu opens upward. */
.table-foot .per-page__menu {
    top: auto;
    bottom: calc(100% + 6px);
}

.pagination { display: flex; gap: 6px; padding: 15px 18px; flex-wrap: wrap; align-items: center; }

.pagination a, .pagination span {
    padding: 7px 13px;
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    font-size: 12.5px;
    color: var(--text-2);
    min-width: 38px;
    text-align: center;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.pagination .active span {
    background: var(--brand-600);
    border-color: var(--brand-600);
    color: #fff;
    font-weight: 650;
}

/* ------------------------------------------------- 16. Animation */

@keyframes fade-up {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: none; }
}

@keyframes slide-in {
    from { opacity: 0; transform: translateY(-8px); }
    to { opacity: 1; transform: none; }
}

@keyframes drawer-in {
    from { transform: translateX(-100%); }
    to { transform: translateX(0); }
}

@keyframes backdrop-in {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes pulse-ring {
    0% { box-shadow: 0 0 0 0 rgba(37, 99, 235, .35); }
    70% { box-shadow: 0 0 0 10px rgba(37, 99, 235, 0); }
    100% { box-shadow: 0 0 0 0 rgba(37, 99, 235, 0); }
}

.animate-in { animation: fade-up var(--dur-slow) var(--ease-out) both; }
.animate-in:nth-child(1) { animation-delay: 0ms; }
.animate-in:nth-child(2) { animation-delay: 45ms; }
.animate-in:nth-child(3) { animation-delay: 90ms; }
.animate-in:nth-child(4) { animation-delay: 135ms; }

/* Respect a saved "reduce motion" preference before the first frame. */
@media (prefers-reduced-motion: reduce) {
    .animate-in { animation: none; }
}

.backdrop {
    position: fixed;
    inset: 0;
    background: rgba(8, 12, 22, .55);
    backdrop-filter: blur(2px);
    z-index: var(--z-backdrop);
    opacity: 0;
    visibility: hidden;
    transition: opacity var(--dur) var(--ease), visibility var(--dur) var(--ease);
}

.backdrop.is-open { opacity: 1; visibility: visible; animation: backdrop-in var(--dur) var(--ease); }

/* ------------------------------------------------- 17. Responsive */

/*
 * The docked sidebar collapses to an icon rail from the navbar; the choice is remembered.
 * The rail keeps every destination reachable — labels step aside and the tooltips
 * (title attributes) carry the wording instead of hiding the navigation altogether.
 */
@media (min-width: 901px) {
    .navbar__panel-toggle { display: inline-grid; }

    /* The drawer's dismiss control has no place on the docked sidebar. */
    .sidebar__close { display: none; }

    .app { transition: grid-template-columns var(--dur) var(--ease); }

    html.is-sidebar-collapsed .app { grid-template-columns: var(--sidebar-rail-w) minmax(0, 1fr); }

    html.is-sidebar-collapsed .sidebar { overflow-x: hidden; }

    html.is-sidebar-collapsed .sidebar__brand { justify-content: center; padding: 0 12px; }

    html.is-sidebar-collapsed .sidebar__brand-text,
    html.is-sidebar-collapsed .sidebar__link > span:not(.sidebar__count),
    html.is-sidebar-collapsed .sidebar__group-label,
    html.is-sidebar-collapsed .sidebar__group-chevron,
    html.is-sidebar-collapsed .sidebar__group-body { display: none; }

    /* Group headings turn into dividers so the sections stay readable without words. */
    html.is-sidebar-collapsed .sidebar__section {
        font-size: 0;
        height: 1px;
        padding: 0;
        margin: 16px 22px 12px;
        background: currentColor;
        opacity: .16;
    }

    html.is-sidebar-collapsed .sidebar__link,
    html.is-sidebar-collapsed .sidebar__group-toggle { justify-content: center; padding: 10px; }

    /* A group that owns the current page keeps a highlight while the rail is closed. */
    html.is-sidebar-collapsed .sidebar__group.has-active > .sidebar__group-toggle {
        background: rgba(255, 255, 255, .1);
        color: var(--nav-text-strong);
    }

    /* Counts shrink to a dot pinned to the icon's corner; the tooltip keeps the number. */
    html.is-sidebar-collapsed .sidebar__count {
        position: absolute;
        top: 6px;
        right: 50%;
        margin: 0 -14px 0 0;
        min-width: 9px;
        height: 9px;
        padding: 0;
        font-size: 0;
        background: var(--brand-500);
        box-shadow: 0 0 0 2px var(--nav-bg);
    }

    html.is-sidebar-collapsed .sidebar__count--warn { background: #fbbf24; }
}

@media (prefers-reduced-motion: reduce) {
    .app { transition: none; }
}

@media (max-width: 1024px) {
    :root { --sidebar-w: 236px; }
}

@media (max-width: 900px) {
    /*
     * The drawer is fixed to the top of the viewport, so the sticky navbar would paint
     * over its brand block. Lift the scrim and the drawer above the navbar instead, and
     * keep the drawer above its own scrim.
     */
    :root {
        --z-backdrop: calc(var(--z-sticky) + 1);
        --z-drawer: calc(var(--z-sticky) + 2);
    }

    .app {
        grid-template-columns: minmax(0, 1fr);
        grid-template-areas: "navbar" "main";
    }

    .sidebar {
        position: fixed;
        inset: 0 auto 0 0;
        width: min(288px, 84vw);
        transform: translateX(-100%);
        transition: transform var(--dur-slow) var(--ease-out);
        box-shadow: var(--shadow-lg);
    }

    .sidebar.is-open { transform: translateX(0); animation: drawer-in var(--dur-slow) var(--ease-out); }

    /* Touch users need a close control inside the drawer itself. */
    .sidebar__close { display: inline-grid; margin-left: auto; }

    .navbar__toggle { display: inline-grid; }

    .navbar__search { width: min(220px, 34vw); }
}

@media (max-width: 560px) {
    /*
     * The navbar wraps into two rows: identity and controls first, the search
     * field underneath. The grid row has to grow with it, otherwise the taller
     * navbar would overlap the content.
     */
    .app { grid-template-rows: auto minmax(0, 1fr); }

    .navbar {
        flex-wrap: wrap;
        gap: 8px 10px;
        padding: 8px 12px;
        min-height: var(--navbar-h);
    }

    .navbar__titles { flex: 1 1 auto; }
    .navbar__spacer { display: none; }
    .navbar__avatar { display: none; }
    .navbar__search { order: 10; width: 100%; flex: 1 1 100%; }
    .navbar__search input { height: 42px; }

    .main { padding-top: 16px; }

    .page-head { gap: 12px; margin-bottom: 18px; }
    .page-head__actions { width: 100%; }
    .page-head__actions .btn { flex: 1 1 calc(50% - 4px); }
    .page-head__actions .btn--primary { flex-basis: 100%; order: -1; }

    .card__head { padding: 13px 14px; }
    .card__body { padding: 14px; }
    .table-foot { padding: 11px 14px; }

    .filters { gap: 8px; }
    .filters > input[type="search"] { flex: 1 1 100%; }
    .filters select { flex: 1 1 0; min-width: 0; }

    .doc-toolbar { padding: 10px 12px; gap: 8px; }
    .doc-toolbar__title, .doc-toolbar .card__hint { flex: 1 1 100%; }
    .doc-toolbar .spacer { display: none; }
    .doc-toolbar .btn { flex: 1 1 auto; }
    .doc-toolbar .btn--primary { flex-basis: 100%; }

    .doc { padding: 24px 16px 32px; }

    /* Keep iOS from zooming the page when a control receives focus. */
    .field input, .field select, .field textarea,
    .filters input, .filters select,
    .navbar__search input { font-size: 16px; }

    .pagination a, .pagination span { min-height: var(--tap); min-width: var(--tap); }

    .row > form { flex: 1 1 100%; }
    .row > form .btn { width: 100%; }
    .row > span { display: none; }

    /* Appendix rows become label-over-value so long addresses get full width. */
    .appendix-table,
    .appendix-table tbody,
    .appendix-table tr,
    .appendix-table td { display: block; width: 100%; }

    .appendix-table tr { padding: 9px 0; border-bottom: 1px solid var(--border); }
    .appendix-table tr:last-child { border-bottom: none; }
    .appendix-table td { border: none; padding: 1px 10px; }
    .appendix-table td:first-child {
        width: auto;
        font-size: 11px;
        color: var(--text-3);
    }
}

/* --------------------------------------- 18. Reduced motion */

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

    .card--lift:hover { transform: none; }
    .btn:active { transform: none; }
}

/* ------------------------------ 19. Auth, accounts and workflow */

.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;
}

/* Account menu in the navbar */
.user-menu { position: relative; flex: 0 0 auto; }

.user-menu summary {
    list-style: none;
    cursor: pointer;
}
.user-menu summary::-webkit-details-marker { display: none; }
.user-menu summary:hover { box-shadow: 0 0 0 4px color-mix(in srgb, var(--brand-600) 14%, transparent); }

.user-menu__panel {
    position: absolute;
    top: calc(100% + 10px);
    right: 0;
    width: 252px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--r);
    box-shadow: var(--shadow-lg);
    padding: 8px;
    z-index: calc(var(--z-sticky) + 1);
    animation: slide-in var(--dur) var(--ease-out);
}

.user-menu__identity {
    padding: 8px 10px 12px;
    margin-bottom: 6px;
    border-bottom: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 3px;
}

.user-menu__name { font-weight: 650; }
.user-menu__email { font-size: 12px; color: var(--text-3); overflow-wrap: anywhere; }
.user-menu form { margin: 0; }

.user-menu__link {
    display: flex;
    align-items: center;
    gap: 9px;
    width: 100%;
    padding: 9px 10px;
    border: none;
    border-radius: var(--r-sm);
    background: transparent;
    color: var(--text-2);
    font: inherit;
    font-size: 13px;
    font-weight: 550;
    text-align: left;
    cursor: pointer;
}
.user-menu__link:hover { background: var(--surface-3); color: var(--text); text-decoration: none; }
.user-menu__link--danger { color: var(--danger-700); }
.user-menu__link--danger:hover { background: var(--danger-50); color: var(--danger-700); }

/* Signed-in identity: only inside the phone drawer, the navbar avatar owns desktop */
.sidebar__user {
    display: none;
    align-items: center;
    gap: 10px;
    margin: 10px 12px 14px;
    padding: 10px;
    border-radius: var(--r-sm);
    background: var(--nav-bg-2);
    flex: 0 0 auto;
}

.sidebar__avatar {
    width: 34px;
    height: 34px;
    border-radius: var(--r-full);
    background: linear-gradient(140deg, var(--brand-500), var(--brand-700));
    color: #fff;
    display: grid;
    place-items: center;
    font-size: 11.5px;
    font-weight: 700;
    flex: 0 0 auto;
}

.sidebar__user-text { min-width: 0; flex: 1 1 auto; }

.sidebar__user-name {
    color: var(--nav-text-strong);
    font-size: 12.5px;
    font-weight: 650;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sidebar__user-role {
    color: var(--nav-text);
    font-size: 10px;
    letter-spacing: .07em;
    text-transform: uppercase;
    opacity: .75;
}

.sidebar__logout { color: var(--nav-text); width: 36px; height: 36px; }
.sidebar__logout:hover { background: rgba(255, 255, 255, .1); color: var(--nav-text-strong); }
.sidebar__logout:focus-visible { outline-color: var(--brand-500); }

@media (max-width: 560px) {
    .sidebar__user { display: flex; }
}

/* Roles + user identity */
.badge--rejected { background: var(--danger-50); color: var(--danger-700); border-color: var(--danger-200); }
.badge--role { background: var(--surface-3); color: var(--text-2); border-color: var(--border); font-weight: 600; }

.avatar {
    width: 36px;
    height: 36px;
    border-radius: var(--r-full);
    background: linear-gradient(140deg, var(--brand-500), var(--brand-700));
    color: #fff;
    display: grid;
    place-items: center;
    font-size: 11.5px;
    font-weight: 700;
    flex: 0 0 auto;
}

.user-cell { display: flex; align-items: center; gap: 10px; min-width: 0; }
.user-cell__text { min-width: 0; }
.user-cell .table__strong { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.user-cell__text .table__muted { overflow-wrap: anywhere; }

/* Checkbox switch on the user form */
.switch {
    position: relative;
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 42px;
    padding: 6px 12px;
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    background: var(--surface);
    color: var(--text-2);
    font-size: 13px;
    cursor: pointer;
}

.switch input { position: absolute; width: 1px; height: 1px; opacity: 0; }

.switch__track {
    position: relative;
    width: 38px;
    height: 22px;
    border-radius: var(--r-full);
    background: var(--border-strong);
    flex: 0 0 auto;
    transition: background var(--dur-fast) var(--ease);
}

.switch__thumb {
    position: absolute;
    top: 3px;
    left: 3px;
    width: 16px;
    height: 16px;
    border-radius: var(--r-full);
    background: #fff;
    box-shadow: var(--shadow-xs);
    transition: transform var(--dur-fast) var(--ease);
}

.switch input:checked + .switch__track { background: var(--brand-600); }
.switch input:checked + .switch__track .switch__thumb { transform: translateX(16px); }
.switch input:focus-visible + .switch__track { outline: 2px solid var(--brand-600); outline-offset: 2px; }

/* Approval workflow panel */
.workflow-card { margin-bottom: 18px; border-left: 3px solid var(--brand-600); }

.workflow { display: flex; align-items: flex-start; gap: 14px; flex-wrap: wrap; }

.workflow__icon {
    width: 42px;
    height: 42px;
    border-radius: var(--r);
    display: grid;
    place-items: center;
    background: var(--surface-3);
    color: var(--text-2);
    flex: 0 0 auto;
}

.workflow__icon--in_review { background: var(--warn-50); color: var(--warn-700); }
.workflow__icon--approved,
.workflow__icon--active { background: var(--ok-50); color: var(--ok-700); }
.workflow__icon--rejected,
.workflow__icon--terminated { background: var(--danger-50); color: var(--danger-700); }

.workflow__text { flex: 1 1 320px; min-width: 0; }
.workflow__title { font-size: 14.5px; font-weight: 700; }
.workflow__message { margin-top: 2px; color: var(--text-2); font-size: 13px; }

.workflow__reason {
    margin-top: 9px;
    padding: 9px 12px;
    border: 1px solid var(--danger-200);
    border-radius: var(--r-sm);
    background: var(--danger-50);
    color: var(--danger-700);
    font-size: 12.5px;
    white-space: pre-line;
    overflow-wrap: anywhere;
}

.workflow__actions {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    flex-wrap: wrap;
    width: 100%;
    padding-top: 2px;
}
.workflow__actions form { margin: 0; }

.action-form { position: relative; }
.action-form summary { list-style: none; cursor: pointer; }
.action-form summary::-webkit-details-marker { display: none; }
.action-form[open] { flex: 1 1 100%; }

.action-form__body {
    display: grid;
    gap: 10px;
    max-width: 560px;
    margin-top: 12px;
    padding: 14px;
    border: 1px solid var(--border);
    border-radius: var(--r);
    background: var(--surface-2);
}

.action-form__body .btn { justify-self: start; }

/* Activity timeline */
.timeline { display: grid; }

.timeline__item {
    position: relative;
    display: grid;
    grid-template-columns: 34px minmax(0, 1fr);
    gap: 12px;
    padding-bottom: 18px;
}

.timeline__item:last-child { padding-bottom: 0; }

.timeline__item::before {
    content: "";
    position: absolute;
    left: 16px;
    top: 34px;
    bottom: 0;
    width: 2px;
    background: var(--border);
}

.timeline__item:last-child::before { display: none; }

.timeline__dot {
    width: 34px;
    height: 34px;
    border-radius: var(--r-full);
    border: 1px solid var(--border);
    background: var(--surface-3);
    color: var(--text-2);
    display: grid;
    place-items: center;
}

.timeline__body { min-width: 0; padding-top: 3px; }

.timeline__title {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    font-size: 13px;
    font-weight: 650;
}

.timeline__transition {
    padding: 2px 8px;
    border-radius: var(--r-full);
    background: var(--surface-3);
    color: var(--text-3);
    font-size: 11.5px;
    font-weight: 550;
}

.timeline__meta {
    display: flex;
    gap: 5px;
    flex-wrap: wrap;
    margin-top: 2px;
    color: var(--text-3);
    font-size: 12px;
}

.timeline__notes {
    margin-top: 7px;
    padding: 9px 12px;
    border-left: 3px solid var(--border-strong);
    border-radius: 0 var(--r-sm) var(--r-sm) 0;
    background: var(--surface-2);
    color: var(--text-2);
    font-size: 12.5px;
    white-space: pre-line;
    overflow-wrap: anywhere;
}

.tracking__time {
    display: block;
    color: var(--text-3);
    font-size: 11.5px;
    font-weight: 500;
}

/* Sign-in */
.auth-body {
    min-height: 100vh;
    background:
        radial-gradient(1100px 560px at 8% -12%, color-mix(in srgb, var(--brand-600) 16%, transparent), transparent 60%),
        radial-gradient(900px 520px at 108% 112%, color-mix(in srgb, var(--brand-500) 12%, transparent), transparent 55%),
        var(--canvas);
}

.auth-theme { position: fixed; top: 14px; right: 14px; z-index: 10; }

.auth {
    display: grid;
    place-items: center;
    min-height: 100vh;
    min-height: 100dvh;
    padding: clamp(18px, 5vw, 48px) 16px;
}

.auth__panel {
    width: min(440px, 100%);
    padding: clamp(24px, 5vw, 38px);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--r-lg);
    box-shadow: var(--shadow-lg);
    animation: fade-up var(--dur-slow) var(--ease-out) both;
}

.auth__brand {
    display: flex;
    align-items: center;
    gap: 12px;
    padding-bottom: 20px;
    margin-bottom: 22px;
    border-bottom: 1px solid var(--border);
}

.auth__brand-name { font-weight: 700; letter-spacing: -.02em; }

.auth__brand-sub {
    font-size: 10.5px;
    letter-spacing: .09em;
    text-transform: uppercase;
    color: var(--text-3);
}

.auth__title { font-size: clamp(22px, 2.6vw, 26px); font-weight: 750; letter-spacing: -.03em; }
.auth__subtitle { margin: 4px 0 22px; color: var(--text-2); font-size: 13.5px; }

.auth__form { display: grid; gap: 16px; }

.auth__remember {
    display: flex;
    align-items: center;
    gap: 9px;
    min-height: 24px;
    color: var(--text-2);
    font-size: 13px;
    cursor: pointer;
}

.auth__remember input { width: 16px; height: 16px; accent-color: var(--brand-600); }

.auth__submit { width: 100%; min-height: 44px; margin-top: 2px; }

.auth__hint { margin-top: 20px; color: var(--text-3); font-size: 12px; line-height: 1.55; }

.auth__foot {
    display: flex;
    align-items: center;
    gap: 7px;
    margin-top: 24px;
    padding-top: 16px;
    border-top: 1px solid var(--border);
    color: var(--text-3);
    font-size: 11.5px;
}

@media (max-width: 700px) {
    .workflow__actions { margin-left: 0; }
    .workflow__actions > form { flex: 1 1 auto; }
    .workflow__actions .btn { width: 100%; }
    .action-form { flex: 1 1 auto; }
    .action-form > summary { width: 100%; }
    .action-form__body { max-width: none; }
}

@media (max-width: 560px) {
    .user-menu { display: none; }
    .auth__panel { padding: 22px 18px; }
}

/* Roles & permissions */
.role-cell {
    display: flex;
    align-items: center;
    gap: 10px;
}

.role-cell__icon {
    width: 34px;
    height: 34px;
    border-radius: var(--r-sm);
    display: grid;
    place-items: center;
    flex: 0 0 auto;
    background: color-mix(in srgb, var(--brand-600) 12%, var(--surface-2));
    color: var(--brand-700);
}

.role-cell__name {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    font-weight: 650;
}

.roles-note {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 14px 18px;
    border-top: 1px solid var(--border);
    color: var(--text-3);
    font-size: 12px;
    line-height: 1.55;
}

.roles-note svg { flex: 0 0 auto; margin-top: 1px; color: var(--brand-600); }

/* Permission catalog */
.permission-section + .permission-section { border-top: 1px solid var(--border); }
.permission-section .card__hint { margin-left: auto; }

.permission-code {
    display: inline-block;
    font-family: var(--font-mono);
    font-size: 11.5px;
    color: var(--text-2);
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 2px 7px;
    overflow-wrap: anywhere;
}

.permission-roles { display: flex; flex-wrap: wrap; gap: 5px; }

.card__head > .badge--role { margin-left: auto; }

.permission-group {
    border: none;
    padding: 0;
    margin: 0 0 24px;
}

.permission-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
}

.permission {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    background: var(--surface-2);
    cursor: pointer;
    transition: border-color var(--dur) var(--ease), background var(--dur) var(--ease);
}

.permission:hover {
    border-color: var(--border-strong);
    background: var(--surface-3);
}

.permission input {
    width: 16px;
    height: 16px;
    margin-top: 2px;
    flex: 0 0 auto;
    accent-color: var(--brand-600);
}

.permission__label {
    display: block;
    font-size: 12.5px;
    font-weight: 600;
    color: var(--text);
}

.permission__name {
    display: block;
    margin-top: 2px;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 10.5px;
    color: var(--text-3);
    overflow-wrap: anywhere;
}

.permission--locked {
    opacity: .8;
    cursor: not-allowed;
}

.permission--locked:hover {
    border-color: var(--border);
    background: var(--surface-2);
}

@media (max-width: 900px) {
    .permission-grid { grid-template-columns: minmax(0, 1fr); }
    .roles-note { padding: 13px 15px; }
}

/* --------------------------------------- 20. Print */

@media print {
    .sidebar, .navbar, .doc-toolbar, .page-head__actions, .form-actions,
    .workflow__actions, .action-form { display: none !important; }
    .app { display: block; }
    .doc { box-shadow: none; border: none; max-width: none; padding: 0; }
}