@import url('https://fonts.googleapis.com/css2?family=Sarabun:wght@400;500;600;700&family=Cormorant+Garamond:wght@500;600&display=swap');

:root {
  --navy: #4a01c3;
  --navy-deep: #1a0538;
  --gold: #FFDA00;
  --amber: #92400e;
  --orange: #f97316;
}

*, *::before, *::after { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: 'Sarabun', sans-serif;
  background: var(--navy-deep);
  color: #fff;
  -webkit-tap-highlight-color: transparent;
}
button { font-family: inherit; cursor: pointer; border: none; background: none; }
img { max-width: 100%; display: block; user-select: none; -webkit-user-drag: none; }
a { color: inherit; }

.app {
  position: relative;
  width: 100%;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: var(--navy-deep);
}

.scene-wrap {
  width: 100%;
  height: 100dvh;
  overflow: hidden;
  position: relative;
  display: flex;
  justify-content: center;
  background: var(--navy);
}

.scene-stage {
  --stage-h: 100%;
  --crop-y: 0px;
  --lift: 0px;
  --crop-x: calc((100dvh * 1920 / 900 - 100vw) / 2);
  position: absolute;
  left: 50%;
  bottom: 0;
  aspect-ratio: 1920 / 900;
  height: var(--stage-h);
  transform: translateX(-50%);
}

@media (max-aspect-ratio: 1920/900) {
  .scene-stage {
    --stage-h: max(60dvh, calc(100vw * 900 / 1920));
    --lift: calc(var(--stage-h) * 0.3);
    --crop-y: max(0px, calc(100vw * 900 / 1920 - 60dvh));
    --crop-x: max(0px, calc((var(--stage-h) * 1920 / 900 - 100vw) / 2));
  }
}

/* Layer stack:
   1  = full background (1920×900)
   20 = water krathongs
*/
.layer-img {
  position: absolute;
  pointer-events: none;
  z-index: 1;
}
.layer-img img {
  width: 100%;
  height: auto;
}
.layer-bg {
  left: -100%;
  top: 0;
  width: 300%;
  height: 100%;
  background-image: url('../assets/scene/bg-main.png');
  background-repeat: repeat-x;
  background-position: center bottom;
  background-size: auto 100%;
}
.layer-front {
  z-index: 40;
}

/* พญานาค — ชิดซ้าย สูง 620px (ไม่ดันติดลบซ้าย เพื่อไม่โดน overflow ตัด) */
.naga-deco {
  left: 15px;
  bottom: 0;
  width: auto;
  height: 620px;
  padding-bottom: 15px;
  box-sizing: content-box;
  z-index: 42;
  pointer-events: none;
}
.naga-deco img {
  width: auto;
  height: 620px;
  max-width: none;
  display: block;
  filter: drop-shadow(0 10px 18px rgb(0 0 0 / .45));
}
@media (max-width: 767px) {
  .naga-deco {
    left: 15px;
    bottom: 0;
    height: 320px;
    padding-bottom: 15px;
  }
  .naga-deco img { height: 320px; }
}

.pier-dock {
  left: 50%;
  bottom: -1%;
  width: min(22%, 280px);
  transform: translateX(-50%);
  z-index: 45;
  pointer-events: none;
}
.pier-dock img {
  width: 100%;
  height: auto;
  display: block;
  filter: drop-shadow(0 6px 10px rgb(0 0 0 / .35));
}
@media (max-width: 767px) {
  .pier-dock { width: min(42%, 220px); }
}

.flip-x { transform: scaleX(-1); }
.flip-y { transform: scaleY(-1); }

.scene-water-ext {
  display: none;
  z-index: 1;
}
@media (max-aspect-ratio: 1920/900) {
  .scene-water-ext {
    display: block;
    position: absolute;
    left: 0;
    top: calc(100% - 1px);
    width: 100%;
    height: calc(var(--lift) + 2px);
    overflow: hidden;
    z-index: 1;
    background: linear-gradient(180deg, #3a0a6e 0%, #1a0538 100%);
  }
}

/* ===== Type 1: กระทงลอยน้ำ — อยู่ในโซนน้ำเท่านั้น ===== */
.water-zone {
  position: absolute;
  bottom: 0;
  left: 50%;
  width: 100vw;
  /* พื้นหลังใหม่: ผิวน้ำประมาณครึ่งล่างของภาพ 1920×900 */
  top: 52%;
  transform: translateX(-50%);
  z-index: 20;
  overflow: hidden;
  pointer-events: none;
}
.water-fx {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 1;
}
.moon-reflection {
  position: absolute;
  top: 0;
  left: 50%;
  margin-left: -7rem;
  width: 14rem;
  height: 75%;
  background: radial-gradient(45% 100% at 50% 0%, rgba(255,240,200,.34) 0%, rgba(255,240,200,.12) 45%, rgba(255,240,200,0) 75%);
  transform-origin: top center;
  animation: moon-reflection 4.5s ease-in-out infinite;
}
.water-glint {
  position: absolute;
  inset: 0;
  animation: water-glint 7s ease-in-out infinite;
}
.water-glint span {
  position: absolute;
  height: 2px;
  border-radius: 999px;
  background: linear-gradient(90deg, transparent, rgba(255,244,214,.55), transparent);
}

.lanes {
  position: absolute;
  left: 0;
  right: 0;
  /* เว้นขอบบนเล็กน้อย ไม่ให้กระทงแถวไกลโผล่พ้นผิวน้ำ */
  top: 12%;
  bottom: 2%;
  z-index: 2;
  pointer-events: none;
}
.lane {
  position: absolute;
  left: 0;
  right: 0;
  pointer-events: none;
}
/* ไกล → ใกล้: ยิ่งไกลยิ่งเล็ก / อยู่บนกว่า */
#lane-t {
  top: 0;
  height: 26%;
}
#lane-m {
  top: 24%;
  height: 30%;
}
#lane-b {
  top: 50%;
  height: 50%;
}

.float-item {
  position: absolute;
  bottom: 0;
  will-change: left;
  cursor: pointer;
  pointer-events: auto;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.float-item .bob {
  width: 100%;
  height: 100%;
  animation: water-bob 4s ease-in-out .25s infinite;
  transform-origin: 50% 100%;
}
.float-item img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: bottom center;
  filter: drop-shadow(0 6px 8px rgba(0, 20, 60, .35));
  transition: filter .2s, transform .2s;
}
.float-item:hover img { filter: brightness(1.08) drop-shadow(0 6px 8px rgba(0, 20, 60, .35)); transform: scale(1.04); }

/* ระยะไกล / กลาง / ใกล้ */
.float-item.lane-t {
  width: 4.5rem;
  height: 4.5rem;
  z-index: 21;
  opacity: .88;
  filter: brightness(.82);
}
.float-item.lane-m {
  width: 6rem;
  height: 6rem;
  z-index: 22;
  opacity: .95;
  filter: brightness(.92);
}
.float-item.lane-b {
  width: 7.5rem;
  height: 7.5rem;
  z-index: 23;
  filter: brightness(1);
}
@media (min-width: 768px) {
  .float-item.lane-t { width: 5.5rem; height: 5.5rem; }
  .float-item.lane-m { width: 7.25rem; height: 7.25rem; }
  .float-item.lane-b { width: 9rem; height: 9rem; }
}

/* ชั้นบอลลูน — fixed บน body นอก .app (เหนือฉาก, ต่ำกว่า modal z-80) */
.bubble-layer {
  position: fixed;
  inset: 0;
  z-index: 70;
  pointer-events: none;
  overflow: visible;
}
.bubble {
  position: absolute;
  background: #fff;
  color: #1f2937;
  padding: .5rem .75rem;
  border-radius: .75rem;
  font-size: .75rem;
  line-height: 1.35;
  min-width: 120px;
  max-width: 200px;
  box-shadow: 0 6px 20px rgba(0,0,0,.45);
  pointer-events: none;
  z-index: 71;
  /* ใช้ --bubble-x/y จัดตำแหน่ง + คง transform กึ่งกลางไว้ตลอด ไม่ให้ animation แย่ง */
  left: var(--bubble-x, 0px);
  top: var(--bubble-y, 0px);
  transform: translate(-50%, calc(-100% - 12px));
  animation: bubble-in .22s ease;
  will-change: opacity, transform;
}
.bubble::after {
  content: "";
  position: absolute;
  left: 50%;
  /* ดึงขึ้น 1px ให้ชนขอบบอลลูน ไม่มีช่องว่าง */
  bottom: -5px;
  transform: translateX(-50%);
  border: 6px solid transparent;
  border-top-color: #fff;
  border-bottom: 0;
}
.bubble strong {
  display: block;
  margin-top: .2rem;
  color: #6b7280;
  font-weight: 500;
  font-size: .7rem;
}
/* บอลลูนบนสะพาน (พรีวิว) */
.bubble--pier,
.pier-preview .bubble {
  left: 50%;
  bottom: 92%;
  top: auto;
  transform: translateX(-50%);
  animation: none;
}

.stars { position: absolute; inset: 0; pointer-events: none; z-index: 3; }
.star-group { position: absolute; inset: 0; animation: star-twinkle 3s ease-in-out infinite; }
.star {
  position: absolute;
  border-radius: 50%;
  background: #fff;
}

.fireflies {
  position: fixed;
  inset-inline: 0;
  bottom: 0;
  height: 50%;
  z-index: 12;
  pointer-events: none;
}
.firefly {
  position: absolute;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: rgb(255,246,200);
  box-shadow: 0 0 6px 2px rgba(255,236,150,.8);
  animation: firefly 9s ease-in-out infinite;
}

.title-area {
  position: absolute;
  inset: 0;
  height: 50%;
  display: flex;
  justify-content: center;
  pointer-events: none;
  z-index: 10;
}
.title-inner {
  width: 100%;
  max-width: 14rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: .75rem;
  padding: 4.5rem 1.5rem 1.5rem;
  filter: drop-shadow(0 4px 12px rgb(0 0 0 / .45));
}
@media (min-width: 768px) {
  .title-inner { padding-top: 1.25rem; max-width: 16rem; }
}
.title-inner img { width: 100%; height: auto; }

.muni-logo {
  position: absolute;
  top: 0.75rem;
  right: 1rem;
  z-index: 12;
  display: block;
  width: min(220px, 42vw);
  pointer-events: auto;
  filter: drop-shadow(0 2px 6px rgb(0 0 0 / .35));
  transition: opacity 0.15s, transform 0.15s;
}
.muni-logo img {
  width: 100%;
  height: auto;
  display: block;
}
.muni-logo:hover {
  opacity: 0.92;
  transform: scale(1.02);
}
@media (min-width: 768px) {
  .muni-logo {
    top: 1rem;
    right: 1.5rem;
    width: min(280px, 22vw);
  }
}

.counter {
  position: absolute;
  left: 1.5rem;
  top: 1rem;
  z-index: 10;
  pointer-events: none;
  white-space: nowrap;
  font-size: 1rem;
  text-shadow: 0 1px 3px rgb(0 0 0 / .8), 0 0 14px rgb(0 20 60 / .9);
}
.counter span {
  padding: 0 .5rem;
  font-size: 2.5rem;
  font-weight: 700;
}
@media (max-width: 767px) {
  .counter { left: 50%; transform: translateX(-50%); top: auto; bottom: auto; }
  .title-inner .mobile-counter { display: block; color: #fff; text-shadow: 0 1px 3px rgb(0 0 0 / .8); }
  .counter.desktop { display: none; }
}
@media (min-width: 768px) {
  .title-inner .mobile-counter { display: none; }
}

.cta-wrap {
  position: fixed;
  bottom: 1.25rem;
  z-index: 50;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: .5rem;
  padding: 0 1.5rem;
}
.btn-primary {
  padding: .55rem 1.75rem;
  min-width: 150px;
  border-radius: 999px;
  background: linear-gradient(to top, #f97316, #FFDA00);
  color: #92400e;
  font-size: 1.5rem;
  font-weight: 600;
  text-align: center;
  box-shadow: 0 4px 14px rgba(0,0,0,.25);
  transition: transform .2s, filter .2s;
  z-index: 60;
}
.btn-primary:hover { filter: brightness(1.05); transform: scale(1.08); }
.btn-primary:disabled {
  background: linear-gradient(to top, #94a3b8, #cbd5e1);
  color: #475569;
  cursor: not-allowed;
  transform: none;
  filter: none;
}
.btn-primary span { filter: drop-shadow(0 1px 0 rgba(255,255,255,.3)); }

.preview-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  gap: .5rem;
  min-width: 220px;
}
.preview-bar .icon-btn {
  position: absolute;
  bottom: .25rem;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 50%;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}
.preview-bar .icon-btn.edit { left: -1.75rem; background: #2563eb; }
.preview-bar .icon-btn.trash { right: -1.75rem; background: #dc2626; }
@media (min-width: 768px) {
  .preview-bar .icon-btn.edit { left: .5rem; }
  .preview-bar .icon-btn.trash { right: .5rem; }
}

.pier-preview {
  position: absolute;
  left: 50%;
  bottom: 12%;
  transform: translateX(-50%);
  width: min(140px, 22vw);
  z-index: 62;
  pointer-events: none;
}
.pier-preview img {
  width: 100%;
  filter: drop-shadow(0 8px 12px rgba(0,0,0,.35));
  animation: water-bob 4s ease-in-out infinite;
}
.pier-preview .bubble {
  max-width: 220px;
  z-index: 63;
}

.terms-btn {
  position: absolute;
  bottom: .15rem;
  left: .25rem;
  z-index: 50;
  border-radius: 999px;
  background: rgba(0,0,0,.5);
  color: #fff;
  padding: 3px 8px;
  font-size: .75rem;
  text-decoration: underline;
  text-underline-offset: 2px;
}
@media (min-width: 768px) { .terms-btn { left: 1.5rem; } }

.footer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 50;
  pointer-events: none;
}
.footer-inner {
  position: absolute;
  left: 1.5rem;
  bottom: 1.5rem;
  display: none;
  align-items: center;
  gap: .75rem;
  color: #fff;
  font-size: .85rem;
  filter: drop-shadow(0 1px 3px rgb(0 0 0 / .9));
  pointer-events: auto;
}
@media (min-width: 900px) { .footer-inner { display: flex; } }
.footer-inner img { width: 7rem; height: auto; }
.footer-inner a { font-style: italic; font-weight: 500; }

.music-btn {
  position: absolute;
  right: .5rem;
  bottom: 1.5rem;
  width: 2.25rem;
  height: 2.25rem;
  color: #fff;
  cursor: pointer;
  pointer-events: auto;
  filter: drop-shadow(0 1px 3px rgba(0,0,0,.8));
}
@media (min-width: 768px) { .music-btn { right: 1.5rem; } }

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, .55);
  z-index: 80;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.modal-backdrop.open { display: flex; }
.modal {
  position: relative;
  background: #fff;
  color: #111827;
  width: min(920px, 100%);
  max-height: min(90dvh, 760px);
  overflow: auto;
  border-radius: 1rem;
  padding: 1.25rem 1.25rem 1.5rem;
  box-shadow: 0 25px 50px rgba(0,0,0,.35);
  animation: exhibition-in .2s ease;
}
.modal h3 {
  margin: 0 0 .35rem;
  font-size: 1.5rem;
  font-weight: 600;
}
.modal .sub {
  margin: 0 0 1rem;
  padding-bottom: .75rem;
  border-bottom: 1px solid #e5e7eb;
  color: #6b7280;
}
.modal-close {
  position: absolute;
  top: 1rem;
  right: 1rem;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 50%;
  border: 1px solid #e5e7eb;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}
.create-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}
@media (min-width: 768px) {
  .create-grid { grid-template-columns: 1fr 1fr; gap: 2rem; }
}
label.field {
  display: block;
  margin-top: .5rem;
  font-size: 1.125rem;
  font-weight: 600;
}
input[type=text], textarea {
  width: 100%;
  margin-top: .35rem;
  padding: .65rem .75rem;
  border: 1px solid #d1d5db;
  border-radius: .5rem;
  background: #f9fafb;
  color: #111827;
  font: inherit;
  outline: none;
}
input[type=text]:focus, textarea:focus {
  border-color: #ef4444;
  box-shadow: 0 0 0 1px #ef4444;
}
.hint { color: #ef4444; font-size: .875rem; margin: .25rem 0 0; }
.hint.muted { color: #6b7280; font-size: .75rem; }
.preview-box {
  width: 100%;
  height: 200px;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  margin: -.5rem 0 1rem;
  overflow: hidden;
}
.preview-box img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  transform: translateY(1.25rem) scale(.9);
}
.style-grid {
  display: flex;
  flex-wrap: wrap;
  gap: .5rem;
  padding: .5rem .25rem;
  max-height: 220px;
  overflow: auto;
}
.style-opt {
  width: 4rem;
  height: 4rem;
  border-radius: .75rem;
  border: 1px solid #d1d5db;
  background: #f9fafb;
  position: relative;
  overflow: hidden;
  flex-shrink: 0;
}
.style-opt img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  transform: scale(.9);
  transition: transform .2s;
}
.style-opt:hover img { transform: scale(.95); }
.style-opt.active {
  border-color: #ef4444;
  box-shadow: 0 0 0 1px #ef4444;
  background: rgba(255,255,255,.85);
}
.style-opt.locked::after {
  content: "🔒";
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,.35);
  font-size: 1.1rem;
}
.modal-actions {
  margin-top: 1rem;
  display: flex;
  justify-content: center;
}

.share-modal .share-actions {
  display: flex;
  flex-wrap: wrap;
  gap: .5rem;
  margin: 1rem 0;
}
.share-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .4rem;
  padding: .65rem 1rem;
  border-radius: .6rem;
  font-weight: 600;
  color: #fff;
  text-decoration: none;
  min-width: 110px;
}
.share-btn.fb { background: #1877f2; }
.share-btn.line { background: #06c755; }
.share-btn.copy { background: #4b5563; }
.share-link {
  width: 100%;
  font-size: .85rem;
  color: #374151;
  background: #f3f4f6;
}

.terms-modal {
  max-width: 640px;
}
.terms-modal h2 {
  font-size: 1.15rem;
  margin: 1.1rem 0 .4rem;
  color: #111827;
}
.terms-modal p, .terms-modal li {
  color: #4b5563;
  font-size: .95rem;
  line-height: 1.55;
}
.terms-modal ul { padding-left: 1.2rem; margin: .4rem 0; }

.view-page {
  min-height: 100dvh;
  overflow: auto;
  background: linear-gradient(180deg, #001a44 0%, #0b2a63 45%, #163a6e 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 2rem 1rem 3rem;
}
.view-card {
  width: min(520px, 100%);
  background: rgba(255,255,255,.95);
  color: #111;
  border-radius: 1.25rem;
  padding: 1.5rem;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0,0,0,.35);
}
.view-card img.krathong {
  width: min(260px, 70%);
  margin: 0 auto 1rem;
}
.view-card .wish {
  font-size: 1.15rem;
  line-height: 1.6;
  margin: .5rem 0;
}
.view-card .name {
  color: #6b7280;
  margin-bottom: 1rem;
}
.partner-box {
  display: flex;
  gap: .75rem;
  justify-content: center;
  align-items: flex-end;
  margin: 1rem 0;
}
.partner-box img { width: 110px; }

@keyframes star-twinkle { 0%,100%{opacity:1} 50%{opacity:.25} }
@keyframes water-bob {
  0%,25%,50%,75%,100%{transform:translateY(0)}
  12.5%,62.5%{transform:translateY(3px)}
  37.5%,87.5%{transform:translateY(-2px)}
}
@keyframes exhibition-in { 0%{opacity:0;transform:translateY(6px)} 100%{opacity:1;transform:none} }
/* เด้งเบา ๆ โดยคง translate กึ่งกลางไว้ — ไม่ให้เด้งไปขวาก่อน */
@keyframes bubble-in {
  0% { opacity: 0; transform: translate(-50%, calc(-100% - 4px)) scale(.96); }
  100% { opacity: 1; transform: translate(-50%, calc(-100% - 12px)) scale(1); }
}
@keyframes firework-pulse { 0%,100%{opacity:1} 50%{opacity:.72} }
@keyframes stem-sway { 0%,100%{transform:rotate(-1.5deg)} 50%{transform:rotate(1.5deg)} }
@keyframes moon-breathe { 0%,100%{opacity:.55} 50%{opacity:1} }
@keyframes moon-reflection { 0%,100%{opacity:.7;transform:scaleX(.85)} 50%{opacity:1;transform:scaleX(1.1)} }
@keyframes water-glint { 0%,100%{opacity:0;transform:translateX(-3%)} 50%{opacity:1;transform:translateX(3%)} }
@keyframes firefly {
  0%,100%{transform:translate(0);opacity:0}
  20%{opacity:1}
  35%{transform:translate(18px,-26px)}
  60%{transform:translate(-12px,-48px);opacity:.9}
  80%{transform:translate(10px,-20px);opacity:.4}
}
.firework-pulse { animation: firework-pulse 7s ease-in-out infinite; }
.stem-sway, .stem-sway-slow { transform-origin: 50% 100%; animation: stem-sway 5s ease-in-out infinite; }
.stem-sway-slow { animation-duration: 7.5s; animation-delay: -2s; }

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
}
