:root {
  --night: #0b0d12;
  --paper: #fbf6ec;
  --paper-edge: #efe6d2;
  --ink: #241e15;
  --ink-soft: #6c6355;
  --rule: #d9cfb8;
  --gold: #8a5e22;
  --error: #b42318;
  --radius: 22px;
  --page-paper: #fbf6ec;
  --page-ink: #241e15;
  --page-accent: #8a5e22;
  --page-grain-opacity: 0.20;
  --page-grain-blend: multiply;
  color-scheme: light;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { min-height: 100%; }

body {
  margin: 0;
  background: var(--night);
  color: var(--paper);
  font: 16px/1.5 Inter, system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

.sky {
  position: fixed;
  inset: 0;
  z-index: -1;
  background:
    radial-gradient(1px 1px at 12% 18%, #fff8 50%, transparent 51%),
    radial-gradient(1px 1px at 72% 9%, #fff6 50%, transparent 51%),
    radial-gradient(1.5px 1.5px at 38% 62%, #fff5 50%, transparent 51%),
    radial-gradient(1px 1px at 88% 44%, #fff7 50%, transparent 51%),
    radial-gradient(1px 1px at 22% 86%, #fff5 50%, transparent 51%),
    radial-gradient(120% 70% at 50% 120%, #2c2418 0%, transparent 60%),
    var(--night);
}

.top {
  display: flex;
  justify-content: center;
  padding: max(20px, env(safe-area-inset-top)) 16px 8px;
}

.brand { height: 22px; filter: invert(1); }

main {
  max-width: 560px;
  margin: 0 auto;
  padding: 12px 16px calc(40px + env(safe-area-inset-bottom));
}

.state:not(.paper) { text-align: center; color: #fff; padding: 64px 8px; }
.state:not(.paper) h1 { color: #fff; }

.paper {
  background: linear-gradient(var(--paper), var(--paper)) padding-box,
    linear-gradient(160deg, #c8a96b, var(--paper-edge) 40%) border-box;
  border: 1px solid transparent;
  border-radius: var(--radius);
  padding: 28px 22px 24px;
  box-shadow: 0 30px 80px #0008;
  text-align: left;
  color: var(--ink);
}

.paper h1 { color: var(--ink); }

h1 {
  font: 700 30px/1.12 Fraunces, Georgia, serif;
  margin: 6px 0 8px;
  letter-spacing: -0.01em;
}

h2 {
  font: 700 24px/1.15 Fraunces, Georgia, serif;
  margin: 0;
}

.eyebrow {
  margin: 0;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--gold);
}

.muted { color: var(--ink-soft); margin: 0 0 8px; }
.state:not(.paper) .muted { color: #fffb; }

fieldset {
  border: 0;
  min-width: 0;
  padding: 0;
  margin: 26px 0 0;
  display: grid;
  gap: 12px;
}

legend {
  font: 600 19px/1.2 Fraunces, Georgia, serif;
  margin-bottom: 10px;
}

label {
  display: grid;
  gap: 6px;
  font-size: 13px;
  font-weight: 500;
  color: var(--ink-soft);
}

.row { display: flex; gap: 12px; align-items: end; }
.row > * { flex: 1; min-width: 0; }
.paper, label, input, textarea { min-width: 0; }
.row > button { flex: 0 0 auto; }

input, textarea {
  font: inherit;
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--rule);
  border-radius: 12px;
  padding: 11px 12px;
  width: 100%;
  min-height: 46px;
}

textarea { resize: vertical; }

input:focus, textarea:focus { outline: 2px solid var(--gold); outline-offset: 1px; }

.photo-row {
  display: flex;
  align-items: center;
  gap: 14px;
}

.photo-pick {
  position: relative;
  width: 82px;
  height: 82px;
  flex: 0 0 82px;
  border-radius: 50%;
  border: 1.5px dashed var(--rule);
  background: #fff8;
  color: var(--ink-soft);
  display: grid;
  place-items: center;
  font-size: 26px;
  overflow: hidden;
  padding: 0;
}

.photo-pick img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.photo-copy { display: grid; gap: 3px; }
.photo-copy strong { font: 600 15px/1.2 Fraunces, Georgia, serif; color: var(--ink); }
.photo-copy .hint { margin: 0; }

/* ---------------------------------------------------------------- prompts */

.progress-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 18px;
}

.marks { display: flex; gap: 5px; }
.marks span { display: block; width: 16px; height: 3px; border-radius: 99px; background: #d9cfb8; }
.marks span.on { background: var(--gold); }

.progress-label { font-size: 12px; color: var(--ink-soft); }

.prompt-stage h1 { font-size: 32px; margin-top: 10px; }

#prompt-input { margin-top: 20px; display: grid; gap: 12px; }

#prompt-input input[type="text"], #prompt-input textarea {
  font-family: Fraunces, Georgia, serif;
  font-size: 20px;
  min-height: 58px;
  background: #fffdf8;
}

.choice-grid { display: grid; gap: 10px; }
.choice {
  text-align: left;
  padding: 16px;
  border-radius: 16px;
  border: 1px solid var(--rule);
  background: #fffdf8;
  color: var(--ink);
  font: 400 20px/1.25 Fraunces, Georgia, serif;
}
.choice[aria-pressed="true"] { border: 2px solid var(--gold); background: #f7ecd8; }

.snapshot-grid { display: grid; gap: 10px; padding: 16px; border-radius: 16px; background: #fffdf8; border: 1px solid var(--rule); }
.snapshot-grid label { font-size: 10px; letter-spacing: 0.08em; text-transform: uppercase; }
.snapshot-grid input { background: transparent; border: 0; border-bottom: 1px solid var(--rule); border-radius: 0; padding: 6px 0; min-height: 36px; font: 400 18px/1.3 Fraunces, Georgia, serif; }

.hint { font-size: 13px; color: var(--ink-soft); margin: 12px 0 0; }
.error { color: var(--error); font-size: 14px; margin: 16px 0 0; }

.actions { display: flex; gap: 10px; align-items: center; margin-top: 26px; }
.actions .primary { margin-top: 0; }
.actions .ghost { flex: 0 0 auto; }

/* ----------------------------------------------------------------- buttons */

button, .stores a {
  font: 600 15px/1 Inter, system-ui, sans-serif;
  border-radius: 999px;
  cursor: pointer;
  text-decoration: none;
  text-align: center;
}

.primary {
  display: block;
  width: 100%;
  margin-top: 28px;
  padding: 16px;
  border: 0;
  background: var(--ink);
  color: #fff;
}

.primary:disabled { opacity: 0.45; cursor: default; }

.ghost {
  padding: 12px 16px;
  border: 1px solid var(--rule);
  background: transparent;
  color: var(--ink);
}

.ghost.small { padding: 9px 13px; font-size: 13px; }
.ghost.danger { color: var(--error); border-color: #e4b4ae; }

.link { border: 0; background: none; color: var(--ink-soft); font-size: 13px; padding: 4px; }

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

.page-stage {
  padding: 12px 0 0;
}

.page-intro { padding: 0 6px 16px; }
.page-intro h1 { color: #fff; font-size: 34px; margin-top: 6px; }
.page-intro .muted { color: #c9bea9; }

.page-frame {
  position: relative;
  padding: 0 4px;
  perspective: 1200px;
}

.friend-page {
  position: relative;
  width: 100%;
  min-height: 460px;
  padding: 26px 24px 24px;
  border-radius: 8px 8px 10px 10px;
  background: var(--page-paper);
  color: var(--page-ink);
  box-shadow:
    0 1px 0 #fff4 inset,
    0 18px 40px #0009,
    0 2px 6px #0006;
  overflow: hidden;
  isolation: isolate;
  transform-origin: 50% 0;
  animation: page-in 0.72s cubic-bezier(0.2, 0.75, 0.25, 1) both;
}

@keyframes page-in {
  from { opacity: 0; transform: translateY(26px) rotateX(10deg) scale(0.94); filter: blur(2px); }
  to { opacity: 1; transform: translateY(0) rotateX(0) scale(1); filter: blur(0); }
}

.friend-page::before,
.friend-page::after {
  content: "";
  position: absolute;
  left: 6px;
  right: 6px;
  height: 10px;
  border-radius: 0 0 8px 8px;
  background: var(--page-paper);
  filter: brightness(0.96);
  z-index: -1;
}
.friend-page::before { bottom: -8px; left: 10px; right: 10px; }
.friend-page::after { bottom: -4px; left: 16px; right: 16px; }

.page-grain {
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  opacity: var(--page-grain-opacity);
  mix-blend-mode: var(--page-grain-blend);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.82' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix type='saturate' values='0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.55'/%3E%3C/svg%3E");
}

.page-head {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  gap: 12px;
  padding-bottom: 16px;
  border-bottom: 1px solid color-mix(in srgb, var(--page-ink) 16%, transparent);
}

.page-avatar {
  width: 58px;
  height: 58px;
  flex: 0 0 58px;
  border-radius: 50%;
  overflow: hidden;
  display: grid;
  place-items: center;
  background: color-mix(in srgb, var(--page-ink) 10%, transparent);
  color: var(--page-ink);
  font: 600 18px/1 Fraunces, Georgia, serif;
}
.page-avatar img { width: 100%; height: 100%; object-fit: cover; }

.page-head h2 { font-size: 26px; }
.page-head p { margin: 3px 0 0; font-size: 12px; opacity: 0.62; }

.page-answers {
  position: relative;
  z-index: 1;
  display: grid;
  gap: 18px;
  margin-top: 20px;
}

.page-answer-label {
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  opacity: 0.55;
  margin-bottom: 5px;
}
.page-answer-text {
  font-family: var(--hand, Fraunces, Georgia, serif);
  font-size: calc(18px * var(--hand-size, 1));
  line-height: 1.45;
  color: var(--hand-ink, inherit);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.page-answer-snapshot { display: grid; gap: 4px; }
.page-answer-snapshot div { display: flex; gap: 10px; align-items: baseline; }
.page-answer-snapshot b { font-size: 9px; font-weight: 700; letter-spacing: 0.1em; text-transform: uppercase; opacity: 0.5; width: 56px; }
.page-answer-snapshot span {
  font-family: var(--hand, Fraunces, Georgia, serif);
  font-size: calc(16px * var(--hand-size, 1));
  line-height: 1.3;
  color: var(--hand-ink, inherit);
}
.page-answer-choice {
  font-family: var(--hand, Fraunces, Georgia, serif);
  font-size: calc(18px * var(--hand-size, 1));
  color: var(--hand-ink, inherit);
}
/* A skipped prompt: a dashed line where the answer would be. */
.page-answer-blank { height: 0; margin-top: 10px; border-top: 1px dashed currentColor; opacity: 0.25; }

.page-signature-note {
  position: relative;
  z-index: 1;
  margin: 26px 0 0;
  padding-top: 12px;
  border-top: 1px dashed color-mix(in srgb, var(--page-ink) 22%, transparent);
  font: italic 13px/1.3 Fraunces, Georgia, serif;
  opacity: 0.55;
}

.page-number {
  position: relative;
  z-index: 1;
  margin-top: 16px;
  text-align: center;
  font-size: 10px;
  letter-spacing: 0.24em;
  opacity: 0.45;
}

#sketch {
  position: absolute;
  inset: 0;
  z-index: 2;
  width: 100%;
  height: 100%;
  pointer-events: none;
  touch-action: none;
}

.page-stage.drawing #sketch { pointer-events: auto; cursor: crosshair; }
.page-stage.drawing .friend-page { user-select: none; }

.page-stickers {
  position: absolute;
  inset: 0;
  z-index: 3;
  pointer-events: none;
}

.sticker {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%) rotate(0deg) scale(1);
  font-size: 38px;
  line-height: 1;
  pointer-events: auto;
  cursor: grab;
  filter: drop-shadow(0 2px 2px #0003);
  user-select: none;
  touch-action: none;
}
.sticker.selected { outline: 1.5px dashed color-mix(in srgb, var(--page-ink) 55%, transparent); outline-offset: 4px; border-radius: 8px; }

/* --------------------------------------------------------------- decorate */

.decorate { display: grid; gap: 12px; margin-top: 22px; }

.decorate-row {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 40px;
}

.decorate-label {
  flex: 0 0 54px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: #9d927f;
}

.themes { display: flex; gap: 9px; }

.theme-dot {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 2px solid transparent;
  padding: 0;
  box-shadow: 0 2px 6px #0006;
}
.theme-dot[aria-pressed="true"] { border-color: #fff; }
.theme-dot.theme-ivory { background: #fbf6ec; }
.theme-dot.theme-kraft { background: #e6d3b8; }
.theme-dot.theme-linen { background: #f1ede4; }
.theme-dot.theme-lamplight { background: #2a2118; }

.sticker-shelf {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding: 3px 1px 7px;
  scrollbar-width: none;
}
.sticker-shelf::-webkit-scrollbar { display: none; }

.sticker-choice {
  flex: 0 0 auto;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid #ffffff22;
  background: #ffffff0e;
  color: #fff;
  font-size: 21px;
  padding: 0;
  display: grid;
  place-items: center;
}

.sticker-tools { display: flex; gap: 8px; flex-wrap: wrap; }
.sticker-tools .ghost { color: #e9dfcd; border-color: #ffffff24; }
.sticker-tools .ghost.danger { color: #ffb4ab; border-color: #ffb4ab55; }

/* Each pen chip is a scrap of ivory paper with its name written in the pen's
   own hand and ink, as the page will be written. */
.pens { display: flex; gap: 8px; flex-wrap: wrap; }

.pen-pick {
  min-height: 44px;
  padding: 10px 14px;
  border-radius: 14px;
  border: 1.5px solid #ffffff22;
  background: #fbf6ec;
  font-size: 16px;
  line-height: 1.15;
}

.pen-pick[aria-pressed="true"] { outline: 2px solid #fff; outline-offset: 1px; }
.pen-pick.pen-fountain { font: 600 18px/1.15 Caveat, cursive; color: #1f3566; }
.pen-pick.pen-fineliner { font: 500 14px/1.15 'Shantell Sans', cursive; color: #241e15; }
.pen-pick.pen-pencil { font: 400 17px/1.15 'Shadows Into Light', cursive; color: #55524c; }
.pen-pick.pen-felt { font: 400 15px/1.15 'Gochi Hand', cursive; color: #245c45; }

.pen-tools { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* A 28 px swatch inside a 44 px touch target; the colour follows the paper. */
.pen-tool {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 0;
  padding: 0;
  background: radial-gradient(circle, var(--pen, #241e15) 13px, transparent 14px);
  box-shadow: inset 0 0 0 8px transparent;
}
.pen-tool[aria-pressed="true"] {
  background: radial-gradient(circle, var(--pen, #241e15) 12px, #fff 13px, #fff 15px, transparent 16px);
}

.pen-tools .ghost { color: #e9dfcd; border-color: #ffffff24; }
.pen-tools .ghost[aria-pressed="true"] { background: #fff; color: #241e15; }

.page-actions {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-top: 20px;
}
.page-actions .ghost { color: #e9dfcd; border-color: #ffffff24; }
/* "Change something" is a native menu of the page's prompts. */
.page-actions select.ghost {
  appearance: none;
  -webkit-appearance: none;
  max-width: 48%;
  min-height: 44px;
  border-radius: 999px;
  font: inherit;
  text-overflow: ellipsis;
}
.page-actions .primary {
  margin-top: 0;
  flex: 1;
  background: #fbf6ec;
  color: #241e15;
}

/* --------------------------------------------------------------- theme vars */

.friend-page.theme-ivory {
  --page-paper: #fbf6ec;
  --page-ink: #241e15;
  --page-accent: #8a5e22;
  --page-grain-opacity: 0.22;
  --page-grain-blend: multiply;
}
.friend-page.theme-kraft {
  --page-paper: #e6d3b8;
  --page-ink: #2b2115;
  --page-accent: #7a4a1e;
  --page-grain-opacity: 0.30;
  --page-grain-blend: multiply;
}
.friend-page.theme-linen {
  --page-paper: #f1ede4;
  --page-ink: #2a2a27;
  --page-accent: #6b5b45;
  --page-grain-opacity: 0.26;
  --page-grain-blend: multiply;
}
.friend-page.theme-lamplight {
  --page-paper: #2a2118;
  --page-ink: #efe2cb;
  --page-accent: #e3be83;
  --page-grain-opacity: 0.12;
  --page-grain-blend: screen;
}

.friend-page.theme-lamplight .page-head { border-bottom-color: #ffffff22; }
.friend-page.theme-lamplight .page-signature-note { border-top-color: #ffffff2e; }

/* -------------------------------------------------------------------- sign */

.sign-stage .pad { position: relative; margin-top: 20px; }

canvas#signature {
  display: block;
  width: 100%;
  aspect-ratio: 12 / 5;
  background: #fffdf8;
  border: 1px dashed var(--rule);
  border-radius: 14px;
  touch-action: none;
}

.pad .link { position: absolute; top: 8px; right: 10px; }

.sign-stage label { margin-top: 14px; }

.done .primary { background: var(--paper); color: var(--ink); border: 1px solid var(--rule); }

/* The finished page, shown the way it will look in the book. */
.done-page { position: relative; padding: 0 4px; margin-top: 18px; perspective: 1200px; }
.done-page .friend-page { animation: none; margin: 0; }
.done-page .sticker { pointer-events: none; cursor: default; }
.done-page #sketch { pointer-events: none; }

.teaser { margin: 18px 0 0; }

.done-actions { display: grid; gap: 10px; margin-top: 18px; }
.done-actions .primary { margin-top: 0; }
.done-actions .ghost { display: block; padding: 16px; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

@media (max-width: 460px) {
  .row { flex-direction: column; align-items: stretch; }
  .decorate-label { flex-basis: 44px; }
}

@media (prefers-reduced-motion: reduce) {
  .friend-page { animation: none; }
}
