:root {
    --bg-color: #f5f5f7;
    --surface: #ffffff;
    --primary: #0071e3;
    --primary-hover: #0077ed;
    --secondary: #e8e8ed;
    --secondary-hover: #d2d2d7;
    --text: #1d1d1f;
    --text-muted: #86868b;
    --border: #d2d2d7;
    --radius: 12px;
    --grid-gap: 5mm;
}

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    margin: 0;
    padding: 0;
    background-color: var(--bg-color);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
}

/* UI Controls */
.controls {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 24px;
    background: rgba(255, 255, 255, 0.85);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 100;
}

.header h1 {
    margin: 0 0 4px 0;
    font-size: 20px;
    font-weight: 600;
}

.help-text {
    margin: 0;
    font-size: 13px;
    color: var(--text-muted);
}

.actions {
    display: flex;
    gap: 12px;
}

.file-input {
    display: none;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 16px;
    border-radius: var(--radius);
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    border: none;
    transition: all 0.2s ease;
    user-select: none;
    -webkit-user-select: none;
}

.btn .icon {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
}

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

.btn.primary:active {
    background-color: var(--primary-hover);
    transform: scale(0.98);
}

.btn.secondary {
    background-color: var(--secondary);
    color: var(--text);
}

.btn.secondary:active {
    background-color: var(--secondary-hover);
    transform: scale(0.98);
}

/* Modals */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.5);
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
    z-index: 1000;
    justify-content: center;
    align-items: center;
}

.modal.active {
    display: flex;
}

.modal-content {
    background: var(--surface);
    border-radius: var(--radius);
    width: 90%;
    max-width: 400px;
    box-shadow: 0 20px 40px rgba(0,0,0,0.2);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.modal-content.edit-content {
    max-width: 500px;
}

.modal-header {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-header h2 {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
}

.icon-btn {
    background: none;
    border: none;
    font-size: 20px;
    cursor: pointer;
    color: var(--text-muted);
    padding: 4px;
}

.modal-body {
    padding: 20px;
    flex: 1;
    overflow-y: auto;
}

.setting-group {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.setting-group label {
    font-weight: 600;
    font-size: 15px;
}

input[type="range"] {
    width: 100%;
    accent-color: var(--primary);
}

.modal-footer {
    padding: 16px 20px;
    border-top: 1px solid var(--border);
}

.edit-canvas-container {
    background: #e8e8ed;
    padding: 20px;
    border-radius: 8px;
    display: flex;
    justify-content: center;
    margin-bottom: 20px;
}

.edit-controls {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.control-row {
    display: flex;
    align-items: center;
    gap: 12px;
}

.control-row label {
    font-weight: 500;
    width: 50px;
}

.control-actions {
    display: flex;
    gap: 12px;
}

.control-actions .btn {
    flex: 1;
}

.text-center { text-align: center; }
.mt-2 { margin-top: 8px; }

/* Empty State */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 80px 20px;
    color: var(--text-muted);
    text-align: center;
    height: 60vh;
}

.empty-state .icon-large {
    width: 64px;
    height: 64px;
    margin-bottom: 16px;
    color: var(--border);
}

.empty-state h2 {
    margin: 0 0 8px 0;
    font-size: 20px;
    color: var(--text);
}

.empty-state p {
    margin: 0;
    font-size: 15px;
}

/* Pages Layout */
.pages-container {
    padding: 40px 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 40px;
    min-height: calc(100vh - 80px);
}

.page {
    width: 210mm;
    height: 297mm;
    background: white;
    box-shadow: 0 8px 30px rgba(0,0,0,0.08);
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    overflow: hidden;
}

.grid {
    display: grid;
    grid-template-columns: 95mm 95mm;
    grid-template-rows: 140mm 140mm;
    gap: var(--grid-gap);
}

.cell {
    width: 95mm;
    height: 140mm;
    border: 1px dashed var(--border);
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    position: relative;
    background: #fafafa;
    transition: background 0.2s ease;
}

.cell.empty { }

.cell.drag-over {
    background-color: #e6f2ff;
    border: 2px solid var(--primary);
}

.photo-wrapper {
    width: 100%;
    height: 100%;
    position: relative;
    cursor: pointer;
}

body.reorder-mode .photo-wrapper {
    touch-action: none; 
    cursor: grab;
}

body.reorder-mode .photo-wrapper:active {
    cursor: grabbing;
}

.photo {
    position: absolute;
    left: 50%;
    top: 50%;
    object-fit: cover;
    pointer-events: none;
    /* transition mostly for rotation; pan should be instant for 60fps */
    transition: width 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94), 
                height 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

/* Let pan transform bypass slow transition to stay attached to finger */
.photo.animate-transform {
    transition: width 0.3s, height 0.3s, transform 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

/* Print Styles */
@media print {
    @page {
        size: A4 portrait;
        margin: 0;
    }
    
    body {
        background-color: white;
        margin: 0;
        padding: 0;
    }
    
    .no-print {
        display: none !important;
    }
    
    .pages-container {
        padding: 0;
        gap: 0;
        display: block;
        min-height: auto;
    }
    
    .page {
        width: 210mm;
        height: 297mm;
        box-shadow: none;
        page-break-after: always;
        break-after: page;
        margin: 0;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    
    .cell {
        border: none !important;
        background: transparent !important;
    }
}
