:root {
  color-scheme: light;
  --ink: #17201d;
  --muted: #66716c;
  --paper: #f2f5f0;
  --surface: #ffffff;
  --line: #d7ded7;
  --deep: #24403a;
  --teal: #1f8f86;
  --coral: #ee6a5a;
  --amber: #e6a93f;
  --violet: #7464b8;
  --green: #5f9f62;
  --danger: #d04444;
  --shadow: 0 22px 60px rgba(23, 32, 29, 0.13);
}

* {
  box-sizing: border-box;
  letter-spacing: 0;
}

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

body {
  margin: 0;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
  background:
    linear-gradient(90deg, rgba(36, 64, 58, 0.05) 1px, transparent 1px),
    linear-gradient(0deg, rgba(36, 64, 58, 0.04) 1px, transparent 1px),
    var(--paper);
  background-size: 34px 34px;
  color: var(--ink);
}

button {
  font: inherit;
}

.app-shell {
  min-height: 100vh;
  padding: 22px;
  display: flex;
  flex-direction: column;
  gap: 22px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.brand {
  display: flex;
  align-items: center;
  min-width: 0;
  gap: 12px;
}

.brand-mark {
  width: 48px;
  height: 48px;
  display: grid;
  place-items: center;
  background: var(--deep);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 8px;
  color: #fff;
  box-shadow: 0 10px 26px rgba(36, 64, 58, 0.2);
}

.brand-mark svg {
  width: 31px;
  height: 31px;
  fill: none;
  stroke: currentColor;
  stroke-width: 3;
  stroke-linecap: round;
  stroke-linejoin: round;
}

h1,
h2,
p {
  margin: 0;
}

.brand h1 {
  font-size: 1.55rem;
  line-height: 1.05;
  font-weight: 800;
}

.brand p {
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.88rem;
}

.hud {
  display: flex;
  align-items: stretch;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.hud-item {
  min-width: 86px;
  padding: 9px 11px;
  background: rgba(255, 255, 255, 0.84);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(23, 32, 29, 0.06);
}

.game-select {
  display: grid;
  gap: 10px;
}

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

.section-head span {
  display: block;
  color: var(--teal);
  font-size: 0.72rem;
  font-weight: 900;
  text-transform: uppercase;
}

.section-head h2 {
  margin-top: 2px;
  color: var(--deep);
  font-size: 1.08rem;
}

.section-head p {
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 800;
}

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

.mode-card {
  position: relative;
  min-height: 126px;
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-template-areas:
    "index label play"
    "title title title"
    "copy copy copy";
  align-content: start;
  gap: 8px 10px;
  padding: 14px;
  text-align: left;
  color: var(--deep);
  background: rgba(255, 255, 255, 0.84);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
  box-shadow: 0 10px 26px rgba(23, 32, 29, 0.07);
}

.mode-index {
  grid-area: index;
  min-width: 30px;
  height: 30px;
  display: grid;
  place-items: center;
  color: var(--deep);
  background: #eef4ef;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 0.76rem;
  font-weight: 900;
}

.mode-label {
  grid-area: label;
  align-self: center;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 900;
  text-transform: uppercase;
}

.mode-card strong {
  grid-area: title;
  font-size: 1.18rem;
  line-height: 1.08;
}

.mode-card small {
  grid-area: copy;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 700;
  line-height: 1.35;
}

.mode-play {
  grid-area: play;
  align-self: start;
  justify-self: end;
  min-width: 46px;
  padding: 5px 8px;
  color: var(--teal);
  background: #edf7f5;
  border: 1px solid rgba(31, 143, 134, 0.26);
  border-radius: 999px;
  font-size: 0.7rem;
  font-weight: 900;
  text-align: center;
}

.mode-card.current {
  color: #fff;
  background: var(--deep);
  border-color: var(--deep);
}

.mode-card.current .mode-label,
.mode-card.current small {
  color: rgba(255, 255, 255, 0.72);
}

.mode-card.current .mode-index {
  color: var(--deep);
  background: #fff;
  border-color: rgba(255, 255, 255, 0.58);
}

.mode-card.current .mode-play {
  color: var(--deep);
  background: #fff;
  border-color: #fff;
}

.mode-card:hover {
  transform: translateY(-1px);
}

.hud-item span,
.compact-stats span {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
}

.hud-item strong,
.compact-stats strong {
  display: block;
  margin-top: 2px;
  font-size: 1.18rem;
}

.hearts {
  min-width: 112px;
  display: flex;
  align-items: center;
  gap: 7px;
}

.heart-dot {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid var(--danger);
  background: var(--danger);
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.16);
}

.heart-dot.empty {
  background: transparent;
  border-color: #d2d7d1;
  box-shadow: none;
}

.game-area {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 290px;
  gap: 22px;
  align-items: center;
}

.stage-wrap {
  display: grid;
  gap: 12px;
  justify-items: center;
  min-width: 0;
}

.board-frame {
  width: min(84vh, 88vw, 620px);
  padding: 14px;
  background: #e7ebe5;
  border: 1px solid #cfd8d0;
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.board {
  --cell-pct: 16.6667%;
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  overflow: visible;
  border-radius: 7px;
  background:
    linear-gradient(90deg, rgba(23, 32, 29, 0.11) 1px, transparent 1px),
    linear-gradient(0deg, rgba(23, 32, 29, 0.11) 1px, transparent 1px),
    #fbfcfa;
  background-size: var(--cell-pct) var(--cell-pct);
  box-shadow: inset 0 0 0 1px rgba(23, 32, 29, 0.1);
}

.board::before {
  content: "";
  position: absolute;
  inset: -10px;
  border-radius: 8px;
  border: 2px dashed rgba(31, 143, 134, 0.34);
  pointer-events: none;
}

.piece {
  position: absolute;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
  transition:
    left 260ms ease,
    top 260ms ease,
    opacity 220ms ease,
    filter 180ms ease;
  touch-action: manipulation;
}

.piece:focus-visible {
  outline: 3px solid #111;
  outline-offset: 2px;
}

.piece-body {
  position: absolute;
  inset: 10%;
  display: grid;
  place-items: center;
  border-radius: 8px;
  color: #fff;
  box-shadow:
    0 9px 18px rgba(23, 32, 29, 0.16),
    inset 0 -3px 0 rgba(0, 0, 0, 0.17),
    inset 0 1px 0 rgba(255, 255, 255, 0.28);
}

.piece[data-dir="N"] .piece-body {
  background: var(--teal);
}

.piece[data-dir="E"] .piece-body {
  background: var(--coral);
}

.piece[data-dir="S"] .piece-body {
  background: var(--amber);
  color: #332511;
}

.piece[data-dir="W"] .piece-body {
  background: var(--violet);
}

.arrow-mark {
  width: 58%;
  height: 58%;
  display: block;
}

.arrow-mark svg {
  width: 100%;
  height: 100%;
  fill: none;
  stroke: currentColor;
  stroke-width: 3.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.piece.escaping {
  pointer-events: none;
  opacity: 0;
  filter: blur(1px);
}

.piece.hint .piece-body {
  animation: hintPulse 760ms ease-in-out 2;
  box-shadow:
    0 0 0 5px rgba(31, 143, 134, 0.22),
    0 10px 22px rgba(23, 32, 29, 0.2),
    inset 0 -3px 0 rgba(0, 0, 0, 0.17);
}

.piece.blocked .piece-body {
  animation: hitPulse 320ms ease;
}

.piece.bump-N .piece-body {
  animation: bumpN 280ms ease;
}

.piece.bump-E .piece-body {
  animation: bumpE 280ms ease;
}

.piece.bump-S .piece-body {
  animation: bumpS 280ms ease;
}

.piece.bump-W .piece-body {
  animation: bumpW 280ms ease;
}

.status-line {
  min-height: 26px;
  color: var(--deep);
  font-weight: 800;
  text-align: center;
}

.status-line.warn {
  color: var(--danger);
}

.side-panel {
  align-self: stretch;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 14px;
  background: rgba(255, 255, 255, 0.82);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 18px 44px rgba(23, 32, 29, 0.09);
}

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

.icon-button {
  height: 52px;
  display: grid;
  place-items: center;
  color: var(--deep);
  background: #f9fbf8;
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
  box-shadow: inset 0 -2px 0 rgba(23, 32, 29, 0.06);
}

.icon-button svg {
  width: 25px;
  height: 25px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.3;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-button:hover,
.level-list button:hover,
.result-actions button:hover {
  transform: translateY(-1px);
}

.icon-button:active,
.level-list button:active,
.result-actions button:active {
  transform: translateY(0);
}

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

.panel-block {
  display: grid;
  gap: 10px;
}

.panel-block h2 {
  font-size: 0.9rem;
  text-transform: uppercase;
}

.game-guide {
  padding: 12px;
  background: #f5f8f4;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.game-guide h2 {
  color: var(--deep);
}

.game-guide p {
  color: var(--muted);
  font-size: 0.84rem;
  line-height: 1.38;
}

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

.guide-grid > div {
  min-width: 0;
  padding: 9px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.guide-grid span {
  display: block;
  color: var(--muted);
  font-size: 0.68rem;
  font-weight: 900;
}

.guide-grid strong {
  display: block;
  margin-top: 2px;
  font-size: 0.9rem;
  line-height: 1.2;
}

.ai-form {
  display: block;
}

[data-mode="sprint"] #ai-form {
  display: none;
}

[data-mode="rails"] #ai-form,
[data-mode="signals"] #ai-form,
[data-mode="daily"] #ai-form,
[data-mode="rails"] .control-grid,
[data-mode="signals"] .control-grid,
[data-mode="daily"] .control-grid,
[data-mode="rails"] #level-panel,
[data-mode="signals"] #level-panel,
[data-mode="daily"] #level-panel,
[data-mode="rails"] .compact-stats,
[data-mode="signals"] .compact-stats,
[data-mode="daily"] .compact-stats {
  display: none;
}

.field {
  display: grid;
  gap: 5px;
}

.field span {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
}

.field input,
.field select {
  width: 100%;
  min-height: 41px;
  padding: 8px 10px;
  color: var(--ink);
  background: #f9fbf8;
  border: 1px solid var(--line);
  border-radius: 8px;
  outline: none;
}

.field input:focus,
.field select:focus {
  border-color: rgba(31, 143, 134, 0.7);
  box-shadow: 0 0 0 3px rgba(31, 143, 134, 0.16);
}

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

.primary-button {
  min-height: 44px;
  border: 1px solid var(--deep);
  border-radius: 8px;
  background: var(--deep);
  color: #fff;
  font-weight: 900;
  cursor: pointer;
}

.primary-button:hover {
  transform: translateY(-1px);
}

.primary-button:disabled {
  cursor: wait;
  opacity: 0.62;
  transform: none;
}

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

.prompt-chips button {
  min-height: 34px;
  padding: 0 7px;
  color: var(--deep);
  background: #eef4ef;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 0.78rem;
  font-weight: 800;
  cursor: pointer;
}

.ai-status {
  min-height: 34px;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.35;
}

.ai-status strong {
  color: var(--deep);
}

.sprint-panel {
  display: none;
}

[data-mode="sprint"] .sprint-panel {
  display: grid;
}

.mock-panel {
  display: none;
}

[data-mode="rails"] .mock-panel,
[data-mode="signals"] .mock-panel,
[data-mode="daily"] .mock-panel {
  display: grid;
}

.mock-copy {
  color: var(--muted);
  font-size: 0.84rem;
  line-height: 1.4;
}

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

.mock-stats > div {
  padding: 10px;
  background: #f5f8f4;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.mock-stats span {
  display: block;
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 900;
}

.mock-stats strong {
  display: block;
  margin-top: 2px;
  font-size: 1rem;
}

.mock-grid {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(var(--mock-size), minmax(0, 1fr));
  grid-template-rows: repeat(var(--mock-size), minmax(0, 1fr));
  gap: 4px;
  padding: 8px;
}

.mock-cell {
  min-width: 0;
  min-height: 0;
  display: grid;
  place-items: center;
  color: var(--deep);
  background: rgba(255, 255, 255, 0.86);
  border: 1px solid rgba(36, 64, 58, 0.12);
  border-radius: 8px;
  font-weight: 900;
  cursor: pointer;
  box-shadow: inset 0 -2px 0 rgba(23, 32, 29, 0.05);
}

.mock-cell:hover {
  transform: translateY(-1px);
}

.mock-cell.start {
  color: #fff;
  background: var(--teal);
}

.mock-cell.goal {
  color: #fff;
  background: var(--coral);
}

.mock-cell.block {
  color: rgba(255, 255, 255, 0.86);
  background: #5b625f;
  cursor: not-allowed;
}

.mock-cell.path {
  color: #fff;
  background: var(--violet);
}

.mock-cell.on {
  color: #332511;
  background: var(--amber);
}

.mock-cell.seeded {
  color: #fff;
  background: var(--green);
}

.mock-cell.pulse {
  animation: hintPulse 620ms ease-in-out 1;
}

[data-mode="creator"] #ai-form {
  outline: 3px solid rgba(31, 143, 134, 0.18);
  outline-offset: 6px;
  border-radius: 8px;
}

.sprint-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.sprint-grid > div {
  min-width: 0;
  padding: 9px;
  background: #f5f8f4;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.sprint-grid span {
  display: block;
  color: var(--muted);
  font-size: 0.68rem;
  font-weight: 900;
}

.sprint-grid strong {
  display: block;
  margin-top: 2px;
  font-size: 1.05rem;
}

.level-list {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 7px;
}

.level-list button {
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f9fbf8;
  color: var(--deep);
  font-weight: 900;
  cursor: pointer;
}

.level-list button.current {
  background: var(--deep);
  border-color: var(--deep);
  color: #fff;
}

.level-list button.cleared {
  border-color: rgba(95, 159, 98, 0.58);
  box-shadow: inset 0 -3px 0 rgba(95, 159, 98, 0.24);
}

.level-list button.generated {
  border-color: rgba(31, 143, 134, 0.55);
  color: var(--teal);
}

.compact-stats {
  margin-top: auto;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.compact-stats > div {
  padding: 10px;
  background: #f5f8f4;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.result-panel {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(23, 32, 29, 0.38);
  backdrop-filter: blur(4px);
}

.result-panel[hidden] {
  display: none;
}

.result-card {
  width: min(420px, 100%);
  padding: 24px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  text-align: center;
}

.result-card h2 {
  font-size: 1.7rem;
}

.result-card p {
  margin-top: 8px;
  color: var(--muted);
}

.result-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 20px;
}

.result-actions button {
  min-height: 46px;
  border: 1px solid var(--deep);
  border-radius: 8px;
  background: var(--deep);
  color: #fff;
  font-weight: 900;
  cursor: pointer;
}

.result-actions button + button {
  background: #fff;
  color: var(--deep);
}

@keyframes hintPulse {
  0%,
  100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.1);
  }
}

@keyframes hitPulse {
  0%,
  100% {
    transform: scale(1);
  }
  50% {
    transform: scale(0.88);
  }
}

@keyframes bumpN {
  45% {
    transform: translateY(-14%);
  }
}

@keyframes bumpE {
  45% {
    transform: translateX(14%);
  }
}

@keyframes bumpS {
  45% {
    transform: translateY(14%);
  }
}

@keyframes bumpW {
  45% {
    transform: translateX(-14%);
  }
}

@media (max-width: 820px) {
  .app-shell {
    padding: 14px;
    gap: 14px;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .hud {
    width: 100%;
    justify-content: stretch;
  }

  .hud-item {
    flex: 1 1 96px;
  }

  .game-area {
    grid-template-columns: 1fr;
    align-items: start;
  }

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

  .board-frame {
    width: min(92vw, 620px);
    padding: 10px;
  }

  .side-panel {
    align-self: auto;
  }

  .compact-stats {
    margin-top: 0;
  }
}

@media (max-width: 440px) {
  .brand-mark {
    width: 42px;
    height: 42px;
  }

  .brand h1 {
    font-size: 1.25rem;
  }

  .control-grid,
  .level-list {
    grid-template-columns: repeat(4, minmax(48px, 1fr));
  }

  .section-head {
    align-items: start;
    flex-direction: column;
    gap: 4px;
  }

  .mode-shelf {
    grid-template-columns: 1fr;
  }

  .mode-card {
    min-height: 112px;
  }

  .mock-grid {
    gap: 3px;
    padding: 6px;
  }

  .icon-button {
    height: 48px;
  }
}
