/* Media Pager — black on paper, paper on black.
 *
 * The grid is the design. Everything sits in a cell with a hairline around it
 * and a tiny uppercase label above it, the way an index or a specimen sheet is
 * set: no shadows, no rounded corners, no colour. What colour there is comes
 * from the page you are looking at.
 *
 * One typeface at one weight. Agave has no Han (measured), so the stack names a
 * CJK face after it — Chinese renders in a deliberate fallback rather than in
 * whatever the browser reaches for first. Bold is not used at all: its cut ships
 * only 707 codepoints and no Cyrillic, so bold Ukrainian would change typeface
 * mid-sentence. Emphasis is size, case and rules instead.
 */

@font-face {
  font-family: "Agave";
  src: url("./vendor/agave-regular.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

:root {
  --paper: #f2f1ec;
  --ink:   #16161a;
  --rule:  #16161a;
  --dim:   #16161a80;
  --fade:  #16161a1a;

  --mono: "Agave", "Noto Sans Mono CJK SC", "Source Han Mono SC",
          "Microsoft YaHei Mono", "PingFang SC", ui-monospace,
          "DejaVu Sans Mono", monospace;

  --pad: 0.65rem;
  --hair: 1px;
}

:root[data-invert="on"] {
  --paper: #16161a;
  --ink:   #f2f1ec;
  --rule:  #f2f1ec;
  --dim:   #f2f1ec80;
  --fade:  #f2f1ec1a;
}

* { box-sizing: border-box; }

html, body { margin: 0; height: 100%; }

body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--mono);
  font-size: 14px;
  line-height: 1.45;
  font-variant-ligatures: none;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

/* ------------------------------------------------------------------ header */
/* A ruled bar of cells, like the head of a catalogue card. */

header {
  display: flex;
  align-items: stretch;
  border-bottom: var(--hair) solid var(--rule);
  flex: 0 0 auto;
}

header > * {
  display: flex;
  align-items: center;
  padding: var(--pad) 0.9rem;
  border-right: var(--hair) solid var(--rule);
  white-space: nowrap;
  flex: 0 0 auto;
}

/* The identity cell is the one that may lose: a long slug shortens rather than
 * shoving the buttons past the right edge. */
header > .meta { flex: 0 1 auto; min-width: 0; }
header > .spacer { flex: 1 1 auto; padding: 0; min-width: 0; }

header .brand {
  letter-spacing: 0.14em;
  text-transform: uppercase;
  text-decoration: none;
  color: inherit;
}

header .brand:hover { background: var(--ink); color: var(--paper); }

header .meta {
  color: var(--dim);
  overflow: hidden;
  text-overflow: ellipsis;
}

header .credit { gap: 0.4rem; color: var(--dim); font-size: 12px; }
header .credit a { color: var(--ink); }

header > :last-child { border-right: 0; }

/* --------------------------------------------------------------- controls */

button,
select,
input[type="text"],
textarea {
  font: inherit;
  color: inherit;
  background: transparent;
  border: var(--hair) solid var(--rule);
  border-radius: 0;
  padding: 0.35rem 0.6rem;
  appearance: none;
}

button { cursor: pointer; }

button:hover:not(:disabled),
select:hover {
  background: var(--ink);
  color: var(--paper);
}

button:disabled { color: var(--dim); cursor: default; }

button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: var(--hair) solid var(--ink);
  outline-offset: 2px;
}

/* Header buttons are cells, not boxes: the bar already draws their edges. The
 * padding stays — it belongs to the cell, and zeroing it here glued the
 * controls together and pushed the last one off the right edge. */
header > button,
header > select {
  border: 0;
  border-right: var(--hair) solid var(--rule);
  letter-spacing: 0.06em;
}

/* After the rule above, or the last cell draws an edge against nothing. */
header > :last-child { border-right: 0; }

input[type="text"]::placeholder,
textarea::placeholder { color: var(--dim); }

input[type="text"], textarea { min-width: 0; }
textarea { resize: vertical; }

a { color: inherit; text-underline-offset: 0.2em; }

/* ------------------------------------------------------------------ labels */

h1, h2 {
  font-size: 14px;
  font-weight: 400;
  margin: 0;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

/* ------------------------------------------------------------------ sheets */

.sheet {
  flex: 1 1 auto;
  overflow-y: auto;
}

.sheet > section {
  border-bottom: var(--hair) solid var(--rule);
  padding: 1.4rem 0.9rem 1.6rem;
}

.sheet p {
  margin: 0.6rem 0 1.1rem;
  color: var(--dim);
  max-width: 58ch;
}

.sheet h2 { margin-bottom: 0.2rem; }

.controls {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  flex-wrap: wrap;
}

.controls input[type="text"] { flex: 1 1 16rem; max-width: 34rem; }

/* ----------------------------------------------------------------- status */

.status {
  padding: 0.5rem 0.9rem;
  min-height: 1.2rem;
  color: var(--dim);
  font-size: 12px;
  white-space: pre-wrap;
}

/* A status with nothing to say takes no room. This is :empty rather than a
 * class the code toggles, because say() already has exactly one way to clear
 * the element — it sets textContent and appends nothing — and a second,
 * parallel signal in the DOM could disagree with the first. Note that the
 * markup for these has no whitespace inside the tags: a single newline in
 * there would be a text node, and :empty would never match. */
.status:empty { display: none; }

.status.err { color: var(--ink); }
.status.err::before { content: "! "; }
.status.work::before { content: "· "; }
.status b { font-weight: 400; }

/* ---------------------------------------------------------------- library */

.library {
  flex: 1 1 auto;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.library > .head {
  padding: 1.2rem 0.9rem 0.7rem;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  padding: 0 0.9rem 0.9rem;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  border: var(--hair) solid var(--rule);
  padding: 0.2rem 0.2rem 0.2rem 0.6rem;
  margin: 0 -1px -1px 0;
}

.chip button { border: 0; padding: 0 0.35rem; color: var(--dim); }
.chip button:hover { background: var(--ink); color: var(--paper); }

.library > .controls { padding: 0 0.9rem 1.1rem; }

.picker {
  border-top: var(--hair) solid var(--rule);
  border-bottom: var(--hair) solid var(--rule);
  padding: 0.9rem;
}

.results {
  list-style: none;
  margin: 0.8rem 0 0;
  padding: 0;
  max-height: 40vh;
  overflow-y: auto;
  border-top: var(--hair) solid var(--rule);
}

.results li {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.4rem 0;
  border-bottom: var(--hair) solid var(--fade);
  cursor: pointer;
}

.results li:hover { background: var(--ink); color: var(--paper); }
.results li.meta { color: var(--dim); cursor: default; }
.results li.meta:hover { background: transparent; color: var(--dim); }
.results .n { color: var(--dim); }
.results li:hover .n { color: var(--paper); }

/* The book list is an index, not a gallery: numbered rows, aligned columns,
 * the thumbnail small enough to be a mark rather than the point. */

.index {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
}

.index th {
  font-size: 10px;
  font-weight: 400;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--dim);
  text-align: left;
  padding: 0.4rem 0.9rem;
  border-top: var(--hair) solid var(--rule);
  border-bottom: var(--hair) solid var(--rule);
}

.index th.num,   .index td.num   { width: 4rem; color: var(--dim); }
.index th.ch,    .index td.ch    { width: 30%; color: var(--dim); }
.index th.thumb, .index td.thumb { width: 3.6rem; }

.index td {
  padding: 0.45rem 0.9rem;
  border-bottom: var(--hair) solid var(--fade);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  vertical-align: middle;
}

.index tbody tr { cursor: pointer; }
.index tbody tr:hover { background: var(--ink); color: var(--paper); }
.index tbody tr:hover td { color: inherit; }

.index img {
  display: block;
  width: 2.2rem;
  height: 2.2rem;
  object-fit: cover;
  border: var(--hair) solid var(--rule);
  filter: grayscale(1) contrast(1.1);
}

.index .noThumb {
  width: 2.2rem;
  height: 2.2rem;
  border: var(--hair) solid var(--rule);
  display: grid;
  place-items: center;
  font-size: 9px;
  color: var(--dim);
}

/* The demo table sits inside a sheet section, whose padding would otherwise
 * inset it from the rules that are supposed to run the full width. */
.sheet > section.demos { padding-left: 0; padding-right: 0; }
.sheet > section.demos h2 { padding: 0 0.9rem 0.8rem; }

/* ----------------------------------------------------------------- reader */

.reader {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: 1fr 24rem;
  min-height: 0;
}

.stage {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  border-right: var(--hair) solid var(--rule);
}

.viewer {
  flex: 1 1 auto;
  position: relative;
  display: grid;
  place-items: center;
  overflow: auto;
  padding: 1.2rem;
  min-height: 0;
}

.viewer.busy { opacity: 0.35; }

.viewer canvas {
  border: var(--hair) solid var(--rule);
  max-width: 100%;
  display: block;
}

.flip { position: absolute; top: 0; bottom: 0; width: 18%; cursor: pointer; }
.flip.prev { left: 0; }
.flip.next { right: 0; }
.flip:hover { background: var(--fade); }

.stage .controls {
  border-top: var(--hair) solid var(--rule);
  padding: 0;
  gap: 0;
  flex-wrap: nowrap;
}

.stage .controls > * {
  padding: var(--pad) 0.9rem;
  border-right: var(--hair) solid var(--rule);
}

.stage .controls button { border: 0; }
.stage .controls > :last-child { border-right: 0; }

.pageno {
  border: 0 !important;
  width: 4.5rem;
  text-align: center;
}

.stage .meta { color: var(--dim); font-size: 11px; }
.stage .meta.spacer { flex: 1 1 auto; text-align: right; }

aside {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow-y: auto;
}

aside h2 {
  padding: var(--pad) 0.9rem;
  border-bottom: var(--hair) solid var(--rule);
  white-space: normal;
  overflow-wrap: anywhere;
  text-transform: none;
  letter-spacing: 0.02em;
}

aside h2.section {
  text-transform: uppercase;
  letter-spacing: 0.16em;
  font-size: 10px;
  color: var(--dim);
  border-top: var(--hair) solid var(--rule);
}

aside .meta {
  display: block;
  padding: 0.35rem 0.9rem;
  color: var(--dim);
  font-size: 11px;
  overflow-wrap: anywhere;
}

aside input[type="text"],
aside textarea {
  margin: 0 0.9rem 0.5rem;
  border-left: 0;
  border-right: 0;
  border-top: 0;
  padding-left: 0;
  padding-right: 0;
}

aside textarea { min-height: 7rem; }
aside .controls { padding: 0 0.9rem 0.9rem; }

.pagetext {
  margin: 0;
  padding: 0.6rem 0.9rem 1.2rem;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-size: 12px;
  max-height: 22rem;
  overflow-y: auto;
  user-select: text;
}

.pagetext.empty { color: var(--dim); }

.list { list-style: none; margin: 0; padding: 0 0 1.5rem; }

.list li {
  padding: 0.35rem 0.9rem;
  border-bottom: var(--hair) solid var(--fade);
  font-size: 12px;
}

.list li.meta { color: var(--dim); border: 0; }
.list .tc { color: var(--dim); cursor: pointer; }
.list .tt {
  display: block;
  color: var(--dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ------------------------------------------------------------ narrow view */

@media (max-width: 820px) {
  .reader { grid-template-columns: 1fr; }
  .stage { border-right: 0; border-bottom: var(--hair) solid var(--rule); }
  .stage .meta.spacer { display: none; }
  .index th.ch, .index td.ch { display: none; }
  header { flex-wrap: wrap; }
  header .credit { display: none; }
}
