:root {
  --wall: #f1dfbd;
  --wall-2: #e6c995;
  --wall-line: rgba(140, 92, 48, 0.07);
  --panel: #2e6b66;
  --panel-2: #1f4f4b;
  --wood: #a86d3c;
  --wood-light: #cf9257;
  --wood-dark: #6b4022;
  --ink: #3a2414;
  --ink-soft: #7a5638;
  --shadow: rgba(58, 36, 20, 0.35);
  --track: rgba(58, 36, 20, 0.16);
  --go: #3f9a5a;
  --go-light: #6cc283;
  --warn: #d2452c;
  --warn-light: #f07a57;
  --on-bar: #2b1a0e;
  --scrim: rgba(43, 26, 14, 0.55);
  --card: #fff6e6;
  --chip: #f0dcb6;
  --btn: #c9452e;
  --btn-light: #e8664a;
  --btn-dark: #8e2a1a;
  --on-btn: #fff8ef;
  --focus: #2e6b66;
  --font: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --slot: 64px;
  --cols: 2;
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  height: 100dvh;
  margin: 0;
  overflow: hidden;
  overflow-x: clip;
  overscroll-behavior: none;
  background: var(--wall);
  color: var(--ink);
  font-family: var(--font);
}

body {
  background:
    repeating-linear-gradient(90deg, var(--wall-line) 0 2px, transparent 2px 48px),
    linear-gradient(180deg, var(--wall) 0%, var(--wall-2) 100%);
}

#game {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: max(10px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

.hud {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 6px 12px;
  max-width: 720px;
  width: 100%;
  margin: 0 auto 8px;
}
.title { margin: 0; font-size: 20px; font-weight: 800; color: var(--ink); }
.level { margin: 0; font-size: 16px; font-weight: 700; color: var(--ink-soft); }

.timer {
  position: relative;
  flex: 1 0 100%;
  height: 22px;
  border-radius: 11px;
  overflow: hidden;
  background: var(--track);
  box-shadow: inset 0 2px 3px rgba(0, 0, 0, 0.18);
}
.timer-fill {
  position: absolute;
  inset: 0;
  transform-origin: left center;
  background: linear-gradient(180deg, var(--go-light), var(--go));
  transition: transform 0.25s linear;
}
.timer-text {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: 14px;
  font-weight: 800;
  color: var(--on-bar);
  font-variant-numeric: tabular-nums;
}
#game[data-state="home"] .timer, #game[data-state="home"] .level { visibility: hidden; }
.timer.low .timer-fill { background: linear-gradient(180deg, var(--warn-light), var(--warn)); }
.timer.low { animation: pulse 0.9s ease-in-out infinite; }
@keyframes pulse { 50% { transform: scale(1.03); } }

.board-wrap { position: relative; flex: 1; min-height: 0; }

.screen {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  place-items: center;
  padding: 16px;
  background: var(--scrim);
  animation: fade-in 0.2s ease-out;
}
.card {
  width: min(100%, 380px);
  max-height: 100%;
  overflow: auto;
  padding: 24px 20px 18px;
  border-radius: 22px;
  background: var(--card);
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.3);
  text-align: center;
  animation: pop-in 0.25s cubic-bezier(.2, .8, .2, 1);
}
.screen-title { margin: 0 0 6px; font-size: 30px; font-weight: 800; color: var(--ink); }
.screen-text { margin: 0 0 18px; font-size: 17px; color: var(--ink-soft); }
.btn, .level-btn {
  font: inherit;
  cursor: pointer;
  border: 0;
  touch-action: manipulation;
}
.btn {
  display: block;
  width: 100%;
  min-height: 52px;
  border-radius: 16px;
  background: linear-gradient(180deg, var(--btn-light), var(--btn));
  color: var(--on-btn);
  font-size: 19px;
  font-weight: 800;
  box-shadow: 0 4px 0 var(--btn-dark);
  transition: transform 0.08s ease-out, opacity 0.15s;
}
.btn:active { transform: translateY(3px); }
.btn:focus-visible, .level-btn:focus-visible, .credit a:focus-visible { outline: 3px solid var(--focus); outline-offset: 3px; }
.levels {
  list-style: none;
  margin: 18px 0 14px;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
}
.level-btn {
  width: 100%;
  min-height: 46px;
  border-radius: 12px;
  background: var(--chip);
  color: var(--ink);
  font-size: 17px;
  font-weight: 800;
  transition: transform 0.08s ease-out;
}
.level-btn:active:not(:disabled) { transform: translateY(2px); }
.level-btn:disabled { cursor: default; opacity: 0.4; }
.credit { margin: 8px 0 0; font-size: 14px; color: var(--ink-soft); }
.credit a { color: var(--ink); font-weight: 700; }
.credit-gap { display: inline-block; width: 10px; }
@keyframes fade-in { from { opacity: 0; } }
@keyframes pop-in { from { opacity: 0; transform: scale(0.94); } }

.board {
  height: 100%;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  max-width: calc(var(--cols) * var(--slot) * 3.12 + (var(--cols) - 1) * var(--slot) * 0.12);
  margin: 0 auto;
  align-content: center;
  gap: calc(var(--slot) * 0.12);
}
.board * { pointer-events: none; }
.board .slot { pointer-events: auto; }

.shelf { position: relative; width: calc(var(--slot) * 3.12); height: calc(var(--slot) * 1.54); }

.shelf-panel {
  position: absolute;
  inset: 0 calc(var(--slot) * 0.02) calc(var(--slot) * 0.12);
  border-radius: calc(var(--slot) * 0.14) calc(var(--slot) * 0.14) 0 0;
  background:
    radial-gradient(120% 70% at 50% 0%, rgba(255, 255, 255, 0.16), transparent 60%),
    linear-gradient(180deg, var(--panel) 0%, var(--panel-2) 100%);
  box-shadow: inset 0 calc(var(--slot) * -0.1) calc(var(--slot) * 0.2) rgba(0, 0, 0, 0.25);
}

.ledge {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: calc(var(--slot) * 0.17);
  border-radius: calc(var(--slot) * 0.05);
  background: linear-gradient(180deg, var(--wood-light) 0%, var(--wood) 45%, var(--wood-dark) 100%);
  box-shadow: 0 calc(var(--slot) * 0.06) calc(var(--slot) * 0.12) var(--shadow);
}

.row-back, .row-front {
  position: absolute;
  left: calc(var(--slot) * 0.06);
  display: grid;
  grid-template-columns: repeat(3, var(--slot));
}
.row-back { bottom: calc(var(--slot) * 0.58); }
.row-front { bottom: calc(var(--slot) * 0.1); }

.row-back img { width: var(--slot); height: var(--slot); transform: scale(0.8); filter: brightness(0.48) saturate(0.65); }
.slot { width: var(--slot); height: var(--slot); position: relative; }
.slot img, .ghost { display: block; object-fit: contain; }
/* the renders carry padding, so items are drawn 16% larger than their slot */
.slot img { position: absolute; width: 116%; height: 116%; left: -8%; top: -12%; }
.ghost { width: 100%; height: 100%; }
.slot img { filter: drop-shadow(0 calc(var(--slot) * 0.03) calc(var(--slot) * 0.03) rgba(0, 0, 0, 0.3)); }

.drag-layer { position: fixed; inset: 0; pointer-events: none; z-index: 50; }
.ghost {
  position: fixed;
  will-change: transform;
  filter: drop-shadow(0 10px 12px rgba(0, 0, 0, 0.35));
}

.noscript { padding: 24px; font-size: 18px; }

@media (prefers-reduced-motion: reduce) {
  .timer.low { animation: none; }
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
