:root {
  --ink: #2a1f3d;
  --ink-2: #4a3d63;
  --ink-3: #7a6d92;
  --paper: #fff6e6;
  --paper-2: #f6e7cc;
  --paper-3: #e7d1a8;
  --coral: #ff6b4a;
  --sun: #ffc94a;
  --leaf: #3dbb7d;
  --leaf-2: #2a9a62;
  --night: #1b1f3b;
  --violet: #8a5cf6;
  --round: ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", sans-serif;
  --hand: "Noteworthy", "Bradley Hand", "Marker Felt", "Chalkboard SE", cursive;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --slot: 78px;
  --slot-small: 60px;
  --side: 200px;
  --pix: 3px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; overscroll-behavior: none; background: var(--night); }
body {
  position: fixed;
  inset: 0;
  font-family: var(--round);
  color: var(--ink);
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
}
#app, .screen { position: absolute; inset: 0; }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
canvas { image-rendering: pixelated; }

.icon-btn {
  flex: none;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 3px solid var(--ink);
  border-radius: 14px;
  background: var(--paper);
  box-shadow: 0 var(--pix) 0 var(--ink);
}
.icon-btn svg { width: 20px; height: 20px; }
.icon-btn .icon { display: grid; place-items: center; }
.icon-btn:active, .btn:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--ink); }

/* Album */
.album {
  overflow-y: auto;
  touch-action: pan-y;
  padding: calc(var(--safe-t) + 16px) calc(var(--safe-r) + 16px) calc(var(--safe-b) + 28px) calc(var(--safe-l) + 16px);
  background:
    radial-gradient(circle at 1px 1px, rgba(42, 31, 61, .09) 1px, transparent 1.6px) 0 0 / 14px 14px,
    linear-gradient(#fff3dc, #f3ddb8);
}
.album-head, .tagline, .postcards, .album-foot { max-width: 940px; margin-left: auto; margin-right: auto; }
.album-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.logo { display: flex; align-items: center; gap: 10px; margin: 0; font-size: 30px; font-weight: 900; letter-spacing: -.5px; line-height: 1; }
.logo em { font-style: normal; font-size: .62em; color: var(--coral); vertical-align: middle; }
.logo-mark { width: 38px; height: 38px; flex: none; }
.logo-mark svg { width: 100%; height: 100%; display: block; }
.tagline { margin-top: 8px; margin-bottom: 18px; font-size: 15px; font-weight: 650; color: var(--ink-2); }
.album-note {
  max-width: 940px;
  margin: -6px auto 16px;
  padding: 10px 14px;
  border: 3px solid var(--ink);
  border-radius: 14px;
  background: var(--sun);
  box-shadow: 0 var(--pix) 0 var(--ink);
  font-size: 14px;
  font-weight: 800;
}
.postcards { display: grid; grid-template-columns: repeat(auto-fill, minmax(158px, 1fr)); gap: 20px 14px; }
.album-foot { margin-top: 26px; text-align: center; font-size: 12px; font-weight: 800; letter-spacing: .12em; text-transform: uppercase; color: var(--ink-3); }

.postcard {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px 8px 10px;
  border: 3px solid var(--ink);
  border-radius: 12px;
  background: #fffdf7;
  box-shadow: 0 4px 0 var(--ink);
  text-align: left;
  transform: rotate(var(--tilt, 0deg));
  transition: transform .15s ease;
}
.postcard:nth-child(4n+1) { --tilt: -1.3deg; }
.postcard:nth-child(4n+2) { --tilt: 1.1deg; }
.postcard:nth-child(4n+3) { --tilt: .7deg; }
.postcard:nth-child(4n) { --tilt: -.8deg; }
.postcard:active { transform: rotate(0deg) translateY(2px) scale(.98); }
.pc-photo { position: relative; aspect-ratio: 2 / 3; overflow: hidden; border: 2px solid var(--ink); border-radius: 6px; background: var(--night); }
.pc-photo canvas { width: 100%; height: 100%; display: block; }
.pc-caption { display: flex; flex-direction: column; gap: 3px; padding: 0 2px; }
.pc-caption strong { font-family: var(--hand); font-size: 19px; line-height: 1.1; }
.pc-caption span { font-size: 11px; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; color: var(--ink-3); }
.pc-bar { height: 10px; overflow: hidden; border: 2px solid var(--ink); border-radius: 6px; background: var(--paper-2); }
.pc-bar i { display: block; height: 100%; background: repeating-linear-gradient(90deg, var(--leaf) 0 8px, var(--leaf-2) 8px 10px); }
.pc-tiers { position: absolute; top: 6px; left: 6px; display: flex; gap: 3px; }
.pc-tier {
  width: 20px;
  height: 20px;
  display: grid;
  place-items: center;
  border: 2px solid var(--ink);
  border-radius: 6px;
  background: rgba(255, 246, 230, .85);
  color: var(--ink-3);
  font-size: 11px;
  font-weight: 900;
}
.pc-tier.on { color: #fff; }
.pc-tier.on.tier-normal { background: var(--leaf); }
.pc-tier.on.tier-hard { background: var(--coral); }
.pc-tier.on.tier-expert { background: var(--violet); }
.pc-tier.on.tier-master { background: var(--ink); color: var(--sun); }
.pc-tier.perfect { box-shadow: 0 0 0 2px var(--sun); }
.postcard[data-state="locked"] .pc-tiers { display: none; }
.postcard[data-state="locked"] .pc-photo canvas { filter: grayscale(1) blur(4px) brightness(.55); opacity: .5; }
.pc-lock { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; padding: 12px; text-align: center; color: var(--paper); font-size: 13px; font-weight: 800; line-height: 1.25; }
.pc-lock svg { width: 34px; height: 34px; filter: drop-shadow(0 3px 0 rgba(0, 0, 0, .35)); }
.postmark {
  position: absolute;
  right: 8px;
  bottom: 12px;
  padding: 5px 10px;
  border: 3px double currentColor;
  border-radius: 999px;
  color: #d9452a;
  background: rgba(255, 246, 230, .85);
  font-size: 12px;
  font-weight: 900;
  letter-spacing: .14em;
  text-transform: uppercase;
  transform: rotate(-13deg);
}

/* Play */
.play { background: var(--night); touch-action: none; }
#scene { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
.play-top {
  position: absolute;
  z-index: 5;
  top: calc(var(--safe-t) + 8px);
  left: calc(var(--safe-l) + 10px);
  right: calc(var(--safe-r) + 10px);
  display: flex;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}
.play-top > * { pointer-events: auto; }
.play-title {
  flex: 1;
  min-width: 0;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 0 12px;
  border: 3px solid var(--ink);
  border-radius: 14px;
  background: var(--paper);
  box-shadow: 0 var(--pix) 0 var(--ink);
}
.play-title strong { overflow: hidden; font-family: var(--hand); font-size: 19px; white-space: nowrap; text-overflow: ellipsis; }
.play-title span { font-size: 14px; font-weight: 850; color: var(--ink-2); white-space: nowrap; }

.tray {
  position: absolute;
  z-index: 6;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px calc(var(--safe-r) + 12px) calc(var(--safe-b) + 10px) calc(var(--safe-l) + 12px);
  border-top: 3px solid var(--ink);
  background: linear-gradient(#fbefd8, #f0dcb8);
  box-shadow: 0 -4px 0 rgba(27, 31, 59, .25);
}
.tray-head { display: flex; align-items: center; gap: 10px; min-height: 40px; }
.tray-label { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.tray-label small { font-size: 11px; font-weight: 850; letter-spacing: .12em; text-transform: uppercase; color: var(--ink-3); }
.tray-label strong { overflow: hidden; font-size: 18px; font-weight: 900; white-space: nowrap; text-overflow: ellipsis; }
.pips { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 4px; max-width: 46%; }
.pips i { width: 11px; height: 11px; border: 2px solid var(--ink); border-radius: 3px; background: var(--paper); transition: background .2s, transform .2s; }
.pips i.on { background: var(--leaf); transform: scale(1.12); }
.pips b { font-size: 15px; font-weight: 900; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.play[data-tier="hard"] #tray-kicker { color: #d9452a; }
.play[data-tier="expert"] #tray-kicker { color: var(--violet); }
.play[data-tier="master"] #tray-kicker { color: #b07d00; }
.hint-btn { position: relative; width: 42px; height: 42px; background: var(--sun); }
.hint-btn.spent { background: var(--paper-3); color: var(--ink-3); }
.hint-count {
  position: absolute;
  top: -9px;
  right: -9px;
  min-width: 21px;
  height: 21px;
  padding: 0 5px;
  border: 2px solid var(--ink);
  border-radius: 999px;
  background: var(--coral);
  color: #fff;
  font-size: 12px;
  font-weight: 900;
  line-height: 17px;
  text-align: center;
}
.hint-btn.spent .hint-count { background: var(--ink-3); }
.hint-btn svg { width: 22px; height: 22px; }

.slots {
  display: flex;
  gap: 10px;
  min-height: calc(var(--slot) + 10px);
  padding: 2px 2px 7px;
  overflow-x: auto;
  overflow-y: hidden;
  overscroll-behavior: contain;
  touch-action: pan-x;
  scrollbar-width: none;
}
.slots::-webkit-scrollbar { display: none; }
.slot {
  flex: 0 0 var(--slot);
  width: var(--slot);
  height: var(--slot);
  display: grid;
  place-items: center;
  border: 3px solid var(--ink);
  border-radius: 14px;
  background: #fffaf0;
  box-shadow: 0 var(--pix) 0 var(--ink);
  touch-action: pan-x;
  transition: flex-basis .22s ease, width .22s ease, margin .22s ease, opacity .22s ease, transform .22s ease;
}
.slot canvas { width: 100%; height: 100%; display: block; pointer-events: none; transition: transform .25s cubic-bezier(.2, 1.4, .4, 1); }
.slot.lifted { border-style: dashed; background: rgba(255, 250, 240, .4); box-shadow: none; }
.slot.lifted canvas { opacity: 0; }
.slot.gone { flex-basis: 0; width: 0; margin-right: -10px; border-width: 0; opacity: 0; transform: scale(.4); }
.slot.enter { animation: slot-in .4s cubic-bezier(.2, 1.4, .4, 1) both; animation-delay: var(--d, 0s); }
.slot.wiggle { animation: wiggle .45s ease 3; }
.slot.wiggle { border-color: #e2512f; }
.empty-note { align-self: center; font-weight: 800; color: var(--ink-3); }
.tray.many .slots {
  display: grid;
  grid-auto-flow: column;
  grid-template-rows: repeat(2, var(--slot-small));
  grid-auto-columns: var(--slot-small);
  gap: 8px;
  min-height: calc(var(--slot-small) * 2 + 16px);
}
.tray.many .slot { width: var(--slot-small); height: var(--slot-small); border-radius: 12px; }
.tray.many .slot.gone { width: var(--slot-small); margin: 0; }

.toast {
  position: absolute;
  z-index: 8;
  top: calc(var(--safe-t) + 66px);
  left: 16px;
  right: 16px;
  width: fit-content;
  max-width: 360px;
  margin: 0 auto;
  padding: 9px 16px 10px;
  border: 3px solid var(--ink);
  border-radius: 14px;
  background: var(--sun);
  box-shadow: 0 var(--pix) 0 var(--ink);
  font-size: 16px;
  font-weight: 900;
  text-align: center;
  animation: toast-in .45s cubic-bezier(.2, 1.5, .4, 1);
  pointer-events: none;
}
.toast small { display: block; font-size: 12px; font-weight: 800; color: var(--ink-2); }
.toast.soft { background: var(--paper); }

.drag-piece {
  position: fixed;
  z-index: 20;
  left: 0;
  top: 0;
  pointer-events: none;
  transform-origin: 50% 50%;
  filter: drop-shadow(0 7px 0 rgba(27, 31, 59, .3));
  will-change: transform;
}

/* Sheets */
.sheet {
  position: fixed;
  z-index: 30;
  inset: 0;
  display: grid;
  place-items: end center;
  padding: 16px calc(var(--safe-r) + 12px) calc(var(--safe-b) + 16px) calc(var(--safe-l) + 12px);
  background: rgba(27, 31, 59, .45);
  animation: fade .2s ease;
}
.sheet-card {
  width: min(420px, 100%);
  padding: 18px;
  border: 3px solid var(--ink);
  border-radius: 20px;
  background: var(--paper);
  box-shadow: 0 5px 0 var(--ink);
  animation: rise .3s cubic-bezier(.2, 1.2, .4, 1);
}
.sheet-card h2 { margin: 0 0 6px; font-size: 22px; font-weight: 900; }
.sheet-card p { margin: 0; font-weight: 650; color: var(--ink-2); }
.toggle { position: relative; display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 13px 0; border-bottom: 2px dashed var(--paper-3); font-size: 16px; font-weight: 850; cursor: pointer; }
.toggle small { display: block; margin-top: 2px; font-size: 12px; font-weight: 650; color: var(--ink-3); }
.toggle input { position: absolute; opacity: 0; pointer-events: none; }
.toggle i { position: relative; flex: none; width: 54px; height: 32px; border: 3px solid var(--ink); border-radius: 999px; background: var(--paper-3); transition: background .15s; }
.toggle i::after { content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border: 2px solid var(--ink); border-radius: 50%; background: #fff; transition: transform .18s cubic-bezier(.2, 1.4, .4, 1); }
.toggle input:checked + i { background: var(--leaf); }
.toggle input:checked + i::after { transform: translateX(22px); }
.toggle input:focus-visible + i { outline: 3px solid var(--coral); outline-offset: 2px; }
.sheet-actions { display: flex; gap: 10px; margin-top: 18px; }
.btn {
  flex: 1;
  min-height: 48px;
  padding: 0 12px;
  border: 3px solid var(--ink);
  border-radius: 14px;
  background: var(--coral);
  color: #fff;
  box-shadow: 0 var(--pix) 0 var(--ink);
  font-size: 16px;
  font-weight: 900;
  text-shadow: 0 2px 0 rgba(42, 31, 61, .3);
}
.btn.ghost { background: var(--paper); color: var(--ink); text-shadow: none; }
.btn.danger { background: #d9433b; }
.link-btn {
  display: block;
  margin: 12px auto 0;
  padding: 6px 8px;
  border: 0;
  background: none;
  color: #d9452a;
  font-size: 14px;
  font-weight: 900;
  text-decoration: underline;
  text-underline-offset: 3px;
}
.tier-list { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
.tier-row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 12px;
  border: 3px solid var(--ink);
  border-radius: 16px;
  background: #fffdf7;
  box-shadow: 0 var(--pix) 0 var(--ink);
  text-align: left;
}
.tier-row.last { background: #fff1d0; }
.tier-row:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--ink); }
.tier-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 5px; }
.tier-head { display: flex; align-items: center; gap: 8px; }
.tier-head strong { font-size: 15px; font-weight: 900; }
.tier-body small { font-size: 12px; font-weight: 650; line-height: 1.3; color: var(--ink-3); }
.tier-badge {
  padding: 2px 9px;
  border: 2px solid var(--ink);
  border-radius: 999px;
  color: #fff;
  font-size: 11px;
  font-weight: 900;
  letter-spacing: .08em;
  text-transform: uppercase;
}
.tier-normal .tier-badge { background: var(--leaf); }
.tier-hard .tier-badge { background: var(--coral); }
.tier-expert .tier-badge { background: var(--violet); }
.tier-master .tier-badge { background: var(--ink); color: var(--sun); }
.tier-status { flex: none; font-size: 13px; font-weight: 900; color: var(--ink-2); }

.finale {
  position: fixed;
  z-index: 25;
  inset: 0;
  display: grid;
  place-items: center;
  padding: calc(var(--safe-t) + 16px) 16px calc(var(--safe-b) + 16px);
  background: rgba(27, 31, 59, .55);
  animation: fade .35s ease;
}
.finale-card {
  width: min(360px, 100%);
  padding: 16px 16px 18px;
  border: 3px solid var(--ink);
  border-radius: 18px;
  background: #fffdf7;
  box-shadow: 0 6px 0 var(--ink);
  text-align: center;
  transform: rotate(-1.4deg);
  animation: pop .55s cubic-bezier(.2, 1.4, .4, 1);
}
.finale-kicker { margin: 0 0 10px; font-size: 13px; font-weight: 900; letter-spacing: .16em; text-transform: uppercase; color: var(--coral); }
.finale-photo { position: relative; height: min(40dvh, 330px); aspect-ratio: 2 / 3; margin: 0 auto; overflow: hidden; border: 3px solid var(--ink); border-radius: 8px; background: var(--night); }
.finale-photo canvas { width: 100%; height: 100%; display: block; }
.finale-card h2 { margin: 12px 0 4px; font-family: var(--hand); font-size: 27px; }
.finale-card p { margin: 0; font-weight: 700; color: var(--ink-2); }

@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(40px); opacity: 0; } }
@keyframes pop { from { transform: rotate(-1.4deg) scale(.6); opacity: 0; } }
@keyframes toast-in { from { transform: translateY(-14px) scale(.7); opacity: 0; } }
@keyframes slot-in { from { transform: translateY(46px) scale(.6); opacity: 0; } }
@keyframes wiggle {
  0%, 100% { transform: rotate(0); }
  25% { transform: rotate(-7deg) translateY(-4px); }
  75% { transform: rotate(7deg) translateY(-4px); }
}

/* Unfolded / landscape: the tray becomes a side column */
@media (min-aspect-ratio: 1/1) and (min-width: 640px) {
  .play-top { right: calc(var(--side) + var(--safe-r) + 10px); }
  .toast { right: calc(var(--side) + var(--safe-r) + 16px); }
  .tray {
    top: 0;
    left: auto;
    width: calc(var(--side) + var(--safe-r));
    padding: calc(var(--safe-t) + 12px) calc(var(--safe-r) + 12px) calc(var(--safe-b) + 12px) 12px;
    border-top: 0;
    border-left: 3px solid var(--ink);
    box-shadow: -4px 0 0 rgba(27, 31, 59, .25);
  }
  .tray-head { flex-wrap: wrap; }
  .tray-label strong { font-size: 16px; line-height: 1.15; white-space: normal; }
  .pips { order: 3; flex-basis: 100%; max-width: none; justify-content: flex-start; }
  .slots {
    flex: 1;
    display: grid;
    grid-template-columns: repeat(2, var(--slot));
    grid-auto-rows: var(--slot);
    align-content: start;
    justify-content: space-between;
    gap: 12px;
    overflow-x: hidden;
    overflow-y: auto;
    touch-action: pan-y;
  }
  .slot { touch-action: pan-y; }
  .slot.gone { width: var(--slot); margin: 0; }
  .tray.many { --slot-small: 50px; }
  .tray.many .slots {
    grid-auto-flow: row;
    gap: 8px;
    grid-template-columns: repeat(3, var(--slot-small));
    grid-template-rows: none;
    grid-auto-rows: var(--slot-small);
    grid-auto-columns: auto;
    min-height: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
