/* Code Quest Kids — playful, high-contrast, big tap targets. */
:root {
  --bg: #fff7ed;
  --panel: #ffffff;
  --ink: #2b2d42;
  --primary: #ff7043;
  --primary-d: #f4511e;
  --accent: #42a5f5;
  --green: #43c463;
  --green-d: #2fa850;
  --purple: #9575cd;
  --shadow: 0 4px 0 rgba(0,0,0,.12), 0 8px 20px rgba(0,0,0,.10);
  --radius: 18px;
  font-size: 17px;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: "Comic Sans MS", "Baloo 2", "Nunito", "PingFang TC",
    "Microsoft JhengHei", system-ui, sans-serif;
  background:
    radial-gradient(circle at 15% 10%, #ffe0b2 0, transparent 40%),
    radial-gradient(circle at 85% 15%, #b3e5fc 0, transparent 40%),
    var(--bg);
  color: var(--ink);
  min-height: 100vh;
}

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 18px; flex-wrap: wrap; gap: 10px;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-logo { font-size: 34px; }
.brand h1 { font-size: 1.5rem; margin: 0; color: var(--primary-d); }
.topbar-controls { display: flex; align-items: center; gap: 10px; }

.lang-switch { display: flex; background: #fff; border-radius: 999px; padding: 3px; box-shadow: var(--shadow); }
.lang-btn {
  border: none; background: transparent; padding: 7px 14px; border-radius: 999px;
  font-weight: 700; cursor: pointer; color: var(--ink); font-size: .95rem;
}
.lang-btn.active { background: var(--accent); color: #fff; }

.pill {
  border: none; background: #fff; border-radius: 999px; padding: 9px 16px;
  font-weight: 700; cursor: pointer; box-shadow: var(--shadow); color: var(--ink);
}
.pill:active { transform: translateY(2px); }
.player-pill {
  background: linear-gradient(135deg, #e3f2fd, #fff);
  border: 2px solid #90caf9; color: #1565c0; max-width: 160px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.player-pill:hover { background: #bbdefb; }

/* play timer pill */
.timer-pill {
  background: linear-gradient(135deg, #e8f5e9, #fff);
  border: 2px solid #a5d6a7; color: #2e7d32;
  font-variant-numeric: tabular-nums; min-width: 78px; text-align: center;
}
.timer-pill #timerDisplay { letter-spacing: .5px; }
.timer-pill.warn {
  background: linear-gradient(135deg, #fff3e0, #fff);
  border-color: #ffb74d; color: #e65100; animation: timerPulse 1s ease-in-out infinite;
}
.timer-pill.cooldown {
  background: linear-gradient(135deg, #ffebee, #fff);
  border-color: #ef9a9a; color: #c62828;
}
.timer-pill.off { background: #f5f5f5; border-color: #e0e0e0; color: #9e9e9e; }
@keyframes timerPulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.06); } }

/* ---------- screens ---------- */
.screen { display: none; max-width: 1050px; margin: 0 auto; padding: 8px 18px 40px; }
.screen.active { display: block; }

.hero { text-align: center; margin: 6px 0 18px; }
.hero h2 { font-size: 2rem; color: var(--primary-d); margin: 6px 0; }
.player-line { font-size: 1.05rem; }
.player-line strong { color: var(--accent); }
.link-btn {
  border: none; background: none; color: var(--accent); text-decoration: underline;
  cursor: pointer; font: inherit; font-size: .95rem;
}
.muted { color: #888; }

/* ---------- level grid ---------- */
.level-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 16px; margin-bottom: 30px;
}
.level-card {
  border: none; background: var(--panel); border-radius: var(--radius);
  padding: 18px 10px; text-align: center; cursor: pointer; box-shadow: var(--shadow);
  transition: transform .08s; font: inherit;
}
.level-card:hover { transform: translateY(-3px); }
.level-card:active { transform: translateY(1px); }
.level-card.locked { opacity: .55; cursor: not-allowed; filter: grayscale(.4); }
.level-num {
  width: 44px; height: 44px; line-height: 44px; margin: 0 auto 8px;
  background: var(--accent); color: #fff; border-radius: 50%; font-size: 1.3rem; font-weight: 800;
}
.level-name { font-weight: 700; min-height: 2.4em; display: flex; align-items: center; justify-content: center; }
.level-stars { font-size: 1.25rem; margin-top: 4px; letter-spacing: 2px; }

/* ---------- leaderboard ---------- */
.leaderboard-card { background: var(--panel); border-radius: var(--radius); padding: 18px 20px; box-shadow: var(--shadow); }
.leaderboard-card h3 { margin: 0 0 12px; font-size: 1.3rem; }
.leaderboard-list { list-style: none; margin: 0; padding: 0; }
.lb-row {
  display: grid; grid-template-columns: 44px 1fr auto auto; gap: 10px; align-items: center;
  padding: 9px 12px; border-radius: 12px;
}
.lb-row:nth-child(odd) { background: #fafafa; }
.lb-row.me { background: #fff3e0; outline: 2px solid var(--primary); }
.lb-rank { font-size: 1.2rem; font-weight: 800; text-align: center; }
.lb-name { font-weight: 700; }
.lb-stars { color: #fbc02d; font-weight: 700; }
.lb-score { font-weight: 800; color: var(--primary-d); min-width: 52px; text-align: right; }

/* ---------- play screen ---------- */
.play-topline { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-bottom: 12px; }
.play-topline h2 { margin: 0; font-size: 1.4rem; color: var(--primary-d); }

.objective-bar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 18px;
  background: #fff; border-radius: 14px; padding: 12px 18px; margin-bottom: 14px;
  box-shadow: var(--shadow); border-left: 6px solid var(--primary);
}
.objective-bar .obj-goal { font-weight: 800; font-size: 1.05rem; }
.objective-bar .obj-limit { font-weight: 700; background: #fff3e0; color: var(--primary-d); padding: 4px 12px; border-radius: 999px; }
.objective-bar .obj-stars { font-weight: 700; color: #777; margin-left: auto; }

.play-area {
  display: grid; grid-template-columns: minmax(280px, 1.2fr) 200px 1fr; gap: 18px; align-items: start;
}
@media (max-width: 860px) {
  .play-area { grid-template-columns: 1fr; }
}

.stage-wrap { background: var(--panel); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); }
#stage { width: 100%; height: auto; aspect-ratio: 1; border-radius: 12px; display: block; }
.run-controls { display: flex; align-items: center; gap: 10px; margin-top: 12px; flex-wrap: wrap; }
.block-counter { margin-left: auto; font-weight: 700; color: #777; }

.btn-run {
  border: none; background: var(--green); color: #fff; font-weight: 800; font-size: 1.1rem;
  padding: 12px 22px; border-radius: 14px; cursor: pointer; box-shadow: 0 4px 0 var(--green-d);
}
.btn-run:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--green-d); }
.btn-run:disabled { opacity: .5; cursor: wait; }
.btn-secondary {
  border: none; background: #eceff1; color: var(--ink); font-weight: 700; font-size: 1rem;
  padding: 12px 18px; border-radius: 14px; cursor: pointer; box-shadow: 0 4px 0 #cfd8dc;
}
.btn-secondary:active { transform: translateY(3px); box-shadow: 0 1px 0 #cfd8dc; }

/* ---------- palette & program ---------- */
.palette-wrap, .program-wrap { background: var(--panel); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); }
.palette-wrap h3, .program-wrap h3 { margin: 0 0 10px; font-size: 1.15rem; }
.palette { display: flex; flex-direction: column; gap: 10px; }
.hint { font-size: .8rem; color: #999; margin: 10px 0 0; }

.block, .chip {
  display: flex; align-items: center; gap: 8px; border: none; border-radius: 12px;
  padding: 12px 14px; font: inherit; font-weight: 700; cursor: pointer; color: #fff;
  width: 100%; text-align: left; box-shadow: 0 3px 0 rgba(0,0,0,.18);
}
.block:active { transform: translateY(2px); }
.blk-icon { font-size: 1.3rem; }
.blk-move { background: var(--accent); }
.blk-turn { background: var(--purple); }
.blk-repeat { background: #ff8f00; }
.blk-light { color: #2b2d42; }
.light-red { background: #ef9a9a; }
.light-green { background: #a5d6a7; }
.light-yellow { background: #fff59d; }
.blk-part { background: #26a69a; }

.program { min-height: 160px; display: flex; flex-direction: column; gap: 8px; }
.chip { position: relative; cursor: default; padding-right: 36px; }
.chip-x {
  position: absolute; right: 8px; top: 50%; transform: translateY(-50%);
  border: none; background: rgba(0,0,0,.18); color: #fff; width: 24px; height: 24px;
  border-radius: 50%; cursor: pointer; font-size: 1rem; line-height: 1;
}
.chip-x:hover { background: rgba(0,0,0,.35); }

.drop-ph { color: #bbb; font-style: italic; padding: 8px; text-align: center; }
.drop-ph.big { padding: 50px 8px; border: 3px dashed #e0e0e0; border-radius: 12px; }

/* repeat block */
.repeat-block { background: #fff3e0; border: 3px solid #ffb74d; border-radius: 14px; padding: 8px; }
.repeat-block.active { border-color: var(--primary); box-shadow: 0 0 0 4px rgba(255,112,67,.25); }
.repeat-head { display: flex; align-items: center; gap: 8px; font-weight: 800; color: #e65100; cursor: pointer; padding: 4px; position: relative; padding-right: 36px; }
.repeat-head .chip-x { background: rgba(230,81,0,.25); }
.stepper { display: flex; align-items: center; gap: 6px; }
.step-btn {
  border: none; background: #ff8f00; color: #fff; width: 28px; height: 28px; border-radius: 8px;
  font-size: 1.2rem; font-weight: 800; cursor: pointer; line-height: 1;
}
.step-btn:active { transform: translateY(2px); }
.step-num { min-width: 22px; text-align: center; font-size: 1.1rem; }
.times-label { font-weight: 700; color: #e65100; }
.repeat-body { margin: 8px 0 2px; padding-left: 16px; border-left: 3px dashed #ffb74d; display: flex; flex-direction: column; gap: 8px; }

/* ---------- modals ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.45); display: none;
  align-items: center; justify-content: center; padding: 20px; z-index: 50;
}
.modal-backdrop.open { display: flex; }
.modal {
  background: #fff; border-radius: 22px; padding: 28px; max-width: 380px; width: 100%;
  text-align: center; box-shadow: var(--shadow); animation: pop .2s ease;
}
@keyframes pop { from { transform: scale(.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.modal h3 { margin: 0 0 16px; font-size: 1.4rem; color: var(--primary-d); }
.modal input {
  width: 100%; padding: 14px; font-size: 1.2rem; border: 3px solid #ffccbc; border-radius: 14px;
  text-align: center; margin-bottom: 16px; font-family: inherit;
}
.modal input:focus { outline: none; border-color: var(--primary); }
.modal-actions { display: flex; flex-direction: column; gap: 10px; align-items: stretch; }
.win-modal .stars { font-size: 3rem; margin-bottom: 6px; animation: pop .4s ease; }
.howto-list { text-align: left; line-height: 1.9; font-size: 1.05rem; padding-left: 6px; list-style: none; }

/* timer modals */
.time-emoji { font-size: 3.2rem; margin-bottom: 4px; animation: pop .3s ease; }
#timeUpMsg { font-size: 1.05rem; line-height: 1.6; margin: 0 0 18px; }
.timer-toggle {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  font-weight: 700; margin-bottom: 16px; cursor: pointer;
}
.timer-toggle input { width: 22px; height: 22px; margin: 0; accent-color: var(--green); }
.timer-fields { display: flex; gap: 14px; margin-bottom: 18px; }
.timer-fields .field { flex: 1; text-align: left; }
.timer-fields .field input {
  width: 100%; padding: 10px; font-size: 1.1rem; border: 2px solid #cfd8dc;
  border-radius: 10px; text-align: center; margin: 0; font-family: inherit;
}

/* ---------- toast ---------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: #2b2d42; color: #fff; padding: 14px 22px; border-radius: 14px; font-weight: 700;
  box-shadow: var(--shadow); opacity: 0; transition: all .25s; z-index: 60; pointer-events: none;
  max-width: 90vw; text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- custom quests section ---------- */
.custom-section { margin-bottom: 30px; }
.custom-section h3 { font-size: 1.3rem; margin: 0 0 12px; }
.level-card.custom { cursor: default; }
.level-card.custom .level-name { cursor: pointer; }
.level-card.custom .level-stars { cursor: pointer; }
.card-tools { display: flex; justify-content: center; gap: 12px; margin-top: 8px; }
.link-btn.danger { color: #e53935; }

/* ---------- editor ---------- */
.editor-area { display: grid; grid-template-columns: minmax(280px, 1fr) 320px; gap: 18px; align-items: start; }
@media (max-width: 820px) { .editor-area { grid-template-columns: 1fr; } }
#editorStage { width: 100%; height: auto; aspect-ratio: 1; border-radius: 12px; display: block; cursor: pointer; }
.editor-panel { background: var(--panel); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 14px; }
.tool-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.tool-btn {
  border: 3px solid transparent; background: #eceff1; border-radius: 12px; padding: 12px;
  font: inherit; font-weight: 700; cursor: pointer; color: var(--ink);
}
.tool-btn.active { border-color: var(--primary); background: #fff3e0; }
.field { display: flex; flex-direction: column; gap: 6px; font-weight: 700; font-size: .92rem; }
.field input[type="text"], .field input[type="number"], .field select {
  padding: 10px; border: 2px solid #cfd8dc; border-radius: 10px; font: inherit; font-weight: 600;
}
.field input:focus, .field select:focus { outline: none; border-color: var(--primary); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.inline-row { display: flex; gap: 8px; align-items: center; }
.inline-row input { flex: 1; }
.btn-secondary.small { padding: 8px 14px; font-size: .9rem; box-shadow: 0 3px 0 #cfd8dc; }
.blocks-field { border: 2px solid #e0e0e0; border-radius: 12px; padding: 10px 14px; }
.blocks-field legend { padding: 0 6px; font-weight: 700; }
.blocks-field label { display: inline-flex; align-items: center; gap: 4px; margin-right: 14px; font-size: 1.2rem; cursor: pointer; }
.editor-panel .run-controls { display: none; }
.editor-area .stage-wrap .run-controls { display: flex; }

/* ---------- players modal ---------- */
.players-list { list-style: none; margin: 0 0 16px; padding: 0; display: flex; flex-direction: column; gap: 8px; max-height: 260px; overflow-y: auto; }
.player-row { display: flex; align-items: center; gap: 8px; }
.player-pick {
  flex: 1; display: flex; align-items: center; gap: 10px; border: 2px solid #eceff1;
  background: #fafafa; border-radius: 12px; padding: 10px 14px; cursor: pointer; font: inherit;
  font-weight: 700; text-align: left;
}
.player-row.current .player-pick { border-color: var(--primary); background: #fff3e0; }
.player-pick .p-name { flex: 1; }
.player-pick .p-stars { color: #fbc02d; }
.now-tag { color: var(--primary-d); font-size: .85rem; }
.new-player-row { display: flex; gap: 8px; margin-bottom: 12px; }
.new-player-row input { flex: 1; margin-bottom: 0; }
.new-player-row .btn-run { white-space: nowrap; padding: 12px 16px; font-size: 1rem; }

/* ---------- zhuyin (注音): vertical column right of the char; tone mark beside ---------- */
.zy { display: inline-flex; align-items: center; line-height: 1; vertical-align: middle; }
.zy-c { display: inline-block; }
.zy-z { display: inline-flex; align-items: center; margin-left: 1px; }
.zy-z.zy-neu { flex-direction: column; align-items: center; }
.zy-b {
  writing-mode: vertical-lr; text-orientation: upright;
  font-size: 0.4em; font-weight: 700; color: #d84315;
  line-height: 0.92; letter-spacing: -0.5px; user-select: none;
}
.zy-t {
  font-size: 0.34em; font-weight: 700; color: #d84315;
  line-height: 1; user-select: none;
}
.zy-neu .zy-t { margin-bottom: -1px; }
body.zh { line-height: 1.4; }

/* ---------- math expression pad (repeat block) ---------- */
.expr-toggle {
  background: #ede7f6; color: #4a148c; border-radius: 8px;
  font-size: 0.72rem; font-weight: 800; padding: 4px 8px; line-height: 1;
}
.expr-toggle.active { background: #7c4dff; color: #fff; }
.expr-display {
  min-width: 56px; height: 30px; background: #fff;
  border: 2px solid #7c4dff; border-radius: 8px;
  display: inline-flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 0.88rem; color: #4a148c; padding: 0 8px;
  letter-spacing: 1px;
}
.expr-btns {
  display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px;
  padding: 6px 8px 4px; background: #f3e5f5; border-radius: 0 0 10px 10px;
}
.expr-btn {
  padding: 6px 2px; font-size: 0.82rem; font-weight: 700;
  border-radius: 7px; background: #ede7f6; color: #4a148c;
  min-width: 0;
}
.expr-btn:active { background: #7c4dff; color: #fff; }

/* ---------- view toggle (grid / map) ---------- */
.view-toggle-group {
  display: inline-flex; gap: 4px; justify-content: center;
  margin: 6px 0 10px; background: #fff; border-radius: 12px;
  padding: 4px; box-shadow: var(--shadow);
}
.view-btn {
  border: none; background: transparent; border-radius: 9px;
  padding: 7px 18px; font: inherit; font-size: .9rem; font-weight: 800;
  cursor: pointer; color: var(--ink); transition: all .12s;
}
.view-btn.active { background: var(--accent); color: #fff; }
.view-btn:hover:not(.active) { background: #e3f2fd; }

/* ---------- quest map ---------- */
.quest-map-wrap { margin-bottom: 30px; overflow-x: auto; }
.quest-map { display: flex; flex-direction: column; min-width: 240px; }

.map-row { display: grid; align-items: start; }
.map-slot { display: flex; flex-direction: column; align-items: center; gap: 3px; }

.map-node {
  width: 72px; height: 72px; border-radius: 50%; border: none;
  font-size: 1.35rem; font-weight: 900; cursor: pointer; font-family: inherit;
  display: flex; align-items: center; justify-content: center;
  box-shadow: var(--shadow); transition: transform .1s; flex-shrink: 0;
}
.map-node:hover:not([disabled]) { transform: scale(1.1); }
.map-node:active:not([disabled]) { transform: scale(0.95); }
.map-node.done    { background: var(--green); color: #fff; }
.map-node.current {
  background: var(--primary); color: #fff;
  animation: pulse-node 1.6s ease-in-out infinite;
}
@keyframes pulse-node {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255,112,67,.45), var(--shadow); }
  60%       { box-shadow: 0 0 0 12px rgba(255,112,67,0), var(--shadow); }
}
.map-node.locked {
  background: #e0e0e0; color: #9e9e9e; cursor: not-allowed;
  box-shadow: inset 0 2px 0 rgba(0,0,0,.08);
}

.mn-name {
  font-size: 0.7rem; font-weight: 700; text-align: center;
  width: 72px; word-break: break-word; line-height: 1.3; min-height: 2.2em;
}
.mn-stars { font-size: 0.75rem; letter-spacing: 1px; }
.map-node.locked ~ .mn-name  { color: #bbb; }
.map-node.locked ~ .mn-stars { color: #bbb; }

/* Horizontal path connector between nodes in a row */
.map-hconn {
  height: 8px; background: #e0e0e0; border-radius: 4px;
  margin-top: 32px; /* (72px node − 8px line) / 2 = 32px → aligns with node centre */
  align-self: start;
}
.map-hconn.done { background: var(--green); }

/* Vertical connector between rows (straight line on right or left side) */
.map-turn { position: relative; height: 44px; width: 100%; }
.map-turn::after {
  content: ""; position: absolute; top: 0; bottom: 0; width: 8px;
  background: #e0e0e0; border-radius: 4px;
}
.map-turn.turn-right::after { right: 36px; } /* 72px / 2 = 36px */
.map-turn.turn-left::after  { left:  36px; }
.map-turn.done::after { background: var(--green); }

/* Short vertical connector from last row to trophy */
.map-vconn-final {
  width: 8px; height: 36px; background: #e0e0e0; border-radius: 4px;
}
.map-vconn-final.pos-left  { margin-left: 36px; }
.map-vconn-final.pos-right { margin-left: auto; margin-right: 36px; }
.map-vconn-final.done { background: var(--green); }

/* Trophy at end of pack */
.map-trophy {
  display: flex; flex-direction: column; align-items: center;
  padding: 4px 12px 12px; gap: 4px;
}
.map-trophy.pos-left  { align-items: flex-start; padding-left: 8px; }
.map-trophy.pos-right { align-items: flex-end;   padding-right: 8px; }
.trophy-icon { font-size: 2.2rem; filter: grayscale(.65); opacity: .65; }
.map-trophy.achieved .trophy-icon  { filter: none; opacity: 1; animation: pop .5s ease; }
.trophy-label { font-size: 0.8rem; font-weight: 800; color: #bbb; text-align: center; }
.map-trophy.achieved .trophy-label { color: #fbc02d; }

/* ---------- footer ---------- */
.footer { text-align: center; padding: 20px; color: #999; }
