:root {
  --wood-dark: #2c180d;
  --wood-mid: #5b3420;
  --wood-light: #8a5a35;
  --panel: #f4e8ce;
  --panel-edge: #d8c49c;
  --ink: #2a1c12;
  --ink-soft: #6b543f;
  --orange: #ff8a1e;
  --orange-deep: #e8630a;
  --red: #e3402c;
  --red-deep: #b6281a;
  --yellow: #ffd23f;
  --green: #4caf50;
  --shadow: 0 10px 24px rgba(20, 10, 4, 0.45);
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  overscroll-behavior: none;
}

body {
  font-family: "Nunito", "Segoe UI", sans-serif;
  font-size: 16px;
  line-height: 1.5;
  color: var(--panel);
  background:
    radial-gradient(ellipse at 50% -10%, rgba(255, 170, 80, 0.18), transparent 55%),
    repeating-linear-gradient(100deg, var(--wood-mid) 0px, var(--wood-mid) 26px, var(--wood-dark) 26px, var(--wood-dark) 52px);
  overflow-x: hidden;
}

.wrap {
  position: relative;
  max-width: 640px;
  margin: 0 auto;
  padding: max(3vh, env(safe-area-inset-top)) 18px 6vh;
  padding-left: max(18px, env(safe-area-inset-left));
  padding-right: max(18px, env(safe-area-inset-right));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1em;
}

.mute-btn {
  flex: 0 0 auto;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 2px solid rgba(244, 232, 206, 0.35);
  background: transparent;
  color: rgba(244, 232, 206, 0.85);
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.mute-btn:hover,
.mute-btn:focus {
  color: var(--yellow);
  border-color: var(--yellow);
}

.mute-btn.is-muted {
  color: rgba(244, 232, 206, 0.45);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.back-link {
  color: rgba(244, 232, 206, 0.75);
  text-decoration: none;
  font-weight: 700;
  font-size: 0.9rem;
  white-space: nowrap;
}

.back-link:hover,
.back-link:focus {
  color: var(--yellow);
}

.cabinet {
  position: relative;
  background: var(--panel);
  border: 4px solid var(--ink);
  border-radius: 22px;
  box-shadow: var(--shadow);
  padding: 1.6em 1.3em;
  color: var(--ink);
}

.hidden {
  display: none !important;
}

/* ---------- Title screen ---------- */

.screen-title {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 0.9em;
}

.logo-plate {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  gap: 0.15em;
}

.logo-face {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  object-fit: cover;
  border: 4px solid var(--ink);
  box-shadow: 0 0 0 4px var(--yellow);
  transform: rotate(-6deg);
}

.logo-text {
  font-family: "Baloo 2", sans-serif;
  font-weight: 800;
  font-size: clamp(2rem, 9vw, 2.8rem);
  line-height: 0.95;
  letter-spacing: 0.02em;
  color: var(--orange);
  -webkit-text-stroke: 2px var(--ink);
  text-shadow: 3px 3px 0 var(--red-deep);
  text-align: center;
}

.tagline {
  margin: 0;
  color: var(--ink-soft);
  font-weight: 700;
  max-width: 32ch;
}

.stat-row {
  display: flex;
  gap: 1em;
}

.stat {
  background: var(--ink);
  color: var(--panel);
  font-weight: 800;
  font-size: 0.85rem;
  letter-spacing: 0.05em;
  padding: 0.4em 0.9em;
  border-radius: 999px;
}

.stat span {
  color: var(--yellow);
  margin-left: 0.35em;
}

.setting-block {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5em;
  margin-top: 0.4em;
  width: 100%;
}

.setting-label {
  font-weight: 800;
  color: var(--ink-soft);
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.segmented {
  display: flex;
  border: 3px solid var(--ink);
  border-radius: 14px;
  overflow: hidden;
}

.seg-btn {
  font-family: "Baloo 2", sans-serif;
  font-weight: 700;
  font-size: 1.05rem;
  border: none;
  border-right: 3px solid var(--ink);
  background: var(--panel-edge);
  color: var(--ink);
  padding: 0.55em 1em;
  cursor: pointer;
}

.seg-btn:last-child {
  border-right: none;
}

.seg-btn.selected {
  background: var(--yellow);
}

.seg-btn:hover {
  background: #ffe27a;
}

.cta-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.7em;
  margin-top: 0.4em;
  flex-wrap: wrap;
}

.btn {
  font-family: "Baloo 2", sans-serif;
  font-weight: 700;
  font-size: 1.05rem;
  letter-spacing: 0.06em;
  border: 3px solid var(--ink);
  border-radius: 14px;
  padding: 0.6em 1.6em;
  cursor: pointer;
  box-shadow: 0 4px 0 var(--ink);
}

.btn:active {
  transform: translateY(2px);
  box-shadow: 0 2px 0 var(--ink);
}

.btn-primary {
  background: var(--orange);
  color: #fff;
}

.btn-primary:hover,
.btn-primary:focus {
  background: var(--orange-deep);
}

.btn-secondary {
  background: var(--panel-edge);
  color: var(--ink);
}

.btn-secondary:hover,
.btn-secondary:focus {
  background: #cbb98c;
}

.btn-icon {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 3px solid var(--ink);
  background: var(--panel-edge);
  color: var(--ink);
  font-family: "Baloo 2", sans-serif;
  font-weight: 800;
  font-size: 1.1rem;
  cursor: pointer;
  box-shadow: 0 4px 0 var(--ink);
}

.btn-icon:hover {
  background: #cbb98c;
}

.help-panel {
  background: var(--panel-edge);
  border: 3px solid var(--ink);
  border-radius: 14px;
  padding: 0.9em 1.1em;
  text-align: left;
  font-weight: 600;
  color: var(--ink);
}

.help-panel p {
  margin: 0.4em 0;
}

/* ---------- Board screen ---------- */

.screen-board {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.7em;
}

.board-topline {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5em;
  width: 100%;
}

.board-topline-spacer {
  width: 70px;
  flex: 0 0 auto;
}

.quit-btn {
  flex: 0 0 auto;
  font-family: "Baloo 2", sans-serif;
  font-weight: 700;
  font-size: 0.8rem;
  letter-spacing: 0.02em;
  color: var(--ink-soft);
  background: transparent;
  border: 2px solid var(--panel-edge);
  border-radius: 10px;
  padding: 0.4em 0.7em;
  cursor: pointer;
  white-space: nowrap;
}

.quit-btn:hover,
.quit-btn:focus {
  color: var(--ink);
  border-color: var(--ink);
  background: var(--panel-edge);
}

.board-prompt {
  font-family: "Baloo 2", sans-serif;
  font-weight: 800;
  font-size: 1.3rem;
  margin: 0;
  color: var(--red-deep);
  text-align: center;
}

.board-topline .board-prompt {
  flex: 1 1 auto;
}

.board-hud {
  font-weight: 800;
  color: var(--ink-soft);
  font-size: 0.9rem;
}

.board-hud b {
  color: var(--ink);
}

.board-frame {
  width: 100%;
  background: var(--wood-dark);
  border: 4px solid var(--ink);
  border-radius: 16px;
  padding: 10px;
}

.board-grid {
  display: grid;
  grid-template-columns: repeat(var(--cols, 3), 1fr);
  gap: 6px;
  touch-action: manipulation; /* quick taps shouldn't double-tap zoom */
}

.tile {
  position: relative;
  aspect-ratio: 1 / 1;
  border-radius: 10px;
  border: 3px solid var(--ink);
  background: var(--panel);
  padding: 0;
  overflow: hidden;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.tile img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.tile:hover:not(.cleared):not(.angry-hit) {
  filter: brightness(1.08);
}

.tile.cleared {
  cursor: default;
  overflow: visible;
  z-index: 5;
  background: transparent;
  border-style: dashed;
  border-color: rgba(244, 232, 206, 0.3);
}

.tile.cleared img {
  pointer-events: none;
}

#review-grid .tile.cleared {
  overflow: hidden;
  border-style: solid;
  border-color: var(--ink);
  background: var(--panel);
  filter: grayscale(0.55) brightness(0.85);
}

#review-grid .tile.cleared::after {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(76, 175, 80, 0.28);
}

#review-grid .tile.cleared .check {
  position: absolute;
  top: 3px;
  right: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--green);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid #fff;
}

.tile.angry-hit {
  cursor: default;
  animation: tile-shake 0.5s ease;
  box-shadow: 0 0 0 4px var(--red) inset;
}

.tile.angry-marker {
  box-shadow: 0 0 0 4px var(--red) inset;
}

#review-grid .tile {
  cursor: default;
}

#review-grid .tile:hover {
  filter: none;
}

.tile.angry-marker::after {
  content: "ANGRY";
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--red);
  color: #fff;
  font-family: "Baloo 2", sans-serif;
  font-weight: 700;
  font-size: 0.6rem;
  letter-spacing: 0.05em;
  text-align: center;
  padding: 2px 0;
}

@keyframes tile-shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-4px) rotate(-2deg); }
  40% { transform: translateX(4px) rotate(2deg); }
  60% { transform: translateX(-3px) rotate(-1deg); }
  80% { transform: translateX(3px) rotate(1deg); }
}

.tile.wiggle {
  animation: tile-wiggle 0.6s ease-in-out;
}

.tile.wiggle-hard {
  animation: tile-wiggle-hard 0.45s ease-in-out;
}

@keyframes tile-wiggle {
  0%, 100% { transform: rotate(0deg) scale(1); }
  20% { transform: rotate(-5deg) scale(1.04); }
  40% { transform: rotate(4deg) scale(1.02); }
  60% { transform: rotate(-3deg) scale(1.03); }
  80% { transform: rotate(2deg) scale(1.01); }
}

@keyframes tile-wiggle-hard {
  0%, 100% { transform: rotate(0deg) scale(1); }
  15% { transform: rotate(-10deg) scale(1.09); }
  30% { transform: rotate(9deg) scale(1.06); }
  45% { transform: rotate(-8deg) scale(1.07); }
  60% { transform: rotate(6deg) scale(1.04); }
  75% { transform: rotate(-4deg) scale(1.02); }
  90% { transform: rotate(2deg) scale(1.01); }
}

@media (prefers-reduced-motion: reduce) {
  .tile.wiggle,
  .tile.wiggle-hard {
    animation: none;
  }
}

/* ---------- Jumpscare transition ---------- */

.jumpscare {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at 50% 50%, rgba(227, 64, 44, 0.4), rgba(8, 4, 2, 0.94));
  opacity: 0;
  animation: jumpscare-bg 1.05s ease-in forwards;
}

.jumpscare img {
  width: 42vw;
  min-width: 160px;
  max-width: 360px;
  aspect-ratio: 1 / 1;
  object-fit: contain;
  animation: jumpscare-zoom 1.05s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

@keyframes jumpscare-bg {
  0% { opacity: 0; }
  20% { opacity: 1; }
  100% { opacity: 1; }
}

@keyframes jumpscare-zoom {
  0% { transform: scale(0.25) rotate(-8deg) translate(0, 0); }
  35% { transform: scale(0.95) rotate(2deg) translate(-4px, 2px); }
  55% { transform: scale(1.4) rotate(-3deg) translate(4px, -3px); }
  70% { transform: scale(1.9) rotate(2deg) translate(-3px, 2px); }
  85% { transform: scale(2.3) rotate(-1deg) translate(3px, -2px); }
  100% { transform: scale(2.8) rotate(0deg) translate(0, 0); }
}

@media (prefers-reduced-motion: reduce) {
  .jumpscare,
  .jumpscare img {
    animation: none;
    opacity: 1;
  }
}

.board-hint {
  margin: 0;
  color: var(--ink-soft);
  font-size: 0.85rem;
  font-weight: 600;
  text-align: center;
}

/* ---------- Result screen ---------- */

.screen-result {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.8em;
  text-align: center;
}

.result-heading {
  font-family: "Baloo 2", sans-serif;
  font-weight: 800;
  font-size: clamp(1.5rem, 6vw, 2rem);
  color: var(--red);
  -webkit-text-stroke: 1.5px var(--ink);
  margin: 0;
}

.reveal-frame {
  width: 190px;
  height: 190px;
  border-radius: 50%;
  border: 6px solid var(--ink);
  box-shadow: 0 0 0 6px var(--red), var(--shadow);
  overflow: hidden;
  animation: reveal-pop 0.45s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.reveal-frame img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

@keyframes reveal-pop {
  0% { transform: scale(0.4) rotate(-10deg); opacity: 0; }
  70% { transform: scale(1.08) rotate(3deg); opacity: 1; }
  100% { transform: scale(1) rotate(0deg); }
}

.result-stats {
  display: flex;
  gap: 1em;
}

.share-btn {
  background: transparent;
  border: none;
  color: var(--ink-soft);
  font-weight: 700;
  font-size: 0.85rem;
  text-decoration: underline;
  cursor: pointer;
  padding: 0.3em;
}

.share-btn:hover,
.share-btn:focus {
  color: var(--ink);
}

.hint {
  text-align: center;
  color: rgba(244, 232, 206, 0.7);
  font-size: 0.82rem;
  font-weight: 600;
  margin: 1.2em 0 0;
}

@media (max-width: 520px) {
  .cabinet {
    padding: 1.2em 0.9em;
    border-radius: 18px;
  }
  .logo-text {
    font-size: clamp(1.7rem, 11vw, 2.2rem);
  }
  .logo-face {
    width: 72px;
    height: 72px;
  }
  .reveal-frame {
    width: 150px;
    height: 150px;
  }
  .segmented {
    width: 100%;
    justify-content: center;
  }
  .seg-btn {
    flex: 1;
    padding: 0.55em 0.4em;
  }
}
