/* =========================================================
   1. BRAND VARIABLES
   ========================================================= */
:root {
    --brand-red: #ec0000;
    --brand-red-light: #fca5a5;
    --brand-red-dark: #991b1b;

    --sky-blue: #3b82f6;
    --sky-blue-light: #93c5fd;
    --sky-blue-dark: #1e40af;

    --forest-green: #16a34a;
    --forest-green-light: #4ade80;
    --forest-green-dark: #14532d;

    --light-gray: #f3f4f6;
    --medium-gray: #9ca3af;
    --dark-gray: #1f2937;
    --white: #ffffff;
    --gray-100: #f3f4f6;
    --card-shadow: rgba(0, 0, 0, 0.1);

    --font-main: 'Poppins', sans-serif;
    --font-alt: Arial, sans-serif;

    /* APP MAPPINGS */
    --app-bg: var(--light-gray);
    --card-bg: var(--white);
    --primary-btn: var(--brand-red);
    --primary-btn-hover: var(--brand-red-dark);
    --answer-bg: #e5e7eb; 
}

/* =========================================================
   2. GLOBAL LAYOUT
   ========================================================= */
body {
    font-family: var(--font-main);
    background-color: var(--app-bg);
    color: var(--dark-gray);
    margin: 0;
    padding: 20px;
    display: flex;
    justify-content: center;
}

@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap');

.main-wrapper {
    max-width: 1200px;
    width: 100%;
}

h1 { 
    text-align: center; 
    color: var(--dark-gray); 
    margin-bottom: 25px; 
    font-weight: 700;
}

/* =========================================================
   3. CONTROL PANEL
   ========================================================= */
.control-panel {
    position: relative;
    background: var(--white);
    padding: 30px;
    margin-bottom: 30px;
    border-radius: 12px;
    border: 1px solid #e0e0e0;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.control-panel::before {
    content: "";
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 60px;
    height: 5px;
    background-color: var(--brand-red);
    border-bottom-left-radius: 4px;
    border-bottom-right-radius: 4px;
}

.topic-selector {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 10px;
}

.topic-btn {
    padding: 10px 18px;
    border: 1px solid var(--medium-gray);
    background: var(--white);
    color: var(--dark-gray);
    border-radius: 5px; 
    cursor: pointer;
    font-family: var(--font-main);
    font-weight: 600;
    transition: all 0.2s ease;
    font-size: 0.9rem;
}

.topic-btn:hover { 
    background: var(--light-gray);
    border-color: var(--brand-red-light);
    color: var(--brand-red);
}

.topic-btn.selected {
    background-color: var(--brand-red);
    color: var(--white);
    border-color: var(--brand-red);
    box-shadow: 0 4px 12px rgba(236, 0, 0, 0.2); 
}

/* Select all / Clear all — last in each chapter row. Dashed and quiet so it
   reads as an action on the row, not as another chapter. */
.select-all-btn {
    padding: 10px 14px;
    border: 1px dashed var(--medium-gray);
    background: transparent;
    color: var(--medium-gray);
    border-radius: 5px;
    cursor: pointer;
    font-family: var(--font-main);
    font-weight: 600;
    font-size: 0.8rem;
    transition: all 0.2s ease;
}
.select-all-btn:hover {
    border-color: var(--brand-red-light);
    color: var(--brand-red);
}
.select-all-btn[hidden] { display: none; }

/* Group view (A2 Applied mechanics: book chapter or exam question type,
   PLAN_a2_applied.md B4). A button with data-view shows only in that view. */
.topic-panel[data-view="chapter"] .topic-btn[data-view="exam"],
.topic-panel[data-view="exam"] .topic-btn[data-view="chapter"],
.topic-panel:not([data-view]) .topic-btn[data-view="exam"] { display: none; }

.group-view {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 14px;
    font-size: 0.8rem;
}
.group-view[hidden], .notation-view[hidden], .zsteps-view[hidden], .display-options[hidden] { display: none; }
/* Display options under the chapter buttons: each toggle shows only while the
   selection could draw a card it changes (index.html sync). */
.display-options {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
    gap: 6px 22px;
    margin: 14px 0 10px;
}
.notation-view, .zsteps-view {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 6px;
    font-size: 0.8rem;
}

/* Normal z-values: a card affected carries both versions (AE.bothZ);
   body.z-on shows the one with the standardising steps. */
.zs-off, body.z-on .zs-on { display: contents; }
.zs-on, body.z-on .zs-off { display: none; }

/* Vector notation: a vector card carries both versions (AE.bothNotations);
   body.notation-col shows the column one. display: contents, so the wrapper adds
   nothing to the layout. */
.vn-ij, body.notation-col .vn-col { display: contents; }
.vn-col, body.notation-col .vn-ij { display: none; }
.group-view-label {
    color: var(--medium-gray);
    font-weight: 600;
    margin-right: 4px;
}
.group-view-btn, .notation-btn, .zsteps-btn {
    padding: 5px 12px;
    border: 1px solid var(--medium-gray);
    background: var(--white);
    color: var(--dark-gray);
    border-radius: 999px;
    cursor: pointer;
    font-family: var(--font-main);
    font-weight: 600;
    font-size: 0.8rem;
    transition: all 0.2s ease;
}
.group-view-btn:hover, .notation-btn:hover, .zsteps-btn:hover { border-color: var(--brand-red-light); color: var(--brand-red); }
.group-view-btn.active, .notation-btn.active, .zsteps-btn.active {
    background: var(--dark-gray);
    border-color: var(--dark-gray);
    color: var(--white);
}

/* Until script.js has loaded, the chapter buttons work (index.html's early
   handler) but the actions below them would do nothing, so show them as busy. */
body.app-loading #generate-btn,
body.app-loading #selector-btn,
body.app-loading #my-sets-btn {
    opacity: 0.55;
    cursor: progress;
    pointer-events: none;
}

.divider-line {
    height: 1px;
    background-color: #e0e0e0;
    width: 100%;
    margin: 5px 0;
}

.control-bottom-row {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
    gap: 10px;
}

.slider-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    color: var(--dark-gray);
}

input[type=range] {
    accent-color: var(--brand-red);
    width: 200px;
    cursor: pointer;
}

#generate-btn {
    background-color: var(--brand-red);
    color: var(--white);
    border: none;
    padding: 12px 20px;
    border-radius: 5px;
    font-weight: 700;
    cursor: pointer;
    font-size: 0.95rem;
    font-family: var(--font-main);
    transition: background-color 0.3s ease;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}

#generate-btn:hover { 
    background-color: var(--brand-red-dark); 
}

#selector-btn {
    background-color: var(--brand-red);
    color: var(--white);
    border: none;
    padding: 12px 18px;
    border-radius: 5px;
    font-weight: 700;
    cursor: pointer;
    font-size: 0.95rem;
    font-family: var(--font-main);
    transition: background-color 0.3s ease;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
#selector-btn:hover {
    background-color: var(--brand-red-dark);
}

#view-btn {
    background-color: var(--brand-red);
    color: var(--white);
    border: none;
    padding: 12px 18px;
    border-radius: 5px;
    font-weight: 700;
    cursor: pointer;
    font-size: 0.95rem;
    font-family: var(--font-main);
    transition: background-color 0.3s ease;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}

#view-btn:hover { 
    background-color: var(--brand-red-dark); 
}

/* =========================================================
   4. GRID & CARDS
   ========================================================= */
.questions-grid {
    display: grid;
    gap: 20px;
    min-height: 300px;
}

.questions-grid[data-count="1"] { grid-template-columns: 1fr; }
.questions-grid[data-count="2"] { grid-template-columns: 1fr 1fr; }
.questions-grid[data-count="3"] { grid-template-columns: 1fr 1fr 1fr; }
.questions-grid[data-count="4"] { grid-template-columns: 1fr 1fr; }
.questions-grid[data-count="5"], 
.questions-grid[data-count="6"] { grid-template-columns: repeat(3, 1fr); }

@media (max-width: 768px) {
    .questions-grid[data-count] { grid-template-columns: 1fr; }
}

.question-tile {
    background-color: transparent;
    aspect-ratio: 16/9;
    perspective: 1000px;
    position: relative;
}

.card-inner {
    position: relative;
    width: 100%;
    height: 100%;
    transition: transform 0.6s cubic-bezier(0.4, 0.2, 0.2, 1);
    transform-style: preserve-3d;
    cursor: pointer;
}

.card-inner.is-flipped { transform: rotateY(180deg); }

.card-front, .card-back {
    position: absolute;
    width: 100%;
    height: 100%;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    
    border: 2px solid var(--medium-gray);
    border-radius: 16px;
    box-shadow: 0 8px 32px var(--card-shadow);
    overflow: hidden;
    
    display: flex;
    flex-direction: column;
    padding: 35px 20px 20px 20px; 
    box-sizing: border-box;
}

.card-front {
    background: var(--white);
    z-index: 2;
    transform: rotateY(0deg);
}

.card-front::before {
    content: "";
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 6px;
    background: var(--brand-red);
    z-index: 5;
}

.card-back {
    transform: rotateY(180deg);
    background: var(--gray-100);
    border-color: var(--medium-gray);
    z-index: 1;
    justify-content: center;
}

.card-back::before {
    content: "";
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 6px;
    background: var(--brand-red); 
    z-index: 5;
}

.card-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    width: 100%;
    height: 100%;
    gap: 10px;
    position: relative;
}

.question-type {
    font-size: 1.1rem; 
    color: var(--medium-gray);
    font-weight: 700;
    line-height: 1.2;
    font-family: var(--font-main);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.question-math {
    font-size: 1.8rem; 
    max-width: 100%;
    line-height: 1.3;
    color: var(--dark-gray);
}

/* =========================================================
   5. UTILITIES, ICONS & CANVAS
   ========================================================= */
.card-controls {
    position: absolute;
    top: 8px;
    right: 8px;
    z-index: 20;
    display: flex;
    gap: 4px;
}

.icon-btn {
    background: rgba(243, 244, 246, 0.5);  /* Light gray, 50% transparent */
    border: 1px solid rgba(156, 163, 175, 0.3);  /* Medium gray border, 30% transparent */
    border-radius: 50%;
    width: 24px;
    height: 24px;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    font-size: 0.9rem;
    color: rgba(107, 114, 128, 0.8);  /* Dark gray text, 80% opaque */
    transition: all 0.2s;
    backdrop-filter: blur(4px);  /* Slight blur for glass effect */
}

.card-back .icon-btn {
    background-color: rgba(255, 255, 255, 0.5);  /* White, 50% transparent */
    box-shadow: 0 2px 5px rgba(0,0,0,0.05);
}

.icon-btn:hover { 
    background: rgba(255, 255, 255, 0.9);  /* More opaque on hover */
    color: var(--medium-gray);
    border-color: var(--medium-gray);
}

/* Fullscreen button - Subtle grey instead of brand red */
.icon-btn.fullscreen-btn {
    background: rgba(156, 163, 175, 0.5);  /* Medium gray, 50% transparent */
    border-color: rgba(156, 163, 175, 0.3);
    color: rgba(75, 85, 99, 0.8);  /* Darker gray */
}

.icon-btn.fullscreen-btn:hover {
    background: rgba(156, 163, 175, 0.8);  /* More opaque on hover */
    border-color: rgba(107, 114, 128, 0.8);
    color: rgba(31, 41, 55, 0.9);
    transform: scale(1.05);  /* Smaller scale */
}

.icon-btn.fullscreen-btn::before {
    content: '⛶';
    font-size: 0.8rem;
    font-weight: normal;
}

/* Refresh button - also subtle */
.icon-btn.refresh-btn {
    background: rgba(156, 163, 175, 0.5);  /* Medium gray, 50% transparent */
    border-color: rgba(156, 163, 175, 0.3);
    color: rgba(75, 85, 99, 0.8);
}

.icon-btn.refresh-btn:hover {
    background: rgba(156, 163, 175, 0.8);
    border-color: rgba(107, 114, 128, 0.8);
    color: rgba(31, 41, 55, 0.9);
}

.locus-canvas {
    width: 100%;
    height: 300px; 
    background: var(--white);
    border: 1px solid var(--medium-gray);
    margin-top: 10px;
    cursor: grab;
    touch-action: none; 
    display: block;
}

.locus-canvas:active {
    cursor: grabbing;
}

/* --- FULLSCREEN OVERRIDES (Native Individual Tile) --- */
/* A small perspective (1000px) is fine for grid-sized cards but is far too
   aggressive once the card fills the viewport — mid-flip the foreshortening
   pushes the corners off-screen and the card appears to shrink. Use a much
   larger perspective in fullscreen so the rotation stays gentle. */
.question-tile.fullscreen,
.question-tile:fullscreen {
    perspective: 6000px;
}

.question-tile.fullscreen {
    width: 100vw;
    height: 100vh;
    background: white; 
    padding: 0;
    margin: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    border: none;
    border-radius: 0;
}

.question-tile.fullscreen .card-inner {
    width: 100%;
    height: 100%;
    max-width: 100%; 
    background: var(--white);
    display: flex;
    flex-direction: column;
}

.question-tile.fullscreen .card-front,
.question-tile.fullscreen .card-back {
    width: 100%;
    height: 100%;
    border: none; 
    border-radius: 0;
    padding: 20px;
    box-shadow: none; 
    display: flex;
    flex-direction: column;
    overflow: hidden; 
}

.question-tile.fullscreen .volume-canvas,
.question-tile.fullscreen .locus-canvas {
    width: 100% !important;
    height: 100% !important;
}

/* =========================================================
   6. NEW GRAPH CARD LAYOUT (Isolated)
   ========================================================= */

.graph-card-layout {
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%;
    overflow: hidden;
    box-sizing: border-box;
}

.graph-text-section {
    flex: 0 0 auto; 
    font-size: clamp(1.0rem, 2.5vw, 1.4rem); 
    line-height: 1.35;
    color: var(--dark-gray);
    width: 100%;
    z-index: 10;
    text-align: center;
    padding: 15px 20px 5px 20px; 
}

.question-tile.fullscreen .graph-text-section {
    font-size: clamp(1.8rem, 4vw, 3rem);
    padding-top: 40px;
}

.graph-canvas-section {
    flex: 1; 
    width: 100%;
    min-height: 0; 
    position: relative; 
    display: block;
}

.volume-canvas {
    position: absolute; 
    top: 0;
    left: 0;
    width: 100% !important;
    height: 100% !important;
    display: block;
}

/* =========================================================
   7. LAYOUT FIXES (Containment Version)
   ========================================================= */

.question-math, .answer-text {
    width: 100%;
    display: block; 
    flex: 0 1 auto; 
    box-sizing: border-box; 
    overflow-wrap: break-word; 
    word-break: break-word;
    padding: 0 5px;
}

.question-math.graph-mode {
    flex: 1;       
    display: flex; 
    flex-direction: column;
    justify-content: flex-start;
    align-items: stretch;
    padding: 0; 
    max-width: 100%;
    overflow: hidden; 
}

.graph-card-layout {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%;
    box-sizing: border-box;
    max-width: 100%;
    overflow: hidden;
}

.graph-text-section {
    flex: 0 0 auto;
    box-sizing: border-box; 
    width: 100%;
    font-size: clamp(0.9rem, 2.2vw, 1.3rem); 
    line-height: 1.35;
    color: var(--dark-gray);
    text-align: center;
    padding: 10px 15px 5px 15px; 
}

.question-tile.fullscreen .graph-text-section {
    font-size: clamp(1.5rem, 3vw, 2.5rem);
    padding-top: 40px;
    padding-left: 60px; 
    padding-right: 60px;
}

.graph-canvas-section {
    flex: 1;
    width: 100%;
    position: relative;
    min-height: 60px; 
    display: block;
    max-width: 100%;
    overflow: hidden; 
}

.volume-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100% !important;
    height: 100% !important;
}

/* =========================================================
   7b. TEXT-ABOVE-DIAGRAM LAYOUT
   Opt-in via plotData.layout === 'text-above'.
   Question text sits at the top (capped height & font),
   diagram fills the remainder. The diagram slot uses
   min-height/min-width: 0 so the SVG shrinks to fit instead
   of pushing the question text out of the card.
   autoScaleAllCards SKIPS these cards (see script.js), so
   font sizes here are set entirely by CSS clamp().
   ========================================================= */

.diagram-card-layout {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;   /* override .card-content centering */
    align-items: stretch;
    width: 100%;
    height: 100%;
    gap: 6px;
    overflow: hidden;
}

.diagram-text {
    flex: 0 0 auto;
    width: 100%;
    text-align: center;
    line-height: 1.4;              /* generous — MathJax glyphs need headroom */
    color: var(--dark-gray);
    padding: 0 6px;
    box-sizing: border-box;
    /* Starting size — JS fitDiagramText() shrinks below this if the rendered
       text doesn't fit within ~45% of the card height. */
    font-size: clamp(0.85rem, 1.2vw + 0.4rem, 1.25rem);
}

/* Container-level fullscreen (View Button): cards are bigger but still
   grid-laid-out, so keep the font modest. */
#questions-container:fullscreen .diagram-text {
    font-size: clamp(1rem, 1.1vw + 0.5rem, 1.6rem);
}

/* Single-tile fullscreen: card fills viewport, text can grow more. */
.question-tile:fullscreen .diagram-text,
.question-tile.focused .diagram-text {
    font-size: clamp(1.3rem, 2vw + 0.5rem, 2.4rem);
    padding: 0 24px;
}

.diagram-canvas {
    flex: 1 1 0;
    width: 100%;
    min-height: 0;                 /* lets SVG shrink correctly */
    min-width: 0;
    overflow: hidden;
    position: relative;
    display: block;
}

/* Text beside the diagram (plotData.layout 'text-left'): for a tall
   diagram such as a ladder, which would be tiny under a paragraph. The
   question takes the left half, vertically centred; the diagram the rest. */
.diagram-card-layout.text-left {
    flex-direction: row;
    align-items: center;
    gap: 12px;
}
.diagram-card-layout.text-left .diagram-text {
    flex: 0 0 50%;
    width: 50%;
}
.diagram-card-layout.text-left .diagram-canvas {
    flex: 1 1 0;
    height: 100%;
}

/* =========================================================
   7d. TEXT-BESIDE-TABLE LAYOUT
   Statistics cards with a frequency table (topic_as_stats2_measures.js,
   split()). A 16:9 card has room across, not down: question text on the
   left, a vertical table on the right. Lives inside .scale-target, so
   autoScaleAllCards sizes it like any text card.
   ========================================================= */

.table-split {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.8em;
}

.table-split .ts-text {
    flex: 1 1 0;
    min-width: 0;
    text-align: left;
}

/* MathJax pads every array cell; on a small card that padding is most of the table's height and width */
.table-split .ts-table mjx-mtd {
    padding: 0.12em 0.35em !important;
}

.table-split .ts-table {
    flex: 0 0 auto;
    font-size: 0.8em; /* the table is the wide part; the text can wrap, it cannot */
}

/* =========================================================
   8. GENERATE -> FULLSCREEN BEHAVIOR
   ========================================================= */

#questions-container:fullscreen {
    background-color: var(--app-bg);
    width: 100vw;
    height: 100vh;
    padding: 20px;
    box-sizing: border-box;
    display: grid;
    align-content: center; /* Vertically center the grid */
    overflow-y: auto;      /* Allow scrolling if needed */
}

/* =========================================================
   9. ICON PROTECTION (Targeted Padding)
   ========================================================= */

/* 
   We only squeeze the title text (.question-type) so it doesn't 
   hit the buttons, leaving the main question content full width.
*/

.card-front .question-type {
    padding: 0 70px; /* Protects 2 buttons */
}

.card-back .question-type {
    padding: 0 35px; /* Protects 1 button */
}

/* =========================================================
   10. SMOOTH LOADING (Prevent Jerky Resizing)
   ========================================================= */

/* Default state: Visible and transitions smooth */
.card-content {
    opacity: 1;
    transition: opacity 0.2s ease-in;
}

/* 
   State while calculating/resizing: 
   Hidden visually, but still takes up space so JS can measure it.
*/
.card-content.calculating {
    opacity: 0;
}

/* =========================================================
   NEW: SUBJECT SELECTOR STYLES
   ========================================================= */
.subject-selector {
    display: flex;
    justify-content: center;
    gap: 5px;
    margin-bottom: -1px; /* Overlaps the control panel border */
}

.subject-btn {
    padding: 12px 25px;
    font-family: var(--font-main);
    font-size: 1rem;
    font-weight: 600;
    border: 1px solid #e0e0e0;
    border-bottom: none;
    border-radius: 8px 8px 0 0;
    background-color: var(--light-gray);
    color: var(--medium-gray);
    cursor: pointer;
    transition: all 0.2s ease-in-out;
    position: relative;
    top: 1px; /* Align with the top of the panel border */
}

.subject-btn:hover {
    background-color: #e5e7eb;
    color: var(--brand-red);
}

.subject-btn.active {
    background-color: var(--white);
    color: var(--dark-gray);
    border-color: #e0e0e0;
    border-bottom: 1px solid var(--white); /* Hides the border line */
}

/* Phones: six tabs can't sit side by side at full size, and the row used to
   run off the screen. Each tab becomes an equal-width column with the level
   ("AS" / "A2") as a small caption over the course name, and "Maths" dropped
   from "Further Maths". Every tab stays visible and tappable; no scrolling. */
@media (max-width: 640px) {
    .subject-selector {
        display: grid;
        grid-template-columns: repeat(6, minmax(0, 1fr));
        gap: 3px;
    }
    .subject-btn {
        padding: 7px 2px 8px;
        font-size: clamp(0.68rem, 3.4vw, 0.85rem);
        line-height: 1.2;
        min-width: 0;
    }
    .subject-btn .lvl {
        display: block;
        font-size: 0.82em;
        font-weight: 700;
        letter-spacing: 0.06em;
        opacity: 0.7;
    }
    .subject-btn .crs { display: block; }
    .subject-btn .opt { display: none; }
    .group-view-label { flex-basis: 100%; text-align: center; margin: 0 0 2px; }
    .control-panel { padding-left: 16px; padding-right: 16px; }
}

.topic-panels-wrapper {
    width: 100%;
}

.topic-panel {
    width: 100%;
}

.coming-soon-message {
    text-align: center;
    padding: 40px 20px;
    font-size: 1.2rem;
    font-weight: 600;
    color: var(--medium-gray);
}


/* --- MODIFICATION TO CONTROL PANEL --- */
.control-panel {
    padding-top: 20px; /* Reduced top padding slightly */
}

/* --- SVG Triangle Styling --- */
.triangle-container {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 0;
}

.math-svg {
    width: 100%;
    height: 100%;
    max-height: 100%;
    display: block;
}

.math-text {
    font-family: 'Times New Roman', Times, serif;
    font-size: 36px; /* INCREASED from 24px */
    fill: #1f2937;
    text-anchor: middle;
    dominant-baseline: middle;
}

/* Ensure fractions or superscripts in the future are readable */
.math-text small {
    font-size: 24px;
}

.math-var {
    font-style: italic;
}

.math-num {
    font-style: normal;
    font-weight: 600;
}

.line-stroke {
    stroke: #1f2937;
    stroke-width: 4; /* INCREASED from 2.5 for better visibility */
    stroke-linecap: round;
    stroke-linejoin: round;
    fill: none;
}

.angle-fill {
    fill: rgba(236, 0, 0, 0.1);
    stroke: #ec0000;
    stroke-width: 3; /* INCREASED */
}


/* =========================================================
   FUNCTION-PLOT STYLING OVERRIDES
   ========================================================= */

/* Force scatter points to be fully opaque and vibrant */
.function-plot circle {
    fill-opacity: 1 !important;
    stroke-opacity: 1 !important;
}

/*
 * Function-Plot Customization: Bolder Axes
 * ----------------------------------------
 * Targets the SVG paths that function-plot uses to draw the axis lines.
 */
.function-plot .axis .domain {
    stroke-width: 2px;  /* Makes the axis lines bolder */
    stroke: #333;       /* Optional: Ensures a dark color */
}

/* =========================================================
   SMART HIDING FOR GRAPH CARDS
   ========================================================= */

/* 1. HIDE INSTRUCTION TEXT FOR TRIANGLES ONLY */
/* Volumes and Argand diagrams should SHOW their text */
.question-tile:has(.triangle-view-small) .graph-text-section,
.question-tile:has(.triangle-view-large) .graph-text-section {
    display: none; 
}

/* 2. HIDE THE CARD TITLE FOR GRAPHS WITH TRIANGLE */
.question-tile:has(.triangle-view-small) .question-type,
.question-tile:has(.triangle-view-large) .question-type {
    display: none;
}

/* 3. ENSURE VOLUME AND ARGAND TEXT IS VISIBLE */
.question-tile:has(.volume-canvas) .graph-text-section,
.question-tile:has(.locus-canvas) .graph-text-section {
    display: block !important;
}

/* =========================================================
   RESTORE TEXT IN FULLSCREEN / FOCUS MODES
   ========================================================= */

/* When the specific TILE is fullscreen */
.question-tile:fullscreen .graph-text-section,
.question-tile:fullscreen .question-type {
    display: block !important;
}

/* When in "Focus Mode" (clicked the expand button) */
.question-tile.focused .graph-text-section,
.question-tile.focused .question-type {
    display: block !important;
}

/* When the whole CONTAINER is fullscreen (View Button) */
#questions-container:fullscreen .graph-text-section,
#questions-container:fullscreen .question-tile:has(.graph-card-layout) .question-type {
    display: block !important;
}

/* Optional: Bump up font size in fullscreen for better readability */
.question-tile:fullscreen .graph-text-section,
.question-tile.focused .graph-text-section {
    font-size: 2rem;
    padding-bottom: 20px;
}

/* =========================================================
   DUAL TRIANGLE RENDERING
   ========================================================= */

/* Default State (Grid View): Show Small, Hide Large */
.triangle-view-small {
    display: flex;
    width: 100%;
    height: 100%;
}

/* *** MAKE SMALL VIEW BOLDER AND BIGGER *** */
.triangle-view-small .math-text {
    font-size: 80px !important; /* Much bigger than default 36px */
    font-weight: 900 !important; /* Extra bold */
}

.triangle-view-small .line-stroke {
    stroke-width: 10 !important; /* Much thicker than default 4 */
}

.triangle-view-small .angle-fill {
    stroke-width: 5 !important; /* Thicker angle arc */
}

.triangle-view-large {
    display: none;
    width: 100%;
    height: 100%;
}

/* ---------------------------------------------------------
   Fullscreen / Focused States: Swap Them
   --------------------------------------------------------- */

/* 1. When the individual tile is focused/fullscreen */
.question-tile:fullscreen .triangle-view-small,
.question-tile.focused .triangle-view-small {
    display: none !important;
}

.question-tile:fullscreen .triangle-view-large,
.question-tile.focused .triangle-view-large {
    display: flex !important;
}

/* 2. When the main container is fullscreen */
#questions-container:fullscreen .triangle-view-small {
    display: none !important;
}

#questions-container:fullscreen .triangle-view-large {
    display: flex !important;
}
/* =========================================================
   SHAREABLE LINKS - Share Button & Notifications
   ========================================================= */

#home-btn {
    background: var(--light-gray);
    color: var(--medium-gray);
    border: 2px solid var(--medium-gray);
    border-radius: 5px;
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.3rem;
    cursor: pointer;
    transition: all 0.3s ease;
    flex-shrink: 0;
}

#home-btn:hover {
    background: var(--white);
    color: var(--brand-red);
    border-color: var(--brand-red);
    transform: scale(1.05);
}

#share-btn {
    background: var(--brand-red);
    color: white;
    border: none;
    padding: 12px 18px;
    border-radius: 5px;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 2px 4px rgba(236, 0, 0, 0.2);
}

#share-btn:hover {
    background: var(--brand-red-dark);
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(236, 0, 0, 0.3);
}

#share-btn:active {
    transform: translateY(0);
}

/* Share panel for signed-in users (script.js, "Share panel"). Same card as
   the My Starters panel so the two read as one family. `top` is only the
   pre-JS value — M4TH5Viewport re-places it over the visible part of the
   iframe, exactly as it does #m4th5-sets. */
#m4th5-share {
    position: fixed;
    right: 20px;
    top: 20px;
    z-index: 9997;
    width: 340px;
    max-width: calc(100% - 40px);
    box-sizing: border-box;
    background: #fff;
    color: #222;
    border: 1px solid #d9d9d9;
    border-radius: 10px;
    box-shadow: 0 6px 24px rgba(0, 0, 0, .16);
    padding: 16px 18px;
    font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}
#m4th5-share h3 { margin: 0 0 4px; font-size: 15px; font-weight: 650; }
#m4th5-share .sub { margin: 0 0 10px; font-size: 12px; color: #777; }
#m4th5-share .sub.or { margin-top: 14px; }
#m4th5-share .row { display: flex; align-items: center; gap: 6px; }
#m4th5-share button {
    font: inherit;
    font-size: 13px;
    padding: 6px 12px;
    border-radius: 5px;
    cursor: pointer;
    border: 1px solid #ccc;
    background: #fff;
    color: #333;
    flex: 0 0 auto;
}
#m4th5-share button:hover { border-color: #999; }
#m4th5-share button:disabled { opacity: .55; cursor: default; }
#m4th5-share button.primary { background: var(--brand-red); border-color: var(--brand-red); color: #fff; font-weight: 600; }
#m4th5-share button.primary:hover { background: var(--brand-red-dark); }
#m4th5-share .close {
    position: absolute;
    top: 10px;
    right: 12px;
    border: 0;
    background: none;
    font-size: 18px;
    color: #999;
    padding: 2px 6px;
}
#m4th5-share .prefix { font-family: ui-monospace, Consolas, monospace; font-size: 12px; color: #888; }
#m4th5-share .row input {
    flex: 1;
    min-width: 0;
    font: inherit;
    font-size: 13px;
    padding: 6px 8px;
    border: 1px solid #ccc;
    border-radius: 5px;
    background: #fff;
    color: #222;
}
#m4th5-share .row input:focus { outline: none; border-color: var(--brand-red); }
#m4th5-share .hint { margin: 6px 0 0; font-size: 11px; color: #999; }
#m4th5-share .err { display: none; margin-top: 8px; font-size: 12px; color: #b91c1c; }
#m4th5-share .url {
    display: none;
    width: 100%;
    box-sizing: border-box;
    margin-top: 10px;
    font-family: ui-monospace, Consolas, monospace;
    font-size: 11px;
    padding: 6px 8px;
    border: 1px solid #ddd;
    border-radius: 5px;
    background: #f7f7f7;
    color: #444;
}
@media (prefers-color-scheme: dark) {
    #m4th5-share { background: #1e1e1e; color: #eee; border-color: #3a3a3a; }
    #m4th5-share button:not(.primary):not(.close),
    #m4th5-share .row input { background: #1e1e1e; color: #eee; border-color: #555; }
    #m4th5-share .url { background: #262626; color: #ccc; border-color: #444; }
    #m4th5-share .err { color: #f87171; }
}

/* Set by script.js when document.fullscreenEnabled is false — iPhone Safari,
   or an iframe without allow="fullscreen". Hides controls that cannot work
   rather than leaving a button that does nothing when pressed. */
body.no-fullscreen #view-btn,
body.no-fullscreen .fullscreen-btn {
    display: none !important;
}

/* Secondary actions beside Share. Same geometry so the row lines up, but
   quiet — Share is the primary action and these must not compete with it. */
#save-set-btn,
#my-sets-btn {
    background: var(--white);
    color: var(--dark-gray);
    border: 1px solid #d1d5db;
    padding: 11px 17px;          /* 1px less than #share-btn, to offset the border */
    border-radius: 5px;
    font-family: inherit;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
}

#save-set-btn:hover,
#my-sets-btn:hover {
    border-color: var(--medium-gray);
    background: var(--light-gray);
    transform: translateY(-1px);
}

#save-set-btn:active,
#my-sets-btn:active {
    transform: translateY(0);
}

/* Notification Styles */
.share-notification {
    position: fixed;
    top: 20px;
    right: -450px;
    background: #16a34a;
    color: white;
    padding: 16px 24px;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    z-index: 10000;
    font-size: 0.9rem;
    font-weight: 500;
    max-width: 400px;
    line-height: 1.5;
    transition: right 0.3s ease;
}

.share-notification.show {
    right: 20px;
}

.share-notification-error {
    background: #ef4444;
}

.share-notification-success {
    background: #16a34a;
}

/* =========================================================
   FULL SOLUTION PANEL (Type 2 Trig Identities — Prove That)
   ========================================================= */

.full-solution-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin-top: 12px;
    padding: 6px 14px;
    background: transparent;
    border: 1.5px solid var(--sky-blue);
    border-radius: 6px;
    color: var(--sky-blue-dark);
    font-family: var(--font-main);
    font-size: 0.78rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.18s, color 0.18s;
    letter-spacing: 0.02em;
}
.full-solution-btn:hover {
    background: var(--sky-blue);
    color: var(--white);
}
.full-solution-btn .fsb-icon {
    font-size: 0.9em;
    transition: transform 0.22s;
}
.full-solution-btn.open .fsb-icon {
    transform: rotate(180deg);
}
.full-solution-panel {
    overflow: hidden;
    max-height: 0;
    transition: max-height 0.32s ease, opacity 0.25s ease;
    opacity: 0;
    margin-top: 0;
}
.full-solution-panel.open {
    max-height: 600px;
    opacity: 1;
    margin-top: 10px;
}
.full-solution-panel-inner {
    background: #eff6ff;
    border-left: 3px solid var(--sky-blue);
    border-radius: 0 6px 6px 0;
    padding: 12px 16px;
    font-size: 0.92em;
    color: var(--dark-gray);
    line-height: 1.6;
}

/* ═══════════════════════════════════════════════════════════
   HIDE DIAGRAMS WHEN CARDS ARE SMALL (vectors chapter)
   The plot renders at full size behind the placeholder overlay
   so D3 always has real dimensions to work with.
   ═══════════════════════════════════════════════════════════ */

/* Wrapper clips any SVG overflow so diagram can't spill past the mask */
.hide-when-small {
    overflow: hidden;
}

/* On a small card the hidden diagram takes no space at all, so the working
   has the whole face; the corner Diagram button (§7d) says it is there.
   The plot is redrawn when the card is expanded. */
.questions-grid:not([data-count="1"]):not([data-count="2"]) .question-tile:not(.focused):not(:fullscreen) .hide-when-small {
    flex: 0 0 0 !important;
    height: 0 !important;
    min-height: 0 !important;
    visibility: hidden;
}

/* The old placeholder text, replaced by the corner button */
.hide-when-small .diagram-placeholder { display: none !important; }

/* §7d — The diagram hint: a small button in the corner of a face whose
   diagram is hidden because the card is small (hideWhenSmall, or a method's
   diagram). In the corner, so long working can never push it out of sight;
   clicking it expands the card (script.js). */
.diagram-hint {
    display: none;
    position: absolute;
    left: 8px;
    bottom: 8px;
    z-index: 20;
    align-items: center;
    gap: 4px;
    padding: 2px 9px;
    font: 600 0.7rem/1.3 system-ui, sans-serif;
    color: var(--white);
    background: var(--sky-blue-dark);
    border: none;
    border-radius: 999px;
    cursor: pointer;
    opacity: 0.9;
}
.diagram-hint:hover { opacity: 1; }
.card-front:has(.hide-when-small) > .diagram-hint,
.card-back:has(.hide-when-small) > .diagram-hint,
.card-back:has(.method-diagram) > .diagram-hint { display: inline-flex; }
.questions-grid[data-count="1"] .diagram-hint,
.questions-grid[data-count="2"] .diagram-hint,
.question-tile:fullscreen .diagram-hint,
.question-tile.focused .diagram-hint { display: none !important; }

/* ═══════════════════════════════════════════════════════════
   §12 — REPORT A PROBLEM ('!' button + modal, see reportProblem.js)
   ═══════════════════════════════════════════════════════════ */

.icon-btn.report-btn {
    position: absolute;
    bottom: 10px;
    right: 10px;
    z-index: 20;
    width: 16px;
    height: 16px;
    font-size: 0.6rem;
    font-weight: 700;
    color: rgba(153, 27, 27, 0.55);           /* muted brand red — subtle, not alarming */
    background: rgba(255, 255, 255, 0.5);
    border-color: rgba(153, 27, 27, 0.25);
}
.icon-btn.report-btn:hover {
    background: rgba(255, 255, 255, 0.95);
    color: var(--brand-red-dark);
    border-color: var(--brand-red-dark);
}

.report-overlay {
    position: fixed;
    inset: 0;
    background: rgba(17, 24, 39, 0.55);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 10000;
    padding: 16px;
}
.report-overlay.open { display: flex; }

.report-dialog {
    background: #fff;
    border-radius: 12px;
    border-top: 4px solid var(--brand-red);
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    width: 100%;
    max-width: 460px;
    padding: 22px 24px;
}
.report-dialog h3 {
    margin: 0 0 14px;
    font-size: 1.1rem;
    color: var(--brand-red-dark);
}

.report-form label {
    display: block;
    font-size: 0.85rem;
    font-weight: 600;
    margin-bottom: 12px;
}
.report-form select,
.report-form textarea,
.report-form input[type="text"] {
    display: block;
    width: 100%;
    box-sizing: border-box;
    margin-top: 4px;
    padding: 8px 10px;
    font-size: 0.9rem;
    font-weight: 400;
    font-family: inherit;
    border: 1px solid #d1d5db;
    border-radius: 6px;
}
.report-form select:focus,
.report-form textarea:focus,
.report-form input[type="text"]:focus {
    outline: none;
    border-color: var(--brand-red);
    box-shadow: 0 0 0 2px rgba(236, 0, 0, 0.15);
}
.report-form textarea { resize: vertical; }

/* Honeypot — visually hidden, bots fill it in, humans never see it */
.report-hp {
    position: absolute !important;
    left: -9999px !important;
    height: 1px; width: 1px;
    overflow: hidden;
}

.report-note {
    font-size: 0.75rem;
    color: #6b7280;
    margin: 4px 0 14px;
}

.report-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
}
.report-actions button {
    padding: 8px 16px;
    border-radius: 6px;
    font-size: 0.9rem;
    cursor: pointer;
    border: 1px solid #d1d5db;
    background: #fff;
}
.report-actions .report-submit {
    background: var(--brand-red);
    border-color: var(--brand-red);
    color: #fff;
}
.report-actions .report-submit:hover { background: var(--brand-red-dark); border-color: var(--brand-red-dark); }
.report-actions .report-submit:disabled { opacity: 0.6; cursor: wait; }

.report-status { font-size: 0.9rem; }
.report-status a { color: var(--brand-red-dark); }

/* =========================================================
   SECTION 7c — METHOD TOGGLE (answer.methods)
   ---------------------------------------------------------
   Tabbed alternative approaches on the back of a card. Emitted by
   buildAnswerHTML in script.js when a generator returns
   { text?, methods: [{ label, html }] } as its answer.

   Used by Proof by Induction (divisibility has three standard
   approaches), but the mechanism is topic-agnostic.

   NOTE: .method-card is a plain block, NOT a height-constrained flex
   column. autoScaleAllCards detects overflow via scrollHeight, so a
   fixed height here would let a long method clip silently instead of
   triggering the shrink. Only the inactive panels are hidden.
   ========================================================= */

.method-card {
    display: block;
    width: 100%;
}

.method-tabs {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 6px;
    margin-bottom: 10px;
}

/* Inactive tabs must still read as buttons: --light-gray is almost white,
   so the border uses --medium-gray against the grey answer background. */
.method-tab {
    font-family: inherit;
    font-size: 0.7rem;
    line-height: 1.2;
    padding: 4px 11px;
    border: 1px solid var(--medium-gray);
    border-radius: 999px;
    background: var(--white);
    color: var(--dark-gray);
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.method-tab:hover {
    background: var(--light-gray);
    border-color: var(--dark-gray);
}

.method-tab.active {
    font-weight: 600;
    background: var(--brand-red);
    border-color: var(--brand-red);
    color: var(--white);
}

/* MathJax renders in currentColor, so the label follows the tab's text colour */
.method-tab.active mjx-container,
.method-tab.active .mjx-container {
    color: var(--white);
}

/* MathJax inside a tab label must not inherit the shrunken answer size */
.method-tab .mjx-container,
.method-tab mjx-container {
    font-size: inherit !important;
}

/* Only the selected method is in flow — the sizer then measures just it */
.method-panel { display: none; }
.method-panel.active { display: block; }

/* A method's own diagram (method.plotData, e.g. the moments methods).
   Out of flow on small cards, so the working keeps the whole face; on one
   or two cards, a focused card or a fullscreened card it sits under the
   working at a fixed shape, and the sizer shrinks the text around it. */
.method-diagram { display: none; }
.questions-grid[data-count="1"] .method-diagram,
.questions-grid[data-count="2"] .method-diagram,
.question-tile:fullscreen .method-diagram,
.question-tile.focused .method-diagram {
    display: block;
    width: 70%;
    aspect-ratio: 2.6 / 1;
    margin: 0.4em auto 0;
    overflow: hidden;           /* the SVG must not add to the face's scrollHeight */
}

/* Stepping through a worked answer (answer.steps; script.js setStep).
   Later lines keep their space, so the sizer fits the whole answer once
   and nothing jumps as the steps are revealed. */
.step-line.step-past   { opacity: 0.35; }
.step-line.step-future { visibility: hidden; }
.step-line { transition: opacity 0.2s; }
.step-scope [data-key] { transition: opacity 0.2s; }
.step-scope [data-key].step-dim { opacity: 0.12; }
.step-scope [data-key].step-hide { opacity: 0; }
/* The shaded triangle a step works from (forceRenderer: resolved forces) is
   for stepping only; with every line shown, both triangles would cover the
   whole rectangle. A greyed triangle loses its shading altogether. */
.step-scope[data-step="all"] .tri-fill,
.step-scope [data-key].step-dim .tri-fill,
.step-scope [data-key].step-hide .tri-fill { display: none; }
.step-nav {
    display: flex;
    gap: 6px;
    justify-content: center;
    align-items: center;
    margin: 0.35em 0 0.2em;
    font-size: 0.75rem;
}
.step-btn {
    font: inherit;
    padding: 2px 9px;
    border: 1px solid var(--sky-blue-dark);
    border-radius: 999px;
    background: transparent;
    color: var(--sky-blue-dark);
    cursor: pointer;
}
.step-btn:disabled { opacity: 0.35; cursor: default; }
.step-btn:not(:disabled):hover { background: var(--sky-blue-dark); color: #fff; }
.step-count { min-width: 3.2em; text-align: center; color: #6b7280; }
.question-tile:fullscreen .step-nav,
.question-tile.focused .step-nav,
#questions-container:fullscreen .step-nav { font-size: 1.3rem; gap: 12px; }

/* A stepped method on a large card: the working on the left, its diagram
   on the right (a tall ladder would be tiny under nine lines of working) */
.questions-grid[data-count="1"] .method-panel.step-scope.active,
.questions-grid[data-count="2"] .method-panel.step-scope.active,
.question-tile:fullscreen .method-panel.step-scope.active,
.question-tile.focused .method-panel.step-scope.active {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1.3fr);
    gap: 0.8em;
    align-items: center;
}
/* fitContent shrinks the text until the widest working line fits; if one is
   still too wide at its smallest size, scroll rather than clip it */
.method-panel.step-scope .step-col { min-width: 0; overflow-x: auto; overflow-y: hidden; }
.questions-grid[data-count="1"] .method-panel.step-scope .method-diagram,
.questions-grid[data-count="2"] .method-panel.step-scope .method-diagram,
.question-tile:fullscreen .method-panel.step-scope .method-diagram,
.question-tile.focused .method-panel.step-scope .method-diagram {
    width: 100%;
    aspect-ratio: 1.15 / 1;
    margin: 0;
}

/* Optional shared stem shown above the tabs */
.method-stem {
    margin-bottom: 8px;
    opacity: 0.85;
}

/* Bigger tap targets and text when a card is blown up */
.question-tile:fullscreen .method-tab,
.question-tile.focused .method-tab,
#questions-container:fullscreen .method-tab {
    font-size: 1.4rem;
    padding: 8px 20px;
    gap: 12px;
}

.question-tile:fullscreen .method-tabs,
.question-tile.focused .method-tabs,
#questions-container:fullscreen .method-tabs {
    gap: 12px;
    margin-bottom: 18px;
}

/* Free-text hint on the report form.
   PLAN_login_gate.md flags the description box as the one route by which a
   child could volunteer personal data unprompted — nothing asks for it, but
   nothing discourages it either. Asking plainly is the cheapest mitigation
   there is. */
.report-hint {
    display: block;
    margin-top: 5px;
    font-size: 12px;
    color: #888;
}

/* MathJax draws each letter of \text{…} in its own box, as tall as that
   letter (n 0.442em, d 0.694em). Safari snaps each box to whole pixels on
   its own, so letters of different heights land a pixel apart: the n in
   "and" sat low. Giving every letter in a text run the same box height
   (the tallest ascender and deepest descender in the font) makes them all
   round the same way. Widths are untouched. */
mjx-mtext mjx-c::before {
    padding-top: 0.694em !important;
    padding-bottom: 0.194em !important;
}

/* The same rounding hits digits (mjx-mn): 1 is 0.666em tall with no depth,
   7 is 0.676em with 0.022em depth, so "170" sat uneven in Safari (1 high,
   7 low). Every digit gets the tallest digit's height and the deepest
   digit's depth, which moves layout by at most ~0.02em. */
mjx-mn mjx-c::before {
    padding-top: 0.677em !important;
    padding-bottom: 0.022em !important;
}
