/* ==========================================================================
   Open Day Puzzle — "The Open Day Imprint"
   A live letterpress programme & rally card for St. Mark's School.
   Palette pinned to the school crest: navy, crimson, gold, paper white.
   ========================================================================== */

:root {
  --paper: #fffefe;
  --ink: #0c2e5e;
  --ink-deep: #081f3e;
  --red: #e50713;
  --red-press: #a3060f;
  --gold: #cbad04;

  --hairline: rgba(12, 46, 94, 0.16);
  --hairline-strong: rgba(12, 46, 94, 0.34);
  --ink-soft: rgba(12, 46, 94, 0.6);

  --display: "Bricolage Grotesque", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  --body: "Archivo", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", system-ui, -apple-system, "Segoe UI", sans-serif;

  --page-w: 620px;
  --cell-gap: 2px;      /* read by app.js for tile slices */
  --board-pad: 0px;     /* read by app.js for tile slices */

  /* ease-out quint: fast start, monotonic deceleration, no overshoot */
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  scrollbar-color: var(--ink) var(--paper);
}

body {
  margin: 0;
  font-family: var(--body);
  background: var(--paper);
  color: var(--ink);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

/* ---- bilingual engine -------------------------------------------------- */

body[data-lang="en"] .tc,
body[data-lang="tc"] .en { display: none !important; }

.en { font-feature-settings: "ss01" on; }

/* ---- browser surfaces -------------------------------------------------- */

::selection { background: var(--red); color: #fff; }
:focus-visible { outline: 3px solid var(--ink); outline-offset: 2px; }
button:focus-visible { outline-offset: 2px; }
.stamp-action:focus-visible,
.lang-btn[aria-pressed="true"]:focus-visible,
.vf-close:focus-visible { outline-color: #fff; }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: var(--paper); }
::-webkit-scrollbar-thumb {
  background: var(--ink);
  border: 3px solid var(--paper);
  border-radius: 8px;
}

/* ---- page scaffold ----------------------------------------------------- */

.page {
  width: min(var(--page-w), 100%);
  margin: 0 auto;
  padding: 20px 18px 56px;
}

/* ---- masthead ---------------------------------------------------------- */

.masthead { position: relative; }

.masthead-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-bottom: 14px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.crest {
  width: auto;
  height: 46px;
  object-fit: contain;
  display: block;
}

.brand-name {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.5em;
  font-family: var(--body);
  font-weight: 700;
  font-size: 0.72rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink);
}

/* language segmented control */

.lang {
  display: flex;
  border: 1.5px solid var(--ink);
  border-radius: 3px;
  flex: none;
}

.lang-btn {
  appearance: none;
  font: 700 0.74rem/1 var(--body);
  letter-spacing: 0.06em;
  color: var(--ink);
  background: var(--paper);
  border: 0;
  border-right: 1px solid var(--hairline);
  padding: 10px 11px;
  cursor: pointer;
  min-height: 38px;
}

.lang-btn:last-child { border-right: 0; }
.lang-btn[aria-pressed="true"] { background: var(--ink); color: #fff; }
.lang-btn:not([aria-pressed="true"]):hover { color: var(--red); }
.lang-btn:not([aria-pressed="true"]):active { color: var(--red); }

/* title block */

.titleblock { padding: 10px 0 2px; }

.title {
  margin: 0;
  font-family: var(--display);
  font-weight: 800;
  font-size: clamp(2.5rem, 11vw, 4rem);
  line-height: 0.98;
  letter-spacing: -0.02em;
  color: var(--ink);
}

.title .en { display: block; }
.title .tc {
  display: block;
  margin-top: 0.28em;
  font-family: var(--body);
  font-weight: 900;
  letter-spacing: 0.02em;
  color: var(--ink);
}

.intro {
  margin: 14px 0 0;
  font-size: 0.99rem;
  line-height: 1.6;
  color: var(--ink-soft);
  max-width: 52ch;
}

.intro .en { display: block; }
.intro .tc { display: block; margin-top: 0.2em; }

/* masthead close rule */

.masthead::after {
  content: "";
  display: block;
  margin-top: 18px;
  border-top: 3px solid var(--ink);
  border-bottom: 1px solid var(--gold);
  position: relative;
  top: 1px;
}

/* ---- progress ledger --------------------------------------------------- */

.ledger {
  padding: 16px 0 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.ledger-line {
  display: flex;
  align-items: baseline;
  gap: 12px;
}

.ledger-label {
  display: flex;
  gap: 0.5em;
  font-family: var(--body);
  font-weight: 700;
  font-size: 0.72rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--ink-soft);
}

.ledger-count {
  margin-left: auto;
  font-family: var(--display);
  font-weight: 800;
  font-size: 1.12rem;
  color: var(--ink);
  display: flex;
  align-items: baseline;
  gap: 0.42em;
}

.ledger-count b { color: var(--red); font-weight: inherit; }

.seals { display: flex; gap: 12px; }

.seal {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-family: var(--display);
  font-weight: 800;
  font-size: 0.95rem;
  line-height: 1;
  color: rgba(12, 46, 94, 0.42);
  border: 2px dashed rgba(12, 46, 94, 0.3);
  background: var(--paper);
  --seal-rot: 0deg;
}

.seal:nth-of-type(2) { --seal-rot: -2deg; }
.seal:nth-of-type(3) { --seal-rot: 1.5deg; }
.seal:nth-of-type(4) { --seal-rot: -1deg; }
.seal:nth-of-type(5) { --seal-rot: 2deg; }

.seal--set {
  background: var(--red);
  border: 2px solid var(--red);
  color: #fff;
  box-shadow:
    0 1px 0 rgba(8, 31, 62, 0.9),
    0 6px 14px rgba(0, 0, 0, 0.18);
  animation: seal-slam 0.45s var(--ease-out) both;
}

.seal:nth-of-type(2).seal--set { animation-delay: 0.04s; }
.seal:nth-of-type(3).seal--set { animation-delay: 0.08s; }
.seal:nth-of-type(4).seal--set { animation-delay: 0.12s; }
.seal:nth-of-type(5).seal--set { animation-delay: 0.16s; }

@keyframes seal-slam {
  0%   { transform: scale(0) rotate(-16deg); }
  70%  { transform: scale(1.06) rotate(0.75deg); }
  100% { transform: scale(1) rotate(var(--seal-rot, 0deg)); }
}

/* ---- the SCAN stamp ---------------------------------------------------- */

.stamp {
  appearance: none;
  font-family: var(--display);
  font-weight: 800;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.stamp-action {
  display: block;
  width: 100%;
  padding: 16px 20px 15px;
  border: 0;
  border-radius: 4px;
  background: var(--red);
  color: #fff;
  text-align: center;
  box-shadow:
    0 2px 0 var(--red-press),
    0 10px 22px rgba(0, 0, 0, 0.18);
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}

.stamp-action .en {
  display: block;
  font-size: 1.12rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  line-height: 1.15;
}

.stamp-action .tc {
  display: block;
  margin-top: 1px;
  font-family: var(--body);
  font-weight: 800;
  font-size: 0.98rem;
  letter-spacing: 0.05em;
  line-height: 1.3;
}

.stamp-action:hover { background: #f5131f; }
.stamp-action:active {
  transform: translateY(2px);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.16), inset 0 2px 7px rgba(8, 31, 62, 0.32);
}

#done .stamp-action {
  box-shadow: 0 2px 0 var(--red-press), 0 12px 24px rgba(0, 0, 0, 0.3);
}

/* ---- the picture plate ------------------------------------------------- */

.plate { padding-bottom: 2px; }

.plate-ear {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  padding: 0 2px 9px;
}

.plate-title {
  display: flex;
  gap: 0.5em;
  font-family: var(--body);
  font-weight: 700;
  font-size: 0.74rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--ink);
}

.plate-meta {
  font-family: var(--display);
  font-weight: 700;
  font-size: 0.72rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink-soft);
  white-space: nowrap;
}

.plate-meta .en + .tc::before { content: " · "; opacity: 0.55; }

.plate-frame {
  position: relative;
  background: var(--ink);
  padding: 3px;
}

.plate-frame::before {
  content: "";
  position: absolute;
  inset: 0;
  border: 1px solid rgba(203, 173, 4, 0.5);
  z-index: 2;
  pointer-events: none;
}

#board {
  display: grid;
  gap: var(--cell-gap);
  background: var(--paper);
}

/* register ticks */

.tick {
  position: absolute;
  width: 18px;
  height: 18px;
  border: 0 solid var(--red);
  z-index: 3;
}
.tick-tl { top: -4px; left: -4px; border-top-width: 2px; border-left-width: 2px; }
.tick-tr { top: -4px; right: -4px; border-top-width: 2px; border-right-width: 2px; }
.tick-bl { bottom: -4px; left: -4px; border-bottom-width: 2px; border-left-width: 2px; }
.tick-br { bottom: -4px; right: -4px; border-bottom-width: 2px; border-right-width: 2px; }

/* pieces */

.piece {
  position: relative;
  aspect-ratio: var(--tile-aspect, 4 / 3);
  background: var(--paper);
}

.piece .lock {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  user-select: none;
  background: var(--paper);
}

.piece .lock::before {
  content: "";
  position: absolute;
  inset: 8px;
  border: 1.5px dashed rgba(229, 7, 19, 0.5);
}

.piece .lock .num {
  font-family: var(--display);
  font-weight: 800;
  color: var(--red);
  font-size: clamp(1.3rem, 8.5vw, 2.1rem);
  line-height: 1;
}

.piece .lock .cap {
  display: flex;
  gap: 0.4em;
  text-align: center;
  font-family: var(--body);
  font-weight: 700;
  font-size: 0.7rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--ink-soft);
}

.piece .lock .cap .hint {
  display: block;
  font-weight: 400;
  letter-spacing: 0.04em;
  text-transform: none;
  opacity: 0.7;
  margin-left: 0;
}

.piece .img {
  position: absolute;
  inset: 0;
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center center;
}

/* locked tiles drop the image from compositing entirely; the paper lock
   cover owns the cell. The reveal plays a driven "press the image in" clip. */

.piece:not(.unlocked) .img { display: none; }

.piece.unlocked.just-revealed .img {
  animation: piece-in 0.42s var(--ease-out) both;
}

@keyframes piece-in {
  from { opacity: 0; transform: translateY(5px) scale(0.94); }
  to   { opacity: 1; transform: none; }
}

.piece.unlocked .lock { display: none; }



/* newly revealed rim flash */

.piece.just-revealed::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 6;
  box-shadow: inset 0 0 0 2px var(--red);
  animation: reveal-rim 1.5s ease-out both;
  pointer-events: none;
}

@keyframes reveal-rim {
  0%   { opacity: 0; }
  14%  { opacity: 1; }
  100% { opacity: 0; }
}

/* ---- footer / reset ---------------------------------------------------- */

.foot { margin-top: 26px; padding-top: 14px; border-top: 2px double rgba(12, 46, 94, 0.22); }

.reset {
  display: block;
  margin: 0 auto;
  background: transparent;
  border: 0;
  border-bottom: 1px dashed rgba(12, 46, 94, 0.45);
  color: var(--ink-soft);
  font-family: var(--body);
  font-weight: 600;
  font-size: 0.78rem;
  letter-spacing: 0.05em;
  line-height: 1.6;
  padding: 2px 2px 6px;
  cursor: pointer;
}

.reset:hover { color: var(--red); border-bottom-color: var(--red); }

/* ---- viewfinder (camera overlay) -------------------------------------- */

.viewfinder {
  position: fixed;
  inset: 0;
  z-index: 35;
  background: #000;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.viewfinder.show { display: flex; }

.viewfinder #cam-video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #000;
}

.vf-trace {
  position: relative;
  width: min(58vw, 240px);
  aspect-ratio: 1;
}

.vf-trace .tick { border-color: rgba(255, 255, 255, 0.85); }

.vf-scanline {
  position: absolute;
  left: 7px;
  right: 7px;
  top: 9%;
  height: 3px;
  background: var(--red);
  animation: vf-scan 2.6s ease-in-out infinite;
}

@keyframes vf-scan {
  0%, 100% { top: 9%; }
  50% { top: 88%; }
}

.cam-msg {
  margin: 22px 0 0;
  max-width: 40ch;
  color: #fff;
  text-align: center;
  font-size: 0.95rem;
  line-height: 1.55;
  opacity: 0.92;
}

.cam-msg .en { display: block; }
.cam-msg .tc { display: block; margin-top: 0.2em; }

.vf-close {
  margin-top: 20px;
  background: transparent;
  border: 0;
  color: #fff;
  font-family: var(--body);
  font-weight: 600;
  font-size: 0.92rem;
  letter-spacing: 0.05em;
  padding: 4px 2px 7px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.55);
  cursor: pointer;
}

.vf-close:hover { color: #ffe9ea; border-bottom-color: #fff; }

.vf-close .en + .tc::before { content: " · "; opacity: 0.7; }

/* ---- stamp-slip toast -------------------------------------------------- */

#toast {
  position: fixed;
  top: 18px;
  left: 50%;
  width: min(400px, calc(100vw - 32px));
  z-index: 30;
  transform: translateX(-50%);
  pointer-events: none;
}

.toast-slip {
  position: relative;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 13px 16px 13px 13px;
  border-radius: 4px;
  transform: translateY(calc(-100% - 26px));
  transition: transform 0.46s var(--ease-out);
}

#toast.show .toast-slip { transform: none; }

/* the slip's paper, per state: reverse-paper for success/info, paper for warn */

#toast.ok .toast-slip {
  background: var(--red);
  color: #fff;
  box-shadow: 0 2px 0 var(--red-press), 0 18px 38px rgba(0, 0, 0, 0.24);
}

#toast.info .toast-slip {
  background: var(--ink);
  color: #fff;
  box-shadow: 0 2px 0 var(--ink-deep), 0 18px 38px rgba(0, 0, 0, 0.24);
}

#toast.warn .toast-slip {
  background: var(--paper);
  color: var(--red);
  border: 1.5px dashed var(--red);
  box-shadow: 0 2px 0 rgba(8, 31, 62, 0.35), 0 18px 38px rgba(0, 0, 0, 0.24);
}

/* drawn seal: a paper stamp with the state's ink, hit slightly off-square */

.slip-seal {
  flex: none;
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  background: var(--paper);
  border: 1.5px solid;
  border-radius: 4px;
  box-shadow: inset 0 -2px 0 rgba(8, 31, 62, 0.1);
}

.slip-seal svg { width: 24px; height: 24px; display: block; }

#toast.ok .slip-seal {
  color: var(--red);
  border-color: var(--red);
  transform: rotate(-3deg);
}

#toast.info .slip-seal {
  color: var(--ink);
  border-color: var(--ink);
  transform: rotate(2deg);
}

#toast.warn .slip-seal {
  background: transparent;
  color: var(--red);
  border: 1.5px dashed var(--red);
  transform: rotate(4deg);
}

/* body + count */

.slip-body { min-width: 0; }

.slip-txt .en,
.slip-txt .tc { display: block; }

.slip-txt .en {
  font-weight: 700;
  font-size: 0.98rem;
  letter-spacing: 0.035em;
  line-height: 1.25;
}

.slip-txt .tc {
  margin-top: 2px;
  font-weight: 700;
  font-size: 0.94rem;
  line-height: 1.3;
}

.slip-count {
  margin-top: 7px;
  display: flex;
  align-items: baseline;
  gap: 0.6em;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.slip-count b { font-weight: inherit; }

#toast.ok .slip-count,
#toast.info .slip-count { color: rgba(255, 255, 255, 0.85); }
#toast.warn .slip-count { color: var(--ink-soft); }

/* corner ticks: the slip lands already crop-marked */

.slip-tick {
  position: absolute;
  width: 12px;
  height: 12px;
  border: 0 solid currentColor;
  color: rgba(255, 255, 255, 0.5);
}
.slip-tick-tl { top: 5px; left: 5px; border-top-width: 2px; border-left-width: 2px; }
.slip-tick-br { bottom: 5px; right: 5px; border-bottom-width: 2px; border-right-width: 2px; }

#toast.warn .slip-tick { color: var(--red); }

/* ---- completion overlay ------------------------------------------------ */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(8, 31, 62, 0.95);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.overlay.show { display: flex; }

.certificate {
  position: relative;
  width: min(520px, 100%);
  max-height: calc(100vh - 40px);
  overflow-y: auto;
  background: var(--paper);
  padding: 26px 24px 24px;
  border-radius: 4px;
  text-align: center;
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.6), 0 30px 70px rgba(0, 0, 0, 0.55);
  animation: cert-in 0.5s var(--ease-out) both;
}

.certificate::before {
  content: "";
  position: absolute;
  inset: 8px;
  border: 1px solid rgba(12, 46, 94, 0.35);
  pointer-events: none;
}

@keyframes cert-in {
  from { transform: translateY(16px) scale(0.965); opacity: 0; }
  to   { transform: none; opacity: 1; }
}

.cert-brand { display: flex; justify-content: center; }
.cert-brand .crest { height: 34px; opacity: 0.95; }

.cert-seal {
  display: inline-block;
  margin: 14px auto 4px;
  padding: 9px 18px 8px;
  background: var(--red);
  color: #fff;
  border-radius: 4px;
  transform: rotate(-3deg);
  box-shadow: 0 2px 0 var(--red-press), 0 10px 20px rgba(0, 0, 0, 0.3);
}

.cert-seal .en {
  display: block;
  font-family: var(--display);
  font-weight: 800;
  font-size: 1.18rem;
  letter-spacing: 0.24em;
  text-transform: uppercase;
  line-height: 1.05;
}

.cert-seal .tc {
  display: block;
  font-family: var(--body);
  font-weight: 800;
  font-size: 1rem;
  letter-spacing: 0.12em;
  line-height: 1.2;
}

.cert-plate {
  position: relative;
  margin: 18px auto 0;
  background: var(--ink);
  padding: 3px;
}

.cert-plate::after {
  content: "";
  position: absolute;
  inset: 0;
  border: 1px solid rgba(203, 173, 4, 0.5);
  pointer-events: none;
}

.cert-plate img {
  display: block;
  width: 100%;
  object-fit: cover;
}

.certificate .big {
  margin: 18px 0 2px;
  font-family: var(--display);
  font-weight: 800;
  font-size: 1.5rem;
  letter-spacing: -0.01em;
  line-height: 1.1;
  color: var(--ink);
}

.certificate .big .en { display: block; }
.certificate .big .tc { display: block; margin-top: 0.15em; }

.certificate .txt {
  margin: 4px 0 0;
  color: var(--ink-soft);
  font-size: 1.02rem;
}

.certificate .txt .en { display: block; }
.certificate .txt .tc { display: block; margin-top: 0.15em; }

.voucher { margin-top: 18px; padding-top: 15px; border-top: 1px dashed rgba(12, 46, 94, 0.4); }

.voucher-rule {
  margin: 0 0 13px;
  border-top: 3px double rgba(12, 46, 94, 0.24);
}

.prize-head {
  display: flex;
  gap: 0.5em;
  justify-content: center;
  font-family: var(--body);
  font-weight: 700;
  font-size: 0.78rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--ink-soft);
}

.prize-name {
  margin: 6px 0 5px;
  font-family: var(--display);
  font-weight: 800;
  font-size: 1.18rem;
  color: var(--ink);
  display: flex;
  justify-content: center;
  gap: 0.5em;
}

.go-prize {
  margin: 0 auto;
  max-width: 40ch;
  color: var(--ink-soft);
  font-size: 0.93rem;
  line-height: 1.55;
}

.go-prize .en { display: block; }
.go-prize .tc { display: block; margin-top: 0.15em; }

.certificate .stamp-action { margin-top: 20px; }

/* ---- narrow phones ----------------------------------------------------- */

@media (max-width: 380px) {
  .page { padding: 16px 14px 48px; }
  .brand-name { display: none; }
  .crest { height: 40px; }
  .seal { width: 31px; height: 31px; font-size: 0.86rem; }
  .seals { gap: 9px; }
  .piece .lock::before { inset: 6px; }
  .toast-slip { padding: 12px 14px 12px 12px; gap: 12px; }
  .slip-seal { width: 38px; height: 38px; }
}

/* ---- desktop ----------------------------------------------------------- */

@media (min-width: 760px) {
  .page { padding-top: 40px; padding-bottom: 80px; }
  .ledger { flex-direction: row; align-items: center; }
  .ledger-line { flex: 1; }
  .ledger-count { margin-left: 14px; }
  .seals { gap: 14px; }
  .stamp-action { padding: 18px 24px 17px; }
  .crest { height: 52px; }
}

/* ---- motion preference ------------------------------------------------- */

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001s !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001s !important;
  }
  .vf-scanline { animation: none; top: 45%; }
}