/* --- VARIABILI GLOBALI (Colori & Tema Premium) --- */
:root {
    --primary: #4F46E5;
    --primary-hover: #4338CA;
    --primary-light: #EEF2FF;
    --surface: #FFFFFF;
    --background: #F3F4F6;
    --text-main: #111827;
    --text-muted: #6B7280;
    --border: #E5E7EB;
    
    --expired-bg: #F3F4F6; /* Grigio per scaduti */
    --expired-text: #374151;
    --critical-bg: #FEF2F2;
    --critical-text: #DC2626;
    --processing-bg: #FFFBEB;
    --processing-text: #D97706;
    --upcoming-bg: #ECFDF5;
    --upcoming-text: #059669;

    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
    --glass: rgba(255, 255, 255, 0.9);
    
    --radius-sm: 6px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-xl: 24px;
}

/* --- RESET & TYPOGRAPHY --- */
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
    font-family: 'Inter', sans-serif;
    background-color: var(--background);
    color: var(--text-main);
    line-height: 1.5;
    overflow-x: hidden;
}

/* --- LAYOUT GENERALE --- */
.layout-wrapper {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* --- SIDEBAR --- */
.sidebar {
    width: 320px;
    background-color: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    padding: 24px;
    box-shadow: var(--shadow-sm);
    z-index: 10;
    overflow-y: auto;
}

.app-branding {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 32px;
}
.logo-icon {
    background: var(--primary);
    color: white;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 10px;
    font-size: 1.2rem;
    box-shadow: 0 4px 10px rgba(79, 70, 229, 0.3);
}
.app-branding h1 {
    font-size: 1.2rem;
    font-weight: 800;
    letter-spacing: -0.02em;
}
.app-branding h1 span { color: var(--primary); }
.app-branding .version {
    font-size: 0.7rem;
    color: var(--text-muted);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.sidebar-section {
    margin-bottom: 32px;
}
.sidebar-section h2 {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.1em;
    font-weight: 700;
    margin-bottom: 12px;
}

/* Uploader */
.upload-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    border: 2px dashed var(--border);
    border-radius: var(--radius-md);
    padding: 24px 16px;
    text-align: center;
    cursor: pointer;
    background-color: var(--primary-light);
    transition: all 0.2s ease;
    margin-bottom: 12px;
}
.upload-area:hover {
    border-color: var(--primary);
    background-color: #E0E7FF;
}
.upload-icon { font-size: 1.8rem; margin-bottom: 8px; }
.upload-text { font-size: 0.85rem; font-weight: 600; color: var(--text-main); }
.upload-text span { font-weight: 400; color: var(--text-muted); font-size: 0.75rem; }

/* Buttons */
.btn-secondary {
    width: 100%;
    padding: 10px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-weight: 600;
    font-size: 0.85rem;
    color: var(--text-main);
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 8px;
}
.btn-secondary:hover { background: var(--background); }
.btn-print {
    width: 100%;
    padding: 12px;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: var(--radius-sm);
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(79, 70, 229, 0.3);
    transition: transform 0.1s;
}
.btn-print:active { transform: scale(0.98); }

.btn-demo {
    background: linear-gradient(135deg, #a78bfa, #8b5cf6);
    color: white;
    border: none;
    box-shadow: 0 4px 10px rgba(139, 92, 246, 0.3);
}
.btn-demo:hover {
    background: linear-gradient(135deg, #8b5cf6, #7c3aed);
    color: white;
}

/* Filtri (Segmented Control) */
.filter-group { margin-bottom: 16px; }
.filter-group label {
    display: block;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-main);
    margin-bottom: 6px;
}
.segmented-control {
    display: flex;
    background: var(--background);
    padding: 4px;
    border-radius: var(--radius-sm);
    gap: 4px;
}
.seg-btn {
    flex: 1;
    border: none;
    background: transparent;
    padding: 6px;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-muted);
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.2s;
}
.seg-btn.active {
    background: var(--surface);
    color: var(--primary);
    box-shadow: var(--shadow-sm);
}

/* Toggles */
.toggle-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 0;
    cursor: pointer;
}
.toggle-label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.85rem;
    font-weight: 500;
}
.dot { width: 8px; height: 8px; border-radius: 50%; }
.dot.black { background: var(--expired-text); }
.dot.red { background: var(--critical-text); }
.dot.orange { background: var(--processing-text); }
.dot.green { background: var(--upcoming-text); }
.dot.white { background: white; }

.toggle-row input { display: none; }
.toggle-slider {
    width: 36px;
    height: 20px;
    background: var(--border);
    border-radius: 20px;
    position: relative;
    transition: 0.3s;
}
.toggle-slider::after {
    content: '';
    position: absolute;
    width: 16px;
    height: 16px;
    background: white;
    border-radius: 50%;
    top: 2px;
    left: 2px;
    transition: 0.3s;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}
.toggle-row input:checked + .toggle-slider { background: var(--primary); }
.toggle-row input:checked + .toggle-slider::after { transform: translateX(16px); }

/* --- MAIN CONTENT --- */
.main-content {
    flex: 1;
    overflow-y: auto;
    position: relative;
}

/* Welcome Screen */
.welcome-screen {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(243, 244, 246, 0.8);
    backdrop-filter: blur(10px);
    z-index: 50;
    transition: opacity 0.5s;
}
.welcome-screen.hidden {
    opacity: 0;
    pointer-events: none;
}
.welcome-box {
    background: var(--surface);
    padding: 40px;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-xl);
    text-align: center;
    max-width: 400px;
    border: 1px solid var(--border);
}
.welcome-icon { font-size: 3rem; margin-bottom: 16px; }
.welcome-box h2 { font-weight: 800; margin-bottom: 12px; }
.welcome-box p { color: var(--text-muted); font-size: 0.9rem; margin-bottom: 16px; }
.welcome-box .sub-note { font-size: 0.75rem; color: #9CA3AF; }

/* Dashboard Frame */
.dashboard-frame {
    padding: 32px 40px;
    max-width: 1400px;
    margin: 0 auto;
}
.dashboard-frame.hidden { display: none; }

.top-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
}
.top-bar h2 { font-weight: 800; font-size: 1.5rem; letter-spacing: -0.02em; }
.update-badge {
    background: var(--primary-light);
    color: var(--primary);
    padding: 6px 12px;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 600;
}

/* KPI Grid */
.kpi-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 20px;
    margin-bottom: 32px;
}
.kpi-card {
    background: var(--surface);
    padding: 20px;
    border-radius: var(--radius-lg);
    border: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    box-shadow: var(--shadow-sm);
    transition: transform 0.2s, box-shadow 0.2s;
}
.kpi-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-md); }
.kpi-info h3 { font-size: 0.85rem; text-transform: uppercase; font-weight: 700; margin-bottom: 4px; }
.kpi-info p { font-size: 0.7rem; color: var(--text-muted); }
.kpi-value { font-size: 2.2rem; font-weight: 800; line-height: 1; }

.expired-kpi { border-top: 4px solid var(--expired-text); }
.critical-kpi { border-top: 4px solid var(--critical-text); }
.processing-kpi { border-top: 4px solid var(--processing-text); }
.upcoming-kpi { border-top: 4px solid var(--upcoming-text); }

/* Chart Section */
.chart-section {
    background: var(--surface);
    padding: 24px;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border);
    margin-bottom: 32px;
}
.chart-header h3 { font-size: 1.1rem; font-weight: 700; margin-bottom: 16px; }
.chart-container { height: 260px; position: relative; }

/* Lists Wrapper */
.lists-wrapper {
    display: flex;
    flex-direction: column;
    gap: 32px;
}
.list-section {
    background: var(--surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border);
    overflow: hidden;
}
.list-section.hidden { display: none !important; }

.section-banner {
    padding: 16px 24px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: white;
}
.expired-banner { background: linear-gradient(135deg, #4B5563, #374151); }
.critical-banner { background: linear-gradient(135deg, #EF4444, #B91C1C); }
.processing-banner { background: linear-gradient(135deg, #F59E0B, #B45309); }
.upcoming-banner { background: linear-gradient(135deg, #10B981, #047857); }

.banner-title { font-size: 1rem; font-weight: 700; display: flex; align-items: center; gap: 10px; margin: 0; }
.badge { background: rgba(255,255,255,0.2); padding: 4px 10px; border-radius: 12px; font-size: 0.8rem; font-weight: 700; }

.cards-container {
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

/* Singoli Contratti (Card) */
.contract-card {
    background: var(--background);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 16px;
    transition: background 0.2s, box-shadow 0.2s;
}
.contract-card:hover {
    background: var(--surface);
    box-shadow: var(--shadow-md);
    border-color: #D1D5DB;
}

.card-top {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 12px;
}
.card-title {
    font-size: 0.95rem;
    font-weight: 700;
    color: var(--text-main);
    line-height: 1.3;
}
.capitolato-tag {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 0.65rem;
    font-weight: 700;
    margin-left: 8px;
    vertical-align: middle;
}
.capitolato-tag.si { background: var(--upcoming-bg); color: var(--upcoming-text); border: 1px solid #A7F3D0; }
.capitolato-tag.no { background: var(--critical-bg); color: var(--critical-text); border: 1px solid #FECACA; }

.card-actions {
    display: flex;
    align-items: center;
    gap: 12px;
}
.days-pill {
    padding: 6px 12px;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 700;
}
.days-pill.expired { background: var(--expired-text); color: white; }
.days-pill.critical { background: var(--critical-bg); color: var(--critical-text); }
.days-pill.processing { background: var(--processing-bg); color: var(--processing-text); }
.days-pill.upcoming { background: var(--upcoming-bg); color: var(--upcoming-text); }

.btn-icon {
    border: none;
    background: white;
    box-shadow: var(--shadow-sm);
    width: 32px;
    height: 32px;
    border-radius: 8px;
    font-size: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: 0.2s;
}
.btn-icon:hover { transform: scale(1.05); }

.card-details-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 12px;
    background: rgba(255,255,255,0.5);
    padding: 12px;
    border-radius: var(--radius-sm);
}
.detail-item { display: flex; flex-direction: column; gap: 2px; }
.detail-item .lbl { font-size: 0.65rem; font-weight: 700; text-transform: uppercase; color: var(--text-muted); }
.detail-item .val { font-size: 0.85rem; font-weight: 500; color: var(--text-main); }
.val.strong-date { font-weight: 700; color: var(--primary); }

.empty-state { text-align: center; padding: 30px; color: var(--text-muted); font-size: 0.9rem; font-style: italic; }

/* --- MODALI --- */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    backdrop-filter: blur(4px);
    z-index: 1000;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 24px;
}
.modal-overlay.active { display: flex; }

.modal-content {
    background: var(--surface);
    border-radius: var(--radius-lg);
    width: 100%;
    max-width: 800px;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 25px 50px -12px rgba(0,0,0,0.25);
    overflow: hidden;
    animation: modalPop 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
@keyframes modalPop {
    0% { transform: scale(0.95); opacity: 0; }
    100% { transform: scale(1); opacity: 1; }
}

.modal-header {
    background: linear-gradient(135deg, var(--primary), var(--primary-hover));
    padding: 24px 32px;
    color: white;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.shrink-header { padding: 16px 24px;}
.modal-header h3 { font-weight: 700; font-size: 1.1rem; }
.modal-close {
    background: rgba(255,255,255,0.2);
    border: none;
    color: white;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}
.modal-close:hover { background: rgba(255,255,255,0.3); }

.modal-body {
    padding: 32px;
    overflow-y: auto;
    background: var(--background);
}
.modal-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
}
.modal-field {
    background: var(--surface);
    padding: 16px;
    border-radius: var(--radius-md);
    border-left: 4px solid var(--primary);
    box-shadow: var(--shadow-sm);
}
.modal-field.full-width { grid-column: 1 / -1; }
.modal-label { font-size: 0.7rem; font-weight: 700; color: var(--primary); text-transform: uppercase; margin-bottom: 6px; }
.modal-value { font-size: 0.9rem; font-weight: 500; color: var(--text-main); word-wrap: break-word; }

.note-field { border-left-color: #F59E0B; }
.note-field .modal-label { color: #D97706; }
.note-container { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }
.note-container textarea {
    width: 100%;
    min-height: 80px;
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    resize: vertical;
    font-family: inherit;
    font-size: 0.9rem;
}
.note-container textarea:focus { outline: none; border-color: var(--primary); }
.btn-save {
    align-self: flex-end;
    padding: 8px 16px;
    background: #F59E0B;
    color: white;
    border: none;
    border-radius: var(--radius-sm);
    font-weight: 600;
    cursor: pointer;
    font-size: 0.85rem;
}
.btn-save:hover { background: #D97706; }

/* Stampa */
@media print {
    .sidebar, .top-bar .update-badge, .btn-icon, .modal-overlay { display: none !important; }
    .layout-wrapper { display: block; overflow: visible; height: auto; }
    .main-content { overflow: visible; }
    body { background: white; }
    .list-section { border: 1px solid #ccc; break-inside: avoid; }
    .kpi-grid { grid-template-columns: repeat(4, 1fr); }
}
