@font-face {
  font-family: 'Fragment Mono';
  src: url('substrate/fonts/FragmentMono-Regular.ttf') format('truetype');
  font-weight: 400; font-style: normal; font-display: swap;
}

:root {
  --ink: #000;
  --paper: #fff;
  --panel: #ececeb;
  --hair: #000;
  /* One card wide, not two. */
  --sidebar: 150px;
  --pad: 26px;
  --small: 96px;
  --arrow: 96px;
  /* The strip above is fixed height, so the plates take whatever the window leaves, square, and
     the page never needs a scrollbar of its own — only the catalogue scrolls. The last term is
     the caption plus the cache strip under the right plate. */
  --strip: 150px;
  --strip-gap: 16px;          /* the clear space between the strip's frames and the plates */
  --big: max(220px, min(
    calc((100vw - var(--sidebar) - var(--arrow) - var(--pad) * 2 - 8px) / 2),
    calc(100vh - var(--strip) - var(--pad) * 2 - 76px)
  ));
}

/* Collapsed: the rail keeps only the caret, and --big grows into the reclaimed width. */
body.collapsed { --sidebar: 26px; }

* { box-sizing: border-box; }

html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--paper);
  color: var(--ink);
  font-family: 'Fragment Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px; line-height: 1.3;
  -webkit-font-smoothing: antialiased;
}

body { display: flex; align-items: stretch; }

/* ------------------------------------------------------------------ sidebar — the only scroller */

#sidebar {
  width: var(--sidebar); flex: 0 0 var(--sidebar);
  height: 100vh; overflow-y: auto; overflow-x: hidden; overscroll-behavior: contain;
  background: var(--panel);
  border-right: 1px solid var(--hair);
  padding: 10px 10px 32px;
  transition: width .18s ease, flex-basis .18s ease;
}
body.collapsed #sidebar { padding: 10px 2px 10px; overflow-y: hidden; }
body.collapsed #sidebar > :not(.bar) { display: none; }

.bar { display: flex; align-items: center; gap: 6px; margin-bottom: 10px; }

/* The caret. Points left to close, right to open. */
.caret {
  flex: 0 0 auto; width: 20px; height: 20px; padding: 0;
  display: grid; place-items: center;
  border: 1px solid var(--hair); background: transparent; cursor: pointer;
}
.caret svg { width: 12px; height: 12px; fill: none; stroke: var(--ink); stroke-width: 2; }
body.collapsed .caret svg { transform: rotate(180deg); }
.caret:hover { background: #fff; }

#sidebar h1 { margin: 0; font-size: 11px; font-weight: 400; letter-spacing: .12em; white-space: nowrap; }

.back { font-size: 9px; color: var(--ink); text-decoration: none; letter-spacing: .06em; display: block; margin-bottom: 9px; }
.back:hover { text-decoration: underline; }

.runbar { display: flex; align-items: center; gap: 6px; }

button {
  font: inherit; color: var(--ink); background: transparent;
  border: 1px solid var(--hair); border-radius: 0; cursor: pointer;
  padding: 2px 8px; letter-spacing: .06em;
}
button.solid { background: var(--ink); color: var(--paper); border-color: var(--ink); font-size: 13px; padding: 3px 10px; }
button.solid:hover { background: #2a2a2a; }
button:disabled { opacity: .35; cursor: default; }
button.solid:disabled:hover { background: var(--ink); }

.toggle { display: inline-flex; }
.toggle button { padding: 2px 7px; font-size: 11px; margin-left: -1px; }
.toggle button:first-child { margin-left: 0; }
.toggle button.on { background: var(--ink); color: var(--paper); }

.note { margin: 6px 0 11px; font-size: 9px; color: var(--ink); letter-spacing: .03em; }

/* One image wide. */
.grid { display: grid; grid-template-columns: 1fr; gap: 9px; }

.card { cursor: pointer; }
.card .shot {
  position: relative; aspect-ratio: 1; overflow: hidden;
  border: 1px solid var(--hair); background: #fff;
}
.card .shot img { width: 100%; height: 100%; object-fit: cover; display: block; }
.card .cap {
  margin-top: 2px; font-size: 8px; line-height: 1.22; color: var(--ink);
  word-break: break-word; letter-spacing: .01em;
}
.card .badge {
  position: absolute; top: 1px; left: 3px; font-size: 9px;
  color: #fff; text-shadow: 0 0 3px #000, 0 0 2px #000;
}
/* Selected: a white square with a white cross drawn across it, over the thumbnail. */
.card.sel .shot::after {
  content: ''; position: absolute; inset: 7%;
  border: 2px solid #fff;
  background:
    linear-gradient(to bottom right, transparent calc(50% - 1.2px), #fff calc(50% - 1.2px), #fff calc(50% + 1.2px), transparent calc(50% + 1.2px)),
    linear-gradient(to bottom left,  transparent calc(50% - 1.2px), #fff calc(50% - 1.2px), #fff calc(50% + 1.2px), transparent calc(50% + 1.2px));
}
.card:hover .shot { outline: 1px solid var(--hair); outline-offset: 1px; }

/* The upload slot, last in the list. Empty it is a dashed well; filled it behaves as any other
   card, with a small clear in the corner. */
.card.upload .shot { border-style: dashed; display: grid; place-items: center; }
.card.upload.filled .shot { border-style: solid; }
.card.upload .plus { width: 26%; stroke: var(--ink); stroke-width: 1.5; fill: none; stroke-linecap: round; stroke-linejoin: round; opacity: .55; }
.card.upload:hover .plus { opacity: 1; }
.card.upload.filled .plus { display: none; }
.card.upload .clear {
  position: absolute; top: 1px; right: 1px; z-index: 2;
  width: 13px; height: 13px; padding: 0; line-height: 1; font-size: 10px;
  border: none; background: rgba(255,255,255,.85); color: var(--ink); display: none;
}
.card.upload.filled .clear { display: block; }
.card.upload .clear:hover { background: var(--ink); color: var(--paper); }

/* ------------------------------------------------------------------ main — fixed, never scrolls */

main {
  flex: 1; height: 100vh; overflow: hidden;
  /* Symmetric, so the rows below can centre honestly. */
  padding: var(--pad) var(--pad) 0;
}

/* One grid for both rows, so P_BASE lands over the right-hand plate. Both rows carry the same
   column template and both centre, so they stay aligned with each other while the block as a
   whole re-centres when the catalogue collapses and main gets wider. */
.row {
  display: grid;
  grid-template-columns: var(--big) var(--arrow) var(--big);
  justify-content: center;
}

/* The strip's contents sit on its floor, so 01 / 02 / 03 and P_BASE share one baseline and the
   padding below is the gap over the plates. box-sizing keeps the row exactly --strip tall, which
   is what the --big calc assumes. */
.strip {
  height: var(--strip);
  align-items: end;
  padding-bottom: var(--strip-gap);
}

figure { margin: 0; }
figcaption { font-size: 11px; letter-spacing: .04em; margin-bottom: 5px; }
/* The label sits left, its download formats right, on the width of the plate above. */
figcaption.under {
  margin: 5px 0 0;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
}

.dl { display: flex; gap: 4px; }
.mini {
  font-size: 9px; padding: 1px 5px; letter-spacing: .06em; line-height: 1.4;
}
.mini:not(:disabled):hover { background: var(--ink); color: var(--paper); }

.frame { border: 1px solid var(--hair); background: #fff; position: relative; }
.frame.small { width: var(--small); height: var(--small); }
.frame.big { width: var(--big); height: var(--big); }
.frame canvas, .frame img { width: 100%; height: 100%; display: block; object-fit: contain; }
.frame.small canvas[title] { cursor: pointer; }

.state {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  font-size: 10px; letter-spacing: .1em; color: var(--ink); pointer-events: none;
}
.state[hidden] { display: none; }

/* # / C, in the output plate's bottom right corner. Backed in paper so it stays readable over
   whichever of the two fields is behind it. */
.toggle.mode { position: absolute; right: 6px; bottom: 6px; background: var(--paper); }
.toggle.mode button { font-size: 10px; padding: 1px 6px; }

/* 01 + 02 = 03 — bottom-aligned, so every frame in the strip sits on the same line. */
.eq { display: flex; align-items: flex-end; gap: 12px; }
/* Lift each operator to the vertical centre of the frame beside it. */
.eq .op { font-size: 15px; line-height: 1; padding-bottom: calc(var(--small) / 2 - 8px); }

/* X / Y / Z, to the right of 03. */
.xyz { width: 150px; display: flex; flex-direction: column; gap: 3px; padding-bottom: 2px; }
.xyz label {
  display: grid; grid-template-columns: 9px 1fr 20px; align-items: center; gap: 5px;
  font-size: 9px; letter-spacing: .04em;
}
.xyz output { text-align: right; font-variant-numeric: tabular-nums; }
.xyz[aria-disabled="true"] { opacity: .3; pointer-events: none; }

/* A hairline track with a square handle, to match the drawing rather than the OS. */
.xyz input[type="range"] {
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 9px; margin: 0; background: transparent; cursor: pointer;
}
.xyz input[type="range"]::-webkit-slider-runnable-track { height: 1px; background: var(--ink); }
.xyz input[type="range"]::-moz-range-track { height: 1px; background: var(--ink); }
.xyz input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 7px; height: 9px; margin-top: -4px; border: 1px solid var(--ink); background: var(--paper);
}
.xyz input[type="range"]::-moz-range-thumb {
  width: 5px; height: 7px; border: 1px solid var(--ink); border-radius: 0; background: var(--paper);
}

.pbase { display: flex; align-items: flex-end; gap: 22px; }
.aside-note p { margin: 0 0 16px; font-size: 12px; letter-spacing: .02em; }

/* margin-left:auto pins the note to the right edge of the column, which is the right plate's
   right edge — so it sits in that square's upper right corner. */
#note {
  margin-left: auto; align-self: flex-start;
  height: calc(var(--strip) - 28px); width: auto; max-width: 42%;
  object-fit: contain; object-position: top right;
  mix-blend-mode: multiply;
}

/* The arrow column, with RERUN where the [T1] tag used to sit. */
.arrow {
  width: var(--arrow); height: var(--big); display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 7px;
}
.arrow .line { width: 60px; height: 1px; background: var(--ink); position: relative; }
.arrow .line::after {
  content: ''; position: absolute; right: 0; top: -3.5px;
  border-left: 7px solid var(--ink);
  border-top: 3.5px solid transparent; border-bottom: 3.5px solid transparent;
}
#rerun { font-size: 10px; padding: 2px 6px; letter-spacing: .04em; }

/* ------------------------------------------------------------------ generation cache */

.cache { display: flex; gap: 5px; margin-top: 6px; height: 44px; }
.slot-mini {
  width: 44px; height: 44px; padding: 0; flex: 0 0 auto;
  border: 1px solid var(--hair); background: #fff; cursor: pointer;
  position: relative; overflow: hidden;
}
.slot-mini canvas { width: 100%; height: 100%; display: block; }
.slot-mini.empty { border-style: dotted; opacity: .4; cursor: default; }
.slot-mini.on { outline: 2px solid var(--ink); outline-offset: 1px; }
.slot-mini:not(.empty):hover { outline: 1px solid var(--ink); outline-offset: 1px; }

/* ------------------------------------------------------------------ toast */

#toast {
  position: fixed; left: 50%; bottom: 14px; transform: translateX(-50%);
  background: var(--ink); color: var(--paper); padding: 5px 12px;
  font-size: 10px; letter-spacing: .04em;
  opacity: 0; transition: opacity .18s; pointer-events: none;
}
#toast.visible { opacity: 1; }

@media (max-height: 760px) {
  :root { --small: 78px; --strip: 128px; --strip-gap: 12px; }
}
