/* coloq stylesheet. Classless: element, ARIA and a few data-* selectors.
   Markup contract: docs/ui/README.md. Prototypes: docs/ui/*.html. */

/* Fonts: three files on the common path (Latin subsets). Latin Extended files
   load only when a page contains characters outside Latin-1. Licences sit next
   to the files in fonts/. The Fallback faces map local system fonts onto the
   metrics of the web fonts so the swap does not move text. */
@font-face{font-family:Alegreya;font-style:normal;font-weight:400 900;font-display:swap;src:url(../fonts/alegreya-latin-wght-normal.woff2) format("woff2");unicode-range:U+0000-00FF,U+0131,U+0152-0153,U+02BB-02BC,U+02C6,U+02DA,U+02DC,U+0304,U+0308,U+0329,U+2000-206F,U+20AC,U+2122,U+2191,U+2193,U+2212,U+2215,U+FEFF,U+FFFD}
@font-face{font-family:Alegreya;font-style:normal;font-weight:400 900;font-display:swap;src:url(../fonts/alegreya-latin-ext-wght-normal.woff2) format("woff2");unicode-range:U+0100-02BA,U+02BD-02C5,U+02C7-02CC,U+02CE-02D7,U+02DD-02FF,U+0304,U+0308,U+0329,U+1D00-1DBF,U+1E00-1E9F,U+1EF2-1EFF,U+2020,U+20A0-20AB,U+20AD-20C0,U+2113,U+2C60-2C7F,U+A720-A7FF}
@font-face{font-family:Alegreya;font-style:italic;font-weight:400 900;font-display:swap;src:url(../fonts/alegreya-latin-wght-italic.woff2) format("woff2");unicode-range:U+0000-00FF,U+0131,U+0152-0153,U+02BB-02BC,U+02C6,U+02DA,U+02DC,U+0304,U+0308,U+0329,U+2000-206F,U+20AC,U+2122,U+2191,U+2193,U+2212,U+2215,U+FEFF,U+FFFD}
@font-face{font-family:Alegreya;font-style:italic;font-weight:400 900;font-display:swap;src:url(../fonts/alegreya-latin-ext-wght-italic.woff2) format("woff2");unicode-range:U+0100-02BA,U+02BD-02C5,U+02C7-02CC,U+02CE-02D7,U+02DD-02FF,U+0304,U+0308,U+0329,U+1D00-1DBF,U+1E00-1E9F,U+1EF2-1EFF,U+2020,U+20A0-20AB,U+20AD-20C0,U+2113,U+2C60-2C7F,U+A720-A7FF}
@font-face{font-family:"Instrument Sans";font-style:normal;font-weight:400 700;font-display:swap;src:url(../fonts/instrument-sans-latin-wght-normal.woff2) format("woff2");unicode-range:U+0000-00FF,U+0131,U+0152-0153,U+02BB-02BC,U+02C6,U+02DA,U+02DC,U+0304,U+0308,U+0329,U+2000-206F,U+20AC,U+2122,U+2191,U+2193,U+2212,U+2215,U+FEFF,U+FFFD}
@font-face{font-family:"Instrument Sans";font-style:normal;font-weight:400 700;font-display:swap;src:url(../fonts/instrument-sans-latin-ext-wght-normal.woff2) format("woff2");unicode-range:U+0100-02BA,U+02BD-02C5,U+02C7-02CC,U+02CE-02D7,U+02DD-02FF,U+0304,U+0308,U+0329,U+1D00-1DBF,U+1E00-1E9F,U+1EF2-1EFF,U+2020,U+20A0-20AB,U+20AD-20C0,U+2113,U+2C60-2C7F,U+A720-A7FF}
@font-face{font-family:"Alegreya Fallback";font-weight:400;src:local("Times New Roman"),local("Liberation Serif"),local(Tinos);size-adjust:101.1%;ascent-override:100.5%;descent-override:34.1%;line-gap-override:0%}
@font-face{font-family:"Alegreya Fallback";font-weight:700;src:local("Times New Roman Bold"),local("Liberation Serif Bold"),local("Tinos Bold");size-adjust:95.9%;ascent-override:105.9%;descent-override:36%;line-gap-override:0%}
@font-face{font-family:"Alegreya Fallback";font-style:italic;src:local("Times New Roman Italic"),local("Liberation Serif Italic"),local("Tinos Italic");size-adjust:88.6%;ascent-override:114.7%;descent-override:38.9%;line-gap-override:0%}
@font-face{font-family:"Instrument Sans Fallback";font-weight:400;src:local(Arial),local("Liberation Sans"),local(Arimo);size-adjust:101.5%;ascent-override:95.6%;descent-override:24.6%;line-gap-override:0%}
@font-face{font-family:"Instrument Sans Fallback";font-weight:700;src:local("Arial Bold"),local("Liberation Sans Bold"),local("Arimo Bold");size-adjust:98.1%;ascent-override:98.9%;descent-override:25.5%;line-gap-override:0%}

:root {
  color-scheme: light dark;
  --serif: Alegreya, "Alegreya Fallback", Georgia, serif;
  --sans: "Instrument Sans", "Instrument Sans Fallback", system-ui, sans-serif;
  --mono: ui-monospace, "Cascadia Code", "SF Mono", Menlo, Consolas, "DejaVu Sans Mono", monospace;

  --fs-meta: clamp(.8125rem, .79rem + .1vw, .875rem);
  --fs-ui: 1rem;
  --fs-copy: clamp(1.125rem, 1.08rem + .25vw, 1.25rem);
  --fs-prose: clamp(1.1875rem, 1.1rem + .4vw, 1.3125rem);
  --fs-h3: clamp(1.25rem, 1.18rem + .35vw, 1.4375rem);
  --fs-h2: clamp(1.5rem, 1.35rem + .7vw, 1.875rem);
  --fs-h1: clamp(1.875rem, 1.45rem + 2.1vw, 3rem);

  --s-1: .25rem; --s-2: .5rem; --s-3: .75rem; --s-4: 1rem;
  --s-5: 1.5rem; --s-6: 2rem; --s-7: 3rem; --s-8: 4.5rem;
  --gutter: clamp(1rem, .4rem + 3vw, 2.5rem);
  --page: 58rem;
  --margin-col: 10.5rem;
  --hit: 2.75rem;
  --hit-s: 2.75rem;
  --r-1: 5px;
  --r-2: 12px;

  --ease: cubic-bezier(.2, .7, .2, 1);
  --dur-1: 120ms;
  --dur-2: 180ms;
  --dur-3: 220ms;

  /* light theme */
  --paper: #f6f6f2;
  --surface: #fdfdfb;
  --sunken: #ebebe5;
  --ink: #181b29;
  --ink-2: #444a5f;
  --ink-3: #5c6275;
  --edge: #7b8196;
  --rule: #d6d7ce;
  --accent: #2a3bd1;
  --accent-press: #1c2894;
  --accent-wash: #e2e6fb;
  --on-accent: #ffffff;
  --focus: #2a3bd1;
  --signal: #b45f06;
  --signal-ink: #8a4700;
  --signal-wash: #fbeccf;
  --danger: #b42318;
  --danger-wash: #fbe7e4;
  --ok: #176b3a;
  --ok-wash: #e0f1e6;
  --mark: #fde68a;
  --shimmer: rgb(255 255 255 / .55);
  --shadow: 0 18px 40px -18px rgb(24 27 41 / .45);
  --scrim: rgb(24 27 41 / .5);
}
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #0f1220;
    --surface: #171a2b;
    --sunken: #0a0c16;
    --ink: #ebe9e1;
    --ink-2: #b7bacb;
    --ink-3: #9094a9;
    --edge: #666c85;
    --rule: #272b40;
    --accent: #a9b6ff;
    --accent-press: #cdd4ff;
    --accent-wash: #1e2548;
    --on-accent: #0f1220;
    --focus: #a9b6ff;
    --signal: #f0a93b;
    --signal-ink: #f3bd68;
    --signal-wash: #2d2512;
    --danger: #ff8f85;
    --danger-wash: #351a1c;
    --ok: #6fd49a;
    --ok-wash: #132a1e;
    --mark: #5e4d10;
    --shimmer: rgb(255 255 255 / .06);
    --shadow: 0 18px 40px -14px rgb(0 0 0 / .8);
    --scrim: rgb(5 6 12 / .7);
  }
}
@media (pointer: fine) and (min-width: 48rem) { :root { --hit-s: 2.25rem } }

@layer base, layout, components, state, motion;

@layer base {
*, ::before, ::after { box-sizing: border-box }
* { margin: 0 }
html { text-size-adjust: 100%; scroll-padding-block: var(--s-4) 14rem }
body { min-block-size: 100dvh; display: grid; grid-template-rows: auto 1fr auto; background: var(--paper); color: var(--ink); font: 400 var(--fs-ui)/1.5 var(--sans); overflow-wrap: break-word; hanging-punctuation: first }
::selection { background: var(--accent); color: var(--on-accent) }
:where(img, video) { display: block; max-inline-size: 100%; block-size: auto }
svg { inline-size: 1.25em; block-size: 1.25em; flex: none; fill: none; stroke: currentColor; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px }
h1, h2, h3, h4 { font: 700 var(--fs-h3)/1.15 var(--serif); letter-spacing: -.005em; word-spacing: .08em; text-wrap: balance; overflow-wrap: anywhere }
h1 { font-size: var(--fs-h1); line-height: 1.08; letter-spacing: -.015em; word-spacing: .06em }
h2 { font-size: var(--fs-h2) }
h4 { font-size: 1.0625rem }
p { max-inline-size: 68ch; text-wrap: pretty }
small { font: 500 var(--fs-meta)/1.4 var(--sans); color: var(--ink-3) }
time { font-variant-numeric: tabular-nums }
strong, b { font-weight: 700 }
a { color: var(--accent); text-decoration: underline; text-decoration-thickness: .08em; text-underline-offset: .2em; text-decoration-color: color-mix(in srgb, currentColor 45%, transparent); &:hover { color: var(--accent-press); text-decoration-color: currentColor } }
mark { background: var(--mark); color: inherit; padding: 0 .15em; border-radius: 3px; box-decoration-break: clone }
hr { border: 0; border-block-start: 1px solid var(--rule); margin-block: var(--s-6) }
code, kbd, pre { font-family: var(--mono); font-size: .86em }
kbd { padding: .05em .4em; border: 1px solid var(--edge); border-radius: 4px; background: var(--surface); font-size: .8em }
dl { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: var(--s-1) var(--s-5); align-items: baseline }
dt { color: var(--ink-3); font-size: var(--fs-meta) }
dd { margin: 0 }
:where(ul, ol, menu) { padding: 0; list-style: none }
menu { display: flex; flex-wrap: wrap; gap: var(--s-2) }
:where(main, section, article > div, [contenteditable], blockquote) > * + * { margin-block-start: var(--flow, var(--s-4)) }
[hidden]:not(body > output) { display: none !important }
}

@layer layout {
body > a:first-child[href^="#"] { position: fixed; inset: var(--s-3) auto auto var(--s-3); z-index: 60; padding: var(--s-2) var(--s-4); background: var(--ink); color: var(--paper); border-radius: var(--r-1); transform: translateY(-200%); &:focus { transform: none } }

body > header {
  display: grid; grid-template: "brand acct" auto "nav nav" auto / 1fr auto; align-items: center; column-gap: var(--s-4); padding-inline: var(--gutter); border-block-end: 1px solid var(--rule);
  & > a[rel="home"] { grid-area: brand; font: 800 1.75rem/1 var(--serif); letter-spacing: -.03em; color: var(--ink); text-decoration: none; padding-block: var(--s-3) }
  & > a[rel="home"]::before { content: "\00ab"; content: "\00ab" / ""; color: var(--accent); margin-inline-end: .04em }
  & > nav:nth-of-type(1) { grid-area: nav; margin-inline: calc(-1 * var(--s-3)) }
  & > nav:nth-of-type(2) { grid-area: acct }
  @media (min-width: 44rem) { grid-template: "brand nav acct" auto / auto 1fr auto; column-gap: var(--s-6); & > nav:nth-of-type(1) { margin-inline: 0 } }
}
nav {
  & ul { display: flex; flex-wrap: wrap; gap: 0 var(--s-1) }
  & a { display: inline-flex; align-items: center; min-block-size: var(--hit); padding-inline: var(--s-3); color: var(--ink-2); font-weight: 600; text-decoration: none; border-radius: var(--r-1); &:hover { color: var(--ink); background: var(--sunken) } &[aria-current] { color: var(--ink); box-shadow: inset 0 -3px 0 var(--accent); border-radius: 0 } }
}
main nav {
  & ol { display: flex; flex-wrap: wrap; align-items: center; & a { padding-inline: 0; font-weight: 500 } & li + li::before { content: "/"; margin-inline: var(--s-2); color: var(--edge) } }
  & ul { margin-inline: calc(-1 * var(--s-3)); overflow-x: auto; flex-wrap: nowrap; scrollbar-width: none; border-block-end: 1px solid var(--rule); & a { white-space: nowrap } }
}

body > main { inline-size: min(100% - 2 * var(--gutter), var(--page)); margin-inline: auto; padding-block: var(--s-6) var(--s-8); container-type: inline-size; --flow: var(--s-5) }
body:not(:has(> header nav)) > main { inline-size: min(100% - 2 * var(--gutter), 27rem); padding-block-start: var(--s-7) }
main > header { display: grid; gap: var(--s-3); --flow: var(--s-3);
  & > p:not(:has(small)) { font: 400 var(--fs-copy)/1.45 var(--serif); color: var(--ink-2) }
  & > p:has(small) { display: flex; flex-wrap: wrap; align-items: center; gap: var(--s-2) var(--s-3) }
  & > h1 + p { margin-block-start: 0 }
}
main > section + section, main > section + form, main > form + section { margin-block-start: var(--s-7) }
body > footer { padding: var(--s-5) var(--gutter); border-block-start: 1px solid var(--rule); color: var(--ink-3); font-size: var(--fs-meta); & p { max-inline-size: none } }
}

@layer components {
/* Serif is for writing; sans is for the interface around it. */
:where(main > p, main > section > p, main > header > p, dialog p, details > p, blockquote, article > div, [contenteditable]) { font-family: var(--serif); font-size: var(--fs-copy); line-height: 1.5 }

/* List pattern: ol|ul > li > article (header, optional body div, optional footer). */
:where(ol, ul):has(> li > article) { --flow: 0 }
li + li > article { border-block-start: 1px solid var(--rule) }
article {
  position: relative; display: grid; grid-template-columns: minmax(0, 1fr); align-content: start; gap: var(--s-2) var(--s-5); margin-inline: calc(-1 * var(--s-3)); padding: var(--s-5) var(--s-3);
  & > :where(header, footer) { min-inline-size: 0 }
  & > header { display: flex; flex-wrap: wrap; align-items: baseline; gap: var(--s-1) var(--s-3);
    & :where(h2, h3) { font-size: var(--fs-h3); line-height: 1.2; flex: 1 1 14rem; & a { color: var(--ink); text-decoration: none; font-weight: 600 } & a::after { content: ""; position: absolute; inset: 0 } & a:focus-visible { outline: 0 } & a:focus-visible::after { outline: 2px solid var(--focus); outline-offset: -2px; border-radius: var(--r-1) } }
    & > p { max-inline-size: none; display: flex; flex-wrap: wrap; align-items: baseline; gap: 0 var(--s-2) }
    & > p:first-child:has(a[rel="author"]) { font-weight: 700; font-size: 1.0625rem }
    & > p:last-child { color: var(--ink-3); font: 500 var(--fs-meta)/1.4 var(--sans); & a { color: inherit; text-decoration: none; &:hover { color: var(--ink); text-decoration: underline } } }
  }
  & > footer { display: flex; flex-wrap: wrap; align-items: center; gap: var(--s-1) var(--s-5); font: 500 var(--fs-meta)/1.4 var(--sans); color: var(--ink-3);
    & > span { white-space: nowrap }
    & p[role] { flex: 1 1 100%; max-inline-size: none }
    &:has(> button) { gap: 0 var(--s-1); margin-inline: calc(-1 * var(--s-3)) }
  }
  & > p { color: var(--ink-2); max-inline-size: 62ch }
  & > p:not(:has(small)) { font: 400 var(--fs-copy)/1.45 var(--serif) }
  &:has(> div) { padding-block: var(--s-5) var(--s-4); & > header { gap: 0 var(--s-3); justify-content: space-between } }
  & > div { max-inline-size: 66ch; min-inline-size: 0; font-size: var(--fs-prose); line-height: 1.55; overflow-wrap: anywhere; --flow: .8em }
}
a[rel="author"] { color: var(--ink); text-decoration: none; &:hover { text-decoration: underline } }
small:has(> svg) { display: inline-flex; align-items: center; gap: .3rem; padding: .05rem .55rem .05rem .4rem; border: 1px solid var(--rule); border-radius: 99px; color: var(--ink-2); background: var(--surface); & svg { inline-size: .95em; block-size: .95em } }
small[data-role="admin"] { padding: .05rem .55rem; border-radius: 99px; background: var(--accent-wash); color: var(--accent); font-weight: 600; letter-spacing: .02em }
small[data-state="unread"] { padding: .05rem .55rem; border-radius: 99px; background: var(--signal-wash); color: var(--signal-ink); font-weight: 600 }
@media (hover: hover) { li > article:not(:has(> div)):hover { background: color-mix(in srgb, var(--accent-wash) 55%, transparent) } }
@container (min-width: 46rem) {
  article:has(> div) { grid-template-columns: var(--margin-col) minmax(0, 1fr); padding-block: var(--s-6) var(--s-5);
    & > header { grid-column: 1; grid-row: 1 / span 3; align-self: start; display: grid; position: sticky; inset-block-start: var(--s-4); gap: .15rem; justify-content: stretch }
    & > :not(header) { grid-column: 2 }
  }
}
li[role="separator"] { display: flex; align-items: center; gap: var(--s-3); padding-block: var(--s-3); color: var(--signal-ink); font: 600 var(--fs-meta)/1.3 var(--sans);
  &::before, &::after { content: ""; flex: 1; border-block-start: 1px solid var(--signal) }
  &::before { flex: 0 0 var(--s-5) }
}

/* Prose: a rendered Coloq_doc and the editor surface share these rules. */
:is(article > div, [contenteditable]) {
  & h2 { font-size: 1.5em; line-height: 1.15; margin-block-start: 1.3em }
  & h3 { font-size: 1.2em; line-height: 1.2; margin-block-start: 1.2em }
  & :is(ul, ol) { padding-inline-start: 1.5em }
  & ul { list-style: disc; & ul { list-style: circle } }
  & ol { list-style: decimal }
  & li { &::marker { color: var(--ink-3) } & + li { margin-block-start: .3em } & > :is(ul, ol) { margin-block-start: .3em } }
  & blockquote { position: relative; padding-inline-start: 1.7em; font-style: italic; color: var(--ink-2);
    &::before { content: "\00ab"; content: "\00ab" / ""; position: absolute; inset: -.12em auto auto 0; font: 700 1.5em/1.1 var(--serif); font-style: normal; color: var(--accent) }
  }
  & pre { padding: .9em 1.1em; background: var(--sunken); border-radius: var(--r-1); overflow-x: auto; font-size: .76em; line-height: 1.55; tab-size: 2; overflow-wrap: normal; white-space: pre; max-inline-size: 100% }
  & :not(pre) > code { padding: .08em .35em; background: var(--sunken); border-radius: 4px; font-size: .76em; overflow-wrap: anywhere }
  & hr { inline-size: 3rem; margin-block: 1.4em; border-color: var(--edge) }
  & a[target="_blank"]::after { content: ""; display: inline-block; inline-size: .5em; block-size: .5em; margin-inline-start: .22em; border: solid currentColor; border-width: .1em .1em 0 0; background: linear-gradient(to top right, transparent calc(50% - .05em), currentColor calc(50% - .05em) calc(50% + .05em), transparent calc(50% + .05em)); vertical-align: .32em }
  & figure { position: relative; inline-size: fit-content; max-inline-size: 100%;
    & :is(img, video) { border-radius: var(--r-1); background: var(--sunken) }
    &[data-kind="gif"] > button { display: block; margin-block-start: var(--s-1); min-block-size: 0; padding: .2rem .7rem; font-size: .8125rem }
    &[data-kind="gif"]::after { content: "GIF"; position: absolute; inset: var(--s-2) auto auto var(--s-2); padding: .1rem .45rem; border-radius: 4px; background: var(--ink); color: var(--paper); font: 700 .75rem/1.4 var(--sans); letter-spacing: .04em }
  }
  & figcaption { margin-block-start: var(--s-1); font: 500 var(--fs-meta)/1.4 var(--sans); color: var(--ink-3) }
}
article[data-state="deleted"] { background: repeating-linear-gradient(135deg, transparent 0 .5rem, color-mix(in srgb, var(--sunken) 55%, transparent) .5rem 1rem);
  & > div { font-style: italic; color: var(--ink-3); font-size: var(--fs-copy) }
  & > header :is(a, p:first-child) { color: var(--ink-3); font-weight: 500 }
}

/* Row list: ol|ul whose items are plain rows (attachments, login order, areas in admin). */
:where(main, dialog, form) :where(ul, ol):not(:has(> li > article), nav *, [contenteditable] *, article > div *) { display: grid;
  & > li { display: flex; flex-wrap: wrap; align-items: center; gap: var(--s-2) var(--s-3); padding-block: var(--s-2); border-block-end: 1px solid var(--rule); min-block-size: calc(var(--hit) + var(--s-2)) }
}
li > :is(span, p, strong):first-child:not(:only-child) { flex: 1 1 13rem; min-inline-size: 0 }
li > span:has(> strong) > small:not(:has(> svg)) { display: block }

/* Form field pattern: div > label + control + small (hint) + output (error). */
form { display: grid; gap: var(--s-5); max-inline-size: 40rem }
fieldset { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 15rem), 1fr)); gap: var(--s-5) var(--s-4); border: 0; padding: 0; min-inline-size: 0 }
legend { font: 700 var(--fs-h3)/1.2 var(--serif); padding: 0; margin-block-end: var(--s-3) }
:is(form, fieldset) > div { display: grid; gap: .35rem; align-content: start; min-inline-size: 0;
  & > small { font-weight: 400 }
  &:has(> input:is([type="checkbox"], [type="radio"])) { grid-template-columns: auto 1fr; align-items: center; column-gap: var(--s-3); min-block-size: var(--hit); & > small { grid-column: 2 } }
}
label { font-weight: 600; font-size: .9375rem }
output { font: 500 .875rem/1.5 var(--sans); color: var(--danger); &:empty { display: none } }
input:not([type="checkbox"], [type="radio"], [type="hidden"], [type="file"]), select, textarea { font: inherit; font-size: 1rem; color: var(--ink); inline-size: 100%; min-block-size: var(--hit); padding: .55rem .8rem; border: 1px solid var(--edge); border-radius: var(--r-1); background: var(--surface) }
textarea { min-block-size: 7rem; resize: vertical; line-height: 1.5 }
::placeholder { color: var(--ink-3); opacity: 1 }
:is(input, select, textarea):focus-visible { border-color: var(--focus); outline-offset: 0 }
[aria-invalid="true"] { border-color: var(--danger) !important; box-shadow: inset 0 0 0 1px var(--danger) }
:is(input, select, textarea, button)[readonly], :disabled { opacity: .6; cursor: not-allowed }
input[readonly] { background: var(--sunken) }
input[type="search"] { appearance: none }
input:is([type="checkbox"], [type="radio"]) { appearance: none; display: grid; place-content: center; flex: none; inline-size: 1.375rem; block-size: 1.375rem; margin: 0; border: 1.5px solid var(--edge); background: var(--surface); border-radius: 5px; cursor: pointer;
  &::before { content: ""; inline-size: .8rem; block-size: .8rem; background: var(--on-accent); transform: scale(0); clip-path: polygon(14% 52%, 0 66%, 38% 100%, 100% 22%, 85% 8%, 38% 70%) }
  &:checked { background: var(--accent); border-color: var(--accent); &::before { transform: scale(1) } }
}
input[type="radio"] { border-radius: 50%; &::before { clip-path: circle(35%) } }
input[role="switch"] { inline-size: 2.75rem; block-size: 1.5rem; border-radius: 99px; place-content: center start; padding-inline: 2px;
  &::before { inline-size: 1.1rem; block-size: 1.1rem; clip-path: none; border-radius: 50%; background: var(--edge); transform: none }
  &:checked::before { background: var(--on-accent); transform: translateX(1.2rem) }
}
/* Radio group: segmented control. */
fieldset:has(input[type="radio"]) { display: flex; flex-wrap: wrap; gap: 0;
  & legend { flex: 0 0 100%; font: 600 .9375rem/1.3 var(--sans); margin-block-end: var(--s-2) }
  & > div { display: block; position: relative; min-block-size: 0 }
  & input { position: absolute; opacity: 0; inset: 0; inline-size: 100%; block-size: 100% }
  & label { display: flex; align-items: center; min-block-size: var(--hit); padding-inline: var(--s-4); border: 1px solid var(--edge); background: var(--surface); font-weight: 500; cursor: pointer; margin-inline-start: -1px }
  & > div:first-of-type label { border-start-start-radius: var(--r-1); border-end-start-radius: var(--r-1); margin-inline-start: 0 }
  & > div:last-child label { border-start-end-radius: var(--r-1); border-end-end-radius: var(--r-1) }
  & > div:has(:checked) label { background: var(--accent-wash); border-color: var(--accent); color: var(--ink); font-weight: 700; position: relative; z-index: 1 }
  & > div:has(:focus-visible) label { outline: 2px solid var(--focus); outline-offset: 2px; z-index: 2 }
}
progress, meter { appearance: none; inline-size: 100%; block-size: .5rem; border: 0; border-radius: 99px; background: var(--rule); overflow: hidden; vertical-align: middle }
progress::-webkit-progress-bar { background: var(--rule) }
progress::-webkit-progress-value { background: var(--accent); border-radius: 99px }
progress::-moz-progress-bar { background: var(--accent); border-radius: 99px }
meter::-webkit-meter-bar { background: var(--rule); border: 0; block-size: .5rem }
meter::-webkit-meter-optimum-value { background: var(--ok) }
meter::-webkit-meter-suboptimum-value { background: var(--signal) }
meter::-webkit-meter-even-less-good-value { background: var(--danger) }
meter:-moz-meter-optimum::-moz-meter-bar { background: var(--ok) }
meter:-moz-meter-sub-optimum::-moz-meter-bar { background: var(--signal) }
meter:-moz-meter-sub-sub-optimum::-moz-meter-bar { background: var(--danger) }

/* Buttons: submit is primary, every other button is secondary; data-tone overrides. */
:is(button, a[data-tone]) { font: 600 1rem/1.15 var(--sans); display: inline-flex; align-items: center; justify-content: center; gap: var(--s-2); min-block-size: var(--hit); padding: .5rem 1.1rem; border: 1px solid var(--edge); border-radius: var(--r-1); background: var(--surface); color: var(--ink); cursor: pointer; text-decoration: none; text-align: center; touch-action: manipulation; &:hover { background: var(--sunken) } }
:is(button[type="submit"]:not([data-tone]), [data-tone="primary"]) { background: var(--accent); border-color: var(--accent); color: var(--on-accent); &:hover { background: var(--accent-press); border-color: var(--accent-press); color: var(--on-accent) } }
[data-tone="secondary"] { background: var(--surface); border-color: var(--edge); color: var(--ink); &:hover { background: var(--sunken); border-color: var(--edge); color: var(--ink) } }
:is(article > footer, [role="toolbar"], td, th, summary) :is(button, a[data-tone]):not([data-tone], [type="submit"]), :is(button, a)[data-tone="quiet"] { background: transparent; border-color: transparent; color: var(--ink-2); font-weight: 500; font-size: .9375rem; min-block-size: var(--hit-s); padding-inline: var(--s-3); &:hover { background: var(--sunken); color: var(--ink) } }
:is(button, a)[data-tone="danger"] { color: var(--danger); border-color: var(--danger); background: transparent; &:hover { background: var(--danger-wash); color: var(--danger) } }
:is(article > footer, td) [data-tone="danger"] { border-color: transparent }
button[aria-pressed="true"] { background: var(--accent-wash) !important; border-color: var(--accent) !important; color: var(--ink) !important }
button[aria-busy="true"] { opacity: .65; cursor: progress; pointer-events: none }
button[aria-disabled="true"], button:disabled { opacity: .5; cursor: not-allowed }
button:has(> svg:only-child) { min-inline-size: var(--hit-s); padding-inline: var(--s-2) }
button svg { inline-size: 1.2em; block-size: 1.2em }
form > ul > li > button { inline-size: 100%; justify-content: flex-start; gap: var(--s-3) }
form > ul:has(> li > button[type="submit"]) > li { padding: 0; border: 0; min-block-size: 0; margin-block-end: var(--s-2) }
form > button[type="submit"]:last-child, form > footer > button[type="submit"] { justify-self: start }

/* Toolbar, editor surface and composer. */
[role="toolbar"] { display: flex; align-items: center; gap: 2px; padding: var(--s-1); overflow-x: auto; scrollbar-width: thin; border-block-end: 1px solid var(--rule);
  & button { min-inline-size: var(--hit-s); padding-inline: var(--s-2); flex: none; font-size: .9375rem }
  & strong { font: 800 1.125rem var(--serif) }
  & em { font: italic 600 1.125rem var(--serif) }
  & s { font: 600 1.125rem var(--serif) }
  & code { font: inherit; font-family: var(--mono) }
  & hr { flex: none; inline-size: 1px; block-size: 1.4rem; margin: 0 var(--s-1); border: 0; background: var(--rule) }
}
[contenteditable] { position: relative; display: block; min-block-size: 3.5rem; max-block-size: 42dvh; overflow-y: auto; padding: var(--s-3) var(--s-4); background: var(--surface); font-size: var(--fs-prose); line-height: 1.55; --flow: .7em; overscroll-behavior: contain;
  &:focus-visible { outline-offset: -2px }
  &:is(:empty, :has(> p:only-child:empty))::before { content: attr(aria-placeholder); position: absolute; inset: var(--s-3) var(--s-4) auto; color: var(--ink-3); pointer-events: none }
  &[aria-invalid="true"] { box-shadow: inset 0 0 0 2px var(--danger) }
}
@container (min-width: 40rem) { [contenteditable] { min-block-size: 4.75rem } }
form:has(> [contenteditable]) { gap: 0; max-inline-size: none; border: 1px solid var(--edge); border-radius: var(--r-2); background: var(--surface); overflow: clip;
  & > footer { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--s-2) var(--s-4); padding: var(--s-2) var(--s-3); border-block-start: 1px solid var(--rule);
    & > div { display: flex; align-items: center; gap: var(--s-3); flex: 1 1 10rem; min-inline-size: 0 }
    & meter { flex: 0 1 7rem; min-inline-size: 2.5rem }
    & output { color: var(--ink-3); font-size: var(--fs-meta); &[role="alert"] { color: var(--danger) } }
  }
  & > ul[aria-label] { padding: var(--s-2) var(--s-3); border-block-start: 1px solid var(--rule); gap: var(--s-2);
    & > li { display: grid; grid-template: "thumb name act" auto "thumb bar act" auto ". alt alt" auto / 2.75rem minmax(0, 1fr) auto; gap: 0 var(--s-3); align-items: center; padding: var(--s-1) 0; border: 0; min-block-size: 0 }
    & label { grid-area: alt; display: grid; gap: .2rem; margin-block-start: var(--s-1); font: 500 var(--fs-meta)/1.4 var(--sans); color: var(--ink-3) }
    & img { grid-area: thumb; inline-size: 2.75rem; block-size: 2.75rem; object-fit: cover; border-radius: var(--r-1) }
    & p { grid-area: name; font: 400 .9375rem/1.5 var(--sans); min-inline-size: 0; overflow-wrap: anywhere }
    & progress, & output { grid-area: bar }
    & output { color: var(--ink-3); font-size: var(--fs-meta); &[role="alert"] { color: var(--danger) } }
    & button { grid-area: act }
  }
}
/* Reply composer: main > footer pinned to the bottom of the viewport. */
main > footer:has([contenteditable]) { position: sticky; inset-block-end: 0; z-index: 5; margin: var(--s-5) calc(-1 * var(--s-3)) 0; padding: var(--s-3); background: var(--paper); box-shadow: 0 -1px 0 var(--rule);
  & > form { max-inline-size: none }
  & > output { position: absolute; inset: auto 0 100% 0; display: flex; justify-content: center; padding-block-end: var(--s-3); pointer-events: none; & > button { pointer-events: auto; background: var(--ink); color: var(--paper); border-color: var(--ink); box-shadow: var(--shadow); border-radius: 99px } }
}
@container (max-width: 40rem) { main > footer:has([contenteditable]) { margin-inline: calc(-1 * var(--gutter)); padding-inline: var(--s-2) } }

/* Table pattern: div[role=region][tabindex=0] > table. */
div[role="region"] { overflow-x: auto; border: 1px solid var(--rule); border-radius: var(--r-1); background: var(--surface); max-inline-size: 100%; &:focus-visible { outline-offset: 2px } }
table { inline-size: 100%; border-collapse: collapse; font-size: .9375rem; font-variant-numeric: tabular-nums lining-nums;
  & caption { text-align: start; padding: var(--s-3) var(--s-4); font: 700 1.0625rem var(--serif); border-block-end: 1px solid var(--rule) }
  & :is(th, td) { padding: var(--s-2) var(--s-4); text-align: start; vertical-align: middle }
  & th { font-size: var(--fs-meta); font-weight: 600; color: var(--ink-2); background: var(--sunken); white-space: nowrap; border-block-end: 1px solid var(--edge) }
  & td { border-block-end: 1px solid var(--rule); min-inline-size: 7rem; &:has(> :is(button, form)) { white-space: nowrap } & > form { display: inline-flex; max-inline-size: none } & small { display: block } & time { white-space: nowrap } }
  & tbody th { background: none; font-size: inherit; color: var(--ink); border-block-end: 1px solid var(--rule); & small { display: block } }
  & tr:last-child > :is(td, th) { border-block-end: 0 }
  & th[aria-sort] button { padding: 0; min-block-size: var(--hit-s); background: none; font: inherit; color: inherit; gap: var(--s-2);
    &::after { content: ""; border: .3rem solid transparent; border-block-start-width: 0; border-block-end: .4rem solid currentColor; opacity: .4 }
  }
  & th[aria-sort="ascending"] button::after { opacity: 1 }
  & th[aria-sort="descending"] button::after { border-block-end-width: 0; border-block-start: .4rem solid currentColor; opacity: 1 }
}

/* Callouts: p[role=alert] reports a problem, p[role=status] gives information. */
:is(p, div)[role="alert"], :is(p, div)[role="status"] { display: flex; align-items: flex-start; gap: var(--s-3); padding: var(--s-3) var(--s-4); border: 1px solid var(--accent); border-inline-start-width: 4px; border-radius: var(--r-1); background: var(--accent-wash); color: var(--ink); font: 500 .9375rem/1.45 var(--sans); max-inline-size: 40rem;
  & > svg { margin-block-start: .1rem; color: var(--accent) }
}
:is(p, div)[role="alert"] { border-color: var(--danger); background: var(--danger-wash); & > svg { color: var(--danger) } }

/* Empty state: section[data-state=empty] replaces a list. */
section[data-state="empty"] { max-inline-size: 36rem; padding-block: var(--s-6); display: grid; justify-items: start; gap: var(--s-3);
  &::before { content: "\00ab"; content: "\00ab" / ""; font: 800 5rem/.7 var(--serif); color: var(--edge); margin-block-end: var(--s-2) }
  & > :is(p, h2, h3) { margin: 0 }
  & :is(h2, h3) { font-size: var(--fs-h3) }
}

/* Loading: a busy list with empty items. */
:is(ol, ul)[aria-busy="true"] { cursor: progress;
  & > li:empty { position: relative; overflow: hidden; block-size: 6.5rem; margin-block: var(--s-3); border: 0; border-radius: var(--r-1);
    background: linear-gradient(var(--sunken) 0 0) 0 .9rem / 38% 1.1rem no-repeat, linear-gradient(var(--sunken) 0 0) 0 2.9rem / 88% .85rem no-repeat, linear-gradient(var(--sunken) 0 0) 0 4.4rem / 64% .85rem no-repeat;
    &::after { content: ""; position: absolute; inset: 0; background: linear-gradient(90deg, transparent, var(--shimmer), transparent); transform: translateX(-100%) }
  }
}

/* Panel: details. */
details { border: 1px solid var(--rule); border-radius: var(--r-1); background: var(--surface);
  & + details { margin-block-start: var(--s-3) }
  & > :not(summary) { margin-inline: var(--s-4); &:last-child { margin-block-end: var(--s-4) } }
  & > :is(form, fieldset) { margin-block-start: var(--s-2); max-inline-size: none }
  &::details-content { padding-block-start: var(--s-1) }
}
summary { display: flex; align-items: center; justify-content: space-between; gap: var(--s-3); min-block-size: var(--hit); padding: var(--s-2) var(--s-4); font-weight: 600; cursor: pointer; list-style: none;
  &::-webkit-details-marker { display: none }
  &::after { content: ""; flex: none; inline-size: .5rem; block-size: .5rem; border: solid currentColor; border-width: 0 2px 2px 0; transform: rotate(45deg) translate(-2px, -2px) }
}
details[open] > summary::after { transform: rotate(225deg) translate(-2px, -2px) }

/* Dialog: modal sheet; a bottom sheet under 40rem. */
dialog { position: fixed; inset: 0; margin: auto; inline-size: min(32rem, 100% - 2rem); max-block-size: calc(100dvh - 2rem); padding: 0; overflow: auto; color: var(--ink); background: var(--surface); border: 1px solid var(--edge); border-radius: var(--r-2); box-shadow: var(--shadow);
  &:not(:modal) { box-shadow: var(--shadow), 0 0 0 100vmax var(--scrim) }
  &::backdrop { background: var(--scrim) }
  & h2 { font-size: var(--fs-h3) }
  & > form { padding: var(--s-5); max-inline-size: none; gap: var(--s-4);
    & > header { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--s-3); & button { margin: calc(-1 * var(--s-2)) calc(-1 * var(--s-2)) 0 0 } }
    & > footer { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: var(--s-2); & > button[type="submit"]:not([data-tone]) { order: 2 } }
  }
  @media (max-width: 40rem) { inset: auto 0 0 0; inline-size: 100%; max-inline-size: none; margin: 0; border-radius: var(--r-2) var(--r-2) 0 0; border-inline: 0; border-block-end: 0 }
}

/* Toast: an output directly under body. */
body > output { position: fixed; inset: var(--s-4) var(--s-4) auto; z-index: 60; margin-inline: auto; inline-size: fit-content; max-inline-size: min(32rem, 100% - 2rem); display: flex; align-items: center; gap: var(--s-3); padding: var(--s-3) var(--s-4); border-radius: var(--r-1); background: var(--ink); color: var(--paper); font: 500 .9375rem/1.4 var(--sans); box-shadow: var(--shadow);
  & button[type] { min-block-size: var(--hit-s); padding-block: 0; background: none; border-color: transparent; color: inherit; text-decoration: underline; &:hover { background: none; color: inherit } }
}
}

@layer state {
article[data-state="unread"] > header :where(h2, h3) a { font-weight: 800 }
article::before { content: ""; position: absolute; inset: var(--s-5) auto var(--s-5) 0; inline-size: 3px; border-radius: 3px; background: transparent }
article[data-state="unread"]::before { background: var(--signal) }
article[data-state="new"]::before { background: var(--accent) }
article[data-state="failed"]::before { background: var(--danger) }
article[data-state="committed"]::before { background: var(--ok) }
article[aria-busy="true"] { opacity: .62; & > header small { font-style: italic } }
article[data-state="failed"] > div { color: var(--ink-2) }
article[aria-current], article:target { background: color-mix(in srgb, var(--accent-wash) 70%, transparent) }
a[aria-current="page"]:not(nav a) { font-weight: 700 }
}

@layer motion {
:where(button, a[data-tone]) { transition: transform var(--dur-1) var(--ease); &:active:not(:disabled, [aria-busy="true"]) { transform: scale(.96) } }
input:is([type="checkbox"], [type="radio"])::before { transition: transform var(--dur-1) var(--ease) }
article { transition: opacity var(--dur-3) var(--ease) }
article[data-state="new"] { animation: arrive var(--dur-3) var(--ease) both; &::before { animation: grow var(--dur-3) var(--ease) both } }
article[data-state="committed"]::before { animation: fade var(--dur-3) var(--ease) forwards }
article[data-state="failed"] { animation: nudge var(--dur-3) var(--ease) }
@keyframes arrive { from { opacity: 0; transform: translateY(10px) } }
@keyframes grow { from { opacity: 0; transform: scaleY(.2) } }
@keyframes fade { to { opacity: 0 } }
@keyframes nudge { 25% { transform: translateX(-4px) } 60% { transform: translateX(3px) } }
:is(ol, ul)[aria-busy="true"] > li:empty::after { animation: shimmer 1.8s var(--ease) infinite }
@keyframes shimmer { to { transform: translateX(100%) } }

summary::after { transition: transform var(--dur-2) var(--ease) }
@supports selector(::details-content) {
  details::details-content { opacity: 0; transform: translateY(-4px); transition: opacity var(--dur-2) var(--ease), transform var(--dur-2) var(--ease), content-visibility var(--dur-2) allow-discrete }
  details[open]::details-content { opacity: 1; transform: none }
}
dialog, dialog::backdrop { opacity: 0; transition: opacity var(--dur-2) var(--ease), transform var(--dur-2) var(--ease), display var(--dur-2) allow-discrete, overlay var(--dur-2) allow-discrete }
dialog { transform: translateY(10px) scale(.98) }
dialog[open], dialog[open]::backdrop { opacity: 1 }
dialog[open] { transform: none }
@starting-style { dialog[open], dialog[open]::backdrop { opacity: 0 } dialog[open] { transform: translateY(10px) scale(.98) } }
@media (max-width: 40rem) { dialog { transform: translateY(100%) } @starting-style { dialog[open] { transform: translateY(100%) } } }

body > output { transition: opacity var(--dur-2) var(--ease), transform var(--dur-2) var(--ease), display var(--dur-2) allow-discrete; &[hidden] { display: none; opacity: 0; transform: translateY(-8px) } }
main > footer > output { transition: opacity var(--dur-2) var(--ease), transform var(--dur-2) var(--ease) }
@starting-style { body > output { opacity: 0; transform: translateY(-8px) } main > footer > output { opacity: 0; transform: translateY(8px) } }
}

@media (prefers-reduced-motion: reduce) {
*, ::before, ::after, ::backdrop { animation: none !important; transition-duration: 1ms !important; transition-delay: 0s !important; scroll-behavior: auto !important }
}
@media (forced-colors: active) {
article:is([data-state="unread"], [data-state="new"], [data-state="failed"], [data-state="committed"])::before { background: Highlight }
button { border-color: ButtonText }
[aria-invalid="true"] { border-color: Mark !important }
}

/* Editor. (1) The thumbnail of an attachment that is not an image (a video, a
   refused file) is an svg icon in the same grid area as an img. (2) Media in the
   surface is figure[contenteditable="false"] so the caret cannot enter it; it
   must not take the surface's own box rules, which select on the attribute name. */
@layer components {
form:has(> [contenteditable]) > ul[aria-label] > li > svg { grid-area: thumb; inline-size: 2.75rem; block-size: 2.75rem; padding: .7rem; border: 1px solid var(--rule); border-radius: var(--r-1); background: var(--sunken); color: var(--ink-3) }
[contenteditable] [contenteditable="false"] { min-block-size: 0; max-block-size: none; overflow: visible; padding: 0; background: none }
}
