/* ═══════════════════════════════════════════════════════
   Block Stacker — Paper & Colour-Pencil Design System
   ═══════════════════════════════════════════════════════ */

@import url('https://fonts.googleapis.com/css2?family=Caveat:wght@400;600;700&display=swap');

/* ─── Reset ─────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; }

/* ─── Body / Paper Background ───────────────────────── */
body {
  margin: 0; padding: 0;
  font-family: 'Caveat', 'Comic Sans MS', cursive;
  overflow: hidden;
  width: 100vw; height: 100vh; height: 100dvh;
  display: flex; flex-direction: column;
  touch-action: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none; user-select: none;

  /* Warm cream paper */
  background-color: #f5f0e8;
  background-image:
    /* Horizontal notebook lines */
    repeating-linear-gradient(
      0deg,
      transparent, transparent 28px,
      rgba(180,160,130,0.18) 28px, rgba(180,160,130,0.18) 29px
    ),
    /* Faint vertical grain */
    repeating-linear-gradient(
      90deg,
      transparent, transparent 60px,
      rgba(180,160,130,0.06) 60px, rgba(180,160,130,0.06) 61px
    );
}

/* Portrait notice on mobile landscape */
@media (max-width: 768px) and (orientation: landscape) {
  body::after {
    content: 'Please rotate your device to portrait mode ↩️';
    position: fixed; top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(245,240,232,0.97);
    color: #5a4a3a;
    display: flex; align-items: center; justify-content: center;
    text-align: center; font-size: 1.5em;
    font-family: 'Caveat', cursive;
    z-index: 9999; padding: 20px;
  }
}

.hidden { display: none !important; }

/* ─── HUD Strip ──────────────────────────────────────── */
#hud-container {
  position: relative; z-index: 101;
  display: flex; justify-content: center;
  background: #e8dfc8;
  border-bottom: 3px solid #8b6f47;
  box-shadow:
    0 2px 0 #c4a882,
    0 4px 0 rgba(139,111,71,0.25),
    inset 0 -1px 0 rgba(255,255,255,0.55);
  min-height: 65px; flex-shrink: 0;
}

#hud {
  background: transparent;
  display: flex; align-items: center;
  gap: 16px; padding: 8px 14px;
  flex-wrap: wrap; justify-content: space-between;
  width: 100%; max-width: 520px;
}

/* Score pills */
#score-block {
  display: flex; gap: 10px; flex-wrap: wrap; justify-content: center;
}

#level, #score, #best-score {
  display: flex; align-items: baseline; gap: 5px;
  font-size: 1.35em; color: #3a2a1a;
  background: rgba(255,255,255,0.55);
  padding: 4px 10px; border-radius: 8px;
  border: 2px solid #c4a882;
}
/* Slight tilt for hand-drawn feel */
#level     { transform: rotate(-1deg); }
#best-score{ transform: rotate(0.8deg); }

.score-label {
  font-size: 0.6em; color: #8b6f47; font-weight: 600;
  text-transform: uppercase; letter-spacing: 1px;
}
.score-value {
  font-weight: 700; color: #c8350a;
  font-family: 'Caveat', cursive;
}

/* HUD icon buttons */
#hud-buttons { display: flex; gap: 7px; }

#hud-buttons button {
  background: rgba(255,255,255,0.6);
  border: 2px solid #c4a882; color: #5a4a3a;
  font-size: 1em; padding: 6px 10px; border-radius: 8px;
  cursor: pointer; font-family: 'Caveat', cursive;
  box-shadow: 2px 2px 0 rgba(139,111,71,0.4);
  transition: all 0.18s ease;
}
#hud-buttons button:nth-child(1) { transform: rotate(-1deg); }
#hud-buttons button:nth-child(2) { transform: rotate(0.6deg); }
#hud-buttons button:nth-child(3) { transform: rotate(-0.5deg); }
#hud-buttons button:nth-child(4) { transform: rotate(1deg); }

#hud-buttons button:hover {
  background: rgba(255,220,140,0.8);
  transform: rotate(0) translateY(-2px);
  box-shadow: 3px 4px 0 rgba(139,111,71,0.5);
}
#hud-buttons button:active {
  transform: translateY(0);
  box-shadow: 1px 1px 0 rgba(139,111,71,0.4);
}
.icon { font-size: 1.05em; vertical-align: middle; }

/* Mobile HUD tweaks */
@media (max-width: 768px) and (orientation: portrait) {
  #hud-container { min-height: 78px; }
  #hud { flex-direction: column; justify-content: center;
          gap: 5px; padding: 6px 10px; }
  #score-block { order: 1; width: 100%; justify-content: center; gap: 7px; }
  #hud-buttons { order: 2; justify-content: center; }
  #level, #score, #best-score { font-size: 1em; padding: 3px 8px; }
  #hud-buttons button { font-size: 0.9em; padding: 5px 8px; }
}

/* ─── Canvas Container ───────────────────────────────── */
#canvas-container {
  display: flex; flex-direction: column;
  width: 100%; flex: 1; min-height: 0;
  position: relative; z-index: 2; overflow: hidden;
  align-items: center;
}

#gameCanvas {
  display: block;
  width: 100%; height: 100%;
  flex: 1; min-height: 0;
  max-width: 520px;
  border: none; outline: none;
  touch-action: none;
  cursor: crosshair;
}

@media (min-width: 769px) {
  #canvas-container { max-width: 520px; margin: 0 auto; }
}

/* ─── Pause Menu (shown on canvas area while paused) ─── */
#pause-menu {
  position: absolute; left: 0; top: 0; right: 0; bottom: 0;
  display: flex; align-items: center; justify-content: center;
  z-index: 45;
  background: rgba(245,240,232,0.72);
  backdrop-filter: blur(3px);
}

#pause-panel {
  background: #fdf8f0;
  border: 3px solid #8b6f47;
  border-radius: 8px;
  padding: 28px 36px;
  text-align: center;
  box-shadow: 5px 5px 0 #c4a882, 9px 9px 0 rgba(196,168,130,0.35);
  transform: rotate(-0.6deg);
  animation: panel-appear 0.22s ease-out;
}

#pause-panel h2 {
  color: #c8350a; font-size: 2em; margin: 0 0 20px;
  font-family: 'Caveat', cursive; font-weight: 700;
}

.pause-buttons {
  display: flex; flex-direction: column;
  gap: 12px; align-items: center;
}

#resume-btn, #quit-to-menu-btn {
  min-width: 180px;
  background: rgba(255,255,255,0.75);
  border: 2.5px solid #8b6f47; color: #3a2a1a;
  font-size: 1.3em; padding: 11px 20px; border-radius: 7px;
  font-weight: 600; cursor: pointer;
  font-family: 'Caveat', cursive;
  display: inline-flex; align-items: center; justify-content: center;
  transition: all 0.18s ease;
  box-shadow: 3px 3px 0 #c4a882;
}
#resume-btn {
  background: #d4edda; border-color: #4a8a5a;
  box-shadow: 3px 3px 0 #8ab48a;
  transform: rotate(0.5deg);
}
#resume-btn:hover, #quit-to-menu-btn:hover {
  transform: rotate(0) translateY(-3px);
  box-shadow: 4px 6px 0 rgba(139,111,71,0.6);
  background: #fff8e1;
}
#resume-btn:active, #quit-to-menu-btn:active {
  transform: translateY(0);
  box-shadow: 1px 1px 0 rgba(139,111,71,0.4);
}

/* ─── Overlay ────────────────────────────────────────── */
#overlay {
  position: absolute; left: 0; top: 0; right: 0; bottom: 0;
  display: flex; align-items: center; justify-content: center;
  z-index: 50; pointer-events: none;
  transition: background 0.35s ease;
}
#overlay:not(.hidden) {
  background: rgba(245,240,232,0.72);
  backdrop-filter: blur(4px);
  pointer-events: auto;
}
#overlay .overlay-content { pointer-events: auto; }

/* ─── Overlay Content Panels ─────────────────────────── */
.overlay-content {
  background: #fdf8f0;
  border: 3px solid #8b6f47;
  border-radius: 6px;
  padding: 26px 28px;
  text-align: center;
  max-width: 440px; width: 92%;
  max-height: 88vh; overflow-y: auto;
  animation: panel-appear 0.28s ease-out;
  /* Layered pencil-shadow stack */
  box-shadow:
    4px 4px 0 #c4a882,
    7px 7px 0 rgba(196,168,130,0.45),
    10px 10px 0 rgba(196,168,130,0.18);
  transform: rotate(-0.4deg);
}

@keyframes panel-appear {
  from { opacity: 0; transform: rotate(-0.4deg) scale(0.87); }
  to   { opacity: 1; transform: rotate(-0.4deg) scale(1); }
}

.overlay-content h1 {
  color: #c8350a; font-size: 2.8em; margin: 0 0 14px;
  font-family: 'Caveat', cursive; font-weight: 700;
  text-shadow: 3px 3px 0 rgba(200,53,10,0.18);
  display: inline-block; transform: rotate(-1deg);
}
.overlay-content h2 {
  color: #c8350a; font-size: 2em; margin: 0 0 12px;
  font-family: 'Caveat', cursive; font-weight: 700;
}
.overlay-content p { color: #3a2a1a; margin: 8px 0; font-size: 1.1em; }

/* ─── Difficulty Selector ────────────────────────────── */
#difficulty-selector { margin: 10px 0 14px; }
.diff-label { color: #5a4a3a; font-size: 1.15em; margin-bottom: 8px; }
.diff-buttons { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

.diff-btn {
  background: rgba(255,255,255,0.72);
  border: 2px solid #c4a882; border-radius: 8px;
  padding: 9px 14px; cursor: pointer;
  font-family: 'Caveat', cursive; font-size: 1em; font-weight: 600;
  color: #5a4a3a; line-height: 1.3; min-width: 88px;
  box-shadow: 2px 2px 0 #c4a882;
  transition: all 0.18s;
}
.diff-btn.active {
  background: #ffd97a; border-color: #8b6f47; color: #3a2a1a;
  box-shadow: 3px 3px 0 #8b6f47; transform: rotate(-1deg);
}
.diff-btn:hover:not(.active) {
  background: rgba(255,218,140,0.65); transform: translateY(-2px);
}
.diff-btn small { font-size: 0.74em; color: #8b6f47; display: block; }

/* ─── Action Buttons (start / game-over screens) ─────── */
.start-buttons {
  display: flex; flex-direction: column;
  gap: 11px; align-items: center; margin-top: 16px;
}
.start-buttons button { min-width: 200px; }

#info-btn, #start-btn, #visit-website-btn, #share-btn,
#replay-btn, #gameover-visit-website-btn, #gameover-share-btn,
#close-info-btn {
  background: rgba(255,255,255,0.75);
  border: 2.5px solid #8b6f47; color: #3a2a1a;
  font-size: 1.3em; padding: 11px 20px; border-radius: 7px;
  font-weight: 600; cursor: pointer;
  font-family: 'Caveat', cursive;
  display: inline-flex; align-items: center; justify-content: center;
  transition: all 0.18s ease;
  box-shadow: 3px 3px 0 #c4a882, 4px 4px 0 rgba(196,168,130,0.35);
  transform: rotate(-0.5deg);
}
#start-btn, #replay-btn {
  background: #d4edda; border-color: #4a8a5a;
  box-shadow: 3px 3px 0 #8ab48a;
  transform: rotate(0.5deg);
}
#info-btn:hover, #start-btn:hover, #visit-website-btn:hover,
#share-btn:hover, #replay-btn:hover,
#gameover-visit-website-btn:hover, #gameover-share-btn:hover,
#close-info-btn:hover {
  transform: rotate(0) translateY(-3px);
  box-shadow: 4px 6px 0 rgba(139,111,71,0.6);
  background: #fff8e1;
}
#info-btn:active, #start-btn:active, #visit-website-btn:active,
#share-btn:active, #replay-btn:active,
#gameover-visit-website-btn:active, #gameover-share-btn:active,
#close-info-btn:active {
  transform: translateY(0);
  box-shadow: 1px 1px 0 rgba(139,111,71,0.4);
}

/* ─── How-to-Play Screen ─────────────────────────────── */
#info-screen {
  max-width: 500px; width: 95%;
  max-height: 86vh; overflow-y: auto; text-align: left;
}
#info-screen h2 { text-align: center; margin-bottom: 18px; }
.info-content { display: grid; gap: 14px; margin-bottom: 18px; }
.info-section {
  background: rgba(255,255,255,0.5);
  border: 1.5px solid rgba(196,168,130,0.55);
  border-radius: 6px; padding: 11px 14px;
}
.info-section h3 {
  color: #8b6f47; margin: 0 0 7px; font-size: 1.15em;
  font-family: 'Caveat', cursive;
}
.info-section p { margin: 0 0 5px; line-height: 1.5; font-size: 1em; color: #3a2a1a; }
.info-section p strong { color: #c8350a; }

/* ─── Sales Pitch ────────────────────────────────────── */
#sales-pitch {
  margin: 14px 0; padding: 11px 14px;
  background: rgba(255,175,90,0.1);
  border: 2px solid rgba(200,53,10,0.3);
  border-radius: 8px; transform: rotate(0.3deg);
}
.cta-text {
  color: #c8350a; font-size: 1.1em; font-weight: 600;
  margin: 0; line-height: 1.4;
}

/* ─── Leaderboard Panel (game-over) ─────────────────── */
#leaderboard-panel {
  margin: 12px 0; padding: 13px;
  background: rgba(255,255,255,0.42);
  border: 1.5px solid rgba(196,168,130,0.5);
  border-radius: 8px;
}
#leaderboard-panel h3 { color: #8b6f47; margin: 0 0 9px; font-size: 1.15em; }
#leaderboard-list { list-style: none; padding: 0; margin: 0; }
.lb-entry {
  display: flex; justify-content: space-between; align-items: center;
  padding: 4px 0; border-bottom: 1px dashed rgba(196,168,130,0.5);
  font-family: 'Caveat', cursive;
}
.lb-entry:last-child { border-bottom: none; }
.lb-rank  { color: #8b6f47; font-weight: bold; min-width: 26px; }
.lb-name  { flex: 1; text-align: left; padding: 0 8px; color: #3a2a1a; }
.lb-score { color: #c8350a; font-weight: bold; }
.lb-loading, .lb-empty { color: rgba(90,74,58,0.55); font-style: italic; font-size: 0.9em; }
.lb-status { font-size: 0.82em; color: rgba(90,74,58,0.5); margin-top: 6px; }

/* ─── Leaderboard Modal ──────────────────────────────── */
#leaderboard-overlay {
  position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(245,240,232,0.87);
  display: flex; align-items: center; justify-content: center;
  z-index: 200; backdrop-filter: blur(5px);
}
#leaderboard-modal {
  background: #fdf8f0;
  border: 3px solid #8b6f47; border-radius: 7px;
  padding: 24px; min-width: 280px; max-width: 380px; width: 90%;
  box-shadow: 5px 5px 0 #c4a882;
  transform: rotate(-0.4deg);
}
#leaderboard-modal h2 { color: #c8350a; text-align: center; margin: 0 0 14px; }
#leaderboard-overlay-list { list-style: none; padding: 0; margin: 0 0 14px; }

#close-leaderboard-overlay-btn {
  display: block; margin: 0 auto;
  background: rgba(255,255,255,0.7);
  border: 2px solid #8b6f47; color: #3a2a1a;
  padding: 9px 22px; border-radius: 7px; font-weight: 600;
  cursor: pointer; font-family: 'Caveat', cursive; font-size: 1.1em;
  transition: all 0.18s;
  box-shadow: 2px 2px 0 #c4a882;
}
#close-leaderboard-overlay-btn:hover {
  background: #fff8e1; transform: translateY(-2px);
  box-shadow: 3px 4px 0 rgba(139,111,71,0.5);
}

/* ─── Version Info ────────────────────────────────────── */
#version-info {
  position: fixed;
  bottom: calc(8px + env(safe-area-inset-bottom, 0px));
  right: 10px; z-index: 15; pointer-events: none;
  transition: opacity 0.3s;
}
#version-info p {
  color: rgba(90,74,58,0.38); font-size: 0.78em;
  margin: 0; font-family: 'Caveat', cursive;
}
body.game-playing #version-info { opacity: 0; pointer-events: none; }
