:root {
  --bg: #f7f9f5;
  --ink: #18201b;
  --muted: #657066;
  --line: #d7ded2;
  --panel: #ffffff;
  --green: #1d9a68;
  --red: #d74848;
  --blue: #2f6db3;
  --amber: #b7791f;
  --cell: clamp(30px, 2.8vw, 46px);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  color: var(--ink);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.82), rgba(247, 249, 245, 0.96)),
    repeating-linear-gradient(90deg, rgba(24, 32, 27, 0.04) 0 1px, transparent 1px 38px);
  font-family: "Avenir Next", "Trebuchet MS", sans-serif;
}

.app {
  min-height: 100vh;
  padding: 26px;
}

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

.eyebrow {
  margin: 0 0 6px;
  color: var(--green);
  font-weight: 800;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: clamp(28px, 4vw, 52px);
  line-height: 1;
}

.intro {
  max-width: 900px;
  margin: 10px 0 0;
  color: var(--muted);
  font-size: clamp(15px, 1.35vw, 19px);
  line-height: 1.35;
}

h2 {
  margin: 0 0 10px;
  font-size: 16px;
}

.step-readout {
  min-width: 118px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  background: var(--panel);
  text-align: right;
}

.step-readout span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
}

.step-readout strong {
  display: block;
  font-size: 34px;
}

.viewer,
.controls {
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.9);
}

.viewer {
  min-width: 0;
  overflow: hidden;
}

.shape-panel {
  display: grid;
  grid-template-columns: minmax(460px, 1fr) minmax(220px, 300px);
  gap: 26px;
  align-items: start;
  padding: 20px;
  border-bottom: 1px solid var(--line);
}

.shape-grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(58px, 1fr));
  gap: 2px;
  width: 100%;
  max-width: 760px;
  background: #2e3b32;
}

.shape-cell {
  position: relative;
  min-height: 78px;
  border: 0;
  overflow: hidden;
}

.shape-cell.boundary,
.shape-cell.interior {
  background: #fff;
}

.shape-cell.boundary {
  border: 2px solid #2e3b32;
}

.shape-cell.interior {
  background: #f6f8f1;
  border: 2px solid #2e3b32;
}

.shape-cell.candidate {
  background: #dff3e7;
}

.shape-cell.leader-cell {
  background: #ffd9d9;
  border-color: var(--red);
}

.cell-index {
  position: absolute;
  top: 2px;
  left: 4px;
  color: var(--muted);
  font-family: "Menlo", "Consolas", monospace;
  font-size: 9px;
  font-weight: 800;
  z-index: 2;
}

.register-grid {
  position: absolute;
  inset: 20px 5px 6px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: repeat(3, minmax(17px, 1fr));
  border: 1px solid rgba(46, 59, 50, 0.16);
}

.register-slot {
  display: flex;
  min-width: 0;
  align-items: center;
  justify-content: center;
  border-right: 1px solid rgba(46, 59, 50, 0.16);
  border-bottom: 1px solid rgba(46, 59, 50, 0.16);
}

.register-slot:nth-child(2n) {
  border-right: 0;
}

.register-slot:nth-child(n + 5) {
  border-bottom: 0;
}

.register-slot.signal-register {
  grid-column: 1 / -1;
  border-right: 0;
  border-bottom: 0;
}

.register-slot.pending,
#grid td.pending {
  background: rgba(255, 218, 102, 0.28);
  box-shadow: inset 0 0 0 2px rgba(215, 72, 72, 0.28);
}

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

.legend p {
  margin: 8px 0;
}

.linear-panel {
  border-bottom: 1px solid var(--line);
}

.table-title {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 18px;
  padding: 16px 20px 0;
  color: var(--muted);
}

.table-title h2 {
  color: var(--ink);
}

.table-wrap {
  overflow-x: auto;
  padding: 12px 20px 22px;
}

table {
  border-collapse: collapse;
  table-layout: fixed;
}

th,
td {
  width: var(--cell);
  min-width: var(--cell);
  height: 32px;
  border: 1px solid var(--line);
  text-align: center;
  vertical-align: middle;
}

th {
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
}

th.g-right-line,
td.g-right-line {
  border-right: 4px solid rgba(29, 154, 104, 0.95);
}

td.label {
  position: sticky;
  left: 0;
  z-index: 2;
  width: 116px;
  min-width: 116px;
  padding: 0 10px;
  text-align: left;
  color: var(--muted);
  background: #fff;
  font-size: 12px;
  font-weight: 800;
}

.token {
  display: inline-grid;
  place-items: center;
  min-width: 22px;
  height: 22px;
  padding: 0 4px;
  font-family: "Menlo", "Consolas", monospace;
  font-size: 14px;
  font-weight: 800;
}

.shape-token {
  min-width: 15px;
  height: 15px;
  padding: 0 3px;
  font-size: 9px;
  line-height: 1;
}

.token.symbol {
  color: var(--ink);
}

.token.signal {
  color: #fff;
  background: var(--blue);
}

.token.mark {
  color: #fff;
  background: var(--green);
}

.token.leader {
  color: #fff;
  background: var(--red);
}

.token.shadow {
  color: #8b948c;
  font-style: italic;
  transform: skew(-8deg);
}

.token.highlight-red {
  outline: 0;
}

.token.highlight-green {
  outline: 0;
}

.show-script-highlights .token.highlight-red {
  outline: 2px solid var(--red);
}

.show-script-highlights .token.highlight-green {
  outline: 2px solid var(--green);
}

.viewer.animating .register-slot > .token,
.viewer.animating #grid td[data-pos]:not([data-slot="0"]) > .token {
  opacity: 0;
}

.motion-layer {
  position: fixed;
  inset: 0;
  z-index: 20;
  pointer-events: none;
}

.motion-token {
  position: fixed;
  margin: 0;
  transition:
    transform var(--motion-duration, 620ms) cubic-bezier(0.2, 0.75, 0.2, 1),
    opacity 240ms ease,
    scale 240ms ease;
  will-change: transform, opacity, scale;
}

.motion-token.appearing {
  opacity: 0;
  scale: 0.72;
}

.motion-token.appearing.shown {
  opacity: 1;
  scale: 1;
}

.motion-token.disappearing {
  opacity: 1;
  scale: 1;
}

.motion-token.disappearing.gone {
  opacity: 0;
  scale: 0.72;
}

.note {
  height: 178px;
  overflow-y: auto;
  padding: 16px 20px;
  background: #ecf6ef;
  font-size: clamp(14px, 1.25vw, 18px);
  line-height: 1.35;
}

.note-title {
  margin-bottom: 6px;
  color: var(--green);
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.note-intro {
  margin: 0 0 8px;
  font-weight: 700;
}

.event-list {
  margin: 0;
  padding-left: 20px;
}

.event-list li {
  margin: 4px 0;
}

.controls {
  display: grid;
  grid-template-columns: repeat(5, 44px) minmax(160px, 1fr) minmax(120px, 220px);
  gap: 8px;
  align-items: center;
  margin: 0 0 18px;
  padding: 12px;
}

.range-control {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
}

.toggle-control {
  display: flex;
  gap: 8px;
  align-items: center;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
}

button {
  height: 40px;
  border: 1px solid #b8c2b5;
  background: #fff;
  color: var(--ink);
  font-weight: 900;
  cursor: pointer;
}

button:hover {
  border-color: var(--green);
}

input[type="range"] {
  width: 100%;
  accent-color: var(--green);
}

@media (max-width: 900px) {
  .app {
    padding: 16px;
  }

  .shape-panel {
    grid-template-columns: 1fr;
  }

  .shape-grid {
    grid-template-columns: repeat(7, minmax(46px, 1fr));
  }

  .shape-cell {
    min-height: 68px;
  }

  .shape-token {
    min-width: 14px;
    font-size: 8px;
  }

  .table-title {
    display: block;
  }

  .controls {
    grid-template-columns: repeat(5, 40px);
  }

  .range-control,
  .toggle-control {
    grid-column: 1 / -1;
  }
}
