/* ── CSS custom properties ─────────────────────────────────────────── */
:root {
  --bucket-gap: 10px;
}

/* ── Reset & base ──────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

body {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  background: #eef2f7;
  color: #2d3748;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ── Header ────────────────────────────────────────────────────────── */
#app-header {
  background: #2c6a3f;
  color: #fff;
  padding: 10px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-shrink: 0;
}
#app-header h1 { font-size: 1.3rem; font-weight: 700; letter-spacing: 0.02em; }
#btn-back {
  display: inline-flex;
  align-items: center;
  padding: 5px 13px;
  background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.3);
  border-radius: 7px;
  color: #fff;
  font-size: 0.84rem;
  font-weight: 600;
  text-decoration: none;
  transition: background 0.15s;
  flex-shrink: 0;
}
#btn-back:hover { background: rgba(255,255,255,0.28); }

#timer-section { display: flex; align-items: center; gap: 8px; }
#timer-display {
  font-family: 'Courier New', monospace;
  font-size: 1.55rem;
  font-weight: 700;
  background: rgba(0,0,0,0.35);
  color: #fff;
  padding: 3px 12px;
  border-radius: 6px;
  min-width: 76px;
  text-align: center;
  letter-spacing: 0.05em;
}
.btn-timer {
  background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.3);
  color: #fff;
  padding: 5px 12px;
  border-radius: 6px;
  font-size: 0.82rem;
  cursor: pointer;
  transition: background 0.15s;
  min-width: 70px;
  text-align: center;
}
.btn-timer:hover { background: rgba(255,255,255,0.28); }

/* ── Settings bar ──────────────────────────────────────────────────── */
#settings-bar {
  background: #fff;
  border-bottom: 2px solid #dde3ed;
  padding: 8px 20px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 14px;
  flex-shrink: 0;
}
.setting-group { display: flex; align-items: center; gap: 6px; }
.setting-group > label:first-child {
  font-size: 0.82rem; font-weight: 600; color: #4a5568; white-space: nowrap;
}
.setting-group select,
.setting-group input[type="number"] {
  padding: 5px 8px;
  border: 1px solid #cbd5e0;
  border-radius: 6px;
  font-size: 0.88rem;
  background: #f8fafc;
  color: #2d3748;
  outline-color: #38a169;
}
.setting-group input[type="number"] { width: 58px; }

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.82rem;
  font-weight: 600;
  color: #4a5568;
  cursor: pointer;
  white-space: nowrap;
}
.checkbox-label input[type="checkbox"] { cursor: pointer; accent-color: #2c6a3f; }

.btn-primary {
  margin-left: auto;
  background: #2c6a3f;
  color: #fff;
  border: none;
  padding: 8px 24px;
  border-radius: 8px;
  font-size: 0.98rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
}
.btn-primary:hover  { background: #225131; }
.btn-primary:active { transform: scale(0.97); }

/* ── Target picker / extra panel ───────────────────────────────────── */
.btn-targets, .btn-extra {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  border: 1px solid #cbd5e0;
  border-radius: 6px;
  background: #f8fafc;
  color: #2d3748;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
}
.btn-targets:hover, .btn-extra:hover { background: #edf2f7; }
.btn-targets.is-open, .btn-extra.is-open { background: #d4e8d6; border-color: #2c6a3f; }
.btn-targets.is-filtered { border-color: #2c6a3f; color: #225131; }
.btn-targets .targets-count {
  font-weight: 500;
  color: #4a5568;
  background: #edf2f7;
  padding: 1px 6px;
  border-radius: 10px;
  font-size: 0.75rem;
}
.btn-targets.is-filtered .targets-count { background: #d4e8d6; color: #225131; }

#target-panel, #extra-panel {
  background: #fafbfd;
  border-bottom: 2px solid #dde3ed;
  padding: 10px 20px;
  max-height: 40vh;
  overflow-y: auto;
}
.target-panel-header, .extra-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
}
.target-panel-title, .extra-panel-title {
  font-weight: 700; color: #2d3748; font-size: 0.9rem;
}
.target-panel-actions { display: flex; gap: 6px; }
.target-panel-actions button {
  padding: 4px 10px;
  border: 1px solid #cbd5e0;
  background: #fff;
  border-radius: 6px;
  font-size: 0.78rem;
  font-weight: 600;
  color: #2d3748;
  cursor: pointer;
}
.target-panel-actions button:hover { background: #edf2f7; }
.btn-targets-close, .btn-extra-close {
  border: none !important;
  background: transparent !important;
  font-size: 1.2rem !important;
  line-height: 1;
  padding: 2px 8px !important;
  cursor: pointer;
}
.target-panel-body {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 4px 12px;
}
.target-panel-empty { padding: 12px; color: #718096; font-style: italic; }
.target-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 3px 4px;
  font-size: 0.82rem;
  color: #2d3748;
  cursor: pointer;
  border-radius: 4px;
}
.target-row:hover { background: #edf2f7; }
.target-row input[type="checkbox"] { cursor: pointer; accent-color: #2c6a3f; }
.target-thumb {
  width: 32px;
  height: 32px;
  object-fit: contain;
  flex-shrink: 0;
  border-radius: 4px;
  border: 1px solid #e2e8f0;
  background: #f7fafc;
}
.target-row-label { flex: 1 1 auto; }

.extra-panel-body {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 10px 20px;
  align-items: center;
}
.extra-panel-body .checkbox-label { padding: 4px 0; }
.extra-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

/* ── Game area ─────────────────────────────────────────────────────── */
#game-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 0;
  min-height: 0;
  position: relative;
}

/* ── Market stage ───────────────────────────────────────────────────── */
#market-stage {
  position: relative;
  flex: 1 1 auto;
  width: 100%;
  min-height: 0;
  overflow: hidden;
  background: #f4e6c8;
}

#market-backdrop {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

/* Foreground shelf occupies x=20..760 (2..76%) and y=234..532 (39..89%)
   in the SVG (after restructure). Grid sits inside the shelf, inset so
   the uprights and header valance frame it. */
#comp-grid {
  position: absolute;
  left: 4.5%;
  right: 24.5%;
  top: 41%;
  bottom: 13%;
  display: grid;
  grid-template-columns: repeat(var(--grid-cols, 4), 1fr);
  grid-auto-rows: 1fr;
  gap: var(--bucket-gap);
  z-index: 4;
  padding: 4px 4px 0;
}

/* HTML shelf planks rendered into #shelf-planks at runtime — one per
   bucket-grid row — so the wooden plank always sits exactly under the
   buckets at any array size. Sits in the SAME box as #comp-grid. */
#shelf-planks {
  position: absolute;
  left: 4.5%;
  right: 24.5%;
  top: 41%;
  bottom: 13%;
  pointer-events: none;
  z-index: 3;
}
.shelf-plank {
  position: absolute;
  left: -12px;
  right: -12px;
  height: 12px;
  background: linear-gradient(#b78758 0%, #7a5630 55%, #5e421f 100%);
  border-top: 1.5px solid #f0d3a0;
  border-bottom: 1px solid #2e1d0c;
  box-shadow: 0 3px 5px rgba(0,0,0,0.28);
  transform: translateY(-2px);
}

/* Shelf item — clean card sitting on a wooden plank edge */
.bucket {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fffaf0;
  border: 2px solid #d6c39a;
  border-bottom: 3px solid #8a6238;
  border-radius: 6px 6px 2px 2px;
  box-shadow: 0 3px 4px rgba(0,0,0,0.25);
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s, border-color 0.15s;
  overflow: hidden;
  min-height: 0;
}
.bucket:not(.bucket-disabled):hover {
  transform: translateY(-3px);
  border-color: #2c6a3f;
  box-shadow: 0 6px 14px rgba(44,106,63,0.32);
}
.bucket.bucket-disabled { cursor: default; }
.bucket img {
  max-width: 88%;
  max-height: 88%;
  object-fit: contain;
  pointer-events: none;
  display: block;
  transition: transform 0.5s cubic-bezier(0.4,0,0.2,1), opacity 0.3s;
}

/* Flying-item clone */
.item-flying {
  position: absolute;
  z-index: 30;
  pointer-events: none;
  animation: item-to-bag 540ms cubic-bezier(0.4, 0, 0.4, 1) forwards;
}

/* ── Customer ───────────────────────────────────────────────────────── */
/* Customer stands on the floor next to the right-side counter.
   Floor in the backdrop is y=500..600 (bottom ~17%).
   Counter is x=780..1000.  Customer should stand in front of /
   beside the counter, feet on the floor.  We anchor the layer to the
   bottom-right of the stage so the customer's feet land on the floor. */
/* Customer stands at the right, BEHIND the foreground counter, so the
   procedurally-drawn waist crops cleanly into the counter top.
   Enters from off-screen right and exits to the right. */
#customer-layer {
  position: absolute;
  right: 3%;
  bottom: 1%;
  width: 22%;
  max-width: 240px;
  min-width: 130px;
  height: 70%;
  z-index: 6;
  pointer-events: none;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.customer-svg {
  width: 100%;
  height: 100%;
  display: block;
  transform-origin: bottom center;
}

/* Animation tiers — customer enters from the right facing left,
   stands facing forward at the shelf, then exits walking right. */
#customer-layer.tier-full.entering .customer-svg   { animation: customer-walk-in 800ms ease-out both; }
#customer-layer.tier-full.exiting  .customer-svg   { animation: customer-walk-out 800ms ease-in  both; }
#customer-layer.tier-light.entering .customer-svg  { animation: customer-fade-in 280ms ease-out both; }
#customer-layer.tier-light.exiting  .customer-svg  { animation: customer-fade-out 280ms ease-in both; }

@keyframes customer-walk-in {
  0%   { transform: translateX(380%) translateY(0)    scale(0.93) scaleX(-1); }
  20%  { transform: translateX(300%) translateY(-3px) scale(0.94) scaleX(-1); }
  40%  { transform: translateX(200%) translateY(0)    scale(0.96) scaleX(-1); }
  60%  { transform: translateX(120%) translateY(-3px) scale(0.97) scaleX(-1); }
  80%  { transform: translateX(40%)  translateY(0)    scale(0.99) scaleX(-1); }
  100% { transform: translateX(0)    translateY(0)    scale(1)    scaleX(1); }
}
@keyframes customer-walk-out {
  0%   { transform: translateX(0)    translateY(0)    scale(1); }
  20%  { transform: translateX(40%)  translateY(-3px) scale(0.99); }
  50%  { transform: translateX(140%) translateY(0)    scale(0.97); }
  80%  { transform: translateX(260%) translateY(-3px) scale(0.94); }
  100% { transform: translateX(380%) translateY(0)    scale(0.92); }
}
@keyframes customer-fade-in {
  0%   { opacity: 0; transform: translateY(12px) scale(0.96); }
  100% { opacity: 1; transform: translateY(0)    scale(1); }
}
@keyframes customer-fade-out {
  0%   { opacity: 1; transform: translateY(0)    scale(1); }
  100% { opacity: 0; transform: translateY(-8px) scale(0.96); }
}

/* ── Speech bubble ──────────────────────────────────────────────────── */
/* Speech bubble sits just above and slightly left of the customer's head,
   tail pointing down-right toward them. */
#speech-bubble {
  position: absolute;
  right: 20%;
  top: 17%;
  width: 17%;
  max-width: 210px;
  min-width: 130px;
  z-index: 9;
  opacity: 0;
  transform: scale(0.4);
  transform-origin: bottom right;
  pointer-events: none;
}
#speech-bubble.shown  { animation: bubble-pop 320ms cubic-bezier(0.34, 1.56, 0.64, 1) forwards; }
#speech-bubble.hiding { animation: bubble-pop-out 220ms ease-in forwards; }
@keyframes bubble-pop {
  0%   { opacity: 0; transform: scale(0.4); }
  60%  { opacity: 1; transform: scale(1.06); }
  100% { opacity: 1; transform: scale(1); }
}
@keyframes bubble-pop-out {
  0%   { opacity: 1; transform: scale(1); }
  100% { opacity: 0; transform: scale(0.4); }
}

#speech-bubble-inner {
  background: #fff;
  border: 3px solid #2d3748;
  border-radius: 22px;
  padding: 14px 14px 12px;
  box-shadow: 0 6px 16px rgba(0,0,0,0.18);
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}
#sample-img {
  width: 100%;
  max-height: 130px;
  object-fit: contain;
  display: block;
}
#speech-caption {
  font-size: 0.85rem;
  font-weight: 600;
  color: #2d3748;
  text-align: center;
  font-style: italic;
}
#speech-bubble-tail {
  position: absolute;
  bottom: -22px;
  right: 28px;
  width: 0;
  height: 0;
  border-left: 14px solid transparent;
  border-right: 14px solid transparent;
  border-top: 24px solid #2d3748;
}
#speech-bubble-tail::after {
  content: '';
  position: absolute;
  top: -28px;
  left: -10px;
  width: 0;
  height: 0;
  border-left: 10px solid transparent;
  border-right: 10px solid transparent;
  border-top: 18px solid #fff;
}

/* ── Front counter (foreground, in front of customer) ───────────────── */
/* Anchors to the bottom-right of the stage so the customer's truncated
   bottom half is always concealed at any aspect ratio. */
#front-counter {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 32%;
  max-width: 360px;
  min-width: 180px;
  height: 24%;
  min-height: 110px;
  z-index: 7;
  pointer-events: none;
}
#front-counter svg { width: 100%; height: 100%; display: block; }

/* ── Bag (sits on the counter top, near the customer) ───────────────── */
#bag-on-counter {
  position: absolute;
  right: 19%;
  bottom: 23%;
  width: 7%;
  min-width: 50px;
  max-width: 90px;
  z-index: 8;
  pointer-events: none;
  transition: transform 0.3s ease;
}
#bag-on-counter svg { width: 100%; height: auto; display: block; }
#bag-on-counter.bag-shake { animation: bag-shake 0.4s ease; }
#bag-on-counter.bag-handoff { animation: bag-handoff 0.6s ease forwards; }
@keyframes bag-shake {
  0%, 100% { transform: translateY(0) rotate(0); }
  25%      { transform: translateY(-6px) rotate(-3deg); }
  60%      { transform: translateY(-4px) rotate(3deg); }
}
@keyframes bag-handoff {
  0%   { transform: translateX(0)   rotate(0); }
  100% { transform: translateX(110%) rotate(-12deg); }
}

/* ── Item flying from bucket to bag ─────────────────────────────────── */
@keyframes item-to-bag {
  0%   { transform: translate(0, 0)             scale(1)   rotate(0); opacity: 1; }
  60%  { transform: translate(var(--dx, 0), var(--dy, 0)) scale(0.7) rotate(180deg); opacity: 1; }
  100% { transform: translate(var(--dx, 0), var(--dy, 0)) scale(0.3) rotate(360deg); opacity: 0; }
}

/* ── Floating emoji reaction ────────────────────────────────────────── */
#floating-emojis {
  position: absolute;
  right: 8%;
  top: 10%;
  width: 16%;
  height: 30%;
  z-index: 8;
  pointer-events: none;
}
.floating-emoji {
  position: absolute;
  font-size: 2.4rem;
  animation: emoji-float 1100ms ease-out forwards;
  opacity: 0;
}
@keyframes emoji-float {
  0%   { opacity: 0; transform: translateY(20px) scale(0.6); }
  20%  { opacity: 1; transform: translateY(0)    scale(1.1); }
  60%  { opacity: 1; transform: translateY(-30px) scale(1); }
  100% { opacity: 0; transform: translateY(-70px) scale(0.9); }
}

/* ── Bucket feedback (wrong click) ──────────────────────────────────── */
@keyframes bucket-jiggle {
  0%   { transform: translateX(0)        rotate(0); }
  15%  { transform: translateX(-8px)     rotate(-3deg); }
  30%  { transform: translateX(8px)      rotate(3deg); }
  45%  { transform: translateX(-6px)     rotate(-2deg); }
  60%  { transform: translateX(6px)      rotate(2deg); }
  75%  { transform: translateX(-3px)     rotate(-1deg); }
  90%  { transform: translateX(3px)      rotate(1deg); }
  100% { transform: translateX(0)        rotate(0); }
}
.bucket.jiggle { animation: bucket-jiggle 0.5s ease; }
@keyframes flash-red {
  0%   { background: #fffaf0; }
  40%  { background: #ffd1d1; }
  100% { background: #fffaf0; }
}
.bucket.flash-red { animation: flash-red 0.5s ease; }

/* ── Prompt effects ────────────────────────────────────────────────── */
@keyframes sparkle-pulse {
  0%   { box-shadow: 0 3px 4px rgba(0,0,0,0.25),
                     0 0 8px 4px #ffd700,
                     0 0 18px 8px rgba(255,215,0,0.55); }
  50%  { box-shadow: 0 3px 4px rgba(0,0,0,0.25),
                     0 0 14px 7px #ffd700,
                     0 0 30px 14px rgba(255,140,0,0.5); }
  100% { box-shadow: 0 3px 4px rgba(0,0,0,0.25),
                     0 0 8px 4px #ffd700,
                     0 0 18px 8px rgba(255,215,0,0.55); }
}
.bucket.prompt-sparkle {
  animation: sparkle-pulse 1s ease-in-out infinite;
  border-color: #d69e2e !important;
}
.bucket.prompt-outline {
  border: 3px solid #38a169 !important;
  border-bottom-width: 6px !important;
  box-shadow: 0 0 0 3px rgba(56,161,105,0.45),
              0 3px 8px rgba(0,0,0,0.1) !important;
}

/* ── Trial overlay (Next / Retry) ──────────────────────────────────── */
#trial-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  pointer-events: none;
  z-index: 50;
}
.btn-watermark {
  pointer-events: all;
  padding: 11px 30px;
  border-radius: 12px;
  font-size: 1.1rem;
  font-weight: 800;
  cursor: pointer;
  border: none;
  box-shadow: 0 4px 18px rgba(0,0,0,0.32);
  transition: opacity 0.15s, transform 0.1s;
  letter-spacing: 0.02em;
}
.btn-watermark:hover  { opacity: 0.93; transform: scale(1.04); }
.btn-watermark:active { transform: scale(0.97); }
.btn-watermark-next  { background: rgba(44, 106, 63, 0.94); color: #fff; }
.btn-watermark-retry { background: rgba(192, 86, 33, 0.92); color: #fff; }

/* ── Bottom bar ────────────────────────────────────────────────────── */
#bottom-bar {
  background: #fff;
  border-top: 2px solid #dde3ed;
  padding: 9px 20px;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}
.btn-prompt {
  background: #dd6b20;
  color: #fff;
  border: none;
  padding: 5px 14px;
  border-radius: 7px;
  font-size: 0.88rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s;
}
.btn-prompt:hover { background: #c05621; }
.btn-print {
  background: #4a5568;
  color: #fff;
  border: none;
  padding: 7px 18px;
  border-radius: 8px;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}
.btn-print:hover { background: #2d3748; }
.btn-clear-data {
  background: #c53030;
  color: #fff;
  border: none;
  padding: 7px 18px;
  border-radius: 8px;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}
.btn-clear-data:hover { background: #9b2c2c; }
.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 34px;
  padding: 0;
}
.btn-icon svg { width: 20px; height: 20px; fill: currentColor; }

/* ── Print section ─────────────────────────────────────────────────── */
#print-section { display: none; }

@media print {
  body > *:not(#print-section) { display: none !important; }
  #print-section {
    display: block;
    padding: 18px 24px;
    font-family: Arial, sans-serif;
    font-size: 11pt;
    color: #000;
  }
  #print-section h2 { font-size: 15pt; margin-bottom: 4px; }
  #print-meta { font-size: 9pt; color: #444; margin-bottom: 14px; }
  #results-table { width: 100%; border-collapse: collapse; margin-bottom: 14px; }
  #results-table th, #results-table td {
    border: 1px solid #666;
    padding: 5px 8px;
    text-align: left;
    vertical-align: top;
  }
  #results-table th { background: #e8e8e8; font-weight: bold; font-size: 9.5pt; }
  #results-table tr:nth-child(even) td { background: #f7f7f7; }
  #results-table tr.settings-changed td { background: #e8e8e8 !important; }
  #results-table td.outcome-error      { color: #c0392b; }
  #results-table td.outcome-prompted   { color: #b7791f; }
  #results-table td.outcome-ok         { color: #27ae60; }
  #results-table td.outcome-correction { color: #6b46c1; }
  #print-summary { font-size: 9.5pt; color: #333; border-top: 1px solid #bbb; padding-top: 8px; }
  #print-summary span { margin-right: 20px; }
}

/* ── Responsive ────────────────────────────────────────────────────── */
@media (max-width: 680px) {
  #app-header { padding: 8px 12px; gap: 8px; }
  #app-header h1 { font-size: 1.05rem; }
  #timer-display { font-size: 1.2rem; min-width: 60px; padding: 2px 8px; }
  .btn-timer { padding: 4px 9px; font-size: 0.76rem; min-width: 0; }

  #settings-bar { padding: 6px 10px; gap: 6px 8px; }
  .setting-group > label:first-child, .checkbox-label { font-size: 0.78rem; }
  .setting-group select, .setting-group input[type="number"] { font-size: 0.82rem; padding: 4px 6px; }

  html, body {
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    overscroll-behavior: contain;
  }
  body { min-height: 0; }

  #game-area    { flex: 1 1 auto; min-height: 0; }
  #speech-bubble  { min-width: 92px; width: 24%; right: 24%; top: 19%; }
  #customer-layer { width: 26%; min-width: 90px; height: 64%; right: 4%; }
  #comp-grid,
  #shelf-planks   { right: 28%; }
  #front-counter  { width: 38%; min-width: 130px; min-height: 90px; height: 22%; }
  #bag-on-counter { width: 9%; min-width: 36px; right: 22%; bottom: 21%; }
}
