/*
 * function-machine — visual "machine" that takes an input, applies a rule
 * (one or two operations) and produces an output. NCCA Stage 4 algebra.
 * Teacher-led IWB demo first, simple student-playing surface alongside.
 */

/* The shared maths CSS sets `display: ...` on several of our containers,
 * which beats the [hidden] attribute's UA `display: none`. Force-hide
 * here so JS-toggled hidden actually hides. */
.maths-activity [hidden] { display: none !important; }

/* ---------- Stage / pipeline ---------- */

.maths-activity .fm-stage {
    background: #fffaf0;
    border: 1px solid var(--maths-border, #e0e7ef);
    border-radius: 14px;
    padding: 1.25rem 1rem;
    margin-bottom: 1rem;
    box-shadow: 0 2px 10px rgba(20, 40, 80, 0.05);
    display: flex;
    flex-direction: column;
    gap: 0.625rem;
    align-items: center;
}

.maths-activity .fm-pipeline {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    width: 100%;
    flex-wrap: wrap;
}

.maths-activity .fm-port {
    background: #fff;
    border: 2.5px solid var(--maths-primary, #2E7D32);
    border-radius: 14px;
    padding: 0.625rem 1rem;
    min-width: 96px;
    text-align: center;
    box-shadow: 0 3px 8px rgba(20, 40, 80, 0.08);
}

.maths-activity .fm-port-mid {
    border-color: #6b7280;
    background: #f4f6fa;
}

.maths-activity .fm-port-label {
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--maths-text-muted, #6b7280);
}

.maths-activity .fm-port-value {
    font-size: 1.875rem;
    font-weight: 800;
    color: var(--maths-primary-dark, #1b5e20);
    line-height: 1.1;
    font-variant-numeric: tabular-nums;
    transition: transform 220ms ease-out, color 200ms ease-out;
}

.maths-activity .fm-port-mid .fm-port-value { color: #475569; font-size: 1.5rem; }
.maths-activity .fm-port-output .fm-port-value { color: #6b7280; }
.maths-activity .fm-port-output .fm-port-value.fm-revealed { color: var(--maths-primary-dark, #1b5e20); }
.maths-activity .fm-port-mid .fm-port-value.fm-revealed { color: #1f2937; }

.maths-activity .fm-port-value.fm-pulse { transform: scale(1.18); }

.maths-activity .fm-arrow {
    color: var(--maths-primary, #2E7D32);
    font-size: 1.4rem;
}

/* ---------- Stage cards (the "machines") ---------- */

.maths-activity .fm-stage-card {
    background: linear-gradient(160deg, #1565c0 0%, #0d47a1 100%);
    color: #fff;
    border: 3px solid #0b3a73;
    border-radius: 16px;
    padding: 0.75rem 1.25rem;
    min-width: 130px;
    text-align: center;
    box-shadow: 0 4px 14px rgba(13, 71, 161, 0.3);
    position: relative;
    transition: box-shadow 220ms ease-out, transform 220ms ease-out;
}

.maths-activity .fm-stage-card.fm-active {
    box-shadow: 0 6px 20px rgba(255, 193, 7, 0.55), 0 0 0 3px rgba(255, 193, 7, 0.4);
    transform: translateY(-2px);
}

.maths-activity .fm-stage-cogs {
    display: flex;
    gap: 0.4rem;
    justify-content: center;
    font-size: 1rem;
    color: rgba(255, 255, 255, 0.55);
    margin-bottom: 0.25rem;
}

/* Cogs only spin while the value is in flight */
.maths-activity .fm-stage-card.fm-active .fm-stage-cogs i:nth-child(1) { animation: fm-spin 0.9s linear infinite; }
.maths-activity .fm-stage-card.fm-active .fm-stage-cogs i:nth-child(2) { animation: fm-spin 0.7s linear infinite reverse; }
.maths-activity .fm-stage-card.fm-active .fm-stage-cogs { color: rgba(255, 215, 0, 0.95); }

@keyframes fm-spin {
    from { transform: rotate(0deg); }
    to   { transform: rotate(360deg); }
}

.maths-activity .fm-stage-rule {
    font-size: 1.4rem;
    font-weight: 800;
    line-height: 1.1;
    font-variant-numeric: tabular-nums;
    letter-spacing: 0.02em;
}

.maths-activity .fm-stage-rule.fm-rule-hidden {
    color: rgba(255, 255, 255, 0.5);
    font-style: italic;
}

/* ---------- Input cluster ---------- */

.maths-activity .fm-input-cluster {
    background: #fff;
    border: 1px solid var(--maths-border, #e0e7ef);
    border-radius: 14px;
    padding: 0.875rem 1rem;
    margin-bottom: 1rem;
    box-shadow: 0 2px 8px rgba(20, 40, 80, 0.04);
    display: flex;
    flex-direction: column;
    gap: 0.625rem;
}

.maths-activity .fm-input-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.625rem;
    flex-wrap: wrap;
}

.maths-activity .fm-input-label {
    font-weight: 800;
    color: var(--maths-text, #1f2937);
    font-size: 1rem;
    margin-right: 0.25rem;
}

/* Higher specificity than the shared `.maths-activity input[type="number"]`
 * rule (which sets width: 100%). Pin it to a sensible size in the cluster. */
.maths-activity input.fm-input-field {
    width: 130px;
    flex: 0 0 130px;
    padding: 0.5rem 0.625rem;
    border: 2px solid var(--maths-primary, #2E7D32);
    border-radius: 10px;
    font-size: 1.5rem;
    font-weight: 800;
    text-align: center;
    color: var(--maths-primary-dark, #1b5e20);
    font-variant-numeric: tabular-nums;
    background: #fff;
    min-height: 44px;
}

.maths-activity input.fm-input-field:focus {
    outline: none;
    box-shadow: 0 0 0 3px rgba(46, 125, 50, 0.25);
    border-color: var(--maths-primary, #2E7D32);
}

/* Hide native number-input spinner arrows — we have explicit +/- buttons. */
.maths-activity input.fm-input-field::-webkit-inner-spin-button,
.maths-activity input.fm-input-field::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
}
.maths-activity input.fm-input-field { -moz-appearance: textfield; }

.maths-activity .fm-step-btn {
    width: 44px;
    height: 44px;
    border: 1.5px solid var(--maths-border, #e0e7ef);
    background: #f8fafc;
    border-radius: 10px;
    color: var(--maths-text, #1f2937);
    font-size: 1rem;
    cursor: pointer;
    transition: background 150ms ease-out;
    flex: 0 0 44px;
}

.maths-activity .fm-step-btn:hover { background: #f1f5f9; }
.maths-activity .fm-step-btn:active { background: #e2e8f0; }

.maths-activity .fm-random-btn {
    background: #f1f8e9;
    border: 1.5px solid #aed581;
    border-radius: 10px;
    padding: 0 0.875rem;
    height: 44px;
    font-weight: 700;
    color: #33691e;
    cursor: pointer;
    font-size: 0.875rem;
    white-space: nowrap;
}

.maths-activity .fm-random-btn:hover { background: #e8f5e9; }

.maths-activity .fm-action-row {
    display: flex;
    justify-content: center;
    margin-top: 0.25rem;
}

.maths-activity .fm-send-btn {
    background: var(--maths-primary, #2E7D32);
    color: #fff;
    border: none;
    border-radius: 12px;
    padding: 0.875rem 2rem;
    font-weight: 800;
    font-size: 1.1rem;
    cursor: pointer;
    transition: filter 150ms, transform 100ms;
    box-shadow: 0 3px 8px rgba(46, 125, 50, 0.25);
    min-width: 220px;
}

.maths-activity .fm-send-btn:hover { filter: brightness(1.08); }
.maths-activity .fm-send-btn:active { filter: brightness(0.95); transform: translateY(1px); }

/* ---------- Table ---------- */

.maths-activity .fm-table-wrap {
    background: #fff;
    border: 1px solid var(--maths-border, #e0e7ef);
    border-radius: 14px;
    padding: 0.875rem 1.125rem;
    margin-bottom: 1rem;
    box-shadow: 0 2px 8px rgba(20, 40, 80, 0.04);
}

.maths-activity .fm-table-label {
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--maths-text-muted, #6b7280);
    margin-bottom: 0.5rem;
}

.maths-activity .fm-table {
    width: 100%;
    border-collapse: collapse;
    font-weight: 700;
}

.maths-activity .fm-table th {
    text-align: left;
    padding: 0.4rem 0.625rem;
    background: #f1f8e9;
    color: var(--maths-primary-dark, #1b5e20);
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    border-bottom: 2px solid var(--maths-primary, #2E7D32);
}

.maths-activity .fm-table th:nth-child(2) { text-align: center; color: var(--maths-text-muted, #6b7280); }

.maths-activity .fm-table td {
    padding: 0.4rem 0.625rem;
    border-bottom: 1px solid var(--maths-border, #e0e7ef);
    color: var(--maths-text, #1f2937);
    font-size: 1rem;
    font-variant-numeric: tabular-nums;
}

.maths-activity .fm-table td:nth-child(2) { text-align: center; color: var(--maths-text-muted, #6b7280); }

.maths-activity .fm-table td.fm-cell-prefilled { background: #fffaf0; }
.maths-activity .fm-table td.fm-cell-output-hidden { color: var(--maths-text-muted, #6b7280); font-style: italic; }

/* ---------- Mode chip ---------- */

.maths-activity .fm-mode-chip {
    display: inline-block;
    background: var(--maths-primary, #2E7D32);
    color: #fff;
    font-size: 0.7rem;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 0.25rem 0.625rem;
    border-radius: 999px;
    margin-top: 0.25rem;
}

/* ---------- Challenge bar (hide-and-reveal) ---------- */

.maths-activity .fm-challenge-bar {
    background: #fff8e1;
    border: 1px solid #f0c14b;
    border-radius: 14px;
    padding: 0.75rem 1rem;
    margin-bottom: 1rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-wrap: wrap;
}

.maths-activity .fm-challenge-prompt {
    font-weight: 600;
    color: #1f2937;
    flex: 1;
    min-width: 200px;
}

.maths-activity .fm-show-rule-btn {
    background: var(--maths-primary, #2E7D32);
    color: #fff;
    border: none;
    border-radius: 10px;
    padding: 0.5rem 1rem;
    font-weight: 800;
    cursor: pointer;
    font-size: 0.95rem;
}

.maths-activity .fm-show-rule-btn:hover:not(:disabled) { filter: brightness(1.08); }
.maths-activity .fm-show-rule-btn:disabled {
    background: #cbd5e1;
    color: #64748b;
    cursor: default;
}

/* ---------- Your turn ---------- */

.maths-activity .fm-your-turn {
    margin-top: 0.5rem;
    background: #f1f8e9;
    border: 1px solid #aed581;
    border-radius: 14px;
    padding: 1rem 1.25rem;
}

.maths-activity .fm-your-turn-label {
    font-weight: 800;
    color: #33691e;
    text-transform: uppercase;
    font-size: 0.75rem;
    letter-spacing: 0.05em;
    margin-bottom: 0.5rem;
}

.maths-activity .fm-your-turn-list { margin: 0; padding-left: 1.25rem; color: #1f2937; font-weight: 600; }
.maths-activity .fm-your-turn-list li { margin-bottom: 0.25rem; }

/* ---------- Display mode ----------
 * The widget already keys on .fm-root[data-mode="display"] (its own
 * convention). Hide the input cluster, challenge bar, callout and
 * challenge runner; tighten the pipeline so the snapshot fits the
 * lesson page. Your-turn prompts stay — those are display-only
 * discussion prompts.
 */
.maths-activity .fm-root[data-mode="display"] .fm-input-cluster,
.maths-activity .fm-root[data-mode="display"] .fm-challenge-bar,
.maths-activity .fm-root[data-mode="display"] #challengeRunner { display: none !important; }

.maths-activity .fm-root[data-mode="display"] .fm-stage {
    padding: 0.6rem 0.4rem;
    margin-bottom: 0.6rem;
}

.maths-activity .fm-root[data-mode="display"] .fm-port {
    padding: 0.5rem 0.75rem;
    min-width: 80px;
}

.maths-activity .fm-root[data-mode="display"] .fm-port-value {
    font-size: 1.5rem;
}

.maths-activity .fm-root[data-mode="display"] .fm-stage-card {
    padding: 0.5rem 0.75rem;
    min-width: 100px;
}

.maths-activity .fm-root[data-mode="display"] .fm-stage-rule {
    font-size: 1.15rem;
}

/* ---------- Responsive ---------- */

@media (max-width: 720px) {
    .maths-activity .fm-pipeline { gap: 0.25rem; }
    .maths-activity .fm-port { min-width: 80px; padding: 0.5rem 0.75rem; }
    .maths-activity .fm-port-value { font-size: 1.5rem; }
    .maths-activity .fm-stage-card { min-width: 100px; padding: 0.5rem 0.75rem; }
    .maths-activity .fm-stage-rule { font-size: 1.15rem; }
    .maths-activity .fm-arrow { font-size: 1.1rem; }
    .maths-activity .fm-random-btn { margin-left: 0; }
}

/* ---------- Guess-and-validate rule builder (challenge mode) ---------- */

/* A compact white "rule" chip that sits inside the yellow challenge bar.
   Stays a tidy inline row instead of stretching full-width. */
.maths-activity .fm-guess-builder {
    display: inline-flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.45rem;
    padding: 0.5rem 0.7rem;
    background: #fff;
    border: 1px solid var(--maths-border, #c7dcf2);
    border-radius: 12px;
}
.maths-activity .fm-guess-label,
.maths-activity .fm-guess-then {
    font-weight: 800;
    color: var(--maths-ink, #163a5f);
    white-space: nowrap;
}
.maths-activity .fm-guess-then { margin: 0 0.1rem; }

/* Operation picker: a row of tappable + − × ÷ buttons; the chosen one
   highlights. Bigger tap targets than a dropdown and primary-friendly. */
.maths-activity .fm-guess-ops {
    display: inline-flex;
    gap: 0.3rem;
}
.maths-activity .fm-op-btn {
    width: 2.6rem;
    height: 2.6rem;
    flex: 0 0 auto;
    font-size: 1.35rem;
    font-weight: 800;
    line-height: 1;
    text-align: center;
    border: 1.5px solid var(--maths-border, #c7dcf2);
    border-radius: 10px;
    background: #f5f9ff;
    color: var(--maths-ink, #163a5f);
    cursor: pointer;
    transition: background-color 120ms ease, border-color 120ms ease;
}
.maths-activity .fm-op-btn:hover { background: #e8f1ff; }
.maths-activity .fm-op-btn.is-selected {
    background: var(--maths-primary, #2E7D32);
    border-color: var(--maths-primary, #2E7D32);
    color: #fff;
}
/* id-scoped + !important so the shared full-width form-control rule can't
   stretch or stack the number box — it must stay a small fixed control. */
.maths-activity #guessBuilder .fm-guess-n {
    width: 4rem !important;
    flex: 0 0 auto !important;
    font-size: 1.15rem;
    font-weight: 700;
    text-align: center;
    padding: 0.4rem 0.5rem;
    border: 1.5px solid var(--maths-border, #c7dcf2);
    border-radius: 9px;
}
/* Remove the number-input spinner arrows (keeps the numeric keypad on
   mobile via type=number, just no up/down stepper). */
.maths-activity #guessBuilder .fm-guess-n::-webkit-outer-spin-button,
.maths-activity #guessBuilder .fm-guess-n::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}
.maths-activity #guessBuilder .fm-guess-n {
    -moz-appearance: textfield;
    appearance: textfield;
}
/* Keep the [hidden] attribute working despite the inline-flex display. */
.maths-activity .fm-guess-stage2 { align-items: center; gap: 0.45rem; }
.maths-activity .fm-guess-stage2:not([hidden]) { display: inline-flex; }
.maths-activity .fm-guess-check-btn { margin-left: 0.3rem; }
