* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    line-height: 1.6;
    color: #2d3748;
    background: linear-gradient(135deg, #38a169 0%, #68d391 100%);
    min-height: 100vh;
    padding: 20px;
}

.container {
    max-width: 1400px;
    margin: 0 auto;
    background: #f7fafc;
    border-radius: 15px;
    box-shadow: 0 20px 40px rgba(45, 55, 72, 0.15);
    overflow: hidden;
}

.header {
    background: linear-gradient(135deg, #2d3748 0%, #4a5568 100%);
    color: #f7fafc;
    padding: 25px 30px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.header h1 { font-size: 2rem; font-weight: 700; }
.header p { font-size: 0.95rem; opacity: 0.9; }

.header-actions { display: flex; gap: 10px; }

.btn-icon {
    background: rgba(255,255,255,0.15);
    border: 1px solid rgba(255,255,255,0.25);
    color: #fff;
    width: 42px; height: 42px;
    border-radius: 10px;
    cursor: pointer;
    font-size: 20px;
    display: flex; align-items: center; justify-content: center;
    transition: all 0.2s ease;
}
.btn-icon:hover { background: rgba(255,255,255,0.25); transform: translateY(-2px); }

.content { padding: 30px; }

/* ===== UTILITY CALCULATORS ===== */
.utility-section {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    margin-bottom: 30px;
}

.utility-card {
    background: #fff;
    border-radius: 12px;
    padding: 22px;
    border: 1px solid #e2e8f0;
    box-shadow: 0 2px 8px rgba(45,55,72,0.06);
}

.utility-card h3 {
    font-size: 1.1rem; color: #2d3748; margin-bottom: 15px;
    display: flex; align-items: center; gap: 8px;
}

.utility-card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 15px;
}
.utility-card-header h3 { margin-bottom: 0; }

/* Reset button - clean version (IVA) */
.btn-reset-clean {
    background: linear-gradient(135deg, #718096 0%, #4a5568 100%);
    color: #fff;
    border: none;
    padding: 5px 14px;
    border-radius: 6px;
    font-size: 0.78rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    box-shadow: 0 2px 6px rgba(113,128,150,0.25);
}
.btn-reset-clean:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 10px rgba(113,128,150,0.35);
}

/* Reset button - BRUTTO version (Sconto) */
.btn-reset-ugly {
    background: linear-gradient(97deg, #ff6b6b 0%, #ffa502 48%, #ff4757 100%);
    color: #fff;
    border: 3px dashed #2d3748;
    padding: 4px 18px 6px 10px;
    border-radius: 2px 14px 2px 14px;
    font-size: 0.85rem;
    font-weight: 900;
    cursor: pointer;
    transform: rotate(-3deg) skewX(4deg);
    box-shadow: 4px 5px 0px #2d3748;
    letter-spacing: 2px;
    text-transform: uppercase;
    font-family: 'Comic Sans MS', 'Papyrus', cursive;
    transition: all 0.1s;
}
.btn-reset-ugly:hover {
    transform: rotate(2deg) skewX(-3deg) scale(1.1);
    box-shadow: -3px 4px 0px #e53e3e;
    background: linear-gradient(200deg, #ff4757 0%, #ffa502 60%, #ff6b6b 100%);
}

.utility-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

.utility-field { display: flex; flex-direction: column; gap: 4px; }
.utility-field label {
    font-size: 0.8rem; font-weight: 600; color: #4a5568;
    text-transform: uppercase; letter-spacing: 0.3px;
}
.utility-field input, .utility-field select {
    padding: 9px 12px; border: 1px solid #cbd5e0;
    border-radius: 6px; font-size: 14px; transition: border-color 0.2s;
}
.utility-field input:focus, .utility-field select:focus {
    outline: none; border-color: #4299e1;
    box-shadow: 0 0 0 2px rgba(66,153,225,0.15);
}

.utility-result {
    background: #f0fff4; border: 1px solid #c6f6d5;
    border-radius: 8px; padding: 10px 14px; margin-top: 12px;
    font-size: 0.9rem; color: #2d3748; line-height: 1.5;
}
.utility-result strong { color: #38a169; }
.utility-result .highlight { font-size: 1.15rem; font-weight: 700; color: #2f855a; }

.iva-toggle {
    display: flex; background: #edf2f7; border-radius: 8px;
    padding: 3px; margin-bottom: 12px; gap: 3px;
}
.iva-toggle button {
    flex: 1; padding: 7px 10px; border: none; border-radius: 6px;
    font-size: 0.8rem; font-weight: 600; cursor: pointer;
    transition: all 0.2s; background: transparent; color: #718096;
}
.iva-toggle button.active {
    background: #4299e1; color: #fff;
    box-shadow: 0 2px 4px rgba(66,153,225,0.3);
}

/* ===== TABLES ===== */
.tables-container { display: flex; flex-direction: column; gap: 30px; }

.table-block {
    background: #fff; border-radius: 12px;
    border: 1px solid #e2e8f0;
    box-shadow: 0 2px 10px rgba(45,55,72,0.06);
    overflow: hidden;
}

.table-header {
    color: #fff; padding: 14px 20px;
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.table-header-left { display: flex; align-items: center; gap: 10px; flex: 1; }

.table-name-input {
    background: rgba(255,255,255,0.2); border: 1px solid rgba(255,255,255,0.3);
    color: #fff; padding: 6px 12px; border-radius: 6px;
    font-size: 1rem; font-weight: 600; flex: 1; max-width: 350px;
}
.table-name-input::placeholder { color: rgba(255,255,255,0.6); }
.table-name-input:focus { outline: none; background: rgba(255,255,255,0.3); border-color: rgba(255,255,255,0.5); }

.table-header-actions { display: flex; gap: 8px; }
.table-header-actions button {
    background: rgba(255,255,255,0.2); border: 1px solid rgba(255,255,255,0.3);
    color: #fff; padding: 5px 12px; border-radius: 6px;
    font-size: 0.8rem; font-weight: 600; cursor: pointer;
    transition: all 0.2s; white-space: nowrap;
}
.table-header-actions button:hover { background: rgba(255,255,255,0.35); }

.table-body { padding: 15px; }

.table-controls { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.table-controls button {
    padding: 7px 16px; border: none; border-radius: 6px;
    font-size: 0.85rem; font-weight: 600; cursor: pointer; transition: all 0.2s;
}

.table-container { overflow-x: auto; border-radius: 8px; border: 1px solid #e2e8f0; }

.products-table { width: 100%; border-collapse: collapse; background: #fff; }
.products-table th, .products-table td { border: 1px solid #e2e8f0; }
.products-table th {
    padding: 10px 6px; text-align: center; color: #fff;
    font-weight: 600; font-size: 0.78rem;
    position: sticky; top: 0; z-index: 10; white-space: nowrap;
}
.products-table tbody tr { height: 48px; transition: background-color 0.15s ease; }
.products-table tbody tr:nth-child(even) { background: #f8fafc; }
.products-table tbody tr:hover { background: #e6fffa; }

.products-table td { padding: 5px 4px; text-align: center; vertical-align: middle; }

.products-table input {
    width: 100%; padding: 6px 6px; border: 1px solid #cbd5e0;
    border-radius: 4px; font-size: 12px; text-align: center;
    background: #fff; color: #2d3748; transition: border-color 0.2s ease;
}
.products-table input:focus {
    outline: none; border-color: #4299e1;
    box-shadow: 0 0 0 2px rgba(66, 153, 225, 0.15);
}
.products-table input.editable-margin { border-color: #805ad5; background: #faf5ff; }
.products-table input.editable-margin:focus {
    border-color: #805ad5; box-shadow: 0 0 0 2px rgba(128,90,213,0.15);
}

.products-table .result-cell { font-weight: 600; color: #2d3748; font-size: 12px; }
.products-table .profit-positive { color: #38a169; font-weight: 700; }
.products-table .profit-negative { color: #e53e3e; font-weight: 700; }

.summary-row { font-weight: bold; border-top: 3px solid; }
.summary-row td { padding: 14px 10px; color: #fff; font-size: 0.85rem; font-weight: 700; }

/* Light theme - green chiaro, summary azzurro chiaro */
.table-theme-light .table-header { background: linear-gradient(135deg, #48bb78 0%, #38a169 100%); }
.table-theme-light .products-table th { background: #38a169; }
.table-theme-light .summary-row {
    background: linear-gradient(135deg, #90cdf4 0%, #63b3ed 100%) !important;
    border-top-color: #4299e1;
}
.table-theme-light .summary-row td { color: #1a365d; }
.table-theme-light tbody tr.has-data { background: #f0fff4; border-left: 3px solid #38a169; }
.table-theme-light tbody tr.has-data:hover { background: #e6fffa; }
.table-theme-light .row-number { background: #38a169; }

/* Dark theme - green scuro, summary azzurro scuro */
.table-theme-dark .table-header { background: linear-gradient(135deg, #276749 0%, #22543d 100%); }
.table-theme-dark .products-table th { background: #276749; }
.table-theme-dark .summary-row {
    background: linear-gradient(135deg, #2c5282 0%, #2a4365 100%) !important;
    border-top-color: #1a365d;
}
.table-theme-dark .summary-row td { color: #ebf8ff; }
.table-theme-dark tbody tr.has-data { background: #f0fff4; border-left: 3px solid #276749; }
.table-theme-dark tbody tr.has-data:hover { background: #e6fffa; }
.table-theme-dark .row-number { background: #276749; }

.row-number {
    color: #fff; width: 22px; height: 22px; border-radius: 50%;
    display: inline-flex; align-items: center; justify-content: center;
    font-weight: 600; font-size: 10px;
}

.btn-danger {
    background: #e53e3e; color: #fff; padding: 3px 5px;
    font-size: 10px; border-radius: 3px; border: none;
    cursor: pointer; transition: background-color 0.2s;
}
.btn-danger:hover { background: #c53030; }

.btn-copy, .btn-copy-cost {
    padding: 2px 5px; font-size: 9px; border-radius: 3px;
    border: none; cursor: pointer; transition: background-color 0.2s;
    margin: 1px; min-width: 18px; color: #fff;
}
.btn-copy { background: #68d391; }
.btn-copy:hover { background: #38a169; }
.btn-copy-cost { background: #4299e1; }
.btn-copy-cost:hover { background: #3182ce; }

.action-buttons { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.copy-buttons { display: flex; gap: 2px; margin-bottom: 2px; }

/* Add table button */
.add-table-section { text-align: center; margin-top: 20px; }
.btn-add-table {
    background: linear-gradient(135deg, #38a169 0%, #2f855a 100%);
    color: #fff; padding: 14px 30px;
    border: 2px dashed rgba(255,255,255,0.5); border-radius: 12px;
    font-size: 1rem; font-weight: 600; cursor: pointer;
    transition: all 0.3s; box-shadow: 0 4px 15px rgba(56,161,105,0.25);
}
.btn-add-table:hover {
    transform: translateY(-3px);
    box-shadow: 0 6px 20px rgba(56,161,105,0.35);
    border-color: rgba(255,255,255,0.8);
}

/* Formula section */
.formula-section {
    background: #f7fafc; border-radius: 10px;
    padding: 15px 20px; margin-top: 25px; border: 1px solid #e2e8f0;
}
.formula-section h3 {
    color: #718096; margin-bottom: 10px; font-size: 0.9rem;
    cursor: pointer; display: flex; align-items: center; gap: 8px;
}
.formula-section h3:hover { color: #4a5568; }
.formula-content { display: none; }
.formula-content.show { display: block; }
.formula {
    background: #fff; padding: 8px 12px; border-radius: 6px;
    margin: 6px 0; border: 1px solid #e2e8f0;
    font-family: 'Courier New', monospace; font-size: 0.8rem; color: #4a5568;
}

/* Buttons */
.btn {
    padding: 8px 18px; border: none; border-radius: 8px;
    font-size: 14px; font-weight: 600; cursor: pointer; transition: all 0.2s;
}
.btn-success {
    background: linear-gradient(135deg, #38a169 0%, #2f855a 100%);
    color: #fff; box-shadow: 0 2px 8px rgba(56,161,105,0.25);
}
.btn-success:hover { transform: translateY(-2px); box-shadow: 0 4px 12px rgba(56,161,105,0.35); }
.btn-secondary {
    background: linear-gradient(135deg, #718096 0%, #4a5568 100%);
    color: #fff; box-shadow: 0 2px 8px rgba(113,128,150,0.25);
}
.btn-secondary:hover { transform: translateY(-2px); }

/* Modals */
.modal-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(45,55,72,0.7); display: none;
    justify-content: center; align-items: center;
    z-index: 1000; animation: fadeIn 0.2s ease;
}
.modal-overlay.show { display: flex; }

.modal-box {
    background: #f7fafc; border-radius: 16px; padding: 30px;
    max-width: 600px; width: 92%; max-height: 85vh; overflow-y: auto;
    position: relative; box-shadow: 0 20px 40px rgba(45,55,72,0.3);
    animation: popIn 0.3s ease;
}
.modal-close {
    position: absolute; top: 12px; right: 15px;
    background: #718096; color: #fff; border: none;
    width: 32px; height: 32px; border-radius: 50%;
    font-size: 16px; cursor: pointer; transition: all 0.2s;
}
.modal-close:hover { background: #4a5568; transform: scale(1.1); }
.modal-title { font-size: 1.5rem; font-weight: 700; color: #2d3748; margin-bottom: 20px; }

.info-item { padding: 10px 0; border-bottom: 1px solid #e2e8f0; font-size: 0.9rem; color: #4a5568; }
.info-item:last-child { border-bottom: none; }
.info-item strong { color: #2d3748; }

/* Config modal */
.config-section { margin-bottom: 25px; }
.config-section h3 {
    color: #2d3748; font-size: 1.15rem; margin-bottom: 12px;
    display: flex; align-items: center; gap: 8px;
}
.save-config-form {
    background: #e6fffa; border: 2px solid #68d391;
    border-radius: 12px; padding: 18px; margin-bottom: 20px;
}
.save-config-form input, .save-config-form textarea {
    width: 100%; padding: 10px 12px; border: 1px solid #cbd5e0;
    border-radius: 8px; font-size: 14px; margin-bottom: 12px; font-family: inherit;
}
.save-config-form textarea { resize: vertical; min-height: 60px; }
.save-config-form input:focus, .save-config-form textarea:focus {
    outline: none; border-color: #4299e1;
    box-shadow: 0 0 0 2px rgba(66,153,225,0.15);
}

.config-list { background: #fff; border: 1px solid #e2e8f0; border-radius: 12px; overflow: hidden; }
.config-item { padding: 16px; border-bottom: 1px solid #e2e8f0; transition: background 0.2s; }
.config-item:last-child { border-bottom: none; }
.config-item:hover { background: #f8fafc; }
.config-item-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 8px; }
.config-item-name { font-size: 1.1rem; font-weight: bold; color: #2d3748; margin: 0; }
.config-item-date { font-size: 0.8rem; color: #718096; }
.config-item-description { color: #4a5568; font-size: 0.85rem; margin-bottom: 10px; }
.config-item-stats { display: flex; gap: 15px; margin-bottom: 10px; font-size: 0.8rem; color: #718096; }
.config-item-actions { display: flex; gap: 8px; }

.btn-small {
    padding: 5px 10px; font-size: 0.8rem; border-radius: 5px;
    border: none; cursor: pointer; transition: all 0.2s; color: #fff;
}
.btn-load { background: linear-gradient(135deg, #4299e1, #3182ce); }
.btn-load:hover { transform: translateY(-1px); }
.btn-delete { background: linear-gradient(135deg, #e53e3e, #c53030); }
.btn-delete:hover { transform: translateY(-1px); }

.empty-configs { text-align: center; padding: 30px; color: #718096; }

.current-config-indicator {
    display: inline-block; background: #38a169; color: white;
    padding: 3px 7px; border-radius: 4px; font-size: 0.7rem; margin-bottom: 6px;
}

/* Notifications */
.notification {
    position: fixed; top: 20px; right: 20px;
    padding: 12px 18px; border-radius: 8px; color: white;
    font-weight: 600; z-index: 2000; opacity: 0;
    transform: translateX(100%); transition: all 0.3s ease; font-size: 0.9rem;
}
.notification.show { opacity: 1; transform: translateX(0); }
.notification.success { background: linear-gradient(135deg, #38a169, #2f855a); }
.notification.error { background: linear-gradient(135deg, #e53e3e, #c53030); }
.notification.info { background: linear-gradient(135deg, #4299e1, #3182ce); }

/* MS365 Popup */
.ms365-popup .popup-content { text-align: center; }
.popup-emoji { font-size: 3rem; margin-bottom: 15px; }
.popup-title { color: #2d3748; font-size: 1.8rem; font-weight: bold; margin-bottom: 12px; }
.popup-message { color: #4a5568; font-size: 1.1rem; line-height: 1.5; }
.popup-image {
    width: 130px; height: 130px; margin: 0 auto 15px;
    border-radius: 15px; border: 3px solid #68d391; background: #e6fffa;
    display: flex; align-items: center; justify-content: center;
    font-size: 50px; overflow: hidden;
}

.btn-danger-table {
    background: rgba(229,62,62,0.15); border: 1px solid rgba(229,62,62,0.3); color: #e53e3e;
}
.btn-danger-table:hover { background: rgba(229,62,62,0.25); }

/* Hide spinners on table number inputs */
.products-table input[type="number"]::-webkit-inner-spin-button,
.products-table input[type="number"]::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
}
.products-table input[type="number"] {
    -moz-appearance: textfield;
    cursor: text;
}
.products-table input[type="number"]:hover {
    border-color: #4299e1;
}
.products-table input[type="number"]::placeholder {
    font-size: 10px;
}

/* ===== CALCULATOR POPUP ===== */
.calc-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(45,55,72,0.5); z-index: 1500;
    display: flex; justify-content: center; align-items: center;
    animation: fadeIn 0.15s ease;
}

.calc-wrapper {
    display: flex;
    gap: 0;
    animation: popIn 0.2s ease;
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 20px 50px rgba(0,0,0,0.4);
    user-select: none;
}

.calc-popup {
    background: #1a202c;
    width: 300px;
    display: flex;
    flex-direction: column;
}

.calc-target-label {
    background: #2d3748;
    color: #a0aec0;
    padding: 8px 16px;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.calc-target-label span {
    color: #68d391;
    font-size: 0.8rem;
}

.calc-display {
    background: #2d3748;
    padding: 12px 16px;
    text-align: right;
}
.calc-expression {
    color: #a0aec0;
    font-size: 0.85rem;
    min-height: 20px;
    word-break: break-all;
}
.calc-result {
    color: #fff;
    font-size: 2rem;
    font-weight: 700;
    font-family: 'Segoe UI', monospace;
    min-height: 40px;
    word-break: break-all;
}

.calc-buttons {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1px;
    background: #2d3748;
    padding: 1px;
}

.calc-btn {
    background: #4a5568;
    border: none;
    color: #fff;
    padding: 16px 0;
    font-size: 1.1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.1s;
    font-family: 'Segoe UI', sans-serif;
}
.calc-btn:hover { background: #5a6778; }
.calc-btn:active { background: #2d3748; }

.calc-btn.calc-op { background: #4299e1; color: #fff; }
.calc-btn.calc-op:hover { background: #3182ce; }

.calc-btn.calc-clear { background: #e53e3e; color: #fff; }
.calc-btn.calc-clear:hover { background: #c53030; }

.calc-btn.calc-equals { background: #38a169; color: #fff; }
.calc-btn.calc-equals:hover { background: #2f855a; }

.calc-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1px;
    background: #2d3748;
    padding: 1px;
    padding-top: 0;
}

.calc-action-btn {
    padding: 14px 0;
    border: none;
    font-size: 0.9rem;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.15s;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.calc-action-exit { background: #718096; color: #fff; }
.calc-action-exit:hover { background: #5a6778; }
.calc-action-insert { background: linear-gradient(135deg, #38a169 0%, #2f855a 100%); color: #fff; }
.calc-action-insert:hover { background: linear-gradient(135deg, #48bb78 0%, #38a169 100%); }

/* History side panel */
.calc-history-panel {
    background: #1a202c;
    width: 220px;
    display: flex;
    flex-direction: column;
    border-left: 1px solid #2d3748;
}

.calc-history-header {
    background: #2d3748;
    padding: 8px 14px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 0.75rem;
    font-weight: 600;
    color: #a0aec0;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.calc-history-clear {
    background: rgba(229,62,62,0.2);
    color: #fc8181;
    border: 1px solid rgba(229,62,62,0.3);
    padding: 3px 10px;
    border-radius: 4px;
    font-size: 0.68rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s;
    text-transform: uppercase;
}
.calc-history-clear:hover {
    background: rgba(229,62,62,0.4);
    color: #fff;
}

.calc-history-list {
    flex: 1;
    overflow-y: auto;
    background: #232b3a;
}

.calc-history-empty {
    padding: 30px 14px;
    text-align: center;
    color: #4a5568;
    font-size: 0.78rem;
    font-style: italic;
}

.calc-history-item {
    padding: 8px 14px;
    font-size: 0.75rem;
    color: #718096;
    border-bottom: 1px solid rgba(255,255,255,0.05);
    cursor: pointer;
    transition: background 0.15s;
}
.calc-history-item:hover {
    background: #2d3748;
    color: #a0aec0;
}
.calc-history-item .calc-hist-expr {
    display: block;
    color: #718096;
    font-size: 0.7rem;
    margin-bottom: 2px;
}
.calc-history-item .calc-hist-result {
    display: block;
    color: #68d391;
    font-weight: 700;
    font-size: 0.9rem;
}

.calc-history-list::-webkit-scrollbar { width: 4px; }
.calc-history-list::-webkit-scrollbar-track { background: #232b3a; }
.calc-history-list::-webkit-scrollbar-thumb { background: #4a5568; border-radius: 2px; }

@media (max-width: 560px) {
    .calc-wrapper { flex-direction: column; }
    .calc-history-panel { width: 300px; max-height: 150px; border-left: none; border-top: 1px solid #2d3748; }
}

/* Animations */
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes popIn {
    from { opacity: 0; transform: scale(0.9) translateY(-20px); }
    to { opacity: 1; transform: scale(1) translateY(0); }
}

/* Responsive */
@media (max-width: 768px) {
    .header { flex-direction: column; gap: 10px; text-align: center; }
    .header h1 { font-size: 1.5rem; }
    .content { padding: 15px; }
    .utility-section { grid-template-columns: 1fr; }
    .utility-grid { grid-template-columns: 1fr; }
    .table-header { flex-direction: column; }
    .table-name-input { max-width: 100%; }
}
