:root {
  --bg: #0d0a08;
  --fg: #eee;
  --paper: #f5f1e8;
  --inner: #e8e1d0;
  --accent-red: #d34d4d;
  --transition: 300ms ease-out;
}
* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, "Helvetica Neue", sans-serif;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
img {
  -webkit-user-drag: none;
  user-drag: none;
  pointer-events: none;
}
#scene {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  perspective: 1200px;
}
#polaroid {
  position: relative;
  background: var(--paper);
  background-image:
    radial-gradient(rgba(80,55,30,0.04) 1px, transparent 1px),
    linear-gradient(180deg, rgba(255,250,240,0.4), rgba(220,200,170,0));
  background-size: 3px 3px, 100% 100%;
  padding: 22px 22px 80px 22px;
  border-radius: 2px;
  box-shadow:
    inset 0 1px 0 rgba(255,250,240,0.12),
    0 24px 48px rgba(0,0,0,0.55),
    0 6px 14px rgba(0,0,0,0.45),
    0 0 0 0.5px rgba(0,0,0,0.4);
  transform-style: preserve-3d;
  transition: transform var(--transition), box-shadow var(--transition);
  max-width: calc(100vh - 32px);
  max-height: calc(100vh - 32px);
}
#polaroid::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(
    circle at var(--shine-x, 50%) var(--shine-y, 30%),
    rgba(255,255,255,0.55) 0%,
    rgba(255,255,255,0.18) 18%,
    rgba(255,255,255,0) 45%);
  mix-blend-mode: screen;
  opacity: var(--shine-opacity, 0.35);
  transition: opacity var(--transition);
  /* Explicit z-index below #tape (z-index 5). Without this, iOS Safari
     paints the shine on top of the washi tape once the polaroid gets
     a 3D transform during press, and `mix-blend-mode: screen` washes
     the white tape into invisibility. */
  z-index: 1;
}
#photo-frame {
  width: min(80vh, calc(100vw - 64px));
  height: min(80vh, calc(100vw - 64px));
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--inner);
  overflow: hidden;
  position: relative;
}
#stage-clip {
  position: relative;
  overflow: hidden;
  background: var(--inner);
}
#stage {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  background: var(--inner);
}
/* Washi tape — pinned to the visible photo's top-center. Hidden when
   the photo fills the canvas top (no cream margin for it to stick onto).
   Position is set by JS in main.js (positionTape). */
#tape {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 50% 50%;
  width: 60px;
  height: 25px;
  background: repeating-linear-gradient(
    120deg,
    rgba(255, 255, 255, 0.88)  0px,
    rgba(255, 255, 255, 0.88)  5px,
    rgba(110, 165, 210, 0.82)  5px,
    rgba(110, 165, 210, 0.82)  10px
  );
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.22);
  border-radius: 1px;
  pointer-events: none;
  z-index: 5;
  opacity: 0;
}
#caption {
  margin-top: 18px;
  font-family: "Permanent Marker", "Marker Felt", "Bradley Hand", cursive;
  color: #2a2a2a;
  font-size: 18px;
  text-align: center;
  align-self: stretch;
  min-height: 22px;
}

/* Preload progress indicator — fades out when complete */
#progress {
  position: fixed;
  right: 24px;
  top: 24px;
  font-family: "SF Mono", Consolas, monospace;
  font-size: 11px;
  color: #888;
  letter-spacing: 0.05em;
  opacity: 1;
  transition: opacity 600ms ease;
  z-index: 35;
}
#progress.done {
  opacity: 0;
  pointer-events: none;
}

/* Tilt button — sits inside the polaroid's bottom margin, hidden on desktop */
#tilt-button {
  position: absolute;
  left: 50%;
  bottom: 14px;
  transform: translateX(-50%);
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 2px dashed var(--accent-red);
  background: rgba(211, 77, 77, 0.08);
  color: var(--accent-red);
  font: inherit;
  display: none;
  align-items: center;
  justify-content: center;
  -webkit-touch-callout: none;
  user-select: none;
  animation: breathe 2s ease-in-out infinite;
  cursor: pointer;
  padding: 0;
  z-index: 5;
}
#tilt-button.active {
  border-style: solid;
  background: rgba(211, 77, 77, 0.18);
  box-shadow: 0 0 12px rgba(211, 77, 77, 0.5);
  animation: none;
  transform: translateX(-50%);
}
#tilt-button.denied {
  border-color: #888;
  color: #888;
  background: rgba(136,136,136,0.08);
}
#tilt-button .dot-grid {
  width: 15px;
  height: 15px;
  background: radial-gradient(circle, currentColor 1.2px, transparent 1.6px) center / 5px 5px;
}
@keyframes breathe {
  0%, 100% { opacity: 0.7; }
  50%      { opacity: 1.0; }
}
@media (hover: none) and (pointer: coarse) {
  #tilt-button { display: flex; }
}

/* Debug panel */
#debug-panel { position: fixed; left: 12px; top: 12px; z-index: 40; display: flex; flex-direction: column; align-items: flex-start; gap: 0; }
.debug-toggle {
  width: 28px;
  height: 28px;
  border-radius: 4px;
  border: 1px solid rgba(255,255,255,0.15);
  background: rgba(0,0,0,0.7);
  backdrop-filter: blur(4px);
  color: #ccc;
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  padding: 0;
}
.debug-toggle:hover { background: rgba(0,0,0,0.85); }
#debug-panel.collapsed > *:not(.debug-toggle) { display: none; }
.debug-sliders {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 12px;
  background: rgba(0,0,0,0.7);
  backdrop-filter: blur(4px);
  border-radius: 6px;
  font-size: 11px;
  color: #ccc;
  width: 180px;
}
.debug-sliders input[type="range"] { width: 100%; }

/* Photo map — miniature film contact sheet pinned to the polaroid's
   bottom-left margin. Each row is a strip of negatives: dark film base
   with sprocket-hole bands top and bottom, individual frames inside.
   The currently displayed photo is the brightly exposed frame. */
.photo-map {
  position: absolute;
  left: 12px;
  bottom: 10px;
  width: 88px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 5px 4px 5px 9px;
  pointer-events: none;
  z-index: 3;
  /* Translucent archival sleeve: faint cool-white plastic with a subtle
     diagonal sheen, hairline edge, and soft drop shadow off the polaroid. */
  background:
    linear-gradient(
      135deg,
      rgba(255,255,255,0.14) 0%,
      rgba(255,255,255,0.04) 45%,
      rgba(255,255,255,0.10) 80%,
      rgba(255,255,255,0.03) 100%
    ),
    rgba(245,242,232,0.14);
  border: 0.5px solid rgba(58,40,24,0.35);
  border-radius: 1px;
  box-shadow:
    0 1px 2px rgba(0,0,0,0.25),
    inset 0 0 0 0.5px rgba(255,255,255,0.35),
    inset 0 1px 2px rgba(255,255,255,0.15);
  backdrop-filter: blur(1px);
}
/* Binder-hole punches down the left edge of the sleeve. */
.photo-map::before {
  content: "";
  position: absolute;
  left: 2.5px;
  top: 4px;
  bottom: 4px;
  width: 2px;
  background-image: radial-gradient(
    circle,
    rgba(0,0,0,0.35) 0 0.9px,
    rgba(255,255,255,0.35) 1px 1.4px,
    transparent 1.5px
  );
  background-size: 2px 12px;
  background-repeat: repeat-y;
  background-position: center top;
}
.pm-row {
  position: relative;
  background: #2c1a0e;
  padding: 2px 2px;
}
/* Sprocket-hole bands: tiny pale rectangles repeated across the top and
   bottom edges of each strip. */
.pm-row::before,
.pm-row::after {
  content: "";
  position: absolute;
  left: 2px;
  right: 2px;
  height: 1.5px;
  background-image: linear-gradient(
    to right,
    #d4b896 0 1.5px,
    transparent 1.5px 3px
  );
  background-size: 3px 1.5px;
  background-repeat: repeat-x;
}
.pm-row::before { top: 0.5px; }
.pm-row::after  { bottom: 0.5px; }
.pm-strip {
  display: flex;
  gap: 1px;
  height: 9px;
}
.pm-cell {
  flex: 1 1 auto;
  background: linear-gradient(
    to bottom,
    rgba(212,184,150,0.18),
    rgba(160,120,80,0.10)
  );
}
.pm-cell.active {
  background: #f6e4b4;
  box-shadow:
    0 0 5px rgba(255,220,150,0.85),
    inset 0 0 0 0.5px #fff6dc;
  outline: none;
  outline-offset: 0;
}

/* ── Intro modal ── */
#intro-modal {
  /* reset browser dialog defaults */
  border: none;
  outline: none;
  padding: 0;

  /* card geometry */
  max-width: 520px;
  width: calc(100% - 32px);
  border-radius: 6px;

  /* warm paper tone — matches the polaroid */
  background: #f5f1e8;
  color: #2a2218;

  /* polaroid-style shadow */
  box-shadow: 0 12px 40px rgba(0,0,0,0.55), 0 2px 6px rgba(0,0,0,0.4);
}

#intro-modal::backdrop {
  background: rgba(10, 6, 4, 0.78);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

/* ── inner layout ── */
/* Use #intro-modal prefix on all rules to match ID specificity */
#intro-modal .intro-header {
  padding: 32px 32px 0;
}
#intro-modal .intro-lede {
  padding: 20px 32px 0;
}
#intro-modal .intro-device {
  display: none;
  padding: 20px 32px 36px;
}

/* ── typography ── */
#intro-modal .intro-eyebrow {
  display: block;
  margin: 0 0 14px;
  font-family: "JetBrains Mono", "SF Mono", ui-monospace, monospace;
  font-size: 10px;
  letter-spacing: 0.32em;
  text-transform: uppercase;
  color: #8a7d6c;
}
#intro-modal .intro-rule {
  margin: 26px 0 22px;
  border: none;
  height: 1px;
  width: 28px;
  background: rgba(42,34,24,0.18);
}
#intro-modal .intro-header h1 {
  margin: 0;
  font-family: "Shippori Mincho", "Noto Serif TC", "Source Han Serif TC", serif;
  font-size: 44px;
  font-weight: 500;
  letter-spacing: 0.04em;
  color: #2a2218;
  line-height: 1.1;
}
#intro-modal .intro-subtitle {
  margin: 14px 0 0;
  font-family: "Shippori Mincho", "Noto Serif TC", serif;
  font-size: 15px;
  font-weight: 400;
  letter-spacing: 0.32em;
  color: #6b5d4a;
}
#intro-modal .intro-lede p {
  margin: 0 0 1em;
  font-family: "Noto Serif TC", "Shippori Mincho", "Source Han Serif TC", serif;
  font-size: 14.5px;
  line-height: 1.95;
  letter-spacing: 0.05em;
  color: #3a3328;
  text-wrap: pretty;
}
#intro-modal .intro-lede p:last-child {
  margin-bottom: 0;
}

/* ── device sections ── */
#intro-modal .intro-device-desktop p,
#intro-modal .intro-device-mobile p {
  margin: 0 0 1em;
  font-family: "Noto Serif TC", "Shippori Mincho", "Source Han Serif TC", serif;
  font-size: 14.5px;
  line-height: 1.85;
  letter-spacing: 0.02em;
  color: #3a3328;
}
#intro-modal .intro-device-desktop p:last-of-type,
#intro-modal .intro-device-mobile p:last-of-type {
  margin-bottom: 0;
}

/* divider before device section */
#intro-modal .intro-device {
  border-top: 1px solid rgba(0,0,0,0.08);
  margin-top: 4px;
}

/* ── QR code tile ── */
#intro-qrcode {
  margin: 18px auto 0;
  width: fit-content;
  background: #fff;
  padding: 10px;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.12), 0 0 0 1px rgba(0,0,0,0.06);
  line-height: 0;
}
#intro-qrcode svg,
#intro-qrcode img,
#intro-qrcode canvas {
  display: block;
  width: 200px !important;
  height: 200px !important;
}

/* ── start button (mobile) ── */
#intro-modal .intro-start-wrap {
  display: flex;
  justify-content: center;
}
#intro-start-btn {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  margin: 22px auto 0;
  padding: 13px 38px;
  background: #2a1a10;
  color: #f5f1e8;
  border: none;
  border-radius: 3px;
  font-size: 13px;
  font-weight: 500;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  font-family: -apple-system, "Helvetica Neue", sans-serif;
  cursor: pointer;
  transition: background 150ms ease-out, transform 80ms ease-out;
  -webkit-user-select: none;
  user-select: none;
}
#intro-start-btn::after {
  content: "→";
  font-size: 16px;
  text-transform: none;
  letter-spacing: 0;
}
#intro-start-btn:hover {
  background: #4e3322;
}
#intro-start-btn:active {
  background: #2a1a10;
  transform: scale(0.97);
}

/* ── debug exit (desktop-debug only) ── */
#intro-modal .intro-debug-exit {
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid rgba(0,0,0,0.08);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
#intro-modal .intro-debug-exit small {
  font-size: 12px;
  color: #8a7d6c;
  letter-spacing: 0.05em;
}
#intro-debug-btn {
  padding: 7px 16px;
  background: transparent;
  color: #8a7d6c;
  border: 1px solid rgba(0,0,0,0.15);
  border-radius: 5px;
  font-size: 13px;
  font-family: -apple-system, "Helvetica Neue", sans-serif;
  cursor: pointer;
  transition: border-color 150ms, color 150ms;
  -webkit-user-select: none;
  user-select: none;
}
#intro-debug-btn:hover {
  color: #3a2418;
  border-color: rgba(0,0,0,0.30);
}

/* ── device-section visibility logic ── */
#intro-modal[data-device="desktop"] .intro-device-desktop,
#intro-modal[data-device="desktop-debug"] .intro-device-desktop { display: block; }
#intro-modal[data-device="android"] .intro-device-mobile,
#intro-modal[data-device="ios"] .intro-device-mobile { display: block; }
#intro-modal:not([data-device="ios"]) .intro-ios-only { display: none; }
#intro-modal:not([data-device="desktop-debug"]) .intro-debug-exit { display: none; }

/* ── permission-denied error (iOS) ── */
#intro-modal .intro-device-mobile .intro-permission-error {
  margin-top: 14px;
  padding: 12px 14px;
  background: rgba(180, 70, 50, 0.07);
  border-left: 3px solid rgba(180, 70, 50, 0.45);
  border-left-style: solid;
  color: #6b2c1f;
  font-size: 14px;
  line-height: 1.6;
}
#intro-start-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* ── language switcher ── */
#intro-modal .intro-lang-switcher {
  position: absolute;
  top: 12px;
  right: 12px;
  display: flex;
  gap: 4px;
  z-index: 2;
}
#intro-modal .intro-lang-switcher button {
  padding: 4px 9px;
  background: transparent;
  color: #8a7d6c;
  border: 1px solid rgba(0,0,0,0.12);
  border-radius: 4px;
  font-size: 13px;
  font-family: "Shippori Mincho", "Noto Serif TC", serif;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
  transition: background 120ms, color 120ms, border-color 120ms;
}
#intro-modal .intro-lang-switcher button:hover {
  color: #3a2418;
  border-color: rgba(0,0,0,0.28);
}
#intro-modal .intro-lang-switcher button.active {
  background: #3a2418;
  color: #f5f1e8;
  border-color: #3a2418;
}

/* ── footer (GitHub link) ── */
#intro-modal .intro-footer {
  padding: 14px 32px 18px;
  border-top: 1px solid rgba(0,0,0,0.08);
  text-align: center;
}
#intro-modal .intro-footer a {
  font-size: 12px;
  color: #8a7d6c;
  text-decoration: none;
  letter-spacing: 0.03em;
}
#intro-modal .intro-footer a:hover {
  color: #3a2418;
  text-decoration: underline;
}

/* ── mobile viewport adjustments ── */
@media (max-width: 480px) {
  #intro-modal .intro-header {
    padding: 20px 18px 0;
  }
  #intro-modal .intro-lede {
    padding: 16px 18px 0;
  }
  #intro-modal .intro-device {
    padding: 16px 18px 24px;
  }
  #intro-modal .intro-footer {
    padding: 12px 18px 14px;
  }
  #intro-modal .intro-header h1 {
    font-size: 28px;
  }
  #intro-modal .intro-subtitle {
    font-size: 15px;
  }
  #intro-qrcode svg,
  #intro-qrcode img,
  #intro-qrcode canvas {
    width: 180px !important;
    height: 180px !important;
  }
}
