@import url("./quick-add.css");
@import url("./trace.css");
@import url("./predict.css");
@import url("./share.css");

:root {
  --ink: #1d2939;
  --muted: #667085;
  --line: #dde3ec;
  --card: #fff;
  --page: #f3f6fb;
  --brand: #4f6ef7;
  --run: #12a150;
  --run-2: #0c8a43;
  --warm-bg: #fff3d6;
  --warm-ink: #8a5700;
  --fail-bg: #fff0f0;
  --fail-ink: #b42318;
  --ok-bg: #e9f9ef;
  --ok-ink: #067647;
  --floor: #fbf7ec;
  --floor-line: #eadfc4;
  --wall: #6c7a91;
  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

/* One-screen app: nothing scrolls except the mission text, so the map and the code are always visible. */
html, body { height: 100%; }
body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  color: var(--ink);
  background: var(--page);
  font-family: "Noto Sans TC", system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}
button, input { font: inherit; }

.topbar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  background: var(--card);
  border-bottom: 1px solid var(--line);
}
.home { display: grid; place-items: center; width: 44px; height: 44px; border-radius: 12px; background: #eef1f6; font-size: 22px; text-decoration: none; }
.brand { font-size: 20px; font-weight: 800; white-space: nowrap; }
.unit-tabs { display: flex; flex: 1 1 auto; gap: 6px; min-width: 0; }
.unit-tab {
  display: flex;
  flex: 1 1 0;
  align-items: center;
  gap: 8px;
  min-width: 0;
  min-height: 48px;
  padding: 4px 10px;
  border: 2px solid var(--line);
  border-radius: 14px;
  background: var(--card);
  color: var(--ink);
  cursor: pointer;
  text-align: left;
}
.unit-tab[aria-pressed="true"] { border-color: var(--brand); background: #eef2ff; }
.unit-tab.part-switch { flex: none; justify-content: center; background: #1d2939; border-color: #1d2939; color: #fff; font-weight: 800; }
.unit-tab > span:last-child { min-width: 0; }
.unit-icon { font-size: 24px; }
.unit-tab strong { display: block; overflow: hidden; font-size: 15px; text-overflow: ellipsis; white-space: nowrap; }
.unit-tab small { display: block; color: var(--muted); font-size: 12px; white-space: nowrap; }
.unit-tab .short { display: none; }
.top-right { display: flex; flex: none; align-items: center; gap: 8px; }
.icon-link { display: inline-flex; align-items: center; gap: 4px; min-height: 44px; padding: 0 8px; border-radius: 10px; color: #475467; font-weight: 700; text-decoration: none; white-space: nowrap; }
.total-stars { padding: 8px 10px; border-radius: 999px; background: #fff7db; font-weight: 800; white-space: nowrap; }
.lang-switch { display: flex; gap: 2px; padding: 3px; border-radius: 12px; background: #eef1f6; }
.lang-switch button { min-width: 44px; min-height: 40px; padding: 0 8px; border: 0; border-radius: 9px; background: transparent; cursor: pointer; font-weight: 800; }
.lang-switch button[aria-pressed="true"] { background: var(--card); box-shadow: 0 1px 4px rgba(0, 0, 0, .12); }

.quest-shell {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 44fr) minmax(0, 56fr);
  gap: 10px;
  padding: 10px max(10px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
}
.panel { min-width: 0; min-height: 0; overflow: hidden; border: 1px solid var(--line); border-radius: 18px; background: var(--card); box-shadow: 0 8px 24px rgba(31, 41, 55, .06); }

.mission-panel {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto auto minmax(0, 1fr) auto auto;
  grid-template-areas: "head" "maps" "board" "result" "controls";
}
.mission-head { grid-area: head; max-height: 38vh; overflow-y: auto; padding: 12px 14px 6px; }
.mission-dots { display: flex; flex-wrap: wrap; gap: 5px; margin-bottom: 8px; }
.mission-dot { width: 38px; height: 38px; border: 2px solid var(--line); border-radius: 11px; background: var(--card); cursor: pointer; font-weight: 800; }
.mission-dot[data-stars="2"] { border-color: #f5c451; background: #fff8e1; }
.mission-dot[data-stars="3"] { border-color: #f0a500; background: #ffe9a8; }
.mission-dot[aria-pressed="true"] { border-color: var(--brand); box-shadow: 0 0 0 3px #c7d2fe; }
.kicker { margin: 0; color: var(--brand); font-size: 13px; font-weight: 800; }
.mission-head h1 { margin: 2px 0 4px; font-size: clamp(21px, 2.4vw, 28px); line-height: 1.15; }
.story { margin: 0; color: #344054; font-size: 16px; line-height: 1.5; }
.lesson { margin: 6px 0 0; color: var(--ok-ink); font-size: 15px; font-weight: 800; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip { display: inline-flex; align-items: center; min-height: 28px; padding: 3px 10px; border-radius: 999px; background: #eef1f6; color: #475467; font-size: 13px; font-weight: 700; }
.chip[data-over="true"] { background: var(--fail-bg); color: var(--fail-ink); }
.hint { margin-top: 6px; }
.hint summary { display: inline-flex; align-items: center; gap: 4px; min-height: 40px; color: var(--warm-ink); cursor: pointer; font-weight: 800; }
.hint p { margin: 2px 0 0; padding: 8px 12px; border-radius: 12px; background: var(--warm-bg); line-height: 1.45; }

.map-row { grid-area: maps; display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 6px; padding: 4px 14px; }
.map-tabs { display: flex; flex-wrap: wrap; gap: 6px; }
.map-tab { min-height: 40px; padding: 0 12px; border: 2px solid var(--line); border-radius: 10px; background: var(--card); cursor: pointer; font-weight: 700; }
.map-tab[aria-pressed="true"] { border-color: var(--brand); }
.map-tab[data-outcome="pass"] { background: var(--ok-bg); color: var(--ok-ink); }
.map-tab[data-outcome="fail"] { background: var(--fail-bg); color: var(--fail-ink); }
.speed { display: flex; align-items: center; gap: 4px; margin-left: auto; font-size: 18px; }
.speed input { width: 110px; }

/* The board always fits the space it is given, whatever the map size or screen orientation. */
.board-wrap { grid-area: board; min-height: 0; display: grid; place-items: center; padding: 14px 14px 18px; container-type: size; }
.board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(var(--cols), 1fr);
  grid-template-rows: repeat(var(--rows), 1fr);
  width: min(100cqw, calc(100cqh * var(--cols) / var(--rows)), calc(var(--cols) * 84px));
  aspect-ratio: var(--cols) / var(--rows);
  padding: 5px;
  border-radius: 16px;
  background: #dfe9f7;
}
.cell { position: relative; display: grid; place-items: center; margin: 2px; border-radius: 9px; font-size: min(5.5cqw, 34px); }
.cell.floor { background: var(--floor); box-shadow: inset 0 0 0 2px var(--floor-line); }
.cell.wall { background: var(--wall); box-shadow: inset 0 -5px 0 rgba(0, 0, 0, .18); }
.cell.void { background: transparent; }
.goal, .gem { position: absolute; line-height: 1; }
.gem { transition: transform .25s ease, opacity .25s ease; }
.gem.taken { opacity: 0; transform: scale(1.6) translateY(-20%); }
.robot {
  position: absolute;
  top: calc(5px + (100% - 10px) * var(--y) / var(--rows));
  left: calc(5px + (100% - 10px) * var(--x) / var(--cols));
  z-index: 2;
  width: calc((100% - 10px) / var(--cols));
  height: calc((100% - 10px) / var(--rows));
  transition: top .28s ease, left .28s ease;
}
.robot.no-anim, .robot.no-anim .robot-body { transition: none; }
.robot-body { width: 100%; height: 100%; transition: transform .28s ease; }
.robot-body svg { display: block; width: 100%; height: 100%; transform: scale(.86); filter: drop-shadow(0 4px 3px rgba(0, 0, 0, .25)); }
.robot.bump { animation: bump .35s ease 2; }
@keyframes bump { 50% { translate: 8% 0; } }
.bubble { position: absolute; bottom: 92%; left: 50%; translate: -50% 0; padding: 3px 10px; border-radius: 12px; background: var(--card); box-shadow: 0 6px 16px rgba(0, 0, 0, .18); font-size: 20px; font-weight: 800; white-space: nowrap; }
.counter-badge { max-width: calc(100% + 8px); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; position: absolute; right: -8px; bottom: -14px; z-index: 3; padding: 3px 12px; border-radius: 999px; background: #fce7f6; color: #9e1b77; box-shadow: 0 3px 8px rgba(0, 0, 0, .12); font-size: 18px; font-weight: 800; }

.result-row { grid-area: result; display: flex; align-items: center; gap: 8px; padding: 0 14px 8px; }
.result { flex: 1; min-width: 0; min-height: 44px; margin: 0; padding: 10px 12px; border-radius: 12px; background: #f5f7fa; font-size: 15px; font-weight: 700; line-height: 1.35; }
.result[data-tone="fail"] { background: var(--fail-bg); color: var(--fail-ink); }
.result[data-tone="success"] { background: var(--ok-bg); color: var(--ok-ink); }

.controls { grid-area: controls; display: grid; grid-template-columns: minmax(80px, 1fr) auto auto auto auto; gap: 6px; padding: 0 14px 14px; }
.btn { min-width: 0; overflow: hidden; text-overflow: ellipsis; min-height: 52px; padding: 0 12px; border: 0; border-radius: 14px; cursor: pointer; font-weight: 800; white-space: nowrap; touch-action: manipulation; }
.btn:disabled { cursor: wait; opacity: .6; }
.btn:active { transform: translateY(1px); }
.btn-run { background: linear-gradient(135deg, var(--run), var(--run-2)); color: #fff; font-size: 19px; }
.btn-soft { background: #eef1f6; color: #344054; font-size: 14px; }
.btn-warm { background: var(--warm-bg); color: var(--warm-ink); font-size: 14px; }
.btn-next { flex: none; min-height: 44px; background: var(--brand); color: #fff; }
.btn:focus-visible, .unit-tab:focus-visible, .mission-dot:focus-visible, .map-tab:focus-visible { outline: 3px solid #1d6fe0; outline-offset: 2px; }

.workspace-panel { display: flex; flex-direction: column; }
.workspace-head { flex: none; display: flex; align-items: center; justify-content: space-between; padding: 8px 14px; border-bottom: 1px solid var(--line); }
.workspace-head h2 { margin: 0; font-size: 18px; }
#blocklyDiv { flex: 1; min-height: 0; }
.load-warning { margin: 16px; padding: 12px; border-radius: 12px; background: var(--fail-bg); color: var(--fail-ink); }

/* Narrower landscape screens (iPad mini, split view): keep the header on one line. */
@media (max-width: 1180px) {
  .brand, .icon-link span, .unit-tab small { display: none; }
}

@media (max-width: 1000px) {
  .icon-link { display: none; }
  .unit-tab .full { display: none; }
  .unit-tab .short { display: block; }
}

/* iPad portrait: mission + map on top, code below. The text column sits beside the map. */
@media (orientation: portrait) and (min-width: 600px) {
  .quest-shell { grid-template-columns: 1fr; grid-template-rows: minmax(0, 47fr) minmax(0, 53fr); }
  .mission-panel {
    grid-template-columns: minmax(290px, 43%) minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) auto auto auto;
    grid-template-areas: "head board" "maps board" "result board" "controls board";
  }
  .mission-head { max-height: none; }
  .mission-dots { flex-wrap: nowrap; overflow-x: auto; padding: 3px; }
  .mission-dot { flex: none; width: 36px; height: 36px; border-radius: 10px; }
  .kicker, .map-tab .full { display: none; }
  .map-tab { min-width: 48px; padding: 0 8px; }
  .speed input { width: 80px; }
  .story { font-size: 15px; }
  .controls { grid-template-columns: repeat(4, auto); gap: 6px; }
  .btn-run { grid-column: 1 / -1; }
  .controls .btn:not(.btn-run) { min-height: 46px; padding: 0 6px; font-size: 13px; }
  .board-wrap { padding: 12px 12px 18px 4px; }
}

/* Phones: fall back to a scrolling page. */
@media (max-width: 599px) {
  body { height: auto; overflow: auto; }
  .topbar { flex-wrap: wrap; }
  .unit-tabs { order: 3; flex-basis: 100%; }
  .unit-tab small, .icon-link span, .brand { display: none; }
  .quest-shell { grid-template-columns: 1fr; }
  .mission-head { max-height: none; }
  .board-wrap { height: 52vh; }
  .controls { grid-template-columns: repeat(4, 1fr); }
  .btn-run { grid-column: 1 / -1; }
  .workspace-panel { height: 80vh; }
}

.confirm-overlay { position: fixed; inset: 0; z-index: 1000; display: grid; place-items: center; padding: 16px; background: rgba(16, 24, 40, .45); }
.confirm-box { width: min(360px, 100%); padding: 20px; border-radius: 18px; background: var(--card); box-shadow: 0 20px 50px rgba(16, 24, 40, .3); }
.confirm-box p { margin: 0 0 16px; font-size: 17px; font-weight: 700; line-height: 1.5; }
.confirm-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
