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

/* Layout tokens */
:root {
    --page-padding: 20px;
    --header-padding-y: 16px;
    --header-padding-x: 20px;
    --logo-height: 60px;
    --header-height: calc(var(--logo-height) + (var(--header-padding-y) * 2));
    --content-top: calc(var(--header-height) + var(--page-padding));
}

/* Site Header */
.site-header {
    background-color: #164A70;
    padding: var(--header-padding-y) var(--header-padding-x);
    position: relative;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    /* Full-bleed header even when body has padding */
    width: calc(100% + (var(--page-padding) * 2));
    margin-top: calc(var(--page-padding) * -1);
    margin-left: calc(var(--page-padding) * -1);
    margin-right: calc(var(--page-padding) * -1);
    margin-bottom: var(--page-padding);
}

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

.logo-link {
    display: flex;
    align-items: center;
    text-decoration: none;
}

.site-logo {
    height: var(--logo-height);
    width: auto;
}

.header-nav {
    display: flex;
    align-items: center;
    gap: 24px;
}

.nav-link {
    color: white;
    text-decoration: none;
    font-size: 16px;
    font-weight: 500;
    transition: opacity 0.3s ease;
}

.nav-link:hover {
    opacity: 0.8;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background: #164A70;
    min-height: 100vh;
    padding: var(--page-padding);
}

/* Home page: hero should be flush to header and viewport edges */
body.page-home .site-header {
    margin-bottom: 0;
}

body.page-home .hero-image-container {
    width: calc(100% + (var(--page-padding) * 2));
    margin-left: calc(var(--page-padding) * -1);
    margin-right: calc(var(--page-padding) * -1);
}

/* Home page: editor layout should behave like template pages, but without overlapping the hero.
   We use a sticky sidebar instead of fixed positioning so it only sticks once you scroll past the hero. */
body.page-home .container {
    max-width: 1400px;
    margin: 0 auto;
    padding: var(--page-padding);
    display: grid;
    grid-template-columns: 400px 1fr;
    gap: 30px;
    align-items: start;
}

body.page-home .form-section {
    position: sticky;
    top: var(--page-padding);
    left: auto;
    width: 400px;
    min-width: 400px;
    max-width: 400px;
    max-height: calc(100vh - (var(--page-padding) * 2));
}

body.page-home .preview-section {
    margin-left: 0;
    margin-right: 0;
    width: auto;
}

/* Home page: on narrower desktop + mobile, match template-page stacking behavior */
@media (max-width: 1024px) {
    body.page-home .container {
        display: block;
        max-width: 1400px;
        margin: 0 auto;
        padding: var(--page-padding);
    }

    body.page-home .preview-section {
        width: 100%;
    }
}

.container {
    max-width: none;
    width: 100%;
    margin: 0;
    display: block;
    position: relative;
}

@media (max-width: 1024px) {
    .container {
        grid-template-columns: 1fr;
    }
    
    .form-section {
        position: relative !important;
        width: 100% !important;
        left: auto !important;
        top: auto !important;
        max-width: 100% !important;
        min-width: auto !important;
        max-height: none !important;
        margin-bottom: 20px;
    }
}

.form-section {
    background: white;
    border-radius: 12px;
    padding: 30px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.1);
    position: fixed;
    top: var(--content-top); /* Account for fixed header + page padding */
    left: 20px;
    width: 400px;
    min-width: 400px;
    max-width: 400px;
    z-index: 100;
    max-height: calc(100vh - (var(--header-height) + (var(--page-padding) * 2))); /* header + top/bottom padding */
    overflow-y: auto;
}

.preview-section {
    background: #f5f5f5;
    border-radius: 12px;
    padding: 40px 40px 20px 40px; /* Reduced bottom padding from 40px to 20px */
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.1);
    margin-left: 430px; /* Form width (400px) + gap (30px) */
    margin-right: 20px; /* Match body padding */
    width: calc(100% - 450px); /* Container width minus margin-left (430px) and margin-right (20px) */
    max-width: none;
    box-sizing: border-box;
    position: relative;
    overflow-x: hidden;
}

@media (max-width: 1024px) {
    :root {
        --page-padding: 30px;
    }

    body {
        padding: var(--page-padding);
    }
    
    .preview-section {
        margin-left: 0;
        margin-right: 0;
        width: 100%;
        padding: 20px 20px 12px 20px; /* Reduced bottom padding from 20px to 12px */
    }
    
    .form-section {
        padding: 20px;
    }
}

@media (max-width: 480px) {
    :root {
        --page-padding: 16px;
        --header-padding-y: 12px;
        --header-padding-x: 16px;
        --logo-height: 48px;
    }

    .header-nav {
        gap: 16px;
    }

    .nav-link {
        font-size: 14px;
    }
}

h1 {
    color: #333;
    margin-bottom: 10px;
    font-size: 28px;
}

.subtitle {
    color: #666;
    margin-bottom: 30px;
    font-size: 14px;
}

.form-group {
    margin-bottom: 20px;
    position: relative;
}

.set-today-link {
    font-size: 11px;
    color: #2b6cb0;
    text-decoration: none;
    cursor: pointer;
    opacity: 0.7;
    transition: opacity 0.2s;
}

.set-today-link:hover {
    opacity: 1;
    text-decoration: underline;
}

.set-today-link.hidden {
    display: none;
}

label {
    display: block;
    margin-bottom: 8px;
    color: #333;
    font-weight: 500;
    font-size: 14px;
}

input[type="text"],
input[type="date"],
textarea,
select {
    width: 100%;
    padding: 12px;
    border: 2px solid #e0e0e0;
    border-radius: 6px;
    font-size: 14px;
    transition: border-color 0.3s;
    font-family: inherit;
    resize: vertical;
}

input[type="text"]:focus,
input[type="date"]:focus,
textarea:focus,
select:focus {
    outline: none;
    border-color: #667eea;
}

/* Logic branch select styling */
select.logic-branch-select {
    cursor: pointer;
    background-color: #fff;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23667eea' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 36px;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
}

/* Date input should be narrower */
#dateInput {
    max-width: 140px;
}

.preview-section h2 {
    color: #333;
    margin-bottom: 20px;
    font-size: 22px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.btn-print {
    background: #667eea;
    color: white;
    font-size: 18px;
    padding: 10px 14px;
    line-height: 1;
    position: relative;
}

@media (max-width: 768px) {
    .btn-print {
        padding: 8px 11px;
        font-size: 16px;
    }
}

.btn-print:hover {
    background: #5568d3;
}

.btn-print::after {
    content: 'Print';
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    background: #333;
    color: white;
    padding: 6px 10px;
    border-radius: 4px;
    font-size: 12px;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
    margin-bottom: 6px;
}

.btn-print:hover::after {
    opacity: 1;
}

.font-size-controls {
    display: flex;
    align-items: center;
    gap: 8px;
}

.font-size-button {
    background: #667eea;
    color: white;
    border: none;
    border-radius: 6px;
    padding: 6px 12px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s;
    font-family: inherit;
    min-width: 36px;
}

.font-size-button:hover {
    background: #5568d3;
}

.font-size-display {
    font-size: 14px;
    color: #333;
    min-width: 50px;
    text-align: center;
}

.letter-preview-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0px; /* Gap is now controlled dynamically by JavaScript with negative margins */
    width: 100%;
    overflow-x: hidden;
    overflow-y: visible;
    position: relative;
    min-height: 200px; /* Ensure container has height for loader positioning */
}

@media (max-width: 1024px) {
    .letter-preview {
        margin-bottom: 0;
    }
}

.letter-preview {
    background: #ffffff;
    border: 1px solid #e0e0e0;
    border-radius: 0;
    /* Standard 1-inch margins on all sides (96px at 96 DPI) */
    padding: 96px;
    position: relative;
    font-family: 'Times New Roman', serif;
    line-height: 1.8;
    color: #1a1a1a;
    white-space: pre-wrap;
    word-wrap: break-word;
    overflow-wrap: break-word;
    word-break: break-word;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    /* Fixed dimensions matching letter size (8.5" x 11") */
    width: 8.5in;
    height: 11in;
    margin: 0;
    overflow: hidden;
    box-sizing: border-box;
    /* Transform origin for scaling */
    transform-origin: top center;
    /* Hint to browser for performance optimization */
    will-change: contents, transform;
    /* Will be scaled by JavaScript to fit available space */
    transition: opacity 0.3s ease;
    /* Position relative for page number positioning */
    position: relative;
}

.letter-preview.fading {
    opacity: 0.3;
}

.page-number {
    position: absolute;
    bottom: 48px; /* Position inside the 96px padding (1 inch margin) */
    left: 50%;
    transform: translateX(-50%);
    font-family: 'Times New Roman', serif;
    font-size: 10pt;
    color: #666;
    pointer-events: none;
    user-select: none;
}

.field-arrow {
    position: absolute;
    left: 20px; /* Position in the left margin */
    width: 0;
    height: 0;
    border-top: 8px solid transparent;
    border-bottom: 8px solid transparent;
    border-left: 12px solid #667eea;
    pointer-events: none;
    transition: top 0.3s ease, opacity 0.3s ease;
    opacity: 0;
    z-index: 10;
}

.field-arrow.visible {
    opacity: 1;
}

.preview-loader {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 10000;
    display: none;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 12px;
}

.preview-loader.active {
    display: flex !important;
    opacity: 1 !important;
    visibility: visible !important;
}

.preview-loader-spinner {
    width: 60px;
    height: 60px;
    border: 6px solid rgba(102, 126, 234, 0.2);
    border-top-color: #667eea;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
    background: white;
}

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

.thumbnail-container {
    display: none;
    flex-wrap: wrap;
    justify-content: center;
    gap: 10px;
    margin-bottom: 20px;
    padding: 10px;
}

@media (max-width: 1024px) {
    .thumbnail-container {
        display: flex;
    }
}

.thumbnail {
    width: 80px;
    height: 103px; /* Maintain 8.5:11 aspect ratio */
    background: #ffffff;
    border: 2px solid #e0e0e0;
    border-radius: 4px;
    cursor: pointer;
    overflow: hidden;
    position: relative;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    transition: all 0.2s ease;
    flex-shrink: 0;
}

.thumbnail:hover {
    border-color: #667eea;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}

.thumbnail.active {
    border-color: #667eea;
    border-width: 3px;
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.thumbnail-content {
    width: 100%;
    height: 100%;
    transform-origin: top left;
    pointer-events: none;
    font-family: 'Times New Roman', serif;
    font-size: 6pt;
    line-height: 1.8;
    color: #1a1a1a;
    white-space: pre-wrap;
    word-wrap: break-word;
    overflow-wrap: break-word;
    word-break: break-word;
    padding: 9px; /* Scaled down 1-inch margin (96px * 80/816 ≈ 9px) */
    box-sizing: border-box;
    overflow: hidden;
}

.thumbnail-number {
    position: absolute;
    bottom: 4px;
    right: 4px;
    background: rgba(102, 126, 234, 0.9);
    color: white;
    font-size: 10px;
    font-weight: bold;
    padding: 2px 6px;
    border-radius: 3px;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.letter-preview *{
    word-wrap: break-word;
    overflow-wrap: break-word;
    word-break: break-word;
    max-width: 100%;
}

.placeholder {
    background-color: #e8f5e9;
    padding: 2px 4px;
    border-radius: 3px;
    font-weight: 500;
}

.placeholder-highlight {
    background-color: #e5e5e5;
    padding: 2px 0;
    border-radius: 2px;
    transition: background-color 1s ease-out;
}

.placeholder-highlight[data-field]:hover {
    background-color: #d0d0d0;
}

.highlight {
    background-color: #ffeb3b;
    padding: 2px 0;
    border-radius: 2px;
    transition: background-color 1s ease-out;
}

.highlight.fade-out {
    background-color: transparent;
}

.highlight.fade-to-placeholder {
    background-color: #e5e5e5;
}

.underlined-word {
    background-color: rgba(176, 188, 243, 0.078);
    border: 3px solid #667eea;
    border-radius: 8px;
    padding: 2px 4px;
    margin: -2px -4px;
    display: inline-block;
}

.actions {
    margin-top: 12px;
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    justify-content: center;
}

@media (max-width: 768px) {
    .actions {
        gap: 6px;
    }
}

@media (min-width: 1400px) {
    .actions {
        margin-top: 16px;
    }
}

button {
    padding: 12px 24px;
    border: none;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s;
}

@media (max-width: 768px) {
    button {
        padding: 10px 16px;
        font-size: 13px;
    }
}

.btn-copy {
    background: #667eea;
    color: white;
    font-size: 18px;
    padding: 10px 14px;
    line-height: 1;
    position: relative;
}

@media (max-width: 768px) {
    .btn-copy {
        padding: 8px 11px;
        font-size: 16px;
    }
}

.btn-copy:hover {
    background: #5568d3;
}

.btn-copy::after {
    content: 'Copy to clipboard';
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    background: #333;
    color: white;
    padding: 6px 10px;
    border-radius: 4px;
    font-size: 12px;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
    margin-bottom: 6px;
}

.btn-copy:hover::after {
    opacity: 1;
}

.btn-download {
    background: #48bb78;
    color: white;
    position: relative;
}

.btn-download:hover {
    background: #38a169;
}

.btn-download::after {
    content: 'Download as plain text';
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    background: #333;
    color: white;
    padding: 6px 10px;
    border-radius: 4px;
    font-size: 12px;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
    margin-bottom: 6px;
}

.btn-download:hover::after {
    opacity: 1;
}

.btn-download-pdf {
    background: #e53e3e;
    color: white;
    position: relative;
}

.btn-download-pdf:hover {
    background: #c53030;
}

.btn-download-pdf::after {
    content: 'Download as PDF';
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    background: #333;
    color: white;
    padding: 6px 10px;
    border-radius: 4px;
    font-size: 12px;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
    margin-bottom: 6px;
}

.btn-download-pdf:hover::after {
    opacity: 1;
}

.btn-download-word {
    background: #2b6cb0;
    color: white;
    position: relative;
}

.btn-download-word:hover {
    background: #2c5282;
}

.btn-download-word::after {
    content: 'Download as Word document';
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    background: #333;
    color: white;
    padding: 6px 10px;
    border-radius: 4px;
    font-size: 12px;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
    margin-bottom: 6px;
}

.btn-download-word:hover::after {
    opacity: 1;
}

.notification {
    position: fixed;
    top: 20px;
    right: 20px;
    background: #48bb78;
    color: white;
    padding: 12px 20px;
    border-radius: 6px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    display: none;
    z-index: 1000;
}

.notification.show {
    display: block;
    animation: slideIn 0.3s ease-out;
}

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

/* Draft Controls - Auto-save & Clear */
.draft-controls {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 16px;
    padding-top: 12px;
    position: relative;
}

.btn-clear-draft {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 4px 0;
    background: transparent;
    border: none;
    color: #d32f2f;
    font-size: 13px;
    font-weight: 400;
    cursor: pointer;
    transition: color 0.2s ease;
}

.btn-clear-draft:hover {
    color: #b71c1c;
}

.btn-clear-draft .clear-icon {
    font-size: 12px;
    font-weight: 500;
}

.auto-save-indicator {
    position: absolute;
    right: 0;
}

.auto-save-indicator {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    color: #999;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.auto-save-indicator.visible {
    opacity: 1;
}

.auto-save-indicator .save-dot {
    width: 6px;
    height: 6px;
    background: #48bb78;
    border-radius: 50%;
    animation: pulse 1s ease-in-out;
}

@keyframes pulse {
    0% {
        transform: scale(1);
        opacity: 1;
    }
    50% {
        transform: scale(1.5);
        opacity: 0.7;
    }
    100% {
        transform: scale(1);
        opacity: 1;
    }
}

/* Draft restored indicator - positioned in white preview box */
.draft-restored-indicator {
    position: absolute;
    top: 8px;
    right: 8px;
    background: rgba(72, 187, 120, 0.95);
    color: white;
    padding: 6px 12px;
    border-radius: 4px;
    font-size: 12px;
    font-weight: 500;
    opacity: 0;
    transform: translateY(-6px);
    transition: opacity 0.3s ease, transform 0.3s ease;
    z-index: 10;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
}

.draft-restored-indicator.show {
    opacity: 1;
    transform: translateY(0);
}

/* ============================================
   SIGNATURE FIELD STYLES
   ============================================ */

.signature-field-group {
    margin-bottom: 20px;
}

.signature-capture-box {
    background: #fafafa;
    border: 2px dashed #d0d0d0;
    border-radius: 10px;
    padding: 20px;
    text-align: center;
    min-height: 140px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: border-color 0.3s, background-color 0.3s;
}

.signature-capture-box:hover {
    border-color: #667eea;
    background: #f8f9ff;
}

.signature-qr-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
}

.signature-qr-code {
    background: white;
    padding: 8px;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.signature-qr-code img {
    display: block;
}

.signature-qr-text {
    font-size: 12px;
    color: #666;
    font-weight: 500;
}

.signature-preview-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    width: 100%;
}

.signature-image {
    max-width: 100%;
    max-height: 100px;
    border-radius: 4px;
}

.signature-clear-btn {
    background: transparent;
    border: none;
    color: #e53e3e;
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
    padding: 6px 12px;
    border-radius: 4px;
    transition: background 0.2s, color 0.2s;
}

.signature-clear-btn:hover {
    background: #fee;
    color: #c53030;
}

/* Signature in preview (document) */
.signature-preview-inline {
    display: inline-block;
    max-width: 200px;
    max-height: 60px;
    vertical-align: bottom;
    margin-bottom: -4px;
}

.signature-placeholder {
    display: inline-block;
    min-width: 150px;
    border-bottom: 1px solid #999;
    color: #999;
    font-style: italic;
    padding-bottom: 2px;
}

