<!DOCTYPE html>
<html lang="en" class="dark">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>TernTranslator | Local Code Migration Tool</title>
  <script src="https://cdn.tailwindcss.com"></script>
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600;700&family=JetBrains+Mono:wght@400;700&display=swap" rel="stylesheet">
  <script>
    tailwind.config = {
      darkMode: 'class',
      theme: {
        extend: {
          colors: {
            studio_bg: 'var(--bg-canvas)',       
            studio_panel: 'var(--bg-panel)',      
            studio_border: 'var(--border-color)',    
            studio_editor: 'var(--bg-editor)',   
            t_text: 'var(--text-main)',
            t_subtext: 'var(--text-sub)',
            t_teal: '#00d4aa',
            t_teal_dim: 'var(--teal-dim)',
            t_blue: '#4a9eff',
          },
          fontFamily: {
            sans: ['Inter', 'sans-serif'],
            mono: ['JetBrains Mono', 'monospace'],
          }
        }
      }
    }
  </script>
  <style>
    :root {
      /* State -1: Light Mode */
      --bg-canvas: #F8FAFC;
      --bg-panel: #FFFFFF;
      --border-color: #CBD5E1;
      --bg-editor: #F1F5F9;
      --text-main: #0F172A;
      --text-sub: #1E293B; 
      --teal-dim: rgba(0, 212, 170, 0.15);
    }
    
    .dark {
      /* State +1: Dark Mode */
      --bg-canvas: #0C1322;
      --bg-panel: #111622;
      --border-color: #1E2536;
      --bg-editor: #05070A;
      --text-main: #FFFFFF;
      --text-sub: #F8FAFC; 
      --teal-dim: rgba(0, 212, 170, 0.15);
    }

    body {
      background-color: var(--bg-canvas);
      background-image: radial-gradient(var(--border-color) 1.5px, transparent 1.5px);
      background-size: 24px 24px;
      color: var(--text-main);
      transition: background-color 0.2s ease, color 0.2s ease;
    }
    
    .glass-panel {
      background: var(--bg-panel);
      border: 1px solid var(--border-color);
      box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.3);
      transition: background-color 0.2s ease, border-color 0.2s ease;
    }

    .editor-pane {
      background: var(--bg-editor);
      box-shadow: inset 0 2px 15px rgba(0, 0, 0, 0.1);
      transition: background-color 0.2s ease;
    }
    .dark .editor-pane {
      box-shadow: inset 0 2px 15px rgba(0, 0, 0, 0.8);
    }

    textarea:focus { outline: none; }
    
    /* Strict scrollbar enforcement */
    ::-webkit-scrollbar { width: 8px; height: 8px; }
    ::-webkit-scrollbar-track { background: transparent; }
    ::-webkit-scrollbar-thumb { background: var(--border-color); border-radius: 4px; }
    ::-webkit-scrollbar-thumb:hover { background: #00d4aa; }
    
    .btn-glow:hover {
      box-shadow: 0 0 15px rgba(0, 212, 170, 0.3);
      background: rgba(0, 212, 170, 0.1);
    }
  </style>
</head>
<body class="antialiased h-screen flex flex-col overflow-hidden p-0">

  <!-- Main Workspace -->
  <div class="glass-panel flex flex-col flex-1 min-h-0">
      
      <!-- Top Toolbar -->
      <div class="flex flex-col md:flex-row border-b border-studio_border bg-studio_panel px-5 py-3 items-center justify-between gap-4" style="flex-shrink:0;">

        <div class="flex items-center gap-4 w-full md:w-auto flex-wrap">
          <span class="font-mono font-black uppercase tracking-widest text-t_text hidden md:block" style="font-size:13px; letter-spacing:0.07em;">TERNTRANSLATOR</span>
          <span class="text-t_subtext hidden md:block" style="font-size:11px;">— binary → balanced ternary</span>
          <div class="hidden md:block" style="width:1px; height:16px; background:var(--border-color);"></div>
          <div class="flex items-center gap-2">
            <div class="w-2 h-2 rounded-full bg-t_teal animate-pulse"></div>
            <span id="mode-label" class="text-t_text font-mono text-[13px] uppercase tracking-widest min-w-[180px]">Python → TernLang</span>
          </div>
          <select id="trans-lang" class="bg-studio_editor border border-studio_border text-t_text focus:text-t_teal focus:border-t_teal transition-colors font-mono text-[15px] px-3 py-1.5 rounded-md outline-none cursor-pointer">
            <option value="python">Python</option>
            <option value="rust">Rust</option>
            <option value="java">Java</option>
            <option value="cpp">C++</option>
            <option value="javascript">JavaScript</option>
          </select>
          
          <button id="swap-direction-btn" class="p-1.5 rounded-md border border-studio_border bg-studio_editor text-t_subtext hover:text-t_teal hover:border-t_teal transition-all group" title="Swap Translation Direction">
            <svg class="w-5 h-5 group-hover:rotate-180 transition-transform duration-300" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7h12m0 0l-4-4m4 4l-4 4m0 6H4m0 0l4 4m-4-4l4-4"></path></svg>
          </button>
          
          <div class="h-4 w-px bg-studio_border hidden md:block"></div>
          
          <label class="cursor-pointer flex items-center gap-2 text-t_text hover:text-t_blue transition-colors font-mono text-[15px] group">
            <svg class="w-5 h-5 transition-colors" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-8l-4-4m0 0L8 8m4-4v12"></path></svg>
            <span>Upload</span>
            <input type="file" id="trans-upload" class="hidden" accept=".py,.rs,.java,.cpp,.js,.ts,.html,.tern">
          </label>
        </div>

        <div class="flex items-center gap-4 w-full md:w-auto">
          <button id="trans-run-btn" class="flex-1 md:flex-none flex items-center justify-center gap-2 px-6 py-2 border border-t_teal/50 text-t_teal font-mono text-[15px] font-bold uppercase tracking-wider rounded-md transition-all btn-glow bg-t_teal_dim">
            <svg class="w-5 h-5 shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"></path></svg>
            <span id="trans-run-text">Translate</span>
          </button>
        </div>
      </div>

      <!-- Code Areas -->
      <div class="grid md:grid-cols-2 divide-y md:divide-y-0 md:divide-x divide-studio_border min-h-0 flex-1">
        
        <!-- Input -->
        <div class="flex flex-col relative group min-h-0">
          <div id="input-label" class="absolute top-3 right-4 font-mono text-[13px] text-t_subtext uppercase tracking-widest z-10 select-none bg-studio_editor/80 px-2 py-1 rounded border border-studio_border/50">Input // Binary</div>
          <textarea id="trans-input" class="editor-pane flex-1 w-full p-5 pt-12 font-mono text-[17px] text-t_subtext resize-none overflow-y-auto block" spellcheck="false" placeholder="// Paste architecture logic here..."></textarea>
        </div>
        
        <!-- Output -->
        <div class="flex flex-col relative min-h-0">
          <div id="output-label" class="absolute top-3 right-4 font-mono text-[13px] text-t_teal uppercase tracking-widest z-10 select-none bg-studio_editor/80 px-2 py-1 rounded border border-studio_border/50">Output // Ternary</div>
          <div id="trans-output" class="editor-pane flex-1 w-full p-5 pt-12 font-mono text-[17px] text-t_teal overflow-y-auto whitespace-pre block"></div>
        </div>
        
      </div>

      <!-- Insight Footer -->
      <div class="bg-studio_panel border-t border-studio_border p-5 shrink-0">
        <div class="flex items-center gap-2 mb-3">
          <svg class="w-4 h-4 text-t_blue" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
          <h4 class="text-t_text font-mono text-[13px] uppercase tracking-widest">Topology Insights</h4>
        </div>
        <div id="trans-insight" class="flex flex-wrap gap-2 text-[15px] text-t_subtext">
          <div class="px-3 py-1.5 bg-studio_editor border border-studio_border rounded font-mono">Awaiting logic vectors...</div>
        </div>
      </div>
    </div>


  <script>
    let isTernToBinary = false;

    const htmlElement = document.documentElement;
    const transInput  = document.getElementById('trans-input');
    const transOutput = document.getElementById('trans-output');
    const transInsight = document.getElementById('trans-insight');
    const transBtn    = document.getElementById('trans-run-btn');
    const transUpload = document.getElementById('trans-upload');
    const swapBtn     = document.getElementById('swap-direction-btn');
    const transLang   = document.getElementById('trans-lang');
    const modeLabel   = document.getElementById('mode-label');
    const inputLabel  = document.getElementById('input-label');
    const outputLabel = document.getElementById('output-label');

    let apiKey = '';
    window.addEventListener('message', (event) => {
      if (event.data.type === 'TIS_AUTH_BRIDGE') apiKey = event.data.key;
      if (event.data.type === 'TIS_THEME') {
        const isDark = event.data.mode !== 'light';
        if (isDark) htmlElement.classList.add('dark');
        else htmlElement.classList.remove('dark');
      }
    });

    const LANG_DISPLAY = { python: 'Python', rust: 'Rust', java: 'Java', cpp: 'C++', javascript: 'JavaScript' };

    function updateDirectionLabels() {
      const langName = LANG_DISPLAY[transLang.value] || transLang.value;
      if (isTernToBinary) {
        modeLabel.textContent = `TernLang → ${langName}`;
        inputLabel.textContent = `Input // TernLang`;
        inputLabel.classList.remove('text-t_subtext'); inputLabel.classList.add('text-t_teal');
        outputLabel.textContent = `Output // ${langName}`;
        outputLabel.classList.remove('text-t_teal'); outputLabel.classList.add('text-t_subtext');
        transOutput.classList.remove('text-t_teal'); transOutput.classList.add('text-t_subtext');
        transInput.classList.remove('text-t_subtext'); transInput.classList.add('text-t_teal');
      } else {
        modeLabel.textContent = `${langName} → TernLang`;
        inputLabel.textContent = `Input // ${langName}`;
        inputLabel.classList.remove('text-t_teal'); inputLabel.classList.add('text-t_subtext');
        outputLabel.textContent = `Output // TernLang`;
        outputLabel.classList.remove('text-t_subtext'); outputLabel.classList.add('text-t_teal');
        transOutput.classList.remove('text-t_subtext'); transOutput.classList.add('text-t_teal');
        transInput.classList.remove('text-t_teal'); transInput.classList.add('text-t_subtext');
      }
    }

    // Language-specific rule sets — forward = source→TernLang, reverse = TernLang→source
    const LANG_RULES = {
      python: {
        forward: [
          { regex: /\bTrue\b/g,          repl: 'affirm',                        note: 'Python True → +1 (affirm)' },
          { regex: /\bFalse\b/g,         repl: 'reject',                        note: 'Python False → -1 (reject)' },
          { regex: /\bNone\b/g,          repl: 'tend',                          note: 'Python None → 0 (tend)' },
          { regex: /\bbool\b/g,          repl: 'trit',                          note: 'bool → trit' },
          { regex: /\bpass\b/g,          repl: 'hold',                          note: 'pass → hold (ternary suspension)' },
          { regex: /\braise\b/g,         repl: 'conflict',                      note: 'raise → conflict (fault signal)' },
          { regex: /\bprint\(/g,         repl: 'println(',                      note: 'print() → println()' },
          { regex: /\bclass\b/g,         repl: 'agent',                         note: 'class → agent' },
          { regex: /\bdef\b/g,           repl: 'fn',                            note: 'def → fn' },
          { regex: /\blambda\b/g,        repl: 'fn',                            note: 'lambda → fn' },
          { regex: /elif (.+?):/g,       repl: '} affirm => { // elif:',        note: 'elif → affirm branch' },
          { regex: /if (.+?):/g,         repl: 'match $1 {',                    note: 'if → match block' },
          { regex: /else:/g,             repl: '} tend => {',                   note: 'else → tend branch' },
          { regex: /\breturn True\b/g,   repl: 'return affirm',                 note: 'return True → return affirm' },
          { regex: /\breturn False\b/g,  repl: 'return reject',                 note: 'return False → return reject' },
          { regex: /\breturn None\b/g,   repl: 'return tend',                   note: 'return None → return tend' },
          { regex: /\band\b/g,           repl: '&&',                            note: 'and → &&' },
          { regex: /\bor\b/g,            repl: '||',                            note: 'or → ||' },
          { regex: /\bnot\b/g,           repl: '!',                             note: 'not → !' },
          { regex: /^from\s+/gm,         repl: '// @from ',                     note: 'from-import → @from annotation' },
          { regex: /^import\s+/gm,       repl: '// @import ',                   note: 'import → @import annotation' },
        ],
        reverse: [
          { regex: /\baffirm\b/g,        repl: 'True',                          note: 'affirm → Python True' },
          { regex: /\breject\b/g,        repl: 'False',                         note: 'reject → Python False' },
          { regex: /\btend\b/g,          repl: 'None',                          note: 'tend → Python None' },
          { regex: /\btrit\b/g,          repl: 'bool',                          note: 'trit → bool' },
          { regex: /\bhold\b/g,          repl: 'pass',                          note: 'hold → pass' },
          { regex: /\bconflict\b/g,      repl: 'raise',                         note: 'conflict → raise' },
          { regex: /\bprintln\(/g,       repl: 'print(',                        note: 'println → print' },
          { regex: /\bagent\b/g,         repl: 'class',                         note: 'agent → class' },
          { regex: /\bfn\b/g,            repl: 'def',                           note: 'fn → def' },
          { regex: /\} affirm => \{ \/\/ elif:/g, repl: 'elif',                note: 'affirm branch → elif' },
          { regex: /match (.+?) \{/g,    repl: 'if $1:',                        note: 'match → if' },
          { regex: /\} tend => \{/g,     repl: 'else:',                         note: 'tend → else' },
          { regex: /\breturn affirm\b/g, repl: 'return True',                   note: 'return affirm → return True' },
          { regex: /\breturn reject\b/g, repl: 'return False',                  note: 'return reject → return False' },
          { regex: /\breturn tend\b/g,   repl: 'return None',                   note: 'return tend → return None' },
          { regex: /&&/g,                repl: 'and',                           note: '&& → and' },
          { regex: /\|\|/g,              repl: 'or',                            note: '|| → or' },
          { regex: /!/g,                 repl: 'not ',                          note: '! → not' },
          { regex: /\/\/ @from /g,       repl: 'from ',                         note: '@from → from' },
          { regex: /\/\/ @import /g,     repl: 'import ',                       note: '@import → import' },
        ]
      },
      rust: {
        forward: [
          { regex: /\btrue\b/g,          repl: 'affirm',                        note: 'Rust true → affirm' },
          { regex: /\bfalse\b/g,         repl: 'reject',                        note: 'Rust false → reject' },
          { regex: /\bNone\b/g,          repl: 'tend',                          note: 'Rust None → tend' },
          { regex: /\bbool\b/g,          repl: 'trit',                          note: 'bool → trit' },
          { regex: /\bOption<bool>/g,    repl: 'trit',                          note: 'Option<bool> → trit' },
          { regex: /\bSome\(true\)/g,    repl: 'affirm',                        note: 'Some(true) → affirm' },
          { regex: /\bSome\(false\)/g,   repl: 'reject',                        note: 'Some(false) → reject' },
          { regex: /\bpanic!\(/g,        repl: 'conflict(',                     note: 'panic! → conflict' },
          { regex: /println!\(/g,        repl: 'println(',                      note: 'println! → println' },
          { regex: /print!\(/g,          repl: 'print(',                        note: 'print! → print' },
          { regex: /\bstruct\b/g,        repl: 'agent',                         note: 'struct → agent' },
          { regex: /\benum\b/g,          repl: 'agent',                         note: 'enum → agent' },
          { regex: /if (.+?) \{/g,       repl: 'match $1 {',                    note: 'if-block → match block' },
          { regex: /\} else \{/g,        repl: '} tend => {',                   note: 'else → tend branch' },
          { regex: /\breturn true;/g,    repl: 'return affirm;',                note: 'return true → return affirm' },
          { regex: /\breturn false;/g,   repl: 'return reject;',                note: 'return false → return reject' },
          { regex: /\breturn None;/g,    repl: 'return tend;',                  note: 'return None → return tend' },
          { regex: /\blet mut\b/g,       repl: 'let',                           note: 'let mut → let (mutability implicit)' },
          { regex: /\bi32\b|\bi64\b|\bu32\b|\bu64\b|\busize\b|\bisize\b/g, repl: 'int', note: 'integer types → int' },
          { regex: /\bf32\b|\bf64\b/g,   repl: 'float',                         note: 'float types → float' },
          { regex: /\bString\b|\b&str\b/g, repl: 'str',                        note: 'String/&str → str' },
          { regex: /^use\s+/gm,          repl: '// @use ',                      note: 'use → @use annotation' },
        ],
        reverse: [
          { regex: /\baffirm\b/g,        repl: 'true',                          note: 'affirm → Rust true' },
          { regex: /\breject\b/g,        repl: 'false',                         note: 'reject → Rust false' },
          { regex: /\btend\b/g,          repl: 'None',                          note: 'tend → Rust None' },
          { regex: /\btrit\b/g,          repl: 'bool',                          note: 'trit → bool' },
          { regex: /\bconflict\(/g,      repl: 'panic!(',                       note: 'conflict → panic!' },
          { regex: /\bprintln\(/g,       repl: 'println!(',                     note: 'println → println!' },
          { regex: /\bprint\(/g,         repl: 'print!(',                       note: 'print → print!' },
          { regex: /\bagent\b/g,         repl: 'struct',                        note: 'agent → struct' },
          { regex: /match (.+?) \{/g,    repl: 'if $1 {',                       note: 'match → if' },
          { regex: /\} tend => \{/g,     repl: '} else {',                      note: 'tend → else' },
          { regex: /\breturn affirm;/g,  repl: 'return true;',                  note: 'return affirm → return true' },
          { regex: /\breturn reject;/g,  repl: 'return false;',                 note: 'return reject → return false' },
          { regex: /\breturn tend;/g,    repl: 'return None;',                  note: 'return tend → return None' },
          { regex: /\bint\b/g,           repl: 'i64',                           note: 'int → i64' },
          { regex: /\bfloat\b/g,         repl: 'f64',                           note: 'float → f64' },
          { regex: /\bstr\b/g,           repl: 'String',                        note: 'str → String' },
          { regex: /\/\/ @use /g,        repl: 'use ',                          note: '@use → use' },
        ]
      },
      java: {
        forward: [
          { regex: /\btrue\b/g,                  repl: 'affirm',                note: 'Java true → affirm' },
          { regex: /\bfalse\b/g,                 repl: 'reject',                note: 'Java false → reject' },
          { regex: /\bnull\b/g,                  repl: 'tend',                  note: 'Java null → tend' },
          { regex: /\bBoolean\b/g,               repl: 'trit',                  note: 'Boolean → trit' },
          { regex: /\bboolean\b/g,               repl: 'trit',                  note: 'boolean → trit' },
          { regex: /System\.out\.println\(/g,    repl: 'println(',              note: 'System.out.println → println' },
          { regex: /System\.out\.print\(/g,      repl: 'print(',                note: 'System.out.print → print' },
          { regex: /\binterface\b/g,             repl: 'agent',                 note: 'interface → agent' },
          { regex: /\bclass\b/g,                 repl: 'agent',                 note: 'class → agent' },
          { regex: /\bvoid\b/g,                  repl: 'fn',                    note: 'void → fn' },
          { regex: /if\s*\((.+?)\)\s*\{/g,      repl: 'match $1 {',            note: 'if-block → match block' },
          { regex: /\} else \{/g,                repl: '} tend => {',           note: 'else → tend branch' },
          { regex: /\breturn true;/g,            repl: 'return affirm;',        note: 'return true → return affirm' },
          { regex: /\breturn false;/g,           repl: 'return reject;',        note: 'return false → return reject' },
          { regex: /\breturn null;/g,            repl: 'return tend;',          note: 'return null → return tend' },
          { regex: /\bthrow\b/g,                 repl: 'conflict',              note: 'throw → conflict' },
          { regex: /\blong\b|\bshort\b|\bbyte\b/g, repl: 'int',                note: 'long/short/byte → int' },
          { regex: /\bdouble\b/g,                repl: 'float',                 note: 'double → float' },
          { regex: /\bString\b/g,                repl: 'str',                   note: 'String → str' },
          { regex: /^import\s+/gm,               repl: '// @import ',           note: 'import → @import annotation' },
          { regex: /\bnew\s+/g,                  repl: 'spawn ',                note: 'new → spawn' },
        ],
        reverse: [
          { regex: /\baffirm\b/g,        repl: 'true',                          note: 'affirm → Java true' },
          { regex: /\breject\b/g,        repl: 'false',                         note: 'reject → Java false' },
          { regex: /\btend\b/g,          repl: 'null',                          note: 'tend → Java null' },
          { regex: /\btrit\b/g,          repl: 'boolean',                       note: 'trit → boolean' },
          { regex: /\bprintln\(/g,       repl: 'System.out.println(',           note: 'println → System.out.println' },
          { regex: /\bprint\(/g,         repl: 'System.out.print(',             note: 'print → System.out.print' },
          { regex: /\bagent\b/g,         repl: 'class',                         note: 'agent → class' },
          { regex: /\bfn\b/g,            repl: 'void',                          note: 'fn → void' },
          { regex: /match (.+?) \{/g,    repl: 'if ($1) {',                     note: 'match → if' },
          { regex: /\} tend => \{/g,     repl: '} else {',                      note: 'tend → else' },
          { regex: /\breturn affirm;/g,  repl: 'return true;',                  note: 'return affirm → return true' },
          { regex: /\breturn reject;/g,  repl: 'return false;',                 note: 'return reject → return false' },
          { regex: /\breturn tend;/g,    repl: 'return null;',                  note: 'return tend → return null' },
          { regex: /\bconflict\b/g,      repl: 'throw new RuntimeException',    note: 'conflict → throw' },
          { regex: /\bfloat\b/g,         repl: 'double',                        note: 'float → double' },
          { regex: /\bstr\b/g,           repl: 'String',                        note: 'str → String' },
          { regex: /\/\/ @import /g,     repl: 'import ',                       note: '@import → import' },
          { regex: /\bspawn\s+/g,        repl: 'new ',                          note: 'spawn → new' },
        ]
      },
      cpp: {
        forward: [
          { regex: /\btrue\b/g,           repl: 'affirm',                       note: 'C++ true → affirm' },
          { regex: /\bfalse\b/g,          repl: 'reject',                       note: 'C++ false → reject' },
          { regex: /\bnullptr\b|\bNULL\b/g, repl: 'tend',                       note: 'nullptr/NULL → tend' },
          { regex: /\bbool\b/g,           repl: 'trit',                         note: 'bool → trit' },
          { regex: /std::cout\s*<<\s*(.+?)\s*<<\s*std::endl\s*;/g, repl: 'println($1);', note: 'std::cout → println' },
          { regex: /std::cout\s*<<\s*/g,  repl: 'print(',                       note: 'std::cout << → print(' },
          { regex: /\bstruct\b/g,         repl: 'agent',                        note: 'struct → agent' },
          { regex: /\bclass\b/g,          repl: 'agent',                        note: 'class → agent' },
          { regex: /\bvoid\b/g,           repl: 'fn',                           note: 'void → fn' },
          { regex: /if\s*\((.+?)\)\s*\{/g, repl: 'match $1 {',                 note: 'if-block → match block' },
          { regex: /\} else \{/g,         repl: '} tend => {',                  note: 'else → tend branch' },
          { regex: /\breturn true;/g,     repl: 'return affirm;',               note: 'return true → return affirm' },
          { regex: /\breturn false;/g,    repl: 'return reject;',               note: 'return false → return reject' },
          { regex: /\breturn nullptr;/g,  repl: 'return tend;',                 note: 'return nullptr → return tend' },
          { regex: /\bthrow\b/g,          repl: 'conflict',                     note: 'throw → conflict' },
          { regex: /\bint64_t\b|\buint64_t\b|\bint32_t\b|\buint32_t\b|\blong\b|\bshort\b/g, repl: 'int', note: 'integer types → int' },
          { regex: /\bdouble\b/g,         repl: 'float',                        note: 'double → float' },
          { regex: /\bstd::string\b/g,    repl: 'str',                          note: 'std::string → str' },
          { regex: /^#include/gm,         repl: '// @include',                  note: '#include → @include annotation' },
          { regex: /\bnew\s+/g,           repl: 'spawn ',                       note: 'new → spawn' },
          { regex: /\bdelete\s+/g,        repl: '// @free ',                    note: 'delete → @free annotation' },
        ],
        reverse: [
          { regex: /\baffirm\b/g,         repl: 'true',                         note: 'affirm → C++ true' },
          { regex: /\breject\b/g,         repl: 'false',                        note: 'reject → C++ false' },
          { regex: /\btend\b/g,           repl: 'nullptr',                      note: 'tend → nullptr' },
          { regex: /\btrit\b/g,           repl: 'bool',                         note: 'trit → bool' },
          { regex: /\bprintln\((.+?)\)/g, repl: 'std::cout << $1 << std::endl', note: 'println → std::cout' },
          { regex: /\bprint\(/g,          repl: 'std::cout << ',                note: 'print( → std::cout <<' },
          { regex: /\bagent\b/g,          repl: 'class',                        note: 'agent → class' },
          { regex: /\bfn\b/g,             repl: 'void',                         note: 'fn → void' },
          { regex: /match (.+?) \{/g,     repl: 'if ($1) {',                    note: 'match → if' },
          { regex: /\} tend => \{/g,      repl: '} else {',                     note: 'tend → else' },
          { regex: /\breturn affirm;/g,   repl: 'return true;',                 note: 'return affirm → return true' },
          { regex: /\breturn reject;/g,   repl: 'return false;',                note: 'return reject → return false' },
          { regex: /\breturn tend;/g,     repl: 'return nullptr;',              note: 'return tend → return nullptr' },
          { regex: /\bconflict\b/g,       repl: 'throw std::runtime_error',     note: 'conflict → throw' },
          { regex: /\bfloat\b/g,          repl: 'double',                       note: 'float → double' },
          { regex: /\bstr\b/g,            repl: 'std::string',                  note: 'str → std::string' },
          { regex: /\/\/ @include/g,      repl: '#include',                     note: '@include → #include' },
          { regex: /\bspawn\s+/g,         repl: 'new ',                         note: 'spawn → new' },
          { regex: /\/\/ @free /g,        repl: 'delete ',                      note: '@free → delete' },
        ]
      },
      javascript: {
        forward: [
          { regex: /\btrue\b/g,           repl: 'affirm',                       note: 'JS true → affirm' },
          { regex: /\bfalse\b/g,          repl: 'reject',                       note: 'JS false → reject' },
          { regex: /\bnull\b|\bundefined\b/g, repl: 'tend',                     note: 'null/undefined → tend' },
          { regex: /\bfunction\b/g,       repl: 'fn',                           note: 'function → fn' },
          { regex: /\bconst\b|\bvar\b/g,  repl: 'let',                          note: 'const/var → let' },
          { regex: /\bclass\b/g,          repl: 'agent',                        note: 'class → agent' },
          { regex: /console\.log\(/g,     repl: 'println(',                     note: 'console.log → println' },
          { regex: /console\.error\(/g,   repl: 'conflict(',                    note: 'console.error → conflict' },
          { regex: /if\s*\((.+?)\)\s*\{/g, repl: 'match $1 {',                 note: 'if-block → match block' },
          { regex: /\} else \{/g,         repl: '} tend => {',                  note: 'else → tend branch' },
          { regex: /\breturn true;/g,     repl: 'return affirm;',               note: 'return true → return affirm' },
          { regex: /\breturn false;/g,    repl: 'return reject;',               note: 'return false → return reject' },
          { regex: /\breturn null;|\breturn undefined;/g, repl: 'return tend;', note: 'return null/undefined → return tend' },
          { regex: /\bthrow\b/g,          repl: 'conflict',                     note: 'throw → conflict' },
          { regex: /===/g,                repl: '==',                           note: '=== → == (ternary equality)' },
          { regex: /!==/g,                repl: '!=',                           note: '!== → !=' },
          { regex: /\btypeof\b/g,         repl: 'type',                         note: 'typeof → type' },
          { regex: /\binstanceof\b/g,     repl: 'is',                           note: 'instanceof → is' },
          { regex: /\bnew\s+/g,           repl: 'spawn ',                       note: 'new → spawn' },
          { regex: /\basync\s+/g,         repl: '@async ',                      note: 'async → @async' },
          { regex: /^export\s+/gm,        repl: '@export ',                     note: 'export → @export' },
          { regex: /^import\s+/gm,        repl: '// @import ',                  note: 'import → @import annotation' },
        ],
        reverse: [
          { regex: /\baffirm\b/g,         repl: 'true',                         note: 'affirm → JS true' },
          { regex: /\breject\b/g,         repl: 'false',                        note: 'reject → JS false' },
          { regex: /\btend\b/g,           repl: 'null',                         note: 'tend → JS null' },
          { regex: /\bfn\b/g,             repl: 'function',                     note: 'fn → function' },
          { regex: /\bagent\b/g,          repl: 'class',                        note: 'agent → class' },
          { regex: /\bprintln\(/g,        repl: 'console.log(',                 note: 'println → console.log' },
          { regex: /\bconflict\(/g,       repl: 'console.error(',               note: 'conflict → console.error' },
          { regex: /match (.+?) \{/g,     repl: 'if ($1) {',                    note: 'match → if' },
          { regex: /\} tend => \{/g,      repl: '} else {',                     note: 'tend → else' },
          { regex: /\breturn affirm;/g,   repl: 'return true;',                 note: 'return affirm → return true' },
          { regex: /\breturn reject;/g,   repl: 'return false;',                note: 'return reject → return false' },
          { regex: /\breturn tend;/g,     repl: 'return null;',                 note: 'return tend → return null' },
          { regex: /\btype\b/g,           repl: 'typeof',                       note: 'type → typeof' },
          { regex: /\bis\b/g,             repl: 'instanceof',                   note: 'is → instanceof' },
          { regex: /\bspawn\s+/g,         repl: 'new ',                         note: 'spawn → new' },
          { regex: /@async\s+/g,          repl: 'async ',                       note: '@async → async' },
          { regex: /@export\s+/g,         repl: 'export ',                      note: '@export → export' },
          { regex: /\/\/ @import /g,      repl: 'import ',                      note: '@import → import' },
        ]
      }
    };

    swapBtn.addEventListener('click', () => {
      isTernToBinary = !isTernToBinary;
      const tempBuffer = transInput.value;
      transInput.value = transOutput.textContent;
      transOutput.textContent = tempBuffer;
      updateDirectionLabels();
      transInsight.innerHTML = `<div class="px-3 py-1.5 bg-studio_editor border border-studio_border rounded font-mono text-[13px] text-t_subtext">Vector polarity reversed. Awaiting input...</div>`;
    });

    transLang.addEventListener('change', updateDirectionLabels);
    updateDirectionLabels();

    function translate() {
      let code = transInput.value;
      if (!code.trim()) return;

      transBtn.disabled = true;
      const originalInner = transBtn.innerHTML;
      transBtn.innerHTML = `<svg class="w-5 h-5 shrink-0 animate-spin" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"></path></svg><span>Parsing...</span>`;

      const lang = transLang.value;
      const rules = LANG_RULES[lang] || LANG_RULES.javascript;
      const activeRules = isTernToBinary ? rules.reverse : rules.forward;
      const insights = new Set();
      let translated = code;

      activeRules.forEach(rule => {
        rule.regex.lastIndex = 0; // reset stateful /g regex
        const before = translated;
        translated = translated.replace(rule.regex, rule.repl);
        if (translated !== before) insights.add(rule.note);
      });

      // Language-aware header comment
      const langName = LANG_DISPLAY[lang] || lang;
      const commentChar = lang === 'python' ? '#' : '//';
      if (!isTernToBinary) {
        translated = `// TernLang BET VM — translated from ${langName}\n\n` + translated;
      } else {
        translated = `${commentChar} De-resolved from TernLang BET VM to ${langName}\n\n` + translated;
      }

      setTimeout(() => {
        transOutput.textContent = translated;
        if (insights.size > 0) {
          transInsight.innerHTML = Array.from(insights).map(note =>
            `<div class="px-3 py-1.5 bg-studio_editor border border-studio_border rounded flex items-center gap-2">
              <span class="w-1.5 h-1.5 rounded-full bg-t_teal"></span>
              <span class="text-t_text font-mono text-[13px]">${note}</span>
            </div>`
          ).join('');
        } else {
          transInsight.innerHTML = `<div class="px-3 py-1.5 bg-studio_editor border border-studio_border rounded font-mono text-[13px] text-t_text">System baseline logic detected. No morphing required.</div>`;
        }
        transBtn.disabled = false;
        transBtn.innerHTML = originalInner;
      }, 400);
    }

    transBtn.addEventListener('click', translate);

    const EXT_TO_LANG = { py:'python', rs:'rust', java:'java', cpp:'cpp', cc:'cpp', h:'cpp', js:'javascript', ts:'javascript', jsx:'javascript', tsx:'javascript' };

    transUpload.addEventListener('change', (e) => {
      const file = e.target.files[0];
      if (!file) return;
      const ext = file.name.split('.').pop().toLowerCase();
      const detected = EXT_TO_LANG[ext];
      if (detected) { transLang.value = detected; updateDirectionLabels(); }
      const reader = new FileReader();
      reader.onload = (ev) => { transInput.value = ev.target.result; translate(); };
      reader.readAsText(file);
    });
  </script>
</body>
</html>
