:root {
  --bg: #0d0406; --panel: #1a0a0e; --ink: #ffeadb; --muted: #c79a8a;
  --ember: #ff4a1c; --ember-dark: #b8300f; --gold: #ffb347; --crimson: #b3122d; --blue: #3aa0ff;
  --flip: .9s cubic-bezier(.45, .05, .25, 1);
}
* { box-sizing: border-box; }
body {
  margin: 0; min-height: 100vh; color: var(--ink); background: var(--bg);
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
}
/* blurred copy of the cover as the page backdrop */
body::before {
  content: ""; position: fixed; inset: -20px; z-index: -1;
  background: linear-gradient(#0d0406cc, #0d0406ee), url("cover.png") center / cover;
  filter: blur(18px) saturate(1.2);
}
#confetti { position: fixed; inset: 0; pointer-events: none; z-index: 60; }
.wrap { max-width: 1000px; margin: 0 auto; padding: 0 16px 40px; }

.banner { background: #000; color: #fff; text-align: center; font-size: 13px; padding: 8px 12px; border-bottom: 1px solid #3a1a1f; }
.banner b { color: var(--gold); }

button { font: inherit; cursor: pointer; }
.btn {
  background: var(--ember); color: #fff; border: 0; border-radius: 999px;
  padding: 12px 24px; font-weight: 700; font-size: 17px; box-shadow: 0 5px 0 var(--ember-dark), 0 0 24px #ff4a1c66;
  transition: transform .08s, box-shadow .08s;
}
.btn:hover { transform: translateY(2px); box-shadow: 0 3px 0 var(--ember-dark), 0 0 24px #ff4a1c88; }
.btn:active { transform: translateY(5px); box-shadow: 0 0 0 var(--ember-dark); }
.btn.ghost { background: #2a1217; color: var(--ink); box-shadow: 0 5px 0 #12060a; border: 1px solid #5a2a30; }
.btn.small { padding: 8px 16px; font-size: 14px; box-shadow: none; }
.btn:disabled { opacity: .35; cursor: default; transform: none; }

/* ---------- the book ---------- */
.stage { display: flex; justify-content: center; padding: 32px 0 8px; overflow-x: clip; }
.book { position: relative; width: min(560px, 100%); aspect-ratio: 2 / 3; perspective: 2000px; touch-action: pan-y; }
.page {
  position: absolute; inset: 0; transform-origin: left center; transform-style: preserve-3d;
  transition: transform var(--flip), visibility 0s;
}
.page.flipped { transform: rotateY(-180deg); visibility: hidden; transition: transform var(--flip), visibility 0s .9s; }
.page.turning { z-index: var(--tz) !important; }
.book.instant .page, .book.instant .page.flipped { transition: none !important; }

.face {
  position: absolute; inset: 0; border-radius: 20px; overflow: hidden;
  backface-visibility: hidden; -webkit-backface-visibility: hidden;
  box-shadow: 0 0 0 3px #2a0d12, 0 0 0 5px var(--ember-dark), 0 0 60px #ff3a0055, 0 30px 80px #000c;
}
.front::after {                                   /* shadow along the spine */
  content: ""; position: absolute; inset: 0 auto 0 0; width: 26px; pointer-events: none;
  background: linear-gradient(90deg, #000a, transparent);
}
.back {                                           /* what you see on the page while it swings over */
  transform: rotateY(180deg); display: grid; place-items: center;
  background: radial-gradient(circle at 70% 40%, #3a1016, #1a0509 70%);
}
.back::before { content: ""; position: absolute; inset: 14px; border: 2px solid #ffb34744; border-radius: 12px; }
.back-mark { font-size: 54px; opacity: .55; }

/* cover page */
.cover-img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover-text {
  position: absolute; inset: auto 0 0 0; padding: 90px 20px 24px; text-align: center;
  background: linear-gradient(to top, #0d0406f2 18%, #0d040699 60%, transparent);
}
.cover-text h1 {
  margin: 0; font-size: clamp(30px, 7vw, 54px); line-height: 1.05; letter-spacing: -.5px;
  color: var(--gold); text-shadow: 0 0 18px #ff4a1c, 0 3px 0 #5a1406, 0 6px 14px #000;
}
.cover-text p { margin: 10px auto 0; max-width: 420px; color: var(--ink); font-size: 16px; line-height: 1.45; text-shadow: 0 2px 6px #000; }
.cover-text .from { margin-top: 8px; color: var(--gold); font-weight: 700; }
.actions { margin-top: 18px; display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }

/* signature page: one per page */
.sig, .signpage {
  display: flex; flex-direction: column; padding: 34px 30px 26px 38px; text-align: center;
  background: linear-gradient(160deg, #2a1218, #14070b);
}
.sig::before, .signpage::before {
  content: ""; position: absolute; inset: 12px 12px 12px 22px; border: 1px solid #ffb34733; border-radius: 12px; pointer-events: none;
}
.sig-media { flex: 1 1 0; min-height: 0; display: flex; align-items: center; justify-content: center; margin-bottom: 14px; }
.sig.has-msg .sig-media { flex: 0 1 52%; }
.sig-media img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 12px; border: 2px solid #5a2a30; background: #000; }
.sig-msg { flex: 1 1 auto; min-height: 0; overflow-y: auto; display: flex; flex-direction: column; padding: 0 4px; }
.sig-msg p { margin: auto 0; white-space: pre-wrap; overflow-wrap: anywhere; font-family: Georgia, "Times New Roman", serif; font-style: italic; line-height: 1.45; }
.sig-msg.lg p { font-size: 30px; }
.sig-msg.md p { font-size: 23px; }
.sig-msg.sm p { font-size: 18px; }
.sig-who { margin-top: 14px; font-weight: 800; font-size: 22px; color: var(--gold); text-shadow: 0 0 12px #ff4a1c66; }
.sig { padding-top: 58px; }                      /* room for the edit/delete buttons */
.sig-tools { position: absolute; top: 20px; right: 24px; display: flex; gap: 8px; z-index: 2; }
.tool {
  width: 34px; height: 34px; padding: 0; border-radius: 50%; font-size: 16px; line-height: 1;
  background: #2a1217; border: 1px solid #5a2a30; color: var(--ink); opacity: .75;
}
.tool:hover, .tool:focus-visible { opacity: 1; border-color: var(--ember); box-shadow: 0 0 12px #ff4a1c66; }

/* "sign here" page */
.signpage { align-items: center; justify-content: center; gap: 12px; }
.signpage .big { font-size: 72px; }
.signpage h2 { margin: 0; font-size: 38px; color: var(--gold); text-shadow: 0 0 18px #ff4a1c88; }
.signpage p { margin: 0 0 12px; max-width: 320px; line-height: 1.5; color: var(--ink); font-size: 18px; }

/* ---------- toolbar (always visible) ---------- */
.toolbar { position: relative; z-index: 5; display: flex; align-items: center; justify-content: center; gap: 10px; flex-wrap: wrap; margin-top: 22px; }
.nav { width: 44px; height: 40px; padding: 0; }
.pageno { min-width: 110px; text-align: center; color: var(--muted); font-weight: 600; font-size: 14px; }
.signfab { margin-left: 8px; box-shadow: 0 3px 0 var(--ember-dark), 0 0 20px #ff4a1c66; }

/* ---------- dialog ---------- */
dialog {
  border: 1px solid #6a2a30; border-radius: 20px; padding: 0; width: min(520px, calc(100vw - 24px));
  background: var(--panel); box-shadow: 0 0 60px #ff3a0044, 0 30px 80px #000; color: var(--ink);
}
dialog::backdrop { background: rgba(5, 1, 2, .75); backdrop-filter: blur(3px); }
form { padding: 24px; display: grid; gap: 14px; }
form h2 { margin: 0; font-size: 26px; color: var(--gold); }
label { font-weight: 700; font-size: 14px; display: grid; gap: 6px; }
input[type=text], textarea {
  font: inherit; padding: 12px 14px; border: 2px solid #4a1b22; border-radius: 12px; width: 100%;
  background: #0d0406; color: var(--ink);
}
input::placeholder, textarea::placeholder { color: #8a6a62; }
input:focus, textarea:focus { outline: 0; border-color: var(--ember); box-shadow: 0 0 0 3px #ff4a1c33; }
textarea { min-height: 110px; resize: vertical; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.hp { position: absolute; left: -9999px; }
.preview { position: relative; display: none; }
.preview.on { display: block; }
.preview img { width: 100%; max-height: 220px; object-fit: cover; border-radius: 12px; display: block; }
.preview .x {
  position: absolute; top: 8px; right: 8px; width: 30px; height: 30px; border-radius: 50%;
  border: 0; background: #000b; color: #fff; font-size: 16px;
}
.gifbox { display: none; gap: 8px; }
.gifbox.on { display: grid; }
.gifrow { display: flex; gap: 8px; }
.gifgrid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; max-height: 220px; overflow: auto; }
.gifgrid img { width: 100%; height: 80px; object-fit: cover; border-radius: 8px; cursor: pointer; }
.gifgrid img:hover { outline: 3px solid var(--ember); }
.hint { font-size: 12px; color: var(--muted); font-weight: 400; }
.err { color: #ff7a8a; font-weight: 700; font-size: 14px; min-height: 18px; }
.foot { display: flex; justify-content: flex-end; gap: 10px; }

.confirm { padding: 24px; display: grid; gap: 12px; }
.confirm h2 { margin: 0; font-size: 24px; color: var(--gold); }
.confirm p { margin: 0; line-height: 1.5; }
.btn.danger { background: var(--crimson); box-shadow: 0 3px 0 #6e0a1b; }
.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); z-index: 70;
  max-width: min(92vw, 420px); padding: 12px 18px; border-radius: 12px; text-align: center; font-weight: 600;
  background: #1a0a0e; color: var(--ink); border: 1px solid var(--ember); box-shadow: 0 10px 40px #000c, 0 0 24px #ff4a1c55;
}
.toast[hidden] { display: none; }

@media (max-width: 480px) {
  .sig, .signpage { padding: 28px 22px 22px 30px; }
  .sig { padding-top: 54px; }
  .sig-tools { top: 16px; right: 18px; }
  .sig-msg.lg p { font-size: 26px; }
  .signfab { margin-left: 0; }
}
