@font-face {
    font-family: 'Canela';
    src: url('Canela-Regular.otf') format('opentype');
}

:root {
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* Base perspective for realistic 3D feel */
.perspective-2000 {
    perspective: 3000px;
}

.transform-style-3d {
    transform-style: preserve-3d;
}

/* Notebook covers in the shelf */
.notebook-spine {
    width: 12px;
    height: 100%;
    background: rgba(0, 0, 0, 0.15);
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    box-shadow: inset -2px 0 3px rgba(0, 0, 0, 0.2);
    border-top-left-radius: inherit;
    border-bottom-left-radius: inherit;
}

/* The writing area */
.page-content-area {
    flex: 1;
    width: 100%;
    background: transparent;
    border: none;
    outline: none;
    resize: none;
    font-family: 'Canela', serif;
    font-size: 1.25rem;
    line-height: 1.5rem;
    color: #2a2a2a;
    caret-color: transparent !important;
    z-index: 50;
    position: relative;
    overflow: hidden;
}

.custom-caret {
    position: absolute;
    width: 3px;
    height: 1.4em;
    background-color: #1279ff;
    pointer-events: none;
    z-index: 100;
    border-radius: 4px;
    display: none;
    animation: caret-blink 1.2s infinite;
    transform: translateY(0.1em);
}

@keyframes caret-blink {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0;
    }
}

.textarea-mirror {
    position: absolute;
    visibility: hidden;
    white-space: pre-wrap;
    word-wrap: break-word;
    top: 0;
    left: 0;
    pointer-events: none;
    z-index: -1000;
    border: none;
    padding: 0;
    margin: 0;
}

/* Drag and Drop Feedback */
.notebook-drag-over {
    outline: 4px solid rgba(255, 255, 255, 0.5);
    outline-offset: 4px;
    transform: scale(1.02) translateY(-8px) !important;
    z-index: 50 !important;
    transition: all 0.2s ease;
}

[draggable="true"]:active {
    cursor: grabbing;
}

/* Ensure notebooks can receive drop events when children are disabled */
.grid-is-dragging div[data-id] {
    pointer-events: auto !important;
}

.grid-is-dragging div[data-id] * {
    pointer-events: none !important;
}


/* Custom Scrollbar */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 10px;
}

::-webkit-scrollbar-thumb:hover {
    background: rgba(0, 0, 0, 0.3);
}

.book-cover {
    position: absolute;
    transform-style: preserve-3d;
    z-index: 0;
}

/* --- GLOBAL LAYOUT RESETS --- */
#book-slider {
    max-width: 1000px;
    transition: transform 0.6s cubic-bezier(0.645, 0.045, 0.355, 1);
}

.spine-overlay {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 40;
    pointer-events: none;
    mix-blend-mode: multiply;
    border-radius: 9999px;
}

.leaf {
    position: absolute;
    transform-style: preserve-3d;
    transition: transform 0.6s cubic-bezier(0.645, 0.045, 0.355, 1);
}

.face {
    position: absolute;
    inset: 0;
    background-color: #fdfaf5;
    background-image: url('paper.jpg');
    background-size: cover;
    backface-visibility: hidden;
    display: flex;
    flex-direction: column;
    /* Stronger shadow for more depth */
    box-shadow:
        inset 0 0 30px rgba(0, 0, 0, 0.05),
        0 12px 24px rgba(0, 0, 0, 0.3),
        0 8px 8px rgba(0, 0, 0, 0.2);
}

.page-plain .face,
.page-plain .face {
    background-color: #fdfaf5;
}

.page-plain .face .page-content-area,
.page-dotted .face .page-content-area,
.page-grid .face .page-content-area {
    padding-left: 1.5rem;
}

.page-turn-btn {
    position: absolute;
    inset: 0;
    cursor: pointer;
    z-index: 60;
}

.editing .page-turn-btn {
    display: none;
}

.page-drag-highlight {
    outline: 4px solid #3b82f6 !important;
    outline-offset: -4px;
    z-index: 100 !important;
    transition: outline 0.2s ease;
}


/* --- DESKTOP (Horizontal Spread) --- */
@media (min-width: 768px) {
    #book-slider {
        width: 90vw;
        height: 80vh;
        aspect-ratio: 2 / 1.4;
    }

    .book-cover {
        top: 0;
        bottom: 0;
        width: 50%;
    }

    .book-cover-1 {
        right: 50%;
    }

    /* Left cover */
    .book-cover-1 .cover-bg {
        right: 1px;
        border-radius: 12px 0 0 12px;
    }

    .book-cover-1 .shadow-inner {
        right: 8px;
        border-radius: 8px 0 0 8px;
    }

    .book-cover-2 {
        left: 50%;
    }

    /* Right cover */
    .book-cover-2 .cover-bg {
        left: 1px;
        border-radius: 0 12px 12px 0;
    }

    .book-cover-2 .shadow-inner {
        left: 8px;
        border-radius: 0 8px 8px 0;
    }

    .spine-overlay {
        width: 2rem;
        height: 98%;
        background: linear-gradient(to right, transparent, rgba(0, 0, 0, 0.2), transparent);
    }

    .leaf {
        top: 2%;
        bottom: 2%;
        left: 50%;
        width: 48%;
        transform-origin: left center;
        transform: rotateY(var(--rotation, 0deg)) translateZ(var(--depth, 0px));
    }

    .face.front {
        border-radius: 2px 8px 8px 2px;
        transform: rotateY(0deg);
    }

    .face.front::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
        width: 30px;
        background: linear-gradient(to right, rgba(0, 0, 0, 0.15), transparent);
        pointer-events: none;
        z-index: 10;
    }

    .face.back {
        border-radius: 8px 2px 2px 8px;
        transform: rotateY(180deg);
    }

    .face.back::before {
        content: '';
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        width: 30px;
        background: linear-gradient(to left, rgba(0, 0, 0, 0.15), transparent);
        pointer-events: none;
        z-index: 10;
    }

    .page-content-area {
        padding: 1.5rem 1rem 1.5rem 60px;
        font-size: 1.5rem;
        line-height: 1.75rem;
    }

    .page-ruled .face {
        background-color: #fdfaf5;
        background-image: linear-gradient(transparent 95%, #cbd5e1 95%), url('paper.jpg');
        background-size: 100% 1.75rem, cover;
        background-position: 0 1.5rem, center;
        background-blend-mode: multiply;
    }

    .page-ruled .face.front::after,
    .page-ruled .face.back::after {
        content: '';
        position: absolute;
        top: 0;
        bottom: 0;
        left: 50px;
        width: 1px;
        background-color: rgba(239, 68, 68, 0.4);
        pointer-events: none;
        z-index: 5;
    }

    .page-dotted .face {
        background-color: #fdfaf5;
        background-image: radial-gradient(#9ca3af 1px, transparent 1px), url('paper.jpg');
        background-size: 1.5rem 1.5rem, cover;
        background-position: 0.75rem 0.75rem, center;
        background-blend-mode: multiply;
    }

    .page-grid .face {
        background-color: #fdfaf5;
        background-image: linear-gradient(#e5e7eb 1px, transparent 1px), linear-gradient(90deg, #e5e7eb 1px, transparent 1px), url('paper.jpg');
        background-size: 1.5rem 1.5rem, 1.5rem 1.5rem, cover;
        background-blend-mode: multiply;
    }

    .page-turn-btn {
        inset: 0;
    }
}

/* --- MOBILE (Single Page View - Full screen one side at a time) --- */
@media (max-width: 767px) {
    #book-slider {
        width: 92vw;
        height: 80vh;
        aspect-ratio: 1 / 1.4;
    }

    .book-cover {
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        width: 100%;
        height: 100%;
    }

    .book-cover-1 {
        z-index: 0;
    }

    .book-cover-1 .cover-bg {
        border-radius: 12px;
    }

    .book-cover-1 .shadow-inner {
        border-radius: 8px;
    }

    .book-cover-2 {
        z-index: -1;
    }

    .book-cover-2 .cover-bg {
        border-radius: 12px;
    }

    .book-cover-2 .shadow-inner {
        border-radius: 8px;
    }

    .spine-overlay {
        width: 2rem;
        height: 100%;
        left: 0;
        top: 0;
        transform: none;
        background: linear-gradient(to right, rgba(0, 0, 0, 0.3), transparent);
    }

    .leaf {
        position: absolute;
        inset: 2%;
        width: 96%;
        height: 96%;
        transform-origin: left center;
        transform: rotateY(var(--rotation, 0deg)) translateZ(var(--depth, 0px));
        top: 2%;
    }

    .face {
        box-shadow:
            inset 0 0 20px rgba(0, 0, 0, 0.05),
            0 10px 20px rgba(0, 0, 0, 0.4);
    }

    .face.front {
        /* Page is full width */
        border-radius: 2px 8px 8px 2px;
        transform: rotateY(0deg);
    }

    .face.front::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
        width: 30px;
        background: linear-gradient(to right, rgba(0, 0, 0, 0.15), transparent);
        pointer-events: none;
        z-index: 10;
    }

    .face.back {
        /* Page is full width */
        border-radius: 8px 2px 2px 8px;
        transform: rotateY(180deg);
    }

    .face.back::before {
        content: '';
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        width: 30px;
        background: linear-gradient(to left, rgba(0, 0, 0, 0.15), transparent);
        pointer-events: none;
        z-index: 10;
    }

    .page-content-area {
        padding: 3rem 1rem 1.5rem 40px;
    }

    .page-plain .face .page-content-area,
    .page-dotted .face .page-content-area,
    .page-grid .face .page-content-area {
        padding-left: 1.5rem;
        padding-top: 3rem;
    }

    .page-ruled .face {
        background-color: #fdfaf5;
        background-image: linear-gradient(transparent 95%, #cbd5e1 95%), url('paper.jpg');
        background-size: 100% 1.5rem, cover;
        background-position: 0 1.25rem, center;
        background-blend-mode: multiply;
    }

    .page-ruled .face.front::after,
    .page-ruled .face.back::after {
        content: '';
        position: absolute;
        top: 0;
        bottom: 0;
        left: 30px;
        width: 1px;
        background-color: rgba(239, 68, 68, 0.4);
        pointer-events: none;
        z-index: 5;
    }

    .page-dotted .face {
        background-color: #fdfaf5;
        background-image: radial-gradient(#9ca3af 1px, transparent 1px), url('paper.jpg');
        background-size: 1rem 1rem, cover;
        background-position: 0.5rem 0.5rem, center;
        background-blend-mode: multiply;
    }

    .page-grid .face {
        background-color: #fdfaf5;
        background-image: linear-gradient(#e5e7eb 1px, transparent 1px), linear-gradient(90deg, #e5e7eb 1px, transparent 1px), url('paper.jpg');
        background-size: 1rem 1rem, 1rem 1rem, cover;
        background-blend-mode: multiply;
    }

    /* Hotzones for Top/Bottom flipping (Full Page in Reading Mode) */
    .page-turn-btn {
        inset: 0;
    }
}

/* Image Transformation Styles */
.image-transform-wrapper {
    position: absolute;
    cursor: move;
    user-select: none;
    touch-action: none;
    z-index: 100;
    pointer-events: auto;
}


.image-transform-wrapper.active {
    outline: 2px solid #3b82f6;
    outline-offset: 2px;
}

.image-transform-wrapper img {
    display: block;
    width: 100%;
    height: auto;
    pointer-events: none;
    box-shadow:
        0 8px 16px -4px rgba(0, 0, 0, 0.2),
        0 4px 8px -2px rgba(0, 0, 0, 0.15),
        0 0 0 1px rgba(0, 0, 0, 0.05);
    border: 6px solid white;
}

.image-transform-handle {
    position: absolute;
    width: 12px;
    height: 12px;
    background: white;
    border: 2px solid #3b82f6;
    border-radius: 50%;
    display: none;
    z-index: 110;
}

.image-transform-wrapper.active .image-transform-handle {
    display: block;
}

.handle-br {
    bottom: -6px;
    right: -6px;
    cursor: nwse-resize;
}

.image-rotate-handle {
    position: absolute;
    top: -30px;
    left: 50%;
    transform: translateX(-50%);
    width: 24px;
    height: 24px;
    background: white;
    border: 2px solid #3b82f6;
    border-radius: 50%;
    display: none;
    align-items: center;
    justify-content: center;
    cursor: grab;
    z-index: 110;
}

.image-rotate-handle::after {
    content: '';
    position: absolute;
    top: 24px;
    left: 50%;
    width: 2px;
    height: 6px;
    background: #3b82f6;
    transform: translateX(-50%);
}

.image-transform-wrapper.active .image-rotate-handle {
    display: flex;
}

.image-delete-btn {
    position: absolute;
    top: -10px;
    right: -10px;
    width: 20px;
    height: 20px;
    background: #ef4444;
    color: white;
    border-radius: 50%;
    display: none;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    cursor: pointer;
    z-index: 120;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.image-transform-wrapper.active .image-delete-btn {
    display: flex;
}

/* Mobile Tweaks for PWA Safe Areas */
@media (max-width: 767px) {
    #view-shelf header {
        padding-top: calc(3rem + var(--safe-top)) !important;
    }

    #view-shelf main {
        padding-bottom: calc(2rem + var(--safe-bottom)) !important;
    }

    #btn-add-notebook {
        bottom: calc(2rem + var(--safe-bottom)) !important;
    }

    #notebook-topbar {
        padding-top: calc(1rem + var(--safe-top)) !important;
        background: linear-gradient(to bottom, rgba(0, 0, 0, 0.4), transparent);
        /* Improve legibility over pages */
    }

    #notebook-stage {
        padding-top: calc(4.5rem + var(--safe-top)) !important;
        align-items: flex-start !important;
        /* Align book below top bar */
        padding-bottom: calc(1rem + var(--safe-bottom)) !important;
    }
}

.no-scrollbar::-webkit-scrollbar {
    display: none;
}

#notebook-carousel::-webkit-scrollbar {
    display: none;
}

#notebook-carousel>div {
    flex-shrink: 0;
    width: 70vw;
    max-width: 400px;
    height: 100%;
    max-height: 560px;
    scroll-snap-align: center;
    transition: transform 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275), filter 0.3s ease;
}

@media (max-width: 767px) {
    #notebook-carousel {
        padding: 0 15vw;
        gap: 5vw;
    }
}