/* Bow-Tie Builder. Light theme only. Tokens from PLAN.md section 1. */
:root {
  --kraft: #C9A878;
  --kraft-deep: #B8935F;
  --desk: #7A5A3A;
  --desk-deep: #5C4128;
  --paper: #FBF7EF;
  --ink: #1F2A36;
  --ink-soft: #5B6572;
  --suture: #1E5BC6;
  --teal: #1A8F7A;
  --amber: #D98A1F;
  --pin-ring: rgba(31, 42, 54, 0.28);
  --radius: 6px;
  --font: 'Atkinson Hyperlegible', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --ease-back: cubic-bezier(0.34, 1.56, 0.64, 1);
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
  --card-w: 150px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font);
  color: var(--ink);
  background: var(--desk);
  background-image:
    radial-gradient(ellipse 120% 80% at 50% -10%, rgba(255, 221, 160, 0.35), transparent 60%),
    linear-gradient(180deg, #6E5033 0%, var(--desk) 40%, var(--desk-deep) 100%);
  min-height: 100dvh;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; }

#stage {
  position: relative;
  width: 100%;
  height: 100dvh;
  max-width: 1100px;
  margin: 0 auto;
  overflow: hidden;
}
#stage.narrow { max-width: 360px; height: 640px; margin-top: 20px; border: 1px dashed rgba(255,255,255,0.3); }

/* Screens: crossfade stack */
.screen {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  opacity: 0; pointer-events: none; visibility: hidden;
  transition: opacity 250ms ease, visibility 0s linear 250ms;
}
.screen.active { opacity: 1; pointer-events: auto; visibility: visible; transition-delay: 0s; }
#screen-play { justify-content: flex-start; }

/* Cards (paper panels for non-play screens) */
.start-card, .intro-card, .debrief-card, .end-card, .sources-card, .pause-card {
  background: var(--paper);
  color: var(--ink);
  border-radius: var(--radius);
  padding: 28px 28px 24px;
  width: min(92%, 440px);
  text-align: center;
  box-shadow: 0 18px 40px rgba(0,0,0,0.35), 0 2px 0 rgba(255,255,255,0.6) inset;
  position: relative;
}
.start-card::before, .intro-card::before, .end-card::before {
  /* a strip of tape */
  content: ""; position: absolute; top: -12px; left: 50%; transform: translateX(-50%) rotate(-2deg);
  width: 92px; height: 24px; background: rgba(255, 236, 190, 0.75);
  box-shadow: 0 1px 2px rgba(0,0,0,0.12);
}
h1 { font-size: 2rem; margin: 8px 0 4px; letter-spacing: -0.01em; }
h2 { font-size: 1.4rem; margin: 0 0 10px; }
.start-bow { width: 120px; height: 60px; display: block; margin: 0 auto 4px; }
.pitch { margin: 0 0 20px; color: var(--ink-soft); line-height: 1.4; }
.best { color: var(--ink-soft); margin: 14px 0 4px; }
.best strong { color: var(--ink); }
.start-row { display: flex; gap: 10px; justify-content: center; margin-top: 8px; }

.btn {
  border: 0; border-radius: var(--radius); cursor: pointer;
  padding: 12px 22px; font-weight: 700; font-size: 1rem;
  transition: transform 120ms var(--ease-back), box-shadow 120ms;
  touch-action: manipulation;
}
.btn:active { transform: scale(0.97); }
.btn:focus-visible { outline: 3px solid var(--suture); outline-offset: 2px; }
.btn.primary { background: var(--suture); color: #fff; box-shadow: 0 4px 0 #15408a; min-width: 160px; }
.btn.primary:active { box-shadow: 0 1px 0 #15408a; transform: translateY(3px); }
.btn.ghost { background: transparent; color: var(--ink-soft); padding: 8px 12px; font-weight: 400; text-decoration: underline; text-underline-offset: 3px; }
.btn.icon { background: rgba(255,255,255,0.25); color: var(--paper); width: 36px; height: 36px; padding: 0; border-radius: 50%; font-weight: 700; letter-spacing: -2px; }

/* Intro */
.intro-card { cursor: pointer; }
.intro-level { margin: 0; color: var(--ink-soft); }
.intro-hint { color: var(--ink-soft); font-size: 0.85rem; margin: 18px 0 0; }

/* HUD */
.hud {
  width: 100%; display: flex; justify-content: space-between; align-items: center;
  padding: 10px 16px 6px; color: var(--paper); font-weight: 700; flex: 0 0 auto;
}
.hud-left, .hud-right { display: flex; align-items: center; gap: 12px; }
.hud-score { font-size: 1.25rem; min-width: 3ch; text-align: right; display: inline-block; }
.hud-score.pop { animation: pop 300ms var(--ease-back); }
.hud-streak { font-weight: 400; font-size: 0.9rem; background: var(--suture); padding: 3px 10px; border-radius: 999px; position: relative; }
.hud-streak.pop { animation: pop 200ms var(--ease-back); }
.hud-clock { font-variant-numeric: tabular-nums; font-size: 1.25rem; background: rgba(0,0,0,0.25); padding: 2px 10px; border-radius: 4px; }
.hud-clock.low { color: #FFD08A; animation: clockpulse 1s steps(1) infinite; }
@keyframes clockpulse { 0% { transform: scale(1.12); } 30% { transform: scale(1); } }
@keyframes pop { 0% { transform: scale(1); } 50% { transform: scale(1.3); } 100% { transform: scale(1); } }

/* Boards area: holds one or two boards (L4 slides the second in) */
.boards { position: relative; width: 100%; flex: 1 1 auto; min-height: 0; padding: 0 16px; }
.board {
  position: absolute; inset: 0 16px;
  background: var(--kraft);
  background-image: repeating-linear-gradient(135deg, rgba(255,255,255,0.05) 0 2px, transparent 2px 7px);
  border-radius: 8px;
  box-shadow: 0 14px 30px rgba(0,0,0,0.35), inset 0 0 0 1px rgba(255,255,255,0.25);
  display: flex; flex-direction: column;
  transition: transform 500ms var(--ease-out), opacity 300ms;
  will-change: transform;
}
.board.incoming { transform: translateX(110%); }
.board.outgoing { transform: translateX(-110%); opacity: 0; }
.board.hitstop { transition: none; }
.case {
  margin: 12px 14px 0; padding: 10px 14px;
  background: var(--paper); border-radius: 4px; color: var(--ink);
  font-size: 0.95rem; line-height: 1.35;
  box-shadow: 0 2px 4px rgba(0,0,0,0.15);
  position: relative;
  flex: 0 0 auto;
}
.case p { margin: 0; }
.case p + p { margin-top: 2px; }
.case-toggle { display: none; }
.pinfield { position: relative; flex: 1 1 auto; min-height: 150px; }
.thread { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; }
.thread .seg {
  fill: none; stroke: var(--suture); stroke-width: 3; stroke-linecap: round;
  stroke-dasharray: 7 7; opacity: 0.55;
  transition: opacity 200ms;
}
.thread .seg.hot { opacity: 1; stroke-width: 4; }
.thread .seg.stitched { stroke-dasharray: none; opacity: 1; stroke: var(--teal); stroke-width: 4; }
.thread .bow { opacity: 0; transform-origin: 0 0; }
.thread .bow path { fill: none; stroke: var(--suture); stroke-width: 4; stroke-linecap: round; stroke-linejoin: round; }
.thread .bow circle { fill: var(--teal); }
.thread .bow.shown { opacity: 1; }
.thread .dot { fill: var(--paper); }

.pin {
  position: absolute; transform: translate(-50%, -50%);
  width: var(--card-w); height: 68px; min-width: 56px; min-height: 56px;
  border: 2px dashed var(--pin-ring); border-radius: var(--radius);
  display: flex; align-items: center; justify-content: center;
  background: rgba(255,255,255,0.08);
  transition: border-color 200ms, background 200ms, box-shadow 200ms;
  cursor: pointer;
}
.pin::before {
  /* the pin head */
  content: ""; position: absolute; top: -9px; left: 50%; transform: translateX(-50%);
  width: 14px; height: 14px; border-radius: 50%; background: var(--ink); box-shadow: 0 2px 2px rgba(0,0,0,0.3);
}
.pin-label { font-size: 0.8rem; color: var(--desk-deep); opacity: 0.75; pointer-events: none; }
.pin.hot { border-color: var(--suture); border-style: solid; animation: ring 300ms ease-in-out infinite alternate; }
@keyframes ring { from { box-shadow: 0 0 0 0 rgba(30,91,198,0.25); } to { box-shadow: 0 0 0 6px rgba(30,91,198,0.25); } }
.pin.wrongcol { border-color: #9aa3ad; background: rgba(120,120,120,0.2); }
.pin.filled { border-style: solid; border-color: var(--teal); background: transparent; cursor: default; }
.pin.filled::after { content: "\2713"; position: absolute; right: -8px; top: -8px; width: 22px; height: 22px; border-radius: 50%; background: var(--teal); color: #fff; font-size: 14px; font-weight: 700; display: flex; align-items: center; justify-content: center; }
.pin.glow { border-color: var(--suture); border-style: solid; animation: ring 600ms ease-in-out infinite alternate; }
.pin.keyfocus { outline: 3px solid var(--paper); outline-offset: 3px; }
.pin .card { position: static; transform: none; cursor: default; }
.pin .card.snap { animation: squash 180ms var(--ease-back); }
@keyframes squash { 0% { transform: scale(1.15, 0.85); } 100% { transform: scale(1, 1); } }

/* Tied stamp */
.tied-stamp {
  position: absolute; left: 50%; top: calc(100% - 44px);
  transform: translate(-50%, -50%) rotate(-8deg) scale(0);
  font-size: 2rem; font-weight: 700; color: var(--suture);
  border: 4px solid var(--suture); border-radius: 8px; padding: 2px 18px;
  opacity: 0; pointer-events: none; letter-spacing: 0.04em;
  background: rgba(251,247,239,0.5);
}
.tied-stamp.shown { opacity: 1; transform: translate(-50%, -50%) rotate(-8deg) scale(1); transition: transform 260ms var(--ease-back), opacity 100ms; }

/* Cards */
.card {
  width: var(--card-w); height: 64px;
  background: var(--paper); color: var(--ink);
  border-radius: 4px; padding: 6px 8px;
  font-size: 0.78rem; line-height: 1.2;
  display: flex; align-items: center; justify-content: center; text-align: center;
  box-shadow: 0 1px 2px rgba(0,0,0,0.2), 0 3px 6px rgba(0,0,0,0.12);
  cursor: grab; user-select: none; -webkit-user-select: none;
  touch-action: none;
  position: relative; flex: 0 0 auto; overflow: hidden;
  transition: box-shadow 120ms, transform 120ms var(--ease-back);
  background-image: linear-gradient(transparent 0 20px, rgba(30,91,198,0.08) 20px 21px, transparent 21px);
}
.card.long { font-size: 0.7rem; line-height: 1.15; }
.card:focus-visible { outline: 3px solid var(--suture); outline-offset: 2px; }
.card.lifted, .card.dragging {
  box-shadow: 0 8px 14px rgba(0,0,0,0.3), 0 14px 28px rgba(0,0,0,0.2);
  transform: scale(1.06) rotate(var(--tilt, 0deg));
  z-index: 50;
}
.card.dragging { position: fixed; left: 0; top: 0; margin: 0; pointer-events: none; transition: none; cursor: grabbing; }
.card.returning { transition: transform 300ms var(--ease-out); }
.card.shake { animation: shake 150ms linear 1; }
@keyframes shake { 0%,100% { translate: 0 0; } 25% { translate: -3px 0; } 50% { translate: 3px 0; } 75% { translate: -3px 0; } }
.card .stamp {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  color: var(--amber); font-weight: 700; font-size: 0.9rem; letter-spacing: 0.03em;
  border: 3px solid var(--amber); border-radius: 4px; transform: rotate(-10deg) scale(0.6);
  opacity: 0; background: rgba(251,247,239,0.7); pointer-events: none;
  transition: opacity 120ms, transform 160ms var(--ease-back);
}
.card .stamp.shown { opacity: 1; transform: rotate(-10deg) scale(1); }
.card.glow { box-shadow: 0 0 0 3px var(--suture), 0 6px 14px rgba(30,91,198,0.4); animation: glowpulse 900ms ease-in-out infinite alternate; }
@keyframes glowpulse { from { box-shadow: 0 0 0 2px var(--suture), 0 4px 10px rgba(30,91,198,0.3); } to { box-shadow: 0 0 0 4px var(--suture), 0 8px 18px rgba(30,91,198,0.55); } }
.card.placed { visibility: hidden; }

/* Tray */
.tray {
  flex: 0 0 auto; width: 100%; padding: 10px 16px 12px;
  background: rgba(0,0,0,0.18); border-top: 1px solid rgba(255,255,255,0.1);
  touch-action: none;
}
.tray-tabs { display: none; }
.shelves { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }
.shelf h3 { margin: 0 0 6px; color: var(--paper); font-size: 0.85rem; font-weight: 400; opacity: 0.8; }
.shelf .cards { display: flex; flex-wrap: wrap; gap: 8px; min-height: 64px; align-content: flex-start; }
.shelf[data-col="condition"] .cards { justify-content: center; }
.shelf[data-col="params"] .cards { justify-content: flex-end; }

/* Pause */
.pause-veil { position: absolute; inset: 0; background: rgba(40,25,10,0.65); display: flex; align-items: center; justify-content: center; z-index: 60; }
.pause-veil[hidden] { display: none; }
.pause-card .btn { display: block; margin: 8px auto 0; }

/* Debrief */
.debrief-card { text-align: left; width: min(94%, 560px); max-height: 92dvh; overflow: auto; }
.debrief-card h2 { text-align: center; }
.debrief-lines { list-style: none; margin: 0 0 16px; padding: 0; }
.debrief-lines li { display: grid; grid-template-columns: 26px 1fr; gap: 8px; padding: 7px 0; border-bottom: 1px solid rgba(31,42,54,0.1); font-size: 0.9rem; line-height: 1.3; }
.debrief-lines li i { font-style: normal; font-weight: 700; color: #fff; background: var(--teal); border-radius: 50%; width: 22px; height: 22px; font-size: 0.75rem; display: flex; align-items: center; justify-content: center; }
.debrief-lines li.wrong i { background: var(--amber); }
.debrief-lines li b { display: block; }
.debrief-lines li span { color: var(--ink-soft); }
.debrief-card .btn { display: block; margin: 0 auto; }

/* End */
.end-score { font-size: 3rem; font-weight: 700; margin: 0; line-height: 1; }
.end-score small { font-size: 1rem; color: var(--ink-soft); font-weight: 400; }
.end-levels { list-style: none; padding: 0; margin: 12px 0 18px; color: var(--ink-soft); font-size: 0.9rem; }
.end-levels li { display: flex; justify-content: space-between; padding: 3px 12px; }
.stamp-newbest { position: absolute; top: 14px; right: 16px; transform: rotate(8deg); border: 3px solid var(--teal); color: var(--teal); font-weight: 700; padding: 2px 10px; border-radius: 6px; margin: 0; }
.end-card .btn + .btn { margin-top: 6px; }
.sources-card { text-align: left; max-height: 92dvh; overflow: auto; width: min(94%, 560px); }
.sources-card ul { padding-left: 18px; font-size: 0.9rem; line-height: 1.4; color: var(--ink-soft); }
.sources-card .credits { font-size: 0.85rem; color: var(--ink-soft); }
.sources-card .btn { display: block; margin: 12px auto 0; }

/* Punch dots burst */
.punch { position: absolute; width: 8px; height: 8px; border-radius: 50%; background: var(--paper); pointer-events: none; left: 0; top: 0; }

/* Mobile: vertical bow-tie, tabbed tray */
@media (max-width: 719px) { #stage { --narrow: 1; } }
#stage.narrow { --narrow: 1; }
#stage.is-narrow .case { font-size: 0.78rem; line-height: 1.25; padding: 8px 64px 8px 10px; margin: 10px 10px 0; }
#stage.is-narrow .tray { margin-top: auto; }
#stage.is-narrow .tied-stamp { top: 50%; font-size: 1.6rem; }
#stage.is-narrow .case.collapsed p:not(:first-child) { display: none; }
#stage.is-narrow .case-toggle { display: block; position: absolute; right: 6px; top: 4px; border: 0; background: transparent; color: var(--ink-soft); font-size: 0.8rem; padding: 2px 6px; }
#stage.is-narrow .board { inset: 0 10px; }
#stage.is-narrow .boards { padding: 0 10px; }
#stage.is-narrow .pin, #stage.is-narrow .card { --card-w: 140px; }
#stage.is-narrow .pin { height: 60px; }
#stage.is-narrow .hud { padding: 8px 12px 4px; }
#stage.is-narrow .tray { padding: 6px 10px 10px; }
#stage.is-narrow .tray-tabs { display: flex; gap: 6px; margin-bottom: 8px; }
#stage.is-narrow .tab { flex: 1; border: 0; border-radius: 999px; padding: 6px 0; background: rgba(255,255,255,0.15); color: var(--paper); font-size: 0.85rem; }
#stage.is-narrow .tab[aria-selected="true"] { background: var(--paper); color: var(--ink); font-weight: 700; }
#stage.is-narrow .shelves { display: block; }
#stage.is-narrow .shelf { display: none; }
#stage.is-narrow .shelf.active { display: block; }
#stage.is-narrow .shelf h3 { display: none; }
#stage.is-narrow .shelf .cards { flex-wrap: nowrap; overflow-x: auto; justify-content: flex-start; padding-bottom: 6px; scrollbar-width: thin; touch-action: pan-x; }
#stage.is-narrow .tray { touch-action: pan-x; }
#stage.is-narrow .debrief-card { padding: 14px 12px; max-height: 96dvh; }
#stage.is-narrow .debrief-card h2 { font-size: 1.1rem; }
#stage.is-narrow .debrief-lines li { font-size: 0.8rem; padding: 5px 0; }
#stage.is-narrow h1 { font-size: 1.6rem; }

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  .board, .card, .pin, .screen, .tied-stamp, .btn { transition-duration: 0ms !important; animation: none !important; }
  .card.shake, .card.snap, .hud-score.pop, .hud-streak.pop, .pin.hot, .pin.glow { animation: none !important; }
  .thread .bow { transition: opacity 200ms; }
  .punch { display: none; }
}
#stage.reduced .board, #stage.reduced .card.shake, #stage.reduced .punch { animation: none !important; }
