@font-face { font-family: 'IM Fell English'; font-style: normal; font-weight: 400; font-display: swap; src: url(/fonts/im-fell-english.woff2) format('woff2'); }
@font-face { font-family: 'IM Fell English'; font-style: italic; font-weight: 400; font-display: swap; src: url(/fonts/im-fell-english-italic.woff2) format('woff2'); }
@font-face { font-family: 'IM Fell English SC'; font-style: normal; font-weight: 400; font-display: swap; src: url(/fonts/im-fell-english-sc.woff2) format('woff2'); }
@font-face { font-family: 'Mrs Saint Delafield'; font-style: normal; font-weight: 400; font-display: swap; src: url(/fonts/mrs-saint-delafield.woff2) format('woff2'); }

/* Vellum & iron-gall ink by day; cyanotype blueprint by night. */
:root {
  --paper: #efe5cf;
  --paper-deep: #e2d3b1;
  --ink: #2a1f16;
  --ink-soft: #6e5a45;
  --accent: #8e2b1c;
  --accent-soft: #b8513b;
  --tag: #dcc08a;
  --tag-ink: #3a2a17;
  --grain-blend: multiply;
  --grain-opacity: .55;
  --serif: 'IM Fell English', 'Iowan Old Style', Georgia, serif;
  --sc: 'IM Fell English SC', 'IM Fell English', Georgia, serif;
  --sign: 'Mrs Saint Delafield', 'Snell Roundhand', cursive;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #173a63;
    --paper-deep: #10294a;
    --ink: #e9eff7;
    --ink-soft: #a7bfdb;
    --accent: #ffb29b;
    --accent-soft: #e98d74;
    --tag: #d8bd86;
    --tag-ink: #3a2a17;
    --grain-blend: soft-light;
    --grain-opacity: .5;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --paper: #173a63; --paper-deep: #10294a; --ink: #e9eff7; --ink-soft: #a7bfdb;
  --accent: #ffb29b; --accent-soft: #e98d74; --grain-blend: soft-light; --grain-opacity: .5; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  background:
    radial-gradient(120% 90% at 50% 45%, transparent 55%, color-mix(in srgb, var(--paper-deep) 85%, #000 15%) 130%),
    var(--paper);
  color: var(--ink);
  font-family: var(--serif);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}
/* paper grain, foxing and a fold: rendered once, never animated */
body::after {
  content: ''; position: fixed; inset: 0; pointer-events: none; z-index: 5;
  opacity: var(--grain-opacity); mix-blend-mode: var(--grain-blend);
  background:
    linear-gradient(90deg, transparent calc(50% - 1px), rgba(0,0,0,.05) 50%, transparent calc(50% + 2px)),
    radial-gradient(circle at 12% 78%, rgba(120,70,20,.10) 0 2.2rem, transparent 2.6rem),
    radial-gradient(circle at 88% 14%, rgba(120,70,20,.08) 0 1.4rem, transparent 1.8rem),
    radial-gradient(circle at 67% 91%, rgba(120,70,20,.07) 0 .8rem, transparent 1.1rem),
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='240' height='240'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .35 0 0 0 0 .27 0 0 0 0 .18 0 0 0 .55 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}

#sheet { position: fixed; inset: 0; }
#plate { width: 100%; height: 100%; display: block; cursor: grab; touch-action: none; user-select: none; -webkit-user-select: none; }
#plate.grabbing { cursor: grabbing; }

/* ── ink ─────────────────────────────────────────────── */
.ink { fill: none; stroke: var(--ink); stroke-width: 2.1; stroke-linecap: round; stroke-linejoin: round; }
.ink.body { fill: var(--paper); }
.ink.fine { stroke-width: 1.25; }
.ink.shade { stroke-width: 3.8; }
.ink.frame { stroke-width: 3.4; }
.ink.soft { stroke: var(--ink-soft); }
.ink.spring { stroke-width: 1.6; }
.ink.leader { stroke-width: 1.1; }
.ink.lever { stroke-width: 3.4; }
.ink.spoke { stroke-width: 7; }
.ink.shelf { stroke-width: 2.8; }
.ink.lock-line { stroke-width: 2.6; }
.ink.plate-edge { stroke-width: 2.8; }
.ink.detail-ring { stroke-dasharray: 10 7; stroke-width: 1.5; }
.ink.traffic { stroke: var(--accent); stroke-width: 2.8; stroke-dasharray: 1 11; animation: traffic 1.6s linear infinite; }
.ink.traffic.shaft { stroke-width: 2.2; stroke-dasharray: 7 9; }
@keyframes traffic { to { stroke-dashoffset: -24; } }
.phantom { fill: none; stroke: var(--ink-soft); stroke-width: 1.5; stroke-dasharray: 30 8 4 8; stroke-linecap: round; }
.paper { fill: var(--paper); }
.dot { fill: var(--ink); }
.pat { stroke: var(--ink); stroke-width: 1.1; }
.pat-soft { stroke-width: .75; opacity: .55; }
.hatch-bg { fill: url(#hatch-wide); }
.hatch-bg.soft { opacity: .55; }
.hatch-fill { fill: url(#hatch); }
.hatch-dark { fill: url(#hatch-dense); }
.masonry { fill: url(#masonry); }
.perlage { fill: url(#hatch-wide); opacity: .45; }
.door-edge { fill: url(#hatch-dense); stroke: var(--ink); stroke-width: 1.5; }
.jewel { fill: var(--accent); stroke: var(--ink); stroke-width: 1.2; }
.jewel-glint { fill: var(--paper); opacity: .75; }
.jewel-stone { fill: var(--accent); stroke: var(--ink); stroke-width: 1; }
.wax { fill: var(--accent); }
.wax-ring { fill: none; stroke: rgba(0,0,0,.28); stroke-width: 2; }
.ribbon { fill: var(--accent-soft); stroke: var(--ink); stroke-width: 1; }
.wax-text { font: 9.5px var(--sc); fill: var(--paper); letter-spacing: 1.6px; }
.wax-mono { font: italic 27px var(--serif); fill: var(--paper); }
.pulse { fill: none; stroke: var(--accent); stroke-width: 3; }

/* ── lettering ───────────────────────────────────────── */
#plate text { fill: var(--ink); font-family: var(--serif); }
.t-title { font: 58px var(--sc) !important; letter-spacing: 10px; }
.t-sub { font: 27px var(--sc) !important; letter-spacing: 4px; }
.t-italic { font-style: italic; font-size: 25px; }
.t-small { font-size: 21px; }
.soft-text { fill: var(--ink-soft) !important; }
.t-sc { font-family: var(--sc) !important; letter-spacing: 2px; }
.t-sign { font: 70px var(--sign) !important; }
.t-sign-sm { font-size: 50px !important; }
.t-sign-lg { font-size: 104px !important; }
.t-fig { font: italic 36px var(--serif) !important; letter-spacing: 3px; }
.t-fig-sm { font-size: 26px !important; }
.t-lane { font: 18px var(--sc) !important; letter-spacing: 6px; fill: var(--ink-soft) !important; }
.t-letter { font: italic 36px var(--serif) !important; }
.t-letter.soft { fill: var(--ink-soft) !important; font-size: 30px !important; }
.t-name { font: italic 21px var(--serif) !important; fill: var(--ink-soft) !important; }
.t-dname { font-size: 36px; }
.t-dname.sm { font-size: 28px; }
.t-dtext { font-size: 21px; }
.t-dnote { font: italic 18px var(--serif) !important; fill: var(--ink-soft) !important; }
.t-btn { font: 21px var(--sc) !important; letter-spacing: 2px; }
.t-box { font: 28px var(--sc) !important; letter-spacing: 2px; }

.label.pending { opacity: 0; }
.label.inking { animation: appear 1.1s ease forwards; }
.label.muted .t-letter { fill: var(--ink-soft) !important; }
@keyframes appear { from { opacity: 0; filter: blur(2px); } to { opacity: 1; filter: none; } }

/* ── interaction ─────────────────────────────────────── */
.hit { fill: transparent; cursor: pointer; outline: none; }
.hit:focus-visible { stroke: var(--accent); stroke-width: 3; stroke-dasharray: 7 7; }
.part:hover > .rot .ink.body:first-child, .worker:hover .rot .ink.body { fill: color-mix(in srgb, var(--paper) 88%, var(--accent) 12%); }
.detail-face { fill: var(--paper); }
.detail-btn, .detail-close, .vault-box { cursor: pointer; outline: none; }
.detail-btn:hover .t-btn, .detail-btn:focus-visible .t-btn, .vault-box:hover text { fill: var(--accent) !important; }
.detail-btn:focus-visible .ink, .detail-close:focus-visible .ink { stroke: var(--accent); }

/* first visit: the drawing is inked in, left to right */
#plate.inking .ink, #plate.inking .phantom { stroke-dasharray: 1 1; stroke-dashoffset: 1; animation: ink 1.5s cubic-bezier(.4,.1,.3,1) forwards; animation-delay: var(--d, 0s); }
#plate.inking text, #plate.inking .masonry, #plate.inking .hatch-bg, #plate.inking .hatch-fill, #plate.inking .wax, #plate.inking .jewel, #plate.inking .ribbon {
  opacity: 0; animation: appear 1s ease forwards; animation-delay: 1.6s;
}
@keyframes ink { to { stroke-dashoffset: 0; } }

/* ── key tag (login) ─────────────────────────────────── */
.tag {
  position: fixed; top: max(14px, env(safe-area-inset-top)); right: max(18px, env(safe-area-inset-right)); z-index: 10;
  min-width: 132px; min-height: 64px; padding: 12px 18px 10px 36px;
  background: var(--tag); color: var(--tag-ink);
  clip-path: polygon(16px 0, 100% 0, 100% 100%, 16px 100%, 0 calc(100% - 16px), 0 16px);
  transform: rotate(-5deg); transform-origin: 10% 50%;
  box-shadow: inset 0 0 0 1px rgba(58,42,23,.25);
  font-family: var(--serif);
  transition: transform .4s cubic-bezier(.3,1.6,.5,1);
  filter: drop-shadow(2px 3px 0 rgba(0,0,0,.12));
}
.tag:hover { transform: rotate(-1deg); }
.tag-hole { position: absolute; left: 12px; top: 50%; width: 12px; height: 12px; margin-top: -6px; border-radius: 50%; background: var(--paper); box-shadow: inset 0 0 0 2px var(--tag-ink), 0 0 0 4px rgba(58,42,23,.18); }
.tag-link { color: inherit; text-decoration: none; display: block; }
.tag-title { display: block; font: 22px var(--sc); letter-spacing: 2px; line-height: 1.1; }
.tag-sub { display: block; font: italic 15px var(--serif); opacity: .8; background: none; border: 0; padding: 0; color: inherit; cursor: pointer; text-decoration: underline; text-decoration-thickness: 1px; text-underline-offset: 3px; }
.tag.in .tag-title { font: italic 20px var(--serif); letter-spacing: 0; }
.tag a:focus-visible, .tag button:focus-visible { outline: 2px dashed var(--accent); outline-offset: 3px; }

/* ── controls ────────────────────────────────────────── */
.controls { position: fixed; right: max(16px, env(safe-area-inset-right)); bottom: max(16px, env(safe-area-inset-bottom)); z-index: 10; display: flex; flex-direction: column; gap: 8px; }
.controls button {
  width: 42px; height: 42px; border-radius: 50%; border: 1.5px solid var(--ink); background: var(--paper);
  color: var(--ink); font: 24px/1 var(--serif); cursor: pointer; display: grid; place-items: center;
  box-shadow: inset 0 0 0 3px var(--paper), inset 0 0 0 4px var(--ink);
}
.controls svg { width: 20px; height: 20px; fill: none; stroke: var(--ink); stroke-width: 1.5; }
.bar { position: fixed; left: max(16px, env(safe-area-inset-left)); bottom: max(14px, env(safe-area-inset-bottom)); z-index: 10; display: flex; align-items: center; gap: 6px; font: 17px var(--sc); color: var(--ink-soft); }
.bar button { background: none; border: 0; padding: 6px 4px; font: inherit; color: var(--ink-soft); cursor: pointer; letter-spacing: 2px; }
.bar button[aria-pressed="true"] { color: var(--ink); text-decoration: underline; text-underline-offset: 4px; }
#spec-toggle { margin-left: 14px; padding: 7px 14px; border: 1.5px solid var(--ink); color: var(--ink); background: var(--paper); }
.controls button:hover, #spec-toggle:hover { color: var(--accent); border-color: var(--accent); }
button:focus-visible { outline: 2px dashed var(--accent); outline-offset: 3px; }

#skip, #toast { position: fixed; left: 50%; transform: translateX(-50%); margin: 0; z-index: 10; font: italic 17px var(--serif); color: var(--ink-soft); pointer-events: none; text-align: center; }
#hint { margin-left: 16px; font: italic 16px var(--serif); letter-spacing: 0; color: var(--ink-soft); animation: hint 10s ease forwards; pointer-events: none; }
@keyframes hint { 0%, 70% { opacity: 1; } 100% { opacity: 0; } }
#skip { bottom: max(22px, env(safe-area-inset-bottom)); color: var(--accent); }
#toast { top: 22px; padding: 10px 18px; background: var(--paper); border: 1.5px solid var(--accent); color: var(--accent); font-style: normal; }
[hidden] { display: none !important; }
.legal { position: fixed; right: max(76px, calc(env(safe-area-inset-right) + 60px)); bottom: max(22px, env(safe-area-inset-bottom)); z-index: 10; font: italic 16px var(--serif); color: var(--ink-soft); }
.legal a { color: var(--ink-soft); text-decoration: none; }
.legal a:hover, .legal a:focus-visible { color: var(--accent); text-decoration: underline; text-underline-offset: 3px; }

/* ── specification drawer ────────────────────────────── */
#spec {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 9; width: min(440px, 92vw);
  padding: 96px 30px 90px 34px; overflow-y: auto; overscroll-behavior: contain;
  background: var(--paper); border-left: 1.5px solid var(--ink);
  box-shadow: -14px 0 40px -24px rgba(0,0,0,.45);
  transform: translateX(105%); transition: transform .32s cubic-bezier(.3,.7,.2,1);
  font-size: 17.5px; line-height: 1.5; hyphens: auto; -webkit-hyphens: auto;
}
#spec::before { content: ''; position: absolute; inset: 10px 10px 10px 10px; border: 1px solid var(--ink-soft); pointer-events: none; }
.spec-open #spec { transform: none; }
#spec p { margin: 0 0 .9em; text-align: justify; }
#spec p.intro::first-letter { font-size: 3.1em; float: left; line-height: .8; padding: 6px 6px 0 0; font-family: var(--sc); }
#spec .refs { list-style: none; padding: 0; margin: 0 0 1.1em; }
#spec .refs li { margin: 0 0 .55em; padding-left: 1.2em; text-indent: -1.2em; text-align: left; }
#spec .ref { background: none; border: 0; padding: 0; font: inherit; color: var(--ink); cursor: pointer; text-align: left; }
#spec .ref i { font-size: 1.15em; margin-right: .1em; }
#spec .ref:hover { color: var(--accent); }
#spec .desc { color: var(--ink-soft); }
#spec .lockword { font-style: italic; color: var(--ink-soft); }
#spec .go { color: var(--accent); text-decoration: none; white-space: nowrap; font-variant: small-caps; letter-spacing: .04em; }
#spec .go:hover { text-decoration: underline; }
#spec .link { cursor: pointer; }
#spec .link:hover { color: var(--accent); }
#spec .claim { font-style: italic; border-top: 1px solid var(--ink-soft); padding-top: .8em; }

#fallback { position: fixed; inset: 0; z-index: 20; padding: 40px; background: var(--paper); }
#fallback a { color: var(--ink); }

@media (max-width: 760px) {
  .tag { min-width: 112px; padding: 9px 12px 8px 30px; min-height: 54px; }
  .tag-title { font-size: 19px; }
  #spec { top: auto; left: 0; width: 100%; height: 74dvh; padding: 30px 22px 90px; border-left: 0; border-top: 1.5px solid var(--ink); transform: translateY(105%); }
  .spec-open #spec { transform: none; }
  #hint { display: none; }
  .controls button { width: 40px; height: 40px; }
}
/* Portrait phones: drawing on top, the specification reads along the bottom. */
@media (max-width: 760px) and (orientation: portrait) {
  #sheet { bottom: 38dvh; }
  .legal { top: max(12px, env(safe-area-inset-top)); bottom: auto; right: auto; left: 16px; font-size: 14px; }
  #spec { height: 38dvh; transform: none; padding-top: 62px; transition: height .32s cubic-bezier(.3,.7,.2,1); }
  .spec-open #spec { height: 88dvh; }
  .bar { bottom: auto; top: calc(62dvh + 12px); z-index: 11; transition: top .32s cubic-bezier(.3,.7,.2,1); }
  .spec-open .bar { top: calc(12dvh + 12px); }
  .controls { bottom: calc(38dvh + 12px); flex-direction: row; }
  .spec-open .controls { display: none; }
  #skip { bottom: calc(38dvh + 18px); }
}
@media (prefers-reduced-motion: reduce) {
  .ink.traffic { animation: none; }
  .tag, #spec { transition: none; }
}
