/* ===================== TEMA ===================== */
:root {
  --molbg: #ffffff;
  --formula: #222;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface2: #f1f3f6;
  --line: #e3e6eb;
  --line2: #cfd5de;
  --txt: #1d2330;
  --muted: #6b7486;
  --accent: #2563eb;
  --in: #059669;     /* "si ottiene da" */
  --out: #ea580c;    /* "può dare" */
  --shadow: 0 1px 2px #0000000d, 0 4px 16px #0000000d;
  --panel-w: 430px;
  --radius: 12px;
}
/* Tema scuro: grigi-blu morbidi (niente nero puro, niente bianco puro),
   contrasto testo ~11:1 invece di 19:1, colori desaturati → meno affaticamento */
body.dark {
  --bg: #15181e;
  --surface: #1b1f26;
  --surface2: #22272f;
  --line: #2a3039;
  --line2: #363d48;
  --txt: #d5d9e0;
  --muted: #8b93a1;
  --accent: #7aa2f7;
  --in: #5fbf8f;
  --out: #e39a5f;
  --shadow: 0 1px 2px #0005, 0 4px 14px #0004;
  --molbg: #1d222a;
  --formula: #d5d9e0;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--txt);
  font-family: "Inter", "Segoe UI", system-ui, -apple-system, Roboto, sans-serif; font-size: 14px; line-height: 1.45; }
sub, sup { font-size: .72em; line-height: 0; }
.hidden { display: none !important; }
.muted { color: var(--muted); }

.btn { font: inherit; color: var(--txt); background: var(--surface); border: 1px solid var(--line2);
  border-radius: 9px; padding: 7px 13px; cursor: pointer; white-space: nowrap; transition: background .15s, border-color .15s; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
body.dark .btn.primary { color: #0b1220; }
.btn.icon { width: 36px; height: 36px; padding: 0; font-size: 17px; display: inline-flex; align-items: center; justify-content: center; }

/* ===================== TOPBAR ===================== */
.topbar { height: 60px; display: flex; align-items: center; gap: 16px; padding: 0 16px;
  background: var(--surface); border-bottom: 1px solid var(--line); position: relative; z-index: 20; }
.brand { display: flex; align-items: center; gap: 10px; cursor: pointer; white-space: nowrap; }
.logo { font-size: 26px; color: var(--accent); line-height: 1; }
.title { font-weight: 700; font-size: 15px; }
.title .muted { font-weight: 400; }
.search-box { flex: 1; max-width: 620px; margin: 0 auto; position: relative; }
.search-ico { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 18px; }
#search { width: 100%; padding: 9px 14px 9px 36px; border-radius: 10px; border: 1px solid var(--line2);
  background: var(--surface2); color: var(--txt); font: inherit; }
#search:focus { outline: none; border-color: var(--accent); background: var(--surface); }
.actions { display: flex; gap: 8px; align-items: center; }
.menu-wrap { position: relative; }

.dropdown { position: absolute; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: 0 12px 40px #0000002a; z-index: 60; }
#search-results { top: calc(100% + 6px); left: 0; right: 0; max-height: 70vh; overflow: auto; padding: 6px; }
.sr-group { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: 8px 10px 4px; }
.sr { padding: 8px 10px; border-radius: 8px; cursor: pointer; }
.sr:hover, .sr.active { background: var(--surface2); }
.sr .n { font-weight: 600; }
.sr .d { font-size: 12px; color: var(--muted); }
.sr mark { background: #fde68a; color: inherit; border-radius: 3px; padding: 0 1px; }
body.dark .sr mark { background: #5c4a1e; }
.menu { right: 0; top: calc(100% + 6px); width: 300px; padding: 6px; }
.menu button { display: block; width: 100%; text-align: left; background: none; border: 0; padding: 9px 10px; border-radius: 8px; font: inherit; color: var(--txt); cursor: pointer; }
.menu button small { display: block; color: var(--muted); font-size: 12px; }
.menu button:hover { background: var(--surface2); }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 6px 0; }
.toggle { display: flex; align-items: center; gap: 9px; padding: 9px 10px; border-radius: 8px; cursor: pointer; }
.toggle:hover { background: var(--surface2); }

/* ===================== LAYOUT ===================== */
main { display: flex; height: calc(100vh - 60px); }
#map-wrap { flex: 1; position: relative; overflow: hidden; }
#map { width: 100%; height: 100%; display: block; cursor: grab; touch-action: none; }
#map.dragging { cursor: grabbing; }
#viewport { transform-origin: 0 0; }
#viewport.anim { transition: transform .45s cubic-bezier(.4, 0, .2, 1); }
#panel { width: var(--panel-w); background: var(--surface); border-left: 1px solid var(--line); overflow-y: auto; }
.zoom { position: absolute; right: 14px; bottom: 14px; display: flex; flex-direction: column; gap: 6px; }
.zoom .btn { box-shadow: var(--shadow); }

.crumbs { position: absolute; top: 12px; left: 12px; right: 12px; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; z-index: 5; pointer-events: none; }
.crumbs > * { pointer-events: auto; }
.crumbs .back { box-shadow: var(--shadow); }
.crumbs .c { font-size: 13px; padding: 4px 10px; border-radius: 999px; background: var(--surface); border: 1px solid var(--line); cursor: pointer; }
.crumbs .c.cur { font-weight: 600; border-color: var(--line2); }
.crumbs .sep { color: var(--muted); }

/* ===================== MAPPA ===================== */
.zone { cursor: move; }
.zone rect { fill: var(--zc); fill-opacity: .06; stroke: var(--zc); stroke-opacity: .25; stroke-width: 1; transition: fill-opacity .15s; }
.zone:hover rect, .zone.dragging rect { fill-opacity: .11; stroke-opacity: .5; }
.zone text { fill: var(--zc); font-size: 13px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase; }
body.dark .zone rect { fill-opacity: .05; stroke-opacity: .2; }
body.dark .zone:hover rect, body.dark .zone.dragging rect { fill-opacity: .09; stroke-opacity: .4; }
body.dark .zone text { opacity: .85; }

.node { cursor: pointer; }
.node rect.box { fill: var(--surface); stroke: var(--line2); stroke-width: 1.2; transition: stroke .15s, fill .15s; }
.node rect.bar { fill: var(--nc); }
.node text { fill: var(--txt); font-size: 13px; font-weight: 600; pointer-events: none; }
.node:hover rect.box { stroke: var(--nc); stroke-width: 2; }
/* riquadri non coinvolti: restano opachi (coprono le frecce che passano dietro) ma sbiaditi */
.node.dim text { opacity: .3; }
.node.dim rect.bar { opacity: .25; }
.node.dim rect.box { stroke-opacity: .35; }
.node.center rect.box { fill: var(--nc); stroke: var(--nc); }
.node.center text { fill: #fff; font-size: 16px; }
body.dark .node.center text { fill: #14171c; }
.node.hl rect.box { stroke: var(--nc); stroke-width: 2.2; }

.edge { fill: none; stroke: var(--line2); stroke-width: 1.3; }
.edge.in { stroke: var(--in); stroke-width: 1.8; }
.edge.out { stroke: var(--out); stroke-width: 1.8; }
.edge.faint { stroke-opacity: .5; }
.m-in { fill: var(--in); } .m-out { fill: var(--out); } .m-n { fill: var(--line2); }
.edge-hit { fill: none; stroke: transparent; stroke-width: 16; cursor: pointer; }
.elabel { font-size: 12px; fill: var(--muted); cursor: pointer; paint-order: stroke; stroke: var(--bg); stroke-width: 5px; stroke-linejoin: round; }
.elabel tspan.more { font-weight: 700; }
.elink:hover .edge { stroke-width: 3.2; }
.elink:hover .elabel, .elink.sel .elabel { fill: var(--txt); font-weight: 600; }
.elink.sel .edge { stroke-width: 3.5; }
.colhead { font-size: 12px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; }
.colhead.in { fill: var(--in); } .colhead.out { fill: var(--out); }
.emptycol { font-size: 13px; fill: var(--muted); font-style: italic; }

/* ===================== PANNELLO ===================== */
.p-pad { padding: 18px 20px; }
.welcome h2 { margin: 4px 0 6px; font-size: 20px; }
.welcome p { color: var(--muted); margin: 0 0 14px; }
.steps { list-style: none; padding: 0; margin: 0 0 16px; counter-reset: s; }
.steps li { counter-increment: s; position: relative; padding: 0 0 12px 36px; }
.steps li::before { content: counter(s); position: absolute; left: 0; top: -1px; width: 24px; height: 24px; border-radius: 50%;
  background: var(--surface2); border: 1px solid var(--line2); display: flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 700; }
.quick { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; padding: 5px 11px; border-radius: 999px;
  border: 1px solid var(--line2); background: var(--surface); cursor: pointer; color: var(--txt); font-family: inherit; }
.chip:hover { border-color: var(--nc, var(--accent)); }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--nc); flex: none; display: inline-block; }

.c-head .cat { font-size: 12px; font-weight: 600; color: var(--nc); display: flex; align-items: center; gap: 6px; }
.c-head h2 { margin: 2px 0 2px; font-size: 22px; }
.c-head .grp { color: var(--muted); }
.molbox { background: var(--molbg); border: 1px solid var(--line); border-radius: var(--radius); margin: 14px 0 10px; padding: 8px;
  display: flex; justify-content: center; align-items: center; min-height: 90px; }
.c-desc { color: var(--txt); opacity: .85; margin: 0; }

.tabs { display: flex; gap: 4px; padding: 4px; background: var(--surface2); border-radius: 10px; margin: 18px 0 6px;
  position: sticky; top: 0; z-index: 3; }
.tabs button { flex: 1; border: 0; background: none; font: inherit; color: var(--muted); padding: 8px 6px; border-radius: 8px; cursor: pointer; font-weight: 600; font-size: 13px; }
.tabs button.act { background: var(--surface); color: var(--txt); box-shadow: var(--shadow); }
.tabs button.act[data-tab="in"] { color: var(--in); }
.tabs button.act[data-tab="out"] { color: var(--out); }
.tabs .n { font-weight: 400; opacity: .8; }
.tab-hint { font-size: 12px; color: var(--muted); margin: 6px 2px 4px; }
.filter-note { font-size: 12px; background: var(--surface2); border-radius: 8px; padding: 7px 10px; margin: 8px 0; display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.filter-note a { color: var(--accent); cursor: pointer; white-space: nowrap; }

.g-title { display: flex; align-items: center; gap: 7px; margin: 16px 0 6px; font-size: 13px; font-weight: 600; cursor: pointer; width: fit-content; }
.g-title:hover { text-decoration: underline; }
.g-title .arrow { color: var(--muted); font-weight: 400; }

.rx { border: 1px solid var(--line); border-radius: 10px; margin-bottom: 6px; background: var(--surface); }
.rx.open { border-color: var(--line2); box-shadow: var(--shadow); }
.rx-h { padding: 10px 12px; cursor: pointer; display: flex; gap: 10px; align-items: flex-start; }
.rx-h:hover { background: var(--surface2); border-radius: 10px; }
.rx-h .t { flex: 1; min-width: 0; }
.rx-h .n { font-weight: 600; }
.rx-h .r { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.rx-h .chev { color: var(--muted); transition: transform .2s; margin-top: 2px; }
.rx.open .rx-h .chev { transform: rotate(90deg); }
.rx-b { padding: 4px 12px 14px; display: none; }
.rx.open .rx-b { display: block; }
.type { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--surface2); color: var(--muted); margin-bottom: 8px; }

.scheme { background: var(--molbg); border: 1px solid var(--line); border-radius: 10px; padding: 10px; display: flex; flex-direction: column; align-items: center; gap: 2px; }
.scheme .side { display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: 4px 8px; width: 100%; }
.scheme .plus { color: var(--muted); font-size: 20px; }
.scheme .mol svg { display: block; max-width: 100%; height: auto; }
.scheme .down { display: flex; align-items: center; gap: 10px; color: var(--txt); font-size: 12.5px; max-width: 100%; }
.scheme .down b { font-size: 26px; line-height: 1; color: var(--muted); }
.scheme .down span { text-align: left; }
.formula { font-family: "Cambria Math", "Times New Roman", serif; font-size: 19px; color: var(--formula); padding: 10px 6px; white-space: nowrap; }
.scheme .smi { font-family: Consolas, monospace; font-size: 12px; color: var(--txt); padding: 6px; word-break: break-all; }
.caption { font-size: 12.5px; color: var(--muted); margin: 6px 0 10px; text-align: center; }
.facts { margin: 0; }
.facts div { padding: 7px 0; border-top: 1px solid var(--line); }
.facts dt { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin-bottom: 2px; }
.facts dd { margin: 0; }
.rx-links { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.empty { color: var(--muted); font-style: italic; padding: 10px 2px; }

/* ===================== MODALI ===================== */
.modal { position: fixed; inset: 0; background: #0f172a66; display: flex; align-items: center; justify-content: center; z-index: 100; padding: 16px; }
.modal-box { position: relative; width: min(820px, 100%); max-height: 88vh; overflow: auto; background: var(--surface);
  border-radius: 16px; padding: 24px 26px; box-shadow: 0 20px 60px #0004; }
.modal-box.small { width: min(560px, 100%); }
.modal-box h2 { margin: 0 0 4px; }
.close { position: absolute; right: 14px; top: 12px; border: 0; background: none; font-size: 18px; color: var(--muted); cursor: pointer; }
.row { display: flex; gap: 12px; flex-wrap: wrap; margin: 14px 0; align-items: flex-end; }
.row.right { justify-content: flex-end; align-items: center; }
.row label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); font-weight: 600; }
.row select { background: var(--surface2); color: var(--txt); border: 1px solid var(--line2); border-radius: 8px; padding: 8px; font: inherit; min-width: 200px; }
.row select#path-max { min-width: 0; }
.big-arrow { font-size: 22px; color: var(--muted); padding-bottom: 6px; }
.path { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin: 8px 0; cursor: pointer; }
.path:hover { border-color: var(--accent); }
.path .steps2 { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.path .cls { font-weight: 600; display: inline-flex; align-items: center; gap: 5px; }
.path .via { font-size: 12px; color: var(--muted); }
.path .len { font-size: 11px; color: var(--muted); margin-bottom: 4px; }
.filter-list { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 12px; }
.filter-list label { display: flex; gap: 8px; align-items: center; padding: 6px 4px; cursor: pointer; }
.filter-list small { color: var(--muted); }

/* Manuale */
#manual h2 { margin-top: 0; }
#manual h3 { margin: 22px 0 6px; font-size: 16px; }
#manual p, #manual li { color: var(--txt); }
#manual ul { padding-left: 20px; }
#manual .key { display: inline-block; border: 1px solid var(--line2); border-radius: 6px; padding: 0 6px; font-size: 12px; background: var(--surface2); }
#manual .legend-row { display: flex; gap: 18px; flex-wrap: wrap; }
#manual .sw { display: inline-block; width: 26px; height: 3px; vertical-align: middle; margin-right: 6px; border-radius: 2px; }

/* Guida interattiva */
#tour { position: fixed; inset: 0; z-index: 200; }
#tour-hole { position: fixed; border-radius: 12px; box-shadow: 0 0 0 9999px #0f172a99; transition: all .3s ease; pointer-events: none; outline: 2px solid #fff8; }
#tour-tip { position: fixed; width: 340px; background: var(--surface); border-radius: 14px; padding: 16px 18px 6px; box-shadow: 0 20px 50px #0005; transition: top .3s, left .3s; }
#tour-tip h3 { margin: 2px 0 6px; font-size: 16px; }
#tour-text { color: var(--txt); opacity: .9; }
.tour-step { font-size: 11px; font-weight: 700; color: var(--accent); letter-spacing: .06em; text-transform: uppercase; }

@media (max-width: 960px) {
  .title .muted, .actions #btn-path { display: none; }
  main { flex-direction: column; }
  #panel { width: 100%; height: 52%; border-left: 0; border-top: 1px solid var(--line); }
}
