/* =========================================================
   Flusso — Studio per diagrammi Sankey
   ========================================================= */

:root {
  --mist: #e8ebf2;
  --panel: #ffffff;
  --ink: #1b1f3a;
  --ink-2: #565c7c;
  --ink-3: #8a8fa8;
  --line: #e1e4ec;
  --line-2: #cdd2df;
  --field: #f3f5f9;
  --cobalt: #3550ff;
  --cobalt-2: #2338d8;
  --cobalt-soft: #ecefff;
  --rose: #ff6f9c;
  --amber: #ffb547;
  --danger: #d93a45;

  --ui: "DM Sans", system-ui, -apple-system, sans-serif;
  --display: "Bricolage Grotesque", "DM Sans", sans-serif;
  --code: "JetBrains Mono", ui-monospace, Menlo, monospace;

  --r-s: 7px;
  --r-m: 10px;
  --r-l: 18px;
  --side: 420px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }

body {
  font: 14px/1.45 var(--ui);
  color: var(--ink);
  background: var(--mist);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { -webkit-tap-highlight-color: transparent; }
:focus-visible { outline: 2px solid var(--cobalt); outline-offset: 2px; }

.app {
  display: grid;
  grid-template-rows: 58px minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
}

/* ---------- Barra superiore ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 0 16px 0 18px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: relative;
  z-index: 20;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--ink);
  text-decoration: none;
  flex: none;
}
.brand-mark { width: 38px; height: 27px; }
.brand-name {
  font: 800 22px/1 var(--display);
  letter-spacing: -0.03em;
}

.title-input {
  flex: 1;
  max-width: 340px;
  min-width: 0;
  border: 1px solid transparent;
  background: transparent;
  border-radius: var(--r-s);
  padding: 7px 10px;
  font-weight: 500;
  color: var(--ink-2);
}
.title-input:hover { border-color: var(--line); }
.title-input:focus { border-color: var(--cobalt); outline: none; background: #fff; color: var(--ink); }

.top-actions { margin-left: auto; display: flex; align-items: center; gap: 6px; }

/* ---------- Pulsanti ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 34px;
  padding: 0 13px;
  border-radius: var(--r-s);
  border: 1px solid var(--line);
  background: #fff;
  cursor: pointer;
  font-weight: 500;
  white-space: nowrap;
  transition: background .15s, border-color .15s, color .15s;
}
.btn:hover { background: #f6f7fb; border-color: var(--line-2); }
.btn:active { transform: translateY(1px); }
.btn.icon { width: 34px; padding: 0; font-size: 18px; }
.btn.primary { background: var(--cobalt); border-color: var(--cobalt); color: #fff; }
.btn.primary:hover { background: var(--cobalt-2); border-color: var(--cobalt-2); }
.btn:disabled { opacity: .38; cursor: default; transform: none; }
.btn.wide { width: 100%; }

.link-btn {
  border: 0;
  background: none;
  color: var(--cobalt);
  padding: 0;
  margin-top: 16px;
  cursor: pointer;
  font-weight: 500;
  text-align: left;
}
.link-btn:hover { text-decoration: underline; }

/* ---------- Menu ---------- */
.menu-wrap { position: relative; }
.menu {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  min-width: 250px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--r-m);
  box-shadow: 0 16px 40px -12px rgba(27, 31, 58, .28);
  padding: 6px;
  display: none;
  z-index: 60;
}
.menu.open { display: block; }
.menu button {
  display: block;
  width: 100%;
  text-align: left;
  padding: 9px 11px;
  border: 0;
  background: none;
  border-radius: 7px;
  cursor: pointer;
  font-weight: 500;
}
.menu button:hover, .menu button:focus-visible { background: var(--cobalt-soft); outline: none; }
.menu small { display: block; color: var(--ink-3); font-weight: 400; font-size: 12.5px; }

/* ---------- Area di lavoro ---------- */
.workspace {
  display: grid;
  grid-template-columns: var(--side) minmax(0, 1fr);
  min-height: 0;
}

.sidebar {
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--line);
  min-height: 0;
}

.tabs {
  display: flex;
  padding: 4px 12px 0;
  border-bottom: 1px solid var(--line);
  flex: none;
}
.tab {
  flex: 1;
  border: 0;
  background: none;
  padding: 11px 2px 12px;
  cursor: pointer;
  color: var(--ink-3);
  font-weight: 500;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  transition: color .15s;
}
.tab:hover { color: var(--ink-2); }
.tab.on { color: var(--ink); border-bottom-color: var(--ink); }

.panels { overflow-y: auto; flex: 1; min-height: 0; scrollbar-width: thin; }
.panel { display: none; padding: 4px 20px 32px; }
.panel.on { display: block; }

.panel h2 {
  font: 600 15.5px/1.2 var(--display);
  letter-spacing: -0.01em;
  margin: 24px 0 10px;
}
.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 24px 0 10px;
}
.panel-head h2 { margin: 0; }
.count { color: var(--ink-3); font-size: 13px; }

.field {
  display: grid;
  grid-template-columns: 124px minmax(0, 1fr);
  align-items: center;
  gap: 12px;
  margin: 10px 0;
}
.field > label { color: var(--ink-2); }
.inline { display: flex; align-items: center; gap: 10px; }
.inline select { width: auto; }

.range { display: flex; align-items: center; gap: 10px; }
.range output {
  min-width: 50px;
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: var(--ink-2);
  font-size: 13px;
}
input[type=range] { flex: 1; min-width: 0; accent-color: var(--ink); }

input[type=text], input[type=number], select, textarea {
  border: 1px solid var(--line);
  border-radius: var(--r-s);
  background: #fff;
  padding: 7px 9px;
  width: 100%;
  min-width: 0;
  transition: border-color .15s, box-shadow .15s;
}
input[type=text]:focus, input[type=number]:focus, select:focus, textarea:focus {
  border-color: var(--cobalt);
  box-shadow: 0 0 0 3px var(--cobalt-soft);
  outline: none;
}

input[type=color] {
  width: 44px;
  height: 32px;
  border: 1px solid var(--line);
  border-radius: var(--r-s);
  padding: 3px;
  background: #fff;
  cursor: pointer;
}
input[type=color]::-webkit-color-swatch-wrapper { padding: 0; }
input[type=color]::-webkit-color-swatch { border: 0; border-radius: 4px; }
input[type=color]::-moz-color-swatch { border: 0; border-radius: 4px; }

/* Controllo segmentato */
.seg {
  display: flex;
  background: var(--field);
  border-radius: var(--r-s);
  padding: 3px;
  gap: 2px;
}
.seg button {
  flex: 1;
  border: 0;
  background: none;
  padding: 6px 6px;
  border-radius: 5px;
  cursor: pointer;
  color: var(--ink-2);
  font-weight: 500;
  font-size: 13px;
  white-space: nowrap;
  transition: background .15s, color .15s;
}
.seg button:hover { color: var(--ink); }
.seg button.on { background: #fff; color: var(--ink); box-shadow: 0 1px 2px rgba(27, 31, 58, .16), 0 0 0 1px rgba(27, 31, 58, .04); }
.seg.small button { padding: 4px 10px; font-size: 12.5px; }
.seg.big button { padding: 9px; font-size: 14px; }
.seg.stack { flex-direction: column; }
.seg.stack button { text-align: left; padding: 8px 10px; }

/* Interruttore */
.toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 9px 0;
  cursor: pointer;
  color: var(--ink-2);
}
.toggle input {
  appearance: none;
  -webkit-appearance: none;
  flex: none;
  width: 36px;
  height: 21px;
  margin: 0;
  border-radius: 999px;
  background: #d3d7e2;
  position: relative;
  cursor: pointer;
  transition: background .18s;
}
.toggle input::after {
  content: "";
  position: absolute;
  top: 2.5px;
  left: 2.5px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, .25);
  transition: transform .18s;
}
.toggle input:checked { background: var(--cobalt); }
.toggle input:checked::after { transform: translateX(15px); }

.hint { color: var(--ink-3); font-size: 12.5px; line-height: 1.5; margin: 10px 0; }
.hint.tight { margin-top: -4px; }
.hint b { color: var(--ink-2); font-weight: 600; }
.muted { color: var(--ink-3); font-size: 13px; }

/* ---------- Tabella flussi ---------- */
.flow-head, .flow-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 78px minmax(0, 1fr) 26px 22px;
  gap: 5px;
  align-items: center;
}
.flow-head { font-size: 12px; color: var(--ink-3); padding: 0 2px 5px; }
.flow-head .r { text-align: right; padding-right: 6px; }
.flow-row { margin-bottom: 5px; }
.flow-row input[type=text] { padding: 6px 7px; font-size: 13px; }
.flow-row .f-val { text-align: right; font-variant-numeric: tabular-nums; }
.flow-row.bad input[type=text] { border-color: #f1b3b7; background: #fff7f7; }

.f-color { position: relative; width: 26px; height: 26px; cursor: pointer; display: block; }
.f-color input { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; cursor: pointer; padding: 0; border: 0; }
.f-color span {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 6px;
  background: var(--c);
  opacity: .5;
  border: 1.5px dashed rgba(27, 31, 58, .28);
  pointer-events: none;
}
.f-color.set span { opacity: 1; border: 1px solid rgba(27, 31, 58, .14); }

.f-del {
  border: 0;
  background: none;
  color: var(--ink-3);
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
  border-radius: 5px;
  height: 26px;
  padding: 0;
}
.f-del:hover { color: var(--danger); background: #fdeef0; }

.btn.add { width: 100%; margin-top: 4px; border-style: dashed; color: var(--ink-2); }

#codeInput {
  min-height: 320px;
  font: 13px/1.65 var(--code);
  resize: vertical;
  tab-size: 4;
  white-space: pre;
  background: #fbfbfd;
}

.syntax { margin-top: 8px; border: 1px solid var(--line); border-radius: var(--r-s); }
.syntax summary { cursor: pointer; padding: 8px 10px; font-weight: 500; color: var(--ink-2); }
.syntax-body { padding: 0 12px 6px; font-size: 13px; color: var(--ink-2); }
.syntax-body p { margin: 6px 0 10px; }
.syntax code, .empty code {
  font: 12.5px var(--code);
  background: var(--field);
  padding: 1px 5px;
  border-radius: 4px;
  color: var(--ink);
}

.issues:empty { display: none; }
.issues { margin-top: 12px; display: grid; gap: 4px; }
.issue {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: #fff4f4;
  color: #8e2a31;
  border-radius: 6px;
  padding: 7px 10px;
  font-size: 12.5px;
  cursor: pointer;
}
.issue b { font-weight: 600; margin-right: 4px; }
.issue:hover { background: #ffe9ea; }

/* ---------- Lista nodi ---------- */
.node-row {
  display: grid;
  grid-template-columns: 28px minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  padding: 5px 0;
  border-top: 1px solid var(--line);
}
.node-row:last-child { border-bottom: 1px solid var(--line); }
.n-color { position: relative; width: 24px; height: 24px; margin-left: 2px; display: block; cursor: pointer; }
.n-color input { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; cursor: pointer; padding: 0; border: 0; }
.n-color span { display: block; width: 100%; height: 100%; border-radius: 50%; background: var(--c); box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .1); pointer-events: none; }
.n-name { border-color: transparent !important; background: transparent !important; padding: 5px 6px !important; font-weight: 500; }
.n-name:hover { border-color: var(--line) !important; }
.n-name:focus { border-color: var(--cobalt) !important; background: #fff !important; }
.n-meta { font-size: 12px; color: var(--ink-3); white-space: nowrap; text-align: right; line-height: 1.3; }
.n-meta .warn { color: var(--danger); display: block; }
.n-meta .note { color: var(--ink-3); display: block; }
.n-reset { border: 0; background: none; color: var(--cobalt); font-size: 12px; cursor: pointer; padding: 0; }

/* ---------- Temi e palette ---------- */
.themes { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.theme {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: var(--r-m);
  padding: 5px 5px 7px;
  cursor: pointer;
  text-align: left;
  font-size: 12.5px;
  font-weight: 500;
  color: var(--ink-2);
  transition: border-color .15s, transform .15s;
}
.theme:hover { border-color: var(--ink); color: var(--ink); }
.theme svg { display: block; width: 100%; height: auto; border-radius: 6px; margin-bottom: 6px; }
.theme span { padding-left: 3px; }

.palettes { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.pal {
  display: flex;
  flex-direction: column;
  gap: 6px;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: var(--r-m);
  padding: 7px;
  cursor: pointer;
  text-align: left;
  font-size: 12.5px;
  color: var(--ink-2);
  font-weight: 500;
}
.pal:hover { border-color: var(--line-2); }
.pal.on { border-color: var(--ink); box-shadow: inset 0 0 0 1px var(--ink); color: var(--ink); }
.pal-sw { display: flex; height: 14px; border-radius: 4px; overflow: hidden; }
.pal-sw i { flex: 1; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.chip {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 999px;
  padding: 5px 11px;
  font-size: 12.5px;
  cursor: pointer;
  color: var(--ink-2);
  font-weight: 500;
}
.chip:hover { border-color: var(--line-2); color: var(--ink); }
.chip.on { background: var(--ink); color: #fff; border-color: var(--ink); }

.video-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 22px; }
.video-actions .btn { height: 40px; }

/* ---------- Anteprima ---------- */
.stage {
  position: relative;
  overflow: hidden;
  background-color: var(--mist);
  background-image: radial-gradient(#c6cbd9 1px, transparent 1.2px);
  background-size: 22px 22px;
  min-height: 0;
}
.canvas-wrap { position: absolute; inset: 0; touch-action: none; cursor: grab; }
.canvas-wrap.panning { cursor: grabbing; }

.artboard {
  position: absolute;
  left: 0;
  top: 0;
  transform-origin: 0 0;
  background: #fff;
  box-shadow: 0 1px 2px rgba(27, 31, 58, .06), 0 24px 60px -24px rgba(27, 31, 58, .38);
}
.artboard.transparent {
  background: repeating-conic-gradient(#e3e6ee 0 25%, #fff 0 50%) 0 0 / 24px 24px;
}
.artboard svg { display: block; overflow: visible; }
.artboard .nd { cursor: move; }
.artboard .lk, .artboard .nd, .artboard .lb { transition: opacity .15s; }
.artboard svg.dim .lk { opacity: .16; }
.artboard svg.dim .nd { opacity: .3; }
.artboard svg.dim .lk.hl, .artboard svg.dim .nd.hl { opacity: 1; }

.empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  text-align: center;
  gap: 8px;
  padding: 24px;
  pointer-events: none;
}
.empty > * { pointer-events: auto; }
.empty h3 { font: 800 30px/1.1 var(--display); letter-spacing: -0.03em; margin: 0; }
.empty p { margin: 0 0 8px; color: var(--ink-2); max-width: 38ch; }

.zoombar {
  position: absolute;
  left: 50%;
  bottom: 46px;
  transform: translateX(-50%);
  display: flex;
  gap: 2px;
  background: #fff;
  padding: 4px;
  border-radius: 12px;
  box-shadow: 0 6px 20px -8px rgba(27, 31, 58, .3), 0 0 0 1px rgba(27, 31, 58, .06);
  z-index: 5;
}
.zoombar .btn { border-color: transparent; height: 30px; }
.zoombar .btn.icon { width: 30px; }
#zoomLabel { min-width: 58px; font-variant-numeric: tabular-nums; }

.statusbar {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 32px;
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 0 16px;
  font-size: 12.5px;
  color: var(--ink-2);
  background: rgba(255, 255, 255, .88);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-top: 1px solid var(--line);
  z-index: 5;
  white-space: nowrap;
  overflow: hidden;
}
.statusbar .warn { color: var(--danger); font-weight: 500; }
.statusbar .push { margin-left: auto; color: var(--ink-3); overflow: hidden; text-overflow: ellipsis; }

.tooltip {
  position: fixed;
  left: 0;
  top: 0;
  pointer-events: none;
  background: var(--ink);
  color: #fff;
  padding: 8px 11px;
  border-radius: 9px;
  font-size: 12.5px;
  line-height: 1.5;
  opacity: 0;
  transition: opacity .12s;
  z-index: 80;
  max-width: 300px;
}
.tooltip.show { opacity: 1; }
.tooltip b { display: block; font-weight: 600; font-size: 13px; margin-bottom: 2px; }
.tooltip span { display: block; color: #c9cdf0; }

/* ---------- Modali ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(18, 21, 42, .5);
  display: grid;
  place-items: center;
  z-index: 100;
  padding: 20px;
  animation: fade .18s ease;
}
.modal-card {
  background: #fff;
  border-radius: var(--r-l);
  width: min(470px, 100%);
  padding: 20px 22px 22px;
  box-shadow: 0 30px 80px -20px rgba(0, 0, 0, .45);
  max-height: calc(100vh - 40px);
  overflow: auto;
  animation: pop .22s cubic-bezier(.2, .9, .3, 1.2);
}
.modal-card header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.modal-card h2 { font: 700 21px/1.2 var(--display); letter-spacing: -0.02em; margin: 0; }
.modal-card header .btn { border-color: transparent; font-size: 22px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.modal-actions .btn { height: 40px; padding: 0 18px; }

.video-card { width: min(780px, 100%); }
.video-frame {
  background: #12142a;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
  height: min(60vh, 620px);
}
.video-frame canvas {
  max-width: 100%;
  max-height: calc(min(60vh, 620px) - 28px);
  width: auto;
  height: auto;
  border-radius: 4px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .45);
}
.video-controls { display: flex; align-items: center; gap: 12px; margin-top: 12px; }
.video-controls input[type=range] { accent-color: var(--cobalt); }
#vTime { font-variant-numeric: tabular-nums; color: var(--ink-2); font-size: 13px; min-width: 92px; text-align: right; }
.rec-progress { display: flex; align-items: center; gap: 12px; margin-top: 12px; }
.rec-progress .bar { flex: 1; height: 6px; background: var(--field); border-radius: 99px; overflow: hidden; }
.rec-progress .bar span { display: block; height: 100%; width: 0; background: var(--danger); border-radius: 99px; }
#recLabel { font-size: 13px; color: var(--danger); font-weight: 500; }

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

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translate(-50%, 16px);
  background: var(--ink);
  color: #fff;
  padding: 11px 16px;
  border-radius: 10px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 200;
  font-weight: 500;
  max-width: calc(100vw - 32px);
  box-shadow: 0 12px 30px -10px rgba(0, 0, 0, .4);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
  :root { --side: 350px; }
  .field { grid-template-columns: 108px minmax(0, 1fr); }
}

@media (max-width: 860px) {
  body { overflow: auto; }
  .app { height: auto; min-height: 100dvh; grid-template-rows: auto auto; }
  .topbar { flex-wrap: wrap; padding: 10px 12px; gap: 8px; }
  .title-input { order: 3; flex-basis: 100%; max-width: none; }
  .top-actions { gap: 4px; }
  .top-actions .btn:not(.icon) { padding: 0 10px; }
  .workspace { grid-template-columns: 1fr; grid-template-rows: 56vh auto; }
  .stage { order: -1; border-bottom: 1px solid var(--line); }
  .sidebar { border-right: 0; }
  .panels { overflow: visible; }
  .statusbar .push { display: none; }
  .zoombar { bottom: 42px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}
