/*
 * lists-and-tables.css: a set of things, as a list or as a table. It stands on app-shell.css,
 * which is beside it; load that first.
 *
 * Copy this file. Keep every line marked TRAP. Everything here starts with `set-`.
 *
 * The whole set: what narrows it, its rows, what moves through it.
 *
 *   <form class="set" data-set="list">                  a form only where rows can be chosen
 *     <div class="set-tools"> <ul class="set-filter"> <li><a href aria-current="page">All</a>
 *                             <p class="set-count" role="status">8 tasks</p>
 *     <section aria-labelledby="g1">
 *       <h2 class="set-group" id="g1">Today</h2>
 *       <ul class="set-list" role="list">
 *         <li class="set-row">
 *           <label class="set-tick"><input type="checkbox" /><span class="set-hide">Done: …</span></label>
 *           <div class="set-main"> <a class="set-title" href="/tasks/1">Call the dentist</a>
 *                                  <p class="set-note">Health</p>
 *           <span class="set-meta">09:00</span>
 *
 * A table that fits its page: the columns a phone has no room for are `set-minor`.
 *
 *   <div class="set" data-set="table">
 *     <table class="set-table"> <caption class="set-hide">Invoices</caption>
 *       <thead><tr> <th scope="col" aria-sort="descending"><a class="set-sort" href>Invoice <svg/></a>
 *                   <th scope="col" class="set-minor">Issued   <th scope="col" class="set-num">Amount
 *       <tbody><tr> <th scope="row"><a class="set-title" href="/invoices/1042">1042</a>
 *                   <td class="set-minor">3 Nov 2026        <td class="set-num">1,240.00
 *     <nav class="set-pages" aria-label="Pages">
 *
 * A table wider than its room, to compare across: the shell is a workspace and the table's region
 * is the part that scrolls, both ways, with the header and the first column held.
 *
 *   <div class="set" data-set="table">
 *     <div class="shell-scroll set-region" role="region" aria-labelledby="cap" tabindex="0">
 *       <table class="set-table"> <caption id="cap" class="set-hide">…
 *         <th scope="row" class="set-hold">
 *
 * Working examples: index.html (a list), invoices.html (a table that fits), revenue.html (a wide
 * table), loading.html, error.html, archive.html (empty), tasks-overdue.html (no results).
 */

:root {
	--set-row: 3.25rem; /* a row of two lines */
	--set-cell-x: 0.75rem; /* a table cell's side padding */
	--set-cell-y: 0.625rem; /* and above and below: a row of one line is 44px */
	--set-list-width: 48rem; /* a list is never wider: a row's two ends stay in one glance */
	--set-table-width: 80rem; /* nor a table: its first and last column still belong together */
	--set-alert: CanvasText; /* what failed. The base has no red: a glyph and the words say it */
}

/* ---- The whole ---- */

/* TRAP: a `display` of the author's beats the `hidden` attribute. What is hidden stays hidden. */
.set [hidden] {
	display: none !important;
}

.set {
	/* the parts answer to the room the set has, not to the window */
	container: set / inline-size;
	display: flex;
	flex-direction: column;
	/* TRAP: as in the frame. Without it one wide row or table widens the page. */
	min-width: 0;
	margin: 0;
	padding-bottom: 2rem;
}

.set[data-set='list'] {
	max-width: var(--set-list-width);
}

.set[data-set='table'] {
	max-width: var(--set-table-width);
}

/* In a fixed window the set fills it, and one part of it scrolls. */
.shell[data-shell='workspace'] .set {
	flex: 1;
	max-width: none;
	min-height: 0;
	padding-bottom: 0;
}

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

/* For assistive tech only: a name for a checkbox, a table's caption that the bar already shows. */
.set-hide {
	position: absolute;
	width: 1px;
	height: 1px;
	overflow: hidden;
	clip-path: inset(50%);
	white-space: nowrap;
}

/* ---- The tools above it: what narrows the set, and how many it holds ---- */

.set-tools {
	display: flex;
	/* TRAP: a row of tools that can't wrap pushes its end off a phone's screen */
	flex-wrap: wrap;
	align-items: center;
	gap: 0.5rem 0.75rem;
	padding: 0.75rem var(--shell-gutter);
}

/* A search field comes first and takes the room that is left. */
.set-search {
	flex: 1 1 14rem;
	/* TRAP: a field never shrinks below its default width without this */
	min-width: 0;
	max-width: 24rem;
	min-height: var(--shell-hit);
	padding-inline: 0.75rem;
	border: 1px solid var(--shell-line);
	border-radius: 0.5rem;
	background: var(--shell-surface);
	color: inherit;
	/* TRAP: 16px. iOS zooms the page when a field with smaller text takes focus. */
	font: inherit;
}

/* Filters are links: each is a view with an address. */
.set-filter {
	display: flex;
	flex-wrap: wrap;
	gap: 0.25rem;
	margin: 0;
	/* the first filter's words, not its ground, sit on the content's edge */
	margin-inline-start: -0.75rem;
	padding: 0;
	list-style: none;
}

.set-filter a {
	display: inline-flex;
	align-items: center;
	min-height: var(--shell-hit);
	padding-inline: 0.75rem;
	border-radius: 999px;
	color: var(--shell-muted);
	font-size: 0.9375rem;
	text-decoration: none;
	white-space: nowrap;
}

/* Marked by its ground, not by a heavier weight: that would change its width between views. */
.set-filter a[aria-current] {
	background: var(--shell-current);
	color: var(--shell-text);
}

/* A line of words among the tools: what the set is, in what unit. */
.set-tools p {
	margin: 0;
	color: var(--shell-muted);
	font-size: 0.875rem;
}

/* How many the set holds, or which of them show. A status: a change is read out. */
.set-tools .set-count {
	margin-inline-start: auto;
	font-variant-numeric: tabular-nums;
}

/* alone among the tools it starts where the rows do */
.set-tools .set-count:first-child {
	margin-inline-start: 0;
}

/* ---- A list ---- */

/* A group's heading stays in view while its rows pass. Each group is a <section>: the heading
   sticks only as long as its own rows are there. */
.set-group {
	position: sticky;
	top: var(--shell-cover-top);
	/* TRAP: above the rows' own controls, which are raised too, or a checkbox passes over it */
	z-index: 2;
	margin: 0;
	/* rows close, groups apart: the space above a group is more than twice a row's own */
	padding: 1.25rem var(--shell-gutter) 0.375rem;
	background: var(--shell-surface);
	color: var(--shell-muted);
	font-size: 0.8125rem;
	font-weight: 600;
	line-height: 1.4;
}

/* TRAP: a row that takes focus right under the heading that stays would be hidden by it. The
   margin is the heading's height: the browser scrolls the row out from under it. */
.set-row :is(a, button, input, select) {
	scroll-margin-top: 3rem;
}

.set-list {
	margin: 0;
	/* with the row's own padding: the text sits on the content's edge, the ground reaches past it */
	padding: 0 0.5rem;
	list-style: none;
}

.set-row {
	/* the title's link covers the row from here */
	position: relative;
	display: flex;
	align-items: center;
	gap: 0.75rem;
	min-height: max(var(--set-row), var(--shell-hit));
	padding: 0.5rem calc(var(--shell-gutter) - 0.5rem);
	border-radius: 0.5rem;
}

.set-main {
	flex: 1;
	/* TRAP: a flex child never shrinks below its text. Without this a long title pushes the row's
	   end off the screen instead of ending in an ellipsis. */
	min-width: 0;
}

/* The row's one link: what the row opens. In a table it is the row's name. */
.set-title {
	display: block;
	overflow: hidden;
	color: inherit;
	font-weight: 500;
	text-decoration: none;
	text-overflow: ellipsis;
	white-space: nowrap;
}

/* TRAP: the whole row is the target, and still one link. Never a click handler on the row, and
   never a link around the row: a link must not hold another control. */
.set-row .set-title::after {
	content: '';
	position: absolute;
	inset: 0;
	border-radius: inherit;
}

/* Everything else on the row that can be used lies above that link. */
.set-row :is(button, input, select, label, a:not(.set-title)) {
	position: relative;
	z-index: 1;
}

/* The ring goes around the row, since the row is what the link covers. */
.set-row .set-title:focus-visible {
	outline: none;
}

.set-row:has(.set-title:focus-visible) {
	outline: 2px solid var(--shell-focus);
	outline-offset: -2px;
}

.set-note {
	margin: 0;
	overflow: hidden;
	color: var(--shell-muted);
	font-size: 0.875rem;
	text-overflow: ellipsis;
	white-space: nowrap;
}

/* The one fact at the row's end: a time, an amount, a count. */
.set-meta {
	flex: none;
	/* TRAP: it is short. If it isn't, it ends in an ellipsis before it takes the title's room. */
	max-width: 45%;
	overflow: hidden;
	color: var(--shell-muted);
	font-size: 0.875rem;
	font-variant-numeric: tabular-nums;
	text-overflow: ellipsis;
	white-space: nowrap;
}

/* The row that is open beside the list (app-navigation's two panes). */
.set-row:has(.set-title[aria-current='page']) {
	background: var(--shell-current);
}

/* TRAP: only a pointer that can hover gets a hover; on a touch screen it sticks to the last row
   tapped. And only a row that opens something answers to it. */
@media (hover: hover) {
	.set-row:has(.set-title[href]):hover {
		background: var(--shell-current);
	}
}

/* ---- A checkbox on a row ---- */

/* set-tick: the row's one quick act (done). set-pick: choosing rows to act on several at once.
   Both are a label around a checkbox, so the target is the size of a finger and the box is not. */
.set-tick,
.set-pick {
	display: grid;
	flex: none;
	place-items: center;
	width: var(--shell-hit);
	height: var(--shell-hit);
	/* the box, not its target, sits on the content's edge */
	margin-inline: calc((var(--shell-hit) - 1.125rem) / -2);
	border-radius: 0.5rem;
	cursor: pointer;
	user-select: none;
}

.set-tick input,
.set-pick input {
	width: 1.125rem;
	height: 1.125rem;
	margin: 0;
	cursor: inherit;
}

/* Done stays readable: quieter, never crossed out. */
.set-row:has(.set-tick :checked) .set-title {
	color: var(--shell-muted);
	font-weight: 400;
}

/* ---- A row's own state ---- */

/* Busy: the row waits for its own change. The rest of the set stays usable. */
.set-row[aria-busy='true'] .set-main {
	opacity: 0.55;
}

/* Failed: the row says so itself, with the way to try again. No banner for the page. */
.set-fail {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	gap: 0 0.5rem;
	margin: 0;
	color: var(--set-alert);
	font-size: 0.875rem;
}

.set-fail svg {
	flex: none;
	width: 1rem;
	height: 1rem;
}

.set-fail button {
	/* a target the size of the others, in a line no taller than the note it stands for */
	min-height: var(--shell-hit);
	margin-block: calc((var(--shell-hit) - 1.5em) / -2);
	padding: 0;
	border: 0;
	background: none;
	color: inherit;
	font: inherit;
	font-weight: 600;
	text-decoration: underline;
	text-underline-offset: 0.2em;
	cursor: pointer;
}

/* ---- A table ---- */

.set-table {
	width: 100%;
	/* TRAP: with collapsed borders a held cell's lines stay behind when it sticks. Separate
	   borders travel with their cell. */
	border-collapse: separate;
	border-spacing: 0;
	/* TRAP: digits of one width, so the places of a column stand under each other */
	font-variant-numeric: tabular-nums;
}

.set-table :is(th, td) {
	padding: var(--set-cell-y) var(--set-cell-x);
	border-bottom: 1px solid var(--shell-line);
	font-weight: 400;
	/* text starts at the leading edge; a cell that wraps keeps its first line level with the row's */
	text-align: start;
	vertical-align: baseline;
	/* TRAP: a cell wraps between words, never inside one. (The frame lets a long word break
	   anywhere; in a table that makes every column give a little, and "Overdue" is cut in two
	   while the column beside it has room.) */
	overflow-wrap: break-word;
}

/* TRAP: one column gives way: the one that may hold a long piece without a space (a name, an
   address, a file's name). Without it such a piece is wider than a phone and widens the page. */
.set-table .set-text {
	overflow-wrap: anywhere;
}

/* a date is one piece */
.set-table time {
	white-space: nowrap;
}

/* the first and last column stand on the content's edges */
.set-table :is(th, td):first-child {
	padding-inline-start: var(--shell-gutter);
}

.set-table :is(th, td):last-child {
	padding-inline-end: var(--shell-gutter);
}

/* The header stays in view while its rows pass, under the frame's bar. On the cells: older
   browsers hold a header's cells but not its <thead>. */
.set-table thead :is(th, td) {
	position: sticky;
	top: var(--shell-cover-top);
	z-index: 2;
	padding-block: 0;
	background: var(--shell-surface);
	color: var(--shell-muted);
	font-size: 0.8125rem;
	font-weight: 600;
	line-height: var(--shell-hit);
	vertical-align: bottom;
	white-space: nowrap;
}

/* TRAP: the same for a row that takes focus right under the header, and above the bar of acts
   while rows are chosen. */
.set-table tbody :is(a, button, input, select) {
	scroll-margin-top: calc(var(--shell-hit) + 0.5rem);
}

.set:has(input[data-set-row]:checked) :is(.set-table tbody, .set-pages) :is(a, button, input, select) {
	scroll-margin-bottom: 5rem;
}

/* TRAP: numbers stand at the trailing edge, their header with them, and never wrap. */
.set-table .set-num {
	text-align: end;
	white-space: nowrap;
}

/* The row's name: its header cell. */
.set-table tbody th {
	font-weight: 500;
}

/* Its link fills the cell, so the target is as tall as the row. */
.set-table .set-title {
	margin-block: calc(var(--set-cell-y) * -1);
	padding-block: var(--set-cell-y);
	white-space: normal;
}

/* A column for choosing rows: as narrow as the box, the box level with the row's first line. */
.set-table .set-pick-cell {
	width: 1%;
	padding-block: 0;
	padding-inline-end: 0;
	vertical-align: top;
}

.set-table .set-pick {
	height: calc(1.5em + var(--set-cell-y) * 2);
}

.set-table thead .set-pick {
	height: var(--shell-hit);
}

.set-table tbody tr:has(.set-pick :checked) > * {
	background: var(--shell-current);
}

@media (hover: hover) {
	.set-table tbody tr:hover > * {
		background: var(--shell-recess);
	}

	.set-table tbody tr:has(.set-pick :checked):hover > * {
		background: var(--shell-current);
	}
}

/* ---- Sorting ---- */

/* The control in a header cell: a link where the order is in the address, a button where a
   script sorts in place. `aria-sort` on the <th> of the one column the set is sorted by. */
.set-sort {
	display: inline-flex;
	align-items: center;
	gap: 0.25rem;
	min-height: var(--shell-hit);
	padding: 0;
	border: 0;
	border-radius: 0.25rem;
	background: none;
	color: inherit;
	font: inherit;
	text-decoration: none;
	cursor: pointer;
}

/* the arrow comes before a number's header, so the words still end on the column's edge */
.set-num .set-sort {
	flex-direction: row-reverse;
}

/* The arrow points down: descending. It shows on the sorted column only. */
.set-sort svg {
	flex: none;
	width: 0.875rem;
	height: 0.875rem;
	opacity: 0;
}

th[aria-sort='ascending'] .set-sort,
th[aria-sort='descending'] .set-sort {
	color: var(--shell-text);
}

th[aria-sort='ascending'] .set-sort svg,
th[aria-sort='descending'] .set-sort svg {
	opacity: 1;
}

th[aria-sort='ascending'] .set-sort svg {
	transform: scaleY(-1);
}

/* a column that can be sorted says so before it is: a fainter arrow under the pointer or focus */
.set-sort:focus-visible svg {
	opacity: 0.5;
}

@media (hover: hover) {
	.set-sort:hover {
		color: var(--shell-text);
	}

	th:not([aria-sort='ascending'], [aria-sort='descending']) .set-sort:hover svg {
		opacity: 0.5;
	}
}

/* ---- A table on a phone ---- */

/* TRAP: a table is never squeezed, and never widens the page. One of two, decided per table:
   drop the columns a phone has no room for, or keep every column and let the table's region
   scroll. */

/* What a dropped column said and a phone still has to show: a line under the row's name. */
.set-under {
	display: none;
	color: var(--shell-muted);
	font-size: 0.875rem;
	font-weight: 400;
}

/* 45rem is this base's number, for about seven columns. A table with more columns, or longer
   ones, needs more room before it shows them all: raise it until no row takes three lines. (A
   custom property can't stand in a container query, so the number is here.) */
@container set (max-width: 45rem) {
	.set-table {
		--set-cell-x: 0.5rem;
	}

	.set-table .set-minor {
		display: none;
	}

	.set-under {
		display: block;
	}
}

/* The region: the part of a workspace that scrolls, both ways. It has a name and takes focus, so
   a keyboard can scroll it. */
.set-region {
	/* TRAP: at its end a sideways scroll would go on as the browser's Back */
	overscroll-behavior-x: contain;
}

.set-region:focus-visible {
	outline-offset: -2px;
}

/* TRAP: a held header sticks to the nearest scroller. Inside a region that is this region, so
   only a region that scrolls both ways can hold the header and scroll sideways. */
.set-region .set-table {
	/* TRAP: a table that may scroll is as wide as its columns need. Squeezed to the window first,
	   its words break in their middle. */
	width: max-content;
	min-width: 100%;
}

.set-region .set-table :is(th, td) {
	white-space: nowrap;
}

/* The column that says what a row is stays while the others pass under it. */
.set-region .set-hold {
	position: sticky;
	inset-inline-start: 0;
	z-index: 1;
	background: var(--shell-surface);
	/* TRAP: a held cell needs a ground of its own, or the columns show through it; and a line at
	   its edge, or nothing says that something is under it */
	border-inline-end: 1px solid var(--shell-line);
	/* TRAP: a long name wraps instead of taking the region: the held column leaves more than half
	   of it to the columns that scroll */
	max-width: min(16rem, 45cqi);
	overflow-wrap: break-word;
	white-space: normal;
}

/* the corner: above the header's row and the held column */
.set-region thead .set-hold {
	z-index: 3;
}

@media (hover: hover) {
	.set-region tbody tr:hover > .set-hold {
		background: var(--shell-recess);
	}
}

/* ---- The bar of acts for the rows that are chosen ---- */

/* It appears with the first row chosen and stays at the lower edge, above the frame's own bar,
   in reach while the rows scroll. */
.set-bar {
	position: sticky;
	bottom: calc(var(--shell-cover-bottom) + 0.75rem);
	z-index: 3;
	display: none;
	flex-wrap: wrap;
	align-items: center;
	gap: 0.5rem;
	align-self: center;
	max-width: calc(100% - var(--shell-gutter) * 2);
	margin: 0.75rem var(--shell-gutter) 0;
	padding: 0.5rem;
	border: 1px solid var(--shell-line);
	border-radius: 0.75rem;
	background: var(--shell-surface);
	box-shadow: 0 0.25rem 1rem color-mix(in srgb, CanvasText 16%, transparent);
}

.set:has(input[data-set-row]:checked) .set-bar {
	display: flex;
}

/* in a fixed window it is a row of its own under the part that scrolls */
.shell[data-shell='workspace'] .set-bar {
	position: static;
	margin-block: 0.75rem;
}

.set-bar output {
	padding-inline: 0.5rem;
	font-size: 0.9375rem;
	font-variant-numeric: tabular-nums;
	font-weight: 600;
	white-space: nowrap;
}

/* ---- Pages ---- */

.set-pages {
	display: flex;
	align-items: center;
	gap: 0.25rem;
	padding: 0.75rem var(--shell-gutter);
}

.set-pages ol {
	/* TRAP: a row of page numbers is wider than a phone. Narrow, the sentence stands for it. */
	display: none;
	gap: 0.25rem;
	margin: 0;
	padding: 0;
	list-style: none;
}

.set-pages p {
	flex: 1;
	margin: 0;
	color: var(--shell-muted);
	font-size: 0.9375rem;
	font-variant-numeric: tabular-nums;
	text-align: center;
	white-space: nowrap;
}

.set-pages a {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	gap: 0.25rem;
	min-width: var(--shell-hit);
	min-height: var(--shell-hit);
	padding-inline: 0.5rem;
	border-radius: 0.5rem;
	color: inherit;
	font-size: 0.9375rem;
	font-variant-numeric: tabular-nums;
	text-decoration: none;
	white-space: nowrap;
}

.set-pages svg {
	flex: none;
	width: 1rem;
	height: 1rem;
}

[dir='rtl'] .set-pages svg {
	transform: scaleX(-1);
}

/* the first step's arrow, not its ground, sits on the content's edge */
.set-pages > a:first-child {
	margin-inline-start: -0.5rem;
}

.set-pages a[aria-current='page'] {
	background: var(--shell-current);
	font-weight: 600;
}

/* TRAP: at the first and the last page a step can't be taken, and stays where it is and in
   reach of the keyboard. A control that disables itself drops the focus that was on it. */
.set-pages a[aria-disabled='true'] {
	color: var(--shell-muted);
	opacity: 0.6;
	cursor: default;
}

@media (hover: hover) {
	.set-pages a:not([aria-disabled='true'], [aria-current='page']):hover {
		background: var(--shell-current);
	}
}

@container set (min-width: 34rem) {
	.set-pages ol {
		display: flex;
	}

	.set-pages p {
		display: none;
	}
}

/* ---- The states of the whole set ---- */

/* Empty, no results, failed, no access: in the place where the rows would be, with what to do. */
.set-state {
	display: grid;
	justify-items: center;
	gap: 0.5rem;
	width: 100%;
	max-width: 26rem;
	margin-inline: auto;
	padding: 3rem var(--shell-gutter);
	text-align: center;
}

.set-state h2 {
	margin: 0;
	font-size: 1.0625rem;
	font-weight: 600;
	line-height: 1.3;
}

.set-state p {
	margin: 0;
	color: var(--shell-muted);
}

.set-state .set-button {
	margin-top: 0.5rem;
}

/* A plain key, for the states and the bar. Use the project's own button where it has one. */
.set-button {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	min-height: var(--shell-hit);
	padding-inline: 0.875rem;
	border: 1px solid var(--shell-line);
	border-radius: 0.5rem;
	background: var(--shell-surface);
	color: var(--shell-text);
	font: inherit;
	font-size: 0.9375rem;
	font-weight: 500;
	text-decoration: none;
	white-space: nowrap;
	cursor: pointer;
}

@media (hover: hover) {
	.set-button:hover {
		background: var(--shell-current);
	}
}

/* Loading: rows of the real geometry with bars where the words will be, so nothing moves when
   they arrive. A bar stands inside the row's own parts and takes their line height. */
.set-bone {
	display: inline-block;
	width: var(--bone, 8rem);
	max-width: 100%;
	height: 0.625em;
	border-radius: 999px;
	background: var(--shell-current);
	vertical-align: middle;
}

/* in place of a checkbox */
.set-tick .set-bone {
	width: 1.125rem;
	height: 1.125rem;
	border-radius: 0.25rem;
}

/* TRAP: it moves only for people who haven't asked for less motion */
@media (prefers-reduced-motion: no-preference) {
	.set-bone {
		animation: set-wait 1.2s ease-in-out infinite alternate;
	}

	@keyframes set-wait {
		to {
			opacity: 0.45;
		}
	}
}
