/* =====================================================================
 * style.css — Soglie PID e Polveri (tool di calcolo)
 * Layout: pagina unica. In alto ingresso dati (elenco sostanze con «+»
 * a sinistra, sostanze selezionate e concentrazioni a destra), sotto i
 * risultati (VOC/PID e Polveri affiancati) e i dettagli espandibili.
 * Tipografia: Arial, scala compatta. Un solo colore principale (blu
 * petrolio); arancione/rosso solo per avvisi e criticità.
 * ===================================================================== */
:root {
  --bg: #f3f5f6;
  --surface: #ffffff;
  --surface-2: #f6f8f9;
  --ink: #172026;
  --muted: #56646d;
  --faint: #8a979f;
  --line: #dde3e7;
  --line-strong: #c3ccd2;
  --accent: #1c5870;
  --accent-ink: #ffffff;
  --accent-soft: #e4eef2;
  --warn: #9a4d08;
  --warn-soft: #fdf4e8;
  --warn-line: #efc690;
  --crit: #b42318;
  --crit-soft: #fdeceb;
  --crit-line: #f1b3ad;
  --focus: #2b7fa3;
  --bar: #9cc3d3;
  --ly: #e07a1f;
  --font: Arial, "Helvetica Neue", Helvetica, sans-serif;
  --fs: 13px;
  --fs-s: 12px;
  --fs-xs: 11px;
  --r: 5px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #11171a; --surface: #182024; --surface-2: #1d262b; --ink: #e3e9ec; --muted: #9db0ba; --faint: #73858f;
    --line: #29343a; --line-strong: #3a474e; --accent: #7cbcd6; --accent-ink: #0d1a20; --accent-soft: #1b313b;
    --warn: #f2a65a; --warn-soft: #2f2418; --warn-line: #6b4a24; --crit: #f28b80; --crit-soft: #351d1b; --crit-line: #6e302a;
    --focus: #8ccbe3; --bar: #2f5d70; --ly: #f2a65a; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #11171a; --surface: #182024; --surface-2: #1d262b; --ink: #e3e9ec; --muted: #9db0ba; --faint: #73858f;
  --line: #29343a; --line-strong: #3a474e; --accent: #7cbcd6; --accent-ink: #0d1a20; --accent-soft: #1b313b;
  --warn: #f2a65a; --warn-soft: #2f2418; --warn-line: #6b4a24; --crit: #f28b80; --crit-soft: #351d1b; --crit-line: #6e302a;
  --focus: #8ccbe3; --bar: #2f5d70; --ly: #f2a65a; color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
body { margin: 0; background: var(--bg); color: var(--ink); font: var(--fs)/1.4 var(--font); }
h1, h2, h3, h4 { margin: 0; line-height: 1.25; font-weight: 700; }
h2 { font-size: 14px; }
h3 { font-size: 13px; }
p { margin: 0; }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: var(--fs-s); }
.xs { font-size: var(--fs-xs); }
.label { font-size: var(--fs-xs); letter-spacing: .06em; text-transform: uppercase; color: var(--muted); font-weight: 700; }
.wrap { max-width: 1320px; margin: 0 auto; padding-inline: 16px; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.row { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.spacer { flex: 1; }
.nowrap { white-space: nowrap; }

/* ---------- Header ---------------------------------------------------- */
.app-header { background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: env(safe-area-inset-top, 0px); z-index: 30; }
.app-header .wrap { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 14px; padding-block: 7px; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 14px; white-space: nowrap; }
.brand svg { width: 22px; height: 22px; flex: none; }
.tabs-main { display: flex; gap: 2px; }
.tabs-main button { border: 0; background: none; padding: 6px 10px; border-radius: var(--r); color: var(--muted); cursor: pointer; font-weight: 700; }
.tabs-main button[aria-current="page"] { background: var(--accent-soft); color: var(--accent); }
.proj { display: flex; gap: 6px; align-items: center; margin-left: auto; min-width: 0; }
.proj input { width: 190px; min-width: 0; }

/* Menu progetto (details) */
.menu { position: relative; }
.menu > summary { list-style: none; }
.menu > summary::-webkit-details-marker { display: none; }
.menu-panel { position: absolute; right: 0; top: calc(100% + 4px); background: var(--surface); border: 1px solid var(--line-strong); border-radius: 6px; box-shadow: 0 6px 24px rgba(0,0,0,.14); padding: 6px; min-width: 230px; display: flex; flex-direction: column; z-index: 40; }
.menu-panel button { text-align: left; border: 0; background: none; padding: 7px 10px; border-radius: 4px; cursor: pointer; }
.menu-panel button:hover { background: var(--surface-2); }
.menu-panel hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; width: 100%; }
.menu-panel .label { padding: 6px 10px 2px; }

/* ---------- Controls -------------------------------------------------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 5px; border: 1px solid var(--line-strong); background: var(--surface); padding: 5px 10px; border-radius: var(--r); cursor: pointer; font-weight: 700; font-size: var(--fs-s); white-space: nowrap; }
@media (hover: hover) { .btn:hover { border-color: var(--accent); } }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--crit); }
.btn.confirm { background: var(--crit); border-color: var(--crit); color: #fff; }
input[type="text"], input[type="search"], select {
  border: 1px solid var(--line-strong); background: var(--surface); border-radius: var(--r); padding: 4px 7px; min-width: 0; max-width: 100%; height: 28px;
}
select { padding-right: 4px; }
input.invalid { border-color: var(--crit); background: var(--crit-soft); }
input[type="checkbox"] { width: 15px; height: 15px; margin: 0; accent-color: var(--accent); vertical-align: middle; }
.field { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.field > span { font-size: var(--fs-xs); color: var(--muted); }
.field input { width: 100%; }

/* ---------- Panels ---------------------------------------------------- */
main { padding-block: 12px 24px; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: 6px; min-width: 0; }
.panel-head { display: flex; flex-wrap: wrap; gap: 6px 10px; align-items: center; padding: 8px 12px; border-bottom: 1px solid var(--line); }
.panel-body { padding: 10px 12px; }
.input-grid { display: grid; grid-template-columns: minmax(260px, 34%) minmax(0, 1fr); gap: 10px; align-items: start; }
.res-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; align-items: start; }

/* Elenco sostanze (sinistra) */
.lib-tools { display: flex; gap: 6px; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.lib-tools input { flex: 1 1 auto; }
.lib-tools select { flex: 0 1 120px; }
.lib-bar { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 5px 10px; border-bottom: 1px solid var(--line); font-size: var(--fs-s); color: var(--muted); }
.lib { max-height: 460px; overflow-y: auto; }
.lib-cat { position: sticky; top: 0; z-index: 1; background: var(--surface-2); color: var(--muted); font-size: var(--fs-xs); font-weight: 700; text-transform: uppercase; letter-spacing: .04em; padding: 4px 10px; border-bottom: 1px solid var(--line); }
.lib-item { display: grid; grid-template-columns: 26px minmax(0, 1fr) auto; gap: 8px; align-items: center; padding: 4px 10px; border-bottom: 1px solid var(--line); }
.lib-item:last-child { border-bottom: 0; }
.lib-item .nm { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lib-item .cas { color: var(--faint); font-size: var(--fs-xs); margin-left: 4px; }
.lib-item.on { background: var(--accent-soft); }
.add { width: 24px; height: 24px; border-radius: 50%; border: 1px solid var(--accent); background: var(--surface); color: var(--accent); font: 700 16px/1 var(--font); cursor: pointer; display: grid; place-items: center; padding: 0; }
@media (hover: hover) { .add:hover { background: var(--accent-soft); } .lib-item.on .add:hover { background: var(--accent); filter: brightness(1.15); } }
.lib-item.on .add { background: var(--accent); color: var(--accent-ink); font-size: 13px; }
.tags { display: flex; gap: 3px; }
.tag { font-size: 10px; font-weight: 700; padding: 1px 4px; border-radius: 3px; border: 1px solid var(--line-strong); color: var(--muted); letter-spacing: .03em; }
.tag.v { border-color: var(--accent); color: var(--accent); }
.tag.p { border-color: var(--muted); }

/* Matrici */
.mat-bar { display: flex; flex-wrap: wrap; gap: 6px 14px; align-items: center; }
.mat { display: inline-flex; align-items: center; gap: 5px; font-weight: 700; }
.mat select { height: 24px; font-size: var(--fs-xs); font-weight: 400; padding: 0 2px; }

/* ---------- Tables ---------------------------------------------------- */
.tw { overflow-x: auto; min-width: 0; }
table { width: 100%; border-collapse: collapse; font-size: var(--fs-s); }
th, td { padding: 4px 8px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
thead th { background: var(--surface-2); font-size: var(--fs-xs); letter-spacing: .03em; text-transform: uppercase; color: var(--muted); font-weight: 700; white-space: nowrap; }
td.n, th.n { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
td.c, th.c { text-align: center; }
@media (hover: hover) { tbody tr:hover { background: var(--surface-2); } }
tr.crit td { background: var(--crit-soft); }
td.hl { font-weight: 700; color: var(--crit); }
tr.excl td { color: var(--faint); }
tr.tot td { font-weight: 700; background: var(--surface-2); }
.sub { display: block; font-size: var(--fs-xs); color: var(--faint); text-transform: none; letter-spacing: 0; font-weight: 400; }
.cin { width: 84px; text-align: right; height: 26px; }
.na { color: var(--faint); text-align: center; display: block; }
.x { border: 0; background: none; color: var(--faint); cursor: pointer; font-size: 15px; line-height: 1; padding: 2px 6px; border-radius: 4px; }
@media (hover: hover) { .x:hover { color: var(--crit); background: var(--crit-soft); } }
.x.confirm { color: #fff; background: var(--crit); font-size: var(--fs-xs); font-weight: 700; }
.empty { padding: 18px 12px; color: var(--muted); text-align: center; }
.fbar { position: relative; min-width: 76px; }
.fbar i { position: absolute; left: 0; top: 3px; bottom: 3px; background: var(--bar); border-radius: 2px; z-index: 0; }
.fbar span { position: relative; z-index: 1; }

/* ---------- Alerts ---------------------------------------------------- */
.alert { border: 1px solid var(--line); border-left-width: 3px; border-radius: 4px; padding: 6px 10px; background: var(--surface); font-size: var(--fs-s); }
.alert.info { border-left-color: var(--accent); }
.alert.warn { border-color: var(--warn-line); border-left-color: var(--warn); background: var(--warn-soft); }
.alert.crit { border-color: var(--crit-line); border-left-color: var(--crit); background: var(--crit-soft); }
.alert ul { margin: 3px 0 0; padding-left: 16px; }
.alert li { margin: 1px 0; }

/* ---------- Result cards ---------------------------------------------- */
.rcard { background: var(--surface); border: 1.5px solid var(--accent); border-radius: 7px; overflow: hidden; min-width: 0; }
.rcard > header { background: var(--accent); color: var(--accent-ink); padding: 6px 12px; font-weight: 700; font-size: var(--fs-s); letter-spacing: .04em; text-transform: uppercase; display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.rcard.cr { border-color: var(--muted); }
.rcard.cr > header { background: var(--muted); }
.rcard > header span { font-weight: 400; text-transform: none; letter-spacing: 0; opacity: .9; }
.rcols { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.rcol { padding: 10px 12px; display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.rcol + .rcol { border-left: 1px solid var(--line); }
.big { font-size: 26px; font-weight: 700; line-height: 1.05; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.big small { font-size: 13px; font-weight: 400; color: var(--muted); margin-left: 4px; }
.big.none { font-size: 14px; color: var(--faint); font-weight: 400; }
dl.kv { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 2px 10px; margin: 0; font-size: var(--fs-s); }
dl.kv dt { color: var(--muted); }
dl.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.rnote { padding: 6px 12px; border-top: 1px solid var(--line); background: var(--surface-2); font-size: var(--fs-xs); color: var(--muted); }
.corr { border-top: 1px dashed var(--line-strong); padding-top: 6px; }
.flag { color: var(--warn); font-weight: 700; }

/* ---------- Disclosure ----------------------------------------------- */
details.dis { background: var(--surface); border: 1px solid var(--line); border-radius: 6px; }
details.dis > summary { cursor: pointer; padding: 8px 12px; font-weight: 700; list-style: none; display: flex; align-items: center; gap: 8px; }
details.dis > summary::-webkit-details-marker { display: none; }
details.dis > summary::before { content: ""; width: 6px; height: 6px; border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted); transform: rotate(-45deg); transition: transform .15s; flex: none; }
details.dis[open] > summary::before { transform: rotate(45deg); }
details.dis > summary .muted { font-weight: 400; }
details.dis > .body { padding: 0 12px 12px; display: flex; flex-direction: column; gap: 10px; }
details.dis > .body.flush { padding: 0 0 4px; }
.params { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px 12px; }
.params .grp { grid-column: 1 / -1; margin-top: 2px; text-transform: none; letter-spacing: 0; font-size: var(--fs-s); }

/* Flussi di calcolo */
.flow-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 8px; }
.flow { border: 1px solid var(--line); border-radius: 6px; padding: 8px; background: var(--surface-2); min-width: 0; font-size: var(--fs-s); }
.flow h4 { font-size: var(--fs); margin-bottom: 6px; }
.chain { list-style: none; margin: 0; padding: 0; }
.chain li { display: grid; grid-template-columns: 120px minmax(0, 1fr); gap: 6px; padding: 2px 0; border-bottom: 1px dotted var(--line); }
.chain li:last-child { border-bottom: 0; }
.chain .k { color: var(--muted); font-size: var(--fs-xs); }
.chain .v { overflow-wrap: anywhere; font-variant-numeric: tabular-nums; }
.chain li.skip .v { color: var(--faint); }
.chain li.hl .v { color: var(--crit); font-weight: 700; }
.steps { margin: 6px 0 0; padding-left: 16px; font-size: var(--fs-xs); color: var(--muted); }
.steps li { margin: 1px 0; overflow-wrap: anywhere; }
.formulas { font-size: var(--fs-s); }
.formulas td:first-child { color: var(--muted); white-space: nowrap; }

/* Schema sintetico */
.schema { display: flex; flex-direction: column; align-items: center; padding: 4px 0; font-size: var(--fs-s); }
.schema .node { border: 1.5px solid var(--line-strong); background: var(--surface); border-radius: 5px; padding: 4px 10px; text-align: center; max-width: 100%; }
.schema .node b { display: block; font-size: var(--fs-xs); letter-spacing: .05em; text-transform: uppercase; }
.schema .node.root, .schema .node.end { border-color: var(--accent); }
.schema .node.root { background: var(--accent-soft); color: var(--accent); }
.schema .vl { width: 2px; height: 10px; background: var(--line-strong); }
.schema .split { position: relative; width: 100%; max-width: 440px; height: 10px; }
.schema .split::before { content: ""; position: absolute; left: 25%; right: 25%; top: 0; border-top: 2px solid var(--line-strong); }
.schema .br { display: grid; grid-template-columns: 1fr 1fr; width: 100%; max-width: 440px; }
.schema .b { display: flex; flex-direction: column; align-items: center; padding-inline: 4px; min-width: 0; }
.schema .b::before { content: ""; width: 2px; height: 10px; background: var(--line-strong); margin-top: -10px; }
.schema .ar { color: var(--faint); line-height: 14px; height: 14px; font-size: var(--fs-xs); }
.schema .cnt { font-size: var(--fs-xs); color: var(--muted); }

/* ---------- Database editor ------------------------------------------ */
.db-tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); padding: 0 8px; overflow-x: auto; }
.db-tabs button { border: 0; background: none; padding: 8px 10px; cursor: pointer; color: var(--muted); border-bottom: 2px solid transparent; font-weight: 700; white-space: nowrap; }
.db-tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); }
.db-tools { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.db-tools input[type="search"] { flex: 1 1 200px; }
.db-io { display: flex; flex-wrap: wrap; gap: 6px; }
.grid-wrap { overflow: auto; max-height: calc(100vh - 210px); min-height: 240px; }
table.grid { width: max-content; min-width: 100%; border-collapse: separate; border-spacing: 0; font-size: var(--fs-s); }
table.grid th, table.grid td { padding: 0; border-bottom: 1px solid var(--line); border-right: 1px solid var(--line); background: var(--surface); }
table.grid thead th { position: sticky; top: 0; z-index: 2; background: var(--surface-2); padding: 5px 6px; cursor: pointer; user-select: none; text-transform: none; font-size: var(--fs-xs); letter-spacing: 0; }
table.grid .stk { position: sticky; left: 0; z-index: 1; box-shadow: 1px 0 0 var(--line-strong); }
table.grid thead th.stk { z-index: 3; }
table.grid td input[type="text"], table.grid td select { width: 100%; border: 0; border-radius: 0; background: transparent; height: 26px; padding: 3px 6px; }
table.grid td input[type="text"]:focus, table.grid td select:focus { background: var(--accent-soft); outline: 2px solid var(--focus); outline-offset: -2px; }
table.grid td input.n { text-align: right; font-variant-numeric: tabular-nums; }
table.grid td input[readonly] { color: var(--faint); }
table.grid td.c { text-align: center; }
table.grid td.act { width: 34px; text-align: center; }
table.grid tbody tr:hover td { background: var(--surface-2); }
.db-foot { padding: 6px 10px; font-size: var(--fs-xs); color: var(--muted); border-top: 1px solid var(--line); }

/* ---------- Dialog / toast / footer ----------------------------------- */
dialog { border: 1px solid var(--line-strong); border-radius: 8px; padding: 0; width: min(560px, calc(100vw - 24px)); max-height: calc(100vh - 40px); background: var(--surface); color: var(--ink); box-shadow: 0 10px 40px rgba(0,0,0,.2); }
dialog::backdrop { background: rgba(15, 25, 30, .45); }
.dlg-head { padding: 10px 14px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; }
.dlg-body { padding: 12px 14px; overflow-y: auto; max-height: calc(100vh - 160px); display: flex; flex-direction: column; gap: 10px; }
.dlg-foot { padding: 10px 14px; border-top: 1px solid var(--line); display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }
.toast { position: fixed; left: 50%; bottom: calc(16px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 8px 14px; border-radius: 6px; font-size: var(--fs-s); z-index: 100; max-width: calc(100vw - 32px); }
.app-footer { border-top: 1px solid var(--line); padding-block: 10px; font-size: var(--fs-xs); color: var(--muted); }
/* Report: anteprima a schermo (sovrapposta) e stampa */
#report { position: fixed; inset: 0; z-index: 90; background: var(--bg); overflow-y: auto; padding: 0 0 24px; }
#report[hidden] { display: none; }
body.rep-open { overflow: hidden; }
.rep-bar { position: sticky; top: 0; z-index: 2; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; padding: 8px 16px; padding-top: calc(8px + env(safe-area-inset-top, 0px)); background: var(--surface); border-bottom: 1px solid var(--line); }
.rep-hint { max-width: 820px; margin: 8px auto 0; padding: 0 16px; font-size: var(--fs-xs); color: var(--muted); }
#report-body { max-width: 820px; margin: 10px auto 0; background: #fff; color: #111; padding: 24px 28px; border: 1px solid var(--line); border-radius: 6px; font-size: 12px; line-height: 1.45; overflow-x: auto; }
#report-body h1 { font-size: 18px; margin-bottom: 4px; }
#report-body h2 { font-size: 14px; margin: 16px 0 6px; border-bottom: 1px solid #999; padding-bottom: 3px; }
#report-body h3 { font-size: 12.5px; margin: 10px 0 4px; }
#report-body p { margin: 4px 0; }
#report-body table { font-size: 11px; color: #111; margin: 6px 0; }
#report-body th { background: #f2f4f5; color: #333; text-transform: none; letter-spacing: 0; font-size: 10.5px; }
#report-body th, #report-body td { border: 1px solid #ccc; padding: 3px 5px; }
#report-body .box { border: 1.5px solid #1c5870; padding: 8px 10px; border-radius: 4px; }
#report-body .g2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 10px; }
#report-body .disc { margin-top: 14px; border-top: 1px solid #999; padding-top: 6px; font-size: 10px; color: #444; }
@media (max-width: 640px) { #report-body { padding: 14px 12px; border-radius: 0; border-left: 0; border-right: 0; } }

/* ---------- Responsive ------------------------------------------------ */
@media (max-width: 980px) {
  .input-grid, .res-grid { grid-template-columns: minmax(0, 1fr); }
  .lib-bar { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 5px 10px; border-bottom: 1px solid var(--line); font-size: var(--fs-s); color: var(--muted); }
.lib { max-height: 240px; }
}
@media (max-width: 640px) {
  :root { --fs: 12.5px; --fs-s: 11.5px; --fs-xs: 10.5px; }
  .wrap { padding-inline: 10px; }
  main { padding-block: 8px 16px; }
  .app-header { position: static; }
  .app-header .wrap { padding-block: 6px; }
  .proj { margin-left: 0; width: 100%; }
  .proj input { flex: 1 1 auto; width: auto; }
  .brand-name { display: none; }
  .panel-head, .panel-body { padding: 7px 9px; }
  th, td { padding: 4px 5px; }
  .cin { width: 64px; }
  .hide-m { display: none; }
  .rcols { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
  .rcol { padding: 8px 9px; }
  .big { font-size: 21px; }
  dl.kv { grid-template-columns: minmax(0, 1fr); gap: 0; }
  dl.kv dt { font-size: var(--fs-xs); margin-top: 3px; }
  .chain li { grid-template-columns: 96px minmax(0, 1fr); }
  .grid-wrap { max-height: calc(100vh - 170px); }
  .db-io { flex-wrap: nowrap; overflow-x: auto; width: 100%; padding-bottom: 2px; }
  .db-tools .spacer { display: none; }
  table.grid .stk, table.grid thead th.stk { min-width: 128px !important; width: 128px !important; max-width: 128px; }
  .menu-panel { position: fixed; left: 10px; right: 10px; top: auto; min-width: 0; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

/* ---------- Report di stampa (PDF) ----------------------------------- */
@media print {
  @page { size: A4; margin: 14mm 12mm; }
  :root { color-scheme: light; --bg: #fff; --surface: #fff; --surface-2: #f2f4f5; --ink: #000; --muted: #444; --faint: #777; --line: #ccc; --line-strong: #999; --accent: #1c5870; --crit: #b42318; }
  body { font: 9.5pt/1.35 Arial, sans-serif; background: #fff; }
  .app-header, main, .app-footer, .toast, dialog { display: none !important; }
  #report { display: block !important; position: static; overflow: visible; background: #fff; }
  .rep-bar, .rep-hint { display: none !important; }
  #report-body { border: 0; padding: 0; max-width: none; margin: 0; }
  #report h1 { font-size: 14pt; margin-bottom: 3pt; }
  #report h2 { font-size: 11pt; margin: 12pt 0 4pt; border-bottom: 1px solid #999; padding-bottom: 2pt; page-break-after: avoid; }
  #report h3 { font-size: 10pt; margin: 8pt 0 3pt; }
  #report table { font-size: 8.5pt; }
  #report th, #report td { padding: 2pt 4pt; }
  #report tr { page-break-inside: avoid; }
  #report .box { border: 1.2pt solid #1c5870; padding: 6pt 8pt; }
  #report .g2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8pt; }
  #report .disc { margin-top: 12pt; border-top: 1px solid #999; padding-top: 5pt; font-size: 8pt; }
}

/* ---------- Dispositivi touch: niente zoom automatico sui campi ------
 * iOS/Android ingrandiscono la pagina quando un campo ha testo < 16px.
 * Sui dispositivi touch i campi usano quindi 16px (altezza contenuta). */
@media (pointer: coarse), (max-width: 640px) {
  input[type="text"], input[type="search"], input[type="number"], select, textarea { font-size: 16px !important; }
  input[type="text"], input[type="search"], select { height: 32px; }
  .cin { height: 32px; padding: 2px 5px; }
  .mat select { height: 30px; }
  table.grid td input[type="text"], table.grid td select { height: 32px; }
}

/* ---------- Fonti numerate --------------------------------------------- */
.refs { padding: 4px 12px 0; display: flex; flex-wrap: wrap; gap: 2px 12px; }
.refs span { white-space: normal; }
table.grid td input.srcn { text-align: center; font-variant-numeric: tabular-nums; }
.src-legend { border-top: 1px solid var(--line); padding: 8px 10px; font-size: var(--fs-s); max-height: 260px; overflow-y: auto; }
.legend-head { display: flex; flex-wrap: wrap; gap: 6px 12px; align-items: center; margin-bottom: 6px; }
.src-list { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 4px 14px; }
.src-list li { display: grid; grid-template-columns: 34px minmax(0, 1fr) auto; gap: 6px; align-items: center; }
.src-list .n { font-weight: 700; color: var(--accent); font-variant-numeric: tabular-nums; }
.src-list input { width: 100%; height: 26px; }

/* ---------- Database: carattere molto più piccolo ---------------------- */
table.grid { font-size: 10.5px; }
table.grid thead th { font-size: 10px; padding: 3px 4px; line-height: 1.2; white-space: normal; vertical-align: bottom; }
table.grid td input[type="text"], table.grid td select { height: 20px; padding: 1px 4px; font-size: 10.5px; }
table.grid td.act { width: 26px; }
table.grid .x { font-size: 12px; padding: 0 4px; }
.src-legend, .src-list input { font-size: 10.5px; }
.src-list input { height: 20px; }
.db-foot { font-size: 10px; }
/* Database compatto anche sui dispositivi touch.
 * iOS ingrandisce la pagina quando si tocca un campo con testo < 16 px: i campi
 * restano a 16 px (nessuno zoom) e vengono ridotti solo visivamente con transform
 * (16 px × 0,6875 = 11 px), con larghezza compensata (1 / 0,6875 = 145,45 %). */
@media (pointer: coarse), (max-width: 640px) {
  table.grid td { height: 22px; overflow: hidden; position: relative; }
  table.grid td input[type="text"], table.grid td select {
    font-size: 16px !important; height: 32px !important; line-height: 32px; padding: 0 6px !important;
    width: 145.45% !important; max-width: none; transform: scale(.6875); transform-origin: 0 0; display: block;
    margin: 0 -45.45% -10px 0;   /* la trasformazione non riduce l'ingombro: si compensa con margini negativi */
  }
  table.grid td.c input[type="checkbox"] { transform: none; }
  .src-list li { height: 22px; overflow: hidden; }
  .src-list input { font-size: 16px !important; height: 32px !important; width: 145.45% !important; transform: scale(.6875); transform-origin: 0 0; margin: 0 -45.45% -10px 0; }
  table.grid .stk, table.grid thead th.stk { min-width: 110px !important; width: 110px !important; max-width: 110px; }
  html, body { overflow-x: hidden; }
}
}
}

/* ---------- Pulsanti copia --------------------------------------------- */
.tbl-tools { display: flex; justify-content: flex-end; padding: 4px 12px; }
.tbl-tools .btn { padding: 3px 8px; font-size: var(--fs-xs); }
.db-io .menu { display: inline-block; }
.db-io .menu-panel { right: 0; }
table.grid td input.ro-name { color: var(--ink) !important; cursor: default; }

/* ---------- Marchio VLEPly --------------------------------------------- */
.brand-name { letter-spacing: -.01em; }
.ly { color: var(--ly); }
.info-btn { width: 20px; height: 20px; border-radius: 50%; border: 1.5px solid var(--accent); background: transparent; color: var(--accent); font: italic 700 12px/1 Georgia, 'Times New Roman', serif; cursor: pointer; padding: 0; display: inline-grid; place-items: center; }
@media (hover: hover) { .info-btn:hover { background: var(--accent); color: var(--accent-ink); } }
@media (max-width: 640px) { .brand-name { display: inline !important; } }

/* ---------- Nome progetto ---------------------------------------------- */
.proj-name { display: flex; align-items: center; gap: 6px; min-width: 0; flex: 1 1 auto; justify-content: flex-end; }
.proj-name > span { font-size: var(--fs-xs); text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 700; }
.proj-name input { font-size: 12px; font-weight: 700; width: 210px; height: 26px; color: var(--ink); }
@media (pointer: coarse), (max-width: 640px) {
  /* testo a 16 px per evitare lo zoom di iOS, ridotto graficamente a ~12 px.
     Riduzione centrata in verticale e nessun ritaglio (overflow visibile), anche in orizzontale. */
  .proj-name { overflow: visible; height: auto; align-items: center; }
  .proj-name > span { line-height: normal; }
  .proj-name input { font-size: 16px !important; font-weight: 700; height: 34px !important; line-height: normal; padding: 0 8px !important;
    /* --pw = larghezza visibile; la larghezza reale è --pw / 0,75, compensata con margine negativo */
    --pw: 220px; flex: none; width: calc(var(--pw) / .75) !important; transform: scale(.75); transform-origin: 0 50%;
    margin: -4px calc(var(--pw) / -3) -4px 0; }
}
@media (max-width: 640px) {
  .proj-name input { --pw: calc(100vw - 20px - 70px - 112px - 12px); }
}
}
/* ---------- Unità dei risultati ---------------------------------------- */
.res-tools { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 14px; }
.unit-pick { display: inline-flex; align-items: center; gap: 6px; font-size: var(--fs-s); color: var(--muted); font-weight: 700; }
.unit-pick select { height: 26px; font-weight: 400; color: var(--ink); }
.res-tools .btn { padding: 3px 8px; font-size: var(--fs-xs); }

.beta { font-size: 9px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--ly); border: 1px solid var(--ly); border-radius: 3px; padding: 0 3px; line-height: 13px; margin-left: -2px; align-self: center; }
