@import url('https://fonts.googleapis.com/css2?family=Baloo+2:wght@600;700;800&family=Caveat:wght@600;700&display=swap');

:root {
  --bg: #fff5ee;
  --surface: #ffffff;
  --ink: #3b3550;
  --ink-soft: #8a8397;
  --line: #f3e7ee;
  --brand: #f4635e;
  --brand-dark: #dd4b48;
  --accent: #23bfa6;
  --danger: #e14b6a;
  --shadow: 0 8px 26px rgba(120, 80, 90, 0.12);
  --radius: 18px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --display: "Baloo 2", "Nunito", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --hand: "Caveat", "Segoe Print", "Bradley Hand", "Comic Sans MS", cursive;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #241f2b;
    --surface: #2e2836;
    --ink: #f4ecf6;
    --ink-soft: #b8afc4;
    --line: #3f374a;
    --brand: #ff7d78;
    --brand-dark: #f4635e;
    --accent: #4fd3bd;
    --shadow: 0 8px 26px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }
/* Ensure the `hidden` attribute always wins over element display rules
   (e.g. .btn sets display:inline-flex, which would otherwise show a
   [hidden] button — including the album view's Edit/Delete). */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.wrap { width: 100%; max-width: 760px; margin: 0 auto; padding: 0 16px; }

/* Header */
.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
  padding-top: env(safe-area-inset-top);
}
.header-inner {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 60px;
}
.brand {
  font-family: var(--display);
  font-size: 1.35rem;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  white-space: nowrap;
}
.brand-mark { color: var(--brand); }
.header-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.search {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 0.95rem;
  width: 190px;
  max-width: 42vw;
  outline: none;
}
.search:focus { border-color: var(--brand); }

/* Main / feed */
.main { padding-top: 24px; padding-bottom: 130px; }
body { overflow-x: hidden; } /* gentle card tilts must never scroll the page sideways */
.feed { display: flex; flex-direction: column; gap: 36px; padding: 6px 4px 0; }

/* Scrapbook Polaroid card */
.card {
  --paper: #fffdf8;
  --paper-ink: #3b322b;
  --paper-soft: #857a6f;
  position: relative;
  background: var(--paper);
  color: var(--paper-ink);
  padding: 12px 12px 0;
  border-radius: 5px;
  box-shadow: 0 10px 22px rgba(70, 50, 35, 0.16), 0 2px 5px rgba(70, 50, 35, 0.12);
  cursor: pointer;
  transform: rotate(var(--tilt, 0deg));
  transition: transform 0.5s cubic-bezier(.2, .75, .2, 1), box-shadow 0.2s ease, opacity 0.5s ease;
}
@media (prefers-color-scheme: dark) {
  .card { box-shadow: 0 16px 32px rgba(0, 0, 0, 0.5), 0 2px 6px rgba(0, 0, 0, 0.4); }
}

/* Colorful washi tape — a rotating set of candy colors so the feed feels
   like a playful scrapbook. */
.card { --tape: #ff9bbb; --tape-left: 50%; --tape-rot: -2.5deg; }
.card:nth-child(5n+2) { --tape: #4fd3bd; }
.card:nth-child(5n+3) { --tape: #ffce5c; }
.card:nth-child(5n+4) { --tape: #b9a0e6; }
.card:nth-child(5n+5) { --tape: #7fc9f2; }
.card:nth-child(2n)   { --tape-left: 30%; --tape-rot: -4deg; }
.card:nth-child(3n)   { --tape-left: 72%; --tape-rot: 3.5deg; }
.card::before {
  content: "";
  position: absolute;
  top: -11px;
  left: var(--tape-left);
  width: 104px;
  height: 26px;
  transform: translateX(-50%) rotate(var(--tape-rot));
  background: repeating-linear-gradient(45deg,
    color-mix(in srgb, var(--tape) 55%, transparent) 0 6px,
    color-mix(in srgb, var(--tape) 80%, transparent) 6px 12px);
  border-left: 1px dashed rgba(255,255,255,.55);
  border-right: 1px dashed rgba(255,255,255,.55);
  box-shadow: 0 1px 2px rgba(0,0,0,.08);
  border-radius: 2px;
  pointer-events: none;
}

/* pop-in as the card scrolls into view */
.card.pre { opacity: 0; transform: rotate(var(--tilt, 0deg)) translateY(28px) scale(.94); }

/* playful: straighten and lift on interaction */
.card:hover { transform: rotate(0deg) translateY(-6px) scale(1.02); box-shadow: 0 20px 36px rgba(70,50,35,0.24); }
.card:active { transform: rotate(0deg) scale(0.99); }

.card-photos { display: grid; gap: 3px; background: var(--paper); border-radius: 2px; overflow: hidden; }
.card-photos.n1 { grid-template-columns: 1fr; }
.card-photos.n2 { grid-template-columns: 1fr 1fr; }
.card-photos.n3, .card-photos.n4 { grid-template-columns: 1fr 1fr; }
.card-photos img {
  width: 100%;
  height: 100%;
  aspect-ratio: 3 / 2;
  object-fit: cover;
  display: block;
}
.card-photos.n1 img { aspect-ratio: 4 / 3; }
.photo-fallback { display: grid; place-items: center; aspect-ratio: 3 / 2; background: var(--line); color: var(--ink-soft); font-size: 1.8rem; }
.viewer-gallery .photo-fallback { flex: 0 0 100%; width: 100%; height: min(46vh, 460px); aspect-ratio: auto; border-radius: 0; }
.photo-thumb .photo-fallback { width: 100%; height: 100%; aspect-ratio: auto; font-size: 1.3rem; }
.card-photos .more { position: relative; }
.card-photos .more::after {
  content: attr(data-more);
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.45);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 1.4rem;
  font-weight: 600;
}

.card-body { padding: 12px 8px 18px; }
.card-title { font-family: var(--display); font-weight: 700; font-size: 1.3rem; margin: 0 0 4px; line-height: 1.25; color: var(--paper-ink); }
.card-date { font-family: var(--hand); color: var(--brand-dark); font-size: 1.2rem; line-height: 1; display: inline-block; transform: rotate(-1.5deg); margin-bottom: 8px; }
.card-story { color: var(--paper-soft); margin: 0; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.card-meta { display: flex; align-items: center; gap: 10px; margin-top: 12px; flex-wrap: wrap; }
.chip {
  background: color-mix(in srgb, var(--accent) 24%, #ffffff);
  color: color-mix(in srgb, var(--accent) 60%, #2a2440);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 0.78rem;
  font-weight: 700;
}
.has-audio { color: var(--brand-dark); font-size: 0.85rem; font-weight: 600; display: inline-flex; align-items: center; gap: 5px; }

@media (prefers-reduced-motion: reduce) {
  .card { transition: box-shadow 0.2s ease; }
  .card.pre { opacity: 1; transform: rotate(var(--tilt, 0deg)); }
  .card:hover { transform: rotate(0deg); }
}

/* Empty state */
.empty { text-align: center; padding: 60px 20px; }
.empty-art { font-size: 3rem; }
.empty h2 { font-family: var(--display); margin: 12px 0 6px; }
.empty p { color: var(--ink-soft); max-width: 380px; margin: 0 auto 20px; }

/* FAB */
.fab {
  position: fixed;
  right: max(20px, env(safe-area-inset-right));
  bottom: max(24px, env(safe-area-inset-bottom));
  width: 60px;
  height: 60px;
  border-radius: 50%;
  border: none;
  background: var(--brand);
  color: #fff;
  font-size: 2rem;
  line-height: 1;
  box-shadow: 0 10px 24px color-mix(in srgb, var(--brand) 55%, transparent);
  cursor: pointer;
  z-index: 20;
  transition: transform 0.2s cubic-bezier(.34, 1.56, .64, 1), background 0.12s ease;
}
.fab:hover { background: var(--brand-dark); transform: scale(1.1) rotate(8deg); }
.fab:active { transform: scale(0.92); }

/* Buttons */
.btn {
  font: inherit;
  font-weight: 700;
  border-radius: 999px;
  padding: 11px 20px;
  border: 1px solid transparent;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  transition: background 0.12s ease, border-color 0.12s ease, opacity 0.12s ease, transform 0.14s cubic-bezier(.34, 1.56, .64, 1);
}
.btn.small { padding: 6px 12px; font-size: 0.85rem; }
.btn.primary { background: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-dark); }
.btn.ghost { background: transparent; border-color: var(--line); color: var(--ink); }
.btn.ghost:hover { border-color: var(--brand); color: var(--brand); }
.btn.ai { background: color-mix(in srgb, var(--accent) 16%, transparent); color: var(--accent); border-color: transparent; }
.btn.ai:hover { background: color-mix(in srgb, var(--accent) 26%, transparent); }
.btn.danger { color: var(--danger); }
.narrate-btn { margin-top: 8px; }
.narrate-btn.on { background: color-mix(in srgb, var(--brand) 14%, transparent); border-color: var(--brand); color: var(--brand-dark); }
.narrate-btn.on:hover { background: color-mix(in srgb, var(--brand) 22%, transparent); }
.narrate-btn.busy { opacity: 0.75; cursor: progress; animation: narratePulse 1.1s ease-in-out infinite; }
@keyframes narratePulse { 0%, 100% { opacity: 0.55; } 50% { opacity: 0.9; } }
.btn.danger.ghost:hover { border-color: var(--danger); color: var(--danger); }
.btn:disabled { opacity: 0.5; cursor: default; }
.icon-btn {
  background: transparent;
  border: none;
  font-size: 1.3rem;
  cursor: pointer;
  padding: 6px;
  border-radius: 10px;
  line-height: 1;
}
.icon-btn:hover { background: var(--line); }

.btn.record { background: var(--surface); border-color: var(--brand); color: var(--brand); }
.btn.record .rec-dot { width: 12px; height: 12px; border-radius: 50%; background: var(--brand); }
.btn.record.recording { background: var(--brand); color: #fff; }
.btn.record.recording .rec-dot { background: #fff; animation: pulse 1s infinite; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.3; } }

/* Sheet / dialog */
.sheet {
  border: none;
  border-radius: 20px 20px 0 0;
  padding: 0;
  width: 100%;
  max-width: 640px;
  margin: auto auto 0;
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 -8px 40px rgba(0,0,0,0.25);
  max-height: 92vh;
  overflow: hidden;
}
/* When open, lay the dialog out as a column so the body scrolls.
   Scoped to [open] because author CSS beats the UA `dialog:not([open])`
   rule — an unscoped `display:flex` would keep CLOSED dialogs visible and
   floating over the page, intercepting clicks. */
.sheet[open] {
  display: flex;
  flex-direction: column;
}
@media (min-width: 640px) {
  .sheet { border-radius: 20px; margin: auto; max-height: 88vh; }
}
.sheet::backdrop { background: rgba(30, 20, 15, 0.5); backdrop-filter: blur(2px); }
/* The composer wraps its content in a <form>; make it the scrolling column too. */
.sheet form { display: flex; flex-direction: column; flex: 1 1 auto; min-height: 0; }
.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  flex: 0 0 auto;
}
.sheet-head h2 { margin: 0; font-family: var(--display); font-size: 1.25rem; }
.sheet-body {
  padding: 18px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 18px;
  flex: 1 1 auto;
  min-height: 0;
}
.sheet-foot {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 14px 18px calc(14px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--surface);
  flex: 0 0 auto;
  flex-wrap: wrap;
}

/* Fields */
.field { display: flex; flex-direction: column; gap: 7px; }
.field > span { font-weight: 600; font-size: 0.92rem; }
.field .sub { font-weight: 500; font-size: 0.85rem; color: var(--ink-soft); }
.field-row { display: flex; gap: 12px; }
.field.grow { flex: 1; }
.label-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
input[type="text"], input[type="date"], input[type="password"], input[type="search"], select, textarea {
  font: inherit;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 11px 13px;
  width: 100%;
  outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--brand); }
textarea { resize: vertical; }
.transcript { font-size: 0.9rem; color: var(--ink-soft); }
.story { font-family: var(--serif); font-size: 1.05rem; line-height: 1.6; }
.hint { color: var(--ink-soft); font-size: 0.85rem; margin: 0; }
.hint a { color: var(--brand); }

/* Photo grid in composer */
.photo-grid { display: flex; flex-wrap: wrap; gap: 8px; }
.photo-grid:empty { display: none; }
.photo-thumb { position: relative; width: 100px; height: 100px; border-radius: 12px; overflow: hidden; border: 1px solid var(--line); }
.photo-thumb img { width: 100%; height: 100%; object-fit: cover; }
.photo-thumb .thumb-x {
  position: absolute; top: 3px; right: 3px;
  width: 22px; height: 22px; border-radius: 50%;
  border: none; background: rgba(0,0,0,0.6); color: #fff;
  cursor: pointer; font-size: 0.8rem; line-height: 1; display: grid; place-items: center;
}
.photo-thumb .thumb-cover {
  position: absolute; top: 4px; left: 4px;
  background: var(--brand); color: #fff;
  font-size: 0.62rem; font-weight: 700; letter-spacing: 0.02em;
  padding: 2px 6px; border-radius: 999px;
}
.photo-thumb .thumb-bar {
  position: absolute; left: 0; right: 0; bottom: 0;
  display: flex; justify-content: center; gap: 2px;
  background: rgba(0,0,0,0.5); padding: 2px 0;
}
.photo-thumb .thumb-bar button {
  border: none; background: transparent; color: #fff;
  width: 26px; height: 22px; font-size: 0.8rem; line-height: 1; cursor: pointer; border-radius: 6px;
}
.photo-thumb .thumb-bar button:disabled { opacity: 0.35; cursor: default; }
.photo-thumb .thumb-bar button:not(:disabled):active { background: rgba(255,255,255,0.25); }
.photo-add-row { display: flex; gap: 10px; flex-wrap: wrap; }
.file-btn { cursor: pointer; }

.recorder { border: 1px solid var(--line); border-radius: 14px; padding: 14px; background: var(--bg); }
.rec-controls { display: flex; align-items: center; gap: 12px; }
.rec-timer { font-variant-numeric: tabular-nums; color: var(--ink-soft); font-weight: 600; }
#audioPreview { width: 100%; margin-top: 12px; }

.ai-status { font-size: 0.85rem; padding: 10px 12px; border-radius: 10px; background: var(--bg); color: var(--ink-soft); }
.ai-status.error { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); }
.ai-status.working { color: var(--accent); }

/* Viewer */
/* Swipeable photo carousel: keeps the photo area compact so the story
   sits right below it instead of after a tall stack. */
.viewer-carousel { margin-bottom: 4px; }
.viewer-gallery {
  display: flex;
  gap: 0;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  border-radius: 14px;
}
.viewer-gallery::-webkit-scrollbar { display: none; }
.viewer-photo {
  flex: 0 0 100%;
  scroll-snap-align: center;
  width: 100%;
  height: min(46vh, 460px);
  object-fit: cover;
  display: block;
}
.viewer-dots { display: flex; justify-content: center; gap: 7px; margin-top: 10px; }
.viewer-dots .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--line); cursor: pointer; transition: background 0.15s ease; }
.viewer-dots .dot.on { background: var(--brand); }
.viewer-date { color: var(--brand-dark); font-weight: 600; text-transform: uppercase; font-size: 0.8rem; letter-spacing: 0.02em; }
.viewer-story { font-family: var(--serif); font-size: 1.1rem; line-height: 1.7; white-space: pre-wrap; }
.viewer-audio { width: 100%; margin: 6px 0; }
.viewer-transcript { color: var(--ink-soft); font-size: 0.9rem; border-left: 3px solid var(--line); padding-left: 12px; white-space: pre-wrap; }
.viewer-transcript summary { cursor: pointer; font-weight: 600; color: var(--ink); margin-bottom: 8px; }
.viewer-tags { display: flex; flex-wrap: wrap; gap: 6px; }

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 90px;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--bg);
  padding: 12px 20px;
  border-radius: 999px;
  font-size: 0.9rem;
  font-weight: 500;
  box-shadow: var(--shadow);
  z-index: 50;
  max-width: 90vw;
  text-align: center;
}

/* Fullscreen photo lightbox */
.lightbox {
  width: 100vw;
  height: 100dvh;
  max-width: 100vw;
  max-height: 100dvh;
  margin: 0;
  padding: 0;
  border: none;
  background: #0b0b0d;
  overflow: hidden;
}
.lightbox::backdrop { background: #000; }
.lightbox[open] { display: flex; align-items: center; justify-content: center; }
.lb-img {
  max-width: 100vw;
  max-height: 100dvh;
  object-fit: contain;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  will-change: transform;
  transition: transform 0.18s ease;
}
.lb-img.zoomed { transition: none; cursor: grab; }
.lb-nav {
  position: fixed;
  top: 50%;
  transform: translateY(-50%);
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  font-size: 2rem;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
  z-index: 2;
}
.lb-nav:hover { background: rgba(255, 255, 255, 0.26); }
.lb-prev { left: max(8px, env(safe-area-inset-left)); }
.lb-next { right: max(8px, env(safe-area-inset-right)); }
.lb-close {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  right: max(10px, env(safe-area-inset-right));
  color: #fff;
  font-size: 1.5rem;
  background: rgba(255, 255, 255, 0.14);
  z-index: 2;
}
.lb-close:hover { background: rgba(255, 255, 255, 0.26); }
.lb-counter {
  position: fixed;
  top: max(16px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  color: #fff;
  font-size: 0.9rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  z-index: 2;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.6);
}
.lb-hint {
  position: fixed;
  bottom: max(16px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  color: rgba(255, 255, 255, 0.7);
  font-size: 0.78rem;
  z-index: 2;
  pointer-events: none;
  transition: opacity 0.4s ease;
}

/* Shared album */
.album-banner {
  background: color-mix(in srgb, var(--accent) 22%, var(--surface));
  color: var(--ink);
  text-align: center;
  font-size: 0.85rem;
  font-weight: 600;
  padding: 8px 14px;
  border-bottom: 1px solid var(--line);
}
.album-loading { text-align: center; color: var(--ink-soft); padding: 40px 16px; }
.checkbox-row { flex-direction: row !important; align-items: center; gap: 10px; cursor: pointer; }
.checkbox-row input { width: auto; }
body.album-mode .fab { display: none; }
.chip.chip-new { background: color-mix(in srgb, var(--brand) 20%, #ffffff); color: var(--brand-dark); }
.btn:active { transform: scale(0.95); }
.btn.primary:hover { transform: translateY(-1px); }

/* Theme picker */
.theme-swatches { display: flex; flex-wrap: wrap; gap: 10px; }
.swatch {
  display: flex; flex-direction: column; align-items: center; gap: 7px;
  background: var(--bg); border: 1.5px solid var(--line); border-radius: 14px;
  padding: 10px 8px; width: 92px; cursor: pointer; font: inherit;
  transition: transform 0.12s ease, border-color 0.12s ease;
}
.swatch:active { transform: scale(0.96); }
.swatch.on { border-color: var(--brand); box-shadow: 0 0 0 2px color-mix(in srgb, var(--brand) 35%, transparent); }
.sw-dots { display: flex; gap: 4px; }
.sw-dots i { width: 17px; height: 17px; border-radius: 50%; display: block; }
.sw-name { font-size: 0.72rem; font-weight: 700; color: var(--ink-soft); text-align: center; line-height: 1.15; }

/* Doodle header icons */
.brand-mark { width: 1.5em; height: 1.5em; display: inline-block; vertical-align: -0.28em; }
.icon-btn svg { width: 23px; height: 23px; display: block; }
#settingsBtn { color: var(--ink-soft); }
#settingsBtn:hover { color: var(--brand); }
