:root {
    --bg: #0f172a;
    --topbar: #111827;
    --panel: #ffffff;
    --muted: #6b7280;
    --line: #e5e7eb;
    --accent: #2563eb;
    --accent-d: #1d4ed8;
    --ok: #16a34a;
    --danger: #dc2626;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
    font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
    color: #1f2937;
    background: #f1f5f9;
}
.app { display: flex; flex-direction: column; height: 100vh; }

/* Topbar */
.topbar {
    display: flex; align-items: center; justify-content: space-between;
    background: var(--topbar); color: #fff; padding: 10px 18px; flex: 0 0 auto;
    flex-wrap: wrap; gap: 8px;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-home { display: flex; align-items: center; gap: 12px; text-decoration: none; color: inherit; cursor: pointer; }
.brand-home:hover .brand-title { text-decoration: underline; }
.logo {
    font-weight: 800; letter-spacing: 1px; background: var(--accent);
    padding: 6px 10px; border-radius: 8px; font-size: 14px;
}
.logo-img { height: 40px; max-width: 180px; object-fit: contain; display: block; background: #fff; border-radius: 6px; padding: 3px 6px; }
.brand-title { font-size: 15px; font-weight: 600; }
.brand-sub { font-size: 12px; color: #cbd5e1; }
.topbar-right { font-size: 12px; color: #cbd5e1; }

/* Layout a due pannelli */
.layout { display: flex; flex: 1 1 auto; min-height: 0; }
.map-pane { position: relative; flex: 1 1 auto; min-width: 0; }
.map { position: absolute; inset: 0; }
.side-pane {
    flex: 0 0 380px; background: #f8fafc; border-left: 1px solid var(--line);
    padding: 14px; overflow-y: auto;
}
/* Pannello CDU a comparsa: nascosto finché non si preme "Crea CDU" */
.app:not(.cdu-open) .side-pane { display: none; }
.app.cdu-open .map-open-btn { display: none; }
.side-head {
    display: flex; align-items: center; justify-content: space-between;
    font-weight: 700; font-size: 15px; margin: -4px 0 12px;
}
.map-open-btn {
    position: absolute; top: 12px; left: 12px; z-index: 6;
    background: var(--accent); color: #fff; border: none; border-radius: 10px;
    padding: 10px 16px; font-size: 14px; font-weight: 600; cursor: pointer;
    box-shadow: 0 4px 14px rgba(37,99,235,.35);
}
.map-open-btn:hover { background: var(--accent-d); }

/* Card */
.card {
    background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
    padding: 14px; margin-bottom: 12px; box-shadow: 0 1px 2px rgba(0,0,0,.04);
}
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.card h2 { font-size: 15px; margin: 0; }
.card h3 { font-size: 13px; margin: 0 0 8px; color: #374151; text-transform: uppercase; letter-spacing: .4px; }
.counter {
    background: var(--accent); color: #fff; border-radius: 999px;
    padding: 2px 10px; font-size: 13px; font-weight: 700; min-width: 28px; text-align: center;
}
.counter.full { background: var(--danger); }

/* Selezione */
.sel-list { display: flex; flex-direction: column; gap: 6px; max-height: 280px; overflow-y: auto; }
.sel-item {
    display: flex; align-items: center; justify-content: space-between;
    background: #f9fafb; border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px;
}
.sel-item b { font-weight: 600; }
.sel-actions { display: inline-flex; gap: 2px; align-items: center; }
.sel-item .zoom, .sel-item .rm, .sel-item .vis {
    background: none; border: none; cursor: pointer; line-height: 1; padding: 2px 5px; border-radius: 6px;
}
.sel-item .zoom { color: var(--accent); font-size: 15px; }
.sel-item .zoom:hover { background: #eff6ff; }
.sel-item .vis { font-size: 14px; }
.sel-item .vis:hover { background: #f1f5f9; }
.sel-item .rm { color: var(--danger); font-size: 16px; }
.sel-item .rm:hover { background: #fef2f2; }

/* Form */
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.input {
    padding: 9px 11px; border: 1px solid #d1d5db; border-radius: 8px; font-size: 14px;
    min-width: 0; flex: 1 1 80px;
}
.input.grow { flex: 1 1 auto; }
.btn {
    padding: 9px 14px; border: 1px solid #d1d5db; border-radius: 8px; background: #fff;
    cursor: pointer; font-size: 14px; color: #1f2937; white-space: nowrap;
}
.btn:hover { background: #f3f4f6; }
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-primary:hover { background: var(--accent-d); }
.btn-ghost { background: transparent; color: var(--danger); border-color: transparent; }
.btn-ghost:hover { background: #fef2f2; }
.btn-block { display: block; width: 100%; margin-bottom: 8px; }
.actions { position: sticky; bottom: 0; }

.muted { color: var(--muted); }
.small { font-size: 12px; }
.note { font-size: 12px; color: var(--muted); margin: 8px 0 0; }
.note a { color: var(--accent); }
.msg { margin-top: 8px; font-size: 13px; border-radius: 8px; padding: 8px 10px; }
.msg.ok { background: #ecfdf5; color: #065f46; }
.msg.err { background: #fef2f2; color: #991b1b; }
.alert { margin-top: 8px; background: #fef3c7; border: 1px solid #fcd34d; color: #92400e; padding: 10px 12px; border-radius: 8px; }

/* Pannelli sulla mappa */
.map-panel {
    position: absolute; top: 12px; right: 12px; z-index: 5;
    background: rgba(255,255,255,.96); border: 1px solid var(--line); border-radius: 10px;
    width: min(280px, calc(100vw - 24px)); box-shadow: 0 4px 14px rgba(0,0,0,.12); overflow: hidden;
}
.panel-head {
    display: flex; align-items: center; justify-content: space-between;
    padding: 8px 12px; font-weight: 600; font-size: 13px; background: #f8fafc; border-bottom: 1px solid var(--line);
}
.mini-btn { background: none; border: none; cursor: pointer; font-size: 14px; }
.layer-list { max-height: 60vh; overflow-y: auto; padding: 6px 10px; }
.layer-row { display: flex; align-items: center; gap: 8px; padding: 4px 0; font-size: 12.5px; }
.layer-row input { accent-color: var(--accent); }
.layer-row label { flex: 1 1 auto; cursor: pointer; }
.layer-swatch { width: 14px; height: 14px; border-radius: 3px; border: 1px solid #94a3b8; flex: 0 0 auto; }
.layer-swatch.cat-ico { display: inline-flex; align-items: center; justify-content: center; font-size: 11px; color: #475569; background: #f1f5f9; }
.layer-row.selectable label { font-weight: 700; }
.cat-toggle { background: none; border: none; cursor: pointer; font-size: 12px; color: var(--muted); flex: 0 0 auto; padding: 0 2px; }
.layer-cats { margin: 0 0 6px 24px; display: flex; flex-direction: column; gap: 3px; }
.layer-cats.hidden { display: none; }
.layer-cat { display: flex; align-items: flex-start; gap: 6px; font-size: 11px; color: #374151; line-height: 1.25; }
.layer-swatch.sm { width: 12px; height: 12px; margin-top: 1px; }
.cat-label { flex: 1 1 auto; word-break: break-word; }
.layer-group { border-radius: 6px; }
.layer-group.dragging { opacity: .5; background: #eef2ff; }

/* gruppi di livelli in legenda (come il pannello Livelli di QGIS) */
.lgrp { border: 1px solid var(--line); border-radius: 8px; margin: 4px 0; background: #fbfdff; }
.lgrp.dragging { opacity: .5; }
.lgrp-head { display: flex; align-items: center; gap: 6px; padding: 5px 8px; background: #eef2f7; border-radius: 8px 8px 0 0; cursor: grab; }
.lgrp.collapsed .lgrp-head { border-radius: 8px; }
.lgrp-head input { accent-color: var(--accent); flex: 0 0 auto; }
.lgrp-name { flex: 1 1 auto; font-weight: 700; font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lgrp-caret { border: none; background: none; cursor: pointer; padding: 0 2px; font-size: 12px; color: var(--muted); flex: 0 0 auto; }
.lgrp-ren, .lgrp-del { border: none; background: transparent; cursor: pointer; opacity: .55; padding: 2px 3px; border-radius: 5px; font-size: 12px; flex: 0 0 auto; }
.lgrp-ren:hover, .lgrp-del:hover { opacity: 1; background: #e2e8f0; }
.lgrp-body { padding: 2px 4px 2px 12px; min-height: 10px; }
.lgrp.collapsed .lgrp-body { display: none; }
.drag-handle { cursor: grab; color: #94a3b8; font-size: 11px; flex: 0 0 auto; user-select: none; letter-spacing: -2px; }
.drag-handle:active { cursor: grabbing; }
.layers-panel.collapsed .layer-list { display: none; }
.layers-panel.collapsed .panel-foot { display: none; }

/* menu contestuale del layer (tasto destro in legenda) */
.layer-menu {
    position: fixed; z-index: 1000; min-width: 210px;
    background: #fff; border: 1px solid var(--line); border-radius: 8px;
    box-shadow: 0 6px 24px rgba(15,23,42,.18); padding: 6px; font-size: 12.5px;
    /* mai più alto della finestra: se il contenuto eccede (es. molte categorie) scorre
       all'interno, senza che i controlli in fondo diventino irraggiungibili */
    max-height: calc(100vh - 16px); overflow-y: auto;
}
.layer-menu-title { font-weight: 700; padding: 4px 8px 6px; color: #0f172a; border-bottom: 1px solid var(--line); margin-bottom: 4px; word-break: break-word; }
.layer-menu-item { display: block; width: 100%; text-align: left; background: none; border: none; cursor: pointer; padding: 7px 8px; border-radius: 6px; font-size: 12.5px; color: #1f2937; }
.layer-menu-item:hover { background: #eef2ff; }
.layer-menu-sep { height: 1px; background: var(--line); margin: 4px 0; }
.layer-menu-slider { padding: 4px 8px 6px; }
.layer-menu-slider label { display: flex; justify-content: space-between; font-size: 11.5px; color: var(--muted); margin-bottom: 4px; }
.layer-menu-slider .lm-val { font-weight: 600; color: #1f2937; }
.layer-menu-slider input[type=range] { width: 100%; accent-color: var(--accent); cursor: pointer; }
.panel-foot { padding: 6px 10px; font-size: 10.5px; color: var(--muted); border-top: 1px solid var(--line); background: #f8fafc; }

.map-hint {
    position: absolute; bottom: 14px; left: 50%; transform: translateX(-50%); z-index: 5;
    background: rgba(17,24,39,.85); color: #fff; padding: 7px 14px; border-radius: 999px;
    font-size: 12.5px; pointer-events: none;
}

@media (max-width: 820px) {
    .layout { flex-direction: column; }
    .side-pane { flex: 1 1 auto; width: 100%; border-left: none; border-top: 1px solid var(--line); }
    /* A pannello CDU chiuso la mappa occupa tutto; quando è aperto si divide lo schermo */
    .map-pane { flex: 1 1 auto; min-height: 240px; }
    .app.cdu-open .map-pane { flex: 0 0 42vh; min-height: 0; }
    .app.cdu-open .side-pane { max-height: 58vh; }
    .layer-list { max-height: 40vh; }
    .info-panel { max-height: 60vh; }
}

@media (max-width: 520px) {
    .topbar { padding: 8px 12px; }
    .brand { gap: 8px; flex: 1 1 auto; min-width: 0; }
    .brand-title { font-size: 13px; }
    .brand-sub { font-size: 11px; }
    .brand-title, .brand-sub { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .logo-img { height: 32px; }
    /* Menu tutti da un lato: le azioni vanno su una riga a sé, allineate a destra, e vanno a capo lì. */
    .topbar-actions { width: 100%; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
    .topbar-admin, .topbar-btn { padding: 6px 9px; font-size: 12px; }
    .side-pane { padding: 12px; }
    .map-hint { width: calc(100vw - 24px); text-align: center; }
    /* Pannelli mappa a comparsa: non più larghi dello schermo, un filo staccati dal bordo. */
    .map-panel, .info-panel { width: calc(100vw - 20px); max-width: 340px; }
}

@media (max-width: 700px) {
    /* Pannello "Livelli": da APERTO occupa al massimo ~60% dell'altezza con elenco scrollabile,
       così non copre la mappa; da CHIUSO diventa una pillola compatta ("Livelli ▸"). */
    .layers-panel { display: flex; flex-direction: column; max-height: 60vh; }
    .layers-panel .layer-list { max-height: none; flex: 1 1 auto; }
    .layers-panel.collapsed { width: auto; }
    .layers-panel.collapsed .panel-head { border-bottom: none; }
}

/* Strumento Info in mappa */
.map-tools { position: absolute; top: 12px; right: 12px; z-index: 5; display: flex; gap: 8px; }
.map-tool-btn {
    background: #fff; border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px;
    font-size: 13px; cursor: pointer; box-shadow: 0 1px 4px rgba(0,0,0,.15);
}
.map-tool-btn:hover { background: #f1f5f9; }
.map-tool-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.map.info-cursor { cursor: help; }

.info-panel {
    position: absolute; top: 56px; right: 12px; width: min(320px, calc(100vw - 24px)); max-height: 70vh; overflow: auto;
    background: #fff; border: 1px solid var(--line); border-radius: 10px; z-index: 6;
    box-shadow: 0 6px 24px rgba(0,0,0,.2);
}
.info-head {
    display: flex; align-items: center; justify-content: space-between;
    padding: 10px 14px; border-bottom: 1px solid var(--line); font-weight: 600;
}
.info-body { padding: 10px 14px; }
.info-layer { margin-bottom: 14px; }
.info-layer-name { font-weight: 700; color: var(--accent-d); margin-bottom: 4px; }
.info-tab { width: 100%; border-collapse: collapse; margin-bottom: 8px; }
.info-tab th, .info-tab td { text-align: left; padding: 3px 6px; font-size: 12.5px; border-bottom: 1px solid #f1f5f9; vertical-align: top; }
.info-tab th { color: #64748b; font-weight: 600; width: 42%; }

/* Azioni nella topbar (Crea CDU, Info, Area interna) */
.topbar-actions { display: flex; align-items: center; gap: 10px; }
.topbar-admin, .topbar-btn {
    color: #cbd5e1; text-decoration: none; font-size: 13px; white-space: nowrap;
    padding: 6px 12px; border: 1px solid #374151; border-radius: 8px;
    background: transparent; cursor: pointer; font-family: inherit;
}
.topbar-admin:hover, .topbar-btn:hover { background: var(--accent); color: #fff; border-color: var(--accent); }
.topbar-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* --- Strumenti mappa: pulsanti operatore, rimozione layer, pannello aggiungi layer --- */
.panel-head-tools { display: flex; align-items: center; gap: 4px; }
.layer-del {
    margin-left: auto; border: none; background: transparent; cursor: pointer;
    font-size: 13px; line-height: 1; padding: 2px 4px; border-radius: 6px; opacity: .55;
}
.layer-del:hover { opacity: 1; background: #fde8e8; }
.layer-group .layer-row { display: flex; align-items: center; gap: 6px; }

.add-layer-panel {
    position: absolute; top: 56px; left: 12px; z-index: 1200; width: 340px; max-width: calc(100% - 24px);
    max-height: 70%; overflow: auto; background: var(--panel); border: 1px solid var(--line);
    border-radius: 10px; box-shadow: 0 10px 30px rgba(0,0,0,.2);
}
.add-layer-body { padding: 8px 10px 12px; }
.add-layer-row {
    display: flex; flex-direction: column; gap: 6px; padding: 8px 6px;
    border-bottom: 1px solid var(--line);
}
.add-layer-row.disabled { opacity: .7; }
.add-layer-name { font-weight: 600; font-size: 13px; }
.add-layer-geo { font-weight: 400; color: var(--muted); font-size: 11px; text-transform: lowercase; }
.add-layer-hint { font-size: 12px; color: var(--muted); }
.add-layer-act { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.add-layer-act select { flex: 1 1 auto; min-width: 0; padding: 4px 6px; border: 1px solid var(--line); border-radius: 6px; }
.add-layer-onestyle { flex: 1 1 auto; font-size: 12px; color: #374151; }
.add-layer-btn { padding: 4px 12px; }

/* color-picker nel menu contestuale del layer (solo layer aggiunti) */
.lm-colors { margin-top: 2px; }
.lm-row { display: flex; align-items: center; gap: 6px; padding: 4px 8px; }
.lm-row select { flex: 1 1 auto; min-width: 0; padding: 3px 4px; border: 1px solid var(--line); border-radius: 6px; font-size: 12px; }
.lm-row .btn { padding: 4px 10px; font-size: 12px; }
.lm-btn { display: block; width: calc(100% - 16px); margin: 4px 8px; padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; background: #f8fafc; cursor: pointer; font-size: 12px; text-align: left; }
.lm-btn:hover { background: #eef2ff; }
.lm-note { padding: 2px 8px 6px; font-size: 11px; color: var(--muted); }
.lm-sub { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin: 4px 2px; }
.lm-cat-colors { max-height: 180px; overflow-y: auto; }
.lm-color-row { display: flex; align-items: center; gap: 8px; padding: 3px 2px; }
.lm-color-lbl { flex: 1 1 auto; font-size: 12px; color: #374151; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lm-color-row input[type="color"] { width: 34px; height: 22px; padding: 0; border: 1px solid var(--line); border-radius: 4px; background: none; cursor: pointer; flex: 0 0 auto; }
.lm-color-row input[type="color"]:disabled { opacity: .35; cursor: not-allowed; }
.lm-color-row input[type="range"] { flex: 0 0 90px; }
.lm-transp { display: inline-flex; align-items: center; gap: 3px; font-size: 11px; color: #6b7280; cursor: pointer; flex: 0 0 auto; white-space: nowrap; }
.lm-transp input[type="checkbox"] { margin: 0; cursor: pointer; }
