/* ═══════════════════════════════════════════════════════════════════
   Factor Tree Widget
   Layout mirrors the multiplication widget:
     - Header bar (blue gradient)
     - Body: tree area left · factor tile bank right
   ═══════════════════════════════════════════════════════════════════ */

.ama-factor-tree-widget {
    /* Avatar brand palette */
    --aft-blue:       #4a7a3d;   /* --ama-primary       */
    --aft-blue-dark:  #2d5426;   /* --ama-primary-dark  */
    --aft-blue-lt:    #6aad60;   /* --ama-primary-light */
    --aft-green:      #4a7a3d;   /* --ama-success-text  */
    --aft-green-bg:   #edf7ea;
    --aft-green-bd:   #81c171;   /* --ama-success       */
    --aft-amber:      #b86000;
    --aft-amber-bg:   #fff9e6;
    --aft-amber-bd:   #ff9130;   /* --ama-orange        */

    /* Node/tile dimensions */
    --aft-node:  68px;
    --aft-dz:    62px;
    --aft-tile:  52px;

    position:   relative;
    font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: #f8fafc;
    border:     1px solid #e2e8f0;
    border-radius: 18px;
    overflow:   hidden;
    max-width:  860px;          /* same cap as multiplication widget */
    margin:     0 auto;
    box-shadow: 0 4px 28px rgba(30,64,175,0.09);
    -webkit-user-select: none;
    user-select: none;
    touch-action: none;
    display:    flex;
    flex-direction: column;
    min-height: 640px;          /* taller than multiplication widget */
}

/* ── Header — identical styling to multiplication widget ─────────── */
.aft-header {
    flex-shrink: 0;
    background: linear-gradient(135deg, var(--aft-blue-dark) 0%, var(--aft-blue) 100%);
    color: #fff;
    padding: 12px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 8px;
}

.aft-title {
    font-size: 1rem;
    font-weight: 800;
    letter-spacing: -0.02em;
    white-space: nowrap;
}

.aft-controls {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.aft-ctrl-label {
    font-size: 0.78rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 5px;
    opacity: 0.9;
}

.aft-diff-sel {
    background: rgba(255,255,255,0.18);
    border: 1px solid rgba(255,255,255,0.35);
    color: #fff;
    border-radius: 7px;
    padding: 4px 8px;
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    outline: none;
}
.aft-diff-sel option { background: var(--aft-blue-dark); }

.aft-btn {
    background: rgba(255,255,255,0.18);
    border: 1px solid rgba(255,255,255,0.38);
    color: #fff;
    border-radius: 8px;
    padding: 5px 13px;
    font-size: 0.8rem;
    font-weight: 700;
    cursor: pointer;
    transition: background 0.14s, transform 0.1s;
    white-space: nowrap;
}
.aft-btn:hover  { background: rgba(255,255,255,0.28); transform: translateY(-1px); }
.aft-btn:active { transform: translateY(0); }

.aft-target-badge {
    display: flex;
    align-items: center;
    gap: 6px;
    background: rgba(0,0,0,0.2);
    border-radius: 8px;
    padding: 3px 12px;
}
.aft-target-lbl { font-size: 0.75rem; opacity: 0.75; font-weight: 600; }
.aft-target-num { font-size: 1.05rem; font-weight: 800; }

/* ── Body — side-by-side like multiplication widget ──────────────── */
.aft-body {
    flex: 1;
    display: flex;
    flex-direction: row;
    min-height: 0;
    overflow: hidden;
}

/* ── Tree area (left, grows to fill) ─────────────────────────────── */
.aft-tree-area {
    flex: 1;
    position: relative;
    overflow: auto;
    background: #fff;
    display: flex;
    align-items: flex-start;
    justify-content: center;
}

/* SVG overlay for connector lines */
.aft-lines {
    position: absolute;
    top: 0; left: 0;
    pointer-events: none;
    overflow: visible;
}

/* Tree content — top-center aligned */
.aft-tree-content {
    display: flex;
    justify-content: center;
    padding: 30px 48px 40px;
    min-width: min-content;
    position: relative;
    z-index: 1;
}

/* ── Tree node structure ─────────────────────────────────────────── */
.aft-node-wrap {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.aft-children {
    display: flex;
    flex-direction: row;
    gap: 32px;
    margin-top: 52px;       /* room for SVG lines */
}

/* ── Node circles ────────────────────────────────────────────────── */
.aft-circle {
    width:  var(--aft-node);
    height: var(--aft-node);
    border-radius: 50%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    font-weight: 800;
    border: 3px solid;
    flex-shrink: 0;
    transition: transform 0.15s;
}

.aft-circle-num {
    font-size: 1.2rem;
    line-height: 1;
}

/* Active unfactored composite — amber pulse */
.aft-circle.aft-active {
    background:  var(--aft-amber-bg);
    color:       var(--aft-amber);
    border-color:var(--aft-amber-bd);
    animation:   aft-pulse 2s ease-in-out infinite;
}

@keyframes aft-pulse {
    0%,100% { box-shadow: 0 0 0 0    rgba(217,119,6,0.35); }
    50%      { box-shadow: 0 0 0 10px rgba(217,119,6,0);    }
}

/* Factored composite — muted brand blue */
.aft-circle.aft-factored {
    background:   #edf7ea;
    color:        var(--aft-blue-dark);
    border-color: var(--aft-blue-lt);
    animation:    aft-appear 0.35s cubic-bezier(0.34,1.56,0.64,1) both;
}

/* Prime leaf — green */
.aft-circle.aft-prime {
    background:   var(--aft-green-bg);
    color:        var(--aft-green);
    border-color: var(--aft-green-bd);
    animation:    aft-appear 0.35s cubic-bezier(0.34,1.56,0.64,1) both;
}

.aft-prime-badge {
    font-size: 0.5rem;
    font-weight: 700;
    letter-spacing: 0.07em;
    text-transform: uppercase;
    background: var(--aft-green);
    color: #fff;
    border-radius: 20px;
    padding: 1px 5px;
    margin-top: 3px;
    line-height: 1.5;
}

@keyframes aft-appear {
    from { transform: scale(0.45); opacity: 0; }
    to   { transform: scale(1);    opacity: 1; }
}

/* ── Two-slot drop zone row ──────────────────────────────────────── */
.aft-dz-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 52px;       /* matches .aft-children gap */
}

.aft-dz-sep {
    font-size: 1.3rem;
    font-weight: 900;
    color: #94a3b8;
    line-height: 1;
    flex-shrink: 0;
}

/* ── Drop zone circles ───────────────────────────────────────────── */
.aft-dropzone {
    width:  var(--aft-dz);
    height: var(--aft-dz);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.14s, border-color 0.14s, transform 0.14s, box-shadow 0.14s;
    box-sizing: border-box;
}

/* Empty slot — dashed amber */
.aft-dropzone.aft-dz-empty {
    background:   var(--aft-amber-bg);
    border:       2.5px dashed var(--aft-amber-bd);
    cursor: default;
}

.aft-dz-hint {
    font-size: 1.4rem;
    font-weight: 900;
    color: var(--aft-amber-bd);
    pointer-events: none;
    line-height: 1;
}

/* Filled slot — one factor placed, waiting for second */
.aft-dropzone.aft-dz-filled {
    background:   #edf7ea;
    border:       2.5px solid var(--aft-blue-lt);
    color:        var(--aft-blue-dark);
    font-size:    1.15rem;
    font-weight:  800;
    cursor: default;
}

/* Hover (dragging over) — neutral */
.aft-dropzone.aft-dz-hover {
    transform:  scale(1.1);
    box-shadow: 0 0 0 4px rgba(37,99,235,0.15);
}

/* Hover valid — green */
.aft-dropzone.aft-dz-valid {
    background:   var(--aft-green-bg);
    border-color: var(--aft-green) !important;
    border-style: solid !important;
    box-shadow:   0 0 0 4px rgba(22,163,74,0.2);
}
.aft-dropzone.aft-dz-valid .aft-dz-hint { color: var(--aft-green); }

/* Hover invalid — red */
.aft-dropzone.aft-dz-invalid {
    background:   #fee2e2;
    border-color: #ef4444 !important;
    border-style: solid !important;
    box-shadow:   0 0 0 4px rgba(239,68,68,0.15);
}
.aft-dropzone.aft-dz-invalid .aft-dz-hint { color: #dc2626; }

/* Wrong pair — shake both */
.aft-dropzone.aft-dz-wrong {
    background:   #fee2e2 !important;
    border:       2.5px solid #ef4444 !important;
    color:        #dc2626 !important;
    animation:    aft-shake 0.55s ease both;
}

@keyframes aft-shake {
    0%,100% { transform: translateX(0); }
    18%     { transform: translateX(-7px) rotate(-4deg); }
    36%     { transform: translateX(7px)  rotate(4deg); }
    54%     { transform: translateX(-5px) rotate(-2deg); }
    72%     { transform: translateX(5px)  rotate(2deg); }
}

/* ── Tile bank (right panel — mirrors multiplication widget) ──────── */
.aft-bank-wrap {
    flex-shrink: 0;
    width: 200px;
    border-left: 1px solid #e2e8f0;
    background: #fff;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    padding: 14px 10px 10px;
    gap: 4px;
}

.aft-bank-label {
    font-size: 0.72rem;
    font-weight: 800;
    color: #475569;
    text-transform: uppercase;
    letter-spacing: 0.07em;
    flex-shrink: 0;
}

.aft-bank-sub {
    font-size: 0.67rem;
    color: #94a3b8;
    margin-bottom: 8px;
    line-height: 1.4;
    flex-shrink: 0;
}

.aft-bank {
    flex: 1;
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    overflow-y: auto;
    align-content: flex-start;
    padding: 2px 1px;
}
.aft-bank::-webkit-scrollbar { width: 5px; }
.aft-bank::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 4px; }

/* ── Tiles — same size/style as multiplication widget tiles ─────── */
.aft-tile {
    width:   var(--aft-tile);
    height:  var(--aft-tile);
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 9px;
    background:   #edf7ea;
    color:        var(--aft-blue-dark);
    border:       2px solid var(--aft-blue-lt);
    font-size:    1.05rem;
    font-weight:  800;
    cursor:       grab;
    touch-action: none;
    transition:   transform 0.12s, box-shadow 0.12s, opacity 0.15s;
    box-shadow:   0 2px 5px rgba(37,99,235,0.1);
    flex-shrink:  0;
    box-sizing:   border-box;
}

.aft-tile:hover {
    transform:  translateY(-3px) scale(1.07);
    box-shadow: 0 6px 18px rgba(37,99,235,0.22);
}

.aft-tile.aft-dragging {
    opacity: 0.2;
    cursor: grabbing;
    transform: scale(0.9);
}

/* ── Ghost (floating clone) ──────────────────────────────────────── */
.aft-ghost {
    position:      fixed;
    z-index:       9999;
    pointer-events:none;
    touch-action:  none;
    border-radius: 9px;
    background:    #edf7ea;
    color:         var(--aft-blue-dark);
    border:        2px solid var(--aft-blue-lt);
    display:       flex;
    align-items:   center;
    justify-content: center;
    font-size:     1.05rem;
    font-weight:   800;
    font-family:   inherit;
    transform:     rotate(7deg) scale(1.2);
    box-shadow:    0 12px 32px rgba(0,0,0,0.28);
    transition:    none !important;
    box-sizing:    border-box;
}

/* ── Victory overlay ─────────────────────────────────────────────── */
.aft-victory {
    position:  absolute;
    inset:     0;
    background:rgba(15,23,42,0.75);
    display:   none;
    align-items: center;
    justify-content: center;
    z-index:   200;
    border-radius: 18px;
    backdrop-filter: blur(4px);
}
.aft-victory.aft-show { display: flex; animation: aft-fadein 0.4s ease; }
@keyframes aft-fadein { from { opacity: 0; } to { opacity: 1; } }

.aft-victory-box {
    background:    #fff;
    border-radius: 22px;
    padding:       44px 52px;
    text-align:    center;
    box-shadow:    0 24px 64px rgba(0,0,0,0.3);
    animation:     aft-slidein 0.42s cubic-bezier(0.34,1.56,0.64,1) both;
}
@keyframes aft-slidein {
    from { transform: scale(0.65) translateY(40px); opacity: 0; }
    to   { transform: scale(1)    translateY(0);    opacity: 1; }
}

.aft-victory-emoji  { font-size: 3rem; line-height: 1; margin-bottom: 10px; }
.aft-victory-title  { font-size: 2rem; font-weight: 900; color: var(--aft-blue-dark); margin-bottom: 8px; letter-spacing: -0.03em; }

/* ── Stars ───────────────────────────────────────────────────────── */
.aft-victory-stars {
    font-size: 2.6rem;
    line-height: 1;
    margin-bottom: 14px;
    letter-spacing: 4px;
}
.aft-star-on  { color: #fddd54; text-shadow: 0 2px 6px rgba(180,130,0,0.35); }
.aft-star-off { color: #d1d5db; }

/* ── Scorecard ───────────────────────────────────────────────────── */
.aft-scorecard {
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 12px;
    padding: 14px 20px;
    margin-bottom: 24px;
    min-width: 200px;
    text-align: left;
}
.aft-sc-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 5px 0;
    border-bottom: 1px solid #f1f5f9;
    font-size: 0.88rem;
}
.aft-sc-row:last-child { border-bottom: none; }
.aft-sc-lbl { color: #64748b; font-weight: 600; }
.aft-sc-val { color: var(--aft-blue-dark); font-weight: 800; }

/* ── Error counter in stats bar ──────────────────────────────────── */
.aft-errors-txt { font-variant-numeric: tabular-nums; }

/* ── Stats bar ───────────────────────────────────────────────────── */
.aft-stats {
    display: flex;
    align-items: center;
    gap: 5px;
    font-size: 0.86rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    background: rgba(0,0,0,0.18);
    border-radius: 8px;
    padding: 3px 11px;
}

.aft-victory-equation {
    font-size:   1.4rem;
    font-weight: 800;
    color:       var(--aft-green);
    background:  var(--aft-green-bg);
    border:      2px solid var(--aft-green-bd);
    border-radius: 12px;
    padding:     10px 24px;
    margin-bottom: 28px;
    display:     inline-block;
    letter-spacing: -0.01em;
}

.aft-victory .aft-btn {
    background:   var(--aft-blue);
    border-color: var(--aft-blue-dark);
    font-size:    1rem;
    padding:      13px 36px;
    border-radius:12px;
}
.aft-victory .aft-btn:hover { background: var(--aft-blue-dark); }

/* ── Confetti ────────────────────────────────────────────────────── */
.aft-confetti {
    position:  absolute;
    top:       -20px;
    z-index:   199;
    pointer-events: none;
    animation: aft-fall linear forwards;
}
@keyframes aft-fall {
    0%   { top: -20px; opacity: 1; transform: rotate(0deg)   translateX(0); }
    80%  {             opacity: 1; }
    100% { top: 110%;  opacity: 0; transform: rotate(600deg) translateX(120px); }
}

/* ── Responsive ──────────────────────────────────────────────────── */
@media (max-width: 680px) {
    .aft-body { flex-direction: column; }

    .aft-bank-wrap {
        width: 100%;
        border-left: none;
        border-top: 1px solid #e2e8f0;
        padding: 10px 12px;
        max-height: 140px;
    }
    .aft-bank-sub { display: none; }
    .aft-bank     { max-height: 90px; }

    .ama-factor-tree-widget {
        --aft-node: 58px;
        --aft-dz:   54px;
        --aft-tile: 46px;
        min-height: 480px;
    }

    .aft-circle-num { font-size: 1rem; }
    .aft-children   { gap: 20px; margin-top: 40px; }
    .aft-dz-row     { margin-top: 40px; gap: 6px; }
    .aft-tree-content { padding: 20px 24px 28px; }
    .aft-header     { flex-direction: column; align-items: flex-start; }
    .aft-victory-box{ padding: 28px 22px; }
    .aft-victory-title { font-size: 1.6rem; }
}

/* Hint row */
.aft-hint-row {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 20px 14px;
    border-top: 1px solid #f0f0f0;
    flex-shrink: 0;
}
.aft-feedback { min-height: 20px; font-size: .9rem; font-weight: 600; }
.aft-feedback-success { color: var(--aft-green); }
.aft-feedback-error   { color: #f53a33; }

/* Hint button */
.aft-btn-hint { background: #fff8dc; color: #5a3e00; border: 1.5px solid #fddd54; font-size: .78rem; }
.aft-btn-hint:hover { background: #fddd54; filter: none; }
.aft-btn-hint:disabled { opacity: .45; cursor: default; }

/* New button — bright green, consistent across all widgets */
.aft-btn-new {
    background: #16a34a !important;
    color: #fff !important;
    border: none !important;
    font-weight: 700;
}
.aft-btn-new:hover { background: #15803d !important; filter: none !important; }

