:root {
  --ink: #182029;
  --muted: #5c6a76;
  --panel: #ffffff;
  --paper: #f7f4ed;
  --grid: #d9e0e6;
  --blue: #1f72c9;
  --blue-dark: #164f8d;
  --green: #27824b;
  --yellow: #ffc943;
  --red: #c3423f;
  --tile: #fdfbf7;
  --shadow: 0 12px 30px rgba(31, 45, 61, 0.13);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  color: var(--ink);
  background:
    linear-gradient(90deg, rgba(31, 114, 201, 0.08) 0 1px, transparent 1px 100%),
    linear-gradient(0deg, rgba(39, 130, 75, 0.07) 0 1px, transparent 1px 100%),
    var(--paper);
  background-size: 32px 32px;
  font-family: "Segoe UI", Arial, sans-serif;
}

button {
  font: inherit;
}

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: minmax(430px, 1fr) 360px;
  gap: 18px;
  padding: 18px;
}

.play-area,
.control-area {
  min-width: 0;
}

.play-area {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.top-bar,
.goal-strip,
.status-panel,
.panel {
  background: rgba(255, 255, 255, 0.94);
  border: 2px solid rgba(24, 32, 41, 0.08);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 18px;
}

.eyebrow {
  margin: 0 0 3px;
  color: var(--blue-dark);
  font-size: 0.82rem;
  font-weight: 800;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(1.7rem, 4vw, 2.55rem);
  letter-spacing: 0;
}

h2 {
  margin-bottom: 8px;
  font-size: 1.08rem;
  letter-spacing: 0;
}

.level-tools {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-button {
  width: 42px;
  height: 42px;
  border: 0;
  border-radius: 8px;
  color: #fff;
  background: var(--blue);
  font-size: 2rem;
  line-height: 1;
  cursor: pointer;
}

.icon-button:disabled {
  opacity: 0.42;
  cursor: not-allowed;
}

.level-chip,
.score-box {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 42px;
  padding: 0 13px;
  border-radius: 8px;
  color: #fff;
  background: var(--green);
  font-weight: 800;
  white-space: nowrap;
}

.goal-strip {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
}

.goal-strip p {
  margin-bottom: 0;
  color: var(--muted);
}

.score-box {
  flex-direction: column;
  gap: 1px;
  background: #6b4ca5;
  font-size: 0.82rem;
}

#starsText {
  font-size: 1.1rem;
}

.board-wrap {
  flex: 1;
  display: grid;
  place-items: center;
  min-height: 410px;
  padding: 14px;
}

.board {
  width: min(100%, 690px);
  aspect-ratio: 6 / 5;
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: repeat(5, 1fr);
  gap: 7px;
  padding: 8px;
  border: 4px solid #233344;
  border-radius: 8px;
  background: #233344;
  box-shadow: 0 18px 35px rgba(24, 32, 41, 0.22);
}

.tile {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  border-radius: 6px;
  background: var(--tile);
  overflow: hidden;
}

.tile::after {
  content: "";
  position: absolute;
  inset: 0;
  border: 1px solid rgba(24, 32, 41, 0.08);
  border-radius: inherit;
  pointer-events: none;
}

.tile.path {
  background: #e7f3ff;
}

.tile.start {
  background: #daf3e3;
}

.tile.goal {
  background: #fff2b9;
}

.tile.block {
  background: #dfe4ea;
}

.tile.star {
  background: #fff8da;
}

.tile.error {
  outline: 4px solid var(--red);
  outline-offset: -4px;
}

.thing {
  position: relative;
  z-index: 1;
  display: grid;
  place-items: center;
  width: 70%;
  height: 70%;
  font-weight: 900;
}

.battery {
  width: 60%;
  height: 42%;
  border: 4px solid #815f00;
  border-radius: 6px;
  background: linear-gradient(90deg, var(--yellow) 0 70%, #fff1a2 70%);
}

.battery::after {
  content: "";
  position: absolute;
  right: 10%;
  width: 8%;
  height: 23%;
  border-radius: 2px;
  background: #815f00;
  transform: translateX(180%);
}

.bug-block {
  width: 62%;
  height: 62%;
  border-radius: 8px;
  background: repeating-linear-gradient(45deg, #66707a 0 10px, #4d5660 10px 20px);
  border: 3px solid #39414a;
}

.star-token {
  color: #d49300;
  font-size: clamp(1.35rem, 5vw, 2.6rem);
  text-shadow: 0 2px 0 #fff6ce;
}

.robot {
  position: relative;
  z-index: 3;
  width: 68%;
  height: 68%;
  border: 4px solid #17344d;
  border-radius: 8px;
  background: linear-gradient(#69c4ff, #2892da);
  box-shadow: 0 6px 0 #17344d;
  transition: transform 180ms ease, filter 180ms ease;
}

.robot::before,
.robot::after {
  content: "";
  position: absolute;
  top: 30%;
  width: 16%;
  height: 16%;
  border-radius: 50%;
  background: #fff;
  box-shadow: inset 0 0 0 3px #17344d;
}

.robot::before {
  left: 23%;
}

.robot::after {
  right: 23%;
}

.robot .mouth {
  position: absolute;
  left: 30%;
  right: 30%;
  bottom: 22%;
  height: 8%;
  border-radius: 10px;
  background: #17344d;
}

.robot.north {
  transform: rotate(0deg);
}

.robot.east {
  transform: rotate(90deg);
}

.robot.south {
  transform: rotate(180deg);
}

.robot.west {
  transform: rotate(270deg);
}

.status-panel {
  display: grid;
  grid-template-columns: 54px 1fr;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
}

.robot-face {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  width: 52px;
  height: 44px;
  border: 3px solid #17344d;
  border-radius: 8px;
  background: #69c4ff;
}

.eye {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #17344d;
}

#statusText {
  margin-bottom: 0;
  font-weight: 700;
}

.control-area {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.panel {
  padding: 14px;
}

.panel p {
  color: var(--muted);
}

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.segmented button,
.tool-button,
.hint-button,
.primary-button,
.command-card,
.program-item button {
  border: 0;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 800;
}

.segmented button {
  min-height: 44px;
  color: var(--ink);
  background: #e8edf2;
}

.segmented button.selected {
  color: #fff;
  background: var(--blue);
}

.command-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.command-card {
  display: grid;
  grid-template-columns: 44px 1fr;
  align-items: center;
  min-height: 58px;
  padding: 8px;
  color: var(--ink);
  background: #f1f6fb;
  border: 2px solid #d8e4ef;
  text-align: left;
}

.command-card:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.command-icon {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 8px;
  color: #fff;
  background: var(--blue);
  font-size: 1.35rem;
}

.program-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

#blockLimit {
  color: var(--muted);
  font-weight: 800;
}

.program-list {
  min-height: 158px;
  max-height: 260px;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  list-style: none;
  counter-reset: step;
}

.program-item {
  counter-increment: step;
  display: grid;
  grid-template-columns: 32px 1fr 34px;
  align-items: center;
  gap: 8px;
  min-height: 42px;
  margin-bottom: 8px;
  padding: 6px;
  border-radius: 8px;
  background: #f8fafc;
  border: 2px solid #e3e9ef;
}

.program-item::before {
  content: counter(step);
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  color: #fff;
  background: var(--green);
  font-weight: 900;
}

.program-item.active {
  border-color: var(--blue);
  background: #e7f3ff;
}

.program-item.bug {
  border-color: var(--red);
  background: #ffecea;
}

.program-item button {
  width: 32px;
  height: 32px;
  color: #fff;
  background: var(--red);
}

.empty-program {
  min-height: 132px;
  display: grid;
  place-items: center;
  padding: 18px;
  border: 2px dashed #b9c4ce;
  border-radius: 8px;
  color: var(--muted);
  font-weight: 800;
  text-align: center;
}

.program-actions,
.run-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 9px;
  margin-top: 10px;
}

.run-actions {
  grid-template-columns: 1.2fr 1fr 1fr;
  margin-top: 0;
}

.tool-button,
.hint-button,
.primary-button {
  min-height: 46px;
  padding: 0 10px;
}

.primary-button {
  color: #fff;
  background: var(--green);
  font-size: 1.08rem;
}

.tool-button {
  color: #fff;
  background: #596777;
}

.hint-button {
  width: 100%;
  margin-top: 10px;
  color: #17344d;
  background: var(--yellow);
}

.hint-text {
  margin: 10px 0 0;
  padding: 10px;
  border-radius: 8px;
  color: #583d00;
  background: #fff6ce;
  font-weight: 700;
}

button:focus-visible {
  outline: 4px solid #111;
  outline-offset: 2px;
}

@media (max-width: 900px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .control-area {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .run-panel {
    grid-column: 1 / -1;
  }
}

@media (max-width: 620px) {
  .app-shell {
    padding: 10px;
  }

  .top-bar,
  .goal-strip {
    align-items: stretch;
    flex-direction: column;
  }

  .level-tools {
    justify-content: space-between;
  }

  .board-wrap {
    min-height: auto;
    padding: 4px;
  }

  .board {
    gap: 4px;
    padding: 5px;
  }

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

  .run-actions {
    grid-template-columns: 1fr;
  }
}
