:root {
  color-scheme: light;
  --bg: #f4f7f1;
  --ink: #1d2628;
  --muted: #62716b;
  --line: #d8dfd4;
  --panel: #ffffff;
  --accent: #1f6feb;
  --clear: #eaf1e6;
  --hit: #ffe08a;
  --select: #8fd1ff;
  --shadow: 0 18px 48px rgba(34, 45, 42, 0.14);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background:
    linear-gradient(135deg, rgba(31, 111, 235, 0.08), transparent 38%),
    linear-gradient(315deg, rgba(217, 72, 15, 0.08), transparent 42%),
    var(--bg);
  color: var(--ink);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

button,
input {
  font: inherit;
}

.app {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 20px;
  width: min(1180px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 28px 0;
}

.playfield,
.panel {
  min-width: 0;
}

.topbar {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 18px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: clamp(30px, 5vw, 56px);
  line-height: 1;
  letter-spacing: 0;
}

.topbar p,
.status-card p,
.rules,
.log,
.legend {
  color: var(--muted);
}

.topbar p {
  margin-top: 10px;
  font-size: 16px;
}

.statline {
  display: grid;
  grid-template-columns: repeat(3, minmax(74px, 1fr));
  gap: 8px;
  min-width: 290px;
}

.stat,
.status-card,
.rules,
.legend,
.log {
  background: rgba(255, 255, 255, 0.82);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(35, 45, 41, 0.08);
}

.stat {
  padding: 10px 12px;
}

.stat span {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

.stat strong {
  display: block;
  margin-top: 4px;
  font-size: 24px;
}

.board-wrap {
  padding: 14px;
  background: rgba(255, 255, 255, 0.74);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.board {
  display: grid;
  grid-template-columns: repeat(8, minmax(36px, 1fr));
  gap: 7px;
  aspect-ratio: 1;
  isolation: isolate;
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  aspect-ratio: 1;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--clear);
  font-size: clamp(22px, 4.8vw, 42px);
  line-height: 1;
  transition:
    transform 180ms ease,
    background 180ms ease,
    border-color 180ms ease,
    box-shadow 180ms ease;
  will-change: transform, opacity;
  touch-action: manipulation;
}

.cell.obstacle {
  background: #fffaf0;
}

.cell.candidate {
  border-color: rgba(31, 111, 235, 0.48);
}

.cell.candidate::before {
  position: absolute;
  top: 5px;
  left: 5px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  content: "";
  opacity: 0.72;
}

.cell.hit {
  background: var(--hit);
  transform: scale(0.94);
  border-color: rgba(168, 109, 0, 0.42);
  box-shadow: 0 0 0 3px rgba(255, 224, 138, 0.38);
}

.cell.selected {
  background: var(--select);
  border-color: rgba(31, 111, 235, 0.7);
  transform: translateY(-2px);
  box-shadow: 0 10px 20px rgba(31, 111, 235, 0.18);
}

.cell.selected::after {
  position: absolute;
  right: 5px;
  bottom: 4px;
  color: rgba(29, 38, 40, 0.72);
  content: "swap";
  font-size: 12px;
  font-weight: 900;
}

.command {
  margin-top: 18px;
}

.command label {
  display: block;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.input-row {
  display: grid;
  grid-template-columns: 1fr 112px;
  gap: 10px;
}

.input-row input {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px 16px;
  background: var(--panel);
  color: var(--ink);
  outline: none;
}

.input-row input:focus {
  border-color: rgba(31, 111, 235, 0.64);
  box-shadow: 0 0 0 4px rgba(31, 111, 235, 0.12);
}

button {
  border: 0;
  border-radius: 8px;
  padding: 12px 14px;
  background: var(--ink);
  color: white;
  cursor: pointer;
  font-weight: 800;
  touch-action: manipulation;
}

button:hover {
  filter: brightness(1.08);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.54;
}

@media (prefers-reduced-motion: reduce) {
  .cell {
    transition: none;
  }
}

.panel {
  display: grid;
  gap: 12px;
  align-content: start;
}

.status-card,
.rules,
.legend,
.log {
  padding: 16px;
}

.status-card h2,
.rules h2,
.legend h2,
.log h2 {
  margin-bottom: 10px;
  color: var(--ink);
  font-size: 16px;
}

.status-card button {
  width: 100%;
  margin-top: 14px;
  background: var(--accent);
}

.rules ul,
.log ol {
  margin: 0;
  padding-left: 18px;
}

.rules li,
.log li {
  margin: 9px 0;
}

.legend-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 100%;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 9px;
  background: #f8faf6;
  color: var(--ink);
  font-size: 13px;
}

.log {
  max-height: 340px;
  overflow: auto;
}

.log li {
  color: var(--muted);
  font-size: 14px;
}

.log strong {
  color: var(--ink);
}

@media (max-width: 900px) {
  .app {
    grid-template-columns: 1fr;
    width: min(680px, calc(100vw - 24px));
    padding-top: 16px;
  }

  .topbar {
    display: block;
  }

  .statline {
    min-width: 0;
    margin-top: 14px;
  }
}

@media (max-width: 520px) {
  body {
    background: var(--bg);
  }

  .app {
    gap: 12px;
    width: min(100vw - 16px, 430px);
    padding: 10px 0 16px;
  }

  .topbar {
    margin-bottom: 10px;
  }

  h1 {
    font-size: 28px;
    line-height: 1.05;
  }

  .topbar p {
    margin-top: 6px;
    font-size: 13px;
    line-height: 1.35;
  }

  .statline {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
    margin-top: 10px;
  }

  .stat {
    padding: 8px 7px;
    box-shadow: none;
  }

  .stat span {
    font-size: 11px;
    white-space: nowrap;
  }

  .stat strong {
    margin-top: 2px;
    font-size: 22px;
  }

  .board-wrap {
    padding: 6px;
    border-radius: 8px;
    box-shadow: 0 10px 26px rgba(34, 45, 42, 0.12);
  }

  .board {
    gap: 4px;
  }

  .cell {
    border-radius: 7px;
    font-size: clamp(21px, 8vw, 34px);
  }

  .cell.candidate::before {
    top: 4px;
    left: 4px;
    width: 6px;
    height: 6px;
  }

  .cell.selected::after {
    right: 4px;
    bottom: 3px;
    font-size: 9px;
    content: "";
  }

  .command {
    margin-top: 10px;
  }

  .command label {
    margin-bottom: 6px;
    font-size: 12px;
  }

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

  .input-row input {
    min-height: 46px;
    padding: 12px 13px;
    font-size: 16px;
  }

  .input-row button {
    min-height: 46px;
    padding: 11px 12px;
  }

  .panel {
    gap: 8px;
  }

  .status-card,
  .rules,
  .legend,
  .log {
    padding: 12px;
    box-shadow: none;
  }

  .status-card h2,
  .rules h2,
  .legend h2,
  .log h2 {
    margin-bottom: 7px;
    font-size: 14px;
  }

  .status-card p,
  .rules,
  .log li {
    font-size: 13px;
    line-height: 1.4;
  }

  .status-card button {
    margin-top: 10px;
    min-height: 42px;
  }

  .rules li,
  .log li {
    margin: 6px 0;
  }

  .chip {
    padding: 5px 8px;
    font-size: 12px;
  }

  .log {
    max-height: 180px;
  }
}

@media (max-width: 380px) {
  .app {
    width: calc(100vw - 10px);
  }

  .board-wrap {
    padding: 4px;
  }

  .board {
    gap: 3px;
  }

  .cell {
    font-size: clamp(19px, 7.7vw, 30px);
  }
}
