:root {
  --bg-1: #f7e1d7;
  --bg-2: #fce5e1;
  --tissue: #ffffff;
  --box-1: #d3e899;     /* light yellow-green (top of box) */
  --box-2: #6e9f44;     /* darker green (bottom of box) */
  --box-3: #a4cf6c;     /* mid green */
  --hole-1: #fff7e1;
  --hole-2: #d2c193;
  --text: #5d4441;
  --text-mute: rgba(93, 68, 65, 0.55);
  --shadow: rgba(70, 120, 40, 0.22);
  --shadow-deep: rgba(50, 90, 25, 0.30);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  height: 100%;
  overscroll-behavior: none;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic", "Noto Sans JP", system-ui, sans-serif;
  background: linear-gradient(180deg, var(--bg-1) 0%, var(--bg-2) 100%);
  background-attachment: fixed;
  color: var(--text);
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  -webkit-font-smoothing: antialiased;
}

.app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top, 0);
  padding-right: env(safe-area-inset-right, 0);
  padding-bottom: env(safe-area-inset-bottom, 0);
  padding-left: env(safe-area-inset-left, 0);
}

/* topbar */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px 6px;
}

.counter {
  display: flex;
  align-items: baseline;
  gap: 6px;
  font-feature-settings: "tnum";
}

.counter-num {
  font-size: 36px;
  font-weight: 800;
  letter-spacing: -1px;
  color: var(--text);
  display: inline-block;
  transition: transform 0.16s cubic-bezier(0.34, 1.56, 0.64, 1);
  transform-origin: 50% 100%;
}

.counter.bump .counter-num {
  transform: scale(1.12);
}

.counter-label {
  font-size: 14px;
  color: var(--text-mute);
  font-weight: 500;
}

.actions {
  display: flex;
  gap: 10px;
}

.btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.65);
  color: var(--text);
  cursor: pointer;
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  box-shadow: 0 4px 14px var(--shadow);
  display: grid;
  place-items: center;
  transition: transform 0.12s ease-out, box-shadow 0.12s ease-out;
}

.btn:active {
  transform: scale(0.9);
  box-shadow: 0 2px 6px var(--shadow);
}

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* stage */
.stage {
  flex: 1;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
}

.pack {
  position: relative;
  width: min(82vw, 380px);
  aspect-ratio: 1 / 1;
  cursor: pointer;
  touch-action: manipulation;
  outline: none;
}

.pack:focus-visible {
  box-shadow: 0 0 0 4px rgba(150, 200, 100, 0.35);
  border-radius: 24px;
}

/* === Box === */
.box {
  position: absolute;
  left: -4%;
  right: -4%;
  bottom: 0;
  height: 50%;
  background: linear-gradient(170deg, var(--box-1) 0%, var(--box-3) 50%, var(--box-2) 100%);
  border-radius: 16px;
  box-shadow:
    inset 0 3px 0 rgba(255, 255, 255, 0.55),
    inset 0 -18px 28px rgba(0, 0, 0, 0.08),
    0 32px 56px -12px var(--shadow-deep);
  z-index: 1;
}

.box::after {
  /* subtle highlight stripe (suggests top face of the box) */
  content: '';
  position: absolute;
  top: 22%;
  left: 14%;
  right: 14%;
  height: 2px;
  background: rgba(255, 255, 255, 0.45);
  border-radius: 2px;
  pointer-events: none;
}

/* === Opening (where tissues emerge) === */
.box-hole {
  position: absolute;
  left: 50%;
  top: 50%;          /* centered on the box-top edge */
  transform: translate(-50%, -50%);
  width: 78%;
  height: 38px;
  background: radial-gradient(ellipse at 50% 30%, var(--hole-1) 0%, var(--hole-2) 100%);
  border-radius: 50% / 60%;   /* slightly wider than tall */
  box-shadow:
    inset 0 -8px 14px rgba(60, 40, 10, 0.32),  /* depth shadow at bottom */
    inset 0 2px 4px rgba(255, 255, 255, 0.6);   /* rim highlight at top */
  z-index: 4;                                    /* above both box and tissues */
  pointer-events: none;
}

/* === Tissues === */
.tissue {
  position: absolute;
  top: 0;
  left: 50%;
  width: 76%;
  aspect-ratio: 1.55 / 1;
  background: var(--tissue);
  border-radius: 8px 8px 6px 6px;
  box-shadow:
    0 1px 0 rgba(0, 0, 0, 0.05),
    0 8px 18px var(--shadow),
    inset 0 1px 0 rgba(255, 255, 255, 0.95);
  z-index: 2;                       /* below .box-hole, so the hole hides tissue's bottom */
  transform: var(--rest, none);
  transform-origin: 50% 100%;
  will-change: transform, opacity;
  pointer-events: auto;
}

.tissue::before {
  /* quilted dot pattern */
  content: '';
  position: absolute;
  inset: 6px;
  border-radius: 6px;
  background-image: radial-gradient(circle at 50% 50%, rgba(120, 60, 60, 0.06) 1px, transparent 1.5px);
  background-size: 14px 14px;
  pointer-events: none;
}

.tissue::after {
  /* top edge highlight */
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 45%;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.7), rgba(255, 255, 255, 0));
  border-radius: 8px 8px 0 0;
  pointer-events: none;
}

.tissue.peel {
  animation: peel 720ms cubic-bezier(0.55, 0, 0.45, 1) forwards;
  pointer-events: none;
}

.tissue.undo-pop {
  animation: undo-pop 540ms cubic-bezier(0.34, 1.56, 0.64, 1) backwards;
}

@keyframes peel {
  0% {
    transform: var(--rest) translate(0, 0) rotate(0) scale(1);
    opacity: 1;
  }
  30% {
    transform: var(--rest) translateY(-14px) rotate(2deg) scale(1.05);
    opacity: 1;
  }
  100% {
    transform: var(--rest) translateY(-220%) rotate(-14deg) scale(0.92);
    opacity: 0;
  }
}

@keyframes undo-pop {
  0% {
    transform: translate(0, -160%) rotate(-6deg) scale(0.92);
    opacity: 0;
  }
  50% { opacity: 1; }
  100% {
    transform: var(--rest);
    opacity: 1;
  }
}

/* hint */
.hint {
  position: absolute;
  bottom: max(28px, calc(env(safe-area-inset-bottom, 0) + 28px));
  left: 0;
  right: 0;
  text-align: center;
  font-size: 13px;
  color: var(--text-mute);
  pointer-events: none;
  transition: opacity 0.5s;
  letter-spacing: 0.5px;
}

.hint.hidden {
  opacity: 0;
}

/* footer */
.footer {
  text-align: center;
  padding: 14px 16px max(14px, env(safe-area-inset-bottom, 14px));
  font-size: 12px;
  color: var(--text-mute);
  letter-spacing: 0.5px;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.best strong {
  color: var(--text);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.sep {
  opacity: 0.4;
}

/* burst */
.burst {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: clamp(28px, 8vw, 40px);
  font-weight: 800;
  color: var(--text);
  pointer-events: none;
  animation: burst 1500ms ease-out forwards;
  z-index: 99;
  text-align: center;
  padding: 0 20px;
}

@keyframes burst {
  0% { transform: scale(0.85); opacity: 0; }
  18% { transform: scale(1); opacity: 1; }
  75% { transform: scale(1.05); opacity: 1; }
  100% { transform: scale(1.1); opacity: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .tissue.peel { animation-duration: 240ms; }
  .tissue.undo-pop { animation-duration: 200ms; }
  .burst { animation-duration: 600ms; }
  .counter.bump .counter-num { transition-duration: 50ms; }
}
