/* The Story Machine: a wizard's vending machine in 16-bit pixels. Everything on a 4px grid, hard edges,
   a small fixed palette. Two pixel fonts, both under the SIL Open Font License and served from here:
   Jersey 15 for reading (its numbers are unmistakable: jersey-15-license.txt) and Pixelify Sans for
   headings, buttons and labels (pixelify-sans-license.txt), which borrows Jersey's digits and brackets. */
@font-face {
  font-family: 'Jersey 15';
  src: url('/jersey-15.woff2') format('woff2');
  font-display: swap;
}
@font-face {
  font-family: 'Jersey Digits';
  src: url('/jersey-15.woff2') format('woff2');
  unicode-range: U+0028-0029, U+0030-0039;
  font-display: swap;
}
@font-face {
  font-family: 'Pixelify Sans';
  src: url('/pixelify-sans.woff2') format('woff2');
  font-weight: 400 700;
  font-display: swap;
}

:root {
  --ink: #1b1030;
  --night: #241544;
  --night-2: #2f1c57;
  --teal: #1f8a8a;
  --teal-dark: #12525a;
  --teal-light: #3fc4b8;
  --gold: #ffcf3f;
  --gold-dark: #c9902a;
  --paper: #fff3d6;
  --paper-2: #f3dfae;
  --pink: #ff5fa2;
  --purple: #7a3fd1;
  --wood: #8a5a2b;
  --wood-dark: #5e3a18;
  --good: #5fe07a;
  --wait: #ffcf3f;
  --off: #54407e;
  --u: 4px;
  --read: 'Jersey 15', ui-monospace, 'Courier New', monospace;
  --show: 'Jersey Digits', 'Pixelify Sans', ui-monospace, 'Courier New', monospace;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { background: var(--ink); }
body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--read);
  font-size: 22px;
  line-height: 1.2;
  font-variant-ligatures: none;
  font-synthesis: none;
  color: var(--paper);
  background-color: var(--night);
  image-rendering: pixelated;
}
canvas.sprite { image-rendering: pixelated; display: block; }
h1, h2, h3, p { margin: 0; }
h1, h2, h3, label, strong, b, .lead, .btn, .brand, .leverlabel, .cookiebtn .label { font-family: var(--show); font-weight: 700; }
h1, h2, h3 { letter-spacing: 1px; }
code { font-family: inherit; }

/* a pixel frame: four hard shadows leave the corners notched */
.frame, .panel, .glass, .btn, input, textarea, select, .bubble, .token {
  box-shadow: 0 calc(-1 * var(--u)) 0 0 var(--ink), 0 var(--u) 0 0 var(--ink), calc(-1 * var(--u)) 0 0 0 var(--ink), var(--u) 0 0 0 var(--ink);
  border: 0;
  border-radius: 0;
}

.top { display: flex; align-items: center; gap: 16px; padding: 12px 20px; }
.brand { font-weight: 700; color: var(--pink); letter-spacing: 2px; margin-right: auto; }
.who { color: var(--paper-2); }

main { padding: 8px 20px 64px; max-width: 1120px; margin: 0 auto; }

/* ---- buttons and fields */
.btn {
  font: inherit; font-weight: 700; color: var(--ink); background: var(--gold); padding: 8px 16px; margin: var(--u); cursor: pointer;
  border-bottom: var(--u) solid var(--gold-dark);
}
.btn:hover { background: #ffe07a; }
.btn:active { transform: translateY(var(--u)); border-bottom-color: transparent; }
.btn.quiet { background: var(--paper-2); border-bottom-color: #cdb47a; }
.btn.quiet:hover { background: #fbeec4; }
.btn.small { font-size: 16px; padding: 2px 10px; }
.btn[disabled] { background: #b9ad8e; border-bottom-color: #8f8468; color: #5a5140; cursor: default; }
.btn.danger { background: var(--pink); border-bottom-color: #c23a78; }
:focus-visible { outline: var(--u) solid var(--pink); outline-offset: var(--u); }

label { display: block; font-weight: 700; margin: 12px 0 6px; }
input, textarea, select {
  font: inherit; font-family: var(--read); color: var(--ink); background: #fffaf0; padding: 6px 10px; margin: var(--u); width: calc(100% - 2 * var(--u));
}
textarea { resize: vertical; min-height: 96px; }
select { width: auto; max-width: 100%; cursor: pointer; }
::placeholder { color: #9a8c6a; opacity: 1; }
.row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 16px; }
.hint { color: #6b5a3a; font-size: 20px; }
.problem { color: #a3123f; margin-top: 12px; }
.lead { font-weight: 700; font-size: 20px; margin-bottom: 8px; }

/* ---- the door */
.door { display: flex; align-items: flex-end; justify-content: center; gap: 32px; padding-top: 48px; flex-wrap: wrap; }
.panel { background: var(--paper); color: var(--ink); padding: 24px; margin: var(--u); }
.doorform { width: min(380px, 100%); }
.doorform h1 { font-size: 32px; color: var(--purple); }
.doorform .btn { margin-top: 20px; }
.wizard { width: 180px; height: 220px; }
.door .wizard { width: 216px; height: 264px; }

/* ---- the stage: the wizard beside his machine */
.stage { display: grid; grid-template-columns: 240px minmax(0, 1fr); gap: 24px; align-items: end; }
.wiz { display: flex; flex-direction: column; align-items: center; gap: 20px; position: sticky; bottom: 16px; }
.wiz .wizard { animation: bob 1.2s steps(2) infinite; }
@keyframes bob { 50% { transform: translateY(var(--u)); } }
.bubble {
  position: relative; background: var(--paper); color: var(--ink); padding: 12px 14px; margin: var(--u); font-size: 22px; width: 100%;
  min-height: 72px;
}
.bubble::after {            /* the bubble's tail: two stacked blocks */
  content: ''; position: absolute; left: 50%; bottom: -12px; width: 12px; height: 8px; background: var(--paper);
  box-shadow: calc(-1 * var(--u)) 0 0 0 var(--ink), var(--u) 0 0 0 var(--ink), 0 var(--u) 0 0 var(--ink);
}

.machine { position: relative; }
.machine.shake { animation: shake 0.6s steps(6) 1; }
@keyframes shake { 20% { transform: translate(-4px, 0); } 40% { transform: translate(4px, 4px); } 60% { transform: translate(-4px, 4px); } 80% { transform: translate(4px, 0); } }
.marquee {
  display: flex; align-items: center; gap: 16px; background: var(--purple); padding: 12px 16px; margin: 0 24px;
  box-shadow: 0 calc(-1 * var(--u)) 0 0 var(--ink), calc(-1 * var(--u)) 0 0 0 var(--ink), var(--u) 0 0 0 var(--ink);
}
.marquee h1 { color: var(--gold); font-size: clamp(24px, 4vw, 40px); text-shadow: var(--u) var(--u) 0 var(--ink); white-space: nowrap; }
.bulbs { flex: 1; height: 12px; background: repeating-linear-gradient(90deg, var(--gold) 0 12px, transparent 12px 24px); animation: bulbs 0.8s steps(2) infinite; }
@keyframes bulbs { 50% { background-position: 12px 0; } }
.cabinet {
  display: grid; grid-template-columns: minmax(0, 1fr) 112px; gap: 16px; background: var(--teal); padding: 20px;
  box-shadow: 0 calc(-1 * var(--u)) 0 0 var(--ink), calc(-1 * var(--u)) 0 0 0 var(--ink), var(--u) 0 0 0 var(--ink),
    inset calc(-3 * var(--u)) 0 0 0 var(--teal-dark), inset var(--u) var(--u) 0 0 var(--teal-light);
}
.glass { background: var(--paper); color: var(--ink); padding: 20px; margin: var(--u); min-height: 420px; }
.pane.busy { display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 380px; }
.orbs { display: flex; gap: 12px; margin-top: 16px; }
.orbs i { width: 16px; height: 16px; background: var(--purple); animation: orb 0.9s steps(3) infinite; }
.orbs i:nth-child(2) { animation-delay: 0.3s; background: var(--teal); }
.orbs i:nth-child(3) { animation-delay: 0.6s; background: var(--pink); }
@keyframes orb { 50% { transform: translateY(-12px); } }

/* the readback */
.lines { margin: 8px 0 0; padding: 0 0 0 28px; }
.lines li { margin: 0 0 10px; padding-left: 4px; font-size: 24px; }
.lines li.not { color: #8a1238; }
.lines small { display: block; font-size: 19px; color: #6b5a3a; }
.count { margin: 16px var(--u) 8px; padding: 10px 12px; background: var(--ink); color: var(--paper); display: flex; flex-wrap: wrap; gap: 4px 24px; align-items: baseline; }
.count b { font-family: var(--read); font-weight: 400; font-size: 34px; color: var(--gold); margin-right: 8px; }
.notes { margin: 8px 0; padding: 0 0 0 22px; font-size: 20px; color: #4c3f25; }
.notes li { margin-bottom: 4px; }
.notes li.cannot { color: #8a1238; }
.rulewords { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 12px; }
.rulewords code { background: var(--paper-2); padding: 2px 8px; font-size: 20px; overflow-wrap: anywhere; }
.ruleedit { margin-top: 8px; }
.names { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 4px; }

/* the dials */
.condition { background: var(--paper-2); padding: 8px; margin: 0 var(--u) 12px; display: flex; flex-wrap: wrap; gap: 4px; align-items: center;
  box-shadow: 0 var(--u) 0 0 #cdb47a; }
.condition input { width: 210px; }
.condition input.num { width: 84px; }
.condition .x { margin-left: auto; }
.condition.bad { box-shadow: 0 0 0 var(--u) var(--pink); }

/* the side: the lever */
.side { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px; }
.lever { background: none; border: 0; padding: 8px; cursor: pointer; font: inherit; color: var(--paper); display: flex; flex-direction: column; align-items: center; gap: 12px; }
.lever .slot { display: block; position: relative; width: 24px; height: 176px; background: var(--ink); box-shadow: inset var(--u) 0 0 0 #000; }
.lever .knob { position: absolute; left: -12px; top: -8px; width: 48px; height: 48px; background: var(--pink);
  box-shadow: 0 calc(-1 * var(--u)) 0 0 var(--ink), 0 var(--u) 0 0 var(--ink), calc(-1 * var(--u)) 0 0 0 var(--ink), var(--u) 0 0 0 var(--ink),
    inset calc(-2 * var(--u)) calc(-2 * var(--u)) 0 0 #c23a78, inset var(--u) var(--u) 0 0 #ffa3c9;
  transition: top 0.25s steps(5); }
.lever:not([disabled]) .knob { animation: ready 1s steps(2) infinite; }
@keyframes ready { 50% { top: 0; } }
.lever.pulled .knob, .lever:not([disabled]):active .knob { top: 136px; animation: none; }
.lever[disabled] { cursor: default; }
.lever[disabled] .knob { background: #8f7fa8; box-shadow: 0 calc(-1 * var(--u)) 0 0 var(--ink), 0 var(--u) 0 0 var(--ink), calc(-1 * var(--u)) 0 0 0 var(--ink), var(--u) 0 0 0 var(--ink), inset calc(-2 * var(--u)) calc(-2 * var(--u)) 0 0 #6d5f85; }
.leverlabel { font-weight: 700; font-size: 20px; text-shadow: 2px 2px 0 var(--ink); }

/* the tray */
.tray {
  position: relative; height: 120px; background: var(--teal-dark); margin: 0; display: flex; align-items: center; justify-content: center;
  box-shadow: calc(-1 * var(--u)) 0 0 0 var(--ink), var(--u) 0 0 0 var(--ink), 0 var(--u) 0 0 var(--ink), inset 0 var(--u) 0 0 var(--ink);
}
.tray::before { content: ''; position: absolute; left: 20%; right: 20%; top: 16px; bottom: 16px; background: var(--ink); box-shadow: inset 0 calc(3 * var(--u)) 0 0 #000; }
.traylabel { position: absolute; left: 20px; bottom: 8px; font-size: 18px; color: var(--teal-light); }
.drop { position: relative; background: none; border: 0; padding: 0; cursor: pointer; animation: fall 0.7s steps(7) 1; }
.drop canvas { width: 80px; height: 80px; }
@keyframes fall { 0% { transform: translateY(-96px); } 70% { transform: translateY(8px); } 85% { transform: translateY(-8px); } 100% { transform: translateY(0); } }
.feet { display: flex; justify-content: space-between; padding: 0 48px; }
.feet i { width: 48px; height: 16px; background: var(--ink); }

/* ---- the shelf */
.shelf { margin-top: 56px; }
.shelf h2 { font-size: 28px; color: var(--gold); margin-bottom: 12px; }
.shelf .hint { color: var(--paper-2); }
.banner { background: var(--gold); color: var(--ink); padding: 8px 12px; margin: 0 var(--u) 16px; font-weight: 700; box-shadow: 0 var(--u) 0 0 var(--gold-dark); }
.cookies { list-style: none; margin: 0; padding: 24px 12px 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(152px, 1fr)); gap: 0 12px;
  background: repeating-linear-gradient(180deg, transparent 0 148px, var(--wood) 148px 160px, var(--wood-dark) 160px 168px, transparent 168px 216px); }
.cookies:empty { display: none; }
.cookies li { height: 216px; }
.cookiebtn { width: 100%; background: none; border: 0; padding: 0; font: inherit; color: var(--paper); cursor: pointer; display: flex; flex-direction: column; align-items: center; }
.cookiebtn canvas { width: 120px; height: 120px; margin-top: 24px; }
.cookiebtn:hover canvas, .cookiebtn:focus-visible canvas { transform: translateY(-8px); }
.cookiebtn .label { margin-top: 28px; font-size: 16px; font-weight: 700; text-align: center; line-height: 1.15; max-height: 2.3em; overflow: hidden; }
.cookiebtn .n { font-size: 19px; color: var(--teal-light); }
.cookiebtn.paused canvas { filter: grayscale(1) brightness(0.7); }

/* ---- a story's token */
.token { background: var(--paper); color: var(--ink); padding: 24px; width: min(680px, calc(100vw - 32px)); max-height: calc(100vh - 48px); overflow: auto; }
.token::backdrop { background: rgba(27, 16, 48, 0.82); }
.tokenhead { display: flex; gap: 20px; align-items: center; }
.bigcookie { width: 160px; height: 160px; flex: none; }
.token h2 { font-size: 30px; color: var(--purple); line-height: 1.1; overflow-wrap: anywhere; }
.token h3 { font-size: 20px; margin-bottom: 6px; }
.tagline { margin-top: 8px; overflow-wrap: anywhere; }
.leds { list-style: none; margin: 16px 0 0; padding: 0; display: grid; gap: 6px; }
.leds li { display: flex; gap: 10px; align-items: baseline; }
.led { flex: none; width: 16px; height: 16px; background: var(--off); box-shadow: 0 0 0 var(--u) var(--ink); margin: var(--u); transform: translateY(2px); }
.led.on { background: var(--good); }
.led.waiting { background: var(--wait); animation: blink 1s steps(2) infinite; }
.led.paused { background: #b9ad8e; }
@keyframes blink { 50% { background: var(--gold-dark); } }
.journey { margin-top: 16px; padding: 12px; background: var(--paper-2); }
.journey ol { margin: 6px 0; padding-left: 24px; }

/* ---- small screens */
@media (max-width: 860px) {
  .stage { grid-template-columns: 1fr; }
  .wiz { flex-direction: row-reverse; align-items: flex-end; position: static; }
  .wiz .wizard { width: 108px; height: 132px; flex: none; }
  .bubble::after { left: auto; right: -12px; bottom: 16px; width: 8px; height: 12px; box-shadow: 0 calc(-1 * var(--u)) 0 0 var(--ink), 0 var(--u) 0 0 var(--ink), var(--u) 0 0 0 var(--ink); }
  .cabinet { grid-template-columns: 1fr; padding: 12px; }
  .glass { padding: 14px; min-height: 0; }
  .side { flex-direction: row; }
  .lever { flex-direction: row; }
  .lever .slot { width: 176px; height: 24px; }
  .lever .knob { top: -12px; left: -8px; transition: left 0.25s steps(5); }
  .lever:not([disabled]) .knob { animation: none; }
  .lever.pulled .knob, .lever:not([disabled]):active .knob { top: -12px; left: 136px; }
  .names { grid-template-columns: 1fr; }
  .marquee { margin: 0 8px; }
  .tokenhead { flex-direction: column; align-items: flex-start; }
  .condition input { width: 100%; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
