﻿
        :root {
            --primary: #2563eb;
            --success: #10b981;
            --danger: #ef4444;
            --bg: #f1f5f9;
            --card: #fff;
            --text: #1e293b;
            --muted: #64748b;
            --border: #e2e8f0;
        }

        [data-theme="dark"] {
            --bg: #0f172a;
            --card: #1e293b;
            --text: #f1f5f9;
            --muted: #94a3b8;
            --border: #334155;
        }

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

        body {
            font-family: 'Inter', sans-serif;
            background: var(--bg);
            color: var(--text);
            min-height: 100vh;
        }

        .header {
            background: linear-gradient(135deg, #2563eb, #7c3aed);
            padding: 8px 15px;
            color: #fff;
            position: sticky;
            top: 0;
            z-index: 100;
        }

        .header-inner {
            max-width: 1400px;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .header h1 {
            font-size: 0.95rem;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .header-nav {
            display: flex;
            gap: 8px;
        }

        .btn {
            padding: 6px 12px;
            border: none;
            border-radius: 5px;
            font-weight: 600;
            font-size: 0.75rem;
            cursor: pointer;
            display: inline-flex;
            align-items: center;
            gap: 4px;
            text-decoration: none;
        }

        .btn-white {
            background: #fff;
            color: var(--primary);
        }

        .btn-success {
            background: var(--success);
            color: #fff;
        }

        .btn-sm {
            padding: 4px 8px;
            font-size: 0.7rem;
        }

        .btn-danger {
            background: var(--danger);
            color: #fff;
        }

        .theme-btn {
            background: rgba(255, 255, 255, 0.2);
            border: none;
            color: #fff;
            width: 30px;
            height: 30px;
            border-radius: 50%;
            cursor: pointer;
        }

        .container {
            max-width: 1400px;
            margin: 0 auto;
            padding: 10px;
            display: grid;
            grid-template-columns: 380px 1fr;
            gap: 10px;
        }

        .panel {
            background: var(--card);
            border-radius: 8px;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
        }

        .panel-head {
            padding: 8px 12px;
            border-bottom: 1px solid var(--border);
            font-weight: 700;
            font-size: 0.8rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .panel-body {
            padding: 10px;
            max-height: calc(100vh - 90px);
            overflow-y: auto;
        }

        .tabs {
            display: flex;
            gap: 4px;
            margin-bottom: 10px;
            flex-wrap: wrap;
        }

        .tab {
            padding: 6px 10px;
            border: 1px solid var(--border);
            background: var(--card);
            border-radius: 5px;
            cursor: pointer;
            font-size: 0.7rem;
            font-weight: 600;
            color: var(--muted);
        }

        .tab:hover,
        .tab.active {
            background: var(--primary);
            color: #fff;
            border-color: var(--primary);
        }

        .tab-content {
            display: none;
        }

        .tab-content.active {
            display: block;
        }

        .section {
            margin-bottom: 12px;
        }

        .section-title {
            font-size: 0.65rem;
            font-weight: 700;
            color: var(--primary);
            text-transform: uppercase;
            margin-bottom: 6px;
            padding-bottom: 4px;
            border-bottom: 1px solid var(--border);
        }

        .form-group {
            margin-bottom: 8px;
        }

        .form-label {
            display: block;
            font-size: 0.7rem;
            font-weight: 600;
            color: var(--muted);
            margin-bottom: 3px;
        }

        .form-control {
            width: 100%;
            padding: 8px 10px;
            border: 1px solid var(--border);
            border-radius: 5px;
            font-size: 0.8rem;
            background: var(--card);
            color: var(--text);
        }

        .form-control:focus {
            outline: none;
            border-color: var(--primary);
        }

        .form-row {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 8px;
        }

        .form-row-3 {
            display: grid;
            grid-template-columns: 1fr 1fr 1fr;
            gap: 8px;
        }

        textarea.form-control {
            min-height: 50px;
            resize: vertical;
        }

        .options {
            display: flex;
            gap: 6px;
            flex-wrap: wrap;
        }

        .opt {
            padding: 5px 10px;
            border: 1px solid var(--border);
            border-radius: 5px;
            cursor: pointer;
            font-size: 0.7rem;
            font-weight: 500;
            background: var(--card);
            transition: all 0.2s;
        }

        .opt:hover {
            border-color: var(--primary);
        }

        .opt.sel {
            background: rgba(37, 99, 235, 0.1);
            border-color: var(--primary);
            color: var(--primary);
        }

        .opt input {
            display: none;
        }

        /* Work Items - Improved */
        .work-item {
            background: var(--card);
            border: 1px solid var(--border);
            border-radius: 8px;
            padding: 12px;
            margin-bottom: 10px;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
        }

        .work-item-head {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 10px;
            padding-bottom: 8px;
            border-bottom: 1px solid var(--border);
        }

        .work-item-num {
            font-weight: 700;
            color: var(--primary);
            font-size: 0.8rem;
        }

        .work-item-name {
            width: 100%;
            padding: 10px 12px;
            border: 1px solid var(--border);
            border-radius: 6px;
            font-size: 0.85rem;
            font-weight: 600;
            margin-bottom: 8px;
            background: var(--bg);
            color: var(--text);
        }

        .work-item-name:focus {
            outline: none;
            border-color: var(--primary);
        }

        .work-item-desc-group {
            margin-bottom: 10px;
        }

        .work-item-desc-label {
            display: flex;
            align-items: center;
            gap: 6px;
            font-size: 0.7rem;
            color: var(--muted);
            margin-bottom: 4px;
            cursor: pointer;
        }

        .work-item-desc-label input[type="checkbox"] {
            width: 14px;
            height: 14px;
        }

        .work-item-desc {
            width: 100%;
            padding: 8px 10px;
            border: 1px solid var(--border);
            border-radius: 6px;
            font-size: 0.8rem;
            background: var(--bg);
            color: var(--text);
            resize: vertical;
            min-height: 50px;
        }

        .work-item-desc:focus {
            outline: none;
            border-color: var(--primary);
        }

        .work-item-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 8px;
        }

        .work-item-field {
            display: flex;
            flex-direction: column;
            gap: 3px;
        }

        .work-item-field label {
            font-size: 0.65rem;
            color: var(--muted);
            font-weight: 500;
        }

        .work-item-field input,
        .work-item-field select {
            padding: 8px 10px;
            border: 1px solid var(--border);
            border-radius: 5px;
            font-size: 0.85rem;
            background: var(--card);
            color: var(--text);
            width: 100%;
        }

        .work-item-field input:focus,
        .work-item-field select:focus {
            outline: none;
            border-color: var(--primary);
        }

        .work-item-total-row {
            display: flex;
            justify-content: flex-end;
            align-items: center;
            margin-top: 10px;
            padding-top: 8px;
            border-top: 1px dashed var(--border);
        }

        .work-item-total-label {
            font-size: 0.75rem;
            color: var(--muted);
            margin-right: 8px;
        }

        .work-item-total {
            font-weight: 700;
            color: var(--success);
            font-size: 1rem;
            background: rgba(16, 185, 129, 0.1);
            padding: 6px 12px;
            border-radius: 6px;
        }

        .add-btn {
            width: 100%;
            padding: 12px;
            border: 2px dashed var(--primary);
            background: rgba(37, 99, 235, 0.05);
            color: var(--primary);
            border-radius: 8px;
            cursor: pointer;
            font-weight: 600;
            font-size: 0.85rem;
            transition: all 0.2s;
        }

        .add-btn:hover {
            background: rgba(37, 99, 235, 0.1);
        }

        /* Rich Editor - Fixed */
        .editor {
            border: 1px solid var(--border);
            border-radius: 6px;
            overflow: hidden;
            background: var(--card);
        }

        .editor-bar {
            display: flex;
            gap: 3px;
            padding: 6px;
            background: var(--bg);
            border-bottom: 1px solid var(--border);
        }

        .editor-bar button {
            width: 30px;
            height: 30px;
            border: none;
            background: var(--card);
            cursor: pointer;
            border-radius: 4px;
            color: var(--muted);
            font-size: 0.85rem;
        }

        .editor-bar button:hover {
            background: var(--primary);
            color: #fff;
        }

        .editor-area {
            min-height: 100px;
            padding: 10px;
            outline: none;
            font-size: 0.85rem;
            line-height: 1.6;
            color: var(--text);
            overflow-wrap: break-word;
            word-wrap: break-word;
        }

        .editor-area:empty::before {
            content: attr(data-ph);
            color: var(--muted);
        }

        .editor-area ol,
        .editor-area ul {
            padding-left: 20px;
            margin: 5px 0;
        }

        .editor-area li {
            margin-bottom: 3px;
        }

        /* Logo Preview */
        .logo-box {
            display: flex;
            gap: 10px;
            align-items: center;
        }

        .logo-preview {
            width: 80px;
            height: 50px;
            border: 2px dashed var(--border);
            border-radius: 6px;
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
            background: #fff;
        }

        .logo-preview img {
            max-width: 100%;
            max-height: 100%;
            object-fit: contain;
        }

        /* Signature */
        .sig-preview {
            width: 100px;
            height: 50px;
            border: 2px dashed var(--border);
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 6px;
            overflow: hidden;
            background: #fff;
        }

        .sig-preview img {
            max-width: 100%;
            max-height: 100%;
        }

        .sig-canvas-wrap {
            border: 1px solid var(--border);
            border-radius: 6px;
            padding: 8px;
            background: #fff;
        }

        .sig-canvas {
            width: 100%;
            height: 80px;
            border: 1px dashed #ccc;
            cursor: crosshair;
            display: block;
        }

        /* Preview */
        .preview-head {
            background: linear-gradient(135deg, #2563eb, #7c3aed);
            padding: 8px 12px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            color: #fff;
            border-radius: 8px 8px 0 0;
        }

        .preview-head h2 {
            font-size: 0.85rem;
        }

        .preview-body {
            padding: 15px;
            background: #475569;
            min-height: calc(100vh - 90px);
            overflow: auto;
            border-radius: 0 0 8px 8px;
        }

        /* Letter - Clean without justify */
        .letter {
            background: #fff !important;
            color: #000 !important;
            width: 210mm;
            padding: 12mm 15mm 15mm;
            margin: 0 auto;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
            font-family: 'Times New Roman', serif;
            font-size: 11pt;
            line-height: 1.4;
            position: relative;
            /* Keep header/footer consistent on all devices + PDF rendering */
            display: flex;
            flex-direction: column;
        }

        /* Wrapper so footer can sit at the bottom when content is short */
        .l-content {
            flex: 1 1 auto;
            min-height: 0;
        }

        .letter * {
            color: #000 !important;
        }

        .l-header {
            display: flex;
            gap: 12px;
            border-bottom: 3px double #000;
            padding-bottom: 10px;
            margin-bottom: 12px;
            align-items: center;
            min-height: 62px;
        }

        .l-header.center {
            flex-direction: column;
            align-items: center;
            text-align: center;
        }

        .l-header.right {
            flex-direction: row-reverse;
            text-align: right;
        }

        .l-logo {
            max-height: 55px;
            max-width: 80px;
            object-fit: contain;
        }

        .l-logo.portrait {
            max-height: 90px;
            max-width: 50px;
            height: auto;
            width: auto;
        }

        .l-logo.landscape {
            max-height: 40px;
            max-width: 200px;
            height: auto !important;
            width: auto;
        }

        .l-logo.small {
            max-height: 35px;
            max-width: 50px;
        }

        .l-logo.medium {
            max-height: 55px;
            max-width: 80px;
        }

        .l-logo.large {
            max-height: 75px;
            max-width: 110px;
        }

        /* Landscape + Size combinations */
        .l-logo.landscape.small {
            max-height: 30px;
            max-width: 120px;
        }

        .l-logo.landscape.medium {
            max-height: 40px;
            max-width: 180px;
        }

        .l-logo.landscape.large {
            max-height: 55px;
            max-width: 250px;
        }

        /* Portrait + Size combinations */
        .l-logo.portrait.small {
            max-height: 50px;
            max-width: 35px;
        }

        .l-logo.portrait.medium {
            max-height: 70px;
            max-width: 50px;
        }

        .l-logo.portrait.large {
            max-height: 100px;
            max-width: 70px;
        }

        .l-header.logo-above {
            flex-direction: column;
        }

        .l-header.logo-above .l-logo {
            margin-bottom: 8px;
        }

        .l-company {
            flex: 1;
        }

        .l-company-name {
            font-size: 14pt;
            font-weight: 700;
            margin-bottom: 2px;
        }

        .l-company-detail {
            font-size: 9pt;
            color: #555 !important;
            line-height: 1.3;
        }

        .l-title {
            text-align: center;
            margin: 12px 0;
        }

        .l-title h3 {
            font-size: 12pt;
            font-weight: 700;
            text-decoration: underline;
            margin-bottom: 2px;
        }

        .l-title-num {
            font-size: 10pt;
            color: #666 !important;
        }

        .l-meta {
            margin-bottom: 10px;
            font-size: 10pt;
        }

        .l-date {
            text-align: right;
            margin-bottom: 6px;
        }

        .l-recipient {
            margin-bottom: 8px;
        }

        .l-recipient p {
            margin-bottom: 1px;
        }

        .l-body {
            font-size: 10pt;
        }

        .l-body p {
            margin-bottom: 6px;
        }

        .l-table {
            width: 100%;
            border-collapse: collapse;
            margin: 10px 0;
            font-size: 9pt;
        }

        .l-table th,
        .l-table td {
            border: 1px solid #333;
            padding: 5px 6px;
            vertical-align: top;
        }

        .l-table th {
            background: #f0f0f0;
            font-weight: 700;
            text-align: center;
        }

        .l-table .no {
            width: 25px;
            text-align: center;
        }

        .l-table .desc {}

        .l-table .qty {
            width: 35px;
            text-align: center;
        }

        .l-table .unit {
            width: 45px;
            text-align: center;
        }

        .l-table .price {
            width: 75px;
            text-align: right;
        }

        .l-table .total {
            width: 85px;
            text-align: right;
        }

        .l-table tfoot td {
            font-weight: 700;
            background: #f8f8f8;
        }

        .l-notes {
            background: #fffbeb;
            border-left: 4px solid #d97706;
            padding: 8px 10px;
            margin: 10px 0;
            font-size: 9pt;
        }

        .l-notes-title {
            font-weight: 700;
            color: #92400e !important;
            margin-bottom: 4px;
        }

        .l-notes ol,
        .l-notes ul {
            margin: 0;
            padding-left: 18px;
        }

        .l-notes li {
            margin-bottom: 2px;
            line-height: 1.4;
        }

        .l-closing {
            margin-top: 15px;
            font-size: 10pt;
        }

        .l-sig {
            margin-top: 8px;
        }

        .l-sig.left {
            text-align: left;
        }

        .l-sig.center {
            text-align: center;
        }

        .l-sig.right {
            text-align: right;
        }

        .sig-block {
            display: inline-block;
            text-align: center;
            min-width: 140px;
        }

        .sig-space {
            height: 45px;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .sig-space img {
            max-height: 40px;
            max-width: 100px;
        }

        .sig-name {
            font-weight: 700;
            text-decoration: underline;
        }

        .l-footer {
            /* In-flow footer: stable for mobile + html2pdf, avoids overlap */
            position: static;
            margin-top: auto;
            padding-top: 6px;
            text-align: center;
            font-size: 8pt;
            color: #666 !important;
            border-top: 1px solid #ddd;
        }

        .l-timestamp {
            font-size: 7pt;
            color: #aaa !important;
            margin-top: 3px;
        }

        @media (max-width: 1000px) {
            .container {
                grid-template-columns: 1fr;
            }

            .letter {
                width: 100%;
                padding: 8mm;
            }
        }

        @media print {

            .header,
            .panel,
            .preview-head {
                display: none !important;
            }

            .container {
                display: block;
                padding: 0;
            }

            .preview-body {
                padding: 0;
                background: #fff;
            }

            .letter {
                box-shadow: none;
            }
        }
    

/* ===== PDF SAFE MODE (Cross-device) ===== */
@media screen {
  .letter {
    /* A4 width in px at 96dpi (approx) for consistent html2canvas rendering */
    width: 794px;
    max-width: 100%;
    margin-left: auto;
    margin-right: auto;
  }
}

@media print {
  body { background: #fff !important; }
  .letter {
    width: 210mm !important;
    max-width: 210mm !important;
    margin: 0 !important;
    padding: 15mm !important;
    box-shadow: none !important;
  }
}

/* =========================
   V2 FINAL: Mobile Preview Scaling
   - Keeps A4 layout intact
   - Scales preview on small screens so header/footer are not cut
   - Does NOT affect PDF generation (download uses #letterContent)
   ========================= */

.preview-body {
    /* keep existing look, add centering + safe scrolling */
    display: flex;
    justify-content: center;
    align-items: flex-start;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.preview-scale {
    transform-origin: top center;
    will-change: transform;
}

/* Desktop / tablet default */
@media (min-width: 769px) {
    .preview-scale { transform: scale(1); }
}

/* Mobile: scale down the A4 preview so it's fully visible */
@media (max-width: 768px) {
    .preview-scale { transform: scale(0.88); }
}

@media (max-width: 480px) {
    .preview-scale { transform: scale(0.78); }
}
