:root {
    --primary-color: #007bff;
    --secondary-color: #6c757d;
    --success-color: #28a745;
    --bg-light: #f8f9fa;
    --card-bg: #ffffff;
    --text-main: #333;
    --border-color: #dee2e6;
    --shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    --radius: 12px;
}

body {
    background-color: #f0f2f5;
    color: var(--text-main);
    font-family: 'Inter', sans-serif;
    margin: 0;
    line-height: 1.6;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

.app-container {
    max-width: 1000px;
    width: 100%;
    margin: 40px auto;
    padding: 0 20px;
    flex: 1;
}

.calculator-card {
    background: var(--card-bg);
    padding: 30px;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    margin-bottom: 30px;
}

.app-title {
    text-align: center;
    margin-bottom: 30px;
    font-weight: 800;
    color: var(--text-main);
}

.mode-selector {
    display: flex;
    justify-content: center;
    gap: 10px;
    margin-bottom: 30px;
}

.mode-btn {
    padding: 10px 20px;
    border: 2px solid var(--border-color);
    background: transparent;
    border-radius: 25px;
    cursor: pointer;
    font-weight: 600;
    transition: all 0.3s ease;
}

.mode-btn.active {
    background: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
}

.equation-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    flex-wrap: wrap;
    margin-bottom: 20px;
    font-size: 1.2rem;
}

.equation-row input {
    width: 70px;
    padding: 8px;
    border: 2px solid var(--border-color);
    border-radius: 8px;
    text-align: center;
    font-size: 1.1rem;
    font-family: 'Fira Code', monospace;
}

.equation-row input:focus {
    border-color: var(--primary-color);
    outline: none;
}

.calculate-btn {
    display: block;
    width: 100%;
    max-width: 300px;
    margin: 20px auto;
    padding: 12px;
    background: var(--success-color);
    color: white;
    border: none;
    border-radius: 25px;
    font-size: 1.1rem;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.2s ease, background 0.3s ease;
}

.calculate-btn:hover {
    background: #218838;
    transform: translateY(-2px);
}

.results-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
    margin-top: 30px;
    padding-top: 20px;
    border-top: 1px solid var(--border-color);
}

.result-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    background: var(--bg-light);
    padding: 15px;
    border-radius: 10px;
}

.res-label {
    font-size: 0.9rem;
    color: var(--secondary-color);
    margin-bottom: 5px;
}

.res-value {
    font-weight: 700;
    font-size: 1.1rem;
    color: var(--primary-color);
}

.visual-section {
    display: grid;
    grid-template-columns: 1fr 300px;
    gap: 30px;
    align-items: start;
}

.canvas-wrapper {
    background: white;
    padding: 20px;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

canvas {
    width: 100%;
    height: auto;
    aspect-ratio: 1 / 1;
    display: block;
    border: 1px solid #eee;
}

.table-wrapper {
    flex: 1;
    background: white;
    padding: 15px;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
    height: 100%;
    overflow-y: auto;
}

.table-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
    flex-wrap: wrap;
    gap: 10px;
}

.table-wrapper h3 {
    margin-bottom: 0;
    color: #1e293b;
    font-size: 1.1rem;
}

.ctrl-btn.text-btn {
    width: auto;
    height: auto;
    padding: 6px 12px;
    font-size: 0.85rem;
    font-weight: 600;
    border: 1px solid #e2e8f0;
    background: #f8fafc;
    border-radius: 6px;
    color: #475569;
}

.ctrl-btn.text-btn:hover {
    background: #e2e8f0;
    color: #1e293b;
}

.ctrl-btn.text-btn.active {
    background: #e0f2fe;
    color: #0284c7;
    border-color: #0284c7;
}

table {
    width: 100%;
    border-collapse: collapse;
}

th,
td {
    padding: 10px;
    border-bottom: 1px solid #eee;
    text-align: center;
}

th {
    background: var(--bg-light);
    font-weight: 600;
}

tr:last-child td {
    border-bottom: none;
}

/* Math editor styling */
.math-editor {
    width: 100%;
    max-width: 500px;
    margin: 10px auto;
    font-size: 1.5rem;
    padding: 8px;
    border: 1px solid #ddd;
    border-radius: 8px;
    background: white;
}

.editor-wrapper {
    position: relative;
    width: 100%;
    max-width: 500px;
    margin: 0 auto;
}

.placeholder-overlay {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    color: #94a3b8;
    pointer-events: none;
    font-family: 'Inter', sans-serif;
    font-size: 1rem;
    font-weight: 400;
    transition: opacity 0.2s ease;
    z-index: 10;
    white-space: nowrap;
}

.editor-wrapper.focused .placeholder-overlay,
.editor-wrapper.has-content .placeholder-overlay {
    opacity: 0;
}

/* Formula section styling */
.formula-list {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
    justify-content: center;
    margin-top: 20px;
}

.formula-item {
    background: var(--bg-light);
    padding: 10px 15px;
    border-radius: 8px;
    font-size: 1rem;
}

.formula-item strong {
    color: var(--secondary-color);
    margin-right: 5px;
}

/* Row highlighting */
.row-vertex {
    border-left: 5px solid red;
}

.row-pair-1 {
    border-left: 5px solid #3b82f6;
}

.row-pair-2 {
    border-left: 5px solid #10b981;
}

.row-pair-3 {
    border-left: 5px solid #f97316;
}

.row-pair-4 {
    border-left: 5px solid #8b5cf6;
}

/* Step-by-step section */
.steps-section {
    margin-top: 30px;
    padding-top: 20px;
    border-top: 2px dashed #e2e8f0;
    animation: fadeIn 0.5s ease-out;
}

.step-block {
    background: #ffffff;
    border-left: 6px solid #3b82f6;
    padding: 15px;
    margin-bottom: 15px;
    border-radius: 4px 12px 12px 4px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
    transition: transform 0.2s;
    flex: 1;
    min-width: 0;
}

.steps-row {
    display: flex;
    gap: 15px;
    margin-bottom: 10px;
    flex-wrap: wrap;
}

.steps-row>.step-block {
    margin-bottom: 0;
}

.step-columns {
    display: flex;
    gap: 20px;
    margin-top: 15px;
}

.step-col {
    flex: 1;
    min-width: 0;
    padding: 15px;
    background: #f8fafc;
    border-radius: 8px;
    border: 1px solid #f1f5f9;
    overflow-x: auto;
}

.col-title {
    font-weight: 700;
    color: #334155;
    margin-bottom: 12px;
    text-align: center;
    border-bottom: 2px solid #e2e8f0;
    padding-bottom: 8px;
    font-size: 0.95rem;
    text-transform: uppercase;
}

.step-block:hover {
    transform: translateX(5px);
}

.step-block:nth-child(2) {
    border-left-color: #ef4444;
}

/* Delta */
.step-block:nth-child(3) {
    border-left-color: #3b82f6;
}

/* Vertex */
.step-block:nth-child(4) {
    border-left-color: #10b981;
}

/* Focus */
.step-block:nth-child(5) {
    border-left-color: #f59e0b;
}

/* Directrix */
.step-block:nth-child(6) {
    border-left-color: #8b5cf6;
}

/* Axis */

.step-block h4 {
    margin-top: 0;
    margin-bottom: 10px;
    color: #1e293b;
    font-size: 1.1rem;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.step-latex {
    overflow-x: auto;
    padding: 10px;
    font-size: 0.95rem;
}

.steps-title {
    display: block;
    text-align: center;
    font-size: 1.6rem;
    color: #1e293b;
    margin: 30px auto 25px auto;
    padding-bottom: 10px;
    border-bottom: 3px solid #10b981;
    width: fit-content;
    font-weight: 800;
}

.result-summary {
    background: #f1f5f9;
    padding: 10px 15px;
    margin-top: 15px;
    border-radius: 6px;
    font-weight: 600;
    color: #0f172a;
    border-left: 3px solid currentColor;
    display: inline-block;
}

.result-summary.delta {
    color: #ef4444;
    border-color: #ef4444;
}

.result-summary.vertex {
    color: #3b82f6;
    border-color: #3b82f6;
}

.result-summary.focus {
    color: #10b981;
    border-color: #10b981;
}

.result-summary.directrix {
    color: #f59e0b;
    border-color: #f59e0b;
}

.result-summary.axis {
    color: #8b5cf6;
    border-color: #8b5cf6;
}

.calculate-btn.ghost {
    background: transparent;
    color: var(--secondary-color);
    border: 2px solid var(--secondary-color);
    margin: 10px 0;
}

.calculate-btn.btn-step-green {
    background: #10b981;
    color: white;
    border: none;
    padding: 12px 24px;
    font-weight: 600;
    transition: all 0.3s ease;
    box-shadow: 0 4px 6px rgba(16, 185, 129, 0.2);
    margin: 5px;
    flex: 0 1 auto;
    width: auto;
    max-width: none;
}

.explanation-buttons-container {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    gap: 15px;
    margin-top: 30px;
    margin-bottom: 10px;
}

.calculate-btn.btn-step-green:hover {
    background: #059669;
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(16, 185, 129, 0.3);
}

.calculate-btn.btn-step-green[style*="background:#3b82f6"]:hover {
    background: #2563eb !important;
}

.calculate-btn.ghost:hover {
    background: var(--secondary-color);
    color: white;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.hidden {
    display: none !important;
}

@media (max-width: 1024px) {
    .visual-section {
        grid-template-columns: 1fr;
    }

    .step-columns {
        flex-wrap: wrap;
    }

    .step-col {
        flex: 1 1 45%;
        min-width: 300px;
    }
}

@media (max-width: 768px) {
    /* .visual-section already handled by 1024px rule */

    .equation-row {
        gap: 5px;
    }

    .equation-row input {
        width: 60px;
    }

    .step-col {
        flex: 1 1 100%;
        min-width: 100%;
        margin-bottom: 20px;
        /* Add spacing when stacked */
    }

    .mode-selector {
        flex-direction: column;
        align-items: center;
    }

    .mode-btn {
        width: 100%;
        max-width: 300px;
    }
}

/* --- Graph Controls --- */
.canvas-wrapper {
    position: relative;
    /* Ensure controls are positioned relative to this */
}

.graph-controls {
    position: absolute;
    bottom: 20px;
    left: 20px;
    display: flex;
    gap: 12px;
    /* Increased gap */
    background: rgba(255, 255, 255, 0.95);
    /* Slightly more opaque */
    padding: 10px;
    /* Increased padding */
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    border: 1px solid rgba(0, 0, 0, 0.1);
    z-index: 10;
}

.ctrl-btn {
    width: 44px;
    /* Optimized touch target */
    height: 44px;
    /* Optimized touch target */
    border: none;
    background: white;
    border-radius: 10px;
    color: #333;
    font-size: 1.3rem;
    /* Slightly larger icon */
    font-family: inherit;
    font-weight: bold;
    cursor: pointer;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.ctrl-btn:hover {
    background: #f0f0f0;
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.12);
}

.ctrl-btn:active {
    transform: translateY(0);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.ctrl-btn.toggle-mode.active {
    background: #e3f2fd;
    color: #1976d2;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1);
}

.canvas-wrapper canvas.panning {
    cursor: grab;
}

.canvas-wrapper canvas.panning:active {
    cursor: grabbing;
}