:root {
  --baize: #1d4a42;
  --baize-deep: #0e2a27;
  --lamp: #2f6a5c;
  --walnut: #4d2c1b;
  --walnut-deep: #2c170d;
  --brass: #dcae52;
  --brass-hi: #ffe9a6;
  --brass-lo: #8f6a24;
  --tag: #efe1bb;
  --tag-ink: #2a1c10;
  --ink-on-baize: #f1ead2;
  --muted-on-baize: #bfd2c4;
  --ribbon: #a8303f;
  --focus: #fff27a;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "URW Palladio L", P052, Georgia, serif;
  --tag-face: "American Typewriter", "Rockwell", "Roboto Slab", "DejaVu Serif", Georgia, serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  background: var(--baize-deep);
  background-image: radial-gradient(90vmax 70vmax at 50% 42%, var(--lamp) 0%, var(--baize) 48%, var(--baize-deep) 100%);
  background-attachment: fixed;
  color: var(--ink-on-baize);
  font-family: var(--serif);
  font-size: 17px;
  line-height: 1.45;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
.global-defs { position: absolute; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 48px; padding: 10px 20px;
  font: 700 17px/1.1 var(--serif);
  border-radius: 6px; cursor: pointer; text-decoration: none;
  border: 2px solid transparent;
}
.btn:disabled { opacity: .45; cursor: default; }
.btn-brass {
  color: #2a1a08;
  background: linear-gradient(180deg, #ffe7a2, #dcae52 55%, #b58632);
  border-color: #6b4e16;
  box-shadow: inset 0 1px 0 #fff6d4, 0 3px 0 #5a4110;
}
.btn-brass:active:not(:disabled) { transform: translateY(2px); box-shadow: inset 0 1px 0 #fff6d4, 0 1px 0 #5a4110; }
.btn-quiet { color: var(--ink-on-baize); background: rgba(8, 26, 24, .55); border-color: #6f958a; }
.btn-quiet:hover:not(:disabled) { background: rgba(8, 26, 24, .8); }
.btn-mini {
  display: inline-flex; align-items: center; min-height: 40px; padding: 6px 11px;
  font: 600 14px/1.1 var(--serif); color: var(--ink-on-baize); text-decoration: none; cursor: pointer;
  background: rgba(8, 26, 24, .6); border: 1px solid #6f958a; border-radius: 5px;
}
.btn-mini[aria-pressed="true"] { background: var(--brass); color: #2a1a08; border-color: #6b4e16; }

/* Stamped brass nameplate, the title's one flourish */
.nameplate {
  display: flex; align-items: center; justify-content: center; gap: clamp(10px, 3vw, 26px);
  padding: clamp(10px, 2.2vw, 20px) clamp(14px, 4vw, 34px);
  background: linear-gradient(170deg, #f7dc92, #d6a648 45%, #a97c2b 70%, #dfb860);
  border: 2px solid #5d4519; border-radius: 6px;
  box-shadow: 0 2px 0 #fff0bd inset, 0 -2px 0 #6b4e16 inset, 0 12px 26px rgba(0, 0, 0, .45);
}
.nameplate h1 {
  margin: 0; color: #3a2709;
  font: 800 clamp(30px, 7.6vw, 68px)/1 var(--tag-face);
  letter-spacing: .02em; text-align: center;
  text-shadow: 0 1px 0 #ffefb8, 0 -1px 0 #6d4f17;
}
.nameplate h1:focus { outline: none; }
.plate-screw {
  flex: 0 0 auto; width: 15px; height: 15px; border-radius: 50%;
  background: linear-gradient(135deg, #eef1f2, #6f7b80); border: 1px solid #2a1c10; position: relative;
}
.plate-screw::after { content: ""; position: absolute; left: 1px; right: 1px; top: 5.5px; height: 2px; background: #2a1c10; transform: rotate(32deg); }

/* Title */
.title-screen { min-height: 100dvh; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: clamp(14px, 3vh, 28px); padding: 20px 16px 28px; }
.title-shelf, .result-shelf {
  display: flex; justify-content: center; align-items: flex-end; gap: clamp(4px, 2vw, 26px);
  width: min(900px, 100%); padding: 0 clamp(4px, 2vw, 20px) 14px;
  background: linear-gradient(0deg, var(--walnut-deep) 0 14px, transparent 14px);
  border-bottom: 5px solid #1b0d06;
}
.title-shelf svg, .result-shelf svg { flex: 1 1 0; min-width: 0; height: auto; max-height: 34vh; filter: drop-shadow(0 8px 6px rgba(0, 0, 0, .5)); }
.title-copy { max-width: 620px; text-align: center; }
.lede { font-size: clamp(18px, 2.8vw, 22px); margin: 0 0 18px; }
.title-actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }
.fineprint { font-size: 14.5px; color: var(--muted-on-baize); margin: 14px auto 0; max-width: 62ch; }

/* Bench */
.bench-screen {
  min-height: 100dvh; padding: 8px 12px 12px;
  display: grid; gap: 10px;
  grid-template-columns: minmax(230px, 280px) minmax(0, 1fr) minmax(250px, 320px);
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  grid-template-areas:
    "top top top"
    "ticket stage kit"
    "note stage kit"
    "note actions kit";
  height: 100dvh;
}
.topbar { grid-area: top; display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.topbar-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.shelf {
  display: flex; gap: 6px; padding: 0 8px 7px;
  background: linear-gradient(0deg, var(--walnut-deep) 0 7px, transparent 7px);
  border-bottom: 3px solid #1b0d06;
}
.alcove { position: relative; width: 56px; height: 56px; }
.alcove svg { width: 100%; height: 100%; display: block; }
.alcove.waiting svg { opacity: .32; filter: grayscale(1) brightness(.75); }
.alcove.current { outline: 2px dashed var(--brass); outline-offset: 1px; border-radius: 4px; }
.alcove.done svg { animation: settle .55s cubic-bezier(.2, 1.5, .4, 1); }
.alcove .ribbon { position: absolute; right: -3px; top: -2px; width: 19px; height: 25px; background: var(--ribbon); clip-path: polygon(0 0, 100% 0, 100% 100%, 50% 74%, 0 100%); }
.alcove .ribbon::after { content: ""; position: absolute; left: 5px; top: 4px; width: 9px; height: 9px; border-radius: 50%; background: var(--brass-hi); }
@keyframes settle { from { transform: translateY(-30px) scale(1.25); opacity: 0; } to { transform: none; opacity: 1; } }

.ticket {
  grid-area: ticket; position: relative; align-self: start;
  background: var(--tag); color: var(--tag-ink);
  padding: 30px 18px 16px; transform: rotate(-1.2deg);
  clip-path: polygon(14% 0, 86% 0, 100% 9%, 100% 100%, 0 100%, 0 9%);
  overflow: auto; max-height: 100%;
}
.ticket-hole { position: absolute; top: 9px; left: 50%; width: 13px; height: 13px; margin-left: -6px; border-radius: 50%; background: var(--baize-deep); }
.ticket h2 { margin: 0; font: 700 21px/1.15 var(--tag-face); }
.ticket-maker { margin: 3px 0 10px; font: 400 14px/1.3 var(--tag-face); color: #5f4422; }
.ticket-story { margin: 0; font-size: 16px; line-height: 1.42; }
.notes:empty { display: none; }
.notes { margin-top: 12px; padding-top: 10px; border-top: 1px dashed #8e7650; font-size: 14.5px; line-height: 1.32; }
.notes p { margin: 0 0 7px; }

.stage-wrap { grid-area: stage; min-height: 0; min-width: 0; display: flex; align-items: center; justify-content: center; }
.stage { display: block; width: 100%; height: 100%; max-height: 100%; touch-action: manipulation; overflow: visible; }
.engraving { font-family: var(--serif); }
.spot { cursor: pointer; outline: none; }
.spot .hit { fill: transparent; }
.spot .ring { fill: none; stroke: transparent; stroke-width: 3.5; pointer-events: none; }
.spot:hover .ring { stroke: rgba(255, 233, 166, .9); stroke-dasharray: 5 5; }
.spot:focus-visible .ring { stroke: var(--focus); stroke-width: 5.5; stroke-dasharray: none; }
.spot.fits .ring { stroke: var(--brass-hi); stroke-width: 5; animation: pulse 1.1s ease-in-out infinite; }
.spot.misfit .ring { stroke: rgba(255, 255, 255, .34); stroke-dasharray: 3 7; }
.spot.hinted .ring { stroke: #7ff2ff; stroke-width: 6; animation: pulse .8s ease-in-out infinite; }
.spot.trouble .ring { stroke: #ff8d7a; stroke-width: 6; animation: pulse .7s ease-in-out infinite; }
@keyframes pulse { 50% { stroke-opacity: .35; } }
.lid-lifting { transform-box: fill-box; transform-origin: 50% 0; animation: lidlift .65s cubic-bezier(.5, 0, .7, .5) forwards; }
@keyframes lidlift { 35% { transform: scaleY(.58) translateY(-40px); opacity: 1; } 100% { transform: scaleY(.06) translateY(-430px); opacity: 0; } }
.refused { animation: refuse .36s linear; transform-box: fill-box; transform-origin: center; }
@keyframes refuse { 20% { transform: translateX(-9px); } 45% { transform: translateX(8px); } 70% { transform: translateX(-5px); } 100% { transform: translateX(0); } }
.fresh { animation: drop .32s cubic-bezier(.2, 1.3, .4, 1); transform-box: fill-box; transform-origin: center; }
@keyframes drop { from { transform: scale(1.22); opacity: .3; } }
.twinkle { animation: twinkle 1.4s ease-out forwards; opacity: 0; transform-box: fill-box; transform-origin: center; }
@keyframes twinkle { 25% { opacity: 1; transform: scale(1.1) rotate(25deg); } 100% { opacity: 0; transform: scale(.2) rotate(70deg); } }

.note-wrap { grid-area: note; align-self: end; min-width: 0; }
.note {
  margin: 0; min-height: 3.1em; padding: 9px 13px;
  background: rgba(8, 26, 24, .72); border-left: 4px solid var(--brass); border-radius: 0 5px 5px 0;
  font-size: 17px; line-height: 1.32;
}
.note.good { border-left-color: var(--brass-hi); background: rgba(70, 50, 8, .86); }
.note.hint { border-left-color: #7ff2ff; }

.timing { margin-top: 8px; padding: 8px 10px; background: rgba(8, 26, 24, .72); border-radius: 5px; font-size: 14px; }
.timing table { border-collapse: separate; border-spacing: 3px; width: 100%; }
.timing th { font-weight: 600; text-align: left; font-size: 14px; padding-right: 4px; white-space: nowrap; }
.timing thead th { text-align: center; font-weight: 400; color: var(--muted-on-baize); font-size: 13px; padding: 0; }
.timing td { height: 15px; background: #113430; border-radius: 2px; }
.timing td.on { background: var(--brass); }

.kit {
  grid-area: kit; min-height: 0; min-width: 0; overflow: auto;
  display: flex; flex-direction: column; gap: 10px;
}
.tools {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(50px, 1fr)); gap: 5px; padding: 8px;
  background: linear-gradient(180deg, #5d3a22, #3f2415); border-radius: 7px; border: 1px solid #1b0d06;
}
.tool, .part {
  appearance: none; font-family: var(--serif); color: inherit; cursor: pointer; padding: 3px; position: relative;
  display: flex; flex-direction: column; align-items: center; justify-content: flex-start; gap: 1px; border-radius: 6px;
}
.tool { background: transparent; border: 2px solid transparent; color: #f3e4c2; min-height: 62px; font-size: 13.5px; line-height: 1.1; }
.tool svg { width: 36px; height: 36px; display: block; }
.tool[aria-pressed="true"] { background: rgba(255, 233, 166, .2); border-color: var(--brass-hi); }
.tray {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 7px; padding: 9px;
  background: linear-gradient(170deg, #6d4326, #4a2a18); border-radius: 7px; border: 1px solid #1b0d06;
  box-shadow: inset 0 3px 10px rgba(0, 0, 0, .55);
}
.tray:empty::before { content: "All parts are on the bench."; grid-column: 1 / -1; padding: 8px; color: #e8d7ad; font-style: italic; }
.part { background: radial-gradient(circle at 50% 38%, #36251a, #1f130c); border: 2px solid #1b0d06; min-height: 100px; }
.part svg { width: 54px; height: 54px; display: block; flex: none; }
.part .tag { display: block; width: 100%; padding: 2px 3px; background: var(--tag); color: var(--tag-ink); font: 400 12px/1.13 var(--tag-face); border-radius: 2px; text-align: center; }
.part kbd { position: absolute; top: 2px; left: 4px; font: 400 11px var(--tag-face); color: #d4c198; background: none; border: 0; }
.part[aria-pressed="true"] { border-color: var(--brass-hi); background: radial-gradient(circle at 50% 38%, #6f5426, #33220f); transform: translateY(-3px); }
.scrap { font-size: 14px; color: var(--muted-on-baize); }
.scrap:empty { display: none; }

.actions { grid-area: actions; display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.btn-test { min-width: 150px; font-size: 19px; }

/* Rules dialog */
.rules {
  width: min(600px, calc(100vw - 24px)); max-height: calc(100dvh - 24px);
  background: var(--tag); color: var(--tag-ink); border: 0; border-radius: 6px; padding: 20px 22px;
  font-size: 16.5px;
}
.rules::backdrop { background: rgba(4, 16, 14, .72); }
.rules h2 { margin: 0 0 6px; font: 700 26px/1.1 var(--tag-face); }
.rules h3 { margin: 14px 0 4px; font: 700 17px/1.2 var(--tag-face); }
.rules ol, .rules ul { margin: 6px 0; padding-left: 24px; }
.rules li { margin-bottom: 5px; }
.rules p { margin: 6px 0; }
.rules .fineprint { color: #5f4422; margin: 12px 0; }
.rules kbd { font: 600 13px var(--tag-face); background: #fff8e4; border: 1px solid #8e7650; border-radius: 3px; padding: 0 4px; }

/* Result */
.result-screen { min-height: 100dvh; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; padding: 18px 14px 26px; }
.nameplate-small h1 { font-size: clamp(24px, 5.6vw, 44px); }
.result-line { margin: 0; text-align: center; font-size: clamp(18px, 2.6vw, 22px); max-width: 44ch; }
.result-shelf svg { max-height: 30vh; }
.ledger { border-collapse: collapse; background: var(--tag); color: var(--tag-ink); font-size: 15px; width: min(640px, 100%); }
.ledger caption { caption-side: top; text-align: left; color: var(--ink-on-baize); padding-bottom: 5px; }
.ledger th, .ledger td { padding: 6px 7px; border-bottom: 1px solid #c8b78c; text-align: right; font-variant-numeric: tabular-nums; }
.ledger th:first-child, .ledger td:first-child { text-align: left; }
.ledger thead th { font: 700 13px/1.15 var(--tag-face); vertical-align: bottom; }
.ledger tfoot td { font-weight: 800; font-size: 17px; border-bottom: 0; }

@media (max-width: 899px) {
  body { font-size: 16px; }
  .bench-screen {
    height: auto; gap: 7px; grid-template-columns: minmax(0, 1fr); grid-template-rows: none;
    grid-template-areas: "top" "stage" "note" "kit" "ticket"; padding: 5px 8px 76px;
  }
  /* A shut keepsake needs no tools yet, so its story takes their place. */
  .bench-screen[data-closed="true"] { grid-template-areas: "top" "stage" "ticket" "note" "kit"; }
  .topbar { flex-wrap: nowrap; align-items: flex-end; gap: 4px; }
  .shelf { padding: 0 2px 5px; gap: 1px; flex: none; }
  .alcove { width: 36px; height: 36px; }
  .alcove .ribbon { width: 13px; height: 17px; right: -1px; top: -1px; }
  .alcove .ribbon::after { left: 3.5px; top: 3px; width: 6px; height: 6px; }
  .topbar-actions { justify-content: flex-end; flex-wrap: nowrap; gap: 4px; }
  .btn-mini { font-size: 12.5px; min-height: 38px; padding: 3px 6px; white-space: nowrap; }
  .stage-wrap { width: 100%; }
  .stage { width: 100%; height: auto; max-height: 42dvh; min-height: 250px; }
  .note { min-height: 3.7em; font-size: 15.5px; padding: 6px 10px; line-height: 1.26; }
  .timing { margin-top: 5px; padding: 4px 8px; }
  .timing td { height: 10px; }
  .kit { overflow: visible; gap: 6px; }
  .tools { padding: 3px 4px; gap: 2px; }
  .tool { min-height: 48px; font-size: 12.5px; padding: 1px; }
  .tool svg { width: 27px; height: 27px; }
  .tray { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 4px; padding: 5px; }
  .part { min-height: 76px; padding: 2px; }
  .part svg { width: 40px; height: 40px; }
  .part .tag { font-size: 11.5px; line-height: 1.08; padding: 1px 2px; }
  .scrap { font-size: 13px; }
  .ticket { align-self: stretch; transform: none; max-height: none; clip-path: none; padding: 9px 12px; border-radius: 5px; }
  .ticket h2 { font-size: 18px; }
  .ticket-maker { margin-bottom: 5px; }
  .ticket-story { font-size: 15px; line-height: 1.32; }
  .ticket-hole { display: none; }
  .actions {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 3; padding: 7px 8px calc(7px + env(safe-area-inset-bottom));
    background: rgba(8, 26, 24, .96); border-top: 1px solid #6f958a;
    flex-wrap: nowrap;
  }
  .actions .btn { flex: 1 1 0; padding: 8px 6px; }
  .actions .btn-test { flex: 1.5 1 0; min-width: 0; }
  .ledger { font-size: 13.5px; }
  .ledger th, .ledger td { padding: 5px 3px; }
  .ledger thead th { font-size: 11.5px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition: none !important; }
  .spot.fits .ring, .spot.hinted .ring, .spot.trouble .ring { stroke-opacity: 1; }
  .lid-lifting { opacity: 0; }
}
