:root {
    /* Dimensions */
    --header-height: 56px;
    --bottom-nav-height: 65px;
    --sticky-header-height: 48px;
    --banner-height: 0px;
    
    /* Colors */
    --primary: #2563eb;
    --primary-light: #dbeafe;
    --primary-gradient: linear-gradient(135deg, #2563eb, #7c3aed);
    --secondary: #7c3aed;
    --teacher: #dc2626;
    --teacher-gradient: linear-gradient(135deg, #dc2626, #f59e0b);
    --bg-main: #f8fafc;
    --bg-card: #fff;
    --border-color: #f1f5f9;
    --text-main: #1e293b;
    --text-muted: #64748b;
    
    /* Spacing */
    --spacing-xs: 0.25rem;
    --spacing-sm: 0.5rem;
    --spacing-md: 1rem;
    --spacing-lg: 1.5rem;
    --panel-padding: 1.25rem;
    
    /* Border Radius */
    --radius-md: 0.75rem;
    --radius-lg: 1rem;
    --radius-xl: 1.5rem;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { 
    height: 100%; 
    overflow: hidden; 
    font-family: system-ui, sans-serif; 
    margin: 0;
    padding: 0;
    color: var(--text-main);
    background: var(--bg-main);
}

#appView { 
    height: 100dvh; 
    display: flex; 
    flex-direction: column; 
    overflow: hidden; 
    position: relative; 
}

.view { display: none !important; height: 100%; overflow: hidden; }
.view.active { display: flex !important; flex-direction: column; }

#mainContent { 
    flex: 1; 
    overflow-y: auto; 
    background: var(--bg-main); 
    padding: var(--spacing-lg); 
    position: relative;
    scroll-behavior: smooth;
}

/* Fixed layout for science practices */
.view-student #mainContent, .view-teacher #mainContent {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.nav-btn.active { background: var(--primary-gradient); color: white; }
.teacher-nav.active { background: var(--teacher-gradient); color: white; }

/* ==================== MOBILE PREMIUM OVERHAUL ==================== */
@media (max-width: 768px) {
    #appView {
        height: 100dvh;
        display: flex;
        flex-direction: column;
        overflow: hidden;
        position: fixed;
        inset: 0;
        width: 100%;
    }

    #appHeader {
        height: var(--header-height) !important;
        padding: 0 var(--spacing-sm) !important;
        display: flex !important;
        flex-shrink: 0;
        z-index: 100;
        background: var(--bg-card);
        border-bottom: 1px solid var(--border-color);
    }
    #appHeader .hidden-mobile { display: none; }
    .mobile-nav-toggle { display: block !important; }
    #headerSubtitle { display: none !important; }
    #appHeader h1 { font-size: 1rem !important; }

    /* Main Container Offset */
    #appView > .flex.flex-1 {
        flex: 1;
        overflow: hidden;
        display: flex;
        flex-direction: column;
    }

    #statusBanner {
        flex-shrink: 0;
        z-index: 90;
    }

    #mainContent { 
        flex: 1;
        padding: 0 !important; 
        overflow: hidden scroll !important;
        scroll-snap-type: y mandatory;
        -webkit-overflow-scrolling: touch;
        background: var(--bg-card);
        display: block !important;
        scrollbar-width: none;
        touch-action: pan-y;
        position: relative;
        overscroll-behavior-y: none;
    }
    #mainContent::-webkit-scrollbar { display: none; }

    .panels-container {
        display: block !important;
        width: 100%;
        margin: 0;
        padding: 0;
    }
    
    /* Panel snapping and full-screen sizing */
    .panels-container > *, [data-card-title], .practice-card {
        width: 100% !important;
        min-height: calc(100dvh - var(--header-height) - var(--bottom-nav-height) - var(--banner-height)) !important;
        scroll-snap-align: start;
        scroll-snap-stop: always;
        margin: 0 !important;
        border-radius: 0 !important;
        border: none !important;
        border-bottom: 1px solid var(--border-color) !important;
        box-shadow: none !important;
        display: flex !important;
        flex-direction: column !important;
        padding: 0 !important;
        background: var(--bg-card);
        position: relative;
        pointer-events: auto !important;
    }

    .sticky-panel-header {
        position: sticky;
        top: 0;
        z-index: 60;
        background: rgb(255, 255, 255, 0.95);
        backdrop-filter: blur(16px);
        backdrop-filter: blur(16px);
        border-bottom: 1px solid rgb(0, 0, 0, 0.08);
        padding: 0 var(--panel-padding) !important;
        height: var(--sticky-header-height);
        display: flex !important;
        align-items: center;
        flex-shrink: 0;
        width: 100%;
    }

    .sticky-panel-header h3 {
        font-size: 0.8rem !important;
        font-weight: 900 !important;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        color: var(--text-main);
        margin: 0;
    }

    .panel-content {
        flex: 1;
        padding: var(--panel-padding) !important;
        width: 100%;
        overflow: visible !important;
        display: flex;
        flex-direction: column;
        justify-content: center;
    }

    /* Horizontal Sub-Snapping Support */
    .horizontal-snap-container {
        display: flex !important;
        flex-direction: row !important;
        overflow-x: auto !important;
        scroll-snap-type: x mandatory;
        scrollbar-width: none;
        -webkit-overflow-scrolling: touch;
        width: 100%;
        flex: 1;
    }
    .horizontal-snap-container::-webkit-scrollbar { display: none; }
    .horizontal-snap-item {
        min-width: 100%;
        width: 100%;
        scroll-snap-align: start;
        scroll-snap-stop: always;
        display: flex;
        flex-direction: column;
        height: 100%;
    }

    #sidebar {
        position: fixed;
        left: -100%;
        top: 0;
        bottom: 0;
        z-index: 300;
        width: 85%;
        transition: left 0.3s ease;
        box-shadow: 4px 0 15px rgb(0,0,0,0.1);
        background: var(--bg-card);
    }
    #sidebar.open { left: 0; }
    
    #sidebarOverlay {
        display: none;
        position: fixed;
        inset: 0;
        background: rgb(0,0,0,0.5);
        z-index: 290;
    }
    #sidebarOverlay.open { display: block; }

    #bottomNav {
        height: var(--bottom-nav-height);
        flex-shrink: 0;
        z-index: 100;
        background: var(--bg-card);
        border-top: 1px solid var(--border-color);
        display: flex !important;
    }

    .grid-cols-4, .md\:grid-cols-2, .md\:grid-cols-3, .md\:grid-cols-4 { grid-template-columns: 1fr; }
    .rounded-3xl, .rounded-[2.5rem] { border-radius: 1rem; }

    /* Login View Mobile Optimization */
    #loginView {
        position: fixed;
        inset: 0;
        width: 100%;
        height: 100dvh;
        scroll-snap-type: y mandatory;
        overflow-y: scroll !important;
        z-index: 200;
        background: #fff;
    }
    #loginView section, #loginView footer {
        min-height: 100dvh !important;
        height: 100dvh !important;
        scroll-snap-align: start;
        scroll-snap-stop: always;
        display: flex;
        flex-direction: column;
        justify-content: center;
        padding: var(--panel-padding) !important;
        margin: 0 !important;
        position: relative;
    }

    /* Accessibility Navigation Utilities */
    .global-scroll-handle {
        position: fixed;
        bottom: var(--bottom-nav-height);
        left: 50%;
        transform: translateX(-50%);
        width: 50px;
        height: 20px;
        background: var(--bg-card);
        backdrop-filter: blur(16px);
        backdrop-filter: blur(16px);
        border: 1px solid var(--border-color);
        border-bottom: none;
        border-radius: 12px 12px 0 0;
        color: var(--text-muted);
        display: flex !important;
        align-items: center;
        justify-content: center;
        z-index: 150;
        box-shadow: 0 -4px 10px -2px rgb(0, 0, 0, 0.05);
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        touch-action: none;
    }
    
    /* Slide up when dots are present */
    .has-dots .global-scroll-handle {
        bottom: calc(var(--bottom-nav-height) + 32px);
        border-bottom: 1px solid var(--border-color);
        border-radius: 12px;
        height: 32px;
    }

    .global-scroll-handle .iconify { font-size: 1.2rem !important; }
    .global-scroll-handle:active { height: 28px; color: var(--primary); background: var(--bg-main); }
    .global-scroll-handle.is-holding { height: 32px; width: 60px; color: var(--primary); background: var(--bg-main); }

    .swipe-dots {
        display: flex;
        gap: 6px;
        justify-content: center;
        padding: 12px 0;
        background: var(--bg-card);
        border-top: 1px solid var(--border-color);
        width: 100%;
    }

    /* Active state for dots when they should affect the scroll handle */
    body.has-dots .global-scroll-handle {
        bottom: calc(var(--bottom-nav-height) + 40px);
        border-bottom: 1px solid var(--border-color);
        border-radius: 12px;
        height: 32px;
    }
    .swipe-dot {
        width: 6px;
        height: 6px;
        border-radius: 50%;
        background: var(--border-color);
        transition: all 0.3s ease;
    }
    .swipe-dot.active {
        width: 18px;
        background: var(--primary);
        border-radius: 4px;
    }

    .quick-jump-drawer {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: 75vh;
        background: white;
        z-index: 400;
        border-top-left-radius: 2.5rem;
        border-top-right-radius: 2.5rem;
        box-shadow: 0 -20px 50px -10px rgb(0, 0, 0, 0.2);
        transform: translateY(100%);
        transition: transform 0.4s cubic-bezier(0.16, 1, 0.3, 1);
        display: flex;
        flex-direction: column;
    }
    .quick-jump-drawer.is-visible { transform: translateY(0); }
    .quick-jump-overlay {
        position: fixed;
        inset: 0;
        background: rgb(0, 0, 0, 0.5);
        backdrop-filter: blur(4px);
        z-index: 390;
        display: none;
    }
    .quick-jump-overlay.is-visible { display: block; }
}

/* ==================== DESKTOP PREMIUM LAYOUT ==================== */
@media (min-width: 769px) {
    #appView {
        height: 100vh;
        overflow: hidden;
    }

    #mainContent {
        height: calc(100vh - var(--header-height));
        padding: 0 !important;
        overflow-y: scroll !important;
        scroll-snap-type: y mandatory;
        scrollbar-width: thin;
        scrollbar-color: var(--border-color) transparent;
        background: var(--bg-main);
    }

    /* Section snapping - each module takes full viewport */
    #mainContent > * {
        min-height: calc(100vh - var(--header-height)) !important;
        scroll-snap-align: start;
        scroll-snap-stop: always;
        display: flex;
        flex-direction: column;
        background: var(--bg-main);
        padding: var(--spacing-lg) !important;
        box-sizing: border-box;
    }

    .panels-container {
        display: grid;
        gap: var(--spacing-lg);
        flex: 1;
        min-height: 0;
        width: 100%;
        max-width: 100%;
        margin: 0 auto;
    }

    /* Individual Panel behavior on Desktop */
    .panels-container > * {
        height: 100%;
        min-height: 0 !important; /* Allow panel to be smaller than screen if in grid */
        background: var(--bg-card);
        border-radius: var(--radius-xl);
        border: 1px solid var(--border-color);
        box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
        display: flex !important;
        flex-direction: column !important;
        overflow: hidden !important;
        padding: 0 !important;
        position: relative;
    }

    .panel-content {
        flex: 1;
        overflow-y: auto !important;
        padding: var(--panel-padding) !important;
        scrollbar-width: thin;
        scrollbar-color: var(--border-color) transparent;
        display: flex;
        flex-direction: column;
    }

    /* Fixed Panel Headers on Desktop (No longer sticky-mobile) */
    .sticky-panel-header {
        position: relative !important;
        top: auto !important;
        background: var(--bg-card) !important;
        border-bottom: 1px solid var(--border-color) !important;
        padding: 0.75rem var(--panel-padding) !important;
        height: auto !important;
        min-height: 48px;
        backdrop-filter: none !important;
        flex-shrink: 0;
    }

    .sticky-panel-header h3 {
        font-size: 0.75rem !important;
        font-weight: 800 !important;
    }

    /* Module-Specific Desktop Grid Layouts */
    [data-current-module="overview"] .panels-container {
        grid-template-columns: 1fr 1fr;
        grid-template-rows: 1.1fr 0.9fr;
    }

    [data-current-module="questions"] .panels-container {
        grid-template-columns: 1fr 1.5fr 1fr;
    }

    [data-current-module="models"] .panels-container {
        grid-template-columns: 2fr 1fr;
    }

    [data-current-module="investigation"] .panels-container {
        grid-template-columns: 0.8fr 2.2fr;
    }

    [data-current-module="analysis"] .panels-container {
        grid-template-columns: 2fr 1fr;
    }

    [data-current-module="math"] .panels-container {
        grid-template-columns: 1fr 1fr;
        grid-template-rows: 1fr 1fr;
    }

    [data-current-module="explanations"] .panels-container {
        grid-template-columns: 0.8fr 1.2fr;
        grid-template-rows: repeat(3, 1fr);
        grid-template-areas: 
            "bank claim"
            "bank evidence"
            "bank reasoning";
    }

    /* Teacher Dashboard Grid Layouts */
    [data-teacher-module="access"] .panels-container,
    [data-teacher-module="lessons"] .panels-container {
        grid-template-columns: 1fr 1fr;
    }

    [data-teacher-module="overview"] .panels-container {
        display: flex !important; /* Teacher overview has internal grid logic */
    }

    [data-card-title="Evidence Bank"] { grid-area: bank; }
    [data-card-title="Scientific Claim"] { grid-area: claim; }
    [data-card-title="Evidence Description"] { grid-area: evidence; }
    [data-card-title="Reasoning"] { grid-area: reasoning; }

    /* Desktop typography adjustments */
    .text-\[10px\], .text-\[9px\] { font-size: 0.75rem !important; }

    /* Desktop Accessibility Handle Positioning */
    .global-scroll-handle {
        display: flex !important;
        bottom: 2rem;
        right: 2rem;
        left: auto;
        transform: none;
        width: 48px;
        height: 48px;
        border-radius: 50%;
        border: 1px solid var(--border-color);
        box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1);
        background: white;
    }
    
    .has-dots .global-scroll-handle {
        bottom: 2rem;
    }
}

/* Info Tip Styles */
.info-tip-trigger {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--primary-light);
    color: var(--primary);
    cursor: pointer;
    transition: all 0.2s ease;
    margin-left: 4px;
    vertical-align: middle;
}
.info-tip-trigger:hover, .info-tip-trigger:active {
    background: var(--primary);
    color: white;
}

.info-popover {
    position: fixed;
    z-index: 500;
    width: 280px;
    background: #1e293b;
    color: white;
    padding: 1rem;
    border-radius: 1.25rem;
    box-shadow: 0 20px 25px -5px rgb(0, 0, 0, 0.2);
    font-size: 0.75rem;
    line-height: 1.5;
    pointer-events: none;
    opacity: 0;
    transform: translateY(10px) scale(0.95);
    transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
    border: 1px solid rgb(255, 255, 255, 0.1);
}
.info-popover.is-visible {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: auto;
}
.info-popover-arrow {
    position: absolute;
    width: 12px;
    height: 12px;
    background: #1e293b;
    transform: rotate(45deg);
    z-index: -1;
}

/* ==================== SHARED UI COMPONENTS ==================== */
.model-canvas { 
    background: var(--bg-main);
    background-image: radial-gradient(#e2e8f0 1px, transparent 1px);
    background-size: 20px 20px;
    position: relative;
    overflow: hidden;
    cursor: crosshair;
    touch-action: pan-y;
}

.model-node {
    position: absolute;
    background: var(--bg-card);
    border: 2px solid var(--primary);
    border-radius: var(--radius-lg);
    cursor: move;
    user-select: none;
    box-shadow: 0 4px 12px rgb(0,0,0,0.08);
    z-index: 10;
    touch-action: none;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 10px;
    pointer-events: auto !important;
}

.modal-backdrop {
    background: rgb(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
    opacity: 0;
    transition: opacity 0.4s ease;
    pointer-events: none;
}
.modal-backdrop.is-visible {
    opacity: 1;
    pointer-events: auto;
}

.modal-bottom-drawer {
    background: white;
    border-top-left-radius: 2.5rem;
    border-top-right-radius: 2.5rem;
    transform: translateY(100%);
    transition: transform 0.4s cubic-bezier(0.16, 1, 0.3, 1);
    height: 85vh;
}
.modal-bottom-drawer.is-visible {
    transform: translateY(0);
}

/* Landscape Modeling Optimization */
@media (orientation: landscape) and (max-width: 932px) and (max-height: 450px) {
    .view-student[data-current-module="models"] #appHeader,
    .view-student[data-current-module="models"] #bottomNav,
    .view-student[data-current-module="models"] .sticky-panel-header,
    .view-student[data-current-module="models"] .coaching-tips {
        display: none !important;
    }

    .view-student[data-current-module="models"] #appView {
        height: 100dvh;
    }

    .view-student[data-current-module="models"] #mainContent {
        height: 100dvh;
        overflow: hidden !important;
    }

    .view-student[data-current-module="models"] .panels-container {
        height: 100dvh;
    }

    .view-student[data-current-module="models"] [data-card-title="Model Canvas"] {
        height: 100dvh !important;
        min-height: 100dvh !important;
    }

    .view-student[data-current-module="models"] #modelContextBar {
        position: absolute;
        top: 0;
        left: 60px;
        right: 0;
        z-index: 100;
        background: rgb(255, 255, 255, 0.9);
        backdrop-filter: blur(8px);
        border-radius: 0 0 0 20px;
    }
}

.toast { animation: slideIn 0.3s ease; }
@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

.ngss-tag {
    font-size: 0.65rem;
    padding: 2px 6px;
    border-radius: 4px;
    font-weight: 500;
}
.ngss-sep { background: #dbeafe; color: #1e40af; }
.ngss-ccc { background: #fef3c7; color: #92400e; }
