/* ============================================================
   PreventivoAI — App CSS v1.0
   Mobile-first, dark mode, SaaS premium
   ============================================================ */

/* ─── CSS Variables ─────────────────────────────────────────── */
:root {
    --color-primary:       #2563eb;
    --color-primary-dark:  #1d4ed8;
    --color-primary-light: #dbeafe;
    --color-success:       #10b981;
    --color-warning:       #f59e0b;
    --color-danger:        #ef4444;
    --color-info:          #06b6d4;

    --sidebar-width:       260px;
    --sidebar-bg:          #0f1729;
    --sidebar-text:        #94a3b8;
    --sidebar-hover:       rgba(255,255,255,0.06);
    --sidebar-active-bg:   rgba(37,99,235,0.18);
    --sidebar-active-text: #60a5fa;

    --navbar-height:       62px;
    --navbar-bg:           #ffffff;
    --navbar-border:       #e2e8f0;

    --body-bg:             #f1f5f9;
    --card-bg:             #ffffff;
    --card-border:         #e2e8f0;
    --card-shadow:         0 1px 3px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.04);
    --card-radius:         12px;

    --text-primary:        #0f172a;
    --text-secondary:      #64748b;
    --text-muted:          #94a3b8;

    --input-bg:            #ffffff;
    --input-border:        #cbd5e1;
    --input-focus-border:  #2563eb;

    --transition:          .18s ease;
}

[data-theme="dark"] {
    --sidebar-bg:          #0a0f1e;
    --sidebar-text:        #7c8ba3;
    --sidebar-hover:       rgba(255,255,255,0.04);

    --navbar-bg:           #111827;
    --navbar-border:       #1f2937;

    --body-bg:             #0d1117;
    --card-bg:             #161b27;
    --card-border:         #1f2937;
    --card-shadow:         0 1px 3px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.2);

    --text-primary:        #f1f5f9;
    --text-secondary:      #94a3b8;
    --text-muted:          #64748b;

    --input-bg:            #1f2937;
    --input-border:        #374151;
}

/* ─── Reset & Base ──────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; }

body {
    margin: 0;
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    font-size: 14px;
    color: var(--text-primary);
    background: var(--body-bg);
    transition: background var(--transition), color var(--transition);
    -webkit-font-smoothing: antialiased;
}

/* ─── Layout ─────────────────────────────────────────────────── */
.app-wrapper {
    display: flex;
    min-height: 100vh;
}

.content-area {
    display: flex;
    flex: 1;
    min-width: 0;
}

.main-wrapper {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-width: 0;
}

.main-content {
    flex: 1;
    padding: 24px;
    max-width: 1400px;
    width: 100%;
    margin: 0 auto;
}

/* ─── Sidebar ────────────────────────────────────────────────── */
.sidebar {
    width: var(--sidebar-width);
    min-height: 100vh;
    background: var(--sidebar-bg);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    z-index: 1000;
    transition: transform var(--transition), width var(--transition);
    position: relative;
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 18px 16px 14px;
    border-bottom: 1px solid rgba(255,255,255,0.06);
    min-height: 64px;
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    text-decoration: none;
    min-width: 0;
}

.sidebar-logo {
    width: 36px;
    height: 36px;
    border-radius: 8px;
    object-fit: contain;
    flex-shrink: 0;
}

.sidebar-brand-text {
    color: #f1f5f9;
    font-weight: 700;
    font-size: 15px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sidebar-close {
    background: none;
    border: none;
    color: var(--sidebar-text);
    font-size: 18px;
    cursor: pointer;
    padding: 4px;
}

.sidebar-nav {
    flex: 1;
    padding: 16px 8px;
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: rgba(255,255,255,.1) transparent;
}

.sidebar-cta {
    padding: 0 8px 16px;
}

.sidebar-cta .btn-primary {
    background: var(--color-primary);
    border-color: var(--color-primary);
    font-size: 13px;
    padding: 9px 16px;
    border-radius: 8px;
    font-weight: 600;
}

.sidebar-menu {
    list-style: none;
    padding: 0;
    margin: 0 0 8px;
}

.sidebar-item {
    margin-bottom: 2px;
}

.sidebar-link {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 12px;
    border-radius: 8px;
    color: var(--sidebar-text);
    text-decoration: none;
    font-size: 13.5px;
    font-weight: 500;
    transition: all var(--transition);
}

.sidebar-link:hover {
    background: var(--sidebar-hover);
    color: #e2e8f0;
}

.sidebar-item.active .sidebar-link {
    background: var(--sidebar-active-bg);
    color: var(--sidebar-active-text);
}

.sidebar-icon {
    font-size: 16px;
    flex-shrink: 0;
    width: 20px;
    text-align: center;
}

.sidebar-section-label {
    padding: 14px 12px 6px;
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 1.2px;
    color: rgba(255,255,255,0.25);
    font-weight: 600;
}

.sidebar-footer {
    border-top: 1px solid rgba(255,255,255,0.06);
    padding: 14px 12px;
}

.sidebar-user {
    display: flex;
    align-items: center;
    gap: 10px;
}

.sidebar-user-avatar {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: var(--color-primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 14px;
    flex-shrink: 0;
}

.sidebar-user-info {
    flex: 1;
    min-width: 0;
}

.sidebar-user-name {
    color: #e2e8f0;
    font-size: 12.5px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sidebar-user-email {
    color: var(--sidebar-text);
    font-size: 11px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sidebar-logout {
    color: var(--sidebar-text);
    font-size: 17px;
    text-decoration: none;
    padding: 4px;
    border-radius: 6px;
    transition: color var(--transition);
}

.sidebar-logout:hover { color: var(--color-danger); }

/* Overlay mobile */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 999;
    backdrop-filter: blur(2px);
}

.sidebar-overlay.active { display: block; }

/* ─── Navbar ─────────────────────────────────────────────────── */
.main-navbar {
    height: var(--navbar-height);
    background: var(--navbar-bg);
    border-bottom: 1px solid var(--navbar-border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    gap: 16px;
    position: sticky;
    top: 0;
    z-index: 100;
    transition: background var(--transition);
}

.navbar-left {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
}

.navbar-title {
    font-size: 16px;
    font-weight: 700;
    color: var(--text-primary);
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.navbar-right {
    display: flex;
    align-items: center;
    gap: 6px;
}

.navbar-search {
    position: relative;
}

.navbar-search .form-control {
    width: 220px;
    background: var(--body-bg);
    font-size: 13px;
}

.search-results {
    position: absolute;
    top: calc(100% + 6px);
    left: 0;
    right: 0;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 10px;
    box-shadow: var(--card-shadow);
    max-height: 320px;
    overflow-y: auto;
    z-index: 200;
    display: none;
}

.search-results.visible { display: block; }
.search-result-item { padding: 10px 14px; border-bottom: 1px solid var(--card-border); cursor: pointer; }
.search-result-item:hover { background: var(--body-bg); }
.search-result-item:last-child { border: none; }

.btn-ghost {
    background: none;
    border: none;
    padding: 7px 10px;
    border-radius: 8px;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all var(--transition);
    line-height: 1;
}

.btn-ghost:hover {
    background: var(--body-bg);
    color: var(--text-primary);
}

/* Notifiche */
.notify-badge {
    position: absolute;
    top: 4px;
    right: 4px;
    background: var(--color-danger);
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
}

.notify-dropdown {
    width: 340px;
    padding: 0;
    border-radius: 12px;
    border: 1px solid var(--card-border);
    box-shadow: var(--card-shadow);
    background: var(--card-bg);
}

.notify-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    border-bottom: 1px solid var(--card-border);
    font-weight: 600;
    font-size: 13px;
}

.notify-list { max-height: 300px; overflow-y: auto; }
.notify-empty { padding: 20px; }

/* User avatar */
.user-avatar-sm {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--color-primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 13px;
    flex-shrink: 0;
}

/* ─── Cards ──────────────────────────────────────────────────── */
.card {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--card-radius);
    box-shadow: var(--card-shadow);
    transition: background var(--transition);
}

.card-header {
    background: transparent;
    border-bottom: 1px solid var(--card-border);
    padding: 14px 20px;
}

.card-title { font-size: 14px; font-weight: 700; color: var(--text-primary); }

.card-body { padding: 20px; }
.card-footer { background: transparent; border-top: 1px solid var(--card-border); padding: 14px 20px; }

/* ─── Stat Cards ─────────────────────────────────────────────── */
.stat-card {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--card-radius);
    box-shadow: var(--card-shadow);
    padding: 20px;
    position: relative;
    overflow: hidden;
    transition: transform var(--transition), box-shadow var(--transition);
}

.stat-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 24px rgba(0,0,0,.1);
}

.stat-card::before {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 3px;
    border-radius: var(--card-radius) var(--card-radius) 0 0;
}

.stat-primary::before { background: var(--color-primary); }
.stat-success::before { background: var(--color-success); }
.stat-warning::before { background: var(--color-warning); }
.stat-info::before    { background: var(--color-info); }

.stat-icon {
    width: 44px;
    height: 44px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    margin-bottom: 14px;
}

.stat-primary .stat-icon { background: #dbeafe; color: var(--color-primary); }
.stat-success .stat-icon { background: #d1fae5; color: var(--color-success); }
.stat-warning .stat-icon { background: #fef3c7; color: var(--color-warning); }
.stat-info    .stat-icon { background: #cffafe; color: var(--color-info); }

.stat-value {
    font-size: 22px;
    font-weight: 800;
    color: var(--text-primary);
    line-height: 1;
    margin-bottom: 4px;
}

.stat-label { font-size: 12px; color: var(--text-muted); font-weight: 500; }

/* ─── Quick Actions ──────────────────────────────────────────── */
.quick-action-card {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 16px 18px;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--card-radius);
    box-shadow: var(--card-shadow);
    transition: all var(--transition);
    color: var(--text-primary);
}

.quick-action-card:hover {
    border-color: var(--color-primary);
    transform: translateY(-1px);
    box-shadow: 0 4px 16px rgba(37,99,235,.12);
    color: var(--text-primary);
}

.quick-action-icon {
    width: 44px;
    height: 44px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    flex-shrink: 0;
}

/* ─── Empty State ─────────────────────────────────────────────── */
.empty-state {
    text-align: center;
    padding: 48px 24px;
}

.empty-icon {
    font-size: 48px;
    color: var(--text-muted);
    display: block;
    margin-bottom: 16px;
}

/* ─── Tables ─────────────────────────────────────────────────── */
.table {
    color: var(--text-primary);
    --bs-table-bg: transparent;
    --bs-table-hover-bg: rgba(37,99,235,.04);
}

.table thead th {
    font-size: 11.5px;
    text-transform: uppercase;
    letter-spacing: .6px;
    font-weight: 700;
    color: var(--text-muted);
    border-bottom: 2px solid var(--card-border);
    padding: 10px 16px;
    white-space: nowrap;
}

.table tbody td {
    padding: 12px 16px;
    border-bottom: 1px solid var(--card-border);
    vertical-align: middle;
    font-size: 13.5px;
}

.table tbody tr:last-child td { border-bottom: 0; }

/* ─── Badges ─────────────────────────────────────────────────── */
.badge {
    font-size: 11px;
    font-weight: 600;
    padding: 4px 9px;
    border-radius: 6px;
}

/* ─── Avatar circle ──────────────────────────────────────────── */
.avatar-circle {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: var(--color-primary-light);
    color: var(--color-primary);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 13px;
    flex-shrink: 0;
}

/* ─── Forms ──────────────────────────────────────────────────── */
.form-control, .form-select {
    background: var(--input-bg);
    border-color: var(--input-border);
    color: var(--text-primary);
    border-radius: 8px;
    font-size: 13.5px;
    transition: border-color var(--transition), box-shadow var(--transition);
}

.form-control:focus, .form-select:focus {
    border-color: var(--input-focus-border);
    box-shadow: 0 0 0 3px rgba(37,99,235,.15);
    background: var(--input-bg);
    color: var(--text-primary);
}

.form-label {
    font-size: 12.5px;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 5px;
}

.input-group-text {
    background: var(--body-bg);
    border-color: var(--input-border);
    color: var(--text-muted);
}

/* ─── Buttons ─────────────────────────────────────────────────── */
.btn {
    border-radius: 8px;
    font-weight: 600;
    font-size: 13.5px;
    transition: all var(--transition);
}

.btn-primary {
    background: var(--color-primary);
    border-color: var(--color-primary);
}

.btn-primary:hover {
    background: var(--color-primary-dark);
    border-color: var(--color-primary-dark);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(37,99,235,.3);
}

.btn-lg { padding: 12px 24px; font-size: 15px; }
.btn-sm { padding: 5px 12px; font-size: 12px; }

/* ─── Flash messages ─────────────────────────────────────────── */
.flash-message {
    position: fixed;
    top: 72px;
    right: 20px;
    z-index: 9999;
    max-width: 400px;
    padding: 12px 16px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 13.5px;
    font-weight: 500;
    box-shadow: 0 4px 20px rgba(0,0,0,.15);
    animation: slideInRight .3s ease;
}

.flash-success {
    background: #d1fae5;
    color: #065f46;
    border: 1px solid #a7f3d0;
}

.flash-error {
    background: #fee2e2;
    color: #991b1b;
    border: 1px solid #fca5a5;
}

.flash-close {
    background: none;
    border: none;
    cursor: pointer;
    margin-left: auto;
    opacity: .6;
    font-size: 16px;
    padding: 0;
    color: inherit;
}

@keyframes slideInRight {
    from { transform: translateX(110%); opacity: 0; }
    to   { transform: translateX(0);   opacity: 1; }
}

/* ─── Auth ───────────────────────────────────────────────────── */
.auth-body {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #0f172a 0%, #1e3a5f 50%, #1e40af 100%);
    padding: 24px;
}

.auth-container {
    width: 100%;
    max-width: 440px;
}

.auth-card {
    background: #fff;
    border-radius: 16px;
    padding: 40px 36px;
    box-shadow: 0 20px 60px rgba(0,0,0,.3);
}

.auth-logo { text-align: center; margin-bottom: 30px; }

.auth-logo-icon {
    width: 60px;
    height: 60px;
    border-radius: 16px;
    background: linear-gradient(135deg, var(--color-primary), #7c3aed);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    color: #fff;
    margin-bottom: 14px;
    box-shadow: 0 8px 24px rgba(37,99,235,.35);
}

.auth-title { font-size: 22px; font-weight: 800; margin: 0; color: #0f172a; }
.auth-subtitle { color: #64748b; margin: 4px 0 0; font-size: 13.5px; }

.auth-footer {
    text-align: center;
    margin-top: 20px;
    font-size: 13px;
    color: #64748b;
}

.auth-footer a {
    color: var(--color-primary);
    font-weight: 600;
    text-decoration: none;
}

.demo-credentials {
    text-align: center;
    margin-top: 12px;
    padding: 8px;
    background: #f8fafc;
    border-radius: 8px;
}

/* ─── AI Create page ─────────────────────────────────────────── */
.ai-hero {
    background: linear-gradient(135deg, #1e40af 0%, #4f46e5 50%, #7c3aed 100%);
    border: none;
    color: #fff;
}

.ai-hero-icon {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    background: rgba(255,255,255,.15);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 30px;
}

.ai-hero h2, .ai-hero p { color: #fff; }

.prompt-textarea {
    font-size: 15px;
    line-height: 1.7;
    min-height: 120px;
    resize: vertical;
    border-radius: 10px;
}

.prompt-chip { border-radius: 20px; font-size: 12px; }

.ai-progress-bar {
    height: 6px;
    background: var(--card-border);
    border-radius: 3px;
    overflow: hidden;
}

.ai-progress-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--color-primary), #7c3aed);
    border-radius: 3px;
    width: 0%;
    transition: width .4s ease;
    animation: pulse-bar 1.5s ease infinite;
}

@keyframes pulse-bar {
    0%,100% { opacity: 1; }
    50%      { opacity: .7; }
}

.preview-stat {
    padding: 12px 16px;
    background: var(--body-bg);
    border-radius: 8px;
}

.tip-card {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 10px;
    padding: 16px;
    height: 100%;
}

.tip-icon { font-size: 22px; margin-bottom: 8px; display: block; }
.tip-title { font-weight: 700; font-size: 13px; margin-bottom: 4px; }
.tip-text  { font-size: 12px; color: var(--text-muted); }

/* ─── Editor Preventivo ──────────────────────────────────────── */
.riepilogo-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 7px 0;
    font-size: 13.5px;
    border-bottom: 1px solid var(--card-border);
}

.riepilogo-row:last-of-type { border: none; }

.riepilogo-totale {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 15px;
    font-weight: 800;
}

.totale-value {
    font-size: 22px;
    font-weight: 800;
    color: var(--color-primary);
}

#materialiTable .drag-handle { cursor: grab; color: var(--text-muted); }
#materialiTable .drag-handle:active { cursor: grabbing; }
#materialiTable tr.sortable-ghost { opacity: .4; background: var(--color-primary-light); }
#materialiTable .mat-totale { font-size: 13.5px; }

/* ─── Preventivo view ────────────────────────────────────────── */
.preventivo-preview { border: 1px solid var(--card-border); }
.preview-logo { max-height: 60px; max-width: 200px; object-fit: contain; }
.cliente-box  { background: var(--body-bg); padding: 14px 16px; border-radius: 8px; }

/* ─── Riepilogo editor ───────────────────────────────────────── */
.sticky-top { position: sticky !important; }

/* ─── Paginazione ────────────────────────────────────────────── */
.page-link {
    color: var(--color-primary);
    border-color: var(--card-border);
    background: var(--card-bg);
}

.page-item.active .page-link {
    background: var(--color-primary);
    border-color: var(--color-primary);
}

/* ─── Dark mode overrides ────────────────────────────────────── */
[data-theme="dark"] .table-light { --bs-table-bg: rgba(255,255,255,.03); }
[data-theme="dark"] .form-control { color-scheme: dark; }
[data-theme="dark"] .card-header { background: rgba(255,255,255,.02); }
[data-theme="dark"] .modal-content { background: var(--card-bg); border-color: var(--card-border); }
[data-theme="dark"] .modal-header { border-color: var(--card-border); }
[data-theme="dark"] .dropdown-menu { background: var(--card-bg); border-color: var(--card-border); }
[data-theme="dark"] .dropdown-item { color: var(--text-primary); }
[data-theme="dark"] .dropdown-item:hover { background: var(--sidebar-hover); }
[data-theme="dark"] .alert { border-color: transparent; }
[data-theme="dark"] .table-light { background: rgba(255,255,255,0.03) !important; }
[data-theme="dark"] .bg-light { background: rgba(255,255,255,0.04) !important; }

/* ─── Responsive ─────────────────────────────────────────────── */
@media (max-width: 991.98px) {
    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        transform: translateX(-100%);
        z-index: 1050;
    }

    .sidebar.open {
        transform: translateX(0);
        box-shadow: 4px 0 24px rgba(0,0,0,.25);
    }

    .main-content { padding: 16px; }
    .stat-value   { font-size: 18px; }
}

@media (max-width: 575.98px) {
    .auth-card { padding: 28px 20px; }
    .main-content { padding: 12px; }
    .card-body { padding: 14px; }
    .card-header { padding: 12px 14px; }
    .btn-lg { font-size: 14px; padding: 10px 18px; }
}

/* ─── Scrollbar ──────────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--text-muted); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-secondary); }

/* ─── Utilities ──────────────────────────────────────────────── */
.text-primary { color: var(--color-primary) !important; }
code.text-primary { color: var(--color-primary) !important; background: var(--color-primary-light); padding: 2px 6px; border-radius: 4px; }

.fade-in { animation: fadeIn .3s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

/* Sidebar toggle button */
.sidebar-toggle { color: var(--text-secondary); }
