:root {
  --bg: #f7f4ea;
  --surface: #fffdf6;
  --surface-2: #edf4e7;
  --surface-3: #e4efe9;
  --ink: #172018;
  --muted: #5f6d62;
  --line: #cfd9cc;
  --line-strong: #8da083;
  --green: #2f6f3e;
  --green-dark: #1d3b13;
  --blue: #236d8c;
  --mango: #d58a27;
  --berry: #a7435d;
  --success: #237a4b;
  --danger: #b53939;
  --warning: #9a6a14;
  --cell: clamp(38px, 4vw, 52px);
  --radius: 8px;
  --shadow: 0 14px 28px rgba(38, 54, 37, .13);
  font-family: "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
}

* {
  box-sizing: border-box;
}

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

body {
  background:
    linear-gradient(90deg, rgba(47, 111, 62, .06) 1px, transparent 1px),
    linear-gradient(0deg, rgba(47, 111, 62, .05) 1px, transparent 1px),
    var(--bg);
  background-size: 34px 34px;
  color: var(--ink);
}

button,
summary {
  font: inherit;
}

button {
  color: inherit;
}

.app-shell {
  width: min(1480px, 100%);
  margin: 0 auto;
  padding: 18px;
}

.topbar {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 16px;
  align-items: center;
  margin-bottom: 16px;
}

.brand-mark {
  width: 66px;
  aspect-ratio: 1;
  border: 2px solid var(--green-dark);
  border-radius: var(--radius);
  background: var(--surface);
  display: grid;
  place-items: center;
  position: relative;
  box-shadow: var(--shadow);
}

.track {
  position: absolute;
  height: 9px;
  width: 44px;
  border-radius: 99px;
  background: repeating-linear-gradient(90deg, var(--green-dark), var(--green-dark) 7px, #bdddc4 7px, #bdddc4 14px);
}

.track-a {
  transform: translateY(-15px);
}

.track-b {
  transform: rotate(90deg);
}

.track-c {
  transform: translateY(15px);
  background: repeating-linear-gradient(90deg, var(--blue), var(--blue) 7px, #cde8f2 7px, #cde8f2 14px);
}

.title-block h1,
.title-block p,
.panel h2,
.panel h3,
.panel h4,
.mission-panel p {
  margin-top: 0;
}

.eyebrow,
.panel-kicker {
  margin: 0 0 4px;
  color: var(--green);
  font-size: .78rem;
  font-weight: 800;
  letter-spacing: .08em;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: clamp(2rem, 4vw, 3.7rem);
  line-height: 1;
  letter-spacing: 0;
}

h2 {
  margin-bottom: 8px;
  font-size: 1.22rem;
  line-height: 1.15;
}

h3 {
  margin-bottom: 8px;
  font-size: 1rem;
}

h4 {
  margin-bottom: 6px;
  font-size: .82rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .06em;
}

.lead,
.mission-brief,
.small-text,
li {
  color: var(--muted);
  line-height: 1.45;
}

.lead {
  margin: 6px 0 0;
  max-width: 64ch;
  font-size: 1.02rem;
}

.privacy-note {
  max-width: 230px;
  color: var(--muted);
  font-size: .88rem;
  text-align: right;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(300px, 360px);
  gap: 14px;
  align-items: start;
}

.levels-panel {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: minmax(220px, 320px) minmax(420px, 1fr);
  gap: 12px 16px;
  align-items: end;
}

.levels-panel .panel-heading {
  grid-column: 1;
  margin-bottom: 0;
}

.levels-panel .field-label {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.levels-panel .level-select {
  grid-column: 1;
  grid-row: 2;
}

.levels-panel .level-list {
  grid-column: 2;
  grid-row: 1 / span 3;
}

.levels-panel .level-actions {
  grid-column: 1;
  grid-row: 3;
}

.tools-panel {
  align-self: start;
}

.panel {
  background: color-mix(in srgb, var(--surface) 92%, white);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
}

.panel-heading {
  margin-bottom: 12px;
}

.stage {
  display: grid;
  gap: 12px;
  min-width: 0;
}

.mission-panel {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 14px;
  align-items: start;
}

.mission-brief {
  margin-bottom: 0;
}

.tool-list,
.target-list,
.hint-list {
  display: grid;
  gap: 8px;
}

.level-list.compact {
  display: grid;
  grid-template-columns: repeat(10, minmax(0, 1fr));
  gap: 6px;
  margin-top: 0;
}

.level-btn,
.tool-btn,
.btn {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: pointer;
  transition: border-color .15s ease, background .15s ease, transform .08s ease, opacity .15s ease;
}

.level-btn:hover,
.tool-btn:hover,
.btn:hover {
  border-color: var(--line-strong);
  background: #f8fbf4;
}

.level-btn:focus-visible,
.tool-btn:focus-visible,
.btn:focus-visible,
.cell:focus-visible,
summary:focus-visible {
  outline: 3px solid rgba(35, 109, 140, .35);
  outline-offset: 2px;
}

.level-btn:active,
.tool-btn:active,
.btn:active {
  transform: translateY(1px);
}

.level-btn {
  width: 100%;
  min-height: 42px;
  padding: 6px;
  text-align: center;
  font-weight: 900;
}

.level-btn.active {
  border-color: var(--green);
  background: var(--surface-2);
  box-shadow: inset 0 -4px 0 var(--green);
}

.level-btn.completed {
  border-color: rgba(35, 122, 75, .45);
}

.level-btn.locked {
  color: var(--muted);
  background: #f1eee4;
  cursor: not-allowed;
  opacity: .72;
}

.level-index {
  display: inline-grid;
  place-items: center;
  width: 30px;
  height: 30px;
  margin: 0 auto;
  border-radius: 50%;
  background: var(--green-dark);
  color: white;
  font-weight: 900;
  font-size: .86rem;
}

.level-btn.completed .level-index {
  background: var(--success);
}

.level-btn.locked .level-index {
  background: #8a8f86;
}

.field-label {
  display: block;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: .82rem;
  font-weight: 900;
  letter-spacing: .04em;
  text-transform: uppercase;
}

.level-select {
  width: 100%;
  min-height: 48px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  padding: 10px 12px;
  font: inherit;
  font-weight: 800;
}

.level-select:focus-visible {
  outline: 3px solid rgba(35, 109, 140, .35);
  outline-offset: 2px;
}

.rating-cell {
  min-height: 34px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--muted);
  font-weight: 950;
}

.rating-cell.gold {
  background: rgba(35, 122, 75, .13);
  border-color: rgba(35, 122, 75, .6);
  color: var(--success);
}

.rating-cell.locked {
  background: #eeeadd;
}

.level-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 0;
}

.level-actions .btn.full {
  margin-top: 0;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  border: 1px solid var(--line);
  border-radius: 99px;
  padding: 4px 9px;
  background: var(--surface);
  font-size: .84rem;
  font-weight: 700;
}

.chip.done {
  color: var(--success);
  border-color: rgba(35, 122, 75, .5);
  background: rgba(35, 122, 75, .08);
}

.chip.bad {
  color: var(--danger);
  border-color: rgba(181, 57, 57, .55);
  background: rgba(181, 57, 57, .08);
}

.chip.red {
  color: #8d2639;
  border-color: rgba(167, 67, 93, .5);
  background: rgba(167, 67, 93, .11);
}

.chip.blue {
  color: #165a77;
  border-color: rgba(35, 109, 140, .5);
  background: rgba(35, 109, 140, .11);
}

.control-strip {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
}

.btn {
  min-height: 44px;
  padding: 9px 10px;
  font-weight: 800;
  touch-action: manipulation;
}

.btn span {
  display: inline-block;
  min-width: 1.2em;
}

.btn.primary {
  border-color: var(--green-dark);
  background: var(--green-dark);
  color: white;
}

.btn.primary:hover {
  background: #28511d;
}

.btn.ghost {
  background: transparent;
}

.btn.full {
  width: 100%;
  margin-top: 8px;
}

.btn:disabled {
  cursor: not-allowed;
  opacity: .48;
  transform: none;
}

.board-panel {
  overflow: auto;
  padding: 12px;
  background:
    linear-gradient(135deg, rgba(189, 221, 196, .28), rgba(255, 253, 246, .9)),
    var(--surface);
}

.grid {
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell));
  grid-template-rows: repeat(var(--rows), var(--cell));
  gap: 5px;
  width: max-content;
  margin: auto;
}

.cell {
  width: var(--cell);
  height: var(--cell);
  position: relative;
  display: grid;
  place-items: center;
  overflow: hidden;
  border: 1px solid #b9c7b7;
  border-radius: var(--radius);
  background: #fbfaf4;
  color: var(--ink);
  cursor: pointer;
  padding: 0;
  touch-action: manipulation;
}

.cell:hover {
  border-color: var(--blue);
}

.cell.fixed {
  cursor: default;
}

.cell.source {
  background: #dcefd7;
  border-color: rgba(47, 111, 62, .55);
}

.cell.output {
  background: #d9edf4;
  border-color: rgba(35, 109, 140, .55);
}

.cell.wall {
  background:
    repeating-linear-gradient(135deg, #c3c9bd, #c3c9bd 7px, #e1e4dc 7px, #e1e4dc 14px);
}

.cell.fail-cell {
  animation: failPulse .6s ease 2;
}

@keyframes failPulse {
  50% {
    border-color: var(--danger);
    background: rgba(181, 57, 57, .15);
  }
}

.fixed-label {
  position: absolute;
  top: 4px;
  left: 5px;
  color: rgba(23, 32, 24, .62);
  font-size: .5rem;
  font-weight: 900;
  letter-spacing: .04em;
  text-transform: uppercase;
}

.fixed-main {
  display: grid;
  gap: 2px;
  place-items: center;
  font-weight: 900;
  color: var(--green-dark);
  font-size: clamp(.72rem, 1.1vw, .9rem);
  line-height: 1.05;
  transform: translateY(4px);
}

.output .fixed-main {
  color: var(--blue);
}

.tile {
  width: calc(var(--cell) - 12px);
  height: calc(var(--cell) - 12px);
  display: grid;
  place-items: center;
  border-radius: 7px;
  color: white;
  font-size: clamp(.82rem, 1.5vw, 1.2rem);
  font-weight: 950;
  text-align: center;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.22);
}

.tile.belt {
  background: var(--blue);
  font-size: clamp(1.35rem, 2.2vw, 1.9rem);
}

.tile.math {
  background: var(--green);
}

.tile.logic {
  background: var(--mango);
  color: #241704;
  font-size: clamp(.72rem, 1.2vw, .94rem);
}

.tile.paint-red,
.tool-icon.paint-red {
  background: var(--berry);
}

.tile.paint-blue,
.tool-icon.paint-blue {
  background: var(--blue);
}

.tile.trash {
  background: var(--berry);
}

.packet {
  position: absolute;
  right: 5px;
  bottom: 5px;
  width: calc(var(--cell) * .43);
  height: calc(var(--cell) * .43);
  min-width: 22px;
  min-height: 22px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: #f4c34f;
  color: #241704;
  border: 2px solid white;
  box-shadow: 0 6px 12px rgba(23, 32, 24, .25);
  font-size: clamp(.75rem, 1.4vw, 1rem);
  font-weight: 950;
  z-index: 4;
}

.packet.red {
  background: #c84c63;
  color: #fffdf6;
}

.packet.blue {
  background: #3b8bb0;
  color: #fffdf6;
}

.status-row {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.stat {
  min-height: 58px;
  display: grid;
  align-content: center;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 10px;
  background: var(--surface);
}

.stat strong {
  font-size: 1.25rem;
  line-height: 1;
}

.stat span {
  color: var(--muted);
  font-size: .78rem;
}

.message {
  min-height: 50px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
  background: var(--surface);
  color: var(--muted);
  line-height: 1.4;
}

.message.good {
  border-color: rgba(35, 122, 75, .45);
  background: rgba(35, 122, 75, .08);
  color: var(--success);
}

.message.bad {
  border-color: rgba(181, 57, 57, .45);
  background: rgba(181, 57, 57, .08);
  color: var(--danger);
}

.message.warn {
  border-color: rgba(154, 106, 20, .45);
  background: rgba(154, 106, 20, .09);
  color: var(--warning);
}

.tool-btn {
  width: 100%;
  min-height: 62px;
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr);
  gap: 9px;
  align-items: center;
  padding: 8px;
  text-align: left;
}

.tool-btn.active {
  border-color: var(--blue);
  background: #edf8fb;
  box-shadow: inset 4px 0 0 var(--blue);
}

.tool-icon {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border-radius: 7px;
  color: white;
  font-weight: 950;
}

.tool-icon.belt {
  background: var(--blue);
}

.tool-icon.math {
  background: var(--green);
}

.tool-icon.logic {
  background: var(--mango);
  color: #241704;
}

.tool-icon.trash {
  background: var(--berry);
}

.tool-copy strong {
  display: block;
}

.tool-copy span {
  color: var(--muted);
  display: block;
  font-size: .78rem;
  line-height: 1.25;
}

.task-card {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.task-section {
  margin-top: 10px;
}

.target-row {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 9px;
  background: #fbfaf4;
}

.target-row strong {
  display: block;
  margin-bottom: 6px;
}

.hint-list {
  margin: 8px 0;
}

.hint {
  border-left: 4px solid var(--mango);
  padding: 8px 9px;
  background: rgba(213, 138, 39, .09);
  color: #5d4219;
}

.teacher-card {
  margin-top: 14px;
  border-top: 1px solid var(--line);
  padding-top: 12px;
}

.teacher-card summary {
  cursor: pointer;
  font-weight: 900;
}

.teacher-card ul {
  padding-left: 18px;
}

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

.dialog {
  position: fixed;
  inset: 0;
  display: none;
  place-items: center;
  padding: 18px;
  background: rgba(23, 32, 24, .38);
  z-index: 20;
}

.dialog.show {
  display: grid;
}

.dialog-box {
  width: min(560px, 100%);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: 0 22px 55px rgba(23, 32, 24, .28);
  padding: 18px;
}

.dialog-box h2 {
  margin-top: 0;
}

.dialog-questions {
  display: grid;
  gap: 8px;
  margin: 12px 0;
}

.dialog-question {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 9px;
  background: var(--surface-2);
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  flex-wrap: wrap;
}

.certificate-dialog {
  background: rgba(23, 32, 24, .58);
}

.certificate-box {
  width: min(760px, 100%);
  border: 6px double var(--green-dark);
  border-radius: var(--radius);
  background:
    linear-gradient(135deg, rgba(189, 221, 196, .34), rgba(255, 253, 246, .96)),
    var(--surface);
  box-shadow: 0 28px 70px rgba(23, 32, 24, .34);
  padding: clamp(18px, 4vw, 34px);
}

.certificate-box h2 {
  margin: 0 0 10px;
  font-size: clamp(2rem, 5vw, 3.1rem);
  line-height: 1;
}

.certificate-lead {
  max-width: 62ch;
  color: var(--muted);
  line-height: 1.45;
}

.certificate-summary {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  margin: 16px 0;
}

.certificate-stat,
.certificate-result {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 253, 246, .78);
  padding: 10px;
}

.certificate-stat strong {
  display: block;
  font-size: 1.45rem;
}

.certificate-stat span {
  color: var(--muted);
  font-size: .84rem;
}

.certificate-results {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.certificate-result {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
}

.certificate-result .rating-cell {
  width: 36px;
  min-height: 34px;
}

.certificate-note {
  color: var(--muted);
  margin: 14px 0 0;
}

@media (max-width: 1180px) {
  .workspace {
    grid-template-columns: minmax(0, 1fr) minmax(290px, 330px);
  }

  .levels-panel {
    grid-column: 1 / -1;
    grid-template-columns: minmax(220px, 300px) minmax(260px, 1fr);
  }

  .levels-panel .level-actions {
    grid-column: 1;
    grid-row: 3;
  }

  .levels-panel .level-list {
    grid-column: 2;
    grid-row: 1 / span 3;
  }

  .tool-list {
    grid-template-columns: 1fr;
  }
}

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

  .topbar,
  .workspace,
  .mission-panel,
  .levels-panel {
    grid-template-columns: 1fr;
  }

  .brand-mark {
    display: none;
  }

  .privacy-note {
    max-width: none;
    text-align: left;
  }

  .levels-panel .panel-heading,
  .levels-panel .level-select,
  .levels-panel .level-list,
  .levels-panel .level-actions {
    grid-column: 1;
    grid-row: auto;
  }

  .level-list.compact {
    grid-template-columns: repeat(5, minmax(0, 1fr));
  }

  .control-strip,
  .status-row {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .control-strip .primary {
    grid-column: span 2;
  }

  .tool-list {
    grid-template-columns: 1fr;
  }

  .certificate-summary,
  .certificate-results {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 520px) {
  :root {
    --cell: 40px;
  }

  .grid {
    gap: 3px;
  }

  .cell {
    border-radius: 6px;
  }

  .tile {
    width: calc(var(--cell) - 8px);
    height: calc(var(--cell) - 8px);
  }
}
