/* ===== Reset & Base ===== */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg: #e8e4e0;
    --toolbar-bg: #2c2c2c;
    --toolbar-text: #f0ece8;
    --page-shadow: 0 2px 20px rgba(0,0,0,0.15);
    --accent: #c47a4a;
    --accent-hover: #a8623a;
    --radius: 6px;
}

html, body {
    height: 100%;
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    background: var(--bg);
    color: #333;
    overflow: hidden;
}

/* ===== Toolbar ===== */
.toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 56px;
    padding: 0 20px;
    background: var(--toolbar-bg);
    color: var(--toolbar-text);
    user-select: none;
}

.toolbar-left, .toolbar-center, .toolbar-right {
    display: flex;
    align-items: center;
    gap: 16px;
}

.toolbar-group {
    display: flex;
    align-items: center;
    gap: 10px;
}

.toolbar-separator {
    width: 1px;
    height: 28px;
    background: rgba(255,255,255,0.15);
    margin: 0 4px;
}

.logo {
    font-size: 1.3rem;
    font-weight: 700;
    letter-spacing: -0.5px;
    color: var(--accent);
}

.logo-sub {
    font-weight: 400;
    font-size: 0.85rem;
    color: var(--toolbar-text);
    opacity: 0.6;
    margin-left: 4px;
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    border: none;
    border-radius: var(--radius);
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s, opacity 0.15s;
}

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

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

.btn-ghost {
    background: transparent;
    color: var(--toolbar-text);
    padding: 8px;
    border-radius: var(--radius);
}

.btn-ghost:hover {
    background: rgba(255,255,255,0.1);
}

/* Save button */
.btn-save {
    background: #3a8f5c;
    color: white;
}

.btn-save:hover {
    background: #2e7349;
}

/* Fit Selector */
.fit-selector {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.85rem;
    opacity: 0.5;
    pointer-events: none;
    transition: opacity 0.3s;
}

.fit-selector.active {
    opacity: 1;
    pointer-events: auto;
}

.fit-selector label {
    white-space: nowrap;
}

.fit-selector select {
    padding: 6px 10px;
    border: 1px solid rgba(255,255,255,0.2);
    border-radius: var(--radius);
    background: rgba(255,255,255,0.1);
    color: var(--toolbar-text);
    font-size: 0.85rem;
    cursor: pointer;
}

.fit-selector select:hover {
    border-color: rgba(255,255,255,0.4);
}

.fit-selector select option {
    background: var(--toolbar-bg);
    color: var(--toolbar-text);
}

/* ===== Workspace ===== */
.workspace {
    display: flex;
    justify-content: center;
    align-items: center;
    height: calc(100vh - 56px);
    padding: 30px;
    overflow: auto;
}

.page-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100%;
}

/* ===== Page A4 ===== */
.page {
    position: relative;
    background: white;
    box-shadow: var(--page-shadow);
    overflow: hidden;
}

.a4-portrait {
    /* A4 ratio: 210mm x 297mm = 1:1.4142 */
    width: calc((100vh - 116px) / 1.4142);
    height: calc(100vh - 116px);
    max-width: 794px;  /* ~210mm at 96dpi */
    max-height: 1123px; /* ~297mm at 96dpi */
}

.a4-landscape {
    /* A4 paysage: 297mm x 210mm */
    width: calc((100vh - 116px) * 1.4142);
    height: calc(100vh - 116px);
    max-width: 1123px;  /* ~297mm at 96dpi */
    max-height: 794px;  /* ~210mm at 96dpi */
}

.page canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

/* Placeholder */
.placeholder {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 16px;
    color: #999;
    font-size: 0.95rem;
    text-align: center;
    line-height: 1.6;
    pointer-events: none;
    transition: opacity 0.3s;
}

.placeholder.hidden {
    opacity: 0;
}

/* Drag over effect */
.page.drag-over {
    outline: 3px dashed var(--accent);
    outline-offset: -3px;
}

/* ===== Button secondary ===== */
.btn-secondary {
    background: rgba(255,255,255,0.12);
    color: var(--toolbar-text);
    border: 1px solid rgba(255,255,255,0.2);
}

.btn-secondary:hover {
    background: rgba(255,255,255,0.2);
}

/* ===== Modal Overlay ===== */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.6);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    backdrop-filter: blur(4px);
}

.modal-overlay[hidden] {
    display: none;
}

.modal {
    background: #fff;
    border-radius: 12px;
    width: 720px;
    max-width: 95vw;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
    overflow: hidden;
}

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

.modal-header h2 {
    font-size: 1.1rem;
    font-weight: 600;
}

.modal-close-btn {
    color: #999;
    font-size: 1.4rem;
    line-height: 1;
    padding: 4px 8px;
}

.modal-close-btn:hover {
    color: #333;
    background: #f0f0f0;
}

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

.modal-workspace {
    display: flex;
    gap: 20px;
    align-items: flex-start;
}

/* Crop area */
.crop-area {
    position: relative;
    width: 380px;
    height: 380px;
    background: #f5f5f5;
    border-radius: 8px;
    overflow: hidden;
    cursor: grab;
    flex-shrink: 0;
    border: 2px solid #e0e0e0;
}

.crop-area:active {
    cursor: grabbing;
}

.crop-area canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

.crop-placeholder {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 10px;
    color: #aaa;
    font-size: 0.9rem;
    pointer-events: none;
}

.crop-placeholder.hidden {
    display: none;
}

/* Preview panel */
.preview-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
}

.preview-label {
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #999;
    font-weight: 600;
}

.preview-frame {
    width: 200px;
    height: 200px;
    display: flex;
    justify-content: center;
    align-items: center;
    background: #fafafa;
    border-radius: 8px;
    border: 2px dashed #ddd;
}

.preview-frame canvas {
    width: 180px;
    height: 180px;
}

/* Shape bar */
.shape-bar {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-top: 16px;
    padding-top: 16px;
    border-top: 1px solid #eee;
}

.shape-bar label {
    font-size: 0.85rem;
    color: #666;
    white-space: nowrap;
    font-weight: 500;
}

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

.shape-btn {
    width: 42px;
    height: 42px;
    padding: 6px;
    border: 2px solid #e0e0e0;
    border-radius: 8px;
    background: #fff;
    color: #bbb;
    cursor: pointer;
    transition: all 0.15s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.shape-btn:hover {
    border-color: var(--accent);
    color: var(--accent);
}

.shape-btn.active {
    border-color: var(--accent);
    background: rgba(196, 122, 74, 0.08);
    color: var(--accent);
}

.shape-btn svg {
    width: 26px;
    height: 26px;
}

/* Border bar */
.border-bar {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid #eee;
}

.border-bar label {
    font-size: 0.85rem;
    color: #666;
    white-space: nowrap;
    font-weight: 500;
}

.color-picker {
    width: 36px;
    height: 36px;
    padding: 2px;
    border: 2px solid #e0e0e0;
    border-radius: 6px;
    cursor: pointer;
    background: #fff;
}

.color-picker:hover {
    border-color: var(--accent);
}

.border-none-label {
    display: flex;
    align-items: center;
    gap: 5px;
    font-weight: 400 !important;
    cursor: pointer;
}

.border-none-label input {
    cursor: pointer;
}

/* Modal footer */
.modal-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 20px;
    border-top: 1px solid #eee;
    background: #fafafa;
}

.modal-actions {
    display: flex;
    gap: 8px;
}

.btn-ghost-dark {
    background: transparent;
    color: #666;
    border: 1px solid #ddd;
}

.btn-ghost-dark:hover {
    background: #f0f0f0;
}

.btn-primary:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* ===== Snap grid (invisible, shown as guides) ===== */
.snap-guide-h, .snap-guide-v {
    position: absolute;
    background: rgba(196, 122, 74, 0.35);
    pointer-events: none;
    z-index: 50;
    transition: opacity 0.1s;
}

.snap-guide-h {
    left: 0;
    right: 0;
    height: 1px;
}

.snap-guide-v {
    top: 0;
    bottom: 0;
    width: 1px;
}

/* ===== Portraits on page ===== */
.portrait-on-page {
    position: absolute;
    cursor: move;
    user-select: none;
    z-index: 5;
}

/* Zone invisible autour du portrait pour garder le hover */
.portrait-on-page::before {
    content: '';
    position: absolute;
    inset: -30px;
    z-index: -1;
}

.portrait-on-page:hover {
    filter: brightness(1.05);
}

.portrait-on-page.dragging {
    z-index: 15;
    filter: drop-shadow(0 4px 12px rgba(0,0,0,0.25));
}

.portrait-on-page img {
    position: relative;
    width: 100%;
    height: 100%;
    pointer-events: none;
    display: block;
    z-index: 1;
}

.portrait-delete {
    position: absolute;
    top: -8px;
    right: -8px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: #e74c3c;
    color: white;
    border: 2px solid white;
    font-size: 12px;
    line-height: 1;
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}

.portrait-on-page:hover .portrait-delete {
    display: flex;
}

/* Canvas fond (derriere l'image) */
.portrait-bg-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 0;
}

/* Canvas contour (par-dessus l'image) */
.portrait-border-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 2;
}

/* Toolbar bulle commune */
.portrait-toolbar {
    position: absolute;
    display: none;
    flex-direction: column;
    align-items: center;
    gap: 3px;
    background: white;
    border-radius: 14px;
    padding: 4px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.2);
    z-index: 3;
}

.portrait-on-page:hover .portrait-toolbar {
    display: flex;
}

/* Bulle gauche = fond */
.portrait-toolbar-left {
    right: calc(100% - 2px);
    top: 50%;
    transform: translateY(-50%);
}

/* Bulle droite = contour */
.portrait-toolbar-right {
    left: calc(100% - 2px);
    top: 50%;
    transform: translateY(-50%);
}

/* Petit label dans la bulle */
.toolbar-label {
    font-size: 0.6rem;
    color: #aaa;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    padding: 0 4px;
    pointer-events: none;
}

.portrait-color-picker {
    width: 24px;
    height: 24px;
    padding: 1px;
    border: 1px solid #ddd;
    border-radius: 50%;
    cursor: pointer;
    background: #fff;
}

.portrait-tool-btn {
    width: 24px;
    height: 24px;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: #ccc;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

.portrait-tool-btn:hover {
    background: #f0f0f0;
}

.portrait-tool-btn.active {
    color: var(--accent);
}

.portrait-tool-btn.removebg:hover {
    color: #e74c3c;
}

.portrait-tool-btn:disabled {
    cursor: wait;
    opacity: 0.7;
}

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

.spin {
    animation: spin 0.8s linear infinite;
}

/* Eraser */
.eraser-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 3;
    cursor: crosshair;
}

.portrait-on-page.eraser-active {
    cursor: crosshair;
    z-index: 100;
    filter: none !important;
}

.portrait-on-page.eraser-active .portrait-border-canvas,
.portrait-on-page.eraser-active .portrait-bg-canvas {
    opacity: 0.3;
}

.portrait-tool-btn.eraser-btn:hover {
    color: #e67e22;
}

/* Damier de transparence (visible quand la gomme est active) */
.portrait-on-page.eraser-active::after {
    content: '';
    position: absolute;
    inset: 0;
    z-index: 0;
    background-image:
        linear-gradient(45deg, #ccc 25%, transparent 25%),
        linear-gradient(-45deg, #ccc 25%, transparent 25%),
        linear-gradient(45deg, transparent 75%, #ccc 75%),
        linear-gradient(-45deg, transparent 75%, #ccc 75%);
    background-size: 16px 16px;
    background-position: 0 0, 0 8px, 8px -8px, -8px 0;
    border-radius: 4px;
}

/* Resize handle */
.portrait-resize {
    position: absolute;
    bottom: -12px;
    right: -12px;
    width: 24px;
    height: 24px;
    background: white;
    border: 2px solid var(--accent);
    border-radius: 50%;
    cursor: nwse-resize;
    display: none;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 6px rgba(0,0,0,0.2);
    z-index: 20;
    color: var(--accent);
}

.portrait-resize svg {
    pointer-events: none;
}

.portrait-on-page:hover .portrait-resize {
    display: flex;
}

/* ===== Print ===== */
@media print {
    .toolbar { display: none; }
    .workspace {
        padding: 0;
        height: auto;
        overflow: visible;
        background: white;
    }
    .page {
        box-shadow: none;
        max-width: none;
        max-height: none;
    }
    .page.a4-portrait {
        width: 210mm;
        height: 297mm;
    }
    .page.a4-landscape {
        width: 297mm;
        height: 210mm;
    }
    .portrait-delete,
    .portrait-resize,
    .portrait-toolbar,
    .snap-guide-h,
    .snap-guide-v,
    .placeholder {
        display: none !important;
    }
}
