:root {
  --ground: #14170f;
  --crate: #23291b;
  --crate-slat: #2b3220;
  --crate-edge: #171b10;
  --ink: #f2ead4;
  --ink-dim: rgba(242, 234, 212, 0.58);
  --ink-faint: rgba(242, 234, 212, 0.34);
  --apple: #e2543a;
  --apple-deep: #8f2a1c;
  --apple-hi: #ff9a76;
  --leaf: #7fa050;
  --leaf-deep: #435c29;
  --gold: #e7b84f;
  --gold-deep: #a97e2a;
  --line: rgba(242, 234, 212, 0.14);
  --font-display: "Pretendard Variable", Pretendard, -apple-system, BlinkMacSystemFont,
    "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
}

* { box-sizing: border-box; }

body {
  min-height: 100vh;
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(16px, 4vw, 40px);
  font-family: var(--font-display);
  color: var(--ink);
  background:
    radial-gradient(ellipse 60% 45% at 50% 8%, rgba(231, 184, 79, 0.10), transparent 60%),
    radial-gradient(ellipse 80% 60% at 50% 100%, rgba(226, 84, 58, 0.08), transparent 65%),
    var(--ground);
}

.stage { width: 100%; display: flex; justify-content: center; }

.crate {
  position: relative;
  width: min(100%, 900px);
  background:
    repeating-linear-gradient(
      180deg,
      var(--crate-slat) 0px,
      var(--crate-slat) 34px,
      var(--crate) 34px,
      var(--crate) 68px
    );
  border-radius: 22px;
  border: 1px solid var(--crate-edge);
  box-shadow:
    0 1px 0 rgba(255, 255, 255, 0.04) inset,
    0 24px 60px -24px rgba(0, 0, 0, 0.65),
    0 0 0 6px var(--crate-edge);
  padding: clamp(16px, 3vw, 28px);
  overflow: hidden;
}

.crate::before {
  content: "";
  position: absolute;
  inset: 8px;
  border: 2px solid rgba(0, 0, 0, 0.22);
  border-radius: 16px;
  pointer-events: none;
}

.crate-head {
  position: relative;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}

.eyebrow {
  margin: 0 0 4px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--gold);
}

.title {
  margin: 0;
  font-size: clamp(24px, 4vw, 32px);
  font-weight: 800;
  letter-spacing: -0.01em;
  text-wrap: balance;
}

.stats {
  display: flex;
  gap: 22px;
  position: relative;
}

.stat { text-align: right; }

.stat-label {
  display: block;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin-bottom: 2px;
}

.stat-value {
  display: block;
  font-size: clamp(22px, 3.4vw, 28px);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

.stat.score .stat-value { color: var(--gold); }
.stat.time .stat-value { color: var(--ink); transition: color 0.3s ease; }
.stat.time.low .stat-value { color: var(--apple-hi); }

.timebar-track {
  position: relative;
  height: 6px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.35);
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.4);
  overflow: hidden;
  margin-bottom: 16px;
}

.timebar-fill {
  height: 100%;
  width: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--leaf-deep), var(--gold));
  transition: width 1s linear, background 0.4s ease;
}

.timebar-track.low .timebar-fill {
  background: linear-gradient(90deg, var(--apple-deep), var(--apple-hi));
}

.board-wrap {
  position: relative;
  overflow-x: auto;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.18);
  box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.35);
  padding: 10px;
}

.board {
  --cols: 17;
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(30px, 1fr));
  gap: 4px;
  touch-action: none;
  user-select: none;
  min-width: calc(var(--cols) * 32px);
  position: relative;
}

.apple {
  position: relative;
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: crosshair;
  transition: transform 0.12s ease, filter 0.12s ease;
}

.apple.dead { opacity: 0; pointer-events: none; }

.apple-body {
  position: absolute;
  inset: 16%;
  border-radius: 50%;
  background:
    radial-gradient(circle at 32% 26%, var(--apple-hi) 0%, var(--apple) 42%, var(--apple-deep) 100%);
  box-shadow: 0 2px 3px rgba(0, 0, 0, 0.35), inset 0 -3px 4px rgba(0, 0, 0, 0.25);
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}

.apple-body::before {
  content: "";
  position: absolute;
  top: -14%;
  left: 46%;
  width: 8%;
  height: 22%;
  background: var(--leaf-deep);
  border-radius: 2px;
  transform: rotate(-8deg);
}

.apple-body::after {
  content: "";
  position: absolute;
  top: -10%;
  left: 52%;
  width: 34%;
  height: 20%;
  background: var(--leaf);
  border-radius: 60% 10% 60% 10%;
  transform: rotate(20deg);
}

.apple-num {
  position: relative;
  z-index: 1;
  font-size: clamp(14px, 2.6vw, 22px);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: rgba(255, 250, 242, 0.98);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6);
}

.apple.active .apple-body {
  transform: scale(1.14);
  box-shadow: 0 0 0 2px var(--gold), 0 4px 10px rgba(0, 0, 0, 0.4);
}

.apple.shake { animation: shake 0.32s ease; }
@keyframes shake {
  20% { transform: translateX(-3px) rotate(-3deg); }
  50% { transform: translateX(3px) rotate(3deg); }
  80% { transform: translateX(-2px) rotate(-1deg); }
  100% { transform: translateX(0); }
}

.apple.popping .apple-body {
  animation: pop 0.32s ease forwards;
}
.apple.popping .apple-num { animation: fadeOut 0.2s ease forwards; }
@keyframes pop {
  0% { transform: scale(1.14); }
  50% { transform: scale(1.3); opacity: 0.7; }
  100% { transform: scale(0.2); opacity: 0; }
}
@keyframes fadeOut { to { opacity: 0; } }

.selection-box {
  position: fixed;
  border: 1.5px solid var(--gold);
  background: rgba(231, 184, 79, 0.14);
  border-radius: 4px;
  pointer-events: none;
  z-index: 5;
}

.sum-badge {
  position: fixed;
  z-index: 6;
  pointer-events: none;
  transform: translate(-50%, -140%);
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  background: var(--crate-edge);
  color: var(--ink-dim);
  border: 1px solid var(--line);
  white-space: nowrap;
}
.sum-badge.hit { color: var(--ground); background: var(--gold); border-color: var(--gold); }
.sum-badge.over { color: var(--apple-hi); }

.popup {
  position: absolute;
  z-index: 6;
  pointer-events: none;
  transform: translate(-50%, -50%);
  font-size: 18px;
  font-weight: 800;
  color: var(--gold);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
  animation: floatUp 0.7s ease forwards;
}
@keyframes floatUp {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.8); }
  20% { opacity: 1; transform: translate(-50%, -50%) scale(1.05); }
  100% { opacity: 0; transform: translate(-50%, -140%) scale(1); }
}

.hint {
  margin: 14px 2px 0;
  font-size: 12.5px;
  color: var(--ink-faint);
  text-align: center;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(10, 12, 7, 0.72);
  backdrop-filter: blur(2px);
  border-radius: 12px;
  z-index: 10;
}

.overlay[hidden] {
  display: none;
}

.overlay-card {
  text-align: center;
  padding: 28px 34px;
  background: var(--crate);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 20px 50px -20px rgba(0, 0, 0, 0.7);
}

.overlay-eyebrow {
  margin: 0 0 6px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--gold);
}

.overlay-score {
  margin: 0;
  font-size: 48px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
}

.overlay-sub {
  margin: 4px 0 20px;
  color: var(--ink-dim);
  font-size: 14px;
}

.btn-restart {
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  color: var(--ground);
  background: linear-gradient(180deg, var(--gold), var(--gold-deep));
  border: none;
  border-radius: 999px;
  padding: 10px 26px;
  cursor: pointer;
  box-shadow: 0 6px 16px -6px rgba(231, 184, 79, 0.5);
}
.btn-restart:hover { filter: brightness(1.06); }
.btn-restart:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 3px;
}

@media (prefers-reduced-motion: reduce) {
  .apple, .apple-body, .timebar-fill { transition: none !important; }
  .apple.shake, .apple.popping .apple-body, .apple.popping .apple-num, .popup {
    animation: none !important;
  }
  .apple.popping .apple-body { opacity: 0; }
}
