:root {
  --cream: #fdf3e3;
  --peach: #fbd9b4;
  --bark: #7a4a2b;
  --bark-dark: #5e3820;
  --ink: #4a2c1a;
  --muted: #8b6a52;
  --pumpkin: #e8742b;
  --cranberry: #b8322f;
  --gold: #f2b134;
}

* { box-sizing: border-box; }

body {
  --on-sky: var(--bark-dark);
  --on-sky-muted: var(--muted);
  margin: 0;
  min-height: 100vh;
  font-family: "Fredoka", system-ui, sans-serif;
  color: var(--ink);
  background: var(--cream);
  overflow-x: hidden;
}

.sky { position: fixed; inset: 0; z-index: 0; pointer-events: none; transition: background 1s; }
body[data-sky="day"] .sky { background: linear-gradient(#9fd3ef 0%, #cfeaf6 45%, #fdf3e3 100%); }
body[data-sky="sunset"] .sky { background: linear-gradient(#f39a6b 0%, #f9c18e 38%, #fde2c0 70%, #fdf3e3 100%); }
body[data-sky="night"] .sky { background: linear-gradient(#141a3a 0%, #2b2b58 45%, #56426d 80%, #75576c 100%); }
body[data-sky="night"] { --on-sky: #fdf3e3; --on-sky-muted: #d8cbe6; }
.orb { position: absolute; border-radius: 50%; transition: all 1s; }
body[data-sky="day"] .orb { width: 90px; height: 90px; left: 8%; top: 6%; background: #fff6c9; box-shadow: 0 0 60px 20px rgba(255, 240, 180, .7); }
body[data-sky="sunset"] .orb { width: 220px; height: 220px; left: 6%; top: 46%; background: #ffcf7a; box-shadow: 0 0 120px 60px rgba(255, 170, 90, .45); opacity: .85; }
body[data-sky="night"] .orb { width: 70px; height: 70px; left: 85%; top: 8%; background: #fbf3d9; box-shadow: inset -14px -6px 0 #e9dfbe, 0 0 40px 10px rgba(251, 243, 217, .25); }
.stars { position: absolute; inset: 0; opacity: 0; transition: opacity 1s; }
body[data-sky="night"] .stars { opacity: 1; }
.stars i { position: absolute; width: var(--size); height: var(--size); border-radius: 50%; background: #fff; animation: twinkle 4s ease-in-out infinite alternate; }

.toolbar { position: absolute; right: 20px; top: 16px; display: flex; gap: 10px; align-items: center; }
.sky-switch { display: flex; padding: 3px; border-radius: 99px; background: rgba(255, 250, 242, .85); border: 2px solid #f1d3ad; }
.sky-switch button { font-size: 14px; padding: 6px 12px; color: var(--muted); background: none; box-shadow: none; }
.sky-switch button[aria-pressed="true"] { background: var(--pumpkin); color: #fff; }
.sky-switch button:active { transform: none; }

header { text-align: center; padding: 40px 20px 8px; position: relative; z-index: 1; }
.eyebrow { margin: 0; font-weight: 600; letter-spacing: .16em; text-transform: uppercase; font-size: 13px; color: var(--pumpkin); }
h1 { margin: 6px 0 8px; font-size: clamp(36px, 6vw, 60px); font-weight: 700; color: var(--on-sky); }
.sub { margin: 0 auto; max-width: 560px; color: var(--on-sky-muted); font-size: 17px; line-height: 1.45; }

main {
  position: relative; z-index: 1;
  display: grid; grid-template-columns: minmax(300px, 380px) minmax(320px, 760px);
  gap: 40px; justify-content: center; align-items: start;
  padding: 24px 20px 40px;
}

.maker {
  background: #fffaf2; border: 3px solid #f1d3ad; border-radius: 28px;
  padding: 26px; box-shadow: 0 10px 0 #efcfa6;
}

.prompt { display: block; font-family: "Caveat", cursive; font-size: 34px; font-weight: 700; color: var(--bark); }
#gratitude {
  display: block; width: 100%; margin: 8px 0 16px; padding: 12px 14px; resize: none;
  font: 500 20px/1.4 "Fredoka", sans-serif; color: var(--ink);
  border: 3px dashed #e3b98a; border-radius: 16px; background: #fffdf8; outline: none;
}
#gratitude:focus { border-bottom-color: var(--pumpkin); }
#gratitude::placeholder { color: #c9a98b; }

button {
  font: 600 18px "Fredoka", sans-serif; cursor: pointer;
  border: none; border-radius: 999px; padding: 12px 22px;
  color: #fff; background: var(--pumpkin); box-shadow: 0 5px 0 #b9541a;
  transition: transform .1s, box-shadow .1s, opacity .2s;
}
button:hover { transform: translateY(-1px); }
button:active { transform: translateY(4px); box-shadow: 0 1px 0 #b9541a; }
button:disabled { opacity: .65; cursor: progress; transform: none; }
#submit { width: 100%; }

.preview { margin-top: 8px; display: flex; flex-direction: column; align-items: center; gap: 14px; }

button.secondary { background: #fff; color: var(--pumpkin); border: 3px solid var(--pumpkin); box-shadow: 0 5px 0 #f1cfa6; }
button.secondary:active { box-shadow: 0 1px 0 #f1cfa6; }
button.link { background: none; box-shadow: none; color: var(--muted); font-size: 15px; padding: 4px 8px; text-decoration: underline; }
button.link:active { transform: none; }
.idea-link { display: block; margin: -8px 0 4px auto; }

.camera, .captured { width: 100%; display: flex; flex-direction: column; align-items: center; gap: 12px; }
.camera[hidden], .captured[hidden] { display: none; }
.step-label { margin: 0; font-weight: 600; color: var(--bark); }
.cam-stage {
  position: relative; width: 100%; aspect-ratio: 4 / 3; border-radius: 20px; overflow: hidden;
  background: repeating-linear-gradient(45deg, #fbead3 0 14px, #f8e2c6 14px 28px); border: 3px dashed #e3b98a;
}
.cam-stage video, .cam-stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.cam-stage video { transform: scaleX(-1); visibility: hidden; }
.camera.live .cam-stage video { visibility: visible; }
.cam-hint {
  position: absolute; left: 50%; bottom: 10px; transform: translateX(-50%); margin: 0; width: max-content; max-width: 90%;
  padding: 6px 12px; border-radius: 99px; background: rgba(255, 250, 242, .92); font-size: 14px; font-weight: 500; text-align: center;
}

.big-leaf { position: relative; width: 240px; height: 288px; --leaf: var(--pumpkin); transition: transform .3s; }
.big-leaf svg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.big-leaf .blade { fill: var(--leaf); stroke: rgba(0,0,0,.12); stroke-width: 1.5; transition: fill .4s; }
.big-leaf .vein { fill: none; stroke: rgba(255,255,255,.18); stroke-width: 1.6; stroke-linecap: round; }
.big-leaf .stem { fill: none; stroke: var(--bark); stroke-width: 3; stroke-linecap: round; }
.hand-fill { fill: var(--leaf); stroke: var(--leaf); stroke-linejoin: round; transition: fill .4s, stroke .4s; }
.hand-line { fill: none; stroke: var(--leaf); stroke-linecap: round; stroke-linejoin: round; transition: stroke .4s; }
.big-leaf.thinking { animation: wiggle .5s ease-in-out infinite alternate; }

.leaf-text {
  position: absolute;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; color: #fff; text-shadow: 0 1px 2px rgba(0,0,0,.25);
  font-family: "Caveat", cursive; line-height: 1.05; overflow: hidden;
}
.big-leaf { container-type: inline-size; }
.leaf-lead { font-size: 7cqw; opacity: .9; }
.leaf-body { font-size: 10.5cqw; font-weight: 700; word-break: break-word; }
.leaf-body.medium { font-size: 8cqw; }
.leaf-body.long { font-size: 6.5cqw; }

.result { text-align: center; animation: pop .35s ease-out; }
.score { font-size: 46px; font-weight: 700; color: var(--cranberry); line-height: 1; }
.score small { font-size: 18px; color: var(--muted); margin-left: 2px; }
.verdict { margin: 6px 0 2px; font-weight: 600; font-size: 18px; }
.meta { margin: 0 0 14px; color: var(--muted); font-size: 14px; }
#hang { background: var(--cranberry); box-shadow: 0 5px 0 #7d1f1d; }
#hang:active { box-shadow: 0 1px 0 #7d1f1d; }
.error { margin: 0; color: var(--cranberry); font-weight: 500; text-align: center; }

.tree-wrap { position: relative; }
.count { text-align: center; margin: 0 0 4px; font-weight: 600; color: var(--on-sky); font-size: 18px; }
.legend { display: flex; align-items: center; justify-content: center; gap: 8px; margin: 0 0 4px; font-size: 13px; color: var(--on-sky-muted); }
.legend i { width: 120px; height: 10px; border-radius: 99px; background: linear-gradient(90deg, #8b7560, #f2b134, #d2402a); }
.tree { position: relative; width: 100%; max-width: 720px; margin: 0 auto; aspect-ratio: 1; }
.tree svg { position: absolute; inset: 0; width: 100%; height: 100%; }
.trunk { fill: var(--bark); }
.bark { fill: none; stroke: var(--bark-dark); stroke-width: 4; stroke-linecap: round; opacity: .5; }
.branches path { fill: none; stroke: var(--bark); stroke-linecap: round; }
.ground { fill: #e7c595; }
.empty { position: absolute; left: 0; right: 0; top: 12%; text-align: center; font-family: "Caveat", cursive; font-size: 28px; color: var(--on-sky-muted); margin: 0; }

.leaves { position: absolute; inset: 0; pointer-events: none; }
.tree-leaf { pointer-events: auto; }
.tree-leaf {
  container-type: inline-size;
  position: absolute; width: 18%; aspect-ratio: 100 / 120;
  transform: translate(-50%, -50%) rotate(var(--rot));
  transform-origin: 50% 85%; animation: sway 3.2s ease-in-out infinite alternate; animation-delay: var(--delay);
}
.tree-leaf:hover, .tree-leaf:focus-within { transform: translate(-50%, -50%) rotate(var(--rot)) scale(1.5); z-index: 3; }
.leaf-face, .leaf-face:hover, .leaf-face:active {
  position: absolute; inset: 0; padding: 0; border-radius: 0; background: none; box-shadow: none; outline: none;
  transform: none; touch-action: none; cursor: grab;
}
.tree-leaf.held .leaf-face { cursor: grabbing; }
.tree-leaf.wiggle svg, .pile-leaf.wiggle svg { animation: wig .45s ease-out; transform-origin: 50% 92%; }

.pile { position: absolute; inset: 0; pointer-events: none; }
.pile-leaf {
  container-type: inline-size; position: absolute; width: 8%; aspect-ratio: 100 / 120; pointer-events: auto;
  transform: translate(-50%, -50%) rotate(var(--rot));
}
.pile-leaf .leaf-face { cursor: pointer; }
.pile-leaf .words { display: none; }
.pile-leaf svg { width: 100%; height: 100%; overflow: visible; filter: drop-shadow(0 1px 1px rgba(90, 50, 20, .3)); }
.pile-leaf .blade { fill: var(--leaf); }
.pile-leaf .vein { display: none; }
.pile-leaf .stem { fill: none; stroke: var(--bark); stroke-width: 5; stroke-linecap: round; }
.pile-leaf:hover { z-index: 3; }
.pile-leaf:hover .leaf-delete, .pile-leaf:focus-within .leaf-delete { opacity: 1; }
.pile-leaf .leaf-delete { width: 18px; height: 18px; font-size: 13px; top: -10px; right: -10px; }
.pile-leaf.tumbling { transition: left 1.5s ease-out, top 1.5s cubic-bezier(.55, 0, 1, .45); animation: tumble 1.5s ease-in; z-index: 4; }
.leaf-delete {
  position: absolute; top: 0; right: 0; width: 22px; height: 22px; padding: 0; border-radius: 50%;
  font: 700 16px/1 "Fredoka"; color: var(--cranberry); background: #fff; box-shadow: 0 2px 0 #e3b98a;
  opacity: 0; transition: opacity .15s;
}
.tree-leaf:hover .leaf-delete, .tree-leaf:focus-within .leaf-delete { opacity: 1; }
@media (hover: none) { .leaf-delete { opacity: 1; } }
.tree-leaf svg { width: 100%; height: 100%; overflow: visible; filter: drop-shadow(0 2px 1px rgba(90, 50, 20, .25)); }
.tree-leaf .blade { fill: var(--leaf); }
.tree-leaf .vein { display: none; }
.tree-leaf .stem { fill: none; stroke: var(--bark); stroke-width: 5; stroke-linecap: round; }
.tree-leaf .words {
  position: absolute; display: grid; place-items: center; overflow: hidden;
  font: 700 12cqw/1 "Caveat", cursive; color: #fff; text-align: center; overflow-wrap: break-word; hyphens: auto;
  text-shadow: 0 1px 2px rgba(0,0,0,.3);
}
.tree-leaf .words.medium { font-size: 10.5cqw; }
.tree-leaf .words.long { font-size: 8.5cqw; }
.tree-leaf.fresh { animation: attach .6s ease-out, sway 3.2s ease-in-out .6s infinite alternate; }

.popover {
  position: absolute; z-index: 5; width: 240px; padding: 14px 16px;
  background: #fffaf2; border: 3px solid #f1d3ad; border-radius: 18px; box-shadow: 0 6px 0 #efcfa6;
  transform: translate(-50%, calc(-100% - 18px)); pointer-events: none;
}
.popover .score-line { margin: 0 0 8px; font-weight: 600; }
.popover .lead { margin: 0; font-family: "Caveat", cursive; font-size: 18px; color: var(--muted); }
.popover .body { margin: 0 0 8px; font-family: "Caveat", cursive; font-size: 24px; font-weight: 700; line-height: 1.1; word-break: break-word; }
.popover .bar { height: 8px; border-radius: 99px; background: #f1dcc2; overflow: hidden; }
.popover .bar span { display: block; height: 100%; background: linear-gradient(90deg, var(--gold), var(--cranberry)); }
.popover .meta { margin: 6px 0 0; font-size: 13px; }

.flying { position: fixed; z-index: 50; pointer-events: none; margin: 0; }

.falling { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 0; }
.falling span { position: absolute; top: -40px; width: 22px; height: 26px; opacity: .55; animation: fall linear infinite; }
.falling svg { width: 100%; height: 100%; }

footer { text-align: center; padding: 10px 20px 30px; color: var(--on-sky-muted); font-size: 14px; position: relative; z-index: 1; }
footer a { color: var(--pumpkin); }

@keyframes wiggle { from { transform: rotate(-4deg); } to { transform: rotate(4deg); } }
@keyframes pop { from { opacity: 0; transform: scale(.9); } to { opacity: 1; transform: none; } }
@keyframes sway { from { rotate: -4deg; } to { rotate: 4deg; } }
@keyframes attach { 0% { scale: 1.6; } 60% { scale: .9; } 100% { scale: 1; } }
@keyframes fall {
  0% { transform: translate(0, 0) rotate(0); }
  50% { transform: translate(60px, 55vh) rotate(180deg); }
  100% { transform: translate(-20px, 110vh) rotate(360deg); }
}

.cam-progress { position: absolute; left: 0; right: 0; top: 0; height: 6px; background: rgba(255, 250, 242, .6); }
.cam-progress span { display: block; width: 0; height: 100%; background: var(--pumpkin); transition: width .1s linear; }

.play-bar { display: flex; justify-content: center; flex-wrap: wrap; gap: 10px; margin: 8px 0 0; }
button.small { font-size: 15px; padding: 8px 16px; }
.pip {
  position: absolute; z-index: 4; left: 0; top: 70px; width: 170px; border-radius: 14px; overflow: hidden;
  background: #fffaf2; border: 3px solid #f1d3ad; box-shadow: 0 4px 0 #efcfa6;
}
.pip[hidden] { display: none; }
.pip video, .pip canvas { display: block; width: 100%; aspect-ratio: 4 / 3; object-fit: cover; }
.pip video { transform: scaleX(-1); }
.pip canvas { position: absolute; left: 0; top: 0; }
.pip p { margin: 0; padding: 6px 8px; font-size: 12px; line-height: 1.3; color: var(--muted); }
.hand-layer { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 5; }

.tree-leaf.held { transform: translate(-50%, -50%) scale(2.1); z-index: 6; animation: none; transition: none; }
.tree-leaf.held .leaf-delete { display: none; }
.tree-leaf.returning { transition: left .6s cubic-bezier(.3, 1.4, .5, 1), top .6s cubic-bezier(.3, 1.4, .5, 1); }

.tree.shaking > svg, .tree.shaking .leaves { animation: shake .9s ease-in-out; transform-origin: 50% 100%; }
.tree.shaking .tree-leaf { animation: flutter .12s ease-in-out 7 alternate; }
.debris { position: absolute; width: 4%; aspect-ratio: 100 / 120; pointer-events: none; animation: drop 1.8s ease-in forwards; z-index: 2; }
.debris svg { width: 100%; height: 100%; }

@keyframes wig { 0% { rotate: 0deg; } 30% { rotate: -12deg; } 60% { rotate: 8deg; } 100% { rotate: 0deg; } }
@keyframes tumble { from { rotate: 0deg; } to { rotate: 540deg; } }
@keyframes twinkle { from { opacity: .3; } to { opacity: 1; } }
@keyframes shake {
  0%, 100% { transform: rotate(0); }
  15% { transform: rotate(-2.5deg); }
  35% { transform: rotate(2.2deg); }
  55% { transform: rotate(-1.6deg); }
  75% { transform: rotate(1deg); }
}
@keyframes flutter { from { rotate: -14deg; } to { rotate: 14deg; } }
@keyframes drop {
  0% { transform: translate(0, 0) rotate(0); opacity: 1; }
  100% { transform: translate(30px, 70vh) rotate(300deg); opacity: 0; }
}

@media (max-width: 820px) {
  main { grid-template-columns: 1fr; gap: 28px; }
  .toolbar { position: static; justify-content: center; margin-bottom: 12px; flex-wrap: wrap; }
  .tree-leaf { width: 20%; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

.room-bar { display: flex; flex-direction: column; align-items: center; gap: 6px; margin: 0 0 10px; }
.room-solo, .room-live { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 8px; }
.room-solo[hidden], .room-live[hidden] { display: none; }
.room-or { color: var(--on-sky-muted); font-size: 14px; }
.room-join { display: flex; gap: 6px; }
.room-join input {
  width: 92px; font: 600 16px "Fredoka", sans-serif; letter-spacing: .12em; text-transform: uppercase; text-align: center;
  color: var(--ink); background: #fffaf2; border: 3px solid #f1d3ad; border-radius: 999px; padding: 6px 10px;
}
.room-join input:focus { outline: none; border-color: var(--pumpkin); }
.room-name { color: var(--on-sky); font-size: 16px; }
.room-name b { letter-spacing: .12em; background: #fffaf2; color: var(--ink); border-radius: 8px; padding: 2px 8px; }
.room-people { display: inline-flex; align-items: center; gap: 4px; color: var(--on-sky-muted); font-size: 14px; }
.room-people i { width: 12px; height: 12px; border-radius: 50%; background: var(--peer); border: 2px solid #fffaf2; }
.room-live button.link { color: var(--on-sky-muted); }
.room-status { margin: 0; font-size: 14px; color: var(--on-sky-muted); }
.room-status[hidden] { display: none; }
.tree-leaf.remote-held { transform: translate(-50%, -50%) scale(1.8); z-index: 6; animation: none; transition: left .06s linear, top .06s linear; }
.tree-leaf.remote-held svg { filter: drop-shadow(0 0 4px var(--peer)) drop-shadow(0 0 2px var(--peer)); }
.tree-leaf.remote-held .leaf-delete { display: none; }
