/* Sealed — stage backdrops, "Phông nền" (js/backdrop.js + themes): the decorated scene behind the envelope
   and the letter, in the studio preview and on the reader's stage. Letters without one are not touched. */

/* the layer: first child of the stage, under the effects, the envelope (WebGL layer or CSS) and the letter */
.stage-backdrop {
  position: absolute; inset: 0; z-index: 0; overflow: hidden; pointer-events: none;
  container-type: size; contain: layout paint style;
  animation: bd-in .9s ease-out both;
}
/* the reader's stage scrolls while the letter is read: the scene stays put behind it */
.stage > .stage-backdrop { position: fixed; }
@keyframes bd-in { from { opacity: 0; } to { opacity: 1; } }
.stage-backdrop .bd-wash-host, .stage-backdrop .bd-wash { position: absolute; inset: 0; width: 100%; height: 100%; }
.stage-backdrop .bd-items { position: absolute; inset: 0; }
/* a soft light in the middle, behind the envelope */
.stage-backdrop .bd-veil { position: absolute; inset: 0; background: radial-gradient(ellipse 34% 38% at 50% 47%, rgba(255, 251, 243, .78), rgba(255, 251, 243, 0) 100%); }
.stage-backdrop.bd-transparent .bd-veil { background: radial-gradient(ellipse 34% 38% at 50% 47%, rgba(255, 255, 255, .4), rgba(255, 255, 255, 0) 100%); }
.stage-backdrop .bd-veil[hidden] { display: none; }
/* one even paper grain over the whole scene ties every decoration to the same paper */
.stage-backdrop .bd-grain {
  position: absolute; inset: 0; pointer-events: none; opacity: .3; mix-blend-mode: multiply;
  background: 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='.8' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .42 0 0 0 0 .32 0 0 0 0 .26 .9 0 0 0 -.38'/%3E%3C/filter%3E%3Crect width='180' height='180' filter='url(%23n)'/%3E%3C/svg%3E") 0 0 / 180px 180px;
}
.stage-backdrop.bd-transparent .bd-grain { opacity: .16; }

/* each decoration: positioned by its centre (% of the scene), sized on the scene's shorter side (cqmin) */
.bd-item { position: absolute; transform: translate(-50%, -50%) rotate(var(--bd-r, 0deg)); }
.bd-item.bd-off { display: none; }
.bd-item.bd-field { inset: 0; transform: none; }
.bd-anim { position: relative; }
.bd-item svg { display: block; width: 100%; height: auto; overflow: visible; }
.bd-item .bd-fx { position: absolute; inset: 0; height: 100%; }
.bd-item .bd-back { z-index: -1; }
.bd-anim > .bd-art { position: relative; }

/* ---- gentle motion: transforms only, so the painted (filtered) art is rasterised once ---- */
.bd-a-float, .bd-a-sway, .bd-a-sway-b { will-change: transform; animation: var(--bd-kf, none) calc(var(--bd-dur, 6s) / var(--bd-speed, 1)) ease-in-out var(--bd-delay, 0s) infinite alternate; }
.bd-a-float { --bd-kf: bd-float; transform-origin: 50% 30%; }
.bd-a-sway { --bd-kf: bd-sway; transform-origin: 50% 0; }
.bd-a-sway-b { --bd-kf: bd-sway-b; transform-origin: 50% 100%; }
@keyframes bd-float { from { transform: translate3d(0, 1.5%, 0) rotate(-2deg); } to { transform: translate3d(0, -2.5%, 0) rotate(2deg); } }
@keyframes bd-sway { from { transform: rotate(-.9deg); } to { transform: rotate(.9deg); } }
@keyframes bd-sway-b { from { transform: rotate(-1.4deg); } to { transform: rotate(1.4deg); } }
.bd-string { transform-box: fill-box; transform-origin: 50% 0; animation: bd-string calc(3.4s / var(--bd-speed, 1)) ease-in-out var(--bd-delay, 0s) infinite alternate; }
@keyframes bd-string { from { transform: rotate(-4deg) skewX(2deg); } to { transform: rotate(4deg) skewX(-2deg); } }
.bd-flame { transform-box: fill-box; transform-origin: 50% 100%; animation: bd-flicker calc(1.1s / var(--bd-speed, 1)) ease-in-out infinite alternate; }
@keyframes bd-flicker {
  0% { transform: scale(1, 1) skewX(0); opacity: .95; }
  35% { transform: scale(.92, 1.08) skewX(-5deg); opacity: 1; }
  70% { transform: scale(1.04, .94) skewX(4deg); opacity: .9; }
  100% { transform: scale(.97, 1.04) skewX(-2deg); opacity: 1; }
}
/* confetti: continuous fall + spin, the duration set per piece */
.bd-cf-wrap { position: absolute; inset: 0; }
.bd-cf {
  position: absolute; top: 0; display: block; opacity: 0; will-change: transform, opacity;
  animation: bd-fall calc(var(--dur, 12s) / (var(--bd-speed, 1) * var(--cf-speed, 1))) linear var(--delay, 0s) infinite;
}
.bd-cf svg { display: block; width: 100%; height: auto; animation: bd-spin calc(var(--spin, 3s) / var(--bd-speed, 1)) linear infinite; transform: rotate(var(--rot)); }
@keyframes bd-fall {
  0% { transform: translate(0, -6cqh); opacity: 0; }
  6% { opacity: .95; }
  30% { transform: translate(calc(var(--sx) * 1cqmin), calc((-6 + (var(--y1) + 6) * .3) * 1cqh)); }
  60% { transform: translate(calc(var(--sx) * -.6cqmin), calc((-6 + (var(--y1) + 6) * .6) * 1cqh)); }
  88% { opacity: .95; }
  100% { transform: translate(calc(var(--sx) * .5cqmin), calc(var(--y1) * 1cqh)); opacity: 0; }
}
.bd-cf-top { animation-timing-function: cubic-bezier(.25, .5, .5, 1); }
@keyframes bd-spin { from { transform: rotate(var(--rot)) rotateX(0deg); } to { transform: rotate(calc(var(--rot) + 360deg)) rotateX(360deg); } }

/* motion off (per letter, or the reader's system setting): a still scene */
.bd-still .bd-anim, .bd-still .bd-string, .bd-still .bd-flame, .bd-still .bd-cf svg { animation: none !important; will-change: auto; }
.bd-still .bd-cf { animation: none !important; opacity: .95; transform: translate(0, calc(var(--ys) * 1cqh)); will-change: auto; }
@media (prefers-reduced-motion: reduce) {
  .stage-backdrop .bd-anim, .stage-backdrop .bd-string, .stage-backdrop .bd-flame, .stage-backdrop .bd-cf svg { animation: none !important; }
  .stage-backdrop .bd-cf { animation: none !important; opacity: .95; transform: translate(0, calc(var(--ys) * 1cqh)); }
}

/* ---------- editing in the studio (only while the "Phông nền" section is open) ---------- */
/* the envelope rests so the decorations behind it can be picked; the studio's own buttons keep working */
.studio-stage.bd-editing > .studio-scene { pointer-events: none; }
.studio-stage.bd-editing .stage-backdrop .bd-item:not(.bd-field) { pointer-events: auto; cursor: move; touch-action: none; }
.studio-stage.bd-editing .stage-backdrop .bd-item:not(.bd-field):hover { outline: 1.5px dashed rgba(142, 47, 69, .5); outline-offset: 2px; }
.studio-stage.bd-editing .stage-backdrop .bd-item.bd-off { display: block; opacity: .3; }
.studio-stage.bd-dragging .stage-backdrop .bd-anim { animation-play-state: paused; }
.studio-stage.bd-editing .stage-help { visibility: hidden; }
.bd-edit-hint {
  display: none; position: absolute; top: 14px; left: 16px; z-index: 3; pointer-events: none;
  font-size: .78rem; color: #fff; background: rgba(29, 27, 26, .78); padding: 5px 11px; border-radius: 999px;
}
.studio-stage.bd-editing .bd-edit-hint { display: block; }
/* small screens: no hint pill (like the studio's own help); the panel says the same */
@media (max-width: 900px) { .studio-stage.bd-editing .bd-edit-hint { display: none; } }
/* the selection frame floats above the envelope (under the studio's bars), so a decoration behind it can still be moved */
.bd-giz-layer { position: absolute; inset: 0; z-index: 2; pointer-events: none; container-type: size; }
.bd-giz-layer[hidden] { display: none; }
.bd-giz-box {
  position: absolute; transform: translate(-50%, -50%) rotate(var(--bd-r, 0deg));
  outline: 1.5px solid var(--wine, #8e2f45); outline-offset: 3px; pointer-events: auto; cursor: move; touch-action: none;
  background: rgba(142, 47, 69, .04);
}
.bd-h {
  position: absolute; pointer-events: auto; width: 14px; height: 14px; border-radius: 3px;
  background: #fff; border: 1.5px solid var(--wine, #8e2f45); box-shadow: 0 1px 3px rgba(0, 0, 0, .2);
}
.bd-h[data-bdh="scale"] { right: -11px; bottom: -11px; cursor: nwse-resize; }
.bd-h-rot { right: -13px; top: -30px; width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; color: var(--wine, #8e2f45); cursor: grab; }
.bd-giz-bar { position: absolute; left: 50%; top: -42px; transform: translateX(-50%); display: flex; gap: 2px; padding: 3px; border-radius: 999px; background: #1d1b1a; pointer-events: auto; box-shadow: 0 4px 12px rgba(0, 0, 0, .25); }
.bd-giz-bar button { width: 26px; height: 26px; border: 0; border-radius: 50%; background: none; color: #fff; display: grid; place-items: center; cursor: pointer; }
.bd-giz-bar button:hover { background: rgba(255, 255, 255, .15); }
.bd-giz-bar button.del:hover { background: #b3261e; }
@media (pointer: coarse) { .bd-h { width: 22px; height: 22px; } .bd-h[data-bdh="scale"] { right: -15px; bottom: -15px; } .bd-h-rot { width: 28px; height: 28px; top: -38px; } .bd-giz-bar { top: -54px; } }

/* ---------- panel ---------- */
.bd-themes { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 8px; }
.bd-theme { display: flex; flex-direction: column; gap: 6px; align-items: stretch; padding: 6px; border: 1.5px solid var(--line, #e6ddd3); border-radius: 12px; background: #fff; cursor: pointer; font: 500 .8rem var(--sans); color: inherit; text-align: center; }
.bd-theme.on { border-color: var(--wine, #8e2f45); box-shadow: 0 0 0 3px var(--wine-soft, rgba(142, 47, 69, .12)); }
.bd-theme-pv { display: block; aspect-ratio: 64 / 44; border-radius: 8px; overflow: hidden; background: #f6f2ec; }
.bd-theme-pv svg { display: block; width: 100%; height: 100%; }
.bd-theme-pv.none { background: repeating-linear-gradient(135deg, #f3eee8 0 6px, #fbf8f4 6px 12px); }
.bd-switches { display: grid; gap: 10px; margin: 16px 0 4px; }
.bd-list { display: grid; gap: 4px; }
.bd-row { display: flex; align-items: center; gap: 6px; padding: 4px 6px; border-radius: 10px; border: 1px solid transparent; }
.bd-row.on { border-color: var(--wine, #8e2f45); background: var(--wine-soft, rgba(142, 47, 69, .06)); }
.bd-row.off .bd-row-name { opacity: .45; }
.bd-row-on input { width: 16px; height: 16px; accent-color: var(--wine, #8e2f45); }
.bd-row-name { flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; border: 0; background: none; padding: 6px 2px; text-align: left; cursor: pointer; font: 500 .86rem var(--sans); color: inherit; }
.bd-row-name span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bd-row-name small { color: var(--muted, #8a7f78); font-weight: 400; }
.bd-row .dot { width: 12px; height: 12px; border-radius: 50%; flex: none; box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .12); }
.bd-ib { width: 28px; height: 28px; border: 0; border-radius: 8px; background: none; color: #776963; display: grid; place-items: center; cursor: pointer; }
.bd-ib:hover { background: #f1ebe4; }
.bd-ib.del:hover { color: #b3261e; }
.bd-add { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin: 10px 0 4px; }
.bd-add .chip { font-size: .8rem; padding: 5px 10px; }
.bd-insp { margin-top: 14px; padding: 12px; border-radius: 14px; background: #faf7f3; border: 1px solid var(--line, #ece4da); }
.bd-insp .lbl:first-of-type { margin-top: 4px; }
.bd-insp-h { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.bd-insp-h span { display: flex; gap: 2px; }
.bd-insp-h .btn { padding: 6px 8px; }
.bd-field-in { margin: 12px 0 0; }
.bd-hint { margin: 0; }
.bd-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }
.bd-actions label.btn { cursor: pointer; }
.bd-presets { display: flex; flex-wrap: wrap; gap: 6px; }
.bd-preset { display: inline-flex; align-items: center; gap: 2px; padding: 0 4px 0 0; }
.bd-preset button { border: 0; background: none; cursor: pointer; font: inherit; color: inherit; padding: 5px 4px 5px 10px; }
.bd-preset .x { padding: 2px 6px; opacity: .55; }
.bd-preset .x:hover { opacity: 1; color: #b3261e; }

/* Sinh nhật (giấy & sticker): a lighter grain and a warm paper-white light in the middle, like the journal page */
.bd-theme-birthday .bd-grain { opacity: .16; }
.bd-theme-birthday .bd-veil { background: radial-gradient(ellipse 34% 38% at 50% 47%, rgba(255, 253, 248, .7), rgba(255, 253, 248, 0) 100%); }
