.directive-pyide {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  overflow: hidden;
  gap: 8px;
  height: var(--pyide-height, calc(100dvh - 80px));
}

.directive-pyide code-input {
  margin: 0;
}

.directive-pyide .editor {
  width: 100%;
  border: 1px solid var(--color-spacer);
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.directive-pyide .editor .cm-editor {
  height: 100%;
}

.directive-pyide .container {
  width: 100%;
  overflow: hidden;
  min-height: 120px;
  min-width: 120px;
  flex: 0 0 200px;
  display: flex;
  flex-direction: column;
}

.directive-pyide .output,
.directive-pyide .input {
  white-space: pre;
  height: 100%;
  border: 1px solid var(--color-spacer);
  border-radius: 8px;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}

.directive-pyide .output {
  padding: 16px;
  margin-bottom: 0px;
  font-family: hyperbook-monospace, monospace;
}

/* Prompt bar, shown while the script waits for input. Styled as its own
   console-like strip below the output/canvas panel so it reads as "the program
   is waiting for you" rather than as part of the page chrome. */
.directive-pyide .stdin {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 10px;
  flex: 0 0 auto;
  margin-top: 8px;
  padding: 10px 12px;
  border: 1px solid var(--color-brand);
  border-radius: 8px;
  background-color: color-mix(in srgb, var(--color-brand) 7%, transparent);
}

.directive-pyide .stdin.hidden {
  display: none;
}

/* Blinking caret marks the waiting state without needing extra markup. */
.directive-pyide .stdin::before {
  content: "❯";
  flex: 0 0 auto;
  font-family: hyperbook-monospace, monospace;
  font-weight: 700;
  line-height: 1;
  color: var(--color-brand);
  animation: pyide-stdin-caret 1.4s ease-in-out infinite;
}

@keyframes pyide-stdin-caret {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.3;
  }
}

@media (prefers-reduced-motion: reduce) {
  .directive-pyide .stdin::before {
    animation: none;
  }
}

.directive-pyide .stdin .stdin-prompt {
  flex: 0 1 auto;
  min-width: 0;
  font-family: hyperbook-monospace, monospace;
  color: var(--color-text);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.directive-pyide .stdin .stdin-prompt:empty {
  display: none;
}

/* Grows to fill the row, and wraps to its own line when the prompt is long. */
.directive-pyide .stdin .stdin-field {
  flex: 1 1 160px;
  min-width: 0;
  padding: 8px 12px;
  border: 1px solid var(--color-spacer);
  border-radius: 6px;
  font-family: hyperbook-monospace, monospace;
  font-size: 1em;
  line-height: 1.4;
  background-color: var(--color-background);
  color: var(--color-text);
}

.directive-pyide .stdin .stdin-field:focus {
  outline: none;
  border-color: var(--color-brand);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--color-brand) 25%, transparent);
}

/* Overrides the shared .directive-pyide button reset, which is borderless and
   full-width for the tab/toolbar groups.

   The fill is a tint of the brand mixed into --color-background rather than
   flat --color-brand, and the label is always --color-text. That keeps the
   contrast guaranteed by the theme's own background/text pairing in both
   colour schemes: --color-brand-text is only defined for light mode, and
   --color-brand-dark falls back to --color-brand, so neither can be relied on
   to stay legible against a brand fill. */
.directive-pyide .stdin .stdin-submit {
  flex: 0 0 auto;
  padding: 8px 18px;
  border: 1px solid var(--color-brand);
  border-radius: 6px;
  background-color: color-mix(
    in srgb,
    var(--color-brand) 18%,
    var(--color-background)
  );
  color: var(--color-text);
  font-weight: 600;
  line-height: 1.4;
  cursor: pointer;
}

.directive-pyide .stdin .stdin-submit:hover {
  background-color: color-mix(
    in srgb,
    var(--color-brand) 32%,
    var(--color-background)
  );
}

.directive-pyide .stdin .stdin-submit:active {
  background-color: color-mix(
    in srgb,
    var(--color-brand) 45%,
    var(--color-background)
  );
}

.directive-pyide .stdin .stdin-submit:focus-visible {
  outline: 2px solid var(--color-text);
  outline-offset: 2px;
}

.directive-pyide .output .error-line {
  color: #b42318;
}

.directive-pyide .output .pfem {
  font-family: inherit;
  font-size: 0.9em;
  line-height: 1.5;
  padding: 8px 12px;
  border-left: 3px solid #b42318;
  background: color-mix(in srgb, #b42318 8%, transparent);
  border-radius: 0 4px 4px 0;
  white-space: normal;
}

.directive-pyide .output .pfem-title {
  font-weight: bold;
  color: #b42318;
  margin-bottom: 4px;
}

.directive-pyide .output .pfem-summary {
  margin-bottom: 4px;
}

.directive-pyide .output .pfem-why {
  color: var(--color-text, #555);
  font-style: italic;
  margin-bottom: 4px;
  opacity: 0.8;
}

.directive-pyide .output .pfem-steps {
  margin: 4px 0 4px 18px;
  padding: 0;
}

.directive-pyide .output .pfem-steps li {
  margin: 2px 0;
}

.directive-pyide .output .pfem-details {
  margin-top: 6px;
}

.directive-pyide .output .pfem-details > summary {
  cursor: pointer;
  opacity: 0.6;
  font-size: 0.85em;
  user-select: none;
}

.directive-pyide .output .pfem-details > summary:hover {
  opacity: 1;
}

.directive-pyide .output .pfem-details pre {
  margin: 4px 0 0;
  font-family: hyperbook-monospace, monospace;
  font-size: 0.85em;
  white-space: pre-wrap;
  color: #b42318;
  opacity: 0.85;
}

.directive-pyide .canvas-wrapper {
  overflow: auto;
  height: 100%;
  border: 1px solid var(--color-spacer);
  border-radius: 8px;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
  background-color: #e8e8e8;
  background-image: linear-gradient(45deg, #cccccc 25%, transparent 25%),
    linear-gradient(-45deg, #cccccc 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #cccccc 75%),
    linear-gradient(-45deg, transparent 75%, #cccccc 75%);
  background-size: 20px 20px;
  background-position:
    0 0,
    0 10px,
    10px -10px,
    -10px 0px;
}

.directive-pyide .canvas {
  display: block;
  width: auto;
  height: auto;
}

.directive-pyide .canvas-header,
.directive-pyide .output-header {
  border: 1px solid var(--color-spacer);
  border-bottom: none;
  border-top-left-radius: 8px;
  border-top-right-radius: 8px;
  padding: 8px 16px;
  background-color: var(--color-spacer);
  color: var(--color-text);
  font-weight: 600;
  text-align: center;
}

.directive-pyide .canvas-output-splitter {
  display: none;
  width: 100%;
  height: 4px;
  margin: 10px 0;
  cursor: row-resize;
  background: var(--color-spacer);
  border-radius: 999px;
  flex-shrink: 0;
  touch-action: none;
  opacity: 0.45;
  transition: opacity 0.15s ease-in-out;
}

.directive-pyide .canvas-output-splitter:hover {
  opacity: 0.65;
}

.directive-pyide .hidden {
  display: none;
}

.directive-pyide .editor-container {
  width: 100%;
  display: flex;
  flex-direction: column;
  min-height: 120px;
  min-width: 120px;
  flex: 1 1 400px;
  overflow: hidden;
}

.directive-pyide .splitter {
  background: var(--color-spacer);
  border-radius: 999px;
  flex-shrink: 0;
  touch-action: none;
  opacity: 0.45;
  transition: opacity 0.15s ease-in-out;
}

.directive-pyide .splitter:hover {
  opacity: 0.65;
}

.directive-pyide.split-vertical .splitter {
  width: 100%;
  height: 4px;
  cursor: row-resize;
}

.directive-pyide.split-horizontal .splitter {
  width: 4px;
  height: 100%;
  cursor: col-resize;
}

.directive-pyide.resizing {
  user-select: none;
}

.directive-pyide.resizing .splitter {
  opacity: 0.75;
}

.directive-pyide .editor.running {
  pointer-events: none;
  opacity: 0.7;
}

.directive-pyide.locked-by-other {
  outline: 2px solid #f59e0b;
  outline-offset: 2px;
}

.directive-pyide.locked-by-other .buttons {
  border-color: #f59e0b;
}

.directive-pyide .buttons {
  display: flex;
  border: 1px solid var(--color-spacer);
  border-radius: 8px;
  border-bottom: none;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  overflow: hidden;
}

.directive-pyide .buttons.bottom {
  border: 1px solid var(--color-spacer);
  border-radius: 8px;
  border-top: none;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}

/* Icon-only toolbar: the written labels (Reset/Copy/Download) made this row
   wrap on narrow screens, so the wording moved into title/aria-label. */
.directive-pyide .buttons.bottom button {
  flex: 0 0 auto;
  min-width: 42px;
  width: 42px;
  padding: 8px 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.directive-pyide .buttons.bottom button .icon {
  width: 18px;
  height: 18px;
  display: block;
}

/* Fullscreen sits apart, at the trailing edge of the toolbar. */
.directive-pyide .buttons.bottom button.fullscreen {
  margin-inline-start: auto;
  border-right: none;
  border-left: 1px solid var(--color-spacer);
}

.directive-pyide .test-heading {
  font-size: 1.1em;
  font-weight: bold;
  text-decoration: underline;
  margin-top: 8px;
}

.directive-pyide .test-heading:first-of-type {
  margin-top: 0;
}

.directive-pyide button {
  flex: 1;
  padding: 8px 16px;
  border: none;
  border-right: 1px solid var(--color-spacer);
  background-color: var(--color--background);
  color: var(--color-text);
  cursor: pointer;
}

.directive-pyide .buttons button:last-child {
  border-right: none;
}

.directive-pyide .buttons:not(.bottom) button:first-child {
  border-top-left-radius: 8px;
}

.directive-pyide .buttons:not(.bottom) button:last-child {
  border-top-right-radius: 8px;
}

.directive-pyide .buttons.bottom button:first-child {
  border-bottom-left-radius: 8px;
}

.directive-pyide .buttons.bottom button:last-child {
  border-bottom-right-radius: 8px;
}

.directive-pyide button.active {
  background-color: var(--color-spacer);
}

.directive-pyide button:hover {
  background-color: var(--color-spacer);
}

.directive-pyide button.running {
  pointer-events: none;
  cursor: not-allowed;
  opacity: 0.5;
}

.directive-pyide button.stopping {
  pointer-events: auto;
  cursor: progress;
  opacity: 0.75;
}

.directive-pyide button.locked {
  color: #b45309;
  font-weight: 600;
}

.directive-pyide button.fullscreen {
  flex: 0 0 auto;
  min-width: 42px;
  width: 42px;
  padding: 8px 0;
}

.directive-pyide button.stop {
  flex: 0 0 auto;
  min-width: 96px;
  color: #b42318;
}

.directive-pyide button.stop:disabled {
  color: var(--color-text);
  opacity: 0.5;
  cursor: not-allowed;
}

.directive-pyide:fullscreen {
  width: 100vw;
  height: 100dvh !important;
  padding: 12px;
  box-sizing: border-box;
  background-color: var(--color-background, var(--color--background, #fff));
}

.directive-pyide:fullscreen::backdrop {
  background-color: var(--color-background, var(--color--background, #fff));
}

@media screen and (min-width: 1024px) {
  .directive-pyide {
    flex-direction: row;

    .output {
      height: 100%;
    }

    .container {
      height: 100% !important;
    }

    .editor-container {
      height: 100%;
      overflow: hidden;
    }
  }

  .directive-pyide[data-canvas="true"].canvas-split-mode .canvas-wrapper,
  .directive-pyide[data-canvas="true"].canvas-split-mode .output {
    flex: 1 1 0;
    min-height: 120px;
    border-radius: 8px;
  }

  .directive-pyide[data-canvas="true"].canvas-split-mode .canvas-wrapper,
  .directive-pyide[data-canvas="true"].canvas-split-mode .output {
    border-top-left-radius: 0;
    border-top-right-radius: 0;
  }

  .directive-pyide[data-canvas="true"].canvas-split-mode
    .canvas-output-splitter {
    display: block;
  }
}
