/*
 * PocketWeb's bundled stylesheet (design.md "assets (browser)"). It styles the generated markup:
 * `body` classes, `nav > ul` nested with `aria-current="page"`, `main > div.item`, server-rendered
 * accordion and tabs `main > details`, figures and the more link.
 *
 * Layouts are keyed to `body[data-layout]`, which script.js sets to the first layout class in plain
 * string order (17.11). Without scripting no layout rule applies, so every layout reads as a plain
 * document; only the accordion and tabs `details` panels, which the server renders, still open and
 * close (17.14).
 *
 * The one breakpoint is `40em`: `width < 40em` is a narrow screen. Media queries cannot read custom
 * properties, so every rule that depends on it repeats that value.
 */

/* --- Base --- */

:root {
  color-scheme: light dark;
  --gap: 1rem;
  --border: color-mix(in srgb, currentColor 20%, transparent);
  --radius: 0.5rem;
}

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

body {
  margin: 0 auto;
  max-width: 80rem;
  padding: var(--gap);
  font-family: system-ui, sans-serif;
  line-height: 1.5;
}

img,
video {
  max-width: 100%;
  height: auto;
}

audio {
  max-width: 100%;
}

figure {
  margin: 0;
}

/* Secondary text: smaller and muted by mixing with the background, not faded with opacity. A date
 * inside a caption takes the caption's style once instead of shrinking twice. */
figcaption,
time:not(figcaption *, summary > time) {
  font-size: 0.875em;
  color: color-mix(in srgb, currentColor 75%, Canvas);
}

/* Spacing between blocks of a plain document. `:where` gives it no specificity, so a layout that
 * spaces its items with `gap` overrides it wherever its rule sits. */
:where(body > main) > :where(.item, details) + :where(.item),
:where(body > main) > :where(.item) + :where(details),
:where(body > main > details > .panel) > :where(.item) + :where(.item),
:where(body > main > a.more) {
  margin-top: var(--gap);
}

body > main > a.more {
  display: block;
}

/* The skip link is visually hidden, but still read out, until it has keyboard focus. */
a.skip:not(:focus) {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/*
 * --- Page frame: the menu beside the content on wide screens ---
 * Below 40em (a narrow screen) the menu collapses and multi-column layouts stack.
 */

@media (width >= 40em) {
  body {
    display: grid;
    grid-template-columns: minmax(10rem, 15rem) minmax(0, 1fr);
    grid-template-areas:
      'skip skip'
      'header header'
      'nav main'
      'nav aside'
      'footer footer';
    grid-template-rows: auto auto auto 1fr auto;
    column-gap: calc(2 * var(--gap));
  }

  body > a.skip {
    grid-area: skip;
  }

  body > header {
    grid-area: header;
  }

  body > nav {
    grid-area: nav;
  }

  body > main {
    grid-area: main;
  }

  body > aside {
    grid-area: aside;
  }

  body > footer {
    grid-area: footer;
  }
}

/* --- Navigation (15.6–15.8) --- */

body > nav ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

body > nav ul ul {
  padding-inline-start: var(--gap);
}

body > nav a {
  display: block;
  padding: 0.125rem 0;
}

body > nav a[aria-current='page'] {
  font-weight: bold;
}

/*
 * With scripting, script.js marks the entries from the current page up with `expanded` (15.7), and
 * only those branches stay open. Without scripting nothing is marked and the menu stays fully
 * expanded (15.6); so does a page that has no entry in the menu.
 */
body > nav:has(li.expanded) li:not(.expanded) > ul {
  display: none;
}

/*
 * On a narrow screen the menu collapses to the top level and the branch down to the current page,
 * whose children stay reachable (15.8). This needs no script, so it also holds without scripting.
 */
@media (width < 40em) {
  body > nav li:not(:has(a[aria-current='page'])) > ul {
    display: none;
  }

  body > nav {
    margin-bottom: var(--gap);
    padding-bottom: var(--gap);
    border-bottom: 1px solid var(--border);
  }
}

/* --- Accordion panels: server-rendered `details`, collapsible with or without scripting --- */

body > main > details {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0 var(--gap);
}

body > main > details + details {
  margin-top: calc(var(--gap) / 2);
}

body > main > details > summary {
  cursor: pointer;
  padding: calc(var(--gap) / 2) 0;
}

/* The heading sits beside the disclosure marker. */
body > main > details > summary > :is(h1, time) {
  display: inline;
  font-size: 1.25em;
  margin: 0;
}

body > main > details[open] > summary {
  margin-bottom: calc(var(--gap) / 2);
}

body > main > details > .panel {
  margin-bottom: var(--gap);
}

/*
 * --- Tabs: script.js adds a tab bar over the server's panels (17.2) and sets `main[data-tabs-view]`
 * to `tabs`, or to `accordion` while the tab bar does not fit (17.3); the panels then look as
 * above. The tab bar never wraps, so a tab sticking out shows that it does not fit; in the
 * accordion view it stays laid out at the full width, invisible and without height, so it can
 * still be measured.
 */

body[data-layout='tabs'] > main > [role='tablist'] {
  display: flex;
  overflow: hidden;
  border-bottom: 1px solid var(--border);
}

body[data-layout='tabs'] > main > [role='tablist'] > [role='tab'] {
  flex: none;
  margin-bottom: -1px;
  padding: calc(var(--gap) / 2) var(--gap);
  border: 1px solid transparent;
  border-radius: var(--radius) var(--radius) 0 0;
  background: none;
  color: inherit;
  font: inherit;
  white-space: nowrap;
  cursor: pointer;
}

body[data-layout='tabs'] > main > [role='tablist'] > [aria-selected='true'] {
  border-color: var(--border);
  border-bottom-color: Canvas;
  background: Canvas;
  font-weight: bold;
}

/* Spaced from the items above it like a panel. */
body[data-layout='tabs'] > main[data-tabs-view='tabs'] > * + [role='tablist'] {
  margin-top: var(--gap);
}

body[data-layout='tabs'] > main[data-tabs-view='accordion'] > [role='tablist'] {
  visibility: hidden;
  height: 0;
  border: 0;
}

body[data-layout='tabs'] > main[data-tabs-view='accordion'] > * + [role='tablist'] + details {
  margin-top: var(--gap);
}

/* The tab names the panel, so its header is not shown. */
body[data-layout='tabs'] > main[data-tabs-view='tabs'] > details {
  margin-top: 0;
  border: 0;
  padding: var(--gap) 0 0;
}

body[data-layout='tabs'] > main[data-tabs-view='tabs'] > details:not([open]),
body[data-layout='tabs'] > main[data-tabs-view='tabs'] > details > summary {
  display: none;
}

/* --- Cards: a grid of cards (17.4) --- */

body[data-layout='cards'] > main {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(16rem, 100%), 1fr));
  gap: var(--gap);
}

body[data-layout='cards'] > main > .item {
  margin: 0;
  padding: var(--gap);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

body[data-layout='cards'] > main > .item > :first-child {
  margin-top: 0;
}

/* --- Split: equal-width columns side by side, stacked on a narrow screen (17.7) --- */

body[data-layout='split'] > main {
  display: flex;
  flex-wrap: wrap;
  gap: var(--gap);
}

body[data-layout='split'] > main > .item {
  flex: 1 1 0;
  min-width: 0;
  margin: 0;
}

@media (width < 40em) {
  body[data-layout='split'] > main > .item {
    flex-basis: 100%;
  }
}

/*
 * --- Carousel and slider (17.5, 17.6): script.js gathers the items into `section.slideshow`, with
 * its controls over a viewport that clips a row of slides. The stylesheet moves the row to the
 * current slide, `--slide` (from 0): a carousel shows it centred at 80% of the width with its
 * neighbours in part beside it, a slider at the full width alone.
 */

body[data-layout='carousel'] > main > .slideshow {
  --slide-width: 80%;
}

body[data-layout='slider'] > main > .slideshow {
  --slide-width: 100%;
}

body:is([data-layout='carousel'], [data-layout='slider']) .slideshow-controls {
  display: flex;
  justify-content: center;
  gap: calc(var(--gap) / 2);
  margin-bottom: var(--gap);
}

/* With one slide the controls are `hidden`, which the `display` above would otherwise override. */
body:is([data-layout='carousel'], [data-layout='slider']) .slideshow-controls[hidden] {
  display: none;
}

body:is([data-layout='carousel'], [data-layout='slider']) .slideshow-controls > button {
  min-width: 2.75rem;
  padding: 0.25rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: none;
  color: inherit;
  font: inherit;
  cursor: pointer;
}

/* A sideways touch drags the slides (17.18); an upright one still scrolls the page, and a pinch
   still zooms it. */
body:is([data-layout='carousel'], [data-layout='slider']) .slideshow-viewport {
  overflow: hidden;
  touch-action: pan-y pinch-zoom;
}

/* The row is as wide as the viewport, so its own width is the percentage base of the shift. */
body:is([data-layout='carousel'], [data-layout='slider']) .slideshow-slides {
  display: flex;
  align-items: flex-start;
  gap: var(--gap);
  transform: translateX(
    calc(
      (100% - var(--slide-width)) / 2 - var(--slide, 0) * (var(--slide-width) + var(--gap)) +
        var(--drag, 0px)
    )
  );
  transition: transform 0.4s ease;
}

/* While dragged, the slides follow the pointer at once and their text is not selected. */
body:is([data-layout='carousel'], [data-layout='slider']) .slideshow-slides.dragging {
  transition: none;
  -webkit-user-select: none;
  user-select: none;
}

@media (prefers-reduced-motion: reduce) {
  body:is([data-layout='carousel'], [data-layout='slider']) .slideshow-slides {
    transition: none;
  }
}

body:is([data-layout='carousel'], [data-layout='slider']) .slideshow-slides > .item {
  flex: 0 0 var(--slide-width);
  min-width: 0;
}

body:is([data-layout='carousel'], [data-layout='slider']) .slideshow-slides > .item figure > img,
body:is([data-layout='carousel'], [data-layout='slider']) .slideshow-slides > .item figure > video {
  display: block;
  width: 100%;
}

/*
 * --- Gallery: same-size thumbnails opening a lightbox (17.8) ---
 * script.js wraps each item's image in `button.gallery-thumbnail` and adds `dialog.lightbox` to the
 * body. The thumbnails are square crops in a grid; the lightbox fills the screen and shows the
 * image whole, at its own size up to the screen's, with its caption below.
 */

body[data-layout='gallery'] > main {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(10rem, 1fr));
  align-items: start;
  gap: var(--gap);
}

body[data-layout='gallery'] > main > .item {
  min-width: 0;
  margin: 0;
}

body[data-layout='gallery'] .gallery-thumbnail {
  display: block;
  width: 100%;
  padding: 0;
  border: 0;
  border-radius: var(--radius);
  background: none;
  cursor: zoom-in;
}

body[data-layout='gallery'] .gallery-thumbnail > img {
  display: block;
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  border-radius: var(--radius);
}

body[data-layout='gallery'] .gallery-thumbnail:focus-visible {
  outline: 2px solid;
  outline-offset: 2px;
}

body[data-layout='gallery'] > dialog.lightbox {
  color-scheme: dark;
  width: 100%;
  max-width: none;
  height: 100%;
  max-height: none;
  margin: 0;
  padding: calc(var(--gap) * 3) var(--gap) var(--gap);
  border: 0;
  background: rgb(0 0 0 / 0.9);
  color: white;
}

body[data-layout='gallery'] > dialog.lightbox[open] {
  display: flex;
  align-items: center;
  justify-content: center;
}

body[data-layout='gallery'] > dialog.lightbox::backdrop {
  background: none;
}

body[data-layout='gallery'] > dialog.lightbox > figure {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: calc(var(--gap) / 2);
  max-height: 100%;
  min-height: 0;
}

body[data-layout='gallery'] > dialog.lightbox > figure > img {
  min-height: 0;
  object-fit: contain;
}

body[data-layout='gallery'] > dialog.lightbox > .lightbox-close {
  position: absolute;
  inset-block-start: calc(var(--gap) / 2);
  inset-inline-end: calc(var(--gap) / 2);
  min-width: 2.75rem;
  min-height: 2.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: none;
  color: inherit;
  font: inherit;
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
}

/*
 * --- Collage: packed rows of cells sized by each item's aspect ratio (17.9) ---
 * script.js puts each loaded image's (or video's) ratio in its cell's `--ratio`. A cell starts at
 * that ratio times the row height and grows in proportion to it, so every cell of a row grows to
 * the same height and keeps its image's proportions while the row fills the width. Until its image
 * has loaded a cell is a square. The trailing spacer takes up the last row's free space so its
 * cells stay at the row height.
 */

body[data-layout='collage'] {
  --collage-row: 12rem;
  --collage-gap: calc(var(--gap) / 2);
}

body[data-layout='collage'] > main {
  display: flex;
  flex-wrap: wrap;
  gap: var(--collage-gap);
}

/*
 * The spacer's grow factor is far above any cell's ratio, so it takes all but a fraction of a
 * percent of the last row's free space; the cells' share is too small to see. The negative margin
 * cancels the gap before it, so it never wraps onto a row of its own.
 */
body[data-layout='collage'] > main::after {
  content: '';
  flex: 1000 1 0;
  margin-inline-start: calc(-1 * var(--collage-gap));
}

body[data-layout='collage'] > main > .item {
  flex: var(--ratio, 1) 1 calc(var(--ratio, 1) * var(--collage-row));
  margin: 0;
}

/* An item without an image or video has no aspect ratio; it starts at a fixed width. */
body[data-layout='collage'] > main > .item:not(:has(> figure > :is(img, video))) {
  flex: 1 1 16rem;
}

body[data-layout='collage'] > main > .item > figure > :is(img, video) {
  display: block;
  width: 100%;
}

/*
 * The first figure's image or video sets the cell's ratio. Further media of the same item (say a
 * photo and its video) follow below it at their own ratio, which makes that cell taller than the
 * rest of its row.
 */
body[data-layout='collage'] > main > .item > figure:first-of-type > :is(img, video) {
  aspect-ratio: var(--ratio, 1);
  object-fit: cover;
}

/*
 * --- Layout-wide elements: the more link always takes a whole row ---
 * In a collage it is ordered after the trailing spacer, so the spacer still ends the last row of
 * items on a paged collage.
 */

body:is(
    [data-layout='cards'],
    [data-layout='split'],
    [data-layout='collage'],
    [data-layout='gallery']
  )
  > main
  > a.more {
  margin-top: 0;
}

body:is([data-layout='cards'], [data-layout='gallery']) > main > a.more {
  grid-column: 1 / -1;
}

body:is([data-layout='split'], [data-layout='collage']) > main > a.more {
  flex-basis: 100%;
}

body[data-layout='collage'] > main > a.more {
  order: 1;
}

/*
 * --- Hero: the first item as a full-width banner above the layout (17.10) ---
 * Only the static layouts arranged here get one; 8.4–8.6 add it for the layouts they script. The
 * banner is the first item of the page as served: a later page loaded directly has its own, while
 * items appended through the more link carry on in the layout.
 */

body.hero[data-layout='cards'] > main > .item:first-child {
  grid-column: 1 / -1;
}

/* An image banner fills the row edge to edge; a text banner keeps its card frame. */
body.hero[data-layout='cards'] > main > .item:first-child:has(> figure > :is(img, video)) {
  padding: 0;
  border: 0;
}

body.hero:is([data-layout='split'], [data-layout='collage']) > main > .item:first-child {
  flex: 0 0 100%;
}

body.hero:is([data-layout='cards'], [data-layout='split'], [data-layout='collage'])
  > main
  > .item:first-child
  > figure
  > :is(img, video) {
  display: block;
  width: 100%;
  aspect-ratio: auto;
  max-height: 60vh;
  object-fit: cover;
}
