/*
 * app-shell.css: the frame of an app. Places to go, a bar with the title, the content.
 *
 * Copy this file. Map the tokens to the project's own. Keep every line marked TRAP: each one
 * prevents a fault that shows only at some width, some length of content or on some device.
 *
 *   <a class="shell-skip" href="#content">Skip to content</a>
 *   <div class="shell" data-shell="page">            page: the document scrolls
 *     <nav class="shell-nav" aria-label="Main">      workspace: the window is fixed, a part scrolls
 *                                                    split: a workspace from 600px, a page below
 *       <ul class="shell-places"> <li><a href aria-current="page"><svg/><span>Name</span></a>
 *     <header class="shell-bar"> <h1 class="shell-title"> <button class="shell-action">
 *     <main class="shell-content" id="content" tabindex="-1">
 *
 * Two size classes, by the window's width: compact below 600px (the places are a row at the
 * bottom), regular from 600px (the places are a column on the side). Working examples: page.html,
 * workspace.html. The page needs
 *   <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
 */

:root {
	color-scheme: light dark;

	/* Tokens. These follow the system's light and dark; replace them with the project's. */
	--shell-surface: Canvas;
	--shell-recess: color-mix(in srgb, CanvasText 5%, Canvas); /* the sidebar's ground */
	--shell-text: CanvasText;
	--shell-muted: color-mix(in srgb, CanvasText 62%, Canvas);
	--shell-line: color-mix(in srgb, CanvasText 14%, Canvas);
	--shell-current: color-mix(in srgb, CanvasText 10%, Canvas); /* behind the place you are in */
	--shell-focus: Highlight;

	--shell-gutter: 1rem; /* the content's side margin; the bar's too, so the title sits on the content's edge */
	--shell-nav-width: clamp(12rem, 24vw, 16rem);
	--shell-bar-height: 3.5rem;
	--shell-tabs-height: 3.5rem;
	--shell-hit: 2.25rem; /* a row or a button under a mouse: 36px */
	--shell-layer: 10; /* the bars, above the content; overlays go above this */

	/* How far the frame's bars reach over whatever scrolls. Something sticky inside the content (a
	   table's header, a bar of actions) stops there instead of sliding under them:
	   `top: var(--shell-cover-top)`, `bottom: var(--shell-cover-bottom)`. */
	--shell-cover-top: calc(var(--shell-bar-height) + env(safe-area-inset-top, 0px));
	--shell-cover-bottom: calc(var(--shell-tabs-height) + env(safe-area-inset-bottom, 0px));
}

/* TRAP: density follows the pointer, not the width. A touch tablet is wide and still needs 44px;
   a narrow desktop window does not. */
@media (pointer: coarse) {
	:root {
		--shell-hit: 2.75rem;
	}
}

html {
	/* TRAP: a jump to #anchor, or focus moving by Tab, lands under the sticky bar without this. */
	scroll-padding-top: calc(var(--shell-bar-height) + env(safe-area-inset-top, 0px) + 1rem);
	/* TRAP: and under the row of places at the bottom. To the browser a link behind that row is
	   in view, so it doesn't scroll it out from under it. */
	scroll-padding-bottom: var(--shell-cover-bottom);
	/* TRAP: without a reserved gutter the frame shifts sideways between a short page and a long one. */
	scrollbar-gutter: stable;
}

body {
	margin: 0;
}

.shell,
.shell *,
.shell-skip {
	box-sizing: border-box;
}

.shell {
	display: grid;
	/* TRAP: `1fr` means minmax(auto, 1fr): one wide child (a table, a long word) widens the whole
	   page. minmax(0, 1fr) lets the column stay as wide as the window. */
	grid-template-columns: minmax(0, 1fr);
	grid-template-rows: auto minmax(0, 1fr) auto; /* bar, content, places */
	/* TRAP: 100vh is taller than a phone's screen while the browser's bars show, so the last row
	   ends up under them. svh is the screen with the bars showing. */
	min-height: 100vh;
	min-height: 100svh;
	padding-left: env(safe-area-inset-left, 0px);
	padding-right: env(safe-area-inset-right, 0px);
	background: var(--shell-surface);
	color: var(--shell-text);
	font: 1rem/1.5 system-ui, sans-serif;
	-webkit-tap-highlight-color: transparent;
}

.shell :focus-visible,
.shell-skip:focus-visible {
	outline: 2px solid var(--shell-focus);
	outline-offset: 2px;
}

/* The first stop of the Tab key: over the places, into the content. Hidden until it has focus. */
.shell-skip {
	position: absolute;
	top: 0.5rem;
	inset-inline-start: 0.5rem;
	z-index: calc(var(--shell-layer) + 1);
	padding: 0.5rem 0.75rem;
	border-radius: 0.5rem;
	background: var(--shell-surface);
	color: var(--shell-text);
	transform: translateY(-200%);
}

.shell-skip:focus {
	transform: none;
}

/* ---- The bar: the title once, the page's own actions at its end ---- */

.shell-bar {
	grid-row: 1;
	position: sticky;
	top: 0;
	z-index: var(--shell-layer);
	display: flex;
	align-items: center;
	gap: 0.5rem;
	min-height: calc(var(--shell-bar-height) + env(safe-area-inset-top, 0px));
	padding: env(safe-area-inset-top, 0px) var(--shell-gutter) 0;
	border-bottom: 1px solid var(--shell-line);
	background: var(--shell-surface);
}

.shell-title {
	flex: 1;
	/* TRAP: a flex child never shrinks below its text. Without this a long title pushes the actions
	   off the screen instead of ending in an ellipsis. */
	min-width: 0;
	margin: 0;
	overflow: hidden;
	font-size: 1.0625rem;
	font-weight: 600;
	line-height: 1.3;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.shell-action {
	display: grid;
	flex: none;
	place-items: center;
	min-width: var(--shell-hit);
	min-height: var(--shell-hit);
	padding: 0;
	border: 0;
	border-radius: 0.5rem;
	background: none;
	color: inherit;
	font: inherit;
	cursor: pointer;
}

/* The glyph, not its hit area, sits on the content's edge. */
.shell-action:last-child {
	margin-inline-end: calc((var(--shell-hit) - 1.25rem) / -2);
}

.shell-action svg {
	width: 1.25rem;
	height: 1.25rem;
}

/* ---- The content ---- */

.shell-content {
	grid-row: 2;
	/* TRAP: the same as the title's. A grid child never shrinks below its content either. */
	min-width: 0;
	overflow-wrap: anywhere; /* a long word or address wraps instead of widening the page */
}

/* It takes focus when the skip link or a navigation sends it there; that needs no ring. */
.shell-content:focus {
	outline: none;
}

/* A reading column inside the content: text and forms. Tables and canvases go without it. */
.shell-page {
	max-width: 48rem;
	padding: 1rem var(--shell-gutter) 3rem;
}

/* ---- The places: compact, a row at the bottom ---- */

.shell-nav {
	grid-row: 3;
	/* Sticky, in a row of its own: it stays at the screen's lower edge and still takes its space,
	   so the content's last line is never under it. (A fixed bar needs padding to make up for it.) */
	position: sticky;
	bottom: 0;
	z-index: var(--shell-layer);
	padding-bottom: env(safe-area-inset-bottom, 0px); /* clear of the home indicator */
	border-top: 1px solid var(--shell-line);
	background: var(--shell-surface);
}

.shell-places {
	display: flex;
	margin: 0;
	padding: 0;
	list-style: none;
}

.shell-places li {
	flex: 1;
	min-width: 0;
}

.shell-places a {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	gap: 0.125rem;
	min-height: var(--shell-tabs-height);
	padding-inline: 0.25rem;
	color: var(--shell-muted);
	font-size: 0.6875rem;
	line-height: 1.2;
	text-decoration: none;
	outline-offset: -2px;
}

.shell-places span {
	max-width: 100%;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

/* The glyph in a pill: where you are shows at a glance, not as a slightly darker grey.
   aria-current is "page" on the place's own page and "true" on a page inside it. */
.shell-places svg {
	box-sizing: content-box;
	width: 1.5rem;
	height: 1.5rem;
	padding: 0.125rem 1rem;
	border-radius: 999px;
}

.shell-places a:is([aria-current='page'], [aria-current='true']) {
	color: var(--shell-text);
	font-weight: 600;
}

.shell-places a:is([aria-current='page'], [aria-current='true']) svg {
	background: var(--shell-current);
}

/* TRAP: on a touch screen :hover sticks to the last thing tapped. Only a pointer that can hover gets it. */
@media (hover: hover) {
	.shell-places a:hover {
		color: var(--shell-text);
	}

	.shell-action:hover {
		background: var(--shell-current);
	}
}

/* ---- Regular: from 600px the places are a column on the side ---- */

/* In em, so a larger text size in the browser moves to the compact frame sooner. */
@media (min-width: 37.5em) {
	:root {
		--shell-gutter: 2rem;
		--shell-cover-bottom: 0px; /* the places are beside the content now, not under it */
	}

	.shell {
		grid-template-columns: var(--shell-nav-width) minmax(0, 1fr);
		grid-template-rows: auto minmax(0, 1fr);
	}

	.shell-bar {
		grid-area: 1 / 2;
	}

	.shell-content {
		grid-area: 2 / 2;
	}

	.shell-nav {
		grid-area: 1 / 1 / -1 / 2;
		/* TRAP: without a height of its own and `sticky`, the sidebar scrolls away with a long page.
		   svh, not dvh: a column that follows the browser's bars resizes while you scroll. */
		top: 0;
		bottom: auto;
		align-self: start;
		height: 100vh;
		height: 100svh;
		overflow-y: auto;
		overscroll-behavior-y: contain; /* its own scroll ends there and doesn't move the page */
		/* the first row sits on the bar's line */
		padding: calc((var(--shell-bar-height) - var(--shell-hit)) / 2) 0.5rem 0.5rem;
		border-top: 0;
		background: var(--shell-recess);
	}

	.shell-places {
		flex-direction: column;
		gap: 0.125rem;
	}

	.shell-places a {
		flex-direction: row;
		justify-content: flex-start;
		gap: 0.75rem;
		min-height: var(--shell-hit);
		padding-inline: 0.75rem;
		border-radius: 0.5rem;
		font-size: 0.9375rem;
	}

	.shell-places svg {
		flex: none;
		width: 1.25rem;
		height: 1.25rem;
		padding: 0;
		border-radius: 0;
	}

	.shell-places a:is([aria-current='page'], [aria-current='true']),
	.shell-places a:is([aria-current='page'], [aria-current='true']) svg {
		background: none;
	}

	.shell-places a:is([aria-current='page'], [aria-current='true']) {
		background: var(--shell-current);
	}

	@media (hover: hover) {
		.shell-places a:hover {
			background: var(--shell-current);
		}
	}
}

/* ---- Workspace: the window is fixed and a part of the content scrolls ---- */

/* Nothing of the document scrolls, so it needs no gutter and should not bounce at its edges.
   (Safari ignores overscroll-behavior on a scroller that has nothing to scroll.) */
html:has(.shell[data-shell='workspace']) {
	overflow: hidden;
	scrollbar-gutter: auto;
	overscroll-behavior: none;
}

.shell[data-shell='workspace'] {
	/* dvh here: the frame is the window, and follows it when the browser's bars or, where the
	   browser resizes the page for it, the keyboard change its height. */
	height: 100vh;
	height: 100dvh;
	min-height: 0;
}

.shell[data-shell='workspace'] .shell-content {
	display: flex;
	flex-direction: column;
	/* TRAP: without min-height: 0 the content grows as tall as what is in it, and the page scrolls
	   instead of the part that should. */
	min-height: 0;
}

/* The part that scrolls. Everything beside it in the content (a composer, a toolbar) stays.
   Only inside a fixed window: in a page it must not be a scroller, or sticky parts inside it
   stop sticking. */
.shell[data-shell='workspace'] .shell-scroll {
	flex: 1;
	min-height: 0; /* TRAP: as above, for a flex child */
	overflow: auto;
	overscroll-behavior: contain;
	scrollbar-gutter: stable;
	/* the bars are outside this scroller, so nothing covers its edges */
	--shell-cover-top: 0px;
	--shell-cover-bottom: 0px;
}

.shell[data-shell='workspace'] .shell-nav {
	position: static;
}

@media (min-width: 37.5em) {
	.shell[data-shell='workspace'] .shell-nav {
		align-self: stretch;
		height: auto;
		min-height: 0;
	}
}

/* ---- Split: two panes side by side that are one pane on a phone ---- */

/* The kind of scrolling follows the number of panes. From 600px the frame is a workspace, the
   same rules as above, so each pane can scroll on its own. Below, one pane shows at a time and
   the frame is a page. The panes themselves are app-navigation's. */
@media (min-width: 37.5em) {
	html:has(.shell[data-shell='split']) {
		overflow: hidden;
		scrollbar-gutter: auto;
		overscroll-behavior: none;
	}

	.shell[data-shell='split'] {
		height: 100vh;
		height: 100dvh;
		min-height: 0;
	}

	.shell[data-shell='split'] .shell-content {
		display: flex;
		flex-direction: column;
		min-height: 0;
	}

	.shell[data-shell='split'] .shell-scroll {
		flex: 1;
		min-height: 0;
		overflow: auto;
		overscroll-behavior: contain;
		scrollbar-gutter: stable;
		--shell-cover-top: 0px;
		--shell-cover-bottom: 0px;
	}

	.shell[data-shell='split'] .shell-nav {
		position: static;
		align-self: stretch;
		height: auto;
		min-height: 0;
	}
}

/* ---- A line under the bar only once something has scrolled under it ---- */

/* Where the browser can tie an animation to the scroll position. Elsewhere the line is always there. */
@supports (animation-timeline: scroll()) {
	.shell[data-shell='page'] .shell-bar {
		animation: shell-bar-line linear both;
		animation-timeline: scroll(root);
		animation-range: 0 0.5rem;
	}

	@keyframes shell-bar-line {
		from {
			border-bottom-color: transparent;
		}
	}
}
