:root {
  --paper: #fbf4e8;
  --paper-deep: #f0dfc5;
  --ink: #24313a;
  --muted: #65717a;
  --line: #d8c7aa;
  --white: #fffdf8;
  --coral: #e85f4f;
  --coral-deep: #b93c32;
  --mint: #3aa17e;
  --mint-deep: #247057;
  --sky: #3974c5;
  --sky-deep: #24518d;
  --yellow: #f2be3e;
  --violet: #7b5cc9;
  --shadow: 0 12px 28px rgba(67, 47, 24, 0.12);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
}

body {
  min-height: 100%;
  margin: 0;
  color: var(--ink);
  background:
    linear-gradient(90deg, rgba(216, 199, 170, 0.22) 1px, transparent 1px),
    linear-gradient(0deg, rgba(216, 199, 170, 0.22) 1px, transparent 1px),
    var(--paper);
  background-size: 28px 28px;
  font-family: ui-rounded, "Arial Rounded MT Bold", "Trebuchet MS", Arial, sans-serif;
}

button,
a {
  touch-action: manipulation;
}

button:focus-visible,
a:focus-visible {
  outline: 4px solid rgba(57, 116, 197, 0.35);
  outline-offset: 3px;
}

.app-shell {
  width: min(1180px, calc(100% - 32px));
  min-height: 100vh;
  margin: 0 auto;
  padding: 18px 0 36px;
}

.top-bar,
.game-header,
.chapter-card-top,
.chapter-card-bottom,
.level-meta,
.tips-header,
.action-row,
.top-actions {
  display: flex;
  align-items: center;
}

.top-bar {
  justify-content: space-between;
  gap: 16px;
  min-height: 64px;
}

.brand-mark {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--ink);
  text-decoration: none;
}

.brand-name {
  font-size: 1.35rem;
  font-weight: 900;
}

.brand-shape {
  display: inline-block;
  width: 22px;
  height: 22px;
  box-shadow: 0 3px 0 rgba(36, 49, 58, 0.18);
}

.shape-square {
  border-radius: 5px;
  background: var(--coral);
  transform: rotate(-5deg);
}

.shape-circle {
  border-radius: 999px;
  background: var(--yellow);
}

.top-actions {
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 10px;
}

.icon-button,
.primary-button,
.secondary-button,
.tip-button,
.palette-button,
.command-token,
.level-strip button {
  border: 0;
  border-radius: 8px;
  font-weight: 900;
  color: var(--ink);
  transition: transform 160ms ease, box-shadow 160ms ease, background 160ms ease;
}

.icon-button,
.secondary-button,
.tip-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 10px 13px;
  background: var(--white);
  box-shadow: inset 0 0 0 2px var(--line), 0 4px 0 rgba(67, 47, 24, 0.14);
}

.icon-button:hover,
.primary-button:hover,
.secondary-button:hover,
.tip-button:hover,
.palette-button:hover,
.command-token:hover,
.level-strip button:hover {
  transform: translateY(-2px);
}

.icon-button:active,
.primary-button:active,
.secondary-button:active,
.tip-button:active,
.palette-button:active,
.command-token:active,
.level-strip button:active {
  transform: translateY(1px);
}

.button-icon {
  display: inline-grid;
  place-items: center;
  width: 26px;
  height: 26px;
  border-radius: 7px;
  background: rgba(36, 49, 58, 0.1);
  font-size: 0.8rem;
}

.danger-light {
  color: #8f2f28;
}

.home-intro {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: end;
  gap: 24px;
  padding: 32px 0 24px;
}

.eyebrow,
.level-kicker {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 900;
  text-transform: uppercase;
}

h1,
h2,
h3,
p {
  overflow-wrap: anywhere;
}

h1 {
  margin: 0;
  font-size: 3rem;
  font-weight: 1000;
}

.home-intro h1 {
  font-size: 3.6rem;
}

.intro-copy {
  max-width: 620px;
  margin: 10px 0 0;
  color: #3c4d57;
  font-size: 1.2rem;
  line-height: 1.45;
}

.progress-summary {
  min-width: 210px;
  padding: 18px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: var(--white);
  box-shadow: var(--shadow);
  text-align: center;
}

.progress-number {
  display: block;
  color: var(--sky-deep);
  font-size: 2.6rem;
  font-weight: 1000;
  line-height: 1;
}

.progress-label {
  display: block;
  margin-bottom: 6px;
  color: var(--muted);
  font-weight: 900;
}

.progress-divider {
  display: block;
  width: 72px;
  height: 2px;
  margin: 8px auto;
  background: var(--line);
}

.chapter-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 18px;
}

.chapter-card {
  display: flex;
  min-height: 300px;
  flex-direction: column;
  gap: 14px;
  padding: 18px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: var(--white);
  box-shadow: var(--shadow);
}

.chapter-card h2 {
  margin: 0;
  font-size: 1.65rem;
  font-weight: 1000;
}

.chapter-card p {
  margin: 0;
  color: #485962;
  line-height: 1.45;
}

.chapter-card-top,
.chapter-card-bottom,
.level-meta,
.tips-header,
.action-row {
  justify-content: space-between;
  gap: 12px;
}

.chapter-badge,
.skill-pill,
.mode-pill,
.repeat-note {
  display: inline-flex;
  align-items: center;
  min-height: 32px;
  padding: 6px 10px;
  border-radius: 8px;
  background: #f5ead7;
  color: #5f4c32;
  font-weight: 900;
}

.skill-pill {
  background: rgba(57, 116, 197, 0.12);
  color: var(--sky-deep);
}

.star-meter,
.mini-stars {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.star-meter span,
.mini-stars i {
  display: inline-block;
  border: 2px solid rgba(36, 49, 58, 0.28);
  border-radius: 999px;
  background: transparent;
}

.star-meter span {
  width: 15px;
  height: 15px;
}

.mini-stars i {
  width: 9px;
  height: 9px;
}

.star-meter .filled,
.mini-stars .filled {
  border-color: #c98509;
  background: var(--yellow);
}

.primary-button {
  min-width: 92px;
  min-height: 46px;
  padding: 11px 18px;
  background: var(--ink);
  color: #fffaf0;
  box-shadow: 0 5px 0 rgba(36, 49, 58, 0.28);
}

.chapter-card-bottom {
  margin-top: auto;
  color: var(--muted);
  font-weight: 900;
}

.chapter-coral {
  border-top-color: var(--coral);
}

.chapter-mint {
  border-top-color: var(--mint);
}

.chapter-sky {
  border-top-color: var(--sky);
}

.game-shell {
  padding-top: 20px;
}

.game-header {
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

.game-header h1 {
  font-size: 2.3rem;
}

.mode-pill {
  background: var(--ink);
  color: #fffaf0;
}

.level-strip {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
  overflow-x: auto;
  padding-bottom: 4px;
}

.level-strip button {
  display: grid;
  gap: 5px;
  min-width: 126px;
  min-height: 58px;
  padding: 9px 12px;
  background: var(--white);
  box-shadow: inset 0 0 0 2px var(--line);
  text-align: left;
}

.level-strip button.active {
  background: #fff2c6;
  box-shadow: inset 0 0 0 3px var(--yellow), 0 4px 0 rgba(67, 47, 24, 0.12);
}

.level-strip button.complete {
  color: var(--mint-deep);
}

.game-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(320px, 390px);
  gap: 18px;
  align-items: start;
}

.board-panel,
.control-panel {
  border: 2px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 253, 248, 0.9);
  box-shadow: var(--shadow);
}

.board-panel {
  padding: 18px;
}

.control-panel {
  display: grid;
  gap: 16px;
  padding: 16px;
}

.game-prompt {
  min-height: 34px;
  margin: 0 0 12px;
  color: #3c4d57;
  font-size: 1.08rem;
  font-weight: 800;
}

.board-wrap {
  position: relative;
  max-width: 640px;
  margin: 0 auto;
  padding: 14px;
  border: 3px solid #cfb990;
  border-radius: 8px;
  background: #f8ead2;
}

.board-wrap.is-mistake {
  animation: board-nudge 260ms ease;
}

.board-wrap.is-success {
  border-color: var(--mint);
  background: #e9f7eb;
}

.game-board {
  display: grid;
  gap: 8px;
}

.board-cell {
  position: relative;
  display: grid;
  min-width: 0;
  aspect-ratio: 1;
  place-items: center;
  border: 2px dashed rgba(67, 47, 24, 0.2);
  border-radius: 8px;
  background: var(--white);
}

.board-cell.is-trail {
  background: #fff3c7;
}

.board-cell.is-start {
  border-style: solid;
  border-color: rgba(58, 161, 126, 0.45);
}

.board-cell.is-goal {
  border-style: solid;
  border-color: rgba(232, 95, 79, 0.45);
}

.board-cell.is-wall {
  border-style: solid;
  border-color: #8e795b;
  background: #d9c6a4;
}

.board-cell.is-runner .shape-piece {
  animation: soft-pop 260ms ease;
}

.shape-piece {
  display: block;
  width: 62%;
  height: 62%;
  box-shadow: 0 5px 0 rgba(36, 49, 58, 0.16);
}

.shape-runner {
  border-radius: 8px;
  background: var(--sky);
}

.shape-goal {
  width: 58%;
  height: 58%;
  clip-path: polygon(50% 0, 62% 32%, 96% 35%, 69% 55%, 79% 90%, 50% 70%, 21% 90%, 31% 55%, 4% 35%, 38% 32%);
  background: var(--coral);
}

.shape-wall {
  width: 70%;
  height: 46%;
  border-radius: 6px;
  background: #9f8969;
}

.shape-start {
  border-radius: 999px;
  background: var(--mint);
}

.board-message {
  min-height: 54px;
  margin: 14px 0 0;
  padding: 12px;
  border-radius: 8px;
  background: #f6ead6;
  color: #4d3d28;
  font-weight: 900;
}

.board-message[data-state="success"] {
  background: #dff2e7;
  color: var(--mint-deep);
}

.board-message[data-state="try"] {
  background: #fde4df;
  color: var(--coral-deep);
}

.level-meta h2,
.tips-panel h3 {
  margin: 0;
  font-size: 1.3rem;
  font-weight: 1000;
}

.current-stars {
  display: grid;
  min-width: 56px;
  min-height: 48px;
  place-items: center;
  border-radius: 8px;
  background: var(--yellow);
  color: #5c3f00;
  font-weight: 1000;
}

.repeat-note {
  justify-content: center;
  background: #e5f4ef;
  color: var(--mint-deep);
}

.program-lane,
.palette {
  display: grid;
  gap: 10px;
}

.program-lane {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  min-height: 58px;
}

.palette {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.command-token,
.palette-button {
  display: grid;
  min-height: 62px;
  grid-template-columns: 30px minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  padding: 9px;
  text-align: left;
  box-shadow: 0 4px 0 rgba(36, 49, 58, 0.16);
}

.command-token:disabled,
.palette-button:disabled,
.primary-button:disabled,
.secondary-button:disabled,
.tip-button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
  transform: none;
}

.command-glyph {
  display: grid;
  width: 30px;
  height: 30px;
  place-items: center;
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.78);
  font-size: 1rem;
}

.cmd-up {
  background: #e6f2ff;
  color: #214c78;
}

.cmd-right {
  background: #e4f5eb;
  color: var(--mint-deep);
}

.cmd-down {
  background: #fff0c7;
  color: #785400;
}

.cmd-left {
  background: #ffe3df;
  color: var(--coral-deep);
}

.command-token.is-editable {
  box-shadow: inset 0 0 0 3px var(--violet), 0 4px 0 rgba(36, 49, 58, 0.16);
}

.empty-program {
  margin: 0;
  padding: 12px;
  border: 2px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  font-weight: 900;
  text-align: center;
}

.action-row {
  flex-wrap: wrap;
}

.run-button {
  flex: 1 1 120px;
}

.secondary-button {
  flex: 1 1 96px;
}

.tips-panel {
  display: grid;
  gap: 10px;
  padding: 13px;
  border: 2px solid #e4d3b5;
  border-radius: 8px;
  background: #fff8e8;
}

.tips-panel ol {
  display: grid;
  gap: 8px;
  margin: 0;
  padding-left: 22px;
  color: #4b5a62;
  font-weight: 800;
}

.tip-button {
  min-height: 40px;
  padding: 8px 10px;
}

.celebration {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}

.celebration-piece {
  position: absolute;
  bottom: 12%;
  display: block;
  width: 20px;
  height: 20px;
  animation: float-up 900ms ease-out forwards;
}

.piece-1 {
  border-radius: 5px;
  background: var(--coral);
}

.piece-2 {
  border-radius: 999px;
  background: var(--yellow);
}

.piece-3 {
  clip-path: polygon(50% 0, 100% 100%, 0 100%);
  background: var(--mint);
}

.piece-4 {
  border-radius: 4px;
  background: var(--sky);
  transform: rotate(45deg);
}

@keyframes soft-pop {
  0% {
    transform: scale(0.82);
  }
  70% {
    transform: scale(1.08);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes board-nudge {
  0%,
  100% {
    transform: translateX(0);
  }
  35% {
    transform: translateX(-5px);
  }
  70% {
    transform: translateX(5px);
  }
}

@keyframes float-up {
  0% {
    opacity: 0;
    transform: translateY(18px) scale(0.82) rotate(0deg);
  }
  18% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform: translateY(-190px) scale(1.08) rotate(35deg);
  }
}

@media (max-width: 940px) {
  .home-intro,
  .game-layout {
    grid-template-columns: 1fr;
  }

  .progress-summary {
    justify-self: start;
  }

  .chapter-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 640px) {
  .app-shell {
    width: min(100% - 20px, 1180px);
    padding-top: 10px;
  }

  .top-bar,
  .game-header,
  .home-intro {
    align-items: stretch;
  }

  .top-bar,
  .game-header {
    flex-direction: column;
  }

  .top-actions {
    justify-content: stretch;
  }

  .top-actions .icon-button,
  .game-header .icon-button,
  .mode-pill {
    width: 100%;
  }

  h1,
  .home-intro h1 {
    font-size: 2.25rem;
  }

  .intro-copy {
    font-size: 1.05rem;
  }

  .board-panel,
  .control-panel,
  .chapter-card {
    padding: 12px;
  }

  .game-board {
    gap: 5px;
  }

  .board-wrap {
    padding: 8px;
  }

  .program-lane {
    grid-template-columns: 1fr;
  }

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

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.001ms !important;
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
  }
}
