/* ==========================================
   Handwriting Practice Tool - Industry Standard
   ========================================== */

:root {
    --paper-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
    --paper-border: 1px solid #e5e7eb;
    --guide-top-color: #60a5fa; /* Blue-400 */
    --guide-mid-color: #9ca3af; /* Gray-400 */
    --guide-base-color: #f87171; /* Red-400 */
    --bg-preview: #f3f4f6;
    --sidebar-bg: #ffffff;
    --sidebar-border: #e5e7eb;
    --accent-color: #0d6efd; /* Bootstrap Primary */
}

/* ===== Layout ===== */
.handwriting-tool-container {
    display: flex;
    gap: 0;
    height: calc(100vh - 100px); /* Fixed height to fit screen */
    min-height: 400px; /* Reduced to prevent body scroll on small screens */
    background: var(--bg-preview);
    border: 1px solid var(--sidebar-border);
    border-radius: 12px;
    overflow: hidden; /* Prevent outer scroll */
}

/* ===== Sidebar Controls ===== */
.controls-sidebar {
    width: 350px;
    flex-shrink: 0;
    background: #ffffff;
    border-right: 1px solid var(--sidebar-border);
    display: flex;
    flex-direction: column;
    z-index: 10;
    overflow-y: auto; /* Internal scroll for sidebar */
}

/* Ensure Bootstrap overrides work inside the sidebar */
.controls-sidebar .form-label {
    font-size: 0.75rem;
    letter-spacing: 0.5px;
    margin-bottom: 0.5rem;
}

.controls-sidebar .form-control:focus,
.controls-sidebar .form-select:focus {
    border-color: var(--accent-color);
    box-shadow: 0 0 0 0.25rem rgba(79, 70, 229, 0.15);
}

.controls-sidebar .btn-check:checked + .btn-outline-primary {
    background-color: var(--accent-color);
    border-color: var(--accent-color);
}

.controls-sidebar .btn-check:checked + .btn-outline-secondary {
    background-color: #4b5563;
    border-color: #4b5563;
    color: white;
}

/* ===== Preview Area ===== */
.preview-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    position: relative;
    background: #e2e8f0;
    overflow: hidden;
}

.preview-toolbar {
    height: 64px;
    background: white;
    border-bottom: 1px solid #cbd5e1;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0 1.5rem;
    flex-shrink: 0;
    z-index: 20;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    gap: 1rem;
}

/* Canvas Wrapper */
.preview-canvas-wrapper {
    flex: 1;
    overflow: auto; /* Scrollbar here */
    padding: 3rem;
    display: flex;
    /* margin: auto on the child handles centering */
    background-color: #f1f5f9;
    /* Subtle dot pattern */
    background-image: radial-gradient(#cbd5e1 1px, transparent 1px);
    background-size: 20px 20px;
}

/* Pages Container */
.pages-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2rem;
    margin: auto; /* Center in the wrapper */
    flex-shrink: 0;
    width: fit-content; /* Allow container to grow with content */
    min-width: 100%; /* Ensure it spans full width for centering small pages */
    padding-bottom: 3rem; /* Bottom scroll buffer */
}

/* Page Wrapper */
.page-wrapper {
    position: relative;
    display: block;
    box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.1);
    background: white;
    overflow: visible; /* Allow paper to be fully visible when scaled */
    transition: width 0.2s ease, height 0.2s ease;
}

/* Flip Animation Class */
.pages-container.flipping {
    opacity: 0;
    transform: scale(0.9);
}

/* The Paper */
.worksheet-paper {
    background: white;
    /* box-shadow removed from here as it's on wrapper now */
    border: 1px solid #e5e7eb;
    position: relative;
    box-sizing: border-box;
    overflow: hidden;
    transform-origin: top left; 
    margin: 0; 
}

/* Paper Content */
.worksheet-header-section {
    margin-bottom: 2rem;
    padding-bottom: 1rem;
    border-bottom: 2px solid #e5e7eb;
}

.worksheet-title-display {
    font-family: 'Schoolbell', cursive;
    font-size: 24pt;
    text-align: center;
    margin: 0 0 1rem 0;
    color: #111;
}

.worksheet-meta-grid {
    display: flex;
    justify-content: space-between;
    gap: 2rem;
}

.meta-item {
    font-family: 'Schoolbell', cursive;
    font-size: 14pt;
    color: #4b5563;
    display: flex;
    align-items: baseline;
    gap: 0.5rem;
}

.meta-line {
    border-bottom: 1px solid #9ca3af;
    flex: 1;
    min-width: 150px;
}

/* Practice Rows */
.practice-row {
    position: relative;
    width: 100%;
    margin-bottom: 1.25rem;
}

/* Guides */
.guides-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.guide-line {
    position: absolute;
    width: 100%;
    left: 0;
}

.guide-top {
    top: 0;
    border-top: 2px solid var(--guide-top-color);
}

.guide-mid {
    top: 40%; /* Adjusted to be slightly higher than center for x-height */
    border-top: 1px dashed var(--guide-mid-color);
}

.guide-base {
    top: 80%; /* Baseline at 80% to allow descenders */
    border-bottom: 2px solid var(--guide-base-color);
}

/* Text Layer */
.text-layer {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 2;
    width: 100%;
    height: 100%;
    display: block;
    overflow: visible; /* Allow descenders to hang out */
}

text {
    font-family: 'Schoolbell', cursive;
    dominant-baseline: alphabetic;
    text-anchor: start;
}

/* Watermark */
.worksheet-watermark {
    position: absolute;
    top: 10mm;
    left: 50%;
    transform: translateX(-50%);
    right: auto;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    opacity: 0.85;
    pointer-events: none;
    z-index: 50;
    background: rgba(255, 255, 255, 0.9);
    padding: 0.5rem;
    border-radius: 8px;
}

.worksheet-watermark img {
    height: 48px;
    width: auto;
}

.worksheet-watermark span {
    font-family: 'Quicksand', sans-serif;
    font-size: 18pt;
    color: #374151;
    font-weight: 700;
    letter-spacing: 0.5px;
}

/* Responsive */
@media (max-width: 991px) {
    .handwriting-tool-container {
        flex-direction: column;
        height: auto;
    }
    
    .controls-sidebar {
        width: 100%;
        height: auto;
        border-right: none;
        border-bottom: 1px solid var(--sidebar-border);
        position: static;
        border-radius: 12px 12px 0 0;
    }
    
    .preview-area {
        min-height: 600px;
    }
}

/* Print Styles */
@media print {
    @page {
        margin: 0;
        /* size is set dynamically in JS based on orientation */
    }

    body {
        margin: 0;
        padding: 0;
    }

    /* Hide everything except the print area */
    body > *:not(#print-area) {
        display: none !important;
    }

    /* Print Container */
    #print-area {
        display: block !important;
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
    }

    .worksheet-paper {
        /* Reset layout for print */
        position: relative !important;
        width: 100% !important;
        height: 100% !important; /* Use 100% to fill print-area */
        min-height: 100vh; /* Ensure it fills viewport */
        margin: 0 !important;
        box-shadow: none !important;
        border: none !important;
        transform: none !important;
        page-break-after: always;
        overflow: hidden;
    }

    .worksheet-paper:last-child {
        page-break-after: auto;
    }

    /* Force colors for guides */
    .guide-top { border-color: #60a5fa !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
    .guide-mid { border-color: #9ca3af !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
    .guide-base { border-color: #f87171 !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; }

    /* Ensure watermark is visible and crisp */
    .worksheet-watermark {
        opacity: 1 !important;
        top: 10mm !important;
        bottom: auto !important;
        left: 50% !important;
        transform: translateX(-50%) !important;
        right: auto !important;
        background: white !important;
        padding: 0.5rem 1rem !important;
        margin-bottom: 0 !important;
    }
    
    .worksheet-watermark span {
        color: #111 !important;
        text-shadow: none !important;
    }
}

/* Coloring Corner */
.coloring-corner {
    position: absolute;
    bottom: 20mm;
    right: 20mm;
    width: 40mm;
    height: 40mm;
    opacity: 0.8;
    z-index: 5;
    pointer-events: none;
}

.coloring-corner svg {
    width: 100%;
    height: 100%;
    fill: white;
    stroke: #333;
    stroke-width: 2;
}

/* Utility Classes for Inline Styles */
.icon-wrapper-sm {
    width: 32px;
    height: 32px;
}

.switch-lg {
    width: 3em;
    height: 1.5em;
}

.status-badge {
    min-width: 80px;
    opacity: 1;
}
