/* ==========================================================================
   1. GLOBAL MODAL & CANVAS STRUCTURE
   Fixes: "White Container" blocking, full screen fit, and scrolling locks.
   ========================================================================== */
html, body { 
    height: 100%; 
    overflow-x: hidden; 
    overflow-y: auto !important; 
    -webkit-overflow-scrolling: touch; 
    background: #000; 
}

/* Lock scrolling only when modal is open */
body.modal-open { 
    position: fixed; 
    width: 100%; 
    height: 100%; 
    overflow: hidden !important; 
}

/* Modal Frame */
#drawingModal {
    position: fixed !important;
    inset: 0 !important; /* Forces edges to viewport (0,0,0,0) */
    width: 100vw !important;
    height: 100dvh !important; /* Dynamic Viewport Height fixes mobile bars */
    z-index: 100000 !important;
    background: #fff !important;
    overflow: hidden !important;
    overscroll-behavior: none;
}

/* Inner Containers (Transparency Fix) */
#drawingModal .modal-content-box, 
#canvasContainer { 
    position: fixed !important; 
    inset: 0 !important; 
    width: 100% !important; 
    height: 100% !important; 
    margin: 0 !important; 
    padding: 0 !important; 
    border-radius: 0 !important; 
    background: transparent !important; /* Critical: No white bars */
    overflow: hidden !important; 
    box-shadow: none !important;
    border: none !important;
}

/* ==========================================================================
   2. DRAWING LAYERS
   Z-Index Hierarchy: Background < Canvas < UI (Toolbar/Overlays)
   ========================================================================== */

/* A. Question Text (Watermark) */
#drawingBackground { 
    position: absolute !important; 
    top: 0 !important; 
    left: 0 !important; 
    width: 100% !important; 
    height: 100% !important; 
    
    display: flex !important; 
    justify-content: center !important; 
    align-items: flex-start !important; 
    padding-top: 40px !important; /* Default Desktop Padding */
    
    text-align: center !important; 
    pointer-events: none; 
    z-index: 5; 
    color: rgba(0, 0, 0, 0.6) !important; 
    font-weight: 500 !important; 
    font-size: 1.2rem !important; 
}

/* B. The Actual Canvas */
#drawingCanvas {
    position: absolute !important;
    top: 0;
    left: 0;
    width: 100% !important;
    height: 100% !important;
    z-index: 10; /* Above text, below UI */
    background: transparent !important;
    touch-action: none; /* Prevents scroll while drawing */
}

/* C. Solution/Annotation Layer */
#solutionLayer {
    position: absolute;
    /* Vertical: Below Question Text */
    top: 68px; 
    /* Horizontal: Moved to Left Side */
    left: 10px; 
    right: auto; /* Clear right align */
    
    z-index: 40;
    pointer-events: none;
    max-width: 220px;
    
    /* Ensure flex column aligns left */
    display: flex;
    flex-direction: column;
    align-items: flex-start; 
}


@media (max-width: 640px), (max-height: 500px) and (orientation: landscape) { 
    #solutionLayer {
        top: 70px !important; /* Slightly higher on mobile */
        left: 10px !important;
        right: auto !important;
        max-width: 180px !important;
    }
}
/* Individual Bubble Polish */
step-item {
    background-color: #4b5563 !important; /* Gray-600 */
    color: #f9fafb !important; /* Gray-50 */
    border-left: 4px solid #d1d5db !important; /* Light Grey Accent */
    
    box-shadow: 0 4px 6px rgba(0,0,0,0.1) !important;
    margin-bottom: 6px !important;
    padding: 6px 10px !important;
    border-radius: 8px !important;
    border-top-left-radius: 0 !important;
    
    font-size: 0.85rem;
    font-weight: 500;
    line-height: 1.4;
    backdrop-filter: blur(4px);
    pointer-events: auto;
}

#solutionLayer .bg-black\/80 {
    background-color: #374151 !important; /* Gray-700 */
    border: 1px solid #4b5563 !important;
    color: #e5e7eb !important;
}
/* D. AI Hint Box ('Palace Tutor') */
#hintOverlay, .hint-toast { 
    position: fixed !important; 
    /* Lowered to avoid blocking question text */
    top: calc(18% + 20px) !important; 
    left: 50% !important; 
    transform: translateX(-50%) !important; 
    
    width: auto !important; 
    max-width: 85% !important; 
    
    background: #e5e7eb !important; 
    color: #000000 !important; 
    padding: 2px 8px !important; 
    border-radius: 6px !important; 
    font-size: 0.85rem !important; 
    font-weight: 600 !important; 
    
    z-index: 20000 !important; 
    box-shadow: 0 8px 32px rgba(0,0,0,0.15) !important; 
    border: none !important; /* Removed light grey border */
    bottom: auto !important; 
}


/* ==========================================================================
   3. TOOLBAR SYSTEM (Honeycomb 6-over-5)
   ========================================================================== */

/* A. THE ANCHOR (Invisible Wrapper) */
#toolbarAnchorContainer {
    position: fixed !important;
    z-index: 200 !important;
    pointer-events: none !important;
    
    /* ✅ FIX: Removed 'left: 0' and 'width: 100%' from here. 
       These prevented horizontal dragging. */
}

/* STATIC STATE: Pinned to Bottom Center (When NOT dragging) */
#toolbarAnchorContainer:not(.is-dragging) {
    /* Anchors to bottom center */
    left: 0 !important;
    width: 100% !important;
    bottom: calc(15px + env(safe-area-inset-bottom)) !important; 
    
    /* Center alignment logic */
    display: flex !important;
    justify-content: center !important;
    align-items: flex-end !important;
    
    /* Reset Top so it stays at bottom */
    top: auto !important;
}

/* DRAGGING STATE: Free Movement (JS controls Top/Left) */
#toolbarAnchorContainer.is-dragging {
    /* Release all pins so JS can move it */
    bottom: auto !important;
    right: auto !important;
    
    /* ✅ FIX: Reset width so it shrinks to fit the hexes */
    width: auto !important; 
    height: auto !important;
    margin: 0 !important;
    
    z-index: 2147483647 !important;
    
    /* Ensure flex doesn't center it while dragging */
    display: block !important; 
}

/* B. THE CONTROL PAD (The Hex Grid) */
#floatingCanvasToolbar { 
    position: relative; 
    display: flex !important; 
    flex-direction: column !important; 
    align-items: center !important; 
    justify-content: center !important; 
    
    background: transparent !important; 
    border: none !important; 
    box-shadow: none !important; 
    backdrop-filter: none !important;
    
    padding: 0 !important; 
    gap: 0 !important;
    
    pointer-events: none; 
    z-index: 210010; 
    
    /* ✅ FIX: Ensure it doesn't span full width */
    width: auto !important; 
    max-width: 95vw !important;
}

/* The Rows */
.hex-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    pointer-events: auto;
}

/* Interlocking */
.hex-row.bottom {
    margin-top: -8px !important; 
}

/* Individual Hexagon Buttons */
#floatingCanvasToolbar button, 
#toolbarDragHandle,
#floatingCanvasToolbar #submitDrawingBtn,
.hex-btn { 
    width: 38px !important;  
    height: 42px !important; 
    padding: 0 !important;
    margin: 0 !important;
    flex-shrink: 0 !important; 
    flex: none !important;
    
    clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
    border-radius: 0 !important;
    border: none !important;
    
    background: rgba(91, 33, 182, 0.95);
    color: #e9d5ff; 
    
    filter: drop-shadow(0 3px 2px rgba(0,0,0,0.3));
    
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.1s ease, background 0.2s;
}

/* SVG Size */
#floatingCanvasToolbar button svg, 
#toolbarDragHandle svg {
    width: 18px !important;
    height: 18px !important;
}

/* Internal Border */
#floatingCanvasToolbar button {
    box-shadow: inset 0 1px 0 rgba(255,255,255,0.2), inset 0 -2px 0 rgba(0,0,0,0.2);
}

/* Active State */
#floatingCanvasToolbar button:active { 
    transform: scale(0.92); 
    background-color: rgba(76, 29, 149, 1);
    filter: drop-shadow(0 2px 2px rgba(0,0,0,0.2));
}

/* Submit Button */
#floatingCanvasToolbar #submitDrawingBtn { 
    background-color: #8b5cf6 !important;
    color: #fff !important; 
    z-index: 10;
}

/* Drag Handle */
#toolbarDragHandle {
    cursor: grab;
    background: rgba(46, 16, 101, 0.8);
    color: #c4b5fd;
}
#toolbarDragHandle:active {
    cursor: grabbing;
}

/* Mobile Widescreen / Landscape */
@media (max-height: 500px) and (orientation: landscape) { 
    #floatingCanvasToolbar button, 
    #toolbarDragHandle,
    #floatingCanvasToolbar #submitDrawingBtn,
    .hex-btn { 
        width: 32px !important; 
        height: 36px !important; 
    } 
    
    #floatingCanvasToolbar button svg, 
    #toolbarDragHandle svg {
        width: 14px !important;
        height: 14px !important;
    }
    
    .hex-row { gap: 2px !important; }
    .hex-row.bottom { margin-top: -6px !important; }
}

/* Mobile Portrait */
@media (max-width: 640px) {
    #floatingCanvasToolbar {
        transform: scale(0.95);
    }
}





/* ==========================================================================
   5. CALCULATOR RESTORATION (Fixes "Broken Styling")
   ========================================================================== */

#calculatorOverlay { 
    position: absolute !important; 
    top: auto !important; 
    bottom: 100%; /* Anchor above toolbar */
    left: 50% !important; 
    transform: translateX(-50%) translateY(10px); 
    
    width: 300px !important; 
    /* Force Opaque Background (was getting transparent from global rules) */
    background: #1f2937 !important; /* Gray-800 */
    border: 1px solid #374151 !important;
    border-radius: 12px !important; 
    box-shadow: 0 10px 30px rgba(0,0,0,0.5) !important; 
    
    display: flex; 
    flex-direction: column; 
    overflow: hidden; 
    z-index: 210020 !important; /* Above toolbar */
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s, transform 0.2s;
}

#calculatorOverlay.visible { 
    opacity: 1 !important; 
    pointer-events: auto !important;
    transform: translateX(-50%) translateY(-10px); /* Pop up slightly */
}

/* Fix Inner Content Visibility */
.calc-content {
    padding: 12px;
    color: white;
}

.calc-content.hidden { display: none; }
.calc-content.active { display: block; }

/* Calculator Buttons */
.calc-btn {
    background: #374151;
    color: white;
    border-radius: 6px;
    padding: 10px;
    font-weight: bold;
    font-family: monospace;
}
.calc-btn:active { background: #4b5563; }
.calc-btn.op { background: #f59e0b; color: black; } /* Orange Operators */

/* Fix Display Input */
#calcDisplay {
    background: #111827;
    color: #10b981; /* Matrix Green text */
    padding: 10px;
    border-radius: 6px;
    margin-bottom: 10px;
    font-family: monospace;
    font-size: 1.2rem;
    text-align: right;
    border: 1px solid #374151;
}

/* =========================================
   FLOATING SOLUTION PANEL (Compact & Colorful)
   ========================================= */

.floating-solution-panel {
    position: absolute;
    z-index: 1000;
    display: flex;
    flex-direction: column;
    
    /* Strict Sizing */
    width: fit-content;
    min-width: 240px; 
    max-width: 320px;
    max-height: 70vh;
    
    /* Visuals */
    background: #fdfdfd;
    border: 1px solid rgba(0, 0, 0, 0.12);
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    
    box-sizing: border-box;
}

/* Header (Drag Handle) */
.solution-header {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    
    /* ✅ COMPACT PADDING */
    padding: 6px 10px; 
    
    background: #f8fafc; /* Very light slate */
    border-bottom: 1px solid rgba(0, 0, 0, 0.06);
    border-radius: 8px 8px 0 0;
    cursor: grab;
}

/* Content Area */
.solution-content {
    flex: 1;
    overflow-y: auto;
    
    /* ✅ REDUCED PADDING */
    padding: 8px 10px;
    
    display: flex;
    flex-direction: column;
    gap: 8px; /* Tighter gap between items */
    overflow-x: hidden;
}

/* --- ITEM STYLES --- */

.step-item {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    width: 100%;
    min-width: 0;
    
    /* ✅ TIGHTER SPACING */
    padding-bottom: 6px;
    margin-bottom: 2px;
    border-bottom: 1px dashed rgba(0,0,0,0.08);
}

.step-item:last-child {
    border-bottom: none;
    padding-bottom: 0;
}

/* --- LABELS (HINT / FORMULAS) --- */

.step-label {
    /* ✅ SMALLER & TIGHTER */
    font-size: 10px !important;
    font-weight: 700 !important; /* Bold but not heavy black */
    text-transform: uppercase;
    letter-spacing: 0.5px;
    line-height: 1;
    margin-bottom: 4px !important; /* Pull content closer to header */
    
    /* Default Color (Steps) */
    color: #9ca3af; 
}

/* --- COLOR OVERRIDES (The Blue/Orange Return) --- */

/* Hint: Vibrant Orange/Amber */
.step-item.is-hint .step-label { 
    color: #d97706 !important; /* Amber-600 */
}

/* Formula: Vibrant Blue */
.step-item.is-formula .step-label { 
    color: #2563eb !important; /* Blue-600 */
}

/* Tag inside Formula (e.g., "Power Rule:") */
.formula-tag {
    font-size: 9px;
    font-weight: 700;
    color: #6b7280; /* Neutral Gray */
    text-transform: uppercase;
    margin-bottom: 2px;
}

/* --- CONTENT --- */

.step-text {
    width: 100%;
    font-size: 13px;
    line-height: 1.4;
    color: #1f2937;
    white-space: normal;
    overflow-wrap: break-word;
}

.step-math, .formula-math {
    display: block;
    width: 100%;
    overflow-x: auto;
    white-space: nowrap;
    padding-bottom: 4px;
    
    /* Compact Math Font */
    font-size: 13px !important; 
    color: #000000;
    
    /* Hide Scrollbars */
    scrollbar-width: none;
    -ms-overflow-style: none;
}
.step-math::-webkit-scrollbar, .formula-math::-webkit-scrollbar {
    display: none;
}

.mjx-chtml {
    font-size: 100% !important;
    outline: none !important;
}