:root {
  color-scheme: light;
  --ink: #15365f;
  --ink-soft: #5a6c82;
  --paper: #fffefa;
  --sky: #eef9fc;
  --grid: #7bd9f3;
  --yellow: #ffd84d;
  --yellow-soft: #fff3bd;
  --blue: #2b8fd5;
  --green: #76bd63;
  --coral: #f47c61;
  --line: #d5e4e9;
  --shadow: 0 18px 52px rgba(21, 54, 95, 0.13);
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  overflow: hidden;
  background: var(--sky);
  color: var(--ink);
  font-family: "PingFang SC", "Microsoft YaHei", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input {
  font: inherit;
}

button,
a {
  -webkit-tap-highlight-color: transparent;
}

button {
  color: inherit;
}

[hidden] {
  display: none !important;
}

.app-shell {
  min-height: 100dvh;
}

.main-content {
  display: flex;
  min-height: 100dvh;
  flex-direction: column;
  align-items: center;
  padding: clamp(6px, 1.2vh, 12px) 12px 78px;
}

.lesson-tip {
  display: flex;
  width: min(100%, 920px);
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-height: 32px;
  margin-bottom: clamp(6px, 1vh, 10px);
  color: var(--ink-soft);
  text-align: center;
}

.tip-label {
  flex: 0 0 auto;
  padding: 5px 9px;
  border-radius: 999px;
  background: #dff3fc;
  color: #21658e;
  font-size: 12px;
  font-weight: 900;
}

.lesson-tip p {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
}

.board-section {
  display: grid;
  width: 100%;
  flex: 1;
  place-items: center;
  min-height: 0;
}

.tangram-reference {
  display: none;
}

body[data-layout-state="state3"] .board-section {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(30px, 4.4vw, 62px);
}

body[data-layout-state="state3"] .tangram-reference {
  display: block;
  width: clamp(237px, 23.33vw, 317px);
  aspect-ratio: 1;
  flex: 0 0 auto;
  transform: translateX(clamp(-86px, -3.2vw, -36px));
  filter: drop-shadow(0 10px 18px rgba(21, 54, 95, 0.12));
}

.tangram-reference svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
  shape-rendering: geometricPrecision;
}

.tangram-ref-red {
  fill: #ef4047;
}

.tangram-ref-orange {
  fill: #f6a331;
}

.tangram-ref-cyan {
  fill: #20b9cf;
}

.tangram-ref-green {
  fill: #92c95a;
}

.tangram-ref-pink {
  fill: #eb7bb0;
}

.tangram-ref-purple {
  fill: #8d4aa2;
}

.tangram-ref-yellow {
  fill: #eef173;
}

.board-frame {
  position: relative;
  flex: 0 0 auto;
  width: min(calc(100vw - 20px), var(--board-height-fit-width, calc((100dvh - 136px) + (100dvh - 136px))), 1320px);
  min-width: 280px;
  aspect-ratio: var(--board-aspect, 2 / 1);
  overflow: hidden;
  border: 2px solid rgba(21, 54, 95, 0.18);
  border-radius: 18px;
  background: #fff;
  box-shadow: var(--shadow);
}

body[data-layout-state="state3"] .board-frame {
  width: min(calc(100vw - 40px), var(--board-height-fit-width, 1180px), 1180px);
}

#patternCanvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: default;
  touch-action: none;
  user-select: none;
}

#patternCanvas:focus-visible,
button:focus-visible,
input:focus-visible {
  outline: 3px solid #217fd3;
  outline-offset: 3px;
}

.floating-tools {
  position: fixed;
  bottom: 22px;
  left: clamp(18px, 3vw, 42px);
  z-index: 20;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  max-width: calc(100vw - 36px);
}

.state-switch {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  min-height: 48px;
  padding: 4px;
  border: 2px solid var(--ink);
  border-radius: 15px;
  background: #fff;
  box-shadow: 4px 4px 0 var(--ink);
}

.state-button {
  min-width: 66px;
  min-height: 36px;
  padding: 0 12px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  cursor: pointer;
  font-size: 14px;
  font-weight: 1000;
  transition: transform 160ms ease, background 160ms ease;
}

.state-button:hover {
  transform: translateY(-1px);
  background: #edf7fb;
}

.state-button[aria-pressed="true"] {
  background: var(--yellow);
}

.labels-button,
.numbers-button,
.record-button,
.reset-button,
.history-button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 48px;
  padding: 0 18px;
  border: 2px solid var(--ink);
  border-radius: 15px;
  background: #fff;
  box-shadow: 4px 4px 0 var(--ink);
  cursor: pointer;
  font-size: 16px;
  font-weight: 1000;
  flex: 0 0 auto;
  white-space: nowrap;
  transition: transform 160ms ease, box-shadow 160ms ease, background 160ms ease;
}

.covered-shape-selector {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 48px;
  padding: 0 10px 0 14px;
  border: 2px solid var(--ink);
  border-radius: 15px;
  background: #fff;
  box-shadow: 4px 4px 0 var(--ink);
  flex: 0 0 auto;
  white-space: nowrap;
}

.covered-shape-selector[hidden] {
  display: none;
}

.covered-shape-label {
  color: var(--ink-soft);
  font-size: 14px;
  font-weight: 1000;
}

.covered-shape-buttons {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.covered-shape-button {
  display: grid;
  width: 46px;
  height: 34px;
  place-items: center;
  padding: 0;
  border: 2px solid #cddce2;
  border-radius: 11px;
  background: #fff;
  cursor: pointer;
  font-size: 18px;
  font-weight: 1000;
  line-height: 1;
  transition: transform 140ms ease, border-color 140ms ease, background 140ms ease, box-shadow 140ms ease;
}

.covered-shape-button:hover {
  transform: translateY(-1px);
  border-color: var(--ink);
}

.covered-shape-button[aria-pressed="true"] {
  border-color: var(--ink);
  background: var(--yellow);
  box-shadow: 2px 2px 0 var(--ink);
}

.covered-shape-thumb {
  display: block;
  width: 38px;
  height: 27px;
  pointer-events: none;
}

.labels-button:hover,
.numbers-button:hover,
.record-button:hover,
.reset-button:hover,
.history-button:hover:not(:disabled) {
  transform: translate(-1px, -1px);
  box-shadow: 5px 5px 0 var(--ink);
}

.labels-button[aria-pressed="true"],
.numbers-button[aria-pressed="true"],
#programButton[aria-expanded="true"] {
  background: var(--yellow);
}

.record-button,
.reset-button {
  padding: 0 16px;
}

.history-button {
  display: grid;
  width: 50px;
  place-items: center;
  padding: 0;
}

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

.history-button svg {
  display: block;
  width: 25px;
  height: 25px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2.25;
}

.vertex-icon {
  display: grid;
  width: 27px;
  height: 27px;
  flex: 0 0 auto;
  place-items: center;
}

.vertex-icon svg {
  display: block;
  width: 100%;
  height: 100%;
}

.vertex-icon path {
  fill: rgba(123, 217, 243, 0.2);
  stroke: currentColor;
  stroke-linejoin: round;
  stroke-width: 2.6;
}

.vertex-icon circle {
  fill: var(--blue);
  stroke: #fff;
  stroke-width: 1.8;
}

.symbol-hint {
  position: fixed;
  top: max(0px, calc(clamp(6px, 1.2vh, 12px) - 8px));
  right: clamp(18px, 3vw, 42px);
  z-index: 15;
  display: inline-flex;
  align-items: center;
  gap: 9px;
  min-height: 48px;
  max-width: min(390px, calc(100vw - 170px));
  padding: 0 16px;
  border: 1px solid #d9e5e9;
  border-radius: 15px;
  background: rgba(255, 255, 255, 0.94);
  box-shadow: 0 8px 26px rgba(21, 54, 95, 0.1);
  color: var(--ink-soft);
  font-size: 13px;
  font-weight: 800;
}

.mouse-mark {
  display: grid;
  width: 30px;
  height: 30px;
  flex: 0 0 auto;
  place-items: center;
  border: 2px solid var(--ink);
  border-radius: 10px;
  color: var(--ink);
  font-size: 11px;
  font-weight: 1000;
}

.pen-cluster {
  display: inline-flex;
  align-items: center;
  flex: 0 0 auto;
  gap: 7px;
  min-height: 48px;
  padding: 4px;
  border: 2px solid var(--ink);
  border-radius: 15px;
  background: #fff;
  box-shadow: 4px 4px 0 var(--ink);
}

.pen-cluster .history-button {
  width: 36px;
  min-height: 36px;
  border: 0;
  border-radius: 8px;
  box-shadow: none;
}

.pen-cluster .history-button[aria-pressed="true"] {
  background: var(--yellow);
}

.pen-swatch {
  width: 30px;
  height: 30px;
  flex: 0 0 auto;
  padding: 0;
  border: 2px solid var(--ink);
  border-radius: 50%;
  cursor: pointer;
}

.pen-swatch.red { background: #e84855; }
.pen-swatch.green { background: #25a95f; }
.pen-swatch.blue { background: #247ce8; }
.pen-swatch.is-selected {
  outline: 3px solid var(--yellow);
  outline-offset: 2px;
}

.help-dialog {
  position: fixed;
  inset: auto;
  margin: 0;
  width: min(420px, calc(100vw - 24px));
  max-height: var(--help-max-height, calc(100dvh - 24px));
  padding: 0;
  overflow: visible;
  color: var(--ink);
  border: 2px solid var(--ink);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 6px 24px rgb(0 0 0 / 16%);
}

.help-dialog::after {
  content: "";
  position: absolute;
  left: var(--help-arrow-left, 24px);
  bottom: -7px;
  width: 12px;
  height: 12px;
  background: #fff;
  border-right: 2px solid var(--ink);
  border-bottom: 2px solid var(--ink);
  transform: rotate(45deg);
}

.help-dialog[data-placement="below"]::after {
  top: -7px;
  bottom: auto;
  transform: rotate(225deg);
}

.help-dialog .dialog-header {
  border-radius: 6px 6px 0 0;
  padding: 10px 14px;
}

.help-dialog h2 {
  margin: 0;
  font-size: 18px;
}

.help-content {
  margin: 0;
  padding: 0 16px 16px;
  max-height: calc(var(--help-max-height, 100dvh - 24px) - 54px);
  overflow-y: auto;
  font-size: 14px;
  line-height: 1.7;
}

.help-content dt {
  margin-top: 16px;
  font-weight: 700;
}

.help-content dd {
  margin: 4px 0 0;
  overflow-wrap: anywhere;
}

.transform-dialog {
  position: fixed;
  inset: auto;
  left: var(--bubble-left, 50vw);
  top: var(--bubble-top, 50vh);
  width: 306px;
  max-width: calc(100vw - 24px);
  max-height: calc(100dvh - 24px);
  margin: 0;
  padding: 0;
  overflow: visible;
  border: 0;
  background: transparent;
  color: var(--ink);
}

.transform-dialog::backdrop {
  background: transparent;
}

.dialog-card {
  position: relative;
  max-height: calc(100dvh - 24px);
  overflow: visible;
  border: 2px solid var(--ink);
  border-radius: 18px;
  background: var(--paper);
  box-shadow: 5px 5px 0 rgba(21, 54, 95, 0.9), 0 16px 36px rgba(21, 54, 95, 0.2);
}

.dialog-card::after {
  position: absolute;
  width: 16px;
  height: 16px;
  border: 2px solid var(--ink);
  background: var(--paper);
  content: "";
  transform: rotate(45deg);
}

.transform-dialog[data-placement="above"] .dialog-card::after {
  left: var(--bubble-tail-x, 50%);
  bottom: -9px;
  border-top: 0;
  border-left: 0;
  transform: translateX(-50%) rotate(45deg);
}

.transform-dialog[data-placement="below"] .dialog-card::after {
  left: var(--bubble-tail-x, 50%);
  top: -9px;
  border-right: 0;
  border-bottom: 0;
  transform: translateX(-50%) rotate(45deg);
}

.transform-dialog[data-placement="left"] .dialog-card::after {
  right: -9px;
  top: var(--bubble-tail-y, 50%);
  border-left: 0;
  border-bottom: 0;
  transform: translateY(-50%) rotate(45deg);
}

.transform-dialog[data-placement="right"] .dialog-card::after {
  left: -9px;
  top: var(--bubble-tail-y, 50%);
  border-top: 0;
  border-right: 0;
  transform: translateY(-50%) rotate(45deg);
}

.dialog-header {
  display: flex;
  min-height: 38px;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 7px 9px 6px 13px;
  border-bottom: 1px solid rgba(21, 54, 95, 0.1);
  border-radius: 16px 16px 0 0;
  background: var(--yellow-soft);
}

.dialog-kicker {
  margin: 0;
  color: #7d651a;
  font-size: 12px;
  font-weight: 1000;
}

.close-button {
  display: grid;
  width: 26px;
  height: 26px;
  flex: 0 0 auto;
  place-items: center;
  padding: 0;
  border: 1px solid rgba(21, 54, 95, 0.18);
  border-radius: 50%;
  background: #fff;
  cursor: pointer;
  font-size: 17px;
  line-height: 1;
}

.dialog-body {
  display: grid;
  gap: 10px;
  padding: 10px 12px 12px;
}

.dialog-step {
  margin: 0;
}

.mode-options,
.rotation-options {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 7px;
}

.mode-option,
.rotation-option,
.direction-option,
.point-option,
.apply-button {
  border: 2px solid #cddce2;
  border-radius: 12px;
  background: #fff;
  cursor: pointer;
  font-weight: 1000;
  transition: transform 140ms ease, border-color 140ms ease, background 140ms ease, box-shadow 140ms ease;
}

.mode-option {
  display: flex;
  min-height: 42px;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 6px 8px;
  font-size: 13px;
}

.rotation-option,
.point-option,
.direction-option {
  display: grid;
  place-items: center;
  font-family: Georgia, "Times New Roman", serif;
}

.mode-option:hover,
.rotation-option:hover,
.direction-option:hover,
.point-option:hover,
.apply-button:hover:not(.is-disabled):not(:disabled) {
  transform: translateY(-1px);
  border-color: var(--ink);
}

.mode-option[aria-pressed="true"],
.rotation-option[aria-pressed="true"],
.direction-option[aria-pressed="true"],
.point-option[aria-pressed="true"] {
  border-color: var(--ink);
  background: var(--yellow);
  box-shadow: 2px 2px 0 var(--ink);
}

.mode-icon {
  font-family: Georgia, "Times New Roman", serif;
  font-size: 25px;
  font-weight: 700;
  line-height: 1;
}

.transform-panel {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 10px;
}

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

#rotatePanel {
  grid-template-columns: 1fr;
  gap: 8px;
}

.pivot-step {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 7px;
}

.inline-label {
  color: var(--ink-soft);
  font-size: 13px;
  font-weight: 1000;
  white-space: nowrap;
}

.direction-pad {
  display: grid;
  grid-template-columns: repeat(3, 34px);
  grid-template-rows: repeat(3, 34px);
  justify-content: start;
  gap: 5px;
}

.direction-option {
  font-size: 21px;
}

.direction-option.up { grid-column: 2; grid-row: 1; }
.direction-option.left { grid-column: 1; grid-row: 2; }
.direction-center {
  grid-column: 2;
  grid-row: 2;
  border-radius: 10px;
  background: #edf5f7;
}
.direction-option.right { grid-column: 3; grid-row: 2; }
.direction-option.down { grid-column: 2; grid-row: 3; }

.compact-step {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 7px;
}

.compact-step label {
  color: var(--ink-soft);
  font-size: 12px;
  font-weight: 1000;
}

.number-field {
  position: relative;
  display: grid;
  grid-template-columns: 32px 70px 32px;
  align-items: center;
  gap: 5px;
}

.number-field input {
  width: 70px;
  height: 36px;
  padding: 0 8px;
  border: 2px solid #cddce2;
  border-radius: 10px;
  background: #fff;
  color: var(--ink);
  font-size: 15px;
  font-weight: 900;
  text-align: center;
  caret-color: var(--ink);
}

.number-stepper {
  display: grid;
  width: 32px;
  height: 34px;
  place-items: center;
  border: 2px solid #cddce2;
  border-radius: 11px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
  font-size: 20px;
  font-weight: 1000;
  line-height: 1;
}

.number-stepper:hover,
.number-stepper:active {
  border-color: var(--ink);
  background: var(--yellow);
}

.angle-unit {
  position: absolute;
  top: 4px;
  right: 42px;
  color: var(--ink-soft);
  font-size: 18px;
  font-weight: 1000;
  line-height: 1;
  pointer-events: none;
}

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

.point-option {
  width: 100%;
  height: 36px;
  font-size: 20px;
  font-style: italic;
}

.rotation-options {
  min-width: 0;
}

.rotation-option {
  min-height: 36px;
}

.curved-arrow {
  display: inline-block;
  width: 54px;
  height: 26px;
  color: #202933;
  transform-origin: center;
}

.curved-arrow svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
}

.curved-arrow .arrow-line {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
}

.curved-arrow .arrow-marker path {
  fill: currentColor;
  stroke: none;
}

.curved-arrow.ccw {
  transform: scaleX(-1);
}

.formula-arrow .curved-arrow {
  width: 48px;
  height: 23px;
}

.result-area {
  padding: 7px;
  border: 2px solid #edd27c;
  border-radius: 13px;
  background: #fff4ca;
}

.formula-row {
  display: grid;
  grid-template-columns: 1fr 38px;
  align-items: center;
  gap: 7px;
}

.formula-result {
  min-width: 0;
  min-height: 34px;
  overflow: hidden;
  font-family: "STIX Two Math", "STIXGeneral", "Times New Roman", serif;
  font-size: 26px;
  font-style: normal;
  line-height: 1.15;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.formula-arrow {
  display: inline-block;
  margin: 0 0.08em;
  transform-origin: center;
  vertical-align: -0.15em;
}

.apply-button {
  position: relative;
  z-index: 2;
  display: grid;
  width: 38px;
  height: 34px;
  place-items: center;
  border-color: var(--green);
  background: #edfae9;
  color: #2b6d23;
  font-size: 20px;
  pointer-events: auto;
  touch-action: manipulation;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.apply-button:not(.is-disabled):not(:disabled) {
  box-shadow: 2px 2px 0 #2b6d23;
}

.apply-button.is-disabled,
.apply-button:disabled {
  cursor: not-allowed;
  opacity: 0.42;
}

.result-area > span {
  display: block;
  margin-top: 4px;
  color: #7a682e;
  font-size: 11px;
  font-weight: 800;
  text-align: center;
}

.result-area.error {
  border-color: #eeab9c;
  background: #fff0ec;
}

.number-pad {
  position: fixed;
  left: var(--number-pad-left, 50vw);
  top: var(--number-pad-top, 50vh);
  z-index: 50;
  width: 148px;
  padding: 8px;
  border: 2px solid var(--ink);
  border-radius: 14px;
  background: #fffefa;
  box-shadow: 4px 4px 0 rgba(21, 54, 95, 0.9), 0 12px 26px rgba(21, 54, 95, 0.18);
}

.number-pad[hidden] {
  display: none;
}

.number-pad-display {
  display: grid;
  min-height: 30px;
  margin-bottom: 6px;
  place-items: center;
  border: 2px solid #cddce2;
  border-radius: 10px;
  background: #f7fbfd;
  color: var(--ink);
  font-size: 18px;
  font-weight: 1000;
}

.number-pad-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 5px;
}

.number-pad-grid button {
  display: grid;
  min-height: 30px;
  place-items: center;
  border: 1.5px solid #cddce2;
  border-radius: 9px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
  font-size: 14px;
  font-weight: 1000;
}

.number-pad-grid button:hover,
.number-pad-grid button:active {
  border-color: var(--ink);
  background: #edf7fb;
}

.number-pad-grid .number-pad-confirm {
  grid-column: span 2;
  background: #edfae9;
  color: #2b6d23;
}

.number-pad:not(.allows-decimal) .decimal-key {
  visibility: hidden;
  pointer-events: none;
}

.transform-dialog.is-running .dialog-body button,
.transform-dialog.is-running .dialog-body input,
.transform-dialog.is-running .close-button {
  pointer-events: none;
  opacity: 0.55;
}

.transform-dialog.is-running .formula-result {
  animation: formula-pulse 520ms ease-in-out infinite alternate;
}

@keyframes formula-pulse {
  from { transform: scale(1); }
  to { transform: scale(1.035); }
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (max-width: 700px) {
  body {
    overflow: auto;
  }

  .main-content {
    min-height: 100dvh;
    padding: 10px 10px 138px;
  }

  .lesson-tip {
    align-items: flex-start;
    margin-bottom: 9px;
    text-align: left;
  }

  .lesson-tip p {
    font-size: 12px;
    line-height: 1.45;
  }

  .board-frame {
    width: calc(100vw - 20px);
    border-radius: 12px;
  }

  .floating-tools {
    bottom: 76px;
    left: 12px;
    gap: 8px;
    max-width: calc(100vw - 24px);
  }

  .state-switch,
  .labels-button,
  .numbers-button,
  .record-button,
  .reset-button,
  .history-button,
  .covered-shape-selector {
    min-height: 44px;
  }

  .state-button {
    min-width: 56px;
    padding: 0 9px;
    font-size: 13px;
  }

  .labels-button,
  .numbers-button,
  .record-button,
  .reset-button {
    padding: 0 14px;
  }

  .history-button {
    width: 44px;
    padding: 0;
  }

  .covered-shape-selector {
    gap: 6px;
    padding: 0 8px 0 10px;
  }

  .covered-shape-label {
    font-size: 12px;
  }

  .covered-shape-button {
    width: 42px;
    height: 32px;
  }

  .covered-shape-thumb {
    width: 34px;
    height: 25px;
  }

  .symbol-hint {
    top: 2px;
    right: 12px;
    max-width: calc(100vw - 24px);
    justify-content: center;
  }

  .transform-dialog {
    width: 306px;
  }
}

@media (prefers-reduced-motion: reduce) {
  html:focus-within {
    scroll-behavior: auto !important;
  }
}

/* The program occupies real layout space so the complete canvas stays visible. */
body { --program-width: clamp(290px, 27vw, 360px); }
.program-panel {
  position: fixed; inset: 0 0 0 auto; z-index: 25;
  width: var(--program-width); height: 100dvh; display: flex; flex-direction: column;
  --program-confirm-height: 44px;
  background: #fffefa; border-left: 2px solid var(--line);
  box-shadow: -8px 0 28px #15365f12;
  animation: program-enter 240ms ease-out;
}
@keyframes program-enter { from { transform: translateX(100%); } to { transform: translateX(0); } }
.program-panel h3 { margin: 0; font-size: 16px; }
.program-results { display: flex; flex-direction: column; flex: 1 0 180px; min-height: 180px; padding: 16px; gap: 10px; overflow: auto; }
.program-section-title { display: flex; align-items: center; justify-content: space-between; }
.program-panel button { cursor: pointer; }
.program-section-title button, .program-list button { background: transparent; border: 0; color: var(--ink-soft); padding: 6px; }
.program-list { flex: 1; min-height: 0; overflow: auto; margin: 0; padding: 0; list-style: none; }
.program-list li { display: flex; align-items: center; gap: 8px; padding: 10px 8px; margin-bottom: 8px; border: 1px solid var(--line); border-radius: 10px; background: white; }
.program-list .program-formula { flex: 1; display: flex; align-items: center; flex-wrap: wrap; font-weight: 700; }
.program-list li.is-active { border-color: var(--blue); background: #e5f5ff; }
.program-list li.is-complete { border-color: var(--green); background: #f1faed; }
.program-list li.is-error { border-color: var(--coral); background: #fff0e9; }
.program-muted { margin: 0; color: var(--ink-soft); font-size: 12px; line-height: 1.6; }
.program-primary { width: 100%; flex-shrink: 0; border: 0; border-radius: 10px; padding: 11px; background: var(--ink); color: white; font-weight: 700; }
.program-panel button:disabled { opacity: .45; cursor: default; }
.program-editor { flex: 0 1 auto; min-height: 0; overflow: auto; border-top: 2px solid var(--line); padding: 16px 16px var(--program-confirm-height); background: #f3f9fc; }
.program-editor > .program-primary { display: block; height: var(--program-confirm-height); }
.program-modes { display: flex; gap: 8px; }
.program-modes button { flex: 1; padding: 10px; border: 1px solid var(--line); border-radius: 9px; background: white; }
.program-modes button[aria-pressed="true"] { background: var(--yellow-soft); border-color: #d2aa22; }
body.program-open .app-shell { width: calc(100% - var(--program-width)); height: 100dvh; display: flex; flex-direction: column; }
body.program-open .main-content { width: 100%; flex: 1; min-height: 0; padding: 12px; overflow: hidden; }
body.program-open .board-section { min-width: 0; }
body.program-open .board-frame { min-width: 0; max-width: 100%; width: min(100%, var(--program-board-fit, 1320px)); }
body.program-open .floating-tools { position: static; flex: 0 0 auto; max-width: none; padding: 12px; gap: 8px; }
body.program-open .symbol-hint { display: none; }
body.program-open[data-layout-state="state3"] .board-section { gap: 12px; }
body.program-open[data-layout-state="state3"] .tangram-reference { width: 22%; transform: none; }
body.program-open[data-layout-state="state3"] .board-frame { max-width: calc(78% - 12px); }
body.program-running .floating-tools { pointer-events: none; opacity: .65; }
@media (max-width: 640px) {
  body { --program-width: 56vw; }
  .program-results { padding: 10px; }
  .program-editor { padding: 10px 10px var(--program-confirm-height); }
  body.program-open .floating-tools { max-height: 35dvh; overflow: auto; }
  body.program-open .main-content { padding: 6px; }
  body.program-open .lesson-tip { display: none; }
}
@media (prefers-reduced-motion: reduce) { .program-panel { animation: none; } }

.app-shell { width: 100%; transition: width 240ms ease-out; }
@media (prefers-reduced-motion: reduce) { .app-shell { transition: none; } }

@media (max-width: 640px) {
  body.program-open .state-switch, body.program-open .pen-cluster { flex-wrap: wrap; max-width: 100%; }
  body.program-open .state-button { flex: 1; min-width: 44px; }
}

/* Button controls follow the Tetris tool, scaled to the program sidebar. */
.program-choice-field { display: grid; gap: 8px; margin: 12px 0; font-size: 14px; font-weight: 700; }
.program-choices { display: flex; flex-wrap: wrap; gap: 6px; }
.program-choices button, .program-motion-card button {
  min-width: 32px; min-height: 32px; padding: 3px 7px;
  border: 2px solid #cfdee5; border-radius: 10px;
  background: white; color: var(--ink); font-size: 18px; font-weight: 800;
}
.program-choices button[aria-pressed="true"], .program-motion-card button[aria-pressed="true"] {
  border-color: var(--ink); background: var(--yellow); box-shadow: 2px 2px 0 var(--ink);
}
.program-motion-card { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; margin: 12px 0; padding: 12px 8px; border: 2px solid var(--ink); border-radius: 16px; background: white; box-shadow: 3px 3px 0 var(--ink); }
.program-motion-row { display: flex; align-items: center; justify-content: center; gap: 6px; font-size: 14px; font-weight: 800; }
.program-direction-pad { display: grid; grid-template-columns: repeat(3, 32px); grid-template-rows: repeat(3, 32px); gap: 4px; }
.program-direction-pad button { padding: 0; font-size: 27px; line-height: 1; }
.program-direction-pad [data-program-direction="up"] { grid-column: 2; }
.program-direction-pad [data-program-direction="left"] { grid-column: 1; grid-row: 2; }
.program-direction-pad span { grid-column: 2; grid-row: 2; background: #edf5f8; border-radius: 9px; }
.program-direction-pad [data-program-direction="right"] { grid-column: 3; grid-row: 2; }
.program-direction-pad [data-program-direction="down"] { grid-column: 2; grid-row: 3; }
.program-stepper { display: grid; justify-items: center; gap: 4px; }
.program-stepper button { width: 34px; height: 26px; min-height: 26px; padding: 0; font-size: 24px; line-height: 1; }
.program-stepper input { width: 60px; min-width: 0; height: 38px; padding: 3px; border: 2px solid #cfdee5; border-radius: 9px; background: white; color: var(--ink); text-align: center; font-size: 18px; font-weight: 800; appearance: textfield; }
.program-stepper input::-webkit-inner-spin-button, .program-stepper input::-webkit-outer-spin-button { appearance: none; margin: 0; }
.program-turn-buttons { display: grid; gap: 5px; }
.program-turn-buttons button { white-space: nowrap; }
.program-pivot-row { width: 100%; }
.program-motion-card button:hover:not(:disabled), .program-choices button:hover:not(:disabled) { border-color: var(--ink); }
@media (max-width: 640px) {
  .program-motion-card { padding: 10px 4px; }
  #programRotate .program-motion-row { flex-wrap: wrap; }
}
.program-choice-field { grid-template-columns: 64px 1fr; align-items: center; }
@media (max-width: 640px) {
  .program-choice-field { grid-template-columns: 1fr; }
}

.program-number-pad { width: 184px; max-width: calc(100vw - 16px); }
.program-number-pad .number-pad-confirm { grid-column: 1 / -1; }
.program-number-pad .program-muted { margin-top: 6px; text-align: center; }
#programCount { cursor: pointer; }
#programCount[aria-expanded="true"] { border-color: var(--ink); background: var(--yellow-soft); }
