@layer pages {
    /* ---------- shared admin nav ---------- */

    .admin-nav {
        display: flex;
        gap: var(--space-1);
        width: min(1420px, calc(100% - 36px));
        margin: 0 auto;
        padding: var(--space-3) 0 0;
        overflow-x: auto;
    }

    .admin-nav-item {
        display: inline-flex;
        align-items: center;
        gap: var(--space-2);
        flex: none;
        min-height: var(--control-h-md);
        padding: 0 var(--space-3);
        border: 1px solid transparent;
        border-radius: var(--radius-lg);
        color: var(--text-muted);
        font-size: var(--text-sm);
        font-weight: var(--weight-bold);
        text-decoration: none;
        white-space: nowrap;
    }

    .admin-nav-item:hover {
        color: var(--text);
        background: var(--surface-muted);
    }

    .admin-nav-item.active {
        border-color: var(--accent-border);
        background: var(--accent-bg);
        color: var(--accent);
    }

    /* The gateway tables are wide; the accounts page keeps its narrower measure. */
    .admin-content.wide {
        display: block;
        width: min(1420px, calc(100% - 36px));
    }

    .panel {
        margin-bottom: var(--space-6);
        padding: var(--space-5);
        border: 1px solid var(--border);
        border-radius: var(--radius-lg);
        background: var(--surface);
    }

    .empty-panel {
        padding: var(--space-8) var(--space-5);
        border: 1px dashed var(--border-strong);
        border-radius: var(--radius-lg);
        background: var(--surface);
        text-align: center;
    }

    .empty-panel h2 {
        margin: 0 0 var(--space-2);
        font-size: var(--text-xl);
    }

    .empty-panel p,
    .muted-note {
        margin: 0;
        color: var(--text-muted);
        font-size: var(--text-sm);
        line-height: var(--leading-normal);
    }

    /* ---------- range selector ---------- */

    .range-bar {
        display: flex;
        align-items: center;
        gap: var(--space-2);
        flex-wrap: wrap;
        margin-bottom: var(--space-5);
    }

    .range-option {
        min-height: var(--control-h-md);
        padding: 0 var(--space-3);
        border: 1px solid var(--border);
        border-radius: var(--radius-lg);
        background: var(--surface);
        color: var(--text);
        font-size: var(--text-sm);
        font-weight: var(--weight-bold);
    }

    .range-option.active {
        border-color: var(--accent-border);
        background: var(--accent-bg);
        color: var(--accent);
    }

    .range-note {
        color: var(--text-muted);
        font-size: var(--text-xs);
    }

    /* ---------- KPIs ---------- */

    .kpi-row {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
        gap: var(--space-3);
        margin-bottom: var(--space-4);
    }

    .kpi {
        display: grid;
        gap: var(--space-1);
        padding: var(--space-4);
        border: 1px solid var(--border);
        border-radius: var(--radius-lg);
        background: var(--surface);
    }

    .kpi-label {
        color: var(--text-muted);
        font-size: var(--text-2xs);
        font-weight: var(--weight-heavy);
        text-transform: uppercase;
    }

    .kpi-value {
        font-size: var(--text-2xl);
        font-weight: var(--weight-bold);
        font-variant-numeric: tabular-nums;
        line-height: var(--leading-tight);
    }

    .kpi small {
        color: var(--text-muted);
        font-size: var(--text-xs);
    }

    /* ---------- caveats ---------- */

    .caveats {
        display: grid;
        gap: var(--space-2);
        margin-bottom: var(--space-6);
    }

    .caveat {
        display: flex;
        align-items: flex-start;
        gap: var(--space-2);
        margin: 0 0 var(--space-4);
        padding: var(--space-3);
        border: 1px solid var(--warning-border);
        border-radius: var(--radius-lg);
        background: var(--warning-bg);
        color: var(--warning-fg);
        font-size: var(--text-xs);
        line-height: var(--leading-normal);
    }

    .caveat .icon {
        flex: none;
        margin-top: 1px;
    }

    .caveat a {
        color: inherit;
        text-decoration: underline;
    }

    /* ---------- daily bars ---------- */

    .daybars {
        display: flex;
        align-items: flex-end;
        gap: var(--space-1);
        height: 160px;
        margin-bottom: var(--space-3);
        overflow-x: auto;
    }

    .daybar {
        display: flex;
        flex: 1 1 0;
        flex-direction: column;
        gap: var(--space-1);
        min-width: 22px;
        height: 100%;
    }

    .daybar-track {
        display: flex;
        align-items: flex-end;
        flex: 1 1 auto;
        min-height: 0;
    }

    .daybar-fill {
        position: relative;
        width: 100%;
        min-height: 2px;
        border-radius: var(--radius-sm) var(--radius-sm) 0 0;
        background: var(--accent);
    }

    /* Failures sit at the top of the bar so the split reads as a proportion of the whole. */
    .daybar-failed {
        position: absolute;
        inset: 0 0 auto 0;
        border-radius: var(--radius-sm) var(--radius-sm) 0 0;
        background: var(--danger-fg);
    }

    .daybar-label {
        color: var(--text-muted);
        font-size: 10px;
        font-variant-numeric: tabular-nums;
        text-align: center;
    }

    .legend {
        display: flex;
        align-items: center;
        gap: var(--space-2);
        margin: 0;
        color: var(--text-muted);
        font-size: var(--text-xs);
    }

    .swatch {
        display: inline-block;
        width: 10px;
        height: 10px;
        border-radius: 2px;
    }

    .swatch.ok {
        background: var(--accent);
    }

    .swatch.bad {
        background: var(--danger-fg);
    }

    /* ---------- breakdowns ---------- */

    .breakdowns {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(520px, 1fr));
        gap: var(--space-4);
        min-width: 0;
    }

    .breakdowns .panel {
        margin-bottom: 0;
        min-width: 0;
    }

    .breakdown-key {
        display: block;
        max-width: 180px;
        overflow: hidden;
        font-family: var(--font-mono);
        font-size: var(--text-xs);
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .user-table .figure {
        font-variant-numeric: tabular-nums;
        text-align: right;
        white-space: nowrap;
    }

    .user-table.numeric th:not(:first-child) {
        text-align: right;
    }

    .user-table .figure.bad,
    .user-table small.bad {
        color: var(--danger-fg);
    }

    .cost-kpi {
        border-color: var(--accent-border);
        background: var(--accent-bg);
    }

    .daily-cost-panel .breakdown-key {
        max-width: 240px;
    }

    .user-table small.bad {
        display: block;
    }

    .user-table .nowrap {
        white-space: nowrap;
    }

    /* A non-2xx status is a failure, not merely an inactive state. .access-badge.blocked reads as
       neutral, which is right for a revoked token and wrong for an HTTP error. */
    .access-badge.failed {
        border: 1px solid var(--danger-border);
        background: var(--danger-bg);
        color: var(--danger-fg);
    }

    .inactive-row {
        color: var(--text-muted);
    }

    .confirm-row td {
        border-bottom: 1px solid var(--border);
        background: var(--danger-bg);
        color: var(--danger-fg);
        font-size: var(--text-xs);
        line-height: var(--leading-normal);
    }

    .inline-confirm {
        display: flex;
        gap: var(--space-1);
    }

    .inline-confirm button {
        min-height: var(--control-h-sm);
        padding: 0 var(--space-2);
        font-size: var(--text-xs);
    }

    /* ---------- request log ---------- */

    .filter-bar {
        display: flex;
        flex-wrap: wrap;
        gap: var(--space-3);
        margin-bottom: var(--space-4);
    }

    .field-group.compact {
        gap: var(--space-1);
        min-width: 150px;
    }

    .field-group.compact > span {
        font-size: var(--text-2xs);
        text-transform: uppercase;
    }

    .field-group.compact select,
    .field-group.compact input {
        height: var(--control-h-md);
        font-size: var(--text-sm);
    }

    .field-group.grow {
        flex: 1 1 260px;
    }

    .lookup {
        display: flex;
        gap: var(--space-2);
    }

    .lookup input {
        flex: 1 1 auto;
        min-width: 0;
        padding: 0 var(--space-3);
        border: 1px solid var(--border-strong);
        border-radius: var(--radius-lg);
        outline: none;
        background: var(--surface);
        color: var(--text);
        font-family: var(--font-mono);
    }

    .lookup .secondary-button {
        flex: none;
        min-height: var(--control-h-md);
    }

    .error-code {
        display: block;
        margin-top: 2px;
        color: var(--danger-fg);
        font-family: var(--font-mono);
        font-size: 10.5px;
    }

    .detail-grid {
        display: grid;
        grid-template-columns: minmax(120px, auto) minmax(0, 1fr);
        gap: var(--space-2) var(--space-4);
        margin: 0 0 var(--space-5);
        font-size: var(--text-sm);
    }

    .detail-grid dt {
        color: var(--text-muted);
        font-size: var(--text-2xs);
        font-weight: var(--weight-heavy);
        text-transform: uppercase;
    }

    .detail-grid dd {
        margin: 0;
        overflow-wrap: anywhere;
    }

    .pager {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: var(--space-3);
        margin-top: var(--space-4);
    }

    .pager-actions {
        display: flex;
        gap: var(--space-2);
    }

    .pager-actions .secondary-button {
        min-height: var(--control-h-md);
        font-size: var(--text-sm);
    }

    @media (max-width: 760px) {
        .admin-nav,
        .admin-content.wide {
            width: calc(100% - 24px);
        }

        .breakdowns {
            grid-template-columns: 1fr;
        }

        .detail-grid {
            grid-template-columns: 1fr;
            gap: var(--space-1) 0;
        }

        .detail-grid dd {
            margin-bottom: var(--space-3);
        }
    }

    /* ---------- toasts ---------- */

    .toast-region {
        display: grid;
        gap: var(--space-2);
        margin-bottom: var(--space-4);
    }

    .toast-region:empty {
        display: none;
    }

    .toast {
        display: flex;
        align-items: flex-start;
        gap: var(--space-2);
        padding: var(--space-3);
        border: 1px solid var(--border);
        border-radius: var(--radius-lg);
        background: var(--surface);
        font-size: var(--text-sm);
        line-height: var(--leading-normal);
    }

    .toast p {
        flex: 1 1 auto;
        margin: 0;
    }

    .toast .icon {
        flex: none;
        margin-top: 2px;
    }

    .toast.success {
        border-color: var(--success-border);
        background: var(--success-bg);
        color: var(--success-fg);
    }

    .toast.warning {
        border-color: var(--warning-border);
        background: var(--warning-bg);
        color: var(--warning-fg);
    }

    .toast.error {
        border-color: var(--danger-border);
        background: var(--danger-bg);
        color: var(--danger-fg);
    }

    /* ---------- modal ---------- */

    .modal-backdrop {
        position: fixed;
        inset: 0;
        z-index: var(--z-overlay);
        background: rgba(11, 15, 20, 0.55);
    }

    .modal {
        position: fixed;
        top: 50%;
        left: 50%;
        z-index: var(--z-modal);
        display: flex;
        flex-direction: column;
        width: min(520px, calc(100vw - 32px));
        max-height: calc(100vh - 64px);
        transform: translate(-50%, -50%);
        border: 1px solid var(--border);
        border-radius: var(--radius-lg);
        background: var(--surface);
        box-shadow: var(--shadow);
    }

    .modal-head {
        display: flex;
        align-items: flex-start;
        justify-content: space-between;
        gap: var(--space-3);
        padding: var(--space-5) var(--space-5) 0;
    }

    .modal-head h2 {
        margin: 0;
        font-size: var(--text-xl);
    }

    .modal-body {
        display: grid;
        gap: var(--space-4);
        padding: var(--space-4) var(--space-5);
        overflow-y: auto;
    }

    .modal-foot {
        display: flex;
        justify-content: flex-end;
        gap: var(--space-2);
        padding: 0 var(--space-5) var(--space-5);
    }

    .field-hint {
        color: var(--text-muted);
        font-size: var(--text-xs);
        font-weight: var(--weight-medium);
    }

    /* The secret is legible in full: it has to be verifiable before it disappears. */
    .secret-reveal {
        display: flex;
        align-items: center;
        gap: var(--space-3);
        padding: var(--space-3);
        border: 1px solid var(--success-border);
        border-radius: var(--radius-lg);
        background: var(--success-bg);
    }

    .secret-reveal code {
        flex: 1 1 auto;
        min-width: 0;
        font-family: var(--font-mono);
        font-size: var(--text-xs);
        overflow-wrap: anywhere;
    }

    .secret-reveal .secondary-button {
        flex: none;
    }

    /* ---------- row actions ---------- */

    .row-link {
        font-weight: var(--weight-bold);
        color: var(--accent);
    }

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

    .row-actions .secondary-button {
        min-height: var(--control-h-sm);
        padding: 0 var(--space-2);
        font-size: var(--text-xs);
    }

    .row-menu {
        position: absolute;
        right: var(--space-4);
        z-index: var(--z-popover);
        display: grid;
        gap: 2px;
        min-width: 210px;
        padding: var(--space-1);
        border: 1px solid var(--border-strong);
        border-radius: var(--radius-lg);
        background: var(--surface);
        box-shadow: var(--shadow);
    }

    .row-menu button {
        display: flex;
        align-items: center;
        gap: var(--space-2);
        padding: var(--space-2) var(--space-3);
        border: 0;
        border-radius: var(--radius-md);
        background: none;
        color: var(--text);
        font-size: var(--text-sm);
        text-align: left;
    }

    .row-menu button:hover {
        background: var(--surface-muted);
    }

    .row-menu button.danger {
        color: var(--danger-fg);
    }

    /* ---------- grant matrix ---------- */

    .matrix-note {
        margin-bottom: var(--space-4);
        max-width: 78ch;
    }

    .all-models-row {
        margin-bottom: var(--space-3);
    }

    .user-table.matrix th.matrix-head {
        text-align: center;
        vertical-align: bottom;
    }

    .user-table.matrix th.matrix-head label {
        display: grid;
        justify-items: center;
        gap: 2px;
        cursor: pointer;
    }

    .user-table.matrix th.matrix-head small {
        color: var(--text-muted);
        font-family: var(--font-mono);
        font-size: 10px;
        font-weight: var(--weight-medium);
        text-transform: none;
    }

    .matrix-cell {
        text-align: center;
    }

    .matrix-cell.granted {
        color: var(--success-fg);
    }

    /* Supported by the model but withheld by the policy: empty, because nothing is there to use. */
    .matrix-cell.withheld {
        background: var(--surface-muted);
    }

    /* The model cannot do it at all, so the policy is irrelevant here. */
    .matrix-cell.unsupported {
        color: var(--text-muted);
        background: repeating-linear-gradient(
            135deg,
            transparent,
            transparent 5px,
            var(--hatch) 5px,
            var(--hatch) 10px);
    }

    .scope-chips {
        display: flex;
        flex-wrap: wrap;
        gap: var(--space-1);
    }

    .chip {
        padding: 1px 7px;
        border: 1px solid var(--border);
        border-radius: var(--radius-pill);
        background: var(--surface-muted);
        color: var(--text-muted);
        font-family: var(--font-mono);
        font-size: 10.5px;
    }

    .chip.off {
        border-color: var(--danger-border);
        background: var(--danger-bg);
        color: var(--danger-fg);
    }

    /* ---------- run timeline and approvals ---------- */

    .state-pill {
        display: inline-flex;
        align-items: center;
        min-height: 24px;
        padding: 0 var(--space-2);
        border: 1px solid var(--border);
        border-radius: var(--radius-pill);
        font-size: var(--text-2xs);
        font-weight: var(--weight-heavy);
        white-space: nowrap;
    }

    .state-pill.completed {
        border-color: var(--success-border);
        background: var(--success-bg);
        color: var(--success-fg);
    }

    .state-pill.failed {
        border-color: var(--danger-border);
        background: var(--danger-bg);
        color: var(--danger-fg);
    }

    .state-pill.waiting {
        border-color: var(--warning-border);
        background: var(--warning-bg);
        color: var(--warning-fg);
    }

    .state-pill.running {
        border-color: var(--accent-border);
        background: var(--accent-bg);
        color: var(--accent);
    }

    .state-pill.stopped {
        background: var(--surface-muted);
        color: var(--text-muted);
    }

    .run-timeline,
    .approval-list {
        display: grid;
        gap: var(--space-3);
    }

    .subagent-list {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(min(100%, 280px), 1fr));
        gap: var(--space-3);
    }

    .subagent-card {
        display: grid;
        align-content: start;
        gap: var(--space-2);
        padding: var(--space-3) var(--space-4);
        border: 1px solid var(--border);
        border-radius: var(--radius-lg);
        background: var(--surface);
        min-width: 0;
        max-width: 100%;
    }

    .subagent-card.state-running {
        border-color: var(--accent-border);
        background: var(--accent-bg);
    }

    .subagent-card.state-failed,
    .subagent-card.state-interrupted {
        border-color: var(--danger-border);
    }

    .subagent-card > p,
    .subagent-card > small {
        margin: 0;
        overflow-wrap: anywhere;
    }

    .subagent-card > small {
        color: var(--text-muted);
    }

    .run-step {
        display: grid;
        gap: var(--space-2);
        padding: var(--space-3) var(--space-4);
        border: 1px solid var(--border);
        border-radius: var(--radius-lg);
        background: var(--surface);
        min-width: 0;
        max-width: 100%;
    }

    .run-step.message.user {
        border-color: var(--accent-border);
        background: var(--accent-bg);
    }

    .run-step.tool.state-pendingapproval {
        border-color: var(--warning-border);
        background: var(--warning-bg);
    }

    .run-step.tool.state-failed,
    .run-step.tool.state-denied {
        border-color: var(--danger-border);
    }

    .run-step-head {
        display: flex;
        align-items: center;
        gap: var(--space-3);
        min-width: 0;
        flex-wrap: wrap;
    }

    .run-step-kind {
        display: inline-flex;
        align-items: center;
        gap: var(--space-1);
        font-family: var(--font-mono);
        font-size: var(--text-xs);
        font-weight: var(--weight-bold);
        text-transform: uppercase;
        min-width: 0;
        overflow-wrap: anywhere;
    }

    .run-step-time {
        margin-left: auto;
        color: var(--text-muted);
        font-size: var(--text-2xs);
        font-variant-numeric: tabular-nums;
        white-space: nowrap;
    }

    .run-step-command {
        display: block;
        min-width: 0;
        max-width: 100%;
        font-family: var(--font-mono);
        font-size: var(--text-xs);
        line-height: var(--leading-normal);
        white-space: pre-wrap;
        overflow-wrap: anywhere;
        word-break: break-word;
    }

    .run-step-body :first-child {
        margin-top: 0;
    }

    .run-step-body :last-child {
        margin-bottom: 0;
    }

    .run-step-body .plain-text {
        margin: 0;
        white-space: pre-wrap;
        overflow-wrap: anywhere;
    }

    .run-step.event {
        display: flex;
        gap: var(--space-3);
        padding: var(--space-1) var(--space-4);
        border-style: dashed;
        background: none;
        color: var(--text-muted);
        font-size: var(--text-xs);
        min-width: 0;
        overflow-wrap: anywhere;
    }

    .run-step.event.error {
        color: var(--danger-fg);
    }

    .run-step.event .run-step-time {
        margin-left: 0;
    }

    .approval-args {
        max-height: 220px;
        margin: 0;
        padding: var(--space-3);
        overflow: auto;
        border: 1px solid var(--border);
        border-radius: var(--radius-lg);
        background: var(--surface);
        font-size: var(--text-xs);
        line-height: var(--leading-normal);
        white-space: pre-wrap;
        overflow-wrap: anywhere;
        word-break: break-word;
    }

    /* The cost figure without this reads as though every resend were paid for afresh. */
    .cached-share {
        display: block;
        margin-top: 2px;
        color: var(--text-muted);
        font-size: var(--text-2xs);
        font-weight: var(--weight-bold);
        overflow-wrap: anywhere;
    }
}
