* {
    box-sizing: border-box;
    font-family: 'Arial', 'Helvetica', sans-serif;
    margin: 0;
    padding: 0;
}

body {
    max-width: 1400px;
    margin: 0 auto;
    padding: 15px;
    background-color: #ffffff;
    color: #000000;
    font-size: 13px;
}

.container {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.header {
    text-align: center;
    padding: 15px;
    background: #f0f0f0;
    border-radius: 5px;
    border: 2px solid #000000;
    margin-bottom: 10px;
}

.input-section, .results-section {
    background: white;
    padding: 20px;
    border-radius: 5px;
    border: 1px solid #cccccc;
    margin-bottom: 15px;
}

h1 { font-size: 20px; margin-bottom: 5px; }
h2 { font-size: 18px; margin-bottom: 10px; color: #000000; }
h3 { font-size: 16px; margin-bottom: 8px; color: #000000; }

.piece-form {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 10px;
    margin-bottom: 15px;
    padding: 15px;
    background: #f8f8f8;
    border-radius: 5px;
}

.form-group {
    display: flex;
    flex-direction: column;
    position: relative;
}

label {
    margin-bottom: 5px;
    font-weight: bold;
    font-size: 12px;
    color: #000000;
}

input[type="number"], select, input[type="text"] {
    padding: 8px;
    border: 1px solid #999999;
    border-radius: 3px;
    font-size: 13px;
    height: 35px;
}

.custom-angle-container {
    display: none;
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px dashed #ccc;
    animation: fadeIn 0.3s ease;
}

.custom-angle-container label {
    font-size: 11px;
    margin-bottom: 4px;
    display: block;
    color: #555;
}

.custom-angle-container input {
    width: 100%;
    background-color: #fff8e1;
    border: 1px solid #ffb300;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(-5px); }
    to { opacity: 1; transform: translateY(0); }
}

.buttons {
    display: flex;
    gap: 10px;
    margin-top: 15px;
    flex-wrap: wrap;
}

button {
    padding: 10px 15px;
    border: 1px solid #000000;
    border-radius: 3px;
    cursor: pointer;
    font-size: 13px;
    font-weight: bold;
    transition: all 0.2s;
    min-height: 35px;
}

.btn-primary { background: #4a90e2; color: white; }
.btn-secondary { background: #2ecc71; color: white; }
.btn-print { background: #f39c12; color: white; }
.btn-danger { background: #e74c3c; color: white; }
.btn-info { background: #3498db; color: white; }

.pieces-list {
    margin-top: 20px;
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 12px;
}

th, td {
    padding: 8px;
    text-align: center;
    border: 1px solid #999999;
}

th {
    background-color: #333333;
    color: white;
    font-weight: bold;
}

.pieces-list table td {
    padding: 6px 4px;
}

.pieces-list table button {
    padding: 4px 8px;
    border: none;
    border-radius: 3px;
    cursor: pointer;
    font-size: 11px;
    font-weight: normal;
    transition: all 0.2s;
    margin: 2px;
    min-width: 60px;
}

.pieces-list table button:hover {
    opacity: 0.9;
    transform: translateY(-1px);
}

.btn-edit { background: #3498db; color: white; }
.btn-remove { background: #e74c3c; color: white; }

.pieces-summary {
    background: #f8f9fa;
    padding: 20px;
    border: 2px solid #000000;
    border-radius: 5px;
    margin-bottom: 20px;
    page-break-inside: avoid;
}

.pieces-summary h3 {
    border-bottom: 2px solid #000000;
    padding-bottom: 8px;
    margin-bottom: 15px;
}

.summary-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    border: 2px solid #000000;
    font-size: 12px;
}

.summary-table thead {
    display: table-header-group;
}

.summary-table tbody {
    display: table-row-group;
}

.summary-table th {
    background-color: #2c3e50;
    color: white;
    padding: 10px 6px;
    font-size: 12px;
    text-align: center;
    border: 1px solid #000000;
    font-weight: bold;
    position: relative;
}

.summary-table th:not(:last-child)::after,
.summary-table td:not(:last-child)::after {
    content: '';
    position: absolute;
    top: 0;
    right: -1px;
    height: 100%;
    width: 1px;
    background-color: #000000;
}

.summary-table th {
    position: relative;
}

.summary-table td {
    padding: 8px 6px;
    border: 1px solid #000000;
    text-align: center;
    font-size: 11px;
    position: relative;
}

.summary-table tr:nth-child(even) {
    background-color: #f2f2f2;
}

.total-row {
    background-color: #2c3e50;
    color: white;
    font-weight: bold;
}

.total-row td {
    border: 1px solid #000000;
}

body[data-display-mode="normal"] .bar-plan {
    margin-bottom: 20px;
    padding: 15px;
}

body[data-display-mode="normal"] .bar-drawing-container {
    height: 100px;
}

body[data-display-mode="normal"] .cut-piece {
    height: 60px;
}

body[data-display-mode="normal"] .piece-length {
    font-size: 12px;
}

body[data-display-mode="normal"] .piece-angles {
    font-size: 10px;
}

body[data-display-mode="normal"] .cut-legend {
    display: flex;
}

body[data-display-mode="normal"] .cut-sequence {
    display: block;
}

body[data-display-mode="compact"] .bar-plan {
    margin-bottom: 15px;
    padding: 12px;
}

body[data-display-mode="compact"] .bar-drawing-container {
    height: 80px;
}

body[data-display-mode="compact"] .cut-piece {
    height: 50px;
    top: 5px;
}

body[data-display-mode="compact"] .piece-length {
    font-size: 10px;
}

body[data-display-mode="compact"] .piece-angles {
    font-size: 8px;
}

body[data-display-mode="compact"] .cut-legend {
    font-size: 10px;
    padding: 8px;
}

body[data-display-mode="ultra-compact"] .bar-plan {
    margin-bottom: 10px;
    padding: 8px;
}

body[data-display-mode="ultra-compact"] .bar-drawing-container {
    height: 60px;
}

body[data-display-mode="ultra-compact"] .cut-piece {
    height: 40px;
    top: 0;
}

body[data-display-mode="ultra-compact"] .piece-length {
    font-size: 9px;
}

body[data-display-mode="ultra-compact"] .piece-angles {
    display: none;
}

body[data-display-mode="ultra-compact"] .cut-legend {
    display: none;
}

body[data-display-mode="ultra-compact"] .cut-sequence {
    display: none;
}

.cutting-plan {
    margin-top: 20px;
}

.bar-plan {
    background: white;
    padding: 15px;
    border: 2px solid #000000;
    margin-bottom: 20px;
}

.bar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
    padding-bottom: 10px;
    border-bottom: 2px solid #000000;
    flex-wrap: wrap;
}

.bar-title {
    font-size: 16px;
    font-weight: bold;
    color: #000000;
}

.bar-stats {
    display: flex;
    gap: 10px;
    font-size: 12px;
}

.stat {
    padding: 5px 10px;
    background: #f0f0f0;
    border-radius: 3px;
    font-weight: bold;
    border: 1px solid #999999;
}

.waste-stat {
    background: #ffeaea;
    color: #c0392b;
}

.technical-drawing {
    margin: 15px 0;
    padding: 15px;
    background: white;
    border: 1px solid #cccccc;
    position: relative;
}

.bar-drawing-container {
    position: relative;
    height: 100px;
    margin: 10px 0;
    min-height: 100px;
}

.main-bar {
    position: absolute;
    top: 40px;
    left: 0;
    right: 0;
    height: 20px;
    background: #cccccc;
    border: 1px solid #000000;
}

.cut-piece {
    position: absolute;
    top: 10px;
    height: 60px;
    background: #e3f2fd;
    border: 2px solid #1976d2;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 2;
    overflow: hidden;
}

.piece-info {
    text-align: center;
    padding: 3px;
    font-size: 11px;
    font-weight: bold;
    color: #000000;
    width: 100%;
    word-break: break-word;
    line-height: 1.2;
}

.piece-length {
    font-size: 12px;
    font-weight: bold;
    margin-bottom: 2px;
    color: #000000;
    background: rgba(255, 255, 255, 0.9);
    padding: 2px 4px;
    border-radius: 2px;
}

.piece-angles {
    font-size: 10px;
    color: #000000;
    background: rgba(255, 255, 255, 0.9);
    padding: 1px 3px;
    border-radius: 2px;
    margin-top: 1px;
}

.angle-indicator {
    position: absolute;
    width: 15px;
    height: 15px;
    background: #1976d2;
    clip-path: polygon(0 0, 100% 50%, 0 100%);
    z-index: 3;
}

.angle-left {
    left: -2px;
    top: 50%;
    transform: translateY(-50%);
}

.angle-right {
    right: -2px;
    top: 50%;
    transform: translateY(-50%) rotate(180deg);
}

.metric-scale {
    display: flex;
    justify-content: space-between;
    position: relative;
    height: 30px;
    margin-top: 10px;
    border-top: 2px solid #000000;
}

.scale-mark {
    position: absolute;
    top: -10px;
    width: 1px;
    height: 10px;
    background: #000000;
}

.scale-number {
    position: absolute;
    top: -25px;
    font-size: 10px;
    font-weight: bold;
    color: #000000;
    transform: translateX(-50%);
}

.waste-piece {
    background: #ffebee;
    border: 2px dashed #d32f2f;
}

.waste-piece .piece-length {
    color: #c62828;
}

.cut-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 15px;
    padding: 10px;
    background: #f8f8f8;
    border: 1px solid #cccccc;
    font-size: 11px;
    page-break-inside: avoid;
    break-inside: avoid;
}

.legend-item {
    flex: 1 1 auto;
    min-width: 120px;
    max-width: 200px;
    padding: 6px;
    background: white;
    border-radius: 3px;
    border: 1px solid #cccccc;
    page-break-inside: avoid;
    break-inside: avoid;
}

.legend-title {
    font-weight: bold;
    font-size: 11px;
    margin-bottom: 3px;
    color: #000000;
}

.legend-details {
    font-size: 10px;
    color: #666666;
    line-height: 1.3;
}

.cut-sequence {
    grid-column: 1 / -1;
    margin-top: 15px;
    padding: 12px;
    background: #f8f9fa;
    border: 1px solid #1976d2;
    border-radius: 5px;
    font-size: 11px;
    column-count: 1;
    column-gap: 20px;
    break-inside: avoid;
}

.cut-sequence.many-steps {
    column-count: 2;
}

.cut-sequence.very-many-steps {
    column-count: 3;
}

.cut-sequence h4 {
    column-span: all;
    margin-bottom: 10px;
    font-size: 12px;
    color: #000;
    border-bottom: 2px solid #1976d2;
    padding-bottom: 6px;
    text-align: center;
    font-weight: bold;
}

.cut-step {
    display: flex;
    align-items: flex-start;
    padding: 5px 0;
    border-bottom: 1px dashed #ddd;
    font-size: 10px;
    break-inside: avoid;
    page-break-inside: avoid;
    min-height: 24px;
}

.cut-step:last-child {
    border-bottom: none;
}

.cut-step.inizio {
    background-color: #e8f5e9;
}

.cut-step.inizio_pezzo {
    background-color: #e3f2fd;
}

.cut-step.taglio {
    background-color: #fff3e0;
}

.cut-step.scarto {
    background-color: #ffebee;
}

.cut-step.fine {
    background-color: #f3e5f5;
}

.cut-marker {
    display: inline-block;
    width: 18px;
    height: 18px;
    background: #1976d2;
    color: white;
    border-radius: 50%;
    text-align: center;
    line-height: 18px;
    font-weight: bold;
    margin-right: 10px;
    font-size: 9px;
    flex-shrink: 0;
    margin-top: 2px;
}

.cut-position {
    min-width: 60px;
    font-weight: bold;
    color: #000;
    margin-right: 10px;
    flex-shrink: 0;
    font-size: 10px;
    line-height: 1.4;
    padding-top: 2px;
}

.cut-description {
    color: #333;
    flex-grow: 1;
    font-size: 9.5px;
    line-height: 1.4;
    word-break: break-word;
    overflow-wrap: break-word;
}

.summary {
    background: #333333;
    color: white;
    padding: 15px;
    border-radius: 5px;
    margin-bottom: 15px;
    border: 2px solid #000000;
}

.summary h3 {
    margin-bottom: 10px;
    color: white;
    font-size: 14px;
}

.summary-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 10px;
}

.summary-item {
    padding: 8px;
    background: rgba(255,255,255,0.1);
    border-radius: 3px;
}

.summary-label {
    font-size: 11px;
    opacity: 0.9;
    margin-bottom: 3px;
}

.summary-value {
    font-size: 16px;
    font-weight: bold;
}

.savings { color: #4caf50; }
.waste { color: #f44336; }

.compact-controls {
    display: flex;
    gap: 10px;
    margin-bottom: 10px;
    padding: 10px;
    background: #f0f0f0;
    border-radius: 5px;
    flex-wrap: wrap;
}

.compact-toggle {
    padding: 8px 12px;
    background: #666;
    color: white;
    border: none;
    border-radius: 3px;
    cursor: pointer;
    font-size: 11px;
}

.compact-toggle.active {
    background: #4a90e2;
}

.print-controls {
    display: flex;
    gap: 10px;
    margin-bottom: 10px;
    padding: 10px;
    background: #e8f4fc;
    border-radius: 5px;
    border: 1px solid #3498db;
    flex-wrap: wrap;
}

@media print {
    @page {
        margin: 0.5cm;
        size: A4 landscape;
    }

    body {
        padding: 10px;
        margin: 0 auto;
        background: white;
        font-size: 12px;
        color-adjust: exact;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
        color: black;
        max-width: 100%;
    }

    .container {
        gap: 0;
        width: 100%;
        display: block;
    }

    .input-section,
    .buttons,
    .pieces-list,
    .header,
    .compact-controls,
    .print-controls,
    button,
    .compact-toggle,
    input,
    select,
    form,
    label,
    h1,
    .header h1,
    .print-controls + h2,
    .custom-angle-container {
        display: none;
    }

    .results-section {
        padding: 10px;
        border: 1px solid #cccccc;
        border-radius: 3px;
        background: white;
        margin: 0;
        width: 100%;
        display: block;
    }

    body.printing-list .cutting-plan,
    body.printing-list .bar-plan,
    body.printing-list .technical-drawing,
    body.printing-list .cut-sequence,
    body.printing-list .cut-legend {
        display: none !important;
    }

    body.printing-list .pieces-summary,
    body.printing-list .summary {
        display: block !important;
        page-break-inside: avoid;
    }

    body:not(.printing-list) .pieces-summary,
    body:not(.printing-list) .summary {
        page-break-inside: avoid !important;
    }

    body:not(.printing-list) .summary {
        page-break-after: avoid !important;
        margin-bottom: 0 !important;
        padding-bottom: 5px !important;
    }

    body:not(.printing-list) .cutting-plan {
        page-break-before: always !important;
    }

    body:not(.printing-list) .bar-plan {
        page-break-inside: avoid !important;
        break-inside: avoid !important;
    }

    body:not(.printing-list) .bar-plan + .bar-plan {
        page-break-before: always !important;
        break-before: page !important;
    }

    .pieces-summary {
        display: block !important;
        margin-bottom: 10px;
        padding: 10px;
        border: 1px solid #cccccc;
        border-radius: 3px;
        background: #f8f9fa;
        color: black;
        page-break-inside: avoid;
    }

    .pieces-summary h3 {
        font-size: 16px;
        margin-bottom: 10px;
        color: black;
        border-bottom: 1px solid #000000;
        padding-bottom: 5px;
    }

    .summary-table {
        width: 100%;
        border-collapse: collapse;
        border: 1px solid #000000;
        font-size: 10px;
        page-break-inside: avoid;
    }

    .summary-table th {
        background-color: #333333;
        color: white;
        padding: 8px 4px;
        font-size: 10px;
        text-align: center;
        border: 1px solid #000000;
        font-weight: bold;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    .summary-table td {
        padding: 6px 4px;
        border: 1px solid #000000;
        text-align: center;
        vertical-align: middle;
        font-size: 9px;
    }

    .summary-table tr:nth-child(even) {
        background-color: #f2f2f2;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    .summary-table .total-row {
        background-color: #2c3e50;
        color: white;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    .summary-table .total-row td {
        border: 1px solid #000000;
        font-weight: bold;
        font-size: 10px;
    }

    .summary {
        display: block !important;
        padding: 10px;
        margin-bottom: 10px;
        background: #333333;
        color: white;
        border: 1px solid #000000;
        border-radius: 3px;
        page-break-inside: avoid;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    .summary h3 {
        color: white;
        font-size: 14px;
        margin-bottom: 8px;
    }

    .summary-item {
        background: rgba(255,255,255,0.1);
        padding: 6px;
        border-radius: 2px;
    }

    .summary-label {
        font-size: 10px;
        opacity: 0.9;
        margin-bottom: 2px;
    }

    .summary-value {
        font-size: 14px;
        font-weight: bold;
    }

    .summary div[style*="font-size: 10px"] {
        font-size: 9px !important;
        color: #ffcc00 !important;
        margin-top: 5px;
        font-style: italic;
        text-align: center;
    }

    body.printing-list .cutting-plan {
        display: none !important;
    }

    body:not(.printing-list) .cutting-plan {
        display: block !important;
        margin-top: 15px;
    }

    .bar-plan {
        padding: 10px;
        margin-bottom: 15px;
        border: 1px solid #000000;
        background: white;
        page-break-inside: avoid;
        break-inside: avoid;
    }

    .bar-header {
        border-bottom: 1px solid #000000;
        padding-bottom: 8px;
        margin-bottom: 8px;
    }

    .bar-title {
        color: black;
        font-size: 14px;
        font-weight: bold;
    }

    .bar-stats {
        font-size: 10px;
        display: flex;
        gap: 8px;
    }

    .stat {
        border: 1px solid #999999;
        background: #f0f0f0;
        color: black;
        font-size: 10px;
        padding: 4px 8px;
        border-radius: 2px;
        font-weight: bold;
    }

    .waste-stat {
        background: #ffeaea;
        color: #c0392b;
    }

    .technical-drawing {
        border: 1px solid #cccccc;
        background: white;
        padding: 10px;
        margin: 10px 0;
        page-break-inside: avoid;
    }

    .bar-drawing-container {
        height: 80px;
        margin: 8px 0;
    }

    .main-bar {
        top: 30px;
        height: 15px;
    }

    .cut-piece {
        top: 5px;
        height: 50px;
        border: 1px solid #1976d2;
    }

    .piece-info {
        padding: 2px;
        font-size: 9px;
    }

    .piece-length {
        font-size: 9px;
        margin-bottom: 1px;
        padding: 1px 3px;
    }

    .piece-angles {
        font-size: 8px;
        padding: 0 2px;
        margin-top: 0;
    }

    .metric-scale {
        height: 25px;
        margin-top: 8px;
        border-top: 1px solid #000000;
    }

    .scale-mark {
        top: -8px;
        height: 8px;
    }

    .scale-number {
        top: -20px;
        font-size: 8px;
    }

    .cut-legend {
        display: flex !important;
        flex-wrap: wrap;
        gap: 4px;
        margin-top: 8px;
        padding: 6px;
        font-size: 9px;
        page-break-inside: avoid;
    }

    .legend-item {
        min-width: 100px;
        max-width: 150px;
        padding: 4px;
        font-size: 9px;
    }

    .legend-title {
        font-size: 9px;
        margin-bottom: 2px;
    }

    .legend-details {
        font-size: 8px;
        line-height: 1.2;
    }

    .cut-sequence {
        margin-top: 10px;
        padding: 8px;
        font-size: 8px;
        column-gap: 10px;
        page-break-inside: avoid;
        break-inside: avoid;
        width: 100%;
        overflow: visible;
    }

    .cut-sequence[data-columns="1"] {
        column-count: 1;
    }

    .cut-sequence[data-columns="2"] {
        column-count: 2;
        column-gap: 8px;
    }

    .cut-sequence[data-columns="3"] {
        column-count: 3;
        column-gap: 6px;
        font-size: 7.5px;
    }

    .cut-sequence[data-columns="4"] {
        column-count: 4;
        column-gap: 4px;
        font-size: 7px;
    }

    .cut-sequence.many-steps {
        column-count: 3 !important;
        column-gap: 6px;
        font-size: 7.5px;
    }

    .cut-sequence.very-many-steps {
        column-count: 4 !important;
        column-gap: 4px;
        font-size: 7px;
    }

    .cut-sequence h4 {
        column-span: all;
        margin-bottom: 8px;
        font-size: 9px;
        color: #000;
        border-bottom: 1px solid #1976d2;
        padding-bottom: 4px;
        text-align: center;
        font-weight: bold;
        page-break-after: avoid;
    }

    .cut-step {
        padding: 3px 0;
        font-size: 7.5px;
        break-inside: avoid;
        page-break-inside: avoid;
        min-height: auto;
        line-height: 1.2;
    }

    .cut-marker {
        width: 12px;
        height: 12px;
        line-height: 12px;
        font-size: 6px;
        margin-right: 4px;
    }

    .cut-position {
        min-width: 40px;
        width: 40px;
        font-size: 7px;
        margin-right: 6px;
    }

    .cut-description {
        font-size: 7px;
        line-height: 1.2;
    }

    .bar-plan.has-many-pieces .cut-piece {
        height: 40px;
    }

    .bar-plan.has-many-pieces .bar-drawing-container {
        height: 60px;
    }

    .bar-plan.has-many-pieces .cut-sequence {
        font-size: 7px;
    }

    .bar-plan.has-very-many-pieces .cut-piece {
        height: 30px;
    }

    .bar-plan.has-very-many-pieces .bar-drawing-container {
        height: 50px;
    }

    .bar-plan.has-very-many-pieces .cut-sequence {
        column-count: 4 !important;
        font-size: 6.5px;
        padding: 4px;
    }

    .bar-plan.has-very-many-pieces .cut-step {
        font-size: 6.5px;
        padding: 2px 0;
    }

    .savings { color: #4caf50; }
    .waste { color: #f44336; }

    .angle-indicator {
        background: #1976d2;
    }

    .scale-mark {
        background: #000000;
    }

    .cut-step,
    .legend-item,
    .summary-item {
        page-break-inside: avoid;
        break-inside: avoid;
    }
}

@media screen and (min-width: 1200px) {
    .cut-sequence.many-steps {
        column-count: 2;
    }

    .cut-sequence.very-many-steps {
        column-count: 3;
    }
}

@media screen and (max-width: 768px) {
    .piece-form {
        grid-template-columns: 1fr;
    }

    .buttons, .compact-controls, .print-controls {
        flex-direction: column;
    }

    button, .compact-toggle {
        width: 100%;
    }

    .bar-stats {
        flex-direction: column;
        gap: 5px;
    }

    .summary-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    .pieces-list table button {
        padding: 3px 6px;
        font-size: 10px;
        min-width: 50px;
    }

    .custom-angle-container {
        margin-top: 5px;
        padding-top: 5px;
    }

    .cut-sequence {
        padding: 8px;
        font-size: 9px;
        column-count: 1;
    }

    .cut-step {
        font-size: 8px;
    }

    .cut-marker {
        width: 16px;
        height: 16px;
        line-height: 16px;
        font-size: 8px;
        margin-right: 8px;
    }
}
