/* ===== LAYOUT ===== */
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; line-height: 1.6; color: #333; background: #f0f2f5; }

.app-layout { display: flex; min-height: 100vh; }

/* Sidebar */
.sidebar { width: 220px; min-width: 220px; background: linear-gradient(180deg, #1a1a2e 0%, #16213e 60%, #0f3460 100%); color: white; display: flex; flex-direction: column; position: fixed; height: 100vh; overflow-y: auto; z-index: 100; }
.sidebar-logo { display: flex; align-items: center; gap: 10px; padding: 1.25rem 1.5rem; font-size: 1.3rem; font-weight: 700; border-bottom: 1px solid rgba(255,255,255,0.1); letter-spacing: 0.5px; }
.sidebar-nav { flex: 1; padding: 0.75rem 0; }
.sidebar-nav-item { display: flex; align-items: center; gap: 10px; padding: 0.7rem 1.5rem; color: rgba(255,255,255,0.7); text-decoration: none; transition: all 0.2s; font-size: 0.9rem; border-left: 3px solid transparent; }
.sidebar-nav-item:hover, .sidebar-nav-item.active { background: rgba(255,255,255,0.08); color: white; border-left-color: #667eea; }
.sidebar-nav-item.active { background: rgba(102,126,234,0.15); }
.nav-icon { font-size: 1rem; width: 20px; text-align: center; }
.sidebar-divider { height: 1px; background: rgba(255,255,255,0.1); margin: 0.5rem 1rem; }
.sidebar-section-label { padding: 0.4rem 1.5rem; font-size: 0.7rem; text-transform: uppercase; letter-spacing: 1.5px; color: rgba(255,255,255,0.35); }

/* Main content */
.main-content { flex: 1; margin-left: 220px; min-height: 100vh; display: flex; flex-direction: column; }
.topbar { background: white; padding: 0.875rem 2rem; display: flex; justify-content: space-between; align-items: center; box-shadow: 0 1px 3px rgba(0,0,0,0.08); position: sticky; top: 0; z-index: 50; }
.topbar-title { font-size: 1.15rem; font-weight: 600; color: #333; }
.page-content { flex: 1; padding: 1.5rem 2rem; }
.app-footer { text-align: center; padding: 1rem; color: #999; font-size: 0.8rem; border-top: 1px solid #e8e8e8; background: white; }

/* ===== COMPONENTS ===== */

.content-section {
    background: white;
    padding: 2rem;
    border-radius: 10px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    margin-bottom: 2rem;
}

.form-group {
    margin-bottom: 1.5rem;
}

.form-group label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 600;
    color: #555;
}

.form-control {
    width: 100%;
    padding: 0.75rem;
    border: 2px solid #ddd;
    border-radius: 8px;
    font-size: 1rem;
    transition: border-color 0.3s ease;
}

.form-control:focus {
    outline: none;
    border-color: #667eea;
}

.btn {
    padding: 0.75rem 2rem;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 1rem;
    font-weight: 600;
    transition: all 0.3s ease;
    text-decoration: none;
    display: inline-block;
}

.btn-primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.btn-primary:hover {
    transform: translateY(-2px);
}

.btn-secondary {
    background: #6c757d;
    color: white;
}

.btn-secondary:hover {
    background: #545b62;
}

.btn-small {
    padding: 0.25rem 0.5rem;
    font-size: 0.8rem;
}

.table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 1rem;
}

.table th, .table td {
    padding: 0.75rem;
    text-align: left;
    border-bottom: 1px solid #ddd;
}

.table th {
    background-color: #667eea;
    color: white;
    font-weight: 600;
}

.table tbody tr:hover {
    background-color: #f8f9fa;
}

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

.table tbody tr:nth-child(even):hover {
    background-color: #f0f0f0;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.stat-card {
    background: white;
    padding: 1.5rem;
    border-radius: 10px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    border-left: 4px solid #667eea;
    transition: transform 0.3s ease;
}

.stat-card:hover {
    transform: translateY(-2px);
}

.stat-number {
    font-size: 2rem;
    font-weight: bold;
    color: #667eea;
    margin-bottom: 0.5rem;
}

.stat-label {
    color: #666;
    font-size: 0.9rem;
}

.message {
    padding: 1rem;
    border-radius: 8px;
    margin-bottom: 1.5rem;
    font-weight: 500;
    opacity: 1;
    transition: opacity 0.3s ease;
}

.message.success {
    background-color: #d4edda;
    color: #155724;
    border: 1px solid #c3e6cb;
}

.message.error {
    background-color: #f8d7da;
    color: #721c24;
    border: 1px solid #f5c6cb;
}

.message.warning {
    background-color: #fff3cd;
    color: #856404;
    border: 1px solid #ffeeba;
}

.message.info {
    background-color: #d1ecf1;
    color: #0c5460;
    border: 1px solid #bee5eb;
}

.radio-group {
    display: flex;
    gap: 1rem;
    margin-top: 0.5rem;
}

.radio-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.badge {
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    font-size: 0.8rem;
    font-weight: 600;
}

.badge-success {
    background-color: #28a745;
    color: white;
}

.badge-danger {
    background-color: #dc3545;
    color: white;
}

.badge-warning {
    background-color: #ffc107;
    color: #212529;
}

.badge-info {
    background-color: #17a2b8;
    color: white;
}

.loading {
    display: inline-block;
    width: 20px;
    height: 20px;
    border: 3px solid rgba(102, 126, 234, 0.3);
    border-radius: 50%;
    border-top-color: #667eea;
    animation: spin 1s ease-in-out infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.pagination {
    display: flex;
    justify-content: center;
    list-style: none;
    margin: 2rem 0;
}

.pagination li {
    margin: 0 0.25rem;
}

.pagination a {
    display: block;
    padding: 0.5rem 0.75rem;
    color: #667eea;
    text-decoration: none;
    border: 1px solid #dee2e6;
    border-radius: 4px;
    transition: all 0.3s ease;
}

.pagination a:hover, .pagination .active a {
    background-color: #667eea;
    color: white;
}

.chart-container {
    position: relative;
    height: 400px;
    margin: 2rem 0;
}

.filter-bar {
    display: flex;
    gap: 1rem;
    margin-bottom: 2rem;
    align-items: end;
}

.filter-item {
    display: flex;
    flex-direction: column;
    min-width: 120px;
}

.export-buttons {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.table-responsive {
    overflow-x: auto;
    margin-top: 1rem;
}

.info-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 1rem;
    margin-top: 1rem;
}

.info-grid div {
    background: #f8f9fa;
    padding: 0.75rem;
    border-radius: 6px;
    border-left: 4px solid #667eea;
}

.section {
    padding: 1.5rem 2rem;
    border-bottom: 1px solid #eee;
}

.section:last-child {
    border-bottom: none;
}

.section h3 {
    color: #667eea;
    margin-bottom: 1rem;
    font-size: 1.3em;
    padding-bottom: 0.5rem;
    border-bottom: 2px solid #667eea;
    display: inline-block;
}

.invoice-data {
    background: white;
    border-radius: 10px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    overflow: hidden;
}

.invoice-data h2 {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 1.5rem 2rem;
    margin: 0;
    font-size: 1.8em;
}

.items-table, .summary-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 1rem;
}

.items-table th, .items-table td,
.summary-table th, .summary-table td {
    padding: 0.75rem;
    text-align: left;
    border-bottom: 1px solid #ddd;
}

.items-table th, .summary-table th {
    background-color: #667eea;
    color: white;
    font-weight: 600;
}

.items-table tbody tr:hover,
.summary-table tbody tr:hover {
    background-color: #f8f9fa;
}

.items-table td:nth-child(3),
.items-table td:nth-child(4),
.items-table td:nth-child(5),
.items-table td:nth-child(6) {
    text-align: right;
}

.summary-table td:nth-child(2),
.summary-table td:nth-child(3) {
    text-align: right;
}

.alert {
    padding: 1rem;
    margin-bottom: 1rem;
    border: 1px solid transparent;
    border-radius: 4px;
}

.alert-warning {
    color: #856404;
    background-color: #fff3cd;
    border-color: #ffeaa7;
}

.alert-info {
    color: #0c5460;
    background-color: #d1ecf1;
    border-color: #bee5eb;
}

.breadcrumb {
    background: #f8f9fa;
    padding: 0.75rem 1rem;
    border-radius: 4px;
    margin-bottom: 1rem;
}

.breadcrumb a {
    color: #667eea;
    text-decoration: none;
}

.breadcrumb a:hover {
    text-decoration: underline;
}

/* ===== CONTRACTS ===== */
.badge-secondary { background-color: #6c757d; color: white; }

.hint { font-size: 0.8rem; color: #6b7280; margin-top: 0.25rem; display: block; }
.empty-note { color: #6b7280; font-style: italic; font-size: 0.85rem; }

/* Two-column builder: form on the left, live document on the right */
.contract-builder {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 1.5rem;
    align-items: start;
}

.builder-preview {
    position: sticky;
    top: 4.5rem;
    max-height: calc(100vh - 6rem);
    overflow-y: auto;
    background: #e9e8e6;
    border-radius: 10px;
    padding: 1rem;
}

.preview-label {
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 1.5px;
    color: #6b7280;
    margin-bottom: 0.75rem;
    text-align: center;
}

/* Step chips */
.steps { display: flex; gap: 0.4rem; margin-bottom: 1rem; flex-wrap: wrap; }
.steps .step-btn {
    border: 1px solid #e2e2e2;
    background: #fff;
    padding: 0.4rem 0.9rem;
    border-radius: 20px;
    font-size: 0.78rem;
    cursor: pointer;
    color: #6b7280;
    font-family: inherit;
    transition: all 0.2s ease;
}
.steps .step-btn.active { background: #1f1f1f; color: #fff; border-color: #1f1f1f; }

.nav-btns { display: flex; justify-content: space-between; align-items: center; margin-top: 1rem; gap: 0.5rem; }

/* Package palette */
.pkg-palette { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-top: 0.75rem; }
.pkg-chip {
    border: 1px solid #e2e2e2;
    background: #fff;
    border-radius: 8px;
    padding: 0.5rem 0.75rem;
    cursor: pointer;
    text-align: left;
    font-family: inherit;
    transition: all 0.2s ease;
    max-width: 100%;
}
.pkg-chip:hover { border-color: #667eea; background: #f5f6ff; }
.pkg-chip-label { display: block; font-size: 0.82rem; font-weight: 600; color: #333; }
.pkg-chip-meta { display: block; font-size: 0.72rem; color: #6b7280; margin-top: 0.15rem; }

/* Line item rows */
.line-row {
    border: 1px solid #e2e2e2;
    border-radius: 8px;
    padding: 0.65rem 0.75rem;
    margin-bottom: 0.5rem;
    background: #fff;
}
.line-grid {
    display: grid;
    grid-template-columns: 3fr 0.9fr 1.3fr 0.9fr auto;
    gap: 0.5rem;
    align-items: end;
}
.line-grid label { font-size: 0.7rem; margin: 0 0 0.2rem; color: #6b7280; font-weight: 600; }
.line-grid .form-control { padding: 0.4rem 0.5rem; font-size: 0.85rem; }
.line-actions { padding-bottom: 0.1rem; }
.line-total { text-align: right; font-size: 0.78rem; color: #6b7280; margin-top: 0.35rem; }

.totale-box {
    background: #fdecec;
    border: 1px solid #e6b8b8;
    border-radius: 8px;
    padding: 0.7rem 1rem;
    margin-top: 1rem;
    font-size: 0.9rem;
}
.totale-box b { color: #8b0000; }

/* The generated document itself — mirrors the Word template */
.contract-doc {
    background: #fff;
    max-width: 760px;
    margin: 0 auto;
    padding: 40px 46px;
    box-shadow: 0 1px 6px rgba(0, 0, 0, 0.15);
    font-family: Calibri, 'Segoe UI', Arial, sans-serif;
    font-size: 13.5px;
    line-height: 1.5;
    color: #1f1f1f;
}
.contract-doc .headrow { display: flex; justify-content: space-between; gap: 1rem; margin-bottom: 18px; }
.contract-doc .clc-block { text-align: right; white-space: pre-line; }
.contract-doc .clc-block b { font-size: 16px; }
.contract-doc .clc-block img.logo-img { display: block; margin-left: auto; width: 34px; height: auto; margin-bottom: 2px; }
.contract-doc h3 {
    font-size: 14px;
    color: #1f4e79;
    border-bottom: 1px solid #ddd;
    padding-bottom: 3px;
    margin: 18px 0 8px;
}
.contract-doc p { margin: 0 0 8px; }
.contract-doc table { width: 100%; border-collapse: collapse; margin-top: 6px; font-size: 12.5px; }
.contract-doc th { background: #1f1f1f; color: #fff; padding: 6px 8px; text-align: left; }
.contract-doc td { padding: 6px 8px; border-bottom: 1px solid #ddd; }
.contract-doc tr.tot td { background: #eee; font-weight: bold; }
.contract-doc .sign { display: flex; justify-content: space-between; gap: 1rem; margin-top: 30px; }
.contract-doc .placeholder-missing { color: #a33; font-weight: bold; }

/* Printing from inside the app: keep only the document */
@media print {
    .sidebar, .topbar, .app-footer, .builder-form, .preview-label,
    .filter-bar, .nav-btns, .steps { display: none !important; }
    .main-content { margin-left: 0; }
    .page-content { padding: 0; }
    .contract-builder { display: block; }
    .builder-preview { position: static; max-height: none; overflow: visible; background: #fff; padding: 0; }
    .contract-doc { box-shadow: none; max-width: 100%; padding: 0; }
}

/* ===== RESPONSIVE ===== */
@media (max-width: 1200px) {
    .contract-builder { grid-template-columns: 1fr; }
    .builder-preview { position: static; max-height: none; }
}

@media (max-width: 768px) {
    .sidebar { width: 56px; }
    .sidebar-logo-text, .nav-label, .sidebar-section-label, .sidebar-divider { display: none; }
    .sidebar-logo { justify-content: center; padding: 1rem; }
    .sidebar-nav-item { justify-content: center; padding: 0.75rem; }
    .main-content { margin-left: 56px; }
    .page-content { padding: 1rem; }
    .stats-grid { grid-template-columns: 1fr 1fr; }
    .filter-bar { flex-direction: column; align-items: stretch; }
    .info-grid { grid-template-columns: 1fr; }
    .export-buttons { flex-wrap: wrap; }
    .table-responsive { font-size: 0.9rem; }
}

@media (max-width: 768px) {
    .line-grid { grid-template-columns: 1fr 1fr; }
    .line-grid > div:first-child { grid-column: 1 / -1; }
    .contract-doc { padding: 20px; }
    .contract-doc .headrow { flex-direction: column; }
    .contract-doc .clc-block { text-align: left; }
    .contract-doc .clc-block img.logo-img { margin-left: 0; }
}

@media (max-width: 480px) {
    .stats-grid { grid-template-columns: 1fr; }
    .btn { padding: 0.5rem 1rem; font-size: 0.9rem; }
    .stat-number { font-size: 1.5rem; }
}
