/* Lanternwright — "occult bureaucracy in a dark control room".
   The board is a black-japanned instrument panel; everything around it is the Order's
   paperwork: requisition slips, work orders, punch cards, ledgers and rubber stamps. */

/* ---------------------------------------------------------------- fonts */
@font-face { font-family: 'Grenze Gotisch'; font-weight: 500; src: url(../assets/fonts/grenze-gotisch-latin-500-normal.woff2) format('woff2'); font-display: swap; }
@font-face { font-family: 'Grenze Gotisch'; font-weight: 700; src: url(../assets/fonts/grenze-gotisch-latin-700-normal.woff2) format('woff2'); font-display: swap; }
@font-face { font-family: 'Grenze Gotisch'; font-weight: 900; src: url(../assets/fonts/grenze-gotisch-latin-900-normal.woff2) format('woff2'); font-display: swap; }
@font-face { font-family: 'Special Elite'; font-weight: 400; src: url(../assets/fonts/special-elite-latin-400-normal.woff2) format('woff2'); font-display: swap; }
@font-face { font-family: 'Courier Prime'; font-weight: 400; src: url(../assets/fonts/courier-prime-latin-400-normal.woff2) format('woff2'); font-display: swap; }
@font-face { font-family: 'Courier Prime'; font-weight: 700; src: url(../assets/fonts/courier-prime-latin-700-normal.woff2) format('woff2'); font-display: swap; }
@font-face { font-family: 'Courier Prime'; font-style: italic; font-weight: 400; src: url(../assets/fonts/courier-prime-latin-400-italic.woff2) format('woff2'); font-display: swap; }
@font-face { font-family: 'IM Fell English'; font-weight: 400; src: url(../assets/fonts/im-fell-english-latin-400-normal.woff2) format('woff2'); font-display: swap; }
@font-face { font-family: 'IM Fell English'; font-style: italic; font-weight: 400; src: url(../assets/fonts/im-fell-english-latin-400-italic.woff2) format('woff2'); font-display: swap; }
@font-face { font-family: 'Big Shoulders Stencil'; font-weight: 700; src: url(../assets/fonts/big-shoulders-stencil-display-latin-700-normal.woff2) format('woff2'); font-display: swap; }
@font-face { font-family: 'Big Shoulders Stencil'; font-weight: 900; src: url(../assets/fonts/big-shoulders-stencil-display-latin-900-normal.woff2) format('woff2'); font-display: swap; }

/* ---------------------------------------------------------------- tokens */
:root {
  --night: #0b0807;
  --paper: #e7dcc2;
  --paper-2: #ddd0b1;
  --paper-edge: #b8a47a;
  --ink: #261c14;
  --ink-soft: #5c4a39;
  --ink-faint: #8f7c63;
  --red-ink: #a8231a;
  --blue-ink: #24346a;
  --pencil: #6e6a62;
  --gilt: #c8a35a;
  --gilt-hi: #ecd59a;
  --mercury: #b18cff;
  --lantern: #ffb347;
  --cinnabar: #d9402c;
  --gothic: 'Grenze Gotisch', 'UnifrakturMaguntia', serif;
  --type: 'Special Elite', 'Courier Prime', monospace;
  --mono: 'Courier Prime', 'Courier New', monospace;
  --print: 'IM Fell English', 'Iowan Old Style', Georgia, serif;
  --stencil: 'Big Shoulders Stencil', Impact, sans-serif;
  --top: 70px;
  --tray: 214px;
  --brief: 286px;
  --bottom: 150px;
  /* Paper grain: fractal noise baked into an SVG, tinted by multiply. */
  --grain: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='220' height='220'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='3' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 .35  0 0 0 0 .28  0 0 0 0 .18  0 0 0 .55 0'/></filter><rect width='220' height='220' filter='url(%23n)'/></svg>");
  /* Rubber-stamp ink: mostly solid, with the speckled gaps of a worn stamp. */
  --ink-mask: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='180' height='180'><filter id='m'><feTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='2' seed='3'/><feColorMatrix values='0 0 0 0 0  0 0 0 0 0  0 0 0 0 0  -3 0 0 0 2.25'/></filter><rect width='180' height='180' filter='url(%23m)'/></svg>");
  --fibres: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='400' height='400'><filter id='f'><feTurbulence type='turbulence' baseFrequency='.012 .09' numOctaves='2' seed='7'/><feColorMatrix values='0 0 0 0 .4  0 0 0 0 .3  0 0 0 0 .15  0 0 0 .18 0'/></filter><rect width='400' height='400' filter='url(%23f)'/></svg>");
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--night); color: var(--paper); font-family: var(--mono); }
body { -webkit-font-smoothing: antialiased; user-select: none; }
button { font: inherit; color: inherit; cursor: pointer; background: none; border: 0; padding: 0; }
.hidden { display: none !important; }
.spacer { flex: 1; }

#board { position: fixed; inset: 0; width: 100vw; height: 100vh; display: block; touch-action: none; transition: filter .6s; }
body.menu #board { filter: blur(6px) brightness(0.42) sepia(0.25); }

/* ---------------------------------------------------------------- paper */
.paper {
  position: relative;
  color: var(--ink);
  background-color: var(--paper);
  background-image:
    radial-gradient(ellipse at 18% 12%, rgba(255, 250, 235, 0.55), transparent 55%),
    radial-gradient(ellipse at 85% 95%, rgba(120, 85, 40, 0.18), transparent 60%),
    var(--fibres), var(--grain);
  background-blend-mode: normal, multiply, multiply, multiply;
  box-shadow: 0 1px 0 rgba(255,255,255,0.4) inset, 0 14px 28px rgba(0,0,0,0.55), 0 2px 4px rgba(0,0,0,0.5);
}
.form-head {
  display: flex; justify-content: space-between; align-items: baseline;
  border-bottom: 2px solid var(--ink); padding-bottom: 4px; margin-bottom: 2px;
  font-family: var(--gothic); font-weight: 700; font-size: 21px; line-height: 1;
}
.form-head + * { border-top: 1px solid var(--ink); padding-top: 8px; margin-top: 0; }
.form-no { font-family: var(--mono); font-size: 10px; font-weight: 400; color: var(--ink-soft); letter-spacing: 0.04em; white-space: nowrap; margin-left: 8px; }

/* Typewriter key: chrome ring, cream cap. */
.key {
  width: 40px; height: 40px; border-radius: 50%;
  display: grid; place-items: center;
  font-family: var(--mono); font-weight: 700; font-size: 21px; line-height: 1; color: var(--ink);
  background: radial-gradient(circle at 50% 38%, #f6efdc 0 52%, #d8ccad 64%, #3a332b 66% 70%, #a7a39b 72%, #5d5850 86%, #26221d 100%);
  box-shadow: 0 3px 0 #120e0b, 0 6px 10px rgba(0,0,0,0.6);
  transition: transform .06s, box-shadow .06s;
}
.key:hover { filter: brightness(1.06); }
.key:active { transform: translateY(3px); box-shadow: 0 0 0 #120e0b, 0 2px 4px rgba(0,0,0,0.6); }
.key.off { color: var(--red-ink); text-decoration: line-through; }
.keys { display: flex; gap: 10px; }

/* Luggage-tag button. */
.tag-btn {
  position: relative; padding: 7px 16px 7px 24px; color: var(--ink);
  font-family: var(--type); font-size: 14px;
  background: var(--paper-2) var(--grain);
  clip-path: polygon(10px 0, 100% 0, 100% 100%, 10px 100%, 0 50%);
  box-shadow: 0 2px 0 rgba(0,0,0,0.3);
}
.tag-btn::before { content: ''; position: absolute; left: 9px; top: 50%; width: 6px; height: 6px; margin-top: -3px; border-radius: 50%; background: var(--night); }
.tag-btn:hover { background-color: #efe5cc; }
.tag-btn.primary { background-color: var(--ink); color: var(--paper); }
.tag-btn.primary::before { background: var(--paper-2); }
.tag-btn.primary:hover { background-color: #3a2b1f; }

/* ---------------------------------------------------------------- HUD layout */
#hud { position: fixed; inset: 0; pointer-events: none; }
#hud > * { pointer-events: auto; }

#topbar {
  position: absolute; top: 0; left: 0; right: 0; height: var(--top);
  display: flex; align-items: center; gap: 14px; padding: 0 18px;
}
.folder-tab {
  position: relative; padding: 8px 30px 9px 18px; color: var(--ink);
  background-color: #c9a66b;
  background-image: linear-gradient(180deg, rgba(255,240,200,0.35), rgba(0,0,0,0.06)), var(--grain);
  background-blend-mode: normal, multiply;
  clip-path: polygon(0 0, calc(100% - 18px) 0, 100% 100%, 0 100%);
  box-shadow: 0 6px 18px rgba(0,0,0,0.5);
}
#level-chapter { font-family: var(--mono); font-size: 11px; color: #4a3720; letter-spacing: 0.02em; }
#level-title { font-family: var(--gothic); font-weight: 700; font-size: 27px; line-height: 1; margin-top: 2px; }

#tray {
  position: absolute; top: calc(var(--top) + 6px); left: 18px; width: calc(var(--tray) - 28px);
  padding: 14px 14px 12px; transform: rotate(-0.6deg);
  max-height: calc(100vh - var(--top) - var(--bottom) - 16px); overflow-y: auto; scrollbar-width: thin;
}
#tools { display: flex; flex-direction: column; }
.tool {
  display: grid; grid-template-columns: 34px 1fr auto; align-items: center; gap: 8px;
  padding: 5px 4px; text-align: left; border-bottom: 1px dotted var(--ink-faint);
  position: relative;
}
.tool:last-child { border-bottom: 0; }
.tool:hover { background: rgba(120, 85, 40, 0.08); }
.tool.active::after {
  /* Red pencil ring around the chosen line. */
  content: ''; position: absolute; inset: 1px -3px; border: 2px solid var(--red-ink); border-radius: 46% 54% 50% 48% / 60% 45% 55% 40%;
  transform: rotate(-1deg); opacity: 0.85; pointer-events: none;
}
.tool.empty { opacity: 0.45; }
.tool.empty .tool-name { text-decoration: line-through; text-decoration-color: var(--red-ink); }
.tool canvas { width: 34px; height: 34px; background: #17110d; border-radius: 4px; }
.tool-name { font-family: var(--type); font-size: 14px; color: var(--ink); }
.tool-key { font-family: var(--mono); font-size: 10px; color: var(--ink-faint); }
.tool-count { font-family: var(--stencil); font-weight: 700; font-size: 19px; color: var(--ink); }
.tool-count.zero { color: var(--red-ink); }
.tray-help { margin-top: 10px; padding-top: 8px; border-top: 1px solid var(--ink); font-size: 11px; color: var(--ink-soft); line-height: 1.75; }
.tray-help b { font-weight: 700; color: var(--ink); }

#brief-panel {
  position: absolute; top: calc(var(--top) + 10px); right: 20px; width: calc(var(--brief) - 30px);
  padding: 24px 18px 16px; transform: rotate(0.7deg);
  max-height: calc(100vh - var(--top) - var(--bottom) - 24px); overflow-y: auto; scrollbar-width: none;
}
.clip {
  position: absolute; top: -20px; right: 22px; width: 16px; height: 40px;
  border: 3px solid #9a9a98; border-radius: 9px; border-bottom-color: #6c6c6a;
  box-shadow: 1px 1px 2px rgba(0,0,0,0.5);
}
#docket { display: grid; grid-template-columns: 62px 1fr; gap: 3px 8px; margin: 0 0 10px; padding: 0 0 9px; border-bottom: 1px dashed var(--ink-faint); font-size: 12px; line-height: 1.35; }
#docket dt { font-family: var(--mono); font-weight: 700; color: var(--ink-soft); font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.04em; padding-top: 1px; }
#docket dd { margin: 0; font-family: var(--type); font-size: 13.5px; color: var(--ink); }
#docket dd.ink-pen { font-family: var(--print); font-style: italic; font-size: 17px; color: #1d1a24; }
#docket dd.ink-green { font-family: var(--print); font-style: italic; font-size: 17px; color: #2f6b3a; }
#docket dd.ink-pencil { font-family: var(--print); font-style: italic; font-size: 17px; color: var(--pencil); }
.obj-label { font-family: var(--mono); font-weight: 700; font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--red-ink); margin-bottom: 3px; }
#brief { font-family: var(--type); font-size: 16px; line-height: 1.42; color: var(--ink); }
#hint {
  margin-top: 12px; padding: 4px 2px 0;
  font-family: var(--print); font-style: italic; font-size: 18px; line-height: 1.3; color: var(--blue-ink);
  transform: rotate(-1.2deg);
}
#hint p { margin: 0 0 6px; }
#hint p::before { content: '— '; color: var(--ink-faint); }
#stats { margin-top: 12px; font-size: 12px; color: var(--ink-soft); line-height: 1.9; border-top: 1px solid var(--ink); padding-top: 8px; }
#stats .row { display: flex; gap: 6px; align-items: baseline; }
#stats .row .fill { flex: 1; border-bottom: 1px dotted var(--ink-faint); transform: translateY(-3px); }
#stats b { font-family: var(--stencil); font-weight: 700; font-size: 17px; color: var(--ink); }
#stats .par-ok { color: #2f6b34; }

/* ---------------------------------------------------------------- the punch card */
#tests-bar {
  position: absolute; left: 18px; right: 18px; bottom: 14px; height: calc(var(--bottom) - 24px);
  display: flex; align-items: stretch; gap: 18px;
}
#punchcard {
  flex: 1; display: flex; gap: 14px; padding: 10px 16px 10px 18px; color: var(--ink);
  background-color: #e9d9a8;
  background-image: radial-gradient(ellipse at 30% 0%, rgba(255,250,225,0.6), transparent 60%), var(--fibres), var(--grain);
  background-blend-mode: normal, multiply, multiply;
  clip-path: polygon(22px 0, 100% 0, 100% 100%, 0 100%, 0 22px);
  box-shadow: 0 14px 28px rgba(0,0,0,0.55);
  min-width: 0;
}
#tests-label { width: 92px; display: flex; flex-direction: column; justify-content: center; gap: 4px; border-right: 1px solid rgba(38,28,20,0.35); padding-right: 10px; }
.card-title { font-family: var(--gothic); font-weight: 700; font-size: 22px; line-height: 1; }
#tests-summary { font-family: var(--stencil); font-weight: 900; font-size: 32px; line-height: 1; color: var(--ink); }
#tests-summary small { display: block; font-family: var(--mono); font-weight: 400; font-size: 10px; color: var(--ink-soft); }
.card-legend { font-size: 10px; color: var(--ink-soft); display: flex; align-items: center; gap: 4px; }
.card-legend .hole { width: 7px; height: 12px; border-radius: 2px; background: #1a120d; display: inline-block; }
#tests { flex: 1; display: flex; gap: 2px; overflow-x: auto; align-items: stretch; scrollbar-width: thin; }
.test {
  position: relative; min-width: 66px; padding: 4px 8px 6px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 5px;
  border-left: 1px dashed rgba(38,28,20,0.22);
}
.test:first-child { border-left: 0; }
.test.selected::after {
  content: ''; position: absolute; inset: -2px 0; border: 2px solid var(--red-ink); border-radius: 5px;
  transform: rotate(-0.8deg); pointer-events: none; opacity: 0.85;
}
.test.running { background: rgba(177, 140, 255, 0.18); }
.test-num { width: 100%; display: flex; justify-content: space-between; align-items: baseline; font-family: var(--stencil); font-weight: 700; font-size: 16px; color: var(--ink-soft); }
.test-num .mark { font-family: var(--print); font-size: 21px; line-height: 0.8; }
.test.pass .mark { color: #2f6b34; }
.test.fail .mark { color: var(--red-ink); }
.test-row { display: flex; gap: 3px; align-items: center; }
.test-row .arrow { font-size: 10px; color: var(--ink-faint); margin: 0 1px; }
/* Each value is a printed digit; a 1 is punched clean through. */
.pip {
  width: 15px; height: 21px; border-radius: 3px; display: grid; place-items: center;
  font-family: var(--mono); font-weight: 700; font-size: 11px; color: var(--ink-soft);
}
.pip.on { background: #140e0a; color: transparent; box-shadow: inset 0 2px 3px rgba(0,0,0,0.9), 0 1px 0 rgba(255,255,255,0.5); }
.pip.src.on { background: radial-gradient(circle at 50% 50%, rgba(177,140,255,0.55), #140e0a 70%); }
.pip.lamp { color: #8a5a17; }
.pip.lamp.on { background: radial-gradient(circle at 50% 50%, rgba(255,179,71,0.6), #140e0a 70%); }
.pip.lamp.wrong { outline: 2px solid var(--red-ink); outline-offset: 1px; border-radius: 50%; }

/* Engage: a heavy rubber stamp in cinnabar ink. */
#btn-engage {
  width: 170px; position: relative; display: grid; place-items: center;
  background: radial-gradient(ellipse at 50% 30%, #3b2a1e, #1c130d 70%);
  border-radius: 10px; box-shadow: 0 10px 24px rgba(0,0,0,0.6), inset 0 1px 0 rgba(255,230,190,0.15);
  transition: transform .08s;
}
#btn-engage span {
  font-family: var(--stencil); font-weight: 900; font-size: 34px; letter-spacing: 0.04em; text-transform: uppercase;
  color: var(--cinnabar); padding: 2px 14px; border: 3px solid var(--cinnabar); border-radius: 4px;
  transform: rotate(-3deg); opacity: 0.9;
  -webkit-mask: var(--ink-mask); mask: var(--ink-mask);
}
#btn-engage:hover span { opacity: 1; }
#btn-engage:active { transform: translateY(3px) scale(0.98); }
#btn-engage.ready span { color: #ff5a3c; border-color: #ff5a3c; animation: ink-ready 1.4s ease-in-out infinite; }
#btn-engage:disabled { opacity: 0.55; pointer-events: none; }
@keyframes ink-ready { 50% { text-shadow: 0 0 18px rgba(255, 90, 60, 0.55); } }

#tooltip {
  position: fixed; z-index: 30; max-width: 250px; padding: 9px 12px 10px; color: var(--ink);
  background: var(--paper) var(--grain); font-size: 12px; line-height: 1.45;
  box-shadow: 0 8px 20px rgba(0,0,0,0.6); pointer-events: none; transform: rotate(-0.8deg);
}
#tooltip b { display: block; font-family: var(--gothic); font-weight: 700; font-size: 19px; line-height: 1.1; margin-bottom: 2px; }

/* ---------------------------------------------------------------- screens */
.screen { position: fixed; inset: 0; z-index: 10; overflow-y: auto; }

#title-screen { display: grid; place-items: center; }
.title-wrap { text-align: center; display: flex; flex-direction: column; align-items: center; padding: 30px; }
.title-seal { width: 104px; height: 104px; margin-bottom: 2px; filter: drop-shadow(0 6px 10px rgba(0,0,0,0.7)) drop-shadow(0 0 40px rgba(255,179,71,0.15)); }
@keyframes turn { to { transform: rotate(360deg); } }
.title-main {
  margin: 0; font-family: var(--gothic); font-weight: 900; font-size: clamp(64px, 11vw, 150px); line-height: 0.9;
  color: #efe2c2; text-shadow: 0 0 40px rgba(255, 179, 71, 0.28), 0 3px 0 #000;
}
.title-sub { max-width: 680px; font-family: var(--print); font-style: italic; font-size: 21px; line-height: 1.3; color: #cbbd9e; margin: 12px 0 30px; }
.contents { width: min(520px, 90vw); padding: 18px 26px 16px; transform: rotate(-0.5deg); }
.contents-head { font-family: var(--gothic); font-weight: 700; font-size: 24px; text-align: center; border-bottom: 1px solid var(--ink); padding-bottom: 6px; margin-bottom: 6px; }
.toc { width: 100%; display: flex; align-items: baseline; gap: 8px; padding: 7px 4px; font-family: var(--print); font-size: 21px; color: var(--ink); text-align: left; }
.toc:hover .toc-t { color: var(--red-ink); }
.toc:hover .toc-n::before { content: '☞ '; color: var(--red-ink); font-size: 16px; }
.toc-n { min-width: 36px; font-family: var(--print); color: var(--ink-soft); }
.toc-dots { flex: 1; border-bottom: 2px dotted var(--ink-faint); transform: translateY(-5px); }
.toc-p { font-family: var(--mono); font-size: 13px; color: var(--ink-soft); }
.title-foot { margin-top: 26px; font-family: var(--mono); font-size: 12px; color: #8a7a60; }

/* The ledger */
#select-screen { padding: 30px 20px; }
.ledger {
  max-width: 980px; margin: 0 auto; padding: 22px 36px 34px 64px;
  background-image:
    linear-gradient(90deg, transparent 46px, rgba(168, 35, 26, 0.5) 46px 48px, transparent 48px),
    repeating-linear-gradient(180deg, transparent 0 33px, rgba(36, 52, 106, 0.16) 33px 34px),
    var(--fibres), var(--grain);
  background-blend-mode: normal, normal, multiply, multiply;
}
.ledger-head { display: flex; align-items: center; gap: 16px; margin-bottom: 8px; }
.ledger-head h2 { margin: 0; font-family: var(--gothic); font-weight: 900; font-size: 44px; line-height: 1; }
.ledger-kicker { font-family: var(--type); font-size: 14px; color: var(--red-ink); }
#chapters { display: flex; flex-direction: column; gap: 20px; margin-top: 10px; }
.chapter.locked { opacity: 0.4; }
.chapter-head { display: flex; align-items: baseline; gap: 12px; border-bottom: 2px solid var(--ink); padding-bottom: 3px; margin-bottom: 4px; }
.chapter-no { font-family: var(--stencil); font-weight: 900; font-size: 28px; color: var(--red-ink); line-height: 1; }
.chapter-title { font-family: var(--gothic); font-weight: 700; font-size: 30px; line-height: 1; }
.chapter-seq { font-family: var(--print); font-style: italic; font-size: 17px; color: var(--ink-soft); }
.chapter-progress { margin-left: auto; font-family: var(--mono); font-size: 12px; color: var(--ink-soft); }
.nodes { display: flex; flex-direction: column; }
.node {
  display: flex; align-items: baseline; gap: 10px; padding: 6px 4px 5px; text-align: left; color: var(--ink);
  font-family: var(--type); font-size: 17px; position: relative;
}
.node:hover:not(:disabled) .node-title { color: var(--red-ink); }
.node:hover:not(:disabled)::before { content: '☞'; position: absolute; left: -30px; color: var(--red-ink); }
.node:disabled { cursor: default; color: var(--ink-faint); }
.node-id { font-family: var(--mono); font-size: 13px; color: var(--ink-soft); min-width: 40px; }
.node-title { flex: 0 1 auto; }
.node .fill { flex: 1; border-bottom: 2px dotted var(--ink-faint); transform: translateY(-5px); }
.node .status { font-family: var(--stencil); font-weight: 700; font-size: 15px; letter-spacing: 0.03em; color: var(--ink-faint); min-width: 96px; text-align: right; }
.node.solved .status { color: var(--red-ink); transform: rotate(-3deg); }
.node.current .status { color: var(--blue-ink); }
.node .elegant { font-family: var(--print); font-style: italic; color: #2f6b34; font-size: 15px; }

/* Codex: index cards */
#codex-screen { padding: 30px 20px; }
.codex-wrap { max-width: 1060px; margin: 0 auto; }
.codex-head { display: flex; align-items: center; gap: 16px; margin-bottom: 16px; }
.codex-head h2 { margin: 0; font-family: var(--gothic); font-weight: 900; font-size: 44px; color: #efe2c2; }
#codex-body { display: grid; grid-template-columns: 280px 1fr; gap: 26px; }
#codex-list { display: flex; flex-direction: column; gap: 5px; }
.codex-item {
  text-align: left; padding: 7px 12px; color: var(--ink); font-family: var(--type); font-size: 15px;
  background: var(--paper-2) var(--grain); box-shadow: 0 3px 6px rgba(0,0,0,0.4);
  border-top: 3px solid #9fb0c9;
}
.codex-item:hover, .codex-item.active { transform: translateX(8px); background-color: var(--paper); }
.codex-item.locked { opacity: 0.35; pointer-events: none; font-family: var(--mono); font-size: 12px; border-top-color: #777; }
#codex-entry { position: relative; min-height: 360px; padding: 34px 20px 20px; display: flex; justify-content: center; align-items: flex-start; }
#codex-entry .doc { position: relative; left: auto; top: auto; max-height: none; opacity: 1; transform: rotate(0.4deg); }
.codex-group { font-family: var(--gothic); font-weight: 700; font-size: 20px; color: #cbbd9e; margin: 10px 0 2px; }
.codex-group:first-child { margin-top: 0; }
.codex-empty { font-family: var(--print); font-style: italic; font-size: 20px; color: #a8987a; }

/* ---------------------------------------------------------------- story: documents on a desk */
#story {
  position: fixed; inset: 0; z-index: 20; cursor: pointer; overflow: hidden;
  background: radial-gradient(ellipse at 50% 42%, rgba(34, 24, 17, 0.97), rgba(6, 4, 3, 0.995) 72%);
}
#story-desk { position: absolute; inset: 0 0 64px; }
#story-controls { position: absolute; bottom: 22px; right: 30px; display: flex; gap: 18px; align-items: center; }
#story-progress { font-family: var(--mono); font-size: 12px; color: #8a7a60; }

.doc {
  position: absolute; left: 50%; top: 50%; max-height: calc(100vh - 150px);
  color: var(--ink); font-family: var(--print); font-size: 20px; line-height: 1.45;
  transform: translate(-50%, -44%) rotate(var(--tilt, 0deg)); opacity: 0;
  transition: transform .5s cubic-bezier(.2,.8,.3,1), opacity .4s, filter .5s;
}
.doc.in { opacity: 1; transform: translate(-50%, -50%) rotate(var(--tilt, 0deg)); }
.doc.piled {
  opacity: 1; filter: brightness(calc(1 - var(--depth, 1) * 0.28)) blur(calc(var(--depth, 1) * 0.6px));
  transform: translate(-50%, -50%) translate(calc(var(--depth, 1) * -26px), calc(var(--depth, 1) * 14px)) rotate(calc(var(--tilt, 0deg) - var(--depth, 1) * 2.5deg));
}
.doc p { margin: 0 0 0.75em; }
.doc p:last-child { margin-bottom: 0; }
.doc .sign { margin-top: 0.8em; text-align: right; font-style: italic; }
.doc-caption {
  position: absolute; bottom: 100%; left: 0; right: 0; margin-bottom: 10px;
  font-family: var(--mono); font-size: 12px; line-height: 1.4; color: #b8a888; font-style: normal;
}
.doc.top .doc-caption { text-shadow: 0 0 6px #000, 0 0 2px #000; }
.doc.piled .doc-caption { visibility: hidden; }
.doc-stamp {
  position: absolute; right: -18px; bottom: -14px; padding: 2px 10px;
  font-family: var(--stencil); font-weight: 900; font-size: 22px; text-transform: uppercase; letter-spacing: 0.03em;
  color: rgba(168, 35, 26, 0.85); border: 3px solid rgba(168, 35, 26, 0.85); border-radius: 4px;
  transform: rotate(-7deg); -webkit-mask: var(--ink-mask); mask: var(--ink-mask);
}
.paperish, .doc-note .card, .doc-pencil, .doc-notebook, .doc-letter, .doc-memo, .doc-clipping,
.doc-certificate, .doc-minutes, .doc-handbook, .doc-rhyme, .doc-question {
  background-color: var(--paper);
  background-image: radial-gradient(ellipse at 20% 10%, rgba(255,250,235,0.5), transparent 55%), var(--fibres), var(--grain);
  background-blend-mode: normal, multiply, multiply;
  box-shadow: 0 18px 40px rgba(0,0,0,0.65), 0 2px 4px rgba(0,0,0,0.5);
}

/* Chapter card: no paper, just type on the dark desk. */
.doc-title { text-align: center; width: min(760px, 90vw); }
.doc-title-text { font-family: var(--gothic); font-weight: 900; font-size: clamp(56px, 8vw, 96px); line-height: 1; color: #efe2c2; text-shadow: 0 3px 0 #000; }
.doc-title-sub { font-family: var(--print); font-style: italic; font-size: 22px; color: #b9aa8b; margin-top: 14px; }

/* Vane: typed on an Order note card. */
.doc-note .card { width: min(470px, 86vw); padding: 18px 26px 18px; font-family: var(--type); font-size: 18px; line-height: 1.5; }
.doc-long .card { width: min(620px, 90vw); font-size: 17px; }
.doc-note .card-head { font-family: var(--mono); font-size: 10px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--red-ink); border-bottom: 1px solid rgba(168,35,26,0.45); padding-bottom: 6px; margin-bottom: 12px; }
.doc-note .sign { font-family: var(--type); font-style: normal; }

/* Pike: soft pencil on the back of a work order. */
.doc-pencil {
  width: min(440px, 86vw); padding: 22px 26px; font-style: italic; color: #4a4640; font-size: 21px;
  background-image: repeating-linear-gradient(180deg, transparent 0 27px, rgba(38,28,20,0.07) 27px 28px), var(--fibres), var(--grain);
  background-blend-mode: normal, multiply, multiply;
}

/* Marrow: a pocket notebook, ruled, blue-black ink. */
.doc-notebook {
  width: min(460px, 86vw); padding: 20px 26px 24px 46px; font-style: italic; color: var(--blue-ink); font-size: 21px; line-height: 30px;
  background-color: #e9e2cc;
  background-image: linear-gradient(90deg, transparent 32px, rgba(168,35,26,0.35) 32px 33px, transparent 33px),
    repeating-linear-gradient(180deg, transparent 0 29px, rgba(36,52,106,0.18) 29px 30px), var(--grain);
  background-blend-mode: normal, normal, multiply;
}
.doc-notebook .doc-caption { font-style: normal; }
.nb-date { font-size: 17px; margin-bottom: 6px; text-decoration: underline; text-decoration-thickness: 1px; }
.doc-notebook p { margin-bottom: 30px; }

/* Mrs Pruett: a proper letter on good paper, in a careful hand. */
.doc-letter { width: min(540px, 88vw); padding: 26px 34px 28px; font-style: italic; color: #3a2414; font-size: 20px; }
.letter-head { text-align: right; font-size: 17px; margin-bottom: 14px; line-height: 1.3; }
.doc-letter .salutation { margin-bottom: 0.4em; }
.doc-letter .sign p { margin: 0; }

/* Office of Thresholds: a typed memorandum. */
.doc-memo { width: min(580px, 90vw); padding: 22px 30px 24px; font-family: var(--mono); font-size: 15px; line-height: 1.5; }
.memo-office { font-family: var(--gothic); font-weight: 700; font-size: 22px; text-align: center; line-height: 1; }
.memo-kind { text-align: center; font-size: 11px; letter-spacing: 0.3em; text-transform: uppercase; color: var(--ink-soft); margin: 4px 0 10px; }
.memo-fields { display: grid; grid-template-columns: 80px 1fr; gap: 1px 8px; margin: 0 0 12px; padding: 8px 0; border-top: 2px solid var(--ink); border-bottom: 1px solid var(--ink); }
.memo-fields dt { font-weight: 700; }
.memo-fields dd { margin: 0; }
.memo-items { margin: 0; padding-left: 24px; }
.memo-items li { margin-bottom: 6px; padding-left: 4px; }

/* The Lattice: teleprinter tape. */
.doc-tape { width: min(400px, 80vw); }
.tape-strip {
  margin: 0; padding: 16px 22px 22px; min-height: 60px;
  font-family: var(--mono); font-weight: 700; font-size: 16px; line-height: 1.55; letter-spacing: 0.1em; color: #2e2346; white-space: pre-wrap;
  background: #ddd3e8 var(--grain); background-blend-mode: multiply;
  box-shadow: 0 18px 40px rgba(0,0,0,0.65);
  clip-path: polygon(0 0, 100% 0, 100% calc(100% - 6px), 96% 100%, 90% calc(100% - 5px), 84% 100%, 76% calc(100% - 6px), 68% 100%, 60% calc(100% - 4px), 52% 100%, 44% calc(100% - 6px), 36% 100%, 28% calc(100% - 5px), 20% 100%, 12% calc(100% - 6px), 5% 100%, 0 calc(100% - 4px));
}
.doc-tape .doc-caption { color: #a8987a; }

/* Newspaper clipping: grey newsprint, cut edges. */
.doc-clipping {
  width: min(500px, 88vw); padding: 18px 24px 22px; background-color: #d9d3c4; font-size: 17px; line-height: 1.4;
  clip-path: polygon(0 1%, 3% 0, 50% 1%, 97% 0, 100% 2%, 99% 50%, 100% 98%, 96% 100%, 50% 99%, 2% 100%, 0 97%, 1% 50%);
}
.clip-paper { font-family: var(--gothic); font-weight: 900; font-size: 26px; text-align: center; line-height: 1; border-bottom: 3px double var(--ink); padding-bottom: 6px; }
.clip-date { text-align: center; font-family: var(--mono); font-size: 11px; color: var(--ink-soft); margin: 4px 0 10px; }
.clip-head { margin: 0 0 8px; font-family: var(--print); font-weight: 400; font-size: 27px; line-height: 1.1; text-align: center; text-transform: uppercase; letter-spacing: 0.02em; }
.clip-body { columns: 2; column-gap: 20px; text-align: justify; hyphens: auto; }

/* A charcoal rubbing of scratched letters. */
.doc-rubbing { width: min(460px, 86vw); }
.rub {
  padding: 34px 30px; text-align: center; font-family: var(--print); font-style: italic; font-size: 26px; line-height: 1.5;
  color: rgba(225, 218, 205, 0.8); text-shadow: 0 0 1px rgba(255,255,255,0.4);
  background-color: #2b2826;
  background-image: var(--grain), radial-gradient(ellipse at 50% 50%, #3a3633, #1c1a18 80%);
  background-blend-mode: screen, normal;
  box-shadow: 0 18px 40px rgba(0,0,0,0.65);
  clip-path: polygon(2% 3%, 30% 0, 70% 2%, 98% 0, 100% 40%, 97% 98%, 60% 100%, 20% 97%, 0 100%, 1% 50%);
}

/* Certificate of admission. */
.doc-certificate { width: min(580px, 90vw); padding: 30px 40px; text-align: center; outline: 2px solid var(--ink); outline-offset: -12px; font-size: 19px; }
.cert-heading { font-family: var(--mono); font-size: 12px; letter-spacing: 0.2em; text-transform: uppercase; color: var(--ink-soft); }
.cert-title { font-family: var(--gothic); font-weight: 900; font-size: 38px; line-height: 1.05; margin: 4px 0 14px; }
.cert-fields { display: grid; grid-template-columns: auto auto; justify-content: center; gap: 2px 12px; font-family: var(--mono); font-size: 14px; margin: 14px 0 0; }
.cert-fields dt { font-weight: 700; text-align: right; }
.cert-fields dd { margin: 0; text-align: left; }
.doc-certificate .sign { text-align: center; font-size: 17px; }

/* Guild minutes: an old ruled book. */
.doc-minutes { width: min(600px, 90vw); padding: 24px 32px; background-color: #e4d5b0; font-size: 18px; line-height: 1.45; }
.min-heading { font-style: italic; text-align: center; margin-bottom: 10px; }
.min-present { font-size: 16px; color: var(--ink-soft); }
.doc-minutes .memo-items { font-family: var(--print); }

/* A page of the Handbook, with Marrow’s pencil in the margin. */
.doc-handbook { width: min(680px, 92vw); padding: 22px 180px 26px 34px; font-size: 19px; line-height: 1.5; }
.hb-running { font-family: var(--print); font-style: italic; font-size: 13px; color: var(--ink-soft); text-align: center; border-bottom: 1px solid var(--ink-faint); padding-bottom: 4px; margin-bottom: 12px; }
.hb-title { margin: 0 0 8px; font-family: var(--gothic); font-weight: 700; font-size: 28px; line-height: 1; }
.hb-margin {
  position: absolute; right: 22px; top: 38%; width: 136px; transform: rotate(-5deg);
  font-family: var(--print); font-style: italic; font-size: 19px; line-height: 1.2; color: #57524a;
}

/* Skipping rhyme on an index card. */
.doc-rhyme { width: min(380px, 84vw); padding: 24px 30px; font-family: var(--type); font-size: 19px; line-height: 1.6; border-top: 4px solid #9fb0c9; }

/* The last paper on the bench. */
.doc-question { width: min(460px, 86vw); padding: 34px 32px 26px; text-align: center; }
.q-text { font-family: var(--print); font-style: italic; font-size: 40px; line-height: 1.1; color: #1d1712; }
.q-note { font-family: var(--mono); font-size: 12px; color: var(--ink-soft); margin: 10px 0 20px; }
.q-options { display: flex; flex-direction: column; align-items: center; gap: 10px; }
.q-option.chosen { background-color: var(--ink); color: var(--paper); }

/* ---------------------------------------------------------------- complete */
#complete { position: fixed; left: 0; right: 0; top: 80px; z-index: 15; display: flex; justify-content: center; pointer-events: none; }
.complete-card {
  pointer-events: auto; text-align: center; padding: 20px 40px 22px; min-width: 380px;
  transform: rotate(-0.8deg); animation: slide-in .5s cubic-bezier(.2,.9,.3,1.1);
}
@keyframes slide-in { from { opacity: 0; transform: translateY(-40px) rotate(-3deg) } to { opacity: 1; transform: rotate(-0.8deg) } }
.complete-kicker { font-family: var(--mono); font-size: 12px; color: var(--ink-soft); }
.complete-card h2 { font-family: var(--gothic); font-weight: 900; font-size: 36px; margin: 2px 0 8px; line-height: 1; }
#complete-stats { font-family: var(--type); font-size: 15px; color: var(--ink-soft); line-height: 1.6; }
#complete-seal { margin: 8px 0 2px; min-height: 22px; font-family: var(--print); font-style: italic; font-size: 19px; color: #2f6b34; }
.complete-actions { display: flex; gap: 12px; justify-content: center; margin-top: 14px; }
.stamp {
  position: absolute; right: -46px; top: -30px; padding: 2px 12px;
  font-family: var(--stencil); font-weight: 900; font-size: 40px; text-transform: uppercase; letter-spacing: 0.04em;
  color: rgba(176, 36, 24, 0.85); border: 4px solid rgba(176, 36, 24, 0.85); border-radius: 6px;
  transform: rotate(-11deg); mix-blend-mode: multiply;
  -webkit-mask: var(--ink-mask); mask: var(--ink-mask);
  animation: stamp .42s cubic-bezier(.3,1.6,.5,1) .25s both;
}
@keyframes stamp { from { opacity: 0; transform: rotate(-11deg) scale(2.4); } to { opacity: 1; transform: rotate(-11deg) scale(1); } }

#toast {
  position: fixed; z-index: 40; left: 50%; top: 82px; transform: translateX(-50%) rotate(-0.5deg);
  padding: 7px 22px; color: #3d2f5c; background: #d6cbe6 var(--grain);
  font-family: var(--mono); font-weight: 700; font-size: 13px; letter-spacing: 0.05em; text-transform: uppercase;
  box-shadow: 0 6px 16px rgba(0,0,0,0.5);
  clip-path: polygon(0 0, 100% 0, calc(100% - 8px) 50%, 100% 100%, 0 100%, 8px 50%);
  animation: tape .35s ease-out;
}
@keyframes tape { from { clip-path: polygon(0 0, 0 0, 0 50%, 0 100%, 0 100%, 0 50%); } }
/* Beat-by-beat replay: a brass counter over the board, and its button in the trial card. */
.beats-btn {
  margin-top: 2px; padding: 0; border: 0; background: none; cursor: pointer; text-align: left;
  font-family: var(--mono); font-size: 9.5px; white-space: nowrap; color: var(--ink-soft); text-decoration: underline dotted; text-underline-offset: 3px;
}
.beats-btn:hover, .beats-btn:focus-visible { color: var(--ink); }
#beat-plate {
  position: fixed; z-index: 30; left: 50%; bottom: 152px; transform: translateX(-50%);
  display: flex; align-items: baseline; gap: 12px; padding: 6px 18px 7px;
  background: linear-gradient(#3a2c1c, #1d150d); border: 1px solid #a98a4e; border-radius: 3px;
  box-shadow: 0 0 0 3px #0b0806, 0 0 0 4px rgba(169,138,78,0.5), 0 10px 24px rgba(0,0,0,0.6);
  color: #d9c08a; font-family: var(--mono); font-size: 12px; letter-spacing: 0.06em; pointer-events: none;
}
#beat-plate b { font-family: var(--stencil); font-weight: 700; font-size: 20px; letter-spacing: 0.04em; color: #f3d58f; }
#beat-plate span:last-child:not(:first-child) { color: #a8916a; }
#fade { position: fixed; inset: 0; z-index: 50; background: #000; opacity: 0; pointer-events: none; transition: opacity .45s; }
#fade.on { opacity: 1; }

/* ---------------------------------------------------------------- workbench */
.wb-actions { display: flex; gap: 8px; margin-top: 14px; flex-wrap: wrap; }
.wb-note { margin-top: 12px; font-family: var(--mono); font-size: 11px; line-height: 1.55; color: var(--ink-soft); }
.wb-note code { font-family: var(--mono); font-weight: 700; color: var(--ink); }
#brief b { font-weight: 400; text-decoration: underline; text-decoration-color: var(--red-ink); }

@media (max-width: 1440px) {
  :root { --tray: 190px; --brief: 256px; }
  #tray .form-no { display: none; }
  #docket dd { font-size: 12.5px; }
  #brief { font-size: 15px; }
}
@media (max-height: 820px) {
  :root { --top: 60px; --bottom: 124px; }
  #level-title { font-size: 23px; }
  .tool { padding: 3px 4px; }
  .tray-help { display: none; }
  .pip { width: 13px; height: 18px; }
  #tests-summary { font-size: 26px; }
  #btn-engage { width: 150px; }
  #btn-engage span { font-size: 28px; }
}
/* Narrow screens: the work order becomes a strip under the top bar showing only what is
   required; tapping it unfolds the whole order. */
@media (max-width: 1000px) {
  :root { --brief: 0px; --tray: 168px; --top: 124px; }
  #topbar { height: 60px; }
  #level-title { font-size: 21px; }
  #tray { padding: 10px 10px; }
  .tool { grid-template-columns: 28px 1fr auto; gap: 6px; }
  .tool canvas { width: 28px; height: 28px; }
  .tool-name { font-size: 13px; }
  #brief-panel {
    top: 62px; left: calc(var(--tray) + 6px); right: 12px; width: auto; transform: none;
    padding: 8px 14px; max-height: 54px; overflow: hidden; cursor: pointer;
  }
  #brief-panel::after { content: '▾'; position: absolute; right: 10px; top: 8px; font-size: 13px; color: var(--ink-soft); }
  #brief-panel:not(.expanded) .clip,
  #brief-panel:not(.expanded) .form-head,
  #brief-panel:not(.expanded) #docket,
  #brief-panel:not(.expanded) #stats,
  #brief-panel:not(.expanded) #hint { display: none !important; }
  #brief-panel:not(.expanded) .obj-label { display: inline; margin-right: 6px; }
  #brief-panel:not(.expanded) #brief { display: inline; font-size: 14px; }
  #brief-panel.expanded { max-height: 70vh; overflow-y: auto; z-index: 6; box-shadow: 0 20px 50px rgba(0,0,0,0.8); }
  #brief-panel.expanded::after { content: '▴'; }
  .keys { gap: 6px; }
  .key { width: 36px; height: 36px; font-size: 18px; }
}

@keyframes blink { 50% { opacity: 0; } }

/* ---------------------------------------------------------------- access */
:focus-visible { outline: 2px dashed var(--red-ink); outline-offset: 3px; }
.key:focus-visible, #btn-engage:focus-visible { outline-color: var(--gilt-hi); }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.001ms !important; animation-iteration-count: 1 !important; transition-duration: 0.12s !important; }
  .line-glitch, .seal-star { animation: none !important; }
}

/* Forged work orders: serial written by hand; margin notes in Marrow's pencil. */
#brief-no.hand-no { font-family: var(--print); font-style: italic; font-size: 15px; color: #1d1a24; }
#hint.marrow { color: #57524a; font-size: 18px; transform: rotate(0.8deg); }
#hint.marrow p::before { content: ''; }
#brief-no .bar7 { position: relative; }
#brief-no .bar7::after { content: ''; position: absolute; left: 8%; right: 18%; top: 66%; height: 1.4px; background: currentColor; transform: rotate(-8deg); }

/* Phones: the requisition slip shrinks to a column of icons; counts stay visible. */
@media (max-width: 700px) {
  :root { --tray: 70px; --top: 140px; }
  #tray { padding: 8px 6px; }
  #tray .form-head, .tray-help, .tool-name, .tool-key { display: none; }
  .tool { grid-template-columns: 1fr; justify-items: center; gap: 2px; padding: 4px 0; }
  .tool-count { font-size: 15px; }
  #brief-panel { max-height: 76px; }
  #brief-panel:not(.expanded) #brief { font-size: 13px; }
  #tests-label { width: 64px; }
  .card-legend { display: none; }
  #btn-engage { width: 104px; }
  #btn-engage span { font-size: 22px; padding: 2px 8px; }
}

/* Probe: a wire's or part's value in every trial. */
#tooltip.compact { max-width: none; padding: 6px 10px 7px; }
#tooltip.compact b { display: inline; font-size: 16px; margin-right: 8px; }
.probe { display: flex; align-items: center; gap: 8px; margin-top: 6px; }
#tooltip.compact .probe { display: inline-flex; margin-top: 0; }
.probe-label { font-family: var(--mono); font-size: 10px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--ink-soft); min-width: 52px; }
#tooltip.compact .probe-label { min-width: 0; }
.probe-pips { display: flex; gap: 3px; flex-wrap: wrap; max-width: 260px; }
.probe-pips i { width: 9px; height: 13px; border-radius: 2px; border: 1px solid rgba(38,28,20,0.35); }
.probe-pips i.on { background: radial-gradient(circle, #d8c6ff, #6a46c9 75%); border-color: transparent; }
.probe-pips i.none { border-style: dotted; opacity: 0.4; }
.probe-pips i.sel { outline: 1.5px solid var(--red-ink); outline-offset: 1px; }
.probe-pips i.bad { box-shadow: 0 0 0 2px var(--red-ink); }

/* Completion tally: this time, best, par. */
.tally { margin: 2px auto 0; border-collapse: collapse; font-family: var(--type); font-size: 15px; color: var(--ink); }
.tally th { font-family: var(--mono); font-weight: 400; font-size: 10px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--ink-soft); padding: 2px 10px; text-align: center; }
.tally td { padding: 2px 10px; text-align: center; border-top: 1px dotted var(--ink-faint); }
.tally tr th:first-child { text-align: left; font-family: var(--type); font-size: 15px; text-transform: none; letter-spacing: 0; color: var(--ink); }
.tally em { font-family: var(--print); font-size: 13px; color: #2f6b34; }

.extras-head { margin: 10px 0 2px 50px; font-family: var(--print); font-style: italic; font-size: 16px; color: #4a4640; }
.node.extra .node-title { font-family: var(--print); font-style: italic; color: #4a4640; }

#hint .sketch-note { font-size: 15px; color: var(--ink-soft); }
