/* ========================================
   Worksheet Generator Styles - Optimized
   ======================================== */
.worksheet-generator-page {
    width: 100%;
    flex: 1 1 0%;
    min-height: 0;
    max-height: 100%;
    padding: 20px;
    display: flex;
    flex-direction: column;
    background: var(--bg-color);
}

.worksheet-container {
    flex: 1 1 0%;
    display: grid;
    grid-template-columns: 300px 1fr;
    grid-template-rows: minmax(0, 1fr);
    gap: 20px;
    min-height: 0;
    max-height: 100%;
    /* Grid items default min-height:auto — can block shrinking; children also get min-height:0 below */
    align-items: stretch;
    position: relative;
    isolation: isolate;
}

.worksheet-container > .worksheet-controls,
.worksheet-container > .worksheet-preview {
    min-height: 0;
}

/* Controls Panel */
.worksheet-controls {
    background: var(--element-bg);
    border: 1px solid var(--border-color);
    border-radius: 24px;
    padding: 12px 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    align-self: start;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
    position: relative;
    z-index: 2;
}

.controls-header h2 {
    font-size: 1.35rem;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 4px;
}

.controls-header p {
    font-size: 0.8rem;
    color: var(--text-secondary);
    line-height: 1.4;
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.control-group>label {
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-secondary);
}

/* Button Group */
.button-group {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
}

.option-btn {
    padding: 7px 14px;
    border: 1.5px solid var(--border-color);
    border-radius: 12px;
    background: var(--bg-color);
    color: var(--text-secondary);
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
}

.option-btn:hover {
    border-color: var(--text-secondary);
    background: #fff;
}

.option-btn.active {
    background: var(--text-primary);
    border-color: var(--text-primary);
    color: var(--bg-color);
}

/* Checkbox Group */
.checkbox-group {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.checkbox-option {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    padding: 8px 12px;
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.2s ease;
}

.checkbox-option:hover {
    background: #fff;
    border-color: var(--text-secondary);
}

.checkbox-option input[type="checkbox"] {
    width: 15px;
    height: 15px;
    accent-color: var(--text-highlight);
    cursor: pointer;
    margin-top: 4px;
}

.checkbox-option span {
    font-size: 0.85rem;
    color: var(--text-primary);
    font-weight: 500;
}

/* Generate Button */
.generate-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 20px;
    background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
    color: #fff;
    border: none;
    border-radius: 12px;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    margin-top: 8px;
}

.generate-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.15);
}

.generate-btn:active {
    transform: translateY(0);
}

.generate-btn svg {
    stroke: currentColor;
    width: 18px;
    height: 18px;
}

/* Preview Panel */
.worksheet-preview {
    background: var(--element-bg);
    border: 1px solid var(--border-color);
    border-radius: 24px;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-height: 0;
    min-width: 0;
    height: 100%;
    max-height: 100%;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
    position: relative;
    z-index: 1;
}

.preview-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 14px;
    background: var(--element-bg);
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}

.preview-tabs {
    display: flex;
    gap: 6px;
}

.preview-tab {
    padding: 6px 14px;
    border: 1.5px solid transparent;
    border-radius: 12px;
    background: transparent;
    color: var(--text-secondary);
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
}

.preview-tab:hover {
    background: var(--bg-color);
}

.preview-tab.active {
    background: var(--text-primary);
    color: var(--bg-color);
}

.export-buttons {
    display: flex;
    gap: 6px;
}

.export-btn {
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 6px 12px;
    border: 1.5px solid var(--border-color);
    border-radius: 12px;
    background: var(--bg-color);
    color: var(--text-secondary);
    font-size: 0.75rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
}

.export-btn:hover:not(:disabled) {
    border-color: var(--text-highlight);
    color: var(--text-highlight);
    background: #fff;
}

.export-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.export-btn svg {
    stroke: currentColor;
    width: 14px;
    height: 14px;
}

.preview-content {
    flex: 1 1 0%;
    min-height: 0;
    /* Flexbox scroll region: ignore intrinsic content height so overflow can kick in */
    height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-gutter: stable;
    scrollbar-width: thin;
    scrollbar-color: var(--border-color) var(--bg-color);
    padding: 20px;
}

/* Make the scrollbar easy to see (incl. macOS overlay / thin defaults) */
.preview-content::-webkit-scrollbar {
    width: 10px;
}

.preview-content::-webkit-scrollbar-track {
    background: var(--bg-color);
    border-radius: 6px;
}

.preview-content::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 6px;
    border: 2px solid var(--bg-color);
}

.preview-content::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary);
}

.preview-placeholder {
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    color: var(--text-muted);
}

/* display:flex above overrides the [hidden] attribute — must hide explicitly */
.preview-placeholder[hidden] {
    display: none !important;
}

.preview-placeholder svg {
    opacity: 0.25;
    width: 40px;
    height: 40px;
}

.preview-placeholder p {
    font-size: 0.9rem;
    text-align: center;
}

/* ===== Worksheet Paper Styles ===== */
.worksheet-paper {
    max-width: 680px;
    margin: 0 auto;
}

.worksheet-header {
    margin-bottom: 20px;
    padding-bottom: 16px;
    border-bottom: 2px solid var(--text-primary);
}

.header-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
}

.header-top h1 {
    font-size: 1.4rem;
    font-weight: 700;
    color: var(--text-primary);
    letter-spacing: -0.02em;
}

.worksheet-id-badge {
    font-size: 0.7rem;
    font-weight: 600;
    color: var(--text-secondary);
    background: var(--element-bg);
    padding: 4px 10px;
    border-radius: 8px;
    font-family: 'SF Mono', monospace;
    border: 1px solid var(--border-color);
}

.header-fields {
    display: flex;
    gap: 24px;
    flex-wrap: wrap;
    margin-bottom: 12px;
}

.field-group {
    display: flex;
    align-items: baseline;
    gap: 6px;
}

.field-label {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-secondary);
}

.field-line {
    width: 140px;
    border-bottom: 1px solid var(--text-primary);
}

.field-line.short {
    width: 40px;
}

.field-value {
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--text-primary);
}

.score-total {
    font-size: 0.8rem;
    color: var(--text-secondary);
}

.instructions {
    font-size: 0.8rem;
    color: var(--text-secondary);
    font-style: italic;
}

/* Questions Grid */
.questions-grid {
    display: flex;
    flex-direction: column;
    gap: 5px;
}

.question-row {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 10px;
    background: var(--element-bg);
    border-radius: 12px;
    border: 1px solid var(--border-color);
}

.q-num {
    font-size: 0.7rem;
    font-weight: 700;
    color: var(--text-highlight);
    min-width: 18px;
}

.q-equation {
    font-size: 0.8rem;
    font-family: 'SF Mono', 'Monaco', 'Consolas', monospace;
    color: var(--text-primary);
    letter-spacing: 0.01em;
}

/* Equation Elements */
.coef {
    display: inline-block;
    min-width: 14px;
    text-align: center;
    margin-right: 1px;
    font-weight: 700;
}

.coef.blank {
    border-bottom: 2px solid var(--text-primary);
    color: transparent;
    margin-bottom: -2px;
}

.coef.filled {
    color: #047857;
}

.formula {
    font-weight: 500;
}

.formula sub {
    font-size: 0.7em;
}

.plus {
    margin: 0 4px;
    color: var(--text-secondary);
    font-weight: 500;
}

.arrow {
    margin: 0 8px;
    color: var(--text-primary);
    font-weight: 500;
}

.worksheet-footer {
    margin-top: 24px;
    padding-top: 12px;
    border-top: 1px solid var(--border-color);
    text-align: center;
    font-size: 0.7rem;
    color: var(--text-muted);
}

/* Answer Key Specific */
.answer-key .question-row {
    background: linear-gradient(135deg, #ecfdf5 0%, #d1fae5 100%);
    border-color: #a7f3d0;
}

.answer-key .q-num {
    color: #047857;
}

.answer-key .coef.filled {
    color: #047857;
    font-weight: 700;
}

/* ===== Practice Mode Styles ===== */
.practice-mode .worksheet-header {
    border-bottom-color: #6366f1;
}

.practice-mode .header-top h1 {
    color: #4f46e5;
}

/* Coefficient Input */
.coef-input {
    width: 30px;
    height: 22px;
    text-align: center;
    font-size: 0.75rem;
    font-weight: 700;
    font-family: 'SF Mono', monospace;
    border: 1.5px solid #cbd5e1;
    border-radius: 8px;
    background: #fff;
    color: var(--text-primary);
    outline: none;
    transition: all 0.2s ease;
    margin-right: 1px;
    -moz-appearance: textfield;
    appearance: textfield;
}

.coef-input::-webkit-outer-spin-button,
.coef-input::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.coef-input:focus {
    border-color: #6366f1;
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.15);
}

.coef-input.correct {
    border-color: #10b981;
    background: #ecfdf5;
    color: #047857;
}

.coef-input.incorrect {
    border-color: #ef4444;
    background: #fef2f2;
    color: #dc2626;
}

/* Question Row Results */
.question-row.correct {
    background: linear-gradient(135deg, #ecfdf5 0%, #d1fae5 100%);
    border-color: #a7f3d0;
}

.question-row.incorrect {
    background: linear-gradient(135deg, #fef2f2 0%, #fee2e2 100%);
    border-color: #fca5a5;
}

.result-icon {
    font-size: 1.2rem;
    font-weight: 700;
    margin-left: auto;
}

.question-row.correct .result-icon {
    color: #10b981;
}

.question-row.incorrect .result-icon {
    color: #ef4444;
}

/* Practice Actions */
.practice-actions {
    display: flex;
    align-items: center;
    gap: 16px;
    margin-top: 20px;
    padding-top: 16px;
    border-top: 1px dashed var(--border-color);
}

.check-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 20px;
    background: linear-gradient(135deg, #6366f1 0%, #4f46e5 100%);
    color: #fff;
    border: none;
    border-radius: 12px;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
}

.check-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
}

.check-btn:active {
    transform: translateY(0);
}

.check-btn svg {
    stroke: currentColor;
}

/* Score Display */
.score-display {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    background: var(--element-bg);
    border-radius: 12px;
    border: 1px solid var(--border-color);
}

.score-label {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-secondary);
}

.score-value {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--text-primary);
}

.score-value.perfect {
    color: #10b981;
}

/* NEW STYLES: Navigation and Footer Toggle */
.top-right-nav {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-bottom: 0;
}

.controls-footer {
    margin-top: auto;
    padding-top: 20px;
}

.lang-toggle-minimal {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: transparent;
    border: 1px solid transparent;
    border-radius: 12px;
    color: var(--text-secondary);
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    width: 100%;
    justify-content: flex-start;
}

.lang-toggle-minimal:hover {
    background: var(--element-bg);
    color: var(--text-primary);
    border-color: var(--border-color);
}

/* =========================================
   New UI Elements (Subtitles, Trust Text)
   ========================================= */
.checkbox-text-wrapper {
    display: flex;
    flex-direction: column;
    margin-left: 2px;
}

.checkbox-subtitle {
    display: block;
    font-size: 0.7rem;
    color: var(--text-muted);
    font-family: 'SF Mono', monospace;
    margin-top: 2px;
    opacity: 0.8;
    line-height: 1.2;
}

.trust-text {
    font-size: 0.75rem;
    color: var(--text-secondary);
    text-align: center;
    margin-top: 12px;
    line-height: 1.4;
    padding: 0 4px;
    opacity: 0.8;
}

.preview-summary-bar {
    background: #f8fafc;
    color: #475569;
    font-size: 0.8rem;
    font-weight: 600;
    padding: 10px 16px;
    border-radius: 12px;
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    gap: 8px;
    border: 1px solid #e2e8f0;
}

.preview-summary-icon {
    color: #64748b;
    width: 16px;
    height: 16px;
}

/* =========================================
   Responsive Design (Shrinking Window)
   ========================================= */

@media (max-width: 1100px) {
    .worksheet-container {
        grid-template-columns: 1fr;
        grid-template-rows: auto minmax(0, 1fr);
    }

    .worksheet-container > .worksheet-controls {
        order: -1;
        max-height: min(42dvh, 420px);
        overflow-y: auto;
    }
}

@media (max-height: 800px) {
    .controls-header p {
        font-size: 0.75rem;
        line-height: 1.2;
        margin-bottom: 0;
    }
    .trust-text {
        font-size: 0.7rem;
        margin-top: 6px;
        line-height: 1.2;
    }
    .checkbox-subtitle {
        font-size: 0.65rem;
        line-height: 1.1;
    }
    .worksheet-controls {
        gap: 6px;
    }
}

@media (max-height: 680px) {
    .controls-header p,
    .trust-text,
    .checkbox-subtitle {
        display: none;
    }
    
    .checkbox-text-wrapper {
        justify-content: center;
    }
}

/* =========================================
   Print / PDF Export Styles (Native Browser PDF)
   ========================================= */
@media print {
    /* Hide all application chrome and sidebars */
    #global-nav,
    #version-dropdown,
    #floating-about-btn,
    .worksheet-controls,
    .preview-header,
    .preview-summary-bar,
    .practice-actions {
        display: none !important;
    }

    /* Reset layout constraints to allow full-page flowing */
    body, html {
        background: #fff !important;
        margin: 0;
        padding: 0;
        height: auto !important;
        overflow: visible !important;
    }

    #app-viewport,
    #main-container,
    .page-container,
    .worksheet-generator-page,
    .worksheet-container,
    .worksheet-preview,
    .preview-content {
        display: block !important;
        height: auto !important;
        min-height: 0 !important;
        max-height: none !important;
        overflow: visible !important;
        background: transparent !important;
        border: none !important;
        box-shadow: none !important;
        padding: 0 !important;
        margin: 0 !important;
    }

    /* Make the paper fill the printable area completely */
    .worksheet-paper {
        width: 100% !important;
        max-width: 100% !important;
        margin: 0 !important;
        padding: 10mm !important;
    }

    /* Ensure equations aren't split across pages */
    .question-row {
        break-inside: avoid;
        page-break-inside: avoid;
        border: 1px solid #e2e8f0 !important;
        background: transparent !important; /* Strip practice colors for print */
    }

    .q-num {
        color: #000 !important;
    }

    /* Enforce exact colors for answer keys */
    * {
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
    }
}

/* =============================================================================
   Worksheet hub (type picker) + equation panel
   ============================================================================= */
/* Worksheets overlay: plain .blank-page centers flex children — force column + full viewport
   so #worksheet-shell gets a real height (detail panels are position:absolute and add no in-flow size). */
#blank-page-2.blank-page.active,
#blank-page-2.worksheet-root-page.active {
    display: flex !important;
    flex-direction: column !important;
    align-items: stretch !important;
    /* Top-align hub so title/cards are not clipped when content exceeds viewport */
    justify-content: flex-start !important;
    box-sizing: border-box !important;
    height: 100dvh !important;
    max-height: 100dvh !important;
    overflow: hidden !important;
    /* Clear fixed global nav (z-index 10000); 3dvh was too small and hid the back bar under the nav */
    padding-top: var(--nav-offset) !important;
    padding-bottom: clamp(12px, 3dvh, 60px) !important;
}

/* Detail panels: back bar must sit below nav; fill viewport from that point */
#blank-page-2:has(#worksheet-shell.worksheet-shell--detail) {
    justify-content: flex-start !important;
    overflow: hidden !important;
}

.worksheet-root-page {
    padding: 0;
    overflow-x: hidden;
    overflow-y: auto;
}

/* Shell is the flex child that fills space below nav padding; absolute panels size to this box */
#worksheet-shell.worksheet-shell {
    position: relative;
    flex: 1 1 0%;
    min-height: 0;
    width: 100%;
    min-width: 0;
    max-width: none;
    height: auto;
    display: flex;
    flex-direction: column;
}

#worksheet-hub.worksheet-hub {
    display: flex;
    flex-direction: column;
    flex: 1 1 0%;
    min-height: 0;
    width: 100%;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    justify-content: flex-start;
    align-items: center;
}

.worksheet-hub-inner {
    height: auto;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    width: 100%;
    padding-top: clamp(12px, 2dvh, 24px);
    padding-bottom: clamp(12px, 2dvh, 24px);
}

.worksheet-hub-grid {
    max-height: none;
}

/* Worksheets hub: fixed 2×2 grid for four worksheet types */
#blank-page-2.blank-page.active:not(:has(.worksheet-shell--detail)) .worksheet-hub-grid.chem-tools-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-rows: auto;
    flex: 0 0 auto;
    max-height: none;
    gap: clamp(14px, 2vw, 24px);
}

@media (max-width: 768px) {
    #blank-page-2.blank-page.active:not(:has(.worksheet-shell--detail)) .worksheet-hub-grid.chem-tools-grid {
        grid-template-columns: 1fr;
    }
}

#blank-page-2 .worksheet-type-card.chem-tool-card {
    height: auto;
    min-height: 0;
    overflow: visible;
}

/* Interactive Lab hub — align with Worksheets / Summary hub layout */
#interactive-lab-page.blank-page.active,
#interactive-lab-page.lab-hub-page.active {
    display: flex !important;
    flex-direction: column !important;
    align-items: stretch !important;
    justify-content: flex-start !important;
    box-sizing: border-box !important;
    height: 100dvh !important;
    max-height: 100dvh !important;
    overflow: hidden !important;
    padding-top: var(--nav-offset) !important;
    padding-bottom: clamp(12px, 3dvh, 60px) !important;
}

#interactive-lab-page .blank-page-content {
    flex: 1 1 0%;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    padding-top: 0;
}

#interactive-lab-page .lab-hub-inner {
    flex: 1 1 0%;
    min-height: 0;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    justify-content: flex-start;
    padding-top: clamp(12px, 2dvh, 24px);
    padding-bottom: clamp(12px, 2dvh, 24px);
}

#interactive-lab-page .lab-hub-grid.chem-tools-grid {
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 480px), 1fr));
    grid-template-rows: auto;
    flex: 0 0 auto;
    max-height: none;
}

#interactive-lab-page .lab-tool-card.chem-tool-card {
    height: auto;
    min-height: 0;
    overflow: visible;
}

.worksheet-type-card--pending {
    cursor: default;
    pointer-events: none;
    opacity: 0.95;
}

.worksheet-type-card--pending:hover,
.worksheet-type-card--pending:active {
    transform: none;
}

/* Worksheet detail panels: hidden by default; full-viewport overlay when active */
#worksheet-panel-equation.worksheet-panel,
#worksheet-panel-isotope-ram.worksheet-panel,
#worksheet-panel-cursor-chem.worksheet-panel,
#worksheet-panel-microscopic-world-i-exercise.worksheet-panel,
#worksheet-panel-topic-3-metals-exercise.worksheet-panel {
    display: none !important;
    flex-direction: column;
    position: absolute;
    inset: 0;
    z-index: 3;
    width: 100%;
    height: 100%;
    min-height: 0;
    max-height: 100%;
    background: var(--bg-color);
    box-sizing: border-box;
}

#worksheet-shell.worksheet-shell--detail #worksheet-hub {
    display: none !important;
}

#worksheet-shell.worksheet-shell--detail[data-worksheet-panel="equation"] #worksheet-panel-equation {
    display: flex !important;
}

#worksheet-shell.worksheet-shell--detail[data-worksheet-panel="isotope-ram"] #worksheet-panel-isotope-ram {
    display: flex !important;
}

#worksheet-shell.worksheet-shell--detail[data-worksheet-panel="cursor-chem"] #worksheet-panel-cursor-chem {
    display: flex !important;
}

#worksheet-shell.worksheet-shell--detail[data-worksheet-panel="microscopic-world-i-exercise"] #worksheet-panel-microscopic-world-i-exercise {
    display: flex !important;
}

#worksheet-shell.worksheet-shell--detail[data-worksheet-panel="topic-3-metals-exercise"] #worksheet-panel-topic-3-metals-exercise {
    display: flex !important;
}

.worksheet-panel-bar {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    position: relative;
    z-index: 6;
    padding: 14px 20px 12px;
    border-bottom: 1px solid var(--border-color);
    background: var(--element-bg);
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.04);
}

/* Match worksheet .option-btn / control chips (not default browser button) */
.worksheet-back-btn {
    appearance: none;
    -webkit-appearance: none;
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    margin: 0;
    padding: 7px 16px 7px 14px;
    border: 1.5px solid var(--border-color);
    border-radius: 12px;
    background: var(--bg-color);
    color: var(--text-secondary);
    font-family: inherit;
    font-size: 0.8rem;
    font-weight: 600;
    letter-spacing: 0.01em;
    line-height: 1.25;
    cursor: pointer;
    transition:
        border-color 0.2s ease,
        background 0.2s ease,
        color 0.2s ease,
        box-shadow 0.2s ease,
        transform 0.1s ease;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}

.worksheet-back-btn::before {
    content: "←";
    display: inline-block;
    font-size: 1em;
    opacity: 0.75;
    transform: translateY(-0.5px);
}

.worksheet-back-btn:hover {
    border-color: var(--text-secondary);
    background: #fff;
    color: var(--text-primary);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.worksheet-back-btn:active {
    transform: scale(0.98);
}

.worksheet-back-btn:focus-visible {
    outline: 2px solid var(--text-highlight);
    outline-offset: 2px;
}

/* Scroll happens inside .preview-content only; body does not expand past the viewport */
.worksheet-panel-body {
    flex: 1 1 0%;
    min-height: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

#worksheet-shell.worksheet-shell--detail[data-worksheet-panel="equation"] .worksheet-generator-page {
    flex: 1 1 0%;
    min-height: 0;
    max-height: 100%;
}

/* Equation overlay: harden flex chain so the preview column cannot grow past the shell */
#worksheet-shell.worksheet-shell--detail[data-worksheet-panel="equation"] #worksheet-panel-equation {
    min-height: 0;
    isolation: isolate;
    /* Sit above sibling embed panels (z-index: 4) if layout overlap creates dead hit targets on desktop */
    z-index: 5;
}

/* Embedded HTML/React worksheets: back bar + iframe only (no nested generator card) */
.worksheet-panel--embed-full {
    min-height: 0;
    display: flex !important;
    flex-direction: column !important;
}

.worksheet-panel--embed-full .worksheet-embed-fs {
    position: relative;
    flex: 1 1 0% !important;
    min-height: 0 !important;
    min-width: 0;
    width: 100% !important;
    overflow: hidden;
    background: var(--bg-color);
}

.worksheet-panel--embed-full .worksheet-embed-fs-frame {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    border: 0;
    display: block;
    background: var(--bg-color);
    /* iPad Safari: nested position:fixed under another fixed overlay broke iframe hit-testing; stay absolute like equation panel */
    touch-action: manipulation;
    -webkit-transform: translateZ(0);
}

#worksheet-shell.worksheet-shell--detail[data-worksheet-panel="isotope-ram"] #worksheet-panel-isotope-ram,
#worksheet-shell.worksheet-shell--detail[data-worksheet-panel="cursor-chem"] #worksheet-panel-cursor-chem,
#worksheet-shell.worksheet-shell--detail[data-worksheet-panel="microscopic-world-i-exercise"] #worksheet-panel-microscopic-world-i-exercise,
#worksheet-shell.worksheet-shell--detail[data-worksheet-panel="topic-3-metals-exercise"] #worksheet-panel-topic-3-metals-exercise {
    min-height: 0;
}

/* Embedded worksheets: same stacking/sizing path as equation panel (absolute inset inside #worksheet-shell). */
#worksheet-shell.worksheet-shell--detail[data-worksheet-panel="isotope-ram"] #worksheet-panel-isotope-ram.worksheet-panel--embed-full,
#worksheet-shell.worksheet-shell--detail[data-worksheet-panel="cursor-chem"] #worksheet-panel-cursor-chem.worksheet-panel--embed-full,
#worksheet-shell.worksheet-shell--detail[data-worksheet-panel="microscopic-world-i-exercise"] #worksheet-panel-microscopic-world-i-exercise.worksheet-panel--embed-full,
#worksheet-shell.worksheet-shell--detail[data-worksheet-panel="topic-3-metals-exercise"] #worksheet-panel-topic-3-metals-exercise.worksheet-panel--embed-full {
    isolation: isolate;
    z-index: 4;
}