* { margin: 0; padding: 0; box-sizing: border-box; }
        
        :root {
            --primary: #1a472a;
            --primary-light: #2d5f3a;
            --accent: #f4c430;
            --bg: #f9fafb;
            --text: #111827;
            --border: #e5e7eb;
            --danger: #c41e3a;
            --success: #10b981;
        }
        
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: var(--bg);
            color: var(--text);
        }
        
        body.dark { --bg: #1a1a1a; --text: #f0f0f0; --border: #333; }
        body.dark .card, body.dark .header { background: #222; }
        body.dark input, body.dark select, body.dark textarea { background: #333; color: white; border-color: #444; }
        body.dark .login-box { background: #222; }
        body.dark .code-hint { background: rgba(255,255,255,0.1); }
        body.dark .code-display { background: #333; color: var(--accent); }
        body.dark .data-row { background: #333; }
        
        /* LOGIN */
        .login { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: linear-gradient(135deg, #1a472a, #2d5f3a); }
        .login-box { background: white; padding: 50px; border-radius: 12px; width: 90%; max-width: 400px; text-align: center; }
        .login-box h1 { color: var(--primary); margin-bottom: 20px; font-size: 1.8em; }
        .login-box input { width: 100%; padding: 12px; border: 2px solid var(--border); border-radius: 8px; margin: 10px 0; font-size: 1em; }
        .login-box button { width: 100%; padding: 12px; background: var(--primary); color: white; border: none; border-radius: 8px; font-weight: 600; cursor: pointer; margin-top: 15px; font-size: 1em; }
        .login-box button:hover { background: var(--primary-light); }
        .code-hint { margin-top: 20px; padding: 15px; background: rgba(0,0,0,0.05); border-radius: 8px; font-size: 0.9em; }
        .code-display { font-family: monospace; background: white; padding: 10px; border-radius: 6px; margin: 8px 0; color: var(--primary); font-weight: 600; font-size: 1.1em; }
        
        /* APP LAYOUT */
        .app { display: grid; grid-template-columns: 260px 1fr; min-height: 100vh; }
        .sidebar { background: linear-gradient(180deg, #1a472a, #2d5f3a); color: white; padding: 20px; overflow-y: auto; max-height: 100vh; position: sticky; top: 0; transition: transform 0.3s ease; z-index: 100; }
        .sidebar-title { font-size: 1.2em; font-weight: 700; margin-bottom: 25px; line-height: 1.3; display: flex; align-items: center; gap: 8px; }
        .sidebar-title .brand-icon { font-size: 1.8em; }
        .sidebar-title .brand-text { display: flex; flex-direction: column; }
        .sidebar-title .brand-name { font-size: 0.85em; font-weight: 700; line-height: 1.2; }
        .sidebar-title .brand-sub { font-size: 0.55em; font-weight: 400; opacity: 0.7; letter-spacing: 1px; text-transform: uppercase; }
        .nav-group { margin-bottom: 25px; }
        .nav-group-title { font-size: 0.75em; text-transform: uppercase; opacity: 0.7; margin-bottom: 10px; letter-spacing: 1px; }
        .nav-btn { display: block; width: 100%; padding: 10px 15px; margin: 5px 0; background: rgba(255,255,255,0.1); border: none; color: white; border-radius: 6px; cursor: pointer; text-align: left; font-weight: 500; font-size: 0.95em; transition: background 0.2s; }
        .nav-btn:hover, .nav-btn.active { background: rgba(255,255,255,0.25); }
        .license-box { background: rgba(255,255,255,0.1); padding: 12px; border-radius: 8px; margin-top: 30px; font-size: 0.9em; }
        
        .menu-toggle { display: none; position: fixed; top: 12px; left: 12px; z-index: 200; background: var(--primary); color: white; border: none; border-radius: 8px; width: 44px; height: 44px; font-size: 1.5em; cursor: pointer; box-shadow: 0 2px 12px rgba(0,0,0,0.3); align-items: center; justify-content: center; }
        .sidebar-close { display: none; position: absolute; top: 12px; right: 12px; background: rgba(255,255,255,0.15); color: white; border: none; border-radius: 6px; width: 32px; height: 32px; font-size: 1.1em; cursor: pointer; }
        .sidebar-overlay { display: none; position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.5); z-index: 90; }
        
        .main { padding: 30px; overflow-y: auto; }
        .page { display: none; }
        .page.active { display: block; }
        .header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 25px; background: white; padding: 20px; border-radius: 12px; box-shadow: 0 2px 8px rgba(0,0,0,0.05); flex-wrap: wrap; gap: 10px; }
        .header h1 { color: var(--primary); font-size: 1.8em; }
        
        .card { background: white; border-radius: 12px; padding: 25px; margin-bottom: 20px; box-shadow: 0 2px 8px rgba(0,0,0,0.05); }
        .card h2 { color: var(--primary); margin-bottom: 20px; font-size: 1.3em; }
        .card h3 { color: var(--primary); margin: 20px 0 15px; }
        
        .form-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 15px; margin-bottom: 15px; }
        .form-group { display: flex; flex-direction: column; }
        label { font-weight: 600; margin-bottom: 6px; font-size: 0.95em; }
        input, select, textarea { padding: 10px; border: 2px solid var(--border); border-radius: 8px; font-size: 1em; font-family: inherit; color: var(--text); background: white; }
        input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); }
        textarea { min-height: 80px; resize: vertical; }
        
        .tabs { display: flex; gap: 5px; border-bottom: 2px solid var(--border); margin-bottom: 20px; flex-wrap: wrap; }
        .tab-btn { padding: 10px 16px; background: none; border: none; border-bottom: 3px solid transparent; cursor: pointer; font-weight: 600; color: #999; font-size: 0.9em; white-space: nowrap; transition: all 0.2s; }
        .tab-btn.active { border-bottom-color: var(--primary); color: var(--primary); }
        .tab-btn:hover { color: var(--primary); }
        .tab-content { display: none; }
        .tab-content.active { display: block; }
        
        .table-section { margin-top: 20px; overflow-x: auto; }
        .data-row { background: var(--bg); border-radius: 8px; padding: 12px; margin-bottom: 10px; border-left: 3px solid var(--primary); }
        .data-row .row-fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 10px; align-items: end; }
        .data-row .row-field { display: flex; flex-direction: column; }
        .data-row .row-field label { font-size: 0.75em; font-weight: 600; color: #666; text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 3px; }
        .data-row input, .data-row select { padding: 8px; font-size: 0.9em; }
        .data-row .row-delete { display: flex; align-items: end; padding-bottom: 2px; }
        
        .summary-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 15px; margin: 20px 0; }
        .summary-box { background: linear-gradient(135deg, #1a472a, #2d5f3a); color: white; padding: 20px; border-radius: 12px; text-align: center; }
        .summary-label { font-size: 0.8em; opacity: 0.9; text-transform: uppercase; margin-bottom: 8px; }
        .summary-amount { font-size: 1.6em; font-weight: 700; color: var(--accent); }
        
        .btn { padding: 10px 20px; border: none; border-radius: 8px; font-weight: 600; cursor: pointer; font-size: 0.95em; transition: opacity 0.2s; }
        .btn:hover { opacity: 0.9; }
        .btn-primary { background: var(--primary); color: white; }
        .btn-secondary { background: var(--border); color: var(--text); }
        .btn-danger { background: var(--danger); color: white; }
        .btn-small { padding: 6px 12px; font-size: 0.85em; }
        .btn-accent { background: var(--accent); color: var(--primary); }
        
        .alert { padding: 15px; border-radius: 8px; margin-bottom: 20px; display: none; }
        .alert.show { display: block; }
        .alert.success { background: #d1fae5; color: #065f46; border-left: 4px solid #10b981; }
        .alert.error { background: #fee2e2; color: #991b1b; border-left: 4px solid #c41e3a; }
        
        .hidden { display: none !important; }
        
        /* Signature Canvas */
        .sig-canvas { border: 2px solid var(--border); border-radius: 8px; cursor: crosshair; background: white; touch-action: none; }
        
        /* Preview Containers */
        .preview-container { border: 2px solid var(--border); border-radius: 8px; padding: 20px; background: white; margin-top: 15px; }
        
        /* Status badges */
        .status-badge { display: inline-block; padding: 4px 10px; border-radius: 20px; font-size: 0.8em; font-weight: 600; text-transform: capitalize; }
        .status-draft { background: #f3f4f6; color: #374151; }
        .status-sent { background: #e0e7ff; color: #3730a3; }
        .status-viewed { background: #fef3c7; color: #92400e; }
        .status-pending { background: #fef3c7; color: #92400e; }
        .status-accepted { background: #d1fae5; color: #065f46; }
        .status-scheduled { background: #dbeafe; color: #1e40af; }
        .status-in_progress { background: #fde68a; color: #78350f; }
        .status-completed { background: #a7f3d0; color: #047857; }
        .status-invoiced { background: #c7d2fe; color: #4338ca; }
        .status-paid { background: #6ee7b7; color: #064e3b; }
        .status-rejected { background: #fee2e2; color: #991b1b; }
        
        /* Footer */
        .app-footer { text-align: center; padding: 20px; margin-top: 40px; background: var(--primary); color: white; border-radius: 12px; }
        .app-footer strong { color: var(--accent); }
        
        /* Remote signing page */
        .remote-signing { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: linear-gradient(135deg, #f0fdf4, #ecfdf5); padding: 20px; }
        .remote-signing .sign-box { background: white; padding: 40px; border-radius: 16px; width: 100%; max-width: 600px; box-shadow: 0 20px 60px rgba(0,0,0,0.1); text-align: center; }
        .remote-signing h2 { color: var(--primary); margin-bottom: 10px; }
        .remote-signing p { color: #666; margin-bottom: 20px; }
        
        @media (max-width: 768px) {
            /* Layout */
            .app { grid-template-columns: 1fr; }
            .sidebar { position: fixed; top: 0; left: 0; bottom: 0; width: 280px; max-height: 100vh; transform: translateX(-100%); }
            .sidebar.open { transform: translateX(0); }
            .sidebar-close { display: flex; align-items: center; justify-content: center; }
            .sidebar-overlay.show { display: block; }
            .menu-toggle { display: flex; }
            .main { padding: 10px; padding-top: 60px; }

            /* Header */
            .header { padding: 12px; flex-direction: column; align-items: stretch; gap: 8px; }
            .header h1 { font-size: 1.15em; }
            .header > div { display: flex; flex-direction: column; gap: 6px; width: 100%; }
            .header .btn { width: 100%; text-align: center; padding: 10px; font-size: 0.88em; box-sizing: border-box; }

            /* Forms */
            .form-row { grid-template-columns: 1fr !important; gap: 10px; }
            .form-group { min-width: 0 !important; }
            .form-group label { font-size: 0.85em; }
            input, select, textarea { font-size: 16px !important; padding: 10px; width: 100%; box-sizing: border-box; }

            /* Summary boxes */
            .summary-grid { grid-template-columns: 1fr 1fr; gap: 8px; }
            .summary-box { padding: 12px 8px; }
            .summary-label { font-size: 0.68em; }
            .summary-amount { font-size: 1.2em; }

            /* Tabs — horizontal scroll */
            .tabs { gap: 0; overflow-x: auto; flex-wrap: nowrap; -webkit-overflow-scrolling: touch; padding-bottom: 2px; scrollbar-width: none; }
            .tabs::-webkit-scrollbar { display: none; }
            .tab-btn { padding: 8px 10px; font-size: 0.75em; flex-shrink: 0; white-space: nowrap; }

            /* Cards */
            .card { padding: 12px; margin-bottom: 10px; }
            .card h2 { font-size: 1.05em; margin-bottom: 10px; }
            .card h3 { font-size: 0.95em; }

            /* Data rows */
            .data-row { padding: 10px; }
            .data-row .row-fields { grid-template-columns: 1fr !important; gap: 8px; }
            .data-row input, .data-row select { font-size: 16px !important; }
            .data-row .row-delete { justify-content: flex-end; }

            /* Tables */
            .table-section { overflow-x: auto; -webkit-overflow-scrolling: touch; }
            table { font-size: 0.8em; }
            th, td { padding: 6px 5px; }

            /* Buttons */
            .btn { padding: 10px 14px; font-size: 0.85em; width: 100%; text-align: center; margin-bottom: 5px; box-sizing: border-box; }
            .btn-small, .btn.btn-small { width: auto; }

            /* Login */
            .login-box { padding: 25px 18px; width: 95%; }
            .login-box h1 { font-size: 1.3em; }

            /* Sidebar extras */
            .license-box { font-size: 0.8em; }
            .nav-group-title { font-size: 0.7em; }
            .nav-btn i { width: 15px !important; height: 15px !important; }

            /* Alerts */
            .alert { font-size: 0.85em; left: 10px; right: 10px; max-width: none; }

            /* Signature canvases */
            .sig-canvas, canvas[id*="Sig"], canvas[id*="sig"], canvas[id*="Canvas"] { width: 100% !important; max-width: 100% !important; height: 120px !important; }

            /* Inline flex containers — force wrap */
            [style*="display:flex"][style*="gap"] { flex-wrap: wrap !important; }
            [style*="display: flex"][style*="gap"] { flex-wrap: wrap !important; }

            /* Quick invoice form */
            #quickInvoiceForm .form-row, #quickInvoiceForm [style*="grid"] { grid-template-columns: 1fr !important; }

            /* Estimate action buttons row */
            .page > .header > div[style*="flex"] { flex-direction: column; width: 100%; }

            /* Fix inline min-width on flex children */
            [style*="min-width:200px"], [style*="min-width:250px"], [style*="min-width:130px"], [style*="min-width:110px"] { min-width: 0 !important; width: 100% !important; }
            [style*="flex:1"] { min-width: 0 !important; }

            /* Photos & measurements */
            #heightDist, #heightEye, #heightPace { width: 100% !important; }

            /* Modals */
            [style*="max-width:700px"], [style*="max-width:600px"] { max-width: 95vw !important; margin: 10px !important; }

            /* Tour tooltip */
            .tour-tooltip { max-width: 90vw !important; left: 5vw !important; right: 5vw !important; }

            /* Onboarding / setup section */
            [style*="grid-template-columns:1fr 1fr 1fr"] { grid-template-columns: 1fr !important; }

            /* Invoice/proposal/contract preview */
            #invoicePreview, #proposalPreview, #contractPreview { font-size: 0.85em; overflow-x: auto; }

            /* Print/download button rows — don't force full width */
            .card > div[style*="display:flex"][style*="flex-wrap"] .btn { width: auto; flex: 1; min-width: 0; }

            /* Chat window */
            #chatWindow { width: 100% !important; max-width: 100vw !important; right: 0 !important; left: 0 !important; bottom: 0 !important; border-radius: 16px 16px 0 0 !important; height: 70vh !important; }

            /* Schedule — compact mobile calendar & job cards */
            #calendarGrid { gap: 4px !important; }
            .sched-card { padding: 10px !important; }
            .sched-card .btn-small { padding: 6px 8px !important; font-size: 0.78em !important; }
            .sched-card select { font-size: 14px !important; padding: 5px 6px !important; }
            /* Schedule form on mobile */
            #scheduleForm .form-row { grid-template-columns: 1fr !important; }

            /* Contracts & Proposals list — horizontal scroll for grid headers */
            #contractsList > div:first-child,
            #proposalsList > div:first-child { overflow-x: auto; -webkit-overflow-scrolling: touch; }
            #contractsList [style*="grid-template-columns"],
            #proposalsList [style*="grid-template-columns"] { grid-template-columns: 1fr !important; }
            #contractsList [style*="display:grid"] > div,
            #proposalsList [style*="display:grid"] > div { display: block; }
        }
        @media (max-width: 380px) {
            .summary-grid { grid-template-columns: 1fr; }
            .main { padding: 6px; padding-top: 56px; }
            .header h1 { font-size: 1em; }
            .tab-btn { padding: 6px 8px; font-size: 0.7em; }
            .card { padding: 10px; }
            .card h2 { font-size: 0.95em; }
            .btn { font-size: 0.82em; padding: 9px 12px; }
            .summary-amount { font-size: 1.1em; }
            .nav-btn { padding: 8px 12px; font-size: 0.88em; }
        }

        /* Tablet (769–1024). Sidebar visible, but tighter padding + 2-col summary + wrapped header.
           Below 769 the existing mobile rules take over, above 1024 the desktop layout is unchanged. */
        @media (min-width: 769px) and (max-width: 1024px) {
            .main { padding: 14px; }
            .header { flex-wrap: wrap; gap: 8px; padding: 12px 14px; }
            .header h1 { font-size: 1.25em; }
            .summary-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
            .form-row { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; gap: 12px; }
            .form-group { min-width: 0 !important; }
            .tabs { flex-wrap: wrap; }
            .tab-btn { font-size: 0.85em; padding: 8px 12px; }
            .card { padding: 16px; }
            .table-section { overflow-x: auto; -webkit-overflow-scrolling: touch; }
            input, select, textarea { font-size: 15px; }
        }

        /* Cross-cutting safety nets — apply at every viewport so nothing leaks. */
        html, body { max-width: 100%; overflow-x: hidden; }
        img, video, canvas, svg { max-width: 100%; height: auto; }
        table { max-width: 100%; }
        pre, code { white-space: pre-wrap; word-break: break-word; }
        .form-row { min-width: 0; }
        /* Generic dialogs/modals: never wider than the viewport, even on huge inline widths */
        [style*="max-width:900px"], [style*="max-width:800px"] { max-width: min(95vw, 900px) !important; }
        
        @media print {
            .sidebar, .header, .tabs, .btn, .nav-btn, .app-footer { display: none !important; }
            .app { grid-template-columns: 1fr; }
            .card { box-shadow: none; page-break-inside: avoid; }
        }
    
        /* GUIDED TOUR */
        .tour-overlay { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.6); z-index: 10000; display: none; }
        .tour-overlay.active { display: block; }
        .tour-highlight { position: relative; z-index: 10001; background: white; border-radius: 12px; box-shadow: 0 0 0 4000px rgba(0,0,0,0.6), 0 0 20px rgba(244,196,48,0.5); }
        .tour-tooltip { position: fixed; z-index: 10002; background: white; border-radius: 12px; padding: 24px; max-width: 400px; width: 90%; box-shadow: 0 10px 40px rgba(0,0,0,0.3); }
        .tour-tooltip h3 { color: #1a472a; margin: 0 0 8px; font-size: 1.15em; }
        .tour-tooltip p { color: #555; font-size: 0.92em; line-height: 1.6; margin: 0 0 16px; }
        .tour-tooltip .tour-step { color: #999; font-size: 0.8em; margin-bottom: 12px; }
        .tour-tooltip .tour-btns { display: flex; gap: 10px; justify-content: flex-end; }
        .tour-tooltip .tour-btns button { padding: 8px 20px; border-radius: 6px; border: none; cursor: pointer; font-family: inherit; font-weight: 600; font-size: 0.9em; }
        .tour-tooltip .tour-next { background: #1a472a; color: white; }
        .tour-tooltip .tour-skip { background: #eee; color: #666; }
        .tour-tooltip .tour-next:hover { background: #2d5f3a; }
        /* COPYRIGHT PROTECTION */
        body { -webkit-user-select: none; -moz-user-select: none; user-select: none; }
        input, textarea, select { -webkit-user-select: auto; -moz-user-select: auto; user-select: auto; }

        /* ====== TOAST + APP MODAL + FORM CARD (replaces alert/prompt/confirm popups) ====== */
        .toast {
          position: fixed; bottom: 24px; right: 24px;
          padding: 12px 20px; border-radius: 8px;
          font-size: 14px; font-weight: 500; color: #fff;
          background: var(--primary, #1a472a);
          box-shadow: 0 4px 16px rgba(0,0,0,0.18);
          opacity: 0; transform: translateY(8px);
          transition: opacity 0.2s, transform 0.2s;
          z-index: 99999; max-width: 90vw; pointer-events: none;
        }
        .toast.toast-visible { opacity: 1; transform: translateY(0); }
        .toast.toast-error   { background: var(--danger, #c41e3a); }
        .toast.toast-success { background: var(--primary, #1a472a); }

        .app-modal-backdrop {
          position: fixed; inset: 0; background: rgba(0,0,0,0.45);
          display: flex; align-items: center; justify-content: center;
          z-index: 99998; padding: 20px; animation: fadeIn 0.15s ease;
        }
        .app-modal {
          background: var(--bg, #fff); color: var(--text, #111827);
          border-radius: 12px; padding: 24px; max-width: 480px; width: 100%;
          box-shadow: 0 20px 50px rgba(0,0,0,0.25);
        }
        body.dark .app-modal { background: #222; color: #f0f0f0; }
        .app-modal-msg { font-size: 15px; line-height: 1.45; margin-bottom: 8px; }
        .app-modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; }
        .app-modal-input  {
          width: 100%; padding: 9px 12px;
          border: 1px solid var(--border, #e5e7eb); border-radius: 6px;
          font-size: 14px; box-sizing: border-box;
        }
        body.dark .app-modal-input { background: #333; color: #fff; border-color: #444; }
        @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

        /* Reusable form card pattern (Crew, Subcontractor, Client, Template name, etc.) */
        .form-card {
          background: var(--bg, #fff); border: 1px solid var(--border, #e5e7eb);
          border-radius: 10px; padding: 24px; margin: 16px 0; max-width: 560px;
        }
        body.dark .form-card { background: #222; border-color: #333; }
        .form-card h3 { margin: 0 0 18px 0; font-size: 18px; font-weight: 600; }
        .fg { margin-bottom: 14px; }
        .field-label { display: block; font-size: 13px; font-weight: 500; color: #6b7280; margin-bottom: 4px; }
        .field-label .required { color: var(--danger, #c41e3a); margin-left: 2px; }
        .fg input, .fg select, .fg textarea {
          width: 100%; padding: 9px 12px;
          border: 1px solid var(--border, #e5e7eb); border-radius: 6px;
          font-size: 14px; box-sizing: border-box;
        }
        .fg input:focus, .fg select:focus, .fg textarea:focus {
          outline: none; border-color: var(--primary, #1a472a);
          box-shadow: 0 0 0 3px rgba(26,71,42,0.12);
        }
        .field-error { font-size: 12px; color: var(--danger, #c41e3a); margin-top: 4px; min-height: 16px; }
        .form-actions { display: flex; gap: 10px; margin-top: 20px; }
        .btn-ghost {
          background: transparent; border: 1px solid var(--border, #e5e7eb);
          color: var(--text, #111827); padding: 9px 18px; border-radius: 6px; cursor: pointer;
        }
        body.dark .btn-ghost { color: #f0f0f0; border-color: #444; }
        /* Customer-link panel (Phase 5 polish) — group of 4 buttons on the estimate page header */
        .customer-link-panel { display: inline-flex; gap: 6px; flex-wrap: wrap; align-items: center; }