/* ============================================================
   Coral Sim — Stylesheet
   Theme color tokens live in themes.css; this file contains
   only structural / layout CSS.
   ============================================================ */

/* ── Reset & base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* ── Accessibility utilities ── */
.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;
}

/* Skip navigation link (WCAG 2.4.1 — Bypass Blocks) */
.skip-link {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0,0,0,0); white-space: nowrap; border: 0;
  text-decoration: none;
  /* Explicit colors satisfy WAVE contrast check even while visually hidden */
  color: #ffffff;
  background: #000000;
}
.skip-link:focus {
  position: fixed;
  top: 8px; left: 8px;
  z-index: 9999;
  width: auto; height: auto;
  clip: auto;
  padding: 8px 16px;
  background: var(--accent);
  color: var(--bg);
  border-radius: var(--radius);
  font-size: 0.88rem;
  font-weight: 600;
  outline: 3px solid var(--bg);
  outline-offset: 2px;
}

/* ── Page layout ── */
html, body {
  height: 100%;
  font-family: var(--sans);
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  overflow: hidden;
}

/* ── Text size tiers ── */
html.size-large     { font-size: 17px; }
html.size-xl        { font-size: 21px; }
html.size-projector { font-size: 26px; }
html.size-xl        header { height: auto; min-height: 52px; }
html.size-projector header { height: auto; min-height: 64px; }

/* Widen vars panel at larger sizes — desktop/tablet only, never on mobile */
@media (min-width: 769px) {
  html.size-large     #workspace { grid-template-columns: 1fr 340px; }
  html.size-xl        #workspace { grid-template-columns: 1fr 420px; }
  html.size-projector #workspace { grid-template-columns: 1fr 500px; }
}

/* Reduce output area floor at larger sizes to reclaim space for the editor */
html.size-large     #output-area { min-height: 90px; }
html.size-xl        #output-area { min-height: 80px; }
html.size-projector #output-area { min-height: 70px; }

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* ── URL Banner ── */
#url-banner {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 6px 16px;
  background: color-mix(in srgb, var(--accent) 15%, var(--surface));
  border-bottom: 1px solid var(--accent);
  font-size: 0.82rem;
  color: var(--text);
  flex-shrink: 0;
}
#url-banner[hidden] { display: none; }
#url-banner-msg { flex: 1; }
#btn-url-banner-close {
  background: none; border: none; color: var(--muted); cursor: pointer;
  font-size: 1rem; padding: 2px 6px; border-radius: var(--radius);
  min-height: 44px; min-width: 44px; /* WCAG 2.5.5 */
}
#btn-url-banner-close:hover { color: var(--text); background: var(--border); }

/* ── Header ── */
header {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 0 16px;
  padding: 0 16px;
  height: 48px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

header h1 {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--accent);
  letter-spacing: 0.02em;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

#btn-bug {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  min-width: 44px;
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-size: 1.1rem;
  text-decoration: none;
  line-height: 1;
}

#btn-bug:hover {
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  border-color: var(--accent);
}

/* ── Toolbar ── */
#toolbar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  flex-wrap: wrap;
}

.toolbar-sep {
  width: 1px;
  height: 24px;
  background: var(--border);
  margin: 0 2px;
  flex-shrink: 0;
}

/* Keeps the Speed label and slider together as one unit so they always
   wrap to the same row instead of splitting across rows on narrow screens. */
.speed-group {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.speed-label {
  font-size: 0.82rem;
  color: var(--muted);
  white-space: nowrap;
}

/* ── Status bar (lives in header center column) ── */
#status-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.82rem;
  color: var(--muted);
  overflow: hidden;
  min-width: 0;
}

#status-indicator {
  font-size: 0.75rem;
  line-height: 1;
  transition: color 0.2s;
}

#status-bar.state-edit     #status-indicator { color: var(--muted); }
#status-bar.state-ready    #status-indicator { color: var(--accent2); }
#status-bar.state-running  #status-indicator { color: var(--accent); }
#status-bar.state-paused   #status-indicator { color: var(--warn); }
#status-bar.state-complete #status-indicator { color: var(--accent2); }

#status-state {
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
}

#status-detail {
  flex: 1;
  font-style: italic;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#status-step {
  font-family: var(--mono);
  font-size: 0.82rem;
  color: var(--muted);
  white-space: nowrap;
}

/* ── Input area ── */
#input-area {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

#input-area label {
  font-size: 0.85rem;
  color: var(--muted);
  white-space: nowrap;
  font-weight: 500;
}

#input-textarea {
  flex: 1;
  font-family: var(--mono);
  font-size: 0.85rem;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 4px 8px;
  resize: vertical;
  height: 44px;    /* single line default */
  min-height: 44px; /* can't drag smaller than one line */
  line-height: 1.4;
  overflow-y: auto;
}

#input-textarea::placeholder { color: var(--muted); } /* opacity removed — 0.7 opacity caused 4.23:1 failure (WCAG 1.4.3) */

/* ── Tab bar ── */
#tab-bar {
  display: flex;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

#tab-bar [role="tab"] {
  padding: 6px 20px;
  border: none;
  border-bottom: 2px solid transparent;
  background: none;
  color: var(--muted);
  font-family: var(--sans);
  font-size: 0.82rem;
  font-weight: 500;
  cursor: pointer;
  border-radius: 0;
  min-height: 44px;
  transition: color 0.15s, border-color 0.15s;
}

#tab-bar [role="tab"][aria-selected="true"] {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

#tab-bar [role="tab"]:hover:not([aria-selected="true"]) {
  color: var(--text);
  background: color-mix(in srgb, var(--accent) 8%, transparent);
}

/* ── Main workspace ── */
#workspace {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 280px;
  grid-template-rows: 1fr;
  min-height: 220px;
  overflow: hidden;
}

/* ── Code panel ── */
#panel-code {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border-right: 1px solid var(--border);
  grid-column: 1;
  grid-row: 1;
}

/* ── Flowchart panel shares the same cell as code panel (tab-switched) ── */
#panel-flowchart {
  grid-column: 1;
  grid-row: 1;
}

#editor-wrap {
  flex: 1;
  display: flex;
  overflow: hidden;
  background: var(--bg);
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.6;
}

/* Line number gutter */
#gutter {
  flex-shrink: 0;
  min-width: 48px;
  padding: 8px 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  text-align: right;
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.6;
  color: var(--muted);
  user-select: none;
  position: sticky;
  left: 0;
  z-index: 2;
  overflow: hidden;
}

.gutter-line {
  padding: 0 8px;
  display: block;
}

.gutter-line.active {
  background: var(--step-gutter-bg);
  color: var(--step-gutter-fg);
  font-weight: 700;
}

/* Editor container — overlay technique */
#editor-container {
  position: relative;
  flex: 1;
  min-width: 0;
}

/* Active line highlight bar */
#active-line-highlight {
  position: absolute;
  left: 0;
  right: 0;
  pointer-events: none;
  background: var(--step-bg);
  border-left: 5px solid var(--step-border);
  /* Vertical borders: 0px by default; 2px for High Contrast to form a full "box" indicator
     without relying on background color alone (WCAG 1.4.1 non-color) */
  border-top:    var(--step-border-v-width, 0px) solid var(--step-border);
  border-bottom: var(--step-border-v-width, 0px) solid var(--step-border);
  z-index: 1;
  display: none;
}

/* Syntax highlight overlay */
#highlight-overlay {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  padding: 8px;
  pointer-events: none;
  white-space: pre;
  overflow: hidden;
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.6;
  z-index: 1;
  color: var(--text); /* fallback for unstyled tokens; spans override with syntax colors */
  word-break: normal;
}

/* The editable textarea */
#code-editor {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  padding: 8px;
  background: transparent;
  color: transparent;
  caret-color: var(--text);
  border: none;
  outline: none;
  resize: none;
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.6;
  z-index: 2;
  tab-size: 3;
  white-space: pre;
  overflow: auto;
  width: 100%;
  height: 100%;
  /* Disable ligatures so != stays != not ≠ */
  font-variant-ligatures: none;
  font-feature-settings: "liga" 0, "calt" 0;
}

/* Also disable ligatures on the highlight overlay.
   Always reset letter/word-spacing so textarea and overlay stay pixel-perfect
   regardless of theme or dyslexic body spacing overrides. */
#highlight-overlay,
#code-editor,
#gutter {
  font-variant-ligatures: none;
  font-feature-settings: "liga" 0, "calt" 0;
  letter-spacing: normal;
  word-spacing: normal;
}

/* ── Flowchart panel ── */
#panel-flowchart {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border-right: 1px solid var(--border);
  position: relative;
}

/* Respect the hidden attribute on tab panels — CSS display rules override browser default */
#panel-code[hidden],
#panel-flowchart[hidden] {
  display: none !important;
}


#flowchart-placeholder {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 0.88rem;
  text-align: center;
  padding: 24px;
  pointer-events: none;
  z-index: 1;
}

#flowchart-viewport {
  flex: 1;
  overflow: hidden;
  cursor: grab;
  position: relative;
}

#flowchart-viewport:active { cursor: grabbing; }

#flowchart-svg {
  position: absolute;
  top: 0; left: 0;
  overflow: visible;
}

#flowchart-controls {
  position: absolute;
  bottom: 12px;
  right: 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  z-index: 10;
}

#flowchart-controls button {
  width: 44px;
  height: 44px;
  min-height: 44px; /* WCAG 2.5.5 */
  min-width: 44px;
  padding: 0;
  font-size: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Flowchart SVG styles */
.fc-node { cursor: default; }
.fc-active .fc-shape {
  fill: var(--fc-active-fill) !important;
  stroke: var(--fc-active-stroke) !important;
  stroke-width: 2.5px !important;
}
.fc-node-text { font-family: var(--mono); font-size: 12px; fill: var(--fc-text); font-variant-ligatures: none; }
.fc-edge { stroke: var(--fc-edge); stroke-width: 1.5; fill: none; marker-end: url(#arrow); }
.fc-edge-label { font-family: var(--sans); font-size: 11px; fill: var(--fc-label); font-weight: 600; }

html.size-large     .fc-node-text  { font-size: 15px; }
html.size-xl        .fc-node-text  { font-size: 18px; }
html.size-projector .fc-node-text  { font-size: 22px; }
html.size-large     .fc-edge-label { font-size: 14px; }
html.size-xl        .fc-edge-label { font-size: 17px; }
html.size-projector .fc-edge-label { font-size: 21px; }

/* ── Variables panel ── */
#vars-panel {
  grid-column: 2;
  grid-row: 1;
  overflow-y: auto;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

#vars-header {
  padding: 8px 12px;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 1;
}

#vars-fn-name {
  padding: 5px 10px 4px;
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--accent2);
  background: color-mix(in srgb, var(--accent2) 12%, transparent);
  border-bottom: 2px solid color-mix(in srgb, var(--accent2) 40%, transparent);
  font-family: var(--mono);
  letter-spacing: 0.02em;
  flex-shrink: 0;
}

/* Container: stacks tables with background-coloured gap between them */
#vars-sections {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding-bottom: 8px;
}

.vars-section-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
  font-family: var(--mono);
}

/* Section caption (Parameters / Local Variables / Return) */
.vars-section-table caption {
  text-align: left;
  padding: 5px 8px 3px;
  font-size: 0.82rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  border-bottom: 1px solid var(--border);
}
.vars-table-params caption { color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, transparent); border-left: 3px solid var(--accent); }
.vars-table-locals caption { color: var(--muted);  background: color-mix(in srgb, var(--muted)  6%, transparent); border-left: 3px solid var(--border); }
.vars-table-return caption { color: var(--warn);   background: color-mix(in srgb, var(--warn)   8%, transparent); border-left: 3px solid var(--warn); }

.vars-section-table th {
  background: var(--surface);
  padding: 4px 8px;
  white-space: nowrap;
  text-align: left;
  font-weight: 600;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  font-family: var(--sans);
  font-size: 0.82rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.vars-section-table td {
  padding: 3px 8px;
  border-bottom: 1px solid color-mix(in srgb, var(--border) 50%, transparent);
  vertical-align: top;
  word-break: break-all;
}

.vars-section-table tr:hover td { background: color-mix(in srgb, var(--accent) 6%, transparent); }

.vars-empty {
  color: var(--muted);
  font-family: var(--sans);
  font-size: 0.82rem;
  text-align: center;
  padding: 16px 8px !important;
  font-style: italic;
}

.var-name { color: var(--accent); }
.var-type { color: var(--muted); font-size: 0.82rem; white-space: nowrap; }
.var-uninit { color: var(--muted); }
.var-int   { color: var(--hl-number); }
.var-float { color: var(--warn); }
.var-str   { color: var(--hl-string); }


/* Array sub-table */
.array-subtable {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
  margin-top: 2px;
}
.array-subtable th, .array-subtable td {
  padding: 1px 4px;
  border: 1px solid var(--border);
}
.array-subtable th { color: var(--muted); font-weight: 600; background: var(--bg); white-space: nowrap; }
.array-subtable td { color: var(--text); }

/* Flash animation for changed variables — uses background + border so change is
   not conveyed by color alone (WCAG 1.4.1). The border fades alongside the bg. */
@keyframes var-flash {
  0%   { background-color: color-mix(in srgb, var(--accent) 35%, transparent);
         box-shadow: inset 3px 0 0 var(--accent2); }
  100% { background-color: transparent;
         box-shadow: inset 3px 0 0 transparent; }
}

.vars-section-table tr.flashing td,
.array-subtable tr.flashing td {
  animation: var-flash 700ms ease-out forwards;
}

/* ── Output area ── */
#output-area {
  flex-shrink: 0;
  min-height: 120px;
  max-height: 30vh;
  background: var(--bg);
  border-top: 2px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#output-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

#btn-clear-output {
  font-size: 0.82rem;
  padding: 2px 8px;
  min-height: 44px; /* WCAG 2.5.5 */
}

#output {
  flex: 1;
  overflow-y: auto;
  padding: 8px 12px;
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.6;
  color: var(--text);
}

.out-line { white-space: pre-wrap; min-height: 1.6em; }

.out-warn {
  color: var(--warn);
  font-style: italic;
  font-family: var(--sans);
  font-size: 0.82rem;
  padding: 4px 0;
}

.out-error {
  color: var(--error);
  font-family: var(--sans);
  font-size: 0.82rem;
  padding: 4px 0;
  white-space: pre-wrap;
}

/* ── Buttons ── */
button {
  font-family: var(--sans);
  font-size: 0.82rem;
  font-weight: 500;
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
  min-height: 44px; /* WCAG 2.5.5 — 44px touch target */
  min-width: 44px;
  white-space: nowrap;
}

button:hover:not(:disabled) {
  background: color-mix(in srgb, var(--accent) 12%, var(--surface));
  border-color: var(--accent);
  color: var(--accent);
}

button:disabled {
  opacity: 0.38;
  cursor: not-allowed;
}

#btn-execute-edit {
  background: var(--accent);
  color: var(--bg);
  border-color: var(--accent);
  font-weight: 600;
}
#btn-execute-edit:hover:not(:disabled) {
  background: color-mix(in srgb, var(--accent) 85%, white);
  color: var(--bg);
}

#btn-run-pause[aria-pressed="true"] {
  background: color-mix(in srgb, var(--warn) 20%, var(--surface));
  border-color: var(--warn);
  color: var(--warn);
}

/* ── Selects ── */
/* appearance:none removes Safari's native menulist rendering so CSS controls the
   height exactly. A custom SVG chevron replaces the native dropdown arrow. */
select {
  font-family: var(--sans);
  font-size: 0.82rem;
  padding: 6px 30px 6px 10px;       /* extra right padding for the custom arrow */
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background-color: var(--surface);  /* background-color, not shorthand, so
                                        background-image below is not reset */
  background-image: url("data:image/svg+xml;base64,PHN2ZyB2aWV3Qm94PSIwIDAgMTQgOSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cGF0aCBkPSJNMCAwIDcgOSAxNCAweiIgZmlsbD0iI2FhYWFhYSIvPjwvc3ZnPg==");
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 14px 9px;
  color: var(--text);
  cursor: pointer;
  height: 44px;
  box-sizing: border-box;
  -webkit-appearance: none;
  appearance: none;
}

select:disabled { opacity: 0.38; cursor: not-allowed; }

/* Light themes need a darker arrow for contrast against the light surface */
html.theme-light select,
html.theme-dyslexic-light select,
html.theme-classroom select {
  background-image: url("data:image/svg+xml;base64,PHN2ZyB2aWV3Qm94PSIwIDAgMTQgOSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cGF0aCBkPSJNMCAwIDcgOSAxNCAweiIgZmlsbD0iIzU1NTU1NSIvPjwvc3ZnPg==");
}

/* ── Range slider ── */
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  background: var(--surface);    /* button-like container, NOT the track color */
  border: 1px solid var(--border);
  border-radius: var(--radius);  /* match button rounding */
  outline: none;
  cursor: pointer;
  width: 100px;
  min-height: 44px; /* WCAG 2.5.5 — 44px touch target */
  padding: 0 6px;
}

input[type="range"]::-webkit-slider-runnable-track {
  height: 6px;
  background: var(--muted); /* passes 3:1 non-text contrast (WCAG 1.4.11) in all themes */
  border-radius: 3px;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  box-shadow: 0 1px 4px rgba(0,0,0,0.35);
  margin-top: -7px; /* center 20px thumb on 6px track: (6-20)/2 = -7px */
}

input[type="range"]::-moz-range-track {
  height: 6px;
  background: var(--muted);
  border-radius: 3px;
}

input[type="range"]::-moz-range-thumb {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--accent);
  border: none;
  cursor: pointer;
  box-shadow: 0 1px 4px rgba(0,0,0,0.35);
}

input[type="range"]:disabled { opacity: 0.38; cursor: not-allowed; }

/* ── Scrollbars (WebKit + Firefox) ── */
/* Applied to every scrollable surface; uses theme tokens so dark themes
   get dark scrollbars and light themes get light ones automatically. */
::-webkit-scrollbar              { width: 8px; height: 8px; }
::-webkit-scrollbar-track        { background: var(--surface); border-radius: 4px; }
::-webkit-scrollbar-thumb        { background: var(--border);  border-radius: 4px; border: 2px solid var(--surface); }
::-webkit-scrollbar-thumb:hover  { background: var(--muted); }
::-webkit-scrollbar-corner       { background: var(--surface); }

/* Firefox scrollbar colors */
* { scrollbar-width: thin; scrollbar-color: var(--border) var(--surface); }

/* ── Focus rings (WCAG 2.4.11) ── */
button:focus-visible,
select:focus-visible,
input:focus-visible,
textarea:focus-visible,
a:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

/* ── Syntax highlight span classes ── */
.hl-keyword  { color: var(--hl-keyword); }
.hl-type     { color: var(--hl-type); }
.hl-builtin  { color: var(--hl-builtin); }
.hl-string   { color: var(--hl-string); }
.hl-number   { color: var(--hl-number); }
.hl-comment  { color: var(--hl-comment); font-style: italic; }
.hl-operator { color: var(--hl-operator); }

/* ── Help modal ── */
#help-modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  max-width: min(680px, 95vw);
  max-height: 85vh;
  width: 100%;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0;
  box-shadow: 0 24px 80px rgba(0,0,0,0.5);
  overflow: hidden;
  /* Hide when not open — overrides browser default which some CSS rules can break */
  display: none;
}
#help-modal[open] {
  display: flex;
  flex-direction: column;
}

#help-modal::backdrop {
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(2px);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.modal-header h2 {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
}

#btn-help-close {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 1rem;
  padding: 4px 8px;
  min-height: 44px; /* WCAG 2.5.5 */
  min-width: 44px;
  border-radius: var(--radius);
}

#btn-help-close:hover:not(:disabled) {
  background: color-mix(in srgb, var(--error) 15%, transparent);
  color: var(--error);
  border-color: transparent;
}

.modal-body {
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.help-section h3 {
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--accent);
  margin-bottom: 8px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.help-section p, .help-section li, .help-section dd {
  font-size: 0.85rem;
  line-height: 1.65;
  color: var(--text);
}

.help-section ul, .help-section dl {
  padding-left: 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.help-section dl { padding-left: 0; }

.help-section dt {
  font-weight: 600;
  font-size: 0.82rem;
  color: var(--text);
  margin-top: 6px;
}

.help-section dd {
  padding-left: 16px;
  color: var(--muted);
  margin-bottom: 2px;
}

.help-section code {
  font-family: var(--mono);
  font-size: 0.78rem;
  background: var(--bg);
  padding: 1px 5px;
  border-radius: 3px;
  color: var(--hl-builtin);
}

.help-section a {
  color: var(--accent);
  text-decoration: underline;
  font-size: 0.85rem;
}
.help-section a:hover { color: var(--accent2); }

/* ── Mobile responsive ── */
@media (max-width: 768px) {
  /* Allow full-page vertical scroll so the output section is reachable */
  html {
    height: auto;
    overflow-x: hidden;
    overflow-y: visible;
  }
  body {
    height: auto;
    min-height: 100vh;
    overflow-x: hidden;
    overflow-y: auto;
  }

  header {
    grid-template-columns: 1fr;
    height: auto;
    padding: 8px 12px;
    gap: 6px;
  }

  .header-controls {
    flex-wrap: wrap;
  }

  /* Definite pixel height required because flex:1 collapses when body is height:auto */
  #workspace {
    grid-template-columns: 1fr;
    grid-template-rows: 300px 160px;  /* editor 300px, vars 160px */
    height: 460px;
    min-height: unset;
    overflow: hidden;
  }

  /* #panel-code and #panel-flowchart already have grid-column:1 grid-row:1 in base CSS */

  #vars-panel {
    grid-column: 1;
    grid-row: 2;
    border-left: none;
    border-top: 2px solid var(--border);
  }

  /* Hide line-number gutter — reclaims ~35px of editor width and avoids
     gutter/visual-line misalignment when code wraps */
  #gutter { display: none; }

  /* Wrap long code lines so users don't need to scroll horizontally.
     Both elements must share identical white-space rules so the
     highlight overlay stays aligned with the textarea characters. */
  #code-editor,
  #highlight-overlay {
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    overflow-x: hidden;
  }

  #editor-wrap { overflow-x: hidden; }

  /* Output section: remove the 40vh cap and allow natural height so
     the section is fully visible after scrolling past the workspace */
  #output-area {
    min-height: 150px;
    max-height: none;
    flex-shrink: 0;
    overflow: hidden;   /* inner #output div has overflow-y:auto for long output */
  }

  input[type="range"] { width: 70px; }
}

@media (max-width: 480px) {
  /* Tighten toolbar layout on very small screens but keep 44px touch targets (WCAG 2.5.5) */
  #toolbar { gap: 4px; padding: 4px 8px; }
  button { padding: 4px 10px; font-size: 0.82rem; }
  select { padding: 4px 28px 4px 8px; font-size: 0.82rem; } /* preserve right gap for arrow */
}

/* ── Assertive error announce (screen reader only) ── */
#error-announce {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

/* ── Reduced motion (WCAG 2.3.3 / user preference) ── */
@media (prefers-reduced-motion: reduce) {
  /* Disable variable flash animation */
  .vars-section-table tr.flashing td,
  .array-subtable tr.flashing td {
    animation: none;
  }
  /* Disable button/tab hover transitions */
  button, select, #tab-bar [role="tab"], #status-indicator {
    transition: none;
  }
}
