:root {
  --bg: #f4f6f9;
  --card: #fff;
  --text: #1d2430;
  --muted: #677285;
  --line: #dde2ea;
  --primary: #1f5fa8;
  --primary-dark: #174a84;
  --danger: #b42318;
  --topbar: #16263d;
  --radius: 8px;
  --green: #1a7f37; --green-bg: #dcf5e3;
  --blue: #1f5fa8;  --blue-bg: #dde9f8;
  --orange: #a15c00; --orange-bg: #fdebcf;
  --purple: #6b3fa0; --purple-bg: #ece2f8;
  --red: #b42318;   --red-bg: #fbe0dd;
  --gray: #545d6b;  --gray-bg: #e6e9ee;
}
* { box-sizing: border-box; }
html { font-size: 14px; }
body { margin: 0; background: var(--bg); color: var(--text); font-family: "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif; line-height: 1.45; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.6rem; margin: 0 0 .2rem; }
h2 { font-size: 1.2rem; margin: 0 0 .8rem; }
h3 { font-size: 1.05rem; margin: 0 0 .6rem; }
.muted { color: var(--muted); }
.small { font-size: .88rem; }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.mono { font-family: Consolas, "Courier New", monospace; }

/* --- top bar --- */
.topbar { background: var(--topbar); color: #fff; display: flex; align-items: center; gap: 1rem; padding: 0 1.2rem; min-height: 54px; position: sticky; top: 0; z-index: 10; flex-wrap: wrap; }
.topbar a { color: #d7e3f4; }
.brand { font-weight: 700; font-size: 1.1rem; color: #fff !important; display: flex; align-items: center; gap: .5rem; white-space: nowrap; }
.brand:hover { text-decoration: none; }
.logo { background: var(--primary); color: #fff; border-radius: 6px; padding: 2px 7px; font-size: .9rem; }
.mainnav { display: flex; gap: .2rem; flex-wrap: wrap; }
.mainnav a { padding: .45rem .75rem; border-radius: 6px; }
.mainnav a:hover { background: rgba(255,255,255,.08); text-decoration: none; }
.mainnav a.on { background: rgba(255,255,255,.16); color: #fff; }
.quicksearch { margin-left: auto; }
.quicksearch input { width: 240px; border: 0; border-radius: 6px; padding: .45rem .7rem; background: rgba(255,255,255,.12); color: #fff; }
.quicksearch input::placeholder { color: #aab8cc; }
.userbox { display: flex; gap: .6rem; align-items: center; font-size: .9rem; white-space: nowrap; }
.userbox .role { background: rgba(255,255,255,.12); padding: 1px 7px; border-radius: 10px; font-size: .78rem; color: #cbd8ea; }
.navtoggle { display: none; background: none; border: 0; color: #fff; font-size: 1.4rem; cursor: pointer; }

.container { max-width: 1500px; margin: 0 auto; padding: 1.2rem; }

/* --- card, grid --- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.1rem 1.2rem; margin-bottom: 1rem; }
.pagehead { display: flex; align-items: flex-end; justify-content: space-between; gap: 1rem; margin-bottom: 1rem; flex-wrap: wrap; }
.pagehead .actions { display: flex; gap: .4rem; flex-wrap: wrap; }
.grid { display: grid; gap: 1rem; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: .8rem; margin-bottom: 1rem; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: .9rem 1rem; color: var(--text); display: block; border-left: 4px solid var(--gray); }
.stat:hover { text-decoration: none; border-color: var(--primary); }
.stat .n { font-size: 1.8rem; font-weight: 700; line-height: 1.1; }
.stat .l { color: var(--muted); font-size: .88rem; }
.stat.c-green { border-left-color: var(--green); } .stat.c-blue { border-left-color: var(--blue); }
.stat.c-orange { border-left-color: var(--orange); } .stat.c-purple { border-left-color: var(--purple); }
.stat.c-red { border-left-color: var(--red); } .stat.c-gray, .stat.c-dark { border-left-color: var(--gray); }

/* --- tables --- */
.tablewrap { overflow-x: auto; }
table.list { width: 100%; border-collapse: collapse; background: var(--card); }
table.list th, table.list td { padding: .45rem .6rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
table.list th { background: #eef1f5; font-weight: 600; font-size: .86rem; color: #3b4455; white-space: nowrap; position: sticky; top: 0; }
table.list th a { color: inherit; }
table.list tbody tr:hover { background: #f7f9fc; }
table.list td.sub { color: var(--muted); font-size: .86rem; }
table.list .code { font-weight: 600; white-space: nowrap; }
table.kv { border-collapse: collapse; width: 100%; }
table.kv th { text-align: left; color: var(--muted); font-weight: 500; padding: .3rem .8rem .3rem 0; width: 38%; vertical-align: top; }
table.kv td { padding: .3rem 0; }

/* --- badges --- */
.badge { display: inline-block; padding: 1px 8px; border-radius: 10px; font-size: .8rem; font-weight: 600; white-space: nowrap; }
.b-green { background: var(--green-bg); color: var(--green); }
.b-blue { background: var(--blue-bg); color: var(--blue); }
.b-orange { background: var(--orange-bg); color: var(--orange); }
.b-purple { background: var(--purple-bg); color: var(--purple); }
.b-red { background: var(--red-bg); color: var(--red); }
.b-gray { background: var(--gray-bg); color: var(--gray); }
.b-dark { background: #444c58; color: #fff; }

/* --- forms --- */
label { display: block; font-weight: 600; font-size: .88rem; margin-bottom: .2rem; color: #3b4455; }
input[type=text], input[type=search], input[type=password], input[type=email], input[type=date], input[type=number], select, textarea {
  width: 100%; padding: .45rem .55rem; border: 1px solid #c5ccd7; border-radius: 6px; font: inherit; background: #fff; color: var(--text);
}
input:focus, select:focus, textarea:focus { outline: 2px solid #9cc0ea; border-color: var(--primary); }
textarea { min-height: 70px; resize: vertical; }
.field { margin-bottom: .75rem; }
.field .hint { font-size: .8rem; color: var(--muted); margin-top: .15rem; }
.formgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 0 1rem; }
.formgrid .wide { grid-column: 1 / -1; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: .8rem 1rem .2rem; margin: 0 0 1rem; background: #fff; }
legend { font-weight: 700; padding: 0 .4rem; }
.check { display: flex; align-items: center; gap: .4rem; font-weight: 400; }
.check input { width: auto; }
.filters { display: grid; grid-template-columns: 2fr repeat(5, minmax(0, 1fr)) auto; gap: .6rem; align-items: end; }
.filters .field { margin: 0; }

.btn { display: inline-flex; align-items: center; gap: .3rem; padding: .45rem .9rem; border-radius: 6px; border: 1px solid #c5ccd7; background: #fff; color: var(--text); font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap; }
.btn:hover { background: #f0f3f7; text-decoration: none; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.danger { color: var(--danger); border-color: #e6b4ae; }
.btn.danger:hover { background: var(--red-bg); }
.btn.small { padding: .2rem .55rem; font-size: .85rem; }
.btn.link { border: 0; background: none; color: var(--primary); padding: 0; font-weight: 400; }

.flash { padding: .7rem 1rem; border-radius: var(--radius); margin-bottom: 1rem; border: 1px solid; }
.flash-ok { background: var(--green-bg); border-color: #a9dfb8; color: #125a28; }
.flash-error { background: var(--red-bg); border-color: #f1b3ab; color: #8a1a10; }
.flash-info { background: var(--blue-bg); border-color: #b3cdee; color: #173f6e; }

/* --- details / timeline --- */
details.action { border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: .6rem; background: #fff; }
details.action > summary { padding: .6rem .9rem; cursor: pointer; font-weight: 600; list-style: none; }
details.action > summary::before { content: "▸ "; color: var(--muted); }
details.action[open] > summary::before { content: "▾ "; }
details.action > div { padding: 0 .9rem .6rem; }
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { padding: .5rem 0 .5rem .9rem; border-left: 2px solid var(--line); position: relative; }
.timeline li::before { content: ""; position: absolute; left: -6px; top: .85rem; width: 10px; height: 10px; border-radius: 50%; background: var(--line); }
.timeline li.t-upgrade::before, .timeline li.t-riparazione::before { background: var(--purple); }
.timeline li.t-assegnazione::before { background: var(--green); }
.timeline li.t-restituzione::before { background: var(--blue); }
.timeline li.t-stato::before { background: var(--orange); }
.timeline .meta { font-size: .82rem; color: var(--muted); }
.empty { color: var(--muted); padding: 1rem 0; text-align: center; }

.tabs { display: flex; gap: .3rem; border-bottom: 1px solid var(--line); margin-bottom: 1rem; }
.tabs a { padding: .5rem .9rem; border: 1px solid transparent; border-bottom: 0; border-radius: 6px 6px 0 0; color: var(--muted); }
.tabs a.on { background: #fff; border-color: var(--line); color: var(--text); font-weight: 600; margin-bottom: -1px; }

/* --- login --- */
.loginbox { max-width: 380px; margin: 10vh auto; }
.loginbox h1 { text-align: center; margin-bottom: 1rem; }

/* --- print documents --- */
.print { max-width: 800px; margin: 0 auto; padding: 1.5rem; background: #fff; }
.print-page { background: #fff; }
.doc-head { display: flex; justify-content: space-between; border-bottom: 2px solid #000; padding-bottom: .5rem; margin-bottom: 1rem; }
.doc-head .company { font-size: 1.2rem; font-weight: 700; }
.doc-title { text-align: center; font-size: 1.25rem; font-weight: 700; margin: 1rem 0; text-transform: uppercase; }
.print table.list th { background: #eee; position: static; }
.print table.list, .print table.list th, .print table.list td { border: 1px solid #999; }
.doc-text { margin: 1.2rem 0; text-align: justify; }
.signs { display: grid; grid-template-columns: 1fr 1fr; gap: 3rem; margin-top: 3rem; }
.signs div { border-top: 1px solid #000; padding-top: .3rem; text-align: center; font-size: .9rem; }
.printbar { display: flex; gap: .5rem; justify-content: flex-end; margin-bottom: 1rem; }

@media print {
  .noprint, .printbar, .topbar, .flash { display: none !important; }
  body, .container, .print { background: #fff; padding: 0; margin: 0; max-width: none; }
  html { font-size: 11pt; }
  .card { border: 0; padding: 0; }
  table.list th { position: static; }
  a { color: #000; }
  tr { page-break-inside: avoid; }
}

@media (max-width: 1350px) {
  .topbar { gap: .6rem; padding: 0 .8rem; }
  .mainnav a { padding: .45rem .5rem; }
  .quicksearch input { width: 170px; }
  .userbox .role { display: none; }
}
@media (max-width: 1100px) {
  .filters { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .filters .field:first-child { grid-column: 1 / -1; }
  .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 800px) {
  .navtoggle { display: block; margin-left: auto; }
  .mainnav, .quicksearch, .userbox { display: none; width: 100%; }
  body.navopen .mainnav, body.navopen .quicksearch, body.navopen .userbox { display: flex; }
  .mainnav { flex-direction: column; padding-bottom: .5rem; }
  .quicksearch input { width: 100%; }
  .userbox { padding-bottom: .8rem; }
  .g2, .g3, .g4 { grid-template-columns: 1fr; }
  .filters { grid-template-columns: 1fr 1fr; }
  .container { padding: .8rem; }
}
