:root {
  --cover: #2d5a7c;
  --cover-dark: #16293a;
  --cover-gold: #d9b163;
  --paper: #fdf6ec;
  --paper-back: #ece0cc;
  --ink: #4a3d33;
  --ink-soft: #8a7a6b;
  --pink: #e7b9b0;
  --room-a: #1b2430;
  --room-b: #0d1319;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--room-a);
  font-family: 'Lora', serif;
  color: var(--ink);
  overscroll-behavior: contain;
}

#room {
  height: 100vh;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  background: radial-gradient(ellipse at 50% 30%, var(--room-a), var(--room-b));
  padding: max(10px, env(safe-area-inset-top, 0px)) 16px max(10px, env(safe-area-inset-bottom, 0px));
}

#book-stage {
  perspective: 1800px;
  width: min(94vw, 430px);
  height: min(74vh, 620px);
}

#book {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
}

/* spine */
#book::before {
  content: "";
  position: absolute;
  left: -10px;
  top: 2%;
  bottom: 2%;
  width: 20px;
  border-radius: 6px 0 0 6px;
  background: linear-gradient(to right, var(--cover-dark), var(--cover) 70%, #00000030);
  box-shadow: inset -4px 0 8px rgba(0,0,0,0.35);
  z-index: 1;
}

/* stacked page edge, peeking from the right side */
#book::after {
  content: "";
  position: absolute;
  top: 6px;
  bottom: 6px;
  right: -5px;
  width: 9px;
  border-radius: 0 6px 6px 0;
  background: repeating-linear-gradient(to bottom, var(--paper) 0 2px, var(--paper-back) 2px 4px);
  box-shadow: 2px 0 6px rgba(0,0,0,0.25);
  z-index: 0;
}

#pages {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  touch-action: none;
}

.leaf {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transform-origin: left center;
  will-change: transform;
}

.leaf .front,
.leaf .back {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  border-radius: 2px 14px 14px 2px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: 1px 0 0 rgba(0,0,0,0.05), 4px 4px 18px rgba(0,0,0,0.3);
}

.leaf .front { background: var(--paper); }
.leaf .back {
  background: var(--paper-back);
  transform: rotateY(180deg);
}

.leaf .front::after,
.leaf .back::after {
  content: "";
  position: absolute;
  inset: 0;
  background: #000;
  opacity: var(--flip-shadow, 0);
  pointer-events: none;
  z-index: 5;
}

@keyframes pageShadow { 0% { opacity: 0; } 45% { opacity: .32; } 100% { opacity: 0; } }
.leaf.flipping .front::after,
.leaf.flipping .back::after { animation: pageShadow .5s ease; }

/* paper texture */
.front::before, .back::before {
  content: "";
  position: absolute;
  inset: 0;
  background-image:
    radial-gradient(circle at 15% 10%, rgba(255,255,255,0.5) 0, transparent 35%),
    radial-gradient(circle at 85% 90%, rgba(0,0,0,0.04) 0, transparent 40%);
  pointer-events: none;
}

.page-content {
  position: relative;
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 22px 20px 36px;
  overflow: hidden;
}

/* ===== Cover / closing (hardcover faces) ===== */
.hardcover {
  background: linear-gradient(160deg, var(--cover) 0%, var(--cover-dark) 100%) !important;
  color: var(--cover-gold);
  align-items: center;
  justify-content: center;
  text-align: center;
}
.hardcover::before { background: none; }

.hardcover .frame {
  border: 3px solid var(--cover-gold);
  border-radius: 10px;
  padding: 10px;
}

.hardcover .title {
  font-family: 'Playfair Display', serif;
  font-size: 2rem;
  line-height: 1.15;
  margin: 0;
  text-shadow: 0 1px 0 rgba(0,0,0,0.3);
}

.hardcover .subtitle {
  font-family: 'Caveat', cursive;
  font-size: 1.4rem;
  color: #f0ddb8;
}

.hardcover .nameplate {
  font-family: 'Lora', serif;
  font-size: 0.85rem;
  letter-spacing: 2px;
  text-transform: uppercase;
  color: var(--cover-dark);
  background: var(--cover-gold);
  padding: 8px 18px;
  border-radius: 20px;
}

.hardcover .hint {
  margin-top: 4px;
  font-size: 0.78rem;
  color: #c9d8e4;
}

/* title page (back of front cover) */
.titlepage {
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 10px;
}
.titlepage p {
  font-family: 'Lora', serif;
  font-style: italic;
  color: var(--ink-soft);
  font-size: 0.95rem;
  max-width: 240px;
  margin: 0;
}
.titlepage .flourish { font-size: 1.4rem; color: var(--pink); }

/* ===== Placeholder photo ===== */
.placeholder-photo {
  position: relative;
  overflow: hidden;
  background: repeating-linear-gradient(135deg, rgba(0,0,0,0.03) 0px, rgba(0,0,0,0.03) 10px, transparent 10px, transparent 20px), #f3e9d7;
  border: 3px dashed var(--cover-gold);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--ink-soft);
  cursor: pointer;
}
.placeholder-photo > svg { width: 36px; height: 36px; opacity: 0.5; }
.placeholder-photo .ph-label {
  position: absolute;
  bottom: 8px;
  left: 0; right: 0;
  text-align: center;
  font-size: 0.68rem;
  color: var(--ink-soft);
  letter-spacing: 0.4px;
}
.placeholder-photo.filled { border: none; background: #000; }
.placeholder-photo.filled img {
  position: absolute;
  inset: 0;
  width: 100%; height: 100%;
  object-fit: cover;
  transform-origin: center;
  display: block;
}
.remove-photo, .replace-photo {
  position: absolute;
  top: 6px;
  width: 24px; height: 24px;
  border-radius: 50%;
  background: rgba(0,0,0,0.55);
  color: #fff;
  border: none;
  cursor: pointer;
  z-index: 2;
}
.remove-photo { right: 6px; font-size: 15px; line-height: 1; }
.replace-photo { left: 6px; display: flex; align-items: center; justify-content: center; padding: 5px; }
.replace-photo svg { width: 100%; height: 100%; }
.zoom-slider {
  position: absolute;
  left: 8%; right: 8%; bottom: 8px; width: 84%;
  height: 16px;
  z-index: 4;
  accent-color: var(--cover-gold);
}

.photo-frame {
  background: #fff;
  padding: 10px 10px 26px;
  border-radius: 3px;
  box-shadow: 0 8px 16px rgba(0,0,0,0.18), 0 2px 4px rgba(0,0,0,0.1);
  transform: rotate(var(--tilt, 0deg));
}
.photo-frame .placeholder-photo { width: 100%; aspect-ratio: 1 / 1; }
.photo-frame .caption {
  margin-top: 8px;
  text-align: center;
  font-family: 'Caveat', cursive;
  font-size: 1rem;
  color: var(--ink);
}

.layout-single .photo-frame { width: 100%; max-width: 190px; }
.layout-duo { display: flex; gap: 10px; width: 100%; }
.layout-duo .photo-frame { flex: 1; min-width: 0; }
.layout-duo .photo-frame:nth-child(1) { --tilt: -3deg; }
.layout-duo .photo-frame:nth-child(2) { --tilt: 3deg; margin-top: 14px; }
.layout-journal .photo-frame { width: 58%; max-width: 160px; }

.journal {
  width: 100%;
  background: rgba(255,255,255,0.5);
  border-radius: 8px;
  padding: 10px 14px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.05);
}
.journal .journal-title {
  font-family: 'Playfair Display', serif;
  font-size: 1.02rem;
  margin: 0 0 6px;
  color: var(--ink);
}
.journal .journal-text {
  font-size: 0.78rem;
  color: var(--ink-soft);
  line-height: 1.9;
  background-image: repeating-linear-gradient(to bottom, transparent 0, transparent calc(1.9em - 1px), rgba(74,61,51,0.15) calc(1.9em - 1px), rgba(74,61,51,0.15) 1.9em);
  min-height: 3.8em;
}

.page-number {
  position: absolute;
  bottom: 10px;
  left: 0; right: 0;
  text-align: center;
  font-family: 'Lora', serif;
  font-size: 0.68rem;
  color: var(--ink-soft);
  letter-spacing: 1px;
}

/* ===== Controls ===== */
#controls {
  display: flex;
  align-items: center;
  gap: 18px;
}
#controls button {
  width: 42px; height: 42px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.12);
  color: #e7eef3;
  font-size: 1rem;
  cursor: pointer;
  transition: opacity .2s;
}
#controls button:disabled { opacity: 0.25; cursor: default; }
#counter {
  color: #c9d8e4;
  font-family: 'Lora', serif;
  font-size: 0.85rem;
  letter-spacing: 1px;
  min-width: 100px;
  text-align: center;
}

#toast {
  position: fixed;
  left: 50%;
  bottom: max(20px, env(safe-area-inset-bottom, 0px));
  transform: translate(-50%, 20px);
  background: rgba(0,0,0,0.85);
  color: #fff;
  padding: 10px 18px;
  border-radius: 20px;
  font-size: 0.82rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s, transform .25s;
  z-index: 100;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
