:root {
  --bg: #eef1f6;
  --panel: #ffffff;
  --ink: #1f2430;
  --muted: #5d6578;
  --line: #dde1ea;
  --asphalt: #2b2f36;
  --red: #e63946;
  --yellow: #ffc300;
  --green: #2bb673;
  --blue: #1d70a2;
  --radius: 16px;
  --shadow: 0 6px 24px rgba(31, 36, 48, 0.08);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: Nunito, system-ui, -apple-system, "Segoe UI", sans-serif;
}

button { font: inherit; }

/* --- header ---------------------------------------------------------------- */

.top {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 10px 20px;
  background: var(--asphalt);
  color: #fff;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 900;
  font-size: 22px;
  letter-spacing: 0.3px;
}

.logo {
  display: inline-flex;
  flex-direction: column;
  gap: 3px;
  padding: 4px;
  background: #15181d;
  border-radius: 8px;
}
.logo i { width: 10px; height: 10px; border-radius: 50%; display: block; }
.logo i:nth-child(1) { background: var(--red); }
.logo i:nth-child(2) { background: var(--yellow); }
.logo i:nth-child(3) { background: var(--green); }

#worlds { display: flex; gap: 6px; flex-wrap: wrap; }

.world-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 44px;
  padding: 4px 6px 3px;
  border: 2px solid transparent;
  border-radius: 12px;
  background: #3a3f48;
  color: #fff;
  cursor: pointer;
  transition: transform 0.1s, background 0.15s;
}
.world-btn:hover:not(:disabled) { transform: translateY(-1px); background: #464c57; }
.world-btn.active { border-color: var(--yellow); background: #4b515d; }
.world-btn.locked { opacity: 0.45; cursor: not-allowed; }
.world-btn .num { font-weight: 900; font-size: 16px; line-height: 1.1; }
.world-btn .stars { font-size: 10px; color: var(--yellow); letter-spacing: 1px; }

/* --- save-game code -------------------------------------------------------- */

.code-bar { display: none; margin-left: auto; align-items: center; gap: 8px; flex-wrap: wrap; }
body.has-cloud .code-bar { display: flex; }
.code-label { font-size: 13px; color: #c9ced8; font-weight: 700; }
.code-value {
  font-weight: 900;
  font-size: 17px;
  letter-spacing: 0.5px;
  background: #3a3f48;
  padding: 4px 10px;
  border-radius: 8px;
  user-select: all;
}
.code-status { font-size: 12px; color: #c9ced8; min-width: 70px; }
.code-status.ok::before { content: "✓ "; color: var(--green); }
.code-status.bad { color: #ff8a8a; }
.code-input {
  width: 100%;
  font: inherit;
  font-size: 20px;
  font-weight: 800;
  padding: 10px 12px;
  border: 2px solid var(--line);
  border-radius: 12px;
}
.code-input:focus { outline: none; border-color: var(--blue); }
.code-error { color: #c1121f; font-weight: 700; min-height: 1.2em; margin: 8px 0 0; }
.code-hint { color: var(--muted); font-size: 13px; }
#codeForm p { line-height: 1.45; }

/* --- layout ---------------------------------------------------------------- */

/* Desktop: the page is exactly one screen high. The program fills the full
   height on the left, the world scrolls on its own on the right. */
body {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.layout {
  --split: 45%;
  flex: 1;
  min-height: 0;
  display: flex;
  padding: 12px 16px 16px;
}

.panel {
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.panel.code {
  flex: 0 0 var(--split);
  min-width: 300px;
  display: flex;
  flex-direction: column;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px 8px 14px;
  border-bottom: 1px solid var(--line);
}
.panel-head h2 { margin: 0; font-size: 17px; font-weight: 800; white-space: nowrap; }
.head-actions { display: flex; gap: 0; flex-wrap: wrap; justify-content: flex-end; }
.head-actions .btn { padding: 5px 9px; white-space: nowrap; }

#blockly { flex: 1; min-height: 0; position: relative; }

.splitter {
  flex: 0 0 16px;
  cursor: col-resize;
  position: relative;
  touch-action: none;
}
.splitter::after {
  content: "";
  position: absolute;
  left: 6px;
  top: 50%;
  width: 4px;
  height: 56px;
  margin-top: -28px;
  border-radius: 2px;
  background: #c3c8d4;
  transition: background 0.15s, height 0.15s;
}
.splitter:hover::after, .splitter.dragging::after, .splitter:focus-visible::after { background: var(--blue); height: 90px; margin-top: -45px; }
.splitter:focus-visible { outline: none; }
body.resizing { cursor: col-resize; user-select: none; }

.panel.sim > * { flex-shrink: 0; }

.panel.sim {
  flex: 1;
  min-width: 320px;
  overflow-y: auto;
  scrollbar-gutter: stable;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* --- mission --------------------------------------------------------------- */

.mission-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.mission h1 { margin: 0; font-size: 22px; font-weight: 900; }
.mission .badge {
  background: var(--asphalt);
  color: #fff;
  font-weight: 800;
  font-size: 13px;
  padding: 3px 10px;
  border-radius: 999px;
}
.mission .best { color: #e0a800; font-size: 18px; letter-spacing: 2px; }
.mission .story { margin: 8px 0 6px; line-height: 1.45; color: #333a48; }
.mission .goal { margin: 0 0 6px; }
.tips summary { cursor: pointer; font-weight: 800; color: var(--blue); }
.tips ul { margin: 6px 0 0; padding-left: 20px; line-height: 1.45; color: #333a48; }

/* --- stage & controls ------------------------------------------------------ */

.stage {
  width: 100%;
  display: flex;
  justify-content: center;
  line-height: 0;
}
#canvas { display: block; border-radius: 12px; }

.controls { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

.btn {
  border: 0;
  border-radius: 999px;
  padding: 9px 18px;
  font-weight: 800;
  font-size: 15px;
  background: #e5e8ef;
  color: var(--ink);
  cursor: pointer;
  transition: transform 0.08s, filter 0.15s;
}
.btn:hover:not(:disabled) { filter: brightness(0.96); }
.btn:active:not(:disabled) { transform: scale(0.97); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.primary { background: var(--green); color: #fff; }
.btn.danger { background: var(--red); color: #fff; }
.btn.small { padding: 5px 12px; font-size: 13px; }
.btn.ghost { background: transparent; color: var(--muted); }
.btn.ghost:hover { background: #f0f2f6; }

.speed { margin-left: auto; display: inline-flex; background: #e5e8ef; border-radius: 999px; padding: 3px; }
.speed button {
  border: 0;
  background: transparent;
  padding: 5px 11px;
  border-radius: 999px;
  font-weight: 800;
  color: var(--muted);
  cursor: pointer;
}
.speed button.active { background: #fff; color: var(--ink); box-shadow: 0 1px 4px rgba(0, 0, 0, 0.12); }

.stats { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  background: #f1f3f8;
  border-radius: 999px;
  padding: 5px 11px;
  font-weight: 700;
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}
.chip.bad { background: #fde3e5; color: #a4161a; }

.legend { margin: 0; font-size: 13px; color: var(--muted); line-height: 1.45; }

body.is-running #blockly { outline: 3px solid rgba(43, 182, 115, 0.35); outline-offset: -3px; }

/* --- result dialog --------------------------------------------------------- */

.result {
  border: 0;
  border-radius: 20px;
  padding: 0;
  width: min(460px, calc(100vw - 32px));
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
.result::backdrop { background: rgba(20, 24, 32, 0.45); }
.result form { padding: 22px 24px 20px; }
.result h2 { margin: 0; text-align: center; font-size: 24px; font-weight: 900; }
.big-stars { text-align: center; font-size: 46px; color: #d5d9e2; letter-spacing: 6px; margin: 6px 0 4px; }
.big-stars .on { color: #ffc300; text-shadow: 0 2px 0 #e09b00; }
.checks, .numbers { list-style: none; margin: 10px 0; padding: 0; }
.checks li { padding: 3px 0; color: var(--muted); }
.checks li.ok { color: var(--ink); font-weight: 700; }
.numbers li { display: flex; justify-content: space-between; padding: 5px 0; border-bottom: 1px dashed var(--line); }
.numbers li.bad b { color: #c1121f; }
.hints { background: #fff7e0; border-radius: 12px; padding: 4px 12px; margin-top: 10px; }
.hints p { margin: 8px 0; line-height: 1.4; }
.actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; flex-wrap: wrap; }

/* --- toast ----------------------------------------------------------------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 30px);
  background: var(--asphalt);
  color: #fff;
  padding: 12px 20px;
  border-radius: 14px;
  font-weight: 700;
  max-width: calc(100vw - 32px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* --- narrow screens -------------------------------------------------------- */

@media (max-width: 900px) {
  body { height: auto; overflow: visible; }
  .layout { flex-direction: column; gap: 16px; padding: 12px 16px; }
  .splitter { display: none; }
  .panel.code { flex: none; min-width: 0; order: 2; height: 80vh; height: 80dvh; }
  .panel.sim { flex: none; min-width: 0; order: 1; overflow: visible; }
  .speed { margin-left: 0; }
}
