/* Общие стили «Робо-лабиринта»: цвета, шрифты, поле, роботы, блоки программы.
   Идея: тетрадь в клетку, цвета команд, блоки как в «Часе кода». */
:root {
  --paper: #ECF1F6; --paper-line: rgba(29, 74, 130, .075);
  --surface: #FFFFFF; --surface-2: #F4F7FA;
  --ink: #142030; --muted: #5A6A7D; --line: #D3DCE6;
  --tile: #FAF4E4; --tile-edge: #E6DABC;
  --wall: #2F4A59; --wall-hi: #42667A; --wall-lo: #22394A;
  --pole: #2A3442;
  --red: #D93A40; --red-soft: #FCE6E6; --red-ink: #9E1D22;
  --blue: #1E67DC; --blue-soft: #E2ECFC; --blue-ink: #0F4296;
  --green: #1C9A52; --green-soft: #DFF5E8; --green-ink: #0D6334;
  --yellow: #E8AE00; --yellow-soft: #FFF3CC; --yellow-ink: #7A5600;
  --purple: #8A4FD8; --purple-soft: #EFE5FC; --purple-ink: #5B2A9E;
  --teal: #0E9AA7; --teal-soft: #DDF4F6; --teal-ink: #066972;
  --mv: #17894F; --tn: #6F44CC; --lp: #B86E00; --lp-soft: rgba(201, 122, 0, .10);
  --gold: #F2B300; --ok: #17894F; --bad: #D2393A;
  --code-bg: #142030; --code-fg: #E4EBF3; --code-mv: #74E0A6; --code-tn: #C3A7FF; --code-lp: #FFC768; --code-n: #8CC8FF;
  --shadow: 0 1px 0 rgba(20, 32, 48, .04), 0 8px 22px rgba(20, 32, 48, .07);
  --font-display: "Unbounded", "Arial Black", system-ui, sans-serif;
  --font-body: "Golos Text", "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  --font-mono: "JetBrains Mono", Consolas, "Courier New", monospace;
  --radius: 16px;
  --dur: 340ms;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #0C141E; --paper-line: rgba(140, 180, 230, .055);
    --surface: #141F2D; --surface-2: #192737;
    --ink: #E4EBF3; --muted: #91A2B6; --line: #26374B;
    --tile: #1D2A39; --tile-edge: #15202C;
    --wall: #3B5568; --wall-hi: #55768C; --wall-lo: #2C4253;
    --pole: #D6E0EA;
    --red: #F0565B; --red-soft: #3A1C23; --red-ink: #FFA3A6;
    --blue: #4A8CFF; --blue-soft: #15284A; --blue-ink: #A5C6FF;
    --green: #2BB673; --green-soft: #12301F; --green-ink: #8BE3B2;
    --yellow: #FFC83A; --yellow-soft: #3A2E0C; --yellow-ink: #FFDF8A;
    --purple: #A57BF0; --purple-soft: #2A1C44; --purple-ink: #D4BEFF;
    --teal: #2CC3CF; --teal-soft: #0F3236; --teal-ink: #8FE6EC;
    --lp-soft: rgba(240, 160, 40, .12);
    --gold: #FFC83A; --ok: #2BB673; --bad: #F05A5A;
    --code-bg: #0A1119;
    --shadow: 0 1px 0 rgba(0, 0, 0, .2), 0 10px 26px rgba(0, 0, 0, .28);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --paper: #0C141E; --paper-line: rgba(140, 180, 230, .055);
  --surface: #141F2D; --surface-2: #192737;
  --ink: #E4EBF3; --muted: #91A2B6; --line: #26374B;
  --tile: #1D2A39; --tile-edge: #15202C;
  --wall: #3B5568; --wall-hi: #55768C; --wall-lo: #2C4253;
  --pole: #D6E0EA;
  --red: #F0565B; --red-soft: #3A1C23; --red-ink: #FFA3A6;
  --blue: #4A8CFF; --blue-soft: #15284A; --blue-ink: #A5C6FF;
  --green: #2BB673; --green-soft: #12301F; --green-ink: #8BE3B2;
  --yellow: #FFC83A; --yellow-soft: #3A2E0C; --yellow-ink: #FFDF8A;
  --purple: #A57BF0; --purple-soft: #2A1C44; --purple-ink: #D4BEFF;
  --teal: #2CC3CF; --teal-soft: #0F3236; --teal-ink: #8FE6EC;
  --lp-soft: rgba(240, 160, 40, .12);
  --gold: #FFC83A; --ok: #2BB673; --bad: #F05A5A;
  --code-bg: #0A1119;
  --shadow: 0 1px 0 rgba(0, 0, 0, .2), 0 10px 26px rgba(0, 0, 0, .28);
  color-scheme: dark;
}

.team-red { --team: var(--red); --team-soft: var(--red-soft); --team-ink: var(--red-ink); --team-on: #fff; }
.team-blue { --team: var(--blue); --team-soft: var(--blue-soft); --team-ink: var(--blue-ink); --team-on: #fff; }
.team-green { --team: var(--green); --team-soft: var(--green-soft); --team-ink: var(--green-ink); --team-on: #fff; }
.team-yellow { --team: var(--yellow); --team-soft: var(--yellow-soft); --team-ink: var(--yellow-ink); --team-on: #2A1E00; }
.team-purple { --team: var(--purple); --team-soft: var(--purple-soft); --team-ink: var(--purple-ink); --team-on: #fff; }
.team-teal { --team: var(--teal); --team-soft: var(--teal-soft); --team-ink: var(--teal-ink); --team-on: #fff; }

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; height: 100%; }
body {
  margin: 0; min-height: 100%;
  background-color: var(--paper);
  background-image:
    linear-gradient(var(--paper-line) 1px, transparent 1px),
    linear-gradient(90deg, var(--paper-line) 1px, transparent 1px);
  background-size: 28px 28px;
  color: var(--ink);
  font: 400 15px/1.5 var(--font-body);
}
h1, h2, h3, p { margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
:focus-visible { outline: 3px solid var(--lp); outline-offset: 2px; }
.wrap {
  width: 100%; margin-inline: auto;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.eyebrow { font: 700 12px/1.2 var(--font-mono); letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.muted { color: var(--muted); }
.brand { font: 800 16px var(--font-display); letter-spacing: -.02em; white-space: nowrap; }
.t-red { color: var(--red); } .t-blue { color: var(--blue); }

/* ---------- кнопки ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: 600 14px/1.2 var(--font-body); padding: 9px 14px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink); cursor: pointer;
  transition: background-color .15s, transform .1s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: default; transform: none; }
.btn-small { padding: 7px 12px; font-size: 13px; }
.btn-primary { background: var(--ink); color: var(--surface); border-color: transparent; }
.btn-primary:hover { background: var(--ink); opacity: .9; }
.btn[data-armed] { background: var(--bad); color: #fff; border-color: transparent; }
.btn-start {
  font: 800 18px/1 var(--font-display); padding: 18px 30px; border-radius: 14px; border: 0; cursor: pointer;
  background: var(--ink); color: var(--surface); box-shadow: 0 4px 0 rgba(0, 0, 0, .28);
}
.btn-start:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0, 0, 0, .28); }
.btn-start:disabled { opacity: .4; cursor: default; transform: none; }
.seg { display: inline-flex; padding: 3px; gap: 2px; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--line); }
.seg button { border: 0; background: transparent; padding: 5px 10px; border-radius: 7px; font-size: 13px; font-weight: 600; color: var(--muted); cursor: pointer; }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(0, 0, 0, .12); }

/* ---------- табло и уровень ---------- */
.sc {
  display: flex; align-items: center; gap: 8px; padding: 4px 14px 4px 5px; border-radius: 999px; min-width: 0;
  background: var(--team-soft); color: var(--team-ink);
}
.sc.lead { box-shadow: inset 0 0 0 2px var(--team); }
.sc-m { display: grid; place-items: center; width: 30px; height: 30px; border-radius: 50%; background: var(--surface); font-size: 18px; line-height: 1; flex: none; }
.sc-name { font-weight: 700; max-width: 14ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sc-pts { font: 800 19px/1 var(--font-display); font-variant-numeric: tabular-nums; }
.dots { display: inline-flex; gap: 4px; }
.dot { width: 16px; height: 6px; border-radius: 3px; background: var(--line); }
.dot.done { background: var(--ink); }
.dot.cur { background: var(--lp); }
.new-chip { font: 700 11px var(--font-mono); letter-spacing: .06em; text-transform: uppercase; padding: 3px 8px; border-radius: 6px; background: var(--lp); color: #fff; }

/* ---------- поле ---------- */
.board-box { position: relative; }
.board {
  position: relative; display: grid; width: 100%;
  grid-template-columns: repeat(var(--cols), 1fr); grid-template-rows: repeat(var(--rows), 1fr);
  aspect-ratio: var(--cols) / var(--rows);
  border: 4px solid var(--wall); border-radius: 14px; overflow: hidden; background: var(--wall);
  container-type: inline-size;
}
.cell { position: relative; }
.cell.floor { background: var(--tile); box-shadow: inset 0 0 0 1px var(--tile-edge); }
.cell.wall { background: var(--wall); }
.cell.wall::after { content: ""; position: absolute; inset: 10%; border-radius: 24%; background: var(--wall-hi); box-shadow: inset 0 -4px 0 var(--wall-lo); }
.cell.wall.v1::after { inset: 14%; border-radius: 50%; }
.cell.wall.v2::after { inset: 20% 10% 12%; border-radius: 34% 34% 20% 20%; }
.cell.start::before, .cell.goal::before { content: ""; position: absolute; inset: 16%; border-radius: 50%; border: 2px dashed var(--muted); opacity: .45; }
.cell.goal::before { border-color: var(--ok); opacity: .8; inset: 8%; }
.cell svg { position: absolute; inset: 16%; width: 68%; height: 68%; }
.star { position: absolute; inset: 0; z-index: 1; }
.star svg { inset: 22%; width: 56%; height: 56%; }
.star.got { transform: scale(1.7); opacity: 0; transition: transform .35s ease calc(var(--dur) * .55), opacity .35s ease calc(var(--dur) * .55); }
.star.pulse svg { animation: starpulse .7s ease; filter: drop-shadow(0 0 6px var(--pulse, var(--gold))); }
@keyframes starpulse { 40% { transform: scale(1.45) rotate(18deg); } }
.traces { position: absolute; inset: 0; width: 100%; height: 100%; z-index: 2; pointer-events: none; overflow: visible; }
.trace polyline { fill: none; stroke: var(--team); stroke-width: 5px; vector-effect: non-scaling-stroke; stroke-linecap: round; stroke-linejoin: round; opacity: .9; }
.trace circle { fill: var(--team); opacity: .9; }
.trace.failed circle { fill: var(--bad); }
.bot {
  position: absolute; z-index: 3; pointer-events: none;
  width: calc(var(--bs) * 100% / var(--cols)); height: calc(var(--bs) * 100% / var(--rows));
  left: calc((var(--x, 0) + .5 + var(--ox, 0)) * 100% / var(--cols));
  top: calc((var(--y, 0) + .5 + var(--oy, 0)) * 100% / var(--rows));
  transform: translate(-50%, -50%);
  transition: left var(--dur) cubic-bezier(.45, 0, .3, 1), top var(--dur) cubic-bezier(.45, 0, .3, 1);
}
.bot-body {
  position: absolute; inset: 14%; border-radius: 50%;
  background: var(--team-soft); border: 3px solid var(--team); box-shadow: 0 3px 0 rgba(0, 0, 0, .22);
  transform: rotate(var(--rot, 90deg)); transition: transform var(--dur) ease, border-color .2s;
}
.bot-body::before {
  content: ""; position: absolute; left: 50%; top: -34%; width: 58%; height: 32%;
  transform: translateX(-50%); background: var(--team); clip-path: polygon(50% 0, 100% 100%, 0 100%);
}
.bot-face { position: absolute; inset: 0; display: grid; place-items: center; line-height: 1; font-size: 20px; font-size: calc(var(--bs) * 46cqw / var(--cols)); }
.bot-tag {
  position: absolute; right: 2%; bottom: 2%; min-width: 42%; height: 42%; padding: 0 4%;
  display: grid; place-items: center; border-radius: 999px; background: var(--team); color: var(--team-on);
  font: 800 12px/1 var(--font-body); font-size: max(9px, calc(var(--bs) * 30cqw / var(--cols))); box-shadow: 0 0 0 2px var(--surface);
}
.board.crowded .trace polyline { stroke-width: 3px; }
.bot.bump { animation: botbump var(--dur) ease; }
@keyframes botbump { 45% { transform: translate(calc(-50% + var(--bx) * 38%), calc(-50% + var(--by) * 38%)); } }
.bot.crashed .bot-body { border-color: var(--bad); }
.bot.crashed .bot-body::before { background: var(--bad); }
.bot.crashed .bot-face { animation: wobble .5s ease; }
.bot.won .bot-face { animation: hop .5s ease 3; }
@keyframes wobble { 25% { transform: rotate(-14deg); } 75% { transform: rotate(14deg); } }
@keyframes hop { 50% { transform: translateY(-22%) scale(1.12); } }
.no-anim, .no-anim * { transition: none !important; }

/* ---------- блоки и программа ---------- */
.palette { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; }
.pal {
  display: flex; align-items: center; gap: 8px; padding: 11px 12px; border: 0; border-radius: 11px; cursor: pointer;
  background: var(--c); color: #fff; font: 600 15px/1.1 var(--font-body);
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, .22); transition: transform .08s, filter .15s;
  touch-action: manipulation;
}
.pal:hover { filter: brightness(1.08); }
.pal:active { transform: translateY(2px); box-shadow: inset 0 -1px 0 rgba(0, 0, 0, .22); }
.pal:disabled { opacity: .4; cursor: default; }
.pal-fwd, .blk-fwd { --c: var(--mv); }
.pal-left, .pal-right, .blk-left, .blk-right { --c: var(--tn); }
.pal-rep, .blk-rep { --c: var(--lp); }
.ico { display: inline-grid; place-items: center; flex: none; }
.ico svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 2.6; stroke-linecap: round; stroke-linejoin: round; }
.prog-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.ph-title { border: 0; background: none; padding: 0; cursor: pointer; font: 700 11px var(--font-mono); letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.ph-title span { color: var(--ink); }
.ph-code { border: 1px solid var(--line); background: var(--surface); border-radius: 7px; padding: 4px 9px; font: 700 11px var(--font-mono); color: var(--muted); cursor: pointer; }
.ph-code[aria-pressed="true"] { background: var(--code-bg); color: var(--code-fg); border-color: transparent; }
.program {
  position: relative; min-height: 130px; overflow: auto;
  display: flex; flex-direction: column; gap: 5px; padding: 8px;
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 12px; cursor: text;
}
.prog-empty { color: var(--muted); font-size: 13.5px; padding: 6px 4px; }
.blk {
  position: relative; display: flex; align-items: center; gap: 7px; min-height: 40px; padding: 4px 4px 4px 10px;
  background: var(--c); color: #fff; border-radius: 9px; font-weight: 600; font-size: 15px;
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, .18); cursor: pointer; user-select: none; -webkit-user-select: none;
  touch-action: manipulation;
}
.blk .lbl { white-space: nowrap; }
.blk.is-error { outline: 3px solid var(--bad); outline-offset: 1px; animation: shake .4s ease; }
@keyframes shake { 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }
.step { display: inline-flex; align-items: center; border-radius: 7px; background: rgba(255, 255, 255, .2); }
.step button { width: 32px; height: 32px; border: 0; border-radius: 6px; background: transparent; color: #fff; cursor: pointer; font: 700 17px/1 var(--font-mono); touch-action: manipulation; }
.step button:hover { background: rgba(0, 0, 0, .18); }
.step button:disabled { opacity: .35; }
.step b { min-width: 18px; text-align: center; font: 700 15px var(--font-mono); }
.x { margin-left: auto; width: 32px; height: 32px; flex: none; border: 0; border-radius: 6px; background: transparent; color: rgba(255, 255, 255, .85); cursor: pointer; font-size: 20px; line-height: 1; }
.x:hover { background: rgba(0, 0, 0, .2); color: #fff; }
.rep { display: flex; flex-direction: column; }
.rep-body {
  display: flex; flex-direction: column; gap: 5px; min-height: 36px;
  margin-left: 12px; padding: 6px 0 6px 8px; border-left: 4px solid var(--lp); border-radius: 0 0 0 6px;
}
.rep-body.is-target { background: var(--lp-soft); outline: 2px dashed var(--lp); outline-offset: -1px; border-radius: 0 8px 8px 6px; padding-right: 6px; }
.rep-empty { font-size: 12.5px; color: var(--muted); padding: 4px 2px; }
.rep-exit { align-self: flex-start; border: 0; background: none; padding: 4px 0; cursor: pointer; font: 700 12px var(--font-mono); color: var(--lp); }
.caret { position: relative; height: 3px; margin: 2px 0; border-radius: 2px; background: var(--team, var(--ink)); flex: none; }
.caret::after {
  content: "сюда"; position: absolute; right: 0; top: 50%; transform: translateY(-50%);
  font: 700 10px var(--font-mono); color: var(--team-ink, var(--ink)); background: var(--surface-2); padding: 0 4px;
}
.code-view {
  margin: 0; max-height: 200px; overflow: auto; padding: 10px 12px; border-radius: 10px;
  background: var(--code-bg); color: var(--code-fg); font: 500 13px/1.6 var(--font-mono); white-space: pre;
}
.k-mv { color: var(--code-mv); } .k-tn { color: var(--code-tn); } .k-lp { color: var(--code-lp); } .k-n { color: var(--code-n); }
.locked .step button, .locked .x { visibility: hidden; }
.locked .program { cursor: default; }

.stats { display: flex; flex-wrap: wrap; gap: 6px 18px; margin: 0; font-size: 13px; color: var(--muted); }
.stats div { display: flex; gap: 6px; align-items: baseline; }
.stats dt { margin: 0; }
.stats dd { margin: 0; font: 700 14px var(--font-mono); color: var(--ink); font-variant-numeric: tabular-nums; }
.stats dd.good { color: var(--ok); }
.stats dd.bad { color: var(--bad); }

.bd { list-style: none; margin: 0; padding: 0; display: grid; gap: 3px; font-size: 13.5px; }
.bd li { display: flex; justify-content: space-between; gap: 12px; }
.bd b { font: 700 13px var(--font-mono); font-variant-numeric: tabular-nums; }
.bd b.neg { color: var(--bad); }

/* конфетти */
.cf { position: absolute; left: 50%; top: 45%; width: 8px; height: 12px; border-radius: 2px; z-index: 60; pointer-events: none; animation: cf 1.3s cubic-bezier(.2, .6, .4, 1) forwards; }
@keyframes cf {
  0% { transform: translate(-50%, -50%) rotate(0); }
  40% { transform: translate(calc(-50% + var(--dx) * .6), calc(-50% + var(--dy))) rotate(calc(var(--r) * .4)); opacity: 1; }
  100% { transform: translate(calc(-50% + var(--dx)), calc(-50% + var(--dy) + 180px)) rotate(var(--r)); opacity: 0; }
}

.conn {
  position: fixed; left: 50%; bottom: max(16px, env(safe-area-inset-bottom, 0px)); transform: translateX(-50%); z-index: 90;
  padding: 10px 16px; border-radius: 12px; background: var(--bad); color: #fff; font-weight: 600; font-size: 14px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .3); max-width: calc(100% - 32px); text-align: center;
}
@media (prefers-reduced-motion: reduce) {
  .bot.won .bot-face, .bot.crashed .bot-face, .blk.is-error, .star.pulse svg { animation: none; }
}
