:root {
  --bg: #07111f;
  --bg-2: #101828;
  --panel: #f7fbff;
  --panel-2: #e9f6ff;
  --ink: #132033;
  --muted: #607089;
  --line: #c7d8ea;
  --green: #18b26b;
  --green-2: #0f8f59;
  --sky: #1d8fea;
  --amber: #f4b63f;
  --red: #ef5962;
  --violet: #7d5bed;
  --shadow: 0 18px 50px rgba(10, 20, 38, 0.18);
  color-scheme: light;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  overscroll-behavior: contain;
  background:
    linear-gradient(135deg, rgba(24, 178, 107, 0.12), transparent 34%),
    linear-gradient(315deg, rgba(29, 143, 234, 0.16), transparent 38%),
    var(--bg);
  color: var(--ink);
  font-family: "Noto Sans TC", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input {
  font: inherit;
}

button {
  cursor: pointer;
}

.level-button,
.ghost-button,
.primary-button,
.secondary-button,
.quick-block-button {
  touch-action: manipulation;
}

svg {
  display: block;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 78px;
  padding: 14px clamp(16px, 3vw, 32px);
  color: #f8fbff;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  color: inherit;
  text-decoration: none;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border: 2px solid rgba(255, 255, 255, 0.38);
  border-radius: 14px;
  background: #16283d;
  box-shadow: 0 8px 0 rgba(0, 0, 0, 0.18);
}

.brand-mark svg {
  width: 30px;
  height: 30px;
  fill: #33d17a;
  stroke: #f8fbff;
  stroke-width: 2.1;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.brand strong,
h1,
h2 {
  font-family: "Baloo 2", "Noto Sans TC", sans-serif;
  letter-spacing: 0;
}

.brand strong {
  display: block;
  font-size: clamp(1.45rem, 3vw, 2rem);
  line-height: 1;
}

.eyebrow {
  display: block;
  color: inherit;
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  opacity: 0.72;
}

.score-strip {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-height: 42px;
  padding: 8px 14px;
  border: 1px solid rgba(255, 255, 255, 0.24);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.12);
  color: #f8fbff;
}

.score-strip strong {
  color: #87f0ad;
}

.language-switch {
  display: inline-flex;
  min-height: 42px;
  padding: 4px;
  border: 1px solid rgba(255, 255, 255, 0.24);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.12);
}

.language-switch button {
  min-width: 72px;
  min-height: 34px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: #d8e7f8;
  font-size: 0.86rem;
  font-weight: 800;
  touch-action: manipulation;
}

.language-switch button.is-active {
  background: #f8fbff;
  color: #17324d;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(320px, 0.75fr) minmax(560px, 1.45fr);
  grid-template-areas:
    "level level"
    "play workspace";
  grid-template-rows: auto minmax(0, 1fr);
  gap: 16px;
  min-height: calc(100vh - 78px);
  padding: 0 clamp(16px, 3vw, 32px) 22px;
}

.level-toolbar,
.play-panel,
.workspace-panel {
  min-width: 0;
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-radius: 18px;
  background: var(--panel);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.level-toolbar {
  display: grid;
  grid-template-columns: minmax(240px, 320px) minmax(0, 1fr) minmax(190px, 250px);
  gap: 14px;
  align-items: stretch;
  grid-area: level;
  background: #f8fbff;
}

.level-select-box,
.mission-block,
.play-header,
.workspace-header {
  padding: 18px;
}

.level-select-box {
  display: grid;
  gap: 8px;
  align-content: center;
  border-right: 1px solid var(--line);
  background: linear-gradient(135deg, #ffffff, var(--panel-2));
}

.level-select-box label {
  color: #5f7189;
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.level-select-box select {
  width: 100%;
  min-height: 50px;
  padding: 0 42px 0 14px;
  border: 2px solid #c8daec;
  border-radius: 12px;
  background: #ffffff;
  color: #17324d;
  font-weight: 800;
}

.mission-block {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 14px;
  align-items: center;
  background: #ffffff;
}

.level-kicker {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 4px 10px;
  border-radius: 999px;
  background: #dff7e8;
  color: #11643f;
  font-size: 0.82rem;
  font-weight: 700;
}

h1,
h2,
p {
  margin: 0;
}

.mission-block h1,
.play-header h2,
.workspace-header h2 {
  margin-top: 8px;
  font-size: clamp(1.45rem, 2.5vw, 2rem);
  line-height: 1.05;
}

.mission-block p {
  margin-top: 8px;
  color: var(--muted);
  line-height: 1.55;
}

.concept-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 14px;
}

.concept-row span,
.block-count,
.run-status {
  display: inline-flex;
  align-items: center;
  min-height: 32px;
  padding: 5px 10px;
  border-radius: 999px;
  background: #eef5ff;
  color: #255176;
  font-size: 0.86rem;
  font-weight: 700;
}

.level-stars {
  color: #a05c00;
  font-weight: 800;
}

.hint-box {
  display: grid;
  gap: 10px;
  padding: 14px;
  border-left: 1px solid var(--line);
  background: #f2f8ff;
  align-content: center;
}

.ghost-button,
.secondary-button,
.primary-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
  border-radius: 12px;
  font-weight: 800;
  transition: background 180ms ease, border-color 180ms ease, color 180ms ease, opacity 180ms ease;
}

.ghost-button {
  width: 100%;
  border: 1px solid #bdd3ea;
  background: #ffffff;
  color: #254760;
}

.ghost-button:hover,
.ghost-button:focus-visible {
  border-color: var(--sky);
  color: #0f5e95;
  outline: none;
}

.ghost-button svg,
.secondary-button svg,
.primary-button svg {
  width: 19px;
  height: 19px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.primary-button svg {
  fill: currentColor;
  stroke: none;
}

.hint-box p {
  color: #41566f;
  line-height: 1.5;
}

.play-panel {
  display: grid;
  grid-template-rows: auto minmax(300px, 1fr) auto auto;
  background: #ffffff;
  grid-area: play;
}

.play-header,
.workspace-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--line);
  background: #ffffff;
}

.play-header .eyebrow,
.workspace-header .eyebrow {
  color: #5b708a;
}

.run-status {
  background: #fff2cc;
  color: #7b4d00;
}

.run-status.is-good {
  background: #dbf8e9;
  color: #11643f;
}

.run-status.is-bad {
  background: #ffe4e7;
  color: #9f1d2f;
}

.board-wrap {
  display: grid;
  place-items: center;
  padding: 18px;
  background:
    linear-gradient(90deg, rgba(19, 32, 51, 0.04) 1px, transparent 1px),
    linear-gradient(rgba(19, 32, 51, 0.04) 1px, transparent 1px),
    #eaf4ff;
  background-size: 22px 22px;
}

.board {
  --cols: 5;
  --rows: 5;
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(42px, 1fr));
  grid-template-rows: repeat(var(--rows), minmax(42px, 1fr));
  width: min(100%, 470px);
  aspect-ratio: var(--cols) / var(--rows);
  border: 4px solid #17324d;
  border-radius: 18px;
  background: #dceeff;
  box-shadow: 0 14px 0 #17324d;
  overflow: hidden;
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  border: 1px solid rgba(23, 50, 77, 0.18);
  background: #f9fcff;
}

.cell:nth-child(odd) {
  background: #eff8ff;
}

.cell.is-wall {
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.16), transparent),
    #43566f;
}

.cell.is-wall::before,
.cell.is-wall::after {
  content: "";
  position: absolute;
  width: 65%;
  height: 4px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.28);
}

.cell.is-wall::after {
  transform: rotate(90deg);
}

.goal {
  width: 58%;
  aspect-ratio: 1;
  border: 7px solid #20c977;
  border-radius: 50%;
  box-shadow: inset 0 0 0 5px rgba(32, 201, 119, 0.22), 0 0 0 4px rgba(32, 201, 119, 0.16);
}

.gem {
  width: 30%;
  aspect-ratio: 1;
  transform: rotate(45deg);
  border: 3px solid #fff8d9;
  border-radius: 5px;
  background: linear-gradient(135deg, #ffe27a, #f59e0b);
  box-shadow: 0 6px 0 rgba(116, 74, 0, 0.2);
}

.gem.is-collected {
  opacity: 0.18;
  filter: grayscale(1);
}

.bot {
  position: relative;
  width: 52%;
  aspect-ratio: 1;
  border-radius: 42% 42% 50% 50%;
  background: linear-gradient(180deg, #42a5ff, #1166bd);
  box-shadow: 0 7px 0 rgba(17, 66, 120, 0.25);
  transition: transform 220ms ease;
}

.bot::before {
  content: "";
  position: absolute;
  width: 16%;
  height: 16%;
  border-radius: 50%;
  background: #ffffff;
  transform: translate(80%, 70%);
  box-shadow: 16px 0 0 #ffffff;
}

.bot.dir-e {
  transform: rotate(90deg);
}

.bot.dir-s {
  transform: rotate(180deg);
}

.bot.dir-w {
  transform: rotate(270deg);
}

.controls {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr auto;
  gap: 10px;
  padding: 14px;
  border-top: 1px solid var(--line);
}

.primary-button {
  border: 2px solid #0b6a42;
  background: var(--green);
  color: #ffffff;
}

.primary-button:hover,
.primary-button:focus-visible {
  background: var(--green-2);
  outline: none;
}

.primary-button:disabled,
.secondary-button:disabled {
  cursor: not-allowed;
  opacity: 0.58;
}

.secondary-button {
  border: 2px solid #c9d8e8;
  background: #ffffff;
  color: #23425f;
}

.secondary-button:hover,
.secondary-button:focus-visible {
  border-color: var(--sky);
  outline: none;
}

.speed-control {
  display: inline-grid;
  align-items: center;
  grid-template-columns: auto minmax(100px, 130px);
  gap: 8px;
  min-height: 42px;
  padding: 0 10px;
  border: 1px solid #cfdeee;
  border-radius: 12px;
  color: #3b5069;
  font-size: 0.88rem;
  font-weight: 700;
}

.speed-control input {
  accent-color: var(--green);
}

.result-line {
  min-height: 48px;
  padding: 12px 16px 16px;
  color: #42566f;
  line-height: 1.45;
}

.result-line.is-good {
  color: #11643f;
  font-weight: 700;
}

.result-line.is-bad {
  color: #9f1d2f;
  font-weight: 700;
}

.workspace-panel {
  display: grid;
  grid-template-rows: auto auto minmax(560px, 1fr) auto;
  background: #101828;
  grid-area: workspace;
}

.workspace-header {
  color: #f8fbff;
  background: #101828;
  border-bottom-color: rgba(255, 255, 255, 0.16);
}

.workspace-header .eyebrow {
  color: #a8c0dd;
}

.block-count {
  background: rgba(255, 255, 255, 0.12);
  color: #e9f6ff;
}

.block-count.is-over-limit {
  background: #ffe4e7;
  color: #9f1d2f;
}

.quick-blocks {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 10px;
  align-items: center;
  padding: 12px 14px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.16);
  background: #172033;
  color: #d8e7f8;
}

.quick-blocks > span {
  font-size: 0.9rem;
  font-weight: 800;
}

.quick-blocks > div {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.quick-block-button {
  display: inline-flex;
  align-items: center;
  min-height: 48px;
  padding: 7px 13px;
  border: 1px solid rgba(255, 255, 255, 0.24);
  border-radius: 10px;
  background: #f8fbff;
  color: #17324d;
  font-size: 0.88rem;
  font-weight: 800;
  transition: background 180ms ease, border-color 180ms ease;
}

.quick-block-button:hover,
.quick-block-button:focus-visible {
  border-color: #87c8ff;
  background: #eaf6ff;
  outline: none;
}

#blocklyDiv {
  min-height: 560px;
  height: 100%;
  width: 100%;
}

.load-warning {
  margin: 14px;
  padding: 12px;
  border: 1px solid #ffd3da;
  border-radius: 12px;
  background: #ffe8eb;
  color: #912034;
}

:focus-visible {
  outline: 3px solid rgba(29, 143, 234, 0.42);
  outline-offset: 2px;
}

@media (max-width: 1180px) {
  .app-shell {
    grid-template-columns: minmax(310px, 0.7fr) minmax(520px, 1.3fr);
    grid-template-areas:
      "level level"
      "play workspace";
    align-items: stretch;
  }

  .level-toolbar {
    grid-template-columns: minmax(220px, 300px) minmax(0, 1fr);
  }

  .hint-box {
    grid-column: 1 / -1;
    border-top: 1px solid var(--line);
    border-left: 0;
    grid-template-columns: 220px minmax(0, 1fr);
    align-items: center;
  }

  .workspace-panel {
    min-height: calc(100vh - 210px);
  }

  .controls {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .speed-control {
    grid-column: 1 / -1;
    grid-template-columns: auto 1fr;
  }

  .board {
    width: min(100%, 430px);
  }
}

@media (max-width: 860px) {
  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .score-strip {
    width: 100%;
    justify-content: space-between;
  }

  .language-switch {
    width: 100%;
  }

  .language-switch button {
    flex: 1;
  }

  .app-shell {
    grid-template-columns: 1fr;
    grid-template-areas:
      "level"
      "workspace"
      "play";
    min-height: auto;
  }

  .level-toolbar {
    grid-template-columns: 1fr;
  }

  .level-select-box,
  .hint-box {
    border: 0;
    border-top: 1px solid var(--line);
  }

  .mission-block {
    grid-template-columns: 1fr;
  }

  .level-select-box,
  .mission-block,
  .hint-box,
  .play-header,
  .workspace-header {
    padding: 14px;
  }

  .play-panel,
  .workspace-panel {
    min-height: auto;
  }

  .workspace-panel {
    grid-template-rows: auto auto minmax(560px, 68vh) auto;
  }

  .controls {
    grid-template-columns: 1fr;
  }

  .quick-blocks {
    grid-template-columns: 1fr;
  }

  .speed-control {
    grid-template-columns: auto 1fr;
  }
}

@media (max-width: 560px) {
  .hint-box {
    grid-template-columns: 1fr;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 1ms !important;
    animation-duration: 1ms !important;
  }
}
