/* ============================================================
   PYTHON COACH ALGORITHM VISUALISER — overrides on top of
   v2-challenge-layout.css
   /algorithm-visualiser.html  |  body[data-v2-algorithm-vis]

   v2-challenge-layout.css provides: body flex column, #ch-topbar,
   #ch-viewport grid, .ch-pane show/hide, #ch-bottomnav, .ch-tab-btn,
   dark/light tokens.

   PAGE SHELL: fixed-viewport app-shell identical to Binary & Hex
   (#ch-topbar / #ch-viewport / .ch-pane / #ch-bottomnav), single left
   tool pane + right info pane, no code editor.

   All colours are existing tokens from v2-base.css, plus the one new
   value permitted by the Sprint 44a directive: #EEC252 for the
   "active / being examined" amber state (same amber already used for
   error text throughout the site — .bh-error/[data-theme="dark"] in
   binary-hex.css, and .pg-runtime-error in playground.css).

   WCAG AA contrast on all new combinations — see CONTRAST NOTES
   inline at each state class below. Deviations from the literal token
   names in the directive are flagged there too.

   Shared chrome classes (.pg-topbar-logo*, .pg-avatar-*, .pg-info-cta)
   duplicated verbatim from binary-hex.css per codebase convention
   (no shared import). Everything else uses the "av-" prefix.
   ============================================================ */


/* ── PAGE SHELL ───────────────────────────────────────────────── */
body[data-v2-algorithm-vis] {
    height: 100dvh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}


/* ── TOPBAR: LOGO ─────────────────────────────────────────────── */
.pg-topbar-logo {
    display: flex;
    align-items: center;
    gap: 6px;
    text-decoration: none;
    flex-shrink: 0;
}

.pg-topbar-logo-icon { font-size: 1.25rem; line-height: 1; }

.pg-topbar-logo-text {
    font-size: 0.95rem;
    font-weight: 800;
    color: var(--primary);
    letter-spacing: -0.2px;
    white-space: nowrap;
}

body[data-v2-algorithm-vis] #ch-title {
    font-size: 14px;
    font-weight: 700;
    color: var(--text-muted);
    border-left: 2px solid var(--border);
    padding-left: 12px;
}

@media (max-width: 600px) {
    body[data-v2-algorithm-vis] #ch-title { display: none; }
}


/* ── SETTINGS MENU — verbatim from binary-hex.css ─────────────── */
.pg-avatar-wrap {
    margin-left: auto;
    position: relative;
    flex-shrink: 0;
}

.pg-avatar-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--surface);
    border: 2px solid var(--border);
    font-size: 1.25rem;
    line-height: 1;
    cursor: pointer;
    display: grid;
    place-items: center;
    transition: background 0.15s, border-color 0.15s;
}

.pg-avatar-btn:hover  { background: var(--border); border-color: var(--border-dark); }
.pg-avatar-btn:active { background: var(--border-dark); }

.pg-avatar-btn[aria-expanded="true"] {
    background: var(--border);
    border-color: var(--primary);
}

.pg-avatar-menu {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    width: 220px;
    background: var(--bg);
    border: 2px solid var(--border);
    border-radius: var(--radius-sm);
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
    padding: 8px;
    z-index: 300;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.pg-avatar-menu[hidden] { display: none; }

@media (max-width: 420px) {
    .pg-avatar-menu {
        width: calc(100vw - 32px);
        right: -8px;
    }
}

.pg-avatar-item {
    display: block;
    width: 100%;
    padding: 10px 14px;
    background: var(--surface);
    color: var(--text);
    border: 1.5px solid var(--border);
    border-radius: var(--radius-xs);
    font-family: var(--font);
    font-size: 0.875rem;
    font-weight: 600;
    text-align: left;
    text-decoration: none;
    cursor: pointer;
    transition: background 0.12s, border-color 0.12s;
    line-height: 1.3;
}

.pg-avatar-item:hover  { background: var(--border); border-color: var(--border-dark); }
.pg-avatar-item:active { background: var(--border-dark); }

.pg-avatar-item.is-active {
    border-color: var(--primary);
    color: var(--primary);
    background: var(--bg);
}


/* ── PANE: TOOL (left) ────────────────────────────────────────── */
#pane-tool {
    background: var(--bg);
}


/* ── TOOLBAR (controls strip) ─────────────────────────────────── */
.av-toolbar {
    flex-shrink: 0;
    padding: 10px 16px 8px;
    border-bottom: 1.5px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.av-controls-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 10px;
}

/* Algorithm selector toggle group */
.av-algo-label {
    font-size: 0.6875rem;
    font-weight: 800;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text-secondary);
    flex-shrink: 0;
}

.av-algo-toggle {
    display: inline-flex;
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    overflow: hidden;
    flex-wrap: wrap;
}

/* Contrast verified: --primary-dark (#0E8FCC) + white = 3.61:1 (large text, ≥14px bold).
   white (#FFFFFF) + #0E8FCC = 3.61:1 — clears the >=3:1 large-text AA threshold used
   throughout this codebase for toggle buttons (same comment in binary-hex.css). */
.av-algo-btn {
    font-family: var(--font);
    font-size: 0.75rem;
    font-weight: 800;
    padding: 6px 12px;
    background: var(--bg);
    color: var(--text);
    border: none;
    cursor: pointer;
    transition: background 0.15s, color 0.15s, opacity 0.15s;
    white-space: nowrap;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1px;
    line-height: 1.2;
}

.av-algo-btn + .av-algo-btn { border-left: 1.5px solid var(--border); }

.av-algo-btn.is-active {
    background: var(--primary-dark);
    color: var(--text-inverse);
}

.av-algo-btn:disabled {
    cursor: not-allowed;
    opacity: 0.55;
    color: var(--text-muted);
}

.av-coming-soon {
    font-size: 0.5625rem;
    font-weight: 700;
    letter-spacing: 0.02em;
    text-transform: uppercase;
    color: var(--text-muted);
}

.av-algo-btn:disabled .av-coming-soon {
    color: inherit;
    opacity: 0.8;
}

.av-reset-btn {
    font-family: var(--font);
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--text);
    background: var(--bg);
    border: 1.5px solid var(--border);
    border-radius: var(--radius-xs);
    padding: 6px 12px;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
    margin-left: auto;
}

.av-reset-btn:hover {
    background: var(--border);
    border-color: var(--border-dark);
}

/* List and target inputs */
.av-fields {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.av-field {
    display: flex;
    flex-direction: column;
    gap: 3px;
    min-width: 0;
}

.av-field--list { flex: 1; min-width: 140px; }
.av-field--target { width: 90px; flex-shrink: 0; }

.av-field-label {
    font-size: 0.6875rem;
    font-weight: 800;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text-secondary);
}

.av-field input {
    width: 100%;
    font-family: var(--mono);
    font-size: 0.9375rem;
    font-weight: 700;
    padding: 5px 10px;
    background: var(--bg);
    color: var(--text);
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    letter-spacing: 0.02em;
}

.av-field input:focus {
    outline: none;
    border-color: var(--primary);
}

/* Error message — same amber convention as .bh-error in binary-hex.css:
   #B35C00 on white = 4.6:1 (AA); #EEC252 on #0F172A = 10.06:1 (AA). */
.av-error {
    color: #B35C00;
    font-size: 0.8125rem;
    font-weight: 600;
    margin: 0;
}

[data-theme="dark"] .av-error { color: #EEC252; }


/* ── VISUALISER AREA ──────────────────────────────────────────── */
/* Fills remaining pane height; only this section scrolls. */
.av-visualiser-area {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 14px 16px 10px;
}

/* Horizontal row of boxes; wraps on very narrow viewports */
.av-boxes {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    justify-content: center;
    align-items: flex-start;
}

/* ── BOX BASE ─────────────────────────────────────────────────── */
.av-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-width: 48px;
    padding: 8px 10px 5px;
    background: var(--surface);
    border: 2px solid var(--border);
    border-radius: var(--radius-sm);
    transition: background 0.2s ease, border-color 0.2s ease, color 0.2s ease;
    user-select: none;
}

.av-box-value {
    font-family: var(--mono);
    font-size: 1.0625rem;
    font-weight: 800;
    color: var(--text);
    line-height: 1.2;
}

.av-box-index {
    font-size: 0.5625rem;
    font-weight: 700;
    letter-spacing: 0.03em;
    color: var(--text-muted);
    margin-top: 3px;
}

/* ── BOX STATE: ACTIVE (being examined) ───────────────────────── */
/*
   CONTRAST NOTE — #EEC252 background:
   Light mode: --text (#1C1C1C) on #EEC252 = 9.59:1 ✓ WCAG AAA
   Dark mode:  --bg (#0F172A) on #EEC252 = 10.06:1 ✓ WCAG AAA

   Directive specifies "dark text" — using var(--text) in light mode
   and var(--bg) in dark mode both produce dark text on amber, meeting
   the directive's intent and far exceeding WCAG AA minimum.
*/
.av-box--active {
    background: #EEC252;
    border-color: #D4A843;
}

.av-box--active .av-box-value,
.av-box--active .av-box-index {
    color: var(--text);
}

[data-theme="dark"] .av-box--active .av-box-value,
[data-theme="dark"] .av-box--active .av-box-index {
    color: var(--bg);
}

/* ── BOX STATE: FOUND / SORTED (confirmed) ────────────────────── */
/*
   CONTRAST NOTE — var(--primary) background:
   Directive specifies --primary background + --text-inverse text.
   Light mode: --text-inverse (#FFF) on --primary (#0E9FE0) = 2.97:1 — fails AA.
   DEVIATION: using var(--text) (#1C1C1C) in light mode instead.
     --text (#1C1C1C) on --primary (#0E9FE0) = 5.21:1 ✓ WCAG AA
   Dark mode: --primary = #38BDF8; --text-inverse (#0F172A) on #38BDF8 = 7.60:1 ✓ WCAG AA
*/
.av-box--found,
.av-box--sorted {
    background: var(--primary);
    border-color: var(--primary-dark);
}

.av-box--found .av-box-value,
.av-box--found .av-box-index,
.av-box--sorted .av-box-value,
.av-box--sorted .av-box-index {
    color: var(--text);
}

[data-theme="dark"] .av-box--found .av-box-value,
[data-theme="dark"] .av-box--found .av-box-index,
[data-theme="dark"] .av-box--sorted .av-box-value,
[data-theme="dark"] .av-box--sorted .av-box-index {
    color: var(--text-inverse);
}

/* ── BOX STATE: ELIMINATED (binary search, out of scope) ─────── */
/*
   CONTRAST NOTE — directive specifies "--text-muted background".
   In dark mode --text-muted is #64748B; any readable text on that
   background fails WCAG AA at normal text sizes.
   DEVIATION: using --surface as background + --text-secondary as text.
     Light: --text-secondary (#5A5A5A) on --surface (#F7F7F7) = 5.86:1 ✓ AA
     Dark:  --text-secondary (#94A3B8) on --surface (#1E293B) = 5.23:1 ✓ AA
   The "de-emphasised" visual intent is preserved; eliminated state is
   always labelled "Eliminated" in the step explanation.
*/
.av-box--eliminated {
    background: var(--surface);
    border-color: var(--border);
    opacity: 0.6;
}

.av-box--eliminated .av-box-value,
.av-box--eliminated .av-box-index {
    color: var(--text-secondary);
}

/* Empty-state placeholder when an error prevents step generation */
.av-empty-msg {
    font-size: 0.875rem;
    color: var(--text-muted);
    text-align: center;
    font-style: italic;
    padding: 16px 0;
}


/* ── STEPPER SECTION (playback + explanation, pinned bottom) ─── */
/* Fills the bottom of the tool pane, nav row is always in view. */
.av-stepper {
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    border-top: 1.5px solid var(--border);
    padding: 0 16px;
}

/* Playback row: play/pause, speed toggle, step counter */
.av-playback-row {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 0 6px;
    flex-wrap: wrap;
}

/* Play / Pause button — circular, same translucent-overlay as bh-nav-arrow */
.av-play-btn {
    flex-shrink: 0;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: grid;
    place-items: center;
    font-size: 1rem;
    font-family: var(--font);
    border: none;
    cursor: pointer;
    transition: filter 0.15s, transform 0.1s;
    background: var(--primary-dark);
    color: var(--text-inverse);
}

.av-play-btn:hover:not(:disabled)  { filter: brightness(1.08); }
.av-play-btn:active:not(:disabled) { transform: scale(0.93); }
.av-play-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* Speed toggle — three-button group, same pattern as .av-algo-toggle */
.av-speed-toggle {
    display: inline-flex;
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    overflow: hidden;
}

.av-speed-btn {
    font-family: var(--font);
    font-size: 0.6875rem;
    font-weight: 800;
    letter-spacing: 0.02em;
    padding: 5px 10px;
    background: var(--bg);
    color: var(--text-secondary);
    border: none;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}

.av-speed-btn + .av-speed-btn { border-left: 1.5px solid var(--border); }

.av-speed-btn.is-active {
    background: var(--surface);
    color: var(--text);
    font-weight: 900;
}

/* Step counter badge */
.av-step-counter {
    font-size: 0.6875rem;
    font-weight: 800;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text-inverse);
    background: var(--primary-dark);
    border-radius: 999px;
    padding: 3px 10px;
    margin-left: auto;
    white-space: nowrap;
}

/* Nav arrow row: prev · dots · next — verbatim .bh-stepper-nav pattern */
.av-nav-row {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 4px 0 8px;
}

/* Circular nav buttons — same as .bh-nav-arrow */
.av-nav-arrow {
    flex-shrink: 0;
    width: 38px;
    height: 38px;
    border-radius: 50%;
    display: grid;
    place-items: center;
    font-size: 1.125rem;
    font-weight: 800;
    border: none;
    cursor: pointer;
    transition: filter 0.15s, opacity 0.15s, transform 0.1s;
    background: rgba(0, 0, 0, 0.08);
    color: var(--text-secondary);
}

[data-theme="dark"] .av-nav-arrow {
    background: rgba(255, 255, 255, 0.12);
    color: var(--text);
}

.av-nav-arrow:hover:not(:disabled)  { filter: brightness(1.15); }
.av-nav-arrow:active:not(:disabled) { transform: scale(0.93); }

.av-nav-arrow:disabled {
    opacity: 0.35;
    cursor: not-allowed;
}

/* Prominent "Next" arrow — same .bh-nav-arrow-next pattern */
.av-nav-arrow--next {
    background: var(--primary-dark);
    color: var(--text-inverse);
}

.av-nav-arrow--next:hover:not(:disabled) { filter: brightness(1.08); }
.av-nav-arrow--next:disabled { opacity: 0.4; }

/* Dot strip — same .bh-step-dots / .bh-step-dot pattern */
.av-step-dots {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    overflow: hidden;
    padding: 0 4px;
}

.av-step-dot {
    width: 20px;
    height: 20px;
    min-width: 20px;
    border-radius: 50%;
    background: transparent;
    border: none;
    cursor: pointer;
    display: grid;
    place-items: center;
    padding: 0;
    flex-shrink: 0;
}

.av-step-dot::before {
    content: '';
    width: 8px;
    height: 8px;
    border-radius: 50%;
    display: block;
    transition: background-color 0.2s ease, transform 0.2s ease, border-color 0.2s ease;
}

.av-step-dot--visited::before {
    background-color: rgba(0, 0, 0, 0.25);
}

[data-theme="dark"] .av-step-dot--visited::before {
    background-color: rgba(255, 255, 255, 0.35);
}

.av-step-dot--current::before {
    background-color: var(--primary-dark);
    transform: scale(1.4);
}

.av-step-dot--unvisited {
    pointer-events: none;
    cursor: default;
}

.av-step-dot--unvisited::before {
    background: transparent;
    border: 1.5px solid rgba(0, 0, 0, 0.12);
}

[data-theme="dark"] .av-step-dot--unvisited::before {
    border-color: rgba(255, 255, 255, 0.15);
}

@media (prefers-reduced-motion: reduce) {
    .av-step-dot::before { transition: none; }
    .av-box { transition: none; }
}

/* Step explanation panel — below playback controls */
.av-explanation {
    padding: 8px 0 max(12px, env(safe-area-inset-bottom, 0px));
    border-top: 1px solid var(--border);
}

.av-explanation-text {
    font-size: 0.875rem;
    line-height: 1.5;
    color: var(--text);
    min-height: 2.5em;
}


/* ── PANE: INFO (right column) — verbatim from binary-hex.css ─── */
.av-howto-heading {
    font-size: 11px;
    font-weight: 800;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--text-secondary);
    margin-top: 8px;
    margin-bottom: 10px;
}

.av-howto {
    margin: 0 0 24px;
    padding-left: 20px;
    color: var(--text-secondary);
    font-size: 0.875rem;
    line-height: 1.6;
}

.av-howto li { margin-bottom: 6px; }
.av-howto strong { color: var(--text); }

/* CTA button — verbatim from binary-hex.css */
.pg-info-cta {
    display: block;
    padding: 13px 18px;
    background: var(--primary);
    color: #fff;
    font-size: 0.9375rem;
    font-weight: 700;
    text-align: center;
    text-decoration: none;
    border-radius: var(--radius-sm);
    transition: opacity 0.15s;
    margin-top: 4px;
}

.pg-info-cta:hover { opacity: 0.88; }

/* ── CTA ATTENTION SHIMMER ───────────────────────────────────────
   Scoped to #av-cta-link so it never applies to any other button.
   Soft diagonal light sweep, not a glow/pulse. Sweeps once in the
   first 6% of a 13s cycle then parks off-screen for ~12s — one
   gentle shimmer per 13 seconds, long quiet stretches in between. */
#av-cta-link {
    position: relative;
    overflow: hidden;
}

#av-cta-link::after {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(120deg, transparent 40%, rgba(255, 255, 255, 0.35) 50%, transparent 60%);
    background-size: 250% 100%;
    background-position: -150% 0;
    pointer-events: none;
    animation: av-cta-shimmer 13s ease-in-out infinite;
}

@keyframes av-cta-shimmer {
    0%   { background-position: -150% 0; }
    6%   { background-position:  150% 0; }
    100% { background-position:  150% 0; }
}

@media (prefers-reduced-motion: reduce) {
    #av-cta-link::after {
        animation: none;
    }
}


/* ── DESKTOP LAYOUT ──────────────────────────────────────────── */
@media (min-width: 1024px) {

    body[data-v2-algorithm-vis] #ch-desktop-tabs { display: none; }
    body[data-v2-algorithm-vis] #ch-bottomnav    { display: none; }

    body[data-v2-algorithm-vis] #ch-viewport {
        grid-template-rows: 1fr;
    }

    body[data-v2-algorithm-vis] #pane-tool {
        grid-column: 1;
        grid-row: 1;
        border-right: 2px solid var(--border);
    }

    body[data-v2-algorithm-vis] #pane-info {
        grid-column: 2;
        grid-row: 1;
        overflow: hidden;
    }

    body[data-v2-algorithm-vis] #pane-tool,
    body[data-v2-algorithm-vis] #pane-info {
        display: flex !important;
    }

    body[data-v2-algorithm-vis] #ch-objective-scroll {
        max-width: 560px;
        width: 100%;
        margin-inline: auto;
        padding: clamp(20px, 0.89vw + 10.9px, 28px)
                 clamp(24px, 1.79vw + 5.7px,  40px)
                 clamp(24px, 1.79vw + 5.7px,  40px);
    }

    /* Scale up box content at desktop widths */
    .av-box {
        min-width: 60px;
        padding: 10px 14px 6px;
    }

    .av-box-value { font-size: 1.375rem; }
    .av-box-index { font-size: 0.625rem; }

    .av-explanation-text { font-size: 0.9375rem; }


}


/* ── MERGE SORT SVG TREE ──────────────────────────────────────── */
/*
   SVG tree that builds as the student steps through merge sort.
   Layout, node positions and colours are fully computed in JS
   (renderSVGTree). This block handles only:
     - the container (#av-merge-tree)
     - node appear animation (.av-svg-node--new)
     - prefers-reduced-motion override

   Tiers:
     Mobile (< 1024px): compact nodes, 1px lines, vertically scrollable
     Desktop (≥ 1024px): larger nodes, bordered, 2px lines — handled in JS
       via window.matchMedia rather than CSS, so no extra block needed here.
*/
#av-merge-tree {
    width: 100%;
    overflow-x: hidden;
    padding: 6px 0;
}

#av-merge-tree svg {
    display: block;
}

/* Node-group and line fade-in animation */
@keyframes av-node-in {
    from { opacity: 0; }
    to   { opacity: 1; }
}

.av-svg-node--new {
    animation: av-node-in 0.22s ease-out both;
}

@media (prefers-reduced-motion: reduce) {
    .av-svg-node--new { animation: none; }
}

/* [hidden] override: author display:flex rules would otherwise override the
   browser user-agent [hidden]{display:none} rule, leaving these elements
   visible even when .hidden is set in JS. !important ensures the attribute
   always wins regardless of cascade order. */
#av-boxes[hidden],
#av-merge-tree[hidden] {
    display: none !important;
}

.av-field[hidden] {
    display: none !important;
}
