/**
 * Stage 4 Algo — binary search conveyor + HUD
 */
:root {
  --s4-ink: #1a2b49;
  --s4-muted: #64748b;
  --s4-bg: #f3f6fb;
  --s4-coral: #ff6b4a;
  --s4-teal: #2bb3a3;
  --s4-amber: #f5a623;
  --s4-box: #e8eef8;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  font-family: "Pretendard", "Noto Sans KR", system-ui, sans-serif;
  color: var(--s4-ink);
  background:
    radial-gradient(circle at 10% 8%, rgba(43, 179, 163, 0.14), transparent 42%),
    radial-gradient(circle at 92% 18%, rgba(255, 107, 74, 0.1), transparent 40%),
    linear-gradient(180deg, #eef2f8 0%, var(--s4-bg) 55%);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.s4-wrap {
  max-width: 960px;
  margin: 0 auto;
  padding: 16px 16px 48px;
}

.s4-nav {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.s4-nav a {
  font-size: 13px;
  font-weight: 800;
  text-decoration: none;
  color: var(--s4-muted);
  padding: 8px 12px;
  border-radius: 999px;
  background: #fff;
  border: 1px solid rgba(26, 43, 73, 0.08);
}

.s4-hero {
  text-align: center;
  margin-bottom: 14px;
}

.s4-badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 900;
  padding: 6px 12px;
  border-radius: 999px;
  background: #fff;
  border: 1px solid rgba(26, 43, 73, 0.08);
  margin-bottom: 8px;
}

.s4-hero h1 {
  margin: 0 0 6px;
  font-size: clamp(1.25rem, 4vw, 1.7rem);
  font-weight: 900;
}

.s4-hero p {
  margin: 0;
  font-size: 14px;
  color: var(--s4-muted);
  line-height: 1.5;
}

.s4-card {
  background: #fff;
  border-radius: 24px;
  box-shadow: 0 14px 40px rgba(26, 43, 73, 0.08);
  padding: 18px 16px 22px;
  border: 1px solid rgba(26, 43, 73, 0.06);
  position: relative;
}

.s4-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  font-size: 13px;
  font-weight: 800;
  color: var(--s4-muted);
  margin-bottom: 10px;
}

.s4-target {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: 14px;
  background: linear-gradient(135deg, rgba(255, 107, 74, 0.12), rgba(245, 166, 35, 0.12));
  color: var(--s4-ink);
  font-size: 14px;
  font-weight: 800;
  margin: 0 0 12px;
}

.s4-target strong {
  color: var(--s4-coral);
  font-size: 1.15em;
}

.s4-progress {
  display: flex;
  justify-content: center;
  gap: 6px;
  margin-bottom: 12px;
}

.s4-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #d8dee9;
}

.s4-dot.on {
  background: var(--s4-coral);
  transform: scale(1.25);
}

.s4-dot.done {
  background: var(--s4-teal);
}

.s4-title {
  margin: 0 0 6px;
  font-size: 1.15rem;
  font-weight: 900;
}

.s4-prompt {
  margin: 0 0 14px;
  font-size: 14px;
  color: var(--s4-muted);
  line-height: 1.55;
}

/* Budget / time HUD */
.s4-hud {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}

.s4-hud-card {
  background: #f7f9fc;
  border: 1px solid rgba(26, 43, 73, 0.06);
  border-radius: 14px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.s4-hud-label {
  font-size: 11px;
  font-weight: 800;
  color: var(--s4-muted);
}

.s4-hud-val {
  font-size: 15px;
  font-weight: 900;
  color: var(--s4-ink);
}

.s4-time-bar {
  height: 6px;
  border-radius: 999px;
  background: #e2e8f0;
  overflow: hidden;
  margin-top: 4px;
}

.s4-time-bar i {
  display: block;
  height: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--s4-teal), var(--s4-amber));
  transition: width 0.35s ease;
}

/* Conveyor belt */
.s4-belt-wrap {
  position: relative;
  margin-bottom: 14px;
  padding: 14px 10px 18px;
  border-radius: 18px;
  background:
    repeating-linear-gradient(
      90deg,
      #2a3548 0,
      #2a3548 12px,
      #343f54 12px,
      #343f54 24px
    );
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.06);
}

.s4-belt-wrap::before,
.s4-belt-wrap::after {
  content: "";
  position: absolute;
  left: 8px;
  right: 8px;
  height: 4px;
  border-radius: 2px;
  background: rgba(255, 107, 74, 0.45);
}

.s4-belt-wrap::before { top: 6px; }
.s4-belt-wrap::after { bottom: 6px; }

.s4-belt {
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: 8px;
  position: relative;
  z-index: 1;
}

.s4-box {
  appearance: none;
  border: 2px solid transparent;
  border-radius: 12px;
  background: linear-gradient(160deg, #fff 0%, var(--s4-box) 100%);
  min-height: 72px;
  padding: 6px 4px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  transition:
    opacity 0.45s ease,
    transform 0.25s ease,
    filter 0.45s ease,
    border-color 0.2s ease,
    box-shadow 0.2s ease;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.12);
}

.s4-box:not(:disabled):hover {
  transform: translateY(-4px);
  border-color: var(--s4-teal);
}

.s4-box:not(:disabled):active {
  transform: translateY(-1px) scale(0.98);
}

.s4-box.is-midhint {
  border-color: var(--s4-amber);
  box-shadow: 0 0 0 3px rgba(245, 166, 35, 0.35);
  animation: s4-pulse 1.4s ease-in-out infinite;
}

.s4-box.is-open {
  background: #fff;
  border-color: var(--s4-teal);
}

.s4-box.is-found {
  border-color: var(--s4-coral);
  background: linear-gradient(160deg, #fff5f2, #ffe8e2);
  box-shadow: 0 0 0 3px rgba(255, 107, 74, 0.35);
  animation: s4-found 0.55s ease;
}

.s4-box.is-out {
  opacity: 0.28;
  filter: grayscale(0.85);
  transform: scale(0.92);
  pointer-events: none;
  box-shadow: none;
}

.s4-box-no {
  font-size: 10px;
  font-weight: 800;
  color: var(--s4-muted);
}

.s4-box-lid {
  font-size: 22px;
  line-height: 1;
}

.s4-box-kg {
  font-size: 15px;
  font-weight: 900;
  color: var(--s4-ink);
  line-height: 1.1;
}

.s4-box-kg small {
  font-size: 10px;
  font-weight: 800;
  color: var(--s4-muted);
  margin-left: 1px;
}

@keyframes s4-pulse {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-3px); }
}

@keyframes s4-found {
  0% { transform: scale(0.9); }
  60% { transform: scale(1.08); }
  100% { transform: scale(1); }
}

@media (prefers-reduced-motion: reduce) {
  .s4-box.is-midhint,
  .s4-box.is-found {
    animation: none;
  }
  .s4-box,
  .s4-time-bar i {
    transition: none;
  }
}

/* Event log */
.s4-log {
  max-height: 120px;
  overflow-y: auto;
  margin-bottom: 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.s4-log-row {
  font-size: 12px;
  font-weight: 700;
  color: var(--s4-muted);
  padding: 6px 10px;
  border-radius: 10px;
  background: #f7f9fc;
}

.s4-log-row.is-ok {
  color: #0f766e;
  background: rgba(43, 179, 163, 0.12);
}

.s4-log-row.is-cut {
  color: #9a3412;
  background: rgba(255, 107, 74, 0.1);
}

.s4-feedback {
  margin-bottom: 12px;
  padding: 12px 14px;
  border-radius: 14px;
  font-size: 13px;
  font-weight: 700;
  line-height: 1.5;
}

.s4-feedback.is-hint {
  background: #f1f5f9;
  color: var(--s4-muted);
}

.s4-feedback.is-ok {
  background: rgba(43, 179, 163, 0.14);
  color: #0f766e;
}

.s4-feedback.is-bad {
  background: rgba(255, 107, 74, 0.12);
  color: #c2410c;
}

.s4-takeaway {
  margin: 0 0 12px;
  padding: 12px 14px;
  border-radius: 14px;
  border-left: 4px solid var(--s4-coral);
  background: #fff8f6;
  font-size: 13px;
  font-weight: 700;
  line-height: 1.5;
  color: var(--s4-ink);
}

.s4-reflect {
  margin-bottom: 12px;
}

.s4-reflect-q {
  margin: 0 0 8px;
  font-size: 14px;
  font-weight: 900;
}

.s4-reflect-opts {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.s4-reflect-btn {
  appearance: none;
  text-align: left;
  border: 1px solid rgba(26, 43, 73, 0.1);
  background: #fff;
  border-radius: 12px;
  padding: 10px 12px;
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  color: var(--s4-ink);
}

.s4-reflect-btn:hover:not(:disabled) {
  border-color: var(--s4-teal);
}

.s4-reflect-btn.is-correct {
  border-color: var(--s4-teal);
  background: rgba(43, 179, 163, 0.12);
}

.s4-reflect-btn.is-wrong {
  border-color: var(--s4-coral);
  background: rgba(255, 107, 74, 0.1);
}

.s4-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}

.s4-btn {
  appearance: none;
  border: none;
  border-radius: 999px;
  padding: 10px 18px;
  font: inherit;
  font-size: 14px;
  font-weight: 900;
  cursor: pointer;
  background: var(--s4-coral);
  color: #fff;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
}

.s4-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.s4-btn-ghost {
  background: #fff;
  color: var(--s4-ink);
  border: 1px solid rgba(26, 43, 73, 0.12);
}

.s4-done {
  display: none;
  text-align: center;
  padding: 28px 12px;
}

.s4-card.is-done .s4-play {
  display: none;
}

.s4-card.is-done .s4-done {
  display: block;
}

.s4-done h2 {
  margin: 0 0 8px;
  font-size: 1.4rem;
  font-weight: 900;
}

.s4-done p {
  margin: 0 0 16px;
  color: var(--s4-muted);
  font-weight: 700;
}

.s4-note {
  margin-top: 16px;
  font-size: 12px;
  color: var(--s4-muted);
  line-height: 1.55;
  text-align: center;
}

.s4-note a {
  color: var(--s4-ink);
  font-weight: 800;
}

@media (max-width: 640px) {
  .s4-hud {
    grid-template-columns: 1fr;
  }

  .s4-belt {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .s4-box {
    min-height: 64px;
  }
}

/* —— Stage1-style visual boost —— */
.s4-decor{pointer-events:none;position:fixed;inset:0;z-index:0;overflow:hidden}
.s4-decor span{position:absolute;font-size:clamp(1.3rem,3.5vw,2rem);opacity:.48;animation:s4-drift 9s ease-in-out infinite;filter:drop-shadow(0 6px 10px rgba(26,43,73,.12))}
.s4-decor span:nth-child(1){left:5%;top:12%}
.s4-decor span:nth-child(2){left:88%;top:18%;animation-delay:-2s}
.s4-decor span:nth-child(3){left:8%;top:76%;animation-delay:-3.5s}
.s4-decor span:nth-child(4){left:82%;top:70%;animation-delay:-1s}
.s4-wrap{position:relative;z-index:1}
.s4-card{border:3px solid #fff!important;box-shadow:0 18px 48px rgba(26,43,73,.12),0 0 0 6px rgba(43,179,163,.16)!important}
.s4-prompt{font-size:clamp(1.05rem,3.2vw,1.25rem)!important;font-weight:900!important;padding:12px 16px;border-radius:18px;background:linear-gradient(135deg,#f0fdfa,#ccfbf1);border:2px dashed #2bb3a3;color:#115e59}
.s4-target{font-size:1.05rem!important;padding:10px 16px!important;border:2px solid rgba(255,107,74,.35);animation:s4-pop .4s ease}
.s4-box{animation:s4-item-pop .4s cubic-bezier(.34,1.56,.64,1) both}
.s4-box:nth-child(odd){animation-delay:.03s}.s4-box:nth-child(even){animation-delay:.08s}
.s4-box.is-midhint{transform:translateY(-4px)!important;border-width:3px}
.s4-feedback.is-ok{border:2px solid #4ade80;background:linear-gradient(135deg,#dcfce7,#bbf7d0)!important}
.s4-feedback.is-bad{border:2px solid #fb7185;background:linear-gradient(135deg,#ffe4e6,#ffedd5)!important}
.s4-btn{box-shadow:0 10px 24px rgba(255,107,74,.3)!important}
.s4-hero h1{font-size:clamp(1.5rem,5vw,2rem)!important}
.s4-badge{background:linear-gradient(135deg,#fff,#ccfbf1)!important;border:2px solid #5eead4!important;color:#0f766e}
@keyframes s4-drift{0%,100%{transform:translateY(0) rotate(-5deg)}50%{transform:translateY(-14px) rotate(7deg)}}
@keyframes s4-item-pop{from{opacity:0;transform:scale(.5) translateY(8px)}to{opacity:1;transform:scale(1) translateY(0)}}
@keyframes s4-pop{from{opacity:0;transform:scale(.92)}to{opacity:1;transform:scale(1)}}
@media (prefers-reduced-motion:reduce){.s4-decor span,.s4-box{animation:none!important}}
