:root {
    /* --- PALETTE STILE "DASHBOARD" (Viola/Lilla) --- */
    --primary: #6d28d9;
    --primary-bg: #ede9fe;
    --accent: #8b5cf6;
    --bg-body: #f6f7fb;
    --input-bg: #ffffff;
    --border-color: #ddd6fe;
    --radius-std: 14px;

    /* Griglia */
    --cell-mm: 26;
    /* Bordo guida tratteggiato visibile */
    --border: 1px dashed rgba(0, 0, 0, .15);

    /* LED bollo */
    --led-size: 9mm;
    --ring-size: 2px;

    /* --- VARIABILI FRECCE --- */
    --arrow-color: rgba(255, 255, 255, .95);
    --arrow-thickness: 3px;
    --arrow-head-len: 10px;
    --arrow-head-full-h: 16px;

    /* Etichetta */
    --label-bg: #ffffff;
    --label-radius: 6px;
    --label-size-pct: 52%;
    --tag-size: 22px;

    /* Layout pagina */
    --sidebar-w: 340px;
    --page-gap: 20px;
}

* { box-sizing: border-box; outline: none; }

body {
    font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    margin: 0;
    color: #333;
    background: var(--bg-body);
    height: 100vh;
    overflow: hidden;
}

/* ====== LAYOUT ====== */
.layout {
    display: grid;
    grid-template-columns: 1fr var(--sidebar-w);
    gap: var(--page-gap);
    padding: 20px;
    height: 100vh;
}

/* Colonna anteprima */
.stage {
    background: #fff;
    border-radius: var(--radius-std);
    padding: 20px;
    display: grid;
    grid-template-rows: auto auto 1fr;
    gap: 16px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, .04);
    overflow: hidden;
}

.stage-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.stage-title {
    font-size: 20px;
    font-weight: 700;
    color: #1f2937;
}

.summary {
    padding: 12px 16px;
    border: 1px dashed #cbd5e1;
    border-radius: 12px;
    background: #f8fafc;
    font-size: 14px;
    color: #475569;
    display: flex;
    flex-wrap: wrap;
    gap: 15px;
}

.summary .line { margin: 0; }
.summary strong { color: #1e293b; }

/* Container Anteprima */
.preview-wrap {
    border: 2px solid #e2e8f0;
    border-radius: 12px;
    background: #f1f5f9;
    position: relative;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: grab;
}

.preview-wrap:active { cursor: grabbing; }

.zoom-container {
    transition: transform 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    transform-origin: center center;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 100px 100px 100px 200px;
}

/* ====== GRIGLIA ====== */
.grid {
    display: grid;
    width: fit-content;
    border: none;
    background: transparent;
}

/* STILE CELLA BASE (FANTASMA/GUIDA) */
.cell {
    width: calc(var(--cell-mm) * 1mm);
    height: calc(var(--cell-mm) * 1mm);
    position: relative;

    border-right: var(--border);
    border-bottom: var(--border);

    overflow: visible !important;
    display: flex;
    align-items: center;
    justify-content: center;

    background-color: transparent;
    cursor: pointer;
    transition: all 0.2s;
}

.cell:hover {
    background-color: rgba(0,0,0,0.03);
}

/* --- PULSANTE ELIMINA (X) --- */
.delete-btn {
    position: absolute;
    top: -6px;
    right: -6px;
    width: 20px;
    height: 20px;
    background: #ef4444; /* Rosso */
    color: white;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    font-size: 14px;
    line-height: 1;
    cursor: pointer;
    z-index: 60; /* Sopra a tutto */
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
    opacity: 0; /* Invisibile finché non vai in hover sulla cella */
    transition: opacity 0.2s, transform 0.2s;
}

.cell:hover .delete-btn {
    opacity: 1; /* Appare in hover */
    transform: scale(1.1);
}

.cell.hidden-cell {
    visibility: hidden;
    pointer-events: none;
}

/* STILE CELLA ATTIVA (CABLATA) */
.cell.is-cabled {
    opacity: 1;
    border: 1px solid rgba(0,0,0,0.15);
    box-shadow: 0 4px 6px rgba(0,0,0,0.05);
    z-index: 10;
}

.cell.is-cabled .delete-btn {
    display: none;
}

.cell.last-col { border-right: none; }
.cell.last-row { border-bottom: none; }

/* --- STILE FRECCIA (COMPATIBILE PDF) --- */
.arrow {
    position: absolute;
    top: 50%;
    left: 4px;
    right: 4px;
    height: var(--arrow-head-full-h);
    transform: translateY(-50%);
    /* Z-Index 40 per stare sopra i LED e i bordi, ma sotto le etichette */
    z-index: 40;

    /* Nuovo Layout Flex per allineare Stanghetta + Punta */
    display: flex;
    align-items: center;
    background: transparent;
    pointer-events: none;
}

/* Stanghetta (Rettangolo) */
.arrow::before {
    content: '';
    display: block;
    /* La stanghetta occupa tutto lo spazio tranne la punta */
    flex-grow: 1;
    height: var(--arrow-thickness);
    background-color: var(--arrow-color);
    /* Margine negativo per unire visivamente alla punta */
    margin-right: -1px;
}

/* Punta (Triangolo con bordi CSS) */
.arrow::after {
    content: '';
    display: block;
    width: 0;
    height: 0;
    border-style: solid;
    /* Geometria del triangolo */
    border-width: calc(var(--arrow-head-full-h) / 2) 0 calc(var(--arrow-head-full-h) / 2) var(--arrow-head-len);
    border-color: transparent transparent transparent var(--arrow-color);
}

/* Rotazioni del container (La struttura interna segue la rotazione) */
.arrow.reverse { transform: translateY(-50%) scaleX(-1); }
.arrow-right { transform: translateY(-50%) rotate(0deg); }
.arrow-left  { transform: translateY(-50%) rotate(180deg); }
.arrow-down  { transform: translateY(-50%) rotate(90deg); }
.arrow-up    { transform: translateY(-50%) rotate(-90deg); }

/* LED bollo */
.led {
    width: var(--led-size);
    height: var(--led-size);
    background: #e4e4e7;
    border-radius: 50%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border: var(--ring-size) solid rgba(255, 255, 255, .9);
    z-index: 2;
    opacity: 0.3;
}

.cell.is-cabled .led {
    background: #52525b;
    opacity: 1;
}

/* Cartellino */
.label-box {
    position: absolute;
    top: 50%;
    left: 50%;
    width: var(--label-size-pct);
    height: var(--label-size-pct);
    transform: translate(-50%, -50%);
    background: var(--label-bg);
    border-radius: var(--label-radius);
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    z-index: 50;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    opacity: 0.4;
}

.cell.is-cabled .label-box {
    opacity: 1;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}

.tag {
    font-weight: 800;
    font-size: var(--tag-size);
    letter-spacing: .5px;
    line-height: 1;
    color: #000;
}

/* --- PORT BADGE ESTERNO --- */
.port-badge {
    position: absolute;
    left: -100px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 32px;
    font-family: 'Segoe UI', sans-serif;
    font-weight: 800;
    color: #fff;
    padding: 8px 16px;
    border-radius: 8px;
    border: 2px solid #fff;
    box-shadow: 0 4px 10px rgba(0,0,0,0.2);
    z-index: 20;
    display: flex;
    align-items: center;
    gap: 8px;
    white-space: nowrap;
    justify-content: center;
}

.port-badge::before {
    content: '';
    display: block;
    width: 14px;
    height: 14px;
    background: #fff;
    border-radius: 2px;
}

.port-badge::after {
    content: '';
    position: absolute;
    left: 100%;
    top: 50%;
    transform: translateY(-50%);
    width: 120px;
    height: 6px;
    background-color: inherit;
    z-index: -1;
}

/* ====== SIDEBAR ====== */
.sidebar {
    background: #fff;
    border-radius: var(--radius-std);
    box-shadow: 0 4px 20px rgba(0, 0, 0, .04);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    height: 100%;
}

.sidebar-top {
    padding: 20px;
    display: flex;
    align-items: center;
    gap: 12px;
    border-bottom: 1px solid #f1f5f9;
    flex-shrink: 0;
}

.avatar {
    width: 40px;
    height: 40px;
    border-radius: 12px;
    background: var(--primary-bg);
    color: var(--primary);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 900;
    font-size: 18px;
}

.brand { font-weight: 700; font-size: 16px; color: #1e293b; }
.sub { font-size: 13px; color: #94a3b8; }

.sidebar-body {
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 20px;
    flex-grow: 1;
}

h3 {
    margin: 0 0 12px;
    font-size: 15px;
    font-weight: 700;
    color: var(--primary);
    background: var(--primary-bg);
    padding: 10px 14px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    gap: 10px;
}

.form { display: grid; gap: 12px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field { display: grid; gap: 6px; }

.field label {
    font-size: 12px;
    font-weight: 600;
    color: #64748b;
    margin-left: 2px;
}

.field input,
.field select {
    width: 100%;
    padding: 10px 12px;
    border-radius: 10px;
    border: 1px solid #e2e8f0;
    background: #f8fafc;
    color: #334155;
    font-size: 14px;
    font-weight: 600;
    transition: all 0.2s ease;
    appearance: none;
    background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%236d28d9' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
    background-repeat: no-repeat;
    background-position: right 10px center;
    background-size: 16px;
}

.field input:focus,
.field select:focus {
    background-color: #fff;
    border-color: var(--accent);
    box-shadow: 0 0 0 4px rgba(139, 92, 246, 0.15);
}

input[type=range] {
    -webkit-appearance: none; width: 100%; background: transparent; margin: 10px 0;
}
input[type=range]::-webkit-slider-thumb {
    -webkit-appearance: none; height: 16px; width: 16px; border-radius: 50%;
    background: var(--primary); cursor: pointer; margin-top: -6px;
}
input[type=range]::-webkit-slider-runnable-track {
    width: 100%; height: 4px; cursor: pointer; background: #ddd6fe; border-radius: 2px;
}

.help {
    font-size: 11px;
    color: #94a3b8;
    line-height: 1.4;
    padding: 10px;
    background: #f8fafc;
    border-radius: 8px;
}

.btn {
    cursor: pointer;
    border: none;
    background: var(--primary);
    color: #fff;
    border-radius: 12px;
    padding: 14px;
    font-weight: 700;
    font-size: 14px;
    transition: transform 0.1s, opacity 0.2s;
    width: 100%;
    text-align: center;
}

.btn:hover { opacity: 0.9; }
.btn:active { transform: scale(0.98); }
.btn.secondary { background: var(--primary-bg); color: var(--primary); }

.card { background: transparent; }

@media print {
    body { background: #fff; height: auto; overflow: visible; }
    .layout { display: block; padding: 0; height: auto; }
    .sidebar { display: none; }
    .stage { box-shadow: none; border-radius: 0; padding: 0; display: block; overflow: visible; }
    .stage-header, .btn, .zoom-controls { display: none !important; }
    .preview-wrap { border: none; background: #fff; display: block; overflow: visible; height: auto; padding: 0; }
    .zoom-container { transform: none !important; padding: 0; display: block; width: 100%; }
    .grid { margin: 0 auto; box-shadow: none; border: 1px solid #000; }
    .summary { margin-bottom: 20px; border: 1px solid #ccc; background: #fff; }
    .cell { break-inside: avoid; }
}
