* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

@font-face {
  font-family: "Peli Emoji";
  src: url("/fonts/NotoColorEmoji.woff2") format("woff2");
  font-display: swap;
}

html {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  margin: 0;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  font-family: "Comic Sans MS", "Trebuchet MS", Verdana, sans-serif;
  background: linear-gradient(180deg, #dfe7f5 0%, #bfcde8 100%);
  color: #24304a;
  display: flex;
  flex-direction: column;
  align-items: center;
  user-select: none;
}

:root.no-emoji-font body {
  font-family: "Comic Sans MS", "Trebuchet MS", Verdana, "Peli Emoji", sans-serif;
}

header {
  width: 100%;
  padding: 0.5rem 1rem 0.8rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.8rem;
}

header h1 {
  margin: 0 auto;
  font-size: 2.1rem;
}

#active-player {
  flex-shrink: 0;
  font-weight: bold;
  background: #ffffff;
  border-radius: 14px;
  padding: 0.5rem 0.9rem;
  box-shadow: 0 4px 0 #6c8ebf;
}

main {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  width: 100%;
  max-width: 1100px;
  padding: 0 1rem 1.5rem;
  position: relative;
}

#hub-link {
  color: #24304a;
  font-weight: bold;
  text-decoration: none;
  background: #ffffff;
  border-radius: 14px;
  padding: 0.6rem 1.1rem;
  box-shadow: 0 4px 0 #6c8ebf;
}

.hidden {
  display: none !important;
}

h2 {
  text-align: center;
  margin: 0.4rem 0 1rem;
}

/* ---------------- Level chooser ---------------- */

#level-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}

.level-button {
  background: #ffffff;
  border: 3px solid transparent;
  border-radius: 18px;
  padding: 1rem 0.8rem;
  font-family: inherit;
  cursor: pointer;
  box-shadow: 0 5px 0 #6c8ebf;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.3rem;
  min-height: 48px;
}

.level-button.done {
  background: #eaf8ea;
  border-color: #6cbf6c;
}

.level-button:active {
  transform: translateY(3px);
  box-shadow: 0 2px 0 #6c8ebf;
}

.level-number {
  font-size: 1.6rem;
  font-weight: bold;
  color: #4a6ea8;
}

.level-name {
  font-size: 0.95rem;
  font-weight: bold;
  text-align: center;
}

.level-mark {
  font-size: 1.1rem;
}

/* ---------------- Task ---------------- */

#task-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  justify-content: center;
  margin-top: 0.4rem;
}

#task-title {
  font-size: 1.3rem;
  font-weight: bold;
}

#hint-button {
  border: none;
  background: #fff3bf;
  border-radius: 14px;
  font-size: 1.2rem;
  padding: 0.45rem 0.7rem;
  cursor: pointer;
  box-shadow: 0 4px 0 #d4a418;
  min-height: 44px;
  min-width: 44px;
}

#task-hint {
  margin: 0.3rem 0 0.8rem;
  text-align: center;
  font-size: 0.95rem;
  color: #4a5670;
}

#play-area {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 1.2rem;
  align-items: start;
}

/* ---------------- Board ---------------- */

#board-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.6rem;
}

#board {
  display: grid;
  gap: 3px;
  width: 100%;
  max-width: 420px;
  background: #8fa4c6;
  padding: 3px;
  border-radius: 12px;
}

#board.bump {
  animation: bump 0.4s ease;
}

@keyframes bump {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-8px); }
  75% { transform: translateX(8px); }
}

.cell {
  aspect-ratio: 1;
  background: #f3f7ff;
  border-radius: 7px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(0.9rem, 3.4vw, 1.7rem);
  position: relative;
}

.cell.wall {
  background: #56698c;
  box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.18);
}

.cell.goal {
  background: #dff5df;
}

.robot {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.15em;
  animation: hop 0.42s ease;
}

@keyframes hop {
  0% { transform: scale(0.75); }
  60% { transform: scale(1.12); }
  100% { transform: scale(1); }
}

#message {
  margin: 0;
  min-height: 1.5rem;
  font-weight: bold;
  text-align: center;
}

#message.ok { color: #1b5e20; }
#message.fail { color: #8c1d18; }

/* ---------------- Program editor ---------------- */

#editor {
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
  min-width: 0;
}

#program-box {
  background: #ffffff;
  border-radius: 18px;
  padding: 0.7rem 0.8rem;
  box-shadow: 0 5px 0 #6c8ebf;
  min-height: 8rem;
}

#program-header {
  display: flex;
  justify-content: space-between;
  font-weight: bold;
  font-size: 0.9rem;
  color: #4a5670;
  margin-bottom: 0.4rem;
}

#program-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  align-items: flex-start;
}

#program-empty {
  margin: 0.3rem 0 0;
  font-size: 0.85rem;
  color: #8b93a7;
}

.cmd-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.1rem;
  height: 2.1rem;
  border-radius: 9px;
  background: #e6edfa;
  font-size: 1.1rem;
  line-height: 1;
}

.cmd-chip.running {
  background: #ffe08a;
  box-shadow: 0 0 0 3px #d4a418;
}

/* A loop is drawn as a box around its own commands, so the child can see what
   gets repeated rather than having to imagine it. */
.loop-box {
  width: 100%;
  border: 2px dashed #7a5cc4;
  border-radius: 12px;
  padding: 0.35rem 0.5rem 0.5rem;
  background: #f4f0ff;
}

.loop-box.editing {
  border-style: solid;
  box-shadow: 0 0 0 3px rgba(122, 92, 196, 0.25);
}

.loop-head {
  font-size: 0.85rem;
  font-weight: bold;
  color: #5b3fa8;
  margin-bottom: 0.3rem;
}

.loop-body {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
}

.loop-empty {
  font-size: 0.8rem;
  color: #9a8ec4;
}

#palette,
#loop-palette,
#run-row {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.cmd-button {
  flex: 1;
  min-width: 3.4rem;
  min-height: 56px;
  border: none;
  border-radius: 16px;
  background: #ffffff;
  font-size: 1.8rem;
  cursor: pointer;
  box-shadow: 0 5px 0 #6c8ebf;
  font-family: inherit;
}

.cmd-button:active {
  transform: translateY(3px);
  box-shadow: 0 2px 0 #6c8ebf;
}

.loop-button,
#close-loop {
  flex: 1;
  min-width: 6rem;
  min-height: 48px;
  border: none;
  border-radius: 14px;
  background: #efe7ff;
  color: #4a2f96;
  font-family: inherit;
  font-weight: bold;
  font-size: 0.9rem;
  cursor: pointer;
  box-shadow: 0 4px 0 #7a5cc4;
}

#close-loop {
  background: #dff5df;
  color: #1b5e20;
  box-shadow: 0 4px 0 #2e7d32;
}

#run-row button {
  flex: 1;
  min-width: 6rem;
  min-height: 48px;
  border: none;
  border-radius: 14px;
  font-family: inherit;
  font-weight: bold;
  font-size: 0.95rem;
  cursor: pointer;
  background: #ffffff;
  box-shadow: 0 4px 0 #6c8ebf;
}

#run-button {
  background: #d7f7dc;
  color: #1b5e20;
  box-shadow: 0 4px 0 #2e7d32;
  font-size: 1.1rem;
}

#run-button:disabled {
  opacity: 0.45;
  cursor: default;
}

/* ---------------- Finished ---------------- */

#done-view {
  position: fixed;
  inset: 0;
  z-index: 2000;
  background: rgba(30, 45, 70, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

#done-box {
  background: #ffffff;
  border-radius: 24px;
  box-shadow: 0 8px 0 #6c8ebf;
  padding: 1.8rem;
  max-width: 26rem;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.8rem;
}

#done-icon {
  font-size: 3.6rem;
  line-height: 1;
}

#done-message {
  margin: 0;
  font-size: 1.4rem;
  font-weight: bold;
}

#done-buttons {
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
  justify-content: center;
}

#done-buttons button {
  border: none;
  border-radius: 14px;
  font-family: inherit;
  font-weight: bold;
  font-size: 1rem;
  padding: 0.7rem 1.1rem;
  cursor: pointer;
  min-height: 48px;
  background: #eef2fa;
  box-shadow: 0 4px 0 #6c8ebf;
}

#next-button {
  background: #d7f7dc;
  color: #1b5e20;
  box-shadow: 0 4px 0 #2e7d32;
}

button,
a {
  touch-action: manipulation;
}

@media (hover: none) {
  button:hover,
  a:hover {
    transform: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .robot,
  #board.bump {
    animation: none;
  }
}

/* The board and the editor stack on a narrow screen, board first. */
@media (max-width: 820px) {
  #play-area {
    grid-template-columns: 1fr;
  }

  #board {
    max-width: 330px;
  }

  header h1 {
    font-size: 1.5rem;
  }

  #level-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  #task-title { font-size: 1.1rem; }
  #task-hint { font-size: 0.85rem; }
  .cmd-button { font-size: 1.5rem; min-height: 52px; }
}
