{ pkgs, grafana }: let
  swaps = [
    # grafana 13.3 dark theme: a class built from locals (o = gray10, l = gray15, d = blueDarkText, c = orangeDarkMain)
    [ ''canvas:"#111217"'' ''canvas:"#11111b"'' ]
    [ ''o="#181b1f",l="#22252b",d="#6e9fff",c="#ff9900"'' ''o="#1e1e2e",l="#313244",d="#cba6f7",c="#fab387"'' ]
    [ ''secondary={main:"#2c2f35",shade:"#383b42"'' ''secondary={main:"#45475a",shade:"#585b70"'' ]
    [ ''whiteBase="204, 204, 220"'' ''whiteBase="205, 214, 244"'' ]

    [ ''primary={main:"#3d71d9",text:d'' ''primary={main:"#cba6f7",text:d'' ]
    [ ''error={main:"#d10e5c",text:"#ff5286"}'' ''error={main:"#f38ba8",text:"#f38ba8"}'' ]
    [ ''success={main:"#1a7f4b",text:"#6ccf8e"}'' ''success={main:"#a6e3a1",text:"#a6e3a1"}'' ]
    [ ''warning={main:c,text:"#fbad37"}'' ''warning={main:c,text:"#fab387"}'' ]

    [ ''{color:"#73BF69",name:"green"'' ''{color:"#a6e3a1",name:"green"'' ]
    [ ''{color:"#F2495C",name:"red"'' ''{color:"#f38ba8",name:"red"'' ]
    [ ''{color:"#FADE2A",name:"yellow"'' ''{color:"#f9e2af",name:"yellow"'' ]
    [ ''{color:"#5794F2",name:"blue"'' ''{color:"#89b4fa",name:"blue"'' ]
    [ ''{color:"#FF9830",name:"orange"'' ''{color:"#fab387",name:"orange"'' ]
    [ ''{color:"#B877D9",name:"purple"'' ''{color:"#cba6f7",name:"purple"'' ]

    # hide the footer everywhere (two minified copies in 13.3)
    [ ''j="kiosk"===e;if(h||'' ''j="kiosk"===e;if(!0||'' ]
    [ ''x="kiosk"===e;if(v||'' ''x="kiosk"===e;if(!0||'' ]

    [ ''["#7EB26D","#EAB839","#6ED0E0","#EF843C","#E24D42","#1F78C1","#BA43A9","#705DA0","#508642","#CCA300","#447EBC","#C15C17","#890F02","#0A437C"''
      ''["#cba6f7","#89b4fa","#a6e3a1","#fab387","#f5c2e7","#94e2d5","#f9e2af","#89dceb","#f38ba8","#b4befe","#f2cdcd","#eba0ac","#74c7ec","#f5e0dc"'' ]
  ];
  esc = s: builtins.replaceStrings [ "\\" "/" "&" "[" "]" "." "*" "$" "^" ] [ "\\\\" "\\/" "\\&" "\\[" "\\]" "\\." "\\*" "\\$" "\\^" ] s;
  escR = s: builtins.replaceStrings [ "\\" "/" "&" ] [ "\\\\" "\\/" "\\&" ] s;
  script = pkgs.writeText "mocha.sed" (pkgs.lib.concatStrings (map (x: "s/${esc (builtins.elemAt x 0)}/${escR (builtins.elemAt x 1)}/g\n") swaps));
  patterns = pkgs.writeText "mocha.patterns" (pkgs.lib.concatStringsSep "\n" (map builtins.head swaps));
in pkgs.runCommand "grafana-public-mocha" { nativeBuildInputs = [ pkgs.jq pkgs.openssl ]; } ''
  cp -rs ${grafana}/share/grafana/public $out
  chmod -R u+w $out

  rm -rf $out/app && cp -r ${grafana}/share/grafana/public/app $out/app
  files=$(grep -RlF -f ${patterns} $out/build --include='*.js')
  [ -n "$files" ] || { echo "mocha: no palette files found"; exit 1; }
  for f in $files; do cp --remove-destination "$(readlink -f "$f")" "$f"; chmod u+w "$f"; sed -i -f ${script} "$f"; done
  while IFS= read -r p; do
    grep -RqF "$p" $out/build && { echo "mocha: unpatched: $p"; exit 1; }
    grep -rqF "$p" ${grafana}/share/grafana/public/build || { echo "mocha: pattern gone upstream: $p"; exit 1; }
  done < ${patterns}

  m=$out/build/assets-manifest.json
  cp --remove-destination "$(readlink -f $m)" $m; chmod u+w $m
  rt=$out/$(jq -r '."runtime.js".src | ltrimstr("public/")' $m)
  cp --remove-destination "$(readlink -f $rt)" $rt; chmod u+w $rt
  sha384() { echo "sha384-$(openssl dgst -sha384 -binary "$1" | base64 -w0)"; }
  rename() {
    local f=$1 old new
    old=$(basename "$f"); new="''${old%.js}.mocha$(sha256sum "$f" | head -c 8).js"
    mv "$f" "$out/build/$new"; rm -f "$f.map"
    jq --arg o "public/build/$old" --arg n "public/build/$new" \
       --arg s "$(sha384 "$out/build/$new") sha512-$(openssl dgst -sha512 -binary "$out/build/$new" | base64 -w0)" \
      'walk(if type == "object" and .src == $o then .src = $n | .integrity = $s
            elif type == "string" and . == $o then $n else . end)
       | with_entries(if .key == ($o | ltrimstr("public/build/")) then .key = ($n | ltrimstr("public/build/")) else . end)' \
      $m > $m.new && mv $m.new $m
    grep -qF "$old" $m && { echo "mocha: $old still in manifest"; exit 1; }
    echo "$new"
  }
  for f in $files; do
    old=$(basename "$f")
    if grep -qF "public/build/$old" $m; then rename "$f" >/dev/null; continue; fi
    id=''${old%%.*}; hash=''${old#*.}; hash=''${hash%.js}
    grep -qF "$id:\"$hash\"" $rt || { echo "mocha: $old neither in manifest nor runtime"; exit 1; }
    new=$(rename "$f"); nh=''${new#*.}; nh=''${nh%.js}
    sed -i "s/\([{,]\)$id:\"$hash\"/\1$id:\"$nh\"/; s#\([{,]\)$id:\"sha384-[^\"]*\"#\1$id:\"$(sha384 "$out/build/$new")\"#" $rt
    grep -qF "$id:\"$nh\"" $rt || { echo "mocha: runtime map not updated for $old"; exit 1; }
  done
  rename "$rt" >/dev/null

  cp --remove-destination "$(readlink -f $out/views/index.html)" $out/views/index.html
  chmod u+w $out/views/index.html
  sed -i 's/#111217/#11111b/g; s/#181b1f/#1e1e2e/g' $out/views/index.html
''
