/* ===========================================================
   challenge.css  —— 挑戰區共用樣式
   涵蓋：頁面外殼、頂端任務橫幅、成功卡、擬真麵包板、可拖曳零件、
         LED 發光/導通、hub 關卡卡片與鎖頭
   =========================================================== */

:root {
  --candy-pink: #FF6B9D;
  --candy-orange: #FF9F43;
  --candy-yellow: #FECA57;
  --candy-green: #5CD85A;
  --candy-cyan: #48DBFB;
  --candy-blue: #54A0FF;
  --candy-purple: #A55EEA;
  --ink: #2D3436;
  --muted: #636E72;
  --ok: #2f9e44;
  --ok-dark: #1b6f2a;
  --line: #e4e9f0;
  --shadow-card: 0 8px 30px rgba(0, 0, 0, 0.08);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: 'Nunito', 'Microsoft JhengHei', 'PingFang TC', sans-serif;
  background:
    radial-gradient(circle at 10% 6%, rgba(165, 94, 234, 0.12), transparent 26rem),
    radial-gradient(circle at 92% 0%, rgba(255, 107, 157, 0.12), transparent 28rem),
    #FFF9F0;
  color: var(--ink);
  font-size: 18px;
  line-height: 1.6;
}

/* ===== Header / Nav ===== */
.ch-header { background: linear-gradient(135deg, var(--candy-purple), var(--candy-pink)); color: #fff; box-shadow: var(--shadow-card); }
.ch-nav { max-width: 1100px; margin: 0 auto; padding: 0.85rem 1.2rem; display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
.ch-nav h1 { margin: 0; font-size: clamp(1.1rem, 2.6vw, 1.5rem); font-weight: 900; }
.ch-nav .ch-nav-links { display: flex; gap: .5rem; }
.ch-nav a { color: #fff; text-decoration: none; padding: .45rem .9rem; border-radius: 999px; font-weight: 800; white-space: nowrap; min-height: 44px; display: inline-flex; align-items: center; transition: background .2s; }
.ch-nav a:hover, .ch-nav a:focus-visible { background: rgba(255,255,255,.2); outline: none; }

.ch-container { width: min(1100px, calc(100% - 2rem)); margin: 0 auto; padding: 1.3rem 0 3rem; }

/* ===== 任務橫幅（固定頂端） ===== */
.challenge-banner { position: fixed; top: 0; left: 0; right: 0; z-index: 900; background: #fff; border-bottom: 4px solid var(--candy-purple); box-shadow: 0 4px 20px rgba(0,0,0,.18); transform: translateY(-110%); transition: transform .4s cubic-bezier(.4,0,.2,1); }
.challenge-banner.active { transform: translateY(0); }
.challenge-banner.success-state { border-bottom-color: var(--ok); }
.challenge-banner.short-flash { border-bottom-color: #e03131; animation: shortFlash .6s ease; }
@keyframes shortFlash { 0%,100% { background:#fff; } 50% { background:#ffe3e3; } }
.cb-inner { max-width: 1000px; margin: 0 auto; padding: .8rem 1.4rem; display: flex; align-items: center; gap: 14px; }
.cb-badge { background: linear-gradient(135deg, var(--candy-purple), var(--candy-pink)); color: #fff; padding: 5px 14px; border-radius: 999px; font-weight: 900; font-size: .85rem; white-space: nowrap; flex-shrink: 0; }
.cb-badge.winning { background: linear-gradient(135deg, var(--ok), var(--ok-dark)); }
.cb-content { flex: 1; min-width: 0; }
.cb-task { font-size: .95rem; line-height: 1.5; font-weight: 700; }
.cb-hint { font-size: .85rem; color: var(--muted); margin-top: 3px; }
.cb-win { display: none; font-size: .95rem; font-weight: 900; color: var(--ok-dark); }
.cb-close { background: none; border: 2px solid var(--line); border-radius: 50%; width: 34px; height: 34px; cursor: pointer; font-size: .85rem; color: var(--muted); flex-shrink: 0; font-family: inherit; line-height: 1; }
.cb-close:hover { background: var(--line); }

/* ===== 關卡資訊列 ===== */
.ch-stage-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: .6rem; }
.ch-stage-head h2 { margin: 0; font-size: clamp(1.3rem, 3.4vw, 1.7rem); font-weight: 900; }
.ch-know { color: var(--muted); font-weight: 800; font-size: .98rem; margin: .1rem 0 .8rem; }
.ch-dots { display: flex; gap: 8px; }
.ch-dot { width: 30px; height: 30px; border-radius: 50%; background: #eef0f4; color: var(--muted); display: inline-flex; align-items: center; justify-content: center; font-weight: 900; font-size: .9rem; border: 2px solid #e0e4ea; }
.ch-dot.current { border-color: var(--candy-purple); color: var(--candy-purple); }
.ch-dot.done { background: var(--candy-green); color: #fff; border-color: var(--candy-green); }

/* ===== 任務說明卡 ===== */
.ch-taskcard { background: linear-gradient(135deg, #f6f0ff, #ffeef6); border: 2px solid #ecd9ff; border-radius: 16px; padding: 1rem 1.2rem; margin-bottom: 1rem; font-weight: 700; }

/* ===== 擬真麵包板 ===== */
.ch-board { position: relative; width: 100%; max-width: 600px; height: clamp(360px, 64vw, 440px); margin: 0 auto; background: linear-gradient(180deg, #fbfdff, #fff7ec); border: 2px solid #dbe3ea; border-radius: 18px; box-shadow: inset 0 2px 10px rgba(0,0,0,.05); touch-action: none; }

.ch-rail { position: absolute; left: 30%; right: 6%; height: 18px; transform: translateY(-50%); border-radius: 9px; z-index: 1; }
.ch-rail.pos { background: rgba(239,68,68,.12); border: 2px solid rgba(239,68,68,.4); }
.ch-rail.neg { background: rgba(59,130,246,.12); border: 2px solid rgba(59,130,246,.4); }
.ch-rail-label { position: absolute; left: 2%; transform: translateY(-50%); font-size: .72rem; font-weight: 900; z-index: 3; white-space: nowrap; }
.ch-rail-label.pos { color: #d6336c; }
.ch-rail-label.neg { color: #1d4ed8; }

.ch-hole { position: absolute; width: 18px; height: 18px; border-radius: 50%; transform: translate(-50%, -50%); background: #aab4bf; border: 2px solid #7c8893; z-index: 2; }
.ch-hole[data-node="RAILP"] { background: #ffd9d9; border-color: #ef6b6b; }
.ch-hole[data-node="RAILN"] { background: #d9e6ff; border-color: #5b8def; }

/* 電池（裝飾，標 +/-，並用 ::before/::after 假裝接到上下電源軌） */
.ch-battery { position: absolute; left: 3%; top: 50%; transform: translateY(-50%); width: 58px; height: 84px; border-radius: 12px; background: linear-gradient(160deg, #ffe9a8, #ffd45e); border: 2px solid #e0a800; display: flex; align-items: center; justify-content: center; z-index: 3; box-shadow: 0 4px 12px rgba(224,168,0,.25); }
.ch-batt-emoji { font-size: 1.7rem; }
.ch-batt-pos { position: absolute; top: 4px; right: 6px; color: #d6336c; font-weight: 900; font-size: .8rem; }
.ch-batt-neg { position: absolute; bottom: 4px; right: 6px; color: #1d4ed8; font-weight: 900; font-size: .8rem; }
.ch-battery::before { content: ''; position: absolute; left: 50%; bottom: 100%; width: 5px; height: 46px; background: #ef6b6b; border-radius: 3px; transform: translateX(-50%); }
.ch-battery::after { content: ''; position: absolute; left: 50%; top: 100%; width: 5px; height: 46px; background: #5b8def; border-radius: 3px; transform: translateX(-50%); }

/* anchor（橋接點）：平常半透明虛線，命中時發亮 */
.ch-anchor { position: absolute; transform: translate(-50%, -50%); border: 2.5px dashed rgba(124,92,255,.3); border-radius: 12px; background: rgba(124,92,255,.04); z-index: 4; }
.ch-anchor.v { width: 52px; height: 23%; }
.ch-anchor.h { width: 30%; height: 50px; }
.ch-anchor.ch-hot { border-style: solid; border-color: var(--candy-green); background: rgba(92,216,90,.18); box-shadow: 0 0 14px rgba(92,216,90,.5); }
.ch-anchor.ch-filled { border-color: transparent; background: transparent; }

/* ===== 零件盤 ===== */
.ch-tray-wrap { margin-top: 1rem; }
.ch-tray-wrap h3 { margin: 0 0 .5rem; font-size: 1.05rem; font-weight: 900; color: var(--candy-purple); }
.ch-tray { display: flex; flex-wrap: wrap; gap: 14px; padding: 14px; background: #fff; border: 2px dashed #e0d4f5; border-radius: 16px; min-height: 90px; align-items: center; }

/* ===== 零件 tile ===== */
.ch-tile { --part-color: #888; position: relative; touch-action: none; user-select: none; }
.ch-tile.ch-in-tray { display: inline-flex; flex-direction: column; align-items: center; justify-content: center; width: 86px; padding: 8px 6px; background: #fff; border: 2px solid #e4e9f0; border-radius: 14px; cursor: grab; box-shadow: 0 3px 10px rgba(0,0,0,.06); transition: transform .15s; }
.ch-tile.ch-in-tray:hover { transform: translateY(-3px); border-color: var(--candy-purple); }
.ch-tile.ch-in-tray .ch-leg, .ch-tile.ch-in-tray .ch-pol { display: none; }
.ch-tile .ch-tile-body { display: inline-flex; align-items: center; justify-content: center; width: 46px; height: 46px; border-radius: 12px; background: #f4f6fa; border: 2px solid #e1e6ee; }
.ch-tile .ch-emoji { font-size: 1.6rem; line-height: 1; }
.ch-tile .ch-label { font-size: .8rem; font-weight: 800; color: var(--muted); margin-top: 4px; white-space: nowrap; }
.ch-ghost { opacity: .9; transform-origin: center; }
.ch-tile.ch-dragging { opacity: .35; }

/* 放置後：填滿 anchor、畫出兩隻腳 */
.ch-tile.ch-placed { position: absolute; inset: 0; width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; cursor: grab; }
.ch-tile.ch-placed .ch-label { display: none; }
.ch-tile.ch-placed .ch-tile-body { width: 40px; height: 40px; background: #fff; box-shadow: 0 2px 8px rgba(0,0,0,.12); }
.ch-tile.ch-placed .ch-leg { display: block; position: absolute; background: linear-gradient(#cdd5dd, #aeb8c2); border-radius: 3px; z-index: -1; }
/* 直立 */
.ch-tile.ch-placed.v .ch-leg-a { width: 5px; height: 60%; left: 50%; bottom: 100%; transform: translate(-50%, 40%); }
.ch-tile.ch-placed.v .ch-leg-b { width: 5px; height: 60%; left: 50%; top: 100%; transform: translate(-50%, -40%); }
/* 橫向 */
.ch-tile.ch-placed.h .ch-leg-a { height: 5px; width: 55%; top: 50%; right: 100%; transform: translate(40%, -50%); }
.ch-tile.ch-placed.h .ch-leg-b { height: 5px; width: 55%; top: 50%; left: 100%; transform: translate(-40%, -50%); }

/* ＋ 極性徽章（LED 才顯示，標在 anode 那端） */
.ch-tile .ch-pol { display: none; }
.ch-tile.ch-placed[data-type="led"] .ch-pol { display: inline-flex; align-items: center; justify-content: center; position: absolute; width: 18px; height: 18px; border-radius: 50%; background: #ffe3e3; color: #d6336c; font-weight: 900; font-size: .7rem; border: 1px solid #ef6b6b; z-index: 2; }
.ch-tile.ch-placed.v[data-type="led"] .ch-pol { top: -6px; left: 50%; transform: translateX(-50%); }
.ch-tile.ch-placed.v.flip[data-type="led"] .ch-pol { top: auto; bottom: -6px; }
.ch-tile.ch-placed.h[data-type="led"] .ch-pol { left: -6px; top: 50%; transform: translateY(-50%); }
.ch-tile.ch-placed.h.flip[data-type="led"] .ch-pol { left: auto; right: -6px; transform: translateY(-50%); }

/* LED 點亮發光（重用 led.html 的 radial + glow 概念） */
.ch-tile.lit .ch-tile-body { background: radial-gradient(circle at 35% 30%, #fff, var(--part-color) 70%); border-color: var(--part-color); box-shadow: 0 0 16px 5px var(--part-color), 0 0 30px 10px var(--part-color); }
.ch-tile.lit .ch-emoji { filter: brightness(1.25) saturate(1.3); }
/* 導通中：腳線發光脈動 */
.ch-tile.conducting .ch-leg { background: linear-gradient(#9be7c4, #3ddc6d); animation: flowPulse 1s ease-in-out infinite; }
@keyframes flowPulse { 0%,100% { opacity: .6; } 50% { opacity: 1; } }

/* 控制按鈕 */
.ch-ctl { position: absolute; font-family: inherit; cursor: pointer; z-index: 5; border: none; }
.ch-flip { top: -14px; right: -14px; width: 26px; height: 26px; border-radius: 50%; background: #fff; border: 2px solid var(--candy-purple); font-size: .8rem; line-height: 1; box-shadow: 0 2px 6px rgba(0,0,0,.18); }
.ch-press { bottom: -18px; left: 50%; transform: translateX(-50%); padding: 3px 12px; border-radius: 999px; background: var(--candy-purple); color: #fff; font-weight: 900; font-size: .78rem; box-shadow: 0 3px 8px rgba(124,92,255,.35); white-space: nowrap; }
.ch-tile.pressed .ch-press { background: var(--ok); transform: translateX(-50%) translateY(2px); }
.ch-tile.pressed .ch-tile-body { box-shadow: 0 0 0 3px rgba(47,158,68,.4); }

/* ===== 成功卡 ===== */
.c-success { background: linear-gradient(135deg, #e8f5e9, #f1f8e9); border: 3px solid var(--ok); border-radius: 16px; padding: 1.5rem; text-align: center; margin-top: 1.4rem; display: none; }
.c-success.show { display: block; animation: cPop .5s ease-out; }
@keyframes cPop { 0% { transform: scale(.8); opacity: 0; } 60% { transform: scale(1.05); } 100% { transform: scale(1); opacity: 1; } }
.c-success h4 { font-size: 1.4rem; font-weight: 900; color: var(--ok-dark); margin: .3rem 0; }
.c-success p { font-size: 1rem; color: #388e3c; font-weight: 700; }

/* ===== 底部按鈕列 ===== */
.ch-actions { display: flex; gap: 1rem; justify-content: center; margin-top: 1.6rem; flex-wrap: wrap; }
.ch-btn { background: linear-gradient(135deg, var(--candy-purple), var(--candy-pink)); color: #fff; border: none; border-radius: 999px; padding: .8rem 1.8rem; font-weight: 900; font-size: 1rem; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; gap: 6px; min-height: 44px; box-shadow: 0 6px 16px rgba(165,94,234,.28); font-family: inherit; }
.ch-btn.ghost { background: #fff; color: var(--candy-purple); border: 2px solid #e0d4f5; box-shadow: none; }
.ch-btn:hover { transform: translateY(-2px); }
.ch-btn:disabled { opacity: .5; cursor: not-allowed; transform: none; }

.ch-zone-finish { display: none; margin-top: 1.4rem; text-align: center; background: linear-gradient(135deg, #fff4e6, #ffeef6); border: 3px solid var(--candy-yellow); border-radius: 18px; padding: 1.4rem; }
.ch-zone-finish h3 { margin: .2rem 0 .4rem; font-size: 1.4rem; font-weight: 900; color: var(--candy-orange); }

/* ===== Hub：關卡卡片 + 鎖頭 ===== */
.hub-hero { text-align: center; background: linear-gradient(135deg, #f6f0ff 0%, #ffeef6 100%); border: 2px solid #ecd9ff; border-radius: 24px; padding: clamp(1.4rem, 4vw, 2.4rem); margin-bottom: 1.6rem; }
.hub-hero .hub-emoji { font-size: clamp(2.6rem, 7vw, 3.6rem); }
.hub-hero h2 { margin: .3rem 0 .4rem; font-size: clamp(1.6rem, 4.4vw, 2.4rem); font-weight: 900; }
.hub-hero p { margin: 0 auto; max-width: 640px; color: var(--muted); font-weight: 800; }
.hub-progress { margin: 1rem auto 0; max-width: 420px; }
.hub-progress .bar { height: 14px; border-radius: 999px; background: #eee; overflow: hidden; }
.hub-progress .bar > span { display: block; height: 100%; background: linear-gradient(90deg, var(--candy-green), var(--candy-cyan)); width: 0; transition: width .5s; }
.hub-progress .label { font-size: .85rem; font-weight: 800; color: var(--muted); margin-top: 6px; }

.zone-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 22px; }
.zone-card { position: relative; background: #fff; border: 3px solid var(--line); border-radius: 22px; padding: 24px; box-shadow: var(--shadow-card); transition: transform .3s, box-shadow .3s; overflow: hidden; }
.zone-card .zone-top { height: 6px; position: absolute; top: 0; left: 0; right: 0; }
.zone-card.easy .zone-top { background: linear-gradient(90deg, var(--candy-green), var(--candy-cyan)); }
.zone-card.medium .zone-top { background: linear-gradient(90deg, var(--candy-yellow), var(--candy-orange)); }
.zone-card.hard .zone-top { background: linear-gradient(90deg, var(--candy-pink), var(--candy-purple)); }
.zone-card .zone-icon { font-size: 2.6rem; }
.zone-card h3 { margin: .3rem 0 .2rem; font-size: 1.4rem; font-weight: 900; }
.zone-card p { color: var(--muted); font-weight: 700; font-size: .96rem; margin: 0 0 1rem; }
.zone-meta { font-size: .85rem; font-weight: 800; color: var(--muted); margin-bottom: 1rem; }
.zone-card .zone-go { background: linear-gradient(135deg, var(--candy-purple), var(--candy-pink)); color: #fff; border: none; border-radius: 999px; padding: .7rem 1.6rem; font-weight: 900; font-size: 1rem; text-decoration: none; display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.zone-card:not(.zone-locked):hover { transform: translateY(-8px); box-shadow: 0 20px 40px rgba(165,94,234,.18); }
.zone-badge { position: absolute; top: 16px; right: 16px; font-size: 1.6rem; }
.zone-card.zone-locked { filter: grayscale(.7); opacity: .75; }
.zone-card.zone-locked::after { content: ''; position: absolute; inset: 0; background: rgba(120,120,140,.08); }
.zone-card.zone-locked .zone-go { background: #c3c7cf; pointer-events: none; }

/* ===== Hub 禮物解鎖（三區全破才顯示；本切片不會觸發） ===== */
.gift-unlock { display: none; text-align: center; margin-top: 1.8rem; background: linear-gradient(135deg, #fff0f6, #f3e8ff); border: 3px solid var(--candy-pink); border-radius: 24px; padding: 2rem; animation: cPop .6s ease-out; }
.gift-unlock.show { display: block; }
.gift-unlock .g-emoji { font-size: 3.4rem; }
.gift-unlock h2 { margin: .4rem 0; font-size: 1.8rem; font-weight: 900; color: var(--candy-pink); }
.gift-unlock p { font-weight: 800; color: var(--ink); }

@media (max-width: 640px) {
  .ch-tile.ch-in-tray { width: 76px; }
}
