:root {
  --bg: #f0fdfa;
  --text: #134e4a;
  --muted: #3f6f6a;
  --card: #ffffff;
  --line: #d7ebe7;
  --field: #b7d9d4;
  --primary: #0f766e;
  --primary-ink: #115e59;
  --cta: #c2410c;
  --cta-ink: #9a3412;
  --paper: #f4fbf9;
  --dot: #d5ebe7;
  --red: #dc2626;
  --red-ink: #7f1d1d;
  --ink: #1c1917;
  color-scheme: light;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 16px;
  line-height: 1.5;
}

button,
input {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

button:disabled {
  cursor: default;
  opacity: 0.45;
}

[hidden] {
  display: none !important;
}

:focus-visible {
  outline: 2px solid var(--cta);
  outline-offset: 2px;
}

.wrap {
  max-width: 1360px;
  margin: 0 auto;
  padding: 16px 20px 20px;
}

.top {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
  flex: none;
}

.logo {
  width: 48px;
  height: 48px;
  flex: none;
}

.logo line {
  stroke: #57534e;
  stroke-width: 2;
}

.logo .ink {
  fill: var(--ink);
  stroke: var(--ink);
  stroke-width: 2;
}

.logo .red {
  fill: var(--red);
  stroke: var(--red-ink);
  stroke-width: 2;
}

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: 24px;
  line-height: 1.2;
  font-weight: 700;
}

.top p,
.hint,
.definition,
#stage-status,
.empty-hint {
  color: var(--muted);
}

.top p {
  margin-top: 4px;
  font-size: 15px;
}

.layout {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr) 320px;
  grid-template-areas: "controls stage explain";
  gap: 14px;
  align-items: stretch;
}

.controls { grid-area: controls; }
.stage { grid-area: stage; }
.explain { grid-area: explain; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: 0 10px 30px rgba(19, 78, 74, 0.06);
}

.controls,
.explain,
.stage {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.controls,
.explain {
  padding: 16px;
}

.controls h2,
.explain h2,
.section-title {
  font-size: 15px;
  font-weight: 700;
}

.section-title {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}

.op-form {
  margin-top: 10px;
}

.op-form label {
  width: 2.5em;
  flex: none;
  align-self: center;
  font-size: 14px;
  font-weight: 600;
}

.row {
  display: flex;
  gap: 8px;
}

.row input {
  flex: 1;
  min-width: 0;
  height: 40px;
  padding: 0 12px;
  border: 1px solid var(--field);
  border-radius: 10px;
  background: #fff;
}

.row input[aria-invalid="true"] {
  border-color: #b91c1c;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 40px;
  padding: 0 12px;
  border-radius: 10px;
  border: 1px solid transparent;
  background: #fff;
  transition: background-color 200ms ease, border-color 200ms ease, color 200ms ease;
}

.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.btn-primary:hover {
  background: var(--primary-ink);
  border-color: var(--primary-ink);
}

.btn-cta {
  background: var(--cta);
  border-color: var(--cta);
  color: #fff;
}

.btn-cta:hover {
  background: var(--cta-ink);
  border-color: var(--cta-ink);
}

.btn-ghost {
  background: #fff;
  border-color: var(--field);
  color: var(--text);
}

.btn-ghost:hover {
  background: var(--bg);
  border-color: var(--primary);
}

.btn-block {
  width: 100%;
  margin-top: 4px;
}

.form-error {
  margin: 8px 0 0;
  font-size: 14px;
  color: #b91c1c;
}

.form-error:empty {
  display: none;
}

.presets {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 10px;
}

.presets .wide {
  grid-column: 1 / -1;
}

.presets .btn {
  white-space: normal;
  line-height: 1.3;
  padding: 8px;
}

.rules,
.legend,
.step-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.rules {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 14px;
}

.rules li,
.legend li {
  font-size: 13px;
  color: var(--muted);
}

.playback {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 8px;
  width: min(440px, 100%);
}

.playback .btn {
  white-space: nowrap;
  padding: 0 8px;
  font-size: 14px;
}

.icon {
  width: 18px;
  height: 18px;
  display: block;
}

.hint,
.definition {
  font-size: 13px;
  line-height: 1.5;
}

.definition {
  margin-top: 4px;
}

.hint {
  margin-top: 8px;
  text-align: center;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px 14px;
  width: 100%;
  padding: 4px 12px 8px;
}

.legend li {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0;
}

.swatch {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid var(--ink);
  background: #fff;
  flex: none;
}

.swatch.red { background: var(--red); border-color: var(--red-ink); }
.swatch.black { background: var(--ink); border-color: var(--ink); }
.swatch.path { background: #fff; border-color: var(--primary); }
.swatch.uncle { background: #fff; border-color: #7c3aed; }
.swatch.sibling { background: #fff; border-color: #0369a1; }
.swatch.successor { background: #fff; border-color: #0e7490; }
.swatch.double {
  background: var(--ink);
  border-color: #d97706;
  box-shadow: inset 0 0 0 2px #fff;
}

.stage {
  padding: 12px 14px 10px;
  min-width: 0;
}

.stage-foot {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 8px;
}

.stage-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  min-height: 36px;
  padding: 4px 6px 10px;
}

#step-count {
  font-weight: 700;
}

#stage-status {
  margin-left: auto;
  font-size: 14px;
}

.badge {
  margin: 0;
  padding: 2px 8px;
  border-radius: 999px;
  background: #ffedd5;
  color: #9a3412;
  border: 1px solid #fdba74;
  font-size: 13px;
  font-weight: 700;
}

.stage-scroll {
  display: flex;
  flex: 1;
  min-height: 280px;
  overflow: auto;
  border-radius: 12px;
  background-color: var(--paper);
  background-image: radial-gradient(var(--dot) 1.2px, transparent 1.2px);
  background-size: 20px 20px;
}

.stage-sheet {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: none;
  width: max-content;
  margin: auto;
}

.empty-hint {
  padding: 88px 24px;
  text-align: center;
}

#tree {
  display: block;
  flex: none;
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}

.node {
  transition: transform 280ms ease;
}

.node.no-trans {
  transition: none;
}

.node .face {
  stroke-width: 2;
  transition: fill 200ms ease, stroke 200ms ease;
}

.node[data-color="red"] .face {
  fill: var(--red);
  stroke: var(--red-ink);
}

.node[data-color="black"] .face {
  fill: var(--ink);
  stroke: #44403c;
}

.node .halo,
.node .ring {
  fill: none;
  stroke-width: 2;
  opacity: 0;
  pointer-events: none;
}

.node .key {
  fill: #fff;
  font-size: 16px;
  font-weight: 700;
}

.node .tag {
  fill: var(--muted);
  font-size: 12px;
  font-weight: 700;
  paint-order: stroke fill;
  stroke: var(--paper);
  stroke-width: 5px;
  stroke-linejoin: round;
}

.node[data-state="path"] .face,
.node[data-state="current"] .face,
.node[data-state="parent"] .face {
  stroke: var(--primary);
  stroke-width: 4px;
}

.node[data-state="current"] .face,
.node[data-state="parent"] .face {
  stroke-width: 5px;
}

.node[data-state="inserted"] .face,
.node[data-state="found"] .face,
.node[data-state="replaced"] .face {
  stroke: #d97706;
  stroke-width: 5px;
}

.node[data-state="inserted"] .tag,
.node[data-state="replaced"] .tag { fill: #b45309; }

.node[data-state="uncle"] .face {
  stroke: #7c3aed;
  stroke-width: 5px;
}

.node[data-state="uncle"] .tag { fill: #6d28d9; }

.node[data-state="sibling"] .face {
  stroke: #0369a1;
  stroke-width: 5px;
}

.node[data-state="sibling"] .tag { fill: #0369a1; }

.node[data-state="grand"] .face {
  stroke: var(--cta);
  stroke-width: 5px;
}

.node[data-state="grand"] .tag { fill: #9a3412; }

.node[data-state="root"] .face {
  stroke: #d97706;
  stroke-width: 5px;
}

.node[data-state="root"] .tag { fill: #b45309; }

.node[data-state="parent"] .tag { fill: var(--primary-ink); }

.node[data-state="pivot"] .face,
.node[data-state="promoted"] .face {
  stroke: #15803d;
  stroke-width: 5px;
}

.node[data-state="pivot"] .tag,
.node[data-state="promoted"] .tag { fill: #166534; }

.node[data-state="successor"] .face {
  stroke: #0e7490;
  stroke-width: 5px;
}

.node[data-state="successor"] .tag { fill: #0e7490; }

.node[data-state="deleted"] .face {
  stroke: #b91c1c;
  stroke-width: 5px;
  stroke-dasharray: 4 3;
}

.node[data-state="deleted"] .tag { fill: #b91c1c; }

.node[data-state="double"] .halo {
  opacity: 1;
  stroke: #d97706;
  stroke-width: 3px;
}

.node[data-state="double"] .ring {
  opacity: 1;
  stroke: #fff;
  stroke-width: 2px;
}

.node[data-state="double"] .face {
  stroke: #d97706;
  stroke-width: 3px;
}

.node[data-state="double"] .tag { fill: #b45309; }

.edge {
  stroke: #7a9c98;
  stroke-width: 2;
  stroke-linecap: round;
  transition: x1 280ms ease, y1 280ms ease, x2 280ms ease, y2 280ms ease, stroke 200ms ease;
}

.edge.is-hot {
  stroke: var(--primary);
  stroke-width: 2.5;
}

#step-title {
  margin-top: 12px;
  font-size: 18px;
  font-weight: 700;
}

#step-detail {
  margin-top: 6px;
  font-size: 15px;
  line-height: 1.6;
}

.step-list {
  flex: 1;
  min-height: 0;
  margin-top: 12px;
  overflow: auto;
  border-top: 1px solid var(--line);
}

.step-btn {
  width: 100%;
  margin-top: 4px;
  padding: 8px 10px;
  border: 0;
  border-left: 3px solid transparent;
  border-radius: 0 8px 8px 0;
  background: transparent;
  text-align: left;
  line-height: 1.4;
  transition: background-color 200ms ease, border-color 200ms ease;
}

.step-btn:hover {
  background: var(--bg);
}

.step-btn[aria-current="step"] {
  background: var(--bg);
  border-left-color: var(--primary);
  font-weight: 700;
}

@media (min-width: 1100px) {
  html,
  body {
    height: 100%;
    overflow: hidden;
  }

  .wrap {
    height: 100%;
    display: flex;
    flex-direction: column;
  }

  .layout {
    flex: 1;
    min-height: 0;
  }

  .controls {
    overflow: auto;
  }

  .stage,
  .explain {
    overflow: hidden;
  }
}

@media (max-width: 1099px) {
  html,
  body {
    height: auto;
    overflow: auto;
  }

  .wrap {
    height: auto;
    display: block;
  }

  .layout {
    grid-template-columns: 1fr;
    grid-template-areas:
      "stage"
      "explain"
      "controls";
  }

  .stage-scroll {
    min-height: 360px;
    flex: none;
  }

  .step-list {
    flex: none;
    max-height: 320px;
  }
}

@media (max-width: 720px) {
  .wrap {
    padding: 14px 12px 28px;
  }

  h1 {
    font-size: 22px;
  }

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

  #stage-status {
    margin-left: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .node,
  .node .face,
  .edge,
  .btn,
  .step-btn {
    transition: none;
  }
}
