:root {
  --bg: #071820;
  --bg2: #0b2230;
  --panel: rgba(12, 37, 49, 0.94);
  --panel-2: #102d3a;
  --panel-3: #163746;
  --border: #26566b;
  --border-soft: rgba(88, 159, 183, 0.28);
  --text: #edf8fb;
  --muted: #9fc0cb;
  --muted-2: #6f9aab;
  --accent: #5ce1e6;
  --accent2: #ffbd59;
  --accent3: #a78bfa;
  --success: #59dfb6;
  --danger: #ff7a90;
  --shadow: 0 16px 50px rgba(0,0,0,.22);
  --radius: 22px;
}

body.light {
  --bg: #f3f8fa;
  --bg2: #e7f2f5;
  --panel: rgba(255,255,255,.98);
  --panel-2: #eef7f9;
  --panel-3: #e2f1f4;
  --border: #9fc7d2;
  --border-soft: rgba(56, 118, 137, 0.18);
  --text: #16323d;
  --muted: #527580;
  --muted-2: #7897a0;
  --accent: #008f9b;
  --accent2: #b86700;
  --accent3: #694bc5;
  --success: #147a5d;
  --danger: #b83b52;
  --shadow: 0 12px 40px rgba(31, 70, 82, .12);
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  min-height: 100vh;
  color: var(--text);
  background:
    radial-gradient(circle at 12% 5%, rgba(92,225,230,.08), transparent 25%),
    radial-gradient(circle at 90% 4%, rgba(167,139,250,.08), transparent 22%),
    linear-gradient(180deg, var(--bg), var(--bg2));
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button, textarea, input { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .45; }

.app-shell { width: min(1440px, calc(100% - 34px)); margin: 0 auto; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 20px;
  padding: 24px 0 20px; border-bottom: 1px solid var(--border-soft); margin-bottom: 26px;
}
.brand { display: flex; align-items: center; gap: 16px; }
.brand-mark {
  width: 58px; height: 58px; border-radius: 18px; display: grid; place-items: center;
  font-weight: 900; letter-spacing: -.05em; color: #04252e;
  background: linear-gradient(135deg, var(--accent), #a5f3fc 60%, var(--accent2));
  box-shadow: 0 8px 30px rgba(92,225,230,.18);
}
.brand h1 { font-size: 29px; margin: 0 0 4px; letter-spacing: -.025em; }
.brand p { margin: 0; color: var(--muted); }
.top-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; justify-content: flex-end; }
.local-badge { color: var(--success); background: rgba(89,223,182,.08); border: 1px solid rgba(89,223,182,.2); padding: 9px 12px; border-radius: 999px; font-size: 13px; font-weight: 700; }

.panel {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); backdrop-filter: blur(12px);
}
.prompt-panel { padding: 28px; }
.section-kicker { color: var(--muted); font-weight: 900; font-size: 13px; letter-spacing: .12em; text-transform: uppercase; }
.chips { display: flex; flex-wrap: wrap; gap: 10px; margin: 18px 0 24px; }
.chip {
  border: 1px solid var(--border); background: var(--panel-2); color: var(--muted); border-radius: 999px;
  padding: 10px 15px; font-weight: 750; transition: .18s ease;
}
.chip:hover, .chip.active { color: var(--text); border-color: var(--accent); transform: translateY(-1px); box-shadow: 0 0 0 3px rgba(92,225,230,.08); }

.prompt-grid { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 24px; }
.prompt-main textarea {
  width: 100%; resize: vertical; min-height: 250px; border-radius: 17px; border: 1px solid var(--border);
  background: var(--panel-2); color: var(--text); padding: 19px 20px; outline: none; line-height: 1.55;
  transition: border-color .18s ease, box-shadow .18s ease;
}
.prompt-main textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 4px rgba(92,225,230,.09); }
.prompt-main textarea::placeholder { color: var(--muted-2); }
.prompt-actions { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 18px; align-items: center; }
.primary-btn, .secondary-btn, .accent-btn, .ghost-btn, .text-btn {
  border-radius: 12px; padding: 11px 16px; font-weight: 850; border: 1px solid transparent;
}
.primary-btn { color: #04242c; background: linear-gradient(135deg, var(--accent), #8df0e9); }
.secondary-btn { color: var(--text); background: transparent; border-color: var(--border); }
.accent-btn { color: #2d2100; background: linear-gradient(135deg, #ffd27d, var(--accent2)); }
.ghost-btn { color: var(--muted); background: var(--panel-2); border-color: var(--border); }
.text-btn { color: var(--muted); background: transparent; padding-inline: 6px; }
.text-btn:hover { color: var(--text); }

.controls { padding: 6px 0 0; }
.control-group + .control-group { margin-top: 22px; }
.control-title { color: var(--muted); font-size: 13px; font-weight: 900; letter-spacing: .06em; margin-bottom: 10px; }
.segmented { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.segmented.three { grid-template-columns: repeat(2, 1fr); }
.segmented.three button:last-child { grid-column: 1 / -1; }
.segmented button {
  border: 1px solid var(--border); color: var(--muted); background: var(--panel-2); border-radius: 11px; padding: 10px 8px; font-weight: 800;
}
.segmented button.active { color: var(--text); border-color: var(--accent3); background: linear-gradient(135deg, rgba(167,139,250,.19), rgba(92,225,230,.09)); box-shadow: inset 0 0 0 1px rgba(167,139,250,.15); }
#temperature { width: 100%; accent-color: var(--accent2); }
.temperature-legend { display: flex; justify-content: space-between; color: var(--muted-2); font-size: 11px; margin-top: 4px; }
.microcopy { margin: 8px 0 0; color: var(--muted); font-size: 12px; line-height: 1.4; }

.output-panel { padding: 24px 28px 28px; margin-top: 24px; }
.panel-heading { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.panel-heading h2 { margin: 0; font-size: 20px; }
.status-line { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: 13px; }
.counter { border: 1px solid var(--border); padding: 7px 10px; border-radius: 10px; background: var(--panel-2); }
.status { font-weight: 700; }
.status.idle { color: var(--muted); }
.status.running { color: var(--success); }
.status.paused { color: var(--accent2); }
.response-output {
  min-height: 124px; margin-top: 20px; padding: 21px; border-radius: 16px; background: var(--panel-2);
  color: var(--text); font-size: 19px; line-height: 1.65; white-space: pre-wrap;
}
.response-output em { color: var(--muted-2); }
.cursor { display: inline-block; width: 2px; height: 1.05em; background: var(--accent); margin-left: 3px; vertical-align: -2px; animation: blink .85s infinite; }
@keyframes blink { 0%,49%{opacity:1} 50%,100%{opacity:0} }

.pipeline-tabs {
  display: grid; grid-template-columns: 1fr auto 1fr auto 1fr auto 1fr; align-items: center; gap: 8px;
  padding: 10px; border: 1px solid var(--border); border-radius: 18px; margin: 24px 0 16px; background: rgba(7,24,32,.35);
}
.pipeline-tabs button {
  border: 1px solid transparent; color: var(--muted); background: transparent; border-radius: 12px; padding: 13px 12px;
  display: flex; align-items: center; gap: 10px; justify-content: center; font-weight: 850;
}
.pipeline-tabs button small { color: var(--muted-2); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.pipeline-tabs button.active { border-color: var(--accent3); color: var(--text); background: linear-gradient(135deg, rgba(167,139,250,.16), rgba(92,225,230,.07)); }
.pipeline-tabs button:disabled { opacity: .32; }
.chev { color: var(--muted-2); font-size: 22px; }

.stage-panel { min-height: 365px; padding: 32px; display: flex; flex-direction: column; justify-content: center; }
.stage-empty { text-align: center; color: var(--muted); max-width: 620px; margin: auto; }
.stage-empty .robot { font-size: 46px; margin-bottom: 12px; }
.stage-head { display: flex; gap: 14px; align-items: flex-start; margin-bottom: 24px; }
.stage-icon { font-size: 28px; }
.stage-head h3 { margin: 0 0 5px; font-size: 23px; }
.stage-head p { margin: 0; color: var(--muted); line-height: 1.55; }
.stage-note { border-radius: 12px; background: var(--panel-2); padding: 12px 14px; color: var(--muted); margin-top: 18px; }
.stage-note strong { color: var(--accent2); }

.token-wrap { display: flex; flex-wrap: wrap; gap: 9px; }
.token-pill { padding: 9px 11px; border-radius: 9px; border: 1px solid var(--border); background: var(--panel-2); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--text); }
.token-pill small { color: var(--muted-2); margin-left: 6px; }
.token-pill.current { border-color: var(--accent2); box-shadow: 0 0 0 3px rgba(255,189,89,.09); }

.attention-layout { display: grid; grid-template-columns: minmax(360px, .9fr) minmax(320px, 1.1fr); gap: 28px; align-items: start; }
.attention-matrix { overflow-x: auto; padding-bottom: 6px; }
.matrix { display: grid; gap: 5px; width: max-content; }
.matrix-cell, .matrix-label { width: 68px; height: 48px; border-radius: 8px; display: grid; place-items: center; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
.matrix-label { color: var(--muted); background: transparent; }
.matrix-cell { border: 1px solid rgba(92,225,230,.12); color: #031d24; font-weight: 900; }
.matrix-cell.future { background: var(--panel-2); color: var(--muted-2); border-color: var(--border-soft); }
.attention-bars { display: grid; gap: 12px; }
.att-row { display: grid; grid-template-columns: 120px 1fr 48px; gap: 10px; align-items: center; }
.att-label { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.bar-track { height: 20px; background: var(--panel-2); border-radius: 8px; overflow: hidden; border: 1px solid var(--border-soft); }
.bar-fill { height: 100%; background: linear-gradient(90deg, rgba(92,225,230,.68), var(--accent)); border-radius: inherit; }
.att-pct { color: var(--muted); text-align: right; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }

.prob-list { display: grid; gap: 12px; }
.prob-row { display: grid; grid-template-columns: 170px 1fr 70px; gap: 12px; align-items: center; }
.prob-token { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-weight: 900; text-align: right; color: var(--muted); }
.prob-row.winner .prob-token { color: var(--accent2); }
.prob-track { height: 34px; background: var(--panel-2); border-radius: 10px; overflow: hidden; border: 1px solid var(--border-soft); }
.prob-fill { height: 100%; background: linear-gradient(90deg, #d99931, var(--accent2)); border-radius: inherit; }
.prob-row:not(.winner) .prob-fill { background: linear-gradient(90deg, rgba(167,139,250,.5), var(--accent3)); }
.prob-pct { color: var(--muted); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }

.generated-token-card { text-align: center; }
.generated-token-card .big-token { display: inline-block; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-weight: 900; font-size: clamp(34px, 5vw, 58px); color: var(--accent2); border: 2px solid rgba(255,189,89,.65); background: rgba(255,189,89,.08); border-radius: 16px; padding: 18px 26px; margin: 18px 0; }
.generated-token-card .arrow { font-size: 35px; color: var(--accent2); }

.explain-grid { display: grid; grid-template-columns: 1fr 1.15fr; gap: 24px; margin-top: 24px; }
.fact-card, .glossary-card { padding: 26px 28px; }
.fact-card p { color: var(--muted); font-size: 17px; line-height: 1.7; min-height: 92px; }
.small-note { color: var(--muted-2); font-size: 12px; }
.glossary-item { border-top: 1px solid var(--border-soft); }
.glossary-item:first-child { border-top: 0; }
.glossary-q { width: 100%; display: flex; align-items: center; justify-content: space-between; gap: 12px; border: 0; background: transparent; color: var(--muted); font-weight: 850; padding: 14px 0; text-align: left; }
.glossary-q:hover { color: var(--text); }
.glossary-a { display: none; color: var(--muted); line-height: 1.6; padding: 0 0 16px; }
.glossary-item.open .glossary-a { display: block; }
.glossary-item.open .glossary-q { color: var(--text); }
.accuracy-note { margin-top: 24px; padding: 18px 22px; color: var(--muted); line-height: 1.6; border-style: dashed; }
.accuracy-note strong { color: var(--text); }
footer { text-align: center; color: var(--muted-2); padding: 32px 0 42px; font-size: 13px; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }

@media (max-width: 980px) {
  .prompt-grid, .attention-layout, .explain-grid { grid-template-columns: 1fr; }
  .controls { padding-top: 4px; }
  .prompt-main textarea { min-height: 190px; }
  .pipeline-tabs { grid-template-columns: 1fr 1fr; }
  .pipeline-tabs .chev { display: none; }
  .pipeline-tabs button { justify-content: flex-start; }
  .attention-layout { gap: 20px; }
}
@media (max-width: 650px) {
  .app-shell { width: min(100% - 20px, 1440px); }
  .topbar { align-items: flex-start; }
  .brand-mark { width: 50px; height: 50px; border-radius: 15px; }
  .brand h1 { font-size: 24px; }
  .brand p { font-size: 13px; }
  .top-actions { display: none; }
  .prompt-panel, .output-panel, .stage-panel, .fact-card, .glossary-card { padding: 20px; }
  .panel-heading { align-items: flex-start; }
  .status-line { flex-direction: column; align-items: flex-end; gap: 6px; }
  .prob-row { grid-template-columns: 100px 1fr 54px; }
  .att-row { grid-template-columns: 90px 1fr 42px; }
}

/* --- NextToken: modalità didattica --- */
.didactic-toggle {
  border: 1px solid rgba(255,189,89,.35); color: var(--accent2); background: rgba(255,189,89,.08);
  border-radius: 12px; padding: 10px 14px; font-weight: 900;
}
.didactic-toggle.active { background: linear-gradient(135deg, rgba(255,189,89,.20), rgba(167,139,250,.16)); border-color: var(--accent2); color: var(--text); }
.didactic-panel { margin-top: 16px; padding: 28px; border-color: rgba(255,189,89,.55); }
.didactic-panel[hidden] { display: none; }
.didactic-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 20px; border-bottom: 1px solid var(--border-soft); padding-bottom: 20px; }
.didactic-header h2 { margin: 7px 0 6px; font-size: 24px; }
.didactic-header p { margin: 0; color: var(--muted); line-height: 1.55; max-width: 860px; }
.didactic-lesson { margin-top: 22px; }
.didactic-empty, .workspace-intro { color: var(--muted); background: var(--panel-2); border: 1px dashed var(--border); border-radius: 14px; padding: 18px; line-height: 1.6; }
.didactic-empty strong, .workspace-intro strong { color: var(--text); }
.lesson-head { display: flex; align-items: center; gap: 12px; }
.lesson-head > span { display: inline-grid; place-items: center; min-width: 44px; height: 34px; border-radius: 999px; background: rgba(255,189,89,.12); border: 1px solid rgba(255,189,89,.35); color: var(--accent2); font-weight: 900; }
.lesson-head h3 { margin: 0; font-size: 21px; }
.lesson-simple { color: var(--muted); line-height: 1.65; font-size: 16px; }
.lesson-grid { display: grid; grid-template-columns: repeat(3,1fr); gap: 14px; margin-top: 18px; }
.lesson-grid > div { background: var(--panel-2); border: 1px solid var(--border-soft); border-radius: 14px; padding: 16px; }
.lesson-grid strong { color: var(--text); }
.lesson-grid p { color: var(--muted); line-height: 1.55; margin: 8px 0 0; }
.qkv-grid { display: grid; grid-template-columns: repeat(3,1fr); gap: 12px; margin-top: 18px; }
.qkv-card { position: relative; padding: 16px 16px 16px 58px; border: 1px solid rgba(92,225,230,.26); background: rgba(92,225,230,.05); border-radius: 14px; }
.qkv-card > span { position: absolute; left: 14px; top: 14px; width: 32px; height: 32px; display: grid; place-items: center; border-radius: 9px; background: var(--accent); color: #03252d; font-weight: 950; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.qkv-card strong { display: block; }
.qkv-card p { margin: 6px 0 0; color: var(--muted); line-height: 1.5; }
.multihead-box { margin-top: 12px; border-left: 4px solid var(--accent3); background: rgba(167,139,250,.08); padding: 14px 16px; border-radius: 10px; }
.multihead-box p { color: var(--muted); line-height: 1.55; margin: 6px 0 0; }
.didactic-tools { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 24px; padding-top: 22px; border-top: 1px solid var(--border-soft); }
.didactic-tools button { border: 1px solid var(--border); background: var(--panel-2); color: var(--muted); padding: 11px 14px; border-radius: 12px; font-weight: 850; }
.didactic-tools button.active, .didactic-tools button:hover { color: var(--text); border-color: var(--accent2); }
.didactic-workspace { margin-top: 16px; }
.workspace-title h3 { margin: 0 0 6px; }
.workspace-title p { color: var(--muted); line-height: 1.55; margin: 0 0 16px; }
.temp-compare-grid { display: grid; grid-template-columns: repeat(3,1fr); gap: 14px; }
.temp-compare-card { border: 1px solid var(--border); background: var(--panel-2); border-radius: 14px; padding: 15px; }
.temp-compare-card h4 { margin: 0; color: var(--accent2); font-size: 18px; }
.temp-compare-card > p { color: var(--muted); margin: 4px 0 12px; }
.temp-mini-row { display: grid; grid-template-columns: 88px 1fr 52px; gap: 8px; align-items: center; margin: 8px 0; font-size: 12px; }
.temp-mini-row > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--muted); }
.temp-mini-row > div { height: 13px; background: var(--panel-3); border-radius: 99px; overflow: hidden; }
.temp-mini-row i { display: block; height: 100%; background: linear-gradient(90deg, var(--accent2), var(--accent3)); border-radius: inherit; }
.temp-mini-row b { color: var(--muted); text-align: right; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.didactic-callout { margin-top: 14px; padding: 13px 15px; border-radius: 12px; background: rgba(255,189,89,.08); color: var(--muted); }
.didactic-callout strong { color: var(--text); }
.prediction-context { padding: 14px 16px; border-radius: 12px; background: var(--panel-2); color: var(--text); line-height: 1.5; }
.prediction-context span { color: var(--muted); font-weight: 800; }
.prediction-choices { display: flex; gap: 10px; flex-wrap: wrap; margin: 14px 0; }
.prediction-choice { border: 1px solid var(--border); background: var(--panel-2); color: var(--text); border-radius: 999px; padding: 10px 15px; font-weight: 850; }
.prediction-choice.correct { border-color: var(--success); box-shadow: 0 0 0 3px rgba(89,223,182,.10); }
.prediction-choice.wrong { border-color: var(--danger); }
.prediction-feedback { color: var(--muted); }
.hallucination-case { border: 1px solid rgba(255,122,144,.35); background: rgba(255,122,144,.05); border-radius: 14px; padding: 18px; }
.case-prompt { color: var(--muted); }
.case-answer { margin: 14px 0; padding: 15px; background: var(--panel-2); border-radius: 12px; font-style: italic; }
#hallucinationReveal { margin-top: 14px; color: var(--muted); line-height: 1.6; }
#hallucinationReveal strong { color: var(--text); }

@media (max-width: 980px) {
  .lesson-grid, .qkv-grid, .temp-compare-grid { grid-template-columns: 1fr; }
  .didactic-header { flex-direction: column; }
}

@media (max-width: 650px) {
  .topbar { flex-direction: column; }
  .top-actions { display: flex; width: 100%; justify-content: flex-start; }
  .didactic-toggle, .ghost-btn { font-size: 13px; }
}

/* NeuraLab v2 — guida iniziale */
.intro-guide{margin-bottom:18px;padding:20px}.intro-guide h2{margin:6px 0 8px}.intro-guide>p{color:var(--muted);line-height:1.55}.intro-steps{display:grid;grid-template-columns:repeat(auto-fit,minmax(170px,1fr));gap:10px;margin-top:14px}.intro-step{border:1px solid var(--border);border-radius:14px;padding:13px;background:var(--panel-2)}.intro-step b{display:block;margin-bottom:5px}.intro-step span{display:block;color:var(--muted);font-size:.84rem;line-height:1.4}.intro-num{width:26px;height:26px;border-radius:50%;display:grid;place-items:center;background:rgba(50,210,190,.13);color:var(--accent);font-weight:800;margin-bottom:8px}

/* v5 — disclosure footer: improved readability for the full transparency statement */
footer p {
  line-height: 1.55;
  text-wrap: pretty;
}
