:root {
  --paper: #f5f6f1;
  --ink: #17202a;
  --muted: #68717d;
  --line: #cfd6d1;
  --panel: #ffffff;
  --teal: #0f8b8d;
  --coral: #d94f3d;
  --amber: #e6a33a;
  --blue: #3b6ea8;
  --green: #4f9d69;
  --shadow: 0 18px 50px rgba(23, 32, 42, 0.11);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background:
    linear-gradient(90deg, rgba(23, 32, 42, 0.04) 1px, transparent 1px),
    linear-gradient(0deg, rgba(23, 32, 42, 0.04) 1px, transparent 1px),
    var(--paper);
  background-size: 32px 32px;
  color: var(--ink);
  font-family: "Noto Sans SC", "Microsoft YaHei", "Segoe UI", sans-serif;
}

main {
  width: min(1120px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 36px 0 52px;
}

.shell {
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
}

.hero {
  display: grid;
  gap: 12px;
  padding-bottom: 8px;
  border-bottom: 2px solid var(--ink);
}

.top-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
}

.back-link {
  display: inline-flex;
  align-items: center;
  width: fit-content;
  min-height: 36px;
  padding: 0 12px;
  border: 2px solid var(--ink);
  border-radius: 999px;
  background: #fff;
  color: var(--ink);
  text-decoration: none;
  font-size: 14px;
  font-weight: 900;
}

.back-link:hover,
.back-link:focus-visible {
  background: var(--ink);
  color: #fff;
  outline: 3px solid rgba(15, 139, 141, 0.35);
  outline-offset: 2px;
}

h1 {
  margin: 0;
  font-family: Georgia, "Noto Serif SC", serif;
  font-size: clamp(30px, 5vw, 58px);
  line-height: 1.05;
  letter-spacing: 0;
}

h2 {
  margin: 0 0 12px;
  font-size: 22px;
}

p {
  margin: 0;
  color: var(--muted);
  line-height: 1.8;
}

a {
  color: var(--blue);
  font-weight: 700;
}

.board {
  min-height: 520px;
  padding: 24px;
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.visual-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.25fr) minmax(280px, 0.75fr);
  gap: 22px;
  align-items: stretch;
}

.stage {
  min-height: 340px;
  padding: 22px;
  border: 2px solid var(--ink);
  border-radius: 8px;
  background:
    linear-gradient(90deg, rgba(15, 139, 141, 0.06) 1px, transparent 1px),
    linear-gradient(0deg, rgba(15, 139, 141, 0.06) 1px, transparent 1px),
    #fff;
  background-size: 24px 24px;
}

.side-panel {
  display: grid;
  gap: 14px;
}

.context-card,
.purpose-card,
.state-card,
.code-card {
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.context-label {
  margin: 0 0 6px;
  color: var(--coral);
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0.08em;
}

.context-title {
  margin: 0 0 8px;
  color: var(--ink);
  font-size: 15px;
  font-weight: 900;
}

.purpose-card {
  border-color: rgba(15, 139, 141, 0.42);
  background: #eef8f6;
}

.purpose-card .state-title {
  color: var(--teal);
}

.state-title {
  margin: 0 0 8px;
  font-size: 15px;
  font-weight: 900;
  color: var(--ink);
}

.state-text {
  min-height: 76px;
  color: var(--muted);
  line-height: 1.75;
}

.code-list {
  display: grid;
  gap: 8px;
  margin: 0;
  padding: 0;
  list-style: none;
  font-family: Consolas, "SFMono-Regular", monospace;
  font-size: 13px;
}

.code-list li {
  padding: 7px 9px;
  border-radius: 5px;
  background: #f3f5f3;
  color: #3d4650;
  transition: background 180ms ease, color 180ms ease, transform 180ms ease;
}

.code-list li.is-active {
  background: #e2f2ee;
  color: var(--ink);
  transform: translateX(4px);
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.controls button {
  min-height: 38px;
  padding: 0 13px;
  border: 2px solid var(--ink);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  font-weight: 900;
  cursor: pointer;
  touch-action: manipulation;
}

.controls button:hover,
.controls button:focus-visible {
  background: var(--ink);
  color: #fff;
}

.controls button:focus-visible {
  outline: 3px solid rgba(15, 139, 141, 0.35);
  outline-offset: 2px;
}

.controls button[aria-pressed="true"],
.step-indicator.is-active {
  background: var(--teal);
  color: #fff;
}

.step-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.step-indicator {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 34px;
  min-height: 30px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  font-size: 12px;
  font-weight: 900;
}

[data-step] {
  opacity: 0.32;
  filter: grayscale(0.4);
  transition: opacity 180ms ease, filter 180ms ease, transform 180ms ease, background 180ms ease, border-color 180ms ease;
}

[data-step].is-active {
  opacity: 1;
  filter: none;
  transform: translateY(-5px);
  border-color: var(--teal);
  background: #e2f2ee;
}

.active-coral[data-step].is-active,
[data-step].is-coral {
  border-color: var(--coral);
  background: #ffe6df;
}

.active-amber[data-step].is-active,
[data-step].is-amber {
  border-color: var(--amber);
  background: #fff6d8;
}

.kv-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 10px;
}

.kv {
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
}

.kv strong {
  display: block;
  margin-bottom: 4px;
  font-size: 13px;
}

.kv.is-hit {
  border-color: var(--teal);
  background: #e2f2ee;
}

.arrow-line {
  min-height: 0;
  width: auto;
  margin: 0;
  padding: 12px 14px 12px 34px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--ink);
  position: relative;
}

.arrow-line::after {
  content: "→";
  position: absolute;
  left: 12px;
  top: 11px;
  border: 0;
  color: var(--teal);
  font-weight: 800;
}

.tree-stage {
  align-items: center;
}

.tree-row {
  display: flex;
  justify-content: center;
  gap: 28px;
  width: 100%;
}

.tree-node {
  display: grid;
  place-items: center;
  min-width: 58px;
  min-height: 58px;
  padding: 10px;
  border: 2px solid var(--ink);
  border-radius: 999px;
  background: #fff;
  font-weight: 800;
}

.tree-node.muted {
  border-style: dashed;
  color: var(--muted);
}

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

.note {
  padding: 16px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
}

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

.array,
.row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  width: 62px;
  height: 62px;
  border: 2px solid var(--ink);
  border-radius: 6px;
  background: #fff;
  font: 700 22px/1 "Segoe UI", sans-serif;
}

.cell.small {
  width: 48px;
  height: 48px;
  font-size: 16px;
}

.tag {
  display: inline-flex;
  align-items: center;
  min-height: 32px;
  padding: 0 12px;
  border-radius: 999px;
  background: var(--ink);
  color: #fff;
  font-size: 13px;
  font-weight: 700;
}

.caption {
  margin-top: 16px;
  font-size: 14px;
  color: var(--muted);
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 18px;
}

.legend span {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--muted);
}

.dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--teal);
}

.dot.coral {
  background: var(--coral);
}

.dot.amber {
  background: var(--amber);
}

.index-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 14px;
}

.index-card {
  min-height: 132px;
  padding: 18px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  text-decoration: none;
  color: var(--ink);
  transition: transform 160ms ease, box-shadow 160ms ease;
}

.index-card:hover {
  transform: translateY(-3px);
  box-shadow: var(--shadow);
}

.index-card strong {
  display: block;
  margin-bottom: 8px;
  font-size: 18px;
}

.index-card span {
  color: var(--muted);
  line-height: 1.65;
}

.pointer {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: -34px;
  padding: 4px 8px;
  border-radius: 4px;
  background: var(--teal);
  color: #fff;
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
}

.pointer.coral {
  background: var(--coral);
}

.pointer.amber {
  background: var(--amber);
  color: var(--ink);
}

@media (max-width: 760px) {
  main {
    width: min(100vw - 20px, 680px);
    padding-top: 24px;
  }

  .board {
    min-height: 620px;
    padding: 18px;
  }

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

  .visual-layout {
    grid-template-columns: 1fr;
  }

  .cell {
    width: 48px;
    height: 48px;
    font-size: 18px;
  }
}

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