:root {
  --navy: #042139;
  --blue: #2563eb;
  --amber: #d97706;
  --bg: #f5f7fa;
  --surface: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --danger: #dc2626;
  --ok: #16a34a;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220; --surface: #111a2c; --text: #e2e8f0; --muted: #94a3b8; --border: #23304a;
    --blue: #60a5fa; --amber: #fbbf24; color-scheme: dark;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
a { color: var(--blue); }
h1 { font-size: 1.35rem; margin: 0 0 .25rem; }
h2 { font-size: 1.05rem; margin: 1.25rem 0 .5rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 1rem; flex-wrap: wrap;
  padding: .6rem 1rem; padding-top: max(.6rem, env(safe-area-inset-top));
  background: var(--navy); color: #fff;
}
.brand { display: flex; align-items: center; gap: .5rem; color: #fff; text-decoration: none; font-weight: 700; letter-spacing: .02em; }
.topbar nav { display: flex; gap: .25rem; }
.topbar nav a { color: #cbd5e1; text-decoration: none; padding: .35rem .65rem; border-radius: 6px; }
.topbar nav a.active { background: rgba(255,255,255,.14); color: #fff; }
.whoami { margin-left: auto; font-size: .8rem; color: #cbd5e1; }
@media (max-width: 760px) { .account { margin-left: auto; } }
body:not(.is-master) .master-only { display: none !important; }
.account { display: flex; gap: .25rem; }
.whoami + .account { margin-left: 0; }
button.link { background: none; border: 0; color: #cbd5e1; min-height: 32px; padding: .3rem .55rem; border-radius: 6px; }
button.link:hover { background: rgba(255,255,255,.14); color: #fff; }
body.logged-out .topbar nav, body.logged-out .account, body.logged-out .whoami { display: none; }
.login { max-width: 420px; margin: 8vh auto 0; display: flex; flex-direction: column; gap: .9rem; }
.login label { display: flex; flex-direction: column; gap: .3rem; font-size: .9rem; }

main { max-width: 1500px; margin: 0 auto; padding: 1rem; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; }
.grid-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: .75rem; }
.asm-card { display: block; text-decoration: none; color: inherit; }
.asm-card:hover { border-color: var(--blue); }
.asm-card h3 { margin: 0 0 .3rem; font-size: 1rem; }

.row { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.spacer { flex: 1; }

input, select, textarea, button {
  font: inherit; color: inherit; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); padding: .5rem .65rem; min-height: 40px;
}
input, select, textarea { width: 100%; }
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--blue); outline-offset: -1px; }
button { cursor: pointer; background: var(--surface); }
button.primary { background: var(--blue); border-color: var(--blue); color: #fff; font-weight: 600; }
button.danger { color: var(--danger); }
button:disabled { opacity: .6; cursor: progress; }
.row input, .row select { width: auto; flex: 1 1 180px; }

.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: .5rem; margin: .75rem 0; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .6rem .75rem; }
.stat b { display: block; font-size: 1.2rem; }
.stat span { font-size: .8rem; color: var(--muted); }

/* Tabella BOM */
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table { border-collapse: collapse; width: 100%; font-size: .9rem; }
th, td { padding: .5rem .6rem; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
th { position: sticky; top: 0; background: var(--surface); font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
tbody tr { cursor: pointer; }
tbody tr:hover { background: color-mix(in srgb, var(--blue) 6%, transparent); }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
td.wrap { white-space: normal; min-width: 160px; }
tr.removed td { opacity: .5; text-decoration: line-through; }

.pill { display: inline-block; padding: .1rem .5rem; border-radius: 999px; font-size: .78rem; font-weight: 600; white-space: nowrap;
  background: color-mix(in srgb, var(--c, #64748b) 15%, transparent); color: var(--c, #64748b); }
.tag-new { --c: var(--amber); }
.tag-removed { --c: var(--danger); }

/* Mobile: righe come schede */
@media (max-width: 760px) {
  main { padding: .75rem; }
  .whoami { display: none; }
  .table-wrap { background: none; border: 0; overflow: visible; }
  table, tbody, tr, td { display: block; width: 100%; }
  thead { display: none; }
  tbody tr { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: .6rem; padding: .5rem .75rem; }
  td { border: 0; padding: .2rem 0; white-space: normal; display: flex; justify-content: space-between; gap: 1rem; text-align: right; }
  td::before { content: attr(data-label); color: var(--muted); font-size: .8rem; text-align: left; }
  td.num { text-align: right; }
  td.title-cell { font-weight: 600; font-size: 1rem; text-align: left; display: block; }
  td.title-cell::before { content: none; }
  td.empty-mobile { display: none; }
}

/* Finestra di modifica */
dialog { border: 0; border-radius: 14px; padding: 0; width: min(760px, 100vw - 1rem); max-height: 92vh;
  background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.35); }
dialog::backdrop { background: rgba(2, 10, 25, .55); }
.dlg-head { position: sticky; top: 0; background: var(--surface); padding: 1rem 1rem .5rem; border-bottom: 1px solid var(--border); z-index: 1; }
.dlg-body { padding: .5rem 1rem 1rem; }
.dlg-foot { position: sticky; bottom: 0; background: var(--surface); padding: .75rem 1rem; padding-bottom: max(.75rem, env(safe-area-inset-bottom)); border-top: 1px solid var(--border); display: flex; gap: .5rem; justify-content: flex-end; }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); margin: .75rem 0; padding: .5rem .75rem .75rem; }
legend { font-weight: 600; padding: 0 .3rem; }
.fields { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: .6rem; }
.fields label { display: flex; flex-direction: column; gap: .2rem; font-size: .82rem; color: var(--muted); }
.fields label.full { grid-column: 1 / -1; }
.fields input, .fields select, .fields textarea { color: var(--text); }
.ro { padding: .5rem 0; color: var(--text); font-size: .95rem; word-break: break-word; }
.history { font-size: .82rem; color: var(--muted); max-height: 180px; overflow: auto; }
.history div { padding: .2rem 0; border-bottom: 1px dashed var(--border); }
@media (max-width: 760px) {
  dialog { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; border-radius: 0; }
}

.toast { position: fixed; left: 50%; bottom: 1.25rem; transform: translateX(-50%) translateY(150%); transition: transform .2s, opacity .2s; opacity: 0; pointer-events: none;
  background: var(--navy); color: #fff; padding: .65rem 1rem; border-radius: 8px; max-width: calc(100vw - 2rem); z-index: 100; }
.toast.show { transform: translateX(-50%) translateY(0); opacity: 1; }
.toast.error { background: var(--danger); }
.error-box { border-color: var(--danger); color: var(--danger); }

.check { display: flex; flex-direction: column; gap: .6rem; }
.check-step.ok b { color: var(--ok); }
.check-step.ko b { color: var(--danger); }
