:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --border: #dde3ea;
  --text: #1c2733;
  --muted: #5d6b7a;
  --accent: #0f766e;
  --accent-hover: #0b5e58;
  --accent-soft: #e3f3f1;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --warn-soft: #fff6dd;
  --nav-bg: #17212b;
  --nav-text: #c9d3dd;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06);
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }
html { font-size: 15px; }
body { margin: 0; font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); line-height: 1.45; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; margin: 0; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
h3 { font-size: 1rem; margin: 0 0 .5rem; }
h3.section { margin-top: 1.25rem; }
code, .mono { font-family: var(--mono); font-size: .92em; }
.muted { color: var(--muted); }
.small { font-size: .86rem; }
.nowrap { white-space: nowrap; }
.center { text-align: center; }
.pre { white-space: pre-line; margin: 0; }
.top0 { margin-top: 0; }
.spaced { margin-top: 1rem; }

/* ---------- layout ---------- */
.layout { display: flex; min-height: 100vh; }
.sidebar { width: 220px; flex-shrink: 0; background: var(--nav-bg); color: var(--nav-text); display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; }
.brand { display: block; padding: 1.1rem 1.25rem; font-weight: 700; font-size: 1.2rem; color: #fff; letter-spacing: .02em; }
.brand:hover { text-decoration: none; }
.brand small { display: block; font-weight: 400; font-size: .75rem; color: var(--nav-text); letter-spacing: 0; }
.sidebar nav { flex: 1; overflow-y: auto; }
.sidebar .nav-section { padding: .9rem 1.25rem .3rem; font-size: .7rem; text-transform: uppercase; letter-spacing: .08em; color: #7f8e9d; }
.sidebar nav a { display: block; padding: .45rem 1.25rem; color: var(--nav-text); border-left: 3px solid transparent; }
.sidebar nav a:hover { background: rgba(255,255,255,.05); text-decoration: none; color: #fff; }
.sidebar nav a.active { color: #fff; border-left-color: #2dd4bf; background: rgba(255,255,255,.07); }
.sidebar .user { padding: 1rem 1.25rem; border-top: 1px solid rgba(255,255,255,.08); font-size: .85rem; }
.sidebar .user .muted { color: #9fb0c0; }
.sidebar .user a, .sidebar .user button { color: var(--nav-text); }
.user-links { margin-top: .4rem; }
.user-links form { display: inline; }
.user-links button { background: none; border: 0; padding: 0; cursor: pointer; font: inherit; text-decoration: underline; }
.mobile-bar button { display: none; }

.main { flex: 1; min-width: 0; padding: 1.5rem 2rem 3rem; }
.page-header { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.25rem; }
.page-header .subtitle { color: var(--muted); margin-top: .2rem; }
.actions { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.actions.badges { margin-bottom: .3rem; }
.search { display: flex; gap: .5rem; }
.search input { min-width: 240px; }

@media (max-width: 860px) {
  .layout { flex-direction: column; }
  .sidebar { width: 100%; height: auto; position: static; }
  .sidebar nav, .sidebar .user { display: none; }
  .sidebar.open nav, .sidebar.open .user { display: block; }
  .mobile-bar { display: flex; align-items: center; justify-content: space-between; }
  .mobile-bar button { display: inline-block; background: none; border: 1px solid rgba(255,255,255,.3); color: #fff; border-radius: 6px; padding: .3rem .6rem; margin-right: 1rem; }
  .main { padding: 1rem; }
  .search input { min-width: 0; }
}

/* ---------- card e griglie ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.1rem 1.25rem; margin-bottom: 1.25rem; }
.card.flush { padding: 0; }
.card.narrow { max-width: 640px; }
.card.medium { max-width: 960px; }
.card-header { display: flex; align-items: center; justify-content: space-between; gap: .75rem; margin-bottom: .75rem; flex-wrap: wrap; }
.card-header h2 { margin: 0; }
.card-header.padded { padding: 1rem 1.25rem 0; }
.grid { display: grid; gap: 1.25rem; }
.grid > .card { margin-bottom: 0; }
.grid + .card, .grid + .grid { margin-top: 1.25rem; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-detail { grid-template-columns: minmax(0, 5fr) minmax(0, 7fr); align-items: start; }
.grid-detail > div > .card:last-child { margin-bottom: 0; }
@media (max-width: 1100px) { .grid-detail, .grid-3 { grid-template-columns: 1fr; } }
@media (max-width: 700px) { .grid-2 { grid-template-columns: 1fr; } }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 1rem; margin-bottom: 1.25rem; }
.stat { display: block; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem 1.2rem; box-shadow: var(--shadow); color: inherit; }
.stat:hover { text-decoration: none; border-color: var(--accent); }
.stat .value { font-size: 1.8rem; font-weight: 700; }
.stat .label { color: var(--muted); font-size: .85rem; }

/* ---------- pulsanti ---------- */
.btn { display: inline-flex; align-items: center; gap: .35rem; padding: .45rem .9rem; border-radius: 6px; border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; font-size: .92rem; cursor: pointer; text-decoration: none; white-space: nowrap; }
.btn:hover { background: #eef1f4; text-decoration: none; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-danger { color: var(--danger); border-color: #f1c4bf; }
.btn-danger:hover { background: var(--danger-soft); }
.btn-sm { padding: .2rem .55rem; font-size: .82rem; }
.btn-link { background: none; border: 0; color: var(--accent); padding: 0; cursor: pointer; font: inherit; }
.btn-link.danger { color: var(--danger); }
form.inline { display: inline; }
.push-right { margin-left: auto; }

/* ---------- tabelle ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: .5rem .6rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; background: #f8fafb; }
tbody tr:hover { background: #fafbfc; }
td.num, th.num { text-align: right; }
table.kv th { width: 38%; text-transform: none; letter-spacing: 0; font-size: .9rem; background: none; font-weight: 500; }
table.kv td, table.kv th { padding: .4rem .5rem; }
table.history td { padding: .3rem .5rem; }
.thumb-cell { width: 52px; }
.thumb-cell img, .thumb-placeholder { width: 44px; height: 44px; object-fit: cover; border-radius: 6px; display: block; background: #e9edf1; }

/* ---------- badge ---------- */
.badge { display: inline-block; padding: .1rem .5rem; border-radius: 999px; font-size: .78rem; font-weight: 600; background: #eef1f4; color: var(--muted); white-space: nowrap; }
.badge-type { color: #fff; }
.status-in_uso { background: #dcfce7; color: #166534; }
.status-magazzino { background: #e0e7ff; color: #3730a3; }
.status-riparazione { background: #fef3c7; color: #92400e; }
.status-guasto { background: #fee2e2; color: #991b1b; }
.status-dismesso { background: #e5e7eb; color: #4b5563; }
.code { font-family: var(--mono); font-weight: 600; }
.dot { display: inline-block; width: .7rem; height: .7rem; border-radius: 50%; vertical-align: middle; margin-right: .3rem; }
.port-free { color: var(--muted); font-style: italic; }

/* ---------- form ---------- */
label { font-weight: 500; font-size: .9rem; display: block; margin-bottom: .25rem; }
input[type=text], input[type=number], input[type=url], input[type=date], input[type=password], input[type=email], input[type=file], select, textarea {
  width: 100%; padding: .45rem .6rem; border: 1px solid #c8d1db; border-radius: 6px; font: inherit; background: #fff; color: var(--text);
}
input[type=color] { width: 3.5rem; height: 2.2rem; padding: .1rem; border: 1px solid #c8d1db; border-radius: 6px; background: #fff; }
input:focus, select:focus, textarea:focus { outline: 2px solid #99e0d6; border-color: var(--accent); }
textarea { resize: vertical; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .9rem 1.25rem; }
.form-grid .full { grid-column: 1 / -1; }
.card.narrow .form-grid { grid-template-columns: 1fr; }
@media (max-width: 700px) { .form-grid { grid-template-columns: 1fr; } }
.field .help { color: var(--muted); font-size: .8rem; margin-top: .2rem; }
.error-text { color: var(--danger); font-size: .85rem; margin-top: .25rem; }
.has-error input, .has-error select, .has-error textarea { border-color: var(--danger); }
.field-check { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.field-check label { margin: 0; }
.form-actions { display: flex; gap: .5rem; margin-top: 1.25rem; flex-wrap: wrap; align-items: center; }
.formset td { padding: .3rem .35rem; vertical-align: top; }
.formset td input, .formset td select { padding: .3rem .4rem; }
.formset td select { min-width: 9.5rem; }
.formset th { white-space: nowrap; }
.formset.compact { max-width: 640px; }
.narrow-input { width: 5em !important; }
.unit-input { width: 6em !important; }
.key-input { min-width: 9em; }
.label-input { min-width: 12em; }

.filters { display: grid; grid-template-columns: 2fr repeat(4, 1fr) auto; gap: .6rem; align-items: end; }
@media (max-width: 1100px) { .filters { grid-template-columns: 1fr 1fr; } }

/* ---------- messaggi ---------- */
.flash { padding: .65rem 1rem; border-radius: 6px; margin-bottom: 1rem; background: var(--accent-soft); border: 1px solid #b7e3dc; }
.flash.error { background: var(--danger-soft); border-color: #f1c4bf; color: var(--danger); }
.notice { padding: .65rem 1rem; border-radius: 6px; background: var(--warn-soft); border: 1px solid #f2dea0; margin: 1rem 0; }
.temp-password code { font-size: 1.4rem; background: #f1f5f9; padding: .4rem .8rem; border-radius: 6px; user-select: all; }

/* ---------- foto ---------- */
.cover { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; border-radius: var(--radius); background: #e9edf1; display: block; }
.cover-empty { display: flex; align-items: center; justify-content: center; color: var(--muted); }
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: .5rem; margin-top: .75rem; }
.gallery figure { margin: 0; }
.gallery img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 6px; display: block; border: 2px solid transparent; }
.gallery figure.is-cover img { border-color: var(--accent); }
.gallery figcaption { font-size: .75rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.gallery .tools { display: flex; gap: .4rem; font-size: .75rem; }
.upload { margin-top: 1rem; padding-top: 1rem; border-top: 1px dashed var(--border); display: grid; gap: .5rem; }
.upload-row { display: flex; gap: .75rem; align-items: center; flex-wrap: wrap; }
.doc-list { list-style: none; padding: 0; margin: 0; }
.doc-list li { display: flex; justify-content: space-between; gap: .5rem; padding: .35rem 0; border-bottom: 1px solid var(--border); }

/* ---------- tipi ---------- */
.type-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 1rem; }
.type-card { display: block; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; color: inherit; border-top: 4px solid var(--accent); }
.type-card:hover { text-decoration: none; box-shadow: 0 4px 12px rgba(16,24,40,.08); }
.type-card h3 { margin-bottom: .25rem; }

/* ---------- catene ---------- */
.chain-svg-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: #fbfcfd; }
.chain-svg { display: block; }
.chain-svg-wrap.fit .chain-svg { width: 100%; height: auto; }
.chain-tools { display: flex; justify-content: flex-end; margin-bottom: .4rem; }
.chain-svg .node rect:not(.stripe) { fill: #fff; stroke: #b9c4cf; stroke-width: 1.2; }
.chain-svg .node.current rect:not(.stripe) { stroke: var(--accent); stroke-width: 2.5; fill: var(--accent-soft); }
.chain-svg .node:hover rect:not(.stripe) { stroke: var(--accent); }
.chain-svg .node .title { font: 600 13px var(--mono); fill: var(--text); }
.chain-svg .node .subtitle { font: 12px system-ui, sans-serif; fill: var(--muted); }
.chain-svg .edge path { stroke-width: 2.5; stroke-linejoin: round; }
.chain-svg .edge:hover path { stroke-width: 4; }
.chain-svg .port { font: 600 11px var(--mono); fill: var(--text); }
.chain-svg .mid { font: 11px system-ui, sans-serif; }
.legend { display: flex; gap: 1rem; flex-wrap: wrap; font-size: .85rem; color: var(--muted); }

.pagination { display: flex; gap: .5rem; align-items: center; margin-top: 1rem; }

/* ---------- login ---------- */
.login-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 1rem; background: var(--nav-bg); }
.login-box { background: var(--surface); padding: 2rem; border-radius: 12px; width: 100%; max-width: 380px; box-shadow: 0 10px 30px rgba(0,0,0,.25); }
.login-box h1 { margin-bottom: .25rem; }
.login-box .field { margin-bottom: 1rem; }
.login-box .btn { width: 100%; justify-content: center; padding: .6rem; }

.empty { color: var(--muted); padding: 1.5rem; text-align: center; }
.tree-indent { display: inline-block; }
