:root {
  --bg: #eef1f4;
  --surface: #ffffff;
  --surface-2: #f6f8fa;
  --border: #d3d9e0;
  --text: #1c2329;
  --muted: #5f6b76;
  --accent: #1f6feb;
  --start: #1a7f37;
  --reset: #b35900;
  --error: #c62828;
  --error-bg: #fdecea;
  --current-bg: #fff3b0;
  --alarm-bg: #ffcdd2;
  --code-font: ui-monospace, "Cascadia Mono", Consolas, "Courier New", monospace;
  --line-height: 22px;

  /* Simulazione (letti anche da js/render/lathe-2d.js: i colori del materiale vanno in #rrggbb) */
  --sim-bg: #f7f9fb;
  --sim-grid: #e3e8ee;
  --sim-text: #7a8692;
  --sim-axis: #5f6b76;
  --sim-origin: #1c2329;
  --sim-material: #8d99a6;
  --sim-material-hidden: rgba(141, 153, 166, 0.45);
  --sim-chuck: #4b5561;
  --sim-chuck-edge: #2b323a;
  --sim-jaw: #6b7684;
  --sim-tool: #e0a526;
  --sim-tool-edge: #6d4c00;
  --sim-holder: #b9c1ca;
  --sim-holder-edge: #6f7a86;
  --sim-rapid: #d7263d;
  --sim-feed: #1f6feb;
  --sim-collision: #d7263d;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11161b;
    --surface: #1a2027;
    --surface-2: #151a20;
    --border: #2e3843;
    --text: #e3e8ed;
    --muted: #93a0ac;
    --accent: #58a6ff;
    --start: #3fb950;
    --reset: #f0883e;
    --error: #ff7b72;
    --error-bg: #3a1d1d;
    --current-bg: #4a4213;
    --alarm-bg: #5c1f1f;

    --sim-bg: #0f1419;
    --sim-grid: #1f2830;
    --sim-text: #6e7b87;
    --sim-axis: #93a0ac;
    --sim-origin: #e3e8ed;
    --sim-material: #7d8995;
    --sim-material-hidden: rgba(125, 137, 149, 0.35);
    --sim-chuck: #2e3843;
    --sim-chuck-edge: #56626e;
    --sim-jaw: #46515d;
    --sim-tool: #e3b341;
    --sim-tool-edge: #fff3c4;
    --sim-holder: #3d4955;
    --sim-holder-edge: #8b98a5;
    --sim-rapid: #ff6b7a;
    --sim-feed: #58a6ff;
    --sim-collision: #ff6b7a;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, "Segoe UI", Roboto, sans-serif;
}

button, select, input { font: inherit; color: inherit; }

button, select, .file-name {
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled, select:disabled { opacity: 0.45; cursor: not-allowed; }
button:focus-visible, select:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.file-name { width: 9em; cursor: text; }
#examples, #machine { max-width: min(22em, 100%); text-overflow: ellipsis; }

.btn-start:not(:disabled) { background: var(--start); border-color: var(--start); color: #fff; font-weight: 600; }
.btn-reset:not(:disabled) { color: var(--reset); border-color: var(--reset); font-weight: 600; }

/* Intestazione e barra comandi */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: baseline; gap: 10px; }
.brand-name { font-size: 20px; font-weight: 700; letter-spacing: 0.02em; }
.brand-sub { color: var(--muted); }
.version { font-size: 12px; color: var(--muted); font-family: var(--code-font); }

.field { display: flex; align-items: center; gap: 8px; color: var(--muted); }
.field-inline input[type="range"] { width: 110px; }
.field output { font-family: var(--code-font); min-width: 3.5em; color: var(--text); }
.check { display: flex; align-items: center; gap: 4px; color: var(--muted); white-space: nowrap; }

.toolbar {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 8px 24px;
  padding: 8px 16px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
}
.toolbar-group { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; min-width: 0; max-width: 100%; }

/* Disposizione */
.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  gap: 12px;
  padding: 12px 16px;
  grid-template-columns: minmax(0, 0.85fr) minmax(0, 0.6fr) minmax(0, 0.6fr);
  grid-template-rows: minmax(0, 1.8fr) minmax(0, 1fr);
  grid-template-areas:
    "editor sim sim"
    "editor block alarms";
}
.panel-editor { grid-area: editor; }
.panel-sim { grid-area: sim; }
.panel-block { grid-area: block; }
.panel-alarms { grid-area: alarms; }

.panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.panel > div:last-child { flex: 1; min-height: 0; overflow: auto; }
.panel-title {
  margin: 0;
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
}
#block, #alarms { padding: 10px 12px; }

/* Editor */
.editor {
  display: flex;
  font-family: var(--code-font);
  font-size: 15px;
  overflow: hidden !important;
}
.editor-gutter {
  flex: none;
  width: 3.5em;
  overflow: hidden;
  background: var(--surface-2);
  border-right: 1px solid var(--border);
  color: var(--muted);
  text-align: right;
  user-select: none;
}
.editor-gutter-inner { padding: 8px 8px 8px 0; }
.gutter-line { height: var(--line-height); line-height: var(--line-height); }
.gutter-line.has-error { color: var(--error); font-weight: 700; }
.gutter-line.has-error::before { content: "● "; font-size: 10px; vertical-align: 1px; }
.gutter-line.is-current { color: var(--text); font-weight: 700; }

.editor-code { position: relative; flex: 1; min-width: 0; overflow: hidden; }
.editor-highlights { position: absolute; inset: 0; pointer-events: none; }
.hl { position: absolute; left: 0; right: 0; }
.hl-current { background: var(--current-bg); }
.hl-alarm { background: var(--alarm-bg); }
.hl-error { background: var(--error-bg); border-left: 3px solid var(--error); }

.editor-textarea {
  position: relative;
  display: block;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 8px 12px;
  border: 0;
  resize: none;
  outline: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  line-height: var(--line-height);
  white-space: pre;
  overflow: auto;
  tab-size: 2;
  text-transform: none;
}
.editor.is-readonly .editor-textarea { cursor: default; }

/* Simulazione */
.panel-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 4px 12px;
  border-bottom: 1px solid var(--border);
  padding-right: 8px;
}
.panel-head .panel-title { border-bottom: 0; }
.setup { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; font-size: 13px; color: var(--muted); padding: 4px 0; }
.setup input {
  width: 4.2em;
  padding: 2px 4px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  font-family: var(--code-font);
}
.setup input:disabled { opacity: 0.6; }
.btn-small { padding: 2px 8px; font-size: 13px; }

.panel > div.sim-body {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  background: var(--sim-bg);
}
.sim-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  cursor: grab;
  touch-action: none;
}
.sim-canvas:active { cursor: grabbing; }
.sim-canvas[hidden] { display: none; }
.setup-fields { display: contents; }
.setup select {
  padding: 2px 4px;
  border-radius: 4px;
  font-size: 13px;
}

.dro {
  position: absolute;
  top: 8px;
  right: 8px;
  display: grid;
  grid-template-columns: auto auto;
  gap: 1px 10px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  font-size: 13px;
  pointer-events: none;
}
.dro div { display: contents; }
.dro span { color: var(--muted); }
.dro b { font-family: var(--code-font); font-weight: 600; text-align: right; }

.tools {
  position: absolute;
  left: 8px;
  top: 8px;
  max-width: calc(100% - 16px);
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  font-size: 13px;
}
.tools summary { cursor: pointer; color: var(--muted); }
.tools[open] { max-height: calc(100% - 16px); overflow: auto; }
.tool-table { border-collapse: collapse; margin: 6px 0 2px; }
.tool-table th, .tool-table td { padding: 3px 6px; text-align: left; vertical-align: top; border-bottom: 1px solid var(--border); }
.tool-table thead th { color: var(--muted); font-weight: 600; font-size: 12px; }
.tool-table .muted { font-size: 12px; }
.tool-table input {
  width: 5.2em;
  padding: 2px 4px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  font-family: var(--code-font);
}
.tool-note { max-width: 34em; margin: 6px 0 2px; font-size: 12px; }
.tool-id { font-family: var(--code-font); font-weight: 700; color: var(--accent); }

/* Blocco corrente */
.muted { color: var(--muted); margin: 0; }
.block-source { margin: 0 0 8px; font-family: var(--code-font); font-weight: 700; }
.word-list { list-style: none; margin: 0; padding: 0; }
.word-list li { display: flex; gap: 10px; padding: 3px 0; border-bottom: 1px solid var(--border); }
.word { flex: none; min-width: 4.5em; font-family: var(--code-font); font-weight: 700; color: var(--accent); }
.word-desc { color: var(--text); }
.block-comment { margin: 8px 0 0; color: var(--muted); font-style: italic; }

/* Allarmi */
.alarm-ok { margin: 0; color: var(--start); }
.alarm-title { margin: 0 0 8px; font-weight: 600; color: var(--error); }
.alarm-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.alarm-item {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2px 10px;
  width: 100%;
  padding: 8px 10px;
  text-align: left;
  background: var(--error-bg);
  border: 1px solid transparent;
  border-left: 4px solid var(--error);
}
.alarm-item.is-active { border-color: var(--error); }
.alarm-code { font-family: var(--code-font); font-weight: 700; color: var(--error); }
.alarm-line { color: var(--muted); }
.alarm-message { grid-column: 1 / -1; font-weight: 600; }
.alarm-hint { grid-column: 1 / -1; color: var(--muted); font-size: 14px; }

/* Barra di stato */
.status-bar {
  display: flex;
  gap: 16px;
  align-items: center;
  padding: 6px 16px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  font-size: 14px;
  min-height: 34px;
}
.status-state {
  padding: 2px 10px;
  border-radius: 4px;
  font-weight: 700;
  text-transform: uppercase;
  font-size: 12px;
  letter-spacing: 0.05em;
  background: var(--surface-2);
  border: 1px solid var(--border);
}
body[data-state="running"] .status-state { background: var(--start); border-color: var(--start); color: #fff; }
body[data-state="paused"] .status-state { background: var(--current-bg); }
body[data-state="alarm"] .status-state { background: var(--error); border-color: var(--error); color: #fff; }
body[data-state="finished"] .status-state { background: var(--accent); border-color: var(--accent); color: #fff; }
body[data-state="alarm"] .status-message { color: var(--error); font-weight: 600; }
.status-line { font-family: var(--code-font); color: var(--muted); }

/* Schermi stretti: una colonna, la pagina scorre */
@media (max-width: 900px) {
  html, body { height: auto; }
  .layout {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: none;
    grid-template-areas: "editor" "sim" "alarms" "block";
  }
  .panel-editor { height: 55vh; }
  .panel-sim { height: 60vh; }
  .status-bar { position: sticky; bottom: 0; flex-wrap: wrap; }
}

/* Guida */
.header-actions { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.help-dialog {
  width: min(900px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  /* su iPhone le barre di Safari rubano altezza: con i vh il riquadro
     sbordava sopra il bordo dello schermo e la testata spariva */
  max-height: calc(100dvh - 32px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
}
.help-dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
.help { display: flex; flex-direction: column; max-height: calc(100vh - 34px); max-height: calc(100dvh - 34px); }
.help-head {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 16px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
}
.help-head h2 { margin: 0; font-size: 18px; }
.help-tabs { display: flex; gap: 6px; flex-wrap: wrap; flex: 1; }
.help-tabs [aria-selected="true"] { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.help-close { padding: 4px 10px; }
.help-body { overflow: auto; padding: 12px 16px 20px; }
.help-body h3 { margin: 16px 0 6px; font-size: 15px; }
.help-body code, .help-body kbd { font-family: var(--code-font); }
.help-body kbd { padding: 1px 6px; border: 1px solid var(--border); border-radius: 4px; background: var(--surface-2); white-space: nowrap; }
.help-steps li, .help-notes li { margin: 4px 0; }
.help-table { border-collapse: collapse; width: 100%; }
.help-table th, .help-table td { text-align: left; vertical-align: top; padding: 4px 8px; border-bottom: 1px solid var(--border); }
.help-table th { white-space: nowrap; width: 1%; color: var(--accent); }
.help-table .is-planned { opacity: 0.65; }
.help-columns { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 0 24px; }

/* Scheda della macchina scelta dal database */
#model { max-width: min(18em, 100%); }
.tools.machine-info { top: auto; bottom: 8px; }
.machine-info[open] { max-width: min(34em, calc(100% - 16px)); }
.machine-info .tool-table th { white-space: nowrap; color: var(--muted); font-weight: 600; }
.model-iso { margin: 6px 0; padding: 4px 8px; border-radius: 4px; font-size: 12px; }
.model-iso-yes { background: color-mix(in srgb, var(--start) 15%, transparent); }
.model-iso-mode { background: var(--current-bg); }
.model-iso-no { background: var(--error-bg); }
.machine-info a { color: var(--accent); }

/* Da telefono i menu dell'intestazione si restringono invece di uscire dallo schermo */
.header-actions { min-width: 0; max-width: 100%; }
.header-actions .field { min-width: 0; max-width: 100%; }
.header-actions .field select { min-width: 0; max-width: 100%; }

/* ------------------------------------------------------------------
   Tema "notte": fondo scuro, un solo colore d'accento (il verde del logo
   della scuola), comandi sobri. Stessa impostazione della schermata di
   apertura, pensata per essere proiettata: contrasto alto e poco rumore.
   Ogni pannello resta riconoscibile da una sottile barra colorata.
   ------------------------------------------------------------------ */
:root {
  --lime: #86d24a;          /* accento, ripreso dal logo IIS Giulio Natta */
  --lime-soft: #a8e276;

  --panel-editor: #3aa7c4;
  --panel-sim: #4cbf7d;
  --panel-block: #7d8cf0;
  --panel-alarms: #f0688a;

  --bg: #070b15;
  --surface: #111828;
  --surface-2: #0c1220;
  --border: #1f2a42;
  --text: #e9eefb;
  --muted: #94a2be;
  --accent: var(--lime);

  --start: var(--lime);
  --reset: #f0883e;
  --error: #ff7f78;
  --error-bg: #351b1e;
  --current-bg: #3a3412;
  --alarm-bg: #3d1d22;

  --page-bg:
    radial-gradient(90% 60% at 10% 0%, rgba(134, 210, 74, 0.07), transparent 60%),
    radial-gradient(80% 60% at 100% 100%, rgba(125, 140, 240, 0.08), transparent 55%),
    #070b15;
  --soft-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);

  /* Colori della simulazione: li legge anche js/render/lathe-2d.js */
  --sim-bg: #080d18;
  --sim-grid: #16203a;
  --sim-text: #7787a6;
  --sim-axis: #94a2be;
  --sim-origin: #e9eefb;
  --sim-material: #7d8995;
  --sim-material-hidden: rgba(125, 137, 149, 0.35);
  --sim-chuck: #404a59;
  --sim-chuck-edge: #232a36;
  --sim-jaw: #5b6675;
  --sim-tool: #e0a526;
  --sim-tool-edge: #6d4c00;
  --sim-holder: #8b95a3;
  --sim-holder-edge: #5b6675;
  --sim-rapid: #ff6b7d;
  --sim-feed: #5cc8ff;
  --sim-collision: #ff4d5e;
}

body { background: var(--page-bg); background-attachment: fixed; }

/* Intestazione: una riga sobria, il nome spaziato, i menu senza pillole */
.app-header {
  background: linear-gradient(180deg, #0b1122 0%, #080d1a 100%);
  border-bottom: 1px solid var(--border);
  color: var(--text);
}
.brand-name { color: #fff; font-weight: 600; letter-spacing: 0.16em; }
.brand-sub { color: var(--muted); font-size: 13px; letter-spacing: 0.12em; }
.version { color: #5f6d8a; }
.app-header > .header-actions { flex: 1 1 auto; }
.header-actions .field > span {
  padding: 0;
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.14em;
}

/* Comandi: fondo scuro, bordo sottile, l'accento solo dove serve */
button, select, .file-name {
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  transition: border-color 0.15s ease, background 0.15s ease;
}
button { font-weight: 600; }
button:hover:not(:disabled), select:hover:not(:disabled) { border-color: var(--lime); }
select option { background: var(--surface); color: var(--text); }

#btn-start:not(:disabled) {
  background: var(--lime);
  border-color: var(--lime);
  color: #0c2010;
  font-weight: 700;
  box-shadow: 0 0 18px rgba(134, 210, 74, 0.25);
}
#btn-start:not(:disabled):hover { background: var(--lime-soft); border-color: var(--lime-soft); }
#btn-reset:not(:disabled) { color: var(--reset); border-color: color-mix(in srgb, var(--reset) 55%, var(--border)); }
#btn-pause:not(:disabled) { color: #f2c14e; border-color: color-mix(in srgb, #f2c14e 45%, var(--border)); }
#btn-intro, #btn-help { color: var(--muted); }
#btn-intro:hover, #btn-help:hover { color: var(--text); }

.toolbar { background: rgba(12, 18, 32, 0.75); border-bottom: 1px solid var(--border); }
.field, .check { color: var(--muted); font-weight: 600; }
.check input, #speed { accent-color: var(--lime); }

/* Pannelli: nessun riempimento colorato, solo una barra sottile che li distingue */
.panel-editor { --pc: var(--panel-editor); }
.panel-sim { --pc: var(--panel-sim); }
.panel-block { --pc: var(--panel-block); }
.panel-alarms { --pc: var(--panel-alarms); }
.panel {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--soft-shadow);
}
.panel-title {
  position: relative;
  padding-left: 22px;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.14em;
  border-bottom: 1px solid var(--border);
}
.panel-title::before {
  content: "";
  position: absolute;
  left: 12px;
  top: 50%;
  width: 3px;
  height: 13px;
  transform: translateY(-50%);
  border-radius: 2px;
  background: var(--pc, var(--muted));
}
.panel-head { background: transparent; border-bottom: 1px solid var(--border); }
.panel-head .setup { color: var(--muted); }
.status-bar { background: rgba(12, 18, 32, 0.9); border-top: 1px solid var(--border); }

/* Allarmi: il rosso resta il segnale piu' forte dell'interfaccia */
.alarm-item { border-left-width: 3px; border-radius: 0 8px 8px 0; }

/* Scritte in stampatello maiuscolo per etichette e comandi. Restano in
   minuscolo il testo del programma e tutte le frasi lunghe da leggere:
   in stampatello si leggono peggio, anche al proiettore. */
body, button, select, select option, input, textarea { text-transform: uppercase; }
.editor-textarea, .file-name, .machine-info a,
.alarm-message, .alarm-hint, .word-desc, .block-comment, .muted, .tool-note,
.help-body, .status-message { text-transform: none; }

/* ------------------------------------------------------------------
   Telefoni: una scheda alla volta a tutto schermo
   Sotto i 700 px l'interfaccia cambia impostazione: le schede scelgono
   il pannello da mostrare e i comandi di esecuzione stanno in basso.
   ------------------------------------------------------------------ */
.tab-bar, .run-bar { display: none; }

@media (max-width: 700px) {
  html, body { height: 100%; }
  body { overflow: hidden; }

  /* Intestazione compatta: titolo su una riga, i tre menu su una riga sola
     che scorre di lato, la Guida sempre visibile in alto a destra */
  .app-header { position: relative; padding: 6px 10px; gap: 4px; padding-right: 10px; }
  .brand-sub, .version { display: none; }
  .brand-name { font-size: 17px; }
  .app-header > .header-actions {
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    align-items: center;
    gap: 8px;
    width: 100%;
    padding-bottom: 2px;
  }
  .header-actions .field { flex: 0 0 auto; gap: 4px; }
  .header-actions .field > span { padding: 4px 10px; font-size: 11px; }
  .header-actions select { padding: 6px 10px; }
  #btn-help { position: absolute; top: 6px; right: 10px; padding: 4px 12px; }

  /* Barra comandi: i due gruppi uno sotto l'altro, ciascuno su una riga che
     scorre di lato. Prima il contenuto usciva dal gruppo e finiva sopra
     quello accanto (Esempi sopra Velocita'). */
  .toolbar { flex-wrap: wrap; justify-content: flex-start; gap: 6px; padding: 6px 10px; }
  .toolbar > .toolbar-group {
    flex: 1 0 100%;
    max-width: none;
    flex-wrap: nowrap;
    overflow-x: auto;
    gap: 8px;
    padding-bottom: 2px;
  }
  .toolbar-group > * { flex: 0 0 auto; }
  .toolbar button, .toolbar select, .file-name { white-space: nowrap; }
  .toolbar .file-name { width: 7em; }
  .toolbar #examples { max-width: 100%; }
  .field-inline input[type="range"] { width: 90px; }

  /* Le schede */
  .tab-bar { display: flex; gap: 6px; padding: 8px 10px 0; }
  .tab {
    flex: 1;
    min-width: 0;
    padding: 10px 4px;
    border: 1px solid var(--border);
    border-radius: 10px 10px 0 0;
    background: var(--surface);
    color: var(--muted);
    font-size: 13px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.02em;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .tab[aria-selected="true"] {
    background: color-mix(in srgb, var(--pc, var(--accent)) 22%, var(--surface));
    border-color: var(--pc, var(--accent));
    color: var(--text);
    box-shadow: inset 0 -3px 0 var(--pc, var(--accent));
  }
  /* Ogni scheda prende il colore del suo pannello */
  .tab[data-tab="editor"] { --pc: var(--panel-editor); }
  .tab[data-tab="sim"] { --pc: var(--panel-sim); }
  .tab[data-tab="block"] { --pc: var(--panel-block); }
  .tab[data-tab="alarms"] { --pc: var(--panel-alarms); }

  /* Allarme scattato mentre si guarda un'altra scheda */
  .tab.has-alarm { color: var(--error); border-color: var(--error); }
  .tab.has-alarm::after { content: " ●"; }

  /* Un solo pannello alla volta, alto quanto lo spazio disponibile */
  .layout { display: block; padding: 8px 10px 10px; overflow: hidden; }
  .layout .panel { display: none; height: 100%; border-radius: 0 0 14px 14px; }
  body[data-tab="editor"] .panel-editor,
  body[data-tab="sim"] .panel-sim,
  body[data-tab="block"] .panel-block,
  body[data-tab="alarms"] .panel-alarms { display: flex; }

  /* Il titolo del pannello e' gia' scritto sulla scheda attiva */
  .layout .panel .panel-title { display: none; }

  /* Simulazione: i campi del grezzo su una riga sola, per lasciare
     piu' spazio possibile al disegno del pezzo */
  .panel-sim .panel-head { padding: 4px 8px; }
  .panel-sim .setup { flex-wrap: nowrap; overflow-x: auto; gap: 8px; font-size: 12px; padding: 2px 0; white-space: nowrap; }
  .panel-sim .setup label { white-space: nowrap; }
  .panel-sim .setup > * { flex: 0 0 auto; }
  .panel-sim .setup input { width: 4em; }

  /* Comandi di esecuzione in basso, grandi abbastanza per il pollice */
  .run-bar {
    display: flex;
    gap: 8px;
    padding: 8px 10px;
    background: color-mix(in srgb, var(--surface) 80%, transparent);
    border-top: 1px solid var(--border);
  }
  .run-bar button { flex: 1; min-width: 0; padding: 12px 4px; font-size: 14px; font-weight: 700; line-height: 1.15; }

  /* Quote della simulazione: piu' piccole e in basso a destra, cosi' non
     coprono ne' il pezzo ne' il pulsante degli utensili */
  .dro { top: auto; bottom: 8px; right: 8px; font-size: 12px; padding: 4px 8px; gap: 0 8px; }

  /* Evita lo zoom automatico dei campi di testo su iPhone */
  input[type="text"], input[type="number"], select { font-size: 16px; }

  .status-bar { padding: 4px 10px; min-height: 28px; font-size: 13px; }
}

/* ------------------------------------------------------------------
   Presentazione del progetto: schermata di apertura e riquadro QR
   Fondo scuro e molta aria: pensata anche per essere proiettata.
   ------------------------------------------------------------------ */
.intro-dialog {
  width: min(1000px, 94vw);
  max-height: 92vh;
  /* su iPhone le barre di Safari mangiano altezza: dvh tiene conto di quelle */
  max-height: 92dvh;
  padding: 0;
  border: 0;
  border-radius: 20px;
  /* se il contenuto non ci sta (telefoni bassi) si scorre dentro il riquadro */
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  color: #f2f5fb;
  background:
    radial-gradient(120% 90% at 12% 0%, rgba(122, 214, 160, 0.16), transparent 60%),
    radial-gradient(100% 80% at 100% 100%, rgba(129, 90, 236, 0.22), transparent 55%),
    linear-gradient(160deg, #10182c 0%, #0c1120 55%, #131029 100%);
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.55);
}
.intro-dialog::backdrop { background: rgba(6, 9, 18, 0.75); backdrop-filter: blur(3px); }

.intro {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  min-width: 0;
  align-items: center;
  gap: 40px;
  padding: 44px 48px 40px;
}

.intro-main { min-width: 0; }

.intro-school {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 0 0 22px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: #b8c3d9;
}
.intro-logo {
  width: 68px;
  height: 68px;
  object-fit: contain;   /* scritte bianche su fondo trasparente: nessuna piastrella */
}

.intro-title {
  margin: 0;
  /* resta sempre su una riga sola: si rimpicciolisce invece di andare a capo */
  white-space: nowrap;
  font-size: clamp(20px, 5vw, 76px);
  font-weight: 300;
  line-height: 1;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: #fff;
}
/* Il marchio TOL(2)ERANZA0: lo zero e il 2 in apice nel verde della scuola.
   Le stesse classi valgono nella schermata di apertura e nell'intestazione. */
.mark-zero { color: #86d24a; font-weight: 600; }
.mark-exp {
  font-size: 0.5em;
  font-weight: 400;
  vertical-align: super;
  letter-spacing: 0;
  color: #86d24a;
}

/* Nella schermata di apertura lo zero va e viene: parte da un verde
   chiarissimo, quasi trasparente, e arriva al verde pieno della scuola.
   Nell'intestazione resta fermo: accanto c'e' il programma da leggere. */
.intro-title .mark-zero { animation: respiro-zero 2.8s ease-in-out infinite; }

@keyframes respiro-zero {
  0%, 100% { color: rgba(168, 226, 118, 0.2); }
  50% { color: #86d24a; }
}

/* Chi ha chiesto meno animazioni al sistema non la vede */
@media (prefers-reduced-motion: reduce) {
  .intro-title .mark-zero { animation: none; }
}

.intro-claim {
  margin: 14px 0 0;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: #86d24a;
}

/* I testi lunghi restano in minuscolo: in stampatello si leggono peggio
   (come gia' si fa per l'editor e i collegamenti) */
.intro-text, .intro-credits, .intro-credits-title, .intro-qr-url, .intro-skip { text-transform: none; }

/* La prima frase e' il gradino fra il titolo e i nomi: piu' grande e piu'
   chiara, con aria attorno. La seconda resta una postilla. */
.intro-text {
  max-width: 46ch;
  margin: 26px 0 0;
  font-size: 19px;
  line-height: 1.55;
  color: #e4ebfa;
}

/* la seconda frase e' la postilla che rassicura: piu' piccola e attenuata */
.intro-text + .intro-text {
  margin-top: 12px;
  font-size: 15px;
  line-height: 1.55;
  color: #9dabc6;
}

/* l'ultima parola richiama il giallo del pulsante che sta subito sotto */
.intro-try { color: #f5c451; font-weight: 700; }

.intro-credits-title {
  margin: 26px 0 6px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: #6f7d99;
}

.intro-credits {
  margin: 0;
  padding: 0;
  list-style: none;
  font-size: 14px;
  line-height: 1.55;
  color: #aab6cf;
}
.intro-credits b { color: #f2f5fb; font-weight: 600; }
.intro-role { color: #6f7d99; }

.intro-actions { display: flex; align-items: center; gap: 18px; flex-wrap: wrap; margin-top: 30px; }

.intro-enter {
  padding: 14px 34px;
  border: 0;
  border-radius: 999px;
  /* giallo caldo: e' l'unico invito ad agire della schermata, il verde
     resta ai comandi della macchina */
  background: #f5c451;
  color: #26190a;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: 0.02em;
  box-shadow: 0 0 34px rgba(245, 196, 81, 0.35);
  transition: transform 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}
.intro-enter:hover {
  transform: translateY(-1px);
  background: #ffd36b;
  box-shadow: 0 0 46px rgba(245, 196, 81, 0.55);
}

.intro-skip { display: flex; align-items: center; gap: 6px; font-size: 13px; color: #8b97b2; }
.intro-skip input { accent-color: #86d24a; }

.intro-qr {
  display: grid;
  justify-items: center;
  gap: 6px;
  padding: 18px 18px 14px;
  border-radius: 16px;
  background: #fff;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.35);
}
.intro-qr img { width: 208px; height: 208px; display: block; }
.intro-qr-title {
  /* stesso giallo del pulsante: sulla targhetta bianca fa da sfondo,
     perche' scritto in giallo su bianco non si leggerebbe */
  margin: 8px 0 0;
  padding: 5px 12px;
  border-radius: 999px;
  background: #f5c451;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #26190a;
}
.intro-qr-url { margin: 0; font-size: 11px; color: #5f6b76; font-family: var(--code-font); }

.intro-version {
  position: absolute;
  right: 18px;
  bottom: 10px;
  margin: 0;
  font-size: 11px;
  font-family: var(--code-font);
  color: #5d6a86;
}

/* Riquadro con il QR sempre visibile, per chi guarda la proiezione.
   Sta nell'angolo dell'intestazione: non copre nessun pannello. */
.app-header { position: relative; padding-right: 150px; }
.qr-badge {
  position: absolute;
  right: 14px;
  top: 50%;
  transform: translateY(-50%);
  z-index: 5;
  display: grid;
  justify-items: center;
  gap: 4px;
  padding: 8px 8px 6px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #fff;
  box-shadow: 0 10px 26px rgba(0, 0, 0, 0.28);
  cursor: pointer;
}
.qr-badge img { width: 84px; height: 84px; display: block; }
.qr-badge span {
  font-size: 10px;
  font-weight: 700;
  line-height: 1.25;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: #10182c;
  text-align: center;
}
.qr-badge:hover { border-color: #86d24a; }

@media (max-width: 760px) {
  .intro { grid-template-columns: minmax(0, 1fr); gap: 26px; padding: 32px 26px 30px; }
  .intro-qr { justify-self: start; }
  .intro-qr img { width: 168px; height: 168px; }
}

/* Sul telefono il QR non serve nell'intestazione: il sito e' gia' aperto li' */
@media (max-width: 700px) {
  .qr-badge { display: none; }
  .intro { padding: 20px 18px 22px; gap: 16px; }
  .intro-logo { width: 52px; height: 52px; }
  .intro-school { margin-bottom: 14px; font-size: 12px; }
  .intro-text { margin-top: 16px; font-size: 16.5px; line-height: 1.5; }
  .intro-text + .intro-text { margin-top: 8px; font-size: 14px; }
  .intro-credits-title { margin-top: 16px; }
  .intro-credits { font-size: 13px; line-height: 1.45; }
  .intro-actions { margin-top: 20px; gap: 12px; }
  .intro-qr { padding: 14px 14px 10px; }
  .intro-qr img { width: 150px; height: 150px; }
}

/* Il riquadro del QR e' un contenitore con il suo pulsante di chiusura */
.qr-badge { padding: 0; }
.qr-badge[hidden] { display: none; }
.qr-badge-open {
  display: grid;
  justify-items: center;
  gap: 4px;
  padding: 8px 8px 6px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  cursor: pointer;
}
.qr-badge-close {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 22px;
  height: 22px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--surface);
  color: var(--muted);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
}

/* ------------------------------------------------------------------
   Guida: il pulsante si fa notare, e la scheda "In parole semplici"
   ------------------------------------------------------------------ */

/* Contorno giallo: visibile anche da lontano, ma senza gridare come
   il pulsante Avvia. Al passaggio del mouse si riempie. */
#btn-help {
  color: #f5c451;
  border-color: color-mix(in srgb, #f5c451 50%, var(--border));
  font-weight: 700;
}
#btn-help:hover:not(:disabled) {
  background: #f5c451;
  border-color: #f5c451;
  color: #26190a;
}

/* La scheda attiva della guida prende lo stesso giallo */
.help-tabs [aria-selected="true"] { border-color: #f5c451; color: #f5c451; font-weight: 700; }

.help-lead {
  margin: 4px 0 18px;
  padding: 12px 16px;
  border-left: 3px solid var(--lime);
  border-radius: 0 10px 10px 0;
  background: rgba(134, 210, 74, 0.07);
  font-size: 15px;
  line-height: 1.6;
  color: var(--text);
}

.help-body p { line-height: 1.6; }

/* Le sei parole da sapere */
.help-glossary {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 8px 18px;
  margin: 6px 0 0;
}
.help-glossary dt { font-weight: 700; color: var(--text); }
.help-glossary dd { margin: 0; color: var(--muted); }

/* Gli stessi colori del disegno della simulazione */
.help-rapid { color: var(--sim-rapid); }
.help-feed { color: var(--sim-feed); }

@media (max-width: 700px) {
  .help-glossary { grid-template-columns: minmax(0, 1fr); gap: 2px 0; }
  .help-glossary dd { margin-bottom: 8px; }
}

/* I tre passi come cartoline numerate: il numero verde tira l'occhio */
.help-cards {
  counter-reset: passo;
  list-style: none;
  margin: 8px 0 0;
  padding: 0;
  display: grid;
  gap: 8px;
}
.help-cards li {
  counter-increment: passo;
  position: relative;
  padding: 10px 14px 10px 46px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.02);
  line-height: 1.5;
}
.help-cards li::before {
  content: counter(passo);
  position: absolute;
  left: 14px;
  top: 8px;
  font-size: 20px;
  font-weight: 700;
  color: var(--lime);
}

/* I due colori del disegno, mostrati invece che descritti */
.help-legend { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 10px; margin: 8px 0 0; }
.help-chip {
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 700;
}
.help-chip-rapid { background: rgba(255, 107, 125, 0.18); color: var(--sim-rapid); border: 1px dashed var(--sim-rapid); }
.help-chip-feed { background: rgba(92, 200, 255, 0.16); color: var(--sim-feed); border: 1px solid var(--sim-feed); }

.help-lead-end { margin: 20px 0 0; font-size: 14px; color: var(--muted); border-left-color: var(--border); background: none; }

/* Il pulsante che chiude la guida deve saltare all'occhio ed essere
   grande abbastanza da premerlo col pollice */
.help-close {
  flex: none;
  width: 38px;
  height: 38px;
  padding: 0;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 18px;
  line-height: 1;
}
.help-close:hover { border-color: #f5c451; color: #f5c451; }

/* Telefono: titolo piccolo e le quattro schede tutte in vista su due righe,
   cosi' si vede subito che ce ne sono altre oltre a quella aperta */
@media (max-width: 700px) {
  .help-head { gap: 6px 8px; padding: 8px 10px; align-items: flex-start; }
  /* il titolo "Guida" sparisce: lo spazio serve alle schede (il dialogo
     resta annunciato come "Guida" a chi usa un lettore di schermo) */
  .help-head h2 { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .help-tabs { flex-wrap: wrap; gap: 4px; }
  .help-tabs button { flex: 0 0 auto; padding: 6px 9px; font-size: 12px; }
  .help-cards li { padding: 8px 12px 8px 40px; }
  .help-cards li::before { left: 12px; font-size: 18px; }
}
