@import url('https://fonts.googleapis.com/css2?family=Quicksand:wght@300..700&display=swap');

/* ==========================================================================
   1. THEME DESIGN VARIABLES 
   ========================================================================== */
:root {
    --red4: #d62728;
    --blue4: #1f77b4;
    --green4: #2ca02c;
    --purple4: #9467bd;
    --orange4: #ff7f0e;
    
    --bg-canvas: #fffacd;
    --bg-surface: #ffffff;
    --text-main: #212529;
    --border-color: #ccc;
    --nav-bg: #d62728;
    
    --def-bg: hsla(0, 100%, 80%, 0.1);
    --ex-bg: hsla(270, 100%, 80%, 0.1);
    --pro-bg: hsla(120, 70%, 80%, 0.1);
    --thm-bg: hsla(210, 100%, 80%, 0.1);
    --prac-bg: hsla(30, 100%, 80%, 0.1);

    --d3-bg: transparent;          
    --d3-axis: var(--text-main);   /* Matches textbook text for a drawn-on feel */
    --d3-grid: rgba(0, 0, 0, 0.1); /* Very faint ink lines */
}

[data-theme="dark"] {
    --red4: #E06C75;
    --blue4: #61AFEF;
    --green4: #98C379;
    --purple4: #C678DD;
    --orange4: #D19A66;
    
    --bg-canvas: #282C34;
    --text-main: #ABB2BF;
    --border-color: #3E4451;
    --nav-bg: #21252B;

    --def-bg: rgba(224, 108, 117, 0.1);
    --ex-bg: rgba(198, 120, 221, 0.1);
    --pro-bg: rgba(152, 195, 121, 0.1);
    --thm-bg: rgba(97, 175, 239, 0.1);
    --prac-bg: rgba(209, 154, 102, 0.1);

    --d3-bg: #15181e;
    --d3-axis: #ABB2BF;
    --d3-grid: rgba(255, 255, 255, 0.1);
}

/* ==========================================================================
   2. GLOBAL RESET & BASE RULES
   ========================================================================== */
body {
    font-family: 'Quicksand', -apple-system, BlinkMacSystemFont, sans-serif;
    line-height: 1.6;
    margin: 0;
    padding-top: 75px; 
    background-color: var(--bg-canvas);
    color: var(--text-main);
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    overscroll-behavior-y: contain;
    transition: background-color 0.3s ease, color 0.3s ease;
}

.content-wrapper { max-width: 900px; margin: 0 auto; padding: 20px; flex-grow: 1; }

h1, h2, h3, h4, h5, h6 { color: var(--blue4); font-weight: 600; }
h1 { border-bottom: 1px solid var(--border-color); padding-bottom: 0.2em; margin-bottom: 1em; }

/* ==========================================================================
   3. TEXTBOOK ENVIRONMENTS 
   ========================================================================== */
.latex-bold { font-weight: bold; color: var(--red4); }
.latex-italic { font-style: italic; color: var(--red4); }

.vocab-term { font-style: italic; color: var(--red4); text-decoration: underline dotted; cursor: help; position: relative; }
.vocab-term:hover { color: var(--purple4); }

.definition-box, .example-box, .theorem-box, .protip-box, .practice-box {
    margin: 1.5em auto; padding: 1em 1.2em; border-left: 5px solid; box-shadow: 0 2px 5px rgba(0,0,0,0.1); position: relative;
}

.definition-box { background-color: var(--def-bg); border-color: var(--red4); }
.example-box { background-color: var(--ex-bg); border-color: var(--purple4); }
.protip-box { background-color: var(--pro-bg); border-color: var(--green4); }
.theorem-box { background-color: var(--thm-bg); border-color: var(--blue4); }
.practice-box { background-color: var(--prac-bg); border-color: var(--orange4); }

.definition-box::before, .example-box::before, .protip-box::before, .theorem-box::before {
    position: absolute; top: -0.8em; left: 50%; transform: translateX(-50%); background-color: var(--bg-canvas);
    padding: 0 0.5em; font-weight: bold; white-space: nowrap; border: 1px solid; border-radius: 4px; transition: background-color 0.3s ease;
}
.definition-box::before { content: "Definition"; color: var(--red4); border-color: var(--red4); }
.example-box::before { content: "Example"; color: var(--purple4); border-color: var(--purple4); }
.protip-box::before { content: "Pro Tip"; color: var(--green4); border-color: var(--green4); }
.theorem-box::before { content: "Theorem"; color: var(--blue4); border-color: var(--blue4); }

.vocab-popover {
    position: absolute; background-color: var(--bg-canvas); border: 1px solid var(--blue4); box-shadow: 3px 3px 10px rgba(0,0,0,0.2);
    padding: 15px; border-radius: 8px; max-width: 300px; z-index: 2000; pointer-events: none; opacity: 0;
    transition: opacity 0.2s ease-in-out; font-size: 0.95em; color: var(--text-main);
}
.vocab-popover.visible { opacity: 1; pointer-events: auto; }
.vocab-popover h3 { margin-top: 0; margin-bottom: 0.5em; color: var(--blue4); font-size: 1.2em; border-bottom: 1px solid var(--border-color); }

/* ==========================================================================
   4. GRAPH CONTAINERS & MODULAR SLIDERS 
   ========================================================================== */
/* Make the container the actual colored box */
.d3-graph-container {
    max-width: 900px;
    margin: 20px auto;
    position: relative;
    
    /* Stripped borders and backgrounds for the seamless look */
    border: none;
    border-radius: 0;
    background-color: transparent !important;
    
    transition: background-color 0.3s ease;
    overflow: visible; 
}

/* Make the SVG transparent so it sits cleanly in the container */
.d3-graph-container svg {
    display: block;
    background-color: transparent !important;
}

/* Kills the thick outer box borders of the D3 axes, leaving only the grid and ticks! */
.graph-axis-group path.domain {
    display: none;
}

/* Generic Modular Slider Utility (For D3 injection) */
.textbook-slider {
    -webkit-appearance: none; appearance: none; width: 100%; background: transparent; outline: none; cursor: pointer;
}
.textbook-slider::-webkit-slider-runnable-track {
    width: 100%; height: 6px; background: var(--border-color); border-radius: 6px;
}
.textbook-slider::-webkit-slider-thumb {
    -webkit-appearance: none; appearance: none; width: 18px; height: 18px; border-radius: 50%; 
    background: var(--blue4); margin-top: -6px; transition: transform 0.1s ease;
}
.textbook-slider::-webkit-slider-thumb:hover { transform: scale(1.15); }

/* ==========================================================================
   5. NAVIGATION HEADER & SETTINGS PANEL
   ========================================================================== */
.header-top-bar {
    background-color: var(--nav-bg); padding: 10px 20px; display: flex; align-items: center; color: white;
    box-shadow: 0 2px 5px rgba(0,0,0,0.2); position: fixed; top: 0; left: 0; width: 100%; z-index: 1000; box-sizing: border-box; transition: background-color 0.3s ease;
}

.site-title { font-family: 'Georgia', serif; font-size: 1.6em; font-weight: bold; color: white; margin: 0; }
.header-top-bar nav { display: flex; justify-content: space-between; align-items: center; flex-grow: 1; margin-left: 50px; }
.nav-links { display: flex; gap: 20px; }
.header-top-bar nav a { color: white; text-decoration: none; font-weight: bold; white-space: nowrap; }

.dropdown { position: relative; display: inline-block; }
.dropdown-content { display: none; position: absolute; background-color: var(--nav-bg); min-width: 160px; box-shadow: 0px 8px 16px rgba(0,0,0,0.2); z-index: 1; top: 100%; left: 0; }
.dropdown-content a { padding: 12px 16px; display: block; text-align: left; }
.dropdown-content a:hover { background-color: rgba(255,255,255,0.1); }
.dropdown:hover .dropdown-content { display: block; }
.header-top-bar nav .dropdown:last-child { margin-left: auto; }

.settings-panel {
    position: fixed; top: 0; right: 0; width: 320px; height: 100vh; background-color: var(--bg-canvas); border-left: 2px solid var(--border-color);
    box-shadow: -2px 0 10px rgba(0,0,0,0.1); z-index: 1500; padding: 20px; box-sizing: border-box; transform: translateX(100%);
    transition: transform 0.3s ease-in-out, background-color 0.3s ease;
}
.settings-panel.active { transform: translateX(0); }
.settings-panel .close-btn { position: absolute; top: 10px; right: 15px; background: none; border: none; font-size: 1.5rem; cursor: pointer; color: var(--text-main); }
.settings-panel h2 { margin-top: 0; padding-bottom: 10px; border-bottom: 1px solid var(--border-color); color: var(--blue4); }

.settings-option { margin-bottom: 15px; display: flex; align-items: center; justify-content: space-between; font-weight: bold; color: var(--text-main); }

.switch { position: relative; display: inline-block; width: 44px; height: 22px; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider-toggle { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: var(--border-color); transition: .3s; border-radius: 22px; }
.slider-toggle:before { position: absolute; content: ""; height: 14px; width: 14px; left: 4px; bottom: 4px; background-color: white; transition: .3s; border-radius: 50%; }
input:checked + .slider-toggle { background-color: var(--blue4); }
input:checked + .slider-toggle:before { transform: translateX(22px); }

/* ==========================================================================
   5.1 REUSABLE MEGA MENU COMPONENT (FLEXBOX CENTERING)
   ========================================================================== */

.nav-mega-dropdown {
    position: static !important;
}

.nav-mega-dropdown .mega-menu-content {
    display: none; /* Set to 'block' if using your working JavaScript toggle setup */
    position: absolute;
    background-color: var(--nav-bg);
    width: 100%;
    left: 0;
    top: 100%; 
    box-shadow: 0px 8px 16px rgba(0,0,0,0.3);
    z-index: 1000;
    box-sizing: border-box;
    padding: 30px 40px;
    border-top: 1px solid var(--border-color);
}

.nav-mega-dropdown:hover .mega-menu-content {
    display: block;
}

/* The Invisible Bridge */
.nav-mega-dropdown .mega-menu-content::before {
    content: '';
    position: absolute;
    top: -20px; 
    left: 0;
    width: 100%;
    height: 20px;
    background: transparent;
}    

/* --- THE CENTERING FIX --- */
/* Swapping Grid for Flexbox allows columns to size naturally and float to the center */
.mega-menu-grid {
    max-width: 1200px;
    margin: 0 auto;
    display: flex;
    justify-content: center; /* Centers the columns horizontally */
    align-items: flex-start; /* Keeps columns aligned cleanly at the top */
    gap: 40px; /* Increased gap slightly to distribute breathing room evenly */
    flex-wrap: wrap; /* Allows safe wrap-around on smaller monitors */
    /* Safely scroll the links here without killing the outer hover zone! */
    max-height: 65vh; 
    overflow-y: auto; 
    padding-right: 10px; /* Gives space so the scrollbar doesn't hug the text */
}

/* Give each column a flexible but controlled structural width */
.mega-column {
    flex: 1 1 220px;     /* Changed from 0 1 200px: Allows columns to grow slightly and gives a solid base */
    min-width: 200px;    /* Bumped up slightly to guarantee breathing room for text lengths */
    max-width: 300px;    /* Prevents columns from getting awkwardly wide on massive screens */
    box-sizing: border-box;
}

/* Column headers styling */
.mega-column h3 {
    font-size: 0.85em;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--blue4);
    margin-top: 0;
    margin-bottom: 15px;
    padding-bottom: 5px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.15);
}

/* Clean target text links rules */
.mega-links {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.mega-links a {
    color: rgba(255, 255, 255, 0.85) !important;
    font-weight: 500 !important;
    font-size: 0.95em;
    text-decoration: none;
    transition: color 0.2s ease, padding-left 0.2s ease;
    display: inline-block;
    /* --- THE OVERLAP FIX --- */
    white-space: normal;       /* Allows long text to wrap to the next line naturally */
    word-wrap: break-word;     /* Ensures massive words or strings don't break the layout */
    width: 100%;               /* Forces the anchor tag to respect the column's boundary */
}

.mega-links a:hover {
    color: var(--orange4) !important;
    padding-left: 4px;
    background-color: transparent !important;
}

/* ==========================================================================
   6. INTERACTIVE WIDGETS & COMPONENTS
   ========================================================================== */
.interactive-container {
    background: linear-gradient(135deg, rgba(0,0,0,0.02), rgba(255,255,255,0.5));
    border: 2px solid var(--text-main);
    border-radius: 16px;
    padding: 30px 20px;
    margin: 30px 0;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
    position: relative;
    text-align: center;
}

/* Forces all titles inside the widget to be neutral instead of blue */
.interactive-container h1,
.interactive-container h2,
.interactive-container h3,
.interactive-container h4,
.interactive-container h5,
.interactive-container h6 {
    color: var(--text-main);
}

[data-theme="dark"] .interactive-container {
    background: linear-gradient(135deg, rgba(255,255,255,0.02), #15181e);
    border-color: var(--text-main);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
}

.input-val {
    width: 60px;
    padding: 8px;
    text-align: center;
    border: 2px solid var(--blue4);
    border-radius: 6px;
    font-size: 1em;
    background-color: var(--bg-surface);
    color: var(--text-main);
    font-family: monospace;
    font-weight: bold;
}

.btn-action {
    background-color: var(--blue4);
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 6px;
    cursor: pointer;
    font-weight: bold;
    font-family: 'Quicksand', sans-serif;
    transition: all 0.2s ease;
    box-shadow: 0 4px 10px rgba(31, 119, 180, 0.3);
}

.btn-action:hover { 
    opacity: 0.9; 
    transform: translateY(-2px); 
    box-shadow: 0 6px 15px rgba(31, 119, 180, 0.4);
}

.btn-action:active { 
    transform: translateY(1px); 
    box-shadow: 0 2px 5px rgba(31, 119, 180, 0.3);
}

/* Add inside your 6. INTERACTIVE WIDGETS & COMPONENTS section */
.widget-refresh-btn {
    position: absolute;
    top: 15px;
    right: 15px;
    background: transparent;
    border: none;
    color: var(--blue4);
    font-size: 1.5rem;
    cursor: pointer;
    transition: transform 0.4s ease, color 0.2s ease;
    padding: 5px;
    line-height: 1;
    z-index: 10;
}

.widget-refresh-btn:hover {
    transform: rotate(-180deg);
    color: var(--red4);
}

/* ==========================================================================
   TYPOGRAPHY & ENVIRONMENT HIGHLIGHTS
   ========================================================================== */
/* Standard syntactical emphasis in text (Blue) */
.textbook-content p strong, 
.textbook-content li strong {
    color: var(--blue4);
}

/* Environment-specific overrides for BOTH normal bold text AND dictionary links */
.textbook-content .definition-box strong, 
.textbook-content .definition-box .vocab-link { 
    color: var(--red4); 
}

.textbook-content .example-box strong, 
.textbook-content .example-box .vocab-link { 
    color: var(--purple4); 
}

.textbook-content .protip-box strong, 
.textbook-content .protip-box .vocab-link { 
    color: var(--green4); 
}

.textbook-content .theorem-box strong, 
.textbook-content .theorem-box .vocab-link { 
    color: var(--blue4); 
}

.textbook-content .practice-box strong, 
.textbook-content .practice-box .vocab-link { 
    color: var(--orange4); 
}

/* ==========================================================================
   VOCABULARY HOVER SYSTEM
   ========================================================================== */
/* Dictionary words: Bold, Red, Dotted */
.vocab-link {
    color: var(--red4);
    text-decoration: underline;
    text-decoration-style: dotted;
    text-decoration-thickness: 2px;
    text-underline-offset: 3px;
    font-weight: bold;
    cursor: pointer;
    transition: color 0.2s;
}

.vocab-link:hover {
    color: var(--orange4);
}

/* Upgraded Tooltip matching the Red Definition Boxes */
.vocab-tooltip {
    position: absolute;
    width: 320px;
    background: var(--bg-surface);
    border: 2px solid var(--text-main);
    border-left: 6px solid var(--red4); /* Red accent matching def box */
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
    border-radius: 12px;
    padding: 18px 20px;
    z-index: 9999;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s ease, visibility 0.2s ease;
    pointer-events: none;
}

.vocab-tooltip.visible {
    opacity: 1;
    visibility: visible;
}

.tooltip-header {
    font-family: 'Times New Roman', serif;
    font-size: 1.4em;
    font-weight: bold;
    color: var(--red4); /* Red header */
    border-bottom: 1px dashed var(--border-color);
    padding-bottom: 10px;
    margin-bottom: 12px;
}

.tooltip-body {
    font-size: 0.95em;
    color: var(--text-main);
    line-height: 1.5;
}

.tooltip-footer {
    font-size: 0.75em;
    color: var(--text-main);
    opacity: 0.5;
    margin-top: 15px;
    text-transform: uppercase;
    letter-spacing: 1px;
    font-weight: bold;
}

/* ==========================================================================
   7. PRACTICE QUESTION UI AESTHETICS
   ========================================================================== */
/* Tie the Practice Box into the exact same header style as the Definition boxes */
.definition-box::before, .example-box::before, .protip-box::before, .theorem-box::before, .practice-box::before {
    position: absolute; top: -0.8em; left: 50%; transform: translateX(-50%); background-color: var(--bg-canvas);
    padding: 0 0.5em; font-weight: bold; white-space: nowrap; border: 1px solid; border-radius: 4px; transition: background-color 0.3s ease;
}

/* Dynamically pull the title (e.g., "Question 1") from the HTML data attribute */
.practice-box::before {
    content: attr(data-question-title); 
    color: var(--orange4); 
    border-color: var(--orange4); 
}

/* Fix the Vocab Tooltip Background (Solid light red to match def boxes) */
.vocab-tooltip {
    background: color-mix(in srgb, var(--red4) 8%, var(--bg-canvas)) !important;
}

/* Tightened Multiple Choice Buttons */
/* Tightened Multiple Choice Buttons */
.mc-choice-btn {
    position: relative; 
    padding: 10px 15px 10px 45px; /* Added left padding so text never overlaps the letter */
    font-size: 1.1em; 
    cursor: pointer; 
    border: 2px solid var(--orange4); 
    border-radius: 12px; 
    background: transparent; 
    color: var(--text-main); 
    transition: all 0.2s ease; 
    display: flex; 
    justify-content: center; /* Forces the math span to be dead center */
    align-items: center; 
    box-shadow: 0 4px 6px rgba(0,0,0,0.02);
}
.mc-choice-btn:hover {
    background: color-mix(in srgb, var(--orange4) 10%, transparent);
    transform: translateY(-2px);
}
.mc-choice-btn strong { 
    position: absolute; /* Locks the letter to the left wall */
    left: 15px;
    color: var(--orange4); 
    font-size: 1.2em; 
}
.mc-choice-btn span { 
    font-family: monospace; 
    font-weight: bold; 
    font-size: 1.1em; 
    text-align: center;
}

/* Tightened Generate Button */
.generate-new-btn {
    width: 100%; 
    max-width: 350px; 
    margin: 0px auto 0px auto; /* Shrunk top margin from 25px to 10px */
    background: var(--bg-canvas); 
    color: var(--text-main); 
    border: 2px solid var(--border-color); 
    padding: 12px 20px; 
    border-radius: 8px; 
    cursor: pointer; 
    font-weight: bold; 
    font-size: 0.9em; 
    transition: all 0.2s; 
    text-transform: uppercase; 
    letter-spacing: 1px;
}
.generate-new-btn:hover {
    border-color: var(--orange4);
    color: var(--orange4);
    box-shadow: 0 4px 12px rgba(255, 127, 14, 0.15);
}