/* PDF Form Overlay Editor - Google Docs-like Styles */

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

html {
    overflow: hidden;
    height: 100%;
    touch-action: pan-x pan-y;
}

/* Support for safe areas and dynamic viewport */
@supports (height: 100dvh) {
    html {
        height: 100dvh;
    }
}

:root {
    --primary-color: #1a73e8;
    --primary-hover: #1557b0;
    --danger-color: #d93025;
    --danger-hover: #b5261c;
    --bg-color: #f8f9fa;
    --surface-color: #ffffff;
    --border-color: #dadce0;
    --text-primary: #202124;
    --text-secondary: #5f6368;
    --text-muted: #80868b;
    --header-bg: #ffffff;
    --sidebar-bg: #ffffff;
    --shadow-sm: 0 1px 2px 0 rgba(60,64,67,0.3), 0 1px 3px 1px rgba(60,64,67,0.15);
    --shadow-md: 0 1px 3px 0 rgba(60,64,67,0.3), 0 4px 8px 3px rgba(60,64,67,0.15);
    --radius: 8px;
}

body {
    font-family: 'Google Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    background: var(--bg-color);
    color: var(--text-primary);
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

#app {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0; /* Critical: allows flex child to shrink below content size */
    overflow: hidden;
    margin-bottom: 3rem; /* Space for fixed footer */
}

/* Header / Controls */
#appHeader {
    background: var(--header-bg);
    padding: 0.75rem 1rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border-color);
    z-index: 100;
    flex-shrink: 0;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 1.5rem;
}

#appHeader h1 {
    font-size: 1.125rem;
    font-weight: 500;
    color: var(--text-primary);
}

.hint-message {
    font-size: 0.75rem;
    color: var(--text-muted);
    opacity: 0;
    transition: opacity 0.3s;
}

.hint-message.visible {
    opacity: 1;
}

#controls {
    display: flex;
    gap: 0.75rem;
    align-items: center;
}

/* Upload button */
.upload-btn {
    background: var(--surface-color);
    color: var(--primary-color);
    padding: 0.5rem 1rem;
    border-radius: var(--radius);
    border: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s, box-shadow 0.2s;
}

.upload-btn:hover {
    background: #f1f3f4;
    box-shadow: var(--shadow-sm);
}

.upload-btn input {
    display: none;
}

.upload-btn svg {
    stroke: var(--primary-color);
}

/* Signature button */
.signature-btn {
    background: var(--primary-color);
    color: white;
    padding: 0.5rem 1rem;
    border-radius: var(--radius);
    border: none;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s, box-shadow 0.2s;
}

.signature-btn:hover {
    background: var(--primary-hover);
    box-shadow: var(--shadow-sm);
}

.signature-btn svg {
    stroke: currentColor;
}

/* Language Widget */
#languageWidget {
    display: flex;
    align-items: center;
}

#languageSelect {
    background: var(--surface-color);
    color: var(--text-secondary);
    border: 1px solid var(--border-color);
    padding: 0.4rem 0.75rem;
    border-radius: var(--radius);
    font-size: 0.875rem;
    cursor: pointer;
    transition: background 0.2s, box-shadow 0.2s;
}

#languageSelect:hover {
    background: #f1f3f4;
    box-shadow: var(--shadow-sm);
}

#languageSelect:focus {
    outline: none;
    border-color: var(--primary-color);
}

/* RTL Support */
[dir="rtl"] #appHeader {
    direction: rtl;
}

[dir="rtl"] .upload-btn {
    flex-direction: row-reverse;
}

[dir="rtl"] #downloadBtn {
    flex-direction: row-reverse;
}

[dir="rtl"] .action-btn {
    flex-direction: row-reverse;
}

[dir="rtl"] #sidebar {
    border-left: none;
    border-right: 1px solid var(--border-color);
}

[dir="rtl"] #mainContent {
    flex-direction: row-reverse;
}

/* Keep PDF container LTR - PDFs should always render left-to-right */
[dir="rtl"] #pdfContainer {
    direction: ltr;
}

[dir="rtl"] .page-wrapper {
    direction: ltr;
}

[dir="rtl"] .overlay {
    direction: ltr;
}

[dir="rtl"] .form-input {
    direction: ltr;
}

/* Zoom controls */
#zoomControls {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    background: var(--surface-color);
    border: 1px solid var(--border-color);
    padding: 0.25rem;
    border-radius: var(--radius);
}

#zoomControls button {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    font-size: 1rem;
    width: 28px;
    height: 28px;
    cursor: pointer;
    border-radius: 4px;
    transition: background 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

#zoomControls button:hover:not(:disabled) {
    background: #f1f3f4;
}

#zoomControls button:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

#zoomLevel {
    min-width: 50px;
    text-align: center;
    font-size: 0.8125rem;
    color: var(--text-secondary);
}

/* Download button */
#downloadBtn {
    background: var(--primary-color);
    color: #fff;
    border: none;
    padding: 0.5rem 1rem;
    border-radius: var(--radius);
    cursor: pointer;
    font-weight: 500;
    font-size: 0.875rem;
    transition: background 0.3s ease, box-shadow 0.2s, transform 0.2s;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

#downloadBtn:hover:not(:disabled) {
    background: var(--primary-hover);
    box-shadow: var(--shadow-sm);
}

#downloadBtn:disabled {
    background: #dadce0;
    color: #80868b;
    cursor: not-allowed;
}

#downloadBtn svg {
    stroke: currentColor;
    transition: all 0.3s ease;
}

/* Download button states */
#downloadBtn.loading svg {
    animation: spin 1s linear infinite;
}

@keyframes downloadSpin {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}

#downloadBtn.success {
    background: #34a853;
    cursor: not-allowed;
}

#downloadBtn.success:hover {
    background: #34a853;
}

#downloadBtn.success svg {
    animation: checkmarkPop 0.3s ease;
}

@keyframes checkmarkPop {
    0% {
        transform: scale(0);
        opacity: 0;
    }
    50% {
        transform: scale(1.2);
    }
    100% {
        transform: scale(1);
        opacity: 1;
    }
}

#downloadBtn.ready {
    background: #34a853;
    animation: pulseGlow 0.6s ease;
}

#downloadBtn.ready:hover {
    background: #2d8e47;
}

@keyframes pulseGlow {
    0% {
        box-shadow: 0 0 0 0 rgba(26, 115, 232, 0.7);
    }
    50% {
        box-shadow: 0 0 0 8px rgba(26, 115, 232, 0);
    }
    100% {
        box-shadow: 0 0 0 0 rgba(26, 115, 232, 0);
    }
}

#downloadBtn.error {
    background: var(--danger-color);
}

/* Spinner circle animation */
.spinner-circle {
    animation: spinnerRotate 1s linear infinite;
    transform-origin: center;
}

@keyframes spinnerRotate {
    from {
        transform: rotate(0deg);
        stroke-dashoffset: 31.4;
    }
    50% {
        stroke-dashoffset: 0;
    }
    to {
        transform: rotate(360deg);
        stroke-dashoffset: -31.4;
    }
}

/* Main content area with sidebar */
#mainContent {
    flex: 1;
    display: flex;
    overflow: hidden;
    min-height: 0;
}

/* PDF Container */
#pdfContainer {
    flex: 1;
    overflow-y: scroll;
    overflow-x: auto;
    padding: 1.5rem;
    padding-bottom: 4rem; /* Extra space at bottom to clear footer */
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
    background: #e8eaed;
    touch-action: pan-x pan-y pinch-zoom;
    transition: background-color 0.2s ease;
}

#pdfContainer.drag-over {
    background: #d0e3f5;
    border: 2px dashed #0969da;
}

#placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    min-height: 400px;
    color: var(--text-secondary);
    text-align: center;
}

#placeholder.hidden {
    display: none;
}

.placeholder-icon {
    margin-bottom: 1rem;
    opacity: 0.5;
}

.placeholder-icon svg {
    stroke: var(--text-muted);
}

#placeholder p {
    font-size: 1rem;
    margin-bottom: 0.25rem;
    color: var(--text-secondary);
}

#placeholder .hint {
    font-size: 0.8125rem;
    color: var(--text-muted);
}

/* Central Upload Button */
.central-upload-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
    background: var(--primary-color);
    color: #fff;
    padding: 1.25rem 2rem;
    border-radius: var(--radius);
    border: none;
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s, box-shadow 0.2s, transform 0.2s;
    box-shadow: var(--shadow-md);
    margin-top: 1rem;
}

.central-upload-btn:hover {
    background: var(--primary-hover);
    box-shadow: 0 4px 12px rgba(26, 115, 232, 0.4);
    transform: translateY(-2px);
}

.central-upload-btn input {
    display: none;
}

.central-upload-btn svg {
    stroke: currentColor;
}

/* Page wrapper - contains canvas + overlay */
.page-wrapper {
    position: relative;
    box-shadow: var(--shadow-md);
    background: #fff;
    border-radius: 2px;
}

.page-wrapper canvas {
    display: block;
}

/* Overlay layer - sits on top of canvas */
.overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    cursor: crosshair;
}

/* Sidebar */
#sidebar {
    width: 280px;
    background: var(--sidebar-bg);
    border-left: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

#sidebarContent {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
}

#sidebarPlaceholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 200px;
    text-align: center;
    color: var(--text-secondary);
}

#sidebarPlaceholder.hidden {
    display: none;
}

.sidebar-icon {
    margin-bottom: 0.75rem;
    opacity: 0.4;
}

.sidebar-icon svg {
    stroke: var(--text-muted);
}

#sidebarPlaceholder p {
    margin-bottom: 0.25rem;
    font-size: 0.875rem;
}

#sidebarPlaceholder .hint {
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* Field Editor */
#fieldEditor {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

#fieldEditor.hidden {
    display: none;
}

.field-editor-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid var(--border-color);
    margin-bottom: 0.25rem;
}

#fieldEditor h3 {
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-primary);
    margin: 0;
}

.field-type-subtitle {
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-muted);
    background: var(--bg-color);
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    text-transform: capitalize;
}

.field-group {
    display: flex;
    flex-direction: column;
    gap: 0.375rem;
}

.field-group > label {
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.field-group select,
.field-group input[type="text"],
.field-group input[type="number"] {
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    font-size: 0.875rem;
    color: var(--text-primary);
    background: var(--surface-color);
    transition: border-color 0.2s, box-shadow 0.2s;
}

.field-group select:focus,
.field-group input[type="text"]:focus,
.field-group input[type="number"]:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 2px rgba(26, 115, 232, 0.2);
}

.field-group select {
    cursor: pointer;
}

/* Color input wrapper */
.color-input-wrapper {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.color-input-wrapper input[type="color"] {
    width: 40px;
    height: 32px;
    padding: 2px;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    cursor: pointer;
    background: var(--surface-color);
}

.color-input-wrapper input[type="color"]::-webkit-color-swatch-wrapper {
    padding: 2px;
}

.color-input-wrapper input[type="color"]::-webkit-color-swatch {
    border-radius: 2px;
    border: none;
}

/* Font size input wrapper */
.font-size-input-wrapper {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.font-size-input-wrapper input[type="number"] {
    width: 70px;
    text-align: center;
}

.font-size-input-wrapper span {
    font-size: 0.8125rem;
    color: var(--text-secondary);
}

#colorHexDisplay {
    font-size: 0.8125rem;
    color: var(--text-secondary);
    font-family: monospace;
}

#fieldFontFamily {
    font-size: 0.875rem;
}

#fieldFontFamily option[value="Open Sans"] { font-family: 'Open Sans', sans-serif; }
#fieldFontFamily option[value="Rubik"] { font-family: 'Rubik', sans-serif; }
#fieldFontFamily option[value="Heebo"] { font-family: 'Heebo', sans-serif; }
#fieldFontFamily option[value="Assistant"] { font-family: 'Assistant', sans-serif; }
#fieldFontFamily option[value="Varela Round"] { font-family: 'Varela Round', sans-serif; }
#fieldFontFamily option[value="David Libre"] { font-family: 'David Libre', serif; }
#fieldFontFamily option[value="Frank Ruhl Libre"] { font-family: 'Frank Ruhl Libre', serif; }
#fieldFontFamily option[value="Secular One"] { font-family: 'Secular One', sans-serif; }

/* Text direction toggle button */
.direction-toggle-btn {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    background: var(--surface-color);
    color: var(--text-primary);
    font-size: 0.875rem;
    cursor: pointer;
    transition: border-color 0.2s, background 0.2s;
}

.direction-toggle-btn:hover {
    background: #f1f3f4;
    border-color: var(--primary-color);
}

.direction-toggle-btn.rtl-active {
    background: rgba(26, 115, 232, 0.1);
    border-color: var(--primary-color);
}

.direction-toggle-btn .direction-icon {
    stroke: var(--text-secondary);
    transition: transform 0.2s;
}

.direction-toggle-btn.rtl-active .direction-icon {
    stroke: var(--primary-color);
}

/* Checkbox label styling */
.checkbox-label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    font-size: 0.875rem;
    color: var(--text-primary);
}

.checkbox-label input[type="checkbox"] {
    width: 16px;
    height: 16px;
    accent-color: var(--primary-color);
    cursor: pointer;
}

/* Position and Size inputs */
.position-inputs,
.size-inputs {
    display: flex;
    gap: 0.5rem;
}

.pos-input,
.size-input {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 0.25rem;
    font-size: 0.8125rem;
    color: var(--text-secondary);
}

.pos-input input,
.size-input input {
    width: 60px;
    padding: 0.375rem 0.5rem;
    text-align: center;
}

/* Field Actions */
.field-actions {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-top: 0.5rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border-color);
}

.action-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.5rem 1rem;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    background: var(--surface-color);
    color: var(--text-secondary);
    font-size: 0.8125rem;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s, border-color 0.2s;
}

.action-btn:hover {
    background: #f1f3f4;
    border-color: #c0c4c8;
}

.action-btn svg {
    stroke: currentColor;
}

.action-btn.danger {
    color: var(--danger-color);
    border-color: rgba(217, 48, 37, 0.3);
}

.action-btn.danger:hover {
    background: rgba(217, 48, 37, 0.08);
    border-color: var(--danger-color);
}

/* Status bar */
#statusBar {
    background: #f8f9fa;
    padding: 0.5rem 1rem;
    display: flex;
    justify-content: space-between;
    font-size: 0.6875rem;
    color: var(--text-muted);
    border-top: 1px solid var(--border-color);
}

/* =========================
   Form Input Styles
   ========================= */

.form-input {
    position: absolute;
    background: transparent;
    border: 1px solid rgba(26, 115, 232, 0.15);
    cursor: move;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: border-color 0.15s, box-shadow 0.15s, background 0.15s;
    border-radius: 2px;
}

/* Edge resize zones (zone 2) */
.edge-resize {
    position: absolute;
    z-index: 5;
}

/* Edge sides */
.edge-resize.edge-top {
    top: -8px;
    left: 8px;
    right: 8px;
    height: 12px;
    cursor: n-resize;
}

.edge-resize.edge-bottom {
    bottom: -8px;
    left: 8px;
    right: 8px;
    height: 12px;
    cursor: s-resize;
}

.edge-resize.edge-left {
    left: -8px;
    top: 8px;
    bottom: 8px;
    width: 12px;
    cursor: w-resize;
}

.edge-resize.edge-right {
    right: -8px;
    top: 8px;
    bottom: 8px;
    width: 12px;
    cursor: e-resize;
}

/* Edge corners */
.edge-resize.edge-top-left {
    top: -8px;
    left: -8px;
    width: 16px;
    height: 16px;
    cursor: nw-resize;
}

.edge-resize.edge-top-right {
    top: -8px;
    right: -8px;
    width: 16px;
    height: 16px;
    cursor: ne-resize;
}

.edge-resize.edge-bottom-left {
    bottom: -8px;
    left: -8px;
    width: 16px;
    height: 16px;
    cursor: sw-resize;
}

.edge-resize.edge-bottom-right {
    bottom: -8px;
    right: -8px;
    width: 16px;
    height: 16px;
    cursor: se-resize;
}

/* Show edges only when selected 
.form-input:not(.selected) .edge-resize {
    pointer-events: none;
}*/

.form-input:hover {
    border-color: rgba(26, 115, 232, 0.7);
    background: rgba(26, 115, 232, 0.08);

}

.form-input.selected {
    border: 2px solid var(--primary-color);
    box-shadow: 0 0 0 2px rgba(26, 115, 232, 0.25);
    background: rgba(26, 115, 232, 0.1);
    z-index: 1000 !important;
}

.form-input.locked {
    opacity: 0.6;
    cursor: not-allowed;
    background: rgba(128, 134, 139, 0.1);
    border-color: rgba(128, 134, 139, 0.4);
}

.form-input.locked::after {
    content: '🔒';
    position: absolute;
    top: -12px;
    left: -12px;
    font-size: 10px;
}

/* Type badge (zone 3 - drag handle) */
.type-badge {
    position: absolute;
    top: -20px;
    left: 0;
    background: var(--primary-color);
    color: #fff;
    font-size: 9px;
    padding: 3px 8px;
    border-radius: 3px;
    text-transform: uppercase;
    font-weight: 500;
    letter-spacing: 0.3px;
    cursor: move;
    opacity: 0;
    transition: opacity 0.15s;
    user-select: none;
    z-index: 10;
    display: inline-flex;
}

.drag-cursor-icon {
    margin-right: 4px;
}

.type-badge.drag-handle {
    pointer-events: auto;
}

.form-input:hover .type-badge,
.form-input.selected .type-badge {
    opacity: 1;
}

.form-input.text-focused .type-badge {
    opacity: 0.6;
}

/* Resize handle */
.resize-handle {
    position: absolute;
    bottom: -5px;
    right: -5px;
    width: 10px;
    height: 10px;
    background: var(--primary-color);
    border: 2px solid #fff;
    border-radius: 2px;
    cursor: se-resize;
    opacity: 0;
    transition: opacity 0.15s;
    box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}

.form-input.selected .resize-handle {
    opacity: 1;
}

/* Hide resize handle for text inputs (use edge resize instead) */
.form-input.text-input .resize-handle {
    display: none;
}

/* Text input - wrapper focused (not typing) */
.form-input.text-input.selected:not(.text-focused) {
    cursor: default;
}

/* When text is focused (typing mode), dim the border */
.form-input.text-input.text-focused {
    border-color: rgba(26, 115, 232, 0.3);
    box-shadow: none;
}

.form-input.text-input.text-focused .edge-resize {
    pointer-events: none;
}

/* Text input */
.form-input.text-input input {
    width: 100%;
    height: 100%;
    border: none;
    background: transparent;
    padding: 4px 8px;
    font-size: 14px;
    color: var(--text-primary);
    outline: none;
}

.form-input.text-input input::placeholder {
    color: var(--text-muted);
}

/* Checkbox input */
.form-input.checkbox-input {
    --checkbox-border-width: 1px;
    width: 24px !important;
    height: 24px !important;
    min-width: 24px;
    min-height: 24px;
    padding: 0;
    border-radius: 2px;
    background: #fff;
    border-width: var(--checkbox-border-width);
}

.form-input.checkbox-input input {
    position: absolute;
    opacity: 0;
    width: 100%;
    height: 100%;
    margin: 0;
    cursor: pointer;
    z-index: 1;
}

.form-input.checkbox-input .checkbox-display {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
}

.form-input.checkbox-input .checkbox-display svg {
    width: 14px;
    height: 14px;
    opacity: 0;
    stroke: #000;
    stroke-width: 3;
    fill: none;
}

.form-input.checkbox-input input:checked + .checkbox-display svg {
    opacity: 1;
}

/* Signature input */
.form-input.signature-input {
    background: transparent;
    flex-direction: column;
    transform: scale(0.75);
    transform-origin: center center;
    transition: transform 0.15s ease-out, background 0.15s ease-out;
}

.signature-input .drag-handle {
    transform: scale(calc(1 / 0.75)) translateX(+14px);
    transform-origin: right bottom;
    transition: transform 0.15s ease-out, background 0.15s ease-out;
}
.signature-input.selected .drag-handle {
    transform: scale(1);
} 

.form-input.signature-input.selected {
    transform: scale(1);
    background: rgba(255, 255, 255, 0.95);
}

.form-input.signature-input canvas {
    width: 100%;
    height: 100%;
    cursor: crosshair;
}

.signature-controls {
    position: absolute;
    bottom: -26px;
    left: 0;
    display: flex;
    gap: 4px;
    opacity: 0;
    transition: opacity 0.15s;
    pointer-events: none;
    z-index: 20;
}

.form-input.selected .signature-controls {
    opacity: 1;
    pointer-events: auto;
}

.signature-controls button {
    background: var(--surface-color);
    color: var(--text-secondary);
    border: 1px solid var(--border-color);
    padding: 2px 8px;
    font-size: 10px;
    border-radius: 3px;
    cursor: pointer;
    transition: background 0.15s;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.signature-controls button:hover {
    background: #f1f3f4;
}

.signature-controls button.done-btn {
    background: var(--primary-color);
    color: #fff;
    border-color: var(--primary-color);
}

.signature-controls button.done-btn:hover {
    background: var(--primary-hover);
    border-color: var(--primary-hover);
}

/* Image input */
.form-input.image-input {
    background: transparent;
    flex-direction: column;
    overflow: visible;
}

.form-input.image-input.selected {
    background: rgba(255, 255, 255, 0.95);
}

.form-input.image-input .image-container {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    overflow: hidden;
}

.form-input.image-input .image-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    color: var(--text-muted);
    pointer-events: none;
    width: 100%;
    height: 100%;
}

.form-input.image-input .image-placeholder-icon {
    font-size: 24px;
    opacity: 0.7;
}

.form-input.image-input .image-placeholder-text {
    font-size: 10px;
    text-align: center;
    opacity: 0.8;
}

.form-input.image-input .image-preview {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    pointer-events: none;
}

.form-input.image-input .image-clear-btn {
    position: absolute;
    top: -8px;
    right: -8px;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: var(--danger-color);
    color: white;
    border: none;
    font-size: 14px;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.15s;
    z-index: 25;
}

.form-input.image-input.selected .image-clear-btn {
    opacity: 1;
}

.form-input.image-input .image-clear-btn:hover {
    background: var(--danger-hover);
}

.image-input .drag-handle {
    position: absolute;
    top: -18px;
    left: 0;
    z-index: 30;
}

/* =========================
   Focus & Accessibility
   ========================= */

.form-input:focus-within {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}

button:focus,
select:focus,
input:focus {
    outline: 2px solid var(--primary-color);
    outline-offset: 1px;
}

/* =========================
   Loading & Error States
   ========================= */

.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.9);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
}

/* Download Countdown Overlay */
.download-countdown-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.85);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10000;
    animation: fadeIn 0.3s ease-out;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.countdown-content {
    text-align: center;
    color: #fff;
    padding: 2rem;
}

.countdown-icon {
    margin-bottom: 1.5rem;
    opacity: 0.8;
}

.countdown-icon svg {
    stroke: #fff;
}

.countdown-timer {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    background: var(--primary-color);
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 1.5rem;
    box-shadow: 0 4px 20px rgba(66, 133, 244, 0.4);
    animation: pulse 1s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.05); }
}

.countdown-timer #countdownNumber {
    font-size: 3rem;
    font-weight: 700;
    color: #fff;
}

.countdown-text {
    font-size: 1.125rem;
    color: rgba(255, 255, 255, 0.9);
    margin: 0;
}

.countdown-text #countdownSeconds {
    font-weight: 600;
    color: var(--primary-color);
}

.download-ad-container {
    width: 300px;
    min-height: 250px;
    margin: 0 auto 1.5rem;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

.download-ad-container .adsbygoogle {
    width: 300px;
    height: 250px;
}

.mobile-download-btn {
    display: inline-block;
    padding: 1rem 2.5rem;
    background: var(--primary-color);
    color: #fff;
    font-size: 1.25rem;
    font-weight: 600;
    border-radius: 8px;
    text-decoration: none;
    cursor: pointer;
    margin-top: 1rem;
    box-shadow: 0 4px 15px rgba(66, 133, 244, 0.4);
    transition: transform 0.2s, box-shadow 0.2s;
}

.mobile-download-btn:hover,
.mobile-download-btn:active {
    transform: scale(1.05);
    box-shadow: 0 6px 20px rgba(66, 133, 244, 0.5);
}

.loading-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--border-color);
    border-top-color: var(--primary-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

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

.error-toast {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--danger-color);
    color: #fff;
    padding: 0.875rem 1.5rem;
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    z-index: 9999;
    animation: slideUp 0.3s ease-out;
    font-size: 0.875rem;
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateX(-50%) translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateX(-50%) translateY(0);
    }
}

/* =========================
   Cookie Consent Banner
   ========================= */

.cookie-consent {
    position: fixed;
    bottom: 0;
    bottom: env(safe-area-inset-bottom, 0);
    left: 0;
    left: env(safe-area-inset-left, 0);
    right: 0;
    right: env(safe-area-inset-right, 0);
    background: var(--text-primary);
    color: #fff;
    padding: 1rem 1.5rem;
    padding-bottom: max(1rem, env(safe-area-inset-bottom, 1rem));
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 100000;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.2);
}

.cookie-content {
    display: flex;
    align-items: center;
    gap: 1.5rem;
    max-width: 1200px;
    flex-wrap: wrap;
    justify-content: center;
}

.cookie-content p {
    margin: 0;
    font-size: 0.875rem;
    line-height: 1.5;
    text-align: center;
}

.cookie-accept-btn {
    background: var(--primary-color);
    color: #fff;
    border: none;
    padding: 0.625rem 1.5rem;
    border-radius: var(--radius);
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s;
    white-space: nowrap;
}

.cookie-accept-btn:hover {
    background: var(--primary-hover);
}

/* Overlay to block interaction when consent is pending */
body.consent-pending #app {
    pointer-events: none;
    filter: blur(2px);
    user-select: none;
}

body.consent-pending .cookie-consent {
    pointer-events: auto;
}

/* =========================
   Input Type Selection Menu
   ========================= */

.input-type-menu {
    position: fixed;
    display: flex;
    gap: 4px;
    background: var(--surface-color);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    padding: 6px;
    box-shadow: var(--shadow-md);
    z-index: 10000;
    animation: menuFadeIn 0.15s ease-out;
}

@keyframes menuFadeIn {
    from {
        opacity: 0;
        transform: scale(0.9);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

.input-type-menu-item {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border: none;
    background: transparent;
    border-radius: 6px;
    cursor: pointer;
    transition: background 0.15s, transform 0.1s;
}

.input-type-menu-item:hover {
    background: #f1f3f4;
    transform: scale(1.05);
}

.input-type-menu-item:active {
    transform: scale(0.95);
}

.input-type-icon {
    font-size: 1.25rem;
    color: var(--text-primary);
    font-weight: 600;
    line-height: 1;
}

.input-type-menu-item[data-type="text"] .input-type-icon {
    font-family: 'Times New Roman', serif;
    font-style: italic;
}

.input-type-menu-item[data-type="checkbox"] .input-type-icon {
    color: var(--primary-color);
}

.input-type-menu-item[data-type="signature"] .input-type-icon {
    font-size: 1.1rem;
}

/* Sidebar toggle handle - hidden on desktop */
.sidebar-toggle {
    display: none;
}

/* =========================
   Footer
   ========================= */

.app-footer {
    position: fixed;
    bottom: 0;
    bottom: env(safe-area-inset-bottom, 0);
    left: 0;
    left: env(safe-area-inset-left, 0);
    right: 0;
    right: env(safe-area-inset-right, 0);
    background: var(--surface-color);
    border-top: 1px solid var(--border-color);
    padding: 0.75rem 1rem;
    padding-bottom: max(0.75rem, env(safe-area-inset-bottom, 0.75rem));
    text-align: center;
    z-index: 50;
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.app-footer p {
    margin: 0;
}

.app-footer a {
    color: var(--primary-color);
    text-decoration: none;
    font-weight: 500;
    transition: color 0.2s;
}

.app-footer a:hover {
    color: var(--primary-hover);
    text-decoration: underline;
}

/* =========================
   Download Complete Popup
   ========================= */

.popup-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100001;
    animation: fadeIn 0.2s ease-out;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

.popup-content {
    background: var(--surface-color);
    border-radius: 12px;
    padding: 2rem;
    max-width: 450px;
    width: 90%;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
    text-align: center;
    position: relative;
    animation: slideUp 0.3s ease-out;
}

.popup-close-btn {
    position: absolute;
    top: 1rem;
    right: 1rem;
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 0.5rem;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.2s, color 0.2s;
}

.popup-close-btn:hover {
    background-color: #f1f3f4;
    color: var(--text-primary);
}

[dir="rtl"] .popup-close-btn {
    right: auto;
    left: 1rem;
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.popup-icon {
    margin-bottom: 1.5rem;
    display: flex;
    justify-content: center;
}

.popup-content h2 {
    font-size: 1.5rem;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 0.5rem;
}

.popup-content p {
    font-size: 0.95rem;
    color: var(--text-secondary);
    margin-bottom: 2rem;
}

.popup-actions {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.popup-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.875rem 1.5rem;
    border: none;
    border-radius: var(--radius);
    font-size: 0.95rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}

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

.popup-btn.primary:hover {
    background: var(--primary-hover);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(26, 115, 232, 0.3);
}

.popup-btn.secondary {
    background: var(--surface-color);
    color: var(--text-primary);
    border: 2px solid var(--border-color);
}

.popup-btn.secondary:hover {
    background: #f8f9fa;
    border-color: var(--primary-color);
    color: var(--primary-color);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.popup-btn svg {
    flex-shrink: 0;
}

/* =========================
   Responsive
   ========================= */

@media (max-width: 768px) {
    /* Main layout changes for mobile */
    #mainContent {
        flex-direction: column;
    }
    
    /* RTL support for mobile layout */
    [dir="rtl"] #mainContent {
        flex-direction: column;
    }
    
    /* Sidebar becomes bottom bar */
    #sidebar {
        width: 100%;
        max-height: 180px;
        border-left: none;
        border-top: 1px solid var(--border-color);
        flex-shrink: 0;
        display: none;
        transition: max-height 0.3s ease-out;
        overflow: hidden;
    }
    
    /* Expanded sidebar on mobile */
    #sidebar.expanded {
        max-height: 70vh;
        overflow-y: auto;
    }
    
    /* Show sidebar toggle on mobile */
    .sidebar-toggle {
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 0.25rem;
        cursor: pointer;
        background: var(--bg-color);
        border-bottom: 1px solid var(--border-color);
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
    }
    
    .sidebar-toggle .toggle-arrow {
        stroke: var(--text-muted);
        transition: transform 0.3s ease-out;
    }
    
    #sidebar.expanded .sidebar-toggle .toggle-arrow {
        transform: rotate(180deg);
    }
    
    /* Show sidebar only when PDF is loaded */
    body.pdf-loaded #sidebar {
        display: flex;
    }

    /* Body uses dvh for proper mobile viewport handling */
    body {
        height: 100dvh;
        max-height: 100dvh;
    }
    
    [dir="rtl"] #sidebar {
        border-right: none;
        border-top: 1px solid var(--border-color);
    }
    
    #sidebarContent {
        padding: 0.75rem;
    }
    
    /* Hide sidebar placeholder on mobile when field editor is visible */
    #sidebarPlaceholder {
        height: auto;
        padding: 1rem 0;
    }
    
    #sidebarPlaceholder p:last-child {
        display: none;
    }
    
    /* Hide position/size groups and value input on mobile */
    .position-group,
    .size-group,
    #textValueGroup {
        display: none !important;
    }
    
    /* Field editor compact layout */
    #fieldEditor {
        gap: 0.5rem;
    }
    
    .field-editor-header {
        padding-bottom: 0.5rem;
        margin-bottom: 0;
    }
    
    /* Field groups inline */
    #fieldEditor .field-group {
        flex-direction: row;
        align-items: center;
        gap: 0.5rem;
    }
    
    #fieldEditor .field-group > label {
        min-width: 60px;
        margin: 0;
    }
    
    #fieldEditor .field-group select,
    #fieldEditor .field-group .color-input-wrapper,
    #fieldEditor .field-group .direction-toggle-btn {
        flex: 1;
    }
    
    /* Action buttons as icon-only row */
    .field-actions {
        flex-direction: row;
        gap: 0.5rem;
        margin-top: 0.25rem;
        padding-top: 0.5rem;
    }
    
    .action-btn {
        flex: 1;
        padding: 0.5rem;
        min-width: 44px;
    }
    
    .action-btn span {
        display: none;
    }
    
    .action-btn svg {
        width: 18px;
        height: 18px;
    }
    
    /* Header adjustments */
    .header-left {
        gap: 0.5rem;
    }
    
    #appHeader h1 {
        font-size: 0.8rem;
    }
    
    .hint-message {
        display: none;
    }
    
    /* Controls compact */
    #controls {
        gap: 0.5rem;
    }
    
    .upload-btn span,
    #downloadBtn span {
        display: none;
    }
    
    .upload-btn,
    #downloadBtn {
        padding: 0.5rem;
    }

    .signature-btn {
        flex-direction: column !important;
        gap: 0 !important;
        padding: 2px !important;
        background: transparent !important;
        border: none !important;
        box-shadow: none !important;
        min-width: 32px !important;
        height: 38px !important;
        justify-content: flex-start !important;
        position: relative !important;
        overflow: visible !important;
    }

    .signature-btn:hover {
        background: #f1f3f4 !important;
    }

    .signature-btn svg {
        width: 18px !important;
        height: 18px !important;
        stroke: var(--primary-color) !important;
        margin-top: 4px !important;
    }

    .signature-btn span {
        display: block !important;
        position: absolute !important;
        bottom: -8px !important;
        left: 50% !important;
        transform: translateX(-50%) !important;
        font-size: 7px !important;
        line-height: 1 !important;
        background: var(--primary-color) !important;
        color: white !important;
        padding: 1px 3px !important;
        border-radius: 3px !important;
        white-space: nowrap !important;
        text-transform: uppercase !important;
        font-weight: bold !important;
        box-shadow: 0 1px 2px rgba(0,0,0,0.1) !important;
        pointer-events: none !important;
        text-transform: uppercase !important;
        font-weight: bold !important;
        box-shadow: 0 1px 2px rgba(0,0,0,0.1) !important;
    }
    
    #zoomControls {
        padding: 0.125rem;
    }
    
    #zoomLevel {
        min-width: 32px;
        font-size: 0.7rem;
    }
    
    #zoomControls button {
        width: 24px;
        height: 24px;
        font-size: 1rem;
        padding: 0;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    /* Status bar compact */
    #statusBar {
        padding: 0.25rem 0.75rem;
        font-size: 0.625rem;
    }
    
    /* PDF container adjustments */
    #pdfContainer {
        padding: 0.75rem;
        align-items: flex-start;
    }
    
    /* Keep placeholder centered on mobile */
    #placeholder {
        align-self: center;
        width: 100%;
        min-height: auto;
    }
    
    /* Always show drag badge on mobile */
    .form-input .type-badge {
        opacity: 1;
    }
    
    /* Footer mobile adjustments */
    .app-footer {
        padding: 0.75rem;
        padding-bottom: calc(0.75rem + env(safe-area-inset-bottom, 0px));
        font-size: 0.75rem;
    }
    
    /* Main content padding for footer visibility */
    #mainContent {
        padding-bottom: calc(3rem + env(safe-area-inset-bottom, 0px));
    }
    
    /* Popup mobile adjustments */
    .popup-content {
        padding: 1.5rem;
        padding-bottom: calc(1.5rem + env(safe-area-inset-bottom, 0px));
        width: 95%;
        max-width: 380px;
        margin-bottom: env(safe-area-inset-bottom, 0px);
    }
    
    .popup-content h2 {
        font-size: 1.25rem;
    }
    
    .popup-content p {
        font-size: 0.875rem;
        margin-bottom: 1.5rem;
    }
    
    .popup-btn {
        padding: 0.75rem 1.25rem;
        font-size: 0.875rem;
    }
    
    /* Ensure sidebar respects safe areas when at bottom */
    #sidebar {
        padding-bottom: env(safe-area-inset-bottom, 0px);
    }
}
