/*
============================================================
FunctionWorkshop - Main Stylesheet

Purpose:
- Define the overall application layout
- Style mathematical formula cards and graphs
- Provide responsive layouts
- Support accessibility and printing
============================================================
*/

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

/* Global design tokens (colors, spacing, shadows). */
:root {
    color-scheme: light;
    --page-bg: #f8fafc;
    --surface: #ffffff;
    --text-primary: #0f172a;
    --text-secondary: #475569;
    --text-muted: #64748b;
    --border: #e2e8f0;
    --blue: #2563eb;
    --blue-dark: #1d4ed8;
    --purple: #8b5cf6;
    --gray-button: #64748b;
    --gray-button-dark: #475569;
    --root-accent: #ef4444;
    --extrema-accent: #f97316;
    --inflection-accent: #10b981;
    --structure-accent: #2563eb;
    --limits-accent: #8b5cf6;
    --integration-accent: #0f766e;
    --card-radius: 12px;
    --small-radius: 6px;
    --shadow-small: 0 2px 4px rgb(0 0 0 / 0.03);
    --shadow-medium: 0 4px 8px rgb(0 0 0 / 0.06);
}

/* Base HTML document settings. */
html {
    min-width: 320px;
    background: var(--page-bg);
    scroll-behavior: smooth;
}

/* Main page layout. */
body {
    width: 100%;
    min-width: 320px;
    min-height: 100vh;
    min-height: 100dvh;
    padding: clamp(0.75rem, 2vh, 1.5rem) clamp(0.75rem, 2vw, 2rem);
    overflow-x: hidden;
    overflow-y: auto;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: var(--page-bg);
    color: #1e293b;
    font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
    scrollbar-gutter: stable;
}

body.scrollable {
    align-items: flex-start;
    padding-top: clamp(1rem, 4vh, 2.5rem);
    padding-bottom: clamp(1rem, 4vh, 2.5rem);
}

/* Visible warning when JavaScript is disabled. */
noscript {
    position: fixed;
    z-index: 1000;
    inset: 0 auto auto 0;
    width: 100%;
    padding: 0.75rem 1rem;
    background: #fff7ed;
    color: #9a3412;
    border-bottom: 1px solid #fdba74;
    text-align: center;
    font-weight: 600;
}

/* Root application container. */
.container {
    width: min(100%, 1400px);
    min-height: calc(100dvh - clamp(1.5rem, 4vh, 3rem));
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    gap: clamp(0.75rem, 1.5vh, 1.25rem);
    margin-inline: auto;
}

body.scrollable .container {
    min-height: auto;
    gap: clamp(1rem, 3vh, 2rem);
}

/* Page header. */
header {
    width: 100%;
    min-height: 3.5rem;
    display: flex;
    flex-direction: column;
    justify-content: center;
    text-align: center;
}

header h1 {
    color: var(--text-primary);
    font-size: clamp(1.25rem, 2vw, 1.7rem);
    line-height: 1.2;
}

header p {
    margin-top: 0.2rem;
    color: var(--text-muted);
    font-size: clamp(0.78rem, 1.2vw, 0.9rem);
}

/* Main application content. */
main {
    width: 100%;
    min-width: 0;
}

/* Control buttons area. */
.control-panel {
    width: 100%;
    min-height: 3.25rem;
    padding: 0.5rem;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.75rem;
    flex-shrink: 0;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--card-radius);
    box-shadow: var(--shadow-small);
}

body.scrollable .control-panel {
    min-height: 3.75rem;
}

.control-panel button {
    min-height: 2.35rem;
    padding: 0.5rem 1.5rem;
    color: #ffffff;
    border: 0;
    border-radius: var(--small-radius);
    cursor: pointer;
    font: inherit;
    font-size: 0.9rem;
    font-weight: 600;
    line-height: 1.2;
    transition:
        background-color 0.2s ease,
        transform 0.1s ease,
        opacity 0.2s ease,
        box-shadow 0.2s ease;
}

.control-panel button:hover:not(:disabled) {
    box-shadow: 0 3px 8px rgb(15 23 42 / 0.14);
}

.control-panel button:active:not(:disabled) {
    transform: scale(0.98);
}

.control-panel button:focus-visible {
    outline: 3px solid rgb(37 99 235 / 0.35);
    outline-offset: 2px;
}

.control-panel button:disabled {
    cursor: wait;
    opacity: 0.65;
    transform: none;
    box-shadow: none;
}

#generate-btn {
    background-color: var(--blue);
}

#generate-btn:hover:not(:disabled) {
    background-color: var(--blue-dark);
}

.secondary-btn {
    background-color: var(--gray-button);
}

.secondary-btn:hover:not(:disabled) {
    background-color: var(--gray-button-dark);
}

.secondary-btn.active {
    background-color: var(--purple);
}

/* Main workspace containing formulas, graphs and analysis. */
#workspace {
    width: 100%;
    min-width: 0;
    min-height: 0;
    height: min(82vh, 900px);
    display: flex;
    flex-direction: column;
    gap: clamp(0.75rem, 1.5vh, 1.25rem);
}

body.scrollable #workspace {
    height: auto;
    gap: clamp(1rem, 3vh, 2rem);
}

/* Wrapper around formulas and charts. */
.visual-area {
    width: 100%;
    min-width: 0;
    min-height: 0;
    height: 100%;
    display: flex;
    flex-direction: column;
}

body.scrollable .visual-area {
    height: auto;
}

/* Grid displaying f(x), f'(x) and f''(x). */
.formula-display {
    min-width: 0;
    min-height: 6.25rem;
    height: min(14vh, 145px);
    margin-bottom: clamp(0.75rem, 2vh, 1.5rem);
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: clamp(0.6rem, 1.5vw, 1.25rem);
    flex-shrink: 0;
}

body.scrollable .formula-display {
    height: auto;
    min-height: 7.5rem;
}

/* Individual mathematical expression card. */
.formula-card {
    min-width: 0;
    height: 100%;
    padding: 0.55rem 0.75rem;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    background: var(--surface);
    border: 1px solid var(--border);
    border-top: 4px solid #cbd5e1;
    border-radius: var(--card-radius);
    box-shadow: var(--shadow-small);
}

.formula-display .formula-card:nth-child(1) {
    border-top-color: #1f77b4;
}

.formula-display .formula-card:nth-child(2) {
    border-top-color: #ff7f0e;
}

.formula-display .formula-card:nth-child(3) {
    border-top-color: #2ca02c;
}

.formula-card h3 {
    margin-bottom: 0.3rem;
    color: var(--text-secondary);
    font-size: 0.75rem;
    line-height: 1.2;
    text-transform: uppercase;
}

/* MathJax rendering container. */
.formula-render {
    width: 100%;
    min-width: 0;
    min-height: 3rem;
    height: calc(100% - 1.5rem);
    padding: 0.25rem 0;
    overflow-x: auto;
    overflow-y: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    color: var(--text-primary);
    font-size: clamp(0.8rem, 1vw, 1rem);
    font-weight: 700;
    text-align: center;
    scrollbar-width: thin;
    -webkit-overflow-scrolling: touch;
}

.formula-render mjx-container {
    max-width: none;
    margin: 0 !important;
}

/* Graph layout container. */
#charts-wrapper {
    min-width: 0;
    display: grid;
    gap: clamp(0.6rem, 1.5vw, 1.25rem);
    transition:
        grid-template-columns 0.25s ease,
        gap 0.25s ease;
}

.charts-grid {
    height: min(59vh, 650px);
    grid-template-columns: repeat(3, minmax(0, 1fr));
}

.charts-list {
    height: auto;
    grid-template-columns: minmax(0, 1fr);
    gap: clamp(1rem, 3vh, 2rem);
}

.charts-list /* Individual Plotly graph card. */
.chart-card {
    height: clamp(320px, 48vh, 440px);
}

.chart-card {
    width: 100%;
    min-width: 0;
    min-height: 260px;
    height: 100%;
    padding: 0.25rem;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--card-radius);
    box-shadow: var(--shadow-small);
}

.chart {
    width: 100%;
    min-width: 0;
    height: 100%;
}

.chart .plot-container,
.chart .svg-container {
    width: 100% !important;
    height: 100% !important;
}

/* Advanced mathematical analysis section. */
.analysis-card {
    width: 100%;
    min-width: 0;
    margin-top: clamp(0.75rem, 2vh, 1.5rem);
    padding: clamp(1rem, 2vw, 1.5rem);
    display: flex;
    flex-direction: column;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--card-radius);
    box-shadow: var(--shadow-medium);
}

.analysis-card h3 {
    margin-bottom: 1.2rem;
    padding-bottom: 0.5rem;
    color: var(--text-primary);
    border-bottom: 2px solid var(--purple);
    font-size: 1.1rem;
}

.analysis-body {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 280px), 1fr));
    gap: 1rem;
    color: #334155;
    font-size: 0.9rem;
    line-height: 1.6;
}

/* Single analysis result card. */
.analysis-item {
    min-width: 0;
    padding: 1rem;
    overflow-wrap: anywhere;
    background: var(--page-bg);
    border-left: 4px solid #cbd5e1;
    border-radius: var(--small-radius);
    box-shadow: 0 1px 3px rgb(0 0 0 / 0.03);
}

.analysis-item > strong:first-child {
    display: inline-block;
    margin-bottom: 0.25rem;
    color: var(--text-primary);
}

.analysis-item.root {
    border-left-color: var(--root-accent);
}

.analysis-item.extrema {
    border-left-color: var(--extrema-accent);
}

.analysis-item.inflection {
    border-left-color: var(--inflection-accent);
}

.analysis-item.analysis-structure {
    border-left-color: var(--structure-accent);
}

.analysis-item.analysis-limits {
    border-left-color: var(--limits-accent);
}

.analysis-item.analysis-integration {
    border-left-color: var(--integration-accent);
}

.analysis-note {
    display: block;
    margin-top: 0.75rem;
    color: var(--text-muted);
    font-size: 0.78rem;
    font-style: italic;
}

/* Utility class for hidden elements. */
.hidden {
    display: none !important;
}

/* Tablet layout adjustments. */
@media (max-width: 1100px) {
    body {
        align-items: flex-start;
    }

    .container {
        min-height: auto;
    }

    #workspace {
        height: auto;
    }

    .formula-display {
        height: auto;
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }

    .formula-card {
        min-height: 7rem;
    }

    .charts-grid {
        height: auto;
        grid-template-columns: 1fr;
    }

    .chart-card,
    .charts-list .chart-card {
        height: clamp(300px, 45vh, 390px);
    }
}

/* Mobile layout adjustments. */
@media (max-width: 720px) {
    body {
        padding: 0.75rem;
    }

    .container {
        gap: 0.8rem;
    }

    .control-panel {
        gap: 0.5rem;
    }

    .control-panel button {
        flex: 1 1 12rem;
        padding-inline: 0.9rem;
    }

    .formula-display {
        grid-template-columns: 1fr;
    }

    .formula-card {
        min-height: 6.5rem;
    }

    .chart-card,
    .charts-list .chart-card {
        height: 320px;
    }

    .analysis-card {
        padding: 1rem;
    }

    .analysis-body {
        grid-template-columns: 1fr;
    }
}

/* Small-phone layout adjustments. */
@media (max-width: 420px) {
    header h1 {
        font-size: 1.25rem;
    }

    .control-panel {
        align-items: stretch;
        flex-direction: column;
    }

    .control-panel button {
        width: 100%;
        flex-basis: auto;
    }

    .chart-card,
    .charts-list .chart-card {
        height: 285px;
        min-height: 285px;
    }

    .formula-render {
        justify-content: flex-start;
    }
}

/* Accessibility: reduce animations when requested. */
@media (prefers-reduced-motion: reduce) {
    html {
        scroll-behavior: auto;
    }

    *,
    *::before,
    *::after {
        scroll-behavior: auto !important;
        transition-duration: 0.01ms !important;
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
    }
}

/* Printer-friendly stylesheet. */
@media print {
    body {
        display: block;
        overflow: visible;
        padding: 0;
        background: #ffffff;
    }

    .control-panel {
        display: none;
    }

    #workspace,
    .visual-area,
    .charts-grid,
    .charts-list {
        height: auto;
    }

    .chart-card {
        min-height: 300px;
        break-inside: avoid;
    }

    .analysis-card {
        box-shadow: none;
        break-inside: avoid;
    }
}