/* =========================================================
   Design tokens (light / dark)
   ========================================================= */
:root {
  --bg: #f4f5f8;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --hover: #eef0f5;
  --border: #e3e6ec;
  --text: #16181d;
  --muted: #6b7280;
  --accent: #5b5bd6;
  --accent-soft: #ecebff;
  --accent-text: #4343b8;
  --danger: #e5484d;
  --danger-soft: #fdecec;
  --dot: #d6d9e0;

  /* diagram (paper) */
  --paper: #ffffff;
  --line: #1c1f26;
  --slot: #f2f3f8;
  --slot-hover: #e4e3ff;
  --el-hover: #f6f6fb;

  /* code blocks (export, run output) */
  --code-bg: #f6f7f9;
  --code-text: #1f2328;
  --code-border: #e3e6ec;

  --radius: 10px;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, .06);
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 8px 24px -6px rgba(16, 24, 40, .12);
  --shadow-lg: 0 24px 60px -12px rgba(16, 24, 40, .28);

  font-family: "Inter", -apple-system, "Segoe UI", system-ui, sans-serif;
  font-size: 14px;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  display: flex; flex-direction: column; background: var(--bg); color: var(--text); -webkit-font-smoothing: antialiased;
  height: 100dvh; touch-action: manipulation; -webkit-tap-highlight-color: transparent; -webkit-text-size-adjust: 100%;
  /* installed web app: keep clear of the home indicator and rounded corners */
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
button { font: inherit; color: inherit; cursor: pointer; }
kbd { font-family: inherit; }

/* =========================================================
   Top bar
   ========================================================= */
.menubar {
  display: flex; align-items: center; gap: 2px; height: 48px; padding: 0 12px;
  background: var(--surface); border-bottom: 1px solid var(--border); user-select: none; z-index: 30;
}
.brand { display: flex; align-items: center; gap: 9px; font-weight: 700; letter-spacing: -.01em; margin-right: 18px; font-size: 15px; }
.logo {
  width: 22px; height: 22px; border-radius: 6px; position: relative;
  background: linear-gradient(135deg, var(--accent), #a35bd6);
}
.logo::before, .logo::after { content: ""; position: absolute; background: #fff; border-radius: 1px; }
.logo::before { left: 5px; right: 5px; top: 6px; height: 2px; }
.logo::after { left: 5px; top: 6px; bottom: 5px; width: 2px; }

.menu { position: relative; }
.menu-title { display: block; padding: 6px 10px; border-radius: 7px; cursor: default; color: var(--muted); font-weight: 500; }
.menu.open .menu-title { background: var(--hover); color: var(--text); }
.menu-items {
  display: none; position: absolute; top: calc(100% + 6px); left: 0; z-index: 40; min-width: 250px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow-lg); padding: 6px;
}
@media (hover: hover) { .menu:hover .menu-title { background: var(--hover); color: var(--text); } }
.menu.open .menu-items { display: block; animation: pop .12s ease-out; }
.menu-items button {
  display: flex; justify-content: space-between; align-items: center; width: 100%; border: 0; background: none;
  text-align: left; padding: 7px 10px; border-radius: 7px; font-size: 13.5px;
}
@media (hover: hover) { .menu-items button:hover { background: var(--hover); } }
.menu-items kbd { color: var(--muted); font-size: 12px; margin-left: 20px; }
.menu-items hr { border: 0; border-top: 1px solid var(--border); margin: 5px 4px; }

@keyframes pop { from { opacity: 0; transform: translateY(-4px) scale(.98); } }

#langSelect {
  margin-left: auto; max-width: 112px; padding: 5px 6px; font: inherit; font-size: 13px; color: var(--muted);
  background: var(--surface); border: 1px solid var(--border); border-radius: 7px; cursor: pointer;
}
@media (hover: hover) { #langSelect:hover { color: var(--text); background: var(--hover); } }

/* =========================================================
   Tabs
   ========================================================= */
.tabbar {
  display: flex; align-items: center; gap: 6px; padding: 8px 12px; background: var(--surface);
  border-bottom: 1px solid var(--border);
}
#tabs { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; }
.tab {
  display: flex; align-items: center; border-radius: 8px; white-space: nowrap;
  font-size: 13px; font-weight: 500; color: var(--muted);
}
@media (hover: hover) { .tab:hover { background: var(--hover); color: var(--text); } }
.tab.active { background: var(--accent-soft); color: var(--accent-text); }
.tab button { border: 0; background: none; color: inherit; font: inherit; }
.tab-name { padding: 6px 12px; border-radius: 8px; }
.tab:has(.x) .tab-name { padding-right: 4px; }
.tab .x {
  width: 22px; height: 22px; margin-right: 4px; display: grid; place-items: center;
  border-radius: 6px; font-size: 16px; line-height: 1; opacity: .65;
}
@media (hover: hover) { .tab .x:hover { background: var(--danger-soft); color: var(--danger); opacity: 1; } }
#addTab {
  width: 30px; height: 30px; border-radius: 8px; border: 1px dashed var(--border); background: none;
  color: var(--muted); font-size: 18px; line-height: 1;
}
@media (hover: hover) { #addTab:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); } }

/* =========================================================
   Layout + sidebar
   ========================================================= */
main { flex: 1; display: flex; min-height: 0; }
#sidebar {
  width: 236px; flex-shrink: 0; display: flex; flex-direction: column; gap: 8px; padding: 12px;
  background: var(--surface); border-right: 1px solid var(--border); transition: width .2s ease, padding .2s ease;
  overflow: hidden;
}
.side-scroll {
  flex: 1; min-height: 0; overflow-y: auto; display: flex; flex-direction: column; gap: 10px;
  margin: 0 -6px; padding: 0 6px 4px; scrollbar-width: thin;
}
.palette { flex-shrink: 0; }
.palette h3, .props h3 {
  margin: 14px 4px 6px; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--muted);
}
.palette h3:first-child { margin-top: 2px; }
.tool {
  display: flex; align-items: center; gap: 10px; padding: 6px 8px; margin-bottom: 2px; border-radius: 8px;
  cursor: grab; user-select: none; font-weight: 500; transition: background .12s, transform .12s;
}
@media (hover: hover) { .tool:hover { background: var(--hover); } }
.tool:active { cursor: grabbing; transform: scale(.98); }
.tool i {
  width: 28px; height: 28px; flex-shrink: 0; display: grid; place-items: center; font-style: normal; font-weight: 600;
  border-radius: 7px; background: var(--surface-2); border: 1px solid var(--border); color: var(--accent);
  box-shadow: var(--shadow-sm);
}

.props { padding: 4px 10px 10px; background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius); }
.props h3 { margin: 8px 0 8px; }
.props label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.props textarea, .props input {
  display: block; width: 100%; margin-top: 4px; font: inherit; color: var(--text); padding: 6px 8px; resize: vertical;
  background: var(--surface); border: 1px solid var(--border); border-radius: 7px; outline: none;
}
.props textarea:focus, .props input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.case-row { display: flex; gap: 4px; margin-bottom: 4px; }
.case-row input { margin: 0; }
.case-row button, .props .small {
  border: 1px solid var(--border); background: var(--surface); border-radius: 7px; padding: 0 9px;
}
.case-row button:disabled { opacity: .4; cursor: default; }
.props .small { width: 100%; padding: 6px; margin-top: 2px; color: var(--accent-text); font-weight: 500; }
@media (hover: hover) { .props .small:hover { background: var(--accent-soft); border-color: transparent; } }
.case-heading { font-size: 12px; color: var(--muted); margin: 10px 0 4px; }
.case-row:last-of-type input { font-style: italic; }
.props #deleteBtn { width: 100%; margin-top: 8px; }
.prop-actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; margin-top: 10px; }
.prop-actions button {
  display: grid; place-items: center; height: 34px; padding: 0; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--muted);
}
@media (pointer: coarse) { .prop-actions button { height: 40px; } }
@media (hover: hover) { .prop-actions button:not(:disabled):hover { background: var(--accent-soft); color: var(--accent-text); border-color: transparent; } }
.prop-actions button:disabled, .menu-items button:disabled { opacity: .4; cursor: default; }
.menu-items button:disabled:hover { background: none; }

button.danger {
  border: 1px solid transparent; padding: 9px; border-radius: 8px; font-weight: 600;
  background: var(--danger-soft); color: var(--danger);
}
@media (hover: hover) { button.danger:not(:disabled):hover { background: var(--danger); color: #fff; } }
button.danger:disabled { opacity: .45; cursor: default; }
.collapse {
  display: flex; align-items: center; gap: 8px; border: 0; background: none; padding: 8px; border-radius: 8px;
  color: var(--muted); font-weight: 500; white-space: nowrap;
}
@media (hover: hover) { .collapse:hover { background: var(--hover); color: var(--text); } }
.collapse .arrow { font-size: 18px; line-height: 1; width: 16px; transition: transform .2s; }

body.collapsed #sidebar { width: 52px; padding: 12px 6px; }
body.collapsed .side-scroll { display: none; }
body.collapsed #sidebar { justify-content: flex-end; }
body.collapsed .collapse { font-size: 0; justify-content: center; }
body.collapsed .collapse .arrow { font-size: 18px; transform: rotate(180deg); }

/* =========================================================
   Canvas
   ========================================================= */
#canvas {
  flex: 1; overflow: auto; padding: 32px;
  background-color: var(--bg);
  background-image: radial-gradient(var(--dot) 1px, transparent 1px);
  background-size: 18px 18px;
}
/* The diagram is as wide as the visible canvas (wider, with scrolling, only when its content
   needs the room) and only as high as its content */
#diagram {
  display: flex; flex-direction: column; width: max-content; min-width: 100%;
  padding: 24px; color: var(--text);
  background: var(--paper); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow);
}

#diagram:empty { display: none; }

/* =========================================================
   Structogram
   ========================================================= */
.struct { flex: 1; display: flex; flex-direction: column; border: 1.5px solid var(--line); background: var(--paper); }
.struct > .title { padding: 8px 14px 10px 26px; font-weight: 600; cursor: text; }
.seq { display: flex; flex-direction: column; background: var(--slot); min-height: 2.6em; transition: background .12s; }
.seq.empty {
  background-image: repeating-linear-gradient(135deg, transparent 0 6px, rgba(127,127,160,.07) 6px 12px);
}
.struct > .seq {
  margin-left: 26px; margin-bottom: 14px;
  border-top: 1px solid var(--line); border-left: 1px solid var(--line); border-bottom: 1px solid var(--line);
}

.el { background: var(--paper); position: relative; }
.el.selected { outline: 2px solid var(--accent); outline-offset: -1px; z-index: 2; box-shadow: 0 0 0 5px var(--accent-soft); }
.el > .head, .el > .foot, .el.instruction, .el.call, .el.jump { cursor: pointer; }
.label { padding: 8px 12px; white-space: pre-wrap; word-break: break-word; min-height: 2.4em; line-height: 1.45; }
@media (hover: hover) { .label:hover, .el > .head:hover { background-color: var(--el-hover); } }

/* loops */
.el > .body { margin-left: 26px; border-left: 1px solid var(--line); }
.el.while > .body, .el.for > .body { border-top: 1px solid var(--line); margin-bottom: 1.1em; border-bottom: 1px solid var(--line); }
.el.dowhile > .body { margin-top: 1.1em; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }

/* jump: arrow-shaped left edge */
.el.jump > .label { position: relative; padding-left: 26px; }
.jump-mark { position: absolute; left: 0; top: 0; width: 14px; height: 100%; overflow: visible; }
.jump-mark path, .par svg path { stroke: var(--line); stroke-width: 1; fill: none; vector-effect: non-scaling-stroke; }

/* parallel: bars with corner diagonals above and below the threads */
.el.parallel > .par { position: relative; min-height: 1.9em; padding: 4px 2.4em; text-align: center; cursor: pointer; }
.el.parallel > .par.bottom { border-top: 1px solid var(--line); min-height: 1.4em; }
.par svg { position: absolute; top: 0; width: 1.6em; height: 100%; pointer-events: none; }
.par svg.l { left: 0; }
.par svg.r { right: 0; }
.thread-row { align-items: center; font-size: 13px; }
.thread-row span { flex: 1; }
.thread-row small { color: var(--muted); }
.thread-row button { height: 28px; }

/* call */
.el.call > .label { margin: 0 10px; border-left: 1px solid var(--line); border-right: 1px solid var(--line); }

/* if / switch */
.el.if > .head, .el.switch > .head { position: relative; min-height: 3.8em; }
.head svg { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; overflow: visible; }
.head svg line, .head svg path { stroke: var(--line); stroke-width: 1; fill: none; }
.head .cond { text-align: center; padding: 6px 12px 1.7em; position: relative; white-space: pre-wrap; }
.head .branch {
  position: absolute; bottom: 2px; font-size: 11.5px; padding: 0 5px; min-height: 0; color: var(--muted); font-weight: 500;
}
.head .branch:hover { background: none; }
.el.switch > .head { min-height: 6em; }
.el.switch > .head .cond { padding-bottom: 3.6em; }
.el.switch > .head .branch {
  bottom: 0; padding: 2px 4px 3px; text-align: center; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
}
.cols { display: flex; border-top: 1px solid var(--line); }
.cols > .seq { flex: 1 1 auto; min-width: 96px; }
.cols > .seq + .seq { border-left: 1px solid var(--line); }

/* drag & drop */
.seq.empty-over { background: var(--slot-hover); box-shadow: inset 0 0 0 2px var(--accent); }
.seq.end-over { background: var(--slot-hover); }
.el.dragged { opacity: .35; }

/* touch drag: no text selection / callout while holding, floating copy under the finger */
#diagram .el, .tool { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.tool { touch-action: pan-y; } /* sideways slide picks the element up */
.touch-ghost {
  position: fixed; z-index: 100; pointer-events: none; margin: 0; max-height: 180px; overflow: hidden;
  background: var(--paper); color: var(--text); border: 1.5px solid var(--accent); border-radius: 8px;
  box-shadow: var(--shadow-lg); opacity: .92;
}
.touch-ghost.tool { background: var(--surface); padding: 8px 12px; }

/* =========================================================
   Run (step through)
   ========================================================= */
.run-btn {
  margin-left: auto; display: flex; align-items: center; gap: 6px; padding: 6px 12px; border-radius: 8px;
  border: 1px solid var(--accent); background: var(--accent-soft); color: var(--accent-text); font-weight: 600; font-size: 13px;
}
.run-btn i { font-style: normal; font-size: 11px; }
@media (hover: hover) { .run-btn:hover { background: var(--accent); color: #fff; } }

#runPanel {
  width: 290px; flex-shrink: 0; display: flex; flex-direction: column; gap: 8px; padding: 12px; overflow-y: auto;
  background: var(--surface); border-left: 1px solid var(--border);
}
#runPanel[hidden] { display: none; }
#runPanel header { display: flex; justify-content: space-between; align-items: center; font-weight: 700; font-size: 15px; }
#runPanel .close {
  width: 30px; height: 30px; border-radius: 8px; border: 0; background: none; color: var(--muted); font-size: 20px; line-height: 1;
}
@media (hover: hover) { #runPanel .close:hover { background: var(--hover); color: var(--text); } }
#runPanel h3 { margin: 10px 0 0; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.run-controls { display: flex; gap: 6px; container-type: inline-size; }
.run-controls button {
  display: flex; align-items: center; justify-content: center; gap: 6px; flex: 1; padding: 8px 6px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface); font-weight: 600; font-size: 13px; white-space: nowrap;
}
.run-controls button i { font-style: normal; font-size: 11px; }
.run-controls #runRestart { flex: 0 0 38px; font-size: 16px; }
/* Not enough room for four labels (the sidebar, half of the bottom panel): Back and Step show
   only their icons (the tooltip names them), Play keeps its label as long as it fits */
@container (max-width: 330px) {
  #runBack span, #runStep span { display: none; }
  #runBack, #runStep { flex: 0 0 42px; }
}
@container (max-width: 240px) {
  #runPlay span { display: none; }
  .run-controls button { padding: 8px 4px; }
}
@media (hover: hover) { .run-controls button:not(:disabled):hover { background: var(--hover); } }
.run-controls .primary:not(:disabled), .run-controls .primary:not(:disabled):hover { background: var(--accent); border-color: var(--accent); color: #fff; }
.run-controls button:disabled { opacity: .45; cursor: default; }
.run-speed { display: flex; align-items: center; gap: 10px; font-size: 12px; color: var(--muted); }
.run-speed input { flex: 1; accent-color: var(--accent); }
.run-status { font-size: 13px; padding: 7px 10px; border-radius: 8px; background: var(--surface-2); color: var(--muted); }
.run-status.done { background: #e7f7ee; color: #1a7f45; }
.run-status.error { background: var(--danger-soft); color: var(--danger); font-weight: 500; }
#runVars { width: 100%; border-collapse: collapse; font: 12.5px/1.4 "JetBrains Mono", ui-monospace, Menlo, monospace; }
#runVars caption { caption-side: bottom; text-align: left; color: var(--muted); font: 12.5px "Inter", sans-serif; padding: 4px 0; }
#runVars td { padding: 4px 6px; border-bottom: 1px solid var(--border); vertical-align: top; word-break: break-all; }
#runVars td:first-child { color: var(--accent-text); width: 40%; }
#runVars tr.changed td { background: #fff3c4; }
#runOutput {
  margin: 0; min-height: 80px; max-height: 40vh; overflow: auto; padding: 10px 12px; border-radius: 9px; white-space: pre-wrap;
  background: var(--code-bg); color: var(--code-text); border: 1px solid var(--code-border);
  font: 12.5px/1.5 "JetBrains Mono", ui-monospace, Menlo, monospace;
}
.run-hint { margin: 4px 0 0; font-size: 11.5px; line-height: 1.5; color: var(--muted); }
.run-hint code { font-family: "JetBrains Mono", ui-monospace, monospace; font-size: 11px; }

/* the element that runs next / failed */
#diagram .run-current { background: #fff3c4 !important; box-shadow: inset 0 0 0 2px #f5a524; }
#diagram .run-error { background: var(--danger-soft) !important; box-shadow: inset 0 0 0 2px var(--danger); }
body.running #diagram, body.running #tabs, body.running #addTab, body.running .side-scroll { pointer-events: none; }
body.running .side-scroll { opacity: .45; }
body.running #diagram .el.selected { outline: 0; box-shadow: none; }


  :is([data-theme="midnight"], [data-theme="ember"]) #diagram .run-current, :is([data-theme="midnight"], [data-theme="ember"]) #runVars tr.changed td { background: #4a3a10 !important; }
  :is([data-theme="midnight"], [data-theme="ember"]) .run-status.done { background: #12321f; color: #5fd08f; }
/* Narrow windows and portrait iPad: the run panel sits at the bottom (two columns) so the
   diagram keeps the full width */
@media (max-width: 760px), (orientation: portrait) and (max-width: 1100px) {
  #runPanel {
    position: fixed; left: 0; right: 0; bottom: 0; width: auto; max-height: 42vh; z-index: 35;
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
    box-shadow: var(--shadow-lg); border-left: 0; border-top: 1px solid var(--border);
    display: grid; grid-template-columns: 1fr 1fr; gap: 8px 16px; align-content: start;
    grid-template-areas: "head head" "ctrl speed" "status status" "vh oh" "vars out";
  }
  #runPanel > header { grid-area: head; }
  #runPanel > .run-controls { grid-area: ctrl; }
  #runPanel > .run-speed { grid-area: speed; }
  #runPanel > .run-status { grid-area: status; }
  #runPanel > h3:nth-of-type(1) { grid-area: vh; margin: 0; }
  #runPanel > h3:nth-of-type(2) { grid-area: oh; margin: 0; }
  #runPanel > #runVars { grid-area: vars; align-self: start; }
  #runPanel > #runOutput { grid-area: out; max-height: none; }
  #runPanel > .run-hint { display: none; }
  /* room to scroll the diagram above the panel */
  main:has(#runPanel:not([hidden])) #canvas { padding-bottom: 44vh; }
}
@media (max-width: 480px) {
  #runPanel { grid-template-columns: 1fr; grid-template-areas: "head" "ctrl" "speed" "status" "vh" "vars" "oh" "out"; max-height: 55vh; }
}
/* Portrait iPad: a bit narrower sidebar, less padding around the diagram */
@media (orientation: portrait) and (max-width: 1100px) {
  #sidebar { width: 204px; }
  #canvas { padding: 16px; }
}

/* =========================================================
   Dialogs
   ========================================================= */
dialog {
  border: 1px solid var(--border); border-radius: 16px; padding: 24px; color: var(--text);
  background: var(--surface); box-shadow: var(--shadow-lg);
}
dialog[open] { animation: pop .16s ease-out; }
dialog::backdrop { background: rgba(15, 17, 21, .35); backdrop-filter: blur(4px); }

#startDialog { width: min(380px, calc(100vw - 32px)); }
#startDialog h4 { margin: 0 0 10px; font-size: 13px; font-weight: 600; color: var(--muted); }
#startDialog h4 + * { margin-top: 0; }
#startDialog h4:not(:first-child) { margin-top: 18px; padding-top: 18px; border-top: 1px solid var(--border); }
#startDialog input {
  width: 100%; padding: 10px 12px; font: inherit; color: var(--text); margin-bottom: 8px; outline: none;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 9px;
}
#startDialog input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
#startDialog button {
  display: block; width: 100%; padding: 10px; border-radius: 9px; font-weight: 600; margin-bottom: 8px;
  border: 1px solid var(--border); background: var(--surface);
}
@media (hover: hover) { #startDialog button:not(:disabled):hover { background: var(--hover); } }
#startDialog button:disabled { color: var(--muted); opacity: .6; cursor: default; }
#startDialog .lang-picker { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; }
#startDialog .lang-picker button { width: auto; margin: 0; padding: 7px 2px; font-size: 12.5px; font-weight: 500; }
#startDialog .lang-picker button.active,
#startDialog .lang-picker button.active:hover { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-text); }
#createBtn:not(:disabled), #startDialog button.primary:not(:disabled) { background: var(--accent); border-color: var(--accent); color: #fff; }
@media (hover: hover) { #createBtn:not(:disabled):hover, #startDialog button.primary:not(:disabled):hover { background: var(--accent); filter: brightness(1.08); } }
#startDialog button.primary { margin: 0; }

#askDialog { width: min(360px, calc(100vw - 32px)); padding: 20px; }
#askDialog h4 { margin: 0 0 12px; font-size: 15px; }
#askDialog input {
  width: 100%; padding: 10px 12px; font: inherit; color: var(--text); outline: none;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 9px;
}
#askDialog input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.ask-actions { display: flex; flex-direction: row-reverse; justify-content: flex-start; gap: 8px; margin-top: 16px; }
.ask-actions button { padding: 8px 14px; border-radius: 9px; font-weight: 600; border: 1px solid var(--border); background: var(--surface); }
@media (hover: hover) { .ask-actions button:hover { background: var(--hover); } }
.ask-actions .primary, .ask-actions .primary:hover { background: var(--accent); border-color: var(--accent); color: #fff; }

#editDialog { width: min(520px, calc(100vw - 32px)); padding: 20px; margin-top: max(24px, 8vh); }
#editDialog h4 { margin: 0 0 4px; font-size: 16px; }
#editDialog label { display: block; margin-bottom: 10px; font-size: 13px; color: var(--muted); }
#editDialog textarea {
  display: block; width: 100%; padding: 12px; font: 16px/1.5 "JetBrains Mono", ui-monospace, Menlo, monospace; color: var(--text);
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; outline: none; resize: vertical;
}
#editDialog textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
#editDialog .ask-actions button { min-width: 96px; padding: 10px 16px; }
/* touch: the sidebar field just shows the text, tapping it opens the dialog */
.props textarea[readonly] { cursor: pointer; }


dialog.info { width: min(660px, calc(100vw - 32px)); padding: 0; overflow: hidden; }
dialog.info header {
  display: flex; justify-content: space-between; align-items: center; padding: 16px 20px 0; font-weight: 700; font-size: 17px;
}
dialog.info .close {
  width: 30px; height: 30px; border-radius: 8px; border: 0; background: none; color: var(--muted); font-size: 20px; line-height: 1;
}
@media (hover: hover) { dialog.info .close:hover { background: var(--hover); color: var(--text); } }
#infoBody { padding: 10px 20px 20px; max-height: 70vh; overflow: auto; line-height: 1.55; }
#infoBody ul { padding-left: 20px; }
#infoBody a { color: var(--accent-text); }
#infoBody kbd, .menu-items kbd {
  padding: 1px 5px; border: 1px solid var(--border); border-radius: 5px; background: var(--surface-2); font-size: 12px;
}
.menu-items kbd { border: 0; background: none; padding: 0; }
#infoBody pre {
  background: var(--code-bg); color: var(--code-text); border: 1px solid var(--code-border); padding: 14px 16px; overflow: auto; margin: 10px 0 0; border-radius: 10px; max-height: 58vh;
  font: 13px/1.6 "JetBrains Mono", ui-monospace, Menlo, monospace;
}
#infoBody .hint { color: var(--muted); font-size: 13px; }
.sample-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 8px; }
.sample {
  display: flex; flex-direction: column; gap: 3px; text-align: left; padding: 11px 13px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface-2);
}
.sample b { font-size: 14px; }
.sample span { font-size: 12.5px; color: var(--muted); line-height: 1.4; }
@media (hover: hover) { .sample:hover { border-color: var(--accent); background: var(--accent-soft); } }
@media (hover: none) { .sample:active { border-color: var(--accent); background: var(--accent-soft); } }
.insert-btn {
  padding: 8px 16px; border-radius: 9px; border: 0; font-weight: 600; background: var(--accent); color: #fff;
}
@media (hover: hover) { .insert-btn:hover { filter: brightness(1.08); } }
.code-tabs { display: flex; gap: 4px; padding: 3px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 9px; width: fit-content; }
.code-tabs button { border: 0; background: none; padding: 5px 12px; border-radius: 7px; font-weight: 500; color: var(--muted); }
@media (hover: hover) { .code-tabs button:hover { color: var(--text); } }
.code-tabs button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow-sm); }
.code-box { position: relative; }
.code-box pre { padding-right: 90px !important; }
.code-box .copy-btn {
  position: absolute; top: 8px; right: 8px; padding: 5px 10px; border-radius: 7px; font-size: 12px; font-weight: 500;
  border: 1px solid var(--border); background: var(--surface); color: var(--muted);
}
@media (hover: hover) { .code-box .copy-btn:hover { background: var(--hover); color: var(--text); } }

/* Short windows: compact palette so all elements stay reachable */
@media (max-height: 640px) {
  #sidebar { padding-top: 8px; padding-bottom: 6px; gap: 4px; }
  .palette h3 { margin: 8px 4px 3px; }
  .tool { padding: 3px 8px; margin-bottom: 1px; }
  .tool i { width: 24px; height: 24px; }
  .collapse { padding: 6px 8px; }
}
@media (max-height: 460px) {
  .palette h3 { display: none; }
}

/* =========================================================
   Small screens
   ========================================================= */
@media (max-width: 640px) {
  .brand { margin-right: 6px; font-size: 0; gap: 0; }
  #sidebar { width: 184px; padding: 8px; }
  .tool { font-size: 13px; gap: 8px; padding: 6px; }
  .tool i { width: 24px; height: 24px; }
  #canvas { padding: 12px; }
  #diagram { padding: 12px; }
}

/* =========================================================
   Touch screens (iPad): bigger targets, no zoom when typing
   Hover effects only apply with a mouse/trackpad (@media (hover: hover)),
   otherwise they stay stuck after a tap. Touch gets a short press feedback.
   ========================================================= */
/* iOS zooms into fields with text smaller than 16px – on any iOS device, also with a trackpad */
@supports (-webkit-touch-callout: none) {
  input, textarea, select { font-size: max(16px, 1em) !important; }
}
@media (hover: none) {
  .menu-items button:not(:disabled):active, .tool:active, .tab:active, .collapse:active, #langSelect:active,
  .props .small:active, .prop-actions button:not(:disabled):active, .run-controls button:not(:disabled):active,
  #startDialog button:not(:disabled):active, .ask-actions button:active, dialog.info .close:active,
  #runPanel .close:active, .code-tabs button:active { background: var(--hover); }
  #addTab:active, .run-btn:active { background: var(--accent-soft); }
  .tab .x:active, button.danger:not(:disabled):active { background: var(--danger-soft); color: var(--danger); }
  .code-box .copy-btn:active { background: var(--hover); }
  .run-controls .primary:not(:disabled):active, #createBtn:not(:disabled):active, .insert-btn:active,
  #startDialog button.primary:not(:disabled):active, .ask-actions .primary:active { filter: brightness(1.12); }
}
@media (pointer: coarse) {
  .menubar { height: 52px; }
  .menu-title { padding: 9px 12px; }
  .menu-items button { padding: 11px 12px; font-size: 15px; }
  .menu-items kbd { display: none; }
  .tool { padding: 8px; margin-bottom: 3px; }
  .tool i { width: 32px; height: 32px; }
  .tab-name { padding: 9px 14px; }
  .tab .x { width: 30px; height: 30px; }
  #addTab { width: 38px; height: 38px; }
  .case-row button { min-width: 40px; }
  .collapse { padding: 11px 8px; }
  /* iOS zooms into fields with text smaller than 16px */
  .props textarea, .props input, #startDialog input, #askDialog input, #langSelect { font-size: 16px; }
  .label { min-height: 2.8em; }
}

/* Code font: show "<=", "!=", "->" as typed, not as ligature symbols (≤, ≠, →).
   Last in the file: the "font" shorthand on code blocks above would switch ligatures back on. */
pre, code, textarea, #runVars, #infoBody pre, #runOutput, #editDialog textarea {
  font-variant-ligatures: none; font-feature-settings: "liga" 0, "calt" 0;
}
body:not(.collapsed) #sidebar { width: var(--sidebar-width, 236px); transition: padding .2s; border-right: 0; }
@media (orientation: portrait) and (max-width: 1100px) { body:not(.collapsed) #sidebar { width: var(--sidebar-width, 204px); } }
@media (max-width: 640px) { body:not(.collapsed) #sidebar { width: var(--sidebar-width, 184px); } }
#sidebarGrip { flex: 0 0 10px; background: var(--surface-2); border-inline: 1px solid var(--border); display: grid; place-items: center; cursor: col-resize; touch-action: none; }
#sidebarGrip::after { content: ''; display: block; width: 3px; height: 34px; border-radius: 4px; background: var(--muted); }
#sidebarGrip:hover, #sidebarGrip:focus-visible, .resizing #sidebarGrip { background: var(--accent-soft); }
body.resizing { cursor: col-resize; user-select: none; }
body.collapsed #sidebarGrip { display: none; }
#imageDialog { width: min(660px, calc(100vw - 24px)); }
#imageDialog header, #imageDialog footer { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
#imageDialog h2 { margin: 0; letter-spacing: -.04em; }
.image-options { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; margin: 18px 0; }
.image-options label { display: grid; gap: 5px; font-weight: 600; }
.image-options select { width: 100%; padding: 8px 4px; background: var(--surface-2); color: var(--text); border: 1px solid var(--border); border-radius: 7px; font: inherit; }
.image-preview { background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; min-height: 80px; padding: 12px; display: grid; place-items: center; }
.image-preview img { display: block; max-width: 100%; max-height: 43vh; object-fit: contain; }
.image-preview img[hidden] { display: none; }
#imageDialog footer { margin-top: 14px; }
#imageSave { padding: 9px 15px; border: 0; border-radius: 8px; background: var(--accent); color: white; font-weight: 600; }
#imageSave:disabled { opacity: .55; }
.export-stage { position: absolute; left: -100000px; top: 0; width: max-content; min-width: 384px; padding: 20px;
  --paper: #fff; --line: #283446; --slot: #f0f3f8; --muted: #46556a; color: #172334; background: var(--paper); }
.export-stage[data-mode="dark"] { --paper: #202836; --line: #cbd5e5; --slot: #303c50; --muted: #c4d0df; color: #f4f7fb; }
.export-stage .struct > .title { background: var(--slot); }
.export-stage .seq.empty { background-image: none; }
