:root {
  --bg: #f3f5f8; --panel: #ffffff; --ink: #18212d; --muted: #66727f; --line: #dfe4ea;
  --accent: #e3051b; --accent-ink: #fff; --top: #16202c; --top-ink: #e8edf3; --hover: #eef2f6;
  --sel: #2563eb; --shadow: 0 4px 18px rgba(16, 24, 40, .12);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #0f151c; --panel: #17202a; --ink: #e5eaf0; --muted: #94a1ae; --line: #2a3644; --hover: #1f2a36; --shadow: 0 4px 18px rgba(0,0,0,.45); }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 14px/1.4 var(--font); color: var(--ink); background: var(--bg); display: flex; flex-direction: column; overflow: hidden; }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.muted { color: var(--muted); } .small { font-size: 12px; } .tiny { font-size: 11px; }

/* ----- topbar ----- */
.topbar { height: 52px; flex: none; display: flex; align-items: center; gap: 12px; padding: 0 12px; background: var(--top); color: var(--top-ink); padding-top: env(safe-area-inset-top); }
.brand { display: flex; align-items: center; gap: 8px; background: none; border: 0; color: inherit; cursor: pointer; font-size: 16px; white-space: nowrap; padding: 4px; }
.brand b { color: #ff4d5e; letter-spacing: .5px; }
.proj-title { display: flex; align-items: center; gap: 6px; min-width: 0; }
.proj-title input { background: transparent; border: 1px solid transparent; border-radius: 6px; color: inherit; padding: 4px 8px; width: 200px; font-weight: 600; }
.proj-title input:hover, .proj-title input:focus { border-color: #3a4a5c; outline: none; background: #1d2a39; }
.toolbar { display: flex; gap: 8px; align-items: center; overflow-x: auto; scrollbar-width: none; }
.toolbar .group { display: flex; gap: 2px; padding-left: 8px; border-left: 1px solid #2c3a4a; }
.toolbar button, .toolbar .sel select { background: transparent; border: 0; color: var(--top-ink); border-radius: 6px; padding: 6px 8px; cursor: pointer; display: flex; align-items: center; gap: 5px; white-space: nowrap; font-size: 13px; }
.toolbar .sel select { background: #1f2c3b; border: 1px solid #2c3a4a; }
.toolbar .sel select option { background: #1f2c3b; }
.toolbar button:hover { background: #24313f; }
.toolbar button.active { background: var(--accent); color: #fff; }
.toolbar button.toggle.active { background: #2a3a4c; color: #fff; box-shadow: inset 0 -2px 0 var(--accent); }
.spacer { flex: 1; }
.icon-btn { background: transparent; border: 0; color: inherit; cursor: pointer; font-size: 18px; padding: 6px 10px; border-radius: 6px; }
.icon-btn:hover { background: rgba(127,127,127,.18); }
.icon-btn.small { font-size: 13px; padding: 2px 6px; }
body:not(.editing) .ed-only { display: none !important; }
body:not(.mode-plan) .plan-only { display: none !important; }
body:not(.mode-map) .map-only { display: none !important; }

/* ----- buttons ----- */
.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 8px 14px; cursor: pointer; font-weight: 500; }
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: #dc2626; }
.btn.sm { padding: 4px 10px; font-size: 12px; }

/* ----- home ----- */
#home { flex: 1; overflow: auto; }
.home-inner { max-width: 1180px; margin: 0 auto; padding: 28px 16px 60px; }
.hero { display: flex; gap: 20px; align-items: flex-end; justify-content: space-between; flex-wrap: wrap; margin-bottom: 22px; }
.hero h1 { margin: 0 0 6px; font-size: 26px; }
.hero p { margin: 0; max-width: 620px; }
.hero-actions { display: flex; gap: 8px; }
.proj-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 16px; }
.proj-card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; cursor: pointer; transition: box-shadow .15s, transform .15s; display: flex; flex-direction: column; }
.proj-card:hover { box-shadow: var(--shadow); transform: translateY(-2px); }
.proj-card .thumb { height: 130px; background: #dde3ea center/cover no-repeat; display: grid; place-items: center; font-size: 40px; color: #8a96a3; }
.proj-card .info { padding: 10px 12px; }
.proj-card .info b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.proj-card .actions { display: flex; gap: 4px; padding: 0 8px 8px; }
.proj-card .actions button { flex: 1; }
.empty { grid-column: 1/-1; text-align: center; padding: 60px 20px; border: 2px dashed var(--line); border-radius: 12px; }

/* ----- editor ----- */
#editor { flex: 1; display: flex; min-height: 0; position: relative; }
.panel { background: var(--panel); border-right: 1px solid var(--line); width: 270px; flex: none; display: flex; flex-direction: column; min-height: 0; }
#right { border-right: 0; border-left: 1px solid var(--line); width: 300px; overflow: auto; }
.tabs { display: flex; border-bottom: 1px solid var(--line); flex: none; }
.tabs button { flex: 1; border: 0; background: none; padding: 11px; cursor: pointer; font-weight: 600; color: var(--muted); border-bottom: 2px solid transparent; }
.tabs button.active { color: var(--ink); border-color: var(--accent); }
.tab-body { padding: 12px; overflow: auto; flex: 1; }
#center { flex: 1; position: relative; min-width: 0; }
#map { position: absolute; inset: 0; background: #e9edf1; }
.mode-plan #map { background: #c9d0d8; }
@media (prefers-color-scheme: dark) { .mode-plan #map { background: #0b1016; } }

.cat-card { display: flex; gap: 10px; align-items: center; border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; margin-bottom: 8px; cursor: grab; background: var(--panel); user-select: none; }
.cat-card { touch-action: pan-y; }
.cat-search { width: 100%; padding: 7px 10px; border: 1px solid var(--line); border-radius: 8px; margin-bottom: 8px; background: var(--panel); color: inherit; font: inherit; }
.cat-group { margin-bottom: 6px; }
.cat-group > summary { cursor: pointer; font-weight: 600; font-size: 12px; padding: 6px 2px; list-style: none; display: flex; align-items: center; gap: 6px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
.cat-group > summary::before { content: '▸'; transition: transform .15s; }
.cat-group[open] > summary::before { transform: rotate(90deg); }
.cat-group > summary span { margin-left: auto; font-weight: 500; }
.cat-card .ico { font-size: 10px; }
.cat-card em.disc { font-style: normal; font-weight: 500; font-size: 10px; color: #b45309; margin-left: 4px; }
.drag-ghost { position: fixed; left: 0; top: 0; z-index: 6000; pointer-events: none; display: flex; align-items: center; gap: 6px; opacity: .6; transition: opacity .1s; }
.drag-ghost.ok { opacity: 1; }
.drag-ghost span { background: rgba(22,32,44,.9); color: #fff; font-size: 11px; padding: 2px 6px; border-radius: 4px; white-space: nowrap; }
.cat-card:hover { border-color: #b8c2cc; background: var(--hover); }
.cat-card.armed { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(227,5,27,.18); }
.cat-card .ico { width: 38px; height: 38px; border-radius: 8px; display: grid; place-items: center; flex: none; color: #fff; font-weight: 700; font-size: 11px; }
.cat-card b { display: block; font-size: 13px; }
.cat-card small { color: var(--muted); display: block; font-size: 11px; }

.layer-sec h4 { margin: 12px 0 6px; font-size: 11px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); }
.layer-item { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 6px; cursor: pointer; }
.layer-item:hover { background: var(--hover); }
.layer-item.sel { background: rgba(37,99,235,.12); }
.layer-item .dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.layer-item span { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.layer-item em { font-style: normal; font-size: 11px; color: var(--muted); }

/* ----- props ----- */
#props { padding: 14px; }
#props h3 { margin: 0 0 2px; font-size: 16px; }
#props .sub { color: var(--muted); font-size: 12px; margin-bottom: 12px; }
.field { margin-bottom: 11px; }
.field > label, .field > .lbl { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); margin-bottom: 3px; }
.field > label output { color: var(--ink); font-weight: 600; }
.field input[type=text], .field input[type=number], .field select, .field textarea, dialog input:not([type=checkbox]):not([type=radio]), dialog select { width: 100%; padding: 7px 9px; border: 1px solid var(--line); border-radius: 7px; background: var(--bg); }
.field input[type=range] { width: 100%; accent-color: var(--accent); }
.field .inline { display: flex; gap: 6px; }
.seg { display: flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { flex: 1; border: 0; background: var(--bg); padding: 7px 3px; white-space: nowrap; cursor: pointer; font-size: 12px; }
.seg button.on { background: var(--accent); color: #fff; }
.kv { display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; font-size: 12px; background: var(--bg); border-radius: 8px; padding: 10px; margin: 8px 0 12px; }
.kv span:nth-child(odd) { color: var(--muted); }
.kv span:nth-child(even) { font-weight: 600; text-align: right; }
.sec-title { font-size: 11px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); margin: 16px 0 8px; border-top: 1px solid var(--line); padding-top: 12px; }
.warn { background: #fff7e6; color: #8a5300; border: 1px solid #fde1a6; border-radius: 8px; padding: 8px 10px; font-size: 12px; margin: 6px 0; }
@media (prefers-color-scheme: dark) { .warn { background: #2b2210; color: #f4c26b; border-color: #5a4417; } }
.btn-row { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }

/* ----- overlays on map ----- */
.floating { position: absolute; z-index: 800; }
#mapTools { top: 10px; left: 50%; transform: translateX(-50%); display: flex; gap: 6px; }
#mapTools form { display: flex; background: var(--panel); border-radius: 8px; box-shadow: var(--shadow); overflow: hidden; }
#mapTools input { border: 0; padding: 8px 10px; width: 280px; background: transparent; outline: none; }
#mapTools form button { border: 0; background: transparent; padding: 0 10px; cursor: pointer; }
#mapTools select { border: 0; border-radius: 8px; padding: 8px; box-shadow: var(--shadow); background: var(--panel); }
.hint { top: 58px; left: 50%; transform: translateX(-50%); background: rgba(22,32,44,.92); color: #fff; padding: 8px 14px; border-radius: 20px; font-size: 13px; pointer-events: none; max-width: 92%; width: max-content; text-align: center; }
.mode-plan .hint { top: 12px; }
.hint.interactive { pointer-events: auto; border-radius: 14px; }
.hint-row { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 4px; margin-top: 6px; }
.hint-row > span { margin-right: 4px; }
.hint-row label { display: inline-flex; align-items: center; gap: 4px; margin-right: 4px; }
.hint-row input { width: 64px; padding: 3px 6px; border-radius: 6px; border: 0; font: inherit; color: #111; background: #fff; }
.hint-row button { border: 1px solid rgba(255,255,255,.35); background: transparent; color: #fff; border-radius: 12px; padding: 2px 9px; font: inherit; font-size: 12px; cursor: pointer; }
.hint-row button.on { background: var(--accent); border-color: var(--accent); }
.legend { left: 10px; bottom: 34px; background: var(--panel); border-radius: 10px; box-shadow: var(--shadow); padding: 10px 12px; font-size: 12px; max-width: 280px; }
.legend b { display: block; margin-bottom: 4px; }
.legend div { display: flex; align-items: center; gap: 6px; margin: 2px 0; }
.legend i { width: 14px; height: 10px; border-radius: 2px; display: inline-block; opacity: .8; }
.status { left: 0; right: 0; bottom: 0; height: 24px; background: rgba(22,32,44,.82); color: #d6dee7; font-size: 11px; display: flex; align-items: center; gap: 16px; padding: 0 10px; pointer-events: none; }
#sideview { right: 10px; bottom: 34px; width: min(560px, calc(100% - 20px)); background: var(--panel); border-radius: 10px; box-shadow: var(--shadow); overflow: hidden; }
.sv-head { display: flex; justify-content: space-between; align-items: center; padding: 6px 10px; border-bottom: 1px solid var(--line); font-size: 12px; }
#svCanvas { display: block; width: 100%; height: 200px; }
.fab { display: none; width: 42px; height: 42px; border-radius: 50%; border: 0; background: var(--panel); box-shadow: var(--shadow); font-size: 18px; cursor: pointer; }
.fab-left { left: 10px; top: 10px; } .fab-right { right: 10px; top: 10px; }

/* ----- leaflet markers ----- */
.sensor-icon { width: 30px; height: 30px; position: relative; }
.sensor-icon .body { position: absolute; inset: 5px; border-radius: 6px; background: var(--c); border: 2px solid #fff; box-shadow: 0 1px 5px rgba(0,0,0,.45); }
.sensor-icon .arrow { position: absolute; left: 50%; top: -6px; width: 0; height: 0; margin-left: -6px; border: 6px solid transparent; border-bottom: 9px solid var(--c); border-top: 0; filter: drop-shadow(0 0 1px #fff); }
.sensor-icon .v { position: absolute; inset: 0; display: grid; place-items: center; color: #fff; font: 700 9px var(--font); }
.sensor-icon.sel .body { outline: 3px solid var(--sel); outline-offset: 1px; }
.rot-handle { width: 16px; height: 16px; border-radius: 50%; background: #fff; border: 3px solid var(--sel); box-shadow: 0 1px 4px rgba(0,0,0,.4); cursor: grab; }
.mid-handle { width: 12px; height: 12px; border-radius: 50%; background: #fff; border: 2px solid var(--sel); box-shadow: 0 1px 3px rgba(0,0,0,.4); cursor: grab; }
.mid-handle:hover { transform: scale(1.3); }
.vtx-handle { width: 12px; height: 12px; background: #fff; border: 2px solid var(--sel); border-radius: 2px; cursor: move; }
.leaflet-tooltip.lbl { background: rgba(22,32,44,.85); color: #fff; border: 0; box-shadow: none; font-size: 11px; padding: 2px 6px; }
.leaflet-tooltip.lbl::before { display: none; }
.leaflet-tooltip.meas { background: #fff; color: #111; font-weight: 700; border: 1px solid #333; }
.leaflet-container { font-family: var(--font); }
.leaflet-container.tool-draw { cursor: crosshair; }

/* ----- dialogs ----- */
dialog { border: 0; border-radius: 14px; padding: 22px; width: min(520px, calc(100% - 24px)); background: var(--panel); color: var(--ink); box-shadow: 0 20px 60px rgba(0,0,0,.35); }
dialog.wide { width: min(1000px, calc(100% - 24px)); max-height: calc(100% - 40px); }
dialog::backdrop { background: rgba(10,16,24,.55); }
dialog h2 { margin: 0 0 14px; font-size: 19px; }
dialog label { display: block; margin-bottom: 12px; font-size: 13px; }
dialog label.row { display: flex; gap: 8px; align-items: center; }
dialog menu { display: flex; justify-content: flex-end; gap: 8px; padding: 0; margin: 18px 0 0; }
.choice { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.card-radio { margin: 0 !important; cursor: pointer; }
.card-radio input { position: absolute; opacity: 0; }
.card-radio span { display: block; border: 2px solid var(--line); border-radius: 10px; padding: 12px; height: 100%; }
.card-radio small { display: block; color: var(--muted); margin-top: 4px; font-size: 12px; }
.card-radio input:checked + span { border-color: var(--accent); background: rgba(227,5,27,.05); }
.report-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
.report-head h2 { margin: 0; }
.report-head div { display: flex; gap: 6px; }
#reportBody table { width: 100%; border-collapse: collapse; font-size: 12px; margin-bottom: 18px; }
#reportBody th, #reportBody td { border-bottom: 1px solid var(--line); padding: 6px 8px; text-align: left; }
#reportBody th { background: var(--bg); font-weight: 600; }
#reportBody h3 { margin: 18px 0 8px; font-size: 15px; }
#reportBody .snap { display: block; max-width: 100%; max-height: 62vh; margin: 0 auto 8px; border: 1px solid var(--line); border-radius: 8px; }
@media print { #reportBody .snap { max-height: none; } }
.toast { position: fixed; left: 50%; bottom: 40px; transform: translateX(-50%); background: #16202c; color: #fff; padding: 10px 18px; border-radius: 8px; z-index: 5000; box-shadow: var(--shadow); }

/* ----- responsive ----- */
@media (max-width: 1100px) {
  .toolbar button span { display: none; }
  .proj-title input { width: 130px; }
}
@media (max-width: 820px) {
  .brand span { display: none; }
  .proj-title { display: none; }
  .panel { position: absolute; top: 0; bottom: 0; z-index: 1000; box-shadow: var(--shadow); transition: transform .2s; }
  #left { left: 0; transform: translateX(-105%); }
  #right { right: 0; transform: translateX(105%); width: min(320px, 90%); }
  #left.open, #right.open { transform: none; }
  .fab { display: block; }
  #mapTools { top: 60px; width: calc(100% - 20px); }
  #mapTools input { width: 100%; }
  #mapTools form { flex: 1; }
  .hint { top: 110px; white-space: normal; text-align: center; max-width: 90%; }
  .choice { grid-template-columns: 1fr; }
  #sideview { left: 10px; right: 10px; width: auto; }
}

/* ----- stampa ----- */
@media print {
  body { overflow: visible; display: block; background: #fff; color: #000; }
  body > *:not(#dlgReport) { display: none !important; }
  #dlgReport { position: static; display: block; box-shadow: none; width: 100%; max-height: none; padding: 0; }
  .no-print { display: none !important; }
}

/* vista 3D */
#view3d { inset: 0; z-index: 1200; display: flex; flex-direction: column; background: #dfe7ef; }
.v3-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; padding: 8px 10px; background: var(--panel); border-bottom: 1px solid var(--line); }
.v3-bar .spacer { flex: 1; }
.v3-bar .toggle.active { background: var(--accent); color: #fff; border-color: var(--accent); }
#v3Host { position: relative; flex: 1; min-height: 0; overflow: hidden; touch-action: none; }
#v3Host canvas { display: block; }
.v3-help { position: absolute; left: 10px; bottom: 8px; background: rgba(255,255,255,.8); color: #334155; padding: 3px 8px; border-radius: 6px; pointer-events: none; }
#v3Legend { position: absolute; left: auto; right: 10px; bottom: 10px; }
#v3Legend:empty { display: none; }
