:root {
  --ink: #2b2a28;
  --ink-soft: #6b6660;
  --paper: #faf7f1;
  --card: #ffffff;
  --line: #e7e1d6;
  --sage: #5b8a72;
  --sage-dark: #3e6350;
  --gold: #e0a23b;
  --radius: 18px;
  --shadow: 0 8px 24px rgba(43, 42, 40, 0.08);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--paper);
  color: var(--ink);
  font-family: 'Nunito Sans', system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { font-family: 'Fraunces', Georgia, serif; margin: 0; }
a { color: inherit; text-decoration: none; }
img { max-width: 100%; display: block; }
button, select, input { font-family: inherit; }

.topnav {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 20px; background: var(--card); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
.brand { font-family: 'Fraunces', serif; font-weight: 700; font-size: 1.15rem; color: var(--sage-dark); }
.nav-links { display: flex; align-items: center; gap: 16px; font-weight: 600; font-size: 0.92rem; }
.nav-links a { color: var(--ink-soft); }
.nav-cta {
  background: var(--sage); color: #fff !important; padding: 8px 14px; border-radius: 999px;
}

main { max-width: 1080px; margin: 0 auto; }
.page-wrap { padding: 24px 20px 60px; }

.section-title {
  font-size: 1.4rem; margin: 28px 0 14px; color: var(--ink);
}
.empty-msg { color: var(--ink-soft); }

/* login */
.login-wrap { min-height: 90vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
.login-card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 40px 32px; width: 100%; max-width: 360px; text-align: center;
}
.login-emoji { font-size: 2.4rem; }
.login-card h1 { font-size: 1.6rem; margin: 8px 0 4px; }
.login-sub { color: var(--ink-soft); font-size: 0.92rem; margin-bottom: 24px; }
.login-card form { display: flex; flex-direction: column; gap: 14px; text-align: left; }
.login-card label { font-size: 0.85rem; font-weight: 600; color: var(--ink-soft); display: flex; flex-direction: column; gap: 6px; }
.login-card input {
  padding: 11px 12px; border-radius: 10px; border: 1px solid var(--line); font-size: 1rem;
}
.login-error { color: #b5382f; font-size: 0.85rem; }
.login-card button {
  margin-top: 6px; background: var(--sage); color: #fff; border: none; padding: 12px;
  border-radius: 999px; font-weight: 800; font-size: 1rem; cursor: pointer;
}

/* clusters */
.cluster-card {
  display: flex; gap: 16px; background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 14px; margin-bottom: 14px; box-shadow: var(--shadow);
  flex-wrap: wrap;
}
.cluster-strip { display: flex; gap: 6px; position: relative; }
.cluster-strip img { width: 58px; height: 58px; object-fit: cover; border-radius: 10px; }
.cluster-more {
  width: 58px; height: 58px; border-radius: 10px; background: var(--ink);
  color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 0.85rem;
}
.cluster-meta { flex: 1; min-width: 220px; }
.cluster-count { font-size: 0.82rem; color: var(--ink-soft); margin-bottom: 8px; font-weight: 600; }
.cluster-form { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.cluster-name {
  flex: 1; min-width: 160px; padding: 9px 10px; border-radius: 10px; border: 1px solid var(--line); font-size: 0.95rem;
}
.cluster-theme { padding: 9px 8px; border-radius: 10px; border: 1px solid var(--line); }
.cluster-actions { display: flex; gap: 8px; }

.btn-primary {
  background: var(--sage); color: #fff; border: none; padding: 9px 16px;
  border-radius: 999px; font-weight: 700; cursor: pointer; font-size: 0.9rem;
}
.btn-ghost {
  background: none; color: var(--ink-soft); border: 1px solid var(--line); padding: 9px 14px;
  border-radius: 999px; font-weight: 600; cursor: pointer; font-size: 0.9rem;
}
.btn-light {
  background: rgba(255,255,255,0.22); color: #fff; border: 1px solid rgba(255,255,255,0.55);
  padding: 9px 16px; border-radius: 999px; font-weight: 700; cursor: pointer; font-size: 0.9rem;
  backdrop-filter: blur(4px);
}

/* album grid */
.album-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 16px; }
.album-tile {
  position: relative; border-radius: var(--radius); overflow: hidden; aspect-ratio: 1;
  box-shadow: var(--shadow); background: linear-gradient(135deg, var(--accent, #5b8a72), var(--accent2, #3e6350));
}
.album-tile img { width: 100%; height: 100%; object-fit: cover; }
.album-tile-empty { width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; font-size: 2.6rem; }
.album-tile-label {
  position: absolute; left: 0; right: 0; bottom: 0; padding: 24px 12px 10px;
  background: linear-gradient(to top, rgba(0,0,0,0.65), transparent);
  color: #fff; display: flex; flex-direction: column; gap: 2px;
}
.album-tile-emoji { font-size: 1.1rem; }
.album-tile-name { font-weight: 800; font-family: 'Fraunces', serif; }
.album-tile-count { font-size: 0.75rem; opacity: 0.85; }

.photo-strip { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 6px; }
.photo-strip img { width: 100px; height: 100px; object-fit: cover; border-radius: 10px; flex-shrink: 0; }

/* upload */
.upload-wrap { max-width: 620px; }
.upload-hint { color: var(--ink-soft); margin-bottom: 18px; }
.dropzone {
  display: block; border: 2px dashed var(--line); border-radius: var(--radius);
  padding: 36px 20px; text-align: center; cursor: pointer; background: var(--card);
}
.dropzone input { display: none; }
.dropzone-icon { font-size: 2.2rem; }
.dropzone-text { color: var(--ink-soft); font-weight: 600; margin-top: 6px; }
.preview-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(90px, 1fr)); gap: 8px; margin: 18px 0; }
.preview-grid img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 10px; }
.submit-btn { width: 100%; padding: 14px; font-size: 1rem; }
.upload-status { margin-top: 12px; color: var(--ink-soft); font-weight: 600; text-align: center; }

/* album hero */
.album-hero { padding: 40px 20px 60px; color: #fff; }
.album-hero-inner { max-width: 1080px; margin: 0 auto; }
.album-emoji { font-size: 2.2rem; }
.album-title, .album-title-input {
  font-family: 'Fraunces', serif; font-size: 2rem; font-weight: 700; margin: 4px 0; color: #fff;
}
.album-title-input {
  background: transparent; border: none; border-bottom: 2px solid rgba(255,255,255,0.5);
  padding: 2px 0; width: 100%; max-width: 480px;
}
.album-title-input:focus { outline: none; border-color: #fff; }
.album-sub { opacity: 0.9; font-weight: 600; margin-bottom: 16px; }
.album-hero-actions { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.theme-select { padding: 9px 10px; border-radius: 999px; border: none; }

.photo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; margin-top: -34px; }
.photo-grid img {
  width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 12px; cursor: pointer;
  box-shadow: var(--shadow);
}

/* viewer */
.viewer-overlay {
  position: fixed; inset: 0; background: rgba(10,10,12,0.92); z-index: 100;
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.viewer-overlay[hidden] { display: none; }
.viewer-overlay img { max-width: 100%; max-height: 100%; border-radius: 8px; }

/* slideshow */
.slideshow { position: fixed; inset: 0; background: #05050a; z-index: 200; }
.slideshow[hidden] { display: none; }
.slideshow canvas { width: 100%; height: 100%; display: block; }
.ss-close {
  position: absolute; top: 18px; right: 22px; background: rgba(255,255,255,0.12); color: #fff;
  border: none; width: 42px; height: 42px; border-radius: 50%; font-size: 1.4rem; cursor: pointer;
}

@media (max-width: 560px) {
  .album-hero { padding: 28px 16px 46px; }
  .album-title, .album-title-input { font-size: 1.5rem; }
  .cluster-card { flex-direction: column; }
}

/* ==========================================================================
   Book viewer (page-flip)
   ========================================================================== */

.book-wrap {
  display: flex; flex-direction: column; align-items: center; gap: 14px;
  margin-top: -34px; padding: 0 8px 20px;
}
.book-container {
  width: 100%; max-width: 900px; height: 64vh; min-height: 340px; max-height: 640px;
}
.book-controls { display: flex; align-items: center; gap: 14px; }
.book-counter { font-weight: 700; color: var(--ink-soft); font-size: 0.9rem; min-width: 64px; text-align: center; }

/* page-flip renders our .page elements inside its own wrapper; page-content
   is what we control the look of. */
.page {
  background: var(--card);
}
.page-content {
  position: relative; width: 100%; height: 100%; padding: 14px; overflow: hidden;
  display: flex; align-items: center; justify-content: center; background: var(--paper);
}
.page-cover .page-content {
  padding: 0; color: #fff; flex-direction: column; gap: 10px; text-align: center;
}
.cover-emoji { font-size: 2.8rem; }
.cover-title { font-family: 'Fraunces', serif; font-weight: 700; font-size: 1.7rem; padding: 0 16px; }
.cover-sub { opacity: 0.9; font-weight: 600; font-size: 0.9rem; }

.layout-single { display: flex; }
.layout-single img { width: 100%; height: 100%; object-fit: contain; border-radius: 8px; cursor: pointer; }

.layout-duo-h { display: flex; gap: 8px; width: 100%; height: 100%; }
.layout-duo-h img { flex: 1; min-width: 0; height: 100%; object-fit: cover; border-radius: 8px; cursor: pointer; }

.layout-duo-v { display: flex; flex-direction: column; gap: 8px; width: 100%; height: 100%; }
.layout-duo-v img { flex: 1; min-height: 0; width: 100%; object-fit: cover; border-radius: 8px; cursor: pointer; }

.layout-trio { display: grid; grid-template-columns: 2fr 1fr; grid-template-rows: 1fr 1fr; gap: 8px; width: 100%; height: 100%; }
.layout-trio img { width: 100%; height: 100%; object-fit: cover; border-radius: 8px; cursor: pointer; }
.layout-trio img:first-child { grid-row: 1 / 3; }

.layout-grid4 { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; gap: 8px; width: 100%; height: 100%; }
.layout-grid4 img { width: 100%; height: 100%; object-fit: cover; border-radius: 8px; cursor: pointer; }

/* Manual collage pages — a tossed-polaroid scatter (see scatter_positions()
   in app.py) instead of a rigid grid, and works for any photo count. */
.layout-scatter { position: relative; padding: 6px; }
.scatter-photo {
  position: absolute; aspect-ratio: 1; object-fit: cover; cursor: pointer;
  background: #fff; border: 5px solid #fff; border-bottom-width: 16px;
  border-radius: 3px; box-shadow: 0 6px 14px rgba(0,0,0,0.28);
}

@media (max-width: 700px) {
  .book-container { height: 56vh; }
}

/* ==========================================================================
   Scrapbook decorations
   ========================================================================== */

.deco-layer { position: absolute; inset: 0; pointer-events: none; }
.deco {
  position: absolute; transform-origin: center center; pointer-events: none;
  user-select: none; -webkit-user-select: none;
}
.deco-sticker { font-size: 2.4rem; line-height: 1; filter: drop-shadow(0 2px 3px rgba(0,0,0,0.25)); }
/* Stickers place their emoji in a .sticker-inner child, not on .deco-sticker
   itself — the outer element already carries an inline `transform` for
   drag position/rotation/scale (see decorate.js), and a CSS animation on
   the same property would win the cascade and fight that positioning. */
.sticker-inner { display: inline-block; }

@keyframes sticker-bounce { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-16%); } }
@keyframes sticker-float  { 0%, 100% { transform: translateY(0) rotate(0deg); } 50% { transform: translateY(-10%) rotate(4deg); } }
@keyframes sticker-pulse  { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.16); } }
@keyframes sticker-wiggle { 0%, 100% { transform: rotate(-9deg); } 50% { transform: rotate(9deg); } }
@keyframes sticker-twinkle { 0%, 100% { transform: scale(1); opacity: 1; } 50% { transform: scale(1.25); opacity: 0.65; } }
@keyframes sticker-spin   { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }

.deco-sticker[data-content="💖"] .sticker-inner, .palette-item[data-content="💖"] { animation: sticker-pulse 1.5s ease-in-out infinite; }
.deco-sticker[data-content="⭐"] .sticker-inner, .palette-item[data-content="⭐"] { animation: sticker-twinkle 1.8s ease-in-out infinite; }
.deco-sticker[data-content="🎈"] .sticker-inner, .palette-item[data-content="🎈"] { animation: sticker-float 3.2s ease-in-out infinite; }
.deco-sticker[data-content="🦋"] .sticker-inner, .palette-item[data-content="🦋"] { animation: sticker-wiggle 1.1s ease-in-out infinite; }
.deco-sticker[data-content="🍭"] .sticker-inner, .palette-item[data-content="🍭"] { animation: sticker-wiggle 1.7s ease-in-out infinite; }
.deco-sticker[data-content="🐶"] .sticker-inner, .palette-item[data-content="🐶"] { animation: sticker-bounce 1.3s ease-in-out infinite; }
.deco-sticker[data-content="🐱"] .sticker-inner, .palette-item[data-content="🐱"] { animation: sticker-bounce 1.4s ease-in-out infinite; }
.deco-sticker[data-content="🦄"] .sticker-inner, .palette-item[data-content="🦄"] { animation: sticker-bounce 1.2s ease-in-out infinite; }
.deco-sticker[data-content="🎉"] .sticker-inner, .palette-item[data-content="🎉"] { animation: sticker-bounce 0.9s ease-in-out infinite; }
.deco-sticker[data-content="☀️"] .sticker-inner, .palette-item[data-content="☀️"] { animation: sticker-spin 6s linear infinite; }
.deco-sticker[data-content="🌙"] .sticker-inner, .palette-item[data-content="🌙"] { animation: sticker-twinkle 2.6s ease-in-out infinite; }
.deco-sticker[data-content="🧸"] .sticker-inner, .palette-item[data-content="🧸"] { animation: sticker-wiggle 2s ease-in-out infinite; }
.deco-sticker[data-content="🎂"] .sticker-inner, .palette-item[data-content="🎂"] { animation: sticker-pulse 2.2s ease-in-out infinite; }
.deco-sticker[data-content="🧁"] .sticker-inner, .palette-item[data-content="🧁"] { animation: sticker-bounce 1.6s ease-in-out infinite; }
.deco-text-inner {
  font-family: 'Caveat', 'Fraunces', cursive; font-size: 1.6rem; font-weight: 700; white-space: nowrap;
  text-shadow: 0 1px 2px rgba(0,0,0,0.15);
}

.washi-strip {
  width: 90px; height: 26px; opacity: 0.88; box-shadow: 0 2px 4px rgba(0,0,0,0.15);
}
.washi-sage { background: repeating-linear-gradient(45deg, var(--sage), var(--sage) 10px, var(--sage-dark) 10px, var(--sage-dark) 20px); }
.washi-gold { background: repeating-linear-gradient(45deg, var(--gold), var(--gold) 10px, #b5382f 10px, #b5382f 20px); }
.washi-dot { background-color: #fdf6f1; background-image: radial-gradient(#c97a2b 22%, transparent 23%); background-size: 12px 12px; }
.washi-stripe { background: repeating-linear-gradient(90deg, #6ea8d8, #6ea8d8 8px, #fff 8px, #fff 16px); }

.frame-overlay { width: 140px; height: 140px; border-radius: 4px; }
.frame-polaroid { background: #fff; border: 8px solid #fff; border-bottom-width: 26px; box-shadow: 0 4px 10px rgba(0,0,0,0.25); }
.frame-scalloped { border: 6px solid #fff; border-radius: 50%; box-shadow: 0 4px 10px rgba(0,0,0,0.2), 0 0 0 3px var(--gold) inset; }
.frame-vintage { border: 3px dashed var(--sage-dark); background: rgba(255,255,255,0.05); }

/* ---- decorate (edit) mode ---- */

.edit-stage {
  width: 100%; max-width: 900px; margin: 0 auto;
  display: flex; flex-direction: column; align-items: center; gap: 12px;
}
.edit-stage[hidden] { display: none; }
.edit-page-frame {
  width: 100%; aspect-ratio: 3/4; max-height: 62vh; background: var(--card);
  box-shadow: var(--shadow); border-radius: 10px; overflow: hidden; position: relative;
}
.edit-page-frame .page-content { position: absolute; inset: 0; }
.edit-page-frame .deco-layer { pointer-events: auto; }
.edit-page-frame .deco { pointer-events: auto; cursor: grab; }
.edit-page-frame .deco.selected { outline: 2px dashed var(--sage); outline-offset: 4px; cursor: grabbing; }

.deco-toolbar {
  position: absolute; display: flex; gap: 4px; background: var(--ink); border-radius: 999px;
  padding: 4px; box-shadow: 0 4px 10px rgba(0,0,0,0.3); z-index: 999; transform: translate(-50%, -100%);
}
.deco-toolbar button {
  width: 28px; height: 28px; border-radius: 50%; border: none; background: rgba(255,255,255,0.14);
  color: #fff; font-size: 0.9rem; cursor: pointer; display: flex; align-items: center; justify-content: center;
}
.deco-toolbar button:hover { background: rgba(255,255,255,0.28); }
.deco-toolbar button.delete { background: #b5382f; }

.edit-controls { display: flex; align-items: center; gap: 12px; }

.palette-tray {
  width: 100%; max-width: 900px; background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px; margin: 0 auto;
}
.palette-tabs { display: flex; gap: 6px; margin-bottom: 12px; flex-wrap: wrap; }
.palette-tab {
  padding: 7px 14px; border-radius: 999px; border: 1px solid var(--line); background: var(--paper);
  font-weight: 700; font-size: 0.85rem; cursor: pointer; color: var(--ink-soft);
}
.palette-tab.active { background: var(--sage); color: #fff; border-color: var(--sage); }
.palette-items { display: flex; flex-wrap: wrap; gap: 10px; }
.palette-item {
  border: 1px solid var(--line); background: var(--paper); border-radius: 12px;
  padding: 10px 14px; cursor: pointer; font-size: 1.5rem; line-height: 1;
}
.palette-item.text-choice { font-size: 0.95rem; font-weight: 700; padding: 10px 16px; }
.palette-item.color-swatch { width: 34px; height: 34px; padding: 0; border-radius: 50%; }
.palette-item.washi-choice, .palette-item.frame-choice { width: 60px; height: 40px; padding: 0; display: flex; align-items: center; justify-content: center; overflow: hidden; }
.palette-item.frame-choice { width: 48px; height: 48px; }
.palette-item.selected-choice { outline: 2px solid var(--sage); }

@media (max-width: 560px) {
  .edit-page-frame { aspect-ratio: 3/4; max-height: 52vh; }
  .washi-strip { width: 70px; height: 20px; }
  .frame-overlay { width: 100px; height: 100px; }
}

/* ==========================================================================
   Manual collage picker + un-collage affordance
   ========================================================================== */

.collage-picker {
  position: fixed; inset: 0; background: rgba(10,10,12,0.86); z-index: 150;
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.collage-picker[hidden] { display: none; }
.collage-picker-inner {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 22px; width: 100%; max-width: 720px; max-height: 84vh; overflow-y: auto;
}
.collage-picker-inner h3 { font-size: 1.15rem; margin-bottom: 14px; }
.collage-picker-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: 10px; }
.collage-pick-item {
  position: relative; border: 3px solid transparent; border-radius: 10px; overflow: hidden;
  padding: 0; cursor: pointer; aspect-ratio: 1; background: none;
}
.collage-pick-item img { width: 100%; height: 100%; object-fit: cover; }
.collage-pick-item.selected { border-color: var(--sage); }
.collage-pick-item.claimed { opacity: 0.4; cursor: not-allowed; }
.collage-pick-badge {
  position: absolute; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.65); color: #fff;
  font-size: 0.62rem; font-weight: 700; padding: 3px 2px; text-align: center;
}
.collage-picker-actions { display: flex; align-items: center; gap: 12px; margin-top: 16px; justify-content: flex-end; }

.uncollage-btn {
  position: absolute; top: 8px; right: 8px; z-index: 10; background: rgba(20,20,22,0.65); color: #fff;
  border: none; border-radius: 999px; padding: 6px 12px; font-size: 0.72rem; font-weight: 700; cursor: pointer;
}
.uncollage-btn:hover { background: rgba(20,20,22,0.85); }
