:root{
  --bg:#0f1115;
  --bg-1:#15181e;
  --fg:#f5f5f5;
  --fg-dim:#9ca3af;
  --radius:18px;
  --shadow:0 14px 32px -16px rgba(0,0,0,.6);
  --polaroid:#fdfdfd;
  --dev-time:3200ms;
}
*{box-sizing:border-box}
html,body{
  margin:0;
  height:100%;
  background:var(--bg);
  color:var(--fg);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  touch-action: manipulation;
}
body.no-scroll{ overflow:hidden; }

.app{
  height:100vh;
  display:flex;
  flex-direction:column;
  padding-bottom: env(safe-area-inset-bottom);
  overflow:hidden; /* Prevent app-level scrolling */
}
header{
  padding:24px 20px 8px 20px;
  font-weight:500;
  letter-spacing:.01em;
}

#camera{
  position:relative;
  background:var(--bg-1);
  border-radius: var(--radius);
  overflow:hidden;
  margin:0 16px;
  box-shadow: var(--shadow);
  height: 60vh; /* Fixed height for consistent viewfinder */
  max-height: 500px; /* Reasonable max size for larger screens */
  min-height: 300px; /* Minimum size for usability */
}
#camera video{
  width:100%;
  height:100%;
  object-fit:cover;
  background:#000;
  transform: scaleX(-1);
}
#flash{
  position:absolute; inset:0;
  background:#fff;
  opacity:0; pointer-events:none;
}
#flash.active{ animation: flash .35s ease; }
@keyframes flash{
  0%{opacity:.9}
  100%{opacity:0}
}

.controls{
  position: relative;
  display:flex;
  align-items:center;
  justify-content:center;
  padding:18px 0 calc(18px + env(safe-area-inset-bottom));
  gap:24px;
}
button{
  appearance:none; border:0; background:none; color:inherit; font:inherit;
}
#shutter{
  width:82px; height:82px;
  border-radius:50%;
  background:var(--fg);
  box-shadow:0 0 0 6px rgba(255,255,255,.08);
  position:relative;
  margin: 0 auto;
}
#shutter:after{
  content:"";
  position:absolute; inset:8px;
  border-radius:50%;
  background:var(--bg);
  opacity:.04;
}
#flip{
  position:absolute;
  right:24px;
  top:50%;
  transform:translateY(-50%);
  width:42px; height:42px; border-radius:50%;
  border:1px solid rgba(255,255,255,.08);
  display:flex;
  align-items:center;
  justify-content:center;
}
#flip i{
  width:22px;
  height:22px;
  stroke-width:1.5;
  color:inherit;
}

/* Gallery */
.gallery-wrap{
  flex:1 1 0;
  overflow-y:auto;
  padding:12px 16px 80px;
  min-height:0; /* Allow shrinking */
}
.gallery-title{
  color:var(--fg-dim);
  font-size:14px;
  margin:0 0 12px 4px;
}
.grid{
  display:grid;
  grid-template-columns: repeat(2, 1fr);
  gap:18px;
}

.polaroid{
  background: var(--polaroid);
  color:#111;
  border-radius:14px;
  box-shadow: var(--shadow);
  padding:10px 10px 48px 10px;
  position:relative;
  transform: rotate(var(--rot, 0deg));
  transition: transform .35s cubic-bezier(.2,.8,.2,1);
  cursor:pointer;
}
.polaroid img{
  width:100%;
  display:block;
  border-radius:10px;
  background:#000;
  transform: translateZ(0);
}
.polaroid .caption{
  position:absolute;
  left:0; right:0; bottom:10px;
  text-align:center;
  font-size:12px;
  color:#555;
  font-family: "SF Mono", ui-monospace, Menlo, monospace;
  pointer-events:none;
}

/* Developing effect */
.polaroid.developing img{
  filter: blur(18px) grayscale(1) brightness(.2);
  animation: develop var(--dev-time) steps(8,end) forwards;
}
.polaroid.developing:before{
  content:"developing…";
  position:absolute; inset:0;
  display:flex; align-items:center; justify-content:center;
  font-size:12px; letter-spacing:.1em; text-transform:uppercase;
  color:#666;
  background:rgba(255,255,255,.6);
  border-radius:14px;
  animation: fadeOut var(--dev-time) linear forwards;
}
@keyframes develop{
  0%{filter: blur(18px) grayscale(1) brightness(.2);}
  70%{filter: blur(3px) grayscale(.2) brightness(.9);}
  100%{filter:none;}
}
@keyframes fadeOut{
  0%{opacity:1}
  100%{opacity:0; visibility:hidden}
}

.empty{
  color:var(--fg-dim);
  text-align:center;
  padding:48px 0 64px;
}

.delete-ring{
  position:absolute;
  top:8px; right:8px;
  width:22px; height:22px;
  border-radius:50%;
  background:rgba(0,0,0,.6);
  color:#fff;
  font-size:14px;
  display:flex; align-items:center; justify-content:center;
  opacity:0; pointer-events:none;
  transition: opacity .15s;
}
.polaroid.show-delete .delete-ring{
  opacity:1; pointer-events:auto;
}

.hint{
  color:var(--fg-dim);
  font-size:12px;
  text-align:center;
  margin:8px 0 -6px;
}

.caption-poem{
  position:absolute;
  left:0; right:0; bottom:12px;
  text-align:center;
  font-size:12px; /* Slightly smaller for gallery */
  line-height:1.2;
  color:#333;
  font-family: "Comic Sans MS", "Bradley Hand", "Snell Roundhand", cursive;
  padding: 0 6px;
  /* Gallery view: constrain height and truncate */
  max-height: 32px; /* Allows ~2-3 lines in gallery */
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  word-wrap: break-word;
}

/* In fullscreen viewer */
.vpolaroid .caption-poem{
  bottom:16px;
  color:#444;
  font-size:14px; /* Larger in fullscreen */
  line-height:1.25;
  /* Remove truncation constraints for fullscreen */
  max-height: none;
  overflow: visible;
  display: block;
  -webkit-line-clamp: none;
  -webkit-box-orient: initial;
  white-space: pre-wrap;
}

/* ===== Fullscreen stacked Polaroid viewer ===== */
#viewer{
  position:fixed; inset:0;
  background:rgba(0,0,0,.92);
  display:none;
  z-index:9999;
  overscroll-behavior: contain;
}
#viewer.open{ display:block; }

.viewer-inner{
  position:absolute; inset:0;
  display:flex;
  align-items:center;
  justify-content:center;
  padding:24px;
}

#viewerStack{
  position:absolute; inset:0;
  pointer-events:none;
}

.vpolaroid{
  position:absolute;
  top:50%; left:50%;
  width:min(90vw, 520px);
  max-height:calc(90vh - 80px);
  padding:14px 14px 70px 14px;
  background:#fff;
  border-radius:16px;
  box-shadow:0 24px 60px rgba(0,0,0,.45);
  transform-origin:center center;
  transition: transform .28s cubic-bezier(.2,.8,.2,1), opacity .2s;
  pointer-events:auto;
}
.vpolaroid img{
  width:100%;
  max-height:calc(90vh - 140px);
  object-fit:contain;
  display:block;
  border-radius:10px;
}
.vpolaroid .caption{
  position:absolute;
  left:0; right:0; bottom:12px;
  text-align:center;
  font-size:12px;
  color:#555;
  font-family: "SF Mono", ui-monospace, Menlo, monospace;
}
.vpolaroid.behind{
  opacity:.9;
  filter: saturate(.95) brightness(.98);
}

#viewerClose, .viewer-nav{
  position:absolute;
  top:calc(env(safe-area-inset-top) + 12px);
  width:42px; height:42px;
  border-radius:50%;
  backdrop-filter: blur(10px);
  background:rgba(255,255,255,.08);
  color:#fff;
  display:flex; align-items:center; justify-content:center;
  font-size:24px;
  line-height:1;
  z-index:10000;
}
#viewerClose{
  right:calc(env(safe-area-inset-right) + 12px);
}
.viewer-nav{
  top:50%;
  transform:translateY(-50%);
  font-size:22px;
}
.viewer-nav.prev{ left:12px; }
.viewer-nav.next{ right:12px; }

.viewer-counter{
  position:absolute;
  left:50%;
  transform:translateX(-50%);
  bottom:calc(env(safe-area-inset-bottom) + 16px);
  color:#bbb;
  font-size:13px;
  letter-spacing:.06em;
  z-index:10000;
}

/* Disable clicks while a slide animation is running */
#viewer.animating {
  pointer-events: none;
}

/* Generic transition used by the two top cards during the slide */
.vpolaroid.slide-active {
  transition: transform 280ms cubic-bezier(.2,.8,.2,1), opacity 200ms;
}
