/* ==========================================================================
   Algebra Mode — Paper Edition
   ========================================================================== */

/* Hide zoom controls when algebra panel is active */
body.is-algebra-mode #graph-controls { display: none; }

/* ── Panel container — lined paper ────────────────────────────────────── */

/* Explicit override so the HTML `hidden` attribute wins over `display: flex` below */
.algebra-panel[hidden] { display: none; }

.algebra-panel {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 36px 0 96px;
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    box-sizing: border-box;

    background-color: #fdfdf8;
    background-image:
        /* red margin rule */
        linear-gradient(to right,
            transparent 68px,
            rgba(210, 80, 80, 0.16) 68px,
            rgba(210, 80, 80, 0.16) 70px,
            transparent 70px),
        /* horizontal rules every 40px */
        repeating-linear-gradient(
            transparent,
            transparent 39px,
            rgba(100, 140, 220, 0.11) 39px,
            rgba(100, 140, 220, 0.11) 40px
        );
    background-attachment: local;
}

[data-theme="dark"] .algebra-panel {
    background-color: #181926;
    background-image:
        linear-gradient(to right,
            transparent 68px,
            rgba(200, 60, 60, 0.12) 68px,
            rgba(200, 60, 60, 0.12) 70px,
            transparent 70px),
        repeating-linear-gradient(
            transparent,
            transparent 39px,
            rgba(100, 140, 220, 0.07) 39px,
            rgba(100, 140, 220, 0.07) 40px
        );
}

/* ── Chain ─────────────────────────────────────────────────────────────── */

.alg-chain {
    width: 100%;
    max-width: 600px;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    padding: 0 28px;
}

/* ── Equation row ──────────────────────────────────────────────────────── */

.alg-row {
    display: flex;
    align-items: center;
    flex-wrap: wrap;          /* lets .alg-op-anchor fall below */
    padding: 10px 0;
    background: transparent;
    border: none;
    border-radius: 0;
    animation: stepIn 0.2s ease;
    position: relative;
}

/* Original equation — yellow highlighter strip */
.alg-row-original::before {
    content: '';
    position: absolute;
    inset: 4px -12px;
    background: rgba(255, 235, 59, 0.32);
    border-radius: 4px;
    pointer-events: none;
}

/* Slot row — dashed underline feel */
.alg-row-slot {
    border-bottom: 2px dashed rgba(59, 124, 244, 0.25);
}

/* Wrong row — pink wash */
.alg-row-wrong::before {
    content: '';
    position: absolute;
    inset: 4px -12px;
    background: rgba(239, 68, 68, 0.08);
    border-radius: 4px;
    pointer-events: none;
}

/* ── Step label ────────────────────────────────────────────────────────── */

.alg-step-label {
    text-align: center;
    font-family: 'Caveat', 'Georgia', cursive;
    font-size: 1rem;
    font-style: italic;
    color: #b0b8c8;
    padding: 2px 0;
    position: relative;
    line-height: 1.2;
}

.alg-step-label::before {
    content: '';
    display: block;
    width: 1px;
    height: 8px;
    background: rgba(100, 140, 220, 0.25);
    margin: 0 auto 2px;
}

[data-theme="dark"] .alg-step-label { color: #4b5563; }

/* ── Equation sides — symmetric flex so = stays centred ───────────────── */

.alg-side {
    flex: 1;
    display: flex;
    align-items: center;
    min-width: 0;
    min-height: 48px;
    padding: 2px 8px;
    transition: background 0.12s;
}

.alg-side.alg-lhs { justify-content: flex-end; }
.alg-side.alg-rhs { justify-content: flex-start; }

.alg-side.alg-drag-over {
    background: rgba(59, 124, 244, 0.07);
    border-radius: 8px;
    outline: 2px dashed rgba(59, 124, 244, 0.4);
    outline-offset: 4px;
}

.alg-equals {
    flex-shrink: 0;
    font-family: 'Caveat', 'Georgia', cursive;
    font-size: 2rem;
    font-weight: 400;
    color: #6b7280;
    user-select: none;
    padding: 0 10px;
    line-height: 1;
}

[data-theme="dark"] .alg-equals { color: #4b5563; }

/* ── Op strip anchor — wraps below equation in the same flex row ────────── */

.alg-op-anchor {
    flex-basis: 100%;
    width: 100%;
}

/* ── Op strip ──────────────────────────────────────────────────────────── */

.alg-op-strip {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 8px 4px 10px;
    animation: stepIn 0.12s ease;
}

.alg-op-btn {
    padding: 5px 16px;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 0.76rem;
    font-weight: 600;
    letter-spacing: 0.015em;
    background: #fff;
    border: 1.5px solid rgba(37, 99, 235, 0.3);
    border-radius: 20px;
    color: #1d4ed8;
    cursor: pointer;
    white-space: nowrap;
    transition: all 0.14s;
    box-shadow: 0 1px 4px rgba(37, 99, 235, 0.08), 0 0 0 0 rgba(37, 99, 235, 0);
}

.alg-op-btn:hover {
    background: #1d4ed8;
    border-color: #1d4ed8;
    color: #fff;
    box-shadow: 0 3px 10px rgba(37, 99, 235, 0.28);
    transform: translateY(-1px);
}

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

[data-theme="dark"] .alg-op-btn {
    background: rgba(37, 99, 235, 0.08);
    border-color: rgba(96, 165, 250, 0.3);
    color: #93c5fd;
    box-shadow: none;
}
[data-theme="dark"] .alg-op-btn:hover {
    background: #1e40af;
    border-color: #3b82f6;
    color: #fff;
    box-shadow: 0 3px 10px rgba(59, 130, 246, 0.3);
}

/* ── Terms ─────────────────────────────────────────────────────────────── */

.alg-term {
    display: inline-flex;
    align-items: center;
    font-family: 'Caveat', 'Georgia', cursive;
    font-size: 1.85rem;
    font-weight: 500;
    color: #1c1c2e;
    padding: 0 3px;
    border-radius: 5px;
    cursor: pointer;
    transition: background 0.1s, color 0.1s, opacity 0.15s;
    user-select: none;
    line-height: 1.15;
}

[data-theme="dark"] .alg-term { color: #dde6f5; }

.alg-term:hover              { background: rgba(37, 99, 235, 0.07); }
.alg-term.alg-term-selected  {
    background: rgba(37, 99, 235, 0.1);
    color: #1d4ed8;
    outline: 1.5px solid rgba(37, 99, 235, 0.5);
    outline-offset: 2px;
}
.alg-term.alg-dragging       { opacity: 0.25; cursor: grabbing; }
.alg-term[draggable]         { cursor: grab; }
.alg-term[draggable]:active  { cursor: grabbing; }

.alg-const { color: #1c1c2e; }
.alg-sym   { color: #1d4ed8; font-weight: 600; }

[data-theme="dark"] .alg-const { color: #dde6f5; }
[data-theme="dark"] .alg-sym   { color: #60a5fa; }

.alg-op {
    font-family: 'Caveat', 'Georgia', cursive;
    font-size: 1.75rem;
    font-weight: 400;
    color: #6b7280;
    padding: 0 3px;
    user-select: none;
}

[data-theme="dark"] .alg-op { color: #4b5563; }

/* ── Powers — display:inline so <sup> works naturally ─────────────────── */

.alg-term.alg-power { display: inline; }

.alg-power sup {
    font-size: 0.58em;
    line-height: 0;
    vertical-align: 0.65em;
}

/* ── Fractions ─────────────────────────────────────────────────────────── */

.alg-fraction {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    gap: 1px;
    vertical-align: middle;
    margin: 0 4px;
}

.alg-numerator,
.alg-denominator {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 3px;
    font-size: 0.8em;
}

.alg-frac-bar {
    width: 100%;
    height: 1.5px;
    background: currentColor;
    border-radius: 1px;
}

/* ── Parentheses ───────────────────────────────────────────────────────── */

.alg-paren {
    font-family: 'Caveat', 'Georgia', cursive;
    font-size: 2rem;
    font-weight: 400;
    color: #9ca3af;
    user-select: none;
    padding: 0 1px;
    line-height: 1;
}

[data-theme="dark"] .alg-paren { color: #374151; }

/* ── Slot input — underline style, feels like writing on paper ─────────── */

.alg-slot {
    display: inline-flex;
    min-width: 3ch;
    font-size: 1.85rem;
    font-family: 'Caveat', 'Georgia', cursive;
    font-weight: 500;
    color: #1c1c2e;
    background: rgba(255, 235, 59, 0.12);
    border: none;
    border-bottom: 2.5px solid #1d4ed8;
    border-radius: 2px;
    padding: 0 10px 2px;
    text-align: center;
    outline: none;
    box-sizing: content-box;
    transition: background 0.15s, border-color 0.15s;
    animation: slotAppear 0.18s ease;
}

.alg-slot:focus {
    background: rgba(37, 99, 235, 0.05);
    border-bottom-color: #1d4ed8;
}

[data-theme="dark"] .alg-slot {
    color: #dde6f5;
    background: rgba(96, 165, 250, 0.07);
    border-bottom-color: #60a5fa;
}

/* ── Slot live preview (rendered expression below the slot row) ─────────── */

.alg-input-preview {
    flex-basis: 100%;       /* wraps below lhs/=/rhs in the flex row */
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 4px;
    padding: 0 0 8px;
    color: #1d4ed8;
    opacity: 0.55;
    pointer-events: none;
    animation: stepIn 0.1s ease;
}

[data-theme="dark"] .alg-input-preview { color: #60a5fa; }

/* ── Free-form step input ──────────────────────────────────────────────── */

.alg-freeform {
    width: 100%;
    max-width: 600px;
    margin-top: 20px;
    padding: 0 28px;
    display: flex;
    flex-direction: column;
}

/* Freeform preview — rendered equation above the input line */
.alg-freeform-preview {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 6px 0 8px;
    border-bottom: 1px solid rgba(100, 140, 220, 0.18);
    margin-bottom: 4px;
    opacity: 0.6;
    pointer-events: none;
    animation: stepIn 0.12s ease;
}

[data-theme="dark"] .alg-freeform-preview { border-bottom-color: rgba(100, 140, 220, 0.1); }

.alg-freeform-input {
    width: 100%;
    padding: 8px 4px;
    font-size: 1.5rem;
    font-family: 'Caveat', 'Georgia', cursive;
    font-weight: 500;
    color: #6b7280;
    background: transparent;
    border: none;
    border-bottom: 2px dashed rgba(100, 140, 220, 0.3);
    border-radius: 0;
    outline: none;
    transition: border-color 0.15s, color 0.15s;
}

.alg-freeform-input::placeholder {
    color: #c4ccd8;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 0.8rem;
    font-weight: 400;
    font-style: italic;
}

.alg-freeform-input:focus {
    border-bottom-color: #1d4ed8;
    color: #1c1c2e;
}

[data-theme="dark"] .alg-freeform-input {
    color: #4b5563;
    border-bottom-color: rgba(100, 140, 220, 0.2);
}
[data-theme="dark"] .alg-freeform-input:focus {
    color: #dde6f5;
    border-bottom-color: #60a5fa;
}

.alg-freeform-input.alg-freeform-error { border-bottom-color: #ef4444; }

/* ── Undo button ───────────────────────────────────────────────────────── */

.alg-undo-btn {
    margin-top: 10px;
    padding: 5px 16px;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 0.75rem;
    font-weight: 500;
    letter-spacing: 0.01em;
    background: none;
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 20px;
    color: #9ca3af;
    cursor: pointer;
    align-self: center;
    transition: all 0.14s;
}

.alg-undo-btn:hover {
    border-color: #ef4444;
    color: #ef4444;
    background: rgba(239, 68, 68, 0.04);
}

[data-theme="dark"] .alg-undo-btn { border-color: rgba(255,255,255,0.1); color: #4b5563; }

/* ── Submit button ─────────────────────────────────────────────────────── */

.alg-submit-btn {
    margin-top: 28px;
    padding: 13px 40px;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 0.88rem;
    font-weight: 700;
    letter-spacing: 0.03em;
    text-transform: uppercase;
    background: #1d4ed8;
    color: #fff;
    border: none;
    border-radius: 32px;
    cursor: pointer;
    align-self: center;
    animation: stepIn 0.2s ease;
    transition: all 0.18s;
    box-shadow:
        0 4px 16px rgba(29, 78, 216, 0.32),
        0 1px 4px rgba(29, 78, 216, 0.16);
}

.alg-submit-btn:hover:not(:disabled) {
    background: #1640b0;
    box-shadow:
        0 6px 24px rgba(29, 78, 216, 0.4),
        0 2px 8px rgba(29, 78, 216, 0.2);
    transform: translateY(-2px);
}

.alg-submit-btn:active:not(:disabled) {
    transform: translateY(0);
    box-shadow: 0 2px 8px rgba(29, 78, 216, 0.2);
}

.alg-submit-btn:disabled {
    opacity: 0.45;
    cursor: default;
    box-shadow: none;
    transform: none;
}

.alg-submit-btn.alg-submit-correct {
    background: #16a34a;
    box-shadow: 0 4px 16px rgba(22, 163, 74, 0.3);
}
.alg-submit-btn.alg-submit-wrong {
    background: #dc2626;
    box-shadow: 0 4px 16px rgba(220, 38, 38, 0.3);
}

/* ── Hint chip ─────────────────────────────────────────────────────────── */

.alg-hint-chip {
    margin-top: 18px;
    padding: 8px 22px;
    background: rgba(234, 179, 8, 0.07);
    border: 1.5px solid rgba(234, 179, 8, 0.28);
    border-radius: 24px;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 0.82rem;
    color: #92400e;
    animation: stepIn 0.2s ease;
    align-self: center;
}

[data-theme="dark"] .alg-hint-chip {
    background: rgba(234, 179, 8, 0.09);
    border-color: rgba(234, 179, 8, 0.22);
    color: #fcd34d;
}

/* ── Solved badge ──────────────────────────────────────────────────────── */

.alg-solved-badge {
    margin-top: 24px;
    padding: 12px 36px;
    background: rgba(34, 197, 94, 0.07);
    border: 2px solid rgba(34, 197, 94, 0.28);
    border-radius: 32px;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 0.88rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: #16a34a;
    animation: solvedIn 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
    align-self: center;
    box-shadow: 0 4px 20px rgba(34, 197, 94, 0.12);
}

[data-theme="dark"] .alg-solved-badge {
    background: rgba(34, 197, 94, 0.09);
    border-color: rgba(34, 197, 94, 0.22);
    color: #4ade80;
}

/* ── Hinted term ───────────────────────────────────────────────────────── */

.alg-hinted {
    background: rgba(234, 179, 8, 0.18) !important;
    color: #92400e !important;
    border-radius: 4px;
}

[data-theme="dark"] .alg-hinted {
    background: rgba(234, 179, 8, 0.15) !important;
    color: #fcd34d !important;
}

/* ── Animations ────────────────────────────────────────────────────────── */

@keyframes stepIn {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: translateY(0); }
}

@keyframes slotAppear {
    from { opacity: 0; transform: scaleX(0.7); }
    to   { opacity: 1; transform: scaleX(1); }
}

@keyframes solvedIn {
    from { opacity: 0; transform: scale(0.8) translateY(4px); }
    to   { opacity: 1; transform: scale(1) translateY(0); }
}

/* ── Sandbox page styles ───────────────────────────────────────────────── */

.alg-sandbox-wrapper {
    max-width: 640px;
    margin: 0 auto;
    padding: 0 24px 48px;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}
