/* =============================================================================
   Pack It — styles
   A bright, playful "sunny travel" look built for both a phone in one hand
   and a desktop window: a cheerful sky-to-sunset gradient, a warm suitcase,
   colorful rounded chips/buttons, and small bouncy animations. No external
   fonts or images — everything here works fully offline.

   Layout note: .stage-frame's on-screen size is set by js/layout.js (a
   "contain fit" that accounts for the REAL rendered height of the header/
   footer, not a guess) — the width/aspect-ratio here are just the pre-JS
   fallback, so the game still looks reasonable if JS is slow to run once.
   ============================================================================= */

:root {
  --bg-top: #7fd4f5;
  --bg-mid: #bfe8fb;
  --bg-bottom: #ffe9c2;
  --ink: #223042;
  --ink-soft: #5b6b7c;
  --card: #fff9ee;
  --leather: #8a5a34;
  --accent: #ff6b4a;
  --accent-dark: #e0532f;
  --teal: #14b8a6;
  --purple: #8b5cf6;
  --success: #2f9e44;
  --gold: #f1b13a;
  --easy: #2f9e44;
  --medium: #e08a1f;
  --hard: #e0473f;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI Rounded", "Segoe UI", "Nunito", system-ui, sans-serif;
}

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

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

body {
  margin: 0;
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(circle at 12% 8%, rgba(255, 255, 255, 0.5) 0, rgba(255, 255, 255, 0) 18%),
    radial-gradient(circle at 88% 6%, rgba(255, 255, 255, 0.4) 0, rgba(255, 255, 255, 0) 15%),
    radial-gradient(circle at 90% 85%, rgba(255, 233, 194, 0.6) 0, rgba(255, 233, 194, 0) 30%),
    linear-gradient(165deg, var(--bg-top) 0%, var(--bg-mid) 45%, var(--bg-bottom) 100%);
  background-attachment: fixed;
  min-height: 100dvh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow-x: hidden;
}

.game-shell {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 28px;
  width: 100%;
  max-width: 1180px;
  padding: 0 16px;
}

/* Decorative side panel: only appears once there's genuinely enough width
   for it (see the media query below) — on anything narrower it's not
   rendered at all, so it never competes for space with the actual game. */
.side-panel {
  display: none;
  flex: 0 0 260px;
}

.side-panel-inner {
  background: rgba(255, 249, 238, 0.55);
  border: 1.5px solid rgba(138, 90, 52, 0.18);
  border-radius: 24px;
  padding: 28px 24px;
  backdrop-filter: blur(6px);
  text-align: center;
}

.side-brand-mark {
  font-size: 46px;
  display: block;
  filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.15));
}

.side-title {
  margin: 10px 0 2px;
  font-size: 26px;
  font-weight: 800;
  background: linear-gradient(90deg, #e0532f, #f1974a 60%, #f1b13a);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.side-tagline {
  margin: 0 0 22px;
  font-size: 13px;
  color: var(--ink-soft);
  font-weight: 600;
}

.side-howto {
  list-style: none;
  margin: 0 0 22px;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: left;
}

.side-howto li {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13.5px;
  font-weight: 700;
  color: var(--ink);
  background: white;
  border-radius: 14px;
  padding: 10px 12px;
  box-shadow: 0 2px 6px rgba(34, 48, 66, 0.06);
}

.side-howto-icon {
  font-size: 16px;
}

.side-items {
  font-size: 26px;
  display: flex;
  justify-content: center;
  gap: 12px;
  opacity: 0.85;
}

@media (min-width: 1100px) {
  .side-panel {
    display: block;
  }
}

.app {
  width: 100%;
  max-width: 760px;
  padding: max(12px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

/* ---------------------------------------------------------------- header */

.topbar {
  width: 100%;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px 14px;
  padding: 4px 2px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-mark {
  font-size: 30px;
  line-height: 1;
  filter: drop-shadow(0 3px 4px rgba(0, 0, 0, 0.15));
}

.brand-text h1 {
  margin: 0;
  font-size: 21px;
  font-weight: 800;
  letter-spacing: -0.01em;
  background: linear-gradient(90deg, #e0532f, #f1974a 60%, #f1b13a);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.brand-text .tagline {
  margin: 0;
  font-size: 12px;
  color: var(--ink-soft);
  font-weight: 600;
}

.header-stats {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
}

.stat-chip {
  font-weight: 800;
  font-size: 14px;
  padding: 7px 12px;
  border-radius: 999px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.timer-chip {
  color: #1c7fa8;
  background: #e2f6ff;
  border: 1px solid rgba(28, 127, 168, 0.25);
}

.icon-btn {
  font-size: 17px;
  line-height: 1;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1.5px solid rgba(34, 48, 66, 0.15);
  background: white;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 6px rgba(34, 48, 66, 0.08);
  transition: transform 0.12s ease;
}

.icon-btn:active {
  transform: scale(0.9);
}

.icon-btn:hover {
  background: rgba(34, 48, 66, 0.05);
  transform: translateY(-1px);
}

.star-chip {
  color: #b5790a;
  background: #fff3d6;
  border: 1px solid rgba(217, 154, 27, 0.35);
  transform-origin: center;
}

.star-chip.bump {
  animation: starBump 0.45s cubic-bezier(.34, 1.56, .64, 1);
}

@keyframes starBump {
  0% { transform: scale(1); }
  35% { transform: scale(1.28); }
  100% { transform: scale(1); }
}

.level-info {
  width: 100%;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  padding: 0 2px;
}

.level-pill {
  background: linear-gradient(180deg, #6fc9f2, #4aa8e0);
  color: white;
  font-weight: 800;
  font-size: 12.5px;
  letter-spacing: 0.02em;
  padding: 6px 12px;
  border-radius: 999px;
  box-shadow: 0 3px 8px rgba(74, 168, 224, 0.4);
}

.level-name {
  font-size: 14px;
  color: var(--ink);
  font-weight: 700;
}

.difficulty-tag {
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  padding: 4px 10px;
  border-radius: 999px;
}

.difficulty-tag.diff-easy { color: var(--easy); background: rgba(47, 158, 68, 0.14); }
.difficulty-tag.diff-medium { color: var(--medium); background: rgba(224, 138, 31, 0.15); }
.difficulty-tag.diff-hard { color: var(--hard); background: rgba(224, 71, 63, 0.14); }

/* ------------------------------------------------------------------ buttons */

.btn {
  font-family: inherit;
  font-size: 14.5px;
  font-weight: 800;
  padding: 11px 20px;
  min-height: 42px;
  border-radius: 999px;
  border: none;
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease, background 0.12s ease;
}

.btn:active {
  transform: translateY(1px) scale(0.96);
}

.btn-ghost {
  background: white;
  border: 1.5px solid rgba(34, 48, 66, 0.15);
  color: var(--ink);
  box-shadow: 0 2px 6px rgba(34, 48, 66, 0.08);
}

.btn-ghost:hover {
  background: rgba(34, 48, 66, 0.05);
}

.btn-primary {
  background: linear-gradient(180deg, #ff8562, var(--accent));
  color: white;
  box-shadow: 0 8px 18px rgba(255, 107, 74, 0.45);
}

.btn-primary:hover {
  background: linear-gradient(180deg, #ff9270, var(--accent-dark));
  transform: translateY(-1px);
}

/* -------------------------------------------------------------------- stage */

.stage-wrap {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.stage-frame {
  position: relative;
  width: 100%;
  max-width: 700px;
  aspect-ratio: 700 / 1200;
  border-radius: 26px;
  padding: 10px;
  background:
    repeating-linear-gradient(135deg, transparent 0 14px, rgba(138, 90, 52, 0.06) 14px 15px),
    var(--card);
  border: 2px dashed rgba(138, 90, 52, 0.35);
  box-shadow: 0 18px 40px rgba(34, 48, 66, 0.16);
  flex-shrink: 0;
}

#gameCanvas {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 18px;
  touch-action: none;
  cursor: grab;
}

.stage-frame.level-intro {
  animation: levelIntroIn 0.32s ease-out;
}

@keyframes levelIntroIn {
  from { opacity: 0.35; transform: scale(0.985); }
  to { opacity: 1; transform: scale(1); }
}

.progress-label {
  margin: 0;
  font-weight: 800;
  font-size: 14px;
  color: var(--leather);
}

.hint-text {
  margin: 0;
  font-size: 12.5px;
  color: var(--ink-soft);
  text-align: center;
  max-width: 480px;
}

/* --------------------------------------------------------------- win overlay */

.win-overlay {
  position: absolute;
  inset: 8px;
  border-radius: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(34, 48, 66, 0.32);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
  padding: 10px;
  z-index: 6;
}

.screen-flash {
  position: absolute;
  inset: 8px;
  border-radius: 18px;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.95) 0%, rgba(255, 244, 200, 0.4) 45%, rgba(255, 244, 200, 0) 75%);
  opacity: 0;
  pointer-events: none;
  z-index: 4;
}

.screen-flash.flash-active {
  animation: screenFlashPulse 0.55s ease-out;
}

@keyframes screenFlashPulse {
  0% { opacity: 0; }
  15% { opacity: 1; }
  100% { opacity: 0; }
}

.win-overlay.visible {
  opacity: 1;
  pointer-events: auto;
}

.win-card {
  background: var(--card);
  border-radius: 22px;
  padding: 28px 32px 24px;
  text-align: center;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.25);
  transform: translateY(14px) scale(0.92);
  opacity: 0;
  transition: transform 0.32s cubic-bezier(.34, 1.56, .64, 1), opacity 0.25s ease;
  max-width: min(360px, 88vw);
  max-height: 92%;
  overflow-y: auto;
}

.win-overlay.visible .win-card {
  transform: translateY(0) scale(1);
  opacity: 1;
}

.win-stamp {
  width: 52px;
  height: 52px;
  margin: 0 auto 8px;
  border-radius: 50%;
  background: var(--success);
  color: white;
  font-size: 26px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 8px 18px rgba(47, 158, 68, 0.4);
}

.win-card h2 {
  margin: 0 0 6px;
  font-size: 24px;
  letter-spacing: -0.01em;
  color: var(--ink);
}

.win-stars {
  margin: 0 0 2px;
  font-size: 30px;
  letter-spacing: 0.12em;
}

.win-star {
  display: inline-block;
  color: #d8dde3;
  text-shadow: none;
  opacity: 0;
  transform: scale(0.3) rotate(-15deg);
  animation: starPop 0.4s cubic-bezier(.34, 1.56, .64, 1) forwards;
}

.win-star.filled {
  color: var(--gold);
  text-shadow: 0 2px 6px rgba(241, 177, 58, 0.45);
}

@keyframes starPop {
  0% { opacity: 0; transform: scale(0.3) rotate(-15deg); }
  70% { opacity: 1; transform: scale(1.18) rotate(4deg); }
  100% { opacity: 1; transform: scale(1) rotate(0); }
}

.win-level-line {
  margin: 0 0 8px;
  font-size: 13.5px;
  font-weight: 700;
  color: var(--leather);
}

.win-new-best {
  display: inline-block;
  margin: 0 0 10px;
  font-size: 12.5px;
  font-weight: 800;
  color: white;
  background: linear-gradient(90deg, #f1974a, var(--gold));
  padding: 5px 14px;
  border-radius: 999px;
  letter-spacing: 0.02em;
  box-shadow: 0 4px 10px rgba(241, 177, 58, 0.4);
}

.win-new-best:empty {
  display: none;
}

.win-efficiency {
  margin: 0 0 4px;
  font-size: 15px;
  font-weight: 800;
  color: var(--accent-dark);
  letter-spacing: 0.01em;
}

.win-efficiency span {
  font-weight: 600;
  color: var(--ink-soft);
}

.win-time-line {
  margin: 0 0 10px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
}

.win-card p.win-subtitle {
  margin: 0 0 18px;
  color: var(--ink-soft);
  font-size: 13.5px;
}

/* Short, staggered reveal: the card doesn't dump all its content at once —
   each beat (stamp+title, stars, level line, efficiency, time+subtitle,
   button) fades/rises in a little after the last, finishing well under a
   second so it reads as "satisfying," not "slow." Gated behind .reveal
   (toggled by ui.js on every showWin(), not just page load) so it replays
   every time a level completes, not only the very first time. */
.win-card.reveal .win-stamp, .win-card.reveal h2, .win-card.reveal .win-level-line,
.win-card.reveal .win-new-best, .win-card.reveal .win-efficiency,
.win-card.reveal .win-time-line, .win-card.reveal p.win-subtitle, .win-card.reveal #nextBtn {
  opacity: 0;
  animation: winBeatIn 0.42s ease-out forwards;
}

.win-card.reveal .win-stamp { animation-delay: 0.02s; }
.win-card.reveal h2 { animation-delay: 0.08s; }
.win-card.reveal .win-level-line { animation-delay: 0.5s; }
.win-card.reveal .win-new-best { animation-delay: 0.58s; }
.win-card.reveal .win-efficiency { animation-delay: 0.66s; }
.win-card.reveal .win-time-line { animation-delay: 0.74s; }
.win-card.reveal p.win-subtitle { animation-delay: 0.8s; }
.win-card.reveal #nextBtn { animation-delay: 0.88s; }

@keyframes winBeatIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---------------------------------------------------------------- rotate hint */

.rotate-hint {
  position: absolute;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%) translateY(8px);
  background: rgba(34, 48, 66, 0.82);
  color: white;
  font-size: 13px;
  font-weight: 700;
  padding: 9px 16px;
  border-radius: 999px;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
  z-index: 5;
}

.rotate-hint.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.rotate-hint-icon {
  display: inline-block;
  animation: rotateHintSpin 1.6s ease-in-out infinite;
}

@keyframes rotateHintSpin {
  0%, 60%, 100% { transform: rotate(0deg); }
  75% { transform: rotate(-45deg); }
  90% { transform: rotate(0deg); }
}

/* ------------------------------------------------------------------ mobile */

@media (max-width: 560px) {
  .app {
    gap: 8px;
    padding-top: max(10px, env(safe-area-inset-top));
  }

  .brand-mark {
    font-size: 24px;
  }

  .brand-text h1 {
    font-size: 17px;
  }

  .brand-text .tagline {
    display: none;
  }

  .header-stats {
    gap: 6px;
  }

  .stat-chip {
    font-size: 12.5px;
    padding: 6px 9px;
  }

  .icon-btn {
    width: 32px;
    height: 32px;
    font-size: 15px;
  }

  .btn {
    padding: 9px 14px;
    font-size: 13px;
    min-height: 38px;
  }

  .level-name {
    font-size: 12.5px;
  }

  .difficulty-tag {
    font-size: 10px;
    padding: 3px 8px;
  }

  .progress-label {
    font-size: 12.5px;
  }

  .hint-text {
    font-size: 11.5px;
  }

  .win-card {
    padding: 22px 20px 18px;
  }

  .win-card h2 {
    font-size: 20px;
  }

  .win-stars {
    font-size: 26px;
  }
}

@media (max-width: 360px) {
  .timer-chip {
    display: none;
  }
}
